Textures the GPU writes and the title reads on the CPU must go back to title memory (water simulation; boats sink otherwise) #46

Open
opened 2026-11-26 11:17:54 +00:00 by 64bitMango · 14 comments
64bitMango commented 2026-11-26 11:17:54 +00:00 (Migrated from github.com)
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by 64bitMango on 2026-11-26T11:17:54Z.


From the Windows forks of the same build (1.0.2.1); worth planning for in the Metal backend's texture model.

What happens. The ocean's height and displacement maps are textures that compute passes write and the title's physics samples on the CPU. If the layer never copies them back into title memory, the CPU reads zeros: boats sink, the sea plane goes under, and the first boat mission fails. With the read-back, boats stay up through whole runs (avatarmanz/VIctory-7900XTX docs/findings.md; fix by Geckomacabre, fxceless/victory-fork pull request #999, VICTORY_TEXTURE_BACK, on by default in avatarmanz's tree).

Why it matters more natively. Even with title memory mapped straight into MTLBuffers, textures live in Metal's own (twiddled) layouts, so a GPU write to a texture is not visible in the title's linear/tiled bytes. Textures the title reads back need an explicit copy and re-tiling into title memory after the GPU writes them, at least for those it is known to read (found by a CPU read fault on the texture's pages, as the forks do).

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:issue:6625503561 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46). Originally posted by [64bitMango](https://github.com/64bitMango) on 2026-11-26T11:17:54Z. --- From the Windows forks of the same build (1.0.2.1); worth planning for in the Metal backend's texture model. **What happens.** The ocean's height and displacement maps are textures that compute passes write and the title's physics samples on the CPU. If the layer never copies them back into title memory, the CPU reads zeros: boats sink, the sea plane goes under, and the first boat mission fails. With the read-back, boats stay up through whole runs (avatarmanz/VIctory-7900XTX `docs/findings.md`; fix by Geckomacabre, fxceless/victory-fork pull request #999, `VICTORY_TEXTURE_BACK`, on by default in avatarmanz's tree). **Why it matters more natively.** Even with title memory mapped straight into `MTLBuffer`s, textures live in Metal's own (twiddled) layouts, so a GPU write to a texture is not visible in the title's linear/tiled bytes. Textures the title reads back need an explicit copy and re-tiling into title memory after the GPU writes them, at least for those it is known to read (found by a CPU read fault on the texture's pages, as the forks do).
LakshanSivakumar commented 2026-11-26 13:28:27 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T13:28:27Z.


Thanks for flagging this early, @64bitMango — good catch to keep on the radar before 3D world rendering and physics are active.

Because Metal textures use private/twiddled layouts even when title pools are zero-copy mapped as MTLBuffers, GPU-written textures that the CPU samples (like the ocean height/displacement maps) will need an explicit readback + re-tiling path into title memory. Added bug and help wanted labels.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7193399937 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7193399937). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T13:28:27Z. --- Thanks for flagging this early, @64bitMango — good catch to keep on the radar before 3D world rendering and physics are active. Because Metal textures use private/twiddled layouts even when title pools are zero-copy mapped as `MTLBuffer`s, GPU-written textures that the CPU samples (like the ocean height/displacement maps) will need an explicit readback + re-tiling path into title memory. Added `bug` and `help wanted` labels.
LakshanSivakumar commented 2026-11-26 15:56:59 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T15:56:59Z.


Status: not started. Matters once the world runs (see #15). Still open and up for grabs.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7196970565 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7196970565). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T15:56:59Z. --- Status: not started. Matters once the world runs (see #15). Still open and up for grabs.
LakshanSivakumar commented 2026-11-26 17:16:15 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T17:16:15Z.


@Olima34, assigning this to you as well, lower priority than #4 and #43. It needs a way to find textures the GPU writes that the title then reads on the CPU, and a copy back into title memory after the GPU writes. Happy to discuss the design here before code.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7198748237 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7198748237). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T17:16:15Z. --- @Olima34, assigning this to you as well, lower priority than #4 and #43. It needs a way to find textures the GPU writes that the title then reads on the CPU, and a copy back into title memory after the GPU writes. Happy to discuss the design here before code.
Olima34 commented 2026-11-26 17:22:47 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by Olima34 on 2026-11-26T17:22:47Z.


Design proposal, for discussion before any code. Facts are from current main.

What exists

  • No copy-back of any kind on the native host (native/memory.rs, docs/native-host.md: "host textures never go back to title memory").
  • The inverse of the detiler already exists: victory_formats::Layout::tile shares walk with detile, so it covers the same layouts. Its only caller today is XG.
  • Host textures are StorageModeShared, and ResourceBackend::read_texture (getBytes of a mip) exists.
  • Completed fence values reach the title in two places: glue::refresh (the 1 ms host thread, then fence::raise) and the title's own GetCompletedValue / SetEventOnCompletion polls (fence.rs).
  • Nothing records which textures the GPU wrote. The only signal is TextureInfo.flags (RT 1 / DS 2 / UA 4) plus the bound targets and View::Unordered at draw or dispatch time.

Proposal, in three small PRs

  1. Find them (diagnostic, VICTORY_TRACE_GPU_WRITTEN, off by default). At each draw or dispatch, record the texture keys bound as RT/DS or as UAV. Per submission, log each written texture once: key, format, size, swizzle mode, flags, and the submission's fence value. You run it on a story or world load and tell us which keys are the ocean maps (small, UAV, written every frame by compute). That gives us real data instead of a guess.
  2. Copy back a chosen set, on the CPU. A texture joins the set by a rule we pick from step 1's data (a first guess: UAV-written, not a render target, at most a few MiB). When a submission that wrote one completes, on every path that makes its fence value visible to the title (both above) and before the value is raised:
    • read_texture for each mip and slice;
    • Layout::tile into title memory;
    • set the scanner's baseline hash to the new bytes, so the write-back is not seen as a title write and re-uploaded.
      Ordering: the title can only read after it sees the fence, so the bytes are there in time. Unsupported layouts (SW_4KB_R_X, MSAA, 96-bit) are logged once and skipped.
  3. Only if profiles show the CPU copy matters: a compute tiling kernel that writes the tiled bytes straight into the pool MTLBuffer at the end of the submission. It is ordered on the GPU and costs no CPU, and it is the same addressing as the planned compute detile (docs/metal-backend.md).

What I'd rather not do: detect CPU reads with PROT_NONE guards. The native host has no page protection. It would cost a fault per first touch per frame and hit the Rosetta masked-store problem watch.rs documents. Picking the set from step 1 avoids it.

Questions

  • Does the title wait on a fence before its physics reads the maps, or does it read a frame late with no wait? If the latter, step 2 still works: the copy lands at completion either way.
  • Is a size cap acceptable as the first rule, or would you rather list keys or formats from the step 1 log?

If this looks right, I'll start with step 1.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7198915152 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7198915152). Originally posted by [Olima34](https://github.com/Olima34) on 2026-11-26T17:22:47Z. --- Design proposal, for discussion before any code. Facts are from current main. **What exists** - No copy-back of any kind on the native host (`native/memory.rs`, `docs/native-host.md`: "host textures never go back to title memory"). - The inverse of the detiler already exists: `victory_formats::Layout::tile` shares `walk` with `detile`, so it covers the same layouts. Its only caller today is XG. - Host textures are `StorageModeShared`, and `ResourceBackend::read_texture` (`getBytes` of a mip) exists. - Completed fence values reach the title in two places: `glue::refresh` (the 1 ms host thread, then `fence::raise`) and the title's own `GetCompletedValue` / `SetEventOnCompletion` polls (`fence.rs`). - Nothing records which textures the GPU wrote. The only signal is `TextureInfo.flags` (RT 1 / DS 2 / UA 4) plus the bound targets and `View::Unordered` at draw or dispatch time. **Proposal, in three small PRs** 1. **Find them (diagnostic, `VICTORY_TRACE_GPU_WRITTEN`, off by default).** At each draw or dispatch, record the texture keys bound as RT/DS or as UAV. Per submission, log each written texture once: key, format, size, swizzle mode, flags, and the submission's fence value. You run it on a story or world load and tell us which keys are the ocean maps (small, UAV, written every frame by compute). That gives us real data instead of a guess. 2. **Copy back a chosen set, on the CPU.** A texture joins the set by a rule we pick from step 1's data (a first guess: UAV-written, not a render target, at most a few MiB). When a submission that wrote one completes, on every path that makes its fence value visible to the title (both above) and *before* the value is raised: - `read_texture` for each mip and slice; - `Layout::tile` into title memory; - set the scanner's baseline hash to the new bytes, so the write-back is not seen as a title write and re-uploaded. Ordering: the title can only read after it sees the fence, so the bytes are there in time. Unsupported layouts (`SW_4KB_R_X`, MSAA, 96-bit) are logged once and skipped. 3. **Only if profiles show the CPU copy matters: a compute tiling kernel** that writes the tiled bytes straight into the pool `MTLBuffer` at the end of the submission. It is ordered on the GPU and costs no CPU, and it is the same addressing as the planned compute detile (`docs/metal-backend.md`). **What I'd rather not do:** detect CPU *reads* with `PROT_NONE` guards. The native host has no page protection. It would cost a fault per first touch per frame and hit the Rosetta masked-store problem `watch.rs` documents. Picking the set from step 1 avoids it. **Questions** - Does the title wait on a fence before its physics reads the maps, or does it read a frame late with no wait? If the latter, step 2 still works: the copy lands at completion either way. - Is a size cap acceptable as the first rule, or would you rather list keys or formats from the step 1 log? If this looks right, I'll start with step 1.
LakshanSivakumar commented 2026-11-26 18:11:46 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by LakshanSivakumar on 2026-11-26T18:11:46Z.


@Olima34 The plan looks right; please start with step 1. A size cap is fine as the first rule, as long as step 1 logs the keys so we can check it against real data. We don't know yet whether the title waits on a fence before the physics reads the maps, but as you say, step 2 works either way.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7200168015 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7200168015). Originally posted by [LakshanSivakumar](https://github.com/LakshanSivakumar) on 2026-11-26T18:11:46Z. --- @Olima34 The plan looks right; please start with step 1. A size cap is fine as the first rule, as long as step 1 logs the keys so we can check it against real data. We don't know yet whether the title waits on a fence before the physics reads the maps, but as you say, step 2 works either way.
lautarocherro commented 2026-11-27 19:48:31 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by lautarocherro on 2026-11-27T19:48:31Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7226890690 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7226890690). Originally posted by [lautarocherro](https://github.com/lautarocherro) on 2026-11-27T19:48:31Z. --- /claim
github-actions[bot] commented 2026-11-27 19:48:48 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-27T19:48:48Z.


Claimed by @lautarocherro. Open a draft pull request with Refs #46 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7226895439 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7226895439). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-27T19:48:48Z. --- Claimed by @lautarocherro. Open a draft pull request with `Refs #46` in its description within 24 hours, or the claim expires.
lautarocherro commented 2026-11-29 02:25:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by lautarocherro on 2026-11-29T02:25:40Z.


/release

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7249420833 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7249420833). Originally posted by [lautarocherro](https://github.com/lautarocherro) on 2026-11-29T02:25:40Z. --- /release
github-actions[bot] commented 2026-11-29 02:25:49 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-29T02:25:49Z.


Released. This issue is free to claim.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7249422379 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7249422379). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T02:25:49Z. --- Released. This issue is free to claim.
AnastasisKon commented 2026-11-29 09:07:05 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-29T09:07:05Z.


/claim

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7253547925 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253547925). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-29T09:07:05Z. --- /claim
github-actions[bot] commented 2026-11-29 09:07:22 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-29T09:07:22Z.


Claimed by @AnastasisKon. Open a draft pull request with Refs #46 in its description within 24 hours, or the claim expires.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7253550866 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253550866). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T09:07:22Z. --- Claimed by @AnastasisKon. Open a draft pull request with `Refs #46` in its description within 24 hours, or the claim expires.
AnastasisKon commented 2026-11-29 09:10:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-29T09:10:03Z.


Status after claiming and investigating: the implementation this issue asked for is already on main — the fence-driven copy-back behind VICTORY_TEXTURE_BACK=1 (textures.rs: Title::copy_back/write_back, retile through Back, the freed-range and left-pool skips of #339, BACK_LIMIT, render-target exclusion), documented in docs/native-host.md and ROADMAP. Credit to whoever landed it; nothing about the mechanism is missing.

What remains, per ROADMAP, is run evidence past the M5 Pro crash ('22 textures named, then crashes before boats can be seen'). From this machine (M3 Pro, current main bf64dc4e, headless run of 2026-11-29): the run never reaches textures at all — it dies at 3.4 s on the SGD5 shader-database int3 at image+0x141330a44, the boot blocker of docs/native-host.md, before a DirectStorage queue even exists. So the run evidence this issue needs is gated on solving that blocker first, which is the repo's standing item 3.

Releasing the claim: the remaining work here is blocked on the boot blocker, and whoever solves that (or runs on a machine past it) should take the verification run with VICTORY_TEXTURE_BACK=1.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7253580893 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253580893). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-29T09:10:03Z. --- Status after claiming and investigating: the implementation this issue asked for is already on main — the fence-driven copy-back behind VICTORY_TEXTURE_BACK=1 (textures.rs: Title::copy_back/write_back, retile through Back, the freed-range and left-pool skips of #339, BACK_LIMIT, render-target exclusion), documented in docs/native-host.md and ROADMAP. Credit to whoever landed it; nothing about the mechanism is missing. What remains, per ROADMAP, is run evidence past the M5 Pro crash ('22 textures named, then crashes before boats can be seen'). From this machine (M3 Pro, current main bf64dc4e, headless run of 2026-11-29): the run never reaches textures at all — it dies at 3.4 s on the SGD5 shader-database int3 at image+0x141330a44, the boot blocker of docs/native-host.md, before a DirectStorage queue even exists. So the run evidence this issue needs is gated on solving that blocker first, which is the repo's standing item 3. Releasing the claim: the remaining work here is blocked on the boot blocker, and whoever solves that (or runs on a machine past it) should take the verification run with VICTORY_TEXTURE_BACK=1.
AnastasisKon commented 2026-11-29 09:10:04 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by AnastasisKon on 2026-11-29T09:10:04Z.


/release

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7253581070 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253581070). Originally posted by [AnastasisKon](https://github.com/AnastasisKon) on 2026-11-29T09:10:04Z. --- /release
github-actions[bot] commented 2026-11-29 09:10:20 +00:00 (Migrated from github.com)
Author
Contributor

Imported from LakshanSivakumar/Native-Victory-macOS. Originally posted by github-actions[bot] on 2026-11-29T09:10:20Z.


Released. This issue is free to claim.

<!-- greyfinch-sync:LakshanSivakumar/Native-Victory-macOS:comment:7253584083 --> Imported from [LakshanSivakumar/Native-Victory-macOS](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253584083). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-11-29T09:10:20Z. --- Released. This issue is free to claim.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LakshanSivakumar/Native-Victory-macOS#46
No description provided.