Skip to content

d3d12: forward ClearDepthStencilView on console command lists - #2451

Merged
tedin7 merged 2 commits into
R0Y00:mainfrom
Holastor:d3d12/clear-depth-stencil-view
Nov 25, 2026
Merged

tedin7 merged 2 commits into
R0Y00:mainfrom
Holastor:d3d12/clear-depth-stencil-view

Conversation

@Holastor

@Holastor Holastor commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Description

Implements CommandList slot 51 (ClearDepthStencilView) on console command lists in d3d12_xs.dll.

Details

  • Dispatches through both tables[list_kind][51] and list_typed[51], preserving x64 float parameter delivery (XMM3 for Depth).
  • Resolves the CPU descriptor handle through shadow view records with creation map fallback (view_target(handle, 21)).
  • Validates flags: rejects empty/zero flags.
  • Validates read-only plane flags: refuses depth clears on D3D12_DSV_FLAG_READ_ONLY_DEPTH, and stencil clears on D3D12_DSV_FLAG_READ_ONLY_STENCIL.
  • Masks out D3D12_CLEAR_FLAG_STENCIL if target format lacks a stencil plane.
  • Checks rect_count and bounds readability.
  • Automatically transitions the depth target to D3D12_RESOURCE_STATE_DEPTH_WRITE on the desktop command list.
  • Increments render_clears and render_target_unresolved diagnostic counters.

Test Coverage

Added synthetic test tests/startup_clear_depth_stencil.cpp:

  • Valid ClearDepthStencilView on 1x1 depth surface with depth 0.25f.
  • Valid ClearDepthStencilView on 2x2 depth surface with depth 0.75f and explicit rect.
  • Negative control: Read-only DSV clear refusal (verifying surface is not overwritten).
  • Negative control: RTV descriptor passed as DSV (wrong kind refusal and counter increment).
  • Negative control: Empty descriptor handle (unresolved refusal and counter increment).
  • Negative control: Zero clear flags refusal.
  • Verified readback on desktop D3D12 device matching expected bit patterns.
  • Verified GetDeviceRemovedReason() == S_OK and 0 debug layer errors on both WARP and physical hardware.

Evidence

ctest --preset dev --no-tests=error -R 'startup_clear_depth_stencil' -V

Observed:

1/2 Test #282: startup_clear_depth_stencil_warp .......   Passed    0.12 sec
2/2 Test #283: startup_clear_depth_stencil_hardware ...   Passed    0.20 sec
100% tests passed, 0 tests failed out of 2

Release build:

ctest --preset release --no-tests=error -R 'startup_clear_depth_stencil|startup_clear_uav' -V

Observed:

100% tests passed, 0 tests failed out of 4

Environment:

  • Windows 11 x64
  • Adapter: AMD Radeon RX 6600M & Microsoft Basic Render Driver (WARP)
  • Driver: AMD Software / Adrenalin
  • D3D12 Debug Layer: clean (0 errors)

Build commit

Head commit: 248f9151 (d3d12: document why slot 51 is set in both table paths), based on b312f9da (d3d12: forward ClearDepthStencilView on console command lists). All test runs above were executed against b312f9da; the comment-only commit does not change behavior.

Reviewer notes

@Holastor
Holastor marked this pull request as ready for review November 25, 2026 17:23

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head b312f9da (comment only, no Windows run on my side).

Issue 1 (scope): this branch carries other work.
git log origin/main..HEAD shows 4 commits, and only the last one is the ClearDepthStencilView change:

  • d97f04ab d3d12: support unplaced buffer memory SRVs in device slot 70
  • 0bc0b2b8 Merge branch 'gpu/slot70-unplaced-buffer-backing'
  • 87a23b30 d3d12: reject unplaced buffer memory SRVs overlapping live resources
  • b312f9da d3d12: forward ClearDepthStencilView on console command lists

The first three are the content of open PR #2395. In the diff that is about 180 lines in src/startup_stubs/d3d12_memory_srv.inl, the helpers in memory_srv_buffer.h, the clear_unplaced_buffers hook in release(), two mutation controls in CMakeLists.txt, and a docs section. None of it is in the title or body. Same shape as #2473 from the same author.
Fix: rebase after #2395 merges, or drop those three commits so the diff is only slot 51 (d3d12_xs_device.cpp, tests/startup_clear_depth_stencil.cpp, its CMake entry).

Issue 2 (possible duplicate): list slot 51 was already proposed in #1806 ("startup: clear depth and stencil views (command-list slot 51)", closed, not merged). Main has no slot 51 handler today, so this is not a conflict with merged code. A maintainer should decide whether #1806 and this PR cover the same slice before either goes on.

Issue 3 (Windows results): the body has no head SHA and no build commit. The dev and release numbers in the body cannot be tied to b312f9da. Please state the commit those runs used.

