1d6fa303d3ef58a06c183ae52a96ea0d48f72f5d
[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 ccl_device bool BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
41                                             const Ray *ray,
42                                             Intersection *isect_array,
43                                             const uint max_hits,
44                                             uint *num_hits)
45 {
46         /* todo:
47          * - likely and unlikely for if() statements
48          * - test restrict attribute for pointers
49          */
50
51         /* traversal stack in CUDA thread-local memory */
52         int traversal_stack[BVH_STACK_SIZE];
53         traversal_stack[0] = ENTRYPOINT_SENTINEL;
54
55         /* traversal variables in registers */
56         int stack_ptr = 0;
57         int node_addr = kernel_data.bvh.root;
58
59         /* ray parameters in registers */
60         const float tmax = ray->t;
61         float3 P = ray->P;
62         float3 dir = bvh_clamp_direction(ray->D);
63         float3 idir = bvh_inverse_direction(dir);
64         int object = OBJECT_NONE;
65         float isect_t = tmax;
66
67 #if BVH_FEATURE(BVH_MOTION)
68         Transform ob_itfm;
69 #endif
70
71 #if BVH_FEATURE(BVH_INSTANCING)
72         int num_hits_in_instance = 0;
73 #endif
74
75         *num_hits = 0;
76         isect_array->t = tmax;
77
78 #if defined(__KERNEL_SSE2__)
79         const shuffle_swap_t shuf_identity = shuffle_swap_identity();
80         const shuffle_swap_t shuf_swap = shuffle_swap_swap();
81
82         const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
83         ssef Psplat[3], idirsplat[3];
84 #  if BVH_FEATURE(BVH_HAIR)
85         ssef tnear(0.0f), tfar(isect_t);
86 #  endif
87         shuffle_swap_t shufflexyz[3];
88
89         Psplat[0] = ssef(P.x);
90         Psplat[1] = ssef(P.y);
91         Psplat[2] = ssef(P.z);
92
93         ssef tsplat(0.0f, 0.0f, -isect_t, -isect_t);
94
95         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
96 #endif  /* __KERNEL_SSE2__ */
97
98         IsectPrecalc isect_precalc;
99         triangle_intersect_precalc(dir, &isect_precalc);
100
101         /* traversal loop */
102         do {
103                 do {
104                         /* traverse internal nodes */
105                         while(node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
106                                 int node_addr_ahild1, traverse_mask;
107                                 float dist[2];
108                                 float4 cnodes = kernel_tex_fetch(__bvh_nodes, node_addr+0);
109
110 #if !defined(__KERNEL_SSE2__)
111                                 traverse_mask = NODE_INTERSECT(kg,
112                                                                P,
113 #  if BVH_FEATURE(BVH_HAIR)
114                                                                dir,
115 #  endif
116                                                                idir,
117                                                                isect_t,
118                                                                node_addr,
119                                                                PATH_RAY_SHADOW,
120                                                                dist);
121 #else // __KERNEL_SSE2__
122                                 traverse_mask = NODE_INTERSECT(kg,
123                                                                P,
124                                                                dir,
125 #  if BVH_FEATURE(BVH_HAIR)
126                                                                tnear,
127                                                                tfar,
128 #  endif
129                                                                tsplat,
130                                                                Psplat,
131                                                                idirsplat,
132                                                                shufflexyz,
133                                                                node_addr,
134                                                                PATH_RAY_SHADOW,
135                                                                dist);
136 #endif // __KERNEL_SSE2__
137
138                                 node_addr = __float_as_int(cnodes.z);
139                                 node_addr_ahild1 = __float_as_int(cnodes.w);
140
141                                 if(traverse_mask == 3) {
142                                         /* Both children were intersected, push the farther one. */
143                                         bool is_closest_child1 = (dist[1] < dist[0]);
144                                         if(is_closest_child1) {
145                                                 int tmp = node_addr;
146                                                 node_addr = node_addr_ahild1;
147                                                 node_addr_ahild1 = tmp;
148                                         }
149
150                                         ++stack_ptr;
151                                         kernel_assert(stack_ptr < BVH_STACK_SIZE);
152                                         traversal_stack[stack_ptr] = node_addr_ahild1;
153                                 }
154                                 else {
155                                         /* One child was intersected. */
156                                         if(traverse_mask == 2) {
157                                                 node_addr = node_addr_ahild1;
158                                         }
159                                         else if(traverse_mask == 0) {
160                                                 /* Neither child was intersected. */
161                                                 node_addr = traversal_stack[stack_ptr];
162                                                 --stack_ptr;
163                                         }
164                                 }
165                         }
166
167                         /* if node is leaf, fetch triangle list */
168                         if(node_addr < 0) {
169                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-node_addr-1));
170                                 int prim_addr = __float_as_int(leaf.x);
171
172 #if BVH_FEATURE(BVH_INSTANCING)
173                                 if(prim_addr >= 0) {
174 #endif
175                                         const int prim_addr2 = __float_as_int(leaf.y);
176                                         const uint type = __float_as_int(leaf.w);
177                                         const uint p_type = type & PRIMITIVE_ALL;
178
179                                         /* pop */
180                                         node_addr = traversal_stack[stack_ptr];
181                                         --stack_ptr;
182
183                                         /* primitive intersection */
184                                         while(prim_addr < prim_addr2) {
185                                                 kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
186
187                                                 bool hit;
188
189                                                 /* todo: specialized intersect functions which don't fill in
190                                                  * isect unless needed and check SD_HAS_TRANSPARENT_SHADOW?
191                                                  * might give a few % performance improvement */
192
193                                                 switch(p_type) {
194                                                         case PRIMITIVE_TRIANGLE: {
195                                                                 hit = triangle_intersect(kg,
196                                                                                          &isect_precalc,
197                                                                                          isect_array,
198                                                                                          P,
199                                                                                          PATH_RAY_SHADOW,
200                                                                                          object,
201                                                                                          prim_addr);
202                                                                 break;
203                                                         }
204 #if BVH_FEATURE(BVH_MOTION)
205                                                         case PRIMITIVE_MOTION_TRIANGLE: {
206                                                                 hit = motion_triangle_intersect(kg,
207                                                                                                 isect_array,
208                                                                                                 P,
209                                                                                                 dir,
210                                                                                                 ray->time,
211                                                                                                 PATH_RAY_SHADOW,
212                                                                                                 object,
213                                                                                                 prim_addr);
214                                                                 break;
215                                                         }
216 #endif
217 #if BVH_FEATURE(BVH_HAIR)
218                                                         case PRIMITIVE_CURVE:
219                                                         case PRIMITIVE_MOTION_CURVE: {
220                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE) {
221                                                                         hit = bvh_cardinal_curve_intersect(kg,
222                                                                                                            isect_array,
223                                                                                                            P,
224                                                                                                            dir,
225                                                                                                            PATH_RAY_SHADOW,
226                                                                                                            object,
227                                                                                                            prim_addr,
228                                                                                                            ray->time,
229                                                                                                            type,
230                                                                                                            NULL,
231                                                                                                            0, 0);
232                                                                 }
233                                                                 else {
234                                                                         hit = bvh_curve_intersect(kg,
235                                                                                                   isect_array,
236                                                                                                   P,
237                                                                                                   dir,
238                                                                                                   PATH_RAY_SHADOW,
239                                                                                                   object,
240                                                                                                   prim_addr,
241                                                                                                   ray->time,
242                                                                                                   type,
243                                                                                                   NULL,
244                                                                                                   0, 0);
245                                                                 }
246                                                                 break;
247                                                         }
248 #endif
249                                                         default: {
250                                                                 hit = false;
251                                                                 break;
252                                                         }
253                                                 }
254
255                                                 /* shadow ray early termination */
256                                                 if(hit) {
257                                                         /* detect if this surface has a shader with transparent shadows */
258
259                                                         /* todo: optimize so primitive visibility flag indicates if
260                                                          * the primitive has a transparent shadow shader? */
261                                                         int prim = kernel_tex_fetch(__prim_index, isect_array->prim);
262                                                         int shader = 0;
263
264 #ifdef __HAIR__
265                                                         if(kernel_tex_fetch(__prim_type, isect_array->prim) & PRIMITIVE_ALL_TRIANGLE)
266 #endif
267                                                         {
268                                                                 shader = kernel_tex_fetch(__tri_shader, prim);
269                                                         }
270 #ifdef __HAIR__
271                                                         else {
272                                                                 float4 str = kernel_tex_fetch(__curves, prim);
273                                                                 shader = __float_as_int(str.z);
274                                                         }
275 #endif
276                                                         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
277
278                                                         /* if no transparent shadows, all light is blocked */
279                                                         if(!(flag & SD_HAS_TRANSPARENT_SHADOW)) {
280                                                                 return true;
281                                                         }
282                                                         /* if maximum number of hits reached, block all light */
283                                                         else if(*num_hits == max_hits) {
284                                                                 return true;
285                                                         }
286
287                                                         /* move on to next entry in intersections array */
288                                                         isect_array++;
289                                                         (*num_hits)++;
290 #if BVH_FEATURE(BVH_INSTANCING)
291                                                         num_hits_in_instance++;
292 #endif
293
294                                                         isect_array->t = isect_t;
295                                                 }
296
297                                                 prim_addr++;
298                                         }
299                                 }
300 #if BVH_FEATURE(BVH_INSTANCING)
301                                 else {
302                                         /* instance push */
303                                         object = kernel_tex_fetch(__prim_object, -prim_addr-1);
304
305 #  if BVH_FEATURE(BVH_MOTION)
306                                         bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
307 #  else
308                                         bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
309 #  endif
310
311                                         triangle_intersect_precalc(dir, &isect_precalc);
312                                         num_hits_in_instance = 0;
313                                         isect_array->t = isect_t;
314
315 #  if defined(__KERNEL_SSE2__)
316                                         Psplat[0] = ssef(P.x);
317                                         Psplat[1] = ssef(P.y);
318                                         Psplat[2] = ssef(P.z);
319
320                                         tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
321 #    if BVH_FEATURE(BVH_HAIR)
322                                         tfar = ssef(isect_t);
323 #    endif
324                                         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
325 #  endif
326
327                                         ++stack_ptr;
328                                         kernel_assert(stack_ptr < BVH_STACK_SIZE);
329                                         traversal_stack[stack_ptr] = ENTRYPOINT_SENTINEL;
330
331                                         node_addr = kernel_tex_fetch(__object_node, object);
332                                 }
333                         }
334 #endif  /* FEATURE(BVH_INSTANCING) */
335                 } while(node_addr != ENTRYPOINT_SENTINEL);
336
337 #if BVH_FEATURE(BVH_INSTANCING)
338                 if(stack_ptr >= 0) {
339                         kernel_assert(object != OBJECT_NONE);
340
341                         if(num_hits_in_instance) {
342                                 float t_fac;
343
344 #  if BVH_FEATURE(BVH_MOTION)
345                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
346 #  else
347                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
348 #  endif
349
350                                 triangle_intersect_precalc(dir, &isect_precalc);
351
352                                 /* scale isect->t to adjust for instancing */
353                                 for(int i = 0; i < num_hits_in_instance; i++)
354                                         (isect_array-i-1)->t *= t_fac;
355                         }
356                         else {
357                                 float ignore_t = FLT_MAX;
358
359 #  if BVH_FEATURE(BVH_MOTION)
360                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &ignore_t, &ob_itfm);
361 #  else
362                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &ignore_t);
363 #  endif
364                                 triangle_intersect_precalc(dir, &isect_precalc);
365                         }
366
367                         isect_t = tmax;
368                         isect_array->t = isect_t;
369
370 #  if defined(__KERNEL_SSE2__)
371                         Psplat[0] = ssef(P.x);
372                         Psplat[1] = ssef(P.y);
373                         Psplat[2] = ssef(P.z);
374
375                         tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
376 #    if BVH_FEATURE(BVH_HAIR)
377                         tfar = ssef(isect_t);
378 #    endif
379                         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
380 #  endif
381
382                         object = OBJECT_NONE;
383                         node_addr = traversal_stack[stack_ptr];
384                         --stack_ptr;
385                 }
386 #endif  /* FEATURE(BVH_INSTANCING) */
387         } while(node_addr != ENTRYPOINT_SENTINEL);
388
389         return false;
390 }
391
392 ccl_device_inline bool BVH_FUNCTION_NAME(KernelGlobals *kg,
393                                          const Ray *ray,
394                                          Intersection *isect_array,
395                                          const uint max_hits,
396                                          uint *num_hits)
397 {
398 #ifdef __QBVH__
399         if(kernel_data.bvh.use_qbvh) {
400                 return BVH_FUNCTION_FULL_NAME(QBVH)(kg,
401                                                     ray,
402                                                     isect_array,
403                                                     max_hits,
404                                                     num_hits);
405         }
406         else
407 #endif
408         {
409                 kernel_assert(kernel_data.bvh.use_qbvh == false);
410                 return BVH_FUNCTION_FULL_NAME(BVH)(kg,
411                                                    ray,
412                                                    isect_array,
413                                                    max_hits,
414                                                    num_hits);
415         }
416 }
417
418 #undef BVH_FUNCTION_NAME
419 #undef BVH_FUNCTION_FEATURES
420 #undef NODE_INTERSECT