Skip to content

vulkan: default to classic VkRenderPass on ARM Mali r49+ and hash get_rp - #534

Merged
mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/vk-mali-renderpass-default
Nov 24, 2026
Merged

mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/vk-mali-renderpass-default

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

On ARM Mali proprietary Vulkan drivers r49+ (tested on Mali-G925-Immortalis MC12, v1.r49p1, Android 16), VK_KHR_dynamic_rendering triggers repeated secondary allocations / mmap+munmap churn inside libGLES_mali.so on the render thread, adding ~2–3 ms per frame in CPU render-thread overhead compared to classic VkRenderPass (rpcompat.c), even when dynamic rendering features are unused.

Meanwhile, rpcompat.c's get_rp() cache previously performed a linear scan over a fixed RP_MAX = 512 array, hashed/compared an RpKey with implicit 2-byte tail padding, and callers did not guard VK_NULL_HANDLE / OOM returns across vk_rp_compat / vk_begin_rendering / ImGui init.


Changes (Updated for Maintainer Review & Rebased onto v0.4.12-beta 79ba1909)

  1. Pure driver gating & precedence (runtime/nvn/vulkan/device_compat.h, runtime/nvn/vulkan/dyn_vi.h):
    • Moved vk_arm_driver_major() from dyn_vi.h into device_compat.h using freestanding ASCII helpers (vk_compat_is_digit / vk_compat_is_alnum) so wasm32 -ffreestanding -O2 -nostdlib compatibility tests stay dependency-free.
    • Added vk_compat_skip_ws(), vk_compat_env_nonzero(), and vk_dyn_render_want(supported, driver_id, driver_info, have_driver_props, no_dr_env, setting_dyn_render, &reason):
      • Hardware lacking dynamic rendering stays OFF ("the driver lacks it").
      • Explicit LARECOMP_NO_DYNAMIC_RENDERING=1/0 retains highest precedence (with leading/trailing whitespace & sign/zero tolerance and malformed-token fallthrough).
      • g_settings.dyn_render (0 = Off, 1 = On, -1 = Auto) follows env override.
      • Under Auto (-1), dynamic rendering defaults OFF only on Android when have_driver_props && driver_id == VK_DRIVER_ID_ARM_PROPRIETARY && major >= 49 ("Mali r49+ default").
  2. Dynamic-rendering setup & Video setting wiring (runtime/settings.h, runtime/settings.c, runtime/nvn/vulkan/device.c, runtime/ui/{launcher.mm,menu.mm}, runtime/ui/i18n_*.inc):
    • Added g_settings.dyn_render (dyn_render = auto | 1 | 0, default -1 Auto) persisted in base.ini (Video -> Dynamic rendering), exposed cleanly in both launcher and in-game menu across all 11 i18n_*.inc locales (note under the setting: defaults off on ARM Mali r49+ drivers).
    • Preserved bool dr_ext_ok = cg[OPT_DR]; before gating on device.c, keeping QCHAIN(dr_ext_ok, q_dr) capability queries accurate and logging exact reason ([vk] no dynamic rendering (Mali r49+ default): classic render passes vs [vk] dynamic rendering enabled (settings)).
  3. O(1) growing hash table & full VK_NULL_HANDLE/OOM propagation (runtime/nvn/vulkan/{rpcompat.c,vk.h,device.c,exec.c,imgui_glue.cpp}, runtime/ui/{launcher.mm,menu.mm}):
    • Replaced the linear 512-cap scan in get_rp() with a dynamic power-of-two open-addressed hash table (RP_INIT_CAP = 1024, rp_grow() doubling at >50% load factor via overflow-safe uint64 comparison and OOM safety). Added explicit RpKey.pad[2] zeroed via _Static_assert + memcpy, _Atomic int nlate acquire/release ordering in vk_rp_collect(), and cap_late > INT_MAX / 2 overflow + realloc OOM safety in destroy_late().
    • Converted vk_begin_rendering and vk_imgui_backend_init to return bool and added vk_rendering_active(). Checked VK_NULL_HANDLE / false returns across all callers (device.c, exec.c, imgui_glue.cpp, launcher.mm, menu.mm including g_init_failed / BackendRendererUserData guards).
  4. Tests (tools/test_mali_rp_default.c, tools/test_rt_load_upload.c, tools/test_android_runtime.ps1):
    • Added host + Android Bionic tests covering vk_dyn_render_want() across Mali r48/r49/r54/NULL, Turnip, RADV, PanVK, Qualcomm, Samsung, g_settings.dyn_render (-1/0/1), env precedence & edge-case tokens, plus test_rpcompat_cache() (OOM before table init, 1024 -> 2048 -> 4096 -> 8192 growth, make_rp()/vkCreateFramebuffer OOM safety, vk_begin_rendering / vk_rendering_active, and drop_all() / vk_rp_collect() deferred-destroy sequence ordering).

