A memory pool contains a number of VkDeviceMemory
blocks. The library automatically creates and manages default pool for each memory type available on the device. Default memory pool automatically grows in size. Size of allocated blocks is also variable and managed automatically.
You can create custom pool and allocate memory out of it. It can be useful if you want to:
- Keep certain kind of allocations separate from others.
- Enforce particular, fixed size of Vulkan memory blocks.
- Limit maximum amount of Vulkan memory allocated for that pool.
- Reserve minimum or fixed amount of Vulkan memory always preallocated for that pool.
To use custom memory pools:
- Fill VmaPoolCreateInfo structure.
- Call vmaCreatePool() to obtain
VmaPool
handle.
- When making an allocation, set VmaAllocationCreateInfo::pool to this handle. You don't need to specify any other parameters of this structure, like usage.
Example:
poolCreateInfo.blockSize = 128ull * 1024 * 1024;
VmaPool pool;
VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
bufCreateInfo.size = 1024;
bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
allocCreateInfo.
pool = pool;
VkBuffer buf;
VmaAllocation alloc;
vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
You have to free all allocations made from this pool before destroying it.
Choosing memory type index
When creating a pool, you must explicitly specify memory type index. To find the one suitable for your buffers or images, you can use code similar to the following:
VkBufferCreateInfo dummyBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
dummyBufCreateInfo.size = 1024;
dummyBufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
VkBuffer dummyBuf;
vkCreateBuffer(device, &dummyBufCreateInfo, nullptr, &dummyBuf);
VkMemoryRequirements memReq;
vkGetBufferMemoryRequirements(device, dummyBuf, &memReq);
uint32_t memTypeIndex;
vkDestroyBuffer(device, dummyBuf, nullptr);
Dummy buffer is needed to query driver for memReq.memoryTypeBits
. Memory is never allocated for this buffer. You should fill structures dummyBufCreateInfo
and allocCreateInfo
with the same parameters as you are going to use for buffers created in your pool.