Blender V5.3
traversal.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2009-2010 NVIDIA Corporation
2 * SPDX-FileCopyrightText: 2009-2012 Intel Corporation
3 * SPDX-FileCopyrightText: 2011-2022 Blender Foundation
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Adapted code from NVIDIA Corporation. */
8
9#if BVH_FEATURE(BVH_HAIR)
10# define NODE_INTERSECT bvh_node_intersect
11#else
12# define NODE_INTERSECT bvh_aligned_node_intersect
13#endif
14
15/* This is a template BVH traversal function, where various features can be
16 * enabled/disabled. This way we can compile optimized versions for each case
17 * without new features slowing things down.
18 *
19 * BVH_HAIR: hair curve rendering
20 * BVH_POINTCLOUD: point cloud rendering
21 * BVH_MOTION: motion blur rendering
22 */
23
25 const ccl_private Ray *ray,
27 const uint visibility)
28{
29 /* todo:
30 * - test if pushing distance on the stack helps (for non shadow rays)
31 * - separate version for shadow rays
32 * - likely and unlikely for if() statements
33 * - test restrict attribute for pointers
34 */
35
36 /* traversal stack in CUDA thread-local memory */
37 int traversal_stack[BVH_STACK_SIZE];
38 traversal_stack[0] = ENTRYPOINT_SENTINEL;
39
40 /* traversal variables in registers */
41 int stack_ptr = 0;
42 int node_addr = kernel_data.bvh.root;
43
44 /* ray parameters in registers */
45 float3 P = ray->P;
46 float3 dir = bvh_clamp_direction(ray->D);
47 float3 idir = bvh_inverse_direction(dir);
48 const float tmin = ray->tmin;
49 int object = OBJECT_NONE;
50
51 isect->t = ray->tmax;
52 isect->u = 0.0f;
53 isect->v = 0.0f;
54 isect->prim = PRIM_NONE;
55 isect->object = OBJECT_NONE;
56
57 /* traversal loop */
58 do {
59 do {
60 /* traverse internal nodes */
61 while (node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
62 int node_addr_child1, traverse_mask;
63 float dist[2];
64 float4 cnodes = kernel_data_fetch(bvh_nodes, node_addr + 0);
65
66 {
67 traverse_mask = NODE_INTERSECT(kg,
68 P,
70 dir,
71#endif
72 idir,
73 tmin,
74 isect->t,
75 node_addr,
76 visibility,
77 dist);
78 }
79
80 node_addr = __float_as_int(cnodes.z);
81 node_addr_child1 = __float_as_int(cnodes.w);
82
83 if (traverse_mask == 3) {
84 /* Both children were intersected, push the farther one. */
85 bool is_closest_child1 = (dist[1] < dist[0]);
86 if (is_closest_child1) {
87 int tmp = node_addr;
88 node_addr = node_addr_child1;
89 node_addr_child1 = tmp;
90 }
91
92 ++stack_ptr;
93 kernel_assert(stack_ptr < BVH_STACK_SIZE);
94 traversal_stack[stack_ptr] = node_addr_child1;
95 }
96 else {
97 /* One child was intersected. */
98 if (traverse_mask == 2) {
99 node_addr = node_addr_child1;
100 }
101 else if (traverse_mask == 0) {
102 /* Neither child was intersected. */
103 node_addr = traversal_stack[stack_ptr];
104 --stack_ptr;
105 }
106 }
107 }
108
109 /* if node is leaf, fetch triangle list */
110 if (node_addr < 0) {
111 float4 leaf = kernel_data_fetch(bvh_leaf_nodes, (-node_addr - 1));
112 int prim_addr = __float_as_int(leaf.x);
113
114 if (prim_addr >= 0) {
115 const int prim_addr2 = __float_as_int(leaf.y);
116 const uint type = __float_as_int(leaf.w);
117
118 /* pop */
119 node_addr = traversal_stack[stack_ptr];
120 --stack_ptr;
121
122 /* primitive intersection */
123 for (; prim_addr < prim_addr2; prim_addr++) {
124
125 const int prim_object = (object == OBJECT_NONE) ?
126 kernel_data_fetch(prim_object, prim_addr) :
127 object;
128 const int prim = kernel_data_fetch(prim_index, prim_addr);
129 if (intersection_skip_self_shadow(ray->self, prim_object, prim)) {
130 continue;
131 }
132
133#ifdef __SHADOW_LINKING__
134 if (intersection_skip_shadow_link(kg, ray->self, prim_object)) {
135 continue;
136 }
137#endif
138
139 switch (type & PRIMITIVE_ALL) {
140 case PRIMITIVE_TRIANGLE: {
141 if (triangle_intersect(kg,
142 isect,
143 P,
144 dir,
145 tmin,
146 isect->t,
147 visibility,
148 prim_object,
149 prim,
150 prim_addr))
151 {
152 /* shadow ray early termination */
153 if (visibility & PATH_RAY_VISIBILITY_SHADOW_OPAQUE) {
154 return true;
155 }
156 }
157 break;
158 }
159#if BVH_FEATURE(BVH_MOTION)
162 isect,
163 P,
164 dir,
165 tmin,
166 isect->t,
167 ray->time,
168 visibility,
169 prim_object,
170 prim,
171 prim_addr))
172 {
173 /* shadow ray early termination */
174 if (visibility & PATH_RAY_VISIBILITY_SHADOW_OPAQUE)
175 return true;
176 }
177 break;
178 }
179#endif /* BVH_FEATURE(BVH_MOTION) */
180#if BVH_FEATURE(BVH_HAIR) && defined(__HAIR__)
187 if ((type & PRIMITIVE_MOTION) && kernel_data.bvh.use_bvh_steps) {
188 const float2 prim_time = kernel_data_fetch(prim_time, prim_addr);
189 if (ray->time < prim_time.x || ray->time > prim_time.y) {
190 break;
191 }
192 }
193
194 const int curve_type = kernel_data_fetch(prim_type, prim_addr);
195 const bool hit = curve_intersect(
196 kg, isect, P, dir, tmin, isect->t, prim_object, prim, ray->time, curve_type);
197 if (hit) {
198 /* shadow ray early termination */
199 if (visibility & PATH_RAY_VISIBILITY_SHADOW_OPAQUE)
200 return true;
201 }
202 break;
203 }
204#endif /* BVH_FEATURE(BVH_HAIR) */
205#if BVH_FEATURE(BVH_POINTCLOUD)
206# if defined(__POINTCLOUD__)
207 case PRIMITIVE_POINT:
209 if ((type & PRIMITIVE_MOTION) && kernel_data.bvh.use_bvh_steps) {
210 const float2 prim_time = kernel_data_fetch(prim_time, prim_addr);
211 if (ray->time < prim_time.x || ray->time > prim_time.y) {
212 break;
213 }
214 }
215
216 const int point_type = kernel_data_fetch(prim_type, prim_addr);
217 const bool hit = point_intersect(
218 kg, isect, P, dir, tmin, isect->t, prim_object, prim, ray->time, point_type);
219 if (hit) {
220 /* shadow ray early termination */
221 if (visibility & PATH_RAY_VISIBILITY_SHADOW_OPAQUE)
222 return true;
223 }
224 break;
225 }
226# endif
227# if defined(__GSPLATS__)
228 case PRIMITIVE_GSPLAT:
230 if ((type & PRIMITIVE_MOTION) && kernel_data.bvh.use_bvh_steps) {
231 const float2 prim_time = kernel_data_fetch(prim_time, prim_addr);
232 if (ray->time < prim_time.x || ray->time > prim_time.y) {
233 break;
234 }
235 }
236
237 const int point_type = kernel_data_fetch(prim_type, prim_addr);
238 const bool hit = gsplat_intersect(
239 kg, isect, P, dir, tmin, isect->t, prim_object, prim, ray->time, point_type);
240 if (hit) {
241 /* shadow ray early termination */
242 if (visibility & PATH_RAY_VISIBILITY_SHADOW_OPAQUE)
243 return true;
244 }
245 break;
246 }
247# endif
248#endif /* BVH_FEATURE(BVH_POINTCLOUD) */
249 }
250 }
251 }
252 else {
253 /* instance push */
254 object = kernel_data_fetch(prim_object, -prim_addr - 1);
255
256#if BVH_FEATURE(BVH_MOTION)
257 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir);
258#else
259 bvh_instance_push(kg, object, ray, &P, &dir, &idir);
260#endif
261
262 ++stack_ptr;
263 kernel_assert(stack_ptr < BVH_STACK_SIZE);
264 traversal_stack[stack_ptr] = ENTRYPOINT_SENTINEL;
265
266 node_addr = kernel_data_fetch(object_node, object);
267 }
268 }
269 } while (node_addr != ENTRYPOINT_SENTINEL);
270
271 if (stack_ptr >= 0) {
272 kernel_assert(object != OBJECT_NONE);
273
274 /* instance pop */
275 bvh_instance_pop(ray, &P, &dir, &idir);
276
277 object = OBJECT_NONE;
278 node_addr = traversal_stack[stack_ptr];
279 --stack_ptr;
280 }
281 } while (node_addr != ENTRYPOINT_SENTINEL);
282
283 return (isect->prim != PRIM_NONE);
284}
285
287 const ccl_private Ray *ray,
289 const uint visibility)
290{
291 return BVH_FUNCTION_FULL_NAME(BVH)(kg, ray, isect, visibility);
292}
293
294#undef BVH_FUNCTION_NAME
295#undef BVH_FUNCTION_FEATURES
296#undef NODE_INTERSECT
Definition bvh/bvh.h:67
ccl_device_inline bool intersection_skip_shadow_link(KernelGlobals kg, const ccl_ray_data RaySelfPrimitives &self, const int isect_object)
ccl_device_inline bool intersection_skip_self_shadow(const ccl_ray_data RaySelfPrimitives &self, const int object, const int prim)
#define kernel_assert(cond)
#define kernel_data
#define kernel_data_fetch(name, index)
#define PRIM_NONE
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define ccl_device_noinline
#define __float_as_int(x)
#define BVH_FUNCTION_NAME
#define BVH_STACK_SIZE
#define BVH_FEATURE(f)
#define ENTRYPOINT_SENTINEL
#define BVH_HAIR
#define BVH_FUNCTION_FULL_NAME(prefix)
ccl_device_inline void bvh_instance_push(KernelGlobals kg, const int object, const ccl_private Ray *ray, ccl_private float3 *P, ccl_private float3 *dir, ccl_private float3 *idir)
ccl_device_inline float3 bvh_inverse_direction(const float3 dir)
ccl_device_inline float3 bvh_clamp_direction(const float3 dir)
ccl_device_inline void bvh_instance_pop(const ccl_private Ray *ray, ccl_private float3 *P, ccl_private float3 *dir, ccl_private float3 *idir)
@ PRIMITIVE_MOTION_CURVE_RIBBON
@ PRIMITIVE_CURVE_THICK_LINEAR
@ PRIMITIVE_ALL
@ PRIMITIVE_MOTION
@ PRIMITIVE_MOTION_TRIANGLE
@ PRIMITIVE_GSPLAT
@ PRIMITIVE_CURVE_RIBBON
@ PRIMITIVE_MOTION_GSPLAT
@ PRIMITIVE_MOTION_CURVE_THICK
@ PRIMITIVE_CURVE_THICK
@ PRIMITIVE_TRIANGLE
@ PRIMITIVE_MOTION_CURVE_THICK_LINEAR
@ PRIMITIVE_MOTION_POINT
@ PRIMITIVE_POINT
@ PATH_RAY_VISIBILITY_SHADOW_OPAQUE
#define NODE_INTERSECT
Definition local.h:11
CCL_NAMESPACE_BEGIN ccl_device_inline bool motion_triangle_intersect(KernelGlobals kg, ccl_private Intersection *isect, const float3 P, const float3 dir, const float tmin, const float tmax, const float time, const uint visibility, const int object, const int prim, const int prim_addr)
float x
float y
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
CCL_NAMESPACE_BEGIN ccl_device_inline bool triangle_intersect(KernelGlobals kg, ccl_private Intersection *isect, const float3 P, const float3 dir, const float tmin, const float tmax, const uint visibility, const int object, const int prim, const int prim_addr)