From 74caf773619bbf6a0f95c598b66261a6bef392ee Mon Sep 17 00:00:00 2001 From: Brecht Van Lommel Date: Mon, 29 Aug 2022 19:12:15 +0200 Subject: Cycles: add option to specify OptiX runtime root directory This allows individual users or Linux distributions to specify a directory Cycles will automatically look for the OptiX include folder, to compile kernels at runtime. It is still possible to override this with the OPTIX_ROOT_DIR environment variable at runtime. Based on patch by Sebastian Parborg. Ref D15792 --- intern/cycles/device/CMakeLists.txt | 2 ++ intern/cycles/device/optix/device_impl.cpp | 37 ++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'intern') diff --git a/intern/cycles/device/CMakeLists.txt b/intern/cycles/device/CMakeLists.txt index 4ae123cd634..24855d795d1 100644 --- a/intern/cycles/device/CMakeLists.txt +++ b/intern/cycles/device/CMakeLists.txt @@ -19,6 +19,8 @@ if(WITH_CYCLES_DEVICE_OPTIX OR WITH_CYCLES_DEVICE_CUDA) ) add_definitions(-DCYCLES_CUDA_NVCC_EXECUTABLE="${CUDA_NVCC_EXECUTABLE}") endif() + + add_definitions(-DCYCLES_RUNTIME_OPTIX_ROOT_DIR="${CYCLES_RUNTIME_OPTIX_ROOT_DIR}") endif() if(WITH_CYCLES_DEVICE_HIP AND WITH_HIP_DYNLOAD) diff --git a/intern/cycles/device/optix/device_impl.cpp b/intern/cycles/device/optix/device_impl.cpp index 94a46acaf18..8aac2b803f2 100644 --- a/intern/cycles/device/optix/device_impl.cpp +++ b/intern/cycles/device/optix/device_impl.cpp @@ -342,15 +342,29 @@ BVHLayoutMask OptiXDevice::get_bvh_layout_mask() const return BVH_LAYOUT_OPTIX; } +static string get_optix_include_dir() +{ + const char *env_dir = getenv("OPTIX_ROOT_DIR"); + const char *default_dir = CYCLES_RUNTIME_OPTIX_ROOT_DIR; + + if (env_dir && env_dir[0]) { + const string env_include_dir = path_join(env_dir, "include"); + return env_include_dir; + } + else if (default_dir[0]) { + const string default_include_dir = path_join(default_dir, "include"); + return default_include_dir; + } + + return string(); +} + string OptiXDevice::compile_kernel_get_common_cflags(const uint kernel_features) { string common_cflags = CUDADevice::compile_kernel_get_common_cflags(kernel_features); /* Add OptiX SDK include directory to include paths. */ - const char *optix_sdk_path = getenv("OPTIX_ROOT_DIR"); - if (optix_sdk_path) { - common_cflags += string_printf(" -I\"%s/include\"", optix_sdk_path); - } + common_cflags += string_printf(" -I\"%s/include\"", get_optix_include_dir().c_str()); /* Specialization for shader raytracing. */ if (kernel_features & KERNEL_FEATURE_NODE_RAYTRACE) { @@ -460,10 +474,19 @@ bool OptiXDevice::load_kernels(const uint kernel_features) "lib/kernel_optix_shader_raytrace.ptx" : "lib/kernel_optix.ptx"); if (use_adaptive_compilation() || path_file_size(ptx_filename) == -1) { - if (!getenv("OPTIX_ROOT_DIR")) { + std::string optix_include_dir = get_optix_include_dir(); + if (optix_include_dir.empty()) { set_error( - "Missing OPTIX_ROOT_DIR environment variable (which must be set with the path to " - "the Optix SDK to be able to compile Optix kernels on demand)."); + "Unable to compile OptiX kernels at runtime. Set OPTIX_ROOT_DIR environment variable " + "to a directory containing the OptiX SDK."); + return false; + } + else if (!path_is_directory(optix_include_dir)) { + set_error(string_printf( + "OptiX headers not found at %s, unable to compile OptiX kernels at runtime. Install " + "OptiX SDK in the specified location, or set OPTIX_ROOT_DIR environment variable to a " + "directory containing the OptiX SDK.", + optix_include_dir.c_str())); return false; } ptx_filename = compile_kernel( -- cgit v1.2.3