Cycles / OSL:
[blender.git] / intern / cycles / kernel / kernel_bvh_traversal.h
1 /*
2  * Adapted from code Copyright 2009-2010 NVIDIA Corporation
3  * Modifications Copyright 2011, Blender Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 /* This is a template BVH traversal function, where various features can be
19  * enabled/disabled. This way we can compile optimized versions for each case
20  * without new features slowing things down.
21  *
22  * BVH_INSTANCING: object instancing
23  * BVH_HAIR: hair curve rendering
24  * BVH_HAIR_MINIMUM_WIDTH: hair curve rendering with minimum width
25  * BVH_SUBSURFACE: subsurface same object, random triangle intersection
26  * BVH_MOTION: motion blur rendering
27  *
28  */
29
30 #define FEATURE(f) (((BVH_FUNCTION_FEATURES) & (f)) != 0)
31
32 __device bool BVH_FUNCTION_NAME
33 (KernelGlobals *kg, const Ray *ray, Intersection *isect
34 #if FEATURE(BVH_SUBSURFACE)
35 , int subsurface_object, float subsurface_random
36 #else
37 , const uint visibility
38 #endif
39 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH) && !FEATURE(BVH_SUBSURFACE)
40 , uint *lcg_state, float difl, float extmax
41 #endif
42 )
43 {
44         /* traversal stack in CUDA thread-local memory */
45         int traversalStack[BVH_STACK_SIZE];
46         traversalStack[0] = ENTRYPOINT_SENTINEL;
47
48         /* traversal variables in registers */
49         int stackPtr = 0;
50         int nodeAddr = kernel_data.bvh.root;
51
52         /* ray parameters in registers */
53         const float tmax = ray->t;
54         float3 P = ray->P;
55         float3 idir = bvh_inverse_direction(ray->D);
56         int object = ~0;
57
58 #if FEATURE(BVH_SUBSURFACE)
59         const uint visibility = ~0;
60         int num_hits = 0;
61 #endif
62
63 #if FEATURE(BVH_MOTION)
64         Transform ob_tfm;
65 #endif
66
67         isect->t = tmax;
68         isect->object = ~0;
69         isect->prim = ~0;
70         isect->u = 0.0f;
71         isect->v = 0.0f;
72
73         /* traversal loop */
74         do {
75                 do
76                 {
77                         /* traverse internal nodes */
78                         while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL)
79                         {
80                                 bool traverseChild0, traverseChild1, closestChild1;
81                                 int nodeAddrChild1;
82
83 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH) && !FEATURE(BVH_SUBSURFACE)
84                                 bvh_node_intersect(kg, &traverseChild0, &traverseChild1,
85                                         &closestChild1, &nodeAddr, &nodeAddrChild1,
86                                         P, idir, isect->t, visibility, nodeAddr, difl, extmax);
87 #else
88                                 bvh_node_intersect(kg, &traverseChild0, &traverseChild1,
89                                         &closestChild1, &nodeAddr, &nodeAddrChild1,
90 #ifdef __HAIR__
91                                         P, idir, isect->t, visibility, nodeAddr, 0.0f, 0.0f);
92 #else
93                                         P, idir, isect->t, visibility, nodeAddr);
94 #endif
95 #endif
96
97                                 if(traverseChild0 != traverseChild1) {
98                                         /* one child was intersected */
99                                         if(traverseChild1) {
100                                                 nodeAddr = nodeAddrChild1;
101                                         }
102                                 }
103                                 else {
104                                         if(!traverseChild0) {
105                                                 /* neither child was intersected */
106                                                 nodeAddr = traversalStack[stackPtr];
107                                                 --stackPtr;
108                                         }
109                                         else {
110                                                 /* both children were intersected, push the farther one */
111                                                 if(closestChild1) {
112                                                         int tmp = nodeAddr;
113                                                         nodeAddr = nodeAddrChild1;
114                                                         nodeAddrChild1 = tmp;
115                                                 }
116
117                                                 ++stackPtr;
118                                                 traversalStack[stackPtr] = nodeAddrChild1;
119                                         }
120                                 }
121                         }
122
123                         /* if node is leaf, fetch triangle list */
124                         if(nodeAddr < 0) {
125                                 float4 leaf = kernel_tex_fetch(__bvh_nodes, (-nodeAddr-1)*BVH_NODE_SIZE+(BVH_NODE_SIZE-1));
126                                 int primAddr = __float_as_int(leaf.x);
127
128 #if FEATURE(BVH_INSTANCING)
129                                 if(primAddr >= 0) {
130 #endif
131                                         int primAddr2 = __float_as_int(leaf.y);
132
133                                         /* pop */
134                                         nodeAddr = traversalStack[stackPtr];
135                                         --stackPtr;
136
137                                         /* primitive intersection */
138                                         while(primAddr < primAddr2) {
139 #if FEATURE(BVH_SUBSURFACE)
140                                                 /* only primitives from the same object */
141                                                 uint tri_object = (object == ~0)? kernel_tex_fetch(__prim_object, primAddr): object;
142
143                                                 if(tri_object == subsurface_object) {
144 #endif
145
146                                                         /* intersect ray against primitive */
147 #if FEATURE(BVH_HAIR)
148                                                         uint segment = kernel_tex_fetch(__prim_segment, primAddr);
149 #if !FEATURE(BVH_SUBSURFACE)
150                                                         if(segment != ~0) {
151                                                                 if(kernel_data.curve_kernel_data.curveflags & CURVE_KN_INTERPOLATE) 
152 #if FEATURE(BVH_HAIR_MINIMUM_WIDTH)
153                                                                         bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
154                                                                 else
155                                                                         bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment, lcg_state, difl, extmax);
156 #else
157                                                                         bvh_cardinal_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
158                                                                 else
159                                                                         bvh_curve_intersect(kg, isect, P, idir, visibility, object, primAddr, segment);
160 #endif
161                                                         }
162                                                         else
163 #endif
164 #endif
165 #if FEATURE(BVH_SUBSURFACE)
166 #if FEATURE(BVH_HAIR)
167                                                         if(segment == ~0)
168 #endif
169                                                                 bvh_triangle_intersect_subsurface(kg, isect, P, idir, object, primAddr, tmax, &num_hits, subsurface_random);
170
171                                                 }
172 #else
173                                                                 bvh_triangle_intersect(kg, isect, P, idir, visibility, object, primAddr);
174
175                                                         /* shadow ray early termination */
176                                                         if(visibility == PATH_RAY_SHADOW_OPAQUE && isect->prim != ~0)
177                                                                 return true;
178 #endif
179
180                                                 primAddr++;
181                                         }
182                                 }
183 #if FEATURE(BVH_INSTANCING)
184                                 else {
185                                         /* instance push */
186 #if FEATURE(BVH_SUBSURFACE)
187                                         if(subsurface_object == kernel_tex_fetch(__prim_object, -primAddr-1)) {
188                                                 object = subsurface_object;
189 #else
190                                                 object = kernel_tex_fetch(__prim_object, -primAddr-1);
191 #endif
192
193 #if FEATURE(BVH_MOTION)
194                                                 bvh_instance_motion_push(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
195 #else
196                                                 bvh_instance_push(kg, object, ray, &P, &idir, &isect->t, tmax);
197 #endif
198
199                                                 ++stackPtr;
200                                                 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL;
201
202                                                 nodeAddr = kernel_tex_fetch(__object_node, object);
203 #if FEATURE(BVH_SUBSURFACE)
204                                         }
205                                         else {
206                                                 /* pop */
207                                                 nodeAddr = traversalStack[stackPtr];
208                                                 --stackPtr;
209                                         }
210 #endif
211                                 }
212                         }
213 #endif
214                 } while(nodeAddr != ENTRYPOINT_SENTINEL);
215
216 #if FEATURE(BVH_INSTANCING)
217                 if(stackPtr >= 0) {
218                         kernel_assert(object != ~0);
219
220                         /* instance pop */
221 #if FEATURE(BVH_MOTION)
222                         bvh_instance_motion_pop(kg, object, ray, &P, &idir, &isect->t, &ob_tfm, tmax);
223 #else
224                         bvh_instance_pop(kg, object, ray, &P, &idir, &isect->t, tmax);
225 #endif
226                         object = ~0;
227                         nodeAddr = traversalStack[stackPtr];
228                         --stackPtr;
229                 }
230 #endif
231         } while(nodeAddr != ENTRYPOINT_SENTINEL);
232
233 #if FEATURE(BVH_SUBSURFACE)
234         return (num_hits != 0);
235 #else
236         return (isect->prim != ~0);
237 #endif
238 }
239
240 #undef FEATURE
241 #undef BVH_FUNCTION_NAME
242 #undef BVH_FUNCTION_FEATURES
243