From b294ef47df0ae87c42fe8114987817fb28326f10 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 27 Dec 2021 23:26:25 +0200 Subject: [PATCH] Make Buffer::AsyncTransfer default-constructible and move-assignable --- source/backends/opengl/buffer_backend.cpp | 4 ++-- source/backends/vulkan/buffer_backend.cpp | 16 ++++++++-------- source/core/buffer.cpp | 17 ++++++++++++++++- source/core/buffer.h | 4 +++- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/source/backends/opengl/buffer_backend.cpp b/source/backends/opengl/buffer_backend.cpp index c5954083..dc081b1c 100644 --- a/source/backends/opengl/buffer_backend.cpp +++ b/source/backends/opengl/buffer_backend.cpp @@ -135,12 +135,12 @@ void OpenGLBuffer::unbind_scratch() void Buffer::AsyncTransfer::allocate() { - dest_addr = buffer.map(); + dest_addr = buffer->map(); } void Buffer::AsyncTransfer::finalize() { - buffer.unmap(); + buffer->unmap(); } } // namespace GL diff --git a/source/backends/vulkan/buffer_backend.cpp b/source/backends/vulkan/buffer_backend.cpp index aaf41570..750c926b 100644 --- a/source/backends/vulkan/buffer_backend.cpp +++ b/source/backends/vulkan/buffer_backend.cpp @@ -110,15 +110,15 @@ void VulkanBuffer::set_vulkan_object_name() const void Buffer::AsyncTransfer::allocate() { - if(buffer.can_map()) - dest_addr = static_cast(buffer.map())+offset; + if(buffer->can_map()) + dest_addr = static_cast(buffer->map())+offset; else { - Buffer &buf = buffer; + Buffer &buf = *buffer; size_t off = offset; size_t sz = size; - dest_addr = buffer.device.get_transfer_queue().prepare_transfer(&buffer, false, size, + dest_addr = buf.device.get_transfer_queue().prepare_transfer(buffer, false, size, [&buf, off, sz](){ buf.device.get_synchronizer().write_buffer(buf.handle, off, sz); }, @@ -136,13 +136,13 @@ void Buffer::AsyncTransfer::allocate() void Buffer::AsyncTransfer::finalize() { - if(buffer.can_map()) + if(buffer->can_map()) { - buffer.unmap(); - buffer.device.get_synchronizer().write_buffer(buffer.handle, offset, size, true); + buffer->unmap(); + buffer->device.get_synchronizer().write_buffer(buffer->handle, offset, size, true); } else - buffer.device.get_transfer_queue().finalize_transfer(dest_addr); + buffer->device.get_transfer_queue().finalize_transfer(dest_addr); } } // namespace GL diff --git a/source/core/buffer.cpp b/source/core/buffer.cpp index b6f2e44b..d5fc29d1 100644 --- a/source/core/buffer.cpp +++ b/source/core/buffer.cpp @@ -76,7 +76,7 @@ bool Buffer::unmap() Buffer::AsyncTransfer::AsyncTransfer(Buffer &b, size_t o, size_t s): - buffer(b), + buffer(&b), offset(o), size(s), dest_addr(0) @@ -93,6 +93,21 @@ Buffer::AsyncTransfer::AsyncTransfer(AsyncTransfer &&other): 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) diff --git a/source/core/buffer.h b/source/core/buffer.h index 66eb8d41..9b129361 100644 --- a/source/core/buffer.h +++ b/source/core/buffer.h @@ -49,14 +49,16 @@ public: friend class Buffer; private: - Buffer &buffer; + Buffer *buffer = 0; std::size_t offset = 0; std::size_t size = 0; void *dest_addr = 0; AsyncTransfer(Buffer &, std::size_t, std::size_t); public: + AsyncTransfer() = default; AsyncTransfer(AsyncTransfer &&); + AsyncTransfer &operator=(AsyncTransfer &&); ~AsyncTransfer(); private: -- 2.45.2