Skip to content

vk: cache Maxwell stage translation metadata (.smeta) beside warm SPIR-V - #482

Merged
mamcjr merged 2 commits into
mamcjr:mainfrom
Propinbo:feat/shader-meta-cache
Nov 18, 2026
Merged

mamcjr merged 2 commits into
mamcjr:mainfrom
Propinbo:feat/shader-meta-cache

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Even when cache/spirv/<hash>.spv was warm on disk, restarting the game still routed every newly encountered Prog through the single-threaded translator thread (maxwell_translate_bounded + translate_lock + GLSL string emission) solely to reconstruct ShaderInfo, vs_uses_vid, fs_gather, and compute the .spv hash inside vk_glsl_to_spirv():

  1. Binary .smeta stage metadata cache (runtime/nvn/vulkan/shader_meta_cache.h, LARECOMP_META_CACHE=0: off):
    • Stores compact MWSMET01 .smeta records (MwMetaHdr 160 B + MwMetaTex[] 16 B, atomic temp-file write + _WIN32 overwrite fallback + payload checksum) in cache/spirv/ beside .spv.
    • mw_stage_key() computes a 128-bit key from SPH (0x50 B), Maxwell instructions up to bra self, BRX jump-table constants (when MW_TF_BRX_WINDOW && glsl && cmax < 0x10000), HEAP_BASE/HEAP_MAX, canonical ShaderKey (including gmem_linear_lookup), and all 23 translate.c / shaders.c configuration bits (MW_TF_*).
    • Disabled automatically whenever debug/override hooks (LARECOMP_VK_NO_SPIRV_CACHE, LARECOMP_SAVE_MSL, LARECOMP_DUMP_FS, LARECOMP_SHADER_SUBST, LARECOMP_SHADER_PATCH, maxwell_dbg_code, maxwell_dbg_vs) are active.
  2. Parallel warm Prog reconstruction (runtime/nvn/vulkan/shaders.{h,c}, runtime/nvn/vulkan/exec.c):
    • Exposes vk_shader_cache_ensure_dirs(), vk_spirv_load_cached(), and vk_glsl_to_spirv_ex().
    • prog_fast_or_translate() runs on jobs workers: on VS+FS .smeta + .spv hit it bypasses translate_lock and shaderc completely via compile_program_from_spirv(); on miss it falls back to translate_program(), which stores .smeta after .spv succeeds.
    • Includes standalone worker safety (pthread_once pools, jobs_drain() / jobs_abort in vk_exec_reset() before vkFreeMemory(gmem_mem) unmaps HEAP_BASE on Turnip map_placed) and cached gmem_miss_log_on() (LARECOMP_NO_GMISS read once in vk_exec_init() instead of per draw).
  3. Unit & corruption tests (tools/test_shader_meta_cache.c):
    • Covers unaligned blobs, bra self trailing padding, BRX positive/negative controls, all 23 MW_TF_* bits, gmem_linear_lookup / glsl / ShaderKey fields, ntex = 0..64 round-trips, overwrite, and rejection of bad magic/version/spv_hash=0/trailing bytes/truncation/bit flips.

Verification

Standalone Branch Checks (this commit)

  • Host WASM/Node (NDK 30.0.16248370): test_vulkan_compat.py (16), test_vulkan_startup.py (33), test_shader_cache.py (17) — all passed.
  • Host Linux (cc -Wall -Wextra -Werror -O2 -pthread) & Android ARM64 Bionic (SM-X730): tools/test_shader_meta_cache.c — passed.
  • Android :app:assembleDebug (JDK 17): BUILD SUCCESSFUL with 0 warnings in modified/added files.

Combined Physical-GPU Measurements (prior unified 3-feature prototype before splitting)

  • Standalone benchmarks for this commit alone have not yet been run separately; in combined testing on hateno-night (cap30, sgsr1 1080p, warm .spv, 0 Vulkan errors):
    • SM-X730 (Mali-G925 MC12 r49p1): Adding warm .smeta reduced world-entry compile wait further from 1296 ms (cold .smeta) -> 994 ms (4968 ms baseline, 5.0x total) with 906 / 913 .smeta hits (99.2%).
    • Adreno 830 (Turnip_Gen8_V38_Patched, driver-id 18): 906 / 908 (99.8%) .smeta hits across 3-pass A/B (-82.9% exit compile wait vs baseline); post-rebase smoke verified [vk] reboot clean drain before vkFreeMemory(gmem_mem) and 452 / 452 (100%) .smeta hits on Repeat 2.

