From f5ac652497f22b159b5e44bf69f44092ff91936b Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 18 Jun 2019 13:28:04 +0300 Subject: [PATCH] Only update the changed parts of VertexSetup 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 | 25 ++++++++++++++++--------- source/vertexsetup.h | 12 ++++++++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/source/vertexsetup.cpp b/source/vertexsetup.cpp index f6a71f49..275585be 100644 --- a/source/vertexsetup.cpp +++ b/source/vertexsetup.cpp @@ -10,7 +10,7 @@ namespace Msp { namespace GL { VertexSetup::VertexSetup(): - dirty(false), + dirty(0), array(0), index_buffer(0) { @@ -28,23 +28,32 @@ VertexSetup::~VertexSetup() 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; - update(); + update(INDEX_BUFFER); } -void VertexSetup::update() const +void VertexSetup::update(unsigned mask) const { if(current()!=this) { - dirty = true; + dirty |= mask; 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(); @@ -63,8 +72,6 @@ void VertexSetup::update() const glEnableVertexAttribArray(t); ptr += sz; } - - glBindBuffer(ELEMENT_ARRAY_BUFFER, index_buffer->get_id()); } void VertexSetup::bind() const @@ -74,8 +81,8 @@ void VertexSetup::bind() const glBindVertexArray(id); if(dirty) { - update(); - dirty = false; + update(dirty); + dirty = 0; } } } diff --git a/source/vertexsetup.h b/source/vertexsetup.h index 466eed40..c4cadfca 100644 --- a/source/vertexsetup.h +++ b/source/vertexsetup.h @@ -15,8 +15,14 @@ objects. Intended for internal use. class VertexSetup: public Bindable { private: + enum ComponentMask + { + VERTEX_ARRAY = 1, + INDEX_BUFFER = 2 + }; + unsigned id; - mutable bool dirty; + mutable unsigned dirty; const VertexArray *array; const Buffer *index_buffer; @@ -25,11 +31,13 @@ public: ~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 update() const; + void update(unsigned) const; + void update_vertex_array() const; public: void bind() const; -- 2.43.0