Cycles / OSL:
[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., closure_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 "osl_closures.h"
37 #include "osl_shader.h"
38
39 #include "util_debug.h"
40 #include "util_math.h"
41 #include "util_param.h"
42
43 #include "kernel_types.h"
44 #include "kernel_montecarlo.h"
45
46 #include "closure/bsdf_util.h"
47 #include "closure/bsdf_ashikhmin_velvet.h"
48 #include "closure/bsdf_diffuse.h"
49 #include "closure/bsdf_microfacet.h"
50 #include "closure/bsdf_oren_nayar.h"
51 #include "closure/bsdf_reflection.h"
52 #include "closure/bsdf_refraction.h"
53 #include "closure/bsdf_transparent.h"
54 #include "closure/bsdf_ward.h"
55 #include "closure/bsdf_westin.h"
56
57 CCL_NAMESPACE_BEGIN
58
59 using namespace OSL;
60
61 /* BSDF class definitions */
62
63 BSDF_CLOSURE_CLASS_BEGIN(Diffuse, diffuse, diffuse, LABEL_DIFFUSE)
64         CLOSURE_FLOAT3_PARAM(DiffuseClosure, sc.N),
65 BSDF_CLOSURE_CLASS_END(Diffuse, diffuse)
66
67 BSDF_CLOSURE_CLASS_BEGIN(Translucent, translucent, translucent, LABEL_DIFFUSE)
68         CLOSURE_FLOAT3_PARAM(TranslucentClosure, sc.N),
69 BSDF_CLOSURE_CLASS_END(Translucent, translucent)
70
71 BSDF_CLOSURE_CLASS_BEGIN(OrenNayar, oren_nayar, oren_nayar, LABEL_DIFFUSE)
72         CLOSURE_FLOAT3_PARAM(OrenNayarClosure, sc.N),
73         CLOSURE_FLOAT_PARAM(OrenNayarClosure, sc.data0),
74 BSDF_CLOSURE_CLASS_END(OrenNayar, oren_nayar)
75
76 BSDF_CLOSURE_CLASS_BEGIN(Reflection, reflection, reflection, LABEL_SINGULAR)
77         CLOSURE_FLOAT3_PARAM(ReflectionClosure, sc.N),
78 BSDF_CLOSURE_CLASS_END(Reflection, reflection)
79
80 BSDF_CLOSURE_CLASS_BEGIN(Refraction, refraction, refraction, LABEL_SINGULAR)
81         CLOSURE_FLOAT3_PARAM(RefractionClosure, sc.N),
82         CLOSURE_FLOAT_PARAM(RefractionClosure, sc.data0),
83 BSDF_CLOSURE_CLASS_END(Refraction, refraction)
84
85 BSDF_CLOSURE_CLASS_BEGIN(WestinBackscatter, westin_backscatter, westin_backscatter, LABEL_GLOSSY)
86         CLOSURE_FLOAT3_PARAM(WestinBackscatterClosure, sc.N),
87         CLOSURE_FLOAT_PARAM(WestinBackscatterClosure, sc.data0),
88 BSDF_CLOSURE_CLASS_END(WestinBackscatter, westin_backscatter)
89
90 BSDF_CLOSURE_CLASS_BEGIN(WestinSheen, westin_sheen, westin_sheen, LABEL_DIFFUSE)
91         CLOSURE_FLOAT3_PARAM(WestinSheenClosure, sc.N),
92         CLOSURE_FLOAT_PARAM(WestinSheenClosure, sc.data0),
93 BSDF_CLOSURE_CLASS_END(WestinSheen, westin_sheen)
94
95 BSDF_CLOSURE_CLASS_BEGIN(Transparent, transparent, transparent, LABEL_SINGULAR)
96 BSDF_CLOSURE_CLASS_END(Transparent, transparent)
97
98 BSDF_CLOSURE_CLASS_BEGIN(AshikhminVelvet, ashikhmin_velvet, ashikhmin_velvet, LABEL_DIFFUSE)
99         CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, sc.N),
100         CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, sc.data0),
101 BSDF_CLOSURE_CLASS_END(AshikhminVelvet, ashikhmin_velvet)
102
103 BSDF_CLOSURE_CLASS_BEGIN(Ward, ward, ward, LABEL_GLOSSY)
104         CLOSURE_FLOAT3_PARAM(WardClosure, sc.N),
105         CLOSURE_FLOAT3_PARAM(WardClosure, sc.T),
106         CLOSURE_FLOAT_PARAM(WardClosure, sc.data0),
107         CLOSURE_FLOAT_PARAM(WardClosure, sc.data1),
108 BSDF_CLOSURE_CLASS_END(Ward, ward)
109
110 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, microfacet_ggx, LABEL_GLOSSY)
111         CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, sc.N),
112         CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, sc.data0),
113 BSDF_CLOSURE_CLASS_END(MicrofacetGGX, microfacet_ggx)
114
115 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, microfacet_beckmann, LABEL_GLOSSY)
116         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, sc.N),
117         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, sc.data0),
118 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmann, microfacet_beckmann)
119
120 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, microfacet_ggx, LABEL_GLOSSY)
121         CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, sc.N),
122         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data0),
123         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data1),
124 BSDF_CLOSURE_CLASS_END(MicrofacetGGXRefraction, microfacet_ggx_refraction)
125
126 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, microfacet_beckmann, LABEL_GLOSSY)
127         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, sc.N),
128         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data0),
129         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data1),
130 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction)
131
132 /* Registration */
133
134 static void generic_closure_setup(OSL::RendererServices *, int id, void *data)
135 {
136         assert(data);
137         OSL::ClosurePrimitive *prim = (OSL::ClosurePrimitive *)data;
138         prim->setup();
139 }
140
141 static bool generic_closure_compare(int id, const void *dataA, const void *dataB)
142 {
143         assert(dataA && dataB);
144
145         OSL::ClosurePrimitive *primA = (OSL::ClosurePrimitive *)dataA;
146         OSL::ClosurePrimitive *primB = (OSL::ClosurePrimitive *)dataB;
147         return primA->mergeable(primB);
148 }
149
150 static void register_closure(OSL::ShadingSystem *ss, const char *name, int id, OSL::ClosureParam *params, OSL::PrepareClosureFunc prepare)
151 {
152         ss->register_closure(name, id, params, prepare, generic_closure_setup, generic_closure_compare);
153 }
154
155 void OSLShader::register_closures(OSLShadingSystem *ss_)
156 {
157         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)ss_;
158         int id = 0;
159
160         register_closure(ss, "diffuse", id++,
161                 bsdf_diffuse_params(), bsdf_diffuse_prepare);
162         register_closure(ss, "oren_nayar", id++,
163                 bsdf_oren_nayar_params(), bsdf_oren_nayar_prepare);
164         register_closure(ss, "translucent", id++,
165                 bsdf_translucent_params(), bsdf_translucent_prepare);
166         register_closure(ss, "reflection", id++,
167                 bsdf_reflection_params(), bsdf_reflection_prepare);
168         register_closure(ss, "refraction", id++,
169                 bsdf_refraction_params(), bsdf_refraction_prepare);
170         register_closure(ss, "transparent", id++,
171                 bsdf_transparent_params(), bsdf_transparent_prepare);
172         register_closure(ss, "microfacet_ggx", id++,
173                 bsdf_microfacet_ggx_params(), bsdf_microfacet_ggx_prepare);
174         register_closure(ss, "microfacet_ggx_refraction", id++,
175                 bsdf_microfacet_ggx_refraction_params(), bsdf_microfacet_ggx_refraction_prepare);
176         register_closure(ss, "microfacet_beckmann", id++,
177                 bsdf_microfacet_beckmann_params(), bsdf_microfacet_beckmann_prepare);
178         register_closure(ss, "microfacet_beckmann_refraction", id++,
179                 bsdf_microfacet_beckmann_refraction_params(), bsdf_microfacet_beckmann_refraction_prepare);
180         register_closure(ss, "ward", id++,
181                 bsdf_ward_params(), bsdf_ward_prepare);
182         register_closure(ss, "ashikhmin_velvet", id++,
183                 bsdf_ashikhmin_velvet_params(), bsdf_ashikhmin_velvet_prepare);
184         register_closure(ss, "emission", id++,
185                 closure_emission_params(), closure_emission_prepare);
186         register_closure(ss, "background", id++,
187                 closure_background_params(), closure_background_prepare);
188         register_closure(ss, "holdout", id++,
189                 closure_holdout_params(), closure_holdout_prepare);
190         register_closure(ss, "ambient_occlusion", id++,
191                 closure_ambient_occlusion_params(), closure_ambient_occlusion_prepare);
192         register_closure(ss, "diffuse_ramp", id++,
193                 closure_bsdf_diffuse_ramp_params(), closure_bsdf_diffuse_ramp_prepare);
194         register_closure(ss, "phong_ramp", id++,
195                 closure_bsdf_phong_ramp_params(), closure_bsdf_phong_ramp_prepare);
196         register_closure(ss, "diffuse_toon", id++,
197                 closure_bsdf_diffuse_toon_params(), closure_bsdf_diffuse_toon_prepare);
198         register_closure(ss, "specular_toon", id++,
199                 closure_bsdf_specular_toon_params(), closure_bsdf_specular_toon_prepare);
200         register_closure(ss, "westin_backscatter", id++,
201                 closure_westin_backscatter_params(), closure_westin_backscatter_prepare);
202         register_closure(ss, "westin_sheen", id++,
203                 closure_westin_sheen_params(), closure_westin_sheen_prepare);
204         register_closure(ss, "bssrdf_cubic", id++,
205                 closure_bssrdf_params(), closure_bssrdf_prepare);
206 }
207
208 CCL_NAMESPACE_END
209