NDEVR
API Documentation
GLESShader.h
1#pragma once
2#include "GLESGraphicsDevice.h"
3#include <NDEVR/QTTools.h>
4#include <NDEVR/Dictionary.h>
5#include <NDEVR/String.h>
6#include <NDEVR/RGBColor.h>
7#include <QOpenGLContext>
8namespace NDEVR
9{
14
16 {
17 public:
27
35
37 {
39 bool is_simple = false;
40 bool is_integer = false;
41 bool is_tbn = false;
42 bool has_color_channel = false;
43 bool has_normal_channel = false;
44 bool has_grid = false;
45 bool has_texture = false;
46 bool is_relative_to_camera = false;
50 uint04 id() const
51 {
52 if (IsInvalid(m_id))
53 {
54 m_id = 1024U * cast<uint04>(type);
55 m_id += 16384U * palette_size;
56 if (is_simple)
57 m_id += 0x01;
58 if (is_integer)
59 m_id += 0x02;
60 if (is_tbn)
61 m_id += 0x04;
63 m_id += 0x08;
64 if (has_grid)
65 m_id += 0x10;
66 if (has_texture)
67 m_id += 0x20;
69 m_id += 0x40;
71 m_id += 0x80;
72 }
73 return m_id;
74 }
75 private:
76 mutable uint04 m_id = Constant<uint04>::Invalid;
77 };
78
89 void compile(ShaderPart type, const char* src);
91 void linkShader();
106 int uniformLocation(const char* const location) const
107 {
108 uint08 hash = String::hash(location);
109 return uniformLocation(hash, location);
110 }
111
115 int uniformLocation(uint08 hash, const char* const location) const;
120 bool checkID(int id, std::size_t value);
121 template<class t_type>
122 bool checkID(int id, const t_type& value)
123 {
124 std::hash<t_type> hasher;
125 std::size_t hash = hasher(value);
126 return checkID(id, hash);
127 }
128 template<uint01 t_dims, class t_type>
129 bool checkID(int id, const Buffer<Vector<t_dims, t_type>>& value)
130 {
131 std::hash<t_type> hasher;
132 std::size_t hash = 0;
133 for (uint01 i = 0; i < value.size(); i++)
134 {
135 for (uint01 n = 0; n < t_dims; n++)
136 hash = (hash * 0x100000001b3ULL) ^ (hasher(value[i][n]) + 0x9e3779b97f4a7c15ULL);
137 }
138 return checkID(id, hash);
139 }
140 template<class t_type>
141 bool checkID(int id, const Buffer<t_type>& value)
142 {
143 std::hash<t_type> hasher;
144 std::size_t hash = 0;
145 for (uint01 i = 0; i < value.size(); i++)
146 hash = (hash * 0x100000001b3ULL) ^ (hasher(value[i]) + 0x9e3779b97f4a7c15ULL);
147 return checkID(id, hash);
148 }
149 template<uint01 t_dims, class t_type>
150 bool checkID(int id, const Vector<t_dims, t_type>& value)
151 {
152 std::hash<t_type> hasher;
153 std::size_t hash = 0;
154 for(uint01 i = 0; i < t_dims; i++)
155 hash = (hash * 0x100000001b3ULL) ^ (hasher(value[i]) + 0x9e3779b97f4a7c15ULL);
156 return checkID(id, hash);
157 }
158 template<uint01 t_dim_a, uint01 t_dim_b, class t_type>
159 bool checkID(int id, const Vector<t_dim_a, Vector<t_dim_b, t_type>>& value)
160 {
161 std::hash<t_type> hasher;
162 std::size_t hash = 0;
163 for (uint01 i = 0; i < t_dim_a; i++)
164 for (uint01 n = 0; n < t_dim_b; n++)
165 hash = (hash * 0x100000001b3ULL) ^ (hasher(value[i][n]) + 0x9e3779b97f4a7c15ULL);
166 return checkID(id, hash);
167 }
168 template<uint01 t_dim_a, uint01 t_dim_b, class t_type>
169 bool checkID(int id, const Matrix<t_type, t_dim_a, t_dim_b>& value)
170 {
171 std::hash<t_type> hasher;
172 std::size_t hash = 0;
173 for (uint01 i = 0; i < t_dim_a; i++)
174 for (uint01 n = 0; n < t_dim_b; n++)
175 hash = (hash * 0x100000001b3ULL) ^ (hasher(value[i][n]) + 0x9e3779b97f4a7c15ULL);
176 return checkID(id, hash);
177 }
180 uint04 id() const;
184 template<class t_type>
185 void setUniformValue(const char* const location, const t_type& value)
186 {
187 int var = uniformLocation(location);
188 if (var == -1)
189 return;
190 setUniformValue(var, value);
191 }
192 template<size_t t_size>
193 void setUniformValue(int location, const fltp04(&value)[t_size])
194 {
195 if(!checkID(location, value))
196 m_device->glUniform1fv(location, t_size, &value[0]);
197 }
198 template<size_t t_size>
199 void setUniformValue(int location, const uint04(&value)[t_size])
200 {
201 if (!checkID(location, value))
202 m_device->glUniform1uiv(location, t_size, &value[0]);
203 }
204 template<size_t t_size>
205 void setUniformValue(int location, const RGBColor(&value)[t_size])
206 {
207 if (!checkID(location, value))
208 m_device->glUniform1uiv(location, t_size, &value[0]);
209 }
210 template<uint01 t_size>
211 void setUniformValue(int location, const Vector<t_size, fltp04>& value)
212 {
213 if (!checkID(location, value))
214 m_device->glUniform1fv(location, t_size, &value[0]);
215 }
216 template<uint01 t_size>
217 void setUniformValue(int location, const Vector<t_size, Vector<4, fltp04>>& value)
218 {
219 if (!checkID(location, value))
220 m_device->glUniform4fv(location, t_size, &value[0][0]);
221 }
222 void setUniformValue(int location, const Buffer<Vector<4, fltp04>>& value)
223 {
224 if (!checkID(location, value))
225 m_device->glUniform4fv(location, value.size(), &value[0][0]);
226 }
227 template<uint01 t_size>
228 void setUniformValue(int location, const Vector<t_size, RGBColor>& value)
229 {
230 if (!checkID(location, value))
231 m_device->glUniform1uiv(location, t_size, (uint04*)&value[0]);
232 }
233 void setUniformValue(int location, const Vector<3, sint04>& value);
234 void setUniformValue(int location, const Vector<3, fltp04>& value);
235 void setUniformValue(int location, const Vector<4, sint04>& value);
236 void setUniformValue(int location, const Vector<4, fltp04>& value);
237 void setUniformValue(int location, const Matrix<fltp04>& value);
238 void setUniformValue(int location, const fltp04& value);
239 void setUniformValue(int location, const uint04& value);
240 void setUniformValue(int location, const sint04& value);
241 //void setUniformValue(int location, const Buffer<fltp04>& value);
243 void bind();
245 void unBind();
248 const Definition& definition() const { return m_definition; }
252 private:
256 void checkCompileErrors(GLuint shader, StringView type) const;
257 String buildFlatVertexSource() const;
258 String buildFlatFragmentSource() const;
259 String buildFullVertexSource() const;
260 String buildFullFragmentSource() const;
261 String buildTBNVertexSource() const;
262 String buildTBNFragmentSource() const;
263 private:
264 Definition m_definition;
265 Buffer<std::size_t> m_bound_values;
266 static GLESShader* s_bound_shader;
267 uint04 m_program_id = Constant<uint04>::Invalid;
268 uint04 m_fragment = Constant<uint04>::Invalid;
269 uint04 m_vertex = Constant<uint04>::Invalid;
270 uint04 m_geometry = Constant<uint04>::Invalid;
271 mutable Dictionary<uint08, int> m_shader_variables;
273 };
274
299}
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A hash-based key-value store, useful for quick associative lookups.
Definition Dictionary.h:64
Provides a modifiable pointer that has shared ownership of a dynamically allocated object.
Definition Pointer.hpp:356
GLESShader * shader(const GLESShader::Definition &def)
Retrieves an existing shader matching the definition, or nullptr.
GLESShader * createShader(const GLESShader::Definition &def)
Creates and compiles a new shader from the given definition.
GLESShaderManager(const DynamicPointer< GLESGraphicsDevice > &device)
Constructs a GLESShaderManager for the given device.
const Dictionary< uint04, GLESShader * > & shaders() const
Returns the dictionary of all compiled shaders.
Definition GLESShader.h:286
A Shader for rendering default OpenGL logic to the graphics card.
Definition GLESShader.h:16
GLESShader(const Definition &def, const DynamicPointer< GLESGraphicsDevice > &device)
Constructs a GLESShader from a definition and device.
void compileAndLink()
Compiles all shader stages and links the program.
void compile(ShaderPart type, const char *src)
Compiles a single shader stage from source.
ShaderType
Classifies the type of geometry a shader is designed to render.
Definition GLESShader.h:20
@ e_solid
Solid/filled rendering shader.
Definition GLESShader.h:23
@ e_shader_type_size
Sentinel for the number of shader types.
Definition GLESShader.h:24
@ e_linework
Line rendering shader.
Definition GLESShader.h:22
@ e_points
Point rendering shader.
Definition GLESShader.h:21
@ e_all
Matches all shader types.
Definition GLESShader.h:25
static GLESShader * BoundShader()
Returns the currently bound shader.
int uniformLocation(const char *const location) const
Returns the uniform location for a named variable.
Definition GLESShader.h:106
void bind()
Binds this shader program for use.
uint04 id() const
Returns the unique identifier of this shader based on its definition.
~GLESShader()
Destroys the shader and releases GPU programs.
int uniformLocation(uint08 hash, const char *const location) const
Returns the uniform location using a pre-computed hash.
String buildVertexSource() const
Builds the complete vertex shader source string for the current definition.
void unBind()
Unbinds this shader program.
void linkShader()
Links all compiled shader stages into a program.
String buildFragmentSource() const
Builds the complete fragment shader source string for the current definition.
const Definition & definition() const
Returns the shader definition.
Definition GLESShader.h:248
ShaderPart
Identifies a stage within the shader pipeline.
Definition GLESShader.h:30
@ Vertex
Vertex shader stage.
Definition GLESShader.h:31
@ Fragment
Fragment shader stage.
Definition GLESShader.h:32
@ Geometry
Geometry shader stage.
Definition GLESShader.h:33
bool checkID(int id, std::size_t value)
Checks if a uniform value has changed since last set.
String versionedShaderCode(ShaderPart type, const String &src) const
Prepends the appropriate GLSL version directive and defines to shader source.
void setUniformValue(const char *const location, const t_type &value)
Sets a uniform value by name.
Definition GLESShader.h:185
The core String View class for the NDEVR API.
Definition StringView.h:58
The core String class for the NDEVR API.
Definition String.h:95
constexpr uint08 hash() const
Creates a simple, quick hash of the object.
Definition String.h:524
A fixed-size array with N dimensions used as the basis for geometric and mathematical types.
Definition Vector.hpp:62
The primary namespace for the NDEVR SDK.
@ type
The type identifier string for this model node.
Definition Model.h:58
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408
Defines the set of features a shader must support, used to select or compile the correct variant.
Definition GLESShader.h:37
bool has_normal_channel
Whether a per-vertex normal channel is available.
Definition GLESShader.h:43
uint04 palette_size
The number of palette colors.
Definition GLESShader.h:47
bool is_integer
Whether vertex attributes are integer-typed.
Definition GLESShader.h:40
bool has_color_channel
Whether a per-vertex color channel is available.
Definition GLESShader.h:42
bool has_texture
Whether texture mapping is enabled.
Definition GLESShader.h:45
bool is_simple
Whether the shader uses simplified rendering.
Definition GLESShader.h:39
uint04 id() const
Computes a unique identifier for this definition based on its flags.
Definition GLESShader.h:50
bool is_tbn
Whether tangent-bitangent-normal data is used.
Definition GLESShader.h:41
bool is_relative_to_camera
Whether rendering is camera-relative.
Definition GLESShader.h:46
ShaderType type
The geometry type this shader handles.
Definition GLESShader.h:38
bool has_grid
Whether grid overlay rendering is enabled.
Definition GLESShader.h:44