Dawn/src/dawnopengl/display/shader/ShaderProgram.cpp

57 lines
1.3 KiB
C++

// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// 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();
}
}