Skip to content

d3d12: exact depth bias through VK_EXT_depth_bias_control (D3D-325) - #2718

Open
UlteriorMotive00 wants to merge 4 commits into
ItsNotRexed:mainfrom
UlteriorMotive00:fix/native-depth-bias-exact
Open

UlteriorMotive00 wants to merge 4 commits into
ItsNotRexed:mainfrom
UlteriorMotive00:fix/native-depth-bias-exact

Conversation

@UlteriorMotive00

Copy link
Copy Markdown
Contributor

D3D-325 (finding, listed). Depends on: none (based on main 49475796).

What and why

D3D's DepthBias is exactly one step of the depth format: r is "the minimum representable value > 0 in the depth-buffer format", so 1 / (2^n - 1) for an n-bit UNORM, and 2^(exponent(z) - 23) for D32_FLOAT. Vulkan lets a fixed-point attachment's r be anywhere up to twice that unless the pipeline sets VkDepthBiasRepresentationInfoEXT::depthBiasExact. The RTX 5070 uses twice. On native D24_UNORM_S8_UINT a flat draw stored +6 / -10 / +2000 steps for DepthBias +3 / -5 / +1000, and D16_UNORM behaves the same way. Shadow-map and decal biases on D16/D24 targets would land at double what the game set.

Changes

  • VulkanDeviceInfo::depth_bias_exact: VK_EXT_depth_bias_control with depthBiasControl and depthBiasExact is enabled on the device.
  • The standalone device enables the extension and both features where the GPU has them (leastRepresentableValueForceUnormRepresentation and floatRepresentation stay off; they aren't needed). vktest::Device does the same and reports depth_bias_exact.
  • create_graphics: a PSO with a bias chains VkDepthBiasRepresentationInfoEXT { LEAST_REPRESENTABLE_VALUE_FORMAT, depthBiasExact = VK_TRUE } into its rasterization state. The struct lives in the PSO's Recipe, next to the raster it hangs from, so draw-time variants (build(unbound)) keep it. The recipe is only ever used by reference.
  • docs/d3d12_vulkan.md: a "Exact depth bias" item next to the PSO feature list.

Tests

New d3d12_depth_bias_test (CTest d3d12_depth_bias, synthetic, no game files). A flat full-screen triangle (new test/glsl/vs_depth.vert) at a depth on one of the format's steps (so no rounding tie decides the code) is drawn with DepthFunc ALWAYS and DepthBias 0, +1, +3, -5, +1000 into D16_UNORM, D24_UNORM_S8_UINT and D32_FLOAT. Stored depth is read back through a footprint and compared with D3D's formula. Without depthBiasExact the test prints what the device does (INFO) instead of failing. D24 is skipped where the layer stores it as D32 (no D24 on the GPU), because that path's bias is D3D-309's.

Tested on Windows 11 Home 26300, MSVC 14.44 (VS 2022 Build Tools), Ninja RelWithDebInfo via scripts/build_runtime.ps1:

  • NVIDIA GeForce RTX 5070, 617.42 (depthBiasExact available), with LEONIDA_VK_VALIDATION=1: 15/15 exact. D16 and D24 +0/+1/+3/-5/+1000 steps; D32F 0.375 → 0.37500003 / 0.375000089 / 0.374999851 / 0.375029802 for +1/+3/-5/+1000.
  • Control on the same GPU, with the layer not asking for exact bias: 8 failures. D16 and D24 store +2/+6/-10/+2000 steps; D32F is already exact.
  • AMD Radeon integrated graphics (gfx1036, AMD 2026.04.17, no VK_EXT_depth_bias_control), with validation: D32F exact. D16 reports INFO +2/+6/-10/+2000 steps (the driver's 2x, which this PR can't change there). D24 is skipped (stored as D32).
  • Full ctest: 125/125 on both GPUs.

Not covered

  • AMD's Windows driver has no VK_EXT_depth_bias_control here, so its fixed-point bias stays at 2x on D16. A fallback would need the device's r measured once (a probe draw) and the constant factor halved; I can file that as a finding if it's wanted. AMD's D24 is stored as D32, so it falls under D3D-309, not this.
  • Slope-scaled bias doesn't depend on r, so it isn't affected. Not separately tested here.

AI-assisted: yes.

🤖 Generated with Claude Code

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent Windows/MSVC 19.44 run of 6d98f33 on RTX 5070 / 621.24. The submitted d3d12_depth_bias CTest passed all 15 cases across native D16, D24S8 and D32F attachments.

I expanded the local test to 39 cases with positive and negative slopes, constant and slope terms together, and PSOs declaring an RTV that is left unbound at draw time while the DSV stays bound. All 9,984 depth texels matched the D3D formula exactly with depthBiasExact, including the draw-time variants. I then prevented the test device from enabling the extension or advertising it to the layer. The same 39 cases matched a control with doubled fixed-point constant bias, unchanged slope term, and unchanged D32F. For example, a constant +3 stored +3 with the extension and +6 without it, including when the RTV was unbound.

This exercised the layer on test-created Vulkan devices on one NVIDIA driver. It did not test the standalone device's feature negotiation, another ICD, or Vulkan validation layers.

@yasaiiko

Copy link
Copy Markdown

Independent Windows test of 6d98f33 on Intel integrated GPU 8086:7D41, driver 32.0.101.9150, Windows 11 build 26300. Built d3d12_depth_bias_test with x64 MSVC/Ninja (RelWithDebInfo), then ran ctest --test-dir review-build -R "^d3d12_depth_bias$" -V.

The driver reported depthBiasExact unavailable. All 15 readback comparisons still matched the D3D reference on the fallback path, including native D16_UNORM, native D24_UNORM_S8_UINT and D32_FLOAT at biases 0, +1, +3, -5 and +1000. CTest passed 1/1 with zero fixture failures. This adds Intel fallback coverage alongside the RTX exact-path result above. AI-assisted testing and report.

@UlteriorMotive00

Copy link
Copy Markdown
Contributor Author

Thanks @duudiis, that's a much wider check than mine. On the gap you named (the standalone device's negotiation), I pushed ef931e08. Like the other optional features it negotiates, the standalone device now names a GPU without depthBiasExact once: [rt] d3d12: no VK_EXT_depth_bias_control depthBiasExact; fixed-point DepthBias is the driver's (up to 2x D3D's).

