MemoryAllocatorBlockList.cs 9.4 KB

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