SemaphoreUpdater.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Ryujinx.Graphics.GAL;
  2. using System;
  3. namespace Ryujinx.Graphics.Gpu.Engine.Threed
  4. {
  5. /// <summary>
  6. /// Semaphore updater.
  7. /// </summary>
  8. class SemaphoreUpdater
  9. {
  10. /// <summary>
  11. /// GPU semaphore operation.
  12. /// </summary>
  13. private enum SemaphoreOperation
  14. {
  15. Release = 0,
  16. Acquire = 1,
  17. Counter = 2
  18. }
  19. /// <summary>
  20. /// Counter type for GPU counter reset.
  21. /// </summary>
  22. private enum ResetCounterType
  23. {
  24. SamplesPassed = 1,
  25. ZcullStats = 2,
  26. TransformFeedbackPrimitivesWritten = 0x10,
  27. InputVertices = 0x12,
  28. InputPrimitives = 0x13,
  29. VertexShaderInvocations = 0x15,
  30. TessControlShaderInvocations = 0x16,
  31. TessEvaluationShaderInvocations = 0x17,
  32. TessEvaluationShaderPrimitives = 0x18,
  33. GeometryShaderInvocations = 0x1a,
  34. GeometryShaderPrimitives = 0x1b,
  35. ClipperInputPrimitives = 0x1c,
  36. ClipperOutputPrimitives = 0x1d,
  37. FragmentShaderInvocations = 0x1e,
  38. PrimitivesGenerated = 0x1f
  39. }
  40. /// <summary>
  41. /// Counter type for GPU counter reporting.
  42. /// </summary>
  43. private enum ReportCounterType
  44. {
  45. Payload = 0,
  46. InputVertices = 1,
  47. InputPrimitives = 3,
  48. VertexShaderInvocations = 5,
  49. GeometryShaderInvocations = 7,
  50. GeometryShaderPrimitives = 9,
  51. ZcullStats0 = 0xa,
  52. TransformFeedbackPrimitivesWritten = 0xb,
  53. ZcullStats1 = 0xc,
  54. ZcullStats2 = 0xe,
  55. ClipperInputPrimitives = 0xf,
  56. ZcullStats3 = 0x10,
  57. ClipperOutputPrimitives = 0x11,
  58. PrimitivesGenerated = 0x12,
  59. FragmentShaderInvocations = 0x13,
  60. SamplesPassed = 0x15,
  61. TransformFeedbackOffset = 0x1a,
  62. TessControlShaderInvocations = 0x1b,
  63. TessEvaluationShaderInvocations = 0x1d,
  64. TessEvaluationShaderPrimitives = 0x1f
  65. }
  66. private readonly GpuContext _context;
  67. private readonly GpuChannel _channel;
  68. private readonly DeviceStateWithShadow<ThreedClassState> _state;
  69. /// <summary>
  70. /// Creates a new instance of the semaphore updater.
  71. /// </summary>
  72. /// <param name="context">GPU context</param>
  73. /// <param name="channel">GPU channel</param>
  74. /// <param name="state">Channel state</param>
  75. public SemaphoreUpdater(GpuContext context, GpuChannel channel, DeviceStateWithShadow<ThreedClassState> state)
  76. {
  77. _context = context;
  78. _channel = channel;
  79. _state = state;
  80. }
  81. /// <summary>
  82. /// Resets the value of an internal GPU counter back to zero.
  83. /// </summary>
  84. /// <param name="argument">Method call argument</param>
  85. public void ResetCounter(int argument)
  86. {
  87. ResetCounterType type = (ResetCounterType)argument;
  88. switch (type)
  89. {
  90. case ResetCounterType.SamplesPassed:
  91. _context.Renderer.ResetCounter(CounterType.SamplesPassed);
  92. break;
  93. case ResetCounterType.PrimitivesGenerated:
  94. _context.Renderer.ResetCounter(CounterType.PrimitivesGenerated);
  95. break;
  96. case ResetCounterType.TransformFeedbackPrimitivesWritten:
  97. _context.Renderer.ResetCounter(CounterType.TransformFeedbackPrimitivesWritten);
  98. break;
  99. }
  100. }
  101. /// <summary>
  102. /// Writes a GPU counter to guest memory.
  103. /// </summary>
  104. /// <param name="argument">Method call argument</param>
  105. public void Report(int argument)
  106. {
  107. SemaphoreOperation op = (SemaphoreOperation)(argument & 3);
  108. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  109. switch (op)
  110. {
  111. case SemaphoreOperation.Release: ReleaseSemaphore(); break;
  112. case SemaphoreOperation.Counter: ReportCounter(type); break;
  113. }
  114. }
  115. /// <summary>
  116. /// Writes (or Releases) a GPU semaphore value to guest memory.
  117. /// </summary>
  118. private void ReleaseSemaphore()
  119. {
  120. _channel.MemoryManager.Write(_state.State.SemaphoreAddress.Pack(), _state.State.SemaphorePayload);
  121. _context.AdvanceSequence();
  122. }
  123. /// <summary>
  124. /// Packed GPU counter data (including GPU timestamp) in memory.
  125. /// </summary>
  126. private struct CounterData
  127. {
  128. public ulong Counter;
  129. public ulong Timestamp;
  130. }
  131. /// <summary>
  132. /// Writes a GPU counter to guest memory.
  133. /// This also writes the current timestamp value.
  134. /// </summary>
  135. /// <param name="type">Counter to be written to memory</param>
  136. private void ReportCounter(ReportCounterType type)
  137. {
  138. ulong gpuVa = _state.State.SemaphoreAddress.Pack();
  139. ulong ticks = _context.GetTimestamp();
  140. float divisor = type switch
  141. {
  142. ReportCounterType.SamplesPassed => _channel.TextureManager.RenderTargetScale * _channel.TextureManager.RenderTargetScale,
  143. _ => 1f
  144. };
  145. ICounterEvent counter = null;
  146. void resultHandler(object evt, ulong result)
  147. {
  148. if (divisor != 1f)
  149. {
  150. result = (ulong)MathF.Ceiling(result / divisor);
  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.Payload:
  165. resultHandler(null, (ulong)_state.State.SemaphorePayload);
  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. }
  180. }