Shader program cleanup

This commit is contained in:
Dan Paulat 2022-10-02 22:28:56 -05:00
parent 7a07e0f698
commit 8d2fcf3802
2 changed files with 14 additions and 16 deletions

View file

@ -13,19 +13,18 @@ namespace gl
static const std::string logPrefix_ = "scwx::qt::gl::shader_program"; static const std::string logPrefix_ = "scwx::qt::gl::shader_program";
static const auto logger_ = scwx::util::Logger::Create(logPrefix_); static const auto logger_ = scwx::util::Logger::Create(logPrefix_);
static constexpr GLsizei INFO_LOG_BUF_SIZE = 512; static constexpr GLsizei kInfoLogBufSize = 512;
class ShaderProgramImpl class ShaderProgram::Impl
{ {
public: public:
explicit ShaderProgramImpl(OpenGLFunctions& gl) : explicit Impl(OpenGLFunctions& gl) : gl_(gl), id_ {GL_INVALID_INDEX}
gl_(gl), id_ {GL_INVALID_INDEX}
{ {
// Create shader program // Create shader program
id_ = gl_.glCreateProgram(); id_ = gl_.glCreateProgram();
} }
~ShaderProgramImpl() ~Impl()
{ {
// Delete shader program // Delete shader program
gl_.glDeleteProgram(id_); gl_.glDeleteProgram(id_);
@ -37,12 +36,12 @@ public:
}; };
ShaderProgram::ShaderProgram(OpenGLFunctions& gl) : ShaderProgram::ShaderProgram(OpenGLFunctions& gl) :
p(std::make_unique<ShaderProgramImpl>(gl)) p(std::make_unique<Impl>(gl))
{ {
} }
ShaderProgram::~ShaderProgram() = default; ShaderProgram::~ShaderProgram() = default;
ShaderProgram::ShaderProgram(ShaderProgram&&) noexcept = default; ShaderProgram::ShaderProgram(ShaderProgram&&) noexcept = default;
ShaderProgram& ShaderProgram::operator=(ShaderProgram&&) noexcept = default; ShaderProgram& ShaderProgram::operator=(ShaderProgram&&) noexcept = default;
GLuint ShaderProgram::id() const GLuint ShaderProgram::id() const
@ -59,7 +58,7 @@ bool ShaderProgram::Load(const std::string& vertexPath,
GLint glSuccess; GLint glSuccess;
bool success = true; bool success = true;
char infoLog[INFO_LOG_BUF_SIZE]; char infoLog[kInfoLogBufSize];
GLsizei logLength; GLsizei logLength;
QFile vertexFile(vertexPath.c_str()); QFile vertexFile(vertexPath.c_str());
@ -102,7 +101,7 @@ bool ShaderProgram::Load(const std::string& vertexPath,
// Check for errors // Check for errors
gl.glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &glSuccess); gl.glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &glSuccess);
gl.glGetShaderInfoLog(vertexShader, INFO_LOG_BUF_SIZE, &logLength, infoLog); gl.glGetShaderInfoLog(vertexShader, kInfoLogBufSize, &logLength, infoLog);
if (!glSuccess) if (!glSuccess)
{ {
logger_->error("Vertex shader compilation failed: {}", infoLog); logger_->error("Vertex shader compilation failed: {}", infoLog);
@ -122,8 +121,7 @@ bool ShaderProgram::Load(const std::string& vertexPath,
// Check for errors // Check for errors
gl.glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &glSuccess); gl.glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &glSuccess);
gl.glGetShaderInfoLog( gl.glGetShaderInfoLog(fragmentShader, kInfoLogBufSize, &logLength, infoLog);
fragmentShader, INFO_LOG_BUF_SIZE, &logLength, infoLog);
if (!glSuccess) if (!glSuccess)
{ {
logger_->error("Fragment shader compilation failed: {}", infoLog); logger_->error("Fragment shader compilation failed: {}", infoLog);
@ -142,7 +140,7 @@ bool ShaderProgram::Load(const std::string& vertexPath,
// Check for errors // Check for errors
gl.glGetProgramiv(p->id_, GL_LINK_STATUS, &glSuccess); gl.glGetProgramiv(p->id_, GL_LINK_STATUS, &glSuccess);
gl.glGetProgramInfoLog(p->id_, INFO_LOG_BUF_SIZE, &logLength, infoLog); gl.glGetProgramInfoLog(p->id_, kInfoLogBufSize, &logLength, infoLog);
if (!glSuccess) if (!glSuccess)
{ {
logger_->error("Shader program link failed: {}", infoLog); logger_->error("Shader program link failed: {}", infoLog);

View file

@ -16,15 +16,13 @@ namespace qt
namespace gl namespace gl
{ {
class ShaderProgramImpl;
class ShaderProgram class ShaderProgram
{ {
public: public:
explicit ShaderProgram(OpenGLFunctions& gl); explicit ShaderProgram(OpenGLFunctions& gl);
virtual ~ShaderProgram(); virtual ~ShaderProgram();
ShaderProgram(const ShaderProgram&) = delete; ShaderProgram(const ShaderProgram&) = delete;
ShaderProgram& operator=(const ShaderProgram&) = delete; ShaderProgram& operator=(const ShaderProgram&) = delete;
ShaderProgram(ShaderProgram&&) noexcept; ShaderProgram(ShaderProgram&&) noexcept;
@ -37,7 +35,9 @@ public:
void Use() const; void Use() const;
private: private:
std::unique_ptr<ShaderProgramImpl> p; class Impl;
std::unique_ptr<Impl> p;
}; };
} // namespace gl } // namespace gl