Skip to content

D3D12.X: the depth bias of a 16-bit depth buffer is in 24-bit units - #2858

Open
tiagonleal wants to merge 2 commits into
xcl-dev:mainfrom
tiagonleal:depth-bias-of-a-16-bit-depth-buffer
Open

tiagonleal wants to merge 2 commits into
xcl-dev:mainfrom
tiagonleal:depth-bias-of-a-16-bit-depth-buffer

Conversation

@tiagonleal

@tiagonleal tiagonleal commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

The console's D16_UNORM_S8_UINT (118) has no PC format, so its depth buffers become D24_UNORM_S8_UINT. On a UNORM depth buffer D3D12 applies DepthBias * 2^-n + SlopeScaledDepthBias * MaxDepthSlope (n: depth bits), clamped by DepthBiasClamp. A title's DepthBias meant for a 16-bit buffer (2^-16 a unit) reached the 24-bit one unchanged (2^-24 a unit), so every constant bias on such a buffer was 256 times weaker than on the console. The bias is now scaled by 2^8 for that format. Slope and clamp are not in these units and stay.

Where the bias reaches the PC:

  • pipelines: pc_topology_type, where the DSVFormat becomes the PC's (all graphics paths), and the GS pipeline stream, which passed the rasterizer state on directly
  • pipeline streams: the RASTERIZER / RASTERIZER1 / RASTERIZER2 subobjects of the two-pass mesh and stand-in streams, by the stream's DEPTH_STENCIL_FORMAT wherever it sits. The float bias of RASTERIZER1 / RASTERIZER2 is scaled before it is rounded (-0.5 on a D16S8 buffer becomes -128).
  • dynamic bias: try_native_depth_bias, by the console DSVFormat of the bound pipeline, with the float scaled exactly (-0.5 becomes -128.0, not rounded first). It stores the scaled bias it actually sent, and a pipeline switch or RSSetDepthBias compares the new target scaled for the bound pipeline's format with that stored value, so a compute, untracked or non-dynamic pipeline in between cannot hide a change of format. The patched-PSO fallback scales before rounding and is not scaled a second time.

