2 * Adapted from code Copyright 2009-2010 NVIDIA Corporation
3 * Modifications Copyright 2011, Blender Foundation.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * "Persistent while-while kernel" used in:
23 * "Understanding the Efficiency of Ray Traversal on GPUs",
24 * Timo Aila and Samuli Laine,
25 * Proc. High-Performance Graphics 2009
28 /* bottom-most stack entry, indicating the end of traversal */
30 #define ENTRYPOINT_SENTINEL 0x76543210
31 /* 64 object BVH + 64 mesh BVH + 64 object node splitting */
32 #define BVH_STACK_SIZE 192
33 #define BVH_NODE_SIZE 4
34 #define TRI_NODE_SIZE 3
36 /* silly workaround for float extended precision that happens when compiling
37 * without sse support on x86, it results in different results for float ops
38 * that you would otherwise expect to compare correctly */
39 #if !defined(__i386__) || defined(__SSE__)
40 #define NO_EXTENDED_PRECISION
42 #define NO_EXTENDED_PRECISION volatile
45 __device_inline float3 bvh_inverse_direction(float3 dir)
47 /* avoid divide by zero (ooeps = exp2f(-80.0f)) */
48 float ooeps = 0.00000000000000000000000082718061255302767487140869206996285356581211090087890625f;
51 idir.x = 1.0f/((fabsf(dir.x) > ooeps)? dir.x: copysignf(ooeps, dir.x));
52 idir.y = 1.0f/((fabsf(dir.y) > ooeps)? dir.y: copysignf(ooeps, dir.y));
53 idir.z = 1.0f/((fabsf(dir.z) > ooeps)? dir.z: copysignf(ooeps, dir.z));
58 __device_inline void bvh_instance_push(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *idir, float *t, const float tmax)
60 Transform tfm = object_fetch_transform(kg, object, OBJECT_INVERSE_TRANSFORM);
62 *P = transform_point(&tfm, ray->P);
64 float3 dir = transform_direction(&tfm, ray->D);
67 dir = normalize_len(dir, &len);
69 *idir = bvh_inverse_direction(dir);
75 __device_inline void bvh_instance_pop(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *idir, float *t, const float tmax)
78 Transform tfm = object_fetch_transform(kg, object, OBJECT_TRANSFORM);
79 *t *= len(transform_direction(&tfm, 1.0f/(*idir)));
83 *idir = bvh_inverse_direction(ray->D);
86 #ifdef __OBJECT_MOTION__
87 __device_inline void bvh_instance_motion_push(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *idir, float *t, Transform *tfm, const float tmax)
90 *tfm = object_fetch_transform_motion_test(kg, object, ray->time, &itfm);
92 *P = transform_point(&itfm, ray->P);
94 float3 dir = transform_direction(&itfm, ray->D);
97 dir = normalize_len(dir, &len);
99 *idir = bvh_inverse_direction(dir);
105 __device_inline void bvh_instance_motion_pop(KernelGlobals *kg, int object, const Ray *ray, float3 *P, float3 *idir, float *t, Transform *tfm, const float tmax)
108 *t *= len(transform_direction(tfm, 1.0f/(*idir)));
111 *idir = bvh_inverse_direction(ray->D);
115 /* Sven Woop's algorithm */
116 __device_inline bool bvh_triangle_intersect(KernelGlobals *kg, Intersection *isect,
117 float3 P, float3 idir, uint visibility, int object, int triAddr)
119 /* compute and check intersection t-value */
120 float4 v00 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+0);
121 float4 v11 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+1);
122 float3 dir = 1.0f/idir;
124 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z;
125 float invDz = 1.0f/(dir.x*v00.x + dir.y*v00.y + dir.z*v00.z);
126 float t = Oz * invDz;
128 if(t > 0.0f && t < isect->t) {
129 /* compute and check barycentric u */
130 float Ox = v11.w + P.x*v11.x + P.y*v11.y + P.z*v11.z;
131 float Dx = dir.x*v11.x + dir.y*v11.y + dir.z*v11.z;
135 /* compute and check barycentric v */
136 float4 v22 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+2);
137 float Oy = v22.w + P.x*v22.x + P.y*v22.y + P.z*v22.z;
138 float Dy = dir.x*v22.x + dir.y*v22.y + dir.z*v22.z;
141 if(v >= 0.0f && u + v <= 1.0f) {
142 #ifdef __VISIBILITY_FLAG__
143 /* visibility flag test. we do it here under the assumption
144 * that most triangles are culled by node flags */
145 if(kernel_tex_fetch(__prim_visibility, triAddr) & visibility)
148 /* record intersection */
149 isect->prim = triAddr;
150 isect->object = object;
164 __device_inline void curvebounds(float *lower, float *upper, float *extremta, float *extrema, float *extremtb, float *extremb, float p0, float p1, float p2, float p3)
166 float halfdiscroot = (p2 * p2 - 3 * p3 * p1);
172 *lower = p0 + p1 + p2 + p3;
175 if(*lower >= *upper) {
180 if(halfdiscroot >= 0) {
181 halfdiscroot = sqrt(halfdiscroot);
182 ta = (-p2 - halfdiscroot) / (3 * p3);
183 tb = (-p2 + halfdiscroot) / (3 * p3);
188 if(ta > 0.0f && ta < 1.0f) {
192 *extrema = p3 * t3 + p2 * t2 + p1 * ta + p0;
193 if(*extrema > *upper) {
196 if(*extrema < *lower) {
200 if(tb > 0.0f && tb < 1.0f) {
204 *extremb = p3 * t3 + p2 * t2 + p1 * tb + p0;
205 if(*extremb >= *upper) {
208 if(*extremb <= *lower) {
214 __device_inline bool bvh_cardinal_curve_intersect(KernelGlobals *kg, Intersection *isect,
215 float3 P, float3 idir, uint visibility, int object, int curveAddr, int segment, uint *lcg_state, float difl, float extmax)
217 float epsilon = 0.0f;
218 int depth = kernel_data.curve.subdivisions;
220 /* curve Intersection check */
221 float3 dir = 1.0f/idir;
223 int flags = kernel_data.curve.curveflags;
225 int prim = kernel_tex_fetch(__prim_index, curveAddr);
227 float3 curve_coef[4];
230 /*obtain curve parameters*/
232 /*ray transform created - this should be created at beginning of intersection loop*/
234 float d = sqrtf(dir.x * dir.x + dir.z * dir.z);
235 htfm = make_transform(
236 dir.z / d, 0, -dir.x /d, 0,
237 -dir.x * dir.y /d, d, -dir.y * dir.z /d, 0,
238 dir.x, dir.y, dir.z, 0,
241 float4 v00 = kernel_tex_fetch(__curves, prim);
243 int k0 = __float_as_int(v00.x) + segment;
246 int ka = max(k0 - 1,__float_as_int(v00.x));
247 int kb = min(k1 + 1,__float_as_int(v00.x) + __float_as_int(v00.y) - 1);
249 float4 P0 = kernel_tex_fetch(__curve_keys, ka);
250 float4 P1 = kernel_tex_fetch(__curve_keys, k0);
251 float4 P2 = kernel_tex_fetch(__curve_keys, k1);
252 float4 P3 = kernel_tex_fetch(__curve_keys, kb);
254 float3 p0 = transform_point(&htfm, float4_to_float3(P0) - P);
255 float3 p1 = transform_point(&htfm, float4_to_float3(P1) - P);
256 float3 p2 = transform_point(&htfm, float4_to_float3(P2) - P);
257 float3 p3 = transform_point(&htfm, float4_to_float3(P3) - P);
261 curve_coef[1] = -fc*p0 + fc*p2;
262 curve_coef[2] = 2.0f * fc * p0 + (fc - 3.0f) * p1 + (3.0f - 2.0f * fc) * p2 - fc * p3;
263 curve_coef[3] = -fc * p0 + (2.0f - fc) * p1 + (fc - 2.0f) * p2 + fc * p3;
269 float r_curr = max(r_st, r_en);
271 if((flags & CURVE_KN_RIBBONS) || !(flags & CURVE_KN_BACKFACING))
272 epsilon = 2 * r_curr;
274 /*find bounds - this is slow for cubic curves*/
278 curvebounds(&lower, &upper, &zextrem[0], &zextrem[1], &zextrem[2], &zextrem[3], curve_coef[0].z, curve_coef[1].z, curve_coef[2].z, curve_coef[3].z);
279 if(lower - r_curr > isect->t || upper + r_curr < epsilon)
282 /*minimum width extension*/
283 float mw_extension = min(difl * fabsf(upper), extmax);
284 float r_ext = mw_extension + r_curr;
287 curvebounds(&lower, &upper, &xextrem[0], &xextrem[1], &xextrem[2], &xextrem[3], curve_coef[0].x, curve_coef[1].x, curve_coef[2].x, curve_coef[3].x);
288 if(lower > r_ext || upper < -r_ext)
292 curvebounds(&lower, &upper, &yextrem[0], &yextrem[1], &yextrem[2], &yextrem[3], curve_coef[0].y, curve_coef[1].y, curve_coef[2].y, curve_coef[3].y);
293 if(lower > r_ext || upper < -r_ext)
296 /*setup recurrent loop*/
297 int level = 1 << depth;
299 float resol = 1.0f / (float)level;
303 while(!(tree >> (depth))) {
304 float i_st = tree * resol;
305 float i_en = i_st + (level * resol);
306 float3 p_st = ((curve_coef[3] * i_st + curve_coef[2]) * i_st + curve_coef[1]) * i_st + curve_coef[0];
307 float3 p_en = ((curve_coef[3] * i_en + curve_coef[2]) * i_en + curve_coef[1]) * i_en + curve_coef[0];
309 float bminx = min(p_st.x, p_en.x);
310 float bmaxx = max(p_st.x, p_en.x);
311 float bminy = min(p_st.y, p_en.y);
312 float bmaxy = max(p_st.y, p_en.y);
313 float bminz = min(p_st.z, p_en.z);
314 float bmaxz = max(p_st.z, p_en.z);
316 if(xextrem[0] >= i_st && xextrem[0] <= i_en) {
317 bminx = min(bminx,xextrem[1]);
318 bmaxx = max(bmaxx,xextrem[1]);
320 if(xextrem[2] >= i_st && xextrem[2] <= i_en) {
321 bminx = min(bminx,xextrem[3]);
322 bmaxx = max(bmaxx,xextrem[3]);
324 if(yextrem[0] >= i_st && yextrem[0] <= i_en) {
325 bminy = min(bminy,yextrem[1]);
326 bmaxy = max(bmaxy,yextrem[1]);
328 if(yextrem[2] >= i_st && yextrem[2] <= i_en) {
329 bminy = min(bminy,yextrem[3]);
330 bmaxy = max(bmaxy,yextrem[3]);
332 if(zextrem[0] >= i_st && zextrem[0] <= i_en) {
333 bminz = min(bminz,zextrem[1]);
334 bmaxz = max(bmaxz,zextrem[1]);
336 if(zextrem[2] >= i_st && zextrem[2] <= i_en) {
337 bminz = min(bminz,zextrem[3]);
338 bmaxz = max(bmaxz,zextrem[3]);
341 float r1 = r_st + (r_en - r_st) * i_st;
342 float r2 = r_st + (r_en - r_st) * i_en;
343 r_curr = max(r1, r2);
345 mw_extension = min(difl * fabsf(bmaxz), extmax);
346 float r_ext = mw_extension + r_curr;
347 float coverage = 1.0f;
349 if (bminz - r_curr > isect->t || bmaxz + r_curr < epsilon || bminx > r_ext|| bmaxx < -r_ext|| bminy > r_ext|| bmaxy < -r_ext) {
350 /* the bounding box does not overlap the square centered at O.*/
352 level = tree & -tree;
354 else if (level == 1) {
356 /* the maximum recursion depth is reached.
357 * check if dP0.(Q-P0)>=0 and dPn.(Pn-Q)>=0.
358 * dP* is reversed if necessary.*/
361 if(flags & CURVE_KN_RIBBONS) {
362 float3 tg = (p_en - p_st);
363 float w = tg.x * tg.x + tg.y * tg.y;
366 level = tree & -tree;
369 w = -(p_st.x * tg.x + p_st.y * tg.y) / w;
370 w = clamp((float)w, 0.0f, 1.0f);
372 /* compute u on the curve segment.*/
373 u = i_st * (1 - w) + i_en * w;
374 r_curr = r_st + (r_en - r_st) * u;
375 /* compare x-y distances.*/
376 float3 p_curr = ((curve_coef[3] * u + curve_coef[2]) * u + curve_coef[1]) * u + curve_coef[0];
378 float3 dp_st = (3 * curve_coef[3] * i_st + 2 * curve_coef[2]) * i_st + curve_coef[1];
379 if (dot(tg, dp_st)< 0)
381 if (dot(dp_st, -p_st) + p_curr.z * dp_st.z < 0) {
383 level = tree & -tree;
386 float3 dp_en = (3 * curve_coef[3] * i_en + 2 * curve_coef[2]) * i_en + curve_coef[1];
387 if (dot(tg, dp_en) < 0)
389 if (dot(dp_en, p_en) - p_curr.z * dp_en.z < 0) {
391 level = tree & -tree;
395 /* compute coverage */
396 float r_ext = r_curr;
399 mw_extension = min(difl * fabsf(bmaxz), extmax);
400 r_ext = mw_extension + r_curr;
401 float d = sqrtf(p_curr.x * p_curr.x + p_curr.y * p_curr.y);
402 float d0 = d - r_curr;
403 float d1 = d + r_curr;
405 coverage = (min(d1 / mw_extension, 1.0f) - min(d0 / mw_extension, 1.0f)) * 0.5f;
407 coverage = (min(d1 / mw_extension, 1.0f) + min(-d0 / mw_extension, 1.0f)) * 0.5f;
410 if (p_curr.x * p_curr.x + p_curr.y * p_curr.y >= r_ext * r_ext || p_curr.z <= epsilon) {
412 level = tree & -tree;
415 /* compare z distances.*/
416 if (isect->t < p_curr.z) {
418 level = tree & -tree;
424 float l = len(p_en - p_st);
425 /*minimum width extension*/
429 mw_extension = min(len(p_st - P) * difl, extmax);
430 or1 = r1 < mw_extension ? mw_extension : r1;
431 mw_extension = min(len(p_en - P) * difl, extmax);
432 or2 = r2 < mw_extension ? mw_extension : r2;
435 float3 tg = (p_en - p_st) / l;
436 float gd = (or2 - or1) / l;
437 float difz = -dot(p_st,tg);
438 float cyla = 1.0f - (tg.z * tg.z * (1 + gd*gd));
439 float halfb = (-p_st.z - tg.z*(difz + gd*(difz*gd + or1)));
440 float tcentre = -halfb/cyla;
441 float zcentre = difz + (tg.z * tcentre);
442 float3 tdif = - p_st;
444 float tdifz = dot(tdif,tg);
445 float tb = 2*(tdif.z - tg.z*(tdifz + gd*(tdifz*gd + or1)));
446 float tc = dot(tdif,tdif) - tdifz * tdifz * (1 + gd*gd) - or1*or1 - 2*or1*tdifz*gd;
447 float td = tb*tb - 4*cyla*tc;
450 level = tree & -tree;
454 float rootd = sqrtf(td);
455 float correction = ((-tb - rootd)/(2*cyla));
456 t = tcentre + correction;
457 float w = (zcentre + (tg.z * correction))/l;
459 float3 dp_st = (3 * curve_coef[3] * i_st + 2 * curve_coef[2]) * i_st + curve_coef[1];
460 if (dot(tg, dp_st)< 0)
462 float3 dp_en = (3 * curve_coef[3] * i_en + 2 * curve_coef[2]) * i_en + curve_coef[1];
463 if (dot(tg, dp_en) < 0)
467 if(flags & CURVE_KN_BACKFACING && (dot(dp_st, -p_st) + t * dp_st.z < 0 || dot(dp_en, p_en) - t * dp_en.z < 0 || isect->t < t || t <= 0.0f)) {
468 correction = ((-tb + rootd)/(2*cyla));
469 t = tcentre + correction;
470 w = (zcentre + (tg.z * correction))/l;
473 if (dot(dp_st, -p_st) + t * dp_st.z < 0 || dot(dp_en, p_en) - t * dp_en.z < 0 || isect->t < t || t <= 0.0f) {
475 level = tree & -tree;
479 w = clamp((float)w, 0.0f, 1.0f);
480 /* compute u on the curve segment.*/
481 u = i_st * (1 - w) + i_en * w;
482 r_curr = r1 + (r2 - r1) * w;
483 r_ext = or1 + (or2 - or1) * w;
484 coverage = r_curr/r_ext;
487 /* we found a new intersection.*/
489 /*stochastic fade from minimum width*/
490 if(lcg_state && coverage != 1.0f) {
491 if(lcg_step(lcg_state) > coverage)
495 #ifdef __VISIBILITY_FLAG__
496 /* visibility flag test. we do it here under the assumption
497 * that most triangles are culled by node flags */
498 if(kernel_tex_fetch(__prim_visibility, curveAddr) & visibility)
501 /* record intersection */
502 isect->prim = curveAddr;
503 isect->segment = segment;
504 isect->object = object;
507 /*isect->v = 1.0f - coverage; */
513 level = tree & -tree;
516 /* split the curve into two curves and process */
524 __device_inline bool bvh_curve_intersect(KernelGlobals *kg, Intersection *isect,
525 float3 P, float3 idir, uint visibility, int object, int curveAddr, int segment, uint *lcg_state, float difl, float extmax)
527 /* curve Intersection check */
528 int flags = kernel_data.curve.curveflags;
530 int prim = kernel_tex_fetch(__prim_index, curveAddr);
531 float4 v00 = kernel_tex_fetch(__curves, prim);
533 int cnum = __float_as_int(v00.x);
534 int k0 = cnum + segment;
537 float4 P1 = kernel_tex_fetch(__curve_keys, k0);
538 float4 P2 = kernel_tex_fetch(__curve_keys, k1);
542 float3 p1 = float4_to_float3(P1);
543 float3 p2 = float4_to_float3(P2);
545 /*minimum width extension*/
549 float pixelsize = min(len(p1 - P) * difl, extmax);
550 r1 = or1 < pixelsize ? pixelsize : or1;
551 pixelsize = min(len(p2 - P) * difl, extmax);
552 r2 = or2 < pixelsize ? pixelsize : or2;
556 float mr = max(r1,r2);
558 float3 dir = 1.0f/idir;
559 float l = len(p2 - p1);
561 float sp_r = mr + 0.5f * l;
562 float3 sphere_dif = P - ((p1 + p2) * 0.5f);
563 float sphere_b = dot(dir,sphere_dif);
564 sphere_dif = sphere_dif - sphere_b * dir;
565 sphere_b = dot(dir,sphere_dif);
566 float sdisc = sphere_b * sphere_b - len_squared(sphere_dif) + sp_r * sp_r;
570 /* obtain parameters and test midpoint distance for suitable modes*/
571 float3 tg = (p2 - p1) / l;
572 float gd = (r2 - r1) / l;
573 float dirz = dot(dir,tg);
574 float difz = dot(dif,tg);
576 float a = 1.0f - (dirz*dirz*(1 + gd*gd));
577 float halfb = dot(dir,dif) - dirz*(difz + gd*(difz*gd + r1));
579 float tcentre = -halfb/a;
580 float zcentre = difz + (dirz * tcentre);
582 if((tcentre > isect->t) && !(flags & CURVE_KN_ACCURATE))
584 if((zcentre < 0 || zcentre > l) && !(flags & CURVE_KN_ACCURATE) && !(flags & CURVE_KN_INTERSECTCORRECTION))
587 /* test minimum separation*/
588 float3 cprod = cross(tg, dir);
589 float3 cprod2 = cross(tg, dif);
590 float cprodsq = len_squared(cprod);
591 float cprod2sq = len_squared(cprod2);
592 float distscaled = dot(cprod,dif);
595 distscaled = cprod2sq;
597 distscaled = (distscaled*distscaled)/cprodsq;
599 if(distscaled > mr*mr)
602 /* calculate true intersection*/
603 float3 tdif = P - p1 + tcentre * dir;
604 float tdifz = dot(tdif,tg);
605 float tb = 2*(dot(dir,tdif) - dirz*(tdifz + gd*(tdifz*gd + r1)));
606 float tc = dot(tdif,tdif) - tdifz * tdifz * (1 + gd*gd) - r1*r1 - 2*r1*tdifz*gd;
607 float td = tb*tb - 4*a*tc;
613 float correction = 0.0f;
614 if(flags & CURVE_KN_ACCURATE) {
616 correction = ((-tb - rootd)/(2*a));
619 float t = tcentre + correction;
623 if(flags & CURVE_KN_INTERSECTCORRECTION) {
625 correction = ((-tb - rootd)/(2*a));
626 t = tcentre + correction;
629 float z = zcentre + (dirz * correction);
630 bool backface = false;
632 if(flags & CURVE_KN_BACKFACING && (t < 0.0f || z < 0 || z > l)) {
634 correction = ((-tb + rootd)/(2*a));
635 t = tcentre + correction;
636 z = zcentre + (dirz * correction);
639 /*stochastic fade from minimum width*/
640 float adjradius = or1 + z * (or2 - or1) / l;
641 adjradius = adjradius / (r1 + z * gd);
642 if(lcg_state && adjradius != 1.0f) {
643 if(lcg_step(lcg_state) > adjradius)
648 if(t > 0.0f && t < isect->t && z >= 0 && z <= l) {
650 if (flags & CURVE_KN_ENCLOSEFILTER) {
652 float enc_ratio = kernel_data.curve.encasing_ratio;
653 if((dot(P - p1, tg) > -r1 * enc_ratio) && (dot(P - p2, tg) < r2 * enc_ratio)) {
654 float a2 = 1.0f - (dirz*dirz*(1 + gd*gd*enc_ratio*enc_ratio));
655 float c2 = dot(dif,dif) - difz * difz * (1 + gd*gd*enc_ratio*enc_ratio) - r1*r1*enc_ratio*enc_ratio - 2*r1*difz*gd*enc_ratio;
661 #ifdef __VISIBILITY_FLAG__
662 /* visibility flag test. we do it here under the assumption
663 * that most triangles are culled by node flags */
664 if(kernel_tex_fetch(__prim_visibility, curveAddr) & visibility)
667 /* record intersection */
668 isect->prim = curveAddr;
669 isect->segment = segment;
670 isect->object = object;
672 isect->v = td/(4*a*a);
673 /*isect->v = 1.0f - adjradius;*/
677 isect->u = -isect->u;
688 #ifdef __SUBSURFACE__
689 /* Special ray intersection routines for subsurface scattering. In that case we
690 * only want to intersect with primitives in the same object, and if case of
691 * multiple hits we pick a single random primitive as the intersection point. */
693 __device_inline bool bvh_triangle_intersect_subsurface(KernelGlobals *kg, Intersection *isect,
694 float3 P, float3 idir, int object, int triAddr, float tmax, int *num_hits, float subsurface_random)
696 /* compute and check intersection t-value */
697 float4 v00 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+0);
698 float4 v11 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+1);
699 float3 dir = 1.0f/idir;
701 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z;
702 float invDz = 1.0f/(dir.x*v00.x + dir.y*v00.y + dir.z*v00.z);
703 float t = Oz * invDz;
705 if(t > 0.0f && t < tmax) {
706 /* compute and check barycentric u */
707 float Ox = v11.w + P.x*v11.x + P.y*v11.y + P.z*v11.z;
708 float Dx = dir.x*v11.x + dir.y*v11.y + dir.z*v11.z;
712 /* compute and check barycentric v */
713 float4 v22 = kernel_tex_fetch(__tri_woop, triAddr*TRI_NODE_SIZE+2);
714 float Oy = v22.w + P.x*v22.x + P.y*v22.y + P.z*v22.z;
715 float Dy = dir.x*v22.x + dir.y*v22.y + dir.z*v22.z;
718 if(v >= 0.0f && u + v <= 1.0f) {
721 if(subsurface_random * (*num_hits) <= 1.0f) {
722 /* record intersection */
723 isect->prim = triAddr;
724 isect->object = object;
738 /* BVH intersection function variations */
740 #define BVH_INSTANCING 1
743 #define BVH_HAIR_MINIMUM_WIDTH 8
744 #define BVH_SUBSURFACE 16
746 #define BVH_FUNCTION_NAME bvh_intersect
747 #define BVH_FUNCTION_FEATURES 0
748 #include "kernel_bvh_traversal.h"
750 #if defined(__INSTANCING__)
751 #define BVH_FUNCTION_NAME bvh_intersect_instancing
752 #define BVH_FUNCTION_FEATURES BVH_INSTANCING
753 #include "kernel_bvh_traversal.h"
756 #if defined(__HAIR__)
757 #define BVH_FUNCTION_NAME bvh_intersect_hair
758 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH
759 #include "kernel_bvh_traversal.h"
762 #if defined(__OBJECT_MOTION__)
763 #define BVH_FUNCTION_NAME bvh_intersect_motion
764 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_MOTION
765 #include "kernel_bvh_traversal.h"
768 #if defined(__HAIR__) && defined(__OBJECT_MOTION__)
769 #define BVH_FUNCTION_NAME bvh_intersect_hair_motion
770 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH|BVH_MOTION
771 #include "kernel_bvh_traversal.h"
774 #if defined(__SUBSURFACE__)
775 #define BVH_FUNCTION_NAME bvh_intersect_subsurface
776 #define BVH_FUNCTION_FEATURES BVH_SUBSURFACE
777 #include "kernel_bvh_traversal.h"
780 #if defined(__SUBSURFACE__) && defined(__INSTANCING__)
781 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_instancing
782 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_SUBSURFACE
783 #include "kernel_bvh_traversal.h"
786 #if defined(__SUBSURFACE__) && defined(__HAIR__)
787 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_hair
788 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_SUBSURFACE|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH
789 #include "kernel_bvh_traversal.h"
792 #if defined(__SUBSURFACE__) && defined(__OBJECT_MOTION__)
793 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_motion
794 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_SUBSURFACE|BVH_MOTION
795 #include "kernel_bvh_traversal.h"
798 #if defined(__SUBSURFACE__) && defined(__HAIR__) && defined(__OBJECT_MOTION__)
799 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_hair_motion
800 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_SUBSURFACE|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH|BVH_MOTION
801 #include "kernel_bvh_traversal.h"
806 __device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect, uint *lcg_state, float difl, float extmax)
808 __device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect)
811 #ifdef __OBJECT_MOTION__
812 if(kernel_data.bvh.have_motion) {
814 if(kernel_data.bvh.have_curves)
815 return bvh_intersect_hair_motion(kg, ray, isect, visibility, lcg_state, difl, extmax);
816 #endif /* __HAIR__ */
818 return bvh_intersect_motion(kg, ray, isect, visibility);
820 #endif /* __OBJECT_MOTION__ */
823 if(kernel_data.bvh.have_curves)
824 return bvh_intersect_hair(kg, ray, isect, visibility, lcg_state, difl, extmax);
825 #endif /* __HAIR__ */
827 #ifdef __KERNEL_CPU__
829 #ifdef __INSTANCING__
830 if(kernel_data.bvh.have_instancing)
831 return bvh_intersect_instancing(kg, ray, isect, visibility);
832 #endif /* __INSTANCING__ */
834 return bvh_intersect(kg, ray, isect, visibility);
835 #else /* __KERNEL_CPU__ */
837 #ifdef __INSTANCING__
838 return bvh_intersect_instancing(kg, ray, isect, visibility);
840 return bvh_intersect(kg, ray, isect, visibility);
841 #endif /* __INSTANCING__ */
843 #endif /* __KERNEL_CPU__ */
846 #ifdef __SUBSURFACE__
847 __device_inline int scene_intersect_subsurface(KernelGlobals *kg, const Ray *ray, Intersection *isect, int subsurface_object, float subsurface_random)
849 #ifdef __OBJECT_MOTION__
850 if(kernel_data.bvh.have_motion) {
852 if(kernel_data.bvh.have_curves)
853 return bvh_intersect_subsurface_hair_motion(kg, ray, isect, subsurface_object, subsurface_random);
854 #endif /* __HAIR__ */
856 return bvh_intersect_subsurface_motion(kg, ray, isect, subsurface_object, subsurface_random);
858 #endif /* __OBJECT_MOTION__ */
861 if(kernel_data.bvh.have_curves)
862 return bvh_intersect_subsurface_hair(kg, ray, isect, subsurface_object, subsurface_random);
863 #endif /* __HAIR__ */
865 #ifdef __KERNEL_CPU__
867 #ifdef __INSTANCING__
868 if(kernel_data.bvh.have_instancing)
869 return bvh_intersect_subsurface_instancing(kg, ray, isect, subsurface_object, subsurface_random);
870 #endif /* __INSTANCING__ */
872 return bvh_intersect_subsurface(kg, ray, isect, subsurface_object, subsurface_random);
873 #else /* __KERNEL_CPU__ */
875 #ifdef __INSTANCING__
876 return bvh_intersect_subsurface_instancing(kg, ray, isect, subsurface_object, subsurface_random);
878 return bvh_intersect_subsurface(kg, ray, isect, subsurface_object, subsurface_random);
879 #endif /* __INSTANCING__ */
881 #endif /* __KERNEL_CPU__ */
885 /* Ray offset to avoid self intersection */
887 __device_inline float3 ray_offset(float3 P, float3 Ng)
889 #ifdef __INTERSECTION_REFINE__
890 const float epsilon_f = 1e-5f;
891 /* ideally this should match epsilon_f, but instancing/mblur
892 * precision makes it problematic */
893 const float epsilon_test = 1.0f;
894 const int epsilon_i = 32;
899 if(fabsf(P.x) < epsilon_test) {
900 res.x = P.x + Ng.x*epsilon_f;
903 uint ix = __float_as_uint(P.x);
904 ix += ((ix ^ __float_as_uint(Ng.x)) >> 31)? -epsilon_i: epsilon_i;
905 res.x = __uint_as_float(ix);
909 if(fabsf(P.y) < epsilon_test) {
910 res.y = P.y + Ng.y*epsilon_f;
913 uint iy = __float_as_uint(P.y);
914 iy += ((iy ^ __float_as_uint(Ng.y)) >> 31)? -epsilon_i: epsilon_i;
915 res.y = __uint_as_float(iy);
919 if(fabsf(P.z) < epsilon_test) {
920 res.z = P.z + Ng.z*epsilon_f;
923 uint iz = __float_as_uint(P.z);
924 iz += ((iz ^ __float_as_uint(Ng.z)) >> 31)? -epsilon_i: epsilon_i;
925 res.z = __uint_as_float(iz);
930 const float epsilon_f = 1e-4f;
931 return P + epsilon_f*Ng;
935 /* Refine triangle intersection to more precise hit point. For rays that travel
936 * far the precision is often not so good, this reintersects the primitive from
937 * a closer distance. */
939 __device_inline float3 bvh_triangle_refine(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray)
945 #ifdef __INTERSECTION_REFINE__
946 if(isect->object != ~0) {
947 #ifdef __OBJECT_MOTION__
948 Transform tfm = sd->ob_itfm;
950 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
953 P = transform_point(&tfm, P);
954 D = transform_direction(&tfm, D*t);
955 D = normalize_len(D, &t);
960 float4 v00 = kernel_tex_fetch(__tri_woop, isect->prim*TRI_NODE_SIZE+0);
961 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z;
962 float invDz = 1.0f/(D.x*v00.x + D.y*v00.y + D.z*v00.z);
963 float rt = Oz * invDz;
967 if(isect->object != ~0) {
968 #ifdef __OBJECT_MOTION__
969 Transform tfm = sd->ob_tfm;
971 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
974 P = transform_point(&tfm, P);
985 __device_inline float3 curvetangent(float t, float3 p0, float3 p1, float3 p2, float3 p3)
990 data[0] = -3.0f * fc * t2 + 4.0f * fc * t - fc;
991 data[1] = 3.0f * (2.0f - fc) * t2 + 2.0f * (fc - 3.0f) * t;
992 data[2] = 3.0f * (fc - 2.0f) * t2 + 2.0f * (3.0f - 2.0f * fc) * t + fc;
993 data[3] = 3.0f * fc * t2 - 2.0f * fc * t;
994 return data[0] * p0 + data[1] * p1 + data[2] * p2 + data[3] * p3;
997 __device_inline float3 curvepoint(float t, float3 p0, float3 p1, float3 p2, float3 p3)
1003 data[0] = -fc * t3 + 2.0f * fc * t2 - fc * t;
1004 data[1] = (2.0f - fc) * t3 + (fc - 3.0f) * t2 + 1.0f;
1005 data[2] = (fc - 2.0f) * t3 + (3.0f - 2.0f * fc) * t2 + fc * t;
1006 data[3] = fc * t3 - fc * t2;
1007 return data[0] * p0 + data[1] * p1 + data[2] * p2 + data[3] * p3;
1010 __device_inline float3 bvh_curve_refine(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray, float t)
1012 int flag = kernel_data.curve.curveflags;
1016 if(isect->object != ~0) {
1017 #ifdef __OBJECT_MOTION__
1018 Transform tfm = sd->ob_itfm;
1020 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
1023 P = transform_point(&tfm, P);
1024 D = transform_direction(&tfm, D*t);
1025 D = normalize_len(D, &t);
1028 int prim = kernel_tex_fetch(__prim_index, isect->prim);
1029 float4 v00 = kernel_tex_fetch(__curves, prim);
1031 int k0 = __float_as_int(v00.x) + isect->segment;
1034 float4 P1 = kernel_tex_fetch(__curve_keys, k0);
1035 float4 P2 = kernel_tex_fetch(__curve_keys, k1);
1037 float3 tg = normalize_len(float4_to_float3(P2 - P1),&l);
1040 float gd = ((r2 - r1)/l);
1044 if(flag & CURVE_KN_INTERPOLATE) {
1045 int ka = max(k0 - 1,__float_as_int(v00.x));
1046 int kb = min(k1 + 1,__float_as_int(v00.x) + __float_as_int(v00.y) - 1);
1048 float4 P0 = kernel_tex_fetch(__curve_keys, ka);
1049 float4 P3 = kernel_tex_fetch(__curve_keys, kb);
1052 p[0] = float4_to_float3(P0);
1053 p[1] = float4_to_float3(P1);
1054 p[2] = float4_to_float3(P2);
1055 p[3] = float4_to_float3(P3);
1057 tg = normalize(curvetangent(isect->u,p[0],p[1],p[2],p[3]));
1058 float3 p_curr = curvepoint(isect->u,p[0],p[1],p[2],p[3]);
1065 if(kernel_data.curve.curveflags & CURVE_KN_RIBBONS)
1066 sd->Ng = normalize(-(D - tg * (dot(tg,D))));
1068 sd->Ng = normalize(P - p_curr);
1069 sd->Ng = sd->Ng - gd * tg;
1070 sd->Ng = normalize(sd->Ng);
1075 float3 dif = P - float4_to_float3(P1);
1078 sd->u = dot(dif,tg)/l;
1082 if (flag & CURVE_KN_TRUETANGENTGNORMAL) {
1083 sd->Ng = -(D - tg * dot(tg,D));
1084 sd->Ng = normalize(sd->Ng);
1087 sd->Ng = (dif - tg * sd->u * l) / (P1.w + sd->u * l * gd);
1089 sd->Ng = sd->Ng - gd * tg ;
1090 sd->Ng = normalize(sd->Ng);
1096 if (flag & CURVE_KN_TANGENTGNORMAL && !(flag & CURVE_KN_TRUETANGENTGNORMAL)) {
1097 sd->N = -(D - tg * dot(tg,D));
1098 sd->N = normalize(sd->N);
1100 if (!(flag & CURVE_KN_TANGENTGNORMAL) && flag & CURVE_KN_TRUETANGENTGNORMAL) {
1101 sd->N = (dif - tg * sd->u * l) / (P1.w + sd->u * l * gd);
1103 sd->N = sd->N - gd * tg ;
1104 sd->N = normalize(sd->N);
1112 sd->dPdv = cross(tg,sd->Ng);
1115 /*add fading parameter for minimum pixel width with transparency bsdf*/
1116 /*sd->curve_transparency = isect->v;*/
1117 /*sd->curve_radius = sd->u * gd * l + r1;*/
1119 if(isect->object != ~0) {
1120 #ifdef __OBJECT_MOTION__
1121 Transform tfm = sd->ob_tfm;
1123 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
1126 P = transform_point(&tfm, P);