Companion Focused PRs

@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks, but one blocking issue: the .smeta key (shader_meta_cache.h ~144) has no translator or build version. After an app update that changes translate.c, the key still matches and points at the old .spv, so players would keep running the old SPIR-V forever (today the GLSL-source hash invalidates it by itself). Please mix a build id into the key (versionCode, the git hash, or a hash of the translator sources). Also, ShaderKey is hashed field by field (~157): a field added later would be silently left out; hashing the whole struct (with zeroed padding) avoids that. And please test the cold cache: a new program now goes through a worker first and may wait behind pipeline builds before translation starts. Then rebase on #481.

@Propinbo
Propinbo force-pushed the feat/shader-meta-cache branch from ce13c72 to b4e80e7 Compare November 17, 2026 22:57
@Propinbo

Copy link
Copy Markdown
Contributor Author

Addressed all three points and rebased onto #481 (e8fac0f → b4e80e7, so GitHub's diff against main includes #481 + #482 until #481 lands):

  1. Translator / build ID in .smeta key (benchmark_commit.cmake, shader_meta_cache.h, exec.c):
    • Bumped MW_META_VERSION to 3 and mixed BENCHMARK_COMMIT ":" MW_TRANSLATOR_ID (SHA-256 of translate.c, shader.h, shader_meta_cache.h, shader_cache.h, and shaders.c generated by CMake) into mw_stage_key() so any runtime/translator update invalidates stale .smeta entries automatically.
  2. Full ShaderKey struct hashing (shader_meta_cache.h):
    • Switched mw_stage_key() to hash the entire 88-byte ShaderKey struct after zeroing pad_ (_Static_assert(sizeof(bool) == 1 && sizeof(ShaderKey) == 88 && ...)), ensuring any future ShaderKey field is covered automatically.
    • Cached getenv() option reads safely via pthread_once while keeping dynamic g_settings.shader_int32 live and recomputing keys with the exact translation snapshot under translate_lock.
    • Verified with tools/test_shader_meta_cache.c (PASS on ARM64 Bionic SM-X730 and host ASan+UBSan).
  3. Cold-cache scheduling (mw_meta_exists() in shader_meta_cache.h / exec.c) & Mali-G925 A/B (SM-X730, plateau, URGENT=1):
    • Added mw_meta_exists(): when no .smeta file exists on disk, get_program() dispatches directly to run_translate_ex(translate_program, ...) instead of pushing cold misses onto jobs_head behind pending pipeline builds.
Condition Mode Shader Wait (exit) Max Wait [vk] shader-meta load_s cam_s low1 p99
True cold (0 .spv, 0 .smeta, 0 pipe) META_CACHE=0 4105.80 ms 80.81 ms off (0 .smeta) 20.3 s 7.6 s 29.49 33.893 ms
True cold (0 .spv, 0 .smeta, 0 pipe) META_CACHE=1 4118.01 ms (+0.3%) 81.70 ms 12 hits / 433 misses (611 saved) 20.1 s 7.5 s 27.80* 33.969 ms
Warm .spv + .smeta, cold pipe META_CACHE=0 2615.42 ms 76.34 ms off (611 .smeta) 19.7 s 6.8 s 29.45 33.882 ms
Warm .spv + .smeta, cold pipe META_CACHE=1 2402.89 ms (-8.1%) 77.19 ms 442 hits / 0 misses (100%) 19.5 s 6.6 s 29.52 33.806 ms

*Note: Both cold runs had identical 22 / 12 frames over 50 / 100 ms across load/settle; in META_CACHE=1 a single 43.62 ms frame occurred during 2.98 MB BC texture decode right at window start. On the warm .smeta run, 442/442 programs bypassed maxwell_translate + shaderc (0 misses), leaving only 1,121 cold graphics pipeline builds.

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 17, 2026
@mamcjr

mamcjr commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks, the translator id in the key and the whole-struct hash fix it. I'll merge it after #481, after checking here that an update over an old cache misses as it should.

@mamcjr

mamcjr commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks! On the POCO, over main's warm cache: the first load missed (349 misses, metadata built), from the reboot on 357 hits / 0 misses, and a second run all hits. Merging.

@mamcjr
mamcjr merged commit 6840da4 into mamcjr:main Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants