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 from upstream-pr/2234 into main
star41905453-coder commented 2026-11-26 09:31:01 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by star41905453-coder on 2026-11-26T09:31:01Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5507455639 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234). Originally posted by [star41905453-coder](https://github.com/star41905453-coder) on 2026-11-26T09:31:01Z. --- 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.
ecox982745 commented 2026-11-26 09:42:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ecox982745 on 2026-11-26T09:42:07Z.


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 6b7d0fd219 (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
<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7188354695 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#issuecomment-7188354695). Originally posted by [ecox982745](https://github.com/ecox982745) on 2026-11-26T09:42:07Z. --- 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](https://docs.vulkan.org/spec/latest/chapters/fragops.html#fragops-dbt) 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. <details> <summary>Test-only patch used on 6b7d0fd219 (depth-bounds-capable branch)</summary> ```diff --- 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 ``` </details>
lixonik commented 2026-11-26 11:05:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T11:05:31Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6073024788 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#pullrequestreview-6073024788). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T11:05:31Z. **Review:** COMMENTED --- 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.
lixonik commented 2026-11-26 11:05:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T11:05:31Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 537

@@ -484,6 +492,110 @@ int main(int argc, char** argv) {
             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.
+        D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{};
+        EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options,
+                                                     sizeof(bounds_options))));
+        if (bounds_options.DepthBoundsTestSupported) {
+            Ref<ID3D12GraphicsCommandList1> list1;
+            EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
+            D3D12_RESOURCE_DESC depth_desc = rt_desc;
+            depth_desc.Format = DXGI_FORMAT_D32_FLOAT;
+            depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
+            Ref<ID3D12Resource> depth_image;
+            EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
+                                                             D3D12_RESOURCE_STATE_COMMON, nullptr,
+                                                             IID_PPV_ARGS(&depth_image.p))));
+            const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1,
+                                                      D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
+            Ref<ID3D12DescriptorHeap> dsv_heap;
+            EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p))));
+            const D3D12_CPU_DESCRIPTOR_HANDLE dsv{
+                SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))};
+            device->CreateDepthStencilView(depth_image.p, nullptr, dsv);
+            Stream bounds_stream = vs_stream;
+            D3D12_DEPTH_STENCIL_DESC1 bounds{};
+            bounds.DepthEnable = TRUE;
+            bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
+            bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+            bounds.DepthBoundsTestEnable = TRUE;
+            bounds_stream.add(DepthStencil1Subobject{.inner = bounds});
+            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) {
+                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);

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

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4707452106 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4707452106). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T11:05:31Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 537 ~~~~diff @@ -484,6 +492,110 @@ int main(int argc, char** argv) { 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. + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options, + sizeof(bounds_options)))); + if (bounds_options.DepthBoundsTestSupported) { + Ref<ID3D12GraphicsCommandList1> list1; + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref<ID3D12Resource> depth_image; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&depth_image.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref<ID3D12DescriptorHeap> dsv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))}; + device->CreateDepthStencilView(depth_image.p, nullptr, dsv); + Stream bounds_stream = vs_stream; + D3D12_DEPTH_STENCIL_DESC1 bounds{}; + bounds.DepthEnable = TRUE; + bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounds.DepthBoundsTestEnable = TRUE; + bounds_stream.add(DepthStencil1Subobject{.inner = bounds}); + 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) { + 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); ~~~~ --- [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](https://docs.vulkan.org/spec/latest/chapters/fragops.html#fragops-depthbounds) and [D3D12 OMSetDepthBounds remarks](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist1-omsetdepthbounds#remarks).
lixonik commented 2026-11-26 11:05:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T11:05:31Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 554

@@ -484,6 +492,110 @@ int main(int argc, char** argv) {
             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.
+        D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{};
+        EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options,
+                                                     sizeof(bounds_options))));
+        if (bounds_options.DepthBoundsTestSupported) {
+            Ref<ID3D12GraphicsCommandList1> list1;
+            EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
+            D3D12_RESOURCE_DESC depth_desc = rt_desc;
+            depth_desc.Format = DXGI_FORMAT_D32_FLOAT;
+            depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
+            Ref<ID3D12Resource> depth_image;
+            EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
+                                                             D3D12_RESOURCE_STATE_COMMON, nullptr,
+                                                             IID_PPV_ARGS(&depth_image.p))));
+            const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1,
+                                                      D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
+            Ref<ID3D12DescriptorHeap> dsv_heap;
+            EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p))));
+            const D3D12_CPU_DESCRIPTOR_HANDLE dsv{
+                SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))};
+            device->CreateDepthStencilView(depth_image.p, nullptr, dsv);
+            Stream bounds_stream = vs_stream;
+            D3D12_DEPTH_STENCIL_DESC1 bounds{};
+            bounds.DepthEnable = TRUE;
+            bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
+            bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+            bounds.DepthBoundsTestEnable = TRUE;
+            bounds_stream.add(DepthStencil1Subobject{.inner = bounds});
+            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) {
+                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->SetGraphicsRootSignature(root.p);
+                list->RSSetViewports(1, &viewport);
+                list->RSSetScissorRects(1, &scissor);
+                list->OMSetRenderTargets(1, &rtv, FALSE, &dsv);
+                list1->OMSetDepthBounds(low, high);
+                list->SetGraphicsRootConstantBufferView(1, upload_va + 512);
+                list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+                list->DrawInstanced(3, 1, 0, 0);
+                list->ResourceBarrier(1, &to_copy);
+                list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
+                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)));
+                void* pixels_out = nullptr;
+                EXPECT(SUCCEEDED(readback->Map(0, nullptr, &pixels_out)));
+                std::memcpy(pixels.data(), pixels_out, pixels.size());

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

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4707452113 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4707452113). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T11:05:31Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 554 ~~~~diff @@ -484,6 +492,110 @@ int main(int argc, char** argv) { 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. + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options, + sizeof(bounds_options)))); + if (bounds_options.DepthBoundsTestSupported) { + Ref<ID3D12GraphicsCommandList1> list1; + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref<ID3D12Resource> depth_image; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&depth_image.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref<ID3D12DescriptorHeap> dsv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))}; + device->CreateDepthStencilView(depth_image.p, nullptr, dsv); + Stream bounds_stream = vs_stream; + D3D12_DEPTH_STENCIL_DESC1 bounds{}; + bounds.DepthEnable = TRUE; + bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounds.DepthBoundsTestEnable = TRUE; + bounds_stream.add(DepthStencil1Subobject{.inner = bounds}); + 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) { + 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->SetGraphicsRootSignature(root.p); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, &dsv); + list1->OMSetDepthBounds(low, high); + list->SetGraphicsRootConstantBufferView(1, upload_va + 512); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list->DrawInstanced(3, 1, 0, 0); + list->ResourceBarrier(1, &to_copy); + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + 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))); + void* pixels_out = nullptr; + EXPECT(SUCCEEDED(readback->Map(0, nullptr, &pixels_out))); + std::memcpy(pixels.data(), pixels_out, pixels.size()); ~~~~ --- [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.
star41905453-coder commented 2026-11-27 03:17:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by star41905453-coder on 2026-11-27T03:17:42Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 537

@@ -484,6 +492,110 @@ int main(int argc, char** argv) {
             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.
+        D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{};
+        EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options,
+                                                     sizeof(bounds_options))));
+        if (bounds_options.DepthBoundsTestSupported) {
+            Ref<ID3D12GraphicsCommandList1> list1;
+            EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
+            D3D12_RESOURCE_DESC depth_desc = rt_desc;
+            depth_desc.Format = DXGI_FORMAT_D32_FLOAT;
+            depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
+            Ref<ID3D12Resource> depth_image;
+            EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
+                                                             D3D12_RESOURCE_STATE_COMMON, nullptr,
+                                                             IID_PPV_ARGS(&depth_image.p))));
+            const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1,
+                                                      D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
+            Ref<ID3D12DescriptorHeap> dsv_heap;
+            EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p))));
+            const D3D12_CPU_DESCRIPTOR_HANDLE dsv{
+                SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))};
+            device->CreateDepthStencilView(depth_image.p, nullptr, dsv);
+            Stream bounds_stream = vs_stream;
+            D3D12_DEPTH_STENCIL_DESC1 bounds{};
+            bounds.DepthEnable = TRUE;
+            bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
+            bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+            bounds.DepthBoundsTestEnable = TRUE;
+            bounds_stream.add(DepthStencil1Subobject{.inner = bounds});
+            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) {
+                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);

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4715342488 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4715342488). Originally posted by [star41905453-coder](https://github.com/star41905453-coder) on 2026-11-27T03:17:42Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 537 ~~~~diff @@ -484,6 +492,110 @@ int main(int argc, char** argv) { 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. + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options, + sizeof(bounds_options)))); + if (bounds_options.DepthBoundsTestSupported) { + Ref<ID3D12GraphicsCommandList1> list1; + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref<ID3D12Resource> depth_image; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&depth_image.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref<ID3D12DescriptorHeap> dsv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))}; + device->CreateDepthStencilView(depth_image.p, nullptr, dsv); + Stream bounds_stream = vs_stream; + D3D12_DEPTH_STENCIL_DESC1 bounds{}; + bounds.DepthEnable = TRUE; + bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounds.DepthBoundsTestEnable = TRUE; + bounds_stream.add(DepthStencil1Subobject{.inner = bounds}); + 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) { + 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); ~~~~ --- 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.
star41905453-coder commented 2026-11-27 03:17:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by star41905453-coder on 2026-11-27T03:17:44Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 554

@@ -484,6 +492,110 @@ int main(int argc, char** argv) {
             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.
+        D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{};
+        EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options,
+                                                     sizeof(bounds_options))));
+        if (bounds_options.DepthBoundsTestSupported) {
+            Ref<ID3D12GraphicsCommandList1> list1;
+            EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
+            D3D12_RESOURCE_DESC depth_desc = rt_desc;
+            depth_desc.Format = DXGI_FORMAT_D32_FLOAT;
+            depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
+            Ref<ID3D12Resource> depth_image;
+            EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
+                                                             D3D12_RESOURCE_STATE_COMMON, nullptr,
+                                                             IID_PPV_ARGS(&depth_image.p))));
+            const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1,
+                                                      D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
+            Ref<ID3D12DescriptorHeap> dsv_heap;
+            EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p))));
+            const D3D12_CPU_DESCRIPTOR_HANDLE dsv{
+                SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))};
+            device->CreateDepthStencilView(depth_image.p, nullptr, dsv);
+            Stream bounds_stream = vs_stream;
+            D3D12_DEPTH_STENCIL_DESC1 bounds{};
+            bounds.DepthEnable = TRUE;
+            bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
+            bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+            bounds.DepthBoundsTestEnable = TRUE;
+            bounds_stream.add(DepthStencil1Subobject{.inner = bounds});
+            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) {
+                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->SetGraphicsRootSignature(root.p);
+                list->RSSetViewports(1, &viewport);
+                list->RSSetScissorRects(1, &scissor);
+                list->OMSetRenderTargets(1, &rtv, FALSE, &dsv);
+                list1->OMSetDepthBounds(low, high);
+                list->SetGraphicsRootConstantBufferView(1, upload_va + 512);
+                list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+                list->DrawInstanced(3, 1, 0, 0);
+                list->ResourceBarrier(1, &to_copy);
+                list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr);
+                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)));
+                void* pixels_out = nullptr;
+                EXPECT(SUCCEEDED(readback->Map(0, nullptr, &pixels_out)));
+                std::memcpy(pixels.data(), pixels_out, pixels.size());

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4715342588 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4715342588). Originally posted by [star41905453-coder](https://github.com/star41905453-coder) on 2026-11-27T03:17:44Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 554 ~~~~diff @@ -484,6 +492,110 @@ int main(int argc, char** argv) { 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. + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_options{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_options, + sizeof(bounds_options)))); + if (bounds_options.DepthBoundsTestSupported) { + Ref<ID3D12GraphicsCommandList1> list1; + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref<ID3D12Resource> depth_image; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&depth_image.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref<ID3D12DescriptorHeap> dsv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))}; + device->CreateDepthStencilView(depth_image.p, nullptr, dsv); + Stream bounds_stream = vs_stream; + D3D12_DEPTH_STENCIL_DESC1 bounds{}; + bounds.DepthEnable = TRUE; + bounds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + bounds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounds.DepthBoundsTestEnable = TRUE; + bounds_stream.add(DepthStencil1Subobject{.inner = bounds}); + 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) { + 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->SetGraphicsRootSignature(root.p); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, &dsv); + list1->OMSetDepthBounds(low, high); + list->SetGraphicsRootConstantBufferView(1, upload_va + 512); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list->DrawInstanced(3, 1, 0, 0); + list->ResourceBarrier(1, &to_copy); + list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); + 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))); + void* pixels_out = nullptr; + EXPECT(SUCCEEDED(readback->Map(0, nullptr, &pixels_out))); + std::memcpy(pixels.data(), pixels_out, pixels.size()); ~~~~ --- 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.
lixonik commented 2026-11-27 12:14:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T12:14:05Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 518

@@ -506,6 +506,97 @@ int main(int argc, char** argv) {
         EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped)));
         std::memcpy(pixels.data(), mapped, pixels.size());
 
