Cycles Volume Render: support for rendering of homogeneous volume with absorption.
[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 __UV__
392         sd->u = 0.0f;
393         sd->v = 0.0f;
394 #endif
395
396 #ifdef __DPDU__
397         /* dPdu/dPdv */
398         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
399         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
400 #endif
401
402 #ifdef __RAY_DIFFERENTIALS__
403         /* differentials */
404         sd->dP = ray->dD;
405         differential_incoming(&sd->dI, sd->dP);
406         sd->du.dx = 0.0f;
407         sd->du.dy = 0.0f;
408         sd->dv.dx = 0.0f;
409         sd->dv.dy = 0.0f;
410 #endif
411 }
412
413 /* ShaderData setup from point inside volume */
414
415 ccl_device_inline void shader_setup_from_volume(KernelGlobals *kg, ShaderData *sd, const Ray *ray, int volume_shader, int bounce)
416 {
417         /* vectors */
418         sd->P = ray->P;
419         sd->N = -ray->D;  
420         sd->Ng = -ray->D;
421         sd->I = -ray->D;
422         sd->shader = volume_shader;
423         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
424 #ifdef __OBJECT_MOTION__
425         sd->time = ray->time;
426 #endif
427         sd->ray_length = 0.0f; /* todo: can we set this to some useful value? */
428         sd->ray_depth = bounce;
429
430 #ifdef __INSTANCING__
431         sd->object = ~0; /* todo: fill this for texture coordinates */
432 #endif
433         sd->prim = ~0;
434 #ifdef __HAIR__
435         sd->segment = ~0;
436 #endif
437
438 #ifdef __UV__
439         sd->u = 0.0f;
440         sd->v = 0.0f;
441 #endif
442
443 #ifdef __DPDU__
444         /* dPdu/dPdv */
445         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
446         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
447 #endif
448
449 #ifdef __RAY_DIFFERENTIALS__
450         /* differentials */
451         sd->dP = ray->dD;
452         differential_incoming(&sd->dI, sd->dP);
453         sd->du = differential_zero();
454         sd->dv = differential_zero();
455 #endif
456
457         /* for NDC coordinates */
458         sd->ray_P = ray->P;
459         sd->ray_dP = ray->dP;
460 }
461
462 /* BSDF */
463
464 #ifdef __MULTI_CLOSURE__
465
466 ccl_device_inline void _shader_bsdf_multi_eval(KernelGlobals *kg, const ShaderData *sd, const float3 omega_in, float *pdf,
467         int skip_bsdf, BsdfEval *result_eval, float sum_pdf, float sum_sample_weight)
468 {
469         /* this is the veach one-sample model with balance heuristic, some pdf
470          * factors drop out when using balance heuristic weighting */
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 ccl_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         _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 ccl_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 ccl_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 ccl_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 ccl_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 ccl_device float3 shader_bsdf_alpha(KernelGlobals *kg, ShaderData *sd)
626 {
627         float3 alpha = make_float3(1.0f, 1.0f, 1.0f) - shader_bsdf_transparency(kg, sd);
628
629         alpha = max(alpha, make_float3(0.0f, 0.0f, 0.0f));
630         alpha = min(alpha, make_float3(1.0f, 1.0f, 1.0f));
631         
632         return alpha;
633 }
634
635 ccl_device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
636 {
637 #ifdef __MULTI_CLOSURE__
638         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
639
640         for(int i = 0; i< sd->num_closure; i++) {
641                 ShaderClosure *sc = &sd->closure[i];
642
643                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
644                         eval += sc->weight;
645         }
646
647         return eval;
648 #else
649         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
650                 return sd->closure.weight;
651         else
652                 return make_float3(0.0f, 0.0f, 0.0f);
653 #endif
654 }
655
656 ccl_device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
657 {
658 #ifdef __MULTI_CLOSURE__
659         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
660
661         for(int i = 0; i< sd->num_closure; i++) {
662                 ShaderClosure *sc = &sd->closure[i];
663
664                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
665                         eval += sc->weight;
666         }
667
668         return eval;
669 #else
670         if(CLOSURE_IS_BSDF_GLOSSY(sd->closure.type))
671                 return sd->closure.weight;
672         else
673                 return make_float3(0.0f, 0.0f, 0.0f);
674 #endif
675 }
676
677 ccl_device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
678 {
679 #ifdef __MULTI_CLOSURE__
680         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
681
682         for(int i = 0; i< sd->num_closure; i++) {
683                 ShaderClosure *sc = &sd->closure[i];
684
685                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
686                         eval += sc->weight;
687         }
688
689         return eval;
690 #else
691         if(CLOSURE_IS_BSDF_TRANSMISSION(sd->closure.type))
692                 return sd->closure.weight;
693         else
694                 return make_float3(0.0f, 0.0f, 0.0f);
695 #endif
696 }
697
698 ccl_device float3 shader_bsdf_subsurface(KernelGlobals *kg, ShaderData *sd)
699 {
700 #ifdef __MULTI_CLOSURE__
701         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
702
703         for(int i = 0; i< sd->num_closure; i++) {
704                 ShaderClosure *sc = &sd->closure[i];
705
706                 if(CLOSURE_IS_BSSRDF(sc->type))
707                         eval += sc->weight;
708         }
709
710         return eval;
711 #else
712         if(CLOSURE_IS_BSSRDF(sd->closure.type))
713                 return sd->closure.weight;
714         else
715                 return make_float3(0.0f, 0.0f, 0.0f);
716 #endif
717 }
718
719 ccl_device float3 shader_bsdf_ao(KernelGlobals *kg, ShaderData *sd, float ao_factor, float3 *N_)
720 {
721 #ifdef __MULTI_CLOSURE__
722         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
723         float3 N = make_float3(0.0f, 0.0f, 0.0f);
724
725         for(int i = 0; i< sd->num_closure; i++) {
726                 ShaderClosure *sc = &sd->closure[i];
727
728                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
729                         eval += sc->weight*ao_factor;
730                         N += sc->N*average(sc->weight);
731                 }
732                 else if(CLOSURE_IS_AMBIENT_OCCLUSION(sc->type)) {
733                         eval += sc->weight;
734                         N += sd->N*average(sc->weight);
735                 }
736         }
737
738         if(is_zero(N))
739                 N = sd->N;
740         else
741                 N = normalize(N);
742
743         *N_ = N;
744         return eval;
745 #else
746         *N_ = sd->N;
747
748         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
749                 return sd->closure.weight*ao_factor;
750         else if(CLOSURE_IS_AMBIENT_OCCLUSION(sd->closure.type))
751                 return sd->closure.weight;
752         else
753                 return make_float3(0.0f, 0.0f, 0.0f);
754 #endif
755 }
756
757 ccl_device float3 shader_bssrdf_sum(ShaderData *sd, float3 *N_, float *texture_blur_)
758 {
759 #ifdef __MULTI_CLOSURE__
760         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
761         float3 N = make_float3(0.0f, 0.0f, 0.0f);
762         float texture_blur = 0.0f, weight_sum = 0.0f;
763
764         for(int i = 0; i< sd->num_closure; i++) {
765                 ShaderClosure *sc = &sd->closure[i];
766
767                 if(CLOSURE_IS_BSSRDF(sc->type)) {
768                         float avg_weight = fabsf(average(sc->weight));
769
770                         N += sc->N*avg_weight;
771                         eval += sc->weight;
772                         texture_blur += sc->data1*avg_weight;
773                         weight_sum += avg_weight;
774                 }
775         }
776
777         if(N_)
778                 *N_ = (is_zero(N))? sd->N: normalize(N);
779
780         if(texture_blur_)
781                 *texture_blur_ = texture_blur/weight_sum;
782         
783         return eval;
784 #else
785         if(CLOSURE_IS_BSSRDF(sd->closure.type)) {
786                 if(N_) *N_ = sd->closure.N;
787                 if(texture_blur_) *texture_blur_ = sd->closure.data1;
788
789                 return sd->closure.weight;
790         }
791         else {
792                 if(N_) *N_ = sd->N;
793                 if(texture_blur_) *texture_blur_ = 0.0f;
794
795                 return make_float3(0.0f, 0.0f, 0.0f);
796         }
797 #endif
798 }
799
800 /* Emission */
801
802 ccl_device float3 emissive_eval(KernelGlobals *kg, ShaderData *sd, ShaderClosure *sc)
803 {
804         return emissive_simple_eval(sd->Ng, sd->I);
805 }
806
807 ccl_device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
808 {
809         float3 eval;
810 #ifdef __MULTI_CLOSURE__
811         eval = make_float3(0.0f, 0.0f, 0.0f);
812
813         for(int i = 0; i < sd->num_closure; i++) {
814                 ShaderClosure *sc = &sd->closure[i];
815
816                 if(CLOSURE_IS_EMISSION(sc->type))
817                         eval += emissive_eval(kg, sd, sc)*sc->weight;
818         }
819 #else
820         eval = emissive_eval(kg, sd, &sd->closure)*sd->closure.weight;
821 #endif
822
823         return eval;
824 }
825
826 /* Holdout */
827
828 ccl_device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
829 {
830 #ifdef __MULTI_CLOSURE__
831         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
832
833         for(int i = 0; i < sd->num_closure; i++) {
834                 ShaderClosure *sc = &sd->closure[i];
835
836                 if(CLOSURE_IS_HOLDOUT(sc->type))
837                         weight += sc->weight;
838         }
839
840         return weight;
841 #else
842         if(sd->closure.type == CLOSURE_HOLDOUT_ID)
843                 return make_float3(1.0f, 1.0f, 1.0f);
844
845         return make_float3(0.0f, 0.0f, 0.0f);
846 #endif
847 }
848
849 /* Surface Evaluation */
850
851 ccl_device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
852         float randb, int path_flag, ShaderContext ctx)
853 {
854 #ifdef __OSL__
855         if (kg->osl)
856                 OSLShader::eval_surface(kg, sd, randb, path_flag, ctx);
857         else
858 #endif
859         {
860 #ifdef __SVM__
861                 svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, randb, path_flag);
862 #else
863                 sd->closure.weight = make_float3(0.8f, 0.8f, 0.8f);
864                 sd->closure.N = sd->N;
865                 sd->flag |= bsdf_diffuse_setup(&sd->closure);
866 #endif
867         }
868 }
869
870 /* Background Evaluation */
871
872 ccl_device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag, ShaderContext ctx)
873 {
874 #ifdef __OSL__
875         if (kg->osl)
876                 return OSLShader::eval_background(kg, sd, path_flag, ctx);
877         else
878 #endif
879
880         {
881 #ifdef __SVM__
882                 svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, 0.0f, path_flag);
883
884 #ifdef __MULTI_CLOSURE__
885                 float3 eval = make_float3(0.0f, 0.0f, 0.0f);
886
887                 for(int i = 0; i< sd->num_closure; i++) {
888                         const ShaderClosure *sc = &sd->closure[i];
889
890                         if(CLOSURE_IS_BACKGROUND(sc->type))
891                                 eval += sc->weight;
892                 }
893
894                 return eval;
895 #else
896                 if(sd->closure.type == CLOSURE_BACKGROUND_ID)
897                         return sd->closure.weight;
898                 else
899                         return make_float3(0.0f, 0.0f, 0.0f);
900 #endif
901
902 #else
903                 return make_float3(0.8f, 0.8f, 0.8f);
904 #endif
905         }
906 }
907
908 /* Volume */
909
910 ccl_device float3 shader_volume_eval_phase(KernelGlobals *kg, ShaderData *sd,
911         float3 omega_in, float3 omega_out)
912 {
913 #ifdef __MULTI_CLOSURE__
914         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
915         float pdf;
916
917         for(int i = 0; i< sd->num_closure; i++) {
918                 const ShaderClosure *sc = &sd->closure[i];
919
920                 if(CLOSURE_IS_VOLUME(sc->type))
921                         eval += volume_eval_phase(sc, omega_in, omega_out, &pdf);
922         }
923
924         return eval;
925 #else
926         float pdf;
927         return volume_eval_phase(&sd->closure, omega_in, omega_out, &pdf);
928 #endif
929 }
930
931 /* Volume Evaluation */
932
933 ccl_device void shader_eval_volume(KernelGlobals *kg, ShaderData *sd,
934         float randb, int path_flag, ShaderContext ctx)
935 {
936 #ifdef __SVM__
937 #ifdef __OSL__
938         if (kg->osl)
939                 OSLShader::eval_volume(kg, sd, randb, path_flag, ctx);
940         else
941 #endif
942                 svm_eval_nodes(kg, sd, SHADER_TYPE_VOLUME, randb, path_flag);
943 #endif
944 }
945
946 /* Displacement Evaluation */
947
948 ccl_device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd, ShaderContext ctx)
949 {
950         /* this will modify sd->P */
951 #ifdef __SVM__
952 #ifdef __OSL__
953         if (kg->osl)
954                 OSLShader::eval_displacement(kg, sd, ctx);
955         else
956 #endif
957                 svm_eval_nodes(kg, sd, SHADER_TYPE_DISPLACEMENT, 0.0f, 0);
958 #endif
959 }
960
961 /* Transparent Shadows */
962
963 #ifdef __TRANSPARENT_SHADOWS__
964 ccl_device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
965 {
966         int prim = kernel_tex_fetch(__prim_index, isect->prim);
967         int shader = 0;
968
969 #ifdef __HAIR__
970         if(kernel_tex_fetch(__prim_segment, isect->prim) == ~0) {
971 #endif
972                 float4 Ns = kernel_tex_fetch(__tri_normal, prim);
973                 shader = __float_as_int(Ns.w);
974 #ifdef __HAIR__
975         }
976         else {
977                 float4 str = kernel_tex_fetch(__curves, prim);
978                 shader = __float_as_int(str.z);
979         }
980 #endif
981         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
982
983         return (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
984 }
985 #endif
986
987 /* Merging */
988
989 #ifdef __BRANCHED_PATH__
990 ccl_device void shader_merge_closures(KernelGlobals *kg, ShaderData *sd)
991 {
992         /* merge identical closures, better when we sample a single closure at a time */
993         for(int i = 0; i < sd->num_closure; i++) {
994                 ShaderClosure *sci = &sd->closure[i];
995
996                 for(int j = i + 1; j < sd->num_closure; j++) {
997                         ShaderClosure *scj = &sd->closure[j];
998
999 #ifdef __OSL__
1000                         if(!sci->prim && !scj->prim && sci->type == scj->type && sci->data0 == scj->data0 && sci->data1 == scj->data1) {
1001 #else
1002                         if(sci->type == scj->type && sci->data0 == scj->data0 && sci->data1 == scj->data1) {
1003 #endif
1004                                 sci->weight += scj->weight;
1005                                 sci->sample_weight += scj->sample_weight;
1006
1007                                 int size = sd->num_closure - (j+1);
1008                                 if(size > 0) {
1009                                         for(int k = 0; k < size; k++) {
1010                                                 scj[k] = scj[k+1];
1011                                         }
1012                                 }
1013
1014                                 sd->num_closure--;
1015                                 j--;
1016                         }
1017                 }
1018         }
1019 }
1020 #endif
1021
1022 CCL_NAMESPACE_END
1023