MethodReport.cs 4.8 KB

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