native: copy textures compute writes back into title memory (VICTORY_TEXTURE_BACK=1) #151

Manually merged
github merged 3 commits from gh-pr/151/head into gh-pr/151/base 2026-11-27 00:51:26 +00:00
Owner

@⁠m0rff on GitHub · 2026-11-26 21:41 UTC · original
m0rff:m0rff/gpu-compute-texture-readback → main · +430 −10 · 9 files · 3 commits · merged 2026-11-26 22:38 UTC

Refs #46.

This overlaps @⁠Olima34's design on #46, which was approved and assigned there. I found no branch or PR for it yet. If one is in progress, I'm happy to rebase onto it, fold this into it, or drop this.

What

Textures a compute pass writes are retiled back into title memory, so CPU reads (the ocean height and displacement maps) see the GPU's result. Off unless VICTORY_TEXTURE_BACK=1.

Native Metal host only. The Windows path (src/texture.rs, watch.rs) is unchanged.

As in the proposed design:

  • no page protection or read faults;
  • the copy happens before a fence value is shown to the title;
  • Layout::tile does the retiling;
  • the upload hash is updated, so the write-back is not uploaded again;
  • there is a size cap (4 MiB, a guess).

Where it differs: there is no separate step 1 trace. Written textures are found from the unordered access views in the tables each dispatch binds, and each one copied or skipped for size is logged once, which gives the data step 1 was after.

If the title itself wrote the memory since the last upload, its bytes are kept.

Tested (M2 Pro, native arm64)

  • A compute dispatch with our own shader (tex_fill.hlsl) writes a 64x64 RGBA8 texture in LINEAR, SW_4KB_S and SW_64KB_D_X. Once the fence value shows, title memory equals the retiled texels. The test fails with the copy-back off.
  • A CPU write made while the same work is pending is not overwritten.
  • The B4G4R4A4 conversion round-trips.
  • cargo test -p victory-hle -p victory -p victory-gpu -p xtask, the Windows-target clippy and cargo xtask policy pass.

tex_fill.dxil is added to the policy allowlist, like the other fixtures compiled from our own HLSL.

Not tested / not covered

  • No game run. Whether boats float, which textures get named, and the frame cost are unknown.
  • Mip chains, arrays, 3D textures and other element sizes.
  • UAVs reached by indexing the descriptor heap directly, pixel shader UAV writes and GPU copies into textures are not seen.
  • A title that polls a GPU-written label instead of a fence could read before the copy.
  • The copy runs on the CPU, under the title lock.
**@⁠m0rff** on GitHub · 2026-11-26 21:41 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151) `m0rff:m0rff/gpu-compute-texture-readback` → `main` · +430 −10 · 9 files · 3 commits · merged 2026-11-26 22:38 UTC Refs #46. **This overlaps @⁠Olima34's design on #46**, which was approved and assigned there. I found no branch or PR for it yet. If one is in progress, I'm happy to rebase onto it, fold this into it, or drop this. ## What Textures a compute pass writes are retiled back into title memory, so CPU reads (the ocean height and displacement maps) see the GPU's result. Off unless `VICTORY_TEXTURE_BACK=1`. Native Metal host only. The Windows path (`src/texture.rs`, `watch.rs`) is unchanged. As in the proposed design: - no page protection or read faults; - the copy happens before a fence value is shown to the title; - `Layout::tile` does the retiling; - the upload hash is updated, so the write-back is not uploaded again; - there is a size cap (4 MiB, a guess). Where it differs: there is no separate step 1 trace. Written textures are found from the unordered access views in the tables each dispatch binds, and each one copied or skipped for size is logged once, which gives the data step 1 was after. If the title itself wrote the memory since the last upload, its bytes are kept. ## Tested (M2 Pro, native arm64) - A compute dispatch with our own shader (`tex_fill.hlsl`) writes a 64x64 RGBA8 texture in `LINEAR`, `SW_4KB_S` and `SW_64KB_D_X`. Once the fence value shows, title memory equals the retiled texels. The test fails with the copy-back off. - A CPU write made while the same work is pending is not overwritten. - The B4G4R4A4 conversion round-trips. - `cargo test -p victory-hle -p victory -p victory-gpu -p xtask`, the Windows-target clippy and `cargo xtask policy` pass. `tex_fill.dxil` is added to the policy allowlist, like the other fixtures compiled from our own HLSL. ## Not tested / not covered - **No game run.** Whether boats float, which textures get named, and the frame cost are unknown. - Mip chains, arrays, 3D textures and other element sizes. - UAVs reached by indexing the descriptor heap directly, pixel shader UAV writes and GPU copies into textures are not seen. - A title that polls a GPU-written label instead of a fence could read before the copy. - The copy runs on the CPU, under the title lock.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:51:25 +00:00
github manually merged commit 06b5e46246 into gh-pr/151/base 2026-11-27 00:51:26 +00:00
Author
Owner

@⁠Olima34 on GitHub · 2026-11-26 21:43 UTC · original

Thanks @⁠m0rff, and for asking. Nothing of mine is in progress on #46, so no overlap: please carry it on. Finding the written textures from the UAVs each dispatch binds is a good replacement for the step-1 trace.

