Skip to content

Runtime: a pixel's depth is clamped to the viewport range with clipping on, as D3D12 does - #1703

Open
nightmoon1996 wants to merge 1 commit into
ItsNotRexed:mainfrom
nightmoon1996:fix/depth-clamp-viewport-range
Open

nightmoon1996 wants to merge 1 commit into
ItsNotRexed:mainfrom
nightmoon1996:fix/depth-clamp-viewport-range

Conversation

@nightmoon1996

@nightmoon1996 nightmoon1996 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #1548 (comment) (new D3D item)

What and why

D3D12 clamps the depth a pixel ends up with (written by the pixel shader, or moved by depth bias) to the viewport's MinDepth..MaxDepth, whether or not DepthClipEnable is set; DepthClipEnable only decides whether geometry is clipped at the near and far planes. The layer set depthClampEnable = !DepthClipEnable, and Vulkan only clamps with depthClampEnable on. So with clipping on, the default, a depth outside the viewport range reached the depth buffer as it was.

Measured on the PC runtime first (D32_FLOAT cleared to 0.75, viewport depth 0.25..0.5, depth test ALWAYS, one full-screen triangle). The RTX 4060 Ti and WARP gave the same value in every cell:

Case DepthClipEnable TRUE FALSE Layer on main, TRUE
SV_Depth 0.9 0.5 0.5 0.9
SV_Depth 0.1 0.25 0.25 0.1
SV_Depth 0.4 0.4 0.4 0.4
depth bias past MaxDepth 0.5 0.5 0.613
depth bias below MinDepth 0.25 0.25 0.137
z/w = 1.5 0.75 (clipped) 0.5 0.75
z/w = -0.5 0.75 (clipped) 0.25 0.75
z = 0.5, no bias 0.375 0.375 0.375

