Blender V5.3
optix/device_impl.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2019-2022 Blender Foundation
3 *
4 * SPDX-License-Identifier: Apache-2.0 */
5
6#ifdef WITH_OPTIX
7
9# include "device/optix/queue.h"
10
11# include "bvh/bvh.h"
12# include "bvh/optix.h"
13
14# include "scene/hair.h"
15# include "scene/mesh.h"
16# include "scene/object.h"
17# include "scene/pointcloud.h"
18# include "scene/scene.h"
19
20# include "util/algorithm.h"
21# include "util/debug.h"
22# include "util/log.h"
23# include "util/path.h"
24# include "util/progress.h"
25# include "util/task.h"
26# include "util/time.h"
27
28# define __KERNEL_OPTIX__
30
32
33static void execute_optix_task(TaskPool &pool, OptixTask task, OptixResult &failure_reason)
34{
35 OptixTask additional_tasks[16];
36 unsigned int num_additional_tasks = 0;
37
38 const OptixResult result = optixTaskExecute(task, additional_tasks, 16, &num_additional_tasks);
39 if (result == OPTIX_SUCCESS) {
40 for (unsigned int i = 0; i < num_additional_tasks; ++i) {
41 pool.push([&pool, additional_task = additional_tasks[i], &failure_reason] {
42 execute_optix_task(pool, additional_task, failure_reason);
43 });
44 }
45 }
46 else {
47 failure_reason = result;
48 }
49}
50
51OptiXDevice::OptiXDevice(const DeviceInfo &info, Stats &stats, Profiler &profiler, bool headless)
52 : CUDADevice(info, stats, profiler, headless),
53# ifdef WITH_OSL
54 osl_colorsystem(this, "osl_colorsystem", MEM_READ_ONLY),
55# endif
56 sbt_data(this, "__sbt", MEM_READ_ONLY),
57 launch_params(this, "kernel_params", false)
58{
59 /* Make the CUDA context current. */
60 if (!cuContext) {
61 /* Do not initialize if CUDA context creation failed already. */
62 return;
63 }
64 const CUDAContextScope scope(this);
65
66 /* Create OptiX context for this device. */
67 OptixDeviceContextOptions options = {};
68 options.logCallbackLevel = 4; /* Fatal = 1, Error = 2, Warning = 3, Print = 4. */
69 options.logCallbackFunction = [](unsigned int level, const char *, const char *message, void *) {
70 switch (level) {
71 case 1:
72 LOG_FATAL << message;
73 break;
74 case 2:
75 LOG_ERROR << message;
76 break;
77 case 3:
78 LOG_WARNING << message;
79 break;
80 case 4:
81 LOG_DEBUG << message;
82 break;
83 default:
84 break;
85 }
86 };
87 if (DebugFlags().optix.use_debug) {
88 LOG_INFO << "Using OptiX debug mode.";
89 options.validationMode = OPTIX_DEVICE_CONTEXT_VALIDATION_MODE_ALL;
90 }
91 optix_assert(optixDeviceContextCreate(cuContext, &options, &context));
92 optix_assert(optixDeviceContextSetLogCallback(
93 context, options.logCallbackFunction, options.logCallbackData, options.logCallbackLevel));
94
95 unsigned int rtcore_version = 0;
96 optix_assert(optixDeviceContextGetProperty(
97 context, OPTIX_DEVICE_PROPERTY_RTCORE_VERSION, &rtcore_version, sizeof(rtcore_version)));
98 this->info.use_hardware_raytracing = rtcore_version > 0;
99
100 /* Fix weird compiler bug that assigns wrong size. */
101 launch_params.data_elements = sizeof(KernelParamsOptiX);
102
103 /* Allocate launch parameter buffer memory on device. */
104 launch_params.alloc_to_device(1);
105}
106
107OptiXDevice::~OptiXDevice()
108{
109 /* Make CUDA context current. */
110 const CUDAContextScope scope(this);
111
112 free_bvh_memory_delayed();
113
114 sbt_data.free();
115 image_info.free();
116 launch_params.free();
117
118 /* Unload modules. */
119 if (optix_module != nullptr) {
120 optixModuleDestroy(optix_module);
121 }
122 if (mnee_module != nullptr) {
123 optixModuleDestroy(mnee_module);
124 }
125 if (shader_raytrace_module != nullptr) {
126 optixModuleDestroy(shader_raytrace_module);
127 }
128 for (int i = 0; i < 2; ++i) {
129 if (builtin_modules[i] != nullptr) {
130 optixModuleDestroy(builtin_modules[i]);
131 }
132 }
133 for (int i = 0; i < NUM_PIPELINES; ++i) {
134 if (pipelines[i] != nullptr) {
135 optixPipelineDestroy(pipelines[i]);
136 }
137 }
138 for (int i = 0; i < NUM_PROGRAM_GROUPS; ++i) {
139 if (groups[i] != nullptr) {
140 optixProgramGroupDestroy(groups[i]);
141 }
142 }
143
144# ifdef WITH_OSL
145 if (osl_camera_module != nullptr) {
146 optixModuleDestroy(osl_camera_module);
147 }
148 if (osl_shadow_module != nullptr) {
149 optixModuleDestroy(osl_shadow_module);
150 }
151 if (osl_shadow_curve_module != nullptr) {
152 optixModuleDestroy(osl_shadow_curve_module);
153 }
154 if (osl_volume_module != nullptr) {
155 optixModuleDestroy(osl_volume_module);
156 }
157 for (const OptixModule &module : osl_modules) {
158 if (module != nullptr) {
159 optixModuleDestroy(module);
160 }
161 }
162 for (const OptixProgramGroup &group : osl_groups) {
163 if (group != nullptr) {
164 optixProgramGroupDestroy(group);
165 }
166 }
167 osl_colorsystem.free();
168# endif
169
170 optixDeviceContextDestroy(context);
171}
172
173unique_ptr<DeviceQueue> OptiXDevice::gpu_queue_create()
174{
175 return make_unique<OptiXDeviceQueue>(this);
176}
177
178BVHLayoutMask OptiXDevice::get_bvh_layout_mask(const uint64_t /*kernel_features*/) const
179{
180 /* OptiX has its own internal acceleration structure format. */
181 return BVH_LAYOUT_OPTIX;
182}
183
184static string get_optix_include_dir()
185{
186 const char *env_dir = getenv("OPTIX_ROOT_DIR");
187 const char *default_dir = CYCLES_RUNTIME_OPTIX_ROOT_DIR;
188
189 if (env_dir && env_dir[0]) {
190 const string env_include_dir = path_join(env_dir, "include");
191 return env_include_dir;
192 }
193 if (default_dir[0]) {
194 const string default_include_dir = path_join(default_dir, "include");
195 return default_include_dir;
196 }
197
198 return string();
199}
200
201string OptiXDevice::compile_kernel_get_common_cflags(const uint64_t kernel_features)
202{
203 string common_cflags = CUDADevice::compile_kernel_get_common_cflags(kernel_features);
204
205 /* Add OptiX SDK include directory to include paths. */
206 common_cflags += string_printf(" -I\"%s\"", get_optix_include_dir().c_str());
207
208 /* Specialization for shader ray-tracing. */
209 if (kernel_features & KERNEL_FEATURE_NODE_RAYTRACE) {
210 common_cflags += " --keep-device-functions";
211 }
212
213 return common_cflags;
214}
215
216void OptiXDevice::create_optix_module(TaskPool &pool,
217 OptixModuleCompileOptions &module_options,
218 string &ptx_data,
219 OptixModule &module,
220 OptixResult &result)
221{
222 OptixTask task = nullptr;
223 result = optixModuleCreateWithTasks(context,
224 &module_options,
225 &pipeline_options,
226 ptx_data.data(),
227 ptx_data.size(),
228 nullptr,
229 nullptr,
230 &module,
231 &task);
232 if (result == OPTIX_SUCCESS) {
233 pool.push([&pool, task, &result] { execute_optix_task(pool, task, result); });
234 }
235}
236
237/* Add hit and miss programs to the pipeline group, which are needed by shader raytrace and MNEE
238 * and live in a separate OptiX module. */
239static void add_hit_miss_program_groups(const OptixProgramGroup *groups,
240 vector<OptixProgramGroup> &pipeline_groups)
241{
242 if (groups[PG_MISS] != nullptr) {
243 pipeline_groups.push_back(groups[PG_MISS]);
244 }
245
246 for (int i = HIT_PROGAM_GROUP_OFFSET; i < HIT_PROGAM_GROUP_OFFSET + NUM_HIT_PROGRAM_GROUPS; i++)
247 {
248 if (groups[i] != nullptr) {
249 pipeline_groups.push_back(groups[i]);
250 }
251 }
252}
253
254/* Compute required stack size for hit programs, which are needed by shader raytrace and MNEE
255 * and live in a separate OptiX module. */
256static unsigned int hit_program_continuation_stack_size(const OptixStackSizes *stack_size)
257{
258 unsigned int trace_css = 0;
259 for (int i = HIT_PROGAM_GROUP_OFFSET; i < HIT_PROGAM_GROUP_OFFSET + NUM_HIT_PROGRAM_GROUPS; i++)
260 {
261 trace_css = std::max(trace_css, stack_size[i].cssCH);
262 trace_css = std::max(trace_css, stack_size[i].cssIS + stack_size[i].cssAH);
263 }
264 return trace_css;
265}
266
267bool OptiXDevice::load_kernels(const uint64_t kernel_features)
268{
269 if (have_error()) {
270 /* Abort early if context creation failed already. */
271 return false;
272 }
273
274# ifdef WITH_OSL
275 const bool use_osl_shading = (kernel_features & KERNEL_FEATURE_OSL_SHADING);
276 const bool use_osl_camera = (kernel_features & KERNEL_FEATURE_OSL_CAMERA);
277 const bool use_osl_volume = use_osl_shading && (kernel_features & KERNEL_FEATURE_VOLUME);
278# else
279 const bool use_osl_shading = false;
280 const bool use_osl_camera = false;
281# endif
282 const bool use_shader_raytrace = (kernel_features & KERNEL_FEATURE_NODE_RAYTRACE);
283 const bool use_mnee = (kernel_features & KERNEL_FEATURE_MNEE);
284
285 /* Skip creating OptiX module if only doing denoising. */
286 const bool need_optix_kernels = (kernel_features &
288
289 /* Detect existence of OptiX kernel and SDK here early. So we can error out
290 * before compiling the CUDA kernels, to avoid failing right after when
291 * compiling the OptiX kernel. */
292 string suffix = use_osl_shading ? "_osl" : "";
293 string ptx_filename;
294 if (need_optix_kernels) {
295 ptx_filename = path_get("lib/kernel_optix" + suffix + ".ptx.zst");
296 if (use_adaptive_compilation() || path_file_size(ptx_filename) == -1) {
297 std::string optix_include_dir = get_optix_include_dir();
298 if (optix_include_dir.empty()) {
299 set_error(
300 "Unable to compile OptiX kernels at runtime. Set OPTIX_ROOT_DIR environment variable "
301 "to a directory containing the OptiX SDK.");
302 return false;
303 }
304 if (!path_is_directory(optix_include_dir)) {
305 set_error(string_printf(
306 "OptiX headers not found at %s, unable to compile OptiX kernels at runtime. Install "
307 "OptiX SDK in the specified location, or set OPTIX_ROOT_DIR environment variable to a "
308 "directory containing the OptiX SDK.",
309 optix_include_dir.c_str()));
310 return false;
311 }
312 }
313 }
314
315 /* Load CUDA modules because we need some of the utility kernels. */
316 if (!CUDADevice::load_kernels(kernel_features)) {
317 return false;
318 }
319
320 if (!need_optix_kernels) {
321 return true;
322 }
323
324 const CUDAContextScope scope(this);
325
326 /* Unload existing OptiX module and pipelines first. */
327 if (optix_module != nullptr) {
328 optixModuleDestroy(optix_module);
329 optix_module = nullptr;
330 }
331 if (mnee_module != nullptr) {
332 optixModuleDestroy(mnee_module);
333 mnee_module = nullptr;
334 }
335 if (shader_raytrace_module != nullptr) {
336 optixModuleDestroy(shader_raytrace_module);
337 shader_raytrace_module = nullptr;
338 }
339 for (int i = 0; i < 2; ++i) {
340 if (builtin_modules[i] != nullptr) {
341 optixModuleDestroy(builtin_modules[i]);
342 builtin_modules[i] = nullptr;
343 }
344 }
345 for (int i = 0; i < NUM_PIPELINES; ++i) {
346 if (pipelines[i] != nullptr) {
347 optixPipelineDestroy(pipelines[i]);
348 pipelines[i] = nullptr;
349 }
350 }
351 for (int i = 0; i < NUM_PROGRAM_GROUPS; ++i) {
352 if (groups[i] != nullptr) {
353 optixProgramGroupDestroy(groups[i]);
354 groups[i] = nullptr;
355 }
356 }
357
358# ifdef WITH_OSL
359 if (osl_camera_module != nullptr) {
360 optixModuleDestroy(osl_camera_module);
361 osl_camera_module = nullptr;
362 }
363 if (osl_shadow_module != nullptr) {
364 optixModuleDestroy(osl_shadow_module);
365 osl_shadow_module = nullptr;
366 }
367 if (osl_shadow_curve_module != nullptr) {
368 optixModuleDestroy(osl_shadow_curve_module);
369 osl_shadow_curve_module = nullptr;
370 }
371 if (osl_volume_module != nullptr) {
372 optixModuleDestroy(osl_volume_module);
373 osl_volume_module = nullptr;
374 }
375
376 /* Recreating base OptiX module invalidates all OSL modules too, since they link against it. */
377 for (const OptixModule &module : osl_modules) {
378 if (module != nullptr) {
379 optixModuleDestroy(module);
380 }
381 }
382 osl_modules.clear();
383
384 for (const OptixProgramGroup &group : osl_groups) {
385 if (group != nullptr) {
386 optixProgramGroupDestroy(group);
387 }
388 }
389 osl_groups.clear();
390# endif
391
392 OptixModuleCompileOptions module_options = {};
393 module_options.maxRegisterCount = 0; /* Do not set an explicit register limit. */
394
395 if (DebugFlags().optix.use_debug) {
396 module_options.optLevel = OPTIX_COMPILE_OPTIMIZATION_LEVEL_0;
397 module_options.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_FULL;
398 }
399 else {
400 module_options.optLevel = OPTIX_COMPILE_OPTIMIZATION_LEVEL_3;
401 module_options.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE;
402 }
403
404 module_options.boundValues = nullptr;
405 module_options.numBoundValues = 0;
406 module_options.payloadTypes = nullptr;
407 module_options.numPayloadTypes = 0;
408
409 /* Default to no motion blur and two-level graph, since it is the fastest option. */
410 pipeline_options.usesMotionBlur = false;
411 pipeline_options.traversableGraphFlags =
412 OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_LEVEL_INSTANCING;
413 pipeline_options.numPayloadValues = 8;
414 pipeline_options.numAttributeValues = 2; /* u, v */
415 pipeline_options.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE;
416 pipeline_options.pipelineLaunchParamsVariableName = "kernel_params"; /* See globals.h */
417
418 pipeline_options.usesPrimitiveTypeFlags = OPTIX_PRIMITIVE_TYPE_FLAGS_TRIANGLE;
419 if (kernel_features & KERNEL_FEATURE_HAIR_THICK) {
420 pipeline_options.usesPrimitiveTypeFlags |= OPTIX_PRIMITIVE_TYPE_FLAGS_ROUND_LINEAR |
421 OPTIX_PRIMITIVE_TYPE_FLAGS_ROUND_CATMULLROM;
422 }
423 if (kernel_features & (KERNEL_FEATURE_HAIR_RIBBON | KERNEL_FEATURE_POINTCLOUD)) {
424 pipeline_options.usesPrimitiveTypeFlags |= OPTIX_PRIMITIVE_TYPE_FLAGS_CUSTOM;
425 }
426
427 /* Keep track of whether motion blur is enabled, so to enable/disable motion in BVH builds
428 * This is necessary since objects may be reported to have motion if the Vector pass is
429 * active, but may still need to be rendered without motion blur if that isn't active as well. */
430 if (kernel_features & KERNEL_FEATURE_OBJECT_MOTION) {
431 pipeline_options.usesMotionBlur = true;
432 /* Motion blur can insert motion transforms into the traversal graph.
433 * It is no longer a two-level graph then, so need to set flags to allow any configuration. */
434 pipeline_options.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_ANY;
435 }
436
437 { /* Load and compile PTX modules with OptiX kernels.
438 * All in one TaskPool so the OptiX driver can compile them concurrently. */
439 string base_ptx_data;
440 if (use_adaptive_compilation() || path_file_size(ptx_filename) == -1) {
441 string cflags = compile_kernel_get_common_cflags(kernel_features);
442 ptx_filename = compile_kernel(cflags, ("kernel" + suffix).c_str(), true);
443 }
444 LOG_INFO << "OptiX: Loading module from " << ptx_filename;
445 if (ptx_filename.empty() || !path_read_compressed_text(ptx_filename, base_ptx_data)) {
446 set_error(string_printf("Failed to load OptiX kernel from '%s'", ptx_filename.c_str()));
447 return false;
448 }
449
450 auto load_optional_module = [this, &kernel_features](const string &name,
451 string &ptx_data) -> bool {
452 string filename = path_get("lib/" + name + ".ptx.zst");
453 if (use_adaptive_compilation() || path_file_size(filename) == -1) {
454 /* Map kernel_optix_foo.ptx to kernel_foo.cu. */
455 const char *suffix = "_optix";
456 string source_name = name;
457 const size_t optix_pos = source_name.find(suffix);
458 if (optix_pos != string::npos) {
459 source_name.erase(optix_pos, strlen(suffix));
460 }
461
462 /* Runtime compile. */
463 const string cflags = compile_kernel_get_common_cflags(kernel_features);
464 filename = compile_kernel(cflags, source_name.c_str(), true);
465 }
466 LOG_INFO << "OptiX: Loading optional module from " << filename;
467 if (filename.empty() || !path_read_compressed_text(filename, ptx_data)) {
468 set_error(string_printf("Failed to load OptiX kernel from '%s'", filename.c_str()));
469 return false;
470 }
471 return true;
472 };
473
474 string mnee_ptx_data;
475 if (use_mnee &&
476 !load_optional_module(use_osl_shading ? "kernel_optix_osl_mnee" : "kernel_optix_mnee",
477 mnee_ptx_data))
478 {
479 return false;
480 }
481 string shader_raytrace_ptx_data;
482 if (use_shader_raytrace &&
483 !load_optional_module(use_osl_shading ? "kernel_optix_osl_shader_raytrace" :
484 "kernel_optix_shader_raytrace",
485 shader_raytrace_ptx_data))
486 {
487 return false;
488 }
489# ifdef WITH_OSL
490 string osl_camera_ptx_data;
491 if (use_osl_camera && !load_optional_module("kernel_optix_osl_camera", osl_camera_ptx_data)) {
492 return false;
493 }
494 string osl_shadow_ptx_data;
495 if (use_osl_shading && !load_optional_module("kernel_optix_osl_shadow", osl_shadow_ptx_data)) {
496 return false;
497 }
498 string osl_shadow_curve_ptx_data;
499 if (use_osl_shading &&
500 !load_optional_module("kernel_optix_osl_shadow_curve", osl_shadow_curve_ptx_data))
501 {
502 return false;
503 }
504 string osl_volume_ptx_data;
505 if (use_osl_volume && !load_optional_module("kernel_optix_osl_volume", osl_volume_ptx_data)) {
506 return false;
507 }
508# endif
509
510 TaskPool pool;
511 OptixResult base_result = OPTIX_SUCCESS;
512 OptixResult mnee_result = OPTIX_SUCCESS;
513 OptixResult shader_raytrace_result = OPTIX_SUCCESS;
514# ifdef WITH_OSL
515 OptixResult osl_camera_result = OPTIX_SUCCESS;
516 OptixResult osl_shadow_result = OPTIX_SUCCESS;
517 OptixResult osl_shadow_curve_result = OPTIX_SUCCESS;
518 OptixResult osl_volume_result = OPTIX_SUCCESS;
519# endif
520
521 const scoped_timer load_module_timer;
522 LOG_INFO << "OptiX: Creating modules.";
523 create_optix_module(pool, module_options, base_ptx_data, optix_module, base_result);
524 if (use_mnee) {
525 create_optix_module(pool, module_options, mnee_ptx_data, mnee_module, mnee_result);
526 }
527 if (use_shader_raytrace) {
528 create_optix_module(pool,
529 module_options,
530 shader_raytrace_ptx_data,
531 shader_raytrace_module,
532 shader_raytrace_result);
533 }
534# ifdef WITH_OSL
535 if (use_osl_shading) {
536 create_optix_module(
537 pool, module_options, osl_shadow_ptx_data, osl_shadow_module, osl_shadow_result);
538 create_optix_module(pool,
539 module_options,
540 osl_shadow_curve_ptx_data,
541 osl_shadow_curve_module,
542 osl_shadow_curve_result);
543 }
544 if (use_osl_camera) {
545 create_optix_module(
546 pool, module_options, osl_camera_ptx_data, osl_camera_module, osl_camera_result);
547 }
548 if (use_osl_volume) {
549 create_optix_module(
550 pool, module_options, osl_volume_ptx_data, osl_volume_module, osl_volume_result);
551 }
552# endif
553 pool.wait_work();
554 LOG_INFO << "OptiX: modules created in " << load_module_timer.get_time() << " seconds.";
555
556 if (base_result != OPTIX_SUCCESS) {
557 set_error(string_printf("Failed to load OptiX kernel from '%s' (%s)",
558 ptx_filename.c_str(),
559 optixGetErrorName(base_result)));
560 return false;
561 }
562 if (mnee_result != OPTIX_SUCCESS) {
563 set_error(
564 string_printf("Failed to load OptiX MNEE kernel (%s)", optixGetErrorName(mnee_result)));
565 return false;
566 }
567 if (shader_raytrace_result != OPTIX_SUCCESS) {
568 set_error(string_printf("Failed to load OptiX shader ray-tracing kernel (%s)",
569 optixGetErrorName(shader_raytrace_result)));
570 return false;
571 }
572# ifdef WITH_OSL
573 if (osl_camera_result != OPTIX_SUCCESS) {
574 set_error(string_printf("Failed to load OptiX OSL camera kernel (%s)",
575 optixGetErrorName(osl_camera_result)));
576 return false;
577 }
578 if (osl_shadow_result != OPTIX_SUCCESS) {
579 set_error(string_printf("Failed to load OptiX OSL shadow kernel (%s)",
580 optixGetErrorName(osl_shadow_result)));
581 return false;
582 }
583 if (osl_shadow_curve_result != OPTIX_SUCCESS) {
584 set_error(string_printf("Failed to load OptiX OSL shadow curve kernel (%s)",
585 optixGetErrorName(osl_shadow_curve_result)));
586 return false;
587 }
588 if (osl_volume_result != OPTIX_SUCCESS) {
589 set_error(string_printf("Failed to load OptiX OSL volume kernel (%s)",
590 optixGetErrorName(osl_volume_result)));
591 return false;
592 }
593# endif
594 }
595
596 /* Create program groups. */
597 OptixProgramGroupDesc group_descs[NUM_PROGRAM_GROUPS] = {};
598 OptixProgramGroupOptions group_options = {}; /* There are no options currently. */
599 group_descs[PG_RGEN_INTERSECT_CLOSEST].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
600 group_descs[PG_RGEN_INTERSECT_CLOSEST].raygen.module = optix_module;
601 group_descs[PG_RGEN_INTERSECT_CLOSEST].raygen.entryFunctionName =
602 "__raygen__kernel_optix_integrator_intersect_closest";
603 group_descs[PG_RGEN_INTERSECT_SHADOW].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
604 group_descs[PG_RGEN_INTERSECT_SHADOW].raygen.module = optix_module;
605 group_descs[PG_RGEN_INTERSECT_SHADOW].raygen.entryFunctionName =
606 "__raygen__kernel_optix_integrator_intersect_shadow";
607 group_descs[PG_RGEN_INTERSECT_SUBSURFACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
608 group_descs[PG_RGEN_INTERSECT_SUBSURFACE].raygen.module = optix_module;
609 group_descs[PG_RGEN_INTERSECT_SUBSURFACE].raygen.entryFunctionName =
610 "__raygen__kernel_optix_integrator_intersect_subsurface";
611 group_descs[PG_RGEN_INTERSECT_VOLUME_STACK].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
612 group_descs[PG_RGEN_INTERSECT_VOLUME_STACK].raygen.module = optix_module;
613 group_descs[PG_RGEN_INTERSECT_VOLUME_STACK].raygen.entryFunctionName =
614 "__raygen__kernel_optix_integrator_intersect_volume_stack";
615 group_descs[PG_RGEN_INTERSECT_DEDICATED_LIGHT].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
616 group_descs[PG_RGEN_INTERSECT_DEDICATED_LIGHT].raygen.module = optix_module;
617 group_descs[PG_RGEN_INTERSECT_DEDICATED_LIGHT].raygen.entryFunctionName =
618 "__raygen__kernel_optix_integrator_intersect_dedicated_light";
619 group_descs[PG_MISS].kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
620 group_descs[PG_MISS].miss.module = optix_module;
621 group_descs[PG_MISS].miss.entryFunctionName = "__miss__kernel_optix_miss";
622 group_descs[PG_HITD].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
623 group_descs[PG_HITD].hitgroup.moduleCH = optix_module;
624 group_descs[PG_HITD].hitgroup.entryFunctionNameCH = "__closesthit__kernel_optix_hit";
625 group_descs[PG_HITD].hitgroup.moduleAH = optix_module;
626 group_descs[PG_HITD].hitgroup.entryFunctionNameAH = "__anyhit__kernel_optix_visibility_test";
627 group_descs[PG_HITS].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
628 group_descs[PG_HITS].hitgroup.moduleAH = optix_module;
629 group_descs[PG_HITS].hitgroup.entryFunctionNameAH = "__anyhit__kernel_optix_shadow_all_hit";
630 group_descs[PG_HITV].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
631 group_descs[PG_HITV].hitgroup.moduleCH = optix_module;
632 group_descs[PG_HITV].hitgroup.entryFunctionNameCH = "__closesthit__kernel_optix_hit";
633 group_descs[PG_HITV].hitgroup.moduleAH = optix_module;
634 group_descs[PG_HITV].hitgroup.entryFunctionNameAH = "__anyhit__kernel_optix_volume_test";
635
636 OptixProgramGroupDesc ignore_desc = {};
637 ignore_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
638 ignore_desc.hitgroup.moduleCH = optix_module;
639 ignore_desc.hitgroup.entryFunctionNameCH = "__closesthit__kernel_optix_ignore";
640 ignore_desc.hitgroup.moduleAH = optix_module;
641 ignore_desc.hitgroup.entryFunctionNameAH = "__anyhit__kernel_optix_ignore";
642
643 if (kernel_features & KERNEL_FEATURE_HAIR_THICK) {
644 /* Built-in thick curve intersection. */
645 OptixBuiltinISOptions builtin_options = {};
646 builtin_options.builtinISModuleType = OPTIX_PRIMITIVE_TYPE_ROUND_CATMULLROM;
647 builtin_options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_TRACE |
648 OPTIX_BUILD_FLAG_ALLOW_COMPACTION | OPTIX_BUILD_FLAG_ALLOW_UPDATE;
649 builtin_options.curveEndcapFlags = OPTIX_CURVE_ENDCAP_DEFAULT; /* Disable end-caps. */
650 builtin_options.usesMotionBlur = false;
651
652 optix_assert(optixBuiltinISModuleGet(
653 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[0]));
654
655 group_descs[PG_HITD].hitgroup.moduleIS = builtin_modules[0];
656 group_descs[PG_HITD].hitgroup.entryFunctionNameIS = nullptr;
657 group_descs[PG_HITS].hitgroup.moduleIS = builtin_modules[0];
658 group_descs[PG_HITS].hitgroup.entryFunctionNameIS = nullptr;
659
660 if (pipeline_options.usesMotionBlur) {
661 builtin_options.usesMotionBlur = true;
662
663 optix_assert(optixBuiltinISModuleGet(
664 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[1]));
665
666 group_descs[PG_HITD_MOTION] = group_descs[PG_HITD];
667 group_descs[PG_HITD_MOTION].hitgroup.moduleIS = builtin_modules[1];
668 group_descs[PG_HITS_MOTION] = group_descs[PG_HITS];
669 group_descs[PG_HITS_MOTION].hitgroup.moduleIS = builtin_modules[1];
670 }
671
672 builtin_options.builtinISModuleType = OPTIX_PRIMITIVE_TYPE_ROUND_LINEAR;
673 builtin_options.usesMotionBlur = false;
674
675 optix_assert(optixBuiltinISModuleGet(
676 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[2]));
677
678 group_descs[PG_HITD_CURVE_LINEAR] = group_descs[PG_HITD];
679 group_descs[PG_HITD_CURVE_LINEAR].hitgroup.moduleIS = builtin_modules[2];
680 group_descs[PG_HITS_CURVE_LINEAR] = group_descs[PG_HITS];
681 group_descs[PG_HITS_CURVE_LINEAR].hitgroup.moduleIS = builtin_modules[2];
682 group_descs[PG_HITV_CURVE_LINEAR] = ignore_desc;
683 group_descs[PG_HITL_CURVE_LINEAR] = ignore_desc;
684
685 if (pipeline_options.usesMotionBlur) {
686 builtin_options.usesMotionBlur = true;
687
688 optix_assert(optixBuiltinISModuleGet(
689 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[3]));
690
691 group_descs[PG_HITD_CURVE_LINEAR_MOTION] = group_descs[PG_HITD_CURVE_LINEAR];
692 group_descs[PG_HITD_CURVE_LINEAR_MOTION].hitgroup.moduleIS = builtin_modules[3];
693 group_descs[PG_HITS_CURVE_LINEAR_MOTION] = group_descs[PG_HITS_CURVE_LINEAR];
694 group_descs[PG_HITS_CURVE_LINEAR_MOTION].hitgroup.moduleIS = builtin_modules[3];
695 group_descs[PG_HITV_CURVE_LINEAR_MOTION] = ignore_desc;
696 group_descs[PG_HITL_CURVE_LINEAR_MOTION] = ignore_desc;
697 }
698 }
699 if (kernel_features & KERNEL_FEATURE_HAIR_RIBBON) {
700 /* Custom ribbon intersection. */
701 group_descs[PG_HITD_CURVE_RIBBON] = group_descs[PG_HITD];
702 group_descs[PG_HITD_CURVE_RIBBON].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
703 group_descs[PG_HITD_CURVE_RIBBON].hitgroup.moduleIS = optix_module;
704 group_descs[PG_HITD_CURVE_RIBBON].hitgroup.entryFunctionNameIS =
705 "__intersection__curve_ribbon";
706 group_descs[PG_HITS_CURVE_RIBBON] = group_descs[PG_HITS];
707 group_descs[PG_HITS_CURVE_RIBBON].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
708 group_descs[PG_HITS_CURVE_RIBBON].hitgroup.moduleIS = optix_module;
709 group_descs[PG_HITS_CURVE_RIBBON].hitgroup.entryFunctionNameIS =
710 "__intersection__curve_ribbon";
711 group_descs[PG_HITV_CURVE_RIBBON] = ignore_desc;
712 group_descs[PG_HITL_CURVE_RIBBON] = ignore_desc;
713 }
714
715 if (kernel_features & KERNEL_FEATURE_POINTCLOUD) {
716 group_descs[PG_HITD_POINTCLOUD] = group_descs[PG_HITD];
717 group_descs[PG_HITD_POINTCLOUD].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
718 group_descs[PG_HITD_POINTCLOUD].hitgroup.moduleIS = optix_module;
719 group_descs[PG_HITD_POINTCLOUD].hitgroup.entryFunctionNameIS = "__intersection__point";
720 group_descs[PG_HITS_POINTCLOUD] = group_descs[PG_HITS];
721 group_descs[PG_HITS_POINTCLOUD].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
722 group_descs[PG_HITS_POINTCLOUD].hitgroup.moduleIS = optix_module;
723 group_descs[PG_HITS_POINTCLOUD].hitgroup.entryFunctionNameIS = "__intersection__point";
724 group_descs[PG_HITV_POINTCLOUD] = ignore_desc;
725 group_descs[PG_HITL_POINTCLOUD] = ignore_desc;
726 }
727
728 /* Add hit group for local intersections. */
730 group_descs[PG_HITL].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
731 group_descs[PG_HITL].hitgroup.moduleAH = optix_module;
732 group_descs[PG_HITL].hitgroup.entryFunctionNameAH = "__anyhit__kernel_optix_local_hit";
733 }
734
735 /* Shader ray-tracing replaces some functions with direct callables. */
736 if (use_shader_raytrace) {
737 group_descs[PG_RGEN_SHADE_SURFACE_RAYTRACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
738 group_descs[PG_RGEN_SHADE_SURFACE_RAYTRACE].raygen.module = shader_raytrace_module;
739 group_descs[PG_RGEN_SHADE_SURFACE_RAYTRACE].raygen.entryFunctionName =
740 "__raygen__kernel_optix_integrator_shade_surface_raytrace";
741
742 /* Kernels with OSL shading support are built without SVM, so can skip those direct callables
743 * there. */
744 if (!use_osl_shading) {
745 group_descs[PG_CALL_SVM_AO].kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
746 group_descs[PG_CALL_SVM_AO].callables.moduleDC = shader_raytrace_module;
747 group_descs[PG_CALL_SVM_AO].callables.entryFunctionNameDC = "__direct_callable__svm_node_ao";
748 group_descs[PG_CALL_SVM_BEVEL].kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
749 group_descs[PG_CALL_SVM_BEVEL].callables.moduleDC = shader_raytrace_module;
750 group_descs[PG_CALL_SVM_BEVEL].callables.entryFunctionNameDC =
751 "__direct_callable__svm_node_bevel";
752 }
753 }
754
755 if (use_mnee) {
756 group_descs[PG_RGEN_INTERSECT_MNEE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
757 group_descs[PG_RGEN_INTERSECT_MNEE].raygen.module = mnee_module;
758 group_descs[PG_RGEN_INTERSECT_MNEE].raygen.entryFunctionName =
759 "__raygen__kernel_optix_integrator_intersect_mnee";
760 }
761
762# ifdef WITH_OSL
763 /* OSL uses direct callables to execute, so shading needs to be done in OptiX if OSL is used. */
764 if (use_osl_shading) {
765 group_descs[PG_RGEN_SHADE_BACKGROUND].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
766 group_descs[PG_RGEN_SHADE_BACKGROUND].raygen.module = optix_module;
767 group_descs[PG_RGEN_SHADE_BACKGROUND].raygen.entryFunctionName =
768 "__raygen__kernel_optix_integrator_shade_background";
769 group_descs[PG_RGEN_SHADE_LIGHT_NEE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
770 group_descs[PG_RGEN_SHADE_LIGHT_NEE].raygen.module = optix_module;
771 group_descs[PG_RGEN_SHADE_LIGHT_NEE].raygen.entryFunctionName =
772 "__raygen__kernel_optix_integrator_shade_light_nee";
773 group_descs[PG_RGEN_SHADE_LIGHT_FORWARD].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
774 group_descs[PG_RGEN_SHADE_LIGHT_FORWARD].raygen.module = optix_module;
775 group_descs[PG_RGEN_SHADE_LIGHT_FORWARD].raygen.entryFunctionName =
776 "__raygen__kernel_optix_integrator_shade_light_forward";
777 group_descs[PG_RGEN_SHADE_SURFACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
778 group_descs[PG_RGEN_SHADE_SURFACE].raygen.module = optix_module;
779 group_descs[PG_RGEN_SHADE_SURFACE].raygen.entryFunctionName =
780 "__raygen__kernel_optix_integrator_shade_surface";
781 if (use_osl_volume) {
782 group_descs[PG_RGEN_SHADE_VOLUME].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
783 group_descs[PG_RGEN_SHADE_VOLUME].raygen.module = osl_volume_module;
784 group_descs[PG_RGEN_SHADE_VOLUME].raygen.entryFunctionName =
785 "__raygen__kernel_optix_integrator_shade_volume";
786 group_descs[PG_RGEN_SHADE_VOLUME_RAY_MARCHING].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
787 group_descs[PG_RGEN_SHADE_VOLUME_RAY_MARCHING].raygen.module = osl_volume_module;
788 group_descs[PG_RGEN_SHADE_VOLUME_RAY_MARCHING].raygen.entryFunctionName =
789 "__raygen__kernel_optix_integrator_shade_volume_ray_marching";
790 }
791 group_descs[PG_RGEN_SHADE_SHADOW].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
792 group_descs[PG_RGEN_SHADE_SHADOW].raygen.module = osl_shadow_module;
793 group_descs[PG_RGEN_SHADE_SHADOW].raygen.entryFunctionName =
794 "__raygen__kernel_optix_integrator_shade_shadow";
795 group_descs[PG_RGEN_SHADE_DEDICATED_LIGHT].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
796 group_descs[PG_RGEN_SHADE_DEDICATED_LIGHT].raygen.module = optix_module;
797 group_descs[PG_RGEN_SHADE_DEDICATED_LIGHT].raygen.entryFunctionName =
798 "__raygen__kernel_optix_integrator_shade_dedicated_light";
799 group_descs[PG_RGEN_EVAL_DISPLACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
800 group_descs[PG_RGEN_EVAL_DISPLACE].raygen.module = optix_module;
801 group_descs[PG_RGEN_EVAL_DISPLACE].raygen.entryFunctionName =
802 "__raygen__kernel_optix_shader_eval_displace";
803 group_descs[PG_RGEN_EVAL_BACKGROUND].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
804 group_descs[PG_RGEN_EVAL_BACKGROUND].raygen.module = optix_module;
805 group_descs[PG_RGEN_EVAL_BACKGROUND].raygen.entryFunctionName =
806 "__raygen__kernel_optix_shader_eval_background";
807 group_descs[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
808 group_descs[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY].raygen.module = osl_shadow_curve_module;
809 group_descs[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY].raygen.entryFunctionName =
810 "__raygen__kernel_optix_shader_eval_curve_shadow_transparency";
811 group_descs[PG_RGEN_EVAL_VOLUME_DENSITY].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
812 group_descs[PG_RGEN_EVAL_VOLUME_DENSITY].raygen.module = optix_module;
813 group_descs[PG_RGEN_EVAL_VOLUME_DENSITY].raygen.entryFunctionName =
814 "__raygen__kernel_optix_shader_eval_volume_density";
815 }
816
817 /* When using custom OSL cameras, integrator_init_from_camera is its own specialized module. */
818 if (use_osl_camera) {
819 group_descs[PG_RGEN_INIT_FROM_CAMERA].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
820 group_descs[PG_RGEN_INIT_FROM_CAMERA].raygen.module = osl_camera_module;
821 group_descs[PG_RGEN_INIT_FROM_CAMERA].raygen.entryFunctionName =
822 "__raygen__kernel_optix_integrator_init_from_camera";
823 }
824# endif
825
826 optix_assert(optixProgramGroupCreate(
827 context, group_descs, NUM_PROGRAM_GROUPS, &group_options, nullptr, nullptr, groups));
828
829 /* Get program stack sizes. */
830 auto get_pipeline_stack_size = [&](OptixPipeline pipeline,
831 const vector<OptixProgramGroup> &pipeline_groups,
832 unsigned int &trace_css) {
833 vector<OptixStackSizes> stack_size(NUM_PROGRAM_GROUPS);
834 for (int i = 0; i < NUM_PROGRAM_GROUPS; ++i) {
835 /* Only groups that are part of the pipeline, otherwise this is an error. */
836 if (groups[i] != nullptr &&
837 std::find(pipeline_groups.begin(), pipeline_groups.end(), groups[i]) !=
838 pipeline_groups.end())
839 {
840 optix_assert(optixProgramGroupGetStackSize(groups[i], &stack_size[i], pipeline));
841 }
842 }
843
844 /* Calculate maximum trace continuation stack size. */
845 trace_css = hit_program_continuation_stack_size(stack_size.data());
846
847 return stack_size;
848 };
849
850 /* Set up SBT, which in this case is used only to select between different programs. */
851 sbt_data.alloc(NUM_PROGRAM_GROUPS);
852 memset(sbt_data.host_pointer, 0, sizeof(SbtRecord) * NUM_PROGRAM_GROUPS);
853 for (int i = 0; i < NUM_PROGRAM_GROUPS; ++i) {
854 if (groups[i] != nullptr) {
855 optix_assert(optixSbtRecordPackHeader(groups[i], &sbt_data[i]));
856 }
857 }
858 sbt_data.copy_to_device(); /* Upload SBT to device. */
859
860 OptixPipelineLinkOptions link_options = {};
861 link_options.maxTraceDepth = 1;
862
863 if (use_osl_shading || use_osl_camera) {
864 /* OSL kernels will be (re)created on by OSL manager. */
865 }
866 else if (use_shader_raytrace || use_mnee) {
867 /* Create shader ray-tracing and MNEE pipeline. */
868 vector<OptixProgramGroup> pipeline_groups;
869 pipeline_groups.reserve(NUM_PROGRAM_GROUPS);
870 if (use_shader_raytrace) {
871 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE_RAYTRACE]);
872 pipeline_groups.push_back(groups[PG_CALL_SVM_AO]);
873 pipeline_groups.push_back(groups[PG_CALL_SVM_BEVEL]);
874 }
875 if (use_mnee) {
876 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_MNEE]);
877 }
878 add_hit_miss_program_groups(groups, pipeline_groups);
879
880 optix_assert(optixPipelineCreate(context,
881 &pipeline_options,
882 &link_options,
883 pipeline_groups.data(),
884 pipeline_groups.size(),
885 nullptr,
886 nullptr,
887 &pipelines[PIP_SHADE]));
888
889 unsigned int trace_css;
890 vector<OptixStackSizes> stack_size = get_pipeline_stack_size(
891 pipelines[PIP_SHADE], pipeline_groups, trace_css);
892
893 /* Combine ray generation and trace continuation stack size. */
894 const unsigned int css = std::max(stack_size[PG_RGEN_SHADE_SURFACE_RAYTRACE].cssRG,
895 stack_size[PG_RGEN_INTERSECT_MNEE].cssRG) +
896 link_options.maxTraceDepth * trace_css;
897 const unsigned int dss = std::max(stack_size[PG_CALL_SVM_AO].dssDC,
898 stack_size[PG_CALL_SVM_BEVEL].dssDC);
899
900 /* Set stack size depending on pipeline options. */
901 optix_assert(optixPipelineSetStackSize(
902 pipelines[PIP_SHADE], 0, dss, css, pipeline_options.usesMotionBlur ? 3 : 2));
903 }
904
905 { /* Create intersection-only pipeline. */
906 vector<OptixProgramGroup> pipeline_groups;
907 pipeline_groups.reserve(NUM_PROGRAM_GROUPS);
908 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_CLOSEST]);
909 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_SHADOW]);
910 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_SUBSURFACE]);
911 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_VOLUME_STACK]);
912 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_DEDICATED_LIGHT]);
913 pipeline_groups.push_back(groups[PG_MISS]);
914 pipeline_groups.push_back(groups[PG_HITD]);
915 pipeline_groups.push_back(groups[PG_HITS]);
916 pipeline_groups.push_back(groups[PG_HITL]);
917 pipeline_groups.push_back(groups[PG_HITV]);
918 if (pipeline_options.usesMotionBlur) {
919 pipeline_groups.push_back(groups[PG_HITD_MOTION]);
920 pipeline_groups.push_back(groups[PG_HITS_MOTION]);
921 }
922 if (kernel_features & KERNEL_FEATURE_HAIR_THICK) {
923 pipeline_groups.push_back(groups[PG_HITD_CURVE_LINEAR]);
924 pipeline_groups.push_back(groups[PG_HITS_CURVE_LINEAR]);
925 if (pipeline_options.usesMotionBlur) {
926 pipeline_groups.push_back(groups[PG_HITD_CURVE_LINEAR_MOTION]);
927 pipeline_groups.push_back(groups[PG_HITS_CURVE_LINEAR_MOTION]);
928 }
929 }
930 if (kernel_features & KERNEL_FEATURE_HAIR_RIBBON) {
931 pipeline_groups.push_back(groups[PG_HITD_CURVE_RIBBON]);
932 pipeline_groups.push_back(groups[PG_HITS_CURVE_RIBBON]);
933 }
934 if (kernel_features & KERNEL_FEATURE_POINTCLOUD) {
935 pipeline_groups.push_back(groups[PG_HITD_POINTCLOUD]);
936 pipeline_groups.push_back(groups[PG_HITS_POINTCLOUD]);
937 }
938
939 optix_assert(optixPipelineCreate(context,
940 &pipeline_options,
941 &link_options,
942 pipeline_groups.data(),
943 pipeline_groups.size(),
944 nullptr,
945 nullptr,
946 &pipelines[PIP_INTERSECT]));
947
948 unsigned int trace_css;
949 vector<OptixStackSizes> stack_size = get_pipeline_stack_size(
950 pipelines[PIP_INTERSECT], pipeline_groups, trace_css);
951
952 /* Calculate continuation stack size based on the maximum of all ray generation stack sizes. */
953 const unsigned int css =
954 std::max(stack_size[PG_RGEN_INTERSECT_CLOSEST].cssRG,
955 std::max(stack_size[PG_RGEN_INTERSECT_SHADOW].cssRG,
956 std::max(stack_size[PG_RGEN_INTERSECT_SUBSURFACE].cssRG,
957 stack_size[PG_RGEN_INTERSECT_VOLUME_STACK].cssRG))) +
958 link_options.maxTraceDepth * trace_css;
959
960 optix_assert(optixPipelineSetStackSize(
961 pipelines[PIP_INTERSECT], 0, 0, css, pipeline_options.usesMotionBlur ? 3 : 2));
962 }
963
964 return !have_error();
965}
966
967bool OptiXDevice::load_osl_kernels()
968{
969# ifdef WITH_OSL
970 if (have_error()) {
971 return false;
972 }
973
974 struct OSLKernel {
975 string ptx;
976 ustring fused_entry;
977 };
978
979 auto get_osl_kernel = [&](const OSL::ShaderGroupRef &group) {
980 if (!group) {
981 return OSLKernel{};
982 }
983 /* Other attribute access crashes when there are no layers. */
984 int num_layers = 0;
985 osl_globals.ss->getattribute(group.get(), "num_layers", num_layers);
986 if (num_layers == 0) {
987 return OSLKernel{};
988 }
989
990 string osl_ptx;
991 ustring fused_name;
992
993 osl_globals.ss->getattribute(group.get(), "group_fused_name", fused_name);
994 osl_globals.ss->getattribute(
995 group.get(), "ptx_compiled_version", OSL::TypeDesc::PTR, &osl_ptx);
996
997 int groupdata_size = 0;
998 osl_globals.ss->getattribute(group.get(), "llvm_groupdata_size", groupdata_size);
999 if (groupdata_size == 0) {
1000 // Old attribute name from our patched OSL version as fallback.
1001 osl_globals.ss->getattribute(group.get(), "groupdata_size", groupdata_size);
1002 }
1003 if (groupdata_size > 2048) { /* See 'group_data' array in kernel/osl/osl.h */
1004 set_error(
1005 string_printf("Requested OSL group data size (%d) is greater than the maximum "
1006 "supported with OptiX (2048)",
1007 groupdata_size));
1008 return OSLKernel{};
1009 }
1010
1011 return OSLKernel{std::move(osl_ptx), std::move(fused_name)};
1012 };
1013
1014 /* This has to be in the same order as the ShaderType enum, so that the index calculation in
1015 * osl_eval_nodes checks out */
1016 vector<OSLKernel> osl_kernels;
1017 osl_kernels.emplace_back(get_osl_kernel(osl_globals.camera_state));
1018 for (const OSL::ShaderGroupRef &group : osl_globals.surface_state) {
1019 osl_kernels.emplace_back(get_osl_kernel(group));
1020 }
1021 for (const OSL::ShaderGroupRef &group : osl_globals.volume_state) {
1022 osl_kernels.emplace_back(get_osl_kernel(group));
1023 }
1024 for (const OSL::ShaderGroupRef &group : osl_globals.displacement_state) {
1025 osl_kernels.emplace_back(get_osl_kernel(group));
1026 }
1027 for (const OSL::ShaderGroupRef &group : osl_globals.bump_state) {
1028 osl_kernels.emplace_back(get_osl_kernel(group));
1029 }
1030
1031 if (have_error()) {
1032 return false;
1033 }
1034
1035 const CUDAContextScope scope(this);
1036
1037 if (pipelines[PIP_SHADE]) {
1038 optixPipelineDestroy(pipelines[PIP_SHADE]);
1039 }
1040
1041 for (OptixModule &module : osl_modules) {
1042 if (module != nullptr) {
1043 optixModuleDestroy(module);
1044 module = nullptr;
1045 }
1046 }
1047 for (OptixProgramGroup &group : osl_groups) {
1048 if (group != nullptr) {
1049 optixProgramGroupDestroy(group);
1050 group = nullptr;
1051 }
1052 }
1053
1054 /* We always need to reserve a spot for the camera shader group, but if it's unused
1055 * and there are no other shader groups, we can skip creating the pipeline. */
1056 if (osl_kernels.size() == 1 && osl_kernels[0].ptx.empty()) {
1057 return true;
1058 }
1059
1060 OptixProgramGroupOptions group_options = {}; /* There are no options currently. */
1061 OptixModuleCompileOptions module_options = {};
1062 module_options.optLevel = OPTIX_COMPILE_OPTIMIZATION_LEVEL_3;
1063 module_options.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE;
1064
1065 /* In addition to the modules for each OSL group, we need to load our own osl_services.ptx
1066 * as well as the shadeops.ptx that's embedded in OSL. */
1067 size_t id_osl_services = osl_kernels.size();
1068 size_t id_osl_shadeops = osl_kernels.size() + 1;
1069 osl_groups.resize(osl_kernels.size() + 2);
1070 osl_modules.resize(osl_kernels.size() + 2);
1071
1072 { /* Load and compile PTX module with OSL services. */
1073 string osl_services_ptx, ptx_filename = path_get("lib/kernel_optix_osl_services.ptx.zst");
1074 if (!path_read_compressed_text(ptx_filename, osl_services_ptx)) {
1075 set_error(string_printf("Failed to load OptiX OSL services kernel from '%s'",
1076 ptx_filename.c_str()));
1077 return false;
1078 }
1079
1080 const char *shadeops_ptx_ptr = nullptr;
1081 osl_globals.ss->getattribute("shadeops_cuda_ptx", OSL::TypeDesc::PTR, &shadeops_ptx_ptr);
1082 int shadeops_ptx_size = 0;
1083 osl_globals.ss->getattribute("shadeops_cuda_ptx_size", OSL::TypeDesc::INT, &shadeops_ptx_size);
1084 string shadeops_ptx(shadeops_ptx_ptr, shadeops_ptx_size);
1085
1086 TaskPool pool;
1087 OptixResult services_result, shadeops_result;
1088 {
1089 const scoped_timer timer;
1090 LOG_INFO << "OptiX: Creating OSL services and shaderops modules.";
1091 create_optix_module(
1092 pool, module_options, osl_services_ptx, osl_modules[id_osl_services], services_result);
1093 create_optix_module(
1094 pool, module_options, shadeops_ptx, osl_modules[id_osl_shadeops], shadeops_result);
1095 pool.wait_work();
1096 LOG_INFO << "OptiX: OSL services and shaderops modules created in " << timer.get_time()
1097 << " seconds.";
1098 }
1099
1100 {
1101 if (services_result != OPTIX_SUCCESS) {
1102 set_error(string_printf("Failed to load OptiX OSL services kernel from '%s' (%s)",
1103 ptx_filename.c_str(),
1104 optixGetErrorName(services_result)));
1105 return false;
1106 }
1107 OptixProgramGroupDesc group_desc = {};
1108 group_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
1109 group_desc.callables.entryFunctionNameDC = "__direct_callable__dummy_services";
1110 group_desc.callables.moduleDC = osl_modules[id_osl_services];
1111
1112 optix_assert(optixProgramGroupCreate(context,
1113 &group_desc,
1114 1,
1115 &group_options,
1116 nullptr,
1117 nullptr,
1118 &osl_groups[id_osl_services]));
1119 }
1120
1121 {
1122 if (shadeops_result != OPTIX_SUCCESS) {
1123 set_error(string_printf("Failed to load OptiX OSL shadeops kernel (%s)",
1124 optixGetErrorName(shadeops_result)));
1125 return false;
1126 }
1127 OptixProgramGroupDesc group_desc = {};
1128 group_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
1129 group_desc.callables.entryFunctionNameDC = "__direct_callable__dummy_shadeops";
1130 group_desc.callables.moduleDC = osl_modules[id_osl_shadeops];
1131
1132 optix_assert(optixProgramGroupCreate(context,
1133 &group_desc,
1134 1,
1135 &group_options,
1136 nullptr,
1137 nullptr,
1138 &osl_groups[id_osl_shadeops]));
1139 }
1140 }
1141
1142 TaskPool pool;
1143 vector<OptixResult> results(osl_kernels.size(), OPTIX_SUCCESS);
1144
1145 {
1146 const scoped_timer timer;
1147 LOG_INFO << "OptiX: Creating OSL kernel modules.";
1148 for (size_t i = 0; i < osl_kernels.size(); ++i) {
1149 if (osl_kernels[i].ptx.empty()) {
1150 continue;
1151 }
1152 create_optix_module(pool, module_options, osl_kernels[i].ptx, osl_modules[i], results[i]);
1153 }
1154 pool.wait_work();
1155 LOG_INFO << "OptiX: OSL kernel modules created in " << timer.get_time() << " seconds.";
1156 }
1157
1158 for (size_t i = 0; i < osl_kernels.size(); ++i) {
1159 if (osl_kernels[i].ptx.empty()) {
1160 continue;
1161 }
1162
1163 if (results[i] != OPTIX_SUCCESS) {
1164 set_error(string_printf("Failed to load OptiX OSL kernel for %s (%s)",
1165 osl_kernels[i].fused_entry.c_str(),
1166 optixGetErrorName(results[i])));
1167 return false;
1168 }
1169
1170 OptixProgramGroupDesc group_desc = {};
1171 group_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
1172 group_desc.callables.entryFunctionNameDC = osl_kernels[i].fused_entry.c_str();
1173 group_desc.callables.moduleDC = osl_modules[i];
1174
1175 optix_assert(optixProgramGroupCreate(
1176 context, &group_desc, 1, &group_options, nullptr, nullptr, &osl_groups[i]));
1177 }
1178
1179 /* Update SBT with new entries. */
1180 sbt_data.alloc(NUM_PROGRAM_GROUPS + osl_groups.size());
1181 for (int i = 0; i < NUM_PROGRAM_GROUPS; ++i) {
1182 if (groups[i] != nullptr) {
1183 optix_assert(optixSbtRecordPackHeader(groups[i], &sbt_data[i]));
1184 }
1185 }
1186 for (size_t i = 0; i < osl_groups.size(); ++i) {
1187 if (osl_groups[i] != nullptr) {
1188 optix_assert(optixSbtRecordPackHeader(osl_groups[i], &sbt_data[NUM_PROGRAM_GROUPS + i]));
1189 }
1190 else {
1191 /* Default to "__direct_callable__dummy_services", so that OSL evaluation for empty
1192 * materials has direct callables to call and does not crash. */
1193 optix_assert(optixSbtRecordPackHeader(osl_groups[id_osl_services],
1194 &sbt_data[NUM_PROGRAM_GROUPS + i]));
1195 }
1196 }
1197 sbt_data.copy_to_device(); /* Upload updated SBT to device. */
1198
1199 OptixPipelineLinkOptions link_options = {};
1200 link_options.maxTraceDepth = 0;
1201
1202 {
1203 vector<OptixProgramGroup> pipeline_groups;
1204 pipeline_groups.reserve(NUM_PROGRAM_GROUPS);
1205 pipeline_groups.push_back(groups[PG_RGEN_SHADE_BACKGROUND]);
1206 pipeline_groups.push_back(groups[PG_RGEN_SHADE_LIGHT_NEE]);
1207 pipeline_groups.push_back(groups[PG_RGEN_SHADE_LIGHT_FORWARD]);
1208 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE]);
1209 if (groups[PG_RGEN_SHADE_SURFACE_RAYTRACE] != nullptr) {
1210 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE_RAYTRACE]);
1211 }
1212 if (groups[PG_CALL_SVM_AO] != nullptr) {
1213 pipeline_groups.push_back(groups[PG_CALL_SVM_AO]);
1214 }
1215 if (groups[PG_CALL_SVM_BEVEL] != nullptr) {
1216 pipeline_groups.push_back(groups[PG_CALL_SVM_BEVEL]);
1217 }
1218 if (groups[PG_RGEN_INTERSECT_MNEE] != nullptr) {
1219 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_MNEE]);
1220 }
1221 if (groups[PG_RGEN_SHADE_VOLUME] != nullptr) {
1222 pipeline_groups.push_back(groups[PG_RGEN_SHADE_VOLUME]);
1223 }
1224 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SHADOW]);
1225 pipeline_groups.push_back(groups[PG_RGEN_SHADE_DEDICATED_LIGHT]);
1226 pipeline_groups.push_back(groups[PG_RGEN_EVAL_DISPLACE]);
1227 pipeline_groups.push_back(groups[PG_RGEN_EVAL_BACKGROUND]);
1228 pipeline_groups.push_back(groups[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY]);
1229 pipeline_groups.push_back(groups[PG_RGEN_INIT_FROM_CAMERA]);
1230 pipeline_groups.push_back(groups[PG_RGEN_EVAL_VOLUME_DENSITY]);
1231
1232 /* For shader ray-tracing, trace depth and hit program groups are needed. */
1233 if (groups[PG_RGEN_SHADE_SURFACE_RAYTRACE] != nullptr ||
1234 groups[PG_RGEN_INTERSECT_MNEE] != nullptr)
1235 {
1236 link_options.maxTraceDepth = 1;
1237 add_hit_miss_program_groups(groups, pipeline_groups);
1238 }
1239
1240 for (const OptixProgramGroup &group : osl_groups) {
1241 if (group != nullptr) {
1242 pipeline_groups.push_back(group);
1243 }
1244 }
1245
1246 optix_assert(optixPipelineCreate(context,
1247 &pipeline_options,
1248 &link_options,
1249 pipeline_groups.data(),
1250 pipeline_groups.size(),
1251 nullptr,
1252 nullptr,
1253 &pipelines[PIP_SHADE]));
1254
1255 /* Get program stack sizes. */
1256 OptixStackSizes stack_size[NUM_PROGRAM_GROUPS] = {};
1257 vector<OptixStackSizes> osl_stack_size(osl_groups.size());
1258
1259 for (int i = 0; i < NUM_PROGRAM_GROUPS; ++i) {
1260 if (groups[i] != nullptr) {
1261 optix_assert(
1262 optixProgramGroupGetStackSize(groups[i], &stack_size[i], pipelines[PIP_SHADE]));
1263 }
1264 }
1265 for (size_t i = 0; i < osl_groups.size(); ++i) {
1266 if (osl_groups[i] != nullptr) {
1267 optix_assert(optixProgramGroupGetStackSize(
1268 osl_groups[i], &osl_stack_size[i], pipelines[PIP_SHADE]));
1269 }
1270 }
1271
1272 const unsigned int trace_css = hit_program_continuation_stack_size(stack_size);
1273 const unsigned int css = std::max(stack_size[PG_RGEN_SHADE_SURFACE_RAYTRACE].cssRG,
1274 stack_size[PG_RGEN_INTERSECT_MNEE].cssRG) +
1275 link_options.maxTraceDepth * trace_css;
1276 unsigned int dss = std::max(stack_size[PG_CALL_SVM_AO].dssDC,
1277 stack_size[PG_CALL_SVM_BEVEL].dssDC);
1278 for (unsigned int i = 0; i < osl_stack_size.size(); ++i) {
1279 dss = std::max(dss, osl_stack_size[i].dssDC);
1280 }
1281
1282 optix_assert(optixPipelineSetStackSize(
1283 pipelines[PIP_SHADE], 0, dss, css, pipeline_options.usesMotionBlur ? 3 : 2));
1284 }
1285
1286 /* Copy colorsystem data from OSL to the device. */
1287 {
1288 /* The interface here is somewhat complex, since the colorsystem contains strings whose
1289 * representation is different between CPU and GPU.
1290 * OSL's ColorSystem type therefore consists of two parts: First the "fixed data" (e.g. floats)
1291 * that is identical between both, and then the strings.
1292 * To perform this conversion, in addition to the pointer to the CPU data, we query two sizes:
1293 * The total size of the CPU data and the number of strings. */
1294 uint8_t *cpu_data = nullptr;
1295 size_t cpu_data_sizes[2] = {0, 0};
1296 osl_globals.ss->getattribute("colorsystem", OSL::TypeDesc::PTR, &cpu_data);
1297 osl_globals.ss->getattribute(
1298 "colorsystem:sizes", TypeDesc(TypeDesc::LONGLONG, 2), (void *)cpu_data_sizes);
1299
1300 size_t cpu_full_size = cpu_data_sizes[0];
1301 size_t num_strings = cpu_data_sizes[1];
1302 size_t fixed_data_size = cpu_full_size - sizeof(ustringhash) * num_strings;
1303
1304 /* Allocate a buffer to fit the fixed data, as well as all the strings in GPU form. */
1305 uint8_t *gpu_data = osl_colorsystem.alloc(fixed_data_size + sizeof(size_t) * num_strings);
1306
1307 /* Copy the fixed data as-is. */
1308 memcpy(gpu_data, cpu_data, fixed_data_size);
1309
1310 /* Convert each string to GPU format. */
1311 ustringhash *cpu_strings = reinterpret_cast<ustringhash *>(cpu_data + fixed_data_size);
1312 size_t *gpu_strings = reinterpret_cast<size_t *>(gpu_data + fixed_data_size);
1313 for (int i = 0; i < num_strings; i++) {
1314 gpu_strings[i] = cpu_strings[i].hash();
1315 }
1316
1317 /* Copy GPU form of the data to the device. */
1318 osl_colorsystem.copy_to_device();
1319
1320 update_launch_params(offsetof(KernelParamsOptiX, osl_colorsystem),
1321 &osl_colorsystem.device_pointer,
1322 sizeof(device_ptr));
1323 }
1324
1325 return !have_error();
1326# else
1327 return false;
1328# endif
1329}
1330
1331OSLGlobals *OptiXDevice::get_cpu_osl_memory()
1332{
1333# ifdef WITH_OSL
1334 return &osl_globals;
1335# else
1336 return nullptr;
1337# endif
1338}
1339
1340bool OptiXDevice::build_optix_bvh(BVHOptiX *bvh,
1341 OptixBuildOperation operation,
1342 const OptixBuildInput &build_input,
1343 const uint16_t num_motion_steps)
1344{
1345 /* Allocate and build acceleration structures only one at a time, to prevent parallel builds
1346 * from running out of memory (since both original and compacted acceleration structure memory
1347 * may be allocated at the same time for the duration of this function). The builds would
1348 * otherwise happen on the same CUDA stream anyway. */
1349 static thread_mutex mutex;
1350 thread_scoped_lock lock(mutex);
1351
1352 const CUDAContextScope scope(this);
1353
1354 bool use_fast_trace_bvh = (bvh->params.bvh_type == BVH_TYPE_STATIC);
1355
1356 /* Compute memory usage. */
1357 OptixAccelBufferSizes sizes = {};
1358 OptixAccelBuildOptions options = {};
1359 options.operation = operation;
1360 if (build_input.type == OPTIX_BUILD_INPUT_TYPE_CURVES) {
1361 /* The build flags have to match the ones used to query the built-in curve intersection
1362 * program (see optixBuiltinISModuleGet above) */
1363 options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_TRACE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION |
1364 OPTIX_BUILD_FLAG_ALLOW_UPDATE;
1365 use_fast_trace_bvh = true;
1366 }
1367 else if (use_fast_trace_bvh) {
1368 LOG_INFO << "Using fast to trace OptiX BVH";
1369 options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_TRACE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION;
1370 }
1371 else {
1372 LOG_INFO << "Using fast to update OptiX BVH";
1373 options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_BUILD | OPTIX_BUILD_FLAG_ALLOW_UPDATE;
1374 }
1375
1376 options.motionOptions.numKeys = num_motion_steps;
1377 options.motionOptions.flags = OPTIX_MOTION_FLAG_START_VANISH | OPTIX_MOTION_FLAG_END_VANISH;
1378 options.motionOptions.timeBegin = 0.0f;
1379 options.motionOptions.timeEnd = 1.0f;
1380
1381 optix_assert(optixAccelComputeMemoryUsage(context, &options, &build_input, 1, &sizes));
1382
1383 /* Allocate required output buffers. */
1384 device_only_memory<char> temp_mem(this, "optix temp as build mem", true);
1385 temp_mem.alloc_to_device(align_up(sizes.tempSizeInBytes, 8) + 8);
1386 if (!temp_mem.device_pointer) {
1387 /* Make sure temporary memory allocation succeeded. */
1388 return false;
1389 }
1390
1391 /* Acceleration structure memory has to be allocated on the device (not allowed on the host). */
1392 device_only_memory<char> &out_data = *bvh->as_data;
1393 if (operation == OPTIX_BUILD_OPERATION_BUILD) {
1394 assert(out_data.device == this);
1395 out_data.alloc_to_device(sizes.outputSizeInBytes);
1396 if (!out_data.device_pointer) {
1397 return false;
1398 }
1399 }
1400 else {
1401 assert(out_data.device_pointer && out_data.device_size >= sizes.outputSizeInBytes);
1402 }
1403
1404 /* Finally build the acceleration structure. */
1405 OptixAccelEmitDesc compacted_size_prop = {};
1406 compacted_size_prop.type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE;
1407 /* A tiny space was allocated for this property at the end of the temporary buffer above.
1408 * Make sure this pointer is 8-byte aligned. */
1409 compacted_size_prop.result = align_up(temp_mem.device_pointer + sizes.tempSizeInBytes, 8);
1410
1411 OptixTraversableHandle out_handle = 0;
1412 optix_assert(optixAccelBuild(context,
1413 nullptr,
1414 &options,
1415 &build_input,
1416 1,
1417 temp_mem.device_pointer,
1418 sizes.tempSizeInBytes,
1419 out_data.device_pointer,
1420 sizes.outputSizeInBytes,
1421 &out_handle,
1422 use_fast_trace_bvh ? &compacted_size_prop : nullptr,
1423 use_fast_trace_bvh ? 1 : 0));
1424 bvh->traversable_handle = static_cast<uint64_t>(out_handle);
1425
1426 /* Wait for all operations to finish. */
1427 cuda_assert(cuStreamSynchronize(nullptr));
1428
1429 /* Compact acceleration structure to save memory (do not do this in viewport for faster builds).
1430 */
1431 if (use_fast_trace_bvh) {
1432 uint64_t compacted_size = sizes.outputSizeInBytes;
1433 cuda_assert(cuMemcpyDtoH(&compacted_size, compacted_size_prop.result, sizeof(compacted_size)));
1434
1435 /* Temporary memory is no longer needed, so free it now to make space. */
1436 temp_mem.free();
1437
1438 /* There is no point compacting if the size does not change. */
1439 if (compacted_size < sizes.outputSizeInBytes) {
1440 device_only_memory<char> compacted_data(this, "optix compacted as", false);
1441 compacted_data.alloc_to_device(compacted_size);
1442 if (!compacted_data.device_pointer) {
1443 /* Do not compact if memory allocation for compacted acceleration structure fails.
1444 * Can just use the uncompacted one then, so succeed here regardless. */
1445 return !have_error();
1446 }
1447
1448 optix_assert(optixAccelCompact(context,
1449 nullptr,
1450 out_handle,
1451 compacted_data.device_pointer,
1452 compacted_size,
1453 &out_handle));
1454 bvh->traversable_handle = static_cast<uint64_t>(out_handle);
1455
1456 /* Wait for compaction to finish. */
1457 cuda_assert(cuStreamSynchronize(nullptr));
1458
1459 std::swap(out_data.device_size, compacted_data.device_size);
1460 std::swap(out_data.device_pointer, compacted_data.device_pointer);
1461 /* Original acceleration structure memory is freed when 'compacted_data' goes out of scope.
1462 */
1463 }
1464 }
1465
1466 return !have_error();
1467}
1468
1469void OptiXDevice::build_bvh(BVH *bvh, Progress &progress, bool refit)
1470{
1471 const bool use_fast_trace_bvh = (bvh->params.bvh_type == BVH_TYPE_STATIC);
1472
1473 free_bvh_memory_delayed();
1474
1475 BVHOptiX *const bvh_optix = static_cast<BVHOptiX *>(bvh);
1476
1477 progress.set_substatus("Building OptiX acceleration structure");
1478
1479 if (!bvh->params.top_level) {
1480 assert(bvh->objects.size() == 1 && bvh->geometry.size() == 1);
1481
1482 /* Refit is only possible in viewport for now (because AS is built with
1483 * OPTIX_BUILD_FLAG_ALLOW_UPDATE only there, see above). */
1484 OptixBuildOperation operation = OPTIX_BUILD_OPERATION_BUILD;
1485 if (refit && !use_fast_trace_bvh) {
1486 assert(bvh_optix->traversable_handle != 0);
1487 operation = OPTIX_BUILD_OPERATION_UPDATE;
1488 }
1489 else {
1490 bvh_optix->as_data->free();
1491 bvh_optix->traversable_handle = 0;
1492 }
1493
1494 /* Build bottom level acceleration structures (BLAS). */
1495 Geometry *const geom = bvh->geometry[0];
1496 if (geom->is_hair()) {
1497 /* Build BLAS for curve primitives. */
1498 Hair *const hair = static_cast<Hair *const>(geom);
1499 if (hair->num_segments() == 0) {
1500 return;
1501 }
1502
1503 const size_t num_segments = hair->num_segments();
1504
1505 size_t num_motion_steps = 1;
1506 const Attribute *attr_P = hair->attributes.find(ATTR_STD_POSITION);
1507 const Attribute *attr_R = hair->attributes.find(ATTR_STD_RADIUS);
1508 if (pipeline_options.usesMotionBlur && hair->get_use_motion_blur() && attr_P->has_motion()) {
1509 num_motion_steps = hair->get_motion_steps();
1510 }
1511
1512 device_vector<OptixAabb> aabb_data(this, "optix temp aabb data", MEM_READ_ONLY);
1513 device_vector<int> index_data(this, "optix temp index data", MEM_READ_ONLY);
1514 device_vector<float4> vertex_data(this, "optix temp vertex data", MEM_READ_ONLY);
1515 /* Four control points for each curve segment. */
1516 size_t num_vertices = num_segments * 4;
1517 if (hair->curve_shape == CURVE_THICK_LINEAR) {
1518 num_vertices = hair->num_keys();
1519 index_data.alloc(num_segments);
1520 vertex_data.alloc(num_vertices * num_motion_steps);
1521 }
1522 else if (hair->curve_shape == CURVE_THICK) {
1523 num_vertices = hair->num_keys() + 2 * hair->num_curves();
1524 index_data.alloc(num_segments);
1525 vertex_data.alloc(num_vertices * num_motion_steps);
1526 }
1527 else {
1528 aabb_data.alloc(num_segments * num_motion_steps);
1529 }
1530
1531 /* Get AABBs for each motion step. */
1532 for (size_t step = 0; step < num_motion_steps; ++step) {
1533 const packed_float3 *keys = attr_P->data_at_time_step<packed_float3>(step,
1534 num_motion_steps);
1535 const float *curve_radius_step = attr_R->data_at_time_step<float>(step, num_motion_steps);
1536
1537 if (hair->curve_shape == CURVE_THICK || hair->curve_shape == CURVE_THICK_LINEAR) {
1538 for (size_t curve_index = 0, segment_index = 0, vertex_index = step * num_vertices;
1539 curve_index < hair->num_curves();
1540 ++curve_index)
1541 {
1542 const Hair::Curve curve = hair->get_curve(curve_index);
1543
1544 if (hair->curve_shape == CURVE_THICK_LINEAR) {
1545 const int first_key_index = curve.first_key;
1546
1547 for (int k = 0; k < curve.num_segments(); ++k) {
1548 if (step == 0) {
1549 index_data[segment_index++] = vertex_index;
1550 }
1551 vertex_data[vertex_index++] = make_float4(float3(keys[first_key_index + k]),
1552 curve_radius_step[first_key_index + k]);
1553 }
1554
1555 const int last_key_index = first_key_index + curve.num_keys - 1;
1556 vertex_data[vertex_index++] = make_float4(float3(keys[last_key_index]),
1557 curve_radius_step[last_key_index]);
1558 }
1559 else {
1560 const int first_key_index = curve.first_key;
1561 vertex_data[vertex_index++] = make_float4(float3(keys[first_key_index]),
1562 curve_radius_step[first_key_index]);
1563
1564 for (int k = 0; k < curve.num_segments(); ++k) {
1565 if (step == 0) {
1566 index_data[segment_index++] = vertex_index - 1;
1567 }
1568 vertex_data[vertex_index++] = make_float4(float3(keys[first_key_index + k]),
1569 curve_radius_step[first_key_index + k]);
1570 }
1571
1572 const int last_key_index = first_key_index + curve.num_keys - 1;
1573 vertex_data[vertex_index++] = make_float4(float3(keys[last_key_index]),
1574 curve_radius_step[last_key_index]);
1575 vertex_data[vertex_index++] = make_float4(float3(keys[last_key_index]),
1576 curve_radius_step[last_key_index]);
1577 }
1578 }
1579 }
1580 else {
1581 for (size_t curve_index = 0, i = 0; curve_index < hair->num_curves(); ++curve_index) {
1582 const Hair::Curve curve = hair->get_curve(curve_index);
1583
1584 for (int segment = 0; segment < curve.num_segments(); ++segment, ++i) {
1586 curve.bounds_grow(segment, keys, curve_radius_step, bounds);
1587
1588 const size_t index = step * num_segments + i;
1589 aabb_data[index].minX = bounds.min.x;
1590 aabb_data[index].minY = bounds.min.y;
1591 aabb_data[index].minZ = bounds.min.z;
1592 aabb_data[index].maxX = bounds.max.x;
1593 aabb_data[index].maxY = bounds.max.y;
1594 aabb_data[index].maxZ = bounds.max.z;
1595 }
1596 }
1597 }
1598 }
1599
1600 /* Upload AABB data to GPU. */
1601 aabb_data.copy_to_device();
1602 index_data.copy_to_device();
1603 vertex_data.copy_to_device();
1604
1605 vector<device_ptr> aabb_ptrs;
1606 aabb_ptrs.reserve(num_motion_steps);
1607 vector<device_ptr> width_ptrs;
1608 vector<device_ptr> vertex_ptrs;
1609 width_ptrs.reserve(num_motion_steps);
1610 vertex_ptrs.reserve(num_motion_steps);
1611 for (size_t step = 0; step < num_motion_steps; ++step) {
1612 aabb_ptrs.push_back(aabb_data.device_pointer + step * num_segments * sizeof(OptixAabb));
1613 const device_ptr base_ptr = vertex_data.device_pointer +
1614 step * num_vertices * sizeof(float4);
1615 width_ptrs.push_back(base_ptr + 3 * sizeof(float)); /* Offset by vertex size. */
1616 vertex_ptrs.push_back(base_ptr);
1617 }
1618
1619 /* Force a single any-hit call, so shadow record-all behavior works correctly. */
1620 unsigned int build_flags = OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
1621 OptixBuildInput build_input = {};
1622 if (hair->curve_shape != CURVE_RIBBON) {
1623 build_input.type = OPTIX_BUILD_INPUT_TYPE_CURVES;
1624 if (hair->curve_shape == CURVE_THICK_LINEAR) {
1625 build_input.curveArray.curveType = OPTIX_PRIMITIVE_TYPE_ROUND_LINEAR;
1626 }
1627 else {
1628 build_input.curveArray.curveType = OPTIX_PRIMITIVE_TYPE_ROUND_CATMULLROM;
1629 }
1630 build_input.curveArray.numPrimitives = num_segments;
1631 build_input.curveArray.vertexBuffers = (CUdeviceptr *)vertex_ptrs.data();
1632 build_input.curveArray.numVertices = num_vertices;
1633 build_input.curveArray.vertexStrideInBytes = sizeof(float4);
1634 build_input.curveArray.widthBuffers = (CUdeviceptr *)width_ptrs.data();
1635 build_input.curveArray.widthStrideInBytes = sizeof(float4);
1636 build_input.curveArray.indexBuffer = (CUdeviceptr)index_data.device_pointer;
1637 build_input.curveArray.indexStrideInBytes = sizeof(int);
1638 build_input.curveArray.flag = build_flags;
1639 build_input.curveArray.primitiveIndexOffset = hair->curve_segment_offset;
1640 }
1641 else {
1642 /* Disable visibility test any-hit program, since it is already checked during
1643 * intersection. Those trace calls that require any-hit can force it with a ray flag. */
1644 build_flags |= OPTIX_GEOMETRY_FLAG_DISABLE_ANYHIT;
1645
1646 build_input.type = OPTIX_BUILD_INPUT_TYPE_CUSTOM_PRIMITIVES;
1647 build_input.customPrimitiveArray.aabbBuffers = (CUdeviceptr *)aabb_ptrs.data();
1648 build_input.customPrimitiveArray.numPrimitives = num_segments;
1649 build_input.customPrimitiveArray.strideInBytes = sizeof(OptixAabb);
1650 build_input.customPrimitiveArray.flags = &build_flags;
1651 build_input.customPrimitiveArray.numSbtRecords = 1;
1652 build_input.customPrimitiveArray.primitiveIndexOffset = hair->curve_segment_offset;
1653 }
1654
1655 if (!build_optix_bvh(bvh_optix, operation, build_input, num_motion_steps)) {
1656 progress.set_error("Failed to build OptiX acceleration structure");
1657 }
1658 }
1659 else if (geom->is_mesh() || geom->is_volume()) {
1660 /* Build BLAS for triangle primitives. */
1661 Mesh *const mesh = static_cast<Mesh *const>(geom);
1662 if (mesh->num_triangles() == 0) {
1663 return;
1664 }
1665
1666 const size_t num_verts = mesh->num_verts();
1667
1668 size_t num_motion_steps = 1;
1669 const Attribute *attr_P = mesh->attributes.find(ATTR_STD_POSITION);
1670 if (pipeline_options.usesMotionBlur && mesh->get_use_motion_blur() && attr_P->has_motion()) {
1671 num_motion_steps = mesh->get_motion_steps();
1672 }
1673
1674 device_vector<int> index_data(this, "optix temp index data", MEM_READ_ONLY);
1675 index_data.alloc(mesh->get_triangles().size());
1676 memcpy(index_data.data(),
1677 mesh->get_triangles().data(),
1678 mesh->get_triangles().size() * sizeof(int));
1679 device_vector<packed_float3> vertex_data(this, "optix temp vertex data", MEM_READ_ONLY);
1680 vertex_data.alloc(num_verts * num_motion_steps);
1681
1682 for (size_t step = 0; step < num_motion_steps; ++step) {
1683 const packed_float3 *verts = attr_P->data_at_time_step<packed_float3>(step,
1684 num_motion_steps);
1685 std::copy_n(verts, num_verts, vertex_data.data() + num_verts * step);
1686 }
1687
1688 /* Upload triangle data to GPU. */
1689 index_data.copy_to_device();
1690 vertex_data.copy_to_device();
1691
1692 vector<device_ptr> vertex_ptrs;
1693 vertex_ptrs.reserve(num_motion_steps);
1694 for (size_t step = 0; step < num_motion_steps; ++step) {
1695 vertex_ptrs.push_back(vertex_data.device_pointer +
1696 num_verts * step * sizeof(packed_float3));
1697 }
1698
1699 /* Force a single any-hit call, so shadow record-all behavior works correctly. */
1700 unsigned int build_flags = OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
1701 OptixBuildInput build_input = {};
1702 build_input.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES;
1703 build_input.triangleArray.vertexBuffers = (CUdeviceptr *)vertex_ptrs.data();
1704 build_input.triangleArray.numVertices = num_verts;
1705 build_input.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3;
1706 build_input.triangleArray.vertexStrideInBytes = sizeof(packed_float3);
1707 build_input.triangleArray.indexBuffer = index_data.device_pointer;
1708 build_input.triangleArray.numIndexTriplets = mesh->num_triangles();
1709 build_input.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3;
1710 build_input.triangleArray.indexStrideInBytes = 3 * sizeof(int);
1711 build_input.triangleArray.flags = &build_flags;
1712 /* The SBT does not store per primitive data since Cycles already allocates separate
1713 * buffers for that purpose. OptiX does not allow this to be zero though, so just pass in
1714 * one and rely on that having the same meaning in this case. */
1715 build_input.triangleArray.numSbtRecords = 1;
1716 build_input.triangleArray.primitiveIndexOffset = mesh->prim_offset;
1717
1718 if (!build_optix_bvh(bvh_optix, operation, build_input, num_motion_steps)) {
1719 progress.set_error("Failed to build OptiX acceleration structure");
1720 }
1721 }
1722 else if (geom->is_pointcloud()) {
1723 /* Build BLAS for points primitives. */
1724 PointCloud *const pointcloud = static_cast<PointCloud *const>(geom);
1725 const size_t num_points = pointcloud->num_points();
1726 if (num_points == 0) {
1727 return;
1728 }
1729
1730 size_t num_motion_steps = 1;
1731 Attribute *attr_P = pointcloud->attributes.find(ATTR_STD_POSITION);
1732 Attribute *attr_R = pointcloud->attributes.find(ATTR_STD_RADIUS);
1733 if (pipeline_options.usesMotionBlur && pointcloud->get_use_motion_blur() &&
1734 attr_P->has_motion())
1735 {
1736 num_motion_steps = pointcloud->get_motion_steps();
1737 }
1738
1739 device_vector<OptixAabb> aabb_data(this, "optix temp aabb data", MEM_READ_ONLY);
1740 aabb_data.alloc(num_points * num_motion_steps);
1741
1742 /* Get AABBs for each motion step. */
1743 for (size_t step = 0; step < num_motion_steps; ++step) {
1744 const packed_float3 *points = attr_P->data_at_time_step<packed_float3>(step,
1745 num_motion_steps);
1746 const float *radius = attr_R->data_at_time_step<float>(step, num_motion_steps);
1747
1748 for (size_t i = 0; i < num_points; ++i) {
1749 const PointCloud::Point point = pointcloud->get_point(i);
1751 point.bounds_grow(points, radius, bounds);
1752
1753 const size_t index = step * num_points + i;
1754 aabb_data[index].minX = bounds.min.x;
1755 aabb_data[index].minY = bounds.min.y;
1756 aabb_data[index].minZ = bounds.min.z;
1757 aabb_data[index].maxX = bounds.max.x;
1758 aabb_data[index].maxY = bounds.max.y;
1759 aabb_data[index].maxZ = bounds.max.z;
1760 }
1761 }
1762
1763 /* Upload AABB data to GPU. */
1764 aabb_data.copy_to_device();
1765
1766 vector<device_ptr> aabb_ptrs;
1767 aabb_ptrs.reserve(num_motion_steps);
1768 for (size_t step = 0; step < num_motion_steps; ++step) {
1769 aabb_ptrs.push_back(aabb_data.device_pointer + step * num_points * sizeof(OptixAabb));
1770 }
1771
1772 /* Disable visibility test any-hit program, since it is already checked during
1773 * intersection. Those trace calls that require anyhit can force it with a ray flag.
1774 * For those, force a single any-hit call, so shadow record-all behavior works correctly. */
1775 unsigned int build_flags = OPTIX_GEOMETRY_FLAG_DISABLE_ANYHIT |
1776 OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
1777 OptixBuildInput build_input = {};
1778 build_input.type = OPTIX_BUILD_INPUT_TYPE_CUSTOM_PRIMITIVES;
1779 build_input.customPrimitiveArray.aabbBuffers = (CUdeviceptr *)aabb_ptrs.data();
1780 build_input.customPrimitiveArray.numPrimitives = num_points;
1781 build_input.customPrimitiveArray.strideInBytes = sizeof(OptixAabb);
1782 build_input.customPrimitiveArray.flags = &build_flags;
1783 build_input.customPrimitiveArray.numSbtRecords = 1;
1784 build_input.customPrimitiveArray.primitiveIndexOffset = pointcloud->prim_offset;
1785
1786 if (!build_optix_bvh(bvh_optix, operation, build_input, num_motion_steps)) {
1787 progress.set_error("Failed to build OptiX acceleration structure");
1788 }
1789 }
1790 }
1791 else {
1792 unsigned int num_instances = 0;
1793 unsigned int max_num_instances = 0xFFFFFFFF;
1794
1795 bvh_optix->as_data->free();
1796 bvh_optix->traversable_handle = 0;
1797 bvh_optix->motion_transform_data->free();
1798
1799 optixDeviceContextGetProperty(context,
1800 OPTIX_DEVICE_PROPERTY_LIMIT_MAX_INSTANCE_ID,
1801 &max_num_instances,
1802 sizeof(max_num_instances));
1803 /* Do not count first bit, which is used to distinguish instanced and non-instanced objects. */
1804 max_num_instances >>= 1;
1805 if (bvh->objects.size() > max_num_instances) {
1806 progress.set_error(
1807 "Failed to build OptiX acceleration structure because there are too many instances");
1808 return;
1809 }
1810
1811 /* Fill instance descriptions. */
1812 device_vector<OptixInstance> instances(this, "optix tlas instances", MEM_READ_ONLY);
1813 instances.alloc(bvh->objects.size());
1814
1815 /* Calculate total motion transform size and allocate memory for them. */
1816 size_t motion_transform_offset = 0;
1817 if (pipeline_options.usesMotionBlur) {
1818 size_t total_motion_transform_size = 0;
1819 for (Object *const ob : bvh->objects) {
1820 if (ob->is_traceable() && ob->use_motion()) {
1821 total_motion_transform_size = align_up(total_motion_transform_size,
1822 OPTIX_TRANSFORM_BYTE_ALIGNMENT);
1823 const size_t motion_keys = max(ob->get_motion().size(), (size_t)2) - 2;
1824 total_motion_transform_size = total_motion_transform_size +
1825 sizeof(OptixSRTMotionTransform) +
1826 motion_keys * sizeof(OptixSRTData);
1827 }
1828 }
1829
1830 assert(bvh_optix->motion_transform_data->device == this);
1831 bvh_optix->motion_transform_data->alloc_to_device(total_motion_transform_size);
1832 }
1833
1834 for (Object *ob : bvh->objects) {
1835 /* Skip non-traceable objects. */
1836 if (!ob->is_traceable()) {
1837 continue;
1838 }
1839
1840 BVHOptiX *const blas = static_cast<BVHOptiX *>(ob->get_geometry()->bvh.get());
1841 OptixTraversableHandle handle = blas->traversable_handle;
1842 if (handle == 0) {
1843 continue;
1844 }
1845
1846 OptixInstance &instance = instances[num_instances++];
1847 memset(&instance, 0, sizeof(instance));
1848
1849 /* Clear transform to identity matrix. */
1850 instance.transform[0] = 1.0f;
1851 instance.transform[5] = 1.0f;
1852 instance.transform[10] = 1.0f;
1853
1854 /* Set user instance ID to object index. */
1855 instance.instanceId = ob->get_device_index();
1856
1857 /* Add some of the object visibility bits to the mask.
1858 * __prim_visibility contains the combined visibility bits of all instances, so is not
1859 * reliable if they differ between instances. But the OptiX visibility mask can only contain
1860 * 8 bits, so have to trade-off here and select just a few important ones.
1861 */
1862 instance.visibilityMask = ob->visibility_for_tracing() & 0xFF;
1863
1864 /* Have to have at least one bit in the mask, or else instance would always be culled. */
1865 if (0 == instance.visibilityMask) {
1866 instance.visibilityMask = 0xFF;
1867 }
1868
1869 if (ob->get_geometry()->is_hair()) {
1870 const Hair *hair = static_cast<const Hair *>(ob->get_geometry());
1871 if (hair->curve_shape == CURVE_RIBBON) {
1872 instance.sbtOffset = PG_HITD_CURVE_RIBBON - PG_HITD;
1873
1874 /* Also skip curve ribbons in local trace calls. */
1875 instance.visibilityMask |= 4;
1876 }
1877 else if (hair->curve_shape == CURVE_THICK_LINEAR) {
1878 instance.sbtOffset = PG_HITD_CURVE_LINEAR - PG_HITD;
1879 if (pipeline_options.usesMotionBlur && hair->has_motion_blur()) {
1880 instance.sbtOffset = PG_HITD_CURVE_LINEAR_MOTION - PG_HITD;
1881 }
1882 }
1883 else {
1884 if (pipeline_options.usesMotionBlur && hair->has_motion_blur()) {
1885 /* Select between motion blur and non-motion blur built-in intersection module. */
1886 instance.sbtOffset = PG_HITD_MOTION - PG_HITD;
1887 }
1888 }
1889 }
1890 else if (ob->get_geometry()->is_pointcloud()) {
1891 /* Use the hit group that has an intersection program for point clouds. */
1892 instance.sbtOffset = PG_HITD_POINTCLOUD - PG_HITD;
1893
1894 /* Also skip point clouds in local trace calls. */
1895 instance.visibilityMask |= 4;
1896 }
1897 {
1898 /* Can disable __anyhit__kernel_optix_visibility_test by default (except for thick curves,
1899 * since it needs to filter out end-caps there).
1900 *
1901 * It is enabled where necessary (visibility mask exceeds 8 bits or the other any-hit
1902 * programs like __anyhit__kernel_optix_shadow_all_hit) via OPTIX_RAY_FLAG_ENFORCE_ANYHIT.
1903 */
1904 instance.flags = OPTIX_INSTANCE_FLAG_DISABLE_ANYHIT;
1905 }
1906
1907 /* Insert motion traversable if object has motion. */
1908 if (pipeline_options.usesMotionBlur && ob->use_motion()) {
1909 size_t motion_keys = max(ob->get_motion().size(), (size_t)2) - 2;
1910 size_t motion_transform_size = sizeof(OptixSRTMotionTransform) +
1911 motion_keys * sizeof(OptixSRTData);
1912
1913 const CUDAContextScope scope(this);
1914
1915 motion_transform_offset = align_up(motion_transform_offset,
1916 OPTIX_TRANSFORM_BYTE_ALIGNMENT);
1917 CUdeviceptr motion_transform_gpu = bvh_optix->motion_transform_data->device_pointer +
1918 motion_transform_offset;
1919 motion_transform_offset += motion_transform_size;
1920
1921 /* Allocate host side memory for motion transform and fill it with transform data. */
1922 array<uint8_t> motion_transform_storage(motion_transform_size);
1923 OptixSRTMotionTransform *motion_transform = reinterpret_cast<OptixSRTMotionTransform *>(
1924 motion_transform_storage.data());
1925 motion_transform->child = handle;
1926 motion_transform->motionOptions.numKeys = ob->get_motion().size();
1927 motion_transform->motionOptions.flags = OPTIX_MOTION_FLAG_NONE;
1928 motion_transform->motionOptions.timeBegin = 0.0f;
1929 motion_transform->motionOptions.timeEnd = 1.0f;
1930
1931 OptixSRTData *const srt_data = motion_transform->srtData;
1932 array<DecomposedTransform> decomp(ob->get_motion().size());
1934 decomp.data(), ob->get_motion().data(), ob->get_motion().size());
1935
1936 for (size_t i = 0; i < ob->get_motion().size(); ++i) {
1937 /* Scale. */
1938 srt_data[i].sx = decomp[i].y.w; /* scale.x.x */
1939 srt_data[i].sy = decomp[i].z.w; /* scale.y.y */
1940 srt_data[i].sz = decomp[i].w.w; /* scale.z.z */
1941
1942 /* Shear. */
1943 srt_data[i].a = decomp[i].z.x; /* scale.x.y */
1944 srt_data[i].b = decomp[i].z.y; /* scale.x.z */
1945 srt_data[i].c = decomp[i].w.x; /* scale.y.z */
1946 assert(decomp[i].z.z == 0.0f); /* scale.y.x */
1947 assert(decomp[i].w.y == 0.0f); /* scale.z.x */
1948 assert(decomp[i].w.z == 0.0f); /* scale.z.y */
1949
1950 /* Pivot point. */
1951 srt_data[i].pvx = 0.0f;
1952 srt_data[i].pvy = 0.0f;
1953 srt_data[i].pvz = 0.0f;
1954
1955 /* Rotation. */
1956 srt_data[i].qx = decomp[i].x.x;
1957 srt_data[i].qy = decomp[i].x.y;
1958 srt_data[i].qz = decomp[i].x.z;
1959 srt_data[i].qw = decomp[i].x.w;
1960
1961 /* Translation. */
1962 srt_data[i].tx = decomp[i].y.x;
1963 srt_data[i].ty = decomp[i].y.y;
1964 srt_data[i].tz = decomp[i].y.z;
1965 }
1966
1967 /* Upload motion transform to GPU. */
1968 cuMemcpyHtoD(motion_transform_gpu, motion_transform, motion_transform_size);
1969 motion_transform = nullptr;
1970 motion_transform_storage.clear();
1971
1972 /* Get traversable handle to motion transform. */
1973 optixConvertPointerToTraversableHandle(context,
1974 motion_transform_gpu,
1975 OPTIX_TRAVERSABLE_TYPE_SRT_MOTION_TRANSFORM,
1976 &instance.traversableHandle);
1977 }
1978 else {
1979 instance.traversableHandle = handle;
1980
1981 if (ob->get_geometry()->is_instanced()) {
1982 /* Set transform matrix. */
1983 memcpy(instance.transform, &ob->get_tfm(), sizeof(instance.transform));
1984 }
1985 }
1986 }
1987
1988 /* Upload instance descriptions. */
1989 instances.resize(num_instances);
1990 instances.copy_to_device();
1991
1992 /* Build top-level acceleration structure (TLAS) */
1993 OptixBuildInput build_input = {};
1994 build_input.type = OPTIX_BUILD_INPUT_TYPE_INSTANCES;
1995 build_input.instanceArray.instances = instances.device_pointer;
1996 build_input.instanceArray.numInstances = num_instances;
1997
1998 if (!build_optix_bvh(bvh_optix, OPTIX_BUILD_OPERATION_BUILD, build_input, 0)) {
1999 progress.set_error("Failed to build OptiX acceleration structure");
2000 }
2001 tlas_handle = bvh_optix->traversable_handle;
2002 }
2003}
2004
2005void OptiXDevice::release_bvh(BVH *bvh)
2006{
2007 thread_scoped_lock lock(delayed_free_bvh_mutex);
2008 /* Do delayed free of BVH memory, since geometry holding BVH might be deleted
2009 * while GPU is still rendering. */
2010 BVHOptiX *const bvh_optix = static_cast<BVHOptiX *>(bvh);
2011
2012 delayed_free_bvh_memory.emplace_back(std::move(bvh_optix->as_data));
2013 delayed_free_bvh_memory.emplace_back(std::move(bvh_optix->motion_transform_data));
2014 bvh_optix->traversable_handle = 0;
2015}
2016
2017void OptiXDevice::free_bvh_memory_delayed()
2018{
2019 thread_scoped_lock lock(delayed_free_bvh_mutex);
2020 delayed_free_bvh_memory.free_memory();
2021}
2022
2023void OptiXDevice::const_copy_to(const char *name, void *host, const size_t size)
2024{
2025 /* Set constant memory for CUDA module. */
2026 CUDADevice::const_copy_to(name, host, size);
2027
2028 if (strcmp(name, "data") == 0) {
2029 assert(size <= sizeof(KernelData));
2030
2031 /* Update traversable handle (since it is different for each device on multi devices). */
2032 KernelData *const data = (KernelData *)host;
2033 *(OptixTraversableHandle *)&data->device_bvh = tlas_handle;
2034
2035 update_launch_params(offsetof(KernelParamsOptiX, data), host, size);
2036 return;
2037 }
2038
2039 /* Update data storage pointers in launch parameters. */
2040# define KERNEL_DATA_ARRAY(data_type, data_name) \
2041 if (strcmp(name, #data_name) == 0) { \
2042 update_launch_params(offsetof(KernelParamsOptiX, data_name), host, size); \
2043 return; \
2044 }
2045 KERNEL_DATA_ARRAY(IntegratorStateGPU, integrator_state)
2046# include "kernel/data_arrays.h"
2047# undef KERNEL_DATA_ARRAY
2048}
2049
2050void OptiXDevice::update_launch_params(const size_t offset, void *data, const size_t data_size)
2051{
2052 const CUDAContextScope scope(this);
2053
2054 cuda_assert(cuMemcpyHtoD(launch_params.device_pointer + offset, data, data_size));
2055}
2056
2058
2059#endif /* WITH_OPTIX */
volatile int lock
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
Definition btDbvt.cpp:299
btScalar max
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
Attribute * find(ustring name) const
bool has_motion() const
const T * data_at_time_step(const int step, const int num_time_steps) const
bool top_level
Definition params.h:80
int bvh_type
Definition params.h:105
Definition bvh/bvh.h:67
vector< Geometry * > geometry
Definition bvh/bvh.h:70
BVHParams params
Definition bvh/bvh.h:69
vector< Object * > objects
Definition bvh/bvh.h:71
float3 max
Definition boundbox.h:20
float3 min
Definition boundbox.h:20
bool use_hardware_raytracing
bool is_volume() const
bool is_pointcloud() const
bool is_hair() const
size_t prim_offset
virtual bool has_motion_blur() const
AttributeSet attributes
bool is_mesh() const
Definition hair.h:13
Curve get_curve(const size_t i) const
Definition hair.h:94
size_t curve_segment_offset
Definition hair.h:76
size_t num_curves() const
Definition hair.h:109
size_t num_segments() const
Definition hair.h:114
CurveShapeType curve_shape
Definition hair.h:77
size_t num_keys() const
Definition hair.h:103
NODE_DECLARE size_t num_verts() const
size_t num_triangles() const
Definition scene/mesh.h:73
bool use_motion() const
int get_device_index() const
bool is_traceable() const
uint visibility_for_tracing() const
Point get_point(const int i) const
size_t num_points() const
void set_substatus(const string &substatus_)
Definition progress.h:259
void set_error(const string &error_message_)
Definition progress.h:98
void push(TaskRunFunction &&task)
Definition task.cpp:21
void wait_work(Summary *stats=nullptr)
Definition task.cpp:27
void alloc_to_device(const size_t num, bool shrink_to_fit=true)
double get_time() const
Definition time.h:52
@ MEM_READ_ONLY
CCL_NAMESPACE_BEGIN struct Options options
#define KERNEL_DATA_ARRAY(type, name)
Definition data_arrays.h:8
#define KERNEL_FEATURE_VOLUME
#define KERNEL_FEATURE_HAIR_RIBBON
#define KERNEL_FEATURE_OBJECT_MOTION
#define KERNEL_FEATURE_OSL_SHADING
#define KERNEL_FEATURE_SUBSURFACE
#define KERNEL_FEATURE_HAIR_THICK
#define KERNEL_FEATURE_PATH_TRACING
#define KERNEL_FEATURE_OSL_CAMERA
#define KERNEL_FEATURE_NODE_RAYTRACE
#define KERNEL_FEATURE_BAKING
#define KERNEL_FEATURE_MNEE
#define KERNEL_FEATURE_POINTCLOUD
#define CCL_NAMESPACE_END
#define offsetof(t, d)
#define assert(assertion)
#define index(i)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
@ CURVE_THICK
@ CURVE_RIBBON
@ CURVE_THICK_LINEAR
@ BVH_LAYOUT_OPTIX
@ ATTR_STD_POSITION
@ ATTR_STD_RADIUS
#define LOG_DEBUG
Definition log.h:107
#define LOG_FATAL
Definition log.h:99
#define LOG_ERROR
Definition log.h:101
#define LOG_WARNING
Definition log.h:103
#define LOG_INFO
Definition log.h:106
Segment< FEdge *, Vec3r > segment
std::string filename(const std::string_view &str, size_t pos)
BMesh const char void * data
unsigned char uint8_t
Definition BKE_text.h:79
PipelineGraphic pipeline(vert_main, frag_main)
int BVHLayoutMask
Definition params.h:50
@ BVH_TYPE_STATIC
Definition params.h:40
size_t path_file_size(const string &path)
Definition path.cpp:594
bool path_is_directory(const string &path)
Definition path.cpp:622
string path_get(const string &sub)
Definition path.cpp:368
string path_join(const string &dir, const string &file)
Definition path.cpp:436
bool path_read_compressed_text(const string &path, string &text)
Definition path.cpp:803
static struct PyModuleDef module
Definition python.cpp:944
#define make_float4
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
Definition string.cpp:25
void bounds_grow(const int k, const packed_float3 *curve_keys, const float *curve_radius, BoundBox &bounds) const
Definition hair.cpp:44
int first_key
Definition hair.h:19
int num_segments() const
Definition hair.h:22
int num_keys
Definition hair.h:20
void bounds_grow(const packed_float3 *points, const float *radius, BoundBox &bounds) const
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
std::mutex thread_mutex
Definition thread.h:27
std::unique_lock< std::mutex > thread_scoped_lock
Definition thread.h:28
void transform_motion_decompose(DecomposedTransform *decomp, const Transform *motion, const size_t size)
uint64_t device_ptr
Definition types_base.h:44
ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)
Definition types_base.h:48