Pyrogenesis trunk
Resource aliasing (overlap)

New explicit graphics APIs (Vulkan and Direct3D 12), thanks to manual memory management, give an opportunity to alias (overlap) multiple resources in the same region of memory - a feature not available in the old APIs (Direct3D 11, OpenGL).

It can be useful to save video memory, but it must be used with caution.

For example, if you know the flow of your whole render frame in advance, you are going to use some intermediate textures or buffers only during a small range of render passes, and you know these ranges don't overlap in time, you can bind these resources to the same place in memory, even if they have completely different parameters (width, height, format etc.).

Resource aliasing (overlap)

Such scenario is possible using VMA, but you need to create your images manually. Then you need to calculate parameters of an allocation to be made using formula:

  • allocation size = max(size of each image)
  • allocation alignment = max(alignment of each image)
  • allocation memoryTypeBits = bitwise AND(memoryTypeBits of each image)

Following example shows two different images bound to the same place in memory, allocated to fit largest of them.

// A 512x512 texture to be sampled.
img1CreateInfo.imageType = VK_IMAGE_TYPE_2D;
img1CreateInfo.extent.width = 512;
img1CreateInfo.extent.height = 512;
img1CreateInfo.extent.depth = 1;
img1CreateInfo.mipLevels = 10;
img1CreateInfo.arrayLayers = 1;
img1CreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
// A full screen texture to be used as color attachment.
img2CreateInfo.imageType = VK_IMAGE_TYPE_2D;
img2CreateInfo.extent.width = 1920;
img2CreateInfo.extent.height = 1080;
img2CreateInfo.extent.depth = 1;
img2CreateInfo.mipLevels = 1;
img2CreateInfo.arrayLayers = 1;
img2CreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
VkImage img1;
res = vkCreateImage(device, &img1CreateInfo, nullptr, &img1);
VkImage img2;
res = vkCreateImage(device, &img2CreateInfo, nullptr, &img2);
vkGetImageMemoryRequirements(device, img1, &img1MemReq);
vkGetImageMemoryRequirements(device, img2, &img2MemReq);
VkMemoryRequirements finalMemReq = {};
finalMemReq.size = std::max(img1MemReq.size, img2MemReq.size);
finalMemReq.alignment = std::max(img1MemReq.alignment, img2MemReq.alignment);
finalMemReq.memoryTypeBits = img1MemReq.memoryTypeBits & img2MemReq.memoryTypeBits;
// Validate if(finalMemReq.memoryTypeBits != 0)
VmaAllocationCreateInfo allocCreateInfo = {};
res = vmaAllocateMemory(allocator, &finalMemReq, &allocCreateInfo, &alloc, nullptr);
res = vmaBindImageMemory(allocator, alloc, img1);
res = vmaBindImageMemory(allocator, alloc, img2);
// You can use img1, img2 here, but not at the same time!
vmaFreeMemory(allocator, alloc);
vkDestroyImage(allocator, img2, nullptr);
vkDestroyImage(allocator, img1, nullptr);
VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory(VmaAllocator VMA_NOT_NULL allocator, VmaAllocation VMA_NOT_NULL allocation, VkImage VMA_NOT_NULL_NON_DISPATCHABLE image)
Binds image to allocation.
VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory(VmaAllocator VMA_NOT_NULL allocator, const VmaAllocation VMA_NULLABLE allocation)
Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(),...
VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory(VmaAllocator VMA_NOT_NULL allocator, const VkMemoryRequirements *VMA_NOT_NULL pVkMemoryRequirements, const VmaAllocationCreateInfo *VMA_NOT_NULL pCreateInfo, VmaAllocation VMA_NULLABLE *VMA_NOT_NULL pAllocation, VmaAllocationInfo *VMA_NULLABLE pAllocationInfo)
General purpose memory allocation.
uint32_t depth
Definition: vulkan.h:1779
uint32_t height
Definition: vulkan.h:1778
uint32_t width
Definition: vulkan.h:1777
Definition: vulkan.h:2685
VkImageLayout initialLayout
Definition: vulkan.h:2700
uint32_t mipLevels
Definition: vulkan.h:2692
uint32_t arrayLayers
Definition: vulkan.h:2693
VkSampleCountFlagBits samples
Definition: vulkan.h:2694
VkExtent3D extent
Definition: vulkan.h:2691
VkFormat format
Definition: vulkan.h:2690
VkImageType imageType
Definition: vulkan.h:2689
VkImageTiling tiling
Definition: vulkan.h:2695
VkImageUsageFlags usage
Definition: vulkan.h:2696
Definition: vulkan.h:2540
uint32_t memoryTypeBits
Definition: vulkan.h:2543
VkDeviceSize size
Definition: vulkan.h:2541
VkDeviceSize alignment
Definition: vulkan.h:2542
Parameters of new VmaAllocation.
Definition: vk_mem_alloc.h:1222
VkMemoryPropertyFlags preferredFlags
Flags that preferably should be set in a memory type chosen for an allocation.
Definition: vk_mem_alloc.h:1240
Represents single memory allocation.
@ VK_IMAGE_LAYOUT_UNDEFINED
Definition: vulkan.h:813
@ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
Definition: vulkan.h:917
@ VK_IMAGE_TILING_OPTIMAL
Definition: vulkan.h:828
@ VK_IMAGE_USAGE_TRANSFER_DST_BIT
Definition: vulkan.h:840
@ VK_IMAGE_USAGE_SAMPLED_BIT
Definition: vulkan.h:841
@ VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT
Definition: vulkan.h:843
@ VK_SAMPLE_COUNT_1_BIT
Definition: vulkan.h:1357
#define vkCreateImage
Definition: vulkan.h:4619
#define vkDestroyImage
Definition: vulkan.h:4673
@ VK_IMAGE_TYPE_2D
Definition: vulkan.h:834
#define vkGetImageMemoryRequirements
Definition: vulkan.h:4767
@ VK_FORMAT_R8G8B8A8_SRGB
Definition: vulkan.h:550
@ VK_FORMAT_R8G8B8A8_UNORM
Definition: vulkan.h:544
@ VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO
Definition: vulkan.h:1112

Remember that using resources that alias in memory requires proper synchronization. You need to issue a memory barrier to make sure commands that use img1 and img2 don't overlap on GPU timeline. You also need to treat a resource after aliasing as uninitialized - containing garbage data. For example, if you use img1 and then want to use img2, you need to issue an image memory barrier for img2 with oldLayout = VK_IMAGE_LAYOUT_UNDEFINED.

Additional considerations:

  • Vulkan also allows to interpret contents of memory between aliasing resources consistently in some cases. See chapter 11.8. "Memory Aliasing" of Vulkan specification or VK_IMAGE_CREATE_ALIAS_BIT flag.
  • You can create more complex layout where different images and buffers are bound at different offsets inside one large allocation. For example, one can imagine a big texture used in some render passes, aliasing with a set of many small buffers used between in some further passes. To bind a resource at non-zero offset in an allocation, use vmaBindBufferMemory2() / vmaBindImageMemory2().
  • Before allocating memory for the resources you want to alias, check memoryTypeBits returned in memory requirements of each resource to make sure the bits overlap. Some GPUs may expose multiple memory types suitable e.g. only for buffers or images with COLOR_ATTACHMENT usage, so the sets of memory types supported by your resources may be disjoint. Aliasing them is not possible in that case.