20 #ifndef HEADER_TEXTURE_SHADER_HPP
21 #define HEADER_TEXTURE_SHADER_HPP
23 #include "graphics/central_settings.hpp"
24 #include "graphics/gl_headers.hpp"
25 #include "graphics/shader.hpp"
26 #include "utils/cpp2011.hpp"
36 ST_NEAREST_FILTERED = ST_MIN,
37 ST_TRILINEAR_ANISOTROPIC_FILTERED,
40 ST_NEAREST_FILTERED_ARRAY2D,
42 ST_SHADOW_SAMPLER_ARRAY2D,
43 ST_TRILINEAR_CLAMPED_ARRAY2D,
44 ST_VOLUME_LINEAR_FILTERED,
45 ST_NEARED_CLAMPED_FILTERED,
46 ST_BILINEAR_CLAMPED_FILTERED,
49 ST_MAX = ST_SEMI_TRILINEAR
52 ST_MAX = ST_TEXTURE_BUFFER
65 typedef std::function<void(GLuint, GLuint)> BindFunction;
68 static void bindTextureNearest(GLuint tex_unit, GLuint tex_id);
69 static void bindTextureBilinear(GLuint texture_unit, GLuint tex_id);
70 static void bindTextureBilinearClamped(GLuint tex_unit, GLuint tex_id);
71 static void bindTextureNearestClamped(GLuint tex_unit, GLuint tex_id);
72 static void bindTextureTrilinearAnisotropic(GLuint tex_unit, GLuint tex_id);
73 static void bindTextureSemiTrilinear(GLuint tex_unit, GLuint tex_id);
74 static void bindCubemapTrilinear(GLuint tex_unit, GLuint tex_id);
75 static void bindTextureNearestArray(GLuint tex_unit, GLuint tex_id);
76 static void bindTextureShadow(GLuint tex_unit, GLuint tex_id);
77 static void bindTextureShadowArray(GLuint tex_unit, GLuint tex_id);
78 static void bindTrilinearClampedArrayTexture(GLuint tex_unit, GLuint tex_id);
79 static void bindTextureVolume(GLuint tex_unit, GLuint tex_id);
80 static void bindTextureBuffer(GLuint tex_unit, GLuint tex_id);
82 GLuint createSamplers(SamplerTypeNew sampler_type);
85 static GLuint createNearestSampler();
86 static GLuint createTrilinearSampler();
87 static GLuint createBilinearSampler();
88 static GLuint createShadowSampler();
89 static GLuint createTrilinearClampedArray();
90 static GLuint createBilinearClampedSampler();
91 static GLuint createSemiTrilinearSampler();
93 static BindFunction m_all_bind_functions[];
94 std::vector<BindFunction> m_bind_functions;
95 static GLuint m_all_texture_types[];
104 template<
class C,
int NUM_TEXTURES,
typename...tp>
111 std::vector<GLuint> m_texture_units;
112 std::vector<GLenum> m_texture_type;
113 std::vector<GLenum> m_texture_location;
116 std::vector<GLuint> m_sampler_ids;
123 template<
unsigned N,
typename...Args>
126 static_assert(N == NUM_TEXTURES,
"Wrong number of texture names");
133 template<
unsigned N,
typename...Args>
135 SamplerTypeNew sampler_type, Args...args)
138 m_sampler_ids.push_back(createSamplers(sampler_type));
140 assert(sampler_type >= ST_MIN && sampler_type <= ST_MAX);
141 m_texture_type.push_back(m_all_texture_types[sampler_type]);
143 GLuint location = this->getUniformLocation(name);
144 m_texture_location.push_back(location);
145 glUniform1i(location, tex_unit);
146 m_texture_units.push_back(tex_unit);
149 assert(sampler_type >= ST_MIN && sampler_type <= ST_MAX);
150 m_bind_functions.push_back( m_all_bind_functions[sampler_type]);
152 assignTextureNamesImpl<N + 1>(args...);
160 template<
typename...Args>
164 assignTextureNamesImpl<0>(args...);
175 static_assert(N == NUM_TEXTURES,
"Not enough texture set");
181 template<
int N,
typename... TexIds>
184 if (CVS->isARBSamplerObjectsUsable())
186 glActiveTexture(GL_TEXTURE0 + m_texture_units[N]);
187 glBindTexture(m_texture_type[N], tex_id);
188 glBindSampler(m_texture_units[N], m_sampler_ids[N]);
192 m_bind_functions[N](m_texture_units[N], tex_id);
194 setTextureUnitsImpl<N + 1>(args...);
201 template<
typename... TexIds>
204 setTextureUnitsImpl<0>(args...);
217 static_assert(N == NUM_TEXTURES,
"Not enough handles set");
226 template<
int N,
typename... HandlesId>
229 #if !defined(USE_GLES2)
231 glUniformHandleui64ARB(m_texture_location[N], handle);
233 setTextureHandlesImpl<N + 1>(args...);
241 template<
typename... HandlesId>
244 setTextureHandlesImpl<0>(ids...);
253 for (
unsigned i = 0; i < m_sampler_ids.size(); i++)
254 glDeleteSamplers(1, &m_sampler_ids[i]);
void use()
Activates the shader calling glUseProgram.
Definition: shader.hpp:103
The main templated base class for all shaders that do not use textures.
Definition: shader.hpp:119
A simple non-templated base class for a shader that uses textures.
Definition: texture_shader.hpp:63
Class C needs to be the newly declared shaders class (necessary for the instance template).
Definition: texture_shader.hpp:107
void assignSamplerNames(Args...args)
The protected interface for setting sampler names - it is only called from instances.
Definition: texture_shader.hpp:161
void setTextureHandlesImpl()
End of recursion, just checks at compile time if number of arguments is correct.
Definition: texture_shader.hpp:215
void setTextureHandles(HandlesId... ids)
The protected interface.
Definition: texture_shader.hpp:242
void setTextureHandlesImpl(uint64_t handle, HandlesId... args)
Recursive implementation of setTextureHandles.
Definition: texture_shader.hpp:227
void setTextureUnitsImpl(GLuint tex_id, TexIds... args)
The recursive implementation.
Definition: texture_shader.hpp:182
void assignTextureNamesImpl(GLuint tex_unit, const char *name, SamplerTypeNew sampler_type, Args...args)
Recursive implementation, peeling the texture unit and name off the list of arguments.
Definition: texture_shader.hpp:134
void setTextureUnitsImpl()
End of recursion, just check if number of arguments is correct.
Definition: texture_shader.hpp:173
void setTextureUnits(TexIds... args)
Public implementation of setTextureUnits.
Definition: texture_shader.hpp:202
~TextureShader()
Destructor which frees al lsampler ids.
Definition: texture_shader.hpp:251
void assignTextureNamesImpl()
End of recursive variadic template AssigTextureNames.
Definition: texture_shader.hpp:124