Skip to content

d3d12: realize console streams with their depth-bounds flag and back-face stencil masks (RT-DEPTHBOUNDS-STREAM-EXTRAS) - #1695

Open
thiefyzheng wants to merge 3 commits into
ProjectLeonida:mainfrom
thiefyzheng:fix/depthbounds-stream-extras
Open

thiefyzheng wants to merge 3 commits into
ProjectLeonida:mainfrom
thiefyzheng:fix/depthbounds-stream-extras

Conversation

@thiefyzheng

Copy link
Copy Markdown
Contributor

RT-DEPTHBOUNDS-STREAM-EXTRAS — Depends on: #815

Claimed on #1608 (#1608 (comment)). This fixes @djrolfes' integration finding on #815 (#815 (comment)). @kotekompotek: happy to have this folded into #815 instead of a stacked PR — say so and I'll drop it.

Note: the diff includes #815's commit until it merges, since this branches from its head efea97331.

What

  • PipelineState::resolve_type's std::call_once block (runtime/shims/d3d12_vulkan_device.cpp:7059) realized a console stream with create_graphics(stream.graphics, &stream.ms, &stream.as), silently dropping stream.extras (the depth-bounds flag, d3d12: support hardware depth-bounds testing and OMSetDepthBounds #815's GraphicsStreamExtras) and stream.back_stencil (DEPTH_STENCIL2 back-face masks, which parse_pipeline_stream fills right next to the front ones). The eager CreatePipelineState branch passes both. It now passes stream.back_masks?&stream.back_stencil:nullptr and &stream.extras — argument-for-argument identical to the eager call at ~12104.
  • prepare_console_stream (~10367) set the pipeline cache key from pipeline_key(stream.graphics) alone; the plain D3D12_DEPTH_STENCIL_DESC the key hashes cannot see the depth-bounds flag or back-face masks, so two console PSOs differing only in those would collide. The key now post-mixes back_masks, back_stencil.read/.write and extras.depth_bounds_test with the same cached_pso::KeyBuilder/sha256_hex idiom pipeline_key itself uses.
  • Regression test in runtime/test/d3d12_mesh_test.cpp: a console stream (device slot 86, mesh-shader subobject 24) carrying DEPTH_STENCIL1 with DepthBoundsTestEnable and a D32_FLOAT DSV, drawn once inside OMSetDepthBounds(0, 1) (red — depth 0 is inside) and once inside OMSetDepthBounds(0.5, 1) (blue — the discard only happens if the flag reached the pipeline). Without the source fix both draws come out red.

What was run

  • Nothing was compiled or executed on the author host: no GPU, no Vulkan SDK, MSVC-only runtime tests. Not GPU-confirmed.
  • An independent mechanical review verified, against the actual branch sources: the create_graphics signature match (7119–7123), the PipelineStream fields and their parse fill, every identifier the test uses in scope with types lined up, fence values (the new bounds_fence pre-increments from 3, first Signal is 4 — no collision with the earlier Signals 1–3), the readback footprint/offset parity with the file's existing usage, slot 86 = console CreatePipelineState, and subobject 24 = the console mesh shader (consistent with the existing stream test at line 341 and the console→PC remap in d3d12_xs_pso_stream.h:31). That review caught two real defects, both fixed here: the test's second-frame depth barrier indicated a COMMON→DEPTH_WRITE transition from a buffer already in DEPTH_WRITE (now conditional on first_frame), and the cache-key collision above.
  • The guard demonstration owed (run d3d12_mesh_test with the one-line source change reverted and show the bounds case fails) needs a depth-bounds-capable Vulkan device, which the author host does not have. The test skips cleanly when DepthBoundsTestSupported is FALSE or in pc_only builds.

Attribution

AI-assisted: implementation (Claude Sonnet 5.5 worker), independent review and follow-up fixes (GLM 5.3 Flash worker), orchestration and verification by the contributor. No game files were used or are needed; no game data in this PR.

@kawaii-shakal kawaii-shakal 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.

Review (read-only, not run). The pipeline-cache key now including the back-face masks and the depth-bounds flag is an important catch, and the plumbing through OMSetDepthBounds, bundles and Draw looks complete. Three points:

  1. Nothing has been compiled or run (per the description). This touches vulkan_replay.h, d3d12_com.cpp and the device; please get at least a build and d3d12_mesh_test from someone with a depthBounds-capable GPU before merge. I can run it on an RTX 5070 Ti if it helps.
  2. Feature enable on the non-standalone device. enable.depthBounds = chosen_features.depthBounds is added in create_vulkan_device_standalone. DepthBoundsTestSupported and the PSO check read state_.info.features.depthBounds. When the device comes from the runtime's own creation path (not standalone), is info.features the enabled set or the supported set? If it is the supported set, a PSO could enable depthBoundsTestEnable on a device that never enabled the feature (a VUID).
  3. dyn_list[9]: with #778 (adds VK_DYNAMIC_STATE_SAMPLE_LOCATIONS_EXT to the same list) the maximum becomes 7 + 1 + 1 = 9, so 9 is exactly enough. A static_assert or a comment counting the entries would keep the next addition from overflowing it.

