]> git.tdb.fi Git - libs/gl.git/blobdiff - source/bufferable.cpp
Add an asynchronous update interface to Bufferable
[libs/gl.git] / source / bufferable.cpp
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