30 #ifdef E3D_BACKEND_OPENGL
33 #elif defined E3D_BACKEND_GLES
34 # include <GLES3/gl3.h>
35 # include <GLES3/gl31.h>
38 #include "eng3d/shader.hpp"
39 #include "eng3d/glsl_trans.hpp"
40 #include "eng3d/utils.hpp"
41 #include "eng3d/log.hpp"
43 #if defined E3D_BACKEND_OPENGL || defined E3D_BACKEND_GLES
49 if(use_transpiler)
try {
51 ctx.defines = defintions;
54 buffer = ctx.to_text();
55 line_numbers = ctx.line_numbers;
60 id = glCreateShader(type);
63 const char* c_code = buffer.c_str();
64 glShaderSource(
id, 1, &c_code, NULL);
69 void Eng3D::OpenGL::Shader::compile(GLuint) {
74 glGetShaderiv(
id, GL_COMPILE_STATUS, &r);
77 glGetShaderiv(
id, GL_INFO_LOG_LENGTH, &infoLen);
78 std::string shader_error_info;
79 shader_error_info.resize(infoLen);
80 glGetShaderInfoLog(
id, infoLen, NULL, &shader_error_info[0]);
82 std::ostringstream output_error;
83 output_error <<
"Shader did not compile:\n";
84 std::istringstream error_lines(shader_error_info);
86 getline(error_lines,
error);
87 while(!error_lines.eof()) {
88 std::istringstream sline(
error);
103 }
else if(ch ==
'(') {
107 size_t read_lines = 0;
108 std::stringstream code(buffer);
109 std::string error_code;
110 while(read_lines < row && !code.eof()) {
111 getline(code, error_code);
115 getline(sline,
error);
116 if(row > 0 && row - 1 < line_numbers.size())
117 row = line_numbers[row - 1];
119 output_error <<
"(" << row <<
")" <<
":" << error_code;
120 output_error <<
"\n^^^: " <<
error <<
"\n";
121 getline(error_lines,
error);
132 glDeleteShader(this->get_id());
143 :
Eng3D::OpenGL::Shader(_buffer, GL_VERTEX_SHADER)
152 :
Eng3D::OpenGL::Shader(_buffer, GL_FRAGMENT_SHADER, use_transpiler, defintions)
157 #if !defined E3D_BACKEND_GLES
162 :
Eng3D::OpenGL::Shader(_buffer, GL_GEOMETRY_SHADER)
171 :
Eng3D::OpenGL::Shader(_buffer, GL_TESS_CONTROL_SHADER)
180 :
Eng3D::OpenGL::Shader(_buffer, GL_TESS_EVALUATION_SHADER)
190 id = glCreateProgram();
193 glBindAttribLocation(
id, 0,
"m_pos");
194 glBindAttribLocation(
id, 1,
"m_texcoord");
209 glGetProgramiv(
id, GL_LINK_STATUS, &r);
212 glGetProgramiv(
id, GL_INFO_LOG_LENGTH, &infoLen);
213 std::string shader_error_info;
214 shader_error_info.resize(infoLen);
215 glGetShaderInfoLog(
id, infoLen, NULL, &shader_error_info[0]);
224 glAttachShader(
id, shader.
get_id());
234 set_uniform(
"projection", projection);
235 set_uniform(
"view", view);
236 set_uniform(
"model", model);
240 glProgramUniformMatrix4fv(
id, glGetUniformLocation(
id, name.c_str()), 1, GL_FALSE, glm::value_ptr(uniform));
244 glProgramUniform2f(
id, glGetUniformLocation(
id, name.c_str()), value1, value2);
248 glProgramUniform3f(
id, glGetUniformLocation(
id, name.c_str()), value1, value2, value3);
252 set_uniform(name, uniform.x, uniform.y);
256 set_uniform(name, uniform.x, uniform.y, uniform.z);
260 set_uniform(name, uniform.x, uniform.y, uniform.z, uniform.w);
264 glProgramUniform4f(
id, glGetUniformLocation(
id, name.c_str()), value1, value2, value3, value4);
268 glProgramUniform1f(
id, glGetUniformLocation(
id, name.c_str()), value);
272 glProgramUniform1i(
id, glGetUniformLocation(
id, name.c_str()), value);
277 glActiveTexture(GL_TEXTURE0 + value);
278 set_uniform(name, value);
279 glBindTexture(GL_TEXTURE_2D, texture.
id);
283 glActiveTexture(GL_TEXTURE0 + value);
284 set_uniform(name, value);
285 glBindTexture(GL_TEXTURE_2D_ARRAY, texture.
id);
std::vector< Eng3D::GLSL::Token >::iterator it
virtual const char * what() const noexcept
FragmentShader(const std::string &_buffer, bool use_transpiler=true, std::vector< Eng3D::GLSL::Define > defintions={})
GeometryShader(const std::string &_buffer)
void set_texture(int value, const std::string &name, const Eng3D::Texture &texture) const
void attach_shader(const Eng3D::OpenGL::Shader &shader)
void set_uniform(const std::string &name, glm::mat4 uniform) const
unsigned int get_id() const
void set_PVM(glm::mat4 projection, glm::mat4 view, glm::mat4 model) const
unsigned int get_id() const
Shader(const std::string &_buffer, unsigned int type, bool use_transpiler=true, std::vector< Eng3D::GLSL::Define > defintions={})
TessControlShader(const std::string &_buffer)
TessEvalShader(const std::string &_buffer)
VertexShader(const std::string &_buffer)
void error(const std::string_view category, const std::string_view msg)
void debug(const std::string_view category, const std::string_view msg)
std::string translate_format(const std::string_view format, Args &&... args)
String formatter, with translation.
#define CXX_THROW(class,...)