Skip to content

SyncWait wakes on the GPU's own signal of its fence (DM_NVN_FAST_WAKE); segments reach the encode thread at once - #158

Closed
v0lumehi wants to merge 2 commits into
seedds:mainfrom
v0lumehi:fast-wake
Closed

v0lumehi wants to merge 2 commits into
seedds:mainfrom
v0lumehi:fast-wake

Conversation

@v0lumehi

@v0lumehi v0lumehi commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Part of #142: Breath of the Wild performance on iPhone and Mac (60 → 120 fps, then 4K). Start there to see how the PRs fit together and the merge order.

Builds on #157, which builds on #87: this PR contains their commits; only the last two commits are new here (compare). It needs #157 because the fast wake uses its wait ledger (mtl_wait_at and its wait sites) and is measured with it.

Update: #87 and #157 landed on main as a188613 (snapshots off by default there), and this branch now builds on main: it holds only its two own commits.

Problem

Breath of the Wild blocks in one SyncWait per frame, on the fence of its newest submission. With #157's wait ledger (DM_NVN_WAITS=1) on an M2 Max, that wait woke 0.29 ms after the GPU had finished, because the waiter slept until the commit feedback handler of the fence's submission had run, and the handler comes 0.26–0.28 ms after the GPU end. That time sits on the critical path of every frame.

With the encode thread on, a segmented submission's segments (DM_NVN_CUT) reached the encode thread only when their block of 256 records filled, so the GPU started the frame's first segments later than the submit thread had finished them.

Change

  1. Fast wake (DM_NVN_FAST_WAKE, on by default; =0 turns it off). The game's SyncWait (newest fence, older fence, acquire sync) now waits on g_ev, the event the queue signals after each submission, instead of on the feedback handler.
    • On waking, it makes the CPU-side writes of every submission up to the fence that no handler has made yet (post_early): events, samples-passed reports and counter reports.
    • The writes are kept in a ring of POST_RING records. Each record runs once, by whichever comes first, the handler or the waiter (post_claim, post_run).
    • A counter report of the fence's last segment takes the wake time as that segment's end, and the later of its commit and the previous segment's end as its start.
    • Breath of the Wild writes 5 timestamp reports per frame and nothing else at the GPU end; the ledger now counts these writes. Its frame-interval controller and its dynamic resolution read them right after the frame's wait. That is why simply letting the wait run ahead of them broke the picture, and why the waiter makes the writes itself.
  2. Segment publish. With the encode thread on, a segment's commit record is handed to the encode thread at once (rec_publish in sub_cut), not when its block of records fills.

Measurements

Reproduce

  • DM_NVN_WAITS=1: the [waits] line every 300 frames gives newest-fence SyncWait wakes … ms after the GPU end (feedback handler at … ms) and the CPU-side writes at the GPU end per frame.
  • DM_NVN_FAST_WAKE=0 for the comparison. The log says at the first wait whether SyncWaits wake on the GPU's signal.
  • DM_NVN_CUT=<n> for the segment size.

Risk

  • Fast wake applies to every NVN game, with and without the encode thread. The writes stay the same and are made once; only the thread that makes them changes. A submission whose writes never ran when its ring slot comes round again is a fatal error (post ring: the writes of fence N never ran), so a lost write cannot pass silently.
  • When the event wait times out, a wait with a timeout of 1 ms or more goes on as before, on the feedback handler; a shorter one returns its result.
  • The segment publish only changes when the encode thread sees a segment.

Tested

On this branch:

Not tested on this branch: the iPhone (the numbers above are from the previous base), other games, and DM_NVN_FAST_WAKE=0 in play.

…FAST_WAKE, on by default) and runs the submission's CPU-side writes itself when its feedback handler has not yet

The waiter used to sleep until the commit feedback handler of the fence's submission had run, which comes 0.26-0.28 ms after the GPU end on an M2 Max. It now waits on g_ev, which the queue signals after each submission, and then makes the writes of every submission up to the fence that no handler has made yet (post_early: events, samples-passed and counter reports; a ring of POST_RING records, each run once, by whichever comes first). A counter report of the fence's last segment takes the wake time as the segment's end and the later of its commit and the previous segment's end as its start (segment timestamps). Breath of the Wild writes 5 timestamp reports a frame and nothing else at the GPU end (the wait ledger now counts them): the frame-interval controller and dynamic resolution read them right after the frame's wait, which is why letting that wait lag broke the picture. Mac, Breath of the Wild, encode thread and snapshots, DM_FPS_TARGET=120, load 4.8-5.5 (not a clean measurement): the newest-fence SyncWait wakes 0.08 ms after the GPU end instead of 0.29, its blocked time 5.8 -> 5.0-5.3 ms a frame, frame time p50 9.35 -> 8.4-8.7 ms; the picture is right. t_nvn passes with and without DM_NVN_ENC_THREAD=1 DM_PRESENT_SNAP=1. t_fuse_check waits up to 2 s for its comparison, which runs in the commit feedback handler that a fast-woken SyncWait no longer waits for (a diagnostic, DM_NVN_FUSE_CHECK)
…rd is handed to the encode thread at once (rec_publish), not when its block of 256 records fills

Mac, Breath of the Wild, encode thread and snapshots, DM_FPS_TARGET=120, load 4-6 (direction only): the frame's wait on its newest fence (the critical path's tail) DM_NVN_CUT=0 12.5 ms a frame (frame time p50 16-17 ms), 64 (default) 5.6 ms (p50 9.2), 16 5.35 ms (p50 8.9); 64 stays the default until the iPhone has measured smaller segments
v0lumehi added a commit to v0lumehi/nxnative that referenced this pull request Oct 26, 2026
…platform mac along a fixed route and reports frame-time percentiles

