From f9f0505ed1aab7261502cdb6f69a477b9ea52a46 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 04:52:28 -0500 Subject: [PATCH] Build a large compute shader ahead only when a run has dispatched it, and release the shaders of pipelines the boot does not keep. The 2 GiB of device memory a filled cache cost was not the thousands of compute pipelines but twenty of them: the compute shaders of 0.9 to 2.0 million SPIR-V words, the game's tensor-core upscaler programs, which nothing dispatches. A pipeline of one holds about 100 MiB of device memory from the moment it exists, and the driver keeps that memory when the pipeline is destroyed, so keeping only the recently dispatched pipelines did not help: the boot pass still created the twenty from pipeline-cache.bin at every start. Measured with --probe-prewarm on a copy of an installed copy's caches (RTX 5090, NVIDIA 621.01): the pass over all 4,066 compute shaders took the device memory from 386 to 2,398 MiB and the process's commit up by 2.5 GB; the same pass before the twenty were in the cache left the device memory at 386 MiB, and all 4,046 other pipelines kept at once cost 2 MiB. A compute shader over NX2NAVI_LIB_PREBUILD_BIG is now built ahead, from the caches or by the background builder, only when the usage record says a run dispatched it. None of the twenty is among the 138 shaders eleven runs dispatched. Should the game dispatch one for the first time, that dispatch compiles it once and later boots build it ahead. The boot also reads the shader of a pipeline it does not keep as a copy and releases it after the build, instead of leaving 3,900 shaders loaded (240 MB of commit); a first dispatch then reads its shader from the disk, 0.04 to 0.10 ms in the probe. With both, the same pass on the same caches leaves the device memory at 386 MiB and raises the commit by 47 MB; 256 first dispatches after it took 0.13 ms on average, 1.8 ms at most, all read from the cache file. With compute_keep = all, which builds everything as before, the pass ends at 2,528 MiB. Not yet run in the game. Co-Authored-By: Claude Fable 5.2 --- docs/architecture.md | 7 +-- docs/building.md | 3 +- nx2navi.ini.example | 5 +- runtime/vk_memory.c | 6 +-- runtime/vk_pipeline.c | 106 ++++++++++++++++++++++++++++-------------- 5 files changed, 84 insertions(+), 43 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index 8c5e63ef..918324d1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -118,9 +118,10 @@ keeps per-core data without locks and relies on that, so the host models it (`ru are compiled outside the pipeline lock, and one compile serves every thread that needs it. The boot builds the pipeline of every compute shader play recorded or the shader preparation listed, but keeps only those of the shaders dispatched most recently (`compute-used.bin`, `compute_keep`): the others go into the pipeline cache and are - destroyed, and their first dispatch creates them from it. A kept pipeline holds driver memory whether it is used or - not, and the list has thousands of compute shaders a session never dispatches (`runtime/vk_pipeline.c`, "compute - pipelines the boot keeps"). + destroyed, and their first dispatch creates them from it. A compute shader of more than half a million SPIR-V words + is built ahead only when a run has dispatched it: a pipeline of one holds about 100 MiB of device memory whether it + is used or not, also after it is destroyed, and the list has about twenty of them that the game never dispatches + (`runtime/vk_pipeline.c`, "compute pipelines the boot keeps"). - The game's tensor-core upscaler is replaced by a decode-and-scale pass (`vk_tonemap.c`); `vk_fsr.c` runs AMD FSR 1 (EASU, RCAS) on the present when the window is larger than the picture. - HDR (`vk_hdr.c`): the game tonemaps its scene into a float target, upscales it, draws the HUD into a second float diff --git a/docs/building.md b/docs/building.md index 9e280ad4..be38ce59 100644 --- a/docs/building.md +++ b/docs/building.md @@ -306,7 +306,8 @@ exe's own version first, then of the other one. `--probe-prewarm` runs the boot's pipeline stages without the game, from the caches next to the exe (it skips when there are none): the pipeline cache load, the shader prewarm, the recorded pipelines and the compute pipeline prebuild, with a `mem: boot stage` log line after each that gives the device memory and the process commit so far. It then asks -for compute pipelines the boot did not keep, as their first dispatch would, and prints what one costs. A change to what +for compute pipelines the boot did not keep, as their first dispatch would, and prints what one costs. The device +memory after the compute stage should be what it was before the stage, apart from the kept pipelines. A change to what the boot builds or keeps (`runtime/vk_pipeline.c`: `prewarm_run`, `lb_compute`, the `compute-used.bin` record) passes it. Shaders: `.venv-py314\Scripts\python tools\shader_census.py --version --translate` extracts every Maxwell shader diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 9b51914f..a9c1594c 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -106,8 +106,9 @@ # lib_prebuild_threads = 2 # How many compute pipelines the start builds and keeps: those of the compute shaders the game used most recently. # Every other known one is built into the pipeline cache and released again, and made from the cache when the game -# first uses it (well under a millisecond). all: the start keeps the pipeline of every known compute shader, as -# versions up to 0.6.1 did, which held more than 2 GB of video memory. 0: none are kept. +# first uses it (well under a millisecond). Very large compute shaders the game has never used are not built at all. +# all: the start builds and keeps the pipeline of every known compute shader, as versions up to 0.6.1 did, which held +# more than 2 GB of video memory. 0: none are kept. # compute_keep = 512 # 1: very large compute shaders that no cache holds are built on one background thread after the start. 0: each is # built when the game first uses it, which can hold that frame for a second or two. diff --git a/runtime/vk_memory.c b/runtime/vk_memory.c index 0135a0df..e07431f4 100644 --- a/runtime/vk_memory.c +++ b/runtime/vk_memory.c @@ -2685,9 +2685,9 @@ static void outside_watch(uint64_t frame) { // One line per boot stage (vk_pipeline.c calls it after the pipeline cache load, the shader prewarm, the recorded // pipelines and the compute pipeline prebuild): the device-local memory in use, the part of it VMA did not allocate // (outside_watch's "outside VMA": pipelines and the driver's own, before the swapchain and the upscaler exist), and the -// process's commit and working set. Subtracting two lines gives what a stage left behind: on an installed copy with -// its prepared shader list the boot's compute pipelines alone held 2.3 GiB of device memory on an RTX 5090 (4,066 -// pipelines kept, 95 of them dispatched before the first village), which is what this line was added to place. +// process's commit and working set. Subtracting two lines gives what a stage left behind: on an installed copy the +// compute pipeline prebuild alone left 2.1 GiB of device memory on an RTX 5090, held by twenty very large compute +// shaders no run dispatches (vk_pipeline.c, "compute pipelines the boot keeps"), which this line was added to place. void vk_mem_stage_log(const char *stage) { VkCtx *c = vk_ctx(); double proc, used, limit; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index f30c354f..a96965b0 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -4113,6 +4113,7 @@ static struct { // skipped: variants of the skip list (SK below) uint64_t built[3], have[3], fail[3], warmed[3], noshader, nolib, skipped, done, cs_draw; uint64_t big_words, deferred; // LB_BIG_WORDS; compute shaders over it left to their dispatch + uint64_t big_unused; // compute shaders over it that no run dispatched: not built at all double warm_ms[3]; volatile int32_t kept; // libraries entered in libs[] so far int32_t keep_n; @@ -4296,25 +4297,35 @@ static int cs_flight_remove(const GfxShader *cs) { // 1 when a dispatch waited // ---- compute pipelines the boot keeps (compute-used.bin next to pipeline-keys.bin; NX2NAVI_COMPUTE_KEEP) // The boot prebuild (lb_compute) used to build the pipeline of every compute shader of the warm list (the ones play -// recorded and the prepared ones, shader-prepared.bin) and keep all of them for the process. A pipeline that exists -// holds driver memory whether or not anything dispatches it: on an RTX 5090 (NVIDIA 621.01) the 4,066 compute shaders -// of an installed copy's list (42,344 variants, 42,142 of them prepared) held 2.3 GiB of device memory and a matching -// share of the process's commit from the title screen on, while a boot into the first village dispatched 95 of them. -// That is memory an 8 to 12 GB card needs for textures, and a copy with prepared shaders pays it from its first boot. -// So the boot keeps only the pipelines of the NX2NAVI_COMPUTE_KEEP (default 512) compute shaders dispatched most -// recently, and builds every other one into the pipeline cache and destroys it at once (lb_compute keep 0): the cache -// then holds it, so its first dispatch creates it without compiling, and no pipeline of an unused shader stays. On -// that machine a compute pipeline came from pipeline-cache.bin in 0.25 ms on average at boot, and 256 first dispatches -// of pipelines the boot had not kept (--probe-prewarm, with a game running beside it) took 0.19 ms on average and 3 ms -// at most. Built and destroyed one after the other, they leave nothing behind in the driver either: 372 of them left -// its device memory where it was. NVIDIA's driver does not hand back the device memory of destroyed pipelines, it -// keeps it for the next ones (400 test pipelines, destroyed together: no change), so building all of them first and -// destroying them afterwards would end at the same 2.3 GiB. +// recorded and the prepared ones, shader-prepared.bin) and keep all of them for the process: 4,066 on an installed +// copy (42,344 variants in its list, 42,142 of them prepared), of which a boot into the first village dispatched 95. +// What that cost was twenty of them. A copy's list has about twenty compute shaders of 0.9 to 2.0 million SPIR-V +// words: the game's tensor-core upscaler programs, which the game never dispatches natively (vk_exec_draw.c +// native_tensor) and which no run of the usage record below ever dispatched. A pipeline of one holds about 100 MiB +// of device memory from the moment it exists, used or not, and the driver keeps that memory when the pipeline is +// destroyed. On an RTX 5090 (NVIDIA 621.01) a boot pass that read all 4,066 pipelines from pipeline-cache.bin took the +// device memory from 386 to 2,398 MiB although it destroyed 3,949 of them at once; the same pass before the twenty +// were in the cache left it at 386, and building only the twenty took it to 1,370 (--probe-prewarm, 2026-11-10; the +// game showed 608 to 2,743 MiB over the same stage). The 4,046 others are cheap: all of them kept cost 2 MiB of device +// memory and 0.7 GB of the process's commit. So a copy with prepared shaders and a filled pipeline cache held 2 GiB +// for nothing from the title screen on, on cards that need that memory for textures. +// What the boot does now: +// - A compute shader over LB.big_words is built ahead only when a run has dispatched it (lb_compute, its keep +// argument). One that no run dispatched is not created at all: not from the pipeline caches and not by the +// background builder (bg_main). If the game does dispatch one for the first time, that dispatch builds it, which +// holds the frame for the compile once (0.65 to 1.9 s measured for such a shader), and the record has it built ahead +// from the next boot on. +// - Of the others the boot keeps the pipelines of the NX2NAVI_COMPUTE_KEEP (default 512) dispatched most recently, and +// builds every other one into the pipeline cache and destroys it at once (lb_compute keep 0), from a copy of its +// shader that is released again (lb_worker): the cache then holds it, so its first dispatch creates it without +// compiling. On that machine 94 first dispatches in play took 0.24 ms on average and 1.6 ms at most, 14 of them in +// the busiest 33 ms; reading the shader from the disk at that moment adds 0.05 to 0.15 ms (--probe-prewarm). // The record: one entry per compute shader a run dispatched (its input id) with the number of the last run that did; // each run that dispatches anything takes the next number. Kept at boot are the keep_n entries with the highest // numbers, so a short run does not push out what a long one used, and nothing is forgotten until newer shaders take // its place. Without a record (the first run that has one) nothing is kept and the run pays the first dispatches once. -// NX2NAVI_COMPUTE_KEEP=all keeps every compute shader of the warm list as before; 0 keeps none. +// NX2NAVI_COMPUTE_KEEP=all builds and keeps every compute shader of the warm list, the large ones too, as before; 0 +// keeps none. #define CU_VERSION 1 typedef struct { char magic[8]; uint32_t version, rec_size, run, pad; } CuHeader; typedef struct { uint8_t iid[32]; uint32_t run, pad; } CuRec; @@ -4831,7 +4842,8 @@ static void bg_start(void) { } // keep 0: the pipeline is built for the pipeline cache only and destroyed at once, unless a dispatch came to wait for -// it meanwhile (comment at compute-used.bin). +// it meanwhile; a shader over LB.big_words is then not built at all (comment at compute-used.bin). cs may be a copy +// the caller releases afterwards when keep is 0: nothing here holds on to it then. static void lb_compute(const GfxShader *cs, const uint8_t iid[32], int keep) { struct { const GfxShader *cs; uint64_t compute; } k = {cs, 1}; // no padding: the key is hashed and compared as bytes uint64_t h = key_hash(&k, sizeof k); @@ -4840,6 +4852,12 @@ static void lb_compute(const GfxShader *cs, const uint8_t iid[32], int keep) { uint64_t words = 0; gfx_shader_spirv(cs, &words); int big = LB.big_words && words > LB.big_words; // only from the caches (LB_BIG_WORDS) + if (big && !keep) { // no run dispatched it: a pipeline of it would hold about 100 MiB of device memory unused + plat_mutex_lock(&LB.stat); + LB.big_unused++; + plat_mutex_unlock(&LB.stat); + return; + } if (big && !P.cache_control) { if (bg_on()) bg_queue(cs, iid, words); else lb_defer(iid, words); @@ -4968,7 +4986,8 @@ static void lb_report(const char *how) { "cache only: vertex %llu (avg %.1f ms), fragment %llu (avg %.1f ms), compute %llu (avg %.1f ms); %llu failed, %llu not in the shader cache, " "%llu vertex/fragment skipped (graphics pipeline library off), %llu skipped (the driver crashed on them before, " "shader-prepare-skip.txt), %llu large compute shaders left to their first dispatch (NX2NAVI_LIB_PREBUILD_BIG), %zu " - "queued for the background builder (NX2NAVI_LIB_PREBUILD_BIG_BG); process working set %.0f MB", how, (unsigned long long)LB.done, LB.n, + "queued for the background builder (NX2NAVI_LIB_PREBUILD_BIG_BG), %llu large compute shaders not built because " + "no run has dispatched them; process working set %.0f MB", how, (unsigned long long)LB.done, LB.n, now_ms() - LB.t0, (unsigned long long)LB.built[0], LB.built[0] ? LB.ms[0] / LB.built[0] : 0.0, (unsigned long long)LB.have[0], (unsigned long long)LB.built[1], LB.built[1] ? LB.ms[1] / LB.built[1] : 0.0, (unsigned long long)LB.have[1], (unsigned long long)LB.built[2], LB.built[2] ? LB.ms[2] / LB.built[2] : 0.0, @@ -4977,7 +4996,7 @@ static void lb_report(const char *how) { (unsigned long long)LB.warmed[2], LB.warmed[2] ? LB.warm_ms[2] / LB.warmed[2] : 0.0, (unsigned long long)(LB.fail[0] + LB.fail[1] + LB.fail[2]), (unsigned long long)LB.noshader, (unsigned long long)LB.nolib, (unsigned long long)LB.skipped, - (unsigned long long)LB.deferred, BG.n, (double)mem_proc); + (unsigned long long)LB.deferred, BG.n, (unsigned long long)LB.big_unused, (double)mem_proc); plat_mutex_unlock_shared(&LB.stat); } @@ -4996,19 +5015,27 @@ static void lb_worker(void *arg) { plat_mutex_unlock(&LB.stat); continue; } + // A compute shader whose pipeline does not stay is read as a copy of this thread's own and released after its + // build: kept in the shader cache's memory, the 3,900 of an installed copy's list were 240 MB of the process's + // commit for shaders most runs never dispatch. A dispatch that does come reads its shader then (0.05 to + // 0.15 ms from the files this pass has just read). The shaders of kept pipelines and libraries stay loaded: + // their pipeline and library entries point at them. + int keep = i < LB.n_keep, owned = 0; plat_thread_priority(PLAT_PRIO_NORMAL); // gfx_shader's cache lock, disk reads - const GfxShader *sh = gfx_shader_load_input(LB.e[i].iid, 0); + const GfxShader *sh = keep || LB.e[i].stage != GFX_STAGE_CS ? gfx_shader_load_input(LB.e[i].iid, 0) + : gfx_shader_load_private(LB.e[i].iid, &owned); plat_thread_priority(PLAT_PRIO_IDLE); if (!sh) { plat_mutex_lock(&LB.stat); LB.noshader++; plat_mutex_unlock(&LB.stat); } else if (gfx_shader_stage(sh) == GFX_STAGE_CS) { - lb_compute(sh, LB.e[i].iid, i < LB.n_keep); - } else if (gfx_shader_stage(sh) == GFX_STAGE_VS || gfx_shader_stage(sh) == GFX_STAGE_FS) { - if (P.gpl) lb_library(sh, gfx_shader_stage(sh) == GFX_STAGE_VS, 1); + lb_compute(sh, LB.e[i].iid, keep); + } else if (!owned && (gfx_shader_stage(sh) == GFX_STAGE_VS || gfx_shader_stage(sh) == GFX_STAGE_FS)) { + if (P.gpl) lb_library(sh, gfx_shader_stage(sh) == GFX_STAGE_VS, 1); // a kept library points at sh: never a copy else lb_skip_lib(); } + if (owned) gfx_shader_release_private(sh); plat_mutex_lock(&LB.stat); LB.done++; plat_mutex_unlock(&LB.stat); @@ -5420,10 +5447,12 @@ int vk_pipeline_warm_shaders(void) { // the shader prewarm, the recorded pipelines and the compute pipeline prebuild, each followed by vk_mem_stage_log's // line, so the log tells what every stage leaves in device memory and in the process's commit. Then up to // PROBE_DISPATCHES compute shaders whose pipeline the boot did not keep are asked for as a dispatch would -// (vk_pipeline_compute), which gives what a first dispatch costs on this machine and how many came from the pipeline -// cache file. Fails when one of those pipelines is not created, when the prebuild kept a pipeline it should not have, -// or when the record of dispatched shaders, written to a file of the probe's own and read back, does not hold the -// probe's dispatches. compute-used.bin itself stays as it is; the pipeline cache is saved as after a boot. +// (gfx_shader_load_input for the shader, then vk_pipeline_compute), which gives what a first dispatch costs on this +// machine and how many came from the pipeline cache file. Fails when one of those pipelines is not created, when the +// prebuild kept a pipeline it should not have, when a large compute shader went to the background builder although no +// run dispatched it, or when the record of dispatched shaders, written to a file of the probe's own and read back, +// does not hold the probe's dispatches. compute-used.bin itself stays as it is; the pipeline cache is saved as after +// a boot. #define PROBE_DISPATCHES 256 int vk_pipeline_probe_prewarm(void) { vk_pipeline_async_enable(); @@ -5442,27 +5471,36 @@ int vk_pipeline_probe_prewarm(void) { vk_pipeline_stats(&st); plat_mutex_lock_shared(&LB.stat); uint64_t kept = LB.built[2], cache_only = LB.warmed[2], failed = LB.fail[2], there = LB.have[2]; + uint64_t big_unused = LB.big_unused; + size_t queued = BG.n; plat_mutex_unlock_shared(&LB.stat); printf("probe-prewarm: %zu compute shaders, %zu to keep: %llu kept, %llu built into the pipeline cache only, %llu " - "there already, %llu failed; %llu graphics pipelines\n", LB.n, LB.n_keep, (unsigned long long)kept, + "there already, %llu failed, %llu large ones not built (no run dispatched them), %zu large ones for the " + "background builder; %llu graphics pipelines\n", LB.n, LB.n_keep, (unsigned long long)kept, (unsigned long long)cache_only, (unsigned long long)there, (unsigned long long)failed, - (unsigned long long)st.graphics); + (unsigned long long)big_unused, queued, (unsigned long long)st.graphics); int fails = kept > LB.n_keep; // a dispatch never waits in this mode, so nothing else may have been kept if (fails) printf("probe-prewarm: FAIL: %llu pipelines kept, %zu expected at most\n", (unsigned long long)kept, LB.n_keep); + if (queued > LB.n_keep) // only a shader a run dispatched may be built ahead when it is large + fails++, printf("probe-prewarm: FAIL: %zu large compute shaders queued for the background builder, %zu kept " + "shaders at most could be among them\n", queued, LB.n_keep); uint32_t asked = 0, got = 0; - double t_all = 0, t_max = 0; + double t_all = 0, t_max = 0, t_load = 0; for (size_t i = LB.n_keep; i < LB.n && asked < PROBE_DISPATCHES; i++) { - const GfxShader *sh = LB.e[i].stage == GFX_STAGE_CS ? gfx_shader_load_input(LB.e[i].iid, 0) : NULL; + if (LB.e[i].stage != GFX_STAGE_CS) continue; + double l0 = now_ms(); + const GfxShader *sh = gfx_shader_load_input(LB.e[i].iid, 0); // from the disk: the boot released its copy + double load = now_ms() - l0; uint64_t words = 0; if (!sh || gfx_shader_stage(sh) != GFX_STAGE_CS) continue; gfx_shader_spirv(sh, &words); - if (LB.big_words && words > LB.big_words) continue; // those wait for the background builder + if (LB.big_words && words > LB.big_words) continue; // a first dispatch of one compiles it: not what is timed here const VkPipeInfo *info = NULL; double t0 = now_ms(); VkPipeline p = vk_pipeline_compute(sh, &info); double ms = now_ms() - t0; asked++, got += p != VK_NULL_HANDLE; - t_all += ms; + t_all += ms, t_load += load; if (ms > t_max) t_max = ms; } vk_mem_stage_log("the probe's first dispatches"); @@ -5470,8 +5508,8 @@ int vk_pipeline_probe_prewarm(void) { uint64_t hit = CF.hit; plat_mutex_unlock_shared(&P.lock); printf("probe-prewarm: %u first dispatches of pipelines the boot did not keep: %u created, avg %.2f ms, longest %.2f " - "ms, %llu read from the pipeline cache file\n", asked, got, asked ? t_all / asked : 0.0, t_max, - (unsigned long long)hit); + "ms, %llu read from the pipeline cache file; reading their shaders took avg %.2f ms\n", asked, got, + asked ? t_all / asked : 0.0, t_max, (unsigned long long)hit, asked ? t_load / asked : 0.0); if (got != asked) fails++, printf("probe-prewarm: FAIL: %u pipelines were not created\n", asked - got); if (CU.opened) { char rp[sizeof CU.path + 8];