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