AI-assisted review (Claude Code, Claude Opus 5.5).

@potatoe0100

Copy link
Copy Markdown
Contributor

Tested 766c6c42e merged onto main bcaa13dad (clean), native Windows 11 Pro 26H2 (build 26300), NVIDIA GeForce RTX 4060 (depthBounds and mesh shaders supported), driver 621.14, VS 2022 RelWithDebInfo + Ninja.

  • Full ctest -j 6: 409 of 409 passed, same as main; that includes d3d12_mesh, d3d12_bundle_forbidden, d3d12_close_parity, d3d12_com and d3d12_vulkan_device.
  • d3d12_mesh_test with LEONIDA_VK_VALIDATION=1: exit 0, 0 VUIDs, "DispatchMesh over X, Y and Z and a stream VS draw matched all 4096 pixels".
  • run_gpu_tests.py: 209/263 passed, 53 skipped, identical to main.

AI-assisted testing (non-developer tester).

@adrianmjim

Copy link
Copy Markdown

Tested 766c6c42e applied onto main 42537e7b6 on AMD: Radeon RX 7900 XT (RDNA3, depthBounds = true), AMD driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, VS 2022 RelWithDebInfo. No game files.

Tests:

  • The 14 related tests pass, plain and with LEONIDA_VK_VALIDATION=1 (no messages): d3d12_mesh, d3d12_com, d3d12_close_parity, d3d12_bundle_forbidden, d3d12_vulkan_device, pso_stream, d3d12_draw, d3d12_pso_validation, d3d12_depth24_draw, d3d12_geometry, d3d12_multi_viewport, d3d12_rect_list, d3d12_quad_index, d3d12_translated_draw.
  • Whole ctest -j 6: 399 of 402, with the same three failures as main.

Three things in the code:

1. OMSetDepthBounds with a NaN. The D3D12 reference says of both Min and Max: "NaN values silently convert to 0". At replay the PR makes any NaN an empty range ((mn != mn) || (mx != mx) → set_depth_bounds(1, 0)), so every pixel is discarded. OMSetDepthBounds(NaN, 0.5) should test [0, 0.5]. (0.2, NaN) happens to come out right, because D3D12's [0.2, 0] is empty too. Mapping NaN to 0 first and then applying the existing clamp/empty rules would match D3D12.

2. The extra cache-key mix is dropped. pipeline->set_cache_key(pipeline_key(stream.graphics) + leonida::sha256_hex(extra.data())) appends a second 64-character hash to a key that is already a 64-character sha256_hex. cached_pso::encode()/check() do padded.resize(key_bytes) with key_bytes = 64, so the back-face masks and the depth-bounds flag never reach the cached blob. So two console streams that differ only in those return byte-identical GetCachedBlob blobs, and the mix has no effect. Hashing the two parts together, for example by feeding extra's bytes into the same KeyBuilder before the sha256_hex, would keep the key at 64 characters.

3. The new d3d12_mesh case doesn't go through the new resolve_type path. ctest runs it without LEONIDA_PIPELINE_COMPILE=on-demand, so slot 86 goes to the eager CreatePipelineState path, which this PR also changes to pass &stream.extras. The test would pass without the resolve_type change.

I tried it with LEONIDA_PIPELINE_COMPILE=on-demand. d3d12_mesh then fails, but it fails the same way on main without this PR: the console stream from slot 86 isn't created (SUCCEEDED(console_hr) && console_pso.p, line 353 with the PR, line 344 on main). So the test can't cover the on-demand path at the moment. That failure is separate and predates this PR.

thiefyzheng added a commit to thiefyzheng/leonida-1 that referenced this pull request Nov 25, 2026
…ilder) and map NaN depth bounds to 0

Review findings on ProjectLeonida#1695 (adrianmjim, RX 7900 XT):
- The extras hash was appended to the 64-char pipeline key, but cached_pso
  resizes the key to 64 bytes, so the second hash never reached the cached
  blob. pipeline_key now takes an optional KeyBuilder hashed with the
  graphics fields into one key; other call sites are unchanged.
