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