CounterQueueEvent.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. private double _divisor = 1f;
  21. public CounterQueueEvent(CounterQueue queue, CounterType type, ulong drawIndex)
  22. {
  23. _queue = queue;
  24. _counter = queue.GetQueryObject();
  25. Type = type;
  26. DrawIndex = drawIndex;
  27. _counter.Begin(_queue.ResetSequence);
  28. }
  29. public Auto<DisposableBuffer> GetBuffer()
  30. {
  31. return _counter.GetBuffer();
  32. }
  33. internal void Clear(bool counterReset)
  34. {
  35. if (counterReset)
  36. {
  37. _counter.Reset();
  38. }
  39. ClearCounter = true;
  40. }
  41. internal void Complete(bool withResult, double divisor)
  42. {
  43. _counter.End(withResult);
  44. _divisor = divisor;
  45. }
  46. internal bool TryConsume(ref ulong result, bool block, AutoResetEvent wakeSignal = null)
  47. {
  48. lock (_lock)
  49. {
  50. if (Disposed)
  51. {
  52. return true;
  53. }
  54. if (ClearCounter)
  55. {
  56. result = 0;
  57. }
  58. long queryResult;
  59. if (block)
  60. {
  61. queryResult = _counter.AwaitResult(wakeSignal);
  62. }
  63. else
  64. {
  65. if (!_counter.TryGetResult(out queryResult))
  66. {
  67. return false;
  68. }
  69. }
  70. result += _divisor == 1 ? (ulong)queryResult : (ulong)Math.Ceiling(queryResult / _divisor);
  71. _result = result;
  72. OnResult?.Invoke(this, result);
  73. Dispose(); // Return the our resources to the pool.
  74. return true;
  75. }
  76. }
  77. public void Flush()
  78. {
  79. if (Disposed)
  80. {
  81. return;
  82. }
  83. // Tell the queue to process all events up to this one.
  84. _queue.FlushTo(this);
  85. }
  86. public void DecrementRefCount()
  87. {
  88. if (Interlocked.Decrement(ref _refCount) == 0)
  89. {
  90. DisposeInternal();
  91. }
  92. }
  93. public bool ReserveForHostAccess()
  94. {
  95. if (_hostAccessReserved)
  96. {
  97. return true;
  98. }
  99. if (IsValueAvailable())
  100. {
  101. return false;
  102. }
  103. if (Interlocked.Increment(ref _refCount) == 1)
  104. {
  105. Interlocked.Decrement(ref _refCount);
  106. return false;
  107. }
  108. _hostAccessReserved = true;
  109. return true;
  110. }
  111. public void ReleaseHostAccess()
  112. {
  113. _hostAccessReserved = false;
  114. DecrementRefCount();
  115. }
  116. private void DisposeInternal()
  117. {
  118. _queue.ReturnQueryObject(_counter);
  119. }
  120. private bool IsValueAvailable()
  121. {
  122. return _result != ulong.MaxValue || _counter.TryGetResult(out _);
  123. }
  124. public void Dispose()
  125. {
  126. Disposed = true;
  127. DecrementRefCount();
  128. }
  129. }
  130. }