1
0
Fork 0

Adding Spectre/GfxDriver/ShaderProgram base class.

This commit is contained in:
Henrik Hautakoski 2022-09-18 12:26:01 +02:00
parent b91bc22db1
commit cd9114bfd0
2 changed files with 146 additions and 0 deletions

View file

@ -0,0 +1,77 @@
#ifndef SPECTRE_GFXDRIVER_SHADERPROGRAM_H
#define SPECTRE_GFXDRIVER_SHADERPROGRAM_H
#include <Spectre/Math/Matrix4.h>
#include <Spectre/Math/Color.h>
namespace sp {
enum ShaderType {
Vertex,
Fragment
};
class ShaderProgram
{
public:
virtual ~ShaderProgram();
// Load a shader program from file.
virtual bool loadFromFile(const std::string& filename);
// Load a shader from file.
virtual bool loadFromFile(const std::string& filename, ShaderType type);
// Load a shader from memory.
virtual bool loadFromMemory(const std::string& source, ShaderType type) = 0;
// Link the program.
virtual bool link() = 0;
// Returns true if the program was linked successfully
virtual bool isLinked() const;
// Enable this shader
// All shader operations after this call will affect this shader.
virtual void enable() const = 0;
// Disable this shader
// Operations will no longer affect this shader after this call.
virtual void disable() const = 0;
// Get the last shader error.
virtual std::string getLastError() const;
// ---------------------
// Variables.
// ---------------------
// TODO: Move this to a ShaderParameter class
virtual bool setMVPMatrix(const Matrix4f& matrix) = 0;
// ---------------------
// General Variables.
// ---------------------
virtual bool setAttribute(const std::string& name, float value) const = 0;
virtual bool setAttribute(const std::string& name, const Matrix4f& matrix) const = 0;
virtual bool setUniform(const std::string& name, const Matrix4f& matrix) const = 0;
virtual bool setUniform(const std::string& name, const Color& color) const = 0;
protected:
std::string m_extension;
mutable std::string m_error;
bool m_linked;
};
} // namespace sp
#endif /* SECTRE_GRAPHICS_SHADERPROGRAM_H */

View file

@ -0,0 +1,69 @@
#include <Spectre/System/Path.h>
#include <Spectre/System/File.h>
#include <Spectre/GfxDriver/ShaderProgram.h>
#include <string>
namespace sp
{
ShaderProgram::~ShaderProgram()
{
}
bool ShaderProgram::loadFromFile(const std::string& filename)
{
std::string extension = Path::getExtension(filename);
// Meta file. load real shaders.
if (extension == "shader." + m_extension) {
// 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." + m_extension, ShaderType::Vertex) &&
loadFromFile(base_name + "frag." + m_extension, ShaderType::Fragment);
} else if (extension == "vert." + m_extension) {
return loadFromFile(filename, ShaderType::Vertex);
} else if (extension == "frag." + m_extension) {
return loadFromFile(filename, ShaderType::Fragment);
}
m_error = "Invalid file extension: " + extension;
return false;
}
bool ShaderProgram::loadFromFile(const std::string& filename, ShaderType type)
{
std::string src;
File file;
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, type);
}
bool ShaderProgram::isLinked() const
{
return m_linked;
}
std::string ShaderProgram::getLastError() const
{
return m_error;
}
} // namespace sp