97bd1b1ac928bf74e29837651a33b491236becc2
[blender-staging.git] / intern / cycles / kernel / osl / osl_closures.h
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 #ifndef __OSL_CLOSURES_H__
34 #define __OSL_CLOSURES_H__
35
36 #include "util_types.h"
37 #include "kernel_types.h"
38
39 #include <OSL/oslclosure.h>
40 #include <OSL/oslexec.h>
41 #include <OSL/genclosure.h>
42
43 CCL_NAMESPACE_BEGIN
44
45 OSL::ClosureParam *closure_emission_params();
46 OSL::ClosureParam *closure_background_params();
47 OSL::ClosureParam *closure_holdout_params();
48 OSL::ClosureParam *closure_ambient_occlusion_params();
49 OSL::ClosureParam *closure_bsdf_diffuse_ramp_params();
50 OSL::ClosureParam *closure_bsdf_phong_ramp_params();
51 OSL::ClosureParam *closure_bssrdf_cubic_params();
52 OSL::ClosureParam *closure_bssrdf_gaussian_params();
53 OSL::ClosureParam *closure_henyey_greenstein_volume_params();
54
55 void closure_emission_prepare(OSL::RendererServices *, int id, void *data);
56 void closure_background_prepare(OSL::RendererServices *, int id, void *data);
57 void closure_holdout_prepare(OSL::RendererServices *, int id, void *data);
58 void closure_ambient_occlusion_prepare(OSL::RendererServices *, int id, void *data);
59 void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
60 void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
61 void closure_bssrdf_cubic_prepare(OSL::RendererServices *, int id, void *data);
62 void closure_bssrdf_gaussian_prepare(OSL::RendererServices *, int id, void *data);
63 void closure_henyey_greenstein_volume_prepare(OSL::RendererServices *, int id, void *data);
64
65 #define CCLOSURE_PREPARE(name, classname)          \
66 void name(RendererServices *, int id, void *data) \
67 {                                                 \
68         memset(data, 0, sizeof(classname));           \
69         new (data) classname();                       \
70 }
71
72 #define CCLOSURE_PREPARE_STATIC(name, classname) static CCLOSURE_PREPARE(name, classname)
73
74 #define CLOSURE_FLOAT3_PARAM(st, fld) \
75         { TypeDesc::TypeVector, reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) }
76
77 #define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
78 #define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
79 #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
80
81 #if OSL_LIBRARY_VERSION_CODE < 10700
82 #  undef CLOSURE_STRING_KEYPARAM
83 #  define CLOSURE_STRING_KEYPARAM(st, fld, key) { TypeDesc::TypeString, 0, key, 0 }
84 #endif
85
86 /* Closure */
87
88 class CClosurePrimitive {
89 public:
90         enum Category {
91                 BSDF,             ///< Reflective and/or transmissive surface
92                 BSSRDF,           ///< Sub-surface light transfer
93                 Emissive,         ///< Light emission
94                 Background,       ///< Background emission
95                 Volume,           ///< Volume scattering
96                 Holdout,          ///< Holdout from alpha
97                 AmbientOcclusion, ///< Ambient occlusion
98         };
99
100         CClosurePrimitive (Category category_) : category (category_) {}
101         virtual ~CClosurePrimitive() {}
102         virtual void setup() {}
103
104         Category category;
105
106 #if OSL_LIBRARY_VERSION_CODE >= 10700
107         OSL::ustring label;
108 #endif
109 };
110
111 /* BSDF */
112
113 class CBSDFClosure : public CClosurePrimitive {
114 public:
115         ShaderClosure sc;
116
117         CBSDFClosure(int scattering) : CClosurePrimitive(BSDF),
118           m_scattering_label(scattering), m_shaderdata_flag(0)
119         {}
120
121         int scattering() const { return m_scattering_label; }
122         int shaderdata_flag() const { return m_shaderdata_flag; }
123
124         virtual void blur(float roughness) = 0;
125         virtual float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0;
126         virtual float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0;
127
128         virtual int sample(const float3 &Ng,
129                            const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy,
130                            float randu, float randv,
131                            float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy,
132                            float &pdf, float3 &eval) const = 0;
133
134 protected:
135         int m_scattering_label;
136         int m_shaderdata_flag;
137 };
138
139 #define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, svmlower, TYPE) \
140 \
141 class Upper##Closure : public CBSDFClosure { \
142 public: \
143         Upper##Closure() : CBSDFClosure(TYPE) \
144         { \
145         } \
146 \
147         void setup() \
148         { \
149                 sc.prim = NULL; \
150                 m_shaderdata_flag = bsdf_##lower##_setup(&sc); \
151         } \
152 \
153         void blur(float roughness) \
154         { \
155         } \
156 \
157         float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
158         { \
159                 pdf = 0.0f; \
160                 return make_float3(0.0f, 0.0f, 0.0f); \
161         } \
162 \
163         float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
164         { \
165                 pdf = 0.0f; \
166                 return make_float3(0.0f, 0.0f, 0.0f); \
167         } \
168 \
169         int sample(const float3 &Ng, \
170                    const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, \
171                    float randu, float randv, \
172                    float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, \
173                    float &pdf, float3 &eval) const \
174         { \
175                 pdf = 0; \
176                 return LABEL_NONE; \
177         } \
178 }; \
179 \
180 static ClosureParam *bsdf_##lower##_params() \
181 { \
182         static ClosureParam params[] = {
183
184 /* parameters */
185
186 #define BSDF_CLOSURE_CLASS_END(Upper, lower) \
187                 CLOSURE_STRING_KEYPARAM(Upper##Closure, label, "label"), \
188             CLOSURE_FINISH_PARAM(Upper##Closure) \
189         }; \
190         return params; \
191 } \
192 \
193 CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
194
195
196 /* Volume */
197
198 class CVolumeClosure : public CClosurePrimitive {
199 public:
200         ShaderClosure sc;
201
202         CVolumeClosure(int scattering) : CClosurePrimitive(Volume),
203           m_scattering_label(scattering), m_shaderdata_flag(0)
204         {}
205         ~CVolumeClosure() { }
206
207         int scattering() const { return m_scattering_label; }
208         int shaderdata_flag() const { return m_shaderdata_flag; }
209
210 protected:
211         int m_scattering_label;
212         int m_shaderdata_flag;
213 };
214
215 #define VOLUME_CLOSURE_CLASS_BEGIN(Upper, lower, TYPE) \
216 \
217 class Upper##Closure : public CVolumeClosure { \
218 public: \
219         Upper##Closure() : CVolumeClosure(TYPE) {} \
220 \
221         void setup() \
222         { \
223                 sc.prim = NULL; \
224                 m_shaderdata_flag = volume_##lower##_setup(&sc); \
225         } \
226 }; \
227 \
228 static ClosureParam *volume_##lower##_params() \
229 { \
230         static ClosureParam params[] = {
231
232 /* parameters */
233
234 #define VOLUME_CLOSURE_CLASS_END(Upper, lower) \
235                 CLOSURE_STRING_KEYPARAM(Upper##Closure, label, "label"), \
236             CLOSURE_FINISH_PARAM(Upper##Closure) \
237         }; \
238         return params; \
239 } \
240 \
241 CCLOSURE_PREPARE_STATIC(volume_##lower##_prepare, Upper##Closure)
242
243 CCL_NAMESPACE_END
244
245 #endif /* __OSL_CLOSURES_H__ */
246