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