Fix T48824: Crash when having too many ray-to-volume intersections
[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                                                         /* Update number of hits now, so we do proper check on max bounces. */
341                                                         (*num_hits)++;
342
343                                                         /* detect if this surface has a shader with transparent shadows */
344
345                                                         /* todo: optimize so primitive visibility flag indicates if
346                                                          * the primitive has a transparent shadow shader? */
347                                                         int prim = kernel_tex_fetch(__prim_index, isect_array->prim);
348                                                         int shader = 0;
349
350 #ifdef __HAIR__
351                                                         if(kernel_tex_fetch(__prim_type, isect_array->prim) & PRIMITIVE_ALL_TRIANGLE)
352 #endif
353                                                         {
354                                                                 shader = kernel_tex_fetch(__tri_shader, prim);
355                                                         }
356 #ifdef __HAIR__
357                                                         else {
358                                                                 float4 str = kernel_tex_fetch(__curves, prim);
359                                                                 shader = __float_as_int(str.z);
360                                                         }
361 #endif
362                                                         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
363
364                                                         /* if no transparent shadows, all light is blocked */
365                                                         if(!(flag & SD_HAS_TRANSPARENT_SHADOW)) {
366                                                                 return true;
367                                                         }
368                                                         /* if maximum number of hits reached, block all light */
369                                                         else if(*num_hits == max_hits) {
370                                                                 return true;
371                                                         }
372
373 #if BVH_FEATURE(BVH_INSTANCING)
374                                                         num_hits_in_instance++;
375 #endif
376                                                         /* Move on to next entry in intersections array */
377                                                         isect_array++;
378                                                         isect_array->t = isect_t;
379                                                 }
380
381                                                 prim_addr++;
382                                         }
383                                 }
384 #if BVH_FEATURE(BVH_INSTANCING)
385                                 else {
386                                         /* Instance push. */
387                                         object = kernel_tex_fetch(__prim_object, -prim_addr-1);
388
389 #  if BVH_FEATURE(BVH_MOTION)
390                                         bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
391 #  else
392                                         bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
393 #  endif
394
395                                         num_hits_in_instance = 0;
396                                         isect_array->t = isect_t;
397
398                                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
399                                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
400                                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
401                                         tfar = ssef(isect_t);
402 #  if BVH_FEATURE(BVH_HAIR)
403                                         dir4 = sse3f(ssef(dir.x), ssef(dir.y), ssef(dir.z));
404 #  endif
405                                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
406 #  ifdef __KERNEL_AVX2__
407                                         P_idir = P*idir;
408                                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
409 #  endif
410 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
411                                         org4 = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
412 #  endif
413
414                                         triangle_intersect_precalc(dir, &isect_precalc);
415
416                                         ++stack_ptr;
417                                         kernel_assert(stack_ptr < BVH_QSTACK_SIZE);
418                                         traversal_stack[stack_ptr].addr = ENTRYPOINT_SENTINEL;
419
420                                         node_addr = kernel_tex_fetch(__object_node, object);
421
422                                 }
423                         }
424 #endif  /* FEATURE(BVH_INSTANCING) */
425                 } while(node_addr != ENTRYPOINT_SENTINEL);
426
427 #if BVH_FEATURE(BVH_INSTANCING)
428                 if(stack_ptr >= 0) {
429                         kernel_assert(object != OBJECT_NONE);
430
431                         if(num_hits_in_instance) {
432                                 float t_fac;
433
434 #  if BVH_FEATURE(BVH_MOTION)
435                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
436 #  else
437                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
438 #  endif
439
440                                 /* scale isect->t to adjust for instancing */
441                                 for(int i = 0; i < num_hits_in_instance; i++)
442                                         (isect_array-i-1)->t *= t_fac;
443                         }
444                         else {
445                                 float ignore_t = FLT_MAX;
446
447 #  if BVH_FEATURE(BVH_MOTION)
448                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &ignore_t, &ob_itfm);
449 #  else
450                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &ignore_t);
451 #  endif
452                         }
453
454                         isect_t = tmax;
455                         isect_array->t = isect_t;
456
457                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
458                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
459                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
460                         tfar = ssef(tmax);
461 #  if BVH_FEATURE(BVH_HAIR)
462                         dir4 = sse3f(ssef(dir.x), ssef(dir.y), ssef(dir.z));
463 #  endif
464                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
465 #  ifdef __KERNEL_AVX2__
466                         P_idir = P*idir;
467                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
468 #  endif
469 #  if BVH_FEATURE(BVH_HAIR) || !defined(__KERNEL_AVX2__)
470                         org4 = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
471 #  endif
472
473                         triangle_intersect_precalc(dir, &isect_precalc);
474
475                         object = OBJECT_NONE;
476                         node_addr = traversal_stack[stack_ptr].addr;
477                         --stack_ptr;
478                 }
479 #endif  /* FEATURE(BVH_INSTANCING) */
480         } while(node_addr != ENTRYPOINT_SENTINEL);
481
482         return false;
483 }
484
485 #undef NODE_INTERSECT