Lagrange
Shader.h
1/*
2 * Copyright 2021 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/ui/api.h>
15#include <lagrange/ui/Entity.h>
16#include <lagrange/ui/utils/math.h>
17
18#include <lagrange/ui/types/Color.h>
19#include <lagrange/ui/types/Texture.h>
20
21#include <lagrange/Logger.h>
22
23#if defined(__EMSCRIPTEN__)
24#include <webgl/webgl2.h>
25#else
26#include <GL/gl3w.h>
27#endif
28
29#include <typeindex>
30#include <variant>
31#include <memory>
32#include <string>
33#include <unordered_map>
34#include <vector>
35
36namespace lagrange {
37namespace ui {
38
39
41{
42 std::shared_ptr<Texture> texture = nullptr;
43 Texture::Transform transform;
44 Eigen::Vector4f color;
45};
46
47
48template <typename T>
50{
51 using value_type = T;
52 T default_value;
53 std::string display_name;
54};
55
56struct ShaderColorProperty : public ShaderProperty<Color>
57{
58 bool is_attrib = false; // Use as vertex attribute default value, not uniform
59};
60
61struct ShaderVectorProperty : public ShaderProperty<Eigen::Vector4f>
62{
63};
64
65
66struct ShaderTextureProperty : public ShaderProperty<ShaderTextureValue>
67{
68 int value_dimension = 0;
69 GLenum sampler_type;
70
72 bool normal = false; // Is it a normal texture
73 bool transformable = false; // Can 2D transform be used
74 bool colormap = false; // Treat as colormap / transfer function
75};
76
77struct ShaderFloatProperty : public ShaderProperty<float>
78{
79 float min_value = std::numeric_limits<float>::lowest();
80 float max_value = std::numeric_limits<float>::max();
81};
82
84{
85};
86
88{
89 int min_value = std::numeric_limits<int>::lowest();
90 int max_value = std::numeric_limits<int>::max();
91};
92
93//
94struct GLQuery
95{
96 GLenum type;
97 GLuint id;
98 GLint result;
99};
100
103{
104 constexpr static const StringID DepthTest = "DepthTest"_hs;
105 constexpr static const StringID DepthMask = "DepthMask"_hs;
106 constexpr static const StringID DepthFunc = "DepthFunc"_hs;
107 constexpr static const StringID BlendEquation = "BlendEquation"_hs;
108 constexpr static const StringID DrawBuffer = "DrawBuffer"_hs;
109 constexpr static const StringID ReadBuffer = "ReadBuffer"_hs;
110 constexpr static const StringID CullFaceEnabled = "CullFaceEnabled"_hs;
111 constexpr static const StringID CullFace = "CullFace"_hs;
112 constexpr static const StringID ColorMask = "ColorMask"_hs;
113
114 constexpr static const StringID ScissorTest = "ScissorTest"_hs;
115 constexpr static const StringID ScissorX = "ScissorX"_hs;
116 constexpr static const StringID ScissorY = "ScissorY"_hs;
117 constexpr static const StringID ScissorWidth = "ScissorWidth"_hs;
118 constexpr static const StringID ScissortHeight = "ScissortHeight"_hs;
119
120
122 constexpr static const StringID BlendSrcRGB = "BlendSrcRGB"_hs;
123 constexpr static const StringID BlendDstRGB = "BlendDstRGB"_hs;
124 constexpr static const StringID BlendSrcAlpha = "BlendSrcAlpha"_hs;
125 constexpr static const StringID BlendDstAlpha = "BlendDstAlpha"_hs;
126
128 constexpr static const StringID Query = "Query"_hs;
129
131 constexpr static const StringID Pass = "_Pass"_hs;
132
134 constexpr static const StringID PolygonMode = "_PolygonMode"_hs;
135
136 constexpr static const StringID PointSize = "_PointSize"_hs;
137
139 constexpr static const StringID Primitive = "Primitive"_hs;
140};
141
143
144enum ShaderInterface {
145 SHADER_INTERFACE_NONE = 0,
146 SHADER_INTERFACE_UNIFORM = 1,
147 SHADER_INTERFACE_ATTRIB = 2
148};
149
150struct LA_UI_API ShaderValue
151{
152 int location;
153 int size;
154 GLenum type;
155 ShaderInterface shaderInterface;
156 const ShaderValue& operator=(Eigen::Vector2f val) const;
157 const ShaderValue& operator=(Eigen::Vector3f val) const;
158 const ShaderValue& operator=(Eigen::Vector4f val) const;
159
160 const ShaderValue& operator=(Eigen::Matrix2f val) const;
161 const ShaderValue& operator=(Eigen::Matrix3f val) const;
162 const ShaderValue& operator=(Eigen::Matrix4f val) const;
163 const ShaderValue& operator=(Eigen::Affine3f val) const;
164 const ShaderValue& operator=(Eigen::Projective3f val) const;
165
166
167 const ShaderValue& operator=(double val) const;
168 const ShaderValue& operator=(float val) const;
169 const ShaderValue& operator=(int val) const;
170 const ShaderValue& operator=(bool val) const;
171
172
173 const ShaderValue& operator=(const std::vector<Eigen::Vector3f>& arr) const;
174
175
176 const ShaderValue& set_array(const float* data, int n) const;
177 const ShaderValue& set_array(const int* data, int n) const;
178 const ShaderValue& set_array(const unsigned int* data, int n) const;
179
180 // NO templated Eigen matrix base, reason:
181 // If passing things to shader,
182 // pass it using the exact type to avoid mistakes
183
184 const ShaderValue& set_vectors(const Eigen::Vector2f* data, int n) const;
185 const ShaderValue& set_vectors(const Eigen::Vector3f* data, int n) const;
186 const ShaderValue& set_vectors(const Eigen::Vector4f* data, int n) const;
187 const ShaderValue& set_matrices(const Eigen::Matrix2f* data, int n, bool transpose = false)
188 const;
189 const ShaderValue& set_matrices(const Eigen::Matrix3f* data, int n, bool transpose = false)
190 const;
191 const ShaderValue& set_matrices(const Eigen::Matrix4f* data, int n, bool transpose = false)
192 const;
193 const ShaderValue& set_matrices(const Eigen::Affine3f* data, int n, bool transpose = false)
194 const;
195
196
197 static ShaderValue none;
198};
199
200class LA_UI_API ShaderException : public std::runtime_error
201{
202public:
203 ShaderException(const char* str)
204 : std::runtime_error(str)
205 {}
206
207 void set_desc(const std::string& desc) { m_desc = desc; }
208 const std::string& get_desc() { return m_desc; }
209
210private:
211 std::string m_desc;
212};
213
214using ShaderDefines = std::vector<std::pair<std::string, std::string>>;
215
216class LA_UI_API Shader
217{
218public:
219 Shader(const std::string& code, const ShaderDefines& defines); // throws
220
221 bool bind() const;
222 static void unbind();
223
224 const ShaderValue& operator[](const std::string& name);
225
226 const std::string& get_source() const;
227 std::string& get_source();
228
229 const ShaderDefines& get_defines() const;
230
231 const std::unordered_map<StringID, ShaderValue>& uniforms() const { return m_uniforms; }
232 const std::unordered_map<StringID, ShaderValue>& attribs() const { return m_attribs; }
233 const std::unordered_map<StringID, std::string>& names() const { return m_names; }
234
235 const std::string& name(StringID id) const { return m_names.at(id); }
236
237 const ShaderValue& uniform(const std::string& name) const { return uniform(string_id(name)); }
238
239 const ShaderValue& uniform(StringID id) const
240 {
241 auto it = m_uniforms.find(id);
242 if (it == m_uniforms.end()) return ShaderValue::none;
243 return it->second;
244 }
245
246 const ShaderValue& attrib(const std::string& name) const { return attrib(string_id(name)); }
247
248 const ShaderValue& attrib(StringID id) const
249 {
250 auto it = m_attribs.find(id);
251 if (it == m_attribs.end()) return ShaderValue::none;
252 return it->second;
253 }
254
255 const std::unordered_map<StringID, int>& sampler_indices() const { return m_sampler_indices; }
256
257 const std::unordered_map<StringID, ShaderTextureProperty>& texture_properties() const
258 {
259 return m_texture_properties;
260 }
261
262 const std::unordered_map<StringID, ShaderFloatProperty>& float_properties() const
263 {
264 return m_float_properties;
265 }
266
267 const std::unordered_map<StringID, ShaderColorProperty>& color_properties() const
268 {
269 return m_color_properties;
270 }
271
272 const std::unordered_map<StringID, ShaderVectorProperty>& vector_properties() const
273 {
274 return m_vector_properties;
275 }
276
277 const std::unordered_map<StringID, ShaderBoolProperty>& bool_properties() const
278 {
279 return m_bool_properties;
280 }
281
282 const std::unordered_map<StringID, ShaderIntProperty>& int_properties() const
283 {
284 return m_int_properties;
285 }
286
287 void upload_default_values();
288
289private:
290 void process_properties(std::string& source);
291
292 GLuint m_id;
293 std::unordered_map<StringID, ShaderValue> m_uniforms;
294 std::unordered_map<StringID, ShaderValue> m_attribs;
295 std::unordered_map<StringID, int> m_sampler_indices;
296 std::string m_source;
297 ShaderDefines m_defines;
298
299 std::unordered_map<StringID, ShaderTextureProperty> m_texture_properties;
300 std::unordered_map<StringID, ShaderFloatProperty> m_float_properties;
301 std::unordered_map<StringID, ShaderColorProperty> m_color_properties;
302 std::unordered_map<StringID, ShaderVectorProperty> m_vector_properties;
303 std::unordered_map<StringID, ShaderBoolProperty> m_bool_properties;
304 std::unordered_map<StringID, ShaderIntProperty> m_int_properties;
305 std::unordered_map<StringID, std::string> m_names;
306};
307
308} // namespace ui
309} // namespace lagrange
Definition: Shader.h:201
Definition: Shader.h:217
Lagrange UI Viewer and mini 3D engine.
Definition: AcceleratedPicking.h:22
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Shader.h:95
Rasterization pipeline option names.
Definition: Shader.h:103
static constexpr const StringID Pass
Use for transparency and polygon offset.
Definition: Shader.h:131
static constexpr const StringID PolygonMode
Rasterize as point/line/polygon, values: GL_POINT/GL_LINE/GL_FILL. Applies to GL_FRONT_AND_BACK by de...
Definition: Shader.h:134
static constexpr const StringID Query
Will only render entities with GLQuery of the same type.
Definition: Shader.h:128
static constexpr const StringID Primitive
Not set by default, use to override.
Definition: Shader.h:139
static constexpr const StringID BlendSrcRGB
Blend func separate.
Definition: Shader.h:122
Definition: Shader.h:84
Definition: Shader.h:57
Definition: Shader.h:78
Definition: Shader.h:88
Definition: Shader.h:50
bool normal
Semantics:
Definition: Shader.h:72
Definition: Shader.h:41
Definition: Shader.h:151
Definition: Shader.h:62
Definition: Texture.h:33