Upgrade & Rollback Notes

  • One-time cache upgrade on Mali r49+: Existing vk_pipelines.bin entries built with VK_KHR_dynamic_rendering undergo a one-time rebuild onto classic VkRenderPass (rpcompat.c) on first boot after the change; warm .smeta / SPIR-V caches remain reusable and subsequent boots load warm with classic render passes.
  • Zero-Cost Guarantee & Non-Target GPUs: Default behavior changes only on Android devices reporting VK_DRIVER_ID_ARM_PROPRIETARY (driver-id 9) with major version >= 49 and dyn_render = auto. Adreno/Turnip, Qualcomm proprietary, Mali < r49, Xclipse, PanVK, and desktop (#ifndef __ANDROID__) keep dynamic rendering ON by default.
  • Rollback: Set Settings → Video → Dynamic rendering to On, or pass LARECOMP_NO_DYNAMIC_RENDERING=0.

Verification (Exact Final Commit c1a29d9381aeb63739ecfc9c14f1cb3f00e1ff36 atop v0.4.12-beta)

Environment

  • Device: Samsung Galaxy Tab S11 (SM-X730), SoC MT6991, Android 16 (API 36)
  • GPU / Driver: Mali-G925-Immortalis MC12 - v1.r49p1-03bet0.f8a98b506b89b21c80a508d70abfcb3f (driver-id 9, Vulkan 1.3.278)
  • Game / Base: BOTW 1.9.0 (native), rebased onto v0.4.12-beta (79ba1909f716d7761046a767651c62dbfa917ef1)
  • Signed Release APK SHA-256: 681e4f13d27a5205feaec6515752185c56b188f851799c8bb7a86e8d9dd2a314 (0 new compiler warnings)

Tested vs. Untested Matrix

Target / GPU Status Notes
Mali-G925 (r49p1, SM-X730) Tested (commit c1a29d93) Bionic 9/9 + live 5a_warmup/5a_measured (with inplace_reads=1 + pre-existing vk_pipelines.bin upgrade + 2 reboots) + 5b dyn_render=1 INI + PNG visual check
Older Mali < r49, Turnip, Qualcomm, Xclipse, PanVK Unit-tested in test_mali_rp_default vk_dyn_render_want() verified unchanged (DR=on); not runtime-tested on hardware
Desktop (#ifndef __ANDROID__) Syntax-checked (-U__ANDROID__) settings.c, rpcompat.c, device.c, imgui_glue.cpp, i18n.mm, menu.mm compile cleanly under NDK Clang -Werror

Automated & Unit Checks (c1a29d93)

  • tools/test_vulkan_compat.py: 16 Vulkan compatibility checks passed
  • tools/test_vulkan_startup.py: 33 Vulkan startup checks passed
  • tools/test_pipeline_cache_save.py (executed on SM-X730): PASS
  • tools/test_android_runtime.ps1 -DeviceSerial R52YA06LXAT: 9/9 Bionic tests passed (including expanded test_mali_rp_default and updated test_rt_load_upload)
  • workspace_check.py pr: PASS (commits=1 files=25 mergeable=YES)

Live SM-X730 Checks (c1a29d93)

  1. Default Classic RP + inplace_reads=1 + Pre-existing vk_pipelines.bin (67,078,586 B) Upgrade (cap30, res_mode=10, upscaler=3 SGSR1, 2 in-place reboots):
    • Startup log: [vk] no dynamic rendering (Mali r49+ default): classic render passes (1..8 RPs created) and [vk] targets a draw writes, sampled by it: in place (settings).
    • Reboots clean (0 kept after teardown in 254/278 ms and 279/226 ms; shader-meta: 435 hits / 3 misses then 402 hits / 0 misses).
    • 5a_measured (451/451/243 frames): plateau 30.0 fps (low_1% 29.57, gpu p50 28.074 ms, render p50 17.018 ms, skipped 0), zora-plaza 30.0 fps (low_1% 29.41, gpu p50 30.598 ms, render p50 18.243 ms, skipped 0), kakariko-day 16.19 fps (low_1% 12.74, gpu p50 59.510 ms, render p50 42.871 ms, skipped 2 — GPU-bound at res_mode=10); visual captures clean across all 3 scenes.
  2. Settings Override (dyn_render = 1 in base.ini, zero feature env vars):
    • Startup log: [vk] dynamic rendering enabled (settings); title screen rendered cleanly.

(Historical 9d5e732c cap-30/60p A/B benchmark & simpleperf data comparing Dynamic Rendering ON vs Classic RP default on Mali-G925 showed -1.87 to -2.40 ms render-thread p50 savings at cap-30 and +3.46 fps in kakariko-day 60p; archived on evidence/pr-mali-renderpass-default-20261121.)

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 21, 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! On Turnip nothing changes by default: vk_dyn_render_want returns true unless the variable is set, and every rpcompat entry point returns early on g_vk.dyn_render. The hash table is a good change.

Before merging:

  1. This turns #518 off. #518 only enables local read with dynamic rendering (g_vk.local_read = want_lr && g_vk.dyn_render && ...), and this PR turns dynamic rendering off by default on the same Mali r49+ drivers. With both merged, local read would never turn on. Which one do you want on those drivers? I'd like to settle that before merging either.
  2. The first session after the update on Mali r49+. prewarm_vk_flags() includes dyn_render, so every prewarm file is rejected once, and every vk_pipelines.bin entry misses (the pipelines are now built against vk_rp_compat render passes). The first session stutters like a fresh install. Fine, but it needs a line in the release notes.
  3. One device measured (G925, r49p1), and the default flips for every r49+ Mali (G715/G720 phones and others). Since most players can't set variables, could the rollback be a Settings option instead of env-only?
  4. Could you do one Mali run with inplace_reads=1 (#507, now in main) on this PR? #507 was probably checked with dynamic rendering.

Minor:

  • get_rp returns make_rp(k) without caching when the first rp_grow() fails or the insertion is refused, so every call then makes and leaks a render pass (main has the same leak past RP_MAX, out of memory only).
  • nrps == 512 in the log is now arbitrary.
  • LARECOMP_NO_DYNAMIC_RENDERING=abc now forces dynamic rendering on for Mali (main's atoi read it as not off).

I'll run LARECOMP_NO_DYNAMIC_RENDERING=1 on the POCO with this PR (3-scene benchmark plus two reboots) to exercise the hash table.

@Propinbo
Propinbo force-pushed the feat/vk-mali-renderpass-default branch from 9d5e732 to c1a29d9 Compare November 24, 2026 15:08
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased onto upstream/main (v0.4.12-beta, 79ba1909, amended commit c1a29d93) and updated in response to review feedback:

Review Updates Implemented (c1a29d93)

  • Video setting & localization (g_settings.dyn_render): Added -1 (Auto), 0 (Off), 1 (On) persisted as dyn_render in base.ini and wired through settings.{h,c}, device.c, launcher.mm, menu.mm, and all 11 i18n_*.inc locales (descriptive Mali r49+ note included, without any #NNN issue numbers). LARECOMP_NO_DYNAMIC_RENDERING=1/0 overrides the setting, and unsupported hardware can never be forced ON.
  • Full VK_NULL_HANDLE / OOM propagation (rpcompat.c, vk.h, device.c, exec.c, imgui_glue.cpp, launcher.mm, menu.mm):
    • Converted vk_begin_rendering() and vk_imgui_backend_init() to return bool and added vk_rendering_active().
    • Guarded all vk_rp_compat() and vk_begin_rendering() call sites (exec.c, device.c, imgui_glue.cpp, launcher.mm, menu.mm), plus ImGui::GetIO().BackendRendererUserData check in vk_imgui_texture_rgba() and clean g_init_failed / exit(EXIT_FAILURE) handling in menu.mm / launcher.mm.
    • Added uint64 load-check arithmetic, INT_MAX / 2 deferred-queue growth guard, and checked realloc in rpcompat.c, plus expanded test_mali_rp_default.c (1024 -> 2048 -> 4096 -> 8192 rehash, make_rp/vkCreateFramebuffer OOM, vk_begin_rendering, and drop_all()/vk_rp_collect() queue ordering).
  • Upgrade release note: Documented in the PR description that Mali r49+ users updating from dynamic-rendering caches experience a one-time vk_pipelines.bin rebuild to classic render passes (while keeping .smeta / SPIR-V caches intact).

Verification on Exact Final Commit (c1a29d93)

  • Unit & host checks: test_vulkan_compat.py (16/16), test_vulkan_startup.py (33/33), test_pipeline_cache_save.py on SM-X730 (PASS), tools/test_android_runtime.ps1 (9/9 Bionic tests including expanded test_mali_rp_default and test_rt_load_upload), WSL gcc & NDK Clang -Werror Android/Desktop syntax checks (0 errors), and workspace_check.py pr (commits=1 files=25 mergeable=YES).
  • Pre-push adversarial review: 3 parallel read-only reviewers confirmed READY (0 CRITICAL / 0 MAJOR).
  • SM-X730 live check (681e4f13..., Mali-G925 v1.r49p1, BOTW 1.9.0):
    • Confirmed [vk] no dynamic rendering (Mali r49+ default): classic render passes + [vk] targets a draw writes, sampled by it: in place (settings) on pre-existing vk_pipelines.bin (67,078,586 B) upgrade across 2 clean in-place reboots and all 3 benchmark scenes (plateau 30.0 fps / 28.074 ms GPU p50, zora-plaza 30.0 fps / 30.598 ms GPU p50, kakariko-day 16.19 fps / 59.510 ms GPU p50 at res_mode=10, all scenes visually verified).
    • Confirmed dyn_render = 1 in base.ini (with zero feature env vars) logs [vk] dynamic rendering enabled (settings) and renders cleanly.

@mamcjr
mamcjr merged commit 98dd516 into mamcjr:main Nov 24, 2026
@mamcjr

mamcjr commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Merged, thanks! It conflicted with #531 only in tools/test_android_runtime.ps1; I kept both test lists. It's in 0.4.13-beta, with a line in the notes about the one-time pipeline rebuild on Mali r49+.

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