Skip to content

vulkan: resolve warm shader and pipeline cache hits inline on Mali r49+ - #529

Open
Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:feat/vk-warm-cache-fastpath
Open

Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:feat/vk-warm-cache-fastpath

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Even when cache/spirv (*.smeta + *.spv) and cache/vk_pipelines.bin are warm from an earlier session, every newly encountered (program, state) key on boot or fast-travel still goes through two asynchronous hops before its first draw can execute:

  1. get_program() unconditionally dispatches warm meta-cache loads to background threads, so p->state == 0 skips the draw on frame N.
  2. Once the program finishes, schedule_pipeline_build() (when keep == false, i.e., normal 3D scene draws) unconditionally dispatches vkCreateGraphicsPipelines(..., g_vk.pcache, ...) to a worker thread, skipping draws behind busy worker queues during fast-travel dissolve and world entry.

Solution

Resolve warm disk-cache / driver-cache hits synchronously on the render thread within a bounded, FPS-scaled per-frame budget before falling back to async workers:

  • runtime/nvn/vulkan/warm_cache_fastpath.h + device.c + vk.h:
    • Share vk_pipe_cache_ctrl_select() between device.c and unit tests to enable VkPhysicalDeviceVulkan13Features.pipelineCreationCacheControl by default strictly on verified proprietary ARM Mali r49+ drivers (g_vk.api >= VK_API_VERSION_1_3 && h13.pipelineCreationCacheControl && g_vk.pcache).
    • Scale the default per-frame inline budget with target FPS via vk_warm_inline_default_us() (VK_WARM_INLINE_DEFAULT_30_US = 4000 us at <= 30 fps, VK_WARM_INLINE_DEFAULT_45_US = 3000 us at 31..45 fps, VK_WARM_INLINE_DEFAULT_60_US = 2000 us at > 45 / uncapped fps; LARECOMP_INLINE_WARM_US=N overrides and clamps at VK_WARM_INLINE_MAX_US = 20000 us, <= 0 disables inline probing).
    • Refreshed at most once per frame in exec.c (warm_inline_budget_refresh(frame) / warm_inline_target_fps()), with warm_budget_fps_frame reset in vk_exec_reset().
  • runtime/nvn/vulkan/shader_meta_cache.h + exec.c:
    • Pre-check vertex .smeta out_mask via mw_meta_peek_out_mask() and verify fragment meta presence with mw_meta_exists("cache/spirv", fs_h1) before entering the inline SPIR-V/meta load path, eliminating wasted inline loads on cold fragment variants; reuse the precomputed fs_key inside prog_load_from_meta_cache().
    • Share prog_load_from_meta_cache() between prog_fast_or_translate() (worker fallback) and bounded inline loading inside get_program().
    • Refactor build_pipeline() around build_pipeline_ex(Pipe *pp, bool cache_only): when cache_only is true, pass VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT. On a cache miss (r != VK_SUCCESS, catching VK_PIPELINE_COMPILE_REQUIRED as well as errors), destroy any stray handle and return false cleanly without mutating pp->state, retry_s, pipe_oom, LARECOMP_PIPE_FAIL_OOM, live_gpipes, stat_compiles, MG_GPIPE, or benchmark build counters, then queue the normal background worker.
    • Record BP_BUILDS, BP_BUILD_NS, BP_BUILD_WAIT_NS, and vk_mgrow_add(MG_GPIPE, mg0) accurately on inline Prog and VkPipeline hits (sync_shaders(), urgent_shaders_on(), !g_vk.exec_stats, and jobs_abort / vk_exec_reset() respected; waits wrapped in WD_R_BUILD_WAIT).
    • Thread-safety note: converted the LARECOMP_NO_DYN_UBO env-lookup cache in compile_program_from_spirv() from a plain static int off = -1 to static _Atomic int off = -1 (atomic_load_explicit / atomic_store_explicit, memory_order_relaxed) because compile_program_from_spirv() can now run inline on the render thread concurrently with background translation workers.
  • tools/test_warm_cache_fastpath.c + tools/test_shader_meta_cache.c + tools/test_rt_load_upload.c + tools/test_android_runtime.ps1:
    • Host + Bionic unit and mocked-exec.c tests covering vk_pipe_cache_ctrl_select, FPS budget tiers, env overrides/clamping/whitespace, mw_meta_peek_out_mask bounds/short-read/corrupt-header handling, VK_PIPELINE_CREATE_FAIL_ON_PIPELINE_COMPILE_REQUIRED_BIT miss cleanup, and BP_*/MG_GPIPE accounting.

