Vulkan Memory Allocator
|
#include <vulkan/vulkan.h>
Go to the source code of this file.
Classes | |
struct | VmaDeviceMemoryCallbacks |
Set of callbacks that the library will call for vkAllocateMemory and vkFreeMemory . More... | |
struct | VmaVulkanFunctions |
Pointers to some Vulkan functions - a subset used by the library. More... | |
struct | VmaAllocatorCreateInfo |
Description of a Allocator to be created. More... | |
struct | VmaStatInfo |
Calculated statistics of memory usage in entire allocator. More... | |
struct | VmaStats |
General statistics from current state of Allocator. More... | |
struct | VmaAllocationCreateInfo |
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... | |
Macros | |
#define | VMA_STATS_STRING_ENABLED 1 |
Typedefs | |
typedef void(VKAPI_PTR * | PFN_vmaAllocateDeviceMemoryFunction) (VmaAllocator allocator, uint32_t memoryType, VkDeviceMemory memory, VkDeviceSize size) |
Callback function called after successful vkAllocateMemory. More... | |
typedef void(VKAPI_PTR * | PFN_vmaFreeDeviceMemoryFunction) (VmaAllocator allocator, uint32_t memoryType, VkDeviceMemory memory, VkDeviceSize size) |
Callback function called before vkFreeMemory. More... | |
typedef struct VmaDeviceMemoryCallbacks | VmaDeviceMemoryCallbacks |
Set of callbacks that the library will call for vkAllocateMemory and vkFreeMemory . More... | |
typedef enum VmaAllocatorCreateFlagBits | VmaAllocatorCreateFlagBits |
Flags for created VmaAllocator. More... | |
typedef VkFlags | VmaAllocatorCreateFlags |
typedef struct VmaVulkanFunctions | VmaVulkanFunctions |
Pointers to some Vulkan functions - a subset used by the library. More... | |
typedef struct VmaAllocatorCreateInfo | VmaAllocatorCreateInfo |
Description of a Allocator to be created. More... | |
typedef struct VmaStatInfo | VmaStatInfo |
Calculated statistics of memory usage in entire allocator. More... | |
typedef struct VmaStats | VmaStats |
General statistics from current state of Allocator. More... | |
typedef enum VmaMemoryUsage | VmaMemoryUsage |
typedef enum VmaAllocationCreateFlagBits | VmaAllocationCreateFlagBits |
Flags to be passed as VmaAllocationCreateInfo::flags. More... | |
typedef VkFlags | VmaAllocationCreateFlags |
typedef struct VmaAllocationCreateInfo | VmaAllocationCreateInfo |
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... | |
Functions | |
VkResult | vmaCreateAllocator (const VmaAllocatorCreateInfo *pCreateInfo, VmaAllocator *pAllocator) |
Creates Allocator object. More... | |
void | vmaDestroyAllocator (VmaAllocator allocator) |
Destroys allocator object. More... | |
void | vmaGetPhysicalDeviceProperties (VmaAllocator allocator, const VkPhysicalDeviceProperties **ppPhysicalDeviceProperties) |
void | vmaGetMemoryProperties (VmaAllocator allocator, const VkPhysicalDeviceMemoryProperties **ppPhysicalDeviceMemoryProperties) |
void | vmaGetMemoryTypeProperties (VmaAllocator allocator, uint32_t memoryTypeIndex, VkMemoryPropertyFlags *pFlags) |
Given Memory Type Index, returns Property Flags of this memory type. More... | |
void | vmaSetCurrentFrameIndex (VmaAllocator allocator, uint32_t frameIndex) |
Sets index of the current frame. More... | |
void | vmaCalculateStats (VmaAllocator allocator, VmaStats *pStats) |
Retrieves statistics from current state of the Allocator. More... | |
void | vmaBuildStatsString (VmaAllocator allocator, char **ppStatsString, VkBool32 detailedMap) |
Builds and returns statistics as string in JSON format. More... | |
void | vmaFreeStatsString (VmaAllocator allocator, char *pStatsString) |
VkResult | vmaFindMemoryTypeIndex (VmaAllocator allocator, uint32_t memoryTypeBits, const VmaAllocationCreateInfo *pAllocationCreateInfo, uint32_t *pMemoryTypeIndex) |
Helps to find memoryTypeIndex, given memoryTypeBits and VmaAllocationCreateInfo. More... | |
VkResult | vmaFindMemoryTypeIndexForBufferInfo (VmaAllocator allocator, const VkBufferCreateInfo *pBufferCreateInfo, const VmaAllocationCreateInfo *pAllocationCreateInfo, uint32_t *pMemoryTypeIndex) |
Helps to find memoryTypeIndex, given VkBufferCreateInfo and VmaAllocationCreateInfo. More... | |
VkResult | vmaFindMemoryTypeIndexForImageInfo (VmaAllocator allocator, const VkImageCreateInfo *pImageCreateInfo, const VmaAllocationCreateInfo *pAllocationCreateInfo, uint32_t *pMemoryTypeIndex) |
Helps to find memoryTypeIndex, given VkImageCreateInfo and VmaAllocationCreateInfo. More... | |
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 and atomically marks it as used in current frame. More... | |
VkBool32 | vmaTouchAllocation (VmaAllocator allocator, VmaAllocation allocation) |
Returns VK_TRUE if allocation is not lost and atomically marks it as used in current frame. 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) |
Maps memory represented by given allocation and returns pointer to it. More... | |
void | vmaUnmapMemory (VmaAllocator allocator, VmaAllocation allocation) |
Unmaps memory represented by given allocation, mapped previously using vmaMapMemory(). More... | |
VkResult | vmaDefragment (VmaAllocator allocator, VmaAllocation *pAllocations, size_t allocationCount, VkBool32 *pAllocationsChanged, const VmaDefragmentationInfo *pDefragmentationInfo, VmaDefragmentationStats *pDefragmentationStats) |
Compacts memory by moving allocations. More... | |
VkResult | vmaCreateBuffer (VmaAllocator allocator, const VkBufferCreateInfo *pBufferCreateInfo, const VmaAllocationCreateInfo *pAllocationCreateInfo, VkBuffer *pBuffer, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo) |
void | vmaDestroyBuffer (VmaAllocator allocator, VkBuffer buffer, VmaAllocation allocation) |
Destroys Vulkan buffer and frees allocated memory. More... | |
VkResult | vmaCreateImage (VmaAllocator allocator, const VkImageCreateInfo *pImageCreateInfo, const VmaAllocationCreateInfo *pAllocationCreateInfo, VkImage *pImage, VmaAllocation *pAllocation, VmaAllocationInfo *pAllocationInfo) |
Function similar to vmaCreateBuffer(). More... | |
void | vmaDestroyImage (VmaAllocator allocator, VkImage image, VmaAllocation allocation) |
Destroys Vulkan image and frees allocated memory. More... | |
#define VMA_STATS_STRING_ENABLED 1 |
typedef void(VKAPI_PTR * PFN_vmaAllocateDeviceMemoryFunction) (VmaAllocator allocator, uint32_t memoryType, VkDeviceMemory memory, VkDeviceSize size) |
Callback function called after successful vkAllocateMemory.
typedef void(VKAPI_PTR * PFN_vmaFreeDeviceMemoryFunction) (VmaAllocator allocator, uint32_t memoryType, VkDeviceMemory memory, VkDeviceSize size) |
Callback function called before vkFreeMemory.
typedef enum VmaAllocationCreateFlagBits VmaAllocationCreateFlagBits |
Flags to be passed as VmaAllocationCreateInfo::flags.
typedef VkFlags VmaAllocationCreateFlags |
typedef struct VmaAllocationCreateInfo VmaAllocationCreateInfo |
typedef struct VmaAllocationInfo VmaAllocationInfo |
Parameters of VmaAllocation
objects, that can be retrieved using function vmaGetAllocationInfo().
typedef enum VmaAllocatorCreateFlagBits VmaAllocatorCreateFlagBits |
Flags for created VmaAllocator.
typedef VkFlags VmaAllocatorCreateFlags |
typedef struct VmaAllocatorCreateInfo VmaAllocatorCreateInfo |
Description of a Allocator to be created.
typedef struct VmaDefragmentationInfo VmaDefragmentationInfo |
Optional configuration parameters to be passed to function vmaDefragment().
typedef struct VmaDefragmentationStats VmaDefragmentationStats |
Statistics returned by function vmaDefragment().
typedef struct VmaDeviceMemoryCallbacks VmaDeviceMemoryCallbacks |
Set of callbacks that the library will call for vkAllocateMemory
and vkFreeMemory
.
Provided for informative purpose, e.g. to gather statistics about number of allocations or total amount of memory allocated in Vulkan.
typedef enum VmaMemoryUsage VmaMemoryUsage |
typedef enum VmaPoolCreateFlagBits VmaPoolCreateFlagBits |
Flags to be passed as VmaPoolCreateInfo::flags.
typedef VkFlags VmaPoolCreateFlags |
typedef struct VmaPoolCreateInfo VmaPoolCreateInfo |
Describes parameter of created VmaPool
.
typedef struct VmaPoolStats VmaPoolStats |
Describes parameter of existing VmaPool
.
typedef struct VmaStatInfo VmaStatInfo |
Calculated statistics of memory usage in entire allocator.
typedef struct VmaVulkanFunctions VmaVulkanFunctions |
Pointers to some Vulkan functions - a subset used by the library.
Flags to be passed as VmaAllocationCreateInfo::flags.
Enumerator | |
---|---|
VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT | Set this flag if the allocation should have its own memory block. Use it for special, big resources, like fullscreen images used as attachments. This flag must also be used for host visible resources that you want to map simultaneously because otherwise they might end up as regions of the same You should not use this flag if VmaAllocationCreateInfo::pool is not null. |
VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT | Set this flag to only try to allocate from existing If new allocation cannot be placed in any of the existing blocks, allocation fails with You should not use VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT and VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT at the same time. It makes no sense. If VmaAllocationCreateInfo::pool is not null, this flag is implied and ignored. |
VMA_ALLOCATION_CREATE_MAPPED_BIT | Set this flag to use a memory that will be persistently mapped and retrieve pointer to it. Pointer to mapped memory will be returned through VmaAllocationInfo::pMappedData. Is it valid to use this flag for allocation made from memory type that is not You should not use this flag together with VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT. |
VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT | Allocation created with this flag can become lost as a result of another allocation with VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flag, so you must check it before use. To check if allocation is not lost, call vmaGetAllocationInfo() and check if VmaAllocationInfo::deviceMemory is not For details about supporting lost allocations, see Lost Allocations chapter of User Guide on Main Page. You should not use this flag together with VMA_ALLOCATION_CREATE_MAPPED_BIT. |
VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT | While creating allocation using this flag, other allocations that were created with flag VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT can become lost. For details about supporting lost allocations, see Lost Allocations chapter of User Guide on Main Page. |
VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT | Set this flag to treat VmaAllocationCreateInfo::pUserData as pointer to a null-terminated string. Instead of copying pointer value, a local copy of the string is made and stored in allocation's |
VMA_ALLOCATION_CREATE_FLAG_BITS_MAX_ENUM |
Flags for created VmaAllocator.
Enumerator | |
---|---|
VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT | Allocator and all objects created from it will not be synchronized internally, so you must guarantee they are used from only one thread at a time or synchronized externally by you. Using this flag may increase performance because internal mutexes are not used. |
VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT | Enables usage of VK_KHR_dedicated_allocation extension. Using this extenion will automatically allocate dedicated blocks of memory for some buffers and images instead of suballocating place for them out of bigger memory blocks (as if you explicitly used VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT flag) when it is recommended by the driver. It may improve performance on some GPUs. You may set this flag only if you found out that following device extensions are supported, you enabled them while creating Vulkan device passed as VmaAllocatorCreateInfo::device, and you want them to be used internally by this library:
When this flag is set, you can experience following warnings reported by Vulkan validation layer. You can ignore them.
|
VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM |
enum VmaMemoryUsage |
Enumerator | |
---|---|
VMA_MEMORY_USAGE_UNKNOWN | No intended memory usage specified. Use other members of VmaAllocationCreateInfo to specify your requirements. |
VMA_MEMORY_USAGE_GPU_ONLY | Memory will be used on device only, so fast access from the device is preferred. It usually means device-local GPU (video) memory. No need to be mappable on host. It is roughly equivalent of Usage:
Allocation may still end up in |
VMA_MEMORY_USAGE_CPU_ONLY | Memory will be mappable on host. It usually means CPU (system) memory. Resources created in this pool may still be accessible to the device, but access to them can be slower. Guarantees to be Usage: Staging copy of resources used as transfer source. |
VMA_MEMORY_USAGE_CPU_TO_GPU | Memory that is both mappable on host (guarantees to be Usage: Resources written frequently by host (dynamic), read by device. E.g. textures, vertex buffers, uniform buffers updated every frame or every draw call. |
VMA_MEMORY_USAGE_GPU_TO_CPU | Memory mappable on host (guarantees to be Usage:
|
VMA_MEMORY_USAGE_MAX_ENUM |
Flags to be passed as VmaPoolCreateInfo::flags.
Enumerator | |
---|---|
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 |
VkResult vmaAllocateMemory | ( | VmaAllocator | allocator, |
const VkMemoryRequirements * | pVkMemoryRequirements, | ||
const VmaAllocationCreateInfo * | pCreateInfo, | ||
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 VmaAllocationCreateInfo * | pCreateInfo, | ||
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 VmaAllocationCreateInfo * | pCreateInfo, | ||
VmaAllocation * | pAllocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
Function similar to vmaAllocateMemoryForBuffer().
void vmaBuildStatsString | ( | VmaAllocator | allocator, |
char ** | ppStatsString, | ||
VkBool32 | detailedMap | ||
) |
Builds and returns statistics as string in JSON format.
[out] | ppStatsString | Must be freed using vmaFreeStatsString() function. |
void vmaCalculateStats | ( | VmaAllocator | allocator, |
VmaStats * | pStats | ||
) |
Retrieves statistics from current state of the Allocator.
VkResult vmaCreateAllocator | ( | const VmaAllocatorCreateInfo * | pCreateInfo, |
VmaAllocator * | pAllocator | ||
) |
Creates Allocator object.
VkResult vmaCreateBuffer | ( | VmaAllocator | allocator, |
const VkBufferCreateInfo * | pBufferCreateInfo, | ||
const VmaAllocationCreateInfo * | pAllocationCreateInfo, | ||
VkBuffer * | pBuffer, | ||
VmaAllocation * | pAllocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
[out] | pBuffer | Buffer that was created. |
[out] | pAllocation | Allocation that was created. |
[out] | pAllocationInfo | Optional. Information about allocated memory. It can be later fetched using function vmaGetAllocationInfo(). |
This function automatically:
If any of these operations fail, buffer and allocation are not created, returned value is negative error code, *pBuffer and *pAllocation are null.
If the function succeeded, you must destroy both buffer and allocation when you no longer need them using either convenience function vmaDestroyBuffer() or separately, using vkDestroyBuffer()
and vmaFreeMemory().
If VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT flag was used, VK_KHR_dedicated_allocation extension is used internally to query driver whether it requires or prefers the new buffer to have dedicated allocation. If yes, and if dedicated allocation is possible (VmaAllocationCreateInfo::pool is null and VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT is not used), it creates dedicated allocation for this buffer, just like when using VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.
VkResult vmaCreateImage | ( | VmaAllocator | allocator, |
const VkImageCreateInfo * | pImageCreateInfo, | ||
const VmaAllocationCreateInfo * | pAllocationCreateInfo, | ||
VkImage * | pImage, | ||
VmaAllocation * | pAllocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
Function similar to vmaCreateBuffer().
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.
VkResult vmaCreatePool | ( | VmaAllocator | allocator, |
const VmaPoolCreateInfo * | pCreateInfo, | ||
VmaPool * | pPool | ||
) |
Allocates Vulkan device memory and creates VmaPool
object.
allocator | Allocator object. | |
pCreateInfo | Parameters of pool to create. | |
[out] | pPool | Handle to created pool. |
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:
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.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:
Note: Please don't expect memory to be fully compacted after this call. Algorithms inside are based on some heuristics that try to maximize number of Vulkan memory blocks to make totally empty to release them, as well as to maximimze continuous empty space inside remaining blocks, while minimizing the number and size of data that needs to be moved. Some fragmentation still remains after this call. This is normal.
Warning: This function is not 100% correct according to Vulkan specification. Use it at your own risk. That's because Vulkan doesn't guarantee that memory requirements (size and alignment) for a new buffer or image are consistent. They may be different even for subsequent calls with the same parameters. It really does happen on some platforms, especially with images.
Warning: This function may be time-consuming, so you shouldn't call it too often (like every frame or after every resource creation/destruction). You can call it on special occasions (like when reloading a game level or when you just destroyed a lot of objects).
void vmaDestroyAllocator | ( | VmaAllocator | allocator | ) |
Destroys allocator object.
void vmaDestroyBuffer | ( | VmaAllocator | allocator, |
VkBuffer | buffer, | ||
VmaAllocation | allocation | ||
) |
Destroys Vulkan buffer and frees allocated memory.
This is just a convenience function equivalent to:
It it safe to pass null as buffer and/or allocation.
void vmaDestroyImage | ( | VmaAllocator | allocator, |
VkImage | image, | ||
VmaAllocation | allocation | ||
) |
Destroys Vulkan image and frees allocated memory.
This is just a convenience function equivalent to:
It it safe to pass null as image and/or allocation.
void vmaDestroyPool | ( | VmaAllocator | allocator, |
VmaPool | pool | ||
) |
Destroys VmaPool object and frees Vulkan device memory.
VkResult vmaFindMemoryTypeIndex | ( | VmaAllocator | allocator, |
uint32_t | memoryTypeBits, | ||
const VmaAllocationCreateInfo * | pAllocationCreateInfo, | ||
uint32_t * | pMemoryTypeIndex | ||
) |
Helps to find memoryTypeIndex, given memoryTypeBits and VmaAllocationCreateInfo.
This algorithm tries to find a memory type that:
VkResult vmaFindMemoryTypeIndexForBufferInfo | ( | VmaAllocator | allocator, |
const VkBufferCreateInfo * | pBufferCreateInfo, | ||
const VmaAllocationCreateInfo * | pAllocationCreateInfo, | ||
uint32_t * | pMemoryTypeIndex | ||
) |
Helps to find memoryTypeIndex, given VkBufferCreateInfo and VmaAllocationCreateInfo.
It can be useful e.g. to determine value to be used as VmaPoolCreateInfo::memoryTypeIndex. It internally creates a temporary, dummy buffer that never has memory bound. It is just a convenience function, equivalent to calling:
vkCreateBuffer
vkGetBufferMemoryRequirements
vmaFindMemoryTypeIndex
vkDestroyBuffer
VkResult vmaFindMemoryTypeIndexForImageInfo | ( | VmaAllocator | allocator, |
const VkImageCreateInfo * | pImageCreateInfo, | ||
const VmaAllocationCreateInfo * | pAllocationCreateInfo, | ||
uint32_t * | pMemoryTypeIndex | ||
) |
Helps to find memoryTypeIndex, given VkImageCreateInfo and VmaAllocationCreateInfo.
It can be useful e.g. to determine value to be used as VmaPoolCreateInfo::memoryTypeIndex. It internally creates a temporary, dummy image that never has memory bound. It is just a convenience function, equivalent to calling:
vkCreateImage
vkGetImageMemoryRequirements
vmaFindMemoryTypeIndex
vkDestroyImage
void vmaFreeMemory | ( | VmaAllocator | allocator, |
VmaAllocation | allocation | ||
) |
Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage().
void vmaFreeStatsString | ( | VmaAllocator | allocator, |
char * | pStatsString | ||
) |
void vmaGetAllocationInfo | ( | VmaAllocator | allocator, |
VmaAllocation | allocation, | ||
VmaAllocationInfo * | pAllocationInfo | ||
) |
Returns current information about specified allocation and atomically marks it as used in current frame.
Current paramters of given allocation are returned in pAllocationInfo
.
This function also atomically "touches" allocation - marks it as used in current frame, just like vmaTouchAllocation(). If the allocation is in lost state, pAllocationInfo->deviceMemory == VK_NULL_HANDLE
.
Although this function uses atomics and doesn't lock any mutex, so it should be quite efficient, you can avoid calling it too often.
void vmaGetMemoryProperties | ( | VmaAllocator | allocator, |
const VkPhysicalDeviceMemoryProperties ** | ppPhysicalDeviceMemoryProperties | ||
) |
PhysicalDeviceMemoryProperties are fetched from physicalDevice by the allocator. You can access it here, without fetching it again on your own.
void vmaGetMemoryTypeProperties | ( | VmaAllocator | allocator, |
uint32_t | memoryTypeIndex, | ||
VkMemoryPropertyFlags * | pFlags | ||
) |
Given Memory Type Index, returns Property Flags of this memory type.
This is just a convenience function. Same information can be obtained using vmaGetMemoryProperties().
void vmaGetPhysicalDeviceProperties | ( | VmaAllocator | allocator, |
const VkPhysicalDeviceProperties ** | ppPhysicalDeviceProperties | ||
) |
PhysicalDeviceProperties are fetched from physicalDevice by the allocator. You can access it here, without fetching it again on your own.
void vmaGetPoolStats | ( | VmaAllocator | allocator, |
VmaPool | pool, | ||
VmaPoolStats * | pPoolStats | ||
) |
Retrieves statistics of existing VmaPool object.
allocator | Allocator object. | |
pool | Pool object. | |
[out] | pPoolStats | Statistics of specified pool. |
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.
allocator | Allocator object. | |
pool | Pool. | |
[out] | pLostAllocationCount | Number of allocations marked as lost. Optional - pass null if you don't need this information. |
VkResult vmaMapMemory | ( | VmaAllocator | allocator, |
VmaAllocation | allocation, | ||
void ** | ppData | ||
) |
Maps memory represented by given allocation and returns pointer to it.
Maps memory represented by given allocation to make it accessible to CPU code. When succeeded, *ppData
contains pointer to first byte of this memory. If the allocation is part of bigger VkDeviceMemory
block, the pointer is correctly offseted to the beginning of region assigned to this particular allocation.
Mapping is internally reference-counted and synchronized, so despite raw Vulkan function vkMapMemory()
cannot be used to map same block of VkDeviceMemory
multiple times simultaneously, it is safe to call this function on allocations assigned to the same memory block. Actual Vulkan memory will be mapped on first mapping and unmapped on last unmapping.
If the function succeeded, you must call vmaUnmapMemory() to unmap the allocation when mapping is no longer needed or before freeing the allocation, at the latest.
It also safe to call this function multiple times on the same allocation. You must call vmaUnmapMemory() same number of times as you called vmaMapMemory().
It is also safe to call this function on allocation created with VMA_ALLOCATION_CREATE_MAPPED_BIT flag. Its memory stays mapped all the time. You must still call vmaUnmapMemory() same number of times as you called vmaMapMemory(). You must not call vmaUnmapMemory() additional time to free the "0-th" mapping made automatically due to VMA_ALLOCATION_CREATE_MAPPED_BIT flag.
This function fails when used on allocation made in memory type that is not HOST_VISIBLE
.
This function always fails when called for allocation that was created with VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocations cannot be mapped.
void vmaSetAllocationUserData | ( | VmaAllocator | allocator, |
VmaAllocation | allocation, | ||
void * | pUserData | ||
) |
Sets pUserData in given allocation to new value.
If the allocation was created with VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT, pUserData must be either null, or pointer to a null-terminated string. The function makes local copy of the string and sets it as allocation's pUserData
. String passed as pUserData doesn't need to be valid for whole lifetime of the allocation - you can free it after this call. String previously pointed by allocation's pUserData is freed from memory.
If the flag was not used, the value of pointer pUserData
is just copied to allocation's pUserData
. It is opaque, so you can use it however you want - e.g. as a pointer, ordinal number or some handle to you own data.
void vmaSetCurrentFrameIndex | ( | VmaAllocator | allocator, |
uint32_t | frameIndex | ||
) |
Sets index of the current frame.
This function must be used if you make allocations with VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT and VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flags to inform the allocator when a new frame begins. Allocations queried using vmaGetAllocationInfo() cannot become lost in the current frame.
VkBool32 vmaTouchAllocation | ( | VmaAllocator | allocator, |
VmaAllocation | allocation | ||
) |
Returns VK_TRUE
if allocation is not lost and atomically marks it as used in current frame.
If the allocation has been created with VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag, this function returns VK_TRUE
if it's not in lost state, so it can still be used. It then also atomically "touches" the allocation - marks it as used in current frame, so that you can be sure it won't become lost in current frame or next frameInUseCount
frames.
If the allocation is in lost state, the function returns VK_FALSE
. Memory of such allocation, as well as buffer or image bound to it, should not be used. Lost allocation and the buffer/image still need to be destroyed.
If the allocation has been created without VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag, this function always returns VK_TRUE
.
void vmaUnmapMemory | ( | VmaAllocator | allocator, |
VmaAllocation | allocation | ||
) |
Unmaps memory represented by given allocation, mapped previously using vmaMapMemory().
For details, see description of vmaMapMemory().