]> git.tdb.fi Git - libs/gl.git/commitdiff
Add an asynchronous update interface to Bufferable
authorMikko Rasa <tdb@tdb.fi>
Tue, 23 Sep 2014 20:23:14 +0000 (23:23 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 23 Sep 2014 20:23:14 +0000 (23:23 +0300)
source/bufferable.cpp
source/bufferable.h
source/uniformblock.cpp
source/uniformblock.h

index 04793fb793905ebfae448899195873879d6ff542..569ffb2f08a252cb1d153e591df420a1108dcf5a 100644 (file)
@@ -43,6 +43,11 @@ void Bufferable::use_buffer(Buffer *buf, Bufferable *prev)
        update_offset();
 }
 
+Bufferable::AsyncUpdater *Bufferable::refresh_async() const
+{
+       return dirty ? new AsyncUpdater(*this) : 0;
+}
+
 void Bufferable::unlink_from_buffer()
 {
        if(prev_in_buffer)
@@ -108,19 +113,56 @@ void Bufferable::update_buffer() const
                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();
+                               b->upload_data(0);
                for(const Bufferable *b=next_in_buffer; b; b=b->next_in_buffer)
                        if(!b->dirty)
-                               b->upload_data();
+                               b->upload_data(0);
        }
 
-       upload_data();
+       upload_data(0);
        dirty = false;
 }
 
-void Bufferable::upload_data() const
+void Bufferable::upload_data(char *target) const
+{
+       if(target)
+       {
+               const char *source = reinterpret_cast<const char *>(get_data_pointer());
+               copy(source, source+get_data_size(), target);
+       }
+       else
+               buffer->sub_data(offset, get_data_size(), get_data_pointer());
+}
+
+
+Bufferable::AsyncUpdater::AsyncUpdater(const Bufferable &b):
+       bufferable(b)
 {
-       buffer->sub_data(offset, get_data_size(), get_data_pointer());
+       buffer_resized = bufferable.resize_buffer();
+       mapped_address = reinterpret_cast<char *>(bufferable.buffer->map(WRITE_ONLY));
+}
+
+Bufferable::AsyncUpdater::~AsyncUpdater()
+{
+       bufferable.buffer->unmap();
+}
+
+void Bufferable::AsyncUpdater::upload_data()
+{
+       bufferable.upload_data(mapped_address+bufferable.offset);
+       // Update all bufferables in the same buffer at once
+       for(const Bufferable *b=bufferable.prev_in_buffer; b; b=b->prev_in_buffer)
+               if(b->dirty || buffer_resized)
+               {
+                       b->upload_data(mapped_address+b->offset);
+                       b->dirty = false;
+               }
+       for(const Bufferable *b=bufferable.next_in_buffer; b; b=b->next_in_buffer)
+               if(b->dirty || buffer_resized)
+               {
+                       b->upload_data(mapped_address+b->offset);
+                       b->dirty = false;
+               }
 }
 
 } // namespace GL
index d7d7e45fe2183fa606395bda73f184a1f3ace282..f157c6281d2197d17b56bd78a7d61cd1754ee186 100644 (file)
@@ -14,6 +14,21 @@ uploading fresh data to the buffer.
 */
 class Bufferable
 {
+public:
+       class AsyncUpdater
+       {
+       private:
+               const Bufferable &bufferable;
+               char *mapped_address;
+               bool buffer_resized;
+
+       public:
+               AsyncUpdater(const Bufferable &);
+               ~AsyncUpdater();
+
+               void upload_data();
+       };
+
 private:
        Buffer *buffer;
        unsigned offset;
@@ -34,6 +49,8 @@ public:
        /** Uploads new data into the buffer if necessary. */
        void refresh() const { if(dirty) update_buffer(); }
 
+       AsyncUpdater *refresh_async() const;
+
 private:
        void unlink_from_buffer();
 
@@ -69,8 +86,9 @@ protected:
        /** Resizes the buffer if necessary and calls upload_data(). */
        void update_buffer() const;
 
-       /** Uploads data to the buffer. */
-       virtual void upload_data() const;
+       /** Uploads data to the buffer.  Receives pointer to mapped buffer memory as
+       parameter.  If null, buffer interface should be used instead. */
+       virtual void upload_data(char *) const;
 };
 
 } // namespace GL
index 498973f91de39dccf64512a00e0eceb1b3b2d63b..09060eb7622ce36d9ddf158339411d89e9445d86 100644 (file)
@@ -44,11 +44,15 @@ void UniformBlock::offset_changed()
        buf_range = 0;
 }
 
-void UniformBlock::upload_data() const
+void UniformBlock::upload_data(char *target) const
 {
        if(!buf_range)
                buf_range = new BufferRange(*get_buffer(), get_offset(), size);
-       buf_range->data(&data[0]);
+
+       if(target)
+               copy(data.begin(), data.end(), target);
+       else
+               buf_range->data(&data[0]);
 }
 
 void UniformBlock::attach(int index, const Uniform &uni)
index 877a255f5861bfbdf6c5e313253fb24588d16aa2..02e9086ef4ed75bdf118ca2fe41fd21ac4f74637 100644 (file)
@@ -39,7 +39,7 @@ private:
        virtual const void *get_data_pointer() const { return &data[0]; }
        virtual unsigned get_alignment() const;
        virtual void offset_changed();
-       virtual void upload_data() const;
+       virtual void upload_data(char *) const;
 
 public:
        void attach(int, const Uniform &);