Blender V5.3
kernel/bvh/bvh.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
8#include "kernel/bvh/nodes.h"
9#include "kernel/bvh/types.h"
10#include "kernel/bvh/util.h"
11
15#include "kernel/geom/object.h"
18
19/* Device specific acceleration structures for ray tracing. */
20
21#if defined(__EMBREE__)
23# define __BVH2__
24#elif defined(__KERNEL_METALRT__)
26#elif defined(__KERNEL_OPTIX__)
28#elif defined(__KERNEL_HIPRT__)
30#else
31# define __BVH2__
32#endif
33
34#if defined(__KERNEL_ONEAPI__) && defined(WITH_EMBREE_GPU)
35/* bool is apparently not tested for specialization constants:
36 * https://github.com/intel/llvm/blob/39d1c65272a786b2b13a6f094facfddf9408406d/sycl/test/basic_tests/SYCL-2020-spec-constants.cpp#L25-L27
37 * Instead of adding one more bool specialization constant, we reuse existing embree_features one
38 * and use RTC_FEATURE_FLAG_NONE as value to test for avoiding to call Embree on GPU.
39 */
40/* We set it to RTC_FEATURE_FLAG_NONE by default so AoT binaries contain MNE and ray-trace kernels
41 * pre-compiled without Embree.
42 * Changing this default value would require updating the logic in oneapi_load_kernels(). */
43static constexpr sycl::specialization_id<RTCFeatureFlags> oneapi_embree_features{
44 RTC_FEATURE_FLAG_NONE};
45# define IF_USING_EMBREE \
46 if (kernel_handler.get_specialization_constant<oneapi_embree_features>() != \
47 RTC_FEATURE_FLAG_NONE)
48# define IF_NOT_USING_EMBREE \
49 if (kernel_handler.get_specialization_constant<oneapi_embree_features>() == \
50 RTC_FEATURE_FLAG_NONE)
51#else
52# define IF_USING_EMBREE
53# define IF_NOT_USING_EMBREE
54#endif
55
57
58/* --------------------------------------------------------------------
59 * Transparent shadow BVH traversal, recording multiple intersections.
60 */
61
62#ifdef __TRANSPARENT_SHADOWS__
63
64# if defined(__BVH2__)
65# define BVH_FUNCTION_NAME bvh_intersect_shadow_all
66# define BVH_FUNCTION_FEATURES BVH_POINTCLOUD
68
69# if defined(__HAIR__)
70# define BVH_FUNCTION_NAME bvh_intersect_shadow_all_hair
71# define BVH_FUNCTION_FEATURES BVH_HAIR | BVH_POINTCLOUD
73# endif
74
75# if defined(__OBJECT_MOTION__)
76# define BVH_FUNCTION_NAME bvh_intersect_shadow_all_motion
77# define BVH_FUNCTION_FEATURES BVH_MOTION | BVH_POINTCLOUD
79# endif
80
81# if defined(__HAIR__) && defined(__OBJECT_MOTION__)
82# define BVH_FUNCTION_NAME bvh_intersect_shadow_all_hair_motion
83# define BVH_FUNCTION_FEATURES BVH_HAIR | BVH_MOTION | BVH_POINTCLOUD
85# endif
86
87ccl_device_inline void scene_intersect_shadow_all_bvh2(
89 const ccl_private Ray *ccl_restrict ray,
90 ccl_private BVHShadowAllPayload &ccl_restrict payload)
91{
92# ifdef __OBJECT_MOTION__
93 if (kernel_data.bvh.have_motion) {
94# ifdef __HAIR__
95 if (kernel_data.bvh.have_curves) {
96 bvh_intersect_shadow_all_hair_motion(kg, ray, payload);
97 return;
98 }
99# endif /* __HAIR__ */
100 bvh_intersect_shadow_all_motion(kg, ray, payload);
101 return;
102 }
103# endif /* __OBJECT_MOTION__ */
104
105# ifdef __HAIR__
106 if (kernel_data.bvh.have_curves) {
107 bvh_intersect_shadow_all_hair(kg, ray, payload);
108 return;
109 }
110# endif /* __HAIR__ */
111 bvh_intersect_shadow_all(kg, ray, payload);
112}
113# endif /* __BVH2__ */
114
115ccl_device_intersect void scene_intersect_shadow_all(KernelGlobals kg,
117 const ccl_private Ray *ray,
118 const uint visibility,
119 const uint max_transparent_hits,
120 ccl_private uint *num_recorded_hits,
121 ccl_private float3 *throughput)
122{
123# if !defined(__KERNEL_OPTIX__)
124 /* OptiX does not perform well with conditional trace calls, so it handles the validity of the
125 * ray in the scene_intersect_shadow_all_optix(). */
126 if (!intersection_ray_valid(ray)) {
127 *num_recorded_hits = 0;
128 *throughput = one_float3();
129 return;
130 }
131# endif
132
133 BVHShadowAllPayload payload;
134
135 /* A bit of a tricky initialization:
136 * - Some backends require extra ray information for custom motion blur intersection.
137 * - Some backends utilize registers to pass commonly accessed data to the trace calls. */
138# if !defined(__KERNEL_OPTIX__)
139 BVH_PAYLOAD_BASE(payload).ray_self = ray->self;
140 BVH_PAYLOAD_BASE(payload).ray_visibility = visibility;
141# if defined(__KERNEL_HIPRT__)
142 BVH_PAYLOAD_BASE(payload).ray_time = ray->time;
143# endif
144# endif
145
146 payload.state = state;
147 payload.max_transparent_hits = max_transparent_hits;
148 payload.max_record_isect_t = ray->tmax;
149
150# ifdef __EMBREE__
152 {
153 if (kernel_data.device_bvh) {
154 kernel_embree_intersect_shadow_all(kg, ray, payload);
155 *num_recorded_hits = payload.num_recorded_hits;
156 *throughput = payload.throughput;
157 return;
158 }
159 }
160# endif
161
163 {
164# if defined(__BVH2__)
165 scene_intersect_shadow_all_bvh2(kg, ray, payload);
166# elif defined(__KERNEL_HIPRT__)
167 scene_intersect_shadow_all_hiprt(kg, ray, payload);
168# elif defined(__KERNEL_METALRT__)
169 scene_intersect_shadow_all_metalrt(ray, payload);
170# elif defined(__KERNEL_OPTIX__)
171 scene_intersect_shadow_all_optix(ray, visibility, payload);
172# endif
173
174 *num_recorded_hits = payload.num_recorded_hits;
175 *throughput = payload.throughput;
176
177 return;
178 }
179
180 kernel_assert(false);
181}
182#endif /* __TRANSPARENT_SHADOWS__ */
183
184// ------------------------------------------------------------------------------------------------
185
186#ifdef __BVH2__
187
188/* BVH2
189 *
190 * Bounding volume hierarchy for ray tracing, when no native acceleration
191 * structure is available for the device.
192 *
193 * We compile different variations of the same BVH traversal function for
194 * faster rendering when some types of primitives are not needed, using #includes
195 * to work around the lack of C++ templates in OpenCL.
196 *
197 * Originally based on "Understanding the Efficiency of Ray Traversal on GPUs",
198 * the code has been extended and modified to support more primitives and work
199 * with CPU and various GPU kernel languages. */
200
201/* Regular BVH traversal */
202
203# define BVH_FUNCTION_NAME bvh_intersect
204# define BVH_FUNCTION_FEATURES BVH_POINTCLOUD
205# include "kernel/bvh/traversal.h"
206
207# if defined(__HAIR__)
208# define BVH_FUNCTION_NAME bvh_intersect_hair
209# define BVH_FUNCTION_FEATURES BVH_HAIR | BVH_POINTCLOUD
210# include "kernel/bvh/traversal.h"
211# endif
212
213# if defined(__OBJECT_MOTION__)
214# define BVH_FUNCTION_NAME bvh_intersect_motion
215# define BVH_FUNCTION_FEATURES BVH_MOTION | BVH_POINTCLOUD
216# include "kernel/bvh/traversal.h"
217# endif
218
219# if defined(__HAIR__) && defined(__OBJECT_MOTION__)
220# define BVH_FUNCTION_NAME bvh_intersect_hair_motion
221# define BVH_FUNCTION_FEATURES BVH_HAIR | BVH_MOTION | BVH_POINTCLOUD
222# include "kernel/bvh/traversal.h"
223# endif
224
226 const ccl_private Ray *ray,
227 const uint visibility,
229{
230 if (!intersection_ray_valid(ray)) {
231 return false;
232 }
233
234# ifdef __EMBREE__
236 {
237 if (kernel_data.device_bvh) {
238 return kernel_embree_intersect(kg, ray, visibility, isect);
239 }
240 }
241# endif
242
244 {
245# ifdef __OBJECT_MOTION__
246 if (kernel_data.bvh.have_motion) {
247# ifdef __HAIR__
248 if (kernel_data.bvh.have_curves) {
249 return bvh_intersect_hair_motion(kg, ray, isect, visibility);
250 }
251# endif /* __HAIR__ */
252
253 return bvh_intersect_motion(kg, ray, isect, visibility);
254 }
255# endif /* __OBJECT_MOTION__ */
256
257# ifdef __HAIR__
258 if (kernel_data.bvh.have_curves) {
259 return bvh_intersect_hair(kg, ray, isect, visibility);
260 }
261# endif /* __HAIR__ */
262
263 return bvh_intersect(kg, ray, isect, visibility);
264 }
265
266 kernel_assert(false);
267 return false;
268}
269
271 const ccl_private Ray *ray,
272 const uint visibility)
273{
274 Intersection isect;
275 return scene_intersect(kg, ray, visibility, &isect);
276}
277
278/* Single object BVH traversal, for SSS/AO/bevel. */
279
280# ifdef __BVH_LOCAL__
281
282# define BVH_FUNCTION_NAME bvh_intersect_local
283# define BVH_FUNCTION_FEATURES BVH_HAIR
284# include "kernel/bvh/local.h"
285
286# if defined(__OBJECT_MOTION__)
287# define BVH_FUNCTION_NAME bvh_intersect_local_motion
288# define BVH_FUNCTION_FEATURES BVH_MOTION | BVH_HAIR
289# include "kernel/bvh/local.h"
290# endif
291
292template<bool single_hit = false>
293ccl_device_intersect bool scene_intersect_local(KernelGlobals kg,
294 const ccl_private Ray *ray,
295 ccl_private LocalIntersection *local_isect,
296 const int local_object,
297 ccl_private uint *lcg_state,
298 const int max_hits)
299{
300 if (!intersection_ray_valid(ray)) {
301 if (local_isect) {
302 local_isect->num_hits = 0;
303 }
304 return false;
305 }
306
307# ifdef __EMBREE__
309 {
310 if (kernel_data.device_bvh) {
311 return kernel_embree_intersect_local(
312 kg, ray, local_isect, local_object, lcg_state, max_hits);
313 }
314 }
315# endif
316
318 {
319# ifdef __OBJECT_MOTION__
320 if (kernel_data.bvh.have_motion) {
321 return bvh_intersect_local_motion(kg, ray, local_isect, local_object, lcg_state, max_hits);
322 }
323# endif /* __OBJECT_MOTION__ */
324 return bvh_intersect_local(kg, ray, local_isect, local_object, lcg_state, max_hits);
325 }
326
327 kernel_assert(false);
328 return false;
329}
330# endif
331
332/* Volume BVH traversal, for initializing or updating the volume stack. */
333
334# if defined(__VOLUME__) && !defined(__VOLUME_RECORD_ALL__)
335
336# define BVH_FUNCTION_NAME bvh_intersect_volume
337# define BVH_FUNCTION_FEATURES BVH_HAIR
338# include "kernel/bvh/volume.h"
339
340# if defined(__OBJECT_MOTION__)
341# define BVH_FUNCTION_NAME bvh_intersect_volume_motion
342# define BVH_FUNCTION_FEATURES BVH_MOTION | BVH_HAIR
343# include "kernel/bvh/volume.h"
344# endif
345
346ccl_device_intersect bool scene_intersect_volume(KernelGlobals kg,
347 const ccl_private Ray *ray,
349 const uint visibility)
350{
351 if (!intersection_ray_valid(ray)) {
352 return false;
353 }
354
355# ifdef __EMBREE__
357 {
358 if (kernel_data.device_bvh) {
359 return kernel_embree_intersect_volume(kg, ray, isect, visibility);
360 }
361 }
362# endif
363
365 {
366# ifdef __OBJECT_MOTION__
367 if (kernel_data.bvh.have_motion) {
368 return bvh_intersect_volume_motion(kg, ray, isect, visibility);
369 }
370# endif /* __OBJECT_MOTION__ */
371
372 return bvh_intersect_volume(kg, ray, isect, visibility);
373 }
374
375 kernel_assert(false);
376 return false;
377}
378# endif /* defined(__VOLUME__) && !defined(__VOLUME_RECORD_ALL__) */
379
380/* Volume BVH traversal, for initializing or updating the volume stack.
381 * Variation that records multiple intersections at once. */
382
383# if defined(__VOLUME__) && defined(__VOLUME_RECORD_ALL__)
384
385# define BVH_FUNCTION_NAME bvh_intersect_volume_all
386# define BVH_FUNCTION_FEATURES BVH_HAIR
387# include "kernel/bvh/volume_all.h"
388
389# if defined(__OBJECT_MOTION__)
390# define BVH_FUNCTION_NAME bvh_intersect_volume_all_motion
391# define BVH_FUNCTION_FEATURES BVH_MOTION | BVH_HAIR
392# include "kernel/bvh/volume_all.h"
393# endif
394
395ccl_device_intersect uint scene_intersect_volume(KernelGlobals kg,
396 const ccl_private Ray *ray,
398 const uint max_hits,
399 const uint visibility)
400{
401 if (!intersection_ray_valid(ray)) {
402 return false;
403 }
404
405# ifdef __EMBREE__
407 {
408 if (kernel_data.device_bvh) {
409 return kernel_embree_intersect_volume(kg, ray, isect, max_hits, visibility);
410 }
411 }
412# endif
413
415 {
416# ifdef __OBJECT_MOTION__
417 if (kernel_data.bvh.have_motion) {
418 return bvh_intersect_volume_all_motion(kg, ray, isect, max_hits, visibility);
419 }
420# endif /* __OBJECT_MOTION__ */
421
422 return bvh_intersect_volume_all(kg, ray, isect, max_hits, visibility);
423 }
424
425 kernel_assert(false);
426 return false;
427}
428
429# endif /* defined(__VOLUME__) && defined(__VOLUME_RECORD_ALL__) */
430
431# undef BVH_FEATURE
432# undef BVH_NAME_JOIN
433# undef BVH_NAME_EVAL
434# undef BVH_FUNCTION_FULL_NAME
435
436#endif /* __BVH2__ */
437
CCL_NAMESPACE_BEGIN ccl_device_inline bool intersection_ray_valid(const ccl_private Ray *ray)
#define kernel_assert(cond)
#define kernel_data
#define ccl_restrict
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
#define BVH_PAYLOAD_BASE(obj)
#define IF_NOT_USING_EMBREE
ccl_device_intersect bool scene_intersect(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility, ccl_private Intersection *isect)
ccl_device_intersect bool scene_intersect_shadow(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility)
#define IF_USING_EMBREE
#define ccl_device_intersect
ccl_device_intersect bool kernel_embree_intersect(KernelGlobals kg, const ccl_private Ray *ray, const uint visibility, ccl_private Intersection *isect)
const int state
ccl_device_inline float3 one_float3()
Definition math_float3.h:26
unsigned int uint
Definition BKE_text.h:55
void visibility(Visibility &srt, const ViewCulling &culling, const uint3 global_id, const uint3 local_id, const uint3 workgroup_id)
IntegratorShadowStateCPU * IntegratorShadowState
Definition state.h:229