Skip to content

d3d12/vulkan: write NaN depth clears with a copy on AMD (D3D-CLEAR-NAN-DEPTH) - #787

Closed
bvtterbroot wants to merge 4 commits into
ProjectLeonida:mainfrom
bvtterbroot:nan-depth-clear
Closed

bvtterbroot wants to merge 4 commits into
ProjectLeonida:mainfrom
bvtterbroot:nan-depth-clear

Conversation

@bvtterbroot

Copy link
Copy Markdown

On AMD, d3d12_clear stays red even with #516: AMD's Vulkan driver stores 1.0 for a NaN depth clear value, so a D3D12 clear that WARP and native D3D12 store as NaN comes back as 1.0. This writes such a clear with a copy, which the driver does not touch, so the layer matches WARP on every vendor.

Builds on #516 (its two commits are the first two here; the change is the last commit). D3D-CLEAR-NAN-DEPTH, follow-up to #506. Supersedes the test-only draft #307, which reports the same driver behaviour on an RX 9070 XT.

What changes

  • runtime/src/vulkan_replay.h: with VK_EXT_depth_range_unrestricted enabled, a depth clear whose value is NaN on a D32_SFLOAT or D32_SFLOAT_S8_UINT depth plane is a vkCmdFillBuffer of the value's 32 bits into transfer scratch (one rectangle's worth, copy_scratch), then one vkCmdCopyBufferToImage region per cleared rectangle (the whole mip without rectangles), on the depth aspect only. A stencil cleared in the same call is still a clear, of the stencil aspect alone. Every other clear is unchanged, and without the extension NaN is still cleared to 0 as d3d12: use legal Vulkan depth clear values (D3D-CLEAR-DEPTH-RANGE) #516 does.
  • runtime/test/d3d12_clear_test.cpp: two more edge cases on D32_FLOAT_S8X24_UINT with DEPTH | STENCIL: NaN with stencil 0x11 / 0x22 in the rectangle, and NaN then 0.5 in the rectangle. Depth and stencil are checked against WARP and the expected value, in both the unrestricted and the --restricted-depth run.
  • docs/d3d12_vulkan.md: the driver behaviour and the copy, under "Depth clears".

Tested

