API Documentation
Loading...
Searching...
No Matches
GLESShader.h
Go to the documentation of this file.
1#pragma once
8#include <QOpenGLContext>
9#include <QFile>
10namespace NDEVR
11{
13 {
14 public:
32 GLESShader(ShaderType type, bool is_integer, const DynamicPointer<GLESGraphicsDevice>& device)
33 : m_device(device)
34 , m_shader_type(type)
35 , m_is_int(is_integer)
36 {
37 //m_program.addShaderFromSourceCode(QOpenGLShader::Vertex, versionedShaderCode(vertex));
38 //m_program.addShaderFromSourceCode(QOpenGLShader::Fragment, versionedShaderCode(fragment));
39 lib_assert(m_device->isReady(), "Not yet ready to render");
40
41 }
43 {
46 linkShader();
47 }
48 void compile(ShaderPart type, const char* src)
49 {
50 // vertex shader
51 switch(type)
52 {
54 {
55 m_vertex = m_device->glCreateShader(GL_VERTEX_SHADER);
56 m_device->glShaderSource(m_vertex, 1, &src, NULL);
57 m_device->glCompileShader(m_vertex);
59 } break;
61 {
62 // fragment Shader
63 m_fragment = m_device->glCreateShader(GL_FRAGMENT_SHADER);
64 m_device->glShaderSource(m_fragment, 1, &src, NULL);
65 m_device->glCompileShader(m_fragment);
66 checkCompileErrors(m_fragment, "FRAGMENT");
67 } break;
68 #ifdef GL_GEOMETRY_SHADER
70 {
71 m_geometry = m_device->glCreateShader(GL_GEOMETRY_SHADER);
72 m_device->glShaderSource(m_geometry, 1, &src, NULL);
73 m_device->glCompileShader(m_geometry);
74 checkCompileErrors(m_geometry, "GEOMETRY");
75 } break;
76 #endif
77 default:
78 lib_assert(false, "unsupported shader type");
79 break;
80 }
81
82 }
84 {
85 m_program_id = m_device->glCreateProgram();
86 m_device->glAttachShader(m_program_id, m_vertex);
87 m_device->glAttachShader(m_program_id, m_fragment);
88 if (!isNaN(m_geometry))
89 m_device->glAttachShader(m_program_id, m_geometry);
90 m_device->glLinkProgram(m_program_id);
92 // delete the shaders as they're linked into our program now and no longer necessary
93 m_device->glDeleteShader(m_vertex);
94 m_device->glDeleteShader(m_fragment);
95 if (!isNaN(m_geometry))
96 m_device->glDeleteShader(m_geometry);
100 }
102 {
103 m_device->glDeleteProgram(m_program_id);
105 }
106 void compileShader(ShaderPart type, const String& shader)
107 {
108 QFile file((":Resources/Shaders/" + shader + ".glsl").getAs<QString>());
109 uint04 file_size = cast<uint04>(file.size());
110 String code;
111 code.setSize(file_size, '\0');
112 bool success = file.open(QIODevice::ReadOnly);
113 if (success)
114 {
115 cast<uint04>(file.read(code.begin(), cast<uint04>(file_size)));
116 compile(type, versionedShaderCode(type, code.begin()));
117 }
118
119 }
120 String versionedShaderCode(ShaderPart type, const char* src) const
121 {
122 String code;
123 if (m_device->context()->isOpenGLES())
124 code = "#version 300 es\n";
125 else
126 code = "#version 330\n";
127 if (type == ShaderPart::Vertex)
128 {
129 if (m_is_int)
130 code += "layout (location = 0) in highp ivec3 in_position;\n";
131 else
132 code += "layout (location = 0) in highp vec3 in_position;\n";
133 }
134 else
135 {
136 switch (m_shader_type)
137 {
139 code += "#define POINT_SHADER 1\n";
140 break;
142 code += "#define LINEWORK_SHADER 1\n";
143 break;
144 default:
145 code += "#define SOLID_SHADER 1\n";
146 break;
147 }
148 }
149 code.append(src);
150 return code;
151 }
152 int uniformLocation(const char* const location) const
153 {
154 uint04 hash = String::hash(location);
155 auto iter = m_shader_variables.find(hash);
156 if (iter != m_shader_variables.end())
157 return iter.value();
158 int index = m_device->glGetUniformLocation(m_program_id, location);
159 if (index == -1)
160 {
161 String bad;
162 }
163 //lib_assert(index >= 0, "Bad uniform location");
164 m_shader_variables[hash] = index;
165 return index;
166 }
167 uint04 id() const
168 {
169 return m_program_id;
170 }
171 template<class t_type>
172 void setUniformValue(const char* const location, const t_type& value)
173 {
174 int var = uniformLocation(location);
175 if (var == -1)
176 return;
177 setUniformValue(var, value);
178 }
179 void setUniformValue(int location, const Vector<3, sint04>& value)
180 {
181 m_device->glUniform3iv(location, 1, &value[0]);
182 }
183 void setUniformValue(int location, const Vector<3, fltp04>& value)
184 {
185 m_device->glUniform3fv(location, 1, &value[0]);
186 }
187 void setUniformValue(int location, const Vector<4, sint04>& value)
188 {
189 m_device->glUniform4iv(location, 1, &value[0]);
190 }
191 void setUniformValue(int location, const Vector<4, fltp04>& value)
192 {
193 m_device->glUniform4fv(location, 1, &value[0]);
194 }
195 void setUniformValue(int location, const Matrix<fltp04>& value)
196 {
197 m_device->glUniformMatrix4fv(location, 1, false, &value[0][0]);
198 }
199 void setUniformValue(int location, const fltp04& value)
200 {
201 m_device->glUniform1f(location, value);
202 }
203 void setUniformValue(int location, const uint04& value)
204 {
205 m_device->glUniform1ui(location, value);
206 }
207 void setUniformValue(int location, const RGBColor& value)
208 {
209 m_device->glUniform1ui(location, value.convertToABGR32BitColor());
210 }
211 void setUniformValue(int location, const Buffer<fltp04>& value)
212 {
213 m_device->glUniform1fv(location, value.size(), value.begin());
214 }
215 template<size_t t_size>
216 void setUniformValue(int location, const fltp04(&value)[t_size])
217 {
218 m_device->glUniform1fv(location, t_size, &value[0]);
219 }
220 template<size_t t_size>
221 void setUniformValue(int location, const uint04(&value)[t_size])
222 {
223 m_device->glUniform1uiv(location, t_size, &value[0]);
224 }
225 template<size_t t_size>
226 void setUniformValue(int location, const RGBColor(&value)[t_size])
227 {
228 m_device->glUniform1uiv(location, t_size, &value[0]);
229 }
230 template<uint01 t_size>
231 void setUniformValue(int location, const Vector<t_size, fltp04>& value)
232 {
233 m_device->glUniform1fv(location, t_size, &value[0]);
234 }
235 template<uint01 t_size>
236 void setUniformValue(int location, const Vector<t_size, RGBColor>& value)
237 {
238 m_device->glUniform1uiv(location, t_size, (uint04*)&value[0]);
239 }
240 void bind()
241 {
242 m_device->glUseProgram(m_program_id);
243 }
244 protected:
245 void checkCompileErrors(GLuint shader, String type) const
246 {
247 GLint success;
248 GLchar infoLog[1024];
249 if (type != "PROGRAM")
250 {
251 m_device->glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
252 if (!success)
253 {
254 m_device->glGetShaderInfoLog(shader, 1024, NULL, infoLog);
255 throw Exception("Shader Compile Error ERROR::SHADER_COMPILATION_ERROR of type: " + type + "\n" + infoLog);
256 }
257 }
258 else
259 {
260 m_device->glGetProgramiv(shader, GL_LINK_STATUS, &success);
261 if (!success)
262 {
263 m_device->glGetProgramInfoLog(shader, 1024, NULL, infoLog);
264 throw Exception("Shader Compile Error ERROR::PROGRAM_LINKING_ERROR of type: " + type + "\n" + infoLog);
265 }
266 }
267 }
275 bool m_is_int = false;
276 };
277
279 {
280 public:
281
283 : m_device(device)
284 {
285 }
295 {
296 if (is_int)
297 {
298 auto iter = m_int_shaders.find(id);
299 if (iter != m_int_shaders.end())
300 return iter.value();
302 m_int_shaders[id] = shader;
303 return shader;
304 }
305 else
306 {
307 auto iter = m_flt_shaders.find(id);
308 if (iter != m_flt_shaders.end())
309 return iter.value();
311 m_flt_shaders[id] = shader;
312 return shader;
313 }
314 }
316 {
318 shader->compileAndLink();
319 return shader;
320 }
321 protected:
325 };
326}
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:64
constexpr t_index_type size() const
Definition Buffer.hpp:1461
void setSize(t_index_type new_size)
Definition Buffer.hpp:1413
decltype(auto) begin()
Definition Buffer.hpp:504
Definition Dictionary.h:48
Definition Pointer.hpp:303
Definition Exception.hpp:56
Definition GLESShader.h:13
uint04 m_vertex
Definition GLESShader.h:270
void linkShader()
Definition GLESShader.h:83
void setUniformValue(int location, const uint04(&value)[t_size])
Definition GLESShader.h:221
void setUniformValue(int location, const Vector< 4, sint04 > &value)
Definition GLESShader.h:187
void compileShader(ShaderPart type, const String &shader)
Definition GLESShader.h:106
void setUniformValue(int location, const Vector< 4, fltp04 > &value)
Definition GLESShader.h:191
String versionedShaderCode(ShaderPart type, const char *src) const
Definition GLESShader.h:120
void setUniformValue(int location, const Vector< t_size, RGBColor > &value)
Definition GLESShader.h:236
void checkCompileErrors(GLuint shader, String type) const
Definition GLESShader.h:245
uint04 m_geometry
Definition GLESShader.h:271
void bind()
Definition GLESShader.h:240
void compile(ShaderPart type, const char *src)
Definition GLESShader.h:48
bool m_is_int
Definition GLESShader.h:275
ShaderType
Definition GLESShader.h:16
@ e_simple_points
Definition GLESShader.h:17
@ e_solid
Definition GLESShader.h:20
@ e_solid_grid
Definition GLESShader.h:21
@ e_linework
Definition GLESShader.h:19
@ e_solid_texture
Definition GLESShader.h:22
@ e_points
Definition GLESShader.h:18
@ e_all
Definition GLESShader.h:24
@ e_shader_type_size
Definition GLESShader.h:23
uint04 id() const
Definition GLESShader.h:167
ShaderPart
Definition GLESShader.h:27
@ Fragment
Definition GLESShader.h:29
@ Geometry
Definition GLESShader.h:30
@ Vertex
Definition GLESShader.h:28
void setUniformValue(const char *const location, const t_type &value)
Definition GLESShader.h:172
GLESShader(ShaderType type, bool is_integer, const DynamicPointer< GLESGraphicsDevice > &device)
Definition GLESShader.h:32
uint04 m_program_id
Definition GLESShader.h:268
void setUniformValue(int location, const Vector< 3, sint04 > &value)
Definition GLESShader.h:179
void setUniformValue(int location, const Buffer< fltp04 > &value)
Definition GLESShader.h:211
DynamicPointer< GLESGraphicsDevice > m_device
Definition GLESShader.h:273
void setUniformValue(int location, const Matrix< fltp04 > &value)
Definition GLESShader.h:195
void compileAndLink()
Definition GLESShader.h:42
void setUniformValue(int location, const fltp04(&value)[t_size])
Definition GLESShader.h:216
int uniformLocation(const char *const location) const
Definition GLESShader.h:152
void setUniformValue(int location, const fltp04 &value)
Definition GLESShader.h:199
void setUniformValue(int location, const RGBColor(&value)[t_size])
Definition GLESShader.h:226
ShaderType m_shader_type
Definition GLESShader.h:274
void setUniformValue(int location, const Vector< t_size, fltp04 > &value)
Definition GLESShader.h:231
void setUniformValue(int location, const Vector< 3, fltp04 > &value)
Definition GLESShader.h:183
void setUniformValue(int location, const uint04 &value)
Definition GLESShader.h:203
~GLESShader()
Definition GLESShader.h:101
void setUniformValue(int location, const RGBColor &value)
Definition GLESShader.h:207
uint04 m_fragment
Definition GLESShader.h:269
Dictionary< uint04, int > m_shader_variables
Definition GLESShader.h:272
Definition GLESShader.h:279
Dictionary< GLESShader::ShaderType, DynamicPointer< GLESShader > > m_flt_shaders
Definition GLESShader.h:323
const Dictionary< GLESShader::ShaderType, DynamicPointer< GLESShader > > & integerShaders() const
Definition GLESShader.h:286
GLESShaderManager(const DynamicPointer< GLESGraphicsDevice > &device)
Definition GLESShader.h:282
const Dictionary< GLESShader::ShaderType, DynamicPointer< GLESShader > > & floatShaders() const
Definition GLESShader.h:290
DynamicPointer< GLESGraphicsDevice > m_device
Definition GLESShader.h:324
DynamicPointer< GLESShader > shader(const GLESShader::ShaderType &id, bool is_int)
Definition GLESShader.h:294
Dictionary< GLESShader::ShaderType, DynamicPointer< GLESShader > > m_int_shaders
Definition GLESShader.h:322
DynamicPointer< GLESShader > createShader(const GLESShader::ShaderType &id, bool is_int)
Definition GLESShader.h:315
Definition Geometry.h:64
Definition Matrix.hpp:173
Represents a color in the RGB space with optional alpha transparency.
Definition RGBColor.h:53
NDEVR_BASE_API uint04 convertToABGR32BitColor() const
Definition RGBColor.cpp:66
Definition String.h:40
NDEVR_BASE_API String & append(const String &string)
Definition String.cpp:484
NDEVR_BASE_API uint08 hash() const
Definition String.cpp:494
An element of a vector space. An element of the real coordinate space Rn Basis vector,...
Definition Vector.hpp:62
Definition ACIColor.h:37
float fltp04
Defines an alias representing a 4 byte floating-point number.
Definition BaseValues.hpp:157
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:120
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
constexpr bool isNaN(const t_type &value)
Query if 'value' is valid or invalid.
Definition BaseFunctions.hpp:200
Definition BaseValues.hpp:272