2 #include "buffer_backend.h"
11 VulkanBuffer::VulkanBuffer():
12 device(Device::get_current())
15 VulkanBuffer::VulkanBuffer(VulkanBuffer &&other):
18 memory_id(other.memory_id),
19 mapped_address(other.mapped_address),
20 debug_name(move(other.debug_name))
24 other.mapped_address = 0;
27 VulkanBuffer::~VulkanBuffer()
30 device.get_destroy_queue().destroy(handle, memory_id);
33 void VulkanBuffer::allocate()
35 const Buffer &self = *static_cast<const Buffer *>(this);
36 const VulkanFunctions &vk = device.get_functions();
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;
45 vk.CreateBuffer(buffer_info, handle);
47 memory_id = device.get_allocator().allocate(handle, (self.usage==STREAMING ? STREAMING_MEMORY : DEVICE_MEMORY));
49 if(!debug_name.empty())
50 set_vulkan_object_name();
53 void VulkanBuffer::sub_data(size_t off, size_t sz, const void *d)
55 TransferQueue &tq = device.get_transfer_queue();
56 void *staging = tq.prepare_transfer(this, false, sz,
58 device.get_synchronizer().write_buffer(handle, off, sz);
60 [this, off, sz](VkCommandBuffer cmd_buf, VkBuffer staging_buf, size_t src_off){
61 const VulkanFunctions &vk = device.get_functions();
63 VkBufferCopy region = { };
64 region.srcOffset = src_off;
65 region.dstOffset = off;
67 vk.CmdCopyBuffer(cmd_buf, staging_buf, handle, 1, ®ion);
70 const char *src = static_cast<const char *>(d);
71 copy(src, src+sz, static_cast<char *>(staging));
72 tq.finalize_transfer(staging);
75 unsigned VulkanBuffer::get_multiplicity() const
77 BufferUsage usage = static_cast<const Buffer *>(this)->usage;
78 return (usage==STREAMING ? device.get_n_frames_in_flight() : 1);
81 bool VulkanBuffer::can_map() const
83 return static_cast<const Buffer *>(this)->usage==STREAMING;
86 void *VulkanBuffer::map()
88 size_t size = static_cast<const Buffer *>(this)->size;
89 mapped_address = device.get_allocator().map(memory_id, 0, size);
90 return mapped_address;
93 bool VulkanBuffer::unmap()
95 device.get_allocator().unmap(mapped_address);
100 void VulkanBuffer::set_debug_name(const string &name)
105 set_vulkan_object_name();
111 void VulkanBuffer::set_vulkan_object_name() const
114 const VulkanFunctions &vk = device.get_functions();
116 VkDebugUtilsObjectNameInfoEXT name_info = { };
117 name_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT;
118 name_info.objectType = VK_OBJECT_TYPE_BUFFER;
119 name_info.objectHandle = reinterpret_cast<uint64_t>(handle);
120 name_info.pObjectName = debug_name.c_str();
121 vk.SetDebugUtilsObjectName(name_info);