]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/transferqueue.h
419760bdf4d6b709bd4b28a393428f8da45f9383
[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         using TransferCallback = void(VkCommandBuffer, VkBuffer, std::size_t);
32
33         struct PendingTransfer
34         {
35                 unsigned buffer_index = 0;
36                 std::size_t offset = 0;
37                 std::size_t size = 0;
38                 std::function<TransferCallback> callback;
39         };
40
41         Device &device;
42         std::size_t default_buffer_size = 16*1048576;
43         std::vector<StagingBuffer> buffers;
44         std::vector<PendingTransfer> transfers;
45
46 public:
47         TransferQueue(Device &);
48
49         template<typename T>
50         void *prepare_transfer(std::size_t, T &&);
51
52 private:
53         PendingTransfer &prepare_transfer(std::size_t);
54
55 public:
56         void dispatch_transfers(VkCommandBuffer);
57 };
58
59 template<typename T>
60 void *TransferQueue::prepare_transfer(std::size_t size, T &&callback)
61 {
62         PendingTransfer &pt = prepare_transfer(size);
63         pt.callback = std::forward<T>(callback);
64         return static_cast<char *>(buffers[pt.buffer_index].mapped_address)+pt.offset;
65 }
66
67 } // namespace GL
68 } // namespace Msp
69
70 #endif