]> git.tdb.fi Git - libs/gl.git/blob - source/backends/vulkan/buffer_backend.cpp
Refactor Vulkan memory mapping functions
[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.get_total_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         Buffer::AsyncTransfer transfer(*static_cast<Buffer *>(this), off, sz);
56         const char *src = static_cast<const char *>(d);
57         copy(src, src+sz, static_cast<char *>(transfer.get_address()));
58 }
59
60 unsigned VulkanBuffer::get_multiplicity() const
61 {
62         BufferUsage usage = static_cast<const Buffer *>(this)->usage;
63         return (usage==STREAMING ? device.get_n_frames_in_flight() : 1);
64 }
65
66 bool VulkanBuffer::can_map() const
67 {
68         return static_cast<const Buffer *>(this)->usage==STREAMING;
69 }
70
71 void *VulkanBuffer::map()
72 {
73         mapped_address = device.get_allocator().map(memory_id);
74         return mapped_address;
75 }
76
77 bool VulkanBuffer::unmap()
78 {
79         device.get_allocator().unmap(memory_id);
80         mapped_address = 0;
81         return true;
82 }
83
84 void VulkanBuffer::set_debug_name(const string &name)
85 {
86 #ifdef DEBUG
87         debug_name = name;
88         if(handle)
89                 set_vulkan_object_name();
90 #else
91         (void)name;
92 #endif
93 }
94
95 void VulkanBuffer::set_vulkan_object_name() const
96 {
97 #ifdef DEBUG
98         const VulkanFunctions &vk = device.get_functions();
99
100         VkDebugUtilsObjectNameInfoEXT name_info = { };
101         name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
102         name_info.objectType = VK_OBJECT_TYPE_BUFFER;
103         name_info.objectHandle = reinterpret_cast<uint64_t>(handle);
104         name_info.pObjectName = debug_name.c_str();
105         vk.SetDebugUtilsObjectName(name_info);
106 #endif
107 }
108
109
110 void Buffer::AsyncTransfer::allocate()
111 {
112         if(buffer->can_map())
113                 dest_addr = static_cast<char *>(buffer->map())+offset;
114         else
115         {
116                 Buffer &buf = *buffer;
117                 size_t off = offset;
118                 size_t sz = size;
119
120                 dest_addr = buf.device.get_transfer_queue().prepare_transfer(buffer, false, size,
121                         [&buf, off, sz](){
122                                 buf.device.get_synchronizer().write_buffer(buf.handle, off, sz);
123                         },
124                         [&buf, off, sz](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
125                                 const VulkanFunctions &vk = buf.device.get_functions();
126
127                                 VkBufferCopy region = { };
128                                 region.srcOffset = src_off;
129                                 region.dstOffset = off;
130                                 region.size = sz;
131                                 vk.CmdCopyBuffer(cmd_buf, staging_buf, buf.handle, 1, &region);
132                         });
133         }
134 }
135
136 void Buffer::AsyncTransfer::finalize()
137 {
138         if(buffer->can_map())
139         {
140                 buffer->unmap();
141                 buffer->device.get_synchronizer().write_buffer(buffer->handle, offset, size, true);
142         }
143         else
144                 buffer->device.get_transfer_queue().finalize_transfer(dest_addr);
145 }
146
147 } // namespace GL
148 } // namespace Msp