Blender V5.3
optix/queue.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_OPTIX
6
7# include "device/optix/queue.h"
9
10# define __KERNEL_OPTIX__
12
14
15/* CUDADeviceQueue */
16
17OptiXDeviceQueue::OptiXDeviceQueue(OptiXDevice *device) : CUDADeviceQueue(device) {}
18
19void OptiXDeviceQueue::init_execution()
20{
21 CUDADeviceQueue::init_execution();
22}
23
24static bool is_optix_specific_kernel(DeviceKernel kernel, bool osl_shading, bool osl_camera)
25{
26# ifdef WITH_OSL
27 /* OSL uses direct callables to execute, so shading needs to be done in OptiX if OSL is used. */
28 if (osl_shading && device_kernel_has_shading(kernel)) {
29 return true;
30 }
31 if (osl_camera && kernel == DEVICE_KERNEL_INTEGRATOR_INIT_FROM_CAMERA) {
32 return true;
33 }
34# else
35 (void)osl_shading;
36 (void)osl_camera;
37# endif
38
39 return device_kernel_has_intersection(kernel);
40}
41
42bool OptiXDeviceQueue::enqueue(DeviceKernel kernel,
43 const int work_size,
44 const DeviceKernelArguments &args)
45{
46 OptiXDevice *const optix_device = static_cast<OptiXDevice *>(cuda_device_);
47
48# ifdef WITH_OSL
49 const OSLGlobals *og = static_cast<const OSLGlobals *>(optix_device->get_cpu_osl_memory());
50 const bool osl_shading = og->use_shading;
51 const bool osl_camera = og->use_camera;
52# else
53 const bool osl_shading = false;
54 const bool osl_camera = false;
55# endif
56
57 if (!is_optix_specific_kernel(kernel, osl_shading, osl_camera)) {
58 return CUDADeviceQueue::enqueue(kernel, work_size, args);
59 }
60
61 if (cuda_device_->have_error()) {
62 return false;
63 }
64
65 debug_enqueue_begin(kernel, work_size);
66
67 const CUDAContextScope scope(cuda_device_);
68
69 const device_ptr sbt_data_ptr = optix_device->sbt_data.device_pointer;
70 const device_ptr launch_params_ptr = optix_device->launch_params.device_pointer;
71
72 auto set_launch_param = [&](size_t offset, size_t size, int arg) {
73 cuda_device_assert(
74 cuda_device_,
75 cuMemcpyHtoDAsync(launch_params_ptr + offset, args.values[arg], size, cuda_stream_));
76 };
77
78 set_launch_param(offsetof(KernelParamsOptiX, path_index_array), sizeof(device_ptr), 0);
79
81 set_launch_param(offsetof(KernelParamsOptiX, render_buffer), sizeof(device_ptr), 1);
82 }
87 {
88 set_launch_param(offsetof(KernelParamsOptiX, shader_eval_cache_miss), sizeof(device_ptr), 2);
89 set_launch_param(offsetof(KernelParamsOptiX, shader_eval_offset), sizeof(int32_t), 3);
90 }
91
93 set_launch_param(offsetof(KernelParamsOptiX, num_tiles), sizeof(int32_t), 1);
94 set_launch_param(offsetof(KernelParamsOptiX, render_buffer), sizeof(device_ptr), 2);
95 set_launch_param(offsetof(KernelParamsOptiX, max_tile_work_size), sizeof(int32_t), 3);
96 }
97
98 cuda_device_assert(cuda_device_, cuStreamSynchronize(cuda_stream_));
99
100 OptixPipeline pipeline = nullptr;
101 OptixShaderBindingTable sbt_params = {};
102
103 switch (kernel) {
105 pipeline = optix_device->pipelines[PIP_SHADE];
106 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_BACKGROUND * sizeof(SbtRecord);
107 break;
109 pipeline = optix_device->pipelines[PIP_SHADE];
110 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_LIGHT_NEE * sizeof(SbtRecord);
111 break;
113 pipeline = optix_device->pipelines[PIP_SHADE];
114 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_LIGHT_FORWARD * sizeof(SbtRecord);
115 break;
117 pipeline = optix_device->pipelines[PIP_SHADE];
118 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_SURFACE * sizeof(SbtRecord);
119 break;
121 pipeline = optix_device->pipelines[PIP_SHADE];
122 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_SURFACE_RAYTRACE * sizeof(SbtRecord);
123 break;
125 pipeline = optix_device->pipelines[PIP_SHADE];
126 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_INTERSECT_MNEE * sizeof(SbtRecord);
127 break;
129 pipeline = optix_device->pipelines[PIP_SHADE];
130 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_VOLUME * sizeof(SbtRecord);
131 break;
133 pipeline = optix_device->pipelines[PIP_SHADE];
134 sbt_params.raygenRecord = sbt_data_ptr +
135 PG_RGEN_SHADE_VOLUME_RAY_MARCHING * sizeof(SbtRecord);
136 break;
138 pipeline = optix_device->pipelines[PIP_SHADE];
139 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_SHADOW * sizeof(SbtRecord);
140 break;
142 pipeline = optix_device->pipelines[PIP_SHADE];
143 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_SHADE_DEDICATED_LIGHT * sizeof(SbtRecord);
144 break;
145
147 pipeline = optix_device->pipelines[PIP_INTERSECT];
148 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_INTERSECT_CLOSEST * sizeof(SbtRecord);
149 break;
151 pipeline = optix_device->pipelines[PIP_INTERSECT];
152 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_INTERSECT_SHADOW * sizeof(SbtRecord);
153 break;
155 pipeline = optix_device->pipelines[PIP_INTERSECT];
156 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_INTERSECT_SUBSURFACE * sizeof(SbtRecord);
157 break;
159 pipeline = optix_device->pipelines[PIP_INTERSECT];
160 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_INTERSECT_VOLUME_STACK * sizeof(SbtRecord);
161 break;
163 pipeline = optix_device->pipelines[PIP_INTERSECT];
164 sbt_params.raygenRecord = sbt_data_ptr +
165 PG_RGEN_INTERSECT_DEDICATED_LIGHT * sizeof(SbtRecord);
166 break;
167
169 pipeline = optix_device->pipelines[PIP_SHADE];
170 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_EVAL_DISPLACE * sizeof(SbtRecord);
171 break;
173 pipeline = optix_device->pipelines[PIP_SHADE];
174 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_EVAL_BACKGROUND * sizeof(SbtRecord);
175 break;
177 pipeline = optix_device->pipelines[PIP_SHADE];
178 sbt_params.raygenRecord = sbt_data_ptr +
179 PG_RGEN_EVAL_CURVE_SHADOW_TRANSPARENCY * sizeof(SbtRecord);
180 break;
182 pipeline = optix_device->pipelines[PIP_SHADE];
183 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_EVAL_VOLUME_DENSITY * sizeof(SbtRecord);
184 break;
185
187 pipeline = optix_device->pipelines[PIP_SHADE];
188 sbt_params.raygenRecord = sbt_data_ptr + PG_RGEN_INIT_FROM_CAMERA * sizeof(SbtRecord);
189 break;
190
191 default:
192 LOG_ERROR << "Invalid kernel " << device_kernel_as_string(kernel)
193 << " is attempted to be enqueued.";
194 return false;
195 }
196
197 sbt_params.missRecordBase = sbt_data_ptr + MISS_PROGRAM_GROUP_OFFSET * sizeof(SbtRecord);
198 sbt_params.missRecordStrideInBytes = sizeof(SbtRecord);
199 sbt_params.missRecordCount = NUM_MISS_PROGRAM_GROUPS;
200 sbt_params.hitgroupRecordBase = sbt_data_ptr + HIT_PROGAM_GROUP_OFFSET * sizeof(SbtRecord);
201 sbt_params.hitgroupRecordStrideInBytes = sizeof(SbtRecord);
202 sbt_params.hitgroupRecordCount = NUM_HIT_PROGRAM_GROUPS;
203 sbt_params.callablesRecordBase = sbt_data_ptr + CALLABLE_PROGRAM_GROUPS_BASE * sizeof(SbtRecord);
204 sbt_params.callablesRecordCount = NUM_CALLABLE_PROGRAM_GROUPS;
205 sbt_params.callablesRecordStrideInBytes = sizeof(SbtRecord);
206
207# ifdef WITH_OSL
208 if (osl_shading || osl_camera) {
209 sbt_params.callablesRecordCount += static_cast<unsigned int>(optix_device->osl_groups.size());
210 }
211# endif
212
213 /* Launch the ray generation program. */
214 optix_device_assert(optix_device,
215 optixLaunch(pipeline,
216 cuda_stream_,
217 launch_params_ptr,
218 optix_device->launch_params.data_elements,
219 &sbt_params,
220 work_size,
221 1,
222 1));
223
224 debug_enqueue_end();
225
226 return !(optix_device->have_error());
227}
228
230
231#endif /* WITH_OPTIX */
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN bool device_kernel_has_shading(DeviceKernel kernel)
bool device_kernel_has_intersection(DeviceKernel kernel)
const char * device_kernel_as_string(DeviceKernel kernel)
#define offsetof(t, d)
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int num_tiles
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float const int max_tile_work_size
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float const int const ccl_global int * path_index_array
ccl_gpu_kernel_postfix ccl_global KernelWorkTile const int ccl_global float * render_buffer
DeviceKernel
@ DEVICE_KERNEL_INTEGRATOR_SHADE_DEDICATED_LIGHT
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE
@ DEVICE_KERNEL_SHADER_EVAL_DISPLACE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_MNEE
@ DEVICE_KERNEL_SHADER_EVAL_VOLUME_DENSITY
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SUBSURFACE
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_VOLUME_STACK
@ DEVICE_KERNEL_SHADER_EVAL_BACKGROUND
@ DEVICE_KERNEL_INTEGRATOR_SHADE_SURFACE_RAYTRACE
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_DEDICATED_LIGHT
@ DEVICE_KERNEL_INTEGRATOR_INIT_FROM_CAMERA
@ DEVICE_KERNEL_INTEGRATOR_SHADE_LIGHT_FORWARD
@ DEVICE_KERNEL_INTEGRATOR_SHADE_VOLUME_RAY_MARCHING
@ DEVICE_KERNEL_INTEGRATOR_SHADE_LIGHT_NEE
@ DEVICE_KERNEL_SHADER_EVAL_CURVE_SHADOW_TRANSPARENCY
@ DEVICE_KERNEL_INTEGRATOR_SHADE_VOLUME
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_SHADOW
@ DEVICE_KERNEL_INTEGRATOR_INTERSECT_CLOSEST
@ DEVICE_KERNEL_INTEGRATOR_SHADE_BACKGROUND
#define LOG_ERROR
Definition log.h:101
PipelineGraphic pipeline(vert_main, frag_main)
void * values[MAX_ARGS]
uint64_t device_ptr
Definition types_base.h:44