Brett 2023-12-28 00:10:02 -05:00
parent 2e1eb6707e
commit 5fd86f274b
2 changed files with 43 additions and 12 deletions

View File

@ -23,6 +23,7 @@
#include <vector> #include <vector>
#include <blt/gfx/gl_includes.h> #include <blt/gfx/gl_includes.h>
#include <variant> #include <variant>
#include <array>
namespace blt::gfx namespace blt::gfx
{ {
@ -35,22 +36,39 @@ 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 class static_dynamic_array
{ {
private: private:
static constexpr size_t DATA_SIZE = 8; static constexpr size_t DATA_SIZE = 8;
std::variant<GLuint[DATA_SIZE], GLuint*> data_; typedef std::array<GLuint, DATA_SIZE> array_t;
std::variant<array_t, GLuint*> data_;
size_t size_ = DATA_SIZE; size_t size_ = DATA_SIZE;
void swap();
public: public:
static_dynamic_array() = default; static_dynamic_array();
static_dynamic_array(const static_dynamic_array& copy) = delete; 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=(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<GLuint*>(data_))
return std::get<GLuint*>(data_)[index];
else
return std::get<array_t>(data_)[index];
}
~static_dynamic_array() ~static_dynamic_array()
{ {
@ -82,6 +100,7 @@ namespace blt::gfx
~vbo_t(); ~vbo_t();
}; };
GLuint vaoID; GLuint vaoID;
public: public:
basic_vertex_array(); basic_vertex_array();

View File

@ -16,20 +16,32 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <blt/gfx/model.h> #include <blt/gfx/model.h>
#include <blt/std/utility.h> #include <blt/std/memory_util.h>
namespace blt::gfx namespace blt::gfx
{ {
static_dynamic_array::static_dynamic_array(static_dynamic_array&& move) noexcept void static_dynamic_array::swap()
{ {
if (std::holds_alternative<GLuint[DATA_SIZE]>(move.data_)){ size_t next_size = blt::mem::next_byte_allocation(size_);
data_ = std::move(std::get<GLuint[DATA_SIZE]>(move.data_)); auto* new_data = new GLuint[next_size];
std::memset(new_data, 0, next_size);
if (std::holds_alternative<GLuint*>(data_))
{
auto ptr = std::get<GLuint*>(data_);
std::memcpy(new_data, ptr, size_);
delete ptr;
} else { } else {
data_ = std::get<GLuint*>(move.data_); auto data = std::get<array_t>(data_);
size_ = move.size_; std::memcpy(new_data, data.data(), DATA_SIZE);
} }
move.data_ = nullptr; data_ = new_data;
move.size_ = 0; size_ = next_size;
}
static_dynamic_array::static_dynamic_array()
{
auto ptr = std::get<array_t>(data_);
std::memset(ptr.data(), 0, DATA_SIZE);
} }
/* /*