Skip to content

d3d12: the on-demand stream carries the depth-bounds flag too (DEPTH-BOUNDS-ON-DEMAND-STREAM) - #2234

Open
star41905453-coder wants to merge 1 commit into
ProjectLeonida:mainfrom
star41905453-coder:depth-bounds-stream
Open

star41905453-coder wants to merge 1 commit into
ProjectLeonida:mainfrom
star41905453-coder:depth-bounds-stream

Conversation

@star41905453-coder

@star41905453-coder star41905453-coder commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

A stream realized on demand (PipelineState::set_pending_stream, resolved on first use) went through a second create_graphics call site that passed neither the depth-bounds flag nor the back-face stencil masks, so a title that bound a console PSO before it was realized silently lost DepthBoundsTestEnable — the depth-bounds test then always on and clipping nothing, with no refusal and no log. This passes both there, as the eager call site already does.

Test (d3d12_mesh_test): the stream PSO with DepthBoundsTestEnable is now drawn through, not only created.

  • The depth attachment is cleared to 0.0, outside the first bounds, so the bounds test is what decides and not the clear (review point: clearing to 1.0 left the clear value inside 0.9..1.0).
  • The bounds draws write into their own readback buffer, so the mesh image rows compared later are untouched (review point: they overwrote pixels and broke that comparison).
  • 0.9..1.0 must paint nothing, 0.0..1.0 must paint the whole viewport. Where the device has no depthBounds (llvmpipe here) creation is refused instead, and a success there would mean the flag was dropped.

Run: d3d12_mesh, d3d12_mesh_on_demand and d3d12_mesh_heavy_cache pass locally. The draw half still needs a depth-bounds-capable GPU: this box has none, so it skips here.

Item: DEPTH-BOUNDS-ON-DEMAND-STREAM, claimed in #1608.

@star41905453-coder star41905453-coder changed the title probe d3d12: the on-demand stream carries the depth-bounds flag too (DEPTH-BOUNDS-ON-DEMAND-STREAM) Nov 26, 2026
@ecox982745

Copy link
Copy Markdown

The depth-bounds forwarding works on an RTX 4090 (Windows, NVIDIA 616.64). I tested 6b7d0fd219 and a control build with only the resolve_type call to create_graphics restored to its parent version. Both used the same synthetic 64×64 draw and Vulkan validation.

Runtime Compilation mode Yellow pixels with bounds 0.9–1.0 (expect 0) Yellow pixels with bounds 0.0–1.0 (expect 4,096)
PR head Eager 0 4,096
PR head On demand 0 4,096
Forwarding reverted Eager 0 4,096
Forwarding reverted On demand 4,096 4,096

Three additional on-demand runs on the PR head gave the same pixel counts. Both passing modes completed the full mesh executable under --pc-stream-only, including its original pixel comparison, with zero Vulkan validation errors through teardown.

The current test needs a few changes to catch this. The unmodified eager test painted all 4,096 pixels in both draws. Clearing the D32 attachment to 1.0 puts it inside both requested ranges. Clearing to 0.5 gives a value inside exactly one range. Vulkan compares the depth already in that attachment. Vulkan depth-bounds specification

For the capable-device branch I created the bounds PSO through console device slot 86, waited for each draw with a fresh fence value, and counted exact RGBA pixels directly from the readback. This preserves the original quadrant image for the final mesh comparison. With those readback corrections but the existing PC create(...) helper, the reverted build still passed on demand. Switching to slot 86 made the control fail as shown above.

I used --pc-stream-only to get past the earlier console-numbered mesh assertion described in the PR. The bounds draw below explicitly calls slot 86 and uses the VS/PS stream. This verifies the depth-bounds change on the on-demand console path. Back-face stencil masks and devices without depth bounds were outside this check.

Test-only patch used on 6b7d0fd (depth-bounds-capable branch)
--- a/runtime/test/d3d12_mesh_test.cpp
+++ b/runtime/test/d3d12_mesh_test.cpp
@@ -492,10 +492,8 @@
             queue->ExecuteCommandLists(1, lists);
             EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::invalid_draw);
         }
