Skip to content

nvn/submit: commit a submission's first segment after 8 draws even while earlier work runs (OoT −0.85 ms a frame on an M1) - #2526

Closed
AdyaGMD wants to merge 1 commit into
seedds:mainfrom
AdyaGMD:early-first-segment
Closed

AdyaGMD wants to merge 1 commit into
seedds:mainfrom
AdyaGMD:early-first-segment

Conversation

@AdyaGMD

@AdyaGMD AdyaGMD commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

What

A submission's first segment is cut after DM_NVN_CUT_FIRST_N draws (8) and committed at once, even while earlier segments are still running (DM_NVN_CUT_FIRST, on by default; =0 off, ab for the DM_AB phases).

Why

Ocarina of Time holds one frame in flight: its main thread waits on the fence of the frame it just submitted (wait ledger: SyncWait on the newest fence 0.72 times a frame, 17.4 ms). At each frame boundary the GPU has nothing queued until the next frame's first segment is committed.

sub_cut's busy rule commits a segment only once no earlier one is in flight. That held the first segment back until the previous frame's last ran out, and then until the next cut point. A counter-free Metal System Trace on an M1 showed 0.4–3 ms of GPU idle at every frame boundary. Committed early, the first segment runs while the previous frame's last work finishes.

Performance

Ocarina of Time (0400486024DEC000), Kokiri Forest, MacBook M1 (8-core GPU), fullscreen, in a tree with other local changes. Those include the image-heap alias flush once a submission (companion PR), which lets command buffers overlap at all.

With (frame p50) Without
ab:2,3/4, 300-frame phases 36.33 / 36.41 / 36.37 ms 37.19 / 37.33 / 37.41 ms
Phases swapped, ab:0/2, 450-frame phases 36.48 / 36.53 / 36.51 ms 37.57 / 37.38 / 37.35 ms

About −0.85 ms a frame, lower in every phase both ways.

An earlier test of committing every segment at once gained nothing while the alias flush on every barrier serialized the GPU. So this gain probably depends on the companion change.

Switches (in docs/env_switches.tsv)

  • DM_NVN_CUT_FIRST=0|1|ab: on by default.
  • DM_NVN_CUT_FIRST_N=<draws>: the first segment's size (default 8).

Not run

  • This branch's host in game (it was measured in another tree).
  • Any test suite.
  • iPhone.
  • Other games.

🤖 Generated with Claude Code

…ven while earlier work runs: Ocarina of Time -0.85 ms a frame on an M1

Ocarina of Time holds one frame in flight: its main thread waits on the fence of the frame it just submitted (the wait ledger: SyncWait on the newest fence 0.72 times a frame,
17.4 ms), so at each frame boundary the GPU has nothing queued until the next frame's first segment is committed. sub_cut's busy rule commits a segment only once no earlier one
is in flight, which held that first segment back until the previous frame's last ran out, and then until the next cut point: a counter-free Metal System Trace on an M1 showed
0.4-3 ms of GPU idle there every frame. Now a submission's first segment is cut after DM_NVN_CUT_FIRST_N draws (8) and committed at once (DM_NVN_CUT_FIRST, on by default;
=0 off, ab for the DM_AB phases), so it runs while the previous frame's last work finishes.

Measured (0400486024DEC000, Kokiri Forest, MacBook M1 8-core GPU, fullscreen, in a tree with other local changes, among them the image-heap alias flush once a submission of the
companion PR, which lets command buffers overlap at all): DM_NVN_CUT_FIRST=ab:2,3/4 in 300-frame phases, frame p50 36.33 / 36.41 / 36.37 ms with it against 37.19 / 37.33 /
37.41 without; with the phases swapped (ab:0/2, 450-frame phases) 36.48 / 36.53 / 36.51 against 37.57 / 37.38 / 37.35: -0.85 ms, lower in every phase both ways. Without
the overlap the alias flush on every barrier forbids, an earlier test of committing every segment at once gained nothing, so the gain here likely needs that change.

Not run: this branch's host in game, any test suite, the iPhone, other games.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@devMlGUE

Copy link
Copy Markdown
Contributor

Tried this on an M1 Pro (16-core GPU, macOS 27.2.1) with OoT, in a tree with your other open PRs merged on top of main (no 2525), MetalFX on, dynamic resolution off, mesh-as-compute off. Same-process DM_AB alternating, 240-frame phases, standing still at Lon Lon Ranch for ~7 min.

On this GPU I don't see a frame-time change: frame p50 stayed at 33.3 ms with it on and off, at a 30 and a 60 fps target. The M1 Pro has headroom here (GPU ~24-27 ms), so there's no idle gap at frame boundaries to win back the way there is on your 8-core M1.

One thing I did notice: the GPU time counter reads higher with it on, every cycle:

GPU p50
CUT_FIRST off 24.3 ms
CUT_FIRST on 26.6 ms

That's +2.4 ms in the paired 60 fps run and about +1.9 ms in a 9-phase leave-one-out run at 30 fps. It might just be segments overlapping and being counted twice rather than real extra work, since frame pacing didn't change at all, but it'll make GPU-time comparisons look worse with this on.

