shared vbos

main
Brett 2024-01-08 21:12:31 -05:00
parent 1019d4aefb
commit 5bff8b675c
8 changed files with 68 additions and 33 deletions

View File

@ -28,9 +28,18 @@
namespace blt::gfx namespace blt::gfx
{ {
typedef blt::vec3 vertex_t; typedef blt::vec3f vertex_t;
typedef blt::vec2 uv_t; typedef blt::vec2f uv_t;
typedef blt::vec3 normal_t; typedef blt::vec3f normal_t;
class model_data
{
public:
private:
std::vector<vertex_t> vertices;
std::vector<uv_t> uvs;
std::vector<normal_t> normals;
};
struct face_t struct face_t
{ {

View File

@ -28,14 +28,6 @@
namespace blt::gfx namespace blt::gfx
{ {
class model_data
{
public:
private:
std::vector<blt::vec3f> vertices;
std::vector<blt::vec2f> uvs;
std::vector<blt::vec3f> normals;
};
struct vbo_t struct vbo_t
{ {
@ -48,6 +40,8 @@ namespace blt::gfx
void bind() const; void bind() const;
void unbind() const;
void allocate(GLsizeiptr size, GLint mem_type = GL_STATIC_DRAW, const void* data = nullptr); void allocate(GLsizeiptr size, GLint mem_type = GL_STATIC_DRAW, const void* data = nullptr);
template<typename T> template<typename T>
@ -63,6 +57,28 @@ namespace blt::gfx
void destroy(); void destroy();
}; };
struct vbo_t_owner
{
vbo_t vbo;
vbo_t_owner() = default;
explicit vbo_t_owner(vbo_t vbo): vbo(vbo)
{}
vbo_t* operator->(){
return &vbo;
}
~vbo_t_owner()
{
if (!vbo.bufferID_)
return;
vbo.destroy();
vbo.unbind();
}
};
/** /**
* Since most VAOs will not use more than 8 VBOs it makes no sense to heap allocate memory to store them * 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 * This class is used to make that easier to handle
@ -71,8 +87,9 @@ namespace blt::gfx
{ {
private: private:
static constexpr size_t DATA_SIZE = 8; static constexpr size_t DATA_SIZE = 8;
typedef std::array<vbo_t, DATA_SIZE> array_t; using vbo_type = std::shared_ptr<vbo_t_owner>;
std::variant<array_t, vbo_t*> data_; using array_t = std::array<vbo_type, DATA_SIZE>;
std::variant<array_t, vbo_type*> data_;
size_t size_ = DATA_SIZE; size_t size_ = DATA_SIZE;
size_t max = 0; size_t max = 0;
@ -89,7 +106,7 @@ namespace blt::gfx
static_dynamic_array& operator=(static_dynamic_array&& move) noexcept = default; static_dynamic_array& operator=(static_dynamic_array&& move) noexcept = default;
vbo_t& operator[](size_t index); vbo_type& operator[](size_t index);
[[nodiscard]] inline size_t used() const noexcept [[nodiscard]] inline size_t used() const noexcept
{ {
@ -98,8 +115,8 @@ namespace blt::gfx
~static_dynamic_array() ~static_dynamic_array()
{ {
if (std::holds_alternative<vbo_t*>(data_)) if (std::holds_alternative<vbo_type*>(data_))
delete[] std::get<vbo_t*>(data_); delete[] std::get<vbo_type*>(data_);
} }
}; };
@ -111,7 +128,7 @@ namespace blt::gfx
private: private:
GLuint vaoID; GLuint vaoID;
static_dynamic_array VBOs; static_dynamic_array VBOs;
HASHSET <GLuint> used_attributes; HASHSET<GLuint> used_attributes;
vbo_t element; vbo_t element;
public: public:
vertex_array(); vertex_array();
@ -155,10 +172,10 @@ namespace blt::gfx
*/ */
inline vbo_t& operator[](size_t index) inline vbo_t& operator[](size_t index)
{ {
return VBOs[index]; return VBOs[index]->vbo;
} }
inline void bind() inline void bind() const
{ {
glBindVertexArray(vaoID); glBindVertexArray(vaoID);
} }

View File

@ -6798,6 +6798,7 @@ static stbir__info * stbir__alloc_internal_mem_and_build_samplers( stbir__sample
alloc_ring_buffer_num_entries = conservative_split_output_size; alloc_ring_buffer_num_entries = conservative_split_output_size;
ring_buffer_size = alloc_ring_buffer_num_entries * ring_buffer_length_bytes; ring_buffer_size = alloc_ring_buffer_num_entries * ring_buffer_length_bytes;
(void)ring_buffer_size;
// The vertical buffer is used differently, depending on whether we are scattering // The vertical buffer is used differently, depending on whether we are scattering
// the vertical scanlines, or gathering them. // the vertical scanlines, or gathering them.

@ -1 +1 @@
Subproject commit 8411810ab5df8f330f9d7cf96a96179375dc4704 Subproject commit 903bac9fc10e48005d68a0f9e9d060de538e898c

@ -1 +1 @@
Subproject commit 240ab5890b2e8da294937a1710b021ac3f271472 Subproject commit a1b06823fe2d964a62fda99385499b218cf5cea5

@ -1 +1 @@
Subproject commit cfab14287405a0d34f6a0fec1336f46415728fcf Subproject commit 6675317107257c2cc16c947b359d557821d85bf2

View File

@ -17,6 +17,7 @@
*/ */
#include <blt/gfx/model.h> #include <blt/gfx/model.h>
#include <blt/std/memory_util.h> #include <blt/std/memory_util.h>
#include "blt/std/logging.h"
namespace blt::gfx namespace blt::gfx
{ {
@ -24,17 +25,19 @@ namespace blt::gfx
{ {
size_t next_size = blt::mem::next_byte_allocation(size_); size_t next_size = blt::mem::next_byte_allocation(size_);
auto* new_data = new vbo_t[next_size]; auto* new_data = new vbo_type[next_size];
if (std::holds_alternative<vbo_t*>(data_)) if (std::holds_alternative<vbo_type*>(data_))
{ {
auto* ptr = std::get<vbo_t*>(data_); auto* ptr = std::get<vbo_type*>(data_);
std::memcpy(new_data, ptr, sizeof(vbo_t) * size_); for (size_t i = 0; i < size_; i++)
new_data[i] = ptr[i];
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(), sizeof(vbo_t) * DATA_SIZE); for (size_t i = 0; i < DATA_SIZE; i++)
new_data[i] = data[i];
} }
data_ = new_data; data_ = new_data;
size_ = next_size; size_ = next_size;
@ -43,13 +46,13 @@ namespace blt::gfx
static_dynamic_array::static_dynamic_array() static_dynamic_array::static_dynamic_array()
{} {}
vbo_t& static_dynamic_array::operator[](size_t index) static_dynamic_array::vbo_type& static_dynamic_array::operator[](size_t index)
{ {
if (index >= size_) if (index >= size_)
swap(); swap();
max = std::max(index, max); max = std::max(index, max);
if (std::holds_alternative<vbo_t*>(data_)) if (std::holds_alternative<vbo_type*>(data_))
return std::get<vbo_t*>(data_)[index]; return std::get<vbo_type*>(data_)[index];
else else
return std::get<array_t>(data_)[index]; return std::get<array_t>(data_)[index];
} }
@ -102,6 +105,11 @@ namespace blt::gfx
glBindBuffer(buffer_type, bufferID_); glBindBuffer(buffer_type, bufferID_);
} }
void vbo_t::unbind() const
{
glBindBuffer(buffer_type, 0);
}
/* /*
* ---------------------------- * ----------------------------
* basic_vertex_array * basic_vertex_array
@ -117,8 +125,7 @@ namespace blt::gfx
{ {
// free all VBOs // free all VBOs
for (size_t i = 0; i < VBOs.used(); i++) for (size_t i = 0; i < VBOs.used(); i++)
if (VBOs[i].bufferID_) VBOs[i] = nullptr;
VBOs[i].destroy();
// then free the vertex array // then free the vertex array
glDeleteVertexArrays(1, &vaoID); glDeleteVertexArrays(1, &vaoID);
} }
@ -132,7 +139,7 @@ namespace blt::gfx
glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset); glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset);
glEnableVertexAttribArray(attribute_number); glEnableVertexAttribArray(attribute_number);
VBOs[attribute_number] = vbo; VBOs[attribute_number] = std::make_shared<vbo_t_owner>(vbo);
unbind(); unbind();
} }

View File

@ -78,6 +78,7 @@ namespace blt::gfx
auto* back = loaded_textures.back(); auto* back = loaded_textures.back();
textures_2d.insert({back->getName(), new texture_gl2D(back->texture())}); textures_2d.insert({back->getName(), new texture_gl2D(back->texture())});
BLT_DEBUG("Loaded texture '%s'", back->getName().c_str()); BLT_DEBUG("Loaded texture '%s'", back->getName().c_str());
delete back;
loaded_textures.pop_back(); loaded_textures.pop_back();
} }
} }