696a48a7b3ea9b23a0e28f7df4a0a5b793c06f3e
[blender-staging.git] / intern / cycles / kernel / geom / geom_qbvh_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 /* This is a template BVH traversal function for volumes, where
21  * various features can be enabled/disabled. This way we can compile optimized
22  * versions for each case without new features slowing things down.
23  *
24  * BVH_INSTANCING: object instancing
25  * BVH_HAIR: hair curve rendering
26  * BVH_MOTION: motion blur rendering
27  *
28  */
29
30 ccl_device bool BVH_FUNCTION_FULL_NAME(QBVH)(KernelGlobals *kg,
31                                              const Ray *ray,
32                                              Intersection *isect)
33 {
34         /* TODO(sergey):
35          * - Test if pushing distance on the stack helps.
36          * - Likely and unlikely for if() statements.
37          * - Test restrict attribute for pointers.
38          */
39
40         /* Traversal stack in CUDA thread-local memory. */
41         QBVHStackItem traversalStack[BVH_QSTACK_SIZE];
42         traversalStack[0].addr = ENTRYPOINT_SENTINEL;
43
44         /* Traversal variables in registers. */
45         int stackPtr = 0;
46         int nodeAddr = kernel_data.bvh.root;
47
48         /* Ray parameters in registers. */
49         float3 P = ray->P;
50         float3 dir = bvh_clamp_direction(ray->D);
51         float3 idir = bvh_inverse_direction(dir);
52         int object = OBJECT_NONE;
53
54         const uint visibility = PATH_RAY_ALL_VISIBILITY;
55
56 #if BVH_FEATURE(BVH_MOTION)
57         Transform ob_itfm;
58 #endif
59
60 #ifndef __KERNEL_SSE41__
61         if(!isfinite(P.x)) {
62                 return false;
63         }
64 #endif
65
66         isect->t = ray->t;
67         isect->u = 0.0f;
68         isect->v = 0.0f;
69         isect->prim = PRIM_NONE;
70         isect->object = OBJECT_NONE;
71
72         ssef tnear(0.0f), tfar(ray->t);
73         sse3f idir4(ssef(idir.x), ssef(idir.y), ssef(idir.z));
74
75 #ifdef __KERNEL_AVX2__
76         float3 P_idir = P*idir;
77         sse3f P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
78 #else
79         sse3f org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
80 #endif
81
82         /* Offsets to select the side that becomes the lower or upper bound. */
83         int near_x, near_y, near_z;
84         int far_x, far_y, far_z;
85
86         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
87         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
88         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
89
90         IsectPrecalc isect_precalc;
91         triangle_intersect_precalc(dir, &isect_precalc);
92
93         /* Traversal loop. */
94         do {
95                 do {
96                         /* Traverse internal nodes. */
97                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
98                                 ssef dist;
99                                 int traverseChild = qbvh_node_intersect(kg,
100                                                                         tnear,
101                                                                         tfar,
102 #ifdef __KERNEL_AVX2__
103                                                                         P_idir4,
104 #else
105                                                                         org,
106 #endif
107                                                                         idir4,
108                                                                         near_x, near_y, near_z,
109                                                                         far_x, far_y, far_z,
110                                                                         nodeAddr,
111                                                                         &dist);
112
113                                 if(traverseChild != 0) {
114                                         float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_QNODE_SIZE+6);
115
116                                         /* One child is hit, continue with that child. */
117                                         int r = __bscf(traverseChild);
118                                         if(traverseChild == 0) {
119                                                 nodeAddr = __float_as_int(cnodes[r]);
120                                                 continue;
121                                         }
122
123                                         /* Two children are hit, push far child, and continue with
124                                          * closer child.
125                                          */
126                                         int c0 = __float_as_int(cnodes[r]);
127                                         float d0 = ((float*)&dist)[r];
128                                         r = __bscf(traverseChild);
129                                         int c1 = __float_as_int(cnodes[r]);
130                                         float d1 = ((float*)&dist)[r];
131                                         if(traverseChild == 0) {
132                                                 if(d1 < d0) {
133                                                         nodeAddr = c1;
134                                                         ++stackPtr;
135                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
136                                                         traversalStack[stackPtr].addr = c0;
137                                                         traversalStack[stackPtr].dist = d0;
138                                                         continue;
139                                                 }
140                                                 else {
141                                                         nodeAddr = c0;
142                                                         ++stackPtr;
143                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
144                                                         traversalStack[stackPtr].addr = c1;
145                                                         traversalStack[stackPtr].dist = d1;
146                                                         continue;
147                                                 }
148                                         }
149
150                                         /* Here starts the slow path for 3 or 4 hit children. We push
151                                          * all nodes onto the stack to sort them there.
152                                          */
153                                         ++stackPtr;
154                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
155                                         traversalStack[stackPtr].addr = c1;
156                                         traversalStack[stackPtr].dist = d1;
157                                         ++stackPtr;
158                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
159                                         traversalStack[stackPtr].addr = c0;
160                                         traversalStack[stackPtr].dist = d0;
161
162                                         /* Three children are hit, push all onto stack and sort 3
163                                          * stack items, continue with closest child.
164                                          */
165                                         r = __bscf(traverseChild);
166                                         int c2 = __float_as_int(cnodes[r]);
167                                         float d2 = ((float*)&dist)[r];
168                                         if(traverseChild == 0) {
169                                                 ++stackPtr;
170                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
171                                                 traversalStack[stackPtr].addr = c2;
172                                                 traversalStack[stackPtr].dist = d2;
173                                                 qbvh_stack_sort(&traversalStack[stackPtr],
174                                                                 &traversalStack[stackPtr - 1],
175                                                                 &traversalStack[stackPtr - 2]);
176                                                 nodeAddr = traversalStack[stackPtr].addr;
177                                                 --stackPtr;
178                                                 continue;
179                                         }
180
181                                         /* Four children are hit, push all onto stack and sort 4
182                                          * stack items, continue with closest child.
183                                          */
184                                         r = __bscf(traverseChild);
185                                         int c3 = __float_as_int(cnodes[r]);
186                                         float d3 = ((float*)&dist)[r];
187                                         ++stackPtr;
188                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
189                                         traversalStack[stackPtr].addr = c3;
190                                         traversalStack[stackPtr].dist = d3;
191                                         ++stackPtr;
192                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
193                                         traversalStack[stackPtr].addr = c2;
194                                         traversalStack[stackPtr].dist = d2;
195                                         qbvh_stack_sort(&traversalStack[stackPtr],
196                                                         &traversalStack[stackPtr - 1],
197                                                         &traversalStack[stackPtr - 2],
198                                                         &traversalStack[stackPtr - 3]);
199                                 }
200
201                                 nodeAddr = traversalStack[stackPtr].addr;
202                                 --stackPtr;
203                         }
204
205                         /* If node is leaf, fetch triangle list. */
206                         if(nodeAddr < 0) {
207                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_QNODE_LEAF_SIZE);
208                                 int primAddr = __float_as_int(leaf.x);
209
210 #if BVH_FEATURE(BVH_INSTANCING)
211                                 if(primAddr >= 0) {
212 #endif
213                                         int primAddr2 = __float_as_int(leaf.y);
214                                         const uint type = __float_as_int(leaf.w);
215                                         const uint p_type = type & PRIMITIVE_ALL;
216
217                                         /* Pop. */
218                                         nodeAddr = traversalStack[stackPtr].addr;
219                                         --stackPtr;
220
221                                         /* Primitive intersection. */
222                                         switch(p_type) {
223                                                 case PRIMITIVE_TRIANGLE: {
224                                                         for(; primAddr < primAddr2; primAddr++) {
225                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
226                                                                 /* Only primitives from volume object. */
227                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
228                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
229                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
230                                                                         continue;
231                                                                 }
232                                                                 /* Intersect ray against primitive. */
233                                                                 triangle_intersect(kg, &isect_precalc, isect, P, visibility, object, primAddr);
234                                                         }
235                                                         break;
236                                                 }
237 #if BVH_FEATURE(BVH_MOTION)
238                                                 case PRIMITIVE_MOTION_TRIANGLE: {
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                                                                 /* Intersect ray against primitive. */
248                                                                 motion_triangle_intersect(kg, isect, P, dir, ray->time, visibility, object, primAddr);
249                                                         }
250                                                         break;
251                                                 }
252 #endif
253 #if BVH_FEATURE(BVH_HAIR)
254                                                 case PRIMITIVE_CURVE:
255                                                 case PRIMITIVE_MOTION_CURVE: {
256                                                         for(; primAddr < primAddr2; primAddr++) {
257                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
258                                                                 /* Only primitives from volume object. */
259                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
260                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
261                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
262                                                                         continue;
263                                                                 }
264                                                                 /* Intersect ray against primitive. */
265                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
266                                                                         bvh_cardinal_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
267                                                                 else
268                                                                         bvh_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
269                                                         }
270                                                         break;
271                                                 }
272 #endif
273                                         }
274                                 }
275 #if BVH_FEATURE(BVH_INSTANCING)
276                                 else {
277                                         /* Instance push. */
278                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
279                                         int object_flag = kernel_tex_fetch(__object_flag, object);
280
281                                         if(object_flag & SD_OBJECT_HAS_VOLUME) {
282
283 #  if BVH_FEATURE(BVH_MOTION)
284                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_itfm);
285 #  else
286                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect->t);
287 #  endif
288
289                                                 if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
290                                                 if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
291                                                 if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
292                                                 tfar = ssef(isect->t);
293                                                 idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
294 #  ifdef __KERNEL_AVX2__
295                                                 P_idir = P*idir;
296                                                 P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
297 #  else
298                                                 org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
299 #  endif
300                                                 triangle_intersect_precalc(dir, &isect_precalc);
301
302                                                 ++stackPtr;
303                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
304                                                 traversalStack[stackPtr].addr = ENTRYPOINT_SENTINEL;
305
306                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
307                                         }
308                                         else {
309                                                 /* Pop. */
310                                                 object = OBJECT_NONE;
311                                                 nodeAddr = traversalStack[stackPtr].addr;
312                                                 --stackPtr;
313                                         }
314                                 }
315                         }
316 #endif  /* FEATURE(BVH_INSTANCING) */
317                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
318
319 #if BVH_FEATURE(BVH_INSTANCING)
320                 if(stackPtr >= 0) {
321                         kernel_assert(object != OBJECT_NONE);
322
323                         /* Instance pop. */
324 #  if BVH_FEATURE(BVH_MOTION)
325                         bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_itfm);
326 #  else
327                         bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &isect->t);
328 #  endif
329
330                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
331                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
332                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
333                         tfar = ssef(isect->t);
334                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
335 #  ifdef __KERNEL_AVX2__
336                         P_idir = P*idir;
337                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
338 #  else
339                         org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
340 #  endif
341                         triangle_intersect_precalc(dir, &isect_precalc);
342
343                         object = OBJECT_NONE;
344                         nodeAddr = traversalStack[stackPtr].addr;
345                         --stackPtr;
346                 }
347 #endif  /* FEATURE(BVH_INSTANCING) */
348         } while(nodeAddr != ENTRYPOINT_SENTINEL);
349
350         return (isect->prim != PRIM_NONE);
351 }