20#ifndef HEADER_SHADER_HPP
21#define HEADER_SHADER_HPP
23#include "graphics/gl_headers.hpp"
24#include "graphics/shader_files_manager.hpp"
25#include "graphics/shared_gpu_objects.hpp"
58 std::vector<std::shared_ptr<GLuint> > m_shaders;
62 template<
typename ... Types>
68 template<
typename ... Types>
76 m_shaders.push_back(shader_id);
83 template<
typename ... Types>
97 const char **varyings,
98 unsigned varyingscount);
99 static void killShaders();
105 GLuint getUniformLocation(
const char *name)
107 return glGetUniformLocation(
m_program, name);
117template<
typename T,
typename... Args>
121 std::vector<GLuint> m_uniforms;
126 GLuint block_index = glGetUniformBlockIndex(
m_program, name);
127 if (block_index != GL_INVALID_INDEX)
128 glUniformBlockBinding(
m_program, block_index, index);
138 template<
typename... U>
141 static_assert(
sizeof...(rest) ==
sizeof...(Args),
142 "Count of Uniform's name mismatch");
160 template<
typename... U>
163 m_uniforms.push_back(glGetUniformLocation(
m_program, name));
180 template<
unsigned N = 0,
typename... Args1>
183 glUniform1fv(m_uniforms[N], (
int)v.size(), v.data());
184 setUniformsImpl<N + 1>(arg...);
190 template<
unsigned N = 0>
197 template<
unsigned N = 0,
typename... Args1>
200 glUniformMatrix4fv(m_uniforms[N], 1, GL_FALSE, mat.pointer());
201 setUniformsImpl<N + 1>(arg...);
206 template<
unsigned N = 0,
typename... Args1>
209 glUniform3f(m_uniforms[N], col.r, col.g, col.b);
210 setUniformsImpl<N + 1>(arg...);
215 template<
unsigned N = 0,
typename... Args1>
218 glUniform4i(m_uniforms[N], col.getRed(), col.getGreen(),
219 col.getBlue(), col.getAlpha());
220 setUniformsImpl<N + 1>(arg...);
225 template<
unsigned N = 0,
typename... Args1>
228 glUniform4f(m_uniforms[N], ff[0], ff[1], ff[2], ff[3]);
229 setUniformsImpl<N + 1>(arg...);
234 template<
unsigned N = 0,
typename... Args1>
237 glUniform3f(m_uniforms[N], v.X, v.Y, v.Z);
238 setUniformsImpl<N + 1>(arg...);
243 template<
unsigned N = 0,
typename... Args1>
246 glUniform2f(m_uniforms[N], v.X, v.Y);
247 setUniformsImpl<N + 1>(arg...);
252 template<
unsigned N = 0,
typename... Args1>
255 glUniform2f(m_uniforms[N], v.Width, v.Height);
256 setUniformsImpl<N + 1>(arg...);
261 template<
unsigned N = 0,
typename... Args1>
264 glUniform1f(m_uniforms[N], f);
265 setUniformsImpl<N + 1>(arg...);
270 template<
unsigned N = 0,
typename... Args1>
273 glUniform1i(m_uniforms[N], f);
274 setUniformsImpl<N + 1>(arg...);
286 template<
typename ...Types>
289 Log::error(
"shader", filepath);
296 template<
typename ...Types>
312 template<
typename... T1>
316 GLuint uniform_loc = glGetUniformLocation(
m_program, uniform);
317 glUniform1i(uniform_loc, index);
331 template<
typename... T1>
334 GLuint uniform_loc = glGetUniformLocation(
m_program, uniform);
335 glUniform1i(uniform_loc, index);
355 template<
typename ... Types>
362 GLint Result = GL_FALSE;
363 glGetProgramiv(
m_program, GL_LINK_STATUS, &Result);
364 if (Result == GL_FALSE)
367 Log::error(
"Shader",
"Error when linking these shaders :");
369 glGetProgramiv(
m_program, GL_INFO_LOG_LENGTH, &info_length);
370 char *error_message =
new char[info_length];
371 glGetProgramInfoLog(
m_program, info_length, NULL, error_message);
372 Log::error(
"Shader", error_message);
373 delete[] error_message;
376 for (
auto shader : m_shaders)
382 void drawFullScreenEffect(Args...args)
385 glBindVertexArray(SharedGPUObjects::getFullScreenQuadVAO());
387 glDrawArrays(GL_TRIANGLES, 0, 3);
A simple non-templated base class.
Definition: shader.hpp:43
ShaderBase()
Constructor, which adds the shader to all instantiated shaders (for the reload-all-shaders debug opti...
Definition: shader.cpp:72
void use()
Activates the shader calling glUseProgram.
Definition: shader.hpp:103
void loadAndAttachShader(GLint shader_type, const char *name, Types ... args)
Convenience interface using const char.
Definition: shader.hpp:84
static std::vector< void(*)()> m_all_kill_functions
Maintains a list of all shaders.
Definition: shader.hpp:46
void loadAndAttachShader()
Ends recursion.
Definition: shader.hpp:63
GLuint m_program
OpenGL's program id.
Definition: shader.hpp:57
int loadTFBProgram(const std::string &vertex_file_path, const char **varyings, unsigned varyingscount)
Loads a transform feedback buffer shader with a given number of varying parameters.
Definition: shader.cpp:37
SharedShader getShaderFile(const std::string &file, unsigned type)
Get a shader file.
Definition: shader_files_manager.cpp:282
The main templated base class for all shaders that do not use textures.
Definition: shader.hpp:119
void printFileList(GLint shader_type, const char *filepath, Types ... args)
Variadic template to print a list of file names.
Definition: shader.hpp:287
void setUniformsImpl(float f, Args1... arg) const
Implementation for setUniforms for a float uniform.
Definition: shader.hpp:262
void bindPoint(const char *name, int index)
Finds the specified uniform block and assigns a binding point to it.
Definition: shader.hpp:124
void setUniformsImpl(const irr::core::vector3df &v, Args1... arg) const
Implementation for setUniforms for a vector3df uniform.
Definition: shader.hpp:235
void setUniformsImpl(const irr::core::vector2df &v, Args1... arg) const
Implementation for setUniforms for a vector2df uniform.
Definition: shader.hpp:244
void assignUniforms(U... rest)
This variadic template collects all names of uniforms in a std::vector.
Definition: shader.hpp:139
void setUniformsImpl() const
End of recursion for setUniforms implementation.
Definition: shader.hpp:191
void setUniformsImpl(const irr::core::matrix4 &mat, Args1... arg) const
Implementation for setUniforms for a matrix uniform.
Definition: shader.hpp:198
void setUniformsImpl(const irr::video::SColor &col, Args1... arg) const
Implementation for setUniforms for a SColor uniform.
Definition: shader.hpp:216
void assignTextureUnitNoUse(GLuint index, const char *uniform, T1... rest)
Recursive implementation of assignTextureUnit, but without the call to gluseProgram (which is done by...
Definition: shader.hpp:332
void setUniforms(const Args &... args) const
Sets the uniforms for this shader.
Definition: shader.hpp:173
void setUniformsImpl(const std::vector< float > &v, Args1... arg) const
Implementation for setUniforms for a vector<float> uniform.
Definition: shader.hpp:181
void assignUniformsImpl()
End of recursive implementation of assignUniforms.
Definition: shader.hpp:148
void setUniformsImpl(const std::array< float, 4 > &ff, Args1... arg) const
Implementation for setUniforms for a 4 floats uniform.
Definition: shader.hpp:226
void setUniformsImpl(const irr::core::dimension2df &v, Args1... arg) const
Implementation for setUniforms for a dimension2df uniform.
Definition: shader.hpp:253
void setUniformsImpl(const irr::video::SColorf &col, Args1... arg) const
Implementation for setUniforms for a matrix SColorF values.
Definition: shader.hpp:207
void setUniformsImpl(int f, Args1... arg) const
Implementation for setUniforms for an int uniform.
Definition: shader.hpp:271
Shader()
Constructor.
Definition: shader.hpp:347
void assignTextureUnit(GLuint index, const char *uniform, T1... rest)
Variadic top level/public interface.
Definition: shader.hpp:313
void assignUniformsImpl(const char *name, U... rest)
Recursive implementation of assignniforms.
Definition: shader.hpp:161
void assignTextureUnitNoUse()
End of recursion.
Definition: shader.hpp:326
void printFileList()
End recursion for variadic template.
Definition: shader.hpp:297
void loadProgram(AttributeType type, Types ... args)
Load a list of shaders and links them all together.
Definition: shader.hpp:356
Definition: singleton.hpp:87
static void kill()
Used to kill the singleton, if needed.
Definition: singleton.hpp:107
static ShaderFilesManager * getInstance()
Used to get the instance.
Definition: singleton.hpp:99