Vulkan Memory Allocator
|
In your project code:
#define VMA_IMPLEMENTATION #include "vk_mem_alloc.h"
At program startup:
VkPhysicalDevice
and VkDevice
object.VmaAllocator
object by calling vmaCreateAllocator().VmaAllocatorCreateInfo allocatorInfo = {}; allocatorInfo.physicalDevice = physicalDevice; allocatorInfo.device = device; VmaAllocator allocator; vmaCreateAllocator(&allocatorInfo, &allocator);
When you want to create a buffer or image:
VkBufferCreateInfo
/ VkImageCreateInfo
structure.VkBuffer
/VkImage
with memory already allocated and bound to it.VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; bufferInfo.size = 65536; bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; VmaAllocationCreateInfo allocInfo = {}; allocInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; VkBuffer buffer; VmaAllocation allocation; vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);
Don't forget to destroy your objects when no longer needed:
vmaDestroyBuffer(allocator, buffer, allocation); vmaDestroyAllocator(allocator);