Vulkan Memory Allocator
Classes | Typedefs | Enumerations | Functions
Layer 2 Allocating Memory

Classes

struct  VmaPoolCreateInfo
 Describes parameter of created VmaPool. More...
 
struct  VmaPoolStats
 Describes parameter of existing VmaPool. More...
 
struct  VmaAllocationInfo
 Parameters of VmaAllocation objects, that can be retrieved using function vmaGetAllocationInfo(). More...
 
struct  VmaDefragmentationInfo
 Optional configuration parameters to be passed to function vmaDefragment(). More...
 
struct  VmaDefragmentationStats
 Statistics returned by function vmaDefragment(). More...
 

Typedefs

typedef enum VmaPoolCreateFlagBits VmaPoolCreateFlagBits
 Flags to be passed as VmaPoolCreateInfo::flags. More...
 
typedef VkFlags VmaPoolCreateFlags
 
typedef struct VmaPoolCreateInfo VmaPoolCreateInfo
 Describes parameter of created VmaPool. More...
 
typedef struct VmaPoolStats VmaPoolStats
 Describes parameter of existing VmaPool. More...
 
typedef struct VmaAllocationInfo VmaAllocationInfo
 Parameters of VmaAllocation objects, that can be retrieved using function vmaGetAllocationInfo(). More...
 
typedef struct VmaDefragmentationInfo VmaDefragmentationInfo
 Optional configuration parameters to be passed to function vmaDefragment(). More...
 
typedef struct VmaDefragmentationStats VmaDefragmentationStats
 Statistics returned by function vmaDefragment(). More...
 

Enumerations

enum  VmaPoolCreateFlagBits { VMA_POOL_CREATE_PERSISTENT_MAP_BIT = 0x00000001, VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT = 0x00000002, VMA_POOL_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF }
 Flags to be passed as VmaPoolCreateInfo::flags. More...
 

Functions

VkResult vmaCreatePool (VmaAllocator allocator, const VmaPoolCreateInfo *pCreateInfo, VmaPool *pPool)
 Allocates Vulkan device memory and creates VmaPool object. More...
 
void vmaDestroyPool (VmaAllocator allocator, VmaPool pool)
 Destroys VmaPool object and frees Vulkan device memory. More...
 
void vmaGetPoolStats (VmaAllocator allocator, VmaPool pool, VmaPoolStats *pPoolStats)
 Retrieves statistics of existing VmaPool object. More...
 
void vmaMakePoolAllocationsLost (VmaAllocator allocator, VmaPool pool, size_t *pLostAllocationCount)
 Marks all allocations in given pool as lost if they are not used in current frame or VmaPoolCreateInfo::frameInUseCount back from now. More...
 
VkResult vmaAllocateMemory (VmaAllocator allocator, const VkMemoryRequirements *pVkMemoryRequirements, const VmaAllocationCreateInfo *pCreateInfo, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo)
 General purpose memory allocation. More...
 
VkResult vmaAllocateMemoryForBuffer (VmaAllocator allocator, VkBuffer buffer, const VmaAllocationCreateInfo *pCreateInfo, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo)
 
VkResult vmaAllocateMemoryForImage (VmaAllocator allocator, VkImage image, const VmaAllocationCreateInfo *pCreateInfo, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo)
 Function similar to vmaAllocateMemoryForBuffer(). More...
 
void vmaFreeMemory (VmaAllocator allocator, VmaAllocation allocation)
 Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage(). More...
 
void vmaGetAllocationInfo (VmaAllocator allocator, VmaAllocation allocation, VmaAllocationInfo *pAllocationInfo)
 Returns current information about specified allocation. More...
 
void vmaSetAllocationUserData (VmaAllocator allocator, VmaAllocation allocation, void *pUserData)
 Sets pUserData in given allocation to new value. More...
 
void vmaCreateLostAllocation (VmaAllocator allocator, VmaAllocation *pAllocation)
 Creates new allocation that is in lost state from the beginning. More...
 
VkResult vmaMapMemory (VmaAllocator allocator, VmaAllocation allocation, void **ppData)
 
