Blender V5.3
shadow_all.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
24#ifndef __KERNEL_GPU__
26#else
28#endif
30 const ccl_private Ray *ccl_restrict ray,
31 ccl_private BVHShadowAllPayload &ccl_restrict payload)
32{
33 /* todo:
34 * - likely and unlikely for if() statements
35 * - test restrict attribute for pointers
36 */
37
38 /* traversal stack in CUDA thread-local memory */
39 int traversal_stack[BVH_STACK_SIZE];
40 traversal_stack[0] = ENTRYPOINT_SENTINEL;
41
42 /* traversal variables in registers */
43 int stack_ptr = 0;
44 int node_addr = kernel_data.bvh.root;
45
46 /* ray parameters in registers */
47 float3 P = ray->P;
48 float3 dir = bvh_clamp_direction(ray->D);
49 float3 idir = bvh_inverse_direction(dir);
50 float tmin = ray->tmin;
51 int object = OBJECT_NONE;
52
53 /* Max distance in world space. May be dynamically reduced when max number of recorded hits is
54 * exceeded and we no longer need to find hits beyond the max distance found. */
55 const float tmax = ray->tmax;
56
57 const uint visibility = payload.base.ray_visibility;
58
59 /* traversal loop */
60 do {
61 do {
62 /* traverse internal nodes */
63 while (node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
64 int node_addr_child1, traverse_mask;
65 float dist[2];
66 float4 cnodes = kernel_data_fetch(bvh_nodes, node_addr + 0);
67
68 traverse_mask = NODE_INTERSECT(kg,
69 P,
71 dir,
72#endif
73 idir,
74 tmin,
75 tmax,
76 node_addr,
77 visibility,
78 dist);
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 kernel_assert((kernel_data_fetch(prim_type, prim_addr) & PRIMITIVE_ALL) ==
125 (type & PRIMITIVE_ALL));
126 bool hit;
127
128 /* todo: specialized intersect functions which don't fill in
129 * isect unless needed and check SD_HAS_TRANSPARENT_SHADOW?
130 * might give a few % performance improvement */
132
133 const int prim_object = (object == OBJECT_NONE) ?
134 kernel_data_fetch(prim_object, prim_addr) :
135 object;
136 const int prim = kernel_data_fetch(prim_index, prim_addr);
137 if (intersection_skip_self_shadow(ray->self, prim_object, prim)) {
138 continue;
139 }
140
141#ifdef __SHADOW_LINKING__
142 if (intersection_skip_shadow_link(kg, ray->self, prim_object)) {
143 continue;
144 }
145#endif
146
147 switch (type & PRIMITIVE_ALL) {
148 case PRIMITIVE_TRIANGLE: {
149 hit = triangle_intersect(
150 kg, &isect, P, dir, tmin, tmax, visibility, prim_object, prim, prim_addr);
151 break;
152 }
153#if BVH_FEATURE(BVH_MOTION)
156 &isect,
157 P,
158 dir,
159 tmin,
160 tmax,
161 ray->time,
162 visibility,
163 prim_object,
164 prim,
165 prim_addr);
166 break;
167 }
168#endif
169#if BVH_FEATURE(BVH_HAIR) && defined(__HAIR__)
176 if ((type & PRIMITIVE_MOTION) && kernel_data.bvh.use_bvh_steps) {
177 const float2 prim_time = kernel_data_fetch(prim_time, prim_addr);
178 if (ray->time < prim_time.x || ray->time > prim_time.y) {
179 hit = false;
180 break;
181 }
182 }
183
184 const int curve_type = kernel_data_fetch(prim_type, prim_addr);
185 hit = curve_intersect(
186 kg, &isect, P, dir, tmin, tmax, prim_object, prim, ray->time, curve_type);
187
188 break;
189 }
190#endif
191#if BVH_FEATURE(BVH_POINTCLOUD)
192# if defined(__POINTCLOUD__)
193 case PRIMITIVE_POINT:
195 if ((type & PRIMITIVE_MOTION) && kernel_data.bvh.use_bvh_steps) {
196 const float2 prim_time = kernel_data_fetch(prim_time, prim_addr);
197 if (ray->time < prim_time.x || ray->time > prim_time.y) {
198 hit = false;
199 break;
200 }
201 }
202
203 const int point_type = kernel_data_fetch(prim_type, prim_addr);
204 hit = point_intersect(
205 kg, &isect, P, dir, tmin, tmax, prim_object, prim, ray->time, point_type);
206 break;
207 }
208# endif
209# if defined(__GSPLATS__)
210 case PRIMITIVE_GSPLAT:
212 if ((type & PRIMITIVE_MOTION) && kernel_data.bvh.use_bvh_steps) {
213 const float2 prim_time = kernel_data_fetch(prim_time, prim_addr);
214 if (ray->time < prim_time.x || ray->time > prim_time.y) {
215 hit = false;
216 break;
217 }
218 }
219
220 /* TODO(sergey): Accumulate throughput instead of recording intersection. */
221 const int point_type = kernel_data_fetch(prim_type, prim_addr);
222 hit = gsplat_intersect(
223 kg, &isect, P, dir, tmin, tmax, prim_object, prim, ray->time, point_type);
224 break;
225 }
226# endif
227#endif /* BVH_FEATURE(BVH_POINTCLOUD) */
228 default: {
229 hit = false;
230 break;
231 }
232 }
233
234 if (hit) {
236 payload.state,
237 payload,
238 payload.base.ray_self,
239 payload.base.ray_visibility,
240 isect))
241 {
242 return;
243 }
244 }
245 }
246 }
247 else {
248 /* instance push */
249 object = kernel_data_fetch(prim_object, -prim_addr - 1);
250
251#if BVH_FEATURE(BVH_MOTION)
252 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir);
253#else
254 bvh_instance_push(kg, object, ray, &P, &dir, &idir);
255#endif
256
257 ++stack_ptr;
258 kernel_assert(stack_ptr < BVH_STACK_SIZE);
259 traversal_stack[stack_ptr] = ENTRYPOINT_SENTINEL;
260
261 node_addr = kernel_data_fetch(object_node, object);
262 }
263 }
264 } while (node_addr != ENTRYPOINT_SENTINEL);
265
266 if (stack_ptr >= 0) {
267 kernel_assert(object != OBJECT_NONE);
268
269 /* Instance pop. */
270 bvh_instance_pop(ray, &P, &dir, &idir);
271
272 object = OBJECT_NONE;
273 node_addr = traversal_stack[stack_ptr];
274 --stack_ptr;
275 }
276 } while (node_addr != ENTRYPOINT_SENTINEL);
277}
278
280 const ccl_private Ray *ccl_restrict ray,
281 ccl_private BVHShadowAllPayload &ccl_restrict payload)
282{
283 BVH_FUNCTION_FULL_NAME(BVH)(kg, ray, payload);
284}
285
286#undef BVH_FUNCTION_NAME
287#undef BVH_FUNCTION_FEATURES
288#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 ccl_restrict
#define ccl_optional_struct_init
#define kernel_data_fetch(name, index)
#define OBJECT_NONE
#define ccl_private
#define ccl_device_inline
#define __float_as_int(x)
ccl_device_forceinline bool bvh_shadow_all_anyhit_filter(KernelGlobals kg, IntegratorShadowState state, ccl_ray_data BVHShadowAllPayload &ccl_restrict payload, const ccl_ray_data RaySelfPrimitives &ccl_restrict ray_self, const uint ray_visibility, const Intersection isect)
#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
#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)
#define ccl_device
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)