aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Graphics.OpenGL/Counters.cs
blob: e82a040f0e060cafe1590ea17d3976fc9199d55d (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
using OpenTK.Graphics.OpenGL;
using Ryujinx.Graphics.GAL;
using System;

namespace Ryujinx.Graphics.OpenGL
{
    class Counters
    {
        private int[] _queryObjects;

        private ulong[] _accumulatedCounters;

        public Counters()
        {
            int count = Enum.GetNames(typeof(CounterType)).Length;

            _queryObjects = new int[count];

            _accumulatedCounters = new ulong[count];
        }

        public void Initialize()
        {
            for (int index = 0; index < _queryObjects.Length; index++)
            {
                int handle = GL.GenQuery();

                _queryObjects[index] = handle;

                CounterType type = (CounterType)index;

                GL.BeginQuery(GetTarget(type), handle);
            }
        }

        public ulong GetCounter(CounterType type)
        {
            UpdateAccumulatedCounter(type);

            return _accumulatedCounters[(int)type];
        }

        public void ResetCounter(CounterType type)
        {
            UpdateAccumulatedCounter(type);

            _accumulatedCounters[(int)type] = 0;
        }

        private void UpdateAccumulatedCounter(CounterType type)
        {
            int handle = _queryObjects[(int)type];

            QueryTarget target = GetTarget(type);

            GL.EndQuery(target);

            GL.GetQueryObject(handle, GetQueryObjectParam.QueryResult, out long result);

            _accumulatedCounters[(int)type] += (ulong)result;

            GL.BeginQuery(target, handle);
        }

        private static QueryTarget GetTarget(CounterType type)
        {
            switch (type)
            {
                case CounterType.SamplesPassed:                      return QueryTarget.SamplesPassed;
                case CounterType.PrimitivesGenerated:                return QueryTarget.PrimitivesGenerated;
                case CounterType.TransformFeedbackPrimitivesWritten: return QueryTarget.TransformFeedbackPrimitivesWritten;
            }

            return QueryTarget.SamplesPassed;
        }
    }
}