]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/transferqueue.h
Refactor TransferQueue to require explicit finalization of transfers
[libs/gl.git] / source / backends / vulkan / transferqueue.h
1 #ifndef MSP_GL_VULKAN_TRANSFERQUEUE_H_
2 #define MSP_GL_VULKAN_TRANSFERQUEUE_H_
3
4 #include <functional>
5 #include <vector>
6 #include "handles.h"
7
8 namespace Msp {
9 namespace GL {
10
11 class Buffer;
12 class Device;
13
14 class TransferQueue
15 {
16 private:
17         struct StagingBuffer
18         {
19                 Device &device;
20                 VkBuffer buffer = 0;
21                 unsigned memory_id = 0;
22                 std::size_t size = 0;
23                 std::size_t used = 0;
24                 void *mapped_address = 0;
25
26                 StagingBuffer(Device &, std::size_t);
27                 StagingBuffer(StagingBuffer &&);
28                 ~StagingBuffer();
29         };
30
31         struct PendingTransfer
32         {
33                 const void *object = 0;
34                 unsigned order = 0;
35                 int buffer_index = -1;
36                 std::size_t offset = 0;
37                 std::size_t size = 0;
38                 void *staging_address = 0;
39                 std::function<void()> synchronize;
40                 std::function<void(VkCommandBuffer, VkBuffer, std::size_t)> transfer;
41         };
42
43         Device &device;
44         std::size_t default_buffer_size = 16*1048576;
45         std::vector<StagingBuffer> buffers;
46         std::vector<PendingTransfer> transfers;
47         std::vector<PendingTransfer> async_transfers;
48         std::map<const void *, unsigned> next_orders;
49
50 public:
51         TransferQueue(Device &);
52
53         template<typename S, typename T>
54         void *prepare_transfer(const void *, bool, std::size_t, S &&, T &&);
55
56         template<typename S, typename T>
57         void prepare_transfer(const void *o, bool r, S &&s, T &&t)
58         { prepare_transfer(o, r, 0, std::forward<S>(s), std::forward<T>(t)); }
59
60         void finalize_transfer(void *);
61
62 private:
63         void allocate_staging(PendingTransfer &, std::size_t);
64         PendingTransfer &prepare_transfer(const void *, bool, std::size_t);
65         PendingTransfer &insert_transfer(PendingTransfer &&);
66
67 public:
68         void dispatch_transfers(VkCommandBuffer);
69 };
70
71 template<typename S, typename T>
72 void *TransferQueue::prepare_transfer(const void *object, bool ordered, std::size_t size, S &&synchronize, T &&transfer)
73 {
74         PendingTransfer &pt = prepare_transfer(object, ordered, size);
75         pt.synchronize = std::forward<S>(synchronize);
76         pt.transfer = std::forward<T>(transfer);
77         return pt.staging_address;
78 }
79
80 } // namespace GL
81 } // namespace Msp
82
83 #endif