+        // Depth bounds through the stream, drawn: the PSO is created pending and realized on first use, so a flag
+        // dropped on the way into create_graphics would let the fragment through. The depth attachment is cleared to
+        // 0.0, outside the first bounds, so the bounds test is what decides, not the clear. The VS writes z = 0, so
+        // 0.9..1.0 must leave the target blue and 0.0..1.0 must paint it yellow. Its own readback buffer, so the mesh
+        // image rows above stay as they are.
+        {
+            D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{};
+            EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support,
+                                                         sizeof bounds_support)));
+            if (bounds_support.DepthBoundsTestSupported) {

[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.
<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4719233128 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4719233128). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-27T12:14:05Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 518 ~~~~diff @@ -506,6 +506,97 @@ int main(int argc, char** argv) { EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); std::memcpy(pixels.data(), mapped, pixels.size()); + // Depth bounds through the stream, drawn: the PSO is created pending and realized on first use, so a flag + // dropped on the way into create_graphics would let the fragment through. The depth attachment is cleared to + // 0.0, outside the first bounds, so the bounds test is what decides, not the clear. The VS writes z = 0, so + // 0.9..1.0 must leave the target blue and 0.0..1.0 must paint it yellow. Its own readback buffer, so the mesh + // image rows above stay as they are. + { + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support, + sizeof bounds_support))); + if (bounds_support.DepthBoundsTestSupported) { ~~~~ --- [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.
lixonik commented 2026-11-27 12:14:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T12:14:05Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 556

@@ -506,6 +506,97 @@ int main(int argc, char** argv) {
         EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped)));
         std::memcpy(pixels.data(), mapped, pixels.size());
 
