Cysles: Avoid having ShaderData on the stack
[blender.git] / intern / cycles / kernel / geom / geom_qbvh_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-2014, 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 for subsurface scattering, where
21  * various features can be enabled/disabled. This way we can compile optimized
22  * versions for each case without new features slowing things down.
23  *
24  * BVH_INSTANCING: object instancing
25  * BVH_MOTION: motion blur rendering
26  *
27  */
28
29 ccl_device void BVH_FUNCTION_FULL_NAME(QBVH)(KernelGlobals *kg,
30                                              const Ray *ray,
31                                              SubsurfaceIntersection *ss_isect,
32                                              int subsurface_object,
33                                              uint *lcg_state,
34                                              int max_hits)
35 {
36         /* TODO(sergey):
37          * - Test if pushing distance on the stack helps (for non shadow rays).
38          * - Separate version for shadow rays.
39          * - Likely and unlikely for if() statements.
40          * - SSE for hair.
41          * - Test restrict attribute for pointers.
42          */
43
44         /* Traversal stack in CUDA thread-local memory. */
45         QBVHStackItem traversalStack[BVH_QSTACK_SIZE];
46         traversalStack[0].addr = ENTRYPOINT_SENTINEL;
47
48         /* Traversal variables in registers. */
49         int stackPtr = 0;
50         int nodeAddr = kernel_data.bvh.root;
51
52         /* Ray parameters in registers. */
53         float3 P = ray->P;
54         float3 dir = bvh_clamp_direction(ray->D);
55         float3 idir = bvh_inverse_direction(dir);
56         int object = OBJECT_NONE;
57         float isect_t = ray->t;
58
59         ss_isect->num_hits = 0;
60
61 #if BVH_FEATURE(BVH_MOTION)
62         Transform ob_itfm;
63 #endif
64
65 #ifndef __KERNEL_SSE41__
66         if(!isfinite(P.x)) {
67                 return;
68         }
69 #endif
70
71         ssef tnear(0.0f), tfar(isect_t);
72         sse3f idir4(ssef(idir.x), ssef(idir.y), ssef(idir.z));
73
74 #ifdef __KERNEL_AVX2__
75         float3 P_idir = P*idir;
76         sse3f P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
77 #else
78         sse3f org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
79 #endif
80
81         /* Offsets to select the side that becomes the lower or upper bound. */
82         int near_x, near_y, near_z;
83         int far_x, far_y, far_z;
84
85         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
86         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
87         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
88
89         IsectPrecalc isect_precalc;
90         triangle_intersect_precalc(dir, &isect_precalc);
91
92         /* Traversal loop. */
93         do {
94                 do {
95                         /* Traverse internal nodes. */
96                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
97                                 ssef dist;
98                                 int traverseChild = qbvh_node_intersect(kg,
99                                                                         tnear,
100                                                                         tfar,
101 #ifdef __KERNEL_AVX2__
102                                                                         P_idir4,
103 #else
104                                                                         org,
105 #endif
106                                                                         idir4,
107                                                                         near_x, near_y, near_z,
108                                                                         far_x, far_y, far_z,
109                                                                         nodeAddr,
110                                                                         &dist);
111
112                                 if(traverseChild != 0) {
113                                         float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_QNODE_SIZE+6);
114
115                                         /* One child is hit, continue with that child. */
116                                         int r = __bscf(traverseChild);
117                                         if(traverseChild == 0) {
118                                                 nodeAddr = __float_as_int(cnodes[r]);
119                                                 continue;
120                                         }
121
122                                         /* Two children are hit, push far child, and continue with
123                                          * closer child.
124                                          */
125                                         int c0 = __float_as_int(cnodes[r]);
126                                         float d0 = ((float*)&dist)[r];
127                                         r = __bscf(traverseChild);
128                                         int c1 = __float_as_int(cnodes[r]);
129                                         float d1 = ((float*)&dist)[r];
130                                         if(traverseChild == 0) {
131                                                 if(d1 < d0) {
132                                                         nodeAddr = c1;
133                                                         ++stackPtr;
134                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
135                                                         traversalStack[stackPtr].addr = c0;
136                                                         traversalStack[stackPtr].dist = d0;
137                                                         continue;
138                                                 }
139                                                 else {
140                                                         nodeAddr = c0;
141                                                         ++stackPtr;
142                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
143                                                         traversalStack[stackPtr].addr = c1;
144                                                         traversalStack[stackPtr].dist = d1;
145                                                         continue;
146                                                 }
147                                         }
148
149                                         /* Here starts the slow path for 3 or 4 hit children. We push
150                                          * all nodes onto the stack to sort them there.
151                                          */
152                                         ++stackPtr;
153                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
154                                         traversalStack[stackPtr].addr = c1;
155                                         traversalStack[stackPtr].dist = d1;
156                                         ++stackPtr;
157                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
158                                         traversalStack[stackPtr].addr = c0;
159                                         traversalStack[stackPtr].dist = d0;
160
161                                         /* Three children are hit, push all onto stack and sort 3
162                                          * stack items, continue with closest child.
163                                          */
164                                         r = __bscf(traverseChild);
165                                         int c2 = __float_as_int(cnodes[r]);
166                                         float d2 = ((float*)&dist)[r];
167                                         if(traverseChild == 0) {
168                                                 ++stackPtr;
169                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
170                                                 traversalStack[stackPtr].addr = c2;
171                                                 traversalStack[stackPtr].dist = d2;
172                                                 qbvh_stack_sort(&traversalStack[stackPtr],
173                                                                 &traversalStack[stackPtr - 1],
174                                                                 &traversalStack[stackPtr - 2]);
175                                                 nodeAddr = traversalStack[stackPtr].addr;
176                                                 --stackPtr;
177                                                 continue;
178                                         }
179
180                                         /* Four children are hit, push all onto stack and sort 4
181                                          * stack items, continue with closest child.
182                                          */
183                                         r = __bscf(traverseChild);
184                                         int c3 = __float_as_int(cnodes[r]);
185                                         float d3 = ((float*)&dist)[r];
186                                         ++stackPtr;
187                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
188                                         traversalStack[stackPtr].addr = c3;
189                                         traversalStack[stackPtr].dist = d3;
190                                         ++stackPtr;
191                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
192                                         traversalStack[stackPtr].addr = c2;
193                                         traversalStack[stackPtr].dist = d2;
194                                         qbvh_stack_sort(&traversalStack[stackPtr],
195                                                         &traversalStack[stackPtr - 1],
196                                                         &traversalStack[stackPtr - 2],
197                                                         &traversalStack[stackPtr - 3]);
198                                 }
199
200                                 nodeAddr = traversalStack[stackPtr].addr;
201                                 --stackPtr;
202                         }
203
204                         /* If node is leaf, fetch triangle list. */
205                         if(nodeAddr < 0) {
206                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_QNODE_LEAF_SIZE);
207                                 int primAddr = __float_as_int(leaf.x);
208
209 #if BVH_FEATURE(BVH_INSTANCING)
210                                 if(primAddr >= 0) {
211 #endif
212                                         int primAddr2 = __float_as_int(leaf.y);
213                                         const uint type = __float_as_int(leaf.w);
214
215                                         /* Pop. */
216                                         nodeAddr = traversalStack[stackPtr].addr;
217                                         --stackPtr;
218
219                                         /* Primitive intersection. */
220                                         switch(type & PRIMITIVE_ALL) {
221                                                 case PRIMITIVE_TRIANGLE: {
222                                                         /* Intersect ray against primitive, */
223                                                         for(; primAddr < primAddr2; primAddr++) {
224                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
225                                                                 /* Only primitives from the same object. */
226                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
227                                                                 if(tri_object != subsurface_object) {
228                                                                         continue;
229                                                                 }
230                                                                 triangle_intersect_subsurface(kg,
231                                                                                               &isect_precalc,
232                                                                                               ss_isect,
233                                                                                               P,
234                                                                                               object,
235                                                                                               primAddr,
236                                                                                               isect_t,
237                                                                                               lcg_state,
238                                                                                               max_hits);
239                                                         }
240                                                         break;
241                                                 }
242 #if BVH_FEATURE(BVH_MOTION)
243                                                 case PRIMITIVE_MOTION_TRIANGLE: {
244                                                         /* Intersect ray against primitive. */
245                                                         for(; primAddr < primAddr2; primAddr++) {
246                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
247                                                                 /* Only primitives from the same object. */
248                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
249                                                                 if(tri_object != subsurface_object) {
250                                                                         continue;
251                                                                 }
252                                                                 motion_triangle_intersect_subsurface(kg,
253                                                                                                      ss_isect,
254                                                                                                      P,
255                                                                                                      dir,
256                                                                                                      ray->time,
257                                                                                                      object,
258                                                                                                      primAddr,
259                                                                                                      isect_t,
260                                                                                                      lcg_state,
261                                                                                                      max_hits);
262                                                         }
263                                                         break;
264                                                 }
265 #endif
266                                                 default:
267                                                         break;
268                                         }
269                                 }
270 #if BVH_FEATURE(BVH_INSTANCING)
271                                 else {
272                                         /* Instance push. */
273                                         if(subsurface_object == kernel_tex_fetch(__prim_object, -primAddr-1)) {
274                                                 object = subsurface_object;
275
276 #if BVH_FEATURE(BVH_MOTION)
277                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
278 #else
279                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
280 #endif
281
282                                                 if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
283                                                 if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
284                                                 if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
285                                                 tfar = ssef(isect_t);
286                                                 idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
287 #ifdef __KERNEL_AVX2__
288                                                 P_idir = P*idir;
289                                                 P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
290 #else
291                                                 org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
292 #endif
293                                                 triangle_intersect_precalc(dir, &isect_precalc);
294
295                                                 ++stackPtr;
296                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
297                                                 traversalStack[stackPtr].addr = ENTRYPOINT_SENTINEL;
298
299                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
300                                         }
301                                         else {
302                                                 /* Pop. */
303                                                 nodeAddr = traversalStack[stackPtr].addr;
304                                                 --stackPtr;
305                                         }
306
307                                 }
308                         }
309 #endif  /* FEATURE(BVH_INSTANCING) */
310                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
311
312 #if BVH_FEATURE(BVH_INSTANCING)
313                 if(stackPtr >= 0) {
314                         kernel_assert(object != OBJECT_NONE);
315
316                         /* Instance pop. */
317 #if BVH_FEATURE(BVH_MOTION)
318                         bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
319 #else
320                         bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &isect_t);
321 #endif
322
323                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
324                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
325                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
326                         tfar = ssef(isect_t);
327                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
328 #ifdef __KERNEL_AVX2__
329                         P_idir = P*idir;
330                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
331 #else
332                         org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
333 #endif
334                         triangle_intersect_precalc(dir, &isect_precalc);
335
336                         object = OBJECT_NONE;
337                         nodeAddr = traversalStack[stackPtr].addr;
338                         --stackPtr;
339                 }
340 #endif  /* FEATURE(BVH_INSTANCING) */
341         } while(nodeAddr != ENTRYPOINT_SENTINEL);
342 }