CounterQueueEvent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using Ryujinx.Graphics.GAL;
  2. using System;
  3. using System.Threading;
  4. namespace Ryujinx.Graphics.Vulkan.Queries
  5. {
  6. class CounterQueueEvent : ICounterEvent
  7. {
  8. public event EventHandler<ulong> OnResult;
  9. public CounterType Type { get; }
  10. public bool ClearCounter { get; private set; }
  11. public bool Disposed { get; private set; }
  12. public bool Invalid { get; set; }
  13. public ulong DrawIndex { get; }
  14. private CounterQueue _queue;
  15. private BufferedQuery _counter;
  16. private bool _hostAccessReserved = false;
  17. private int _refCount = 1; // Starts with a reference from the counter queue.
  18. private object _lock = new object();
  19. private ulong _result = ulong.MaxValue;
  20. public CounterQueueEvent(CounterQueue queue, CounterType type, ulong drawIndex)
  21. {
  22. _queue = queue;
  23. _counter = queue.GetQueryObject();
  24. Type = type;
  25. DrawIndex = drawIndex;
  26. _counter.Begin(_queue.ResetSequence);
  27. }
  28. public Auto<DisposableBuffer> GetBuffer()
  29. {
  30. return _counter.GetBuffer();
  31. }
  32. internal void Clear(bool counterReset)
  33. {
  34. if (counterReset)
  35. {
  36. _counter.Reset();
  37. }
  38. ClearCounter = true;
  39. }
  40. internal void Complete(bool withResult)
  41. {
  42. _counter.End(withResult);
  43. }
  44. internal bool TryConsume(ref ulong result, bool block, AutoResetEvent wakeSignal = null)
  45. {
  46. lock (_lock)
  47. {
  48. if (Disposed)
  49. {
  50. return true;
  51. }
  52. if (ClearCounter)
  53. {
  54. result = 0;
  55. }
  56. long queryResult;
  57. if (block)
  58. {
  59. queryResult = _counter.AwaitResult(wakeSignal);
  60. }
  61. else
  62. {
  63. if (!_counter.TryGetResult(out queryResult))
  64. {
  65. return false;
  66. }
  67. }
  68. result += (ulong)queryResult;
  69. _result = result;
  70. OnResult?.Invoke(this, result);
  71. Dispose(); // Return the our resources to the pool.
  72. return true;
  73. }
  74. }
  75. public void Flush()
  76. {
  77. if (Disposed)
  78. {
  79. return;
  80. }
  81. // Tell the queue to process all events up to this one.
  82. _queue.FlushTo(this);
  83. }
  84. public void DecrementRefCount()
  85. {
  86. if (Interlocked.Decrement(ref _refCount) == 0)
  87. {
  88. DisposeInternal();
  89. }
  90. }
  91. public bool ReserveForHostAccess()
  92. {
  93. if (_hostAccessReserved)
  94. {
  95. return true;
  96. }
  97. if (IsValueAvailable())
  98. {
  99. return false;
  100. }
  101. if (Interlocked.Increment(ref _refCount) == 1)
  102. {
  103. Interlocked.Decrement(ref _refCount);
  104. return false;
  105. }
  106. _hostAccessReserved = true;
  107. return true;
  108. }
  109. public void ReleaseHostAccess()
  110. {
  111. _hostAccessReserved = false;
  112. DecrementRefCount();
  113. }
  114. private void DisposeInternal()
  115. {
  116. _queue.ReturnQueryObject(_counter);
  117. }
  118. private bool IsValueAvailable()
  119. {
  120. return _result != ulong.MaxValue || _counter.TryGetResult(out _);
  121. }
  122. public void Dispose()
  123. {
  124. Disposed = true;
  125. DecrementRefCount();
  126. }
  127. }
  128. }