Debian Codegraph @

Source/Core/VideoBackends/OGL/OGLPerfQuery.cpp

raw symbols SCIP
1 // Copyright 2012 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoBackends/OGL/OGLPerfQuery.h"
5
6 #include <memory>
7
8 #include "Common/CommonTypes.h"
9 #include "Common/GL/GLExtensions/GLExtensions.h"
10
11 #include "VideoCommon/FramebufferManager.h"
12 #include "VideoCommon/VideoCommon.h"
13 #include "VideoCommon/VideoConfig.h"
14
15 namespace OGL
16 {
17 std::unique_ptr<PerfQueryBase> GetPerfQuery(bool is_gles)
18 {
19 if (is_gles && GLExtensions::Supports("GL_NV_occlusion_query_samples"))
20 return std::make_unique<PerfQueryGLESNV>();
21 else if (is_gles)
22 return std::make_unique<PerfQueryGL>(GL_ANY_SAMPLES_PASSED);
23 else
24 return std::make_unique<PerfQueryGL>(GL_SAMPLES_PASSED);
25 }
26
27 PerfQuery::PerfQuery() : m_query_read_pos()
28 {
29 ResetQuery();
30 }
31
32 void PerfQuery::EnableQuery(PerfQueryGroup group)
33 {
34 m_query->EnableQuery(group);
35 }
36
37 void PerfQuery::DisableQuery(PerfQueryGroup group)
38 {
39 m_query->DisableQuery(group);
40 }
41
42 bool PerfQuery::IsFlushed() const
43 {
44 return m_query_count.load(std::memory_order_relaxed) == 0;
45 }
46
47 // TODO: could selectively flush things, but I don't think that will do much
48 void PerfQuery::FlushResults()
49 {
50 m_query->FlushResults();
51 }
52
53 void PerfQuery::ResetQuery()
54 {
55 m_query_count.store(0, std::memory_order_relaxed);
56 for (auto& result : m_results)
57 result.store(0, std::memory_order_relaxed);
58 }
59
60 u32 PerfQuery::GetQueryResult(PerfQueryType type)
61 {
62 u32 result = 0;
63
64 if (type == PQ_ZCOMP_INPUT_ZCOMPLOC || type == PQ_ZCOMP_OUTPUT_ZCOMPLOC)
65 {
66 result = m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
67 }
68 else if (type == PQ_ZCOMP_INPUT || type == PQ_ZCOMP_OUTPUT)
69 {
70 result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed);
71 }
72 else if (type == PQ_BLEND_INPUT)
73 {
74 result = m_results[PQG_ZCOMP].load(std::memory_order_relaxed) +
75 m_results[PQG_ZCOMP_ZCOMPLOC].load(std::memory_order_relaxed);
76 }
77 else if (type == PQ_EFB_COPY_CLOCKS)
78 {
79 result = m_results[PQG_EFB_COPY_CLOCKS].load(std::memory_order_relaxed);
80 }
81
82 return result / 4;
83 }
84
85 // Implementations
86 PerfQueryGL::PerfQueryGL(GLenum query_type) : m_query_type(query_type)
87 {
88 for (ActiveQuery& query : m_query_buffer)
89 glGenQueries(1, &query.query_id);
90 }
91
92 PerfQueryGL::~PerfQueryGL()
93 {
94 for (ActiveQuery& query : m_query_buffer)
95 glDeleteQueries(1, &query.query_id);
96 }
97
98 void PerfQueryGL::EnableQuery(PerfQueryGroup group)
99 {
100 u32 query_count = m_query_count.load(std::memory_order_relaxed);
101
102 // Is this sane?
103 if (query_count > m_query_buffer.size() / 2)
104 {
105 WeakFlush();
106 query_count = m_query_count.load(std::memory_order_relaxed);
107 }
108
109 if (m_query_buffer.size() == query_count)
110 {
111 FlushOne();
112 query_count = m_query_count.load(std::memory_order_relaxed);
113 // ERROR_LOG_FMT(VIDEO, "Flushed query buffer early!");
114 }
115
116 // start query
117 if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
118 {
119 auto& entry = m_query_buffer[(m_query_read_pos + query_count) % m_query_buffer.size()];
120
121 glBeginQuery(m_query_type, entry.query_id);
122 entry.query_group = group;
123
124 m_query_count.fetch_add(1, std::memory_order_relaxed);
125 }
126 }
127 void PerfQueryGL::DisableQuery(PerfQueryGroup group)
128 {
129 // stop query
130 if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
131 {
132 glEndQuery(m_query_type);
133 }
134 }
135
136 void PerfQueryGL::WeakFlush()
137 {
138 while (!IsFlushed())
139 {
140 auto& entry = m_query_buffer[m_query_read_pos];
141
142 GLuint result = GL_FALSE;
143 glGetQueryObjectuiv(entry.query_id, GL_QUERY_RESULT_AVAILABLE, &result);
144
145 if (GL_TRUE == result)
146 {
147 FlushOne();
148 }
149 else
150 {
151 break;
152 }
153 }
154 }
155
156 void PerfQueryGL::FlushOne()
157 {
158 auto& entry = m_query_buffer[m_query_read_pos];
159
160 GLuint result = 0;
161 glGetQueryObjectuiv(entry.query_id, GL_QUERY_RESULT, &result);
162
163 // NOTE: Reported pixel metrics should be referenced to native resolution
164 // TODO: Dropping the lower 2 bits from this count should be closer to actual
165 // hardware behavior when drawing triangles.
166 result = static_cast<u64>(result) * EFB_WIDTH * EFB_HEIGHT /
167 (g_framebuffer_manager->GetEFBWidth() * g_framebuffer_manager->GetEFBHeight());
168
169 // Adjust for multisampling
170 if (g_ActiveConfig.iMultisamples > 1)
171 result /= g_ActiveConfig.iMultisamples;
172
173 m_results[entry.query_group].fetch_add(result, std::memory_order_relaxed);
174
175 m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
176 m_query_count.fetch_sub(1, std::memory_order_relaxed);
177 }
178
179 // TODO: could selectively flush things, but I don't think that will do much
180 void PerfQueryGL::FlushResults()
181 {
182 while (!IsFlushed())
183 FlushOne();
184 }
185
186 PerfQueryGLESNV::PerfQueryGLESNV()
187 {
188 for (ActiveQuery& query : m_query_buffer)
189 glGenOcclusionQueriesNV(1, &query.query_id);
190 }
191
192 PerfQueryGLESNV::~PerfQueryGLESNV()
193 {
194 for (ActiveQuery& query : m_query_buffer)
195 glDeleteOcclusionQueriesNV(1, &query.query_id);
196 }
197
198 void PerfQueryGLESNV::EnableQuery(PerfQueryGroup group)
199 {
200 u32 query_count = m_query_count.load(std::memory_order_relaxed);
201
202 // Is this sane?
203 if (query_count > m_query_buffer.size() / 2)
204 {
205 WeakFlush();
206 query_count = m_query_count.load(std::memory_order_relaxed);
207 }
208
209 if (m_query_buffer.size() == query_count)
210 {
211 FlushOne();
212 query_count = m_query_count.load(std::memory_order_relaxed);
213 // ERROR_LOG_FMT(VIDEO, "Flushed query buffer early!");
214 }
215
216 // start query
217 if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
218 {
219 auto& entry = m_query_buffer[(m_query_read_pos + query_count) % m_query_buffer.size()];
220
221 glBeginOcclusionQueryNV(entry.query_id);
222 entry.query_group = group;
223
224 m_query_count.fetch_add(1, std::memory_order_relaxed);
225 }
226 }
227 void PerfQueryGLESNV::DisableQuery(PerfQueryGroup group)
228 {
229 // stop query
230 if (group == PQG_ZCOMP_ZCOMPLOC || group == PQG_ZCOMP)
231 {
232 glEndOcclusionQueryNV();
233 }
234 }
235
236 void PerfQueryGLESNV::WeakFlush()
237 {
238 while (!IsFlushed())
239 {
240 auto& entry = m_query_buffer[m_query_read_pos];
241
242 GLuint result = GL_FALSE;
243 glGetOcclusionQueryuivNV(entry.query_id, GL_PIXEL_COUNT_AVAILABLE_NV, &result);
244
245 if (GL_TRUE == result)
246 {
247 FlushOne();
248 }
249 else
250 {
251 break;
252 }
253 }
254 }
255
256 void PerfQueryGLESNV::FlushOne()
257 {
258 auto& entry = m_query_buffer[m_query_read_pos];
259
260 GLuint result = 0;
261 glGetOcclusionQueryuivNV(entry.query_id, GL_OCCLUSION_TEST_RESULT_HP, &result);
262
263 // NOTE: Reported pixel metrics should be referenced to native resolution
264 // TODO: Dropping the lower 2 bits from this count should be closer to actual
265 // hardware behavior when drawing triangles.
266 const u64 native_res_result =
267 static_cast<u64>(result) * EFB_WIDTH * EFB_HEIGHT /
268 (g_framebuffer_manager->GetEFBWidth() * g_framebuffer_manager->GetEFBHeight());
269 m_results[entry.query_group].fetch_add(static_cast<u32>(native_res_result),
270 std::memory_order_relaxed);
271
272 m_query_read_pos = (m_query_read_pos + 1) % m_query_buffer.size();
273 m_query_count.fetch_sub(1, std::memory_order_relaxed);
274 }
275
276 // TODO: could selectively flush things, but I don't think that will do much
277 void PerfQueryGLESNV::FlushResults()
278 {
279 while (!IsFlushed())
280 FlushOne();
281 }
282
283 } // namespace OGL