GPU: give depth targets their unordered access through a twin texture #469

Closed
github-bot wants to merge 1 commit from gh-pr/469/head into gh-pr/469/base
Owner

@⁠AnoHunted on GitHub · 2026-11-24 03:47 UTC · original
AnoHunted:depth-uav-twin → main · +431 −9 · 4 files · 1 commits · closed without merging

What

A title texture that is both a depth target and unordered access (flags DEPTH | UNORDERED) now gets a second
host texture, its twin, and its unordered access views are made on the twin. This is the plan in the code comment
in resource::host_texture. The twin has the depth's description, with ALLOW_UNORDERED_ACCESS in place of
ALLOW_DEPTH_STENCIL, in the typeless format of the depth's family: R16_TYPELESS for D16, R32_TYPELESS for
D32.

Lists keep the two coherent where they already track texture states. That is HostList::transition, which
ensure_state, the title's transition barriers and every draw's and dispatch's readable go through:

  • The first unordered access use in a list copies the depth into the twin (CopyResource), then moves the twin to
    UNORDERED_ACCESS. The depth itself is not touched while the twin is in use.
  • The next use of any other kind copies the twin back first. That covers depth target, shader view, copy, clear,
    and a title barrier out of UNORDERED_ACCESS.
  • A list that ends with the twin ahead copies it back in HostList::close. Every list starts and ends with the
    depth current, so the order lists are recorded and submitted in does not matter.

Also:

  • The title's UAV barriers on such a texture name the twin (list::host_barrier).
  • A view that names a depth format, the typeless one or no format gets the color format with the depth's bits:
    R16_UNORM for D16, R32_FLOAT for D32. A view with no description becomes mip 0, every array slice. Other
    formats of the family (R16_FLOAT, R16_UINT, R32_UINT, ...) stay as the title asked.
  • No twin is made for depth formats whose family has no unordered access format (D24S8, D32S8 and the console's
    16-bit depth with stencil), for multisampled depth, or for depth that is not 2D. Those behave as before, with
    one log line.

Files: resource.rs (the twin, host_unordered), host.rs (transition split into track and
sync_twin/copy_texture, the copy-back in close), descriptor/host.rs (views on the twin), list.rs (UAV
barriers).

Why

D3D12 does not allow ALLOW_DEPTH_STENCIL together with ALLOW_UNORDERED_ACCESS on one resource. The console
allows both, and the title's shadow maps ask for it (the code comment says so). Until now the host copy lost its
UAV use. descriptor::host::fits then refused every unordered access view of it and logged
view the host does not allow with unordered=true format=53 flags=2 (R16_TYPELESS, depth only). The slot
stayed a null view, and what compute shaders wrote through it was dropped.

Why plain copies: Microsoft's CopyResource and CopyTextureRegion pages allow copies between formats that are
"identical or at least from the same type group", and say a depth-stencil resource can be the source or the
destination. D16_UNORM is in the R16_TYPELESS group and D32_FLOAT is in R32_TYPELESS. A depth texture that
shaders may read already has a typeless host copy (DEPTH_FORMATS), so in that case the two textures have the same
format. No full-screen pass writing SV_Depth is needed. The test below checks that the bits survive both
directions.

Why a typeless twin rather than R16_UNORM/R32_FLOAT: fits gives a view of a typed texture the texture's own
format. A title view in R16_UINT of a R16_UNORM twin would be written as UNORM. A typeless twin keeps the
title's view format. Only views with no color format get the one with the depth's bits.

Cost:

  • Textures without a twin pay one Option check per state transition and nothing per draw.
  • A twin-backed texture costs a second texture of its size.
  • Each list that gives it unordered access does two full copies, one in and one back. That is per list, not per
    draw or dispatch.

Tested

Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1; also WARP (VICTORY_D3D12_WARP=1, diagnostics build).

