MemoryAllocatorBlockList.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using Ryujinx.Common;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. namespace Ryujinx.Graphics.Vulkan
  7. {
  8. class MemoryAllocatorBlockList : IDisposable
  9. {
  10. private const ulong InvalidOffset = ulong.MaxValue;
  11. public class Block : IComparable<Block>
  12. {
  13. public DeviceMemory Memory { get; private set; }
  14. public IntPtr HostPointer { get; private set; }
  15. public ulong Size { get; }
  16. public bool Mapped => HostPointer != IntPtr.Zero;
  17. private struct Range : IComparable<Range>
  18. {
  19. public ulong Offset { get; }
  20. public ulong Size { get; }
  21. public Range(ulong offset, ulong size)
  22. {
  23. Offset = offset;
  24. Size = size;
  25. }
  26. public int CompareTo(Range other)
  27. {
  28. return Offset.CompareTo(other.Offset);
  29. }
  30. }
  31. private readonly List<Range> _freeRanges;
  32. public Block(DeviceMemory memory, IntPtr hostPointer, ulong size)
  33. {
  34. Memory = memory;
  35. HostPointer = hostPointer;
  36. Size = size;
  37. _freeRanges = new List<Range>
  38. {
  39. new Range(0, size)
  40. };
  41. }
  42. public ulong Allocate(ulong size, ulong alignment)
  43. {
  44. for (int i = 0; i < _freeRanges.Count; i++)
  45. {
  46. var range = _freeRanges[i];
  47. ulong alignedOffset = BitUtils.AlignUp(range.Offset, (int)alignment);
  48. ulong sizeDelta = alignedOffset - range.Offset;
  49. ulong usableSize = range.Size - sizeDelta;
  50. if (sizeDelta < range.Size && usableSize >= size)
  51. {
  52. _freeRanges.RemoveAt(i);
  53. if (sizeDelta != 0)
  54. {
  55. InsertFreeRange(range.Offset, sizeDelta);
  56. }
  57. ulong endOffset = range.Offset + range.Size;
  58. ulong remainingSize = endOffset - (alignedOffset + size);
  59. if (remainingSize != 0)
  60. {
  61. InsertFreeRange(endOffset - remainingSize, remainingSize);
  62. }
  63. return alignedOffset;
  64. }
  65. }
  66. return InvalidOffset;
  67. }
  68. public void Free(ulong offset, ulong size)
  69. {
  70. InsertFreeRangeComingled(offset, size);
  71. }
  72. private void InsertFreeRange(ulong offset, ulong size)
  73. {
  74. var range = new Range(offset, size);
  75. int index = _freeRanges.BinarySearch(range);
  76. if (index < 0)
  77. {
  78. index = ~index;
  79. }
  80. _freeRanges.Insert(index, range);
  81. }
  82. private void InsertFreeRangeComingled(ulong offset, ulong size)
  83. {
  84. ulong endOffset = offset + size;
  85. var range = new Range(offset, size);
  86. int index = _freeRanges.BinarySearch(range);
  87. if (index < 0)
  88. {
  89. index = ~index;
  90. }
  91. if (index < _freeRanges.Count && _freeRanges[index].Offset == endOffset)
  92. {
  93. endOffset = _freeRanges[index].Offset + _freeRanges[index].Size;
  94. _freeRanges.RemoveAt(index);
  95. }
  96. if (index > 0 && _freeRanges[index - 1].Offset + _freeRanges[index - 1].Size == offset)
  97. {
  98. offset = _freeRanges[index - 1].Offset;
  99. _freeRanges.RemoveAt(--index);
  100. }
  101. range = new Range(offset, endOffset - offset);
  102. _freeRanges.Insert(index, range);
  103. }
  104. public bool IsTotallyFree()
  105. {
  106. if (_freeRanges.Count == 1 && _freeRanges[0].Size == Size)
  107. {
  108. Debug.Assert(_freeRanges[0].Offset == 0);
  109. return true;
  110. }
  111. return false;
  112. }
  113. public int CompareTo(Block other)
  114. {
  115. return Size.CompareTo(other.Size);
  116. }
  117. public unsafe void Destroy(Vk api, Device device)
  118. {
  119. if (Mapped)
  120. {
  121. api.UnmapMemory(device, Memory);
  122. HostPointer = IntPtr.Zero;
  123. }
  124. if (Memory.Handle != 0)
  125. {
  126. api.FreeMemory(device, Memory, null);
  127. Memory = default;
  128. }
  129. }
  130. }
  131. private readonly List<Block> _blocks;
  132. private readonly Vk _api;
  133. private readonly Device _device;
  134. public int MemoryTypeIndex { get; }
  135. private readonly int _blockAlignment;
  136. public MemoryAllocatorBlockList(Vk api, Device device, int memoryTypeIndex, int blockAlignment)
  137. {
  138. _blocks = new List<Block>();
  139. _api = api;
  140. _device = device;
  141. MemoryTypeIndex = memoryTypeIndex;
  142. _blockAlignment = blockAlignment;
  143. }
  144. public unsafe MemoryAllocation Allocate(ulong size, ulong alignment, bool map)
  145. {
  146. // Ensure we have a sane alignment value.
  147. if ((ulong)(int)alignment != alignment || (int)alignment <= 0)
  148. {
  149. throw new ArgumentOutOfRangeException(nameof(alignment), $"Invalid alignment 0x{alignment:X}.");
  150. }
  151. for (int i = 0; i < _blocks.Count; i++)
  152. {
  153. var block = _blocks[i];
  154. if (block.Mapped == map && block.Size >= size)
  155. {
  156. ulong offset = block.Allocate(size, alignment);
  157. if (offset != InvalidOffset)
  158. {
  159. return new MemoryAllocation(this, block, block.Memory, GetHostPointer(block, offset), offset, size);
  160. }
  161. }
  162. }
  163. ulong blockAlignedSize = BitUtils.AlignUp(size, _blockAlignment);
  164. var memoryAllocateInfo = new MemoryAllocateInfo()
  165. {
  166. SType = StructureType.MemoryAllocateInfo,
  167. AllocationSize = blockAlignedSize,
  168. MemoryTypeIndex = (uint)MemoryTypeIndex
  169. };
  170. _api.AllocateMemory(_device, memoryAllocateInfo, null, out var deviceMemory).ThrowOnError();
  171. IntPtr hostPointer = IntPtr.Zero;
  172. if (map)
  173. {
  174. unsafe
  175. {
  176. void* pointer = null;
  177. _api.MapMemory(_device, deviceMemory, 0, blockAlignedSize, 0, ref pointer).ThrowOnError();
  178. hostPointer = (IntPtr)pointer;
  179. }
  180. }
  181. var newBlock = new Block(deviceMemory, hostPointer, blockAlignedSize);
  182. InsertBlock(newBlock);
  183. ulong newBlockOffset = newBlock.Allocate(size, alignment);
  184. Debug.Assert(newBlockOffset != InvalidOffset);
  185. return new MemoryAllocation(this, newBlock, deviceMemory, GetHostPointer(newBlock, newBlockOffset), newBlockOffset, size);
  186. }
  187. private static IntPtr GetHostPointer(Block block, ulong offset)
  188. {
  189. if (block.HostPointer == IntPtr.Zero)
  190. {
  191. return IntPtr.Zero;
  192. }
  193. return (IntPtr)((nuint)(nint)block.HostPointer + offset);
  194. }
  195. public unsafe void Free(Block block, ulong offset, ulong size)
  196. {
  197. block.Free(offset, size);
  198. if (block.IsTotallyFree())
  199. {
  200. for (int i = 0; i < _blocks.Count; i++)
  201. {
  202. if (_blocks[i] == block)
  203. {
  204. _blocks.RemoveAt(i);
  205. break;
  206. }
  207. }
  208. block.Destroy(_api, _device);
  209. }
  210. }
  211. private void InsertBlock(Block block)
  212. {
  213. int index = _blocks.BinarySearch(block);
  214. if (index < 0)
  215. {
  216. index = ~index;
  217. }
  218. _blocks.Insert(index, block);
  219. }
  220. public unsafe void Dispose()
  221. {
  222. for (int i = 0; i < _blocks.Count; i++)
  223. {
  224. _blocks[i].Destroy(_api, _device);
  225. }
  226. }
  227. }
  228. }