From 6ac2da7e8f0a6cf77a7c7d190d5b00704b735096 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Tue, 3 Nov 2026 21:41:09 +0300 Subject: [PATCH] Add a frame queue that frees a window image at its display time nvnWindowAcquireTexture holds the game until the image is free, which was one period after its present's display time, when the next frame reaches the display. The present is a copy queued by then, so with frame_queue = 1 the image is free from that time: the game draws the next frame while the previous one waits for its turn, and a frame that takes up to one period more still makes its slot. In the town at 4K at 60 fps on an RTX 5070, frames over 17.5 ms fell from 25-33 to 1-8 per 30 s, for up to one frame more input delay. Off by default; the menu switches it at once. --- runtime/cfg.c | 6 +++--- runtime/nvn_api_obj.c | 16 +++++++++++----- runtime/ui/menu.cpp | 1 + runtime/ui_text.c | 11 +++++++++++ runtime/ui_text.h | 1 + totk.ini.example | 3 +++ 6 files changed, 30 insertions(+), 8 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index de418f3c..5aaa900d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -39,7 +39,7 @@ static const char *const known[] = { "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", - "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", + "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", @@ -63,7 +63,7 @@ static const char *const known[] = { // The totk.ini section a key cfg_set may write goes to when the file lacks it (totk.ini.example's sections). static const struct { const char *key, *section; } sections[] = { - {"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, + {"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"FRAME_QUEUE", "frame rate"}, {"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"}, {"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"}, {"GPU_INDEX", "display"}, {"OUTPUT_RES", "display"}, {"RENDER_SCALE", "display"}, {"UPSCALER", "display"}, @@ -133,7 +133,7 @@ static const CfgMeta metas[] = { {"SHADOW_RANGE", "100", "50..400", RESTART}, {"CONTACT_SHADOWS", "0", "0..100", RESTART}, {"PCSS", "0", "0..200", RESTART}, // Game - {"FPS_LIMIT", "60", "30|40|45|60|90|120|144|display", LIVE}, {"FPS60", "1", "0|1", LIVE}, + {"FPS_LIMIT", "60", "30|40|45|60|90|120|144|display", LIVE}, {"FPS60", "1", "0|1", LIVE}, {"FRAME_QUEUE", "0", "0|1", LIVE}, {"REALTIME", "1", "0|1", RESTART}, {"CLOTH_REALTIME", "1", "0|1", RESTART}, {"MODS_REBUILD", "auto", "auto|off", RESTART}, {"HIGHFPS_FIX", "1", "0|1", RESTART}, diff --git a/runtime/nvn_api_obj.c b/runtime/nvn_api_obj.c index bca459ac..0324d6e9 100644 --- a/runtime/nvn_api_obj.c +++ b/runtime/nvn_api_obj.c @@ -11,6 +11,7 @@ #include #include +#include "cfg.h" #include "gfx.h" #include "host.h" #include "nvn_api.h" @@ -667,8 +668,12 @@ static void sleep_until(uint64_t deadline) { } // NVNwindowAcquireTextureResult nvnWindowAcquireTexture(NVNwindow *w, NVNsync *s, int *index): blocks here, not in the -// present, until the image's previous present plus one period has passed (the game often presents with a lock held). -// That is the time the next frame goes to the display, so the image is free as soon as that time is reached. +// present, until the image is free (the game often presents with a lock held). Without the frame queue that is the +// image's previous present plus one period, when the next frame goes to the display, so a frame gets exactly one period +// from acquire to present. Its present is a copy already on the queue by its display time, so with the frame queue +// (TOTK_FRAME_QUEUE=1) the image is free from that time on: the game draws the next frame while the previous one waits +// for its turn, and a frame that takes up to one more period still makes its slot, for up to one frame more latency. +// Town at 4K with DLAA at 60 on an RTX 5070: frames over 17.5 ms 1-8 per 30 s with the queue against 25-33 without. NVN_API(nvnWindowAcquireTexture) { win_t *o = arg_p(c, 0); sync_t *s = arg_p(c, 1); @@ -693,8 +698,9 @@ NVN_API(nvnWindowAcquireTexture) { // void nvnQueuePresentTexture(NVNqueue *q, NVNwindow *w, int index): queues the copy and returns at once. The display // time comes from the pacer: the back end's present thread holds the host present until then (back-edge pacing, as the -// Switch's display flips at vblank), so the present cadence does not follow the replay time; one period later the -// image is free for the next acquire. Added latency: the slack of the frame (period minus its work), as on the Switch. +// Switch's display flips at vblank), so the present cadence does not follow the replay time; one period later (at that +// time with the frame queue) the image is free for the next acquire. Added latency: the slack of the frame (period minus +// its work), as on the Switch, plus up to a period with the frame queue. NVN_API(nvnQueuePresentTexture) { win_t *o = arg_p(c, 1); int idx = (int)arg_x(c, 2); @@ -709,7 +715,7 @@ NVN_API(nvnQueuePresentTexture) { uint64_t slot = now + pace(&o->pace, now, period); gfx_backend()->present(t, o->crop, slot); o->ticket[idx] = o->presents; - o->avail[idx] = slot + period; + o->avail[idx] = cfg_bool("TOTK_FRAME_QUEUE") ? slot : slot + period; frame_step_present(slot, o->interval); // frame_rate.c: the real-time step follows the display times rt_frame_presented(t, o->presents); } diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index d76bdde5..bec5ba32 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -699,6 +699,7 @@ static void tab_screen(float scale) { static void tab_game(float scale) { opt_begin("##game", scale); row_fps_limit(); + opt_toggle("TOTK_FRAME_QUEUE", UI_MENU_FRAME_QUEUE, UI_MENU_FRAME_QUEUE_DESC); row_game_lang(); static const UiTextId mode[] = {UI_MENU_MODE_DOCKED, UI_MENU_MODE_HANDHELD}; opt_choice("TOTK_OPERATION_MODE", UI_MENU_OPERATION_MODE, mode, 2, UI_MENU_OPERATION_MODE_DESC); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 28490bdc..2b60b059 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -168,6 +168,9 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_FPS_60] = "60 FPS", [UI_MENU_FPS_90] = "90 FPS", [UI_MENU_FPS_120] = "120 FPS", [UI_MENU_FPS_144] = "144 FPS", // the monitor's refresh rate; printf format with the rate in Hz when it is known [UI_MENU_FPS_DISPLAY] = "Monitor rate", [UI_MENU_FPS_DISPLAY_HZ] = "Monitor rate (%.0f Hz)", + [UI_MENU_FRAME_QUEUE] = "Frame queue", + [UI_MENU_FRAME_QUEUE_DESC] = "The game starts a frame while the previous one waits for its turn on screen: far fewer " + "dropped frames at the limit, up to one frame more input delay. Applies at once.", [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", @@ -621,6 +624,9 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_FPS_30] = "30 FPS", [UI_MENU_FPS_40] = "40 FPS", [UI_MENU_FPS_45] = "45 FPS", [UI_MENU_FPS_60] = "60 FPS", [UI_MENU_FPS_90] = "90 FPS", [UI_MENU_FPS_120] = "120 FPS", [UI_MENU_FPS_144] = "144 FPS", [UI_MENU_FPS_DISPLAY] = "Как у монитора", [UI_MENU_FPS_DISPLAY_HZ] = "Как у монитора (%.0f Гц)", + [UI_MENU_FRAME_QUEUE] = "Очередь кадров", + [UI_MENU_FRAME_QUEUE_DESC] = "Игра начинает кадр, пока предыдущий ждёт показа: намного меньше пропущенных кадров на " + "лимите, задержка ввода больше не более чем на кадр. Применяется сразу.", [UI_MENU_REALTIME] = "Скорость игры по часам", [UI_MENU_REALTIME_DESC] = "Игра идёт по реальному времени, медленный кадр её не замедляет.", [UI_MENU_CLOTH_REALTIME] = "Ткань по часам", @@ -1034,6 +1040,9 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { "a janela perde o foco, como no Switch ao apertar Home; um carregamento interrompido " "assim pode parar de vez.", [UI_MENU_FPS_DISPLAY] = "Taxa do monitor", [UI_MENU_FPS_DISPLAY_HZ] = "Taxa do monitor (%.0f Hz)", + [UI_MENU_FRAME_QUEUE] = "Fila de quadros", + [UI_MENU_FRAME_QUEUE_DESC] = "O jogo começa um quadro enquanto o anterior espera a vez de aparecer: muito menos quadros " + "perdidos no limite, com até um quadro a mais de atraso na entrada. Vale na hora.", [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", @@ -1550,6 +1559,8 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_FPS_144] = "144 FPS", [UI_MENU_FPS_DISPLAY] = "显示器刷新率", [UI_MENU_FPS_DISPLAY_HZ] = "显示器刷新率(%.0f Hz)", + [UI_MENU_FRAME_QUEUE] = "帧队列", + [UI_MENU_FRAME_QUEUE_DESC] = "前一帧等待显示时游戏就开始绘制下一帧:在帧率上限下掉帧大幅减少,输入延迟最多增加一帧。立即生效。", [UI_MENU_REALTIME] = "游戏速度跟随时钟", [UI_MENU_REALTIME_DESC] = "按真实时间推进游戏,帧率低时游戏速度也不会变慢。", [UI_MENU_CLOTH_REALTIME] = "布料跟随时钟", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index edf669fb..73f9bca9 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -62,6 +62,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_FRAME_QUEUE, UI_MENU_FRAME_QUEUE_DESC, 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, diff --git a/totk.ini.example b/totk.ini.example index f5c28b7e..bd2384b0 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -17,6 +17,9 @@ # fps_limit = 60 # Older key, read only while fps_limit is unset: fps60 = 0 means a limit of 30. # fps60 = 1 +# Frame queue: the game starts a frame while the previous one waits for its display time, so a frame that runs long +# still makes its turn. Far fewer dropped frames at the limit for up to one frame more input delay. Takes effect at once. +# frame_queue = 0 # Real-time frame step: game speed follows the clock instead of the frame count. # realtime = 1 # Cloth simulation on the real-time clock. Needs realtime = 1.