New host GPU test resource::tests::unordered_writes_reach_a_depth_target:

  • Setup: an 8x8 D16 title texture with depth and unordered access, made with create_committed. Its R16_UNORM
    UAV is made into a host heap by the same make that draws use. A cs_5_1 shader (system compiler) writes a
    value to its first N columns. Before each dispatch the test calls list::ensure_state(.., UNORDERED_ACCESS), as
    readable does.
  • List 1: clears the depth to 0.25 through a DSV, writes 0.75 to columns 0-3, and ends.
  • List 2: writes 0.5 to columns 0-1, then moves the depth to COPY_SOURCE and copies the depth texture itself to
    a readback buffer.
  • Checks every texel: columns 0-1 are 0.5, 2-3 are 0.75 (list 1's writes, copied back at its close and copied in
    again by list 2), and 4-7 are 0.25 (the clear, kept through both copy-ins). It uses HostList and
    Host::execute, so the preludes and state tracking are the real ones.
    • The same test on main's code (test only, without the fix) fails on the RTX 5080 and on WARP:
      texel (0, 0): 0x4000, wanted 0.5 (0x8000). The write is lost and the depth still holds the clear.
    • Branch: passes on the RTX 5080 (plain and diagnostics builds) and on WARP.

Whole runs, main (d2a4bae) and branch side by side in one job:

  • cargo test --workspace (RTX 5080): main 264 passed, 0 failed, 4 ignored. Branch 265 passed, 0 failed,
    4 ignored (the new test).
  • cargo test -p victory-gpu --features diagnostics on WARP: main 45 passed, 0 failed, 1 ignored. Branch 46
    passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-gpu --all-targets: the same single warning on both, in host.rs:267, which this
    change does not touch.
  • cargo xtask policy: 151 files ok on both.
  • rustfmt --check on the four files: the same number of reported spots on both, none on lines this change adds
    (the files were not rustfmt-clean before).

The test machine has no D3D12 debug layer (D3D12GetDebugInterface returns 0x887A002D), so there was no
debug-layer or GPU-based validation check of the copies and barriers.

Not tested

  • The title: whether its shadow-map passes now produce the right shadows, and what the two copies per list cost
    there.
  • D32 depth (the test covers D16; the code path is the same with the other table entry), arrays, mips, and views
    with no description.
  • The title's own barriers on such a texture (host_barrier kinds 0 and 2). The test goes through
    ensure_state, which ends in the same HostList::transition.
  • AMD and Intel GPUs. Typed UAV loads of R16_UNORM need TypedUAVLoadAdditionalFormats; stores do not.
  • The D3D12 debug layer and GPU-based validation (not installed on the test machine).
  • Known gap, marked shortcut: in sync_twin: a dispatch that reads the depth through a shader view after its
    UAV was readied in the same dispatch loses its writes, because the read copies the twin back first. Whole-texture
    states cannot hold both anyway. Views reached only by direct heap indexing are not transitioned, as before.

No game files were used.

