summaryrefslogtreecommitdiff
path: root/gfx/models/mesh.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2026-03-11 20:45:05 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2026-03-11 20:45:05 +0000
commitc403a71564def731f4d3b80d6ff63f08aa3c7ea3 (patch)
treee4302316295639da9a2d10181c498986c8806754 /gfx/models/mesh.h
parentAdd missing typename exporting base::value_type from InstanceVertices (diff)
downloadilt-c403a71564def731f4d3b80d6ff63f08aa3c7ea3.tar.bz2
ilt-c403a71564def731f4d3b80d6ff63f08aa3c7ea3.tar.xz
ilt-c403a71564def731f4d3b80d6ff63f08aa3c7ea3.zip
Reuse vertex array objects for common structures with DSA
Slashes the number of VAOs required and the amount of setup required.
Diffstat (limited to 'gfx/models/mesh.h')
-rw-r--r--gfx/models/mesh.h26
1 files changed, 20 insertions, 6 deletions
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 <gfx/gl/glVertexArray.h>
#include <glad/gl.h>
#include <ranges>
#include <span>
#include <stdTypeDefs.h>
+class Vertex;
+
class MeshBase {
public:
class Dimensions {
@@ -35,10 +37,11 @@ public:
}
protected:
- MeshBase(GLsizei numIndices, GLenum mode, const std::vector<RelativePosition3D> &);
+ MeshBase(GLsizei numIndices, GLenum mode, const std::vector<RelativePosition3D> &, GLsizei vertexStride);
- glVertexArray vertexArrayObject;
+ std::shared_ptr<glVertexArray> vertexArrayObject;
glBuffers<2> vertexArrayBuffers;
+ GLsizei vertexStride;
GLsizei numIndices;
GLenum mode;
Dimensions dimensions;
@@ -50,18 +53,29 @@ public:
MeshBase {static_cast<GLsizei>(indices.size()), mode,
materializeRange(vertices | std::views::transform([](const auto & vertex) {
return static_cast<RelativePosition3D>(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<glVertexArray>();
+ configureVAO(*vertexArrayObject, 0);
+ }
}
auto
configureVAO(glVertexArray & vao, GLuint divisor) const
{
- return vao.configure().addAttribsFor<V>(divisor, vertexArrayBuffers[0]).addIndices(vertexArrayBuffers[1]);
+ glDebugScope _ {0};
+ return vao.configure().addAttribsFor<V>(divisor);
}
+
+protected:
+ static std::weak_ptr<glVertexArray> commonVertexArrayObject;
};
+template<typename T> std::weak_ptr<glVertexArray> MeshT<T>::commonVertexArrayObject;
+
using Mesh = MeshT<Vertex>;