void vmaUnmapMemory (VmaAllocator allocator, VmaAllocation allocation)
 
void vmaUnmapPersistentlyMappedMemory (VmaAllocator allocator)
 Unmaps persistently mapped memory of types that are HOST_COHERENT and DEVICE_LOCAL. More...
 
VkResult vmaMapPersistentlyMappedMemory (VmaAllocator allocator)
 Maps back persistently mapped memory of types that are HOST_COHERENT and DEVICE_LOCAL. More...
 
VkResult vmaDefragment (VmaAllocator allocator, VmaAllocation *pAllocations, size_t allocationCount, VkBool32 *pAllocationsChanged, const VmaDefragmentationInfo *pDefragmentationInfo, VmaDefragmentationStats *pDefragmentationStats)
 Compacts memory by moving allocations. More...
 

Detailed Description

Typedef Documentation

◆ VmaAllocationInfo

Parameters of VmaAllocation objects, that can be retrieved using function vmaGetAllocationInfo().

◆ VmaDefragmentationInfo

Optional configuration parameters to be passed to function vmaDefragment().

◆ VmaDefragmentationStats

Statistics returned by function vmaDefragment().

◆ VmaPoolCreateFlagBits

Flags to be passed as VmaPoolCreateInfo::flags.

◆ VmaPoolCreateFlags

typedef VkFlags VmaPoolCreateFlags

◆ VmaPoolCreateInfo

Describes parameter of created VmaPool.

◆ VmaPoolStats

typedef struct VmaPoolStats VmaPoolStats

Describes parameter of existing VmaPool.

Enumeration Type Documentation

◆ VmaPoolCreateFlagBits

Flags to be passed as VmaPoolCreateInfo::flags.

Enumerator
VMA_POOL_CREATE_PERSISTENT_MAP_BIT 

Set this flag to use a memory that will be persistently mapped.

Each allocation made from this pool will have VmaAllocationInfo::pMappedData available.

Usage of this flag must match usage of VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT flag for every allocation made from this pool.

VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT 

Use this flag if you always allocate only buffers and linear images or only optimal images out of this pool and so Buffer-Image Granularity can be ignored.

This is na optional optimization flag.

If you always allocate using vmaCreateBuffer(), vmaCreateImage(), vmaAllocateMemoryForBuffer(), then you don't need to use it because allocator knows exact type of your allocations so it can handle Buffer-Image Granularity in the optimal way.

If you also allocate using vmaAllocateMemoryForImage() or vmaAllocateMemory(), exact type of such allocations is not known, so allocator must be conservative in handling Buffer-Image Granularity, which can lead to suboptimal allocation (wasted memory). In that case, if you can make sure you always allocate only buffers and linear images or only optimal images out of this pool, use this flag to make allocator disregard Buffer-Image Granularity and so make allocations more optimal.

VMA_POOL_CREATE_FLAG_BITS_MAX_ENUM 

Function Documentation

◆ vmaAllocateMemory()

VkResult vmaAllocateMemory ( VmaAllocator  allocator,
const VkMemoryRequirements *  pVkMemoryRequirements,
const VmaAllocationCreateInfo pCreateInfo,
VmaAllocation *  pAllocation,
VmaAllocationInfo pAllocationInfo 
)

General purpose memory allocation.

Parameters
[out]pAllocationHandle to allocated memory.
[out]pAllocationInfoOptional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo().

You should free the memory using vmaFreeMemory().

It is recommended to use vmaAllocateMemoryForBuffer(), vmaAllocateMemoryForImage(), vmaCreateBuffer(), vmaCreateImage() instead whenever possible.

◆ vmaAllocateMemoryForBuffer()

VkResult vmaAllocateMemoryForBuffer ( VmaAllocator  allocator,
VkBuffer  buffer,
const VmaAllocationCreateInfo pCreateInfo,
VmaAllocation *  pAllocation,
VmaAllocationInfo pAllocationInfo 
)
Parameters
[out]pAllocationHandle to allocated memory.
[out]pAllocationInfoOptional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo().

You should free the memory using vmaFreeMemory().

◆ vmaAllocateMemoryForImage()

