SemaphoreUpdater.cs 6.5 KB

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