nf7/common/gl_obj.hh
2022-10-06 10:52:19 +09:00

84 lines
1.6 KiB
C++

#pragma once
#include <cassert>
#include <memory>
#include <GL/glew.h>
#include "nf7.hh"
#include "common/factory.hh"
#include "common/future.hh"
namespace nf7::gl {
template <typename T>
class Obj final {
public:
Obj() = delete;
Obj(const std::shared_ptr<nf7::Context>& ctx, GLuint id) noexcept :
ctx_(ctx), id_(id) {
}
Obj(const std::shared_ptr<nf7::Context>& ctx) noexcept :
Obj(ctx, T::Gen()) {
}
~Obj() noexcept {
ctx_->env().ExecGL(ctx_, [id = id_]() { T::Delete(id); });
}
Obj(const Obj&) = delete;
Obj(Obj&&) = delete;
Obj& operator=(const Obj&) = delete;
Obj& operator=(Obj&&) = delete;
GLuint id() const noexcept { return id_; }
T& meta() noexcept { return meta_; }
const T& meta() const noexcept { return meta_; }
private:
std::shared_ptr<nf7::Context> ctx_;
GLuint id_;
T meta_;
};
struct Obj_BufferMeta final {
public:
GLenum type;
size_t size;
static GLuint Gen() noexcept {
GLuint id;
glGenBuffers(&id, 1);
return id;
}
static void Delete(GLuint id) noexcept {
glDeleteBuffers(&id);
}
};
using Buffer = Obj<Obj_BufferMeta>;
using BufferFactory = AsyncFactory<std::shared_ptr<Buffer>>;
struct Obj_BufferMeta final {
public:
GLenum type;
GLenum format;
uint32_t w, h, d;
static GLuint Gen() noexcept {
GLuint id;
glGenTextures(&id, 1);
return id;
}
static void Delete(GLuint id) noexcept {
glDeleteTextures(&id);
}
};
using Texture = Obj<Obj_TextureMeta>;
using TextureFactory = AsyncFactory<std::shared_ptr<Texture>>;
} // namespace nf7::gl