<divclass="textblock"><p>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.</p>
<p>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.).</p>
<p>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:</p>
<divclass="ttc"id="astruct_vma_allocation_create_info_html"><divclass="ttname"><ahref="struct_vma_allocation_create_info.html">VmaAllocationCreateInfo</a></div><divclass="ttdoc">Parameters of new VmaAllocation.</div><divclass="ttdef"><b>Definition</b> vk_mem_alloc.h:1245</div></div>
<divclass="ttc"id="astruct_vma_allocation_create_info_html_a7fe8d81a1ad10b2a2faacacee5b15d6d"><divclass="ttname"><ahref="struct_vma_allocation_create_info.html#a7fe8d81a1ad10b2a2faacacee5b15d6d">VmaAllocationCreateInfo::preferredFlags</a></div><divclass="ttdeci">VkMemoryPropertyFlags preferredFlags</div><divclass="ttdoc">Flags that preferably should be set in a memory type chosen for an allocation.</div><divclass="ttdef"><b>Definition</b> vk_mem_alloc.h:1263</div></div>
<divclass="ttc"id="astruct_vma_allocation_html"><divclass="ttname"><ahref="struct_vma_allocation.html">VmaAllocation</a></div><divclass="ttdoc">Represents single memory allocation.</div></div>
</div><!-- fragment --><p>VMA also provides convenience functions that create a buffer or image and bind it to memory represented by an existing <aclass="el"href="struct_vma_allocation.html"title="Represents single memory allocation.">VmaAllocation</a>: <aclass="el"href="group__group__alloc.html#ga60d5d4803e3c82505a2bfddb929adb03"title="Creates a new VkBuffer, binds already created memory for it.">vmaCreateAliasingBuffer()</a>, <aclass="el"href="group__group__alloc.html#gaf0cf014344213e117bd9f9cf5f928122"title="Creates a new VkBuffer, binds already created memory for it.">vmaCreateAliasingBuffer2()</a>, <aclass="el"href="group__group__alloc.html#gaebc4db1f94b53dba2338b4c0fd80d0dc"title="Function similar to vmaCreateAliasingBuffer() but for images.">vmaCreateAliasingImage()</a>, <aclass="el"href="group__group__alloc.html#ga69ac829f5bb0737449fa92c2d971f1bb"title="Function similar to vmaCreateAliasingBuffer2() but for images.">vmaCreateAliasingImage2()</a>. Versions with "2" offer additional parameter <code>allocationLocalOffset</code>.</p>
<p>Remember that using resources that alias in memory requires proper synchronization. You need to issue a memory barrier to make sure commands that use <code>img1</code> and <code>img2</code> 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 <code>img1</code> and then want to use <code>img2</code>, you need to issue an image memory barrier for <code>img2</code> with <code>oldLayout</code> = <code>VK_IMAGE_LAYOUT_UNDEFINED</code>.</p>
<li>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 <code>VK_IMAGE_CREATE_ALIAS_BIT</code> flag.</li>
<li>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 <aclass="el"href="group__group__alloc.html#ga861f4f27189a7d11ab9d9eedc825cb6b"title="Binds buffer to allocation with additional parameters.">vmaBindBufferMemory2()</a> / <aclass="el"href="group__group__alloc.html#ga5f3502dd7d38b53fb1533ea3921d038d"title="Binds image to allocation with additional parameters.">vmaBindImageMemory2()</a>.</li>
<li>Before allocating memory for the resources you want to alias, check <code>memoryTypeBits</code> 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 <code>COLOR_ATTACHMENT</code> usage, so the sets of memory types supported by your resources may be disjoint. Aliasing them is not possible in that case. </li>