From d4cf889a912884d1eb4e495e45a458e38f5689d2 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 11:48:22 -0500 Subject: [PATCH 001/153] Do not count a stale texture pool slot as left for later. A slot whose storage is unmapped has nothing to make, but with the creation budget at 0 the lookup noted it as left before asking whether its storage can be read. In a busy frame, once the frame's creation time was spent, about a thousand stale slots were noted at once, and the pool refresh took that for an area load: up to 274 times a minute in one session's log, each time lifting the count budget and the frame's creation time for the rest of the frame. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_texture.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 2f2f2723..e9fc6390 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -2693,8 +2693,13 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) { // evicted by disuse: the pool refresh keeps the stand-in, without spending the creation budget on it if (pool_ctx && holds.n && nvn_hm_get(&holds, &k)) resolve_held = 1; else if (create_budget < 0) e = entry_of(&s->t); + // Unmapped storage first: such a slot has nothing to make, now or later. Asked after the budget, every stale + // slot a refresh met with the budget at 0 was noted as left: in a busy frame (its creation time spent) that + // was about a thousand slots at once, which vk_exec_draw.c pool_sync_entries took for an area load, several + // times a second in some places, and each time lifted the count budget and the frame's creation time. + else if (!mem_readable((uint64_t)(uintptr_t)s->t.mem, s->t.L.size)) e = NULL; else if (!create_budget && !(s->cwant && want_take())) create_deferred = 1; - else if (mem_readable((uint64_t)(uintptr_t)s->t.mem, s->t.L.size)) { + else { e = entry_of(&s->t); if (create_budget > 0) create_budget--; } From 63c7d3cb741b178ee8deb59e45a99e88b81cc2f6 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 11:48:24 -0500 Subject: [PATCH 002/153] Make the frame limit count the frames the game draws, with frame generation on top. The limit counted the frames on screen, so the game was paced at the limit over frame generation's multiplier: 60 at 4x drew 15 frames a second, and each step up of the multiplier took drawn frames away. Now a limit of 60 at 4x is 60 drawn and 240 shown. Only the display choice still counts the screen's frames, since a frame past the monitor's refresh is never seen, and with no limit set the default is 60 drawn rather than the monitor's rate. Co-Authored-By: Claude Fable 5.2 --- README.md | 4 +-- docs/README-BuildKit.md | 3 +- nx2navi.ini.example | 15 +++++---- runtime/frame_rate.c | 67 +++++++++++++++++----------------------- runtime/host.h | 3 +- runtime/ui/menu.cpp | 6 ++-- runtime/ui_text.c | 13 ++++---- tests/frame_step_check.c | 20 ++++++++++-- 8 files changed, 66 insertions(+), 65 deletions(-) diff --git a/README.md b/README.md index 39abd0f3..9c417385 100644 --- a/README.md +++ b/README.md @@ -36,8 +36,8 @@ Report problems as issues, following [CONTRIBUTING.md](CONTRIBUTING.md). GPUs (one, two or three generated frames for every frame the game draws), or AMD FSR 3.1 (any GPU), a generated frame between every two the game draws, with the HUD kept out of it, for about twice the frame rate on a fast display; in a window of another shape than the picture, frames are generated inside the picture only, so the black - bars stay clean. With frame generation on, the frame limit counts the frames on screen, and with no limit chosen - it is the display's refresh rate. The frame limit can also be + bars stay clean. The frame limit counts the frames the game draws, and frame generation adds its + frames on top of it. The frame limit can also be handed to RivaTuner Statistics Server - HDR on HDR displays: the game's own native HDR, or its SDR picture tone mapped to the display's peak, with exposure, highlights, shadows, contrast, saturation and gamma in the menu - The game's known shaders prepared when the game is built, and the pipelines met in play built again in the diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index ad573506..5f29d7c6 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -153,8 +153,7 @@ beside the game. cards; Automatic picks by the card), Upscaling (DLSS on NVIDIA RTX cards, FSR 3.1 on any card where `amd_fidelityfx_vk.dll` loads), Frame generation (NVIDIA DLSS on RTX 40 and 50 cards, with a DLSS multiplier of 2x, or 3x and 4x on RTX 50 cards; FSR 3.1 on any card where it loads; it needs DLSS or FSR 3.1 - upscaling on, and the frame limit then counts the frames on screen; with no limit chosen it is the display's refresh - rate), HDR (the game's picture tone mapped to your display, with its own grading rows, or Native, the game's own HDR; turn HDR on in Windows first) and the + upscaling on; the frame limit counts the frames the game draws and the generated ones come on top), HDR (the game's picture tone mapped to your display, with its own grading rows, or Native, the game's own HDR; turn HDR on in Windows first) and the resolution. A setting that takes effect only when the game starts again says "after restart" once you change it, until you set it back, and the Restart button at the bottom then counts the settings waiting for it. - **Controls** in the menu has Button prompts: the game's own, or Xbox, PlayStation, Steam Deck or keyboard and mouse ones when diff --git a/nx2navi.ini.example b/nx2navi.ini.example index f0b1fc0b..2ab095cc 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -15,9 +15,9 @@ # 36 do, while 60 does not. vk_present = immediate shows a frame as soon as it is presented, without this quantisation, # at the cost of tearing. A monitor running with variable refresh (FreeSync, G-Sync) follows the frames instead, so # there any limit inside its range is even. -# With frame generation on (framegen, [display]) the limit counts the frames on screen, generated ones included: the -# game draws the limit divided by the multiplier. Unset, the limit is then the monitor's refresh rate instead of 60, so -# at 4x on a 165 Hz monitor the game draws about 41 fps and the screen shows 165; a limit set here is kept as it is. +# With frame generation on (framegen, [display]) the limit still counts the frames the game draws, and the generated +# frames come on top: 60 at 4x is 60 drawn and 240 on screen. Only display counts the frames on screen, so there the +# game draws the monitor's rate divided by the multiplier. # fps_limit = 60 # Where RivaTuner Statistics Server is installed and running, the limit goes into its profile for the game's # executable and RTSS does the limiting instead of the game; closing the game puts back what the profile held. @@ -406,20 +406,19 @@ # frame between every two the game draws, the HUD kept out of it) or dlss (NVIDIA DLSS Frame Generation on GeForce RTX # 40 and 50 GPUs, paced with Reflex; it needs nx2navi_dlssg.dll and NVIDIA Streamline's files next to the executable, # which the setup puts there). Both need DLSS or FSR 3.1 upscaling (dlaa = 1). While frame generation runs, fps_limit -# counts the frames on screen, generated ones included: the game draws the limit divided by the multiplier (2 for fsr3). -# With fps_limit unset it is the display's rate. +# counts the frames the game draws and the screen shows that times the multiplier (2 for fsr3); fps_limit = display +# alone holds the screen to the monitor's rate and the game to that rate divided by the multiplier. # fsr3 takes effect at once, dlss (on or off) at the next start. # framegen = off # On Linux dlss runs through NVIDIA's NGX itself, without Streamline: it needs dlaa = 1 with DLSS when the game starts # and NVIDIA's libnvidia-ngx-dlssg.so. (from NVIDIA's DLSS SDK repository) beside the game, then switches at # once, and its multiplier is fg_mult: 2 to 6 (above 2 needs an RTX 50 GPU) or dynamic, which generates only the frames -# needed to reach the display's refresh rate at the rate the game draws. There fps_limit counts the frames the game -# draws, and framegen_multiplier is not used. +# needed to reach the display's refresh rate at the rate the game draws. framegen_multiplier is not used there. # fg_mult = 2 # DLSS frame generation's multiplier: the frames on screen for each frame the game draws, 2, 3 or 4 (one, two or three # generated). 2 needs an RTX 40 GPU or newer, 3 and 4 an RTX 50 and a current driver. A value above what the GPU and # driver allow (Streamline reports it) is lowered to that, with a line in the log; the menu offers only what they allow. -# Each step adds a little latency, and on a 60 Hz display 4 leaves the game drawing 15 fps. Changes while the game runs +# Each step adds a little latency, and a display shows no more than its refresh rate. Changes while the game runs # take effect at once. # framegen_multiplier = 2 diff --git a/runtime/frame_rate.c b/runtime/frame_rate.c index 25838b33..e7430c9d 100644 --- a/runtime/frame_rate.c +++ b/runtime/frame_rate.c @@ -115,8 +115,7 @@ int frame_step_realtime(void) { return step_word != NULL; } // The frame limit (menu, NX2NAVI_FPS_LIMIT, live): 30, 60, 90, 120, 144, any whole rate from 30 to 240, or display (the // refresh rate of the window's monitor, at most 240). A higher number gives 240, the rate of the shortest step the -// game survives (STEP_MIN_S). Unset, the older NX2NAVI_FPS60=0 still means 30, and frame generation in use means display -// (frame_limit_follows_display). A limit of 30 switches the set +// game survives (STEP_MIN_S). Unset, the older NX2NAVI_FPS60=0 still means 30. A limit of 30 switches the set // fps60 off (the shipping minimum interval 2), every other limit switches it on; the controller reads its minimum // interval from the patched sites each frame. With the real-time step the pacer holds frames to the limit; above 60 // only the pacer changes, as the step's floor is the limit's ceiling. Without it the game steps whole 60 Hz vsyncs and the pacer @@ -144,11 +143,9 @@ int frame_limit_adjustable(void) { return 1; } static unsigned limit_seen; static int limit_read; static int limit_hz = -1; // 0: display; -2: unlimited (free-running builds); -1 before the first read -static int limit_unset; // neither NX2NAVI_FPS_LIMIT nor the older NX2NAVI_FPS60=0 is set: the default 60 static int limit_parse(void) { const char *e = cfg_get("NX2NAVI_FPS_LIMIT"), *old = cfg_get("NX2NAVI_FPS60"); - limit_unset = (!e || !*e) && !(old && !strcmp(old, "0")); if (!e || !*e) return old && !strcmp(old, "0") ? 30 : 60; if (!strcmp(e, "display")) return 0; int free_run = frame_free_running(); @@ -215,22 +212,25 @@ static int movie_cap_hz(void) { // multiplier with NVIDIA's (vk_dlssg.c, one to three); 1 while neither generates (off, or paused for the menu). static int generated_factor(void) { return vk_fg_generating() ? 2 : vk_dlssg_multiplier(); } -// The limit in effect, as limit_hz counts it (0 display, -2 unlimited, else the rate). With frame generation's -// swapchain in use and no limit chosen it is the display's rate: the pacer divides the limit by generated_factor, so -// the default 60 held the game to 30, 20 or 15 rendered frames a second on any monitor, 15 at DLSS 4x on a 165 Hz one, -// where the display rate gives 41 rendered and 165 shown. "In use" rather than generating keeps the limit still while -// the menu pauses generation. A limit the player set is kept as it is. -int frame_limit_follows_display(void) { return limit_unset && limit_hz > 0 && (vk_fg_active() || vk_dlssg_active()); } -static int limit_effective(void) { return frame_limit_follows_display() ? 0 : limit_hz; } - -// The rate the game renders at under the limit: frame_step_limit_hz over the frames shown per game frame (mouse_direct.c, -// whose camera input follows the game's own frame time); 0 unknown. -double frame_step_game_hz(void) { - double hz = frame_step_limit_hz(); - int gen = generated_factor(); +// The limit counts the frames the game draws, and frame generation adds its frames on top: a limit of 60 at 4x is 60 +// drawn and 240 shown. Counted on the screen instead, the game was paced at the limit over the factor, so each step up +// of the multiplier took drawn frames away (60 at 4x drew 15) and the generated ones had less to work from and more +// delay. Only "display" is a rate on the screen, the monitor's: there the game draws that rate over the factor, since a +// frame past the monitor's refresh is never seen. The result is game frames a second, before the ceiling of the step. +static double limit_game_hz(int limit, int gen) { + if (limit) return shown_hz(limit); + double hz = shown_hz(gfx_window_refresh_hz()); return gen > 1 ? hz / gen : hz; } +// The rate the game renders at under the limit (mouse_direct.c, whose camera input follows the game's own frame time); +// 0 unknown or unlimited. +double frame_step_game_hz(void) { + if (frame_step_limit_hz() <= 0) return 0; + double hz = limit_game_hz(limit_hz, generated_factor()); + return !frame_free_running() && !step_word && hz > 60 ? 60 : hz > LIMIT_MAX_HZ ? LIMIT_MAX_HZ : hz; +} + // 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. static double paced_hz; // the game frames a second the newest frame_step_cap_ns paces at (ocarina_step); 0: none @@ -257,26 +257,17 @@ uint64_t frame_step_cap_ns(void) { paced_hz = 0; return 0; } - // unlimited in a build that steps by the clock: LIMIT_FREE_MAX_HZ game frames; during a movie, the movie's rate. - // Both times frame generation's factor for the division below. - int follow = frame_limit_follows_display(), limit = follow ? 0 : limit_hz; - static int follow_was, follow_logs; - if (follow != follow_was) { - follow_was = follow; - if (follow_logs++ < 20) { - if (follow) rt_log("frame rate: frame generation with no limit set: the limit is the monitor's refresh rate"); - else rt_log("frame rate: limit %d fps again", limit_hz); - } - } - double hz = limit == -2 ? (double)(movie_hz ? movie_hz : LIMIT_FREE_MAX_HZ) * gen - : shown_hz(limit ? limit : gfx_window_refresh_hz()); + // unlimited in a build that steps by the clock: LIMIT_FREE_MAX_HZ game frames; during a movie, the movie's rate + double hz = limit_hz == -2 ? LIMIT_FREE_MAX_HZ : limit_game_hz(limit_hz, gen); double max = frame_free_running() ? LIMIT_FREE_MAX_HZ : LIMIT_MAX_HZ; - if (limit == -2) max *= gen; // the cap is on the game's frames there, not the ones shown if (hz > max) hz = max; - // Frame generation presents one to three generated frames between each two of the game's: the game is paced at the - // limit over that factor (half, a third, a quarter), so the screen shows the limit. With VSync and the limit at the - // display's rate, the generated frames then fill the display's refreshes and none is dropped. - if (gen > 1) hz /= gen; + static double logged_hz; + static int logged_gen, gen_logs; + if (gen > 1 && (hz != logged_hz || gen != logged_gen) && gen_logs < 40) { + gen_logs++, logged_hz = hz, logged_gen = gen; + rt_log("frame rate: the game draws up to %.0f frames a second and frame generation shows %d for each (%.0f on " + "screen)", hz, gen, hz * gen); + } if (movie_hz) hz = movie_hz; paced_hz = hz; rtss_limit_want(rtss ? (int64_t)(hz * 1000 + 0.5) : -1); @@ -284,17 +275,17 @@ uint64_t frame_step_cap_ns(void) { return (uint64_t)(1e9 / hz + 0.5); } -// The frame rate the limit gives, for the performance overlay: the setting, the monitor's rate for "display", 60 at +// The limit as it is set, for the performance overlay: the setting, the monitor's rate for "display", 60 at // most without the real-time step; the game's own rate (60 Hz over its present interval) in a build without the patch // sets; 0 before the first frame read the setting. double frame_step_limit_hz(void) { if (frame_free_running()) { // 0 for unlimited, as before the first read frame_limit_live(); - int limit = limit_effective(); + int limit = limit_hz; if (limit == -2 || limit == -1) return 0; return shown_hz(limit ? limit : gfx_window_refresh_hz()); } - int hz = limit_effective(); + int hz = limit_hz; if (hz < 0) return 0; double r = shown_hz(hz ? hz : gfx_window_refresh_hz()); return !step_word && r > 60 ? 60 : r > LIMIT_MAX_HZ ? LIMIT_MAX_HZ : r; diff --git a/runtime/host.h b/runtime/host.h index 9f9137cb..e77c8a6f 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -336,8 +336,7 @@ int ov_movie_live(void); // ov_movie2.c: a mov int frame_limit_adjustable(void); // the frame limit setting changes the rate (always) int frame_free_running(void); // no fps60/realtime patch set: the game steps by the clock double frame_step_limit_hz(void); // the frame rate the limit gives (overlay); 0 unknown -double frame_step_game_hz(void); // ... over frame generation's factor: the game's rate -int frame_limit_follows_display(void); // no limit set and frame generation in use: the display's rate +double frame_step_game_hz(void); // the rate the game draws at under the limit; 0 unknown // 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 int rtss_limit_active(void); // RTSS limits to the wanted rate: the pacer holds no frames diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 65c25399..ff048e40 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -73,7 +73,6 @@ int vk_ffx_available(void); // vk_ffx.cpp: FSR 3.1 upscaling can run int vk_ffx_framegen_available(void); // ... and FSR 3.1 frame generation int vk_texture_detail_auto(void); // vk_texture.c: the texture detail auto picks on this card, 0 full to 2 low double frame_step_limit_hz(void); -int frame_limit_follows_display(void); int frame_limit_adjustable(void); int rtss_limit_state(void); int frame_free_running(void); @@ -756,8 +755,7 @@ static void row_game_lang(void) { opt_choice_list("NX2NAVI_LANG", UI_MENU_GAME_LANG, vals, names, n + 1, UI_MENU_GAME_LANG_DESC, cur); } -// The frame limit (frame_rate.c): unset, the older fps60 = 0 still means 30, and frame generation in use means the -// monitor's rate (frame_limit_follows_display), which the row then shows. The monitor choice shows the rate it gives. +// The frame limit (frame_rate.c): unset, the older fps60 = 0 still means 30. The monitor choice shows the rate it gives. // The note says when RivaTuner Statistics Server does the limiting (rtss_limit.c), cannot change its profile, or let the // game run past its limit. static void row_fps_limit() { @@ -778,7 +776,7 @@ static void row_fps_limit() { names[9] = display; } const char *set = cfg_get("NX2NAVI_FPS_LIMIT"), *old = cfg_get("NX2NAVI_FPS60"); - int cur = (!set || !*set) && old && !strcmp(old, "0") ? 0 : frame_limit_follows_display() ? 9 : -1; + int cur = (!set || !*set) && old && !strcmp(old, "0") ? 0 : -1; int rtss = rtss_limit_state(); const char *note = rtss == 1 ? menu_text(UI_MENU_FPS_RTSS) : rtss == 2 ? menu_text(UI_MENU_FPS_RTSS_READONLY) diff --git a/runtime/ui_text.c b/runtime/ui_text.c index d68515f2..faaa02d3 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -179,7 +179,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_FXAA_DESC] = "The game's own FXAA edge smoothing. It blurs fine detail a little; with DLAA on it is not needed.", [UI_MENU_FPS_LIMIT] = "Frame rate limit", - [UI_MENU_FPS_LIMIT_DESC] = "The highest frame rate the game runs at.", + [UI_MENU_FPS_LIMIT_DESC] = "The highest frame rate the game draws at. Frame generation adds its frames on top.", [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", // the monitor's refresh rate; printf format with the rate in Hz when it is known @@ -575,9 +575,8 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_FG_MULT_DYNAMIC] = "Dynamic", [UI_MENU_FRAMEGEN_MULT_DESC] = "Frames on screen for each frame the game draws: 2x adds one generated frame, 3x " "two, 4x three. Higher is smoother but adds a little delay. The frame limit counts " - "the frames on screen, so the game draws the limit divided by this: set the limit " - "to your display's rate. 2x needs a GeForce RTX 40 GPU or newer, 3x and 4x an RTX " - "50.", + "the frames the game draws, so the screen shows the limit times this. 2x needs a " + "GeForce RTX 40 GPU or newer, 3x and 4x an RTX 50.", [UI_MENU_FRAMEGEN_MULT_RTX50] = "3x and 4x need an RTX 50 GPU", [UI_MENU_FRAMEGEN_MULT_UPTO] = "up to %dx on this GPU and driver", [UI_MENU_FRAMEGEN_MULT_OLD_DLL] = "2x only: update the DLSS frame generation files", @@ -744,7 +743,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_FXAA_DESC] = "Собственное сглаживание FXAA. Слегка размывает мелкие детали; с DLAA оно не нужно.", [UI_MENU_FPS_LIMIT] = "Ограничение частоты кадров", - [UI_MENU_FPS_LIMIT_DESC] = "Максимальная частота кадров игры.", + [UI_MENU_FPS_LIMIT_DESC] = "Максимальная частота кадров, которые рисует игра. Генерация кадров добавляет свои сверх неё.", [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 Гц)", @@ -1202,7 +1201,7 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_SHARPNESS_NEEDS_FSR] = "só com AMD FSR 1", [UI_MENU_FPS_LIMIT] = "Limite de quadros", - [UI_MENU_FPS_LIMIT_DESC] = "A taxa de quadros máxima do jogo.", + [UI_MENU_FPS_LIMIT_DESC] = "A taxa máxima de quadros que o jogo desenha. A geração de quadros soma os dela por cima.", [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_ASSUME_FOCUS] = "Continuar rodando fora de foco", @@ -1781,7 +1780,7 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { [UI_MENU_COLOR_GAMMA] = "伽马", [UI_MENU_COLOR_GAMMA_DESC] = "小于 1 提亮暗部与中间调,大于 1 压暗;1 表示不调整。", [UI_MENU_FPS_LIMIT] = "帧率上限", - [UI_MENU_FPS_LIMIT_DESC] = "游戏的最高帧率。", + [UI_MENU_FPS_LIMIT_DESC] = "游戏绘制的最高帧率。帧生成的帧在此之上另算。", [UI_MENU_FPS_30] = "30 FPS", [UI_MENU_FPS_40] = "40 FPS", [UI_MENU_FPS_45] = "45 FPS", diff --git a/tests/frame_step_check.c b/tests/frame_step_check.c index fa6e503e..7628ec1a 100644 --- a/tests/frame_step_check.c +++ b/tests/frame_step_check.c @@ -32,8 +32,9 @@ int ov_movie_live(void) { return 0; } int vk_fg_generating(void) { return 0; } int vk_fg_active(void) { return 0; } int vk_fg_wanted(void) { return 0; } -int vk_dlssg_multiplier(void) { return 1; } -int vk_dlssg_active(void) { return 0; } +static int fg_mult = 1; // frames on screen per game frame +int vk_dlssg_multiplier(void) { return fg_mult; } +int vk_dlssg_active(void) { return fg_mult > 1; } void rtss_limit_want(int64_t millihz) { (void)millihz; } int rtss_limit_active(void) { return 0; } int plat_mem_read(uint64_t a, void *out, size_t n) { (void)a, (void)out, (void)n; return 0; } @@ -135,6 +136,21 @@ int main(int argc, char **argv) { float g = present(0.0011); // 900 fps, as a quick menu without the pacer would run CHECK(g >= 0.125f, "short frames: %.4f frames", g); } + // The limit counts the frames the game draws: frame generation's multiplier leaves the pace of a numeric limit and + // of unlimited alone, and divides only the monitor's rate (144 Hz here), which is a rate on the screen. + static const struct { const char *l; int gen; double hz, game_hz; } caps[] = { + {"60", 1, 60, 60}, {"60", 4, 60, 60}, {"120", 2, 120, 120}, {"240", 4, 240, 240}, + {"display", 1, 144, 144}, {"display", 4, 36, 36}, {"unlimited", 1, 240, 0}, {"unlimited", 4, 240, 0}}; + for (unsigned i = 0; i < sizeof caps / sizeof *caps; i++) { + set_limit(caps[i].l); + fg_mult = caps[i].gen; + double hz = 1e9 / (double)frame_step_cap_ns(), game = frame_step_game_hz(); + CHECK(fabs(hz - caps[i].hz) < 0.01, "limit %s at %dx paces the game at %.3f fps, not %.0f", caps[i].l, caps[i].gen, + hz, caps[i].hz); + CHECK(fabs(game - caps[i].game_hz) < 0.01, "limit %s at %dx reports %.3f game fps, not %.0f", caps[i].l, + caps[i].gen, game, caps[i].game_hz); + } + fg_mult = 1; printf("frame_step_check: %s\n", fails ? "FAILED" : "ok"); return fails != 0; } From bfcefe6fa24bd236fc11c617a4beef7b8cde44f5 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:02:12 -0500 Subject: [PATCH 003/153] A frame no longer stops for the pipelines of an area's first frame: passes the game renders frame after frame stay per-frame through a loading screen, and the waits that remain share 50 ms a frame. A draw whose pipeline is still building is skipped when its pass is rendered every frame and waits otherwise, so that a lookup table or a volume generated once is not left empty. "Every frame" meant "in the last four frames", so after a loading screen or a full-screen menu every pass of the first frame read as new: each draw with a new pipeline waited on the replay thread, up to 30 s each, and a mesh draw, whose pipeline has no fast link, for its whole compile. A player's 0.7.1 log shows it at three area changes: frames of 954, 1015 and 684 ms with 853, 960 and 663 ms of pipeline wait, six mesh draws waiting 52 to 660 ms in passes whose targets had been rendered in thousands of frames. The target history (now runtime/vk_target_frames.h) also counts the frames each target was rendered in, for as long as it is the same guest texture. From eight frames on its pass is per-frame however long ago the last one was, and its draws are skipped and built by the workers like any other (pipe_skip_steady, on by default). A target rendered for the first time still waits, now from one pool of 50 ms a presented frame that the full-screen draws share (pipe_frame_wait_ms; oneshot_budget = 0 gives the old wait back), a mesh draw 8 ms at most, and the wait for a one-shot draw's shader comes from the same pool (gfx_shader_await takes a limit). A full-screen draw with no picture left to hold waits 40 ms more instead of 500. One frame so waits under 100 ms for pipelines in all. Every 600 frames with a miss the log says how many draws found a shader or pipeline missing, how long they held the replay thread in all and in the worst frame, by the kind of wait, and how many were skipped at its end. tests/target_frames_check.py replays the area change of that log against the header. Not yet run in the game. Co-Authored-By: Claude Fable 5.2 --- docs/architecture.md | 7 +- docs/building.md | 6 ++ nx2navi.ini.example | 21 ++-- runtime/cfg.c | 7 +- runtime/gfx.h | 7 +- runtime/gfx_shader.c | 23 +++-- runtime/vk_exec.c | 102 +++++++----------- runtime/vk_exec.h | 5 +- runtime/vk_exec_draw.c | 193 +++++++++++++++++++++++++---------- runtime/vk_target_frames.h | 106 +++++++++++++++++++ tests/target_frames_check.c | 119 +++++++++++++++++++++ tests/target_frames_check.py | 40 ++++++++ 12 files changed, 492 insertions(+), 144 deletions(-) create mode 100644 runtime/vk_target_frames.h create mode 100644 tests/target_frames_check.c create mode 100644 tests/target_frames_check.py diff --git a/docs/architecture.md b/docs/architecture.md index 69b58511..7c3830dc 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -131,7 +131,12 @@ keeps per-core data without locks and relies on that, so the host models it (`ru ring of Vulkan `{x, y, z}` records filled on the GPU. - Pipelines: `VK_EXT_graphics_pipeline_library` where the driver has it: stage libraries compiled ahead, a fast link at a new state, an optimised pipeline replacing it from a background thread. A draw whose pipeline is not ready is - skipped when one of its pass's targets was rendered in the previous frame, and waits otherwise. Three things keep + skipped when its pass is one the game renders frame after frame: one of its targets was rendered in the last few + frames, or in eight frames or more at any time, which keeps the first frame after a loading screen from reading as + all new (`runtime/vk_target_frames.h`). A draw into a target rendered for the first time, and a full-screen draw, + wait instead, from one pool of 50 ms a presented frame (`pipe_frame_wait_ms`; `runtime/vk_exec_draw.c` + `prepare_draw_ex`, `frame_wait`); what is not ready when the pool is spent is skipped and built on a worker, so no + frame waits for pipelines much longer than that. Three things keep that short on a first visit (`runtime/vk_pipeline.c`): a pipeline whose fragment shader reads a target of its own pass is fast-linked like any other, the feedback loop flags being part of the fragment output library alone (`gpl_feedback`); the library workers also link the pipelines of skipped draws, so a burst of new materials is diff --git a/docs/building.md b/docs/building.md index 8237793c..508936f5 100644 --- a/docs/building.md +++ b/docs/building.md @@ -322,6 +322,12 @@ A change to what the library warm logs when the driver's cache of stage librarie `runtime/vk_pipeline.c` `lw_main`) passes `python tests\warm_hint_check.py`, which builds the header alone and checks the added text by GPU vendor and by why the cache was cold. No game files. +A change to which render passes count as drawn frame after frame (`runtime/vk_target_frames.h`: a draw into one is +skipped while its pipeline builds, a draw into any other waits) passes `python tests\target_frames_check.py`, which +builds the header alone and replays an area change after a loading screen from a player's log: the scene, probe and +half-resolution passes are per-frame again at once, a lookup table, a volume's slices and an icon capture still wait, +and an image taken over by another texture starts without the history of the one before. No GPU, no game files. + The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: diff --git a/nx2navi.ini.example b/nx2navi.ini.example index f0b1fc0b..51b33931 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -90,9 +90,17 @@ # Draws whose pipeline is still building: default waits on a reserved worker; draw is the previous scheduling. # pipe_wait = default # Which draws are dropped instead of waiting for their pipeline: strict (only draws into per-frame passes), any (also -# draws into passes whose target a recent frame rendered; full-screen draws wait, up to 50 ms a frame) or fullscreen +# draws into passes whose target a recent frame rendered; full-screen draws wait, from pipe_frame_wait_ms) or fullscreen # (those and full-screen draws: no wait, but a lighting or fog pass that is dropped shows as a black or unlit frame). # pipe_skip = any +# How many milliseconds one frame may wait, in all, for the shaders and pipelines of its full-screen draws and of +# draws into pictures drawn for the first time (0 to 500). What is not ready by then is left out of that frame and +# built in the background. +# pipe_frame_wait_ms = 50 +# A picture the game has drawn in many frames counts as drawn every frame after a pause too (a loading screen, a +# menu), so the first frame of an area leaves a draw out while its pipeline is built instead of stopping for it. +# 0: as up to 0.7.1, such a frame waits for each new pipeline in turn. +# pipe_skip_steady = 1 # How many milliseconds the draws of one frame may wait, in all, for a missing pipeline to be linked from the shader # libraries in the graphics driver's cache before they are dropped (0 to 100; 0: dropped at once, as before). # pipe_link_ms = 6 @@ -112,13 +120,12 @@ # NVIDIA under Windows, Shader Cache Size Unlimited in the NVIDIA app), start once with force, or delete # shader-warm.txt, to warm again. # lib_warm = 1 -# How long (ms) a draw into a target no recent frame rendered waits for a pipeline built for the first time, -# then is skipped. The default waits, as such a target (a lookup table, a new area's target) can stay empty if -# skipped; a lower value caps the stall. +# Draws into a picture drawn for the first time (a lookup table, a new area's target) wait for a pipeline built for +# the first time from pipe_frame_wait_ms; one left over is drawn the next frame its picture is drawn. 0: as up to +# 0.7.1, each such draw waits up to oneshot_wait_ms milliseconds, which stops the game for as long as the pipelines +# of a new area take to build. +# oneshot_budget = 1 # oneshot_wait_ms = 30000 -# 1: those draws share a wait budget per presented frame instead (100 ms for large passes, 300 ms for small ones); a -# draw left over is drawn the next frame its target is drawn. Shortens the long first frame of a new area. -# oneshot_budget = 0 # Build shader stage libraries in the background ahead of time, and the threads for it (1 to 16; default 2). More # threads mostly wait on the graphics driver's lock: 16 on NVIDIA spent about 8 cores spinning in the driver. # lib_prebuild = 1 diff --git a/runtime/cfg.c b/runtime/cfg.c index 2605166f..df0d434e 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -43,7 +43,7 @@ static const char *const known[] = { "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLSS_IDLE_FREE", "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", "DUMP_RAW", "DUMP_SHADERS", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLIGHT_EVERY", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FG_MULT", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", - "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_LINK_DRAW_MS", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", + "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_FRAME_WAIT_MS", "PIPE_LINK_DRAW_MS", "PIPE_SKIP_STEADY", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_LIVE", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", @@ -114,6 +114,7 @@ static const struct { const char *key, *section; } sections[] = { {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, {"POOL_CREATE_MS", "performance"}, {"POOL_LOAD_BACKLOG", "performance"}, {"POOL_LOAD_MS", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, + {"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"PIPE_PREBUILD", "performance"}, {"COMPUTE_KEEP", "performance"}, @@ -229,6 +230,8 @@ static const CfgMeta metas[] = { {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART}, {"PIPE_LINK_MS", "6", "0..100", RESTART}, // vk_exec_draw.c link_budget {"PIPE_LINK_DRAW_MS", "3", "1..50", RESTART}, // vk_exec_draw.c link_draw_ms + {"PIPE_FRAME_WAIT_MS", "50", "0..500", RESTART}, // vk_exec_draw.c frame_wait + {"PIPE_SKIP_STEADY", "1", "0|1", RESTART}, // vk_exec.c steady_on {"PIPE_AHEAD", "1", "0|1", RESTART}, // vk_exec.c vk_exec_ahead, vk_pipeline.c vk_pipeline_hint {"GPL_FEEDBACK", "1", "0|1", RESTART}, // vk_pipeline.c gpl_feedback {"PIPE_FAST_WORKERS", "1", "0|1", RESTART}, // vk_pipeline.c lib_worker, fast_take @@ -238,7 +241,7 @@ static const CfgMeta metas[] = { {"LIB_WARM", "1", "0|1|force", RESTART}, // vk_pipeline.c lw_start: the driver's cache of stage libraries, warmed when cold {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, - {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "0", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, + {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "1", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, {"BIG_BUILD_MAX_MB", "4096", "0..262144", RESTART}, // all|: the compute pipelines the boot keeps, those dispatched most recently (vk_pipeline.c, compute-used.bin) {"COMPUTE_KEEP", "512", NULL, RESTART}, diff --git a/runtime/gfx.h b/runtime/gfx.h index 0619209f..168090e5 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -48,10 +48,11 @@ const GfxShader *gfx_shader_get(int stage, const uint8_t *control, const uint8_t // NULL returned with *pending = 1 until it is ready. A failed translation stays failed (*pending = 0). const GfxShader *gfx_shader_get_async(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, const GfxShaderAnswers *a, int *pending); -// As gfx_shader_get_async, but a shader queued for translation is moved to the front of the queue and waited for. -// NULL when the translation failed. +// As gfx_shader_get_async, but a shader queued for translation is moved to the front of the queue and waited for, +// wait_ms at most (UINT32_MAX: until it is done). NULL when the translation failed, or with *pending = 1 when it was +// still running at the end of the wait: it stays at the front of the queue. const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, - const GfxShaderAnswers *a); + const GfxShaderAnswers *a, uint32_t wait_ms, int *pending); uint64_t gfx_shader_async_done(void); // translations finished by the background worker void gfx_shader_counts(uint64_t *keys, uint64_t *translated); // distinct keys in memory, translations done (NX2NAVI_TRACE=perf) void gfx_shader_disk_counts(uint64_t *sync, uint64_t *sync_ns, uint64_t *bg); // disk cache reads on the render diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index c822fc0c..51feb53f 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -2211,24 +2211,31 @@ static const GfxShader *get_async_one(Req r, int stage, const uint8_t *control, } const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, - const GfxShaderAnswers *a) { - int pending; + const GfxShaderAnswers *a, uint32_t wait_ms, int *pending) { uint8_t ctl2[CONTROL_SIZE]; control = nvn2_control(control, ctl2); - const GfxShader *s = gfx_shader_get_async(stage, control, code, code_size, a, &pending); + const GfxShader *s = gfx_shader_get_async(stage, control, code, code_size, a, pending); Req r; - if (!pending || !req_init(&r, stage, control, code, code_size, NULL, a)) return s; + if (!*pending) return s; + *pending = 0; + if (!req_init(&r, stage, control, code, code_size, NULL, a)) return s; uint64_t p0 = perf_begin(); // the wait counts as the render thread's translation time + uint64_t end = wait_ms == UINT32_MAX ? 0 : plat_now_ms() + wait_ms, now = 0; plat_mutex_lock(&X.lock); to_front(&X.lhead, &X.ltail, r.key); // queued: to the front, a worker takes it next to_front(&X.head, &X.tail, r.key); XWait *w; - while ((w = *wait_slot(r.key)) && !w->failed) plat_cond_wait(&X.done, &X.lock); + while ((w = *wait_slot(r.key)) && !w->failed && (!end || (now = plat_now_ms()) < end)) { + if (end) plat_cond_wait_ms(&X.done, &X.lock, (uint32_t)(end - now)); + else plat_cond_wait(&X.done, &X.lock); + } + int failed = w && w->failed; plat_mutex_unlock(&X.lock); perf_end(PERF_XLATE, p0); - if (w) return NULL; // failed: logged by the worker - s = gfx_shader_get_async(stage, control, code, code_size, a, &pending); - return pending ? NULL : s; + if (failed) return NULL; // logged by the worker + if (w) return *pending = 1, NULL; // the wait ended first + s = gfx_shader_get_async(stage, control, code, code_size, a, pending); + return *pending ? NULL : s; } uint64_t gfx_shader_async_done(void) { return (uint64_t)n_async; } diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 6ac73408..2d7e94e4 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -26,6 +26,7 @@ #include "tex.h" #include "vk.h" #include "vk_exec.h" +#include "vk_target_frames.h" #include "vk_dlaa.h" #define WEAK __attribute__((weak)) @@ -477,73 +478,34 @@ static void free_views(void *arg) { // completion thread, once the submission free(l); } -// ---- target history -// The presented frame (gfx_counters.frames + 1; 0: never) each target subresource was last rendered into, and the one -// before. prepare_draw lets a draw into a target no recent frame rendered (a one-shot draw: a volume or LUT generated -// once) wait for its shaders and pipeline instead of skipping it. Per subresource, not per image: a volume may be -// generated a few slices per frame. -// Recent means within TFR_RECENT frames, not the frame before alone: the game's output images are a ring of three, its -// probe and shadow layers are refreshed in turn every second to fourth frame, and the mips of its small effect buffers -// alternate between images. Sets of TFR_WAYS, the oldest entry replaced: hundreds of subresources are rendered a -// frame, and in a direct-mapped table of 1024 a fifth of the scene passes and most of the probe layers read as never -// rendered because another subresource had taken their slot. Each misread made a draw whose pipeline was still -// building wait for it (ONESHOT_WAIT_MS), so a new area was entered one pipeline at a time. -#define TFR_SETS 1024 -#define TFR_WAYS 4 -#define TFR_RECENT 4 -typedef struct { VkImage image; uint32_t level, layer; uint64_t last, prev; } TargetFrames; -static TargetFrames tfr[TFR_SETS * TFR_WAYS]; +// ---- target history (vk_target_frames.h: the table, what recent and steady mean, and the pass verdict) +uint64_t vk_exec_steady_passes; // lookups a steady target alone made per-frame (vk_exec_targets_recent) -// The target's entry, or with make the entry it takes (the set's oldest, an empty one first); NULL without make -// when it has none. -static TargetFrames *tfr_find(const VkExecTarget *t, int make) { - uint64_t h = ((uint64_t)(uintptr_t)t->image ^ (uint64_t)t->level << 56 ^ (uint64_t)t->base_layer << 40) * - 0x9E3779B97F4A7C15ull; - TargetFrames *set = &tfr[(h >> 54) * TFR_WAYS], *oldest = set; - for (int i = 0; i < TFR_WAYS; i++) { - if (set[i].image == t->image && set[i].level == t->level && set[i].layer == t->base_layer) return &set[i]; - if (set[i].last < oldest->last) oldest = &set[i]; +static TfrTarget tfr_of(const VkExecTarget *t) { + return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->tex ? t->tex->serial : 0}; +} + +static int steady_on(void) { + static int on = -1; + if (on < 0) { + on = cfg_bool("NX2NAVI_PIPE_SKIP_STEADY"); + rt_log("vk: a pass whose targets the game has rendered in %d frames or more counts as per-frame after a " + "pause too, so its draws are skipped, not waited for, while a pipeline builds: %s (NX2NAVI_PIPE_SKIP_STEADY)", + TFR_STEADY, on ? "on" : "off"); } - if (!make) return NULL; - *oldest = (TargetFrames){t->image, t->level, t->base_layer, 0, 0}; - return oldest; + return on; } -static void tfr_note(const VkExecTarget *t, uint64_t stamp) { - TargetFrames *e = tfr_find(t, 1); - if (e->last != stamp) e->prev = e->last, e->last = stamp; -} - -// The frame before the current one that last rendered the target, 0 for none. -static uint64_t tfr_before(const VkExecTarget *t, uint64_t stamp) { - const TargetFrames *e = tfr_find(t, 0); - return !e ? 0 : e->last == stamp ? e->prev : e->last; -} - -static int tfr_recent(const VkExecTarget *t, uint64_t stamp) { - uint64_t before = tfr_before(t, stamp); - return before && stamp - before <= TFR_RECENT; -} - -// No frame before the current one rendered the target. -static int tfr_first(const VkExecTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } - -// With any, a recent depth target alone does not make the pass per-frame when a colour target is rendered for the -// first time: the game's item icon capture (an inventory icon) draws into a new colour target with the -// per-frame depth buffer, once. Its draws skipped there stay missing from the icon until the item is captured again. +// tfr_pass_recent for the current pass; vk_exec_steady_passes counts the lookups that only a steady target made +// per-frame. int vk_exec_targets_recent(VkExec *ex, int any) { resolve_rt(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; - int n = 0, color = 0, depth = 0, color_first = 0; - for (int i = 0; i < 9; i++) { - const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth; - if (!t->image) continue; - n++; - if (tfr_recent(t, stamp)) *(i < 8 ? &color : &depth) = 1; - else if (!any) return 0; - else if (i < 8 && tfr_first(t, stamp)) color_first = 1; - } - return any ? color || (depth && !color_first) : n > 0; + TfrTarget c[8], d = tfr_of(&ex->depth); + for (int i = 0; i < 8; i++) c[i] = tfr_of(&ex->color[i]); + int by_steady = 0, r = tfr_pass_recent(c, 8, &d, stamp, any, steady_on(), &by_steady); + vk_exec_steady_passes += (uint64_t)by_steady; + return r; } // One line naming each target of the current pass: format, extent, layer and the two frames it was last rendered in @@ -555,10 +517,12 @@ void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap) { for (int i = 0; i < 9 && n < cap; i++) { const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth; if (!t->image) continue; - const TargetFrames *e = tfr_find(t, 0); - int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu", n ? "; " : "", i < 8 ? 'c' : 'd', - i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, - (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0)); + TfrTarget ft = tfr_of(t); + const TargetFrames *e = tfr_find(&ft, 0); + int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu in %u frames", n ? "; " : "", + i < 8 ? 'c' : 'd', i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, + (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0), + e && e->serial == ft.serial ? e->n : 0); if (w < 0) break; n += (size_t)w; } @@ -699,8 +663,14 @@ void vk_exec_pass_end(VkExec *ex) { if (vk_dump_armed) vk_dump_pass_end(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; for (int i = 0; i < 8; i++) - if (ex->color[i].image) vk_texture_gpu_written(ex->color[i].tex), tfr_note(&ex->color[i], stamp); - if (ex->depth.image) vk_texture_gpu_written(ex->depth.tex), tfr_note(&ex->depth, stamp); + if (ex->color[i].image) { + TfrTarget ft = tfr_of(&ex->color[i]); + vk_texture_gpu_written(ex->color[i].tex), tfr_note(&ft, stamp); + } + if (ex->depth.image) { + TfrTarget ft = tfr_of(&ex->depth); + vk_texture_gpu_written(ex->depth.tex), tfr_note(&ft, stamp); + } } VkCommandBuffer vk_exec_inline_begin(int *reopen) { diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 6ce192c6..40645d56 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -159,9 +159,10 @@ VkRect2D vk_exec_scissor(const VkExec *ex); void vk_exec_feedback_war(VkExec *ex, uint32_t written); void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h); // pass size when no attachment is set // 1: every target of the current pass (any 1: at least one, and not only the depth target while a colour target is -// rendered for the first time) was rendered in the previous presented frame (per-frame targets); 0 otherwise, or when -// the pass has none. +// rendered for the first time) is one the game renders frame after frame: rendered in the last few presented frames, +// or steady (vk_target_frames.h tfr_pass_recent); 0 otherwise, or when the pass has none. int vk_exec_targets_recent(VkExec *ex, int any); +extern uint64_t vk_exec_steady_passes; // its answers of 1 that only a steady target gave (vk_target_frames.h) // Looking ahead (vk_exec.c): called by a draw that found its shader or pipeline missing, before it waits or is skipped. // The rest of the submission being replayed is read once and the stage libraries of the pipelines its later draws need // are queued to the workers (vk_pipeline_hint). vk_exec_ahead_op (vk_exec_draw.c) applies one bind or draw op of that diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4f375ebe..683ec91a 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -43,10 +43,19 @@ #define NVN_STAGE_CS 5 #define NVN_STAGE_TASK 6 // NVN2 #define NVN_STAGE_MESH 7 -// prepare_draw: how long a one-shot draw waits for its pipeline before it is skipped (NX2NAVI_ONESHOT_WAIT_MS, default -// 30000). Such a draw renders into a target the previous frame did not (a volume, a LUT, a target made new at an area -// change); skipped, the target can stay empty for the session, so the default waits. A lower value trades that risk for -// a frame that never stalls longer than the value on a first-time pipeline. +// prepare_draw: how long a one-shot draw waits for its shaders and pipeline before it is skipped. Such a draw renders +// into a target that is neither recent nor steady (vk_target_frames.h: a volume, a lookup table, an icon, a target +// made new at an area change); skipped, a target the game draws once stays as it was. +// NX2NAVI_ONESHOT_BUDGET (default 1): the one-shot draws of a presented frame wait from that frame's wait pool +// (frame_wait, shared with the full-screen draws), a mesh draw MESH_WAIT_MS at most. Until 0.7.1 each waited up to +// NX2NAVI_ONESHOT_WAIT_MS (30 s) on the replay thread, which is what =0 still does: after a scene load every pass was +// new or not recent, so the first frame waited for one pipeline after another, and for the whole compile of each mesh +// pipeline (0.2 to 1.3 s cold): 5-14 s on an RTX 4080 SUPER with no pipeline recorded, and frames of 0.7 to 1 s at +// area changes of a played install (vk_target_frames.h "Steady targets"), while the pipeline workers sat idle. A +// draw skipped at the end of its wait comes back the next frame its target is drawn, when the target is recent and +// the pipeline is built on a worker like any other; a target drawn only once whose pipeline took longer than the pool +// stays as it was until the game draws it again. With the stage libraries in the driver's cache a fast link takes +// 2 to 6 ms, so the pool covers the lookup tables of a load several times over. static uint32_t oneshot_wait_ms(void) { static int64_t ms = -1; if (ms < 0) { @@ -58,32 +67,18 @@ static uint32_t oneshot_wait_ms(void) { return (uint32_t)ms; } #define ONESHOT_WAIT_MS oneshot_wait_ms() -// NX2NAVI_ONESHOT_BUDGET=1: one-shot draws wait for their pipelines from a budget per presented frame instead of up to -// ONESHOT_WAIT_MS each (oneshot_budget): one budget for passes of ONESHOT_LARGE_PX pixels or more (the scene, its -// half-resolution buffers, shadow maps) and one for smaller passes (LUTs, icon captures, the smallest bloom levels). -// After a scene load every pass is new, so every draw of the first frame counts as one-shot, and the frame waited for -// one cold pipeline after another, 300-1200 ms each: 5-14 s on an RTX 4080 SUPER while the workers sat idle. A draw -// skipped for the budget comes back next frame, when its pass is recent and its pipeline is built on a worker like any -// other; a target drawn only once (a LUT baked at load) stays as it was until the game draws it again. Off by default -// until play shows no target left empty that way. -#define ONESHOT_LARGE_PX 400000u -#define ONESHOT_BUDGET_LARGE_NS 100000000ull -#define ONESHOT_BUDGET_SMALL_NS 300000000ull +// A mesh pipeline has no stage libraries and so no fast link: the wait is for its whole build, a few milliseconds +// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which. +#define MESH_WAIT_MS 8u static int oneshot_budget_on(void) { static int on = -1; if (on < 0) { - on = rt_knob("ONESHOT_BUDGET", "0") == 1; - if (on) rt_log("vk_exec: one-shot draws wait for their pipelines from a per-frame budget (NX2NAVI_ONESHOT_BUDGET=1)"); + on = rt_knob("ONESHOT_BUDGET", "1") == 1; + if (!on) rt_log("vk_exec: a one-shot draw waits up to %u ms for its shaders and pipeline, outside the frame's wait " + "pool (NX2NAVI_ONESHOT_BUDGET=0)", ONESHOT_WAIT_MS); } return on; } -// The wait budget left to this frame's one-shot draws (prepare_draw_ex), by pass size, reset at each presented frame. -static uint64_t *oneshot_budget(int large) { - static uint64_t frame = ~0ull, left[2]; - uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); - if (f != frame) frame = f, left[0] = ONESHOT_BUDGET_SMALL_NS, left[1] = ONESHOT_BUDGET_LARGE_NS; - return &left[large != 0]; -} enum { W_PROGRAM, W_SHADER, W_PIPELINE, W_NO_VS, W_INDEX, W_UB, W_BIND, W_SSBO_ALIGN, W_SSBO_SLOT, W_RESTART, W_INDIRECT, W_MESH, W_COUNT }; @@ -945,11 +940,12 @@ static void answers_for(VkExec *ex, int st, const uint8_t *ctl, uint32_t mask, G } // *present: the program has this stage. Returns NULL when it has not or the translation failed. pending (graphics -// stages, vk_pipeline_async_on): a shader not translated yet is queued and NULL returned with *pending = 1. await: a -// shader queued earlier is waited for (gfx_shader_await) instead. +// stages, vk_pipeline_async_on): a shader not translated yet is queued and NULL returned with *pending = 1. await_ms: +// a shader queued earlier is waited for instead, that long at most (gfx_shader_await; UINT32_MAX: until it is done), +// and *pending = 1 says the wait ended first. static int sf_mvprev; // shader_for asks for the vertex shader's last-frame variant (per-object motion) -static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pending, int await) { +static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pending, uint32_t await_ms) { const uint8_t *ctl, *code; uint32_t len; *present = 0; @@ -977,7 +973,9 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin } int gs = st == NVN_STAGE_VS ? GFX_STAGE_VS : st == NVN_STAGE_FS ? GFX_STAGE_FS : st == NVN_STAGE_TASK ? GFX_STAGE_TS : st == NVN_STAGE_MESH ? GFX_STAGE_MS : GFX_STAGE_CS; - const GfxShader *s = await ? gfx_shader_await(gs, ctl, code, len, &a) + int late = 0; + if (await_ms && !pending) pending = ⪭ + const GfxShader *s = await_ms ? gfx_shader_await(gs, ctl, code, len, &a, await_ms, pending) : pending && (vk_pipeline_async_on() || a.mvprev) ? gfx_shader_get_async(gs, ctl, code, len, &a, pending) : gfx_shader_get(gs, ctl, code, len, NULL, &a); if (pending && *pending) return NULL; @@ -3341,6 +3339,61 @@ static int pipe_skip_mode(void) { // cold it runs out after a few draws and the rest are skipped as before, so a burst of cold pipelines costs a frame // this much and no more. Measured on an RX 7900 XT, first walk through a village with no pipeline recorded: 2,349 // draws skipped without the budget, with the libraries warm in both runs. +// NX2NAVI_PIPE_FRAME_WAIT_MS (default 50; 0 to 500): the wait pool of one presented frame. Its full-screen draws and +// its one-shot draws (NX2NAVI_ONESHOT_BUDGET) wait for their shaders and pipelines from it, each for what the ones +// before left, and are skipped once it is spent, so the pipelines of one frame hold the replay thread this long in +// all and no longer (beside the fast links' own small budget, link_budget, and FULLSCREEN_LAST_MS). +static uint64_t *frame_wait(void) { + static uint64_t frame = ~0ull, left, per_frame = ~0ull; + if (per_frame == ~0ull) { + long long ms = cfg_int("NX2NAVI_PIPE_FRAME_WAIT_MS"); + per_frame = (uint64_t)(ms < 0 ? 0 : ms > 500 ? 500 : ms) * 1000000ull; + rt_log("vk_exec: the full-screen and one-shot draws of a frame wait up to %llu ms in all for their shaders and " + "pipelines (NX2NAVI_PIPE_FRAME_WAIT_MS), then are skipped while a worker builds them", + (unsigned long long)(per_frame / 1000000)); + } + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = per_frame; + return &left; +} + +// The milliseconds of pool not used up since t0, when the caller began to draw on it; UINT32_MAX without a pool. +static uint32_t pool_ms(const uint64_t *pool, uint64_t t0) { + if (!pool) return UINT32_MAX; + uint64_t spent = kern_now_ns() - t0; + return spent < *pool ? (uint32_t)((*pool - spent) / 1000000) : 0; +} + +static void pool_charge(uint64_t *pool, uint64_t t0) { + if (!pool) return; + uint64_t spent = kern_now_ns() - t0; + *pool = spent < *pool ? *pool - spent : 0; +} + +// What a one-shot draw may wait for its pipeline from pool. +static uint32_t oneshot_ms(const uint64_t *pool, int mesh) { + uint32_t ms = (uint32_t)(*pool / 1000000); + return mesh && ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; +} + +// ---- wait accounting: what the replay thread spent on draws that found their pipeline or a shader missing +// Counted from the miss to the draw's bind or skip, the look-ahead scan included, by the kind of wait the draw was +// allowed; left: the draws skipped at the end of it. draw_stats_tick logs the sums with the skipped draws, so a +// session's log says how much time the pipelines cost on this thread and which rule spent it. +enum { WK_LINK, WK_FULL, WK_LAST, WK_ONESHOT, WK_ONESHOT_MESH, WK_SHADER, WK_N }; +static const char *const wk_names[WK_N] = {"a fast link", "full-screen", "full-screen with no picture to hold", + "one-shot", "one-shot mesh", "one-shot, for a shader"}; +static struct { uint64_t n[WK_N], ns[WK_N], max_ns[WK_N], left[WK_N], frame_ns, frame_max_ns, frame; } WS; + +static void wait_note(int kind, uint64_t t0, int left) { + uint64_t ns = kern_now_ns() - t0, f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + WS.n[kind]++, WS.ns[kind] += ns, WS.left[kind] += left != 0; + if (ns > WS.max_ns[kind]) WS.max_ns[kind] = ns; + if (f != WS.frame) WS.frame = f, WS.frame_ns = 0; + WS.frame_ns += ns; + if (WS.frame_ns > WS.frame_max_ns) WS.frame_max_ns = WS.frame_ns; +} + static uint32_t link_draw_ms(void) { static int ms = -1; if (ms < 0) { @@ -3350,9 +3403,12 @@ static uint32_t link_draw_ms(void) { return (uint32_t)ms; } #define LINK_DRAW_MS link_draw_ms() -// How long a full-screen draw waits on for its pipeline once its frame budget is spent and the previous picture -// cannot be held any longer (prepare_draw_ex): the longest optimised build of such a pipeline measured was 194 ms. -#define FULLSCREEN_LAST_MS 500u +// How long a full-screen draw waits on for its pipeline once the frame's wait pool is spent and the previous picture +// cannot be held any longer (prepare_draw_ex). Every such pipeline has a fast link (vk_pipeline.c gpl_feedback), so +// the wait is for stage libraries: a few milliseconds from the driver's cache, and in the one case a player's session +// shows the draw had its pipeline 18 ms into this wait. It was 500 ms while a pipeline with a feedback loop waited +// for its whole optimised build; with the pool before it, a frame now waits under 100 ms for pipelines in all. +#define FULLSCREEN_LAST_MS 40u static uint64_t *link_budget(void) { static uint64_t frame = ~0ull, left, per_frame = ~0ull; if (per_frame == ~0ull) { @@ -3438,6 +3494,25 @@ static void draw_stats_tick(void) { uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); if (!DRS.frame) DRS.frame = f; if (f - DRS.frame < DRAW_STATS_FRAMES) return; + uint64_t waits = 0, wait_ns = 0; + for (int k = 0; k < WK_N; k++) waits += WS.n[k], wait_ns += WS.ns[k]; + static uint64_t steady_logged; + if (waits || vk_exec_steady_passes != steady_logged) { + char line[900]; + int o = snprintf(line, sizeof line, "vk_exec: over %llu frames %llu draws found a shader or pipeline missing and " + "held the replay thread %.1f ms in all, %.1f ms in the worst frame; by the wait they were allowed:", + (unsigned long long)(f - DRS.frame), (unsigned long long)waits, wait_ns / 1e6, WS.frame_max_ns / 1e6); + for (int k = 0; k < WK_N && o < (int)sizeof line - 160; k++) + if (WS.n[k]) + o += snprintf(line + o, sizeof line - o, " %s %llu (%.1f ms, longest %.1f ms, %llu skipped at its end);", + wk_names[k], (unsigned long long)WS.n[k], WS.ns[k] / 1e6, WS.max_ns[k] / 1e6, + (unsigned long long)WS.left[k]); + snprintf(line + o, sizeof line - o, " %llu lookups found only steady targets and skipped a draw that would have " + "waited as one-shot", (unsigned long long)(vk_exec_steady_passes - steady_logged)); + rt_log("%s", line); + steady_logged = vk_exec_steady_passes; + memset(&WS, 0, sizeof WS); + } for (int m = 0; m < 2; m++) { uint64_t any = 0; for (int k = 0; k < SK_COUNT; k++) any += DRS.skipped[m][k]; @@ -3582,9 +3657,17 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (!(oneshot = !vk_exec_targets_recent(ex, skip >= 1))) // skipped until ready return perf_count(PERF_CNT_SHADER_SKIP), draw_skipped(ex, mesh, SK_SHADER_PENDING); perf_count(PERF_CNT_ONESHOT_WAIT); - if (vs_wait) vs = shader_for(ex, vst, &has_vs, NULL, 1); - if (ts_wait) ts = shader_for(ex, NVN_STAGE_TASK, &has_ts, NULL, 1); - if (fs_wait) fs = shader_for(ex, NVN_STAGE_FS, &has_fs, NULL, 1); + // from the frame's wait pool (NX2NAVI_ONESHOT_BUDGET=0: until each is translated); a shader the pool does not + // reach stays at the front of the translators' queue, and the draw is skipped like a per-frame one + uint64_t *pool = oneshot_budget_on() ? frame_wait() : NULL, x0 = kern_now_ns(); + uint32_t ms; + if (vs_wait && (ms = pool_ms(pool, x0))) vs = shader_for(ex, vst, &has_vs, &vs_wait, ms); + if (ts_wait && (ms = pool_ms(pool, x0))) ts = shader_for(ex, NVN_STAGE_TASK, &has_ts, &ts_wait, ms); + if (fs_wait && (ms = pool_ms(pool, x0))) fs = shader_for(ex, NVN_STAGE_FS, &has_fs, &fs_wait, ms); + pool_charge(pool, x0); + wait_note(WK_SHADER, x0, vs_wait || ts_wait || fs_wait); + if (vs_wait || ts_wait || fs_wait) + return perf_count(PERF_CNT_SHADER_SKIP), draw_skipped(ex, mesh, SK_SHADER_PENDING); } if (!vs || (has_fs && !fs) || (has_ts && !ts)) return draw_skipped(ex, mesh, SK_SHADER_FAILED); if (shader_skipped(vs) || shader_skipped(fs)) return draw_skipped(ex, mesh, SK_POOL_OFF); // NX2NAVI_SKIP_SHADERS @@ -3601,29 +3684,25 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int const VkPipeInfo *info = NULL; int pending; // A draw of at most 6 vertices over half the render area or more (a full-screen triangle or quad, a tile) waits for - // its pipeline: skipping it can leave a black tile. The waits share 50 ms per presented frame - // (NX2NAVI_PIPE_SKIP=fullscreen skips them instead). A one-shot draw waits without that budget, up to - // ONESHOT_WAIT_MS, or from its own per-frame budget with NX2NAVI_ONESHOT_BUDGET=1 (oneshot_budget). Everything else - // is skipped while its pipeline is built. - static uint64_t wait_frame; - static uint64_t wait_left_ns; + // its pipeline: skipping it can leave a black tile. Those waits and the one-shot draws' come from the frame's wait + // pool (frame_wait; NX2NAVI_PIPE_SKIP=fullscreen skips the full-screen draws instead, and with + // NX2NAVI_ONESHOT_BUDGET=0 a one-shot draw waits up to ONESHOT_WAIT_MS outside the pool). Everything else waits + // for its fast link from the small budget of link_budget and is skipped while its pipeline is built. uint32_t wait_ms = 0; uint64_t t0 = 0; uint64_t *budget = NULL; // the per-frame budget a timed wait is charged to int linking = 0; // the wait is the short one for a fast link (link_budget) - int large = (uint64_t)ex->pass_w * ex->pass_h >= ONESHOT_LARGE_PX; + int fullscreen = 0; // the wait is a full-screen draw's if (oneshot > 0 && oneshot_budget_on()) { - budget = oneshot_budget(large); - wait_ms = (uint32_t)(*budget / 1000000); + budget = frame_wait(); + wait_ms = oneshot_ms(budget, mesh); if (wait_ms) t0 = kern_now_ns(); } else if (oneshot > 0) { wait_ms = ONESHOT_WAIT_MS; } else if (skip < 2 && count <= 6 && (uint64_t)sc.extent.width * sc.extent.height * 2 >= (uint64_t)ex->pass_w * ex->pass_h) { - uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); - if (f != wait_frame) wait_frame = f, wait_left_ns = 50000000; - wait_ms = (uint32_t)(wait_left_ns / 1000000); + budget = frame_wait(), fullscreen = 1; + wait_ms = (uint32_t)(*budget / 1000000); if (wait_ms) t0 = kern_now_ns(); - budget = &wait_left_ns; } else { budget = link_budget(); wait_ms = (uint32_t)(*budget / 1000000); @@ -3636,7 +3715,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int // from the info the cache entry keeps. VkPipeline p; const char *why = NULL; - uint64_t w0 = 0; + uint64_t w0 = 0, m0 = 0; // m0: when the lookup missed (wait_note) + int kind = WK_LINK; if (pipe_memo_on() && PM.valid && !memcmp(&PM.s, &s, sizeof s)) { info = PM.info, pending = 0; p = __atomic_load_n(&info->pipeline, __ATOMIC_ACQUIRE); @@ -3646,6 +3726,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int // of the later draws' pipelines build on the workers from here on), inside this draw's wait budget. p = vk_pipeline_graphics_async(&s, &info, &pending, 0); if (pending) { + m0 = kern_now_ns(); + kind = oneshot > 0 ? (mesh ? WK_ONESHOT_MESH : WK_ONESHOT) : fullscreen ? WK_FULL : WK_LINK; if (linking) t0 = kern_now_ns(); else w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; vk_exec_ahead(ex); @@ -3659,25 +3741,26 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (pending && oneshot < 0 && !vk_exec_targets_recent(ex, skip >= 1)) { perf_count(PERF_CNT_ONESHOT_WAIT); why = "one-shot"; + kind = mesh ? WK_ONESHOT_MESH : WK_ONESHOT; if (!w0) w0 = kern_now_ns(); if (oneshot_budget_on()) { - budget = oneshot_budget(large); - uint64_t b0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, (uint32_t)(*budget / 1000000)); - uint64_t spent = kern_now_ns() - b0; - *budget = spent < *budget ? *budget - spent : 0; + uint64_t *pool = frame_wait(), b0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh)); + pool_charge(pool, b0); } else { p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); } } // A full-screen draw still pending after its budget: skipped, it leaves the frame black or unlit. While the // previous picture can stay on screen in its place (vk_frame_hold_left) that is what happens; once that time - // is used up the draw waits on, FULLSCREEN_LAST_MS at most, so that no such frame is shown. - if (pending && budget == &wait_left_ns && !vk_frame_hold_left()) { + // is used up the draw waits on, FULLSCREEN_LAST_MS at most, before such a frame is shown. + if (pending && fullscreen && !vk_frame_hold_left()) { why = "full-screen, nothing left to hold the picture"; + kind = WK_LAST; if (!w0) w0 = kern_now_ns(); p = vk_pipeline_graphics_async(&s, &info, &pending, FULLSCREEN_LAST_MS); } + if (m0) wait_note(kind, m0, pending), draw_stats_tick(); PM.valid = !pending && p && info; if (PM.valid) PM.s = s, PM.info = info; } diff --git a/runtime/vk_target_frames.h b/runtime/vk_target_frames.h new file mode 100644 index 00000000..40aa8249 --- /dev/null +++ b/runtime/vk_target_frames.h @@ -0,0 +1,106 @@ +// Target history: the presented frame (gfx_counters.frames + 1; 0: never) each render target subresource was last +// rendered into, the one before, and in how many frames. vk_exec_draw.c prepare_draw_ex lets a draw into a target the +// game does not render frame after frame (a one-shot draw: a volume or lookup table generated once) wait for its +// shaders and pipeline instead of skipping it. Own header so tests/target_frames_check.c checks the rules without +// Vulkan; vk_exec.c is the one runtime file that includes it (the table is static). +// Per subresource, not per image: a volume may be generated a few slices per frame. +// +// Recent means within TFR_RECENT frames, not the frame before alone: the game's output images are a ring of three, its +// probe and shadow layers are refreshed in turn every second to fourth frame, and the mips of its small effect buffers +// alternate between images. Sets of TFR_WAYS, the oldest entry replaced: hundreds of subresources are rendered a +// frame, and in a direct-mapped table of 1024 a fifth of the scene passes and most of the probe layers read as never +// rendered because another subresource had taken their slot. Each misread made a draw whose pipeline was still +// building wait for it, so a new area was entered one pipeline at a time. +// +// Steady targets (NX2NAVI_PIPE_SKIP_STEADY, default 1; 0 = as before). Recent alone called every pass one-shot in the +// first frame after the game had drawn something else for a while (a loading screen between two areas, a full-screen +// menu): the scene's own targets, the probe layers and the half-resolution buffers had last been rendered hundreds of +// frames before, so each draw with a new pipeline waited on the replay thread, and a mesh draw, whose pipeline has no +// fast link, for its whole compile. One player's session shows it three times at area changes: frames of 954, 1015 +// and 684 ms, six mesh draws waiting 52 to 660 ms each in passes whose targets had been rendered in thousands of +// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as it is the same +// guest texture (its initialization serial: an image taken over by another texture starts again at none). From +// TFR_STEADY frames on the target is one the game draws again and again, and its draws are skipped while a pipeline +// builds, as in any other frame, however long ago the last one was. A target generated once never gets there; one +// generated again at every area load would after TFR_STEADY loads, by when its pipelines are recorded and prebuilt +// at the start. +#pragma once + +#include + +#define TFR_SETS 1024 +#define TFR_WAYS 8 +#define TFR_RECENT 4 +#define TFR_STEADY 8 + +// One render target of a pass: the image handle as a number, the subresource, the guest texture's serial (0 unknown). +typedef struct { uint64_t image; uint32_t level, layer; uint64_t serial; } TfrTarget; +typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, serial; } TargetFrames; +static TargetFrames tfr[TFR_SETS * TFR_WAYS]; + +// The target's entry, or with make the entry it takes (the set's oldest, an empty one first); NULL without make +// when it has none. +static inline TargetFrames *tfr_find(const TfrTarget *t, int make) { + uint64_t h = (t->image ^ (uint64_t)t->level << 56 ^ (uint64_t)t->layer << 40) * 0x9E3779B97F4A7C15ull; + TargetFrames *set = &tfr[(h >> 54) * TFR_WAYS], *oldest = set; + for (int i = 0; i < TFR_WAYS; i++) { + if (set[i].image == t->image && set[i].level == t->level && set[i].layer == t->layer) return &set[i]; + if (set[i].last < oldest->last) oldest = &set[i]; + } + if (!make) return NULL; + *oldest = (TargetFrames){.image = t->image, .level = t->level, .layer = t->layer}; + return oldest; +} + +// The target is rendered in frame stamp. +static inline void tfr_note(const TfrTarget *t, uint64_t stamp) { + TargetFrames *e = tfr_find(t, 1); + if (e->serial != t->serial) e->serial = t->serial, e->n = 0; // another texture has the image now: its own count + if (e->last != stamp) e->prev = e->last, e->last = stamp, e->n += e->n < UINT32_MAX; +} + +// The frame before stamp that last rendered the target, 0 for none. +static inline uint64_t tfr_before(const TfrTarget *t, uint64_t stamp) { + const TargetFrames *e = tfr_find(t, 0); + return !e ? 0 : e->last == stamp ? e->prev : e->last; +} + +static inline int tfr_recent(const TfrTarget *t, uint64_t stamp) { + uint64_t before = tfr_before(t, stamp); + return before && stamp - before <= TFR_RECENT; +} + +// No frame before stamp rendered the target. +static inline int tfr_first(const TfrTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } + +// TFR_STEADY frames or more before stamp rendered the target as the texture it is now. +static inline int tfr_steady(const TfrTarget *t, uint64_t stamp) { + const TargetFrames *e = tfr_find(t, 0); + return e && e->serial == t->serial && e->n - (e->last == stamp) >= TFR_STEADY; +} + +// Whether the pass with the n_color colour targets c and the depth target d (NULL: none) is one the game renders +// frame after frame, so that a draw into it may be skipped in frame stamp and comes back in the next. +// any 0: every target is recent (and the pass has one). any 1: a colour target is, or the depth target is while no +// colour target is rendered for the first time: the game's item icon capture draws into a new colour target with the +// per-frame depth buffer, once, and its draws skipped there stay missing from the icon until the item is captured +// again. steady 1: a steady target counts as a recent one; *by_steady is set when only that made the answer 1. +static inline int tfr_pass_recent(const TfrTarget *c, int n_color, const TfrTarget *d, uint64_t stamp, int any, + int steady, int *by_steady) { + int n = 0, color[2] = {0, 0}, depth[2] = {0, 0}, color_first = 0, all[2] = {1, 1}; + for (int i = 0; i <= n_color; i++) { + const TfrTarget *t = i < n_color ? &c[i] : d; + if (!t || !t->image) continue; + n++; + int recent = tfr_recent(t, stamp), kept = recent || (steady && tfr_steady(t, stamp)); + if (recent) *(i < n_color ? &color[0] : &depth[0]) = 1; + else all[0] = 0; + if (kept) *(i < n_color ? &color[1] : &depth[1]) = 1; + else all[1] = 0; + if (i < n_color && tfr_first(t, stamp)) color_first = 1; // one never rendered is neither recent nor steady + } + int r[2]; // without the steady rule, and with it + for (int k = 0; k < 2; k++) r[k] = any ? color[k] || (depth[k] && !color_first) : n > 0 && all[k]; + if (by_steady) *by_steady = r[1] && !r[0]; + return r[1]; +} diff --git a/tests/target_frames_check.c b/tests/target_frames_check.c new file mode 100644 index 00000000..0dd5b81e --- /dev/null +++ b/tests/target_frames_check.c @@ -0,0 +1,119 @@ +// runtime/vk_target_frames.h: which passes count as rendered frame after frame, so that a draw into them is skipped +// while its pipeline builds instead of holding the replay thread. Built and run by tests/target_frames_check.py; +// prints each failed check and exits 1 on any. +// The frame numbers of the first block are those of a player's log: an area change after 238 frames of loading +// screen, where every pass read as one-shot and six mesh draws waited 52 to 660 ms each. +#include +#include + +#include "vk_target_frames.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t serial) { + return (TfrTarget){image, level, layer, serial}; +} + +// The verdict with the steady rule and without it; *by: only the steady rule made it per-frame. +static int pass(const TfrTarget *c, int n, const TfrTarget *d, uint64_t stamp, int any, int steady, int *by) { + int b = 0, r = tfr_pass_recent(c, n, d, stamp, any, steady, &b); + if (by) *by = b; + return r; +} + +int main(void) { + int by; + // ---- the area change of the log + TfrTarget scene = T(0x1000, 0, 0, 11), scene_d = T(0x1001, 0, 0, 12); // drawn every frame + TfrTarget probe = T(0x2000, 0, 0, 21), probe_d = T(0x2001, 0, 0, 22); // every second frame + TfrTarget probe_l3 = T(0x2000, 0, 3, 21); // a layer drawn every 15th frame + TfrTarget half = T(0x3000, 0, 0, 31), half_d = T(0x3001, 0, 0, 32); // the half-resolution buffers + for (uint64_t f = 16000; f <= 17925; f++) { + tfr_note(&scene, f), tfr_note(&scene_d, f), tfr_note(&half, f), tfr_note(&half_d, f); + if (f % 2) tfr_note(&probe, f), tfr_note(&probe_d, f); + if (f % 15 == 0) tfr_note(&probe_l3, f); + } + // the frame after: per-frame by being recent, with or without the rule + CHECK(pass(&scene, 1, &scene_d, 17926, 1, 0, NULL) == 1, "a pass drawn the frame before is per-frame"); + CHECK(pass(&scene, 1, &scene_d, 17926, 0, 0, NULL) == 1, "strict: every target recent"); + CHECK(pass(&scene, 1, &scene_d, 17926, 1, 1, &by) == 1 && !by, "recent passes are not counted as steady ones"); + // 238 frames of loading screen later (its own targets are other images) + const uint64_t now = 18163; + TfrTarget load = T(0x9000, 0, 0, 91); + for (uint64_t f = 17926; f < now; f++) tfr_note(&load, f); + CHECK(pass(&scene, 1, &scene_d, now, 1, 0, NULL) == 0, "without the rule the scene pass reads as one-shot after the pause"); + CHECK(pass(&scene, 1, &scene_d, now, 1, 1, &by) == 1 && by, "the scene pass is per-frame after the pause"); + CHECK(pass(&scene, 1, &scene_d, now, 0, 1, &by) == 1 && by, "strict: the scene pass too"); + CHECK(pass(&probe, 1, &probe_d, now, 1, 1, NULL) == 1, "the probe pass (128x128, seen 17925/17923) is per-frame"); + CHECK(pass(&probe_l3, 1, &probe_d, now, 1, 0, NULL) == 0, "a layer drawn every 15th frame is never recent"); + CHECK(pass(&probe_l3, 1, &probe_d, now, 1, 1, NULL) == 1, "a layer drawn every 15th frame, in 128 frames, is steady"); + // the 960x540 pass of the log: one colour target made new, one steady, the depth target drawn earlier this frame + TfrTarget fresh = T(0x4000, 0, 0, 41), cc[2] = {fresh, half}; + tfr_note(&fresh, now), tfr_note(&half, now), tfr_note(&half_d, now); // the pass begins: its targets are noted + CHECK(pass(cc, 2, &half_d, now, 1, 0, NULL) == 0, "without the rule: no recent colour target, a new one beside"); + CHECK(pass(cc, 2, &half_d, now, 1, 1, &by) == 1 && by, "a steady colour target beside a new one: per-frame"); + CHECK(pass(cc, 2, &half_d, now, 0, 1, NULL) == 0, "strict: the new colour target keeps the pass one-shot"); + // the next frame all of it is recent again + tfr_note(&scene, now), tfr_note(&scene_d, now); + CHECK(pass(&scene, 1, &scene_d, now + 1, 1, 0, NULL) == 1, "recent again in the next frame"); + CHECK(pass(&fresh, 1, &half_d, now + 1, 1, 0, NULL) == 1, "the new target is recent in its second frame"); + + // ---- what stays one-shot + TfrTarget lut = T(0x5000, 0, 0, 51); + CHECK(pass(&lut, 1, NULL, now, 1, 1, NULL) == 0, "a target never rendered is one-shot"); + tfr_note(&lut, now); + CHECK(pass(&lut, 1, NULL, now, 1, 1, NULL) == 0, "its first frame, already noted by its pass, is still one-shot"); + tfr_note(&lut, now); // a second pass into it in the same frame + for (int load_no = 1; load_no < TFR_STEADY; load_no++) { // generated again at each area load, far apart + uint64_t f = now + (uint64_t)load_no * 5000; + CHECK(pass(&lut, 1, NULL, f, 1, 1, NULL) == 0, "a table generated at %d area loads is one-shot at the next", load_no); + tfr_note(&lut, f); + } + CHECK(pass(&lut, 1, NULL, now + (uint64_t)TFR_STEADY * 5000, 1, 1, NULL) == 1, + "after %d loads the table counts as steady: its pipelines are recorded by then", TFR_STEADY); + // a volume generated a few slices per frame: each slice is its own target and is rendered once + for (uint32_t z = 0; z < 64; z++) { + TfrTarget slice = T(0x6000, 0, z, 61); + CHECK(pass(&slice, 1, NULL, now + 10 + z / 4, 1, 1, NULL) == 0, "volume slice %u is one-shot", z); + tfr_note(&slice, now + 10 + z / 4); + } + // the item icon capture: a new colour target with the per-frame depth buffer, once + TfrTarget icon = T(0x7000, 0, 0, 71); + uint64_t f2 = now + 1; + tfr_note(&scene_d, f2), tfr_note(&icon, f2); + CHECK(pass(&icon, 1, &scene_d, f2, 1, 1, NULL) == 0, "an icon's first capture is one-shot though its depth buffer is recent"); + CHECK(pass(&icon, 1, &scene_d, f2, 1, 0, NULL) == 0, "and was before the rule"); + // an image taken over by another texture starts again: the per-frame history of its handle is not the new texture's + TfrTarget reused = T(scene.image, 0, 0, 500); + CHECK(tfr_steady(&scene, f2 + 300) == 1, "the scene target is steady"); + CHECK(pass(&reused, 1, NULL, f2 + 300, 1, 1, NULL) == 0, "the same image as another texture is not steady"); + tfr_note(&reused, f2 + 300); + CHECK(tfr_steady(&scene, f2 + 301) == 0, "and the texture that had it lost its count with the image"); + CHECK(tfr_find(&reused, 0)->n == 1, "the new texture's count begins at one"); + // a pass with no target at all + CHECK(pass(NULL, 0, NULL, now, 1, 1, NULL) == 0 && pass(NULL, 0, NULL, now, 0, 1, NULL) == 0, "no targets: not per-frame"); + + // ---- the table keeps the steady targets through an area's first frame + // 400 per-frame targets, a pause, then 600 targets made new are rendered before the old ones come round again + memset(tfr, 0, sizeof tfr); + for (uint64_t f = 1; f <= 100; f++) + for (uint32_t i = 0; i < 400; i++) { + TfrTarget t = T(0x7f3a00000000ull + (uint64_t)(i / 4) * 0x2c0, 0, i % 4, 1000 + i / 4); + tfr_note(&t, f); + } + for (uint32_t i = 0; i < 600; i++) { + TfrTarget t = T(0x7f3b00000000ull + (uint64_t)i * 0x2c0, 0, 0, 5000 + i); + tfr_note(&t, 400); + } + int kept = 0; + for (uint32_t i = 0; i < 400; i++) { + TfrTarget t = T(0x7f3a00000000ull + (uint64_t)(i / 4) * 0x2c0, 0, i % 4, 1000 + i / 4); + kept += tfr_steady(&t, 400); + } + CHECK(kept == 400, "%d of 400 steady targets still known after 600 new ones in one frame", kept); + + if (fails) return printf("%d checks failed\n", fails), 1; + printf("target_frames_check: ok\n"); + return 0; +} diff --git a/tests/target_frames_check.py b/tests/target_frames_check.py new file mode 100644 index 00000000..ec96037a --- /dev/null +++ b/tests/target_frames_check.py @@ -0,0 +1,40 @@ +"""Build and run tests/target_frames_check.c: runtime/vk_target_frames.h, which render passes count as drawn frame +after frame, so that a draw into them is skipped while its pipeline builds instead of waited for. + +No GPU, no game files, any Python. + +Usage: target_frames_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'target_frames' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('target_frames_check.exe' if os.name == 'nt' else 'target_frames_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'target_frames_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main()) From 9d257edf5ab4ebcab776799e70b3854fde125f80 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:02:24 -0500 Subject: [PATCH 004/153] The log says which missed pipelines were slow and why, and a slow frame that waited for a pipeline is charged to the pipeline. A session's log gave the counts of slow pipelines and slow frames but not what they were. Now: - Each pipeline a draw missed that took 50 ms or longer to become usable gets a line (the first 48 of a run): mesh or vertex, fast-linked or built whole, whether pipeline-keys.bin named it, its shaders' input ids and where their translations came from (the shader cache, a previous translator's, or translated in this session). The summary line counts the same: how many were mesh pipelines, how many the key file named and the prebuild had not reached, how many had a shader translated in this session. - Each shader library that took 25 ms or longer gets a line (the first 32): its shader, the translation's origin, and whether outputs were added for its fragment shader, which is a module the preparation never built. - The count of draw lookups that "found shader libraries missing" says what it counts: a library not made in this process yet, not one the driver's cache lacks. - After the boot prewarm one line says how many of the recorded pipelines' shaders are a previous translator's translations, beside the pipeline cache hits that show whether any pipeline was compiled again. - The frame-time summary's "over 25 ms by cause" took the replay's whole time as one part, so every frame that waited for a pipeline, a translation or a texture upload inside the replay counted as "replay". The replay's part is now its time without those waits. gfx_shader_origin gives a translation's origin. Co-Authored-By: Claude Fable 5.2 --- runtime/gfx.h | 3 ++ runtime/gfx_shader.c | 4 ++ runtime/perf.c | 7 +++- runtime/vk_pipeline.c | 96 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 91 insertions(+), 19 deletions(-) diff --git a/runtime/gfx.h b/runtime/gfx.h index 168090e5..bcff7188 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -62,6 +62,9 @@ void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgro int bda); const uint8_t *gfx_shader_key(const GfxShader *s); // the 32-byte cache key (stable while the translator is) const uint8_t *gfx_shader_input_id(const GfxShader *s); // the 32-byte input id (stable across translators) +// Where the translation in use came from: 0 the shader cache, under the current translator; 1 the shader cache, a +// previous translator's (gfx_shader.c "Carry"); 2 translated in this process, so nothing was prepared from it. +int gfx_shader_origin(const GfxShader *s); // By input id from memory or disk; xlate 1 also translates it from its stored inputs. NULL if absent. const GfxShader *gfx_shader_load_input(const uint8_t iid[32], int xlate); // Boot prewarm: out[i] = gfx_shader_load_input(iids[i], 1) on `threads` threads with up to `procs` translators, then diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 51feb53f..be532ff2 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -129,6 +129,7 @@ struct GfxShader { // (4 bits per target, the SPH omap); 0 unknown (older translations) uint8_t carried; // 1 + the previous identity (C.prev) that translated it; 0 the // current translator + uint8_t fresh; // translated in this process (translate_one) }; static PlatMutex cache_lock = PLAT_MUTEX_INIT; // memory cache @@ -1973,6 +1974,7 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co } in_store(s->iid, stage, control, code, r, a); plat_inc64(&n_xlated); + s->fresh = 1; return mem_insert(s); } @@ -2244,6 +2246,8 @@ void gfx_shader_counts(uint64_t *keys, uint64_t *translated) { *keys = (uint64_t const uint8_t *gfx_shader_key(const GfxShader *s) { return s->key; } +int gfx_shader_origin(const GfxShader *s) { return s->fresh ? 2 : s->carried ? 1 : 0; } + const uint8_t *gfx_shader_input_id(const GfxShader *s) { return s->iid; } // By input id: memory, then the disk cache under the current translator, then (xlate 1) a translation from the stored diff --git a/runtime/perf.c b/runtime/perf.c index aab9a331..f801ca97 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -133,6 +133,9 @@ typedef struct { // What a frame over 25 ms spent its time on, so a count of slow frames says where to look instead of only that they // happened. A frame is charged to the largest part when that part is at least a quarter of it, and to "unattributed" // otherwise - the part of a slow frame none of the recorded waits explains (the GPU, or a stall outside them). +// The replay's part is its time without the pipeline, translation and texture waits, which all happen inside it: with +// them in, every frame that waited 900 ms for a pipeline was charged to "replay", the larger number, and a log +// could not tell a pipeline stall from a slow replay. enum { FT_C_PIPE, FT_C_XLATE, FT_C_TEX, FT_C_FENCE, FT_C_PACE, FT_C_REPLAY, FT_C_OTHER, FT_C_N }; static const char *const ft_cause_name[FT_C_N] = { "pipeline", "translation", "texture", "fence", "pacing", "replay", "unattributed" }; @@ -380,7 +383,9 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) { part[FT_C_TEX] = frame_ns[PERF_TEX]; part[FT_C_FENCE] = frame_ns[PERF_FENCE] + frame_ns[PERF_DRAIN]; part[FT_C_PACE] = frame_ns[PERF_PACE]; - part[FT_C_REPLAY] = (uint64_t)rep > 0 ? (uint64_t)rep : 0; + part[FT_C_REPLAY] = rep > 0 ? (uint64_t)rep : 0; + uint64_t inside = part[FT_C_PIPE] + part[FT_C_XLATE] + part[FT_C_TEX]; // waits on the replay thread + part[FT_C_REPLAY] = part[FT_C_REPLAY] > inside ? part[FT_C_REPLAY] - inside : 0; uint64_t known = 0; for (int c = 0; c < FT_C_OTHER; c++) known += part[c]; part[FT_C_OTHER] = d > known ? d - known : 0; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 2524c5b5..d4051baf 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -76,7 +76,7 @@ static struct { int indep; // stage-independent layouts (vk_caps.gpl): vertex set 0, fragment set 1 int push_stage; // GFX_STAGE_* whose set is pushed with independent layouts int gpl; // draws whose pipeline is building get a fast-linked one (vk_caps.gpl) - struct { uint64_t links, fb_links, wlinks, flinks, dlinks, pr_added, lib_slow[4], ready_n[6], ready_nofast, hints, hint_libs, requeued, link_fail, defer, lib_n[4], lib_fail, swaps, slow, pre_used[4]; double pr_added_ms, lib_slow_ms[4], ready_ms, ready_max, link_ms, link_max, lib_ms[4], lib_max[4], dlink_ms, dlink_max; } g; + struct { uint64_t links, fb_links, wlinks, flinks, dlinks, pr_added, lib_slow[4], ready_n[6], ready_nofast, ready_mesh, ready_recorded, ready_fresh, ready_logged, lib_logged, hints, hint_libs, requeued, link_fail, defer, lib_n[4], lib_fail, swaps, slow, pre_used[4]; double pr_added_ms, lib_slow_ms[4], ready_ms, ready_max, link_ms, link_max, lib_ms[4], lib_max[4], dlink_ms, dlink_max; } g; int vs_outs_all; // NX2NAVI_VS_OUTS: 0 union of paired fragment inputs, 1 (all) all 32 output locations int dyn_vi, dyn_clamp, dyn_poly, dyn_blend_en, dyn_blend_eq, dyn_wmask, dyn_a2c; int f_logic_op, f_depth_clamp, f_fill_non_solid, f_dual_src, f_full_subgroups; @@ -1905,6 +1905,12 @@ static VkShaderStageFlags stage_bit(int stage) { static int is_mesh(const VkPipeState *s) { return s->vs && gfx_shader_stage(s->vs) == GFX_STAGE_MS; } +// Where a shader's translation came from (gfx_shader_origin), for the log. +static const char *origin_name(const GfxShader *s) { + static const char *const name[3] = {"from the shader cache", "a previous translator's", "translated in this session"}; + return s ? name[gfx_shader_origin(s)] : "none"; +} + // The shaders of the pipeline this thread builds, for the crash notes (platform.h plat_crash_context): the first 8 // bytes of each cache key, as gfx_shader.c names its cache files. static void crash_note(const char *what, const GfxShader *a, const GfxShader *b, const GfxShader *c) { @@ -2820,8 +2826,21 @@ static VkPipeline lib_build(Lib *b, int kind, const VkPipeState *s, const GKey * plat_mutex_lock(&P.lock); P.g.lib_n[kind]++, P.g.lib_ms[kind] += ms; if (ms > P.g.lib_max[kind]) P.g.lib_max[kind] = ms; - if (kind == LIB_PR && ((const PrKey *)(const void *)b->key)->missing) P.g.pr_added++, P.g.pr_added_ms += ms; + int added = kind == LIB_PR && ((const PrKey *)(const void *)b->key)->missing; + if (added) P.g.pr_added++, P.g.pr_added_ms += ms; if (ms >= 25) P.g.lib_slow[kind]++, P.g.lib_slow_ms[kind] += ms; + // A shader library that took this long was not in the driver's cache; why not, for the first 32 of a run + if (ms >= 25 && (kind == LIB_PR || kind == LIB_FS) && P.g.lib_logged < 32) { + const GfxShader *sh = kind == LIB_PR ? s->vs : s->fs; + const uint8_t *id = sh ? gfx_shader_input_id(sh) : NULL; + P.g.lib_logged++; + rt_log("vk_pipeline: gpl: a %s library took %.0f ms: shader %02x%02x%02x%02x, %s%s", + kind == LIB_PR ? "pre-rasterization" : "fragment shader", ms, id ? id[0] : 0, id ? id[1] : 0, + id ? id[2] : 0, id ? id[3] : 0, origin_name(sh), + added ? "; outputs were added for its fragment shader, a module the preparation did not build" + : sh && gfx_shader_origin(sh) == 2 ? "; nothing was prepared from a shader first translated now" + : "; the driver's cache did not have it, or the build waited behind others in the driver"); + } } if (!pipe && ++P.g.lib_fail <= 8) rt_log("vk_pipeline: gpl: stage library %d not built", kind); b->pipe = pipe, b->building = 0; @@ -2909,18 +2928,6 @@ static void lib_keys(const VkPipeState *s, const GKey *k, ViKey *v, PrKey *pk, F memcpy(ok->color, k->color, sizeof ok->color), memcpy(ok->blend, k->blend, sizeof ok->blend); } -// p, which a draw missed, has its first usable pipeline: how long that took is how long its draws were skipped or -// waited. fast 0: it had no fast link and this is the optimised build. Caller holds P.lock. -static void ready_note(Pipe *p, int fast) { - if (!p->t_miss) return; - double ms = (double)(plat_now_ns() - p->t_miss) / 1e6; - p->t_miss = 0; - P.g.ready_n[ms < 4 ? 0 : ms < 8 ? 1 : ms < 17 ? 2 : ms < 50 ? 3 : ms < 150 ? 4 : 5]++; - P.g.ready_ms += ms; - if (ms > P.g.ready_max) P.g.ready_max = ms; - P.g.ready_nofast += !fast; -} - // NX2NAVI_GPL_FEEDBACK (default 1; 0 = as before): a pipeline whose fragment shader samples an attachment of its own pass // (VkPipeState.feedback: soft particles and water reading the depth buffer, decals, full-screen passes that read the // target they write) gets a fast link like any other. The feedback loop flags belong to the fragment output state, so @@ -3205,6 +3212,44 @@ static void keys_open(void) { static double now_ms(void) { return (double)plat_now_ns() / 1e6; } // Caller holds P.lock. +// p, which a draw missed, has its first usable pipeline: how long that took is how long its draws were skipped or +// waited. fast 0: it had no fast link and this is the optimised build. Caller holds P.lock. +// The summary (save_with) counts what a slow one can be: a mesh pipeline (no stage libraries, so its whole build), one +// the key file names whose prebuild had not come to it, one with a shader translated in this session (nothing of it +// was prepared, so its libraries compile cold). The first READY_LOGS that took READY_SLOW_MS or longer get a line +// each with those facts, so a log says which pipelines left their draws out of the picture and for which reason. +#define READY_SLOW_MS 50.0 +#define READY_LOGS 48 +static void ready_note(Pipe *p, int fast) { + if (!p->t_miss) return; + double ms = (double)(plat_now_ns() - p->t_miss) / 1e6; + p->t_miss = 0; + P.g.ready_n[ms < 4 ? 0 : ms < 8 ? 1 : ms < 17 ? 2 : ms < 50 ? 3 : ms < 150 ? 4 : 5]++; + P.g.ready_ms += ms; + if (ms > P.g.ready_max) P.g.ready_max = ms; + P.g.ready_nofast += !fast; + const Job *j = p->job; + if (!j) return; + int mesh = is_mesh(&j->s); + int fresh = gfx_shader_origin(j->s.vs) == 2 || (j->s.fs && gfx_shader_origin(j->s.fs) == 2) || + (j->s.ts && gfx_shader_origin(j->s.ts) == 2); + P.g.ready_mesh += (uint64_t)mesh, P.g.ready_recorded += j->prebuild, P.g.ready_fresh += (uint64_t)fresh; + if (ms < READY_SLOW_MS || P.g.ready_logged >= READY_LOGS) return; + P.g.ready_logged++; + const uint8_t *vi = gfx_shader_input_id(j->s.vs), *fi = j->s.fs ? gfx_shader_input_id(j->s.fs) : NULL; + char fid[16] = "none"; + if (fi) snprintf(fid, sizeof fid, "%02x%02x%02x%02x", fi[0], fi[1], fi[2], fi[3]); + rt_log("vk_pipeline: a pipeline a draw missed was usable after %.0f ms: a %s pipeline, %s; %s; %s shader " + "%02x%02x%02x%02x (%s), fragment shader %s (%s)%s%s", + ms, mesh ? "mesh" : "vertex", + fast ? "fast-linked, so the time went to its stage libraries (the lines \"a ... library took\" name the slow ones)" + : mesh ? "which has no stage libraries: this is its whole build" + : "which got no fast link: this is its whole build", + j->prebuild ? "pipeline-keys.bin names it and its prebuild had not come to it" : "not in pipeline-keys.bin before", + mesh ? "mesh" : "vertex", vi[0], vi[1], vi[2], vi[3], origin_name(j->s.vs), fid, origin_name(j->s.fs), + j->k.fbl ? "; feedback loop" : "", P.g.ready_logged == READY_LOGS ? " (the last such line of this run)" : ""); +} + static void prebuild_report(void) { rt_log("vk_pipeline: prebuild done: %u built, %u failed, %u asked for by a draw first; %.0f ms after the device was " "ready", K.built, K.failed, K.promoted, now_ms() - K.t0); @@ -3884,6 +3929,9 @@ static void prewarm_run(void) { __atomic_store_n(&pw_phase, 1, __ATOMIC_RELEASE); gfx_shader_prewarm((const uint8_t(*)[32])ids, u, sh, cpus, procs < 1 ? 1 : procs); double t1 = now_ms(); + size_t carried = 0, fresh = 0; // the recorded pipelines' shaders by where their translation came from + for (size_t i = 0; i < u; i++) + if (sh[i]) carried += gfx_shader_origin(sh[i]) == 1, fresh += gfx_shader_origin(sh[i]) == 2; vk_mem_stage_log("the shader prewarm"); if (P.indep) { // vertex output unions first: workers may take a job before the queue is complete @@ -3974,6 +4022,13 @@ static void prewarm_run(void) { free(K.file); K.file = NULL; plat_mutex_unlock(&P.lock); + // After an update with another translator: a record names its shaders by input id, which no translator changes, + // so every record still finds its shaders, and the pipeline cache gives the pipeline back wherever the translation + // in use is the one it was built from. The line below this one says how many it gave back. + if (carried || fresh) + rt_log("vk_pipeline: prewarm: of the %zu shaders the recorded pipelines name, %zu are a previous translator's " + "translations and %zu were translated now; a recorded pipeline is compiled again only where its shaders' " + "translation changed, which the pipeline-cache.bin hits below show", u, carried, fresh); fb_log("after the boot prewarm"); vk_mem_stage_log("the recorded pipelines' prebuild"); vk_pipeline_prewarm_marker_clear(); // the boot's share is done; what the pool builds later runs during play @@ -5895,8 +5950,9 @@ static void save_with(uint32_t wait_ms) { if (P.ready && P.gpl) { rt_log("vk_pipeline: gpl: %llu fast links (%llu by workers, %llu of those by the library workers for skipped draws; " "%llu of pipelines with a feedback loop; avg %.2f ms, " - "max %.2f ms, %llu failed), %llu draws " - "lookups found shader libraries missing (left to a worker), %llu optimised pipelines swapped in (%llu queued " + "max %.2f ms, %llu failed), %llu lookups by a draw found a stage library not made in this process yet and " + "left it to a worker (whether the driver's cache has it shows in the library times below), %llu optimised " + "pipelines swapped in (%llu queued " "low after their fast link), %llu drawing-thread links over 5 ms; %llu waiting draws built their libraries " "(avg %.2f ms, max %.2f ms)", (unsigned long long)P.g.links, (unsigned long long)P.g.wlinks, (unsigned long long)P.g.flinks, @@ -5907,10 +5963,14 @@ static void save_with(uint32_t wait_ms) { (unsigned long long)P.g.dlinks, P.g.dlinks ? P.g.dlink_ms / P.g.dlinks : 0.0, P.g.dlink_max); uint64_t rn = P.g.ready_n[0] + P.g.ready_n[1] + P.g.ready_n[2] + P.g.ready_n[3] + P.g.ready_n[4] + P.g.ready_n[5]; rt_log("vk_pipeline: gpl: pipelines a draw missed were usable after under 4 ms %llu, 4-8 ms %llu, 8-17 ms %llu, " - "17-50 ms %llu, 50-150 ms %llu, longer %llu (avg %.1f ms, longest %.0f ms); %llu of them had no fast link", + "17-50 ms %llu, 50-150 ms %llu, longer %llu (avg %.1f ms, longest %.0f ms); %llu of them had no fast link; " + "%llu were mesh pipelines (no stage libraries), %llu were named in pipeline-keys.bin and not prebuilt yet " + "(the others were new to this install), %llu had a shader translated in this session", (unsigned long long)P.g.ready_n[0], (unsigned long long)P.g.ready_n[1], (unsigned long long)P.g.ready_n[2], (unsigned long long)P.g.ready_n[3], (unsigned long long)P.g.ready_n[4], (unsigned long long)P.g.ready_n[5], - rn ? P.g.ready_ms / (double)rn : 0.0, P.g.ready_max, (unsigned long long)P.g.ready_nofast); + rn ? P.g.ready_ms / (double)rn : 0.0, P.g.ready_max, (unsigned long long)P.g.ready_nofast, + (unsigned long long)P.g.ready_mesh, (unsigned long long)P.g.ready_recorded, + (unsigned long long)P.g.ready_fresh); if (P.g.hints) rt_log("vk_pipeline: gpl: looking ahead named %llu pipeline states not known yet and queued %llu stage libraries " "for them", (unsigned long long)P.g.hints, (unsigned long long)P.g.hint_libs); From e9ec6de6bc70f8ea8c43070be2feade7d1f5bfa4 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:14:29 -0500 Subject: [PATCH 005/153] A render target's frame count follows its storage, not the guest's texture object, and a mesh draw no longer waits for a pipeline that is known to be compiling. Found in the first run of the previous two commits (a first visit of Kokiri Forest from a state that had only seen Hyrule Field): - The count of frames a target was rendered in started again whenever the guest's texture object changed, and the game initializes its render target objects anew every frame at the same storage: the log showed `seen 2129/2128 in 1 frames`, so no target ever became steady and the rule did nothing. The owner of an image is now the storage address the texture cache keys it by. An image handed on to a texture elsewhere still starts at none. - A one-shot mesh draw waited its 8 ms in every frame that met the same pipeline while it compiled for 150 to 1100 ms: 17 waits, 170 ms, every draw skipped at the end, up to four in one frame. A mesh pipeline that has been building for longer than that wait is a compile, and the draws after the first do not wait for it (vk_pipeline_building_ms). - The last wait of a full-screen draw with no picture to hold was 40 ms a draw, and two such draws made 86 ms in one frame. It is 40 ms a frame now. - A one-shot vertex draw skipped at the end of its wait gets its log line however short the wait was (seven were skipped with no line, so their targets could not be named). That run against the same cell of the earlier comparison (same exe path, cache state, route and settings; one run each): the load's worst wait of the draws in one frame 99 ms against 735 ms, no mesh wait over 13 ms against 524 and 206 ms. Two long frames remain in both runs (628 and 862 ms here, 660 and 854 ms there): two compute pipelines built at their first dispatch, about 395 and 397 ms. Co-Authored-By: Claude Fable 5.2 --- runtime/vk.h | 3 +++ runtime/vk_exec.c | 4 ++-- runtime/vk_exec_draw.c | 46 +++++++++++++++++++++++++------------ runtime/vk_pipeline.c | 12 ++++++++++ runtime/vk_target_frames.h | 26 +++++++++++---------- tests/target_frames_check.c | 7 +++--- 6 files changed, 66 insertions(+), 32 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 8a1face5..423ed31a 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -714,6 +714,9 @@ VkPipeline vk_pipeline_graphics(const VkPipeState *s, const VkPipeInfo **info); // wait_ms > 0 waits up to that long for a pending pipeline first. Pipelines built in a run are recorded to // pipeline-keys.bin next to the pipeline cache and prebuilt at the next boot (NX2NAVI_PIPE_PREBUILD=0: not). VkPipeline vk_pipeline_graphics_async(const VkPipeState *s, const VkPipeInfo **info, int *pending, uint32_t wait_ms); +// How long the pipeline of s has been building since a draw first missed it, in milliseconds; 0 when it is unknown, +// usable, or no draw has missed it. +double vk_pipeline_building_ms(const VkPipeState *s); // The stage libraries of a draw the replay has not reached yet, queued to the workers when no pipeline is known for its // state (vk_pipeline.c "stage libraries ahead of their draws"). Returns the libraries queued. int vk_pipeline_hint(const VkPipeState *s); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 2d7e94e4..1d8866d0 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -482,7 +482,7 @@ static void free_views(void *arg) { // completion thread, once the submission uint64_t vk_exec_steady_passes; // lookups a steady target alone made per-frame (vk_exec_targets_recent) static TfrTarget tfr_of(const VkExecTarget *t) { - return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->tex ? t->tex->serial : 0}; + return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->img ? t->img->addr : 0}; } static int steady_on(void) { @@ -522,7 +522,7 @@ void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap) { int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu in %u frames", n ? "; " : "", i < 8 ? 'c' : 'd', i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0), - e && e->serial == ft.serial ? e->n : 0); + e && e->owner == ft.owner ? e->n : 0); if (w < 0) break; n += (size_t)w; } diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 683ec91a..c412607b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -68,7 +68,10 @@ static uint32_t oneshot_wait_ms(void) { } #define ONESHOT_WAIT_MS oneshot_wait_ms() // A mesh pipeline has no stage libraries and so no fast link: the wait is for its whole build, a few milliseconds -// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which. +// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which, and one +// that has been building for longer than this is a compile: the draws that meet it in the frames after wait for +// nothing (oneshot_ms). Before that rule a first visit waited 8 to 13 ms for each of 17 mesh draws over nine frames, +// up to four in one frame, and every one of them was skipped at the end. #define MESH_WAIT_MS 8u static int oneshot_budget_on(void) { static int on = -1; @@ -3370,10 +3373,12 @@ static void pool_charge(uint64_t *pool, uint64_t t0) { *pool = spent < *pool ? *pool - spent : 0; } -// What a one-shot draw may wait for its pipeline from pool. -static uint32_t oneshot_ms(const uint64_t *pool, int mesh) { +// What a one-shot draw with the state s may wait for its pipeline from pool. +static uint32_t oneshot_ms(const uint64_t *pool, int mesh, const VkPipeState *s) { uint32_t ms = (uint32_t)(*pool / 1000000); - return mesh && ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; + if (!mesh || !ms) return ms; + if (vk_pipeline_building_ms(s) > MESH_WAIT_MS) return 0; // a compile (MESH_WAIT_MS) + return ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; } // ---- wait accounting: what the replay thread spent on draws that found their pipeline or a shader missing @@ -3403,12 +3408,19 @@ static uint32_t link_draw_ms(void) { return (uint32_t)ms; } #define LINK_DRAW_MS link_draw_ms() -// How long a full-screen draw waits on for its pipeline once the frame's wait pool is spent and the previous picture -// cannot be held any longer (prepare_draw_ex). Every such pipeline has a fast link (vk_pipeline.c gpl_feedback), so -// the wait is for stage libraries: a few milliseconds from the driver's cache, and in the one case a player's session -// shows the draw had its pipeline 18 ms into this wait. It was 500 ms while a pipeline with a feedback loop waited -// for its whole optimised build; with the pool before it, a frame now waits under 100 ms for pipelines in all. +// How long the full-screen draws of a frame wait on for their pipelines, in all, once the frame's wait pool is spent +// and the previous picture cannot be held any longer (prepare_draw_ex, last_wait). Every such pipeline has a fast link +// (vk_pipeline.c gpl_feedback), so the wait is for stage libraries: a few milliseconds from the driver's cache, and +// in the one case a player's session shows the draw had its pipeline 18 ms into this wait. It was 500 ms for each +// draw while a pipeline with a feedback loop waited for its whole optimised build; with the pool before it, a frame +// now waits under 100 ms for pipelines in all. #define FULLSCREEN_LAST_MS 40u +static uint64_t *last_wait(void) { + static uint64_t frame = ~0ull, left; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = FULLSCREEN_LAST_MS * 1000000ull; + return &left; +} static uint64_t *link_budget(void) { static uint64_t frame = ~0ull, left, per_frame = ~0ull; if (per_frame == ~0ull) { @@ -3695,7 +3707,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int int fullscreen = 0; // the wait is a full-screen draw's if (oneshot > 0 && oneshot_budget_on()) { budget = frame_wait(); - wait_ms = oneshot_ms(budget, mesh); + wait_ms = oneshot_ms(budget, mesh, &s); if (wait_ms) t0 = kern_now_ns(); } else if (oneshot > 0) { wait_ms = ONESHOT_WAIT_MS; @@ -3745,7 +3757,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (!w0) w0 = kern_now_ns(); if (oneshot_budget_on()) { uint64_t *pool = frame_wait(), b0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh)); + p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh, &s)); pool_charge(pool, b0); } else { p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); @@ -3753,12 +3765,15 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } // A full-screen draw still pending after its budget: skipped, it leaves the frame black or unlit. While the // previous picture can stay on screen in its place (vk_frame_hold_left) that is what happens; once that time - // is used up the draw waits on, FULLSCREEN_LAST_MS at most, before such a frame is shown. - if (pending && fullscreen && !vk_frame_hold_left()) { + // is used up the draw waits on, from the FULLSCREEN_LAST_MS of its frame, before such a frame is shown. + uint64_t *last = pending && fullscreen && !vk_frame_hold_left() ? last_wait() : NULL; + if (last && *last >= 1000000) { why = "full-screen, nothing left to hold the picture"; kind = WK_LAST; if (!w0) w0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, FULLSCREEN_LAST_MS); + uint64_t b0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, (uint32_t)(*last / 1000000)); + pool_charge(last, b0); } if (m0) wait_note(kind, m0, pending), draw_stats_tick(); PM.valid = !pending && p && info; @@ -3766,7 +3781,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } uint64_t waited = w0 ? kern_now_ns() - w0 : 0; static int wait_logs; - if (why && waited >= 2000000 && wait_logs < 200) { + // also a one-shot draw skipped at the end of its wait, however short: its target may not be drawn again + if (why && (waited >= 2000000 || (pending && kind == WK_ONESHOT)) && wait_logs < 200) { char tg[512]; wait_logs++; vk_exec_targets_describe(ex, tg, sizeof tg); diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index d4051baf..327b128a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3819,6 +3819,18 @@ VkPipeline vk_pipeline_graphics_async(const VkPipeState *s, const VkPipeInfo **i return graphics(s, info, A.on, pending, wait_ms); } +double vk_pipeline_building_ms(const VkPipeState *s) { + if (!s->vs || !P.ready) return 0; + GKey k; + gkey_fill(s, &k); + uint64_t h = key_hash(&k, sizeof k); + plat_mutex_lock(&P.lock); + const Pipe *p = pipe_find(&k, sizeof k, h); + double ms = p && p->building && !p->info.pipeline && p->t_miss ? (double)(plat_now_ns() - p->t_miss) / 1e6 : 0; + plat_mutex_unlock(&P.lock); + return ms; +} + // ---- boot prewarm from the key file (comment at "pipeline keys") static int id_cmp(const void *x, const void *y) { return memcmp(x, y, 32); } diff --git a/runtime/vk_target_frames.h b/runtime/vk_target_frames.h index 40aa8249..d2fece2f 100644 --- a/runtime/vk_target_frames.h +++ b/runtime/vk_target_frames.h @@ -18,12 +18,14 @@ // frames before, so each draw with a new pipeline waited on the replay thread, and a mesh draw, whose pipeline has no // fast link, for its whole compile. One player's session shows it three times at area changes: frames of 954, 1015 // and 684 ms, six mesh draws waiting 52 to 660 ms each in passes whose targets had been rendered in thousands of -// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as it is the same -// guest texture (its initialization serial: an image taken over by another texture starts again at none). From -// TFR_STEADY frames on the target is one the game draws again and again, and its draws are skipped while a pipeline -// builds, as in any other frame, however long ago the last one was. A target generated once never gets there; one -// generated again at every area load would after TFR_STEADY loads, by when its pipelines are recorded and prebuilt -// at the start. +// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as the image is the +// texture at the same storage (its owner, the storage GPU address the texture cache keys the image by: an image +// handed on to a texture elsewhere starts again at none). The guest's texture object does not serve as the owner: +// the game initializes its render target objects anew every frame at the same storage, so a count kept per object +// never passed one. From TFR_STEADY frames on the target is one the game draws again and again, and its draws are +// skipped while a pipeline builds, as in any other frame, however long ago the last one was. A target generated once +// never gets there; one generated again at every area load would after TFR_STEADY loads, by when its pipelines are +// recorded and prebuilt at the start. #pragma once #include @@ -33,9 +35,9 @@ #define TFR_RECENT 4 #define TFR_STEADY 8 -// One render target of a pass: the image handle as a number, the subresource, the guest texture's serial (0 unknown). -typedef struct { uint64_t image; uint32_t level, layer; uint64_t serial; } TfrTarget; -typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, serial; } TargetFrames; +// One render target of a pass: the image handle as a number, the subresource, the image's owner (0 unknown). +typedef struct { uint64_t image; uint32_t level, layer; uint64_t owner; } TfrTarget; +typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, owner; } TargetFrames; static TargetFrames tfr[TFR_SETS * TFR_WAYS]; // The target's entry, or with make the entry it takes (the set's oldest, an empty one first); NULL without make @@ -55,7 +57,7 @@ static inline TargetFrames *tfr_find(const TfrTarget *t, int make) { // The target is rendered in frame stamp. static inline void tfr_note(const TfrTarget *t, uint64_t stamp) { TargetFrames *e = tfr_find(t, 1); - if (e->serial != t->serial) e->serial = t->serial, e->n = 0; // another texture has the image now: its own count + if (e->owner != t->owner) e->owner = t->owner, e->n = 0; // another texture has the image now: its own count if (e->last != stamp) e->prev = e->last, e->last = stamp, e->n += e->n < UINT32_MAX; } @@ -73,10 +75,10 @@ static inline int tfr_recent(const TfrTarget *t, uint64_t stamp) { // No frame before stamp rendered the target. static inline int tfr_first(const TfrTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } -// TFR_STEADY frames or more before stamp rendered the target as the texture it is now. +// TFR_STEADY frames or more before stamp rendered the target under the owner it has now. static inline int tfr_steady(const TfrTarget *t, uint64_t stamp) { const TargetFrames *e = tfr_find(t, 0); - return e && e->serial == t->serial && e->n - (e->last == stamp) >= TFR_STEADY; + return e && e->owner == t->owner && e->n - (e->last == stamp) >= TFR_STEADY; } // Whether the pass with the n_color colour targets c and the depth target d (NULL: none) is one the game renders diff --git a/tests/target_frames_check.c b/tests/target_frames_check.c index 0dd5b81e..f20ccc6b 100644 --- a/tests/target_frames_check.c +++ b/tests/target_frames_check.c @@ -11,8 +11,8 @@ static int fails; #define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) -static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t serial) { - return (TfrTarget){image, level, layer, serial}; +static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t owner) { + return (TfrTarget){image, level, layer, owner}; } // The verdict with the steady rule and without it; *by: only the steady rule made it per-frame. @@ -84,7 +84,8 @@ int main(void) { tfr_note(&scene_d, f2), tfr_note(&icon, f2); CHECK(pass(&icon, 1, &scene_d, f2, 1, 1, NULL) == 0, "an icon's first capture is one-shot though its depth buffer is recent"); CHECK(pass(&icon, 1, &scene_d, f2, 1, 0, NULL) == 0, "and was before the rule"); - // an image taken over by another texture starts again: the per-frame history of its handle is not the new texture's + // an image handed on to a texture at other storage starts again: the per-frame history of its handle is not the + // new texture's TfrTarget reused = T(scene.image, 0, 0, 500); CHECK(tfr_steady(&scene, f2 + 300) == 1, "the scene target is steady"); CHECK(pass(&reused, 1, NULL, f2 + 300, 1, 1, NULL) == 0, "the same image as another texture is not steady"); From 0a9bbed34a7069a3aea5c6148b2fbf798eb708e2 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:26:31 -0500 Subject: [PATCH 006/153] Say in the crash report when the crash is in another program's module, and report video memory when the device is lost. A crash inside a recorder's Vulkan layer read like a fault of the game: the report now says that the module is not part of the game and what to do. A lost device now logs the video memory in use against the driver's budget, so a report from a card with little memory shows whether the memory ran out. Co-Authored-By: Claude Fable 5.2 --- runtime/main.c | 15 +++++++++++++++ runtime/vk.h | 1 + runtime/vk_device.c | 1 + runtime/vk_memory.c | 12 ++++++++++++ 4 files changed, 29 insertions(+) diff --git a/runtime/main.c b/runtime/main.c index f452a8e5..0462cedd 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -35,6 +35,7 @@ // Also here: the runtime services the generated code calls besides rt_dispatch and rt_svc (rt_brk, rt_udf, // rt_ticks, the function profiler hook), logging, SHA-256, and the crash reporter (a vectored exception handler that // names the lifted function, the guest registers and the guest frame-pointer chain). +#include #include #include #include @@ -256,6 +257,17 @@ static uint64_t lifted_function(uintptr_t rip) { static int readable(uint64_t a, uint64_t n) { return plat_mem_readable(a, n); } +// A module another program put into the game's process: the Vulkan layer or the hook of a recorder, an overlay or a +// capture tool, by the words such files carry in their names. A crash inside one is that program's: one report was an +// access violation in a recorder's layer the moment the present mode changed and the swapchain was made again. +static int foreign_hook(const char *module) { + char low[160]; + size_t n = 0; + for (; module && module[n] && n < sizeof low - 1; n++) low[n] = (char)tolower((unsigned char)module[n]); + low[n] = 0; + return strstr(low, "vk_layer") || strstr(low, "hook") || strstr(low, "overlay"); +} + static void crash_handler(const PlatCrash *x) { static volatile int32_t busy; if (plat_xchg32(&busy, 1)) plat_exit(71); @@ -269,6 +281,9 @@ static void crash_handler(const PlatCrash *x) { rt_logf(", %s address %#llx", x->access == 1 ? "writing" : x->access == 8 ? "executing" : "reading", (unsigned long long)x->addr); rt_log_raw("\n", 1); + if (foreign_hook(x->module)) + rt_logf(" %s is not part of the game: a recording, overlay or capture program put it into the game. Close that " + "program or turn off its Vulkan capture, then start the game again\n", x->module); uint64_t f = x->module ? 0 : lifted_function(rip); if (f) rt_logf(" in lifted function f_%llx = %s\n", (unsigned long long)f, ldr_describe(f, d, sizeof d)); // abort and invalid parameter: where the host code called it; a fault in another module: the host calls that led diff --git a/runtime/vk.h b/runtime/vk.h index 8a1face5..8b3ca0db 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -353,6 +353,7 @@ void vk_mem_class_log(const VkPhysicalDeviceProperties *p, const VkPhysicalDevic int vk_mem_class_of(uint64_t addr); // VK_MCLS_* of the pool at addr (NONE when unbacked) void vk_mem_census(uint64_t frame); // at each present: pool census and traffic, logged at frame 1800 and, // with NX2NAVI_TRACE=perf, every 1800 frames +void vk_mem_lost_report(void); // a line with the video memory in use and its budget (device lost) float vk_mem_device_used_mb(void); // device-local memory this process holds (the driver's budget // figure), MB; -1 without VK_EXT_memory_budget. Any thread. void vk_mem_stage_log(const char *stage); // one line after a boot stage: device memory, process commit diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 9167b87f..c72af9a8 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -588,6 +588,7 @@ void vk_device_fault_report(VkResult r) { done = 1; vk_crumbs_report(); vk_pass_marks_report(); + vk_mem_lost_report(); VkCtx *c = vk_ctx(); PFN_vkGetDeviceFaultInfoEXT get = c && c->has_device_fault ? (PFN_vkGetDeviceFaultInfoEXT)vk_dpa(c->device, "vkGetDeviceFaultInfoEXT") diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 265f0f80..f2ee73a1 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -3247,6 +3247,18 @@ static void outside_watch(uint64_t frame) { // The overlay's "game" figure beside the card's memory in use (perfmon.c, its sampling thread): what the driver // counts for this process on the device-local heaps, the same figure vram_use_log splits by use. +// With the device lost report (vk_device.c): the video memory in use and the budget the driver gives the game. A lost +// device with the memory at or over its budget points at the memory; one far below it does not, and the report of a +// card with 8 GB that ended this way held neither number. +void vk_mem_lost_report(void) { + uint64_t used, budget; + int exact; + if (!vram_query(&used, &budget, &exact)) return; + rt_log("vk: video memory when the device was lost: %llu MiB in use of a budget of %llu MiB (%s)", + (unsigned long long)(used >> 20), (unsigned long long)(budget >> 20), + exact ? "the driver's figures, every program counted in the budget" : "this game's own allocations only"); +} + float vk_mem_device_used_mb(void) { VkCtx *c = vk_ctx(); if (!c || !c->has_memory_budget) return -1; From fd22aab8d4726beb20d18ded5b51be160618f8f5 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:31:22 -0500 Subject: [PATCH 007/153] Let the launcher replace a cached font file it has registered. A font file registered with Windows stays open, so writing it failed with Permission denied. When the cached title font was registered but its family could not be used, every start tried to decode it again, failed on the locked file and kept the system font. The registration is now given up before the fonts are decoded again, and an unchanged file is not rewritten. Co-Authored-By: Claude Fable 5.2 --- launcher/gamefiles.py | 4 +++- launcher/ui/app.py | 5 +++++ launcher/winsys.py | 11 +++++++++++ tests/launcher_test.py | 8 ++++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/launcher/gamefiles.py b/launcher/gamefiles.py index bf3998a0..f37568aa 100644 --- a/launcher/gamefiles.py +++ b/launcher/gamefiles.py @@ -326,7 +326,9 @@ def extract_fonts(layout, game_dir='', dump=''): fam = font_family(otf) if otf else None if not otf or not fam: continue - (d / f'{role}.otf').write_bytes(otf) + p = d / f'{role}.otf' + if not (p.is_file() and p.read_bytes() == otf): # a file this process registered as a font stays as it is + p.write_bytes(otf) (d / f'{role}.family').write_text(fam, encoding='utf-8') out[role] = (d / f'{role}.otf', fam) return out diff --git a/launcher/ui/app.py b/launcher/ui/app.py index 6b73ce8e..130effa1 100644 --- a/launcher/ui/app.py +++ b/launcher/ui/app.py @@ -315,6 +315,11 @@ class App: got = heard = {} try: if fonts: + # The cached files are registered (load_fonts) and were not usable, or this would not run: + # registered, Windows keeps them open, and decoding them again failed with "Permission denied" + # at every start, so a cache file that had gone bad was never replaced. + for path, _family in gamefiles.cached_fonts(self.layout).values(): + winsys.unregister_font(path) got = gamefiles.extract_fonts(self.layout, game_dir, dump) except Exception as e: self.ctx.log(f'fonts: {e}') diff --git a/launcher/winsys.py b/launcher/winsys.py index 04789f62..e0cd91e5 100644 --- a/launcher/winsys.py +++ b/launcher/winsys.py @@ -141,6 +141,17 @@ def register_font(path): return False +def unregister_font(path): + '''Give up a font file register_font added, so the file can be written again: Windows keeps a registered font + file open, and writing it then fails with "Permission denied".''' + if os.name != 'nt': + return + try: + ctypes.windll.gdi32.RemoveFontResourceExW(ctypes.c_wchar_p(str(path)), 0x10, None) # FR_PRIVATE + except (AttributeError, OSError): + pass + + def open_path(path): path = str(path) if os.name == 'nt': diff --git a/tests/launcher_test.py b/tests/launcher_test.py index a839f02a..92837ed8 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1019,6 +1019,14 @@ class FontTest(Tmp): self.assertEqual(got['title'][1], 'DejaVu Sans') self.assertEqual(Path(got['title'][0]).read_bytes(), otf) self.assertEqual(gamefiles.cached_fonts(self.layout)['title'][1], 'DejaVu Sans') + # Registered as a font, Windows keeps the file open: decoding it again must not fail on the unchanged file, + # and once the registration is given up a file that went bad is replaced. + path = Path(got['title'][0]) + winsys.register_font(path) + self.assertEqual(gamefiles.extract_fonts(self.layout, self.tmp / 'game')['title'][1], 'DejaVu Sans') + winsys.unregister_font(path) + path.write_bytes(b'not a font') + self.assertEqual(Path(gamefiles.extract_fonts(self.layout, self.tmp / 'game')['title'][0]).read_bytes(), otf) # ---------------------------------------------------------------- the button art of a release From a6a3944ba0321b845089ca80b162d2b050726bb6 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:40:10 -0500 Subject: [PATCH 008/153] Report a lost device where it is first seen, at the read of the submissions' timeline. The result of that read was not checked. After a device loss the value was taken as it came, the callbacks of submissions that never ran were called as if their work were done, and the game went on recording until the next checked queue call failed. That was an upload's, so two players' reports read as an upload fault. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_sync.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 85dea2f7..9e7ee07a 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -401,7 +401,15 @@ static void completion_main(void *arg) { vk_device_fault_report(r); rt_fatal("vk: vkWaitSemaphores failed (%d; device lost?)", (int)r); } - vkGetSemaphoreCounterValue(c->device, c->timeline, &reached); + // A lost device is reported here, where it is first seen. Unchecked, the value was taken as it came: the + // callbacks of submissions that never ran were called as if their work were done (one report's first sign + // was an occlusion query "not available after its fence"), and the game went on recording until the next + // checked queue call failed, an upload's, which then read as the cause. + r = vkGetSemaphoreCounterValue(c->device, c->timeline, &reached); + if (r != VK_SUCCESS) { + vk_device_fault_report(r); + rt_fatal("vk: the submissions' timeline could not be read (%d; device lost?)", (int)r); + } plat_mutex_lock(&st_lock); if (reached > issued) reached = issued; } From 5988871aba5915ab8d43bc9b71a56ecbe0b4875b Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:40:11 -0500 Subject: [PATCH 009/153] Pick texture detail one step lower where ASTC textures stay uncompressed on a card under 20 GB. The automatic levels assume the ASTC textures end as BC3. Decoded on the CPU or with the BC3 pass off they stay RGBA8 and take four times the memory, which is what one mip level left out gives back. A card with 8 GB that decoded on the CPU went 560 MiB over its budget and lost its device. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 +++-- runtime/vk_texture.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 2ab095cc..a60402ea 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -346,8 +346,9 @@ # more, with smaller levels) without that level, low without the two largest when they are 1024x1024 or more. Surfaces # seen up close look softer; interface art, small textures, shadow maps and everything the game draws itself keep their # size. auto picks by the video memory Windows grants the game: full from 8192 MiB (cards of 10 GB and more), low under -# 6144 MiB (cards of 6 GB and less), reduced between (8 GB cards). The log says what was picked ("texture detail:"). -# Read at start. +# 6144 MiB (cards of 6 GB and less), reduced between (8 GB cards). Where the ASTC textures stay uncompressed (astc = +# cpu or astc_bc = 0) and the budget is under 20480 MiB, auto goes one step lower, as they take four times the memory. +# The log says what was picked ("texture detail:"). Read at start. # texture_detail = auto # Sun shadow map size: game (the game's own, which changes with the view and drops to about 400 near the ground, so # leaf shadows look blocky and jump) or a fixed 256, 512, 768, 1024, 1536 or 2048. Above 960 the texture heap grows with diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index e9fc6390..8a7dfbfd 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -739,6 +739,11 @@ static void astc_probe_alpha(const tex_t *t, const TexVkFormat *f) { #define DETAIL_LOW_MIN 1024 #define DETAIL_FULL_MB 8192 #define DETAIL_LOW_MB 6144 +// The levels above assume the ASTC textures end as BC3 (or stay ASTC), a byte a texel. Where they stay RGBA8 (astc = +// cpu, a card without the decoder, astc_bc = 0) they take four times that, which is what one level left out gives +// back: under this budget auto then goes one step lower. A card with 8 GiB that decoded on the CPU held two texture +// arrays of 852 MiB each, was 560 MiB over its budget and lost its device. +#define DETAIL_RGBA8_MB 20480 #define EXACT_MAX 65536 static nvn_hmap exact = {.klen = sizeof(TexKey)}; // keys made whole whatever the setting; value 1 static uint64_t n_exact; // textures made whole again so far @@ -756,7 +761,9 @@ int vk_texture_detail_auto(void) { mb = vk_mem_vram(&used, &budget, &exact_budget) ? (int64_t)(budget >> 20) : 0; __atomic_store_n(&detail_budget_mb, mb, __ATOMIC_RELAXED); } - return !mb || mb >= DETAIL_FULL_MB ? 0 : mb >= DETAIL_LOW_MB ? 1 : 2; + int most = !mb || mb >= DETAIL_FULL_MB ? 0 : mb >= DETAIL_LOW_MB ? 1 : 2; + if (mb && mb < DETAIL_RGBA8_MB && most < 2 && vk_astc_mode() != VK_ASTC_NATIVE && !vk_astc_bc()) most++; + return most; } static int detail_most(void) { // the most levels a texture is made without: 0 full, 1 reduced, 2 low @@ -773,7 +780,9 @@ static int detail_most(void) { // the most levels a texture is made without: 0 rt_log("vk: texture detail: full (texture_detail = %s; the device reports no video memory budget)", v); else rt_log("vk: texture detail: %s, chosen for a video memory budget of %lld MiB (texture_detail = %s; full from " - "%d MiB, low under %d MiB)", names[most], (long long)detail_budget_mb, v, DETAIL_FULL_MB, DETAIL_LOW_MB); + "%d MiB, low under %d MiB%s)", names[most], (long long)detail_budget_mb, v, DETAIL_FULL_MB, DETAIL_LOW_MB, + vk_astc_mode() != VK_ASTC_NATIVE && !vk_astc_bc() && detail_budget_mb < DETAIL_RGBA8_MB + ? "; one step lower because ASTC textures stay uncompressed here" : ""); } return most; } From 61cd4784f8f2431d2c5c5f97a04ba356b6f42a9f Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:42:17 -0500 Subject: [PATCH 010/153] Build the Linux kit against pinned Vulkan headers and without #embed, and try the compiler before the long steps. A player's AppImage build of 0.7.1 stopped in its compile step after eight minutes of RomFS work: four files did not compile, because the kit builds with the system's compiler and headers and theirs were older than the runtime needs. vk_pipeline.c and vk_device.c name VK_KHR_pipeline_binary, VK_KHR_dynamic_rendering_local_read and VK_KHR_shader_maximal_reconvergence, which their Vulkan headers did not declare, and ui/menu.cpp takes its font in with #embed, which clang has from version 19. - Vulkan headers: tools/vulkan_pin.py holds the release the other builds use (1.4.363, the archive and hash tools/make_release_linux.py already pinned, which now reads them from there). The AppImage carries its C headers in toolchain/vulkan-headers and builds against them whatever the system has; a kit or a source checkout uses the system's when they are at least that release and fetches the pinned ones otherwise (build_game.py vulkan_include). Their folder goes first on the include path of the runtime's sources only, so the lifted code's objects keep their keys, and a runtime object compiled before against the system's headers is taken over instead of compiled again. AppRun no longer asks for the system's Vulkan headers. - #embed: tools/build_shaders.py also writes the font's bytes as runtime/ui/roboto_ttf.h (ignored by git, carried by a kit), and menu.cpp includes that where the compiler has no __has_embed. With #embed nothing changes. - Before its long steps a Linux build that will compile now checks every runtime source that needs no header of the install with the compile step's own command (clang -fsyntax-only) and links an empty program against the game's libraries (make_game.py preflight): an old compiler, an old header or a missing development package stops it in the first seconds with the compiler's own errors. A failed compile names every file that failed, not only the three whose errors it prints. Checked on Windows: tests/launcher_test.py passes with four new tests (117); the header of bytes and #embed give the same 162,588 bytes, and both branches of menu.cpp compile; the early check passes on the Windows file set with clang 23 and stops on files made to fail; the 34 local headers of release 363 are the upstream tag's files by git blob id. Not run: anything on Linux, the pinned archive's download, a compiler older than 23. Co-Authored-By: Claude Fable 5.2 --- .gitignore | 2 + THIRD_PARTY_NOTICES.md | 1 + docs/README-BuildKit.md | 16 ++++- docs/building.md | 7 ++ runtime/ui/menu.cpp | 9 ++- tests/launcher_test.py | 125 +++++++++++++++++++++++++++++++++ tools/build_game.py | 38 ++++++++++ tools/build_shaders.py | 32 +++++++-- tools/make_appimage.py | 18 +++-- tools/make_game.py | 135 ++++++++++++++++++++++++++++++------ tools/make_kit.py | 3 +- tools/make_release_linux.py | 8 +-- tools/vulkan_pin.py | 109 +++++++++++++++++++++++++++++ 13 files changed, 465 insertions(+), 38 deletions(-) create mode 100644 tools/vulkan_pin.py diff --git a/.gitignore b/.gitignore index 66d8ebec..464aaae3 100644 --- a/.gitignore +++ b/.gitignore @@ -39,6 +39,8 @@ __pycache__/ # tools/make_*.py assemblers when the game is built; tools/check_content.py refuses them in a commit) /runtime/*_spv.h *.spv +# The menu font's bytes as a header for compilers without #embed (tools/build_shaders.py EMBEDDED) +/runtime/ui/roboto_ttf.h # Build outputs *.exe diff --git a/THIRD_PARTY_NOTICES.md b/THIRD_PARTY_NOTICES.md index 26317abd..83a29525 100644 --- a/THIRD_PARTY_NOTICES.md +++ b/THIRD_PARTY_NOTICES.md @@ -123,6 +123,7 @@ folder's `licenses/` and in each component's own folder. | zlib 1.4 (MSYS2 `mingw-w64-ucrt-x86_64-zlib`), static library | https://zlib.net, Copyright (C) Jean-loup Gailly and Mark Adler | zlib | linked into `NX2navi-Setup.exe` (its unzip) | | mingw-w64 CRT startup code and winpthreads 14.0.0, GCC 16.2.0's `libgcc` (MSYS2 packages), static | https://www.mingw-w64.org, https://gcc.gnu.org | mingw-w64: ZPL-2.1 / MIT / public domain; libgcc: GPL-3.0 with the GCC Runtime Library Exception | linked into `NX2navi.exe` and `NX2navi-Setup.exe` | | NVIDIA DLSS 310.9.1 (`nvngx_dlss.dll`, NVIDIA's own build, unmodified; pinned with its size and sha256 in `tools/dlss_pin.py`) | https://github.com/NVIDIA/DLSS @ `v310.9.1` (`lib/Windows_x86_64/rel/nvngx_dlss.dll`), Copyright NVIDIA Corporation | NVIDIA RTX SDKs License (https://github.com/NVIDIA/DLSS/blob/v310.9.1/LICENSE.txt); licensed for systems with NVIDIA GPUs only | `app//bin/` with `nvngx_dlss-NOTICE.txt`, also in the build kit's `bin/`; `tools/make_game.py` copies both beside the built game, where NGX loads the library (`runtime/vk_dlaa.c`) | +| Vulkan headers 1.4.363 (`include/vulkan/*.h` and `include/vk_video/*.h`, unmodified; the release pinned with its sha256 in `tools/vulkan_pin.py`) | https://github.com/KhronosGroup/Vulkan-Headers @ `v1.4.363`, Copyright 2015-2026 The Khronos Group Inc. | Apache-2.0 OR MIT | the Linux build kit's `toolchain/vulkan-headers/` (the AppImage of `tools/make_appimage.py`) with the repository's `LICENSE.md`; `tools/make_game.py` compiles the runtime against them on the player's PC, ahead of the system's headers. Never in this repository: a build fetches them | | NVIDIA DLSS 310.9.1 for Linux (`libnvidia-ngx-dlss.so.310.9.1`, NVIDIA's own build, unmodified; pinned with its size and sha256 in `tools/dlss_pin.py`) | https://github.com/NVIDIA/DLSS @ `v310.9.1` (`lib/Linux_x86_64/rel/libnvidia-ngx-dlss.so.310.9.1`), Copyright NVIDIA Corporation | NVIDIA RTX SDKs License (https://github.com/NVIDIA/DLSS/blob/v310.9.1/LICENSE.txt), the one licence of the repository's Windows and Linux libraries; licensed for systems with NVIDIA GPUs only | the Linux build kit's `bin/` (the AppImage of `tools/make_appimage.py`, unless made with `--no-dlss`) with `libnvidia-ngx-dlss-NOTICE.txt`; `tools/make_game.py` copies both beside the built game, where NGX loads the library (`runtime/vk_dlaa.c`) | | NVIDIA Streamline 2.14.16 runtime (`sl.interposer.dll`, `sl.common.dll`, `sl.dlss_g.dll`, `sl.pcl.dll`, `sl.reflex.dll`, NVIDIA's own builds, unmodified) and NVIDIA DLSS Frame Generation 310.9.16 (`nvngx_dlssg.dll`, as the Streamline SDK ships it) | https://github.com/NVIDIA-RTX/Streamline, SDK release 2.14.16 (`bin/x64/`), Copyright NVIDIA Corporation | Streamline: MIT; `nvngx_dlssg.dll`: NVIDIA RTX SDKs License, licensed for systems with NVIDIA GPUs only | `app//bin/dlssg/` with both licence texts and `dlssg-NOTICE.txt` (each file's sha256), when the release is made with `tools/make_launcher.py --dlssg`, and the build kit's `bin/dlssg/` with `tools/make_kit.py --dlssg` (the SDK release pinned with its size and sha256 and each file's sha256 in `tools/streamline_pin.py`); `tools/make_game.py` copies them beside the built game, where `nx2navi_dlssg.dll` (this repository's `runtime/dlssg/`, built with the Streamline SDK's MIT headers) loads them (`runtime/vk_dlssg.c`) | | Button prompt art: the 128x128 PNGs of the Input Prompts Pack V7 (`XGamepad`, `P5Gamepad` and `Keyboard_Mouse` styles), unmodified | the pack as licensed to the repository owner; never in this repository | the pack's licence, which allows redistributing these pictures in NX2navi's releases (confirmed by the repository owner, 2026-11-09) | `app//prompt-art/` and the build kit's `prompt-art/` when made with `--prompt-art` (`tools/prompt_art.py`); `tools/pad_prompts.py` draws the game's Xbox, PlayStation, Steam Deck (from the Xbox and PlayStation pictures) and keyboard and mouse prompts from them on the player's PC | diff --git a/docs/README-BuildKit.md b/docs/README-BuildKit.md index ad573506..de62bab7 100644 --- a/docs/README-BuildKit.md +++ b/docs/README-BuildKit.md @@ -78,6 +78,17 @@ RADV). It takes clang, clang++ and lld from `PATH` and links against the system' (GPL-2.0): `pip install --user zbic`, or in a virtual environment with the packages above. Vulkan Memory Allocator is one header: the system's (AUR `vulkan-memory-allocator`), else the build fetches the pinned 3.4.0 into `toolchain/vma/` and checks its hash. + The Vulkan headers must be release 363 (1.4.363) or newer: the runtime names extensions that older headers do not + declare (`VK_KHR_pipeline_binary` among them), and distributions with long support package older ones. The + AppImage carries the pinned headers (`toolchain/vulkan-headers/`, `tools/vulkan_pin.py`) and builds against them + whatever the system has; a build from a kit or a source checkout uses the system's when they are new enough and + otherwise fetches the pinned ones there once and checks their hash. The loader's library still comes from the + system (`vulkan-icd-loader`, `libvulkan-dev`, `vulkan-loader-devel`). + The compiler is the system's too. The releases are built with clang 23; an older one works as far as it compiles + the runtime (one without `#embed`, before clang 19, takes the menu's font from a header the kit carries). Before + its long steps the build checks every runtime source with that compiler and links against the libraries, so a + compiler, a header or a library it cannot use stops it in the first seconds, with the compiler's own words, and + not after the RomFS image. 2. The shader translator: a kit with `bin/nx2navi_xlate` has it; from a source checkout, build it once with `python tools/build_xlate.py` (needs the .NET 10 SDK). The build copies it beside the game. A kit also carries the @@ -132,8 +143,9 @@ switches at once, with a *DLSS multiplier* of 2x to 6x (above 2x on RTX 50 cards frames needed to reach the display's refresh rate (`framegen = dlss` and `fg_mult` in `nx2navi.ini`). The log says `ngxfg: DLSS Frame Generation available` when NGX found the library. -`tools/make_appimage.py` packs this kit into one `NX2navi-Linux--x86_64.AppImage` with its own Python, for -players without a source checkout: it checks for the packages above, installs zbic with pip on its first run, picks +`tools/make_appimage.py` packs this kit into one `NX2navi-Linux--x86_64.AppImage` with its own Python and the +pinned Vulkan headers, for players without a source checkout: it checks for the packages above (their Vulkan headers +are not needed), installs zbic with pip on its first run, picks `--jobs` from the memory, builds into `~/Games/Ocarina` (or the folder given after the dump) and writes `play.sh` beside the game. diff --git a/docs/building.md b/docs/building.md index 8237793c..aa463889 100644 --- a/docs/building.md +++ b/docs/building.md @@ -94,6 +94,9 @@ of them. .venv\Scripts\python tools\build_game.py --version ``` + The menu's font comes into `runtime/ui/menu.cpp` with `#embed`; for a compiler without it (before clang 19, which + a Linux build with the distribution's compiler can meet) the same step writes the font's bytes as + `runtime/ui/roboto_ttf.h` (git ignores it), which `menu.cpp` then includes. The runtime's own shaders (upscaling, HDR, texture decode and the like) are in the repository as source only: GLSL and SPIR-V assembly in `runtime\shaders\`, and two small ones assembled by `tools\make_dlaa_mask.py` and `tools\make_mvobj_fs.py`. `build_game.py` compiles them into `runtime\_spv.h` (git ignores these) before it @@ -219,6 +222,10 @@ Requirements: Vulkan Memory Allocator is a single header (AUR `vulkan-memory-allocator`). Without it, `build_game.py` fetches the version the Windows release pins (3.4.0) into `toolchain/vma/` on first use and checks its hash. + The Vulkan headers must be release 363 (1.4.363, the one MSYS2 supplies on Windows) or newer: the runtime names + extensions older headers lack. Where the system's are older, `build_game.py` and `make_game.py` fetch that release + into `toolchain/vulkan-headers/` on first use, check its hash and put it ahead of the system's (`tools/vulkan_pin.py`, + `build_game.py` `vulkan_include`). The steps are the Windows ones with POSIX paths: `.venv/bin/python` in place of `.venv\Scripts\python`, and `nx2navi` in place of `nx2navi.exe`. `build_game.py` takes clang, lld and Ninja from PATH (or the ones in `/bin` with diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 65c25399..b37a0b5e 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -83,11 +83,18 @@ void cheats_frame(uint64_t frame); // cheats.c bool tab_cheats(float scale); // ui/cheats_tab.cpp; true: restart into the rebuild } -// Roboto Medium (Apache-2.0, ui/fonts/LICENSE-Roboto.txt) with Latin and Cyrillic glyphs. +// Roboto Medium (Apache-2.0, ui/fonts/LICENSE-Roboto.txt) with Latin and Cyrillic glyphs. The file comes in with +// #embed where the compiler has it (clang 19 and later define __has_embed). An older one, which a Linux build with the +// distribution's compiler can meet, includes the same bytes as numbers from roboto_ttf.h, which tools/build_shaders.py +// writes beside this file from the font when the game is built (a kit carries it). #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wc23-extensions" // #embed in C++ static const unsigned char roboto_ttf[] = { +#ifdef __has_embed #embed "fonts/Roboto-Medium.ttf" +#else +#include "roboto_ttf.h" +#endif }; #pragma clang diagnostic pop diff --git a/tests/launcher_test.py b/tests/launcher_test.py index a839f02a..8a67f561 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1870,6 +1870,131 @@ class MakeGameTest(Tmp): self.assertNotEqual(self.mg.object_key(plain, src, dst, cache, 'id', sysroot=True), self.mg.object_key(fast, src, dst, cache, 'id', sysroot=True)) + def test_vulkan_headers_go_first_for_the_runtime_only(self): + """The folder of the pinned Vulkan headers goes ahead of every other include folder for the runtime's sources, + so that no folder that also is a system one answers for ; the lifted code's command, which is + part of its objects' keys, is the one it was.""" + inc = ['-I', 'rt', '-I', '/usr/include'] + with_vk = self.mg.runtime_cmd('clang', 'clang++', inc, 3, first=['-I', 'kit/vulkan']) + without = self.mg.runtime_cmd('clang', 'clang++', inc, 3) + guest = (Path('lift/main_0001.c'), Path('obj/main_main_0001.o')) + self.assertEqual(with_vk(*guest), without(*guest)) + for src in (Path('rt/vk_device.c'), Path('rt/ui/menu.cpp')): + cmd, plain = with_vk(src, Path('obj/rt_x.o')), without(src, Path('obj/rt_x.o')) + self.assertEqual(cmd.count('-I'), plain.count('-I') + 1) + self.assertLess(cmd.index('kit/vulkan'), cmd.index('rt')) + self.assertLess(cmd.index('kit/vulkan'), cmd.index('/usr/include')) + + def test_early_check_tries_the_runtime_before_the_long_steps(self): + """The check of a Linux build runs the compile step's own command with -fsyntax-only on each source and links + an empty program against the game's libraries. When files fail it stops with every one of them named (the + compile step named three of four), and a missing header is told from an old one.""" + srcs = [self.tmp / n for n in ('good.c', 'bad1.c', 'bad2.c', 'bad3.c', 'bad4.cpp')] + for s in srcs: + s.write_text('int x;\n') + ran = [] + + def compiler(errors): + def run(cmd, **kw): + ran.append([str(c) for c in cmd]) + name = next((Path(c).name for c in cmd if Path(str(c)).name.startswith(('good', 'bad'))), '') + bad = name.startswith('bad') + return subprocess.CompletedProcess(cmd, 1 if bad else 0, '', errors.get(name, 'error: old') if bad else '') + return run + + def check(errors, sources): + ran.clear() + err = io.StringIO() + with mock.patch.multiple(self.mg, toolchain=lambda msys: (Path('clang'), Path('clang++'), {}, Path('vma')), + clang_version=lambda clang, env: 'clang version 18.1.3', + vulkan_headers=lambda clang, env: ['-I', 'kit/vulkan'], + runtime_headers=lambda rt: None, EVENTS=False), \ + mock.patch.object(self.mg.subprocess, 'run', compiler(errors)), \ + contextlib.redirect_stdout(io.StringIO()), contextlib.redirect_stderr(err): + try: + self.mg.preflight(self.msys, 2, sources) + return None, err.getvalue() + except SystemExit: + return 'stopped', err.getvalue() + + stopped, text = check({}, srcs) + self.assertEqual(stopped, 'stopped') + self.assertIn('did not compile: bad1.c, bad2.c, bad3.c, bad4.cpp', text) + self.assertIn('4 of the runtime\'s 5 files', text) + self.assertIn('clang version 18.1.3', text) + self.assertIn('kit/vulkan', text) # where the Vulkan headers came from + compiles = [c for c in ran if '-fsyntax-only' in c] + self.assertEqual(len(compiles), 5) + for c in compiles: + self.assertEqual(c[1], '-fsyntax-only') + self.assertFalse({'-c', '-o'} & set(c)) + self.assertLess(c.index('kit/vulkan'), c.index(str(self.mg.KIT / 'runtime'))) + self.assertEqual(len(ran), 5) # no link is tried once sources failed + stopped, text = check({'bad1.c': "bad1.c:1:10: fatal error: 'SDL3/SDL.h' file not found"}, srcs[:2]) + self.assertIn('These headers are missing: SDL3/SDL.h', text) + # every source passes: the empty program is linked against the game's libraries + stopped, text = check({}, srcs[:1]) + self.assertIsNone(stopped) + link = ran[-1] + self.assertNotIn('-fsyntax-only', link) + self.assertEqual(link[-len(self.mg.LINK_LIBS):], self.mg.LINK_LIBS) + + def test_pinned_vulkan_headers(self): + """tools/vulkan_pin.py keeps the C headers and the licence note of the pinned archive and nothing else, takes + no other archive, and says the folder holds them only once the last file is written.""" + import vulkan_pin + buf = io.BytesIO() + with tarfile.open(fileobj=buf, mode='w:gz') as tar: + for name, data in (('include/vulkan/vulkan_core.h', b'#define VK_HEADER_VERSION 363\n'), + ('include/vulkan/vulkan.h', b'#include "vulkan_core.h"\n'), + ('include/vk_video/vulkan_video_codecs_common.h', b'\n'), + ('include/vulkan/vulkan.hpp', b'// C++\n'), ('registry/vk.xml', b''), + ('include/vulkan/../../outside.h', b'x'), ('LICENSE.md', b'Apache-2.0 OR MIT\n')): + info = tarfile.TarInfo(vulkan_pin.TOP + name) + info.size = len(data) + tar.addfile(info, io.BytesIO(data)) + archive = self.tmp / 'headers.tar.gz' + archive.write_bytes(buf.getvalue()) + dest = self.tmp / 'toolchain' / 'vulkan-headers' + with self.assertRaises(SystemExit), contextlib.redirect_stdout(io.StringIO()): + vulkan_pin.resolve(dest, archive) # not the pinned archive + self.assertFalse(vulkan_pin.have(dest)) + pinned = (vulkan_pin.SOURCE[0], hashlib.sha256(buf.getvalue()).hexdigest()) + with mock.patch.object(vulkan_pin, 'SOURCE', pinned): + self.assertEqual(vulkan_pin.resolve(dest, archive), dest) + files = sorted(f.relative_to(dest).as_posix() for f in dest.rglob('*') if f.is_file()) + self.assertEqual(files, ['LICENSE.md', 'include/vk_video/vulkan_video_codecs_common.h', + 'include/vulkan/vulkan.h', 'include/vulkan/vulkan_core.h', vulkan_pin.STAMP]) + self.assertFalse((self.tmp / 'outside.h').exists() or (self.tmp / 'toolchain' / 'outside.h').exists()) + self.assertTrue(vulkan_pin.have(dest)) + self.assertEqual(vulkan_pin.resolve(dest), dest) # there already: nothing is fetched + self.assertEqual(vulkan_pin.header_version((dest / 'include' / 'vulkan' / 'vulkan_core.h').read_text()), 363) + self.assertEqual(vulkan_pin.header_version('#define VK_HEADER_VERSION_COMPLETE 1\n'), 0) + (dest / vulkan_pin.STAMP).unlink() # a folder cut short while it was written + self.assertFalse(vulkan_pin.have(dest)) + + def test_embedded_file_as_a_header_of_bytes(self): + """tools/build_shaders.py writes the menu font's bytes as a header for compilers without #embed: the numbers + are the file's bytes, and the header is stale once the file changes.""" + import build_shaders + rt = self.tmp / 'runtime' + font = rt / 'ui' / 'fonts' / 'Roboto-Medium.ttf' + font.parent.mkdir(parents=True) + data = bytes(range(256)) * 3 + b'\r\n\x00tail' + font.write_bytes(data) + header = rt / 'ui' / 'roboto_ttf.h' + with mock.patch.object(build_shaders, 'ASSEMBLED', {}), contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(build_shaders.stale(rt), [font]) + self.assertEqual(build_shaders.build(rt), 1) + self.assertEqual(build_shaders.stale(rt), []) + numbers = ''.join(ln for ln in header.read_text(encoding='utf-8').splitlines() if not ln.startswith('//')) + self.assertEqual(bytes(int(n) for n in numbers.split(',') if n), data) + font.write_bytes(data + b'!') + self.assertEqual(build_shaders.stale(rt), [font]) + menu = (ROOT / 'runtime' / 'ui' / 'menu.cpp').read_text(encoding='utf-8') + self.assertIn('#ifdef __has_embed\n#embed "fonts/Roboto-Medium.ttf"\n#else\n#include "roboto_ttf.h"\n#endif', menu) + self.assertEqual(build_shaders.EMBEDDED, {'ui/fonts/Roboto-Medium.ttf': 'ui/roboto_ttf.h'}) + def test_objects_of_an_earlier_version_are_taken_over(self): """An entry under a key an earlier version gave the same compile is re-recorded under the new key when its files read as they did; a header in the earlier kit's folder is read in this kit's (that folder may be gone).""" diff --git a/tools/build_game.py b/tools/build_game.py index 33a0236f..e04db449 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -61,6 +61,7 @@ import ctypes.wintypes as wt # the job-object structures below; importing it w import hashlib import json import os +import re import shutil import subprocess import sys @@ -180,6 +181,40 @@ def vma_include(): return VMA_LOCAL +def system_vulkan_version(clang, env=None): + """VK_HEADER_VERSION of the this compiler finds by itself (its own search path, CPATH + included), 0 when it finds none.""" + probe = '#include \nnx2navi_vulkan_headers VK_HEADER_VERSION\n' + try: + p = subprocess.run([str(clang), '-E', '-P', '-x', 'c', '-'], input=probe, capture_output=True, text=True, + env=env, errors='replace') + except OSError: + return 0 + m = re.search(r'nx2navi_vulkan_headers\s+(\d+)', p.stdout) if p.returncode == 0 else None + return int(m.group(1)) if m else 0 + + +def vulkan_include(clang=None, env=None): + """A build with the system's own headers (Linux without --deps): the folder to put ahead of them for + , or None when the system's serve. The runtime names Vulkan extensions newer than the headers + many distributions package, and every file that does fails to compile there (tools/vulkan_pin.py). So: + toolchain/vulkan-headers when it holds the pinned release (a kit carries it; a checkout once it was fetched), else + nothing when the system's headers are at least that release, else the pinned release, fetched there once. + Windows takes MSYS2's headers and returns None.""" + if WIN: + return None + import vulkan_pin + local = ROOT / vulkan_pin.FOLDER + if vulkan_pin.have(local): + return local / 'include' + found = system_vulkan_version(clang or CLANG, env) + if found >= vulkan_pin.HEADER_VERSION: + return None + print(f'build_game: the system\'s Vulkan headers are {f"release {found}" if found else "missing"}; the runtime is ' + f'built against release {vulkan_pin.HEADER_VERSION} ({vulkan_pin.VERSION}) or newer', flush=True) + return vulkan_pin.resolve(local) / 'include' + + # --toolchain: an llvm-mingw tree. The libraries, headers and DLLs MSYS2 supplies come from the deps directory instead. LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lvulkan-1', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a'] @@ -380,6 +415,9 @@ def ninja_file(gen, out, mods, pool_depth, skip=(), prebuilt=False, runtime_lib= vma = f'{cmd_path(DEPS / "include")} -I {cmd_path(DEPS / "include" / "vma")}' if DEPS else cmd_path(vma_include()) imgui = cmd_path(RUNTIME / 'third_party' / 'imgui') rt_inc = f'{inc} -I {cmd_path(out)} -I {vma} -I {imgui}' # romfs_config.h, vk_mem_alloc.h (+ deps headers), imgui.h + vulkan = None if DEPS else vulkan_include() # deps carries the Vulkan headers itself + if vulkan: # first, so that no other folder on the path answers for (VMA's may be /usr/include) + rt_inc = f'-I {cmd_path(vulkan)} {rt_inc}' libs = [f'-L {cmd_path(DEPS / "lib")}'] + (LLVM_LINK_LIBS if WIN else LINK_LIBS) if DEPS else LINK_LIBS # A compiler cache (sccache) may front clang: every lift rewrites the generated chunks with the same contents, so # the cached objects save the compile step on the next build. diff --git a/tools/build_shaders.py b/tools/build_shaders.py index feddcca7..69088412 100644 --- a/tools/build_shaders.py +++ b/tools/build_shaders.py @@ -1,4 +1,4 @@ -"""Build the runtime's own shaders into C headers of SPIR-V words. +"""Build the runtime's own shaders into C headers of SPIR-V words, and the files it embeds into headers of bytes. The repository holds the shaders as source only. Each becomes runtime/_spv.h, which declares `static const uint32_t _spv[]`; the headers are build outputs (ignored by git) and tools/build_game.py builds the @@ -8,6 +8,9 @@ missing and the stale ones before it compiles the runtime. Three kinds of source runtime/shaders/.spvasm SPIR-V assembly, assembled with spirv-as and checked with spirv-val tools/make_dlaa_mask.py, make_mvobj_fs.py shaders assembled in Python (ASSEMBLED); they need no other tool +One more kind of header is no shader: a file the runtime embeds (EMBEDDED: the menu's font) as a list of its bytes, +for a compiler without #embed (runtime/ui/menu.cpp includes it there). It needs no tool. + A compiled or assembled header records the SHA-256 of its source and is stale when that no longer matches; a Python one is stale when its text is not what its script gives. glslc, spirv-as and spirv-val come from MSYS2 UCRT64 on Windows (packages mingw-w64-ucrt-x86_64-shaderc and mingw-w64-ucrt-x86_64-spirv-tools) and from PATH elsewhere @@ -36,10 +39,28 @@ ASM = '.spvasm' ASM_TARGET = 'vulkan1.3' # header name -> the script beside this one that assembles it; the script's HEADER is the header's text ASSEMBLED = {'dlaa_mask': 'make_dlaa_mask.py', 'mvobj_fs': 'make_mvobj_fs.py'} +# a file below runtime/ the runtime embeds -> the header of its bytes, below runtime/ too (git ignores it) +EMBEDDED = {'ui/fonts/Roboto-Medium.ttf': 'ui/roboto_ttf.h'} + + +def embedded(src): + """True for a file of EMBEDDED: a binary file, read as it is.""" + return src.suffix not in STAGES and src.suffix not in (ASM, '.py') def source_hash(src): - return hashlib.sha256(src.read_bytes().replace(b'\r\n', b'\n')).hexdigest() # the same on a CRLF checkout + data = src.read_bytes() + return hashlib.sha256(data if embedded(src) else data.replace(b'\r\n', b'\n')).hexdigest() # text: the same on a CRLF checkout + + +def bytes_header(src, runtime): + """The header of an embedded file: its bytes as decimal numbers, to include inside an array's braces.""" + data = src.read_bytes() + lines = [f'// Generated by tools/build_shaders.py from runtime/{src.relative_to(runtime).as_posix()}; do not edit.', + f'// source sha256 {source_hash(src)}', + f'// The file\'s {len(data)} bytes, for a compiler without #embed.'] + lines += [','.join(map(str, data[i:i + 32])) + ',' for i in range(0, len(data), 32)] + return '\n'.join(lines) + '\n' def header_of(src, words): @@ -64,6 +85,7 @@ def sources(runtime): found = sorted(p for p in shaders.iterdir() if p.suffix in STAGES or p.suffix == ASM) if shaders.is_dir() else [] out = [(p, runtime / f'{p.stem}_spv.h') for p in found] out += [(TOOLS / script, runtime / f'{name}_spv.h') for name, script in ASSEMBLED.items() if (TOOLS / script).is_file()] + out += [(runtime / src, runtime / header) for src, header in EMBEDDED.items() if (runtime / src).is_file()] return out @@ -130,12 +152,14 @@ def build(runtime, only=None): todo = stale(runtime) if only is None else list(only) headers = dict(sources(runtime)) for src in todo: - text = assembled_text(src) if src.suffix == '.py' else header_of(src, words_of(src)) + text = (assembled_text(src) if src.suffix == '.py' else bytes_header(src, runtime) if embedded(src) + else header_of(src, words_of(src))) header = headers[src] part = header.with_name(header.name + '.part') # whole or not at all: another build may be reading the folder part.write_text(text, encoding='utf-8', newline='\n') part.replace(header) - print(f'{header.name}: {text.count("0x")} words from {src.name}') + print(f'{header.name}: {src.stat().st_size} bytes of {src.name}' if embedded(src) + else f'{header.name}: {text.count("0x")} words from {src.name}') return len(todo) diff --git a/tools/make_appimage.py b/tools/make_appimage.py index 99696708..b199b8fc 100644 --- a/tools/make_appimage.py +++ b/tools/make_appimage.py @@ -8,9 +8,10 @@ with --events and reads its progress lines). The AppImage holds no game files: the kit's tools/, recomp/, runtime/ and docs, the shader translator built by tools/build_xlate.py (bin/nx2navi_xlate), NVIDIA's DLSS library with its notice (bin/, tools/dlss_pin.py: downloaded -from NVIDIA's DLSS SDK repository and checked, or --dlss; not with --no-dlss), the pinned Vulkan Memory Allocator -header (an AppImage made without the library downloads it after a build on machines with NVIDIA's driver: AppRun's -get_dlss), and a relocatable CPython 3.14 (python-build-standalone, as `uv python install 3.14` puts in +from NVIDIA's DLSS SDK repository and checked, or --dlss; not with --no-dlss: an AppImage made without the library +downloads it after a build on machines with NVIDIA's driver, AppRun's get_dlss), the pinned Vulkan Memory Allocator +header, the pinned Vulkan headers (toolchain/vulkan-headers, tools/vulkan_pin.py: the runtime names Vulkan extensions +newer than the headers many distributions package), and a relocatable CPython 3.14 (python-build-standalone, as `uv python install 3.14` puts in ~/.local/share/uv/python) with numpy, lz4, zstandard, fonttools and pillow, and with --prompt-art the button art make_game.py's prompts step draws from (as make_kit.py --prompt-art: the pictures tools/prompt_art.py art_files names, into the kit's prompt-art folder; without it that step is skipped and the game keeps the Switch prompts). zbic, the GPL @@ -18,7 +19,9 @@ package that unpacks the game's program files, is not bundled: the AppImage inst ~/.local/share/nx2navi/pydeps the first time it runs, as the Windows launcher downloads it. The compiler and the libraries come from the system (README-BuildKit.md, "Linux"): the AppImage checks for clang, -clang++, lld and the headers before it starts, and names the packages for Arch, Fedora and Debian/Ubuntu. +clang++, lld and the headers before it starts, and names the packages for Arch, Fedora and Debian/Ubuntu. Whether +they are new enough is found out by trying them: tools/make_game.py checks the runtime's sources with that compiler +and links against those libraries before the build's long steps (preflight). """ import argparse import os @@ -72,7 +75,8 @@ ev step deps '"state":"start"' missing=() for tool in clang clang++ ld.lld; do command -v "$tool" >/dev/null || missing+=("$tool"); done -for h in SDL3/SDL.h vulkan/vulkan.h opus/opus.h vpx/vpx_decoder.h dav1d/dav1d.h zstd.h; do +# The Vulkan headers come with this file (the kit's toolchain/vulkan-headers): a distribution's may be too old. +for h in SDL3/SDL.h opus/opus.h vpx/vpx_decoder.h dav1d/dav1d.h zstd.h; do [ -e "/usr/include/$h" ] || [ -e "/usr/local/include/$h" ] || missing+=("$h") done if [ ${#missing[@]} -gt 0 ]; then @@ -226,6 +230,10 @@ def main(): dlss_pin.place(dlss, kit / 'bin', linux=True) (kit / 'toolchain' / 'vma').mkdir(parents=True) shutil.copy2(ROOT / 'toolchain' / 'vma' / 'vk_mem_alloc.h', kit / 'toolchain' / 'vma' / 'vk_mem_alloc.h') + # The Vulkan headers the runtime is written against, ahead of the system's on the player's machine + # (tools/build_game.py vulkan_include finds them in the kit): fetched and checked here when not there yet + import vulkan_pin + shutil.copytree(vulkan_pin.resolve(ROOT / vulkan_pin.FOLDER), kit / vulkan_pin.FOLDER) if a.prompt_art: import prompt_art n, size = prompt_art.copy_art(a.prompt_art, kit / prompt_art.FOLDER) diff --git a/tools/make_game.py b/tools/make_game.py index 12d2476c..b13295a8 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -884,6 +884,81 @@ def compile_all(srcs, cmd_for, cache, jobs, env, progress=None, salt='', stop=No return len(todo), reused, bad +def report_failures(bad): + """The files that did not compile, to stderr: the compiler's errors of the first three, and the names of all of + them, so that a paste of the build's end says which they were.""" + for src, err in bad[:3]: + print(f'--- {src}\n{err}', file=sys.stderr) + print('did not compile: ' + ', '.join(Path(src).name for src, _ in bad), file=sys.stderr, flush=True) + + +def runtime_headers(rt): + """The headers tools/build_shaders.py builds into runtime/ (the runtime's own shaders, the menu font's bytes): in + the repository as source only, so from a source checkout they are not there until they are built, and every + runtime file that includes one would fail to compile. A kit carries them built; a release's kit carries neither + tools/build_shaders.py nor the compilers it calls, and skips this.""" + try: + import build_shaders + except ModuleNotFoundError: + return + stale = build_shaders.stale(rt) + if stale: + say(f' building {len(stale)} of the runtime\'s own headers (runtime/*_spv.h, the menu font)') + build_shaders.build(rt, stale) + + +def preflight(msys, jobs, sources=None): + """Linux, before the long steps of a build whose compile step will run: this machine's compiler, headers and + libraries are tried on the runtime itself. Every runtime source that needs no header of this install is checked + with the compile step's own command (clang -fsyntax-only), and an empty program is linked against the libraries + the game links. The build takes the compiler, the linker and the libraries from the system (only the Vulkan + headers and VMA come with the kit), and a distribution's older compiler, older headers or a missing development + package stopped the compile step only after the RomFS image and the lift, ten minutes and more into the build. + sources: the files to check instead (tests).""" + clang, clangxx, env, vma = toolchain(msys) + version = clang_version(clang, env) + say(f'check: the compiler is {version}') + rt = KIT / 'runtime' + runtime_headers(rt) + inc = ['-I', str(rt), '-I', str(rt / 'third_party' / 'imgui'), '-I', str(vma)] + vulkan = vulkan_headers(clang, env) + cmd_for = runtime_cmd(clang, clangxx, inc, first=vulkan) + if sources is None: + tainted = per_install_names() + sources = [p for p, _ in runtime_sources() if not per_install(p, tainted)] + + def check(src): + cmd = [c for c in cmd_for(src, Path('unused.o')) if c not in ('-c', '-o', 'unused.o')] + p = subprocess.run([cmd[0], '-fsyntax-only', *cmd[1:]], capture_output=True, text=True, env=env, errors='replace') + return src, p.returncode, p.stderr[-1500:] + + t0 = time.time() + with concurrent.futures.ThreadPoolExecutor(max(1, jobs)) as ex: + bad = [(src, err) for src, rc, err in ex.map(check, sources) if rc] + if bad: + report_failures(bad) + lacking = sorted(set(re.findall(r"fatal error: '([^']+)' file not found", ''.join(e for _, e in bad)))) + fail(f'{len(bad)} of the runtime\'s {len(sources)} files do not compile on this machine, so the build would stop ' + f'in its compile step. ' + + (f'These headers are missing: {", ".join(lacking)}. Install the development packages ' + f'README-BuildKit.md lists under "Linux".' if lacking else + f'The compiler ({version}) or a library\'s headers are older than the runtime needs: the releases are ' + f'built with clang 23, and the Vulkan headers come ' + + (f'from {vulkan[1]}' if vulkan else 'from the system') + '. The errors above name what is missing.')) + with tempfile.TemporaryDirectory(prefix='nx2navi-check-') as tmp: + probe = Path(tmp) / 'probe.c' + probe.write_text('int main(void) { return 0; }\n', encoding='ascii') + ld = (['-fuse-ld=lld'] if shutil.which('ld.lld') else []) + ['-no-pie'] + p = subprocess.run([str(clangxx), *ld, '-o', str(Path(tmp) / 'probe'), '-x', 'c', str(probe), '-x', 'none', + *LINK_LIBS], capture_output=True, text=True, env=env, errors='replace') + if p.returncode: + print(p.stderr[-2000:], file=sys.stderr) + fail('the libraries the game links are not all there (the linker\'s lines above name the missing ones): ' + 'install the development packages README-BuildKit.md lists under "Linux". ' + 'On Fedora the static C++ library is its own package, libstdc++-static.') + say(f'check: {len(sources)} runtime files and the libraries pass, {time.time() - t0:.0f} s') + + def runtime_sources(): """(source, object name) of every runtime file this OS's build compiles: the Win32 files on Windows, the SDL window's (*_sdl.*) and platform/linux.c elsewhere (tools/build_game.py's foreign_os).""" @@ -985,8 +1060,10 @@ def fast_clang(msys, clang, env): return fast if mine and clang_ident(fast, env) == mine else None -def runtime_cmd(clang, clangxx, inc, level=0, fast=None, msys=None): - """fast: fast_clang's compiler for the lifted code, with msys as its sysroot.""" +def runtime_cmd(clang, clangxx, inc, level=0, fast=None, msys=None, first=()): + """fast: fast_clang's compiler for the lifted code, with msys as its sysroot. first: include flags that go + ahead of inc for the runtime's sources only (vulkan_headers); the lifted code includes none of those headers, + and its command, which is part of its objects' keys, stays as it was.""" def cmd_for(src, dst): cpp = src.suffix in ('.cpp', '.cxx') # the lifted code at -O1 (its size makes -O2 slow to build), the runtime at -O2; neither may assume strict @@ -999,7 +1076,7 @@ def runtime_cmd(clang, clangxx, inc, level=0, fast=None, msys=None): # -fdeclspec: third_party/ffx_api/ffx_api.h declares its entry points __declspec(dllexport) unconditionally declspec = ['-fdeclspec'] if cpp and not WIN else [] return [*cc, '-c', '-std=gnu++17' if cpp else '-std=gnu11', '-O1' if guest else '-O2', - *march, '-fno-strict-aliasing', '-w', *declspec, *inc, '-o', str(dst), str(src)] + *march, '-fno-strict-aliasing', '-w', *declspec, *([] if guest else first), *inc, '-o', str(dst), str(src)] return cmd_for @@ -1018,12 +1095,12 @@ def make_runtime_lib(dest, msys, jobs, obj=None): inc = ['-I', str(rt), '-I', str(rt / 'third_party' / 'imgui'), '-I', str(vma)] cache = ObjectCache(obj / 'objects.json', {'': KIT}) t0 = time.time() - compiled, reused, bad = compile_all(srcs, runtime_cmd(clang, clangxx, inc), cache, jobs, env, + compiled, reused, bad = compile_all(srcs, runtime_cmd(clang, clangxx, inc, first=vulkan_headers(clang, env)), + cache, jobs, env, lambda d, t: (d % 20 == 0 or d == t) and say(f' {d} of {t}'), salt=compiler_id(clang, env)) if bad: - for src, err in bad[:3]: - print(f'--- {src}\n{err}', file=sys.stderr) + report_failures(bad) fail(f'{len(bad)} runtime files did not compile') lib = dest / RUNTIME_LIB lib.unlink(missing_ok=True) @@ -1168,6 +1245,21 @@ def toolchain(msys): return Path(shutil.which('clang')), Path(shutil.which('clang++')), dict(os.environ), vma +def vulkan_headers(clang, env): + """The include flags that put the kit's Vulkan headers ahead of the system's for the runtime's sources: the + pinned release in /toolchain/vulkan-headers (tools/vulkan_pin.py), which the AppImage's kit carries and a + source checkout fetches when the system's headers are older (tools/build_game.py vulkan_include). None on + Windows, where MSYS2 supplies the headers, and none where the system's headers are new enough.""" + if WIN: + return [] + try: + from build_game import vulkan_include + folder = vulkan_include(clang, env) + except ImportError: # a kit without tools/vulkan_pin.py: the system's headers, as before it existed + return [] + return ['-I', str(folder)] if folder else [] + + def xlate_source(): """The folder of the shader translator to copy beside the game: the kit's bin/, else a source checkout's build/xlate/ (tools/build_xlate.py).""" @@ -1223,17 +1315,7 @@ def step_compile(out, cache_dir, msys, jobs, romfs_job=None, update=False): gen.mkdir(parents=True, exist_ok=True) obj.mkdir(parents=True, exist_ok=True) rt = KIT / 'runtime' - # The runtime's own shaders are in the repository as source only: from a source checkout their headers are not - # there until they are built, and every runtime file that includes one would fail to compile. A kit carries them - # built and does not carry tools/build_shaders.py (nor the compilers it calls), so a kit build skips this step. - try: - import build_shaders - except ModuleNotFoundError: - build_shaders = None - stale = build_shaders.stale(rt) if build_shaders else [] - if stale: - say(f' compiling {len(stale)} of the runtime\'s own shaders (runtime/*_spv.h)') - build_shaders.build(rt, stale) + runtime_headers(rt) clang, clangxx, env, vma = toolchain(msys) if not romfs_job: romfs_index(out, gen) @@ -1260,15 +1342,23 @@ def step_compile(out, cache_dir, msys, jobs, romfs_job=None, update=False): fast = fast_clang(msys, clang, env) if fast: say(f' the lifted code compiles with {fast}') - cmd_for = runtime_cmd(clang, clangxx, inc, level, fast, msys) + vulkan = vulkan_headers(clang, env) + if vulkan: + say(f' the Vulkan headers come from {vulkan[1]}') + cmd_for = runtime_cmd(clang, clangxx, inc, level, fast, msys, first=vulkan) # The keys earlier versions gave the same compiles (compile_all alts): 0.6.0's, with the compiler's file time and, - # with the fast compiler, its id and --sysroot; 0.5.0's and earlier's, with their kit's folder in the paths. + # with the fast compiler, its id and --sysroot; 0.5.0's and earlier's, with their kit's folder in the paths; and + # up to 0.7.1 the runtime's without the Vulkan headers' folder ahead of the system's (an object that compiled + # against the system's headers is as good). plain = runtime_cmd(clang, clangxx, inc, level, None, msys) + no_vulkan = runtime_cmd(clang, clangxx, inc, level, fast, msys) fast_old = (cid_old + compiler_ids(fast, env)[1]) if fast else None kits = earlier_kits() def alts(src, dst): keys = [object_key(plain(src, dst), src, dst, cache, cid_old, sysroot=True)] + if vulkan: + keys.append(object_key(no_vulkan(src, dst), src, dst, cache, cid)) if fast: keys.append(object_key(cmd_for(src, dst), src, dst, cache, fast_old, sysroot=True)) keys += [legacy_key(src, dst, cache, cid_old, clang, clangxx, k, lift, gen, vma) for k in kits] @@ -1306,8 +1396,7 @@ def step_compile(out, cache_dir, msys, jobs, romfs_job=None, update=False): compiled, reused = compiled + c2, reused + r2 say(f' {compiled} compiled, {reused} unchanged since the last build') if bad: - for src, err in bad[:3]: - print(f'--- {src}\n{err}', file=sys.stderr) + report_failures(bad) fail(f'{len(bad)} files did not compile') if cache.keep_only({o.name for _, o in srcs}): cache.save() @@ -1846,6 +1935,10 @@ def main(): # the compile step's key does not name the level: without this a finished compile reads as done before state['steps'].pop('compile', None) keep_awake() + # Linux: the compiler, headers and libraries are the system's, so they are tried on the runtime before the long + # steps of a build that will reach the compile step (the first step whose key is not current makes the rest run) + if not WIN and any(state['steps'].get(n) != step_key(n, src.identity, out) for n in STEPS[:STEPS.index('compile') + 1]): + preflight(msys, a.jobs) t0 = time.time() romfs_job = None runs = {'exefs': lambda: step_exefs(src, out), 'romfs': lambda: step_romfs(src, out, min(a.jobs, 8)), diff --git a/tools/make_kit.py b/tools/make_kit.py index 14472176..6c77b9cb 100644 --- a/tools/make_kit.py +++ b/tools/make_kit.py @@ -30,7 +30,8 @@ NAME = 'NX2navi-BuildKit' TOOLS = ('make_game.py', 'nup.py', 'romfs_index.py', 'check_content.py', 'hostmem.py', 'prepare_shaders.py', 'shader_census.py', 'shader_list.py', 'zstd_compat.py', 'pad_prompts.py', 'prompt_art.py', 'button_icons.py', 'romfs_check.py', 'build_game.py', 'check_isa.py', 'build_shaders.py', 'make_dlaa_mask.py', 'make_mvobj_fs.py', - 'pack_shader_cache.py') # make_game.py imports build_game's vma_include on Linux for the VMA header, and + 'pack_shader_cache.py', 'vulkan_pin.py') # make_game.py imports build_game's vma_include and vulkan_include on +# Linux for the VMA header and for Vulkan headers newer than the system's (which build_game takes from vulkan_pin), and # build_game.py imports check_isa and build_shaders, which reads the two make_ scripts' shaders TOP = ('LICENSE', 'LICENSE-ADDITIONS', 'THIRD_PARTY_NOTICES.md', 'VERSION', 'nx2navi.ini.example') SKIP_DIRS = {'__pycache__', '.pytest_cache'} diff --git a/tools/make_release_linux.py b/tools/make_release_linux.py index 2d476b7e..90a50710 100644 --- a/tools/make_release_linux.py +++ b/tools/make_release_linux.py @@ -40,6 +40,7 @@ from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import build_game # noqa: E402 import make_release as mr # noqa: E402 +import vulkan_pin # noqa: E402 ROOT = mr.ROOT MAIN_ROOT = mr.MAIN_ROOT @@ -69,8 +70,7 @@ PINS = { '5749cbc4e668273514150a80e387a957f933c6ed3f5f11e03fb30955e2bbead6'), 'zstd-src': ('https://github.com/facebook/zstd/releases/download/v1.5.7/zstd-1.5.7.tar.gz', 'eb33e51f49a15e023950cd7825ca74a4a2b43db8354825ac24fc1b7ee09e6fa3'), - 'vulkan-headers-src': ('https://github.com/KhronosGroup/Vulkan-Headers/archive/refs/tags/v1.4.363.tar.gz', - 'cbaf687d3c59b9666fe080e7f8396b6b4f4d344a768ee6b337c59852f4526b68'), + 'vulkan-headers-src': vulkan_pin.SOURCE, # the release a build with the system's headers takes too 'vma-header': build_game.VMA_FETCH, # appimagetool and the type-2 runtime from release tags, not "continuous", whose uploads change under the same # URL and would stop the build at the hash check @@ -169,8 +169,8 @@ def build_deps(out, llvm, sysroot): d = out / 'runtime' / 'deps' (d / 'include' / 'vma').mkdir(parents=True, exist_ok=True) (d / 'lib').mkdir(parents=True, exist_ok=True) - vk = tar_extract('vulkan-headers-src', WORK / 'vulkan-headers', ['Vulkan-Headers-1.4.363/include', - 'Vulkan-Headers-1.4.363/LICENSE.md'], strip=1) + vk = tar_extract('vulkan-headers-src', WORK / 'vulkan-headers', [f'{vulkan_pin.TOP}include', + f'{vulkan_pin.TOP}LICENSE.md'], strip=1) for sub in ('vulkan', 'vk_video'): shutil.copytree(vk / 'include' / sub, d / 'include' / sub) shutil.copy2(mr.fetch('vma-header'), d / 'include' / 'vma' / 'vk_mem_alloc.h') diff --git a/tools/vulkan_pin.py b/tools/vulkan_pin.py new file mode 100644 index 00000000..673eb403 --- /dev/null +++ b/tools/vulkan_pin.py @@ -0,0 +1,109 @@ +"""The Vulkan headers the runtime is compiled against where the system's are older. + +The runtime names structures and constants of Vulkan extensions as they come out (runtime/vk_device.c asks for +VK_KHR_pipeline_binary, VK_KHR_dynamic_rendering_local_read and VK_KHR_shader_maximal_reconvergence when the driver +has them), so it needs headers at least as new as the ones it was written against. On Windows MSYS2 supplies them and +the Linux release (tools/make_release_linux.py) carries them in runtime/deps. A build with the system's own compiler +and headers (tools/make_appimage.py's kit, a source checkout) met distributions whose packaged headers are years +older: every file that names a newer extension then fails to compile. Such a build takes the headers from here +instead: the release the others use, from Khronos's Vulkan-Headers repository, checked against its SHA-256. + +Only the C headers are kept (include/vulkan/*.h and include/vk_video/*.h, which vulkan_core.h includes) with the +repository's licence note. The headers are Apache-2.0 OR MIT (THIRD_PARTY_NOTICES.md). Standard library only: the +build kit's Python runs this. + + python tools/vulkan_pin.py [--dest toolchain/vulkan-headers] fetch and unpack the pinned headers +""" +import argparse +import hashlib +import io +import re +import shutil +import sys +import tarfile +import urllib.request +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +VERSION = '1.4.363' +HEADER_VERSION = 363 # VK_HEADER_VERSION of that release; the numbers only grow, across 1.3 and 1.4 alike +# (address, SHA-256): the archive GitHub makes of the release's tag +SOURCE = (f'https://github.com/KhronosGroup/Vulkan-Headers/archive/refs/tags/v{VERSION}.tar.gz', + 'cbaf687d3c59b9666fe080e7f8396b6b4f4d344a768ee6b337c59852f4526b68') +TOP = f'Vulkan-Headers-{VERSION}/' # the archive's top folder +FOLDER = Path('toolchain') / 'vulkan-headers' # below a checkout's or a kit's root: include/ and LICENSE.md +STAMP = 'pinned.txt' # in the folder: the release it holds, written last +KEEP = re.compile(r'include/(vulkan|vk_video)/[A-Za-z0-9_]+\.h|LICENSE\.md') + + +def have(folder): + """True when folder holds the pinned headers, complete (the stamp is written after the last file).""" + try: + return (folder / STAMP).read_text(encoding='ascii').strip() == VERSION and \ + (folder / 'include' / 'vulkan' / 'vulkan_core.h').is_file() + except OSError: + return False + + +def unpack(archive, folder): + """The C headers and the licence note of the release's archive (bytes) into folder, replacing what is there.""" + shutil.rmtree(folder, ignore_errors=True) + n = 0 + with tarfile.open(fileobj=io.BytesIO(archive), mode='r:gz') as tar: + for m in tar: + rel = m.name[len(TOP):] if m.name.startswith(TOP) else '' + if not m.isfile() or not KEEP.fullmatch(rel): # a name the pattern takes has no '..' and no other folder + continue + dst = folder / rel + dst.parent.mkdir(parents=True, exist_ok=True) + dst.write_bytes(tar.extractfile(m).read()) + n += 1 + if not (folder / 'include' / 'vulkan' / 'vulkan_core.h').is_file() or not (folder / 'LICENSE.md').is_file(): + shutil.rmtree(folder, ignore_errors=True) + sys.exit(f'vulkan_pin: the archive of Vulkan-Headers {VERSION} lacks vulkan_core.h or LICENSE.md') + (folder / STAMP).write_text(VERSION + '\n', encoding='ascii') + return n + + +def resolve(folder=None, archive=None): + """The folder holding the pinned headers (default /toolchain/vulkan-headers), fetched and unpacked there + when it does not hold them yet. archive: the release's archive on disk instead of the download; it must be the + pinned file either way. Returns the folder; its include/ goes on the include path.""" + folder = Path(folder) if folder else ROOT / FOLDER + if have(folder): + return folder + url, want = SOURCE + if archive: + data = Path(archive).read_bytes() + else: + print(f'vulkan_pin: fetching the Vulkan headers {VERSION} into {folder}', flush=True) + try: + data = urllib.request.urlopen(url, timeout=120).read() + except OSError as e: + sys.exit(f'vulkan_pin: fetching {url} failed ({e}). Install Vulkan headers {VERSION} or newer, or put the ' + f'archive on disk and run tools/vulkan_pin.py --archive ') + if hashlib.sha256(data).hexdigest() != want: + sys.exit(f'vulkan_pin: {archive or url} is not the pinned archive of Vulkan-Headers {VERSION} (SHA-256 differs)') + unpack(data, folder) + return folder + + +def header_version(text): + """VK_HEADER_VERSION as vulkan_core.h defines it in text, 0 when it does not.""" + m = re.search(r'^[ \t]*#[ \t]*define[ \t]+VK_HEADER_VERSION[ \t]+(\d+)', text, re.M) + return int(m.group(1)) if m else 0 + + +def main(): + ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + ap.add_argument('--dest', type=Path, default=ROOT / FOLDER, help='where the headers go (default toolchain/vulkan-headers)') + ap.add_argument('--archive', type=Path, help=f'the archive of Vulkan-Headers {VERSION} on disk instead of the download') + a = ap.parse_args() + folder = resolve(a.dest, a.archive) + n = sum(1 for _ in (folder / 'include').rglob('*.h')) + print(f'{folder}: Vulkan headers {VERSION}, {n} files') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) From 27009e85f34aa10646ec24503a5899e48a259c4f Mon Sep 17 00:00:00 2001 From: Tom Pipe Date: Tue, 10 Nov 2026 12:45:08 -0500 Subject: [PATCH 011/153] Link dav1d in tools/build_game.py, so the developer build links again. The developer build stopped at undefined dav1d symbols since the AV1 movie decoder (runtime/ov_movie2.c) came in: tools/make_game.py linked dav1d, tools/build_game.py did not. Windows links libdav1d.a statically, as libvpx and zstd are, so the exe imports no libdav1d DLL; Linux links -ldav1d. The llvm-mingw link list and the Linux release's static list get dav1d too, and docs/building.md lists the package for MSYS2 and Arch. From pull request #439, with the two other link lists added. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 4 ++-- tools/build_game.py | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/docs/building.md b/docs/building.md index 8237793c..3da1c6ca 100644 --- a/docs/building.md +++ b/docs/building.md @@ -24,7 +24,7 @@ Versions are the ones the build was verified with; newer patch releases are fine MSYS2 packages, after `pacman -Syuu` (run it twice; the first pass updates the core and exits): ```sh -pacman -S --needed --noconfirm mingw-w64-ucrt-x86_64-{clang,lld,ninja,gcc,make,pkgconf,sdl3,opus,libvpx,zstd,vulkan-devel,\ +pacman -S --needed --noconfirm mingw-w64-ucrt-x86_64-{clang,lld,ninja,gcc,make,pkgconf,sdl3,opus,libvpx,dav1d,zstd,vulkan-devel,\ vulkan-memory-allocator,vulkan-validation-layers,shaderc,spirv-tools} base-devel git unzip ``` @@ -213,7 +213,7 @@ Requirements: - Packages on Arch: ```sh - pacman -S --needed clang lld ninja make sdl3 opus libvpx zstd vulkan-headers vulkan-icd-loader \ + pacman -S --needed clang lld ninja make sdl3 opus libvpx dav1d zstd vulkan-headers vulkan-icd-loader \ vulkan-validation-layers shaderc spirv-tools dotnet-sdk uv git ``` diff --git a/tools/build_game.py b/tools/build_game.py index 33a0236f..e4eaad0b 100644 --- a/tools/build_game.py +++ b/tools/build_game.py @@ -5,7 +5,7 @@ recompiled only when it, a header it includes or its flags change. On Linux the same steps make build//nx2navi with the clang, lld and ninja on PATH (or the clang and lld in --toolchain DIR/bin): the runtime's *_sdl.* and platform/linux.c take the place of its *_win.*, platform/win32.c and nx2navi.rc, the libraries are -the system's (Vulkan, SDL3, Opus, libvpx, zstd; libstdc++ static), and the executable finds a libSDL3.so.0 beside it +the system's (Vulkan, SDL3, Opus, dav1d, libvpx, zstd; libstdc++ static), and the executable finds a libSDL3.so.0 beside it first (rpath $ORIGIN). docs/building.md lists the packages. .venv\\Scripts\\python tools\\build_game.py # generated/v121 -> build/v121/nx2navi.exe @@ -45,8 +45,8 @@ of its processes (Windows job object accounting: committed private bytes). --ninja PATH names the Ninja to run; without MSYS2 the bin/ninja.exe of the release folder (tools/make_release.py) is used. --toolchain DIR builds with an llvm-mingw tree (DIR/bin/clang.exe) instead of MSYS2 UCRT64 clang. Ninja still comes -from MSYS2. The libraries and headers MSYS2 supplies (Vulkan, SDL3, Opus, libvpx, VMA) then come from --deps DIR -(default toolchain/deps-mingw: include/, lib/ with the import libraries and libvpx.a, bin/ with the DLLs), and the C++ +from MSYS2. The libraries and headers MSYS2 supplies (Vulkan, SDL3, Opus, dav1d, libvpx, VMA) then come from --deps DIR +(default toolchain/deps-mingw: include/, lib/ with the import libraries, libdav1d.a and libvpx.a, bin/ with the DLLs), and the C++ runtime is libc++ from llvm-mingw. Such a build never reuses another build's generated objects. Split build. --make-runtime-lib compiles only runtime/ into /libnx2navirt.a (what CI prebuilds). --runtime-lib PATH @@ -145,14 +145,14 @@ if WIN: RT_FLAGS.append('-D_WIN32_WINNT=0x0A00') # -Xlinker passes the map path as one argument; -Wl, would split it at any comma in the build dir path LINK_FLAGS = ['-fuse-ld=lld', '-Wl,--stack,16777216', '-Xlinker', '"--Map=$builddir/nx2navi.map"'] # the map feeds tools/prof_report.py - LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lSDL3', '-lopus', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx static (movies); libzstd static (resolution.c) + LINK_LIBS = ['-lpsapi', '-lbcrypt', '-lvulkan-1', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lSDL3', '-lopus', '-l:libdav1d.a', '-l:libvpx.a', '-l:libzstd.a', '-l:libstdc++.a', '-l:libwinpthread.a'] # Vulkan back end; libvpx and libdav1d static (movies); libzstd static (resolution.c) VMA_INCLUDE = UCRT.parent / 'include' / 'vma' # vk_mem_alloc.h; MSYS2 package vulkan-memory-allocator else: # -no-pie: the lifted modules' function tables (hundreds of thousands of pointers) need no load-time relocation. # $ORIGIN: a libSDL3.so.0 next to nx2navi comes before the system's (the release's portable folder). LINK_FLAGS = ['-fuse-ld=lld', '-no-pie', '-Wl,-z,noexecstack', "'-Wl,-rpath,$$ORIGIN'", '"-Wl,-Map=$builddir/nx2navi.map"'] - LINK_LIBS = ['-lvulkan', '-lSDL3', '-lopus', '-lvpx', '-lzstd', '-l:libstdc++.a', '-lm', '-lpthread', '-ldl'] + LINK_LIBS = ['-lvulkan', '-lSDL3', '-lopus', '-ldav1d', '-lvpx', '-lzstd', '-l:libstdc++.a', '-lm', '-lpthread', '-ldl'] VMA_INCLUDE = Path('/usr/include') # vk_mem_alloc.h; Arch (AUR) vulkan-memory-allocator, Debian libvulkan-memory-allocator-dev # Without a system vk_mem_alloc.h, the header the Windows release pins (tools/make_release.py, VMA 3.4.0) is fetched once # into toolchain/vma: one MIT header (THIRD_PARTY_NOTICES.md), checked against its hash. @@ -182,7 +182,7 @@ def vma_include(): # --toolchain: an llvm-mingw tree. The libraries, headers and DLLs MSYS2 supplies come from the deps directory instead. LLVM_LINK_LIBS = ['-lpsapi', '-lbcrypt', '-luser32', '-lgdi32', '-ldwmapi', '-limm32', '-lwinmm', '-lole32', '-lshell32', '-luuid', '-lvulkan-1', '-lSDL3', '-lopus', - '-l:libvpx.a', '-l:libzstd.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a'] + '-l:libdav1d.a', '-l:libvpx.a', '-l:libzstd.a', '-l:libc++.a', '-l:libc++abi.a', '-l:libunwind.a', '-l:libwinpthread.a'] DEFAULT_DEPS = ROOT / 'toolchain' / 'deps-mingw' # Runtime sources compiled per install, not in the library: romfs.c includes romfs_config.h, patches.c and ov_zstd.c # the patches.h of the player's game version (its patch page is the page after that version's main; ov_zstd.c's host @@ -856,9 +856,9 @@ def main(): DEPS = (args.deps or DEFAULT_DEPS).resolve() dlls = sorted((DEPS / 'bin').glob('*.dll')) if WIN else [] if DEPS and not WIN: - # A release (--deps): Opus, libvpx and zstd go into the executable, so it needs no more than libc, Vulkan and - # the libSDL3.so.0 beside it - for lib in ('opus', 'vpx', 'zstd'): + # A release (--deps): Opus, dav1d, libvpx and zstd go into the executable, so it needs no more than libc, + # Vulkan and the libSDL3.so.0 beside it + for lib in ('opus', 'dav1d', 'vpx', 'zstd'): LINK_LIBS[LINK_LIBS.index(f'-l{lib}')] = f'-l:lib{lib}.a' if args.sysroot and not WIN: # Every compile and the link: the game then needs no newer glibc than the sysroot's (the Steam Runtime's) From 0d9417a8c7ed4e9134187236ea22a90d4b579bc5 Mon Sep 17 00:00:00 2001 From: Shikakiben <108907095@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:45:33 -0500 Subject: [PATCH 012/153] Check zbic in the Linux AppImage the way make_game.py imports it. The AppImage tested for zbic with PYTHONPATH and the user's site-packages in view, then ran the build with Python's isolated mode, which sees neither. A player with zbic installed for their own Python of the same minor version passed the test, never got it installed where the build looks, and was told to run the pip command that had put it in the place the build ignores. The test now uses the same isolation and the same search path as the build, so zbic is installed into the AppImage's own folder whenever the build could not import it. From pull request #436. Co-Authored-By: Claude Fable 5.2 --- tools/make_appimage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/make_appimage.py b/tools/make_appimage.py index 99696708..79991371 100644 --- a/tools/make_appimage.py +++ b/tools/make_appimage.py @@ -91,7 +91,7 @@ EOF exit 1 fi -if ! PYTHONPATH="$PYDEPS" "$PY" -c 'import zbic' 2>/dev/null; then +if ! "$PY" -I -c 'import sys; sys.path[:0] = [sys.argv[1]]; import zbic' "$PYDEPS" 2>/dev/null; then echo "Installing zbic (GPL-2.0, unpacks the game's program files) into $PYDEPS ..." "$PY" -m pip install --quiet --disable-pip-version-check --target "$PYDEPS" zbic || { echo "error: pip could not install zbic (it needs the internet once)" >&2 From 86c0fe4199a8676d7eae467b23286dd44aacbaf1 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:47:22 -0500 Subject: [PATCH 013/153] Let a game build on a network share get past a short dropout and a share that refuses private folders. A player who keeps the game on a mapped network drive could not finish a build. Three things change in tools/make_game.py: - The output folder keeps its drive letter on Windows. It was resolved to the share's UNC name, which is a second connection to the same server, and some servers drop that one under the compilers' load while the mapped drive stays up. - When a source's size cannot be read because its folder stopped answering, the build says so and asks again for up to a minute before it stops with a message that names the drive. It used to end in a traceback. A file that is gone from a folder that answers still ends the build at once. - A finished file is put in place through a temporary file beside it instead of a file in a temporary folder. Such a folder is made for its owner alone, and some shares refuse that while they take ordinary files; the build then reported a permissions fault in the output folder after everything had compiled. From pull request #419: the first and third change are the contributor's, the wait is written again so that a file that is really gone does not wait a minute, and the launcher tests cover all three. Co-Authored-By: Matthias Co-Authored-By: Claude Fable 5.2 --- tests/launcher_test.py | 76 ++++++++++++++++++++++++++++++++++++++++++ tools/make_game.py | 59 ++++++++++++++++++++++++++++---- 2 files changed, 128 insertions(+), 7 deletions(-) diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 92837ed8..915ee895 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1685,6 +1685,82 @@ class MakeGameTest(Tmp): self.assertEqual(dst.stat().st_mode & 0o777, src.stat().st_mode & 0o777) self.assertFalse(self.mg.copy_if_changed(src, dst)) + def test_copy_if_changed_makes_no_folder(self): + """A network share can refuse the owner-only folder TemporaryDirectory makes: the copy needs a file only, and + leaves nothing behind, also when the copy fails.""" + out = self.tmp / 'share' + out.mkdir() + src, dst = self.tmp / 'new.bin', out / 'installed.bin' + src.write_bytes(b'new') + dst.write_bytes(b'old') + refused = mock.patch.object(self.mg.tempfile, 'TemporaryDirectory', side_effect=PermissionError('refused')) + with refused, mock.patch.object(self.mg.os, 'mkdir', side_effect=PermissionError('refused')): + self.assertTrue(self.mg.copy_if_changed(src, dst)) + self.assertEqual(dst.read_bytes(), b'new') + self.assertEqual([p.name for p in out.iterdir()], ['installed.bin']) + src.write_bytes(b'newer') + with mock.patch.object(self.mg.shutil, 'copy2', side_effect=OSError('the share went away')): + with self.assertRaises(OSError): + self.mg.copy_if_changed(src, dst) + self.assertEqual(dst.read_bytes(), b'new') # the installed file is as it was + self.assertEqual([p.name for p in out.iterdir()], ['installed.bin']) + + def test_source_size_waits_for_a_share(self): + """A share that stops answering for a moment: the source's folder is not there either, and the size is asked + again. A folder that answers without the file ends the build at once.""" + src = self.tmp / 'lift' / 'a.c' + src.parent.mkdir() + src.write_bytes(b'int a;\n') + real_stat, real_is_dir = Path.stat, Path.is_dir + down = {'left': 3} + + def stat(p, *a, **k): + if p == src and down['left'] > 0: + raise FileNotFoundError(2, 'The network path was not found', str(p)) + return real_stat(p, *a, **k) + + def is_dir(p): # each look at the folder is one try nearer to the share's return + if p == src.parent and down['left'] > 0: + down['left'] -= 1 + return False + return real_is_dir(p) + + lines = [] + with mock.patch.object(Path, 'stat', stat), mock.patch.object(Path, 'is_dir', is_dir), \ + mock.patch.object(self.mg.time, 'sleep') as sleep, mock.patch.object(self.mg, 'say', lines.append): + self.assertEqual(self.mg.source_size(src), 7) + self.assertEqual(sleep.call_count, 3) # three tries found the share down + self.assertEqual(len(lines), 1) # said once, not at every try + # the share never comes back: the build stops after SOURCE_WAIT and names the drive + down['left'] = 10 ** 6 + clock = iter(range(0, 10 ** 6, 5)) + with mock.patch.object(Path, 'stat', stat), mock.patch.object(Path, 'is_dir', is_dir), \ + mock.patch.object(self.mg.time, 'sleep'), mock.patch.object(self.mg, 'say'), \ + mock.patch.object(self.mg.time, 'monotonic', lambda: next(clock)), \ + mock.patch.object(self.mg, 'fail', side_effect=SystemExit) as fail: + with self.assertRaises(SystemExit): + self.mg.source_size(src) + self.assertIn('stopped answering', fail.call_args[0][0]) + # the file is gone and its folder answers: no wait + down['left'] = 0 + src.unlink() + with mock.patch.object(self.mg.time, 'sleep') as sleep, \ + mock.patch.object(self.mg, 'fail', side_effect=SystemExit) as fail: + with self.assertRaises(SystemExit): + self.mg.source_size(src) + sleep.assert_not_called() + self.assertIn('lift folder', fail.call_args[0][0]) + + def test_out_path_keeps_a_drive_letter(self): + """Windows: --out is made absolute without resolve(), which would turn a mapped drive into its UNC name.""" + with mock.patch.object(self.mg, 'WIN', True), \ + mock.patch.object(Path, 'resolve', side_effect=AssertionError('resolve() was called')): + got = self.mg.out_path('game') + self.assertEqual(got, Path(os.path.abspath('game'))) + self.assertTrue(got.is_absolute()) + with mock.patch.object(self.mg, 'WIN', False): + self.assertEqual(self.mg.out_path(self.tmp / 'x' / '..' / 'game'), (self.tmp / 'game').resolve()) + @unittest.skipUnless(shutil.which('cc'), 'needs a C compiler') def test_object_cache(self): src = self.tmp / 'src' diff --git a/tools/make_game.py b/tools/make_game.py index 12d2476c..49cecb37 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -534,7 +534,9 @@ def pe_imports(path): def copy_if_changed(src, dst): """Atomically replace dst unless its contents match, even when source modification times are normalized. - A same-directory temporary copy preserves modes, replaces read-only files on POSIX and leaves dst intact on copy failure.""" + A same-directory temporary copy preserves modes, replaces read-only files on POSIX and leaves dst intact on copy + failure. The copy is a temporary file, not a file in a temporary folder: such a folder is made for its owner alone, + and some network shares refuse that while they take ordinary files.""" dst = Path(dst) s = os.stat(src) try: @@ -543,13 +545,21 @@ def copy_if_changed(src, dst): return False except FileNotFoundError: pass + replacement = None try: - with tempfile.TemporaryDirectory(dir=dst.parent, prefix='.nx2navi-copy-') as tmp: - replacement = Path(tmp) / dst.name - shutil.copy2(src, replacement) - os.replace(replacement, dst) + fd, name = tempfile.mkstemp(dir=dst.parent, prefix='.nx2navi-copy-') + os.close(fd) + replacement = Path(name) + shutil.copy2(src, replacement) + os.replace(replacement, dst) except PermissionError: fail(f'cannot replace {dst}: close the game and check output-directory permissions') + finally: + if replacement is not None: # still there only when the copy or the replace failed + try: + replacement.unlink(missing_ok=True) + except OSError: + pass return True @@ -832,6 +842,34 @@ def earlier_kits(): return sorted(kits) +SOURCE_WAIT, SOURCE_PAUSE = 60, 2 # seconds source_size waits for a drive in all, and between two tries + + +def source_size(src): + """The size of a source file about to be compiled. The sources sit in the build cache beside the game, which a + player may keep on a network share, and a share that loses its connection under the build's load answers 'not there' + until it is back: while the source's folder does not answer either, the question is asked again for up to + SOURCE_WAIT seconds. A folder that answers without the file means the file is gone, which no wait brings back.""" + src = Path(src) + end = time.monotonic() + SOURCE_WAIT + told = False + while True: + try: + return src.stat().st_size + except OSError as e: + away = not src.parent.is_dir() + if not away or time.monotonic() >= end: + fail(f'cannot read {src} ({e.strerror or e}): ' + + ('the drive or network share with the game folder stopped answering; check it and run the ' + 'build again' if away else + 'the file is gone from the build cache; delete the lift folder in the build cache and run ' + 'the build again, which makes it anew')) + if not told: + say(f' {src.parent} does not answer; waiting up to {SOURCE_WAIT} s for the drive to come back') + told = True + time.sleep(SOURCE_PAUSE) + + def compile_all(srcs, cmd_for, cache, jobs, env, progress=None, salt='', stop=None, alts=None): """Compile each (source, object) whose object is not current in cache. cmd_for(src, dst) -> argv without the dependency-file options; salt identifies the compiler. Returns (compiled, reused, failures) with failures as @@ -853,7 +891,7 @@ def compile_all(srcs, cmd_for, cache, jobs, env, progress=None, salt='', stop=No reused = len(srcs) - len(todo) if adopted: say(f' {adopted} objects an earlier version compiled are kept: the same compile of the same sources') - todo.sort(key=lambda job: job[0].stat().st_size, reverse=True) + todo.sort(key=lambda job: source_size(job[0]), reverse=True) def cc(job): src, dst = job @@ -1774,6 +1812,13 @@ def step_key(step, identity, out=None): f'|{sha256_file(xlate_source() / "nx2navi_xlate.id")}') +def out_path(arg): + """--out as an absolute path. On Windows a mapped drive letter stays as given: resolve() would name the folder by + its server and share instead (a UNC path), which is a second connection to the same share, and some servers drop + that one under the compilers' load while the mapped drive stays up.""" + return Path(os.path.abspath(arg)) if WIN else Path(arg).resolve() + + def main(): global EVENTS ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) @@ -1804,7 +1849,7 @@ def main(): ap.error('give --nup, both --exefs and --romfs, or --rebuild') if a.rebuild and not a.out: ap.error('--rebuild needs --out') - out = Path(a.out).resolve() if a.out else None + out = out_path(a.out) if a.out else None cache = out / 'build-cache' if out else None state_path = cache / 'state.json' if cache else None state = load_state(state_path) if state_path else {} From 919aa7b2a880579d30eaf53d2bb3d35bf5056765 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:48:50 -0500 Subject: [PATCH 014/153] Let the mouse camera in stick mode reach the game's top turn speed at a camera speed above 1. With the mouse camera in stick mode and a camera speed above 1, the mouse could never turn faster than a full stick does at camera speed 1, however high the mouse sensitivity was set. The mouse's counts were limited to full tilt first and divided by the camera speed afterwards, so at camera speed 1.5 the most the mouse could send was two thirds of full tilt, which the game's own factor brought back to the old top speed. The division now comes before the limit (runtime/input_map.c input_mouse_stick): a slow movement turns as far as before, and a fast flick reaches full tilt, which is the game's top speed with the camera speed in it. Direct mouse mode, the default, does not use this code. A camera speed of 1 or less behaves as before. From pull request #424. tests/mouse_stick_check.py is new here: it builds input_map.c alone and checks the deflection at each camera speed without the game. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 5 ++++ docs/controls.md | 2 +- nx2navi.ini.example | 3 +- runtime/camera_speed.c | 3 +- runtime/cfg_probe.c | 16 ++++++---- runtime/input.c | 8 ++--- runtime/input_map.c | 6 ++-- runtime/input_map.h | 5 +++- tests/mouse_stick_check.c | 61 ++++++++++++++++++++++++++++++++++++++ tests/mouse_stick_check.py | 35 ++++++++++++++++++++++ 10 files changed, 126 insertions(+), 18 deletions(-) create mode 100644 tests/mouse_stick_check.c create mode 100644 tests/mouse_stick_check.py diff --git a/docs/building.md b/docs/building.md index 3da1c6ca..da13975a 100644 --- a/docs/building.md +++ b/docs/building.md @@ -322,6 +322,11 @@ A change to what the library warm logs when the driver's cache of stage librarie `runtime/vk_pipeline.c` `lw_main`) passes `python tests\warm_hint_check.py`, which builds the header alone and checks the added text by GPU vendor and by why the cache was cold. No game files. +A change to the mouse camera's stick deflection (`runtime/input_map.c` `input_mouse_stick`) passes +`python tests\mouse_stick_check.py`, which builds that file with the rest of the runtime stubbed and checks the +deflection at each camera speed: a slow movement is divided by the factor, a fast flick reaches full tilt. No game +files. + The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: diff --git a/docs/controls.md b/docs/controls.md index a5f00305..7e467309 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -108,7 +108,7 @@ bind_Y = pad:north, key:mouse_l ## Mouse camera -Mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * NX2NAVI_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule. +Mouse motion (raw input) becomes right-stick deflection. Each presented frame takes the counts moved since the last frame, divides by the frame time and sets the deflection to `counts per second * NX2NAVI_MOUSE_SENS / 3000` of full, held for that frame, so the turn rate does not depend on the frame rate. This is not mouselook: the game's stick curve and dead zone still apply, so very slow movements do nothing and a fast flick saturates. With a camera speed other than 1 (`NX2NAVI_STICK_SENS`) the deflection is divided by that factor before it is limited to full tilt: the turn for a given movement stays the same, and the fastest turn a flick reaches is the game's top speed with the factor in it. The mouse deflection competes with the gamepad and arrow keys by the larger-deflection rule. `NX2NAVI_MOUSE_SENS` (0 to 10, default 1; higher turns faster, 0 turns the mouse camera and the cursor capture off). While the window is the foreground window the cursor is hidden and confined to the client area. It is released on focus loss, minimise, while F11 changes the window, during a size or move loop and while a text entry is open. Esc releases it by hand: the cursor shows, the mouse camera and mouse buttons stop, and the window can be moved, minimised or closed. A mouse click inside the client area captures it again; that click does not reach the game. Activating the window by a click on its title bar or border does not capture, so it can be dragged or closed right away; Alt+Tab or a click in the client area captures as before. `NX2NAVI_INPUT=none` disables the capture too. diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 2ab095cc..eb75b735 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -533,7 +533,8 @@ # the stick reads centred, and the rest of its travel is scaled to start there. # stick_deadzone = 8 # Right-stick camera speed (0.1 to 3): multiplies the game's own camera turn rates, top speed included, at each of the -# five camera sensitivities in the game's Options. Keys bound to the right stick follow it; the mouse keeps mouse_sens. +# five camera sensitivities in the game's Options. Keys bound to the right stick follow it; the mouse keeps mouse_sens +# for its turn per movement, and in stick mode the fastest turn it can reach rises and falls with this. # A change while the game runs takes effect at once (the first one of a run within a few seconds). # stick_sens = 1 # The dead zone of one stick, in place of stick_deadzone for that stick (0 to 50; unset = stick_deadzone). The Controls diff --git a/runtime/camera_speed.c b/runtime/camera_speed.c index db2b2fe7..6a2c11af 100644 --- a/runtime/camera_speed.c +++ b/runtime/camera_speed.c @@ -4,7 +4,8 @@ // PlayerCameraParam.bgyml inside Pack/Actor/PlayerCamera.pack.zs. romfs.c serves a copy of that pack with every entry // multiplied (cam_pack_build), so the setting raises the camera's top speed as well as its speed at a partial tilt, at // any of the five sensitivities. The mouse camera reaches the game as a right-stick deflection (input.c, mouse_camera), -// which divides it by the same factor (cam_speed_applied), so NX2NAVI_MOUSE_SENS alone sets the mouse's speed. +// divided by the same factor (cam_speed_applied) before it is limited to full tilt (input_mouse_stick), so +// NX2NAVI_MOUSE_SENS alone sets the mouse's turn per movement, and its top speed is the game's with the factor in it. // // The pack is a SARC archive compressed with the dictionary pack.zsdic of Pack/ZsDic.pack.zs. A float in a BYML array // is stored in the array itself, so the multiplied values take the bytes of the old ones and no offset moves. diff --git a/runtime/cfg_probe.c b/runtime/cfg_probe.c index 44c40088..a10cd1d5 100644 --- a/runtime/cfg_probe.c +++ b/runtime/cfg_probe.c @@ -330,18 +330,22 @@ int probe_cfg(void) { CHECK(st[0] == -32767 && st[1] == 0, "keys: left arrow is right stick left"); input_map_stick(dn, 0, st); CHECK(st[0] == 0 && st[1] == 0, "keys: arrows leave the left stick alone"); - input_mouse_stick(30, 0, 1.0 / 60, 1, st); + input_mouse_stick(30, 0, 1.0 / 60, 1, 1, st); CHECK(st[0] == 32767 * 1800 / 3000 && st[1] == 0, "mouse: 30 counts in 1/60 s is 60% right"); - input_mouse_stick(0, -10, 1.0 / 60, 1, st); + input_mouse_stick(0, -10, 1.0 / 60, 1, 1, st); CHECK(st[0] == 0 && st[1] > 0, "mouse: moving up is stick up"); - input_mouse_stick(30, 0, 1.0 / 30, 1, st); + input_mouse_stick(30, 0, 1.0 / 30, 1, 1, st); CHECK(st[0] == 32767 * 900 / 3000, "mouse: the same counts over twice the time are half the deflection"); - input_mouse_stick(15, 0, 1.0 / 60, 2, st); + input_mouse_stick(15, 0, 1.0 / 60, 2, 1, st); CHECK(st[0] == 32767 * 1800 / 3000, "mouse: doubling sensitivity halves the counts needed (15 counts at 2 = 30 at 1)"); - input_mouse_stick(1000, 1000, 1.0 / 60, 1, st); + input_mouse_stick(1000, 1000, 1.0 / 60, 1, 1, st); CHECK((int64_t)st[0] * st[0] + (int64_t)st[1] * st[1] <= 32768ll * 32768, "mouse: clamped to the unit circle"); - input_mouse_stick(30, 0, 1.0 / 60, 0, st); + input_mouse_stick(30, 0, 1.0 / 60, 0, 1, st); CHECK(st[0] == 0 && st[1] == 0, "mouse: sensitivity 0 is off"); + input_mouse_stick(30, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == 13107 && st[1] == 0, "mouse: a camera speed of 1.5 divides the deflection, so the turn per count stays"); + input_mouse_stick(1000, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == 32767 && st[1] == 0, "mouse: a fast flick reaches full tilt at a camera speed of 1.5 too"); input_pad_stick(3000, -2000, 0.15f, st); CHECK(st[0] == 0 && st[1] == 0, "pad stick: an 11% drift inside a 15% dead zone reads centred"); input_pad_stick(4000, -3000, 0, st); diff --git a/runtime/input.c b/runtime/input.c index 1b429cda..24936611 100644 --- a/runtime/input.c +++ b/runtime/input.c @@ -368,17 +368,15 @@ static void mouse_camera(int32_t out[2], int selector) { if (fr != frame) { double dt = age < 0.004 ? 0.004 : age > 0.1 ? 0.1 : age; ikb_mouse_take(&dx, &dy); - input_mouse_stick(dx, dy, dt, mouse_sens_get(), held); + input_mouse_stick(dx, dy, dt, mouse_sens_get(), cam_speed_applied(), held); frame = fr; t0 = now; } else if (age > 0.1) { held[0] = held[1] = 0; // frames stopped: do not hold a deflection } } - double k = cam_speed_applied(), x = held[0] / k, y = held[1] / k, len = sqrt(x * x + y * y); - if (len > STICK_MAX) x *= STICK_MAX / len, y *= STICK_MAX / len; - out[0] = (int32_t)lround(x); - out[1] = (int32_t)lround(y); + out[0] = held[0]; + out[1] = held[1]; } static float stick_dead[2] = {-1, -1}; // input_pad_deadzone of the left and right stick; live (cfg_version) diff --git a/runtime/input_map.c b/runtime/input_map.c index 2b55ca87..29875062 100644 --- a/runtime/input_map.c +++ b/runtime/input_map.c @@ -349,10 +349,10 @@ void input_map_stick(const uint8_t down[KEY_COUNT], int stick, int32_t out[2]) { out[1] = dy * m; } -void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t out[2]) { +void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, float cam, int32_t out[2]) { out[0] = out[1] = 0; - if (!(dt_s > 0) || !(sens > 0)) return; - double k = STICK_MAX * (double)sens / (MOUSE_FULL_CPS * dt_s); + if (!(dt_s > 0) || !(sens > 0) || !(cam > 0)) return; + double k = STICK_MAX * (double)sens / (MOUSE_FULL_CPS * dt_s * cam); double x = dx * k, y = -dy * k, len = sqrt(x * x + y * y); if (len > STICK_MAX) { x *= STICK_MAX / len; y *= STICK_MAX / len; } out[0] = (int32_t)lrint(x); diff --git a/runtime/input_map.h b/runtime/input_map.h index 024bfd32..299f4823 100644 --- a/runtime/input_map.h +++ b/runtime/input_map.h @@ -117,8 +117,11 @@ int input_map_held(const uint8_t down[KEY_COUNT], int action); // unit circle. void input_map_stick(const uint8_t down[KEY_COUNT], int stick, int32_t out[2]); // Mouse motion in raw counts over dt_s seconds to right-stick deflection: full deflection at 3000 / sens counts/s. +// `cam` is the factor the game multiplies the right stick's turn rates by (cam_speed_applied). The deflection is +// divided by it, so the turn per count stays what sens says, and only then clamped: a clamp taken first would hold the +// mouse's fastest turn at the game's unmultiplied top speed, however high the factor. // Clamped to the unit circle, up (negative dy) is +y. -void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, int32_t out[2]); +void input_mouse_stick(int32_t dx, int32_t dy, double dt_s, float sens, float cam, int32_t out[2]); // A gamepad stick (x, y in -32768..32767, up is +y) through a round dead zone of `dead` (fraction of full deflection, // 0..0.5): inside the circle the stick reads 0, and the rest of the travel is scaled to start at 0 at the edge, so a // drifting stick rests at 0 and full deflection still reaches full. The result stays inside the unit square. diff --git a/tests/mouse_stick_check.c b/tests/mouse_stick_check.c new file mode 100644 index 00000000..50028ba2 --- /dev/null +++ b/tests/mouse_stick_check.c @@ -0,0 +1,61 @@ +// The mouse camera in stick mode (runtime/input_map.c input_mouse_stick): raw counts to a right-stick deflection with +// the camera speed taken out of it. The game multiplies the right stick's turn rates by the camera speed, so the +// deflection is divided by it, and the division comes before the limit to full tilt: a slow movement turns as far as +// at camera speed 1, and a fast flick reaches full tilt, which the game turns into its top speed with the factor in +// it. No game files. tests/mouse_stick_check.py builds and runs this with runtime/input_map.c. +#include +#include +#include +#include +#include "input_map.h" + +// what input_map.c needs from the rest of the runtime; none of it is reached from input_mouse_stick +const char *cfg_get(const char *key) { (void)key; return NULL; } +double cfg_double(const char *key) { (void)key; return 0; } +void rt_log(const char *fmt, ...) { (void)fmt; } + +#define FULL 32767 + +static int fails; +#define CHECK(cond, ...) do { if (!(cond)) { printf("FAIL: " __VA_ARGS__); putchar('\n'); fails++; } } while (0) + +int main(void) { + static const float cams[] = {0.1f, 0.5f, 1.0f, 1.5f, 2.0f, 3.0f}; + int32_t st[2], one[2]; + for (unsigned i = 0; i < sizeof cams / sizeof *cams; i++) { + float cam = cams[i]; + // a slow movement: the deflection is the one at camera speed 1 divided by the factor (while that is below full + // tilt), so the game's multiplication gives the same turn + for (int counts = 1; counts <= 40; counts += 3) { + input_mouse_stick(counts, 0, 1.0 / 60, 1, 1, one); + input_mouse_stick(counts, 0, 1.0 / 60, 1, cam, st); + double at_one = FULL * (counts * 60.0) / 3000.0; // full tilt at 3000 counts a second + double want = at_one / cam > FULL ? FULL : at_one / cam; + CHECK(fabs(one[0] - at_one) <= 0.5, "camera speed 1, %d counts: %d, expected %.1f", counts, one[0], at_one); + CHECK(fabs(st[0] - want) <= 0.5 && st[1] == 0, "camera speed %.1f, %d counts: %d, expected %.1f", cam, counts, + st[0], want); + } + // a fast flick reaches full tilt at every camera speed, in each direction + input_mouse_stick(5000, 0, 1.0 / 60, 1, cam, st); + CHECK(st[0] == FULL && st[1] == 0, "camera speed %.1f: a flick right gives %d %d", cam, st[0], st[1]); + input_mouse_stick(0, 5000, 1.0 / 60, 1, cam, st); + CHECK(st[0] == 0 && st[1] == -FULL, "camera speed %.1f: a flick down gives %d %d", cam, st[0], st[1]); + input_mouse_stick(-5000, -5000, 1.0 / 60, 1, cam, st); + double len = sqrt((double)st[0] * st[0] + (double)st[1] * st[1]); + CHECK(st[0] < 0 && st[1] > 0 && st[0] == -st[1] && len <= FULL + 1.0 && len >= FULL - 2.0, + "camera speed %.1f: a diagonal flick gives %d %d", cam, st[0], st[1]); + } + // the case that was wrong: at camera speed 1.5 a flick stopped at two thirds of full tilt, the top speed of camera + // speed 1 + input_mouse_stick(80, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == FULL, "camera speed 1.5, 80 counts in 1/60 s: %d, expected full tilt", st[0]); + input_mouse_stick(30, 0, 1.0 / 60, 1, 1.5f, st); + CHECK(st[0] == 13107, "camera speed 1.5, 30 counts in 1/60 s: %d, expected 13107", st[0]); + // nothing to divide by: no deflection + input_mouse_stick(30, 0, 1.0 / 60, 1, 0, st); + CHECK(st[0] == 0 && st[1] == 0, "camera speed 0 gives %d %d", st[0], st[1]); + input_mouse_stick(30, 0, 1.0 / 60, 1, -1, st); + CHECK(st[0] == 0 && st[1] == 0, "a negative camera speed gives %d %d", st[0], st[1]); + if (!fails) puts("mouse stick: the camera speed divides the deflection before the limit to full tilt"); + return fails != 0; +} diff --git a/tests/mouse_stick_check.py b/tests/mouse_stick_check.py new file mode 100644 index 00000000..c5e3455d --- /dev/null +++ b/tests/mouse_stick_check.py @@ -0,0 +1,35 @@ +"""Build and run tests/mouse_stick_check.c with runtime/input_map.c: the mouse camera's stick deflection at each camera +speed. No game files. + +Usage: python tests/mouse_stick_check.py [--cc ] +""" +import argparse +import os +import subprocess +import sys +import tempfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def main(): + ap = argparse.ArgumentParser(description=__doc__) + ap.add_argument('--cc', help='the C compiler (default: MSYS2 UCRT64 clang on Windows, clang elsewhere)') + args = ap.parse_args() + win = os.name == 'nt' + cc = args.cc or (str(MSYS_BIN / 'clang.exe') if win and MSYS_BIN.exists() else 'clang') + env = dict(os.environ) + if win and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + with tempfile.TemporaryDirectory(prefix='nx2navi-mouse-stick-') as tmp: + exe = Path(tmp) / ('check.exe' if win else 'check') + subprocess.check_call([cc, '-O2', '-Wall', '-fno-strict-aliasing', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'mouse_stick_check.c'), str(ROOT / 'runtime' / 'input_map.c'), + *([] if win else ['-lm']), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main()) From 541c4ff84c3732ddc11d77424b4b7bdad4004832 Mon Sep 17 00:00:00 2001 From: Michael Schienbein <7663497+mooki000@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:49:10 -0500 Subject: [PATCH 015/153] Start the game again after Restart on every processor it had (Linux). On Linux each Restart from the menu cost the next game one more processor core on machines with 12 or more logical processors. A process started with posix_spawn begins with the mask of the thread that spawned it, and since the recording thread has a core of its own no thread has every processor: the restarted game began without that core and took the next fast one for its own recording thread. plat_restart (runtime/platform/linux.c) now widens the calling thread's mask to all of the process's processors for the time of the spawn and puts it back. A mask set from outside is kept, helper programs started with plat_spawn are unchanged, and Windows is not touched. From pull request #435, with its test tests/platform_restart_mask_test.c, which needs Linux. Not compiled or run here: this machine has no Linux compiler. Co-Authored-By: Claude Fable 5.2 --- runtime/platform/linux.c | 19 +++++++ tests/platform_restart_mask_test.c | 82 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 tests/platform_restart_mask_test.c diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 4851de73..66b7cb2d 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -2373,6 +2373,22 @@ static void spawn_close_from(posix_spawn_file_actions_t *fa, int from) { if (posix_spawn_file_actions_addclosefrom_np) posix_spawn_file_actions_addclosefrom_np(fa, from); } +// The calling thread's mask with every processor this process uses, for the time of a posix_spawn: a child starts +// with the mask of the thread that spawned it, and after plat_cpu_reserve no thread has them all (the recording thread +// has its core alone, every other thread lacks that core). The game plat_restart starts would begin without the core, +// take the next fast one for its own recording thread, and so have one core fewer after every Restart. +// 1 when the mask was widened: *saved is then the mask to put back. +static int spawn_mask_whole(cpu_set_t *saved) { + cpu_set_t whole; + plat_mutex_lock(&cpu_lock); + int taken = cpu_taker != 0; + CPU_OR(&whole, &cpu_taken, &cpu_others); + plat_mutex_unlock(&cpu_lock); + if (!taken || sched_getaffinity(0, sizeof *saved, saved)) return 0; + CPU_OR(&whole, &whole, saved); + return !CPU_EQUAL(&whole, saved) && !sched_setaffinity(0, sizeof whole, &whole); +} + int plat_restart(void) { // the arguments, NUL-separated, from /proc/self/cmdline static char args[16384]; @@ -2399,8 +2415,11 @@ int plat_restart(void) { posix_spawnattr_setsigdefault(&at, &all); posix_spawnattr_setflags(&at, POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF); setenv(RESTART_WAIT, wait, 1); + cpu_set_t mask; + int whole = spawn_mask_whole(&mask); pid_t pid; int e = posix_spawn(&pid, exe, &fa, &at, argv, environ); + if (whole) sched_setaffinity(0, sizeof mask, &mask); unsetenv(RESTART_WAIT); posix_spawnattr_destroy(&at); posix_spawn_file_actions_destroy(&fa); diff --git a/tests/platform_restart_mask_test.c b/tests/platform_restart_mask_test.c new file mode 100644 index 00000000..d6678689 --- /dev/null +++ b/tests/platform_restart_mask_test.c @@ -0,0 +1,82 @@ +// plat_restart on Linux (runtime/platform/linux.c): the game it starts may run on every processor this one could +// before plat_cpu_reserve took a core for the recording thread, whichever thread asks for the restart. No game files. +// Build and run from the repository root: +// clang -O2 -ffunction-sections -fdata-sections -Iruntime -c runtime/platform/linux.c -o /tmp/plat.o +// clang -O2 -Wall -Iruntime tests/platform_restart_mask_test.c /tmp/plat.o -Wl,--gc-sections -lpthread -ldl -o /tmp/restart_mask +// /tmp/restart_mask (exit 77: this machine has no core plat_cpu_reserve would take) +// The test starts itself again through plat_restart; that copy only writes how many processors it may run on. +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include "platform/platform.h" + +#define OUT "RESTART_MASK_TEST_OUT" + +static int allowed(void) { + cpu_set_t set; + return sched_getaffinity(0, sizeof set, &set) ? -1 : CPU_COUNT(&set); +} + +// What the copy plat_restart started wrote, waiting up to 5 s for it; -1 when it never came. +static int restarted_count(const char *path) { + for (int i = 0; i < 500; i++) { + FILE *f = fopen(path, "r"); + int n = -1; + if (f) { + int got = fscanf(f, "%d", &n); + fclose(f); + if (got == 1) { + remove(path); + return n; + } + } + usleep(10000); + } + return -1; +} + +static atomic_int go, done; + +// A thread that existed before the core was taken, as the game's main thread does: it asks for the restart. +static void *other_thread(void *arg) { + (void)arg; + while (!atomic_load(&go)) usleep(1000); + plat_restart(); + atomic_store(&done, 1); + return NULL; +} + +int main(void) { + const char *out = getenv(OUT); + if (getenv("NX2NAVI_RESTART_WAIT") && out) { // the copy plat_restart started + FILE *f = fopen(out, "w"); + if (f) fprintf(f, "%d\n", allowed()), fclose(f); + return 0; + } + char path[64]; + snprintf(path, sizeof path, "/tmp/restart-mask-%d.txt", (int)getpid()); + setenv(OUT, path, 1); + remove(path); + int before = allowed(), fails = 0; + pthread_t th; + pthread_create(&th, NULL, other_thread, NULL); + char which[128]; + int core = plat_cpu_reserve(which, sizeof which); + if (!core) { puts("SKIP: no core to reserve on this machine"); return 77; } + atomic_store(&go, 1); + while (!atomic_load(&done)) usleep(1000); + pthread_join(th, NULL); + int from_other = restarted_count(path); + plat_restart(); // and from the thread that holds the core + int from_holder = restarted_count(path); + printf("%d processors before; the core taken: %s; a restart from another thread starts with %d, from the holder with %d\n", + before, which, from_other, from_holder); + if (from_other != before) puts("FAIL: the restarted process lacks processors (asked by another thread)"), fails++; + if (from_holder != before) puts("FAIL: the restarted process lacks processors (asked by the holder)"), fails++; + if (!fails) puts("restart mask: the restarted process has every processor"); + return fails != 0; +} From 5374d04d0f1913ee3355a3239285260b096101be Mon Sep 17 00:00:00 2001 From: Chris Toph Date: Tue, 10 Nov 2026 12:50:56 -0500 Subject: [PATCH 016/153] Keep an optional history of GPU address ranges, so the report of a lost graphics device can name what held the faulting address. After a lost device the driver's fault report gives an address and nothing says what was there. With va_report = 1 (NX2NAVI_VA_REPORT, off by default) the game keeps every address range the driver binds to a buffer or image and every range it unbinds (VK_EXT_device_address_binding_report, runtime/vk_vaddr.c). The report of a lost device then names the ranges that covered each faulting page, or the nearest ones when none did, and vaddr-history.txt beside the log holds the whole history, also when the driver gives no fault report. With 2 the call stack behind each range of 1 MiB or more is kept too. A player sees nothing of this unless the setting is on: without it no extension is enabled and no callback runs. From pull request #349. Its second part, which stops a frame dump from turning on mesh query statistics, is left out: it changes what a dump writes for everyone who uses one. Built for Windows; no device was lost to try it. Co-Authored-By: Claude Fable 5.2 --- docs/building.md | 7 +++++++ nx2navi.ini.example | 6 ++++++ runtime/cfg.c | 5 +++-- runtime/platform/linux.c | 16 ++++++++++++++++ runtime/platform/platform.h | 2 ++ runtime/platform/win32.c | 7 +++++++ runtime/vk.h | 9 +++++++++ runtime/vk_device.c | 21 ++++++++++++++++++--- runtime/vk_headroom.c | 1 + 9 files changed, 69 insertions(+), 5 deletions(-) diff --git a/docs/building.md b/docs/building.md index da13975a..0d7f0c89 100644 --- a/docs/building.md +++ b/docs/building.md @@ -278,6 +278,13 @@ Before a commit: `git config core.hooksPath .githooks` runs it before each local commit. - `build\\nx2navi.exe --selftest`: layout, dispatch table and host-function registry, without game files. +GPU page faults: `NX2NAVI_VA_REPORT=1` keeps the driver's GPU address bind/unbind reports +(VK_EXT_device_address_binding_report, `runtime/vk_vaddr.c`); `=2` also keeps the host call stack of ranges of 1 MiB +or more. After a lost device, the log names the reports whose range covered each faulting page, or the nearest ranges +when none did, and `vaddr-history.txt` beside the log holds the whole history. Stack frames are module offsets: add +the executable's load address (the link map's base) before looking them up in `ocarina.map`. Each bind and unbind +costs a callback, and `=2`'s stack walks enough to hide a timing-dependent fault. + A change to the in-game menu's look (`runtime/ui/menu_skin.cpp`, `option_rows.cpp`, `perf_panel.cpp`) can be looked at without game files: `python tools/menu_preview.py` builds the menu's own drawing code with the host's clang++ and writes the Screen tab, the full performance overlay and the boot screen to `build/menu-preview/` (`menu.png`, diff --git a/nx2navi.ini.example b/nx2navi.ini.example index eb75b735..87c50f13 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -621,6 +621,12 @@ # 1: samples each new ASTC texture's blocks on the CPU and logs how many are opaque ("ASTC alpha probe" lines), which # could be stored at half the video memory. Slows area loads. Default: 0. # astc_bc_probe = 0 +# GPU address history for the report of a lost graphics device, on drivers with +# VK_EXT_device_address_binding_report. 1: the game keeps every address range the driver binds to a buffer or image and +# every range it unbinds; after a lost device the log names what held each faulting address, and vaddr-history.txt +# beside the log holds the whole history. 2: also the call stack that made each range of 1 MiB or more, which slows +# area loads enough to hide a fault that depends on timing. Default: 0. +# va_report = 0 # Flight recorder: keeps the last frames and saves them to a flight folder next to the folder of nx2navi.exe when a frame # takes longer than flight_spike_ms (0 to 60000). # flight = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index 2605166f..e4fcaf27 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -68,7 +68,7 @@ static const char *const known[] = { "ZSTD_CHECK", "ZSTD_NATIVE", "BIND_REUSE", "DYN_DELTA", "STG_ARENA", "SUBMIT_BATCH", "ASTC_GPU_DESWIZZLE", "ASTC_SCRATCH_KEEP", "ASTC_ARENA", "TEXUP_LOG_MS", "DYN_RES", "DYN_RES_TARGET", "DYN_RES_STEP", "TEXTURE_DETAIL", - "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", + "RECORDER_CORE", "RECORDER_PRIO", "REPLAY_TIME", "LOCK_TIME", "POOL_CREATE_BUDGET", "POOL_CREATE_MS", "POOL_LOAD_BACKLOG", "POOL_LOAD_MS", "VA_REPORT", "VMA_BLOCK_MB", "ASTC_BC_PROBE", "SYNC_WAKE_ALL", "LOG_ASYNC", }; @@ -125,7 +125,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_NAN_PRESERVE", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, - {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, + {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; @@ -268,6 +268,7 @@ static const CfgMeta metas[] = { {"VMA_BLOCK_MB", "64", "16..256", RESTART}, // vk_headroom.c vk_vma_block_size {"ASTC_BC_PROBE", "0", "0|1", RESTART}, // vk_texture.c astc_probe_alpha {"PASS_CRUMBS", "", "0|1", RESTART}, // vk_crumbs.c pass marks; unset: VK_AMD_buffer_marker only + {"VA_REPORT", "0", "0|1|2", RESTART}, // vk_vaddr.c GPU address history for a lost device's report; 2: call stacks {"INPUT_RECORD", "", NULL, RESTART}, }; diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 66b7cb2d..ccb6eac0 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -1567,6 +1567,22 @@ int plat_module_of(uintptr_t addr, char *name, size_t cap, uintptr_t *base) { return snprintf(name, cap, "%s", n ? n + 1 : d.dli_fname) < (int)cap; } +typedef struct { uintptr_t *pc; int n, max; } StackWalk; + +static _Unwind_Reason_Code stack_step(struct _Unwind_Context *ctx, void *p) { + StackWalk *w = p; + uintptr_t ip = _Unwind_GetIP(ctx); + if (!ip || w->n == w->max) return _URC_END_OF_STACK; + w->pc[w->n++] = ip; + return _URC_NO_REASON; +} + +int plat_stack(uintptr_t *pc, int max) { + StackWalk w = {pc, 0, max}; + _Unwind_Backtrace(stack_step, &w); + return w.n; +} + int plat_exe_path(char *out, size_t cap) { ssize_t n = readlink("/proc/self/exe", out, cap - 1); if (n <= 0 || (size_t)n >= cap - 1) return 0; diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index df8bad8b..81c7f741 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -416,6 +416,8 @@ uintptr_t plat_exe_base(void); // where the executabl // The module (executable or library) holding code address addr: its file name without directories into name, its load // address into *base. 0 when none. int plat_module_of(uintptr_t addr, char *name, size_t cap, uintptr_t *base); +// Return addresses of the calling thread's stack into pc, innermost first, at most max: their count. +int plat_stack(uintptr_t *pc, int max); #define PLAT_PATH_MAX 1024 // path buffers of the runtime #ifdef _WIN32 #define PLAT_EXE_SUFFIX ".exe" // of the programs the runtime starts (nx2navi_xlate) diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 7cbfa47c..9f8138d1 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -1562,6 +1562,13 @@ int plat_module_of(uintptr_t addr, char *name, size_t cap, uintptr_t *base) { return WideCharToMultiByte(CP_UTF8, 0, n, -1, name, (int)cap, NULL, NULL) != 0; } +int plat_stack(uintptr_t *pc, int max) { + void *f[62]; + USHORT n = RtlCaptureStackBackTrace(1, max < 62 ? (DWORD)max : 62, f, NULL); + for (USHORT i = 0; i < n; i++) pc[i] = (uintptr_t)f[i]; + return n; +} + int plat_exe_path(char *out, size_t cap) { wchar_t w[WPATH]; DWORD n = GetModuleFileNameW(NULL, w, WPATH); diff --git a/runtime/vk.h b/runtime/vk.h index 8b3ca0db..55147b61 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -82,6 +82,15 @@ int vk_selftest(void); // 0 on success // process, after the GPU breadcrumbs (vk_crumbs_report). Any other result returns at once. void vk_device_fault_report(VkResult r); +// ---- GPU address history (vk_vaddr.c, NX2NAVI_VA_REPORT=1, 2 with call stacks): VK_EXT_device_address_binding_report's +// reports, kept so vk_device_fault_report can name the objects that held a faulting address. +int vk_vaddr_wanted(void); +void vk_vaddr_start(VkInstance instance); // create_instance, after vkCreateInstance +void vk_vaddr_device(int supported); // create_device: the driver reports address bindings +void vk_vaddr_fault(uint64_t addr, uint64_t precision); // logs the reports whose range covered addr's page +void vk_vaddr_dump(void); // the whole history to vaddr-history.txt beside the log +void vk_vaddr_stop(VkInstance instance); // before vkDestroyInstance + // ---- GPU breadcrumbs (vk_crumbs.c): with VK_AMD_buffer_marker, each crumb has the GPU write its code into host memory // twice, when the command processor reaches it and when all work before it has finished. After a lost device the two // codes bracket the work that hung. Without the extension every call returns at once. diff --git a/runtime/vk_device.c b/runtime/vk_device.c index c72af9a8..485fcaef 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -141,8 +141,9 @@ static int create_instance(void) { rt_log("vk: NX2NAVI_VK_VALIDATION set but %s is not installed; continuing without", layers[0]); validate = 0; } - // DLSS Frame Generation: Streamline's plugins call its naming functions even when they say it is off - if (validate || (vk_dlssg_wanted() && instance_ext_present(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))) + // DLSS Frame Generation: Streamline's plugins call its naming functions even when they say it is off; the GPU + // address history (vk_vaddr.c) gets the driver's binding reports through a messenger + if (validate || ((vk_dlssg_wanted() || vk_vaddr_wanted()) && instance_ext_present(VK_EXT_DEBUG_UTILS_EXTENSION_NAME))) ext[n_ext++] = VK_EXT_DEBUG_UTILS_EXTENSION_NAME; // armed, not wanted: the menu switches HDR on during the run (vk_hdr.c), and the extensions are fixed here if (vk_hdr_armed() && instance_ext_present(VK_EXT_SWAPCHAIN_COLOR_SPACE_EXTENSION_NAME)) // HDR10 surface formats @@ -168,6 +169,7 @@ static int create_instance(void) { .pfnUserCallback = debug_cb}; if (f) f(g_vk.instance, &mi, NULL, &g_vk.messenger); } + vk_vaddr_start(g_vk.instance); return 1; } @@ -299,6 +301,7 @@ typedef struct { VkPhysicalDeviceShaderMaximalReconvergenceFeaturesKHR mrc; VkPhysicalDeviceMaintenance5FeaturesKHR m5; VkPhysicalDevicePipelineBinaryFeaturesKHR pbin; + VkPhysicalDeviceAddressBindingReportFeaturesEXT abr; } FeatureChain; static int create_device(void) { @@ -362,6 +365,12 @@ static int create_device(void) { CHAIN(fbl, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ATTACHMENT_FEEDBACK_LOOP_LAYOUT_FEATURES_EXT); CHAIN(lr, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DYNAMIC_RENDERING_LOCAL_READ_FEATURES_KHR); } + // The GPU address history for a lost device's report (vk_vaddr.c, NX2NAVI_VA_REPORT=1) + int want_va = vk_vaddr_wanted() && has_ext(ep, ne, VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME); + if (want_va) { + ext[n_ext++] = VK_EXT_DEVICE_ADDRESS_BINDING_REPORT_EXTENSION_NAME; + CHAIN(abr, VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ADDRESS_BINDING_REPORT_FEATURES_EXT); + } vk_dlaa_device_exts(ep, ne, ext, &n_ext, 40); // NGX's; the eight slots after it stay for the eight below // FSR 3.1 (vk_ffx.cpp): its Vulkan back end looks vkGetBufferMemoryRequirements2KHR up by that name, which resolves // only with the extension enabled (the core function has no suffix); without it the back end called address 0. @@ -431,6 +440,7 @@ static int create_device(void) { else if (!h->textureCompressionBC) miss = "textureCompressionBC"; if (miss) { rt_log("vk: required feature missing on %s: %s", g_vk.props.deviceName, miss); free(c); return 0; } if (!c->fault.deviceFault) g_vk.has_device_fault = 0; + if (vk_vaddr_wanted()) vk_vaddr_device(want_va && c->abr.reportAddressBinding); if (g_vk.has_vrs) { // the rate is set per draw; no rate attachment and no per-primitive rates c->vrs.attachmentFragmentShadingRate = c->vrs.primitiveFragmentShadingRate = VK_FALSE; if (!c->vrs.pipelineFragmentShadingRate) g_vk.has_vrs = 0; @@ -537,6 +547,7 @@ static void destroy_all(void) { (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(g_vk.instance, "vkDestroyDebugUtilsMessengerEXT"); if (f) f(g_vk.instance, g_vk.messenger, NULL); } + vk_vaddr_stop(g_vk.instance); if (g_vk.instance) vkDestroyInstance(g_vk.instance, NULL); g_errors_total += g_vk.validation_errors; memset(&g_vk, 0, sizeof g_vk); @@ -596,6 +607,7 @@ void vk_device_fault_report(VkResult r) { if (!get) { rt_log("vk: device lost; no fault report (VK_EXT_device_fault %s)", c && c->has_device_fault ? "has no entry point" : "not enabled"); + vk_vaddr_dump(); // the history still names what held a page another report (the kernel's log) gives plat_mutex_unlock(&lock); return; } @@ -613,13 +625,16 @@ void vk_device_fault_report(VkResult r) { rt_log("vk: device fault: \"%s\" (%u address infos, %u vendor infos, vendor binary %llu bytes)", info.description, n.addressInfoCount, n.vendorInfoCount, (unsigned long long)binary); // The faulting access lies in the precision-aligned range around the reported address. - for (uint32_t i = 0; i < n.addressInfoCount; i++) + for (uint32_t i = 0; i < n.addressInfoCount; i++) { rt_log("vk: device fault address %u: %s at %#llx, precision %#llx", i, fault_address_type(ai[i].addressType), (unsigned long long)ai[i].reportedAddress, (unsigned long long)ai[i].addressPrecision); + vk_vaddr_fault(ai[i].reportedAddress, ai[i].addressPrecision); + } for (uint32_t i = 0; i < n.vendorInfoCount; i++) rt_log("vk: device fault vendor info %u: \"%s\" code %#llx data %#llx", i, vi[i].description, (unsigned long long)vi[i].vendorFaultCode, (unsigned long long)vi[i].vendorFaultData); } + vk_vaddr_dump(); free(ai); free(vi); plat_mutex_unlock(&lock); diff --git a/runtime/vk_headroom.c b/runtime/vk_headroom.c index 481e8eed..811d7cfd 100644 --- a/runtime/vk_headroom.c +++ b/runtime/vk_headroom.c @@ -157,6 +157,7 @@ static void top_up(const VkCtx *c, uint32_t type) { static void headroom_main(void *arg) { (void)arg; __atomic_store_n(&H.tid, plat_thread_id(), __ATOMIC_RELEASE); + if (vk_vaddr_wanted()) rt_log("vk: headroom: thread %u", H.tid); // names this thread in the address history (vk_vaddr.c) plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); uint64_t watch = 0; // memory types seen so far for (;;) { From 0a502ecf65991aba43b35d7401505a0042b30cf0 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 13:10:34 -0500 Subject: [PATCH 017/153] Let the pick-up window's number count at least one a frame, so the window closes above 60 fps. The window that shows a pick-up above the character counts its number up to the new total by a whole step a frame and closes once they are equal. The game rounds that step down, and below one 60 Hz frame per update it was 0: above 60 fps the window stayed until the area was left, and for larger amounts the counting sound was started again every frame. The step is now floored at 1, switchable with pickup_count. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 4 ++++ recomp/patches-v111.txt | 11 +++++++++++ runtime/cfg.c | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index a60402ea..7fe8d4e6 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -40,6 +40,10 @@ # in a frame shorter than 1/240 s, so things standing on moving platforms were left behind. 1 rounds the step count # up instead; 0 is the game's own count. # physics_steps = 1 +# The number in the window that shows a pick-up (a rupee) counts up by whole steps a frame, and above 60 fps the game +# rounded that step down to nothing: the window stayed until the area was left and its counting sound went on. 1 lets +# it count at least one a frame; 0 is the game's own count. +# pickup_count = 1 # The step the game is given each frame: 1 keeps it steady (the same value from frame to frame while the frame rate # holds, exactly one 60 Hz frame at a 60 fps limit) and makes up for a late frame over the next second; 0 is the mean # of the last five frame times, which changes with every frame. diff --git a/recomp/patches-v111.txt b/recomp/patches-v111.txt index 98b54a1a..2292999c 100644 --- a/recomp/patches-v111.txt +++ b/recomp/patches-v111.txt @@ -19,6 +19,7 @@ set res_fixed NX2NAVI_PATCH_RES_FIXED 0 set ostep NX2NAVI_OCARINA_REALTIME 1 set zstd NX2NAVI_ZSTD_NATIVE 1 set osolver NX2NAVI_PHYSICS_STEPS 1 +set opickup NX2NAVI_PICKUP_COUNT 1 # Real-time step: the framework's per-frame update (main+0x6e6be0) gives the time manager **[main+0x4c7e890] its # nominal frame through main+0x3b4e300 (+0x10 frames per update, +0x14 the inverse, +0x18 seconds, +0x1c the rate), @@ -48,6 +49,16 @@ site osolver main 38afd10 bd440d02 1e2a1002 # the same three words in the seco site osolver main 38afd1c 1f010800 1f218800 site osolver main 38afd20 1e380001 1e280001 +# Pick-up window count: the window that shows a pick-up above the character (its update is main+0xa67e50) moves the +# number it shows (+0x360) to the new total (+0x35c) by trunc(frames per update * max(1, difference / 50)) a frame, and +# closes only once the two are equal. Below one frame per update that is 0: above 60 fps, and at 60 while the step runs +# a little under one frame, the count never ends, so the window stayed until the area was left, and for an amount over +# 9 the counting sound was started again every frame and its closing sound never came. The increment is floored at 1 +# without the flags (clz is 32 only for 0, so increment += clz >> 5); at one and two frames per update it is what the +# game computes. Above 60 fps a small amount then counts one a rendered frame, faster than at 60. w13 held the time +# manager, last read at main+0xa682ec, and every path from the site reaches a call before it reads x13 again. +site opickup main a68300 1e38000c 1e38000c 5ac0118d 53057dad 0b0d018c # fcvtzs w12, s0; clz w13, w12; lsr w13, w13, #5; add w12, w12, w13 + # Game image: the graphics setup (main+0x1202ce0) fills the renderer's configuration with the size of the picture the # game presents, 2560x1440 at every console mode (config +0x14, copied to renderer +0x9c, which main+0x346be50 reads # for the main render target set, main+0xf49040), and the display settings object (main+0x31f3890) stores the same size diff --git a/runtime/cfg.c b/runtime/cfg.c index 2605166f..557cdb95 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -31,7 +31,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. static const char *const known[] = { - "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) + "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", @@ -76,7 +76,7 @@ static const char *const known[] = { static const struct { const char *key, *section; } sections[] = { {"FPS_LIMIT", "frame rate"}, {"FPS60", "frame rate"}, {"RTSS", "frame rate"}, {"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"}, - {"PHYSICS_STEPS", "frame rate"}, {"STEP_STEADY", "frame rate"}, + {"PHYSICS_STEPS", "frame rate"}, {"PICKUP_COUNT", "frame rate"}, {"STEP_STEADY", "frame rate"}, {"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"}, {"GPU_INDEX", "display"}, {"MONITOR", "display"}, {"OUTPUT_RES", "display"}, {"GAME_ASPECT", "display"}, {"GAME_RES", "display"}, {"RENDER_SCALE", "display"}, {"UPSCALER", "display"}, {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"}, From df0db643ed2a18a8a93746fc4702d9b76b9ee902 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 13:19:51 -0500 Subject: [PATCH 018/153] Add a test switch that lets a new depth image take the live one's depth. A contributor traced the one-frame dark shadowing at the castle town gate to depth targets the game remakes at other sizes about a thousand times a minute: the new image started cleared, so everything near the camera read as in shadow for a frame. With depth_alias = 1 a depth-only image of the same format copies the live one's depth for the region both cover. Their three runs each way showed 6, 4 and 0 dark frames without it and none with it; the cost of the copies and other scenes are not measured yet, so it is off by default. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 +++++ runtime/cfg.c | 6 +++--- runtime/vk_texture.c | 46 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 50 insertions(+), 7 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 7fe8d4e6..d512b7e2 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -188,6 +188,11 @@ # memory = auto # A new texture created over a live texture's memory starts with that content instead of empty. # tex_alias = 1 +# Test, off by default: a new depth image made over a live one of the same format at the same address takes its depth +# for the region both cover, instead of starting cleared. The game remakes its depth targets at other sizes many times a +# minute in some places, and a cleared one shows everything near the camera as in shadow for one frame. ab switches it +# in blocks of game time for comparisons; depth_alias_log = 1 logs each such image with its frame. +# depth_alias = 0 # Small texture uploads stage in ranges of a few kept buffers instead of a new buffer each (0 = a buffer each). # stg_arena = 1 # With astc = gpu: the compute shader reads the blocks in the game's tiled layout, so the CPU only copies the diff --git a/runtime/cfg.c b/runtime/cfg.c index 557cdb95..e6d341c0 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -62,7 +62,7 @@ static const char *const known[] = { "SVC_SLOW_MS", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_IDLE", "SPLIT_IN_PASS", "SPLIT_US", "SPV_MARK_TEST", "SPV_OVERRIDE", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", - "SET_AT", "SWKBD_TEST", "TAA", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_DISUSE", "TEX_GEN_UNDO", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_REUSE", "TEX_COMMIT_FREE_MB", "TEX_PROC_COMMIT_MB", "TEX_EVICT", "TEX_COMMIT_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "EVO_TRACE", "EVENT_AT_SYNC", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NAN_PRESERVE", "VK_NOOPT", "VK_NO_FLOAT64", + "SET_AT", "SWKBD_TEST", "TAA", "TEST_PAD", "TEST_PAD_AT", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "DEPTH_ALIAS", "DEPTH_ALIAS_LOG", "TEX_DISUSE", "TEX_GEN_UNDO", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TEX_RELEASED_MB", "TEX_REUSE", "TEX_COMMIT_FREE_MB", "TEX_PROC_COMMIT_MB", "TEX_EVICT", "TEX_COMMIT_EVICT", "MIRROR_EVICT", "VRAM_BUDGET_MB", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "EVO_TRACE", "EVENT_AT_SYNC", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NAN_PRESERVE", "VK_NOOPT", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECONVERGE", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WB_NO_REUP", "WB_MERGE_MAX", "WB_PROBE", "WB_SPLIT", "WB_LAZY", "WB_LAZY_TOUCH", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", @@ -118,7 +118,7 @@ static const struct { const char *key, *section; } sections[] = { {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"PIPE_PREBUILD", "performance"}, {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, - {"TEX_ALIAS", "performance"}, {"TEX_DISUSE", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, + {"TEX_ALIAS", "performance"}, {"DEPTH_ALIAS", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, {"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_IDLE", "performance"}, {"SPLIT_IN_PASS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, @@ -245,7 +245,7 @@ static const CfgMeta metas[] = { {"SHADER_CARRY", "1", "0|1", RESTART}, // gfx_shader.c "Carry": a previous translator's translations until translated again // textures and memory {"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART}, - {"TEX_ALIAS", "1", "0|1", RESTART}, {"TEX_DISUSE", "1", "0|1|2", RESTART}, {"TEX_GEN_UNDO", "1", "0|1", RESTART}, {"TEX_REUSE", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "80", "0..100", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART}, + {"TEX_ALIAS", "1", "0|1", RESTART}, {"DEPTH_ALIAS", "0", "0|1|ab", RESTART}, {"DEPTH_ALIAS_LOG", "0", "0|1", RESTART}, {"TEX_DISUSE", "1", "0|1|2", RESTART}, {"TEX_GEN_UNDO", "1", "0|1", RESTART}, {"TEX_REUSE", "1", "0|1", RESTART}, {"TEX_VRAM_PRESSURE", "80", "0..100", RESTART}, {"ASTC_BC", "", "0|1", RESTART}, {"ZSTD_NATIVE", "1", "0|1", RESTART}, {"ZSTD_CHECK", "0", "0|1", RESTART}, {"STG_ARENA", "1", "0|1", RESTART}, {"ASTC_GPU_DESWIZZLE", "1", "0|1", RESTART}, {"ASTC_SCRATCH_KEEP", "0", "0|1", RESTART}, {"ASTC_ARENA", "0", "0|1", RESTART}, // vk_astc.c scratch_get, vk_texture.c astc_arena // render fixes diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 8a7dfbfd..261c3fde 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -2178,9 +2178,30 @@ static int img_block(const Tex *e, int *bw, int *bh) { // on screen, so a descriptor made over a live target's memory is often a few texels larger or smaller than it. The game // reads the bytes there as its content (the same memory on the console); started empty instead, the scene went black // for a frame about once a second while the camera turned, from 0.6.1 on (issue #253). +// NX2NAVI_DEPTH_ALIAS=1 (test, default 0): a depth-only image of the same format as the live one at its address takes +// its depth too (the region both cover), where it started cleared to 0 (init_image). NX2NAVI_DEPTH_ALIAS_LOG=1 logs +// every depth entry made over a live texture, with the frame, so a one-frame dark shadowing can be lined up with it. +static int depth_alias(void) { + static int on = -1; + if (on < 0) { + on = rt_knob("DEPTH_ALIAS", "0"); + rt_log("vk: depth aliases: %s (NX2NAVI_DEPTH_ALIAS)", on == 2 ? "a new depth image takes the live one's depth in the B blocks" + : on ? "a new depth image takes the live one's depth" + : "a new depth image starts cleared"); + } + return RT_KNOB_ON(on); +} + +static int depth_alias_log(void) { + static int on = -1; + if (on < 0) on = rt_knob("DEPTH_ALIAS_LOG", "0"); + return on; +} + static int alias_compatible(const Tex *e, const Tex *src, const char **why) { const VkTexImage *p = &e->pub, *q = &src->pub; - if (e->f.depth || e->f.stencil || src->f.depth || src->f.stencil || p->samples > 1 || q->samples > 1) + int dd = depth_alias() && e->f.depth && src->f.depth && !e->f.stencil && !src->f.stencil && e->f.vk == src->f.vk; + if (((e->f.depth || e->f.stencil || src->f.depth || src->f.stencil) && !dd) || p->samples > 1 || q->samples > 1) return *why = "depth, stencil or multisample", 0; if (p->type != q->type) return *why = "another image type", 0; if (p->skip || q->skip) return *why = "made without its top levels (texture detail)", 0; // level i is not level i @@ -2211,6 +2232,7 @@ static int alias_copy(Tex *e, const Tex *src) { VkCommandBuffer cb = vk_exec_inline_begin(&reopen); if (!cb) return 0; int bw, bh, bpb = img_block(e, &bw, &bh); + VkImageAspectFlags asp = e->f.depth ? VK_IMAGE_ASPECT_DEPTH_BIT : VK_IMAGE_ASPECT_COLOR_BIT; // depth_alias uint32_t levels = e->pub.levels < src->pub.levels ? e->pub.levels : src->pub.levels; uint32_t layers = e->pub.layers < src->pub.layers ? e->pub.layers : src->pub.layers, n = levels * layers, k = 0; // the region both images cover (alias_compatible: the same shape for block-compressed ones); the rest of a larger @@ -2225,7 +2247,7 @@ static int alias_copy(Tex *e, const Tex *src) { uint32_t w = umin(mip(e->pub.width, lv), mip(src->pub.width, lv)); uint32_t h = umin(mip(e->pub.height, lv), mip(src->pub.height, lv)); uint32_t z = umin(mip(e->pub.depth, lv), mip(src->pub.depth, lv)); - rg[k] = (VkBufferImageCopy){.bufferOffset = total, .imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, lv, ly, 1}, + rg[k] = (VkBufferImageCopy){.bufferOffset = total, .imageSubresource = {asp, lv, ly, 1}, .imageExtent = {w, h, z}}; total += ((VkDeviceSize)(w + bw - 1) / bw * ((h + bh - 1) / bh) * z * bpb + 15) & ~15ull; } @@ -2245,11 +2267,17 @@ static int alias_copy(Tex *e, const Tex *src) { .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED, .newLayout = VK_IMAGE_LAYOUT_GENERAL, .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .image = e->pub.image, - .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS}}; + .subresourceRange = {asp, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS}}; VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &ib}; vkCmdPipelineBarrier2(cb, &di); - if (partial && vk_fix_zero() && !e->bound_to && !e->pub.mirrored && !format_compressed(e->f.vk)) { + if (partial && e->f.depth) { // the rest of a larger depth image starts as init_image leaves a new one + VkClearDepthStencilValue dz = {0.0f, 0}; + VkImageSubresourceRange all = {asp, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS}; + vkCmdClearDepthStencilImage(cb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, &dz, 1, &all); + barrier(cb, VK_PIPELINE_STAGE_2_CLEAR_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, + VK_ACCESS_2_TRANSFER_WRITE_BIT); + } else if (partial && vk_fix_zero() && !e->bound_to && !e->pub.mirrored && !format_compressed(e->f.vk)) { VkClearColorValue zero = {0}; VkImageSubresourceRange all = {VK_IMAGE_ASPECT_COLOR_BIT, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS}; vkCmdClearColorImage(cb, e->pub.image, VK_IMAGE_LAYOUT_GENERAL, &zero, 1, &all); @@ -2331,6 +2359,16 @@ static void start_at(Tex *e, const TexKey *k, int taken_back) { int from_released = !src && rsrc && !live_content; if (from_released) src = rsrc; if (alias) perf_count(PERF_CNT_TEX_ALIAS); + if (alias && (e->f.depth || e->f.stencil) && depth_alias_log()) { // depth_alias + static int n_dlog; + if (n_dlog++ < 20000) + rt_log("vk: depth alias: frame %llu %#llx %ux%ux%u fmt %#x%s over %ux%ux%u fmt %#x%s: %s", + (unsigned long long)__atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED), (unsigned long long)k->addr, + e->pub.width, e->pub.height, e->pub.layers, e->t.d.format, taken_back ? " (taken back)" : "", + other->pub.width, other->pub.height, other->pub.layers, other->t.d.format, + other->f.depth ? " depth" : "", taken_back && !newer && !e->pub.mirrored ? "kept its own" + : !tex_alias ? "cleared (aliases off)" : src ? (from_released ? "copied from a released one" : "copied") : why); + } if (taken_back && !newer && !e->pub.mirrored) { // its image still holds its content n_alias_kept++; return; From ce206a9239d700ec01bbe625a6df7102d34a1e2b Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Tue, 10 Nov 2026 14:08:48 -0500 Subject: [PATCH 019/153] Order passes and dispatches by the game's own barriers on NVIDIA graphics cards, which takes about a tenth off the GPU's time for a frame. Every pass begin and every compute dispatch recorded a full barrier, which makes the GPU finish everything before each of them. With barrier_game = 1 (NX2NAVI_BARRIER_GAME) a pass after an earlier pass gets a barrier for its attachments only and a dispatch after other dispatches gets none; a change between drawing and compute keeps the full barrier, and so do the game's own barriers and the replay's copies, clears, fills and uniform updates. On an RTX 5090 at 2560x1440 the GPU's time for a frame fell from 5.4 to 4.9 ms standing in Kokiri Forest (two in-run A/B runs, the control's blocks differ by 0.7 %) and from 5.1 to 4.4 ms on a walk through it. The frame rate did not change there, because the replay thread is the limit on that machine; it should rise where the GPU is. The default is 1 on NVIDIA graphics cards only, and 0 on every other vendor's: the game placed its barriers for an NVIDIA GPU, Vulkan asks for more of them than the game recorded, and no AMD or Intel driver has run this. 34 screenshots across the A and B blocks of one run show the same picture on NVIDIA. NX2NAVI_TRACE=perf logs the barriers a frame by kind. From pull request #389, which had it on for every vendor; the default by vendor and the settings text are added here. The validation layer's synchronization check could not be run on this machine. Co-Authored-By: Claude Fable 5.2 --- nx2navi.ini.example | 5 ++ runtime/cfg.c | 3 +- runtime/vk_backend.c | 1 + runtime/vk_exec.c | 102 ++++++++++++++++++++++++++++++++++++++++- runtime/vk_exec.h | 9 ++++ runtime/vk_exec_draw.c | 4 +- 6 files changed, 119 insertions(+), 5 deletions(-) diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 87c50f13..10748499 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -705,6 +705,11 @@ # about a third more frames a second in open areas. 0: copied back at the end of every GPU submission, as in 0.7.0. # With shadow storage (Linux, Wine) it stays off. # wb_lazy = 1 +# Barriers between the game's draw passes and compute dispatches. 1: only where the game itself asks for one, or where +# the work changes between drawing and compute; this takes about a tenth off the graphics card's time for a frame. +# 0: a full barrier before every pass and every dispatch. Default: 1 on NVIDIA graphics cards, 0 on all others, where it +# has not been tried: set 1 there to try it, and back to 0 if the picture shows faults. +# barrier_game = 1 # Linux, shadow storage: 1 clears the write watch's protection of a range in one call before the GPU's bytes are copied # back into the game's memory, instead of taking a fault at the first store into each page. # shadow_bulk_write = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index e4fcaf27..115377e2 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -33,6 +33,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o static const char *const known[] = { "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS", + "BARRIER_GAME", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", @@ -124,7 +125,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index cbd08768..e3859a65 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -219,6 +219,7 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin if (!vram_probe) vram_probe = 1, perfmon_set_vram_game(vk_mem_device_used_mb); // the overlay's "game" beside VRAM vk_exec_frame_log(t, frame); dynres_frame(); // before vk_dlaa_frame reads the DLSS mode it may override + vk_exec_barrier_log(frame); vk_dlaa_frame(); vk_sampler_aniso_live(); // NX2NAVI_ANISO changed in the menu: the surface textures' samplers take the new level vk_mvsurvey_frame(); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 6ac73408..441f046b 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -585,8 +585,71 @@ static VkImageMemoryBarrier2 layout_barrier(VkImage img, VkImageAspectFlags aspe // Full barrier. The images in the feedback layout become want[] (pass begin) or none (every other barrier): the others // go back to GENERAL and the new ones leave it, in the same command. +// NX2NAVI_BARRIER_GAME (1; 0: every pass begin and dispatch a full barrier; ab: in the B blocks): the game's own +// barriers order its work, as on the Switch. A pass that +// samples what an earlier one drew, a dispatch that reads what another dispatch wrote, an indirect draw whose arguments +// a dispatch wrote: on the Switch each needs nvnCommandBufferBarrier (ORDER_* and INVALIDATE_* bits) in the game's +// command stream, which the replay keeps as a full barrier (OP_BARRIER). What the GPU orders by itself there is kept: a +// switch between graphics and compute work drains the one before (the 3D and compute classes share the engine). With +// every pass begin and every dispatch a full barrier, 2.3 ms of a 9.7 ms GPU frame in Kokiri Forest was barriers. +// With the switch: +// - a pass begin after an earlier pass and no dispatch since the last full barrier gets an attachment barrier +// (colour and depth/stencil output before the next pass's loads and writes: Vulkan orders attachment access only +// inside one pass instance), after neither nothing; +// - a dispatch after other dispatches only gets none; +// - a pass after a dispatch, and a dispatch after a pass, get a full barrier, the drain of the switch; +// - either gets a full barrier as before after one the replay itself asked for (vk_exec_barrier: its copies, +// clears, fills, uniform updates and stand-in copies call it before their commands, which sets bar_dirty), and +// when feedback-loop targets change layout. +// Copies, clears and the game's barriers keep their full barriers. +// +// The default is 1 on NVIDIA GPUs and 0 on every other. The game placed its barriers for an NVIDIA GPU, and what it +// left out is what such a GPU orders by itself; Vulkan asks for a barrier wherever a pass samples what an earlier pass +// drew or a dispatch reads what an earlier one wrote, whether the game recorded one or not. On an RTX 5090 the picture +// was the same with the switch on and off (34 screenshots across A/B blocks in Kokiri Forest) and the GPU time fell by +// 0.5 to 0.6 ms of 5.4 a frame; no other vendor's driver has run it, so there the setting has to be turned on by hand. +static int barrier_game(void) { + static int mode = -1; + if (mode < 0) { + const VkCtx *c = vk_ctx(); + int nvidia = c && c->props.vendorID == 0x10de; + mode = rt_knob("BARRIER_GAME", nvidia ? "1" : "0"); + if (mode) + rt_log("vk_exec: NX2NAVI_BARRIER_GAME=%s: passes and dispatches are ordered by the game's barriers", + mode == 2 ? "ab" : "1"); + } + return RT_KNOB_ON(mode); +} + +// NX2NAVI_TRACE=perf: barriers per frame by kind (vk_exec_barrier_log) +enum { BS_FULL, BS_GAME, BS_PASS_FULL, BS_PASS_ATT, BS_PASS_NONE, BS_DISP_FULL, BS_DISP_NONE, BS_N }; +static uint64_t bstat[BS_N]; +static const char *const bs_name[BS_N] = {"replay full", "game", "pass full", "pass attachment", "pass none", + "dispatch full", "dispatch none"}; +enum { BR_OTHER, BR_GAME, BR_PASS, BR_DISPATCH }; + +static void barrier_to_why(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant, int why); static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant) { + barrier_to_why(ex, want, wasp, nwant, BR_OTHER); +} + +static void attachment_barrier(VkExec *ex) { + const VkPipelineStageFlags2 st = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT | + VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT; + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = st, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, + .dstStageMask = st, + .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | + VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT | + VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(ex->cb, &di); + ex->recorded++; +} + +static void barrier_to_why(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant, int why) { vk_exec_pass_end(ex); + if (why == BR_OTHER) ex->bar_dirty = 1; // also when it falls together with the last one: its commands follow VkImageMemoryBarrier2 ib[18]; uint32_t n = 0; for (int i = 0; i < ex->nfbl; i++) @@ -599,6 +662,21 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags for (int i = 0; i < nwant; i++) ex->fbl_img[i] = want[i], ex->fbl_aspect[i] = wasp[i]; ex->nfbl = nwant; if (ex->barrier_at == ex->recorded && !n) return; + if (!n && !ex->bar_dirty && ((why == BR_PASS && !ex->bar_dispatches) || (why == BR_DISPATCH && !ex->bar_passes)) && + barrier_game()) { + if (why == BR_DISPATCH) { + bstat[BS_DISP_NONE]++; + } else if (ex->bar_passes) { + attachment_barrier(ex); + bstat[BS_PASS_ATT]++; + } else { + bstat[BS_PASS_NONE]++; + } + return; + } + bstat[why == BR_GAME ? BS_GAME : why == BR_PASS ? BS_PASS_FULL : why == BR_DISPATCH ? BS_DISP_FULL : BS_FULL]++; + if (why != BR_OTHER) ex->bar_dirty = 0; // after BR_OTHER the replay's own commands follow: the next pass or + ex->bar_passes = ex->bar_dispatches = 0; // dispatch orders after them vk_exec_frame.fb_layouts += n; VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, @@ -613,6 +691,25 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags void vk_exec_barrier(VkExec *ex) { barrier_to(ex, NULL, NULL, 0); } +void vk_exec_barrier_dispatch(VkExec *ex) { + barrier_to_why(ex, NULL, NULL, 0, BR_DISPATCH); + ex->bar_dispatches++; +} + +void vk_exec_barrier_log(uint64_t frame) { + static uint64_t last; + if (!(rt_trace & RT_TRACE_PERF) || frame % 600) return; + double f = frame > last ? (double)(frame - last) : 1.0; + last = frame; + char s[320]; + int o = 0; + for (int k = 0; k < BS_N; k++) { + o += snprintf(s + o, sizeof s - o, "%s%s %.1f", k ? ", " : "", bs_name[k], bstat[k] / f); + bstat[k] = 0; + } + rt_log("vk_exec: frame %llu: barriers per frame: %s", (unsigned long long)frame, s); +} + void vk_exec_pass_begin(VkExec *ex) { if (ex->in_pass) return; resolve_rt(ex); @@ -630,7 +727,8 @@ void vk_exec_pass_begin(VkExec *ex) { if (has_image(want, nwant, t->image)) continue; want[nwant] = t->image, wasp[nwant++] = i < 8 ? VK_IMAGE_ASPECT_COLOR_BIT : t->img->aspect; } - barrier_to(ex, want, wasp, nwant); // passes, clears, copies and dispatches see each other's results + barrier_to_why(ex, want, wasp, nwant, BR_PASS); // passes, clears, copies and dispatches see each other's results + ex->bar_passes++; ex->fbl_mask = fbl; ex->fb_dirty = 0; ex->fb_read = 0; @@ -1390,7 +1488,7 @@ static const op_hdr *walk(VkExec *ex, const op_hdr *h, int depth) { case OP_COPY_STRIDE: ex->copy_stride = ((const p_copy_stride *)a)->stride; break; case OP_SET_TEX_POOL: ex->tex_pool = ((const p_set_pool *)a)->pool, ex->tex_serial++; break; case OP_SET_SAMP_POOL: ex->samp_pool = ((const p_set_pool *)a)->pool; break; - case OP_BARRIER: vk_exec_barrier(ex); break; + case OP_BARRIER: barrier_to_why(ex, NULL, NULL, 0, BR_GAME); break; case OP_DRAW: case OP_DRAW_IDX: case OP_DISPATCH: case OP_UPDATE_UB: case OP_DRAW_IDX_IND: case OP_DISPATCH_IND: if (h->op == OP_DRAW || h->op == OP_DRAW_IDX || h->op == OP_DRAW_IDX_IND) vk_exec_frame.draws_seen++, split_draws++; if (h->op == OP_DISPATCH || h->op == OP_DISPATCH_IND) vk_exec_frame.dispatches++, split_draws++; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 6ce192c6..baf35d82 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -103,6 +103,10 @@ typedef struct VkExec { void *views; // image views created for this command buffer (freed at its fence) int nviews, cap_views; int barrier_at; // recorded right after the last full barrier (-1: none yet) + // NX2NAVI_BARRIER_GAME (vk_exec.c barrier_to_why): the last full barrier was one the replay itself needed (a copy, + // clear or fill of its own follows it), so the next pass or dispatch gets a full one too; passes and dispatches + // begun since the last full barrier + int bar_dirty, bar_passes, bar_dispatches; // Feedback loops (vk.h VkCtx.feedback, vk_exec_draw.c feedback_prepare). Learned targets are attached in // ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT; they stay in it across consecutive passes and go back to GENERAL at the next @@ -128,6 +132,11 @@ void vk_exec_pass_end(VkExec *ex); // Full memory barrier (all commands, all memory); ends the open pass first. Copies and dispatches order themselves with // it. Skipped when nothing was recorded since the last one. void vk_exec_barrier(VkExec *ex); +// The barrier before a game dispatch: a full one, or with NX2NAVI_BARRIER_GAME none unless the replay's own commands +// came since the last full barrier (vk_exec.c barrier_to_why). Ends the open pass. +void vk_exec_barrier_dispatch(VkExec *ex); +// NX2NAVI_TRACE=perf, every 600 frames: the barriers per frame by kind. +void vk_exec_barrier_log(uint64_t frame); // vk_texture.c (NX2NAVI_TEX_ALIAS): commands that must run at the replay's current position. vk_exec_inline_begin returns // the command buffer being recorded after a full barrier (an open pass is ended; *reopen says so), or VK_NULL_HANDLE // when this thread is not recording a replay. vk_exec_inline_end begins the pass again when it was open. diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4f375ebe..0c611fd9 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -5182,7 +5182,7 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) { VkPipeline p = vk_pipeline_compute(cs, &info); if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); if ((info->pool && !pool_on()) || shader_skipped(cs)) return; // NX2NAVI_POOL=0, NX2NAVI_SKIP_SHADERS - vk_exec_barrier(ex); // ends the pass; earlier writes are visible to the shader + vk_exec_barrier_dispatch(ex); // ends the pass; earlier writes are visible to the shader bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); c0_dim[0] = (uint32_t)d->x, c0_dim[1] = (uint32_t)d->y, c0_dim[2] = (uint32_t)d->z; twin_dispatch = 1; @@ -5217,7 +5217,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { VkPipeline p = vk_pipeline_compute(cs, &info); if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); if ((info->pool && !pool_on()) || shader_skipped(cs)) return; // NX2NAVI_POOL=0, NX2NAVI_SKIP_SHADERS - vk_exec_barrier(ex); + vk_exec_barrier_dispatch(ex); VK_MEM_USE(ip); bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); c0_dim[0] = c0_dim[1] = c0_dim[2] = 0; From 4a954a1c6d401e2ed5c1e8218fdf17d7f89e3e5f Mon Sep 17 00:00:00 2001 From: ordinarybob Date: Tue, 10 Nov 2026 14:09:21 -0500 Subject: [PATCH 020/153] Refresh the texture pool from a list of its entries instead of walking every slot, for about 7 % more frames a second where the replay thread is the limit. Each submission's first sync of the bindless texture pool walked every registered slot, 33,000 in Kokiri Forest, to find the few textures the CPU wrote and the few slots that changed. Three parts of that walk change, each with its own switch (pool_fast, pool_presnap, pool_group; on by default, 0 and ab accepted): - The freshness check goes through a list of the entries the slots resolved to, with a check by 2 MiB region first; slots that resolved to nothing are looked up again when the game's memory map changes and every eighth submission. The use stamps the idle eviction reads are left from a count of the slots that hold each entry. - The check takes the write-watch snapshots of its regions first, so unchanged entries pass the region test. - The GPU-written slots of one address bucket with one descriptor form a group, which the dirty walk leaves alone while its first member is unchanged. Measured on an RTX 5090 at 2560x1440, standing in Kokiri Forest, in-run A/B over five runs with all three switched (three of them with the barrier change switched too): 93.6 to 100.7, 96.4 to 103.1, 95.8 to 102.6, 99.3 to 107.1 and 91.9 to 99.9 frames a second, the replay thread 0.7 to 0.8 ms a frame shorter; a control's blocks differ by 0.4 %. On a walk with NX2NAVI_POOL_GROUP_CHECK=1 no group that was left alone had missed a write, and screenshots across the A and B blocks show the same picture. From pull request #390. Not tried: the texture eviction under memory pressure (a 9 GiB budget was never reached on the walk), AMD and Intel drivers, Linux. One of the nine runs with it on ended by itself with exit code 0 and nothing in its log two seconds after the window lost the focus; three repeats of that run did not. Co-Authored-By: Claude Fable 5.2 --- runtime/cfg.c | 1 + runtime/host.h | 1 + runtime/memory.c | 9 ++ runtime/vk.h | 36 ++++- runtime/vk_exec_draw.c | 261 ++++++++++++++++++++++++++++-- runtime/vk_memory.c | 37 +++++ runtime/vk_texture.c | 352 ++++++++++++++++++++++++++++++++++++++--- 7 files changed, 663 insertions(+), 34 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 115377e2..31cf1cca 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -54,6 +54,7 @@ static const char *const known[] = { "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW", + "POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "REFLEX", "RENDER_SCALE", "REPLAY_ASYNC", "RTSS", "RTSS_ENUM", "SCENE_SHARE", "PRESNAP_PLACE", diff --git a/runtime/host.h b/runtime/host.h index e77c8a6f..e8af6dde 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -161,6 +161,7 @@ uint32_t mem_map(uint64_t dst, uint64_t src, uint64_t n); uint32_t mem_unmap(uint64_t dst, uint64_t src, uint64_t n); // 0x05 uint32_t mem_query(uint64_t a, RtMemoryInfo *out); // 0x06 int mem_readable(uint64_t a, uint64_t n); // every page readable +uint32_t mem_map_gen(void); // changes with everything mem_readable answers from (mapping, heap, permissions) int mem_read_hold(uint64_t a, uint64_t n); // mem_readable that keeps the range mapped until mem_read_release void mem_read_release(void); uint32_t mem_map_physical(uint64_t a, uint64_t n); // 0x2C diff --git a/runtime/memory.c b/runtime/memory.c index 21983d6b..c8bff935 100644 --- a/runtime/memory.c +++ b/runtime/memory.c @@ -683,6 +683,11 @@ static uint64_t image_bytes(void) { static int overlaps(uint64_t a, uint64_t n, uint64_t b, uint64_t m) { return a < b + m && b < a + n; } static int inside(uint64_t a, uint64_t n, RtRange r) { return a >= r.base && n <= r.size && a - r.base <= r.size - n; } +// Bumped (lock held exclusively) by every change of what is mapped or readable: vk_exec_draw.c's texture pool looks the +// slots over unreadable storage up again only then (and now and then). +static uint32_t map_gen; +uint32_t mem_map_gen(void) { return __atomic_load_n(&map_gen, __ATOMIC_ACQUIRE); } + // ---- 0x01 SetHeapSize uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { plat_mutex_lock(&lock); @@ -693,6 +698,7 @@ uint32_t mem_set_heap_size(uint64_t size, uint64_t *addr) { if (size > heap_size) commit(L.heap.base + heap_size, size - heap_size, "heap"); if (size < heap_size) decommit(L.heap.base + size, heap_size - size, "heap"); heap_size = size; + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); if (addr) *addr = L.heap.base; return R_OK; @@ -749,6 +755,7 @@ uint32_t mem_set_permission(uint64_t a, uint64_t n, uint32_t perm) { } memset(t, (int)perm, n / PAGE); protect(a, n, perm == PERM_RW ? PLAT_PROT_RW : perm == PERM_R ? PLAT_PROT_READ : PLAT_PROT_NONE, "module data"); + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); rt_log("%s data permission +%#llx size %#llx perm %u", m->name, (unsigned long long)(a - m->base), (unsigned long long)n, perm); @@ -820,6 +827,7 @@ static uint32_t stack_move(int unmap, uint64_t dst, uint64_t src, uint64_t n) { stack_maps[nstack_maps++] = (StackMap){dst, src, n}; } out: + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); return r; } @@ -872,6 +880,7 @@ static uint32_t alias_map(int unmap, uint64_t a, uint64_t n) { alias_bytes += fresh * PAGE; } out: + __atomic_store_n(&map_gen, map_gen + 1, __ATOMIC_RELEASE); plat_mutex_unlock(&lock); if (r) rt_log("%sMapPhysicalMemory %#llx+%#llx failed %#x (alias %#llx used, heap %#llx)", unmap ? "Un" : "", (unsigned long long)a, (unsigned long long)n, r, (unsigned long long)alias_bytes, diff --git a/runtime/vk.h b/runtime/vk.h index 55147b61..a70d5d21 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -565,8 +565,42 @@ uint32_t vk_texture_entry_gen(void); // vk_texture.c: bumped by // Slots whose token may have changed since *pos (0 at first; then the value these calls leave): fn is called for each // (some more than once) and *pos advanced. 0 when too many changes came since: the caller looks at every slot. *pos // from vk_texture_dirty_pos_locked after such a walk. Texture lock held. -int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg); +// gfn, when given, gets each slot group of a bucket in place of fn for its members. +int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void (*gfn)(int g, void *arg), void *arg); +// Slot groups (vk_texture.c): the GPU-written slots of one address bucket with one descriptor hash kh, which resolve +// alike. head the first member, vk_texture_group_next_locked the next (-1: the end), n how many; stamp and fmask the +// texture pool's (vk_exec_draw.c pool_dirty_group: 1 when every member holds the same views, of the kinds fmask; a slot +// that joins goes to the tail, refilled on its own), set 0 by vk_texture_groups_unstamp_locked. Texture lock held. +typedef struct { uint64_t kh; int32_t head, n; uint32_t stamp, fmask; } VkSlotGroup; +VkSlotGroup *vk_texture_group_locked(int g); +int vk_texture_group_next_locked(int id); +void vk_texture_groups_unstamp_locked(void); uint64_t vk_texture_dirty_pos_locked(void); +// The bindless pool's entries (vk_texture.c): after a pool fill of slot id, notes the entry it resolved to (1; 0: none) +// and gives its number for vk_texture_pool_hold_locked in *ent (0: none); a submission's freshness check of every +// noted entry (returns how many). Texture lock held. +int vk_texture_pool_note_locked(int id, uint32_t *ent); +int vk_texture_pool_fresh_locked(void); +// A pool slot that held the entry old (0: none) now holds ent: the entries slots hold get the use stamps a lookup +// leaves (vk_texture.c touch) from the check by list. Texture lock held. +void vk_texture_pool_hold_locked(uint32_t old, uint32_t ent); +// A slot whose last lookup found no entry, looked up again: 1 it resolves now; 0 still none, its storage may become +// readable; -1 none until its token changes (a CPU slot whose texture was released after its registration, no storage). +int vk_texture_slot_retry_locked(int id); +// vk_memory.c, for the pool's freshness check: the generation table's lock, shared; with it held, 1 when every 2 MiB +// region of [addr, addr+size) has this epoch's write-watch snapshot and no page stamped after since (nothing in it can +// have changed), 0 when a closer look is due (vk_mem_changed_snap). +void vk_mem_gen_lock_shared(void); +void vk_mem_gen_unlock_shared(void); +int vk_mem_unchanged_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch); +// The same check's reason when it fails: 1 a region without this epoch's snapshot yet, 2 one untracked, 3 one written +// since, 4 nothing to check (no size, beyond the regions); 0 unchanged. +int vk_mem_unchanged_why_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch); +// The lock exclusively, for the snapshot of the regions of [addr, addr+size) that have none in epoch yet (the number of +// write-watch queries it took). +void vk_mem_gen_lock(void); +void vk_mem_gen_unlock(void); +uint64_t vk_mem_snap_range_locked(uint64_t addr, uint64_t size, uint32_t epoch); void vk_sampler_dlss_bias(float bias); // texture LOD bias added for DLSS upscaling (material samplers) void vk_sampler_aniso_live(void); // each present: NX2NAVI_ANISO after a settings change (tiled samplers) uint32_t vk_sampler_gen(void); // changes whenever a sampler slot is registered diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 0c611fd9..36fd1c4b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -1743,6 +1743,9 @@ static int tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int stor // absent from that submit's page tables, a GPU page fault on the 128x128 probe pass of Kokiri Forest's first frame // (RADV's radv_wait_for_vm_map_updates hid it, as did NX2NAVI_SPLIT_DRAWS=0). volatile uint32_t vk_pool_kinds_used; +// Per texture slot, what pool_fill keeps between refills: the descriptor hash of the registration it was read under, +// and the lookup memo stamp its entries were last written from (0: none) with the kinds that write covered. +typedef struct { uint64_t kh; uint32_t kreg, stamp, fmask; } PoolSlot; #define POOL_SETS VK_POOL_SETS #define POOL_LOG (1u << 20) // entries of the change log: binding * VK_POOL_TEX + id, one per write static struct { @@ -1758,7 +1761,24 @@ static struct { uint64_t logn; // writes so far uint64_t *last; // per entry: the log position of its newest write (catch-up writes it once, there) uint64_t switches, waits, catchup, written, rewrites, catch_ns, sync_ns, n_slots, n_refill, n_fresh; + uint64_t n_walk[2], walk_visit[2], walk_refill[2], walk_ns[2]; // dirty walks: at a submission's start, within one + uint64_t n_visit; // slots the dirty walks looked at + uint64_t n_gwalk, n_gskip, n_gskip_slots, n_grep; // POOL_GROUP: groups walked, left alone and their slots, first + // members looked up for the memo + uint64_t n_gchk_writes, n_gchk_groups; // POOL_GROUP_CHECK: writes the groups left alone got all the same, and in how many + uint64_t n_gchk_pending, n_gchk_bad; // of members registered since their last fill (not counted above); this group's + uint64_t n_gnouni, n_gchanged; // groups walked member by member: not known to hold one resolution, first member changed + int gmode; // pool_group() as of the last sync uint64_t n_by_reg, n_by_addr; // refills whose token changed by a registration, by an address generation only + uint64_t full_ns, n_full; // the walks over every slot (a submission's first sync) and their time + uint64_t ep_ns, n_ep, n_ep_ent; // POOL_FAST: a submission's freshness check of the noted entries instead + uint64_t unres_ns, n_unres_walks; // of it: the walks over the slots without an entry + uint32_t unres_mg; // mem_map_gen of the last such walk + int unres_more; // it left a lookup for the creation budget: the next submission walks again + PoolSlot *ps; // [id] + uint8_t *unres_on; // [id]: its last fill resolved to no entry (POOL_FAST: retried each submission) + int *unres, nunres, cunres; // those slots (some may have left: unres_on clear) + uint32_t *ent; // [id]: the entry its last fill resolved to, held there (pool_hold); 0: none uint64_t dirty_pos; // vk_texture_dirty_slots_locked position (0: the next sync walks every slot) int ready, uab, sealed; // sealed: a part of the current submission already reads sets[cur] uint32_t kinds, gen, epoch, sgen; @@ -1836,7 +1856,10 @@ static VkDescriptorSetLayout pool_layout_make(void) { PL.tok = calloc(VK_POOL_TEX, sizeof *PL.tok); PL.view = calloc((size_t)VK_POOL_KINDS * VK_POOL_TEX, sizeof *PL.view); PL.samp = calloc(VK_POOL_SAMP, sizeof *PL.samp); - if (!PL.tok || !PL.view || !PL.samp) rt_fatal("vk: out of memory"); + PL.ps = calloc(VK_POOL_TEX, sizeof *PL.ps); + PL.unres_on = calloc(VK_POOL_TEX, 1); + PL.ent = calloc(VK_POOL_TEX, sizeof *PL.ent); + if (!PL.tok || !PL.view || !PL.samp || !PL.ps || !PL.unres_on || !PL.ent) rt_fatal("vk: out of memory"); rt_log("vk: bindless texture pools: %d kinds x %d textures, %d samplers, %s, %d sets (one per submission in turn)%s", VK_POOL_KINDS, VK_POOL_TEX, VK_POOL_SAMP, PL.uab ? "update after bind" : "no update-after-bind on this GPU (entries written while bound are undefined)", PL.nsets, @@ -2071,6 +2094,7 @@ uint64_t vk_pool_takes(void) { return __atomic_load_n(&pool_takes, __ATOMIC_ACQU // The current set gets the entries written into other sets in [from, to) of the log, as PL.view / PL.samp hold them now. static uint64_t pool_memo_hits; // pool_fill: slots that shared another slot's lookup (defined with pool_memo below) +static uint64_t pool_memo_same; // of those, slots whose entries held the shared views already static void pool_catch_up(uint64_t from, uint64_t to) { VkDescriptorSet set = PL.set; @@ -2089,23 +2113,47 @@ static void pool_catch_up(uint64_t from, uint64_t to) { pool_flush(); PL.switches++; if (PL.switches % 3600 == 0) { // diagnosis: what invalidates the slots (vk_texture.c vk_texture_bucket_stats) - char bs[320]; + char bs[768]; vk_texture_bucket_stats(bs, sizeof bs); rt_log("vk: texture pool buckets over 3600 switches: %s", bs); } if (PL.switches % 3600 == 0) rt_log("vk: texture pool sets: %llu switches, %llu waited for the GPU, %.1f log entries and %.1f writes per " - "catch-up, %llu whole rewrites; %.3f ms catch-up and %.3f ms entry refresh per switch (%.0f slots, %.0f " - "refilled: %.0f registered, %.0f address changes, %.0f shared a lookup; %.0f checked); %llu identical registrations kept; " + "catch-up, %llu whole rewrites; %.3f ms catch-up and %.3f ms entry refresh per switch (%.3f ms of it in " + "%.2f walks over every slot, %.3f ms in %.2f checks of %.0f listed entries and %d slots without one " + "(%.3f ms in %.2f walks over those); " + "%.0f slots, %.0f refilled: %.0f registered, %.0f address changes, %.0f " + "shared a lookup, %.0f of them holding its views already; %.0f checked); %llu identical registrations kept; " "%llu refreshes created nothing for their frame's time (pool_create_ms), %llu lookups of slots left in the frame skipped", (unsigned long long)PL.switches, (unsigned long long)PL.waits, (double)PL.catchup / (double)PL.switches, (double)PL.written / (double)PL.switches, (unsigned long long)PL.rewrites, PL.catch_ns / 1e6 / (double)PL.switches, PL.sync_ns / 1e6 / (double)PL.switches, + PL.full_ns / 1e6 / (double)PL.switches, (double)PL.n_full / (double)PL.switches, + PL.ep_ns / 1e6 / (double)PL.switches, (double)PL.n_ep / (double)PL.switches, + PL.n_ep ? (double)PL.n_ep_ent / (double)PL.n_ep : 0.0, PL.nunres, + PL.unres_ns / 1e6 / (double)PL.switches, (double)PL.n_unres_walks / (double)PL.switches, (double)PL.n_slots / (double)PL.switches, (double)PL.n_refill / (double)PL.switches, (double)PL.n_by_reg / (double)PL.switches, (double)PL.n_by_addr / (double)PL.switches, - (double)pool_memo_hits / (double)PL.switches, + (double)pool_memo_hits / (double)PL.switches, (double)pool_memo_same / (double)PL.switches, (double)PL.n_fresh / (double)PL.switches, (unsigned long long)vk_texture_same_regs(), (unsigned long long)PL.n_frame_stops, (unsigned long long)PL.n_defer_skips); + if (PL.switches % 3600 == 0) + rt_log("vk: texture pool dirty walks per switch: %.2f at a submission's start (%.0f slots looked at, %.0f " + "refilled, %.3f ms), %.2f within one (%.0f looked at, %.0f refilled, %.3f ms)", + (double)PL.n_walk[0] / (double)PL.switches, (double)PL.walk_visit[0] / (double)PL.switches, + (double)PL.walk_refill[0] / (double)PL.switches, PL.walk_ns[0] / 1e6 / (double)PL.switches, + (double)PL.n_walk[1] / (double)PL.switches, (double)PL.walk_visit[1] / (double)PL.switches, + (double)PL.walk_refill[1] / (double)PL.switches, PL.walk_ns[1] / 1e6 / (double)PL.switches); + if (PL.switches % 3600 == 0) + rt_log("vk: texture pool slot groups per switch (NX2NAVI_POOL_GROUP): %.0f walked, %.0f left alone with %.0f " + "slots, %.0f first members refilled (%.0f changed), %.0f not known to hold one resolution; check " + "(NX2NAVI_POOL_GROUP_CHECK): %llu writes in %llu groups left alone so far, %llu more to members registered " + "since their last fill", + (double)PL.n_gwalk / (double)PL.switches, (double)PL.n_gskip / (double)PL.switches, + (double)PL.n_gskip_slots / (double)PL.switches, (double)PL.n_grep / (double)PL.switches, + (double)PL.n_gchanged / (double)PL.switches, (double)PL.n_gnouni / (double)PL.switches, + (unsigned long long)PL.n_gchk_writes, (unsigned long long)PL.n_gchk_groups, + (unsigned long long)PL.n_gchk_pending); } // Entry id of every kind in mask: the slot's view of that kind or the kind's stand-in. Texture lock held. 0: the @@ -2151,22 +2199,75 @@ static int pool_on(void) { // transient render target registered over its storage, and every finalize and initialize of that target changes their // tokens, so a refill looked up the same texture ~400 times, ~15,000 lookups a frame in Kokiri Forest. An entry holds the // views one slot got, for the slots with its hash, address generation and entry generation. +// Its stamp changes only when one of its views does (NX2NAVI_POOL_FAST, default on): a slot whose entries were written +// from the entry at the stamp it has now (PoolSlot) holds those views already, so a refill that finds the entry so +// compares and writes nothing. That is most refills: a transient render target released and taken back moves the +// address generation of its storage, ~1,300 slots in Kokiri Forest, and every slot resolves as before (18,500 of 18,800 +// refills a submission shared a lookup, each comparing up to 16 views a quarter megabyte apart: 1.9 ms). #define POOL_MEMO 4096 -static struct { uint64_t kh; uint32_t agen, egen, mask; VkImageView view[VK_POOL_KINDS]; } pool_memo[POOL_MEMO]; -static uint64_t pool_memo_hits; +static struct { uint64_t kh; uint32_t agen, egen, mask, stamp, got, ent; VkImageView view[VK_POOL_KINDS]; } pool_memo[POOL_MEMO]; +static uint32_t pool_stamp; // the last stamp given out +static uint64_t pool_memo_hits, pool_memo_same; + +static int pool_fast(void) { // ab: in the B blocks + static int on = -1; + if (on < 0) on = rt_knob("POOL_FAST", "1"); + return RT_KNOB_ON(on); +} + +// Slot id's last fill resolved to the entry ent (vk_texture_pool_note_locked; 0: none). vk_texture.c counts the slots +// that hold each entry: the entries held are the ones the walk over every slot reached with a lookup in each +// submission, and the check by list leaves their use stamps from that count (vk_texture_pool_hold_locked). +static void pool_hold(uint32_t id, uint32_t ent) { + if (PL.ent[id] == ent) return; + vk_texture_pool_hold_locked(PL.ent[id], ent); + PL.ent[id] = ent; +} + +// Slot id's last fill resolved to no entry (got 0) or to one: the slots without one are looked up again each +// submission (pool_sync_entries), as the walk over every slot did: an entry their storage could not have then may be +// made now, and its address generation then refills them. +static void pool_unres(uint32_t id, int got) { + if (got || PL.unres_on[id]) { + if (got) PL.unres_on[id] = 0; + return; + } + PL.unres_on[id] = 1; + if (PL.nunres == PL.cunres) { + int n = PL.cunres ? PL.cunres * 2 : 1024; + int *p = realloc(PL.unres, sizeof *p * (size_t)n); + if (!p) rt_fatal("vk: out of memory"); + PL.unres = p, PL.cunres = n; + } + PL.unres[PL.nunres++] = (int)id; +} static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { vk_texture_create_deferred_locked(); - uint64_t kh = tok ? vk_texture_slot_khash_locked((int)id) : 0; + PoolSlot *ps = &PL.ps[id]; + uint64_t kh = 0; + if (tok) { // the descriptor hash changes only with a registration (the token's upper half) + uint32_t reg = (uint32_t)(tok >> 32); + if (ps->kreg != reg || !ps->kh) ps->kh = vk_texture_slot_khash_locked((int)id), ps->kreg = reg; + kh = ps->kh; + } uint32_t ag = (uint32_t)tok; typeof(pool_memo[0]) *m = kh ? &pool_memo[(kh >> 20) % POOL_MEMO] : NULL; if (m && m->kh == kh && m->agen == ag && m->egen == vk_texture_entry_gen() && (m->mask & mask) == mask) { + pool_memo_hits++; + pool_unres(id, (int)m->got); + pool_hold(id, m->ent); + if (ps->stamp && ps->stamp == m->stamp && (ps->fmask & mask) == mask && pool_fast()) { + pool_memo_same++; + return 1; + } for (int k = 0; k < VK_POOL_KINDS; k++) { if (!(mask >> k & 1)) continue; VkImageView *e = &PL.view[(size_t)k * VK_POOL_TEX + id]; if (*e != m->view[k]) *e = m->view[k], pool_write((uint32_t)k, id, m->view[k], VK_NULL_HANDLE); } - pool_memo_hits++; + ps->fmask = ps->stamp == m->stamp ? ps->fmask | mask : mask; + ps->stamp = m->stamp; return 1; } for (int k = 0; k < VK_POOL_KINDS; k++) { @@ -2180,12 +2281,29 @@ static int pool_fill(VkExec *ex, uint32_t id, uint32_t mask, uint64_t tok) { if (*e != ii.imageView) *e = ii.imageView, pool_write((uint32_t)k, id, ii.imageView, VK_NULL_HANDLE); } int ok = !vk_texture_create_deferred_locked(); + uint32_t ent = 0; + int got = vk_texture_pool_note_locked((int)id, &ent); + if (tok) pool_unres(id, got); + pool_hold(id, tok ? ent : 0); + ps->stamp = 0; // its entries match no memo entry, unless the one below if (m && ok) { // the generation after the lookups: an entry they created is part of what they resolved uint32_t eg = vk_texture_entry_gen(); - if (m->kh != kh || m->agen != ag || m->egen != eg) m->kh = kh, m->agen = ag, m->egen = eg, m->mask = 0; + // a view that changes gives the entry a new stamp; one kept outside the mask keeps its value, so a kind that + // comes back with another view is a change too + int changed = !m->stamp; for (int k = 0; k < VK_POOL_KINDS; k++) - if (mask >> k & 1) m->view[k] = PL.view[(size_t)k * VK_POOL_TEX + id]; + if (mask >> k & 1) { + VkImageView v = PL.view[(size_t)k * VK_POOL_TEX + id]; + if (m->view[k] != v) m->view[k] = v, changed = 1; + } + if (m->kh != kh || m->agen != ag || m->egen != eg) m->kh = kh, m->agen = ag, m->egen = eg, m->mask = 0; m->mask |= mask; + m->got = (uint32_t)got, m->ent = ent; + if (changed) { + if (!++pool_stamp) pool_stamp = 1; + m->stamp = pool_stamp; + } + ps->stamp = m->stamp, ps->fmask = mask; } return ok; } @@ -2237,6 +2355,7 @@ static int pool_refill(VkExec *ex, uint32_t id, uint32_t used, uint64_t t, int s static void pool_dirty_slot(int id, void *arg) { // vk_texture_dirty_slots_locked: a slot whose token may have changed PoolDirty *d = arg; + PL.n_visit++; if (id >= d->n) return; uint64_t t = vk_texture_slot_token(id); if (t == PL.tok[id]) return; @@ -2253,6 +2372,76 @@ static int pool_current(void) { vk_texture_slots_gen() == PL.gen && vk_texture_epoch() == PL.epoch && vk_sampler_gen() == PL.sgen; } +// NX2NAVI_POOL_GROUP=1 (default), 0, ab: a dirty bucket's slot groups (vk_texture.c) are walked whole. The members of a +// group resolve alike, so once a walk of the group brought every member to the current resolution (every fill done, an +// entry for each) they all hold the same views, and later the group is left alone when its first member still holds the +// current resolution: its token did not change (its views are what that token resolves to), or its refill wrote nothing. +// A slot that joined since is at the tail and refilled on its own (its registration's entry on the dirty ring). +// The members keep their older tokens: their views are what those tokens resolved to, and what the current ones do. +// (Comparing the memo's stamps instead left half the groups to their members: the memo's entries are shared between +// hashes, so the same views came back under a new stamp.) +static int pool_group(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("POOL_GROUP", "1"); + return RT_KNOB_ON(mode); +} + +// NX2NAVI_POOL_GROUP_CHECK=1: a group left alone is walked member by member all the same, and the writes that walk +// makes are counted (none when the rule holds; the pool log's slot groups line), but for the members registered since +// their last fill: their registration's entry on the dirty ring refills them in the same walk (counted apart). +static int pool_group_check(void) { + static int on = -1; + if (on < 0) on = rt_knob("POOL_GROUP_CHECK", "0") == 1; // a diagnosis: no settings row + return on; +} + +static void pool_dirty_group(int g, void *arg) { + PoolDirty *d = arg; + uint64_t chk = 0; + VkSlotGroup *G = vk_texture_group_locked(g); + int rep = G->head; + PL.n_gwalk++; + if (!G->stamp) PL.n_gnouni++; + if (G->stamp && (G->fmask & d->used) == d->used && rep >= 0 && rep < d->n && !PL.unres_on[rep]) { + uint64_t t = vk_texture_slot_token(rep); + int same = t == PL.tok[rep]; + if (!same) { // the first member's refill: nothing written, an entry found, its token taken + uint64_t l0 = PL.logn; + PL.n_grep++; + pool_dirty_slot(rep, arg); + same = PL.tok[rep] == t && PL.logn == l0 && !PL.unres_on[rep]; + if (!same) PL.n_gchanged++; + } + if (same) { + PL.n_gskip++, PL.n_gskip_slots += (uint64_t)G->n; + if (!pool_group_check()) return; + chk = PL.logn + 1; + } + } + int r0 = d->retry, uni = 1; // member by member: then all hold the current resolution, unless a fill was left + d->retry = 0; + for (int id = G->head; id >= 0; id = vk_texture_group_next_locked(id)) { + if (id >= d->n) continue; // outside the pool + if (chk) { // the check: a member registered since its last fill is refilled by its own entry anyway + uint64_t l0 = PL.logn; + int pending = vk_texture_slot_token(id) >> 32 != PL.tok[id] >> 32; + pool_dirty_slot(id, arg); + if (PL.logn != l0) { + if (pending) PL.n_gchk_pending += PL.logn - l0; + else PL.n_gchk_writes += PL.logn - l0, PL.n_gchk_bad++; + } + } else { + pool_dirty_slot(id, arg); + } + if (PL.unres_on[id]) uni = 0; + } + if (d->retry) uni = 0; + d->retry |= r0; + G = vk_texture_group_locked(g); + G->stamp = (uint32_t)uni, G->fmask = uni ? d->used : 0; + if (chk && PL.n_gchk_bad) PL.n_gchk_groups++, PL.n_gchk_bad = 0; +} + static void pool_sync_entries(VkExec *ex) { uint32_t used = __atomic_load_n(&vk_pool_kinds_used, __ATOMIC_RELAXED); uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen(); @@ -2277,12 +2466,51 @@ static void pool_sync_entries(VkExec *ex) { vk_texture_create_budget_locked(spent ? 0 : pool_create_budget()); PL.n_frame_stops += (uint64_t)spent; // Only a slot change since the last sync (no new kinds, no new submission, nothing left over): the slots that - // changed (vk_texture_dirty_slots_locked), not the whole table + // changed (vk_texture_dirty_slots_locked), not the whole table. With NX2NAVI_POOL_FAST (default on) a new + // submission too: its freshness check goes through the entries the slots resolved to and the slots that + // resolved to none (vk_texture_pool_fresh_locked, pool_unres), not every slot. PoolDirty pd = {ex, used, n, 0, spent}; - int walked = !fresh && ep == PL.epoch && !PL.retry && PL.dirty_pos && - vk_texture_dirty_slots_locked(&PL.dirty_pos, pool_dirty_slot, &pd); + int newep = ep != PL.epoch; + uint64_t v0 = PL.n_visit, f0 = PL.n_refill, w0 = kern_now_ns(); + int grp = pool_group(); + if (grp && !PL.gmode) vk_texture_groups_unstamp_locked(); // members were refilled one by one meanwhile + PL.gmode = grp; + int walked = !fresh && (!newep || pool_fast()) && !PL.retry && PL.dirty_pos && + vk_texture_dirty_slots_locked(&PL.dirty_pos, pool_dirty_slot, grp ? pool_dirty_group : NULL, &pd); + if (walked) { + int at = newep ? 0 : 1; + PL.n_walk[at]++, PL.walk_visit[at] += PL.n_visit - v0, PL.walk_refill[at] += PL.n_refill - f0; + PL.walk_ns[at] += kern_now_ns() - w0; + } retry = pd.retry; + if (walked && newep) { + uint64_t te = kern_now_ns(); + PL.n_ep_ent += (uint64_t)vk_texture_pool_fresh_locked(); + // The slots without an entry are over storage the guest has not mapped (stale descriptors, 1,160 in + // Kokiri Forest): looked up again when the mapping changed, while a lookup is left for the creation budget, + // and every 8th submission for the rest (an entry whose creation failed, an image at their address that + // took GPU content since: slot_resolve's alias rule) + uint32_t mg = mem_map_gen(); + uint64_t tu = kern_now_ns(); + int walk_unres = mg != PL.unres_mg || PL.unres_more || !(PL.n_ep & 7); + if (walk_unres) PL.unres_mg = mg, PL.n_unres_walks++, vk_texture_create_deferred_locked(); + for (int i = 0; walk_unres && i < PL.nunres;) { + int id = PL.unres[i]; + uint64_t t = PL.unres_on[id] ? vk_texture_slot_token(id) : 0; + int r = t && t == PL.tok[id] ? vk_texture_slot_retry_locked(id) : -1; + if (r) { // resolves now (an entry it made changes its token: refilled), was refilled since, or + PL.unres_on[id] = 0; // resolves to none until its token changes (the refill lists it again) + PL.unres[i] = PL.unres[--PL.nunres]; + continue; + } + i++; + } + if (walk_unres) PL.unres_more = vk_texture_create_deferred_locked(); + PL.unres_ns += kern_now_ns() - tu; + PL.n_ep++, PL.ep_ns += kern_now_ns() - te; + } if (!walked) PL.dirty_pos = vk_texture_dirty_pos_locked(); + uint64_t tw = walked ? 0 : kern_now_ns(); for (int id = 0; id < n && !walked; id++) { // slots past n were never registered: stand-ins since the first sync uint64_t t = vk_texture_slot_token(id); if (t != PL.tok[id]) { @@ -2298,7 +2526,10 @@ static void pool_sync_entries(VkExec *ex) { vk_texture_by_id_locked(id, NULL, NULL); // the submit's freshness check } } - if (!walked) PL.n_slots += (uint64_t)n; + if (!walked) { + PL.n_slots += (uint64_t)n, PL.n_full++, PL.full_ns += kern_now_ns() - tw; + vk_texture_groups_unstamp_locked(); // the walk over every slot refilled members one by one + } vk_texture_create_budget_locked(-1); } PL.retry = retry != 0; diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index f2ee73a1..e730abea 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -914,6 +914,43 @@ int vk_mem_changed_snap(uint64_t addr, uint64_t size, uint32_t since, uint32_t e return untracked ? -1 : changed; } +void vk_mem_gen_lock_shared(void) { plat_mutex_lock_shared(&gen_lock); } +void vk_mem_gen_unlock_shared(void) { plat_mutex_unlock_shared(&gen_lock); } + +void vk_mem_gen_lock(void) { plat_mutex_lock(&gen_lock); } +void vk_mem_gen_unlock(void) { plat_mutex_unlock(&gen_lock); } + +int vk_mem_unchanged_why_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch) { + if (!size || !epoch) return 4; + uint64_t lo = addr & ~0xfffull, hi = (addr + size + 0xfff) & ~0xfffull; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (r1 >= SNAP_REGIONS) return 4; + for (uint64_t r = r0; r <= r1; r++) { // REG_SHIFT is SNAP_SHIFT: reg_max is the newest stamp in the region + if (snap_at[r] != epoch) return 1; + if (snap_untracked[r]) return 2; + if (reg_max[r] > since) return 3; + } + return 0; +} + +int vk_mem_unchanged_locked(uint64_t addr, uint64_t size, uint32_t since, uint32_t epoch) { + return !vk_mem_unchanged_why_locked(addr, size, since, epoch); +} + +uint64_t vk_mem_snap_range_locked(uint64_t addr, uint64_t size, uint32_t epoch) { + if (!size || !epoch) return 0; + uint64_t lo = addr & ~0xfffull, hi = (addr + size + 0xfff) & ~0xfffull; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (r1 >= SNAP_REGIONS) return 0; + for (uint64_t r = r0; r <= r1; r++) + if (snap_at[r] != epoch) { + uint64_t q = snap_regions(r0, r1, epoch); + n_snap_calls += q; + return q; + } + return 0; +} + void vk_mem_snap_stats(uint64_t *checks, uint64_t *calls, uint64_t *untracked) { *checks = __atomic_exchange_n(&n_snap_checks, 0, __ATOMIC_RELAXED); *calls = __atomic_exchange_n(&n_snap_calls, 0, __ATOMIC_RELAXED); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index e9fc6390..87a9cc4b 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -89,6 +89,8 @@ typedef struct Tex { uint64_t ow_frame; // gfx_counters.frames when overwritten was set uint8_t overwritten; // released, and a texture at another address was drawn over its storage (overwrite_others) uint8_t in_gwin; // in gwin[] + int pidx; // 1 + its index in pent[] (vk_texture_pool_note_locked); 0: not listed + int ph; // 1 + its index in pref[] (pool_ref); 0: none } Tex; // A slot keeps what vk_texture_by_id resolved for it, so a draw binding skips the key lookup and the address chain: the @@ -296,32 +298,135 @@ static void tex_gen_flip(Tex *e, int plain) { dirty_push(DIRTY_BUCKET | b); } -// Moves slot id into bucket b's list (the lists exist once a slot was registered: vk_texture_register). -static void slot_rebucket(int id, uint32_t b) { +// Slot groups: the GPU-written slots of one address bucket that share a descriptor hash (TexSlot.khash) resolve alike +// under the same address and entry generations, as the texture pool's memo has it (vk_exec_draw.c pool_fill). Ocarina +// keeps up to ~400 descriptors of each transient render target registered over its storage, and the fullest bucket, +// 1,281 slots, moves 8 times a submission in Kokiri Forest: 12,800 of the 17,600 slots a submission's dirty walks +// refilled held their views already. The dirty walk can hand each group to the pool whole (vk_texture_dirty_slots_locked's +// gfn), which leaves its members alone when their resolution did not change (NX2NAVI_POOL_GROUP). A slot is in its +// bucket's own list (khash 0: a CPU-written texture) or in its group's member list, through the same links. A slot that +// joins a group goes to its tail and leaves the group's stamp as it was: its registration put it on the dirty ring, so +// the walk that follows refills it on its own, and the head, whose state the pool tests, is a member from before. (The +// game's 118 registrations a submission cleared the stamps of the groups they joined, and the walk at a submission's +// start went member by member as before: 11,300 of 11,550 slots.) +typedef struct { VkSlotGroup pub; int32_t gnext, gprev, tail; uint32_t bucket; } SlotGroup; +static SlotGroup *grp; +static int ngrp, cgrp, grp_free = -1, ngrp_live; +static uint64_t n_grouped; // slots in groups +static int32_t bucket_ghead[AGEN_N]; // per bucket: its first group (-1: none; set with the lists) +static int32_t *slot_grp; // per slot id: its group, -1 none +typedef struct { uint64_t kh, b; } GrpKey; +static nvn_hmap grp_by = {.klen = sizeof(GrpKey)}; // -> group + 1 + +static int grp_get(uint64_t kh, uint32_t b) { + GrpKey k = {kh, b}; + void *v = nvn_hm_get(&grp_by, &k); + if (v) return (int)((intptr_t)v - 1); + int g; + if (grp_free >= 0) { + g = grp_free, grp_free = grp[g].gnext; + } else { + if (ngrp == cgrp) { + int n = cgrp ? cgrp * 2 : 1024; + SlotGroup *p = realloc(grp, sizeof *p * (size_t)n); + if (!p) rt_fatal("vk: out of memory for texture slot groups"); + grp = p, cgrp = n; + } + g = ngrp++; + } + SlotGroup *G = &grp[g]; + memset(G, 0, sizeof *G); + G->pub.kh = kh, G->pub.head = G->tail = -1, G->bucket = b; + G->gprev = -1, G->gnext = bucket_ghead[b]; + if (G->gnext >= 0) grp[G->gnext].gprev = g; + bucket_ghead[b] = g; + nvn_hm_put(&grp_by, &k, (void *)(intptr_t)(g + 1)); + ngrp_live++; + return g; +} + +static void grp_drop(int g) { + SlotGroup *G = &grp[g]; + GrpKey k = {G->pub.kh, G->bucket}; + nvn_hm_del(&grp_by, &k); + if (G->gprev >= 0) grp[G->gprev].gnext = G->gnext; + else bucket_ghead[G->bucket] = G->gnext; + if (G->gnext >= 0) grp[G->gnext].gprev = G->gprev; + G->pub.kh = 0, G->pub.head = G->tail = -1, G->pub.n = 0, G->pub.stamp = 0; + G->gnext = grp_free, grp_free = g; + ngrp_live--; +} + +// Places slot id in bucket b: at the tail of its group when it has a descriptor hash, else at the head of the bucket's +// own list (the lists exist once a slot was registered: vk_texture_register). +static void slot_place(int id, uint32_t b, uint64_t kh) { if (slot_prev[id] != -2) { // out of its old list + int32_t g = slot_grp[id]; + int32_t *head = g >= 0 ? &grp[g].pub.head : &bucket_head[slot_bucket[id]]; if (slot_prev[id] >= 0) slot_next[slot_prev[id]] = slot_next[id]; - else bucket_head[slot_bucket[id]] = slot_next[id]; + else *head = slot_next[id]; if (slot_next[id] >= 0) slot_prev[slot_next[id]] = slot_prev[id]; + else if (g >= 0) grp[g].tail = slot_prev[id]; + slot_grp[id] = -1; + if (g >= 0) { + n_grouped--; + if (!--grp[g].pub.n) grp_drop(g); + } } slot_bucket[id] = (uint16_t)b; + if (kh) { + int g = grp_get(kh, b); // may move grp + SlotGroup *G = &grp[g]; + slot_grp[id] = g; + G->pub.n++, n_grouped++; + slot_next[id] = -1; + slot_prev[id] = G->tail; + if (G->tail >= 0) slot_next[G->tail] = id; + else G->pub.head = id; + G->tail = id; + return; + } slot_prev[id] = -1; slot_next[id] = bucket_head[b]; if (bucket_head[b] >= 0) slot_prev[bucket_head[b]] = id; bucket_head[b] = id; } +VkSlotGroup *vk_texture_group_locked(int g) { return &grp[g].pub; } +int vk_texture_group_next_locked(int id) { return slot_next[id]; } +void vk_texture_groups_unstamp_locked(void) { + for (int g = 0; g < ngrp; g++) grp[g].pub.stamp = 0; +} + uint64_t vk_texture_dirty_pos_locked(void) { return ndirty; } -int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void *arg) { +// A bucket listed several times since *pos is walked once a round: the walk reads its slots' tokens after every change +// listed before the round began (the lock is held). Changes the walk itself makes (an entry a lookup created) are the +// next round's. A bucket's slot groups go to gfn when given, else their members to fn. +int vk_texture_dirty_slots_locked(uint64_t *pos, void (*fn)(int id, void *arg), void (*gfn)(int g, void *arg), void *arg) { if (!slot_next || ndirty - *pos > DIRTY_N) { *pos = ndirty; return 0; } - for (uint64_t q = *pos; q < ndirty; q++) { - uint32_t x = dirty[q % DIRTY_N]; - if (x & DIRTY_BUCKET) - for (int32_t id = bucket_head[x & ~DIRTY_BUCKET]; id >= 0; id = slot_next[id]) fn(id, arg); - else fn((int)x, arg); + static uint32_t seen[AGEN_N], round; + for (uint64_t q = *pos; q < ndirty;) { + uint64_t end = ndirty; + if (!++round) memset(seen, 0, sizeof seen), round = 1; + for (; q < end; q++) { + uint32_t x = dirty[q % DIRTY_N]; + if (x & DIRTY_BUCKET) { + uint32_t b = x & ~DIRTY_BUCKET; + if (seen[b] == round) continue; + seen[b] = round; + for (int32_t id = bucket_head[b]; id >= 0; id = slot_next[id]) fn(id, arg); + for (int32_t g = bucket_ghead[b]; g >= 0; g = grp[g].gnext) { // the walk registers nothing + if (gfn) gfn(g, arg); + else for (int32_t id = grp[g].pub.head; id >= 0; id = slot_next[id]) fn(id, arg); + } + } else { + fn((int)x, arg); + } + } } *pos = ndirty; return 1; @@ -1121,10 +1226,189 @@ static void rel_unlink(Tex *e) { e->rel_prev = e->rel_next = NULL; } +// The entries the bindless texture pool's slots resolved to, for a submission's freshness check (make_fresh_snap once +// per entry), which looked at every slot instead: 33,000 slots, 1.5 ms of the replay thread a submission in Kokiri +// Forest. vk_exec_draw.c pool_fill notes a slot's entry after it resolved the slot; only entries whose content the CPU +// provides are listed (the check does nothing for the others; one the GPU took over, gpu_owned, never uploads again +// and leaves), and an entry stays listed until it is destroyed: the list holds every entry a slot resolves to, and +// some that no slot does any more. Each keeps its storage range and the page generation of its last check here, so +// that the check of an entry whose 2 MiB regions took no write since (vk_mem_unchanged_locked, one shared lock for the +// whole list) touches neither the entry nor the lock: 5,600 entries took 0.77 ms a submission one by one. Texture lock +// held. +typedef struct { Tex *e; uint64_t addr, size; uint32_t since; int ok; } PEnt; // ok: uploaded as of since +static PEnt *pent; +static int npent, cpent, *pq, cpq; +static uint64_t n_pool_checks, n_pool_skips; +// The region check's misses (vk_mem_unchanged_why_locked): not uploaded, a region without this epoch's snapshot yet, one +// untracked, one written since, nothing to check; and the snapshot queries its own pass made (NX2NAVI_POOL_PRESNAP). +static uint64_t n_pool_miss[5], n_presnap_q; +static void make_fresh_snap(Tex *e); + +static void pool_unlist(Tex *e) { + if (!e->pidx) return; + int i = e->pidx - 1; + pent[i] = pent[--npent]; + pent[i].e->pidx = i + 1; + e->pidx = 0; +} + +// The entries the pool's slots hold: for each, how many slots' last fill resolved to it (vk_exec_draw.c pool_hold; a fill +// that shared another slot's lookup counts for the entry that lookup found). The walk over every slot looked each slot +// up in each submission, which left its entry's use stamp (touch): the idle clock reads it, and start_at's alias rule +// reads the frame of an entry with GPU content. The check by list is no lookup, so it leaves the stamps from these +// counts (pool_touch): in each submission for the GPU-owned entries, in turn for the others, each once in +// POOL_TOUCH_DIV submissions (IDLE_ROUNDS is 240 of them). An entry keeps its place until it is destroyed and no slot +// holds it any more: a destroyed one is refilled out of its slots by the bump of its bucket. +#define POOL_TOUCH_DIV 64 +typedef struct { Tex *e; uint32_t refs; int next; uint8_t gown; } PRef; // e NULL: destroyed, or free (refs 0, next) +static PRef *pref; +static int npref, cpref, pref_free; // pref_free: 1 + the first free place +static void touch(Tex *e); + +static uint32_t pool_ref(Tex *e) { + if (e->ph) return (uint32_t)e->ph; + int i; + if (pref_free) { + i = pref_free - 1, pref_free = pref[i].next; + } else { + if (npref == cpref) { + int n = cpref ? cpref * 2 : 1024; + PRef *q = realloc(pref, sizeof *q * (size_t)n); + if (!q) rt_fatal("vk: out of memory for the texture pool's entry counts"); + pref = q, cpref = n; + } + i = npref++; + } + pref[i] = (PRef){e, 0, 0, e->pub.gpu_owned}; + return (uint32_t)(e->ph = i + 1); +} + +void vk_texture_pool_hold_locked(uint32_t old, uint32_t ent) { + if (ent) pref[ent - 1].refs++; + if (old && !--pref[old - 1].refs && !pref[old - 1].e) pref[old - 1].next = pref_free, pref_free = (int)old; +} + +static void pool_forget(Tex *e) { // destroy_tex + if (!e->ph) return; + PRef *q = &pref[e->ph - 1]; + q->e = NULL; + if (!q->refs) q->next = pref_free, pref_free = e->ph; + e->ph = 0; +} + +static void pool_touch(void) { + static int at; + for (int i = 0; i < npref; i++) + if (pref[i].gown && pref[i].refs && pref[i].e) touch(pref[i].e); + for (int k = (npref + POOL_TOUCH_DIV - 1) / POOL_TOUCH_DIV; k-- > 0;) { + if (at >= npref) at = 0; + const PRef *q = &pref[at++]; + if (!q->gown && q->refs && q->e) touch(q->e); + } +} + +// e becomes GPU-owned: the list's check does nothing for it any more, and its use stamp is left in each submission. +static void pool_gpu_owned(Tex *e) { + pool_unlist(e); + if (e->ph) pref[e->ph - 1].gown = 1; +} + +int vk_texture_pool_note_locked(int id, uint32_t *ent) { + *ent = 0; + if (id < 0 || id >= ntslots || !tslots[id].used) return 0; + // The caller's lookups left the slot resolved under its bucket's generation, unless it made none (a fill without a + // token or a kind): an entry cached under another generation may be destroyed, so the slot is looked up first. + if (tslots[id].ce && tslots[id].cgen != agen[sinfo[id] & 0xffff]) vk_texture_by_id_locked(id, NULL, NULL); + Tex *e = tslots[id].ce; + if (!e) return 0; + if (!e->pidx && e->pub.mirrored && !e->pub.gpu_owned) { + if (npent == cpent) { + int n = cpent ? cpent * 2 : 1024; + PEnt *p = realloc(pent, sizeof *p * (size_t)n); + int *q = realloc(pq, sizeof *q * (size_t)n); + if (!p || !q) rt_fatal("vk: out of memory for the texture pool's entry list"); + pent = p, pq = q, cpent = cpq = n; + } + pent[npent++] = (PEnt){e, e->pub.addr, e->t.L.size, 0, 0}; + e->pidx = npent; + } + *ent = pool_ref(e); + return 1; +} + +// Released entries are left out: a slot resolves to none of those whose content the CPU provides (slot_resolve's stale +// rule), and the game may be unmapping their storage. +// NX2NAVI_POOL_PRESNAP=1 (default), 0, ab: the check takes the write-watch snapshot of an entry's regions that have +// none in this submission yet before it looks at them (one exclusive hold of the generation table's lock for the list), +// so an unchanged entry in such a region passes there. Without it 97 % of the 5,600 entries missed the region check for +// want of a snapshot and went the long way, one entry and one lock at a time. With it 97 % pass, 3.5 snapshot queries +// a submission: the check 0.50 to 0.19 ms a submission, the replay thread -0.37 ms against the control. +static int pool_presnap(void) { + static int mode = -1; + if (mode < 0) mode = rt_knob("POOL_PRESNAP", "1"); + return RT_KNOB_ON(mode); +} + +// The submission being recorded checked the pool's entries by this list: for the idle clock it walked the pool +// (vk_texture_new_submit). +static int pool_by_list; + +int vk_texture_pool_fresh_locked(void) { + int nq = 0, ps = pool_presnap(); + pool_by_list = 1; + if (ps) vk_mem_gen_lock(); + else vk_mem_gen_lock_shared(); + for (int i = 0; i < npent; i++) { + const PEnt *p = &pent[i]; + if (p->ok) { + if (ps) n_presnap_q += vk_mem_snap_range_locked(p->addr, p->size, submit_epoch); + int why = vk_mem_unchanged_why_locked(p->addr, p->size, p->since, submit_epoch); + if (!why) continue; + n_pool_miss[why]++; + } else { + n_pool_miss[0]++; + } + pq[nq++] = i; + } + if (ps) vk_mem_gen_unlock(); + else vk_mem_gen_unlock_shared(); + n_pool_skips += (uint64_t)(npent - nq); + for (int k = nq; k-- > 0;) { // from the end: an entry leaving swaps the last one into its place + int i = pq[k]; + PEnt *p = &pent[i]; + Tex *e = p->e; + if (e->pub.gpu_owned) { + pool_unlist(e); + continue; + } + if (e->released) continue; + if (!e->uploaded || e->checked != submit_epoch) { + make_fresh_snap(e); + e->checked = submit_epoch; + n_pool_checks++; + } + p->since = e->wgen, p->ok = e->uploaded; + } + pool_touch(); + return npent; +} + +// slot_resolve's last answer was none because of its stale rule (vk_texture_slot_retry_locked) +static int resolve_stale; + +int vk_texture_slot_retry_locked(int id) { + if (id < 0 || id >= ntslots || !tslots[id].used || !tslots[id].t.mem) return -1; + resolve_stale = 0; + if (vk_texture_by_id_locked(id, NULL, NULL)) return 1; + return resolve_stale ? -1 : 0; +} + static void retire_remove(Tex *e); static void destroy_tex(Tex *e) { if (e->retiring) retire_remove(e); + pool_unlist(e); + pool_forget(e); own_remove(e); gwin_remove(e); n_site[0]++; @@ -1186,6 +1470,10 @@ static int comes_back_intact(const Tex *e) { return e->pub.mirrored && !e->pub.g // texture object, nothing samples it. The clock is look_round, which only a submission whose slot checks reached half // of the registered pool slots moves on (vk_texture_new_submit): counted in submissions, a pause screen or a fade that // draws nothing through the pool made the whole world's textures idle within seconds. +// With NX2NAVI_POOL_FAST the refresh checks the entries by list instead (vk_texture_pool_fresh_locked), which is no +// lookup. Such a submission counts for the clock all the same, and the entries the pool's slots hold get their stamp +// from the count of those slots (pool_touch), each at least once in POOL_TOUCH_DIV submissions: an entry a slot names +// never comes near IDLE_ROUNDS. #define IDLE_ROUNDS 240 static uint32_t look_round = 1; // submissions whose lookups walked the texture pool static uint32_t round_checks; // slot checks of the submission being recorded (vk_texture_by_id_locked) @@ -1198,6 +1486,7 @@ static void touch(Tex *e) { } void vk_texture_pool_ctx(int on) { pool_ctx = on; } + static int idle(const Tex *e, uint64_t now); // NX2NAVI_TEX_RELEASED_MB (default a thirty-second of the installed memory, 512 MiB to 2 GiB; 0 = no limit) bounds the @@ -1552,6 +1841,7 @@ static uint64_t evict_disused(uint64_t want) { } retiring[nretiring++] = e; e->retiring = 1, e->retire_takes = takes, e->retire_due = 0; + pool_unlist(e); // no slot resolves to it in the pool refresh; taken back, the refill of its slots lists it again retiring_bytes += e->bytes; freed += e->bytes; tex_gen_bump(e->key.addr); @@ -2336,6 +2626,7 @@ static void start_at(Tex *e, const TexKey *k, int taken_back) { if (e->pub.mirrored) { // the content is src's GPU render, newer than the storage bytes vk_mem_changed(e->pub.addr, e->t.L.size, 0, &e->wgen); e->uploaded = 1; + pool_gpu_owned(e); e->pub.gpu_owned = 1; } e->chash_ok = 0; @@ -2491,6 +2782,7 @@ void vk_texture_gpu_written(const tex_t *t) { e->scan_frame = now + 1; overwrite_others(e, now); } + pool_gpu_owned(e); e->pub.gpu_owned = 1; e->chash_ok = 0; // the image now holds the GPU's content: matching storage bytes no longer mean "unchanged" if (e->pub.skip) { // texture detail leaves out what the GPU can write (detail_skip); said if it ever does @@ -2578,10 +2870,11 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { if (!sinfo) { uint64_t *p = calloc(1 << 20, sizeof *p); slot_next = malloc(sizeof *slot_next << 20), slot_prev = malloc(sizeof *slot_prev << 20); + slot_grp = malloc(sizeof *slot_grp << 20); slot_bucket = calloc(1 << 20, sizeof *slot_bucket); - if (!p || !slot_next || !slot_prev || !slot_bucket) rt_fatal("vk: out of memory for texture slot tokens"); - for (int i = 0; i < (1 << 20); i++) slot_prev[i] = -2, slot_next[i] = -1; - for (int i = 0; i < AGEN_N; i++) bucket_head[i] = -1; + if (!p || !slot_next || !slot_prev || !slot_grp || !slot_bucket) rt_fatal("vk: out of memory for texture slot tokens"); + for (int i = 0; i < (1 << 20); i++) slot_prev[i] = -2, slot_next[i] = -1, slot_grp[i] = -1; + for (int i = 0; i < AGEN_N; i++) bucket_head[i] = -1, bucket_ghead[i] = -1; __atomic_store_n(&sinfo, p, __ATOMIC_RELEASE); } TexSlot *s = &tslots[id]; @@ -2607,7 +2900,6 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { slots_gen++; if (!++reg_serial) reg_serial = 1; __atomic_store_n(&sinfo[id], (uint64_t)reg_serial << 16 | agen_idx((uintptr_t)t->mem), __ATOMIC_RELEASE); - slot_rebucket(id, agen_idx((uintptr_t)t->mem)); dirty_push((uint32_t)id); s->has_view = v != NULL; if (v) s->v = *v; @@ -2623,6 +2915,7 @@ void vk_texture_register(int id, const tex_t *t, const view_t *v) { for (size_t i = 0; i < sizeof s->v; i++) h = (h ^ b[i]) * 0x100000001b3ull; s->khash = (h ^ s->has_view) | 1; } + slot_place(id, agen_idx((uintptr_t)t->mem), s->khash); unlock(); } @@ -2689,6 +2982,7 @@ static Tex *slot_resolve(TexSlot *s, view_t *view, int *has_view) { else if (!e && !cpu_slot) e = own && own->superseded ? NULL : own; // a superseded one: entry_of takes it back else if (!e && cpu_slot && s->finalized) stale = 1; } + resolve_stale = stale; if (!e && s->t.mem && !stale) { // evicted by disuse: the pool refresh keeps the stand-in, without spending the creation budget on it if (pool_ctx && holds.n && nvn_hm_get(&holds, &k)) resolve_held = 1; @@ -2757,7 +3051,27 @@ void vk_texture_bucket_stats(char *out, size_t n) { (unsigned long long)n_site[2], (unsigned long long)n_site[3], (unsigned long long)n_undone, (unsigned long long)hot, occ[top[0]], occ[top[1]], occ[top[2]], occ[top[3]], bump_count[top[0]], bump_count[top[1]], bump_count[top[2]], bump_count[top[3]]); - n_bumps = n_undone = 0; + size_t used = strlen(out); + if (used < n) + snprintf(out + used, n - used, "; pool freshness check: %llu entries checked, %llu passed by their regions (missed: " + "%llu not uploaded, %llu a region without a snapshot yet, %llu untracked, %llu written since, %llu " + "nothing to check; %llu snapshot queries of NX2NAVI_POOL_PRESNAP)", + (unsigned long long)n_pool_checks, (unsigned long long)n_pool_skips, + (unsigned long long)n_pool_miss[0], (unsigned long long)n_pool_miss[1], + (unsigned long long)n_pool_miss[2], (unsigned long long)n_pool_miss[3], + (unsigned long long)n_pool_miss[4], (unsigned long long)n_presnap_q); + used = strlen(out); + if (used < n) + snprintf(out + used, n - used, "; %d slot groups holding %llu slots", ngrp_live, (unsigned long long)n_grouped); + int held = 0, gown = 0; + for (int i = 0; i < npref; i++) + if (pref[i].refs && pref[i].e) held++, gown += pref[i].gown; + used = strlen(out); + if (used < n) + snprintf(out + used, n - used, "; pool slots hold %d entries, %d of them GPU-owned (stamped in each submission " + "checked by list)", held, gown); + n_bumps = n_undone = n_pool_checks = n_pool_skips = n_presnap_q = 0; + memset(n_pool_miss, 0, sizeof n_pool_miss); memset(n_site, 0, sizeof n_site); memset(bump_count, 0, sizeof bump_count); unlock(); @@ -2800,8 +3114,9 @@ const VkTexImage *vk_texture_by_id_locked(int id, view_t *view, int *has_view) { s->cheld = (uint8_t)(!s->ce && resolve_held); } Tex *e = s->ce; - // a draw binding: the texture is in use, whether or not the pool walk checked it first in this submission - if (e && !pool_ctx) e->samp_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + // a draw binding: the texture is in use, whether or not the pool's refresh checked it first in this submission + // (by list that check is no lookup, vk_texture_pool_fresh_locked, so the stamp of the use is left here too) + if (e && !pool_ctx) touch(e); // Bindings of one submit share one check, answered from the submit's write-watch snapshot (guest writes during a // replay race the GPU read on hardware too). After one check an entry is either uploaded or never uploads (not // mirrored, GPU-owned or no CPU layout), so a slot that checked its entry in this submit has nothing left to do. A @@ -2858,8 +3173,9 @@ uint32_t vk_texture_epoch(void) { return __atomic_load_n(&submit_epoch, __ATOMIC void vk_texture_new_submit(void) { lock(); - if (round_checks && (uint64_t)round_checks * 2 >= pool_slots) look_round++; // the last one walked the pool - round_checks = 0; + // the last one walked the pool, or checked its entries by list + if (pool_by_list || (round_checks && (uint64_t)round_checks * 2 >= pool_slots)) look_round++; + round_checks = 0, pool_by_list = 0; if (!++submit_epoch) submit_epoch = 1; rate_tick(); sweep_pressure(); From be136d8d3b797f5191b014cc3a905dcd33a5ad3d Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 14:18:02 -0500 Subject: [PATCH 021/153] Credit a sampled texture through the slots that follow it, and take back a retired texture that cannot be destroyed. A mark on a pool slot without an entry of its own went to the slot's own key, so a texture sampled only through a second descriptor at its address counted as unsampled, was retired after two minutes under memory pressure, and a mark on its stand-in could not take it back. Where the pool's syncs kept leaving slots behind, a retired texture was never destroyed either and its slots kept the stand-in: on a card with 6 GB the ground of the field was drawn without its textures and its height. The mark now goes to the textures the slot follows, and a retired texture that is not due after five seconds is taken back. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_texture.c | 47 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 261c3fde..473147ed 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -54,6 +54,7 @@ typedef struct Tex { // other than the texture pool refresh reached it ("disuse") uint8_t retiring; // evicted by disuse, in retiring[], not destroyed yet: the pool refresh leaves it out uint64_t retire_takes; // vk_pool_takes when it was retired + uint64_t retire_frame; // gfx_counters.frames when it was retired uint64_t retire_due; // vk_sync_issued once every pool set was taken since (0: not yet); destroyed after it int uploaded, released; uint64_t uid; // names the entry in its bucket's released set (tex_gen_flip); 0 until its first release @@ -1460,6 +1461,8 @@ static uint32_t marks_live; static int resolve_held; // slot_resolve left the slot out for a held key or a retired entry static uint64_t n_disuse, n_disuse_bytes, n_disuse_back; // since the last perf line static uint64_t n_retire_cancel, n_retire_freed; // since the last perf line +static uint64_t n_retire_stalled; // retired textures taken back because they never came due +#define RETIRE_PATIENCE PRESENTS(5 * 60) static int disuse_logs; static Tex **retiring; // retired entries not destroyed yet static int nretiring, cretiring; @@ -1486,11 +1489,22 @@ static void retire_sweep(void) { static int keep = -1; if (keep < 0) keep = rt_knob("TEX_DISUSE", "1") == 2; uint64_t takes = vk_pool_takes(), done = vk_sync_fence_done(); + uint64_t now = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); int k = 0; for (int i = 0; i < nretiring; i++) { Tex *e = retiring[i]; if (!e->retiring) continue; // taken back if (!e->retire_due && takes - e->retire_takes > VK_POOL_SETS) e->retire_due = vk_sync_issued(); + // The takes that count are those of syncs that left no slot behind, and a PC that has no creation time over + // in most frames leaves some behind in nearly every sync: there a retired texture was never destroyed and its + // slots showed the stand-in for as long as that lasted, minutes in one report. One that is still not due + // after RETIRE_PATIENCE presents is taken back and counts as sampled now. + if (!e->retire_due && now - e->retire_frame > RETIRE_PATIENCE) { + retire_cancel(e); + e->samp_frame = now; + n_retire_stalled++; + continue; + } if (!keep && e->retire_due && done >= e->retire_due) { e->retiring = 0, retiring_bytes -= e->bytes; nvn_hm_put(&holds, &e->key, (void *)(uintptr_t)1); @@ -1501,6 +1515,15 @@ static void retire_sweep(void) { retiring[k++] = e; } nretiring = k; + static uint64_t stalled_logged; + static int stalled_logs; + if (n_retire_stalled != stalled_logged && stalled_logs < 8) { + stalled_logs++; + rt_log("vk: %llu textures evicted as unsampled were taken back: the texture pool had not been brought up to " + "date completely in %d s, so they could not be destroyed and their slots showed stand-ins", + (unsigned long long)(n_retire_stalled - stalled_logged), 5); + stalled_logged = n_retire_stalled; + } } // A lookup of key k that found no entry: 1 when the pool refresh asks for a held key (it gets the stand-in). Any other @@ -1560,7 +1583,7 @@ static uint64_t evict_disused(uint64_t want) { retiring = r, cretiring = c; } retiring[nretiring++] = e; - e->retiring = 1, e->retire_takes = takes, e->retire_due = 0; + e->retiring = 1, e->retire_takes = takes, e->retire_due = 0, e->retire_frame = now; retiring_bytes += e->bytes; freed += e->bytes; tex_gen_bump(e->key.addr); @@ -1593,10 +1616,28 @@ void vk_pool_marks_read(const uint32_t *marks, uint32_t n) { TexKey k; make_key(&s->t, &k); Tex *e = nvn_hm_get(&by_key, &k); - if (e) { + if (e && !e->released) { if (e->retiring) retire_cancel(e); // retired, and its stand-in was sampled: back as it is e->samp_frame = now; - } else if (holds.n && nvn_hm_del(&holds, &k)) { // the stand-in was sampled: the next pool refresh makes it + continue; + } + // The slot has no entry of its own: it samples another descriptor's image at its address (slot_resolve follows + // one of the same target, as a second descriptor with another swizzle does). That image is what was sampled. + // With the mark given to the slot's own key alone, a texture sampled only through such slots was never noted + // as sampled while the slots' cached resolution was out of date, which it is at most reads in a bucket the + // game keeps changing: it was retired as unused after two minutes, and a mark on its stand-in could not take + // it back. While the pool's syncs kept leaving slots behind, a retired texture was never destroyed either + // (retire_sweep waits for complete takes), so its slots kept the stand-in: the ground of the field drawn + // without its textures and its height. + int followed = 0; + for (Tex *p = nvn_hm_get(&by_addr, &k.addr); p; p = p->next_at) { + if (p->released || p->key.d.target != k.d.target) continue; + if (p->retiring) retire_cancel(p); + p->samp_frame = now; + followed = 1; + } + if (followed) continue; + if (holds.n && nvn_hm_del(&holds, &k)) { // the stand-in was sampled: the next pool refresh makes it n_disuse_back++; s->cheld = 0; s->cwant = 1; // on screen as a stand-in: made at the next refresh, whatever the frame's creation time From 834b36a6d95ea9b2dfd28994069a6a46e2c7a94b Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 14:22:52 -0500 Subject: [PATCH 022/153] Stamp a retired texture as used when it is taken back. While retired it got no use stamp, so it counted as idle the moment it came back, and a sweep over the video memory budget could evict it as idle in the same call that took it back: on the cards that are over budget, which are the ones the take-back is for. Co-Authored-By: Claude Fable 5.2 --- runtime/vk_texture.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 473147ed..2a70311f 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -1474,6 +1474,11 @@ static void retire_cancel(Tex *e) { retiring_bytes -= e->bytes; if (holds.n) nvn_hm_del(&holds, &e->key); n_retire_cancel++, n_disuse_back++; + // In use as of now: while retired the pool refresh left it out, so it carries no use stamp of that time and + // counted as idle the moment it came back. A sweep over the budget (sweep_pressure calls retire_sweep and then + // evict_idle) could evict it in the same call that took it back. + e->use_round = look_round; + e->use_frame = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); tex_gen_bump(e->key.addr); } From 006bdfa4b2467c264f1c7272bbc4703491e77462 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 14:52:13 -0500 Subject: [PATCH 023/153] Let the launcher's bug report export write the game's log and settings when the game has no save yet. A game that stops before its first save is the one its player most wants to report, and Export Save refused it: "the game has no save yet". With Add Log and Settings on (the default) the launcher now writes NX2navi-log-_