Environment & Hardware Evidence

  • PR branch commit (v0.4.12-beta rebase + review fixes): 87ba2a4f603b81d82bb988a62ac186ebc5e08632 atop upstream/main (79ba1909f716d7761046a767651c62dbfa917ef1)
  • Combined device test tree (#534 c1a29d93 + #529 87ba2a4f, classic render passes default active): commit 2d08bfa459fa, APK SHA-256 89a7574a6cebde5713ef3eea70cd16eb69a5f3a4119b9d061e617a6c0b764288
  • Device: Samsung Galaxy Tab S11 (SM-X730), SoC MT6991 (Dimensity 9400+), Android 16 (API 36)
  • GPU / Driver: Mali-G925-Immortalis MC12 (driver-id 9), v1.r49p1-03bet0.f8a98b506b89b21c80a508d70abfcb3f, Vulkan 1.3.278
  • Game: BOTW 1.9.0, 100% internal resolution (res_mode=10, upscaler=3), inplace_reads=1, shadows=1

1. First-Teleport A/B Atop #534 Classic Render Passes (30 fps -> 4 ms Tier, LARECOMP_PIPE_PREWARM=0, Identical Warm Cache Snapshot 5093 .spv / 14559 .smeta / 67078586 B vk_pipelines.bin)

Run Mode / Flags Total Skipped Teleport Window Skipped pace_window_period_ms (p50 / p95 / p99 / max) vk_window_max_ms (p50 / p95 / p99 / max) render_p50 vk_window_fps (min / med)
TP_OFF LARECOMP_VK_NO_CACHE_CTRL=1 15059 1199 33.37 / 36.88 / 38.07 / 95.4 ms 39.5 / 186.4 / 219.68 / 228.0 ms 19.29 ms 24.1 / 29.6
TP_ON_default Default (no env overrides, 4 ms tier) 5365 (-64.4%) 511 (-57.4%) 33.52 / 37.70 / 38.56 / 89.7 ms 44.5 / 192.05 / 224.81 / 233.0 ms 17.40 ms 24.6 / 29.15
  • Startup capability verification (TP_ON_default): [vk] no dynamic rendering (Mali r49+ default): classic render passes (1..8), [vk-cap] pipeline cache control: on (driver default), [vk] targets a draw writes, sampled by it: in place (settings).
  • Visual check of 30 fps teleport dissolve onset captures (f_022–f_042): Link and the blue Sheikah dissolve tendrils remain completely intact across dissolve onset with zero missing limbs or empty frames.

2. 3-Scene Benchmark Atop #534 (B30_ON_default, frame_rate=30, 4 ms Tier, No Env Overrides, 2 Clean Reboots + Exit)

  • Inline fastpath telemetry (bench30_on_default):
    [vk] warm-cache fastpath (reboot): prog 143 hits, pipe 340 hits / 5 misses
    [vk] warm-cache fastpath (reboot): prog 105 hits, pipe 213 hits / 11 misses
    [vk] warm-cache fastpath (exit): prog 154 hits, pipe 303 hits / 3 misses
    
    Total: 402 inline Prog hits and 856 / 875 (97.8%) inline VkPipeline cache hits (0 kept across 274 ms / 237 ms teardowns).
  • Scene metrics (res_mode=10, upscaler=3, inplace_reads=1):
    • plateau (ok): 29.86 fps, 1% low 24.41, frame p50 / p95 / p99 = 33.335 / 33.817 / 38.520 ms, gpu p50 29.113 ms, render p50 20.674 ms, skipped_draws = 0
    • zora-plaza (ok): 30.00 fps, 1% low 29.44, frame p50 / p95 / p99 = 33.330 / 33.802 / 33.913 ms, gpu p50 30.520 ms, render p50 20.020 ms, skipped_draws = 0
    • kakariko-day (ok): 19.69 fps, 1% low 12.46, frame p50 / p95 / p99 = 56.473 / 70.992 / 76.340 ms, gpu p50 57.972 ms, render p50 42.885 ms, skipped_draws = 0, builds = 2 (build_ns = 237847, build_wait_ns = 238384, long_frame_causes.pipeline_wait = 1)
Earlier standalone v0.4.10-beta (`edeb35eb`, dynamic rendering default, `75%` res) A/B visual comparison on empty-Link teleport repro
Before (LARECOMP_VK_CACHE_CTRL=0, f_076) After (edeb35eb Default On, f_074)
Before: Link vanishes on warm teleport onset After: Link stays intact on warm teleport onset

GPU Coverage, Zero-Overhead Guarantee & Rollback

GPU / Driver Default Behavior Status
Mali-G925 (r49p1, driver-id 9) Enabled (driver default) Tested (SM-X730)
Proprietary Mali r49+ Enabled (driver default) Enabled conservatively via vk_arm_driver_major >= 49
Adreno Turnip (driver-id 18) Off by default Untested on device (LARECOMP_VK_CACHE_CTRL=1 to opt in)
Qualcomm stock, Mali < r49, PanVK, Xclipse, others Off by default Untested; zero f13 feature change
  • Zero overhead when off / on untested GPUs: g_vk.pipe_cache_ctrl stays false and f13.pipelineCreationCacheControl = VK_FALSE; get_program() and schedule_pipeline_build() take a single boolean check on cold key insertion with no extra Vulkan calls or clock reads.
  • Rollback env vars: LARECOMP_VK_CACHE_CTRL=0 or LARECOMP_VK_NO_CACHE_CTRL=1 disables completely; LARECOMP_INLINE_WARM_US=N sets a custom per-frame budget (0 disables inline probing).

Checks Executed (87ba2a4f)

  • tools/test_android_runtime.ps1 on SM-X730: 9/9 native Bionic suites passed (including test_warm_cache_fastpath and test_rt_load_upload), plus standalone Bionic test_shader_meta_cache.
  • Host Wasm/Python suites (NDK 30.0.16248370): test_shader_cache.py (17), test_vulkan_compat.py (16), test_vulkan_startup.py (33), test_gpu_timing_trace.py (8), test_soft_linear.py (23 + 3 + 203).
  • Host C (-Wall -Wextra -Werror) + NDK 30 Clang (-Werror, Android + Desktop): device.c, exec.c, test_warm_cache_fastpath.c, test_shader_meta_cache.c, test_rt_load_upload.c.
  • workspace_check.py pr .mali/worktrees/warm-cache-pipe-fastpath --base upstream/main: PASS (commits=1, files=9, mergeable=YES).

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 21, 2026
@Propinbo
Propinbo force-pushed the feat/vk-warm-cache-fastpath branch from edeb35e to ea24f43 Compare November 22, 2026 11:39
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto upstream/main (75bfc64c, commit ea24f43c) to resolve the merge conflict with #520 in build_pipeline_ex():

  • Preserved #520's OOM retry_s / pipe_oom handling and guarded LARECOMP_PIPE_FAIL_OOM under if (!cache_only) so warm-cache FAIL_ON_PIPELINE_COMPILE_REQUIRED probes cannot trigger premature failure state or double-schedule builds.

Re-verified on exact commit ea24f43c:

  • Host checks: test_vulkan_compat.py (16), test_vulkan_startup.py (33), test_soft_linear.py (23 + 3 + 203), test_shader_cache.py (17), test_gpu_timing_trace.py (8), test_fullscreen_narrow.py --wsl (PASS).
  • NDK r30 Clang syntax/warning checks (-Wall -Wextra) + test_warm_cache_fastpath.c (-Werror): 0 new warnings.
  • SM-X730 Bionic runtime suite (test_android_runtime.ps1): 9/9 PASS (including test_warm_cache_fastpath).
  • Pre-push 2-agent independent review & workspace_check.py pr: PASS (0 CRITICAL / 0 MAJOR, 1 commit, 6 files, mergeable=YES).

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 22, 2026

@mamcjr mamcjr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Off Mali r49+ nothing changes (g_vk.pipe_cache_ctrl stays false), and the refactors behave like main as far as I can see.

Before merging:

  1. The inline budget is large, and one call can overshoot it. vk_warm_budget_allow only checks spent < limit before each call, and one meta load (file I/O, two vkCreateShaderModule, layouts) or one pipeline creation can run well past it. With the render thread at 18-20 ms on the Tab S11 at 30 fps, 10 ms on top leaves almost no headroom, and at 60 fps the budget alone is most of the frame. Could the default be 2-4 ms (your first prototype used 2), or scale with the frame target? And could you post frame-time p95/p99 inside the teleport window, not only skipped draws and 1% lows?
  2. A partial hit wastes inline work. The inline path is entered when the vertex .smeta exists, even if the fragment one doesn't. A new fragment variant of a cached vertex shader then costs one inline load before missing, and the work is redone by translate_program. Check that the fragment file exists too, or keep the loaded vi.
  3. #534 changes what Mali r49+ users will run (classic render passes), so please re-measure this on top of it.

Minor: the LARECOMP_NO_DYN_UBO static became atomic, which is right now that compile_program_from_spirv also runs on the render thread; worth a line in the PR description.

@Propinbo
Propinbo force-pushed the feat/vk-warm-cache-fastpath branch from ea24f43 to 87ba2a4 Compare November 24, 2026 15:51
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto v0.4.12-beta (79ba1909, commit 87ba2a4f) and addressed all four review items:

  1. FPS-scaled inline budget (warm_cache_fastpath.h / exec.c) + teleport p95/p99:

    • Replaced the flat 10 ms default with FPS-scaled tiers via vk_warm_inline_default_us(): 4000 us (<= 30 fps), 3000 us (31..45 fps), and 2000 us (> 45 / uncapped fps), refreshed at most once per frame in warm_inline_budget_refresh(frame) (reset in vk_exec_reset()), while keeping LARECOMP_INLINE_WARM_US (clamped at 20000 us, <= 0 disables) as an explicit override.
    • Added benchmark frame/build counter attribution (BP_BUILDS, BP_BUILD_NS, BP_BUILD_WAIT_NS, and MG_GPIPE) on inline Prog/VkPipeline hits so long_frame_causes.pipeline_wait stays accurate whenever an inline hit spends render-thread time (miss probes still destroy any stray handle and leave state/counters untouched).
  2. Pre-checking both stages before entering inline meta loading (shader_meta_cache.h / exec.c):

    • Added header-only mw_meta_peek_out_mask() to read vertex out_mask first, compute the exact fragment ShaderKey hash (fs_key / fs_h1 / fs_h2), and require mw_meta_exists("cache/spirv", fs_h1) before calling prog_load_from_meta_cache(). Cold fragment variants no longer pay an inline vertex .smeta / .spv load only to miss and fall back to translation; hit paths reuse the already-computed fs_key.
  3. Re-measured atop #534 (classic render passes default on Mali r49+):

    • Built a merged test image combining #534 (c1a29d93) + #529 (87ba2a4f, test tree 2d08bfa459fa, APK 89a7574a...) on SM-X730 (Mali-G925, v1.r49p1, Android 16, res_mode=10, upscaler=3, inplace_reads=1), confirming [vk] no dynamic rendering (Mali r49+ default): classic render passes (1..8) and [vk-cap] pipeline cache control: on (driver default) together:
    • First-teleport A/B (30 fps -> 4 ms tier, LARECOMP_PIPE_PREWARM=0, identical warm cache snapshot 5093 .spv / 14559 .smeta / 67078586 B vk_pipelines.bin):
      • TP_OFF (LARECOMP_VK_NO_CACHE_CTRL=1): total_skipped = 15059, window_skipped = 1199, pace_window_period_ms p50 / p95 / p99 / max = 33.37 / 36.88 / 38.07 / 95.4 ms (render_p50 = 19.29 ms), vk_window_max_ms p50 / p95 / p99 / max = 39.5 / 186.4 / 219.68 / 228.0 ms.
      • TP_ON_default (driver default, 4 ms tier): total_skipped = 5365 (-64.4%), window_skipped = 511 (-57.4%), pace_window_period_ms p50 / p95 / p99 / max = 33.52 / 37.70 / 38.56 / 89.7 ms (render_p50 = 17.40 ms), vk_window_max_ms p50 / p95 / p99 / max = 44.5 / 192.05 / 224.81 / 233.0 ms; dissolve onset screenshots (f_022–f_042) show clean Link/tendril rendering with zero broken geometry.
    • 3-scene B30_ON_default (30 fps -> 4 ms tier, 2 clean reboots + exit):
      • Fastpath hits: 143 + 105 + 154 = 402 inline Prog hits and 340/5 + 213/11 + 303/3 = 856 / 875 (97.8%) inline VkPipeline hits.
      • plateau (ok): 29.86 fps (1% low 24.41), frame p50 / p95 / p99 = 33.335 / 33.817 / 38.520 ms, gpu p50 29.113 ms, skipped = 0.
      • zora-plaza (ok): 30.00 fps (1% low 29.44), frame p50 / p95 / p99 = 33.330 / 33.802 / 33.913 ms, gpu p50 30.520 ms, skipped = 0.
      • kakariko-day (ok): 19.69 fps (1% low 12.46), frame p50 / p95 / p99 = 56.473 / 70.992 / 76.340 ms, gpu p50 57.972 ms, skipped = 0, builds = 2 (build_ns = 237847, build_wait_ns = 238384, pipeline_wait = 1).
  4. Atomic LARECOMP_NO_DYN_UBO note & expanded test coverage (87ba2a4f):

    • Documented the _Atomic int off conversion in compile_program_from_spirv() in the PR description.
    • Shared vk_pipe_cache_ctrl_select() with device.c and added mocked exec.c integration tests in tools/test_rt_load_upload.c plus meta-peek/budget boundary tests in tools/test_shader_meta_cache.c and tools/test_warm_cache_fastpath.c.
    • Verified on 87ba2a4f: test_android_runtime.ps1 9/9 PASS on SM-X730 + standalone Bionic test_shader_meta_cache PASS, host Wasm/Python suites (test_shader_cache.py 17, test_vulkan_compat.py 16, test_vulkan_startup.py 33, test_gpu_timing_trace.py 8, test_soft_linear.py 23+3+203), -Werror host + NDK Clang builds, 2/2 independent pre-PR code reviews (READY, 0 CRITICAL/MAJOR), and workspace_check.py pr PASS.

@mamcjr

mamcjr commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks for the update; the scaled budget and the fragment check look good. One thing before merging, now that #534 is in main: #534 added an if (!crp) { ...; return; } in build_pipeline, and this PR turns it into bool build_pipeline_ex, so on top of main exec.c stops compiling (-Wreturn-mismatch), and tools/test_rt_load_upload.c the same way. Git doesn't flag it. Could you rebase on main and make that path if (cache_only) return false; first, then return false;? Otherwise a cache-only probe would mark the build failed and arm the out-of-memory retry. Optional: framepace_target() already gives the frame target the budget needs.

@Propinbo
Propinbo force-pushed the feat/vk-warm-cache-fastpath branch from 87ba2a4 to e5be41a Compare November 26, 2026 15:12
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (208746bb / v0.5.0-beta, commit e5be41a) and addressed both points:

  1. !g_vk.dyn_render / !crp handling in build_pipeline_ex() (runtime/nvn/vulkan/exec.c):
    • Added if (cache_only) return false; before setting pp->retry_s, pipe_oom, and signal_build_done(&pp->state, 2); return false; so exec.c compiles cleanly alongside #534 and a cache-only probe never marks the pipeline failed or arms OOM retry.
  2. framepace_target() reuse (runtime/nvn/vulkan/exec.c):
    • Replaced warm_inline_target_fps() with framepace_on() ? framepace_target() : 30 inside warm_inline_budget_refresh().
  3. Expanded regression coverage (tools/test_rt_load_upload.c):
    • Added mocked assertions covering !g_vk.dyn_render with null vk_rp_compat() (cache_only clean vs normal build state=2 + retry_s + pipe_oom), framepace_target() budget tiers (30 -> 4 ms, 45 -> 3 ms, 60/0 -> 2 ms, !framepace_on() -> 4 ms), and pipe_cache_ctrl=false / exec_stats=true inline bypass.

Verified after the final edit (e5be41a):

  • NDK Clang Android syntax checks (exec.c, device.c) & workspace_check.py pr: PASS.
  • Host Python suites (test_vulkan_compat.py 16, test_vulkan_startup.py 33, test_shader_cache.py 17): PASS.
  • SM-X730 Bionic runs: test_warm_cache_fastpath PASS, test_shader_meta_cache PASS, test_rt_load_upload warm-cache fastpath integration (Cases 1–7) + mock sync validator (0 hazards) PASS (note: upstream/main itself currently fails the unrelated Mali (0x13B5) manual On in-place feedback, Automatic=copy... check after 9f46f29).
  • 2/2 independent pre-PR code reviews (READY, 0 CRITICAL / 0 MAJOR).

@mamcjr

mamcjr commented Nov 27, 2026

Copy link
Copy Markdown
Owner

All the points from the last review are fixed: budget by frame target, the fragment file checked first, measured on top of #534, return false before the out-of-memory retry, framepace_target().

One small change: the inline paths call bench_add(BP_BUILD_WAIT_NS) but not account_build_wait() (exec.c:1714), so their render-thread time is missing from the shader-build (exit): wait … ms line everyone compares. Could you add account_build_wait(dt) on both inline paths?

Also, it now conflicts with main in tools/test_android_runtime.ps1 (#545's test_motion was added there today): a rebase keeping both lists is enough. Note for the numbers: the test_rt_load_upload failure you saw was main's own and is fixed in 486844d.

The teleport window's p95 went slightly up (186 to 192 ms) while skipped draws fell 64%: that's the expected trade, and I'll say so in the release notes. With those two changes I'll merge it.

@Propinbo
Propinbo force-pushed the feat/vk-warm-cache-fastpath branch from e5be41a to 63c9e1a Compare November 27, 2026 13:44
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased onto v0.5.1-beta (da30d45, commit 63c9e1a) and addressed both items:

  1. Inline build-wait accounting (runtime/nvn/vulkan/exec.c): Replaced direct bench_add(BP_BUILD_WAIT_NS, dt) with account_build_wait(dt) in both get_program() and schedule_pipeline_build() so shader-build (exit): wait ... ms includes render-thread inline cache loads/creates while keeping BP_BUILD_WAIT_NS single-counted.
  2. Test rebase & coverage (tools/test_android_runtime.ps1, tools/test_rt_load_upload.c): Preserved #545's test_motion alongside test_warm_cache_fastpath, updated mocked Cases 2–3 to assert account_build_wait counters and single-counted BP_BUILD_WAIT_NS, and added Case 8 covering get_program() inline cache hit and FS-missing probe miss.

Verified on 63c9e1a:

  • SM-X730: tools/test_android_runtime.ps1 12/12 PASS (including test_rt_load_upload, test_motion, test_warm_cache_fastpath) + standalone Bionic test_shader_meta_cache PASS.
  • Host: gcc test_warm_cache_fastpath & test_shader_meta_cache PASS, Android NDK r30 Clang syntax check PASS, workspace_check.py pr PASS, and 2-agent independent pre-PR review READY.

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 27, 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