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