12int scl::gl_shader_program::CurrentlyBoundShaderId {};
14constexpr GLenum scl::gl_shader_program::GetGLShaderType(
shader_type Type)
28bool scl::gl_shader_program::CompileShader(
const shader_props &Shader, GLuint &ShaderId)
30 if (ShaderId != 0) glDeleteShader(ShaderId);
31 SCL_CORE_ASSERT((ShaderId = glCreateShader(GetGLShaderType(Shader.Type))) != 0,
32 "Error in creation OpenGL shader primitive.");
36 std::string source_str = Shader.Source;
37 const char *source = source_str.c_str();
38 char ErrorLog[1000] {};
39 glShaderSource(ShaderId, 1, &source,
nullptr);
40 glCompileShader(ShaderId);
41 glGetShaderiv(ShaderId, GL_COMPILE_STATUS, &res);
44 glGetShaderInfoLog(ShaderId,
sizeof(ErrorLog), &res, ErrorLog);
45 SCL_CORE_ERROR(
"Error in compiling shader during shader program \"{}\" creation.\nError log:\n{}", DebugName, std::string(ErrorLog));
52bool scl::gl_shader_program::CompileProgram(
const std::vector<GLuint> &Shaders)
54 SCL_CORE_ASSERT((Id = glCreateProgram()) != 0,
"Error in creation OpenGL shader program premitive.");
57 char ErrorLog[1000] {};
58 for (GLuint shader_id : Shaders) glAttachShader(Id, shader_id);
60 glGetProgramiv(Id, GL_LINK_STATUS, &res);
63 glGetProgramInfoLog(Id,
sizeof(ErrorLog), &res, ErrorLog);
64 SCL_CORE_ERROR(
"Error in linking shader program \"{}\".\nError log:\n{}", DebugName, std::string(ErrorLog));
70bool scl::gl_shader_program::Create(
const std::vector<shader_props> &Shaders)
72 u32 shaders_count = (
u32)Shaders.size();
75 std::vector<GLuint> shaders_ids(shaders_count);
76 for (
u32 i = 0; i < shaders_count; i++)
77 if (!CompileShader(Shaders[i], shaders_ids[i])) {
79 for (
auto shader_id : shaders_ids)
80 if (shader_id != 0) glDeleteShader(shader_id);
85 if (!CompileProgram(shaders_ids))
88 for (
auto shader_id : shaders_ids) {
90 if (Id != 0) glDetachShader(Id, shader_id);
91 glDeleteShader(shader_id);
94 if (Id != 0) glDeleteProgram(Id), Id = 0;
102int scl::gl_shader_program::GetOrCacheLocation(
const std::string &Name)
const
104 auto it = VariablesLocations.find(Name);
105 if (it != VariablesLocations.end())
return it->second;
107 int location = glGetUniformLocation(Id, Name.c_str());
108 VariablesLocations.emplace(Name, location);
110 SCL_CORE_WARN(
"Currently bound shader \"{}\" don't have active variable with name \"{}\".",
115bool scl::gl_shader_program::CheckIfShaderBinded()
const
117 if (Id != gl_shader_program::CurrentlyBoundShaderId)
119 if (!ShaderNotBindedErrorAlreadyShown)
121 SCL_CORE_ERROR(
"Trying set shader variable to shader \"{}\", which is not binded shader.", DebugName);
122 ShaderNotBindedErrorAlreadyShown =
true;
145 gl_shader_program::CurrentlyBoundShaderId = Id;
152 CurrentlyBoundShaderId = 0;
164 VariablesLocations.clear();
165 ShaderNotBindedErrorAlreadyShown =
false;
169 int shaders_count {};
170 GLuint shaders_ids[5] {};
171 glGetAttachedShaders(Id, 5, &shaders_count, shaders_ids);
173 for (
int i = 0; i < shaders_count; i++)
175 glDetachShader(Id, shaders_ids[i]);
176 glDeleteShader(shaders_ids[i]);
185 if (!CheckIfShaderBinded())
return false;
186 int location = GetOrCacheLocation(Name);
187 if (location == -1)
return false;
189 glUniform1i(location, Value);
195 if (!CheckIfShaderBinded())
return false;
196 int location = GetOrCacheLocation(Name);
197 if (location == -1)
return false;
199 glUniform1f(location, Value);
205 if (!CheckIfShaderBinded())
return false;
206 int location = GetOrCacheLocation(Name);
207 if (location == -1)
return false;
209 glUniform2fv(location, 1, (
float *)&Value);
215 if (!CheckIfShaderBinded())
return false;
216 int location = GetOrCacheLocation(Name);
217 if (location == -1)
return false;
219 glUniform3fv(location, 1, (
float *)&Value);
225 if (!CheckIfShaderBinded())
return false;
226 int location = GetOrCacheLocation(Name);
227 if (location == -1)
return false;
229 glUniform4fv(location, 1, (
float *)&Value);
235 if (!CheckIfShaderBinded())
return false;
236 int location = GetOrCacheLocation(Name);
237 if (location == -1)
return false;
239 glUniform1i(location, Value);
245 if (!CheckIfShaderBinded())
return false;
246 int location = GetOrCacheLocation(Name);
247 if (location == -1)
return false;
249 glUniform2iv(location, 1, (
int *)&Value);
255 if (!CheckIfShaderBinded())
return false;
256 int location = GetOrCacheLocation(Name);
257 if (location == -1)
return false;
259 glUniform3iv(location, 1, (
int *)&Value);
265 if (!CheckIfShaderBinded())
return false;
266 int location = GetOrCacheLocation(Name);
267 if (location == -1)
return false;
269 glUniform4iv(location, 1, (
int *)&Value);
275 if (!CheckIfShaderBinded())
return false;
276 int location = GetOrCacheLocation(Name);
277 if (location == -1)
return false;
279 glUniform1ui(location, Value);
285 if (!CheckIfShaderBinded())
return false;
286 int location = GetOrCacheLocation(Name);
287 if (location == -1)
return false;
289 glUniform2uiv(location, 1, (
u32 *)&Value);
295 if (!CheckIfShaderBinded())
return false;
296 int location = GetOrCacheLocation(Name);
297 if (location == -1)
return false;
299 glUniform3uiv(location, 1, (
u32 *)&Value);
305 if (!CheckIfShaderBinded())
return false;
306 int location = GetOrCacheLocation(Name);
307 if (location == -1)
return false;
309 glUniform4uiv(location, 1, (
u32 *)&Value);
315 if (!CheckIfShaderBinded())
return false;
316 int location = GetOrCacheLocation(Name);
317 if (location == -1)
return false;
319 glUniformMatrix3fv(location, 1, FALSE, (
float *)&Value);
325 if (!CheckIfShaderBinded())
return false;
326 int location = GetOrCacheLocation(Name);
327 if (location == -1)
return false;
329 glUniformMatrix4fv(location, 1, FALSE, (
float *)&Value);
335 if (!CheckIfShaderBinded())
return false;
336 glUniform1i(Location, Value);
342 if (!CheckIfShaderBinded())
return false;
343 glUniform1f(Location, Value);
349 if (!CheckIfShaderBinded())
return false;
350 glUniform2fv(Location, 1, (
float *)&Value);
356 if (!CheckIfShaderBinded())
return false;
357 glUniform3fv(Location, 1, (
float *)&Value);
363 if (!CheckIfShaderBinded())
return false;
364 glUniform4fv(Location, 1, (
float *)&Value);
370 if (!CheckIfShaderBinded())
return false;
371 glUniform1i(Location, Value);
377 if (!CheckIfShaderBinded())
return false;
378 glUniform2iv(Location, 1, (
int *)&Value);
384 if (!CheckIfShaderBinded())
return false;
385 glUniform3iv(Location, 1, (
int *)&Value);
391 if (!CheckIfShaderBinded())
return false;
392 glUniform4iv(Location, 1, (
int *)&Value);
398 if (!CheckIfShaderBinded())
return false;
399 glUniform1ui(Location, Value);
405 if (!CheckIfShaderBinded())
return false;
406 glUniform2uiv(Location, 1, (
u32 *)&Value);
412 if (!CheckIfShaderBinded())
return false;
413 glUniform3uiv(Location, 1, (
u32 *)&Value);
419 if (!CheckIfShaderBinded())
return false;
420 glUniform4uiv(Location, 1, (
u32 *)&Value);
426 if (!CheckIfShaderBinded())
return false;
427 glUniformMatrix3fv(Location, 1, FALSE, (
float *)&Value);
433 if (!CheckIfShaderBinded())
return false;
434 glUniformMatrix4fv(Location, 1, FALSE, (
float *)&Value);
#define SCL_CORE_ASSERT(expr,...)
bool SetInt(const std::string &Name, int Value) const override
void Bind() const override
bool SetInt3(const std::string &Name, const ivec3 &Value) const override
bool SetInt4(const std::string &Name, const ivec4 &Value) const override
gl_shader_program(const std::vector< shader_props > &Shaders, const std::string &DebugName)
bool SetBool(const std::string &Name, bool Value) const override
bool SetUInt2(const std::string &Name, const uvec2 &Value) const override
void Unbind() const override
bool SetFloat4(const std::string &Name, const vec4 &Value) const override
bool SetUInt3(const std::string &Name, const uvec3 &Value) const override
bool SetMatr4(const std::string &Name, const matr4 &Value) const override
void Update(const std::vector< shader_props > &Shaders) override
bool SetMatr3(const std::string &Name, const matr3 &Value) const override
bool SetFloat(const std::string &Name, float Value) const override
bool SetFloat3(const std::string &Name, const vec3 &Value) const override
bool SetFloat2(const std::string &Name, const vec2 &Value) const override
bool SetUInt4(const std::string &Name, const uvec4 &Value) const override
bool SetInt2(const std::string &Name, const ivec2 &Value) const override
bool SetUInt(const std::string &Name, u32 Value) const override
OpenGL shader program class definition module.
#define SCL_CORE_SUCCES(...)
#define SCL_CORE_WARN(...)
#define SCL_CORE_INFO(...)
#define SCL_CORE_ERROR(...)
Sculpto library prehompiled header. Defines common definitions, includes commonly used modules.