X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fbuffer.cpp;h=36ac440a54426c186c16ae238c51a337312445e8;hb=e1be82a4dfce8d90358c506f65be09da4dc9d5ec;hp=ed0c552360ac8fad2345236e2024d17455200334;hpb=6d2e2a0bb28496a8c25b441009bdd2a1a1e72d81;p=libs%2Fgl.git diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index ed0c5523..36ac440a 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -31,13 +31,23 @@ void Buffer::data(const void *d) } void Buffer::sub_data(size_t off, size_t sz, const void *d) +{ + check_sub_data(off, sz, "Buffer::sub_data"); + BufferBackend::sub_data(off, sz, d); +} + +Buffer::AsyncTransfer Buffer::sub_data_async(size_t off, size_t sz) +{ + check_sub_data(off, sz, "Buffer::sub_data_async"); + return AsyncTransfer(*this, off, sz); +} + +void Buffer::check_sub_data(size_t off, size_t sz, const char *func) { if(size==0) - throw invalid_operation("Buffer::sub_data"); + throw invalid_operation(func); if(off>get_total_size() || off%size+sz>size) - throw out_of_range("Buffer::sub_data"); - - BufferBackend::sub_data(off, sz, d); + throw out_of_range(func); } void Buffer::require_size(size_t req_sz) const @@ -48,7 +58,7 @@ void Buffer::require_size(size_t req_sz) const void *Buffer::map() { - if(!can_map() || mapped) + if(size==0 || !can_map() || mapped) throw invalid_operation("Buffer::map"); void *result = BufferBackend::map(); mapped = true; @@ -57,12 +67,52 @@ void *Buffer::map() bool Buffer::unmap() { - if(!can_map() || !mapped) - throw invalid_operation("Buffer::map"); + if(size==0 || !can_map() || !mapped) + throw invalid_operation("Buffer::unmap"); bool result = BufferBackend::unmap(); mapped = false; return result; } + +Buffer::AsyncTransfer::AsyncTransfer(Buffer &b, size_t o, size_t s): + buffer(&b), + offset(o), + size(s), + dest_addr(0) +{ + allocate(); +} + +Buffer::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other): + buffer(other.buffer), + offset(other.offset), + size(other.size), + dest_addr(other.dest_addr) +{ + other.dest_addr = 0; +} + +Buffer::AsyncTransfer &Buffer::AsyncTransfer::operator=(AsyncTransfer &&other) +{ + if(dest_addr) + finalize(); + + buffer = other.buffer; + offset = other.offset; + size = other.size; + dest_addr = other.dest_addr; + + other.dest_addr = 0; + + return *this; +} + +Buffer::AsyncTransfer::~AsyncTransfer() +{ + if(dest_addr) + finalize(); +} + } // namespace GL } // namespace Msp