diff --git a/include/blt/gfx/model.h b/include/blt/gfx/model.h index 70a20e2..2e07967 100644 --- a/include/blt/gfx/model.h +++ b/include/blt/gfx/model.h @@ -23,6 +23,7 @@ #include #include #include +#include namespace blt::gfx { @@ -35,22 +36,39 @@ namespace blt::gfx std::vector 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; - std::variant data_; + typedef std::array array_t; + std::variant data_; size_t size_ = DATA_SIZE; + + void swap(); public: - static_dynamic_array() = default; + static_dynamic_array(); static_dynamic_array(const static_dynamic_array& copy) = delete; - static_dynamic_array(static_dynamic_array&& move) noexcept; + 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) = delete; + static_dynamic_array& operator=(static_dynamic_array&& move) = default; + + GLuint& operator[](size_t index) + { + if (index >= size_) + swap(); + if (std::holds_alternative(data_)) + return std::get(data_)[index]; + else + return std::get(data_)[index]; + } ~static_dynamic_array() { @@ -82,6 +100,7 @@ namespace blt::gfx ~vbo_t(); }; + GLuint vaoID; public: basic_vertex_array(); diff --git a/src/blt/gfx/model.cpp b/src/blt/gfx/model.cpp index 7e14ddf..92e8499 100644 --- a/src/blt/gfx/model.cpp +++ b/src/blt/gfx/model.cpp @@ -16,20 +16,32 @@ * along with this program. If not, see . */ #include -#include +#include namespace blt::gfx { - static_dynamic_array::static_dynamic_array(static_dynamic_array&& move) noexcept + void static_dynamic_array::swap() { - if (std::holds_alternative(move.data_)){ - data_ = std::move(std::get(move.data_)); + size_t next_size = blt::mem::next_byte_allocation(size_); + auto* new_data = new GLuint[next_size]; + std::memset(new_data, 0, next_size); + if (std::holds_alternative(data_)) + { + auto ptr = std::get(data_); + std::memcpy(new_data, ptr, size_); + delete ptr; } else { - data_ = std::get(move.data_); - size_ = move.size_; + auto data = std::get(data_); + std::memcpy(new_data, data.data(), DATA_SIZE); } - move.data_ = nullptr; - move.size_ = 0; + data_ = new_data; + size_ = next_size; + } + + static_dynamic_array::static_dynamic_array() + { + auto ptr = std::get(data_); + std::memset(ptr.data(), 0, DATA_SIZE); } /*