]> git.tdb.fi Git - libs/gl.git/commitdiff
Only update the changed parts of VertexSetup
authorMikko Rasa <tdb@tdb.fi>
Tue, 18 Jun 2019 10:28:04 +0000 (13:28 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 18 Jun 2019 10:33:37 +0000 (13:33 +0300)
Besides being a minor optimization, this fixes a crash if update gets
called before both vertex array and index buffer are set.

source/vertexsetup.cpp
source/vertexsetup.h

index f6a71f4996162b9addaa3b337c6a5ea35121e1be..275585beac07c39a2ef260229a0c22727081f3af 100644 (file)
@@ -10,7 +10,7 @@ namespace Msp {
 namespace GL {
 
 VertexSetup::VertexSetup():
 namespace GL {
 
 VertexSetup::VertexSetup():
-       dirty(false),
+       dirty(0),
        array(0),
        index_buffer(0)
 {
        array(0),
        index_buffer(0)
 {
@@ -28,23 +28,32 @@ VertexSetup::~VertexSetup()
 void VertexSetup::set_vertex_array(const VertexArray &a)
 {
        array = &a;
 void VertexSetup::set_vertex_array(const VertexArray &a)
 {
        array = &a;
-       update();
+       update(VERTEX_ARRAY);
 }
 
 void VertexSetup::set_index_buffer(const Buffer &ibuf)
 {
        index_buffer = &ibuf;
 }
 
 void VertexSetup::set_index_buffer(const Buffer &ibuf)
 {
        index_buffer = &ibuf;
-       update();
+       update(INDEX_BUFFER);
 }
 
 }
 
-void VertexSetup::update() const
+void VertexSetup::update(unsigned mask) const
 {
        if(current()!=this)
        {
 {
        if(current()!=this)
        {
-               dirty = true;
+               dirty |= mask;
                return;
        }
 
                return;
        }
 
+       if(mask&VERTEX_ARRAY)
+               update_vertex_array();
+
+       if(mask&INDEX_BUFFER)
+               glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
+}
+
+void VertexSetup::update_vertex_array() const
+{
        Bind bind_vbuf(array->get_buffer(), ARRAY_BUFFER);
 
        const VertexFormat &fmt = array->get_format();
        Bind bind_vbuf(array->get_buffer(), ARRAY_BUFFER);
 
        const VertexFormat &fmt = array->get_format();
@@ -63,8 +72,6 @@ void VertexSetup::update() const
                glEnableVertexAttribArray(t);
                ptr += sz;
        }
                glEnableVertexAttribArray(t);
                ptr += sz;
        }
-
-       glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id());
 }
 
 void VertexSetup::bind() const
 }
 
 void VertexSetup::bind() const
@@ -74,8 +81,8 @@ void VertexSetup::bind() const
                glBindVertexArray(id);
                if(dirty)
                {
                glBindVertexArray(id);
                if(dirty)
                {
-                       update();
-                       dirty = false;
+                       update(dirty);
+                       dirty = 0;
                }
        }
 }
                }
        }
 }
index 466eed40659d3d61fa8c9a27b00520ac43849c73..c4cadfcaed49a9206d3ef500df6688b4f762edae 100644 (file)
@@ -15,8 +15,14 @@ objects.  Intended for internal use.
 class VertexSetup: public Bindable<VertexSetup>
 {
 private:
 class VertexSetup: public Bindable<VertexSetup>
 {
 private:
+       enum ComponentMask
+       {
+               VERTEX_ARRAY = 1,
+               INDEX_BUFFER = 2
+       };
+
        unsigned id;
        unsigned id;
-       mutable bool dirty;
+       mutable unsigned dirty;
        const VertexArray *array;
        const Buffer *index_buffer;
 
        const VertexArray *array;
        const Buffer *index_buffer;
 
@@ -25,11 +31,13 @@ public:
        ~VertexSetup();
 
        void set_vertex_array(const VertexArray &);
        ~VertexSetup();
 
        void set_vertex_array(const VertexArray &);
+       void set_instance_array(const VertexArray &);
        void set_index_buffer(const Buffer &);
        const Buffer *get_index_buffer() const { return index_buffer; }
 
 private:
        void set_index_buffer(const Buffer &);
        const Buffer *get_index_buffer() const { return index_buffer; }
 
 private:
-       void update() const;
+       void update(unsigned) const;
+       void update_vertex_array() const;
 
 public:
        void bind() const;
 
 public:
        void bind() const;