From: Mikko Rasa Date: Tue, 23 Sep 2014 17:11:16 +0000 (+0300) Subject: Refactor the internal interface of Bufferable a bit X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=98cc25ffe956bc162c053c96df659ba40dfe2d6e Refactor the internal interface of Bufferable a bit --- diff --git a/source/batch.cpp b/source/batch.cpp index 7100653e..1b0c47bf 100644 --- a/source/batch.cpp +++ b/source/batch.cpp @@ -196,11 +196,6 @@ void Batch::append_index(unsigned i) data.push_back(i); } -void Batch::upload_data() const -{ - get_buffer()->sub_data(get_offset(), data.size(), &data[0]); -} - unsigned Batch::get_index_size() const { if(data_type==UNSIGNED_SHORT) diff --git a/source/batch.h b/source/batch.h index 907a0b7f..0ce4ecf1 100644 --- a/source/batch.h +++ b/source/batch.h @@ -56,7 +56,7 @@ public: private: void append_index(unsigned); virtual unsigned get_data_size() const { return data.size(); } - virtual void upload_data() const; + virtual const void *get_data_pointer() const { return &data[0]; } unsigned get_index_size() const; public: unsigned size() const { return data.size()/get_index_size(); } diff --git a/source/bufferable.cpp b/source/bufferable.cpp index eb3f1c00..04793fb7 100644 --- a/source/bufferable.cpp +++ b/source/bufferable.cpp @@ -80,9 +80,8 @@ void Bufferable::update_offset() the buffer is actually used. */ } -void Bufferable::update_buffer() const +bool Bufferable::resize_buffer() const { - BindRestore bind(buffer, buffer->get_type()); if(offset+get_data_size()>=buffer->get_size()) { const Bufferable *last = this; @@ -93,17 +92,36 @@ void Bufferable::update_buffer() const if(total_size>buffer->get_size()) { buffer->data(total_size, 0); - /* Resizing the buffer invalidates its contents. Non-dirty data may - be in use, so reupload it. */ - for(const Bufferable *b=last; b; b=b->prev_in_buffer) - if(!b->dirty) - b->upload_data(); + return true; } } + return false; +} + +void Bufferable::update_buffer() const +{ + BindRestore bind(buffer, buffer->get_type()); + if(resize_buffer()) + { + /* Resizing the buffer invalidates its contents. Non-dirty data may + be in use, so reupload it. */ + for(const Bufferable *b=prev_in_buffer; b; b=b->prev_in_buffer) + if(!b->dirty) + b->upload_data(); + for(const Bufferable *b=next_in_buffer; b; b=b->next_in_buffer) + if(!b->dirty) + b->upload_data(); + } + upload_data(); dirty = false; } +void Bufferable::upload_data() const +{ + buffer->sub_data(offset, get_data_size(), get_data_pointer()); +} + } // namespace GL } // namespace Msp diff --git a/source/bufferable.h b/source/bufferable.h index c8c9393c..d7d7e45f 100644 --- a/source/bufferable.h +++ b/source/bufferable.h @@ -44,6 +44,9 @@ protected: /** Returns the amount of data to be stored in the buffer, in bytes. */ virtual unsigned get_data_size() const = 0; + /** Returns a pointer to the start of data in client memory. */ + virtual const void *get_data_pointer() const = 0; + /** Returns the alignment required for the data, in bytes. The offset is guaranteed to be a multiple of this. */ virtual unsigned get_alignment() const { return 1; } @@ -59,11 +62,15 @@ protected: /** Called when the offset for the data has changed. */ virtual void offset_changed() { } +private: + bool resize_buffer() const; + +protected: /** Resizes the buffer if necessary and calls upload_data(). */ void update_buffer() const; /** Uploads data to the buffer. */ - virtual void upload_data() const = 0; + virtual void upload_data() const; }; } // namespace GL diff --git a/source/uniformblock.h b/source/uniformblock.h index 192e7c2e..877a255f 100644 --- a/source/uniformblock.h +++ b/source/uniformblock.h @@ -36,6 +36,7 @@ public: private: virtual unsigned get_data_size() const { return size; } + virtual const void *get_data_pointer() const { return &data[0]; } virtual unsigned get_alignment() const; virtual void offset_changed(); virtual void upload_data() const; diff --git a/source/vertexarray.cpp b/source/vertexarray.cpp index 72fd8a48..38481099 100644 --- a/source/vertexarray.cpp +++ b/source/vertexarray.cpp @@ -103,11 +103,6 @@ unsigned VertexArray::get_data_size() const return data.size()*sizeof(float); } -void VertexArray::upload_data() const -{ - get_buffer()->sub_data(get_offset(), get_data_size(), &data[0]); -} - void VertexArray::apply() const { if(format.empty()) diff --git a/source/vertexarray.h b/source/vertexarray.h index e38f0aff..0223a1f4 100644 --- a/source/vertexarray.h +++ b/source/vertexarray.h @@ -61,7 +61,7 @@ public: float *modify(unsigned); private: virtual unsigned get_data_size() const; - virtual void upload_data() const; + virtual const void *get_data_pointer() const { return &data[0]; } public: unsigned size() const { return data.size()/stride; }