Skip to content

Keep shared texture aliases on their host while a storage write is pending - #2182

Closed
akratch wants to merge 1 commit into
seedds:mainfrom
akratch:contrib/shared-storage-alias-20261110
Closed

akratch wants to merge 1 commit into
seedds:mainfrom
akratch:contrib/shared-storage-alias-20261110

Conversation

@akratch

@akratch akratch commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

A GPU copy into one mip can disappear from both sampled texture aliases when the CPU writes a different layer before submission completes. The GPU-updated guest bytes are correct, but looking up the other alias uploads the old storage into a replacement host texture before the pending GPU store runs.

Identical storage and descriptions share an NvPkg; the pending-store guard tracked only the writing texture serial. Record the pending command-buffer generation on that shared package and check it before replacement through either alias. The existing serial guard remains. This adds no wait, cache flush or game-specific rule.

The synthetic regression extends copy_stream with a second serial over the same packaged RGBA8 array. It independently reads all four layers and three mips through both aliases against literal byte patterns, and checks the stored guest bytes. It verifies the GPU-copied mip, the separately CPU-written layer and untouched neighbors.

Validation on Apple M4 Max, macOS 27.2, Xcode 27.2, with Metal API validation:

  • Before the production fix: four failures in each encoder-flag setting. Both aliases read all 4,096 bytes of the copied mip incorrectly; premature replacement/reupload checks also fail.
  • Final fixture with the fix: 275/275 checks in each encoder-flag setting; 276/276 with fast wake disabled. These direct-backend flag variations do not independently prove replay-thread concurrency.
  • Six documentation tests pass. Focused LLVM 23.1.5 analysis of the backend and fixture has zero new findings; the baseline is unchanged. git diff --check passes.

The regression waits for the store's fence before the final lookup. GPU edge-snapshot completion and 1D-array storage coverage remain separate questions; this change does not claim to fix them or to explain any particular game's visual issue.

regress.py impact ab41d6a..HEAD reports Breath of the Wild, Pokémon Scarlet and Tears of the Kingdom as affected, Hades as unaffected, and 152 profiles without usage manifests as unknown. No exact-branch game run or iOS gameplay check was performed; validation here is the synthetic storage regression. No shader translation-chain change or performance claim.

A GPU copy through one serial can be lost from both sampled aliases when a
CPU write to another mip makes a second serial replace their shared NvPkg
before the first serial's storage write is submitted. Upload batches run
before the command buffer being encoded, so the replacement gets stale bytes.
Record the pending store generation on the package and defer replacement
through every alias until commit, using the existing upload queue ordering.

Extend the synthetic readback fixture with two serials, four layers and three
mips, checking every subresource through independent GPU readback. Unchanged
ab41 fails four of 274 checks. With the added explicit fence-completion
assertion, the fix passes 275 checks in both encoder flag settings under Metal
validation, and 276 with fast wake disabled. These flags
do not independently prove replay-thread concurrency. No game attribution or
performance improvement is claimed. Edge-snapshot synchronization and 1D-array
storage coverage in the broader upstream change remain separate open risks.

Document the finding and regenerate the symptom index. The shader translation
chain and player builds are unchanged.

Co-authored-by: Adam Kratch <70729875+akratch@users.noreply.github.com>
@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation labels Nov 10, 2026
seedds added a commit that referenced this pull request Nov 13, 2026
…2182)

A GPU write into a texture uploaded from its storage is stored into the storage on the GPU in the command buffer being encoded (gsb_store), and an upload batch runs before that command buffer, so a texture with a pending store keeps its host texture until the commit (gsb_held). The hold was keyed by the serial the store went through, but serials over the same storage and description share one NvPkg (pkg_shared): a look through another serial after a CPU write into another image replaced the shared host texture from the storage without the store. The store then wrote the old host texture and the storage, and page tracking does not see a GPU write, so the copied image stayed the storage's old bytes through every serial.

The hold is now on the bytes: each store notes its texture's storage range for the command buffer being encoded (64 ranges, then everything), and a packaged texture whose storage overlaps one keeps its host texture until the commit, through whichever object it is looked at. This replaces the serial set rather than adding a package field beside it. Open, and recorded in the reference entry: an object of another description over the same bytes (an sRGB twin) has a package of its own, which is now held as well but never learns of the store, so its next partial upload copies the stored image from its own host texture.

PR #2182 found the serial key and wrote the test. t_readback's copy_stream_alias stores through one of two objects over a four-layer, three-level RGBA8 array, writes another layer on the CPU, and reads every image of both back on the GPU, once with a second object of the same format and once with an sRGB one (the sRGB object's stored image skipped, as above). On main the same-format run fails 4 checks, all 4,096 bytes of the copied level lost through both objects.

Checked: t_readback 334/334 and 335/335 (DM_NVN_FAST_WAKE 1 and 0, Metal debug layer), test_docs, git diff --check. regress.py check on OCTOPATH TRAVELER II (the lightest NVN game here with a route; the packaged path is NVN's): PASS, 13980/13980 shaders, no MSL change, 59.8 fps and p99 17.87 ms before and after, no new red lines, on an M4 Pro. No game artifact is attributed to the finding.

Co-authored-by: akratch <70729875+akratch@users.noreply.github.com>
@seedds

seedds commented Nov 13, 2026

Copy link
Copy Markdown
Owner

Landed as 05b635c, with you as co-author. Thank you for finding this: your fixture reproduced on current main (4 of 275 checks failing, all 4,096 bytes of the copied mip lost through both aliases).

The landed fix keys the hold by storage range instead of adding store_gen beside the serial set. A pending store is a property of the bytes it writes, so gsb_hold/gsb_held now record the storage range of each store for the command buffer being encoded. Any packaged texture whose storage overlaps one keeps its host texture until the commit, whichever object it is looked up through. That covers the same serial and the shared package with one guard, and holds a package of another description over the same bytes too.

Your test is in t_readback.m, now run twice: with a second object of the same format, and with an sRGB object, which has its own package. The sRGB run shows a gap that is still open, recorded in the new nvn-objects.md entry. That package never learns of a store made through the other package, so its next partial upload copies the stored image from its own host texture. The sRGB run skips that image.

Checked: t_readback 334/334 and 335/335 (fast wake on and off, Metal debug layer), test_docs, and a full regress.py check on OCTOPATH TRAVELER II (PASS, 59.8 fps and p99 unchanged, no new red lines).

@seedds seedds closed this Nov 13, 2026
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…s has a pending storage write (seedds)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 pushed a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
seedds#2182, seedds#2225, seedds#2194, seedds#2273 (mkw/sets.tsv)

Each corrects wrong behaviour on code this game runs and brings a test that fails without it or a measurement; none changes the shader chain. Built on this Mac with 58 warnings as upstream; t_fstep passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…s has a pending storage write (seedds)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
seedds#2182, seedds#2225, seedds#2194, seedds#2273 (mkw/sets.tsv)

Each corrects wrong behaviour on code this game runs and brings a test that fails without it or a measurement; none changes the shader chain. Built on this Mac with 58 warnings as upstream; t_fstep passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
…s has a pending storage write (seedds)

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
kingbumi31 added a commit to kingbumi31/no-lag-kart that referenced this pull request Nov 15, 2026
seedds#2182, seedds#2225, seedds#2194, seedds#2273 (mkw/sets.tsv)

Each corrects wrong behaviour on code this game runs and brings a test that fails without it or a measurement; none changes the shader chain. Built on this Mac with 58 warnings as upstream; t_fstep passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JcmewFJDrDUooyBk9ygMAv
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