]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/buffer_backend.cpp
Refactor TransferQueue to require explicit finalization of transfers
[libs/gl.git] / source / backends / vulkan / buffer_backend.cpp
1 #include "buffer.h"
2 #include "buffer_backend.h"
3 #include "device.h"
4 #include "vulkan.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 VulkanBuffer::VulkanBuffer():
12         device(Device::get_current())
13 { }
14
15 VulkanBuffer::VulkanBuffer(VulkanBuffer &&other):
16         device(other.device),
17         handle(other.handle),
18         memory_id(other.memory_id),
19         mapped_address(other.mapped_address),
20         debug_name(move(other.debug_name))
21 {
22         other.handle = 0;
23         other.memory_id = 0;
24         other.mapped_address = 0;
25 }
26
27 VulkanBuffer::~VulkanBuffer()
28 {
29         if(handle)
30                 device.get_destroy_queue().destroy(handle, memory_id);
31 }
32
33 void VulkanBuffer::allocate()
34 {
35         const Buffer &self = *static_cast<const Buffer *>(this);
36         const VulkanFunctions &vk = device.get_functions();
37
38         VkBufferCreateInfo buffer_info = { };
39         buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
40         buffer_info.size = self.size;
41         buffer_info.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT|VK_BUFFER_USAGE_TRANSFER_DST_BIT|VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT|
42                 VK_BUFFER_USAGE_INDEX_BUFFER_BIT|VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
43         buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
44
45         vk.CreateBuffer(buffer_info, handle);
46
47         memory_id = device.get_allocator().allocate(handle, (self.usage==STREAMING ? STREAMING_MEMORY : DEVICE_MEMORY));
48
49         if(!debug_name.empty())
50                 set_vulkan_object_name();
51 }
52
53 void VulkanBuffer::sub_data(size_t off, size_t sz, const void *d)
54 {
55         TransferQueue &tq = device.get_transfer_queue();
56         void *staging = tq.prepare_transfer(this, false, sz,
57                 [this, off, sz](){
58                         device.get_synchronizer().write_buffer(handle, off, sz);
59                 },
60                 [this, off, sz](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
61                         const VulkanFunctions &vk = device.get_functions();
62
63                         VkBufferCopy region = { };
64                         region.srcOffset = src_off;
65                         region.dstOffset = off;
66                         region.size = sz;
67                         vk.CmdCopyBuffer(cmd_buf, staging_buf, handle, 1, &region);
68                 });
69
70         const char *src = static_cast<const char *>(d);
71         copy(src, src+sz, static_cast<char *>(staging));
72         tq.finalize_transfer(staging);
73 }
74
75 bool VulkanBuffer::can_map() const
76 {
77         return static_cast<const Buffer *>(this)->usage==STREAMING;
78 }
79
80 void *VulkanBuffer::map()
81 {
82         size_t size = static_cast<const Buffer *>(this)->size;
83         mapped_address = device.get_allocator().map(memory_id, 0, size);
84         return mapped_address;
85 }
86
87 bool VulkanBuffer::unmap()
88 {
89         device.get_allocator().unmap(mapped_address);
90         mapped_address = 0;
91         return true;
92 }
93
94 void VulkanBuffer::set_debug_name(const string &name)
95 {
96 #ifdef DEBUG
97         debug_name = name;
98         if(handle)
99                 set_vulkan_object_name();
100 #else
101         (void)name;
102 #endif
103 }
104
105 void VulkanBuffer::set_vulkan_object_name() const
106 {
107 #ifdef DEBUG
108         const VulkanFunctions &vk = device.get_functions();
109
110         VkDebugUtilsObjectNameInfoEXT name_info = { };
111         name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
112         name_info.objectType = VK_OBJECT_TYPE_BUFFER;
113         name_info.objectHandle = reinterpret_cast<uint64_t>(handle);
114         name_info.pObjectName = debug_name.c_str();
115         vk.SetDebugUtilsObjectName(name_info);
116 #endif
117 }
118
119 } // namespace GL
120 } // namespace Msp