2 * Adapted from code Copyright 2009-2010 NVIDIA Corporation,
3 * and code copyright 2009-2012 Intel Corporation
5 * Modifications Copyright 2011-2013, 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.
20 /* This is a template BVH traversal function, where various features can be
21 * enabled/disabled. This way we can compile optimized versions for each case
22 * without new features slowing things down.
24 * BVH_INSTANCING: object instancing
25 * BVH_HAIR: hair curve rendering
26 * BVH_HAIR_MINIMUM_WIDTH: hair curve rendering with minimum width
27 * BVH_MOTION: motion blur rendering
31 #define FEATURE(f) (((BVH_FUNCTION_FEATURES) & (f)) != 0)
33 __device bool BVH_FUNCTION_NAME
34 (KernelGlobals *kg, const Ray *ray, Intersection *isect, const uint visibility
35 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
36 , uint *lcg_state, float difl, float extmax
41 * - test if pushing distance on the stack helps (for non shadow rays)
42 * - separate version for shadow rays
43 * - likely and unlikely for if() statements
45 * - test restrict attribute for pointers
48 /* traversal stack in CUDA thread-local memory */
49 int traversalStack[BVH_STACK_SIZE];
50 traversalStack[0] = ENTRYPOINT_SENTINEL;
52 /* traversal variables in registers */
54 int nodeAddr = kernel_data.bvh.root;
56 /* ray parameters in registers */
57 const float tmax = ray->t;
59 float3 idir = bvh_inverse_direction(ray->D);
62 #if FEATURE(BVH_MOTION)
72 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
73 const shuffle_swap_t shuf_identity = shuffle_swap_identity();
74 const shuffle_swap_t shuf_swap = shuffle_swap_swap();
76 const __m128i pn = _mm_set_epi32(0x80000000, 0x80000000, 0x00000000, 0x00000000);
77 __m128 Psplat[3], idirsplat[3];
79 Psplat[0] = _mm_set_ps1(P.x);
80 Psplat[1] = _mm_set_ps1(P.y);
81 Psplat[2] = _mm_set_ps1(P.z);
83 idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
84 idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
85 idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
87 __m128 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
89 shuffle_swap_t shufflex = (idir.x >= 0)? shuf_identity: shuf_swap;
90 shuffle_swap_t shuffley = (idir.y >= 0)? shuf_identity: shuf_swap;
91 shuffle_swap_t shufflez = (idir.z >= 0)? shuf_identity: shuf_swap;
98 /* traverse internal nodes */
99 while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL)
101 bool traverseChild0, traverseChild1;
104 #if !defined(__KERNEL_SSE2__) || FEATURE(BVH_HAIR_MINIMUM_WIDTH)
105 /* Intersect two child bounding boxes, non-SSE version */
108 /* fetch node data */
109 float4 node0 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+0);
110 float4 node1 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+1);
111 float4 node2 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+2);
112 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+3);
114 /* intersect ray against child nodes */
115 NO_EXTENDED_PRECISION float c0lox = (node0.x - P.x) * idir.x;
116 NO_EXTENDED_PRECISION float c0hix = (node0.z - P.x) * idir.x;
117 NO_EXTENDED_PRECISION float c0loy = (node1.x - P.y) * idir.y;
118 NO_EXTENDED_PRECISION float c0hiy = (node1.z - P.y) * idir.y;
119 NO_EXTENDED_PRECISION float c0loz = (node2.x - P.z) * idir.z;
120 NO_EXTENDED_PRECISION float c0hiz = (node2.z - P.z) * idir.z;
121 NO_EXTENDED_PRECISION float c0min = max4(min(c0lox, c0hix), min(c0loy, c0hiy), min(c0loz, c0hiz), 0.0f);
122 NO_EXTENDED_PRECISION float c0max = min4(max(c0lox, c0hix), max(c0loy, c0hiy), max(c0loz, c0hiz), t);
124 NO_EXTENDED_PRECISION float c1lox = (node0.y - P.x) * idir.x;
125 NO_EXTENDED_PRECISION float c1hix = (node0.w - P.x) * idir.x;
126 NO_EXTENDED_PRECISION float c1loy = (node1.y - P.y) * idir.y;
127 NO_EXTENDED_PRECISION float c1hiy = (node1.w - P.y) * idir.y;
128 NO_EXTENDED_PRECISION float c1loz = (node2.y - P.z) * idir.z;
129 NO_EXTENDED_PRECISION float c1hiz = (node2.w - P.z) * idir.z;
130 NO_EXTENDED_PRECISION float c1min = max4(min(c1lox, c1hix), min(c1loy, c1hiy), min(c1loz, c1hiz), 0.0f);
131 NO_EXTENDED_PRECISION float c1max = min4(max(c1lox, c1hix), max(c1loy, c1hiy), max(c1loz, c1hiz), t);
133 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
135 float hdiff = 1.0f + difl;
136 float ldiff = 1.0f - difl;
137 if(__float_as_int(cnodes.z) & PATH_RAY_CURVE) {
138 c0min = max(ldiff * c0min, c0min - extmax);
139 c0max = min(hdiff * c0max, c0max + extmax);
141 if(__float_as_int(cnodes.w) & PATH_RAY_CURVE) {
142 c1min = max(ldiff * c1min, c1min - extmax);
143 c1max = min(hdiff * c1max, c1max + extmax);
148 /* decide which nodes to traverse next */
149 #ifdef __VISIBILITY_FLAG__
150 /* this visibility test gives a 5% performance hit, how to solve? */
151 traverseChild0 = (c0max >= c0min) && (__float_as_uint(cnodes.z) & visibility);
152 traverseChild1 = (c1max >= c1min) && (__float_as_uint(cnodes.w) & visibility);
154 traverseChild0 = (c0max >= c0min);
155 traverseChild1 = (c1max >= c1min);
158 #else // __KERNEL_SSE2__
159 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
161 /* fetch node data */
162 __m128 *bvh_nodes = (__m128*)kg->__bvh_nodes.data + nodeAddr*BVH_NODE_SIZE;
163 float4 cnodes = ((float4*)bvh_nodes)[3];
165 /* intersect ray against child nodes */
166 const __m128 tminmaxx = _mm_mul_ps(_mm_sub_ps(shuffle_swap(bvh_nodes[0], shufflex), Psplat[0]), idirsplat[0]);
167 const __m128 tminmaxy = _mm_mul_ps(_mm_sub_ps(shuffle_swap(bvh_nodes[1], shuffley), Psplat[1]), idirsplat[1]);
168 const __m128 tminmaxz = _mm_mul_ps(_mm_sub_ps(shuffle_swap(bvh_nodes[2], shufflez), Psplat[2]), idirsplat[2]);
170 const __m128 tminmax = _mm_xor_ps(_mm_max_ps(_mm_max_ps(tminmaxx, tminmaxy), _mm_max_ps(tminmaxz, tsplat)), _mm_castsi128_ps(pn));
171 const __m128 lrhit = _mm_cmple_ps(tminmax, shuffle_swap(tminmax, shuf_swap));
173 /* decide which nodes to traverse next */
174 #ifdef __VISIBILITY_FLAG__
175 /* this visibility test gives a 5% performance hit, how to solve? */
176 traverseChild0 = (_mm_movemask_ps(lrhit) & 1) && (__float_as_uint(cnodes.z) & visibility);
177 traverseChild1 = (_mm_movemask_ps(lrhit) & 2) && (__float_as_uint(cnodes.w) & visibility);
179 traverseChild0 = (_mm_movemask_ps(lrhit) & 1);
180 traverseChild1 = (_mm_movemask_ps(lrhit) & 2);
182 #endif // __KERNEL_SSE2__
184 nodeAddr = __float_as_int(cnodes.x);
185 nodeAddrChild1 = __float_as_int(cnodes.y);
187 if(traverseChild0 && traverseChild1) {
188 /* both children were intersected, push the farther one */
189 #if !defined(__KERNEL_SSE2__) || FEATURE(BVH_HAIR_MINIMUM_WIDTH)
190 bool closestChild1 = (c1min < c0min);
192 union { __m128 m128; float v[4]; } uminmax;
193 uminmax.m128 = tminmax;
194 bool closestChild1 = uminmax.v[1] < uminmax.v[0];
199 nodeAddr = nodeAddrChild1;
200 nodeAddrChild1 = tmp;
204 traversalStack[stackPtr] = nodeAddrChild1;
207 /* one child was intersected */
209 nodeAddr = nodeAddrChild1;
211 else if(!traverseChild0) {
212 /* neither child was intersected */
213 nodeAddr = traversalStack[stackPtr];
219 /* if node is leaf, fetch triangle list */
221 float4 leaf = kernel_tex_fetch(__bvh_nodes, (-nodeAddr-1)*BVH_NODE_SIZE+(BVH_NODE_SIZE-1));
222 int primAddr = __float_as_int(leaf.x);
224 #if FEATURE(BVH_INSTANCING)
227 int primAddr2 = __float_as_int(leaf.y);
230 nodeAddr = traversalStack[stackPtr];
233 /* primitive intersection */
234 while(primAddr < primAddr2) {
237 /* intersect ray against primitive */
238 #if FEATURE(BVH_HAIR)
239 uint segment = kernel_tex_fetch(__prim_segment, primAddr);
242 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
243 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
244 hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
246 hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
248 hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
250 hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
255 hit = bvh_triangle_intersect(kg, isect, P, idir, visibility, object, primAddr);
257 /* shadow ray early termination */
258 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
260 if(visibility == PATH_RAY_SHADOW_OPAQUE)
263 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
266 if(hit && visibility == PATH_RAY_SHADOW_OPAQUE)
273 #if FEATURE(BVH_INSTANCING)
276 object = kernel_tex_fetch(__prim_object, -primAddr-1);
278 #if FEATURE(BVH_MOTION)
279 bvh_instance_motion_push(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
281 bvh_instance_push(kg, object, ray, &P, &idir, &isect->t, tmax);
284 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
285 Psplat[0] = _mm_set_ps1(P.x);
286 Psplat[1] = _mm_set_ps1(P.y);
287 Psplat[2] = _mm_set_ps1(P.z);
289 idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
290 idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
291 idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
293 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
295 shufflex = (idir.x >= 0)? shuf_identity: shuf_swap;
296 shuffley = (idir.y >= 0)? shuf_identity: shuf_swap;
297 shufflez = (idir.z >= 0)? shuf_identity: shuf_swap;
301 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
303 nodeAddr = kernel_tex_fetch(__object_node, object);
307 } while(nodeAddr != ENTRYPOINT_SENTINEL);
309 #if FEATURE(BVH_INSTANCING)
311 kernel_assert(object != ~0);
314 #if FEATURE(BVH_MOTION)
315 bvh_instance_motion_pop(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
317 bvh_instance_pop(kg, object, ray, &P, &idir, &isect->t, tmax);
320 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
321 Psplat[0] = _mm_set_ps1(P.x);
322 Psplat[1] = _mm_set_ps1(P.y);
323 Psplat[2] = _mm_set_ps1(P.z);
325 idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
326 idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
327 idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
329 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
331 shufflex = (idir.x >= 0)? shuf_identity: shuf_swap;
332 shuffley = (idir.y >= 0)? shuf_identity: shuf_swap;
333 shufflez = (idir.z >= 0)? shuf_identity: shuf_swap;
337 nodeAddr = traversalStack[stackPtr];
341 } while(nodeAddr != ENTRYPOINT_SENTINEL);
343 return (isect->prim != ~0);
347 #undef BVH_FUNCTION_NAME
348 #undef BVH_FUNCTION_FEATURES