cfca405e7a53f3fe0059217fc5f060262a898cab
[blender.git] / intern / cycles / kernel / kernel_bvh_traversal.h
1 /*
2  * Adapted from code Copyright 2009-2010 NVIDIA Corporation,
3  * and code copyright 2009-2012 Intel Corporation
4  *
5  * Modifications Copyright 2011-2013, Blender Foundation.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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.
23  *
24  * BVH_INSTANCING: object instancing
25  * BVH_HAIR: hair curve rendering
26  * BVH_HAIR_MINIMUM_WIDTH: hair curve rendering with minimum width
27  * BVH_SUBSURFACE: subsurface same object, random triangle intersection
28  * BVH_MOTION: motion blur rendering
29  *
30  */
31
32 #define FEATURE(f) (((BVH_FUNCTION_FEATURES) & (f)) != 0)
33
34 __device bool BVH_FUNCTION_NAME
35 (KernelGlobals *kg, const Ray *ray, Intersection *isect
36 #if FEATURE(BVH_SUBSURFACE)
37 , int subsurface_object, float subsurface_random
38 #else
39 , const uint visibility
40 #endif
41 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH) && !FEATURE(BVH_SUBSURFACE)
42 , uint *lcg_state, float difl, float extmax
43 #endif
44 )
45 {
46         /* todo:
47          * - test if pushing distance on the stack helps (for non shadow rays)
48          * - separate version for shadow rays
49          * - likely and unlikely for if() statements
50          * - SSE for hair
51          * - test restrict attribute for pointers
52          */
53         
54         /* traversal stack in CUDA thread-local memory */
55         int traversalStack[BVH_STACK_SIZE];
56         traversalStack[0] = ENTRYPOINT_SENTINEL;
57
58         /* traversal variables in registers */
59         int stackPtr = 0;
60         int nodeAddr = kernel_data.bvh.root;
61
62         /* ray parameters in registers */
63         const float tmax = ray->t;
64         float3 P = ray->P;
65         float3 idir = bvh_inverse_direction(ray->D);
66         int object = ~0;
67
68 #if FEATURE(BVH_SUBSURFACE)
69         const uint visibility = ~0;
70         int num_hits = 0;
71 #endif
72
73 #if FEATURE(BVH_MOTION)
74         Transform ob_tfm;
75 #endif
76
77         isect->t = tmax;
78         isect->object = ~0;
79         isect->prim = ~0;
80         isect->u = 0.0f;
81         isect->v = 0.0f;
82
83 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
84         const shuffle_swap_t shuf_identity = shuffle_swap_identity();
85         const shuffle_swap_t shuf_swap = shuffle_swap_swap();
86         
87         const __m128i pn = _mm_set_epi32(0x80000000, 0x80000000, 0x00000000, 0x00000000);
88         __m128 Psplat[3], idirsplat[3];
89
90         Psplat[0] = _mm_set_ps1(P.x);
91         Psplat[1] = _mm_set_ps1(P.y);
92         Psplat[2] = _mm_set_ps1(P.z);
93
94         idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
95         idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
96         idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
97
98         __m128 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
99
100         shuffle_swap_t shufflex = (idir.x >= 0)? shuf_identity: shuf_swap;
101         shuffle_swap_t shuffley = (idir.y >= 0)? shuf_identity: shuf_swap;
102         shuffle_swap_t shufflez = (idir.z >= 0)? shuf_identity: shuf_swap;
103 #endif
104
105         /* traversal loop */
106         do {
107                 do
108                 {
109                         /* traverse internal nodes */
110                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL)
111                         {
112                                 bool traverseChild0, traverseChild1;
113                                 int nodeAddrChild1;
114
115 #if !defined(__KERNEL_SSE2__) || FEATURE(BVH_HAIR_MINIMUM_WIDTH)
116                                 /* Intersect two child bounding boxes, non-SSE version */
117                                 float t = isect->t;
118
119                                 /* fetch node data */
120                                 float4 node0 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+0);
121                                 float4 node1 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+1);
122                                 float4 node2 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+2);
123                                 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+3);
124
125                                 /* intersect ray against child nodes */
126                                 NO_EXTENDED_PRECISION float c0lox = (node0.x - P.x) * idir.x;
127                                 NO_EXTENDED_PRECISION float c0hix = (node0.z - P.x) * idir.x;
128                                 NO_EXTENDED_PRECISION float c0loy = (node1.x - P.y) * idir.y;
129                                 NO_EXTENDED_PRECISION float c0hiy = (node1.z - P.y) * idir.y;
130                                 NO_EXTENDED_PRECISION float c0loz = (node2.x - P.z) * idir.z;
131                                 NO_EXTENDED_PRECISION float c0hiz = (node2.z - P.z) * idir.z;
132                                 NO_EXTENDED_PRECISION float c0min = max4(min(c0lox, c0hix), min(c0loy, c0hiy), min(c0loz, c0hiz), 0.0f);
133                                 NO_EXTENDED_PRECISION float c0max = min4(max(c0lox, c0hix), max(c0loy, c0hiy), max(c0loz, c0hiz), t);
134
135                                 NO_EXTENDED_PRECISION float c1lox = (node0.y - P.x) * idir.x;
136                                 NO_EXTENDED_PRECISION float c1hix = (node0.w - P.x) * idir.x;
137                                 NO_EXTENDED_PRECISION float c1loy = (node1.y - P.y) * idir.y;
138                                 NO_EXTENDED_PRECISION float c1hiy = (node1.w - P.y) * idir.y;
139                                 NO_EXTENDED_PRECISION float c1loz = (node2.y - P.z) * idir.z;
140                                 NO_EXTENDED_PRECISION float c1hiz = (node2.w - P.z) * idir.z;
141                                 NO_EXTENDED_PRECISION float c1min = max4(min(c1lox, c1hix), min(c1loy, c1hiy), min(c1loz, c1hiz), 0.0f);
142                                 NO_EXTENDED_PRECISION float c1max = min4(max(c1lox, c1hix), max(c1loy, c1hiy), max(c1loz, c1hiz), t);
143
144 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH) && !FEATURE(BVH_SUBSURFACE)
145                                 if(difl != 0.0f) {
146                                         float hdiff = 1.0f + difl;
147                                         float ldiff = 1.0f - difl;
148                                         if(__float_as_int(cnodes.z) & PATH_RAY_CURVE) {
149                                                 c0min = max(ldiff * c0min, c0min - extmax);
150                                                 c0max = min(hdiff * c0max, c0max + extmax);
151                                         }
152                                         if(__float_as_int(cnodes.w) & PATH_RAY_CURVE) {
153                                                 c1min = max(ldiff * c1min, c1min - extmax);
154                                                 c1max = min(hdiff * c1max, c1max + extmax);
155                                         }
156                                 }
157 #endif
158
159                                 /* decide which nodes to traverse next */
160 #ifdef __VISIBILITY_FLAG__
161                                 /* this visibility test gives a 5% performance hit, how to solve? */
162                                 traverseChild0 = (c0max >= c0min) && (__float_as_uint(cnodes.z) & visibility);
163                                 traverseChild1 = (c1max >= c1min) && (__float_as_uint(cnodes.w) & visibility);
164 #else
165                                 traverseChild0 = (c0max >= c0min);
166                                 traverseChild1 = (c1max >= c1min);
167 #endif
168
169 #else // __KERNEL_SSE2__
170                                 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
171
172                                 /* fetch node data */
173                                 __m128 *bvh_nodes = (__m128*)kg->__bvh_nodes.data + nodeAddr*BVH_NODE_SIZE;
174                                 float4 cnodes = ((float4*)bvh_nodes)[3];
175
176                                 /* intersect ray against child nodes */
177                                 const __m128 tminmaxx = _mm_mul_ps(_mm_sub_ps(shuffle_swap(bvh_nodes[0], shufflex), Psplat[0]), idirsplat[0]);
178                                 const __m128 tminmaxy = _mm_mul_ps(_mm_sub_ps(shuffle_swap(bvh_nodes[1], shuffley), Psplat[1]), idirsplat[1]);
179                                 const __m128 tminmaxz = _mm_mul_ps(_mm_sub_ps(shuffle_swap(bvh_nodes[2], shufflez), Psplat[2]), idirsplat[2]);
180
181                                 const __m128 tminmax = _mm_xor_ps(_mm_max_ps(_mm_max_ps(tminmaxx, tminmaxy), _mm_max_ps(tminmaxz, tsplat)), _mm_castsi128_ps(pn));
182                                 const __m128 lrhit = _mm_cmple_ps(tminmax, shuffle_swap(tminmax, shuf_swap));
183
184                                 /* decide which nodes to traverse next */
185 #ifdef __VISIBILITY_FLAG__
186                                 /* this visibility test gives a 5% performance hit, how to solve? */
187                                 traverseChild0 = (_mm_movemask_ps(lrhit) & 1) && (__float_as_uint(cnodes.z) & visibility);
188                                 traverseChild1 = (_mm_movemask_ps(lrhit) & 2) && (__float_as_uint(cnodes.w) & visibility);
189 #else
190                                 traverseChild0 = (_mm_movemask_ps(lrhit) & 1);
191                                 traverseChild1 = (_mm_movemask_ps(lrhit) & 2);
192 #endif
193 #endif // __KERNEL_SSE2__
194
195                                 nodeAddr = __float_as_int(cnodes.x);
196                                 nodeAddrChild1 = __float_as_int(cnodes.y);
197
198                                 if(traverseChild0 && traverseChild1) {
199                                         /* both children were intersected, push the farther one */
200 #if !defined(__KERNEL_SSE2__) || FEATURE(BVH_HAIR_MINIMUM_WIDTH)
201                                         bool closestChild1 = (c1min < c0min);
202 #else
203                                         union { __m128 m128; float v[4]; } uminmax;
204                                         uminmax.m128 = tminmax;
205                                         bool closestChild1 = uminmax.v[1] < uminmax.v[0];
206 #endif
207
208                                         if(closestChild1) {
209                                                 int tmp = nodeAddr;
210                                                 nodeAddr = nodeAddrChild1;
211                                                 nodeAddrChild1 = tmp;
212                                         }
213
214                                         ++stackPtr;
215                                         traversalStack[stackPtr] = nodeAddrChild1;
216                                 }
217                                 else {
218                                         /* one child was intersected */
219                                         if(traverseChild1) {
220                                                 nodeAddr = nodeAddrChild1;
221                                         }
222                                         else if(!traverseChild0) {
223                                                 /* neither child was intersected */
224                                                 nodeAddr = traversalStack[stackPtr];
225                                                 --stackPtr;
226                                         }
227                                 }
228                         }
229
230                         /* if node is leaf, fetch triangle list */
231                         if(nodeAddr < 0) {
232                                 float4 leaf = kernel_tex_fetch(__bvh_nodes, (-nodeAddr-1)*BVH_NODE_SIZE+(BVH_NODE_SIZE-1));
233                                 int primAddr = __float_as_int(leaf.x);
234
235 #if FEATURE(BVH_INSTANCING)
236                                 if(primAddr >= 0) {
237 #endif
238                                         int primAddr2 = __float_as_int(leaf.y);
239
240                                         /* pop */
241                                         nodeAddr = traversalStack[stackPtr];
242                                         --stackPtr;
243
244                                         /* primitive intersection */
245                                         while(primAddr < primAddr2) {
246                                                 bool hit;
247
248 #if FEATURE(BVH_SUBSURFACE)
249                                                 /* only primitives from the same object */
250                                                 uint tri_object = (object == ~0)? kernel_tex_fetch(__prim_object, primAddr): object;
251
252                                                 if(tri_object == subsurface_object) {
253 #endif
254
255                                                         /* intersect ray against primitive */
256 #if FEATURE(BVH_HAIR)
257                                                         uint segment = kernel_tex_fetch(__prim_segment, primAddr);
258 #if !FEATURE(BVH_SUBSURFACE)
259                                                         if(segment != ~0) {
260
261                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE) 
262 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
263                                                                         hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
264                                                                 else
265                                                                         hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
266 #else
267                                                                         hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
268                                                                 else
269                                                                         hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
270 #endif
271                                                         }
272                                                         else
273 #endif
274 #endif
275 #if FEATURE(BVH_SUBSURFACE)
276 #if FEATURE(BVH_HAIR)
277                                                         if(segment == ~0)
278 #endif
279                                                         {
280                                                                 hit = bvh_triangle_intersect_subsurface(kg, isect, P, idir, object, primAddr, tmax, &num_hits, subsurface_random);
281                                                                 (void)hit;
282                                                         }
283
284                                                 }
285 #else
286                                                                 hit = bvh_triangle_intersect(kg, isect, P, idir, visibility, object, primAddr);
287
288                                                         /* shadow ray early termination */
289 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
290                                                         if(hit) {
291                                                                 if(visibility == PATH_RAY_SHADOW_OPAQUE)
292                                                                         return true;
293
294                                                                 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
295                                                         }
296 #else
297                                                         if(hit && visibility == PATH_RAY_SHADOW_OPAQUE)
298                                                                 return true;
299 #endif
300
301 #endif
302
303                                                 primAddr++;
304                                         }
305                                 }
306 #if FEATURE(BVH_INSTANCING)
307                                 else {
308                                         /* instance push */
309 #if FEATURE(BVH_SUBSURFACE)
310                                         if(subsurface_object == kernel_tex_fetch(__prim_object, -primAddr-1)) {
311                                                 object = subsurface_object;
312 #else
313                                                 object = kernel_tex_fetch(__prim_object, -primAddr-1);
314 #endif
315
316 #if FEATURE(BVH_MOTION)
317                                                 bvh_instance_motion_push(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
318 #else
319                                                 bvh_instance_push(kg, object, ray, &P, &idir, &isect->t, tmax);
320 #endif
321
322 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
323                                                 Psplat[0] = _mm_set_ps1(P.x);
324                                                 Psplat[1] = _mm_set_ps1(P.y);
325                                                 Psplat[2] = _mm_set_ps1(P.z);
326
327                                                 idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
328                                                 idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
329                                                 idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
330
331                                                 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
332
333                                                 shufflex = (idir.x >= 0)? shuf_identity: shuf_swap;
334                                                 shuffley = (idir.y >= 0)? shuf_identity: shuf_swap;
335                                                 shufflez = (idir.z >= 0)? shuf_identity: shuf_swap;
336 #endif
337
338                                                 ++stackPtr;
339                                                 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
340
341                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
342 #if FEATURE(BVH_SUBSURFACE)
343                                         }
344                                         else {
345                                                 /* pop */
346                                                 nodeAddr = traversalStack[stackPtr];
347                                                 --stackPtr;
348                                         }
349 #endif
350                                 }
351                         }
352 #endif
353                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
354
355 #if FEATURE(BVH_INSTANCING)
356                 if(stackPtr >= 0) {
357                         kernel_assert(object != ~0);
358
359                         /* instance pop */
360 #if FEATURE(BVH_MOTION)
361                         bvh_instance_motion_pop(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
362 #else
363                         bvh_instance_pop(kg, object, ray, &P, &idir, &isect->t, tmax);
364 #endif
365
366 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
367                         Psplat[0] = _mm_set_ps1(P.x);
368                         Psplat[1] = _mm_set_ps1(P.y);
369                         Psplat[2] = _mm_set_ps1(P.z);
370
371                         idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
372                         idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
373                         idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
374
375                         tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
376
377                         shufflex = (idir.x >= 0)? shuf_identity: shuf_swap;
378                         shuffley = (idir.y >= 0)? shuf_identity: shuf_swap;
379                         shufflez = (idir.z >= 0)? shuf_identity: shuf_swap;
380 #endif
381
382                         object = ~0;
383                         nodeAddr = traversalStack[stackPtr];
384                         --stackPtr;
385                 }
386 #endif
387         } while(nodeAddr != ENTRYPOINT_SENTINEL);
388
389 #if FEATURE(BVH_SUBSURFACE)
390         return (num_hits != 0);
391 #else
392         return (isect->prim != ~0);
393 #endif
394 }
395
396 #undef FEATURE
397 #undef BVH_FUNCTION_NAME
398 #undef BVH_FUNCTION_FEATURES
399