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 "kernel/kernel_types.h"
37 #include "util/util_types.h"
38 
39 #include <OSL/genclosure.h>
40 #include <OSL/oslclosure.h>
41 #include <OSL/oslexec.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_bsdf_diffuse_ramp_params();
49 OSL::ClosureParam *closure_bsdf_phong_ramp_params();
50 OSL::ClosureParam *closure_bsdf_transparent_params();
51 OSL::ClosureParam *closure_bssrdf_params();
52 OSL::ClosureParam *closure_absorption_params();
53 OSL::ClosureParam *closure_henyey_greenstein_params();
54 OSL::ClosureParam *closure_bsdf_microfacet_params();
55 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_params();
56 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_params();
57 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_params();
58 OSL::ClosureParam *closure_bsdf_microfacet_ggx_fresnel_params();
59 OSL::ClosureParam *closure_bsdf_microfacet_ggx_aniso_fresnel_params();
60 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_fresnel_params();
61 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_glass_fresnel_params();
62 OSL::ClosureParam *closure_bsdf_microfacet_multi_ggx_aniso_fresnel_params();
63 OSL::ClosureParam *closure_bsdf_principled_clearcoat_params();
64 
65 void closure_emission_prepare(OSL::RendererServices *, int id, void *data);
66 void closure_background_prepare(OSL::RendererServices *, int id, void *data);
67 void closure_holdout_prepare(OSL::RendererServices *, int id, void *data);
68 void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
69 void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
70 void closure_bsdf_transparent_prepare(OSL::RendererServices *, int id, void *data);
71 void closure_bssrdf_prepare(OSL::RendererServices *, int id, void *data);
72 void closure_absorption_prepare(OSL::RendererServices *, int id, void *data);
73 void closure_henyey_greenstein_prepare(OSL::RendererServices *, int id, void *data);
74 void closure_bsdf_microfacet_prepare(OSL::RendererServices *, int id, void *data);
75 void closure_bsdf_microfacet_multi_ggx_prepare(OSL::RendererServices *, int id, void *data);
76 void closure_bsdf_microfacet_multi_ggx_glass_prepare(OSL::RendererServices *, int id, void *data);
77 void closure_bsdf_microfacet_multi_ggx_aniso_prepare(OSL::RendererServices *, int id, void *data);
78 void closure_bsdf_microfacet_ggx_fresnel_prepare(OSL::RendererServices *, int id, void *data);
79 void closure_bsdf_microfacet_ggx_aniso_fresnel_prepare(OSL::RendererServices *,
80                                                        int id,
81                                                        void *data);
82 void closure_bsdf_microfacet_multi_ggx_fresnel_prepare(OSL::RendererServices *,
83                                                        int id,
84                                                        void *data);
85 void closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare(OSL::RendererServices *,
86                                                              int id,
87                                                              void *data);
88 void closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare(OSL::RendererServices *,
89                                                              int id,
90                                                              void *data);
91 void closure_bsdf_principled_clearcoat_prepare(OSL::RendererServices *, int id, void *data);
92 void closure_bsdf_principled_hair_prepare(OSL::RendererServices *, int id, void *data);
93 
94 #define CCLOSURE_PREPARE(name, classname) \
95   void name(RendererServices *, int id, void *data) \
96   { \
97     memset(data, 0, sizeof(classname)); \
98     new (data) classname(); \
99   }
100 
101 #define CCLOSURE_PREPARE_STATIC(name, classname) static CCLOSURE_PREPARE(name, classname)
102 
103 #define CLOSURE_FLOAT3_PARAM(st, fld) \
104   { \
105     TypeDesc::TypeVector, (int)reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) \
106   }
107 
108 #define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
109 #define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
110 #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
111 
112 /* Closure */
113 
114 class CClosurePrimitive {
115  public:
116   virtual void setup(ShaderData *sd, int path_flag, float3 weight) = 0;
117 
118   OSL::ustring label;
119 };
120 
121 /* BSDF */
122 
123 class CBSDFClosure : public CClosurePrimitive {
124  public:
125   bool skip(const ShaderData *sd, int path_flag, int scattering);
126 };
127 
128 #define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, structname, TYPE) \
129 \
130   class Upper##Closure : public CBSDFClosure { \
131    public: \
132     structname params; \
133     float3 unused; \
134 \
135     void setup(ShaderData *sd, int path_flag, float3 weight) \
136     { \
137       if (!skip(sd, path_flag, TYPE)) { \
138         structname *bsdf = (structname *)bsdf_alloc_osl(sd, sizeof(structname), weight, &params); \
139         sd->flag |= (bsdf) ? bsdf_##lower##_setup(bsdf) : 0; \
140       } \
141     } \
142   }; \
143 \
144   static ClosureParam *bsdf_##lower##_params() \
145   { \
146     static ClosureParam params[] = {
147 
148 /* parameters */
149 
150 #define BSDF_CLOSURE_CLASS_END(Upper, lower) \
151   CLOSURE_STRING_KEYPARAM(Upper##Closure, label, "label"), CLOSURE_FINISH_PARAM(Upper##Closure) \
152   } \
153   ; \
154   return params; \
155   } \
156 \
157   CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
158 
159 CCL_NAMESPACE_END
160 
161 #endif /* __OSL_CLOSURES_H__ */
162