How to include CuOmmBaking with minimum CMake config?

I am trying to include OmmBaking from optix-toolkit GitHub - NVIDIA/optix-toolkit: Set of utilities supporting workflows common in GPU raytracing applications
However, there is no individual cmake doc for each of them. Without luck, I have to look into the cmake file.
You guys are using 3 different deps for ommBakingSimple example. That make me confused, are they madatory.

target_link_libraries( ommBakingSimple
  OptiXToolkit::CuOmmBaking
  OptiXToolkit::Util
  OptiX::OptiX
)

Then I started to try it in my cmake. The OptiXToolkit:: name space doesn’t work out of box. ALIAS offer me help, this config eventually become valid. While it didn’t work when I want to include <OptiXToolkit/CuOmmBaking/CuOmmBaking.h> in my cpp file.

target_include_directories(pname PRIVATE ${CMAKE_SOURCE_DIR}/projects/optix-toolkit)
target_include_directories(pname PRIVATE ${CMAKE_SOURCE_DIR}/projects/optix-toolkit/CuOmmBaking)

add_library(OptiXToolkit::CuOmmBaking ALIAS CuOmmBaking)
target_link_libraries(pname PRIVATE OptiXToolkit::CuOmmBaking)

Hi @iaomw ,

the OptiX OmmBaking sample needs OmmBaking and OptiX.

The OptiXToolkit::Util library is providing something used by the sample, so yes, all three are required for the sample.

What is used from Util I don’t know off hand, but looking at the sample should make it obvious from what headers it is including.

You should add OmmBaking as a subdirectory and the target should all be setup correctly with the proper aliases. I guess this wasn’t done.

Hi,
The vcpkg approach requires me to include(ProjectOptions) before my project(root_project). However the optix render is not root_project, this action will probably pollute other sub projects. And otk is trying to use my CMAKE_TOOLCHAIN_FILE searching for deps, ignored it’s own vcpkg.

If the variable CMAKE_TOOLCHAIN_FILE is not set when ProjectOptions is included, it will be set to point to the CMake integration in the toolkit’s vcpkg submodule. A parent project using vcpkg for dependency management may be using its own submodule of vcpkg for toolchain integration and should set CMAKE_TOOLCHAIN_FILE as appropriate before including the toolkit’s ProjectOptions module.

While I don’t understand how to solve this conflict, my CMAKE_TOOLCHAIN_FILEis sent from command line, what is the before under this use case.

The FetchContent approach seems working at first, cmake stops arguing, and I don’t need pollute root_project. Unfortunately it has compile error.

