X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fbufferable.cpp;h=3178e0adefa38f72e2d8aa22ecbae6769c50cdc7;hp=04793fb793905ebfae448899195873879d6ff542;hb=08e19bc2b4eba572bc7699378cf55cd8772ac67e;hpb=98cc25ffe956bc162c053c96df659ba40dfe2d6e diff --git a/source/bufferable.cpp b/source/bufferable.cpp index 04793fb7..3178e0ad 100644 --- a/source/bufferable.cpp +++ b/source/bufferable.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include "bindable.h" #include "buffer.h" #include "bufferable.h" @@ -40,9 +42,15 @@ void Bufferable::use_buffer(Buffer *buf, Bufferable *prev) } } + dirty = true; update_offset(); } +Bufferable::AsyncUpdater *Bufferable::refresh_async() const +{ + return dirty ? new AsyncUpdater(*this) : 0; +} + void Bufferable::unlink_from_buffer() { if(prev_in_buffer) @@ -101,26 +109,64 @@ bool Bufferable::resize_buffer() const void Bufferable::update_buffer() const { - BindRestore bind(buffer, buffer->get_type()); + Conditional _bind(!ARB_direct_state_access, 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(); + 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(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_resized = bufferable.resize_buffer(); + mapped_address = reinterpret_cast(bufferable.buffer->map(WRITE_ONLY)); +} + +Bufferable::AsyncUpdater::~AsyncUpdater() +{ + bufferable.buffer->unmap(); +} + +void Bufferable::AsyncUpdater::upload_data() { - buffer->sub_data(offset, get_data_size(), get_data_pointer()); + 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