-        // Depth bounds through the stream and the lazy realization: these PSOs are created pending (the record is
-        // realized on first use), and a depth-bounds flag dropped on the way into create_graphics would let the
-        // fragment through. The VS writes z = 0, so bounds of 0.9..1.0 must leave the target blue and 0.0..1.0 must
-        // paint it yellow.
+        // Exercise the console stream in both compilation modes. Clearing its depth attachment to 0.5 makes
+        // 0.9..1.0 reject every sample and 0.0..1.0 accept every sample.
         D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{};
         EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options,
                                                      sizeof(bounds_options))));
@@ -526,15 +524,22 @@
             bounds_stream.add(Subobject<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, DXGI_FORMAT>{
                 .inner = DXGI_FORMAT_D32_FLOAT});
             Ref<ID3D12PipelineState> bounds_pso;
-            EXPECT(SUCCEEDED(create(device.p, bounds_stream, bounds_pso)) && bounds_pso.p);
-
-            const auto draw_bounded = [&](float low, float high) {
+            using Create = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**);
+            const auto desc = bounds_stream.desc();
+            const HRESULT bounds_hr =
+                reinterpret_cast<Create>(slot(device.p, 86))(device.p, &desc, IID_PPV_ARGS(&bounds_pso.p));
+            EXPECT(SUCCEEDED(bounds_hr) && bounds_pso.p);
+
+            Ref<ID3D12Fence> bounds_fence;
+            EXPECT(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&bounds_fence.p))));
+            UINT64 submission = 0;
+            const auto draw_bounded = [&](float low, float high, UINT& yellow, UINT& blue_count) {
                 EXPECT(SUCCEEDED(allocator->Reset()) && SUCCEEDED(list->Reset(allocator.p, bounds_pso.p)));
                 D3D12_RESOURCE_BARRIER to_rt = transition(target.p, D3D12_RESOURCE_STATE_COPY_SOURCE,
                                                            D3D12_RESOURCE_STATE_RENDER_TARGET);
                 list->ResourceBarrier(1, &to_rt);
                 list->ClearRenderTargetView(rtv, blue, 0, nullptr);
-                list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
+                list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 0.5f, 0, 0, nullptr);
                 list->SetGraphicsRootSignature(root.p);
                 list->RSSetViewports(1, &viewport);
                 list->RSSetScissorRects(1, &scissor);
@@ -548,20 +553,25 @@
                 EXPECT(list->Close() == S_OK);
                 queue->ExecuteCommandLists(1, lists);
                 EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
-                EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr)));
+                ++submission;
+                EXPECT(SUCCEEDED(queue->Signal(bounds_fence.p, submission)) &&
+                       SUCCEEDED(bounds_fence->SetEventOnCompletion(submission, nullptr)));
                 void* pixels_out = nullptr;
                 EXPECT(SUCCEEDED(readback->Map(0, nullptr, &pixels_out)));
-                std::memcpy(pixels.data(), pixels_out, pixels.size());
-                UINT yellow = 0;
-                for (UINT i = 0; i < pixels.size(); i += 4)
-                    yellow += pixels[i] > 200 && pixels[i + 1] > 200 && pixels[i + 2] < 60;
-                return int(yellow);
+                const auto* bytes = static_cast<const std::uint8_t*>(pixels_out);
+                for (UINT i = 0; i < pixels.size(); i += 4) {
+                    yellow += bytes[i] == 255 && bytes[i + 1] == 255 && bytes[i + 2] == 0 && bytes[i + 3] == 255;
+                    blue_count += bytes[i] == 0 && bytes[i + 1] == 0 && bytes[i + 2] == 255 && bytes[i + 3] == 255;
+                }
+                readback->Unmap(0, nullptr);
+                return 0;
             };
