<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>
</div><!-- fragment --><p>Remember that using resouces 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>
<p>Additional considerations:</p>
<ul>
<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>
</ul>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<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>