Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11212 from Sam-Belliveau/realtime-fps-and-vps-cou…
…nter

VideoCommon: Improve FPS/VPS Counting with Euler + Moving Averages and Colors
  • Loading branch information
AdmiralCurtiss committed Nov 22, 2022
2 parents 5e442f6 + edb2c90 commit 3145825
Show file tree
Hide file tree
Showing 15 changed files with 196 additions and 72 deletions.
Expand Up @@ -173,6 +173,9 @@ public enum BooleanSetting implements AbstractBooleanSetting
GFX_WIDESCREEN_HACK(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "wideScreenHack", false),
GFX_CROP(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "Crop", false),
GFX_SHOW_FPS(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "ShowFPS", false),
GFX_SHOW_VPS(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "ShowVPS", false),
GFX_SHOW_SPEED(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "ShowSpeed", false),
GFX_SHOW_SPEED_COLORS(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "ShowSpeedColors", true),
GFX_OVERLAY_STATS(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "OverlayStats", false),
GFX_DUMP_TEXTURES(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "DumpTextures", false),
GFX_DUMP_MIP_TEXTURES(Settings.FILE_GFX, Settings.SECTION_GFX_SETTINGS, "DumpMipTextures", false),
Expand Down
Expand Up @@ -690,6 +690,13 @@ private void addGraphicsSettings(ArrayList<SettingsItem> sl)
R.string.video_backend, 0, R.array.videoBackendEntries, R.array.videoBackendValues));
sl.add(new CheckBoxSetting(mContext, BooleanSetting.GFX_SHOW_FPS, R.string.show_fps,
R.string.show_fps_description));
sl.add(new CheckBoxSetting(mContext, BooleanSetting.GFX_SHOW_VPS, R.string.show_vps,
R.string.show_vps_description));
sl.add(new CheckBoxSetting(mContext, BooleanSetting.GFX_SHOW_SPEED, R.string.show_speed,
R.string.show_speed_description));
sl.add(new CheckBoxSetting(mContext, BooleanSetting.GFX_SHOW_SPEED_COLORS,
R.string.show_speed_colors,
R.string.show_speed_colors_description));
sl.add(new SingleChoiceSettingDynamicDescriptions(mContext,
IntSetting.GFX_SHADER_COMPILATION_MODE, R.string.shader_compilation_mode, 0,
R.array.shaderCompilationModeEntries, R.array.shaderCompilationModeValues,
Expand Down
9 changes: 7 additions & 2 deletions Source/Android/app/src/main/res/values/strings.xml
Expand Up @@ -246,8 +246,13 @@
<string name="video_backend">Video Backend</string>
<string name="video_backend_description">Select the API used for graphics rendering.</string>
<string name="show_fps">Show FPS</string>
<string name="show_fps_description">Show the number of frames rendered per second as a measure of emulation speed.</string>

<string name="show_fps_description">Shows the number of distinct frames rendered per second as a measure of visual smoothness.</string>
<string name="show_vps">Show VPS</string>
<string name="show_vps_description">Show the number of frames rendered per second as a measure of emulation speed.</string>
<string name="show_speed">Show % Speed</string>
<string name="show_speed_description">Shows the % speed of emulation compared to full speed.</string>
<string name="show_speed_colors">Show Speed Color</string>
<string name="show_speed_colors_description">Changes the color of the FPS counter depending on emulation speed.</string>
<string name="enhancements_submenu">Enhancements</string>
<string name="internal_resolution">Internal Resolution</string>
<string name="internal_resolution_description">Specifies the resolution used to render at. A high resolution will improve visual quality a lot but is also quite heavy on performance and might cause glitches in certain games.</string>
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Core/Config/GraphicsSettings.cpp
Expand Up @@ -27,6 +27,10 @@ const Info<bool> GFX_CROP{{System::GFX, "Settings", "Crop"}, false};
const Info<int> GFX_SAFE_TEXTURE_CACHE_COLOR_SAMPLES{
{System::GFX, "Settings", "SafeTextureCacheColorSamples"}, 128};
const Info<bool> GFX_SHOW_FPS{{System::GFX, "Settings", "ShowFPS"}, false};
const Info<bool> GFX_SHOW_VPS{{System::GFX, "Settings", "ShowVPS"}, false};
const Info<bool> GFX_SHOW_SPEED{{System::GFX, "Settings", "ShowSpeed"}, false};
const Info<bool> GFX_SHOW_SPEED_COLORS{{System::GFX, "Settings", "ShowSpeedColors"}, true};
const Info<int> GFX_PERF_SAMP_WINDOW{{System::GFX, "Settings", "PerfSampWindowMS"}, 1000};
const Info<bool> GFX_SHOW_NETPLAY_PING{{System::GFX, "Settings", "ShowNetPlayPing"}, false};
const Info<bool> GFX_SHOW_NETPLAY_MESSAGES{{System::GFX, "Settings", "ShowNetPlayMessages"}, false};
const Info<bool> GFX_LOG_RENDER_TIME_TO_FILE{{System::GFX, "Settings", "LogRenderTimeToFile"},
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Core/Config/GraphicsSettings.h
Expand Up @@ -29,6 +29,10 @@ extern const Info<AspectMode> GFX_SUGGESTED_ASPECT_RATIO;
extern const Info<bool> GFX_CROP;
extern const Info<int> GFX_SAFE_TEXTURE_CACHE_COLOR_SAMPLES;
extern const Info<bool> GFX_SHOW_FPS;
extern const Info<bool> GFX_SHOW_VPS;
extern const Info<bool> GFX_SHOW_SPEED;
extern const Info<bool> GFX_SHOW_SPEED_COLORS;
extern const Info<int> GFX_PERF_SAMP_WINDOW;
extern const Info<bool> GFX_SHOW_NETPLAY_PING;
extern const Info<bool> GFX_SHOW_NETPLAY_MESSAGES;
extern const Info<bool> GFX_LOG_RENDER_TIME_TO_FILE;
Expand Down
55 changes: 55 additions & 0 deletions Source/Core/DolphinQt/Config/Graphics/AdvancedWidget.cpp
Expand Up @@ -45,6 +45,27 @@ void AdvancedWidget::CreateWidgets()
{
auto* main_layout = new QVBoxLayout;

// Performance
auto* performance_box = new QGroupBox(tr("Performance Statistics"));
auto* performance_layout = new QGridLayout();
performance_box->setLayout(performance_layout);

m_show_fps = new GraphicsBool(tr("Show FPS"), Config::GFX_SHOW_FPS);
m_show_vps = new GraphicsBool(tr("Show VPS"), Config::GFX_SHOW_VPS);
m_show_speed = new GraphicsBool(tr("Show % Speed"), Config::GFX_SHOW_SPEED);
m_show_speed_colors = new GraphicsBool(tr("Show Speed Colors"), Config::GFX_SHOW_SPEED_COLORS);
m_perf_samp_window = new GraphicsInteger(0, 10000, Config::GFX_PERF_SAMP_WINDOW, 100);
m_log_render_time =
new GraphicsBool(tr("Log Render Time to File"), Config::GFX_LOG_RENDER_TIME_TO_FILE);

performance_layout->addWidget(m_show_fps, 0, 0);
performance_layout->addWidget(m_show_vps, 1, 0);
performance_layout->addWidget(m_show_speed, 0, 1);
performance_layout->addWidget(new QLabel(tr("Performance Sample Window (ms):")), 2, 0);
performance_layout->addWidget(m_perf_samp_window, 2, 1);
performance_layout->addWidget(m_log_render_time, 3, 0);
performance_layout->addWidget(m_show_speed_colors, 3, 1);

// Debugging
auto* debugging_box = new QGroupBox(tr("Debugging"));
auto* debugging_layout = new QGridLayout();
Expand Down Expand Up @@ -155,6 +176,7 @@ void AdvancedWidget::CreateWidgets()
experimental_layout->addWidget(m_defer_efb_access_invalidation, 0, 0);
experimental_layout->addWidget(m_manual_texture_sampling, 0, 1);

main_layout->addWidget(performance_box);
main_layout->addWidget(debugging_box);
main_layout->addWidget(utility_box);
main_layout->addWidget(texture_dump_box);
Expand Down Expand Up @@ -214,6 +236,32 @@ void AdvancedWidget::OnEmulationStateChanged(bool running)

void AdvancedWidget::AddDescriptions()
{
static const char TR_SHOW_FPS_DESCRIPTION[] =
QT_TR_NOOP("Shows the number of distinct frames rendered per second as a measure of "
"visual smoothness.<br><br><dolphin_emphasis>If unsure, leave this "
"unchecked.</dolphin_emphasis>");
static const char TR_SHOW_VPS_DESCRIPTION[] =
QT_TR_NOOP("Shows the number of frames rendered per second as a measure of "
"emulation speed.<br><br><dolphin_emphasis>If unsure, leave this "
"unchecked.</dolphin_emphasis>");
static const char TR_SHOW_SPEED_DESCRIPTION[] =
QT_TR_NOOP("Shows the % speed of emulation compared to full speed."
"<br><br><dolphin_emphasis>If unsure, leave this "
"unchecked.</dolphin_emphasis>");
static const char TR_SHOW_SPEED_COLORS_DESCRIPTION[] =
QT_TR_NOOP("Changes the color of the FPS counter depending on emulation speed."
"<br><br><dolphin_emphasis>If unsure, leave this "
"checked.</dolphin_emphasis>");
static const char TR_PERF_SAMP_WINDOW_DESCRIPTION[] =
QT_TR_NOOP("The amount of time the FPS and VPS counters will sample over."
"<br><br>The higher the value, the more stable the FPS/VPS counter will be, "
"but the slower it will be slower to update."
"<br><br><dolphin_emphasis>If unsure, leave this "
"at 1000ms.</dolphin_emphasis>");
static const char TR_LOG_RENDERTIME_DESCRIPTION[] = QT_TR_NOOP(
"Logs the render time of every frame to User/Logs/render_time.txt.<br><br>Use this "
"feature to measure Dolphin's performance.<br><br><dolphin_emphasis>If "
"unsure, leave this unchecked.</dolphin_emphasis>");
static const char TR_WIREFRAME_DESCRIPTION[] =
QT_TR_NOOP("Renders the scene as a wireframe.<br><br><dolphin_emphasis>If unsure, leave "
"this unchecked.</dolphin_emphasis>");
Expand Down Expand Up @@ -331,10 +379,17 @@ void AdvancedWidget::AddDescriptions()
static const char IF_UNSURE_UNCHECKED[] =
QT_TR_NOOP("<dolphin_emphasis>If unsure, leave this unchecked.</dolphin_emphasis>");

m_show_fps->SetDescription(tr(TR_SHOW_FPS_DESCRIPTION));
m_show_vps->SetDescription(tr(TR_SHOW_VPS_DESCRIPTION));
m_show_speed->SetDescription(tr(TR_SHOW_SPEED_DESCRIPTION));
m_log_render_time->SetDescription(tr(TR_LOG_RENDERTIME_DESCRIPTION));
m_show_speed_colors->SetDescription(tr(TR_SHOW_SPEED_COLORS_DESCRIPTION));

m_enable_wireframe->SetDescription(tr(TR_WIREFRAME_DESCRIPTION));
m_show_statistics->SetDescription(tr(TR_SHOW_STATS_DESCRIPTION));
m_enable_format_overlay->SetDescription(tr(TR_TEXTURE_FORMAT_DESCRIPTION));
m_enable_api_validation->SetDescription(tr(TR_VALIDATION_LAYER_DESCRIPTION));
m_perf_samp_window->SetDescription(tr(TR_PERF_SAMP_WINDOW_DESCRIPTION));
m_dump_textures->SetDescription(tr(TR_DUMP_TEXTURE_DESCRIPTION));
m_dump_mip_textures->SetDescription(tr(TR_DUMP_MIP_TEXTURE_DESCRIPTION));
m_dump_base_textures->SetDescription(tr(TR_DUMP_BASE_TEXTURE_DESCRIPTION));
Expand Down
6 changes: 6 additions & 0 deletions Source/Core/DolphinQt/Config/Graphics/AdvancedWidget.h
Expand Up @@ -35,6 +35,12 @@ class AdvancedWidget final : public GraphicsWidget
GraphicsBool* m_show_statistics;
GraphicsBool* m_enable_format_overlay;
GraphicsBool* m_enable_api_validation;
GraphicsBool* m_show_fps;
GraphicsBool* m_show_vps;
GraphicsBool* m_show_speed;
GraphicsBool* m_show_speed_colors;
GraphicsInteger* m_perf_samp_window;
GraphicsBool* m_log_render_time;

// Utility
GraphicsBool* m_prefetch_custom_textures;
Expand Down
26 changes: 4 additions & 22 deletions Source/Core/DolphinQt/Config/Graphics/GeneralWidget.cpp
Expand Up @@ -87,10 +87,7 @@ void GeneralWidget::CreateWidgets()
auto* m_options_box = new QGroupBox(tr("Other"));
auto* m_options_layout = new QGridLayout();

m_show_fps = new GraphicsBool(tr("Show FPS"), Config::GFX_SHOW_FPS);
m_show_ping = new GraphicsBool(tr("Show NetPlay Ping"), Config::GFX_SHOW_NETPLAY_PING);
m_log_render_time =
new GraphicsBool(tr("Log Render Time to File"), Config::GFX_LOG_RENDER_TIME_TO_FILE);
m_autoadjust_window_size =
new GraphicsBool(tr("Auto-Adjust Window Size"), Config::MAIN_RENDER_WINDOW_AUTOSIZE);
m_show_messages =
Expand All @@ -99,14 +96,11 @@ void GeneralWidget::CreateWidgets()

m_options_box->setLayout(m_options_layout);

m_options_layout->addWidget(m_show_fps, 0, 0);
m_options_layout->addWidget(m_log_render_time, 0, 1);
m_options_layout->addWidget(m_render_main_window, 0, 0);
m_options_layout->addWidget(m_autoadjust_window_size, 1, 0);

m_options_layout->addWidget(m_render_main_window, 1, 0);
m_options_layout->addWidget(m_autoadjust_window_size, 1, 1);

m_options_layout->addWidget(m_show_messages, 2, 0);
m_options_layout->addWidget(m_show_ping, 2, 1);
m_options_layout->addWidget(m_show_messages, 0, 1);
m_options_layout->addWidget(m_show_ping, 1, 1);

// Other
auto* shader_compilation_box = new QGroupBox(tr("Shader Compilation"));
Expand Down Expand Up @@ -226,17 +220,9 @@ void GeneralWidget::AddDescriptions()
"if emulation speed is below 100%.<br><br><dolphin_emphasis>If unsure, leave "
"this "
"unchecked.</dolphin_emphasis>");
static const char TR_SHOW_FPS_DESCRIPTION[] =
QT_TR_NOOP("Shows the number of frames rendered per second as a measure of "
"emulation speed.<br><br><dolphin_emphasis>If unsure, leave this "
"unchecked.</dolphin_emphasis>");
static const char TR_SHOW_NETPLAY_PING_DESCRIPTION[] = QT_TR_NOOP(
"Shows the player's maximum ping while playing on "
"NetPlay.<br><br><dolphin_emphasis>If unsure, leave this unchecked.</dolphin_emphasis>");
static const char TR_LOG_RENDERTIME_DESCRIPTION[] = QT_TR_NOOP(
"Logs the render time of every frame to User/Logs/render_time.txt.<br><br>Use this "
"feature to measure Dolphin's performance.<br><br><dolphin_emphasis>If "
"unsure, leave this unchecked.</dolphin_emphasis>");
static const char TR_SHOW_NETPLAY_MESSAGES_DESCRIPTION[] =
QT_TR_NOOP("Shows chat messages, buffer changes, and desync alerts "
"while playing NetPlay.<br><br><dolphin_emphasis>If unsure, leave "
Expand Down Expand Up @@ -281,12 +267,8 @@ void GeneralWidget::AddDescriptions()

m_enable_fullscreen->SetDescription(tr(TR_FULLSCREEN_DESCRIPTION));

m_show_fps->SetDescription(tr(TR_SHOW_FPS_DESCRIPTION));

m_show_ping->SetDescription(tr(TR_SHOW_NETPLAY_PING_DESCRIPTION));

m_log_render_time->SetDescription(tr(TR_LOG_RENDERTIME_DESCRIPTION));

m_autoadjust_window_size->SetDescription(tr(TR_AUTOSIZE_DESCRIPTION));

m_show_messages->SetDescription(tr(TR_SHOW_NETPLAY_MESSAGES_DESCRIPTION));
Expand Down
2 changes: 0 additions & 2 deletions Source/Core/DolphinQt/Config/Graphics/GeneralWidget.h
Expand Up @@ -49,9 +49,7 @@ class GeneralWidget final : public GraphicsWidget
GraphicsBool* m_enable_fullscreen;

// Options
GraphicsBool* m_show_fps;
GraphicsBool* m_show_ping;
GraphicsBool* m_log_render_time;
GraphicsBool* m_autoadjust_window_size;
GraphicsBool* m_show_messages;
GraphicsBool* m_render_main_window;
Expand Down
63 changes: 42 additions & 21 deletions Source/Core/VideoCommon/FPSCounter.cpp
Expand Up @@ -3,7 +3,7 @@

#include "VideoCommon/FPSCounter.h"

#include <fstream>
#include <cmath>
#include <iomanip>

#include "Common/CommonTypes.h"
Expand All @@ -12,11 +12,15 @@
#include "Core/Core.h"
#include "VideoCommon/VideoConfig.h"

static constexpr u64 FPS_REFRESH_INTERVAL_US = 250000;
static constexpr double US_TO_MS = 1000.0;
static constexpr double US_TO_S = 1000000.0;

FPSCounter::FPSCounter()
static constexpr double FPS_SAMPLE_RC_RATIO = 0.25;

FPSCounter::FPSCounter(const char* log_name)
{
m_last_time = Common::Timer::NowUs();
m_log_name = log_name;

m_on_state_changed_handle = Core::AddOnStateChangedCallback([this](Core::State state) {
if (state == Core::State::Paused)
Expand All @@ -31,47 +35,64 @@ FPSCounter::~FPSCounter()
Core::RemoveOnStateChangedCallback(&m_on_state_changed_handle);
}

void FPSCounter::LogRenderTimeToFile(u64 val)
void FPSCounter::LogRenderTimeToFile(s64 val)
{
if (!m_bench_file.is_open())
{
File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + "render_time.txt",
std::ios_base::out);
File::OpenFStream(m_bench_file, File::GetUserPath(D_LOGS_IDX) + m_log_name, std::ios_base::out);
}

m_bench_file << std::fixed << std::setprecision(8) << (val / 1000.0) << std::endl;
m_bench_file << std::fixed << std::setprecision(8) << (val / US_TO_MS) << std::endl;
}

void FPSCounter::Update()
{
const u64 time = Common::Timer::NowUs();
const u64 diff = time - m_last_time;
m_time_diff_secs = static_cast<double>(diff / 1000000.0);
if (g_ActiveConfig.bLogRenderTimeToFile)
LogRenderTimeToFile(diff);
if (m_paused)
return;

m_frame_counter++;
m_time_since_update += diff;
const s64 time = Common::Timer::NowUs();
const s64 diff = std::max<s64>(0, time - m_last_time);
const s64 window = std::max(1, g_ActiveConfig.iPerfSampleUSec);

m_raw_dt = diff / US_TO_S;
m_last_time = time;

if (m_time_since_update >= FPS_REFRESH_INTERVAL_US)
m_dt_total += diff;
m_dt_queue.push_back(diff);

while (window <= m_dt_total - m_dt_queue.front())
{
m_fps = m_frame_counter / (m_time_since_update / 1000000.0);
m_frame_counter = 0;
m_time_since_update = 0;
m_dt_total -= m_dt_queue.front();
m_dt_queue.pop_front();
}

// This frame count takes into account frames that are partially in the sample window
const double fps = (m_dt_queue.size() * US_TO_S) / m_dt_total;
const double rc = FPS_SAMPLE_RC_RATIO * std::min(window, m_dt_total) / US_TO_S;
const double a = std::max(0.0, 1.0 - std::exp(-m_raw_dt / rc));

// Sometimes euler averages can break when the average is inf/nan
// This small check makes sure that if it does break, it gets fixed
if (std::isfinite(m_avg_fps))
m_avg_fps += a * (fps - m_avg_fps);
else
m_avg_fps = fps;

if (g_ActiveConfig.bLogRenderTimeToFile)
LogRenderTimeToFile(diff);
}

void FPSCounter::SetPaused(bool paused)
{
if (paused)
m_paused = paused;
if (m_paused)
{
m_last_time_pause = Common::Timer::NowUs();
}
else
{
const u64 time = Common::Timer::NowUs();
const u64 diff = time - m_last_time_pause;
const s64 time = Common::Timer::NowUs();
const s64 diff = time - m_last_time_pause;
m_last_time += diff;
}
}
29 changes: 18 additions & 11 deletions Source/Core/VideoCommon/FPSCounter.h
Expand Up @@ -3,14 +3,15 @@

#pragma once

#include <deque>
#include <fstream>

#include "Common/CommonTypes.h"

class FPSCounter
{
public:
FPSCounter();
explicit FPSCounter(const char* log_name = "log.txt");
~FPSCounter();
FPSCounter(const FPSCounter&) = delete;
FPSCounter& operator=(const FPSCounter&) = delete;
Expand All @@ -20,20 +21,26 @@ class FPSCounter
// Called when a frame is rendered (updated every second).
void Update();

float GetFPS() const { return m_fps; }
double GetDeltaTime() const { return m_time_diff_secs; }
double GetFPS() const { return m_avg_fps; }

double GetDeltaTime() const { return m_raw_dt; }

private:
void LogRenderTimeToFile(s64 val);
void SetPaused(bool paused);

u64 m_last_time = 0;
u64 m_time_since_update = 0;
u64 m_last_time_pause = 0;
u32 m_frame_counter = 0;
int m_on_state_changed_handle = -1;
float m_fps = 0.f;
const char* m_log_name;
std::ofstream m_bench_file;
double m_time_diff_secs = 0.0;

void LogRenderTimeToFile(u64 val);
bool m_paused = false;
s64 m_last_time = 0;

double m_avg_fps = 0.0;
double m_raw_dt = 0.0;

s64 m_dt_total = 0;
std::deque<s64> m_dt_queue;

int m_on_state_changed_handle = -1;
s64 m_last_time_pause = 0;
};

0 comments on commit 3145825

Please sign in to comment.