**@⁠AnoHunted** on GitHub · 2026-11-24 03:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/469) `AnoHunted:depth-uav-twin` → `main` · +431 −9 · 4 files · 1 commits · closed without merging ## What A title texture that is both a depth target and unordered access (flags `DEPTH | UNORDERED`) now gets a second host texture, its twin, and its unordered access views are made on the twin. This is the plan in the code comment in `resource::host_texture`. The twin has the depth's description, with `ALLOW_UNORDERED_ACCESS` in place of `ALLOW_DEPTH_STENCIL`, in the typeless format of the depth's family: `R16_TYPELESS` for D16, `R32_TYPELESS` for D32. Lists keep the two coherent where they already track texture states. That is `HostList::transition`, which `ensure_state`, the title's transition barriers and every draw's and dispatch's `readable` go through: - The first unordered access use in a list copies the depth into the twin (`CopyResource`), then moves the twin to `UNORDERED_ACCESS`. The depth itself is not touched while the twin is in use. - The next use of any other kind copies the twin back first. That covers depth target, shader view, copy, clear, and a title barrier out of `UNORDERED_ACCESS`. - A list that ends with the twin ahead copies it back in `HostList::close`. Every list starts and ends with the depth current, so the order lists are recorded and submitted in does not matter. Also: - The title's UAV barriers on such a texture name the twin (`list::host_barrier`). - A view that names a depth format, the typeless one or no format gets the color format with the depth's bits: `R16_UNORM` for D16, `R32_FLOAT` for D32. A view with no description becomes mip 0, every array slice. Other formats of the family (`R16_FLOAT`, `R16_UINT`, `R32_UINT`, ...) stay as the title asked. - No twin is made for depth formats whose family has no unordered access format (D24S8, D32S8 and the console's 16-bit depth with stencil), for multisampled depth, or for depth that is not 2D. Those behave as before, with one log line. Files: `resource.rs` (the twin, `host_unordered`), `host.rs` (`transition` split into `track` and `sync_twin`/`copy_texture`, the copy-back in `close`), `descriptor/host.rs` (views on the twin), `list.rs` (UAV barriers). ## Why D3D12 does not allow `ALLOW_DEPTH_STENCIL` together with `ALLOW_UNORDERED_ACCESS` on one resource. The console allows both, and the title's shadow maps ask for it (the code comment says so). Until now the host copy lost its UAV use. `descriptor::host::fits` then refused every unordered access view of it and logged `view the host does not allow` with `unordered=true format=53 flags=2` (`R16_TYPELESS`, depth only). The slot stayed a null view, and what compute shaders wrote through it was dropped. Why plain copies: Microsoft's `CopyResource` and `CopyTextureRegion` pages allow copies between formats that are "identical or at least from the same type group", and say a depth-stencil resource can be the source or the destination. `D16_UNORM` is in the `R16_TYPELESS` group and `D32_FLOAT` is in `R32_TYPELESS`. A depth texture that shaders may read already has a typeless host copy (`DEPTH_FORMATS`), so in that case the two textures have the same format. No full-screen pass writing `SV_Depth` is needed. The test below checks that the bits survive both directions. Why a typeless twin rather than `R16_UNORM`/`R32_FLOAT`: `fits` gives a view of a typed texture the texture's own format. A title view in `R16_UINT` of a `R16_UNORM` twin would be written as UNORM. A typeless twin keeps the title's view format. Only views with no color format get the one with the depth's bits. Cost: - Textures without a twin pay one `Option` check per state transition and nothing per draw. - A twin-backed texture costs a second texture of its size. - Each list that gives it unordered access does two full copies, one in and one back. That is per list, not per draw or dispatch. ## Tested Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1; also WARP (`VICTORY_D3D12_WARP=1`, diagnostics build). New host GPU test `resource::tests::unordered_writes_reach_a_depth_target`: - Setup: an 8x8 D16 title texture with depth and unordered access, made with `create_committed`. Its `R16_UNORM` UAV is made into a host heap by the same `make` that draws use. A `cs_5_1` shader (system compiler) writes a value to its first N columns. Before each dispatch the test calls `list::ensure_state(.., UNORDERED_ACCESS)`, as `readable` does. - List 1: clears the depth to 0.25 through a DSV, writes 0.75 to columns 0-3, and ends. - List 2: writes 0.5 to columns 0-1, then moves the depth to `COPY_SOURCE` and copies the depth texture itself to a readback buffer. - Checks every texel: columns 0-1 are 0.5, 2-3 are 0.75 (list 1's writes, copied back at its close and copied in again by list 2), and 4-7 are 0.25 (the clear, kept through both copy-ins). It uses `HostList` and `Host::execute`, so the preludes and state tracking are the real ones. - The same test on main's code (test only, without the fix) fails on the RTX 5080 and on WARP: `texel (0, 0): 0x4000, wanted 0.5 (0x8000)`. The write is lost and the depth still holds the clear. - Branch: passes on the RTX 5080 (plain and diagnostics builds) and on WARP. Whole runs, main (`d2a4bae`) and branch side by side in one job: - `cargo test --workspace` (RTX 5080): main 264 passed, 0 failed, 4 ignored. Branch 265 passed, 0 failed, 4 ignored (the new test). - `cargo test -p victory-gpu --features diagnostics` on WARP: main 45 passed, 0 failed, 1 ignored. Branch 46 passed, 0 failed, 1 ignored. - `cargo clippy -p victory-gpu --all-targets`: the same single warning on both, in `host.rs:267`, which this change does not touch. - `cargo xtask policy`: 151 files ok on both. - `rustfmt --check` on the four files: the same number of reported spots on both, none on lines this change adds (the files were not rustfmt-clean before). The test machine has no D3D12 debug layer (`D3D12GetDebugInterface` returns `0x887A002D`), so there was no debug-layer or GPU-based validation check of the copies and barriers. ## Not tested - The title: whether its shadow-map passes now produce the right shadows, and what the two copies per list cost there. - D32 depth (the test covers D16; the code path is the same with the other table entry), arrays, mips, and views with no description. - The title's own barriers on such a texture (`host_barrier` kinds 0 and 2). The test goes through `ensure_state`, which ends in the same `HostList::transition`. - AMD and Intel GPUs. Typed UAV loads of `R16_UNORM` need `TypedUAVLoadAdditionalFormats`; stores do not. - The D3D12 debug layer and GPU-based validation (not installed on the test machine). - Known gap, marked `shortcut:` in `sync_twin`: a dispatch that reads the depth through a shader view after its UAV was readied in the same dispatch loses its writes, because the read copies the twin back first. Whole-texture states cannot hold both anyway. Views reached only by direct heap indexing are not transitioned, as before. No game files were used.
The host refuses textures that are both depth targets and unordered
access, so the host copy of such a title texture (the title's shadow
maps) lost its UAV use, every unordered access view of it was refused
("view the host does not allow", unordered=true format=53 flags=2) and
what compute shaders wrote through it never landed.

