Skip to content

widescreen (1.8.2 / 1.9.0): fix narrow-screen shadow frustum without mutating mAngle (#539) - #543

Open
Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:fix/narrow-shadow-539
Open

Propinbo wants to merge 1 commit into
mamcjr:mainfrom
Propinbo:fix/narrow-shadow-539

Conversation

@Propinbo

@Propinbo Propinbo commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #539 per review Option (c): instead of writing the narrowed Vert+ fovy back to mAngle (+0xA0), leave stored mAngle untouched (preserving zero FOV creep in self-referential camera blends) and substitute the effective 2 * atan(mFovyTan) at the view-frustum builder's virtual getFovy() read in BOTW 1.9.0.

Changes

  • runtime/hle/widescreen.c: Record the live sead::PerspectiveProjection vtable pointer in rt_persp_update() (cleared in widescreen_reset()) and add rt_persp_fovy(proj, orig_s0), returning the reshaped Vert+ fovy matching mFovyTan (or computing it from shape before the first matrix build) only for verified PerspectiveProjection instances under active narrow Vert+ rendering. Non-PerspectiveProjection subclasses (e.g. OrthoProjection), 1:1 shadow/reflection cameras, Hor- (LARECOMP_NARROW_HOR_MINUS=1), 16:9 (Original), and ultrawide (> 16:9) pass through orig_s0 unchanged.
  • runtime/native.c & tools/patches.py: Hook 0x7000f90da4 (fmov s10, s0, immediately after the virtual getFovy() call at 0x7000f90d98 inside f_7000f90cf4, called for shadow cascades and main culling) in PATCHES_190 and Native fixes[]. Both recompiled botwgame.so and Native mode use the same rt_persp_fovy(x0, s0) helper.
  • tools/test_fullscreen_narrow.py: Keeps upstream/main's self-referential lerp creep test intact and adds unit tests + a fovy negative control covering 16:10, 4:3, clamp consistency, g_persp_vtable / subclass isolation, 1:1 cameras, Hor-, Original, and ultrawide.

1.8.2 Scope Note

I only have BOTW 1.9.0 (cd57b23fa4bbad65803d9788c01821ee) locally, so PATCHES_182 / Native 182 is left untouched rather than guessing an unverified 1.8.2 address. To port the frustum hook to 1.8.2, run:

python -B tools/port_addrs.py <path/to/1.9.0/main> <path/to/1.8.2/main> code:0x7000f90da4

Verification (commit 473ff8a, rebased onto upstream/main 208746bb)

  • Host tests (tools/test_fullscreen_narrow.py --wsl): PASS (including --wide-test across -0.5..inf and all 11/11 --negative-control variants including fovy).
  • NSO & compiler checks: PATCHES_190[0x7000f90da4] verified against BOTW 1.9.0 main (exact word 0x1e20400a); NDK Clang -Wall -Wextra -Werror clean.
  • Device verification (Galaxy Tab S11 / SM-X730, Mali-G925, BOTW 1.9.0, isolated scratch):
    • Baseline (upstream/main unlocked 2560x1600): frame_01260 upper band top_10_30 = 105.78 (shadow clipping visible).
    • Isolating f_7000f90cf4 alone vs 4 other getFovy / +0x12f0 candidates: 130.73 (fixed), while the other 4 candidates individually stayed 102.36–111.70.
    • Final Vert+ 16:10 (aspect = unlocked): frame_01260 top_10_30 = 130.50, [widescreen] logs confirm fovy 40.0 -> 44.0 deg (mAngle preserved) across pause/inventory/zoom transitions with zero creep.
    • Hor- rollback (LARECOMP_NARROW_HOR_MINUS=1): 131.07; 16:9 Original (aspect = original): 134.79 (2560x1440).

Propinbo added a commit to Propinbo/BOTWRecompAndroid that referenced this pull request Nov 22, 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 for tracking down the #539 band. The cause makes sense: the readers of getFovy() disagree with mMatrix. But writing mAngle brings back the problem #474 avoided on purpose. Main's widescreen.c says "Leave stored fovy/mAngle (p[2]) untouched so game zoom/paraglider/cutscene blends reading fovy cannot creep".

FOV creep. Main's test did a 60-frame lerp that reads the current proj[fvi] back (cur = lerp_p[fvi]; next = cur + (target - cur) * 0.12). This PR replaces it with a lerp that keeps the value on the host (expected), so it never feeds the scaled getFovy() back. Only the exact passthrough setFovy(getFovy()) stays idempotent, through the exact p[2] == scaled_fovy match in vert_scaled_match. Any game code that does setFovy(lerp(getFovy(), target, k)) (a camera blend, the scope, the paraglider, a cutscene) writes a value near the scaled one but not equal to it. rt_persp_update then takes it as a new original and scales it again, so it climbs toward the 2.95 rad clamp.

Could you do one of these?

  • (a) Restore main's self-referential lerp test and make it pass.
  • (b) Show with LARECOMP_LOG_ASPECT or a setFovy hook that no 1.8.2/1.9.0 caller blends from getFovy() (scope, paraglider, Sheikah Slate camera, cutscenes, horse).
  • (c) Keep mAngle and fix the readers instead. The PR lists four call sites; patch them, or hook getFovy there.

Also, the matching is getting hard to reason about: three aspect fields, two caches, exact-float signature matches on (near, far, scaled_fovy), and a getter (rt_persp_aspect) that now changes state on a read. Option (c) would also let most of that go. On the POCO's 20:9 none of this runs, so I'll test it with LARECOMP_WIDE_TEST=0.9: scope in/out, paraglider, a cutscene, horse, comparing the FOV with LARECOMP_LOG_ASPECT=1.

@Propinbo
Propinbo force-pushed the fix/narrow-shadow-539 branch from 213c521 to d27739c Compare November 24, 2026 14:29
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased cleanly onto upstream/main (79ba1909) and pushed commit d27739c8 addressing your feedback and findings from a 2-agent adversarial review:

1. Option (c) check on BOTW 1.9.0 NSO

Static disassembly of BOTW 1.9.0 shows why restoring via getFovy() alone is incomplete:

  • sead::PerspectiveProjection::setFovy_ callers compute FOV from camera-owned state rather than reading back the projection struct on normal frames, except 0x700124f12c reads mAngle (+0xA0) directly without calling getFovy(). Leaving mAngle at the unscaled 16:9 angle fixes FOV-blend inputs only by leaving direct mAngle readers (including shadow/frustum paths behind #539) out of sync with the scaled projection matrix (p[3..5]), while our idempotency tracking (vert_scaled_match / vert_sig_match) already handles getFovy() -> setFovy_() safely under sinCosIdx rounding.

2. Simplification & review fixes (runtime/hle/widescreen.c)

  • Removed unused reported_aspect and prev_aspect fields from VertProj, leaving only scaled_aspect, and made rt_persp_aspect() strictly read-only across all branches.
  • Updated vert_trig_match() tan tolerance to quadratic 1e-4f * (1.0f + st * st) (sin/cos quotient sensitivity for wide FOVs on 0.5625 and near the 1.49f clamp), aligned vert_sig_match() p[2] check with 1e-6f, and deduplicated vert_recent_record() on orig_fovy (< 1e-6f) while refreshing scaled_aspect.
  • Updated vert_drop() to keep vert_used active while recent_vert_n > 0 (vert_count == 0 && recent_vert_n == 0) so tracked slots dropping first on resize out of narrow mode (0.9 -> 1.0 / 0.9 -> 1.25) no longer strand copied (idx < 0) or evicted projections, and added vert_sig_find() fallback in rt_persp_aspect() on found_idx >= 0 mismatch.

3. Verification

  • Host tests (d27739c8): python -B tools/test_fullscreen_narrow.py --wsl (main + --wide-test, memcmp read-only rt_persp_aspect() snapshot test, tracked-drops-first copy restore ordering, wide-FOV sin/cos quotient tests, and cross-copy 40° <- 50° fallback): PASS, plus 12/12 --negative-control variants (camera, ui, anchor, map, crop, band, crop_title, oob_guard, tall_ui, backdrop, mangle, copy_dedup).
  • Compiler & hygiene (d27739c8): Android NDK r30 Clang -std=c11 -Wall -Wextra -Werror -fsyntax-only PASS; 2-agent independent pre-PR review READY (0 CRITICAL / 0 MAJOR); workspace_check.py pr PASS (1 commit, 2 files, mergeable=YES).
  • Physical SM-X730 (Mali-G925, 16:10 Unlocked, prior commit 831499f before the final host-verified tolerance/resize-out refinements): Verified visually and via logs in Zora's Domain Plaza (2560x1440) across Scope x3, Jump, Cryonis Rune, Bow aim (ZR), Pause (+), Map (-), and downward camera pitch — #539 dark terrain shadow blotch eliminated and zero FOV creep observed across those exercised transitions.

@mamcjr

mamcjr commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thanks for the work on this. It still writes mAngle (p[2] = fovy_new in rt_persp_update), and main's self-referential lerp test (reading the current proj[fvi] back each step) isn't restored: added back, it fails, settling at 99.9 degrees instead of 54.8 at 16:10, and zooming in widens the view. The PR's own tests pass because none of them feeds the scaled value back.

The disassembly covers direct callers only: one of them is exactly a getFovy-then-setFovy copy from one projection to another (0x7000eefe68 -> 0x7000eefe70), and it can't rule out virtual +0x30 calls, inlined ldr s,[x,#0xa0] reads, camera code that keeps getFovy and blends later, or 1.8.2. Option (c) looks concrete now: keep mAngle and patch the reader you found (0x700124f12c, plus its 1.8.2 port) in tools/patches.py; that would also let most of the matching code go. Also: is_narrow_aspect accepts 1.0 to 1.777, so a native 1:1 or 4:3 projection inheriting a scaled FOV gets the screen's aspect, and vert_used never clears after a narrow phase.

@Propinbo
Propinbo force-pushed the fix/narrow-shadow-539 branch from d27739c to 473ff8a Compare November 26, 2026 15:54
@Propinbo Propinbo changed the title widescreen: sync Vert+ mAngle with scaled FOV to fix narrow shadow artifacts widescreen (1.9.0): fix narrow-screen shadow frustum without mutating mAngle (#539) Nov 26, 2026
@Propinbo

Copy link
Copy Markdown
Contributor Author

Reworked PR #543 (473ff8a, rebased onto upstream/main 208746bb) to Option (c) as suggested:

  1. mAngle untouched / main lerp test preserved: Restored upstream/main's rt_persp_update() behavior (leaving stored mAngle at +0xA0 un-mutated) and kept main's self-referential lerp test intact in tools/test_fullscreen_narrow.py, eliminating any possibility of camera blend creep.
  2. Minimal frustum-reader fix isolated on BOTW 1.9.0: Tested the 5 getFovy() / embedded +0x12f0 reader sites individually on Galaxy Tab S11 (BOTW 1.9.0, 2560x1600): only the view-frustum builder (0x7000f90cf4, called by shadow cascades and culling) resolved the clipped upper shadow band on frame_01260 (top_10_30 luma: 105.78 -> 130.50; Hor- rollback 131.07; 16:9 Original 134.79), while the other 4 candidates individually scored 102.36–111.70.
  3. Polymorphic subclass guard: Hooked 0x7000f90da4 (fmov s10, s0 right after the virtual getFovy() call at 0x7000f90d98) via rt_persp_fovy(proj, orig_s0) in both tools/patches.py (PATCHES_190) and runtime/native.c, using the live sead::PerspectiveProjection vtable pointer captured in rt_persp_update() so OrthoProjection and non-narrow/1:1/Hor-/16:9 cameras pass through orig_s0 untouched.
  4. 1.8.2 note: I do not have a local BOTW 1.8.2 NSO to verify its address/word, so PATCHES_182 is left untouched; the tools/port_addrs.py command against code:0x7000f90da4 is included in the PR description above.

@mamcjr

mamcjr commented Nov 27, 2026

Copy link
Copy Markdown
Owner

Thanks, this version is much better: mAngle isn't written any more, main's lerp test is back unchanged, and the hook at 0x7000f90da4 is safe (by then x0 is the projection again: ldr x8,[x20]; mov x0,x20 runs before the fmov).

Two things before merging:

  1. 1.8.2. The same function is f_7000e7d004 there, and the hook is at 0x7000e7d0b4: I checked the NSO word (0x1e20400a, fmov s10, s0), and the instructions around it match 1.9.0's. Could you add PATCHES_182[0x7000e7d0b4] (and the native.c entry if 1.8.2 has one there)? Otherwise [Bug] Large shadow artifacts appear in 16:10 aspect ratio when looking down (v0.4.9+) #539 stays on 1.8.2.
  2. Rebase: it now conflicts with main in tools/test_fullscreen_narrow.py (from Add an optional centered 16:9 world map on narrow screens #574, merged today).

Minor: g_persp_vtable keeps only the last vtable seen. If another class's matrix update also passes 0x7000eb4a44, the fix could switch on and off between frames for some cameras. A two- or three-entry set would rule that out.

After that I'll run it here with LARECOMP_WIDE_TEST=0.9 (scope, paraglider, a cutscene, horse) and merge.

@Propinbo
Propinbo force-pushed the fix/narrow-shadow-539 branch from 473ff8a to 6bccdbf Compare November 27, 2026 13:47
@Propinbo Propinbo changed the title widescreen (1.9.0): fix narrow-screen shadow frustum without mutating mAngle (#539) widescreen (1.8.2 / 1.9.0): fix narrow-screen shadow frustum without mutating mAngle (#539) Nov 27, 2026
@Propinbo

Copy link
Copy Markdown
Contributor Author

Rebased onto v0.5.1-beta (da30d45, commit 6bccdbf) and addressed all review points:

  1. BOTW 1.8.2 support (tools/patches.py, runtime/native.c): Added PATCHES_182[0x7000e7d0b4] (alongside PATCHES_190[0x7000f90da4], sharing _frustum_fovy_182 expecting 0x1e20400a) and registered {182, 0x7000e7d0b4, ..., fix_persp_fovy, "frustum fovy"} in runtime/native.c.
  2. Multi-vtable set (runtime/hle/widescreen.c): Replaced the single g_persp_vtable pointer with a lock-free 4-entry g_persp_vtables[] set cleared on widescreen_reset(), and placed the widescreen_allowed() && shape_vert_plus(shape) guard before guest reads/atomic vtable lookups so inactive/16:9/20:9/Hor- modes exit with zero overhead.
  3. Rebase & tests (tools/test_fullscreen_narrow.py): Kept #574's center_world_map and center_map negative control intact alongside Section 5 rt_persp_fovy coverage (including multi-subclass non-clobbering, both 1.49f clamp paths, NAN/16:9 guards, widescreen_reset(), and --negative-control fovy).

Verified on 6bccdbf:

  • python -B tools/test_fullscreen_narrow.py --wsl (main, --wide-test -0.5..inf, and 12/12 --negative-control variants): PASS.
  • patches.py smoke check (PATCHES_182[0x7000e7d0b4] & PATCHES_190[0x7000f90da4] on 0x1e20400a), NDK Clang -std=c11 -Wall -Wextra -Werror syntax check (widescreen.c, native.c), workspace_check.py pr: PASS, and 2-agent independent review: READY.

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.

[Bug] Large shadow artifacts appear in 16:10 aspect ratio when looking down (v0.4.9+)

2 participants