-            const int outside = draw_bounded(0.9f, 1.0f);
-            const int inside = draw_bounded(0.0f, 1.0f);
-            std::printf("depth bounds through a pending stream: %d px outside the bounds, %d px inside\n", outside,
-                        inside);
-            EXPECT(outside == 0 && inside > 0);
+            UINT outside = 0, inside = 0, blue_outside = 0, blue_inside = 0;
+            EXPECT(draw_bounded(0.9f, 1.0f, outside, blue_outside) == 0);
+            EXPECT(draw_bounded(0.0f, 1.0f, inside, blue_inside) == 0);
+            std::printf("depth bounds via slot 86: outside yellow=%u blue=%u; inside yellow=%u blue=%u\n",
+                        outside, blue_outside, inside, blue_inside);
+            EXPECT(outside == 0 && blue_outside == size * size && inside == size * size && blue_inside == 0);
         } else {
             // No depthBounds here: the layer refuses such a PSO with E_NOTIMPL. On a device that prepares PSOs on
             // demand the refusal comes at the first use instead, and only if the flag arrived: a stream flag dropped

@github-actions github-actions Bot added the needs rebase Conflicts with the base branch: rebase it on main label Nov 26, 2026

@lixonik lixonik 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.

Built the exact head and its immediate parent (#815) with MSVC on Windows 11. On an NVIDIA GeForce RTX 5070 (610.88), d3d12_mesh passed on the parent and failed on this head with 4096 yellow pixels outside the expected bounds. Two issues with the new fixture are inline. With a scratch-only correction to the clear value, readback buffer and fence, the full default-mode mesh test passed and reported 0 outside / 4096 inside. The on-demand run still stops at SUCCEEDED(console_hr) on both commits, before the new case. d3d12_bundle_forbidden passed at this head. d3d12_close_parity had the same three CopyTextureRegion expectations fail on both commits.

AI-assisted.

Comment thread runtime/test/d3d12_mesh_test.cpp Outdated
D3D12_RESOURCE_STATE_RENDER_TARGET);
list->ResourceBarrier(1, &to_rt);
list->ClearRenderTargetView(rtv, blue, 0, nullptr);
list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);

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.

[P2] Clear stored depth outside the tested bounds.

Depth bounds compares the value already in the depth attachment. The fixture clears it to 1.0, inside [0.9, 1.0], so the first draw can paint yellow with the flag working. Clearing to 0.0 produced the expected 0 outside and 4096 inside. See the Vulkan depth-bounds operation and D3D12 OMSetDepthBounds remarks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a6e←rebased head: depth now clears to 0.0 so the test decides, and the bounds draws write into their own readback buffer, leaving the mesh rows alone.

Comment thread runtime/test/d3d12_mesh_test.cpp Outdated
EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr)));
void* pixels_out = nullptr;
EXPECT(SUCCEEDED(readback->Map(0, nullptr, &pixels_out)));
std::memcpy(pixels.data(), pixels_out, pixels.size());

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.

[P2] Keep the depth-bounds readback out of the mesh image's pixels buffer.

Once the clear value is fixed, the second bounds draw overwrites pixels with all yellow. The final mesh comparison then fails at (0,0), where it expects red. Use a separate local buffer for these draws. Both calls also signal and wait for fence value 2, which the later test reuses. A dedicated fence with increasing values made the two readbacks independent and left the later mesh comparison intact.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a6e←rebased head: depth now clears to 0.0 so the test decides, and the bounds draws write into their own readback buffer, leaving the mesh rows alone.

…BOUNDS-ON-DEMAND-STREAM)

A stream realized on demand (PipelineState::set_pending_stream, resolved on first
use) goes through a second create_graphics call site that passes neither the
depth-bounds flag nor the back-face stencil masks, so a title that binds a
console PSO before it is realized silently loses DepthBoundsTestEnable - the
test is then always on and clips nothing.

Pass both there, as the eager call site does.

Test (d3d12_mesh_test): the stream PSO with DepthBoundsTestEnable is now drawn
through. Depth is cleared to 0.0, outside the first bounds, so the test decides
and not the clear; the full-screen triangle is drawn with 0.9..1.0 (nothing may
paint) and 0.0..1.0 (the whole viewport must). The draws use their own readback
buffer, so the mesh image rows compared later are untouched. Where the device has
no depthBounds (llvmpipe here) the creation is refused instead and success would
mean the flag was dropped.

Depends on: ubiquitarianism#815
@star41905453-coder
star41905453-coder marked this pull request as ready for review November 27, 2026 03:25
@github-actions github-actions Bot removed the needs rebase Conflicts with the base branch: rebase it on main label Nov 27, 2026

@lixonik lixonik 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.

