MethodReport.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. SemaphoreOperation op = (SemaphoreOperation)(argument & 3);
  22. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  23. switch (op)
  24. {
  25. case SemaphoreOperation.Release: ReleaseSemaphore(state); break;
  26. case SemaphoreOperation.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<SemaphoreState>(MethodOffset.ReportState);
  36. _context.MemoryManager.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. var rs = state.Get<SemaphoreState>(MethodOffset.ReportState);
  56. ulong gpuVa = rs.Address.Pack();
  57. ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  58. if (GraphicsConfig.FastGpuTime)
  59. {
  60. // Divide by some amount to report time as if operations were performed faster than they really are.
  61. // This can prevent some games from switching to a lower resolution because rendering is too slow.
  62. ticks /= 256;
  63. }
  64. ICounterEvent counter = null;
  65. EventHandler<ulong> resultHandler = (object evt, ulong result) =>
  66. {
  67. CounterData counterData = new CounterData();
  68. counterData.Counter = result;
  69. counterData.Timestamp = ticks;
  70. if (counter?.Invalid != true)
  71. {
  72. _context.MemoryManager.Write(gpuVa, counterData);
  73. }
  74. };
  75. switch (type)
  76. {
  77. case ReportCounterType.Zero:
  78. resultHandler(null, 0);
  79. break;
  80. case ReportCounterType.SamplesPassed:
  81. counter = _context.Renderer.ReportCounter(CounterType.SamplesPassed, resultHandler);
  82. break;
  83. case ReportCounterType.PrimitivesGenerated:
  84. counter = _context.Renderer.ReportCounter(CounterType.PrimitivesGenerated, resultHandler);
  85. break;
  86. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  87. counter = _context.Renderer.ReportCounter(CounterType.TransformFeedbackPrimitivesWritten, resultHandler);
  88. break;
  89. }
  90. _counterCache.AddOrUpdate(gpuVa, counter);
  91. }
  92. /// <summary>
  93. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  94. /// </summary>
  95. /// <remarks>
  96. /// The frequency is 614400000 Hz.
  97. /// </remarks>
  98. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  99. /// <returns>Maxwell ticks</returns>
  100. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  101. {
  102. // We need to divide first to avoid overflows.
  103. // We fix up the result later by calculating the difference and adding
  104. // that to the result.
  105. ulong divided = nanoseconds / NsToTicksFractionDenominator;
  106. ulong rounded = divided * NsToTicksFractionDenominator;
  107. ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
  108. return divided * NsToTicksFractionNumerator + errorBias;
  109. }
  110. }
  111. }