Skip to content

D3D12.X: depth buffers that shaders write get a twin for their UAVs (#970) - #1063

Closed
IvyNotFound wants to merge 2 commits into
xcl-dev:mainfrom
IvyNotFound:depth-uav-twins
Closed

IvyNotFound wants to merge 2 commits into
xcl-dev:mainfrom
IvyNotFound:depth-uav-twins

Conversation

@IvyNotFound

@IvyNotFound IvyNotFound commented Nov 24, 2026 •

Copy link
Copy Markdown

Stacked on #1027; only the top commit (21aa52c) belongs here. Part of #970: the second step planned there, off by default.

A console depth buffer that shaders write too has no ALLOW_UNORDERED_ACCESS on the PC, and its UAVs were null views, so what the title's shaders wrote there was lost. With XCL_DEPTH_UAV_TWINS=1, such a depth buffer of one plane (R16 / R32 typeless) gets a twin: a texture of the same format and size with unordered access and no depth, kept as the depth buffer's private data (released with it). Its UAVs are views of the twin, and at the title's enhanced barriers (#146) into the UNORDERED_ACCESS layout the depth buffer is copied to the twin, at those out of it the twin back. Without the variable nothing changes.

XCL applies none of the title's enhanced barriers, so on the PC such a depth buffer stays in the state it was made in: the copies' transitions start from that state (kept at CreatePlacedResourceX, DEPTH_WRITE when not known). Depth + stencil formats keep null views: the PC has no UAV format for them.

What is in it

  • crates/xcl/src/hle/d3d12x/depth_twin.rs (new): enabled, twin_desc, twin_for_uav / twin_of (private data), note_created / forget, twin_uav_desc (a typed view for a UAV made without a description), sync (transitions + CopyResource either way; no transition of the depth buffer when it is in the copy's state already). Tests: a_d16_or_d32_depth_buffer_gets_a_twin_with_unordered_access, a_depth_buffer_in_the_copys_state_is_not_moved_to_it, a_uav_without_a_description_is_a_typed_view_of_the_twin, a_released_depth_buffers_state_is_forgotten; GPU test gpu_a_uav_write_to_a_depth_buffer_goes_through_its_twin (a D16 depth buffer's clear reaches the twin at the barrier into UAV, a write through the twin's UAV reaches the depth buffer at the barrier out of it; fails without the twin).
  • crates/xcl/src/hle/d3d12x/real.rs: dev_create_uav_x makes the view on the twin instead of a null view (typed when the title gave no description); res_release forgets the state of a released depth buffer.
  • crates/xcl/src/hle/d3d12x/list_x.rs: note_depth_uav_barrier_groups returns the copies a Compute dispatches: a shader takes no more arguments from user SGPRs than it has (XboxBvhBuilder builds empty BLAS / TLAS) #146 call needs (twin_copy); uav_barrier_x runs them after its global UAV barrier. The barrier log says whether the UAVs are null views or views of the twin.
  • crates/xcl/src/hle/d3d12x/console_list.rs: the same for console-recorded lists, recorded in the stream (TWIN_COPY_MARKER) and run at replay.
  • crates/xcl/src/hle/d3d12x/device_x.rs: create_placed_resource_x keeps the state such a depth buffer is made in.

Tested

  • NVIDIA GeForce RTX 5080 16 GB, driver 621.24 (32.0.16.2124); AMD Ryzen 7 7800X3D; Windows 11 25H2 (26200.9974); rustc 1.100.0 MSVC; DXC 1.9.2602.17; tests in debug, runs in release
  • cargo test --workspace --no-fail-fast -- --test-threads=4 at 21aa52c (rebased onto main b7aa9f3) with XCL_SAMPLE_RPF, _DLLS, _TTPF, _BVH4, _BK2, _AWC set: 777 passed, 0 failed, 41 ignored; SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware. Before the rebases, at 0322514, one run had the Tests: placed_title_textures_share_vram_and_preserve_pixels fails now and then at its cubemap reuse check (main 9ce2070) #718 flake (720 passed, 3 failed).
  • The retail title of Newer retail title (TitleID 7952987A): reaches world loading, then the title's GPU watchdog ("GPU Loc Sig") after ListX #101 stub #505, scripts/smoke.ps1, XCL_PRESS_A_AT=15:
    • the regression runs below (60 s, variable unset; main b7aa9f3 and this head 21aa52c, after the rebase);
    • 85 s with XCL_DEPTH_UAV_TWINS=1 at 0322514 (before the rebase, same code): 256+ D16 shadow maps get twins at ~9 s (the 4 depth + stencil views stay null); the enhanced barriers into and out of UNORDERED_ACCESS from ~66 s copied depth → twin and back 1024+ times; no device removal, the run ends by itself (exit 0x0, 1320 frames); peak commit 32058 MB (main's 60 s run: 27340 MB, its 300 s run: 30308 MB).
    • 300 s with XCL_DEPTH_UAV_TWINS=1 at 3ef1119 (before the review fixes): the same twins and copies, no device removal, it ends in the title's "GPU Loc Sig" abort at 95.5 s as main does.

Regression run

main:

xcl-smoke v1
commit: b7aa9f3bd212088e0d927439284dc5ed325ce3c7
gpu: NVIDIA GeForce RTX 5080
title: game
seconds: 60
exit: 0x0
frames shown: 1375 (PresentX 1375)
presents per 2 s (last 3): 36 53 51
device removals: 0
crashes: 0
peak commit: 26063 MB

this PR:

xcl-smoke v1
commit: 21aa52ccdd4ad1190b5f982855f4de2ef0771ff6
gpu: NVIDIA GeForce RTX 5080
title: game
seconds: 60
exit: 0x0
frames shown: 1371 (PresentX 1371)
presents per 2 s (last 3): 32 41 48
device removals: 0
crashes: 0
peak commit: 26176 MB

Not tested

AI use

  • Claude Code, Claude Opus 5.5: wrote the code, the tests and this description, and ran the tests and the runs above.

🤖 Generated with Claude Code

@certified-retart

Copy link
Copy Markdown
Contributor

Independent review and local GPU test of commit 3ef1119 (per CONTRIBUTING.md, reviewing/testing this PR rather than opening a duplicate for #970).

Confirmed code findings

  1. MADE_IN never removes released resources (depth_twin.rs / real.rs): note_created(res, state) inserts into the global MADE_IN map, but real::res_release never forgets res on final release. If a later depth buffer reuses the same COM pointer address (e.g. created via another path or in a different initial state), state_of(res) returns the stale state from the freed resource. Adding a depth_twin::forget(this as usize) call in real::res_release (when n == 0) fixes both the leak and pointer-reuse staleness.
  2. Same-state transition in depth_twin::sync when made == depth_copy: barrier(&[transition(depth, made, depth_copy), ...]) and the matching restore barrier emit a StateBefore == StateAfter transition if a depth+UAV texture was created in COPY_SOURCE (entering UAV) or COPY_DEST (leaving UAV), which D3D12 validation rejects. Omitting the depth transition when made == depth_copy avoids it.
  3. pDesc == NULL on typeless twins (real.rs dev_create_uav_x): f(this, twin.as_raw(), std::ptr::null_mut(), desc, at) forwards a null desc directly to the twin. Because pc_resource_desc converts typed depth formats (D16_UNORM / D32_FLOAT) to typeless (R16_TYPELESS / R32_TYPELESS), D3D12 rejects CreateUnorderedAccessView with pDesc == NULL on the twin unless a typed view desc is synthesized via pc_shader_view_format.
  4. (Minor) note_created is currently only hooked in device_x::create_placed_resource_x, so any depth+UAV resource created via dev_create_committed_resource_x or dev_create_placed_resource_x with a non-DEPTH_WRITE initial state falls back to DEPTH_WRITE.

Tested

  • Hardware: NVIDIA GeForce RTX 5070 (driver 32.0.16.1074), Ryzen 7 9800X3D, Windows 10 Pro 19045, rustc 1.100.0 (debug)
  • cargo test -p xcl --lib depth_twin -- --nocapture: 2 passed, 0 failed, 0 ignored (gpu_a_uav_write_to_a_depth_buffer_goes_through_its_twin ran on RTX 5070 and passed)
  • cargo test -p xcl --lib a_depth_buffer_without_unordered_access -- --nocapture: 1 passed, 0 failed, 0 ignored
  • cargo test -p xcl --lib an_enhanced_barrier_into_or_out_of_unordered_access -- --nocapture: 1 passed, 0 failed, 0 ignored

Not tested

  • Retail game smoke run (no game files on this machine), AMD GPU (gpu_a_uav_write_to_a_depth_buffer_goes_through_its_twin uses D3D12CreateDevice(None, ...)), D3D12 debug layer (not installed on this host).

AI use

  • Antigravity (gemini-4-neon) assisted with diff inspection and drafting this review; every finding above was checked against 3ef1119 and the test commands were run locally on the hardware listed.

@IvyNotFound

Copy link
Copy Markdown
Author

Thanks for the review. Points 1–3 were right; fixed in 0322514 (squashed into the PR's commit):

  1. res_release calls depth_twin::forget on the last release, so a resource made later at the same address does not take the old state (test a_released_depth_buffers_state_is_forgotten).
  2. sync leaves the depth buffer's transition out when it is in the copy's state already (depth_transitions, test a_depth_buffer_in_the_copys_state_is_not_moved_to_it).
  3. A UAV the title makes without a description gets a typed view of the twin, R16_UNORM / R32_FLOAT, an array view for cubes and cascades (twin_uav_desc, test a_uav_without_a_description_is_a_typed_view_of_the_twin).

Point 4 stays as it is in this PR: depth buffers from the other creation paths fall back to DEPTH_WRITE, listed under Not tested; the retail title's come through CreatePlacedResourceX.

At 0322514 on an RTX 5080 (driver 621.24): cargo test -p xcl --lib depth_twin 5 passed, 0 failed; the workspace suite and the runs are in the updated description (85 s with XCL_DEPTH_UAV_TWINS=1: 1024+ copies, no device removal).

AI use: Claude Code (Claude Opus 5.5) checked the findings against the code, wrote the fixes and this reply, and ran the tests.

@eizispark

Copy link
Copy Markdown
Contributor

Hardware test report — commit 0322514

Verified clean merge with main (47efa2e) on 0322514d51c6dcb03e5d2bfca6d75975f837eb6c: pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 720 passed, 3 failed, 41 ignored (GPU tests skipped on this adapter: none); the 3 failures under --test-threads=4 were the known concurrent global-state tests (hle::d3d12x::title_textures::tests::placed_title_textures_share_vram_and_preserve_pixels, hle::d3d12x::title_textures::tests::transitions_apply_when_their_list_is_submitted, hle::d3d12x::vram::tests::d32s8_targets_take_their_own_size_of_video_memory), each passing solo with --exact --test-threads=1 (1 passed, 0 failed)
  • cargo test -p xcl --lib depth_twin -- --nocapture: 5 passed, 0 failed, 0 ignored (0 skipped), including hle::d3d12x::depth_twin::tests::gpu_a_uav_write_to_a_depth_buffer_goes_through_its_twin executed on AMD Radeon RX 9070 XT (D16_UNORM clear → twin UAV write → copy-back round trip), plus hle::d3d12x::list_x::tests::a_depth_buffer_without_unordered_access_is_one_with_null_uavs and an_enhanced_barrier_into_or_out_of_unordered_access_is_a_uav_use (ok)
  • Additional 60 s retail smoke run with XCL_DEPTH_UAV_TWINS=1: exit: 0x0, frames shown: 1662 (PresentX 1662), presents per 2 s (last 3): 60 60 60, device removals: 0, crashes: 0, peak commit: 15028 MB

Regression run
main (47efa2e):

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1677 (PresentX 1677)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14853 MB

PR (0322514):

xcl-smoke v1
commit: 0322514d51c6dcb03e5d2bfca6d75975f837eb6c
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
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: 14856 MB

Not tested

  • 3D world shadow-map rendering past the menus; retail run under XCL_D3D12_DEBUG=1; NVIDIA / Intel GPUs

AI use: Antigravity (Google DeepMind), Gemini (exact version string not exposed in runtime environment) — ran the workspace test suite, depth_twin GPU tests, and 60 s retail smoke checks (default and XCL_DEPTH_UAV_TWINS=1) on the hardware above and formatted this report.

@juliptree

juliptree commented Nov 24, 2026 •

Copy link
Copy Markdown

Independently tested 0322514 on an RTX 5060 Ti with NVIDIA driver 616.92. The existing depth_twin tests passed 5/5 on the clean PR head. With a local test-only D32 extension, all 6 passed with zero skips.

After GPU fence completion, all 2,048 texels read back as 0.25 after the D32_FLOAT depth clear copied to the R32_FLOAT UAV twin, and as 0.75 after the UAV write copied back to the depth texture. I verified the actual D3D12 adapter by LUID.

The debug-layer rerun matched both values, but the debug interface returned 0x887A002D, so debug-layer messages were unavailable. I can share the synthetic D32 fixture if you’d like to include it.
The D32 round-trip fixture is available as a single test-only commit on top of 0322514. Feel free to cherry-pick it into this PR. The committed version passed all six depth_twin tests on the RTX 5060 Ti, with zero skips.

Codex (GPT-6) assisted with testing and reporting.

@JoaoPauluu

Copy link
Copy Markdown

A regression run under Wine on Linux, on a retail title.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. c1af15d as it is (merge base 47efa2e) and main 47efa2e, no uncommitted changes. Retail title 1.0.2.1, scripts/linux/smoke.sh (Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257) for 60 s, no input with XCL_DEPTH_UAV_TWINS=1 for this PR's run. Twelve runs in a row, main first and last, other PRs in between.
  • The PR's run had XCL_DEPTH_UAV_TWINS=1, and GTA VI reaches the change: at 39.18–39.23 s the log shows a depth buffer that shaders write (…, format 53) has a twin for its UAVs, up to (256 so far), for buffers from 128x128x6 to 2048x2048x1. There is no no twin line. The log's WARN (72), not kept in step (4) and ERROR (0) counts are the same as on main, and background_pipelines is the same [228, 0, 57595]. I did not look at the picture: under this setup it is black on main too.

Regression run

main (first run):

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1384 (PresentX 1384)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 0 MB

this PR:

xcl-smoke v1
commit: c1af15d61c9727c6e8691a2a48a4742ce009c3e7
gpu: NVIDIA GeForce RTX 3060
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1389 (PresentX 1389)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 0 MB

The last main run: 1425 frames, exit: 0x0, no device removal, no crash. Under Wine, peak commit reads 0 MB, so these blocks do not check memory.

Not tested

  • the test suite on this head; native Windows; AMD.

AI use

  • Claude Code (Claude Opus 5.5) built both commits, ran the runs and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

IvyNotFound and others added 2 commits November 24, 2026 17:02
…#970)

A console depth buffer that shaders write too has no ALLOW_UNORDERED_ACCESS
on the PC (pc_resource_desc drops it) and its UAVs are null views, so the
writes are lost. The title's barriers that use such a resource as a UAV are
now counted and logged (the first 8, then every power of two):
- transitions from or to UNORDERED_ACCESS and UAV barriers naming it, both
  for PC-format lists (resource_barrier_x) and console-recorded ones
  (console_list replay);
- enhanced texture barriers (xcl-dev#146, D3D12_BARRIER_GROUP) with the
  UNORDERED_ACCESS layout or access. XCL still turns xcl-dev#146 into one global
  UAV barrier; only the description is read here.
The null-view warning gives its total instead of stopping at 4.

Measured on the retail title of xcl-dev#505 (RTX 5080, 300 s runs, A at 15 s):
no legacy barrier touches these resources; enhanced barriers move D16
shadow maps (768x768x6, 1024x1024, 512x512) into or out of the
UNORDERED_ACCESS layout once the world starts loading (62-64 s; 12 in a
run with every one logged).

No behaviour change: every barrier goes to the PC as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…cl-dev#970)

A console depth buffer that is also a UAV has no ALLOW_UNORDERED_ACCESS on
the PC and its UAVs were null views: what the title's shaders wrote there
was lost (the retail title of xcl-dev#505 moves its D16 shadow maps into the
UNORDERED_ACCESS layout before it renders depth into them).

With XCL_DEPTH_UAV_TWINS=1, such a depth buffer of one plane (R16 / R32
typeless) gets a twin: a texture of the same format and size with unordered
access and no depth, kept as the depth buffer's private data. Its UAVs are
views of the twin, and at the title's enhanced barriers (xcl-dev#146) into the
UNORDERED_ACCESS layout the depth buffer is copied to the twin, at those
out of it the twin back (in console-recorded lists the copies are recorded
in the stream). XCL applies none of the title's enhanced barriers, so the
copies' transitions start from the PC state the depth buffer was made in
(kept at CreatePlacedResourceX; DEPTH_WRITE when not known). Depth and
stencil formats keep null views: the PC has no UAV format for them.

Off by default: without the variable nothing changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@github-actions

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 29, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 21aa52c

This is stacked on #1027 (its first commit) and adds depth_twin.rs behind XCL_DEPTH_UAV_TWINS=1. Main has since done the same for #970 in hle/d3d12x/depth_uav.rs (#3203; turned on by default in 4dd049f, #3290). A depth plane gets an R32/R16 twin with unordered access, its UAVs are views of the twin, and the plane is copied into the twin on the title's transition into UNORDERED_ACCESS and back on the transition out. Main also covers stencil planes and #146 barrier groups, and the module's header credits this PR for the PC creation state.
Merging this would add a second twin mechanism next to main's.
Recommendation: close (superseded by #3290 / #3203).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

@throny

throny commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded: this change is already on main (4dd049f). main depth_uav.rs (#3203, on by default via 4dd049f) has the R32/R16 depth twins copied around UAV transitions and credits #1063 in its header

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

@throny throny added the duplicate This issue or pull request already exists label Dec 1, 2026
@throny throny closed this Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

duplicate This issue or pull request already exists stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants