diff --git a/docs/architecture.md b/docs/architecture.md index 5f762ff5..6c0c9baa 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -155,6 +155,12 @@ keeps per-core data without locks and relies on that, so the host models it (`ru 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 pipeline cache is two files beside the game. `pipeline-cache.bin` holds what the preparation built and what + earlier runs made; what a run compiles goes into `pipeline-cache-play.bin`, so one new pipeline in play writes a + small file and not hundreds of megabytes. A cache is written only when its data grew. The small file is merged + into the large one at a start once it reached `pipeline_cache_play_mb`, and by every preparation; the stage + libraries the library warm builds in play go to the driver's own cache alone (`runtime/vk_pipeline.c`, "the play + cache", `cache_save`). - 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 861a099e..1afc3618 100644 --- a/docs/building.md +++ b/docs/building.md @@ -409,6 +409,15 @@ A change to the start's memory warning (`runtime/mem_need.h`, `runtime/memory.c` `python tests\mem_need_check.py []`, which builds the header alone and checks when the game's need counts as short. With the executable it also runs `--probe-memneed` (it prints the machine's commit and the answer and starts nothing) with `mem_need_mb` at 0, at 1 and at a value no machine has, and reads the warning line of the last. +A change to how `pipeline-cache.bin` is read (`runtime/vk_cache_file.h`, `runtime/vk_pipeline.c` `cache_load`) passes +`python tests\pipeline_cache_file_check.py`, which builds the header alone and reads a sparse file just over 2 GB +through it: measured and read whole without a size limit, refused by its real size under one. No game files, no GPU; +about 2 GB of memory for a few seconds. + +A change to the layout of `shader-prepared.bin` (`runtime/prp_file.h`, `runtime/gfx_shader.c` +`gfx_shader_prepare_seed`, `tools/make_game.py` `prepared_from`) passes `python tests\prp_file_check.py`, which builds +the header alone, checks the parse of both versions and has `make_game.py` read the list the C side wrote. No game +files, no GPU. A change to where the lazy write-back is on by default (`runtime/wb_lazy_choice.h`, `runtime/vk_memory.c` `vk_mem_lazy_decide`) passes `python tests\wb_lazy_check.py`, which builds the header alone and checks the choice and diff --git a/docs/launcher.md b/docs/launcher.md index a5570ba1..65eece2b 100644 --- a/docs/launcher.md +++ b/docs/launcher.md @@ -263,7 +263,11 @@ The stub swaps `python.next` in while no Python runs, then starts the new versio game files are already unpacked builds on from them as well (`jobs.InstallJob.args`). 7. **Prepare the Shaders** is skipped once shaders were prepared before (`shader-prepared.bin`): the game keeps using them and translates them again with the new translator in the background (`NX2NAVI_SHADER_CARRY`). With - `shader_carry = 0`, or nothing prepared yet, the step prepares them all as at the install. The step still builds + `shader_carry = 0`, or nothing prepared yet, the step prepares them all as at the install. So it does when the + list was prepared from another shader seed than the one this kit writes: `shader-prepared.bin` carries the digests + of the two seed files and the translator identity (`runtime/prp_file.h`), the step writes the seed first + (`prepare_shaders.py --no-run`, seconds) and compares (`make_game.py` `seed_changed`); a list from before it + recorded its seed is prepared again once. Another translator alone does not prepare again. The step still builds the prepared shaders into the graphics driver's cache again (`make_game.py` `warm_driver_cache`): seconds while the driver has them, 20 minutes on a player's PC when the update changed what they are built with. Its bar follows that count from 0 to 100 %, and the Updates row says an update takes from a few minutes to half an hour diff --git a/nx2navi.ini.example b/nx2navi.ini.example index b3a1c50f..1e447edc 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -174,6 +174,12 @@ # pipe_feedback = 1 # pipeline-cache.bin larger than this (MB) starts empty and is rewritten (0: no limit). # pipeline_cache_max_mb = 512 +# 1: the pipelines a run compiles go into pipeline-cache-play.bin, a small file of its own, so pipeline-cache.bin +# (hundreds of MB) is not written again while you play; the small file is folded into the large one at a start once +# it has grown. 0: one file, written whole whenever a run made a new pipeline. +# pipeline_cache_play = 1 +# The size (MB) at which pipeline-cache-play.bin is folded into pipeline-cache.bin at a start. +# pipeline_cache_play_mb = 64 # 1: where the driver has VK_KHR_pipeline_binary, compiled pipelines are kept in pipeline-binaries.bin under the # runtime's own keys, so a renamed or updated exe starts without compiling them again (pipeline-cache.bin is then # not used). Default 0 (pipeline-cache.bin only) while it is being tested. diff --git a/runtime/cfg.c b/runtime/cfg.c index 06914652..c352c9c9 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -55,7 +55,7 @@ static const char *const known[] = { "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_CLICK", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MICROPHONE", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MONITOR", "MOTION", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOUSE_TRACE", "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", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", + "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PACE_HZ", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_RES_FIXED", "PATCH_SCENE", "PATCH_WIDE_ASPECT", "PATCH_WIDE_HUD", "PATCH_WIDE_MOVIE", "PATCH_WIDE_OUT", "PATCH_WIDE_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPELINE_CACHE_MAX_MB", "PIPELINE_CACHE_PLAY", "PIPELINE_CACHE_PLAY_MB", "PIPE_BINARIES", "PIPE_BINARIES_MAX_MB", "PIPE_FEEDBACK", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "POWER_FULL", "TEX_SAME", "PRESENT_HELD", "HELD_SYNC", "PREPARE_PIPELINES", "PREWARM_FULL", "PREWARM_WAIT_MS", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_REPLAY", "PROFILE_SLOW", "POOL_FAST", "POOL_GROUP", "POOL_GROUP_CHECK", "POOL_PRESNAP", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", @@ -136,7 +136,7 @@ static const struct { const char *key, *section; } sections[] = { {"COMPUTE_KEEP", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"DEPTH_INIT_INLINE", "performance"}, {"DEPTH_ALIAS_LOG", "diagnostics"}, {"TEX_DISUSE", "performance"}, {"TEX_DISUSE_EVICT", "performance"}, {"TEX_GEN_UNDO", "performance"}, {"TEX_REUSE", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"TEX_PROC_COMMIT_MB", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, - {"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, + {"BIND_SKIP", "performance"}, {"SHADER_CACHE_FLUSH", "performance"}, {"SHADER_CARRY", "performance"}, {"PIPELINE_CACHE_PLAY", "performance"}, {"PIPELINE_CACHE_PLAY_MB", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_IDLE", "performance"}, {"SPLIT_IN_PASS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"VK_NAN_BITS", "performance"}, @@ -280,7 +280,9 @@ static const CfgMeta metas[] = { {"LIB_WARM", "1", "0|1|force", RESTART}, // vk_pipeline.c lw_start: the driver's cache of stage libraries, warmed when cold {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "", "0|1", RESTART}, // vk_caps.c vk_caps_gpl: by the driver, off with AMD's for RDNA 2 and older {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, - {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "1", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, + {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, + {"PIPELINE_CACHE_PLAY", "1", "0|1", RESTART}, {"PIPELINE_CACHE_PLAY_MB", "64", "1..65536", RESTART}, // vk_pipeline.c "the play cache": what a run compiles goes into a file of its own + {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "1", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, {"BIG_BUILD_MAX_MB", "4096", "0..262144", RESTART}, {"BUILD_FREE_MB", "4096", "0..1048576", RESTART}, {"BUILD_RESERVE_MB", "64", "0..65536", RESTART}, // vk_pipeline.c gate_enter {"MEM_NEED_MB", "24576", "0..1048576", RESTART}, // memory.c mem_start_note; the default is mem_need.h MEM_NEED_DEFAULT_MB diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index f35468cf..132c395e 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -80,6 +80,7 @@ #include "gfx.h" #include "host.h" #include "platform/platform.h" +#include "prp_file.h" #define CONTROL_SIZE 0x880 #define CODE_SKIP 0x30 // graphics code starts after the first 0x30 bytes of the program @@ -2820,10 +2821,40 @@ void gfx_shader_prepare_recorded(int threads, int procs) { } // ---- prepared variants (shader-prepared.bin next to pipeline-keys.bin) -// "NX2NSPRP", version, record size; records: stage, priority, 2 zero bytes, input id. gfx_shader_prepare_seed writes +// The layout is prp_file.h's: a head that says what the list was prepared from (the two seed files and the +// translator), then one record per variant: stage, priority, 2 zero bytes, input id. gfx_shader_prepare_seed writes // them in priority order, one per input id; gfx_shader_warm_list reads them at boot. -#define PRP_VERSION 1 -#define PRP_REC 36 + +// The sha256 of the whole file; 0 when it cannot be read. +static int file_sha256(const char *path, uint8_t out[32]) { + FILE *f = fopen(path, "rb"); + if (!f) return 0; + fseek(f, 0, SEEK_END); + long len = ftell(f); // a seed past 2 GB is not read by rec_read either + fseek(f, 0, SEEK_SET); + uint8_t *b = len > 0 ? malloc((size_t)len) : NULL; + int ok = b && fread(b, 1, (size_t)len, f) == (size_t)len; + fclose(f); + if (ok) rt_sha256(b, (size_t)len, out); + free(b); + return ok; +} + +// The records of the prepared list at path (PRP_REC bytes each, malloc'd; their count in *n), NULL without any. +// *from, *has_from (both optional): what a version 2 file says it was prepared from. +uint8_t *gfx_shader_prepared_read(const char *path, size_t *n, PrpFrom *from, int *has_from) { + uint64_t size = 0; + uint8_t *f = plat_file_read(path, &size), *out = NULL; + PrpFrom fr = {{0}}; + int has = 0; + const uint8_t *recs = prp_parse(f, f ? size : 0, n, &fr, &has); + if (recs && (out = malloc(*n * PRP_REC))) memcpy(out, recs, *n * PRP_REC); + if (!out) *n = 0; + if (from) *from = fr; + if (has_from) *has_from = has; + free(f); + return out; +} int gfx_shader_prepare_seed(const char *dir, int threads, int procs) { char ans[PATH_CAP + 32], prg[PATH_CAP + 32], out[PATH_CAP + 32]; @@ -2833,6 +2864,23 @@ int gfx_shader_prepare_seed(const char *dir, int threads, int procs) { double t0 = (double)plat_now_ms(); uint64_t fail0 = (uint64_t)n_failures; uint8_t *ab = NULL, (*iid)[32] = NULL; + // What this list is prepared from goes into its head (prp_file.h): the seed as it is on the disk now, and the + // translator before the host options are mixed in, which is what nx2navi_xlate.id holds. The list there before + // is replaced whole at the end; the log says whether it came from this seed. + paths(); + PrpFrom from = {{0}}, was; + int seeded = file_sha256(prg, from.programs) && file_sha256(ans, from.answers), had = 0; + memcpy(from.translator, xlate_base, 32); + size_t n_was = 0; + free(gfx_shader_prepared_read(out, &n_was, &was, &had)); + if (seeded && n_was) { + int same_seed = had && !memcmp(was.programs, from.programs, 32) && !memcmp(was.answers, from.answers, 32); + int same_xl = had && !memcmp(was.translator, from.translator, 32); + rt_log("gfx_shader: prepare: %s holds %zu variants %s; it is replaced by this preparation", out, n_was, + !had ? "and does not say what they were prepared from" + : !same_seed ? "prepared from another seed" + : !same_xl ? "prepared from this seed by another translator" : "prepared from this seed and translator"); + } // For the whole run: the variants built into the driver's cache after it (vk_pipeline.c lb_warm_prepared) are the // current translator's, as before carrying existed. plat_inc32(&C.suspended); @@ -2852,18 +2900,21 @@ int gfx_shader_prepare_seed(const char *dir, int threads, int procs) { by[r[1]]++, stages[r[0]]++, u++; } free(seen.v); - RecHeader h = {{'N', 'X', '2', 'N', 'S', 'P', 'R', 'P'}, PRP_VERSION, PRP_REC}; - uint8_t *file = recs ? malloc(sizeof h + u * PRP_REC + 1) : NULL; + uint8_t *file = recs ? malloc(PRP_HEAD + PRP_FROM_SIZE + u * PRP_REC + 1) : NULL; int ok = file != NULL; if (ok) { - memcpy(file, &h, sizeof h), memcpy(file + sizeof h, recs, u * PRP_REC); - ok = plat_file_write_atomic(out, file, sizeof h + u * PRP_REC); + size_t head = prp_head_write(file, &from); + memcpy(file + head, recs, u * PRP_REC); + ok = plat_file_write_atomic(out, file, head + u * PRP_REC); } - char msg[640]; + char msg[768]; snprintf(msg, sizeof msg, "gfx_shader: prepared %zu shader variants (vertex %zu, fragment %zu, compute %zu; title %zu, " - "warm-up %zu, other %zu) from %lld seed records, %llu translations failed, in %.0f ms; %s %s", u, + "warm-up %zu, other %zu) from %lld seed records (seed programs %02x%02x%02x%02x, answers %02x%02x%02x%02x, " + "translator %02x%02x%02x%02x), %llu translations failed, in %.0f ms; %s %s", u, stages[GFX_STAGE_VS], stages[GFX_STAGE_FS], stages[GFX_STAGE_CS], by[0], by[1], by[2], n, - (unsigned long long)((uint64_t)n_failures - fail0), (double)plat_now_ms() - t0, + from.programs[0], from.programs[1], from.programs[2], from.programs[3], from.answers[0], from.answers[1], + from.answers[2], from.answers[3], from.translator[0], from.translator[1], from.translator[2], + from.translator[3], (unsigned long long)((uint64_t)n_failures - fail0), (double)plat_now_ms() - t0, ok ? "wrote" : "cannot write", out); rt_log("%s", msg); printf("%s\n", msg); @@ -2884,7 +2935,17 @@ size_t gfx_shader_warm_list(GfxWarmEnt **out) { plat_mutex_lock(&rec_io); // no append meanwhile rec_read(RC.ans_path, "NX2NSANS", ANS_VERSION, ANS_REC, &ab, &an); plat_mutex_unlock(&rec_io); - rec_read(prp, "NX2NSPRP", PRP_VERSION, PRP_REC, &pb, &pn); + PrpFrom from; + int has_from = 0; + pb = gfx_shader_prepared_read(prp, &pn, &from, &has_from), pn *= PRP_REC; + if (pb && has_from) + rt_log("gfx_shader: %s was prepared from seed programs %02x%02x%02x%02x, answers %02x%02x%02x%02x, by translator " + "%02x%02x%02x%02x%s", prp, from.programs[0], from.programs[1], from.programs[2], from.programs[3], + from.answers[0], from.answers[1], from.answers[2], from.answers[3], from.translator[0], from.translator[1], + from.translator[2], from.translator[3], + memcmp(from.translator, xlate_base, 32) ? " (not this one: its translations are carried over)" : ""); + else if (pb) + rt_log("gfx_shader: %s does not say what it was prepared from (written before the list recorded it)", prp); size_t na = an / ANS_REC, np = pn / PRP_REC, n = 0; GfxWarmEnt *e = calloc(na + np + 1, sizeof *e); Set64 seen = {0}; diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 28cead8c..99a7d256 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -702,8 +702,15 @@ static int write_swap(const char *path, const void *data, size_t n, int durable) if (len < 0 || len >= (int)sizeof tmp || !widen(tmp, a, WPATH) || !widen(path, b, WPATH)) return 0; HANDLE h = CreateFileW(a, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (h == INVALID_HANDLE_VALUE) return 0; - DWORD put = 0; - int ok = WriteFile(h, data, (DWORD)n, &put, NULL) && put == n && (!durable || FlushFileBuffers(h)); + // In pieces: WriteFile takes a 32-bit count, so a block of 4 GB or more (a pipeline cache can grow that far) + // would otherwise be cut to its low 32 bits and the write refused. + int ok = 1; + for (size_t at = 0; ok && at < n;) { + DWORD piece = n - at > (1u << 30) ? (1u << 30) : (DWORD)(n - at), put = 0; + ok = WriteFile(h, (const char *)data + at, piece, &put, NULL) && put == piece; + at += piece; + } + ok = ok && (!durable || FlushFileBuffers(h)); CloseHandle(h); if (ok && MoveFileExW(a, b, MOVEFILE_REPLACE_EXISTING | (durable ? MOVEFILE_WRITE_THROUGH : 0))) return 1; DeleteFileW(a); diff --git a/runtime/prp_file.h b/runtime/prp_file.h new file mode 100644 index 00000000..7d2ca525 --- /dev/null +++ b/runtime/prp_file.h @@ -0,0 +1,66 @@ +// The layout of shader-prepared.bin, the list of shader variants a preparation translated and built ahead of play +// (runtime/gfx_shader.c gfx_shader_prepare_seed writes it; gfx_shader_warm_list and runtime/vk_pipeline.c lw_start, +// lb_warm_prepared read it; tools/make_game.py prepared_from reads its head). +// +// "NX2NSPRP", version (u32), record size (u32, 36) +// version 2 only: what the list was prepared from, 96 bytes: the sha256 of seed-programs.bin, the sha256 of +// seed-answers.bin (each over the whole file), and the translator identity (the 32 bytes of nx2navi_xlate.id, +// before the host options are mixed in) +// records: stage, priority, 2 zero bytes, input id (32 bytes); in priority order, one per input id +// +// A version 1 file has no such block: it does not say what it was prepared from. The seed decides which variants are in +// the list, and nothing else recorded it: a build that came with another seed (more shaders listed, another pipeline +// list) kept the old list, and the variants only the new seed names were translated when play first drew them. With +// the block the build compares it with the seed it has just written and prepares again when they differ +// (tools/make_game.py step_shaders). The translator identity is recorded for the same comparison; a list prepared by +// another translator is not discarded for that alone, since input ids do not depend on the translator and the game +// carries the translations over (gfx_shader.c, "Carry"). +// tests/prp_file_check.py checks this header against the reader in tools/make_game.py. +#pragma once +#include +#include +#include + +#define PRP_MAGIC "NX2NSPRP" +#define PRP_VERSION 2 +#define PRP_REC 36 +#define PRP_HEAD 16 // magic, version, record size +#define PRP_FROM_SIZE 96 + +typedef struct { + uint8_t programs[32]; // sha256 of seed-programs.bin + uint8_t answers[32]; // sha256 of seed-answers.bin + uint8_t translator[32]; // the translator identity +} PrpFrom; + +// The head of a version 2 file into out (PRP_HEAD + PRP_FROM_SIZE bytes); returns that size. +static inline size_t prp_head_write(uint8_t *out, const PrpFrom *from) { + uint32_t v = PRP_VERSION, r = PRP_REC; + memcpy(out, PRP_MAGIC, 8), memcpy(out + 8, &v, 4), memcpy(out + 12, &r, 4); + memcpy(out + PRP_HEAD, from->programs, 32), memcpy(out + PRP_HEAD + 32, from->answers, 32); + memcpy(out + PRP_HEAD + 64, from->translator, 32); + return PRP_HEAD + PRP_FROM_SIZE; +} + +// The records of the file f of size bytes: a pointer into f and their count in *n; NULL for a file that is no prepared +// list or has no whole record. *has_from: 1 with *from filled for a version 2 file, 0 for a version 1 file. +static inline const uint8_t *prp_parse(const uint8_t *f, uint64_t size, size_t *n, PrpFrom *from, int *has_from) { + uint32_t v = 0, r = 0; + *n = 0, *has_from = 0; + if (!f || size < PRP_HEAD || memcmp(f, PRP_MAGIC, 8)) return NULL; + memcpy(&v, f + 8, 4), memcpy(&r, f + 12, 4); + if (r != PRP_REC || (v != 1 && v != 2)) return NULL; + uint64_t at = PRP_HEAD; + if (v == 2) { + if (size < PRP_HEAD + PRP_FROM_SIZE) return NULL; + memcpy(from->programs, f + PRP_HEAD, 32), memcpy(from->answers, f + PRP_HEAD + 32, 32); + memcpy(from->translator, f + PRP_HEAD + 64, 32); + *has_from = 1, at += PRP_FROM_SIZE; + } + *n = (size_t)((size - at) / PRP_REC); + return *n ? f + at : NULL; +} + +// runtime/gfx_shader.c: the records of the prepared list at path (PRP_REC bytes each, malloc'd; their count in *n), +// NULL without any. *from, *has_from (both optional): what a version 2 file says it was prepared from. +uint8_t *gfx_shader_prepared_read(const char *path, size_t *n, PrpFrom *from, int *has_from); diff --git a/runtime/vk_cache_file.h b/runtime/vk_cache_file.h new file mode 100644 index 00000000..f098037d --- /dev/null +++ b/runtime/vk_cache_file.h @@ -0,0 +1,66 @@ +// Reads pipeline-cache.bin into memory for cache_load (runtime/vk_pipeline.c), with 64-bit sizes on every platform. +// ftell returns a long, which is 32 bits on Windows: a file past 2 GB measured as -1 there, and the run started with an +// empty cache without a word in the log. The length comes from the 64-bit tell, the read runs in pieces no C library +// call has to take whole, and every refusal names its reason and the file's size, so cache_load can log it. +// tests/pipeline_cache_file_check.py reads a sparse file just over 2 GB through it. +#pragma once +#include +#include +#include + +enum { + CACHE_FILE_OK, + CACHE_FILE_NONE, // no such file: a first run + CACHE_FILE_UNSIZED, // the file's length could not be measured + CACHE_FILE_SHORT, // not longer than the header every cache starts with + CACHE_FILE_OVER_CAP, // longer than the limit; not read + CACHE_FILE_NO_MEMORY, // no block of the file's size + CACHE_FILE_READ_FAIL, // the read ended early +}; + +#define CACHE_FILE_PIECE ((size_t)64 << 20) + +static inline const char *cache_file_why(int why) { + switch (why) { + case CACHE_FILE_UNSIZED: return "its size cannot be measured"; + case CACHE_FILE_SHORT: return "it is shorter than a pipeline cache header"; + case CACHE_FILE_OVER_CAP: return "it is over the size limit"; + case CACHE_FILE_NO_MEMORY: return "there is no memory for a block of its size"; + case CACHE_FILE_READ_FAIL: return "reading it ended early"; + default: return ""; + } +} + +// The whole file in a malloc block, or NULL with *why set. *len is the file's length in bytes whenever it could be +// measured, read or not. min: a file of at most this many bytes is refused; cap: so is one longer than cap (0: no limit), +// before any of it is read. +static inline void *cache_file_read(const char *path, uint64_t min, uint64_t cap, uint64_t *len, int *why) { + *len = 0; + FILE *f = fopen(path, "rb"); + if (!f) return *why = CACHE_FILE_NONE, NULL; +#ifdef _WIN32 + int64_t n = _fseeki64(f, 0, SEEK_END) ? -1 : _ftelli64(f); + if (n >= 0 && _fseeki64(f, 0, SEEK_SET)) n = -1; +#else + int64_t n = fseeko(f, 0, SEEK_END) ? -1 : (int64_t)ftello(f); + if (n >= 0 && fseeko(f, 0, SEEK_SET)) n = -1; +#endif + void *data = NULL; + if (n < 0) *why = CACHE_FILE_UNSIZED; + else if (*len = (uint64_t)n, (uint64_t)n <= min) *why = CACHE_FILE_SHORT; + else if (cap && (uint64_t)n > cap) *why = CACHE_FILE_OVER_CAP; + else if ((uint64_t)n > SIZE_MAX || !(data = malloc((size_t)n))) *why = CACHE_FILE_NO_MEMORY; + else { + size_t got = 0, want = (size_t)n; + while (got < want) { + size_t piece = want - got < CACHE_FILE_PIECE ? want - got : CACHE_FILE_PIECE; + size_t r = fread((char *)data + got, 1, piece, f); + got += r; + if (r != piece) break; + } + if (got == want) *why = CACHE_FILE_OK; + else free(data), data = NULL, *why = CACHE_FILE_READ_FAIL; + } + fclose(f); + return data; +} diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 497b1b32..cce67e01 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -47,8 +47,10 @@ #include "host.h" #include "nvn_hmap.h" #include "platform/platform.h" +#include "prp_file.h" #include "tex.h" #include "vk_build_gate.h" +#include "vk_cache_file.h" #include "vk_gpa.h" #include "vk_warm_hint.h" @@ -84,6 +86,11 @@ static struct { int cache_control; // pipelineCreationCacheControl: a create can ask for the pipeline caches only int feedback; // NX2NAVI_PIPE_FEEDBACK: 1 count pipeline cache hits, 2 also log each compute and graphics build uint64_t cache_loaded; // bytes of pipeline-cache.bin the driver took at boot + VkPipelineCache play; // what this run compiles (pipeline-cache-play.bin); VK_NULL_HANDLE: all goes into cache + uint64_t play_loaded; // bytes of pipeline-cache-play.bin the driver took at boot + uint64_t saved[2]; // the data size of cache [0] and play [1] that their files hold; 0: not known + int fold; // the play file's content was merged into cache: saving cache removes the play file + volatile uint64_t play_made; // pipelines this run compiled into play int pv_last; // provokingVertexLast enabled: NVN's default is the last vertex VkShaderStageFlags sg32_stages; // stages that accept requiredSubgroupSize = 32 (0: not available) int reconverge; // modules with subgroup ops get MaximallyReconvergesKHR (spirv_reconverge) @@ -106,6 +113,7 @@ static struct { PFN_vkCmdSetColorWriteMaskEXT set_wmask; PFN_vkCmdSetAlphaToCoverageEnableEXT set_a2c; char cache_path[PLAT_PATH_MAX * 2]; + char play_path[PLAT_PATH_MAX * 2 + 8]; // pipeline-cache-play.bin beside cache_path VkPipeStats stats; } P = {.lock = PLAT_MUTEX_INIT}; @@ -524,17 +532,62 @@ static PlatMutex cache_rw = PLAT_MUTEX_INIT; // but the background builder of large compute shaders) for P.cache itself. A create into it does not hold cache_rw, so // a build of minutes does not keep pipeline-cache.bin from being saved; bg_merge moves its content into P.cache. static __thread VkPipelineCache own_cache; +// 1 on a thread whose creates take no pipeline cache of ours at all (the library warm in play, lw_worker and lw_probe): +// what it builds is wanted in the driver's own disk cache only. Built into P.cache, the 37,572 stage libraries of a +// warm grew it by 1.6 GB (2026-11-11: a save of 2026.7 MB in play, +1612 MB since the boot); the file was then over +// NX2NAVI_PIPELINE_CACHE_MAX_MB, so the next start dropped it whole and compiled the pipelines it had held. +static __thread int no_cache; // New content in P.cache that P.stats does not count, for autosave_once to save: bg_merge's merges, and the compiles // of pipelines built for the cache only (lb_compute keep 0). static volatile uint32_t cache_merges; +// ---- the play cache (NX2NAVI_PIPELINE_CACHE_PLAY, default 1; needs pipelineCreationCacheControl) +// pipeline-cache.bin holds what the preparation built and what earlier runs made: 300 MB and more on an install. One +// file can only be written whole, so a single new pipeline in play had the whole of it copied out of the driver and +// written again (2026-11-11: saves of 358.1 MB for 0.3 MB of new data, and 302 to 306 MB written in each of seven +// sessions that added about 0.5 MB each). So what a run compiles goes into a second cache, P.play, with a file of its +// own, pipeline-cache-play.bin beside the first: a create asks P.cache with +// VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT, which gives the pipeline when that cache or the driver's +// own has it and VK_PIPELINE_COMPILE_REQUIRED when not, and only then builds into P.play (pipe_create_in). In play +// only the play file is saved; pipeline-cache.bin is not written. +// The play file is read back at the next start, into P.play and merged into P.cache in memory (cache_load), so the +// first create finds its pipelines. Once +// it has grown to NX2NAVI_PIPELINE_CACHE_PLAY_MB (default 64) it is merged into P.cache at the start, before play: the +// autosave thread writes pipeline-cache.bin once and removes the play file (cache_save). The preparation modes +// (--prepare-shaders, --warm-shaders, --prepare-pipelines) merge it at any size and build into P.cache alone, as before. +// A pipeline the driver's own cache gives to the first create enters P.cache in memory only: it is in a file again +// when the play file is next folded in, or when the driver no longer has it and it is compiled. +// Without the device feature, with pipeline binaries on, or with the setting 0, P.play stays VK_NULL_HANDLE and +// everything goes into P.cache as before. +static int prep_mode; // a preparation mode (set before ensure_init): no play cache + +static VkResult pipe_create_one(int kind, void *ci, VkPipeline *pipe, VkPipelineCache cache) { + return kind == 2 ? vkCreateComputePipelines(P.device, cache, 1, ci, NULL, pipe) + : vkCreateGraphicsPipelines(P.device, cache, 1, ci, NULL, pipe); +} + static VkResult pipe_create_in(int kind, void *ci, VkPipeline *pipe, VkPipelineCache cache) { + if (no_cache) cache = VK_NULL_HANDLE; int shared = cache && !own_cache; if (cache && own_cache) cache = own_cache; uint64_t t0 = perf_mark_begin(); // NX2NAVI_TRACE=perf: the create on any thread, its wait for cache_rw included if (shared) plat_mutex_lock_shared(&cache_rw); - VkResult r = kind == 2 ? vkCreateComputePipelines(P.device, cache, 1, ci, NULL, pipe) - : vkCreateGraphicsPipelines(P.device, cache, 1, ci, NULL, pipe); + VkResult r; + if (shared && P.play) { + PipeCiHead *hd = ci; + VkPipelineCreateFlags flags = hd->flags; + hd->flags = flags | VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT; + r = pipe_create_one(kind, ci, pipe, cache); + hd->flags = flags; + if (r == VK_PIPELINE_COMPILE_REQUIRED) { + // Not in P.cache nor in the driver's own cache: from P.play, or compiled into it. A caller that asked for + // cached pipelines only (its own FAIL_ON_PIPELINE_COMPILE_REQUIRED) gets the play cache's answer. + r = pipe_create_one(kind, ci, pipe, P.play); + if (r == VK_SUCCESS && *pipe) __atomic_fetch_add(&P.play_made, 1, __ATOMIC_RELAXED); + } + } else { + r = pipe_create_one(kind, ci, pipe, cache); + } if (shared) plat_mutex_unlock_shared(&cache_rw); perf_mark_end(PERF_M_PIPE, t0, 0); return r; @@ -592,7 +645,62 @@ static VkResult pipe_create(int kind, void *ci, VkPipeline *pipe) { return r; } -// Cache data whose header names another device or driver is dropped before Vulkan sees it. +// The data of the pipeline cache file at path for vkCreatePipelineCache (malloc'd, its size in *size), NULL when the +// run starts without it, which the log says with the file's size. Cache data whose header names another device or +// driver is dropped before Vulkan sees it. +// NX2NAVI_PIPELINE_CACHE_MAX_MB (default 512; 0 no limit): a larger file starts the run empty, and the run's +// saves replace it. The file only grows: entries of earlier translators and builds stay, and on NVIDIA +// (621.01) an entry another executable wrote is neither used nor written again, so a renamed or updated exe +// compiles while the file keeps its size (2026-11-09: 650 MB, 15-18 % of the pipelines read from it). The +// whole file sits in process memory twice at the boot (this read and the driver's copy) and is copied out +// at every save of it, which a 16 GB machine near its commit limit cannot spare. The size is checked before the +// read, so a file that is dropped never takes its size in memory. Sizes are 64-bit all the way +// (vk_cache_file.h cache_file_read): a file past 2 GB is read with the limit raised or off, and refused by +// its real size otherwise. +static void *cache_data_read(VkCtx *c, const char *path, size_t *size) { + *size = 0; + if (!path[0]) return NULL; + uint64_t cap = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_MAX_MB"), NULL, 10) << 20, len = 0; + int why = CACHE_FILE_NONE; + void *data = cache_file_read(path, sizeof(VkPipelineCacheHeaderVersionOne), cap, &len, &why); + if (!data) { + if (why == CACHE_FILE_OVER_CAP) + rt_log("vk_pipeline: %s holds %.0f MB, over NX2NAVI_PIPELINE_CACHE_MAX_MB %llu: starting empty; this run " + "rewrites it", path, len / 1048576.0, (unsigned long long)(cap >> 20)); + else if (why != CACHE_FILE_NONE) + rt_log("vk_pipeline: %s (%llu bytes, %.1f MB) is not read: %s; starting empty", path, + (unsigned long long)len, len / 1048576.0, cache_file_why(why)); + return NULL; + } + VkPipelineCacheHeaderVersionOne h; + memcpy(&h, data, sizeof h); + if (h.headerVersion != VK_PIPELINE_CACHE_HEADER_VERSION_ONE || h.vendorID != c->props.vendorID || + h.deviceID != c->props.deviceID || memcmp(h.pipelineCacheUUID, c->props.pipelineCacheUUID, VK_UUID_SIZE)) { + rt_log("vk_pipeline: %s (%.1f MB) was written by another device or driver; starting empty", path, + len / 1048576.0); + free(data); + return NULL; + } + *size = (size_t)len; + return data; +} + +// A pipeline cache from data (may be NULL: empty); VK_NULL_HANDLE when the driver makes none. *kept: the size of +// the data it holds afterwards. A driver may drop initial data it does not accept without failing the create, which +// leaves every build of the run compiling while the cache file looks valid. +static VkPipelineCache cache_make(VkCtx *c, const void *data, size_t size, size_t *kept) { + VkPipelineCache cache = VK_NULL_HANDLE; + VkPipelineCacheCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, .initialDataSize = size, + .pInitialData = size ? data : NULL}; + if (vkCreatePipelineCache(c->device, &ci, NULL, &cache) != VK_SUCCESS) { + ci.initialDataSize = 0, ci.pInitialData = NULL; + if (vkCreatePipelineCache(c->device, &ci, NULL, &cache) != VK_SUCCESS) cache = VK_NULL_HANDLE; + } + *kept = 0; + if (cache && vkGetPipelineCacheData(c->device, cache, kept, NULL) != VK_SUCCESS) *kept = 0; + return cache; +} + static void cache_load(VkCtx *c) { cache_path_init(); pb_init(c); @@ -600,53 +708,67 @@ static void cache_load(VkCtx *c) { rt_log("vk_pipeline: %s is not read or written while pipeline binaries are on", P.cache_path); return; } - void *data = NULL; - size_t size = 0; - FILE *f = P.cache_path[0] ? fopen(P.cache_path, "rb") : NULL; - if (f) { - fseek(f, 0, SEEK_END); - long len = ftell(f); - fseek(f, 0, SEEK_SET); - // NX2NAVI_PIPELINE_CACHE_MAX_MB (default 512; 0 no limit): a larger file starts the run empty, and the run's - // saves replace it. The file only grows: entries of earlier translators and builds stay, and on NVIDIA - // (621.01) an entry another executable wrote is neither used nor written again, so a renamed or updated exe - // compiles while the file keeps its size (2026-11-09: 650 MB, 15-18 % of the pipelines read from it). The - // whole file sits in process memory twice at the boot (this read and the driver's copy) and is copied out - // at every save, which a 16 GB machine near its commit limit cannot spare. The size is checked before the - // read, so a file that is dropped never takes its size in memory. - uint64_t cap = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_MAX_MB"), NULL, 10) << 20; - if (len > 0 && cap && (uint64_t)len > cap) { - rt_log("vk_pipeline: %s holds %.0f MB, over NX2NAVI_PIPELINE_CACHE_MAX_MB %llu: starting empty; this run " - "rewrites it", P.cache_path, len / 1048576.0, (unsigned long long)(cap >> 20)); - len = 0; - } - if (len > (long)sizeof(VkPipelineCacheHeaderVersionOne) && (data = malloc((size_t)len)) && - fread(data, 1, (size_t)len, f) == (size_t)len) - size = (size_t)len; - fclose(f); - VkPipelineCacheHeaderVersionOne h; - if (size) memcpy(&h, data, sizeof h); - if (size && (h.headerVersion != VK_PIPELINE_CACHE_HEADER_VERSION_ONE || h.vendorID != c->props.vendorID || - h.deviceID != c->props.deviceID || memcmp(h.pipelineCacheUUID, c->props.pipelineCacheUUID, VK_UUID_SIZE))) { - rt_log("vk_pipeline: %s was written by another device or driver; starting empty", P.cache_path); - size = 0; - } - } - VkPipelineCacheCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_PIPELINE_CACHE_CREATE_INFO, .initialDataSize = size, - .pInitialData = size ? data : NULL}; - if (vkCreatePipelineCache(c->device, &ci, NULL, &P.cache) != VK_SUCCESS) { - ci.initialDataSize = 0, ci.pInitialData = NULL; - if (vkCreatePipelineCache(c->device, &ci, NULL, &P.cache) != VK_SUCCESS) P.cache = VK_NULL_HANDLE; - } + size_t size = 0, kept = 0; + void *data = cache_data_read(c, P.cache_path, &size); + P.cache = cache_make(c, data, size, &kept); free(data); - // What the driver kept of the file: a driver may drop initial data it does not accept without failing the create, - // which leaves every build of the run compiling while the cache file looks valid. - size_t kept = 0; - if (P.cache && vkGetPipelineCacheData(c->device, P.cache, &kept, NULL) != VK_SUCCESS) kept = 0; P.cache_loaded = kept; + // The file holds this data when the driver took all of it: a save is due only once the cache has grown (cache_save). + P.saved[0] = size && kept == size ? kept : 0; if (size) rt_log("vk_pipeline: %s: %.1f MB read, the driver holds %.1f MB of it", P.cache_path, size / 1048576.0, kept / 1048576.0); + if (!P.cache || !P.cache_path[0]) return; + + // The play cache (see above pipe_create_in). + snprintf(P.play_path, sizeof P.play_path, "%s", P.cache_path); + size_t n = strlen(P.play_path); + if (n > 4 && !strcmp(P.play_path + n - 4, ".bin")) n -= 4; + snprintf(P.play_path + n, sizeof P.play_path - n, "-play.bin"); + int want = cfg_bool("NX2NAVI_PIPELINE_CACHE_PLAY") && !prep_mode, on = want && P.cache_control; + size_t psize = 0, pkept = 0; + void *pdata = cache_data_read(c, P.play_path, &psize); + VkPipelineCache play = psize || on ? cache_make(c, pdata, psize, &pkept) : VK_NULL_HANDLE; + free(pdata); + uint64_t fold_at = (uint64_t)strtoull(cfg_value("NX2NAVI_PIPELINE_CACHE_PLAY_MB"), NULL, 10) << 20; + int fold = play && psize && (!on || psize >= fold_at); + if (fold) { + // Into P.cache now; the file is written by the first save, which then removes the play file. A run that ends + // before that save finds both files as they were and merges again. + VkResult r = vkMergePipelineCaches(c->device, P.cache, 1, &play); + vkDestroyPipelineCache(c->device, play, NULL); + play = VK_NULL_HANDLE; + if (r == VK_SUCCESS) { + P.fold = 1, P.saved[0] = 0; + rt_log("vk_pipeline: %s: %.1f MB read, the driver holds %.1f MB of it; merged into the cache of %s, which is " + "written once now (%s)", P.play_path, psize / 1048576.0, pkept / 1048576.0, P.cache_path, + prep_mode ? "a preparation builds into that file alone" + : !want ? "NX2NAVI_PIPELINE_CACHE_PLAY=0" + : !P.cache_control ? "no pipelineCreationCacheControl on this device" + : "it reached the size at which it is folded in, before play"); + if (on) play = cache_make(c, NULL, 0, &pkept), psize = 0; + } else { + rt_log("vk_pipeline: %s did not merge into the cache of %s (%d); it is left as it is and not used in this run", + P.play_path, P.cache_path, (int)r); + on = 0; + } + } else if (psize) { + // Kept as the play cache, so its file goes on holding what earlier runs made, and merged into P.cache in + // memory, where the first create of pipe_create_in finds its pipelines. Without the merge that create took + // them from the driver's own cache when it had them, at 8 ms a pipeline against 0.4 ms from ours. + VkResult r = vkMergePipelineCaches(c->device, P.cache, 1, &play); + rt_log("vk_pipeline: %s: %.1f MB read, the driver holds %.1f MB of it%s", P.play_path, psize / 1048576.0, + pkept / 1048576.0, r == VK_SUCCESS ? "" : "; it did not merge into the other cache in memory"); + } + P.play = on ? play : VK_NULL_HANDLE; + P.play_loaded = P.play ? pkept : 0; + // An empty play cache is not written (its data is a header): its size counts as saved, as a whole file's does. + P.saved[1] = P.play && (!psize || pkept == psize) ? pkept : 0; + if (P.play) + rt_log("vk_pipeline: what this run compiles goes into %s; %s is not written in play", P.play_path, P.cache_path); + else if (want && !P.cache_control) + rt_log("vk_pipeline: no play cache (no pipelineCreationCacheControl on this device): a new pipeline has %s " + "written whole", P.cache_path); } // The float widths spirv_nan_preserve declares on this device (P.nan_preserve), from NX2NAVI_VK_NAN_PRESERVE: 0 off, @@ -3193,12 +3315,13 @@ typedef struct Job { uint32_t fs_off; uint8_t low; // in the low queue, or running at below-normal priority uint8_t prebuild, demanded; // from the key file; a draw asked for it before it was ready - int worker; // running on A.thread[worker]; -1 queued + int worker; // running on A.thread[worker]; -1 queued; JOB_ON_DRAW on the thread that waits for it VkPipeState s; GKey k; } Job; #define MAX_WORKERS 4 +#define JOB_ON_DRAW (-2) static struct { int on; // set once by vk_pipeline_async_enable, before the replay runs @@ -3210,6 +3333,7 @@ static struct { uint32_t inflight; // queued or running, both queues int low_running; // pool workers running a job taken from the low queue int gate_shut; // pool workers the memory gate has refused a low job since it last admitted one + uint64_t first, taken; // waited-for jobs moved to the head of the draw queue; built by the waiting thread PlatCond idle; // with P.lock: no job queued or running } A = {.idle = PLAT_COND_INIT}; @@ -3662,6 +3786,38 @@ int vk_pipeline_probe_buildgate(void) { return fails ? 1 : 0; } +// The whole build of job j, which the caller took off its queue, and the job's end: its pipeline is entered, the +// threads waiting for it are woken and j is freed. lowrun: 1 on a pool worker that took j from the low queue. Caller +// holds P.lock, which is released for the build. +static void job_build(Job *j, int lowrun) { + plat_mutex_unlock(&P.lock); + VkPipeline pipe = create_graphics(&j->s, &j->k, j->l, j->fs_off); + plat_mutex_lock(&P.lock); + if (pipe || !j->p->fast) j->p->info.pipeline = pipe; // a failed optimised build keeps the fast-linked one + ready_note(j->p, 0); + if (pipe && j->p->fast) P.g.swaps++; + j->p->building = 0; + j->p->job = NULL; + if (pipe) { + P.stats.graphics++, P.stats.async_built++; + if (!j->prebuild) keys_note(&j->s); + } else { + P.stats.failures++; + rt_log("vk_pipeline: graphics pipeline not built: pipeline creation failed"); + } + if (j->prebuild) { + if (pipe) K.built++; + else K.failed++; + K.promoted += j->demanded; + if (!--K.left && !K.loading) prebuild_report(); + } + A.low_running -= lowrun; + if (!--A.inflight) plat_cond_broadcast(&A.idle); + plat_cond_broadcast(&done_cv); + if (pipe && (!j->prebuild || j->demanded)) perf_count(PERF_CNT_PIPE_ASYNC); // a draw waited for it + free(j); +} + static void pipe_worker(void *arg) { int me = (int)(intptr_t)arg; plat_mutex_lock(&P.lock); @@ -3736,33 +3892,8 @@ static void pipe_worker(void *arg) { vkDestroyPipeline(P.device, f, NULL); } } - plat_mutex_unlock(&P.lock); - VkPipeline pipe = create_graphics(&j->s, &j->k, j->l, j->fs_off); - plat_mutex_lock(&P.lock); - if (pipe || !j->p->fast) j->p->info.pipeline = pipe; // a failed optimised build keeps the fast-linked one - ready_note(j->p, 0); - if (pipe && j->p->fast) P.g.swaps++; - j->p->building = 0; - j->p->job = NULL; - if (pipe) { - P.stats.graphics++, P.stats.async_built++; - if (!j->prebuild) keys_note(&j->s); - } else { - P.stats.failures++; - rt_log("vk_pipeline: graphics pipeline not built: pipeline creation failed"); - } - if (j->prebuild) { - if (pipe) K.built++; - else K.failed++; - K.promoted += j->demanded; - if (!--K.left && !K.loading) prebuild_report(); - } - A.low_running -= lowrun; + job_build(j, lowrun); if (lowrun) gate_leave(held); - if (!--A.inflight) plat_cond_broadcast(&A.idle); - plat_cond_broadcast(&done_cv); - if (pipe && (!j->prebuild || j->demanded)) perf_count(PERF_CNT_PIPE_ASYNC); // a draw waited for it - free(j); } return; } @@ -3830,7 +3961,7 @@ static void promote(Pipe *p) { if (!j || !j->low) return; if (j->prebuild && !j->demanded && !p->info.pipeline) p->t_miss = plat_now_ns(); j->low = 0, j->demanded = 1; - if (j->worker < 0) { + if (j->worker == -1) { if (j->prev) j->prev->next = j->next; else A.low = j->next; if (j->next) j->next->prev = j->prev; @@ -3841,12 +3972,52 @@ static void promote(Pipe *p) { A.tail = j; plat_cond_signal(&work_cv); if (U.lib_workers) plat_cond_signal(&lib_cv); // its fast link (lib_worker) - } else if (A.prio_low[j->worker]) { + } else if (j->worker >= 0 && A.prio_low[j->worker]) { A.prio_low[j->worker] = 0; plat_thread_set_priority(A.thread[j->worker], PLAT_PRIO_NORMAL); } } +// A draw waits up to wait_ms for the pipeline of job j, which has no fast link (a mesh pipeline, a pipeline without +// stage libraries, or one whose link failed) and so needs its whole build. While the job is still queued the wait +// buys nothing: the draw queue is first in, first out, and the jobs before it are the fast links and builds of other +// draws' misses, so on a burst of new pipelines the one a draw stood waiting for started last and the draw was +// skipped at the end of its wait all the same. +// - The job goes to the head of the draw queue and a worker is woken, so the next free worker takes it. +// - A wait longer than TAKE_WAIT_MS is not one of a frame's budgets (vk_exec_draw.c frame_wait is at most 500 ms): the +// caller wants this pipeline whatever it takes (a one-shot draw with NX2NAVI_ONESHOT_BUDGET=0). With no worker idle +// the job is then built here at once (job_build), as a library no worker took is (lib_wait). A wait inside a +// budget is never turned into a build on this thread: a compile takes 0.2 to 1.1 s for a mesh pipeline, and the +// budget exists so that the replay thread is not held that long. +// Caller holds P.lock; it is released while the job is built here. +#define TAKE_WAIT_MS 500u +static void job_wanted(Job *j, uint32_t wait_ms) { + if (!j || j->worker != -1 || j->low) return; // running already, or gone + Job *q = NULL; + if (A.head != j) { + for (q = A.head; q && q->next != j; q = q->next) {} + if (!q) return; + } + if (wait_ms > TAKE_WAIT_MS && !U.idle) { + if (q) { + if (!(q->next = j->next)) A.tail = q; + } else if (!(A.head = j->next)) { + A.tail = NULL; + } + j->next = j->prev = NULL; + j->worker = JOB_ON_DRAW; + A.taken++; + job_build(j, 0); + return; + } + if (q) { // to the head + if (!(q->next = j->next)) A.tail = q; + j->next = A.head, A.head = j; + A.first++; + } + plat_cond_signal(&work_cv); +} + // Enters a missing pipeline: built here (mode 0), or queued for a draw (1) or for the prebuild (2). Failures are // entered too. *queued: 1 when a job owns it. Caller holds P.lock. static Pipe *pipe_miss(const VkPipeState *s, const GKey *k, uint64_t h, int mode, int *queued) { @@ -3922,7 +4093,7 @@ static Pipe *pipe_miss(const VkPipeState *s, const GKey *k, uint64_t h, int mode // A job still in the draw queue goes to the head of the low queue: its pipeline has (or is about to have) a fast-linked // one, so the optimised build waits behind other draws' misses. Caller holds P.lock. static void job_lower(Job *j) { - if (!j || j->worker >= 0 || j->low) return; + if (!j || j->worker != -1 || j->low) return; if (A.head == j) { if (!(A.head = j->next)) A.tail = NULL; } else { @@ -4118,6 +4289,7 @@ static VkPipeline graphics(const VkPipeState *s, const VkPipeInfo **info, int as promote(p); uint64_t p0 = wait_ms ? perf_begin() : 0; // the wait counts as the render thread's pipeline time double now; + if (wait_ms) job_wanted(p->job, wait_ms); // releases P.lock when it builds the job here while (p->building && !p->info.pipeline && wait_ms && (now = now_ms()) < end) plat_cond_wait_ms(&done_cv, &P.lock, (uint32_t)(end - now) + 1); if (wait_ms) perf_end(PERF_PIPE, p0); @@ -5297,10 +5469,12 @@ static void bg_merge(VkPipelineCache own, int built) { int locked = 0; while (built && !LB.stop && !(locked = plat_mutex_trylock(&cache_rw))) plat_sleep_ms(2); if (locked) { - VkResult r = vkMergePipelineCaches(P.device, P.cache, 1, &own); + // what play compiled: into the play cache when there is one, so its file gets it and not pipeline-cache.bin + VkResult r = vkMergePipelineCaches(P.device, P.play ? P.play : P.cache, 1, &own); plat_mutex_unlock(&cache_rw); if (r == VK_SUCCESS) __atomic_fetch_add(&cache_merges, 1, __ATOMIC_RELAXED); - else rt_log("vk_pipeline: background compute build: its cache did not merge into %s (%d)", P.cache_path, (int)r); + else rt_log("vk_pipeline: background compute build: its cache did not merge into %s (%d)", + P.play ? P.play_path : P.cache_path, (int)r); } vkDestroyPipelineCache(P.device, own, NULL); } @@ -5720,6 +5894,7 @@ static double lw_build(const GfxWarmEnt *e) { static void lw_worker(void *arg) { (void)arg; plat_thread_priority(PLAT_PRIO_IDLE); + no_cache = 1; // into the driver's own cache only (see no_cache) for (;;) { size_t i = (size_t)plat_inc64(&LW.next) - 1; if (i >= LW.n || LW.stop) break; @@ -5834,12 +6009,11 @@ static void lw_start(void) { if (!cache_dir(dir, sizeof dir)) return; snprintf(path, sizeof path, "%s/shader-prepared.bin", dir); snprintf(LW.stamp, sizeof LW.stamp, "%s/shader-warm.txt", dir); - uint64_t size = 0; - uint8_t *f = plat_file_read(path, &size); - size_t n = f && size >= 16 ? (size_t)(size - 16) / 36 : 0; + size_t n = 0; + uint8_t *f = gfx_shader_prepared_read(path, &n, NULL, NULL); // the records alone, whichever version the file is LW.e = n ? calloc(n, sizeof *LW.e) : NULL; for (size_t i = 0; LW.e && i < n; i++) - memcpy(LW.e[i].iid, f + 16 + 36 * i + 4, 32), LW.e[i].stage = f[16 + 36 * i], LW.e[i].prio = f[16 + 36 * i + 1]; + memcpy(LW.e[i].iid, f + PRP_REC * i + 4, 32), LW.e[i].stage = f[PRP_REC * i], LW.e[i].prio = f[PRP_REC * i + 1]; free(f); if (!LW.e) return; // nothing prepared (a build without the preparation step) LW.n = n; @@ -5973,12 +6147,11 @@ static void lb_warm_worker(void *arg) { static void lb_warm_prepared(const char *dir) { char path[PLAT_PATH_MAX * 2 + 32]; snprintf(path, sizeof path, "%s/shader-prepared.bin", dir); - uint64_t size = 0; - uint8_t *f = plat_file_read(path, &size); - size_t n = f && size >= 16 ? (size_t)(size - 16) / 36 : 0; + size_t n = 0; + uint8_t *f = gfx_shader_prepared_read(path, &n, NULL, NULL); // the records alone, whichever version the file is LB.e = n ? calloc(n, sizeof *LB.e) : NULL; for (size_t i = 0; LB.e && i < n; i++) - memcpy(LB.e[i].iid, f + 16 + 36 * i + 4, 32), LB.e[i].stage = f[16 + 36 * i], LB.e[i].prio = f[16 + 36 * i + 1]; + memcpy(LB.e[i].iid, f + PRP_REC * i + 4, 32), LB.e[i].stage = f[PRP_REC * i], LB.e[i].prio = f[PRP_REC * i + 1]; free(f); if (!LB.e) return; skip_init(dir); @@ -6171,6 +6344,7 @@ static int seed_pipelines(const char *dir) { // nx2navi.exe --prepare-pipelines: the list's pipelines alone, for a folder whose shaders are prepared already. int vk_pipeline_prepare_pipelines(void) { + prep_mode = 1; // builds into pipeline-cache.bin alone (cache_load) if (!ensure_init()) { fprintf(stderr, "prepare-pipelines: no Vulkan device\n"); return 1; @@ -6190,6 +6364,7 @@ int vk_pipeline_prepare_pipelines(void) { // cache key), the seed next to nx2navi.exe is translated on every core, then every prepared variant's stage library or // compute pipeline is built once into the driver's disk cache (lb_warm_prepared). No game runs. int vk_pipeline_prepare_shaders(void) { + prep_mode = 1; // builds into pipeline-cache.bin alone (cache_load) if (!ensure_init()) { fprintf(stderr, "prepare-shaders: no Vulkan device\n"); return 1; @@ -6212,6 +6387,7 @@ int vk_pipeline_prepare_shaders(void) { // 1.8 s) and holds a limited amount: preparing another build dir evicted this one's libraries, and a library a draw // needed then took 40-220 ms in play instead of 0.2 ms. Seconds when the libraries are still there. int vk_pipeline_warm_shaders(void) { + prep_mode = 1; // builds into pipeline-cache.bin alone (cache_load) if (!ensure_init()) { fprintf(stderr, "warm-shaders: no Vulkan device\n"); return 1; @@ -6456,10 +6632,62 @@ void vk_pipeline_stats(VkPipeStats *s) { static PlatMutex save_lock = PLAT_MUTEX_INIT; // one cache save at a time; shutdown takes it to destroy the cache -// Copies P.cache's data into pipeline-cache.bin, with cache_rw held exclusively for the copy only (not the file write). +// Copies a pipeline cache's data into its file, with cache_rw held exclusively for the copy only (not the file write): +// P.play into pipeline-cache-play.bin when there is a play cache, else P.cache into pipeline-cache.bin; and P.cache +// once, with or without a play cache, after the play file was merged into it at the start (P.fold), which then +// removes the play file. +// Only when the data changed: the size of a cache's data is asked first, and a cache whose data has the size its file +// holds is neither copied nor written. A pipeline cache only gains entries, so the same size is the same data. Before, +// every save copied and wrote the whole cache whenever a pipeline had been created since the last one, also when the +// cache had held it: three saves of 358.1 MB in one run that added 0.3 MB (2026-11-11). // The lock is only ever tried, never waited for: a waiting exclusive request would hold back every new build // (a shared request queues behind it), so a pending save could stall the replay. wait_ms: how long to keep trying // while builds run (0: one try). Returns 0 when no build-free moment came, 1 otherwise (saved, or nothing to save). +static int cache_save_one(int which, uint32_t wait_ms) { + VkPipelineCache cache = which ? P.play : P.cache; + const char *path = which ? P.play_path : P.cache_path; + if (!cache) return 1; + uint64_t until = plat_now_ms() + wait_ms; + while (!plat_mutex_trylock(&cache_rw)) { + if (plat_now_ms() >= until) return 0; + plat_sleep_ms(2); + } + size_t size = 0; + void *data = NULL; + int sized = vkGetPipelineCacheData(P.device, cache, &size, NULL) == VK_SUCCESS && size; + int same = sized && size == P.saved[which]; + int got = sized && !same && (data = malloc(size)) && vkGetPipelineCacheData(P.device, cache, &size, data) == VK_SUCCESS; + plat_mutex_unlock(&cache_rw); + static uint64_t skipped[2]; + if (same) skipped[which]++; + if (sized && !same && !got) rt_log("vk_pipeline: %s not saved: no copy of its %.1f MB", path, size / 1048576.0); + if (got) { + uint64_t t0 = plat_now_ms(); + if (!plat_file_write_atomic(path, data, size)) { + rt_log("vk_pipeline: cannot write %s (%.1f MB)", path, size / 1048576.0); + } else { + uint64_t loaded = which ? P.play_loaded : P.cache_loaded; + rt_log("vk_pipeline: %s saved: %.1f MB (%+.1f MB since the boot) written in %llu ms; %llu saves before it were " + "skipped with the data unchanged", path, size / 1048576.0, ((double)size - (double)loaded) / 1048576.0, + (unsigned long long)(plat_now_ms() - t0), (unsigned long long)skipped[which]); + P.saved[which] = size, skipped[which] = 0; + if (!which && P.fold) { // the play file's content is in pipeline-cache.bin now + P.fold = 0; + plat_file_delete(P.play_path); + rt_log("vk_pipeline: %s removed: %s holds its pipelines", P.play_path, P.cache_path); + } + if (P.feedback) { + char when[96]; + snprintf(when, sizeof when, "at a save of %.1f MB (%+.1f MB since the boot)", size / 1048576.0, + ((double)size - (double)loaded) / 1048576.0); + fb_log(when); + } + } + } + free(data); + return 1; +} + static int cache_save(uint32_t wait_ms) { if (!P.ready || !P.cache || !P.cache_path[0]) return 1; // Not under P.lock: holding P.lock through the copy and the file write (about 60 ms on the 1.4.3 fight) stopped @@ -6469,31 +6697,11 @@ static int cache_save(uint32_t wait_ms) { plat_mutex_unlock(&save_lock); return 1; } - uint64_t until = plat_now_ms() + wait_ms; - while (!plat_mutex_trylock(&cache_rw)) { - if (plat_now_ms() >= until) { - plat_mutex_unlock(&save_lock); - return 0; - } - plat_sleep_ms(2); - } - size_t size = 0; - void *data = NULL; - int got = vkGetPipelineCacheData(P.device, P.cache, &size, NULL) == VK_SUCCESS && size && (data = malloc(size)) && - vkGetPipelineCacheData(P.device, P.cache, &size, data) == VK_SUCCESS; - plat_mutex_unlock(&cache_rw); - if (got) { - if (!plat_file_write_atomic(P.cache_path, data, size)) rt_log("vk_pipeline: cannot write %s", P.cache_path); - else if (P.feedback) { - char when[96]; - snprintf(when, sizeof when, "at a save of %.1f MB (%+.1f MB since the boot)", size / 1048576.0, - ((double)size - (double)P.cache_loaded) / 1048576.0); - fb_log(when); - } - } - free(data); + int ok = 1; + if (!P.play || P.fold) ok = cache_save_one(0, wait_ms); + if (P.play) ok = cache_save_one(1, wait_ms) && ok; plat_mutex_unlock(&save_lock); - return 1; + return ok; } // Builds still running after this long at a save that waits (exit, the shader preparation) leave the file as it was. @@ -6528,6 +6736,10 @@ static void save_with(uint32_t wait_ms) { if (P.g.hints) rt_log("vk_pipeline: gpl: looking ahead named %llu pipeline states not known yet and queued %llu stage libraries " "for them", (unsigned long long)P.g.hints, (unsigned long long)P.g.hint_libs); + if (A.first || A.taken) + rt_log("vk_pipeline: whole builds a draw waited for while they were still queued: %llu moved to the head of " + "the queue, %llu built by the waiting thread (a wait outside the frame's budget, no worker idle)", + (unsigned long long)A.first, (unsigned long long)A.taken); if (U.waits || U.queued) rt_log("vk_pipeline: gpl: library jobs for waiting draws: %llu queued, %llu built by a worker, %llu by the " "thread that needed them (no worker free), %llu waits ended at their limit; %llu fast links on the %d " @@ -6551,7 +6763,8 @@ static void save_with(uint32_t wait_ms) { gfx_shader_record_flush(1); if (P.ready && !P.cache) pb_log("at a save"); // with P.cache the line comes with its own save below if (!cache_save(wait_ms) && wait_ms) - rt_log("vk_pipeline: %s not saved: pipeline builds kept running for %u ms", P.cache_path, (unsigned)wait_ms); + rt_log("vk_pipeline: %s not saved: pipeline builds kept running for %u ms", P.play ? P.play_path : P.cache_path, + (unsigned)wait_ms); } // The save at exit and after the shader preparation: it waits up to SAVE_WAIT_MS for a moment with no build running. @@ -6576,7 +6789,7 @@ static void autosave_once(void) { VkPipeStats s; vk_pipeline_stats(&s); uint64_t n = s.graphics + s.compute + cache_merges; // a merge comes after its pipeline was counted (bg_main) - if (n == last_n) return; + if (n == last_n && !P.fold) return; // P.fold: the play file merged at the start is to be written // Only at a moment with no build running (cache_save): while one runs the save waits for the next tick, 500 ms // on, before it logs or flushes anything, so a busy stretch does not repeat the statistics lines every tick. if (P.cache && P.cache_path[0]) { @@ -6638,7 +6851,8 @@ void vk_pipeline_shutdown(void) { } plat_mutex_lock(&save_lock); // not while the autosave thread copies it if (P.cache) vkDestroyPipelineCache(P.device, P.cache, NULL); - P.cache = VK_NULL_HANDLE; + if (P.play) vkDestroyPipelineCache(P.device, P.play, NULL); + P.cache = P.play = VK_NULL_HANDLE; plat_mutex_unlock(&save_lock); memset(&P.stats, 0, sizeof P.stats); plat_xchg32(&P.ready, 0); diff --git a/tests/launcher_test.py b/tests/launcher_test.py index 47aaefea..69e86d4f 100644 --- a/tests/launcher_test.py +++ b/tests/launcher_test.py @@ -1438,6 +1438,11 @@ print(json.dumps({'status': 'pass', 'folders': []})) ''' +def prepared_list(programs, answers, translator, records=1): + """A version 2 shader-prepared.bin (runtime/prp_file.h): the head, what it was prepared from, then records.""" + return b'NX2NSPRP' + struct.pack(' +#include +#include +#include + +#include "vk_cache_file.h" + +#ifdef _WIN32 +#include +#include +#else +#include +#include +#endif + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +#define HEADER 32 // the size of the header every pipeline cache starts with (VkPipelineCacheHeaderVersionOne) + +// A file of len bytes: head at its start, tail as its last bytes, a hole between them that takes no disk space. +static int make_sparse(const char *path, uint64_t len, const char *head, const char *tail) { + size_t hn = strlen(head), tn = strlen(tail); +#ifdef _WIN32 + HANDLE h = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + if (h == INVALID_HANDLE_VALUE) return 0; + DWORD got = 0; + LARGE_INTEGER at = {.QuadPart = (LONGLONG)(len - tn)}; + int ok = DeviceIoControl(h, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &got, NULL) && + WriteFile(h, head, (DWORD)hn, &got, NULL) && got == hn && SetFilePointerEx(h, at, NULL, FILE_BEGIN) && + WriteFile(h, tail, (DWORD)tn, &got, NULL) && got == tn; + CloseHandle(h); + return ok; +#else + int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) return 0; + int ok = write(fd, head, hn) == (ssize_t)hn && pwrite(fd, tail, tn, (off_t)(len - tn)) == (ssize_t)tn; + close(fd); + return ok; +#endif +} + +int main(int argc, char **argv) { + if (argc < 2) return printf("usage: pipeline_cache_file_check \n"), 2; + char path[1024]; + uint64_t len = 0; + int why = -1; + + // no file: a first run, which is not a refusal + snprintf(path, sizeof path, "%s/none.bin", argv[1]); + remove(path); + CHECK(!cache_file_read(path, HEADER, 0, &len, &why) && why == CACHE_FILE_NONE && len == 0, "no file: why %d", why); + + // a file of a header alone holds no pipeline: refused with its size + snprintf(path, sizeof path, "%s/short.bin", argv[1]); + FILE *f = fopen(path, "wb"); + for (int i = 0; f && i < HEADER; i++) fputc(i, f); + if (f) fclose(f); + CHECK(!cache_file_read(path, HEADER, 0, &len, &why) && why == CACHE_FILE_SHORT && len == HEADER, + "header only: why %d, len %llu", why, (unsigned long long)len); + remove(path); + + // a small file: read whole + f = fopen(path, "wb"); + for (int i = 0; f && i < 4096; i++) fputc(i & 0xff, f); + if (f) fclose(f); + uint8_t *d = cache_file_read(path, HEADER, 1 << 20, &len, &why); + CHECK(d && why == CACHE_FILE_OK && len == 4096 && d[4095] == 0xff && d[257] == 1, "small file: why %d, len %llu", why, + (unsigned long long)len); + free(d); + remove(path); + + // just over 2 GB: a 32-bit tell measures it as negative + const uint64_t big = (2ull << 30) + 4096; + snprintf(path, sizeof path, "%s/big.bin", argv[1]); + if (!make_sparse(path, big, "HEAD-OF-THE-CACHE", "TAIL-OF-THE-CACHE")) { + printf("FAIL: cannot make the sparse file %s\n", path); + remove(path); + return 1; + } + f = fopen(path, "rb"); + fseek(f, 0, SEEK_END); + long l32 = ftell(f); + fclose(f); + printf("pipeline cache file: %llu bytes; ftell into a long (%d bits here) gives %ld\n", (unsigned long long)big, + (int)sizeof(long) * 8, l32); + + // under a limit: refused before any read, and its real size comes back for the log line + d = cache_file_read(path, HEADER, 512ull << 20, &len, &why); + CHECK(!d && why == CACHE_FILE_OVER_CAP && len == big, "over the limit: why %d, len %llu", why, (unsigned long long)len); + CHECK(strlen(cache_file_why(why)) > 0, "a refusal has words for the log"); + free(d); + + // with no limit, and with a limit above its size: read whole + for (int pass = 0; pass < 2; pass++) { + d = cache_file_read(path, HEADER, pass ? 4096ull << 20 : 0, &len, &why); + CHECK(d && why == CACHE_FILE_OK && len == big, "pass %d: over 2 GB: why %d (%s), len %llu", pass, why, + cache_file_why(why), (unsigned long long)len); + if (d) { + CHECK(!memcmp(d, "HEAD-OF-THE-CACHE", 17), "pass %d: the start of the file is the start of the block", pass); + CHECK(!memcmp(d + big - 17, "TAIL-OF-THE-CACHE", 17), "pass %d: the last bytes are past 2 GB in the block", pass); + CHECK(d[1ull << 31] == 0 && d[(1ull << 30) + 5] == 0, "pass %d: the hole reads as zeros", pass); + } + free(d); + } + remove(path); + + if (!fails) printf("pipeline cache file: all checks passed\n"); + return fails != 0; +} diff --git a/tests/pipeline_cache_file_check.py b/tests/pipeline_cache_file_check.py new file mode 100644 index 00000000..ef226437 --- /dev/null +++ b/tests/pipeline_cache_file_check.py @@ -0,0 +1,38 @@ +"""Build and run tests/pipeline_cache_file_check.c: runtime/vk_cache_file.h, the read of pipeline-cache.bin with 64-bit +sizes. The test makes a sparse file just over 2 GB in build/test/pipeline_cache_file (it takes no disk space, and about +2 GB of memory for the seconds the read lasts) and removes it. + +Usage: pipeline_cache_file_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'pipeline_cache_file' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('pipeline_cache_file_check.exe' if os.name == 'nt' else 'pipeline_cache_file_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'pipeline_cache_file_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe), str(work)], env=env, timeout=600).returncode + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/prp_file_check.c b/tests/prp_file_check.c new file mode 100644 index 00000000..174d15ae --- /dev/null +++ b/tests/prp_file_check.c @@ -0,0 +1,66 @@ +// runtime/prp_file.h: the layout of shader-prepared.bin. Checks the parse of a version 1 list, of a version 2 list and +// of damaged ones, then writes a version 2 list into the folder given as argv[1] for tests/prp_file_check.py, which +// reads its head with tools/make_game.py prepared_from. Prints each failed check and exits 1 on any. +#include +#include +#include + +#include "prp_file.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +int main(int argc, char **argv) { + if (argc < 2) return printf("usage: prp_file_check \n"), 2; + uint8_t file[PRP_HEAD + PRP_FROM_SIZE + 3 * PRP_REC + 8]; + PrpFrom from, got; + for (int i = 0; i < 32; i++) from.programs[i] = (uint8_t)i, from.answers[i] = (uint8_t)(0x40 + i), from.translator[i] = (uint8_t)(0x80 + i); + size_t n = 99; + int has = 9; + + // version 2: the head, then three records + size_t head = prp_head_write(file, &from); + CHECK(head == PRP_HEAD + PRP_FROM_SIZE && head == 112, "the head is %zu bytes", head); + for (int r = 0; r < 3; r++) { + uint8_t *rec = file + head + r * PRP_REC; + memset(rec, 0, PRP_REC); + rec[0] = (uint8_t)(r + 1), rec[1] = (uint8_t)r, memset(rec + 4, 0xa0 + r, 32); + } + const uint8_t *recs = prp_parse(file, head + 3 * PRP_REC, &n, &got, &has); + CHECK(recs == file + head && n == 3 && has == 1, "version 2: %zu records, has_from %d", n, has); + CHECK(!memcmp(&got, &from, sizeof from), "version 2: what it was prepared from comes back"); + CHECK(recs && recs[PRP_REC * 2] == 3 && recs[PRP_REC * 2 + 4] == 0xa2, "version 2: the third record"); + // a cut last record is not counted + recs = prp_parse(file, head + 2 * PRP_REC + 35, &n, &got, &has); + CHECK(recs && n == 2, "a cut record: %zu", n); + // the head alone, or less: no list + CHECK(!prp_parse(file, head, &n, &got, &has) && n == 0, "no records"); + CHECK(!prp_parse(file, head - 1, &n, &got, &has) && n == 0, "a cut head"); + CHECK(!prp_parse(NULL, 0, &n, &got, &has), "no file"); + + // version 1: records right after the 16 bytes, and nothing about where they came from + uint8_t v1[PRP_HEAD + 2 * PRP_REC]; + uint32_t one = 1, rec = PRP_REC; + memcpy(v1, PRP_MAGIC, 8), memcpy(v1 + 8, &one, 4), memcpy(v1 + 12, &rec, 4); + memset(v1 + PRP_HEAD, 0x11, 2 * PRP_REC); + recs = prp_parse(v1, sizeof v1, &n, &got, &has); + CHECK(recs == v1 + PRP_HEAD && n == 2 && has == 0, "version 1: %zu records, has_from %d", n, has); + + // another version, record size or magic: not read + uint32_t three = 3, other = 40; + memcpy(v1 + 8, &three, 4); + CHECK(!prp_parse(v1, sizeof v1, &n, &got, &has), "version 3"); + memcpy(v1 + 8, &one, 4), memcpy(v1 + 12, &other, 4); + CHECK(!prp_parse(v1, sizeof v1, &n, &got, &has), "another record size"); + memcpy(v1 + 12, &rec, 4), v1[0] = 'X'; + CHECK(!prp_parse(v1, sizeof v1, &n, &got, &has), "another magic"); + + char path[1024]; + snprintf(path, sizeof path, "%s/shader-prepared.bin", argv[1]); + FILE *f = fopen(path, "wb"); + CHECK(f && fwrite(file, 1, head + 3 * PRP_REC, f) == head + 3 * PRP_REC, "cannot write %s", path); + if (f) fclose(f); + + if (!fails) printf("prepared list file: all C checks passed\n"); + return fails != 0; +} diff --git a/tests/prp_file_check.py b/tests/prp_file_check.py new file mode 100644 index 00000000..53e915d4 --- /dev/null +++ b/tests/prp_file_check.py @@ -0,0 +1,53 @@ +"""Build and run tests/prp_file_check.c: runtime/prp_file.h, the layout of shader-prepared.bin with what the list was +prepared from. The list the C side writes is then read by tools/make_game.py prepared_from, so the game's writer and +the build's reader cannot drift apart. No game files, no GPU. + +Usage: prp_file_check.py +""" +import importlib.util +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'prp_file' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('prp_file_check.exe' if os.name == 'nt' else 'prp_file_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'prp_file_check.c'), '-o', str(exe)], env=env) + rc = subprocess.run([str(exe), str(work)], env=env).returncode + if rc: + return rc + sys.path.insert(0, str(ROOT / 'tools')) + spec = importlib.util.spec_from_file_location('make_game', ROOT / 'tools' / 'make_game.py') + mg = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mg) + want = {'programs': bytes(range(32)).hex(), 'answers': bytes(range(0x40, 0x60)).hex(), + 'translator': bytes(range(0x80, 0xa0)).hex()} + got = mg.prepared_from(work) + if got != want: + print(f'FAIL: tools/make_game.py prepared_from reads {got}, the game wrote {want}') + return 1 + print('prepared list file: tools/make_game.py reads what the game writes') + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tools/make_game.py b/tools/make_game.py index 8b121eeb..f5ec3d77 100644 --- a/tools/make_game.py +++ b/tools/make_game.py @@ -1830,6 +1830,47 @@ def shader_carry(out): return (out / 'shader-prepared.bin').is_file() and bool(cache_identities(out)) +# The head of shader-prepared.bin (runtime/prp_file.h): magic, version, record size, and from version 2 on what the +# list was prepared from: sha256 of seed-programs.bin, sha256 of seed-answers.bin, the translator identity. +PREPARED_HEAD = struct.Struct('<8sII') +SEED_FILES = ('seed-programs.bin', 'seed-answers.bin') + + +def prepared_from(out): + """What the game folder's prepared shader list says it was prepared from: {'programs', 'answers', 'translator'} + as hex digests; {} for a list that does not say (written before the list recorded it); None without a list.""" + try: + with open(out / 'shader-prepared.bin', 'rb') as f: + data = f.read(PREPARED_HEAD.size + 96) + except OSError: + return None + if len(data) < PREPARED_HEAD.size: + return {} + magic, version, _rec = PREPARED_HEAD.unpack_from(data) + if magic != b'NX2NSPRP' or version != 2 or len(data) < PREPARED_HEAD.size + 96: + return {} + body = data[PREPARED_HEAD.size:] + return {'programs': body[:32].hex(), 'answers': body[32:64].hex(), 'translator': body[64:96].hex()} + + +def seed_changed(out): + """Why the shaders prepared before cannot stay in use, given the seed just written into the game folder + (SEED_FILES): a sentence, or '' when the list was prepared from this very seed. A list that does not say what it + was prepared from counts as changed: nothing tells whether it holds what this seed names. + The translator is not compared: input ids do not depend on it and the game carries the translations of the one + before over (shader_carry).""" + was = prepared_from(out) + if not was: + return 'the shaders prepared before do not say which shader seed they were prepared from' + try: + now = {'programs': sha256_file(out / SEED_FILES[0]), 'answers': sha256_file(out / SEED_FILES[1])} + except OSError: + return '' # no seed to compare with: as before the list recorded it + if (was['programs'], was['answers']) != (now['programs'], now['answers']): + return 'the shader seed changed since the shaders were prepared' + return '' + + def shader_run(cmd, cwd=None, build_only=False): """Runs a shader preparation command and returns its exit code. Its output is read as it comes: the counts nx2navi.exe prints become progress events, the other lines go on to the log, and while nothing moves an event @@ -1921,7 +1962,8 @@ def step_shaders(out, update=False): (shader_run reports its progress). update: after a kit update the shaders prepared before stay in use and the game translates them again in the background (shader_carry), so the update does not wait the minutes a full preparation takes; the driver's cache - gets their stage libraries again (warm_driver_cache).""" + gets their stage libraries again (warm_driver_cache). Only a list prepared from the seed this kit writes stays + (seed_changed): with another seed, or a list that does not say, everything is prepared again as at the install.""" # The game translates with the copy beside it. The compile step refreshes that copy, but a kit that changes only # the translator runs this step again alone (step_key), which would then prepare with the old one. Only a kit that # has one: the compile step, which always runs first, stops a build without it. @@ -1931,7 +1973,22 @@ def step_shaders(out, update=False): for name in (XLATE_NAME, 'nx2navi_xlate.id'): copy_if_changed(d / name, out / name) break - if update and shader_carry(out): + cmd = [sys.executable, '-u', str(KIT / 'tools' / 'prepare_shaders.py'), str(out), '--romfs', str(out / 'romfs'), + '--exefs', str(out / 'exefs'), '--no-list', '--exe', EXE_NAME] + why = '' + carry = update and shader_carry(out) + if carry and prepared_from(out) is not None: + # The seed this kit makes from the game files, written without preparing anything (seconds): the list + # prepared before stays in use only when it was prepared from this seed. Another seed names other shader + # variants (a newer pipeline list, a change to which variants are prepared), and those the old list lacks + # would be translated when play first draws them. A seed that cannot be written leaves things as they were. + if shader_run(cmd + ['--no-run']) == 0: + why = seed_changed(out) + if why: + say(f' {why}: preparing them again') + if carry and not why: + for seed in SEED_FILES: + (out / seed).unlink(missing_ok=True) say(' the shaders prepared before this update stay in use; the game translates them again in the background') if loose_shaders(out) >= PACK_LOOSE_MIN: # minutes without a count (4 on a player's 328,000 files): the row says what runs meanwhile @@ -1939,14 +1996,12 @@ def step_shaders(out, update=False): pack_shader_cache(out) warm_driver_cache(out) return - cmd = [sys.executable, '-u', str(KIT / 'tools' / 'prepare_shaders.py'), str(out), '--romfs', str(out / 'romfs'), - '--exefs', str(out / 'exefs'), '--no-list', '--exe', EXE_NAME] rc = shader_run(cmd) if rc: fail('preparing the shaders failed (the lines above say why); the game still runs, compiling shaders as it ' 'meets them. Run the build again to retry this step') # translated now: the shader cache holds them. seed-pipeline-keys.bin stays: --warm-shaders builds from it again - for seed in ('seed-programs.bin', 'seed-answers.bin'): + for seed in SEED_FILES: (out / seed).unlink(missing_ok=True) pack_shader_cache(out) diff --git a/tools/package_build.py b/tools/package_build.py index 915b9330..6c6b5ab6 100644 --- a/tools/package_build.py +++ b/tools/package_build.py @@ -25,8 +25,8 @@ EXE = '.exe' if os.name == 'nt' else '' DLLS = ('SDL3.dll', 'libopus-0.dll', 'libiconv-2.dll') if os.name == 'nt' else () # Prepared state next to the exe: translator answers, programs, the prepared list, pipeline cache and its keys, and the # variant lists `nx2navi.exe --prepare-shaders` reads (the driver keys its library cache by exe path, so each copy runs it). -STATE = ('romfs-index.bin', 'pipeline-cache.bin', 'pipeline-keys.bin', 'shader-answers.bin', 'shader-programs.bin', - 'shader-prepared.bin', 'seed-answers.bin', 'seed-programs.bin') +STATE = ('romfs-index.bin', 'pipeline-cache.bin', 'pipeline-cache-play.bin', 'pipeline-keys.bin', 'shader-answers.bin', + 'shader-programs.bin', 'shader-prepared.bin', 'seed-answers.bin', 'seed-programs.bin') def copy(src, dst): dst.parent.mkdir(parents=True, exist_ok=True)