From c3eea71370eb94cff1fd96185458643fab6eb2c5 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 3 Nov 2022 19:47:46 +0000 Subject: Restructure how shaders are worked with Needs a tidy-up --- gfx/gl/shader.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 gfx/gl/shader.cpp (limited to 'gfx/gl/shader.cpp') diff --git a/gfx/gl/shader.cpp b/gfx/gl/shader.cpp new file mode 100644 index 0000000..8b0f614 --- /dev/null +++ b/gfx/gl/shader.cpp @@ -0,0 +1,41 @@ +#include "shader.h" +#include +#include +#include + +Shader::operator GLuint() const +{ + if (!shader) { + shader.emplace(type); + glShaderSource(*shader, 1, &text, &len); + glCompileShader(*shader); + + CheckShaderError(*shader, GL_COMPILE_STATUS, false, "Error compiling shader!"); + } + return *shader; +} + +void +Shader::CheckShaderError(GLuint shader, GLuint flag, bool isProgram, std::string_view errorMessage) +{ + GLint success = 0; + + if (isProgram) { + glGetProgramiv(shader, flag, &success); + } + else { + 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()); + } + + throw std::runtime_error {std::string {errorMessage} + ": '" + std::string {error.data(), error.size()} + "'"}; + } +} -- cgit v1.2.3