Cycles: Make all #include statements relative to cycles source directory
[blender.git] / intern / cycles / kernel / bvh / 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 "kernel/bvh/qbvh_volume_all.h"
22 #endif
23
24 #if BVH_FEATURE(BVH_HAIR)
25 #  define NODE_INTERSECT bvh_node_intersect
26 #else
27 #  define NODE_INTERSECT bvh_aligned_node_intersect
28 #endif
29
30 /* This is a template BVH traversal function for volumes, where
31  * various features can be enabled/disabled. This way we can compile optimized
32  * versions for each case without new features slowing things down.
33  *
34  * BVH_INSTANCING: object instancing
35  * BVH_MOTION: motion blur rendering
36  *
37  */
38
39 #ifndef __KERNEL_GPU__
40 ccl_device
41 #else
42 ccl_device_inline
43 #endif
44 uint BVH_FUNCTION_FULL_NAME(BVH)(KernelGlobals *kg,
45                                  const Ray *ray,
46                                  Intersection *isect_array,
47                                  const uint max_hits,
48                                  const uint visibility)
49 {
50         /* todo:
51          * - test if pushing distance on the stack helps (for non shadow rays)
52          * - separate version for shadow rays
53          * - likely and unlikely for if() statements
54          * - test restrict attribute for pointers
55          */
56
57         /* traversal stack in CUDA thread-local memory */
58         int traversal_stack[BVH_STACK_SIZE];
59         traversal_stack[0] = ENTRYPOINT_SENTINEL;
60
61         /* traversal variables in registers */
62         int stack_ptr = 0;
63         int node_addr = kernel_data.bvh.root;
64
65         /* ray parameters in registers */
66         const float tmax = ray->t;
67         float3 P = ray->P;
68         float3 dir = bvh_clamp_direction(ray->D);
69         float3 idir = bvh_inverse_direction(dir);
70         int object = OBJECT_NONE;
71         float isect_t = tmax;
72
73 #if BVH_FEATURE(BVH_MOTION)
74         Transform ob_itfm;
75 #endif
76
77 #if BVH_FEATURE(BVH_INSTANCING)
78         int num_hits_in_instance = 0;
79 #endif
80
81         uint num_hits = 0;
82         isect_array->t = tmax;
83
84 #if defined(__KERNEL_SSE2__)
85         const shuffle_swap_t shuf_identity = shuffle_swap_identity();
86         const shuffle_swap_t shuf_swap = shuffle_swap_swap();
87
88         const ssef pn = cast(ssei(0, 0, 0x80000000, 0x80000000));
89         ssef Psplat[3], idirsplat[3];
90 #  if BVH_FEATURE(BVH_HAIR)
91         ssef tnear(0.0f), tfar(isect_t);
92 #  endif
93         shuffle_swap_t shufflexyz[3];
94
95         Psplat[0] = ssef(P.x);
96         Psplat[1] = ssef(P.y);
97         Psplat[2] = ssef(P.z);
98
99         ssef tsplat(0.0f, 0.0f, -isect_t, -isect_t);
100
101         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
102 #endif  /* __KERNEL_SSE2__ */
103
104         /* traversal loop */
105         do {
106                 do {
107                         /* traverse internal nodes */
108                         while(node_addr >= 0 && node_addr != ENTRYPOINT_SENTINEL) {
109                                 int node_addr_child1, traverse_mask;
110                                 float dist[2];
111                                 float4 cnodes = kernel_tex_fetch(__bvh_nodes, node_addr+0);
112
113 #if !defined(__KERNEL_SSE2__)
114                                 traverse_mask = NODE_INTERSECT(kg,
115                                                                P,
116 #  if BVH_FEATURE(BVH_HAIR)
117                                                                dir,
118 #  endif
119                                                                idir,
120                                                                isect_t,
121                                                                node_addr,
122                                                                visibility,
123                                                                dist);
124 #else // __KERNEL_SSE2__
125                                 traverse_mask = NODE_INTERSECT(kg,
126                                                                P,
127                                                                dir,
128 #  if BVH_FEATURE(BVH_HAIR)
129                                                                tnear,
130                                                                tfar,
131 #  endif
132                                                                tsplat,
133                                                                Psplat,
134                                                                idirsplat,
135                                                                shufflexyz,
136                                                                node_addr,
137                                                                visibility,
138                                                                dist);
139 #endif // __KERNEL_SSE2__
140
141                                 node_addr = __float_as_int(cnodes.z);
142                                 node_addr_child1 = __float_as_int(cnodes.w);
143
144                                 if(traverse_mask == 3) {
145                                         /* Both children were intersected, push the farther one. */
146                                         bool is_closest_child1 = (dist[1] < dist[0]);
147                                         if(is_closest_child1) {
148                                                 int tmp = node_addr;
149                                                 node_addr = node_addr_child1;
150                                                 node_addr_child1 = tmp;
151                                         }
152
153                                         ++stack_ptr;
154                                         kernel_assert(stack_ptr < BVH_STACK_SIZE);
155                                         traversal_stack[stack_ptr] = node_addr_child1;
156                                 }
157                                 else {
158                                         /* One child was intersected. */
159                                         if(traverse_mask == 2) {
160                                                 node_addr = node_addr_child1;
161                                         }
162                                         else if(traverse_mask == 0) {
163                                                 /* Neither child was intersected. */
164                                                 node_addr = traversal_stack[stack_ptr];
165                                                 --stack_ptr;
166                                         }
167                                 }
168                         }
169
170                         /* if node is leaf, fetch triangle list */
171                         if(node_addr < 0) {
172                                 float4 leaf = kernel_tex_fetch(__bvh_leaf_nodes, (-node_addr-1));
173                                 int prim_addr = __float_as_int(leaf.x);
174
175 #if BVH_FEATURE(BVH_INSTANCING)
176                                 if(prim_addr >= 0) {
177 #endif
178                                         const int prim_addr2 = __float_as_int(leaf.y);
179                                         const uint type = __float_as_int(leaf.w);
180                                         bool hit;
181
182                                         /* pop */
183                                         node_addr = traversal_stack[stack_ptr];
184                                         --stack_ptr;
185
186                                         /* primitive intersection */
187                                         switch(type & PRIMITIVE_ALL) {
188                                                 case PRIMITIVE_TRIANGLE: {
189                                                         /* intersect ray against primitive */
190                                                         for(; prim_addr < prim_addr2; prim_addr++) {
191                                                                 kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
192                                                                 /* only primitives from volume object */
193                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, prim_addr): object;
194                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
195                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
196                                                                         continue;
197                                                                 }
198                                                                 hit = triangle_intersect(kg,
199                                                                                          isect_array,
200                                                                                          P,
201                                                                                          dir,
202                                                                                          visibility,
203                                                                                          object,
204                                                                                          prim_addr);
205                                                                 if(hit) {
206                                                                         /* Move on to next entry in intersections array. */
207                                                                         isect_array++;
208                                                                         num_hits++;
209 #if BVH_FEATURE(BVH_INSTANCING)
210                                                                         num_hits_in_instance++;
211 #endif
212                                                                         isect_array->t = isect_t;
213                                                                         if(num_hits == max_hits) {
214 #if BVH_FEATURE(BVH_INSTANCING)
215 #  if BVH_FEATURE(BVH_MOTION)
216                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
217 #  else
218                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
219                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
220 #  endif
221                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
222                                                                                         (isect_array-i-1)->t *= t_fac;
223                                                                                 }
224 #endif  /* BVH_FEATURE(BVH_INSTANCING) */
225                                                                                 return num_hits;
226                                                                         }
227                                                                 }
228                                                         }
229                                                         break;
230                                                 }
231 #if BVH_FEATURE(BVH_MOTION)
232                                                 case PRIMITIVE_MOTION_TRIANGLE: {
233                                                         /* intersect ray against primitive */
234                                                         for(; prim_addr < prim_addr2; prim_addr++) {
235                                                                 kernel_assert(kernel_tex_fetch(__prim_type, prim_addr) == type);
236                                                                 /* only primitives from volume object */
237                                                                 uint tri_object = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, prim_addr): object;
238                                                                 int object_flag = kernel_tex_fetch(__object_flag, tri_object);
239                                                                 if((object_flag & SD_OBJECT_HAS_VOLUME) == 0) {
240                                                                         continue;
241                                                                 }
242                                                                 hit = motion_triangle_intersect(kg,
243                                                                                                 isect_array,
244                                                                                                 P,
245                                                                                                 dir,
246                                                                                                 ray->time,
247                                                                                                 visibility,
248                                                                                                 object,
249                                                                                                 prim_addr);
250                                                                 if(hit) {
251                                                                         /* Move on to next entry in intersections array. */
252                                                                         isect_array++;
253                                                                         num_hits++;
254 #  if BVH_FEATURE(BVH_INSTANCING)
255                                                                         num_hits_in_instance++;
256 #  endif
257                                                                         isect_array->t = isect_t;
258                                                                         if(num_hits == max_hits) {
259 #  if BVH_FEATURE(BVH_INSTANCING)
260 #    if BVH_FEATURE(BVH_MOTION)
261                                                                                 float t_fac = 1.0f / len(transform_direction(&ob_itfm, dir));
262 #    else
263                                                                                 Transform itfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
264                                                                                 float t_fac = 1.0f / len(transform_direction(&itfm, dir));
265 #    endif
266                                                                                 for(int i = 0; i < num_hits_in_instance; i++) {
267                                                                                         (isect_array-i-1)->t *= t_fac;
268                                                                                 }
269 #  endif  /* BVH_FEATURE(BVH_INSTANCING) */
270                                                                                 return num_hits;
271                                                                         }
272                                                                 }
273                                                         }
274                                                         break;
275                                                 }
276 #endif  /* BVH_MOTION */
277                                                 default: {
278                                                         break;
279                                                 }
280                                         }
281                                 }
282 #if BVH_FEATURE(BVH_INSTANCING)
283                                 else {
284                                         /* instance push */
285                                         object = kernel_tex_fetch(__prim_object, -prim_addr-1);
286                                         int object_flag = kernel_tex_fetch(__object_flag, object);
287                                         if(object_flag & SD_OBJECT_HAS_VOLUME) {
288 #  if BVH_FEATURE(BVH_MOTION)
289                                                 isect_t = bvh_instance_motion_push(kg, object, ray, &P, &dir, &idir, isect_t, &ob_itfm);
290 #  else
291                                                 isect_t = bvh_instance_push(kg, object, ray, &P, &dir, &idir, isect_t);
292 #  endif
293
294                                                 num_hits_in_instance = 0;
295                                                 isect_array->t = isect_t;
296
297 #  if defined(__KERNEL_SSE2__)
298                                                 Psplat[0] = ssef(P.x);
299                                                 Psplat[1] = ssef(P.y);
300                                                 Psplat[2] = ssef(P.z);
301
302                                                 tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
303 #    if BVH_FEATURE(BVH_HAIR)
304                                                 tfar = ssef(isect_t);
305 #    endif
306
307                                                 gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
308 #  endif
309
310                                                 ++stack_ptr;
311                                                 kernel_assert(stack_ptr < BVH_STACK_SIZE);
312                                                 traversal_stack[stack_ptr] = ENTRYPOINT_SENTINEL;
313
314                                                 node_addr = kernel_tex_fetch(__object_node, object);
315                                         }
316                                         else {
317                                                 /* pop */
318                                                 object = OBJECT_NONE;
319                                                 node_addr = traversal_stack[stack_ptr];
320                                                 --stack_ptr;
321                                         }
322                                 }
323                         }
324 #endif  /* FEATURE(BVH_INSTANCING) */
325                 } while(node_addr != ENTRYPOINT_SENTINEL);
326
327 #if BVH_FEATURE(BVH_INSTANCING)
328                 if(stack_ptr >= 0) {
329                         kernel_assert(object != OBJECT_NONE);
330
331                         /* Instance pop. */
332                         if(num_hits_in_instance) {
333                                 float t_fac;
334 #  if BVH_FEATURE(BVH_MOTION)
335                                 bvh_instance_motion_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac, &ob_itfm);
336 #  else
337                                 bvh_instance_pop_factor(kg, object, ray, &P, &dir, &idir, &t_fac);
338 #  endif
339                                 /* Scale isect->t to adjust for instancing. */
340                                 for(int i = 0; i < num_hits_in_instance; i++) {
341                                         (isect_array-i-1)->t *= t_fac;
342                                 }
343                         }
344                         else {
345 #  if BVH_FEATURE(BVH_MOTION)
346                                 bvh_instance_motion_pop(kg, object, ray, &P, &dir, &idir, FLT_MAX, &ob_itfm);
347 #  else
348                                 bvh_instance_pop(kg, object, ray, &P, &dir, &idir, FLT_MAX);
349 #  endif
350                         }
351
352                         isect_t = tmax;
353                         isect_array->t = isect_t;
354
355 #  if defined(__KERNEL_SSE2__)
356                         Psplat[0] = ssef(P.x);
357                         Psplat[1] = ssef(P.y);
358                         Psplat[2] = ssef(P.z);
359
360                         tsplat = ssef(0.0f, 0.0f, -isect_t, -isect_t);
361 #    if BVH_FEATURE(BVH_HAIR)
362                         tfar = ssef(isect_t);
363 #    endif
364
365                         gen_idirsplat_swap(pn, shuf_identity, shuf_swap, idir, idirsplat, shufflexyz);
366 #  endif
367
368                         object = OBJECT_NONE;
369                         node_addr = traversal_stack[stack_ptr];
370                         --stack_ptr;
371                 }
372 #endif  /* FEATURE(BVH_INSTANCING) */
373         } while(node_addr != ENTRYPOINT_SENTINEL);
374
375         return num_hits;
376 }
377
378 ccl_device_inline uint BVH_FUNCTION_NAME(KernelGlobals *kg,
379                                          const Ray *ray,
380                                          Intersection *isect_array,
381                                          const uint max_hits,
382                                          const uint visibility)
383 {
384 #ifdef __QBVH__
385         if(kernel_data.bvh.use_qbvh) {
386                 return BVH_FUNCTION_FULL_NAME(QBVH)(kg,
387                                                     ray,
388                                                     isect_array,
389                                                     max_hits,
390                                                     visibility);
391         }
392         else
393 #endif
394         {
395                 kernel_assert(kernel_data.bvh.use_qbvh == false);
396                 return BVH_FUNCTION_FULL_NAME(BVH)(kg,
397                                                    ray,
398                                                    isect_array,
399                                                    max_hits,
400                                                    visibility);
401         }
402 }
403
404 #undef BVH_FUNCTION_NAME
405 #undef BVH_FUNCTION_FEATURES
406 #undef NODE_INTERSECT