Pyrogenesis  trunk
Vulkan Memory Allocator

Easy to integrate Vulkan memory allocation library.

Documentation: Browse online: Vulkan Memory Allocator (generated from Doxygen-style comments in include/vk_mem_alloc.h)

License: MIT. See LICENSE.txt

Changelog: See CHANGELOG.md

Product page: Vulkan Memory Allocator on GPUOpen

Build status:

Problem

Memory allocation and resource (buffer and image) creation in Vulkan is difficult (comparing to older graphics APIs, like D3D11 or OpenGL) for several reasons:

Features

This library can help game developers to manage memory allocations and resource creation by offering some higher-level functions:

  1. Functions that help to choose correct and optimal memory type based on intended usage of the memory.
    • Required or preferred traits of the memory are expressed using higher-level description comparing to Vulkan flags.
  2. Functions that allocate memory blocks, reserve and return parts of them (VkDeviceMemory + offset + size) to the user.
    • Library keeps track of allocated memory blocks, used and unused ranges inside them, finds best matching unused ranges for new allocations, respects all the rules of alignment and buffer/image granularity.
  3. Functions that can create an image/buffer, allocate memory for it and bind them together - all in one call.

Additional features:

Prerequisites

Example

Basic usage of this library is very simple. Advanced features are optional. After you created global VmaAllocator object, a complete code needed to create a buffer may look like this:

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 = {};
VkBuffer buffer;
VmaAllocation allocation;
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);

With this one function call:

  1. VkBuffer is created.
  2. VkDeviceMemory block is allocated if needed.
  3. An unused region of the memory block is bound to this buffer.

VmaAllocation is an object that represents memory assigned to this buffer. It can be queried for parameters like VkDeviceMemory handle and offset.

How to build

On Windows it is recommended to use CMake UI. Alternatively you can generate a Visual Studio project map using CMake in command line: cmake -B./build/ -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 16 2019" -A x64 ./

On Linux:

mkdir build
cd build
cmake ..
make

The following targets are available

Target Description CMake option Default setting
VmaSample VMA sample application VMA_BUILD_SAMPLE OFF
VmaBuildSampleShaders Shaders for VmaSample VMA_BUILD_SAMPLE_SHADERS OFF

Please note that while VulkanMemoryAllocator library is supported on other platforms besides Windows, VmaSample is not.

These CMake options are available

CMake option Description Default setting
VMA_RECORDING_ENABLED Enable VMA memory recording for debugging OFF
VMA_USE_STL_CONTAINERS Use C++ STL containers instead of VMA's containers OFF
VMA_STATIC_VULKAN_FUNCTIONS Link statically with Vulkan API OFF
VMA_DYNAMIC_VULKAN_FUNCTIONS Fetch pointers to Vulkan functions internally (no static linking) ON
VMA_DEBUG_ALWAYS_DEDICATED_MEMORY Every allocation will have its own memory block OFF
VMA_DEBUG_INITIALIZE_ALLOCATIONS Automatically fill new allocations and destroyed allocations with some bit pattern OFF
VMA_DEBUG_GLOBAL_MUTEX Enable single mutex protecting all entry calls to the library OFF
VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT Never exceed VkPhysicalDeviceLimits::maxMemoryAllocationCount and return error OFF

Binaries

The release comes with precompiled binary executable for "VulkanSample" application which contains test suite. It is compiled using Visual Studio 2019, so it requires appropriate libraries to work, including "MSVCP140.dll", "VCRUNTIME140.dll", "VCRUNTIME140_1.dll". If the launch fails with error message telling about those files missing, please download and install Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019, "x64" version.

Read more

See Documentation.

Software using this library

Many other projects on GitHub and some game development studios that use Vulkan in their games.

See also