Cycles: Add _all suffix to shadow traversal file
[blender.git] / intern / cycles / kernel / bvh / qbvh_shadow_all.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, 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_MOTION: motion blur rendering
27  *
28  */
29
30 #if BVH_FEATURE(BVH_HAIR)
31 #  define NODE_INTERSECT qbvh_node_intersect
32 #else
33 #  define NODE_INTERSECT qbvh_aligned_node_intersect
34 #endif
35
36 ccl_device bool BVH_FUNCTION_FULL_NAME(QBVH)(KernelGlobals *kg,
37                                              const Ray *ray,
38                                              Intersection *isect_array,
39                                              const uint max_hits,
40                                              uint *num_hits)
41 {
42         /* TODO(sergey):
43          * - Likely and unlikely for if() statements.
44          * - Test restrict attribute for pointers.
45          */
46
47         /* Traversal stack in CUDA thread-local memory. */
48         QBVHStackItem traversalStack[BVH_QSTACK_SIZE];
49         traversalStack[0].addr = ENTRYPOINT_SENTINEL;
50
51         /* Traversal variables in registers. */
52         int stackPtr = 0;
53         int nodeAddr = kernel_data.bvh.root;
54
55         /* Ray parameters in registers. */
56         const float tmax = ray->t;
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 = tmax;
62
63 #if BVH_FEATURE(BVH_MOTION)
64         Transform ob_itfm;
65 #endif
66
67         *num_hits = 0;
68         isect_array->t = tmax;
69
70 #ifndef __KERNEL_SSE41__
71         if(!isfinite(P.x)) {
72                 return false;
73         }
74 #endif
75
76 #if BVH_FEATURE(BVH_INSTANCING)
77         int num_hits_in_instance = 0;
78 #endif
79
80         ssef tnear(0.0f), tfar(tmax);
81 #if BVH_FEATURE(BVH_HAIR)
82         sse3f dir4(ssef(dir.x), ssef(dir.y), ssef(dir.z));
83 #endif
84         sse3f idir4(ssef(idir.x), ssef(idir.y), ssef(idir.z));
85
86 #ifdef __KERNEL_AVX2__
87         float3 P_idir = P*idir;
88         sse3f P_idir4(P_idir.x, P_idir.y, P_idir.z);
89 #endif
90 #if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
91         sse3f org4(ssef(P.x), ssef(P.y), ssef(P.z));
92 #endif
93
94         /* Offsets to select the side that becomes the lower or upper bound. */
95         int near_x, near_y, near_z;
96         int far_x, far_y, far_z;
97
98         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
99         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
100         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
101
102         IsectPrecalc isect_precalc;
103         triangle_intersect_precalc(dir, &isect_precalc);
104
105         /* Traversal loop. */
106         do {
107                 do {
108                         /* Traverse internal nodes. */
109                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
110                                 float4 inodes = kernel_tex_fetch(__bvh_nodes, nodeAddr+0);
111
112 #ifdef __VISIBILITY_FLAG__
113                                 if((__float_as_uint(inodes.x) & PATH_RAY_SHADOW) == 0) {
114                                         /* Pop. */
115                                         nodeAddr = traversalStack[stackPtr].addr;
116                                         --stackPtr;
117                                         continue;
118                                 }
119 #endif
120
121                                 ssef dist;
122                                 int traverseChild = NODE_INTERSECT(kg,
123                                                                    tnear,
124                                                                    tfar,
125 #ifdef __KERNEL_AVX2__
126                                                                    P_idir4,
127 #endif
128 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
129                                                                    org4,
130 #  endif
131 #  if BVH_FEATURE(BVH_HAIR)
132                                                                    dir4,
133 #  endif
134                                                                    idir4,
135                                                                    near_x, near_y, near_z,
136                                                                    far_x, far_y, far_z,
137                                                                    nodeAddr,
138                                                                    &dist);
139
140                                 if(traverseChild != 0) {
141                                         float4 cnodes;
142 #if BVH_FEATURE(BVH_HAIR)
143                                         if(__float_as_uint(inodes.x) & PATH_RAY_NODE_UNALIGNED) {
144                                                 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr+13);
145                                         }
146                                         else
147 #endif
148                                         {
149                                                 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr+7);
150                                         }
151
152                                         /* One child is hit, continue with that child. */
153                                         int r = __bscf(traverseChild);
154                                         if(traverseChild == 0) {
155                                                 nodeAddr = __float_as_int(cnodes[r]);
156                                                 continue;
157                                         }
158
159                                         /* Two children are hit, push far child, and continue with
160                                          * closer child.
161                                          */
162                                         int c0 = __float_as_int(cnodes[r]);
163                                         float d0 = ((float*)&dist)[r];
164                                         r = __bscf(traverseChild);
165                                         int c1 = __float_as_int(cnodes[r]);
166                                         float d1 = ((float*)&dist)[r];
167                                         if(traverseChild == 0) {
168                                                 if(d1 < d0) {
169                                                         nodeAddr = c1;
170                                                         ++stackPtr;
171                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
172                                                         traversalStack[stackPtr].addr = c0;
173                                                         traversalStack[stackPtr].dist = d0;
174                                                         continue;
175                                                 }
176                                                 else {
177                                                         nodeAddr = c0;
178                                                         ++stackPtr;
179                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
180                                                         traversalStack[stackPtr].addr = c1;
181                                                         traversalStack[stackPtr].dist = d1;
182                                                         continue;
183                                                 }
184                                         }
185
186                                         /* Here starts the slow path for 3 or 4 hit children. We push
187                                          * all nodes onto the stack to sort them there.
188                                          */
189                                         ++stackPtr;
190                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
191                                         traversalStack[stackPtr].addr = c1;
192                                         traversalStack[stackPtr].dist = d1;
193                                         ++stackPtr;
194                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
195                                         traversalStack[stackPtr].addr = c0;
196                                         traversalStack[stackPtr].dist = d0;
197
198                                         /* Three children are hit, push all onto stack and sort 3
199                                          * stack items, continue with closest child.
200                                          */
201                                         r = __bscf(traverseChild);
202                                         int c2 = __float_as_int(cnodes[r]);
203                                         float d2 = ((float*)&dist)[r];
204                                         if(traverseChild == 0) {
205                                                 ++stackPtr;
206                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
207                                                 traversalStack[stackPtr].addr = c2;
208                                                 traversalStack[stackPtr].dist = d2;
209                                                 qbvh_stack_sort(&traversalStack[stackPtr],
210                                                                 &traversalStack[stackPtr - 1],
211                                                                 &traversalStack[stackPtr - 2]);
212                                                 nodeAddr = traversalStack[stackPtr].addr;
213                                                 --stackPtr;
214                                                 continue;
215                                         }
216
217                                         /* Four children are hit, push all onto stack and sort 4
218                                          * stack items, continue with closest child.
219                                          */
220                                         r = __bscf(traverseChild);
221                                         int c3 = __float_as_int(cnodes[r]);
222                                         float d3 = ((float*)&dist)[r];
223                                         ++stackPtr;
224                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
225                                         traversalStack[stackPtr].addr = c3;
226                                         traversalStack[stackPtr].dist = d3;
227                                         ++stackPtr;
228                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
229                                         traversalStack[stackPtr].addr = c2;
230                                         traversalStack[stackPtr].dist = d2;
231                                         qbvh_stack_sort(&traversalStack[stackPtr],
232                                                         &traversalStack[stackPtr - 1],
233                                                         &traversalStack[stackPtr - 2],
234                                                         &traversalStack[stackPtr - 3]);
235                                 }
236
237                                 nodeAddr = traversalStack[stackPtr].addr;
238                                 --stackPtr;
239                         }
240
241                         /* If node is leaf, fetch triangle list. */
242                         if(nodeAddr < 0) {
243                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1));
244 #ifdef __VISIBILITY_FLAG__
245                                 if((__float_as_uint(leaf.z) & PATH_RAY_SHADOW) == 0) {
246                                         /* Pop. */
247                                         nodeAddr = traversalStack[stackPtr].addr;
248                                         --stackPtr;
249                                         continue;
250                                 }
251 #endif
252
253                                 int primAddr = __float_as_int(leaf.x);
254
255 #if BVH_FEATURE(BVH_INSTANCING)
256                                 if(primAddr >= 0) {
257 #endif
258                                         int primAddr2 = __float_as_int(leaf.y);
259                                         const uint type = __float_as_int(leaf.w);
260                                         const uint p_type = type & PRIMITIVE_ALL;
261
262                                         /* Pop. */
263                                         nodeAddr = traversalStack[stackPtr].addr;
264                                         --stackPtr;
265
266                                         /* Primitive intersection. */
267                                         while(primAddr < primAddr2) {
268                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
269
270                                                 bool hit;
271
272                                                 /* todo: specialized intersect functions which don't fill in
273                                                  * isect unless needed and check SD_HAS_TRANSPARENT_SHADOW?
274                                                  * might give a few % performance improvement */
275
276                                                 switch(p_type) {
277                                                         case PRIMITIVE_TRIANGLE: {
278                                                                 hit = triangle_intersect(kg, &isect_precalc, isect_array, P, PATH_RAY_SHADOW, object, primAddr);
279                                                                 break;
280                                                         }
281 #if BVH_FEATURE(BVH_MOTION)
282                                                         case PRIMITIVE_MOTION_TRIANGLE: {
283                                                                 hit = motion_triangle_intersect(kg, isect_array, P, dir, ray->time, PATH_RAY_SHADOW, object, primAddr);
284                                                                 break;
285                                                         }
286 #endif
287 #if BVH_FEATURE(BVH_HAIR)
288                                                         case PRIMITIVE_CURVE:
289                                                         case PRIMITIVE_MOTION_CURVE: {
290                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE) 
291                                                                         hit = bvh_cardinal_curve_intersect(kg, isect_array, P, dir, PATH_RAY_SHADOW, object, primAddr, ray->time, type, NULL, 0, 0);
292                                                                 else
293                                                                         hit = bvh_curve_intersect(kg, isect_array, P, dir, PATH_RAY_SHADOW, object, primAddr, ray->time, type, NULL, 0, 0);
294                                                                 break;
295                                                         }
296 #endif
297                                                         default: {
298                                                                 hit = false;
299                                                                 break;
300                                                         }
301                                                 }
302
303                                                 /* Shadow ray early termination. */
304                                                 if(hit) {
305                                                         /* detect if this surface has a shader with transparent shadows */
306
307                                                         /* todo: optimize so primitive visibility flag indicates if
308                                                          * the primitive has a transparent shadow shader? */
309                                                         int prim = kernel_tex_fetch(__prim_index, isect_array->prim);
310                                                         int shader = 0;
311
312 #ifdef __HAIR__
313                                                         if(kernel_tex_fetch(__prim_type, isect_array->prim) & PRIMITIVE_ALL_TRIANGLE)
314 #endif
315                                                         {
316                                                                 shader = kernel_tex_fetch(__tri_shader, prim);
317                                                         }
318 #ifdef __HAIR__
319                                                         else {
320                                                                 float4 str = kernel_tex_fetch(__curves, prim);
321                                                                 shader = __float_as_int(str.z);
322                                                         }
323 #endif
324                                                         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
325
326                                                         /* if no transparent shadows, all light is blocked */
327                                                         if(!(flag & SD_HAS_TRANSPARENT_SHADOW)) {
328                                                                 return true;
329                                                         }
330                                                         /* if maximum number of hits reached, block all light */
331                                                         else if(*num_hits == max_hits) {
332                                                                 return true;
333                                                         }
334
335                                                         /* move on to next entry in intersections array */
336                                                         isect_array++;
337                                                         (*num_hits)++;
338 #if BVH_FEATURE(BVH_INSTANCING)
339                                                         num_hits_in_instance++;
340 #endif
341
342                                                         isect_array->t = isect_t;
343                                                 }
344
345                                                 primAddr++;
346                                         }
347                                 }
348 #if BVH_FEATURE(BVH_INSTANCING)
349                                 else {
350                                         /* Instance push. */
351                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
352
353 #  if BVH_FEATURE(BVH_MOTION)
354                                         bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
355 #  else
356                                         bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
357 #  endif
358
359                                         num_hits_in_instance = 0;
360                                         isect_array->t = isect_t;
361
362                                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
363                                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
364                                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
365                                         tfar = ssef(isect_t);
366 #  if BVH_FEATURE(BVH_HAIR)
367                                         dir4 = sse3f(ssef(dir.x), ssef(dir.y), ssef(dir.z));
368 #  endif
369                                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
370 #  ifdef __KERNEL_AVX2__
371                                         P_idir = P*idir;
372                                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
373 #  endif
374 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
375                                         org4 = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
376 #  endif
377
378                                         triangle_intersect_precalc(dir, &isect_precalc);
379
380                                         ++stackPtr;
381                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
382                                         traversalStack[stackPtr].addr = ENTRYPOINT_SENTINEL;
383
384                                         nodeAddr = kernel_tex_fetch(__object_node, object);
385
386                                 }
387                         }
388 #endif  /* FEATURE(BVH_INSTANCING) */
389                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
390
391 #if BVH_FEATURE(BVH_INSTANCING)
392                 if(stackPtr >= 0) {
393                         kernel_assert(object != OBJECT_NONE);
394
395                         if(num_hits_in_instance) {
396                                 float t_fac;
397
398 #  if BVH_FEATURE(BVH_MOTION)
399                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
400 #  else
401                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
402 #  endif
403
404                                 /* scale isect->t to adjust for instancing */
405                                 for(int i = 0; i < num_hits_in_instance; i++)
406                                         (isect_array-i-1)->t *= t_fac;
407                         }
408                         else {
409                                 float ignore_t = FLT_MAX;
410
411 #  if BVH_FEATURE(BVH_MOTION)
412                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &ignore_t, &ob_itfm);
413 #  else
414                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &ignore_t);
415 #  endif
416                         }
417
418                         isect_t = tmax;
419                         isect_array->t = isect_t;
420
421                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
422                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
423                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
424                         tfar = ssef(tmax);
425 #  if BVH_FEATURE(BVH_HAIR)
426                         dir4 = sse3f(ssef(dir.x), ssef(dir.y), ssef(dir.z));
427 #  endif
428                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
429 #  ifdef __KERNEL_AVX2__
430                         P_idir = P*idir;
431                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
432 #  endif
433 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
434                         org4 = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
435 #  endif
436
437                         triangle_intersect_precalc(dir, &isect_precalc);
438
439                         object = OBJECT_NONE;
440                         nodeAddr = traversalStack[stackPtr].addr;
441                         --stackPtr;
442                 }
443 #endif  /* FEATURE(BVH_INSTANCING) */
444         } while(nodeAddr != ENTRYPOINT_SENTINEL);
445
446         return false;
447 }
448
449 #undef NODE_INTERSECT