Repository navigation
d3d12: D24-as-D32 depth: triangle-only bias, slope term only with a slope factor (D3D-309 follow-up) - #3797
d3d12: D24-as-D32 depth: triangle-only bias, slope term only with a slope factor (D3D-309 follow-up)#3797JustDownTV wants to merge 1 commit into
Conversation
kacper-serewis
left a comment
There was a problem hiding this comment.
At 654e666 the D32-backed D24 fragment adjustment applies polygon bias to points and ordinary lines. At z=0.15625, +1 changes D24 code 2621440 to 2621441, enough to flip EQUAL and the stencil pass outcome. Keep quantization but select bias by rasterized primitive (GS output where applicable; wireframe triangles still get bias). Also isolate slope and clamp tests with zero constant bias: accepting arbitrary sloped mismatch as KNOWN on native D24 hides other errors. Eight synthetic fragment modules and VS/GS passed Vulkan 1.0 validation. CPU reference and source review, no driver readback. AI-assisted.
Desamodeth
left a comment
There was a problem hiding this comment.
Tested 654e666 on Windows 11 / RTX 5070 (621.24), MSVC Release, Vulkan validation on. The unmodified native D24 run had 20 PASS, 12 KNOWN bias differences and 6 clamp skips. To exercise the fallback on the same GPU I masked only the D24 depth/stencil attachment bit in the test fixture's format query. With the PR device implementation all 38 D32S8 cases passed, including GS without PS and shader-written depth; no Vulkan validation errors were reported. With f847748's device implementation under the identical mask, LESS with bias +1/+2 passed all 4096 pixels instead of zero, and EQUAL passed zero instead of 4096. The control stopped later at clamp pipeline creation. Restoring the unmodified fixture reproduced the native result. Synthetic draws only. AI-assisted.
|
Tested 654e666 on Windows 11, Visual Studio 2026, RelWithDebInfo, Vulkan SDK 1.4.363.1, with
So the fix holds on RDNA4 as well as RDNA2. These are all triangle draws, so they don't cover @kacper-serewis's point/line bias concern. |
whattheflop
left a comment
There was a problem hiding this comment.
AI-assisted CPU review at 654e6664. The native MSVC 19.38 build passed. SPIRV-Tools validated ten synthetic inputs, 24 patched outputs, two f16-linked stages and four built-in fragment variants. The patch preserved explicit depth on both live returns, skipped the valid EarlyFragmentTests input, and kept the f16 input locations after the runtime linker relocated them. This complements the existing point/line review.
One additional case: for constant bias +3 with a zero slope factor, the exact helper still emits depth derivatives, their maximum and an OpFMul by 0.0. Direct3D ?15.10 requires skipping that multiply when the factor is zero, specifically because an edge-on wireframe triangle can yield an infinite slope and turn constant-only bias into NaN. Consider constructing the bias from its constant term and emitting slope work only when bias.slope != 0; a structural test can verify the helper emits no derivatives for constant-only bias. I checked the emitted sequence, not the driver-level result for edge-on triangles. Helper ? Direct3D 11.3 specification ?15.10
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>
|
Tested
Reviewed the device change against my original branch: the new parts are the GS-without-PS case and the docs, and both look right. The built-in fragment stage goes in after whatever stages exist, so a VS + GS pipeline gets it the same as a VS-only one. On the overlap with #2718: that PR used to add a Thanks for carrying it, @JustDownTV. AI-assisted (Claude Code). |
|
Tested 654e666 (based on current This GPU has no D24 attachment, so it takes the path the PR changes: the log says
AI-assisted (Claude Code). No game files. |
|
AI-assisted review of 1. Points and lines (already raised by @kacper-serewis): confirmed on real D3D12. D3D12 applies no bias to them. With a D24 target, a line list at z=0.5 and 2. Slope-scaled bias error grows with I drew a plane z = 0.4 + 0.15·x_clip on a 64×64 target:
Shadow passes commonly use slope factors in the 2–16 range, so this adds per-quad noise to stored depth. Possible fixes:
3. Wireframe triangles get a wrong slope term (minor, partly older than this PR). The derivatives along a rasterised edge are not the polygon's slope. On a vertical edge with slope 1, the expected term is 131072 steps, and the 3090's D3D12 gives 131066. The fallback gives about 106431. The native Vulkan path is also off here, by about 1.14×. 4. Minor doc point. EQUAL against a depth bound that isn't on the 24-bit grid fails even with zero bias. Case: viewport Checked and correct:
Points 1, 2 and 4 are measured; point 3 is measured against the 3090's D3D12 only. |
|
Tested 654e666 on Windows 11, AMD Radeon iGPU gfx1036 (driver 26.10.1) and RTX 5080 (driver 616.92):
No negative control run. AI-assisted. |
654e666 to
1f23faf
Compare
|
Thanks @kacper-serewis, both points were right. Fixed in
While at it: the shader-written-depth case now uses EQUAL against an off-grid cleared value (it could not fail before), the test fails on any validation error, the bias arithmetic is AMD gfx1036 iGPU (D32S8 path), |
1f23faf to
1330310
Compare
|
Thanks to everyone who tested
AMD gfx1036 iGPU (D32 path), |
|
Tested 1330310 on RTX 3090 (NVIDIA 616.56, Windows 11 23H2): pass. This confirms the fixes for my points 1 and 2. AI-assisted.
|
kacper-serewis
left a comment
There was a problem hiding this comment.
Rechecked 1330310 on Windows RTX 5070, driver 616.56. Native D24 had 65 passes, 12 known constant-bias results and six clamp skips. A private D32-backed override passed all 83. Points and lines retained depth code 5033000 at bias 1000 with and without PS; the constant-only stage emitted zero derivatives and the slope stage two. No unexpected failures or validation errors. Both earlier review findings are resolved. Synthetic tests, AI-assisted.
|
Approved by static review (no build here). The conversion is right: the bias term scales Only gap, pre-existing and scoped out in the body: the viewport Approve. AI-assisted review, no game files, no GPU. |
…lope factor (D3D-309) Review follow-up to the D3D-309 change already on main (654e666): - Points and lines are quantised but not biased, as in D3D (only triangles, wireframe ones included, take DepthBias). - The slope term and its derivatives are emitted only when SlopeScaledDepthBias is non-zero (D3D11.3 15.10), so an edge-on triangle cannot turn a constant-only bias into NaN. The bias operations carry NoContraction, and the clamp before OpConvertFToU uses NMin/NMax. - The D24-as-D32 check is computed once in CreateGraphicsPipelineState. - d3d12_depth24_draw_test: slopes in y and in both axes, slope factors -2, 8 and 16, clamps, no constant bias in the slope cases (so native D24 is held to them), LINE and POINT pipelines, a pixel shader writing an off-grid depth, the built-in stage's derivatives checked on the CPU, and failure on any validation error. The doc states the ~1 + |SlopeScaledDepthBias| step bound and what is not covered.
1330310 to
b4f9cfd
Compare
|
Tested b4f9cfd on Windows 11 (MSVC BuildTools 2022, Ninja, RelWithDebInfo, -j 4; no Vulkan validation layer installed), one GPU at a time, device line confirmed: AMD Radeon RDNA2 iGPU (driver 26.10.1) and NVIDIA RTX 5080 (driver 616.92). Ran AI-assisted. |
|
Tested b4f9cfd on NVIDIA GeForce RTX 3070 (Ampere), driver 610.88, Windows 11 Pro 10.0.26200: pass
Built with MSVC through |
Claimed in the coordination issue as: D3D-309 (claim; @marquezinii passed on it, @UlteriorMotive00 released it)
Depends on: none (based on
main0db7fed5).Status: the first version of this PR (
654e6664) is already onmain(merge0a9b3ff3). This PR now carries only the review follow-up on top of it, rebased onmain0db7fed5asb4f9cfd9: no bias on points and lines (@kacper-serewis), the slope term and its derivatives only with a non-zero slope factor (@whattheflop),NoContractionon the bias andNMin/NMaxbefore the conversion, the D24-as-D32 check computed once, the stricter test and the doc's bound and "not covered" notes (@CharlyRien). Retested after the rebase withLEONIDA_VK_VALIDATION=1: AMD gfx1036 iGPU (D32 path)d3d12_depth24_draw_test83 of 83, 0 validation errors, andd3d12_m3_frame_testpasses; RTX 5070 Ti (native D24) 65 pass, 0 failures. The description below is the whole change as reviewed.What and why
On a GPU without a D24 attachment (AMD) the layer stores
D24_UNORM_S8_UINTasD32_SFLOAT_S8_UINT. In draws that breaks the two D3D rules of the finding (@marquezinii, measured by @Vonsek on #291):RasterizerState.DepthBiasis in fixed D24 units, but Vulkan scales a float attachment's constant bias by the primitive's exponent.depth24_clear_value) and fragment depth is not, so an EQUAL test at a cleared value fails.This builds on @UlteriorMotive00's branch (
fix/d3d-309-d24-float-depth,0ff3f8f6), which they offered when they released the item; they are credited as co-author of the commit. For a single-sampled graphics pipeline with depth enabled on such a target, the pixel stage writesFragDepth = quantise(z + bias):bias = DepthBias / (2^24 - 1) + SlopeScaledDepthBias * max(|dz/dx|, |dz/dy|), clamped byDepthBiasClamp, as the D3D11.3 spec gives it for a UNORM target, for triangles only (wireframe included). POINT and LINE pipelines get the quantisation with no bias, as in D3D; a GS takes and emits triangles here;quantiseisdepth24_convert.comp's conversion (round to nearest even ofz * (2^24 - 1)in float, NaN and negatives to 0), so a fragment holds exactly the float a D24 code reads back as;SlopeScaledDepthBiasis non-zero, as D3D11.3 15.10 requires (0 times an edge-on triangle's infinite slope would be NaN; review by @whattheflop);src/depth24_fragment.hedits the translated pixel shader's SPIR-V, or builds a pixel stage for a pipeline that has none. A shader that writes its own depth has it quantised, unbiased, as in D3D.Not covered (in
docs/d3d12_vulkan.md): multisampled targets and shaders withEarlyFragmentTestskeep the old path and are logged; the slope term of wireframe triangles comes from the edge's derivatives, not the plane (the constant term is exact); a depth that reaches the viewport'sMinDepth..MaxDepth(D3D clamps before quantising, here the quantised value is clamped after and only withDepthClipEnable = FALSE, so an EQUAL test at a bound off the 24-bit grid fails, as onmain; measured by @CharlyRien; see #1703); early depth testing is off for these pipelines because of theFragDepthwrite. The slope term can be about1 + |SlopeScaledDepthBias|24-bit steps from D24 hardware's plane-exact one, since the derivative of the interpolated float depth is about one ulp off (measured by @CharlyRien: 13 steps at a factor of 16 on an inexact plane, which the test now reproduces). Computing the slope once per primitive would make it exact; that is a follow-up.Changes on top of the original branch:
main, where graphics PSOs take a geometry shader and link f16 halves before creating the modules (Runtime: geometry shaders in graphics PSOs (VS + GS + PS) #2109, SHADER-378). The patch runs in the module loop, after the f16 link, onstage.words.stage_infos.size() == 1, false once a GS is there, so such a pipeline lost its bias entirely; it now tests for a missing PS.sizeno longer shadowsvulkan_replay.h(C4459). The D24-as-D32 check is computed once inCreateGraphicsPipelineState. The patcher's clamp beforeOpConvertFToUusesNMin/NMax, defined for a NaN.Tested
Windows 11, MSVC 14.51, Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0,
LEONIDA_VK_VALIDATION=1(the test now fails on any validation error).d3d12_depth24_draw_testchecks, with no PS, with a PS, and with a pass-through GS and no PS: the finding's LESS cases (bias -1 as positive control, 0, +1, +2) and EQUAL at the cleared 0.15625; flat stored depth for bias 0/3/-5/1000; sloped stored depth with slopes in x, in y and in both with different sizes (somaxand not|dz/dx|alone or a sum), slope factors 1, -2, 8 and 16 (the last on a plane whose depth is off the float grid), with and without clamp, and with no constant bias so a native D24 is held to them (only cases with a constant bias may printKNOWNthere, D3D-325). Then LINE and POINT pipelines: LESS one step above must pass the same drawn pixels with bias 0, +1 and +2/slope 1, EQUAL at the cleared (off-grid) 0.15625 must pass, bias 1000 must leave the stored code alone. Then a PS writing an off-grid 0.15625 as its depth, with bias 0 and 7: EQUAL against the cleared 0.15625 and the stored code. On the CPU (so also without a GPU): the built-in stage has no derivatives for a constant-only bias.AMD Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, RDNA2), AMD Windows driver 25.10.30.02 (selected with
VK_DRIVER_FILES; D24 stored as D32S8):main'sd3d12_vulkan_device.cppthe finding reproduces: LESS with bias +1/+2 passes 4096 of 4096 (expected 0), EQUAL at the cleared 0.15625 passes 0 of 4096, stored depth with bias 3/-5/1000 is off by -1/+3/-500 codes.max_slope = |dz/dx|, the y and xy cases fail (up to 65536 steps); without quantising a shader-written depth, both EQUAL checks fail.d3d12_m3_frame_test(translator-shaped shaders on a D24S8 DSV with depth enabled, so the patched path): pass, 0 validation errors.NVIDIA GeForce RTX 5070 Ti, driver 621.14 (native D24, the new path stays off): 65 pass, 0 failures;
KNOWNonly for the constant term (D3D-325, #2718); the clamp cases skip because the test device leavesdepthBiasClampoff.Translator output: the 38 pixel-shader fixtures of
tests/semantic/fixtures/disasmtranslated withtools/rdna2spirv.pypassspirv-val --target-env vulkan1.2before and afterdepth24_patch_fragment(bias 0, and bias 3 / slope 1.5 / clamp 0.01: 76 of 76), and so does the built-in depth-only stage; a hand-written PS that exports depth (exp mrtz) patches to a valid module with the quantisation before itsOpReturn(local checks, not committed).ctest --test-dir runtime/buildon both GPUs: everything passes exceptd3d12_vulkan_device,d3d12_typeless_rtvandroot_signature_oracle, which fail the same way onmainon this machine.Overlap: #2718 (D3D-325, exact bias on native fixed-point targets) edits the same rasterizer lines; the two are complementary, and whichever lands second needs a small rebase. #1451 (
RSSetDepthBiasas dynamic state) isn't onmain; once it is, a D24-as-D32 pipeline with dynamic bias needs its bias passed to the pixel stage instead of baked. #1703 (viewport depth clamp with clipping on) would also clamp the quantisedFragDepth.AI-assisted: yes.
Checklist
docs/design.md, with how it was confirmed. (No translator rule: this is the D3D12 layer; the behaviour is documented indocs/d3d12_vulkan.md.)