Cycles OSL minor optimizations: recycle shading context, don't do memory
[blender.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 <OSL/oslclosure.h>
37 #include <OSL/oslexec.h>
38 #include <OSL/genclosure.h>
39
40 #include "kernel_types.h"
41
42 #include "util_types.h"
43
44 CCL_NAMESPACE_BEGIN
45
46 OSL::ClosureParam *closure_emission_params();
47 OSL::ClosureParam *closure_background_params();
48 OSL::ClosureParam *closure_holdout_params();
49 OSL::ClosureParam *closure_ambient_occlusion_params();
50 OSL::ClosureParam *closure_bsdf_phong_ramp_params();
51 OSL::ClosureParam *closure_bsdf_diffuse_ramp_params();
52
53 void closure_emission_prepare(OSL::RendererServices *, int id, void *data);
54 void closure_background_prepare(OSL::RendererServices *, int id, void *data);
55 void closure_holdout_prepare(OSL::RendererServices *, int id, void *data);
56 void closure_ambient_occlusion_prepare(OSL::RendererServices *, int id, void *data);
57 void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
58 void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
59
60 enum {
61         AmbientOcclusion = 100
62 };
63
64 #define CLOSURE_PREPARE(name, classname)          \
65 void name(RendererServices *, int id, void *data) \
66 {                                                 \
67         memset(data, 0, sizeof(classname));           \
68         new (data) classname();                       \
69 }
70
71 #define CLOSURE_PREPARE_STATIC(name, classname) static CLOSURE_PREPARE(name, classname)
72
73 #define CLOSURE_FLOAT3_PARAM(st, fld) \
74     { TypeDesc::TypeVector, reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) }
75
76 #define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
77 #define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
78 #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
79
80 /* BSDF */
81
82 class CBSDFClosure : public OSL::ClosurePrimitive {
83 public:
84         ShaderClosure sc;
85
86         CBSDFClosure(int scattering) : OSL::ClosurePrimitive(BSDF),
87           m_scattering_label(scattering), m_shaderdata_flag(0) { }
88         ~CBSDFClosure() { }
89
90         int scattering() const { return m_scattering_label; }
91         int shaderdata_flag() const { return m_shaderdata_flag; }
92
93         virtual void blur(float roughness) = 0;
94         virtual float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0;
95         virtual float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0;
96
97         virtual int sample(const float3 &Ng,
98                            const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy,
99                            float randu, float randv,
100                            float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy,
101                            float &pdf, float3 &eval) const = 0;
102
103 protected:
104         int m_scattering_label;
105         int m_shaderdata_flag;
106 };
107
108 #define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, svmlower, TYPE) \
109 \
110 class Upper##Closure : public CBSDFClosure { \
111 public: \
112         Upper##Closure() : CBSDFClosure(TYPE) {} \
113         size_t memsize() const { return sizeof(*this); } \
114         const char *name() const { return #lower; } \
115 \
116         void setup() \
117         { \
118                 sc.prim = NULL; \
119                 m_shaderdata_flag = bsdf_##lower##_setup(&sc); \
120         } \
121 \
122         bool mergeable(const ClosurePrimitive *other) const \
123         { \
124                 return false; \
125         } \
126         \
127         void blur(float roughness) \
128         { \
129                 bsdf_##svmlower##_blur(&sc, roughness); \
130         } \
131 \
132         void print_on(std::ostream &out) const \
133         { \
134                 out << name() << " ((" << sc.N[0] << ", " << sc.N[1] << ", " << sc.N[2] << "))"; \
135         } \
136 \
137         float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
138         { \
139                 return bsdf_##svmlower##_eval_reflect(&sc, omega_out, omega_in, &pdf); \
140         } \
141 \
142         float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
143         { \
144                 return bsdf_##svmlower##_eval_transmit(&sc, omega_out, omega_in, &pdf); \
145         } \
146 \
147         int sample(const float3 &Ng, \
148                    const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, \
149                    float randu, float randv, \
150                    float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, \
151                    float &pdf, float3 &eval) const \
152         { \
153                 return bsdf_##svmlower##_sample(&sc, Ng, omega_out, domega_out_dx, domega_out_dy, \
154                         randu, randv, &eval, &omega_in, &domega_in_dx, &domega_in_dy, &pdf); \
155         } \
156 }; \
157 \
158 static ClosureParam *bsdf_##lower##_params() \
159 { \
160         static ClosureParam params[] = {
161
162 /* parameters */
163
164 #define BSDF_CLOSURE_CLASS_END(Upper, lower) \
165                 CLOSURE_STRING_KEYPARAM("label"), \
166             CLOSURE_FINISH_PARAM(Upper##Closure) \
167         }; \
168         return params; \
169 } \
170 \
171 CLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
172
173 CCL_NAMESPACE_END
174
175 #endif /* __OSL_CLOSURES_H__ */
176