Ran d3d12_queue_wait_test (which uses create_vulkan_device_standalone) with LEONIDA_VK_VALIDATION=1:

  • RTX 5070, 617.42: extension and features enabled, no validation messages at vkCreateDevice or after, exit 0.
  • AMD Radeon iGPU (gfx1036, 2026.04.17): the new line is printed (the driver has no VK_EXT_depth_bias_control), no validation messages, exit 0.

That covers the standalone path's device creation, not draws through it. AI-assisted.

@gkissel

gkissel commented Nov 22, 2026

Copy link
Copy Markdown

Tested ef931e08 on a different NVIDIA architecture: GeForce GTX 1650 Mobile (Turing, TU117), Linux driver 620.70.06. The Windows build was cross-compiled with clang-cl 23.1.4 and xwin SDK 10.0.26100 and run under Wine 11.21 (LEONIDA_VK_DEVICE=1650, implicit layers disabled). The driver exposes VK_EXT_depth_bias_control with depthBiasExact = true.

  • ctest -R "^(d3d12_depth_bias|d3d12_queue_wait)$": 2/2 pass. The test prints depthBiasExact: enabled and all 15 cases match D3D (D16 / D24S8 / D32F at bias 0, +1, +3, -5, +1000).
  • Negative control: a local one-line change set recipe_.bias_representation.depthBiasExact = VK_FALSE (d3d12_vulkan_device.cpp:2593), and the test was rebuilt and run again. Result: 8 failures, all on the fixed-point formats, at exactly 2× D3D (D16 and D24S8 store +2 / +6 / -10 / +2000 steps for +1 / +3 / -5 / +1000, 256 of 256 texels). D32F still matches, and bias 0 passes. So Turing doubles fixed-point bias the same way the RTX 5070 does, and this PR's chained struct is what fixes it. The change was reverted afterwards, and the tree is clean at ef931e08.

AI-assisted.

UlteriorMotive00 and others added 3 commits November 22, 2026 16:49
D3D's DepthBias is exactly one step of the depth format. Vulkan lets a
fixed-point attachment's step be up to twice that unless the pipeline asks
for depthBiasExact, and the RTX 5070 (617.42) uses twice: D24 +3 stored as +6,
+1000 as +2000, and the same on D16.

Where the GPU has VK_EXT_depth_bias_control with depthBiasExact, the
standalone device (and the test device) enable it, VulkanDeviceInfo carries
depth_bias_exact, and each PSO with a bias chains
VkDepthBiasRepresentationInfoEXT (FORMAT representation, exact) from its
recipe, so draw-time variants keep it.

d3d12_depth_bias_test draws flat triangles with DepthBias 0, +1, +3, -5,
+1000 into D16, D24S8 and D32F targets and compares stored depth with D3D's
formula; without the extension it reports what the device does.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Like the other optional features the standalone device negotiates, a GPU
without VK_EXT_depth_bias_control's depthBiasExact is named once, since its
fixed-point DepthBias then stays the driver's (up to 2x D3D's).

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ulkan_replay.h locals (C4459)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@UlteriorMotive00
UlteriorMotive00 force-pushed the fix/native-depth-bias-exact branch from ef931e0 to 37ac244 Compare November 22, 2026 21:50
ItsNotRexed#3797 (D3D-309) adds its own test/glsl/vs_depth.vert and a generated/vs_depth.spv rule; a second rule for the
same output is a CMake error, so this test's shader gets its own name and its CMake block sits elsewhere.

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

Copy link
Copy Markdown
Contributor Author

Rebased onto main f847748c (it conflicted after the merge burst), plus two small test-only commits:

Thanks @duudiis, @yasaiiko and @gkissel for the runs on Turing, Intel and a second RTX 5070.

Tested on 37ac244d/eff6276e (Windows 11, MSVC 14.44, Vulkan SDK 1.4.363):

AI-assisted (Claude Code).

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