One ordering point, from reading the diff (not run):

  • submitted() signals our fence with a separate execute after the title's submission, which already carries the title's own signals.
  • completed() (and the poll path) reads the title fences first, then runs copy_back(), which only copies once our fence has its value.

So the title's fence can show a value one beat before ours does. completed() then returns that value while copy_back() skips the submission as not reached yet, and a title waiting on that fence reads title memory before the copy. Two ways I see to close it:

  • signal our fence inside the same submission, before the title's signals; or
  • in completed() and the poll, don't show a title fence value past a submission whose copy-back is still pending (clamp it to the last value whose writes are copied).

A test that holds our fence back while letting the title's complete would pin it.

I now have the game running locally (M5 Pro). Once a run reaches the world I'll try VICTORY_TEXTURE_BACK=1 and post which textures get named and the frame cost.

**@⁠Olima34** on GitHub · 2026-11-26 21:43 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#issuecomment-7203997818) Thanks @⁠m0rff, and for asking. Nothing of mine is in progress on #46, so no overlap: please carry it on. Finding the written textures from the UAVs each dispatch binds is a good replacement for the step-1 trace. One ordering point, from reading the diff (not run): - `submitted()` signals our fence with a separate `execute` **after** the title's submission, which already carries the title's own signals. - `completed()` (and the poll path) reads the title fences first, then runs `copy_back()`, which only copies once **our** fence has its value. So the title's fence can show a value one beat before ours does. `completed()` then returns that value while `copy_back()` skips the submission as not reached yet, and a title waiting on that fence reads title memory before the copy. Two ways I see to close it: - signal our fence inside the same submission, before the title's signals; or - in `completed()` and the poll, don't show a title fence value past a submission whose copy-back is still pending (clamp it to the last value whose writes are copied). A test that holds our fence back while letting the title's complete would pin it. I now have the game running locally (M5 Pro). Once a run reaches the world I'll try `VICTORY_TEXTURE_BACK=1` and post which textures get named and the frame cost.
Author
Owner

@⁠m0rff on GitHub · 2026-11-26 22:09 UTC · original

Checked the ordering: the work's execute carried no title signals. Those go out in later command buffers (the held signal action or signal_gpu), after the one that signalled our fence, so on one queue the title's value shouldn't show before ours unless command buffers complete out of order.

I've still taken your first option: our fence is now signalled in the work's own command buffer, so it doesn't depend on that. No hold-back test, since the two can't be separated now.

Still open: a queue that waits on a GPU-written memory label and then signals could get ahead of the copy. It's noted in docs/native-host.md.

Looking forward to your world run: which textures get named, and the frame cost.

**@⁠m0rff** on GitHub · 2026-11-26 22:09 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#issuecomment-7204409279) Checked the ordering: the work's `execute` carried no title signals. Those go out in later command buffers (the held `signal` action or `signal_gpu`), after the one that signalled our fence, so on one queue the title's value shouldn't show before ours unless command buffers complete out of order. I've still taken your first option: our fence is now signalled in the work's own command buffer, so it doesn't depend on that. No hold-back test, since the two can't be separated now. Still open: a queue that waits on a GPU-written memory label and then signals could get ahead of the copy. It's noted in `docs/native-host.md`. Looking forward to your world run: which textures get named, and the frame cost.
Author
Owner

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

Thanks, @⁠m0rff. You're right that the title's signals go out in later command buffers, and signalling our fence in the work's own buffer removes the question either way. Agreed on the memory-label case being documented rather than solved here.

On the world run: my runs reach the first world frame here (Series S, frame ~2500–4140) and then crash at the known image+0x55e56f8, so I'll only see what gets written up to that frame. I'll build your branch with VICTORY_TEXTURE_BACK=1 and post which textures it names and what it costs per frame there.

**@⁠Olima34** on GitHub · 2026-11-26 22:10 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#issuecomment-7204434966) Thanks, @⁠m0rff. You're right that the title's signals go out in later command buffers, and signalling our fence in the work's own buffer removes the question either way. Agreed on the memory-label case being documented rather than solved here. On the world run: my runs reach the first world frame here (Series S, frame ~2500–4140) and then crash at the known `image+0x55e56f8`, so I'll only see what gets written up to that frame. I'll build your branch with `VICTORY_TEXTURE_BACK=1` and post which textures it names and what it costs per frame there.
Author
Owner

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

World run with this branch (fa7f1148), VICTORY_TEXTURE_BACK=1. Apple M5 Pro, 48 GB, Series S, windowed. It reaches the first world frame and then the known image+0x55e56f8 crash (frame 2671, +140 s), the same as without the branch.

Textures named: 22 copied back, each logged once. Nothing was reported as skipped for size.

  • By dimension: 15 Tex2d, 4 Tex3d, 3 Tex1d.
  • 19 have one mip; three have 5, 6 and 9 mips.
  • DXGI formats: 10 (R16G16B16A16_FLOAT) ×6, 42 (R32_UINT) ×4, 41 (R32_FLOAT) ×3, 17 (R32G32_UINT) ×3, 28 (R8G8B8A8_UNORM) ×2, 24 (R10G10B10A2) ×2, 61 (R8_UNORM) ×1, 26 (R11G11B10_FLOAT) ×1.
  • The largest are 256×256×16 Tex3d (4 MiB) and 64×64×64 Tex3d (1 MiB).
  • When: 1 at load (+~10 s), then 21 in the last ~10 s before the world frame.

