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