Tell in the log and the menu when RivaTuner Statistics Server's own limit holds the game beside another limiter.

With NX2NAVI_RTSS=0, frame generation or the driver's low latency, the game keeps RTSS out of its frame limit, but a
limit the player set in RTSS itself (its Global profile or the game's own: a frame rate limit, a frame time limit or
scanline sync) still holds the frames, on top of the game's limiter, the driver's or frame generation's. Nothing read
RTSS's settings in such a run: a first want that kept RTSS out never started its thread, and frame_step_cap_ns folded
the reasons into -1, so the log said "NX2NAVI_RTSS=0 or frame generation" for low latency too.

frame_step_cap_ns now passes the reason (rtss_decide.h RTSS_OUT_SETTING, RTSS_OUT_FRAMEGEN, RTSS_OUT_LOWLAT; the
unlimited branch too), and the kept-out line names it. A kept-out first want starts the worker as well. While RTSS is
kept out the worker never loads RTSS's hooks library and only reads: its shared memory, and once RTSS lists the game
the [Framerate] values of Profiles\<exe>.cfg, else Profiles\Global, with the system's ini reader, again only when
either file's write time changes. When one of them limits the game, the log names the file, the values and the other
limiter, once per change, and rtss_limit_state returns 4, which the frame rate row shows as a new note
(UI_MENU_FPS_RTSS_TWO, in English, Russian, Brazilian Portuguese and Simplified Chinese). A later want of a limit
(frame generation or low latency switched off) loads the hooks and hands RTSS the limit as before; NX2NAVI_RTSS=0
stays read-only for the run. Pacing does not change.

Not reproduced before the change: a stacked limit needs RTSS running in a play-test. tests/rtss_worker_check.c gains
three scenarios against the fake RTSS (a Global limit with NX2NAVI_RTSS=0, scanline sync in the game's profile under
low latency and then a limit, frame generation with no limit of RTSS's own), and tests/rtss_limit_check.c builds
rtss_decide.h alone and checks which settings hold the frames and their text. Both pass, with frame_step_check and a
compile of every runtime file; -Wall adds no warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:13:34 -05:00
parent a1153aa079
commit b0488796a1
10 changed files with 344 additions and 43 deletions

View File

@@ -55,6 +55,7 @@
#include "input.h"
#include "layout.h"
#include "platform/platform.h"
#include "rtss_decide.h"
#include "vk_dlssg.h"
#include "vk_fg.h"
#include "vk_lowlat.h"
@@ -276,7 +277,8 @@ double frame_step_game_hz(void) {
// RivaTuner Statistics Server (rtss_limit.c) gets the same limit; while it limits this process the pacer holds no
// frames, so the two never stack. NX2NAVI_RTSS=0 and frame generation (RTSS would count the generated frames) keep it
// out, and so does a driver's low latency while it is in effect (vk_lowlat.c): the driver holds the limit where the
// frame starts, and RTSS would hold the finished picture, the wait that mode is there to remove.
// frame starts, and RTSS would hold the finished picture, the wait that mode is there to remove. RTSS gets the reason
// (RTSS_OUT_*), for the log and for its check of a limit set in RTSS itself.
static double paced_hz; // the game frames a second the newest frame_step_cap_ns paces at (ocarina_step); 0: none
static int movie_paced; // the movie rate the newest frame_step_cap_ns paces at (ocarina_step, frame_step_movie_hz); 0: none
int frame_step_movie_hz(void) { return movie_paced; }
@@ -287,7 +289,10 @@ uint64_t frame_step_cap_ns(void) {
const char *e = cfg_get("NX2NAVI_RTSS");
rtss_allowed = !(e && !strcmp(e, "0"));
}
int rtss = rtss_allowed && !vk_fg_wanted() && !vk_dlssg_active() && !vk_lowlat_on();
int64_t rtss_out = !rtss_allowed ? RTSS_OUT_SETTING
: vk_fg_wanted() || vk_dlssg_active() ? RTSS_OUT_FRAMEGEN
: vk_lowlat_on() ? RTSS_OUT_LOWLAT
: 0;
int gen = generated_factor();
int movie_hz = movie_cap_hz();
static int movie_clock = -1; // NX2NAVI_MOVIE_CLOCK=0: the steady step and the usual pacing through a movie too
@@ -305,7 +310,7 @@ uint64_t frame_step_cap_ns(void) {
}
}
if (limit_hz == -2 && !movie_hz && !frame_free_running()) { // unlimited: the pacer does not hold frames, and
rtss_limit_want(rtss ? 0 : -1); // RTSS's limit is cleared
rtss_limit_want(rtss_out ? rtss_out : 0); // RTSS's limit is cleared
paced_hz = 0;
return 0;
}
@@ -322,7 +327,7 @@ uint64_t frame_step_cap_ns(void) {
}
if (movie_hz) hz = movie_hz;
paced_hz = hz;
rtss_limit_want(rtss ? (int64_t)(hz * 1000 + 0.5) : -1);
rtss_limit_want(rtss_out ? rtss_out : (int64_t)(hz * 1000 + 0.5));
if (rtss_limit_active()) return 0;
return (uint64_t)(1e9 / hz + 0.5);
}

