2 * Adapted from code Copyright 2009-2010 NVIDIA Corporation,
3 * and code copyright 2009-2012 Intel Corporation
5 * Modifications Copyright 2011-2014, Blender Foundation.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 #include "geom_qbvh_volume.h"
24 /* This is a template BVH traversal function for volumes, where
25 * various features can be enabled/disabled. This way we can compile optimized
26 * versions for each case without new features slowing things down.
28 * BVH_INSTANCING: object instancing
29 * BVH_HAIR: hair curve rendering
30 * BVH_MOTION: motion blur rendering
34 ccl_device bool BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
39 * - test if pushing distance on the stack helps (for non shadow rays)
40 * - separate version for shadow rays
41 * - likely and unlikely for if() statements
42 * - test restrict attribute for pointers
45 /* traversal stack in CUDA thread-local memory */
46 int traversalStack[BVH_STACK_SIZE];
47 traversalStack[0] = ENTRYPOINT_SENTINEL;
49 /* traversal variables in registers */
51 int nodeAddr = kernel_data.bvh.root;
53 /* ray parameters in registers */
55 float3 dir = bvh_clamp_direction(ray->D);
56 float3 idir = bvh_inverse_direction(dir);
57 int object = OBJECT_NONE;
59 const uint visibility = PATH_RAY_ALL_VISIBILITY;
61 #if BVH_FEATURE(BVH_MOTION)
68 isect->prim = PRIM_NONE;
69 isect->object = OBJECT_NONE;
71 #if defined(__KERNEL_SSE2__)
72 const shuffle_swap_t shuf_identity = shuffle_swap_identity();
73 const shuffle_swap_t shuf_swap = shuffle_swap_swap();
75 const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
76 ssef Psplat[3], idirsplat[3];
77 shuffle_swap_t shufflexyz[3];
79 Psplat[0] = ssef(P.x);
80 Psplat[1] = ssef(P.y);
81 Psplat[2] = ssef(P.z);
83 ssef tsplat(0.0f, 0.0f, -isect->t, -isect->t);
85 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
88 IsectPrecalc isect_precalc;
89 triangle_intersect_precalc(dir, &isect_precalc);
94 /* traverse internal nodes */
95 while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
96 bool traverseChild0, traverseChild1;
99 #if !defined(__KERNEL_SSE2__)
100 /* Intersect two child bounding boxes, non-SSE version */
103 /* fetch node data */
104 float4 node0 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+0);
105 float4 node1 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+1);
106 float4 node2 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+2);
107 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+3);
109 /* intersect ray against child nodes */
110 NO_EXTENDED_PRECISION float c0lox = (node0.x - P.x) * idir.x;
111 NO_EXTENDED_PRECISION float c0hix = (node0.z - P.x) * idir.x;
112 NO_EXTENDED_PRECISION float c0loy = (node1.x - P.y) * idir.y;
113 NO_EXTENDED_PRECISION float c0hiy = (node1.z - P.y) * idir.y;
114 NO_EXTENDED_PRECISION float c0loz = (node2.x - P.z) * idir.z;
115 NO_EXTENDED_PRECISION float c0hiz = (node2.z - P.z) * idir.z;
116 NO_EXTENDED_PRECISION float c0min = max4(min(c0lox, c0hix), min(c0loy, c0hiy), min(c0loz, c0hiz), 0.0f);
117 NO_EXTENDED_PRECISION float c0max = min4(max(c0lox, c0hix), max(c0loy, c0hiy), max(c0loz, c0hiz), t);
119 NO_EXTENDED_PRECISION float c1lox = (node0.y - P.x) * idir.x;
120 NO_EXTENDED_PRECISION float c1hix = (node0.w - P.x) * idir.x;
121 NO_EXTENDED_PRECISION float c1loy = (node1.y - P.y) * idir.y;
122 NO_EXTENDED_PRECISION float c1hiy = (node1.w - P.y) * idir.y;
123 NO_EXTENDED_PRECISION float c1loz = (node2.y - P.z) * idir.z;
124 NO_EXTENDED_PRECISION float c1hiz = (node2.w - P.z) * idir.z;
125 NO_EXTENDED_PRECISION float c1min = max4(min(c1lox, c1hix), min(c1loy, c1hiy), min(c1loz, c1hiz), 0.0f);
126 NO_EXTENDED_PRECISION float c1max = min4(max(c1lox, c1hix), max(c1loy, c1hiy), max(c1loz, c1hiz), t);
128 /* decide which nodes to traverse next */
129 traverseChild0 = (c0max >= c0min);
130 traverseChild1 = (c1max >= c1min);
132 #else // __KERNEL_SSE2__
133 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
135 /* fetch node data */
136 const ssef *bvh_nodes = (ssef*)kg->__bvh_nodes.data + nodeAddr*BVH_NODE_SIZE;
137 const float4 cnodes = ((float4*)bvh_nodes)[3];
139 /* intersect ray against child nodes */
140 const ssef tminmaxx = (shuffle_swap(bvh_nodes[0], shufflexyz[0]) - Psplat[0]) * idirsplat[0];
141 const ssef tminmaxy = (shuffle_swap(bvh_nodes[1], shufflexyz[1]) - Psplat[1]) * idirsplat[1];
142 const ssef tminmaxz = (shuffle_swap(bvh_nodes[2], shufflexyz[2]) - Psplat[2]) * idirsplat[2];
144 /* calculate { c0min, c1min, -c0max, -c1max} */
145 ssef minmax = max(max(tminmaxx, tminmaxy), max(tminmaxz, tsplat));
146 const ssef tminmax = minmax ^ pn;
148 const sseb lrhit = tminmax <= shuffle<2, 3, 0, 1>(tminmax);
150 /* decide which nodes to traverse next */
151 traverseChild0 = (movemask(lrhit) & 1);
152 traverseChild1 = (movemask(lrhit) & 2);
153 #endif // __KERNEL_SSE2__
155 nodeAddr = __float_as_int(cnodes.x);
156 nodeAddrChild1 = __float_as_int(cnodes.y);
158 if(traverseChild0 && traverseChild1) {
159 /* both children were intersected, push the farther one */
160 #if !defined(__KERNEL_SSE2__)
161 bool closestChild1 = (c1min < c0min);
163 bool closestChild1 = tminmax[1] < tminmax[0];
168 nodeAddr = nodeAddrChild1;
169 nodeAddrChild1 = tmp;
173 kernel_assert(stackPtr < BVH_STACK_SIZE);
174 traversalStack[stackPtr] = nodeAddrChild1;
177 /* one child was intersected */
179 nodeAddr = nodeAddrChild1;
181 else if(!traverseChild0) {
182 /* neither child was intersected */
183 nodeAddr = traversalStack[stackPtr];
189 /* if node is leaf, fetch triangle list */
191 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_NODE_LEAF_SIZE);
192 int primAddr = __float_as_int(leaf.x);
194 #if BVH_FEATURE(BVH_INSTANCING)
197 const int primAddr2 = __float_as_int(leaf.y);
198 const uint type = __float_as_int(leaf.w);
201 nodeAddr = traversalStack[stackPtr];
204 /* primitive intersection */
205 switch(type & PRIMITIVE_ALL) {
206 case PRIMITIVE_TRIANGLE: {
207 /* intersect ray against primitive */
208 for(; primAddr < primAddr2; primAddr++) {
209 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
210 /* only primitives from volume object */
211 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
212 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
213 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
216 triangle_intersect(kg, &isect_precalc, isect, P, visibility, object, primAddr);
220 #if BVH_FEATURE(BVH_MOTION)
221 case PRIMITIVE_MOTION_TRIANGLE: {
222 /* intersect ray against primitive */
223 for(; primAddr < primAddr2; primAddr++) {
224 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
225 /* only primitives from volume object */
226 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
227 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
228 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
231 motion_triangle_intersect(kg, isect, P, dir, ray->time, visibility, object, primAddr);
236 #if BVH_FEATURE(BVH_HAIR)
237 case PRIMITIVE_CURVE:
238 case PRIMITIVE_MOTION_CURVE: {
239 /* intersect ray against primitive */
240 for(; primAddr < primAddr2; primAddr++) {
241 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
242 /* only primitives from volume object */
243 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
244 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
245 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
248 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
249 bvh_cardinal_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
251 bvh_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
261 #if BVH_FEATURE(BVH_INSTANCING)
264 object = kernel_tex_fetch(__prim_object, -primAddr-1);
265 int object_flag = kernel_tex_fetch(__object_flag, object);
267 if(object_flag & SD_OBJECT_HAS_VOLUME) {
269 # if BVH_FEATURE(BVH_MOTION)
270 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_itfm);
272 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect->t);
275 triangle_intersect_precalc(dir, &isect_precalc);
277 # if defined(__KERNEL_SSE2__)
278 Psplat[0] = ssef(P.x);
279 Psplat[1] = ssef(P.y);
280 Psplat[2] = ssef(P.z);
282 tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
284 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
288 kernel_assert(stackPtr < BVH_STACK_SIZE);
289 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
291 nodeAddr = kernel_tex_fetch(__object_node, object);
295 object = OBJECT_NONE;
296 nodeAddr = traversalStack[stackPtr];
301 #endif /* FEATURE(BVH_INSTANCING) */
302 } while(nodeAddr != ENTRYPOINT_SENTINEL);
304 #if BVH_FEATURE(BVH_INSTANCING)
306 kernel_assert(object != OBJECT_NONE);
309 # if BVH_FEATURE(BVH_MOTION)
310 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_itfm);
312 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &isect->t);
315 triangle_intersect_precalc(dir, &isect_precalc);
317 # if defined(__KERNEL_SSE2__)
318 Psplat[0] = ssef(P.x);
319 Psplat[1] = ssef(P.y);
320 Psplat[2] = ssef(P.z);
322 tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
324 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
327 object = OBJECT_NONE;
328 nodeAddr = traversalStack[stackPtr];
331 #endif /* FEATURE(BVH_MOTION) */
332 } while(nodeAddr != ENTRYPOINT_SENTINEL);
334 return (isect->prim != PRIM_NONE);
337 ccl_device_inline bool BVH_FUNCTION_NAME(KernelGlobals *kg,
342 if(kernel_data.bvh.use_qbvh) {
343 return BVH_FUNCTION_FULL_NAME(QBVH)(kg,
350 kernel_assert(kernel_data.bvh.use_qbvh == false);
351 return BVH_FUNCTION_FULL_NAME(BVH)(kg,
357 #undef BVH_FUNCTION_NAME
358 #undef BVH_FUNCTION_FEATURES