VulkanCommandBufferPool.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using Silk.NET.Vulkan;
  4. namespace Ryujinx.Ava.Ui.Vulkan
  5. {
  6. internal class VulkanCommandBufferPool : IDisposable
  7. {
  8. private readonly VulkanDevice _device;
  9. private readonly CommandPool _commandPool;
  10. private readonly List<VulkanCommandBuffer> _usedCommandBuffers = new();
  11. private readonly object _lock = new object();
  12. public unsafe VulkanCommandBufferPool(VulkanDevice device, VulkanPhysicalDevice physicalDevice)
  13. {
  14. _device = device;
  15. var commandPoolCreateInfo = new CommandPoolCreateInfo
  16. {
  17. SType = StructureType.CommandPoolCreateInfo,
  18. Flags = CommandPoolCreateFlags.CommandPoolCreateResetCommandBufferBit,
  19. QueueFamilyIndex = physicalDevice.QueueFamilyIndex
  20. };
  21. device.Api.CreateCommandPool(_device.InternalHandle, commandPoolCreateInfo, null, out _commandPool)
  22. .ThrowOnError();
  23. }
  24. private CommandBuffer AllocateCommandBuffer()
  25. {
  26. var commandBufferAllocateInfo = new CommandBufferAllocateInfo
  27. {
  28. SType = StructureType.CommandBufferAllocateInfo,
  29. CommandPool = _commandPool,
  30. CommandBufferCount = 1,
  31. Level = CommandBufferLevel.Primary
  32. };
  33. lock (_lock)
  34. {
  35. _device.Api.AllocateCommandBuffers(_device.InternalHandle, commandBufferAllocateInfo, out var commandBuffer);
  36. return commandBuffer;
  37. }
  38. }
  39. public VulkanCommandBuffer CreateCommandBuffer()
  40. {
  41. return new(_device, this);
  42. }
  43. public void FreeUsedCommandBuffers()
  44. {
  45. lock (_lock)
  46. {
  47. foreach (var usedCommandBuffer in _usedCommandBuffers)
  48. {
  49. usedCommandBuffer.Dispose();
  50. }
  51. _usedCommandBuffers.Clear();
  52. }
  53. }
  54. private void DisposeCommandBuffer(VulkanCommandBuffer commandBuffer)
  55. {
  56. lock (_lock)
  57. {
  58. _usedCommandBuffers.Add(commandBuffer);
  59. }
  60. }
  61. public void Dispose()
  62. {
  63. lock (_lock)
  64. {
  65. FreeUsedCommandBuffers();
  66. _device.Api.DestroyCommandPool(_device.InternalHandle, _commandPool, Span<AllocationCallbacks>.Empty);
  67. }
  68. }
  69. public class VulkanCommandBuffer : IDisposable
  70. {
  71. private readonly VulkanCommandBufferPool _commandBufferPool;
  72. private readonly VulkanDevice _device;
  73. private readonly Fence _fence;
  74. private bool _hasEnded;
  75. private bool _hasStarted;
  76. private bool _isDisposed;
  77. private object _lock = new object();
  78. public IntPtr Handle => InternalHandle.Handle;
  79. internal CommandBuffer InternalHandle { get; }
  80. internal unsafe VulkanCommandBuffer(VulkanDevice device, VulkanCommandBufferPool commandBufferPool)
  81. {
  82. _device = device;
  83. _commandBufferPool = commandBufferPool;
  84. InternalHandle = _commandBufferPool.AllocateCommandBuffer();
  85. var fenceCreateInfo = new FenceCreateInfo()
  86. {
  87. SType = StructureType.FenceCreateInfo,
  88. Flags = FenceCreateFlags.FenceCreateSignaledBit
  89. };
  90. device.Api.CreateFence(device.InternalHandle, fenceCreateInfo, null, out _fence);
  91. }
  92. public void WaitForFence()
  93. {
  94. if (_isDisposed)
  95. {
  96. return;
  97. }
  98. lock (_lock)
  99. {
  100. if (!_isDisposed)
  101. {
  102. _device.Api.WaitForFences(_device.InternalHandle, 1, _fence, true, ulong.MaxValue);
  103. }
  104. }
  105. }
  106. public void BeginRecording()
  107. {
  108. if (!_hasStarted)
  109. {
  110. _hasStarted = true;
  111. var beginInfo = new CommandBufferBeginInfo
  112. {
  113. SType = StructureType.CommandBufferBeginInfo,
  114. Flags = CommandBufferUsageFlags.CommandBufferUsageOneTimeSubmitBit
  115. };
  116. _device.Api.BeginCommandBuffer(InternalHandle, beginInfo);
  117. }
  118. }
  119. public void EndRecording()
  120. {
  121. if (_hasStarted && !_hasEnded)
  122. {
  123. _hasEnded = true;
  124. _device.Api.EndCommandBuffer(InternalHandle);
  125. }
  126. }
  127. public void Submit()
  128. {
  129. Submit(null, null, null, _fence);
  130. }
  131. public unsafe void Submit(
  132. ReadOnlySpan<Semaphore> waitSemaphores,
  133. ReadOnlySpan<PipelineStageFlags> waitDstStageMask,
  134. ReadOnlySpan<Semaphore> signalSemaphores,
  135. Fence? fence = null)
  136. {
  137. EndRecording();
  138. if (!fence.HasValue)
  139. {
  140. fence = _fence;
  141. }
  142. fixed (Semaphore* pWaitSemaphores = waitSemaphores, pSignalSemaphores = signalSemaphores)
  143. {
  144. fixed (PipelineStageFlags* pWaitDstStageMask = waitDstStageMask)
  145. {
  146. var commandBuffer = InternalHandle;
  147. var submitInfo = new SubmitInfo
  148. {
  149. SType = StructureType.SubmitInfo,
  150. WaitSemaphoreCount = waitSemaphores != null ? (uint)waitSemaphores.Length : 0,
  151. PWaitSemaphores = pWaitSemaphores,
  152. PWaitDstStageMask = pWaitDstStageMask,
  153. CommandBufferCount = 1,
  154. PCommandBuffers = &commandBuffer,
  155. SignalSemaphoreCount = signalSemaphores != null ? (uint)signalSemaphores.Length : 0,
  156. PSignalSemaphores = pSignalSemaphores,
  157. };
  158. _device.Api.ResetFences(_device.InternalHandle, 1, fence.Value);
  159. _device.Submit(submitInfo, fence.Value);
  160. }
  161. }
  162. _commandBufferPool.DisposeCommandBuffer(this);
  163. }
  164. public void Dispose()
  165. {
  166. lock (_lock)
  167. {
  168. if (!_isDisposed)
  169. {
  170. _isDisposed = true;
  171. _device.Api.WaitForFences(_device.InternalHandle, 1, _fence, true, ulong.MaxValue);
  172. _device.Api.FreeCommandBuffers(_device.InternalHandle, _commandBufferPool._commandPool, 1, InternalHandle);
  173. _device.Api.DestroyFence(_device.InternalHandle, _fence, Span<AllocationCallbacks>.Empty);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }