NVIDIA OptiX 7.1 API nvidia_logo_transpbg.gif Up
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
optix_stubs.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_stubs_h__
34 #define __optix_optix_stubs_h__
35 
36 #include "optix_function_table.h"
37 
38 #ifdef _WIN32
39 #ifndef WIN32_LEAN_AND_MEAN
40 #define WIN32_LEAN_AND_MEAN 1
41 #endif
42 #include <windows.h>
43 // The cfgmgr32 header is necessary for interrogating driver information in the registry.
44 // For convenience the library is also linked in automatically using the #pragma command.
45 #include <cfgmgr32.h>
46 #pragma comment( lib, "Cfgmgr32.lib" )
47 #include <string.h>
48 #else
49 #include <dlfcn.h>
50 #endif
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 // The function table needs to be defined in exactly one translation unit. This can be
57 // achieved by including optix_function_table_definition.h in that translation unit.
59 
60 #ifdef _WIN32
61 static void* optixLoadWindowsDllFromName( const char* optixDllName )
62 {
63  void* handle = NULL;
64 
65 
66  // Get the size of the path first, then allocate
67  unsigned int size = GetSystemDirectoryA( NULL, 0 );
68  if( size == 0 )
69  {
70  // Couldn't get the system path size, so bail
71  return NULL;
72  }
73  size_t pathSize = size + 1 + strlen( optixDllName );
74  char* systemPath = (char*)malloc( pathSize );
75  if( GetSystemDirectoryA( systemPath, size ) != size - 1 )
76  {
77  // Something went wrong
78  free( systemPath );
79  return NULL;
80  }
81  strcat( systemPath, "\\" );
82  strcat( systemPath, optixDllName );
83  handle = LoadLibraryA( systemPath );
84  free( systemPath );
85  if( handle )
86  return handle;
87 
88  // If we didn't find it, go looking in the register store. Since nvoptix.dll doesn't
89  // have its own registry entry, we are going to look for the opengl driver which lives
90  // next to nvoptix.dll. 0 (null) will be returned if any errors occured.
91 
92  static const char* deviceInstanceIdentifiersGUID = "{4d36e968-e325-11ce-bfc1-08002be10318}";
93  const ULONG flags = CM_GETIDLIST_FILTER_CLASS | CM_GETIDLIST_FILTER_PRESENT;
94  ULONG deviceListSize = 0;
95  if( CM_Get_Device_ID_List_SizeA( &deviceListSize, deviceInstanceIdentifiersGUID, flags ) != CR_SUCCESS )
96  {
97  return NULL;
98  }
99  char* deviceNames = (char*)malloc( deviceListSize );
100  if( CM_Get_Device_ID_ListA( deviceInstanceIdentifiersGUID, deviceNames, deviceListSize, flags ) )
101  {
102  free( deviceNames );
103  return NULL;
104  }
105  DEVINST devID = 0;
106  char* dllPath = 0;
107 
108  // Continue to the next device if errors are encountered.
109  for( char* deviceName = deviceNames; *deviceName; deviceName += strlen( deviceName ) + 1 )
110  {
111  if( CM_Locate_DevNodeA( &devID, deviceName, CM_LOCATE_DEVNODE_NORMAL ) != CR_SUCCESS )
112  {
113  continue;
114  }
115  HKEY regKey = 0;
116  if( CM_Open_DevNode_Key( devID, KEY_QUERY_VALUE, 0, RegDisposition_OpenExisting, &regKey, CM_REGISTRY_SOFTWARE ) != CR_SUCCESS )
117  {
118  continue;
119  }
120  const char* valueName = "OpenGLDriverName";
121  DWORD valueSize = 0;
122  LSTATUS ret = RegQueryValueExA( regKey, valueName, NULL, NULL, NULL, &valueSize );
123  if( ret != ERROR_SUCCESS )
124  {
125  RegCloseKey( regKey );
126  continue;
127  }
128  char* regValue = (char*)malloc( valueSize );
129  ret = RegQueryValueExA( regKey, valueName, NULL, NULL, (LPBYTE)regValue, &valueSize );
130  if( ret != ERROR_SUCCESS )
131  {
132  free( regValue );
133  RegCloseKey( regKey );
134  continue;
135  }
136  // Strip the opengl driver dll name from the string then create a new string with
137  // the path and the nvoptix.dll name
138  for( int i = valueSize - 1; i >= 0 && regValue[i] != '\\'; --i )
139  regValue[i] = '\0';
140  size_t newPathSize = strlen( regValue ) + strlen( optixDllName ) + 1;
141  dllPath = (char*)malloc( newPathSize );
142  strcpy( dllPath, regValue );
143  strcat( dllPath, optixDllName );
144  free( regValue );
145  RegCloseKey( regKey );
146  handle = LoadLibraryA( (LPCSTR)dllPath );
147  free( dllPath );
148  if( handle )
149  break;
150  }
151  free( deviceNames );
152  return handle;
153 }
154 
155 static void* optixLoadWindowsDll( )
156 {
157  return optixLoadWindowsDllFromName( "nvoptix.dll" );
158 }
159 #endif
160 
163 
168 inline OptixResult optixInitWithHandle( void** handlePtr )
172 {
173  // Make sure these functions get initialized to zero in case the DLL and function
174  // table can't be loaded
175  g_optixFunctionTable.optixGetErrorName = 0;
176  g_optixFunctionTable.optixGetErrorString = 0;
177 
178  if( !handlePtr )
180 
181 #ifdef _WIN32
182  *handlePtr = optixLoadWindowsDll();
183  if( !*handlePtr )
185 
186  void* symbol = GetProcAddress( (HMODULE)*handlePtr, "optixQueryFunctionTable" );
187  if( !symbol )
189 #else
190  *handlePtr = dlopen( "libnvoptix.so.1", RTLD_NOW );
191  if( !*handlePtr )
193 
194  void* symbol = dlsym( *handlePtr, "optixQueryFunctionTable" );
195  if( !symbol )
197 #endif
198 
199  OptixQueryFunctionTable_t* optixQueryFunctionTable = (OptixQueryFunctionTable_t*)symbol;
200 
201  return optixQueryFunctionTable( OPTIX_ABI_VERSION, 0, 0, 0, &g_optixFunctionTable, sizeof( g_optixFunctionTable ) );
202 }
203 
207 inline OptixResult optixInit( void )
208 {
209  void* handle;
210  return optixInitWithHandle( &handle );
211 }
212  // end group optix_utilities
214 
215 #ifndef OPTIX_DOXYGEN_SHOULD_SKIP_THIS
216 
217 // Stub functions that forward calls to the corresponding function pointer in the function table.
218 
219 inline const char* optixGetErrorName( OptixResult result )
220 {
221  if( g_optixFunctionTable.optixGetErrorName )
222  return g_optixFunctionTable.optixGetErrorName( result );
223 
224  // If the DLL and symbol table couldn't be loaded, provide a set of error strings
225  // suitable for processing errors related to the DLL loading.
226  switch( result )
227  {
228  case OPTIX_SUCCESS:
229  return "OPTIX_SUCCESS";
231  return "OPTIX_ERROR_INVALID_VALUE";
233  return "OPTIX_ERROR_UNSUPPORTED_ABI_VERSION";
235  return "OPTIX_ERROR_FUNCTION_TABLE_SIZE_MISMATCH";
237  return "OPTIX_ERROR_INVALID_ENTRY_FUNCTION_OPTIONS";
239  return "OPTIX_ERROR_LIBRARY_NOT_FOUND";
241  return "OPTIX_ERROR_ENTRY_SYMBOL_NOT_FOUND";
242  default:
243  return "Unknown OptixResult code";
244  }
245 }
246 
247 inline const char* optixGetErrorString( OptixResult result )
248 {
249  if( g_optixFunctionTable.optixGetErrorString )
250  return g_optixFunctionTable.optixGetErrorString( result );
251 
252  // If the DLL and symbol table couldn't be loaded, provide a set of error strings
253  // suitable for processing errors related to the DLL loading.
254  switch( result )
255  {
256  case OPTIX_SUCCESS:
257  return "Success";
259  return "Invalid value";
261  return "Unsupported ABI version";
263  return "Function table size mismatch";
265  return "Invalid options to entry function";
267  return "Library not found";
269  return "Entry symbol not found";
270  default:
271  return "Unknown OptixResult code";
272  }
273 }
274 
275 inline OptixResult optixDeviceContextCreate( CUcontext fromContext, const OptixDeviceContextOptions* options, OptixDeviceContext* context )
276 {
277  return g_optixFunctionTable.optixDeviceContextCreate( fromContext, options, context );
278 }
279 
281 {
282  return g_optixFunctionTable.optixDeviceContextDestroy( context );
283 }
284 
285 inline OptixResult optixDeviceContextGetProperty( OptixDeviceContext context, OptixDeviceProperty property, void* value, size_t sizeInBytes )
286 {
287  return g_optixFunctionTable.optixDeviceContextGetProperty( context, property, value, sizeInBytes );
288 }
289 
291  OptixLogCallback callbackFunction,
292  void* callbackData,
293  unsigned int callbackLevel )
294 {
295  return g_optixFunctionTable.optixDeviceContextSetLogCallback( context, callbackFunction, callbackData, callbackLevel );
296 }
297 
299 {
300  return g_optixFunctionTable.optixDeviceContextSetCacheEnabled( context, enabled );
301 }
302 
303 inline OptixResult optixDeviceContextSetCacheLocation( OptixDeviceContext context, const char* location )
304 {
305  return g_optixFunctionTable.optixDeviceContextSetCacheLocation( context, location );
306 }
307 
308 inline OptixResult optixDeviceContextSetCacheDatabaseSizes( OptixDeviceContext context, size_t lowWaterMark, size_t highWaterMark )
309 {
310  return g_optixFunctionTable.optixDeviceContextSetCacheDatabaseSizes( context, lowWaterMark, highWaterMark );
311 }
312 
314 {
315  return g_optixFunctionTable.optixDeviceContextGetCacheEnabled( context, enabled );
316 }
317 
318 inline OptixResult optixDeviceContextGetCacheLocation( OptixDeviceContext context, char* location, size_t locationSize )
319 {
320  return g_optixFunctionTable.optixDeviceContextGetCacheLocation( context, location, locationSize );
321 }
322 
323 inline OptixResult optixDeviceContextGetCacheDatabaseSizes( OptixDeviceContext context, size_t* lowWaterMark, size_t* highWaterMark )
324 {
325  return g_optixFunctionTable.optixDeviceContextGetCacheDatabaseSizes( context, lowWaterMark, highWaterMark );
326 }
327 
329  const OptixModuleCompileOptions* moduleCompileOptions,
330  const OptixPipelineCompileOptions* pipelineCompileOptions,
331  const char* PTX,
332  size_t PTXsize,
333  char* logString,
334  size_t* logStringSize,
335  OptixModule* module )
336 {
337  return g_optixFunctionTable.optixModuleCreateFromPTX( context, moduleCompileOptions, pipelineCompileOptions, PTX,
338  PTXsize, logString, logStringSize, module );
339 }
340 
342 {
343  return g_optixFunctionTable.optixModuleDestroy( module );
344 }
345 
347  const OptixModuleCompileOptions* moduleCompileOptions,
348  const OptixPipelineCompileOptions* pipelineCompileOptions,
349  const OptixBuiltinISOptions* builtinISOptions,
350  OptixModule* builtinModule )
351 {
352  return g_optixFunctionTable.optixBuiltinISModuleGet( context, moduleCompileOptions, pipelineCompileOptions,
353  builtinISOptions, builtinModule );
354 }
355 
357  const OptixProgramGroupDesc* programDescriptions,
358  unsigned int numProgramGroups,
359  const OptixProgramGroupOptions* options,
360  char* logString,
361  size_t* logStringSize,
362  OptixProgramGroup* programGroups )
363 {
364  return g_optixFunctionTable.optixProgramGroupCreate( context, programDescriptions, numProgramGroups, options,
365  logString, logStringSize, programGroups );
366 }
367 
369 {
370  return g_optixFunctionTable.optixProgramGroupDestroy( programGroup );
371 }
372 
374 {
375  return g_optixFunctionTable.optixProgramGroupGetStackSize( programGroup, stackSizes );
376 }
377 
379  const OptixPipelineCompileOptions* pipelineCompileOptions,
380  const OptixPipelineLinkOptions* pipelineLinkOptions,
381  const OptixProgramGroup* programGroups,
382  unsigned int numProgramGroups,
383  char* logString,
384  size_t* logStringSize,
385  OptixPipeline* pipeline )
386 {
387  return g_optixFunctionTable.optixPipelineCreate( context, pipelineCompileOptions, pipelineLinkOptions, programGroups,
388  numProgramGroups, logString, logStringSize, pipeline );
389 }
390 
392 {
393  return g_optixFunctionTable.optixPipelineDestroy( pipeline );
394 }
395 
397  unsigned int directCallableStackSizeFromTraversal,
398  unsigned int directCallableStackSizeFromState,
399  unsigned int continuationStackSize,
400  unsigned int maxTraversableGraphDepth )
401 {
402  return g_optixFunctionTable.optixPipelineSetStackSize( pipeline, directCallableStackSizeFromTraversal, directCallableStackSizeFromState,
403  continuationStackSize, maxTraversableGraphDepth );
404 }
405 
407  const OptixAccelBuildOptions* accelOptions,
408  const OptixBuildInput* buildInputs,
409  unsigned int numBuildInputs,
410  OptixAccelBufferSizes* bufferSizes )
411 {
412  return g_optixFunctionTable.optixAccelComputeMemoryUsage( context, accelOptions, buildInputs, numBuildInputs, bufferSizes );
413 }
414 
416  CUstream stream,
417  const OptixAccelBuildOptions* accelOptions,
418  const OptixBuildInput* buildInputs,
419  unsigned int numBuildInputs,
420  CUdeviceptr tempBuffer,
421  size_t tempBufferSizeInBytes,
422  CUdeviceptr outputBuffer,
423  size_t outputBufferSizeInBytes,
424  OptixTraversableHandle* outputHandle,
425  const OptixAccelEmitDesc* emittedProperties,
426  unsigned int numEmittedProperties )
427 {
428  return g_optixFunctionTable.optixAccelBuild( context, stream, accelOptions, buildInputs, numBuildInputs, tempBuffer,
429  tempBufferSizeInBytes, outputBuffer, outputBufferSizeInBytes,
430  outputHandle, emittedProperties, numEmittedProperties );
431 }
432 
433 
435 {
436  return g_optixFunctionTable.optixAccelGetRelocationInfo( context, handle, info );
437 }
438 
439 
441 {
442  return g_optixFunctionTable.optixAccelCheckRelocationCompatibility( context, info, compatible );
443 }
444 
446  CUstream stream,
447  const OptixAccelRelocationInfo* info,
448  CUdeviceptr instanceTraversableHandles,
449  size_t numInstanceTraversableHandles,
450  CUdeviceptr targetAccel,
451  size_t targetAccelSizeInBytes,
452  OptixTraversableHandle* targetHandle )
453 {
454  return g_optixFunctionTable.optixAccelRelocate( context, stream, info, instanceTraversableHandles, numInstanceTraversableHandles,
455  targetAccel, targetAccelSizeInBytes, targetHandle );
456 }
457 
459  CUstream stream,
460  OptixTraversableHandle inputHandle,
461  CUdeviceptr outputBuffer,
462  size_t outputBufferSizeInBytes,
463  OptixTraversableHandle* outputHandle )
464 {
465  return g_optixFunctionTable.optixAccelCompact( context, stream, inputHandle, outputBuffer, outputBufferSizeInBytes, outputHandle );
466 }
467 
469  CUdeviceptr pointer,
470  OptixTraversableType traversableType,
471  OptixTraversableHandle* traversableHandle )
472 {
473  return g_optixFunctionTable.optixConvertPointerToTraversableHandle( onDevice, pointer, traversableType, traversableHandle );
474 }
475 
476 inline OptixResult optixSbtRecordPackHeader( OptixProgramGroup programGroup, void* sbtRecordHeaderHostPointer )
477 {
478  return g_optixFunctionTable.optixSbtRecordPackHeader( programGroup, sbtRecordHeaderHostPointer );
479 }
480 
481 inline OptixResult optixLaunch( OptixPipeline pipeline,
482  CUstream stream,
483  CUdeviceptr pipelineParams,
484  size_t pipelineParamsSize,
485  const OptixShaderBindingTable* sbt,
486  unsigned int width,
487  unsigned int height,
488  unsigned int depth )
489 {
490  return g_optixFunctionTable.optixLaunch( pipeline, stream, pipelineParams, pipelineParamsSize, sbt, width, height, depth );
491 }
492 
493 inline OptixResult optixDenoiserCreate( OptixDeviceContext context, const OptixDenoiserOptions* options, OptixDenoiser* returnHandle )
494 {
495  return g_optixFunctionTable.optixDenoiserCreate( context, options, returnHandle );
496 }
497 
499 {
500  return g_optixFunctionTable.optixDenoiserDestroy( handle );
501 }
502 
504  unsigned int maximumInputWidth,
505  unsigned int maximumInputHeight,
506  OptixDenoiserSizes* returnSizes )
507 {
508  return g_optixFunctionTable.optixDenoiserComputeMemoryResources( handle, maximumInputWidth, maximumInputHeight, returnSizes );
509 }
510 
512  CUstream stream,
513  unsigned int inputWidth,
514  unsigned int inputHeight,
515  CUdeviceptr denoiserState,
516  size_t denoiserStateSizeInBytes,
517  CUdeviceptr scratch,
518  size_t scratchSizeInBytes )
519 {
520  return g_optixFunctionTable.optixDenoiserSetup( denoiser, stream, inputWidth, inputHeight, denoiserState,
521  denoiserStateSizeInBytes, scratch, scratchSizeInBytes );
522 }
523 
525  CUstream stream,
526  const OptixDenoiserParams* params,
527  CUdeviceptr denoiserData,
528  size_t denoiserDataSize,
529  const OptixImage2D* inputLayers,
530  unsigned int numInputLayers,
531  unsigned int inputOffsetX,
532  unsigned int inputOffsetY,
533  const OptixImage2D* outputLayer,
534  CUdeviceptr scratch,
535  size_t scratchSizeInBytes )
536 {
537  return g_optixFunctionTable.optixDenoiserInvoke( handle, stream, params, denoiserData, denoiserDataSize,
538  inputLayers, numInputLayers, inputOffsetX, inputOffsetY,
539  outputLayer, scratch, scratchSizeInBytes );
540 }
541 
542 inline OptixResult optixDenoiserSetModel( OptixDenoiser handle, OptixDenoiserModelKind kind, void* data, size_t sizeInBytes )
543 {
544  return g_optixFunctionTable.optixDenoiserSetModel( handle, kind, data, sizeInBytes );
545 }
546 
548  CUstream stream,
549  const OptixImage2D* inputImage,
550  CUdeviceptr outputIntensity,
551  CUdeviceptr scratch,
552  size_t scratchSizeInBytes )
553 {
554  return g_optixFunctionTable.optixDenoiserComputeIntensity( handle, stream, inputImage, outputIntensity, scratch, scratchSizeInBytes );
555 }
556 
557 #endif // OPTIX_DOXYGEN_SHOULD_SKIP_THIS
558 
559 #ifdef __cplusplus
560 }
561 #endif
562 
563 #endif // __optix_optix_stubs_h__
564