Skip to content

Commit 0d601d8

Browse files
Added documentation chapter "Writing custom defragmentation algorithm".
1 parent 638f42d commit 0d601d8

4 files changed

Lines changed: 179 additions & 149 deletions

File tree

docs/html/defragmentation.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,16 @@ <h1><a class="anchor" id="defragmentation_additional_notes"></a>
106106
<p>While using defragmentation, you may experience validation layer warnings, which you just need to ignore. See <a class="el" href="general_considerations.html#general_considerations_validation_layer_warnings">Validation layer warnings</a>.</p>
107107
<p>If you defragment allocations bound to images, these images should be created with <code>VK_IMAGE_CREATE_ALIAS_BIT</code> flag, to make sure that new image created with same parameters and pointing to data copied to another memory region will interpret its contents consistently. Otherwise you may experience corrupted data on some implementations, e.g. due to different pixel swizzling used internally by the graphics driver.</p>
108108
<p>If you defragment allocations bound to images, new images to be bound to new memory region after defragmentation should be created with <code>VK_IMAGE_LAYOUT_PREINITIALIZED</code> and then transitioned to their original layout from before defragmentation using an image memory barrier.</p>
109-
<p>Please don't expect memory to be fully compacted after defragmentation. Algorithms inside are based on some heuristics that try to maximize number of Vulkan memory blocks to make totally empty to release them, as well as to maximimze continuous empty space inside remaining blocks, while minimizing the number and size of allocations that needs to be moved. Some fragmentation may still remain after this call. This is normal. </p>
109+
<p>Please don't expect memory to be fully compacted after defragmentation. Algorithms inside are based on some heuristics that try to maximize number of Vulkan memory blocks to make totally empty to release them, as well as to maximimze continuous empty space inside remaining blocks, while minimizing the number and size of allocations that need to be moved. Some fragmentation may still remain - this is normal.</p>
110+
<h1><a class="anchor" id="defragmentation_custom_algorithm"></a>
111+
Writing custom defragmentation algorithm</h1>
112+
<p>If you want to implement your own, custom defragmentation algorithm, there is infrastructure prepared for that, but it is not exposed through the library API - you need to hack its source code. Here are steps needed to do this:</p>
113+
<ol type="1">
114+
<li>Main thing you need to do is to define your own class derived from base abstract class <code>VmaDefragmentationAlgorithm</code> and implement your version of its pure virtual method. See definition and comments of this class for details.</li>
115+
<li>Your code needs to interact with device memory block metadata. If you need more access to its data than it's provided by its public interface, declare your new class as a friend class e.g. in class <code>VmaBlockMetadata_Generic</code>.</li>
116+
<li>If you want to create a flag that would enable your algorithm or pass some additional flags to configure it, define some enum with such flags and use them in <a class="el" href="struct_vma_defragmentation_info2.html#a53e844ee5633e229cf6daf14b2d9fff9" title="Reserved for future use. Should be 0. ">VmaDefragmentationInfo2::flags</a>.</li>
117+
<li>Modify function <code>VmaBlockVectorDefragmentationContext::Begin</code> to create object of your new class whenever needed. </li>
118+
</ol>
110119
</div></div><!-- contents -->
111120
<!-- start footer part -->
112121
<hr class="footer"/><address class="footer"><small>

docs/html/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ <h1><a class="anchor" id="main_table_of_contents"></a>
109109
<li><a class="el" href="defragmentation.html#defragmentation_cpu">Defragmenting CPU memory</a></li>
110110
<li><a class="el" href="defragmentation.html#defragmentation_gpu">Defragmenting GPU memory</a></li>
111111
<li><a class="el" href="defragmentation.html#defragmentation_additional_notes">Additional notes</a></li>
112+
<li><a class="el" href="defragmentation.html#defragmentation_custom_algorithm">Writing custom allocation algorithm</a></li>
112113
</ul>
113114
</li>
114115
<li><a class="el" href="lost_allocations.html">Lost allocations</a></li>

docs/html/vk__mem__alloc_8h_source.html

Lines changed: 146 additions & 146 deletions
Large diffs are not rendered by default.

src/vk_mem_alloc.h

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Documentation of all members: vk_mem_alloc.h
6565
- [Defragmenting CPU memory](@ref defragmentation_cpu)
6666
- [Defragmenting GPU memory](@ref defragmentation_gpu)
6767
- [Additional notes](@ref defragmentation_additional_notes)
68+
- [Writing custom allocation algorithm](@ref defragmentation_custom_algorithm)
6869
- \subpage lost_allocations
6970
- \subpage statistics
7071
- [Numeric statistics](@ref statistics_numeric_statistics)
@@ -903,7 +904,26 @@ Please don't expect memory to be fully compacted after defragmentation.
903904
Algorithms inside are based on some heuristics that try to maximize number of Vulkan
904905
memory blocks to make totally empty to release them, as well as to maximimze continuous
905906
empty space inside remaining blocks, while minimizing the number and size of allocations that
906-
needs to be moved. Some fragmentation may still remain after this call. This is normal.
907+
need to be moved. Some fragmentation may still remain - this is normal.
908+
909+
\section defragmentation_custom_algorithm Writing custom defragmentation algorithm
910+
911+
If you want to implement your own, custom defragmentation algorithm,
912+
there is infrastructure prepared for that,
913+
but it is not exposed through the library API - you need to hack its source code.
914+
Here are steps needed to do this:
915+
916+
-# Main thing you need to do is to define your own class derived from base abstract
917+
class `VmaDefragmentationAlgorithm` and implement your version of its pure virtual method.
918+
See definition and comments of this class for details.
919+
-# Your code needs to interact with device memory block metadata.
920+
If you need more access to its data than it's provided by its public interface,
921+
declare your new class as a friend class e.g. in class `VmaBlockMetadata_Generic`.
922+
-# If you want to create a flag that would enable your algorithm or pass some additional
923+
flags to configure it, define some enum with such flags and use them in
924+
VmaDefragmentationInfo2::flags.
925+
-# Modify function `VmaBlockVectorDefragmentationContext::Begin` to create object
926+
of your new class whenever needed.
907927

908928

909929
\page lost_allocations Lost allocations
@@ -5844,7 +5864,7 @@ struct VmaPool_T
58445864
Performs defragmentation:
58455865

58465866
- Updates `pBlockVector->m_pMetadata`.
5847-
- Updates allocations by calling ChangeBlockAllocation().
5867+
- Updates allocations by calling ChangeBlockAllocation() or ChangeOffset().
58485868
- Does not move actual data, only returns requested moves as `moves`.
58495869
*/
58505870
class VmaDefragmentationAlgorithm

0 commit comments

Comments
 (0)