Cost: no measurable change in frame rate over the same stretch. Frames 2048–2560 run at 28.0 / 24.0 / 9.3 / 21.2 / 19.3 fps with the branch, against 28.0 / 26.9 / 11.0 / 19.8 / 17.4 fps on the same Mac without it, within run-to-run noise.

One thing to check: the PR says mip chains and 3D textures aren't covered, but the log reports 4 Tex3d and 3 mipped textures as copied back. If only mip 0 / slice 0 is retiled, those may be partly written back. Either skip them (log once), or make sure every mip and slice goes through Layout::tile.

Whether boats float can't be seen yet: the crash comes first. I'll rerun once we get past it.

**@⁠Olima34** on GitHub · 2026-11-26 22:16 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#issuecomment-7204526276) World run with this branch (`fa7f1148`), `VICTORY_TEXTURE_BACK=1`. Apple M5 Pro, 48 GB, Series S, windowed. It reaches the first world frame and then the known `image+0x55e56f8` crash (frame 2671, +140 s), the same as without the branch. **Textures named:** 22 copied back, each logged once. Nothing was reported as skipped for size. - By dimension: 15 Tex2d, 4 **Tex3d**, 3 Tex1d. - 19 have one mip; three have **5, 6 and 9 mips**. - DXGI formats: 10 (R16G16B16A16_FLOAT) ×6, 42 (R32_UINT) ×4, 41 (R32_FLOAT) ×3, 17 (R32G32_UINT) ×3, 28 (R8G8B8A8_UNORM) ×2, 24 (R10G10B10A2) ×2, 61 (R8_UNORM) ×1, 26 (R11G11B10_FLOAT) ×1. - The largest are 256×256×16 Tex3d (4 MiB) and 64×64×64 Tex3d (1 MiB). - When: 1 at load (+~10 s), then 21 in the last ~10 s before the world frame. **Cost:** no measurable change in frame rate over the same stretch. Frames 2048–2560 run at 28.0 / 24.0 / 9.3 / 21.2 / 19.3 fps with the branch, against 28.0 / 26.9 / 11.0 / 19.8 / 17.4 fps on the same Mac without it, within run-to-run noise. **One thing to check:** the PR says mip chains and 3D textures aren't covered, but the log reports 4 Tex3d and 3 mipped textures as copied back. If only mip 0 / slice 0 is retiled, those may be partly written back. Either skip them (log once), or make sure every mip and slice goes through `Layout::tile`. Whether boats float can't be seen yet: the crash comes first. I'll rerun once we get past it.
Author
Owner

@⁠m0rff on GitHub · 2026-11-26 22:24 UTC · original

No partial write-back: download goes over every mip and array slice, and reads a 3D mip with its full depth. "Not covered" meant untested, which was unclear.

06b5e46 adds an upload/download round trip for 1D, mipped 2D (5 and 9 mips), a 2D array, and 3D with and without mips, in LINEAR, SW_4KB_S, SW_64KB_S and SW_64KB_D_X; all match. It shows nothing is dropped, not that the layout equations match the console, since both directions use them. A dispatch writing those shapes is still untested.

Also fixed: a texture skipped on its first completion was never logged afterwards, so your 22 may be an undercount.

Thanks for the run. I've added the numbers to docs/native-host.md.

**@⁠m0rff** on GitHub · 2026-11-26 22:24 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#issuecomment-7204652102) No partial write-back: `download` goes over every mip and array slice, and reads a 3D mip with its full depth. "Not covered" meant untested, which was unclear. `06b5e46` adds an upload/download round trip for 1D, mipped 2D (5 and 9 mips), a 2D array, and 3D with and without mips, in `LINEAR`, `SW_4KB_S`, `SW_64KB_S` and `SW_64KB_D_X`; all match. It shows nothing is dropped, not that the layout equations match the console, since both directions use them. A dispatch writing those shapes is still untested. Also fixed: a texture skipped on its first completion was never logged afterwards, so your 22 may be an undercount. Thanks for the run. I've added the numbers to `docs/native-host.md`.
Author
Owner

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

Thanks, that settles it: every mip and slice goes through the download, and the round trip test pins it. I'll rerun with this branch once we get past the world frame here.

**@⁠Olima34** on GitHub · 2026-11-26 22:28 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#issuecomment-7204703543) Thanks, that settles it: every mip and slice goes through the download, and the round trip test pins it. I'll rerun with this branch once we get past the world frame here.
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-26 22:38 UTC · original · approved these changes

Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!

**@⁠LakshanSivakumar** on GitHub · 2026-11-26 22:38 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/151#pullrequestreview-6081186625) · **approved these changes** Reviewed and tested together with the other open PRs on main (macOS x86_64 workspace tests, the setup shell tests, and Windows clippy). Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.