Such a texture now gets a twin: the same description with unordered
access instead of depth, in the typeless format of the depth's family
(R16 for D16, R32 for D32), and its unordered access views and UAV
barriers name the twin. Views that name a depth format or none take
the color format with the depth's bits (R16_UNORM, R32_FLOAT).

Lists keep the two coherent where they already track texture states
(HostList::transition): unordered access after other use copies the
depth into the twin, other use after it copies the twin back, and a
list that ends with the twin ahead copies it back as it closes, so
every list starts and ends with the depth current. CopyResource takes
depth resources and formats of one type group, so plain copies keep
the bits. Textures without a twin pay one check per transition.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:07 +00:00
github-bot closed this pull request 2026-11-26 04:05:07 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:51 UTC · original

Verified c47c963 on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 44 passed, 0 failed, 1 ignored.

Not covered: a depth target actually sampled and written as UAV in the title (no game data here).

**@⁠DaviTostes** on GitHub · 2026-11-24 03:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/469#issuecomment-7121481461) Verified `c47c963` on **Arch Linux x86_64**, Windows MSVC release build under **Wine 11.22** (`victory-gpu`, `--test-threads=1`): **44 passed, 0 failed, 1 ignored**. Not covered: a depth target actually sampled and written as UAV in the title (no game data here).
Author
Owner

@⁠Raxstreams on GitHub · 2026-11-24 04:05 UTC · original

Tested on a local integration with packed HDR aliases and native command-list continuations. The unordered_writes_reach_a_depth_target GPU readback passed, including the twin-to-depth copy. The combined GPU suite completed with 126 passed and four ignored.

**@⁠Raxstreams** on GitHub · 2026-11-24 04:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/469#issuecomment-7121694649) Tested on a local integration with packed HDR aliases and native command-list continuations. The unordered_writes_reach_a_depth_target GPU readback passed, including the twin-to-depth copy. The combined GPU suite completed with 126 passed and four ignored.
Author
Owner

@⁠qntic on GitHub · 2026-11-24 09:30 UTC · original

I've independently implemented D32S8 depth-plane UAV support, as well as stencil-plane UAV support for both D24S8 and D32S8.

The stencil implementation preserves depth values while allowing stencil writes across command lists.

Both implementations pass hardware regression tests on an RX 6600 XT under native Windows, including D3D12 debug-layer validation, GPU-based validation and negative controls.

I noticed MichaelSmithAI's D32S8 PR. I'll compare our implementations to avoid duplicating work and share the additional stencil support.

**@⁠qntic** on GitHub · 2026-11-24 09:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/469#issuecomment-7127502898) I've independently implemented D32S8 depth-plane UAV support, as well as stencil-plane UAV support for both D24S8 and D32S8. The stencil implementation preserves depth values while allowing stencil writes across command lists. Both implementations pass hardware regression tests on an RX 6600 XT under native Windows, including D3D12 debug-layer validation, GPU-based validation and negative controls. I noticed MichaelSmithAI's D32S8 PR. I'll compare our implementations to avoid duplicating work and share the additional stencil support.
Author
Owner

