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