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