[build] [466/703 66% :: 78.649] Building CUDA object render\otk\OmmBaking\CMakeFiles\CuOmmBaking.dir\src\Texture.cu.obj

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cuda.h(22252): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cuda.h(22958): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cub/warp/specializations/warp_exchange_shfl.cuh(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cuda/functional(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cuda.h(22252): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cuda.h(22958): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cub/warp/specializations/warp_exchange_shfl.cuh(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] C:\PROGRA~1\NVIDIA~2\CUDA\v12.6\include\cuda/functional(1): warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss

[build] Texture.cu

[build] tmpxft_00005df0_00000000-10_Texture.cudafe1.cpp

[build] ninja: build stopped: subcommand failed.

About the ALIAS OptiX::OptiX, it didn’t work in my cmake even under FetchContent approach.
Through cmake log, I am sure otk successfully find my optix 8.1 install.
-external:I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v12.6\include" -external:I"C:\ProgramData\NVIDIA Corporation\OptiX SDK 8.1.0\include"

For the render itself, I already have another approach to include optix, are there any possible conflict?

I suspect you’re encountering problems involving the way you are incoporating the OptiX Toolkit in your CMake project. Can you provide your CMakeLists.txt?
You shouldn’t have to define the OptiXToolkit::CuOmmBaking alias. It’s defined at line 61 of OmmBaking/CMakeLists.txt.
OptiX::OptiX is not an alias, it’s an interface library that simply records the location of the OptiX and CUDA include directories as target properties. That is defined in CMake/FindOptiX.cmake
You will very likely encounter problems if you attempt to define OptiX::OptiX in some other way, since CMake will not allow multiple definitions of the same library name.

Hello, I didn’t define OptiX::OptiX anywhere. I included Optix headers directly in my cmake and nvrtc config, it works without any problem. The OptiXToolkit::CuOmmBaking alias is only for testing and play, it didn’t work with or without.

Here is my cmake config related to otk below

# add_compile_options(-DOTK_USE_VCPKG=OFF)
# add_compile_options(-DOTK_BUILD_TESTS=OFF)
# add_compile_options(-DOTK_BUILD_EXAMPLES=OFF)
# add_compile_options(-DBUILD_SHARED_LIBS:BOOL=OFF)

add_subdirectory(${CMAKE_SOURCE_DIR}/projects/optix-toolkit otk)
# add_subdirectory(${CMAKE_SOURCE_DIR}/projects/optix-toolkit/OmmBaking omm)
# target_link_libraries(render PRIVATE Optix::Optix)
target_link_libraries(render PRIVATE OptiXToolkit::CuOmmBaking OptiXToolkit::Util)
"cmake.configureSettings": {
        "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
        "VCPKG_TARGET_TRIPLET": "x64-windows",
        "configured": true,
},
"cmake.configureArgs": [
        // "-DBUILD_SHARED_LIBS:BOOL=OFF",
        "-DOTK_USE_VCPKG=OFF",
        "-DOTK_FETCH_CONTENT:BOOL=OFF",
        "-DOTK_BUILD_DOCS=OFF",
        "-DOTK_BUILD_TESTS=OFF",
        "-DOTK_BUILD_EXAMPLES=OFF",
        //"-DOTK_LIBRARIES=OmmBaking",
        "-DImath_DIR:PATH=C:/Github/Imath",
        "-DOpenEXR_DIR:PATH=C:/Github/openexr",
} // vscode cmake config

However, it’s always trying to find openexr under my VCPKG_ROOT even I offered -DOpenEXR_DIR. It’s DemandLoading tyring to find openexr, but I don’t acutally use DemandLoading at current stage.

Then I found out -DOTK_LIBRARIES=OmmBaking with only target_link_libraries(render PRIVATE OptiXToolkit::CuOmmBaking) could compile without any problem. In that case, OptiXToolkit::Util is gone, it seems not belong to any library DemandLoading;Memory;OmmBaking;ShaderUtil

In my understanding, OmmBaking itself don’t acutually need Optix and other bloat. It could be pure C++/cuda code, cook the omm data, then I pass it to Optix APIs. May require some data struct from Optix headers.

Great, I’m glad you got it working. Yes, most of the third party dependencies in the OptiX Toolkit are incurred by the DemandLoading library, so setting OTK_LIBRARIES is a good way to simplify things.
OmmBaking does need the OptiX SDK, which is a header-only package. The OptiX API calls are implemented by the driver.
I thought OmmBaking relied on the Util library for trivial stuff like error reporting. That might have changed. I’ll take a look and eliminate that dependency if possible. Thanks for the note.

My config work fine for debug build under vscode/cmake. While it failed under release build. I guess both otk and me included CUDA, they have some kind of conflict.
In my cmake, CUDA is included by this 2 lines.

find_package(CUDAToolkit REQUIRED COMPONENTS cudart nvrtc REQUIRED)
target_link_libraries(render PRIVATE CUDA::cudart CUDA::nvrtc)
[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaCreateChannelDesc already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaCreateTextureObject already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaDeviceGetAttribute already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaDeviceSynchronize already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaDriverGetVersion already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaFree already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaFreeArray already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaFreeAsync already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaFreeHost already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGetDevice already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGetDeviceProperties_v2 already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGetErrorString already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGetLastError already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGraphicsMapResources already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGraphicsResourceGetMappedPointer already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGraphicsUnmapResources already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaGraphicsUnregisterResource already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaHostAlloc already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaHostGetDevicePointer already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMalloc already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMallocArray already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMallocAsync already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMallocManaged already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMemcpy already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMemcpy2DToArray already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMemcpyToArray already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaMemset already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaRuntimeGetVersion already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaSetDevice already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaStreamDestroy already defined in cudart.lib(cudart64_12.dll)

[build] cudart_static.lib(cudart_generated_cuda_runtime_api.obj) : error LNK2005: cudaStreamSynchronize already defined in cudart.lib(cudart64_12.dll)

Try making that PUBLIC instead of PRIVATE. I think duplicate target_link_libraries are supposed to be combined, but perhaps not if they’re PRIVATE. The OmmBaking CUDA link dependency is PUBLIC.

It turns out to be the conflict betweencudart and cudart_static. Then I also changed intocudart_static, it works flawless under both PUBLIC/PRIVATE.

Then I also test nvrtc_static, it doen’t compile with error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' possibly conflict with Qt5 from my vcpkg.

For nvrtc_static, I am considering the possibility making the app fully portable no matter CudaSDK is installed or not on user’s computer.

For CuOmmBaking itself, my final config is below:

cmake -G Ninja -B build -DCMAKE_BUILD_TYPE="${b}" `
    -DCMAKE_TOOLCHAIN_FILE="${env:VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" `
    -D_MY_Other_Configs `
    -DOTK_USE_VCPKG:BOOL=OFF `
    -DOTK_FETCH_CONTENT:BOOL=OFF `
    -DOTK_BUILD_DOCS:BOOL=OFF `
    -DOTK_BUILD_TESTS:BOOL=OFF `
    -DOTK_BUILD_EXAMPLES:BOOL=OFF `
    -DOTK_LIBRARIES=OmmBaking
# -DBUILD_SHARED_LIBS:BOOL=OFF # Both ON and OFF work

CMakeList.txt for the Optix render

find_package(CUDAToolkit REQUIRED COMPONENTS cudart nvrtc REQUIRED)
target_link_libraries(render_name PRIVATE CUDA::cudart_static CUDA::nvrtc)

add_subdirectory(${CMAKE_SOURCE_DIR}/projects/optix-toolkit otk)
target_link_libraries(render_name PRIVATE OptiXToolkit::CuOmmBaking)

Nice. Thanks for following up.