VkResult vmaAllocateMemoryForImage ( VmaAllocator  allocator,
VkImage  image,
const VmaAllocationCreateInfo pCreateInfo,
VmaAllocation *  pAllocation,
VmaAllocationInfo pAllocationInfo 
)

Function similar to vmaAllocateMemoryForBuffer().

◆ vmaCreateLostAllocation()

void vmaCreateLostAllocation ( VmaAllocator  allocator,
VmaAllocation *  pAllocation 
)

Creates new allocation that is in lost state from the beginning.

It can be useful if you need a dummy, non-null allocation.

You still need to destroy created object using vmaFreeMemory().

Returned allocation is not tied to any specific memory pool or memory type and not bound to any image or buffer. It has size = 0. It cannot be turned into a real, non-empty allocation.

◆ vmaCreatePool()

VkResult vmaCreatePool ( VmaAllocator  allocator,
const VmaPoolCreateInfo pCreateInfo,
VmaPool *  pPool 
)

Allocates Vulkan device memory and creates VmaPool object.

Parameters
allocatorAllocator object.
pCreateInfoParameters of pool to create.
[out]pPoolHandle to created pool.

◆ vmaDefragment()

VkResult vmaDefragment ( VmaAllocator  allocator,
VmaAllocation *  pAllocations,
size_t  allocationCount,
VkBool32 *  pAllocationsChanged,
const VmaDefragmentationInfo pDefragmentationInfo,
VmaDefragmentationStats pDefragmentationStats 
)

Compacts memory by moving allocations.

Parameters
pAllocationsArray of allocations that can be moved during this compation.
allocationCountNumber of elements in pAllocations and pAllocationsChanged arrays.
[out]pAllocationsChangedArray of boolean values that will indicate whether matching allocation in pAllocations array has been moved. This parameter is optional. Pass null if you don't need this information.
pDefragmentationInfoConfiguration parameters. Optional - pass null to use default values.
[out]pDefragmentationStatsStatistics returned by the function. Optional - pass null if you don't need this information.
Returns
VK_SUCCESS if completed, VK_INCOMPLETE if succeeded but didn't make all possible optimizations because limits specified in pDefragmentationInfo have been reached, negative error code in case of error.

This function works by moving allocations to different places (different VkDeviceMemory objects and/or different offsets) in order to optimize memory usage. Only allocations that are in pAllocations array can be moved. All other allocations are considered nonmovable in this call. Basic rules:

  • Only allocations made in memory types that have VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT flag can be compacted. You may pass other allocations but it makes no sense - these will never be moved.
  • You may pass allocations made with VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT but it makes no sense - they will never be moved.
  • Both allocations made with or without VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT flag can be compacted. If not persistently mapped, memory will be mapped temporarily inside this function if needed, so it shouldn't be mapped by you for the time of this call.
  • You must not pass same VmaAllocation object multiple times in pAllocations array.

The function also frees empty VkDeviceMemory blocks.

After allocation has been moved, its VmaAllocationInfo::deviceMemory and/or VmaAllocationInfo::offset changes. You must query them again using vmaGetAllocationInfo() if you need them.

If an allocation has been moved, data in memory is copied to new place automatically, but if it was bound to a buffer or an image, you must destroy that object yourself, create new one and bind it to the new memory pointed by the allocation. You must use vkDestroyBuffer(), vkDestroyImage(), vkCreateBuffer(), vkCreateImage() for that purpose and NOT vmaDestroyBuffer(), vmaDestroyImage(), vmaCreateBuffer(), vmaCreateImage()! Example:

VkDevice device = ...;
VmaAllocator allocator = ...;
std::vector<VkBuffer> buffers = ...;
std::vector<VmaAllocation> allocations = ...;

std::vector<VkBool32> allocationsChanged(allocations.size());
vmaDefragment(allocator, allocations.data(), allocations.size(), allocationsChanged.data(), nullptr, nullptr);

