Repository navigation
Add late tick start to cut input lag #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| // Late tick start. Mario Kart Wii reads the controller at the start of each tick, works about a | ||
| // millisecond, then sleeps until the next retrace before it draws, so the controller read sits | ||
| // unused for most of an interval. Holding each tick until shortly before that retrace moves the | ||
| // read next to the draw. The game still sees exactly the same retraces. | ||
| namespace LateTickStart { | ||
|
|
||
| // How long before the next retrace the tick should start: the slowest recent work before the wait, | ||
| // plus a 2 ms margin and any miss penalty; at least 3 ms, at most the interval less 2 ms. | ||
| // 0 = don't hold. | ||
| uint64_t LeadNanos(double workP99Ms, double missPenaltyMs, uint64_t intervalNanos); | ||
|
|
||
| // A frame that missed its retrace adds 2 ms of room (up to 8); every frame on time takes 0.05 off. | ||
| double NextMissPenalty(double currentMs, bool missedRetrace); | ||
|
|
||
| // Tick start -> the drawing thread's first wait, over the last 60 ticks. | ||
| class WorkMeter { | ||
| public: | ||
| void OnTickStart(uint64_t ns); | ||
| void OnWaitStart(uint64_t ns); | ||
| // Rounds up, so with the full window it is the slowest tick but one. False until 10 ticks. | ||
| bool P99(double& ms) const; | ||
|
|
||
| private: | ||
| static constexpr int kWindow = 60; | ||
| double m_samples[kWindow]{}; | ||
| int m_count = 0; | ||
| int m_write = 0; | ||
| uint64_t m_tickStart = 0; | ||
| bool m_measured = true; | ||
| }; | ||
|
|
||
| } // namespace LateTickStart |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #include "late_tick_start.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cmath> | ||
|
|
||
| namespace LateTickStart { | ||
|
|
||
| uint64_t LeadNanos(double workP99Ms, double missPenaltyMs, uint64_t intervalNanos) { | ||
| constexpr double kMarginMs = 2.0; | ||
| constexpr uint64_t kFloorNanos = 3'000'000; | ||
| constexpr uint64_t kHeadroomNanos = 2'000'000; | ||
| if (intervalNanos <= kFloorNanos + kHeadroomNanos) { | ||
| return 0; | ||
| } | ||
| const double leadMs = workP99Ms + missPenaltyMs + kMarginMs; | ||
| const uint64_t lead = leadMs > 0.0 ? static_cast<uint64_t>(leadMs * 1e6 + 0.5) : 0; | ||
| return std::clamp(lead, kFloorNanos, intervalNanos - kHeadroomNanos); | ||
| } | ||
|
|
||
| double NextMissPenalty(double currentMs, bool missedRetrace) { | ||
| return missedRetrace ? std::min(currentMs + 2.0, 8.0) : std::max(currentMs - 0.05, 0.0); | ||
| } | ||
|
|
||
| void WorkMeter::OnTickStart(uint64_t ns) { | ||
| m_tickStart = ns; | ||
| m_measured = false; | ||
| } | ||
|
|
||
| void WorkMeter::OnWaitStart(uint64_t ns) { | ||
| if (m_measured || ns < m_tickStart) { | ||
| return; | ||
| } | ||
| m_measured = true; | ||
| m_samples[m_write] = static_cast<double>(ns - m_tickStart) / 1e6; | ||
| m_write = (m_write + 1) % kWindow; | ||
| m_count = std::min(m_count + 1, kWindow); | ||
| } | ||
|
|
||
| bool WorkMeter::P99(double& ms) const { | ||
| if (m_count < 10) { | ||
| return false; | ||
| } | ||
| double sorted[kWindow]; | ||
| std::copy(m_samples, m_samples + m_count, sorted); | ||
| std::sort(sorted, sorted + m_count); | ||
| ms = sorted[static_cast<int>(std::ceil((m_count - 1) * 0.99))]; | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace LateTickStart |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| #include "input_bindings.h" | ||
| #include "motion_trick.h" | ||
| #include "game_graphics_options.h" | ||
| #include "hle_stubs.h" | ||
| #include "music_attenuation.h" | ||
| #include "runtime_config.h" | ||
| #include "runtime_log.h" | ||
|
|
@@ -114,6 +115,7 @@ bool g_skipUnreadyPipelines = RuntimeConfigFile::SkipUnreadyPipelines(true); | |
| bool g_vsync = RuntimeConfigFile::Vsync(); | ||
| bool g_disableCopyFilter = RuntimeConfigFile::DisableCopyFilter(true); | ||
| bool g_showFps = RuntimeConfigFile::ShowFps(true); | ||
| bool g_lateTickStart = RuntimeConfigFile::LateTickStart(false); | ||
| bool g_forceAspect = RuntimeConfigFile::ForceAspectEnabled(); | ||
| bool g_widescreen = RuntimeConfigFile::WidescreenEnabled(true); | ||
| #if defined(__APPLE__) | ||
|
|
@@ -1290,6 +1292,14 @@ void DrawGraphicsSettings() { | |
| if (ImGui::Checkbox("Show FPS", &g_showFps)) { | ||
| RuntimeConfigFile::SetShowFps(g_showFps); | ||
| } | ||
| if (ImGui::Checkbox("Late tick start (lower input lag)", &g_lateTickStart)) { | ||
| VI_HLE_SetLateTickStart(g_lateTickStart); | ||
| RuntimeConfigFile::SetLateTickStart(g_lateTickStart); | ||
| } | ||
| ImGui::PushTextWrapPos(ImGui::GetCursorPosX() + 380.0f); | ||
| ImGui::TextDisabled("Starts each game tick just before the game needs your input, so it shows up about " | ||
| "12 ms sooner. Turn it off if you see stutter."); | ||
| ImGui::PopTextWrapPos(); | ||
| ImGui::Separator(); | ||
| ImGui::Text("Graphics API: %s", GraphicsApiDisplayName()); | ||
| } | ||
|
|
@@ -1642,6 +1652,7 @@ void InitializeRuntimeSettings() noexcept { | |
| g_displayMode = static_cast<int>(aurora_get_display_mode()); | ||
| aurora_set_disable_copy_filter(g_disableCopyFilter); | ||
| aurora_set_skip_unready_pipelines(g_skipUnreadyPipelines); | ||
| VI_HLE_SetLateTickStart(g_lateTickStart); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Include
Suggested fix
|
||
| g_strapInputAccepted.store(false, std::memory_order_relaxed); | ||
| g_startupDismissFrame.store(UINT64_MAX, std::memory_order_relaxed); | ||
| g_bootShadersReady.store(false, std::memory_order_relaxed); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Report a failed setting save.
If the config write fails, the checkbox and VI still use the new value, but the old value returns on restart. Check the return value and show a save error so the user can retry.