Cycles / SSS:
[blender.git] / intern / cycles / kernel / kernel_shader.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 /*
20  * ShaderData, used in four steps:
21  *
22  * Setup from incoming ray, sampled position and background.
23  * Execute for surface, volume or displacement.
24  * Evaluate one or more closures.
25  * Release.
26  *
27  */
28
29 #include "closure/bsdf_util.h"
30 #include "closure/bsdf.h"
31 #include "closure/emissive.h"
32 #include "closure/volume.h"
33
34 #include "svm/svm.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 /* ShaderData setup from incoming ray */
39
40 #ifdef __OBJECT_MOTION__
41 #if defined(__KERNEL_CUDA_VERSION__) && __KERNEL_CUDA_VERSION__ <= 42
42 __device_noinline
43 #else
44 __device
45 #endif
46 void shader_setup_object_transforms(KernelGlobals *kg, ShaderData *sd, float time)
47 {
48         /* note that this is a separate non-inlined function to work around crash
49          * on CUDA sm 2.0, otherwise kernel execution crashes (compiler bug?) */
50         if(sd->flag & SD_OBJECT_MOTION) {
51                 sd->ob_tfm = object_fetch_transform_motion(kg, sd->object, time);
52                 sd->ob_itfm= transform_quick_inverse(sd->ob_tfm);
53         }
54         else {
55                 sd->ob_tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
56                 sd->ob_itfm = object_fetch_transform(kg, sd->object, OBJECT_INVERSE_TRANSFORM);
57         }
58 }
59 #endif
60
61 #if defined(__KERNEL_CUDA_VERSION__) && __KERNEL_CUDA_VERSION__ <= 42
62 __device_noinline
63 #else
64 __device
65 #endif
66 void shader_setup_from_ray(KernelGlobals *kg, ShaderData *sd,
67         const Intersection *isect, const Ray *ray, int bounce)
68 {
69 #ifdef __INSTANCING__
70         sd->object = (isect->object == ~0)? kernel_tex_fetch(__prim_object, isect->prim): isect->object;
71 #endif
72
73         sd->flag = kernel_tex_fetch(__object_flag, sd->object);
74
75         /* matrices and time */
76 #ifdef __OBJECT_MOTION__
77         shader_setup_object_transforms(kg, sd, ray->time);
78         sd->time = ray->time;
79 #endif
80
81         sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
82         sd->ray_length = isect->t;
83         sd->ray_depth = bounce;
84
85 #ifdef __HAIR__
86         if(kernel_tex_fetch(__prim_segment, isect->prim) != ~0) {
87                 /* Strand Shader setting*/
88                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
89
90                 sd->shader = __float_as_int(curvedata.z);
91                 sd->segment = isect->segment;
92
93                 float tcorr = isect->t;
94                 if(kernel_data.curve_kernel_data.curveflags & CURVE_KN_POSTINTERSECTCORRECTION) {
95                         tcorr = (isect->u < 0)? tcorr + sqrtf(isect->v) : tcorr - sqrtf(isect->v);
96                         sd->ray_length = tcorr;
97                 }
98
99                 sd->P = bvh_curve_refine(kg, sd, isect, ray, tcorr);
100         }
101         else {
102 #endif
103                 /* fetch triangle data */
104                 float4 Ns = kernel_tex_fetch(__tri_normal, sd->prim);
105                 float3 Ng = make_float3(Ns.x, Ns.y, Ns.z);
106                 sd->shader = __float_as_int(Ns.w);
107
108 #ifdef __HAIR__
109                 sd->segment = ~0;
110                 /*elements for minimum hair width using transparency bsdf*/
111                 /*sd->curve_transparency = 0.0f;*/
112                 /*sd->curve_radius = 0.0f;*/
113 #endif
114
115 #ifdef __UV__
116                 sd->u = isect->u;
117                 sd->v = isect->v;
118 #endif
119
120                 /* vectors */
121                 sd->P = bvh_triangle_refine(kg, sd, isect, ray);
122                 sd->Ng = Ng;
123                 sd->N = Ng;
124                 
125                 /* smooth normal */
126                 if(sd->shader & SHADER_SMOOTH_NORMAL)
127                         sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
128
129 #ifdef __DPDU__
130                 /* dPdu/dPdv */
131                 triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
132 #endif
133
134 #ifdef __HAIR__
135         }
136 #endif
137
138         sd->I = -ray->D;
139
140         sd->flag |= kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
141
142 #ifdef __INSTANCING__
143         if(isect->object != ~0) {
144                 /* instance transform */
145                 object_normal_transform(kg, sd, &sd->N);
146                 object_normal_transform(kg, sd, &sd->Ng);
147 #ifdef __DPDU__
148                 object_dir_transform(kg, sd, &sd->dPdu);
149                 object_dir_transform(kg, sd, &sd->dPdv);
150 #endif
151         }
152 #endif
153
154         /* backfacing test */
155         bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
156
157         if(backfacing) {
158                 sd->flag |= SD_BACKFACING;
159                 sd->Ng = -sd->Ng;
160                 sd->N = -sd->N;
161 #ifdef __DPDU__
162                 sd->dPdu = -sd->dPdu;
163                 sd->dPdv = -sd->dPdv;
164 #endif
165         }
166
167 #ifdef __RAY_DIFFERENTIALS__
168         /* differentials */
169         differential_transfer(&sd->dP, ray->dP, ray->D, ray->dD, sd->Ng, isect->t);
170         differential_incoming(&sd->dI, ray->dD);
171         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
172 #endif
173 }
174
175 /* ShaderData setup from BSSRDF scatter */
176
177 #ifdef __SUBSURFACE__
178 __device_inline void shader_setup_from_subsurface(KernelGlobals *kg, ShaderData *sd,
179         const Intersection *isect, const Ray *ray)
180 {
181         bool backfacing = sd->flag & SD_BACKFACING;
182
183         /* object, matrices, time, ray_length stay the same */
184         sd->flag = kernel_tex_fetch(__object_flag, sd->object);
185         sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
186
187 #ifdef __HAIR__
188         if(kernel_tex_fetch(__prim_segment, isect->prim) != ~0) {
189                 /* Strand Shader setting*/
190                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
191
192                 sd->shader = __float_as_int(curvedata.z);
193                 sd->segment = isect->segment;
194
195                 float tcorr = isect->t;
196                 if(kernel_data.curve_kernel_data.curveflags & CURVE_KN_POSTINTERSECTCORRECTION)
197                         tcorr = (isect->u < 0)? tcorr + sqrtf(isect->v) : tcorr - sqrtf(isect->v);
198
199                 sd->P = bvh_curve_refine(kg, sd, isect, ray, tcorr);
200         }
201         else {
202 #endif
203                 /* fetch triangle data */
204                 float4 Ns = kernel_tex_fetch(__tri_normal, sd->prim);
205                 float3 Ng = make_float3(Ns.x, Ns.y, Ns.z);
206                 sd->shader = __float_as_int(Ns.w);
207
208 #ifdef __HAIR__
209                 sd->segment = ~0;
210 #endif
211
212 #ifdef __UV__
213                 sd->u = isect->u;
214                 sd->v = isect->v;
215 #endif
216
217                 /* vectors */
218                 sd->P = bvh_triangle_refine(kg, sd, isect, ray);
219                 sd->Ng = Ng;
220                 sd->N = Ng;
221                 
222                 /* smooth normal */
223                 if(sd->shader & SHADER_SMOOTH_NORMAL)
224                         sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
225
226 #ifdef __DPDU__
227                 /* dPdu/dPdv */
228                 triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
229 #endif
230
231 #ifdef __HAIR__
232         }
233 #endif
234
235         sd->flag |= kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
236
237 #ifdef __INSTANCING__
238         if(isect->object != ~0) {
239                 /* instance transform */
240                 object_normal_transform(kg, sd, &sd->N);
241                 object_normal_transform(kg, sd, &sd->Ng);
242 #ifdef __DPDU__
243                 object_dir_transform(kg, sd, &sd->dPdu);
244                 object_dir_transform(kg, sd, &sd->dPdv);
245 #endif
246         }
247 #endif
248
249         /* backfacing test */
250         if(backfacing) {
251                 sd->flag |= SD_BACKFACING;
252                 sd->Ng = -sd->Ng;
253                 sd->N = -sd->N;
254 #ifdef __DPDU__
255                 sd->dPdu = -sd->dPdu;
256                 sd->dPdv = -sd->dPdv;
257 #endif
258         }
259
260         /* should not get used in principle as the shading will only use a diffuse
261          * BSDF, but the shader might still access it */
262         sd->I = sd->N;
263
264 #ifdef __RAY_DIFFERENTIALS__
265         /* differentials */
266         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
267         /* don't modify dP and dI */
268 #endif
269 }
270 #endif
271
272 /* ShaderData setup from position sampled on mesh */
273
274 #if defined(__KERNEL_CUDA_VERSION__) && __KERNEL_CUDA_VERSION__ <= 42
275 __device_noinline
276 #else
277 __device
278 #endif
279 void shader_setup_from_sample(KernelGlobals *kg, ShaderData *sd,
280         const float3 P, const float3 Ng, const float3 I,
281         int shader, int object, int prim, float u, float v, float t, float time, int bounce, int segment)
282 {
283         /* vectors */
284         sd->P = P;
285         sd->N = Ng;
286         sd->Ng = Ng;
287         sd->I = I;
288         sd->shader = shader;
289 #ifdef __HAIR__
290         sd->segment = segment;
291 #endif
292
293         /* primitive */
294 #ifdef __INSTANCING__
295         sd->object = object;
296 #endif
297         /* currently no access to bvh prim index for strand sd->prim - this will cause errors with needs fixing*/
298         sd->prim = prim;
299 #ifdef __UV__
300         sd->u = u;
301         sd->v = v;
302 #endif
303         sd->ray_length = t;
304         sd->ray_depth = bounce;
305
306         /* detect instancing, for non-instanced the object index is -object-1 */
307 #ifdef __INSTANCING__
308         bool instanced = false;
309
310         if(sd->prim != ~0) {
311                 if(sd->object >= 0)
312                         instanced = true;
313                 else
314 #endif
315                         sd->object = ~sd->object;
316 #ifdef __INSTANCING__
317         }
318 #endif
319
320         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
321         if(sd->object != -1) {
322                 sd->flag |= kernel_tex_fetch(__object_flag, sd->object);
323
324 #ifdef __OBJECT_MOTION__
325                 shader_setup_object_transforms(kg, sd, time);
326         }
327
328         sd->time = time;
329 #else
330         }
331 #endif
332
333         /* smooth normal */
334 #ifdef __HAIR__
335         if(sd->shader & SHADER_SMOOTH_NORMAL && sd->segment == ~0) {
336                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
337 #else
338         if(sd->shader & SHADER_SMOOTH_NORMAL) {
339                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
340 #endif
341
342 #ifdef __INSTANCING__
343                 if(instanced)
344                         object_normal_transform(kg, sd, &sd->N);
345 #endif
346         }
347
348 #ifdef __DPDU__
349         /* dPdu/dPdv */
350 #ifdef __HAIR__
351         if(sd->prim == ~0 || sd->segment != ~0) {
352                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
353                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
354         }
355 #else
356         if(sd->prim == ~0) {
357                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
358                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
359         }
360 #endif
361         else {
362                 triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
363
364 #ifdef __INSTANCING__
365                 if(instanced) {
366                         object_dir_transform(kg, sd, &sd->dPdu);
367                         object_dir_transform(kg, sd, &sd->dPdv);
368                 }
369 #endif
370         }
371 #endif
372
373         /* backfacing test */
374         if(sd->prim != ~0) {
375                 bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
376
377                 if(backfacing) {
378                         sd->flag |= SD_BACKFACING;
379                         sd->Ng = -sd->Ng;
380                         sd->N = -sd->N;
381 #ifdef __DPDU__
382                         sd->dPdu = -sd->dPdu;
383                         sd->dPdv = -sd->dPdv;
384 #endif
385                 }
386         }
387
388 #ifdef __RAY_DIFFERENTIALS__
389         /* no ray differentials here yet */
390         sd->dP = differential3_zero();
391         sd->dI = differential3_zero();
392         sd->du = differential_zero();
393         sd->dv = differential_zero();
394 #endif
395 }
396
397 /* ShaderData setup for displacement */
398
399 __device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
400         int object, int prim, float u, float v)
401 {
402         float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
403         int shader;
404
405         P = triangle_point_MT(kg, prim, u, v);
406         Ng = triangle_normal_MT(kg, prim, &shader);
407
408         /* force smooth shading for displacement */
409         shader |= SHADER_SMOOTH_NORMAL;
410
411         /* watch out: no instance transform currently */
412
413         shader_setup_from_sample(kg, sd, P, Ng, I, shader, object, prim, u, v, 0.0f, TIME_INVALID, 0, ~0);
414 }
415
416 /* ShaderData setup from ray into background */
417
418 __device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray, int bounce)
419 {
420         /* vectors */
421         sd->P = ray->D;
422         sd->N = -ray->D;
423         sd->Ng = -ray->D;
424         sd->I = -ray->D;
425         sd->shader = kernel_data.background.shader;
426         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
427 #ifdef __OBJECT_MOTION__
428         sd->time = ray->time;
429 #endif
430         sd->ray_length = 0.0f;
431         sd->ray_depth = bounce;
432
433 #ifdef __INSTANCING__
434         sd->object = ~0;
435 #endif
436         sd->prim = ~0;
437 #ifdef __HAIR__
438         sd->segment = ~0;
439 #endif
440 #ifdef __UV__
441         sd->u = 0.0f;
442         sd->v = 0.0f;
443 #endif
444
445 #ifdef __DPDU__
446         /* dPdu/dPdv */
447         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
448         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
449 #endif
450
451 #ifdef __RAY_DIFFERENTIALS__
452         /* differentials */
453         sd->dP = ray->dD;
454         differential_incoming(&sd->dI, sd->dP);
455         sd->du = differential_zero();
456         sd->dv = differential_zero();
457 #endif
458
459         /* for NDC coordinates */
460         sd->ray_P = ray->P;
461         sd->ray_dP = ray->dP;
462 }
463
464 /* BSDF */
465
466 #ifdef __MULTI_CLOSURE__
467
468 __device_inline void _shader_bsdf_multi_eval(KernelGlobals *kg, const ShaderData *sd, const float3 omega_in, float *pdf,
469         int skip_bsdf, BsdfEval *result_eval, float sum_pdf, float sum_sample_weight)
470 {
471         for(int i = 0; i< sd->num_closure; i++) {
472                 if(i == skip_bsdf)
473                         continue;
474
475                 const ShaderClosure *sc = &sd->closure[i];
476
477                 if(CLOSURE_IS_BSDF(sc->type)) {
478                         float bsdf_pdf = 0.0f;
479                         float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
480
481                         if(bsdf_pdf != 0.0f) {
482                                 bsdf_eval_accum(result_eval, sc->type, eval*sc->weight);
483                                 sum_pdf += bsdf_pdf*sc->sample_weight;
484                         }
485
486                         sum_sample_weight += sc->sample_weight;
487                 }
488         }
489
490         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
491 }
492
493 #endif
494
495 __device void shader_bsdf_eval(KernelGlobals *kg, const ShaderData *sd,
496         const float3 omega_in, BsdfEval *eval, float *pdf)
497 {
498 #ifdef __MULTI_CLOSURE__
499         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
500
501         return _shader_bsdf_multi_eval(kg, sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
502 #else
503         const ShaderClosure *sc = &sd->closure;
504
505         *pdf = 0.0f;
506         *eval = bsdf_eval(kg, sd, sc, omega_in, pdf)*sc->weight;
507 #endif
508 }
509
510 __device int shader_bsdf_sample(KernelGlobals *kg, const ShaderData *sd,
511         float randu, float randv, BsdfEval *bsdf_eval,
512         float3 *omega_in, differential3 *domega_in, float *pdf)
513 {
514 #ifdef __MULTI_CLOSURE__
515         int sampled = 0;
516
517         if(sd->num_closure > 1) {
518                 /* pick a BSDF closure based on sample weights */
519                 float sum = 0.0f;
520
521                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
522                         const ShaderClosure *sc = &sd->closure[sampled];
523                         
524                         if(CLOSURE_IS_BSDF(sc->type))
525                                 sum += sc->sample_weight;
526                 }
527
528                 float r = sd->randb_closure*sum;
529                 sum = 0.0f;
530
531                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
532                         const ShaderClosure *sc = &sd->closure[sampled];
533                         
534                         if(CLOSURE_IS_BSDF(sc->type)) {
535                                 sum += sc->sample_weight;
536
537                                 if(r <= sum)
538                                         break;
539                         }
540                 }
541
542                 if(sampled == sd->num_closure) {
543                         *pdf = 0.0f;
544                         return LABEL_NONE;
545                 }
546         }
547
548         const ShaderClosure *sc = &sd->closure[sampled];
549         int label;
550         float3 eval;
551
552         *pdf = 0.0f;
553         label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
554
555         if(*pdf != 0.0f) {
556                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
557
558                 if(sd->num_closure > 1) {
559                         float sweight = sc->sample_weight;
560                         _shader_bsdf_multi_eval(kg, sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
561                 }
562         }
563
564         return label;
565 #else
566         /* sample the single closure that we picked */
567         *pdf = 0.0f;
568         int label = bsdf_sample(kg, sd, &sd->closure, randu, randv, bsdf_eval, omega_in, domega_in, pdf);
569         *bsdf_eval *= sd->closure.weight;
570         return label;
571 #endif
572 }
573
574 __device int shader_bsdf_sample_closure(KernelGlobals *kg, const ShaderData *sd,
575         const ShaderClosure *sc, float randu, float randv, BsdfEval *bsdf_eval,
576         float3 *omega_in, differential3 *domega_in, float *pdf)
577 {
578         int label;
579         float3 eval;
580
581         *pdf = 0.0f;
582         label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
583
584         if(*pdf != 0.0f)
585                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
586
587         return label;
588 }
589
590 __device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
591 {
592 #ifdef __MULTI_CLOSURE__
593         for(int i = 0; i< sd->num_closure; i++) {
594                 ShaderClosure *sc = &sd->closure[i];
595
596                 if(CLOSURE_IS_BSDF(sc->type))
597                         bsdf_blur(kg, sc, roughness);
598         }
599 #else
600         bsdf_blur(kg, &sd->closure, roughness);
601 #endif
602 }
603
604 __device float3 shader_bsdf_transparency(KernelGlobals *kg, ShaderData *sd)
605 {
606 #ifdef __MULTI_CLOSURE__
607         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
608
609         for(int i = 0; i< sd->num_closure; i++) {
610                 ShaderClosure *sc = &sd->closure[i];
611
612                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) // todo: make this work for osl
613                         eval += sc->weight;
614         }
615
616         return eval;
617 #else
618         if(sd->closure.type == CLOSURE_BSDF_TRANSPARENT_ID)
619                 return sd->closure.weight;
620         else
621                 return make_float3(0.0f, 0.0f, 0.0f);
622 #endif
623 }
624
625 __device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
626 {
627 #ifdef __MULTI_CLOSURE__
628         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
629
630         for(int i = 0; i< sd->num_closure; i++) {
631                 ShaderClosure *sc = &sd->closure[i];
632
633                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
634                         eval += sc->weight;
635         }
636
637         return eval;
638 #else
639         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
640                 return sd->closure.weight;
641         else
642                 return make_float3(0.0f, 0.0f, 0.0f);
643 #endif
644 }
645
646 __device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
647 {
648 #ifdef __MULTI_CLOSURE__
649         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
650
651         for(int i = 0; i< sd->num_closure; i++) {
652                 ShaderClosure *sc = &sd->closure[i];
653
654                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
655                         eval += sc->weight;
656         }
657
658         return eval;
659 #else
660         if(CLOSURE_IS_BSDF_GLOSSY(sd->closure.type))
661                 return sd->closure.weight;
662         else
663                 return make_float3(0.0f, 0.0f, 0.0f);
664 #endif
665 }
666
667 __device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
668 {
669 #ifdef __MULTI_CLOSURE__
670         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
671
672         for(int i = 0; i< sd->num_closure; i++) {
673                 ShaderClosure *sc = &sd->closure[i];
674
675                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
676                         eval += sc->weight;
677         }
678
679         return eval;
680 #else
681         if(CLOSURE_IS_BSDF_TRANSMISSION(sd->closure.type))
682                 return sd->closure.weight;
683         else
684                 return make_float3(0.0f, 0.0f, 0.0f);
685 #endif
686 }
687
688 __device float3 shader_bsdf_subsurface(KernelGlobals *kg, ShaderData *sd)
689 {
690 #ifdef __MULTI_CLOSURE__
691         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
692
693         for(int i = 0; i< sd->num_closure; i++) {
694                 ShaderClosure *sc = &sd->closure[i];
695
696                 if(CLOSURE_IS_BSSRDF(sc->type))
697                         eval += sc->weight;
698         }
699
700         return eval;
701 #else
702         if(CLOSURE_IS_BSSRDF(sd->closure.type))
703                 return sd->closure.weight;
704         else
705                 return make_float3(0.0f, 0.0f, 0.0f);
706 #endif
707 }
708
709 __device float3 shader_bsdf_ao(KernelGlobals *kg, ShaderData *sd, float ao_factor, float3 *N)
710 {
711 #ifdef __MULTI_CLOSURE__
712         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
713
714         *N = make_float3(0.0f, 0.0f, 0.0f);
715
716         for(int i = 0; i< sd->num_closure; i++) {
717                 ShaderClosure *sc = &sd->closure[i];
718
719                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
720                         eval += sc->weight*ao_factor;
721                         *N += sc->N*average(sc->weight);
722                 }
723                 if(CLOSURE_IS_AMBIENT_OCCLUSION(sc->type)) {
724                         eval += sc->weight;
725                         *N += sd->N*average(sc->weight);
726                 }
727         }
728
729         if(is_zero(*N))
730                 *N = sd->N;
731         else
732                 *N = normalize(*N);
733
734         return eval;
735 #else
736         *N = sd->N;
737
738         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
739                 return sd->closure.weight*ao_factor;
740         else if(CLOSURE_IS_AMBIENT_OCCLUSION(sd->closure.type))
741                 return sd->closure.weight;
742         else
743                 return make_float3(0.0f, 0.0f, 0.0f);
744 #endif
745 }
746
747 /* Emission */
748
749 __device float3 emissive_eval(KernelGlobals *kg, ShaderData *sd, ShaderClosure *sc)
750 {
751 #ifdef __OSL__
752         if(kg->osl && sc->prim)
753                 return OSLShader::emissive_eval(sd, sc);
754 #endif
755
756         return emissive_simple_eval(sd->Ng, sd->I);
757 }
758
759 __device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
760 {
761         float3 eval;
762 #ifdef __MULTI_CLOSURE__
763         eval = make_float3(0.0f, 0.0f, 0.0f);
764
765         for(int i = 0; i < sd->num_closure; i++) {
766                 ShaderClosure *sc = &sd->closure[i];
767
768                 if(CLOSURE_IS_EMISSION(sc->type))
769                         eval += emissive_eval(kg, sd, sc)*sc->weight;
770         }
771 #else
772         eval = emissive_eval(kg, sd, &sd->closure)*sd->closure.weight;
773 #endif
774
775         return eval;
776 }
777
778 /* Holdout */
779
780 __device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
781 {
782 #ifdef __MULTI_CLOSURE__
783         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
784
785         for(int i = 0; i < sd->num_closure; i++) {
786                 ShaderClosure *sc = &sd->closure[i];
787
788                 if(CLOSURE_IS_HOLDOUT(sc->type))
789                         weight += sc->weight;
790         }
791
792         return weight;
793 #else
794         if(sd->closure.type == CLOSURE_HOLDOUT_ID)
795                 return make_float3(1.0f, 1.0f, 1.0f);
796
797         return make_float3(0.0f, 0.0f, 0.0f);
798 #endif
799 }
800
801 /* Surface Evaluation */
802
803 __device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
804         float randb, int path_flag, ShaderContext ctx)
805 {
806 #ifdef __OSL__
807         if (kg->osl)
808                 OSLShader::eval_surface(kg, sd, randb, path_flag, ctx);
809         else
810 #endif
811         {
812 #ifdef __SVM__
813                 svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, randb, path_flag);
814 #else
815                 sd->closure.weight = make_float3(0.8f, 0.8f, 0.8f);
816                 sd->closure.N = sd->N;
817                 sd->flag |= bsdf_diffuse_setup(&sd->closure);
818 #endif
819         }
820 }
821
822 /* Background Evaluation */
823
824 __device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx)
825 {
826 #ifdef __OSL__
827         if (kg->osl)
828                 return OSLShader::eval_background(kg, sd, path_flag, ctx);
829         else
830 #endif
831
832         {
833 #ifdef __SVM__
834                 svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, 0.0f, path_flag);
835
836 #ifdef __MULTI_CLOSURE__
837                 float3 eval = make_float3(0.0f, 0.0f, 0.0f);
838
839                 for(int i = 0; i< sd->num_closure; i++) {
840                         const ShaderClosure *sc = &sd->closure[i];
841
842                         if(CLOSURE_IS_BACKGROUND(sc->type))
843                                 eval += sc->weight;
844                 }
845
846                 return eval;
847 #else
848                 if(sd->closure.type == CLOSURE_BACKGROUND_ID)
849                         return sd->closure.weight;
850                 else
851                         return make_float3(0.0f, 0.0f, 0.0f);
852 #endif
853
854 #else
855                 return make_float3(0.8f, 0.8f, 0.8f);
856 #endif
857         }
858 }
859
860 /* Volume */
861
862 __device float3 shader_volume_eval_phase(KernelGlobals *kg, ShaderData *sd,
863         float3 omega_in, float3 omega_out)
864 {
865 #ifdef __MULTI_CLOSURE__
866         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
867
868         for(int i = 0; i< sd->num_closure; i++) {
869                 const ShaderClosure *sc = &sd->closure[i];
870
871                 if(CLOSURE_IS_VOLUME(sc->type))
872                         eval += volume_eval_phase(kg, sc, omega_in, omega_out);
873         }
874
875         return eval;
876 #else
877         return volume_eval_phase(kg, &sd->closure, omega_in, omega_out);
878 #endif
879 }
880
881 /* Volume Evaluation */
882
883 __device void shader_eval_volume(KernelGlobals *kg, ShaderData *sd,
884         float randb, int path_flag, ShaderContext ctx)
885 {
886 #ifdef __SVM__
887 #ifdef __OSL__
888         if (kg->osl)
889                 OSLShader::eval_volume(kg, sd, randb, path_flag, ctx);
890         else
891 #endif
892                 svm_eval_nodes(kg, sd, SHADER_TYPE_VOLUME, randb, path_flag);
893 #endif
894 }
895
896 /* Displacement Evaluation */
897
898 __device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderContext ctx)
899 {
900         /* this will modify sd->P */
901 #ifdef __SVM__
902 #ifdef __OSL__
903         if (kg->osl)
904                 OSLShader::eval_displacement(kg, sd, ctx);
905         else
906 #endif
907                 svm_eval_nodes(kg, sd, SHADER_TYPE_DISPLACEMENT, 0.0f, 0);
908 #endif
909 }
910
911 /* Transparent Shadows */
912
913 #ifdef __TRANSPARENT_SHADOWS__
914 __device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
915 {
916         int prim = kernel_tex_fetch(__prim_index, isect->prim);
917         int shader = 0;
918
919 #ifdef __HAIR__
920         if(kernel_tex_fetch(__prim_segment, isect->prim) == ~0) {
921 #endif
922                 float4 Ns = kernel_tex_fetch(__tri_normal, prim);
923                 shader = __float_as_int(Ns.w);
924 #ifdef __HAIR__
925         }
926         else {
927                 float4 str = kernel_tex_fetch(__curves, prim);
928                 shader = __float_as_int(str.z);
929         }
930 #endif
931         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
932
933         return (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
934 }
935 #endif
936
937 /* Merging */
938
939 #ifdef __NON_PROGRESSIVE__
940 __device void shader_merge_closures(KernelGlobals *kg, ShaderData *sd)
941 {
942         /* merge identical closures, better when we sample a single closure at a time */
943         for(int i = 0; i < sd->num_closure; i++) {
944                 ShaderClosure *sci = &sd->closure[i];
945
946                 for(int j = i + 1; j < sd->num_closure; j++) {
947                         ShaderClosure *scj = &sd->closure[j];
948
949 #ifdef __OSL__
950                         if(!sci->prim && !scj->prim && sci->type == scj->type && sci->data0 == scj->data0 && sci->data1 == scj->data1) {
951 #else
952                         if(sci->type == scj->type && sci->data0 == scj->data0 && sci->data1 == scj->data1) {
953 #endif
954                                 sci->weight += scj->weight;
955                                 sci->sample_weight += scj->sample_weight;
956
957                                 int size = sd->num_closure - (j+1);
958                                 if(size > 0)
959                                         memmove(scj, scj+1, size*sizeof(ShaderClosure));
960
961                                 sd->num_closure--;
962                                 j--;
963                         }
964                 }
965         }
966 }
967 #endif
968
969 CCL_NAMESPACE_END
970