Graphics: Remove Shader and ShaderProgram classes. they are combined into GfxDriver/ShaderProgram.
This commit is contained in:
parent
82e08d0785
commit
81f3e1e399
5 changed files with 0 additions and 451 deletions
|
|
@ -113,8 +113,6 @@ local graphics_module = Module("source/Graphics", {
|
|||
"Renderable2D.cpp",
|
||||
"Renderer2D.cpp",
|
||||
"RenderState.cpp",
|
||||
"Shader.cpp",
|
||||
"ShaderProgram.cpp",
|
||||
"Texture.cpp",
|
||||
|
||||
-- Text
|
||||
|
|
|
|||
|
|
@ -1,58 +0,0 @@
|
|||
|
||||
#ifndef SPECTRE_GRAPHICS_SHADER_H
|
||||
#define SPECTRE_GRAPHICS_SHADER_H
|
||||
|
||||
#include <string>
|
||||
|
||||
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 */
|
||||
|
|
@ -1,90 +0,0 @@
|
|||
|
||||
#ifndef SPECTRE_GRAPHICS_SHADER_PROGRAM_H
|
||||
#define SPECTRE_GRAPHICS_SHADER_PROGRAM_H
|
||||
|
||||
#include <string>
|
||||
#include <Spectre/Math/Matrix4.h>
|
||||
#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:
|
||||
// <name>.glsl - Virtual file, will load all real shader files with the same name.
|
||||
// <name>.vert.glsl - Vertex shader.
|
||||
// <name>.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 */
|
||||
|
|
@ -1,104 +0,0 @@
|
|||
|
||||
#include <Spectre/System/Path.h>
|
||||
#include <Spectre/System/File.h>
|
||||
#include <Spectre/Graphics/Shader.h>
|
||||
#include <Graphics/GL/gl.h>
|
||||
|
||||
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
|
||||
|
|
@ -1,197 +0,0 @@
|
|||
|
||||
#include <Spectre/Math/Color.h>
|
||||
#include <Spectre/Graphics/ShaderProgram.h>
|
||||
#include <Spectre/System/Path.h>
|
||||
#include <Graphics/GL/gl.h>
|
||||
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue