Merge branch 'master' into blender2.8
[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 "util/util_types.h"
37 #include "kernel/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_bsdf_transparent_params();
52 OSL::ClosureParam *closure_bssrdf_params();
53 OSL::ClosureParam *closure_absorption_params();
54 OSL::ClosureParam *closure_henyey_greenstein_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_ambient_occlusion_prepare(OSL::RendererServices *, int id, void *data);
69 void closure_bsdf_diffuse_ramp_prepare(OSL::RendererServices *, int id, void *data);
70 void closure_bsdf_phong_ramp_prepare(OSL::RendererServices *, int id, void *data);
71 void closure_bsdf_transparent_prepare(OSL::RendererServices *, int id, void *data);
72 void closure_bssrdf_prepare(OSL::RendererServices *, int id, void *data);
73 void closure_absorption_prepare(OSL::RendererServices *, int id, void *data);
74 void closure_henyey_greenstein_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 *, int id, void *data);
80 void closure_bsdf_microfacet_multi_ggx_fresnel_prepare(OSL::RendererServices *, int id, void *data);
81 void closure_bsdf_microfacet_multi_ggx_glass_fresnel_prepare(OSL::RendererServices *, int id, void *data);
82 void closure_bsdf_microfacet_multi_ggx_aniso_fresnel_prepare(OSL::RendererServices *, int id, void *data);
83 void closure_bsdf_principled_clearcoat_prepare(OSL::RendererServices *, int id, void *data);
84
85 #define CCLOSURE_PREPARE(name, classname)          \
86 void name(RendererServices *, int id, void *data) \
87 {                                                 \
88         memset(data, 0, sizeof(classname));           \
89         new (data) classname();                       \
90 }
91
92 #define CCLOSURE_PREPARE_STATIC(name, classname) static CCLOSURE_PREPARE(name, classname)
93
94 #define CLOSURE_FLOAT3_PARAM(st, fld) \
95         { TypeDesc::TypeVector, (int)reckless_offsetof(st, fld), NULL, sizeof(OSL::Vec3) }
96
97 #define TO_VEC3(v) OSL::Vec3(v.x, v.y, v.z)
98 #define TO_COLOR3(v) OSL::Color3(v.x, v.y, v.z)
99 #define TO_FLOAT3(v) make_float3(v[0], v[1], v[2])
100
101 /* Closure */
102
103 class CClosurePrimitive {
104 public:
105         virtual void setup(ShaderData *sd, int path_flag, float3 weight) = 0;
106
107         OSL::ustring label;
108 };
109
110 /* BSDF */
111
112 class CBSDFClosure : public CClosurePrimitive {
113 public:
114         bool skip(const ShaderData *sd, int path_flag, int scattering);
115 };
116
117 #define BSDF_CLOSURE_CLASS_BEGIN(Upper, lower, structname, TYPE) \
118 \
119 class Upper##Closure : public CBSDFClosure { \
120 public: \
121         structname params; \
122         float3 unused; \
123 \
124         void setup(ShaderData *sd, int path_flag, float3 weight) \
125         { \
126             if(!skip(sd, path_flag, TYPE)) { \
127                         structname *bsdf = (structname*)bsdf_alloc_osl(sd, sizeof(structname), weight, &params); \
128                         sd->flag |= (bsdf) ? bsdf_##lower##_setup(bsdf) : 0; \
129                 } \
130         } \
131 }; \
132 \
133 static ClosureParam *bsdf_##lower##_params() \
134 { \
135         static ClosureParam params[] = {
136
137 /* parameters */
138
139 #define BSDF_CLOSURE_CLASS_END(Upper, lower) \
140                 CLOSURE_STRING_KEYPARAM(Upper##Closure, label, "label"), \
141                 CLOSURE_FINISH_PARAM(Upper##Closure) \
142         }; \
143         return params; \
144 } \
145 \
146 CCLOSURE_PREPARE_STATIC(bsdf_##lower##_prepare, Upper##Closure)
147
148 CCL_NAMESPACE_END
149
150 #endif /* __OSL_CLOSURES_H__ */
151