hyporo-cpp/source/hpr/gpu/shader_program.hpp

97 lines
1.7 KiB
C++
Raw Normal View History

2022-09-24 16:07:46 +05:00
#pragma once
2023-01-11 14:46:49 +05:00
#include "../containers.hpp"
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
#include <string>
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
#ifndef __gl_h_
#include <glad/glad.h>
#endif
2022-09-24 16:07:46 +05:00
namespace hpr::gpu
{
2023-01-11 14:46:49 +05:00
class ShaderProgram
{
protected:
unsigned int p_index;
darray<Shader> p_shaders;
public:
// Constructors
inline
ShaderProgram() :
p_index {0}
{}
virtual
~ShaderProgram() = default;
[[nodiscard]]
unsigned int index() const
{
return p_index;
}
darray<Shader> shaders()
{
return p_shaders;
}
void create(const std::string& label = "")
{
p_index = glCreateProgram();
}
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
void attach(const Shader& shader)
{
glAttachShader(p_index, shader.index());
p_shaders.push(shader);
}
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
void detach(const Shader& shader)
{
// WARNING: segfault, destroy_at (char)
p_shaders.remove([shader](const Shader& _shader)
{
return shader.index() == _shader.index();
});
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
glDetachShader(p_index, shader.index());
}
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
void link()
{
glLinkProgram(p_index);
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
GLint status;
glGetProgramiv(p_index, GL_LINK_STATUS, &status);
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
if (status == GL_FALSE)
throw std::runtime_error("Shader program link error");
}
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
void destroy()
{
//for (auto& shader : p_shaders)
// detach(shader);
glDeleteShader(p_index);
}
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
void bind()
{
glUseProgram(p_index);
}
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
void unbind()
{
glUseProgram(0);
}
};
2022-09-24 16:07:46 +05:00
2023-01-11 14:46:49 +05:00
}