What looks fine

  • list_clear_depth_stencil follows the sibling list_clear_target (slot 52): shadow-record view lookup via resolve_view(desktop, 21), transition to DEPTH_WRITE, render_clears and render_target_unresolved counters.
  • Refusals are covered by the test: zero flags, read-only depth view, RTV passed as DSV, empty handle.
  • Stencil flag is masked off for formats without a stencil plane, and the call returns if nothing is left.
  • tables[list_kind][51] is also set directly, which the sibling slots 52 to 55 do not do. Harmless, but worth a one-line reason in the code if it is needed.
  • docs/startup-runtime.md changes in this diff belong to the slot 70 commits only.

Commands run locally (Linux)

  • python3 tests/docs_links_check.py -> passed (19 files, 464 links)
  • python3 tests/bidi_controls_check.py -> passed
  • git diff --check origin/main...HEAD -> clean
  • The D3D12 tests need Windows headers, so I did not build them or run mutations on Linux.

AI-assisted; commands listed were executed locally.

@ChazY11

ChazY11 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Built and run here: 2/2 pass on WARP and a physical GPU; five mutations, two caught, three survivors — and one of my earlier runs was void, so here is the corrected set

ctest --preset dev -R 'clear_depth_stencil'
    1/2 startup_clear_depth_stencil_warp .......  Passed   0.48 sec
    2/2 startup_clear_depth_stencil_hardware ...  Passed   2.93 sec
    100% tests passed, 0 tests failed out of 2

First, a correction about my own method, because it produced three bogus results before I noticed: I tried
$s.Replace(old, new, 1) in PowerShell, which has no such overload, so the mutated text was never written, the build
failed, and the harness dutifully reported "caught" for all three. Those rows are void. The set below was produced with a
single-occurrence substring splice and a build check on every iteration.

Five mutations of list_clear_depth_stencil

mutation result
zero clear flags accepted (!(flags & (DEPTH | STENCIL)) → false) caught
depth clear on a READ_ONLY_DEPTH view accepted caught
stencil clear on a READ_ONLY_STENCIL view accepted not caught
the STENCIL bit is no longer dropped when the format has no stencil not caught
the early return after that drop is removed (if (!flags) return; → if (false) return;) not caught

The two catches are the important pair: both are cases where passing the call through would make the driver raise a
debug-layer error rather than return something wrong, and the fixture pins them.

The third one is the asymmetric survivor, and it is worth a look: depth-on-read-only is caught, stencil-on-read-only is
not. The two guards are written identically twenty lines apart

if ((flags & D3D12_CLEAR_FLAG_DEPTH)   && (view.dsv_flags & D3D12_DSV_FLAG_READ_ONLY_DEPTH))   { … return; }
if ((flags & D3D12_CLEAR_FLAG_STENCIL) && (view.dsv_flags & D3D12_DSV_FLAG_READ_ONLY_STENCIL)) { … return; }

so a fixture that creates one read-only-depth view and one read-only-stencil view would cover both — or, if the intent
is that a read-only stencil view cannot exist without a read-only depth view, say so in a comment and the asymmetry is
explained rather than accidental.

The fourth and fifth are the "format has no stencil" normalisation: clearing stencil on an R32_FLOAT DSV drops the bit
and then returns if nothing is left. Neither the drop nor the subsequent return is asserted, so the fixture never clears
stencil on a stencil-less format. One case each would pin them, and the pair matters together — dropping the bit without
the early return would issue a depth-only clear when the caller asked for both.

What I did not do

I did not read tests/startup_clear_depth_stencil.cpp (331 lines) closely enough to say which rect-count and
dsv_flags combinations it builds; the survivors above are states the file demonstrably does not reach, not states I
read it to exclude. If a stencil-less-format case is already there and my mutation is inert for another reason, tell me
and I will re-check rather than leave the claim standing.

Environment

Windows 11 build 26300.9974; WARP and the installed physical GPU; startup_clear_depth_stencil built with
cmake --build --preset dev --target …. Each mutation was built (build verified separately from the test result),
run, reverted and rebuilt; final state 2/2 green.

AI-assisted.

@Holastor
Holastor force-pushed the d3d12/clear-depth-stencil-view branch from 248f915 to e8eb303 Compare November 25, 2026 20:28
Holastor and others added 2 commits November 26, 2026 01:30
Implement list slot 51 (ClearDepthStencilView) in d3d12_xs.dll. Resolves the
descriptor through the DSV shadow records with creation map fallback, validates
readable rect bounds and non-zero flags, checks against read-only DSV plane flags,
and transitions the depth target to D3D12_RESOURCE_STATE_DEPTH_WRITE before
forwarding to the desktop graphics command list.

Adds synthetic fixture tests/startup_clear_depth_stencil.cpp checking WARP and
hardware backends, verifying valid depth clears with and without rects, read-only
DSV refusal, non-DSV (RTV) handle rejection, and empty descriptor handling.

# Conflicts:
#	CMakeLists.txt
Add a one-line reason for setting tables[list_kind][51] directly while
siblings 52-55 only write list_typed: the ClearDepthStencilView fixture
calls list vtable[51] directly via invoke(), so the slot must be reachable
through both the typed dispatch and the generic table path.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@Holastor
Holastor force-pushed the d3d12/clear-depth-stencil-view branch from e8eb303 to 282bdaa Compare November 25, 2026 20:33
@Holastor

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (99af3da0), dropping the three slot 70 backing commits (d97f04ab, 0bc0b2b8, 87a23b30) and resolving the CMakeLists.txt conflict with merged #2473.

