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