+        // Depth bounds through the stream, drawn: the PSO is created pending and realized on first use, so a flag
+        // dropped on the way into create_graphics would let the fragment through. The depth attachment is cleared to
+        // 0.0, outside the first bounds, so the bounds test is what decides, not the clear. The VS writes z = 0, so
+        // 0.9..1.0 must leave the target blue and 0.0..1.0 must paint it yellow. Its own readback buffer, so the mesh
+        // image rows above stay as they are.
+        {
+            D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{};
+            EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support,
+                                                         sizeof bounds_support)));
+            if (bounds_support.DepthBoundsTestSupported) {
+                Ref<ID3D12GraphicsCommandList1> list1;
+                EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
+                D3D12_RESOURCE_DESC depth_desc = rt_desc;
+                depth_desc.Format = DXGI_FORMAT_D32_FLOAT;
+                depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
+                Ref<ID3D12Resource> depth_image;
+                EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
+                                                                 D3D12_RESOURCE_STATE_COMMON, nullptr,
+                                                                 IID_PPV_ARGS(&depth_image.p))));
+                const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1,
+                                                          D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
+                Ref<ID3D12DescriptorHeap> dsv_heap;
+                EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p))));
+                const D3D12_CPU_DESCRIPTOR_HANDLE dsv{
+                    SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))};
+                device->CreateDepthStencilView(depth_image.p, nullptr, dsv);
+
+                Ref<ID3D12Resource> bounds_readback;
+                const auto bounds_desc = buffer_desc(size * 256);
+                EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &bounds_desc,
+                                                                 D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
+                                                                 IID_PPV_ARGS(&bounds_readback.p))));
+                D3D12_TEXTURE_COPY_LOCATION bounds_dst{};
+                bounds_dst.pResource = bounds_readback.p;
+                bounds_dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
+                bounds_dst.PlacedFootprint = dst.PlacedFootprint;
+
+                Stream bounds_stream = vs_stream;
+                D3D12_DEPTH_STENCIL_DESC1 bounds_state{};
+                bounds_state.DepthEnable = TRUE;
+                bounds_state.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
+                bounds_state.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+                bounds_state.DepthBoundsTestEnable = TRUE;
+                bounds_stream.add(DepthStencil1Subobject{.inner = bounds_state});
+                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);

