Skip to content

nvn/vulkan: add opt-in graphics pipeline prewarming during save loads (#294) - #475

Merged
mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/pipeline-prewarm-294
Nov 17, 2026
Merged

mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/pipeline-prewarm-294

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Adds opt-in loading-screen graphics pipeline prewarming (#294), disabled by default (Video > Pipeline prewarming (experimental) in botwrecompandroid.ini, or LARECOMP_PIPE_PREWARM=1/0).

While playing in the 3D world, unique graphics pipeline keys belonging to already-compiled shader programs (Prog.pw_id) are deduplicated and stored in a bounded binary profile (cache/pipeline-prewarm.bin, up to 256 entries by default or 1..512 via LARECOMP_PIPE_PREWARM_MAX). On subsequent save loads or fast-travel/reload transitions, once a matching Prog reaches state == 1 in prog_cache while shader workers and the translator thread are idle, at most one prewarm Pipe at a time is queued into pipe_cache without delaying loading-screen shader work or eroding pipe_keep() headroom on Mali/low-RAM devices.


Changes (17 files on main 26da3fa, commit 6454ccc)

  • runtime/settings.h, runtime/settings.c, runtime/ui/menu.mm, runtime/ui/i18n_*.inc (11 locales):
    • Added bool pipe_prewarm (default false), pipe_prewarm = 0/1 INI persistence, settings_reset_defaults(), Video > Pipeline prewarming (experimental) toggle (no issue number in help text), and translations in all 11 i18n_*.inc files (de, es, fr, it, ja, ko, nl, pt, ru, zh-Hans, zh-Hant).
  • runtime/nvn/vulkan/pipeline_prewarm.inc & runtime/nvn/vulkan/exec.c:
    • Zero-cost default-off check (prewarm_enabled() checks a cached env override and atomic setting read before any hashing, allocation, or file I/O).
    • Captures PwProgId (vs_h, fs_h, key_h, nv, nf) in compile_program() right before freeing SPIR-V buffers and publishing state.
    • Validates cache/pipeline-prewarm.bin against a 64-byte header (BOTWPW01 magic, version, baked_size, vk_flags, g_game_version, vendorID, deviceID, driverVersion, pipelineCacheUUID, payload hash), strict per-field ranges (ncolor, power-of-two samples, used_streams, vertex attribute bindings, topo_class, PipeFx bounds), and duplicate rejection; flushes atomically via .tmp + rename every 10 s in-world, at loading transitions, and on vk_exec_reset().
    • Tracks jobs_active/jobs_workers/tjobs_active so enqueue_job(..., idle_only=true) never competes with active shader work, probes pipe_cache read-only (pw_cache_find) before insertion so busy rejections never leave NULL slots, enforces PW_HEADROOM = 256 when pipe_keep() > 0, clears pw_pending in pipe_free(), and records first-draw ready hits ([vk] pipeline prewarming: ... ready_at_first_draw ...) with fallback rebuild on state == 2.
    • Handles PipeKey hash collisions non-blockingly via prewarm_resolve_collision() (skips the draw while pp->state == 0 instead of sleeping on the render thread; destroys any built VkPipeline, updates live_gpipes, and rebuilds once pp->state != 0).
    • Isolates per-boot scene detection inside pipeline_prewarm.inc via pw_reset_frame = renderer_present_count() in prewarm_reset() instead of clearing global g_scene_frame in vk_exec_reset().
  • tools/test_pipeline_prewarm.c:
    • Standalone Android Bionic C unit test covering default-off/env override, world-only recording/dedup/cap, atomic binary round-trip + malformed/tampered rejection, loading/unpause/timeout transitions, busy-worker + single-in-flight + PW_HEADROOM guards, first-draw ready hits + failure rebuild, non-blocking hash collision handling, and pw_reset_frame isolation.

Verification

  • Host C/WASM & i18n checks: test_vulkan_compat.py (16), test_vulkan_startup.py (33), test_shader_cache.py (17), and UTF-8 / key parity check across all 11 i18n_*.inc files: PASS.
  • Android Bionic arm64 (SM-X730):
    • tools/test_pipeline_prewarm.c (-Wall -Wextra -Werror): PASS.
    • tools/test_android_runtime.ps1 (6/6: test_kernel_affinity, test_bc_shadow, test_buffer_copy, test_nvn_lifetime, test_object_cache, test_benchmark_trace): PASS.
  • APK build: .\gradlew.bat :app:assembleDebug (JDK 17): BUILD SUCCESSFUL.
  • Physical SM-X730 matched cold-vk_pipelines.bin ABBA stutter check (Mali-G925-Immortalis MC12 v1.r49p1-03bet0, BOTW 1.9.0 Recompiled, hateno-night, 30 fps cap, warm 3,047 SPIR-V, identical 256-record cache/pipeline-prewarm.bin 186,432 B, warmup_s=0, calm_s=0, window_s=12):
Boot Prewarm load_s Queued Post-load world (605–608 frames, arrive..drain) world_build_wait_ms Post-load >50ms / >100ms / max_ms Window (353–356 frames) fps / 1% low Window p99 / max / over_50ms
OFF_1 off 20.4 s 0 4203.8 ms (1211 builds) 22 / 10 / 1938.2 ms 29.36 / 23.66 40.49 / 43.78 / 0
ON_1 on 20.1 s 6 3457.8 ms (1228 builds) 25 / 13 / 2040.7 ms 29.36 / 17.19 38.58 / 111.73 / 1
ON_2 on 20.4 s 5 3447.6 ms (1229 builds) 27 / 14 / 1878.4 ms 29.34 / 24.53 40.09 / 41.08 / 0
OFF_2 off 20.6 s 0 4305.2 ms (1202 builds) 23 / 14 / 1916.7 ms 29.58 / 24.89 39.06 / 41.73 / 0

(With pipe_prewarm = 0 and no env override, 0 [vk] pipeline prewarming: lines are logged and cache/pipeline-prewarm.bin SHA-256 stays unchanged.)

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

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks! It's safe while off. A few things before merging:

  1. The new toggle needs its text in all 11 i18n_*.inc files, and the help text shouldn't mention "(Proposal: Choosing my next contribution without overlapping ongoing work #294)".
  2. g_scene_frame = 0 in vk_exec_reset (exec.c ~6157) runs even with the option off and feeds widescreen.c and the sync-lag check. Please explain why it's needed, or put it behind the option.
  3. The usleep loop on the render thread (~7332) on a hash collision: please make it non-blocking (skip that key this time).

After that I'll do an on/off A/B on the Adreno 830. Stutter numbers on and off from your side would help too.

@Propinbo
Propinbo force-pushed the feat/pipeline-prewarm-294 branch from 56716f0 to 6454ccc Compare November 17, 2026 14:23
@Propinbo

Copy link
Copy Markdown
Contributor Author

Thanks for the review — I have rebased feat/pipeline-prewarm-294 onto current main (26da3fa, commit 6454ccc) and addressed all three requested changes plus the ON/OFF stutter numbers:

  1. All 11 i18n_*.inc files + removed "(#294)" (runtime/ui/menu.mm):
    • Dropped "(#294)" from the menu.mm help text and added both "Pipeline prewarming (experimental)" and its help string to i18n_de.inc, i18n_es.inc, i18n_fr.inc, i18n_it.inc, i18n_ja.inc, i18n_ko.inc, i18n_nl.inc, i18n_pt.inc, i18n_ru.inc, i18n_zh_hans.inc, and i18n_zh_hant.inc.
  2. Removed g_scene_frame = 0 from vk_exec_reset() (runtime/nvn/vulkan/exec.c & pipeline_prewarm.inc):
    • g_scene_frame = 0 is no longer touched in vk_exec_reset(), so widescreen.c and objects.c sync-lag behave identically to main. Instead, prewarm_reset() snapshots pw_reset_frame = renderer_present_count() inside pipeline_prewarm.inc, and prewarm_scene_frame() treats g_scene_frame <= pw_reset_frame as 0 until a post-reset 3D scene frame arrives.
  3. Non-blocking hash-collision handling (runtime/nvn/vulkan/exec.c & pipeline_prewarm.inc):
    • Removed the render-thread usleep(200) spin loop. On a PipeKey hash collision against a prewarmed entry, prewarm_resolve_collision() returns false immediately while pp->state == 0 (skipping that draw as pipeline still building), and once pp->state != 0, destroys any built VkPipeline (decrementing live_gpipes if state == 1), clears pw_pending/prewarmed, updates pp->k, and queues build_pipeline.

Verification (6454ccc on 26da3fa):

  • test_vulkan_compat.py (16), test_vulkan_startup.py (33), test_shader_cache.py (17), tools/test_pipeline_prewarm.c (-Wall -Wextra -Werror on SM-X730, including pw_reset_frame and prewarm_resolve_collision tests), tools/test_android_runtime.ps1 (6/6 on SM-X730), and ./gradlew :app:assembleDebug (JDK 17): all PASS.

Matched cold-vk_pipelines.bin ABBA stutter test on SM-X730 (Mali-G925-Immortalis MC12, hateno-night, 30 fps cap, warm 3,047 SPIR-V, 256-entry pipeline-prewarm.bin, warmup_s=0, calm_s=0, window_s=12):
(Each boot deleted cache/vk_pipelines.bin before launch; OFF runs had pipe_prewarm = 0 and logged 0 prewarming lines with unchanged pipeline-prewarm.bin SHA-256.)

Boot Prewarm load_s Queued Post-load world (605–608 frames, arrive..drain) world_build_wait_ms Post-load p99 / max (ms) Post-load >33 / >50 / >100 ms Window (353–356 frames) fps / 1% low Window p99 / max / over_50
OFF_1 off 20.4 s 0 4203.8 (1211 builds) 155.4 / 1938.2 345 / 22 / 10 29.36 / 23.66 40.49 / 43.78 / 0
ON_1 on 20.1 s 6 (>=1/6 ready) 3457.8 (1228 builds) 148.9 / 2040.7 262 / 25 / 13 29.36 / 17.19 38.58 / 111.73 / 1
ON_2 on 20.4 s 5 (>=1/5 ready) 3447.6 (1229 builds) 164.7 / 1878.4 327 / 27 / 14 29.34 / 24.53 40.09 / 41.08 / 0
OFF_2 off 20.6 s 0 4305.2 (1202 builds) 201.3 / 1916.7 352 / 23 / 14 29.58 / 24.89 39.06 / 41.73 / 0

Note on the single-scene Mali result: because only 5–6 pipelines whose Progs finished compiling before g_scene_frame can prewarm during Continue -> world (without eagerly translating uncompiled shaders), cumulative blocking world_build_wait_ms across world entry dropped from 4.20–4.31 s (OFF) to 3.45–3.46 s (ON, -802 ms mean), while late post-entry pipeline >50 ms hitches (22/23 OFF vs 25/27 ON) and peak hitches (~1.9 s) remain driven by shaders translated after world entry on Mali. Ready for your A/B on Adreno 830.

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

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks, all three are done: the 11 languages, g_scene_frame left alone, and no wait on the render thread. I'll do the on/off A/B on the Adreno 830 and merge if it's clean.

@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks! Tested on the POCO F7 Ultra (Adreno 830, Turnip), Plateau benchmark from a copy of a save, two repeats each: with the option off, the same GPU times as main; with LARECOMP_PIPE_PREWARM=1, "loaded 256 recorded pipelines", 6 queued while loading, no errors, the same times. Merging.

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