2 * Adapted from Open Shading Language with this license:
4 * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
7 * Modifications Copyright 2011, Blender Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
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.
33 #ifndef __OSL_CLOSURES_H__
34 #define __OSL_CLOSURES_H__
36 #include "util_types.h"
37 #include "kernel_types.h"
39 #include <OSL/oslclosure.h>
40 #include <OSL/oslexec.h>
41 #include <OSL/genclosure.h>
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_westin_backscatter_params();
52 OSL::ClosureParam *closure_westin_sheen_params();
53 OSL::ClosureParam *closure_bssrdf_cubic_params();
54 OSL::ClosureParam *closure_bssrdf_gaussian_params();
55 OSL::ClosureParam *closure_bssrdf_cubic_extended_params();
56 OSL::ClosureParam *closure_bssrdf_gaussian_extended_params();
58 void closure_emission_prepare(OSL::RendererServices *, int id, void *data);
59 void closure_background_prepare(OSL::RendererServices *, int id, void *data);
60 void closure_holdout_prepare(OSL::RendererServices *, int id, void *data);
61 void closure_ambient_occlusion_prepare(OSL::RendererServices *, int id, void *data);
62 void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
63 void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
64 void closure_westin_backscatter_prepare(OSL::RendererServices *, int id, void *data);
65 void closure_westin_sheen_prepare(OSL::RendererServices *, int id, void *data);
66 void closure_bssrdf_cubic_prepare(OSL::RendererServices *, int id, void *data);
67 void closure_bssrdf_gaussian_prepare(OSL::RendererServices *, int id, void *data);
69 #define CCLOSURE_PREPARE(name, classname) \
70 void name(RendererServices *, int id, void *data) \
72 memset(data, 0, sizeof(classname)); \
73 new (data) classname(); \
76 #define CCLOSURE_PREPARE_STATIC(name, classname) static CCLOSURE_PREPARE(name, classname)
78 #define CLOSURE_FLOAT3_PARAM(st, fld) \
79 { TypeDesc::TypeVector, reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) }
81 #define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
82 #define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
83 #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
87 class CClosurePrimitive {
90 BSDF, ///< Reflective and/or transmissive surface
91 BSSRDF, ///< Sub-surface light transfer
92 Emissive, ///< Light emission
93 Background, ///< Background emission
94 Volume, ///< Volume scattering
95 Holdout, ///< Holdout from alpha
96 AmbientOcclusion, ///< Ambient occlusion
99 CClosurePrimitive (Category category_) : category (category_) {}
100 virtual ~CClosurePrimitive() {}
101 virtual void setup() {}
108 class CBSDFClosure : public CClosurePrimitive {
112 CBSDFClosure(int scattering) : CClosurePrimitive(BSDF),
113 m_scattering_label(scattering), m_shaderdata_flag(0)
116 int scattering() const { return m_scattering_label; }
117 int shaderdata_flag() const { return m_shaderdata_flag; }
119 virtual void blur(float roughness) = 0;
120 virtual float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0;
121 virtual float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float &pdf) const = 0;
123 virtual int sample(const float3 &Ng,
124 const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy,
125 float randu, float randv,
126 float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy,
127 float &pdf, float3 &eval) const = 0;
130 int m_scattering_label;
131 int m_shaderdata_flag;
134 #define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, svmlower, TYPE) \
136 class Upper##Closure : public CBSDFClosure { \
138 Upper##Closure() : CBSDFClosure(TYPE) \
145 m_shaderdata_flag = bsdf_##lower##_setup(&sc); \
148 void blur(float roughness) \
150 bsdf_##svmlower##_blur(&sc, roughness); \
153 float3 eval_reflect(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
155 return bsdf_##svmlower##_eval_reflect(&sc, omega_out, omega_in, &pdf); \
158 float3 eval_transmit(const float3 &omega_out, const float3 &omega_in, float& pdf) const \
160 return bsdf_##svmlower##_eval_transmit(&sc, omega_out, omega_in, &pdf); \
163 int sample(const float3 &Ng, \
164 const float3 &omega_out, const float3 &domega_out_dx, const float3 &domega_out_dy, \
165 float randu, float randv, \
166 float3 &omega_in, float3 &domega_in_dx, float3 &domega_in_dy, \
167 float &pdf, float3 &eval) const \
169 return bsdf_##svmlower##_sample(&sc, Ng, omega_out, domega_out_dx, domega_out_dy, \
170 randu, randv, &eval, &omega_in, &domega_in_dx, &domega_in_dy, &pdf); \
174 static ClosureParam *bsdf_##lower##_params() \
176 static ClosureParam params[] = {
180 #define BSDF_CLOSURE_CLASS_END(Upper, lower) \
181 CLOSURE_STRING_KEYPARAM("label"), \
182 CLOSURE_FINISH_PARAM(Upper##Closure) \
187 CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
191 #endif /* __OSL_CLOSURES_H__ */