The branch now carries only the two topic commits (76f7d8ae and 282bdaa6).

Commit 282bdaa6 adds the comment explaining why tables[list_kind][51] is populated alongside list_typed[51] (the test fixture exercises direct vtable slot invocation via invoke()).

Windows Verification

  • Build commit: 282bdaa6
  • Environment: Windows 11 x64, MSVC 19.44, AMD Radeon RX 6600M & Microsoft Basic Render Driver (WARP), D3D12 Debug Layer clean (0 errors, GetDeviceRemovedReason() == S_OK).

Dev (--preset dev):

cmake --build --preset dev --target startup_clear_depth_stencil
ctest --preset dev --no-tests=error -R '^startup_clear_depth_stencil' -V
1/2 Test #301: startup_clear_depth_stencil_warp .......   Passed    0.12 sec
2/2 Test #302: startup_clear_depth_stencil_hardware ...   Passed    0.14 sec
100% tests passed, 0 tests failed out of 2

Release (--preset release):

cmake --build --preset release --target startup_clear_depth_stencil
ctest --preset release --no-tests=error -R '^startup_clear_depth_stencil' -V
1/2 Test #301: startup_clear_depth_stencil_warp .......   Passed    0.10 sec
2/2 Test #302: startup_clear_depth_stencil_hardware ...   Passed    0.13 sec
100% tests passed, 0 tests failed out of 2

@Makto94

Makto94 commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Hardware run at head 282bdaa6 (build exit code 0).

Environment: NVIDIA GeForce RTX 3070 Ti, driver 32.0.16.2101 (NVIDIA 621.01) and Windows 11 Pro build 26300.9974 as reported by the system, dev preset (MSVC). The runs were serial (-j1). The debug layer was enabled (x2w-d3d12 debug layer: 0x00000000 enabled).

Command: ctest --preset dev --no-tests=error -j1 -V -R '^startup_clear_depth_stencil'

Result: 2/2 passed, 0 failed (startup_clear_depth_stencil_warp and startup_clear_depth_stencil_hardware). 0 capability skips (77).

startup_clear_depth_stencil_hardware (test 302) prints adapter=NVIDIA GeForce RTX 3070 Ti:

  • 1 valid clear of A to 0.25: refused +0 (expected +0)
  • 2 valid clear of B with rect to 0.75: refused +0 (expected +0)
  • 3 read-only DSV clear refusal: refused +0 (expected +0)
  • 4 RTV handle passed as DSV (wrong kind): refused +1 (expected +1)
  • 5 empty descriptor handle: refused +1 (expected +1)
  • 6 zero clear flags: refused +0 (expected +0)
  • 7 A depth: 0.2500 (expected 0.2500)
  • 8 B[0]–B[3]: each depth: 0.7500 (expected 0.7500)
  • device removed reason: 0x00000000, debug layer errors: 0

The WARP run (test 301, adapter=Microsoft Basic Render Driver) prints the same values for lines 1-8 and the same final line.

For information: in both tests the debug layer prints 2 messages with sev=2 id=821 (ClearDepthStencilView: The clear values do not match those passed to resource creation). They do not count as errors in the debug layer errors: 0 line.

This run covers NVIDIA and WARP only. Not covered: any test outside the filter above.

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head 282bdaa against current main (comment review, no approval).

What I checked

  • list_clear_depth_stencil (slot 51) in src/startup_stubs/d3d12_xs_device.cpp: it follows the sibling list_clear_target (slot 52). The view is resolved from the shadow record, with the creation map fallback now enabled for slot 21 in resolve_view. Zero flags, read-only depth, read-only stencil and unreadable rect arrays are all refused before the call. The target is moved to DEPTH_WRITE before the clear.
  • Table wiring: list_typed[51] and tables[list_kind][51] are both set, and the comment explains why. The depth float is a typed parameter, so x64 delivery stays correct.
  • CMake: the new test uses the same warp/hardware loop and properties as its neighbours.
  • Existing evidence on this head: the Windows CI job passes, and the contributor runs on WARP and on physical GPUs (see the thread) pass 2/2.

Non-blocking coverage gaps (also raised in the thread by ChazY11)

  • tests/startup_clear_depth_stencil.cpp builds only a read-only-depth view (line 254). The read-only-stencil guard is never exercised, so removing it is not caught.
  • No case clears stencil on a stencil-less format, so the stencil bit drop and the if (!flags) return; that follows it are not pinned. A follow-up with one case each would close this.

Linux commands run in a worktree of the head:

  • python3 tests/docs_links_check.py passes.
  • python3 tests/bidi_controls_check.py passes.
  • git diff --check origin/main...HEAD is clean.

Not run: Linux cannot build or run the D3D12 fixture, so I did not mutate the code myself. I rely on the Windows CI run and the contributor receipts in the thread.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 merged commit e9ae884 into R0Y00:main Nov 25, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants