NVIDIA OptiX 7.1 API nvidia_logo_transpbg.gif Up
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
optix_denoiser_tiling.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 NVIDIA Corporation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of NVIDIA CORPORATION nor the names of its
13  * contributors may be used to endorse or promote products derived
14  * from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
32 
33 #ifndef optix_denoiser_tiling_h
34 #define optix_denoiser_tiling_h
35 
36 
37 #include <optix.h>
38 
39 #include <algorithm>
40 #include <vector>
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
55 {
56  // input tile image
58 
59  // output tile image
61 
62  // overlap offsets, parameters for #optixUtilDenoiserInvoke
63  unsigned int inputOffsetX;
64  unsigned int inputOffsetY;
65 };
66 
73 inline unsigned int optixUtilGetPixelStride( const OptixImage2D& image )
74 {
75  unsigned int pixelStrideInBytes = image.pixelStrideInBytes;
76  if( pixelStrideInBytes == 0 )
77  {
78  switch( image.format )
79  {
81  pixelStrideInBytes = 3 * sizeof( short );
82  break;
84  pixelStrideInBytes = 4 * sizeof( short );
85  break;
87  pixelStrideInBytes = 3 * sizeof( float );
88  break;
90  pixelStrideInBytes = 4 * sizeof( float );
91  break;
93  pixelStrideInBytes = 3 * sizeof( char );
94  break;
96  pixelStrideInBytes = 4 * sizeof( char );
97  break;
98  }
99  }
100  return pixelStrideInBytes;
101 }
102 
113  const OptixImage2D& input,
114  const OptixImage2D& output,
115  unsigned int overlapWindowSizeInPixels,
116  unsigned int tileWidth,
117  unsigned int tileHeight,
118  std::vector<OptixUtilDenoiserImageTile>& tiles )
119 {
120  if( tileWidth == 0 || tileHeight == 0 )
122 
123  unsigned int inPixelStride = optixUtilGetPixelStride( input );
124  unsigned int outPixelStride = optixUtilGetPixelStride( output );
125 
126  int inp_w = std::min( tileWidth + 2 * overlapWindowSizeInPixels, input.width );
127  int inp_h = std::min( tileHeight + 2 * overlapWindowSizeInPixels, input.height );
128  int inp_y = 0, copied_y = 0;
129 
130  do
131  {
132  int inputOffsetY = inp_y == 0 ? 0 : std::max( (int)overlapWindowSizeInPixels, inp_h - ( (int)input.height - inp_y ) );
133  int copy_y = inp_y == 0 ? std::min( input.height, tileHeight + overlapWindowSizeInPixels ) :
134  std::min( tileHeight, input.height - copied_y );
135 
136  int inp_x = 0, copied_x = 0;
137  do
138  {
139  int inputOffsetX = inp_x == 0 ? 0 : std::max( (int)overlapWindowSizeInPixels, inp_w - ( (int)input.width - inp_x ) );
140  int copy_x = inp_x == 0 ? std::min( input.width, tileWidth + overlapWindowSizeInPixels ) :
141  std::min( tileWidth, input.width - copied_x );
142 
144  tile.input.data = input.data + ( inp_y - inputOffsetY ) * input.rowStrideInBytes
145  + ( inp_x - inputOffsetX ) * inPixelStride;
146  tile.input.width = inp_w;
147  tile.input.height = inp_h;
150  tile.input.format = input.format;
151 
152  tile.output.data = output.data + inp_y * output.rowStrideInBytes + inp_x * outPixelStride;
153  tile.output.width = copy_x;
154  tile.output.height = copy_y;
157  tile.output.format = output.format;
158 
159  tile.inputOffsetX = inputOffsetX;
160  tile.inputOffsetY = inputOffsetY;
161  tiles.push_back( tile );
162 
163  inp_x += inp_x == 0 ? tileWidth + overlapWindowSizeInPixels : tileWidth;
164  copied_x += copy_x;
165  } while( inp_x < static_cast<int>( input.width ) );
166 
167  inp_y += inp_y == 0 ? tileHeight + overlapWindowSizeInPixels : tileHeight;
168  copied_y += copy_y;
169  } while( inp_y < static_cast<int>( input.height ) );
170 
171  return OPTIX_SUCCESS;
172 }
173 
177 
184 
203  OptixDenoiser& denoiser,
204  CUstream stream,
205  const OptixDenoiserParams* params,
206  CUdeviceptr denoiserState,
207  size_t denoiserStateSizeInBytes,
208  const OptixImage2D* inputLayers,
209  unsigned int numInputLayers,
210  const OptixImage2D* outputLayer,
211  CUdeviceptr scratch,
212  size_t scratchSizeInBytes,
213  unsigned int overlapWindowSizeInPixels,
214  unsigned int tileWidth,
215  unsigned int tileHeight )
216 {
217  if( !inputLayers || !outputLayer )
219 
220  std::vector<std::vector<OptixUtilDenoiserImageTile>> tiles( numInputLayers );
221  for( unsigned int l = 0; l < numInputLayers; l++ )
222  if( const OptixResult res = optixUtilDenoiserSplitImage( inputLayers[l], *outputLayer, overlapWindowSizeInPixels,
223  tileWidth, tileHeight, tiles[l] ) )
224  return res;
225 
226  for( size_t t = 0; t < tiles[0].size(); t++ )
227  {
228  std::vector<OptixImage2D> tlayers;
229  for( int l = 0; l < static_cast<int>( numInputLayers ); l++ )
230  tlayers.push_back( ( tiles[l] )[t].input );
231 
232  if( const OptixResult res =
233  optixDenoiserInvoke( denoiser, stream, params, denoiserState, denoiserStateSizeInBytes, &tlayers[0],
234  numInputLayers, ( tiles[0] )[t].inputOffsetX, ( tiles[0] )[t].inputOffsetY,
235  &( tiles[0] )[t].output, scratch, scratchSizeInBytes ) )
236  return res;
237  }
238  return OPTIX_SUCCESS;
239 }
240  // end group optix_utilities
242 
243 #ifdef __cplusplus
244 }
245 #endif
246 
247 #endif // __optix_optix_stack_size_h__
248