- OMSetDepthBounds: NaN converts to 0 per the D3D12 spec; map NaN to 0
  first, then apply the existing clamp/empty rules.
@thiefyzheng

Copy link
Copy Markdown
Contributor Author

@adrianmjim — thank you for the 7900 XT run and the three review points. Both code findings are fixed in 6f710e57 (a fast-forward on the tested 766c6c42, so your results still describe the base):

1. Cache-key mix truncated away — fixed. cached_pso resizing the key to 64 chars means the appended second hash never reached the blob. pipeline_key (graphics overload) now takes an optional const KeyBuilder* extra hashed into the same builder before its single sha256_hex; prepare_console_stream passes the back-masks/back-stencil/depth-bounds builder. All other pipeline_key call sites keep byte-identical keys (default nullptr, no bytes appended): the trace print at ~7285, the compute overload at ~10371, shared_native_graphics at ~10388, CreateComputePipelineState at ~11967, CreateGraphicsPipelineState at ~11984 (eager CreatePipelineState doesn't call pipeline_key at all).

2. NaN depth bounds — fixed on this branch, with a note for @kotekompotek. Replay now maps NaN to 0 first (vulkan_replay.h ~4893), then the existing clamp/inverted-is-empty rules unchanged — so (NaN, 0.5) behaves as [0, 0.5] and (0.2, NaN) as [0.2, 0] (empty). Record-time acceptance and bundles are untouched. This is replay-side code that came in with #815; kotekompotek, if you would rather carry it in #815 itself, say so and I'll drop the hunk here and rebase on your fix.

3. Test covers only the eager slot-86 path — acknowledged. Under plain ctest LEONIDA_PIPELINE_COMPILE isn't on-demand, so CreatePipelineState (~12090-12122, eager create_graphics at 12118) is what the mesh case exercises; the resolve_type lazy realization at ~7055/7063 is not covered, because on-demand mode fails earlier — console stream from slot 86 not created (the SUCCEEDED(console_hr) bail at d3d12_mesh_test.cpp:349/~353), identically on main. I have not touched that pre-existing failure; the case still guards the eager path plus the replay-side bounds, and the resolve_type change remains a code-path-observation fix for now.

New commit is again not compiled and not GPU-confirmed on the author host (no Vulkan SDK / MSVC here); it needs one more hardware look like yours when convenient.

@adrianmjim

Copy link
Copy Markdown

Rechecked 6f710e574 on the RX 7900 XT (driver 32.0.32029.2010, Windows 11 26300, VS 2022 RelWithDebInfo). Both points are fixed:

  • The extras now go through the same KeyBuilder, so the key stays one 64-character hash.
  • OMSetDepthBounds(NaN, 0.5) is in the state sweep and gives the same result as host D3D12 here (0x00000000 | 0x00000000 | match).

The 14 related tests pass with LEONIDA_VK_VALIDATION=1 and no VUIDs. Whole ctest -j 6: 398 of 402 pass or skip. The failures are d3d12_texture_tiles, d3d12_clear and d3d12_geometry_views, which fail on main here too, plus d3d12_api_descriptor_copy, which failed once under -j 6 and passes 3 of 3 alone.

@thiefyzheng

Copy link
Copy Markdown
Contributor Author

@adrianmjim — thank you for the recheck on 6f710e57. Recording the state for whoever merges: the head now carries an independent hardware verification on RDNA3 covering all three of your original points (single 64-char key, NaN→0 matching host D3D12, eager-path suite green with LEONIDA_VK_VALIDATION=1, 0 VUIDs), with the only ctest failures being the three pre-existing main baseline ones plus one -j 6 flake that passes alone.

Open item unchanged: @kotekompotek, whether the NaN hunk stays here or you carry it in #815 — the default assumed here is that it stays unless you'd rather have it; a maintainer can also drop it at merge. Everything else on this PR is reviewer-verified. AI-assisted.

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 25, 2026
kotekompotek and others added 3 commits November 25, 2026 19:11
…face stencil masks

PipelineState::resolve_type built a lazily realized console stream with create_graphics(graphics, ms, as) only,
dropping stream.extras (DepthBoundsTestEnable) and stream.back_stencil (DEPTH_STENCIL2 back-face masks) that the
eager CreatePipelineState branch passes. Pass both. Adds a mesh-test case that draws through a console stream with
DepthBoundsTestEnable and checks OMSetDepthBounds discards the draw. Not compiled or run on this host (no GPU,
no Vulkan SDK, MSVC-only build).

The new test's per-frame barrier array builds the depth transition unconditionally but submits
only one barrier after the first frame, so the COMMON before-state never fires on later frames;
state it conditionally on first_frame anyway so the array matches what the debug layer sees.
prepare_console_stream's cache key now folds in stream.back_masks/back_stencil and
stream.extras.depth_bounds_test (post-mixed with the same KeyBuilder/sha256_hex idiom
shared_native_graphics uses), so console PSOs differing only in these stop colliding.
…ilder) and map NaN depth bounds to 0

