SemaphoreUpdater.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Graphics.Gpu.Engine.Threed
  5. {
  6. /// <summary>
  7. /// Semaphore updater.
  8. /// </summary>
  9. class SemaphoreUpdater
  10. {
  11. private const int NsToTicksFractionNumerator = 384;
  12. private const int NsToTicksFractionDenominator = 625;
  13. /// <summary>
  14. /// GPU semaphore operation.
  15. /// </summary>
  16. private enum SemaphoreOperation
  17. {
  18. Release = 0,
  19. Acquire = 1,
  20. Counter = 2
  21. }
  22. /// <summary>
  23. /// Counter type for GPU counter reset.
  24. /// </summary>
  25. private enum ResetCounterType
  26. {
  27. SamplesPassed = 1,
  28. ZcullStats = 2,
  29. TransformFeedbackPrimitivesWritten = 0x10,
  30. InputVertices = 0x12,
  31. InputPrimitives = 0x13,
  32. VertexShaderInvocations = 0x15,
  33. TessControlShaderInvocations = 0x16,
  34. TessEvaluationShaderInvocations = 0x17,
  35. TessEvaluationShaderPrimitives = 0x18,
  36. GeometryShaderInvocations = 0x1a,
  37. GeometryShaderPrimitives = 0x1b,
  38. ClipperInputPrimitives = 0x1c,
  39. ClipperOutputPrimitives = 0x1d,
  40. FragmentShaderInvocations = 0x1e,
  41. PrimitivesGenerated = 0x1f
  42. }
  43. /// <summary>
  44. /// Counter type for GPU counter reporting.
  45. /// </summary>
  46. private enum ReportCounterType
  47. {
  48. Zero = 0,
  49. InputVertices = 1,
  50. InputPrimitives = 3,
  51. VertexShaderInvocations = 5,
  52. GeometryShaderInvocations = 7,
  53. GeometryShaderPrimitives = 9,
  54. ZcullStats0 = 0xa,
  55. TransformFeedbackPrimitivesWritten = 0xb,
  56. ZcullStats1 = 0xc,
  57. ZcullStats2 = 0xe,
  58. ClipperInputPrimitives = 0xf,
  59. ZcullStats3 = 0x10,
  60. ClipperOutputPrimitives = 0x11,
  61. PrimitivesGenerated = 0x12,
  62. FragmentShaderInvocations = 0x13,
  63. SamplesPassed = 0x15,
  64. TransformFeedbackOffset = 0x1a,
  65. TessControlShaderInvocations = 0x1b,
  66. TessEvaluationShaderInvocations = 0x1d,
  67. TessEvaluationShaderPrimitives = 0x1f
  68. }
  69. private readonly GpuContext _context;
  70. private readonly GpuChannel _channel;
  71. private readonly DeviceStateWithShadow<ThreedClassState> _state;
  72. /// <summary>
  73. /// Creates a new instance of the semaphore updater.
  74. /// </summary>
  75. /// <param name="context">GPU context</param>
  76. /// <param name="channel">GPU channel</param>
  77. /// <param name="state">Channel state</param>
  78. public SemaphoreUpdater(GpuContext context, GpuChannel channel, DeviceStateWithShadow<ThreedClassState> state)
  79. {
  80. _context = context;
  81. _channel = channel;
  82. _state = state;
  83. }
  84. /// <summary>
  85. /// Resets the value of an internal GPU counter back to zero.
  86. /// </summary>
  87. /// <param name="argument">Method call argument</param>
  88. public void ResetCounter(int argument)
  89. {
  90. ResetCounterType type = (ResetCounterType)argument;
  91. switch (type)
  92. {
  93. case ResetCounterType.SamplesPassed:
  94. _context.Renderer.ResetCounter(CounterType.SamplesPassed);
  95. break;
  96. case ResetCounterType.PrimitivesGenerated:
  97. _context.Renderer.ResetCounter(CounterType.PrimitivesGenerated);
  98. break;
  99. case ResetCounterType.TransformFeedbackPrimitivesWritten:
  100. _context.Renderer.ResetCounter(CounterType.TransformFeedbackPrimitivesWritten);
  101. break;
  102. }
  103. }
  104. /// <summary>
  105. /// Writes a GPU counter to guest memory.
  106. /// </summary>
  107. /// <param name="argument">Method call argument</param>
  108. public void Report(int argument)
  109. {
  110. SemaphoreOperation op = (SemaphoreOperation)(argument & 3);
  111. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  112. switch (op)
  113. {
  114. case SemaphoreOperation.Release: ReleaseSemaphore(); break;
  115. case SemaphoreOperation.Counter: ReportCounter(type); break;
  116. }
  117. }
  118. /// <summary>
  119. /// Writes (or Releases) a GPU semaphore value to guest memory.
  120. /// </summary>
  121. private void ReleaseSemaphore()
  122. {
  123. _channel.MemoryManager.Write(_state.State.SemaphoreAddress.Pack(), _state.State.SemaphorePayload);
  124. _context.AdvanceSequence();
  125. }
  126. /// <summary>
  127. /// Packed GPU counter data (including GPU timestamp) in memory.
  128. /// </summary>
  129. private struct CounterData
  130. {
  131. public ulong Counter;
  132. public ulong Timestamp;
  133. }
  134. /// <summary>
  135. /// Writes a GPU counter to guest memory.
  136. /// This also writes the current timestamp value.
  137. /// </summary>
  138. /// <param name="type">Counter to be written to memory</param>
  139. private void ReportCounter(ReportCounterType type)
  140. {
  141. ulong gpuVa = _state.State.SemaphoreAddress.Pack();
  142. ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  143. if (GraphicsConfig.FastGpuTime)
  144. {
  145. // Divide by some amount to report time as if operations were performed faster than they really are.
  146. // This can prevent some games from switching to a lower resolution because rendering is too slow.
  147. ticks /= 256;
  148. }
  149. ICounterEvent counter = null;
  150. void resultHandler(object evt, ulong result)
  151. {
  152. CounterData counterData = new CounterData
  153. {
  154. Counter = result,
  155. Timestamp = ticks
  156. };
  157. if (counter?.Invalid != true)
  158. {
  159. _channel.MemoryManager.Write(gpuVa, counterData);
  160. }
  161. }
  162. switch (type)
  163. {
  164. case ReportCounterType.Zero:
  165. resultHandler(null, 0);
  166. break;
  167. case ReportCounterType.SamplesPassed:
  168. counter = _context.Renderer.ReportCounter(CounterType.SamplesPassed, resultHandler, false);
  169. break;
  170. case ReportCounterType.PrimitivesGenerated:
  171. counter = _context.Renderer.ReportCounter(CounterType.PrimitivesGenerated, resultHandler, false);
  172. break;
  173. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  174. counter = _context.Renderer.ReportCounter(CounterType.TransformFeedbackPrimitivesWritten, resultHandler, false);
  175. break;
  176. }
  177. _channel.MemoryManager.CounterCache.AddOrUpdate(gpuVa, counter);
  178. }
  179. /// <summary>
  180. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  181. /// </summary>
  182. /// <remarks>
  183. /// The frequency is 614400000 Hz.
  184. /// </remarks>
  185. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  186. /// <returns>Maxwell ticks</returns>
  187. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  188. {
  189. // We need to divide first to avoid overflows.
  190. // We fix up the result later by calculating the difference and adding
  191. // that to the result.
  192. ulong divided = nanoseconds / NsToTicksFractionDenominator;
  193. ulong rounded = divided * NsToTicksFractionDenominator;
  194. ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
  195. return divided * NsToTicksFractionNumerator + errorBias;
  196. }
  197. }
  198. }