MethodReport.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ulong counter = 0;
  57. switch (type)
  58. {
  59. case ReportCounterType.Zero:
  60. counter = 0;
  61. break;
  62. case ReportCounterType.SamplesPassed:
  63. counter = _context.Renderer.GetCounter(CounterType.SamplesPassed);
  64. break;
  65. case ReportCounterType.PrimitivesGenerated:
  66. counter = _context.Renderer.GetCounter(CounterType.PrimitivesGenerated);
  67. break;
  68. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  69. counter = _context.Renderer.GetCounter(CounterType.TransformFeedbackPrimitivesWritten);
  70. break;
  71. }
  72. ulong ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  73. if (GraphicsConfig.FastGpuTime)
  74. {
  75. // Divide by some amount to report time as if operations were performed faster than they really are.
  76. // This can prevent some games from switching to a lower resolution because rendering is too slow.
  77. ticks /= 256;
  78. }
  79. counterData.Counter = counter;
  80. counterData.Timestamp = ticks;
  81. Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);
  82. Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);
  83. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  84. _context.MemoryAccessor.Write(rs.Address.Pack(), data);
  85. _counterCache.AddOrUpdate(rs.Address.Pack());
  86. }
  87. /// <summary>
  88. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  89. /// </summary>
  90. /// <remarks>
  91. /// The frequency is 614400000 Hz.
  92. /// </remarks>
  93. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  94. /// <returns>Maxwell ticks</returns>
  95. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  96. {
  97. // We need to divide first to avoid overflows.
  98. // We fix up the result later by calculating the difference and adding
  99. // that to the result.
  100. ulong divided = nanoseconds / NsToTicksFractionDenominator;
  101. ulong rounded = divided * NsToTicksFractionDenominator;
  102. ulong errorBias = (nanoseconds - rounded) * NsToTicksFractionNumerator / NsToTicksFractionDenominator;
  103. return divided * NsToTicksFractionNumerator + errorBias;
  104. }
  105. }
  106. }