From 60694532a1221246cd5bf9391980532313c2c76b Mon Sep 17 00:00:00 2001 From: chrg6 Date: Fri, 13 Nov 2026 19:17:33 -0500 Subject: [PATCH] Round a fractional limit down for a RivaTuner Statistics Server without the denominator field, so "display" never holds the game above the refresh. apply() rounded the millihertz to the nearest whole frame where RTSS has no FramerateLimitDenominator, so "display" on a 59.94 Hz monitor wrote 60 and on a 143.856 Hz one 144: above the refresh, one frame more than the monitor shows every 17 s and every 7 s. Only "display" asks a fractional rate under RTSS (fixed limits and movie rates are whole, frame generation keeps RTSS out), so the new rtss_decide.h rtss_limit_value rounds every fraction down when the denominator is missing (59/1, 143/1), and keeps a whole rate over 1 and a fraction over 1000 where RTSS knows the denominator. VRR and present-wait handling stay to be measured. Not reproduced before the change. rtss_limit_check (T15: rtss_limit_value at 59940, 60000, 143856, 0 and 240000 with and without the denominator) and rtss_worker_check (round_logged_whole now 59/1 and 143/1, round_logged) pass. Co-Authored-By: Claude Opus 5.5 --- runtime/rtss_decide.h | 11 +++++++++++ runtime/rtss_limit.c | 7 +++---- tests/rtss_limit_check.c | 15 +++++++++++++++ tests/rtss_worker_check.c | 10 ++++++---- 4 files changed, 35 insertions(+), 8 deletions(-) diff --git a/runtime/rtss_decide.h b/runtime/rtss_decide.h index 997244a7..6e43f608 100644 --- a/runtime/rtss_decide.h +++ b/runtime/rtss_decide.h @@ -143,6 +143,17 @@ typedef struct { // the frames that catch up, and the picture would fall behind the sound at each late frame for good. static inline int64_t rtss_target(int64_t w, int64_t movie_mhz) { return w > 0 && movie_mhz > w ? 0 : w; } +// The profile's FramerateLimit and FramerateLimitDenominator for mhz (>= 0): a whole rate over 1, a fraction over 1000 +// where RTSS knows the denominator (has_den), else rounded down to whole frames. Only "display" asks a fraction (59.94 +// or 143.856 Hz monitors), and a limit rounded up (60, 144) let the game run above the refresh, so that every 7 to 17 +// seconds a frame came that the monitor had no refresh for. +static inline void rtss_limit_value(int64_t mhz, int has_den, uint32_t *limit, uint32_t *den) { + *den = 1; + if (mhz % 1000 == 0) *limit = (uint32_t)(mhz / 1000); + else if (has_den) *limit = (uint32_t)mhz, *den = 1000; + else *limit = (uint32_t)(mhz / 1000); +} + // The hooks library is loaded only on a poll that may write the profile: a limit is wanted, RTSS is not out of it for // the run, and it runs. A poll of a kept-out want never loads it; a later limit does. static inline int rtss_wants_hooks(const RtssState *s, const RtssIn *in) { diff --git a/runtime/rtss_limit.c b/runtime/rtss_limit.c index 2b715562..1ed1b8f1 100644 --- a/runtime/rtss_limit.c +++ b/runtime/rtss_limit.c @@ -214,10 +214,9 @@ static int apply(void *ctx, int64_t mhz, int first, int clear) { has_denominator = get_property("FramerateLimitDenominator", (LPBYTE)&old_denominator, sizeof old_denominator); if (!old_denominator) old_denominator = 1; } - DWORD limit, denominator = 1; - if (mhz % 1000 == 0) limit = (DWORD)(mhz / 1000); - else if (has_denominator) limit = (DWORD)mhz, denominator = 1000; - else limit = (DWORD)((mhz + 500) / 1000); + uint32_t l, d; + rtss_limit_value(mhz, has_denominator, &l, &d); + DWORD limit = l, denominator = d; if (first) { // the line goes in before the profile changes: a kill between the two finds it as it was rec = (RtssSaved){had_profile, old_limit, old_denominator, limit, denominator, ""}; snprintf(rec.name, sizeof rec.name, "%s", profile); diff --git a/tests/rtss_limit_check.c b/tests/rtss_limit_check.c index 3e75019d..3f74fec5 100644 --- a/tests/rtss_limit_check.c +++ b/tests/rtss_limit_check.c @@ -578,6 +578,20 @@ static void saved(void) { "the denominator is compared too"); } +// The profile's values for a limit (T15): whole rates as they are, a fraction over 1000 where RTSS knows the +// denominator, else rounded down, so "display" at 59.94 Hz never holds the game above the refresh. +static void limit_value(void) { + static const struct { int64_t mhz; int has_den; uint32_t limit, den; } cases[] = { + {59940, 0, 59, 1}, {59940, 1, 59940, 1000}, {60000, 0, 60, 1}, {60000, 1, 60, 1}, + {143856, 0, 143, 1}, {143856, 1, 143856, 1000}, {0, 0, 0, 1}, {240000, 1, 240, 1}}; + for (unsigned i = 0; i < sizeof cases / sizeof *cases; i++) { + uint32_t l = 7, d = 7; + rtss_limit_value(cases[i].mhz, cases[i].has_den, &l, &d); + CHECK(l == cases[i].limit && d == cases[i].den, "%lld mHz, denominator %d: %u/%u, not %u/%u", + (long long)cases[i].mhz, cases[i].has_den, l, d, cases[i].limit, cases[i].den); + } +} + int main(void) { settings(); reasons(); @@ -595,6 +609,7 @@ int main(void) { two(); pace_period(); saved(); + limit_value(); printf("rtss_limit_check: %s\n", fails ? "FAIL" : "ok"); return fails != 0; } diff --git a/tests/rtss_worker_check.c b/tests/rtss_worker_check.c index 79efcdeb..b2685eb8 100644 --- a/tests/rtss_worker_check.c +++ b/tests/rtss_worker_check.c @@ -578,7 +578,8 @@ static void active_logged(void) { } // A limit of a fractional monitor (display at 59.94 and 143.856 Hz): an RTSS that knows the denominator holds it as it -// is, one that does not holds it rounded to whole frames, and the log says what the profile holds, not what was asked. +// is, one that does not holds it rounded down to whole frames, never above the refresh, and the log says what the +// profile holds, not what was asked. static void round_logged(void) { app_set(1, 0xA, 1000, 2000, 60); ask(59940); @@ -593,10 +594,11 @@ static void round_logged_whole(void) { ask(59940); poll(); show("display at 59.94 Hz, RTSS without a denominator"); - CHECK(own.limit == 60, "rounded to whole frames"); - CHECK(logged("profile ocarina.exe limits to 60/1 (60.000 fps; the game asked 59.940)"), "the log names 60/1"); + CHECK(own.limit == 59, "rounded down to whole frames"); + CHECK(logged("profile ocarina.exe limits to 59/1 (59.000 fps; the game asked 59.940)"), "the log names 59/1"); settle(143856); - CHECK(logged("profile ocarina.exe limits to 144/1 (144.000 fps; the game asked 143.856)"), "the log names 144/1"); + CHECK(own.limit == 143, "143.856 rounded down"); + CHECK(logged("profile ocarina.exe limits to 143/1 (143.000 fps; the game asked 143.856)"), "the log names 143/1"); settle(60000); CHECK(logged("profile ocarina.exe limits to 60.000 fps\n"), "a whole limit as it is"); }