@⁠sambagarandawertanda on GitHub · 2026-11-24 10:44 UTC · original

An independent RTX 3080 check for the D16 twin texture approach. On native Windows with NVIDIA driver 621.14, the runtime rejected one R16_TYPELESS resource with both depth and UAV flags (E_INVALIDARG). Separate depth and UAV resources allocated successfully, and the adapter reported typed R16_UNORM UAV load and store support.

I cleared a 16×8 D16_UNORM depth view to 0.25 with a 0.75 inset, then used CopyResource to bring it into the R16_UNORM UAV view. All 128 texels matched on GPU readback (16384 outside, 49151 inside). A compute shader complemented those values and copied the twin back to depth. All 128 matched again, with the two values swapped. The queue fence completed and the device stayed available.

This was a standalone synthetic test of the copy path with explicit barriers. The debug layer was unavailable. VIctory’s list state tracking, shadow-map output, and copy cost in the title still need testing on this card.

**@⁠sambagarandawertanda** on GitHub · 2026-11-24 10:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/469#issuecomment-7129178889) An independent RTX 3080 check for the D16 twin texture approach. On native Windows with NVIDIA driver 621.14, the runtime rejected one `R16_TYPELESS` resource with both depth and UAV flags (`E_INVALIDARG`). Separate depth and UAV resources allocated successfully, and the adapter reported typed `R16_UNORM` UAV load and store support. I cleared a 16×8 `D16_UNORM` depth view to 0.25 with a 0.75 inset, then used `CopyResource` to bring it into the `R16_UNORM` UAV view. All 128 texels matched on GPU readback (16384 outside, 49151 inside). A compute shader complemented those values and copied the twin back to depth. All 128 matched again, with the two values swapped. The queue fence completed and the device stayed available. This was a standalone synthetic test of the copy path with explicit barriers. The debug layer was unavailable. VIctory’s list state tracking, shadow-map output, and copy cost in the title still need testing on this card.
Author
Owner

@⁠qntic on GitHub · 2026-11-24 11:11 UTC · original

ive published my d32s8 depth and stencil uav implementations here:

https://github.com/qntic/VIctory/tree/pub/depth-stencil-uav

346 tests passed, including hardware tests on rx 6600 xt, gpu-based validation and negative controls. clippy and warp tests also pass.

i cant open a pr because of the current repository restrictions, but feel free to review or cherry-pick my two commits if useful.

**@⁠qntic** on GitHub · 2026-11-24 11:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/469#issuecomment-7129777268) ive published my d32s8 depth and stencil uav implementations here: https://github.com/qntic/VIctory/tree/pub/depth-stencil-uav 346 tests passed, including hardware tests on rx 6600 xt, gpu-based validation and negative controls. clippy and warp tests also pass. i cant open a pr because of the current repository restrictions, but feel free to review or cherry-pick my two commits if useful.
Author
Owner

@⁠qntic on GitHub · 2026-11-24 11:16 UTC · original

hey @⁠Marvisak , would it be possible to get access to open PRs?
ive been contributing hardware regression tests, depth/stencil uav fixes and memory diagnostics on my rx 6600 xt. some of my tests have already been used by another contributor to identify issues on polaris.

i have two branches ready for review, with passing workspace tests and clippy checks:

https://github.com/qntic/VIctory/tree/pub/depth-stencil-uav

https://github.com/qntic/VIctory/tree/pub/memory-diagnostics

happy to keep contributing and testing on amd hardware.

**@⁠qntic** on GitHub · 2026-11-24 11:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/469#issuecomment-7129887516) hey @⁠Marvisak , would it be possible to get access to open PRs? ive been contributing hardware regression tests, depth/stencil uav fixes and memory diagnostics on my rx 6600 xt. some of my tests have already been used by another contributor to identify issues on polaris. i have two branches ready for review, with passing workspace tests and clippy checks: https://github.com/qntic/VIctory/tree/pub/depth-stencil-uav https://github.com/qntic/VIctory/tree/pub/memory-diagnostics happy to keep contributing and testing on amd hardware.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!469
No description provided.