]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/transferqueue.h
Align staging memory with an alignment suitable for image 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                 unsigned async_count = 0;
26                 unsigned last_frame = 0;
27
28                 StagingBuffer(Device &, std::size_t);
29                 StagingBuffer(StagingBuffer &&);
30                 ~StagingBuffer();
31         };
32
33         struct PendingTransfer
34         {
35                 const void *object = 0;
36                 unsigned order = 0;
37                 int buffer_index = -1;
38                 std::size_t offset = 0;
39                 std::size_t size = 0;
40                 void *staging_address = 0;
41                 std::function<void()> synchronize;
42                 std::function<void(VkCommandBuffer, VkBuffer, std::size_t)> transfer;
43         };
44
45         Device &device;
46         std::size_t default_buffer_size = 16*1048576;
47         std::vector<StagingBuffer> buffers;
48         std::vector<PendingTransfer> transfers;
49         std::vector<PendingTransfer> async_transfers;
50         std::map<const void *, unsigned> next_orders;
51         unsigned current_frame = 0;
52
53 public:
54         TransferQueue(Device &);
55
56         template<typename S, typename T>
57         void *prepare_transfer(const void *, bool, std::size_t, S &&, T &&);
58
59         template<typename S, typename T>
60         void prepare_transfer(const void *o, bool r, S &&s, T &&t)
61         { prepare_transfer(o, r, 0, std::forward<S>(s), std::forward<T>(t)); }
62
63         void finalize_transfer(void *);
64
65 private:
66         void allocate_staging(PendingTransfer &, std::size_t);
67         PendingTransfer &prepare_transfer(const void *, bool, std::size_t);
68         PendingTransfer &insert_transfer(PendingTransfer &&);
69
70 public:
71         void dispatch_transfers(VkCommandBuffer);
72 };
73
74 template<typename S, typename T>
75 void *TransferQueue::prepare_transfer(const void *object, bool ordered, std::size_t size, S &&synchronize, T &&transfer)
76 {
77         PendingTransfer &pt = prepare_transfer(object, ordered, size);
78         pt.synchronize = std::forward<S>(synchronize);
79         pt.transfer = std::forward<T>(transfer);
80         return pt.staging_address;
81 }
82
83 } // namespace GL
84 } // namespace Msp
85
86 #endif