5#ifdef VISUTWIN_HAS_VULKAN
12#include "spdlog/spdlog.h"
19 auto* vkDev =
static_cast<VulkanGraphicsDevice*
>(device);
20 _allocator = vkDev->vmaAllocator();
23 size_t bufferSize =
static_cast<size_t>(numIndices) * bytesPerIndex;
24 if (bufferSize == 0)
return;
26 VkBufferCreateInfo bufferInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
27 bufferInfo.size = bufferSize;
28 bufferInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
30 VmaAllocationCreateInfo allocInfo{};
31 allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
33 vmaCreateBuffer(_allocator, &bufferInfo, &allocInfo, &_buffer, &_allocation,
nullptr);
36 VulkanIndexBuffer::~VulkanIndexBuffer()
38 if (_allocator != VK_NULL_HANDLE && _buffer != VK_NULL_HANDLE) {
39 vmaDestroyBuffer(_allocator, _buffer, _allocation);
43 bool VulkanIndexBuffer::setData(
const std::vector<uint8_t>& data)
45 if (data.empty() || !_allocator || !_buffer)
return false;
47 uploadStaging(data.data(), data.size());
51 void VulkanIndexBuffer::uploadStaging(
const void* data,
size_t size)
53 auto* vkDev =
static_cast<VulkanGraphicsDevice*
>(_device);
55 VkBuffer stagingBuffer;
56 VmaAllocation stagingAlloc;
58 VkBufferCreateInfo stagingInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
59 stagingInfo.size = size;
60 stagingInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
62 VmaAllocationCreateInfo stagingAllocInfo{};
63 stagingAllocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
65 vmaCreateBuffer(_allocator, &stagingInfo, &stagingAllocInfo,
66 &stagingBuffer, &stagingAlloc,
nullptr);
69 vmaMapMemory(_allocator, stagingAlloc, &mapped);
70 memcpy(mapped, data, size);
71 vmaUnmapMemory(_allocator, stagingAlloc);
73 vulkanImmediateSubmit(vkDev, [&](VkCommandBuffer cmd) {
76 vkCmdCopyBuffer(cmd, stagingBuffer, _buffer, 1, ©);
79 vmaDestroyBuffer(_allocator, stagingBuffer, stagingAlloc);
Abstract GPU interface for resource creation, state management, and draw submission.