Merge branch 'master' into blender2.8
[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         PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
58
59 #ifdef __INSTANCING__
60         sd->object = (isect->object == PRIM_NONE)? kernel_tex_fetch(__prim_object, isect->prim): isect->object;
61 #endif
62         sd->lamp = LAMP_NONE;
63
64         sd->type = isect->type;
65         sd->flag = 0;
66         sd->object_flag = kernel_tex_fetch(__object_flag,
67                                                       sd->object);
68
69         /* matrices and time */
70 #ifdef __OBJECT_MOTION__
71         shader_setup_object_transforms(kg, sd, ray->time);
72 #endif
73         sd->time = ray->time;
74
75         sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
76         sd->ray_length = isect->t;
77
78 #ifdef __UV__
79         sd->u = isect->u;
80         sd->v = isect->v;
81 #endif
82
83 #ifdef __HAIR__
84         if(sd->type & PRIMITIVE_ALL_CURVE) {
85                 /* curve */
86                 float4 curvedata = kernel_tex_fetch(__curves, sd->prim);
87
88                 sd->shader = __float_as_int(curvedata.z);
89                 sd->P = curve_refine(kg, sd, isect, ray);
90         }
91         else
92 #endif
93         if(sd->type & PRIMITIVE_TRIANGLE) {
94                 /* static triangle */
95                 float3 Ng = triangle_normal(kg, sd);
96                 sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
97
98                 /* vectors */
99                 sd->P = triangle_refine(kg, sd, isect, ray);
100                 sd->Ng = Ng;
101                 sd->N = Ng;
102
103                 /* smooth normal */
104                 if(sd->shader & SHADER_SMOOTH_NORMAL)
105                         sd->N = triangle_smooth_normal(kg, Ng, sd->prim, sd->u, sd->v);
106
107 #ifdef __DPDU__
108                 /* dPdu/dPdv */
109                 triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
110 #endif
111         }
112         else {
113                 /* motion triangle */
114                 motion_triangle_shader_setup(kg, sd, isect, ray, false);
115         }
116
117         sd->I = -ray->D;
118
119         sd->flag |= kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
120
121 #ifdef __INSTANCING__
122         if(isect->object != OBJECT_NONE) {
123                 /* instance transform */
124                 object_normal_transform_auto(kg, sd, &sd->N);
125                 object_normal_transform_auto(kg, sd, &sd->Ng);
126 #  ifdef __DPDU__
127                 object_dir_transform_auto(kg, sd, &sd->dPdu);
128                 object_dir_transform_auto(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         PROFILING_SHADER(sd->shader);
154         PROFILING_OBJECT(sd->object);
155 }
156
157 /* ShaderData setup from BSSRDF scatter */
158
159 #ifdef __SUBSURFACE__
160 #  ifndef __KERNEL_CUDA__
161 ccl_device
162 #  else
163 ccl_device_inline
164 #  endif
165 void shader_setup_from_subsurface(
166         KernelGlobals *kg,
167         ShaderData *sd,
168         const Intersection *isect,
169         const Ray *ray)
170 {
171         PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
172
173         const bool backfacing = sd->flag & SD_BACKFACING;
174
175         /* object, matrices, time, ray_length stay the same */
176         sd->flag = 0;
177         sd->object_flag = kernel_tex_fetch(__object_flag, sd->object);
178         sd->prim = kernel_tex_fetch(__prim_index, isect->prim);
179         sd->type = isect->type;
180
181 #  ifdef __UV__
182         sd->u = isect->u;
183         sd->v = isect->v;
184 #  endif
185
186         /* fetch triangle data */
187         if(sd->type == PRIMITIVE_TRIANGLE) {
188                 float3 Ng = triangle_normal(kg, sd);
189                 sd->shader = kernel_tex_fetch(__tri_shader, sd->prim);
190
191                 /* static triangle */
192                 sd->P = triangle_refine_local(kg, sd, isect, ray);
193                 sd->Ng = Ng;
194                 sd->N = Ng;
195
196                 if(sd->shader & SHADER_SMOOTH_NORMAL)
197                         sd->N = triangle_smooth_normal(kg, Ng, sd->prim, sd->u, sd->v);
198
199 #  ifdef __DPDU__
200                 /* dPdu/dPdv */
201                 triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
202 #  endif
203         }
204         else {
205                 /* motion triangle */
206                 motion_triangle_shader_setup(kg, sd, isect, ray, true);
207         }
208
209         sd->flag |= kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
210
211 #  ifdef __INSTANCING__
212         if(isect->object != OBJECT_NONE) {
213                 /* instance transform */
214                 object_normal_transform_auto(kg, sd, &sd->N);
215                 object_normal_transform_auto(kg, sd, &sd->Ng);
216 #    ifdef __DPDU__
217                 object_dir_transform_auto(kg, sd, &sd->dPdu);
218                 object_dir_transform_auto(kg, sd, &sd->dPdv);
219 #    endif
220         }
221 #  endif
222
223         /* backfacing test */
224         if(backfacing) {
225                 sd->flag |= SD_BACKFACING;
226                 sd->Ng = -sd->Ng;
227                 sd->N = -sd->N;
228 #  ifdef __DPDU__
229                 sd->dPdu = -sd->dPdu;
230                 sd->dPdv = -sd->dPdv;
231 #  endif
232         }
233
234         /* should not get used in principle as the shading will only use a diffuse
235          * BSDF, but the shader might still access it */
236         sd->I = sd->N;
237
238 #  ifdef __RAY_DIFFERENTIALS__
239         /* differentials */
240         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
241         /* don't modify dP and dI */
242 #  endif
243
244         PROFILING_SHADER(sd->shader);
245 }
246 #endif
247
248 /* ShaderData setup from position sampled on mesh */
249
250 ccl_device_inline void shader_setup_from_sample(KernelGlobals *kg,
251                                                 ShaderData *sd,
252                                                 const float3 P,
253                                                 const float3 Ng,
254                                                 const float3 I,
255                                                 int shader, int object, int prim,
256                                                 float u, float v, float t,
257                                                 float time,
258                                                 bool object_space,
259                                                 int lamp)
260 {
261         PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
262
263         /* vectors */
264         sd->P = P;
265         sd->N = Ng;
266         sd->Ng = Ng;
267         sd->I = I;
268         sd->shader = shader;
269         if(prim != PRIM_NONE)
270                 sd->type = PRIMITIVE_TRIANGLE;
271         else if(lamp != LAMP_NONE)
272                 sd->type = PRIMITIVE_LAMP;
273         else
274                 sd->type = PRIMITIVE_NONE;
275
276         /* primitive */
277 #ifdef __INSTANCING__
278         sd->object = object;
279 #endif
280         sd->lamp = LAMP_NONE;
281         /* currently no access to bvh prim index for strand sd->prim*/
282         sd->prim = prim;
283 #ifdef __UV__
284         sd->u = u;
285         sd->v = v;
286 #endif
287         sd->time = time;
288         sd->ray_length = t;
289
290         sd->flag = kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
291         sd->object_flag = 0;
292         if(sd->object != OBJECT_NONE) {
293                 sd->object_flag |= kernel_tex_fetch(__object_flag,
294                                                     sd->object);
295
296 #ifdef __OBJECT_MOTION__
297                 shader_setup_object_transforms(kg, sd, time);
298         }
299         else if(lamp != LAMP_NONE) {
300                 sd->ob_tfm  = lamp_fetch_transform(kg, lamp, false);
301                 sd->ob_itfm = lamp_fetch_transform(kg, lamp, true);
302                 sd->lamp = lamp;
303 #endif
304         }
305
306         /* transform into world space */
307         if(object_space) {
308                 object_position_transform_auto(kg, sd, &sd->P);
309                 object_normal_transform_auto(kg, sd, &sd->Ng);
310                 sd->N = sd->Ng;
311                 object_dir_transform_auto(kg, sd, &sd->I);
312         }
313
314         if(sd->type & PRIMITIVE_TRIANGLE) {
315                 /* smooth normal */
316                 if(sd->shader & SHADER_SMOOTH_NORMAL) {
317                         sd->N = triangle_smooth_normal(kg, Ng, sd->prim, sd->u, sd->v);
318
319 #ifdef __INSTANCING__
320                         if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
321                                 object_normal_transform_auto(kg, sd, &sd->N);
322                         }
323 #endif
324                 }
325
326                 /* dPdu/dPdv */
327 #ifdef __DPDU__
328                 triangle_dPdudv(kg, sd->prim, &sd->dPdu, &sd->dPdv);
329
330 #  ifdef __INSTANCING__
331                 if(!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
332                         object_dir_transform_auto(kg, sd, &sd->dPdu);
333                         object_dir_transform_auto(kg, sd, &sd->dPdv);
334                 }
335 #  endif
336 #endif
337         }
338         else {
339 #ifdef __DPDU__
340                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
341                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
342 #endif
343         }
344
345         /* backfacing test */
346         if(sd->prim != PRIM_NONE) {
347                 bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
348
349                 if(backfacing) {
350                         sd->flag |= SD_BACKFACING;
351                         sd->Ng = -sd->Ng;
352                         sd->N = -sd->N;
353 #ifdef __DPDU__
354                         sd->dPdu = -sd->dPdu;
355                         sd->dPdv = -sd->dPdv;
356 #endif
357                 }
358         }
359
360 #ifdef __RAY_DIFFERENTIALS__
361         /* no ray differentials here yet */
362         sd->dP = differential3_zero();
363         sd->dI = differential3_zero();
364         sd->du = differential_zero();
365         sd->dv = differential_zero();
366 #endif
367
368         PROFILING_SHADER(sd->shader);
369         PROFILING_OBJECT(sd->object);
370 }
371
372 /* ShaderData setup for displacement */
373
374 ccl_device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
375         int object, int prim, float u, float v)
376 {
377         float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
378         int shader;
379
380         triangle_point_normal(kg, object, prim, u, v, &P, &Ng, &shader);
381
382         /* force smooth shading for displacement */
383         shader |= SHADER_SMOOTH_NORMAL;
384
385         shader_setup_from_sample(kg, sd,
386                                  P, Ng, I,
387                                  shader, object, prim,
388                                  u, v, 0.0f, 0.5f,
389                                  !(kernel_tex_fetch(__object_flag, object) & SD_OBJECT_TRANSFORM_APPLIED),
390                                  LAMP_NONE);
391 }
392
393 /* ShaderData setup from ray into background */
394
395 ccl_device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
396 {
397         PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
398
399         /* vectors */
400         sd->P = ray->D;
401         sd->N = -ray->D;
402         sd->Ng = -ray->D;
403         sd->I = -ray->D;
404         sd->shader = kernel_data.background.surface_shader;
405         sd->flag = kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
406         sd->object_flag = 0;
407         sd->time = ray->time;
408         sd->ray_length = 0.0f;
409
410 #ifdef __INSTANCING__
411         sd->object = PRIM_NONE;
412 #endif
413         sd->lamp = LAMP_NONE;
414         sd->prim = PRIM_NONE;
415 #ifdef __UV__
416         sd->u = 0.0f;
417         sd->v = 0.0f;
418 #endif
419
420 #ifdef __DPDU__
421         /* dPdu/dPdv */
422         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
423         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
424 #endif
425
426 #ifdef __RAY_DIFFERENTIALS__
427         /* differentials */
428         sd->dP = ray->dD;
429         differential_incoming(&sd->dI, sd->dP);
430         sd->du = differential_zero();
431         sd->dv = differential_zero();
432 #endif
433
434         PROFILING_SHADER(sd->shader);
435         PROFILING_OBJECT(sd->object);
436 }
437
438 /* ShaderData setup from point inside volume */
439
440 #ifdef __VOLUME__
441 ccl_device_inline void shader_setup_from_volume(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
442 {
443         PROFILING_INIT(kg, PROFILING_SHADER_SETUP);
444
445         /* vectors */
446         sd->P = ray->P;
447         sd->N = -ray->D;
448         sd->Ng = -ray->D;
449         sd->I = -ray->D;
450         sd->shader = SHADER_NONE;
451         sd->flag = 0;
452         sd->object_flag = 0;
453         sd->time = ray->time;
454         sd->ray_length = 0.0f; /* todo: can we set this to some useful value? */
455
456 #  ifdef __INSTANCING__
457         sd->object = PRIM_NONE; /* todo: fill this for texture coordinates */
458 #  endif
459         sd->lamp = LAMP_NONE;
460         sd->prim = PRIM_NONE;
461         sd->type = PRIMITIVE_NONE;
462
463 #  ifdef __UV__
464         sd->u = 0.0f;
465         sd->v = 0.0f;
466 #  endif
467
468 #  ifdef __DPDU__
469         /* dPdu/dPdv */
470         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
471         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
472 #  endif
473
474 #  ifdef __RAY_DIFFERENTIALS__
475         /* differentials */
476         sd->dP = ray->dD;
477         differential_incoming(&sd->dI, sd->dP);
478         sd->du = differential_zero();
479         sd->dv = differential_zero();
480 #  endif
481
482         /* for NDC coordinates */
483         sd->ray_P = ray->P;
484         sd->ray_dP = ray->dP;
485
486         PROFILING_SHADER(sd->shader);
487         PROFILING_OBJECT(sd->object);
488 }
489 #endif  /* __VOLUME__ */
490
491 /* Merging */
492
493 #if defined(__BRANCHED_PATH__) || defined(__VOLUME__)
494 ccl_device_inline void shader_merge_closures(ShaderData *sd)
495 {
496         /* merge identical closures, better when we sample a single closure at a time */
497         for(int i = 0; i < sd->num_closure; i++) {
498                 ShaderClosure *sci = &sd->closure[i];
499
500                 for(int j = i + 1; j < sd->num_closure; j++) {
501                         ShaderClosure *scj = &sd->closure[j];
502
503                         if(sci->type != scj->type)
504                                 continue;
505                         if(!bsdf_merge(sci, scj))
506                                 continue;
507
508                         sci->weight += scj->weight;
509                         sci->sample_weight += scj->sample_weight;
510
511                         int size = sd->num_closure - (j+1);
512                         if(size > 0) {
513                                 for(int k = 0; k < size; k++) {
514                                         scj[k] = scj[k+1];
515                                 }
516                         }
517
518                         sd->num_closure--;
519                         kernel_assert(sd->num_closure >= 0);
520                         j--;
521                 }
522         }
523 }
524 #endif  /* __BRANCHED_PATH__ || __VOLUME__ */
525
526 /* Defensive sampling. */
527
528 ccl_device_inline void shader_prepare_closures(ShaderData *sd,
529                                                ccl_addr_space PathState *state)
530 {
531         /* We can likely also do defensive sampling at deeper bounces, particularly
532          * for cases like a perfect mirror but possibly also others. This will need
533          * a good heuristic. */
534         if(state->bounce + state->transparent_bounce == 0 && sd->num_closure > 1) {
535                 float sum = 0.0f;
536
537                 for(int i = 0; i < sd->num_closure; i++) {
538                         ShaderClosure *sc = &sd->closure[i];
539                         if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
540                                 sum += sc->sample_weight;
541                         }
542                 }
543
544                 for(int i = 0; i < sd->num_closure; i++) {
545                         ShaderClosure *sc = &sd->closure[i];
546                         if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
547                                 sc->sample_weight = max(sc->sample_weight, 0.125f * sum);
548                         }
549                 }
550         }
551 }
552
553
554 /* BSDF */
555
556 ccl_device_inline void _shader_bsdf_multi_eval(KernelGlobals *kg, ShaderData *sd, const float3 omega_in, float *pdf,
557         const ShaderClosure *skip_sc, BsdfEval *result_eval, float sum_pdf, float sum_sample_weight)
558 {
559         /* this is the veach one-sample model with balance heuristic, some pdf
560          * factors drop out when using balance heuristic weighting */
561         for(int i = 0; i < sd->num_closure; i++) {
562                 const ShaderClosure *sc = &sd->closure[i];
563
564                 if(sc != skip_sc && CLOSURE_IS_BSDF(sc->type)) {
565                         float bsdf_pdf = 0.0f;
566                         float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
567
568                         if(bsdf_pdf != 0.0f) {
569                                 bsdf_eval_accum(result_eval, sc->type, eval*sc->weight, 1.0f);
570                                 sum_pdf += bsdf_pdf*sc->sample_weight;
571                         }
572
573                         sum_sample_weight += sc->sample_weight;
574                 }
575         }
576
577         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
578 }
579
580 #ifdef __BRANCHED_PATH__
581 ccl_device_inline void _shader_bsdf_multi_eval_branched(KernelGlobals *kg,
582                                                         ShaderData *sd,
583                                                         const float3 omega_in,
584                                                         BsdfEval *result_eval,
585                                                         float light_pdf,
586                                                         bool use_mis)
587 {
588         for(int i = 0; i < sd->num_closure; i++) {
589                 const ShaderClosure *sc = &sd->closure[i];
590                 if(CLOSURE_IS_BSDF(sc->type)) {
591                         float bsdf_pdf = 0.0f;
592                         float3 eval = bsdf_eval(kg, sd, sc, omega_in, &bsdf_pdf);
593                         if(bsdf_pdf != 0.0f) {
594                                 float mis_weight = use_mis? power_heuristic(light_pdf, bsdf_pdf): 1.0f;
595                                 bsdf_eval_accum(result_eval,
596                                                 sc->type,
597                                                 eval * sc->weight,
598                                                 mis_weight);
599                         }
600                 }
601         }
602 }
603 #endif  /* __BRANCHED_PATH__ */
604
605
606 #ifndef __KERNEL_CUDA__
607 ccl_device
608 #else
609 ccl_device_inline
610 #endif
611 void shader_bsdf_eval(KernelGlobals *kg,
612                       ShaderData *sd,
613                       const float3 omega_in,
614                       BsdfEval *eval,
615                       float light_pdf,
616                       bool use_mis)
617 {
618         PROFILING_INIT(kg, PROFILING_CLOSURE_EVAL);
619
620         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
621
622 #ifdef __BRANCHED_PATH__
623         if(kernel_data.integrator.branched)
624                 _shader_bsdf_multi_eval_branched(kg, sd, omega_in, eval, light_pdf, use_mis);
625         else
626 #endif
627         {
628                 float pdf;
629                 _shader_bsdf_multi_eval(kg, sd, omega_in, &pdf, NULL, eval, 0.0f, 0.0f);
630                 if(use_mis) {
631                         float weight = power_heuristic(light_pdf, pdf);
632                         bsdf_eval_mis(eval, weight);
633                 }
634         }
635 }
636
637 ccl_device_inline const ShaderClosure *shader_bsdf_pick(ShaderData *sd,
638                                                         float *randu)
639 {
640         /* Note the sampling here must match shader_bssrdf_pick,
641          * since we reuse the same random number. */
642         int sampled = 0;
643
644         if(sd->num_closure > 1) {
645                 /* Pick a BSDF or based on sample weights. */
646                 float sum = 0.0f;
647
648                 for(int i = 0; i < sd->num_closure; i++) {
649                         const ShaderClosure *sc = &sd->closure[i];
650
651                         if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
652                                 sum += sc->sample_weight;
653                         }
654                 }
655
656                 float r = (*randu)*sum;
657                 float partial_sum = 0.0f;
658
659                 for(int i = 0; i < sd->num_closure; i++) {
660                         const ShaderClosure *sc = &sd->closure[i];
661
662                         if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
663                                 float next_sum = partial_sum + sc->sample_weight;
664
665                                 if(r < next_sum) {
666                                         sampled = i;
667
668                                         /* Rescale to reuse for direction sample, to better
669                                          * preserve stratifaction. */
670                                         *randu = (r - partial_sum) / sc->sample_weight;
671                                         break;
672                                 }
673
674                                 partial_sum = next_sum;
675                         }
676                 }
677         }
678
679         const ShaderClosure *sc = &sd->closure[sampled];
680         return CLOSURE_IS_BSDF(sc->type)? sc: NULL;
681 }
682
683 ccl_device_inline const ShaderClosure *shader_bssrdf_pick(ShaderData *sd,
684                                                           ccl_addr_space float3 *throughput,
685                                                           float *randu)
686 {
687         /* Note the sampling here must match shader_bsdf_pick,
688          * since we reuse the same random number. */
689         int sampled = 0;
690
691         if(sd->num_closure > 1) {
692                 /* Pick a BSDF or BSSRDF or based on sample weights. */
693                 float sum_bsdf = 0.0f;
694                 float sum_bssrdf = 0.0f;
695
696                 for(int i = 0; i < sd->num_closure; i++) {
697                         const ShaderClosure *sc = &sd->closure[i];
698
699                         if(CLOSURE_IS_BSDF(sc->type)) {
700                                 sum_bsdf += sc->sample_weight;
701                         }
702                         else if(CLOSURE_IS_BSSRDF(sc->type)) {
703                                 sum_bssrdf += sc->sample_weight;
704                         }
705                 }
706
707                 float r = (*randu)*(sum_bsdf + sum_bssrdf);
708                 float partial_sum = 0.0f;
709
710                 for(int i = 0; i < sd->num_closure; i++) {
711                         const ShaderClosure *sc = &sd->closure[i];
712
713                         if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type)) {
714                                 float next_sum = partial_sum + sc->sample_weight;
715
716                                 if(r < next_sum) {
717                                         if(CLOSURE_IS_BSDF(sc->type)) {
718                                                 *throughput *= (sum_bsdf + sum_bssrdf) / sum_bsdf;
719                                                 return NULL;
720                                         }
721                                         else {
722                                                 *throughput *= (sum_bsdf + sum_bssrdf) / sum_bssrdf;
723                                                 sampled = i;
724
725                                                 /* Rescale to reuse for direction sample, to better
726                                                  * preserve stratifaction. */
727                                                 *randu = (r - partial_sum) / sc->sample_weight;
728                                                 break;
729                                         }
730                                 }
731
732                                 partial_sum = next_sum;
733                         }
734                 }
735         }
736
737         const ShaderClosure *sc = &sd->closure[sampled];
738         return CLOSURE_IS_BSSRDF(sc->type)? sc: NULL;
739 }
740
741 ccl_device_inline int shader_bsdf_sample(KernelGlobals *kg,
742                                          ShaderData *sd,
743                                          float randu, float randv,
744                                          BsdfEval *bsdf_eval,
745                                          float3 *omega_in,
746                                          differential3 *domega_in,
747                                          float *pdf)
748 {
749         PROFILING_INIT(kg, PROFILING_CLOSURE_SAMPLE);
750
751         const ShaderClosure *sc = shader_bsdf_pick(sd, &randu);
752         if(sc == NULL) {
753                 *pdf = 0.0f;
754                 return LABEL_NONE;
755         }
756
757         /* BSSRDF should already have been handled elsewhere. */
758         kernel_assert(CLOSURE_IS_BSDF(sc->type));
759
760         int label;
761         float3 eval;
762
763         *pdf = 0.0f;
764         label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
765
766         if(*pdf != 0.0f) {
767                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
768
769                 if(sd->num_closure > 1) {
770                         float sweight = sc->sample_weight;
771                         _shader_bsdf_multi_eval(kg, sd, *omega_in, pdf, sc, bsdf_eval, *pdf*sweight, sweight);
772                 }
773         }
774
775         return label;
776 }
777
778 ccl_device int shader_bsdf_sample_closure(KernelGlobals *kg, ShaderData *sd,
779         const ShaderClosure *sc, float randu, float randv, BsdfEval *bsdf_eval,
780         float3 *omega_in, differential3 *domega_in, float *pdf)
781 {
782         PROFILING_INIT(kg, PROFILING_CLOSURE_SAMPLE);
783
784         int label;
785         float3 eval;
786
787         *pdf = 0.0f;
788         label = bsdf_sample(kg, sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
789
790         if(*pdf != 0.0f)
791                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
792
793         return label;
794 }
795
796 ccl_device float shader_bsdf_average_roughness(ShaderData *sd)
797 {
798         float roughness = 0.0f;
799         float sum_weight = 0.0f;
800
801         for(int i = 0; i < sd->num_closure; i++) {
802                 ShaderClosure *sc = &sd->closure[i];
803
804                 if(CLOSURE_IS_BSDF(sc->type)) {
805                         /* sqrt once to undo the squaring from multiplying roughness on the
806                          * two axes, and once for the squared roughness convention. */
807                         float weight = fabsf(average(sc->weight));
808                         roughness += weight * sqrtf(safe_sqrtf(bsdf_get_roughness_squared(sc)));
809                         sum_weight += weight;
810                 }
811         }
812
813         return (sum_weight > 0.0f) ? roughness / sum_weight : 0.0f;
814 }
815
816 ccl_device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
817 {
818         for(int i = 0; i < sd->num_closure; i++) {
819                 ShaderClosure *sc = &sd->closure[i];
820
821                 if(CLOSURE_IS_BSDF(sc->type))
822                         bsdf_blur(kg, sc, roughness);
823         }
824 }
825
826 ccl_device float3 shader_bsdf_transparency(KernelGlobals *kg, const ShaderData *sd)
827 {
828         if(sd->flag & SD_HAS_ONLY_VOLUME) {
829                 return make_float3(1.0f, 1.0f, 1.0f);
830         }
831         else if(sd->flag & SD_TRANSPARENT) {
832                 return sd->closure_transparent_extinction;
833         }
834         else {
835                 return make_float3(0.0f, 0.0f, 0.0f);
836         }
837 }
838
839 ccl_device void shader_bsdf_disable_transparency(KernelGlobals *kg, ShaderData *sd)
840 {
841         if(sd->flag & SD_TRANSPARENT) {
842                 for(int i = 0; i < sd->num_closure; i++) {
843                         ShaderClosure *sc = &sd->closure[i];
844
845                         if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) {
846                                 sc->sample_weight = 0.0f;
847                                 sc->weight = make_float3(0.0f, 0.0f, 0.0f);
848                         }
849                 }
850
851                 sd->flag &= ~SD_TRANSPARENT;
852         }
853 }
854
855 ccl_device float3 shader_bsdf_alpha(KernelGlobals *kg, ShaderData *sd)
856 {
857         float3 alpha = make_float3(1.0f, 1.0f, 1.0f) - shader_bsdf_transparency(kg, sd);
858
859         alpha = max(alpha, make_float3(0.0f, 0.0f, 0.0f));
860         alpha = min(alpha, make_float3(1.0f, 1.0f, 1.0f));
861
862         return alpha;
863 }
864
865 ccl_device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
866 {
867         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
868
869         for(int i = 0; i < sd->num_closure; i++) {
870                 ShaderClosure *sc = &sd->closure[i];
871
872                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
873                         eval += sc->weight;
874         }
875
876         return eval;
877 }
878
879 ccl_device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
880 {
881         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
882
883         for(int i = 0; i < sd->num_closure; i++) {
884                 ShaderClosure *sc = &sd->closure[i];
885
886                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
887                         eval += sc->weight;
888         }
889
890         return eval;
891 }
892
893 ccl_device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
894 {
895         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
896
897         for(int i = 0; i < sd->num_closure; i++) {
898                 ShaderClosure *sc = &sd->closure[i];
899
900                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
901                         eval += sc->weight;
902         }
903
904         return eval;
905 }
906
907 ccl_device float3 shader_bsdf_subsurface(KernelGlobals *kg, ShaderData *sd)
908 {
909         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
910
911         for(int i = 0; i < sd->num_closure; i++) {
912                 ShaderClosure *sc = &sd->closure[i];
913
914                 if(CLOSURE_IS_BSSRDF(sc->type) || CLOSURE_IS_BSDF_BSSRDF(sc->type))
915                         eval += sc->weight;
916         }
917
918         return eval;
919 }
920
921 ccl_device float3 shader_bsdf_average_normal(KernelGlobals *kg, ShaderData *sd)
922 {
923         float3 N = make_float3(0.0f, 0.0f, 0.0f);
924
925         for(int i = 0; i < sd->num_closure; i++) {
926                 ShaderClosure *sc = &sd->closure[i];
927                 if(CLOSURE_IS_BSDF_OR_BSSRDF(sc->type))
928                         N += sc->N*fabsf(average(sc->weight));
929         }
930
931         return (is_zero(N))? sd->N : normalize(N);
932 }
933
934 ccl_device float3 shader_bsdf_ao(KernelGlobals *kg, ShaderData *sd, float ao_factor, float3 *N_)
935 {
936         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
937         float3 N = make_float3(0.0f, 0.0f, 0.0f);
938
939         for(int i = 0; i < sd->num_closure; i++) {
940                 ShaderClosure *sc = &sd->closure[i];
941
942                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
943                         const DiffuseBsdf *bsdf = (const DiffuseBsdf*)sc;
944                         eval += sc->weight*ao_factor;
945                         N += bsdf->N*fabsf(average(sc->weight));
946                 }
947         }
948
949         *N_ = (is_zero(N))? sd->N : normalize(N);
950         return eval;
951 }
952
953 #ifdef __SUBSURFACE__
954 ccl_device float3 shader_bssrdf_sum(ShaderData *sd, float3 *N_, float *texture_blur_)
955 {
956         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
957         float3 N = make_float3(0.0f, 0.0f, 0.0f);
958         float texture_blur = 0.0f, weight_sum = 0.0f;
959
960         for(int i = 0; i < sd->num_closure; i++) {
961                 ShaderClosure *sc = &sd->closure[i];
962
963                 if(CLOSURE_IS_BSSRDF(sc->type)) {
964                         const Bssrdf *bssrdf = (const Bssrdf*)sc;
965                         float avg_weight = fabsf(average(sc->weight));
966
967                         N += bssrdf->N*avg_weight;
968                         eval += sc->weight;
969                         texture_blur += bssrdf->texture_blur*avg_weight;
970                         weight_sum += avg_weight;
971                 }
972         }
973
974         if(N_)
975                 *N_ = (is_zero(N))? sd->N: normalize(N);
976
977         if(texture_blur_)
978                 *texture_blur_ = safe_divide(texture_blur, weight_sum);
979
980         return eval;
981 }
982 #endif  /* __SUBSURFACE__ */
983
984 /* Emission */
985
986 ccl_device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
987 {
988         if(sd->flag & SD_EMISSION) {
989                 return emissive_simple_eval(sd->Ng, sd->I) * sd->closure_emission_background;
990         }
991         else {
992                 return make_float3(0.0f, 0.0f, 0.0f);
993         }
994 }
995
996 /* Holdout */
997
998 ccl_device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
999 {
1000         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
1001
1002         for(int i = 0; i < sd->num_closure; i++) {
1003                 ShaderClosure *sc = &sd->closure[i];
1004
1005                 if(CLOSURE_IS_HOLDOUT(sc->type))
1006                         weight += sc->weight;
1007         }
1008
1009         return weight;
1010 }
1011
1012 /* Surface Evaluation */
1013
1014 ccl_device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
1015         ccl_addr_space PathState *state, int path_flag)
1016 {
1017         PROFILING_INIT(kg, PROFILING_SHADER_EVAL);
1018
1019         /* If path is being terminated, we are tracing a shadow ray or evaluating
1020          * emission, then we don't need to store closures. The emission and shadow
1021          * shader data also do not have a closure array to save GPU memory. */
1022         int max_closures;
1023         if(path_flag & (PATH_RAY_TERMINATE|PATH_RAY_SHADOW|PATH_RAY_EMISSION)) {
1024                 max_closures = 0;
1025         }
1026         else {
1027                 max_closures = kernel_data.integrator.max_closures;
1028         }
1029
1030         sd->num_closure = 0;
1031         sd->num_closure_left = max_closures;
1032
1033 #ifdef __OSL__
1034         if(kg->osl)
1035                 OSLShader::eval_surface(kg, sd, state, path_flag);
1036         else
1037 #endif
1038         {
1039 #ifdef __SVM__
1040                 svm_eval_nodes(kg, sd, state, SHADER_TYPE_SURFACE, path_flag);
1041 #else
1042                 DiffuseBsdf *bsdf = (DiffuseBsdf*)bsdf_alloc(sd,
1043                                                              sizeof(DiffuseBsdf),
1044                                                              make_float3(0.8f, 0.8f, 0.8f));
1045                 if(bsdf != NULL) {
1046                         bsdf->N = sd->N;
1047                         sd->flag |= bsdf_diffuse_setup(bsdf);
1048                 }
1049 #endif
1050         }
1051
1052         if(sd->flag & SD_BSDF_NEEDS_LCG) {
1053                 sd->lcg_state = lcg_state_init_addrspace(state, 0xb4bc3953);
1054         }
1055 }
1056
1057 /* Background Evaluation */
1058
1059 ccl_device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd,
1060         ccl_addr_space PathState *state, int path_flag)
1061 {
1062         sd->num_closure = 0;
1063         sd->num_closure_left = 0;
1064
1065 #ifdef __SVM__
1066 #  ifdef __OSL__
1067         if(kg->osl) {
1068                 OSLShader::eval_background(kg, sd, state, path_flag);
1069         }
1070         else
1071 #  endif  /* __OSL__ */
1072         {
1073                 svm_eval_nodes(kg, sd, state, SHADER_TYPE_SURFACE, path_flag);
1074         }
1075
1076         if(sd->flag & SD_EMISSION) {
1077                 return sd->closure_emission_background;
1078         }
1079         else {
1080                 return make_float3(0.0f, 0.0f, 0.0f);
1081         }
1082 #else  /* __SVM__ */
1083         return make_float3(0.8f, 0.8f, 0.8f);
1084 #endif  /* __SVM__ */
1085 }
1086
1087 /* Volume */
1088
1089 #ifdef __VOLUME__
1090
1091 ccl_device_inline void _shader_volume_phase_multi_eval(const ShaderData *sd, const float3 omega_in, float *pdf,
1092         int skip_phase, BsdfEval *result_eval, float sum_pdf, float sum_sample_weight)
1093 {
1094         for(int i = 0; i < sd->num_closure; i++) {
1095                 if(i == skip_phase)
1096                         continue;
1097
1098                 const ShaderClosure *sc = &sd->closure[i];
1099
1100                 if(CLOSURE_IS_PHASE(sc->type)) {
1101                         float phase_pdf = 0.0f;
1102                         float3 eval = volume_phase_eval(sd, sc, omega_in, &phase_pdf);
1103
1104                         if(phase_pdf != 0.0f) {
1105                                 bsdf_eval_accum(result_eval, sc->type, eval, 1.0f);
1106                                 sum_pdf += phase_pdf*sc->sample_weight;
1107                         }
1108
1109                         sum_sample_weight += sc->sample_weight;
1110                 }
1111         }
1112
1113         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
1114 }
1115
1116 ccl_device void shader_volume_phase_eval(KernelGlobals *kg, const ShaderData *sd,
1117         const float3 omega_in, BsdfEval *eval, float *pdf)
1118 {
1119         PROFILING_INIT(kg, PROFILING_CLOSURE_VOLUME_EVAL);
1120
1121         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
1122
1123         _shader_volume_phase_multi_eval(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
1124 }
1125
1126 ccl_device int shader_volume_phase_sample(KernelGlobals *kg, const ShaderData *sd,
1127         float randu, float randv, BsdfEval *phase_eval,
1128         float3 *omega_in, differential3 *domega_in, float *pdf)
1129 {
1130         PROFILING_INIT(kg, PROFILING_CLOSURE_VOLUME_SAMPLE);
1131
1132         int sampled = 0;
1133
1134         if(sd->num_closure > 1) {
1135                 /* pick a phase closure based on sample weights */
1136                 float sum = 0.0f;
1137
1138                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
1139                         const ShaderClosure *sc = &sd->closure[sampled];
1140
1141                         if(CLOSURE_IS_PHASE(sc->type))
1142                                 sum += sc->sample_weight;
1143                 }
1144
1145                 float r = randu*sum;
1146                 float partial_sum = 0.0f;
1147
1148                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
1149                         const ShaderClosure *sc = &sd->closure[sampled];
1150
1151                         if(CLOSURE_IS_PHASE(sc->type)) {
1152                                 float next_sum = partial_sum + sc->sample_weight;
1153
1154                                 if(r <= next_sum) {
1155                                         /* Rescale to reuse for BSDF direction sample. */
1156                                         randu = (r - partial_sum) / sc->sample_weight;
1157                                         break;
1158                                 }
1159
1160                                 partial_sum = next_sum;
1161                         }
1162                 }
1163
1164                 if(sampled == sd->num_closure) {
1165                         *pdf = 0.0f;
1166                         return LABEL_NONE;
1167                 }
1168         }
1169
1170         /* todo: this isn't quite correct, we don't weight anisotropy properly
1171          * depending on color channels, even if this is perhaps not a common case */
1172         const ShaderClosure *sc = &sd->closure[sampled];
1173         int label;
1174         float3 eval;
1175
1176         *pdf = 0.0f;
1177         label = volume_phase_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
1178
1179         if(*pdf != 0.0f) {
1180                 bsdf_eval_init(phase_eval, sc->type, eval, kernel_data.film.use_light_pass);
1181         }
1182
1183         return label;
1184 }
1185
1186 ccl_device int shader_phase_sample_closure(KernelGlobals *kg, const ShaderData *sd,
1187         const ShaderClosure *sc, float randu, float randv, BsdfEval *phase_eval,
1188         float3 *omega_in, differential3 *domega_in, float *pdf)
1189 {
1190         PROFILING_INIT(kg, PROFILING_CLOSURE_VOLUME_SAMPLE);
1191
1192         int label;
1193         float3 eval;
1194
1195         *pdf = 0.0f;
1196         label = volume_phase_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
1197
1198         if(*pdf != 0.0f)
1199                 bsdf_eval_init(phase_eval, sc->type, eval, kernel_data.film.use_light_pass);
1200
1201         return label;
1202 }
1203
1204 /* Volume Evaluation */
1205
1206 ccl_device_inline void shader_eval_volume(KernelGlobals *kg,
1207                                           ShaderData *sd,
1208                                           ccl_addr_space PathState *state,
1209                                           ccl_addr_space VolumeStack *stack,
1210                                           int path_flag)
1211 {
1212         /* If path is being terminated, we are tracing a shadow ray or evaluating
1213          * emission, then we don't need to store closures. The emission and shadow
1214          * shader data also do not have a closure array to save GPU memory. */
1215         int max_closures;
1216         if(path_flag & (PATH_RAY_TERMINATE|PATH_RAY_SHADOW|PATH_RAY_EMISSION)) {
1217                 max_closures = 0;
1218         }
1219         else {
1220                 max_closures = kernel_data.integrator.max_closures;
1221         }
1222
1223         /* reset closures once at the start, we will be accumulating the closures
1224          * for all volumes in the stack into a single array of closures */
1225         sd->num_closure = 0;
1226         sd->num_closure_left = max_closures;
1227         sd->flag = 0;
1228         sd->object_flag = 0;
1229
1230         for(int i = 0; stack[i].shader != SHADER_NONE; i++) {
1231                 /* setup shaderdata from stack. it's mostly setup already in
1232                  * shader_setup_from_volume, this switching should be quick */
1233                 sd->object = stack[i].object;
1234                 sd->lamp = LAMP_NONE;
1235                 sd->shader = stack[i].shader;
1236
1237                 sd->flag &= ~SD_SHADER_FLAGS;
1238                 sd->flag |= kernel_tex_fetch(__shaders, (sd->shader & SHADER_MASK)).flags;
1239                 sd->object_flag &= ~SD_OBJECT_FLAGS;
1240
1241                 if(sd->object != OBJECT_NONE) {
1242                         sd->object_flag |= kernel_tex_fetch(__object_flag, sd->object);
1243
1244 #ifdef __OBJECT_MOTION__
1245                         /* todo: this is inefficient for motion blur, we should be
1246                          * caching matrices instead of recomputing them each step */
1247                         shader_setup_object_transforms(kg, sd, sd->time);
1248 #endif
1249                 }
1250
1251                 /* evaluate shader */
1252 #ifdef __SVM__
1253 #  ifdef __OSL__
1254                 if(kg->osl) {
1255                         OSLShader::eval_volume(kg, sd, state, path_flag);
1256                 }
1257                 else
1258 #  endif
1259                 {
1260                         svm_eval_nodes(kg, sd, state, SHADER_TYPE_VOLUME, path_flag);
1261                 }
1262 #endif
1263
1264                 /* merge closures to avoid exceeding number of closures limit */
1265                 if(i > 0)
1266                         shader_merge_closures(sd);
1267         }
1268 }
1269
1270 #endif  /* __VOLUME__ */
1271
1272 /* Displacement Evaluation */
1273
1274 ccl_device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd, ccl_addr_space PathState *state)
1275 {
1276         sd->num_closure = 0;
1277         sd->num_closure_left = 0;
1278
1279         /* this will modify sd->P */
1280 #ifdef __SVM__
1281 #  ifdef __OSL__
1282         if(kg->osl)
1283                 OSLShader::eval_displacement(kg, sd, state);
1284         else
1285 #  endif
1286         {
1287                 svm_eval_nodes(kg, sd, state, SHADER_TYPE_DISPLACEMENT, 0);
1288         }
1289 #endif
1290 }
1291
1292 /* Transparent Shadows */
1293
1294 #ifdef __TRANSPARENT_SHADOWS__
1295 ccl_device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
1296 {
1297         int prim = kernel_tex_fetch(__prim_index, isect->prim);
1298         int shader = 0;
1299
1300 #ifdef __HAIR__
1301         if(kernel_tex_fetch(__prim_type, isect->prim) & PRIMITIVE_ALL_TRIANGLE) {
1302 #endif
1303                 shader = kernel_tex_fetch(__tri_shader, prim);
1304 #ifdef __HAIR__
1305         }
1306         else {
1307                 float4 str = kernel_tex_fetch(__curves, prim);
1308                 shader = __float_as_int(str.z);
1309         }
1310 #endif
1311         int flag = kernel_tex_fetch(__shaders, (shader & SHADER_MASK)).flags;
1312
1313         return (flag & SD_HAS_TRANSPARENT_SHADOW) != 0;
1314 }
1315 #endif  /* __TRANSPARENT_SHADOWS__ */
1316
1317 ccl_device float shader_cryptomatte_id(KernelGlobals *kg, int shader)
1318 {
1319         return kernel_tex_fetch(__shaders, (shader & SHADER_MASK)).cryptomatte_id;
1320 }
1321
1322 CCL_NAMESPACE_END