Debian Codegraph @

Source/Core/VideoCommon/PerformanceMetrics.cpp

raw symbols SCIP
1 // Copyright 2022 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/PerformanceMetrics.h"
5
6 #include <algorithm>
7
8 #include <imgui.h>
9 #include <implot.h>
10
11 #include "Common/HookableEvent.h"
12 #include "Core/Config/GraphicsSettings.h"
13 #include "Core/Core.h"
14 #include "VideoCommon/FramebufferManager.h"
15 #include "VideoCommon/VideoConfig.h"
16
17 PerformanceMetrics::PerformanceMetrics()
18 {
19 const auto invalidate_counters_last_time = [this](Core::State) {
20 m_fps_counter.InvalidateLastTime();
21 m_vps_counter.InvalidateLastTime();
22 };
23 m_state_change_hook = Core::AddOnStateChangedCallback(invalidate_counters_last_time);
24 }
25
26 void PerformanceMetrics::Reset()
27 {
28 m_fps_counter.Reset();
29 m_vps_counter.Reset();
30
31 m_time_sleeping = DT::zero();
32 m_samples = {};
33
34 m_speed = 0;
35 m_max_speed = 0;
36
37 m_frame_presentation_offset = DT{};
38 }
39
40 void PerformanceMetrics::CountFrame()
41 {
42 m_fps_counter.Count();
43 }
44
45 void PerformanceMetrics::CountVBlank()
46 {
47 m_vps_counter.Count();
48 }
49
50 void PerformanceMetrics::CountThrottleSleep(DT sleep)
51 {
52 m_time_sleeping += sleep;
53 }
54
55 void PerformanceMetrics::AdjustClockSpeed(s64 ticks, u32 new_ppc_clock, u32 old_ppc_clock)
56 {
57 for (auto& sample : m_samples)
58 {
59 const s64 diff = (sample.core_ticks - ticks) * new_ppc_clock / old_ppc_clock;
60 sample.core_ticks = ticks + diff;
61 }
62 }
63
64 void PerformanceMetrics::CountPerformanceMarker(s64 core_ticks, u32 ticks_per_second)
65 {
66 const auto clock_time = Clock::now();
67 const auto work_time = clock_time - m_time_sleeping;
68
69 m_samples.emplace_back(
70 PerfSample{.clock_time = clock_time, .work_time = work_time, .core_ticks = core_ticks});
71
72 const auto sample_window = std::chrono::microseconds{g_ActiveConfig.iPerfSampleUSec};
73 while (clock_time - m_samples.front().clock_time > sample_window)
74 m_samples.pop_front();
75
76 // Avoid division by zero when we just have one sample.
77 if (m_samples.size() < 2)
78 return;
79
80 const PerfSample& oldest = m_samples.front();
81 const auto elapsed_core_time = DT_s(core_ticks - oldest.core_ticks) / ticks_per_second;
82
83 m_speed.store(elapsed_core_time / (clock_time - oldest.clock_time), std::memory_order_relaxed);
84 m_max_speed.store(elapsed_core_time / (work_time - oldest.work_time), std::memory_order_relaxed);
85 }
86
87 double PerformanceMetrics::GetFPS() const
88 {
89 return m_fps_counter.GetHzAvg();
90 }
91
92 u32 PerformanceMetrics::GetEFBWidth() const
93 {
94 if (g_framebuffer_manager)
95 return g_framebuffer_manager->GetEFBWidth();
96 return 0;
97 }
98
99 u32 PerformanceMetrics::GetEFBHeight() const
100 {
101 if (g_framebuffer_manager)
102 return g_framebuffer_manager->GetEFBHeight();
103 return 0;
104 }
105
106 double PerformanceMetrics::GetVPS() const
107 {
108 return m_vps_counter.GetHzAvg();
109 }
110
111 double PerformanceMetrics::GetSpeed() const
112 {
113 return m_speed.load(std::memory_order_relaxed);
114 }
115
116 double PerformanceMetrics::GetMaxSpeed() const
117 {
118 return m_max_speed.load(std::memory_order_relaxed);
119 }
120
121 void PerformanceMetrics::SetLatestFramePresentationOffset(DT offset)
122 {
123 m_frame_presentation_offset.store(offset, std::memory_order_relaxed);
124 }
125
126 void PerformanceMetrics::DrawImGuiStats(const float backbuffer_scale)
127 {
128 m_vps_counter.UpdateStats();
129 m_fps_counter.UpdateStats();
130
131 const bool movable_overlays = Config::Get(Config::GFX_MOVABLE_PERFORMANCE_METRICS);
132 const int movable_flag = movable_overlays ? ImGuiWindowFlags_None : ImGuiWindowFlags_NoMove;
133
134 const float bg_alpha = 0.7f;
135 const auto imgui_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings |
136 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav | movable_flag |
137 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoFocusOnAppearing;
138
139 const double fps = GetFPS();
140 const double vps = GetVPS();
141 const double speed = GetSpeed();
142 const u32 width = GetEFBWidth();
143 const u32 height = GetEFBHeight();
144
145 static ImVec2 last_display_size(-1.0f, -1.0f);
146
147 // Change Color based on % Speed
148 float r = 0.0f, g = 1.0f, b = 1.0f;
149 if (g_ActiveConfig.bShowSpeedColors)
150 {
151 r = 1.0 - (speed - 0.8) / 0.2;
152 g = speed / 0.8;
153 b = (speed - 0.9) / 0.1;
154 }
155
156 const float window_padding = 8.f * backbuffer_scale;
157
158 const ImVec2& display_size = ImGui::GetIO().DisplaySize;
159 const bool display_size_changed =
160 display_size.x != last_display_size.x || display_size.y != last_display_size.y;
161 last_display_size = display_size;
162 // There are too many edge cases to reasonably handle when the display size changes, so just reset
163 // the layout to default. Hopefully users aren't changing window sizes or resolutions too often.
164 const ImGuiCond set_next_position_condition =
165 (display_size_changed || !movable_overlays) ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
166 // Reset the graph size when changing resolutions, and otherwise let the user manually resize it.
167 const ImGuiCond set_next_size_condition =
168 display_size_changed ? ImGuiCond_Always : ImGuiCond_FirstUseEver;
169
170 float window_y = window_padding;
171 float window_x = display_size.x - window_padding;
172
173 const auto clamp_window_position = [&] {
174 const ImVec2 position = ImGui::GetWindowPos();
175 const ImVec2 size = ImGui::GetWindowSize();
176 const float window_min_x = window_padding;
177 const float window_max_x = display_size.x - window_padding - size.x;
178 const float window_min_y = window_padding;
179 const float window_max_y = display_size.y - window_padding - size.y;
180
181 if (window_min_x > window_max_x || window_min_y > window_max_y)
182 return;
183
184 const float clamped_window_x = std::clamp(position.x, window_min_x, window_max_x);
185 const float clamped_window_y = std::clamp(position.y, window_min_y, window_max_y);
186 const bool window_needs_clamping =
187 (clamped_window_x != position.x) || (clamped_window_y != position.y);
188
189 if (window_needs_clamping)
190 ImGui::SetWindowPos(ImVec2(clamped_window_x, clamped_window_y), ImGuiCond_Always);
191 };
192
193 const float min_auto_graph_width = 200.f * backbuffer_scale + 2.f * window_padding;
194 const float min_auto_graph_height = 144.f * backbuffer_scale + 2.f * window_padding;
195
196 const float graph_width = std::max(min_auto_graph_width, display_size.x / 4.f);
197 const float graph_height = std::max(min_auto_graph_height, display_size.y / 4.f);
198
199 const bool stack_vertically = !g_ActiveConfig.bShowGraphs;
200
201 ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.f);
202 ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 14.f * backbuffer_scale);
203 if (g_ActiveConfig.bShowGraphs)
204 {
205 // A font size of 13 is small enough to keep the tick numbers from overlapping too much.
206 ImGui::PushFont(NULL, 13.0f);
207 ImGui::PushStyleColor(ImGuiCol_ResizeGrip, 0);
208 const auto graph_flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoSavedSettings |
209 ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoNav | movable_flag |
210 ImGuiWindowFlags_NoFocusOnAppearing;
211 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 4.f * backbuffer_scale));
212
213 // Position in the top-right corner of the screen.
214 ImGui::SetNextWindowPos(ImVec2(window_x, window_y), set_next_position_condition,
215 ImVec2(1.0f, 0.0f));
216 ImGui::SetNextWindowSize(ImVec2(graph_width, graph_height), set_next_size_condition);
217 ImGui::SetNextWindowBgAlpha(bg_alpha);
218 if (ImGui::Begin("PerformanceGraphs", nullptr, graph_flags))
219 {
220 static constexpr std::size_t num_ticks = 17;
221 static constexpr std::array<double, num_ticks> tick_marks = {0.0,
222 1000.0 / 360.0,
223 1000.0 / 240.0,
224 1000.0 / 180.0,
225 1000.0 / 120.0,
226 1000.0 / 90.00,
227 1000.0 / 59.94,
228 1000.0 / 40.00,
229 1000.0 / 29.97,
230 1000.0 / 24.00,
231 1000.0 / 20.00,
232 1000.0 / 15.00,
233 1000.0 / 10.00,
234 1000.0 / 5.000,
235 1000.0 / 2.000,
236 1000.0,
237 2000.0};
238
239 clamp_window_position();
240 window_y += ImGui::GetWindowHeight() + window_padding;
241
242 const DT vblank_time = m_vps_counter.GetDtAvg() + 2 * m_vps_counter.GetDtStd();
243 const DT frame_time = m_fps_counter.GetDtAvg() + 2 * m_fps_counter.GetDtStd();
244 const double target_max_time = DT_ms(vblank_time + frame_time).count();
245 const double a =
246 std::max(0.0, 1.0 - std::exp(-4.0 * (DT_s(m_fps_counter.GetLastRawDt()) /
247 DT_s(m_fps_counter.GetSampleWindow()))));
248
249 if (std::isfinite(m_graph_max_time))
250 m_graph_max_time += a * (target_max_time - m_graph_max_time);
251 else
252 m_graph_max_time = target_max_time;
253
254 const double total_frame_time =
255 DT_ms(std::max(m_fps_counter.GetSampleWindow(), m_vps_counter.GetSampleWindow())).count();
256
257 if (ImPlot::BeginPlot("PerformanceGraphs", ImVec2(-1.0, -1.0),
258 ImPlotFlags_NoFrame | ImPlotFlags_NoTitle | ImPlotFlags_NoMenus |
259 ImPlotFlags_NoInputs))
260 {
261 ImPlot::PushStyleColor(ImPlotCol_PlotBg, {0, 0, 0, 0});
262 ImPlot::PushStyleColor(ImPlotCol_LegendBg, {0, 0, 0, 0.2f});
263 ImPlot::PushStyleVar(ImPlotStyleVar_FitPadding, ImVec2(0.f, 0.f));
264 ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, 1.5f * backbuffer_scale);
265 ImPlot::SetupAxes(nullptr, nullptr,
266 ImPlotAxisFlags_Lock | ImPlotAxisFlags_Invert |
267 ImPlotAxisFlags_NoDecorations | ImPlotAxisFlags_NoHighlight,
268 ImPlotAxisFlags_Lock | ImPlotAxisFlags_Invert | ImPlotAxisFlags_NoLabel |
269 ImPlotAxisFlags_NoHighlight);
270 ImPlot::SetupAxisFormat(ImAxis_Y1, "%.1f");
271 ImPlot::SetupAxisTicks(ImAxis_Y1, tick_marks.data(), num_ticks);
272 ImPlot::SetupAxesLimits(0, total_frame_time, 0, m_graph_max_time, ImGuiCond_Always);
273 ImPlot::SetupLegend(ImPlotLocation_SouthEast, ImPlotLegendFlags_None);
274 m_vps_counter.ImPlotPlotLines("V-Blank (ms)");
275 m_fps_counter.ImPlotPlotLines("Frame (ms)");
276 ImPlot::EndPlot();
277 ImPlot::PopStyleVar(2);
278 ImPlot::PopStyleColor(2);
279 }
280 ImGui::PopStyleVar();
281 }
282 ImGui::End();
283 ImGui::PopFont();
284 ImGui::PopStyleColor();
285 }
286
287 if (g_ActiveConfig.bShowSpeed)
288 {
289 // Position in the top-right corner of the screen.
290 ImGui::SetNextWindowPos(ImVec2(window_x, window_y), set_next_position_condition,
291 ImVec2(1.0f, 0.0f));
292 ImGui::SetNextWindowBgAlpha(bg_alpha);
293
294 if (ImGui::Begin("SpeedStats", nullptr, imgui_flags))
295 {
296 if (stack_vertically)
297 window_y += ImGui::GetWindowHeight() + window_padding;
298 else
299 window_x -= ImGui::GetWindowWidth() + window_padding;
300 clamp_window_position();
301 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "Speed:%4.0lf%%", 100.0 * speed);
302 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "Max:%6.0lf%%", 100.0 * GetMaxSpeed());
303 }
304 ImGui::End();
305 }
306
307 if (g_ActiveConfig.bShowFPS || g_ActiveConfig.bShowFTimes)
308 {
309 // Position in the top-right corner of the screen.
310 ImGui::SetNextWindowPos(ImVec2(window_x, window_y), set_next_position_condition,
311 ImVec2(1.0f, 0.0f));
312 ImGui::SetNextWindowBgAlpha(bg_alpha);
313
314 if (ImGui::Begin("FPSStats", nullptr, imgui_flags))
315 {
316 if (stack_vertically)
317 window_y += ImGui::GetWindowHeight() + window_padding;
318 else
319 window_x -= ImGui::GetWindowWidth() + window_padding;
320 clamp_window_position();
321 if (g_ActiveConfig.bShowFPS)
322 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "FPS:%7.2lf", fps);
323 if (g_ActiveConfig.bShowFTimes)
324 {
325 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "dt:%6.2lfms",
326 DT_ms(m_fps_counter.GetDtAvg()).count());
327 ImGui::TextColored(ImVec4(r, g, b, 1.0f), " ±:%6.2lfms",
328 DT_ms(m_fps_counter.GetDtStd()).count());
329
330 const auto offset =
331 DT_ms(m_frame_presentation_offset.load(std::memory_order_relaxed)).count();
332 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "ofs:%5.1lfms", offset);
333 }
334 }
335 ImGui::End();
336 }
337
338 if (g_ActiveConfig.bShowInternalResolution)
339 {
340 ImGui::SetNextWindowPos(ImVec2(window_x, window_y), set_next_position_condition,
341 ImVec2(1.0f, 0.0f));
342 ImGui::SetNextWindowBgAlpha(bg_alpha);
343
344 if (ImGui::Begin("ResolutionStats", nullptr, imgui_flags))
345 {
346 if (stack_vertically)
347 window_y += ImGui::GetWindowHeight() + window_padding;
348 else
349 window_x -= ImGui::GetWindowWidth() + window_padding;
350
351 clamp_window_position();
352
353 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "Res: %ux%u", width, height);
354 }
355 ImGui::End();
356 }
357
358 if (g_ActiveConfig.bShowVPS || g_ActiveConfig.bShowVTimes)
359 {
360 // Position in the top-right corner of the screen.
361 ImGui::SetNextWindowPos(ImVec2(window_x, window_y), set_next_position_condition,
362 ImVec2(1.0f, 0.0f));
363 ImGui::SetNextWindowBgAlpha(bg_alpha);
364
365 if (ImGui::Begin("VPSStats", nullptr, imgui_flags))
366 {
367 if (stack_vertically)
368 window_y += ImGui::GetWindowHeight() + window_padding;
369 else
370 window_x -= ImGui::GetWindowWidth() + window_padding;
371 clamp_window_position();
372 if (g_ActiveConfig.bShowVPS)
373 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "VPS:%7.2lf", vps);
374 if (g_ActiveConfig.bShowVTimes)
375 {
376 ImGui::TextColored(ImVec4(r, g, b, 1.0f), "dt:%6.2lfms",
377 DT_ms(m_vps_counter.GetDtAvg()).count());
378 ImGui::TextColored(ImVec4(r, g, b, 1.0f), " ±:%6.2lfms",
379 DT_ms(m_vps_counter.GetDtStd()).count());
380 }
381 }
382 ImGui::End();
383 }
384
385 ImGui::PopStyleVar(2);
386 }