e3a5dcffee5fad4a91b93cdfe9f2c466cf133bea
[blender.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_tfm;
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 #if defined(__KERNEL_DEBUG__)
99                                 isect->num_traversal_steps++;
100 #endif
101
102                                 ssef dist;
103                                 int traverseChild = qbvh_node_intersect(kg,
104                                                                         tnear,
105                                                                         tfar,
106 #ifdef __KERNEL_AVX2__
107                                                                         P_idir4,
108 #else
109                                                                         org,
110 #endif
111                                                                         idir4,
112                                                                         near_x, near_y, near_z,
113                                                                         far_x, far_y, far_z,
114                                                                         nodeAddr,
115                                                                         &dist);
116
117                                 if(traverseChild != 0) {
118                                         float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_QNODE_SIZE+6);
119
120                                         /* One child is hit, continue with that child. */
121                                         int r = __bscf(traverseChild);
122                                         if(traverseChild == 0) {
123                                                 nodeAddr = __float_as_int(cnodes[r]);
124                                                 continue;
125                                         }
126
127                                         /* Two children are hit, push far child, and continue with
128                                          * closer child.
129                                          */
130                                         int c0 = __float_as_int(cnodes[r]);
131                                         float d0 = ((float*)&dist)[r];
132                                         r = __bscf(traverseChild);
133                                         int c1 = __float_as_int(cnodes[r]);
134                                         float d1 = ((float*)&dist)[r];
135                                         if(traverseChild == 0) {
136                                                 if(d1 < d0) {
137                                                         nodeAddr = c1;
138                                                         ++stackPtr;
139                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
140                                                         traversalStack[stackPtr].addr = c0;
141                                                         traversalStack[stackPtr].dist = d0;
142                                                         continue;
143                                                 }
144                                                 else {
145                                                         nodeAddr = c0;
146                                                         ++stackPtr;
147                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
148                                                         traversalStack[stackPtr].addr = c1;
149                                                         traversalStack[stackPtr].dist = d1;
150                                                         continue;
151                                                 }
152                                         }
153
154                                         /* Here starts the slow path for 3 or 4 hit children. We push
155                                          * all nodes onto the stack to sort them there.
156                                          */
157                                         ++stackPtr;
158                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
159                                         traversalStack[stackPtr].addr = c1;
160                                         traversalStack[stackPtr].dist = d1;
161                                         ++stackPtr;
162                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
163                                         traversalStack[stackPtr].addr = c0;
164                                         traversalStack[stackPtr].dist = d0;
165
166                                         /* Three children are hit, push all onto stack and sort 3
167                                          * stack items, continue with closest child.
168                                          */
169                                         r = __bscf(traverseChild);
170                                         int c2 = __float_as_int(cnodes[r]);
171                                         float d2 = ((float*)&dist)[r];
172                                         if(traverseChild == 0) {
173                                                 ++stackPtr;
174                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
175                                                 traversalStack[stackPtr].addr = c2;
176                                                 traversalStack[stackPtr].dist = d2;
177                                                 qbvh_stack_sort(&traversalStack[stackPtr],
178                                                                 &traversalStack[stackPtr - 1],
179                                                                 &traversalStack[stackPtr - 2]);
180                                                 nodeAddr = traversalStack[stackPtr].addr;
181                                                 --stackPtr;
182                                                 continue;
183                                         }
184
185                                         /* Four children are hit, push all onto stack and sort 4
186                                          * stack items, continue with closest child.
187                                          */
188                                         r = __bscf(traverseChild);
189                                         int c3 = __float_as_int(cnodes[r]);
190                                         float d3 = ((float*)&dist)[r];
191                                         ++stackPtr;
192                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
193                                         traversalStack[stackPtr].addr = c3;
194                                         traversalStack[stackPtr].dist = d3;
195                                         ++stackPtr;
196                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
197                                         traversalStack[stackPtr].addr = c2;
198                                         traversalStack[stackPtr].dist = d2;
199                                         qbvh_stack_sort(&traversalStack[stackPtr],
200                                                         &traversalStack[stackPtr - 1],
201                                                         &traversalStack[stackPtr - 2],
202                                                         &traversalStack[stackPtr - 3]);
203                                 }
204
205                                 nodeAddr = traversalStack[stackPtr].addr;
206                                 --stackPtr;
207                         }
208
209                         /* If node is leaf, fetch triangle list. */
210                         if(nodeAddr < 0) {
211                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_QNODE_LEAF_SIZE);
212                                 int primAddr = __float_as_int(leaf.x);
213
214 #if BVH_FEATURE(BVH_INSTANCING)
215                                 if(primAddr >= 0) {
216 #endif
217                                         int primAddr2 = __float_as_int(leaf.y);
218                                         const uint type = __float_as_int(leaf.w);
219                                         const uint p_type = type & PRIMITIVE_ALL;
220
221                                         /* Pop. */
222                                         nodeAddr = traversalStack[stackPtr].addr;
223                                         --stackPtr;
224
225                                         /* Primitive intersection. */
226                                         switch(p_type) {
227                                                 case PRIMITIVE_TRIANGLE: {
228                                                         for(; primAddr < primAddr2; primAddr++) {
229                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
230                                                                 /* Only primitives from volume object. */
231                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
232                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
233                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
234                                                                         continue;
235                                                                 }
236                                                                 /* Intersect ray against primitive. */
237                                                                 triangle_intersect(kg, &isect_precalc, isect, P, visibility, object, primAddr);
238                                                         }
239                                                         break;
240                                                 }
241 #if BVH_FEATURE(BVH_MOTION)
242                                                 case PRIMITIVE_MOTION_TRIANGLE: {
243                                                         for(; primAddr < primAddr2; primAddr++) {
244                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
245                                                                 /* Only primitives from volume object. */
246                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
247                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
248                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
249                                                                         continue;
250                                                                 }
251                                                                 /* Intersect ray against primitive. */
252                                                                 motion_triangle_intersect(kg, isect, P, dir, ray->time, visibility, object, primAddr);
253                                                         }
254                                                         break;
255                                                 }
256 #endif
257 #if BVH_FEATURE(BVH_HAIR)
258                                                 case PRIMITIVE_CURVE:
259                                                 case PRIMITIVE_MOTION_CURVE: {
260                                                         for(; primAddr < primAddr2; primAddr++) {
261                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
262                                                                 /* Only primitives from volume object. */
263                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
264                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
265                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
266                                                                         continue;
267                                                                 }
268                                                                 /* Intersect ray against primitive. */
269                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
270                                                                         bvh_cardinal_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
271                                                                 else
272                                                                         bvh_curve_intersect(kg, isect, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
273                                                         }
274                                                         break;
275                                                 }
276 #endif
277                                         }
278                                 }
279 #if BVH_FEATURE(BVH_INSTANCING)
280                                 else {
281                                         /* Instance push. */
282                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
283                                         int object_flag = kernel_tex_fetch(__object_flag, object);
284
285                                         if(object_flag & SD_OBJECT_HAS_VOLUME) {
286
287 #if BVH_FEATURE(BVH_MOTION)
288                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_tfm);
289 #else
290                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect->t);
291 #endif
292
293                                                 if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
294                                                 if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
295                                                 if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
296                                                 tfar = ssef(isect->t);
297                                                 idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
298 #ifdef __KERNEL_AVX2__
299                                                 P_idir = P*idir;
300                                                 P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
301 #else
302                                                 org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
303 #endif
304                                                 triangle_intersect_precalc(dir, &isect_precalc);
305
306                                                 ++stackPtr;
307                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
308                                                 traversalStack[stackPtr].addr = ENTRYPOINT_SENTINEL;
309
310                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
311                                         }
312                                         else {
313                                                 /* Pop. */
314                                                 object = OBJECT_NONE;
315                                                 nodeAddr = traversalStack[stackPtr].addr;
316                                                 --stackPtr;
317                                         }
318                                 }
319                         }
320 #endif  /* FEATURE(BVH_INSTANCING) */
321                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
322
323 #if BVH_FEATURE(BVH_INSTANCING)
324                 if(stackPtr >= 0) {
325                         kernel_assert(object != OBJECT_NONE);
326
327                         /* Instance pop. */
328 #if BVH_FEATURE(BVH_MOTION)
329                         bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &isect->t, &ob_tfm);
330 #else
331                         bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &isect->t);
332 #endif
333
334                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
335                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
336                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
337                         tfar = ssef(isect->t);
338                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
339 #ifdef __KERNEL_AVX2__
340                         P_idir = P*idir;
341                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
342 #else
343                         org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
344 #endif
345                         triangle_intersect_precalc(dir, &isect_precalc);
346
347                         object = OBJECT_NONE;
348                         nodeAddr = traversalStack[stackPtr].addr;
349                         --stackPtr;
350                 }
351 #endif  /* FEATURE(BVH_INSTANCING) */
352         } while(nodeAddr != ENTRYPOINT_SENTINEL);
353
354         return (isect->prim != PRIM_NONE);
355 }