main
Brett 2023-12-28 03:49:00 -05:00
parent 5fd86f274b
commit f9cd25f00e
2 changed files with 44 additions and 33 deletions

View File

@ -36,53 +36,6 @@ namespace blt::gfx
std::vector<blt::vec3f> normals; std::vector<blt::vec3f> normals;
}; };
/**
* Since most VAOs will not use more than 8 VBOs it makes no sense to heap allocate memory to store them
* This class is used to make that easier to handle
*/
class static_dynamic_array
{
private:
static constexpr size_t DATA_SIZE = 8;
typedef std::array<GLuint, DATA_SIZE> array_t;
std::variant<array_t, GLuint*> data_;
size_t size_ = DATA_SIZE;
void swap();
public:
static_dynamic_array();
static_dynamic_array(const static_dynamic_array& copy) = delete;
static_dynamic_array(static_dynamic_array&& move) = default;
static_dynamic_array& operator=(const static_dynamic_array& copy) = delete;
static_dynamic_array& operator=(static_dynamic_array&& move) = default;
GLuint& operator[](size_t index)
{
if (index >= size_)
swap();
if (std::holds_alternative<GLuint*>(data_))
return std::get<GLuint*>(data_)[index];
else
return std::get<array_t>(data_)[index];
}
~static_dynamic_array()
{
if (std::holds_alternative<GLuint*>(data_))
delete[] std::get<GLuint*>(data_);
}
};
/**
* basic VAO class.
*/
class basic_vertex_array
{
private:
struct vbo_t struct vbo_t
{ {
GLuint bufferID_ = 0; GLuint bufferID_ = 0;
@ -101,7 +54,63 @@ namespace blt::gfx
~vbo_t(); ~vbo_t();
}; };
/**
* Since most VAOs will not use more than 8 VBOs it makes no sense to heap allocate memory to store them
* This class is used to make that easier to handle
*/
class static_dynamic_array
{
private:
static constexpr size_t DATA_SIZE = 8;
typedef std::array<vbo_t, DATA_SIZE> array_t;
std::variant<array_t, vbo_t*> data_;
size_t size_ = DATA_SIZE;
size_t max = 0;
void swap();
public:
static_dynamic_array();
static_dynamic_array(const static_dynamic_array& copy) = delete;
static_dynamic_array(static_dynamic_array&& move) noexcept = default;
static_dynamic_array& operator=(const static_dynamic_array& copy) = delete;
static_dynamic_array& operator=(static_dynamic_array&& move) noexcept = default;
vbo_t& operator[](size_t index)
{
if (index >= size_)
swap();
max = std::max(index, max);
if (std::holds_alternative<vbo_t*>(data_))
return std::get<vbo_t*>(data_)[index];
else
return std::get<array_t>(data_)[index];
}
[[nodiscard]] size_t used() const noexcept {
return max;
}
~static_dynamic_array()
{
if (std::holds_alternative<vbo_t*>(data_))
delete[] std::get<vbo_t*>(data_);
}
};
/**
* basic VAO class.
*/
class basic_vertex_array
{
private:
GLuint vaoID; GLuint vaoID;
static_dynamic_array VBOs;
public: public:
basic_vertex_array(); basic_vertex_array();
}; };

View File

@ -23,13 +23,15 @@ namespace blt::gfx
void static_dynamic_array::swap() void static_dynamic_array::swap()
{ {
size_t next_size = blt::mem::next_byte_allocation(size_); size_t next_size = blt::mem::next_byte_allocation(size_);
auto* new_data = new GLuint[next_size]; auto* new_data = new GLuint[next_size];
std::memset(new_data, 0, next_size); std::memset(new_data, 0, next_size);
if (std::holds_alternative<GLuint*>(data_)) if (std::holds_alternative<GLuint*>(data_))
{ {
auto ptr = std::get<GLuint*>(data_); auto* ptr = std::get<GLuint*>(data_);
std::memcpy(new_data, ptr, size_); std::memcpy(new_data, ptr, size_);
delete ptr; delete[] ptr;
} else { } else {
auto data = std::get<array_t>(data_); auto data = std::get<array_t>(data_);
std::memcpy(new_data, data.data(), DATA_SIZE); std::memcpy(new_data, data.data(), DATA_SIZE);
@ -46,26 +48,26 @@ namespace blt::gfx
/* /*
* ----------------------------------- * -----------------------------------
* basic_vertex_array::vbo_t * vbo_t
* ----------------------------------- * -----------------------------------
*/ */
basic_vertex_array::vbo_t::vbo_t() vbo_t::vbo_t()
{ {
glGenBuffers(1, &bufferID_); glGenBuffers(1, &bufferID_);
} }
basic_vertex_array::vbo_t::~vbo_t() vbo_t::~vbo_t()
{ {
glDeleteBuffers(1, &bufferID_); glDeleteBuffers(1, &bufferID_);
} }
void basic_vertex_array::vbo_t::allocate(GLsizeiptr size, GLint buffer_type, GLint memory_type, void* data) void vbo_t::allocate(GLsizeiptr size, GLint buffer_type, GLint memory_type, void* data)
{ {
size_ = size; size_ = size;
glBufferData(buffer_type, size, data, memory_type); glBufferData(buffer_type, size, data, memory_type);
} }
void blt::gfx::basic_vertex_array::vbo_t::update(GLsizeiptr size, void* data, GLint buffer_type, GLint memory_type) void vbo_t::update(GLsizeiptr size, void* data, GLint buffer_type, GLint memory_type)
{ {
if (size <= size_) if (size <= size_)
sub_update(0, size, data, buffer_type); sub_update(0, size, data, buffer_type);