MethodReport.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Ryujinx.Common;
  2. using Ryujinx.Graphics.GAL;
  3. using Ryujinx.Graphics.Gpu.State;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. namespace Ryujinx.Graphics.Gpu.Engine
  7. {
  8. partial class Methods
  9. {
  10. private const int NsToTicksFractionNumerator = 384;
  11. private const int NsToTicksFractionDenominator = 625;
  12. private ulong _runningCounter;
  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. ReportMode mode = (ReportMode)(argument & 3);
  21. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  22. switch (mode)
  23. {
  24. case ReportMode.Semaphore: ReportSemaphore(state); break;
  25. case ReportMode.Counter: ReportCounter(state, type); break;
  26. }
  27. }
  28. /// <summary>
  29. /// Writes a GPU semaphore value to guest memory.
  30. /// </summary>
  31. /// <param name="state">Current GPU state</param>
  32. private void ReportSemaphore(GpuState state)
  33. {
  34. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  35. _context.MemoryAccessor.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. CounterData counterData = new CounterData();
  55. ulong counter = 0;
  56. switch (type)
  57. {
  58. case ReportCounterType.Zero:
  59. counter = 0;
  60. break;
  61. case ReportCounterType.SamplesPassed:
  62. counter = _context.Renderer.GetCounter(CounterType.SamplesPassed);
  63. break;
  64. case ReportCounterType.PrimitivesGenerated:
  65. counter = _context.Renderer.GetCounter(CounterType.PrimitivesGenerated);
  66. break;
  67. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  68. counter = _context.Renderer.GetCounter(CounterType.TransformFeedbackPrimitivesWritten);
  69. break;
  70. }
  71. ulong ticks;
  72. if (GraphicsConfig.FastGpuTime)
  73. {
  74. ticks = _runningCounter++;
  75. }
  76. else
  77. {
  78. ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  79. }
  80. counterData.Counter = counter;
  81. counterData.Timestamp = ticks;
  82. Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);
  83. Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);
  84. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  85. _context.MemoryAccessor.Write(rs.Address.Pack(), data);
  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. }