native: depth textures compute writes get colour twins, copied where the use changes (#258) #374

Manually merged
github merged 1 commit from github-bot/gh-371 into main 2026-11-27 16:23:13 +00:00 AGit
Owner

@⁠Tarraga on GitHub · 2026-11-27 14:28 UTC · original
Tarraga:native-depth-twins → main · +443 −0 · 4 files · 1 commits · merged 2026-11-27 16:14 UTC

Refs #258. This covers only the piece I claimed there: depth textures that compute writes.

Problem

The tiled light draws test equal against a 1280x720 depth target of DXGI format 118, the console's D16_UNORM_S8_UINT. The host keeps it as 32-bit float depth with stencil. Compute writes it through unordered views: R16_UNORM views of the depth plane, and R8_UINT views of the stencil plane. Our VICTORY_TRACE_TARGETS=1 run lists it among the UAVs of f0c91b4d2578f5bf (160x90 groups), 570f1473f4641b30 and bad01f53f53248e5.

On main, a colour view of a depth texture is the depth texture itself, because create_texture_view keeps the depth format. So compute writes the depth texture directly. Metal does not support shader writes to a depth texture. On an M4 Pro, a blit copy of the depth plane reads what compute wrote, but a later render pass's depth test does not see it.

Fix

  • The twin. At the first unordered view of a depth texture object's depth plane, the host makes a colour twin (NativeHost::depth_twin):
    • R16Unorm for a 16-bit depth texture (the shadow maps);
    • R32Float for a 32-bit one. An R16_UNORM view of a 32-bit plane writes the twin as 32-bit floats.
  • Views. One-channel colour views of the depth plane made after that show the twin. Entries mirrored before it are mirrored again.
  • Copies. The newer contents move between the two where the use changes (NativeHost::twin_copies), as VIctory's twin.rs does on the D3D12 path:
    • into the twin before a compute pass;
    • into the depth texture before a render pass whose depth target it is, or a copy to or from it.
    • Each copy happens once per change, through a buffer of the twin's (DepthFromDepthStencil).
    • A depth clear makes the depth texture the newer side with no copy.
  • Switch. VICTORY_DEPTH_TWINS=0 turns it off.

Guesses, also in docs/native-host.md:

  • The title writes k / 65535 through an R16_UNORM view of the 32-bit plane, the very float its draws rasterise. VIctory's D3D12 path converts k to k * 0x37800080; the host keeps the float the shader stores.
  • A render pass that only reads a twin, with no compute pass since the last draws into its depth texture, reads the twin's older contents. For example, a shadow map sampled through a colour view. In the frames we traced, compute runs between the shadow draws and the lighting.
  • Entries are mirrored again only where the host checks them at submission (the bound tables).
  • The stencil plane's R8_UINT views still reach nothing.

Why the default is on

I know you prefer defaults that don't change behaviour for others until more Macs have run them. Here is why I think on is right for this one, and I'll switch it off if you disagree:

  • What changes. Only depth textures that get an unordered view of their depth plane. On main their compute writes are lost to the depth test (the test below), so a lit pass that tests against them is wrong on every Mac. No other texture, view or pass changes. A submission with no twin adds no pass and no copy.
  • Speed. It is not a speed trade-off like VICTORY_PASSES_PER_BUFFER. The cost is copies of the twinned textures only. I have no clean before-and-after frame rate: the run with twins both ways (r164) ran 1.8 to 1.9 world frames a second, and the closest run without twins (r159, before the alias) ran 2.0 to 2.1, with other changes in between.
  • Memory, the real risk for 16 GB Macs (#8). It needs one twin plus a staging buffer of the same size per such texture. One world run made 108 twins: the material depth, 3 other depth targets, and 104 shadow maps of 512x384 to 1536x1536. Had they all been alive at once, that would be about 97 MB of twins and as much in buffers. If that is too much for 16 GB Macs before others have tried it, I'll make the default off, with VICTORY_DEPTH_TWINS=1 to turn it on.

Evidence

One Mac (M4 Pro, 48 GB, Series S, warm cache). The game runs were on our working branch, not on this main: main of 27 November morning, plus a placed-view alias like the one main now has, and other changes of ours.

  • Before the twin of the material depth (r162). The prologue's window scene was washed out. In the screen captures from 199 s to 423 s, the mean luminance was 164 to 174 of 255, and no pixel was dark (below 16).
  • With it (r163, r164). The scene was lit at sunset, close to the Wine route's picture of the same scene: a mean of 26 to 29, with 67 to 72% of pixels dark. I did not measure why it was washed out without the twin. The light draws then tested against a depth plane whose compute writes the depth test did not see.
  • Twins both ways (r164). 480 s with no GPU error, 1.8 to 1.9 world frames a second.

Tests

  • native::tests::a_draw_tests_against_the_depth_compute_wrote_through_an_unordered_view needs the Shader Converter. In one list, a D32_FLOAT texture gets:
    1. a depth clear to 1.0;
    2. a tex_fill dispatch that writes x / 255 through an R32_FLOAT unordered view;
    3. a full-screen draw at depth 0.02, tested LessEqual.
    • With the change, the draw lands at columns 6 to 15 of 16, as it should.
    • On main 728845d1, with only this test added, it fails: the draw lands nowhere (green row [0; 16]).
  • texture_tests::an_unordered_view_of_a_depth_texture_writes_a_colour_twin: the twin is made at the unordered view, not at a colour read before it, and the newer side moves as described.
  • texture_tests::a_depth_twin_and_its_depth_plane_hold_the_same_texels: for DXGI 118 and a 16-bit depth texture, what the twin holds goes into the depth plane and back on the GPU, bit for bit.
  • victory-gpu on x86_64 with D3DMetal's converter (VICTORY_MSC_DYLIB): the texture tests and native::tests::a_*, 49 of 49 pass.
  • victory-gpu on arm64: 360 pass and 3 fail. The failures are described_rectangle_list_draw_uses_supplied_triangles, an_indirect_dispatch_mesh_x_draws_the_groups_its_record_holds and a_drawn_target_is_not_filled_from_title_memory_again. This Mac has no arm64 converter, so their pipelines are not made. All three pass on x86_64 with the converter.
  • cargo xtask policy passes.
  • The Windows clippy target isn't installed here. Host clippy shows no warnings in the new code.

How to check that the switch takes the old path

With VICTORY_DEPTH_TWINS=0, depth_twin returns None before anything else. No twin is ever made, the twin map stays empty, and twin_copies / twin_uses return at once. The views and passes are exactly main's.

**@⁠Tarraga** on GitHub · 2026-11-27 14:28 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/371) `Tarraga:native-depth-twins` → `main` · +443 −0 · 4 files · 1 commits · merged 2026-11-27 16:14 UTC Refs #258. This covers only the piece I claimed there: depth textures that compute writes. ## Problem The tiled light draws test equal against a 1280x720 depth target of DXGI format 118, the console's `D16_UNORM_S8_UINT`. The host keeps it as 32-bit float depth with stencil. Compute writes it through unordered views: `R16_UNORM` views of the depth plane, and `R8_UINT` views of the stencil plane. Our `VICTORY_TRACE_TARGETS=1` run lists it among the UAVs of `f0c91b4d2578f5bf` (160x90 groups), `570f1473f4641b30` and `bad01f53f53248e5`. On main, a colour view of a depth texture is the depth texture itself, because `create_texture_view` keeps the depth format. So compute writes the depth texture directly. Metal does not support shader writes to a depth texture. On an M4 Pro, a blit copy of the depth plane reads what compute wrote, but a later render pass's depth test does not see it. ## Fix - **The twin.** At the first unordered view of a depth texture object's depth plane, the host makes a colour twin (`NativeHost::depth_twin`): - `R16Unorm` for a 16-bit depth texture (the shadow maps); - `R32Float` for a 32-bit one. An `R16_UNORM` view of a 32-bit plane writes the twin as 32-bit floats. - **Views.** One-channel colour views of the depth plane made after that show the twin. Entries mirrored before it are mirrored again. - **Copies.** The newer contents move between the two where the use changes (`NativeHost::twin_copies`), as VIctory's `twin.rs` does on the D3D12 path: - into the twin before a compute pass; - into the depth texture before a render pass whose depth target it is, or a copy to or from it. - Each copy happens once per change, through a buffer of the twin's (`DepthFromDepthStencil`). - A depth clear makes the depth texture the newer side with no copy. - **Switch.** `VICTORY_DEPTH_TWINS=0` turns it off. Guesses, also in `docs/native-host.md`: - The title writes `k / 65535` through an `R16_UNORM` view of the 32-bit plane, the very float its draws rasterise. VIctory's D3D12 path converts `k` to `k * 0x37800080`; the host keeps the float the shader stores. - A render pass that only reads a twin, with no compute pass since the last draws into its depth texture, reads the twin's older contents. For example, a shadow map sampled through a colour view. In the frames we traced, compute runs between the shadow draws and the lighting. - Entries are mirrored again only where the host checks them at submission (the bound tables). - The stencil plane's `R8_UINT` views still reach nothing. ## Why the default is on I know you prefer defaults that don't change behaviour for others until more Macs have run them. Here is why I think on is right for this one, and I'll switch it off if you disagree: - **What changes.** Only depth textures that get an unordered view of their depth plane. On main their compute writes are lost to the depth test (the test below), so a lit pass that tests against them is wrong on every Mac. No other texture, view or pass changes. A submission with no twin adds no pass and no copy. - **Speed.** It is not a speed trade-off like `VICTORY_PASSES_PER_BUFFER`. The cost is copies of the twinned textures only. I have no clean before-and-after frame rate: the run with twins both ways (r164) ran 1.8 to 1.9 world frames a second, and the closest run without twins (r159, before the alias) ran 2.0 to 2.1, with other changes in between. - **Memory, the real risk for 16 GB Macs (#8).** It needs one twin plus a staging buffer of the same size per such texture. One world run made 108 twins: the material depth, 3 other depth targets, and 104 shadow maps of 512x384 to 1536x1536. Had they all been alive at once, that would be about 97 MB of twins and as much in buffers. If that is too much for 16 GB Macs before others have tried it, I'll make the default off, with `VICTORY_DEPTH_TWINS=1` to turn it on. ## Evidence One Mac (M4 Pro, 48 GB, Series S, warm cache). The game runs were on our working branch, not on this main: main of 27 November morning, plus a placed-view alias like the one main now has, and other changes of ours. - **Before the twin of the material depth (r162).** The prologue's window scene was washed out. In the screen captures from 199 s to 423 s, the mean luminance was 164 to 174 of 255, and no pixel was dark (below 16). - **With it (r163, r164).** The scene was lit at sunset, close to the Wine route's picture of the same scene: a mean of 26 to 29, with 67 to 72% of pixels dark. I did not measure why it was washed out without the twin. The light draws then tested against a depth plane whose compute writes the depth test did not see. - **Twins both ways (r164).** 480 s with no GPU error, 1.8 to 1.9 world frames a second. ## Tests - `native::tests::a_draw_tests_against_the_depth_compute_wrote_through_an_unordered_view` needs the Shader Converter. In one list, a `D32_FLOAT` texture gets: 1. a depth clear to 1.0; 2. a `tex_fill` dispatch that writes x / 255 through an `R32_FLOAT` unordered view; 3. a full-screen draw at depth 0.02, tested `LessEqual`. - With the change, the draw lands at columns 6 to 15 of 16, as it should. - On main `728845d1`, with only this test added, it fails: the draw lands nowhere (green row `[0; 16]`). - `texture_tests::an_unordered_view_of_a_depth_texture_writes_a_colour_twin`: the twin is made at the unordered view, not at a colour read before it, and the newer side moves as described. - `texture_tests::a_depth_twin_and_its_depth_plane_hold_the_same_texels`: for DXGI 118 and a 16-bit depth texture, what the twin holds goes into the depth plane and back on the GPU, bit for bit. - victory-gpu on x86_64 with D3DMetal's converter (`VICTORY_MSC_DYLIB`): the texture tests and `native::tests::a_*`, 49 of 49 pass. - victory-gpu on arm64: 360 pass and 3 fail. The failures are `described_rectangle_list_draw_uses_supplied_triangles`, `an_indirect_dispatch_mesh_x_draws_the_groups_its_record_holds` and `a_drawn_target_is_not_filled_from_title_memory_again`. This Mac has no arm64 converter, so their pipelines are not made. All three pass on x86_64 with the converter. - `cargo xtask policy` passes. - The Windows clippy target isn't installed here. Host clippy shows no warnings in the new code. ## How to check that the switch takes the old path With `VICTORY_DEPTH_TWINS=0`, `depth_twin` returns `None` before anything else. No twin is ever made, the twin map stays empty, and `twin_copies` / `twin_uses` return at once. The views and passes are exactly main's.
The tiled light draws test equal against the material depth (DXGI 118, the console's
D16_UNORM_S8_UINT, kept as 32-bit float depth with stencil), which compute writes through
R16_UNORM views of its depth plane. A colour view of a depth texture was the depth texture
itself; Metal does not support shader writes to one, and on an M4 Pro a later render pass's
depth test does not see them (a blit copy does).

The first unordered view of a depth plane now makes a colour twin (R16Unorm for a 16-bit depth
texture, R32Float for a 32-bit one; an R16_UNORM view of a 32-bit plane writes 32-bit floats).
Later one-channel colour views of the plane show the twin, and entries mirrored before are
mirrored again. The newer contents are copied through a buffer into the twin before a compute
pass, and into the depth texture before a render pass into it or a copy to or from it; a depth
clear makes the depth texture newer. VICTORY_DEPTH_TWINS=0 turns it off. Guesses are in
docs/native-host.md.

Test: tests::a_draw_tests_against_the_depth_compute_wrote_through_an_unordered_view fails on
main (the draw passes at no pixel) and passes with this change.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:34:09 +00:00
github-bot referenced this pull request from a commit 2026-11-27 16:13:14 +00:00
github manually merged commit 88f99bd3f0 into main 2026-11-27 16:23:13 +00:00
Author
Owner

@⁠LakshanSivakumar on GitHub · 2026-11-27 16:15 UTC · original

Merged in 4cf84c83. It was still a draft; I merged it because the change looks complete and is off with VICTORY_DEPTH_TWINS=0. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. I did not check the lighting result itself. Thanks.

**@⁠LakshanSivakumar** on GitHub · 2026-11-27 16:15 UTC · [original](https://github.com/LakshanSivakumar/Native-Victory-macOS/pull/371#issuecomment-7223043057) Merged in 4cf84c83. It was still a draft; I merged it because the change looks complete and is off with VICTORY_DEPTH_TWINS=0. Before pushing I ran the tests (native arm64), Windows clippy, the policy check, a release build, and one headless game run of the exact tree (170 s, Series S): menus at 30 fps, the world entered at about 81 s and kept presenting to the end at about 1 frame a second, 0 GPU errors, no crash. I did not check the lighting result itself. Thanks.
This discussion has been locked. Commenting is limited to contributors.
No description provided.