VulkanCommandBufferPool.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Input;
  4. using Silk.NET.Vulkan;
  5. using SilkNetDemo;
  6. namespace Avalonia.Vulkan
  7. {
  8. public class VulkanCommandBufferPool : IDisposable
  9. {
  10. private readonly Vk _api;
  11. private readonly Device _device;
  12. private readonly Queue _queue;
  13. private readonly CommandPool _commandPool;
  14. private readonly List<VulkanCommandBuffer> _usedCommandBuffers = new();
  15. private object _lock = new object();
  16. public unsafe VulkanCommandBufferPool(Vk api, Device device, Queue queue, uint queueFamilyIndex)
  17. {
  18. _api = api;
  19. _device = device;
  20. _queue = queue;
  21. var commandPoolCreateInfo = new CommandPoolCreateInfo
  22. {
  23. SType = StructureType.CommandPoolCreateInfo,
  24. Flags = CommandPoolCreateFlags.CommandPoolCreateResetCommandBufferBit,
  25. QueueFamilyIndex = queueFamilyIndex
  26. };
  27. _api.CreateCommandPool(_device, commandPoolCreateInfo, null, out _commandPool)
  28. .ThrowOnError();
  29. }
  30. public unsafe void Dispose()
  31. {
  32. lock (_lock)
  33. {
  34. FreeUsedCommandBuffers();
  35. _api.DestroyCommandPool(_device, _commandPool, null);
  36. }
  37. }
  38. private CommandBuffer AllocateCommandBuffer()
  39. {
  40. var commandBufferAllocateInfo = new CommandBufferAllocateInfo
  41. {
  42. SType = StructureType.CommandBufferAllocateInfo,
  43. CommandPool = _commandPool,
  44. CommandBufferCount = 1,
  45. Level = CommandBufferLevel.Primary
  46. };
  47. lock (_lock)
  48. {
  49. _api.AllocateCommandBuffers(_device, commandBufferAllocateInfo, out var commandBuffer);
  50. return commandBuffer;
  51. }
  52. }
  53. public VulkanCommandBuffer CreateCommandBuffer()
  54. {
  55. return new(_api, _device, _queue, this);
  56. }
  57. public void FreeUsedCommandBuffers()
  58. {
  59. lock (_lock)
  60. {
  61. foreach (var usedCommandBuffer in _usedCommandBuffers) usedCommandBuffer.Dispose();
  62. _usedCommandBuffers.Clear();
  63. }
  64. }
  65. private void DisposeCommandBuffer(VulkanCommandBuffer commandBuffer)
  66. {
  67. lock (_lock)
  68. {
  69. _usedCommandBuffers.Add(commandBuffer);
  70. }
  71. }
  72. public class VulkanCommandBuffer : IDisposable
  73. {
  74. private readonly VulkanCommandBufferPool _commandBufferPool;
  75. private readonly Vk _api;
  76. private readonly Device _device;
  77. private readonly Queue _queue;
  78. private readonly Fence _fence;
  79. private bool _hasEnded;
  80. private bool _hasStarted;
  81. public IntPtr Handle => InternalHandle.Handle;
  82. internal CommandBuffer InternalHandle { get; }
  83. internal unsafe VulkanCommandBuffer(Vk api, Device device, Queue queue, VulkanCommandBufferPool commandBufferPool)
  84. {
  85. _api = api;
  86. _device = device;
  87. _queue = queue;
  88. _commandBufferPool = commandBufferPool;
  89. InternalHandle = _commandBufferPool.AllocateCommandBuffer();
  90. var fenceCreateInfo = new FenceCreateInfo()
  91. {
  92. SType = StructureType.FenceCreateInfo,
  93. Flags = FenceCreateFlags.FenceCreateSignaledBit
  94. };
  95. api.CreateFence(device, fenceCreateInfo, null, out _fence);
  96. }
  97. public unsafe void Dispose()
  98. {
  99. _api.WaitForFences(_device, 1, _fence, true, ulong.MaxValue);
  100. lock (_commandBufferPool._lock)
  101. {
  102. _api.FreeCommandBuffers(_device, _commandBufferPool._commandPool, 1, InternalHandle);
  103. }
  104. _api.DestroyFence(_device, _fence, null);
  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. _api.BeginCommandBuffer(InternalHandle, beginInfo);
  117. }
  118. }
  119. public void EndRecording()
  120. {
  121. if (_hasStarted && !_hasEnded)
  122. {
  123. _hasEnded = true;
  124. _api.EndCommandBuffer(InternalHandle);
  125. }
  126. }
  127. public void Submit()
  128. {
  129. Submit(null, null, null, _fence);
  130. }
  131. public class KeyedMutexSubmitInfo
  132. {
  133. public ulong? AcquireKey { get; set; }
  134. public ulong? ReleaseKey { get; set; }
  135. public DeviceMemory DeviceMemory { get; set; }
  136. }
  137. public unsafe void Submit(
  138. ReadOnlySpan<Semaphore> waitSemaphores,
  139. ReadOnlySpan<PipelineStageFlags> waitDstStageMask = default,
  140. ReadOnlySpan<Semaphore> signalSemaphores = default,
  141. Fence? fence = null,
  142. KeyedMutexSubmitInfo keyedMutex = null)
  143. {
  144. EndRecording();
  145. if (!fence.HasValue)
  146. fence = _fence;
  147. ulong acquireKey = keyedMutex?.AcquireKey ?? 0, releaseKey = keyedMutex?.ReleaseKey ?? 0;
  148. DeviceMemory devMem = keyedMutex?.DeviceMemory ?? default;
  149. uint timeout = uint.MaxValue;
  150. Win32KeyedMutexAcquireReleaseInfoKHR mutex = default;
  151. if (keyedMutex != null)
  152. mutex = new Win32KeyedMutexAcquireReleaseInfoKHR
  153. {
  154. SType = StructureType.Win32KeyedMutexAcquireReleaseInfoKhr,
  155. AcquireCount = keyedMutex.AcquireKey.HasValue ? 1u : 0u,
  156. ReleaseCount = keyedMutex.ReleaseKey.HasValue ? 1u : 0u,
  157. PAcquireKeys = &acquireKey,
  158. PReleaseKeys = &releaseKey,
  159. PAcquireSyncs = &devMem,
  160. PReleaseSyncs = &devMem,
  161. PAcquireTimeouts = &timeout
  162. };
  163. fixed (Semaphore* pWaitSemaphores = waitSemaphores, pSignalSemaphores = signalSemaphores)
  164. {
  165. fixed (PipelineStageFlags* pWaitDstStageMask = waitDstStageMask)
  166. {
  167. var commandBuffer = InternalHandle;
  168. var submitInfo = new SubmitInfo
  169. {
  170. PNext = keyedMutex != null ? &mutex : null,
  171. SType = StructureType.SubmitInfo,
  172. WaitSemaphoreCount = waitSemaphores != null ? (uint)waitSemaphores.Length : 0,
  173. PWaitSemaphores = pWaitSemaphores,
  174. PWaitDstStageMask = pWaitDstStageMask,
  175. CommandBufferCount = 1,
  176. PCommandBuffers = &commandBuffer,
  177. SignalSemaphoreCount = signalSemaphores != null ? (uint)signalSemaphores.Length : 0,
  178. PSignalSemaphores = pSignalSemaphores,
  179. };
  180. _api.ResetFences(_device, 1, fence.Value);
  181. _api.QueueSubmit(_queue, 1, submitInfo, fence.Value);
  182. }
  183. }
  184. _commandBufferPool.DisposeCommandBuffer(this);
  185. }
  186. }
  187. }
  188. }