MethodReport.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. private void Report(GpuState state, int argument)
  12. {
  13. ReportMode mode = (ReportMode)(argument & 3);
  14. ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);
  15. switch (mode)
  16. {
  17. case ReportMode.Semaphore: ReportSemaphore(state); break;
  18. case ReportMode.Counter: ReportCounter(state, type); break;
  19. }
  20. }
  21. private void ReportSemaphore(GpuState state)
  22. {
  23. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  24. _context.MemoryAccessor.Write(rs.Address.Pack(), rs.Payload);
  25. _context.AdvanceSequence();
  26. }
  27. private struct CounterData
  28. {
  29. public ulong Counter;
  30. public ulong Timestamp;
  31. }
  32. private void ReportCounter(GpuState state, ReportCounterType type)
  33. {
  34. CounterData counterData = new CounterData();
  35. ulong counter = 0;
  36. switch (type)
  37. {
  38. case ReportCounterType.Zero:
  39. counter = 0;
  40. break;
  41. case ReportCounterType.SamplesPassed:
  42. counter = _context.Renderer.GetCounter(CounterType.SamplesPassed);
  43. break;
  44. case ReportCounterType.PrimitivesGenerated:
  45. counter = _context.Renderer.GetCounter(CounterType.PrimitivesGenerated);
  46. break;
  47. case ReportCounterType.TransformFeedbackPrimitivesWritten:
  48. counter = _context.Renderer.GetCounter(CounterType.TransformFeedbackPrimitivesWritten);
  49. break;
  50. }
  51. ulong ticks;
  52. if (GraphicsConfig.FastGpuTime)
  53. {
  54. ticks = _runningCounter++;
  55. }
  56. else
  57. {
  58. ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
  59. }
  60. counterData.Counter = counter;
  61. counterData.Timestamp = ticks;
  62. Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);
  63. Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);
  64. var rs = state.Get<ReportState>(MethodOffset.ReportState);
  65. _context.MemoryAccessor.Write(rs.Address.Pack(), data);
  66. }
  67. private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
  68. {
  69. // We need to divide first to avoid overflows.
  70. // We fix up the result later by calculating the difference and adding
  71. // that to the result.
  72. ulong divided = nanoseconds / 625;
  73. ulong rounded = divided * 625;
  74. ulong errorBias = ((nanoseconds - rounded) * 384) / 625;
  75. return divided * 384 + errorBias;
  76. }
  77. }
  78. }