AutoFlushCounter.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Diagnostics;
  3. namespace Ryujinx.Graphics.Vulkan
  4. {
  5. internal class AutoFlushCounter
  6. {
  7. // How often to flush on framebuffer change.
  8. private readonly static long FramebufferFlushTimer = Stopwatch.Frequency / 1000;
  9. private const int MinDrawCountForFlush = 10;
  10. private const int InitialQueryCountForFlush = 32;
  11. private long _lastFlush;
  12. private ulong _lastDrawCount;
  13. private bool _hasPendingQuery;
  14. private int _queryCount;
  15. public void RegisterFlush(ulong drawCount)
  16. {
  17. _lastFlush = Stopwatch.GetTimestamp();
  18. _lastDrawCount = drawCount;
  19. _hasPendingQuery = false;
  20. }
  21. public bool RegisterPendingQuery()
  22. {
  23. _hasPendingQuery = true;
  24. // Interrupt render passes to flush queries, so that early results arrive sooner.
  25. if (++_queryCount == InitialQueryCountForFlush)
  26. {
  27. return true;
  28. }
  29. return false;
  30. }
  31. public bool ShouldFlushQuery()
  32. {
  33. return _hasPendingQuery;
  34. }
  35. public bool ShouldFlush(ulong drawCount)
  36. {
  37. _queryCount = 0;
  38. if (_hasPendingQuery)
  39. {
  40. return true;
  41. }
  42. long draws = (long)(drawCount - _lastDrawCount);
  43. if (draws < MinDrawCountForFlush)
  44. {
  45. if (draws == 0)
  46. {
  47. _lastFlush = Stopwatch.GetTimestamp();
  48. }
  49. return false;
  50. }
  51. long flushTimeout = FramebufferFlushTimer;
  52. long now = Stopwatch.GetTimestamp();
  53. return now > _lastFlush + flushTimeout;
  54. }
  55. }
  56. }