From 969999f9858043d2a2053f93209f878a3eb5a02a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 3 Aug 2024 12:23:54 +0100 Subject: Split CheckShaderError into shader/program versions They're similar, but need splitting for better diagnostics --- gfx/gl/program.cpp | 19 +++++++++++++++++-- gfx/gl/program.h | 1 + gfx/gl/shader.cpp | 18 ++++-------------- gfx/gl/shader.h | 6 ++++-- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/gfx/gl/program.cpp b/gfx/gl/program.cpp index 7287fde..fdd4c6f 100644 --- a/gfx/gl/program.cpp +++ b/gfx/gl/program.cpp @@ -10,10 +10,10 @@ void Program::linkAndValidate() const { glLinkProgram(m_program); - Shader::CheckShaderError(m_program, GL_LINK_STATUS, true, "Error linking shader program"); + checkProgramError(m_program, GL_LINK_STATUS, "Error linking shader program"); glValidateProgram(m_program); - Shader::CheckShaderError(m_program, GL_VALIDATE_STATUS, true, "Invalid shader program"); + checkProgramError(m_program, GL_VALIDATE_STATUS, "Invalid shader program"); } void @@ -22,6 +22,21 @@ Program::use() const glUseProgram(m_program); } +void +Program::checkProgramError(GLuint program, GLuint flag, std::string_view errorMessage) const +{ + GLint success = 0; + + glGetProgramiv(program, flag, &success); + + if (success == GL_FALSE) { + std::array error {}; + glGetProgramInfoLog(program, error.size(), nullptr, error.data()); + + throw std::runtime_error {std::format("{}: '{}'", errorMessage, error.data())}; + } +} + Program::UniformLocation::UniformLocation(GLuint program, const char * name) : location {glGetUniformLocation(program, name)} { diff --git a/gfx/gl/program.h b/gfx/gl/program.h index fa6dac9..c89a128 100644 --- a/gfx/gl/program.h +++ b/gfx/gl/program.h @@ -49,6 +49,7 @@ public: } protected: + void checkProgramError(GLuint program, GLuint flag, std::string_view errorMessage) const; void use() const; void linkAndValidate() const; ProgramRef m_program; diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp index 0bc127a..47d36d0 100644 --- a/gfx/gl/shader.cpp +++ b/gfx/gl/shader.cpp @@ -46,30 +46,20 @@ Shader::compile() const } glCompileShader(shader); - CheckShaderError(shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); + checkShaderError(shader, GL_COMPILE_STATUS, "Error compiling shader!"); return shader; } void -Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage) +Shader::checkShaderError(GLuint shader, GLuint flag, std::string_view errorMessage) const { GLint success = 0; - if (isProgram) { - glGetProgramiv(shader, flag, &success); - } - else { - glGetShaderiv(shader, flag, &success); - } + glGetShaderiv(shader, flag, &success); if (success == GL_FALSE) { std::array error {}; - if (isProgram) { - glGetProgramInfoLog(shader, error.size(), nullptr, error.data()); - } - else { - glGetShaderInfoLog(shader, error.size(), nullptr, error.data()); - } + glGetShaderInfoLog(shader, error.size(), nullptr, error.data()); throw std::runtime_error {std::format("{}: '{}'", errorMessage, error.data())}; } diff --git a/gfx/gl/shader.h b/gfx/gl/shader.h index c6b45af..1e4a2ee 100644 --- a/gfx/gl/shader.h +++ b/gfx/gl/shader.h @@ -15,10 +15,12 @@ public: } [[nodiscard]] ShaderRef compile() const; - static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage); private: - const std::basic_string_view text; + using Source = std::basic_string_view; + + void checkShaderError(GLuint shader, GLuint flag, std::string_view errorMessage) const; + const Source text; GLuint type; bool lookups; }; -- cgit v1.2.3