NVIDIA OptiX 7.1 API nvidia_logo_transpbg.gif Up
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
optix_stack_size.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_optix_stack_size_h__
34 #define __optix_optix_stack_size_h__
35 
36 #include "optix.h"
37 
38 #include <algorithm>
39 #include <cstring>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
53 {
54  if( !stackSizes )
56 
57  OptixStackSizes localStackSizes;
58  OptixResult result = optixProgramGroupGetStackSize( programGroup, &localStackSizes );
59  if( result != OPTIX_SUCCESS )
60  return result;
61 
62  stackSizes->cssRG = std::max( stackSizes->cssRG, localStackSizes.cssRG );
63  stackSizes->cssMS = std::max( stackSizes->cssMS, localStackSizes.cssMS );
64  stackSizes->cssCH = std::max( stackSizes->cssCH, localStackSizes.cssCH );
65  stackSizes->cssAH = std::max( stackSizes->cssAH, localStackSizes.cssAH );
66  stackSizes->cssIS = std::max( stackSizes->cssIS, localStackSizes.cssIS );
67  stackSizes->cssCC = std::max( stackSizes->cssCC, localStackSizes.cssCC );
68  stackSizes->dssDC = std::max( stackSizes->dssDC, localStackSizes.dssDC );
69 
70  return OPTIX_SUCCESS;
71 }
72 
87  unsigned int maxTraceDepth,
88  unsigned int maxCCDepth,
89  unsigned int maxDCDepth,
90  unsigned int* directCallableStackSizeFromTraversal,
91  unsigned int* directCallableStackSizeFromState,
92  unsigned int* continuationStackSize )
93 {
94  if( !stackSizes )
96 
97  const unsigned int cssRG = stackSizes->cssRG;
98  const unsigned int cssMS = stackSizes->cssMS;
99  const unsigned int cssCH = stackSizes->cssCH;
100  const unsigned int cssAH = stackSizes->cssAH;
101  const unsigned int cssIS = stackSizes->cssIS;
102  const unsigned int cssCC = stackSizes->cssCC;
103  const unsigned int dssDC = stackSizes->dssDC;
104 
105  if( directCallableStackSizeFromTraversal )
106  *directCallableStackSizeFromTraversal = maxDCDepth * dssDC;
107  if( directCallableStackSizeFromState )
108  *directCallableStackSizeFromState = maxDCDepth * dssDC;
109 
110  // upper bound on continuation stack used by call trees of continuation callables
111  unsigned int cssCCTree = maxCCDepth * cssCC;
112 
113  // upper bound on continuation stack used by CH or MS programs including the call tree of
114  // continuation callables
115  unsigned int cssCHOrMSPlusCCTree = std::max( cssCH, cssMS ) + cssCCTree;
116 
117  // clang-format off
118  if( continuationStackSize )
119  *continuationStackSize
120  = cssRG + cssCCTree
121  + ( std::max( maxTraceDepth, 1u ) - 1 ) * cssCHOrMSPlusCCTree
122  + std::min( maxTraceDepth, 1u ) * std::max( cssCHOrMSPlusCCTree, cssIS + cssAH );
123  // clang-format on
124 
125  return OPTIX_SUCCESS;
126 }
127 
152  unsigned int dssDCFromTraversal,
153  unsigned int dssDCFromState,
154  unsigned int maxTraceDepth,
155  unsigned int maxCCDepth,
156  unsigned int maxDCDepthFromTraversal,
157  unsigned int maxDCDepthFromState,
158  unsigned int* directCallableStackSizeFromTraversal,
159  unsigned int* directCallableStackSizeFromState,
160  unsigned int* continuationStackSize )
161 {
162  if( !stackSizes )
164 
165  const unsigned int cssRG = stackSizes->cssRG;
166  const unsigned int cssMS = stackSizes->cssMS;
167  const unsigned int cssCH = stackSizes->cssCH;
168  const unsigned int cssAH = stackSizes->cssAH;
169  const unsigned int cssIS = stackSizes->cssIS;
170  const unsigned int cssCC = stackSizes->cssCC;
171  // use dssDCFromTraversal and dssDCFromState instead of stackSizes->dssDC
172 
173  if( directCallableStackSizeFromTraversal )
174  *directCallableStackSizeFromTraversal = maxDCDepthFromTraversal * dssDCFromTraversal;
175  if( directCallableStackSizeFromState )
176  *directCallableStackSizeFromState = maxDCDepthFromState * dssDCFromState;
177 
178  // upper bound on continuation stack used by call trees of continuation callables
179  unsigned int cssCCTree = maxCCDepth * cssCC;
180 
181  // upper bound on continuation stack used by CH or MS programs including the call tree of
182  // continuation callables
183  unsigned int cssCHOrMSPlusCCTree = std::max( cssCH, cssMS ) + cssCCTree;
184 
185  // clang-format off
186  if( continuationStackSize )
187  *continuationStackSize
188  = cssRG + cssCCTree
189  + ( std::max( maxTraceDepth, 1u ) - 1 ) * cssCHOrMSPlusCCTree
190  + std::min( maxTraceDepth, 1u ) * std::max( cssCHOrMSPlusCCTree, cssIS + cssAH );
191  // clang-format on
192 
193  return OPTIX_SUCCESS;
194 }
195 
213  unsigned int cssCCTree,
214  unsigned int maxTraceDepth,
215  unsigned int maxDCDepth,
216  unsigned int* directCallableStackSizeFromTraversal,
217  unsigned int* directCallableStackSizeFromState,
218  unsigned int* continuationStackSize )
219 {
220  if( !stackSizes )
222 
223  const unsigned int cssRG = stackSizes->cssRG;
224  const unsigned int cssMS = stackSizes->cssMS;
225  const unsigned int cssCH = stackSizes->cssCH;
226  const unsigned int cssAH = stackSizes->cssAH;
227  const unsigned int cssIS = stackSizes->cssIS;
228  // use cssCCTree instead of stackSizes->cssCC and maxCCDepth
229  const unsigned int dssDC = stackSizes->dssDC;
230 
231  if( directCallableStackSizeFromTraversal )
232  *directCallableStackSizeFromTraversal = maxDCDepth * dssDC;
233  if( directCallableStackSizeFromState )
234  *directCallableStackSizeFromState = maxDCDepth * dssDC;
235 
236  // upper bound on continuation stack used by CH or MS programs including the call tree of
237  // continuation callables
238  unsigned int cssCHOrMSPlusCCTree = std::max( cssCH, cssMS ) + cssCCTree;
239 
240  // clang-format off
241  if( continuationStackSize )
242  *continuationStackSize
243  = cssRG + cssCCTree
244  + ( std::max( maxTraceDepth, 1u ) - 1 ) * cssCHOrMSPlusCCTree
245  + std::min( maxTraceDepth, 1u ) * std::max( cssCHOrMSPlusCCTree, cssIS + cssAH );
246  // clang-format on
247 
248  return OPTIX_SUCCESS;
249 }
250 
264  OptixProgramGroup programGroupMS1,
265  const OptixProgramGroup* programGroupCH1,
266  unsigned int programGroupCH1Count,
267  OptixProgramGroup programGroupMS2,
268  const OptixProgramGroup* programGroupCH2,
269  unsigned int programGroupCH2Count,
270  unsigned int* directCallableStackSizeFromTraversal,
271  unsigned int* directCallableStackSizeFromState,
272  unsigned int* continuationStackSize )
273 {
274  if( !programGroupCH1 && ( programGroupCH1Count > 0 ) )
276  if( !programGroupCH2 && ( programGroupCH2Count > 0 ) )
278 
279  OptixResult result;
280 
281  OptixStackSizes stackSizesRG = {};
282  result = optixProgramGroupGetStackSize( programGroupRG, &stackSizesRG );
283  if( result != OPTIX_SUCCESS )
284  return result;
285 
286  OptixStackSizes stackSizesMS1 = {};
287  result = optixProgramGroupGetStackSize( programGroupMS1, &stackSizesMS1 );
288  if( result != OPTIX_SUCCESS )
289  return result;
290 
291  OptixStackSizes stackSizesCH1 = {};
292  for( unsigned int i = 0; i < programGroupCH1Count; ++i )
293  {
294  result = optixUtilAccumulateStackSizes( programGroupCH1[i], &stackSizesCH1 );
295  if( result != OPTIX_SUCCESS )
296  return result;
297  }
298 
299  OptixStackSizes stackSizesMS2 = {};
300  result = optixProgramGroupGetStackSize( programGroupMS2, &stackSizesMS2 );
301  if( result != OPTIX_SUCCESS )
302  return result;
303 
304  OptixStackSizes stackSizesCH2 = {};
305  memset( &stackSizesCH2, 0, sizeof( OptixStackSizes ) );
306  for( unsigned int i = 0; i < programGroupCH2Count; ++i )
307  {
308  result = optixUtilAccumulateStackSizes( programGroupCH2[i], &stackSizesCH2 );
309  if( result != OPTIX_SUCCESS )
310  return result;
311  }
312 
313  const unsigned int cssRG = stackSizesRG.cssRG;
314  const unsigned int cssMS1 = stackSizesMS1.cssMS;
315  const unsigned int cssCH1 = stackSizesCH1.cssCH;
316  const unsigned int cssMS2 = stackSizesMS2.cssMS;
317  const unsigned int cssCH2 = stackSizesCH2.cssCH;
318  // no AH, IS, CC, or DC programs
319 
320  if( directCallableStackSizeFromTraversal )
321  *directCallableStackSizeFromTraversal = 0;
322  if( directCallableStackSizeFromState )
323  *directCallableStackSizeFromState = 0;
324 
325  if( continuationStackSize )
326  *continuationStackSize = cssRG + std::max( cssMS1, cssCH1 + std::max( cssMS2, cssCH2 ) );
327 
328  return OPTIX_SUCCESS;
329 }
330  // end group optix_utilities
332 
333 #ifdef __cplusplus
334 }
335 #endif
336 
337 #endif // __optix_optix_stack_size_h__
338