Cycles: Fix wrong camera in volume check when domain is only visible to camera rays
[blender-staging.git] / intern / cycles / kernel / geom / geom_qbvh_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 /* 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 uint BVH_FUNCTION_FULL_NAME(QBVH)(KernelGlobals *kg,
31                                              const Ray *ray,
32                                              Intersection *isect_array,
33                                              const uint max_hits,
34                                              const uint visibility)
35 {
36         /* TODO(sergey):
37          * - Test if pushing distance on the stack helps.
38          * - Likely and unlikely for if() statements.
39          * - Test restrict attribute for pointers.
40          */
41
42         /* Traversal stack in CUDA thread-local memory. */
43         QBVHStackItem traversalStack[BVH_QSTACK_SIZE];
44         traversalStack[0].addr = ENTRYPOINT_SENTINEL;
45
46         /* Traversal variables in registers. */
47         int stackPtr = 0;
48         int nodeAddr = kernel_data.bvh.root;
49
50         /* Ray parameters in registers. */
51         const float tmax = ray->t;
52         float3 P = ray->P;
53         float3 dir = bvh_clamp_direction(ray->D);
54         float3 idir = bvh_inverse_direction(dir);
55         int object = OBJECT_NONE;
56         float isect_t = tmax;
57
58 #if BVH_FEATURE(BVH_MOTION)
59         Transform ob_itfm;
60 #endif
61
62         uint num_hits = 0;
63         isect_array->t = tmax;
64
65 #ifndef __KERNEL_SSE41__
66         if(!isfinite(P.x)) {
67                 return false;
68         }
69 #endif
70
71 #if BVH_FEATURE(BVH_INSTANCING)
72         int num_hits_in_instance = 0;
73 #endif
74
75         ssef tnear(0.0f), tfar(isect_t);
76         sse3f idir4(ssef(idir.x), ssef(idir.y), ssef(idir.z));
77
78 #ifdef __KERNEL_AVX2__
79         float3 P_idir = P*idir;
80         sse3f P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
81 #else
82         sse3f org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
83 #endif
84
85         /* Offsets to select the side that becomes the lower or upper bound. */
86         int near_x, near_y, near_z;
87         int far_x, far_y, far_z;
88
89         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
90         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
91         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
92
93         IsectPrecalc isect_precalc;
94         triangle_intersect_precalc(dir, &isect_precalc);
95
96         /* Traversal loop. */
97         do {
98                 do {
99                         /* Traverse internal nodes. */
100                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) {
101                                 ssef dist;
102                                 int traverseChild = qbvh_node_intersect(kg,
103                                                                         tnear,
104                                                                         tfar,
105 #ifdef __KERNEL_AVX2__
106                                                                         P_idir4,
107 #else
108                                                                         org,
109 #endif
110                                                                         idir4,
111                                                                         near_x, near_y, near_z,
112                                                                         far_x, far_y, far_z,
113                                                                         nodeAddr,
114                                                                         &dist);
115
116                                 if(traverseChild != 0) {
117                                         float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*BVH_QNODE_SIZE+6);
118
119                                         /* One child is hit, continue with that child. */
120                                         int r = __bscf(traverseChild);
121                                         if(traverseChild == 0) {
122                                                 nodeAddr = __float_as_int(cnodes[r]);
123                                                 continue;
124                                         }
125
126                                         /* Two children are hit, push far child, and continue with
127                                          * closer child.
128                                          */
129                                         int c0 = __float_as_int(cnodes[r]);
130                                         float d0 = ((float*)&dist)[r];
131                                         r = __bscf(traverseChild);
132                                         int c1 = __float_as_int(cnodes[r]);
133                                         float d1 = ((float*)&dist)[r];
134                                         if(traverseChild == 0) {
135                                                 if(d1 < d0) {
136                                                         nodeAddr = c1;
137                                                         ++stackPtr;
138                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
139                                                         traversalStack[stackPtr].addr = c0;
140                                                         traversalStack[stackPtr].dist = d0;
141                                                         continue;
142                                                 }
143                                                 else {
144                                                         nodeAddr = c0;
145                                                         ++stackPtr;
146                                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
147                                                         traversalStack[stackPtr].addr = c1;
148                                                         traversalStack[stackPtr].dist = d1;
149                                                         continue;
150                                                 }
151                                         }
152
153                                         /* Here starts the slow path for 3 or 4 hit children. We push
154                                          * all nodes onto the stack to sort them there.
155                                          */
156                                         ++stackPtr;
157                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
158                                         traversalStack[stackPtr].addr = c1;
159                                         traversalStack[stackPtr].dist = d1;
160                                         ++stackPtr;
161                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
162                                         traversalStack[stackPtr].addr = c0;
163                                         traversalStack[stackPtr].dist = d0;
164
165                                         /* Three children are hit, push all onto stack and sort 3
166                                          * stack items, continue with closest child.
167                                          */
168                                         r = __bscf(traverseChild);
169                                         int c2 = __float_as_int(cnodes[r]);
170                                         float d2 = ((float*)&dist)[r];
171                                         if(traverseChild == 0) {
172                                                 ++stackPtr;
173                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
174                                                 traversalStack[stackPtr].addr = c2;
175                                                 traversalStack[stackPtr].dist = d2;
176                                                 qbvh_stack_sort(&traversalStack[stackPtr],
177                                                                 &traversalStack[stackPtr - 1],
178                                                                 &traversalStack[stackPtr - 2]);
179                                                 nodeAddr = traversalStack[stackPtr].addr;
180                                                 --stackPtr;
181                                                 continue;
182                                         }
183
184                                         /* Four children are hit, push all onto stack and sort 4
185                                          * stack items, continue with closest child.
186                                          */
187                                         r = __bscf(traverseChild);
188                                         int c3 = __float_as_int(cnodes[r]);
189                                         float d3 = ((float*)&dist)[r];
190                                         ++stackPtr;
191                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
192                                         traversalStack[stackPtr].addr = c3;
193                                         traversalStack[stackPtr].dist = d3;
194                                         ++stackPtr;
195                                         kernel_assert(stackPtr < BVH_QSTACK_SIZE);
196                                         traversalStack[stackPtr].addr = c2;
197                                         traversalStack[stackPtr].dist = d2;
198                                         qbvh_stack_sort(&traversalStack[stackPtr],
199                                                         &traversalStack[stackPtr - 1],
200                                                         &traversalStack[stackPtr - 2],
201                                                         &traversalStack[stackPtr - 3]);
202                                 }
203
204                                 nodeAddr = traversalStack[stackPtr].addr;
205                                 --stackPtr;
206                         }
207
208                         /* If node is leaf, fetch triangle list. */
209                         if(nodeAddr < 0) {
210                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-nodeAddr-1)*BVH_QNODE_LEAF_SIZE);
211                                 int primAddr = __float_as_int(leaf.x);
212
213 #if BVH_FEATURE(BVH_INSTANCING)
214                                 if(primAddr >= 0) {
215 #endif
216                                         int primAddr2 = __float_as_int(leaf.y);
217                                         const uint type = __float_as_int(leaf.w);
218                                         const uint p_type = type & PRIMITIVE_ALL;
219                                         bool hit;
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                                                                 hit = triangle_intersect(kg, &isect_precalc, isect_array, P, visibility, object, primAddr);
238                                                                 if(hit) {
239                                                                         /* Move on to next entry in intersections array. */
240                                                                         isect_array++;
241                                                                         num_hits++;
242 #if BVH_FEATURE(BVH_INSTANCING)
243                                                                         num_hits_in_instance++;
244 #endif
245                                                                         isect_array->t = isect_t;
246                                                                         if(num_hits == max_hits) {
247 #if BVH_FEATURE(BVH_INSTANCING)
248 #  if BVH_FEATURE(BVH_MOTION)
249                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
250 #  else
251                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
252                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
253 #  endif
254                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
255                                                                                         (isect_array-i-1)->t *= t_fac;
256                                                                                 }
257 #endif  /* BVH_FEATURE(BVH_INSTANCING) */
258                                                                                 return num_hits;
259                                                                         }
260                                                                 }
261                                                         }
262                                                         break;
263                                                 }
264 #if BVH_FEATURE(BVH_MOTION)
265                                                 case PRIMITIVE_MOTION_TRIANGLE: {
266                                                         for(; primAddr < primAddr2; primAddr++) {
267                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
268                                                                 /* Only primitives from volume object. */
269                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
270                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
271                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
272                                                                         continue;
273                                                                 }
274                                                                 /* Intersect ray against primitive. */
275                                                                 hit = motion_triangle_intersect(kg, isect_array, P, dir, ray->time, visibility, object, primAddr);
276                                                                 if(hit) {
277                                                                         /* Move on to next entry in intersections array. */
278                                                                         isect_array++;
279                                                                         num_hits++;
280 #  if BVH_FEATURE(BVH_INSTANCING)
281                                                                         num_hits_in_instance++;
282 #  endif
283                                                                         isect_array->t = isect_t;
284                                                                         if(num_hits == max_hits) {
285 #  if BVH_FEATURE(BVH_INSTANCING)
286 #    if BVH_FEATURE(BVH_MOTION)
287                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
288 #    else
289                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
290                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
291 #    endif
292                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
293                                                                                         (isect_array-i-1)->t *= t_fac;
294                                                                                 }
295 #  endif  /* BVH_FEATURE(BVH_INSTANCING) */
296                                                                                 return num_hits;
297                                                                         }
298                                                                 }
299                                                         }
300                                                         break;
301                                                 }
302 #endif
303 #if BVH_FEATURE(BVH_HAIR)
304                                                 case PRIMITIVE_CURVE:
305                                                 case PRIMITIVE_MOTION_CURVE: {
306                                                         for(; primAddr < primAddr2; primAddr++) {
307                                                                 kernel_assert(kernel_tex_fetch(__prim_type, primAddr) == type);
308                                                                 /* Only primitives from volume object. */
309                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, primAddr): object;
310                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
311                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
312                                                                         continue;
313                                                                 }
314                                                                 /* Intersect ray against primitive. */
315                                                                 if(kernel_data.curve.curveflags & CURVE_KN_INTERPOLATE)
316                                                                         hit = bvh_cardinal_curve_intersect(kg, isect_array, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
317                                                                 else
318                                                                         hit = bvh_curve_intersect(kg, isect_array, P, dir, visibility, object, primAddr, ray->time, type, NULL, 0, 0);
319                                                                 if(hit) {
320                                                                         /* Move on to next entry in intersections array. */
321                                                                         isect_array++;
322                                                                         num_hits++;
323 #  if BVH_FEATURE(BVH_INSTANCING)
324                                                                         num_hits_in_instance++;
325 #  endif
326                                                                         isect_array->t = isect_t;
327                                                                         if(num_hits == max_hits) {
328 #  if BVH_FEATURE(BVH_INSTANCING)
329 #    if BVH_FEATURE(BVH_MOTION)
330                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
331 #    else
332                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
333                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
334 #    endif
335                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
336                                                                                         (isect_array-i-1)->t *= t_fac;
337                                                                                 }
338 #  endif  /* BVH_FEATURE(BVH_INSTANCING) */
339                                                                                 return num_hits;
340                                                                         }
341                                                                 }
342                                                         }
343                                                         break;
344                                                 }
345 #endif  /* BVH_HAIR */
346                                         }
347                                 }
348 #if BVH_FEATURE(BVH_INSTANCING)
349                                 else {
350                                         /* Instance push. */
351                                         object = kernel_tex_fetch(__prim_object, -primAddr-1);
352                                         int object_flag = kernel_tex_fetch(__object_flag, object);
353
354                                         if(object_flag & SD_OBJECT_HAS_VOLUME) {
355
356 #  if BVH_FEATURE(BVH_MOTION)
357                                                 bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, &isect_t, &ob_itfm);
358 #  else
359                                                 bvh_instance_push(kg, object, ray, &P, &dir, &idir, &isect_t);
360 #  endif
361
362                                                 if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
363                                                 if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
364                                                 if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
365                                                 tfar = ssef(isect_t);
366                                                 idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
367 #  ifdef __KERNEL_AVX2__
368                                                 P_idir = P*idir;
369                                                 P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
370 #  else
371                                                 org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
372 #  endif
373                                                 triangle_intersect_precalc(dir, &isect_precalc);
374                                                 num_hits_in_instance = 0;
375                                                 isect_array->t = isect_t;
376
377                                                 ++stackPtr;
378                                                 kernel_assert(stackPtr < BVH_QSTACK_SIZE);
379                                                 traversalStack[stackPtr].addr = ENTRYPOINT_SENTINEL;
380
381                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
382                                         }
383                                         else {
384                                                 /* Pop. */
385                                                 object = OBJECT_NONE;
386                                                 nodeAddr = traversalStack[stackPtr].addr;
387                                                 --stackPtr;
388                                         }
389                                 }
390                         }
391 #endif  /* FEATURE(BVH_INSTANCING) */
392                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
393
394 #if BVH_FEATURE(BVH_INSTANCING)
395                 if(stackPtr >= 0) {
396                         kernel_assert(object != OBJECT_NONE);
397
398                         /* Instance pop. */
399                         if(num_hits_in_instance) {
400                                 float t_fac;
401 #  if BVH_FEATURE(BVH_MOTION)
402                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
403 #  else
404                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
405 #  endif
406                                 triangle_intersect_precalc(dir, &isect_precalc);
407                                 /* Scale isect->t to adjust for instancing. */
408                                 for(int i = 0; i < num_hits_in_instance; i++) {
409                                         (isect_array-i-1)->t *= t_fac;
410                                 }
411                         }
412                         else {
413                                 float ignore_t = FLT_MAX;
414 #  if BVH_FEATURE(BVH_MOTION)
415                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, &ignore_t, &ob_itfm);
416 #  else
417                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, &ignore_t);
418 #  endif
419                                 triangle_intersect_precalc(dir, &isect_precalc);
420                         }
421
422                         if(idir.x >= 0.0f) { near_x = 0; far_x = 1; } else { near_x = 1; far_x = 0; }
423                         if(idir.y >= 0.0f) { near_y = 2; far_y = 3; } else { near_y = 3; far_y = 2; }
424                         if(idir.z >= 0.0f) { near_z = 4; far_z = 5; } else { near_z = 5; far_z = 4; }
425                         tfar = ssef(isect_t);
426                         idir4 = sse3f(ssef(idir.x), ssef(idir.y), ssef(idir.z));
427 #  ifdef __KERNEL_AVX2__
428                         P_idir = P*idir;
429                         P_idir4 = sse3f(P_idir.x, P_idir.y, P_idir.z);
430 #  else
431                         org = sse3f(ssef(P.x), ssef(P.y), ssef(P.z));
432 #  endif
433                         triangle_intersect_precalc(dir, &isect_precalc);
434                         isect_t = tmax;
435                         isect_array->t = isect_t;
436
437                         object = OBJECT_NONE;
438                         nodeAddr = traversalStack[stackPtr].addr;
439                         --stackPtr;
440                 }
441 #endif  /* FEATURE(BVH_INSTANCING) */
442         } while(nodeAddr != ENTRYPOINT_SENTINEL);
443
444         return num_hits;
445 }