View File

@@ -428,10 +428,12 @@ void frame_rate_sound_step_host(Cpu *c); // the step the sound
float frame_step_frames(void); // the 60 Hz frames the game steps per update (set ostep), else 1
void frame_step_flight(float *page, float *game, uint64_t *presents); // flight.c: the step at a host present
// rtss_limit.c: RivaTuner Statistics Server takes the frame limit where it runs
void rtss_limit_want(int64_t mhz); // the pacer's limit in millihertz: >0, 0 none, -1 keep RTSS out
void rtss_limit_want(int64_t mhz); // the pacer's limit in millihertz: >0, 0 none; or why RTSS is
// kept out (rtss_decide.h RTSS_OUT_*)
int rtss_limit_active(void); // RTSS limits to the wanted rate: the pacer holds no frames
int rtss_limit_state(void); // menu: 1 RTSS limits, 2 its profile is read-only, 3 it let the
// game run past its limit
// game run past its limit, 4 kept out while a limit set in
// RTSS itself holds the game too
void rtss_limit_restore(void); // before the process ends: the profile's old limit back
int64_t frame_step_world_us(void); // game time since the loaded save's first frame, -1 before
// (NX2NAVI_FPS_LIMIT, live)

45
runtime/rtss_decide.h Normal file
View File

@@ -0,0 +1,45 @@
// RivaTuner Statistics Server as the frame limiter (rtss_limit.c): the parts that make no system call, so that
// tests/rtss_limit_check.c builds them on any system.
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
// rtss_limit_want's argument: a limit in millihertz (>0), 0 for none, or why RTSS is kept out of it (frame_rate.c
// frame_step_cap_ns). RTSS_UNSET is the want before the first one.
enum {
RTSS_UNSET = -2,
RTSS_OUT_SETTING = -1, // NX2NAVI_RTSS=0, for the whole run
RTSS_OUT_FRAMEGEN = -3, // frame generation: RTSS would count the generated frames too
RTSS_OUT_LOWLAT = -4, // a driver's low latency (vk_lowlat.c) holds the frames where they start
};
// What keeps RTSS out, for the log.
static inline const char *rtss_out_text(int64_t want) {
return want == RTSS_OUT_FRAMEGEN ? "frame generation"
: want == RTSS_OUT_LOWLAT ? "the driver's low latency"
: "NX2NAVI_RTSS=0";
}
// The limiter that holds the frames while RTSS is kept out, for the log.
static inline const char *rtss_out_limiter(int64_t want) {
return want == RTSS_OUT_FRAMEGEN ? "frame generation and the game's limiter"
: want == RTSS_OUT_LOWLAT ? "the driver's low latency"
: "the game's limiter";
}
// RTSS's own [Framerate] settings for the game, as its profile file holds them (rtss_limit.c read_settings): a frame
// rate limit (Limit over LimitDenominator), a frame time limit (LimitTime over LimitTimeDenominator) and scanline sync
// (SyncScanline0 and SyncScanline1); 0 is off for each.
typedef struct {
int limit, limit_den, time, time_den, scan0, scan1;
} RtssSettings;
// RTSS holds the game's frames by a setting of its own.
static inline int rtss_settings_limit(const RtssSettings *s) { return s->limit || s->time || s->scan0 || s->scan1; }
// "limit L/D, frame time limit T/TD, scanline sync S0/S1", the values as the file holds them.
static inline void rtss_settings_text(char *out, size_t n, const RtssSettings *s) {
snprintf(out, n, "limit %d/%d, frame time limit %d/%d, scanline sync %d/%d", s->limit, s->limit_den, s->time,
s->time_den, s->scan0, s->scan1);
}

View File

