<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>
<ul>
<li>allocation size = max(size of each image)</li>
<li>allocation alignment = max(alignment of each image)</li>
<li>allocation memoryTypeBits = bitwise AND(memoryTypeBits of each image)</li>
</ul>
<p>Following example shows two different images bound to the same place in memory, allocated to fit largest of them.</p>
<divclass="fragment"><divclass="line"><spanclass="comment">// A 512x512 texture to be sampled.</span></div>
<divclass="ttc"id="astruct_vma_allocation_create_info_html_accb8b06b1f677d858cb9af20705fa910"><divclass="ttname"><ahref="struct_vma_allocation_create_info.html#accb8b06b1f677d858cb9af20705fa910">VmaAllocationCreateInfo::usage</a></div><divclass="ttdeci">VmaMemoryUsage usage</div><divclass="ttdoc">Intended usage of memory.</div><divclass="ttdef"><b>Definition:</b> vk_mem_alloc.h:2887</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>
<divclass="ttc"id="avk__mem__alloc_8h_html_a3d3ca45799923aa5d138e9e5f9eb2da5"><divclass="ttname"><ahref="vk__mem__alloc_8h.html#a3d3ca45799923aa5d138e9e5f9eb2da5">vmaBindImageMemory</a></div><divclass="ttdeci">VkResult vmaBindImageMemory(VmaAllocator allocator, VmaAllocation allocation, VkImage image)</div><divclass="ttdoc">Binds image to allocation.</div></div>
</div><!-- fragment --><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 of an allocation, use <aclass="el"href="vk__mem__alloc_8h.html#a927c944f45e0f2941182abb6f608e64a"title="Binds buffer to allocation with additional parameters.">vmaBindBufferMemory2()</a> / <aclass="el"href="vk__mem__alloc_8h.html#aa8251ee81b0045a443e35b8e8aa021bc"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>