CommandBufferPool.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. using SharpMetal.Metal;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Runtime.Versioning;
  6. using System.Threading;
  7. namespace Ryujinx.Graphics.Metal
  8. {
  9. [SupportedOSPlatform("macos")]
  10. class CommandBufferPool : IDisposable
  11. {
  12. public const int MaxCommandBuffers = 16;
  13. private readonly int _totalCommandBuffers;
  14. private readonly int _totalCommandBuffersMask;
  15. private readonly MTLCommandQueue _queue;
  16. private readonly Thread _owner;
  17. private IEncoderFactory _defaultEncoderFactory;
  18. public bool OwnedByCurrentThread => _owner == Thread.CurrentThread;
  19. [SupportedOSPlatform("macos")]
  20. private struct ReservedCommandBuffer
  21. {
  22. public bool InUse;
  23. public bool InConsumption;
  24. public int SubmissionCount;
  25. public MTLCommandBuffer CommandBuffer;
  26. public CommandBufferEncoder Encoders;
  27. public FenceHolder Fence;
  28. public List<IAuto> Dependants;
  29. public List<MultiFenceHolder> Waitables;
  30. public void Use(MTLCommandQueue queue, IEncoderFactory stateManager)
  31. {
  32. MTLCommandBufferDescriptor descriptor = new();
  33. #if DEBUG
  34. descriptor.ErrorOptions = MTLCommandBufferErrorOption.EncoderExecutionStatus;
  35. #endif
  36. CommandBuffer = queue.CommandBuffer(descriptor);
  37. Fence = new FenceHolder(CommandBuffer);
  38. Encoders.Initialize(CommandBuffer, stateManager);
  39. InUse = true;
  40. }
  41. public void Initialize()
  42. {
  43. Dependants = new List<IAuto>();
  44. Waitables = new List<MultiFenceHolder>();
  45. Encoders = new CommandBufferEncoder();
  46. }
  47. }
  48. private readonly ReservedCommandBuffer[] _commandBuffers;
  49. private readonly int[] _queuedIndexes;
  50. private int _queuedIndexesPtr;
  51. private int _queuedCount;
  52. private int _inUseCount;
  53. public CommandBufferPool(MTLCommandQueue queue, bool isLight = false)
  54. {
  55. _queue = queue;
  56. _owner = Thread.CurrentThread;
  57. _totalCommandBuffers = isLight ? 2 : MaxCommandBuffers;
  58. _totalCommandBuffersMask = _totalCommandBuffers - 1;
  59. _commandBuffers = new ReservedCommandBuffer[_totalCommandBuffers];
  60. _queuedIndexes = new int[_totalCommandBuffers];
  61. _queuedIndexesPtr = 0;
  62. _queuedCount = 0;
  63. }
  64. public void Initialize(IEncoderFactory encoderFactory)
  65. {
  66. _defaultEncoderFactory = encoderFactory;
  67. for (int i = 0; i < _totalCommandBuffers; i++)
  68. {
  69. _commandBuffers[i].Initialize();
  70. WaitAndDecrementRef(i);
  71. }
  72. }
  73. public void AddDependant(int cbIndex, IAuto dependant)
  74. {
  75. dependant.IncrementReferenceCount();
  76. _commandBuffers[cbIndex].Dependants.Add(dependant);
  77. }
  78. public void AddWaitable(MultiFenceHolder waitable)
  79. {
  80. lock (_commandBuffers)
  81. {
  82. for (int i = 0; i < _totalCommandBuffers; i++)
  83. {
  84. ref var entry = ref _commandBuffers[i];
  85. if (entry.InConsumption)
  86. {
  87. AddWaitable(i, waitable);
  88. }
  89. }
  90. }
  91. }
  92. public void AddInUseWaitable(MultiFenceHolder waitable)
  93. {
  94. lock (_commandBuffers)
  95. {
  96. for (int i = 0; i < _totalCommandBuffers; i++)
  97. {
  98. ref var entry = ref _commandBuffers[i];
  99. if (entry.InUse)
  100. {
  101. AddWaitable(i, waitable);
  102. }
  103. }
  104. }
  105. }
  106. public void AddWaitable(int cbIndex, MultiFenceHolder waitable)
  107. {
  108. ref var entry = ref _commandBuffers[cbIndex];
  109. if (waitable.AddFence(cbIndex, entry.Fence))
  110. {
  111. entry.Waitables.Add(waitable);
  112. }
  113. }
  114. public bool IsFenceOnRentedCommandBuffer(FenceHolder fence)
  115. {
  116. lock (_commandBuffers)
  117. {
  118. for (int i = 0; i < _totalCommandBuffers; i++)
  119. {
  120. ref var entry = ref _commandBuffers[i];
  121. if (entry.InUse && entry.Fence == fence)
  122. {
  123. return true;
  124. }
  125. }
  126. }
  127. return false;
  128. }
  129. public FenceHolder GetFence(int cbIndex)
  130. {
  131. return _commandBuffers[cbIndex].Fence;
  132. }
  133. public int GetSubmissionCount(int cbIndex)
  134. {
  135. return _commandBuffers[cbIndex].SubmissionCount;
  136. }
  137. private int FreeConsumed(bool wait)
  138. {
  139. int freeEntry = 0;
  140. while (_queuedCount > 0)
  141. {
  142. int index = _queuedIndexes[_queuedIndexesPtr];
  143. ref var entry = ref _commandBuffers[index];
  144. if (wait || !entry.InConsumption || entry.Fence.IsSignaled())
  145. {
  146. WaitAndDecrementRef(index);
  147. wait = false;
  148. freeEntry = index;
  149. _queuedCount--;
  150. _queuedIndexesPtr = (_queuedIndexesPtr + 1) % _totalCommandBuffers;
  151. }
  152. else
  153. {
  154. break;
  155. }
  156. }
  157. return freeEntry;
  158. }
  159. public CommandBufferScoped ReturnAndRent(CommandBufferScoped cbs)
  160. {
  161. Return(cbs);
  162. return Rent();
  163. }
  164. public CommandBufferScoped Rent()
  165. {
  166. lock (_commandBuffers)
  167. {
  168. int cursor = FreeConsumed(_inUseCount + _queuedCount == _totalCommandBuffers);
  169. for (int i = 0; i < _totalCommandBuffers; i++)
  170. {
  171. ref var entry = ref _commandBuffers[cursor];
  172. if (!entry.InUse && !entry.InConsumption)
  173. {
  174. entry.Use(_queue, _defaultEncoderFactory);
  175. _inUseCount++;
  176. return new CommandBufferScoped(this, entry.CommandBuffer, entry.Encoders, cursor);
  177. }
  178. cursor = (cursor + 1) & _totalCommandBuffersMask;
  179. }
  180. }
  181. throw new InvalidOperationException($"Out of command buffers (In use: {_inUseCount}, queued: {_queuedCount}, total: {_totalCommandBuffers})");
  182. }
  183. public void Return(CommandBufferScoped cbs)
  184. {
  185. // Ensure the encoder is committed.
  186. cbs.Encoders.EndCurrentPass();
  187. lock (_commandBuffers)
  188. {
  189. int cbIndex = cbs.CommandBufferIndex;
  190. ref var entry = ref _commandBuffers[cbIndex];
  191. Debug.Assert(entry.InUse);
  192. Debug.Assert(entry.CommandBuffer.NativePtr == cbs.CommandBuffer.NativePtr);
  193. entry.InUse = false;
  194. entry.InConsumption = true;
  195. entry.SubmissionCount++;
  196. _inUseCount--;
  197. var commandBuffer = entry.CommandBuffer;
  198. commandBuffer.Commit();
  199. int ptr = (_queuedIndexesPtr + _queuedCount) % _totalCommandBuffers;
  200. _queuedIndexes[ptr] = cbIndex;
  201. _queuedCount++;
  202. }
  203. }
  204. private void WaitAndDecrementRef(int cbIndex)
  205. {
  206. ref var entry = ref _commandBuffers[cbIndex];
  207. if (entry.InConsumption)
  208. {
  209. entry.Fence.Wait();
  210. entry.InConsumption = false;
  211. }
  212. foreach (var dependant in entry.Dependants)
  213. {
  214. dependant.DecrementReferenceCount(cbIndex);
  215. }
  216. foreach (var waitable in entry.Waitables)
  217. {
  218. waitable.RemoveFence(cbIndex);
  219. waitable.RemoveBufferUses(cbIndex);
  220. }
  221. entry.Dependants.Clear();
  222. entry.Waitables.Clear();
  223. entry.Fence?.Dispose();
  224. }
  225. public void Dispose()
  226. {
  227. for (int i = 0; i < _totalCommandBuffers; i++)
  228. {
  229. WaitAndDecrementRef(i);
  230. }
  231. }
  232. }
  233. }