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 14:43:49 +00:00 by github-bot · 14 comments
Owner

@⁠64bitMango on GitHub · 2026-11-26 11:17 UTC · original

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).

**@⁠64bitMango** on GitHub · 2026-11-26 11:17 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46) 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 14:43:49 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 13:28 UTC · original

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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 13:28 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7193399937) 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.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 15:56 UTC · original

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

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 15:56 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7196970565) Status: not started. Matters once the world runs (see #15). Still open and up for grabs.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 17:16 UTC · original

@⁠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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 17:16 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7198748237) @⁠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.
Author
Owner

@⁠Olima34 on GitHub · 2026-11-26 17:22 UTC · original

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.

**@⁠Olima34** on GitHub · 2026-11-26 17:22 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7198915152) 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.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 18:11 UTC · original

@⁠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.

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 18:11 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7200168015) @⁠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.
Author
Owner

@⁠lautarocherro on GitHub · 2026-11-27 19:48 UTC · original

/claim

**@⁠lautarocherro** on GitHub · 2026-11-27 19:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7226890690) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-27 19:48 UTC · original

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

**@⁠github-actions[bot]** on GitHub · 2026-11-27 19:48 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7226895439) Claimed by @⁠lautarocherro. Open a draft pull request with `Refs #46` in its description within 24 hours, or the claim expires.
Author
Owner

@⁠lautarocherro on GitHub · 2026-11-29 02:25 UTC · original

/release

**@⁠lautarocherro** on GitHub · 2026-11-29 02:25 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7249420833) /release
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-29 02:25 UTC · original

Released. This issue is free to claim.

**@⁠github-actions[bot]** on GitHub · 2026-11-29 02:25 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7249422379) Released. This issue is free to claim.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-29 09:07 UTC · original

/claim

**@⁠AnastasisKon** on GitHub · 2026-11-29 09:07 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253547925) /claim
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-29 09:07 UTC · original

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

**@⁠github-actions[bot]** on GitHub · 2026-11-29 09:07 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253550866) Claimed by @⁠AnastasisKon. Open a draft pull request with `Refs #46` in its description within 24 hours, or the claim expires.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-29 09:10 UTC · original

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 #340, 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** on GitHub · 2026-11-29 09:10 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253580893) 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 #340, 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.
Author
Owner

@⁠AnastasisKon on GitHub · 2026-11-29 09:10 UTC · original

/release

**@⁠AnastasisKon** on GitHub · 2026-11-29 09:10 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253581070) /release
Author
Owner

@⁠github-actions[bot] on GitHub · 2026-11-29 09:10 UTC · original

Released. This issue is free to claim.

**@⁠github-actions[bot]** on GitHub · 2026-11-29 09:10 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/issues/46#issuecomment-7253584083) Released. This issue is free to claim.
This discussion has been locked. Commenting is limited to contributors.
No description provided.