The change:

  • Device creation enables VK_EXT_depth_clip_enable when the device has it (VulkanDeviceInfo::depth_clip_enable, false by default, so a device handed in by a caller behaves as before).
  • With the extension and depthClamp, a graphics pipeline always sets depthClampEnable and carries DepthClipEnable in VkPipelineRasterizationDepthClipStateCreateInfoEXT. The struct is kept in the pipeline recipe and chained in build(), so variant pipelines get it too.
  • Without them, today's mapping stays as it is, with one log line.
  • DepthClipEnable = FALSE pipelines behave as before (Runtime: DepthClipEnable = FALSE clamps depth instead of clipping #813's half, already on main).

To keep clear of #1451: the "Features" sentence in docs/d3d12_vulkan.md is not edited, and vk_test_device.h adds its lines away from enable.logicOp.

Tested

Windows 11, NVIDIA GeForce RTX 4060 Ti (driver 32.0.16.2101), built with clang-cl 23.1.6 and the xwin SDK (10.0.26100) through runtime/cmake/clang-cl-xwin.cmake, run natively. No game files.

  • New d3d12_depth_clamp (built with the other glslangValidator tests): the 8 cases above for both DepthClipEnable values, all 256 pixels, exact float equality between the layer and the PC runtime, and both equal to the table. Passes against the RTX 4060 Ti as host and with LEONIDA_HOST_ADAPTER=warp.
  • The same test runs a second pass on a device with depth_clip_enable = false: the FALSE cases and the in-range and clipped TRUE cases still match, and the out-of-range TRUE cases stay unclamped, which pins the fallback.
  • Negative control: with main's d3d12_vulkan_device.cpp, the four out-of-range TRUE rows differ from the host (last column above).
  • Full ctest: 94 passed, 0 failed; skipped or disabled here: bink_decode, fsr_dispatch, bvh_builder, bink_audio (need the package), d3d12_rgb9e5_rtv (not a render target on this GPU), gameinput (no SDL3 in this build).
  • Under Wine the test skips only the host comparison and still checks the layer against the table, as d3d12_depth_resolve does. That path was written but not run here.
  • Not run: Vulkan validation (the Khronos layer is not installed on this machine), any AMD or Intel GPU.

One thing measured and left out of the pass/fail set: with an inverted viewport (MinDepth 0.5, MaxDepth 0.25) the two hosts disagree. The RTX 4060 Ti clamps to the range between the two ends (0.9 → 0.5, 0.1 → 0.25, 0.4 → 0.4) and the layer matches it; WARP writes 0.25 for everything. The test prints those rows without judging them.

AI-assisted (Claude Code).

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (not a translator change)

🤖 Generated with Claude Code

@evilclanker9000

Copy link
Copy Markdown
Contributor

Independently verified commit e1a6e4d2 on Windows 11 (MSVC 19.44, Ninja, VK_LOADER_LAYERS_DISABLE=~implicit~, synthetic scenes):

  • NVIDIA GeForce RTX 5080 (driver 616.92, Vulkan 1.4.351):
    • d3d12_depth_clamp_test.exe: PASS (exit=0) — depth clamp: 16 cases on the layer and on the host (NVIDIA GeForce RTX 5080) returned the same depth; 16 without the extension behaved as documented.
    • d3d12_draw_test.exe: PASS (exit=0) — all 4096 pixels matched across M1, XOR/fullscreen, and strip-cut passes.
  • AMD Radeon(TM) Graphics (driver 26.10.1, Vulkan 1.4.315):
    • d3d12_depth_clamp_test.exe: PASS (exit=0) — 16/16 layer vs host matched, 16/16 without extension behaved as documented.
    • d3d12_draw_test.exe: PASS (exit=0).
  • Note: VK_LAYER_KHRONOS_validation is not installed locally.

AI Assisted.

@dracbat

dracbat commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested e1a6e4d2 on AMD Radeon RX 5700 (RDNA1), AMD Adrenalin 26.11.2 (Vulkan driver 2.0.353), Windows 11: pass. That's an AMD data point next to the RTX 4060 Ti and WARP measurements; this device has VK_EXT_depth_clip_enable.

  • Full runtime ctest (MSVC 14.44, Ninja, RelWithDebInfo, Vulkan SDK 1.4.363.1): 97/97 pass, 3 skipped (bink_decode and bink_audio: no movie set; d3d12_rgb9e5_rtv: no RGB9E5 colour attachment on this GPU).
  • The new d3d12_depth_clamp passes.

AI-assisted (Claude Code).

@opit80

opit80 commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Built e1a6e4d2 natively on Windows 11 with MSVC 19.44 + Ninja (RelWithDebInfo), target d3d12_depth_clamp_test. It adds two MSVC warnings that main does not have:

runtime/src/vulkan_replay.h(1600): warning C4459: declaration of 'size' hides global declaration
runtime/src/vulkan_replay.h(1615): warning C4459: declaration of 'size' hides global declaration
  note: d3d12_depth_clamp_test.cpp(45): see declaration of '`anonymous-namespace'::size'

The new test's namespace-scope constexpr UINT size = 16; (line 45) is visible when vulkan_replay.h's prepare/prepare_draw are instantiated in that TU, so their local size shadows it. Renaming the constant (e.g. kSide; uses at lines 119, 151-152, 179-182, 312) clears both.

@nightmoon1996

Copy link
Copy Markdown
Contributor Author

Rebased onto main (4eb6155); head is now 4090bf3. Thanks @Vonsek and @dracbat for the RTX 5080 and AMD runs, and @opit80 for the MSVC build.

Changes since e1a6e4d:

  • C4459 (@opit80): the test's namespace-scope size is now target_size, so it no longer hides the locals in vulkan_replay.h. clear_depth became initial_depth for the same reason (leonida::clear_depth exists in command_list.h). Checked with clang-cl -Wshadow-all on that file; I have no MSVC here, so a second MSVC build would confirm it.
  • Conflicts in docs/d3d12_vulkan.md, runtime/CMakeLists.txt and runtime/shims/d3d12_com.h were additions next to each other (d3d12_set0_compute, d3d12_pipeline_library, xdxr_acceleration_structures); both sides are kept.
  • Feature chain: main now builds the device feature chain through *v13.next(); the two places that add the depth-clip feature follow that.

The three red jobs on the old head were not from this change:

  • windows: d3d12_indirect_zero failed in 0.02 s on the GPU-less runner. The same test fails the same way in main's own Runtime run 45028252741. It passes here.
  • linux-gpu-tools: vk_test_device.h: 'VkPhysicalDeviceSwapchainMaintenance1FeaturesKHR' was not declared, from the swapchain_maintenance1 code already on main (the runner's Vulkan headers only have the EXT name). main's run 45028252741 has the same error. This job stays red until that is fixed on main.
  • check: tests/test_make_game_assets_pools.py, fixed on main by a7f2bb3; the rebase picks it up.

Tested on this head, Windows 11, RTX 4060 Ti: d3d12_depth_clamp passes against the RTX 4060 Ti and against WARP (16 cases, plus 16 without the extension); d3d12_indirect_zero and d3d12_vulkan_device pass. Full ctest: 2 of 113 not passing, neither from this change: d3d12_subgroup can't be built here (my llvm-mc has no AMDGPU target), and d3d12_draw fails at "bundle blend pixel (0, 0): got 127,0,127,255, expected red 64" with and without this change (the readback race #1995 describes).

AI-assisted.

@nightmoon1996

Copy link
Copy Markdown
Contributor Author

Rebased onto main (4947579) again: three conflicts, all additions next to each other (d3d12_fspx_compute_test in the docs, d3d12_msaa_draw in CMake, the custom-border-colour extension in vk_test_device.h); both sides kept, no change to the fix. On this head, Windows 11, RTX 4060 Ti: d3d12_depth_clamp passes against the RTX 4060 Ti and WARP; d3d12_msaa_draw, d3d12_draw, d3d12_vulkan_device and d3d12_indirect_zero pass (MSAA pipelines go through the same build(), so they get the depth-clip state too). Full ctest: 118 of 119; the one is d3d12_subgroup, which my toolchain can't build (no AMDGPU target in llvm-mc). AI-assisted.

@nightmoon1996

Copy link
Copy Markdown
Contributor Author

Rebased onto main (1628210), now at 7966f59. One conflict in d3d12_vulkan_device.cpp: the device-extension list, where the barycentric, fragment-shader-interlock and maintenance8 pushes landed next to the depth-clip one. Both kept; nothing else changed.

Re-run after the rebase (clang-cl 23.1.6 + xwin SDK 10.0.26100, NVIDIA): d3d12_depth_clamp passes; full ctest 123 of 124, the one not run being d3d12_subgroup, which this toolchain cannot build (no AMDGPU target in its llvm-mc).

AI-assisted (Claude Code); no game files.

@nightmoon1996

Copy link
Copy Markdown
Contributor Author

Rebased onto main (f847748). One conflict in runtime/CMakeLists.txt: the new d3d12_geometry block and this PR's d3d12_depth_clamp block both sit after d3d12_msaa_draw; both kept, nothing else changed.

Re-run (RTX 4060 Ti, driver 32.0.16.2101, clang-cl 23.1.6 + xwin SDK 10.0.26100): d3d12_depth_clamp passes; full ctest 134 of 135, the one not run being d3d12_subgroup, which this toolchain cannot build.

AI-assisted (Claude Code); no game files.

@Flyander Flyander left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The depth-clip extension path matches the reported D3D behavior, and the documented fallback is explicit when the extension is unavailable. The synthetic host-vs-layer readback has independent NVIDIA and AMD results. No additional blocker found in this diff; I did not run tests locally. The current branch still needs to clear its reported merge conflict before it can merge.

…ng on, as D3D12 does

D3D12 clamps the depth a pixel ends up with (written by the pixel shader, or
moved by depth bias) to the viewport's MinDepth..MaxDepth whether or not
DepthClipEnable is set. The layer set depthClampEnable only for
DepthClipEnable = FALSE, so with clipping on, the default, such a depth
reached the buffer unclamped.

Where VK_EXT_depth_clip_enable and depthClamp exist, a pipeline now always
clamps and carries DepthClipEnable in the extension's depth-clip state.
Without them the old mapping stays, with a log line.

d3d12_depth_clamp compares the layer with the PC runtime: D32_FLOAT, a
[0.25, 0.5] viewport, SV_Depth above, below and inside the range, depth
bias past both ends, and geometry beyond the near and far planes, for both
DepthClipEnable values.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@nightmoon1996

Copy link
Copy Markdown
Contributor Author

Rebased onto main (2d3369a). Conflicts were all next to the new conservative-rasterization work (#d3d12_conservative_raster) and d3d12_depth24_draw: the docs bullet, the CMake test blocks, and in d3d12_vulkan_device.cpp the Recipe fields, the extension probe (now through main's has_device_extension) and build(), where the conservative-raster struct chains onto raster.pNext after the depth-clip one, so a PSO can carry both. main does not touch depth clamp or depth clip itself.

Re-run (RTX 4060 Ti, driver 32.0.16.2101, clang-cl 23.1.6 + xwin SDK 10.0.26100): d3d12_depth_clamp passes, d3d12_conservative_raster passes. Full ctest 163 of 172; the 8 not run are the tests whose shaders this toolchain cannot generate (no AMDGPU target in its llvm-mc): d3d12_translated_draw, d3d12_m2_textured, d3d12_m2_formats, d3d12_m3_frame, d3d12_m3_first_pass, d3d12_m4_compute, d3d12_buffer_uav, d3d12_subgroup; and run_tiny_game_dir failed once under -j2 and passed when run alone.

AI-assisted (Claude Code); no game files.

@raxjinn

raxjinn commented Nov 23, 2026

Copy link
Copy Markdown

Tested 607ead1b (no merge with main) on Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 3080 (Ampere), driver 621.35, MSVC 14.44.35207 (an MSVC build; the runs in the PR used clang-cl + xwin), Windows SDK 10.0.26100, Vulkan SDK 1.4.363.0, Ninja, RelWithDebInfo: pass.

  • LEONIDA_VK_VALIDATION=1 ctest -R "^d3d12_depth_clamp$": pass, 0 validation messages. Host: the RTX 3080.
    • All 16 cases (8 cases × both DepthClipEnable values): layer = host = expected.
    • Second pass without the extension: all rows ok. The out-of-range TRUE rows stay unclamped (0.9, 0.1, 0.613, 0.137), as the PR describes.
    • Inverted viewport (information only): the RTX 3080 gives 0.5 / 0.25 / 0.4 for both DepthClipEnable values, the same as the RTX 4060 Ti in the PR body. The layer gives the same values.
  • Red control: with only the runtime/shims/d3d12_vulkan_device.cpp change reverted, the test fails with 4 MISMATCH rows, all DepthClipEnable TRUE: layer 0.9 / 0.1 / 0.613 / 0.137 against host and expected 0.5 / 0.25 / 0.5 / 0.25 (the "Layer on main" column of the PR table).
  • Full ctest: 173 run, 0 failed. Skipped or disabled: the same 10 as main on this machine.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 607ead1 merged with main 98fa503 on Windows (MSVC 14.51, RelWithDebInfo; RTX 4080 SUPER and UHD 770, driver 32.0.101.6129): pass. GitHub flags it as conflicting but the merge is clean. d3d12_depth_clamp passes on both GPUs; on the UHD 770 the layer lacks depthClamp/VK_EXT_depth_clip_enable, so the 16 'without the extension' cases run and behave as documented. Full ctest: RTX 4080 SUPER 174/174; UHD 770 172/174, the two failures (d3d12_sparse_queue_family, d3d12_present) also fail with main's d3d12_vulkan_device.cpp, so they are not from this PR. Negative control: with main's runtime/shims/d3d12_vulkan_device.cpp (keeping the PR's headers) d3d12_depth_clamp fails on both GPUs; with main's three non-test files the test no longer compiles (depth_clip_enable is missing).

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.

7 participants