Cycles: Remove ccl_fetch and SOA
[blender.git] / intern / cycles / kernel / geom / geom_curve.h
1 /*
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use this file except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  */
14
15 CCL_NAMESPACE_BEGIN
16
17 /* Curve Primitive
18  *
19  * Curve primitive for rendering hair and fur. These can be render as flat ribbons
20  * or curves with actual thickness. The curve can also be rendered as line segments
21  * rather than curves for better performance */
22
23 #ifdef __HAIR__
24
25 /* Reading attributes on various curve elements */
26
27 ccl_device float curve_attribute_float(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc, float *dx, float *dy)
28 {
29         if(desc.element == ATTR_ELEMENT_CURVE) {
30 #ifdef __RAY_DIFFERENTIALS__
31                 if(dx) *dx = 0.0f;
32                 if(dy) *dy = 0.0f;
33 #endif
34
35                 return kernel_tex_fetch(__attributes_float, desc.offset + sd->prim);
36         }
37         else if(desc.element == ATTR_ELEMENT_CURVE_KEY || desc.element == ATTR_ELEMENT_CURVE_KEY_MOTION) {
38                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
39                 int k0 = __float_as_int(curvedata.x) + PRIMITIVE_UNPACK_SEGMENT(sd->type);
40                 int k1 = k0 + 1;
41
42                 float f0 = kernel_tex_fetch(__attributes_float, desc.offset + k0);
43                 float f1 = kernel_tex_fetch(__attributes_float, desc.offset + k1);
44
45 #ifdef __RAY_DIFFERENTIALS__
46                 if(dx) *dx = sd->du.dx*(f1 - f0);
47                 if(dy) *dy = 0.0f;
48 #endif
49
50                 return (1.0f - sd->u)*f0 + sd->u*f1;
51         }
52         else {
53 #ifdef __RAY_DIFFERENTIALS__
54                 if(dx) *dx = 0.0f;
55                 if(dy) *dy = 0.0f;
56 #endif
57
58                 return 0.0f;
59         }
60 }
61
62 ccl_device float3 curve_attribute_float3(KernelGlobals *kg, const ShaderData *sd, const AttributeDescriptor desc, float3 *dx, float3 *dy)
63 {
64         if(desc.element == ATTR_ELEMENT_CURVE) {
65                 /* idea: we can't derive any useful differentials here, but for tiled
66                  * mipmap image caching it would be useful to avoid reading the highest
67                  * detail level always. maybe a derivative based on the hair density
68                  * could be computed somehow? */
69 #ifdef __RAY_DIFFERENTIALS__
70                 if(dx) *dx = make_float3(0.0f, 0.0f, 0.0f);
71                 if(dy) *dy = make_float3(0.0f, 0.0f, 0.0f);
72 #endif
73
74                 return float4_to_float3(kernel_tex_fetch(__attributes_float3, desc.offset + sd->prim));
75         }
76         else if(desc.element == ATTR_ELEMENT_CURVE_KEY || desc.element == ATTR_ELEMENT_CURVE_KEY_MOTION) {
77                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
78                 int k0 = __float_as_int(curvedata.x) + PRIMITIVE_UNPACK_SEGMENT(sd->type);
79                 int k1 = k0 + 1;
80
81                 float3 f0 = float4_to_float3(kernel_tex_fetch(__attributes_float3, desc.offset + k0));
82                 float3 f1 = float4_to_float3(kernel_tex_fetch(__attributes_float3, desc.offset + k1));
83
84 #ifdef __RAY_DIFFERENTIALS__
85                 if(dx) *dx = sd->du.dx*(f1 - f0);
86                 if(dy) *dy = make_float3(0.0f, 0.0f, 0.0f);
87 #endif
88
89                 return (1.0f - sd->u)*f0 + sd->u*f1;
90         }
91         else {
92 #ifdef __RAY_DIFFERENTIALS__
93                 if(dx) *dx = make_float3(0.0f, 0.0f, 0.0f);
94                 if(dy) *dy = make_float3(0.0f, 0.0f, 0.0f);
95 #endif
96
97                 return make_float3(0.0f, 0.0f, 0.0f);
98         }
99 }
100
101 /* Curve thickness */
102
103 ccl_device float curve_thickness(KernelGlobals *kg, ShaderData *sd)
104 {
105         float r = 0.0f;
106
107         if(sd->type & PRIMITIVE_ALL_CURVE) {
108                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
109                 int k0 = __float_as_int(curvedata.x) + PRIMITIVE_UNPACK_SEGMENT(sd->type);
110                 int k1 = k0 + 1;
111
112                 float4 P_curve[2];
113
114                 if(sd->type & PRIMITIVE_CURVE) {
115                         P_curve[0]= kernel_tex_fetch(__curve_keys, k0);
116                         P_curve[1]= kernel_tex_fetch(__curve_keys, k1);
117                 }
118                 else {
119                         motion_curve_keys(kg, sd->object, sd->prim, sd->time, k0, k1, P_curve);
120                 }
121
122                 r = (P_curve[1].w - P_curve[0].w) * sd->u + P_curve[0].w;
123         }
124
125         return r*2.0f;
126 }
127
128 /* Curve location for motion pass, linear interpolation between keys and
129  * ignoring radius because we do the same for the motion keys */
130
131 ccl_device float3 curve_motion_center_location(KernelGlobals *kg, ShaderData *sd)
132 {
133         float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
134         int k0 = __float_as_int(curvedata.x) + PRIMITIVE_UNPACK_SEGMENT(sd->type);
135         int k1 = k0 + 1;
136
137         float4 P_curve[2];
138
139         P_curve[0]= kernel_tex_fetch(__curve_keys, k0);
140         P_curve[1]= kernel_tex_fetch(__curve_keys, k1);
141
142         return float4_to_float3(P_curve[1]) * sd->u + float4_to_float3(P_curve[0]) * (1.0f - sd->u);
143 }
144
145 /* Curve tangent normal */
146
147 ccl_device float3 curve_tangent_normal(KernelGlobals *kg, ShaderData *sd)
148 {       
149         float3 tgN = make_float3(0.0f,0.0f,0.0f);
150
151         if(sd->type & PRIMITIVE_ALL_CURVE) {
152
153                 tgN = -(-sd->I - sd->dPdu * (dot(sd->dPdu,-sd->I) / len_squared(sd->dPdu)));
154                 tgN = normalize(tgN);
155
156                 /* need to find suitable scaled gd for corrected normal */
157 #if 0
158                 tgN = normalize(tgN - gd * sd->dPdu);
159 #endif
160         }
161
162         return tgN;
163 }
164
165 /* Curve bounds utility function */
166
167 ccl_device_inline void curvebounds(float *lower, float *upper, float *extremta, float *extrema, float *extremtb, float *extremb, float p0, float p1, float p2, float p3)
168 {
169         float halfdiscroot = (p2 * p2 - 3 * p3 * p1);
170         float ta = -1.0f;
171         float tb = -1.0f;
172
173         *extremta = -1.0f;
174         *extremtb = -1.0f;
175         *upper = p0;
176         *lower = (p0 + p1) + (p2 + p3);
177         *extrema = *upper;
178         *extremb = *lower;
179
180         if(*lower >= *upper) {
181                 *upper = *lower;
182                 *lower = p0;
183         }
184
185         if(halfdiscroot >= 0) {
186                 float inv3p3 = (1.0f/3.0f)/p3;
187                 halfdiscroot = sqrtf(halfdiscroot);
188                 ta = (-p2 - halfdiscroot) * inv3p3;
189                 tb = (-p2 + halfdiscroot) * inv3p3;
190         }
191
192         float t2;
193         float t3;
194
195         if(ta > 0.0f && ta < 1.0f) {
196                 t2 = ta * ta;
197                 t3 = t2 * ta;
198                 *extremta = ta;
199                 *extrema = p3 * t3 + p2 * t2 + p1 * ta + p0;
200
201                 *upper = fmaxf(*extrema, *upper);
202                 *lower = fminf(*extrema, *lower);
203         }
204
205         if(tb > 0.0f && tb < 1.0f) {
206                 t2 = tb * tb;
207                 t3 = t2 * tb;
208                 *extremtb = tb;
209                 *extremb = p3 * t3 + p2 * t2 + p1 * tb + p0;
210
211                 *upper = fmaxf(*extremb, *upper);
212                 *lower = fminf(*extremb, *lower);
213         }
214 }
215
216 #ifdef __KERNEL_SSE2__
217 ccl_device_inline ssef transform_point_T3(const ssef t[3], const ssef &a)
218 {
219         return madd(shuffle<0>(a), t[0], madd(shuffle<1>(a), t[1], shuffle<2>(a) * t[2]));
220 }
221 #endif
222
223 #ifdef __KERNEL_SSE2__
224 /* Pass P and dir by reference to aligned vector */
225 ccl_device_forceinline bool bvh_cardinal_curve_intersect(KernelGlobals *kg, Intersection *isect,
226         const float3 &P, const float3 &dir, uint visibility, int object, int curveAddr, float time, int type, uint *lcg_state, float difl, float extmax)
227 #else
228 ccl_device_forceinline bool bvh_cardinal_curve_intersect(KernelGlobals *kg, Intersection *isect,
229         float3 P, float3 dir, uint visibility, int object, int curveAddr, float time,int type, uint *lcg_state, float difl, float extmax)
230 #endif
231 {
232         const bool is_curve_primitive = (type & PRIMITIVE_CURVE);
233
234         if(!is_curve_primitive && kernel_data.bvh.use_bvh_steps) {
235                 const float2 prim_time = kernel_tex_fetch(__prim_time, curveAddr);
236                 if(time < prim_time.x || time > prim_time.y) {
237                         return false;
238                 }
239         }
240
241         int segment = PRIMITIVE_UNPACK_SEGMENT(type);
242         float epsilon = 0.0f;
243         float r_st, r_en;
244
245         int depth = kernel_data.curve.subdivisions;
246         int flags = kernel_data.curve.curveflags;
247         int prim = kernel_tex_fetch(__prim_index, curveAddr);
248
249 #ifdef __KERNEL_SSE2__
250         ssef vdir = load4f(dir);
251         ssef vcurve_coef[4];
252         const float3 *curve_coef = (float3 *)vcurve_coef;
253         
254         {
255                 ssef dtmp = vdir * vdir;
256                 ssef d_ss = mm_sqrt(dtmp + shuffle<2>(dtmp));
257                 ssef rd_ss = load1f_first(1.0f) / d_ss;
258
259                 ssei v00vec = load4i((ssei *)&kg->__curves.data[prim]);
260                 int2 &v00 = (int2 &)v00vec;
261
262                 int k0 = v00.x + segment;
263                 int k1 = k0 + 1;
264                 int ka = max(k0 - 1, v00.x);
265                 int kb = min(k1 + 1, v00.x + v00.y - 1);
266
267 #ifdef __KERNEL_AVX2__
268                 avxf P_curve_0_1, P_curve_2_3;
269                 if(is_curve_primitive) {
270                         P_curve_0_1 = _mm256_loadu2_m128(&kg->__curve_keys.data[k0].x, &kg->__curve_keys.data[ka].x);
271                         P_curve_2_3 = _mm256_loadu2_m128(&kg->__curve_keys.data[kb].x, &kg->__curve_keys.data[k1].x);
272                 }
273                 else {
274                         int fobject = (object == OBJECT_NONE) ? kernel_tex_fetch(__prim_object, curveAddr) : object;
275                         motion_cardinal_curve_keys_avx(kg, fobject, prim, time, ka, k0, k1, kb, &P_curve_0_1,&P_curve_2_3);
276                 }
277 #else  /* __KERNEL_AVX2__ */
278                 ssef P_curve[4];
279
280                 if(is_curve_primitive) {
281                         P_curve[0] = load4f(&kg->__curve_keys.data[ka].x);
282                         P_curve[1] = load4f(&kg->__curve_keys.data[k0].x);
283                         P_curve[2] = load4f(&kg->__curve_keys.data[k1].x);
284                         P_curve[3] = load4f(&kg->__curve_keys.data[kb].x);
285                 }
286                 else {
287                         int fobject = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, curveAddr): object;
288                         motion_cardinal_curve_keys(kg, fobject, prim, time, ka, k0, k1, kb, (float4*)&P_curve);
289                 }
290 #endif  /* __KERNEL_AVX2__ */
291
292                 ssef rd_sgn = set_sign_bit<0, 1, 1, 1>(shuffle<0>(rd_ss));
293                 ssef mul_zxxy = shuffle<2, 0, 0, 1>(vdir) * rd_sgn;
294                 ssef mul_yz = shuffle<1, 2, 1, 2>(vdir) * mul_zxxy;
295                 ssef mul_shuf = shuffle<0, 1, 2, 3>(mul_zxxy, mul_yz);
296                 ssef vdir0 = vdir & cast(ssei(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0));
297
298                 ssef htfm0 = shuffle<0, 2, 0, 3>(mul_shuf, vdir0);
299                 ssef htfm1 = shuffle<1, 0, 1, 3>(load1f_first(extract<0>(d_ss)), vdir0);
300                 ssef htfm2 = shuffle<1, 3, 2, 3>(mul_shuf, vdir0);
301
302 #ifdef __KERNEL_AVX2__
303                 const avxf vPP = _mm256_broadcast_ps(&P.m128);
304                 const avxf htfm00 = avxf(htfm0.m128, htfm0.m128);
305                 const avxf htfm11 = avxf(htfm1.m128, htfm1.m128);
306                 const avxf htfm22 = avxf(htfm2.m128, htfm2.m128);
307
308                 const avxf p01 = madd(shuffle<0>(P_curve_0_1 - vPP),
309                                       htfm00,
310                                       madd(shuffle<1>(P_curve_0_1 - vPP),
311                                            htfm11,
312                                            shuffle<2>(P_curve_0_1 - vPP) * htfm22));
313                 const avxf p23 = madd(shuffle<0>(P_curve_2_3 - vPP),
314                                       htfm00,
315                                       madd(shuffle<1>(P_curve_2_3 - vPP),
316                                            htfm11,
317                                            shuffle<2>(P_curve_2_3 - vPP)*htfm22));
318
319                 const ssef p0 = _mm256_castps256_ps128(p01);
320                 const ssef p1 = _mm256_extractf128_ps(p01, 1);
321                 const ssef p2 = _mm256_castps256_ps128(p23);
322                 const ssef p3 = _mm256_extractf128_ps(p23, 1);
323
324                 const ssef P_curve_1 = _mm256_extractf128_ps(P_curve_0_1, 1);
325                 r_st = ((float4 &)P_curve_1).w;
326                 const ssef P_curve_2 = _mm256_castps256_ps128(P_curve_2_3);
327                 r_en = ((float4 &)P_curve_2).w;
328 #else  /* __KERNEL_AVX2__ */
329                 ssef htfm[] = { htfm0, htfm1, htfm2 };
330                 ssef vP = load4f(P);
331                 ssef p0 = transform_point_T3(htfm, P_curve[0] - vP);
332                 ssef p1 = transform_point_T3(htfm, P_curve[1] - vP);
333                 ssef p2 = transform_point_T3(htfm, P_curve[2] - vP);
334                 ssef p3 = transform_point_T3(htfm, P_curve[3] - vP);
335
336                 r_st = ((float4 &)P_curve[1]).w;
337                 r_en = ((float4 &)P_curve[2]).w;
338 #endif  /* __KERNEL_AVX2__ */
339
340                 float fc = 0.71f;
341                 ssef vfc = ssef(fc);
342                 ssef vfcxp3 = vfc * p3;
343
344                 vcurve_coef[0] = p1;
345                 vcurve_coef[1] = vfc * (p2 - p0);
346                 vcurve_coef[2] = madd(ssef(fc * 2.0f), p0, madd(ssef(fc - 3.0f), p1, msub(ssef(3.0f - 2.0f * fc), p2, vfcxp3)));
347                 vcurve_coef[3] = msub(ssef(fc - 2.0f), p2 - p1, msub(vfc, p0, vfcxp3));
348
349         }
350 #else
351         float3 curve_coef[4];
352
353         /* curve Intersection check */
354         /* obtain curve parameters */
355         {
356                 /* ray transform created - this should be created at beginning of intersection loop */
357                 Transform htfm;
358                 float d = sqrtf(dir.x * dir.x + dir.z * dir.z);
359                 htfm = make_transform(
360                         dir.z / d, 0, -dir.x /d, 0,
361                         -dir.x * dir.y /d, d, -dir.y * dir.z /d, 0,
362                         dir.x, dir.y, dir.z, 0,
363                         0, 0, 0, 1);
364
365                 float4 v00 = kernel_tex_fetch(__curves, prim);
366
367                 int k0 = __float_as_int(v00.x) + segment;
368                 int k1 = k0 + 1;
369
370                 int ka = max(k0 - 1,__float_as_int(v00.x));
371                 int kb = min(k1 + 1,__float_as_int(v00.x) + __float_as_int(v00.y) - 1);
372
373                 float4 P_curve[4];
374
375                 if(is_curve_primitive) {
376                         P_curve[0] = kernel_tex_fetch(__curve_keys, ka);
377                         P_curve[1] = kernel_tex_fetch(__curve_keys, k0);
378                         P_curve[2] = kernel_tex_fetch(__curve_keys, k1);
379                         P_curve[3] = kernel_tex_fetch(__curve_keys, kb);
380                 }
381                 else {
382                         int fobject = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, curveAddr): object;
383                         motion_cardinal_curve_keys(kg, fobject, prim, time, ka, k0, k1, kb, P_curve);
384                 }
385
386                 float3 p0 = transform_point(&htfm, float4_to_float3(P_curve[0]) - P);
387                 float3 p1 = transform_point(&htfm, float4_to_float3(P_curve[1]) - P);
388                 float3 p2 = transform_point(&htfm, float4_to_float3(P_curve[2]) - P);
389                 float3 p3 = transform_point(&htfm, float4_to_float3(P_curve[3]) - P);
390
391                 float fc = 0.71f;
392                 curve_coef[0] = p1;
393                 curve_coef[1] = -fc*p0 + fc*p2;
394                 curve_coef[2] = 2.0f * fc * p0 + (fc - 3.0f) * p1 + (3.0f - 2.0f * fc) * p2 - fc * p3;
395                 curve_coef[3] = -fc * p0 + (2.0f - fc) * p1 + (fc - 2.0f) * p2 + fc * p3;
396                 r_st = P_curve[1].w;
397                 r_en = P_curve[2].w;
398         }
399 #endif
400
401         float r_curr = max(r_st, r_en);
402
403         if((flags & CURVE_KN_RIBBONS) || !(flags & CURVE_KN_BACKFACING))
404                 epsilon = 2 * r_curr;
405
406         /* find bounds - this is slow for cubic curves */
407         float upper, lower;
408
409         float zextrem[4];
410         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);
411         if(lower - r_curr > isect->t || upper + r_curr < epsilon)
412                 return false;
413
414         /* minimum width extension */
415         float mw_extension = min(difl * fabsf(upper), extmax);
416         float r_ext = mw_extension + r_curr;
417
418         float xextrem[4];
419         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);
420         if(lower > r_ext || upper < -r_ext)
421                 return false;
422
423         float yextrem[4];
424         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);
425         if(lower > r_ext || upper < -r_ext)
426                 return false;
427
428         /* setup recurrent loop */
429         int level = 1 << depth;
430         int tree = 0;
431         float resol = 1.0f / (float)level;
432         bool hit = false;
433
434         /* begin loop */
435         while(!(tree >> (depth))) {
436                 const float i_st = tree * resol;
437                 const float i_en = i_st + (level * resol);
438
439 #ifdef __KERNEL_SSE2__
440                 ssef vi_st = ssef(i_st), vi_en = ssef(i_en);
441                 ssef vp_st = madd(madd(madd(vcurve_coef[3], vi_st, vcurve_coef[2]), vi_st, vcurve_coef[1]), vi_st, vcurve_coef[0]);
442                 ssef vp_en = madd(madd(madd(vcurve_coef[3], vi_en, vcurve_coef[2]), vi_en, vcurve_coef[1]), vi_en, vcurve_coef[0]);
443
444                 ssef vbmin = min(vp_st, vp_en);
445                 ssef vbmax = max(vp_st, vp_en);
446
447                 float3 &bmin = (float3 &)vbmin, &bmax = (float3 &)vbmax;
448                 float &bminx = bmin.x, &bminy = bmin.y, &bminz = bmin.z;
449                 float &bmaxx = bmax.x, &bmaxy = bmax.y, &bmaxz = bmax.z;
450                 float3 &p_st = (float3 &)vp_st, &p_en = (float3 &)vp_en;
451 #else
452                 float3 p_st = ((curve_coef[3] * i_st + curve_coef[2]) * i_st + curve_coef[1]) * i_st + curve_coef[0];
453                 float3 p_en = ((curve_coef[3] * i_en + curve_coef[2]) * i_en + curve_coef[1]) * i_en + curve_coef[0];
454                 
455                 float bminx = min(p_st.x, p_en.x);
456                 float bmaxx = max(p_st.x, p_en.x);
457                 float bminy = min(p_st.y, p_en.y);
458                 float bmaxy = max(p_st.y, p_en.y);
459                 float bminz = min(p_st.z, p_en.z);
460                 float bmaxz = max(p_st.z, p_en.z);
461 #endif
462
463                 if(xextrem[0] >= i_st && xextrem[0] <= i_en) {
464                         bminx = min(bminx,xextrem[1]);
465                         bmaxx = max(bmaxx,xextrem[1]);
466                 }
467                 if(xextrem[2] >= i_st && xextrem[2] <= i_en) {
468                         bminx = min(bminx,xextrem[3]);
469                         bmaxx = max(bmaxx,xextrem[3]);
470                 }
471                 if(yextrem[0] >= i_st && yextrem[0] <= i_en) {
472                         bminy = min(bminy,yextrem[1]);
473                         bmaxy = max(bmaxy,yextrem[1]);
474                 }
475                 if(yextrem[2] >= i_st && yextrem[2] <= i_en) {
476                         bminy = min(bminy,yextrem[3]);
477                         bmaxy = max(bmaxy,yextrem[3]);
478                 }
479                 if(zextrem[0] >= i_st && zextrem[0] <= i_en) {
480                         bminz = min(bminz,zextrem[1]);
481                         bmaxz = max(bmaxz,zextrem[1]);
482                 }
483                 if(zextrem[2] >= i_st && zextrem[2] <= i_en) {
484                         bminz = min(bminz,zextrem[3]);
485                         bmaxz = max(bmaxz,zextrem[3]);
486                 }
487
488                 float r1 = r_st + (r_en - r_st) * i_st;
489                 float r2 = r_st + (r_en - r_st) * i_en;
490                 r_curr = max(r1, r2);
491
492                 mw_extension = min(difl * fabsf(bmaxz), extmax);
493                 float r_ext = mw_extension + r_curr;
494                 float coverage = 1.0f;
495
496                 if(bminz - r_curr > isect->t || bmaxz + r_curr < epsilon || bminx > r_ext|| bmaxx < -r_ext|| bminy > r_ext|| bmaxy < -r_ext) {
497                         /* the bounding box does not overlap the square centered at O */
498                         tree += level;
499                         level = tree & -tree;
500                 }
501                 else if(level == 1) {
502
503                         /* the maximum recursion depth is reached.
504                          * check if dP0.(Q-P0)>=0 and dPn.(Pn-Q)>=0.
505                          * dP* is reversed if necessary.*/
506                         float t = isect->t;
507                         float u = 0.0f;
508                         float gd = 0.0f;
509
510                         if(flags & CURVE_KN_RIBBONS) {
511                                 float3 tg = (p_en - p_st);
512 #ifdef __KERNEL_SSE__
513                                 const float3 tg_sq = tg * tg;
514                                 float w = tg_sq.x + tg_sq.y;
515 #else
516                                 float w = tg.x * tg.x + tg.y * tg.y;
517 #endif
518                                 if(w == 0) {
519                                         tree++;
520                                         level = tree & -tree;
521                                         continue;
522                                 }
523 #ifdef __KERNEL_SSE__
524                                 const float3 p_sttg = p_st * tg;
525                                 w = -(p_sttg.x + p_sttg.y) / w;
526 #else
527                                 w = -(p_st.x * tg.x + p_st.y * tg.y) / w;
528 #endif
529                                 w = saturate(w);
530
531                                 /* compute u on the curve segment */
532                                 u = i_st * (1 - w) + i_en * w;
533                                 r_curr = r_st + (r_en - r_st) * u;
534                                 /* compare x-y distances */
535                                 float3 p_curr = ((curve_coef[3] * u + curve_coef[2]) * u + curve_coef[1]) * u + curve_coef[0];
536
537                                 float3 dp_st = (3 * curve_coef[3] * i_st + 2 * curve_coef[2]) * i_st + curve_coef[1];
538                                 if(dot(tg, dp_st)< 0)
539                                         dp_st *= -1;
540                                 if(dot(dp_st, -p_st) + p_curr.z * dp_st.z < 0) {
541                                         tree++;
542                                         level = tree & -tree;
543                                         continue;
544                                 }
545                                 float3 dp_en = (3 * curve_coef[3] * i_en + 2 * curve_coef[2]) * i_en + curve_coef[1];
546                                 if(dot(tg, dp_en) < 0)
547                                         dp_en *= -1;
548                                 if(dot(dp_en, p_en) - p_curr.z * dp_en.z < 0) {
549                                         tree++;
550                                         level = tree & -tree;
551                                         continue;
552                                 }
553
554                                 /* compute coverage */
555                                 float r_ext = r_curr;
556                                 coverage = 1.0f;
557                                 if(difl != 0.0f) {
558                                         mw_extension = min(difl * fabsf(bmaxz), extmax);
559                                         r_ext = mw_extension + r_curr;
560 #ifdef __KERNEL_SSE__
561                                         const float3 p_curr_sq = p_curr * p_curr;
562                                         const float3 dxxx = _mm_sqrt_ss(_mm_hadd_ps(p_curr_sq.m128, p_curr_sq.m128));
563                                         float d = dxxx.x;
564 #else
565                                         float d = sqrtf(p_curr.x * p_curr.x + p_curr.y * p_curr.y);
566 #endif
567                                         float d0 = d - r_curr;
568                                         float d1 = d + r_curr;
569                                         float inv_mw_extension = 1.0f/mw_extension;
570                                         if(d0 >= 0)
571                                                 coverage = (min(d1 * inv_mw_extension, 1.0f) - min(d0 * inv_mw_extension, 1.0f)) * 0.5f;
572                                         else // inside
573                                                 coverage = (min(d1 * inv_mw_extension, 1.0f) + min(-d0 * inv_mw_extension, 1.0f)) * 0.5f;
574                                 }
575                                 
576                                 if(p_curr.x * p_curr.x + p_curr.y * p_curr.y >= r_ext * r_ext || p_curr.z <= epsilon || isect->t < p_curr.z) {
577                                         tree++;
578                                         level = tree & -tree;
579                                         continue;
580                                 }
581
582                                 t = p_curr.z;
583
584                                 /* stochastic fade from minimum width */
585                                 if(difl != 0.0f && lcg_state) {
586                                         if(coverage != 1.0f && (lcg_step_float(lcg_state) > coverage))
587                                                 return hit;
588                                 }
589                         }
590                         else {
591                                 float l = len(p_en - p_st);
592                                 /* minimum width extension */
593                                 float or1 = r1;
594                                 float or2 = r2;
595
596                                 if(difl != 0.0f) {
597                                         mw_extension = min(len(p_st - P) * difl, extmax);
598                                         or1 = r1 < mw_extension ? mw_extension : r1;
599                                         mw_extension = min(len(p_en - P) * difl, extmax);
600                                         or2 = r2 < mw_extension ? mw_extension : r2;
601                                 }
602                                 /* --- */
603                                 float invl = 1.0f/l;
604                                 float3 tg = (p_en - p_st) * invl;
605                                 gd = (or2 - or1) * invl;
606                                 float difz = -dot(p_st,tg);
607                                 float cyla = 1.0f - (tg.z * tg.z * (1 + gd*gd));
608                                 float invcyla = 1.0f/cyla;
609                                 float halfb = (-p_st.z - tg.z*(difz + gd*(difz*gd + or1)));
610                                 float tcentre = -halfb*invcyla;
611                                 float zcentre = difz + (tg.z * tcentre);
612                                 float3 tdif = - p_st;
613                                 tdif.z += tcentre;
614                                 float tdifz = dot(tdif,tg);
615                                 float tb = 2*(tdif.z - tg.z*(tdifz + gd*(tdifz*gd + or1)));
616                                 float tc = dot(tdif,tdif) - tdifz * tdifz * (1 + gd*gd) - or1*or1 - 2*or1*tdifz*gd;
617                                 float td = tb*tb - 4*cyla*tc;
618                                 if(td < 0.0f) {
619                                         tree++;
620                                         level = tree & -tree;
621                                         continue;
622                                 }
623                                 
624                                 float rootd = sqrtf(td);
625                                 float correction = (-tb - rootd) * 0.5f * invcyla;
626                                 t = tcentre + correction;
627
628                                 float3 dp_st = (3 * curve_coef[3] * i_st + 2 * curve_coef[2]) * i_st + curve_coef[1];
629                                 if(dot(tg, dp_st)< 0)
630                                         dp_st *= -1;
631                                 float3 dp_en = (3 * curve_coef[3] * i_en + 2 * curve_coef[2]) * i_en + curve_coef[1];
632                                 if(dot(tg, dp_en) < 0)
633                                         dp_en *= -1;
634
635                                 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)) {
636                                         correction = (-tb + rootd) * 0.5f * invcyla;
637                                         t = tcentre + correction;
638                                 }                       
639
640                                 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) {
641                                         tree++;
642                                         level = tree & -tree;
643                                         continue;
644                                 }
645
646                                 float w = (zcentre + (tg.z * correction)) * invl;
647                                 w = saturate(w);
648                                 /* compute u on the curve segment */
649                                 u = i_st * (1 - w) + i_en * w;
650
651                                 /* stochastic fade from minimum width */
652                                 if(difl != 0.0f && lcg_state) {
653                                         r_curr = r1 + (r2 - r1) * w;
654                                         r_ext = or1 + (or2 - or1) * w;
655                                         coverage = r_curr/r_ext;
656
657                                         if(coverage != 1.0f && (lcg_step_float(lcg_state) > coverage))
658                                                 return hit;
659                                 }
660                         }
661                         /* we found a new intersection */
662
663 #ifdef __VISIBILITY_FLAG__
664                         /* visibility flag test. we do it here under the assumption
665                          * that most triangles are culled by node flags */
666                         if(kernel_tex_fetch(__prim_visibility, curveAddr) & visibility)
667 #endif
668                         {
669                                 /* record intersection */
670                                 isect->t = t;
671                                 isect->u = u;
672                                 isect->v = gd;
673                                 isect->prim = curveAddr;
674                                 isect->object = object;
675                                 isect->type = type;
676                                 hit = true;
677                         }
678                         
679                         tree++;
680                         level = tree & -tree;
681                 }
682                 else {
683                         /* split the curve into two curves and process */
684                         level = level >> 1;
685                 }
686         }
687
688         return hit;
689 }
690
691 ccl_device_forceinline bool bvh_curve_intersect(KernelGlobals *kg, Intersection *isect,
692         float3 P, float3 direction, uint visibility, int object, int curveAddr, float time, int type, uint *lcg_state, float difl, float extmax)
693 {
694         /* define few macros to minimize code duplication for SSE */
695 #ifndef __KERNEL_SSE2__
696 #  define len3_squared(x) len_squared(x)
697 #  define len3(x) len(x)
698 #  define dot3(x, y) dot(x, y)
699 #endif
700
701         const bool is_curve_primitive = (type & PRIMITIVE_CURVE);
702
703         if(!is_curve_primitive && kernel_data.bvh.use_bvh_steps) {
704                 const float2 prim_time = kernel_tex_fetch(__prim_time, curveAddr);
705                 if(time < prim_time.x || time > prim_time.y) {
706                         return false;
707                 }
708         }
709
710         int segment = PRIMITIVE_UNPACK_SEGMENT(type);
711         /* curve Intersection check */
712         int flags = kernel_data.curve.curveflags;
713
714         int prim = kernel_tex_fetch(__prim_index, curveAddr);
715         float4 v00 = kernel_tex_fetch(__curves, prim);
716
717         int cnum = __float_as_int(v00.x);
718         int k0 = cnum + segment;
719         int k1 = k0 + 1;
720
721 #ifndef __KERNEL_SSE2__
722         float4 P_curve[2];
723
724         if(is_curve_primitive) {
725                 P_curve[0] = kernel_tex_fetch(__curve_keys, k0);
726                 P_curve[1] = kernel_tex_fetch(__curve_keys, k1);
727         }
728         else {
729                 int fobject = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, curveAddr): object;
730                 motion_curve_keys(kg, fobject, prim, time, k0, k1, P_curve);
731         }
732
733         float or1 = P_curve[0].w;
734         float or2 = P_curve[1].w;
735         float3 p1 = float4_to_float3(P_curve[0]);
736         float3 p2 = float4_to_float3(P_curve[1]);
737
738         /* minimum width extension */
739         float r1 = or1;
740         float r2 = or2;
741         float3 dif = P - p1;
742         float3 dif_second = P - p2;
743         if(difl != 0.0f) {
744                 float pixelsize = min(len3(dif) * difl, extmax);
745                 r1 = or1 < pixelsize ? pixelsize : or1;
746                 pixelsize = min(len3(dif_second) * difl, extmax);
747                 r2 = or2 < pixelsize ? pixelsize : or2;
748         }
749         /* --- */
750
751         float3 p21_diff = p2 - p1;
752         float3 sphere_dif1 = (dif + dif_second) * 0.5f;
753         float3 dir = direction;
754         float sphere_b_tmp = dot3(dir, sphere_dif1);
755         float3 sphere_dif2 = sphere_dif1 - sphere_b_tmp * dir;
756 #else
757         ssef P_curve[2];
758         
759         if(is_curve_primitive) {
760                 P_curve[0] = load4f(&kg->__curve_keys.data[k0].x);
761                 P_curve[1] = load4f(&kg->__curve_keys.data[k1].x);
762         }
763         else {
764                 int fobject = (object == OBJECT_NONE)? kernel_tex_fetch(__prim_object, curveAddr): object;
765                 motion_curve_keys(kg, fobject, prim, time, k0, k1, (float4*)&P_curve);
766         }
767
768         const ssef or12 = shuffle<3, 3, 3, 3>(P_curve[0], P_curve[1]);
769
770         ssef r12 = or12;
771         const ssef vP = load4f(P);
772         const ssef dif = vP - P_curve[0];
773         const ssef dif_second = vP - P_curve[1];
774         if(difl != 0.0f) {
775                 const ssef len1_sq = len3_squared_splat(dif);
776                 const ssef len2_sq = len3_squared_splat(dif_second);
777                 const ssef len12 = mm_sqrt(shuffle<0, 0, 0, 0>(len1_sq, len2_sq));
778                 const ssef pixelsize12 = min(len12 * difl, ssef(extmax));
779                 r12 = max(or12, pixelsize12);
780         }
781         float or1 = extract<0>(or12), or2 = extract<0>(shuffle<2>(or12));
782         float r1 = extract<0>(r12), r2 = extract<0>(shuffle<2>(r12));
783
784         const ssef p21_diff = P_curve[1] - P_curve[0];
785         const ssef sphere_dif1 = (dif + dif_second) * 0.5f;
786         const ssef dir = load4f(direction);
787         const ssef sphere_b_tmp = dot3_splat(dir, sphere_dif1);
788         const ssef sphere_dif2 = nmadd(sphere_b_tmp, dir, sphere_dif1);
789 #endif
790
791         float mr = max(r1, r2);
792         float l = len3(p21_diff);
793         float invl = 1.0f / l;
794         float sp_r = mr + 0.5f * l;
795
796         float sphere_b = dot3(dir, sphere_dif2);
797         float sdisc = sphere_b * sphere_b - len3_squared(sphere_dif2) + sp_r * sp_r;
798
799         if(sdisc < 0.0f)
800                 return false;
801
802         /* obtain parameters and test midpoint distance for suitable modes */
803 #ifndef __KERNEL_SSE2__
804         float3 tg = p21_diff * invl;
805 #else
806         const ssef tg = p21_diff * invl;
807 #endif
808         float gd = (r2 - r1) * invl;
809
810         float dirz = dot3(dir, tg);
811         float difz = dot3(dif, tg);
812
813         float a = 1.0f - (dirz*dirz*(1 + gd*gd));
814
815         float halfb = dot3(dir, dif) - dirz*(difz + gd*(difz*gd + r1));
816
817         float tcentre = -halfb/a;
818         float zcentre = difz + (dirz * tcentre);
819
820         if((tcentre > isect->t) && !(flags & CURVE_KN_ACCURATE))
821                 return false;
822         if((zcentre < 0 || zcentre > l) && !(flags & CURVE_KN_ACCURATE) && !(flags & CURVE_KN_INTERSECTCORRECTION))
823                 return false;
824
825         /* test minimum separation */
826 #ifndef __KERNEL_SSE2__
827         float3 cprod = cross(tg, dir);
828         float cprod2sq = len3_squared(cross(tg, dif));
829 #else
830         const ssef cprod = cross(tg, dir);
831         float cprod2sq = len3_squared(cross_zxy(tg, dif));
832 #endif
833         float cprodsq = len3_squared(cprod);
834         float distscaled = dot3(cprod, dif);
835
836         if(cprodsq == 0)
837                 distscaled = cprod2sq;
838         else
839                 distscaled = (distscaled*distscaled)/cprodsq;
840
841         if(distscaled > mr*mr)
842                 return false;
843
844         /* calculate true intersection */
845 #ifndef __KERNEL_SSE2__
846         float3 tdif = dif + tcentre * dir;
847 #else
848         const ssef tdif = madd(ssef(tcentre), dir, dif);
849 #endif
850         float tdifz = dot3(tdif, tg);
851         float tdifma = tdifz*gd + r1;
852         float tb = 2*(dot3(dir, tdif) - dirz*(tdifz + gd*tdifma));
853         float tc = dot3(tdif, tdif) - tdifz*tdifz - tdifma*tdifma;
854         float td = tb*tb - 4*a*tc;
855
856         if(td < 0.0f)
857                 return false;
858
859         float rootd = 0.0f;
860         float correction = 0.0f;
861         if(flags & CURVE_KN_ACCURATE) {
862                 rootd = sqrtf(td);
863                 correction = ((-tb - rootd)/(2*a));
864         }
865
866         float t = tcentre + correction;
867
868         if(t < isect->t) {
869
870                 if(flags & CURVE_KN_INTERSECTCORRECTION) {
871                         rootd = sqrtf(td);
872                         correction = ((-tb - rootd)/(2*a));
873                         t = tcentre + correction;
874                 }
875
876                 float z = zcentre + (dirz * correction);
877                 // bool backface = false;
878
879                 if(flags & CURVE_KN_BACKFACING && (t < 0.0f || z < 0 || z > l)) {
880                         // backface = true;
881                         correction = ((-tb + rootd)/(2*a));
882                         t = tcentre + correction;
883                         z = zcentre + (dirz * correction);
884                 }
885
886                 /* stochastic fade from minimum width */
887                 float adjradius = or1 + z * (or2 - or1) * invl;
888                 adjradius = adjradius / (r1 + z * gd);
889                 if(lcg_state && adjradius != 1.0f) {
890                         if(lcg_step_float(lcg_state) > adjradius)
891                                 return false;
892                 }
893                 /* --- */
894
895                 if(t > 0.0f && t < isect->t && z >= 0 && z <= l) {
896
897                         if(flags & CURVE_KN_ENCLOSEFILTER) {
898                                 float enc_ratio = 1.01f;
899                                 if((difz > -r1 * enc_ratio) && (dot3(dif_second, tg) < r2 * enc_ratio)) {
900                                         float a2 = 1.0f - (dirz*dirz*(1 + gd*gd*enc_ratio*enc_ratio));
901                                         float c2 = dot3(dif, dif) - difz * difz * (1 + gd*gd*enc_ratio*enc_ratio) - r1*r1*enc_ratio*enc_ratio - 2*r1*difz*gd*enc_ratio;
902                                         if(a2*c2 < 0.0f)
903                                                 return false;
904                                 }
905                         }
906
907 #ifdef __VISIBILITY_FLAG__
908                         /* visibility flag test. we do it here under the assumption
909                          * that most triangles are culled by node flags */
910                         if(kernel_tex_fetch(__prim_visibility, curveAddr) & visibility)
911 #endif
912                         {
913                                 /* record intersection */
914                                 isect->t = t;
915                                 isect->u = z*invl;
916                                 isect->v = gd;
917                                 isect->prim = curveAddr;
918                                 isect->object = object;
919                                 isect->type = type;
920
921                                 return true;
922                         }
923                 }
924         }
925
926         return false;
927
928 #ifndef __KERNEL_SSE2__
929 #  undef len3_squared
930 #  undef len3
931 #  undef dot3
932 #endif
933 }
934
935 ccl_device_inline float3 curvetangent(float t, float3 p0, float3 p1, float3 p2, float3 p3)
936 {
937         float fc = 0.71f;
938         float data[4];
939         float t2 = t * t;
940         data[0] = -3.0f * fc          * t2  + 4.0f * fc * t                  - fc;
941         data[1] =  3.0f * (2.0f - fc) * t2  + 2.0f * (fc - 3.0f) * t;
942         data[2] =  3.0f * (fc - 2.0f) * t2  + 2.0f * (3.0f - 2.0f * fc) * t  + fc;
943         data[3] =  3.0f * fc          * t2  - 2.0f * fc * t;
944         return data[0] * p0 + data[1] * p1 + data[2] * p2 + data[3] * p3;
945 }
946
947 ccl_device_inline float3 curvepoint(float t, float3 p0, float3 p1, float3 p2, float3 p3)
948 {
949         float data[4];
950         float fc = 0.71f;
951         float t2 = t * t;
952         float t3 = t2 * t;
953         data[0] = -fc          * t3  + 2.0f * fc          * t2 - fc * t;
954         data[1] =  (2.0f - fc) * t3  + (fc - 3.0f)        * t2 + 1.0f;
955         data[2] =  (fc - 2.0f) * t3  + (3.0f - 2.0f * fc) * t2 + fc * t;
956         data[3] =  fc          * t3  - fc * t2;
957         return data[0] * p0 + data[1] * p1 + data[2] * p2 + data[3] * p3;
958 }
959
960 ccl_device_inline float3 bvh_curve_refine(KernelGlobals *kg, ShaderData *sd, const Intersection *isect, const Ray *ray)
961 {
962         int flag = kernel_data.curve.curveflags;
963         float t = isect->t;
964         float3 P = ray->P;
965         float3 D = ray->D;
966
967         if(isect->object != OBJECT_NONE) {
968 #ifdef __OBJECT_MOTION__
969                 Transform tfm = sd->ob_itfm;
970 #else
971                 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_INVERSE_TRANSFORM);
972 #endif
973
974                 P = transform_point(&tfm, P);
975                 D = transform_direction(&tfm, D*t);
976                 D = normalize_len(D, &t);
977         }
978
979         int prim = kernel_tex_fetch(__prim_index, isect->prim);
980         float4 v00 = kernel_tex_fetch(__curves, prim);
981
982         int k0 = __float_as_int(v00.x) + PRIMITIVE_UNPACK_SEGMENT(sd->type);
983         int k1 = k0 + 1;
984
985         float3 tg;
986
987         if(flag & CURVE_KN_INTERPOLATE) {
988                 int ka = max(k0 - 1,__float_as_int(v00.x));
989                 int kb = min(k1 + 1,__float_as_int(v00.x) + __float_as_int(v00.y) - 1);
990
991                 float4 P_curve[4];
992
993                 if(sd->type & PRIMITIVE_CURVE) {
994                         P_curve[0] = kernel_tex_fetch(__curve_keys, ka);
995                         P_curve[1] = kernel_tex_fetch(__curve_keys, k0);
996                         P_curve[2] = kernel_tex_fetch(__curve_keys, k1);
997                         P_curve[3] = kernel_tex_fetch(__curve_keys, kb);
998                 }
999                 else {
1000                         motion_cardinal_curve_keys(kg, sd->object, sd->prim, sd->time, ka, k0, k1, kb, P_curve);
1001                 }
1002
1003                 float3 p[4];
1004                 p[0] = float4_to_float3(P_curve[0]);
1005                 p[1] = float4_to_float3(P_curve[1]);
1006                 p[2] = float4_to_float3(P_curve[2]);
1007                 p[3] = float4_to_float3(P_curve[3]);
1008
1009                 P = P + D*t;
1010
1011 #ifdef __UV__
1012                 sd->u = isect->u;
1013                 sd->v = 0.0f;
1014 #endif
1015
1016                 tg = normalize(curvetangent(isect->u, p[0], p[1], p[2], p[3]));
1017
1018                 if(kernel_data.curve.curveflags & CURVE_KN_RIBBONS) {
1019                         sd->Ng = normalize(-(D - tg * (dot(tg, D))));
1020                 }
1021                 else {
1022                         /* direction from inside to surface of curve */
1023                         float3 p_curr = curvepoint(isect->u, p[0], p[1], p[2], p[3]);   
1024                         sd->Ng = normalize(P - p_curr);
1025
1026                         /* adjustment for changing radius */
1027                         float gd = isect->v;
1028
1029                         if(gd != 0.0f) {
1030                                 sd->Ng = sd->Ng - gd * tg;
1031                                 sd->Ng = normalize(sd->Ng);
1032                         }
1033                 }
1034
1035                 /* todo: sometimes the normal is still so that this is detected as
1036                  * backfacing even if cull backfaces is enabled */
1037
1038                 sd->N = sd->Ng;
1039         }
1040         else {
1041                 float4 P_curve[2];
1042
1043                 if(sd->type & PRIMITIVE_CURVE) {
1044                         P_curve[0]= kernel_tex_fetch(__curve_keys, k0);
1045                         P_curve[1]= kernel_tex_fetch(__curve_keys, k1);
1046                 }
1047                 else {
1048                         motion_curve_keys(kg, sd->object, sd->prim, sd->time, k0, k1, P_curve);
1049                 }
1050
1051                 float l = 1.0f;
1052                 tg = normalize_len(float4_to_float3(P_curve[1] - P_curve[0]), &l);
1053                 
1054                 P = P + D*t;
1055
1056                 float3 dif = P - float4_to_float3(P_curve[0]);
1057
1058 #ifdef __UV__
1059                 sd->u = dot(dif,tg)/l;
1060                 sd->v = 0.0f;
1061 #endif
1062
1063                 if(flag & CURVE_KN_TRUETANGENTGNORMAL) {
1064                         sd->Ng = -(D - tg * dot(tg, D));
1065                         sd->Ng = normalize(sd->Ng);
1066                 }
1067                 else {
1068                         float gd = isect->v;
1069
1070                         /* direction from inside to surface of curve */
1071                         sd->Ng = (dif - tg * sd->u * l) / (P_curve[0].w + sd->u * l * gd);
1072
1073                         /* adjustment for changing radius */
1074                         if(gd != 0.0f) {
1075                                 sd->Ng = sd->Ng - gd * tg;
1076                                 sd->Ng = normalize(sd->Ng);
1077                         }
1078                 }
1079
1080                 sd->N = sd->Ng;
1081         }
1082
1083 #ifdef __DPDU__
1084         /* dPdu/dPdv */
1085         sd->dPdu = tg;
1086         sd->dPdv = cross(tg, sd->Ng);
1087 #endif
1088
1089         if(isect->object != OBJECT_NONE) {
1090 #ifdef __OBJECT_MOTION__
1091                 Transform tfm = sd->ob_tfm;
1092 #else
1093                 Transform tfm = object_fetch_transform(kg, isect->object, OBJECT_TRANSFORM);
1094 #endif
1095
1096                 P = transform_point(&tfm, P);
1097         }
1098
1099         return P;
1100 }
1101
1102 #endif
1103
1104 CCL_NAMESPACE_END
1105