[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.
<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4719233133 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4719233133). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-27T12:14:05Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 556 ~~~~diff @@ -506,6 +506,97 @@ int main(int argc, char** argv) { EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); std::memcpy(pixels.data(), mapped, pixels.size()); + // Depth bounds through the stream, drawn: the PSO is created pending and realized on first use, so a flag + // dropped on the way into create_graphics would let the fragment through. The depth attachment is cleared to + // 0.0, outside the first bounds, so the bounds test is what decides, not the clear. The VS writes z = 0, so + // 0.9..1.0 must leave the target blue and 0.0..1.0 must paint it yellow. Its own readback buffer, so the mesh + // image rows above stay as they are. + { + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support, + sizeof bounds_support))); + if (bounds_support.DepthBoundsTestSupported) { + Ref<ID3D12GraphicsCommandList1> list1; + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref<ID3D12Resource> depth_image; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&depth_image.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref<ID3D12DescriptorHeap> dsv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))}; + device->CreateDepthStencilView(depth_image.p, nullptr, dsv); + + Ref<ID3D12Resource> bounds_readback; + const auto bounds_desc = buffer_desc(size * 256); + EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &bounds_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&bounds_readback.p)))); + D3D12_TEXTURE_COPY_LOCATION bounds_dst{}; + bounds_dst.pResource = bounds_readback.p; + bounds_dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + bounds_dst.PlacedFootprint = dst.PlacedFootprint; + + Stream bounds_stream = vs_stream; + D3D12_DEPTH_STENCIL_DESC1 bounds_state{}; + bounds_state.DepthEnable = TRUE; + bounds_state.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + bounds_state.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounds_state.DepthBoundsTestEnable = TRUE; + bounds_stream.add(DepthStencil1Subobject{.inner = bounds_state}); + 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); ~~~~ --- [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.
lixonik commented 2026-11-27 12:14:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T12:14:05Z.

