Shaders now compile all the way to OpenGL
This commit is contained in:
@ -4,14 +4,54 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "ShaderProgram.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
#include "assert/assertgl.hpp"
|
||||
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void ShaderProgram::init(
|
||||
const std::vector<std::shared_ptr<ShaderStage>> &stages
|
||||
) {
|
||||
assertTrue(this->id == -1, "ShaderProgram already initialized?");
|
||||
|
||||
IShaderProgram::init(stages);
|
||||
|
||||
// Create the program
|
||||
this->id = glCreateProgram();
|
||||
assertNoGLError();
|
||||
|
||||
// Attach all the shader stages
|
||||
for(auto stage : stages) {
|
||||
glAttachShader(this->id, stage->id);
|
||||
assertNoGLError();
|
||||
}
|
||||
|
||||
// Link and verify the program
|
||||
glLinkProgram(this->id);
|
||||
assertNoGLError();
|
||||
|
||||
GLint status;
|
||||
glGetProgramiv(this->id, GL_LINK_STATUS, &status);
|
||||
assertNoGLError();
|
||||
|
||||
if(!status) {
|
||||
// Failed to link
|
||||
GLint logLength;
|
||||
glGetProgramiv(this->id, GL_INFO_LOG_LENGTH, &logLength);
|
||||
assertNoGLError();
|
||||
|
||||
GLchar *log = new GLchar[logLength];
|
||||
glGetProgramInfoLog(this->id, logLength, NULL, log);
|
||||
assertNoGLError();
|
||||
assertUnreachable("Failed to link shader program:\n%s", log);
|
||||
}
|
||||
}
|
||||
|
||||
ShaderProgram::~ShaderProgram() {
|
||||
// Delete the shader program
|
||||
if(this->id != -1) {
|
||||
glDeleteProgram(this->id);
|
||||
assertNoGLError();
|
||||
}
|
||||
}
|
@ -5,9 +5,13 @@
|
||||
|
||||
#pragma once
|
||||
#include "display/shader/IShaderProgram.hpp"
|
||||
#include "dawnopengl.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class ShaderProgram : public IShaderProgram {
|
||||
private:
|
||||
GLuint id = -1;
|
||||
|
||||
public:
|
||||
void init(
|
||||
const std::vector<std::shared_ptr<ShaderStage>> &stages
|
||||
|
@ -4,6 +4,8 @@
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "ShaderStage.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
#include "assert/assertgl.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@ -11,7 +13,56 @@ void ShaderStage::init(
|
||||
const SlangStage &stage,
|
||||
const std::string &code
|
||||
) {
|
||||
assertTrue(this->id == -1, "ShaderStage already initialized?");
|
||||
|
||||
// Determine GL Shader type from slang shader type.
|
||||
switch(stage) {
|
||||
case SlangStage::SLANG_STAGE_VERTEX:
|
||||
shaderType = GL_VERTEX_SHADER;
|
||||
break;
|
||||
|
||||
case SlangStage::SLANG_STAGE_FRAGMENT:
|
||||
shaderType = GL_FRAGMENT_SHADER;
|
||||
break;
|
||||
|
||||
default:
|
||||
assertUnreachable("Unknown Slang Shader type\n");
|
||||
break;
|
||||
}
|
||||
|
||||
// Initialize the shader.
|
||||
this->id = glCreateShader(shaderType);
|
||||
assertNoGLError();
|
||||
|
||||
// Compile
|
||||
const char_t* cSource = code.c_str();
|
||||
glShaderSource(this->id, 1, &cSource, nullptr);
|
||||
assertNoGLError();
|
||||
|
||||
glCompileShader(this->id);
|
||||
assertNoGLError();
|
||||
|
||||
// Validate shader compiled successfully.
|
||||
GLint status;
|
||||
glGetShaderiv(this->id, GL_COMPILE_STATUS, &status);
|
||||
assertNoGLError();
|
||||
|
||||
if(!status) {
|
||||
// Failed to compile
|
||||
GLint logLength;
|
||||
glGetShaderiv(this->id, GL_INFO_LOG_LENGTH, &logLength);
|
||||
assertNoGLError();
|
||||
|
||||
GLchar *log = new GLchar[logLength];
|
||||
glGetShaderInfoLog(this->id, logLength, NULL, log);
|
||||
assertNoGLError();
|
||||
assertUnreachable("Failed to compile shader stage %i:\n%s", shaderType, log);
|
||||
}
|
||||
}
|
||||
|
||||
ShaderStage::~ShaderStage() {
|
||||
if(this->id != -1) {
|
||||
glDeleteShader(this->id);
|
||||
assertNoGLError();
|
||||
}
|
||||
}
|
@ -5,14 +5,24 @@
|
||||
|
||||
#pragma once
|
||||
#include "display/shader/IShaderStage.hpp"
|
||||
#include "dawnopengl.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class ShaderProgram;
|
||||
|
||||
class ShaderStage : public IShaderStage {
|
||||
protected:
|
||||
GLenum shaderType;
|
||||
GLuint id = -1;
|
||||
|
||||
public:
|
||||
void init(
|
||||
const SlangStage &stage,
|
||||
const std::string &code
|
||||
) override;
|
||||
|
||||
~ShaderStage();
|
||||
|
||||
friend class ShaderProgram;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user