// 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> &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(); } }