Cycles: change __device and similar qualifiers to ccl_device in kernel code.
[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_MOTION: motion blur rendering
28  *
29  */
30
31 #define FEATURE(f) (((BVH_FUNCTION_FEATURES) & (f)) != 0)
32
33 ccl_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
37 #endif
38 )
39 {
40         /* todo:
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
44          * - SSE for hair
45          * - test restrict attribute for pointers
46          */
47         
48         /* traversal stack in CUDA thread-local memory */
49         int traversalStack[BVH_STACK_SIZE];
50         traversalStack[0] = ENTRYPOINT_SENTINEL;
51
52         /* traversal variables in registers */
53         int stackPtr = 0;
54         int nodeAddr = kernel_data.bvh.root;
55
56         /* ray parameters in registers */
57         const float tmax = ray->t;
58         float3 P = ray->P;
59         float3 idir = bvh_inverse_direction(ray->D);
60         int object = ~0;
61
62 #if FEATURE(BVH_MOTION)
63         Transform ob_tfm;
64 #endif
65
66         isect->t = tmax;
67         isect->object = ~0;
68         isect->prim = ~0;
69         isect->u = 0.0f;
70         isect->v = 0.0f;
71
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();
75         
76         const __m128i pn = _mm_set_epi32(0x80000000, 0x80000000, 0x00000000, 0x00000000);
77         __m128 Psplat[3], idirsplat[3];
78
79         Psplat[0] = _mm_set_ps1(P.x);
80         Psplat[1] = _mm_set_ps1(P.y);
81         Psplat[2] = _mm_set_ps1(P.z);
82
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));
86
87         __m128 tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
88
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;
92 #endif
93
94         /* traversal loop */
95         do {
96                 do
97                 {
98                         /* traverse internal nodes */
99                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL)
100                         {
101                                 bool traverseChild0, traverseChild1;
102                                 int nodeAddrChild1;
103
104 #if !defined(__KERNEL_SSE2__) || FEATURE(BVH_HAIR_MINIMUM_WIDTH)
105                                 /* Intersect two child bounding boxes, non-SSE version */
106                                 float t = isect->t;
107
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);
113
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);
123
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);
132
133 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
134                                 if(difl != 0.0f) {
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);
140                                         }
141                                         if(__float_as_int(cnodes.w) & PATH_RAY_CURVE) {
142                                                 c1min = max(ldiff * c1min, c1min - extmax);
143                                                 c1max = min(hdiff * c1max, c1max + extmax);
144                                         }
145                                 }
146 #endif
147
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);
153 #else
154                                 traverseChild0 = (c0max >= c0min);
155                                 traverseChild1 = (c1max >= c1min);
156 #endif
157
158 #else // __KERNEL_SSE2__
159                                 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
160
161                                 /* fetch node data */
162                                 __m128 *bvh_nodes = (__m128*)kg->__bvh_nodes.data + nodeAddr*BVH_NODE_SIZE;
163                                 float4 cnodes = ((float4*)bvh_nodes)[3];
164
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]);
169
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));
172
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);
178 #else
179                                 traverseChild0 = (_mm_movemask_ps(lrhit) & 1);
180                                 traverseChild1 = (_mm_movemask_ps(lrhit) & 2);
181 #endif
182 #endif // __KERNEL_SSE2__
183
184                                 nodeAddr = __float_as_int(cnodes.x);
185                                 nodeAddrChild1 = __float_as_int(cnodes.y);
186
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);
191 #else
192                                         union { __m128 m128; float v[4]; } uminmax;
193                                         uminmax.m128 = tminmax;
194                                         bool closestChild1 = uminmax.v[1] < uminmax.v[0];
195 #endif
196
197                                         if(closestChild1) {
198                                                 int tmp = nodeAddr;
199                                                 nodeAddr = nodeAddrChild1;
200                                                 nodeAddrChild1 = tmp;
201                                         }
202
203                                         ++stackPtr;
204                                         traversalStack[stackPtr] = nodeAddrChild1;
205                                 }
206                                 else {
207                                         /* one child was intersected */
208                                         if(traverseChild1) {
209                                                 nodeAddr = nodeAddrChild1;
210                                         }
211                                         else if(!traverseChild0) {
212                                                 /* neither child was intersected */
213                                                 nodeAddr = traversalStack[stackPtr];
214                                                 --stackPtr;
215                                         }
216                                 }
217                         }
218
219                         /* if node is leaf, fetch triangle list */
220                         if(nodeAddr < 0) {
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);
223
224 #if FEATURE(BVH_INSTANCING)
225                                 if(primAddr >= 0) {
226 #endif
227                                         int primAddr2 = __float_as_int(leaf.y);
228
229                                         /* pop */
230                                         nodeAddr = traversalStack[stackPtr];
231                                         --stackPtr;
232
233                                         /* primitive intersection */
234                                         while(primAddr < primAddr2) {
235                                                 bool hit;
236
237                                                 /* intersect ray against primitive */
238 #if FEATURE(BVH_HAIR)
239                                                 uint segment = kernel_tex_fetch(__prim_segment, primAddr);
240                                                 if(segment != ~0) {
241
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);
245                                                         else
246                                                                 hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
247 #else
248                                                                 hit = bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
249                                                         else
250                                                                 hit = bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
251 #endif
252                                                 }
253                                                 else
254 #endif
255                                                         hit = bvh_triangle_intersect(kg, isect, P, idir, visibility, object, primAddr);
256
257                                                 /* shadow ray early termination */
258 #if defined(__KERNEL_SSE2__) && !FEATURE(BVH_HAIR_MINIMUM_WIDTH)
259                                                 if(hit) {
260                                                         if(visibility == PATH_RAY_SHADOW_OPAQUE)
261                                                                 return true;
262
263                                                         tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
264                                                 }
265 #else
266                                                 if(hit && visibility == PATH_RAY_SHADOW_OPAQUE)
267                                                         return true;
268 #endif
269
270                                                 primAddr++;
271                                         }
272                                 }
273 #if FEATURE(BVH_INSTANCING)
274                                 else {
275                                         /* instance push */
276                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
277
278 #if FEATURE(BVH_MOTION)
279                                         bvh_instance_motion_push(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
280 #else
281                                         bvh_instance_push(kg, object, ray, &P, &idir, &isect->t, tmax);
282 #endif
283
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);
288
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));
292
293                                         tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
294
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;
298 #endif
299
300                                         ++stackPtr;
301                                         traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
302
303                                         nodeAddr = kernel_tex_fetch(__object_node, object);
304                                 }
305                         }
306 #endif
307                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
308
309 #if FEATURE(BVH_INSTANCING)
310                 if(stackPtr >= 0) {
311                         kernel_assert(object != ~0);
312
313                         /* instance pop */
314 #if FEATURE(BVH_MOTION)
315                         bvh_instance_motion_pop(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
316 #else
317                         bvh_instance_pop(kg, object, ray, &P, &idir, &isect->t, tmax);
318 #endif
319
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);
324
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));
328
329                         tsplat = _mm_set_ps(-isect->t, -isect->t, 0.0f, 0.0f);
330
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;
334 #endif
335
336                         object = ~0;
337                         nodeAddr = traversalStack[stackPtr];
338                         --stackPtr;
339                 }
340 #endif
341         } while(nodeAddr != ENTRYPOINT_SENTINEL);
342
343         return (isect->prim != ~0);
344 }
345
346 #undef FEATURE
347 #undef BVH_FUNCTION_NAME
348 #undef BVH_FUNCTION_FEATURES
349