The scaling applies only while pc_view_format(118) is D24S8. If 118 maps to D32_FLOAT (as in #1649), the unit depends on the primitive's depth exponent and no fixed factor is right, so the scaling turns itself off.

Based on e120925e, the last main commit before #2555 (see Regression run).

What is in it

  • crates/xcl/src/hle/d3d12x/device_x.rs: pc_depth_bias (saturating at the i32 limits), DepthBias::for_dsv, base_dsv_format, the fallback's scaling; new test depth_bias_of_a_16_bit_depth_buffer_is_in_24_bit_units (integer, fractional and saturating values)
  • crates/xcl/src/hle/d3d12x/real.rs: pc_topology_type, the GS stream, the two-pass mesh stream and fallback_stream use it; stream_pipeline scales the RASTERIZER1 / RASTERIZER2 float (stream_dsv_format, pc_stream_depth_bias); new tests a_d16s8_pipeline_gets_its_depth_bias_in_d24_units and a_d16s8_stream_gets_its_depth_bias_in_d24_units_in_either_order
  • crates/xcl/src/hle/d3d12x/list_x.rs: the native RSSetDepthBias calls and the pipeline-switch resync use it; new tests native_depth_bias_follows_the_bound_pipelines_depth_format and native_depth_bias_resends_after_a_pipeline_that_does_not_sync

Tested

  • Windows 11 Pro 10.0.26300, NVIDIA GeForce RTX 5070 Ti 16 GB (driver 617.42), AMD Ryzen 7 9800X3D, 32 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09); debug tests, release build
  • Two defects found in review of 971f766 (pipeline switch with a stale stored bias; float bias of RASTERIZER1/2 streams truncated before scaling) are fixed in 25814cc with a test each.
  • cargo test -p xcl --lib -- depth_bias d24_units stream_pipeline_defaults_depth_enable --test-threads=4: 10 passed, 0 failed, 3 of 3 runs on 25814cc. One of the ten, depth_bias_native_list9_and_bundle_fallback, creates a D3D12 device and ran here (no SKIPPED line). The ten are depth_bias_of_a_16_bit_depth_buffer_is_in_24_bit_units, depth_bias_patches_rasterizer_fields, depth_bias_state_tracks_fallback_and_clear, native_depth_bias_follows_the_bound_pipelines_depth_format, depth_bias_native_path_exact_f32_and_list9_fallback, native_depth_bias_resends_after_a_pipeline_that_does_not_sync, a_d16s8_pipeline_gets_its_depth_bias_in_d24_units, a_d16s8_stream_gets_its_depth_bias_in_d24_units_in_either_order, stream_pipeline_defaults_depth_enable_from_dsv_format and depth_bias_native_list9_and_bundle_fallback.
  • With the scaling switched off in pc_depth_bias (local change, not committed) on 971f766, the four scaling tests failed (device_x.rs:1531 left: 4, right: 1024; list_x.rs:5286 left: Some(-3.0), right: Some(-768.0); real.rs:6678 left: (DXGI_FORMAT(45), -8), right: (DXGI_FORMAT(45), -2048); real.rs:6652 left: -8, right: -2048).
  • With the production hunks of 25814cc reverted by hand (not committed), the two tests it touches fail: native_depth_bias_resends_after_a_pipeline_that_does_not_sync (list_x.rs:5339, case 0: [-256.0], left: Some(-256.0), right: Some(-1.0)) and stream_pipeline_defaults_depth_enable_from_dsv_format (real.rs:8538, type 27, bias -0.5, D16S8, left: 0, right: -128). Restored, both pass.
  • cargo test --workspace --no-fail-fast -- --test-threads=4, once on 25814cc: 1443 passed, 4 failed, 47 ignored (25 SKIPPED lines: 21 no wave64, 1 no heap windows, 3 recompiler cases refused as unsupported). The 4 failures are gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 (CreateComputePipelineState returns E_OUTOFMEMORY, 0x8007000E); they fail the same way on e120925e on this machine (cargo test -p xcl --lib fuzz_validate_and_gpu: 0 passed, 4 failed).

Regression run
scripts/smoke.ps1 -Seconds 60 -Title 9cc6a39e, default settings, release builds of both commits in clean trees, one run each, back to back on this machine.

main (e120925):

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1614 (PresentX 1614)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15972 MB

This PR:

xcl-smoke v1
commit: 25814cc264c041797ab8a542cdeed30416244a40
gpu: NVIDIA GeForce RTX 5070 Ti
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1614 (PresentX 1614)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16137 MB

NVIDIA profile for xcl.exe: shader heap 1024 MB

Both builds are based on e120925e, the last main commit before #2555; I could not finish a first launch preparation on a commit that has it (stopped after 2 h 20 min at about 23,860 of 26,503 records on this machine), so the preparation added by #2555 is not covered.

Not tested

AI use

  • Claude Code with Claude Fable 5.2 and Anthropic subagents (Claude Opus 5.5, Claude Sonnet 5.5) helped developing.

🤖 Generated with Claude Code

The console's D16_UNORM_S8_UINT (118) has no PC format; its depth buffers
become D24_UNORM_S8_UINT. On a UNORM depth buffer D3D12 applies
  bias = DepthBias * 2^-n + SlopeScaledDepthBias * MaxDepthSlope
(n: depth bits), clamped by DepthBiasClamp. A title's DepthBias for its
16-bit buffer (2^-16 a unit) reached the 24-bit one unchanged (2^-24 a
unit): every constant bias on such a buffer was 2^8 = 256 times weaker.

