From 5bff8b675c1f4a4412a3523a5fdba33a1ac0a441 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 8 Jan 2024 21:12:31 -0500 Subject: [PATCH] shared vbos --- include/blt/gfx/loader/obj_loader.h | 15 +++++-- include/blt/gfx/model.h | 49 +++++++++++++++-------- include/blt/gfx/stb/stb_image_resize2.h | 1 + libraries/BLT | 2 +- libraries/imgui | 2 +- libraries/openal-soft | 2 +- src/blt/gfx/model.cpp | 29 +++++++++----- src/blt/gfx/renderer/resource_manager.cpp | 1 + 8 files changed, 68 insertions(+), 33 deletions(-) diff --git a/include/blt/gfx/loader/obj_loader.h b/include/blt/gfx/loader/obj_loader.h index 3c4b72f..1e5beb6 100644 --- a/include/blt/gfx/loader/obj_loader.h +++ b/include/blt/gfx/loader/obj_loader.h @@ -28,9 +28,18 @@ namespace blt::gfx { - typedef blt::vec3 vertex_t; - typedef blt::vec2 uv_t; - typedef blt::vec3 normal_t; + typedef blt::vec3f vertex_t; + typedef blt::vec2f uv_t; + typedef blt::vec3f normal_t; + + class model_data + { + public: + private: + std::vector vertices; + std::vector uvs; + std::vector normals; + }; struct face_t { diff --git a/include/blt/gfx/model.h b/include/blt/gfx/model.h index 93c7dcc..7036f99 100644 --- a/include/blt/gfx/model.h +++ b/include/blt/gfx/model.h @@ -28,14 +28,6 @@ namespace blt::gfx { - class model_data - { - public: - private: - std::vector vertices; - std::vector uvs; - std::vector normals; - }; struct vbo_t { @@ -48,6 +40,8 @@ namespace blt::gfx void bind() const; + void unbind() const; + void allocate(GLsizeiptr size, GLint mem_type = GL_STATIC_DRAW, const void* data = nullptr); template @@ -63,6 +57,28 @@ namespace blt::gfx 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 * This class is used to make that easier to handle @@ -71,8 +87,9 @@ namespace blt::gfx { private: static constexpr size_t DATA_SIZE = 8; - typedef std::array array_t; - std::variant data_; + using vbo_type = std::shared_ptr; + using array_t = std::array; + std::variant data_; size_t size_ = DATA_SIZE; size_t max = 0; @@ -89,7 +106,7 @@ namespace blt::gfx 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 { @@ -98,8 +115,8 @@ namespace blt::gfx ~static_dynamic_array() { - if (std::holds_alternative(data_)) - delete[] std::get(data_); + if (std::holds_alternative(data_)) + delete[] std::get(data_); } }; @@ -111,7 +128,7 @@ namespace blt::gfx private: GLuint vaoID; static_dynamic_array VBOs; - HASHSET used_attributes; + HASHSET used_attributes; vbo_t element; public: vertex_array(); @@ -155,10 +172,10 @@ namespace blt::gfx */ inline vbo_t& operator[](size_t index) { - return VBOs[index]; + return VBOs[index]->vbo; } - inline void bind() + inline void bind() const { glBindVertexArray(vaoID); } diff --git a/include/blt/gfx/stb/stb_image_resize2.h b/include/blt/gfx/stb/stb_image_resize2.h index 5a4ece6..65e17b3 100644 --- a/include/blt/gfx/stb/stb_image_resize2.h +++ b/include/blt/gfx/stb/stb_image_resize2.h @@ -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; 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 scanlines, or gathering them. diff --git a/libraries/BLT b/libraries/BLT index 8411810..903bac9 160000 --- a/libraries/BLT +++ b/libraries/BLT @@ -1 +1 @@ -Subproject commit 8411810ab5df8f330f9d7cf96a96179375dc4704 +Subproject commit 903bac9fc10e48005d68a0f9e9d060de538e898c diff --git a/libraries/imgui b/libraries/imgui index 240ab58..a1b0682 160000 --- a/libraries/imgui +++ b/libraries/imgui @@ -1 +1 @@ -Subproject commit 240ab5890b2e8da294937a1710b021ac3f271472 +Subproject commit a1b06823fe2d964a62fda99385499b218cf5cea5 diff --git a/libraries/openal-soft b/libraries/openal-soft index cfab142..6675317 160000 --- a/libraries/openal-soft +++ b/libraries/openal-soft @@ -1 +1 @@ -Subproject commit cfab14287405a0d34f6a0fec1336f46415728fcf +Subproject commit 6675317107257c2cc16c947b359d557821d85bf2 diff --git a/src/blt/gfx/model.cpp b/src/blt/gfx/model.cpp index 98fb460..89eacff 100644 --- a/src/blt/gfx/model.cpp +++ b/src/blt/gfx/model.cpp @@ -17,6 +17,7 @@ */ #include #include +#include "blt/std/logging.h" namespace blt::gfx { @@ -24,17 +25,19 @@ namespace blt::gfx { 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(data_)) + if (std::holds_alternative(data_)) { - auto* ptr = std::get(data_); - std::memcpy(new_data, ptr, sizeof(vbo_t) * size_); + auto* ptr = std::get(data_); + for (size_t i = 0; i < size_; i++) + new_data[i] = ptr[i]; delete[] ptr; } else { auto data = std::get(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; size_ = next_size; @@ -43,13 +46,13 @@ namespace blt::gfx 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_) swap(); max = std::max(index, max); - if (std::holds_alternative(data_)) - return std::get(data_)[index]; + if (std::holds_alternative(data_)) + return std::get(data_)[index]; else return std::get(data_)[index]; } @@ -102,6 +105,11 @@ namespace blt::gfx glBindBuffer(buffer_type, bufferID_); } + void vbo_t::unbind() const + { + glBindBuffer(buffer_type, 0); + } + /* * ---------------------------- * basic_vertex_array @@ -117,8 +125,7 @@ namespace blt::gfx { // free all VBOs for (size_t i = 0; i < VBOs.used(); i++) - if (VBOs[i].bufferID_) - VBOs[i].destroy(); + VBOs[i] = nullptr; // then free the vertex array glDeleteVertexArrays(1, &vaoID); } @@ -132,7 +139,7 @@ namespace blt::gfx glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset); glEnableVertexAttribArray(attribute_number); - VBOs[attribute_number] = vbo; + VBOs[attribute_number] = std::make_shared(vbo); unbind(); } diff --git a/src/blt/gfx/renderer/resource_manager.cpp b/src/blt/gfx/renderer/resource_manager.cpp index e564308..2a45177 100644 --- a/src/blt/gfx/renderer/resource_manager.cpp +++ b/src/blt/gfx/renderer/resource_manager.cpp @@ -78,6 +78,7 @@ namespace blt::gfx auto* back = loaded_textures.back(); textures_2d.insert({back->getName(), new texture_gl2D(back->texture())}); BLT_DEBUG("Loaded texture '%s'", back->getName().c_str()); + delete back; loaded_textures.pop_back(); } }