MethodReport.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ulong _runningCounter;
  11. /// <summary>
  12. /// Writes a GPU counter to guest memory.
  13. /// </summary>
  14. /// <param name="state">Current GPU state</param>
  15. /// <param name="argument">Method call argument</param>
  16. private void Report(GpuState state, int argument)
  17. {
  18. ReportMode mode = (ReportMode)(argument & 3);
  19. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  20. switch (mode)
  21. {
  22. case ReportMode.Semaphore: ReportSemaphore(state); break;
  23. case ReportMode.Counter: ReportCounter(state, type); break;
  24. }
  25. }
  26. /// <summary>
  27. /// Writes a GPU semaphore value to guest memory.
  28. /// </summary>
  29. /// <param name="state">Current GPU state</param>
  30. private void ReportSemaphore(GpuState state)
  31. {
  32. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  33. _context.MemoryAccessor.Write(rs.Address.Pack(), rs.Payload);
  34. _context.AdvanceSequence();
  35. }
  36. /// <summary>
  37. /// Packed GPU counter data (including GPU timestamp) in memory.
  38. /// </summary>
  39. private struct CounterData
  40. {
  41. public ulong Counter;
  42. public ulong Timestamp;
  43. }
  44. /// <summary>
  45. /// Writes a GPU counter to guest memory.
  46. /// This also writes the current timestamp value.
  47. /// </summary>
  48. /// <param name="state">Current GPU state</param>
  49. /// <param name="type">Counter to be written to memory</param>
  50. private void ReportCounter(GpuState state, ReportCounterType type)
  51. {
  52. CounterData counterData = new CounterData();
  53. ulong counter = 0;
  54. switch (type)
  55. {
  56. case ReportCounterType.Zero:
  57. counter = 0;
  58. break;
  59. case ReportCounterType.SamplesPassed:
  60. counter = _context.Renderer.GetCounter(CounterType.SamplesPassed);
  61. break;
  62. case ReportCounterType.PrimitivesGenerated:
  63. counter = _context.Renderer.GetCounter(CounterType.PrimitivesGenerated);
  64. break;
  65. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  66. counter = _context.Renderer.GetCounter(CounterType.TransformFeedbackPrimitivesWritten);
  67. break;
  68. }
  69. ulong ticks;
  70. if (GraphicsConfig.FastGpuTime)
  71. {
  72. ticks = _runningCounter++;
  73. }
  74. else
  75. {
  76. ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  77. }
  78. counterData.Counter = counter;
  79. counterData.Timestamp = ticks;
  80. Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);
  81. Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);
  82. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  83. _context.MemoryAccessor.Write(rs.Address.Pack(), data);
  84. }
  85. /// <summary>
  86. /// Converts a nanoseconds timestamp value to Maxwell time ticks.
  87. /// The frequency is approximately 1.63Hz.
  88. /// </summary>
  89. /// <param name="nanoseconds">Timestamp in nanoseconds</param>
  90. /// <returns>Maxwell ticks</returns>
  91. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  92. {
  93. // We need to divide first to avoid overflows.
  94. // We fix up the result later by calculating the difference and adding
  95. // that to the result.
  96. ulong divided = nanoseconds / 625;
  97. ulong rounded = divided * 625;
  98. ulong errorBias = ((nanoseconds - rounded) * 384) / 625;
  99. return divided * 384 + errorBias;
  100. }
  101. }
  102. }