Files
NBEngine/engine/NBGraphics/include/NBGraphics/OGLObjects.hpp
T

118 lines
3.3 KiB
C++

#pragma once
#ifndef _NB_OGL_OBJECTS
#define _NB_OGL_OBJECTS
#include <NBCore/Errors.hpp>
#include <NBGraphics/GLLoad.hpp>
#include <NBCore/Utils.hpp>
namespace nb {
typedef ConstantMap<GLenum, std::string> GLEnumTableType;
extern const GLEnumTableType GLenumTable;
extern const ConstantMap<TypeCodes, GLint> TypeToGLSL;
class OpenGLError : public Error<OpenGLError> {
using Base = Error<OpenGLError>;
public:
using Base::Base;
enum Codes : unsigned int {
NO_ERROR = GL_NO_ERROR,
INVALID_ENUM = GL_INVALID_ENUM,
INVALID_VALUE = GL_INVALID_VALUE,
INVALID_OPERATION = GL_INVALID_OPERATION,
INVALID_FRAMEBUFFER_OPERATION = GL_INVALID_FRAMEBUFFER_OPERATION,
OUT_OF_MEMORY = GL_OUT_OF_MEMORY,
STACK_UNDERFLOW = GL_STACK_UNDERFLOW,
STACK_OVERFLOW = GL_STACK_OVERFLOW
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
static OpenGLError status();
static OpenGLError status(const std::string& note);
};
class OpenGLObjectError : public Error<OpenGLObjectError> {
using Base = Error<OpenGLObjectError>;
public:
using Base::Base;
enum Codes : unsigned int {
UNDEFINED, HANGING_OBJECT, INVALID_OBJECT, INVALID_CONTEXT
};
static const std::string type;
static const ErrorCodeMap ErrorMessages;
};
class OpenGLObject {
public:
OpenGLObject(const OpenGLObject&) = delete;
OpenGLObject& operator=(const OpenGLObject&) = delete;
OpenGLObject(OpenGLObject&&);
OpenGLObject& operator=(OpenGLObject&&);
virtual ~OpenGLObject() {};
virtual void bind() const = 0;
virtual void unbind() const = 0;
GLuint id() const { return _id; }
protected:
using Codes = OpenGLObjectError::Codes;
OpenGLObject();
void checkObjectContextValidity();
virtual GLuint declare() = 0;
virtual void remove() = 0;
const GLFWwindow* const _arena;
GLuint _id = 0;
};
} // namespace nb
#ifndef OPENGL_CALL
#ifdef _NB_MONITOR_OPENGL_CALLS
namespace NB_OPENGL_CALL_MONITORING {
template<typename Fn>
inline auto run_expression(
const std::string& file,
unsigned int line,
Fn fn
) {
auto check_ = [file, line](const std::string& note="") {
const auto no_err = nb::OpenGLError::NO_ERROR;
auto stat = nb::OpenGLError::status(note);
if (stat.code != no_err) {
THROW_W_CODE_LOC(file, line, stat);
}
};
if constexpr(std::is_same_v<std::invoke_result_t<Fn>, void>) {
check_("Pre-call state");
fn();
check_("Post-call state");
} else {
check_("Pre-call state");
auto ret = fn();
check_("Post-call state");
return ret;
}
}
};
#define OPENGL_CALL(expression) NB_OPENGL_CALL_MONITORING::run_expression(\
__FILE__,\
__LINE__,\
[&]() {return expression;}\
)
#else
#define OPENGL_CALL(x) x
#endif // _NB_MONITOR_OPENGL_CALLS
#endif // OPENGL_CALL
#endif // _NB_OGL_OBJECTS