khalilgharbaoui pushed a commit to khalilgharbaoui/nxnative-ultimate that referenced this pull request Nov 15, 2026
…ven while earlier work runs: Ocarina of Time -0.85 ms a frame on an M1

Hand port of seedds/nxnative PR seedds#2526 (commit efa80a1, Adya). Main split
sub_cut into sub_cut_ready since the PR was written, so the first-segment
draw count goes into sub_cut_ready and the busy rule in sub_cut skips the
first segment, as in the PR.

nxnative-ultimate: off by default here (DM_NVN_CUT_FIRST=1 turns it on); the
PR has it on. No change measured on an M1 Pro.
Aznracer pushed a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 17, 2026
What the 2026-11-16 search tried in in-run A/B and did not keep (open pull
requests seedds#2526, seedds#2677 and seedds#2771 measured on this game, DM_NVN_ARGS_LEAN,
DM_NVN_REC_EAGER, DM_NVN_PASS_FENCE), and what shader_row_bounds measured on
its light loops. Docs only.
atinseau added a commit to atinseau/nxnative that referenced this pull request Nov 17, 2026
What the 2026-11-16 search tried in in-run A/B and did not keep (open pull
requests seedds#2526, seedds#2677 and seedds#2771 measured on this game, DM_NVN_ARGS_LEAN,
DM_NVN_REC_EAGER, DM_NVN_PASS_FENCE), and what shader_row_bounds measured on
its light loops. Docs only.
@gnavarro24

Copy link
Copy Markdown
Contributor

Hi! This branch is based on a main from before commit c70c242 ("ci: repair host and tool test failures on main"). Most of the failing checks here (Host unit tests, Tool tests, clang-tidy, Build check) come from that older base, not from this change.

To pick up the fix, merge the current main into this branch and push:

git fetch origin
git merge origin/main
git push

If the merge has conflicts, ask here if you want help with them.

@dpadr

dpadr commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Tested on an M2 MacBook Air (16 GB), Ocarina of Time v131072, on a local test build that carries this PR. Same method as above, Castle Town only: A = default, B = DM_NVN_CUT_BUSY=0.

  • All stills: A 20.06 ms, B 20.17 ms, so B − A = +0.11 ms against a same-arm spread of 0.25 ms. Frame time is the same within 0.1 ms.
  • GPU cycles a frame: 30.1 against 30.0. Energy: 110 against 111 mJ.
  • In-span GPU idle ran 1.1–2.1 ms in both arms.

On this machine at cap 60, where the GPU is the bottleneck, I see no measurable effect either way. That fits the PR's numbers, which come from an M1 where submission was more CPU-bound.

One caveat: one of the six runs (A3) hung on an unrelated, known freeze and is left out, so A has two runs.

@seedds

seedds commented Nov 24, 2026

Copy link
Copy Markdown
Owner

Thank you for the Metal System Trace and M1 measurements (and thanks to @devMlGUE and @dpadr for the follow-up tests on M1 Pro and M2 Air).

We are closing this PR rather than rebasing it onto current main:

  • No isolated gain on main: The 0.85 ms M1 frame-time reduction was measured alongside companion PR nvn/barrier: image heaps take the alias flush once a submission and after memory writes (OoT −1.3 ms a frame on an M1) #2525, which was closed because skipping attachment alias flushes caused missing foliage on M3 Max and restoring them removed the M1 gain. Follow-up runs without nvn/barrier: image heaps take the alias flush once a submission and after memory writes (OoT −1.3 ms a frame on an M1) #2525 on M1 Pro and M2 Air did not reproduce a frame-time gain (and M1 Pro reported higher GPU times).
  • Superseded by current main: Commit 194da5fb replaced asynchronous commit-feedback polling (0.17–1.2 ms lag) with live GPU-progress tracking on a shared event (~0.04 ms lag), added encode-thread segment holding for submission merging, and generalized DM_NVN_CUT_BUSY to accept an integer in-flight cap (1 by default, 2 for bounded overlap, 0 for unbounded).
  • Stall and contract risks: Because segment index 0 resets on every NVN submission rather than once per frame (22–30 submissions per frame in Ocarina of Time), bypassing the busy check queues command buffers behind running GPU work and flushes held submissions when the encode thread is active, working against the Mac and iPhone submission-queue stall protection. It also still cuts after 8 draws when DM_NVN_CUT=0.

Checked: Reviewed this PR against current main's NVN submission and encode-thread merging paths and the M1, M1 Pro, and M2 Air test reports (no code changes made).

Still open: We have not measured an 8-core M1 on current main directly. If an isolated run on current main still shows GPU idle gaps at frame boundaries on an 8-core M1, please share numbers comparing DM_NVN_CUT_BUSY=2 or a smaller DM_NVN_CUT (and if needed, an idle-only smaller first segment that still respects DM_NVN_CUT=0 and the in-flight limit would be a cleaner follow-up).

@seedds seedds closed this Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants