Skip to content

Texture ID views follow the serials that changed: a pool walk no longer resolves every ID again after each serial table change - #1069

Closed
parsifal295 wants to merge 1 commit into
seedds:mainfrom
parsifal295:texid-serial-notes
Closed

parsifal295 wants to merge 1 commit into
seedds:mainfrom
parsifal295:texid-serial-notes

Conversation

@parsifal295

Copy link
Copy Markdown
Contributor

What and why

In Fire Emblem Fortune's Weave's battle map, the submit thread spent about 14 % of its samples in pool_gpu → tex_by_id → tex_mtl / tex_get. The texture pool table was rebuilt every frame at about 1.9 ms per build.

The cause is how texture ID views were cached. A view stayed valid only while g_ser_gen did not change, and every serial table change bumped it. The game changes a few serials every frame (the per-stage program ring in bind_stages does), so the next lookup sent every registered ID down the slow path. pool_gpu walks all of them, about 5,000 here. Packaged IDs also took the slow path again after any guest write anywhere.

What changes:

  1. Each change records its serial. A change to one serial's object bumps g_ser_gen through ser_note, which records the serial.
  2. Views are valid for an epoch. ID views are now valid for an epoch (treg_ep), and ser_sync invalidates only the IDs registered with a noted serial (g_treg_serial).
  3. Anything unusual falls back to today's behaviour. A new epoch re-checks every ID, as every bump did before. It starts on any of:
    • a bump without a note (the sharing table, upgrades, a dropped upload);
    • more than 64 notes;
    • any sparse mapping.
  4. Packaged IDs keep their view when untouched. A packaged ID whose pages were not written since its upload stamp keeps its view. This is the same test tex_mtl_ makes.
  5. Cached resource IDs are reused. tex_fast_rid returns the cached resource ID of a view tex_by_id kept.

Other users of g_ser_gen are unchanged.

An independent review of the first version found two bugs, both fixed here:

  • Stale first lookup. The fast paths compared view_ser with treg_ep() in one expression. The first lookup after a noted change could read view_ser before the sync cleared it, so the epoch is now read first.
  • Unlocked lookup. fz_p2_draw looked an ID up without g_rmu. The lookup can now run ser_sync, so it takes the lock.

The reference entry is Texture ID views revalidated only for the serials that changed.

Checked

  • Tests: make -C dm/gfx/tests GAME=04007EF01F8AE000 run passes on this branch (main 6a16b7d + this change), including t_cpu_tex. test_docs passes, and docs_symptoms.py was run.
  • Game: Fire Emblem Fortune's Weave (04007EF01F8AE000, v131072), battle map save, headless, 975 draws a frame.
    • Measured on main 4129936's build (the chain has changed since).
    • Pool build went from 1.9 to 1.2 ms. Submit instructions per draw went from 52.5k to 42.8k.
    • Submit CPU went from about 8.2 to 7.4 ms a frame. Other apps were loading the machine during the runs, so this number is noisy; the instruction counts are steadier.
    • With the review fixes, instructions per draw and attachment traffic are unchanged.
    • Screenshots match apart from animation.
  • Devices and OS: MacBook Pro M1 Pro, macOS 27.2.
  • regress.py: not run (see above). regress.py impact lists every NVN game.
  • Not checked:
    • Monster Hunter Rise, which uses sparse textures. Those take the fallback path from the first mapping.
    • Games with CPU-written render targets, such as Tears of the Kingdom.

Docs

  • The finding is in its game note or its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • A chain tool changed: not applicable
  • No game files, keys or data extracted from a game in the change or the attachments

…er resolves every ID again after each serial table change
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 3, 2026
seedds added a commit that referenced this pull request Nov 3, 2026
… few serial changes a frame no longer send every registered ID down the slow path (#1069)

A change to one serial's object bumps g_ser_gen through ser_note, which
records the serial; ID views are valid for an epoch, and ser_sync clears only
the views of IDs registered with a noted serial. A bump without a note, more
than 64 notes or any sparse mapping starts a new epoch, as every bump did
before. A packaged ID whose pages were not written since its upload keeps its
view. Fire Emblem: Fortune's Weave's battle map: the texture pool table no
longer rebuilt every frame (1.9 -> 1.2 ms a build).

Co-authored-by: parsifal295 <130349765+parsifal295@users.noreply.github.com>
@seedds

seedds commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Landed on main as 6fc3421, with you credited as co-author. I audited the lock coverage: every caller of tex_by_id / tex_fast / tex_fast_rid already holds g_rmu, except the fz_p2_draw one you added. g_ser_gen is atomic, and each note is recorded under the same lock as its bump, so a bare bump always forces a new epoch. The gfx suite passes, and regress.py check on BotW and Wonder passes (no new red lines, no changed MSL). Thanks!

@seedds seedds closed this Nov 3, 2026
Know-It-All-Brother pushed a commit to Know-It-All-Brother/nxnative that referenced this pull request Nov 9, 2026
… few serial changes a frame no longer send every registered ID down the slow path (seedds#1069)

A change to one serial's object bumps g_ser_gen through ser_note, which
records the serial; ID views are valid for an epoch, and ser_sync clears only
the views of IDs registered with a noted serial. A bump without a note, more
than 64 notes or any sparse mapping starts a new epoch, as every bump did
before. A packaged ID whose pages were not written since its upload keeps its
view. Fire Emblem: Fortune's Weave's battle map: the texture pool table no
longer rebuilt every frame (1.9 -> 1.2 ms a build).

Co-authored-by: parsifal295 <130349765+parsifal295@users.noreply.github.com>
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.

2 participants