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).

@illusionart453-max

Copy link
Copy Markdown

D3D-327 independent Windows/native D3D12 verification completed on RTX 5060 (Blackwell), NVIDIA 610.88 / 32.0.16.1088, Windows 11 26200, MSVC 19.44, SDK 10.0.26100, RelWithDebInfo. AI-assisted (Codex); synthetic fixtures only.

I built the submitted eff6276 test (15/15 pass), then locally instrumented its footprint readback to record every texel. For the native reference I used the same draw/resource setup with a synthetic HLSL VS compiled by system D3DCompile, normal native descriptor handles and the system D3D12 device. The native adapter LUID was checked against Vulkan's deviceLUID before comparing.

Each run covers 3 depth formats x 5 constant biases x 256 texels = 3,840 values. Native D16, native D24S8 and D32F were all available.

Runtime Mismatches against native D3D12 Result
Parent f847748, with the submitted test target/shader and local readback instrumentation added 2,048 / 3,840 8 fixed-point cases fail
PR head eff6276 0 / 3,840 all 15 cases exact
PR head, local control withholding info.depth_bias_exact 2,048 / 3,840 same values as parent

The eight failures are nonzero constant biases in D16/D24: +1/+3/-5/+1000 become +2/+6/-10/+2000 steps. Zero bias and every D32F case match native even on the parent. The parent and disabled-feature control match each other at every texel.

Local instrumentation was removed and the checkout is clean at eff6276. No slope/unbound-RTV expansion, standalone-device negotiation, other adapter, or Khronos-validation run is claimed; validation layers are unavailable here. No implementation change needed for this verification item.

@Exopus-0x65f7ad0

Copy link
Copy Markdown
Contributor

Independent evidence for this PR, from a differential graphics-state fuzzer I'm writing (d3d12_pso_fuzz_test, claimed in #5256). RTX 5060 Ti / 621.14, Windows 11 26300, MSVC 14.51, Vulkan SDK 1.4.363.0. The layer is the runtime's own standalone device, and the reference is the GPU's own D3D12 (--hardware).

Each seeded program clears a colour target and an optional D16/D24S8/D32/D32S8 target. It then draws triangles through PSOs with random blend, depth-stencil and rasterizer state, including a constant DepthBias in -20..20 on about a third of the PSOs, and compares every byte with the reference. Triangles have constant depth and flat colour, so only the state can make a difference.

400 seeds against NVIDIA's D3D12 differ first difference
main 3faac557 26, all in the depth plane D24 0x34001e vs 0x34000f, D16 0xec21 vs 0xec10: the bias is applied about twice
this PR at eff6276e (fuzzer added on top) 0 — (bit-identical, nothing even within the rounding tolerance)

Reduced example from main (seed 45): one triangle at z = 0.09375 on D24S8, DepthBias = -6, DepthFunc = GREATER_EQUAL. Layer 0x17fff4, host 0x17fffa: 12 codes of bias where D3D12 applies 6. That's the shadow-map case, so this PR matters beyond the test.

The PR conflicts with main now. If it helps, I can rerun this on the rebased head. AI-assisted (Claude Code).

Exopus-0x65f7ad0 added a commit to Exopus-0x65f7ad0/Leonida that referenced this pull request Nov 23, 2026
… scissors per draw

Half the draws get a viewport other than the whole target: whole-pixel offsets from -16 to 24, sizes
8 to 48, and a depth range in 64ths. A third get a scissor rect. Vertices stay on pixel corners or
half-pixels and depths stay exact. This covers the layer's viewport y-flip and depth range.

Over 600 seeds against the RTX 5060 Ti's D3D12, only the DepthBias seeds (ItsNotRexed#2718) differ; with ItsNotRexed#2718,
0 of 600.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Exopus-0x65f7ad0 added a commit to Exopus-0x65f7ad0/Leonida that referenced this pull request Nov 23, 2026
A third of the programs draw to two targets (a second random format) through a pixel shader that
writes the colour to SV_Target0 and its bgra swizzle to SV_Target1. Half their draws set
IndependentBlendEnable with RenderTarget[1]'s own blend state and write mask. Both targets are
compared.

Over 600 seeds (197 with two targets) against the RTX 5060 Ti's D3D12, only the DepthBias seeds
differ; with ItsNotRexed#2718, 0 of 600.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Exopus-0x65f7ad0 added a commit to Exopus-0x65f7ad0/Leonida that referenced this pull request Nov 23, 2026
…as behind --all

Four UINT target formats (R8G8B8A8, R16G16B16A16, R32G32B32A32, R32) drawn by a pixel shader that
writes the colour's bits, so both sides see exact integers; where both devices report
OutputMergerLogicOp, half their draws enable one of the sixteen logic ops. UINT texels compare
exactly. --verbose names both targets and says whether logic ops run.

A nonzero DepthBias is now generated only with --all: on NVIDIA the layer applies about twice
D3D12's bias (D3D-325, fixed by ItsNotRexed#2718), and the default run should stay green on main until that
lands. With --all and ItsNotRexed#2718, 0 of 600 differ.

RTX 5060 Ti against its own D3D12: default 40 seeds and 1,000 seeds under validation, 0 differ, 0
validation messages; --all 600 seeds, 25 differ, all DepthBias.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
abilliontokens added a commit to abilliontokens/Leonida that referenced this pull request Nov 23, 2026
- ItsNotRexed#2718 (D3D-325, the earlier claim on the depth-bias test name) adds a same-named test for static depth bias; illusionart453-max's review on ItsNotRexed#1451 asked one side to rename, and ItsNotRexed#2718 keeps its name.
- File, CMake target, CTest name (d3d12_dynamic_depth_bias), the temp shader-library dir and the comments naming it all move together, so the two suites can coexist without clobbering each other's index.json.
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.

6 participants