20 #ifndef _STELQGLGLSLSHADER_HPP_
21 #define _STELQGLGLSLSHADER_HPP_
24 #include <QGLShaderProgram>
28 #include "StelGLSLShader.hpp"
29 #include "StelGLUtilityFunctions.hpp"
33 typedef float RAW_GL_MATRIX [4][4];
77 UniformType_float = 1,
97 static const unsigned int UNIFORM_STORAGE = 512;
100 static const int MAX_UNIFORMS = 32;
105 static int UNIFORM_SIZES[UniformType_max];
119 Q_ASSERT_X(!
bound, Q_FUNC_INFO,
120 "Forgot to release() a bound shader before destroying");
131 virtual bool build();
133 virtual QString
log()
const
146 state = State_Unlocked;
151 virtual bool addVertexShader(
const QString& name,
const QString& source);
161 "Trying to enable a vertex shader with an unknown name");
162 Q_ASSERT_X(
state == State_Unlocked ||
state == State_Modified, Q_FUNC_INFO,
163 "Can't enable a vertex shader after the shader has been built");
166 if(shader.enabled) {
return;}
167 shader.enabled =
true;
168 state = State_Modified;
174 "Trying to disable a vertex shader with an unknown name");
175 Q_ASSERT_X(
state == State_Unlocked ||
state == State_Modified, Q_FUNC_INFO,
176 "Can't disable a vertex shader after the shader has been built");
179 if(!shader.enabled) {
return;}
180 shader.enabled =
false;
181 state = State_Modified;
199 Q_FUNC_INFO,
"Trying to use an unbound shader for drawing");
217 Q_ASSERT_X(
bound &&
state == State_Built, Q_FUNC_INFO,
218 "uploadUniforms called on a shader that is not bound");
222 const UniformType type =
static_cast<UniformType
>(
uniformTypes[u]);
225 case UniformType_float:
227 const float& value(*reinterpret_cast<const float* const>(data));
231 case UniformType_vec2:
233 const Vec2f& v(*reinterpret_cast<const Vec2f* const>(data));
237 case UniformType_vec3:
239 const Vec3f& v(*reinterpret_cast<const Vec3f* const>(data));
243 case UniformType_vec4:
245 const Vec4f& v(*reinterpret_cast<const Vec4f* const>(data));
249 case UniformType_mat4:
251 const RAW_GL_MATRIX& m(*reinterpret_cast<const RAW_GL_MATRIX* const>(data));
255 case UniformType_bool:
257 const bool& value(*reinterpret_cast<const bool* const>(data));
261 case UniformType_int:
263 const int& value(*reinterpret_cast<const int* const>(data));
268 Q_ASSERT_X(
false, Q_FUNC_INFO,
"Unknown or invalid uniform type");
270 data += UNIFORM_SIZES[type];
293 Q_ASSERT_X(
bound &&
state == State_Built, Q_FUNC_INFO,
294 "pushUniformStorage() called when the shader is not bound");
296 "Too many uniform storage stack pushes");
307 Q_ASSERT_X(
bound &&
state == State_Built, Q_FUNC_INFO,
308 "popUniformStorage() called when the shader is not bound");
310 "Too many uniform storage stack pops (nothing left to pop)");
321 Q_ASSERT_X(
bound &&
state == State_Built, Q_FUNC_INFO,
322 "clearUniforms() called when the shader is not bound");
427 void* setUniformValue_helper(
const char*
const name,
const enum UniformType kind)
429 Q_ASSERT_X(
bound, Q_FUNC_INFO,
430 "Trying to set a uniform value with an unbound shader");
431 Q_ASSERT_X(
uniformCount < MAX_UNIFORMS, Q_FUNC_INFO,
"Too many uniforms");
433 "Uniform storage exceeded");
443 *
static_cast<float*
>(setUniformValue_helper(name, UniformType_float)) = value;
448 *
static_cast<Vec2f*
>(setUniformValue_helper(name, UniformType_vec2)) = value;
453 *
static_cast<Vec3f*
>(setUniformValue_helper(name, UniformType_vec3)) = value;
458 *
static_cast<Vec4f*
>(setUniformValue_helper(name, UniformType_vec4)) = value;
463 *
static_cast<Mat4f*
>(setUniformValue_helper(name, UniformType_mat4)) = m;
468 *
static_cast<bool*
>(setUniformValue_helper(name, UniformType_bool)) = value;
473 *
static_cast<int*
>(setUniformValue_helper(name, UniformType_int)) = value;
482 QGLShaderProgram* getProgramFromCache();
489 QGLShader* createVertexShader(
const QString& source);
492 #endif // _STELQGLGLSLSHADER_HPP_