Skip to content

D3D12.X: depth UAV twins on by default, stencil twins copied with the plane - #3290

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
Foniass:d3d12x-depth-twins-stencil
Nov 29, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
Foniass:d3d12x-depth-twins-stencil

Conversation

@Foniass

@Foniass Foniass commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

A title's tiled lighting tests depth EQUAL against a class buffer (a console D16S8 depth buffer, R24G8 on the PC) that only compute writes, through a UAV of that depth buffer. The PC has no UAV of a depth format, so depth_uav gives such a buffer a twin the shaders write instead, copied to and from the depth plane around the title's transitions. That was opt-in (XCL_DEPTH_UAV_TWIN=1) and covered the depth plane only.

  • Depth UAV twins are on by default (XCL_DEPTH_UAV_TWIN=0 turns them off).
  • The stencil plane's twin is copied too, through a buffer: twin to plane on the way out of UNORDERED_ACCESS, plane to twin on the way in, on direct lists (StencilCopy, list_x::stencil_copies_on). No colour format shares a family with a depth-stencil one, but the PC copies between a buffer and one plane.

This is one of the two pieces amd-dev used to light the retail world; the other, barrier groups (#146: #3287 / #3299), turns the title's enhanced barriers into the transitions that reach plan_barriers. This PR does not depend on it: without it, the title's legacy barriers still reach plan_barriers.

Review fixes (throny, andrewDoing):

  • A D24S8 buffer (console D16S8 and D24S8) went past plan_barriers as it came: only buffers whose depth plane can have a twin were taken, so its stencil twin was never copied, and its UNORDERED_ACCESS transition went to the PC, where a depth-stencil buffer cannot be in that state (andrewDoing's run: Close failed with E_INVALIDARG). Its transitions are now parked like the others (parked): its depth plane is not copied, its stencil twin is.
  • The stencil copy back into the plane had no guard like the depth plane's entered: after a way in on a compute list, or with a twin made since, it would write stale or empty twin contents over the plane. It now copies back only after a copied way in (Cell::stencil_entered).
  • kept() is back to tests only, and the module comment no longer names barrier_groups, which main does not have.
  • Tests: a_stencil_twin_is_copied_around_unordered_access_after_a_copied_way_in (console D16S8 and D32S8), and the D24S8 case in only_depth_plane_subresources_are_copied_and_other_buffers_go_as_they_came (now parked; before, it went as it came).

What is in it

  • crates/xcl/src/hle/d3d12x/depth_uav.rs: DEFAULT_ON = true, the stencil twin's buffer (through), StencilCopy, the stencil part of plan_barriers (parked, stencil_entered); with the underflow fix of amd-dev a46703b6 (then_some(sub - n) evaluated sub - n for every subresource, a panic in debug builds when sub < n)
  • crates/xcl/src/hle/d3d12x/list_x.rs: stencil_copies_on and its two calls around plane_copies_on

Tested

  • NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2114), AMD Ryzen 7 7800X3D, Windows 11 build 26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), debug tests, release smoke
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 4dd049f: 2605 passed, 4 failed, 49 ignored. The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (E_OUTOFMEMORY from CreateComputePipelineState, seeds 70 / 545 / 1019 / 1507). They fail the same way on this branch's base, main fa20459, on this machine, and pass here on newer main (Recompiler: early-exit-safe multi-wave s_barrier in direct DXIL #3264, rebased onto 0fe06eb, has 0 failures). SKIPPED GPU tests: 44 distinct notices, almost all no wave64.
  • Retail title, with the default on: 60 s smoke run below: no device removal or crash, frames and peak commit within the limits. The title makes depth twins in that run (its log: R16 twins of D16 buffers, depth UAV twin #1...). I did not check which scenes a 60 s run reaches, so it does not show whether the tiled lighting changed.

Regression run

main:

xcl-smoke v1
commit: 3b6a91ed096d6d6e0fee52cfb86dc0a7d7c8c1b0
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1193 (PresentX 1193)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15684 MB

this PR:

xcl-smoke v1
commit: 4dd049f646c1724444810a4a2ecc1775c5f8b23a
gpu: NVIDIA GeForce RTX 5070 Ti
title: 3cb1d8e0
seconds: 60
exit: 0x0
frames shown: 1662 (PresentX 1662)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17720 MB

Not tested

  • whether the world's tiled lighting looks right (amd-dev reports "the world is lit" with barrier groups too, on an RX 9070 XT); not checked here
  • AMD GPUs; titles other than this one
  • the stencil copies on compute or copy lists (only direct lists copy; others warn once, as for the depth plane)
  • the stencil copies on real hardware: the new test drives the planner with stand-in twins; no title run here makes a stencil twin

AI use

  • Claude Code with Claude Opus 5.5: ported the depth_uav part of amd-dev a68f609f and the fix of a46703b6 to main, resolved the conflicts, made the review fixes and their tests, ran the tests and the smoke run, wrote this description.
  • Original code: amd-dev a68f609f and a46703b6 (raylitebull, written with Claude Code).

🤖 Generated with Claude Code

@github-actions github-actions Bot added area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Nov 29, 2026
@andrewDoing

Copy link
Copy Markdown
Contributor

On 3370d35f, the DIRECT-list planner skips the stencil copy for console D16S8 (118), which becomes R24G8_TYPELESS (44). A test on an RTX 5070, driver 616.92, created the R8 stencil UAV twin successfully. With the stock planner predicate, recording two alternating stencil-plane updates produced zero stencil-copy messages and command-list Close returned E_INVALIDARG (0x80070057). The fixture stopped before submitting that list.

In depth_uav::plan_with, filter(plane_twinnable) admits formats 19, 39 and 53. The later stencil branch explicitly handles 19 and 44, so 44 cannot reach it. I repeated the same synthetic fixture in a fresh process with one test-only change to admit format 44 for stencil subresource 1 of this single-mip, single-slice resource. That recorded two copies in each direction, completed the fence, and matched both planes on all 256 texels after each round:

Round DSV update Guest stencil UAV rectangle Actual left / right stencil Actual depth on all texels (24-bit)
1 Depth 0.25, stencil 0x19 Left half to 0xA7 0xA7 / 0x19 0x400000
2 Depth only, 0.75 Right half to 0x53 0xA7 / 0x53 0xBFFFFF

The fixture used the guest stencil UAV creation, legacy ResourceBarrier and ClearUnorderedAccessViewUint paths, and read back the original depth/stencil resource. This points to separating stencil-copy eligibility from depth-plane-twin eligibility. The narrow diagnostic covered plane-specific DIRECT transitions on one resource. Whole-resource transitions, enhanced barriers and retail lighting remain untested here.

AI disclosure: OpenAI Codex prepared and ran the fixture and drafted this report.

@Kalmarv

Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

merge-check
base: fa20459
head: 3370d35
verdict: safe to merge

Merged cleanly onto origin/main (fa204595) in a 5-PR batch (#3288, #3290, #3292, #3294, #3300) and verified on NVIDIA GeForce RTX 5070 Ti with DXC 1.9.2609.5:

  • Targeted depth/stencil UAV twin GPU tests (gpu_a_stencil_uav_of_a_console_depth_buffer_is_a_view_of_its_twin, gpu_clear_uav_of_a_stencil_uav_clears_the_twin, gpu_a_stencil_uav_by_address_is_a_view_of_the_placed_buffers_twin): all passed on RTX 5070 Ti.
  • gpu::: 853 passed, 0 failed, 24 ignored (10.45 s, including fuzz_validate_and_gpu_0..3).
  • hle::d3d12x:: (--test-threads=1, skipping 2 known main #3187 uma_driver mixed-window tests): 1144 passed, 0 failed, 3 ignored (30.21 s).
  • Remaining xcl --lib (--test-threads=1): 0 failed.
  • -p xcl --test e2e (after rebuilding xcl-testtitle): 5 passed, 0 failed (including D3D12 debug layer + DRED).
  • GitHub CI (Build and test (Windows, no GPU) and PR description): green.

@github-actions github-actions Bot added needs-2nd-check Has 1 safe merge-check; needs second independent check (#3041) needs:amd-test Needs a test report or merge-check on an AMD GPU labels Nov 29, 2026
@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check claim
head: 3370d35

@throny

throny commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

merge-check
base: d8bd9a3
head: 3370d35
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2618 passed, 0 failed, 49 ignored: the same count as the base, so the suite runs no new test for this change (27 SKIPPED lines). Base alone at d8bd9a3: 2618 passed, 0 failed, 49 ignored.
review: three findings from reading the diff against main d8bd9a3. (1) The new stencil copy is selected with matches!(key.format, 19 | 44), but it runs only for objects that pass plane_twinnable, which on main accepts formats 19, 39 and 53. Format 44 never gets there, so its stencil twin is never copied although the code reads as if it were. (2) The copy from the stencil twin back into the plane on the way out of UNORDERED_ACCESS has no guard like the depth path's entered; on a compute list, or before the twin exists, the way in copies nothing, so the way out can write stale or empty twin contents over the plane's stencil. The missing guard is confirmed by reading; the effect is my reading of it, not a run. (3) kept() loses #[cfg(test)] and the module comment names a caller barrier_groups, which does not exist on main or in this diff, so this looks stacked on work that is not merged. On top of that the switch goes on by default while the new stencil copies and the staging buffer have no test (the existing tests only gain through: None). Cost when on: one twin per depth buffer the title gives unordered access (an R8 texture and a staging buffer more for a stencil twin), and for a depth-stencil target two copies and two to three barrier calls per subresource in each direction, never skipped. XCL_DEPTH_UAV_TWIN=0 turns it off.
performance: not measured.
verdict: not yet: fix (1) and (2) with a test of the stencil path, say what kept() is for on main (or stack the PR on its base), and show a title run with the default on.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K, Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).

… plane

A title's tiled lighting tests depth EQUAL against a class buffer (a
console D16S8 depth buffer, R24G8 on the PC) that only compute writes,
through a UAV of the depth buffer. The PC has no UAV of a depth format:
depth_uav gives such a buffer a twin the shaders write instead, copied
to and from the depth plane around the title's transitions. That was
opt-in (XCL_DEPTH_UAV_TWIN=1) and covered the depth plane only.

- Depth UAV twins are on by default (XCL_DEPTH_UAV_TWIN=0 turns them
  off).
- The stencil plane's twin is copied too, through a buffer (no colour
  format shares a family with a depth-stencil one, but the PC copies
  between a buffer and one plane): plane to twin on a DIRECT list's way
  into UNORDERED_ACCESS, twin to plane on the way out (StencilCopy,
  list_x::stencil_copies_on). Like the depth twin, it is copied back
  only after a copied way in (Cell::stencil_entered): a twin entered on
  a compute list, or made since, holds something else than the plane.
- A D24S8 buffer (console D16S8 and D24S8) has its transitions into and
  out of UNORDERED_ACCESS parked like the others (`parked`): its depth
  plane has no twin, but its stencil plane can, and the PC has no
  depth-stencil buffer in UNORDERED_ACCESS. Before, they went to the PC
  as they came, so its stencil twin was never copied (review on xcl-dev#3290).

Ported from amd-dev a68f609 (raylitebull, written with Claude Code),
the depth_uav part, with the stencil-subresource underflow fix of
a46703b (`then_some(sub - n)` evaluated sub - n for every sub).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Foniass
Foniass force-pushed the d3d12x-depth-twins-stencil branch from 3370d35 to 4dd049f Compare November 29, 2026 17:15
@github-actions github-actions Bot added needs-pr-check Failing the PR description check (CONTRIBUTING.md) and removed checks-disagree Merge-checks disagree (safe vs not yet); needs maintainer/3rd look needs:amd-test Needs a test report or merge-check on an AMD GPU needs-pr-check Failing the PR description check (CONTRIBUTING.md) labels Nov 29, 2026
@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks throny and andrewDoing, both findings were right. Fixed in 4dd049f:

  • (1) D24S8 buffers (format 44) now go through the planner (parked). Their transitions into and out of UNORDERED_ACCESS go to COPY_SOURCE like the others, so no depth-stencil buffer is left in UNORDERED_ACCESS on the PC. Their depth plane is not copied (it has no twin), and their stencil twin is. This is andrewDoing's change of admitting 44 for the stencil plane, without giving 44 a depth twin.
  • (2) The stencil copy back now needs a copied way in (Cell::stencil_entered, like entered for the depth plane). After a way in on a compute list, or with a twin made since the way in, nothing is copied back.
  • (3) kept() is back to #[cfg(test)], and the module comment no longer names barrier_groups. This PR does not depend on D3D12.X: translate list #146 enhanced barrier groups into legacy texture transitions #3287 / D3D12.X: a title's barrier groups (#146) move its textures between states #3299.
  • Tests: a_stencil_twin_is_copied_around_unordered_access_after_a_copied_way_in (console D16S8 and D32S8: no twin, twin made since, direct way in, compute way in, the stencil subresource alone). The existing D24S8 case now expects "parked".

About a title run with the default on: the smoke run in the description has it on, and the title makes depth twins there (R16 twins of D16 buffers, from its log). It makes no stencil twin in that run, so the stencil copies were only exercised by the planner tests. The PR says so under Not tested.

(Written with Claude Code, Claude Opus 5.5.)

@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Note for the maintainers: this PR and #3089 (imnicho) both change hle/d3d12x/depth_uav.rs and conflict with each other: a test merge of both onto main gives a content conflict there. Each merges cleanly on its own.

They also both handle R24G8 (console D16S8 / D24S8) depth buffers, in different ways:

Whichever merges second needs a rebase, and its author should check that the two R24G8 paths fit together: with both, an R24G8 buffer would have a depth twin, so this PR's depth-plane copies would apply to it as well.

(Written with Claude Code, Claude Opus 5.5.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:d3d12 D3D12.X runtime, command lists, resources, sparse memory gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants