Skip to content

Commit

Permalink
Add line numbers to shader error output
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Nov 24, 2024
1 parent 2452d8f commit 14a0481
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 73 deletions.
24 changes: 19 additions & 5 deletions include/jGL/OpenGL/gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,28 @@
namespace jGL::GL
{

class GLRuntimeException;
class GLRuntimeException: public std::exception
{

public:

GLRuntimeException(std::string msg)
: msg(msg)
{}

private:

virtual const char * what() const throw()
{
return msg.c_str();
}

std::string msg;

};
// print buffer status errors
GLuint glBufferStatus(const std::string msg = "");
// print gl error codes
GLuint glError(const std::string msg = "");

// compile a gl shader given a program and source code as const char *
void compileShader(GLuint & shaderProgram, const char * vert, const char * frag);

}
#endif
25 changes: 25 additions & 0 deletions include/jGL/shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ namespace jGL

virtual void use() = 0;

std::string displayVertexSource() const { return formatWithLineNumbers(vertex); }
std::string displayFragmentSource() const { return formatWithLineNumbers(fragment); }

protected:

std::string vertex;
Expand All @@ -137,6 +140,28 @@ namespace jGL
virtual void compile() = 0;

bool parseUniforms();

std::string formatWithLineNumbers(std::string shader) const
{
if (shader.length() == 0) { return shader; }
std::string source = "1: ";
uint32_t line = 1;
auto iter = shader.begin();
while (iter != shader.end())
{
if (*iter == '\n' && iter+1 != shader.end())
{
line += 1;
source += "\n" + std::to_string(line)+": ";
}
else
{
source += *iter;
}
iter++;
}
return source;
}

template <class T>
void detectUniformsAndCreate(std::string code)
Expand Down
64 changes: 63 additions & 1 deletion src/jGL/OpenGL/Shader/glShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,69 @@ namespace jGL::GL

parseUniforms();

compileShader(program,vertex.c_str(),fragment.c_str());
GLuint vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
auto v = vertex.c_str();
glShaderSource(vertexShader,1,&v,NULL);
glCompileShader(vertexShader);

// check it worked!
int success;
const unsigned logSize = 512*4;
char infoLog[logSize];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

if(!success)
{
glGetShaderInfoLog(vertexShader, logSize, NULL, infoLog);
throw
(
GLRuntimeException
(
std::string("GLSL (VERTEX) ERROR: \n") + infoLog + "\n"+displayVertexSource()+"\n"
)
);
}

GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
auto f = fragment.c_str();
glShaderSource(fragmentShader,1,&f,NULL);
glCompileShader(fragmentShader);

glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);

if(!success)
{
glGetShaderInfoLog(fragmentShader, logSize, NULL, infoLog);
throw
(
GLRuntimeException
(
std::string("GLSL (FRAGMENT) ERROR: \n") + infoLog + "\n"+displayFragmentSource()+"\n"
)
);
}

glAttachShader(program,vertexShader);
glAttachShader(program,fragmentShader);
glLinkProgram(program);

// check it linked
glGetProgramiv(program, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(program, logSize, NULL, infoLog);
throw
(
GLRuntimeException
(
std::string("GLSL (LINK) ERROR: \n") + infoLog + "\n"+displayVertexSource()+"\n"+displayFragmentSource()+"\n"
)
);
}
glGetProgramInfoLog(program, logSize, NULL, infoLog);

compiled = true;

for (auto uniform = uniforms.cbegin(); uniform != uniforms.cend(); uniform++)
Expand Down
67 changes: 0 additions & 67 deletions src/jGL/OpenGL/gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@

namespace jGL::GL
{

class GLRuntimeException: public std::exception
{

public:

GLRuntimeException(std::string msg)
: msg(msg)
{}

private:

virtual const char * what() const throw()
{
return msg.c_str();
}

std::string msg;

};

// print buffer status errors
GLuint glBufferStatus(const std::string msg)
{
Expand Down Expand Up @@ -79,50 +58,4 @@ namespace jGL::GL
return e;
}

// compile a gl shader given a program and source code as const char *
void compileShader(GLuint & shaderProgram, const char * vert, const char * frag)
{
GLuint vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader,1,&vert,NULL);
glCompileShader(vertexShader);

// check it worked!
int success;
const unsigned logSize = 512*4;
char infoLog[logSize];
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);

if(!success)
{
glGetShaderInfoLog(vertexShader, logSize, NULL, infoLog);
throw( GLRuntimeException( std::string("GLSL (VERTEX) ERROR: \n") + infoLog + "\n"+vert+"\n") );
}

GLuint fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader,1,&frag,NULL);
glCompileShader(fragmentShader);

glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);

if(!success)
{
glGetShaderInfoLog(fragmentShader, logSize, NULL, infoLog);
throw( GLRuntimeException( std::string("GLSL (FRAGMENT) ERROR: \n") + infoLog +"\n"+frag+"\n") );
}

glAttachShader(shaderProgram,vertexShader);
glAttachShader(shaderProgram,fragmentShader);
glLinkProgram(shaderProgram);

// check it linked
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(shaderProgram, logSize, NULL, infoLog);
throw( GLRuntimeException( std::string("GLSL (LINK) ERROR: \n") + infoLog + "\n"+vert+"\n"+frag+"\n") );
}
glGetProgramInfoLog(shaderProgram, logSize, NULL, infoLog);
}
}

0 comments on commit 14a0481

Please sign in to comment.