Fix #33405: preview render getting stuck in a particular .blend file, ObjectKey
[blender.git] / intern / cycles / kernel / kernel_shader.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 /*
20  * ShaderData, used in four steps:
21  *
22  * Setup from incoming ray, sampled position and background.
23  * Execute for surface, volume or displacement.
24  * Evaluate one or more closures.
25  * Release.
26  *
27  */
28
29 #include "closure/bsdf.h"
30 #include "closure/emissive.h"
31 #include "closure/volume.h"
32
33 #include "svm/svm_bsdf.h"
34 #include "svm/svm.h"
35
36 CCL_NAMESPACE_BEGIN
37
38 /* ShaderData setup from incoming ray */
39
40 #ifdef __OBJECT_MOTION__
41 __device_noinline void shader_setup_object_transforms(KernelGlobals *kg, ShaderData *sd, float time)
42 {
43         /* note that this is a separate non-inlined function to work around crash
44          * on CUDA sm 2.0, otherwise kernel execution crashes (compiler bug?) */
45         if(sd->flag & SD_OBJECT_MOTION) {
46                 sd->ob_tfm = object_fetch_transform_motion(kg, sd->object, time);
47                 sd->ob_itfm= transform_quick_inverse(sd->ob_tfm);
48         }
49         else {
50                 sd->ob_tfm = object_fetch_transform(kg, sd->object, OBJECT_TRANSFORM);
51                 sd->ob_itfm = object_fetch_transform(kg, sd->object, OBJECT_INVERSE_TRANSFORM);
52         }
53 }
54 #endif
55
56 __device_inline void shader_setup_from_ray(KernelGlobals *kg, ShaderData *sd,
57         const Intersection *isect, const Ray *ray)
58 {
59 #ifdef __OSL__
60         if (kg->osl)
61                 OSLShader::init(kg, sd);
62 #endif
63
64         /* fetch triangle data */
65         int prim = kernel_tex_fetch(__prim_index, isect->prim);
66         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
67         float3 Ng = make_float3(Ns.x, Ns.y, Ns.z);
68         int shader = __float_as_int(Ns.w);
69
70         /* triangle */
71 #ifdef __INSTANCING__
72         sd->object = (isect->object == ~0)? kernel_tex_fetch(__prim_object, isect->prim): isect->object;
73 #endif
74         sd->prim = prim;
75 #ifdef __UV__
76         sd->u = isect->u;
77         sd->v = isect->v;
78 #endif
79
80         sd->flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
81         sd->flag |= kernel_tex_fetch(__object_flag, sd->object);
82
83         /* matrices and time */
84 #ifdef __OBJECT_MOTION__
85         shader_setup_object_transforms(kg, sd, ray->time);
86         sd->time = ray->time;
87 #endif
88
89         /* vectors */
90         sd->P = bvh_triangle_refine(kg, sd, isect, ray);
91         sd->Ng = Ng;
92         sd->N = Ng;
93         sd->I = -ray->D;
94         sd->shader = shader;
95         sd->ray_length = isect->t;
96
97         /* smooth normal */
98         if(sd->shader & SHADER_SMOOTH_NORMAL)
99                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
100
101 #ifdef __DPDU__
102         /* dPdu/dPdv */
103         triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
104 #endif
105
106 #ifdef __INSTANCING__
107         if(isect->object != ~0) {
108                 /* instance transform */
109                 object_normal_transform(kg, sd, &sd->N);
110                 object_normal_transform(kg, sd, &sd->Ng);
111 #ifdef __DPDU__
112                 object_dir_transform(kg, sd, &sd->dPdu);
113                 object_dir_transform(kg, sd, &sd->dPdv);
114 #endif
115         }
116 #endif
117
118         /* backfacing test */
119         bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
120
121         if(backfacing) {
122                 sd->flag |= SD_BACKFACING;
123                 sd->Ng = -sd->Ng;
124                 sd->N = -sd->N;
125 #ifdef __DPDU__
126                 sd->dPdu = -sd->dPdu;
127                 sd->dPdv = -sd->dPdv;
128 #endif
129         }
130
131 #ifdef __RAY_DIFFERENTIALS__
132         /* differentials */
133         differential_transfer(&sd->dP, ray->dP, ray->D, ray->dD, sd->Ng, isect->t);
134         differential_incoming(&sd->dI, ray->dD);
135         differential_dudv(&sd->du, &sd->dv, sd->dPdu, sd->dPdv, sd->dP, sd->Ng);
136 #endif
137 }
138
139 /* ShaderData setup from position sampled on mesh */
140
141 __device void shader_setup_from_sample(KernelGlobals *kg, ShaderData *sd,
142         const float3 P, const float3 Ng, const float3 I,
143         int shader, int object, int prim, float u, float v, float t, float time)
144 {
145 #ifdef __OSL__
146         if (kg->osl)
147                 OSLShader::init(kg, sd);
148 #endif
149
150         /* vectors */
151         sd->P = P;
152         sd->N = Ng;
153         sd->Ng = Ng;
154         sd->I = I;
155         sd->shader = shader;
156
157         /* primitive */
158 #ifdef __INSTANCING__
159         sd->object = object;
160 #endif
161         sd->prim = prim;
162 #ifdef __UV__
163         sd->u = u;
164         sd->v = v;
165 #endif
166         sd->ray_length = t;
167
168         /* detect instancing, for non-instanced the object index is -object-1 */
169 #ifdef __INSTANCING__
170         bool instanced = false;
171
172         if(sd->prim != ~0) {
173                 if(sd->object >= 0)
174                         instanced = true;
175                 else
176 #endif
177                         sd->object = ~sd->object;
178 #ifdef __INSTANCING__
179         }
180 #endif
181
182         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
183         if(sd->object != -1) {
184                 sd->flag |= kernel_tex_fetch(__object_flag, sd->object);
185
186 #ifdef __OBJECT_MOTION__
187                 shader_setup_object_transforms(kg, sd, time);
188         }
189
190         sd->time = time;
191 #else
192         }
193 #endif
194
195         /* smooth normal */
196         if(sd->shader & SHADER_SMOOTH_NORMAL) {
197                 sd->N = triangle_smooth_normal(kg, sd->prim, sd->u, sd->v);
198
199 #ifdef __INSTANCING__
200                 if(instanced)
201                         object_normal_transform(kg, sd, &sd->N);
202 #endif
203         }
204
205 #ifdef __DPDU__
206         /* dPdu/dPdv */
207         if(sd->prim == ~0) {
208                 sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
209                 sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
210         }
211         else {
212                 triangle_dPdudv(kg, &sd->dPdu, &sd->dPdv, sd->prim);
213
214 #ifdef __INSTANCING__
215                 if(instanced) {
216                         object_dir_transform(kg, sd, &sd->dPdu);
217                         object_dir_transform(kg, sd, &sd->dPdv);
218                 }
219 #endif
220         }
221 #endif
222
223         /* backfacing test */
224         if(sd->prim != ~0) {
225                 bool backfacing = (dot(sd->Ng, sd->I) < 0.0f);
226
227                 if(backfacing) {
228                         sd->flag |= SD_BACKFACING;
229                         sd->Ng = -sd->Ng;
230                         sd->N = -sd->N;
231 #ifdef __DPDU__
232                         sd->dPdu = -sd->dPdu;
233                         sd->dPdv = -sd->dPdv;
234 #endif
235                 }
236         }
237
238 #ifdef __RAY_DIFFERENTIALS__
239         /* no ray differentials here yet */
240         sd->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
241         sd->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
242         sd->dI.dx = make_float3(0.0f, 0.0f, 0.0f);
243         sd->dI.dy = make_float3(0.0f, 0.0f, 0.0f);
244         sd->du.dx = 0.0f;
245         sd->du.dy = 0.0f;
246         sd->dv.dx = 0.0f;
247         sd->dv.dy = 0.0f;
248 #endif
249 }
250
251 /* ShaderData setup for displacement */
252
253 __device void shader_setup_from_displace(KernelGlobals *kg, ShaderData *sd,
254         int object, int prim, float u, float v)
255 {
256         /* Note: no OSLShader::init call here, this is done in shader_setup_from_sample! */
257
258         float3 P, Ng, I = make_float3(0.0f, 0.0f, 0.0f);
259         int shader;
260
261         P = triangle_point_MT(kg, prim, u, v);
262         Ng = triangle_normal_MT(kg, prim, &shader);
263
264         /* force smooth shading for displacement */
265         shader |= SHADER_SMOOTH_NORMAL;
266
267         /* watch out: no instance transform currently */
268
269         shader_setup_from_sample(kg, sd, P, Ng, I, shader, object, prim, u, v, 0.0f, TIME_INVALID);
270 }
271
272 /* ShaderData setup from ray into background */
273
274 __device_inline void shader_setup_from_background(KernelGlobals *kg, ShaderData *sd, const Ray *ray)
275 {
276 #ifdef __OSL__
277         if (kg->osl)
278                 OSLShader::init(kg, sd);
279 #endif
280
281         /* vectors */
282         sd->P = ray->D;
283         sd->N = -sd->P;
284         sd->Ng = -sd->P;
285         sd->I = -sd->P;
286         sd->shader = kernel_data.background.shader;
287         sd->flag = kernel_tex_fetch(__shader_flag, (sd->shader & SHADER_MASK)*2);
288 #ifdef __OBJECT_MOTION__
289         sd->time = ray->time;
290 #endif
291         sd->ray_length = 0.0f;
292
293 #ifdef __INSTANCING__
294         sd->object = ~0;
295 #endif
296         sd->prim = ~0;
297 #ifdef __UV__
298         sd->u = 0.0f;
299         sd->v = 0.0f;
300 #endif
301
302 #ifdef __DPDU__
303         /* dPdu/dPdv */
304         sd->dPdu = make_float3(0.0f, 0.0f, 0.0f);
305         sd->dPdv = make_float3(0.0f, 0.0f, 0.0f);
306 #endif
307
308 #ifdef __RAY_DIFFERENTIALS__
309         /* differentials */
310         sd->dP = ray->dD;
311         differential_incoming(&sd->dI, sd->dP);
312         sd->du.dx = 0.0f;
313         sd->du.dy = 0.0f;
314         sd->dv.dx = 0.0f;
315         sd->dv.dy = 0.0f;
316 #endif
317 }
318
319 /* BSDF */
320
321 #ifdef __MULTI_CLOSURE__
322
323 #ifdef __OSL__
324 __device_inline void _shader_bsdf_multi_eval_osl(const ShaderData *sd, const float3 omega_in, float *pdf,
325         int skip_bsdf, BsdfEval *bsdf_eval, float sum_pdf, float sum_sample_weight)
326 {
327         for(int i = 0; i< sd->num_closure; i++) {
328                 if(i == skip_bsdf)
329                         continue;
330
331                 const ShaderClosure *sc = &sd->closure[i];
332
333                 if(CLOSURE_IS_BSDF(sc->type)) {
334                         float bsdf_pdf = 0.0f;
335
336                         float3 eval = OSLShader::bsdf_eval(sd, sc, omega_in, bsdf_pdf);
337
338                         if(bsdf_pdf != 0.0f) {
339                                 bsdf_eval_accum(bsdf_eval, sc->type, eval*sc->weight);
340                                 sum_pdf += bsdf_pdf*sc->sample_weight;
341                         }
342
343                         sum_sample_weight += sc->sample_weight;
344                 }
345         }
346
347         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
348 }
349 #endif
350
351 __device_inline void _shader_bsdf_multi_eval_svm(const ShaderData *sd, const float3 omega_in, float *pdf,
352         int skip_bsdf, BsdfEval *bsdf_eval, float sum_pdf, float sum_sample_weight)
353 {
354         for(int i = 0; i< sd->num_closure; i++) {
355                 if(i == skip_bsdf)
356                         continue;
357
358                 const ShaderClosure *sc = &sd->closure[i];
359
360                 if(CLOSURE_IS_BSDF(sc->type)) {
361                         float bsdf_pdf = 0.0f;
362
363                         float3 eval = svm_bsdf_eval(sd, sc, omega_in, &bsdf_pdf);
364
365                         if(bsdf_pdf != 0.0f) {
366                                 bsdf_eval_accum(bsdf_eval, sc->type, eval*sc->weight);
367                                 sum_pdf += bsdf_pdf*sc->sample_weight;
368                         }
369
370                         sum_sample_weight += sc->sample_weight;
371                 }
372         }
373
374         *pdf = (sum_sample_weight > 0.0f)? sum_pdf/sum_sample_weight: 0.0f;
375 }
376
377 #endif
378
379 __device void shader_bsdf_eval(KernelGlobals *kg, const ShaderData *sd,
380         const float3 omega_in, BsdfEval *eval, float *pdf)
381 {
382 #ifdef __MULTI_CLOSURE__
383         bsdf_eval_init(eval, NBUILTIN_CLOSURES, make_float3(0.0f, 0.0f, 0.0f), kernel_data.film.use_light_pass);
384
385 #ifdef __OSL__
386         if (kg->osl)
387                 return _shader_bsdf_multi_eval_osl(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
388         else
389 #endif
390                 return _shader_bsdf_multi_eval_svm(sd, omega_in, pdf, -1, eval, 0.0f, 0.0f);
391 #else
392         const ShaderClosure *sc = &sd->closure;
393
394         *pdf = 0.0f;
395         *eval = svm_bsdf_eval(sd, sc, omega_in, pdf)*sc->weight;
396 #endif
397 }
398
399 __device int shader_bsdf_sample(KernelGlobals *kg, const ShaderData *sd,
400         float randu, float randv, BsdfEval *bsdf_eval,
401         float3 *omega_in, differential3 *domega_in, float *pdf)
402 {
403 #ifdef __MULTI_CLOSURE__
404         int sampled = 0;
405
406         if(sd->num_closure > 1) {
407                 /* pick a BSDF closure based on sample weights */
408                 float sum = 0.0f;
409
410                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
411                         const ShaderClosure *sc = &sd->closure[sampled];
412                         
413                         if(CLOSURE_IS_BSDF(sc->type))
414                                 sum += sc->sample_weight;
415                 }
416
417                 float r = sd->randb_closure*sum;
418                 sum = 0.0f;
419
420                 for(sampled = 0; sampled < sd->num_closure; sampled++) {
421                         const ShaderClosure *sc = &sd->closure[sampled];
422                         
423                         if(CLOSURE_IS_BSDF(sc->type)) {
424                                 sum += sd->closure[sampled].sample_weight;
425
426                                 if(r <= sum)
427                                         break;
428                         }
429                 }
430
431                 if(sampled == sd->num_closure) {
432                         *pdf = 0.0f;
433                         return LABEL_NONE;
434                 }
435         }
436
437         const ShaderClosure *sc = &sd->closure[sampled];
438         int label;
439         float3 eval;
440
441         *pdf = 0.0f;
442 #ifdef __OSL__
443         if (kg->osl)
444                 label = OSLShader::bsdf_sample(sd, sc, randu, randv, eval, *omega_in, *domega_in, *pdf);
445         else
446 #endif
447                 label = svm_bsdf_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
448
449         if(*pdf != 0.0f) {
450                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
451
452                 if(sd->num_closure > 1) {
453                         float sweight = sc->sample_weight;
454 #ifdef __OSL__
455                         if (kg->osl)
456                                 _shader_bsdf_multi_eval_osl(sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
457                         else
458 #endif
459                                 _shader_bsdf_multi_eval_svm(sd, *omega_in, pdf, sampled, bsdf_eval, *pdf*sweight, sweight);
460                 }
461         }
462
463         return label;
464 #else
465         /* sample the single closure that we picked */
466         *pdf = 0.0f;
467         int label = svm_bsdf_sample(sd, &sd->closure, randu, randv, bsdf_eval, omega_in, domega_in, pdf);
468         *bsdf_eval *= sd->closure.weight;
469         return label;
470 #endif
471 }
472
473 __device int shader_bsdf_sample_closure(KernelGlobals *kg, const ShaderData *sd,
474         const ShaderClosure *sc, float randu, float randv, BsdfEval *bsdf_eval,
475         float3 *omega_in, differential3 *domega_in, float *pdf)
476 {
477         int label;
478         float3 eval;
479
480         *pdf = 0.0f;
481 #ifdef __OSL__
482         if (kg->osl)
483                 label = OSLShader::bsdf_sample(sd, sc, randu, randv, eval, *omega_in, *domega_in, *pdf);
484         else
485 #endif
486                 label = svm_bsdf_sample(sd, sc, randu, randv, &eval, omega_in, domega_in, pdf);
487
488         if(*pdf != 0.0f)
489                 bsdf_eval_init(bsdf_eval, sc->type, eval*sc->weight, kernel_data.film.use_light_pass);
490
491         return label;
492 }
493
494 __device void shader_bsdf_blur(KernelGlobals *kg, ShaderData *sd, float roughness)
495 {
496 #ifdef __MULTI_CLOSURE__
497         for(int i = 0; i< sd->num_closure; i++) {
498                 ShaderClosure *sc = &sd->closure[i];
499
500                 if(CLOSURE_IS_BSDF(sc->type)) {
501 #ifdef __OSL__
502                         if (kg->osl)
503                                 OSLShader::bsdf_blur(sc, roughness);
504                         else
505 #endif
506                                 svm_bsdf_blur(sc, roughness);
507                 }
508         }
509 #else
510         svm_bsdf_blur(&sd->closure, roughness);
511 #endif
512 }
513
514 __device float3 shader_bsdf_transparency(KernelGlobals *kg, ShaderData *sd)
515 {
516 #ifdef __MULTI_CLOSURE__
517         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
518
519         for(int i = 0; i< sd->num_closure; i++) {
520                 ShaderClosure *sc = &sd->closure[i];
521
522                 if(sc->type == CLOSURE_BSDF_TRANSPARENT_ID) // todo: make this work for osl
523                         eval += sc->weight;
524         }
525
526         return eval;
527 #else
528         if(sd->closure.type == CLOSURE_BSDF_TRANSPARENT_ID)
529                 return sd->closure.weight;
530         else
531                 return make_float3(0.0f, 0.0f, 0.0f);
532 #endif
533 }
534
535 __device float3 shader_bsdf_diffuse(KernelGlobals *kg, ShaderData *sd)
536 {
537 #ifdef __MULTI_CLOSURE__
538         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
539
540         for(int i = 0; i< sd->num_closure; i++) {
541                 ShaderClosure *sc = &sd->closure[i];
542
543                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type))
544                         eval += sc->weight;
545         }
546
547         return eval;
548 #else
549         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
550                 return sd->closure.weight;
551         else
552                 return make_float3(0.0f, 0.0f, 0.0f);
553 #endif
554 }
555
556 __device float3 shader_bsdf_glossy(KernelGlobals *kg, ShaderData *sd)
557 {
558 #ifdef __MULTI_CLOSURE__
559         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
560
561         for(int i = 0; i< sd->num_closure; i++) {
562                 ShaderClosure *sc = &sd->closure[i];
563
564                 if(CLOSURE_IS_BSDF_GLOSSY(sc->type))
565                         eval += sc->weight;
566         }
567
568         return eval;
569 #else
570         if(CLOSURE_IS_BSDF_GLOSSY(sd->closure.type))
571                 return sd->closure.weight;
572         else
573                 return make_float3(0.0f, 0.0f, 0.0f);
574 #endif
575 }
576
577 __device float3 shader_bsdf_transmission(KernelGlobals *kg, ShaderData *sd)
578 {
579 #ifdef __MULTI_CLOSURE__
580         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
581
582         for(int i = 0; i< sd->num_closure; i++) {
583                 ShaderClosure *sc = &sd->closure[i];
584
585                 if(CLOSURE_IS_BSDF_TRANSMISSION(sc->type))
586                         eval += sc->weight;
587         }
588
589         return eval;
590 #else
591         if(CLOSURE_IS_BSDF_TRANSMISSION(sd->closure.type))
592                 return sd->closure.weight;
593         else
594                 return make_float3(0.0f, 0.0f, 0.0f);
595 #endif
596 }
597
598 __device float3 shader_bsdf_ao(KernelGlobals *kg, ShaderData *sd, float ao_factor, float3 *N)
599 {
600 #ifdef __MULTI_CLOSURE__
601         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
602
603         *N = make_float3(0.0f, 0.0f, 0.0f);
604
605         for(int i = 0; i< sd->num_closure; i++) {
606                 ShaderClosure *sc = &sd->closure[i];
607
608                 if(CLOSURE_IS_BSDF_DIFFUSE(sc->type)) {
609                         eval += sc->weight*ao_factor;
610                         *N += sc->N*average(sc->weight);
611                 }
612                 if(CLOSURE_IS_AMBIENT_OCCLUSION(sc->type)) {
613                         eval += sc->weight;
614                         *N += sd->N*average(sc->weight);
615                 }
616         }
617
618         if(is_zero(*N))
619                 *N = sd->N;
620         else
621                 *N = normalize(*N);
622
623         return eval;
624 #else
625         *N = sd->N;
626
627         if(CLOSURE_IS_BSDF_DIFFUSE(sd->closure.type))
628                 return sd->closure.weight*ao_factor;
629         else if(CLOSURE_IS_AMBIENT_OCCLUSION(sd->closure.type))
630                 return sd->closure.weight;
631         else
632                 return make_float3(0.0f, 0.0f, 0.0f);
633 #endif
634 }
635
636 /* Emission */
637
638 __device float3 shader_emissive_eval(KernelGlobals *kg, ShaderData *sd)
639 {
640         float3 eval;
641 #ifdef __MULTI_CLOSURE__
642         eval = make_float3(0.0f, 0.0f, 0.0f);
643
644         for(int i = 0; i < sd->num_closure; i++) {
645                 ShaderClosure *sc = &sd->closure[i];
646
647                 if(CLOSURE_IS_EMISSION(sc->type)) {
648 #ifdef __OSL__
649                         if (kg->osl)
650                                 eval += OSLShader::emissive_eval(sd, sc)*sc->weight;
651                         else
652 #endif
653                                 eval += svm_emissive_eval(sd, sc)*sc->weight;
654
655                 }
656         }
657 #else
658         eval = svm_emissive_eval(sd, &sd->closure)*sd->closure.weight;
659 #endif
660
661         return eval;
662 }
663
664 /* Holdout */
665
666 __device float3 shader_holdout_eval(KernelGlobals *kg, ShaderData *sd)
667 {
668 #ifdef __MULTI_CLOSURE__
669         float3 weight = make_float3(0.0f, 0.0f, 0.0f);
670
671         for(int i = 0; i < sd->num_closure; i++) {
672                 ShaderClosure *sc = &sd->closure[i];
673
674                 if(CLOSURE_IS_HOLDOUT(sc->type))
675                         weight += sc->weight;
676         }
677
678         return weight;
679 #else
680         if(sd->closure.type == CLOSURE_HOLDOUT_ID)
681                 return make_float3(1.0f, 1.0f, 1.0f);
682
683         return make_float3(0.0f, 0.0f, 0.0f);
684 #endif
685 }
686
687 /* Surface Evaluation */
688
689 __device void shader_eval_surface(KernelGlobals *kg, ShaderData *sd,
690         float randb, int path_flag)
691 {
692 #ifdef __OSL__
693         if (kg->osl)
694                 OSLShader::eval_surface(kg, sd, randb, path_flag);
695         else
696 #endif
697         {
698 #ifdef __SVM__
699                 svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, randb, path_flag);
700 #else
701                 bsdf_diffuse_setup(&sd->closure);
702                 sd->closure.weight = make_float3(0.8f, 0.8f, 0.8f);
703 #endif
704         }
705 }
706
707 /* Background Evaluation */
708
709 __device float3 shader_eval_background(KernelGlobals *kg, ShaderData *sd, int path_flag)
710 {
711 #ifdef __OSL__
712         if (kg->osl)
713                 return OSLShader::eval_background(kg, sd, path_flag);
714         else
715 #endif
716
717         {
718 #ifdef __SVM__
719                 svm_eval_nodes(kg, sd, SHADER_TYPE_SURFACE, 0.0f, path_flag);
720
721 #ifdef __MULTI_CLOSURE__
722                 float3 eval = make_float3(0.0f, 0.0f, 0.0f);
723
724                 for(int i = 0; i< sd->num_closure; i++) {
725                         const ShaderClosure *sc = &sd->closure[i];
726
727                         if(CLOSURE_IS_BACKGROUND(sc->type))
728                                 eval += sc->weight;
729                 }
730
731                 return eval;
732 #else
733                 if(sd->closure.type == CLOSURE_BACKGROUND_ID)
734                         return sd->closure.weight;
735                 else
736                         return make_float3(0.0f, 0.0f, 0.0f);
737 #endif
738
739 #else
740                 return make_float3(0.8f, 0.8f, 0.8f);
741 #endif
742         }
743 }
744
745 /* Volume */
746
747 __device float3 shader_volume_eval_phase(KernelGlobals *kg, ShaderData *sd,
748         float3 omega_in, float3 omega_out)
749 {
750 #ifdef __MULTI_CLOSURE__
751         float3 eval = make_float3(0.0f, 0.0f, 0.0f);
752
753         for(int i = 0; i< sd->num_closure; i++) {
754                 const ShaderClosure *sc = &sd->closure[i];
755
756                 if(CLOSURE_IS_VOLUME(sc->type)) {
757 #ifdef __OSL__
758                         if (kg->osl)
759                                 eval += OSLShader::volume_eval_phase(sc, omega_in, omega_out);
760                         else
761 #endif
762                                 eval += volume_eval_phase(sc, omega_in, omega_out);
763                 }
764         }
765
766         return eval;
767 #else
768         return volume_eval_phase(&sd->closure, omega_in, omega_out);
769 #endif
770 }
771
772 /* Volume Evaluation */
773
774 __device void shader_eval_volume(KernelGlobals *kg, ShaderData *sd,
775         float randb, int path_flag)
776 {
777 #ifdef __SVM__
778 #ifdef __OSL__
779         if (kg->osl)
780                 OSLShader::eval_volume(kg, sd, randb, path_flag);
781         else
782 #endif
783                 svm_eval_nodes(kg, sd, SHADER_TYPE_VOLUME, randb, path_flag);
784 #endif
785 }
786
787 /* Displacement Evaluation */
788
789 __device void shader_eval_displacement(KernelGlobals *kg, ShaderData *sd)
790 {
791         /* this will modify sd->P */
792 #ifdef __SVM__
793 #ifdef __OSL__
794         if (kg->osl)
795                 OSLShader::eval_displacement(kg, sd);
796         else
797 #endif
798                 svm_eval_nodes(kg, sd, SHADER_TYPE_DISPLACEMENT, 0.0f, 0);
799 #endif
800 }
801
802 /* Transparent Shadows */
803
804 #ifdef __TRANSPARENT_SHADOWS__
805 __device bool shader_transparent_shadow(KernelGlobals *kg, Intersection *isect)
806 {
807         int prim = kernel_tex_fetch(__prim_index, isect->prim);
808         float4 Ns = kernel_tex_fetch(__tri_normal, prim);
809         int shader = __float_as_int(Ns.w);
810         int flag = kernel_tex_fetch(__shader_flag, (shader & SHADER_MASK)*2);
811
812         return (flag & SD_HAS_SURFACE_TRANSPARENT) != 0;
813 }
814 #endif
815
816 /* Merging */
817
818 #ifdef __NON_PROGRESSIVE__
819 __device void shader_merge_closures(KernelGlobals *kg, ShaderData *sd)
820 {
821 #ifndef __OSL__
822         /* merge identical closures, better when we sample a single closure at a time */
823         for(int i = 0; i < sd->num_closure; i++) {
824                 ShaderClosure *sci = &sd->closure[i];
825
826                 for(int j = i + 1; j < sd->num_closure; j++) {
827                         ShaderClosure *scj = &sd->closure[j];
828
829                         if(sci->type == scj->type && sci->data0 == scj->data0 && sci->data1 == scj->data1) {
830                                 sci->weight += scj->weight;
831                                 sci->sample_weight += scj->sample_weight;
832
833                                 int size = sd->num_closure - (j+1);
834                                 if(size > 0)
835                                         memmove(scj, scj+1, size*sizeof(ShaderClosure));
836
837                                 sd->num_closure--;
838                         }
839                 }
840         }
841 #endif
842 }
843 #endif
844
845 /* Free ShaderData */
846
847 __device void shader_release(KernelGlobals *kg, ShaderData *sd)
848 {
849 #ifdef __OSL__
850         if (kg->osl)
851                 OSLShader::release(kg, sd);
852 #endif
853 }
854
855 CCL_NAMESPACE_END
856