]> git.tdb.fi Git - libs/gl.git/commitdiff
Better way of refreshing VertexSetup
authorMikko Rasa <tdb@tdb.fi>
Mon, 25 Jan 2021 23:17:52 +0000 (01:17 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 25 Jan 2021 23:33:08 +0000 (01:33 +0200)
It can now disable attribute arrays which are no longer used.

source/mesh.cpp
source/vertexformat.cpp
source/vertexformat.h
source/vertexsetup.cpp
source/vertexsetup.h

index 51468f2f85f850cd7b59405b97e7e2ff17e32432..ae08fd83376f1ca838b44c44f717b5f8b668b937 100644 (file)
@@ -230,7 +230,7 @@ void Mesh::Loader::vertices(const vector<VertexComponent> &c)
        if(allow_gl_calls)
        {
                obj.check_buffers(VERTEX_BUFFER);
-               obj.vtx_setup.set_vertex_array(obj.vertices);
+               obj.vtx_setup.refresh();
        }
 }
 
@@ -285,7 +285,7 @@ bool Mesh::AsyncLoader::process()
        else if(phase==1)
        {
                mesh.resize_buffers();
-               mesh.vtx_setup.set_vertex_array(vertices);
+               mesh.vtx_setup.refresh();
                vertex_updater = mesh.vertices.refresh_async();
                if(!mesh.batches.empty())
                        index_updater = mesh.batches.front().refresh_async();
index 037305f8d74251c9e6cd882540df733e292afc30..6c2e2933ba0f06f7dc9c483321dc9b7fcf2a4138 100644 (file)
@@ -43,6 +43,13 @@ VertexFormat VertexFormat::operator,(unsigned i) const
        return r;
 }
 
+bool VertexFormat::operator==(const VertexFormat &other) const
+{
+       if(count!=other.count)
+               return false;
+       return equal(components, components+count, other.components);
+}
+
 unsigned VertexFormat::stride() const
 {
        unsigned s = 0;
index bcb5b9251ebc73468237e83f2065f9c6a62187ed..3561292245fc640deb2859629cbf8f39c1776e5b 100644 (file)
@@ -44,6 +44,8 @@ public:
 
        VertexFormat operator,(VertexComponent c) const;
        VertexFormat operator,(unsigned i) const;
+       bool operator==(const VertexFormat &) const;
+       bool operator!=(const VertexFormat &other) const { return !(*this==other); }
 
        bool empty() const { return !count; }
        const unsigned char *begin() const { return components; }
index 19edfb39e4a6da0b2ddfe6a0d5566d8d27c56254..d50406a1ab70ce44e7caf645d46009a308c8e86f 100644 (file)
@@ -42,7 +42,8 @@ void VertexSetup::set_vertex_array(const VertexArray &a)
                throw invalid_argument("VertexSetup::set_vertex_array");
 
        vertex_array = &a;
-       update(VERTEX_ARRAY);
+       update(get_update_mask(VERTEX_ARRAY, vertex_format, *vertex_array));
+       vertex_format = vertex_array->get_format();
 }
 
 void VertexSetup::set_instance_array(const VertexArray *a)
@@ -56,7 +57,8 @@ void VertexSetup::set_instance_array(const VertexArray *a)
        }
 
        inst_array = a;
-       update(INSTANCE_ARRAY);
+       update(get_update_mask(INSTANCE_ARRAY, inst_format, *inst_array));
+       inst_format = inst_array->get_format();
 }
 
 void VertexSetup::set_index_buffer(const Buffer &ibuf)
@@ -65,6 +67,34 @@ void VertexSetup::set_index_buffer(const Buffer &ibuf)
        update(INDEX_BUFFER);
 }
 
+void VertexSetup::refresh()
+{
+       if(vertex_array && vertex_array->get_format()!=vertex_format)
+               set_vertex_array(*vertex_array);
+
+       if(inst_array && inst_array->get_format()!=inst_format)
+               set_instance_array(inst_array);
+}
+
+unsigned VertexSetup::get_attribs(const VertexFormat &fmt)
+{
+       unsigned mask = 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);
+               mask |= 1<<t;
+       }
+       return mask;
+}
+
+unsigned VertexSetup::get_update_mask(unsigned base, const VertexFormat &cur_fmt, const VertexArray &new_array)
+{
+       unsigned unused = get_attribs(cur_fmt)&~get_attribs(new_array.get_format());
+       return base | (unused ? UNUSED_ATTRIBS | (unused<<ATTRIB_SHIFT) : 0);
+}
+
 void VertexSetup::update(unsigned mask) const
 {
        static bool direct = ARB_direct_state_access && ARB_vertex_attrib_binding;
@@ -74,14 +104,23 @@ void VertexSetup::update(unsigned mask) const
                return;
        }
 
+       if(mask&UNUSED_ATTRIBS)
+       {
+               for(unsigned i=0, am=mask>>ATTRIB_SHIFT; am; ++i, am>>=1)
+                       if(am&1)
+                       {
+                               if(direct)
+                                       glDisableVertexArrayAttrib(id, i);
+                               else
+                                       glDisableVertexAttribArray(i);
+                       }
+       }
+
        if(mask&VERTEX_ARRAY)
                update_vertex_array(*vertex_array, 0, 0, direct);
 
-       if(mask&INSTANCE_ARRAY)
-       {
-               if(inst_array)
-                       update_vertex_array(*inst_array, 1, 1, direct);
-       }
+       if((mask&INSTANCE_ARRAY) && inst_array)
+               update_vertex_array(*inst_array, 1, 1, direct);
 
        if(mask&INDEX_BUFFER)
        {
index 9e7409ac64b71f3574a32c778964089e21bdf3bf..bae4c5f36c2245d194eacc6b08b1df25658dade7 100644 (file)
@@ -2,6 +2,7 @@
 #define MSP_GL_VERTEXSETUP_H_
 
 #include "bindable.h"
+#include "vertexformat.h"
 
 namespace Msp {
 namespace GL {
@@ -19,13 +20,17 @@ private:
        {
                VERTEX_ARRAY = 1,
                INSTANCE_ARRAY = 2,
-               INDEX_BUFFER = 4
+               INDEX_BUFFER = 4,
+               UNUSED_ATTRIBS = 8,
+               ATTRIB_SHIFT = 4
        };
 
        unsigned id;
        mutable unsigned dirty;
        const VertexArray *vertex_array;
+       VertexFormat vertex_format;
        const VertexArray *inst_array;
+       VertexFormat inst_format;
        const Buffer *index_buffer;
 
 public:
@@ -35,11 +40,14 @@ public:
        void set_vertex_array(const VertexArray &);
        void set_instance_array(const VertexArray *);
        void set_index_buffer(const Buffer &);
+       void refresh();
        const VertexArray *get_vertex_array() const { return vertex_array; }
        const VertexArray *get_instance_array() const { return inst_array; }
        const Buffer *get_index_buffer() const { return index_buffer; }
 
 private:
+       static unsigned get_attribs(const VertexFormat &);
+       static unsigned get_update_mask(unsigned, const VertexFormat &, const VertexArray &);
        void update(unsigned) const;
        void update_vertex_array(const VertexArray &, unsigned, unsigned, bool) const;