Skip to content

vulkan: add submission-scoped cbwin snapshot cache and Mali copy-mode support - #506

Merged
mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/cbwin-mali-snap
Nov 20, 2026
Merged

mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:feat/cbwin-mali-snap

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Reconciles constant-buffer window (cbwin, merged upstream in #453) with Android gcopy_mode GPUs (ARM Mali), cuts redundant per-draw full-UBO transient copies via a submission-scoped base snapshot cache, isolates cbwin async writeback/retire synchronization so non-cbwin devices incur zero extra atomic/seqlock churn, and shrinks per-draw CBP (cbw_patch) uploads.

Key Changes

  1. Mali copy-mode support in cbwin_wanted() / cbwin_bind() (runtime/nvn/vulkan/exec.c):
    • Defaults cbwin_wanted() ON for !gmem_placed && gcopy_mode && vk_driver_is_mali(), evaluating !gmem_placed && gcopy_mode (memory_order_relaxed) before vk_driver_is_mali() so Turnip and gmem_placed builds short-circuit without probing Mali state (mock_mali_calls == 0); non-Mali/non-Turnip GPUs stay default-off unless LARECOMP_CB_WINDOW=1.
    • Preserves #453 behavior: LARECOMP_CBWIN_SNAPSHOT=0 or AB("cbsnap") == 1 falls back cleanly to guest_copy() in gcopy_mode, or binds gmem_buf directly when gmem_placed.
    • Rebased cleanly on v0.4.8-beta (#508 gcopy_dchain merged): vk_exec_reset() preserves #508's gwin_mode / dchain resets alongside cbwin_env, cbwin_snap_env, gmem_placed, cbwin_retire_*, and snapshot/patch caches.
  2. Submission-scoped base snapshot cache (runtime/nvn/vulkan/cbwin_snap.h, exec.c):
    • Adds CbwinBaseCache (16-set x 4-way associative, FIFO/RR per set), keyed by (vk_pending_seq() + (cbwin_retire_gen << 40), addr, size, win_lo, win_hi).
    • CMD_UPDATE_UNIFORM_BUFFER (guarded by if (cbwin_wanted())) calls cbwin_invalidate_outside() so updates strictly inside [win_lo, win_hi) keep the shared base snapshot while outside or straddling writes invalidate it.
    • Zero overhead on non-cbwin GPUs (cbwin_retire_*): Keeps upstream gcopy_gen, guest_copy(), and ga_load_copy() untouched without seqlock ops; isolates wb_apply() / gcopy_retire() tracking behind #ifdef __ANDROID__ cbwin_retire_writing / cbwin_retire_gen guarded by relaxed cbwin_retire_armed (armed on cbwin_bind()), plus memory_order_relaxed on gmem_placed / gcopy_mode hot reads in ubo_direct, cbwin_wanted, cbwin_bind, vcache_lookup, guest_direct, dispatch_compute, and draw_.
  3. Compact CBP patch-buffer dedupe (CbpatchDedupe, stage_set(), draw_()):
    • Tracks cbpatch_active_bytes() (16 bytes when m[0] == 0, 16 + min(m[2], 64) * 16 bytes otherwise) instead of hashing/copying all 1040 bytes on up_fix retries or small windows while preserving 1040-byte VkDescriptorBufferInfo sizing.
  4. Tests (tools/test_cbwin_snap.c, tools/test_rt_load_upload.c, tools/test_android_runtime.ps1):
    • Adds standalone test_cbwin_snap.c unit tests (hit/miss, inside/outside/straddling/inverted invalidation, epoch invalidation, 4-way collision & in-place overwrite, cbpatch_active_bytes clamping, NULL/bounds checks).
    • Extends test_rt_load_upload.c Section 9 with cbwin_bind, CMD_UPDATE_UNIFORM_BUFFER in/out, CMD_COPY_BUFFER_TO_BUFFER, active cbwin_write_begin/end seqlock, 2-thread pthread seqlock contention stress (4,000 writer iterations, 0 torn reads), gcopy_retire() cache drop, #453 LARECOMP_CBWIN_SNAPSHOT=0 & AB("cbsnap")==1, Turnip short-circuit negative control (mock_mali_calls == 0), ok_non_cbwin_zero_churn verifying wb_apply() + gcopy_retire() perform zero seqlock writes when !cbwin_retire_armed, non-Mali negative control, LARECOMP_CB_WINDOW=0, and vk_exec_reset().

Rollback / Zero-Overhead Guarantee

  • LARECOMP_CB_WINDOW=0 reverts Mali to legacy full-UBO copy behavior (patched 0). LARECOMP_CBWIN_SNAPSHOT=0 preserves #453 uncached copy path.
  • On Turnip, placed-heap, and non-cbwin gcopy_mode builds, !gmem_placed && gcopy_mode / !cbwin_retire_armed short-circuit cleanly with 0 gcopy_gen / cbwin_retire_* seqlock churn (ok_non_cbwin_zero_churn, mock_mali_calls == 0).

Isolated On-Device Verification (Exact v0.4.8-beta Commit adbafd6)

Environment: Galaxy Tab S11 (SM-X730), Android 16, Mali-G925-Immortalis MC12 (vendor 0x13b5, device 0xd8300010, driver-id 9, v1.r49p1-03bet0.f8a98b506b89b21c80a508d70abfcb3f), BOTW 1.9.0, upstream base 2b71a10 (v0.4.8-beta, #508 included), commit adbafd6 (debug APK 95908e24580c), USB-powered (plugged: 2, no off-charger power claims made).

Verbatim [cbuf] telemetry proof (30 FPS, LARECOMP_LOG_CBUF_BYTES=1, 2s log intervals):

  • plateau (LARECOMP_CB_WINDOW=0): [cbuf] fixed 1152458 x 0.4 KB, dynamic 51371 x 17.6 KB; copied 912.4 MB; in place, patched 0 (912.4–912.7 MB/2s)
  • plateau (default ON): [cbuf] fixed 1151312 x 0.4 KB, dynamic 37916 x 1.4 KB; copied 83.7 MB; in place, patched 13320 (83.5–83.9 MB/2s, -90.8%)
  • zora-plaza (LARECOMP_CB_WINDOW=0): [cbuf] fixed 1010673 x 0.4 KB, dynamic 51549 x 17.8 KB; copied 962.2 MB; in place, patched 0 (961.5–962.2 MB/2s)
  • zora-plaza (default ON): [cbuf] fixed 1013700 x 0.4 KB, dynamic 38266 x 1.8 KB; copied 138.0 MB; in place, patched 13320 (136.6–140.6 MB/2s, -85.7%)

1. Official 30 FPS benchmark.c A/B (warmup_s=8, calm_s=3, window_s=10, 301 frames/scene)

Scene Mode FPS 1% low Frame p99 GPU p50 / p95 RT p50 / p95 Headroom p50
plateau Off (LARECOMP_CB_WINDOW=0) 30.00 29.48 33.89 ms 26.86 / 32.27 ms 17.51 / 20.75 ms 6.48 ms
plateau On (default) 30.00 29.44 33.91 ms 27.05 / 33.61 ms 17.87 / 20.62 ms 6.29 ms
zora-plaza Off (LARECOMP_CB_WINDOW=0) 30.00 29.49 33.88 ms 27.50 / 33.36 ms 18.56 / 21.34 ms 5.84 ms
zora-plaza On (default) 30.00 29.21 34.04 ms 27.31 / 33.12 ms 18.66 / 21.19 ms 6.02 ms

2. Uncapped 60 FPS Steady-Camera [vk] A/B (LARECOMP_FPS=60, 2s [vk] intervals with 0 skipped, compile 0 during S_WARMUP)

(Note: upstream benchmark.c:810 exits non-30 FPS targets at S_WINDOW, so these metrics come from steady [vk] 2s intervals before S_WINDOW exit; #508 dispatch chaining is active in both Off and On.)

Scene Mode Samples Median / Mean FPS Worst max ms Mean [vk] Upload / 2s Delta Upload
plateau Off 5 45.80 / 45.06 30 ms 1398.70 MB baseline
plateau On 6 45.60 / 45.33 33 ms 168.70 MB -87.9%
zora-plaza Off 7 43.50 / 43.70 29 ms 1456.60 MB baseline
zora-plaza On 7 43.40 / 43.33 30 ms 264.59 MB -81.8%

Verbatim [vk] sample (plateau):

  • Off: [vk] t=30.8 frame 1057 48.1 fps max 30ms level - (386733 draws, 0 skipped, 15646 passes, 1652 dispatches (631 chained), 787 flushes, wait 15ms, compile 0 (waited 0ms), fbcopy 970 (read 18340, breaks 0), upload 1491.5MB, ...)
  • On: [vk] t=30.9 frame 1061 48.1 fps max 25ms level - (386500 draws, 0 skipped, 15495 passes, 1530 dispatches (657 chained), 767 flushes, wait 16ms, compile 0 (waited 0ms), fbcopy 970 (read 18281, breaks 0), upload 176.6MB, ...)

Visual Check (SM-X730: Great Plateau Terrain & Zora's Domain Water with cbwin Active, adbafd6)

Great Plateau Zora's Domain Water & Rain
plateau cbwin zora cbwin

Additional Unit & Build Verification (adbafd6)

  • Host / WSL unit test: cc -std=c11 -Wall -Wextra -Werror -O2 tools/test_cbwin_snap.c -> PASS.
  • Host Python suites (NDK 30): test_vulkan_compat.py (16 PASS), test_vulkan_startup.py (33 PASS), test_shader_cache.py (17 PASS), test_soft_linear.py (23 + 3 + 203 PASS).
  • Android Bionic (SM-X730, tools/test_android_runtime.ps1): 8/8 PASS.
  • Compiler & Gradle: :app:assembleDebug --no-daemon -> BUILD SUCCESSFUL (13s); -U__ANDROID__ has 0 new diagnostics.

Tested vs Untested GPU Matrix

Target Status Notes
ARM Mali-G925 (SM-X730, r49p1) Tested on hardware Exact v0.4.8-beta commit adbafd6: 30 FPS A/B + [cbuf] telemetry + 60 FPS steady [vk] A/B + Plateau & Zora screenshots + 8/8 Bionic
Adreno / Turnip (gmem_placed) & Qualcomm stock Untested on physical hardware Short-circuits before vk_driver_is_mali() (mock_mali_calls == 0) & skips cbwin_retire_* (ok_non_cbwin_zero_churn); preserves #453 behavior
Mali < r49 / Desktop Untested on physical hardware Guarded by unit tests & LARECOMP_CB_WINDOW=0 / LARECOMP_CBWIN_SNAPSHOT=0; desktop retains #453 behavior

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 20, 2026
@Propinbo
Propinbo force-pushed the feat/cbwin-mali-snap branch from 5aec777 to 26c4a5a Compare November 20, 2026 11:01
@mamcjr

mamcjr commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks! Two things before merging: it turns the CB window on by default on Mali in copy mode, which the window never ran in before, with only mock tests; and the seqlock that bumps gcopy_gen on every wb_apply/gcopy_retire changes every copy-mode device (more copies, unmeasured). Could you add Mali in-game numbers (LARECOMP_LOG_CBUF_BYTES, fps, the water and terrain checked on 1.9.0), or ship the window default-off? The guest_copy_flush after the buffer-to-buffer copy is a real fix: that part could go in on its own. Also note it conflicts with #508 (now merged) in vk_exec_reset.

… support

Reconciles cbwin (mamcjr#453) with Android gcopy_mode GPUs (ARM Mali), cuts redundant per-draw full-UBO transient copies via a submission-scoped base snapshot cache, isolates async writeback/retire seqlock synchronization via cbwin-private cbwin_retire_{gen,writing,armed} (zero gcopy_gen churn on non-cbwin GPUs), and shrinks per-draw CBP patch uploads with CbpatchDedupe.
@Propinbo
Propinbo force-pushed the feat/cbwin-mali-snap branch from 26c4a5a to adbafd6 Compare November 20, 2026 14:18
Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 20, 2026
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased onto v0.4.8-beta (2b71a10 with #508 included) in adbafd6 (OPEN, MERGEABLE):

  1. Zero gcopy_gen overhead on non-cbwin devices (runtime/nvn/vulkan/exec.c): Left gcopy_gen, guest_copy(), and ga_load_copy() untouched with zero seqlock writes/reads outside cbwin. Async writeback/retire tracking in wb_apply() / gcopy_retire() now uses #ifdef __ANDROID__ cbwin_retire_{gen,writing,armed} with an early !cbwin_retire_armed skip (ok_non_cbwin_zero_churn test added), and hot gmem_placed/gcopy_mode reads use memory_order_relaxed (mock_mali_calls == 0 when placed).
  2. #508 conflict resolved: Integrated vk_exec_reset() cleanly with #508's gwin_mode / dchain resets.
  3. In-game Mali-G925 (SM-X730, 1.9.0, adbafd6, plugged: 2) numbers & visuals:
    • [cbuf] 30 FPS (LARECOMP_LOG_CBUF_BYTES=1): dynamic copies drop -90.8% on Great Plateau (912.4 -> 83.7 MB/2s, patched 13320) and -85.7% on Zora Plaza (962.2 -> 138.0 MB/2s, patched 13320).
    • 30 FPS benchmark.c matches Off parity (30.00 fps, Plateau & Zora); uncapped 60 FPS steady [vk] upload drops -87.9% (1398.70 -> 168.70 MB/2s) / -81.8% (1456.60 -> 264.59 MB/2s) on top of #508.
    • Checked Great Plateau terrain and Zora's Domain water/rain visually (screenshots in the updated PR body) + 8/8 Android Bionic tests pass.

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 20, 2026
@mamcjr
mamcjr merged commit c02beea into mamcjr:main Nov 20, 2026
@mamcjr

mamcjr commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks for the numbers and the screenshots: −86% to −91% dynamic copies with fps parity on the G925 answers my question, and the extra bookkeeping now stays off on devices without the window. On the POCO (Turnip, placed heap) the default is unchanged: GPU p50 22.5-22.6 ms, as in 0.4.8. Forced on with LARECOMP_CB_WINDOW=1, uploads drop from ~906 to ~91 MB per 2 s and the terrain, grass and water look the same, but GPU p50 goes from 22.6 to 24.0 ms, so keeping it off by default outside Mali copy mode is right. Merged, thanks!

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 21, 2026
HallanBR added a commit to HallanBR/BOTWRecompAndroid that referenced this pull request Nov 24, 2026
…mamcjr#457 merged; how each one landed

The status table and the section titles say merged. Sections 3-6 describe the code as it landed instead of the
proposal: mamcjr#454 queues the slots on every device (LARECOMP_SLOT_DRAIN=1 waits), with the 60 fps numbers of the merged
version; mamcjr#455 says its first version had no effect on Android (SDL lists only the current mode) and the merged one asks
Android through JNI, rechecked on 0.4.13 (40 fps on a 120 Hz screen); mamcjr#453 notes the window stays off by default on
Adreno and mamcjr#506 took the snapshot cache to Mali.
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