From 23d24bbc773c3139a6d443cf9451e1f0f4041006 Mon Sep 17 00:00:00 2001 From: ipavlin98 Date: Tue, 3 Nov 2026 23:15:59 +0300 Subject: [PATCH 01/30] Report heap corruption from the vectored handler The heap manager raises STATUS_HEAP_CORRUPTION and then ends the process itself, so the exception never reaches the unhandled-exception filter, and a run that corrupted its heap ended without a FATAL line. crash_veh hands it to crash_filter, which names the module and the host frames as for any other fault outside the executable. --- runtime/platform/win32.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index c4aca224..61caff59 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -626,8 +626,13 @@ void plat_mem_fault_hook(int (*fn)(uint64_t addr)) { static void (*crash_fn)(const PlatCrash *c); +static LONG WINAPI crash_filter(EXCEPTION_POINTERS *ep); + static LONG CALLBACK crash_veh(EXCEPTION_POINTERS *ep) { DWORD code = ep->ExceptionRecord->ExceptionCode; + // Heap corruption is raised by the heap manager, which then ends the process itself: it never reaches the + // unhandled-exception filter, so it is reported from here, wherever the heap was called from. + if (code == 0xc0000374u) return crash_filter(ep); // STATUS_HEAP_CORRUPTION const char *what = code == EXCEPTION_ACCESS_VIOLATION ? "access violation" : code == EXCEPTION_ILLEGAL_INSTRUCTION ? "illegal instruction" : code == EXCEPTION_INT_DIVIDE_BY_ZERO ? "integer division by zero" From cd32de5ff9a1b29b049a7fb7db701224aa904d8f Mon Sep 17 00:00:00 2001 From: Michael Anthony Bitoon Date: Wed, 4 Nov 2026 20:01:27 +0800 Subject: [PATCH 02/30] Keep DLSS off the inventory screen and on at every camera angle, with the scope or the camera rune open The inventory's preview light target is drawn without depth before the bloom chain; a pass like that with nothing armed ends the scene once the bloom follows, so DLSS no longer runs on the finished menu picture. Arming on that target stays allowed, because at some camera angles the field's effects composite comes before the first pass with the scene depth. The G-buffer width is the widest of the frame, a small pass ends the scene only into a scene colour target, and no pair narrower than the widest G-buffer arms: with the scope open the half-size effects pass armed and DLSS scaled the effects into the light target. --- runtime/vk_dlaa.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/runtime/vk_dlaa.c b/runtime/vk_dlaa.c index db375da9..eb8a8476 100644 --- a/runtime/vk_dlaa.c +++ b/runtime/vk_dlaa.c @@ -289,6 +289,8 @@ int vk_dlaa_available(void) { return vk_dlaa_wanted() && N.available; } static struct { VkExecTarget color, depth; // the armed pair, valid in the frame it was armed int armed, done, gbuf; // this frame: armed on the light target, DLAA ran, the G-buffer was drawn + uint32_t gbuf_w; // the widest G-buffer's width this frame + VkImage nodepth; // this frame: a G-buffer-size target drawn without depth while nothing was armed int ended, up; // this frame: the scene is drawn (jitter over), DLSS wrote the scaled output struct Size { // a DLSS feature and its work images per scene size (dynamic resolution flips it) uint32_t w, h, ow, oh; // render size, output size @@ -1116,6 +1118,7 @@ void vk_dlaa_pass(VkExec *ex) { // arms the frame; at night the game draws into the light target alone with the scene depth before the G-buffer too, // and DLAA then ran at a small pass ahead of the whole scene, which reached the screen jittered. if (ex->pass_ncolor >= 3 && z->image && z->format == VK_FORMAT_D32_SFLOAT && z->width >= DLAA_SCENE_MIN_W) { + D.gbuf_w = D.gbuf && D.gbuf_w > z->width ? D.gbuf_w : z->width; D.gbuf = 1, D.armed = 0; // target 0 is the object-class buffer (RGBA8, the scene size): the characters' bias mask reads its green. A // smaller pass of the same shape (640x360, seen while loading) does not replace the scene's in the same frame. @@ -1124,22 +1127,48 @@ void vk_dlaa_pass(VkExec *ex) { D.ids = *c, D.ids_ok = 1; return; } + // A pass into one scene colour target of the G-buffer's size or larger without depth while nothing is armed ends + // the scene once the bloom chain follows with still nothing armed: in the inventory (1.4.3) the preview's light + // target is drawn that way before the bloom, and with the scene depth only after it, while the menu is composed; + // DLAA ran on the finished menu picture with the previous frame's history, which left stray dark pixels around Link. + // Arming cancels the end, on that target too: at some camera angles the field's effects composite (a full-screen + // draw without depth into the light target) comes before the first pass with the scene depth, and with that target + // barred from arming DLSS stopped for as long as the view held (in the inventory the bloom chain always ended the + // scene first, in both modes, menu opening included). While the scope zooms in, a second G-buffer pass disarms the + // frame, the scene is copied into an output-sized target without depth, and only then is the light target drawn + // again with the scene depth. + // The G-buffer's width is the widest of the frame: the game also draws a half-size G-buffer, after which a + // half-size pass without depth ended the scene at some camera angles. + if (D.gbuf && !D.armed && !D.ended && !z->image && ex->pass_ncolor == 1 && c->image && scene_color(c->format) && + c->width >= D.gbuf_w && !c->level) + D.nodepth = c->image; + if (D.nodepth && !D.armed && !D.ended && ex->pass_w && ex->pass_w * 4 <= D.gbuf_w && c->image && + scene_color(c->format)) + D.ended = 1; // the scene depth is D32F; a D16 target of the scene size also serves one composite pass before the transparents. // A smaller pair never takes over an armed one: the camera rune draws a half-size RGBA16F target with a half-size // D32F depth after the lighting (852x480 under a 1704x960 scene), and DLSS ran on that and wrote its picture into - // the scene's own light target, which left every object black. - if (D.gbuf && ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT && - c->width == z->width && c->height == z->height && c->width >= DLAA_SCENE_MIN_W && + // the scene's own light target, which left every object black. Nor does a pair narrower than the frame's widest + // G-buffer arm at all: the game draws its particles into a half-size RGBA16F target with a half-size D32F depth, and + // with the scope open, where the light target is drawn with a D16 depth and never arms, that effects pass was the + // first pair to arm; DLSS then scaled the effects into the light target, which filled the lens with garbage in the + // DLAA mode. + if (D.gbuf && !D.ended && ex->pass_ncolor == 1 && c->image && scene_color(c->format) && z->image && z->format == VK_FORMAT_D32_SFLOAT && + c->width == z->width && c->height == z->height && c->width >= DLAA_SCENE_MIN_W && c->width >= D.gbuf_w && (!D.armed || (c->width >= D.color.width && c->height >= D.color.height)) && c->samples <= 1 && !c->is3d) { int first = !D.armed; - D.color = *c, D.depth = *z, D.armed = 1; + D.color = *c, D.depth = *z, D.armed = 1, D.nodepth = VK_NULL_HANDLE; if (first) mask_snapshot(ex); // the lit scene, before the transparents (reactive mask) return; } if (!D.armed) return; if (!D.ended) { - if (!ex->pass_w || ex->pass_w * 4 > D.color.width) return; + // The bloom chain's first pass ends the scene: a pass at most a quarter of the scene's width into a scene + // colour target. With the camera rune or the scope open the game draws a 160-wide depth-only pass and 160x90 + // RG16F and RGBA8 passes right after the lighting, then more geometry and the transparents; DLSS ended the + // scene at those, the next G-buffer pass disarmed it and the scaling pass found nothing to evaluate. + if (!ex->pass_w || ex->pass_w * 4 > D.color.width || !c->image || !scene_color(c->format)) return; D.ended = 1; unsigned o[2], s[2]; res_sizes(o, s); @@ -1196,6 +1225,7 @@ void vk_dlaa_frame(void) { D.on = on; // either way the next DLAA frame starts a new history from an unjittered frame D.reset = 1, D.cam_prev_ok = 0, D.jit_ok = D.jit_used = D.armed = D.done = D.gbuf = D.ended = D.up = 0; + D.nodepth = VK_NULL_HANDLE; D.hold = 0; return; } @@ -1204,7 +1234,7 @@ void vk_dlaa_frame(void) { if (D.jit_used && !D.done) D.hold = 60, D.holds++; // jittered but not resolved: that frame shook else if (D.hold) D.hold--; if (!D.done) D.jit_ok = 0; - D.armed = D.done = D.jit_used = D.gbuf = D.ended = D.up = D.ids_ok = 0; + D.armed = D.done = D.jit_used = D.gbuf = D.ended = D.up = D.ids_ok = 0, D.nodepth = VK_NULL_HANDLE; D.cam_addr = 0; } From 02752376f7a0b3dfde0499f7bcbe7f1b1d3402a9 Mon Sep 17 00:00:00 2001 From: corne-ac <127402474+corne-ac@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:01:05 +0200 Subject: [PATCH 03/30] Sync shadow pools from the per-submission write-watch snapshots Each 2 MiB region is now queried at most once per guest submission, shared with the texture checks, instead of once per pool at every part of a submission. On a Steam Deck at Lookout Landing the replay thread's write tracking fell from about 4.4 to 1.3 ms a frame and the frame rate rose about 7.5%. TOTK_SHADOW_SNAP=0 restores the old queries. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk_memory.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/settings.md b/docs/settings.md index 3fee78a3..182873fa 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -124,6 +124,7 @@ Many rows below are developer tools that only act together with another setting | `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution | | `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky | | `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution | +| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. No effect where shadow storage is off (Windows by default). | Caution | | `freeze_idle` (default `60`) | Number of submissions a 2 MiB region stays unwritten before it is made read-only; `0` turns the freeze off. | A larger value keeps more regions writable and costs more write tracking; a write to a frozen region costs about 60 microseconds. | Caution | | `gpl_draw_build` (default `1`) | `0` leaves the missing shader libraries of a waiting draw to its background job instead of building them in parallel at once. | Draws whose pipeline is not ready are skipped meanwhile, until the job finishes. | Caution | | `lib_prebuild_keep` (default `0`) | Number of recorded graphics shader library variants that stay in memory after the boot prebuild. | Each kept library costs 140-460 KB of memory; the driver cache already makes a rebuild cheap. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index a660c26c..396106d1 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -56,7 +56,7 @@ static const char *const known[] = { "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", - "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", + "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHADOW_SNAP", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 3e30682f..449f6b02 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -953,9 +953,40 @@ static void shadow_copy(PoolRec *r, uint64_t a, uint64_t b, int i) { s_copied += b - a; } +// TOTK_SHADOW_SNAP (default 1): a pool's sync takes its written pages from the per-submission write-watch snapshots +// (snap_regions, shared with the texture checks): each 2 MiB region is queried at most once per guest submission, however +// many pools share it and however many parts of the submission touch them. The GPU then reads a pool as of the first +// part that syncs it, nearer the submit call, the moment the game means (vk_usnap.c). 0 queries each pool at every +// sync, as before. Measured on a Steam Deck at Lookout Landing: 716 pool syncs and 729 queries a frame took 4.8 ms of +// the replay thread to copy 2.3 MiB, the query walk costing about 4 us per MiB there; with the snapshots, about 58 +// queries and 1.7 ms for the same bytes. +static int shadow_snap = -1; +static void shadow_copy_newer(PoolRec *r, uint32_t since); + // shadow_lock and gen_lock held: stamps the pool's written pages and copies those newer than its shadow. static void shadow_sync_pool(PoolRec *r) { uint64_t lo = r->import_lo, hi = r->import_hi; + if (shadow_snap < 0) { + shadow_snap = rt_knob("SHADOW_SNAP", "1"); + rt_log("mem: shadow sync: %s (TOTK_SHADOW_SNAP)", + shadow_snap ? "per-submission region snapshots" : "a query per pool at every part"); + } + uint32_t epoch = shadow_snap ? vk_texture_epoch() : 0; + uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; + if (epoch && r1 < SNAP_REGIONS) { + s_queries += snap_regions(r0, r1, epoch); // stamps what the snapshot found, if the region was not seen yet + int untracked = 0; + for (uint64_t k = r0; k <= r1; k++) untracked |= snap_untracked[k]; + uint32_t since = r->sgen; + r->sgen = gen_now; + if (untracked) { // nothing tells which pages changed: all of them + s_untracked++; + shadow_copy(r, lo, hi, 0); + return; + } + shadow_copy_newer(r, since); + return; + } uint32_t g = ++gen_now; int tracked = 1; for (uint64_t a = lo; a < hi;) { // one query per run of regions that are not frozen @@ -974,6 +1005,12 @@ static void shadow_sync_pool(PoolRec *r) { shadow_copy(r, lo, hi, 0); return; } + shadow_copy_newer(r, since); +} + +// shadow_lock and gen_lock held: copies the pool's pages stamped after since into its shadow, in runs. +static void shadow_copy_newer(PoolRec *r, uint32_t since) { + uint64_t lo = r->import_lo, hi = r->import_hi; uint64_t run = 0; for (uint64_t k = lo >> REG_SHIFT; k <= (hi - 1) >> REG_SHIFT; k++) { uint64_t a = k << REG_SHIFT, b = a + (1ull << REG_SHIFT); From a733b946459a7926b803dafed148229871e8e082 Mon Sep 17 00:00:00 2001 From: corne-ac <127402474+corne-ac@users.noreply.github.com> Date: Sat, 7 Nov 2026 22:20:03 +0200 Subject: [PATCH 04/30] Keep shadow_snap off by default under Wine until it has been measured there Shadow storage is on by default under Wine as well, and the region snapshots were only measured on a native Linux host. TOTK_SHADOW_SNAP=1 still turns them on under Wine. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/vk_memory.c | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 182873fa..9c7feca6 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -124,7 +124,7 @@ Many rows below are developer tools that only act together with another setting | `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution | | `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky | | `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution | -| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. No effect where shadow storage is off (Windows by default). | Caution | +| `shadow_snap` (default `1`) | Shadow storage (Linux, Wine): `1` syncs the pools from per-submission write-watch snapshots, each 2 MiB region queried at most once per submission and shared with the texture checks; `0` queries each pool at every part of a submission, as before. | `1` cut the replay thread's write tracking from about 4.4 to 1.3 ms a frame on a Steam Deck; `0` is for comparison runs. Under Wine/Proton the default is `0` until it has been measured there; `1` turns it on. No effect where shadow storage is off (Windows by default). | Caution | | `freeze_idle` (default `60`) | Number of submissions a 2 MiB region stays unwritten before it is made read-only; `0` turns the freeze off. | A larger value keeps more regions writable and costs more write tracking; a write to a frozen region costs about 60 microseconds. | Caution | | `gpl_draw_build` (default `1`) | `0` leaves the missing shader libraries of a waiting draw to its background job instead of building them in parallel at once. | Draws whose pipeline is not ready are skipped meanwhile, until the job finishes. | Caution | | `lib_prebuild_keep` (default `0`) | Number of recorded graphics shader library variants that stay in memory after the boot prebuild. | Each kept library costs 140-460 KB of memory; the driver cache already makes a rebuild cheap. | Caution | diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 449f6b02..c4cd6065 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -953,7 +953,7 @@ static void shadow_copy(PoolRec *r, uint64_t a, uint64_t b, int i) { s_copied += b - a; } -// TOTK_SHADOW_SNAP (default 1): a pool's sync takes its written pages from the per-submission write-watch snapshots +// TOTK_SHADOW_SNAP (default 1 on a native host, 0 under Wine, where it has not been measured): a pool's sync takes its written pages from the per-submission write-watch snapshots // (snap_regions, shared with the texture checks): each 2 MiB region is queried at most once per guest submission, however // many pools share it and however many parts of the submission touch them. The GPU then reads a pool as of the first // part that syncs it, nearer the submit call, the moment the game means (vk_usnap.c). 0 queries each pool at every @@ -967,9 +967,10 @@ static void shadow_copy_newer(PoolRec *r, uint32_t since); static void shadow_sync_pool(PoolRec *r) { uint64_t lo = r->import_lo, hi = r->import_hi; if (shadow_snap < 0) { - shadow_snap = rt_knob("SHADOW_SNAP", "1"); - rt_log("mem: shadow sync: %s (TOTK_SHADOW_SNAP)", - shadow_snap ? "per-submission region snapshots" : "a query per pool at every part"); + shadow_snap = rt_knob("SHADOW_SNAP", plat_is_wine() ? "0" : "1"); + rt_log("mem: shadow sync: %s (TOTK_SHADOW_SNAP%s)", + shadow_snap ? "per-submission region snapshots" : "a query per pool at every part", + plat_is_wine() ? "; off by default under Wine" : ""); } uint32_t epoch = shadow_snap ? vk_texture_epoch() : 0; uint64_t r0 = lo >> SNAP_SHIFT, r1 = (hi - 1) >> SNAP_SHIFT; From 2d92565ada57398b1347ed28f98ab48711d0b0c6 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 8 Nov 2026 11:05:33 +0300 Subject: [PATCH 05/30] Poll briefly before kernel waits and the recorder sleep Guest waits and waits for a virtual core usually end within microseconds. A thread now polls for 20 us with the global lock released before it sleeps on its condition variable, so a quick wake costs no OS wake-up (TOTK_KSPIN_US, 0 turns it off). The recorder polls by the clock for 60 us instead of 4096 pauses, which last a few us on Zen 4 and about 140 us on Coffee Lake (TOTK_REC_SPIN_US). Both stay off with fewer than 6 usable logical processors. Savage Wind Recomp polls the same way before it parks. Machine A, town save, in-run A/B: frame interval -1.8% on average for the kernel poll (5 of 7 block pairs), -0.7% with the process limited to 4 cores / 8 threads; the recorder poll is neutral there. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 4 ++-- runtime/kernel.c | 43 +++++++++++++++++++++++++++++++++++++++++++ runtime/vk_rec.c | 38 ++++++++++++++++++++++++++++++++++---- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index a660c26c..76c3279d 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -45,14 +45,14 @@ static const char *const known[] = { "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", - "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", + "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "KSPIN_US", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", - "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", + "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REC_SPIN_US", "REEXEC", "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", diff --git a/runtime/kernel.c b/runtime/kernel.c index e8fb48b4..00aaa963 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -246,6 +246,44 @@ static void wake_sync_waiters(void) { } } +// ---- a short poll before a sleep +// Most guest waits and waits for a virtual core end within microseconds. The thread polls for TOTK_KSPIN_US (default +// 20) with G released before it sleeps on its condition variable: a wake in that time costs no OS wake-up (4-12 us +// on a desktop Ryzen, more from a deep C-state) and the waker's signal finds nobody asleep. Off with fewer than 6 logical +// processors, where the poll would take a CPU that a running thread needs; TOTK_KSPIN_US=0 turns it off. Savage Wind +// Recomp polls the same way before it parks (runtime/ksync.c cond_wait). TOTK_KSPIN_US=ab polls 20 us in the +// B blocks of TOTK_AB only. +static int64_t g_kspin_ns = -1; +static int g_kspin_ab; + +static uint64_t kspin_ns(void) { + int64_t v = __atomic_load_n(&g_kspin_ns, __ATOMIC_RELAXED); + if (v < 0) { + const char *e = cfg_get("TOTK_KSPIN_US"); + g_kspin_ab = e && !strcmp(e, "ab"); + long long us = e && *e && !g_kspin_ab ? atoll(e) : 20; + uint32_t cpus = plat_cpu_usable(); + v = us <= 0 || cpus < 6 ? 0 : us * 1000; + __atomic_store_n(&g_kspin_ns, v, __ATOMIC_RELAXED); + rt_log("kernel: waits poll %lld us before they sleep%s (TOTK_KSPIN_US, %u logical processors)", + (long long)(v / 1000), g_kspin_ab ? " in the B blocks" : "", cpus); + } + return v && (!g_kspin_ab || rt_ab()) ? (uint64_t)v : 0; +} + +// Holding G: releases it and polls *p until it reaches at_least or ns pass, then takes G again. 1 if it was reached. +static int kspin(const int *p, int at_least, uint64_t ns) { + uint64_t end = kern_now_ns() + ns; + int ok = 0; + unlock(); + for (unsigned i = 1; !(ok = __atomic_load_n(p, __ATOMIC_ACQUIRE) >= at_least); i++) { + plat_pause(); + if (!(i & 31) && kern_now_ns() >= end) break; + } + lock(); + return ok; +} + // SleepConditionVariableSRW counts milliseconds: round up (late is legal, early is not) and re-check the deadline // against the performance counter, because the wait may also end early. static uint32_t block_inner(RtThread *t, int kind, int64_t ns) { @@ -262,6 +300,8 @@ static uint32_t block_inner(RtThread *t, int kind, int64_t ns) { uint64_t now = kern_now_ns(); int forever = ns < 0 || (uint64_t)ns > UINT64_MAX - now; uint64_t deadline = forever ? 0 : now + (uint64_t)ns; + uint64_t poll = kspin_ns(); + if (poll) kspin(&t->woke, 1, forever || poll < (uint64_t)ns ? poll : (uint64_t)ns); while (!t->woke) { if (forever) { plat_cond_wait(&t->cv, &G); @@ -991,6 +1031,9 @@ static void core_take(RtThread *t) { while (__atomic_load_n(&t->cur_core, __ATOMIC_RELAXED) < 0 && kern_now_ns() < end) plat_pause(); lock(); if (t->cur_core >= 0) g_ap_spin_ok++; + } else if (t->cur_core < 0) { // a holder may block or yield any moment (kspin_ns) + uint64_t poll = kspin_ns(); + if (poll) kspin(&t->cur_core, 0, poll); } int sampled = 0; while (t->cur_core < 0) { diff --git a/runtime/vk_rec.c b/runtime/vk_rec.c index 036fe370..0706dbcb 100644 --- a/runtime/vk_rec.c +++ b/runtime/vk_rec.c @@ -43,8 +43,11 @@ static PlatThread *rec_thread; static int rec_prio; // the submitting thread's priority, which the recorder takes static int rec_chk; // TOTK_REC_CHECK (rec_check_handle) static uint64_t rec_delay_ns; // TOTK_REC_DELAY_US (rec_main) +static uint64_t rec_spin_ns; // TOTK_REC_SPIN_US (rec_main); 0: 4096 pauses +static int rec_spin_ab; // TOTK_REC_SPIN_US=ab: the timed poll in the B blocks of TOTK_AB only static volatile uint64_t part_h0; // Q.put_h at rec_begin: where the packets of the part being recorded start static volatile uint64_t part_fence; // the value of the part being recorded (rec_begin) +static uint64_t r_sleeps; // recorder: sleeps on wake_ev, read by rec_stats_log // Producer statistics, logged by rec_stats_log. static struct { _Alignas(64) uint64_t packets, words, drains, fallbacks, drain_ns, full_waits, parts, parts_on, submits; } RS_; @@ -75,11 +78,27 @@ static void rec_main(void *arg) { for (;;) { uint64_t h = __atomic_load_n(&Q.head, __ATOMIC_ACQUIRE); if (t == h) { - for (int i = 0; i < 4096 && t == h; i++) _mm_pause(), h = __atomic_load_n(&Q.head, __ATOMIC_ACQUIRE); + // Poll for the next packets before sleeping: a sleep costs the producer an event set and the recorder + // an OS wake-up. The poll is timed (TOTK_REC_SPIN_US, default 60) because a pause takes 8-150 cycles + // depending on the CPU: 4096 of them are ~140 us on a Coffee Lake i7 and a few us on Zen 4. With fewer than + // 6 logical processors it stays 4096 pauses. Savage Wind Recomp's recorder polls 60 us the same way. + if (rec_spin_ns && (!rec_spin_ab || rt_ab())) { + uint64_t t0 = now_ns(); + for (unsigned i = 1; t == h; i++) { + _mm_pause(); + h = __atomic_load_n(&Q.head, __ATOMIC_ACQUIRE); + if (!(i & 31) && now_ns() - t0 >= rec_spin_ns) break; + } + } else { + for (int i = 0; i < 4096 && t == h; i++) _mm_pause(), h = __atomic_load_n(&Q.head, __ATOMIC_ACQUIRE); + } if (t == h) { __atomic_store_n(&Q.sleeping, 1, __ATOMIC_SEQ_CST); h = __atomic_load_n(&Q.head, __ATOMIC_SEQ_CST); - if (t == h) plat_event_wait_ns(&wake_ev, 2000000); // a missed wake costs at most 2 ms + if (t == h) { + __atomic_add_fetch(&r_sleeps, 1, __ATOMIC_RELAXED); + plat_event_wait_ns(&wake_ev, 2000000); // a missed wake costs at most 2 ms + } __atomic_store_n(&Q.sleeping, 0, __ATOMIC_RELAXED); continue; } @@ -200,6 +219,10 @@ void rec_begin(VkCommandBuffer cb, uint64_t fence, int crumb, uint64_t n) { rec_chk = rt_knob("REC_CHECK", "0"); // debug switches, read before the recorder starts const char *d = cfg_get("TOTK_REC_DELAY_US"); rec_delay_ns = d && *d && atoi(d) > 0 ? (uint64_t)atoi(d) * 1000 : 0; + const char *sp = cfg_get("TOTK_REC_SPIN_US"); + rec_spin_ab = sp && !strcmp(sp, "ab"); + int spin_us = sp && *sp && !rec_spin_ab ? atoi(sp) : 60; + rec_spin_ns = spin_us > 0 && plat_cpu_usable() >= 6 ? (uint64_t)spin_us * 1000 : 0; const char *pw = cfg_get("TOTK_REC_PUB"); // words per publish (pub); 0: every packet Q.pub_words = rec_chk ? 0 : pw && *pw ? (uint64_t)atoi(pw) : 64; if (Q.pub_words > RING_WORDS / 8) Q.pub_words = RING_WORDS / 8; @@ -211,6 +234,11 @@ void rec_begin(VkCommandBuffer cb, uint64_t fence, int crumb, uint64_t n) { rt_log("vk_rec: the replay's Vulkan commands are made by a recorder thread%s", mode == 2 ? " in the B blocks (TOTK_AB)" : ""); if (rec_chk) rt_log("vk_rec: TOTK_REC_CHECK: deferred and destroyed objects are searched in the ring"); + if (rec_spin_ns) + rt_log("vk_rec: the recorder polls %llu us before it sleeps%s (TOTK_REC_SPIN_US)", + (unsigned long long)(rec_spin_ns / 1000), rec_spin_ab ? " in the B blocks" : ""); + else + rt_log("vk_rec: the recorder polls 4096 pauses before it sleeps (TOTK_REC_SPIN_US)"); if (Q.pub_words != 64) rt_log("vk_rec: TOTK_REC_PUB: head published every %llu words", (unsigned long long)Q.pub_words); if (rec_delay_ns) rt_log("vk_rec: TOTK_REC_DELAY_US: the recorder waits %llu us before each batch", @@ -315,10 +343,12 @@ static void submit_run(const A_Submit *a) { void rec_stats_log(void) { if (!(rt_trace & RT_TRACE_PERF) || !RS_.parts) return; rt_log("vk_rec: %llu of %llu parts recorded by the recorder: %llu packets (%.1f MiB), %llu drains (%llu for a call " - "without a packet) waited %.2f ms in all, ring full %llu times, %llu submissions made by the recorder", + "without a packet) waited %.2f ms in all, ring full %llu times, %llu submissions made by the recorder, " + "recorder slept %llu times", (unsigned long long)RS_.parts_on, (unsigned long long)RS_.parts, (unsigned long long)RS_.packets, RS_.words * 8 / 1048576.0, (unsigned long long)RS_.drains, (unsigned long long)RS_.fallbacks, - RS_.drain_ns / 1e6, (unsigned long long)RS_.full_waits, (unsigned long long)RS_.submits); + RS_.drain_ns / 1e6, (unsigned long long)RS_.full_waits, (unsigned long long)RS_.submits, + (unsigned long long)__atomic_exchange_n(&r_sleeps, 0, __ATOMIC_RELAXED)); memset(&RS_, 0, sizeof RS_); } From 320a41e0dc66ff29904d753a6488a5538f382f79 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 8 Nov 2026 11:13:40 +0300 Subject: [PATCH 06/30] Ask hybrid laptops for the dedicated GPU and opt out of Windows power throttling The executable exports NvOptimusEnablement and AmdPowerXpressRequestHighPerformance, which NVIDIA Optimus and AMD switchable graphics read at start to run it on the dedicated GPU; Optimus can otherwise hide its GPU from a program it put on the integrated one. At start the process opts out of EcoQoS (E-cores and low clocks on hybrid Intel CPUs when Windows deems it in the background) and of Windows 11 ignoring its timer resolution while the window is hidden. One "power:" log line says whether Windows accepted it. Co-Authored-By: Claude Opus 5.5 --- runtime/main.c | 4 ++++ runtime/platform/linux.c | 2 ++ runtime/platform/platform.h | 1 + runtime/platform/win32.c | 18 ++++++++++++++++++ 4 files changed, 25 insertions(+) diff --git a/runtime/main.c b/runtime/main.c index e3428719..38bbb941 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -532,6 +532,10 @@ int main(int argc, char **argv) { // Timed guest waits (kernel.c block_inner) sleep in whole milliseconds; at the default 15.6 ms tick a 1 ms wait // costs a full tick. TOTK_TIMER_1MS=0 keeps the default for comparison. if (cfg_bool("TOTK_TIMER_1MS")) plat_timer_1ms(); + int power = plat_power_full(); + if (power >= 0) + rt_log("power: %s", power ? "OS power throttling off (EcoQoS, timer resolution in the background)" + : "OS power throttling: the opt-out was refused"); cfg = (RtConfig){.exefs = rt_default_exefs, .romfs = rt_default_romfs, .profile = rt_default_profile, .gpu = cfg_get("TOTK_GPU")}; portable_paths(&cfg); // command-line paths below still win diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 158601cb..38a840e3 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -481,6 +481,8 @@ uint64_t plat_counter_hz(void) { return 1000000000u; } // inherit it from the thread that creates them. void plat_timer_1ms(void) { prctl(PR_SET_TIMERSLACK, 1000ul, 0, 0, 0); } +int plat_power_full(void) { return -1; } + // clock_nanosleep wakes within the timer slack (50 us by default) of its deadline; the last PLAT_SPIN_NS are spent // yielding so the deadline is met closely, as on Windows. #define PLAT_SPIN_NS 100000u diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 97911d27..2d454d2a 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -99,6 +99,7 @@ uint64_t plat_now_ns(void); // monotonic, high resolution, arbitra uint64_t plat_counter(void); // the OS's raw monotonic counter (QPC on Windows), plat_counter_hz per second uint64_t plat_counter_hz(void); void plat_timer_1ms(void); // asks the OS for timed waits accurate to 1 ms or better (call before threads) +int plat_power_full(void); // opts the process out of OS power throttling: 1 done, 0 refused, -1 no such thing // t * num / den for a fixed num / den without a 128-bit division per call (__udivti3 took 5% of the game's running // samples through plat_now_ns and rt_ticks): t * q + (t * f >> 64) with q = num / den and f = (num % den) * 2^64 / den. // Monotonic in t and at most one below the exact quotient while t * (num % den) / den fits 64 bits. diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 5dda4538..d0e05af8 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -18,6 +18,13 @@ #include "platform.h" +// Hybrid laptops: the drivers read these exports of the executable when it starts and run it on the dedicated GPU. +// NVIDIA Optimus can otherwise hide its GPU from a program it put on the integrated one, whatever the Windows and NVIDIA +// settings say; AMD switchable graphics read the second one. TOTK_GPU_INDEX still picks another GPU. Savage Wind Recomp +// exports the same two (runtime/platform/windows.c). +__declspec(dllexport) DWORD NvOptimusEnablement = 1; +__declspec(dllexport) int AmdPowerXpressRequestHighPerformance = 1; + _Static_assert(sizeof(PlatMutex) == sizeof(SRWLOCK), "PlatMutex holds an SRWLOCK"); _Static_assert(sizeof(PlatCond) == sizeof(CONDITION_VARIABLE), "PlatCond holds a CONDITION_VARIABLE"); @@ -319,6 +326,17 @@ uint64_t plat_counter_hz(void) { return qpc_freq(); } // The default timer tick is 15.6 ms; a 1 ms wait would cost a full tick. void plat_timer_1ms(void) { timeBeginPeriod(1); } +// Windows 11 runs the threads of a process it deems in the background at EcoQoS (on the E-cores of a hybrid Intel CPU, +// at a low clock) and ignores its timer resolution while its window is hidden. The game is always latency-bound, so it +// opts out of both. Windows before 10 1709 does not know the class: 0. +int plat_power_full(void) { + PROCESS_POWER_THROTTLING_STATE s = { + .Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION, + .ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED | PROCESS_POWER_THROTTLING_IGNORE_TIMER_RESOLUTION, + .StateMask = 0}; + return SetProcessInformation(GetCurrentProcess(), ProcessPowerThrottling, &s, sizeof s) ? 1 : 0; +} + static PlatScale qpc_ns; // set before main, so no thread reads it half written __attribute__((constructor)) static void qpc_ns_init(void) { qpc_ns = plat_scale(1000000000u, qpc_freq()); } From 3a26046cde6ffd54581a1faedff3298fbc0b71d4 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 8 Nov 2026 11:46:10 +0300 Subject: [PATCH 07/30] Let the threads every frame goes through prefer the P-cores of a hybrid CPU On an Intel CPU with P-cores and E-cores a thread on an E-core runs at about 60% of a P-core's speed, and the frame waits for its slowest critical thread. The game's main thread, the thread that presents, and the vk replay, recorder and present threads now select the CPU sets of the P-core logical processors the process affinity allows. Nothing is selected on a CPU with one efficiency class or when the affinity leaves only one kind of core. TOTK_PCORE=0 turns it off; "power:" log lines say what was selected and which threads took it. LAS Recomp does the same for its game, render and vsync threads. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 2 +- runtime/host.h | 4 +++ runtime/kernel.c | 1 + runtime/main.c | 10 ++++++ runtime/perf.c | 9 +++++ runtime/platform/linux.c | 6 ++++ runtime/platform/platform.h | 5 +++ runtime/platform/win32.c | 72 +++++++++++++++++++++++++++++++++++++ runtime/vk_backend.c | 4 +++ runtime/vk_rec.c | 1 + 10 files changed, 113 insertions(+), 1 deletion(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 76c3279d..950df3dc 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -50,7 +50,7 @@ static const char *const known[] = { "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", - "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", + "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REC_SPIN_US", "REEXEC", "RENDER_SCALE", "REPLAY_ASYNC", diff --git a/runtime/host.h b/runtime/host.h index 066104e0..c2d94157 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -36,6 +36,10 @@ void perf_end(int kind, uint64_t t0); // counts render-thread time onl void perf_replay(uint64_t t0); // replay (NVN -> Vulkan recording) time on any thread, waits inside it included void perf_replay_thread(void); // the calling thread replays for the render thread: perf_end counts its waits void perf_main_thread(void); // the calling thread is the guest main thread (TOTK_PROFILE_MAIN) +// The calling thread is one every frame goes through: on a hybrid CPU it prefers the P-cores (main.c, TOTK_PCORE); +// the first thread of each role logs it. +enum { PCORE_MAIN, PCORE_RENDER, PCORE_REPLAY, PCORE_RECORDER, PCORE_PRESENT, PCORE_N }; +void perf_core_thread(int role); // Events per 60-frame window: graphics pipelines finished by the background workers, draws skipped while pending, // first texture uploads and re-uploads after CPU writes, draws skipped while a shader translates in the background, // draws that waited for their pending pipeline and got it, draws into a target the previous frame did not render that diff --git a/runtime/kernel.c b/runtime/kernel.c index 00aaa963..4b5114c1 100644 --- a/runtime/kernel.c +++ b/runtime/kernel.c @@ -1561,6 +1561,7 @@ uint32_t kern_main_thread(Cpu **cpu) { t->cpu->sp = t->stack_top; t->tid = plat_thread_id(); perf_main_thread(); + perf_core_thread(PCORE_MAIN); PlatIntr *self = ap_open_self(); lock(); t->hthr = self; diff --git a/runtime/main.c b/runtime/main.c index 38bbb941..cba26ccf 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -536,6 +536,16 @@ int main(int argc, char **argv) { if (power >= 0) rt_log("power: %s", power ? "OS power throttling off (EcoQoS, timer resolution in the background)" : "OS power throttling: the opt-out was refused"); + // Hybrid CPUs: the threads every frame goes through prefer the P-cores (perf_core_thread). TOTK_PCORE=0: off. + if (rt_knob("PCORE", "1")) { + char why[160]; + int n = plat_perf_core_setup(why, sizeof why); + if (n) rt_log("power: %s; the game's main and render threads and the vk replay, recorder and present threads " + "prefer the %d P-core ones (TOTK_PCORE=0 turns it off)", why, n); + else rt_log("power: %s: no P-core preference", why); + } else { + rt_log("power: TOTK_PCORE=0: no P-core preference"); + } cfg = (RtConfig){.exefs = rt_default_exefs, .romfs = rt_default_romfs, .profile = rt_default_profile, .gpu = cfg_get("TOTK_GPU")}; portable_paths(&cfg); // command-line paths below still win diff --git a/runtime/perf.c b/runtime/perf.c index ef04ede5..cae0ff4d 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -62,6 +62,15 @@ uint64_t perf_begin(void) { return kern_now_ns(); } void perf_replay_thread(void) { prof_replay_tid = plat_thread_id(); } void perf_main_thread(void) { prof_main_tid = plat_thread_id(); } +void perf_core_thread(int role) { + static const char *const names[] = {"game main", "game render", "vk replay", "vk recorder", "vk present"}; + static int32_t said; + int r = plat_thread_perf_core(); + if (r < 0 || role < 0 || role >= PCORE_N) return; + if (__atomic_fetch_or(&said, 1 << role, __ATOMIC_RELAXED) & (1 << role)) return; + rt_log("power: the %s thread %s the P-cores", names[role], r ? "prefers" : "could not select"); +} + void perf_end(int kind, uint64_t t0) { if (!t0) return; uint32_t tid = plat_thread_id(); diff --git a/runtime/platform/linux.c b/runtime/platform/linux.c index 38a840e3..fcb88d77 100644 --- a/runtime/platform/linux.c +++ b/runtime/platform/linux.c @@ -483,6 +483,12 @@ void plat_timer_1ms(void) { prctl(PR_SET_TIMERSLACK, 1000ul, 0, 0, 0); } int plat_power_full(void) { return -1; } +int plat_perf_core_setup(char *why, size_t n) { + snprintf(why, n, "not done on Linux"); + return 0; +} +int plat_thread_perf_core(void) { return -1; } + // clock_nanosleep wakes within the timer slack (50 us by default) of its deadline; the last PLAT_SPIN_NS are spent // yielding so the deadline is met closely, as on Windows. #define PLAT_SPIN_NS 100000u diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index 2d454d2a..78873ced 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -100,6 +100,11 @@ uint64_t plat_counter(void); // the OS's raw monotonic counter (QPC uint64_t plat_counter_hz(void); void plat_timer_1ms(void); // asks the OS for timed waits accurate to 1 ms or better (call before threads) int plat_power_full(void); // opts the process out of OS power throttling: 1 done, 0 refused, -1 no such thing +// Hybrid CPUs: finds the performance-core logical processors the process may use, once, before any thread asks; returns +// their count (0: none to prefer, why says which case). plat_thread_perf_core: the calling thread prefers them; 1 done, +// 0 refused, -1 nothing selected. +int plat_perf_core_setup(char *why, size_t n); +int plat_thread_perf_core(void); // t * num / den for a fixed num / den without a 128-bit division per call (__udivti3 took 5% of the game's running // samples through plat_now_ns and rt_ticks): t * q + (t * f >> 64) with q = num / den and f = (num % den) * 2^64 / den. // Monotonic in t and at most one below the exact quotient while t * (num % den) / den fits 64 bits. diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index d0e05af8..c0ee5939 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -296,6 +296,78 @@ uint32_t plat_cpu_usable(void) { return plat_cpu_count(); } +// ---- performance cores of a hybrid CPU +// An Intel CPU with P-cores and E-cores (a Core 5 210H: 4 P-cores with two threads each, 4 E-cores) runs a thread on an +// E-core at about 60% of a P-core's speed, and the frame waits for its slowest critical thread. The few threads every +// frame goes through ask for the P-cores (plat_thread_perf_core): their selected CPU sets are the logical processors +// above the lowest efficiency class that the process affinity allows. A thread's CPU sets bind it like an affinity +// mask, except that processors the system reserves for itself are left out instead of failing. Nothing is selected +// on a CPU with one efficiency class, or when the affinity already leaves only P-cores or only E-cores. LAS Recomp +// does the same for its game, render and vsync threads (runtime/platform/windows.c plat_thread_perf_core). +static ULONG *pc_ids; // the CPU set ids of the allowed P-core logical processors; NULL: none selected +static ULONG pc_n; + +int plat_perf_core_setup(char *why, size_t n) { + ULONG len = 0; + GetSystemCpuSetInformation(NULL, 0, &len, GetCurrentProcess(), 0); + uint8_t *buf = len ? malloc(len) : NULL; + if (!buf || !GetSystemCpuSetInformation((PSYSTEM_CPU_SET_INFORMATION)buf, len, &len, GetCurrentProcess(), 0)) { + free(buf); + snprintf(why, n, "no CPU set information (error %lu)", GetLastError()); + return 0; + } + // The process affinity mask covers its first processor group only: it filters on a single-group system. + DWORD_PTR proc = 0, sys = 0; + int filter = GetActiveProcessorGroupCount() == 1 && GetProcessAffinityMask(GetCurrentProcess(), &proc, &sys) && proc; + int lo = 255, hi = -1, all = 0; + for (ULONG off = 0; off < len;) { + SYSTEM_CPU_SET_INFORMATION *i = (SYSTEM_CPU_SET_INFORMATION *)(buf + off); + if (!i->Size) break; + if (i->Type == CpuSetInformation) { + int ec = i->CpuSet.EfficiencyClass; + lo = ec < lo ? ec : lo, hi = ec > hi ? ec : hi, all++; + } + off += i->Size; + } + if (!all || hi <= lo) { + free(buf); + snprintf(why, n, "not a hybrid CPU (%d logical processors, one efficiency class)", all); + return 0; + } + ULONG *ids = calloc((size_t)all, sizeof *ids); + int np = 0, ne = 0, ok_p = 0, ok_e = 0; + for (ULONG off = 0; ids && off < len;) { + SYSTEM_CPU_SET_INFORMATION *i = (SYSTEM_CPU_SET_INFORMATION *)(buf + off); + if (!i->Size) break; + if (i->Type == CpuSetInformation) { + int perf = i->CpuSet.EfficiencyClass > lo; + int allowed = !filter || (i->CpuSet.Group == 0 && i->CpuSet.LogicalProcessorIndex < 64 && + (proc >> i->CpuSet.LogicalProcessorIndex) & 1); + perf ? np++ : ne++; + if (allowed) perf ? ok_p++ : ok_e++; + if (allowed && perf) ids[pc_n++] = i->CpuSet.Id; + } + off += i->Size; + } + free(buf); + if (!ids || !ok_p || !ok_e) { + free(ids); + pc_n = 0; + snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors; the affinity allows %d + %d, so it decides", + np, ne, ok_p, ok_e); + return 0; + } + pc_ids = ids; + snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors%s", np, ne, + ok_p != np || ok_e != ne ? " (affinity-limited)" : ""); + return (int)pc_n; +} + +int plat_thread_perf_core(void) { + if (!pc_ids) return -1; + return SetThreadSelectedCpuSets(GetCurrentThread(), pc_ids, pc_n) ? 1 : 0; +} + // PLAT_PRIO_* are the THREAD_PRIORITY_* values. void plat_thread_priority(int prio) { SetThreadPriority(GetCurrentThread(), prio); } void plat_thread_set_priority(PlatThread *t, int prio) { SetThreadPriority(t->h, prio); } diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index 5749de6b..5e7535e7 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -49,6 +49,7 @@ static void wait_until(uint64_t at) { static void present_main(void *arg) { (void)arg; + perf_core_thread(PCORE_PRESENT); uint64_t last = 0, last_queued = 0; plat_mutex_lock(&pl); for (;;) { @@ -201,6 +202,7 @@ static uint64_t submit_sync(const uint64_t *handles, int n) { static void replay_main(void *arg) { (void)arg; perf_replay_thread(); + perf_core_thread(PCORE_REPLAY); plat_mutex_lock(&rl); for (;;) { while (!rq_n) plat_cond_wait(&rcv, &rl); @@ -326,6 +328,8 @@ void gfx_exit_log(void) { } static void vk_be_present(const tex_t *t, const int crop[4], uint64_t at_ns) { + static _Thread_local int pcore; // the game's render thread: the one that presents + if (!pcore) pcore = 1, perf_core_thread(PCORE_RENDER); vk_sync_start(); if (!gfx_replay_async()) { uint64_t frame = __atomic_add_fetch(&gfx_counters.frames, 1, __ATOMIC_RELAXED); diff --git a/runtime/vk_rec.c b/runtime/vk_rec.c index 0706dbcb..6d0b7d08 100644 --- a/runtime/vk_rec.c +++ b/runtime/vk_rec.c @@ -74,6 +74,7 @@ static void exec_packet(uint32_t op, void *p); static void rec_main(void *arg) { (void)arg; plat_thread_priority(rec_prio); + perf_core_thread(PCORE_RECORDER); uint64_t t = 0; for (;;) { uint64_t h = __atomic_load_n(&Q.head, __ATOMIC_ACQUIRE); From 90fa6e1788e258d4117c518910f324f7a2c7f0a2 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 8 Nov 2026 12:02:53 +0300 Subject: [PATCH 08/30] Let the recorder submit parts without the submitting thread waiting for it rec_submit queued each part's submission and then drained the ring until the recorder had submitted it: 0.29 ms a frame in the town on a Ryzen 7900X, 0.34 ms on 4 cores. Now it publishes the submission and returns (TOTK_REC_ASYNC=1, default). The queue order the present, uploads, readbacks and device waits rely on is kept by vk_queue_lock: a thread's first lock waits until the recorder submitted what was queued before (rec_queue_wait). The recorder itself and a thread that already holds the lock skip the wait. rec_submit reports the first failed submission the recorder made. Town, in-run A/B: replay -4.1% on all cores, -2.9% on 4 cores / 8 threads; queue users waited about 0.02 ms a frame. fight2 with TOTK_REC_CHECK and the town under sync validation are clean. Where the recorder does not poll (fewer than 6 usable logical processors) the producer now publishes the ring head every 8192 words instead of 64, as Savage Wind Recomp does (runtime/nvn/vulkan/record.c rec_init): each publish there costs an event set and a wake-up. Town on 4 logical processors: frame interval -1.3%. TOTK_REC_PUB=ab compares both; the perf log counts the recorder's wake-ups and the queue waits. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 2 +- runtime/gfx_present.c | 15 ++++++++-- runtime/vk_rec.c | 70 ++++++++++++++++++++++++++++++++++--------- runtime/vk_rec.h | 12 ++++++-- 4 files changed, 79 insertions(+), 20 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index 76c3279d..be7febbe 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -52,7 +52,7 @@ static const char *const known[] = { "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", - "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REC_SPIN_US", "REEXEC", + "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_ASYNC", "REC_END", "REC_SPIN_US", "REEXEC", "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RSTICK_DEADZONE", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", diff --git a/runtime/gfx_present.c b/runtime/gfx_present.c index 472acb41..ca698c7e 100644 --- a/runtime/gfx_present.c +++ b/runtime/gfx_present.c @@ -33,9 +33,20 @@ #define VSYNC_HZ 60 // ---- queue lock +// The recorder queues the parts it records and submits them later (vk_rec.c rec_submit): the first lock of a thread +// waits until the parts queued before it are submitted, so whatever it puts on the queue comes after them, as when +// the submitting thread submitted them itself. Not inside a held lock: the recorder takes this lock to submit. static PlatRMutex g_ql; -void vk_queue_lock(void) { plat_rmutex_lock(&g_ql); } -void vk_queue_unlock(void) { plat_rmutex_unlock(&g_ql); } +static _Thread_local int ql_depth; +void vk_queue_lock(void) { + if (!ql_depth) rec_queue_wait(); + plat_rmutex_lock(&g_ql); + ql_depth++; +} +void vk_queue_unlock(void) { + ql_depth--; + plat_rmutex_unlock(&g_ql); +} // ---- window state shared with the back end static VkSurfaceKHR g_surface; diff --git a/runtime/vk_rec.c b/runtime/vk_rec.c index 0706dbcb..5d9a746d 100644 --- a/runtime/vk_rec.c +++ b/runtime/vk_rec.c @@ -39,6 +39,12 @@ static int rec_live; // producer: rec_begin started the record static int end_mode = -1; // TOTK_REC_END (rec_own) static int sub_pending; // producer: the recorder ended a part that rec_submit has not handed over yet static volatile VkResult r_sub_res; // recorder: the result of its last vkQueueSubmit2, read after the drain +static volatile VkResult r_sub_err; // recorder: the first vkQueueSubmit2 that failed (rec_submit reports it) +static volatile uint64_t q_subs; // producer: submissions queued for the recorder (rec_submit) +static volatile uint64_t r_subs; // recorder: submissions it handed to the driver (submit_run) +static _Thread_local int on_rec; // the calling thread is the recorder +static int rec_async = -1; // TOTK_REC_ASYNC (rec_submit) +static struct { uint64_t n, ns; } RQ_; // waits in rec_queue_wait, logged by rec_stats_log static PlatThread *rec_thread; static int rec_prio; // the submitting thread's priority, which the recorder takes static int rec_chk; // TOTK_REC_CHECK (rec_check_handle) @@ -50,7 +56,7 @@ static volatile uint64_t part_fence; // the value of the part being recorded ( static uint64_t r_sleeps; // recorder: sleeps on wake_ev, read by rec_stats_log // Producer statistics, logged by rec_stats_log. -static struct { _Alignas(64) uint64_t packets, words, drains, fallbacks, drain_ns, full_waits, parts, parts_on, submits; } RS_; +static struct { _Alignas(64) uint64_t packets, words, drains, fallbacks, drain_ns, full_waits, parts, parts_on, submits, wakes; } RS_; enum { R_PAD, R_BeginQuery, R_BeginRendering, R_BindDescriptorSets, R_BindIndexBuffer, R_BindPipeline, R_BindVertexBuffers2, @@ -74,6 +80,7 @@ static void exec_packet(uint32_t op, void *p); static void rec_main(void *arg) { (void)arg; plat_thread_priority(rec_prio); + on_rec = 1; uint64_t t = 0; for (;;) { uint64_t h = __atomic_load_n(&Q.head, __ATOMIC_ACQUIRE); @@ -122,7 +129,7 @@ static void rec_main(void *arg) { static void wake(void) { __atomic_thread_fence(__ATOMIC_SEQ_CST); - if (__atomic_load_n(&Q.sleeping, __ATOMIC_RELAXED)) plat_event_set(&wake_ev); + if (__atomic_load_n(&Q.sleeping, __ATOMIC_RELAXED)) plat_event_set(&wake_ev), RS_.wakes++; } // Room for need words: the tail read last leaves it in all but about one packet in 16 MiB of them. @@ -162,14 +169,21 @@ static void *put(uint32_t op, size_t bytes) { // rec_drain), and the packets that begin and end a part go out at once (pub_now), so the recorder begins and ends // command buffers as early as before; TOTK_REC_CHECK publishes every packet, since rec_check_handle searches the ring // up to head. +// The batch is 64 words (512 bytes) where the recorder polls before it sleeps, 8192 (64 KiB) where it does not (fewer +// than 6 usable logical processors, rec_spin_ns): there it is mostly asleep when a batch goes out, and each publish +// cost the producer an event set and the recorder a wake-up. Savage Wind Recomp measured that at 6.7% of its render +// thread's busy time on 4 cores and moved to 64 KiB there (runtime/nvn/vulkan/record.c rec_init, publish_every). +// TOTK_REC_PUB= sets the batch; =ab: 64 words in the A blocks of TOTK_AB, 8192 in the B blocks. +static int pub_ab; static void pub_now(void) { Q.pub_h = Q.put_h; __atomic_store_n(&Q.head, Q.put_h, __ATOMIC_RELEASE); - if (__atomic_load_n(&Q.sleeping, __ATOMIC_RELAXED)) plat_event_set(&wake_ev); + if (__atomic_load_n(&Q.sleeping, __ATOMIC_RELAXED)) plat_event_set(&wake_ev), RS_.wakes++; } static void pub(void) { - if (Q.put_h - Q.pub_h >= Q.pub_words) pub_now(); + uint64_t w = pub_ab ? (rt_ab() ? 8192 : 64) : Q.pub_words; + if (Q.put_h - Q.pub_h >= w) pub_now(); } void rec_drain(void) { @@ -224,7 +238,8 @@ void rec_begin(VkCommandBuffer cb, uint64_t fence, int crumb, uint64_t n) { int spin_us = sp && *sp && !rec_spin_ab ? atoi(sp) : 60; rec_spin_ns = spin_us > 0 && plat_cpu_usable() >= 6 ? (uint64_t)spin_us * 1000 : 0; const char *pw = cfg_get("TOTK_REC_PUB"); // words per publish (pub); 0: every packet - Q.pub_words = rec_chk ? 0 : pw && *pw ? (uint64_t)atoi(pw) : 64; + pub_ab = !rec_chk && pw && !strcmp(pw, "ab"); + Q.pub_words = rec_chk ? 0 : pw && *pw && !pub_ab ? (uint64_t)atoi(pw) : rec_spin_ns ? 64 : 8192; if (Q.pub_words > RING_WORDS / 8) Q.pub_words = RING_WORDS / 8; rec_thread = plat_thread_new(rec_main, NULL, "vk recorder"); if (!rec_thread) { @@ -239,8 +254,8 @@ void rec_begin(VkCommandBuffer cb, uint64_t fence, int crumb, uint64_t n) { (unsigned long long)(rec_spin_ns / 1000), rec_spin_ab ? " in the B blocks" : ""); else rt_log("vk_rec: the recorder polls 4096 pauses before it sleeps (TOTK_REC_SPIN_US)"); - if (Q.pub_words != 64) rt_log("vk_rec: TOTK_REC_PUB: head published every %llu words", - (unsigned long long)Q.pub_words); + if (pub_ab) rt_log("vk_rec: head published every 64 words, 8192 in the B blocks (TOTK_REC_PUB=ab)"); + else rt_log("vk_rec: head published every %llu words (TOTK_REC_PUB)", (unsigned long long)Q.pub_words); if (rec_delay_ns) rt_log("vk_rec: TOTK_REC_DELAY_US: the recorder waits %llu us before each batch", (unsigned long long)(rec_delay_ns / 1000)); } @@ -323,11 +338,20 @@ VkResult rec_submit(const VkSubmitInfo2 *si) { a->ncb = si->commandBufferInfoCount, a->nsig = si->signalSemaphoreInfoCount; for (uint32_t i = 0; i < a->ncb; i++) a->cbi[i] = si->pCommandBufferInfos[i]; if (a->nsig) a->sig = si->pSignalSemaphoreInfos[0]; - pub(); - // Waited for here, so the queue holds every earlier submission when the caller goes on, as with the recorder off: - // the present, uploads, readbacks and device waits on other threads rely on that order. - rec_drain(); + __atomic_store_n(&q_subs, q_subs + 1, __ATOMIC_RELEASE); RS_.submits++; + // TOTK_REC_ASYNC=1 (default): the submitting thread goes on while the recorder catches up with the part and submits + // it; it waited here for 0.29 ms a frame in the town on a Ryzen 7900X, 0.34 ms limited to 4 cores. The queue order + // the present, uploads, readbacks and device waits rely on holds through vk_queue_lock (rec_queue_wait). The values + // the part signals are counted as issued at once (vk_sync.c): waiting for one on the host before its submission is + // legal for a timeline semaphore. =0: waits here for the submission, as before; =ab with the A/B blocks. + if (rec_async < 0) rec_async = rt_knob("REC_ASYNC", "1"); + if (RT_KNOB_ON(rec_async)) { + pub_now(); + return __atomic_load_n(&r_sub_err, __ATOMIC_ACQUIRE); + } + pub(); + rec_drain(); return r_sub_res; } @@ -336,19 +360,37 @@ static void submit_run(const A_Submit *a) { .pCommandBufferInfos = a->cbi, .signalSemaphoreInfoCount = a->nsig, .pSignalSemaphoreInfos = a->nsig ? &a->sig : NULL}; vk_queue_lock(); - r_sub_res = vkQueueSubmit2(vk_ctx()->gfx_queue, 1, &si, VK_NULL_HANDLE); + VkResult r = vkQueueSubmit2(vk_ctx()->gfx_queue, 1, &si, VK_NULL_HANDLE); vk_queue_unlock(); + r_sub_res = r; + if (r != VK_SUCCESS && __atomic_load_n(&r_sub_err, __ATOMIC_RELAXED) == VK_SUCCESS) + __atomic_store_n(&r_sub_err, r, __ATOMIC_RELEASE); + __atomic_store_n(&r_subs, r_subs + 1, __ATOMIC_RELEASE); +} + +void rec_queue_wait(void) { + if (!rec_thread || on_rec) return; + uint64_t want = __atomic_load_n(&q_subs, __ATOMIC_ACQUIRE); + if (__atomic_load_n(&r_subs, __ATOMIC_ACQUIRE) >= want) return; + uint64_t t0 = now_ns(); + for (int i = 0; __atomic_load_n(&r_subs, __ATOMIC_ACQUIRE) < want; i++) + if (i < 20000) _mm_pause(); + else plat_yield(); + __atomic_add_fetch(&RQ_.n, 1, __ATOMIC_RELAXED); + __atomic_add_fetch(&RQ_.ns, now_ns() - t0, __ATOMIC_RELAXED); } void rec_stats_log(void) { if (!(rt_trace & RT_TRACE_PERF) || !RS_.parts) return; rt_log("vk_rec: %llu of %llu parts recorded by the recorder: %llu packets (%.1f MiB), %llu drains (%llu for a call " "without a packet) waited %.2f ms in all, ring full %llu times, %llu submissions made by the recorder, " - "recorder slept %llu times", + "recorder slept %llu times and was woken %llu times, queue users waited for its submissions %llu times (%.2f ms)", (unsigned long long)RS_.parts_on, (unsigned long long)RS_.parts, (unsigned long long)RS_.packets, RS_.words * 8 / 1048576.0, (unsigned long long)RS_.drains, (unsigned long long)RS_.fallbacks, RS_.drain_ns / 1e6, (unsigned long long)RS_.full_waits, (unsigned long long)RS_.submits, - (unsigned long long)__atomic_exchange_n(&r_sleeps, 0, __ATOMIC_RELAXED)); + (unsigned long long)__atomic_exchange_n(&r_sleeps, 0, __ATOMIC_RELAXED), (unsigned long long)RS_.wakes, + (unsigned long long)__atomic_exchange_n(&RQ_.n, 0, __ATOMIC_RELAXED), + __atomic_exchange_n(&RQ_.ns, 0, __ATOMIC_RELAXED) / 1e6); memset(&RS_, 0, sizeof RS_); } diff --git a/runtime/vk_rec.h b/runtime/vk_rec.h index bbceff90..28c737af 100644 --- a/runtime/vk_rec.h +++ b/runtime/vk_rec.h @@ -5,8 +5,10 @@ // command buffer (uploads, the window, probes) are made at once. A call the ring has no packet for (a structure with a // pNext chain) waits for the recorder to empty the ring and is then made at once on the calling thread. With // TOTK_REC_END=1 (default) the recorder also begins, ends and submits the parts it records, so a part's command buffer -// never changes threads; rec_submit returns once the recorder submitted it. The submitting thread allocates from the -// part pool only while the recorder is idle (vk_sync.c claim_slot). +// never changes threads. rec_submit queues the submission and returns at once (TOTK_REC_ASYNC=1, default; 0: once the +// recorder submitted it); every other user of the queue waits in vk_queue_lock until the recorder submitted what was +// queued before (rec_queue_wait), so queue order stays the order of the calls. The submitting thread allocates from +// the part pool only while the recorder is idle (vk_sync.c claim_slot). // // Every vkCmd* the runtime calls through vk.h goes through the rc_* wrappers below (the macros at the end); the // extension commands called through function pointers have rec_* wrappers of their own. TOTK_VK_RECORDER=0 makes every @@ -24,9 +26,13 @@ void rec_end(void); // waits for the recorder; no-op when rec_ // (1: rec_submit hands the part over); otherwise rec_end and the breadcrumb, and the caller ends cb (0). int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n); // vkQueueSubmit2 of si (at most two command buffers, one signal, no wait, no fence) under vk_queue_lock: by the -// recorder after a part it ended, waited for, else at once. +// recorder after a part it ended, else at once. Queued for the recorder, it returns the first failure of an earlier +// submission the recorder made (VK_SUCCESS if none), since this one's result is not known yet. VkResult rec_submit(const VkSubmitInfo2 *si); void rec_drain(void); // waits until the recorder made every call queued so far +// vk_queue_lock, before it takes the lock (not on the recorder, not when the thread holds the lock already): waits +// until the recorder submitted every submission queued so far. +void rec_queue_wait(void); void rec_stats_log(void); // TOTK_TRACE=perf: packets, bytes, drains and waits since the last call // TOTK_REC_CHECK=1 (debug, recorder on): vk_memory.c reports each object it defers (destroy 0, stamped tl) or destroys // (destroy 1) with the value the GPU reached (gpu); a packet of the part being recorded that still names it is logged From 0d9525ba4aec8f7e22bee49dee875b7d429f2d65 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Sun, 8 Nov 2026 12:11:06 +0300 Subject: [PATCH 09/30] Leave the P-core preference off on hybrid CPUs with fewer than 4 P-cores An i5-1235U or Core 3 100U has 2 P-cores. Binding the five frame-critical threads there, with a spinning recorder among them, crowds two cores and their hyperthreads while the E-cores stay idle. plat_perf_core_setup now counts the allowed physical P-cores and selects nothing below PC_MIN_CORES (4), so the scheduler places those threads as before. Co-Authored-By: Claude Opus 5.5 --- runtime/platform/win32.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index c0ee5939..f3f788f4 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -304,6 +304,10 @@ uint32_t plat_cpu_usable(void) { // mask, except that processors the system reserves for itself are left out instead of failing. Nothing is selected // on a CPU with one efficiency class, or when the affinity already leaves only P-cores or only E-cores. LAS Recomp // does the same for its game, render and vsync threads (runtime/platform/windows.c plat_thread_perf_core). +// Nothing is selected either below PC_MIN_CORES physical P-cores (an i5-1235U or Core 3 100U has 2): the five bound +// threads (perf_core_thread) and a spinning recorder would crowd two cores and their hyperthreads while the E-cores +// stay idle, which can be slower than letting the scheduler place them. +#define PC_MIN_CORES 4 static ULONG *pc_ids; // the CPU set ids of the allowed P-core logical processors; NULL: none selected static ULONG pc_n; @@ -335,8 +339,9 @@ int plat_perf_core_setup(char *why, size_t n) { return 0; } ULONG *ids = calloc((size_t)all, sizeof *ids); - int np = 0, ne = 0, ok_p = 0, ok_e = 0; - for (ULONG off = 0; ids && off < len;) { + uint16_t *cores = calloc((size_t)all, sizeof *cores); // group << 8 | core index of each allowed physical P-core + int np = 0, ne = 0, ok_p = 0, ok_e = 0, nc = 0; + for (ULONG off = 0; ids && cores && off < len;) { SYSTEM_CPU_SET_INFORMATION *i = (SYSTEM_CPU_SET_INFORMATION *)(buf + off); if (!i->Size) break; if (i->Type == CpuSetInformation) { @@ -345,18 +350,33 @@ int plat_perf_core_setup(char *why, size_t n) { (proc >> i->CpuSet.LogicalProcessorIndex) & 1); perf ? np++ : ne++; if (allowed) perf ? ok_p++ : ok_e++; - if (allowed && perf) ids[pc_n++] = i->CpuSet.Id; + if (allowed && perf) { + ids[pc_n++] = i->CpuSet.Id; + uint16_t key = (uint16_t)(i->CpuSet.Group << 8 | i->CpuSet.CoreIndex); + int k = 0; + while (k < nc && cores[k] != key) k++; + if (k == nc) cores[nc++] = key; + } } off += i->Size; } + int counted = ids && cores; free(buf); - if (!ids || !ok_p || !ok_e) { + free(cores); + if (!counted || !ok_p || !ok_e) { free(ids); pc_n = 0; snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors; the affinity allows %d + %d, so it decides", np, ne, ok_p, ok_e); return 0; } + if (nc < PC_MIN_CORES) { + free(ids); + pc_n = 0; + snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors on %d P-cores, fewer than %d", np, ne, nc, + PC_MIN_CORES); + return 0; + } pc_ids = ids; snprintf(why, n, "hybrid CPU, %d P-core + %d E-core logical processors%s", np, ne, ok_p != np || ok_e != ne ? " (affinity-limited)" : ""); From ceece5683a94c01e759ae4e172628b96bb8ea746 Mon Sep 17 00:00:00 2001 From: Wintego <17454585+Wintego@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:20:17 +0300 Subject: [PATCH 10/30] Make the PC prompts for every game resolution the setup lays the UI out for The setup wrote romfs-patches-pc for 16:9 only, so prompts = pc on a 21:9, 32:9, 16:10 or 3:2 screen kept the game resolution at 1920x1080 (cfg.c) or, with output_res set to the screen's shape, fell back to the Switch prompts. - make_romfs_patches.py --reuse : a file of an earlier overlay whose contents match is hard-linked (copied where links fail) instead of compressed again; the output is byte for byte the same. - The setup builds romfs-patches-pc-x for each of ASPECT_SIZES from romfs-patches-pc with --reuse: only the Common archive is compressed, so the fifteen folders take about two minutes and 6 MiB each instead of 22 minutes and 73 MiB each. - cfg.c: the display-shaped output_res default applies with prompts = pc when romfs-patches-pc-x is there (romfs_shaped_pc_ready); Xbox prompts keep 1920x1080 as before. --- docs/controls.md | 2 +- docs/installer.md | 7 ++++-- installer/steps.py | 22 ++++++++++++++++-- runtime/cfg.c | 20 ++++++++++++----- runtime/host.h | 1 + runtime/romfs.c | 17 +++++++++----- runtime/ui/menu.cpp | 4 ++-- tests/romfs_prompts_test.py | 29 ++++++++++++++++++++++++ tools/make_romfs_patches.py | 45 ++++++++++++++++++++++++++++++++----- totk.ini.example | 4 ++-- 10 files changed, 125 insertions(+), 26 deletions(-) diff --git a/docs/controls.md b/docs/controls.md index 9c14b771..3200f001 100644 --- a/docs/controls.md +++ b/docs/controls.md @@ -73,7 +73,7 @@ The Switch prompts stay, with a note under *Button prompts* and a `RomFS overlay - The glyphs keep the width of the game's own, so the right-aligned guides do not run into their text. A key is a rounded square with its label cut out in the game's own text fonts; a mouse button is a mouse with that button filled and the others hollow. - The button guide in play draws every button from images of the Common archive (`Nt_KeyTexA/B/X/Y/L/ZL_00`, `Nt_KeyTexLSt_00` and `_01`, the stick tilted and pressed): all of them are painted with the same icons. The letters of the action prompt's diamond are one character of the Normal font each: E, an arrow for Shift, a bar for Space, L and R for the mouse buttons. - Like the Xbox prompts they follow the defaults and not the bindings. `--binds ` draws the first key of each `bind_` line of that file instead, for a folder made by hand. -- At a game resolution of another shape than 16:9 the game loads `romfs-patches-pc-x` in place of `romfs-patches-x` when it is there: the same prompts with that shape's UI layout, `make_romfs_patches.py --prompts pc --aspect x --out \romfs-patches-pc-x`. The setup does not write those, so without one the Switch prompts stay (*off at a game resolution not 16:9*). +- At a game resolution of another shape than 16:9 the game loads `romfs-patches-pc-x` in place of `romfs-patches-x`: the same prompts with that shape's UI layout, `make_romfs_patches.py --prompts pc --aspect x --reuse \romfs-patches-pc --out \romfs-patches-pc-x`. The setup writes one for each of the fifteen sizes it lays the UI out for, so on a display of one of those shapes the game resolution follows the display with these prompts too. At another size, without that folder, the Switch prompts stay (*off at a game resolution not 16:9*). ## Window keys diff --git a/docs/installer.md b/docs/installer.md index 46d8c5c9..5a8806ad 100644 --- a/docs/installer.md +++ b/docs/installer.md @@ -74,8 +74,11 @@ loads the saves 1.4.3 wrote. writes the translations it makes in play as loose files; running the tool again with the game closed folds those in. The patches are written four times: `romfs-patches`, `romfs-patches-xbox` and `romfs-patches-xbox-label` (`--prompts`: Xbox button prompts for `prompts = xbox`, the first for `pad_layout = nintendo`, the second for `xbox` - and `auto`), and `romfs-patches-pc` (keyboard and mouse prompts for `prompts = pc`). The three prompt folders are - optional: when one cannot be made, the setup warns and finishes. + and `auto`), and `romfs-patches-pc` (keyboard and mouse prompts for `prompts = pc`). The PC prompts are then laid + out once more for each game resolution of another shape, `romfs-patches-pc-x` (`--prompts pc --aspect`). + Those differ from `romfs-patches-pc` in the UI archive only, and `--reuse` hard-links the other files from it, so + the fifteen folders take seconds and a few MiB each. The prompt folders are optional: when one cannot be made, the + setup warns and finishes. When the graphics driver crashes while building the shaders, `prepare_shaders.py` runs that part again without the variants it was building ([building.md](building.md), step 6), and the setup finishes with a warning that names how many were left out. diff --git a/installer/steps.py b/installer/steps.py index 4db8a2b5..ba439a5f 100644 --- a/installer/steps.py +++ b/installer/steps.py @@ -8,7 +8,7 @@ with the bundled clang (hostcheck.c). Install folder: exefs/, romfs/ (romfs.bin, index.json), profile/, shared-fonts/ (Chinese and Korean text), totk.exe and its DLLs, nvngx_dlss.dll (with --dlss), romfs-index.bin, romfs-patches/, romfs-patches-xbox/, -romfs-patches-xbox-label/, romfs-patches-pc/, totk_xlate.exe, the +romfs-patches-xbox-label/, romfs-patches-pc/, romfs-patches-pc-x/, totk_xlate.exe, the shader state of totk.exe --prepare-shaders, totk.ini, totk-install.json. Temp folder (/-): check/ (ExeFS of both inputs, the host check), unpacked/ (the NSP of an NSZ or XCZ input until the extraction is done), gen/ (C), build/ (objects), logs/. The cleanup keeps gen/, build/ and @@ -1097,7 +1097,7 @@ class Prepare(Step): # input_prompts_folder). Both are written on every install, so the menu choices work without another run of # the setup. Optional: when one fails, the game keeps the Switch prompts with that layout and the menu says the # files are missing, so the setup warns once and goes on. romfs-patches-pc likewise, with keyboard and mouse - # prompts for prompts = pc (16:9 only: another shape takes make_romfs_patches.py --prompts pc --aspect). + # prompts for prompts = pc. failed = None for style in ('xbox', 'xbox-label', 'pc'): out = dest / f'romfs-patches-{style}' @@ -1108,6 +1108,24 @@ class Prepare(Step): if code: remove(out) failed = failed or (code, log) + # The PC prompts once more for each shape of ASPECT_SIZES, romfs-patches-pc-x with that shape's UI layout + # (--prompts pc --aspect), which runtime/romfs.c overlay_init loads in place of romfs-patches-x: without + # it a player with prompts = pc on a screen that is not 16:9 chose between the prompts and the screen's shape. + # Only the Common archive differs from romfs-patches-pc; --reuse links the fonts, messages and size table from + # there instead of compressing them again, so the fifteen folders take seconds and a few MiB each. Optional + # like romfs-patches-pc, and skipped without it. + pc = dest / 'romfs-patches-pc' + for size in ASPECT_SIZES: + out = dest / f'romfs-patches-pc-{size}' + remove(out) + if not (pc / 'manifest.tsv').is_file(): + continue + code, log = run_tool(ctx, f'prepare-pc-{size}', layout.tools / 'make_romfs_patches.py', + ['--version', version_tag(game_version(ctx)), '--romfs', dest / 'romfs', '--out', out, + '--prompts', 'pc', '--aspect', size, '--reuse', pc]) + if code: + remove(out) + failed = failed or (code, log) if failed: rep.warning('warn.xbox_prompts_failed', code=failed[0], log=str(failed[1])) diff --git a/runtime/cfg.c b/runtime/cfg.c index a660c26c..3ac171e3 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -337,8 +337,10 @@ static const char *raw_get(const char *name) { // picture between black bars. The sizes are the ones the installer lays the HUD out for (installer/steps.py // ASPECT_SIZES, ui/menu.cpp row_output_res): of the display's shape within 1%, the smallest at least 1080 high that fits // the display, else the largest that fits. A 16:9 display, an unknown one or one of another shape keeps 1920x1080. -// Xbox or keyboard prompts keep 1920x1080 too: the setup builds their folders for 16:9 only (romfs.c overlay_init), so -// a default of another shape would trade the prompts the player chose for the Switch ones. +// Xbox prompts keep 1920x1080 too: the setup builds their folders for 16:9 only (romfs.c overlay_init), so a default of +// another shape would trade the prompts the player chose for the Switch ones. Keyboard prompts have a folder for each +// of these sizes (romfs-patches-pc-x, installer/steps.py Prepare): they keep 1920x1080 only when the one for +// the display's size is missing, as in a source build without make_romfs_patches.py --prompts pc --aspect. static const unsigned SHAPED[][2] = { {2560, 1080}, {3440, 1440}, {3840, 1600}, {5120, 2160}, // 21:9 {3840, 1080}, {5120, 1440}, // 32:9 @@ -346,10 +348,14 @@ static const unsigned SHAPED[][2] = { {1920, 1280}, {2160, 1440}, {2256, 1504}, {2880, 1920}, {3000, 2000}, // 3:2 }; static char screen_res[16]; // set once by cfg_init, "" for none +static int screen_pc; // romfs-patches-pc- has a manifest (set once by cfg_init) -static int switch_prompts(void) { +// Whether the prompts in effect have their folder at screen_res. The same picks as input_map.c input_prompts_pick: an +// unknown or empty value is the Nintendo prompts, an unset one Xbox while the pad layout is "xbox_prompts". +static int shape_keeps_prompts(void) { const char *p = raw_get(PREFIX "PROMPTS"), *l = raw_get(PREFIX "PAD_LAYOUT"); - return (!p || !strcasecmp(p, "nintendo")) && !(l && !strcasecmp(l, "xbox_prompts")); + if (p && *p) return !strcasecmp(p, "pc") ? screen_pc : strcasecmp(p, "xbox") != 0; + return !(l && !strcasecmp(l, "xbox_prompts")); } static void screen_default(void) { @@ -367,7 +373,8 @@ static void screen_default(void) { if (best < 0) best = fit; if (best < 0) return; snprintf(screen_res, sizeof screen_res, "%ux%u", SHAPED[best][0], SHAPED[best][1]); - if (!raw_get(PREFIX "OUTPUT_RES") && switch_prompts()) + screen_pc = romfs_shaped_pc_ready(SHAPED[best][0], SHAPED[best][1]); + if (!raw_get(PREFIX "OUTPUT_RES") && shape_keeps_prompts()) rt_log("cfg: the display is %dx%d: output_res defaults to %s, its shape (totk.ini output_res sets another)", sw, sh, screen_res); } @@ -376,7 +383,8 @@ const char *cfg_get(const char *name) { const char *v = raw_get(name); if (v || strncmp(name, PREFIX, PREFIX_LEN)) return v; if (!profile_of()) - return screen_res[0] && !strcasecmp(name + PREFIX_LEN, "OUTPUT_RES") && switch_prompts() ? screen_res : NULL; + return screen_res[0] && !strcasecmp(name + PREFIX_LEN, "OUTPUT_RES") && shape_keeps_prompts() ? screen_res + : NULL; for (size_t i = 0; i < sizeof DECK / sizeof *DECK; i++) if (!strcasecmp(DECK[i].key, name + PREFIX_LEN)) return DECK[i].value; return NULL; diff --git a/runtime/host.h b/runtime/host.h index 066104e0..85f894d4 100644 --- a/runtime/host.h +++ b/runtime/host.h @@ -280,6 +280,7 @@ int res_is_16x9(unsigned w, unsigned h); // within 1% of 16:9 int res_wants_wide_overlay(const unsigned out[2]); // not 16:9 on a game version with the camera aspect patch int romfs_shaped_ready(unsigned w, unsigned h); // romfs-patches-x has a manifest (romfs.c; works // before fs_init) +int romfs_shaped_pc_ready(unsigned w, unsigned h); // the same for romfs-patches-pc-x void res_wanted(unsigned out[2]); // the output size TOTK_OUTPUT_RES asks for void res_fov_live(void); // TOTK_FOV to the camera (set fov); on a settings change // ---- code patches (patches.c) and the frame step (frame_rate.c) diff --git a/runtime/romfs.c b/runtime/romfs.c index 81665098..52c80481 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -1080,19 +1080,26 @@ static void index_path(char *out, size_t cap) { strcpy(out + k, "romfs-index.bin"); } -// 1 when the game resolution w x h has the UI layout of its shape: romfs-patches-x next to romfs-index.bin with a -// manifest, the folder overlay_init looks for. Works before fs_init: index_dir is not set that early (res_init and -// resolution.c decide run first), so the index path is worked out again here. -int romfs_shaped_ready(unsigned w, unsigned h) { +// 1 when -x has a manifest next to romfs-index.bin. Works before fs_init: index_dir is not set that early +// (cfg_init, res_init and resolution.c decide run first), so the index path is worked out again here. +static int shaped_folder_ready(const char *prefix, unsigned w, unsigned h) { char dir[2100], path[2200]; index_path(dir, sizeof dir); char *sep = plat_last_sep(dir); if (sep) *sep = 0; else strcpy(dir, "."); - return snprintf(path, sizeof path, "%s/romfs-patches-%ux%u/manifest.tsv", dir, w, h) < (int)sizeof path && + return snprintf(path, sizeof path, "%s/%s-%ux%u/manifest.tsv", dir, prefix, w, h) < (int)sizeof path && plat_file_exists(path); } +// 1 when the game resolution w x h has the UI layout of its shape: romfs-patches-x, the folder overlay_init +// looks for. +int romfs_shaped_ready(unsigned w, unsigned h) { return shaped_folder_ready("romfs-patches", w, h); } + +// 1 when it has the PC prompts with that layout too: romfs-patches-pc-x, which overlay_init loads in its place +// with prompts = pc. +int romfs_shaped_pc_ready(unsigned w, unsigned h) { return shaped_folder_ready("romfs-patches-pc", w, h); } + static void load_index(void) { char path[2100]; index_path(path, sizeof path); diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index aa845156..e8e57ef4 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -791,8 +791,8 @@ static const char *prompts_note(bool *restart) { const char *layout = cfg_value("TOTK_PAD_LAYOUT"), *used = romfs_prompts(); const char *want = input_prompts_folder(input_prompts_pick(cfg_get("TOTK_PROMPTS"), layout), layout); if (want == used || (want && used && !strcmp(want, used))) return nullptr; - // A game resolution of another shape needs a folder of its own for the PC prompts (romfs-patches-pc-x), which - // the setup does not write: the shape is the reason then, not the setup. + // A game resolution of another shape needs a folder of its own for the PC prompts (romfs-patches-pc-x). The + // setup writes one for each size the Screen tab offers; at any other size the shape is the reason, not the setup. int off = romfs_prompts_off(want); if (want && !romfs_prompts_ready(want) && off != ROMFS_PROMPTS_SHAPE) return menu_text(UI_MENU_PROMPTS_MISSING); switch (off) { diff --git a/tests/romfs_prompts_test.py b/tests/romfs_prompts_test.py index a21cdd50..0b9c55ee 100644 --- a/tests/romfs_prompts_test.py +++ b/tests/romfs_prompts_test.py @@ -13,6 +13,7 @@ import importlib.util import io import json import random +import shutil import struct import subprocess import sys @@ -572,6 +573,34 @@ class PromptsTest(unittest.TestCase): self.assertEqual(proc.returncode, 0, proc.stderr) self.assertEqual((out / 'manifest.tsv').read_bytes(), (self.outs['v121', 'xbox'][0] / 'manifest.tsv').read_bytes()) + def test_reuse_links_the_files_that_match_and_writes_the_same_overlay(self): + romfs = self.romfs['v143'] + pc, pc_summary = self.outs['v143', 'pc'] + source = self.base / 'reuse' / 'source' # a copy, so the class's folder is never linked or rewritten + shutil.copytree(pc, source) + out = self.base / 'reuse' / 'pc' + proc = romfs.run(out, '--prompts', 'pc', '--reuse', str(source)) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertEqual((out / 'manifest.tsv').read_bytes(), (pc / 'manifest.tsv').read_bytes()) + self.assertEqual(sorted(json.loads(proc.stdout.strip().splitlines()[-1])['reused']), sorted(manifest(pc))) + self.assertEqual(pc_summary['reused'], []) + # From the Xbox prompts only what both share is taken; the rest is made, and the overlay is the same. + xbox = self.outs['v143', 'xbox'][0] + out = self.base / 'reuse' / 'from-xbox' + proc = romfs.run(out, '--prompts', 'pc', '--reuse', str(xbox)) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertEqual((out / 'manifest.tsv').read_bytes(), (pc / 'manifest.tsv').read_bytes()) + shared = {p for p, row in manifest(pc).items() if manifest(xbox).get(p) == row} + self.assertEqual(set(json.loads(proc.stdout.strip().splitlines()[-1])['reused']), shared) + # A new run into the source folder replaces its files instead of writing through the links. + before = {p: (self.base / 'reuse' / 'pc' / p).read_bytes() for p in manifest(pc)} + ini = self.base / 'reuse' / 'totk.ini' + ini.write_text('[input]\nbind_A = key:F\n', encoding='utf-8') + proc = romfs.run(source, '--prompts', 'pc', '--binds', str(ini)) + self.assertEqual(proc.returncode, 0, proc.stderr) + self.assertNotEqual((source / 'manifest.tsv').read_bytes(), (pc / 'manifest.tsv').read_bytes()) + self.assertEqual({p: (self.base / 'reuse' / 'pc' / p).read_bytes() for p in manifest(pc)}, before) + def test_unrecognised_romfs_fails_before_writing(self): cases = {'no text fonts': (dict(text_fonts=False), icons.TEXT_FONTS[0]), 'no size entry': (dict(restbl_skips=(REGIONAL_FONT,)), 'has no entry in the resource size table'), diff --git a/tools/make_romfs_patches.py b/tools/make_romfs_patches.py index f9139543..0a3946d1 100644 --- a/tools/make_romfs_patches.py +++ b/tools/make_romfs_patches.py @@ -45,6 +45,10 @@ from the game's own files on the player's machine, nothing added (tools/button_i guide's images (A, B, X, Y, L, ZL and the stick's two). The one-letter panes around the action prompt name the keys with one character of the Normal font (E, an arrow for Shift, a bar for Space, L and R for the mouse buttons). +--reuse : an overlay made before, such as romfs-patches-pc for romfs-patches-pc-x. Each file of it whose +contents match the new one is hard-linked (copied where links fail) instead of compressed again; the summary's reused +lists them. The output is byte for byte the same as without it. + Input: build/extracted//romfs (romfs.bin and index.json, verified by tools/extract.py). Output: build//romfs-patches/ by default (romfs-patches-