Cysles: Avoid having ShaderData on the stack
[blender.git] / intern / cycles / kernel / geom / geom_bvh_subsurface.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 #ifdef __QBVH__
21 #include "geom_qbvh_subsurface.h"
22 #endif
23
24 /* This is a template BVH traversal function for subsurface scattering, 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.
27  *
28  * BVH_INSTANCING: object instancing
29  * BVH_MOTION: motion blur rendering
30  *
31  */
32
33 ccl_device void BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
34                                             const Ray *ray,
35                                             SubsurfaceIntersection *ss_isect,
36                                             int subsurface_object,
37                                             uint *lcg_state,
38                                             int max_hits)
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         float3 P = ray->P;
58         float3 dir = bvh_clamp_direction(ray->D);
59         float3 idir = bvh_inverse_direction(dir);
60         int object = OBJECT_NONE;
61         float isect_t = ray->t;
62
63         ss_isect->num_hits = 0;
64
65 #if BVH_FEATURE(BVH_MOTION)
66         Transform ob_itfm;
67 #endif
68
69 #if defined(__KERNEL_SSE2__)
70         const shuffle_swap_t shuf_identity = shuffle_swap_identity();
71         const shuffle_swap_t shuf_swap = shuffle_swap_swap();
72         
73         const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
74         ssef Psplat[3], idirsplat[3];
75         shuffle_swap_t shufflexyz[3];
76
77         Psplat[0] = ssef(P.x);
78         Psplat[1] = ssef(P.y);
79         Psplat[2] = ssef(P.z);
80
81         ssef tsplat(0.0f, 0.0f, -isect_t, -isect_t);
82
83         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
84 #endif
85
86         IsectPrecalc isect_precalc;
87         triangle_intersect_precalc(dir, &isect_precalc);
88
89         /* traversal loop */
90         do {
91                 do
92                 {
93                         /* traverse internal nodes */
94                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL)
95                         {
96                                 bool traverseChild0, traverseChild1;
97                                 int nodeAddrChild1;
98
99 #if !defined(__KERNEL_SSE2__)
100                                 /* Intersect two child bounding boxes, non-SSE version */
101                                 float t = isect_t;
102
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);
108
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);
118
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);
127
128                                 /* decide which nodes to traverse next */
129                                 traverseChild0 = (c0max >= c0min);
130                                 traverseChild1 = (c1max >= c1min);
131
132 #else // __KERNEL_SSE2__
133                                 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
134
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];
138
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];
143
144                                 /* calculate { c0min, c1min, -c0max, -c1max} */
145                                 const ssef minmax = max(max(tminmaxx, tminmaxy), max(tminmaxz, tsplat));
146                                 const ssef tminmax = minmax ^ pn;
147                                 const sseb lrhit = tminmax <= shuffle<2, 3, 0, 1>(tminmax);
148
149                                 /* decide which nodes to traverse next */
150                                 traverseChild0 = (movemask(lrhit) & 1);
151                                 traverseChild1 = (movemask(lrhit) & 2);
152 #endif // __KERNEL_SSE2__
153
154                                 nodeAddr = __float_as_int(cnodes.x);
155                                 nodeAddrChild1 = __float_as_int(cnodes.y);
156
157                                 if(traverseChild0 && traverseChild1) {
158                                         /* both children were intersected, push the farther one */
159 #if !defined(__KERNEL_SSE2__)
160                                         bool closestChild1 = (c1min < c0min);
161 #else
162                                         bool closestChild1 = tminmax[1] < tminmax[0];
163 #endif
164
165                                         if(closestChild1) {
166                                                 int tmp = nodeAddr;
167                                                 nodeAddr = nodeAddrChild1;
168                                                 nodeAddrChild1 = tmp;
169                                         }
170
171                                         ++stackPtr;
172                                         kernel_assert(stackPtr < BVH_STACK_SIZE);
173                                         traversalStack[stackPtr] = nodeAddrChild1;
174                                 }
175                                 else {
176                                         /* one child was intersected */
177                                         if(traverseChild1) {
178                                                 nodeAddr = nodeAddrChild1;
179                                         }
180                                         else if(!traverseChild0) {
181                                                 /* neither child was intersected */
182                                                 nodeAddr = traversalStack[stackPtr];
183                                                 --stackPtr;
184                                         }
185                                 }
186                         }
187
188                         /* if node is leaf, fetch triangle list */
189                         if(nodeAddr < 0) {
190                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_NODE_LEAF_SIZE);
191                                 int primAddr = __float_as_int(leaf.x);
192
193 #if BVH_FEATURE(BVH_INSTANCING)
194                                 if(primAddr >= 0) {
195 #endif
196                                         const int primAddr2 = __float_as_int(leaf.y);
197                                         const uint type = __float_as_int(leaf.w);
198
199                                         /* pop */
200                                         nodeAddr = traversalStack[stackPtr];
201                                         --stackPtr;
202
203                                         /* primitive intersection */
204                                         switch(type & PRIMITIVE_ALL) {
205                                                 case PRIMITIVE_TRIANGLE: {
206                                                         /* intersect ray against primitive */
207                                                         for(; primAddr < primAddr2; primAddr++) {
208                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
209                                                                 /* only primitives from the same object */
210                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
211                                                                 if(tri_object != subsurface_object)
212                                                                         continue;
213                                                                 triangle_intersect_subsurface(kg,
214                                                                                               &isect_precalc,
215                                                                                               ss_isect,
216                                                                                               P,
217                                                                                               object,
218                                                                                               primAddr,
219                                                                                               isect_t,
220                                                                                               lcg_state,
221                                                                                               max_hits);
222                                                         }
223                                                         break;
224                                                 }
225 #if BVH_FEATURE(BVH_MOTION)
226                                                 case PRIMITIVE_MOTION_TRIANGLE: {
227                                                         /* intersect ray against primitive */
228                                                         for(; primAddr < primAddr2; primAddr++) {
229                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
230                                                                 /* only primitives from the same object */
231                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
232                                                                 if(tri_object != subsurface_object)
233                                                                         continue;
234                                                                 motion_triangle_intersect_subsurface(kg,
235                                                                                                      ss_isect,
236                                                                                                      P,
237                                                                                                      dir,
238                                                                                                      ray->time,
239                                                                                                      object,
240                                                                                                      primAddr,
241                                                                                                      isect_t,
242                                                                                                      lcg_state,
243                                                                                                      max_hits);
244                                                         }
245                                                         break;
246                                                 }
247 #endif
248                                                 default: {
249                                                         break;
250                                                 }
251                                         }
252                                 }
253 #if BVH_FEATURE(BVH_INSTANCING)
254                                 else {
255                                         /* instance push */
256                                         if(subsurface_object == kernel_tex_fetch(__prim_object, -primAddr-1)) {
257                                                 object = subsurface_object;
258
259 #if BVH_FEATURE(BVH_MOTION)
260                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
261 #else
262                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
263 #endif
264                                                 triangle_intersect_precalc(dir, &isect_precalc);
265
266 #if defined(__KERNEL_SSE2__)
267                                                 Psplat[0] = ssef(P.x);
268                                                 Psplat[1] = ssef(P.y);
269                                                 Psplat[2] = ssef(P.z);
270
271                                                 tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
272
273                                                 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
274 #endif
275
276                                                 ++stackPtr;
277                                                 kernel_assert(stackPtr < BVH_STACK_SIZE);
278                                                 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
279
280                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
281                                         }
282                                         else {
283                                                 /* pop */
284                                                 nodeAddr = traversalStack[stackPtr];
285                                                 --stackPtr;
286                                         }
287                                 }
288                         }
289 #endif  /* FEATURE(BVH_INSTANCING) */
290                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
291
292 #if BVH_FEATURE(BVH_INSTANCING)
293                 if(stackPtr >= 0) {
294                         kernel_assert(object != OBJECT_NONE);
295
296                         /* instance pop */
297 #if BVH_FEATURE(BVH_MOTION)
298                         bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
299 #else
300                         bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &isect_t);
301 #endif
302
303                         triangle_intersect_precalc(dir, &isect_precalc);
304
305 #if defined(__KERNEL_SSE2__)
306                         Psplat[0] = ssef(P.x);
307                         Psplat[1] = ssef(P.y);
308                         Psplat[2] = ssef(P.z);
309
310                         tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
311
312                         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
313 #endif
314
315                         object = OBJECT_NONE;
316                         nodeAddr = traversalStack[stackPtr];
317                         --stackPtr;
318                 }
319 #endif  /* FEATURE(BVH_INSTANCING) */
320         } while(nodeAddr != ENTRYPOINT_SENTINEL);
321 }
322
323 ccl_device_inline void BVH_FUNCTION_NAME(KernelGlobals *kg,
324                                          const Ray *ray,
325                                          SubsurfaceIntersection *ss_isect,
326                                          int subsurface_object,
327                                          uint *lcg_state,
328                                          int max_hits)
329 {
330 #ifdef __QBVH__
331         if(kernel_data.bvh.use_qbvh) {
332                 return BVH_FUNCTION_FULL_NAME(QBVH)(kg,
333                                                     ray,
334                                                     ss_isect,
335                                                     subsurface_object,
336                                                     lcg_state,
337                                                     max_hits);
338         }
339         else
340 #endif
341         {
342                 kernel_assert(kernel_data.bvh.use_qbvh == false);
343                 return BVH_FUNCTION_FULL_NAME(BVH)(kg,
344                                                    ray,
345                                                    ss_isect,
346                                                    subsurface_object,
347                                                    lcg_state,
348                                                    max_hits);
349         }
350 }
351
352 #undef BVH_FUNCTION_NAME
353 #undef BVH_FUNCTION_FEATURES