MultiFenceHolder.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using Silk.NET.Vulkan;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Ryujinx.Graphics.Vulkan
  5. {
  6. /// <summary>
  7. /// Holder for multiple host GPU fences.
  8. /// </summary>
  9. class MultiFenceHolder
  10. {
  11. private static int BufferUsageTrackingGranularity = 4096;
  12. private readonly Dictionary<FenceHolder, int> _fences;
  13. private BufferUsageBitmap _bufferUsageBitmap;
  14. /// <summary>
  15. /// Creates a new instance of the multiple fence holder.
  16. /// </summary>
  17. public MultiFenceHolder()
  18. {
  19. _fences = new Dictionary<FenceHolder, int>();
  20. }
  21. /// <summary>
  22. /// Creates a new instance of the multiple fence holder, with a given buffer size in mind.
  23. /// </summary>
  24. /// <param name="size">Size of the buffer</param>
  25. public MultiFenceHolder(int size)
  26. {
  27. _fences = new Dictionary<FenceHolder, int>();
  28. _bufferUsageBitmap = new BufferUsageBitmap(size, BufferUsageTrackingGranularity);
  29. }
  30. /// <summary>
  31. /// Adds buffer usage information to the uses list.
  32. /// </summary>
  33. /// <param name="cbIndex">Index of the command buffer where the buffer is used</param>
  34. /// <param name="offset">Offset of the buffer being used</param>
  35. /// <param name="size">Size of the buffer region being used, in bytes</param>
  36. public void AddBufferUse(int cbIndex, int offset, int size)
  37. {
  38. _bufferUsageBitmap.Add(cbIndex, offset, size);
  39. }
  40. /// <summary>
  41. /// Removes all buffer usage information for a given command buffer.
  42. /// </summary>
  43. /// <param name="cbIndex">Index of the command buffer where the buffer is used</param>
  44. public void RemoveBufferUses(int cbIndex)
  45. {
  46. _bufferUsageBitmap?.Clear(cbIndex);
  47. }
  48. /// <summary>
  49. /// Checks if a given range of a buffer is being used by a command buffer still being processed by the GPU.
  50. /// </summary>
  51. /// <param name="cbIndex">Index of the command buffer where the buffer is used</param>
  52. /// <param name="offset">Offset of the buffer being used</param>
  53. /// <param name="size">Size of the buffer region being used, in bytes</param>
  54. /// <returns>True if in use, false otherwise</returns>
  55. public bool IsBufferRangeInUse(int cbIndex, int offset, int size)
  56. {
  57. return _bufferUsageBitmap.OverlapsWith(cbIndex, offset, size);
  58. }
  59. /// <summary>
  60. /// Checks if a given range of a buffer is being used by any command buffer still being processed by the GPU.
  61. /// </summary>
  62. /// <param name="offset">Offset of the buffer being used</param>
  63. /// <param name="size">Size of the buffer region being used, in bytes</param>
  64. /// <returns>True if in use, false otherwise</returns>
  65. public bool IsBufferRangeInUse(int offset, int size)
  66. {
  67. return _bufferUsageBitmap.OverlapsWith(offset, size);
  68. }
  69. /// <summary>
  70. /// Adds a fence to the holder.
  71. /// </summary>
  72. /// <param name="cbIndex">Command buffer index of the command buffer that owns the fence</param>
  73. /// <param name="fence">Fence to be added</param>
  74. public void AddFence(int cbIndex, FenceHolder fence)
  75. {
  76. lock (_fences)
  77. {
  78. _fences.TryAdd(fence, cbIndex);
  79. }
  80. }
  81. /// <summary>
  82. /// Removes a fence from the holder.
  83. /// </summary>
  84. /// <param name="cbIndex">Command buffer index of the command buffer that owns the fence</param>
  85. /// <param name="fence">Fence to be removed</param>
  86. public void RemoveFence(int cbIndex, FenceHolder fence)
  87. {
  88. lock (_fences)
  89. {
  90. _fences.Remove(fence);
  91. }
  92. }
  93. /// <summary>
  94. /// Wait until all the fences on the holder are signaled.
  95. /// </summary>
  96. /// <param name="api">Vulkan API instance</param>
  97. /// <param name="device">GPU device that the fences belongs to</param>
  98. public void WaitForFences(Vk api, Device device)
  99. {
  100. WaitForFencesImpl(api, device, 0, 0, false, 0UL);
  101. }
  102. /// <summary>
  103. /// Wait until all the fences on the holder with buffer uses overlapping the specified range are signaled.
  104. /// </summary>
  105. /// <param name="api">Vulkan API instance</param>
  106. /// <param name="device">GPU device that the fences belongs to</param>
  107. /// <param name="offset">Start offset of the buffer range</param>
  108. /// <param name="size">Size of the buffer range in bytes</param>
  109. public void WaitForFences(Vk api, Device device, int offset, int size)
  110. {
  111. WaitForFencesImpl(api, device, offset, size, false, 0UL);
  112. }
  113. /// <summary>
  114. /// Wait until all the fences on the holder are signaled, or the timeout expires.
  115. /// </summary>
  116. /// <param name="api">Vulkan API instance</param>
  117. /// <param name="device">GPU device that the fences belongs to</param>
  118. /// <param name="timeout">Timeout in nanoseconds</param>
  119. /// <returns>True if all fences were signaled, false otherwise</returns>
  120. public bool WaitForFences(Vk api, Device device, ulong timeout)
  121. {
  122. return WaitForFencesImpl(api, device, 0, 0, true, timeout);
  123. }
  124. /// <summary>
  125. /// Wait until all the fences on the holder with buffer uses overlapping the specified range are signaled.
  126. /// </summary>
  127. /// <param name="api">Vulkan API instance</param>
  128. /// <param name="device">GPU device that the fences belongs to</param>
  129. /// <param name="offset">Start offset of the buffer range</param>
  130. /// <param name="size">Size of the buffer range in bytes</param>
  131. /// <param name="hasTimeout">Indicates if <paramref name="timeout"/> should be used</param>
  132. /// <param name="timeout">Timeout in nanoseconds</param>
  133. /// <returns>True if all fences were signaled before the timeout expired, false otherwise</returns>
  134. private bool WaitForFencesImpl(Vk api, Device device, int offset, int size, bool hasTimeout, ulong timeout)
  135. {
  136. FenceHolder[] fenceHolders;
  137. Fence[] fences;
  138. lock (_fences)
  139. {
  140. fenceHolders = size != 0 ? GetOverlappingFences(offset, size) : _fences.Keys.ToArray();
  141. fences = new Fence[fenceHolders.Length];
  142. for (int i = 0; i < fenceHolders.Length; i++)
  143. {
  144. fences[i] = fenceHolders[i].Get();
  145. }
  146. }
  147. if (fences.Length == 0)
  148. {
  149. return true;
  150. }
  151. bool signaled = true;
  152. if (hasTimeout)
  153. {
  154. signaled = FenceHelper.AllSignaled(api, device, fences, timeout);
  155. }
  156. else
  157. {
  158. FenceHelper.WaitAllIndefinitely(api, device, fences);
  159. }
  160. for (int i = 0; i < fenceHolders.Length; i++)
  161. {
  162. fenceHolders[i].Put();
  163. }
  164. return signaled;
  165. }
  166. /// <summary>
  167. /// Gets fences to wait for use of a given buffer region.
  168. /// </summary>
  169. /// <param name="offset">Offset of the range</param>
  170. /// <param name="size">Size of the range in bytes</param>
  171. /// <returns>Fences for the specified region</returns>
  172. private FenceHolder[] GetOverlappingFences(int offset, int size)
  173. {
  174. List<FenceHolder> overlapping = new List<FenceHolder>();
  175. foreach (var kv in _fences)
  176. {
  177. var fence = kv.Key;
  178. var ownerCbIndex = kv.Value;
  179. if (_bufferUsageBitmap.OverlapsWith(ownerCbIndex, offset, size))
  180. {
  181. overlapping.Add(fence);
  182. }
  183. }
  184. return overlapping.ToArray();
  185. }
  186. }
  187. }