18#ifndef HEADER_SP_SHADER_HPP
19#define HEADER_SP_SHADER_HPP
21#include "graphics/gl_headers.hpp"
22#include "graphics/sp/sp_per_object_uniform.hpp"
23#include "utils/log.hpp"
24#include "utils/no_copy.hpp"
35#include <unordered_map>
41enum SamplerType:
unsigned int
55enum RenderPass:
unsigned int
63inline std::ostream& operator<<(std::ostream& os,
const RenderPass& rp)
68 return os <<
"first pass";
70 return os <<
"shadow pass";
72 return os <<
"reserved pass";
78class SPUniformAssigner;
85 std::vector<std::shared_ptr<GLuint> > m_shader_files;
87 GLuint m_program[RP_COUNT];
89 std::map<unsigned, unsigned> m_samplers[RP_COUNT];
91 std::vector<std::tuple<unsigned, std::string, SamplerType,
92 GLuint> >m_prefilled_samplers[RP_COUNT];
94 std::unordered_map<std::string, SPUniformAssigner*> m_uniforms[RP_COUNT];
96 std::unordered_map<std::string, std::function<GLuint()> >
97 m_custom_prefilled_getter[RP_COUNT];
99 std::function<void()> m_use_function[RP_COUNT], m_unuse_function[RP_COUNT];
101 const std::function<void(
SPShader*)> m_init_function;
103 const int m_drawing_priority;
105 const bool m_transparent_shader;
107 const bool m_use_alpha_channel;
109 const bool m_use_tangents;
111 const std::array<bool, 6> m_srgb;
115 static bool m_sp_shader_debug;
116 static std::map<std::string, std::pair<unsigned, SamplerType> >
120 const std::function<
void(
SPShader*)>& init_func,
121 bool transparent_shader =
false,
int drawing_priority = 0,
122 bool use_alpha_channel =
false,
bool use_tangents =
false,
123 const std::array<bool, 6>& srgb =
124 {{
true,
true,
false,
false,
false,
false }});
131 bool hasShader(RenderPass rp) {
return m_program[rp] != 0; }
133 GLuint getShaderProgram(RenderPass rp) {
return m_program[rp]; }
135 void use(RenderPass rp = RP_1ST)
137 if (m_use_function[rp] != NULL)
139 m_use_function[rp]();
142 glUseProgram(m_program[rp]);
146 void unuse(RenderPass rp = RP_1ST)
148 if (m_unuse_function[rp] != NULL)
150 m_unuse_function[rp]();
154 void addShaderFile(
const std::string& name,
155 GLint shader_type, RenderPass rp = RP_1ST);
157 void linkShaderFiles(RenderPass rp = RP_1ST);
159 void addAllTextures(RenderPass rp = RP_1ST);
161 void addAllUniforms(RenderPass rp = RP_1ST);
163 void addCustomPrefilledTextures(SamplerType st, GLuint texture_type,
164 const std::string& name,
165 std::function<GLuint()> func,
166 RenderPass rp = RP_1ST);
168 void bindPrefilledTextures(RenderPass rp = RP_1ST)
const;
170 void bindTextures(
const std::array<GLuint, 6>& tex,
171 RenderPass rp = RP_1ST)
const;
173 void addBasicUniforms(RenderPass rp = RP_1ST)
177 GLuint block_index = glGetUniformBlockIndex(m_program[rp],
179 if (block_index != GL_INVALID_INDEX)
180 glUniformBlockBinding(m_program[rp], block_index, 0);
181 block_index = glGetUniformBlockIndex(m_program[rp],
"SPFogData");
182 if (block_index != GL_INVALID_INDEX)
183 glUniformBlockBinding(m_program[rp], block_index, 2);
187 const std::string& getName()
const {
return m_name; }
190 RenderPass rp = RP_1ST)
const;
193 std::vector<SPUniformAssigner*>* ua_used,
194 RenderPass rp = RP_1ST);
196 void setUseFunction(std::function<
void()> func, RenderPass rp = RP_1ST)
198 m_use_function[rp] = func;
201 void setUnuseFunction(std::function<
void()> func, RenderPass rp = RP_1ST)
203 m_unuse_function[rp] = func;
206 bool isTransparent()
const {
return m_transparent_shader; }
208 bool useAlphaChannel()
const {
return m_use_alpha_channel; }
210 int getDrawingPriority()
const {
return m_drawing_priority; }
212 bool samplerLess(RenderPass rp = RP_1ST)
const
213 {
return m_samplers[rp].empty(); }
219 if (!m_shader_files.empty())
223 m_init_function(
this);
226 bool isSrgbForTextureLayer(
unsigned layer)
const;
228 bool useTangents()
const {
return m_use_tangents; }
230 bool hasTextureLayer(
unsigned layer)
232 for (
unsigned rp = RP_1ST; rp < RP_COUNT; rp++)
234 if (m_samplers[rp].find(layer) != m_samplers[rp].end())
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Definition: sp_shader.hpp:81