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