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_float(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_float(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 void bvh_triangle_intersect_subsurface(KernelGlobals *kg, Intersection *isect_array,
694 float3 P, float3 idir, int object, int triAddr, float tmax, uint *num_hits, uint *lcg_state, int max_hits)
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) {
723 if(*num_hits <= max_hits) {
727 /* reservoir sampling: if we are at the maximum number of
728 * hits, randomly replace element or skip it */
729 hit = lcg_step_uint(lcg_state) % *num_hits;
735 /* record intersection */
736 Intersection *isect = &isect_array[hit];
737 isect->prim = triAddr;
738 isect->object = object;
748 /* BVH intersection function variations */
750 #define BVH_INSTANCING 1
753 #define BVH_HAIR_MINIMUM_WIDTH 8
755 #define BVH_FUNCTION_NAME bvh_intersect
756 #define BVH_FUNCTION_FEATURES 0
757 #include "kernel_bvh_traversal.h"
759 #if defined(__INSTANCING__)
760 #define BVH_FUNCTION_NAME bvh_intersect_instancing
761 #define BVH_FUNCTION_FEATURES BVH_INSTANCING
762 #include "kernel_bvh_traversal.h"
765 #if defined(__HAIR__)
766 #define BVH_FUNCTION_NAME bvh_intersect_hair
767 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH
768 #include "kernel_bvh_traversal.h"
771 #if defined(__OBJECT_MOTION__)
772 #define BVH_FUNCTION_NAME bvh_intersect_motion
773 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_MOTION
774 #include "kernel_bvh_traversal.h"
777 #if defined(__HAIR__) && defined(__OBJECT_MOTION__)
778 #define BVH_FUNCTION_NAME bvh_intersect_hair_motion
779 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH|BVH_MOTION
780 #include "kernel_bvh_traversal.h"
783 #if defined(__SUBSURFACE__)
784 #define BVH_FUNCTION_NAME bvh_intersect_subsurface
785 #include "kernel_bvh_subsurface.h"
788 #if defined(__SUBSURFACE__) && defined(__INSTANCING__)
789 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_instancing
790 #define BVH_FUNCTION_FEATURES BVH_INSTANCING
791 #include "kernel_bvh_subsurface.h"
794 #if defined(__SUBSURFACE__) && defined(__HAIR__)
795 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_hair
796 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH
797 #include "kernel_bvh_subsurface.h"
800 #if defined(__SUBSURFACE__) && defined(__OBJECT_MOTION__)
801 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_motion
802 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_MOTION
803 #include "kernel_bvh_subsurface.h"
806 #if defined(__SUBSURFACE__) && defined(__HAIR__) && defined(__OBJECT_MOTION__)
807 #define BVH_FUNCTION_NAME bvh_intersect_subsurface_hair_motion
808 #define BVH_FUNCTION_FEATURES BVH_INSTANCING|BVH_HAIR|BVH_HAIR_MINIMUM_WIDTH|BVH_MOTION
809 #include "kernel_bvh_subsurface.h"
814 __device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect, uint *lcg_state, float difl, float extmax)
816 __device_inline bool scene_intersect(KernelGlobals *kg, const Ray *ray, const uint visibility, Intersection *isect)
819 #ifdef __OBJECT_MOTION__
820 if(kernel_data.bvh.have_motion) {
822 if(kernel_data.bvh.have_curves)
823 return bvh_intersect_hair_motion(kg, ray, isect, visibility, lcg_state, difl, extmax);
824 #endif /* __HAIR__ */
826 return bvh_intersect_motion(kg, ray, isect, visibility);
828 #endif /* __OBJECT_MOTION__ */
831 if(kernel_data.bvh.have_curves)
832 return bvh_intersect_hair(kg, ray, isect, visibility, lcg_state, difl, extmax);
833 #endif /* __HAIR__ */
835 #ifdef __KERNEL_CPU__
837 #ifdef __INSTANCING__
838 if(kernel_data.bvh.have_instancing)
839 return bvh_intersect_instancing(kg, ray, isect, visibility);
840 #endif /* __INSTANCING__ */
842 return bvh_intersect(kg, ray, isect, visibility);
843 #else /* __KERNEL_CPU__ */
845 #ifdef __INSTANCING__
846 return bvh_intersect_instancing(kg, ray, isect, visibility);
848 return bvh_intersect(kg, ray, isect, visibility);
849 #endif /* __INSTANCING__ */
851 #endif /* __KERNEL_CPU__ */
854 #ifdef __SUBSURFACE__
855 __device_inline uint scene_intersect_subsurface(KernelGlobals *kg, const Ray *ray, Intersection *isect, int subsurface_object, uint *lcg_state, int max_hits)
857 #ifdef __OBJECT_MOTION__
858 if(kernel_data.bvh.have_motion) {
860 if(kernel_data.bvh.have_curves)
861 return bvh_intersect_subsurface_hair_motion(kg, ray, isect, subsurface_object, lcg_state, max_hits);
862 #endif /* __HAIR__ */
864 return bvh_intersect_subsurface_motion(kg, ray, isect, subsurface_object, lcg_state, max_hits);
866 #endif /* __OBJECT_MOTION__ */
869 if(kernel_data.bvh.have_curves)
870 return bvh_intersect_subsurface_hair(kg, ray, isect, subsurface_object, lcg_state, max_hits);
871 #endif /* __HAIR__ */
873 #ifdef __KERNEL_CPU__
875 #ifdef __INSTANCING__
876 if(kernel_data.bvh.have_instancing)
877 return bvh_intersect_subsurface_instancing(kg, ray, isect, subsurface_object, lcg_state, max_hits);
878 #endif /* __INSTANCING__ */
880 return bvh_intersect_subsurface(kg, ray, isect, subsurface_object, lcg_state, max_hits);
881 #else /* __KERNEL_CPU__ */
883 #ifdef __INSTANCING__
884 return bvh_intersect_subsurface_instancing(kg, ray, isect, subsurface_object, lcg_state, max_hits);
886 return bvh_intersect_subsurface(kg, ray, isect, subsurface_object, lcg_state, max_hits);
887 #endif /* __INSTANCING__ */
889 #endif /* __KERNEL_CPU__ */
893 /* Ray offset to avoid self intersection */
895 __device_inline float3 ray_offset(float3 P, float3 Ng)
897 #ifdef __INTERSECTION_REFINE__
898 const float epsilon_f = 1e-5f;
899 /* ideally this should match epsilon_f, but instancing/mblur
900 * precision makes it problematic */
901 const float epsilon_test = 1.0f;
902 const int epsilon_i = 32;
907 if(fabsf(P.x) < epsilon_test) {
908 res.x = P.x + Ng.x*epsilon_f;
911 uint ix = __float_as_uint(P.x);
912 ix += ((ix ^ __float_as_uint(Ng.x)) >> 31)? -epsilon_i: epsilon_i;
913 res.x = __uint_as_float(ix);
917 if(fabsf(P.y) < epsilon_test) {
918 res.y = P.y + Ng.y*epsilon_f;
921 uint iy = __float_as_uint(P.y);
922 iy += ((iy ^ __float_as_uint(Ng.y)) >> 31)? -epsilon_i: epsilon_i;
923 res.y = __uint_as_float(iy);
927 if(fabsf(P.z) < epsilon_test) {
928 res.z = P.z + Ng.z*epsilon_f;
931 uint iz = __float_as_uint(P.z);
932 iz += ((iz ^ __float_as_uint(Ng.z)) >> 31)? -epsilon_i: epsilon_i;
933 res.z = __uint_as_float(iz);
938 const float epsilon_f = 1e-4f;
939 return P + epsilon_f*Ng;
943 /* Refine triangle intersection to more precise hit point. For rays that travel
944 * far the precision is often not so good, this reintersects the primitive from
945 * a closer distance. */
947 __device_inline float3 bvh_triangle_refine(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray)
953 #ifdef __INTERSECTION_REFINE__
954 if(isect->object != ~0) {
955 #ifdef __OBJECT_MOTION__
956 Transform tfm = sd->ob_itfm;
958 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
961 P = transform_point(&tfm, P);
962 D = transform_direction(&tfm, D*t);
963 D = normalize_len(D, &t);
968 float4 v00 = kernel_tex_fetch(__tri_woop, isect->prim*TRI_NODE_SIZE+0);
969 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z;
970 float invDz = 1.0f/(D.x*v00.x + D.y*v00.y + D.z*v00.z);
971 float rt = Oz * invDz;
975 if(isect->object != ~0) {
976 #ifdef __OBJECT_MOTION__
977 Transform tfm = sd->ob_tfm;
979 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
982 P = transform_point(&tfm, P);
991 /* same as above, except that isect->t is assumed to be in object space for instancing */
992 __device_inline float3 bvh_triangle_refine_subsurface(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray)
998 #ifdef __INTERSECTION_REFINE__
999 if(isect->object != ~0) {
1000 #ifdef __OBJECT_MOTION__
1001 Transform tfm = sd->ob_itfm;
1003 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
1006 P = transform_point(&tfm, P);
1007 D = transform_direction(&tfm, D);
1013 float4 v00 = kernel_tex_fetch(__tri_woop, isect->prim*TRI_NODE_SIZE+0);
1014 float Oz = v00.w - P.x*v00.x - P.y*v00.y - P.z*v00.z;
1015 float invDz = 1.0f/(D.x*v00.x + D.y*v00.y + D.z*v00.z);
1016 float rt = Oz * invDz;
1020 if(isect->object != ~0) {
1021 #ifdef __OBJECT_MOTION__
1022 Transform tfm = sd->ob_tfm;
1024 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
1027 P = transform_point(&tfm, P);
1038 __device_inline float3 curvetangent(float t, float3 p0, float3 p1, float3 p2, float3 p3)
1043 data[0] = -3.0f * fc * t2 + 4.0f * fc * t - fc;
1044 data[1] = 3.0f * (2.0f - fc) * t2 + 2.0f * (fc - 3.0f) * t;
1045 data[2] = 3.0f * (fc - 2.0f) * t2 + 2.0f * (3.0f - 2.0f * fc) * t + fc;
1046 data[3] = 3.0f * fc * t2 - 2.0f * fc * t;
1047 return data[0] * p0 + data[1] * p1 + data[2] * p2 + data[3] * p3;
1050 __device_inline float3 curvepoint(float t, float3 p0, float3 p1, float3 p2, float3 p3)
1056 data[0] = -fc * t3 + 2.0f * fc * t2 - fc * t;
1057 data[1] = (2.0f - fc) * t3 + (fc - 3.0f) * t2 + 1.0f;
1058 data[2] = (fc - 2.0f) * t3 + (3.0f - 2.0f * fc) * t2 + fc * t;
1059 data[3] = fc * t3 - fc * t2;
1060 return data[0] * p0 + data[1] * p1 + data[2] * p2 + data[3] * p3;
1063 __device_inline float3 bvh_curve_refine(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray, float t)
1065 int flag = kernel_data.curve.curveflags;
1069 if(isect->object != ~0) {
1070 #ifdef __OBJECT_MOTION__
1071 Transform tfm = sd->ob_itfm;
1073 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
1076 P = transform_point(&tfm, P);
1077 D = transform_direction(&tfm, D*t);
1078 D = normalize_len(D, &t);
1081 int prim = kernel_tex_fetch(__prim_index, isect->prim);
1082 float4 v00 = kernel_tex_fetch(__curves, prim);
1084 int k0 = __float_as_int(v00.x) + isect->segment;
1087 float4 P1 = kernel_tex_fetch(__curve_keys, k0);
1088 float4 P2 = kernel_tex_fetch(__curve_keys, k1);
1090 float3 tg = normalize_len(float4_to_float3(P2 - P1),&l);
1093 float gd = ((r2 - r1)/l);
1097 if(flag & CURVE_KN_INTERPOLATE) {
1098 int ka = max(k0 - 1,__float_as_int(v00.x));
1099 int kb = min(k1 + 1,__float_as_int(v00.x) + __float_as_int(v00.y) - 1);
1101 float4 P0 = kernel_tex_fetch(__curve_keys, ka);
1102 float4 P3 = kernel_tex_fetch(__curve_keys, kb);
1105 p[0] = float4_to_float3(P0);
1106 p[1] = float4_to_float3(P1);
1107 p[2] = float4_to_float3(P2);
1108 p[3] = float4_to_float3(P3);
1110 tg = normalize(curvetangent(isect->u,p[0],p[1],p[2],p[3]));
1111 float3 p_curr = curvepoint(isect->u,p[0],p[1],p[2],p[3]);
1118 if(kernel_data.curve.curveflags & CURVE_KN_RIBBONS)
1119 sd->Ng = normalize(-(D - tg * (dot(tg,D))));
1121 sd->Ng = normalize(P - p_curr);
1122 sd->Ng = sd->Ng - gd * tg;
1123 sd->Ng = normalize(sd->Ng);
1128 float3 dif = P - float4_to_float3(P1);
1131 sd->u = dot(dif,tg)/l;
1135 if (flag & CURVE_KN_TRUETANGENTGNORMAL) {
1136 sd->Ng = -(D - tg * dot(tg,D));
1137 sd->Ng = normalize(sd->Ng);
1140 sd->Ng = (dif - tg * sd->u * l) / (P1.w + sd->u * l * gd);
1142 sd->Ng = sd->Ng - gd * tg ;
1143 sd->Ng = normalize(sd->Ng);
1149 if (flag & CURVE_KN_TANGENTGNORMAL && !(flag & CURVE_KN_TRUETANGENTGNORMAL)) {
1150 sd->N = -(D - tg * dot(tg,D));
1151 sd->N = normalize(sd->N);
1153 if (!(flag & CURVE_KN_TANGENTGNORMAL) && flag & CURVE_KN_TRUETANGENTGNORMAL) {
1154 sd->N = (dif - tg * sd->u * l) / (P1.w + sd->u * l * gd);
1156 sd->N = sd->N - gd * tg ;
1157 sd->N = normalize(sd->N);
1165 sd->dPdv = cross(tg,sd->Ng);
1168 /*add fading parameter for minimum pixel width with transparency bsdf*/
1169 /*sd->curve_transparency = isect->v;*/
1170 /*sd->curve_radius = sd->u * gd * l + r1;*/
1172 if(isect->object != ~0) {
1173 #ifdef __OBJECT_MOTION__
1174 Transform tfm = sd->ob_tfm;
1176 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
1179 P = transform_point(&tfm, P);