Merging r57518 through r57545 from trunk into soc-20133-depsgraph_mt
[blender-staging.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_SSE3__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
84         const __m128i shuffle_identity = _mm_set_epi8(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0);
85         const __m128i shuffle_swap = _mm_set_epi8(7, 6, 5, 4, 3, 2, 1, 0, 15, 14, 13, 12, 11, 10, 9, 8);
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         __m128i shufflex = (idir.x >= 0)? shuffle_identity: shuffle_swap;
101         __m128i shuffley = (idir.y >= 0)? shuffle_identity: shuffle_swap;
102         __m128i shufflez = (idir.z >= 0)? shuffle_identity: shuffle_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                                 float t = isect->t;
115
116 #if !defined(__KERNEL_SSE3__) || FEATURE(BVH_HAIR_MINIMUM_WIDTH)
117                                 /* Intersect two child bounding boxes, non-SSE version */
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_SSE3__
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(shuffle8(bvh_nodes[0], shufflex), Psplat[0]), idirsplat[0]);
178                                 const __m128 tminmaxy = _mm_mul_ps(_mm_sub_ps(shuffle8(bvh_nodes[1], shuffley), Psplat[1]), idirsplat[1]);
179                                 const __m128 tminmaxz = _mm_mul_ps(_mm_sub_ps(shuffle8(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, shuffle8(tminmax, shuffle_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_SSE3__
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_SSE3__) || 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 #if FEATURE(BVH_SUBSURFACE)
248                                                 /* only primitives from the same object */
249                                                 uint tri_object = (object == ~0)? kernel_tex_fetch(__prim_object, primAddr): object;
250
251                                                 if(tri_object == subsurface_object) {
252 #endif
253
254                                                         /* intersect ray against primitive */
255 #if FEATURE(BVH_HAIR)
256                                                         uint segment = kernel_tex_fetch(__prim_segment, primAddr);
257 #if !FEATURE(BVH_SUBSURFACE)
258                                                         if(segment != ~0) {
259
260                                                                 if(kernel_data.curve_kernel_data.curveflags & CURVE_KN_INTERPOLATE) 
261 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
262                                                                         hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
263                                                                 else
264                                                                         hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
265 #else
266                                                                         hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
267                                                                 else
268                                                                         hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
269 #endif
270                                                         }
271                                                         else
272 #endif
273 #endif
274 #if FEATURE(BVH_SUBSURFACE)
275 #if FEATURE(BVH_HAIR)
276                                                         if(segment == ~0)
277 #endif
278                                                                 hit = bvh_triangle_intersect_subsurface(kg, isect, P, idir, object, primAddr, tmax, &num_hits, subsurface_random);
279
280                                                 }
281 #else
282                                                                 hit = bvh_triangle_intersect(kg, isect, P, idir, visibility, object, primAddr);
283
284                                                         /* shadow ray early termination */
285 #if defined(__KERNEL_SSE3__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
286                                                         if(hit) {
287                                                                 if(visibility == PATH_RAY_SHADOW_OPAQUE)
288                                                                         return true;
289
290                                                                 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
291                                                         }
292 #else
293                                                         if(hit && visibility == PATH_RAY_SHADOW_OPAQUE)
294                                                                 return true;
295 #endif
296
297 #endif
298
299                                                 primAddr++;
300                                         }
301                                 }
302 #if FEATURE(BVH_INSTANCING)
303                                 else {
304                                         /* instance push */
305 #if FEATURE(BVH_SUBSURFACE)
306                                         if(subsurface_object == kernel_tex_fetch(__prim_object, -primAddr-1)) {
307                                                 object = subsurface_object;
308 #else
309                                                 object = kernel_tex_fetch(__prim_object, -primAddr-1);
310 #endif
311
312 #if FEATURE(BVH_MOTION)
313                                                 bvh_instance_motion_push(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
314 #else
315                                                 bvh_instance_push(kg, object, ray, &P, &idir, &isect->t, tmax);
316 #endif
317
318 #if defined(__KERNEL_SSE3__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
319                                                 Psplat[0] = _mm_set_ps1(P.x);
320                                                 Psplat[1] = _mm_set_ps1(P.y);
321                                                 Psplat[2] = _mm_set_ps1(P.z);
322
323                                                 idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
324                                                 idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
325                                                 idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
326
327                                                 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
328
329                                                 shufflex = (idir.x >= 0)? shuffle_identity: shuffle_swap;
330                                                 shuffley = (idir.y >= 0)? shuffle_identity: shuffle_swap;
331                                                 shufflez = (idir.z >= 0)? shuffle_identity: shuffle_swap;
332 #endif
333
334                                                 ++stackPtr;
335                                                 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
336
337                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
338 #if FEATURE(BVH_SUBSURFACE)
339                                         }
340                                         else {
341                                                 /* pop */
342                                                 nodeAddr = traversalStack[stackPtr];
343                                                 --stackPtr;
344                                         }
345 #endif
346                                 }
347                         }
348 #endif
349                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
350
351 #if FEATURE(BVH_INSTANCING)
352                 if(stackPtr >= 0) {
353                         kernel_assert(object != ~0);
354
355                         /* instance pop */
356 #if FEATURE(BVH_MOTION)
357                         bvh_instance_motion_pop(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
358 #else
359                         bvh_instance_pop(kg, object, ray, &P, &idir, &isect->t, tmax);
360 #endif
361
362 #if defined(__KERNEL_SSE3__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
363                         Psplat[0] = _mm_set_ps1(P.x);
364                         Psplat[1] = _mm_set_ps1(P.y);
365                         Psplat[2] = _mm_set_ps1(P.z);
366
367                         idirsplat[0] = _mm_xor_ps(_mm_set_ps1(idir.x), _mm_castsi128_ps(pn));
368                         idirsplat[1] = _mm_xor_ps(_mm_set_ps1(idir.y), _mm_castsi128_ps(pn));
369                         idirsplat[2] = _mm_xor_ps(_mm_set_ps1(idir.z), _mm_castsi128_ps(pn));
370
371                         tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
372
373                         shufflex = (idir.x >= 0)? shuffle_identity: shuffle_swap;
374                         shuffley = (idir.y >= 0)? shuffle_identity: shuffle_swap;
375                         shufflez = (idir.z >= 0)? shuffle_identity: shuffle_swap;
376 #endif
377
378                         object = ~0;
379                         nodeAddr = traversalStack[stackPtr];
380                         --stackPtr;
381                 }
382 #endif
383         } while(nodeAddr != ENTRYPOINT_SENTINEL);
384
385 #if FEATURE(BVH_SUBSURFACE)
386         return (num_hits != 0);
387 #else
388         return (isect->prim != ~0);
389 #endif
390 }
391
392 #undef FEATURE
393 #undef BVH_FUNCTION_NAME
394 #undef BVH_FUNCTION_FEATURES
395