Skip to content

vk: prioritize synchronous shader/pipeline builds and drain workers on reset - #481

Merged
mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/urgent-shader-builds
Nov 18, 2026
Merged

mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/urgent-shader-builds

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

On Android (especially Mali where driver vkCreateGraphicsPipelines is slow and VK_EXT_graphics_pipeline_library is unavailable), synchronous draws (keep == true: persistent targets, one-off passes, and no-depth passes) could stall behind dozens of background keep == false shader translations/pipeline builds and polled usleep(200) in wait_built():

  1. Priority queuing (runtime/nvn/vulkan/job_queue.h, LARECOMP_URGENT_SHADERS, default 1 on Android, 0 on desktop):
    • Extracts MwJob into runtime/nvn/vulkan/job_queue.h with mw_job_push(..., urgent), mw_job_pop_active, mw_job_finish, and mw_job_steal.
    • Synchronous (keep == true) Prog builds are pushed to the head of tjobs / jobs and escalate in-flight non-urgent Progs so translate_program() also head-enqueues compile_program().
  2. Condvar wakeups, work-stealing, and inline urgent pipelines (runtime/nvn/vulkan/exec.c):
    • Replaces usleep(200) polling with build_done_cond broadcasts from signal_build_done() / finish_active_job().
    • When wait_built_job() runs for a synchronous draw, it steals and executes the matching queued Prog (g_inline_steal) or Pipe on the waiting render thread instead of sleeping behind unrelated async work; synchronous new/evicted Pipe builds call build_pipeline_urgent() inline.
    • Records BP_BUILD_WAIT_NS, BENCH_BUILD(), and BP_REBUILDS accurately (abort checks run before BENCH_BUILD()), and logs [vk] shader-build (reboot|exit) counters (vk.h, device.c).
  3. pthread_once pool init & clean reboot drain (vk_exec_reset()):
    • Replaces unsynchronized static bool started with pthread_once + pthread_detach and falls back inline if job allocation fails.
    • Adds jobs_drain() with jobs_abort + active job tracking at the start of vk_exec_reset() so in-flight translators/workers finish cleanly before vkFreeMemory(gmem_mem) unmaps HEAP_BASE on Turnip map_placed.
  4. Unit test (tools/test_shader_job_queue.c):
    • Tests empty/1-item/head/middle/tail mw_job_steal, LIFO urgent vs FIFO ordering, active idle detection, and a 600-item multi-threaded worker + stealer + two-queue forwarding race test.

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_job_queue.c — passed.
  • Upstream Android ARM64 Bionic suites (tools/test_android_runtime.ps1 on SM-X730): 6/6 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)

  • Focused standalone hardware measurements for this commit alone have not yet been run separately; in combined A/B testing alongside sibling optimizations (.smeta metadata cache and VK_EXT_vertex_input_dynamic_state) on hateno-night (cap30, sgsr1 1080p, warm .spv, 0 Vulkan errors):
    • SM-X730 (Mali-G925 MC12 r49p1): World-entry (frame >= 480) compile wait dropped 4968 -> 1296 ms (with .smeta cold) -> 994 ms (5.0x, warm .smeta), exit [vk] shader-build max wait dropped 1351.32 -> 45.57 ms.
    • Adreno 830 (Turnip_Gen8_V38_Patched, driver-id 18): Exit wait dropped 738.17 -> 126.40 ms (-82.9%), max wait 94.21 -> 5.62 ms (16.8x); post-rebase smoke verified [vk] reboot: 467 programs, 854 pipelines and 19 compute programs freed (0 kept) cleanly drained before vkFreeMemory(gmem_mem).

