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