1 #include <msp/core/algorithm.h>
2 #include <msp/core/maputils.h>
4 #include "transferqueue.h"
12 TransferQueue::TransferQueue(Device &d):
16 void TransferQueue::allocate_staging(PendingTransfer &transfer, size_t size)
18 auto i = find_if(buffers, [size](const StagingBuffer &b){ return b.used+size<=b.size; });
21 buffers.emplace_back(device, max(default_buffer_size, size));
22 i = prev(buffers.end());
25 transfer.buffer_index = distance(buffers.begin(), i);
26 transfer.offset = i->used;
28 transfer.staging_address = static_cast<char *>(i->mapped_address)+transfer.offset;
34 TransferQueue::PendingTransfer &TransferQueue::prepare_transfer(const void *object, bool ordered, size_t size)
36 PendingTransfer transfer;
37 transfer.object = object;
38 transfer.order = ordered;
42 allocate_staging(transfer, size);
43 auto i = lower_bound_member(async_transfers, transfer.staging_address, &PendingTransfer::staging_address);
44 i = async_transfers.emplace(i, move(transfer));
48 return insert_transfer(move(transfer));
51 void TransferQueue::finalize_transfer(void *staging)
53 auto i = lower_bound_member(async_transfers, staging, &PendingTransfer::staging_address);
54 if(i==async_transfers.end() || i->staging_address!=staging)
55 throw key_error(staging);
57 if(i->buffer_index>=0)
58 --buffers[i->buffer_index].async_count;
59 insert_transfer(move(*i));
60 async_transfers.erase(i);
63 TransferQueue::PendingTransfer &TransferQueue::insert_transfer(PendingTransfer &&pt)
65 bool ordered = pt.order;
67 unsigned &order = next_orders[pt.object];
69 order += (order&1)|ordered;
71 auto j = upper_bound_member(transfers, order, &PendingTransfer::order);
72 j = transfers.emplace(j, move(pt));
80 void TransferQueue::dispatch_transfers(VkCommandBuffer command_buffer)
86 for(auto i=transfers.begin(); i!=transfers.end(); )
89 for(; (j!=transfers.end() && j->order==i->order); ++j)
92 device.get_synchronizer().barrier(command_buffer);
96 VkBuffer buffer = (i->buffer_index>=0 ? buffers[i->buffer_index].buffer : 0);
97 i->transfer(command_buffer, buffer, i->offset);
98 if(i->buffer_index>=0)
99 buffers[i->buffer_index].last_frame = current_frame;
106 unsigned n_frames_in_flight = device.get_n_frames_in_flight();
107 for(StagingBuffer &b: buffers)
108 if(!b.async_count && b.last_frame+n_frames_in_flight<current_frame)
113 TransferQueue::StagingBuffer::StagingBuffer(Device &d, size_t s):
117 const VulkanFunctions &vk = device.get_functions();
119 VkBufferCreateInfo buffer_info = { };
120 buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
121 buffer_info.size = size;
122 buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
123 buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
125 vk.CreateBuffer(buffer_info, buffer);
127 MemoryAllocator &allocator = device.get_allocator();
128 memory_id = allocator.allocate(buffer, STAGING_MEMORY);
129 mapped_address = allocator.map(memory_id, 0, size);
132 TransferQueue::StagingBuffer::StagingBuffer(StagingBuffer &&other):
133 device(other.device),
134 buffer(other.buffer),
135 memory_id(other.memory_id),
138 mapped_address(other.mapped_address)
142 other.mapped_address = 0;
145 TransferQueue::StagingBuffer::~StagingBuffer()
147 const VulkanFunctions &vk = device.get_functions();
148 MemoryAllocator &allocator = device.get_allocator();
152 allocator.unmap(mapped_address);
153 allocator.release(memory_id);
156 vk.DestroyBuffer(buffer);