Merge branch 'master' into blender2.8
[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 traversal_stack[BVH_QSTACK_SIZE];
49         traversal_stack[0].addr = ENTRYPOINT_SENTINEL;
50
51         /* Traversal variables in registers. */
52         int stack_ptr = 0;
53         int node_addr = 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(node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
110                                 float4 inodes = kernel_tex_fetch(__bvh_nodes, node_addr+0);
111
112 #ifdef __VISIBILITY_FLAG__
113                                 if((__float_as_uint(inodes.x) & PATH_RAY_SHADOW) == 0) {
114                                         /* Pop. */
115                                         node_addr = traversal_stack[stack_ptr].addr;
116                                         --stack_ptr;
117                                         continue;
118                                 }
119 #endif
120
121                                 ssef dist;
122                                 int child_mask = 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                                                                 node_addr,
138                                                                 &dist);
139
140                                 if(child_mask != 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, node_addr+13);
145                                         }
146                                         else
147 #endif
148                                         {
149                                                 cnodes = kernel_tex_fetch(__bvh_nodes, node_addr+7);
150                                         }
151
152                                         /* One child is hit, continue with that child. */
153                                         int r = __bscf(child_mask);
154                                         if(child_mask == 0) {
155                                                 node_addr = __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(child_mask);
165                                         int c1 = __float_as_int(cnodes[r]);
166                                         float d1 = ((float*)&dist)[r];
167                                         if(child_mask == 0) {
168                                                 if(d1 < d0) {
169                                                         node_addr = c1;
170                                                         ++stack_ptr;
171                                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
172                                                         traversal_stack[stack_ptr].addr = c0;
173                                                         traversal_stack[stack_ptr].dist = d0;
174                                                         continue;
175                                                 }
176                                                 else {
177                                                         node_addr = c0;
178                                                         ++stack_ptr;
179                                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
180                                                         traversal_stack[stack_ptr].addr = c1;
181                                                         traversal_stack[stack_ptr].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                                         ++stack_ptr;
190                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
191                                         traversal_stack[stack_ptr].addr = c1;
192                                         traversal_stack[stack_ptr].dist = d1;
193                                         ++stack_ptr;
194                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
195                                         traversal_stack[stack_ptr].addr = c0;
196                                         traversal_stack[stack_ptr].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(child_mask);
202                                         int c2 = __float_as_int(cnodes[r]);
203                                         float d2 = ((float*)&dist)[r];
204                                         if(child_mask == 0) {
205                                                 ++stack_ptr;
206                                                 kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
207                                                 traversal_stack[stack_ptr].addr = c2;
208                                                 traversal_stack[stack_ptr].dist = d2;
209                                                 qbvh_stack_sort(&traversal_stack[stack_ptr],
210                                                                 &traversal_stack[stack_ptr - 1],
211                                                                 &traversal_stack[stack_ptr - 2]);
212                                                 node_addr = traversal_stack[stack_ptr].addr;
213                                                 --stack_ptr;
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(child_mask);
221                                         int c3 = __float_as_int(cnodes[r]);
222                                         float d3 = ((float*)&dist)[r];
223                                         ++stack_ptr;
224                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
225                                         traversal_stack[stack_ptr].addr = c3;
226                                         traversal_stack[stack_ptr].dist = d3;
227                                         ++stack_ptr;
228                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
229                                         traversal_stack[stack_ptr].addr = c2;
230                                         traversal_stack[stack_ptr].dist = d2;
231                                         qbvh_stack_sort(&traversal_stack[stack_ptr],
232                                                         &traversal_stack[stack_ptr - 1],
233                                                         &traversal_stack[stack_ptr - 2],
234                                                         &traversal_stack[stack_ptr - 3]);
235                                 }
236
237                                 node_addr = traversal_stack[stack_ptr].addr;
238                                 --stack_ptr;
239                         }
240
241                         /* If node is leaf, fetch triangle list. */
242                         if(node_addr < 0) {
243                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-node_addr-1));
244 #ifdef __VISIBILITY_FLAG__
245                                 if((__float_as_uint(leaf.z) & PATH_RAY_SHADOW) == 0) {
246                                         /* Pop. */
247                                         node_addr = traversal_stack[stack_ptr].addr;
248                                         --stack_ptr;
249                                         continue;
250                                 }
251 #endif
252
253                                 int prim_addr = __float_as_int(leaf.x);
254
255 #if BVH_FEATURE(BVH_INSTANCING)
256                                 if(prim_addr >= 0) {
257 #endif
258                                         int prim_addr2 = __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                                         node_addr = traversal_stack[stack_ptr].addr;
264                                         --stack_ptr;
265
266                                         /* Primitive intersection. */
267                                         while(prim_addr < prim_addr2) {
268                                                 kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == 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,
279                                                                                          &isect_precalc,
280                                                                                          isect_array,
281                                                                                          P,
282                                                                                          PATH_RAY_SHADOW,
283                                                                                          object,
284                                                                                          prim_addr);
285                                                                 break;
286                                                         }
287 #if BVH_FEATURE(BVH_MOTION)
288                                                         case PRIMITIVE_MOTION_TRIANGLE: {
289                                                                 hit = motion_triangle_intersect(kg,
290                                                                                                 isect_array,
291                                                                                                 P,
292                                                                                                 dir,
293                                                                                                 ray->time,
294                                                                                                 PATH_RAY_SHADOW,
295                                                                                                 object,
296                                                                                                 prim_addr);
297                                                                 break;
298                                                         }
299 #endif
300 #if BVH_FEATURE(BVH_HAIR)
301                                                         case PRIMITIVE_CURVE:
302                                                         case PRIMITIVE_MOTION_CURVE: {
303                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE) {
304                                                                         hit = bvh_cardinal_curve_intersect(kg,
305                                                                                                            isect_array,
306                                                                                                            P,
307                                                                                                            dir,
308                                                                                                            PATH_RAY_SHADOW,
309                                                                                                            object,
310                                                                                                            prim_addr,
311                                                                                                            ray->time,
312                                                                                                            type,
313                                                                                                            NULL,
314                                                                                                            0, 0);
315                                                                 }
316                                                                 else {
317                                                                         hit = bvh_curve_intersect(kg,
318                                                                                                   isect_array,
319                                                                                                   P,
320                                                                                                   dir,
321                                                                                                   PATH_RAY_SHADOW,
322                                                                                                   object,
323                                                                                                   prim_addr,
324                                                                                                   ray->time,
325                                                                                                   type,
326                                                                                                   NULL,
327                                                                                                   0, 0);
328                                                                 }
329                                                                 break;
330                                                         }
331 #endif
332                                                         default: {
333                                                                 hit = false;
334                                                                 break;
335                                                         }
336                                                 }
337
338                                                 /* Shadow ray early termination. */
339                                                 if(hit) {
340                                                         /* detect if this surface has a shader with transparent shadows */
341
342                                                         /* todo: optimize so primitive visibility flag indicates if
343                                                          * the primitive has a transparent shadow shader? */
344                                                         int prim = kernel_tex_fetch(__prim_index, isect_array->prim);
345                                                         int shader = 0;
346
347 #ifdef __HAIR__
348                                                         if(kernel_tex_fetch(__prim_type, isect_array->prim) & PRIMITIVE_ALL_TRIANGLE)
349 #endif
350                                                         {
351                                                                 shader = kernel_tex_fetch(__tri_shader, prim);
352                                                         }
353 #ifdef __HAIR__
354                                                         else {
355                                                                 float4 str = kernel_tex_fetch(__curves, prim);
356                                                                 shader = __float_as_int(str.z);
357                                                         }
358 #endif
359                                                         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*SHADER_SIZE);
360
361                                                         /* if no transparent shadows, all light is blocked */
362                                                         if(!(flag & SD_HAS_TRANSPARENT_SHADOW)) {
363                                                                 return true;
364                                                         }
365                                                         /* if maximum number of hits reached, block all light */
366                                                         else if(*num_hits == max_hits) {
367                                                                 return true;
368                                                         }
369
370                                                         /* move on to next entry in intersections array */
371                                                         isect_array++;
372                                                         (*num_hits)++;
373 #if BVH_FEATURE(BVH_INSTANCING)
374                                                         num_hits_in_instance++;
375 #endif
376
377                                                         isect_array->t = isect_t;
378                                                 }
379
380                                                 prim_addr++;
381                                         }
382                                 }
383 #if BVH_FEATURE(BVH_INSTANCING)
384                                 else {
385                                         /* Instance push. */
386                                         object = kernel_tex_fetch(__prim_object, -prim_addr-1);
387
388 #  if BVH_FEATURE(BVH_MOTION)
389                                         bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
390 #  else
391                                         bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
392 #  endif
393
394                                         num_hits_in_instance = 0;
395                                         isect_array->t = isect_t;
396
397                                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
398                                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
399                                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
400                                         tfar = ssef(isect_t);
401 #  if BVH_FEATURE(BVH_HAIR)
402                                         dir4 = sse3f(ssef(dir.x), ssef(dir.y), ssef(dir.z));
403 #  endif
404                                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
405 #  ifdef __KERNEL_AVX2__
406                                         P_idir = P*idir;
407                                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
408 #  endif
409 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
410                                         org4 = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
411 #  endif
412
413                                         triangle_intersect_precalc(dir, &isect_precalc);
414
415                                         ++stack_ptr;
416                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
417                                         traversal_stack[stack_ptr].addr = ENTRYPOINT_SENTINEL;
418
419                                         node_addr = kernel_tex_fetch(__object_node, object);
420
421                                 }
422                         }
423 #endif  /* FEATURE(BVH_INSTANCING) */
424                 } while(node_addr != ENTRYPOINT_SENTINEL);
425
426 #if BVH_FEATURE(BVH_INSTANCING)
427                 if(stack_ptr >= 0) {
428                         kernel_assert(object != OBJECT_NONE);
429
430                         if(num_hits_in_instance) {
431                                 float t_fac;
432
433 #  if BVH_FEATURE(BVH_MOTION)
434                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
435 #  else
436                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
437 #  endif
438
439                                 /* scale isect->t to adjust for instancing */
440                                 for(int i = 0; i < num_hits_in_instance; i++)
441                                         (isect_array-i-1)->t *= t_fac;
442                         }
443                         else {
444                                 float ignore_t = FLT_MAX;
445
446 #  if BVH_FEATURE(BVH_MOTION)
447                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &ignore_t, &ob_itfm);
448 #  else
449                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &ignore_t);
450 #  endif
451                         }
452
453                         isect_t = tmax;
454                         isect_array->t = isect_t;
455
456                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
457                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
458                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
459                         tfar = ssef(tmax);
460 #  if BVH_FEATURE(BVH_HAIR)
461                         dir4 = sse3f(ssef(dir.x), ssef(dir.y), ssef(dir.z));
462 #  endif
463                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
464 #  ifdef __KERNEL_AVX2__
465                         P_idir = P*idir;
466                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
467 #  endif
468 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
469                         org4 = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
470 #  endif
471
472                         triangle_intersect_precalc(dir, &isect_precalc);
473
474                         object = OBJECT_NONE;
475                         node_addr = traversal_stack[stack_ptr].addr;
476                         --stack_ptr;
477                 }
478 #endif  /* FEATURE(BVH_INSTANCING) */
479         } while(node_addr != ENTRYPOINT_SENTINEL);
480
481         return false;
482 }
483
484 #undef NODE_INTERSECT