NBEngine/engine/NBGraphics/ProgramPipeline.hpp
2026-07-18 16:38:24 -05:00

141 lines
3.0 KiB
C++

#pragma once
#ifndef _NB_SHADER
#define _NB_SHADER
#include "GLLoad.hpp"
#include <string>
#include <NBCore/Errors.hpp>
#include <NBCore/Utils.hpp>
#include "OGLObjects.hpp"
namespace nb {
class ShaderError : public Error<ShaderError> {
using Base = Error<ShaderError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class ProgramError : public Error<ProgramError> {
using Base = Error<ProgramError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, LINKING_ERROR
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
enum OpenGLProfiles {
Core,
Compatibility,
ES
};
class Shader : public OpenGLObject {
using Base = OpenGLObject;
public:
using Base::Base;
using Base::id;
Shader(GLenum, const std::string&);
Shader(GLenum, const std::vector<std::string>&);
Shader(Shader&&);
Shader& operator=(Shader&&) = delete;
~Shader() { remove(); }
operator bool();
virtual void bind() const override { /* TODO: Some warning of some kind perhaps*/ }
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
bool success() const;
GLint status(GLenum) const;
std::string log() const;
const GLenum target;
friend Base;
protected:
using Base::_id;
GLint _success;
std::vector<std::string> _sources;
virtual void compile() {
int num_srcs = _sources.size();
std::vector<char*> src_ptrs(num_srcs);
for (int i=0; i < num_srcs; ++i) {
src_ptrs[i] = _sources[i].data();
}
glShaderSource(_id, num_srcs, src_ptrs.data(), NULL);
glCompileShader(_id);
_success = status(GL_COMPILE_STATUS);
if (!_success) {
WARN(log(), 0x0FE);
}
}
virtual GLuint declare() override {
if (!_id) {
_id = _id = glCreateShader(target);
}
return _id;
}
virtual void remove() override {
if (_id) {
glDeleteShader(_id);
}
}
};
class Program : public OpenGLObject {
using Base = OpenGLObject;
public:
using Base::Base;
using Base::id;
Program(SharedVector<Shader>);
Program(Program&&);
Program& operator=(Program&&) = delete;
operator bool();
~Program() { remove(); }
virtual void bind() const override {
glUseProgram(_id);
}
virtual void unbind() const override { /* TODO: Some warning of some kind perhaps*/ }
GLint status(GLenum) const;
std::string log() const;
friend Base;
protected:
using Base::_id;
GLint _success;
virtual GLuint declare() override {
if (!_id) {
_id = glCreateProgram();
}
return _id;
}
virtual void remove() override {
if (_id) {
glDeleteProgram(_id);
}
}
};
}
#endif