Companion Focused PRs

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 17, 2026
@Propinbo
Propinbo force-pushed the feat/urgent-shader-builds branch from c1b10cc to eaec823 Compare November 17, 2026 19:47
Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 17, 2026
…rebases, PR mamcjr#485 async slots, Mali perf ABBA & release rollout
@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks! The drain on reset fixes a real problem (workers reading the heap across its free), and I see no deadlock in the new waits. Two things before merging:

  1. The new places where the render thread now waits (wait_built_job ~1614, the inline translate/compile/pipeline builds ~7706/7711, jobs_drain) have no freeze-watchdog marker (runtime/nvn/vulkan/watchdog.h). Please add one (e.g. WD_R_BUILD_WAIT), so a freeze there is reported as such: we're chasing a freeze on the Mali-G1 ([Mali-G1-Ultra][Dimensity 9500] Random GPU hang, occurs in 5-20 mins #401) with exactly those markers.
  2. It conflicts with vk: cache Maxwell stage translation metadata (.smeta) beside warm SPIR-V #482 (both add the same pools and drain with different names). Let's land vk: prioritize synchronous shader/pipeline builds and drain workers on reset #481 first and rebase vk: cache Maxwell stage translation metadata (.smeta) beside warm SPIR-V #482 on it.

I'll test the in-place reboot and the benchmark scenes (many reboots) here. Since the numbers in the PR come from the combined prototype, an A/B of this change alone (frame time when a keep draw translates inline on the render thread, on your Mali) would help.

@Propinbo
Propinbo force-pushed the feat/urgent-shader-builds branch from eaec823 to e8fac0f Compare November 17, 2026 22:57
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (011e1da → e8fac0f):

  1. Freeze-watchdog markers (runtime/nvn/vulkan/watchdog.{h,c}, exec.c): Added WD_R_BUILD_WAIT ("render waiting on shader build") and instrumented all render-thread shader/pipeline build waits:
    • wait_built_job() (covers urgent-prioritized and work-stolen shader/pipeline waits as well as persistent-target waits),
    • build_pipeline_urgent() (inline synchronous keep pipeline compile on render thread),
    • schedule_pipeline_build() / render_dispatch_program() / compute_program() under LARECOMP_SYNC_SHADERS,
    • jobs_drain() (draining tjobs/jobs on reset/teardown).
    • Also added translator_started / workers_ok guards so run_translate_ex() / run_job_ex() fall back inline if pthread_create() fails, and strengthened tools/test_shader_job_queue.c (PASS on ARM64 Bionic SM-X730 + test_rt_load_upload).
  2. Isolated Mali-G925 A/B (SM-X730, e8fac0f alone, plateau, 30 fps cap, LARECOMP_URGENT_SHADERS=0 vs =1):
Condition URGENT Shader Wait (exit) Max Single Wait Stolen Jobs cam_s gt>50ms / gt>100ms (CSV) [watchdog]
Cold .spv + cold vk_pipelines.bin 0 6190.11 ms 388.46 ms 0 / 718 9.0 s 31 / 21 0
Cold .spv + cold vk_pipelines.bin 1 4025.32 ms (-35.0%) 77.93 ms (5.0× lower) 168 / 718 7.5 s 20 / 13 0
Warm .spv + cold vk_pipelines.bin 0 4495.31 ms 432.85 ms 0 / 702 9.1 s 25 / 18 0
Warm .spv + cold vk_pipelines.bin 1 2969.47 ms (-33.9%) 98.47 ms (4.4× lower) 168 / 718 7.0 s 19 / 11 0
Steady warm .spv + vk_pipelines.bin 0 751.10 ms 77.90 ms 0 / 721 6.3 s 20 / 10 0
Steady warm .spv + vk_pipelines.bin 1 326.72 ms (-56.5%) 10.70 ms (7.3× lower) 168 / 718 6.0 s 16 / 9 0

#482 is now rebased on top of e8fac0f.

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 17, 2026
…ew fixes, Mali A/B & stress validation, and release APK update
@mamcjr

mamcjr commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks, the watchdog markers are in and the A/B is convincing (cold cache 6190 to 4025 ms of shader waits, the worst single wait 388 to 78 ms). I'll run the in-place reboot and the benchmark scenes here and merge.

@mamcjr

mamcjr commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks! Tested on the POCO F7 Ultra (Adreno 830, Turnip) stacked with #483, #482, #453, #455 and #457 on current main: every in-place reboot freed all programs and pipelines with 0 still building, no errors. Merging.

@mamcjr
mamcjr merged commit d303dfd into mamcjr:main Nov 18, 2026
Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 20, 2026
…erged), grass triage parked, Master Plan & device scope
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