device_x::pc_depth_bias turns the integer bias into the PC format's units
(x256 for 118 while it is D24S8 on the PC, saturating at the i32 limits);
DepthBias::for_dsv scales a float bias exactly (-0.5 -> -128.0), rounding
only where a desc needs an integer. Slope and clamp are not in these units
and stay. Applied where a pipeline's DSVFormat becomes the PC's
(pc_topology_type, the GS pipeline stream), to the RASTERIZER /
RASTERIZER1 / RASTERIZER2 subobjects of the two-pass mesh and stand-in
streams (by the stream's DEPTH_STENCIL_FORMAT, in either order), to the
patched-PSO fallback (scaled before rounding, not scaled twice) and to the
native ID3D12GraphicsCommandList9::RSSetDepthBias calls, by the bound
pipeline's console DSVFormat; a pipeline switch between depth formats
resends the bias from its unscaled value.

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

Copy link
Copy Markdown

Verification and code review of PR #2858 at head 971f766cc9f442bc71623b2390bc285a63cdb456 (merge-base e120925e3abc9956a8b9bae631969d32e8ccd43c, cleanly mergeable onto origin/main 8b957bec where #2555 does not touch these files; not superseded on origin/nvidia-dev eae01502; no duplicate open PRs or merged redos found).

Tested

  • Windows 11 Pro for Workstations (build 26200), AMD Ryzen 7 7800X3D, 32 GB RAM, NVIDIA GeForce RTX 3090 24 GB (driver 32.0.16.1742 / 617.42), rustc 1.100.0 (0ef1bc386 2026-11-09), DXC 1.9.2602.17 (21d28f727); debug build for tests, release build for smoke.
  • cargo test -p xcl --lib -- depth_bias d24_units --test-threads=4 --show-output: 8 passed, 0 failed, 0 ignored, 0 SKIPPED (depth_bias_native_list9_and_bundle_fallback ran natively on ID3D12GraphicsCommandList9::RSSetDepthBias).
  • cargo test --workspace -j 8 --no-fail-fast -- --test-threads=8 --show-output: 1446 passed, 0 failed, 47 ignored across all workspace test targets, with 28 SKIPPED lines (not counted as passing coverage): 21 no wave64, 3 wave32, per-lane form: refused (unsupported), 3 no D3D12 debug layer, and 1 no heap windows. Notably, gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_0..3 all passed on 24 GB VRAM.

Regression run
60 s retail smoke run (-Seconds 60 -Title c1cc91a4, default settings, shared DXIL cache, Windows Developer Mode enabled), comparing merge-base main (e120925e3abc9956a8b9bae631969d32e8ccd43c) against PR #2858 (971f766cc9f442bc71623b2390bc285a63cdb456):

main:

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 3090
title: c1cc91a4
seconds: 60
exit: 0x0
frames shown: 1589 (PresentX 1589)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13964 MB

PR #2858:

xcl-smoke v1
commit: 971f766cc9f442bc71623b2390bc285a63cdb456
gpu: NVIDIA GeForce RTX 3090
title: c1cc91a4
seconds: 60
exit: 0x0
frames shown: 1582 (PresentX 1582)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13998 MB

No regressions observed (99.6% of baseline frames, 100.2% peak commit, 0 device removals, 0 crashes).

Code review findings (on 971f766cc9f442bc71623b2390bc285a63cdb456)

  1. crates/xcl/src/hle/d3d12x/list_x.rs (s.native_bias tracks unscaled console bias instead of PC-scaled bias): try_native_depth_bias scales bias.for_dsv(device_x::base_dsv_format(pso)) before calling RSSetDepthBias, while callers (on_create, on_set_pipeline_state, set_depth_bias_x) store the unscaled console DepthBias in s.native_bias. In on_set_pipeline_state (lines 916–934), old_dsv is read from s.bound_pso, which can be updated by an intervening compute, untracked, or non-dynamic graphics PSO without issuing RSSetDepthBias. Switching dynamic D16_UNORM_S8_UINT (118) with an active override (e.g. -1.0, sent as -256.0) -> non-dynamic D24S8 (45) or compute (0) -> dynamic D24S8 (45) leaves target == s.native_bias and target.for_dsv(old_dsv) == target.for_dsv(45), so RSSetDepthBias is skipped and the D24S8 pipeline draws with -256.0 (256× too strong). The reverse (D24S8 -> non-dynamic D16S8 -> dynamic D16S8) leaves -1.0 instead of -256.0 (256× too weak). Storing the PC-scaled DepthBias actually sent to RSSetDepthBias in s.native_bias and comparing target.for_dsv(new_dsv) != s.native_bias across native sync paths avoids this.
  2. crates/xcl/src/hle/d3d12x/real.rs (stream_pipeline 27 | 28 =>, lines 6966–6980): For non-mesh graphics pipeline streams carrying RASTERIZER1 (27) or RASTERIZER2 (28), DepthBias: d.DepthBias as i32 truncates any fractional f32 bias before pc_topology_type(&mut g) scales by 256 (for example, -0.5f32 on 118 becomes 0 instead of -128), unlike pc_stream_depth_bias (mesh/fallback streams) and DepthBias::for_dsv (dynamic/patched-PSO paths).

Not tested

AI use

…sync

Two cases left a bias on a D16_UNORM_S8_UINT (118) buffer in the wrong
units after xcl-dev#2858's first commit:

- The list kept the unscaled bias as the one RSSetDepthBias had, and a
  switch compared the formats of the previous and the new pipeline. A
  compute, untracked or non-dynamic pipeline in between changes the bound
  pipeline without a resend, so dynamic 118 (-1.0 sent as -256.0) ->
  non-dynamic or compute -> dynamic D24S8 drew with -256.0, and the
  reverse order with -1.0. try_native_depth_bias now records the scaled
  bias it sent, and every sync compares the target for the new pipeline's
  format with it.
- A non-mesh pipeline stream's RASTERIZER1 / RASTERIZER2 bias was cut to
  an integer before pc_topology_type scaled it: -0.5 on 118 became 0, not
  -128. It is now scaled first, as in pc_stream_depth_bias and
  DepthBias::for_dsv; other formats keep the truncation.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Decodere

Copy link
Copy Markdown
Contributor

Independent desktop NVIDIA verification of PR #2858 at head 971f766cc9f442bc71623b2390bc285a63cdb456 (1 commit; exact parent & merge-base: e120925e3abc9956a8b9bae631969d32e8ccd43c, pre-#2555 ancestor of main).

Environment

  • GPU: NVIDIA GeForce RTX 4060 (8 GB / 7956 MiB reported by D3D12), driver 32.0.16.2114 (NVIDIA 621.14, WDDM 3.2)
  • CPU / RAM / OS: Intel Core i5-11400F (6C / 12T), 32 GB RAM (31.9 GiB usable), Windows 11 Home 26200 (Developer Mode off)
  • Toolchain: rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC 1.9.2609.5, --release

Targeted release tests & build (971f766c)

Command Result
cargo test --release -p xcl --lib depth_bias_of_a_16_bit_depth_buffer_is_in_24_bit_units -- --nocapture 1 passed; 0 failed; 0 ignored; 1345 filtered out (0.00s)
cargo test --release -p xcl --lib native_depth_bias_follows_the_bound_pipelines_depth_format -- --nocapture 1 passed; 0 failed; 0 ignored; 1345 filtered out (0.00s)
cargo test --release -p xcl --lib a_d16s8_stream_gets_its_depth_bias_in_d24_units_in_either_order -- --nocapture 1 passed; 0 failed; 0 ignored; 1345 filtered out (0.00s)
cargo test --release -p xcl --lib a_d16s8_pipeline_gets_its_depth_bias_in_d24_units -- --nocapture 1 passed; 0 failed; 0 ignored; 1345 filtered out (0.00s)
cargo test --release -p xcl --lib hle::d3d12x::device_x:: (3 consecutive runs) 15 passed; 0 failed; 0 ignored; 1331 filtered out × 3 (0.20s, 0.17s, 0.20s)
cargo test --release -p xcl --lib hle::d3d12x::list_x:: 41 passed; 0 failed; 0 ignored; 1305 filtered out (0.68s)
cargo test --release -p xcl --lib hle::d3d12x::real:: 65 passed; 0 failed; 0 ignored; 1281 filtered out (0.36s)
cargo build --release -p xcl --bin xcl Clean build (1m 09s, exit 0)

Paired quiet 60 s retail smoke (title: 29640639)

Exact parent & merge-base (e120925e3abc9956a8b9bae631969d32e8ccd43c, cached baseline on the same machine):

xcl-smoke v1
commit: e120925e3abc9956a8b9bae631969d32e8ccd43c
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1602 (PresentX 1602)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13979 MB

PR #2858 (971f766cc9f442bc71623b2390bc285a63cdb456):

xcl-smoke v1
commit: 971f766cc9f442bc71623b2390bc285a63cdb456
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1599 (PresentX 1599)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13970 MB

Runtime log observations (xcl.log)

  • 9222 log lines, 0 [ERROR] / device-removal / panic lines.
  • Confirmed native dynamic depth-bias path active on RTX 4060: D3D12.X: native dynamic depth bias (OPTIONS16): supported (ID3D12GraphicsCommandList9::RSSetDepthBias).
  • Final draw/memory telemetry in log: 19543 draws with translated pipelines (0 skipped, 0 with PC pipelines), 1024 dispatches (0 skipped); last memory sample 13970 MB committed by the process; video memory 6115 MB local, 4651 MB non-local.

Limits of coverage

  • RTX 4060 supports OPTIONS16 native dynamic depth bias (ID3D12GraphicsCommandList9::RSSetDepthBias), so the 60 s retail smoke exercised the native dynamic depth-bias hardware path rather than the derived/patched-PSO fallback path on GPU.
  • Visual or pixel-level comparison on a title rendering into D16_UNORM_S8_UINT (118) depth buffers with non-zero constant DepthBias was not performed; ×256 scaling, saturation, stream subobject ordering, pipeline-switch re-emission, and patched-PSO handling were verified via the 4 new unit tests and the full device_x::, list_x::, and real:: suites, while the 60 s smoke confirms clean graphics execution and zero regression on title: 29640639.

AI disclosure: test execution and report drafting assisted by Google Antigravity (Gemini 4 Neon).

@tiagonleal

Copy link
Copy Markdown
Contributor Author

Both findings confirmed on 971f766 and fixed in 25814cc, a new commit on top (971f766 stays an ancestor).

  1. try_native_depth_bias now stores the scaled bias it sent in native_bias. on_set_pipeline_state and set_depth_bias_x compare target.for_dsv(<bound pipeline's DSV>) with it, so a compute, untracked or non-dynamic pipeline in between no longer hides a format change. The new test native_depth_bias_resends_after_a_pipeline_that_does_not_sync runs your sequence in both orders and fails on 971f766 (-256.0 kept instead of -1.0).
  2. stream_pipeline keeps the RASTERIZER1/2 float and, for 118, sets round(for_dsv(bias)) after pc_topology_type. The stream test now covers -0.5, 0.4 and 1.5 on 118 and 45 for both subobjects; it fails on 971f766 (0 instead of -128).

Ran on 25814cc: the depth-bias tests 3 times (10 passed each, including the device test depth_bias_native_list9_and_bundle_fallback), the full workspace suite (1443 passed, 4 failed: the fuzz_validate_and_gpu_0..3 E_OUTOFMEMORY ones that also fail on e120925), and the smoke on both commits (1614 frames each, no device removals). PR description updated.

AI use: Claude Code with Claude Fable 5.2 and Anthropic subagents (Claude Opus 5.5) wrote this; I checked it.

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.

3 participants