]> git.tdb.fi Git - libs/gl.git/commitdiff
Reset staging buffers in TransferQueue once the transfers are done
authorMikko Rasa <tdb@tdb.fi>
Mon, 20 Dec 2021 21:03:33 +0000 (23:03 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 20 Dec 2021 21:09:49 +0000 (23:09 +0200)
source/backends/vulkan/transferqueue.cpp
source/backends/vulkan/transferqueue.h

index 8db81020425ee5e705b15bee3e098002483cf728..9fc941401ed90ef954b4a156ae7b1b801baf149f 100644 (file)
@@ -28,6 +28,7 @@ void TransferQueue::allocate_staging(PendingTransfer &transfer, size_t size)
        transfer.staging_address = static_cast<char *>(i->mapped_address)+transfer.offset;
 
        i->used += size;
+       ++i->async_count;
 }
 
 TransferQueue::PendingTransfer &TransferQueue::prepare_transfer(const void *object, bool ordered, size_t size)
@@ -53,6 +54,8 @@ void TransferQueue::finalize_transfer(void *staging)
        if(i==async_transfers.end() || i->staging_address!=staging)
                throw key_error(staging);
 
+       if(i->buffer_index>=0)
+               --buffers[i->buffer_index].async_count;
        insert_transfer(move(*i));
        async_transfers.erase(i);
 }
@@ -79,6 +82,7 @@ void TransferQueue::dispatch_transfers(VkCommandBuffer command_buffer)
        if(transfers.empty())
                return;
 
+       ++current_frame;
        for(auto i=transfers.begin(); i!=transfers.end(); )
        {
                auto j = i;
@@ -91,11 +95,18 @@ void TransferQueue::dispatch_transfers(VkCommandBuffer command_buffer)
                {
                        VkBuffer buffer = (i->buffer_index>=0 ? buffers[i->buffer_index].buffer : 0);
                        i->transfer(command_buffer, buffer, i->offset);
+                       if(i->buffer_index>=0)
+                               buffers[i->buffer_index].last_frame = current_frame;
                }
        }
 
        transfers.clear();
        next_orders.clear();
+
+       unsigned n_frames_in_flight = device.get_n_frames_in_flight();
+       for(StagingBuffer &b: buffers)
+               if(!b.async_count && b.last_frame+n_frames_in_flight<current_frame)
+                       b.used = 0;
 }
 
 
index 592bb69e1303458870842391c2d1ad41ba44049f..e78137fc4067701fc9a2168ffc6f5e673392e0cd 100644 (file)
@@ -22,6 +22,8 @@ private:
                std::size_t size = 0;
                std::size_t used = 0;
                void *mapped_address = 0;
+               unsigned async_count = 0;
+               unsigned last_frame = 0;
 
                StagingBuffer(Device &, std::size_t);
                StagingBuffer(StagingBuffer &&);
@@ -46,6 +48,7 @@ private:
        std::vector<PendingTransfer> transfers;
        std::vector<PendingTransfer> async_transfers;
        std::map<const void *, unsigned> next_orders;
+       unsigned current_frame = 0;
 
 public:
        TransferQueue(Device &);