1
2 //
3 // This source file is part of appleseed.
4 // Visit https://appleseedhq.net/ for additional information and resources.
5 //
6 // This software is released under the MIT license.
7 //
8 // Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9 // Copyright (c) 2014-2018 Francois Beaune, The appleseedhq Organization
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a copy
12 // of this software and associated documentation files (the "Software"), to deal
13 // in the Software without restriction, including without limitation the rights
14 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 // copies of the Software, and to permit persons to whom the Software is
16 // furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included in
19 // all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 // THE SOFTWARE.
28 //
29
30 #pragma once
31
32 // appleseed.renderer headers.
33 #include "renderer/global/globaltypes.h"
34
35 // appleseed.foundation headers.
36 #include "foundation/image/color.h"
37 #include "foundation/math/vector.h"
38 #include "foundation/platform/types.h"
39
40 // appleseed.main headers.
41 #include "main/dllsymbol.h"
42
43 // Standard headers.
44 #include <cstddef>
45
46 // Forward declarations.
47 namespace renderer { class TextureCache; }
48 namespace renderer { class SourceInputs; }
49
50 namespace renderer
51 {
52
53 //
54 // Source base class.
55 //
56
57 class APPLESEED_DLLSYMBOL Source
58 {
59 public:
60 // Constructor.
61 explicit Source(const bool uniform);
62
63 // Destructor.
64 virtual ~Source() = 0;
65
66 // Compute a signature unique to this source.
67 virtual foundation::uint64 compute_signature() const = 0;
68
69 // Return true if the source is uniform, false if it is varying.
70 bool is_uniform() const;
71
72 struct Hints
73 {
74 // Allow treating this source as a 2D texture map with the following dimensions in pixels.
75 size_t m_width;
76 size_t m_height;
77 };
78
79 // Return hints allowing to treat this source as one of another type.
80 virtual Hints get_hints() const = 0;
81
82 // Evaluate the source at a given shading point.
83 virtual void evaluate(
84 TextureCache& texture_cache,
85 const SourceInputs& source_inputs,
86 float& scalar) const;
87 virtual void evaluate(
88 TextureCache& texture_cache,
89 const SourceInputs& source_inputs,
90 foundation::Color3f& linear_rgb) const;
91 virtual void evaluate(
92 TextureCache& texture_cache,
93 const SourceInputs& source_inputs,
94 Spectrum& spectrum) const;
95 virtual void evaluate(
96 TextureCache& texture_cache,
97 const SourceInputs& source_inputs,
98 Alpha& alpha) const;
99 virtual void evaluate(
100 TextureCache& texture_cache,
101 const SourceInputs& source_inputs,
102 foundation::Color3f& linear_rgb,
103 Alpha& alpha) const;
104 virtual void evaluate(
105 TextureCache& texture_cache,
106 const SourceInputs& source_inputs,
107 Spectrum& spectrum,
108 Alpha& alpha) const;
109
110 // Evaluate the source as a uniform source.
111 virtual void evaluate_uniform(
112 float& scalar) const;
113 virtual void evaluate_uniform(
114 foundation::Color3f& linear_rgb) const;
115 virtual void evaluate_uniform(
116 Spectrum& spectrum) const;
117 virtual void evaluate_uniform(
118 Alpha& alpha) const;
119 virtual void evaluate_uniform(
120 foundation::Color3f& linear_rgb,
121 Alpha& alpha) const;
122 virtual void evaluate_uniform(
123 Spectrum& spectrum,
124 Alpha& alpha) const;
125
126 private:
127 const bool m_uniform;
128 };
129
130
131 //
132 // Source class implementation.
133 //
134
Source(const bool uniform)135 inline Source::Source(const bool uniform)
136 : m_uniform(uniform)
137 {
138 }
139
~Source()140 inline Source::~Source()
141 {
142 }
143
is_uniform()144 inline bool Source::is_uniform() const
145 {
146 return m_uniform;
147 }
148
evaluate(TextureCache & texture_cache,const SourceInputs & source_inputs,float & scalar)149 inline void Source::evaluate(
150 TextureCache& texture_cache,
151 const SourceInputs& source_inputs,
152 float& scalar) const
153 {
154 evaluate_uniform(scalar);
155 }
156
evaluate(TextureCache & texture_cache,const SourceInputs & source_inputs,foundation::Color3f & linear_rgb)157 inline void Source::evaluate(
158 TextureCache& texture_cache,
159 const SourceInputs& source_inputs,
160 foundation::Color3f& linear_rgb) const
161 {
162 evaluate_uniform(linear_rgb);
163 }
164
evaluate(TextureCache & texture_cache,const SourceInputs & source_inputs,Spectrum & spectrum)165 inline void Source::evaluate(
166 TextureCache& texture_cache,
167 const SourceInputs& source_inputs,
168 Spectrum& spectrum) const
169 {
170 evaluate_uniform(spectrum);
171 }
172
evaluate(TextureCache & texture_cache,const SourceInputs & source_inputs,Alpha & alpha)173 inline void Source::evaluate(
174 TextureCache& texture_cache,
175 const SourceInputs& source_inputs,
176 Alpha& alpha) const
177 {
178 evaluate_uniform(alpha);
179 }
180
evaluate(TextureCache & texture_cache,const SourceInputs & source_inputs,foundation::Color3f & linear_rgb,Alpha & alpha)181 inline void Source::evaluate(
182 TextureCache& texture_cache,
183 const SourceInputs& source_inputs,
184 foundation::Color3f& linear_rgb,
185 Alpha& alpha) const
186 {
187 evaluate_uniform(linear_rgb, alpha);
188 }
189
evaluate(TextureCache & texture_cache,const SourceInputs & source_inputs,Spectrum & spectrum,Alpha & alpha)190 inline void Source::evaluate(
191 TextureCache& texture_cache,
192 const SourceInputs& source_inputs,
193 Spectrum& spectrum,
194 Alpha& alpha) const
195 {
196 evaluate_uniform(spectrum, alpha);
197 }
198
evaluate_uniform(float & scalar)199 inline void Source::evaluate_uniform(
200 float& scalar) const
201 {
202 scalar = 0.0f;
203 }
204
evaluate_uniform(foundation::Color3f & linear_rgb)205 inline void Source::evaluate_uniform(
206 foundation::Color3f& linear_rgb) const
207 {
208 linear_rgb.set(0.0f);
209 }
210
evaluate_uniform(Spectrum & spectrum)211 inline void Source::evaluate_uniform(
212 Spectrum& spectrum) const
213 {
214 spectrum.set(0.0f);
215 }
216
evaluate_uniform(Alpha & alpha)217 inline void Source::evaluate_uniform(
218 Alpha& alpha) const
219 {
220 alpha.set(0.0f);
221 }
222
evaluate_uniform(foundation::Color3f & linear_rgb,Alpha & alpha)223 inline void Source::evaluate_uniform(
224 foundation::Color3f& linear_rgb,
225 Alpha& alpha) const
226 {
227 evaluate_uniform(linear_rgb);
228 evaluate_uniform(alpha);
229 }
230
evaluate_uniform(Spectrum & spectrum,Alpha & alpha)231 inline void Source::evaluate_uniform(
232 Spectrum& spectrum,
233 Alpha& alpha) const
234 {
235 evaluate_uniform(spectrum);
236 evaluate_uniform(alpha);
237 }
238
239 } // namespace renderer
240