Code review location: runtime/test/d3d12_mesh_test.cpp; line 580

@@ -506,6 +506,97 @@ int main(int argc, char** argv) {
         EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped)));
         std::memcpy(pixels.data(), mapped, pixels.size());
 
+        // Depth bounds through the stream, drawn: the PSO is created pending and realized on first use, so a flag
+        // dropped on the way into create_graphics would let the fragment through. The depth attachment is cleared to
+        // 0.0, outside the first bounds, so the bounds test is what decides, not the clear. The VS writes z = 0, so
+        // 0.9..1.0 must leave the target blue and 0.0..1.0 must paint it yellow. Its own readback buffer, so the mesh
+        // image rows above stay as they are.
+        {
+            D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{};
+            EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support,
+                                                         sizeof bounds_support)));
+            if (bounds_support.DepthBoundsTestSupported) {
+                Ref<ID3D12GraphicsCommandList1> list1;
+                EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p))));
+                D3D12_RESOURCE_DESC depth_desc = rt_desc;
+                depth_desc.Format = DXGI_FORMAT_D32_FLOAT;
+                depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
+                Ref<ID3D12Resource> depth_image;
+                EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc,
+                                                                 D3D12_RESOURCE_STATE_COMMON, nullptr,
+                                                                 IID_PPV_ARGS(&depth_image.p))));
+                const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1,
+                                                          D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0};
+                Ref<ID3D12DescriptorHeap> dsv_heap;
+                EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p))));
+                const D3D12_CPU_DESCRIPTOR_HANDLE dsv{
+                    SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))};
+                device->CreateDepthStencilView(depth_image.p, nullptr, dsv);
+
+                Ref<ID3D12Resource> bounds_readback;
+                const auto bounds_desc = buffer_desc(size * 256);
+                EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &bounds_desc,
+                                                                 D3D12_RESOURCE_STATE_COPY_DEST, nullptr,
+                                                                 IID_PPV_ARGS(&bounds_readback.p))));
+                D3D12_TEXTURE_COPY_LOCATION bounds_dst{};
+                bounds_dst.pResource = bounds_readback.p;
+                bounds_dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
+                bounds_dst.PlacedFootprint = dst.PlacedFootprint;
+
+                Stream bounds_stream = vs_stream;
+                D3D12_DEPTH_STENCIL_DESC1 bounds_state{};
+                bounds_state.DepthEnable = TRUE;
+                bounds_state.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
+                bounds_state.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS;
+                bounds_state.DepthBoundsTestEnable = TRUE;
+                bounds_stream.add(DepthStencil1Subobject{.inner = bounds_state});
+                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) {
+                    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, 0.0f, 0, 0, nullptr);
+                    list->SetGraphicsRootSignature(root.p);
+                    list->RSSetViewports(1, &viewport);
+                    list->RSSetScissorRects(1, &scissor);
+                    list->OMSetRenderTargets(1, &rtv, FALSE, &dsv);
+                    list1->OMSetDepthBounds(low, high);
+                    list->SetGraphicsRootConstantBufferView(1, upload_va + 512);
+                    list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
+                    list->DrawInstanced(3, 1, 0, 0);
+                    D3D12_RESOURCE_BARRIER to_copy_again =
+                        transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE);
+                    list->ResourceBarrier(1, &to_copy_again);
+                    list->CopyTextureRegion(&bounds_dst, 0, 0, 0, &src, nullptr);
+                    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)));

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

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4719233141 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#discussion_r4719233141). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-27T12:14:05Z. **Code review location:** `runtime/test/d3d12_mesh_test.cpp`; line 580 ~~~~diff @@ -506,6 +506,97 @@ int main(int argc, char** argv) { EXPECT(SUCCEEDED(readback->Map(0, nullptr, &mapped))); std::memcpy(pixels.data(), mapped, pixels.size()); + // Depth bounds through the stream, drawn: the PSO is created pending and realized on first use, so a flag + // dropped on the way into create_graphics would let the fragment through. The depth attachment is cleared to + // 0.0, outside the first bounds, so the bounds test is what decides, not the clear. The VS writes z = 0, so + // 0.9..1.0 must leave the target blue and 0.0..1.0 must paint it yellow. Its own readback buffer, so the mesh + // image rows above stay as they are. + { + D3D12_FEATURE_DATA_D3D12_OPTIONS2 bounds_support{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &bounds_support, + sizeof bounds_support))); + if (bounds_support.DepthBoundsTestSupported) { + Ref<ID3D12GraphicsCommandList1> list1; + EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref<ID3D12Resource> depth_image; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&depth_image.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref<ID3D12DescriptorHeap> dsv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_desc, IID_PPV_ARGS(&dsv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast<XboxHeapHandleFn>(slot(dsv_heap.p, 9))(dsv_heap.p))}; + device->CreateDepthStencilView(depth_image.p, nullptr, dsv); + + Ref<ID3D12Resource> bounds_readback; + const auto bounds_desc = buffer_desc(size * 256); + EXPECT(SUCCEEDED(device->CreateCommittedResource(&readback_heap, D3D12_HEAP_FLAG_NONE, &bounds_desc, + D3D12_RESOURCE_STATE_COPY_DEST, nullptr, + IID_PPV_ARGS(&bounds_readback.p)))); + D3D12_TEXTURE_COPY_LOCATION bounds_dst{}; + bounds_dst.pResource = bounds_readback.p; + bounds_dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + bounds_dst.PlacedFootprint = dst.PlacedFootprint; + + Stream bounds_stream = vs_stream; + D3D12_DEPTH_STENCIL_DESC1 bounds_state{}; + bounds_state.DepthEnable = TRUE; + bounds_state.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL; + bounds_state.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounds_state.DepthBoundsTestEnable = TRUE; + bounds_stream.add(DepthStencil1Subobject{.inner = bounds_state}); + 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) { + 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, 0.0f, 0, 0, nullptr); + list->SetGraphicsRootSignature(root.p); + list->RSSetViewports(1, &viewport); + list->RSSetScissorRects(1, &scissor); + list->OMSetRenderTargets(1, &rtv, FALSE, &dsv); + list1->OMSetDepthBounds(low, high); + list->SetGraphicsRootConstantBufferView(1, upload_va + 512); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + list->DrawInstanced(3, 1, 0, 0); + D3D12_RESOURCE_BARRIER to_copy_again = + transition(target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_copy_again); + list->CopyTextureRegion(&bounds_dst, 0, 0, 0, &src, nullptr); + 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))); ~~~~ --- [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`).
lixonik commented 2026-11-27 12:14:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-27T12:14:05Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6087953924 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2234#pullrequestreview-6087953924). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-27T12:14:05Z. **Review:** COMMENTED --- 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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2234:upstream-pr/2234'
git switch 'upstream-pr/2234'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2234'
git switch 'upstream-pr/2234'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2234'
git switch 'upstream-pr/2234'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2234'
git switch 'main'
git merge --squash 'upstream-pr/2234'
git switch 'main'
git merge --ff-only 'upstream-pr/2234'
git switch 'main'
git merge 'upstream-pr/2234'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2234
No description provided.