Skip to content

vk/mali: defer full-surface clears, suppress 3ms pass split, and emit STORE_OP_NONE - #586

Open
Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:feat/mali-rp-merge-store-opt
Open

Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:feat/mali-rp-merge-store-opt

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Reduces redundant tile writebacks, reloads, and command-buffer flushes on ARM Mali proprietary r49+ GPUs (!has_bc && has_astc_ldr && have_driver_props && driverID == VK_DRIVER_ID_ARM_PROPRIETARY && vk_arm_driver_major(driverInfo) >= 49, tested on Mali-G925-Immortalis MC12 v1.r49p1-03bet0) via two independently toggleable render-pass optimizations:

  1. Deferred full-surface clears & 3 ms mid-pass split suppression (LARECOMP_MALI_RP_MERGE_OPT)

    • Defers whole-surface CMD_CLEAR_COLOR, CMD_CLEAR_DEPTH_STENCIL, and CMD_CLEAR_TEXTURE clears (MaliDeferredClearTable, 64 slots + 64-bit Bloom filter) without splitting an open VkRenderPass unless the cleared subresource conflicts with active pass attachments or draws reading from it (MaliActivePassImgs).
    • Folds pending clears directly into VK_ATTACHMENT_LOAD_OP_CLEAR when the target pass begins (marking those attachments in pass_written so subsequent in-place #549 reads trigger resolve_feedback() cleanly), and materializes any remaining unconsumed clears cleanly (bracketed by vk_full_barrier(), creating a temporary VkImageView from the underlying image if the cached view was evicted, and removing records on destroy/replace via vk_mali_rp_forget_image()) before texture/alias reads, buffer copies, compute dispatches, shader dumps, vk_present_now(), and vk_submit_extra().
    • Preserves #296 unowned write-back safety by verifying b && b->image && b->gpu_owned and rejecting wb_eligible, samples > 1, depth > 1, 3D, bc_vf, and astc_bw targets before mutating backend state.
    • Suppresses the default 3 ms timer split in split_long_pass() when mali_rp_merge_opt is active (so large tile-based passes like G-buffer avoid unnecessary store/reload roundtrips), while still honoring an explicit LARECOMP_VK_SPLIT_US env override and an 8192-draw safety cap.
  2. VK_ATTACHMENT_STORE_OP_NONE lookahead (LARECOMP_MALI_RP_STORE_OPT, requires Vulkan >= 1.3)

    • Caches fragment-shader output component masks (g_mali_fs_meta) and inspects only VK_ATTACHMENT_LOAD_OP_LOAD attachments (cand_mask) ahead in the current CmdRec pass (bounded at 2048 records, aligning early-stop checks with actual split_long_pass / gres_premerge / non-inline boundaries).
    • Assigns VK_ATTACHMENT_STORE_OP_NONE (via vk_mali_choose_store_op) exclusively to VK_ATTACHMENT_LOAD_OP_LOAD color/depth/stencil attachments proven unwritten in that pass; if a later draw or clear writes to any attachment in g_mali_pass_store_none_mask, st.rt_dirty = true restarts the render pass cleanly so a fresh look-ahead runs.
    • Zero hot-path cost on Turnip/Qualcomm, non-ARM GPUs, Mali < r49, desktop (!__ANDROID__), or when disabled (g_vk.mali_rp_merge_opt == false / g_vk.mali_rp_store_opt == false).

Gating, Defaults & Rollback Knobs

  • LARECOMP_MALI_RP_MERGE_OPT (0/1): defaults on only when __ANDROID__ && !has_bc && has_astc_ldr && have_driver_props && driverID == VK_DRIVER_ID_ARM_PROPRIETARY && vk_arm_driver_major(driverInfo) >= 49. Set LARECOMP_MALI_RP_MERGE_OPT=0 to disable deferred clears + 3 ms split suppression, or =1 to opt in on other !has_bc && has_astc_ldr drivers.
  • LARECOMP_MALI_RP_STORE_OPT (0/1): defaults on under the same ARM Mali r49+ gate plus api >= VK_API_VERSION_1_3. Set LARECOMP_MALI_RP_STORE_OPT=0 to keep VK_ATTACHMENT_STORE_OP_STORE everywhere.

Tested vs. Untested Matrix

Platform / GPU Verification Status Default Behavior
ARM Mali-G925-Immortalis MC12 (SM-X730, Android 16 API 36, v1.r49p1-03bet0, Vulkan 1.3.278, Classic VkRenderPass) Tested on 40ccf68 (12/12 Bionic units, 3-scene same-frame parity, 31.6-min continuous soak; pre-rebase 4636313 3-arm A/B cycles) Enabled (ARM Mali r49+ default)
Adreno / Turnip (has_bc = true) & older Vulkan < 1.3 GPUs Unit-gated & reviewed (not benchmarked on hardware) Disabled (merge=0, store=0)
Non-ARM !BC && ASTC GPUs (PanVK, PowerVR, Maleoon) & ARM Mali < r49 Unit-gated & reviewed (not tested on hardware) Disabled by default (LARECOMP_MALI_RP_*=1 overrides)
Desktop Linux/Windows (!__ANDROID__) Host C11 compile/run verified (-U__ANDROID__) Compiled out (false)

Verification on Rebased Commit 40ccf68 (SM-X730, v0.5.1-beta Base da30d45)

1. Startup Capability Proof (defaults ON — both env vars unset)

[vk-cap] gpu Mali-G925-Immortalis MC12 vendor 0x13b5 device 0xd8300010 api 1.3.278 driver-id 9 name Mali-G925-Immortalis MC12 info v1.r49p1-03bet0.f8a98b506b89b21c80a508d70abfcb3f
[vk-cap] Mali RP merge opt: on (ARM Mali r49+ default)
[vk-cap] Mali RP store opt: on (ARM Mali r49+ default)

2. Same-Frame Visual Parity (2560x1440, bilinear, isolated scratch on 40ccf68)

Comparison between baseline (LARECOMP_MALI_RP_MERGE_OPT=0, LARECOMP_MALI_RP_STORE_OPT=0) and defaults ON (both unset) across fixed camera/time-of-day scripted launches:

Scene Best Aligned Pair PSNR (dB) Mean Abs Diff (0..255) % <= 4 Errors (INVARIANT/DEAD VIEW/VUID) Visual Notes
Hateno Dye Interior (dye, static indoor lighting) frame_00784 vs frame_00780 34.89 1.444 91.22% 0 Shelves, bottles, wood grain, and Link gear match; residual diff is 4-frame idle breathing sway around Link's quiver/bow.
Zora's Domain Plaza (zora, water/NPC/UI) frame_00784 vs frame_00781 27.05 4.553 71.37% 0 Crystal pillars, water reflections, shadows, and HUD identical; diff is NPC dialogue bubble/pose & Link stance sway across 3 frames.
Great Plateau (plat, open grass/cliffs) frame_00780 vs frame_00780 14.21 35.652 12.09% 0 Foreground wind-blown grass tufts + passing cloud shadow between boots (luma 107.4 vs 127.3, matching natural open-world boot variance); cliffs, pine shadows, shrine gate, and HUD intact.
Scene Side-by-Side Crop (Baseline A | Candidate B | 8x |A-B|) Full Frame (Defaults ON)
Hateno Dye Interior dye crop dye on
Zora's Domain Plaza zora crop zora on
Great Plateau plat crop plat on

3. 31.6-Minute Continuous Stability Soak (SM-X730, 40ccf68, Defaults ON)

Continuous idle-world soak in Zora's Domain (75% -> sgsr1 2560x1600, 30 Hz cap, inplace_reads=1, isolated scratch):

  • Duration & Integrity: 1898.8 s game time (1890.0 s polled at 15s intervals, 43,714 frames), single uninterrupted PID (30163), 0 crashes, 0 INVARIANT VIOLATION / DEAD VIEW / VUID- / VK_ERROR / WATCHDOG stalls, 29.0 -> 30.4 °C battery temp, 67/67 saves intact afterward.
  • Transient Ring & Memory-Mapping Stability (t=300..1890s steady window):
    • transient ring: initialized once (memory type 0 (device-local host-visible )), flat at ring 256 MB in 4 from t~30s through t=1898.8s (command shadow pages 52, host command chunks 0, gpu mem 1440 MB flat).
    • VmRSS: 3897.8 MB (t=300s) -> 3923.6 MB (900s) -> 3930.4 MB (1500s) -> 3934.2 MB (1890s, plateau across final 18 min).
    • VmSize: 45851.0 MB (t=300s) -> 45874.1 MB (t=1890s).
    • /proc/30163/maps: 8860 (t=300s) -> 9233 (1500s) -> 9248 (1890s, +15 across the final 390s / ~9,100 frames).

Pre-Rebase Cycle Benchmark (Earlier Commit 4636313, SM-X730 Mali-G925 MC12)

Measured at 75% preset (1920x1200 -> SGSR1 2560x1600), 30 Hz cap, 10s steady window after warmup:

  • arm0_off: LARECOMP_MALI_RP_MERGE_OPT=0, LARECOMP_MALI_RP_STORE_OPT=0
  • arm1_merge: LARECOMP_MALI_RP_MERGE_OPT=1, LARECOMP_MALI_RP_STORE_OPT=0
  • arm2_both: LARECOMP_MALI_RP_MERGE_OPT=1, LARECOMP_MALI_RP_STORE_OPT=1
Scene Arm FPS 1% low gpu p50 / p95 (ms) rnd p50 / p95 (ms) GPU Clk med / mean (MHz) MCyc/frame med / mean Passes/2s (Flushes/2s) Errors
plateau arm0_off (0/0) 30.00 29.55 28.95 / 34.10 19.12 / 21.44 1143.9 / 1143.9 33.11 / 33.11 9949 (558) 0
plateau arm1_merge (1/0) 30.00 29.52 27.47 / 33.14 18.12 / 21.11 1118.4 / 1117.2 30.73 / 30.69 (-7.3%) 8634 (435) 0
plateau arm2_both (1/1) 30.00 29.43 27.52 / 33.25 17.02 / 19.75 1118.0 / 1098.1 30.77 / 30.22 (-8.7%) 8556 (416) 0
zora-plaza arm0_off (0/0) 30.00 29.51 30.55 / 35.09 18.99 / 21.40 1324.6 / 1322.2 40.46 / 40.39 11318 (584) 0
zora-plaza arm1_merge (1/0) 30.00 29.49 29.33 / 34.47 18.80 / 21.38 1325.8 / 1323.0 38.89 / 38.80 (-3.9%) 10357 (549) 0
zora-plaza arm2_both (1/1) 30.00 29.50 29.21 / 34.77 19.31 / 22.01 1325.8 / 1315.1 38.73 / 38.42 (-4.9%) 10187 (553) 0
kakariko-day arm0_off (0/0) 29.44 15.53 30.41 / 34.87 21.17 / 23.86 1325.4 / 1334.4 40.31 / 40.58 10679 (595) 0
kakariko-day arm1_merge (1/0) 29.87 19.86 29.63 / 34.77 20.43 / 23.48 1325.4 / 1310.4 39.27 / 38.83 (-4.3%) 9896 (491) 0
kakariko-day arm2_both (1/1) 30.00 29.35 29.80 / 34.92 19.36 / 22.20 1326.1 / 1326.1 39.52 / 39.52 (-2.6%) 9987 (488) 0

Note on MCyc/frame: Dimensity DVFS targets ~28–30 ms per 30 Hz frame by stepping clocks down (1326 -> 1144 -> 1118 MHz), so MCyc/frame = gpu_p50_ms * gpu_mhz / 1000 tracks true GPU workload reduction across clock steps.


Automated Tests (40ccf68)

  • Host C11 unit test (-std=c11 -Wall -Wextra -Werror -O2 -U__ANDROID__): tools/test_mali_rp_opt.c — PASS.
  • NDK aarch64-linux-android30 syntax & link checks (device.c, exec.c, textures.c, tools/test_rt_load_upload.c): PASS (0 errors).
  • On-device Bionic regression suite (tools/test_android_runtime.ps1 + test_mali_rp_opt on SM-X730): 12 / 12 PASS (rc=0, 0 mock validation hazards) — test_kernel_affinity, test_bc_shadow, test_buffer_copy, test_mali_rp_default, test_mali_rp_opt, test_rt_load_upload (including Case 10 mali_rp integration tests), test_cbwin_snap, test_nvn_lifetime, test_object_cache, test_benchmark_trace, test_bc_parallel, test_motion.

@mamcjr

mamcjr commented Nov 27, 2026

Copy link
Copy Markdown
Owner

The deferred clears are careful (applied before every reader, CPU write-back targets excluded, exact matches only), and the measured cycles are a good start. A few correctness gaps first:

  1. STORE_OP_NONE can drop writes. mali_check_store_none_write (exec.c:3669, called at :9695) only logs "INVARIANT VIOLATION" and still records the draw. The look-ahead treats as a pass end some commands that may not end it: the early returns in copy_buffer_to_texture (:9914), copy_texture_to_buffer (:9971) and copy_texture (:10060) against :2935-2941; and its 8192-draw stop (:3044) doesn't match the real split (:10679, only with async on, outside menus, and not counting skipped draws). With only the store option on, nothing splits at 8192 at all. Result: an attachment loaded and untouched early in the pass, then drawn after the look-ahead stopped, keeps its old contents. Suggestion: in draw_ before begin_pass, and in clear(), if the write mask overlaps g_mali_pass_store_none_mask, set st.rt_dirty so the pass restarts with a fresh look-ahead.
  2. Folded clears and in-place reads. Since 4:3 Unlocked aspect: black shadow away from Link that moves with camera pitch (fix included, tested on RG477M) #549, Mali's Automatic samples a draw's own target in place. A clear folded into the load op isn't added to pass_written (:9685), so resolve_feedback (:5393, :5460) doesn't end the pass, and a draw sampling the target before writing it reads pre-clear memory (clear X, draw Y, back to X, sample X). Please set that attachment's bit in pass_written when begin_pass folds a clear.
  3. Lost clear. mali_dclear_materialize returns without clearing when the cached view is dead (:2563), and the view table evicts (textures.c:1559). Please create the view from the image when the clear is applied.
  4. Gating. It turns on for any GPU without BC and with ASTC: PanVK, PowerVR, Maleoon and older Mali drivers too. Please limit it to VK_DRIVER_ID_ARM_PROPRIETARY (r49+, as vulkan: resolve warm shader and pipeline cache hits inline on Mali r49+ #529 does).
  5. Tests and evidence. test_mali_rp_opt.c covers the header helpers only; an integration test in the mocked exec.c (look-ahead stop, clear order, folding) would catch 1-3. And, since it removes the 3 ms split: a 30+ minute session with the ring and memory-mapping counts (Report of a crash in version v0.4.7-beta [Mali] after 30 minutes and waiting 15 seconds as requested. #496 Poço x7 pro 12g / 512g #560), plus a same-frame screenshot comparison.

Also: it conflicts with main in tools/test_android_runtime.ps1 now (#545).

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 27, 2026
@Propinbo
Propinbo force-pushed the feat/mali-rp-merge-store-opt branch from 4636313 to 40ccf68 Compare November 27, 2026 15:51
@Propinbo

Copy link
Copy Markdown
Contributor Author

All 5 review points and the merge conflict on v0.5.1-beta (da30d45) have been resolved in rebased commit 40ccf68:

  1. STORE_OP_NONE restarts & aligned look-ahead stop (runtime/nvn/vulkan/exec.c):

    • Look-ahead now matches real pass termination across copy early-returns and split_long_pass() (only stopping at 8192 under the real async/non-menu/mali_rp_merge_opt split condition, instead of artificially stopping when store_opt runs alone).
    • Added upfront pass-restart guards in draw_() (before begin_pass()) and clear(): whenever g_mali_rp_store_opt && in_pass && !st.rt_dirty && (write_mask & g_mali_pass_store_none_mask), st.rt_dirty = true restarts the render pass cleanly so a fresh look-ahead runs, while keeping mali_check_store_none_write() as a defense-in-depth invariant guard.
  2. Folded clears & #549 in-place reads (runtime/nvn/vulkan/exec.c):

    • When begin_pass() folds deferred clears into VK_ATTACHMENT_LOAD_OP_CLEAR, it now sets the cleared color (1u << i) / depth-stencil (1u << 8) bits in pass_written, so resolve_feedback() sees prior clears and splits the pass before any subsequent in-place sample of that target.
  3. Evicted cached view recovery & image-destroy cleanup (exec.c, textures.c):

    • When e->view is no longer live at materialization time, mali_dclear_materialize() now creates a temporary VkImageView from e->image for the clear and destroys it immediately after, instead of dropping the clear. Added vk_mali_rp_forget_image() at texture destroy/replace boundaries in runtime/nvn/vulkan/textures.c.
  4. Strict VK_DRIVER_ID_ARM_PROPRIETARY (r49+) gating (mali_rp_opt.h, device.c):

    • Narrowed default activation in both vk_mali_rp_merge_opt_want() and vk_mali_rp_store_opt_want() to !has_bc && has_astc_ldr && have_driver_props && driverID == VK_DRIVER_ID_ARM_PROPRIETARY && vk_arm_driver_major(driverInfo) >= 49 (store_opt still requires Vulkan >= 1.3), keeping PanVK, PowerVR, Maleoon, and Mali < r49 off by default while retaining LARECOMP_MALI_RP_MERGE_OPT=0/1 and LARECOMP_MALI_RP_STORE_OPT=0/1 under !has_bc && has_astc_ldr. Verified startup logs with both env vars unset:
      [vk-cap] Mali RP merge opt: on (ARM Mali r49+ default) / [vk-cap] Mali RP store opt: on (ARM Mali r49+ default).
  5. Mocked exec.c integration tests, 31.6-min stability soak, & same-frame parity (40ccf68, SM-X730):

    • Mocked exec.c tests (tools/test_rt_load_upload.c, Case 10 mali_rp): Exercises look-ahead copy/cap/dup stops, STORE_OP_NONE restart on late write, folded LOAD_OP_CLEAR pass_written feedback split, evicted-view clear materialization via temporary view, and fence sequence tracking. tools/test_android_runtime.ps1 + test_mali_rp_opt: 12/12 PASS on SM-X730 (test_rt_load_upload: all passed (0 failure(s)), 0 validation hazards), plus host C11 (-U__ANDROID__ -Werror) PASS.
    • 31.6-min continuous soak (SM-X730, defaults ON, Zora 75% SGSR1): 1898.8 s (43,714 frames, single PID 30163, 0 crashes, 0 INVARIANT VIOLATION / DEAD VIEW / VUID- / VK_ERROR / WATCHDOG, 29.0 -> 30.4 °C, 67/67 saves intact). Transient ring remained steady at ring 256 MB in 4 (command shadow pages 52, host command chunks 0, gpu mem 1440 MB flat). Steady-state (t=300..1890s): VmRSS 3897.8 -> 3934.2 MB, VmSize 45851.0 -> 45874.1 MB, /proc/30163/maps 8860 -> 9248 (+15 over final 390 s).
    • 3-scene same-frame parity (2560x1440 bilinear, defaults ON vs 0/0, 0 errors):
      • Hateno Dye Interior (static indoor): PSNR 34.89 dB, mean_abs 1.444, pct <= 4: 91.22% (frame_00784 vs frame_00780, Link idle sway only)
      • Zora's Domain Plaza: PSNR 27.05 dB, mean_abs 4.553, pct <= 4: 71.37% (frame_00784 vs frame_00781, NPC animation & stance sway; clean rendering)
      • Great Plateau: PSNR 14.21 dB, mean_abs 35.652 (frame_00780 vs frame_00780, wind-blown foreground grass + passing cloud shadow between boots; identical cliffs/pine shadows/HUD)
      • Comparison crops & full frames are embedded in the updated PR description above (evidence/pr-reviews-2026-11-27/pr586/).
  • Also resolved the tools/test_android_runtime.ps1 rebase conflict cleanly atop da30d45 (v0.5.1-beta).

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