A route is a line of the profile's new bench.tsv: the scripted input (DM_KEYS, seconds from the start) and the measured window, from when the scene is loaded. Each run starts from a copy of the same save and reads the host's per-frame rows (DM_NVN_TSV): frame time (start to start) and time on screen at p50 / p95 / p99 / max, frames longer than 1.5 frame times, dropped frames, GPU time p50 / p95, the game's speed against real time where the frame step logs it, and the [RED] lines. Variants (environment switches, or another host, shader libraries, rewritten modules or pipeline archive) run in turn, repeated, and are reported as medians with their range in results.md / results.json beside each run's log, frames.tsv and two screenshots at the same moments. Breath of the Wild has three routes: a save in Zora's Domain with the camera turning and Link walking (zora-pan), the same standing still, and a probe for the load time.

Used for the baseline in issue seedds#159: main + seedds#76 + seedds#158 on an M2 Max at 900p and a 120 fps target, three variants, two runs each. docs/development.md ("A benchmark on the Mac") and dm/games/README.md describe it; dm/tools/tests/test_bench.py checks the window, the percentiles (nearest rank) and that every bench.tsv parses.
seedds added a commit that referenced this pull request Oct 26, 2026
Integrate fast shared-event wakeups, per-segment publication, array-slice
pass merging, and independent compute dispatch hoisting from the PRs.
Serialize CPU completion effects before publishing completed fences and
validate recycled post records. Preserve submission order for early waits.

Guard scheduling against attachment aliases, first-draw and bindless
reads, indirect ranges, stencil writes, and explicit barriers. Add direct
Metal regression coverage and a gated CPU completion race test with TSAN.

Record final BotW and Metroid Dread gameplay validation with optimizations
on and off. BotW retains fewer render passes, but the measured 60 Hz M4
Pro runs do not establish a timing speedup. Keep the original higher-load
and iOS performance claims explicitly unverified.

Co-authored-by: v0lumehi <[email protected]>
@seedds

seedds commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Integrated into main in 6062518, co-authored with @v0lumehi. The shared-event wake and immediate segment publication are retained.

The review confirmed a CPU completion race: claiming a post record did not mean its event/report writes had finished, so another waiter could publish completion too early. The fix serializes those effects, completes earlier submissions in order, and validates the fence generation when a delayed callback encounters a recycled ring slot. Fence/completion counters are atomic.

Validation includes a gated competing-waiter/recycled-slot regression (8 CPU checks, also passing under ThreadSanitizer), 37 production-path Metal ordering checks in direct encoding, deferred encoding, and all optimizations disabled, plus the full graphics suite. After integration with current main, the archive tests (52 bundle / 57 archive checks), profiler tests (28 checks), documentation checks, both Mac hosts, and all 133 iOS source compiles pass. Optional shader-fixture checks without local fixtures were skipped.

Gameplay coverage includes BotW outdoors and Metroid Dread's playable cave room, with optimizations on and off. Fourteen benchmark runs of the fixed implementation, before rebasing onto current main, completed without a red line or crash. The repeated M4 Pro / 60 Hz tests do not establish a timing speedup; blocking SyncWaits were rare. They do not reproduce the original M2 Max / 120 fps workload or validate iPhone runtime performance. Four further on/off execution checks after integrating current main reached frame 5,000 in BotW and 4,400 in Dread, all without errors. Display-link stalls and incomplete screenshots exclude those extra checks from the gameplay/timing evidence.

Full measurements and limitations are recorded in the BotW note and Dread note. Closing because the changes and review fixes are now incorporated into main.

@seedds seedds closed this Oct 26, 2026
@v0lumehi
v0lumehi deleted the fast-wake branch October 26, 2026 09:29
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
Integrate fast shared-event wakeups, per-segment publication, array-slice
pass merging, and independent compute dispatch hoisting from the PRs.
Serialize CPU completion effects before publishing completed fences and
validate recycled post records. Preserve submission order for early waits.

Guard scheduling against attachment aliases, first-draw and bindless
reads, indirect ranges, stencil writes, and explicit barriers. Add direct
Metal regression coverage and a gated CPU completion race test with TSAN.

Record final BotW and Metroid Dread gameplay validation with optimizations
on and off. BotW retains fewer render passes, but the measured 60 Hz M4
Pro runs do not establish a timing speedup. Keep the original higher-load
and iOS performance claims explicitly unverified.

Co-authored-by: v0lumehi <[email protected]>
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 11, 2026
…ings (equivalents of Docked and Handheld preset)' (seedds#158) from iH8Ecchi/BananaNative:fefw-pr-res into android

Reviewed-on: https://git.eden-emu.dev/moceure/BananaNative/pulls/158
ltrdpper pushed a commit to ltrdpper/BananaNative that referenced this pull request Nov 12, 2026
…, seedds#164, seedds#165, seedds#109, seedds#166-seedds#168)

Conflicts: the launcher profile columns 18 (fps_steps) and 19 (mesh_vegetation_cut) are taken,
so variants move to column 20; GAME_SETTINGS keeps hold60, full_vegetation and aspect;
docs/agents/game-patches.md regenerated. moceure#39 (whose old commit 0749171 could not be fetched
from the server) touches no file of this PR but env_switches.tsv, which merges cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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