Move vertex array object management out of Mesh
authorMikko Rasa <tdb@tdb.fi>
Tue, 18 Jun 2019 08:42:22 +0000 (11:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 18 Jun 2019 09:48:27 +0000 (12:48 +0300)
It's called VertexSetup because VertexArray was already in use.  On the
other hand I don't want to tie VAOs with the VertexArray class because
instanced rendering will need two VertexArrays in a single VAO.

source/buffer.cpp
source/mesh.cpp
source/mesh.h
source/vertexsetup.cpp [new file with mode: 0644]
source/vertexsetup.h [new file with mode: 0644]

index 2b18d7d7d7d3bf0a47611207850aec5aabc4cc9d..4b485ddad6cb266d1d96d2e844c40583cb9ace3a 100644 (file)
@@ -3,8 +3,8 @@
 #include <msp/gl/extensions/arb_map_buffer_range.h>
 #include "buffer.h"
 #include "error.h"
-#include "mesh.h"
 #include "misc.h"
+#include "vertexsetup.h"
 
 using namespace std;
 
@@ -127,10 +127,10 @@ void Buffer::bind_to(BufferType t) const
        if(t!=type)
                require_buffer_type(t);
        if(t==ELEMENT_ARRAY_BUFFER)
-               if(const Mesh *m = Mesh::current())
+               if(const VertexSetup *vs = VertexSetup::current())
                {
-                       // Don't change the binding in a mesh's vertex array object
-                       if(this==m->get_index_buffer())
+                       // Don't change the binding in a vertex array object
+                       if(this==vs->get_index_buffer())
                                return;
                        throw invalid_operation("Buffer::bind_to(ELEMENT_ARRAY_BUFFER)");
                }
@@ -141,14 +141,14 @@ void Buffer::bind_to(BufferType t) const
 const Buffer *Buffer::current(BufferType t)
 {
        if(t==ELEMENT_ARRAY_BUFFER)
-               if(const Mesh *m = Mesh::current())
-                       return m->get_index_buffer();
+               if(const VertexSetup *vs = VertexSetup::current())
+                       return vs->get_index_buffer();
        return binding(t);
 }
 
 void Buffer::unbind_from(BufferType type)
 {
-       if(type==ELEMENT_ARRAY_BUFFER && Mesh::current())
+       if(type==ELEMENT_ARRAY_BUFFER && VertexSetup::current())
                throw invalid_operation("Buffer::unbind_from(ELEMENT_ARRAY_BUFFER)");
        if(set_current(type, 0))
                glBindBuffer(type, 0);
index b3b719c516942c42e5012a8ad92ad21dec06958d..a463ba7fb7c7a1414ee42ed116264d672730f514 100644 (file)
@@ -7,6 +7,7 @@
 #include "mesh.h"
 #include "renderer.h"
 #include "resourcemanager.h"
+#include "vertexsetup.h"
 
 using namespace std;
 
@@ -29,9 +30,8 @@ void Mesh::init(ResourceManager *rm)
 {
        vbuf = 0;
        ibuf = 0;
-       vao_id = 0;
+       vtx_setup = 0;
        defer_buffers = ARB_vertex_buffer_object;
-       dirty = true;
        disallow_rendering = false;
        winding = 0;
 
@@ -42,10 +42,9 @@ void Mesh::init(ResourceManager *rm)
 Mesh::~Mesh()
 {
        set_manager(0);
+       delete vtx_setup;
        delete vbuf;
        delete ibuf;
-       if(vao_id)
-               glDeleteVertexArrays(1, &vao_id);
 }
 
 void Mesh::clear()
@@ -80,33 +79,12 @@ void Mesh::create_buffers()
        if(!ibuf)
                ibuf = new Buffer(ELEMENT_ARRAY_BUFFER);
 
-       if(ARB_vertex_array_object && !vao_id)
-               glGenVertexArrays(1, &vao_id);
-}
-
-void Mesh::setup_vao() const
-{
-       Bind bind_vbuf(vbuf, ARRAY_BUFFER);
-
-       const VertexFormat &fmt = vertices.get_format();
-       unsigned stride = get_stride(fmt)*sizeof(float);
-       float *ptr = 0;
-       for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
+       if(ARB_vertex_array_object && !vtx_setup)
        {
-               unsigned t = get_component_type(*c);
-               if(t>=get_component_type(ATTRIB1))
-                       t -= get_component_type(ATTRIB1);
-               unsigned sz = get_component_size(*c);
-               if(*c==COLOR4_UBYTE)
-                       glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
-               else
-                       glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
-               glEnableVertexAttribArray(t);
-               ptr += sz;
+               vtx_setup = new VertexSetup;
+               vtx_setup->set_vertex_array(vertices);
+               vtx_setup->set_index_buffer(*ibuf);
        }
-       glBindBuffer(ELEMENT_ARRAY_BUFFER, ibuf->get_id());
-
-       dirty = false;
 }
 
 unsigned Mesh::get_n_vertices() const
@@ -199,26 +177,24 @@ void Mesh::draw(Renderer &renderer) const
 
 void Mesh::bind() const
 {
-       /* If VAOs are not supported, vao_id is zero and set_current won't get
+       /* If VAOs are not supported, vtx_setup is zero and set_current won't get
        called.  Thus unbind won't try to call a null function either. */
-       if(!vao_id)
+       if(!vtx_setup)
        {
                unbind();
                vertices.apply(false);
        }
        else if(set_current(this))
        {
-               glBindVertexArray(vao_id);
+               vtx_setup->bind();
                vertices.refresh();
-               if(dirty)
-                       setup_vao();
        }
 }
 
 void Mesh::unbind()
 {
        if(set_current(0))
-               glBindVertexArray(0);
+               VertexSetup::unbind();
 }
 
 Resource::AsyncLoader *Mesh::load(IO::Seekable &io, const Resources *)
@@ -266,7 +242,9 @@ void Mesh::Loader::vertices(const vector<VertexComponent> &c)
        for(vector<VertexComponent>::const_iterator i=c.begin(); i!=c.end(); ++i)
                fmt = (fmt, *i);
        obj.vertices.reset(fmt);
-       obj.dirty = true;
+       if(obj.vtx_setup)
+               // Set it again to force the vertex setup to update
+               obj.vtx_setup->set_vertex_array(obj.vertices);
        load_sub(obj.vertices);
 }
 
index 70370633fd0425cf88222ceab22b40d012604a35..db0163e17ef3521ef471109758b9d846c6649782 100644 (file)
@@ -12,6 +12,7 @@ namespace GL {
 
 class Buffer;
 class Renderer;
+class VertexSetup;
 
 /**
 Raw mesh data, consisting of a VertexArray and one or more Batches.  Though a
@@ -55,7 +56,7 @@ private:
        std::vector<Batch> batches;
        Buffer *vbuf;
        Buffer *ibuf;
-       unsigned vao_id;
+       VertexSetup *vtx_setup;
        bool defer_buffers;
        mutable bool dirty;
        bool disallow_rendering;
@@ -73,7 +74,6 @@ public:
        void use_buffers(bool);
 private:
        void create_buffers();
-       void setup_vao() const;
 
 public:
        const VertexArray &get_vertices() const { return vertices; }
diff --git a/source/vertexsetup.cpp b/source/vertexsetup.cpp
new file mode 100644 (file)
index 0000000..f6a71f4
--- /dev/null
@@ -0,0 +1,90 @@
+#include <msp/gl/extensions/arb_vertex_array_object.h>
+#include <msp/gl/extensions/arb_vertex_buffer_object.h>
+#include <msp/gl/extensions/arb_vertex_shader.h>
+#include "buffer.h"
+#include "gl.h"
+#include "vertexarray.h"
+#include "vertexsetup.h"
+
+namespace Msp {
+namespace GL {
+
+VertexSetup::VertexSetup():
+       dirty(false),
+       array(0),
+       index_buffer(0)
+{
+       static Require req(ARB_vertex_array_object);
+       glGenVertexArrays(1, &id);
+}
+
+VertexSetup::~VertexSetup()
+{
+       if(current()==this)
+               unbind();
+       glDeleteVertexArrays(1, &id);
+}
+
+void VertexSetup::set_vertex_array(const VertexArray &a)
+{
+       array = &a;
+       update();
+}
+
+void VertexSetup::set_index_buffer(const Buffer &ibuf)
+{
+       index_buffer = &ibuf;
+       update();
+}
+
+void VertexSetup::update() const
+{
+       if(current()!=this)
+       {
+               dirty = true;
+               return;
+       }
+
+       Bind bind_vbuf(array->get_buffer(), ARRAY_BUFFER);
+
+       const VertexFormat &fmt = array->get_format();
+       unsigned stride = get_stride(fmt)*sizeof(float);
+       float *ptr = 0;
+       for(const unsigned char *c=fmt.begin(); c!=fmt.end(); ++c)
+       {
+               unsigned t = get_component_type(*c);
+               if(t>=get_component_type(ATTRIB1))
+                       t -= get_component_type(ATTRIB1);
+               unsigned sz = get_component_size(*c);
+               if(*c==COLOR4_UBYTE)
+                       glVertexAttribPointer(t, 4, GL_UNSIGNED_BYTE, true, stride, ptr);
+               else
+                       glVertexAttribPointer(t, sz, GL_FLOAT, false, stride, ptr);
+               glEnableVertexAttribArray(t);
+               ptr += sz;
+       }
+
+       glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
+}
+
+void VertexSetup::bind() const
+{
+       if(set_current(this))
+       {
+               glBindVertexArray(id);
+               if(dirty)
+               {
+                       update();
+                       dirty = false;
+               }
+       }
+}
+
+void VertexSetup::unbind()
+{
+       if(set_current(0))
+               glBindVertexArray(0);
+}
+
+} // namespace GL
+} // namespace Msp
diff --git a/source/vertexsetup.h b/source/vertexsetup.h
new file mode 100644 (file)
index 0000000..466eed4
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef MSP_GL_VERTEXSETUP_H_
+#define MSP_GL_VERTEXSETUP_H_
+
+#include "bindable.h"
+
+namespace Msp {
+namespace GL {
+
+class VertexArray;
+
+/**
+Combines a VertexArray with an index buffer.  This wraps OpenGL's vertex array
+objects.  Intended for internal use.
+*/
+class VertexSetup: public Bindable<VertexSetup>
+{
+private:
+       unsigned id;
+       mutable bool dirty;
+       const VertexArray *array;
+       const Buffer *index_buffer;
+
+public:
+       VertexSetup();
+       ~VertexSetup();
+
+       void set_vertex_array(const VertexArray &);
+       void set_index_buffer(const Buffer &);
+       const Buffer *get_index_buffer() const { return index_buffer; }
+
+private:
+       void update() const;
+
+public:
+       void bind() const;
+       static void unbind();
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif