Instantiating the VMA library
To use VMA, you first need to create an instance of the library and store a handle in a variable of type VmaAllocator
. To create one, you need a Vulkan physical device and a device.
How to do it…
Creating a VMA library instance requires instancing two different structures. One stores pointers to API functions that VMA needs to find other function pointers and another structure that provides a physical device, a device, and an instance for creating an allocator:
VkPhysicalDevice physicalDevice; // Valid Physical Device VkDevice device; // Valid Device VkInstance instance; // Valid Instance const uint32_t apiVersion = VK_API_VERSION_1_3; const VmaVulkanFunctions vulkanFunctions = { .vkGetInstanceProcAddr = vkGetInstanceProcAddr, .vkGetDeviceProcAddr = vkGetDeviceProcAddr, #if VMA_VULKAN_VERSION >= 1003000 .vkGetDeviceBufferMemoryRequirements = vkGetDeviceBufferMemoryRequirements, .vkGetDeviceImageMemoryRequirements = vkGetDeviceImageMemoryRequirements, #endif }; VmaAllocator allocator = nullptr; const VmaAllocatorCreateInfo allocInfo = { .physicalDevice = physicalDevice, .device = device, .pVulkanFunctions = &vulkanFunctions, .instance = instance, .vulkanApiVersion = apiVersion, }; vmaCreateAllocator(&allocInfo, &allocator);
The allocator needs pointers to a few Vulkan functions so that it can work based on the features you would like to use. In the preceding case, we provide only the bare minimum for allocating and deallocating memory. The allocator needs to be freed once the context is destroyed with vmaDestroyAllocator
.