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