Blender V2.61 - r43446
|
00001 /* 00002 * Adapted from code Copyright 2009-2010 NVIDIA Corporation 00003 * Modifications Copyright 2011, Blender Foundation. 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); 00006 * you may not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, 00013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 00018 CCL_NAMESPACE_BEGIN 00019 00020 /* 00021 * "Persistent while-while kernel" used in: 00022 * 00023 * "Understanding the Efficiency of Ray Traversal on GPUs", 00024 * Timo Aila and Samuli Laine, 00025 * Proc. High-Performance Graphics 2009 00026 */ 00027 00028 /* bottom-most stack entry, indicating the end of traversal */ 00029 00030 #define ENTRYPOINT_SENTINEL 0x76543210 00031 /* 64 object BVH + 64 mesh BVH + 64 object node splitting */ 00032 #define QBVH_STACK_SIZE 192 00033 #define QBVH_NODE_SIZE 8 00034 #define TRI_NODE_SIZE 3 00035 00036 __device_inline float3 qbvh_inverse_direction(float3 dir) 00037 { 00038 // Avoid divide by zero (ooeps = exp2f(-80.0f)) 00039 float ooeps = 0.00000000000000000000000082718061255302767487140869206996285356581211090087890625f; 00040 float3 idir; 00041 00042 idir.x = 1.0f/((fabsf(dir.x) > ooeps)? dir.x: copysignf(ooeps, dir.x)); 00043 idir.y = 1.0f/((fabsf(dir.y) > ooeps)? dir.y: copysignf(ooeps, dir.y)); 00044 idir.z = 1.0f/((fabsf(dir.z) > ooeps)? dir.z: copysignf(ooeps, dir.z)); 00045 00046 return idir; 00047 } 00048 00049 __device_inline void qbvh_instance_push(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *idir, float *t, const float tmax) 00050 { 00051 Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM); 00052 00053 *P = transform(&tfm, ray->P); 00054 00055 float3 dir = transform_direction(&tfm, ray->D); 00056 00057 float len; 00058 dir = normalize_len(dir, &len); 00059 00060 *idir = qbvh_inverse_direction(dir); 00061 00062 if(*t != FLT_MAX) 00063 *t *= len; 00064 } 00065 00066 __device_inline void qbvh_instance_pop(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *idir, float *t, const float tmax) 00067 { 00068 Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM); 00069 00070 if(*t != FLT_MAX) 00071 *t *= len(transform_direction(&tfm, 1.0f/(*idir))); 00072 00073 *P = ray->P; 00074 *idir = qbvh_inverse_direction(ray->D); 00075 } 00076 00077 #ifdef __KERNEL_CPU__ 00078 00079 __device_inline void qbvh_node_intersect(KernelGlobals *kg, int *traverseChild, 00080 int nodeAddrChild[4], float3 P, float3 idir, float t, int nodeAddr) 00081 { 00082 /* X axis */ 00083 const __m128 bminx = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+0); 00084 const __m128 t0x = _mm_mul_ps(_mm_sub_ps(bminx, _mm_set_ps1(P.x)), _mm_set_ps1(idir.x)); 00085 const __m128 bmaxx = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+1); 00086 const __m128 t1x = _mm_mul_ps(_mm_sub_ps(bmaxx, _mm_set_ps1(P.x)), _mm_set_ps1(idir.x)); 00087 00088 __m128 tmin = _mm_max_ps(_mm_min_ps(t0x, t1x), _mm_setzero_ps()); 00089 __m128 tmax = _mm_min_ps(_mm_max_ps(t0x, t1x), _mm_set_ps1(t)); 00090 00091 /* Y axis */ 00092 const __m128 bminy = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+2); 00093 const __m128 t0y = _mm_mul_ps(_mm_sub_ps(bminy, _mm_set_ps1(P.y)), _mm_set_ps1(idir.y)); 00094 const __m128 bmaxy = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+3); 00095 const __m128 t1y = _mm_mul_ps(_mm_sub_ps(bmaxy, _mm_set_ps1(P.y)), _mm_set_ps1(idir.y)); 00096 00097 tmin = _mm_max_ps(_mm_min_ps(t0y, t1y), tmin); 00098 tmax = _mm_min_ps(_mm_max_ps(t0y, t1y), tmax); 00099 00100 /* Z axis */ 00101 const __m128 bminz = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+4); 00102 const __m128 t0z = _mm_mul_ps(_mm_sub_ps(bminz, _mm_set_ps1(P.z)), _mm_set_ps1(idir.z)); 00103 const __m128 bmaxz = kernel_tex_fetch_m128(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+5); 00104 const __m128 t1z = _mm_mul_ps(_mm_sub_ps(bmaxz, _mm_set_ps1(P.z)), _mm_set_ps1(idir.z)); 00105 00106 tmin = _mm_max_ps(_mm_min_ps(t0z, t1z), tmin); 00107 tmax = _mm_min_ps(_mm_max_ps(t0z, t1z), tmax); 00108 00109 /* compare and get mask */ 00110 *traverseChild = _mm_movemask_ps(_mm_cmple_ps(tmin, tmax)); 00111 00112 /* get node addresses */ 00113 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+6); 00114 00115 nodeAddrChild[0] = __float_as_int(cnodes.x); 00116 nodeAddrChild[1] = __float_as_int(cnodes.y); 00117 nodeAddrChild[2] = __float_as_int(cnodes.z); 00118 nodeAddrChild[3] = __float_as_int(cnodes.w); 00119 } 00120 00121 #else 00122 00123 __device_inline bool qbvh_bb_intersect(float3 bmin, float3 bmax, float3 P, float3 idir, float t) 00124 { 00125 float t0x = (bmin.x - P.x)*idir.x; 00126 float t1x = (bmax.x - P.x)*idir.x; 00127 float t0y = (bmin.y - P.y)*idir.y; 00128 float t1y = (bmax.y - P.y)*idir.y; 00129 float t0z = (bmin.z - P.z)*idir.z; 00130 float t1z = (bmax.z - P.z)*idir.z; 00131 00132 float minx = min(t0x, t1x); 00133 float maxx = max(t0x, t1x); 00134 float miny = min(t0y, t1y); 00135 float maxy = max(t0y, t1y); 00136 float minz = min(t0z, t1z); 00137 float maxz = max(t0z, t1z); 00138 00139 float tmin = max4(0.0f, minx, miny, minz); 00140 float tmax = min4(t, maxx, maxy, maxz); 00141 00142 return (tmin <= tmax); 00143 } 00144 00145 /* intersect four bounding boxes */ 00146 __device_inline void qbvh_node_intersect(KernelGlobals *kg, int *traverseChild, 00147 int nodeAddrChild[4], float3 P, float3 idir, float t, int nodeAddr) 00148 { 00149 /* fetch node data */ 00150 float4 minx = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+0); 00151 float4 miny = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+2); 00152 float4 minz = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+4); 00153 float4 maxx = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+1); 00154 float4 maxy = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+3); 00155 float4 maxz = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+5); 00156 00157 /* intersect bounding boxes */ 00158 bool traverseChild0 = qbvh_bb_intersect(make_float3(minx.x, miny.x, minz.x), make_float3(maxx.x, maxy.x, maxz.x), P, idir, t); 00159 bool traverseChild1 = qbvh_bb_intersect(make_float3(minx.y, miny.y, minz.y), make_float3(maxx.y, maxy.y, maxz.y), P, idir, t); 00160 bool traverseChild2 = qbvh_bb_intersect(make_float3(minx.z, miny.z, minz.z), make_float3(maxx.z, maxy.z, maxz.z), P, idir, t); 00161 bool traverseChild3 = qbvh_bb_intersect(make_float3(minx.w, miny.w, minz.w), make_float3(maxx.w, maxy.w, maxz.w), P, idir, t); 00162 00163 *traverseChild = 0; 00164 if(traverseChild0) *traverseChild |= 1; 00165 if(traverseChild1) *traverseChild |= 2; 00166 if(traverseChild2) *traverseChild |= 4; 00167 if(traverseChild3) *traverseChild |= 8; 00168 00169 /* get node addresses */ 00170 float4 cnodes = kernel_tex_fetch(__bvh_nodes, nodeAddr*QBVH_NODE_SIZE+6); 00171 00172 nodeAddrChild[0] = __float_as_int(cnodes.x); 00173 nodeAddrChild[1] = __float_as_int(cnodes.y); 00174 nodeAddrChild[2] = __float_as_int(cnodes.z); 00175 nodeAddrChild[3] = __float_as_int(cnodes.w); 00176 } 00177 00178 #endif 00179 00180 /* Sven Woop's algorithm */ 00181 __device_inline void qbvh_triangle_intersect(KernelGlobals *kg, Intersection *isect, float3 P, float3 idir, int object, int triAddr) 00182 { 00183 /* compute and check intersection t-value */ 00184 float4 v00 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+0); 00185 float4 v11 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+1); 00186 float3 dir = 1.0f/idir; 00187 00188 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z; 00189 float invDz = 1.0f/(dir.x*v00.x + dir.y*v00.y + dir.z*v00.z); 00190 float t = Oz * invDz; 00191 00192 if(t > 0.0f && t < isect->t) { 00193 /* compute and check barycentric u */ 00194 float Ox = v11.w + P.x*v11.x + P.y*v11.y + P.z*v11.z; 00195 float Dx = dir.x*v11.x + dir.y*v11.y + dir.z*v11.z; 00196 float u = Ox + t*Dx; 00197 00198 if(u >= 0.0f) { 00199 /* compute and check barycentric v */ 00200 float4 v22 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+2); 00201 float Oy = v22.w + P.x*v22.x + P.y*v22.y + P.z*v22.z; 00202 float Dy = dir.x*v22.x + dir.y*v22.y + dir.z*v22.z; 00203 float v = Oy + t*Dy; 00204 00205 if(v >= 0.0f && u + v <= 1.0f) { 00206 /* record intersection */ 00207 isect->prim = triAddr; 00208 isect->object = object; 00209 isect->u = u; 00210 isect->v = v; 00211 isect->t = t; 00212 } 00213 } 00214 } 00215 } 00216 00217 __device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const bool isshadowray, Intersection *isect) 00218 { 00219 /* traversal stack in CUDA thread-local memory */ 00220 int traversalStack[QBVH_STACK_SIZE]; 00221 traversalStack[0] = ENTRYPOINT_SENTINEL; 00222 00223 /* traversal variables in registers */ 00224 int stackPtr = 0; 00225 int nodeAddr = kernel_data.bvh.root; 00226 00227 /* ray parameters in registers */ 00228 const float tmax = ray->t; 00229 float3 P = ray->P; 00230 float3 idir = qbvh_inverse_direction(ray->D); 00231 int object = ~0; 00232 00233 isect->t = tmax; 00234 isect->object = ~0; 00235 isect->prim = ~0; 00236 isect->u = 0.0f; 00237 isect->v = 0.0f; 00238 00239 /* traversal loop */ 00240 do { 00241 do 00242 { 00243 /* traverse internal nodes */ 00244 while(nodeAddr >= 0 && nodeAddr != ENTRYPOINT_SENTINEL) 00245 { 00246 int traverseChild, nodeAddrChild[4]; 00247 00248 qbvh_node_intersect(kg, &traverseChild, nodeAddrChild, 00249 P, idir, isect->t, nodeAddr); 00250 00251 if(traverseChild & 1) { 00252 ++stackPtr; 00253 traversalStack[stackPtr] = nodeAddrChild[0]; 00254 } 00255 00256 if(traverseChild & 2) { 00257 ++stackPtr; 00258 traversalStack[stackPtr] = nodeAddrChild[1]; 00259 } 00260 if(traverseChild & 4) { 00261 ++stackPtr; 00262 traversalStack[stackPtr] = nodeAddrChild[2]; 00263 } 00264 00265 if(traverseChild & 8) { 00266 ++stackPtr; 00267 traversalStack[stackPtr] = nodeAddrChild[3]; 00268 } 00269 00270 nodeAddr = traversalStack[stackPtr]; 00271 --stackPtr; 00272 } 00273 00274 /* if node is leaf, fetch triangle list */ 00275 if(nodeAddr < 0) { 00276 float4 leaf = kernel_tex_fetch(__bvh_nodes, (-nodeAddr-1)*QBVH_NODE_SIZE+(QBVH_NODE_SIZE-2)); 00277 int primAddr = __float_as_int(leaf.x); 00278 00279 #ifdef __INSTANCING__ 00280 if(primAddr >= 0) { 00281 #endif 00282 int primAddr2 = __float_as_int(leaf.y); 00283 00284 /* pop */ 00285 nodeAddr = traversalStack[stackPtr]; 00286 --stackPtr; 00287 00288 /* triangle intersection */ 00289 while(primAddr < primAddr2) { 00290 /* intersect ray against triangle */ 00291 qbvh_triangle_intersect(kg, isect, P, idir, object, primAddr); 00292 00293 /* shadow ray early termination */ 00294 if(isshadowray && isect->prim != ~0) 00295 return true; 00296 00297 primAddr++; 00298 } 00299 #ifdef __INSTANCING__ 00300 } 00301 else { 00302 /* instance push */ 00303 object = kernel_tex_fetch(__prim_object, -primAddr-1); 00304 00305 qbvh_instance_push(kg, object, ray, &P, &idir, &isect->t, tmax); 00306 00307 ++stackPtr; 00308 traversalStack[stackPtr] = ENTRYPOINT_SENTINEL; 00309 00310 nodeAddr = kernel_tex_fetch(__object_node, object); 00311 } 00312 #endif 00313 } 00314 } while(nodeAddr != ENTRYPOINT_SENTINEL); 00315 00316 #ifdef __INSTANCING__ 00317 if(stackPtr >= 0) { 00318 kernel_assert(object != ~0); 00319 00320 /* instance pop */ 00321 qbvh_instance_pop(kg, object, ray, &P, &idir, &isect->t, tmax); 00322 object = ~0; 00323 nodeAddr = traversalStack[stackPtr]; 00324 --stackPtr; 00325 } 00326 #endif 00327 } while(nodeAddr != ENTRYPOINT_SENTINEL); 00328 00329 return (isect->prim != ~0); 00330 } 00331 00332 __device_inline float3 ray_offset(float3 P, float3 Ng) 00333 { 00334 #ifdef __INTERSECTION_REFINE__ 00335 const float epsilon_f = 1e-5f; 00336 const int epsilon_i = 32; 00337 00338 float3 res; 00339 00340 /* x component */ 00341 if(fabsf(P.x) < epsilon_f) { 00342 res.x = P.x + Ng.x*epsilon_f; 00343 } 00344 else { 00345 uint ix = __float_as_uint(P.x); 00346 ix += ((ix ^ __float_as_uint(Ng.x)) >> 31)? -epsilon_i: epsilon_i; 00347 res.x = __uint_as_float(ix); 00348 } 00349 00350 /* y component */ 00351 if(fabsf(P.y) < epsilon_f) { 00352 res.y = P.y + Ng.y*epsilon_f; 00353 } 00354 else { 00355 uint iy = __float_as_uint(P.y); 00356 iy += ((iy ^ __float_as_uint(Ng.y)) >> 31)? -epsilon_i: epsilon_i; 00357 res.y = __uint_as_float(iy); 00358 } 00359 00360 /* z component */ 00361 if(fabsf(P.z) < epsilon_f) { 00362 res.z = P.z + Ng.z*epsilon_f; 00363 } 00364 else { 00365 uint iz = __float_as_uint(P.z); 00366 iz += ((iz ^ __float_as_uint(Ng.z)) >> 31)? -epsilon_i: epsilon_i; 00367 res.z = __uint_as_float(iz); 00368 } 00369 00370 return res; 00371 #else 00372 const float epsilon_f = 1e-4f; 00373 return P + epsilon_f*Ng; 00374 #endif 00375 } 00376 00377 __device_inline float3 bvh_triangle_refine(KernelGlobals *kg, const Intersection *isect, const Ray *ray) 00378 { 00379 float3 P = ray->P; 00380 float3 D = ray->D; 00381 float t = isect->t; 00382 00383 #ifdef __INTERSECTION_REFINE__ 00384 if(isect->object != ~0) { 00385 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM); 00386 00387 P = transform(&tfm, P); 00388 D = transform_direction(&tfm, D*t); 00389 D = normalize_len(D, &t); 00390 } 00391 00392 P = P + D*t; 00393 00394 float4 v00 = kernel_tex_fetch(__tri_woop, isect->prim*TRI_NODE_SIZE+0); 00395 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z; 00396 float invDz = 1.0f/(D.x*v00.x + D.y*v00.y + D.z*v00.z); 00397 float rt = Oz * invDz; 00398 00399 P = P + D*rt; 00400 00401 if(isect->object != ~0) { 00402 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM); 00403 P = transform(&tfm, P); 00404 } 00405 00406 return P; 00407 #else 00408 return P + D*t; 00409 #endif 00410 } 00411 00412 CCL_NAMESPACE_END 00413