26# define __KERNEL_OPTIX__
31static void execute_optix_task(
TaskPool &pool, OptixTask task, OptixResult &failure_reason)
33 OptixTask additional_tasks[16];
34 unsigned int num_additional_tasks = 0;
36 const OptixResult
result = optixTaskExecute(task, additional_tasks, 16, &num_additional_tasks);
37 if (
result == OPTIX_SUCCESS) {
38 for (
unsigned int i = 0;
i < num_additional_tasks; ++
i) {
39 pool.
push([&pool, additional_task = additional_tasks[
i], &failure_reason] {
40 execute_optix_task(pool, additional_task, failure_reason);
50 : CUDADevice(info, stats, profiler, headless),
55 launch_params(this,
"kernel_params",
false)
62 const CUDAContextScope scope(
this);
65 OptixDeviceContextOptions
options = {};
67 options.logCallbackFunction = [](
unsigned int level,
const char *,
const char *message,
void *) {
86 LOG_INFO <<
"Using OptiX debug mode.";
87 options.validationMode = OPTIX_DEVICE_CONTEXT_VALIDATION_MODE_ALL;
89 optix_assert(optixDeviceContextCreate(cuContext, &
options, &context));
90 optix_assert(optixDeviceContextSetLogCallback(
97 launch_params.alloc_to_device(1);
100OptiXDevice::~OptiXDevice()
103 const CUDAContextScope scope(
this);
105 free_bvh_memory_delayed();
109 launch_params.free();
112 if (optix_module !=
nullptr) {
113 optixModuleDestroy(optix_module);
115 for (
int i = 0;
i < 2; ++
i) {
116 if (builtin_modules[
i] !=
nullptr) {
117 optixModuleDestroy(builtin_modules[
i]);
120 for (
int i = 0;
i < NUM_PIPELINES; ++
i) {
121 if (pipelines[
i] !=
nullptr) {
122 optixPipelineDestroy(pipelines[
i]);
125 for (
int i = 0;
i < NUM_PROGRAM_GROUPS; ++
i) {
126 if (groups[
i] !=
nullptr) {
127 optixProgramGroupDestroy(groups[
i]);
132 if (osl_camera_module !=
nullptr) {
133 optixModuleDestroy(osl_camera_module);
135 for (
const OptixModule &
module : osl_modules) {
137 optixModuleDestroy(
module);
140 for (
const OptixProgramGroup &group : osl_groups) {
141 if (group !=
nullptr) {
142 optixProgramGroupDestroy(group);
145 osl_colorsystem.free();
148 optixDeviceContextDestroy(context);
153 return make_unique<OptiXDeviceQueue>(
this);
162static string get_optix_include_dir()
164 const char *env_dir = getenv(
"OPTIX_ROOT_DIR");
165 const char *default_dir = CYCLES_RUNTIME_OPTIX_ROOT_DIR;
167 if (env_dir && env_dir[0]) {
168 const string env_include_dir =
path_join(env_dir,
"include");
169 return env_include_dir;
171 if (default_dir[0]) {
172 const string default_include_dir =
path_join(default_dir,
"include");
173 return default_include_dir;
179string OptiXDevice::compile_kernel_get_common_cflags(
const uint kernel_features)
181 string common_cflags = CUDADevice::compile_kernel_get_common_cflags(kernel_features);
184 common_cflags +=
string_printf(
" -I\"%s\"", get_optix_include_dir().c_str());
188 common_cflags +=
" --keep-device-functions";
191 return common_cflags;
194void OptiXDevice::create_optix_module(
TaskPool &pool,
195 OptixModuleCompileOptions &module_options,
200 OptixTask task =
nullptr;
201 result = optixModuleCreateWithTasks(context,
210 if (
result == OPTIX_SUCCESS) {
211 execute_optix_task(pool, task,
result);
215bool OptiXDevice::load_kernels(
const uint kernel_features)
227 const bool use_osl_shading =
false;
228 const bool use_osl_camera =
false;
232 const bool need_optix_kernels = (kernel_features &
238 string suffix = use_osl_shading ?
"_osl" :
243 if (need_optix_kernels) {
244 ptx_filename =
path_get(
"lib/kernel_optix" + suffix +
".ptx.zst");
245 if (use_adaptive_compilation() ||
path_file_size(ptx_filename) == -1) {
246 std::string optix_include_dir = get_optix_include_dir();
247 if (optix_include_dir.empty()) {
249 "Unable to compile OptiX kernels at runtime. Set OPTIX_ROOT_DIR environment variable "
250 "to a directory containing the OptiX SDK.");
255 "OptiX headers not found at %s, unable to compile OptiX kernels at runtime. Install "
256 "OptiX SDK in the specified location, or set OPTIX_ROOT_DIR environment variable to a "
257 "directory containing the OptiX SDK.",
258 optix_include_dir.c_str()));
265 if (!CUDADevice::load_kernels(kernel_features)) {
269 if (!need_optix_kernels) {
273 const CUDAContextScope scope(
this);
276 if (optix_module !=
nullptr) {
277 optixModuleDestroy(optix_module);
278 optix_module =
nullptr;
280 for (
int i = 0;
i < 2; ++
i) {
281 if (builtin_modules[
i] !=
nullptr) {
282 optixModuleDestroy(builtin_modules[
i]);
283 builtin_modules[
i] =
nullptr;
286 for (
int i = 0;
i < NUM_PIPELINES; ++
i) {
287 if (pipelines[
i] !=
nullptr) {
288 optixPipelineDestroy(pipelines[
i]);
289 pipelines[
i] =
nullptr;
292 for (
int i = 0;
i < NUM_PROGRAM_GROUPS; ++
i) {
293 if (groups[
i] !=
nullptr) {
294 optixProgramGroupDestroy(groups[
i]);
300 if (osl_camera_module !=
nullptr) {
301 optixModuleDestroy(osl_camera_module);
302 osl_camera_module =
nullptr;
306 for (
const OptixModule &
module : osl_modules) {
308 optixModuleDestroy(
module);
313 for (
const OptixProgramGroup &group : osl_groups) {
314 if (group !=
nullptr) {
315 optixProgramGroupDestroy(group);
321 OptixModuleCompileOptions module_options = {};
322 module_options.maxRegisterCount = 0;
325 module_options.optLevel = OPTIX_COMPILE_OPTIMIZATION_LEVEL_0;
326 module_options.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_FULL;
329 module_options.optLevel = OPTIX_COMPILE_OPTIMIZATION_LEVEL_3;
330 module_options.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE;
333 module_options.boundValues =
nullptr;
334 module_options.numBoundValues = 0;
335 module_options.payloadTypes =
nullptr;
336 module_options.numPayloadTypes = 0;
339 pipeline_options.usesMotionBlur =
false;
340 pipeline_options.traversableGraphFlags =
341 OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_SINGLE_LEVEL_INSTANCING;
342 pipeline_options.numPayloadValues = 8;
343 pipeline_options.numAttributeValues = 2;
344 pipeline_options.exceptionFlags = OPTIX_EXCEPTION_FLAG_NONE;
345 pipeline_options.pipelineLaunchParamsVariableName =
"kernel_params";
347 pipeline_options.usesPrimitiveTypeFlags = OPTIX_PRIMITIVE_TYPE_FLAGS_TRIANGLE;
349 pipeline_options.usesPrimitiveTypeFlags |= OPTIX_PRIMITIVE_TYPE_FLAGS_ROUND_LINEAR |
350 OPTIX_PRIMITIVE_TYPE_FLAGS_ROUND_CATMULLROM;
353 pipeline_options.usesPrimitiveTypeFlags |= OPTIX_PRIMITIVE_TYPE_FLAGS_CUSTOM;
360 pipeline_options.usesMotionBlur =
true;
363 pipeline_options.traversableGraphFlags = OPTIX_TRAVERSABLE_GRAPH_FLAG_ALLOW_ANY;
368 if (use_adaptive_compilation() ||
path_file_size(ptx_filename) == -1) {
369 string cflags = compile_kernel_get_common_cflags(kernel_features);
370 ptx_filename = compile_kernel(cflags, (
"kernel" + suffix).c_str(),
"optix",
true);
373 set_error(
string_printf(
"Failed to load OptiX kernel from '%s'", ptx_filename.c_str()));
379 create_optix_module(pool, module_options, ptx_data, optix_module,
result);
381 if (
result != OPTIX_SUCCESS) {
382 set_error(
string_printf(
"Failed to load OptiX kernel from '%s' (%s)",
383 ptx_filename.c_str(),
384 optixGetErrorName(
result)));
390 OptixProgramGroupDesc group_descs[NUM_PROGRAM_GROUPS] = {};
391 OptixProgramGroupOptions group_options = {};
392 group_descs[PG_RGEN_INTERSECT_CLOSEST].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
393 group_descs[PG_RGEN_INTERSECT_CLOSEST].raygen.module = optix_module;
394 group_descs[PG_RGEN_INTERSECT_CLOSEST].raygen.entryFunctionName =
395 "__raygen__kernel_optix_integrator_intersect_closest";
396 group_descs[PG_RGEN_INTERSECT_SHADOW].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
397 group_descs[PG_RGEN_INTERSECT_SHADOW].raygen.module = optix_module;
398 group_descs[PG_RGEN_INTERSECT_SHADOW].raygen.entryFunctionName =
399 "__raygen__kernel_optix_integrator_intersect_shadow";
400 group_descs[PG_RGEN_INTERSECT_SUBSURFACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
401 group_descs[PG_RGEN_INTERSECT_SUBSURFACE].raygen.module = optix_module;
402 group_descs[PG_RGEN_INTERSECT_SUBSURFACE].raygen.entryFunctionName =
403 "__raygen__kernel_optix_integrator_intersect_subsurface";
404 group_descs[PG_RGEN_INTERSECT_VOLUME_STACK].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
405 group_descs[PG_RGEN_INTERSECT_VOLUME_STACK].raygen.module = optix_module;
406 group_descs[PG_RGEN_INTERSECT_VOLUME_STACK].raygen.entryFunctionName =
407 "__raygen__kernel_optix_integrator_intersect_volume_stack";
408 group_descs[PG_RGEN_INTERSECT_DEDICATED_LIGHT].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
409 group_descs[PG_RGEN_INTERSECT_DEDICATED_LIGHT].raygen.module = optix_module;
410 group_descs[PG_RGEN_INTERSECT_DEDICATED_LIGHT].raygen.entryFunctionName =
411 "__raygen__kernel_optix_integrator_intersect_dedicated_light";
412 group_descs[PG_MISS].kind = OPTIX_PROGRAM_GROUP_KIND_MISS;
413 group_descs[PG_MISS].miss.module = optix_module;
414 group_descs[PG_MISS].miss.entryFunctionName =
"__miss__kernel_optix_miss";
415 group_descs[PG_HITD].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
416 group_descs[PG_HITD].hitgroup.moduleCH = optix_module;
417 group_descs[PG_HITD].hitgroup.entryFunctionNameCH =
"__closesthit__kernel_optix_hit";
418 group_descs[PG_HITD].hitgroup.moduleAH = optix_module;
419 group_descs[PG_HITD].hitgroup.entryFunctionNameAH =
"__anyhit__kernel_optix_visibility_test";
420 group_descs[PG_HITS].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
421 group_descs[PG_HITS].hitgroup.moduleAH = optix_module;
422 group_descs[PG_HITS].hitgroup.entryFunctionNameAH =
"__anyhit__kernel_optix_shadow_all_hit";
423 group_descs[PG_HITV].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
424 group_descs[PG_HITV].hitgroup.moduleCH = optix_module;
425 group_descs[PG_HITV].hitgroup.entryFunctionNameCH =
"__closesthit__kernel_optix_hit";
426 group_descs[PG_HITV].hitgroup.moduleAH = optix_module;
427 group_descs[PG_HITV].hitgroup.entryFunctionNameAH =
"__anyhit__kernel_optix_volume_test";
429 OptixProgramGroupDesc ignore_desc = {};
430 ignore_desc.kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
431 ignore_desc.hitgroup.moduleCH = optix_module;
432 ignore_desc.hitgroup.entryFunctionNameCH =
"__closesthit__kernel_optix_ignore";
433 ignore_desc.hitgroup.moduleAH = optix_module;
434 ignore_desc.hitgroup.entryFunctionNameAH =
"__anyhit__kernel_optix_ignore";
438 OptixBuiltinISOptions builtin_options = {};
439 builtin_options.builtinISModuleType = OPTIX_PRIMITIVE_TYPE_ROUND_CATMULLROM;
440 builtin_options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_TRACE |
441 OPTIX_BUILD_FLAG_ALLOW_COMPACTION | OPTIX_BUILD_FLAG_ALLOW_UPDATE;
442 builtin_options.curveEndcapFlags = OPTIX_CURVE_ENDCAP_DEFAULT;
443 builtin_options.usesMotionBlur =
false;
445 optix_assert(optixBuiltinISModuleGet(
446 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[0]));
448 group_descs[PG_HITD].hitgroup.moduleIS = builtin_modules[0];
449 group_descs[PG_HITD].hitgroup.entryFunctionNameIS =
nullptr;
450 group_descs[PG_HITS].hitgroup.moduleIS = builtin_modules[0];
451 group_descs[PG_HITS].hitgroup.entryFunctionNameIS =
nullptr;
453 if (pipeline_options.usesMotionBlur) {
454 builtin_options.usesMotionBlur =
true;
456 optix_assert(optixBuiltinISModuleGet(
457 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[1]));
459 group_descs[PG_HITD_MOTION] = group_descs[PG_HITD];
460 group_descs[PG_HITD_MOTION].hitgroup.moduleIS = builtin_modules[1];
461 group_descs[PG_HITS_MOTION] = group_descs[PG_HITS];
462 group_descs[PG_HITS_MOTION].hitgroup.moduleIS = builtin_modules[1];
465 builtin_options.builtinISModuleType = OPTIX_PRIMITIVE_TYPE_ROUND_LINEAR;
466 builtin_options.usesMotionBlur =
false;
468 optix_assert(optixBuiltinISModuleGet(
469 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[2]));
471 group_descs[PG_HITD_CURVE_LINEAR] = group_descs[PG_HITD];
472 group_descs[PG_HITD_CURVE_LINEAR].hitgroup.moduleIS = builtin_modules[2];
473 group_descs[PG_HITS_CURVE_LINEAR] = group_descs[PG_HITS];
474 group_descs[PG_HITS_CURVE_LINEAR].hitgroup.moduleIS = builtin_modules[2];
475 group_descs[PG_HITV_CURVE_LINEAR] = ignore_desc;
476 group_descs[PG_HITL_CURVE_LINEAR] = ignore_desc;
478 if (pipeline_options.usesMotionBlur) {
479 builtin_options.usesMotionBlur =
true;
481 optix_assert(optixBuiltinISModuleGet(
482 context, &module_options, &pipeline_options, &builtin_options, &builtin_modules[3]));
484 group_descs[PG_HITD_CURVE_LINEAR_MOTION] = group_descs[PG_HITD_CURVE_LINEAR];
485 group_descs[PG_HITD_CURVE_LINEAR_MOTION].hitgroup.moduleIS = builtin_modules[3];
486 group_descs[PG_HITS_CURVE_LINEAR_MOTION] = group_descs[PG_HITS_CURVE_LINEAR];
487 group_descs[PG_HITS_CURVE_LINEAR_MOTION].hitgroup.moduleIS = builtin_modules[3];
488 group_descs[PG_HITV_CURVE_LINEAR_MOTION] = ignore_desc;
489 group_descs[PG_HITL_CURVE_LINEAR_MOTION] = ignore_desc;
494 group_descs[PG_HITD_CURVE_RIBBON] = group_descs[PG_HITD];
495 group_descs[PG_HITD_CURVE_RIBBON].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
496 group_descs[PG_HITD_CURVE_RIBBON].hitgroup.moduleIS = optix_module;
497 group_descs[PG_HITD_CURVE_RIBBON].hitgroup.entryFunctionNameIS =
498 "__intersection__curve_ribbon";
499 group_descs[PG_HITS_CURVE_RIBBON] = group_descs[PG_HITS];
500 group_descs[PG_HITS_CURVE_RIBBON].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
501 group_descs[PG_HITS_CURVE_RIBBON].hitgroup.moduleIS = optix_module;
502 group_descs[PG_HITS_CURVE_RIBBON].hitgroup.entryFunctionNameIS =
503 "__intersection__curve_ribbon";
504 group_descs[PG_HITV_CURVE_RIBBON] = ignore_desc;
505 group_descs[PG_HITL_CURVE_RIBBON] = ignore_desc;
509 group_descs[PG_HITD_POINTCLOUD] = group_descs[PG_HITD];
510 group_descs[PG_HITD_POINTCLOUD].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
511 group_descs[PG_HITD_POINTCLOUD].hitgroup.moduleIS = optix_module;
512 group_descs[PG_HITD_POINTCLOUD].hitgroup.entryFunctionNameIS =
"__intersection__point";
513 group_descs[PG_HITS_POINTCLOUD] = group_descs[PG_HITS];
514 group_descs[PG_HITS_POINTCLOUD].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
515 group_descs[PG_HITS_POINTCLOUD].hitgroup.moduleIS = optix_module;
516 group_descs[PG_HITS_POINTCLOUD].hitgroup.entryFunctionNameIS =
"__intersection__point";
517 group_descs[PG_HITV_POINTCLOUD] = ignore_desc;
518 group_descs[PG_HITL_POINTCLOUD] = ignore_desc;
523 group_descs[PG_HITL].kind = OPTIX_PROGRAM_GROUP_KIND_HITGROUP;
524 group_descs[PG_HITL].hitgroup.moduleAH = optix_module;
525 group_descs[PG_HITL].hitgroup.entryFunctionNameAH =
"__anyhit__kernel_optix_local_hit";
530 group_descs[PG_RGEN_SHADE_SURFACE_RAYTRACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
531 group_descs[PG_RGEN_SHADE_SURFACE_RAYTRACE].raygen.module = optix_module;
532 group_descs[PG_RGEN_SHADE_SURFACE_RAYTRACE].raygen.entryFunctionName =
533 "__raygen__kernel_optix_integrator_shade_surface_raytrace";
537 if (!use_osl_shading) {
538 group_descs[PG_CALL_SVM_AO].kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
539 group_descs[PG_CALL_SVM_AO].callables.moduleDC = optix_module;
540 group_descs[PG_CALL_SVM_AO].callables.entryFunctionNameDC =
"__direct_callable__svm_node_ao";
541 group_descs[PG_CALL_SVM_BEVEL].kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
542 group_descs[PG_CALL_SVM_BEVEL].callables.moduleDC = optix_module;
543 group_descs[PG_CALL_SVM_BEVEL].callables.entryFunctionNameDC =
544 "__direct_callable__svm_node_bevel";
549 group_descs[PG_RGEN_SHADE_SURFACE_MNEE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
550 group_descs[PG_RGEN_SHADE_SURFACE_MNEE].raygen.module = optix_module;
551 group_descs[PG_RGEN_SHADE_SURFACE_MNEE].raygen.entryFunctionName =
552 "__raygen__kernel_optix_integrator_shade_surface_mnee";
556 if (use_osl_shading) {
557 group_descs[PG_RGEN_SHADE_BACKGROUND].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
558 group_descs[PG_RGEN_SHADE_BACKGROUND].raygen.module = optix_module;
559 group_descs[PG_RGEN_SHADE_BACKGROUND].raygen.entryFunctionName =
560 "__raygen__kernel_optix_integrator_shade_background";
561 group_descs[PG_RGEN_SHADE_LIGHT].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
562 group_descs[PG_RGEN_SHADE_LIGHT].raygen.module = optix_module;
563 group_descs[PG_RGEN_SHADE_LIGHT].raygen.entryFunctionName =
564 "__raygen__kernel_optix_integrator_shade_light";
565 group_descs[PG_RGEN_SHADE_SURFACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
566 group_descs[PG_RGEN_SHADE_SURFACE].raygen.module = optix_module;
567 group_descs[PG_RGEN_SHADE_SURFACE].raygen.entryFunctionName =
568 "__raygen__kernel_optix_integrator_shade_surface";
569 group_descs[PG_RGEN_SHADE_VOLUME].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
570 group_descs[PG_RGEN_SHADE_VOLUME].raygen.module = optix_module;
571 group_descs[PG_RGEN_SHADE_VOLUME].raygen.entryFunctionName =
572 "__raygen__kernel_optix_integrator_shade_volume";
573 group_descs[PG_RGEN_SHADE_VOLUME_RAY_MARCHING].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
574 group_descs[PG_RGEN_SHADE_VOLUME_RAY_MARCHING].raygen.module = optix_module;
575 group_descs[PG_RGEN_SHADE_VOLUME_RAY_MARCHING].raygen.entryFunctionName =
576 "__raygen__kernel_optix_integrator_shade_volume_ray_marching";
577 group_descs[PG_RGEN_SHADE_SHADOW].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
578 group_descs[PG_RGEN_SHADE_SHADOW].raygen.module = optix_module;
579 group_descs[PG_RGEN_SHADE_SHADOW].raygen.entryFunctionName =
580 "__raygen__kernel_optix_integrator_shade_shadow";
581 group_descs[PG_RGEN_SHADE_DEDICATED_LIGHT].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
582 group_descs[PG_RGEN_SHADE_DEDICATED_LIGHT].raygen.module = optix_module;
583 group_descs[PG_RGEN_SHADE_DEDICATED_LIGHT].raygen.entryFunctionName =
584 "__raygen__kernel_optix_integrator_shade_dedicated_light";
585 group_descs[PG_RGEN_EVAL_DISPLACE].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
586 group_descs[PG_RGEN_EVAL_DISPLACE].raygen.module = optix_module;
587 group_descs[PG_RGEN_EVAL_DISPLACE].raygen.entryFunctionName =
588 "__raygen__kernel_optix_shader_eval_displace";
589 group_descs[PG_RGEN_EVAL_BACKGROUND].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
590 group_descs[PG_RGEN_EVAL_BACKGROUND].raygen.module = optix_module;
591 group_descs[PG_RGEN_EVAL_BACKGROUND].raygen.entryFunctionName =
592 "__raygen__kernel_optix_shader_eval_background";
593 group_descs[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
594 group_descs[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY].raygen.module = optix_module;
595 group_descs[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY].raygen.entryFunctionName =
596 "__raygen__kernel_optix_shader_eval_curve_shadow_transparency";
597 group_descs[PG_RGEN_EVAL_VOLUME_DENSITY].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
598 group_descs[PG_RGEN_EVAL_VOLUME_DENSITY].raygen.module = optix_module;
599 group_descs[PG_RGEN_EVAL_VOLUME_DENSITY].raygen.entryFunctionName =
600 "__raygen__kernel_optix_shader_eval_volume_density";
605 if (use_osl_camera) {
607 string ptx_data, ptx_filename =
path_get(
"lib/kernel_optix_osl_camera.ptx.zst");
610 string_printf(
"Failed to load OptiX OSL camera kernel from '%s'", ptx_filename.c_str()));
616 create_optix_module(pool, module_options, ptx_data, osl_camera_module,
result);
618 if (
result != OPTIX_SUCCESS) {
619 set_error(
string_printf(
"Failed to load OptiX kernel from '%s' (%s)",
620 ptx_filename.c_str(),
621 optixGetErrorName(
result)));
625 group_descs[PG_RGEN_INIT_FROM_CAMERA].kind = OPTIX_PROGRAM_GROUP_KIND_RAYGEN;
626 group_descs[PG_RGEN_INIT_FROM_CAMERA].raygen.module = osl_camera_module;
627 group_descs[PG_RGEN_INIT_FROM_CAMERA].raygen.entryFunctionName =
628 "__raygen__kernel_optix_integrator_init_from_camera";
632 optix_assert(optixProgramGroupCreate(
633 context, group_descs, NUM_PROGRAM_GROUPS, &group_options,
nullptr,
nullptr, groups));
636 OptixStackSizes stack_size[NUM_PROGRAM_GROUPS] = {};
638 sbt_data.alloc(NUM_PROGRAM_GROUPS);
639 memset(sbt_data.host_pointer, 0,
sizeof(SbtRecord) * NUM_PROGRAM_GROUPS);
640 for (
int i = 0;
i < NUM_PROGRAM_GROUPS; ++
i) {
641 optix_assert(optixSbtRecordPackHeader(groups[
i], &sbt_data[
i]));
642 optix_assert(optixProgramGroupGetStackSize(groups[
i], &stack_size[
i],
nullptr));
644 sbt_data.copy_to_device();
647 unsigned int trace_css = stack_size[PG_HITD].cssCH;
649 trace_css = std::max(trace_css, stack_size[PG_HITD].cssIS + stack_size[PG_HITD].cssAH);
650 trace_css = std::max(trace_css, stack_size[PG_HITS].cssIS + stack_size[PG_HITS].cssAH);
651 trace_css = std::max(trace_css, stack_size[PG_HITL].cssIS + stack_size[PG_HITL].cssAH);
652 trace_css = std::max(trace_css, stack_size[PG_HITV].cssIS + stack_size[PG_HITV].cssAH);
653 trace_css = std::max(trace_css,
654 stack_size[PG_HITD_MOTION].cssIS + stack_size[PG_HITD_MOTION].cssAH);
655 trace_css = std::max(trace_css,
656 stack_size[PG_HITS_MOTION].cssIS + stack_size[PG_HITS_MOTION].cssAH);
657 trace_css = std::max(
658 trace_css, stack_size[PG_HITD_CURVE_LINEAR].cssIS + stack_size[PG_HITD_CURVE_LINEAR].cssAH);
659 trace_css = std::max(
660 trace_css, stack_size[PG_HITS_CURVE_LINEAR].cssIS + stack_size[PG_HITS_CURVE_LINEAR].cssAH);
661 trace_css = std::max(trace_css,
662 stack_size[PG_HITD_CURVE_LINEAR_MOTION].cssIS +
663 stack_size[PG_HITD_CURVE_LINEAR_MOTION].cssAH);
664 trace_css = std::max(trace_css,
665 stack_size[PG_HITS_CURVE_LINEAR_MOTION].cssIS +
666 stack_size[PG_HITS_CURVE_LINEAR_MOTION].cssAH);
667 trace_css = std::max(
668 trace_css, stack_size[PG_HITD_CURVE_RIBBON].cssIS + stack_size[PG_HITD_CURVE_RIBBON].cssAH);
669 trace_css = std::max(
670 trace_css, stack_size[PG_HITS_CURVE_RIBBON].cssIS + stack_size[PG_HITS_CURVE_RIBBON].cssAH);
671 trace_css = std::max(
672 trace_css, stack_size[PG_HITD_POINTCLOUD].cssIS + stack_size[PG_HITD_POINTCLOUD].cssAH);
673 trace_css = std::max(
674 trace_css, stack_size[PG_HITS_POINTCLOUD].cssIS + stack_size[PG_HITS_POINTCLOUD].cssAH);
676 OptixPipelineLinkOptions link_options = {};
677 link_options.maxTraceDepth = 1;
679 if (use_osl_shading || use_osl_camera) {
685 pipeline_groups.reserve(NUM_PROGRAM_GROUPS);
687 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE_RAYTRACE]);
688 pipeline_groups.push_back(groups[PG_CALL_SVM_AO]);
689 pipeline_groups.push_back(groups[PG_CALL_SVM_BEVEL]);
692 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE_MNEE]);
694 pipeline_groups.push_back(groups[PG_MISS]);
695 pipeline_groups.push_back(groups[PG_HITD]);
696 pipeline_groups.push_back(groups[PG_HITS]);
697 pipeline_groups.push_back(groups[PG_HITL]);
698 pipeline_groups.push_back(groups[PG_HITV]);
699 if (pipeline_options.usesMotionBlur) {
700 pipeline_groups.push_back(groups[PG_HITD_MOTION]);
701 pipeline_groups.push_back(groups[PG_HITS_MOTION]);
702 pipeline_groups.push_back(groups[PG_HITV_MOTION]);
703 pipeline_groups.push_back(groups[PG_HITL_MOTION]);
706 pipeline_groups.push_back(groups[PG_HITD_CURVE_LINEAR]);
707 pipeline_groups.push_back(groups[PG_HITS_CURVE_LINEAR]);
708 pipeline_groups.push_back(groups[PG_HITV_CURVE_LINEAR]);
709 pipeline_groups.push_back(groups[PG_HITL_CURVE_LINEAR]);
710 if (pipeline_options.usesMotionBlur) {
711 pipeline_groups.push_back(groups[PG_HITD_CURVE_LINEAR_MOTION]);
712 pipeline_groups.push_back(groups[PG_HITS_CURVE_LINEAR_MOTION]);
713 pipeline_groups.push_back(groups[PG_HITV_CURVE_LINEAR_MOTION]);
714 pipeline_groups.push_back(groups[PG_HITL_CURVE_LINEAR_MOTION]);
718 pipeline_groups.push_back(groups[PG_HITD_CURVE_RIBBON]);
719 pipeline_groups.push_back(groups[PG_HITS_CURVE_RIBBON]);
720 pipeline_groups.push_back(groups[PG_HITV_CURVE_RIBBON]);
721 pipeline_groups.push_back(groups[PG_HITL_CURVE_RIBBON]);
724 pipeline_groups.push_back(groups[PG_HITD_POINTCLOUD]);
725 pipeline_groups.push_back(groups[PG_HITS_POINTCLOUD]);
726 pipeline_groups.push_back(groups[PG_HITV_POINTCLOUD]);
727 pipeline_groups.push_back(groups[PG_HITL_POINTCLOUD]);
730 optix_assert(optixPipelineCreate(context,
733 pipeline_groups.data(),
734 pipeline_groups.size(),
737 &pipelines[PIP_SHADE]));
740 const unsigned int css = std::max(stack_size[PG_RGEN_SHADE_SURFACE_RAYTRACE].cssRG,
741 stack_size[PG_RGEN_SHADE_SURFACE_MNEE].cssRG) +
742 link_options.maxTraceDepth * trace_css;
743 const unsigned int dss = std::max(stack_size[PG_CALL_SVM_AO].dssDC,
744 stack_size[PG_CALL_SVM_BEVEL].dssDC);
747 optix_assert(optixPipelineSetStackSize(
748 pipelines[PIP_SHADE], 0, dss, css, pipeline_options.usesMotionBlur ? 3 : 2));
753 pipeline_groups.reserve(NUM_PROGRAM_GROUPS);
754 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_CLOSEST]);
755 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_SHADOW]);
756 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_SUBSURFACE]);
757 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_VOLUME_STACK]);
758 pipeline_groups.push_back(groups[PG_RGEN_INTERSECT_DEDICATED_LIGHT]);
759 pipeline_groups.push_back(groups[PG_MISS]);
760 pipeline_groups.push_back(groups[PG_HITD]);
761 pipeline_groups.push_back(groups[PG_HITS]);
762 pipeline_groups.push_back(groups[PG_HITL]);
763 pipeline_groups.push_back(groups[PG_HITV]);
764 if (pipeline_options.usesMotionBlur) {
765 pipeline_groups.push_back(groups[PG_HITD_MOTION]);
766 pipeline_groups.push_back(groups[PG_HITS_MOTION]);
769 pipeline_groups.push_back(groups[PG_HITD_CURVE_LINEAR]);
770 pipeline_groups.push_back(groups[PG_HITS_CURVE_LINEAR]);
771 if (pipeline_options.usesMotionBlur) {
772 pipeline_groups.push_back(groups[PG_HITD_CURVE_LINEAR_MOTION]);
773 pipeline_groups.push_back(groups[PG_HITS_CURVE_LINEAR_MOTION]);
777 pipeline_groups.push_back(groups[PG_HITD_CURVE_RIBBON]);
778 pipeline_groups.push_back(groups[PG_HITS_CURVE_RIBBON]);
781 pipeline_groups.push_back(groups[PG_HITD_POINTCLOUD]);
782 pipeline_groups.push_back(groups[PG_HITS_POINTCLOUD]);
785 optix_assert(optixPipelineCreate(context,
788 pipeline_groups.data(),
789 pipeline_groups.size(),
792 &pipelines[PIP_INTERSECT]));
795 const unsigned int css =
796 std::max(stack_size[PG_RGEN_INTERSECT_CLOSEST].cssRG,
797 std::max(stack_size[PG_RGEN_INTERSECT_SHADOW].cssRG,
798 std::max(stack_size[PG_RGEN_INTERSECT_SUBSURFACE].cssRG,
799 stack_size[PG_RGEN_INTERSECT_VOLUME_STACK].cssRG))) +
800 link_options.maxTraceDepth * trace_css;
802 optix_assert(optixPipelineSetStackSize(
803 pipelines[PIP_INTERSECT], 0, 0, css, pipeline_options.usesMotionBlur ? 3 : 2));
806 return !have_error();
809bool OptiXDevice::load_osl_kernels()
821 auto get_osl_kernel = [&](
const OSL::ShaderGroupRef &group) {
825 string osl_ptx, fused_name;
826 osl_globals.ss->getattribute(group.get(),
"group_fused_name", fused_name);
827 osl_globals.ss->getattribute(
828 group.get(),
"ptx_compiled_version", OSL::TypeDesc::PTR, &osl_ptx);
830 int groupdata_size = 0;
831 osl_globals.ss->getattribute(group.get(),
"llvm_groupdata_size", groupdata_size);
832 if (groupdata_size == 0) {
834 osl_globals.ss->getattribute(group.get(),
"groupdata_size", groupdata_size);
836 if (groupdata_size > 2048) {
838 string_printf(
"Requested OSL group data size (%d) is greater than the maximum "
839 "supported with OptiX (2048)",
844 return OSLKernel{std::move(osl_ptx), std::move(fused_name)};
850 osl_kernels.emplace_back(get_osl_kernel(osl_globals.camera_state));
851 for (
const OSL::ShaderGroupRef &group : osl_globals.surface_state) {
852 osl_kernels.emplace_back(get_osl_kernel(group));
854 for (
const OSL::ShaderGroupRef &group : osl_globals.volume_state) {
855 osl_kernels.emplace_back(get_osl_kernel(group));
857 for (
const OSL::ShaderGroupRef &group : osl_globals.displacement_state) {
858 osl_kernels.emplace_back(get_osl_kernel(group));
860 for (
const OSL::ShaderGroupRef &group : osl_globals.bump_state) {
861 osl_kernels.emplace_back(get_osl_kernel(group));
868 const CUDAContextScope scope(
this);
870 if (pipelines[PIP_SHADE]) {
871 optixPipelineDestroy(pipelines[PIP_SHADE]);
874 for (OptixModule &
module : osl_modules) {
876 optixModuleDestroy(
module);
880 for (OptixProgramGroup &group : osl_groups) {
881 if (group !=
nullptr) {
882 optixProgramGroupDestroy(group);
889 if (osl_kernels.size() == 1 && osl_kernels[0].ptx.empty()) {
893 OptixProgramGroupOptions group_options = {};
894 OptixModuleCompileOptions module_options = {};
895 module_options.optLevel = OPTIX_COMPILE_OPTIMIZATION_LEVEL_3;
896 module_options.debugLevel = OPTIX_COMPILE_DEBUG_LEVEL_NONE;
900 size_t id_osl_services = osl_kernels.size();
901 size_t id_osl_shadeops = osl_kernels.size() + 1;
902 osl_groups.resize(osl_kernels.size() + 2);
903 osl_modules.resize(osl_kernels.size() + 2);
906 string osl_services_ptx, ptx_filename =
path_get(
"lib/kernel_optix_osl_services.ptx.zst");
908 set_error(
string_printf(
"Failed to load OptiX OSL services kernel from '%s'",
909 ptx_filename.c_str()));
913 const char *shadeops_ptx_ptr =
nullptr;
914 osl_globals.ss->getattribute(
"shadeops_cuda_ptx", OSL::TypeDesc::PTR, &shadeops_ptx_ptr);
915 int shadeops_ptx_size = 0;
916 osl_globals.ss->getattribute(
"shadeops_cuda_ptx_size", OSL::TypeDesc::INT, &shadeops_ptx_size);
917 string shadeops_ptx(shadeops_ptx_ptr, shadeops_ptx_size);
920 OptixResult services_result, shadeops_result;
922 pool, module_options, osl_services_ptx, osl_modules[id_osl_services], services_result);
924 pool, module_options, shadeops_ptx, osl_modules[id_osl_shadeops], shadeops_result);
928 if (services_result != OPTIX_SUCCESS) {
929 set_error(
string_printf(
"Failed to load OptiX OSL services kernel from '%s' (%s)",
930 ptx_filename.c_str(),
931 optixGetErrorName(services_result)));
934 OptixProgramGroupDesc group_desc = {};
935 group_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
936 group_desc.callables.entryFunctionNameDC =
"__direct_callable__dummy_services";
937 group_desc.callables.moduleDC = osl_modules[id_osl_services];
939 optix_assert(optixProgramGroupCreate(context,
945 &osl_groups[id_osl_services]));
949 if (shadeops_result != OPTIX_SUCCESS) {
950 set_error(
string_printf(
"Failed to load OptiX OSL shadeops kernel (%s)",
951 optixGetErrorName(shadeops_result)));
954 OptixProgramGroupDesc group_desc = {};
955 group_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
956 group_desc.callables.entryFunctionNameDC =
"__direct_callable__dummy_shadeops";
957 group_desc.callables.moduleDC = osl_modules[id_osl_shadeops];
959 optix_assert(optixProgramGroupCreate(context,
965 &osl_groups[id_osl_shadeops]));
972 for (
size_t i = 0;
i < osl_kernels.size(); ++
i) {
973 if (osl_kernels[
i].ptx.empty()) {
977 create_optix_module(pool, module_options, osl_kernels[
i].ptx, osl_modules[
i], results[
i]);
982 for (
size_t i = 0;
i < osl_kernels.size(); ++
i) {
983 if (osl_kernels[
i].ptx.empty()) {
987 if (results[
i] != OPTIX_SUCCESS) {
988 set_error(
string_printf(
"Failed to load OptiX OSL kernel for %s (%s)",
989 osl_kernels[
i].fused_entry.c_str(),
990 optixGetErrorName(results[
i])));
994 OptixProgramGroupDesc group_desc = {};
995 group_desc.kind = OPTIX_PROGRAM_GROUP_KIND_CALLABLES;
996 group_desc.callables.entryFunctionNameDC = osl_kernels[
i].fused_entry.c_str();
997 group_desc.callables.moduleDC = osl_modules[
i];
999 optix_assert(optixProgramGroupCreate(
1000 context, &group_desc, 1, &group_options,
nullptr,
nullptr, &osl_groups[
i]));
1004 sbt_data.alloc(NUM_PROGRAM_GROUPS + osl_groups.size());
1005 for (
int i = 0;
i < NUM_PROGRAM_GROUPS; ++
i) {
1006 optix_assert(optixSbtRecordPackHeader(groups[
i], &sbt_data[
i]));
1008 for (
size_t i = 0;
i < osl_groups.size(); ++
i) {
1009 if (osl_groups[
i] !=
nullptr) {
1010 optix_assert(optixSbtRecordPackHeader(osl_groups[
i], &sbt_data[NUM_PROGRAM_GROUPS +
i]));
1015 optix_assert(optixSbtRecordPackHeader(osl_groups[id_osl_services],
1016 &sbt_data[NUM_PROGRAM_GROUPS +
i]));
1019 sbt_data.copy_to_device();
1021 OptixPipelineLinkOptions link_options = {};
1022 link_options.maxTraceDepth = 0;
1026 pipeline_groups.reserve(NUM_PROGRAM_GROUPS);
1027 pipeline_groups.push_back(groups[PG_RGEN_SHADE_BACKGROUND]);
1028 pipeline_groups.push_back(groups[PG_RGEN_SHADE_LIGHT]);
1029 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE]);
1030 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE_RAYTRACE]);
1031 pipeline_groups.push_back(groups[PG_CALL_SVM_AO]);
1032 pipeline_groups.push_back(groups[PG_CALL_SVM_BEVEL]);
1033 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SURFACE_MNEE]);
1034 pipeline_groups.push_back(groups[PG_RGEN_SHADE_VOLUME]);
1035 pipeline_groups.push_back(groups[PG_RGEN_SHADE_SHADOW]);
1036 pipeline_groups.push_back(groups[PG_RGEN_SHADE_DEDICATED_LIGHT]);
1037 pipeline_groups.push_back(groups[PG_RGEN_EVAL_DISPLACE]);
1038 pipeline_groups.push_back(groups[PG_RGEN_EVAL_BACKGROUND]);
1039 pipeline_groups.push_back(groups[PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY]);
1040 pipeline_groups.push_back(groups[PG_RGEN_INIT_FROM_CAMERA]);
1041 pipeline_groups.push_back(groups[PG_RGEN_EVAL_VOLUME_DENSITY]);
1043 for (
const OptixProgramGroup &group : osl_groups) {
1044 if (group !=
nullptr) {
1045 pipeline_groups.push_back(group);
1049 optix_assert(optixPipelineCreate(context,
1052 pipeline_groups.data(),
1053 pipeline_groups.size(),
1056 &pipelines[PIP_SHADE]));
1059 OptixStackSizes stack_size[NUM_PROGRAM_GROUPS] = {};
1062 for (
int i = 0;
i < NUM_PROGRAM_GROUPS; ++
i) {
1063 optix_assert(optixProgramGroupGetStackSize(groups[
i], &stack_size[
i],
nullptr));
1065 for (
size_t i = 0;
i < osl_groups.size(); ++
i) {
1066 if (osl_groups[
i] !=
nullptr) {
1067 optix_assert(optixProgramGroupGetStackSize(
1068 osl_groups[
i], &osl_stack_size[
i], pipelines[PIP_SHADE]));
1072 const unsigned int css = std::max(stack_size[PG_RGEN_SHADE_SURFACE_RAYTRACE].cssRG,
1073 stack_size[PG_RGEN_SHADE_SURFACE_MNEE].cssRG);
1074 unsigned int dss = std::max(stack_size[PG_CALL_SVM_AO].dssDC,
1075 stack_size[PG_CALL_SVM_BEVEL].dssDC);
1076 for (
unsigned int i = 0;
i < osl_stack_size.size(); ++
i) {
1077 dss = std::max(dss, osl_stack_size[
i].dssDC);
1080 optix_assert(optixPipelineSetStackSize(
1081 pipelines[PIP_SHADE], 0, dss, css, pipeline_options.usesMotionBlur ? 3 : 2));
1093 size_t cpu_data_sizes[2] = {0, 0};
1094 osl_globals.ss->getattribute(
"colorsystem", OSL::TypeDesc::PTR, &cpu_data);
1095 osl_globals.ss->getattribute(
1096 "colorsystem:sizes", TypeDesc(TypeDesc::LONGLONG, 2), (
void *)cpu_data_sizes);
1098 size_t cpu_full_size = cpu_data_sizes[0];
1099 size_t num_strings = cpu_data_sizes[1];
1100 size_t fixed_data_size = cpu_full_size -
sizeof(ustringhash) * num_strings;
1103 uint8_t *gpu_data = osl_colorsystem.alloc(fixed_data_size +
sizeof(
size_t) * num_strings);
1106 memcpy(gpu_data, cpu_data, fixed_data_size);
1109 ustringhash *cpu_strings =
reinterpret_cast<ustringhash *
>(cpu_data + fixed_data_size);
1110 size_t *gpu_strings =
reinterpret_cast<size_t *
>(gpu_data + fixed_data_size);
1111 for (
int i = 0;
i < num_strings;
i++) {
1112 gpu_strings[
i] = cpu_strings[
i].hash();
1116 osl_colorsystem.copy_to_device();
1119 &osl_colorsystem.device_pointer,
1123 return !have_error();
1129OSLGlobals *OptiXDevice::get_cpu_osl_memory()
1132 return &osl_globals;
1138bool OptiXDevice::build_optix_bvh(BVHOptiX *bvh,
1139 OptixBuildOperation operation,
1140 const OptixBuildInput &build_input,
1141 const uint16_t num_motion_steps)
1150 const CUDAContextScope scope(
this);
1155 OptixAccelBufferSizes sizes = {};
1156 OptixAccelBuildOptions
options = {};
1157 options.operation = operation;
1158 if (build_input.type == OPTIX_BUILD_INPUT_TYPE_CURVES) {
1161 options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_TRACE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION |
1162 OPTIX_BUILD_FLAG_ALLOW_UPDATE;
1163 use_fast_trace_bvh =
true;
1165 else if (use_fast_trace_bvh) {
1166 LOG_INFO <<
"Using fast to trace OptiX BVH";
1167 options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_TRACE | OPTIX_BUILD_FLAG_ALLOW_COMPACTION;
1170 LOG_INFO <<
"Using fast to update OptiX BVH";
1171 options.buildFlags = OPTIX_BUILD_FLAG_PREFER_FAST_BUILD | OPTIX_BUILD_FLAG_ALLOW_UPDATE;
1174 options.motionOptions.numKeys = num_motion_steps;
1175 options.motionOptions.flags = OPTIX_MOTION_FLAG_START_VANISH | OPTIX_MOTION_FLAG_END_VANISH;
1176 options.motionOptions.timeBegin = 0.0f;
1177 options.motionOptions.timeEnd = 1.0f;
1179 optix_assert(optixAccelComputeMemoryUsage(context, &
options, &build_input, 1, &sizes));
1183 temp_mem.alloc_to_device(
align_up(sizes.tempSizeInBytes, 8) + 8);
1184 if (!temp_mem.device_pointer) {
1191 if (operation == OPTIX_BUILD_OPERATION_BUILD) {
1203 OptixAccelEmitDesc compacted_size_prop = {};
1204 compacted_size_prop.
type = OPTIX_PROPERTY_TYPE_COMPACTED_SIZE;
1207 compacted_size_prop.result =
align_up(temp_mem.device_pointer + sizes.tempSizeInBytes, 8);
1209 OptixTraversableHandle out_handle = 0;
1210 optix_assert(optixAccelBuild(context,
1215 temp_mem.device_pointer,
1216 sizes.tempSizeInBytes,
1218 sizes.outputSizeInBytes,
1220 use_fast_trace_bvh ? &compacted_size_prop :
nullptr,
1221 use_fast_trace_bvh ? 1 : 0));
1222 bvh->traversable_handle =
static_cast<uint64_t>(out_handle);
1225 cuda_assert(cuStreamSynchronize(
nullptr));
1229 if (use_fast_trace_bvh) {
1230 uint64_t compacted_size = sizes.outputSizeInBytes;
1231 cuda_assert(cuMemcpyDtoH(&compacted_size, compacted_size_prop.result,
sizeof(compacted_size)));
1237 if (compacted_size < sizes.outputSizeInBytes) {
1239 compacted_data.alloc_to_device(compacted_size);
1240 if (!compacted_data.device_pointer) {
1243 return !have_error();
1246 optix_assert(optixAccelCompact(context,
1249 compacted_data.device_pointer,
1252 bvh->traversable_handle =
static_cast<uint64_t>(out_handle);
1255 cuda_assert(cuStreamSynchronize(
nullptr));
1257 std::swap(out_data.
device_size, compacted_data.device_size);
1258 std::swap(out_data.
device_pointer, compacted_data.device_pointer);
1264 return !have_error();
1271 free_bvh_memory_delayed();
1273 BVHOptiX *
const bvh_optix =
static_cast<BVHOptiX *
>(bvh);
1275 progress.
set_substatus(
"Building OptiX acceleration structure");
1282 OptixBuildOperation operation = OPTIX_BUILD_OPERATION_BUILD;
1283 if (
refit && !use_fast_trace_bvh) {
1284 assert(bvh_optix->traversable_handle != 0);
1285 operation = OPTIX_BUILD_OPERATION_UPDATE;
1288 bvh_optix->as_data->free();
1289 bvh_optix->traversable_handle = 0;
1296 Hair *
const hair =
static_cast<Hair *const
>(geom);
1303 size_t num_motion_steps = 1;
1305 if (pipeline_options.usesMotionBlur && hair->get_use_motion_blur() && motion_keys) {
1306 num_motion_steps = hair->get_motion_steps();
1313 size_t num_vertices = num_segments * 4;
1316 index_data.alloc(num_segments);
1317 vertex_data.alloc(num_vertices * num_motion_steps);
1321 index_data.alloc(num_segments);
1322 vertex_data.alloc(num_vertices * num_motion_steps);
1325 aabb_data.alloc(num_segments * num_motion_steps);
1329 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
1331 const float3 *keys = hair->get_curve_keys().data();
1332 size_t center_step = (num_motion_steps - 1) / 2;
1333 if (
step != center_step) {
1334 size_t attr_offset = (
step > center_step) ?
step - 1 :
step;
1336 keys = motion_keys->
data_float3() + attr_offset * hair->get_curve_keys().size();
1340 for (
size_t curve_index = 0, segment_index = 0, vertex_index =
step * num_vertices;
1345 const array<float> &curve_radius = hair->get_curve_radius();
1348 const int first_key_index = curve.
first_key;
1352 index_data[segment_index++] = vertex_index;
1354 vertex_data[vertex_index++] =
make_float4(keys[first_key_index + k].
x,
1355 keys[first_key_index + k].
y,
1356 keys[first_key_index + k].
z,
1357 curve_radius[first_key_index + k]);
1360 const int last_key_index = first_key_index + curve.
num_keys - 1;
1362 vertex_data[vertex_index++] =
make_float4(keys[last_key_index].
x,
1363 keys[last_key_index].
y,
1364 keys[last_key_index].
z,
1365 curve_radius[last_key_index]);
1369 const int first_key_index = curve.
first_key;
1371 vertex_data[vertex_index++] =
make_float4(keys[first_key_index].
x,
1372 keys[first_key_index].
y,
1373 keys[first_key_index].
z,
1374 curve_radius[first_key_index]);
1379 index_data[segment_index++] = vertex_index - 1;
1381 vertex_data[vertex_index++] =
make_float4(keys[first_key_index + k].
x,
1382 keys[first_key_index + k].
y,
1383 keys[first_key_index + k].
z,
1384 curve_radius[first_key_index + k]);
1387 const int last_key_index = first_key_index + curve.
num_keys - 1;
1389 vertex_data[vertex_index++] =
make_float4(keys[last_key_index].
x,
1390 keys[last_key_index].
y,
1391 keys[last_key_index].
z,
1392 curve_radius[last_key_index]);
1393 vertex_data[vertex_index++] =
make_float4(keys[last_key_index].
x,
1394 keys[last_key_index].
y,
1395 keys[last_key_index].
z,
1396 curve_radius[last_key_index]);
1402 for (
size_t curve_index = 0,
i = 0; curve_index < hair->
num_curves(); ++curve_index) {
1409 const size_t index =
step * num_segments +
i;
1410 aabb_data[index].minX =
bounds.min.x;
1411 aabb_data[index].minY =
bounds.min.y;
1412 aabb_data[index].minZ =
bounds.min.z;
1413 aabb_data[index].maxX =
bounds.max.x;
1414 aabb_data[index].maxY =
bounds.max.y;
1415 aabb_data[index].maxZ =
bounds.max.z;
1422 aabb_data.copy_to_device();
1423 index_data.copy_to_device();
1424 vertex_data.copy_to_device();
1427 aabb_ptrs.reserve(num_motion_steps);
1430 width_ptrs.reserve(num_motion_steps);
1431 vertex_ptrs.reserve(num_motion_steps);
1432 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
1433 aabb_ptrs.push_back(aabb_data.device_pointer +
step * num_segments *
sizeof(OptixAabb));
1434 const device_ptr base_ptr = vertex_data.device_pointer +
1436 width_ptrs.push_back(base_ptr + 3 *
sizeof(
float));
1437 vertex_ptrs.push_back(base_ptr);
1441 unsigned int build_flags = OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
1442 OptixBuildInput build_input = {};
1444 build_input.type = OPTIX_BUILD_INPUT_TYPE_CURVES;
1446 build_input.curveArray.curveType = OPTIX_PRIMITIVE_TYPE_ROUND_LINEAR;
1449 build_input.curveArray.curveType = OPTIX_PRIMITIVE_TYPE_ROUND_CATMULLROM;
1451 build_input.curveArray.numPrimitives = num_segments;
1452 build_input.curveArray.vertexBuffers = (CUdeviceptr *)vertex_ptrs.data();
1453 build_input.curveArray.numVertices = num_vertices;
1454 build_input.curveArray.vertexStrideInBytes =
sizeof(
float4);
1455 build_input.curveArray.widthBuffers = (CUdeviceptr *)width_ptrs.data();
1456 build_input.curveArray.widthStrideInBytes =
sizeof(
float4);
1457 build_input.curveArray.indexBuffer = (CUdeviceptr)index_data.device_pointer;
1458 build_input.curveArray.indexStrideInBytes =
sizeof(int);
1459 build_input.curveArray.flag = build_flags;
1465 build_flags |= OPTIX_GEOMETRY_FLAG_DISABLE_ANYHIT;
1467 build_input.type = OPTIX_BUILD_INPUT_TYPE_CUSTOM_PRIMITIVES;
1468 build_input.customPrimitiveArray.aabbBuffers = (CUdeviceptr *)aabb_ptrs.data();
1469 build_input.customPrimitiveArray.numPrimitives = num_segments;
1470 build_input.customPrimitiveArray.strideInBytes =
sizeof(OptixAabb);
1471 build_input.customPrimitiveArray.flags = &build_flags;
1472 build_input.customPrimitiveArray.numSbtRecords = 1;
1476 if (!build_optix_bvh(bvh_optix, operation, build_input, num_motion_steps)) {
1477 progress.
set_error(
"Failed to build OptiX acceleration structure");
1482 Mesh *
const mesh =
static_cast<Mesh *const
>(geom);
1487 const size_t num_verts = mesh->get_verts().size();
1489 size_t num_motion_steps = 1;
1491 if (pipeline_options.usesMotionBlur && mesh->get_use_motion_blur() && motion_keys) {
1492 num_motion_steps = mesh->get_motion_steps();
1496 index_data.alloc(mesh->get_triangles().size());
1497 memcpy(index_data.data(),
1498 mesh->get_triangles().data(),
1499 mesh->get_triangles().size() *
sizeof(
int));
1501 vertex_data.alloc(num_verts * num_motion_steps);
1503 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
1506 size_t center_step = (num_motion_steps - 1) / 2;
1508 if (
step != center_step) {
1512 memcpy(vertex_data.data() + num_verts *
step,
verts, num_verts *
sizeof(
float3));
1516 index_data.copy_to_device();
1517 vertex_data.copy_to_device();
1520 vertex_ptrs.reserve(num_motion_steps);
1521 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
1522 vertex_ptrs.push_back(vertex_data.device_pointer + num_verts *
step *
sizeof(
float3));
1526 unsigned int build_flags = OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
1527 OptixBuildInput build_input = {};
1528 build_input.type = OPTIX_BUILD_INPUT_TYPE_TRIANGLES;
1529 build_input.triangleArray.vertexBuffers = (CUdeviceptr *)vertex_ptrs.data();
1530 build_input.triangleArray.numVertices = num_verts;
1531 build_input.triangleArray.vertexFormat = OPTIX_VERTEX_FORMAT_FLOAT3;
1532 build_input.triangleArray.vertexStrideInBytes =
sizeof(
float4);
1533 build_input.triangleArray.indexBuffer = index_data.device_pointer;
1534 build_input.triangleArray.numIndexTriplets = mesh->
num_triangles();
1535 build_input.triangleArray.indexFormat = OPTIX_INDICES_FORMAT_UNSIGNED_INT3;
1536 build_input.triangleArray.indexStrideInBytes = 3 *
sizeof(int);
1537 build_input.triangleArray.flags = &build_flags;
1541 build_input.triangleArray.numSbtRecords = 1;
1542 build_input.triangleArray.primitiveIndexOffset = mesh->
prim_offset;
1544 if (!build_optix_bvh(bvh_optix, operation, build_input, num_motion_steps)) {
1545 progress.
set_error(
"Failed to build OptiX acceleration structure");
1551 const size_t num_points = pointcloud->
num_points();
1552 if (num_points == 0) {
1556 size_t num_motion_steps = 1;
1558 if (pipeline_options.usesMotionBlur && pointcloud->get_use_motion_blur() && motion_points) {
1559 num_motion_steps = pointcloud->get_motion_steps();
1563 aabb_data.alloc(num_points * num_motion_steps);
1566 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
1568 size_t center_step = (num_motion_steps - 1) / 2;
1570 if (
step == center_step) {
1571 const float3 *points = pointcloud->get_points().data();
1572 const float *radius = pointcloud->get_radius().data();
1574 for (
size_t i = 0;
i < num_points; ++
i) {
1579 const size_t index =
step * num_points +
i;
1580 aabb_data[index].minX =
bounds.min.x;
1581 aabb_data[index].minY =
bounds.min.y;
1582 aabb_data[index].minZ =
bounds.min.z;
1583 aabb_data[index].maxX =
bounds.max.x;
1584 aabb_data[index].maxY =
bounds.max.y;
1585 aabb_data[index].maxZ =
bounds.max.z;
1589 size_t attr_offset = (
step > center_step) ?
step - 1 :
step;
1592 for (
size_t i = 0;
i < num_points; ++
i) {
1597 const size_t index =
step * num_points +
i;
1598 aabb_data[index].minX =
bounds.min.x;
1599 aabb_data[index].minY =
bounds.min.y;
1600 aabb_data[index].minZ =
bounds.min.z;
1601 aabb_data[index].maxX =
bounds.max.x;
1602 aabb_data[index].maxY =
bounds.max.y;
1603 aabb_data[index].maxZ =
bounds.max.z;
1609 aabb_data.copy_to_device();
1612 aabb_ptrs.reserve(num_motion_steps);
1613 for (
size_t step = 0;
step < num_motion_steps; ++
step) {
1614 aabb_ptrs.push_back(aabb_data.device_pointer +
step * num_points *
sizeof(OptixAabb));
1620 unsigned int build_flags = OPTIX_GEOMETRY_FLAG_DISABLE_ANYHIT |
1621 OPTIX_GEOMETRY_FLAG_REQUIRE_SINGLE_ANYHIT_CALL;
1622 OptixBuildInput build_input = {};
1623 build_input.type = OPTIX_BUILD_INPUT_TYPE_CUSTOM_PRIMITIVES;
1624 build_input.customPrimitiveArray.aabbBuffers = (CUdeviceptr *)aabb_ptrs.data();
1625 build_input.customPrimitiveArray.numPrimitives = num_points;
1626 build_input.customPrimitiveArray.strideInBytes =
sizeof(OptixAabb);
1627 build_input.customPrimitiveArray.flags = &build_flags;
1628 build_input.customPrimitiveArray.numSbtRecords = 1;
1629 build_input.customPrimitiveArray.primitiveIndexOffset = pointcloud->
prim_offset;
1631 if (!build_optix_bvh(bvh_optix, operation, build_input, num_motion_steps)) {
1632 progress.
set_error(
"Failed to build OptiX acceleration structure");
1637 unsigned int num_instances = 0;
1638 unsigned int max_num_instances = 0xFFFFFFFF;
1640 bvh_optix->as_data->free();
1641 bvh_optix->traversable_handle = 0;
1642 bvh_optix->motion_transform_data->free();
1644 optixDeviceContextGetProperty(context,
1645 OPTIX_DEVICE_PROPERTY_LIMIT_MAX_INSTANCE_ID,
1647 sizeof(max_num_instances));
1649 max_num_instances >>= 1;
1650 if (bvh->
objects.size() > max_num_instances) {
1652 "Failed to build OptiX acceleration structure because there are too many instances");
1658 instances.alloc(bvh->
objects.size());
1661 size_t motion_transform_offset = 0;
1662 if (pipeline_options.usesMotionBlur) {
1663 size_t total_motion_transform_size = 0;
1666 total_motion_transform_size =
align_up(total_motion_transform_size,
1667 OPTIX_TRANSFORM_BYTE_ALIGNMENT);
1668 const size_t motion_keys =
max(ob->get_motion().size(), (
size_t)2) - 2;
1669 total_motion_transform_size = total_motion_transform_size +
1670 sizeof(OptixSRTMotionTransform) +
1671 motion_keys *
sizeof(OptixSRTData);
1675 assert(bvh_optix->motion_transform_data->device ==
this);
1676 bvh_optix->motion_transform_data->alloc_to_device(total_motion_transform_size);
1685 BVHOptiX *
const blas =
static_cast<BVHOptiX *
>(ob->get_geometry()->bvh.get());
1686 OptixTraversableHandle handle = blas->traversable_handle;
1691 OptixInstance &instance = instances[num_instances++];
1692 memset(&instance, 0,
sizeof(instance));
1695 instance.transform[0] = 1.0f;
1696 instance.transform[5] = 1.0f;
1697 instance.transform[10] = 1.0f;
1710 if (0 == instance.visibilityMask) {
1711 instance.visibilityMask = 0xFF;
1714 if (ob->get_geometry()->is_hair()) {
1715 const Hair *hair =
static_cast<const Hair *
>(ob->get_geometry());
1717 instance.sbtOffset = PG_HITD_CURVE_RIBBON - PG_HITD;
1720 instance.visibilityMask |= 4;
1723 instance.sbtOffset = PG_HITD_CURVE_LINEAR - PG_HITD;
1725 instance.sbtOffset = PG_HITD_CURVE_LINEAR_MOTION - PG_HITD;
1731 instance.sbtOffset = PG_HITD_MOTION - PG_HITD;
1735 else if (ob->get_geometry()->is_pointcloud()) {
1737 instance.sbtOffset = PG_HITD_POINTCLOUD - PG_HITD;
1740 instance.visibilityMask |= 4;
1749 instance.flags = OPTIX_INSTANCE_FLAG_DISABLE_ANYHIT;
1753 if (pipeline_options.usesMotionBlur && ob->
use_motion()) {
1754 size_t motion_keys =
max(ob->get_motion().size(), (
size_t)2) - 2;
1755 size_t motion_transform_size =
sizeof(OptixSRTMotionTransform) +
1756 motion_keys *
sizeof(OptixSRTData);
1758 const CUDAContextScope scope(
this);
1760 motion_transform_offset =
align_up(motion_transform_offset,
1761 OPTIX_TRANSFORM_BYTE_ALIGNMENT);
1762 CUdeviceptr motion_transform_gpu = bvh_optix->motion_transform_data->device_pointer +
1763 motion_transform_offset;
1764 motion_transform_offset += motion_transform_size;
1768 OptixSRTMotionTransform *motion_transform =
reinterpret_cast<OptixSRTMotionTransform *
>(
1769 motion_transform_storage.data());
1770 motion_transform->child = handle;
1771 motion_transform->motionOptions.numKeys = ob->get_motion().size();
1772 motion_transform->motionOptions.flags = OPTIX_MOTION_FLAG_NONE;
1773 motion_transform->motionOptions.timeBegin = 0.0f;
1774 motion_transform->motionOptions.timeEnd = 1.0f;
1776 OptixSRTData *
const srt_data = motion_transform->srtData;
1779 decomp.data(), ob->get_motion().
data(), ob->get_motion().size());
1781 for (
size_t i = 0;
i < ob->get_motion().
size(); ++
i) {
1783 srt_data[
i].sx = decomp[
i].y.w;
1784 srt_data[
i].sy = decomp[
i].z.w;
1785 srt_data[
i].sz = decomp[
i].w.w;
1788 srt_data[
i].a = decomp[
i].z.x;
1789 srt_data[
i].b = decomp[
i].z.y;
1790 srt_data[
i].c = decomp[
i].w.x;
1796 srt_data[
i].pvx = 0.0f;
1797 srt_data[
i].pvy = 0.0f;
1798 srt_data[
i].pvz = 0.0f;
1801 srt_data[
i].qx = decomp[
i].x.x;
1802 srt_data[
i].qy = decomp[
i].x.y;
1803 srt_data[
i].qz = decomp[
i].x.z;
1804 srt_data[
i].qw = decomp[
i].x.w;
1807 srt_data[
i].tx = decomp[
i].y.x;
1808 srt_data[
i].ty = decomp[
i].y.y;
1809 srt_data[
i].tz = decomp[
i].y.z;
1813 cuMemcpyHtoD(motion_transform_gpu, motion_transform, motion_transform_size);
1814 motion_transform =
nullptr;
1815 motion_transform_storage.clear();
1818 optixConvertPointerToTraversableHandle(context,
1819 motion_transform_gpu,
1820 OPTIX_TRAVERSABLE_TYPE_SRT_MOTION_TRANSFORM,
1821 &instance.traversableHandle);
1824 instance.traversableHandle = handle;
1826 if (ob->get_geometry()->is_instanced()) {
1828 memcpy(instance.transform, &ob->get_tfm(),
sizeof(instance.transform));
1834 instances.resize(num_instances);
1835 instances.copy_to_device();
1838 OptixBuildInput build_input = {};
1839 build_input.type = OPTIX_BUILD_INPUT_TYPE_INSTANCES;
1840 build_input.instanceArray.instances = instances.device_pointer;
1841 build_input.instanceArray.numInstances = num_instances;
1843 if (!build_optix_bvh(bvh_optix, OPTIX_BUILD_OPERATION_BUILD, build_input, 0)) {
1844 progress.
set_error(
"Failed to build OptiX acceleration structure");
1846 tlas_handle = bvh_optix->traversable_handle;
1850void OptiXDevice::release_bvh(
BVH *bvh)
1855 BVHOptiX *
const bvh_optix =
static_cast<BVHOptiX *
>(bvh);
1857 delayed_free_bvh_memory.emplace_back(std::move(bvh_optix->as_data));
1858 delayed_free_bvh_memory.emplace_back(std::move(bvh_optix->motion_transform_data));
1859 bvh_optix->traversable_handle = 0;
1862void OptiXDevice::free_bvh_memory_delayed()
1865 delayed_free_bvh_memory.free_memory();
1868void OptiXDevice::const_copy_to(
const char *
name,
void *host,
const size_t size)
1871 CUDADevice::const_copy_to(
name, host,
size);
1873 if (strcmp(
name,
"data") == 0) {
1877 KernelData *
const data = (KernelData *)host;
1878 *(OptixTraversableHandle *)&
data->device_bvh = tlas_handle;
1885# define KERNEL_DATA_ARRAY(data_type, data_name) \
1886 if (strcmp(name, #data_name) == 0) { \
1887 update_launch_params(offsetof(KernelParamsOptiX, data_name), host, size); \
1891# include "kernel/data_arrays.h"
1892# undef KERNEL_DATA_ARRAY
1895void OptiXDevice::update_launch_params(
const size_t offset,
void *
data,
const size_t data_size)
1897 const CUDAContextScope scope(
this);
1899 cuda_assert(cuMemcpyHtoD(launch_params.device_pointer + offset,
data, data_size));
BMesh const char void * data
unsigned long long int uint64_t
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
static btDbvtVolume bounds(btDbvtNode **leaves, int count)
void refit(btStridingMeshInterface *triangles, const btVector3 &aabbMin, const btVector3 &aabbMax)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Attribute * find(ustring name) const
vector< Geometry * > geometry
vector< Object * > objects
bool is_pointcloud() const
virtual bool has_motion_blur() const
Curve get_curve(const size_t i) const
size_t curve_segment_offset
size_t num_curves() const
size_t num_segments() const
CurveShapeType curve_shape
void set_substatus(const string &substatus_)
void set_error(const string &error_message_)
device_ptr device_pointer
void alloc_to_device(const size_t num, bool shrink_to_fit=true)
CCL_NAMESPACE_BEGIN struct Options options
#define KERNEL_DATA_ARRAY(type, name)
DebugFlags & DebugFlags()
#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 assert(assertion)
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
@ ATTR_STD_MOTION_VERTEX_POSITION
Segment< FEdge *, Vec3r > segment
size_t path_file_size(const string &path)
bool path_is_directory(const string &path)
string path_get(const string &sub)
string path_join(const string &dir, const string &file)
bool path_read_compressed_text(const string &path, string &text)
static struct PyModuleDef module
CCL_NAMESPACE_BEGIN string string_printf(const char *format,...)
void bounds_grow(const int k, const float3 *curve_keys, const float *curve_radius, BoundBox &bounds) const
size_t num_triangles() const
int get_device_index() const
bool is_traceable() const
uint visibility_for_tracing() const
void bounds_grow(const float3 *points, const float *radius, BoundBox &bounds) const
Point get_point(const int i) const
size_t num_points() const
void push(TaskRunFunction &&task)
void wait_work(Summary *stats=nullptr)
std::unique_lock< std::mutex > thread_scoped_lock
ccl_device_inline size_t align_up(const size_t offset, const size_t alignment)