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