Debian Codegraph @

Source/Core/VideoBackends/OGL/GPUTimer.h

raw symbols SCIP
1 // Copyright 2016 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include "Common/GL/GLExtensions/GLExtensions.h"
7
8 #ifndef GL_TIME_ELAPSED
9 #define GL_TIME_ELAPSED 0x88BF
10 #endif
11
12 namespace OGL
13 {
14 /*
15 * This class can be used to measure the time it takes for the GPU to perform a draw call
16 * or compute dispatch. To use:
17 *
18 * - Create an instance of GPUTimer before issuing the draw call.
19 * (this can be before or after any binding that needs to be done)
20 *
21 * - (optionally) call Begin(). This is not needed for a single draw call.
22 *
23 * - Issue the draw call or compute dispatch as normal.
24 *
25 * - (optionally) call End(). This is not necessary for a single draw call.
26 *
27 * - Call GetTime{Seconds,Milliseconds,Nanoseconds} to determine how long the operation
28 * took to execute on the GPU.
29 *
30 * NOTE: When the timer is read back, this will force a GL flush, so the more often a timer is used,
31 * the larger of a performance impact it will have. Only one timer can be active at any time, due to
32 * using GL_TIME_ELAPSED. This is not enforced by the class, however.
33 *
34 */
35 class GPUTimer final
36 {
37 public:
38 GPUTimer()
39 {
40 glGenQueries(1, &m_query_id);
41 Begin();
42 }
43
44 ~GPUTimer()
45 {
46 End();
47 glDeleteQueries(1, &m_query_id);
48 }
49
50 void Begin()
51 {
52 if (m_started)
53 glEndQuery(GL_TIME_ELAPSED);
54
55 glBeginQuery(GL_TIME_ELAPSED, m_query_id);
56 m_started = true;
57 }
58
59 void End()
60 {
61 if (!m_started)
62 return;
63
64 glEndQuery(GL_TIME_ELAPSED);
65 m_started = false;
66 }
67
68 double GetTimeSeconds()
69 {
70 GetResult();
71 return static_cast<double>(m_result) / 1000000000.0;
72 }
73
74 double GetTimeMilliseconds()
75 {
76 GetResult();
77 return static_cast<double>(m_result) / 1000000.0;
78 }
79
80 u32 GetTimeNanoseconds()
81 {
82 GetResult();
83 return m_result;
84 }
85
86 private:
87 void GetResult()
88 {
89 if (m_has_result)
90 return;
91
92 if (m_started)
93 End();
94
95 glGetQueryObjectuiv(m_query_id, GL_QUERY_RESULT, &m_result);
96 m_has_result = true;
97 }
98
99 GLuint m_query_id;
100 GLuint m_result = 0;
101 bool m_started = false;
102 bool m_has_result = false;
103 };
104 } // namespace OGL