5#ifdef VISUTWIN_HAS_VULKAN
12#include "spdlog/spdlog.h"
17 const std::shared_ptr<VertexFormat>& format,
int numVertices,
21 auto* vkDev =
static_cast<VulkanGraphicsDevice*
>(device);
22 _allocator = vkDev->vmaAllocator();
24 size_t bufferSize = _storage.size();
25 if (bufferSize == 0)
return;
27 VkBufferCreateInfo bufferInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
28 bufferInfo.size = bufferSize;
29 bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
31 VmaAllocationCreateInfo allocInfo{};
32 allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
34 vmaCreateBuffer(_allocator, &bufferInfo, &allocInfo, &_buffer, &_allocation,
nullptr);
36 if (!options.data.empty()) {
41 VulkanVertexBuffer::~VulkanVertexBuffer()
43 if (_allocator != VK_NULL_HANDLE && _buffer != VK_NULL_HANDLE) {
44 vmaDestroyBuffer(_allocator, _buffer, _allocation);
48 void VulkanVertexBuffer::unlock()
50 if (_storage.empty() || !_allocator || !_buffer)
return;
52 auto* vkDev =
static_cast<VulkanGraphicsDevice*
>(_device);
53 size_t dataSize = _storage.size();
56 VkBuffer stagingBuffer;
57 VmaAllocation stagingAlloc;
59 VkBufferCreateInfo stagingInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
60 stagingInfo.size = dataSize;
61 stagingInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
63 VmaAllocationCreateInfo stagingAllocInfo{};
64 stagingAllocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
66 vmaCreateBuffer(_allocator, &stagingInfo, &stagingAllocInfo,
67 &stagingBuffer, &stagingAlloc,
nullptr);
70 vmaMapMemory(_allocator, stagingAlloc, &mapped);
71 memcpy(mapped, _storage.data(), dataSize);
72 vmaUnmapMemory(_allocator, stagingAlloc);
74 vulkanImmediateSubmit(vkDev, [&](VkCommandBuffer cmd) {
77 vkCmdCopyBuffer(cmd, stagingBuffer, _buffer, 1, ©);
80 vmaDestroyBuffer(_allocator, stagingBuffer, stagingAlloc);
Abstract GPU interface for resource creation, state management, and draw submission.