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