Skip to content

nvn/vulkan: upload CPU-backed write-back render targets before LOAD_OP_LOAD (#296, #438) - #461

Merged
mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:fix/rt-load-upload-296
Nov 17, 2026
Merged

mamcjr merged 1 commit into
mamcjr:mainfrom
Propinbo:fix/rt-load-upload-296

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #296 (also reported in #438) — resolves the underwater walking bug where Link falls through rivers and walks on the riverbed while water remains visually rendered above.

Following review feedback, this branch is narrowed strictly to the #296/#438 write-back render-target upload path (no nvn.h / objects.c / tegra.c changes):

  • Sync CPU-backed write-back render targets before LOAD_OP_LOAD (vk_tex_upload_for_load):
    • When a write-back color render target at mip level 0 is bound without a whole-target LOAD_OP_CLEAR (begin_pass(), or self-sampled in resolve_feedback() before renderer_mark_gpu_owned(t)), vk_tex_upload_for_load(t) calls upload_texture_impl(t, b, true).
    • First upload (!b->uploaded) uploads via s->upload before vk_submit_extra's existing barrier; previously sampled targets whose guest CPU pages changed while !gpu_owned (full_wgen / main hash_full / watch_full) re-upload synchronously on s->main after vk_end_pass() with scoped VkImageMemoryBarrier2 barriers, bypassing the checked_frame / 8-frame stagger / sparse sampling throttles.
    • Preserves snap_ok reuse on Turnip in-place feedback, invalidates snapshot caches only on non-in-place uploads, and removes stale NvnTexture * snapshot entries via vk_alias_texture_destroyed() / vk_alias_reset().
    • Adds undiscard() in destroy_image() and re-uploads in upload_texture_impl() so tex_discarded_bytes decrements accurately with zero drift.

Root Cause (#296 / #438)

  1. BOTW initializes temporary stack NVNtexture descriptors via nvnTextureInitialize without calling nvnTextureFinalize. Clipmap passes keep tile=6/7 pinned (owners > 0) while tile=0..5 drop to owners = 0 and can be evicted (texture_release).
  2. When moving across a clipmap grid boundary, BOTW recreates an evicted 64x64 NVN 0x2a water height clipmap tile first as a level-0 render target (LOAD_OP_LOAD) to draw a partial border strip before any pass samples it.
  3. Previously, begin_pass() uploaded initial CPU contents only for lv > 0 (vk_tex_upload_for_mips) while marking renderer_mark_gpu_owned(t), leaving the untouched interior zeroed in the fresh VkImage. wb_apply() at vk_end_pass() then wrote those zeros back over the guest clipmap tile, which cascading clipmap passes propagated into tile=6/7 until Havok heightfield queries returned hit = 0.

Testing & Validation (rebased onto main 26da3fa, commit 7077d56)

  • Bionic arm64 unit tests (SM-X730):
    • tools/test_rt_load_upload.c: 13/13 PASS (0 Vulkan sync hazards) — covers #296 partial LOAD_OP_LOAD write-back, full- vs partial-area LOAD_OP_CLEAR, intra-submission V1 -> V2 upload on s->main (with page tracking on/off), stagger bypass, open-pass resolve_feedback(), Turnip in-place + pointer-reuse cleanup, and undiscard() tex_discarded_bytes accounting.
    • Negative control (same test compiled against unpatched main 6113c61): 10/13 FAIL (rc=1).
    • All 7/7 suites in tools/test_android_runtime.ps1 (test_kernel_affinity, test_bc_shadow, test_buffer_copy, test_rt_load_upload, test_nvn_lifetime, test_object_cache, test_benchmark_trace): PASS (rc=0).
  • Host / build checks:
    • test_vulkan_compat.py (16 checks), test_vulkan_startup.py (33 checks), and ./gradlew :app:assembleDebug (JDK 17): PASS.
  • Physical Android gameplay (SM-X730, Mali-G925-Immortalis MC12, LARECOMP_NATIVE=1, Dueling Peaks Stable):
    • #461 (6f83821, identical runtime logic before rebase onto 26da3fa): 93 write-backs logged (64x64 f0x2a, 64x64 f0x25, 192x192 f0x6, 1x1 f0x25/2e, 85x20/120x23 f0x1), fbcopy 540 (breaks 0), 30.0 fps (max 35ms), 0 errors. (Controlled #296 underwater route telemetry earlier also verified 24/24 river raycasts hit=1 vs 17/17 hit=0 without the fix.)

@mamcjr

mamcjr commented Nov 16, 2026

Copy link
Copy Markdown
Owner

Thanks, this is a great find. The root cause matches what we see: at level 0, begin_pass marks a render target gpu_owned without uploading its CPU contents, and gpu_owned is never cleared. A texture object recreated over memory that already holds data starts as a zeroed image, and with LOAD_OP_LOAD plus a small strip draw the write-back puts zeros over the rest of the tile. It also explains why it shows up after 30-60 min and why only a restart fixes it.

I'd like to merge the #296 fix, but on its own. The PR is +1932 lines, and only about 200 of the ~440 runtime lines are the fix itself (the rest is the 1486-line mock test). Could you split it?

  1. This PR: vk_tex_upload_for_load, the for_rt_load path, the calls in begin_pass / resolve_feedback, and the snapshot-pointer cleanup.
  2. Separate PRs, each with its own evidence:
    • The vk_tex_guest_span gate on every upload (textures.c ~1087-1095). This is the change I'm most worried about: any texture whose computed span ends past the pool, or outside the guest window, is now not uploaded at all, which can turn textures black anywhere in the game. Logging and clamping would be safer than skipping, or the gate could apply only on the new path.
    • The nvnTextureGetStorageSize / builder_storage_size changes (objects.c). They change sizes the game sees, so they can move its pool layouts, and the Underwater Walking Bug #296 tile (64x64 RGBA16, 512-byte rows) doesn't need them.
    • The 128-byte pitch for 0x40 uploads plus the row-wise hashing.
    • The wb_vk_format_bpb table. The slot-overflow fix is real, but any format missing from the table silently stops write-back.
    • The tegra.c hardening.
  3. destroy_image: resetting discarded without lowering tex_discarded_bytes makes the low-RAM stats drift.

On testing: the A/B with raycasts is convincing, but it was done on a v0.4.3 diagnostic build. On this commit, the #296 route itself wasn't run, only fps and errors were checked. Once it's split and rebased onto current main (it merges cleanly onto 6113c61), I'll run the Little Twin River / Dueling Peaks route here on the Adreno 830 for 60+ min, and check #168 (leaves) and the terrain height tiles (8053dc8) against 0.4.5. A run of the same route on your Mali would be very welcome.

@Propinbo
Propinbo force-pushed the fix/rt-load-upload-296 branch from 0230d2f to 6f83821 Compare November 17, 2026 06:06
Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 17, 2026
…30 A/B + clock check, and 11-scene audit [skip ci]
@Propinbo Propinbo changed the title nvn/vulkan: sync unowned & CPU-mutated write-back RTs and unify linear RT row pitch (#296, #438) nvn/vulkan: upload CPU-backed write-back render targets before LOAD_OP_LOAD (#296, #438) Nov 17, 2026
@Propinbo

Propinbo commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed review. I have split and rebased fix/rt-load-upload-296 onto main (6113c61 / v0.4.5-beta, commit 6f83821) strictly around the #296/#438 write-back render-target upload fix:

  • Narrowed PR scope (6 files, no nvn.h / objects.c / tegra.c changes):
    • Kept only vk_tex_upload_for_load, upload_texture_impl(..., bool for_rt_load) (using full_wgen tracking and main's hash_full/fnv_sample/watch_full paths, plus synchronous s->main upload with scoped VkImageMemoryBarrier2 barriers after vk_end_pass() when already uploaded), the begin_pass() (LOAD_OP_CLEAR skip on full-target clears) and resolve_feedback() calls, snap_ok retention for Turnip in-place feedback, and vk_alias_texture_destroyed() / vk_alias_reset() snapshot pointer cleanup.
    • Addressed tex_discarded_bytes drift: Added undiscard() in both destroy_image() and the re-upload path of upload_texture_impl() so tex_discarded_bytes decrements accurately with zero drift.
    • Deferred unrelated hardening to separate future PRs: Removed vk_tex_guest_span, nvnTextureGetStorageSize/builder_storage_size, 0x40 128B row pitch/hash, wb_vk_format_bpb, and tegra.c overflow helpers from this PR.
  • Verification:
    • Bionic arm64 (SM-X730): tools/test_rt_load_upload.c passes 13/13 checks (0 Vulkan sync hazards), covering #296 partial LOAD_OP_LOAD write-back, full- vs partial-area LOAD_OP_CLEAR, intra-submission s->main upload (V1 -> V2 with page tracking on/off), stagger bypass, open-pass resolve_feedback, Turnip in-place + pointer-reuse cleanup, and undiscard() tex_discarded_bytes accounting. The same test suite compiled against unpatched main (6113c61) fails 10/13 checks. test_kernel_affinity, test_bc_shadow, test_buffer_copy, test_object_cache, test_vulkan_compat.py (16 checks), test_vulkan_startup.py (33 checks), and ./gradlew assembleDebug also pass.
    • In-game on SM-X730 (Mali-G925, LARECOMP_NATIVE=1, Dueling Peaks Stable route, runA-461only.log): Steady 30.0 fps (max 35ms), 93 write-backs logged (64x64 f0x25, 64x64 f0x2a, 192x192 f0x6, 1x1 f0x25/2e, 85x20/120x23 f0x1), fbcopy 540 (breaks 0), 0 errors. (Confirmed separately that #461 alone still leaves #436's NEAREST water artifact on Mali until #436 is also applied.)
    • (Side confirmation on v0.4.5-beta Dimensity 9400+): With LARECOMP_NATIVE=1, [native] logged counter 13000000 Hz: not the Switch's 19.2 MHz - its reads are scaled to it and sdk: 58 svc, 129 TLS, 43 counter reads rewritten (scaled to 19.2 MHz), running smoothly at 30.0 fps.

@mamcjr

mamcjr commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Thanks, this is exactly the split I asked for, and the extra pieces (the full-clear skip, snap_ok on Turnip, resetting uploaded/hashes in destroy_image) all hold up. It merges cleanly onto main and compiles. Before merging I'll run the Little Twin River / Dueling Peaks route here on the Adreno 830 for 60+ min and check #168 (leaves) and the terrain height tiles (8053dc8) against 0.4.5. One follow-up for later, not for this PR: texture-to-texture copies mark the destination gpu_owned without an upload either (exec.c ~8006).

@Propinbo
Propinbo force-pushed the fix/rt-load-upload-296 branch from 6f83821 to 7077d56 Compare November 17, 2026 14:02
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto current main (26da3fa, commit 7077d56) to resolve the conflict in runtime/nvn/vulkan/textures.c and tools/test_android_runtime.ps1 introduced when #315 merged — the write-back render-target logic itself is unchanged.

Re-verified after the rebase:

  • tools/test_android_runtime.ps1 on SM-X730: 7/7 PASS (test_kernel_affinity, test_bc_shadow, test_buffer_copy, test_rt_load_upload 13/13 with 0 Vulkan sync hazards, test_nvn_lifetime, test_object_cache, test_benchmark_trace).
  • test_vulkan_compat.py (16), test_vulkan_startup.py (33), and ./gradlew :app:assembleDebug (JDK 17): PASS.

Noted on the copy_texture gpu_owned path (~exec.c 8006) — I'll handle that in a separate follow-up PR later.

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! Tested here on the POCO F7 Ultra (Adreno 830, Turnip): a 16-scene benchmark tour across the map (16 loads) on main and on 7077d56 merged onto main gave the same results - every scene ran in both (one scene fails the harness check the same way in both), equal GPU times, no errors, normal images. Merging.

@mamcjr
mamcjr merged commit 689372b into mamcjr:main Nov 17, 2026
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
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.

Underwater Walking Bug

2 participants