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.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.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 bool BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
35                                             const Ray *ray,
36                                             Intersection *isect,
37                                             const uint visibility)
38 {
39         /* todo:
40          * - test if pushing distance on the stack helps (for non shadow rays)
41          * - separate version for shadow rays
42          * - likely and unlikely for if() statements
43          * - test restrict attribute for pointers
44          */
45
46         /* traversal stack in CUDA thread-local memory */
47         int traversalStack[BVH_STACK_SIZE];
48         traversalStack[0] = ENTRYPOINT_SENTINEL;
49
50         /* traversal variables in registers */
51         int stackPtr = 0;
52         int nodeAddr = kernel_data.bvh.root;
53
54         /* ray parameters in registers */
55         float3 P = ray->P;
56         float3 dir = bvh_clamp_direction(ray->D);
57         float3 idir = bvh_inverse_direction(dir);
58         int object = OBJECT_NONE;
59
60 #if BVH_FEATURE(BVH_MOTION)
61         Transform ob_itfm;
62 #endif
63
64         isect->t = ray->t;
65         isect->u = 0.0f;
66         isect->v = 0.0f;
67         isect->prim = PRIM_NONE;
68         isect->object = OBJECT_NONE;
69
70 #if defined(__KERNEL_SSE2__)
71         const shuffle_swap_t shuf_identity = shuffle_swap_identity();
72         const shuffle_swap_t shuf_swap = shuffle_swap_swap();
73         
74         const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
75         ssef Psplat[3], idirsplat[3];
76         shuffle_swap_t shufflexyz[3];
77
78         Psplat[0] = ssef(P.x);
79         Psplat[1] = ssef(P.y);
80         Psplat[2] = ssef(P.z);
81
82         ssef tsplat(0.0f, 0.0f, -isect->t, -isect->t);
83
84         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
85 #endif
86
87         IsectPrecalc isect_precalc;
88         triangle_intersect_precalc(dir, &isect_precalc);
89
90         /* traversal loop */
91         do {
92                 do {
93                         /* traverse internal nodes */
94                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
95                                 bool traverseChild0, traverseChild1;
96                                 int nodeAddrChild1;
97
98 #if !defined(__KERNEL_SSE2__)
99                                 /* Intersect two child bounding boxes, non-SSE version */
100                                 float t = isect->t;
101
102                                 /* fetch node data */
103                                 float4 node0 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+0);
104                                 float4 node1 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+1);
105                                 float4 node2 = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+2);
106                                 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_NODE_SIZE+3);
107
108                                 /* intersect ray against child nodes */
109                                 NO_EXTENDED_PRECISION float c0lox = (node0.x - P.x) * idir.x;
110                                 NO_EXTENDED_PRECISION float c0hix = (node0.z - P.x) * idir.x;
111                                 NO_EXTENDED_PRECISION float c0loy = (node1.x - P.y) * idir.y;
112                                 NO_EXTENDED_PRECISION float c0hiy = (node1.z - P.y) * idir.y;
113                                 NO_EXTENDED_PRECISION float c0loz = (node2.x - P.z) * idir.z;
114                                 NO_EXTENDED_PRECISION float c0hiz = (node2.z - P.z) * idir.z;
115                                 NO_EXTENDED_PRECISION float c0min = max4(min(c0lox, c0hix), min(c0loy, c0hiy), min(c0loz, c0hiz), 0.0f);
116                                 NO_EXTENDED_PRECISION float c0max = min4(max(c0lox, c0hix), max(c0loy, c0hiy), max(c0loz, c0hiz), t);
117
118                                 NO_EXTENDED_PRECISION float c1lox = (node0.y - P.x) * idir.x;
119                                 NO_EXTENDED_PRECISION float c1hix = (node0.w - P.x) * idir.x;
120                                 NO_EXTENDED_PRECISION float c1loy = (node1.y - P.y) * idir.y;
121                                 NO_EXTENDED_PRECISION float c1hiy = (node1.w - P.y) * idir.y;
122                                 NO_EXTENDED_PRECISION float c1loz = (node2.y - P.z) * idir.z;
123                                 NO_EXTENDED_PRECISION float c1hiz = (node2.w - P.z) * idir.z;
124                                 NO_EXTENDED_PRECISION float c1min = max4(min(c1lox, c1hix), min(c1loy, c1hiy), min(c1loz, c1hiz), 0.0f);
125                                 NO_EXTENDED_PRECISION float c1max = min4(max(c1lox, c1hix), max(c1loy, c1hiy), max(c1loz, c1hiz), t);
126
127                                 /* decide which nodes to traverse next */
128                                 traverseChild0 = (c0max >= c0min);
129                                 traverseChild1 = (c1max >= c1min);
130
131 #else // __KERNEL_SSE2__
132                                 /* Intersect two child bounding boxes, SSE3 version adapted from Embree */
133
134                                 /* fetch node data */
135                                 const ssef *bvh_nodes = (ssef*)kg->__bvh_nodes.data + nodeAddr*BVH_NODE_SIZE;
136                                 const float4 cnodes = ((float4*)bvh_nodes)[3];
137
138                                 /* intersect ray against child nodes */
139                                 const ssef tminmaxx = (shuffle_swap(bvh_nodes[0], shufflexyz[0]) - Psplat[0]) * idirsplat[0];
140                                 const ssef tminmaxy = (shuffle_swap(bvh_nodes[1], shufflexyz[1]) - Psplat[1]) * idirsplat[1];
141                                 const ssef tminmaxz = (shuffle_swap(bvh_nodes[2], shufflexyz[2]) - Psplat[2]) * idirsplat[2];
142
143                                 /* calculate { c0min, c1min, -c0max, -c1max} */
144                                 ssef minmax = max(max(tminmaxx, tminmaxy), max(tminmaxz, tsplat));
145                                 const ssef tminmax = minmax ^ pn;
146
147                                 const sseb lrhit = tminmax <= shuffle<2, 3, 0, 1>(tminmax);
148
149                                 /* decide which nodes to traverse next */
150                                 traverseChild0 = (movemask(lrhit) & 1);
151                                 traverseChild1 = (movemask(lrhit) & 2);
152 #endif // __KERNEL_SSE2__
153
154                                 nodeAddr = __float_as_int(cnodes.x);
155                                 nodeAddrChild1 = __float_as_int(cnodes.y);
156
157                                 if(traverseChild0 && traverseChild1) {
158                                         /* both children were intersected, push the farther one */
159 #if !defined(__KERNEL_SSE2__)
160                                         bool closestChild1 = (c1min < c0min);
161 #else
162                                         bool closestChild1 = tminmax[1] < tminmax[0];
163 #endif
164
165                                         if(closestChild1) {
166                                                 int tmp = nodeAddr;
167                                                 nodeAddr = nodeAddrChild1;
168                                                 nodeAddrChild1 = tmp;
169                                         }
170
171                                         ++stackPtr;
172                                         kernel_assert(stackPtr < BVH_STACK_SIZE);
173                                         traversalStack[stackPtr] = nodeAddrChild1;
174                                 }
175                                 else {
176                                         /* one child was intersected */
177                                         if(traverseChild1) {
178                                                 nodeAddr = nodeAddrChild1;
179                                         }
180                                         else if(!traverseChild0) {
181                                                 /* neither child was intersected */
182                                                 nodeAddr = traversalStack[stackPtr];
183                                                 --stackPtr;
184                                         }
185                                 }
186                         }
187
188                         /* if node is leaf, fetch triangle list */
189                         if(nodeAddr < 0) {
190                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_NODE_LEAF_SIZE);
191                                 int primAddr = __float_as_int(leaf.x);
192
193 #if BVH_FEATURE(BVH_INSTANCING)
194                                 if(primAddr >= 0) {
195 #endif
196                                         const int primAddr2 = __float_as_int(leaf.y);
197                                         const uint type = __float_as_int(leaf.w);
198
199                                         /* pop */
200                                         nodeAddr = traversalStack[stackPtr];
201                                         --stackPtr;
202
203                                         /* primitive intersection */
204                                         switch(type & PRIMITIVE_ALL) {
205                                                 case PRIMITIVE_TRIANGLE: {
206                                                         /* intersect ray against primitive */
207                                                         for(; primAddr < primAddr2; primAddr++) {
208                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
209                                                                 /* only primitives from volume object */
210                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
211                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
212                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
213                                                                         continue;
214                                                                 }
215                                                                 triangle_intersect(kg, &isect_precalc, isect, P, visibility, object, primAddr);
216                                                         }
217                                                         break;
218                                                 }
219 #if BVH_FEATURE(BVH_MOTION)
220                                                 case PRIMITIVE_MOTION_TRIANGLE: {
221                                                         /* intersect ray against primitive */
222                                                         for(; primAddr < primAddr2; primAddr++) {
223                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
224                                                                 /* only primitives from volume object */
225                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
226                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
227                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
228                                                                         continue;
229                                                                 }
230                                                                 motion_triangle_intersect(kg, isect, P, dir, ray->time, visibility, object, primAddr);
231                                                         }
232                                                         break;
233                                                 }
234 #endif
235 #if BVH_FEATURE(BVH_HAIR)
236                                                 case PRIMITIVE_CURVE:
237                                                 case PRIMITIVE_MOTION_CURVE: {
238                                                         /* intersect ray against primitive */
239                                                         for(; primAddr < primAddr2; primAddr++) {
240                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
241                                                                 /* only primitives from volume object */
242                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
243                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
244                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
245                                                                         continue;
246                                                                 }
247                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
248                                                                         bvh_cardinal_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
249                                                                 else
250                                                                         bvh_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
251                                                         }
252                                                         break;
253                                                 }
254 #endif
255                                                 default: {
256                                                         break;
257                                                 }
258                                         }
259                                 }
260 #if BVH_FEATURE(BVH_INSTANCING)
261                                 else {
262                                         /* instance push */
263                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
264                                         int object_flag = kernel_tex_fetch(__object_flag, object);
265
266                                         if(object_flag & SD_OBJECT_HAS_VOLUME) {
267
268 #  if BVH_FEATURE(BVH_MOTION)
269                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_itfm);
270 #  else
271                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect->t);
272 #  endif
273
274                                                 triangle_intersect_precalc(dir, &isect_precalc);
275
276 #  if defined(__KERNEL_SSE2__)
277                                                 Psplat[0] = ssef(P.x);
278                                                 Psplat[1] = ssef(P.y);
279                                                 Psplat[2] = ssef(P.z);
280
281                                                 tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
282
283                                                 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
284 #  endif
285
286                                                 ++stackPtr;
287                                                 kernel_assert(stackPtr < BVH_STACK_SIZE);
288                                                 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
289
290                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
291                                         }
292                                         else {
293                                                 /* pop */
294                                                 object = OBJECT_NONE;
295                                                 nodeAddr = traversalStack[stackPtr];
296                                                 --stackPtr;
297                                         }
298                                 }
299                         }
300 #endif  /* FEATURE(BVH_INSTANCING) */
301                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
302
303 #if BVH_FEATURE(BVH_INSTANCING)
304                 if(stackPtr >= 0) {
305                         kernel_assert(object != OBJECT_NONE);
306
307                         /* instance pop */
308 #  if BVH_FEATURE(BVH_MOTION)
309                         bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_itfm);
310 #  else
311                         bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &isect->t);
312 #  endif
313
314                         triangle_intersect_precalc(dir, &isect_precalc);
315
316 #  if defined(__KERNEL_SSE2__)
317                         Psplat[0] = ssef(P.x);
318                         Psplat[1] = ssef(P.y);
319                         Psplat[2] = ssef(P.z);
320
321                         tsplat = ssef(0.0f, 0.0f, -isect->t, -isect->t);
322
323                         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
324 #  endif
325
326                         object = OBJECT_NONE;
327                         nodeAddr = traversalStack[stackPtr];
328                         --stackPtr;
329                 }
330 #endif  /* FEATURE(BVH_MOTION) */
331         } while(nodeAddr != ENTRYPOINT_SENTINEL);
332
333         return (isect->prim != PRIM_NONE);
334 }
335
336 ccl_device_inline bool BVH_FUNCTION_NAME(KernelGlobals *kg,
337                                          const Ray *ray,
338                                          Intersection *isect,
339                                          const uint visibility)
340 {
341 #ifdef __QBVH__
342         if(kernel_data.bvh.use_qbvh) {
343                 return BVH_FUNCTION_FULL_NAME(QBVH)(kg,
344                                                     ray,
345                                                     isect,
346                                                     visibility);
347         }
348         else
349 #endif
350         {
351                 kernel_assert(kernel_data.bvh.use_qbvh == false);
352                 return BVH_FUNCTION_FULL_NAME(BVH)(kg,
353                                                    ray,
354                                                    isect,
355                                                    visibility);
356         }
357 }
358
359 #undef BVH_FUNCTION_NAME
360 #undef BVH_FUNCTION_FEATURES