From b002511df045393d1c17d93333fb68d8b659aea8 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Thu, 25 Jan 2018 22:24:40 -0700 Subject: [PATCH] citra-qt: Add customizable speed limit target (#3353) citra-qt: Add customizable speed limit target * Update SDL config for the new frame_limit option * Made max lag time a function of target speed percent. * Added a checkbox to enable/disable frame limiter * UI: Prevent frame_limit from under/overflowing * UI: Hide target speed percent when frame limiter is off * Disable frame limit spin box when framelimit isn't enabled --- src/citra/config.cpp | 5 +- src/citra/default_ini.h | 12 +- src/citra_qt/configuration/config.cpp | 6 +- .../configuration/configure_graphics.cpp | 8 +- .../configuration/configure_graphics.ui | 158 ++++++++++-------- src/citra_qt/main.cpp | 28 +++- src/core/perf_stats.cpp | 19 ++- src/core/settings.cpp | 1 - src/core/settings.h | 3 +- src/core/telemetry_session.cpp | 5 +- src/video_core/video_core.cpp | 3 +- src/video_core/video_core.h | 3 +- 12 files changed, 155 insertions(+), 96 deletions(-) diff --git a/src/citra/config.cpp b/src/citra/config.cpp index 0a84887d9..88e3843a8 100644 --- a/src/citra/config.cpp +++ b/src/citra/config.cpp @@ -91,8 +91,9 @@ void Config::ReadValues() { Settings::values.resolution_factor = (float)sdl2_config->GetReal("Renderer", "resolution_factor", 1.0); Settings::values.use_vsync = sdl2_config->GetBoolean("Renderer", "use_vsync", false); - Settings::values.toggle_framelimit = - sdl2_config->GetBoolean("Renderer", "toggle_framelimit", true); + Settings::values.use_frame_limit = sdl2_config->GetBoolean("Renderer", "use_frame_limit", true); + Settings::values.frame_limit = + static_cast(sdl2_config->GetInteger("Renderer", "frame_limit", 100)); Settings::values.bg_red = (float)sdl2_config->GetReal("Renderer", "bg_red", 0.0); Settings::values.bg_green = (float)sdl2_config->GetReal("Renderer", "bg_green", 0.0); diff --git a/src/citra/default_ini.h b/src/citra/default_ini.h index 7bac9474d..2d9a8d614 100644 --- a/src/citra/default_ini.h +++ b/src/citra/default_ini.h @@ -89,6 +89,14 @@ resolution_factor = # 0 (default): Off, 1: On use_vsync = +# Turns on the frame limiter, which will limit frames output to the target game speed +# 0: Off, 1: On (default) +use_frame_limit = + +# Limits the speed of the game to run no faster than this value as a percentage of target speed +# 1 - 9999: Speed limit as a percentage of target game speed. 100 (default) +frame_limit = + # The clear color for the renderer. What shows up on the sides of the bottom screen. # Must be in range of 0.0-1.0. Defaults to 1.0 for all. bg_red = @@ -115,10 +123,6 @@ custom_bottom_top = custom_bottom_right = custom_bottom_bottom = -# Whether to toggle frame limiter on or off. -# 0: Off, 1 (default): On -toggle_framelimit = - # Swaps the prominent screen with the other screen. # For example, if Single Screen is chosen, setting this to 1 will display the bottom screen instead of the top screen. # 0 (default): Top Screen is prominent, 1: Bottom Screen is prominent diff --git a/src/citra_qt/configuration/config.cpp b/src/citra_qt/configuration/config.cpp index 7e64fbaaf..c0a6115da 100644 --- a/src/citra_qt/configuration/config.cpp +++ b/src/citra_qt/configuration/config.cpp @@ -77,7 +77,8 @@ void Config::ReadValues() { Settings::values.use_shader_jit = qt_config->value("use_shader_jit", true).toBool(); Settings::values.resolution_factor = qt_config->value("resolution_factor", 1.0).toFloat(); Settings::values.use_vsync = qt_config->value("use_vsync", false).toBool(); - Settings::values.toggle_framelimit = qt_config->value("toggle_framelimit", true).toBool(); + Settings::values.use_frame_limit = qt_config->value("use_frame_limit", true).toBool(); + Settings::values.frame_limit = qt_config->value("frame_limit", 100).toInt(); Settings::values.bg_red = qt_config->value("bg_red", 0.0).toFloat(); Settings::values.bg_green = qt_config->value("bg_green", 0.0).toFloat(); @@ -241,7 +242,8 @@ void Config::SaveValues() { qt_config->setValue("use_shader_jit", Settings::values.use_shader_jit); qt_config->setValue("resolution_factor", (double)Settings::values.resolution_factor); qt_config->setValue("use_vsync", Settings::values.use_vsync); - qt_config->setValue("toggle_framelimit", Settings::values.toggle_framelimit); + qt_config->setValue("use_frame_limit", Settings::values.use_frame_limit); + qt_config->setValue("frame_limit", Settings::values.frame_limit); // Cast to double because Qt's written float values are not human-readable qt_config->setValue("bg_red", (double)Settings::values.bg_red); diff --git a/src/citra_qt/configuration/configure_graphics.cpp b/src/citra_qt/configuration/configure_graphics.cpp index d1546f3aa..c3c1cf302 100644 --- a/src/citra_qt/configuration/configure_graphics.cpp +++ b/src/citra_qt/configuration/configure_graphics.cpp @@ -14,6 +14,8 @@ ConfigureGraphics::ConfigureGraphics(QWidget* parent) this->setConfiguration(); ui->toggle_vsync->setEnabled(!Core::System::GetInstance().IsPoweredOn()); + ui->frame_limit->setEnabled(Settings::values.use_frame_limit); + connect(ui->toggle_frame_limit, &QCheckBox::stateChanged, ui->frame_limit, &QSpinBox::setEnabled); ui->layoutBox->setEnabled(!Settings::values.custom_layout); } @@ -96,7 +98,8 @@ void ConfigureGraphics::setConfiguration() { ui->resolution_factor_combobox->setCurrentIndex( static_cast(FromResolutionFactor(Settings::values.resolution_factor))); ui->toggle_vsync->setChecked(Settings::values.use_vsync); - ui->toggle_framelimit->setChecked(Settings::values.toggle_framelimit); + ui->toggle_frame_limit->setChecked(Settings::values.use_frame_limit); + ui->frame_limit->setValue(Settings::values.frame_limit); ui->layout_combobox->setCurrentIndex(static_cast(Settings::values.layout_option)); ui->swap_screen->setChecked(Settings::values.swap_screen); } @@ -107,7 +110,8 @@ void ConfigureGraphics::applyConfiguration() { Settings::values.resolution_factor = ToResolutionFactor(static_cast(ui->resolution_factor_combobox->currentIndex())); Settings::values.use_vsync = ui->toggle_vsync->isChecked(); - Settings::values.toggle_framelimit = ui->toggle_framelimit->isChecked(); + Settings::values.use_frame_limit = ui->toggle_frame_limit->isChecked(); + Settings::values.frame_limit = ui->frame_limit->value(); Settings::values.layout_option = static_cast(ui->layout_combobox->currentIndex()); Settings::values.swap_screen = ui->swap_screen->isChecked(); diff --git a/src/citra_qt/configuration/configure_graphics.ui b/src/citra_qt/configuration/configure_graphics.ui index 5667b14b6..765b303a4 100644 --- a/src/citra_qt/configuration/configure_graphics.ui +++ b/src/citra_qt/configuration/configure_graphics.ui @@ -44,81 +44,101 @@ - - - Limit framerate - - + + + + + Limit Speed Percent + + + + + + + % + + + 1 + + + 9999 + + + 100 + + + + - + + + + + Internal Resolution: + + + + + - - - Internal Resolution: - - + + Auto (Window Size) + - - - - Auto (Window Size) - - - - - Native (400x240) - - - - - 2x Native (800x480) - - - - - 3x Native (1200x720) - - - - - 4x Native (1600x960) - - - - - 5x Native (2000x1200) - - - - - 6x Native (2400x1440) - - - - - 7x Native (2800x1680) - - - - - 8x Native (3200x1920) - - - - - 9x Native (3600x2160) - - - - - 10x Native (4000x2400) - - - + + Native (400x240) + - + + + 2x Native (800x480) + + + + + 3x Native (1200x720) + + + + + 4x Native (1600x960) + + + + + 5x Native (2000x1200) + + + + + 6x Native (2400x1440) + + + + + 7x Native (2800x1680) + + + + + 8x Native (3200x1920) + + + + + 9x Native (3600x2160) + + + + + 10x Native (4000x2400) + + + + + diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index 7ecd9b742..b92564db0 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -269,6 +269,10 @@ void GMainWindow::InitializeHotkeys() { RegisterHotkey("Main Window", "Fullscreen", QKeySequence::FullScreen); RegisterHotkey("Main Window", "Exit Fullscreen", QKeySequence(Qt::Key_Escape), Qt::ApplicationShortcut); + RegisterHotkey("Main Window", "Increase Speed Limit", QKeySequence("+"), + Qt::ApplicationShortcut); + RegisterHotkey("Main Window", "Decrease Speed Limit", QKeySequence("-"), + Qt::ApplicationShortcut); LoadHotkeys(); connect(GetHotkey("Main Window", "Load File", this), &QShortcut::activated, this, @@ -287,6 +291,21 @@ void GMainWindow::InitializeHotkeys() { ToggleFullscreen(); } }); + constexpr u16 SPEED_LIMIT_STEP = 5; + connect(GetHotkey("Main Window", "Increase Speed Limit", this), &QShortcut::activated, this, + [&] { + if (Settings::values.frame_limit < 9999 - SPEED_LIMIT_STEP) { + Settings::values.frame_limit += SPEED_LIMIT_STEP; + UpdateStatusBar(); + } + }); + connect(GetHotkey("Main Window", "Decrease Speed Limit", this), &QShortcut::activated, this, + [&] { + if (Settings::values.frame_limit > SPEED_LIMIT_STEP) { + Settings::values.frame_limit -= SPEED_LIMIT_STEP; + UpdateStatusBar(); + } + }); } void GMainWindow::ShowUpdaterWidgets() { @@ -912,7 +931,14 @@ void GMainWindow::UpdateStatusBar() { auto results = Core::System::GetInstance().GetAndResetPerfStats(); - emu_speed_label->setText(tr("Speed: %1%").arg(results.emulation_speed * 100.0, 0, 'f', 0)); + if (Settings::values.use_frame_limit) { + emu_speed_label->setText(tr("Speed: %1% / %2%") + .arg(results.emulation_speed * 100.0, 0, 'f', 0) + .arg(Settings::values.frame_limit)); + } else { + emu_speed_label->setText(tr("Speed: %1%") + .arg(results.emulation_speed * 100.0, 0, 'f', 0)); + } game_fps_label->setText(tr("Game: %1 FPS").arg(results.game_fps, 0, 'f', 0)); emu_frametime_label->setText(tr("Frame: %1 ms").arg(results.frametime * 1000.0, 0, 'f', 2)); diff --git a/src/core/perf_stats.cpp b/src/core/perf_stats.cpp index 2cdfb9ded..f14e4bbca 100644 --- a/src/core/perf_stats.cpp +++ b/src/core/perf_stats.cpp @@ -75,24 +75,27 @@ double PerfStats::GetLastFrameTimeScale() { } void FrameLimiter::DoFrameLimiting(u64 current_system_time_us) { - // Max lag caused by slow frames. Can be adjusted to compensate for too many slow frames. Higher - // values increase the time needed to recover and limit framerate again after spikes. - constexpr microseconds MAX_LAG_TIME_US = 25ms; - - if (!Settings::values.toggle_framelimit) { + if (!Settings::values.use_frame_limit) { return; } auto now = Clock::now(); + double sleep_scale = Settings::values.frame_limit / 100.0; - frame_limiting_delta_err += microseconds(current_system_time_us - previous_system_time_us); + // Max lag caused by slow frames. Shouldn't be more than the length of a frame at the current + // speed percent or it will clamp too much and prevent this from properly limiting to that + // percent. High values means it'll take longer after a slow frame to recover and start limiting + const microseconds max_lag_time_us = duration_cast( + std::chrono::duration(25ms / sleep_scale)); + frame_limiting_delta_err += duration_cast( + std::chrono::duration( + (current_system_time_us - previous_system_time_us) / sleep_scale)); frame_limiting_delta_err -= duration_cast(now - previous_walltime); frame_limiting_delta_err = - MathUtil::Clamp(frame_limiting_delta_err, -MAX_LAG_TIME_US, MAX_LAG_TIME_US); + MathUtil::Clamp(frame_limiting_delta_err, -max_lag_time_us, max_lag_time_us); if (frame_limiting_delta_err > microseconds::zero()) { std::this_thread::sleep_for(frame_limiting_delta_err); - auto now_after_sleep = Clock::now(); frame_limiting_delta_err -= duration_cast(now_after_sleep - now); now = now_after_sleep; diff --git a/src/core/settings.cpp b/src/core/settings.cpp index efcf1267d..79778f578 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -22,7 +22,6 @@ void Apply() { VideoCore::g_hw_renderer_enabled = values.use_hw_renderer; VideoCore::g_shader_jit_enabled = values.use_shader_jit; - VideoCore::g_toggle_framelimit_enabled = values.toggle_framelimit; if (VideoCore::g_emu_window) { auto layout = VideoCore::g_emu_window->GetFramebufferLayout(); diff --git a/src/core/settings.h b/src/core/settings.h index b8fa3f05a..cb6049cc0 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -97,7 +97,8 @@ struct Values { bool use_shader_jit; float resolution_factor; bool use_vsync; - bool toggle_framelimit; + bool use_frame_limit; + u16 frame_limit; LayoutOption layout_option; bool swap_screen; diff --git a/src/core/telemetry_session.cpp b/src/core/telemetry_session.cpp index 92abbeb5e..2de111a1c 100644 --- a/src/core/telemetry_session.cpp +++ b/src/core/telemetry_session.cpp @@ -167,8 +167,9 @@ TelemetrySession::TelemetrySession() { AddField(Telemetry::FieldType::UserConfig, "Core_UseCpuJit", Settings::values.use_cpu_jit); AddField(Telemetry::FieldType::UserConfig, "Renderer_ResolutionFactor", Settings::values.resolution_factor); - AddField(Telemetry::FieldType::UserConfig, "Renderer_ToggleFramelimit", - Settings::values.toggle_framelimit); + AddField(Telemetry::FieldType::UserConfig, "Renderer_UseFrameLimit", + Settings::values.use_frame_limit); + AddField(Telemetry::FieldType::UserConfig, "Renderer_FrameLimit", Settings::values.frame_limit); AddField(Telemetry::FieldType::UserConfig, "Renderer_UseHwRenderer", Settings::values.use_hw_renderer); AddField(Telemetry::FieldType::UserConfig, "Renderer_UseShaderJit", diff --git a/src/video_core/video_core.cpp b/src/video_core/video_core.cpp index 7186a7652..f4b530604 100644 --- a/src/video_core/video_core.cpp +++ b/src/video_core/video_core.cpp @@ -20,7 +20,6 @@ std::unique_ptr g_renderer; ///< Renderer plugin std::atomic g_hw_renderer_enabled; std::atomic g_shader_jit_enabled; std::atomic g_vsync_enabled; -std::atomic g_toggle_framelimit_enabled; /// Initialize the video core bool Init(EmuWindow* emu_window) { @@ -47,4 +46,4 @@ void Shutdown() { LOG_DEBUG(Render, "shutdown OK"); } -} // namespace +} // namespace VideoCore diff --git a/src/video_core/video_core.h b/src/video_core/video_core.h index 94e0867f0..69c672f29 100644 --- a/src/video_core/video_core.h +++ b/src/video_core/video_core.h @@ -22,7 +22,6 @@ extern EmuWindow* g_emu_window; ///< Emu window // qt ui) extern std::atomic g_hw_renderer_enabled; extern std::atomic g_shader_jit_enabled; -extern std::atomic g_toggle_framelimit_enabled; /// Start the video core void Start(); @@ -33,4 +32,4 @@ bool Init(EmuWindow* emu_window); /// Shutdown the video core void Shutdown(); -} // namespace +} // namespace VideoCore