Retested 9b3f211d3 locally merged onto main a3bafd05d02b on Windows 11 with MSVC 19.44 (Release), NVIDIA GeForce RTX 5070 (610.88); Vulkan validation was unavailable locally, so the GPU checks ran without it.

  • The d3d12_vulkan_device.cpp fix itself is verified red/green on the real on-demand path: in a scratch check with info.features = d.enabled, info.prepare_on_bind = true, and PSO creation through Xbox slot 86 (prepare_console_stream --> set_pending_stream --> resolve_type), main drew 4096 outside, 4096 inside while 9b3f211d3 drew 0 outside, 4096 inside.
  • However, in the committed d3d12_mesh_test (d3d12_mesh and d3d12_mesh_heavy_cache passed 2/2), the new depth-bounds draw block is currently skipped on RTX 5070, does not hit the on-demand resolve_type path even when enabled, and still reuses fence value 2 across both draws. Running d3d12_mesh_test with LEONIDA_PIPELINE_COMPILE=on-demand still fails earlier at line 382 (SUCCEEDED(console_hr) && console_pso.p), and there is no d3d12_mesh_on_demand CTest target in runtime/CMakeLists.txt. Details inline.

AI-assisted.

D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{};
EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support,
sizeof bounds_support)));
if (bounds_support.DepthBoundsTestSupported) {

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.

[P2] The new depth-bounds draw block is silently skipped even on depth-bounds-capable GPUs because device_info in d3d12_mesh_test.cpp (lines 149-167) does not set info.features = d.enabled.

  • Condition: running unmodified d3d12_mesh at 9b3f211d3 (merged onto a3bafd05d02b) on an RTX 5070 where vk_test_device.h enables depthBounds.
  • Actual result: state_.info.features.depthBounds stays VK_FALSE, CheckFeatureSupport(OPTIONS2) reports DepthBoundsTestSupported == FALSE, the earlier check at line 356 logs depth-bounds tests need depthBounds, which the device lacks, and lines 518-597 never run (no depth bounds through a pending stream: output).
  • Expected / how to verify: forward info.features = d.enabled; in device_info (matching d3d12_depth24_draw_test.cpp and d3d12_translated_draw_test.cpp) and verify that d3d12_mesh logs 0 px outside the bounds, 4096 px inside on a depthBounds GPU.

bounds_stream.add(Subobject<D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL_FORMAT, DXGI_FORMAT>{
.inner = DXGI_FORMAT_D32_FLOAT});
Ref<ID3D12PipelineState> bounds_pso;
EXPECT(SUCCEEDED(create(device.p, bounds_stream, bounds_pso)) && bounds_pso.p);

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.

[P2] Even with info.features = d.enabled, this case does not exercise PipelineState::resolve_type (the on-demand path changed in d3d12_vulkan_device.cpp).

  • Condition: added only info.features = d.enabled; in scratch, reverted the two-line fix in d3d12_vulkan_device.cpp back to main, and ran d3d12_mesh_test.
  • Actual result: the test still passed and printed depth bounds through a pending stream: 0 px outside the bounds, 4096 px inside because default d3d12_mesh has prepare_on_bind == false and create() calls PC ID3D12Device2::CreatePipelineState (the eager call site at d3d12_vulkan_device.cpp:17624), bypassing DeviceProxy::prepare_stream (which only wraps Xbox slots 86 and 55 when prepare_on_bind is true). Setting LEONIDA_PIPELINE_COMPILE=on-demand fails earlier at line 382 (console_hr).
  • Expected / how to verify: create a dedicated device with info.features = d.enabled and info.prepare_on_bind = true, create bounds_pso via Xbox slot 86 (slot(device.p, 86)), and draw through it. On RTX 5070 that control gave 4096 outside, 4096 inside on main and 0 outside, 4096 inside with this PR.

EXPECT(list->Close() == S_OK);
queue->ExecuteCommandLists(1, lists);
EXPECT(rt::d3d12::last_execute_result(queue.p) == leonida::ReplayResult::success);
EXPECT(SUCCEEDED(queue->Signal(fence.p, 2)) && SUCCEEDED(fence->SetEventOnCompletion(2, nullptr)));

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.

[P3] draw_bounded still signals and waits on fence value 2 for both draws (and line 785 reuses 2 again).

After draw_bounded(0.9f, 1.0f) completes, fence_.completed() is already 2. On the second call draw_bounded(0.0f, 1.0f), Fence::claim(2, true) returns false (no signal submitted, d3d12_com.cpp:2600) and SetEventOnCompletion(2, nullptr) returns immediately (d3d12_com.cpp:2298) without waiting for the second CopyTextureRegion, making the second bounds_readback read unsynchronized. Use a dedicated Ref<ID3D12Fence> inside this block with monotonically increasing values (1, then 2).

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