From b75e4518961a228e4e2ad9dbcbbb35cf7f86c9e1 Mon Sep 17 00:00:00 2001 From: Juju Date: Tue, 24 Nov 2026 00:53:26 -0500 Subject: [PATCH] d3d12/vulkan: implement hardware depth-bounds testing (DEPTH_STENCIL1/2 + OMSetDepthBounds) --- runtime/CMakeLists.txt | 9 + runtime/shims/d3d12_com.cpp | 18 + runtime/shims/d3d12_com.h | 2 +- runtime/shims/d3d12_vulkan_device.cpp | 61 +-- runtime/src/command_list.h | 2 + runtime/src/vulkan_backend.h | 7 +- runtime/src/vulkan_replay.h | 12 + runtime/test/d3d12_depth_bounds_test.cpp | 503 ++++++++++++++++++++++ runtime/test/d3d12_mesh_test.cpp | 8 +- runtime/test/d3d12_vulkan_device_test.cpp | 17 +- runtime/test/replay_cpu_bench.cpp | 1 + runtime/test/vk_test_device.h | 1 + 12 files changed, 605 insertions(+), 36 deletions(-) create mode 100644 runtime/test/d3d12_depth_bounds_test.cpp diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 64e346f40a..0cbd5dcb31 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2288,6 +2288,15 @@ if(TARGET Vulkan::Headers) target_compile_definitions(d3d12_depth_resolve_test PRIVATE LEONIDA_DEPTH_EDGE_VS_SPV="${binary}") endif() + # Hardware depth-bounds testing (DEPTH_STENCIL1/2 DepthBoundsTestEnable + OMSetDepthBounds) against host D3D12. + add_executable(d3d12_depth_bounds_test test/d3d12_depth_bounds_test.cpp) + target_link_libraries(d3d12_depth_bounds_test PRIVATE gta6rt_d3d12_com d3d12 dxgi dxguid) + target_compile_definitions(d3d12_depth_bounds_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(d3d12_depth_bounds_test PRIVATE /W4 /permissive- /utf-8) + add_test(NAME d3d12_depth_bounds COMMAND d3d12_depth_bounds_test) + set_tests_properties(d3d12_depth_bounds PROPERTIES SKIP_RETURN_CODE 77 + FAIL_REGULAR_EXPRESSION "Validation Error;VUID-") + # ID3D12Resource::GetDesc against the host runtime and WARP; run explicitly on a Vulkan GPU. add_executable(d3d12_resource_desc_test test/d3d12_resource_desc_test.cpp) target_link_libraries(d3d12_resource_desc_test PRIVATE gta6rt_d3d12_com d3d12 dxgi) diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 4f4904e965..2daa32865a 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -461,6 +461,16 @@ class GraphicsCommandList final front_stencil_ref_ = back_stencil_ref_ = reference; } + // ID3D12GraphicsCommandList1: dynamic depth-bounds range for PSOs created with DepthBoundsTestEnable = TRUE. + // Not allowed in a bundle (D3D12 "Recording command lists and bundles"). + void STDMETHODCALLTYPE OMSetDepthBounds(FLOAT min_depth, FLOAT max_depth) override { + d3d12_trace::record("ID3D12GraphicsCommandList1::OMSetDepthBounds"); + if (type_ == D3D12_COMMAND_LIST_TYPE_BUNDLE) return check(false); + if (list_.closed()) return; + depth_bounds_min_ = min_depth; + depth_bounds_max_ = max_depth; + } + void STDMETHODCALLTYPE DrawInstanced(UINT vertices, UINT instances, UINT first_vertex, UINT first_instance) override { d3d12_trace::record("ID3D12GraphicsCommandList::DrawInstanced"); if (defer([=](GraphicsCommandList& l) { l.record_draw(false, vertices, instances, first_vertex, 0, first_instance); }, @@ -490,6 +500,8 @@ class GraphicsCommandList final leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, {}, 0, viewport_, scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; draw.mesh = true; + draw.depth_bounds_min = depth_bounds_min_; + draw.depth_bounds_max = depth_bounds_max_; check(list_.draw(std::move(draw))); } @@ -1274,6 +1286,8 @@ class GraphicsCommandList final bool draw_views_stale_ = true; leonida::TargetView depth_{}; std::array blend_factor_{1, 1, 1, 1}; + float depth_bounds_min_ = 0.0f; + float depth_bounds_max_ = 1.0f; // The root signatures and pipeline states this recording uses. D3D12 lets an application release them right // after Set*RootSignature / SetPipelineState; the list keeps them until Reset or its own release, so the root @@ -1373,6 +1387,8 @@ class GraphicsCommandList final draw_views_stale_ = true; depth_ = {}; blend_factor_ = {1, 1, 1, 1}; + depth_bounds_min_ = 0.0f; + depth_bounds_max_ = 1.0f; set_pipeline(state); } @@ -1418,6 +1434,8 @@ class GraphicsCommandList final leonida::Draw draw{graphics_pipeline_, graphics_.data, draw_views(), depth_, index_, topology_, viewport_, scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, first, base_vertex, first_instance}; + draw.depth_bounds_min = depth_bounds_min_; + draw.depth_bounds_max = depth_bounds_max_; check(list_.draw(std::move(draw))); } }; diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index d0f3b528aa..10dd425fcc 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -176,7 +176,7 @@ struct VulkanDeviceInfo { // D3D12_MESH_SHADER_TIER_1; without it mesh PSOs are refused and the tier is NOT_SUPPORTED. bool mesh_shader = false; // The core features enabled on `device`. Graphics PSOs that need independentBlend, fillModeNonSolid, - // depthBiasClamp or dualSrcBlend are refused unless it is set here. + // depthBiasClamp, dualSrcBlend, logicOp, depthClamp or depthBounds are refused unless it is set here. VkPhysicalDeviceFeatures features{}; // Vulkan 1.2 shaderOutputLayer / shaderOutputViewportIndex enabled on `device`: a translated vertex shader that // writes the layer or viewport declares ShaderLayer / ShaderViewportIndex (the index's `features`). diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 0273f9339b..d1fab13c58 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -3111,6 +3111,7 @@ struct PipelineStream { D3D12_SHADER_BYTECODE as{}; bool has_cs = false; bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS + bool depth_bounds_test = false; }; HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, PipelineStream& out) { @@ -3220,10 +3221,7 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe case D3D12_PIPELINE_STATE_SUBOBJECT_TYPE_DEPTH_STENCIL1: { D3D12_DEPTH_STENCIL_DESC1 d{}; take(d); - if (ok && d.DepthBoundsTestEnable) { - log_once("depth-bounds tests are not supported yet; stream subobject", int(type)); - return E_NOTIMPL; - } + if (ok) out.depth_bounds_test = d.DepthBoundsTestEnable != FALSE; g.DepthStencilState = {d.DepthEnable, d.DepthWriteMask, d.DepthFunc, d.StencilEnable, d.StencilReadMask, d.StencilWriteMask, d.FrontFace, d.BackFace}; break; @@ -3232,10 +3230,7 @@ HRESULT parse_pipeline_stream(const D3D12_PIPELINE_STATE_STREAM_DESC& desc, Pipe D3D12_DEPTH_STENCIL_DESC2 d{}; take(d); if (!ok) break; - if (d.DepthBoundsTestEnable) { - log_once("depth-bounds tests are not supported yet; stream subobject", int(type)); - return E_NOTIMPL; - } + out.depth_bounds_test = d.DepthBoundsTestEnable != FALSE; // Separate front and back stencil masks are not modelled (one read and one write mask per PSO). if (d.StencilEnable && (d.FrontFace.StencilReadMask != d.BackFace.StencilReadMask || d.FrontFace.StencilWriteMask != d.BackFace.StencilWriteMask)) { @@ -3389,7 +3384,7 @@ class PipelineState final // no VS, no vertex input or input assembly (the input layout, topology type and strip cut are ignored, as D3D12 // does for mesh PSOs), drawn by DispatchMesh. An AS (amplification shader) is refused: the game has none. HRESULT create_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, const D3D12_SHADER_BYTECODE* ms = nullptr, - const D3D12_SHADER_BYTECODE* as = nullptr) { + const D3D12_SHADER_BYTECODE* as = nullptr, bool depth_bounds_test = false) { auto& vk = device_.context->vk; const VkDevice dev = device_.info.device; const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength; @@ -3776,6 +3771,9 @@ class PipelineState final depth.depthTestEnable = ds.DepthEnable; depth.depthWriteEnable = ds.DepthEnable && ds.DepthWriteMask == D3D12_DEPTH_WRITE_MASK_ALL; depth.depthCompareOp = compare_op(ds.DepthFunc); + depth.depthBoundsTestEnable = depth_bounds_test ? VK_TRUE : VK_FALSE; + depth.minDepthBounds = 0.0f; + depth.maxDepthBounds = 1.0f; depth.stencilTestEnable = ds.StencilEnable; depth.front = stencil_face(ds.FrontFace, ds.StencilReadMask, ds.StencilWriteMask); depth.back = stencil_face(ds.BackFace, ds.StencilReadMask, ds.StencilWriteMask); @@ -3849,6 +3847,9 @@ class PipelineState final return refuse("logic ops need logicOp, which the device lacks; op", int(first.LogicOp)); if (raster.depthClampEnable && !features.depthClamp) return refuse("DepthClipEnable = FALSE needs depthClamp, which the device lacks; flag", 0); + if (depth.depthBoundsTestEnable && !features.depthBounds) + return refuse("DepthBoundsTestEnable = TRUE needs depthBounds, which the device lacks; flag", 1); + graphics_.depth_bounds = depth_bounds_test; graphics_.color_formats.assign(desc.NumRenderTargets, VK_FORMAT_UNDEFINED); for (UINT i = 0; i < desc.NumRenderTargets; ++i) { @@ -4103,14 +4104,6 @@ class PipelineState final // The graphics pipeline with the formats of the `unbound` slots (leonida::GraphicsPipeline::variant) undefined. VkPipeline build(std::uint32_t unbound) const { - // The stride state is last: a pipeline without vertex bindings (a full-screen pass that builds its vertices - // from SV_VertexID) leaves it out, since no vkCmdBindVertexBuffers2 would ever set it - // (VUID-vkCmdDraw-pStrides-04913). - static constexpr VkDynamicState dynamic_states[] = { - VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, - VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY, VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE, - VK_DYNAMIC_STATE_STENCIL_REFERENCE, VK_DYNAMIC_STATE_BLEND_CONSTANTS, - VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE}; const Recipe& r = recipe_; std::vector colors = graphics_.color_formats; for (std::size_t slot = 0; slot < colors.size(); ++slot) @@ -4155,17 +4148,23 @@ class PipelineState final blend.pAttachments = r.blends.data(); // A mesh pipeline takes neither the topology nor the strides as dynamic state // (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065 and -07066). - static constexpr VkDynamicState mesh_dynamic_states[] = {VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR, - VK_DYNAMIC_STATE_STENCIL_REFERENCE, - VK_DYNAMIC_STATE_BLEND_CONSTANTS}; + // A pipeline without vertex bindings leaves out VERTEX_INPUT_BINDING_STRIDE (VUID-vkCmdDraw-pStrides-04913). + // DEPTH_BOUNDS is added only when depthBoundsTestEnable is set so non-bounds pipelines need not set it. + VkDynamicState dyn_list[8]{}; + std::uint32_t dyn_count = 0; + dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VIEWPORT; + dyn_list[dyn_count++] = VK_DYNAMIC_STATE_SCISSOR; + if (!r.mesh) { + dyn_list[dyn_count++] = VK_DYNAMIC_STATE_PRIMITIVE_TOPOLOGY; + dyn_list[dyn_count++] = VK_DYNAMIC_STATE_PRIMITIVE_RESTART_ENABLE; + } + dyn_list[dyn_count++] = VK_DYNAMIC_STATE_STENCIL_REFERENCE; + dyn_list[dyn_count++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS; + if (!r.mesh && !r.bindings.empty()) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE; + if (r.depth.depthBoundsTestEnable) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS; VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO}; - if (r.mesh) { - dynamic.dynamicStateCount = std::uint32_t(std::size(mesh_dynamic_states)); - dynamic.pDynamicStates = mesh_dynamic_states; - } else { - dynamic.dynamicStateCount = std::uint32_t(std::size(dynamic_states)) - (r.bindings.empty() ? 1 : 0); - dynamic.pDynamicStates = dynamic_states; - } + dynamic.dynamicStateCount = dyn_count; + dynamic.pDynamicStates = dyn_list; VkGraphicsPipelineCreateInfo info{VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO, &rendering}; info.stageCount = std::uint32_t(r.stages.size()); @@ -6183,8 +6182,9 @@ class Device final if (const HRESULT hr = parse_pipeline_stream(*desc, stream); FAILED(hr)) return d3d12_trace::result(method, hr); auto* pipeline = new PipelineState(state_); pipeline->attach_device(identity()); - const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute) - : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as); + const HRESULT hr = + stream.has_cs ? pipeline->create_compute(stream.compute) + : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, stream.depth_bounds_test); if (FAILED(hr)) { pipeline->Release(); return d3d12_trace::result(method, hr); @@ -7645,7 +7645,7 @@ class Device final if (size != sizeof(D3D12_FEATURE_DATA_D3D12_OPTIONS2)) return d3d12_trace::result(method, E_INVALIDARG); auto* opts = static_cast(data); *opts = {}; - opts->DepthBoundsTestSupported = FALSE; + opts->DepthBoundsTestSupported = state_.info.features.depthBounds ? TRUE : FALSE; opts->ProgrammableSamplePositionsTier = D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED; return d3d12_trace::result(method, S_OK); } @@ -9796,6 +9796,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin enable.dualSrcBlend = chosen_features.dualSrcBlend; enable.logicOp = chosen_features.logicOp; enable.depthClamp = chosen_features.depthClamp; + enable.depthBounds = chosen_features.depthBounds; enable.geometryShader = chosen_features.geometryShader; // translated geometry shaders (195 in the corpus) // What translated shaders declare (the index's `features`): 64-bit integers in most of the corpus (25,803 of // 26,503 entries), doubles, 16-bit integers, clip and cull distances in a few vertex shaders, image stores diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index 7c5c09e0ac..7cdff4b451 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h @@ -440,6 +440,8 @@ struct Draw { std::int32_t base_vertex = 0; std::uint32_t first_instance = 0; bool mesh = false; + float depth_bounds_min = 0.0f; + float depth_bounds_max = 1.0f; }; // D3D12_TEXTURE_DATA_PITCH_ALIGNMENT and D3D12_TEXTURE_DATA_PLACEMENT_ALIGNMENT. diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index e3e4defe89..1abcf6f0f7 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -102,7 +102,7 @@ struct VulkanDeviceFunctions { X(vkCmdPushConstants) X(vkCmdDispatch) \ X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ - X(vkCmdSetBlendConstants) X(vkCmdDraw) \ + X(vkCmdSetBlendConstants) X(vkCmdSetDepthBounds) X(vkCmdDraw) \ X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ X(vkCreatePipelineCache) X(vkDestroyPipelineCache) \ X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ @@ -133,8 +133,8 @@ struct VulkanDeviceFunctions { vkCreateShaderModule && vkDestroyShaderModule && vkCreatePipelineLayout && vkDestroyPipelineLayout && vkCreateComputePipelines && vkDestroyPipeline && vkGetBufferDeviceAddress && vkCmdBindIndexBuffer && vkCmdSetViewport && vkCmdSetScissor && - vkCmdSetStencilReference && vkCmdSetBlendConstants && vkCmdDraw && vkCmdDrawIndexed && - vkCreateGraphicsPipelines; + vkCmdSetStencilReference && vkCmdSetBlendConstants && vkCmdSetDepthBounds && vkCmdDraw && + vkCmdDrawIndexed && vkCreateGraphicsPipelines; } // Graphics pipelines also need these Vulkan 1.3 entry points (dynamic rendering, extended dynamic state). @@ -974,6 +974,7 @@ class VulkanContext { replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; replay_.set_stencil_reference = vk.vkCmdSetStencilReference; replay_.set_blend_constants = vk.vkCmdSetBlendConstants; + replay_.set_depth_bounds = vk.vkCmdSetDepthBounds; replay_.draw = vk.vkCmdDraw; replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled replay_.draw_indexed = vk.vkCmdDrawIndexed; diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 889bb71f72..0c9dbd5370 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -769,6 +769,7 @@ struct GraphicsPipeline { std::uint32_t zero_step_slots = 0; // per-instance slots with InstanceDataStepRate 0: stride 0 at draw time std::uint32_t strip_cut = 0; // D3D12_INDEX_BUFFER_STRIP_CUT_VALUE (0: off, 1: 0xFFFF, 2: 0xFFFFFFFF) bool mesh = false; // a mesh shader instead of a vertex shader: drawn by DispatchMesh only + bool depth_bounds = false; // VK_DYNAMIC_STATE_DEPTH_BOUNDS enabled; draw applies OMSetDepthBounds // The same pipeline with some attachment formats VK_FORMAT_UNDEFINED, for a draw that leaves those slots // unbound (D3D12 allows it; dynamic rendering needs the formats to agree). Bit i: colour slot i, bit 8: depth // and stencil. Null when it cannot be built. @@ -854,6 +855,7 @@ struct ReplayFunctions { PFN_vkCmdSetPrimitiveRestartEnable set_primitive_restart_enable = nullptr; PFN_vkCmdSetStencilReference set_stencil_reference = nullptr; PFN_vkCmdSetBlendConstants set_blend_constants = nullptr; + PFN_vkCmdSetDepthBounds set_depth_bounds = nullptr; PFN_vkCmdDraw draw = nullptr; PFN_vkCmdDrawMeshTasksEXT draw_mesh_tasks = nullptr; // VK_EXT_mesh_shader; null: mesh draws are unsupported PFN_vkCmdDrawIndexed draw_indexed = nullptr; @@ -2103,6 +2105,7 @@ ReplayResult prepare_draw(const Draw& draw, Resolve&& resolve, const ReplayFunct if (!pipeline || !pipeline->pipeline) return ReplayResult::unresolved_resource; if (pipeline->mesh != draw.mesh) return ReplayResult::invalid_draw; if (draw.mesh && !vk.draw_mesh_tasks) return ReplayResult::unsupported; + if (pipeline->depth_bounds && !vk.set_depth_bounds) return ReplayResult::unsupported; if (pipeline->bindless_set && !vk.bind_descriptor_sets) return ReplayResult::unsupported; if (pipeline->color_formats.size() > max_render_targets) return ReplayResult::unsupported; p.graphics = pipeline; @@ -4200,6 +4203,15 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c vk.set_stencil_reference(command_buffer, VK_STENCIL_FACE_FRONT_BIT, draw->stencil_front); vk.set_stencil_reference(command_buffer, VK_STENCIL_FACE_BACK_BIT, draw->stencil_back); vk.set_blend_constants(command_buffer, draw->blend_factor.data()); + if (graphics.depth_bounds) { + const float lo = (draw->depth_bounds_min == draw->depth_bounds_min) + ? std::clamp(draw->depth_bounds_min, 0.0f, 1.0f) + : 0.0f; + const float hi = (draw->depth_bounds_max == draw->depth_bounds_max) + ? std::clamp(draw->depth_bounds_max, 0.0f, 1.0f) + : 1.0f; + vk.set_depth_bounds(command_buffer, lo, hi); + } for (std::uint32_t slot = 0; slot < d.vertex_buffers.size(); ++slot) { if (!d.vertex_buffers[slot]) continue; vk.bind_vertex_buffers2(command_buffer, slot, 1, &d.vertex_buffers[slot], &d.vertex_offsets[slot], diff --git a/runtime/test/d3d12_depth_bounds_test.cpp b/runtime/test/d3d12_depth_bounds_test.cpp new file mode 100644 index 0000000000..5aeaa95a2d --- /dev/null +++ b/runtime/test/d3d12_depth_bounds_test.cpp @@ -0,0 +1,503 @@ +// Side-by-side parity test for hardware depth-bounds testing (D3D-DEPTH-BOUNDS): +// - D3D12_FEATURE_DATA_D3D12_OPTIONS2::DepthBoundsTestSupported (matches host & follows features.depthBounds) +// - ID3D12Device2::CreatePipelineState with DEPTH_STENCIL1 and DEPTH_STENCIL2 (DepthBoundsTestEnable = TRUE) +// - ID3D12GraphicsCommandList1::OMSetDepthBounds(Min, Max) -> vkCmdSetDepthBounds / VK_DYNAMIC_STATE_DEPTH_BOUNDS +// - Bundle refusal parity for OMSetDepthBounds +// - Live pixel-for-pixel GPU comparison against the host Windows D3D12 runtime on the same adapter (matched by LUID) +// for default [0, 1] bounds, passing range, rejecting low/high ranges, inclusive boundary [0.5, 0.5], +// DEPTH_STENCIL2, and Reset() restoring [0, 1]. +// Self-contained: builds minimal SPIR-V 1.3 shaders in memory (no glslangValidator or llvm-mc needed) and compiles +// matching HLSL for the host with d3dcompiler_47. Synthetic data only. +#include "vk_test_device.h" +#include "host_wine.h" +#include "d3d12_test_shaders.h" +#include "../shims/d3d12_com.h" +#include "../src/shader_library.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using d3d12test::Ref; + +#define CHECK(expr) \ + do { \ + if (!(expr)) { \ + std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #expr); \ + return 1; \ + } \ + } while (0) + +template +struct alignas(void*) Subobject { + D3D12_PIPELINE_STATE_SUBOBJECT_TYPE type = Type; + Inner inner{}; +}; + +struct StreamBuilder { + std::vector bytes; + template + void add(const S& s) { + const std::size_t at = bytes.size(); + bytes.resize(at + sizeof(S)); + std::memcpy(bytes.data() + at, &s, sizeof(S)); + } + D3D12_PIPELINE_STATE_STREAM_DESC desc() const { return {bytes.size(), const_cast(bytes.data())}; } +}; + +using RootSigSub = Subobject; +using VsSub = Subobject; +using PsSub = Subobject; +using RasterSub = Subobject; +using DepthStencil1Sub = Subobject; +using DepthStencil2Sub = Subobject; +using TopologySub = Subobject; +using RtvFormatsSub = Subobject; +using DsvFormatSub = Subobject; + +// Helper to append a SPIR-V instruction word + operands. +void emit_op(std::vector& out, std::uint16_t opcode, std::initializer_list ops) { + out.push_back((std::uint32_t(ops.size() + 1) << 16) | opcode); + out.insert(out.end(), ops.begin(), ops.end()); +} + +// Minimal valid SPIR-V 1.3 vertex shader: fullscreen triangle from gl_VertexIndex (0, 1, 2) at z = 0.0, w = 1.0. +std::vector make_fullscreen_vs_spv() { + std::vector w = {0x07230203u, 0x00010300u, 0u, 30u, 0u}; + // Capabilities & memory model. + emit_op(w, 17, {1}); // OpCapability Shader + emit_op(w, 14, {0, 1}); // OpMemoryModel Logical GLSL450 + // Entry point: Vertex %main(4) "main" %per_vertex(10) %gl_VertexIndex(13). + emit_op(w, 15, {0, 4, 0x6e69616du, 0u, 10, 13}); + // Decorations: struct %PerVertex(25) { vec4 Position }; + emit_op(w, 71, {25, 2}); // OpDecorate %25 Block + emit_op(w, 72, {25, 0, 11, 0}); // OpMemberDecorate %25 0 BuiltIn Position + emit_op(w, 71, {13, 11, 42}); // OpDecorate %13 BuiltIn VertexIndex + // Types & constants. + emit_op(w, 19, {1}); // %1 = OpTypeVoid + emit_op(w, 33, {2, 1}); // %2 = OpTypeFunction %1 + emit_op(w, 22, {3, 32}); // %3 = OpTypeFloat 32 + emit_op(w, 23, {5, 3, 4}); // %5 = OpTypeVector %3 4 + emit_op(w, 30, {25, 5}); // %25 = OpTypeStruct %5 + emit_op(w, 32, {26, 3, 25}); // %26 = OpTypePointer Output %25 + emit_op(w, 32, {6, 3, 5}); // %6 = OpTypePointer Output %5 + emit_op(w, 21, {7, 32, 1}); // %7 = OpTypeInt 32 1 + emit_op(w, 32, {8, 1, 7}); // %8 = OpTypePointer Input %7 + emit_op(w, 20, {9}); // %9 = OpTypeBool + emit_op(w, 59, {26, 10, 3}); // %10 = OpVariable %26 Output + emit_op(w, 59, {8, 13, 1}); // %13 = OpVariable %8 Input + emit_op(w, 43, {7, 27, 0}); // %27 = OpConstant %7 0 + emit_op(w, 43, {7, 14, 1}); // %14 = OpConstant %7 1 + emit_op(w, 43, {7, 15, 2}); // %15 = OpConstant %7 2 + emit_op(w, 43, {3, 16, std::bit_cast(-1.0f)}); + emit_op(w, 43, {3, 17, std::bit_cast(3.0f)}); + emit_op(w, 43, {3, 18, std::bit_cast(0.0f)}); + emit_op(w, 43, {3, 19, std::bit_cast(1.0f)}); + // Function %main(4). + emit_op(w, 54, {1, 4, 0, 2}); // OpFunction + emit_op(w, 248, {11}); // OpLabel + emit_op(w, 61, {7, 12, 13}); // %12 = OpLoad %7 %13 + emit_op(w, 170, {9, 20, 12, 14}); // %20 = OpIEqual %9 %12 1 + emit_op(w, 169, {3, 21, 20, 17, 16}); // %21 = OpSelect %3 %20 3.0 -1.0 + emit_op(w, 170, {9, 22, 12, 15}); // %22 = OpIEqual %9 %12 2 + emit_op(w, 169, {3, 23, 22, 17, 16}); // %23 = OpSelect %3 %22 3.0 -1.0 + emit_op(w, 80, {5, 24, 21, 23, 18, 19}); // %24 = OpCompositeConstruct %5 %21 %23 0.0 1.0 + emit_op(w, 65, {6, 28, 10, 27}); // %28 = OpAccessChain %6 %10 0 + emit_op(w, 62, {28, 24}); // OpStore %28 %24 + emit_op(w, 253, {}); // OpReturn + emit_op(w, 56, {}); // OpFunctionEnd + return w; +} + +// Minimal valid SPIR-V 1.3 fragment shader: writes constant RGBA (0.75, 0.5, 0.25, 0.5) to Location 0. +std::vector make_solid_ps_spv() { + std::vector w = {0x07230203u, 0x00010300u, 0u, 15u, 0u}; + emit_op(w, 17, {1}); // OpCapability Shader + emit_op(w, 14, {0, 1}); // OpMemoryModel Logical GLSL450 + emit_op(w, 15, {4, 4, 0x6e69616du, 0u, 7}); // OpEntryPoint Fragment %4 "main" %7 + emit_op(w, 16, {4, 7}); // OpExecutionMode %4 OriginUpperLeft + emit_op(w, 71, {7, 30, 0}); // OpDecorate %7 Location 0 + emit_op(w, 19, {1}); // %1 = OpTypeVoid + emit_op(w, 33, {2, 1}); // %2 = OpTypeFunction %1 + emit_op(w, 22, {3, 32}); // %3 = OpTypeFloat 32 + emit_op(w, 23, {5, 3, 4}); // %5 = OpTypeVector %3 4 + emit_op(w, 32, {6, 3, 5}); // %6 = OpTypePointer Output %5 + emit_op(w, 59, {6, 7, 3}); // %7 = OpVariable %6 Output + emit_op(w, 43, {3, 8, std::bit_cast(0.75f)}); + emit_op(w, 43, {3, 9, std::bit_cast(0.5f)}); + emit_op(w, 43, {3, 10, std::bit_cast(0.25f)}); + emit_op(w, 43, {3, 11, std::bit_cast(0.5f)}); + emit_op(w, 44, {5, 12, 8, 9, 10, 11}); // %12 = OpConstantComposite + emit_op(w, 54, {1, 4, 0, 2}); // OpFunction + emit_op(w, 248, {13}); // OpLabel + emit_op(w, 62, {7, 12}); // OpStore %7 %12 + emit_op(w, 253, {}); // OpReturn + emit_op(w, 56, {}); // OpFunctionEnd + return w; +} + +bool write_spv(const std::filesystem::path& path, const std::vector& words) { + std::ofstream f(path, std::ios::binary | std::ios::trunc); + if (!f) return false; + f.write(reinterpret_cast(words.data()), static_cast(words.size() * 4)); + return bool(f); +} + +constexpr char kHostVs[] = + "float4 main(uint id : SV_VertexID) : SV_Position {\n" + " float x = (id == 1) ? 3.0 : -1.0;\n" + " float y = (id == 2) ? 3.0 : -1.0;\n" + " return float4(x, y, 0.0, 1.0);\n" + "}\n"; +constexpr char kHostPs[] = "float4 main() : SV_Target { return float4(0.75, 0.5, 0.25, 0.5); }\n"; + +bool compile_hlsl(pD3DCompile compile, const char* source, std::vector& out, bool vertex) { + ID3DBlob* code = nullptr; + ID3DBlob* errors = nullptr; + const HRESULT hr = compile(source, std::strlen(source), nullptr, nullptr, nullptr, "main", vertex ? "vs_5_0" : "ps_5_0", + 0, 0, &code, &errors); + if (errors) { + std::fprintf(stderr, "%s\n", static_cast(errors->GetBufferPointer())); + errors->Release(); + } + if (FAILED(hr) || !code) return false; + const auto* bytes = static_cast(code->GetBufferPointer()); + out.assign(bytes, bytes + code->GetBufferSize()); + code->Release(); + return true; +} + +bool host_device_for_vk(const vktest::Device& vk, Ref& out, std::string& name) { + VkPhysicalDeviceIDProperties id{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES}; + VkPhysicalDeviceProperties2 props{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &id}; + vk.instance_fn("vkGetPhysicalDeviceProperties2")(vk.physical, &props); + Ref factory; + if (!id.deviceLUIDValid || FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) return false; + LUID luid{}; + std::memcpy(&luid, id.deviceLUID, sizeof(luid)); + Ref adapter; + if (FAILED(factory->EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter.p)))) return false; + DXGI_ADAPTER_DESC1 desc{}; + adapter->GetDesc1(&desc); + char utf8[256]{}; + WideCharToMultiByte(CP_UTF8, 0, desc.Description, -1, utf8, sizeof(utf8), nullptr, nullptr); + name = utf8; + return SUCCEEDED(D3D12CreateDevice(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&out.p))); +} + +struct Programs { + std::vector root, vs, ps; +}; + +struct BoundsCase { + const char* name; + bool use_ds2 = false; + bool call_set_bounds = false; + bool reset_after_reject = false; + float clear_depth = 0.5f; + float min_depth = 0.0f; + float max_depth = 1.0f; + bool expect_pass = true; +}; + +HRESULT make_bounds_pso(ID3D12Device* dev, ID3D12RootSignature* root, const Programs& p, bool use_ds2, + Ref& out) { + Ref dev2; + if (FAILED(dev->QueryInterface(IID_PPV_ARGS(&dev2.p)))) return E_NOINTERFACE; + StreamBuilder sb; + sb.add(RootSigSub{.inner = root}); + sb.add(VsSub{.inner = {p.vs.data(), p.vs.size()}}); + sb.add(PsSub{.inner = {p.ps.data(), p.ps.size()}}); + D3D12_RASTERIZER_DESC r{}; + r.FillMode = D3D12_FILL_MODE_SOLID; + r.CullMode = D3D12_CULL_MODE_NONE; + r.DepthClipEnable = TRUE; + sb.add(RasterSub{.inner = r}); + if (use_ds2) { + D3D12_DEPTH_STENCIL_DESC2 ds{}; + ds.DepthEnable = FALSE; + ds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO; + ds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + ds.DepthBoundsTestEnable = TRUE; + ds.FrontFace = {D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, + D3D12_COMPARISON_FUNC_ALWAYS, 0xff, 0xff}; + ds.BackFace = ds.FrontFace; + sb.add(DepthStencil2Sub{.inner = ds}); + } else { + D3D12_DEPTH_STENCIL_DESC1 ds{}; + ds.DepthEnable = FALSE; + ds.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO; + ds.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + ds.StencilReadMask = ds.StencilWriteMask = 0xff; + ds.FrontFace = {D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, + D3D12_COMPARISON_FUNC_ALWAYS}; + ds.BackFace = ds.FrontFace; + ds.DepthBoundsTestEnable = TRUE; + sb.add(DepthStencil1Sub{.inner = ds}); + } + sb.add(TopologySub{.inner = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE}); + D3D12_RT_FORMAT_ARRAY rts{}; + rts.NumRenderTargets = 1; + rts.RTFormats[0] = DXGI_FORMAT_R8G8B8A8_UNORM; + sb.add(RtvFormatsSub{.inner = rts}); + sb.add(DsvFormatSub{.inner = DXGI_FORMAT_D32_FLOAT}); + const auto stream = sb.desc(); + return dev2->CreatePipelineState(&stream, IID_PPV_ARGS(&out.p)); +} + +bool run_case(ID3D12Device* dev, const Programs& p, const BoundsCase& c, std::uint8_t pixel_out[4]) { + Ref root; + if (FAILED(dev->CreateRootSignature(0, p.root.data(), p.root.size(), IID_PPV_ARGS(&root.p)))) return false; + Ref pso; + if (FAILED(make_bounds_pso(dev, root.p, p, c.use_ds2, pso))) return false; + + const D3D12_HEAP_PROPERTIES def_heap{D3D12_HEAP_TYPE_DEFAULT}, rb_heap{D3D12_HEAP_TYPE_READBACK}; + D3D12_RESOURCE_DESC cd{}; + cd.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + cd.Width = cd.Height = 4; + cd.DepthOrArraySize = cd.MipLevels = 1; + cd.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + cd.SampleDesc = {1, 0}; + cd.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET; + D3D12_RESOURCE_DESC dd = cd; + dd.Format = DXGI_FORMAT_D32_FLOAT; + dd.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + Ref color, depth; + if (FAILED(dev->CreateCommittedResource(&def_heap, D3D12_HEAP_FLAG_NONE, &cd, D3D12_RESOURCE_STATE_RENDER_TARGET, + nullptr, IID_PPV_ARGS(&color.p))) || + FAILED(dev->CreateCommittedResource(&def_heap, D3D12_HEAP_FLAG_NONE, &dd, D3D12_RESOURCE_STATE_DEPTH_WRITE, + nullptr, IID_PPV_ARGS(&depth.p)))) + return false; + + D3D12_PLACED_SUBRESOURCE_FOOTPRINT foot{}; + UINT rows = 0; + UINT64 row_bytes = 0, total = 0; + dev->GetCopyableFootprints(&cd, 0, 1, 0, &foot, &rows, &row_bytes, &total); + Ref readback; + const auto rb_desc = d3d12test::buffer_desc(total); + if (FAILED(dev->CreateCommittedResource(&rb_heap, D3D12_HEAP_FLAG_NONE, &rb_desc, D3D12_RESOURCE_STATE_COPY_DEST, + nullptr, IID_PPV_ARGS(&readback.p)))) + return false; + + Ref rtv_heap, dsv_heap; + const D3D12_DESCRIPTOR_HEAP_DESC rh{D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1}, dh{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1}; + if (FAILED(dev->CreateDescriptorHeap(&rh, IID_PPV_ARGS(&rtv_heap.p))) || + FAILED(dev->CreateDescriptorHeap(&dh, IID_PPV_ARGS(&dsv_heap.p)))) + return false; + const auto rtv = rtv_heap->GetCPUDescriptorHandleForHeapStart(); + const auto dsv = dsv_heap->GetCPUDescriptorHandleForHeapStart(); + dev->CreateRenderTargetView(color.p, nullptr, rtv); + dev->CreateDepthStencilView(depth.p, nullptr, dsv); + + Ref queue; + Ref alloc; + Ref list; + Ref list1; + Ref fence; + const D3D12_COMMAND_QUEUE_DESC qd{D3D12_COMMAND_LIST_TYPE_DIRECT}; + if (FAILED(dev->CreateCommandQueue(&qd, IID_PPV_ARGS(&queue.p))) || + FAILED(dev->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&alloc.p))) || + FAILED(dev->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, alloc.p, pso.p, IID_PPV_ARGS(&list.p))) || + FAILED(list->QueryInterface(IID_PPV_ARGS(&list1.p))) || + FAILED(dev->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))) + return false; + + if (c.reset_after_reject) { + // Set rejecting bounds, close, and reset: Reset() must restore default [0.0, 1.0]. + list1->OMSetDepthBounds(0.8f, 0.9f); + if (FAILED(list->Close()) || FAILED(alloc->Reset()) || FAILED(list->Reset(alloc.p, pso.p))) return false; + } + + const FLOAT clear_color[4] = {0.2f, 0.4f, 0.6f, 0.8f}; + list->ClearRenderTargetView(rtv, clear_color, 0, nullptr); + list->ClearDepthStencilView(dsv, D3D12_CLEAR_FLAG_DEPTH, c.clear_depth, 0, 0, nullptr); + list->OMSetRenderTargets(1, &rtv, FALSE, &dsv); + const D3D12_VIEWPORT vp{0.0f, 0.0f, 4.0f, 4.0f, 0.0f, 1.0f}; + const D3D12_RECT sc{0, 0, 4, 4}; + list->RSSetViewports(1, &vp); + list->RSSetScissorRects(1, &sc); + list->SetGraphicsRootSignature(root.p); + list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); + if (c.call_set_bounds) list1->OMSetDepthBounds(c.min_depth, c.max_depth); + list->DrawInstanced(3, 1, 0, 0); + + const auto to_src = d3d12test::transition(color.p, D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &to_src); + D3D12_TEXTURE_COPY_LOCATION src_loc{color.p, D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX}; + src_loc.SubresourceIndex = 0; + D3D12_TEXTURE_COPY_LOCATION dst_loc{readback.p, D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT}; + dst_loc.PlacedFootprint = foot; + list->CopyTextureRegion(&dst_loc, 0, 0, 0, &src_loc, nullptr); + if (FAILED(list->Close())) return false; + + ID3D12CommandList* lists[] = {list.p}; + queue->ExecuteCommandLists(1, lists); + if (FAILED(queue->Signal(fence.p, 1)) || FAILED(fence->SetEventOnCompletion(1, nullptr))) return false; + + void* mapped = nullptr; + const D3D12_RANGE range{0, static_cast(total)}; + if (FAILED(readback->Map(0, &range, &mapped)) || !mapped) return false; + std::memcpy(pixel_out, static_cast(mapped) + foot.Offset, 4); + const D3D12_RANGE empty{0, 0}; + readback->Unmap(0, &empty); + return true; +} + +} // namespace + +int main() { + vktest::Device vk; + vk.request_buffer_device_address = true; + const auto status = vk.initialize(); + if (status == vktest::DeviceStatus::unavailable) return 77; + if (status != vktest::DeviceStatus::ready) return 1; + + const auto vs_program = d3d12test::fssm_program({0x5653db01u, 0xbf810000u}); + const auto ps_program = d3d12test::fssm_program({0x5053db01u, 0xbf810000u}); + const auto library = std::filesystem::temp_directory_path() / + ("leonida_depth_bounds_test_" + std::to_string(GetCurrentProcessId())); + std::filesystem::create_directories(library); + CHECK(write_spv(library / "vs.spv", make_fullscreen_vs_spv())); + CHECK(write_spv(library / "ps.spv", make_solid_ps_spv())); + { + std::ofstream index(library / "index.json", std::ios::binary | std::ios::trunc); + index << "{\"vs\": {\"stage\": \"vs\", \"fssm_sha256\": \"" << leonida::sha256_hex(vs_program) + << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"input_signature\": []},\n" + " \"ps\": {\"stage\": \"ps\", \"fssm_sha256\": \"" << leonida::sha256_hex(ps_program) + << "\", \"bindings\": [], \"push_constant_bytes\": 144, \"outputs\": [0]}}"; + } + + rt::d3d12::VulkanDeviceInfo info{}; + info.get_instance_proc = vk.instance_proc; + info.instance = vk.instance; + info.physical = vk.physical; + info.device = vk.device; + info.queue = vk.queue; + info.queue_family = vk.family; + info.bindless = vk.bindless; + info.bindless_partially_bound = vk.partially_bound; + info.host_image_copy = vk.host_image_copy; + info.buffer_device_address = vk.buffer_device_address; + info.features = vk.enabled; + info.shader_dir = library.string(); + + Ref ours; + CHECK(SUCCEEDED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))); + + D3D12_FEATURE_DATA_D3D12_OPTIONS2 o2{}; + CHECK(SUCCEEDED(ours->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &o2, sizeof(o2)))); + CHECK(o2.DepthBoundsTestSupported == (vk.enabled.depthBounds ? TRUE : FALSE)); + + D3D12_VERSIONED_ROOT_SIGNATURE_DESC root_desc{}; + root_desc.Version = D3D_ROOT_SIGNATURE_VERSION_1_0; + Programs our_programs; + our_programs.root = d3d12test::serialize_root_signature(root_desc); + CHECK(!our_programs.root.empty()); + our_programs.vs = d3d12test::shader_container(vs_program); + our_programs.ps = d3d12test::shader_container(ps_program); + + // 1. Verify feature-off device refuses DepthBoundsTestEnable = TRUE with E_NOTIMPL and reports FALSE. + { + rt::d3d12::VulkanDeviceInfo no_db = info; + no_db.features.depthBounds = VK_FALSE; + Ref dev_off; + CHECK(SUCCEEDED(rt::d3d12::create_vulkan_device(no_db, IID_PPV_ARGS(&dev_off.p)))); + D3D12_FEATURE_DATA_D3D12_OPTIONS2 off_o2{}; + CHECK(SUCCEEDED(dev_off->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &off_o2, sizeof(off_o2)))); + CHECK(off_o2.DepthBoundsTestSupported == FALSE); + Ref rs; + CHECK(SUCCEEDED(dev_off->CreateRootSignature(0, our_programs.root.data(), our_programs.root.size(), + IID_PPV_ARGS(&rs.p)))); + Ref refused1, refused2; + CHECK(make_bounds_pso(dev_off.p, rs.p, our_programs, false, refused1) == E_NOTIMPL && !refused1.p); + CHECK(make_bounds_pso(dev_off.p, rs.p, our_programs, true, refused2) == E_NOTIMPL && !refused2.p); + } + + // 2. Verify OMSetDepthBounds is rejected in a bundle. + { + Ref ba; + Ref b; + Ref b1; + CHECK(SUCCEEDED(ours->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, IID_PPV_ARGS(&ba.p)))); + CHECK(SUCCEEDED(ours->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_BUNDLE, ba.p, nullptr, IID_PPV_ARGS(&b.p)))); + CHECK(SUCCEEDED(b->QueryInterface(IID_PPV_ARGS(&b1.p)))); + b1->OMSetDepthBounds(0.2f, 0.8f); + CHECK(FAILED(b->Close())); + } + + if (!vk.enabled.depthBounds) { + std::printf("GPU lacks VkPhysicalDeviceFeatures::depthBounds; refusal checks passed, skipping draw checks\n"); + std::filesystem::remove_all(library); + return 0; + } + + // 3. Run live GPU draw cases on our Vulkan device (and side-by-side against host Windows D3D12 when available). + const BoundsCase cases[] = { + {"default [0.0, 1.0] passes at z=0.5", false, false, false, 0.5f, 0.0f, 1.0f, true}, + {"[0.25, 0.75] passes at z=0.5", false, true, false, 0.5f, 0.25f, 0.75f, true}, + {"[0.6, 0.9] rejects at z=0.5", false, true, false, 0.5f, 0.6f, 0.9f, false}, + {"[0.1, 0.4] rejects at z=0.5", false, true, false, 0.5f, 0.1f, 0.4f, false}, + {"[0.5, 0.5] inclusive boundary passes at z=0.5", false, true, false, 0.5f, 0.5f, 0.5f, true}, + {"DEPTH_STENCIL2 [0.6, 0.9] rejects at z=0.5", true, true, false, 0.5f, 0.6f, 0.9f, false}, + {"DEPTH_STENCIL2 [0.25, 0.75] passes at z=0.5", true, true, false, 0.5f, 0.25f, 0.75f, true}, + {"Reset() restores default [0.0, 1.0] after reject", false, false, true, 0.5f, 0.0f, 1.0f, true}, + }; + + Ref host; + std::string adapter; + Programs host_programs; + bool have_host = false; + if (!hostwine::host_is_wine() && host_device_for_vk(vk, host, adapter)) { + D3D12_FEATURE_DATA_D3D12_OPTIONS2 host_o2{}; + if (SUCCEEDED(host->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &host_o2, sizeof(host_o2))) && + host_o2.DepthBoundsTestSupported) { + const HMODULE compiler = LoadLibraryExA("d3dcompiler_47.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); + const auto compile = compiler ? reinterpret_cast(reinterpret_cast( + GetProcAddress(compiler, "D3DCompile"))) + : nullptr; + host_programs.root = our_programs.root; + if (compile && compile_hlsl(compile, kHostVs, host_programs.vs, true) && + compile_hlsl(compile, kHostPs, host_programs.ps, false)) { + have_host = true; + std::printf("host reference: %s (DepthBoundsTestSupported=TRUE)\n", adapter.c_str()); + } + } + } + + // Clear color (0.2, 0.4, 0.6, 0.8) ~= (51, 102, 153, 204); draw color (0.75, 0.5, 0.25, 0.5) ~= (191, 128, 64, 128). + for (const BoundsCase& c : cases) { + std::uint8_t ours_px[4]{}; + CHECK(run_case(ours.p, our_programs, c, ours_px)); + const bool looks_drawn = (ours_px[0] > 150 && ours_px[2] < 100); + const bool looks_clear = (ours_px[0] < 80 && ours_px[2] > 120); + std::printf(" %-52s vulkan=(%3u,%3u,%3u,%3u)", c.name, ours_px[0], ours_px[1], ours_px[2], ours_px[3]); + CHECK(c.expect_pass ? looks_drawn : looks_clear); + if (have_host) { + std::uint8_t host_px[4]{}; + CHECK(run_case(host.p, host_programs, c, host_px)); + std::printf(" host=(%3u,%3u,%3u,%3u)", host_px[0], host_px[1], host_px[2], host_px[3]); + for (int ch = 0; ch < 4; ++ch) { + const int diff = std::abs(int(ours_px[ch]) - int(host_px[ch])); + CHECK(diff <= 1); + } + } + std::printf(" OK\n"); + } + + std::filesystem::remove_all(library); + CHECK(vk.validation_errors() == 0); + std::printf("d3d12_depth_bounds_test: ALL %zu CASES PASSED%s\n", std::size(cases), + have_host ? " (EXACT MATCH vs host D3D12)" : ""); + return 0; +} diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index df430c11a6..2160557a31 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -263,11 +263,17 @@ int main() { bad = mesh_stream; bad.add(AsSubobject{.inner = vs}); EXPECT(create(device.p, bad, refused) == E_NOTIMPL && !refused.p); + // DepthBoundsTestEnable follows VkPhysicalDeviceFeatures::depthBounds. bad = mesh_stream; D3D12_DEPTH_STENCIL_DESC1 bounds{}; bounds.DepthBoundsTestEnable = TRUE; bad.add(DepthStencil1Subobject{.inner = bounds}); - EXPECT(create(device.p, bad, refused) == E_NOTIMPL && !refused.p); + if (vk.enabled.depthBounds) { + Ref bounds_pso; + EXPECT(SUCCEEDED(create(device.p, bad, bounds_pso)) && bounds_pso.p); + } else { + EXPECT(create(device.p, bad, refused) == E_NOTIMPL && !refused.p); + } const D3D12_HEAP_PROPERTIES default_heap{D3D12_HEAP_TYPE_DEFAULT}; const D3D12_HEAP_PROPERTIES upload_heap{D3D12_HEAP_TYPE_UPLOAD}; diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 41418c5386..da86f279cb 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -2421,7 +2421,7 @@ int compare_feature_options(ID3D12Device* ours, const rt::d3d12::VulkanDeviceInf fail("unknown feature unexpectedly answered"); // Local invariants for what this layer actually implements. - if (o2.DepthBoundsTestSupported || + if (o2.DepthBoundsTestSupported != (info.features.depthBounds ? TRUE : FALSE) || o2.ProgrammableSamplePositionsTier != D3D12_PROGRAMMABLE_SAMPLE_POSITIONS_TIER_NOT_SUPPORTED) fail("OPTIONS2 unexpected support"); if (o3.CopyQueueTimestampQueriesSupported || o3.CastingFullyTypedFormatSupported || @@ -2470,6 +2470,21 @@ int compare_feature_options(ID3D12Device* ours, const rt::d3d12::VulkanDeviceInf o21.SampleCmpGradientAndBiasSupported || o21.ExtendedCommandInfoSupported) fail("OPTIONS21 unexpected support"); + // Verify OPTIONS2.DepthBoundsTestSupported follows VulkanDeviceInfo::features.depthBounds. + { + rt::d3d12::VulkanDeviceInfo toggled_info = info; + toggled_info.features.depthBounds = info.features.depthBounds ? VK_FALSE : VK_TRUE; + Ref toggled; + if (FAILED(rt::d3d12::create_vulkan_device(toggled_info, IID_PPV_ARGS(&toggled.p)))) { + fail("failed to create toggled depthBounds device"); + } else { + D3D12_FEATURE_DATA_D3D12_OPTIONS2 toggled2{}; + if (FAILED(toggled->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &toggled2, sizeof(toggled2))) || + toggled2.DepthBoundsTestSupported != (toggled_info.features.depthBounds ? TRUE : FALSE)) + fail("OPTIONS2.DepthBoundsTestSupported did not follow features.depthBounds"); + } + } + // Verify OPTIONS19.AnisoFilterWithPointMipSupported follows VulkanDeviceInfo::sampler_anisotropy. { rt::d3d12::VulkanDeviceInfo toggled_info = info; diff --git a/runtime/test/replay_cpu_bench.cpp b/runtime/test/replay_cpu_bench.cpp index a3957d1669..f7c40a5f11 100644 --- a/runtime/test/replay_cpu_bench.cpp +++ b/runtime/test/replay_cpu_bench.cpp @@ -230,6 +230,7 @@ struct Scene { vk.set_primitive_restart_enable = nop(); vk.set_stencil_reference = nop(); vk.set_blend_constants = nop(); + vk.set_depth_bounds = nop(); vk.draw = nop(); vk.draw_indexed = nop(); } diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 5cccd1836e..0a080bcfc9 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -433,6 +433,7 @@ class Device { enable.shaderSampledImageArrayDynamicIndexing = bindless; enable.shaderStorageImageArrayDynamicIndexing = bindless; enable.logicOp = features.features.logicOp; // d3d12_draw_test's logic-op draw, where available + enable.depthBounds = features.features.depthBounds; // d3d12_depth_bounds_test, where available enable.geometryShader = features.features.geometryShader; // d3d12_geometry_test, where available enable.shaderInt64 = features.features.shaderInt64; // translated shaders declare Int64 for their addresses const char* conditional_extension = VK_EXT_CONDITIONAL_RENDERING_EXTENSION_NAME;