340e44925840baaedd1c755b8bd628c33c20fcc9
[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 "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 #include "closure/bsdf_toon.h"
57 #include "closure/bsdf_hair.h"
58
59 CCL_NAMESPACE_BEGIN
60
61 using namespace OSL;
62
63 /* BSDF class definitions */
64
65 BSDF_CLOSURE_CLASS_BEGIN(Diffuse, diffuse, diffuse, LABEL_DIFFUSE)
66         CLOSURE_FLOAT3_PARAM(DiffuseClosure, sc.N),
67 BSDF_CLOSURE_CLASS_END(Diffuse, diffuse)
68
69 BSDF_CLOSURE_CLASS_BEGIN(Translucent, translucent, translucent, LABEL_DIFFUSE)
70         CLOSURE_FLOAT3_PARAM(TranslucentClosure, sc.N),
71 BSDF_CLOSURE_CLASS_END(Translucent, translucent)
72
73 BSDF_CLOSURE_CLASS_BEGIN(OrenNayar, oren_nayar, oren_nayar, LABEL_DIFFUSE)
74         CLOSURE_FLOAT3_PARAM(OrenNayarClosure, sc.N),
75         CLOSURE_FLOAT_PARAM(OrenNayarClosure, sc.data0),
76 BSDF_CLOSURE_CLASS_END(OrenNayar, oren_nayar)
77
78 BSDF_CLOSURE_CLASS_BEGIN(Reflection, reflection, reflection, LABEL_SINGULAR)
79         CLOSURE_FLOAT3_PARAM(ReflectionClosure, sc.N),
80 BSDF_CLOSURE_CLASS_END(Reflection, reflection)
81
82 BSDF_CLOSURE_CLASS_BEGIN(Refraction, refraction, refraction, LABEL_SINGULAR)
83         CLOSURE_FLOAT3_PARAM(RefractionClosure, sc.N),
84         CLOSURE_FLOAT_PARAM(RefractionClosure, sc.data0),
85 BSDF_CLOSURE_CLASS_END(Refraction, refraction)
86
87 BSDF_CLOSURE_CLASS_BEGIN(WestinBackscatter, westin_backscatter, westin_backscatter, LABEL_GLOSSY)
88         CLOSURE_FLOAT3_PARAM(WestinBackscatterClosure, sc.N),
89         CLOSURE_FLOAT_PARAM(WestinBackscatterClosure, sc.data0),
90 BSDF_CLOSURE_CLASS_END(WestinBackscatter, westin_backscatter)
91
92 BSDF_CLOSURE_CLASS_BEGIN(WestinSheen, westin_sheen, westin_sheen, LABEL_DIFFUSE)
93         CLOSURE_FLOAT3_PARAM(WestinSheenClosure, sc.N),
94         CLOSURE_FLOAT_PARAM(WestinSheenClosure, sc.data0),
95 BSDF_CLOSURE_CLASS_END(WestinSheen, westin_sheen)
96
97 BSDF_CLOSURE_CLASS_BEGIN(Transparent, transparent, transparent, LABEL_SINGULAR)
98 BSDF_CLOSURE_CLASS_END(Transparent, transparent)
99
100 BSDF_CLOSURE_CLASS_BEGIN(AshikhminVelvet, ashikhmin_velvet, ashikhmin_velvet, LABEL_DIFFUSE)
101         CLOSURE_FLOAT3_PARAM(AshikhminVelvetClosure, sc.N),
102         CLOSURE_FLOAT_PARAM(AshikhminVelvetClosure, sc.data0),
103 BSDF_CLOSURE_CLASS_END(AshikhminVelvet, ashikhmin_velvet)
104
105 BSDF_CLOSURE_CLASS_BEGIN(Ward, ward, ward, LABEL_GLOSSY)
106         CLOSURE_FLOAT3_PARAM(WardClosure, sc.N),
107         CLOSURE_FLOAT3_PARAM(WardClosure, sc.T),
108         CLOSURE_FLOAT_PARAM(WardClosure, sc.data0),
109         CLOSURE_FLOAT_PARAM(WardClosure, sc.data1),
110 BSDF_CLOSURE_CLASS_END(Ward, ward)
111
112 BSDF_CLOSURE_CLASS_BEGIN(DiffuseToon, diffuse_toon, diffuse_toon, LABEL_DIFFUSE)
113         CLOSURE_FLOAT3_PARAM(DiffuseToonClosure, sc.N),
114         CLOSURE_FLOAT_PARAM(DiffuseToonClosure, sc.data0),
115         CLOSURE_FLOAT_PARAM(DiffuseToonClosure, sc.data1),
116 BSDF_CLOSURE_CLASS_END(DiffuseToon, diffuse_toon)
117
118 BSDF_CLOSURE_CLASS_BEGIN(GlossyToon, glossy_toon, glossy_toon, LABEL_GLOSSY)
119         CLOSURE_FLOAT3_PARAM(GlossyToonClosure, sc.N),
120         CLOSURE_FLOAT_PARAM(GlossyToonClosure, sc.data0),
121         CLOSURE_FLOAT_PARAM(GlossyToonClosure, sc.data1),
122 BSDF_CLOSURE_CLASS_END(GlossyToon, glossy_toon)
123
124 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGX, microfacet_ggx, microfacet_ggx, LABEL_GLOSSY)
125         CLOSURE_FLOAT3_PARAM(MicrofacetGGXClosure, sc.N),
126         CLOSURE_FLOAT_PARAM(MicrofacetGGXClosure, sc.data0),
127 BSDF_CLOSURE_CLASS_END(MicrofacetGGX, microfacet_ggx)
128
129 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmann, microfacet_beckmann, microfacet_beckmann, LABEL_GLOSSY)
130         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannClosure, sc.N),
131         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannClosure, sc.data0),
132 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmann, microfacet_beckmann)
133
134 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetGGXRefraction, microfacet_ggx_refraction, microfacet_ggx, LABEL_GLOSSY)
135         CLOSURE_FLOAT3_PARAM(MicrofacetGGXRefractionClosure, sc.N),
136         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data0),
137         CLOSURE_FLOAT_PARAM(MicrofacetGGXRefractionClosure, sc.data1),
138 BSDF_CLOSURE_CLASS_END(MicrofacetGGXRefraction, microfacet_ggx_refraction)
139
140 BSDF_CLOSURE_CLASS_BEGIN(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction, microfacet_beckmann, LABEL_GLOSSY)
141         CLOSURE_FLOAT3_PARAM(MicrofacetBeckmannRefractionClosure, sc.N),
142         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data0),
143         CLOSURE_FLOAT_PARAM(MicrofacetBeckmannRefractionClosure, sc.data1),
144 BSDF_CLOSURE_CLASS_END(MicrofacetBeckmannRefraction, microfacet_beckmann_refraction)
145
146 BSDF_CLOSURE_CLASS_BEGIN(HairReflection, hair_reflection, hair_reflection, LABEL_GLOSSY)
147         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.N),
148         CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data0),
149         CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data1),
150 #ifdef __HAIR__
151         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.T),
152         CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.offset),
153 #else
154         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.N),
155         CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data1),
156 #endif
157 BSDF_CLOSURE_CLASS_END(HairReflection, hair_reflection)
158
159 BSDF_CLOSURE_CLASS_BEGIN(HairTransmission, hair_transmission, hair_transmission, LABEL_GLOSSY)
160         CLOSURE_FLOAT3_PARAM(HairTransmissionClosure, sc.N),
161         CLOSURE_FLOAT_PARAM(HairTransmissionClosure, sc.data0),
162         CLOSURE_FLOAT_PARAM(HairTransmissionClosure, sc.data1),
163 #ifdef __HAIR__
164         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.T),
165         CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.offset),
166 #else
167         CLOSURE_FLOAT3_PARAM(HairReflectionClosure, sc.N),
168         CLOSURE_FLOAT_PARAM(HairReflectionClosure, sc.data1),
169 #endif
170 BSDF_CLOSURE_CLASS_END(HairTransmission, hair_transmission)
171
172 /* Registration */
173
174 static void register_closure(OSL::ShadingSystem *ss, const char *name, int id, OSL::ClosureParam *params, OSL::PrepareClosureFunc prepare)
175 {
176         /* optimization: it's possible to not use a prepare function at all and
177          * only initialize the actual class when accessing the closure component
178          * data, but then we need to map the id to the class somehow */
179 #ifdef CLOSURE_PREPARE
180         ss->register_closure(name, id, params, prepare, NULL, NULL);
181 #else
182         ss->register_closure(name, id, params, prepare, NULL);
183 #endif
184 }
185
186 void OSLShader::register_closures(OSLShadingSystem *ss_)
187 {
188         OSL::ShadingSystem *ss = (OSL::ShadingSystem*)ss_;
189         int id = 0;
190
191         register_closure(ss, "diffuse", id++,
192                 bsdf_diffuse_params(), bsdf_diffuse_prepare);
193         register_closure(ss, "oren_nayar", id++,
194                 bsdf_oren_nayar_params(), bsdf_oren_nayar_prepare);
195         register_closure(ss, "translucent", id++,
196                 bsdf_translucent_params(), bsdf_translucent_prepare);
197         register_closure(ss, "reflection", id++,
198                 bsdf_reflection_params(), bsdf_reflection_prepare);
199         register_closure(ss, "refraction", id++,
200                 bsdf_refraction_params(), bsdf_refraction_prepare);
201         register_closure(ss, "transparent", id++,
202                 bsdf_transparent_params(), bsdf_transparent_prepare);
203         register_closure(ss, "microfacet_ggx", id++,
204                 bsdf_microfacet_ggx_params(), bsdf_microfacet_ggx_prepare);
205         register_closure(ss, "microfacet_ggx_refraction", id++,
206                 bsdf_microfacet_ggx_refraction_params(), bsdf_microfacet_ggx_refraction_prepare);
207         register_closure(ss, "microfacet_beckmann", id++,
208                 bsdf_microfacet_beckmann_params(), bsdf_microfacet_beckmann_prepare);
209         register_closure(ss, "microfacet_beckmann_refraction", id++,
210                 bsdf_microfacet_beckmann_refraction_params(), bsdf_microfacet_beckmann_refraction_prepare);
211         register_closure(ss, "ward", id++,
212                 bsdf_ward_params(), bsdf_ward_prepare);
213         register_closure(ss, "ashikhmin_velvet", id++,
214                 bsdf_ashikhmin_velvet_params(), bsdf_ashikhmin_velvet_prepare);
215         register_closure(ss, "diffuse_toon", id++,
216                 bsdf_diffuse_toon_params(), bsdf_diffuse_toon_prepare);
217         register_closure(ss, "glossy_toon", id++,
218                 bsdf_glossy_toon_params(), bsdf_glossy_toon_prepare);
219         register_closure(ss, "specular_toon", id++,
220                 bsdf_glossy_toon_params(), bsdf_glossy_toon_prepare);
221         register_closure(ss, "westin_backscatter", id++,
222                 bsdf_westin_backscatter_params(), bsdf_westin_backscatter_prepare);
223         register_closure(ss, "westin_sheen", id++,
224                 bsdf_westin_sheen_params(), bsdf_westin_sheen_prepare);
225
226         register_closure(ss, "emission", id++,
227                 closure_emission_params(), closure_emission_prepare);
228         register_closure(ss, "background", id++,
229                 closure_background_params(), closure_background_prepare);
230         register_closure(ss, "holdout", id++,
231                 closure_holdout_params(), closure_holdout_prepare);
232         register_closure(ss, "ambient_occlusion", id++,
233                 closure_ambient_occlusion_params(), closure_ambient_occlusion_prepare);
234         register_closure(ss, "diffuse_ramp", id++,
235                 closure_bsdf_diffuse_ramp_params(), closure_bsdf_diffuse_ramp_prepare);
236         register_closure(ss, "phong_ramp", id++,
237                 closure_bsdf_phong_ramp_params(), closure_bsdf_phong_ramp_prepare);
238         register_closure(ss, "bssrdf_cubic", id++,
239                 closure_bssrdf_cubic_params(), closure_bssrdf_cubic_prepare);
240         register_closure(ss, "bssrdf_gaussian", id++,
241                 closure_bssrdf_gaussian_params(), closure_bssrdf_gaussian_prepare);
242         register_closure(ss, "bssrdf_cubic", id++,
243                 closure_bssrdf_cubic_extended_params(), closure_bssrdf_cubic_prepare);
244         register_closure(ss, "bssrdf_gaussian", id++,
245                 closure_bssrdf_gaussian_extended_params(), closure_bssrdf_gaussian_prepare);
246
247         register_closure(ss, "hair_reflection", id++,
248                 bsdf_hair_reflection_params(), bsdf_hair_reflection_prepare);
249         register_closure(ss, "hair_transmission", id++,
250                 bsdf_hair_transmission_params(), bsdf_hair_transmission_prepare);
251 }
252
253 CCL_NAMESPACE_END
254