From 81f3e1e39979a51cdcfaa653a12e107cb8e48c1b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Sun, 18 Sep 2022 13:56:37 +0200 Subject: [PATCH] Graphics: Remove Shader and ShaderProgram classes. they are combined into GfxDriver/ShaderProgram. --- engine.build.lua | 2 - include/Spectre/Graphics/Shader.h | 58 ------- include/Spectre/Graphics/ShaderProgram.h | 90 ----------- source/Graphics/Shader.cpp | 104 ------------ source/Graphics/ShaderProgram.cpp | 197 ----------------------- 5 files changed, 451 deletions(-) delete mode 100644 include/Spectre/Graphics/Shader.h delete mode 100644 include/Spectre/Graphics/ShaderProgram.h delete mode 100644 source/Graphics/Shader.cpp delete mode 100644 source/Graphics/ShaderProgram.cpp diff --git a/engine.build.lua b/engine.build.lua index fb19659..18fe860 100644 --- a/engine.build.lua +++ b/engine.build.lua @@ -113,8 +113,6 @@ local graphics_module = Module("source/Graphics", { "Renderable2D.cpp", "Renderer2D.cpp", "RenderState.cpp", - "Shader.cpp", - "ShaderProgram.cpp", "Texture.cpp", -- Text diff --git a/include/Spectre/Graphics/Shader.h b/include/Spectre/Graphics/Shader.h deleted file mode 100644 index 66e9a5a..0000000 --- a/include/Spectre/Graphics/Shader.h +++ /dev/null @@ -1,58 +0,0 @@ - -#ifndef SPECTRE_GRAPHICS_SHADER_H -#define SPECTRE_GRAPHICS_SHADER_H - -#include - -namespace sp { - -class Shader -{ -public : - enum Type { - Vertex, - Fragment - }; - - Shader(Type type, const std::string& name = ""); - ~Shader(); - - unsigned int getHandle() const; - - const std::string& getName() const; - - // Load shader from file. - bool loadFromFile(const std::string& file); - - // Load shader from memory - bool loadFromMemory(const std::string& source); - - const std::string& getError() const; - - // Is this shader compiled? - bool isCompiled() const; - -protected : - - // Compile the shader. - // Returns true if the shader compiled without errors. false otherwise. - bool compile(); - - std::string fetchErrorLog(); - -protected : - - // Type of shader. Vertex, fragment, geometry etc. - Type m_type; - - unsigned int m_handle; // Shader id. - - // A name for the shader (usually the filename if loaded from file). - std::string m_name; - - std::string m_error; -}; - -} // namespace sp - -#endif /* SPECTRE_GRAPHICS_SHADER_H */ diff --git a/include/Spectre/Graphics/ShaderProgram.h b/include/Spectre/Graphics/ShaderProgram.h deleted file mode 100644 index f11d769..0000000 --- a/include/Spectre/Graphics/ShaderProgram.h +++ /dev/null @@ -1,90 +0,0 @@ - -#ifndef SPECTRE_GRAPHICS_SHADER_PROGRAM_H -#define SPECTRE_GRAPHICS_SHADER_PROGRAM_H - -#include -#include -#include "Shader.h" - -namespace sp { - -struct Color; - -class ShaderProgram -{ -public : - ShaderProgram(); - ~ShaderProgram(); - - void create(); - - void destroy(); - - void addShader(const Shader& shader); - - // Load a shader program from file. - // - // The following naming conventions are supported: - // .glsl - Virtual file, will load all real shader files with the same name. - // .vert.glsl - Vertex shader. - // .frag.glsl - Fragment shader. - bool loadFromFile(const std::string& filename); - - // Load a shader from file. - bool loadFromFile(const std::string& filename, Shader::Type type); - - // Load a shader from memory. - bool loadFromMemory(const std::string& source, Shader::Type type); - - // Link the program. - bool link(); - - // Returns true if the program was linked successfully - bool isLinked() const; - - // Enable this shader - // All shader operations after this call will affect this shader. - void enable() const; - - // Disable this shader - // Operations will no longer affect this shader after this call. - void disable() const; - - // Get the last shader error. - std::string getLastError() const; - - // --------------------- - // Variables. - // --------------------- - - bool setMVPMatrix(const Matrix4f& matrix); - - // --------------------- - // General Variables. - // --------------------- - - bool setAttribute(const std::string& name, float value) const; - - bool setAttribute(const std::string& name, const Matrix4f& matrix) const; - - bool setUniform(const std::string& name, const Matrix4f& matrix) const; - - bool setUniform(const std::string& name, const Color& color) const; - -protected : - - bool getAttribLoc(const std::string& name, int& loc) const; - bool getUniformLoc(const std::string& name, int& loc) const; - - std::string fetchErrorLog(); - -protected : - - unsigned int m_id; // program id - - mutable std::string m_error; -}; - -} // namespace sp - -#endif /* SPECTRE_GRAPHICS_SHADER_PROGRAM_H */ diff --git a/source/Graphics/Shader.cpp b/source/Graphics/Shader.cpp deleted file mode 100644 index e24eb1f..0000000 --- a/source/Graphics/Shader.cpp +++ /dev/null @@ -1,104 +0,0 @@ - -#include -#include -#include -#include - -namespace sp { - -Shader::Shader(Type type, const std::string& name) : -m_name (name) -{ - GLenum internal_type = type == Vertex ? GL_VERTEX_SHADER : GL_FRAGMENT_SHADER; - m_handle = glCreateShader(internal_type); -} - -Shader::~Shader() -{ - if (m_handle) { - glDeleteShader(m_handle); - } -} - -unsigned int Shader::getHandle() const -{ - return m_handle; -} - -const std::string& Shader::getName() const -{ - return m_name; -} - -bool Shader::loadFromFile(const std::string& filename) -{ - std::string src; - File file; - - // If this shader does not have a name, pick the filename. - if (m_name.length() < 1) { - m_name = Path::getBasename(filename); - } - - if (!file.open(filename)) { - m_error = "Can't open file: " + filename; - return false; - } - - // Load file into memory. - if (!file.read(src)) { - m_error = "Could not read file: " + filename; - return false; - } - - return loadFromMemory(src); -} - -bool Shader::loadFromMemory(const std::string& source) -{ - const char *s = source.c_str(); - - glShaderSource(m_handle, 1, &s, NULL); - - return compile(); -} - -const std::string& Shader::getError() const -{ - return m_error; -} - -bool Shader::isCompiled() const -{ - // A shader without handle is not compiled. - if (m_handle) { - - // Query OpenGL for status. - GLint status; - glGetShaderiv(m_handle, GL_COMPILE_STATUS, &status); - return status == GL_TRUE; - } - return false; -} - -bool Shader::compile() -{ - glCompileShader(m_handle); - - if (!isCompiled()) { - m_error = fetchErrorLog(); - return false; - } - return true; -} - -std::string Shader::fetchErrorLog() -{ - GLchar buf[4096] = { '\0' }; - - glGetShaderInfoLog(m_handle, sizeof(buf), NULL, buf); - - return std::string(buf); -} - -} // namespace sp diff --git a/source/Graphics/ShaderProgram.cpp b/source/Graphics/ShaderProgram.cpp deleted file mode 100644 index 0a0da3f..0000000 --- a/source/Graphics/ShaderProgram.cpp +++ /dev/null @@ -1,197 +0,0 @@ - -#include -#include -#include -#include - -namespace sp { - -ShaderProgram::ShaderProgram() : -m_id (0) -{ -} - -ShaderProgram::~ShaderProgram() -{ - destroy(); -} - -void ShaderProgram::create() -{ - destroy(); - - m_id = glCreateProgram(); -} - -void ShaderProgram::destroy() -{ - if (m_id > 0) { - glDeleteProgram(m_id); - m_id = 0; - } -} - -void ShaderProgram::addShader(const Shader& shader) -{ - glAttachShader(m_id, shader.getHandle()); -} - -bool ShaderProgram::loadFromFile(const std::string& filename) -{ - std::string extension = Path::getExtension(filename); - - // Meta file. load real shaders. - if (extension == "shader.glsl") { - - // FIXME: This is ugly :) - std::string base_name = filename.substr(0, filename.length() - extension.length()); - - // vert and frag are not optional. they must exist. - return loadFromFile(base_name + "vert.glsl", Shader::Type::Vertex) && - loadFromFile(base_name + "frag.glsl", Shader::Type::Fragment); - - } else if (extension == "vert.glsl") { - return loadFromFile(filename, Shader::Type::Vertex); - } else if (extension == "frag.glsl") { - return loadFromFile(filename, Shader::Type::Fragment); - } - - m_error = "Invalid file extension: " + extension; - - return false; -} - -bool ShaderProgram::loadFromFile(const std::string& filename, Shader::Type type) -{ - Shader shader(type); - - if (!shader.loadFromFile(filename)) { - m_error = shader.getName() + ": " + shader.getError(); - return false; - } - - addShader(shader); - - return true; -} - -bool ShaderProgram::loadFromMemory(const std::string& source, Shader::Type type) -{ - Shader shader(type); - - if (!shader.loadFromMemory(source)) { - m_error = shader.getError(); - return false; - } - - addShader(shader); - - return true; -} - -bool ShaderProgram::link() -{ - glLinkProgram(m_id); - - if (!isLinked()) { - m_error = fetchErrorLog(); - return false; - } - return true; -} - -bool ShaderProgram::isLinked() const -{ - // Make sure we have a id first. - if (m_id) { - - // Query OpenGL for link status. - GLint status; - glGetProgramiv(m_id, GL_LINK_STATUS, &status); - return status == GL_TRUE; - } - return false; -} - -void ShaderProgram::enable() const -{ - glUseProgram(m_id); -} - -void ShaderProgram::disable() const -{ - glUseProgram(0); -} - -std::string ShaderProgram::getLastError() const -{ - return m_error; -} - -bool ShaderProgram::setMVPMatrix(const Matrix4f& matrix) -{ - return setUniform("u_MVP", matrix); -} - -bool ShaderProgram::setUniform(const std::string& name, const Matrix4f& matrix) const -{ - int loc; - if (getUniformLoc(name, loc)) { - glUniformMatrix4fv(loc, 1, GL_FALSE, matrix.e); - return true; - } - return false; -} - -bool ShaderProgram::setAttribute(const std::string& name, const Matrix4f& matrix) const -{ - int loc; - if (getAttribLoc(name, loc)) { - glVertexAttrib4fv(loc, matrix.e); - return true; - } - return false; -} - -bool ShaderProgram::setUniform(const std::string& name, const Color& color) const -{ - int loc; - if (getUniformLoc(name, loc)) { - Vector4f c = color.toRGBAf(); - - glUniform4f(loc, c.x, c.y, c.z, c.w); - return true; - } - return false; -} - -bool ShaderProgram::getAttribLoc(const std::string& name, int& loc) const -{ - loc = glGetAttribLocation(m_id, name.c_str()); - if (loc < 0) { - m_error = "Attribute variable '" + name + "' was not found in shader."; - return false; - } - return true; -} - -bool ShaderProgram::getUniformLoc(const std::string& name, int& loc) const -{ - loc = glGetUniformLocation(m_id, name.c_str()); - if (loc < 0) { - m_error = "Uniform variable '" + name + "' was not found in shader."; - return false; - } - return true; -} - -std::string ShaderProgram::fetchErrorLog() -{ - GLchar buf[4096] = { '\0' }; - - glGetProgramInfoLog(m_id, sizeof(buf), NULL, buf); - - return std::string(buf); -} - -} // namespace sp