AMD Radeon RX 7900 XTX (RDNA3, Navi 31), AMD driver 26.11.2 (32.0.32029.2010), Vulkan 1.4.359, Windows 11, MSVC 14.44, Vulkan SDK 1.4.363.0. No game files.

  • Before (d3d12: use legal Vulkan depth clear values (D3D-CLEAR-DEPTH-RANGE) #516 at 76033b0, and main 21625bc): d3d12_clear fails with and without LEONIDA_VK_VALIDATION=1, all 15 texels depth clear with NaN (0,0): layer cleared 1, expected nan.
  • After: d3d12_clear, d3d12_clear_restricted_depth, d3d12_clear_formats and d3d12_rgb9e5_rtv pass, 3 runs each with and without LEONIDA_VK_VALIDATION=1, no validation messages. depth clear with NaN: 5x3 depth texels matched (WARP parity), D32_S8 clear with NaN and stencil: 5x3 depth and stencil texels matched (WARP parity).
  • Negative control: with the copy path switched off in the same build, d3d12_clear fails again with layer cleared 1, expected nan.
  • Full ctest: 300/304. The 4 failures are on main on this PC too and are tracked elsewhere: vk_queue_query_sync (OLD-6149), d3d12_texture_tiles, d3d12_execute_indirect_validation (d3d12_execute_indirect_validation: 5 rows where the layer rejects and live WARP and hardware accept (over-strict, not permissive) #504), d3d12_pipeline_library_host (finding in Coordinating who works on what #14). main has d3d12_clear as a fifth.
  • python -I tests/test_ctest_registration.py, test_docs_list_items.py, test_docs_repeated_lines.py, test_docs_table_ids.py, test_exe_checklist.py: pass. scripts/check_no_game_data.py on the changed files: exit 0.

Not tested: NVIDIA and Intel. There the copy replaces a clear that already stored NaN, so the result should not change, but a run with ctest -R d3d12_clear on one of them would confirm it.

AI-assisted (Claude Code wrote the change and ran the tests).

🤖 Generated with Claude Code

sodove and others added 4 commits November 24, 2026 11:47
The AMD Vulkan driver stores 1.0 for a NaN depth clear value in
vkCmdClearDepthStencilImage and vkCmdClearAttachments, even with
VK_EXT_depth_range_unrestricted (RX 7900 XTX on 26.11.2, RX 9070 XT on
26.10.1). D3D12 keeps the quiet NaN, so d3d12_clear fails on AMD.

For a NaN clear of a D32_FLOAT or D32_FLOAT_S8X24_UINT depth plane, fill
transfer scratch with the value's bits and copy it into each cleared
rectangle (depth aspect only). The stencil aspect stays a clear.

d3d12_clear_test gains D32_S8 cases with NaN and stencil, and checks
stencil texels against WARP as well.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve the conflict with #540 in d3d12_vulkan_device.cpp: keep both
mesh_queries and set_depth_range_unrestricted.

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

Copy link
Copy Markdown
Contributor

Tested head 0d69d1be6 on two more AMD generations, RDNA4 and RDNA2, on the same 26.11.2 driver: it fixes both.

Setup: Windows 11 10.0.26200.9974, AMD 26.11.2, Vulkan SDK 1.4.363.0, MSVC 19.44, RelWithDebInfo. Radeon RX 9070 (RDNA4 / GFX1201, driver 32.0.32029.2010) and the Ryzen 5 7600X iGPU (RDNA2 / gfx1036, 32.0.21045.29003), each selected with LEONIDA_VK_DEVICE. Base is your base, main d0da7dade.

ctest -R "^(d3d12_clear|d3d12_clear_restricted_depth|d3d12_clear_formats|d3d12_rgb9e5_rtv)$", each GPU with LEONIDA_VK_VALIDATION 0 and 1:

main d0da7dade this PR
RX 9070 d3d12_clear fails (2/3 pass): a NaN depth stays NaN (0,0): layer cleared 1, WARP nan, every texel 4/4 pass, validation on and off
7600X iGPU (gfx1036) d3d12_clear fails the same way (2/3 pass) 4/4 pass, validation on and off
  • No validation messages in the LEONIDA_VK_VALIDATION=1 runs.
  • d3d12_clear_test run directly on each GPU prints depth clear with NaN: 5x3 depth texels matched (WARP parity) and both new D32_S8 cases (with NaN and stencil, with NaN, then 0.5 in the rectangle) as 5x3 depth and stencil texels matched (WARP parity).

So the driver behaviour is the same on RDNA2, RDNA3 (your 7900 XTX) and RDNA4, and the copy path covers all three.

I also read the runtime diff (vulkan_replay.h, vulkan_backend.h, d3d12_vulkan_device.cpp, d3d12_com.h, vk_test_device.h) and found nothing to flag:

  • nan_depth_copy only takes NaN on D32_SFLOAT / D32_SFLOAT_S8_UINT with the extension enabled, so other clears stay on the old path.
  • The copy writes the value's own bits (memcpy), with the scratch sized for the largest rectangle and the 4-byte offset alignment checked.
  • In the no-rectangle case the region's layer count comes from view_range, which is bounds-checked and never VK_REMAINING_ARRAY_LAYERS.
  • A stencil in the same call is still cleared, on cleared.aspectMask alone, and a depth-only clear skips the clear. I didn't repeat the negative control or the full ctest. Like you, I have no NVIDIA or Intel run.

No game files. AI-assisted: yes

@adrianmjim

Copy link
Copy Markdown

Tested 0d69d1b applied onto main 23c4596 on an AMD Radeon RX 7900 XT: d3d12_clear fails on main (NaN depth clear reads back 1.0) and passes with this PR, 3/3 runs. The new d3d12_clear_restricted_depth passes 3/3. No new failures in the full ctest run.

Hardware/software: AMD Radeon RX 7900 XT (RDNA3), AMD driver 32.0.32029.2010 (Adrenalin 26.11.2, Vulkan 1.4.359, offers VK_EXT_depth_range_unrestricted), Windows 11 build 26300, VS 2022 (x64, RelWithDebInfo), Vulkan SDK 1.4.363.0.

Build:

cmake -S runtime -B runtime/build/d3d12 -G "Visual Studio 17 2022" -A x64 -DLEONIDA_BUILD_WINDOWS_RUNTIME=ON
cmake --build runtime/build/d3d12 --config RelWithDebInfo --parallel 16

Targeted tests, 3 runs each on main and on the PR (third run with LEONIDA_VK_VALIDATION=1):

ctest --test-dir runtime/build/d3d12 -C RelWithDebInfo -R "^<test>$" --output-on-failure
test main 23c4596 (run 1 / 2 / 3 val.) PR 0d69d1b (run 1 / 2 / 3 val.)
d3d12_clear Failed / Failed / Failed Passed / Passed / Passed
d3d12_clear_restricted_depth (not on main) Passed / Passed / Passed
d3d12_clear_formats Passed / Passed / Passed Passed / Passed / Passed
d3d12_rgb9e5_rtv Passed / Passed / Passed Passed / Passed / Passed

On main, d3d12_clear fails in the NaN depth case: all 15 texels of the 5x3 D32_FLOAT target read back 1.0 where WARP has NaN:

a NaN depth stays NaN (0,0): layer cleared 1, WARP nan
...
a NaN depth stays NaN (4,2): layer cleared 1, WARP nan

With validation on, main also reports 4 errors for VUID-VkClearDepthStencilValue-depth-00022 (depth 1.5, -0.5 and NaN passed to vkCmdClearDepthStencilImage / vkCmdClearAttachments without VK_EXT_depth_range_unrestricted enabled).

With the PR, every d3d12_clear run prints:

depth clear with 1.5 and -0.5: 5x3 depth texels matched (WARP parity)
depth clear with NaN: 5x3 depth texels matched (WARP parity)
D32_S8 clear with NaN and stencil: 5x3 depth and stencil texels matched (WARP parity)
D32_S8 clear with NaN, then 0.5 in the rectangle: 5x3 depth and stencil texels matched (WARP parity)
flags 0 leaves the target alone: 5x3 depth texels matched (WARP parity)
RTV and DSV clears: 232 full/rectangle readbacks matched WARP

and d3d12_clear_restricted_depth prints the same five depth lines with "(restricted Vulkan depth)", i.e. 1.5/-0.5 clamped to 1/0 and NaN stored as 0. The validation-on runs of all four tests report no validation errors. Unchanged on both trees: d3d12_clear_formats "251 matched WARP, 0 differ, 0 failed", d3d12_rgb9e5_rtv "full and rectangle clears match all 15 packed texels".

Full ctest on the PR build, once:

ctest --test-dir runtime/build/d3d12 -C RelWithDebInfo -j 4 --timeout 600 --output-on-failure
  • main 23c4596: 300/305 passed; failed: vk_queue_query_sync, d3d12_texture_tiles, d3d12_execute_indirect_validation, d3d12_clear, d3d12_pipeline_library_host
  • PR: 302/306 passed (the extra test is d3d12_clear_restricted_depth); failed: vk_queue_query_sync, d3d12_texture_tiles, d3d12_execute_indirect_validation, d3d12_pipeline_library_host

The four remaining failures also fail on main and are unrelated to depth clears. Skipped/disabled tests are the same 13 on both.

Note: --restricted-depth runs only check_depth_clear_edge_values and returns; the volume, castable-RTV and RTV/DSV clear checks are covered only by the unrestricted d3d12_clear run.

@whoalin1

Copy link
Copy Markdown
Contributor

Independent GPU verification on a second AMD RDNA2 device (plus NVIDIA cross-check). AI-assisted verification by Devin.

Setup

  • PR head: 0d69d1be6 ("Merge main into nan-depth-clear"), detached checkout, incremental cmake --build runtime/build -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo, built clean.
  • Baseline for before/after: origin/main 6261ca916, full rebuild in a separate worktree (runtime/build, same toolchain).
  • Both GPUs on the same machine; each run under a machine-wide GPU lock, one GPU at a time.

AMD Radeon iGPU (RDNA2 gfx1035, driver 24.10.24, Vulkan 1.3.280)

VK_LOADER_DRIVERS_SELECT='*amd-vulkan*' LEONIDA_VK_DEVICE=radeon \
VK_LOADER_LAYERS_DISABLE='~implicit~' LEONIDA_VK_VALIDATION=1 LEONIDA_PC_TAIL_UBO=1 \
ctest -C RelWithDebInfo --output-on-failure -R "d3d12_clear"   # and -R "clear"

NVIDIA RTX 4070 SUPER

VK_LOADER_DRIVERS_SELECT='*nv-vk*' LEONIDA_VK_DEVICE=4070 \
VK_LOADER_LAYERS_DISABLE='~implicit~' \
ctest -C RelWithDebInfo --output-on-failure -R "d3d12_clear"   # and -R "clear"

Results

AMD iGPU — NaN depth clear

Baseline origin/main (6261ca916), d3d12_clear: FAILED — reproduces the reported bug on this RDNA2 iGPU:

depth clear with NaN (0,0): layer cleared 1, expected nan
... (15 texels total, 5x3 grid) ...
layer cleared 1, expected nan
depth clear with 1.5 and -0.5: 5x3 depth texels matched (WARP parity)

All 15 NaN-clear texels read back as 1 instead of NaN — same "driver stores 1.0 for NaN clear values" behaviour the PR describes, now confirmed on an AMD RDNA2 iGPU with driver 24.10.24 (much older than the 26.11.2 used for the author's RX 7900 XTX check).

PR head 0d69d1be6, d3d12_clear: PASSED (7.94 s first run, 2.60 s warm) with LEONIDA_VK_VALIDATION=1 — no validation errors. d3d12_clear_restricted_depth and d3d12_clear_formats also pass.

Wider -R "clear" subset on the PR head: 8/8 passed (d3d12_uav_clear, d3d12_short_uav_clear, d3d12_clear, d3d12_clear_restricted_depth, d3d12_clear_formats, clear_color, replay_clear_gate, replay_uav_clear_offset).

NVIDIA RTX 4070 SUPER

  • PR head -R "clear": 8/8 passed, including d3d12_clear.
  • Baseline origin/main d3d12_clear: passed — NVIDIA's driver never showed the NaN→1.0 defect, so this is a no-op there as expected.

Regression sweep (AMD iGPU, PR head)

scripts/run_gpu_tests.py --build runtime/build --require-pass (translated 258/258 shaders, spirv-val skipped — not on PATH): 224/249 passed, 20 skipped. The only 4 failures are all pre-existing on this iGPU and unrelated to clears:

Verdict

Verified on a second AMD device: the fill + buffer-to-image copy path fixes NaN depth clears on RDNA2 gfx1035 / driver 24.10.24, not just RDNA3. Before/after on the same machine is unambiguous: origin/main fails d3d12_clear with 15 texels reading 1 instead of NaN; this PR's head passes with validation layers on. No clear-related regressions on either GPU.

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants