597d25e9f300a6e8a0fb9062de7f4cbaf4269777
[blender-staging.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(AshikhminVelvet, ashikhmin_velvet, VelvetBsdf, LABEL_DIFFUSE)
96         CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, params.N),
97         CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, params.sigma),
98 BSDF_CLOSURE_CLASS_END(AshikhminVelvet, ashikhmin_velvet)
99
100 BSDF_CLOSURE_CLASS_BEGIN(AshikhminShirley, ashikhmin_shirley_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
101         CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, params.N),
102         CLOSURE_FLOAT3_PARAM(AshikhminShirleyClosure, params.T),
103         CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, params.alpha_x),
104         CLOSURE_FLOAT_PARAM(AshikhminShirleyClosure, params.alpha_y),
105 BSDF_CLOSURE_CLASS_END(AshikhminShirley, ashikhmin_shirley_aniso)
106
107 BSDF_CLOSURE_CLASS_BEGIN(DiffuseToon, diffuse_toon, ToonBsdf, LABEL_DIFFUSE)
108         CLOSURE_FLOAT3_PARAM(DiffuseToonClosure, params.N),
109         CLOSURE_FLOAT_PARAM(DiffuseToonClosure, params.size),
110         CLOSURE_FLOAT_PARAM(DiffuseToonClosure, params.smooth),
111 BSDF_CLOSURE_CLASS_END(DiffuseToon, diffuse_toon)
112
113 BSDF_CLOSURE_CLASS_BEGIN(GlossyToon, glossy_toon, ToonBsdf, LABEL_GLOSSY)
114         CLOSURE_FLOAT3_PARAM(GlossyToonClosure, params.N),
115         CLOSURE_FLOAT_PARAM(GlossyToonClosure, params.size),
116         CLOSURE_FLOAT_PARAM(GlossyToonClosure, params.smooth),
117 BSDF_CLOSURE_CLASS_END(GlossyToon, glossy_toon)
118
119 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
120         CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, params.N),
121         CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, params.alpha_x),
122 BSDF_CLOSURE_CLASS_END(MicrofacetGGX, microfacet_ggx)
123
124 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXAniso, microfacet_ggx_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
125         CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, params.N),
126         CLOSURE_FLOAT3_PARAM(MicrofacetGGXAnisoClosure, params.T),
127         CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, params.alpha_x),
128         CLOSURE_FLOAT_PARAM(MicrofacetGGXAnisoClosure, params.alpha_y),
129 BSDF_CLOSURE_CLASS_END(MicrofacetGGXAniso, microfacet_ggx_aniso)
130
131 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
132         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, params.N),
133         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, params.alpha_x),
134 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmann, microfacet_beckmann)
135
136 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannAniso, microfacet_beckmann_aniso, MicrofacetBsdf, LABEL_GLOSSY|LABEL_REFLECT)
137         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, params.N),
138         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannAnisoClosure, params.T),
139         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, params.alpha_x),
140         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannAnisoClosure, params.alpha_y),
141 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannAniso, microfacet_beckmann_aniso)
142
143 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, MicrofacetBsdf, LABEL_GLOSSY|LABEL_TRANSMIT)
144         CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, params.N),
145         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, params.alpha_x),
146         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, params.ior),
147 BSDF_CLOSURE_CLASS_END(MicrofacetGGXRefraction, microfacet_ggx_refraction)
148
149 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, MicrofacetBsdf, LABEL_GLOSSY|LABEL_TRANSMIT)
150         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, params.N),
151         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, params.alpha_x),
152         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, params.ior),
153 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction)
154
155 BSDF_CLOSURE_CLASS_BEGIN(HairReflection, hair_reflection, HairBsdf, LABEL_GLOSSY)
156         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.N),
157         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.roughness1),
158         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.roughness2),
159         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.T),
160         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.offset),
161 BSDF_CLOSURE_CLASS_END(HairReflection, hair_reflection)
162
163 BSDF_CLOSURE_CLASS_BEGIN(HairTransmission, hair_transmission, HairBsdf, LABEL_GLOSSY)
164         CLOSURE_FLOAT3_PARAM(HairTransmissionClosure, params.N),
165         CLOSURE_FLOAT_PARAM(HairTransmissionClosure, params.roughness1),
166         CLOSURE_FLOAT_PARAM(HairTransmissionClosure, params.roughness2),
167         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, params.T),
168         CLOSURE_FLOAT_PARAM(HairReflectionClosure, params.offset),
169 BSDF_CLOSURE_CLASS_END(HairTransmission, hair_transmission)
170
171 BSDF_CLOSURE_CLASS_BEGIN(PrincipledDiffuse, principled_diffuse, PrincipledDiffuseBsdf, LABEL_DIFFUSE)
172         CLOSURE_FLOAT3_PARAM(PrincipledDiffuseClosure, params.N),
173         CLOSURE_FLOAT_PARAM(PrincipledDiffuseClosure, params.roughness),
174 BSDF_CLOSURE_CLASS_END(PrincipledDiffuse, principled_diffuse)
175
176 BSDF_CLOSURE_CLASS_BEGIN(PrincipledSheen, principled_sheen, PrincipledSheenBsdf, LABEL_DIFFUSE)
177         CLOSURE_FLOAT3_PARAM(PrincipledSheenClosure, params.N),
178 BSDF_CLOSURE_CLASS_END(PrincipledSheen, principled_sheen)
179
180 /* DISNEY PRINCIPLED CLEARCOAT */
181 class PrincipledClearcoatClosure : public CBSDFClosure {
182 public:
183         MicrofacetBsdf params;
184         float clearcoat, clearcoat_roughness;
185
186         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
187         {
188                 MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
189                 if(!bsdf) {
190                         return NULL;
191                 }
192
193                 MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
194                 if(!extra) {
195                         return NULL;
196                 }
197
198                 bsdf->extra = extra;
199                 bsdf->ior = 1.5f;
200                 bsdf->alpha_x = clearcoat_roughness;
201                 bsdf->alpha_y = clearcoat_roughness;
202                 bsdf->extra->cspec0 = make_float3(0.04f, 0.04f, 0.04f);
203                 bsdf->extra->clearcoat = clearcoat;
204                 return bsdf;
205         }
206
207         void setup(ShaderData *sd, int path_flag, float3 weight)
208         {
209                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
210                 sd->flag |= (bsdf) ? bsdf_microfacet_ggx_clearcoat_setup(bsdf, sd) : 0;
211         }
212 };
213
214 ClosureParam *closure_bsdf_principled_clearcoat_params()
215 {
216         static ClosureParam params[] = {
217                 CLOSURE_FLOAT3_PARAM(PrincipledClearcoatClosure, params.N),
218                 CLOSURE_FLOAT_PARAM(PrincipledClearcoatClosure, clearcoat),
219                 CLOSURE_FLOAT_PARAM(PrincipledClearcoatClosure, clearcoat_roughness),
220                 CLOSURE_STRING_KEYPARAM(PrincipledClearcoatClosure, label, "label"),
221                 CLOSURE_FINISH_PARAM(PrincipledClearcoatClosure)
222         };
223         return params;
224 }
225 CCLOSURE_PREPARE(closure_bsdf_principled_clearcoat_prepare, PrincipledClearcoatClosure)
226
227
228 /* Registration */
229
230 static void register_closure(OSL::ShadingSystem *ss, const char *name, int id, OSL::ClosureParam *params, OSL::PrepareClosureFunc prepare)
231 {
232         /* optimization: it's possible to not use a prepare function at all and
233          * only initialize the actual class when accessing the closure component
234          * data, but then we need to map the id to the class somehow */
235 #if OSL_LIBRARY_VERSION_CODE >= 10900
236         ss->register_closure(name, id, params, prepare, NULL);
237 #else
238         ss->register_closure(name, id, params, prepare, NULL, 16);
239 #endif
240 }
241
242 void OSLShader::register_closures(OSLShadingSystem *ss_)
243 {
244         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)ss_;
245         int id = 0;
246
247         register_closure(ss, "diffuse", id++,
248                 bsdf_diffuse_params(), bsdf_diffuse_prepare);
249         register_closure(ss, "oren_nayar", id++,
250                 bsdf_oren_nayar_params(), bsdf_oren_nayar_prepare);
251         register_closure(ss, "translucent", id++,
252                 bsdf_translucent_params(), bsdf_translucent_prepare);
253         register_closure(ss, "reflection", id++,
254                 bsdf_reflection_params(), bsdf_reflection_prepare);
255         register_closure(ss, "refraction", id++,
256                 bsdf_refraction_params(), bsdf_refraction_prepare);
257         register_closure(ss, "transparent", id++,
258                 closure_bsdf_transparent_params(), closure_bsdf_transparent_prepare);
259         register_closure(ss, "microfacet_ggx", id++,
260                 bsdf_microfacet_ggx_params(), bsdf_microfacet_ggx_prepare);
261         register_closure(ss, "microfacet_ggx_aniso", id++,
262                 bsdf_microfacet_ggx_aniso_params(), bsdf_microfacet_ggx_aniso_prepare);
263         register_closure(ss, "microfacet_ggx_refraction", id++,
264                 bsdf_microfacet_ggx_refraction_params(), bsdf_microfacet_ggx_refraction_prepare);
265         register_closure(ss, "microfacet_multi_ggx", id++,
266                 closure_bsdf_microfacet_multi_ggx_params(), closure_bsdf_microfacet_multi_ggx_prepare);
267         register_closure(ss, "microfacet_multi_ggx_glass", id++,
268                 closure_bsdf_microfacet_multi_ggx_glass_params(), closure_bsdf_microfacet_multi_ggx_glass_prepare);
269         register_closure(ss, "microfacet_multi_ggx_aniso", id++,
270                 closure_bsdf_microfacet_multi_ggx_aniso_params(), closure_bsdf_microfacet_multi_ggx_aniso_prepare);
271         register_closure(ss, "microfacet_ggx_fresnel", id++,
272                 closure_bsdf_microfacet_ggx_fresnel_params(), closure_bsdf_microfacet_ggx_fresnel_prepare);
273         register_closure(ss, "microfacet_ggx_aniso_fresnel", id++,
274                 closure_bsdf_microfacet_ggx_aniso_fresnel_params(), closure_bsdf_microfacet_ggx_aniso_fresnel_prepare);
275         register_closure(ss, "microfacet_multi_ggx_fresnel", id++,
276                 closure_bsdf_microfacet_multi_ggx_fresnel_params(), closure_bsdf_microfacet_multi_ggx_fresnel_prepare);
277         register_closure(ss, "microfacet_multi_ggx_glass_fresnel", id++,
278                 closure_bsdf_microfacet_multi_ggx_glass_fresnel_params(), closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare);
279         register_closure(ss, "microfacet_multi_ggx_aniso_fresnel", id++,
280                 closure_bsdf_microfacet_multi_ggx_aniso_fresnel_params(), closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare);
281         register_closure(ss, "microfacet_beckmann", id++,
282                 bsdf_microfacet_beckmann_params(), bsdf_microfacet_beckmann_prepare);
283         register_closure(ss, "microfacet_beckmann_aniso", id++,
284                 bsdf_microfacet_beckmann_aniso_params(), bsdf_microfacet_beckmann_aniso_prepare);
285         register_closure(ss, "microfacet_beckmann_refraction", id++,
286                 bsdf_microfacet_beckmann_refraction_params(), bsdf_microfacet_beckmann_refraction_prepare);
287         register_closure(ss, "ashikhmin_shirley", id++,
288                 bsdf_ashikhmin_shirley_aniso_params(), bsdf_ashikhmin_shirley_aniso_prepare);
289         register_closure(ss, "ashikhmin_velvet", id++,
290                 bsdf_ashikhmin_velvet_params(), bsdf_ashikhmin_velvet_prepare);
291         register_closure(ss, "diffuse_toon", id++,
292                 bsdf_diffuse_toon_params(), bsdf_diffuse_toon_prepare);
293         register_closure(ss, "glossy_toon", id++,
294                 bsdf_glossy_toon_params(), bsdf_glossy_toon_prepare);
295         register_closure(ss, "principled_diffuse", id++,
296                 bsdf_principled_diffuse_params(), bsdf_principled_diffuse_prepare);
297         register_closure(ss, "principled_sheen", id++,
298                 bsdf_principled_sheen_params(), bsdf_principled_sheen_prepare);
299         register_closure(ss, "principled_clearcoat", id++,
300                 closure_bsdf_principled_clearcoat_params(), closure_bsdf_principled_clearcoat_prepare);
301
302         register_closure(ss, "emission", id++,
303                 closure_emission_params(), closure_emission_prepare);
304         register_closure(ss, "background", id++,
305                 closure_background_params(), closure_background_prepare);
306         register_closure(ss, "holdout", id++,
307                 closure_holdout_params(), closure_holdout_prepare);
308         register_closure(ss, "ambient_occlusion", id++,
309                 closure_ambient_occlusion_params(), closure_ambient_occlusion_prepare);
310         register_closure(ss, "diffuse_ramp", id++,
311                 closure_bsdf_diffuse_ramp_params(), closure_bsdf_diffuse_ramp_prepare);
312         register_closure(ss, "phong_ramp", id++,
313                 closure_bsdf_phong_ramp_params(), closure_bsdf_phong_ramp_prepare);
314         register_closure(ss, "bssrdf_cubic", id++,
315                 closure_bssrdf_cubic_params(), closure_bssrdf_cubic_prepare);
316         register_closure(ss, "bssrdf_gaussian", id++,
317                 closure_bssrdf_gaussian_params(), closure_bssrdf_gaussian_prepare);
318         register_closure(ss, "bssrdf_burley", id++,
319                 closure_bssrdf_burley_params(), closure_bssrdf_burley_prepare);
320         register_closure(ss, "bssrdf_principled", id++,
321                 closure_bssrdf_principled_params(), closure_bssrdf_principled_prepare);
322
323         register_closure(ss, "hair_reflection", id++,
324                 bsdf_hair_reflection_params(), bsdf_hair_reflection_prepare);
325         register_closure(ss, "hair_transmission", id++,
326                 bsdf_hair_transmission_params(), bsdf_hair_transmission_prepare);
327
328         register_closure(ss, "henyey_greenstein", id++,
329                 closure_henyey_greenstein_params(), closure_henyey_greenstein_prepare);
330         register_closure(ss, "absorption", id++,
331                 closure_absorption_params(), closure_absorption_prepare);
332 }
333
334 /* BSDF Closure */
335
336 bool CBSDFClosure::skip(const ShaderData *sd, int path_flag, int scattering)
337 {
338         /* caustic options */
339         if((scattering & LABEL_GLOSSY) && (path_flag & PATH_RAY_DIFFUSE)) {
340                 KernelGlobals *kg = sd->osl_globals;
341
342                 if((!kernel_data.integrator.caustics_reflective && (scattering & LABEL_REFLECT)) ||
343                    (!kernel_data.integrator.caustics_refractive && (scattering & LABEL_TRANSMIT)))
344                 {
345                         return true;
346                 }
347         }
348
349         return false;
350 }
351
352
353 /* GGX closures with Fresnel */
354
355 class MicrofacetFresnelClosure : public CBSDFClosure {
356 public:
357         MicrofacetBsdf params;
358         float3 color;
359         float3 cspec0;
360
361         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
362         {
363                 /* Technically, the MultiGGX Glass closure may also transmit. However,
364                 * since this is set statically and only used for caustic flags, this
365                 * is probably as good as it gets. */
366                 if(skip(sd, path_flag, LABEL_GLOSSY | LABEL_REFLECT)) {
367                         return NULL;
368                 }
369
370                 MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
371                 if(!bsdf) {
372                         return NULL;
373                 }
374
375                 MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
376                 if(!extra) {
377                         return NULL;
378                 }
379
380                 bsdf->extra = extra;
381                 bsdf->extra->color = color;
382                 bsdf->extra->cspec0 = cspec0;
383                 return bsdf;
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, sd) : 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, sd) : 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                         return NULL;
452                 }
453
454                 MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
455                 if(!bsdf) {
456                         return NULL;
457                 }
458
459                 MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
460                 if(!extra) {
461                         return NULL;
462                 }
463
464                 bsdf->extra = extra;
465                 bsdf->extra->color = color;
466                 return bsdf;
467         }
468 };
469
470 class MicrofacetMultiGGXClosure : public MicrofacetMultiClosure {
471 public:
472         void setup(ShaderData *sd, int path_flag, float3 weight)
473         {
474                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
475                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_setup(bsdf) : 0;
476         }
477 };
478
479 ClosureParam *closure_bsdf_microfacet_multi_ggx_params()
480 {
481         static ClosureParam params[] = {
482                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N),
483                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x),
484                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color),
485                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"),
486                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure)
487         };
488         return params;
489 }
490 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_prepare, MicrofacetMultiGGXClosure);
491
492 class MicrofacetMultiGGXAnisoClosure : public MicrofacetMultiClosure {
493 public:
494         void setup(ShaderData *sd, int path_flag, float3 weight)
495         {
496                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
497                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_aniso_setup(bsdf) : 0;
498         }
499 };
500
501 ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_params()
502 {
503         static ClosureParam params[] = {
504                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N),
505                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.T),
506                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x),
507                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_y),
508                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color),
509                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"),
510                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure)
511         };
512         return params;
513 }
514 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_aniso_prepare, MicrofacetMultiGGXAnisoClosure);
515
516 class MicrofacetMultiGGXGlassClosure : public MicrofacetMultiClosure {
517 public:
518         MicrofacetMultiGGXGlassClosure() : MicrofacetMultiClosure() {}
519
520         void setup(ShaderData *sd, int path_flag, float3 weight)
521         {
522                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
523                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_glass_setup(bsdf) : 0;
524         }
525 };
526
527 ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_params()
528 {
529         static ClosureParam params[] = {
530                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, params.N),
531                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.alpha_x),
532                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXClosure, params.ior),
533                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXClosure, color),
534                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXClosure, label, "label"),
535                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXClosure)
536         };
537         return params;
538 }
539 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_glass_prepare, MicrofacetMultiGGXGlassClosure);
540
541
542 /* Multiscattering GGX closures with Fresnel */
543
544 class MicrofacetMultiFresnelClosure : public CBSDFClosure {
545 public:
546         MicrofacetBsdf params;
547         float3 color;
548         float3 cspec0;
549
550         MicrofacetBsdf *alloc(ShaderData *sd, int path_flag, float3 weight)
551         {
552                 /* Technically, the MultiGGX closure may also transmit. However,
553                 * since this is set statically and only used for caustic flags, this
554                 * is probably as good as it gets. */
555                 if(skip(sd, path_flag, LABEL_GLOSSY | LABEL_REFLECT)) {
556                         return NULL;
557                 }
558
559                 MicrofacetBsdf *bsdf = (MicrofacetBsdf*)bsdf_alloc_osl(sd, sizeof(MicrofacetBsdf), weight, &params);
560                 if(!bsdf) {
561                         return NULL;
562                 }
563
564                 MicrofacetExtra *extra = (MicrofacetExtra*)closure_alloc_extra(sd, sizeof(MicrofacetExtra));
565                 if(!extra) {
566                         return NULL;
567                 }
568
569                 bsdf->extra = extra;
570                 bsdf->extra->color = color;
571                 bsdf->extra->cspec0 = cspec0;
572                 return bsdf;
573         }
574 };
575
576 class MicrofacetMultiGGXFresnelClosure : public MicrofacetMultiFresnelClosure {
577 public:
578         void setup(ShaderData *sd, int path_flag, float3 weight)
579         {
580                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
581                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_fresnel_setup(bsdf, sd) : 0;
582         }
583 };
584
585 ClosureParam *closure_bsdf_microfacet_multi_ggx_fresnel_params()
586 {
587         static ClosureParam params[] = {
588                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.N),
589                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_x),
590                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.ior),
591                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, color),
592                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, cspec0),
593                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXFresnelClosure, label, "label"),
594                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXFresnelClosure)
595         };
596         return params;
597 }
598 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_fresnel_prepare, MicrofacetMultiGGXFresnelClosure);
599
600 class MicrofacetMultiGGXAnisoFresnelClosure : public MicrofacetMultiFresnelClosure {
601 public:
602         void setup(ShaderData *sd, int path_flag, float3 weight)
603         {
604                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
605                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_aniso_fresnel_setup(bsdf, sd) : 0;
606         }
607 };
608
609 ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_fresnel_params()
610 {
611         static ClosureParam params[] = {
612                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.N),
613                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.T),
614                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_x),
615                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_y),
616                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.ior),
617                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, color),
618                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, cspec0),
619                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXFresnelClosure, label, "label"),
620                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXFresnelClosure)
621         };
622         return params;
623 }
624 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare, MicrofacetMultiGGXAnisoFresnelClosure);
625
626 class MicrofacetMultiGGXGlassFresnelClosure : public MicrofacetMultiFresnelClosure {
627 public:
628         MicrofacetMultiGGXGlassFresnelClosure() : MicrofacetMultiFresnelClosure() {}
629
630         void setup(ShaderData *sd, int path_flag, float3 weight)
631         {
632                 MicrofacetBsdf *bsdf = alloc(sd, path_flag, weight);
633                 sd->flag |= (bsdf) ? bsdf_microfacet_multi_ggx_glass_fresnel_setup(bsdf, sd) : 0;
634         }
635 };
636
637 ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_fresnel_params()
638 {
639         static ClosureParam params[] = {
640                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, params.N),
641                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.alpha_x),
642                 CLOSURE_FLOAT_PARAM(MicrofacetMultiGGXFresnelClosure, params.ior),
643                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, color),
644                 CLOSURE_FLOAT3_PARAM(MicrofacetMultiGGXFresnelClosure, cspec0),
645                 CLOSURE_STRING_KEYPARAM(MicrofacetMultiGGXFresnelClosure, label, "label"),
646                 CLOSURE_FINISH_PARAM(MicrofacetMultiGGXFresnelClosure)
647         };
648         return params;
649 }
650 CCLOSURE_PREPARE(closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare, MicrofacetMultiGGXGlassFresnelClosure);
651
652 /* Transparent */
653
654 class TransparentClosure : public CBSDFClosure {
655 public:
656         ShaderClosure params;
657         float3 unused;
658
659         void setup(ShaderData *sd, int path_flag, float3 weight)
660         {
661                 bsdf_transparent_setup(sd, weight);
662         }
663 };
664
665 ClosureParam *closure_bsdf_transparent_params()
666 {
667         static ClosureParam params[] = {
668                 CLOSURE_STRING_KEYPARAM(TransparentClosure, label, "label"),
669                 CLOSURE_FINISH_PARAM(TransparentClosure)
670         };
671         return params;
672 }
673
674 CCLOSURE_PREPARE(closure_bsdf_transparent_prepare, TransparentClosure)
675
676 /* Volume */
677
678 class VolumeAbsorptionClosure : public CBSDFClosure {
679 public:
680         void setup(ShaderData *sd, int path_flag, float3 weight)
681         {
682                 volume_extinction_setup(sd, weight);
683         }
684 };
685
686 ClosureParam *closure_absorption_params()
687 {
688         static ClosureParam params[] = {
689                 CLOSURE_STRING_KEYPARAM(VolumeAbsorptionClosure, label, "label"),
690                 CLOSURE_FINISH_PARAM(VolumeAbsorptionClosure)
691         };
692         return params;
693 }
694
695 CCLOSURE_PREPARE(closure_absorption_prepare, VolumeAbsorptionClosure)
696
697 class VolumeHenyeyGreensteinClosure : public CBSDFClosure {
698 public:
699         HenyeyGreensteinVolume params;
700
701         void setup(ShaderData *sd, int path_flag, float3 weight)
702         {
703                 volume_extinction_setup(sd, weight);
704
705             HenyeyGreensteinVolume *volume = (HenyeyGreensteinVolume*)bsdf_alloc_osl(sd, sizeof(HenyeyGreensteinVolume), weight, &params);
706                 sd->flag |= (volume) ? volume_henyey_greenstein_setup(volume) : 0;
707         }
708 };
709
710 ClosureParam *closure_henyey_greenstein_params()
711 {
712         static ClosureParam params[] = {
713                 CLOSURE_FLOAT_PARAM(VolumeHenyeyGreensteinClosure, params.g),
714                 CLOSURE_STRING_KEYPARAM(VolumeHenyeyGreensteinClosure, label, "label"),
715                 CLOSURE_FINISH_PARAM(VolumeHenyeyGreensteinClosure)
716         };
717         return params;
718 }
719
720 CCLOSURE_PREPARE(closure_henyey_greenstein_prepare, VolumeHenyeyGreensteinClosure)
721
722
723 CCL_NAMESPACE_END
724