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