CounterQueue.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using Ryujinx.Graphics.GAL;
  2. using Silk.NET.Vulkan;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading;
  7. namespace Ryujinx.Graphics.Vulkan.Queries
  8. {
  9. class CounterQueue : IDisposable
  10. {
  11. private const int QueryPoolInitialSize = 100;
  12. private readonly VulkanRenderer _gd;
  13. private readonly Device _device;
  14. private readonly PipelineFull _pipeline;
  15. public CounterType Type { get; }
  16. public bool Disposed { get; private set; }
  17. private Queue<CounterQueueEvent> _events = new Queue<CounterQueueEvent>();
  18. private CounterQueueEvent _current;
  19. private ulong _accumulatedCounter;
  20. private int _waiterCount;
  21. private object _lock = new object();
  22. private Queue<BufferedQuery> _queryPool;
  23. private AutoResetEvent _queuedEvent = new AutoResetEvent(false);
  24. private AutoResetEvent _wakeSignal = new AutoResetEvent(false);
  25. private AutoResetEvent _eventConsumed = new AutoResetEvent(false);
  26. private Thread _consumerThread;
  27. public int ResetSequence { get; private set; }
  28. internal CounterQueue(VulkanRenderer gd, Device device, PipelineFull pipeline, CounterType type)
  29. {
  30. _gd = gd;
  31. _device = device;
  32. _pipeline = pipeline;
  33. Type = type;
  34. _queryPool = new Queue<BufferedQuery>(QueryPoolInitialSize);
  35. for (int i = 0; i < QueryPoolInitialSize; i++)
  36. {
  37. // AMD Polaris GPUs on Windows seem to have issues reporting 64-bit query results.
  38. _queryPool.Enqueue(new BufferedQuery(_gd, _device, _pipeline, type, gd.IsAmdWindows));
  39. }
  40. _current = new CounterQueueEvent(this, type, 0);
  41. _consumerThread = new Thread(EventConsumer);
  42. _consumerThread.Start();
  43. }
  44. public void ResetCounterPool()
  45. {
  46. ResetSequence++;
  47. }
  48. public void ResetFutureCounters(CommandBuffer cmd, int count)
  49. {
  50. // Pre-emptively reset queries to avoid render pass splitting.
  51. lock (_queryPool)
  52. {
  53. count = Math.Min(count, _queryPool.Count);
  54. for (int i = 0; i < count; i++)
  55. {
  56. _queryPool.ElementAt(i).PoolReset(cmd, ResetSequence);
  57. }
  58. }
  59. }
  60. private void EventConsumer()
  61. {
  62. while (!Disposed)
  63. {
  64. CounterQueueEvent evt = null;
  65. lock (_lock)
  66. {
  67. if (_events.Count > 0)
  68. {
  69. evt = _events.Dequeue();
  70. }
  71. }
  72. if (evt == null)
  73. {
  74. _queuedEvent.WaitOne(); // No more events to go through, wait for more.
  75. }
  76. else
  77. {
  78. // Spin-wait rather than sleeping if there are any waiters, by passing null instead of the wake signal.
  79. evt.TryConsume(ref _accumulatedCounter, true, _waiterCount == 0 ? _wakeSignal : null);
  80. }
  81. if (_waiterCount > 0)
  82. {
  83. _eventConsumed.Set();
  84. }
  85. }
  86. }
  87. internal BufferedQuery GetQueryObject()
  88. {
  89. // Creating/disposing query objects on a context we're sharing with will cause issues.
  90. // So instead, make a lot of query objects on the main thread and reuse them.
  91. lock (_lock)
  92. {
  93. if (_queryPool.Count > 0)
  94. {
  95. BufferedQuery result = _queryPool.Dequeue();
  96. return result;
  97. }
  98. else
  99. {
  100. return new BufferedQuery(_gd, _device, _pipeline, Type, _gd.IsAmdWindows);
  101. }
  102. }
  103. }
  104. internal void ReturnQueryObject(BufferedQuery query)
  105. {
  106. lock (_lock)
  107. {
  108. // The query will be reset when it dequeues.
  109. _queryPool.Enqueue(query);
  110. }
  111. }
  112. public CounterQueueEvent QueueReport(EventHandler<ulong> resultHandler, ulong lastDrawIndex, bool hostReserved)
  113. {
  114. CounterQueueEvent result;
  115. ulong draws = lastDrawIndex - _current.DrawIndex;
  116. lock (_lock)
  117. {
  118. // A query's result only matters if more than one draw was performed during it.
  119. // Otherwise, dummy it out and return 0 immediately.
  120. if (hostReserved)
  121. {
  122. // This counter event is guaranteed to be available for host conditional rendering.
  123. _current.ReserveForHostAccess();
  124. }
  125. _current.Complete(draws > 0 && Type != CounterType.TransformFeedbackPrimitivesWritten);
  126. _events.Enqueue(_current);
  127. _current.OnResult += resultHandler;
  128. result = _current;
  129. _current = new CounterQueueEvent(this, Type, lastDrawIndex);
  130. }
  131. _queuedEvent.Set();
  132. return result;
  133. }
  134. public void QueueReset(ulong lastDrawIndex)
  135. {
  136. ulong draws = lastDrawIndex - _current.DrawIndex;
  137. lock (_lock)
  138. {
  139. _current.Clear(draws != 0);
  140. }
  141. }
  142. public void Flush(bool blocking)
  143. {
  144. if (!blocking)
  145. {
  146. // Just wake the consumer thread - it will update the queries.
  147. _wakeSignal.Set();
  148. return;
  149. }
  150. lock (_lock)
  151. {
  152. // Tell the queue to process all events.
  153. while (_events.Count > 0)
  154. {
  155. CounterQueueEvent flush = _events.Peek();
  156. if (!flush.TryConsume(ref _accumulatedCounter, true))
  157. {
  158. return; // If not blocking, then return when we encounter an event that is not ready yet.
  159. }
  160. _events.Dequeue();
  161. }
  162. }
  163. }
  164. public void FlushTo(CounterQueueEvent evt)
  165. {
  166. // Flush the counter queue on the main thread.
  167. Interlocked.Increment(ref _waiterCount);
  168. _wakeSignal.Set();
  169. while (!evt.Disposed)
  170. {
  171. _eventConsumed.WaitOne(1);
  172. }
  173. Interlocked.Decrement(ref _waiterCount);
  174. }
  175. public void Dispose()
  176. {
  177. lock (_lock)
  178. {
  179. while (_events.Count > 0)
  180. {
  181. CounterQueueEvent evt = _events.Dequeue();
  182. evt.Dispose();
  183. }
  184. Disposed = true;
  185. }
  186. _queuedEvent.Set();
  187. _consumerThread.Join();
  188. _current?.Dispose();
  189. foreach (BufferedQuery query in _queryPool)
  190. {
  191. query.Dispose();
  192. }
  193. _queuedEvent.Dispose();
  194. _wakeSignal.Dispose();
  195. _eventConsumed.Dispose();
  196. }
  197. }
  198. }