<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 create these resources in the same place in memory, even if they have completely different parameters (width, height, format etc.).</p>
<p>Such scenario is possible using <aclass="el"href="namespace_d3_d12_m_a.html">D3D12MA</a>, but you need to create your resources using special function <aclass="el"href="class_d3_d12_m_a_1_1_allocator.html#ab45536f92410aedb7be44ea36b1b4717"title="Creates a new resource in place of an existing allocation. This is useful for memory aliasing.">D3D12MA::Allocator::CreateAliasingResource</a>. Before that, you need to allocate memory with parameters calculated using formula:</p>
<divclass="ttc"id="aclass_d3_d12_m_a_1_1_allocation_html"><divclass="ttname"><ahref="class_d3_d12_m_a_1_1_allocation.html">D3D12MA::Allocation</a></div><divclass="ttdoc">Represents single memory allocation.</div><divclass="ttdef"><b>Definition:</b> D3D12MemAlloc.h:461</div></div>
<divclass="ttc"id="aclass_d3_d12_m_a_1_1_allocation_html_adca8d5a82bed492fe7265fcda6e53da2"><divclass="ttname"><ahref="class_d3_d12_m_a_1_1_allocation.html#adca8d5a82bed492fe7265fcda6e53da2">D3D12MA::Allocation::GetHeap</a></div><divclass="ttdeci">ID3D12Heap * GetHeap() const</div><divclass="ttdoc">Returns memory heap that the resource is created in.</div></div>
<divclass="ttc"id="astruct_d3_d12_m_a_1_1_a_l_l_o_c_a_t_i_o_n___d_e_s_c_html"><divclass="ttname"><ahref="struct_d3_d12_m_a_1_1_a_l_l_o_c_a_t_i_o_n___d_e_s_c.html">D3D12MA::ALLOCATION_DESC</a></div><divclass="ttdoc">Parameters of created D3D12MA::Allocation object. To be used with Allocator::CreateResource.</div><divclass="ttdef"><b>Definition:</b> D3D12MemAlloc.h:277</div></div>
<divclass="ttc"id="astruct_d3_d12_m_a_1_1_a_l_l_o_c_a_t_i_o_n___d_e_s_c_html_a97878838f976b2d1e6b1a76881035690"><divclass="ttname"><ahref="struct_d3_d12_m_a_1_1_a_l_l_o_c_a_t_i_o_n___d_e_s_c.html#a97878838f976b2d1e6b1a76881035690">D3D12MA::ALLOCATION_DESC::ExtraHeapFlags</a></div><divclass="ttdeci">D3D12_HEAP_FLAGS ExtraHeapFlags</div><divclass="ttdoc">Additional heap flags to be used when allocating memory.</div><divclass="ttdef"><b>Definition:</b> D3D12MemAlloc.h:302</div></div>
<divclass="ttc"id="astruct_d3_d12_m_a_1_1_a_l_l_o_c_a_t_i_o_n___d_e_s_c_html_aa46b3c0456e5a23edef3328607ebf4d7"><divclass="ttname"><ahref="struct_d3_d12_m_a_1_1_a_l_l_o_c_a_t_i_o_n___d_e_s_c.html#aa46b3c0456e5a23edef3328607ebf4d7">D3D12MA::ALLOCATION_DESC::HeapType</a></div><divclass="ttdeci">D3D12_HEAP_TYPE HeapType</div><divclass="ttdoc">The type of memory heap where the new allocation should be placed.</div><divclass="ttdef"><b>Definition:</b> D3D12MemAlloc.h:286</div></div>
</div><!-- fragment --><p>Remember that using resouces that alias in memory requires proper synchronization. You need to issue a special barrier of type <code>D3D12_RESOURCE_BARRIER_TYPE_ALIASING</code>. You also need to treat a resource after aliasing as uninitialized - containing garbage data. For example, if you use <code>res1</code> and then want to use <code>res2</code>, you need to first initialize <code>res2</code> using either Clear, Discard, or Copy to the entire resource.</p>
<li>D3D12 also allows to interpret contents of memory between aliasing resources consistently in some cases, which is called "data inheritance". For details, see Microsoft documentation chapter "Memory Aliasing and Data Inheritance".</li>
<li>You can create more complex layout where different textures 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 in some further passes. To bind a resource at non-zero offset of an allocation, call <aclass="el"href="class_d3_d12_m_a_1_1_allocator.html#ab45536f92410aedb7be44ea36b1b4717"title="Creates a new resource in place of an existing allocation. This is useful for memory aliasing.">D3D12MA::Allocator::CreateAliasingResource</a> with appropriate value of <code>AllocationLocalOffset</code> parameter.</li>
<li>Resources of the three categories: buffers, textures with <code>RENDER_TARGET</code> or <code>DEPTH_STENCIL</code> flags, and all other textures, can be placed in the same memory only when <code>allocator->GetD3D12Options().ResourceHeapTier >= D3D12_RESOURCE_HEAP_TIER_2</code>. Otherwise they must be placed in different memory heap types, and thus aliasing them is not possible. </li>