Cycles: Fix wrong camera in volume check when domain is only visible to camera rays
[blender-staging.git] / intern / cycles / kernel / geom / geom_bvh_volume_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-2014, 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 "geom_qbvh_volume_all.h"
22 #endif
23
24 /* This is a template BVH traversal function for volumes, where
25  * various features can be enabled/disabled. This way we can compile optimized
26  * versions for each case without new features slowing things down.
27  *
28  * BVH_INSTANCING: object instancing
29  * BVH_HAIR: hair curve rendering
30  * BVH_MOTION: motion blur rendering
31  *
32  */
33
34 ccl_device uint BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
35                                             const Ray *ray,
36                                             Intersection *isect_array,
37                                             const uint max_hits,
38                                             const uint visibility)
39 {
40         /* todo:
41          * - test if pushing distance on the stack helps (for non shadow rays)
42          * - separate version for shadow rays
43          * - likely and unlikely for if() statements
44          * - test restrict attribute for pointers
45          */
46
47         /* traversal stack in CUDA thread-local memory */
48         int traversalStack[BVH_STACK_SIZE];
49         traversalStack[0] = ENTRYPOINT_SENTINEL;
50
51         /* traversal variables in registers */
52         int stackPtr = 0;
53         int nodeAddr = kernel_data.bvh.root;
54
55         /* ray parameters in registers */
56         const float tmax = ray->t;
57         float3 P = ray->P;
58         float3 dir = bvh_clamp_direction(ray->D);
59         float3 idir = bvh_inverse_direction(dir);
60         int object = OBJECT_NONE;
61         float isect_t = tmax;
62
63 #if BVH_FEATURE(BVH_MOTION)
64         Transform ob_itfm;
65 #endif
66
67 #if BVH_FEATURE(BVH_INSTANCING)
68         int num_hits_in_instance = 0;
69 #endif
70
71         uint num_hits = 0;
72         isect_array->t = tmax;
73
74 #if defined(__KERNEL_SSE2__)
75         const shuffle_swap_t shuf_identity = shuffle_swap_identity();
76         const shuffle_swap_t shuf_swap = shuffle_swap_swap();
77         
78         const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
79         ssef Psplat[3], idirsplat[3];
80         shuffle_swap_t shufflexyz[3];
81
82         Psplat[0] = ssef(P.x);
83         Psplat[1] = ssef(P.y);
84         Psplat[2] = ssef(P.z);
85
86         ssef tsplat(0.0f, 0.0f, -isect_t, -isect_t);
87
88         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
89 #endif
90
91         IsectPrecalc isect_precalc;
92         triangle_intersect_precalc(dir, &isect_precalc);
93
94         /* traversal loop */
95         do {
96                 do {
97                         /* traverse internal nodes */
98                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
99                                 bool traverseChild0, traverseChild1;
100                                 int nodeAddrChild1;
101
102 #if !defined(__KERNEL_SSE2__)
103                                 /* Intersect two child bounding boxes, non-SSE version */
104                                 float t = isect_array->t;
105
106                                 /* fetch node data */
107                                 float4 node0 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+0);
108                                 float4 node1 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+1);
109                                 float4 node2 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+2);
110                                 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+3);
111
112                                 /* intersect ray against child nodes */
113                                 NO_EXTENDED_PRECISION float c0lox = (node0.x - P.x) * idir.x;
114                                 NO_EXTENDED_PRECISION float c0hix = (node0.z - P.x) * idir.x;
115                                 NO_EXTENDED_PRECISION float c0loy = (node1.x - P.y) * idir.y;
116                                 NO_EXTENDED_PRECISION float c0hiy = (node1.z - P.y) * idir.y;
117                                 NO_EXTENDED_PRECISION float c0loz = (node2.x - P.z) * idir.z;
118                                 NO_EXTENDED_PRECISION float c0hiz = (node2.z - P.z) * idir.z;
119                                 NO_EXTENDED_PRECISION float c0min = max4(min(c0lox, c0hix), min(c0loy, c0hiy), min(c0loz, c0hiz), 0.0f);
120                                 NO_EXTENDED_PRECISION float c0max = min4(max(c0lox, c0hix), max(c0loy, c0hiy), max(c0loz, c0hiz), t);
121
122                                 NO_EXTENDED_PRECISION float c1lox = (node0.y - P.x) * idir.x;
123                                 NO_EXTENDED_PRECISION float c1hix = (node0.w - P.x) * idir.x;
124                                 NO_EXTENDED_PRECISION float c1loy = (node1.y - P.y) * idir.y;
125                                 NO_EXTENDED_PRECISION float c1hiy = (node1.w - P.y) * idir.y;
126                                 NO_EXTENDED_PRECISION float c1loz = (node2.y - P.z) * idir.z;
127                                 NO_EXTENDED_PRECISION float c1hiz = (node2.w - P.z) * idir.z;
128                                 NO_EXTENDED_PRECISION float c1min = max4(min(c1lox, c1hix), min(c1loy, c1hiy), min(c1loz, c1hiz), 0.0f);
129                                 NO_EXTENDED_PRECISION float c1max = min4(max(c1lox, c1hix), max(c1loy, c1hiy), max(c1loz, c1hiz), t);
130
131                                 /* decide which nodes to traverse next */
132                                 traverseChild0 = (c0max >= c0min);
133                                 traverseChild1 = (c1max >= c1min);
134
135 #else // __KERNEL_SSE2__
136                                 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
137
138                                 /* fetch node data */
139                                 const ssef *bvh_nodes = (ssef*)kg->__bvh_nodes.data + nodeAddr*BVH_NODE_SIZE;
140                                 const float4 cnodes = ((float4*)bvh_nodes)[3];
141
142                                 /* intersect ray against child nodes */
143                                 const ssef tminmaxx = (shuffle_swap(bvh_nodes[0], shufflexyz[0]) - Psplat[0]) * idirsplat[0];
144                                 const ssef tminmaxy = (shuffle_swap(bvh_nodes[1], shufflexyz[1]) - Psplat[1]) * idirsplat[1];
145                                 const ssef tminmaxz = (shuffle_swap(bvh_nodes[2], shufflexyz[2]) - Psplat[2]) * idirsplat[2];
146
147                                 /* calculate { c0min, c1min, -c0max, -c1max} */
148                                 ssef minmax = max(max(tminmaxx, tminmaxy), max(tminmaxz, tsplat));
149                                 const ssef tminmax = minmax ^ pn;
150
151                                 const sseb lrhit = tminmax <= shuffle<2, 3, 0, 1>(tminmax);
152
153                                 /* decide which nodes to traverse next */
154                                 traverseChild0 = (movemask(lrhit) & 1);
155                                 traverseChild1 = (movemask(lrhit) & 2);
156 #endif // __KERNEL_SSE2__
157
158                                 nodeAddr = __float_as_int(cnodes.x);
159                                 nodeAddrChild1 = __float_as_int(cnodes.y);
160
161                                 if(traverseChild0 && traverseChild1) {
162                                         /* both children were intersected, push the farther one */
163 #if !defined(__KERNEL_SSE2__)
164                                         bool closestChild1 = (c1min < c0min);
165 #else
166                                         bool closestChild1 = tminmax[1] < tminmax[0];
167 #endif
168
169                                         if(closestChild1) {
170                                                 int tmp = nodeAddr;
171                                                 nodeAddr = nodeAddrChild1;
172                                                 nodeAddrChild1 = tmp;
173                                         }
174
175                                         ++stackPtr;
176                                         kernel_assert(stackPtr < BVH_STACK_SIZE);
177                                         traversalStack[stackPtr] = nodeAddrChild1;
178                                 }
179                                 else {
180                                         /* one child was intersected */
181                                         if(traverseChild1) {
182                                                 nodeAddr = nodeAddrChild1;
183                                         }
184                                         else if(!traverseChild0) {
185                                                 /* neither child was intersected */
186                                                 nodeAddr = traversalStack[stackPtr];
187                                                 --stackPtr;
188                                         }
189                                 }
190                         }
191
192                         /* if node is leaf, fetch triangle list */
193                         if(nodeAddr < 0) {
194                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_NODE_LEAF_SIZE);
195                                 int primAddr = __float_as_int(leaf.x);
196
197 #if BVH_FEATURE(BVH_INSTANCING)
198                                 if(primAddr >= 0) {
199 #endif
200                                         const int primAddr2 = __float_as_int(leaf.y);
201                                         const uint type = __float_as_int(leaf.w);
202                                         bool hit;
203
204                                         /* pop */
205                                         nodeAddr = traversalStack[stackPtr];
206                                         --stackPtr;
207
208                                         /* primitive intersection */
209                                         switch(type & PRIMITIVE_ALL) {
210                                                 case PRIMITIVE_TRIANGLE: {
211                                                         /* intersect ray against primitive */
212                                                         for(; primAddr < primAddr2; primAddr++) {
213                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
214                                                                 /* only primitives from volume object */
215                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
216                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
217                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
218                                                                         continue;
219                                                                 }
220                                                                 hit = triangle_intersect(kg, &isect_precalc, isect_array, P, visibility, object, primAddr);
221                                                                 if(hit) {
222                                                                         /* Move on to next entry in intersections array. */
223                                                                         isect_array++;
224                                                                         num_hits++;
225 #if BVH_FEATURE(BVH_INSTANCING)
226                                                                         num_hits_in_instance++;
227 #endif
228                                                                         isect_array->t = isect_t;
229                                                                         if(num_hits == max_hits) {
230 #if BVH_FEATURE(BVH_INSTANCING)
231 #  if BVH_FEATURE(BVH_MOTION)
232                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
233 #  else
234                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
235                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
236 #  endif
237                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
238                                                                                         (isect_array-i-1)->t *= t_fac;
239                                                                                 }
240 #endif  /* BVH_FEATURE(BVH_INSTANCING) */
241                                                                                 return num_hits;
242                                                                         }
243                                                                 }
244                                                         }
245                                                         break;
246                                                 }
247 #if BVH_FEATURE(BVH_MOTION)
248                                                 case PRIMITIVE_MOTION_TRIANGLE: {
249                                                         /* intersect ray against primitive */
250                                                         for(; primAddr < primAddr2; primAddr++) {
251                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
252                                                                 /* only primitives from volume object */
253                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
254                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
255                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
256                                                                         continue;
257                                                                 }
258                                                                 hit = motion_triangle_intersect(kg, isect_array, P, dir, ray->time, visibility, object, primAddr);
259                                                                 if(hit) {
260                                                                         /* Move on to next entry in intersections array. */
261                                                                         isect_array++;
262                                                                         num_hits++;
263 #  if BVH_FEATURE(BVH_INSTANCING)
264                                                                         num_hits_in_instance++;
265 #  endif
266                                                                         isect_array->t = isect_t;
267                                                                         if(num_hits == max_hits) {
268 #  if BVH_FEATURE(BVH_INSTANCING)
269 #    if BVH_FEATURE(BVH_MOTION)
270                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
271 #    else
272                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
273                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
274 #    endif
275                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
276                                                                                         (isect_array-i-1)->t *= t_fac;
277                                                                                 }
278 #  endif  /* BVH_FEATURE(BVH_INSTANCING) */
279                                                                                 return num_hits;
280                                                                         }
281                                                                 }
282                                                         }
283                                                         break;
284                                                 }
285 #endif  /* BVH_MOTION */
286 #if BVH_FEATURE(BVH_HAIR)
287                                                 case PRIMITIVE_CURVE:
288                                                 case PRIMITIVE_MOTION_CURVE: {
289                                                         /* intersect ray against primitive */
290                                                         for(; primAddr < primAddr2; primAddr++) {
291                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
292                                                                 /* only primitives from volume object */
293                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
294                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
295                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
296                                                                         continue;
297                                                                 }
298                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
299                                                                         hit = bvh_cardinal_curve_intersect(kg, isect_array, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
300                                                                 else
301                                                                         hit = bvh_curve_intersect(kg, isect_array, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
302                                                                 if(hit) {
303                                                                         /* Move on to next entry in intersections array. */
304                                                                         isect_array++;
305                                                                         num_hits++;
306 #  if BVH_FEATURE(BVH_INSTANCING)
307                                                                         num_hits_in_instance++;
308 #  endif
309                                                                         isect_array->t = isect_t;
310                                                                         if(num_hits == max_hits) {
311 #  if BVH_FEATURE(BVH_INSTANCING)
312 #    if BVH_FEATURE(BVH_MOTION)
313                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
314 #    else
315                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
316                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
317 #    endif
318                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
319                                                                                         (isect_array-i-1)->t *= t_fac;
320                                                                                 }
321 #  endif  /* BVH_FEATURE(BVH_INSTANCING) */
322                                                                                 return num_hits;
323                                                                         }
324                                                                 }
325                                                         }
326                                                         break;
327                                                 }
328 #endif  /* BVH_HAIR */
329                                                 default: {
330                                                         break;
331                                                 }
332                                         }
333                                 }
334 #if BVH_FEATURE(BVH_INSTANCING)
335                                 else {
336                                         /* instance push */
337                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
338                                         int object_flag = kernel_tex_fetch(__object_flag, object);
339
340                                         if(object_flag & SD_OBJECT_HAS_VOLUME) {
341
342 #  if BVH_FEATURE(BVH_MOTION)
343                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
344 #  else
345                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
346 #  endif
347
348                                                 triangle_intersect_precalc(dir, &isect_precalc);
349                                                 num_hits_in_instance = 0;
350                                                 isect_array->t = isect_t;
351
352 #  if defined(__KERNEL_SSE2__)
353                                                 Psplat[0] = ssef(P.x);
354                                                 Psplat[1] = ssef(P.y);
355                                                 Psplat[2] = ssef(P.z);
356
357                                                 tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
358
359                                                 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
360 #  endif
361
362                                                 ++stackPtr;
363                                                 kernel_assert(stackPtr < BVH_STACK_SIZE);
364                                                 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
365
366                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
367                                         }
368                                         else {
369                                                 /* pop */
370                                                 object = OBJECT_NONE;
371                                                 nodeAddr = traversalStack[stackPtr];
372                                                 --stackPtr;
373                                         }
374                                 }
375                         }
376 #endif  /* FEATURE(BVH_INSTANCING) */
377                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
378
379 #if BVH_FEATURE(BVH_INSTANCING)
380                 if(stackPtr >= 0) {
381                         kernel_assert(object != OBJECT_NONE);
382
383                         if(num_hits_in_instance) {
384                                 float t_fac;
385 #  if BVH_FEATURE(BVH_MOTION)
386                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
387 #  else
388                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
389 #  endif
390                                 triangle_intersect_precalc(dir, &isect_precalc);
391                                 /* Scale isect->t to adjust for instancing. */
392                                 for(int i = 0; i < num_hits_in_instance; i++) {
393                                         (isect_array-i-1)->t *= t_fac;
394                                 }
395                         }
396                         else {
397                                 float ignore_t = FLT_MAX;
398 #  if BVH_FEATURE(BVH_MOTION)
399                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &ignore_t, &ob_itfm);
400 #  else
401                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &ignore_t);
402 #  endif
403                                 triangle_intersect_precalc(dir, &isect_precalc);
404                         }
405
406                         isect_t = tmax;
407                         isect_array->t = isect_t;
408
409 #  if defined(__KERNEL_SSE2__)
410                         Psplat[0] = ssef(P.x);
411                         Psplat[1] = ssef(P.y);
412                         Psplat[2] = ssef(P.z);
413
414                         tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
415
416                         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
417 #  endif
418
419                         object = OBJECT_NONE;
420                         nodeAddr = traversalStack[stackPtr];
421                         --stackPtr;
422                 }
423 #endif  /* FEATURE(BVH_MOTION) */
424         } while(nodeAddr != ENTRYPOINT_SENTINEL);
425
426         return num_hits;
427 }
428
429 ccl_device_inline uint BVH_FUNCTION_NAME(KernelGlobals *kg,
430                                          const Ray *ray,
431                                          Intersection *isect_array,
432                                          const uint max_hits,
433                                          const uint visibility)
434 {
435 #ifdef __QBVH__
436         if(kernel_data.bvh.use_qbvh) {
437                 return BVH_FUNCTION_FULL_NAME(QBVH)(kg,
438                                                     ray,
439                                                     isect_array,
440                                                     max_hits,
441                                                     visibility);
442         }
443         else
444 #endif
445         {
446                 kernel_assert(kernel_data.bvh.use_qbvh == false);
447                 return BVH_FUNCTION_FULL_NAME(BVH)(kg,
448                                                    ray,
449                                                    isect_array,
450                                                    max_hits,
451                                                    visibility);
452         }
453 }
454
455 #undef BVH_FUNCTION_NAME
456 #undef BVH_FUNCTION_FEATURES