SynchronizationManager.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Threading;
  4. namespace Ryujinx.Graphics.Gpu.Synchronization
  5. {
  6. /// <summary>
  7. /// GPU synchronization manager.
  8. /// </summary>
  9. public class SynchronizationManager
  10. {
  11. /// <summary>
  12. /// The maximum number of syncpoints supported by the GM20B.
  13. /// </summary>
  14. public const int MaxHardwareSyncpoints = 192;
  15. /// <summary>
  16. /// Array containing all hardware syncpoints.
  17. /// </summary>
  18. private Syncpoint[] _syncpoints;
  19. public SynchronizationManager()
  20. {
  21. _syncpoints = new Syncpoint[MaxHardwareSyncpoints];
  22. for (uint i = 0; i < _syncpoints.Length; i++)
  23. {
  24. _syncpoints[i] = new Syncpoint(i);
  25. }
  26. }
  27. /// <summary>
  28. /// Increment the value of a syncpoint with a given id.
  29. /// </summary>
  30. /// <param name="id">The id of the syncpoint</param>
  31. /// <exception cref="System.ArgumentOutOfRangeException">Thrown when id >= MaxHardwareSyncpoints</exception>
  32. /// <returns>The incremented value of the syncpoint</returns>
  33. public uint IncrementSyncpoint(uint id)
  34. {
  35. if (id >= MaxHardwareSyncpoints)
  36. {
  37. throw new ArgumentOutOfRangeException(nameof(id));
  38. }
  39. return _syncpoints[id].Increment();
  40. }
  41. /// <summary>
  42. /// Get the value of a syncpoint with a given id.
  43. /// </summary>
  44. /// <param name="id">The id of the syncpoint</param>
  45. /// <exception cref="System.ArgumentOutOfRangeException">Thrown when id >= MaxHardwareSyncpoints</exception>
  46. /// <returns>The value of the syncpoint</returns>
  47. public uint GetSyncpointValue(uint id)
  48. {
  49. if (id >= MaxHardwareSyncpoints)
  50. {
  51. throw new ArgumentOutOfRangeException(nameof(id));
  52. }
  53. return _syncpoints[id].Value;
  54. }
  55. /// <summary>
  56. /// Register a new callback on a syncpoint with a given id at a target threshold.
  57. /// The callback will be called once the threshold is reached and will automatically be unregistered.
  58. /// </summary>
  59. /// <param name="id">The id of the syncpoint</param>
  60. /// <param name="threshold">The target threshold</param>
  61. /// <param name="callback">The callback to call when the threshold is reached</param>
  62. /// <exception cref="System.ArgumentOutOfRangeException">Thrown when id >= MaxHardwareSyncpoints</exception>
  63. /// <returns>The created SyncpointWaiterHandle object or null if already past threshold</returns>
  64. public SyncpointWaiterHandle RegisterCallbackOnSyncpoint(uint id, uint threshold, Action<SyncpointWaiterHandle> callback)
  65. {
  66. if (id >= MaxHardwareSyncpoints)
  67. {
  68. throw new ArgumentOutOfRangeException(nameof(id));
  69. }
  70. return _syncpoints[id].RegisterCallback(threshold, callback);
  71. }
  72. /// <summary>
  73. /// Unregister a callback on a given syncpoint.
  74. /// </summary>
  75. /// <param name="id">The id of the syncpoint</param>
  76. /// <param name="waiterInformation">The waiter information to unregister</param>
  77. /// <exception cref="System.ArgumentOutOfRangeException">Thrown when id >= MaxHardwareSyncpoints</exception>
  78. public void UnregisterCallback(uint id, SyncpointWaiterHandle waiterInformation)
  79. {
  80. if (id >= MaxHardwareSyncpoints)
  81. {
  82. throw new ArgumentOutOfRangeException(nameof(id));
  83. }
  84. _syncpoints[id].UnregisterCallback(waiterInformation);
  85. }
  86. /// <summary>
  87. /// Wait on a syncpoint with a given id at a target threshold.
  88. /// The callback will be called once the threshold is reached and will automatically be unregistered.
  89. /// </summary>
  90. /// <param name="id">The id of the syncpoint</param>
  91. /// <param name="threshold">The target threshold</param>
  92. /// <param name="timeout">The timeout</param>
  93. /// <exception cref="System.ArgumentOutOfRangeException">Thrown when id >= MaxHardwareSyncpoints</exception>
  94. /// <returns>True if timed out</returns>
  95. public bool WaitOnSyncpoint(uint id, uint threshold, TimeSpan timeout)
  96. {
  97. if (id >= MaxHardwareSyncpoints)
  98. {
  99. throw new ArgumentOutOfRangeException(nameof(id));
  100. }
  101. // TODO: Remove this when GPU channel scheduling will be implemented.
  102. if (timeout == Timeout.InfiniteTimeSpan)
  103. {
  104. timeout = TimeSpan.FromSeconds(1);
  105. }
  106. using (ManualResetEvent waitEvent = new ManualResetEvent(false))
  107. {
  108. var info = _syncpoints[id].RegisterCallback(threshold, (x) => waitEvent.Set());
  109. if (info == null)
  110. {
  111. return false;
  112. }
  113. bool signaled = waitEvent.WaitOne(timeout);
  114. if (!signaled && info != null)
  115. {
  116. Logger.Error?.Print(LogClass.Gpu, $"Wait on syncpoint {id} for threshold {threshold} took more than {timeout.TotalMilliseconds}ms, resuming execution...");
  117. _syncpoints[id].UnregisterCallback(info);
  118. }
  119. return !signaled;
  120. }
  121. }
  122. }
  123. }