OptiX 7 excute general cuda program

I want to use the general cuda program in my optix project. For example, do some pre-processing or post-processing.

I refer to the official sample optixRaycasting and change my CMakelists.txt from

OPTIX_add_sample_executable( GRenderer target_name
  ${HEADERS}
  ${SOURCES}
  ${SOURCES_CUDA}

  OPTIONS -rdc true
  )

to

set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/misc/cuda_tools.cu
  PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ
  )

OPTIX_add_sample_executable( GRenderer target_name
  ${HEADERS}
  ${SOURCES}
  ${SOURCES_CUDA}

  # OPTIONS -rdc true
  )

It works well when I don’t have callables.

However, it gets an error when I have some callables, for example, callables used for BSDF sampling

Caught exception: OPTIX_ERROR_INVALID_PTX: Optix call 'optixModuleCreateFromPTX( m_scene.getContext(), &module_compile_options, &m_pipeline_compile_options, ptx.c_str(), ptx.length(), log, &sizeof_log, &m_bsdf_modules[BSDF_DISNEY] )' failed: C:\Users\lyf\Desktop\GRenderer\GRenderer\kernel\pathTracing.cpp:152)
Log:
COMPILE ERROR: No functions with semantic types found

Note that there is no error reported before I modified CMakelists.txt.

I feel quite confused about the error and hope for help!

Hi yetsun!

Your CMakeLists.txt file looks fine. I don’t know what the problem is yet, but I suspect it might have to do with your #include structure. Unfortunately, you can’t mix OptiX PTX with CUDA PTX, and you can’t compile them together. Is your CUDA code including OptiX callables, or are you trying to compile your CUDA code using optixModuleCreateFromPTX()?

You’ll need to make sure that your OptiX code follows OptiX conventions and is launched using optixLaunch(), and make sure your CUDA code follows CUDA conventions and doesn’t include any OptiX specific features or code, and should be launched with a CUDA lauch, for example using the triple-chevron operator (<<<>>>). You can use shared header files with defines and shared helper functions, you just need to make sure your shared helper functions are all inlined on both sides.


David.

Out of curiosity, why did you remove the relocatable device code flag? Without this flag all non-global device functions which are not used in the file may be removed in the compiled output. This flag tells the compiler to keep around all functions for usage in linking.

In short, without this flag your callable programs (which are probably not declared global) are being elided and therefore dont make it into the output PTX and then are not found when you try to load them in a module.

It works well if there does not exist OptiX callables so I think there is no problem with my CUDA code. The OptiX PTX is compiled with optixModuleCreateFromPTX() and the CUDA code seems not to be compiled as PTX but an OBJ file, which is the result of my modified CMakeLists.txt.(as the optix sample optixRaycasting does)

I think this may be the reason that my code runs into an error. I removed the relocatable device code flag as the optix sample optixRaycasting does and actually I do not quite understand the meaning XD. And this configuration does work when no callable programs exist.

If set rdc as true, I get an error when building this project:

GRenderer_generated_cuda_tools.cu.obj : error LNK2019: unresolved external symbol __cudaRegisterLinkedBinary_46_tmpxft_00002854_00000000_10_cuda_tools_cpp1_ii_15d072f1 referenced in function "void __cdecl __nv_cudaEntityRegisterCallback(void * *)" (?__nv_cudaEntityRegisterCallback@@YAXPEAPEAX@Z)
C:\Users\lyf\Desktop\GRenderer\build\bin\Debug\GRenderer.exe : fatal error LNK1120: 1 unresolved externals

So must the relocatable device code flag be removed when I have general CUDA code to execute? If not, how to resolve the building error reported above?

I am not sure why the rdc flag needs to be removed when compiling pure cuda. However, if that is the case, then I would break your cuda code into two files – one with -rdc which contains your callable program and a second which has your pure cuda code and no -rdc flag.