X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fbufferable.cpp;h=e022a71676eed0a52ee36ed9be087b3eac1e9999;hb=9be04243c92f024327e74ad8d48861581d83b7ed;hp=60df3eec92c1bd188dbab825e998baf1dd97e7b4;hpb=900cf40469972b1f32a6dbac95c42f46c3726fa8;p=libs%2Fgl.git diff --git a/source/bufferable.cpp b/source/bufferable.cpp index 60df3eec..e022a716 100644 --- a/source/bufferable.cpp +++ b/source/bufferable.cpp @@ -1,4 +1,7 @@ #include +#include +#include +#include "bindable.h" #include "buffer.h" #include "bufferable.h" @@ -12,6 +15,7 @@ Bufferable::Bufferable(): offset(0), next_in_buffer(0), prev_in_buffer(0), + location_dirty(false), dirty(false) { } @@ -39,9 +43,16 @@ void Bufferable::use_buffer(Buffer *buf, Bufferable *prev) } } + location_dirty = true; + 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) @@ -68,18 +79,20 @@ void Bufferable::update_offset() if(new_offset!=offset) { offset = new_offset; + location_dirty = true; dirty = true; - offset_changed(); } if(next_in_buffer) next_in_buffer->update_offset(); - - /* Do not resize the buffer here, as more bufferables may be added before - the buffer is actually used. */ + else if(buffer && offset+get_data_size()>buffer->get_size()) + { + location_dirty = true; + dirty = true; + } } -void Bufferable::update_buffer() const +bool Bufferable::resize_buffer() const { if(offset+get_data_size()>=buffer->get_size()) { @@ -91,17 +104,74 @@ 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; } } - upload_data(); + return false; +} + +void Bufferable::update_buffer_size() const +{ + if(resize_buffer()) + { + Conditional _bind(!ARB_direct_state_access, buffer, buffer->get_type()); + + /* 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(0); + for(const Bufferable *b=next_in_buffer; b; b=b->next_in_buffer) + if(!b->dirty) + b->upload_data(0); + } +} + +void Bufferable::upload_data(char *target) const +{ + unsigned data_size = get_data_size(); + if(location_dirty) + { + update_buffer_size(); + location_changed(buffer, offset, data_size); + location_dirty = false; + } + + if(target) + { + const char *source = reinterpret_cast(get_data_pointer()); + copy(source, source+data_size, target); + } + else + buffer->sub_data(offset, data_size, get_data_pointer()); dirty = false; } + +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() +{ + 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); + 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); +} + } // namespace GL } // namespace Msp