Vulkan Memory Allocator
|
Classes | |
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 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... | |
Functions | |
VkResult | vmaAllocateMemory (VmaAllocator allocator, const VkMemoryRequirements *pVkMemoryRequirements, const VmaMemoryRequirements *pVmaMemoryRequirements, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo) |
General purpose memory allocation. More... | |
VkResult | vmaAllocateMemoryForBuffer (VmaAllocator allocator, VkBuffer buffer, const VmaMemoryRequirements *pMemoryRequirements, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo) |
VkResult | vmaAllocateMemoryForImage (VmaAllocator allocator, VkImage image, const VmaMemoryRequirements *pMemoryRequirements, 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... | |
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 is HOST_COHERENT and DEVICE_LOCAL. More... | |
VkResult | vmaMapPersistentlyMappedMemory (VmaAllocator allocator) |
Maps back persistently mapped memory of types that is 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... | |
typedef struct VmaAllocationInfo VmaAllocationInfo |
Parameters of VmaAllocation objects, that can be retrieved using function vmaGetAllocationInfo().
typedef struct VmaDefragmentationInfo VmaDefragmentationInfo |
Optional configuration parameters to be passed to function vmaDefragment().
typedef struct VmaDefragmentationStats VmaDefragmentationStats |
Statistics returned by function vmaDefragment().
VkResult vmaAllocateMemory | ( | VmaAllocator | allocator, |
const VkMemoryRequirements * | pVkMemoryRequirements, | ||
const VmaMemoryRequirements * | pVmaMemoryRequirements, | ||
VmaAllocation * | pAllocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
General purpose memory allocation.
[out] | pAllocation | Handle to allocated memory. |
[out] | pAllocationInfo | Optional. 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.
VkResult vmaAllocateMemoryForBuffer | ( | VmaAllocator | allocator, |
VkBuffer | buffer, | ||
const VmaMemoryRequirements * | pMemoryRequirements, | ||
VmaAllocation * | pAllocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
[out] | pAllocation | Handle to allocated memory. |
[out] | pAllocationInfo | Optional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo(). |
You should free the memory using vmaFreeMemory().
VkResult vmaAllocateMemoryForImage | ( | VmaAllocator | allocator, |
VkImage | image, | ||
const VmaMemoryRequirements * | pMemoryRequirements, | ||
VmaAllocation * | pAllocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
Function similar to vmaAllocateMemoryForBuffer().
VkResult vmaDefragment | ( | VmaAllocator | allocator, |
VmaAllocation * | pAllocations, | ||
size_t | allocationCount, | ||
VkBool32 * | pAllocationsChanged, | ||
const VmaDefragmentationInfo * | pDefragmentationInfo, | ||
VmaDefragmentationStats * | pDefragmentationStats | ||
) |
Compacts memory by moving allocations.
pAllocations | Array of allocations that can be moved during this compation. | |
allocationCount | Number of elements in pAllocations and pAllocationsChanged arrays. | |
[out] | pAllocationsChanged | Array 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. |
pDefragmentationInfo | Configuration parameters. Optional - pass null to use default values. | |
[out] | pDefragmentationStats | Statistics returned by the function. Optional - pass null if you don't need this information. |
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:
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).
void vmaFreeMemory | ( | VmaAllocator | allocator, |
VmaAllocation | allocation | ||
) |
Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage().
void vmaGetAllocationInfo | ( | VmaAllocator | allocator, |
VmaAllocation | allocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
Returns current information about specified allocation.
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_MEMORY_REQUIREMENT_PERSISTENT_MAP_BIT as multiple maps to same VkDeviceMemory is illegal.
VkResult vmaMapPersistentlyMappedMemory | ( | VmaAllocator | allocator | ) |
Maps back persistently mapped memory of types that is HOST_COHERENT and DEVICE_LOCAL.
See vmaUnmapPersistentlyMappedMemory().
After this call VmaAllocationInfo::pMappedData of some allocation may have value different than before calling vmaUnmapPersistentlyMappedMemory().
void vmaSetAllocationUserData | ( | VmaAllocator | allocator, |
VmaAllocation | allocation, | ||
void * | pUserData | ||
) |
Sets pUserData in given allocation to new value.
void vmaUnmapMemory | ( | VmaAllocator | allocator, |
VmaAllocation | allocation | ||
) |
void vmaUnmapPersistentlyMappedMemory | ( | VmaAllocator | allocator | ) |
Unmaps persistently mapped memory of types that is HOST_COHERENT and DEVICE_LOCAL.
This is optional performance optimization. On Windows you should call it before every call to vkQueueSubmit and vkQueuePresent. After which you can remap the allocations again using vmaMapPersistentlyMappedMemory(). This is because of the internal behavior of WDDM. 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().