Review findings on ProjectLeonida#1695 (adrianmjim, RX 7900 XT):
- The extras hash was appended to the 64-char pipeline key, but cached_pso
  resizes the key to 64 bytes, so the second hash never reached the cached
  blob. pipeline_key now takes an optional KeyBuilder hashed with the
  graphics fields into one key; other call sites are unchanged.
- OMSetDepthBounds: NaN converts to 0 per the D3D12 spec; map NaN to 0
  first, then apply the existing clamp/empty rules.
@thiefyzheng
thiefyzheng force-pushed the fix/depthbounds-stream-extras branch from 6f710e5 to bb8e113 Compare November 25, 2026 14:26
@thiefyzheng

Copy link
Copy Markdown
Contributor Author

Rebased onto #815's new head a635392c6 (post its 71a769d7d rebase) — our two commits are now 6b7bf490 and bb8e1131, conflict-free (this PR never touches d3d12_com.cpp, where #815's own conflict was). The commit contents are unchanged from the previously verified 766c6c42/6f710e57, so the hardware receipts above (RTX 4060 full suite, RX 7900 XT recheck of both review fixes) carry over modulo the new base; one more spot-check on the new head is welcome when convenient — @potatoe0100, @adrianmjim, either of you has the fastest boots for this.

The NaN hunk stays in this branch as agreed default unless @kotekompotek moves it into #815 — say the word and I'll drop it here. AI-assisted (orchestrator).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Ran #1695 at head bb8e11314 on a depth-bounds-capable GPU.

Config: Windows 11 (26300), RTX 4060 desktop, NVIDIA 610.88 (depthBounds = true), MSVC 19.44, RelWithDebInfo, Vulkan SDK 1.4.363.0. Test targets only, no game files.

  • Builds with MSVC: d3d12_mesh_test, d3d12_vulkan_device_test, d3d12_com_test, d3d12_close_parity_test, d3d12_bundle_forbidden_test (0 errors).
  • ctest: d3d12_mesh, d3d12_vulkan_device, d3d12_com, d3d12_close_parity, d3d12_bundle_forbidden all pass. The new depth-bounds case does run here (not skipped): red inside OMSetDepthBounds(0, 1), clear blue inside (0.5, 1).
  • Guard demonstration (the one owed): with the resolve_type line put back to create_graphics(stream.graphics,&stream.ms,&stream.as) and d3d12_mesh_test rebuilt, the test still passes. So under plain ctest the new case does not catch the dropped flag; slot 86 takes the eager path, as in your finding 7161103050.
  • LEONIDA_PIPELINE_COMPILE=on-demand: fails at line 353: SUCCEEDED(console_hr) && console_pso.p, the same with and without the fix, before the depth-bounds case. That is consistent with 7161103050 on NVIDIA (I compared the PR head with and without the fix line, not main).

So: it compiles and nothing regresses in these 5 tests, but the lazy-path fix itself is not exercised by any test I ran. Not covered: full runtime build, full ctest, AMD/Intel.

AI-assisted: yes (Claude Code ran the tests)

@Tauwni

Tauwni commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

@thiefyzheng heads-up from RT-STATE-AUDIT-PIPELINE (issue #2464, part B): this PR is listed there for depth bounds and back-face stencil, but it carries needs rebase and conflicts with main (2859d0c9b). Once it's updated, I can run it on a Mac (Apple M3, under Wine) and report here. One thing worth knowing for it: MoltenVK and KosmicKrisp both lack depthBounds, so on Macs d3d12_vulkan_device.cpp:12096 refuses every depth-bounds PSO, and carrying the flag on more paths would turn more PSOs into refusals there (finding: #1608 (comment)).

AI-assisted (Claude Code, Claude Opus 5.5).

@thiefyzheng

Copy link
Copy Markdown
Contributor Author

Status for @Tauwni and whoever tests this next: #1695 is stacked on #815 (our two commits sit on #815 head a635392c6), and #815 itself is currently dirty against main (ec91d695c). Rebasing this PR first would not build without #815's changes, so it waits for #815's own rebase; once that lands we rebase onto it and re-run the 14 related tests. No change to the commits (6b7bf490d, bb8e11314) until then.

AI-assisted (Claude Code).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants