From b4d63e919e56b8939cad8a8ca6809c2e9c453e85 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Fri, 19 Jan 2018 02:58:57 +0100 Subject: [PATCH] gs-vertex: Allocate memory if none is assigned Allows code to directly use GS::Vertex for their own purposes without having to rely on GS::VertexBuffer, which comes with the added overhead of allocating GPU memory. --- source/gs-vertex.cpp | 28 ++++++++++++++++++++++++++++ source/gs-vertex.h | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/source/gs-vertex.cpp b/source/gs-vertex.cpp index 271e79a3..82463256 100644 --- a/source/gs-vertex.cpp +++ b/source/gs-vertex.cpp @@ -18,3 +18,31 @@ */ #include "gs-vertex.h" +#include "util-memory.h" + +GS::Vertex::Vertex() { + this->hasStore = true; + this->store = util::malloc_aligned(16, sizeof(vec3) * 3 + sizeof(uint32_t) + sizeof(vec4)*MAXIMUM_UVW_LAYERS); + this->position = reinterpret_cast(store); + this->normal = reinterpret_cast(reinterpret_cast(store) + (16 * 1)); + this->tangent = reinterpret_cast(reinterpret_cast(store) + (16 * 2)); + for (size_t n = 0; n < MAXIMUM_UVW_LAYERS; n++) { + this->uv[n] = reinterpret_cast(reinterpret_cast(store) + (16 * (2 + n))); + } + this->color = reinterpret_cast(reinterpret_cast(store) + (16 * (3 + MAXIMUM_UVW_LAYERS))); +} + +GS::Vertex::~Vertex() { + if (hasStore) + util::free_aligned(store); +} + +GS::Vertex::Vertex(vec3* p, vec3* n, vec3* t, uint32_t* col, vec4* uvs[MAXIMUM_UVW_LAYERS]) + : position(p), normal(n), tangent(t), color(col) { + if (uvs != nullptr) { + for (size_t idx = 0; idx < MAXIMUM_UVW_LAYERS; idx++) { + this->uv[idx] = uvs[idx]; + } + } + this->hasStore = false; +} diff --git a/source/gs-vertex.h b/source/gs-vertex.h index 88c7ea97..77e37834 100644 --- a/source/gs-vertex.h +++ b/source/gs-vertex.h @@ -35,5 +35,13 @@ namespace GS { vec3* tangent; uint32_t* color; vec4* uv[MAXIMUM_UVW_LAYERS]; + + Vertex(); + Vertex(vec3* p, vec3* n, vec3* t, uint32_t* col, vec4* uv[MAXIMUM_UVW_LAYERS]); + ~Vertex(); + + private: + bool hasStore; + void* store; }; }