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.cpp | 11 ++++++++--- gfx/models/mesh.h | 26 ++++++++++++++++++++------ 2 files changed, 28 insertions(+), 9 deletions(-) (limited to 'gfx/models') diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 88314ae..6a53f52 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -1,7 +1,8 @@ #include "mesh.h" -MeshBase::MeshBase(GLsizei m_numIndices, GLenum mode, const std::vector & positions) : - numIndices {m_numIndices}, mode {mode}, dimensions {positions} +MeshBase::MeshBase( + GLsizei m_numIndices, GLenum mode, const std::vector & positions, GLsizei vertexStride) : + vertexStride {vertexStride}, numIndices {m_numIndices}, mode {mode}, dimensions {positions} { } @@ -31,7 +32,9 @@ MeshBase::Dimensions::extents(const std::span position void MeshBase::draw() const { - glBindVertexArray(vertexArrayObject); + glBindVertexArray(*vertexArrayObject); + glVertexArrayVertexBuffer(*vertexArrayObject, 0, vertexArrayBuffers[0], 0, vertexStride); + glVertexArrayElementBuffer(*vertexArrayObject, vertexArrayBuffers[1]); glDrawElements(mode, numIndices, GL_UNSIGNED_INT, nullptr); @@ -42,6 +45,8 @@ void MeshBase::drawInstanced(GLuint vao, GLsizei count, GLuint base) const { glBindVertexArray(vao); + glVertexArrayVertexBuffer(vao, 0, vertexArrayBuffers[0], 0, vertexStride); + glVertexArrayElementBuffer(vao, vertexArrayBuffers[1]); glDrawElementsInstancedBaseInstance(mode, numIndices, GL_UNSIGNED_INT, nullptr, count, base); 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