@@ -33,6 +33,7 @@
#include "host.h"
#include "platform/platform.h"
#include "rtss_decide.h"
#ifdef _WIN32
#include <windows.h>
@@ -50,10 +51,11 @@ typedef void (*UpdateFn)(void);
enum { FAIL_NONE, FAIL_LIBRARY, FAIL_READONLY, FAIL_NOT_LIMITING };
static PlatMutex lock = PLAT_MUTEX_INIT; // the hooks calls and the state below
static _Atomic int64_t want = -2; // millihertz from the pacer: >0 a limit, 0 none, -1 RTSS kept out; -2 unset
static _Atomic int64_t want = RTSS_UNSET; // millihertz from the pacer: >0 a limit, 0 none; RTSS_OUT_* kept out
static _Atomic int active; // RTSS limits this process now
static _Atomic int failed; // FAIL_*: RTSS is out of it for the rest of the run
static _Atomic int closed; // rtss_limit_restore ran: nothing reaches the profile any more
static _Atomic int two; // kept out, and RTSS limits the game by a setting of its own (watch)
static int started;
static char dll_path[MAX_PATH], profile[MAX_PATH], rtss_dir[MAX_PATH];
static HMODULE dll;
@@ -243,12 +245,76 @@ static const char *fail_reason(int f) {
: "RivaTuner Statistics Server's hooks library did not load";
}
// RTSS's own settings for the game, read from its profile file as RTSS keeps it: Profiles\<exe>.cfg, else
// Profiles\Global, and a key the game's profile lacks from Global. The system's ini reader does it, so no call goes into
// RTSS's hooks library, which a run that keeps RTSS out never loads. The files are read again only when the time
// either was written changes. Worker thread only.
static RtssSettings settings;
static char settings_file[MAX_PATH + 16]; // which file they came from, as Profiles\<name>
static uint64_t file_stamp(const char *path) { // 0: no such file
WIN32_FILE_ATTRIBUTE_DATA a;
if (!GetFileAttributesExA(path, GetFileExInfoStandard, &a)) return 0;
return ((uint64_t)a.ftLastWriteTime.dwHighDateTime << 32 | a.ftLastWriteTime.dwLowDateTime) | 1;
}
static int cfg_int(const char *own, const char *global, const char *key) {
char v[32] = "";
if (own) GetPrivateProfileStringA("Framerate", key, "", v, sizeof v, own);
if (!*v) GetPrivateProfileStringA("Framerate", key, "", v, sizeof v, global);
return (int)strtol(v, NULL, 10); // GetPrivateProfileIntA would read a negative scanline as 0
}
static void read_settings(void) {
static uint64_t stamps[2] = {~0ull, ~0ull}; // never read
char own[MAX_PATH * 2], global[MAX_PATH * 2];
snprintf(own, sizeof own, "%s\\Profiles\\%s.cfg", rtss_dir, profile);
snprintf(global, sizeof global, "%s\\Profiles\\Global", rtss_dir);
uint64_t a = file_stamp(own), b = file_stamp(global);
if (a == stamps[0] && b == stamps[1]) return;
stamps[0] = a, stamps[1] = b;
const char *p = a ? own : NULL;
settings = (RtssSettings){cfg_int(p, global, "Limit"), cfg_int(p, global, "LimitDenominator"),
cfg_int(p, global, "LimitTime"), cfg_int(p, global, "LimitTimeDenominator"),
cfg_int(p, global, "SyncScanline0"), cfg_int(p, global, "SyncScanline1")};
if (a) snprintf(settings_file, sizeof settings_file, "Profiles\\%s.cfg", profile);
else snprintf(settings_file, sizeof settings_file, "Profiles\\Global");
}
// While RTSS is kept out (w < 0) the game's own limiter, the driver's low latency or frame generation holds the frames,
// and a limit the player set in RTSS itself for the game (its global profile or the game's own, a frame time limit or
// scanline sync) still holds them a second time. Logged once per change and shown in the menu (rtss_limit_state 4).
static void watch(int64_t w, int listed) {
static int said, lines;
static char said_text[128];
int on = 0;
char text[128] = "";
if (w < 0 && listed) {
read_settings();
on = rtss_settings_limit(&settings);
rtss_settings_text(text, sizeof text, &settings);
}
if (w >= 0) said = 0; // a later kept-out want logs afresh
else if (on != said || (on && strcmp(text, said_text))) {
if (lines++ < 40) {
if (on)
rt_log("frame rate: two limiters: RivaTuner Statistics Server's own setting for the game (%s: %s) limits "
"it beside %s", settings_file, text, rtss_out_limiter(w));
else
rt_log("frame rate: RivaTuner Statistics Server no longer limits the game beside %s", rtss_out_limiter(w));
}
said = on;
snprintf(said_text, sizeof said_text, "%s", text);
}
atomic_store(&two, on);
}
static void worker(void *arg) {
(void)arg;
int was_running = -1, was_hooked = 0, over = 0;
for (;; plat_sleep_ms(POLL_MS)) {
int64_t w = atomic_load(&want);
if (w == -2) continue;
if (w == RTSS_UNSET) continue;
plat_mutex_lock(&lock);
if (atomic_load(&closed)) { // a want read before rtss_limit_restore must not reach the profile after it
plat_mutex_unlock(&lock);
@@ -265,7 +331,9 @@ static void worker(void *arg) {
}
if (w < 0) {
if (dll) put_back();
set_active(0, "RivaTuner Statistics Server kept out: NX2NAVI_RTSS=0 or frame generation");
char why[96];
snprintf(why, sizeof why, "RivaTuner Statistics Server kept out: %s", rtss_out_text(w));
set_active(0, why);
} else if (f) {
set_active(0, fail_reason(f));
} else if (!run) {
@@ -294,11 +362,12 @@ static void worker(void *arg) {
set_active(0, fail_reason(FAIL_NOT_LIMITING));
}
}
watch(w, run && hooked);
plat_mutex_unlock(&lock);
}
}
static void start(void) {
static void start(int64_t first) {
started = 1;
char dir[MAX_PATH];
DWORD size = sizeof dir;
@@ -320,14 +389,17 @@ static void start(void) {
for (const char *s = exe; *s; s++)
if (*s == '\\' || *s == '/') base = s + 1;
snprintf(profile, sizeof profile, "%s", base); // RTSS names a profile after the executable, as ocarina.exe
rt_log("frame rate: RivaTuner Statistics Server found in %s; it takes the frame limit while it runs", dir);
if (first >= 0)
rt_log("frame rate: RivaTuner Statistics Server found in %s; it takes the frame limit while it runs", dir);
else
rt_log("frame rate: RivaTuner Statistics Server found in %s; kept out (%s), so its thread only reads RTSS's own "
"settings for the game, to tell when RTSS limits it a second time", dir, rtss_out_text(first));
if (!plat_thread_spawn(worker, NULL, "rtss", 0)) rt_log("frame rate: the RivaTuner Statistics Server thread did not start");
}
void rtss_limit_want(int64_t mhz) {
if (atomic_load_explicit(&closed, memory_order_relaxed)) return; // the exit put the profile back
if (!started && mhz < 0) return; // kept out from the start, for the whole run (both reasons need a restart)
if (!started) start();
if (!started) start(mhz); // a first want that keeps RTSS out starts the worker too, which then only reads (watch)
// a new limit: the game's own limiter holds it until the worker had RTSS apply it, up to POLL_MS later (a movie's
// cap that waited for that ran the movie's 60 Hz count fast for half a second)
if (atomic_exchange(&want, mhz) != mhz) atomic_store(&active, 0);
@@ -340,8 +412,10 @@ int rtss_limit_active(void) {
int rtss_limit_state(void) {
if (atomic_load_explicit(&closed, memory_order_relaxed)) return 0;
int f = atomic_load_explicit(&failed, memory_order_relaxed);
if (atomic_load(&want) >= 0 && (f == FAIL_READONLY || f == FAIL_NOT_LIMITING)) return f == FAIL_READONLY ? 2 : 3;
return rtss_limit_active() && atomic_load(&want) > 0;
int64_t w = atomic_load(&want);
if (w >= 0 && (f == FAIL_READONLY || f == FAIL_NOT_LIMITING)) return f == FAIL_READONLY ? 2 : 3;
if (w < 0 && w != RTSS_UNSET && atomic_load_explicit(&two, memory_order_relaxed)) return 4;
return rtss_limit_active() && w > 0;
}
void rtss_limit_restore(void) {

View File

@@ -861,6 +861,7 @@ static void row_fps_limit() {
const char *note = rtss == 1 ? menu_text(UI_MENU_FPS_RTSS)
: rtss == 2 ? menu_text(UI_MENU_FPS_RTSS_READONLY)
: rtss == 3 ? menu_text(UI_MENU_FPS_RTSS_IGNORED)
: rtss == 4 ? menu_text(UI_MENU_FPS_RTSS_TWO)
: nullptr;
opt_choice_list("NX2NAVI_FPS_LIMIT", UI_MENU_FPS_LIMIT, vals, names, n, UI_MENU_FPS_LIMIT_DESC, cur, note);
}

View File

@@ -278,6 +278,7 @@ static const char *const text_en[UI_TEXT_COUNT] = {
[UI_MENU_FPS_RTSS] = "limited by RivaTuner Statistics Server",
[UI_MENU_FPS_RTSS_READONLY] = "RivaTuner needs the game run as administrator",
[UI_MENU_FPS_RTSS_IGNORED] = "RivaTuner does not limit this game; the game does",
[UI_MENU_FPS_RTSS_TWO] = "RivaTuner's own limit also applies: two limiters",
[UI_MENU_REALTIME] = "Game speed follows the clock",
[UI_MENU_REALTIME_DESC] = "Steps the game by real time, so a slow frame does not slow the game down.",
[UI_MENU_CLOTH_REALTIME] = "Cloth on the clock",
@@ -914,6 +915,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = {
[UI_MENU_FPS_RTSS] = "ограничивает RivaTuner Statistics Server",
[UI_MENU_FPS_RTSS_READONLY] = "RivaTuner нужен запуск игры от администратора",
[UI_MENU_FPS_RTSS_IGNORED] = "RivaTuner не ограничивает эту игру; ограничивает игра",
[UI_MENU_FPS_RTSS_TWO] = "действует и ограничение самого RivaTuner: два ограничителя",
[UI_MENU_REALTIME] = "Скорость игры по часам",
[UI_MENU_REALTIME_DESC] = "Игра идёт по реальному времени, медленный кадр её не замедляет.",
[UI_MENU_CLOTH_REALTIME] = "Ткань по часам",
@@ -1424,6 +1426,7 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = {
[UI_MENU_FPS_RTSS] = "limitado pelo RivaTuner Statistics Server",
[UI_MENU_FPS_RTSS_READONLY] = "o RivaTuner pede o jogo como administrador",
[UI_MENU_FPS_RTSS_IGNORED] = "o RivaTuner não limita este jogo; o jogo limita",
[UI_MENU_FPS_RTSS_TWO] = "o limite do próprio RivaTuner também vale: dois limitadores",
[UI_MENU_REALTIME] = "Velocidade do jogo pelo relógio",
[UI_MENU_REALTIME_DESC] = "Avança o jogo pelo tempo real, para que um quadro lento não deixe o jogo mais lento.",
[UI_MENU_CLOTH_REALTIME] = "Tecidos pelo relógio",
@@ -2027,6 +2030,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = {
[UI_MENU_FPS_RTSS] = "由 RivaTuner Statistics Server 限制",
[UI_MENU_FPS_RTSS_READONLY] = "RivaTuner 需要以管理员身份运行游戏",
[UI_MENU_FPS_RTSS_IGNORED] = "RivaTuner 未限制本游戏;由游戏自行限制",
[UI_MENU_FPS_RTSS_TWO] = "RivaTuner 自身的限制也在生效:两个限制器",
[UI_MENU_REALTIME] = "游戏速度跟随时钟",
[UI_MENU_REALTIME_DESC] = "按真实时间推进游戏,帧率低时游戏速度也不会变慢。",
[UI_MENU_CLOTH_REALTIME] = "布料跟随时钟",

View File

@@ -73,6 +73,7 @@ typedef enum {
UI_MENU_FPS_LIMIT, UI_MENU_FPS_LIMIT_DESC, UI_MENU_FPS_30, UI_MENU_FPS_40, UI_MENU_FPS_45, UI_MENU_FPS_60, UI_MENU_FPS_90,
UI_MENU_FPS_120, UI_MENU_FPS_144,
UI_MENU_FPS_DISPLAY, UI_MENU_FPS_DISPLAY_HZ, UI_MENU_FPS_UNLIMITED, UI_MENU_FPS_RTSS, UI_MENU_FPS_RTSS_READONLY, UI_MENU_FPS_RTSS_IGNORED,
UI_MENU_FPS_RTSS_TWO,
UI_MENU_REALTIME, UI_MENU_REALTIME_DESC,
UI_MENU_CLOTH_REALTIME, UI_MENU_CLOTH_REALTIME_DESC,
UI_MENU_HIGHFPS_FIX, UI_MENU_HIGHFPS_FIX_DESC,