MethodReport.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.Memory;
  4. using Ryujinx.Graphics.Gpu.State;
  5. using System;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Gpu.Engine
  8. {
  9. partial class Methods
  10. {
  11. private const int NsToTicksFractionNumerator = 384;
  12. private const int NsToTicksFractionDenominator = 625;
  13. private readonly CounterCache _counterCache = new CounterCache();
  14. /// <summary>
  15. /// Writes a GPU counter to guest memory.
  16. /// </summary>
  17. /// <param name="state">Current GPU state</param>
  18. /// <param name="argument">Method call argument</param>
  19. private void Report(GpuState state, int argument)
  20. {
  21. ReportMode mode = (ReportMode)(argument & 3);
  22. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  23. switch (mode)
  24. {
  25. case ReportMode.Release: ReleaseSemaphore(state); break;
  26. case ReportMode.Counter: ReportCounter(state, type); break;
  27. }
  28. }
  29. /// <summary>
  30. /// Writes (or Releases) a GPU semaphore value to guest memory.
  31. /// </summary>
  32. /// <param name="state">Current GPU state</param>
  33. private void ReleaseSemaphore(GpuState state)
  34. {
  35. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  36. _context.MemoryAccessor.Write(rs.Address.Pack(), rs.Payload);
  37. _context.AdvanceSequence();
  38. }
  39. /// <summary>
  40. /// Packed GPU counter data (including GPU timestamp) in memory.
  41. /// </summary>
  42. private struct CounterData
  43. {
  44. public ulong Counter;
  45. public ulong Timestamp;
  46. }
  47. /// <summary>
  48. /// Writes a GPU counter to guest memory.
  49. /// This also writes the current timestamp value.
  50. /// </summary>
  51. /// <param name="state">Current GPU state</param>
  52. /// <param name="type">Counter to be written to memory</param>
  53. private void ReportCounter(GpuState state, ReportCounterType type)
  54. {
  55. CounterData counterData = new CounterData();
  56. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  57. ulong gpuVa = rs.Address.Pack();
  58. ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  59. if (GraphicsConfig.FastGpuTime)
  60. {
  61. // Divide by some amount to report time as if operations were performed faster than they really are.
  62. // This can prevent some games from switching to a lower resolution because rendering is too slow.
  63. ticks /= 256;
  64. }
  65. ICounterEvent counter = null;
  66. EventHandler<ulong> resultHandler = (object evt, ulong result) =>
  67. {
  68. counterData.Counter = result;
  69. counterData.Timestamp = ticks;
  70. Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);
  71. Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);
  72. if (counter?.Invalid != true)
  73. {
  74. _context.MemoryAccessor.Write(gpuVa, data);
  75. }
  76. };
  77. switch (type)
  78. {
  79. case ReportCounterType.Zero:
  80. resultHandler(null, 0);
  81. break;
  82. case ReportCounterType.SamplesPassed:
  83. counter = _context.Renderer.ReportCounter(CounterType.SamplesPassed, resultHandler);
  84. break;
  85. case ReportCounterType.PrimitivesGenerated:
  86. counter = _context.Renderer.ReportCounter(CounterType.PrimitivesGenerated, resultHandler);
  87. break;
  88. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  89. counter = _context.Renderer.ReportCounter(CounterType.TransformFeedbackPrimitivesWritten, resultHandler);
  90. break;
  91. }
  92. _counterCache.AddOrUpdate(gpuVa, counter);
  93. }
  94. /// <summary>
  95. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  96. /// </summary>
  97. /// <remarks>
  98. /// The frequency is 614400000 Hz.
  99. /// </remarks>
  100. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  101. /// <returns>Maxwell ticks</returns>
  102. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  103. {
  104. // We need to divide first to avoid overflows.
  105. // We fix up the result later by calculating the difference and adding
  106. // that to the result.
  107. ulong divided = nanoseconds / NsToTicksFractionDenominator;
  108. ulong rounded = divided * NsToTicksFractionDenominator;
  109. ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
  110. return divided * NsToTicksFractionNumerator + errorBias;
  111. }
  112. }
  113. }