blob: 62415503ba6ea159e7e0caba780ac82f20ead543 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Direct3D 12 Memory Allocator: Defragmentation</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Direct3D 12 Memory Allocator
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div id="nav-path" class="navpath">
<ul>
<li class="navelem"><a class="el" href="index.html">D3D12 Memory Allocator</a></li> </ul>
</div>
</div><!-- top -->
<div><div class="header">
<div class="headertitle"><div class="title">Defragmentation </div></div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p >Interleaved allocations and deallocations of many objects of varying size can cause fragmentation over time, which can lead to a situation where the library is unable to find a continuous range of free memory for a new allocation despite there is enough free space, just scattered across many small free ranges between existing allocations.</p>
<p >To mitigate this problem, you can use defragmentation feature. It doesn't happen automatically though and needs your cooperation, because D3D12MA is a low level library that only allocates memory. It cannot recreate buffers and textures in a new place as it doesn't remember the contents of <code>D3D12_RESOURCE_DESC</code> structure. It cannot copy their contents as it doesn't record any commands to a command list.</p>
<p >Example:</p>
<div class="fragment"><div class="line"><a class="code hl_struct" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c.html">D3D12MA::DEFRAGMENTATION_DESC</a> defragDesc = {};</div>
<div class="line">defragDesc.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c.html#aac9a357e679a0afa9ab84cd06057630b">Flags</a> = <a class="code hl_enumvalue" href="namespace_d3_d12_m_a.html#ab433989212ee6736bf9d63c5bc565df4a9d0301a81136ca79e3ba52542c6d2e13">D3D12MA::DEFRAGMENTATION_FLAG_ALGORITHM_FAST</a>;</div>
<div class="line"> </div>
<div class="line"><a class="code hl_class" href="class_d3_d12_m_a_1_1_defragmentation_context.html">D3D12MA::DefragmentationContext</a>* defragCtx;</div>
<div class="line">allocator-&gt;BeginDefragmentation(&amp;defragDesc, &amp;defragCtx);</div>
<div class="line"> </div>
<div class="line"><span class="keywordflow">for</span>(;;)</div>
<div class="line">{</div>
<div class="line"> <a class="code hl_struct" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html">D3D12MA::DEFRAGMENTATION_PASS_MOVE_INFO</a> pass;</div>
<div class="line"> HRESULT hr = defragCtx-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_defragmentation_context.html#a1606c015d02edc094bb246986159d592">BeginPass</a>(&amp;pass);</div>
<div class="line"> <span class="keywordflow">if</span>(hr == S_OK)</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span>(hr != S_FALSE)</div>
<div class="line"> <span class="comment">// Handle error...</span></div>
<div class="line"> </div>
<div class="line"> <span class="keywordflow">for</span>(UINT i = 0; i &lt; pass.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a8df22d990c318d82fe9fcc5f04132c04">MoveCount</a>; ++i)</div>
<div class="line"> {</div>
<div class="line"> <span class="comment">// Inspect pass.pMoves[i].pSrcAllocation, identify what buffer/texture it represents.</span></div>
<div class="line"> MyEngineResourceData* resData = (MyEngineResourceData*)pMoves[i].pSrcAllocation-&gt;GetPrivateData();</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Recreate this buffer/texture as placed at pass.pMoves[i].pDstTmpAllocation.</span></div>
<div class="line"> D3D12_RESOURCE_DESC resDesc = ...</div>
<div class="line"> ID3D12Resource* newRes;</div>
<div class="line"> hr = device-&gt;CreatePlacedResource(</div>
<div class="line"> pass.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4">pMoves</a>[i].<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#aec0c7f51ccc870c6a88af7c7390d8eda">pDstTmpAllocation</a>-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_allocation.html#adca8d5a82bed492fe7265fcda6e53da2">GetHeap</a>(),</div>
<div class="line"> pass.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4">pMoves</a>[i].<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#aec0c7f51ccc870c6a88af7c7390d8eda">pDstTmpAllocation</a>-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_allocation.html#a47be9557d441797b65de177a3d5cdf60">GetOffset</a>(), &amp;resDesc,</div>
<div class="line"> D3D12_RESOURCE_STATE_COPY_DEST, NULL, IID_PPV_ARGS(&amp;newRes));</div>
<div class="line"> <span class="comment">// Check hr...</span></div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Store new resource in the pDstTmpAllocation.</span></div>
<div class="line"> pass.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4">pMoves</a>[i].<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#aec0c7f51ccc870c6a88af7c7390d8eda">pDstTmpAllocation</a>-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_allocation.html#a414a088c22bae0f29b1038f5f9346d14">SetResource</a>(newRes);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Copy its content to the new place.</span></div>
<div class="line"> cmdList-&gt;CopyResource(</div>
<div class="line"> pass.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4">pMoves</a>[i].<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#aec0c7f51ccc870c6a88af7c7390d8eda">pDstTmpAllocation</a>-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_allocation.html#ad00308118252f82d8f803c623c67bf18">GetResource</a>(),</div>
<div class="line"> pass.<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4">pMoves</a>[i].<a class="code hl_variable" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#a4946b874c958a71c21fac25b515cf5f7">pSrcAllocation</a>-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_allocation.html#ad00308118252f82d8f803c623c67bf18">GetResource</a>());</div>
<div class="line"> }</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Make sure the copy commands finished executing.</span></div>
<div class="line"> cmdQueue-&gt;ExecuteCommandLists(...);</div>
<div class="line"> <span class="comment">// ...</span></div>
<div class="line"> WaitForSingleObject(fenceEvent, INFINITE);</div>
<div class="line"> </div>
<div class="line"> <span class="comment">// Update appropriate descriptors to point to the new places...</span></div>
<div class="line"> </div>
<div class="line"> hr = defragCtx-&gt;<a class="code hl_function" href="class_d3_d12_m_a_1_1_defragmentation_context.html#aad502ba70d6dadaeee37703fd8bf90ae">EndPass</a>(&amp;pass);</div>
<div class="line"> <span class="keywordflow">if</span>(hr == S_OK)</div>
<div class="line"> <span class="keywordflow">break</span>;</div>
<div class="line"> <span class="keywordflow">else</span> <span class="keywordflow">if</span>(hr != S_FALSE)</div>
<div class="line"> <span class="comment">// Handle error...</span></div>
<div class="line">}</div>
<div class="line"> </div>
<div class="line">defragCtx-&gt;Release();</div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_allocation_html_a414a088c22bae0f29b1038f5f9346d14"><div class="ttname"><a href="class_d3_d12_m_a_1_1_allocation.html#a414a088c22bae0f29b1038f5f9346d14">D3D12MA::Allocation::SetResource</a></div><div class="ttdeci">void SetResource(ID3D12Resource *pResource)</div><div class="ttdoc">Releases the resource currently pointed by the allocation (if any), sets it to new one,...</div></div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_allocation_html_a47be9557d441797b65de177a3d5cdf60"><div class="ttname"><a href="class_d3_d12_m_a_1_1_allocation.html#a47be9557d441797b65de177a3d5cdf60">D3D12MA::Allocation::GetOffset</a></div><div class="ttdeci">UINT64 GetOffset() const</div><div class="ttdoc">Returns offset in bytes from the start of memory heap.</div></div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_allocation_html_ad00308118252f82d8f803c623c67bf18"><div class="ttname"><a href="class_d3_d12_m_a_1_1_allocation.html#ad00308118252f82d8f803c623c67bf18">D3D12MA::Allocation::GetResource</a></div><div class="ttdeci">ID3D12Resource * GetResource() const</div><div class="ttdoc">Returns D3D12 resource associated with this object.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:491</div></div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_allocation_html_adca8d5a82bed492fe7265fcda6e53da2"><div class="ttname"><a href="class_d3_d12_m_a_1_1_allocation.html#adca8d5a82bed492fe7265fcda6e53da2">D3D12MA::Allocation::GetHeap</a></div><div class="ttdeci">ID3D12Heap * GetHeap() const</div><div class="ttdoc">Returns memory heap that the resource is created in.</div></div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_defragmentation_context_html"><div class="ttname"><a href="class_d3_d12_m_a_1_1_defragmentation_context.html">D3D12MA::DefragmentationContext</a></div><div class="ttdoc">Represents defragmentation process in progress.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:770</div></div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_defragmentation_context_html_a1606c015d02edc094bb246986159d592"><div class="ttname"><a href="class_d3_d12_m_a_1_1_defragmentation_context.html#a1606c015d02edc094bb246986159d592">D3D12MA::DefragmentationContext::BeginPass</a></div><div class="ttdeci">HRESULT BeginPass(DEFRAGMENTATION_PASS_MOVE_INFO *pPassInfo)</div><div class="ttdoc">Starts single defragmentation pass.</div></div>
<div class="ttc" id="aclass_d3_d12_m_a_1_1_defragmentation_context_html_aad502ba70d6dadaeee37703fd8bf90ae"><div class="ttname"><a href="class_d3_d12_m_a_1_1_defragmentation_context.html#aad502ba70d6dadaeee37703fd8bf90ae">D3D12MA::DefragmentationContext::EndPass</a></div><div class="ttdeci">HRESULT EndPass(DEFRAGMENTATION_PASS_MOVE_INFO *pPassInfo)</div><div class="ttdoc">Ends single defragmentation pass.</div></div>
<div class="ttc" id="anamespace_d3_d12_m_a_html_ab433989212ee6736bf9d63c5bc565df4a9d0301a81136ca79e3ba52542c6d2e13"><div class="ttname"><a href="namespace_d3_d12_m_a.html#ab433989212ee6736bf9d63c5bc565df4a9d0301a81136ca79e3ba52542c6d2e13">D3D12MA::DEFRAGMENTATION_FLAG_ALGORITHM_FAST</a></div><div class="ttdeci">@ DEFRAGMENTATION_FLAG_ALGORITHM_FAST</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:650</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c_html"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c.html">D3D12MA::DEFRAGMENTATION_DESC</a></div><div class="ttdoc">Parameters for defragmentation.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:672</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c_html_aac9a357e679a0afa9ab84cd06057630b"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c.html#aac9a357e679a0afa9ab84cd06057630b">D3D12MA::DEFRAGMENTATION_DESC::Flags</a></div><div class="ttdeci">DEFRAGMENTATION_FLAGS Flags</div><div class="ttdoc">Flags.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:674</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e_html_a4946b874c958a71c21fac25b515cf5f7"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#a4946b874c958a71c21fac25b515cf5f7">D3D12MA::DEFRAGMENTATION_MOVE::pSrcAllocation</a></div><div class="ttdeci">Allocation * pSrcAllocation</div><div class="ttdoc">Allocation that should be moved.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:708</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e_html_aec0c7f51ccc870c6a88af7c7390d8eda"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___m_o_v_e.html#aec0c7f51ccc870c6a88af7c7390d8eda">D3D12MA::DEFRAGMENTATION_MOVE::pDstTmpAllocation</a></div><div class="ttdeci">Allocation * pDstTmpAllocation</div><div class="ttdoc">Temporary allocation pointing to destination memory that will replace pSrcAllocation.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:716</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o_html"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html">D3D12MA::DEFRAGMENTATION_PASS_MOVE_INFO</a></div><div class="ttdoc">Parameters for incremental defragmentation steps.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:724</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o_html_a719fbdaae54251759605c41baeb24dc4"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4">D3D12MA::DEFRAGMENTATION_PASS_MOVE_INFO::pMoves</a></div><div class="ttdeci">DEFRAGMENTATION_MOVE * pMoves</div><div class="ttdoc">Array of moves to be performed by the user in the current defragmentation pass.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:748</div></div>
<div class="ttc" id="astruct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o_html_a8df22d990c318d82fe9fcc5f04132c04"><div class="ttname"><a href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a8df22d990c318d82fe9fcc5f04132c04">D3D12MA::DEFRAGMENTATION_PASS_MOVE_INFO::MoveCount</a></div><div class="ttdeci">UINT32 MoveCount</div><div class="ttdoc">Number of elements in the pMoves array.</div><div class="ttdef"><b>Definition:</b> D3D12MemAlloc.h:726</div></div>
</div><!-- fragment --><p >Although functions like <a class="el" href="class_d3_d12_m_a_1_1_allocator.html#aa37d6b9fe8ea0864f7a35b9d68e8345a" title="Allocates memory and creates a D3D12 resource (buffer or texture). This is the main allocation functi...">D3D12MA::Allocator::CreateResource()</a> create an allocation and a buffer/texture at once, these are just a shortcut for allocating memory and creating a placed resource. Defragmentation works on memory allocations only. You must handle the rest manually. Defragmentation is an iterative process that should repreat "passes" as long as related functions return <code>S_FALSE</code> not <code>S_OK</code>. In each pass:</p>
<ol type="1">
<li><a class="el" href="class_d3_d12_m_a_1_1_defragmentation_context.html#a1606c015d02edc094bb246986159d592" title="Starts single defragmentation pass.">D3D12MA::DefragmentationContext::BeginPass()</a> function call:<ul>
<li>Calculates and returns the list of allocations to be moved in this pass. Note this can be a time-consuming process.</li>
<li>Reserves destination memory for them by creating temporary destination allocations that you can query for their <code>ID3D12Heap</code> + offset using methods like <a class="el" href="class_d3_d12_m_a_1_1_allocation.html#adca8d5a82bed492fe7265fcda6e53da2" title="Returns memory heap that the resource is created in.">D3D12MA::Allocation::GetHeap()</a>.</li>
</ul>
</li>
<li>Inside the pass, <b>you should</b>:<ul>
<li>Inspect the returned list of allocations to be moved.</li>
<li>Create new buffers/textures as placed at the returned destination temporary allocations.</li>
<li>Copy data from source to destination resources if necessary.</li>
<li>Store the pointer to the new resource in the temporary destination allocation.</li>
</ul>
</li>
<li><a class="el" href="class_d3_d12_m_a_1_1_defragmentation_context.html#aad502ba70d6dadaeee37703fd8bf90ae" title="Ends single defragmentation pass.">D3D12MA::DefragmentationContext::EndPass()</a> function call:<ul>
<li>Frees the source memory reserved for the allocations that are moved.</li>
<li>Modifies source <a class="el" href="class_d3_d12_m_a_1_1_allocation.html" title="Represents single memory allocation.">D3D12MA::Allocation</a> objects that are moved to point to the destination reserved memory and destination resource, while source resource is released.</li>
<li>Frees <code>ID3D12Heap</code> blocks that became empty.</li>
</ul>
</li>
</ol>
<p >Defragmentation algorithm tries to move all suitable allocations. You can, however, refuse to move some of them inside a defragmentation pass, by setting <code>pass.pMoves[i].Operation</code> to <a class="el" href="namespace_d3_d12_m_a.html#a82bb787a69699a877b4166789a30e602abefe270a1803998dda3f8e01ec3a4ad6" title="Set this value if you cannot move the allocation. New place reserved at pDstTmpAllocation will be fre...">D3D12MA::DEFRAGMENTATION_MOVE_OPERATION_IGNORE</a>. This is not recommended and may result in suboptimal packing of the allocations after defragmentation. If you cannot ensure any allocation can be moved, it is better to keep movable allocations separate in a custom pool.</p>
<p >Inside a pass, for each allocation that should be moved:</p>
<ul>
<li>You should copy its data from the source to the destination place by calling e.g. <code>CopyResource()</code>.<ul>
<li>You need to make sure these commands finished executing before the source buffers/textures are released by <a class="el" href="class_d3_d12_m_a_1_1_defragmentation_context.html#aad502ba70d6dadaeee37703fd8bf90ae" title="Ends single defragmentation pass.">D3D12MA::DefragmentationContext::EndPass()</a>.</li>
</ul>
</li>
<li>If a resource doesn't contain any meaningful data, e.g. it is a transient render-target texture to be cleared, filled, and used temporarily in each rendering frame, you can just recreate this texture without copying its data.</li>
<li>If the resource is in <code>D3D12_HEAP_TYPE_READBACK</code> memory, you can copy its data on the CPU using <code>memcpy()</code>.</li>
<li>If you cannot move the allocation, you can set <code>pass.pMoves[i].Operation</code> to <a class="el" href="namespace_d3_d12_m_a.html#a82bb787a69699a877b4166789a30e602abefe270a1803998dda3f8e01ec3a4ad6" title="Set this value if you cannot move the allocation. New place reserved at pDstTmpAllocation will be fre...">D3D12MA::DEFRAGMENTATION_MOVE_OPERATION_IGNORE</a>. This will cancel the move.<ul>
<li><a class="el" href="class_d3_d12_m_a_1_1_defragmentation_context.html#aad502ba70d6dadaeee37703fd8bf90ae" title="Ends single defragmentation pass.">D3D12MA::DefragmentationContext::EndPass()</a> will then free the destination memory not the source memory of the allocation, leaving it unchanged.</li>
</ul>
</li>
<li>If you decide the allocation is unimportant and can be destroyed instead of moved (e.g. it wasn't used for long time), you can set <code>pass.pMoves[i].Operation</code> to <a class="el" href="namespace_d3_d12_m_a.html#a82bb787a69699a877b4166789a30e602aa2143507d723de458c2ed94e143ac242" title="Set this value if you decide to abandon the allocation and you destroyed the resource....">D3D12MA::DEFRAGMENTATION_MOVE_OPERATION_DESTROY</a>.<ul>
<li><a class="el" href="class_d3_d12_m_a_1_1_defragmentation_context.html#aad502ba70d6dadaeee37703fd8bf90ae" title="Ends single defragmentation pass.">D3D12MA::DefragmentationContext::EndPass()</a> will then free both source and destination memory, and will destroy the source <a class="el" href="class_d3_d12_m_a_1_1_allocation.html" title="Represents single memory allocation.">D3D12MA::Allocation</a> object.</li>
</ul>
</li>
</ul>
<p >You can defragment a specific custom pool by calling <a class="el" href="class_d3_d12_m_a_1_1_pool.html#adc87bb49c192de8f5a9ca0484c499575" title="Begins defragmentation process of the current pool.">D3D12MA::Pool::BeginDefragmentation</a> or all the default pools by calling <a class="el" href="class_d3_d12_m_a_1_1_allocator.html#a08e1468f1dbb63ce3bf6680e592b2143" title="Begins defragmentation process of the default pools.">D3D12MA::Allocator::BeginDefragmentation</a> (like in the example above).</p>
<p >Defragmentation is always performed in each pool separately. Allocations are never moved between different heap types. The size of the destination memory reserved for a moved allocation is the same as the original one. Alignment of an allocation as it was determined using <code>GetResourceAllocationInfo()</code> is also respected after defragmentation. Buffers/textures should be recreated with the same <code>D3D12_RESOURCE_DESC</code> parameters as the original ones.</p>
<p >You can perform the defragmentation incrementally to limit the number of allocations and bytes to be moved in each pass, e.g. to call it in sync with render frames and not to experience too big hitches. See members: <a class="el" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c.html#ad8d224e0687a35898970d0a5688c6343" title="Maximum numbers of bytes that can be copied during single pass, while moving allocations to different...">D3D12MA::DEFRAGMENTATION_DESC::MaxBytesPerPass</a>, <a class="el" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___d_e_s_c.html#a83bfb404f387863eafdd6703483aed89" title="Maximum number of allocations that can be moved during single pass to a different place.">D3D12MA::DEFRAGMENTATION_DESC::MaxAllocationsPerPass</a>.</p>
<p ><b>Thread safety:</b> It is safe to perform the defragmentation asynchronously to render frames and other Direct3D 12 and D3D12MA usage, possibly from multiple threads, with the exception that allocations returned in <a class="el" href="struct_d3_d12_m_a_1_1_d_e_f_r_a_g_m_e_n_t_a_t_i_o_n___p_a_s_s___m_o_v_e___i_n_f_o.html#a719fbdaae54251759605c41baeb24dc4" title="Array of moves to be performed by the user in the current defragmentation pass.">D3D12MA::DEFRAGMENTATION_PASS_MOVE_INFO::pMoves</a> shouldn't be released until the defragmentation pass is ended. During the call to <a class="el" href="class_d3_d12_m_a_1_1_defragmentation_context.html#a1606c015d02edc094bb246986159d592" title="Starts single defragmentation pass.">D3D12MA::DefragmentationContext::BeginPass()</a>, any operations on the memory pool affected by the defragmentation are blocked by a mutex.</p>
<p >What it means in practice is that you shouldn't free any allocations from the defragmented pool since the moment a call to <code>BeginPass</code> begins. Otherwise, a thread performing the <code>allocation-&gt;Release()</code> would block for the time <code>BeginPass</code> executes and then free the allocation when it finishes, while the allocation could have ended up on the list of allocations to move. A solution to freeing allocations during defragmentation is to find such allocation on the list <code>pass.pMoves[i]</code> and set its operation to <a class="el" href="namespace_d3_d12_m_a.html#a82bb787a69699a877b4166789a30e602aa2143507d723de458c2ed94e143ac242" title="Set this value if you decide to abandon the allocation and you destroyed the resource....">D3D12MA::DEFRAGMENTATION_MOVE_OPERATION_DESTROY</a> instead of calling <code>allocation-&gt;Release()</code>, or simply deferring the release to the time after defragmentation finished.</p>
<p ><b>Mapping</b> is out of scope of this library and so it is not preserved after an allocation is moved during defragmentation. You need to map the new resource yourself if needed.</p>
<dl class="section note"><dt>Note</dt><dd>Defragmentation is not supported in custom pools created with <a class="el" href="namespace_d3_d12_m_a.html#a919d8545365d6b7209a964f2b99936d1aa37a0103f511954ea42a1d0bba286b6a" title="Enables alternative, linear allocation algorithm in this pool.">D3D12MA::POOL_FLAG_ALGORITHM_LINEAR</a>. </dd></dl>
</div></div><!-- contents -->
</div><!-- PageDoc -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by&#160;<a href="https://www.doxygen.org/index.html"><img class="footer" src="doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3
</small></address>
</body>
</html>