Cycles: Make all #include statements relative to cycles source directory
[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 "kernel/closure/alloc.h"
28 #include "kernel/closure/bsdf_util.h"
29 #include "kernel/closure/bsdf.h"
30 #include "kernel/closure/emissive.h"
31
32 #include "kernel/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->object_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_noinline void shader_setup_from_ray(KernelGlobals *kg,
53                                                ShaderData *sd,
54                                                const Intersection *isect,
55                                                const Ray *ray)
56 {
57 #ifdef __INSTANCING__
58         sd->object = (isect->object == PRIM_NONE)? kernel_tex_fetch(__prim_object, isect->prim): isect->object;
59 #endif
60
61         sd->type = isect->type;
62         sd->flag = 0;
63         sd->object_flag = kernel_tex_fetch(__object_flag,
64                                                       sd->object);
65
66         /* matrices and time */
67 #ifdef __OBJECT_MOTION__
68         shader_setup_object_transforms(kg, sd, ray->time);
69         sd->time = ray->time;
70 #endif
71
72         sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
73         sd->ray_length = isect->t;
74
75 #ifdef __UV__
76         sd->u = isect->u;
77         sd->v = isect->v;
78 #endif
79
80 #ifdef __HAIR__
81         if(sd->type & PRIMITIVE_ALL_CURVE) {
82                 /* curve */
83                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
84
85                 sd->shader = __float_as_int(curvedata.z);
86                 sd->P = bvh_curve_refine(kg, sd, isect, ray);
87         }
88         else
89 #endif
90         if(sd->type & PRIMITIVE_TRIANGLE) {
91                 /* static triangle */
92                 float3 Ng = triangle_normal(kg, sd);
93                 sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
94
95                 /* vectors */
96                 sd->P = triangle_refine(kg, sd, isect, ray);
97                 sd->Ng = Ng;
98                 sd->N = Ng;
99                 
100                 /* smooth normal */
101                 if(sd->shader & SHADER_SMOOTH_NORMAL)
102                         sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
103
104 #ifdef __DPDU__
105                 /* dPdu/dPdv */
106                 triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
107 #endif
108         }
109         else {
110                 /* motion triangle */
111                 motion_triangle_shader_setup(kg, sd, isect, ray, false);
112         }
113
114         sd->I = -ray->D;
115
116         sd->flag |= kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*SHADER_SIZE);
117
118 #ifdef __INSTANCING__
119         if(isect->object != OBJECT_NONE) {
120                 /* instance transform */
121                 object_normal_transform_auto(kg, sd, &sd->N);
122                 object_normal_transform_auto(kg, sd, &sd->Ng);
123 #  ifdef __DPDU__
124                 object_dir_transform_auto(kg, sd, &sd->dPdu);
125                 object_dir_transform_auto(kg, sd, &sd->dPdv);
126 #  endif
127         }
128 #endif
129
130         /* backfacing test */
131         bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
132
133         if(backfacing) {
134                 sd->flag |= SD_BACKFACING;
135                 sd->Ng = -sd->Ng;
136                 sd->N = -sd->N;
137 #ifdef __DPDU__
138                 sd->dPdu = -sd->dPdu;
139                 sd->dPdv = -sd->dPdv;
140 #endif
141         }
142
143 #ifdef __RAY_DIFFERENTIALS__
144         /* differentials */
145         differential_transfer(&sd->dP, ray->dP, ray->D, ray->dD, sd->Ng, isect->t);
146         differential_incoming(&sd->dI, ray->dD);
147         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
148 #endif
149 }
150
151 /* ShaderData setup from BSSRDF scatter */
152
153 #ifdef __SUBSURFACE__
154 #  ifndef __KERNEL_CUDA__
155 ccl_device
156 #  else
157 ccl_device_inline
158 #  endif
159 void shader_setup_from_subsurface(
160         KernelGlobals *kg,
161         ShaderData *sd,
162         const Intersection *isect,
163         const Ray *ray)
164 {
165         const bool backfacing = sd->flag & SD_BACKFACING;
166
167         /* object, matrices, time, ray_length stay the same */
168         sd->flag = 0;
169         sd->object_flag = kernel_tex_fetch(__object_flag, sd->object);
170         sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
171         sd->type = isect->type;
172
173 #  ifdef __UV__
174         sd->u = isect->u;
175         sd->v = isect->v;
176 #  endif
177
178         /* fetch triangle data */
179         if(sd->type == PRIMITIVE_TRIANGLE) {
180                 float3 Ng = triangle_normal(kg, sd);
181                 sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
182
183                 /* static triangle */
184                 sd->P = triangle_refine_subsurface(kg, sd, isect, ray);
185                 sd->Ng = Ng;
186                 sd->N = Ng;
187
188                 if(sd->shader & SHADER_SMOOTH_NORMAL)
189                         sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
190
191 #  ifdef __DPDU__
192                 /* dPdu/dPdv */
193                 triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
194 #  endif
195         }
196         else {
197                 /* motion triangle */
198                 motion_triangle_shader_setup(kg, sd, isect, ray, true);
199         }
200
201         sd->flag |= kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*SHADER_SIZE);
202
203 #  ifdef __INSTANCING__
204         if(isect->object != OBJECT_NONE) {
205                 /* instance transform */
206                 object_normal_transform_auto(kg, sd, &sd->N);
207                 object_normal_transform_auto(kg, sd, &sd->Ng);
208 #    ifdef __DPDU__
209                 object_dir_transform_auto(kg, sd, &sd->dPdu);
210                 object_dir_transform_auto(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_inline void shader_setup_from_sample(KernelGlobals *kg,
241                                                 ShaderData *sd,
242                                                 const float3 P,
243                                                 const float3 Ng,
244                                                 const float3 I,
245                                                 int shader, int object, int prim,
246                                                 float u, float v, float t,
247                                                 float time,
248                                                 bool object_space,
249                                                 int lamp)
250 {
251         /* vectors */
252         sd->P = P;
253         sd->N = Ng;
254         sd->Ng = Ng;
255         sd->I = I;
256         sd->shader = shader;
257         if(prim != PRIM_NONE)
258                 sd->type = PRIMITIVE_TRIANGLE;
259         else if(lamp != LAMP_NONE)
260                 sd->type = PRIMITIVE_LAMP;
261         else
262                 sd->type = PRIMITIVE_NONE;
263
264         /* primitive */
265 #ifdef __INSTANCING__
266         sd->object = object;
267 #endif
268         /* currently no access to bvh prim index for strand sd->prim*/
269         sd->prim = prim;
270 #ifdef __UV__
271         sd->u = u;
272         sd->v = v;
273 #endif
274         sd->ray_length = t;
275
276         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*SHADER_SIZE);
277         sd->object_flag = 0;
278         if(sd->object != OBJECT_NONE) {
279                 sd->object_flag |= kernel_tex_fetch(__object_flag,
280                                                                sd->object);
281
282 #ifdef __OBJECT_MOTION__
283                 shader_setup_object_transforms(kg, sd, time);
284                 sd->time = time;
285         }
286         else if(lamp != LAMP_NONE) {
287                 sd->ob_tfm  = lamp_fetch_transform(kg, lamp, false);
288                 sd->ob_itfm = lamp_fetch_transform(kg, lamp, true);
289 #endif
290         }
291
292         /* transform into world space */
293         if(object_space) {
294                 object_position_transform_auto(kg, sd, &sd->P);
295                 object_normal_transform_auto(kg, sd, &sd->Ng);
296                 sd->N = sd->Ng;
297                 object_dir_transform_auto(kg, sd, &sd->I);
298         }
299
300         if(sd->type & PRIMITIVE_TRIANGLE) {
301                 /* smooth normal */
302                 if(sd->shader & SHADER_SMOOTH_NORMAL) {
303                         sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
304
305 #ifdef __INSTANCING__
306                         if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
307                                 object_normal_transform_auto(kg, sd, &sd->N);
308                         }
309 #endif
310                 }
311
312                 /* dPdu/dPdv */
313 #ifdef __DPDU__
314                 triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
315
316 #  ifdef __INSTANCING__
317                 if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
318                         object_dir_transform_auto(kg, sd, &sd->dPdu);
319                         object_dir_transform_auto(kg, sd, &sd->dPdv);
320                 }
321 #  endif
322 #endif
323         }
324         else {
325 #ifdef __DPDU__
326                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
327                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
328 #endif
329         }
330
331         /* backfacing test */
332         if(sd->prim != PRIM_NONE) {
333                 bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
334
335                 if(backfacing) {
336                         sd->flag |= SD_BACKFACING;
337                         sd->Ng = -sd->Ng;
338                         sd->N = -sd->N;
339 #ifdef __DPDU__
340                         sd->dPdu = -sd->dPdu;
341                         sd->dPdv = -sd->dPdv;
342 #endif
343                 }
344         }
345
346 #ifdef __RAY_DIFFERENTIALS__
347         /* no ray differentials here yet */
348         sd->dP = differential3_zero();
349         sd->dI = differential3_zero();
350         sd->du = differential_zero();
351         sd->dv = differential_zero();
352 #endif
353 }
354
355 /* ShaderData setup for displacement */
356
357 ccl_device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
358         int object, int prim, float u, float v)
359 {
360         float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
361         int shader;
362
363         triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
364
365         /* force smooth shading for displacement */
366         shader |= SHADER_SMOOTH_NORMAL;
367
368         shader_setup_from_sample(kg, sd,
369                                  P, Ng, I,
370                                  shader, object, prim,
371                                  u, v, 0.0f, 0.5f,
372                                  !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
373                                  LAMP_NONE);
374 }
375
376 /* ShaderData setup from ray into background */
377
378 ccl_device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
379 {
380         /* vectors */
381         sd->P = ray->D;
382         sd->N = -ray->D;
383         sd->Ng = -ray->D;
384         sd->I = -ray->D;
385         sd->shader = kernel_data.background.surface_shader;
386         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*SHADER_SIZE);
387         sd->object_flag = 0;
388 #ifdef __OBJECT_MOTION__
389         sd->time = ray->time;
390 #endif
391         sd->ray_length = 0.0f;
392
393 #ifdef __INSTANCING__
394         sd->object = PRIM_NONE;
395 #endif
396         sd->prim = PRIM_NONE;
397 #ifdef __UV__
398         sd->u = 0.0f;
399         sd->v = 0.0f;
400 #endif
401
402 #ifdef __DPDU__
403         /* dPdu/dPdv */
404         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
405         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
406 #endif
407
408 #ifdef __RAY_DIFFERENTIALS__
409         /* differentials */
410         sd->dP = ray->dD;
411         differential_incoming(&sd->dI, sd->dP);
412         sd->du = differential_zero();
413         sd->dv = differential_zero();
414 #endif
415 }
416
417 /* ShaderData setup from point inside volume */
418
419 #ifdef __VOLUME__
420 ccl_device_inline void shader_setup_from_volume(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
421 {
422         /* vectors */
423         sd->P = ray->P;
424         sd->N = -ray->D;  
425         sd->Ng = -ray->D;
426         sd->I = -ray->D;
427         sd->shader = SHADER_NONE;
428         sd->flag = 0;
429         sd->object_flag = 0;
430 #ifdef __OBJECT_MOTION__
431         sd->time = ray->time;
432 #endif
433         sd->ray_length = 0.0f; /* todo: can we set this to some useful value? */
434
435 #ifdef __INSTANCING__
436         sd->object = PRIM_NONE; /* todo: fill this for texture coordinates */
437 #endif
438         sd->prim = PRIM_NONE;
439         sd->type = PRIMITIVE_NONE;
440
441 #ifdef __UV__
442         sd->u = 0.0f;
443         sd->v = 0.0f;
444 #endif
445
446 #ifdef __DPDU__
447         /* dPdu/dPdv */
448         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
449         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
450 #endif
451
452 #ifdef __RAY_DIFFERENTIALS__
453         /* differentials */
454         sd->dP = ray->dD;
455         differential_incoming(&sd->dI, sd->dP);
456         sd->du = differential_zero();
457         sd->dv = differential_zero();
458 #endif
459
460         /* for NDC coordinates */
461         sd->ray_P = ray->P;
462         sd->ray_dP = ray->dP;
463 }
464 #endif
465
466 /* Merging */
467
468 #if defined(__BRANCHED_PATH__) || defined(__VOLUME__)
469 ccl_device_inline void shader_merge_closures(ShaderData *sd)
470 {
471         /* merge identical closures, better when we sample a single closure at a time */
472         for(int i = 0; i < sd->num_closure; i++) {
473                 ShaderClosure *sci = &sd->closure[i];
474
475                 for(int j = i + 1; j < sd->num_closure; j++) {
476                         ShaderClosure *scj = &sd->closure[j];
477
478                         if(sci->type != scj->type)
479                                 continue;
480                         if(!bsdf_merge(sci, scj))
481                                 continue;
482
483                         sci->weight += scj->weight;
484                         sci->sample_weight += scj->sample_weight;
485
486                         int size = sd->num_closure - (j+1);
487                         if(size > 0) {
488                                 for(int k = 0; k < size; k++) {
489                                         scj[k] = scj[k+1];
490                                 }
491                         }
492
493                         sd->num_closure--;
494                         kernel_assert(sd->num_closure >= 0);
495                         j--;
496                 }
497         }
498 }
499 #endif
500
501 /* BSDF */
502
503 ccl_device_inline void _shader_bsdf_multi_eval(KernelGlobals *kg, ShaderData *sd, const float3 omega_in, float *pdf,
504         int skip_bsdf, BsdfEval *result_eval, float sum_pdf, float sum_sample_weight)
505 {
506         /* this is the veach one-sample model with balance heuristic, some pdf
507          * factors drop out when using balance heuristic weighting */
508         for(int i = 0; i < sd->num_closure; i++) {
509                 if(i == skip_bsdf)
510                         continue;
511
512                 const ShaderClosure *sc = &sd->closure[i];
513
514                 if(CLOSURE_IS_BSDF(sc->type)) {
515                         float bsdf_pdf = 0.0f;
516                         float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
517
518                         if(bsdf_pdf != 0.0f) {
519                                 bsdf_eval_accum(result_eval, sc->type, eval*sc->weight, 1.0f);
520                                 sum_pdf += bsdf_pdf*sc->sample_weight;
521                         }
522
523                         sum_sample_weight += sc->sample_weight;
524                 }
525         }
526
527         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
528 }
529
530 #ifdef __BRANCHED_PATH__
531 ccl_device_inline void _shader_bsdf_multi_eval_branched(KernelGlobals *kg,
532                                                         ShaderData *sd,
533                                                         const float3 omega_in,
534                                                         BsdfEval *result_eval,
535                                                         float light_pdf,
536                                                         bool use_mis)
537 {
538         for(int i = 0; i < sd->num_closure; i++) {
539                 const ShaderClosure *sc = &sd->closure[i];
540                 if(CLOSURE_IS_BSDF(sc->type)) {
541                         float bsdf_pdf = 0.0f;
542                         float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
543                         if(bsdf_pdf != 0.0f) {
544                                 float mis_weight = use_mis? power_heuristic(light_pdf, bsdf_pdf): 1.0f;
545                                 bsdf_eval_accum(result_eval,
546                                                 sc->type,
547                                                 eval * sc->weight,
548                                                 mis_weight);
549                         }
550                 }
551         }
552 }
553 #endif
554
555
556 #ifndef __KERNEL_CUDA__
557 ccl_device
558 #else
559 ccl_device_inline
560 #endif
561 void shader_bsdf_eval(KernelGlobals *kg,
562                       ShaderData *sd,
563                       const float3 omega_in,
564                       BsdfEval *eval,
565                       float light_pdf,
566                       bool use_mis)
567 {
568         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
569
570 #ifdef __BRANCHED_PATH__
571         if(kernel_data.integrator.branched)
572                 _shader_bsdf_multi_eval_branched(kg, sd, omega_in, eval, light_pdf, use_mis);
573         else
574 #endif
575         {
576                 float pdf;
577                 _shader_bsdf_multi_eval(kg, sd, omega_in, &pdf, -1, eval, 0.0f, 0.0f);
578                 if(use_mis) {
579                         float weight = power_heuristic(light_pdf, pdf);
580                         bsdf_eval_mis(eval, weight);
581                 }
582         }
583 }
584
585 ccl_device_inline int shader_bsdf_sample(KernelGlobals *kg,
586                                          ShaderData *sd,
587                                          float randu, float randv,
588                                          BsdfEval *bsdf_eval,
589                                          float3 *omega_in,
590                                          differential3 *domega_in,
591                                          float *pdf)
592 {
593         int sampled = 0;
594
595         if(sd->num_closure > 1) {
596                 /* pick a BSDF closure based on sample weights */
597                 float sum = 0.0f;
598
599                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
600                         const ShaderClosure *sc = &sd->closure[sampled];
601                         
602                         if(CLOSURE_IS_BSDF(sc->type))
603                                 sum += sc->sample_weight;
604                 }
605
606                 float r = sd->randb_closure*sum;
607                 sum = 0.0f;
608
609                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
610                         const ShaderClosure *sc = &sd->closure[sampled];
611                         
612                         if(CLOSURE_IS_BSDF(sc->type)) {
613                                 sum += sc->sample_weight;
614
615                                 if(r <= sum)
616                                         break;
617                         }
618                 }
619
620                 if(sampled == sd->num_closure) {
621                         *pdf = 0.0f;
622                         return LABEL_NONE;
623                 }
624         }
625
626         const ShaderClosure *sc = &sd->closure[sampled];
627
628         int label;
629         float3 eval;
630
631         *pdf = 0.0f;
632         label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
633
634         if(*pdf != 0.0f) {
635                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
636
637                 if(sd->num_closure > 1) {
638                         float sweight = sc->sample_weight;
639                         _shader_bsdf_multi_eval(kg, sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
640                 }
641         }
642
643         return label;
644 }
645
646 ccl_device int shader_bsdf_sample_closure(KernelGlobals *kg, ShaderData *sd,
647         const ShaderClosure *sc, float randu, float randv, BsdfEval *bsdf_eval,
648         float3 *omega_in, differential3 *domega_in, float *pdf)
649 {
650         int label;
651         float3 eval;
652
653         *pdf = 0.0f;
654         label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
655
656         if(*pdf != 0.0f)
657                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
658
659         return label;
660 }
661
662 ccl_device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
663 {
664         for(int i = 0; i < sd->num_closure; i++) {
665                 ShaderClosure *sc = &sd->closure[i];
666
667                 if(CLOSURE_IS_BSDF(sc->type))
668                         bsdf_blur(kg, sc, roughness);
669         }
670 }
671
672 ccl_device float3 shader_bsdf_transparency(KernelGlobals *kg, ShaderData *sd)
673 {
674         if(sd->flag & SD_HAS_ONLY_VOLUME)
675                 return make_float3(1.0f, 1.0f, 1.0f);
676
677         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
678
679         for(int i = 0; i < sd->num_closure; i++) {
680                 ShaderClosure *sc = &sd->closure[i];
681
682                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) // todo: make this work for osl
683                         eval += sc->weight;
684         }
685
686         return eval;
687 }
688
689 ccl_device void shader_bsdf_disable_transparency(KernelGlobals *kg, ShaderData *sd)
690 {
691         for(int i = 0; i < sd->num_closure; i++) {
692                 ShaderClosure *sc = &sd->closure[i];
693
694                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) {
695                         sc->sample_weight = 0.0f;
696                         sc->weight = make_float3(0.0f, 0.0f, 0.0f);
697                 }
698         }
699 }
700
701 ccl_device float3 shader_bsdf_alpha(KernelGlobals *kg, ShaderData *sd)
702 {
703         float3 alpha = make_float3(1.0f, 1.0f, 1.0f) - shader_bsdf_transparency(kg, sd);
704
705         alpha = max(alpha, make_float3(0.0f, 0.0f, 0.0f));
706         alpha = min(alpha, make_float3(1.0f, 1.0f, 1.0f));
707         
708         return alpha;
709 }
710
711 ccl_device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
712 {
713         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
714
715         for(int i = 0; i < sd->num_closure; i++) {
716                 ShaderClosure *sc = &sd->closure[i];
717
718                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
719                         eval += sc->weight;
720         }
721
722         return eval;
723 }
724
725 ccl_device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
726 {
727         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
728
729         for(int i = 0; i < sd->num_closure; i++) {
730                 ShaderClosure *sc = &sd->closure[i];
731
732                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
733                         eval += sc->weight;
734         }
735
736         return eval;
737 }
738
739 ccl_device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
740 {
741         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
742
743         for(int i = 0; i < sd->num_closure; i++) {
744                 ShaderClosure *sc = &sd->closure[i];
745
746                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
747                         eval += sc->weight;
748         }
749
750         return eval;
751 }
752
753 ccl_device float3 shader_bsdf_subsurface(KernelGlobals *kg, ShaderData *sd)
754 {
755         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
756
757         for(int i = 0; i < sd->num_closure; i++) {
758                 ShaderClosure *sc = &sd->closure[i];
759
760                 if(CLOSURE_IS_BSSRDF(sc->type) || CLOSURE_IS_BSDF_BSSRDF(sc->type))
761                         eval += sc->weight;
762         }
763
764         return eval;
765 }
766
767 ccl_device float3 shader_bsdf_ao(KernelGlobals *kg, ShaderData *sd, float ao_factor, float3 *N_)
768 {
769         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
770         float3 N = make_float3(0.0f, 0.0f, 0.0f);
771
772         for(int i = 0; i < sd->num_closure; i++) {
773                 ShaderClosure *sc = &sd->closure[i];
774
775                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
776                         const DiffuseBsdf *bsdf = (const DiffuseBsdf*)sc;
777                         eval += sc->weight*ao_factor;
778                         N += bsdf->N*average(sc->weight);
779                 }
780                 else if(CLOSURE_IS_AMBIENT_OCCLUSION(sc->type)) {
781                         eval += sc->weight;
782                         N += sd->N*average(sc->weight);
783                 }
784         }
785
786         if(is_zero(N))
787                 N = sd->N;
788         else
789                 N = normalize(N);
790
791         *N_ = N;
792         return eval;
793 }
794
795 #ifdef __SUBSURFACE__
796 ccl_device float3 shader_bssrdf_sum(ShaderData *sd, float3 *N_, float *texture_blur_)
797 {
798         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
799         float3 N = make_float3(0.0f, 0.0f, 0.0f);
800         float texture_blur = 0.0f, weight_sum = 0.0f;
801
802         for(int i = 0; i < sd->num_closure; i++) {
803                 ShaderClosure *sc = &sd->closure[i];
804
805                 if(CLOSURE_IS_BSSRDF(sc->type)) {
806                         const Bssrdf *bssrdf = (const Bssrdf*)sc;
807                         float avg_weight = fabsf(average(sc->weight));
808
809                         N += bssrdf->N*avg_weight;
810                         eval += sc->weight;
811                         texture_blur += bssrdf->texture_blur*avg_weight;
812                         weight_sum += avg_weight;
813                 }
814         }
815
816         if(N_)
817                 *N_ = (is_zero(N))? sd->N: normalize(N);
818
819         if(texture_blur_)
820                 *texture_blur_ = safe_divide(texture_blur, weight_sum);
821         
822         return eval;
823 }
824 #endif
825
826 /* Emission */
827
828 ccl_device float3 emissive_eval(KernelGlobals *kg, ShaderData *sd, ShaderClosure *sc)
829 {
830         return emissive_simple_eval(sd->Ng, sd->I);
831 }
832
833 ccl_device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
834 {
835         float3 eval;
836         eval = make_float3(0.0f, 0.0f, 0.0f);
837
838         for(int i = 0; i < sd->num_closure; i++) {
839                 ShaderClosure *sc = &sd->closure[i];
840
841                 if(CLOSURE_IS_EMISSION(sc->type))
842                         eval += emissive_eval(kg, sd, sc)*sc->weight;
843         }
844
845         return eval;
846 }
847
848 /* Holdout */
849
850 ccl_device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
851 {
852         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
853
854         for(int i = 0; i < sd->num_closure; i++) {
855                 ShaderClosure *sc = &sd->closure[i];
856
857                 if(CLOSURE_IS_HOLDOUT(sc->type))
858                         weight += sc->weight;
859         }
860
861         return weight;
862 }
863
864 /* Surface Evaluation */
865
866 ccl_device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd, RNG *rng,
867         ccl_addr_space PathState *state, float randb, int path_flag, ShaderContext ctx)
868 {
869         sd->num_closure = 0;
870         sd->num_closure_extra = 0;
871         sd->randb_closure = randb;
872
873 #ifdef __OSL__
874         if(kg->osl)
875                 OSLShader::eval_surface(kg, sd, state, path_flag, ctx);
876         else
877 #endif
878         {
879 #ifdef __SVM__
880                 svm_eval_nodes(kg, sd, state, SHADER_TYPE_SURFACE, path_flag);
881 #else
882                 DiffuseBsdf *bsdf = (DiffuseBsdf*)bsdf_alloc(sd,
883                                                              sizeof(DiffuseBsdf),
884                                                              make_float3(0.8f, 0.8f, 0.8f));
885                 bsdf->N = sd->N;
886                 sd->flag |= bsdf_diffuse_setup(bsdf);
887 #endif
888         }
889
890         if(rng && (sd->flag & SD_BSDF_NEEDS_LCG)) {
891                 sd->lcg_state = lcg_state_init(rng, state->rng_offset, state->sample, 0xb4bc3953);
892         }
893 }
894
895 /* Background Evaluation */
896
897 ccl_device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd,
898         ccl_addr_space PathState *state, int path_flag, ShaderContext ctx)
899 {
900         sd->num_closure = 0;
901         sd->num_closure_extra = 0;
902         sd->randb_closure = 0.0f;
903
904 #ifdef __SVM__
905 #ifdef __OSL__
906         if(kg->osl) {
907                 OSLShader::eval_background(kg, sd, state, path_flag, ctx);
908         }
909         else
910 #endif
911         {
912                 svm_eval_nodes(kg, sd, state, SHADER_TYPE_SURFACE, path_flag);
913         }
914
915         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
916
917         for(int i = 0; i < sd->num_closure; i++) {
918                 const ShaderClosure *sc = &sd->closure[i];
919
920                 if(CLOSURE_IS_BACKGROUND(sc->type))
921                         eval += sc->weight;
922         }
923
924         return eval;
925 #else
926         return make_float3(0.8f, 0.8f, 0.8f);
927 #endif
928 }
929
930 /* Volume */
931
932 #ifdef __VOLUME__
933
934 ccl_device_inline void _shader_volume_phase_multi_eval(const ShaderData *sd, const float3 omega_in, float *pdf,
935         int skip_phase, BsdfEval *result_eval, float sum_pdf, float sum_sample_weight)
936 {
937         for(int i = 0; i < sd->num_closure; i++) {
938                 if(i == skip_phase)
939                         continue;
940
941                 const ShaderClosure *sc = &sd->closure[i];
942
943                 if(CLOSURE_IS_PHASE(sc->type)) {
944                         float phase_pdf = 0.0f;
945                         float3 eval = volume_phase_eval(sd, sc, omega_in, &phase_pdf);
946
947                         if(phase_pdf != 0.0f) {
948                                 bsdf_eval_accum(result_eval, sc->type, eval, 1.0f);
949                                 sum_pdf += phase_pdf*sc->sample_weight;
950                         }
951
952                         sum_sample_weight += sc->sample_weight;
953                 }
954         }
955
956         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
957 }
958
959 ccl_device void shader_volume_phase_eval(KernelGlobals *kg, const ShaderData *sd,
960         const float3 omega_in, BsdfEval *eval, float *pdf)
961 {
962         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
963
964         _shader_volume_phase_multi_eval(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
965 }
966
967 ccl_device int shader_volume_phase_sample(KernelGlobals *kg, const ShaderData *sd,
968         float randu, float randv, BsdfEval *phase_eval,
969         float3 *omega_in, differential3 *domega_in, float *pdf)
970 {
971         int sampled = 0;
972
973         if(sd->num_closure > 1) {
974                 /* pick a phase closure based on sample weights */
975                 float sum = 0.0f;
976
977                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
978                         const ShaderClosure *sc = &sd->closure[sampled];
979                         
980                         if(CLOSURE_IS_PHASE(sc->type))
981                                 sum += sc->sample_weight;
982                 }
983
984                 float r = sd->randb_closure*sum;
985                 sum = 0.0f;
986
987                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
988                         const ShaderClosure *sc = &sd->closure[sampled];
989                         
990                         if(CLOSURE_IS_PHASE(sc->type)) {
991                                 sum += sc->sample_weight;
992
993                                 if(r <= sum)
994                                         break;
995                         }
996                 }
997
998                 if(sampled == sd->num_closure) {
999                         *pdf = 0.0f;
1000                         return LABEL_NONE;
1001                 }
1002         }
1003
1004         /* todo: this isn't quite correct, we don't weight anisotropy properly
1005          * depending on color channels, even if this is perhaps not a common case */
1006         const ShaderClosure *sc = &sd->closure[sampled];
1007         int label;
1008         float3 eval;
1009
1010         *pdf = 0.0f;
1011         label = volume_phase_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
1012
1013         if(*pdf != 0.0f) {
1014                 bsdf_eval_init(phase_eval, sc->type, eval, kernel_data.film.use_light_pass);
1015         }
1016
1017         return label;
1018 }
1019
1020 ccl_device int shader_phase_sample_closure(KernelGlobals *kg, const ShaderData *sd,
1021         const ShaderClosure *sc, float randu, float randv, BsdfEval *phase_eval,
1022         float3 *omega_in, differential3 *domega_in, float *pdf)
1023 {
1024         int label;
1025         float3 eval;
1026
1027         *pdf = 0.0f;
1028         label = volume_phase_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
1029
1030         if(*pdf != 0.0f)
1031                 bsdf_eval_init(phase_eval, sc->type, eval, kernel_data.film.use_light_pass);
1032
1033         return label;
1034 }
1035
1036 /* Volume Evaluation */
1037
1038 ccl_device_inline void shader_eval_volume(KernelGlobals *kg,
1039                                           ShaderData *sd,
1040                                           ccl_addr_space PathState *state,
1041                                           ccl_addr_space VolumeStack *stack,
1042                                           int path_flag,
1043                                           ShaderContext ctx)
1044 {
1045         /* reset closures once at the start, we will be accumulating the closures
1046          * for all volumes in the stack into a single array of closures */
1047         sd->num_closure = 0;
1048         sd->num_closure_extra = 0;
1049         sd->flag = 0;
1050         sd->object_flag = 0;
1051
1052         for(int i = 0; stack[i].shader != SHADER_NONE; i++) {
1053                 /* setup shaderdata from stack. it's mostly setup already in
1054                  * shader_setup_from_volume, this switching should be quick */
1055                 sd->object = stack[i].object;
1056                 sd->shader = stack[i].shader;
1057
1058                 sd->flag &= ~SD_SHADER_FLAGS;
1059                 sd->flag |= kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*SHADER_SIZE);
1060                 sd->object_flag &= ~SD_OBJECT_FLAGS;
1061
1062                 if(sd->object != OBJECT_NONE) {
1063                         sd->object_flag |= kernel_tex_fetch(__object_flag, sd->object);
1064
1065 #ifdef __OBJECT_MOTION__
1066                         /* todo: this is inefficient for motion blur, we should be
1067                          * caching matrices instead of recomputing them each step */
1068                         shader_setup_object_transforms(kg, sd, sd->time);
1069 #endif
1070                 }
1071
1072                 /* evaluate shader */
1073 #ifdef __SVM__
1074 #  ifdef __OSL__
1075                 if(kg->osl) {
1076                         OSLShader::eval_volume(kg, sd, state, path_flag, ctx);
1077                 }
1078                 else
1079 #  endif
1080                 {
1081                         svm_eval_nodes(kg, sd, state, SHADER_TYPE_VOLUME, path_flag);
1082                 }
1083 #endif
1084
1085                 /* merge closures to avoid exceeding number of closures limit */
1086                 if(i > 0)
1087                         shader_merge_closures(sd);
1088         }
1089 }
1090
1091 #endif
1092
1093 /* Displacement Evaluation */
1094
1095 ccl_device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd, ccl_addr_space PathState *state, ShaderContext ctx)
1096 {
1097         sd->num_closure = 0;
1098         sd->num_closure_extra = 0;
1099         sd->randb_closure = 0.0f;
1100
1101         /* this will modify sd->P */
1102 #ifdef __SVM__
1103 #  ifdef __OSL__
1104         if(kg->osl)
1105                 OSLShader::eval_displacement(kg, sd, ctx);
1106         else
1107 #  endif
1108         {
1109                 svm_eval_nodes(kg, sd, state, SHADER_TYPE_DISPLACEMENT, 0);
1110         }
1111 #endif
1112 }
1113
1114 /* Transparent Shadows */
1115
1116 #ifdef __TRANSPARENT_SHADOWS__
1117 ccl_device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
1118 {
1119         int prim = kernel_tex_fetch(__prim_index, isect->prim);
1120         int shader = 0;
1121
1122 #ifdef __HAIR__
1123         if(kernel_tex_fetch(__prim_type, isect->prim) & PRIMITIVE_ALL_TRIANGLE) {
1124 #endif
1125                 shader = kernel_tex_fetch(__tri_shader, prim);
1126 #ifdef __HAIR__
1127         }
1128         else {
1129                 float4 str = kernel_tex_fetch(__curves, prim);
1130                 shader = __float_as_int(str.z);
1131         }
1132 #endif
1133         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*SHADER_SIZE);
1134
1135         return (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
1136 }
1137 #endif
1138
1139 CCL_NAMESPACE_END
1140