aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.Gpu/Engine/MethodReport.cs
blob: dd022eb2cd67ab4f863f3f0d3ca07d97e44112df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using Ryujinx.Common;
using Ryujinx.Graphics.GAL;
using Ryujinx.Graphics.Gpu.State;
using System;
using System.Runtime.InteropServices;

namespace Ryujinx.Graphics.Gpu.Engine
{
    partial class Methods
    {
        private ulong _runningCounter;

        private void Report(int argument)
        {
            ReportMode mode = (ReportMode)(argument & 3);

            ReportCounterType type = (ReportCounterType)((argument >> 23) & 0x1f);

            switch (mode)
            {
                case ReportMode.Semaphore: ReportSemaphore();   break;
                case ReportMode.Counter:   ReportCounter(type); break;
            }
        }

        private void ReportSemaphore()
        {
            var state = _context.State.Get<ReportState>(MethodOffset.ReportState);

            _context.MemoryAccessor.Write(state.Address.Pack(), state.Payload);

            _context.AdvanceSequence();
        }

        private struct CounterData
        {
            public ulong Counter;
            public ulong Timestamp;
        }

        private void ReportCounter(ReportCounterType type)
        {
            CounterData counterData = new CounterData();

            ulong counter = 0;

            switch (type)
            {
                case ReportCounterType.Zero:
                    counter = 0;
                    break;
                case ReportCounterType.SamplesPassed:
                    counter = _context.Renderer.GetCounter(CounterType.SamplesPassed);
                    break;
                case ReportCounterType.PrimitivesGenerated:
                    counter = _context.Renderer.GetCounter(CounterType.PrimitivesGenerated);
                    break;
                case ReportCounterType.TransformFeedbackPrimitivesWritten:
                    counter = _context.Renderer.GetCounter(CounterType.TransformFeedbackPrimitivesWritten);
                    break;
            }

            ulong ticks;

            if (GraphicsConfig.FastGpuTime)
            {
                ticks = _runningCounter++;
            }
            else
            {
                ticks = ConvertNanosecondsToTicks((ulong)PerformanceCounter.ElapsedNanoseconds);
            }

            counterData.Counter   = counter;
            counterData.Timestamp = ticks;

            Span<CounterData> counterDataSpan = MemoryMarshal.CreateSpan(ref counterData, 1);

            Span<byte> data = MemoryMarshal.Cast<CounterData, byte>(counterDataSpan);

            var state = _context.State.Get<ReportState>(MethodOffset.ReportState);

            _context.MemoryAccessor.Write(state.Address.Pack(), data);
        }

        private static ulong ConvertNanosecondsToTicks(ulong nanoseconds)
        {
            // We need to divide first to avoid overflows.
            // We fix up the result later by calculating the difference and adding
            // that to the result.
            ulong divided = nanoseconds / 625;

            ulong rounded = divided * 625;

            ulong errorBias = ((nanoseconds - rounded) * 384) / 625;

            return divided * 384 + errorBias;
        }
    }
}