create generic gl::buffer, rebase gl::ubo onto it

This commit is contained in:
milek7
2018-10-21 00:16:02 +02:00
parent e3cfa2acbe
commit 7c9c19908a
5 changed files with 117 additions and 23 deletions

View File

@@ -107,6 +107,7 @@ set(SOURCES
"gl/postfx.cpp" "gl/postfx.cpp"
"gl/cubemap.cpp" "gl/cubemap.cpp"
"gl/glsl_common.cpp" "gl/glsl_common.cpp"
"gl/buffer.cpp"
"imgui/imgui.cpp" "imgui/imgui.cpp"
"imgui/imgui_demo.cpp" "imgui/imgui_demo.cpp"

67
gl/buffer.cpp Normal file
View File

@@ -0,0 +1,67 @@
#include "buffer.h"
GLenum gl::buffer::glenum_target(gl::buffer::targets target)
{
GLenum mapping[13] =
{
GL_ARRAY_BUFFER,
GL_ATOMIC_COUNTER_BUFFER,
GL_COPY_READ_BUFFER,
GL_COPY_WRITE_BUFFER,
GL_DISPATCH_INDIRECT_BUFFER,
GL_DRAW_INDIRECT_BUFFER,
GL_ELEMENT_ARRAY_BUFFER,
GL_PIXEL_PACK_BUFFER,
GL_PIXEL_UNPACK_BUFFER,
GL_SHADER_STORAGE_BUFFER,
GL_TEXTURE_BUFFER,
GL_TRANSFORM_FEEDBACK_BUFFER,
GL_UNIFORM_BUFFER
};
return mapping[target];
}
void gl::buffer::bind(targets target)
{
if (binding_points[target] == *this)
return;
glBindBuffer(glenum_target(target), *this);
binding_points[target] = *this;
}
void gl::buffer::bind_base(targets target, GLuint index)
{
glBindBufferBase(glenum_target(target), index, *this);
binding_points[target] = *this;
}
void gl::buffer::unbind(targets target)
{
glBindBuffer(glenum_target(target), 0);
binding_points[target] = 0;
}
gl::buffer::buffer()
{
glGenBuffers(1, *this);
}
gl::buffer::~buffer()
{
glDeleteBuffers(1, *this);
}
void gl::buffer::allocate(targets target, int size, GLenum hint)
{
bind(target);
glBufferData(glenum_target(target), size, nullptr, hint);
}
void gl::buffer::upload(targets target, const void *data, int offset, int size)
{
bind(target);
glBufferSubData(glenum_target(target), offset, size, data);
}
GLuint gl::buffer::binding_points[13];

44
gl/buffer.h Normal file
View File

@@ -0,0 +1,44 @@
#pragma once
#include "object.h"
#include "bindable.h"
namespace gl
{
class buffer : public object
{
static GLuint binding_points[13];
public:
enum targets
{
ARRAY_BUFFER = 0,
ATOMIC_COUNTER_BUFFER,
COPY_READ_BUFFER,
COPY_WRITE_BUFFER,
DISPATCH_INDIRECT_BUFFER,
DRAW_INDIRECT_BUFFER,
ELEMENT_ARRAY_BUFFER,
PIXEL_PACK_BUFFER,
PIXEL_UNPACK_BUFFER,
SHADER_STORAGE_BUFFER,
TEXTURE_BUFFER,
TRANSFORM_FEEDBACK_BUFFER,
UNIFORM_BUFFER
};
private:
static GLenum glenum_target(targets target);
public:
buffer();
~buffer();
void bind(targets target);
void bind_base(targets target, GLuint index);
static void unbind(targets target);
void allocate(targets target, int size, GLenum hint);
void upload(targets target, const void *data, int offset, int size);
};
}

View File

@@ -3,31 +3,17 @@
gl::ubo::ubo(int size, int idx, GLenum hint) gl::ubo::ubo(int size, int idx, GLenum hint)
{ {
glGenBuffers(1, *this); allocate(buffer::UNIFORM_BUFFER, size, hint);
bind();
glBufferData(GL_UNIFORM_BUFFER, size, nullptr, hint);
index = idx; index = idx;
bind_uniform(); bind_uniform();
} }
void gl::ubo::bind_uniform() void gl::ubo::bind_uniform()
{ {
glBindBufferBase(GL_UNIFORM_BUFFER, index, *this); bind_base(buffer::UNIFORM_BUFFER, index);
active = this;
}
gl::ubo::~ubo()
{
glDeleteBuffers(1, *this);
}
void gl::ubo::bind(GLuint i)
{
glBindBuffer(GL_UNIFORM_BUFFER, i);
} }
void gl::ubo::update(const uint8_t *data, int offset, int size) void gl::ubo::update(const uint8_t *data, int offset, int size)
{ {
bind(); upload(buffer::UNIFORM_BUFFER, data, offset, size);
glBufferSubData(GL_UNIFORM_BUFFER, offset, size, data);
} }

View File

@@ -2,24 +2,20 @@
#include "object.h" #include "object.h"
#include "bindable.h" #include "bindable.h"
#include "buffer.h"
#define UBS_PAD(x) uint8_t PAD[x] #define UBS_PAD(x) uint8_t PAD[x]
namespace gl namespace gl
{ {
static GLuint next_binding_point; class ubo : public buffer
class ubo : public object, public bindable<ubo>
{ {
int index; int index;
public: public:
ubo(int size, int index, GLenum hint = GL_DYNAMIC_DRAW); ubo(int size, int index, GLenum hint = GL_DYNAMIC_DRAW);
~ubo();
void bind_uniform(); void bind_uniform();
using bindable::bind;
static void bind(GLuint i);
void update(const uint8_t *data, int offset, int size); void update(const uint8_t *data, int offset, int size);
template <typename T> void update(const T &data, size_t offset = 0) template <typename T> void update(const T &data, size_t offset = 0)