SemaphoreUpdater.cs 7.9 KB

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