5b66793a05da22f79ce2263f468c972cedd4340c
[blender.git] / intern / cycles / kernel / osl / osl_closures.cpp
1 /*
2  * Adapted from Open Shading Language with this license:
3  *
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011, Blender Foundation.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
12  * * Redistributions of source code must retain the above copyright
13  *   notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  *   notice, this list of conditions and the following disclaimer in the
16  *   documentation and/or other materials provided with the distribution.
17  * * Neither the name of Sony Pictures Imageworks nor the names of its
18  *   contributors may be used to endorse or promote products derived from
19  *   this software without specific prior written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <OSL/genclosure.h>
34 #include <OSL/oslclosure.h>
35
36 #include "kernel/osl/osl_closures.h"
37 #include "kernel/osl/osl_shader.h"
38
39 #include "util/util_debug.h"
40 #include "util/util_math.h"
41 #include "util/util_param.h"
42
43 #include "kernel/kernel_types.h"
44 #include "kernel/kernel_compat_cpu.h"
45 #include "kernel/split/kernel_split_data_types.h"
46 #include "kernel/kernel_globals.h"
47 #include "kernel/kernel_montecarlo.h"
48 #include "kernel/kernel_random.h"
49
50 #include "kernel/closure/alloc.h"
51 #include "kernel/closure/bsdf_util.h"
52 #include "kernel/closure/bsdf_ashikhmin_velvet.h"
53 #include "kernel/closure/bsdf_diffuse.h"
54 #include "kernel/closure/bsdf_microfacet.h"
55 #include "kernel/closure/bsdf_microfacet_multi.h"
56 #include "kernel/closure/bsdf_oren_nayar.h"
57 #include "kernel/closure/bsdf_reflection.h"
58 #include "kernel/closure/bsdf_refraction.h"
59 #include "kernel/closure/bsdf_transparent.h"
60 #include "kernel/closure/bsdf_ashikhmin_shirley.h"
61 #include "kernel/closure/bsdf_toon.h"
62 #include "kernel/closure/bsdf_hair.h"
63 #include "kernel/closure/bsdf_principled_diffuse.h"
64 #include "kernel/closure/bsdf_principled_sheen.h"
65 #include "kernel/closure/volume.h"
66
67 CCL_NAMESPACE_BEGIN
68
69 using namespace OSL;
70
71 /* BSDF class definitions */
72
73 BSDF_CLOSURE_CLASS_BEGIN(Diffuse, diffuse, DiffuseBsdf, LABEL_DIFFUSE)
74         CLOSURE_FLOAT3_PARAM(DiffuseClosure, params.N),
75 BSDF_CLOSURE_CLASS_END(Diffuse, diffuse)
76
77 BSDF_CLOSURE_CLASS_BEGIN(Translucent, translucent, DiffuseBsdf, LABEL_DIFFUSE)
78         CLOSURE_FLOAT3_PARAM(TranslucentClosure, params.N),
79 BSDF_CLOSURE_CLASS_END(Translucent, translucent)
80
81 BSDF_CLOSURE_CLASS_BEGIN(OrenNayar, oren_nayar, OrenNayarBsdf, LABEL_DIFFUSE)
82         CLOSURE_FLOAT3_PARAM(OrenNayarClosure, params.N),
83         CLOSURE_FLOAT_PARAM(OrenNayarClosure, params.roughness),
84 BSDF_CLOSURE_CLASS_END(OrenNayar, oren_nayar)
85
86 BSDF_CLOSURE_CLASS_BEGIN(Reflection, reflection, MicrofacetBsdf, LABEL_SINGULAR)
87         CLOSURE_FLOAT3_PARAM(ReflectionClosure, params.N),
88 BSDF_CLOSURE_CLASS_END(Reflection, reflection)
89
90 BSDF_CLOSURE_CLASS_BEGIN(Refraction, refraction, MicrofacetBsdf, LABEL_SINGULAR)
91         CLOSURE_FLOAT3_PARAM(RefractionClosure, params.N),
92         CLOSURE_FLOAT_PARAM(RefractionClosure, params.ior),
93 BSDF_CLOSURE_CLASS_END(Refraction, refraction)
94
95 BSDF_CLOSURE_CLASS_BEGIN(Transparent, transparent, ShaderClosure, LABEL_SINGULAR)
96 BSDF_CLOSURE_CLASS_END(Transparent, transparent)
97
98 BSDF_CLOSURE_CLASS_BEGIN(AshikhminVelvet, ashikhmin_velvet, VelvetBsdf, LABEL_DIFFUSE)
99         CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, params.N),
100         CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, params.sigma),
101 BSDF_CLOSURE_CLASS_END(AshikhminVelvet, ashikhmin_velvet)
102
103 BSDF_CLOSURE_CLASS_BEGIN(AshikhminShirley, ashikhmin_shirley_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
104         CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, params.N),
105         CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, params.T),
106         CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, params.alpha_x),
107         CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, params.alpha_y),
108 BSDF_CLOSURE_CLASS_END(AshikhminShirley, ashikhmin_shirley_aniso)
109
110 BSDF_CLOSURE_CLASS_BEGIN(DiffuseToon, diffuse_toon, ToonBsdf, LABEL_DIFFUSE)
111         CLOSURE_FLOAT3_PARAM(DiffuseToonClosure, params.N),
112         CLOSURE_FLOAT_PARAM(DiffuseToonClosure, params.size),
113         CLOSURE_FLOAT_PARAM(DiffuseToonClosure, params.smooth),
114 BSDF_CLOSURE_CLASS_END(DiffuseToon, diffuse_toon)
115
116 BSDF_CLOSURE_CLASS_BEGIN(GlossyToon, glossy_toon, ToonBsdf, LABEL_GLOSSY)
117         CLOSURE_FLOAT3_PARAM(GlossyToonClosure, params.N),
118         CLOSURE_FLOAT_PARAM(GlossyToonClosure, params.size),
119         CLOSURE_FLOAT_PARAM(GlossyToonClosure, params.smooth),
120 BSDF_CLOSURE_CLASS_END(GlossyToon, glossy_toon)
121
122 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
123         CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, params.N),
124         CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, params.alpha_x),
125 BSDF_CLOSURE_CLASS_END(MicrofacetGGX, microfacet_ggx)
126
127 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXAniso, microfacet_ggx_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
128         CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, params.N),
129         CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, params.T),
130         CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, params.alpha_x),
131         CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, params.alpha_y),
132 BSDF_CLOSURE_CLASS_END(MicrofacetGGXAniso, microfacet_ggx_aniso)
133
134 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
135         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, params.N),
136         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, params.alpha_x),
137 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmann, microfacet_beckmann)
138
139 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannAniso, microfacet_beckmann_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
140         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, params.N),
141         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, params.T),
142         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, params.alpha_x),
143         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, params.alpha_y),
144 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannAniso, microfacet_beckmann_aniso)
145
146 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, MicrofacetBsdf, LABEL_GLOSSY|LABEL_TRANSMIT)
147         CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, params.N),
148         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, params.alpha_x),
149         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, params.ior),
150 BSDF_CLOSURE_CLASS_END(MicrofacetGGXRefraction, microfacet_ggx_refraction)
151
152 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, MicrofacetBsdf, LABEL_GLOSSY|LABEL_TRANSMIT)
153         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, params.N),
154         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, params.alpha_x),
155         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, params.ior),
156 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction)
157
158 BSDF_CLOSURE_CLASS_BEGIN(HairReflection, hair_reflection, HairBsdf, LABEL_GLOSSY)
159         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, unused),
160         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.roughness1),
161         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.roughness2),
162         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.T),
163         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.offset),
164 BSDF_CLOSURE_CLASS_END(HairReflection, hair_reflection)
165
166 BSDF_CLOSURE_CLASS_BEGIN(HairTransmission, hair_transmission, HairBsdf, LABEL_GLOSSY)
167         CLOSURE_FLOAT3_PARAM(HairTransmissionClosure, unused),
168         CLOSURE_FLOAT_PARAM(HairTransmissionClosure, params.roughness1),
169         CLOSURE_FLOAT_PARAM(HairTransmissionClosure, params.roughness2),
170         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.T),
171         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.offset),
172 BSDF_CLOSURE_CLASS_END(HairTransmission, hair_transmission)
173
174 VOLUME_CLOSURE_CLASS_BEGIN(VolumeHenyeyGreenstein, henyey_greenstein, HenyeyGreensteinVolume, LABEL_VOLUME_SCATTER)
175         CLOSURE_FLOAT_PARAM(VolumeHenyeyGreensteinClosure, params.g),
176 VOLUME_CLOSURE_CLASS_END(VolumeHenyeyGreenstein, henyey_greenstein)
177
178 VOLUME_CLOSURE_CLASS_BEGIN(VolumeAbsorption, absorption, ShaderClosure, LABEL_SINGULAR)
179 VOLUME_CLOSURE_CLASS_END(VolumeAbsorption, absorption)
180
181 BSDF_CLOSURE_CLASS_BEGIN(PrincipledDiffuse, principled_diffuse, PrincipledDiffuseBsdf, LABEL_DIFFUSE)
182         CLOSURE_FLOAT3_PARAM(PrincipledDiffuseClosure, params.N),
183         CLOSURE_FLOAT_PARAM(PrincipledDiffuseClosure, params.roughness),
184 BSDF_CLOSURE_CLASS_END(PrincipledDiffuse, principled_diffuse)
185
186 BSDF_CLOSURE_CLASS_BEGIN(PrincipledSheen, principled_sheen, PrincipledSheenBsdf, LABEL_DIFFUSE)
187         CLOSURE_FLOAT3_PARAM(PrincipledSheenClosure, params.N),
188 BSDF_CLOSURE_CLASS_END(PrincipledSheen, principled_sheen)
189
190 /* DISNEY PRINCIPLED CLEARCOAT */
191 class PrincipledClearcoatClosure : public CBSDFClosure {
192 public:
193         MicrofacetBsdf params;
194         float clearcoat, clearcoat_gloss;
195
196         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
197         {
198                 MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
199                 MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
200                 if(bsdf && extra) {
201                         bsdf->extra = extra;
202
203                         bsdf->ior = 1.5f;
204
205                         bsdf->alpha_x = 0.1f * (1.0f - clearcoat_gloss) + 0.001f * clearcoat_gloss;
206                         bsdf->alpha_y = 0.1f * (1.0f - clearcoat_gloss) + 0.001f * clearcoat_gloss;
207
208                         bsdf->extra->cspec0 = make_float3(0.04f, 0.04f, 0.04f);
209                         bsdf->extra->clearcoat = clearcoat;
210
211                         return bsdf;
212                 }
213
214                 return NULL;
215         }
216
217         void setup(ShaderData *sd, int path_flag, float3 weight)
218         {
219                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
220                 sd->flag |= (bsdf) ? bsdf_microfacet_ggx_clearcoat_setup(bsdf) : 0;
221         }
222 };
223
224 ClosureParam *closure_bsdf_principled_clearcoat_params()
225 {
226         static ClosureParam params[] = {
227                 CLOSURE_FLOAT3_PARAM(PrincipledClearcoatClosure, params.N),
228                 CLOSURE_FLOAT_PARAM(PrincipledClearcoatClosure, clearcoat),
229                 CLOSURE_FLOAT_PARAM(PrincipledClearcoatClosure, clearcoat_gloss),
230                 CLOSURE_STRING_KEYPARAM(PrincipledClearcoatClosure, label, "label"),
231                 CLOSURE_FINISH_PARAM(PrincipledClearcoatClosure)
232         };
233         return params;
234 }
235 CCLOSURE_PREPARE(closure_bsdf_principled_clearcoat_prepare, PrincipledClearcoatClosure)
236
237
238 /* Registration */
239
240 static void register_closure(OSL::ShadingSystem *ss, const char *name, int id, OSL::ClosureParam *params, OSL::PrepareClosureFunc prepare)
241 {
242         /* optimization: it's possible to not use a prepare function at all and
243          * only initialize the actual class when accessing the closure component
244          * data, but then we need to map the id to the class somehow */
245         ss->register_closure(name, id, params, prepare, NULL, 16);
246 }
247
248 void OSLShader::register_closures(OSLShadingSystem *ss_)
249 {
250         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)ss_;
251         int id = 0;
252
253         register_closure(ss, "diffuse", id++,
254                 bsdf_diffuse_params(), bsdf_diffuse_prepare);
255         register_closure(ss, "oren_nayar", id++,
256                 bsdf_oren_nayar_params(), bsdf_oren_nayar_prepare);
257         register_closure(ss, "translucent", id++,
258                 bsdf_translucent_params(), bsdf_translucent_prepare);
259         register_closure(ss, "reflection", id++,
260                 bsdf_reflection_params(), bsdf_reflection_prepare);
261         register_closure(ss, "refraction", id++,
262                 bsdf_refraction_params(), bsdf_refraction_prepare);
263         register_closure(ss, "transparent", id++,
264                 bsdf_transparent_params(), bsdf_transparent_prepare);
265         register_closure(ss, "microfacet_ggx", id++,
266                 bsdf_microfacet_ggx_params(), bsdf_microfacet_ggx_prepare);
267         register_closure(ss, "microfacet_ggx_aniso", id++,
268                 bsdf_microfacet_ggx_aniso_params(), bsdf_microfacet_ggx_aniso_prepare);
269         register_closure(ss, "microfacet_ggx_refraction", id++,
270                 bsdf_microfacet_ggx_refraction_params(), bsdf_microfacet_ggx_refraction_prepare);
271         register_closure(ss, "microfacet_multi_ggx", id++,
272                 closure_bsdf_microfacet_multi_ggx_params(), closure_bsdf_microfacet_multi_ggx_prepare);
273         register_closure(ss, "microfacet_multi_ggx_glass", id++,
274                 closure_bsdf_microfacet_multi_ggx_glass_params(), closure_bsdf_microfacet_multi_ggx_glass_prepare);
275         register_closure(ss, "microfacet_multi_ggx_aniso", id++,
276                 closure_bsdf_microfacet_multi_ggx_aniso_params(), closure_bsdf_microfacet_multi_ggx_aniso_prepare);
277         register_closure(ss, "microfacet_ggx_fresnel", id++,
278                 closure_bsdf_microfacet_ggx_fresnel_params(), closure_bsdf_microfacet_ggx_fresnel_prepare);
279         register_closure(ss, "microfacet_ggx_aniso_fresnel", id++,
280                 closure_bsdf_microfacet_ggx_aniso_fresnel_params(), closure_bsdf_microfacet_ggx_aniso_fresnel_prepare);
281         register_closure(ss, "microfacet_multi_ggx_fresnel", id++,
282                 closure_bsdf_microfacet_multi_ggx_fresnel_params(), closure_bsdf_microfacet_multi_ggx_fresnel_prepare);
283         register_closure(ss, "microfacet_multi_ggx_glass_fresnel", id++,
284                 closure_bsdf_microfacet_multi_ggx_glass_fresnel_params(), closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare);
285         register_closure(ss, "microfacet_multi_ggx_aniso_fresnel", id++,
286                 closure_bsdf_microfacet_multi_ggx_aniso_fresnel_params(), closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare);
287         register_closure(ss, "microfacet_beckmann", id++,
288                 bsdf_microfacet_beckmann_params(), bsdf_microfacet_beckmann_prepare);
289         register_closure(ss, "microfacet_beckmann_aniso", id++,
290                 bsdf_microfacet_beckmann_aniso_params(), bsdf_microfacet_beckmann_aniso_prepare);
291         register_closure(ss, "microfacet_beckmann_refraction", id++,
292                 bsdf_microfacet_beckmann_refraction_params(), bsdf_microfacet_beckmann_refraction_prepare);
293         register_closure(ss, "ashikhmin_shirley", id++,
294                 bsdf_ashikhmin_shirley_aniso_params(), bsdf_ashikhmin_shirley_aniso_prepare);
295         register_closure(ss, "ashikhmin_velvet", id++,
296                 bsdf_ashikhmin_velvet_params(), bsdf_ashikhmin_velvet_prepare);
297         register_closure(ss, "diffuse_toon", id++,
298                 bsdf_diffuse_toon_params(), bsdf_diffuse_toon_prepare);
299         register_closure(ss, "glossy_toon", id++,
300                 bsdf_glossy_toon_params(), bsdf_glossy_toon_prepare);
301         register_closure(ss, "principled_diffuse", id++,
302                 bsdf_principled_diffuse_params(), bsdf_principled_diffuse_prepare);
303         register_closure(ss, "principled_sheen", id++,
304                 bsdf_principled_sheen_params(), bsdf_principled_sheen_prepare);
305         register_closure(ss, "principled_clearcoat", id++,
306                 closure_bsdf_principled_clearcoat_params(), closure_bsdf_principled_clearcoat_prepare);
307
308         register_closure(ss, "emission", id++,
309                 closure_emission_params(), closure_emission_prepare);
310         register_closure(ss, "background", id++,
311                 closure_background_params(), closure_background_prepare);
312         register_closure(ss, "holdout", id++,
313                 closure_holdout_params(), closure_holdout_prepare);
314         register_closure(ss, "ambient_occlusion", id++,
315                 closure_ambient_occlusion_params(), closure_ambient_occlusion_prepare);
316         register_closure(ss, "diffuse_ramp", id++,
317                 closure_bsdf_diffuse_ramp_params(), closure_bsdf_diffuse_ramp_prepare);
318         register_closure(ss, "phong_ramp", id++,
319                 closure_bsdf_phong_ramp_params(), closure_bsdf_phong_ramp_prepare);
320         register_closure(ss, "bssrdf_cubic", id++,
321                 closure_bssrdf_cubic_params(), closure_bssrdf_cubic_prepare);
322         register_closure(ss, "bssrdf_gaussian", id++,
323                 closure_bssrdf_gaussian_params(), closure_bssrdf_gaussian_prepare);
324         register_closure(ss, "bssrdf_burley", id++,
325                 closure_bssrdf_burley_params(), closure_bssrdf_burley_prepare);
326         register_closure(ss, "bssrdf_principled", id++,
327                 closure_bssrdf_principled_params(), closure_bssrdf_principled_prepare);
328
329         register_closure(ss, "hair_reflection", id++,
330                 bsdf_hair_reflection_params(), bsdf_hair_reflection_prepare);
331         register_closure(ss, "hair_transmission", id++,
332                 bsdf_hair_transmission_params(), bsdf_hair_transmission_prepare);
333
334         register_closure(ss, "henyey_greenstein", id++,
335                 volume_henyey_greenstein_params(), volume_henyey_greenstein_prepare);
336         register_closure(ss, "absorption", id++,
337                 volume_absorption_params(), volume_absorption_prepare);
338 }
339
340 /* BSDF Closure */
341
342 bool CBSDFClosure::skip(const ShaderData *sd, int path_flag, int scattering)
343 {
344         /* caustic options */
345         if((scattering & LABEL_GLOSSY) && (path_flag & PATH_RAY_DIFFUSE)) {
346                 KernelGlobals *kg = sd->osl_globals;
347
348                 if((!kernel_data.integrator.caustics_reflective && (scattering & LABEL_REFLECT)) ||
349                    (!kernel_data.integrator.caustics_refractive && (scattering & LABEL_TRANSMIT)))
350                 {
351                         return true;
352                 }
353         }
354
355         return false;
356 }
357
358
359 /* GGX closures with Fresnel */
360
361 class MicrofacetFresnelClosure : public CBSDFClosure {
362 public:
363         MicrofacetBsdf params;
364         float3 color;
365         float3 cspec0;
366
367         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
368         {
369                 /* Technically, the MultiGGX Glass closure may also transmit. However,
370                 * since this is set statically and only used for caustic flags, this
371                 * is probably as good as it gets. */
372                 if(!skip(sd, path_flag, LABEL_GLOSSY | LABEL_REFLECT)) {
373                         MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
374                         MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
375                         if(bsdf && extra) {
376                                 bsdf->extra = extra;
377                                 bsdf->extra->color = color;
378                                 bsdf->extra->cspec0 = cspec0;
379                                 return bsdf;
380                         }
381                 }
382
383                 return NULL;
384         }
385 };
386
387 class MicrofacetGGXFresnelClosure : public MicrofacetFresnelClosure {
388 public:
389         void setup(ShaderData *sd, int path_flag, float3 weight)
390         {
391                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
392                 sd->flag |= (bsdf) ? bsdf_microfacet_ggx_fresnel_setup(bsdf) : 0;
393         }
394 };
395
396 ClosureParam *closure_bsdf_microfacet_ggx_fresnel_params()
397 {
398         static ClosureParam params[] = {
399                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, params.N),
400                 CLOSURE_FLOAT_PARAM(MicrofacetGGXFresnelClosure, params.alpha_x),
401                 CLOSURE_FLOAT_PARAM(MicrofacetGGXFresnelClosure, params.ior),
402                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, color),
403                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, cspec0),
404                 CLOSURE_STRING_KEYPARAM(MicrofacetGGXFresnelClosure, label, "label"),
405                 CLOSURE_FINISH_PARAM(MicrofacetGGXFresnelClosure)
406         };
407         return params;
408 }
409 CCLOSURE_PREPARE(closure_bsdf_microfacet_ggx_fresnel_prepare, MicrofacetGGXFresnelClosure);
410
411 class MicrofacetGGXAnisoFresnelClosure : public MicrofacetFresnelClosure {
412 public:
413         void setup(ShaderData *sd, int path_flag, float3 weight)
414         {
415                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
416                 sd->flag |= (bsdf) ? bsdf_microfacet_ggx_aniso_fresnel_setup(bsdf) : 0;
417         }
418 };
419
420 ClosureParam *closure_bsdf_microfacet_ggx_aniso_fresnel_params()
421 {
422         static ClosureParam params[] = {
423                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, params.N),
424                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, params.T),
425                 CLOSURE_FLOAT_PARAM(MicrofacetGGXFresnelClosure, params.alpha_x),
426                 CLOSURE_FLOAT_PARAM(MicrofacetGGXFresnelClosure, params.alpha_y),
427                 CLOSURE_FLOAT_PARAM(MicrofacetGGXFresnelClosure, params.ior),
428                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, color),
429                 CLOSURE_FLOAT3_PARAM(MicrofacetGGXFresnelClosure, cspec0),
430                 CLOSURE_STRING_KEYPARAM(MicrofacetGGXFresnelClosure, label, "label"),
431                 CLOSURE_FINISH_PARAM(MicrofacetGGXFresnelClosure)
432         };
433         return params;
434 }
435 CCLOSURE_PREPARE(closure_bsdf_microfacet_ggx_aniso_fresnel_prepare, MicrofacetGGXAnisoFresnelClosure);
436
437
438 /* Multiscattering GGX closures */
439
440 class MicrofacetMultiClosure : public CBSDFClosure {
441 public:
442         MicrofacetBsdf params;
443         float3 color;
444
445         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
446         {
447                 /* Technically, the MultiGGX closure may also transmit. However,
448                  * since this is set statically and only used for caustic flags, this
449                  * is probably as good as it gets. */
450             if(!skip(sd, path_flag, LABEL_GLOSSY|LABEL_REFLECT)) {
451                         MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
452                         MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
453                         if(bsdf && extra) {
454                                 bsdf->extra = extra;
455                                 bsdf->extra->color = color;
456                                 return bsdf;
457                         }
458                 }
459
460                 return NULL;
461         }
462 };
463
464 class MicrofacetMultiGGXClosure : public MicrofacetMultiClosure {
465 public:
466         void setup(ShaderData *sd, int path_flag, float3 weight)
467         {
468                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
469                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_setup(bsdf) : 0;
470         }
471 };
472
473 ClosureParam *closure_bsdf_microfacet_multi_ggx_params()
474 {
475         static ClosureParam params[] = {
476                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N),
477                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x),
478                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color),
479                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"),
480                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure)
481         };
482         return params;
483 }
484 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_prepare, MicrofacetMultiGGXClosure);
485
486 class MicrofacetMultiGGXAnisoClosure : public MicrofacetMultiClosure {
487 public:
488         void setup(ShaderData *sd, int path_flag, float3 weight)
489         {
490                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
491                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_aniso_setup(bsdf) : 0;
492         }
493 };
494
495 ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_params()
496 {
497         static ClosureParam params[] = {
498                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N),
499                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.T),
500                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x),
501                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_y),
502                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color),
503                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"),
504                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure)
505         };
506         return params;
507 }
508 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_aniso_prepare, MicrofacetMultiGGXAnisoClosure);
509
510 class MicrofacetMultiGGXGlassClosure : public MicrofacetMultiClosure {
511 public:
512         MicrofacetMultiGGXGlassClosure() : MicrofacetMultiClosure() {}
513
514         void setup(ShaderData *sd, int path_flag, float3 weight)
515         {
516                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
517                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_glass_setup(bsdf) : 0;
518         }
519 };
520
521 ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_params()
522 {
523         static ClosureParam params[] = {
524                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N),
525                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x),
526                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.ior),
527                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color),
528                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"),
529                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure)
530         };
531         return params;
532 }
533 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_glass_prepare, MicrofacetMultiGGXGlassClosure);
534
535
536 /* Multiscattering GGX closures with Fresnel */
537
538 class MicrofacetMultiFresnelClosure : public CBSDFClosure {
539 public:
540         MicrofacetBsdf params;
541         float3 color;
542         float3 cspec0;
543
544         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
545         {
546                 /* Technically, the MultiGGX closure may also transmit. However,
547                 * since this is set statically and only used for caustic flags, this
548                 * is probably as good as it gets. */
549                 if(!skip(sd, path_flag, LABEL_GLOSSY | LABEL_REFLECT)) {
550                         MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
551                         MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
552                         if(bsdf && extra) {
553                                 bsdf->extra = extra;
554                                 bsdf->extra->color = color;
555                                 bsdf->extra->cspec0 = cspec0;
556                                 return bsdf;
557                         }
558                 }
559
560                 return NULL;
561         }
562 };
563
564 class MicrofacetMultiGGXFresnelClosure : public MicrofacetMultiFresnelClosure {
565 public:
566         void setup(ShaderData *sd, int path_flag, float3 weight)
567         {
568                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
569                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_fresnel_setup(bsdf) : 0;
570         }
571 };
572
573 ClosureParam *closure_bsdf_microfacet_multi_ggx_fresnel_params()
574 {
575         static ClosureParam params[] = {
576                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.N),
577                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_x),
578                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.ior),
579                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, color),
580                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, cspec0),
581                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXFresnelClosure, label, "label"),
582                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXFresnelClosure)
583         };
584         return params;
585 }
586 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_fresnel_prepare, MicrofacetMultiGGXFresnelClosure);
587
588 class MicrofacetMultiGGXAnisoFresnelClosure : public MicrofacetMultiFresnelClosure {
589 public:
590         void setup(ShaderData *sd, int path_flag, float3 weight)
591         {
592                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
593                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_aniso_fresnel_setup(bsdf) : 0;
594         }
595 };
596
597 ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_fresnel_params()
598 {
599         static ClosureParam params[] = {
600                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.N),
601                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.T),
602                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_x),
603                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_y),
604                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.ior),
605                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, color),
606                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, cspec0),
607                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXFresnelClosure, label, "label"),
608                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXFresnelClosure)
609         };
610         return params;
611 }
612 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare, MicrofacetMultiGGXAnisoFresnelClosure);
613
614 class MicrofacetMultiGGXGlassFresnelClosure : public MicrofacetMultiFresnelClosure {
615 public:
616         MicrofacetMultiGGXGlassFresnelClosure() : MicrofacetMultiFresnelClosure() {}
617
618         void setup(ShaderData *sd, int path_flag, float3 weight)
619         {
620                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
621                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_glass_fresnel_setup(bsdf) : 0;
622         }
623 };
624
625 ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_fresnel_params()
626 {
627         static ClosureParam params[] = {
628                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.N),
629                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_x),
630                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.ior),
631                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, color),
632                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, cspec0),
633                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXFresnelClosure, label, "label"),
634                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXFresnelClosure)
635         };
636         return params;
637 }
638 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare, MicrofacetMultiGGXGlassFresnelClosure);
639
640 CCL_NAMESPACE_END
641