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,
41 ST_TRILINEAR_CLAMPED_ARRAY2D,
42 ST_VOLUME_LINEAR_FILTERED,
43 ST_NEARED_CLAMPED_FILTERED,
44 ST_BILINEAR_CLAMPED_FILTERED,
47 ST_MAX = ST_SEMI_TRILINEAR
50 ST_MAX = ST_TEXTURE_BUFFER
63 typedef std::function<void(GLuint, GLuint)> BindFunction;
66 static void bindTextureNearest(GLuint tex_unit, GLuint tex_id);
67 static void bindTextureBilinear(GLuint texture_unit, GLuint tex_id);
68 static void bindTextureBilinearClamped(GLuint tex_unit, GLuint tex_id);
69 static void bindTextureNearestClamped(GLuint tex_unit, GLuint tex_id);
70 static void bindTextureTrilinearAnisotropic(GLuint tex_unit, GLuint tex_id);
71 static void bindTextureSemiTrilinear(GLuint tex_unit, GLuint tex_id);
72 static void bindCubemapTrilinear(GLuint tex_unit, GLuint tex_id);
73 static void bindTextureShadow(GLuint tex_unit, GLuint tex_id);
74 static void bindTrilinearClampedArrayTexture(GLuint tex_unit, GLuint tex_id);
75 static void bindTextureVolume(GLuint tex_unit, GLuint tex_id);
76 static void bindTextureBuffer(GLuint tex_unit, GLuint tex_id);
78 GLuint createSamplers(SamplerTypeNew sampler_type);
81 static GLuint createNearestSampler();
82 static GLuint createTrilinearSampler();
83 static GLuint createBilinearSampler();
84 static GLuint createShadowSampler();
85 static GLuint createTrilinearClampedArray();
86 static GLuint createBilinearClampedSampler();
87 static GLuint createSemiTrilinearSampler();
89 static BindFunction m_all_bind_functions[];
90 std::vector<BindFunction> m_bind_functions;
91 static GLuint m_all_texture_types[];
100template<
class C,
int NUM_TEXTURES,
typename...tp>
107 std::vector<GLuint> m_texture_units;
108 std::vector<GLenum> m_texture_type;
109 std::vector<GLenum> m_texture_location;
112 std::vector<GLuint> m_sampler_ids;
119 template<
unsigned N,
typename...Args>
122 static_assert(N == NUM_TEXTURES,
"Wrong number of texture names");
129 template<
unsigned N,
typename...Args>
131 SamplerTypeNew sampler_type, Args...args)
134 m_sampler_ids.push_back(createSamplers(sampler_type));
136 assert(sampler_type >= ST_MIN && sampler_type <= ST_MAX);
137 m_texture_type.push_back(m_all_texture_types[sampler_type]);
139 GLuint location = this->getUniformLocation(name);
140 m_texture_location.push_back(location);
141 glUniform1i(location, tex_unit);
142 m_texture_units.push_back(tex_unit);
145 assert(sampler_type >= ST_MIN && sampler_type <= ST_MAX);
146 m_bind_functions.push_back( m_all_bind_functions[sampler_type]);
148 assignTextureNamesImpl<N + 1>(args...);
156 template<
typename...Args>
160 assignTextureNamesImpl<0>(args...);
171 static_assert(N == NUM_TEXTURES,
"Not enough texture set");
177 template<
int N,
typename... TexIds>
180 if (CVS->isARBSamplerObjectsUsable())
182 glActiveTexture(GL_TEXTURE0 + m_texture_units[N]);
183 glBindTexture(m_texture_type[N], tex_id);
184 glBindSampler(m_texture_units[N], m_sampler_ids[N]);
188 m_bind_functions[N](m_texture_units[N], tex_id);
190 setTextureUnitsImpl<N + 1>(args...);
197 template<
typename... TexIds>
200 setTextureUnitsImpl<0>(args...);
213 static_assert(N == NUM_TEXTURES,
"Not enough handles set");
222 template<
int N,
typename... HandlesId>
225#if !defined(USE_GLES2)
227 glUniformHandleui64ARB(m_texture_location[N], handle);
229 setTextureHandlesImpl<N + 1>(args...);
237 template<
typename... HandlesId>
240 setTextureHandlesImpl<0>(ids...);
249 for (
unsigned i = 0; i < m_sampler_ids.size(); i++)
250 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:61
Class C needs to be the newly declared shaders class (necessary for the instance template).
Definition: texture_shader.hpp:103
void assignSamplerNames(Args...args)
The protected interface for setting sampler names - it is only called from instances.
Definition: texture_shader.hpp:157
void setTextureHandlesImpl()
End of recursion, just checks at compile time if number of arguments is correct.
Definition: texture_shader.hpp:211
void setTextureHandles(HandlesId... ids)
The protected interface.
Definition: texture_shader.hpp:238
void setTextureHandlesImpl(uint64_t handle, HandlesId... args)
Recursive implementation of setTextureHandles.
Definition: texture_shader.hpp:223
void setTextureUnitsImpl(GLuint tex_id, TexIds... args)
The recursive implementation.
Definition: texture_shader.hpp:178
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:130
void setTextureUnitsImpl()
End of recursion, just check if number of arguments is correct.
Definition: texture_shader.hpp:169
void setTextureUnits(TexIds... args)
Public implementation of setTextureUnits.
Definition: texture_shader.hpp:198
~TextureShader()
Destructor which frees al lsampler ids.
Definition: texture_shader.hpp:247
void assignTextureNamesImpl()
End of recursive variadic template AssigTextureNames.
Definition: texture_shader.hpp:120