From c403a71564def731f4d3b80d6ff63f08aa3c7ea3 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 11 Mar 2026 20:45:05 +0000 Subject: Reuse vertex array objects for common structures with DSA Slashes the number of VAOs required and the amount of setup required. --- gfx/models/mesh.h | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'gfx/models/mesh.h') diff --git a/gfx/models/mesh.h b/gfx/models/mesh.h index d4dbd68..fa12ae8 100644 --- a/gfx/models/mesh.h +++ b/gfx/models/mesh.h @@ -2,13 +2,15 @@ #include "config/types.h" #include "gfx/gl/glBuffer.h" -#include "gfx/models/vertex.h" +#include "gfx/gl/gldebug.h" #include #include #include #include #include +class Vertex; + class MeshBase { public: class Dimensions { @@ -35,10 +37,11 @@ public: } protected: - MeshBase(GLsizei numIndices, GLenum mode, const std::vector &); + MeshBase(GLsizei numIndices, GLenum mode, const std::vector &, GLsizei vertexStride); - glVertexArray vertexArrayObject; + std::shared_ptr vertexArrayObject; glBuffers<2> vertexArrayBuffers; + GLsizei vertexStride; GLsizei numIndices; GLenum mode; Dimensions dimensions; @@ -50,18 +53,29 @@ public: MeshBase {static_cast(indices.size()), mode, materializeRange(vertices | std::views::transform([](const auto & vertex) { return static_cast(vertex.pos); - }))} + })), + sizeof(V)} { + glDebugScope _ {0}; vertexArrayBuffers[0].storage(vertices, 0); vertexArrayBuffers[1].storage(indices, 0); - configureVAO(vertexArrayObject, 0); + if (!(vertexArrayObject = commonVertexArrayObject.lock())) { + commonVertexArrayObject = vertexArrayObject = std::make_shared(); + configureVAO(*vertexArrayObject, 0); + } } auto configureVAO(glVertexArray & vao, GLuint divisor) const { - return vao.configure().addAttribsFor(divisor, vertexArrayBuffers[0]).addIndices(vertexArrayBuffers[1]); + glDebugScope _ {0}; + return vao.configure().addAttribsFor(divisor); } + +protected: + static std::weak_ptr commonVertexArrayObject; }; +template std::weak_ptr MeshT::commonVertexArrayObject; + using Mesh = MeshT; -- cgit v1.3