Vulkan Memory Allocator (VMA)

repository·master·Indexed 25 days ago

https://github.com/gpuopen-librariesandsdks/vulkanmemoryallocator

A high-performance C++ header-only library for managing memory allocation and resource creation (buffers and images) in Vulkan. It abstracts manual memory management, providing optimal memory selection, block management, thread safety, and advanced features like defragmentation and resource aliasing. Includes GpuMemDumpVis, a Python tool for visualizing allocator internal state from JSON dumps.

Tokens
1.5K
Snippets
5
Records
9
Agent score
36%

What's inside Vulkan Memory Allocator

  1. Overview of Vulkan Memory Allocator features

    master

    VMA simplifies Vulkan memory management by providing:

    • Optimal Memory Selection: Functions to choose memory types based on usage traits.
    • Block Management: Automatic tracking of used/unused ranges, alignment, and granularity.
    • Unified Creation: Functions to create, allocate, and bind buffers/images in one call.
    • Thread Safety: Internal synchronization for multi-threaded access to memory blocks.
    • Advanced Features: Support for defragmentation, custom memory pools, linear allocators, sparse binding, and resource aliasing.
    • Debugging Tools: JSON state dumps, debug annotations (pName, pUserData), and memory initialization patterns to detect uninitialized usage.
  2. Build Vulkan Memory Allocator from source

    master

    VMA is a header-only library (single header file), so you can skip the installation step after building if you only need the headers.

    On Windows: Use CMake GUI or the command line to generate a Visual Studio solution.

    On Linux: Generate the build files and install to a local prefix.

    # Windows
    cmake -S .  -B build -D VMA_BUILD_SAMPLES=ON
    cmake --open build
    
    # Linux
    cmake -S . -B build
    cmake --install build --prefix build/install
  3. Use GpuMemDumpVis to visualize memory dumps

    master

    GpuMemDumpVis is a Python script used to visualize the internal state of Vulkan Memory Allocator (VMA) or D3D12 Memory Allocator (D3D12MA) libraries.

    To generate a visualization, you must provide a JSON dump file (generated via vmaBuildStatsString() or D3D12MA::Allocator::BuildStatsString()) and specify an output image path. The output format (e.g., PNG, JPEG, BMP) is determined by the file extension provided.

    python GpuMemDumpVis.py -o OUTPUT_FILE INPUT_FILE
  4. Integrate Vulkan Memory Allocator with CMake

    master

    To use VMA in a CMake-based project, use find_package or add_subdirectory. Linking to the target GPUOpen::VulkanMemoryAllocator will automatically configure the necessary include directories.

    find_package(VulkanMemoryAllocator CONFIG REQUIRED)
    target_link_libraries(YourGameEngine PRIVATE GPUOpen::VulkanMemoryAllocator)
  5. Create a Vulkan Buffer with vmaCreateBuffer

    master

    The simplest way to create a buffer and its associated memory is using vmaCreateBuffer. This single call creates the VkBuffer, allocates the required VkDeviceMemory, and binds them together.

    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_AUTO;
    
    VkBuffer buffer;
    VmaAllocation allocation;
    vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr);
  6. Interpret GpuMemDumpVis visualization legend

    master

    The generated image uses specific colors and patterns to represent different memory allocation types and usages:

    VisualDescription
    Light gray (no border)Free space: Unused space in a Vulkan device memory block
    Buffer 1Buffer: Usage containing INDIRECT_BUFFER, VERTEX_BUFFER, or INDEX_BUFFER (Vulkan)
    Buffer 2Buffer: Usage containing STORAGE_BUFFER or STORAGE_TEXEL_BUFFER (Vulkan)
    Buffer 3Buffer: Usage containing UNIFORM_BUFFER or UNIFORM_TEXEL_BUFFER (Vulkan)
    Buffer 4Buffer: Other buffer types
    Image 1Image (OPTIMAL): Usage containing DEPTH_STENCIL_ATTACHMENT (Vulkan) or D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL (D3D12)
    Image 2Image (OPTIMAL): Usage containing INPUT_ATTACHMENT, TRANSIENT_ATTACHMENT, COLOR_ATTACHMENT, or STORAGE (Vulkan), or D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET / D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS (D3D12)
    Image 3Image (OPTIMAL): Usage containing SAMPLED (Vulkan) or a texture not containing D3D12_RESOURCE_FLAG_DENY_SHARED_RESOURCE (D3D12)
    Image 4Image (OPTIMAL): Other optimal tiling images (Vulkan) or textures (D3D12)
    Image LinearImage (LINEAR): Image with LINEAR tiling (Vulkan)
    Image UnknownImage (Unknown): Tiling unknown to the allocator (Vulkan)
    UnknownAllocation: Unknown allocation type
    Black barDetails: Allocations too small to be visualized as filled rectangles

    Warning: The current version of this tool may show incorrect results when allocations alias (overlap) in memory, as it only considers allocation sizes rather than offsets.