for(size_t i = 0; i < allocations.size(); ++i)
{
    if(allocationsChanged[i])
    {
        VmaAllocationInfo allocInfo;
        vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);

        vkDestroyBuffer(device, buffers[i], nullptr);

        VkBufferCreateInfo bufferInfo = ...;
        vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);

        .// You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.

        vkBindBufferMemory(device, buffers[i], allocInfo.deviceMemory, allocInfo.offset);
    }
}

This function may be time-consuming, so you shouldn't call it too often (like every frame or after every resource creation/destruction), but rater you can call it on special occasions (like when reloading a game level, when you just destroyed a lot of objects).

◆ vmaDestroyPool()

void vmaDestroyPool ( VmaAllocator  allocator,
VmaPool  pool 
)

Destroys VmaPool object and frees Vulkan device memory.

◆ vmaFreeMemory()

void vmaFreeMemory ( VmaAllocator  allocator,
VmaAllocation  allocation 
)

Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage().

◆ vmaGetAllocationInfo()

void vmaGetAllocationInfo ( VmaAllocator  allocator,
VmaAllocation  allocation,
VmaAllocationInfo pAllocationInfo 
)

Returns current information about specified allocation.

◆ vmaGetPoolStats()

void vmaGetPoolStats ( VmaAllocator  allocator,
VmaPool  pool,
VmaPoolStats pPoolStats 
)

Retrieves statistics of existing VmaPool object.

Parameters
allocatorAllocator object.
poolPool object.
[out]pPoolStatsStatistics of specified pool.

◆ vmaMakePoolAllocationsLost()

void vmaMakePoolAllocationsLost ( VmaAllocator  allocator,
VmaPool  pool,
size_t *  pLostAllocationCount 
)

Marks all allocations in given pool as lost if they are not used in current frame or VmaPoolCreateInfo::frameInUseCount back from now.

Parameters
allocatorAllocator object.
poolPool.
[out]pLostAllocationCountNumber of allocations marked as lost. Optional - pass null if you don't need this information.

◆ vmaMapMemory()

VkResult vmaMapMemory ( VmaAllocator  allocator,
VmaAllocation  allocation,
void **  ppData 
)

Feel free to use vkMapMemory on these memory blocks on you own if you want, but just for convenience and to make sure correct offset and size is always specified, usage of vmaMapMemory() / vmaUnmapMemory() is recommended.

Do not use it on memory allocated with VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT as multiple maps to same VkDeviceMemory is illegal.

◆ vmaMapPersistentlyMappedMemory()

VkResult vmaMapPersistentlyMappedMemory ( VmaAllocator  allocator)

Maps back persistently mapped memory of types that are HOST_COHERENT and DEVICE_LOCAL.

See vmaUnmapPersistentlyMappedMemory().

After this call VmaAllocationInfo::pMappedData of some allocation may have value different than before calling vmaUnmapPersistentlyMappedMemory().

◆ vmaSetAllocationUserData()

void vmaSetAllocationUserData ( VmaAllocator  allocator,
VmaAllocation  allocation,
void *  pUserData 
)

Sets pUserData in given allocation to new value.

◆ vmaUnmapMemory()

void vmaUnmapMemory ( VmaAllocator  allocator,
VmaAllocation  allocation 
)

◆ vmaUnmapPersistentlyMappedMemory()

void vmaUnmapPersistentlyMappedMemory ( VmaAllocator  allocator)

Unmaps persistently mapped memory of types that are HOST_COHERENT and DEVICE_LOCAL.

This is optional performance optimization. On AMD GPUs on Windows, Vulkan memory from the type that has both DEVICE_LOCAL and HOST_VISIBLE flags should not be mapped for the time of any call to vkQueueSubmit() or vkQueuePresent(). Although legal, that would cause performance degradation because WDDM migrates such memory to system RAM. To ensure this, you can unmap all persistently mapped memory using this function. Example:

vmaUnmapPersistentlyMappedMemory(allocator);
vkQueueSubmit(...)
vmaMapPersistentlyMappedMemory(allocator);

After this call VmaAllocationInfo::pMappedData of some allocations may become null.

This call is reference-counted. Memory is mapped again after you call vmaMapPersistentlyMappedMemory() same number of times that you called vmaUnmapPersistentlyMappedMemory().