diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 53d9747968..fd9eeada23 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2452,7 +2452,11 @@ The trace will enumerate them. a VS stream builds the same PSO as the classic call. - `E_INVALIDARG`, as on the PC runtime: a type given twice, an unknown type, a truncated stream, MS with VS, tessellation or GS, CS with a graphics stage, or no stage at all. - - `E_NOTIMPL` with a log, for what is not built yet: an AS, a depth-bounds test, view instancing. + - `E_NOTIMPL` with a log, for what is not built yet: an AS, view instancing. + - `DEPTH_STENCIL1`/`DEPTH_STENCIL2` `DepthBoundsTestEnable` maps to hardware `depthBoundsTestEnable` + + `VK_DYNAMIC_STATE_DEPTH_BOUNDS` and `OMSetDepthBounds` — recorded on both direct lists and bundles, with + out-of-range/inverted/`NaN` bounds accepted at record time as on D3D12 and sanitized into Vulkan's `0..1` + range at replay — refusing with `E_NOTIMPL` when `VkPhysicalDeviceFeatures::depthBounds` is absent. - `DEPTH_STENCIL2`'s per-face read and write masks go to each face's `VkStencilOpState` (the front face's in the description, the back face's beside it), so `OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported` is TRUE: with `OMSetFrontAndBackStencilRef`'s per-face references, both of that cap's parts. @@ -2582,7 +2586,8 @@ The console has two list topologies Vulkan lacks: rectangle list (`IASetPrimitiv - `OPTIONS19` maps `SupportedSampleCountsWithNoOutputs` from `framebufferNoAttachmentsSampleCounts`, ties `AnisoFilterWithPointMipSupported` to `VulkanDeviceInfo::sampler_anisotropy`, and reports the baseline heap limits (`2048` samplers, `1000000` views); - - `OPTIONS21` reports baseline `D3D12_EXECUTE_INDIRECT_TIER_1_0`, and unimplemented features/tiers (`DepthBounds`, + - `OPTIONS2` reports `DepthBoundsTestSupported` from `VkPhysicalDeviceFeatures::depthBounds`; + - `OPTIONS21` reports baseline `D3D12_EXECUTE_INDIRECT_TIER_1_0`, and unimplemented features/tiers ( `CastingFullyTypedFormat`, MSAA UAVs, independent stencil masks, render passes, RT, mesh shaders, sampler feedback, wave MMA, work graphs, GPU upload heaps) stay `FALSE`/`NOT_SUPPORTED`/`TIER_0`. Checked live against the host D3D12 runtime on the same adapter (`NVIDIA GeForce RTX 5080`, driver `616.56`, Windows 11): 15/15 feature diff --git a/runtime/shims/d3d12_com.cpp b/runtime/shims/d3d12_com.cpp index 6dee464a8f..6a3818ff23 100644 --- a/runtime/shims/d3d12_com.cpp +++ b/runtime/shims/d3d12_com.cpp @@ -235,6 +235,8 @@ class GraphicsCommandList final } UINT front_stencil_ref() const { return front_stencil_ref_; } UINT back_stencil_ref() const { return back_stencil_ref_; } + float min_depth_bounds() const { return min_depth_bounds_; } + float max_depth_bounds() const { return max_depth_bounds_; } D3D12_GPU_VIRTUAL_ADDRESS cbv_srv_uav_base() const { return cbv_srv_uav_base_; } D3D12_GPU_VIRTUAL_ADDRESS sampler_base() const { return sampler_base_; } @@ -565,7 +567,8 @@ class GraphicsCommandList final } if (!any_nonempty_viewport()) return; // as record_draw: nothing is rasterized leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, {}, 0, viewport_, - scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, false, x, y, z, 0, 0}; + scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, + min_depth_bounds_, max_depth_bounds_, false, x, y, z, 0, 0}; note_direct_heap_indexing(graphics_.layout); draw.mesh = true; draw.viewports = draw_viewports(); @@ -938,6 +941,20 @@ class GraphicsCommandList final check(list_.resolve_queries({query_heap_id(heap), u(type), first, count, resource_id(dst), offset})); } + // GCL1. Dynamic depth-bounds range (default 0.0..1.0), allowed in bundles and accepting out-of-range/NaN at record + // time as D3D12 does (replayed cleanly inside Vulkan's 0..1 domain in vulkan_replay.h). + void STDMETHODCALLTYPE OMSetDepthBounds(FLOAT min, FLOAT max) override { + d3d12_trace::record("ID3D12GraphicsCommandList1::OMSetDepthBounds"); + if (list_.closed()) return; + if (defer([=](GraphicsCommandList& l) { + l.min_depth_bounds_ = min; + l.max_depth_bounds_ = max; + })) + return; + min_depth_bounds_ = min; + max_depth_bounds_ = max; + } + // GCL8. Each draw applies the two faces with vkCmdSetStencilReference(VK_STENCIL_FACE_FRONT_BIT / BACK_BIT). void STDMETHODCALLTYPE OMSetFrontAndBackStencilRef(UINT front, UINT back) override { d3d12_trace::record("ID3D12GraphicsCommandList8::OMSetFrontAndBackStencilRef"); @@ -1552,6 +1569,8 @@ class GraphicsCommandList final bool invalid_ = false; UINT front_stencil_ref_ = 0; UINT back_stencil_ref_ = 0; + float min_depth_bounds_ = 0.0f; + float max_depth_bounds_ = 1.0f; RootState compute_; RootState graphics_; std::uintptr_t compute_pipeline_ = 0; @@ -1681,6 +1700,8 @@ class GraphicsCommandList final void reset_state(ID3D12PipelineState* state) { front_stencil_ref_ = back_stencil_ref_ = 0; + min_depth_bounds_ = 0.0f; + max_depth_bounds_ = 1.0f; compute_ = {}; graphics_ = {}; cbv_srv_uav_heap_ = nullptr; @@ -2052,7 +2073,8 @@ class GraphicsCommandList final void record_raster_draw(bool indexed, UINT count, UINT instances, UINT first, INT base_vertex, UINT first_instance, const leonida::IndirectDrawArgs* indirect = nullptr) { leonida::Draw draw{graphics_pipeline_, graphics_user_data(), draw_views(), depth_, index_, topology_, viewport_, - scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, indexed, count, instances, + scissor_, blend_factor_, front_stencil_ref_, back_stencil_ref_, + min_depth_bounds_, max_depth_bounds_, indexed, count, instances, first, base_vertex, first_instance}; draw.viewports = draw_viewports(); const bool recorded = indirect ? list_.draw_indirect(std::move(draw), *indirect) : list_.draw(std::move(draw)); @@ -2807,6 +2829,14 @@ bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT& return true; } +bool depth_bounds(ID3D12GraphicsCommandList1* list, float& min_depth, float& max_depth) { + const GraphicsCommandList* l = GraphicsCommandList::from(list); + if (!l) return false; + min_depth = l->min_depth_bounds(); + max_depth = l->max_depth_bounds(); + return true; +} + bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector& commands) { const GraphicsCommandList* l = GraphicsCommandList::from(list); if (!l) return false; diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index 849791a262..70e7ef0533 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -578,6 +578,9 @@ void set_global_realtime_privilege_override_for_test(int state); // not one of this module's command lists. bool front_back_stencil_ref(ID3D12GraphicsCommandList8* list, UINT& front, UINT& back); +// The depth-bounds range last recorded by ID3D12GraphicsCommandList1::OMSetDepthBounds on `list` (default 0..1). +bool depth_bounds(ID3D12GraphicsCommandList1* list, float& min_depth, float& max_depth); + // A copy of the commands `list` has recorded so far, for tests of what a bundle puts into its parent. False when // `list` is not one of this module's command lists. bool recorded_commands(ID3D12GraphicsCommandList* list, std::vector& commands); diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index aeecf05b0d..6d2115d934 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -7691,7 +7691,10 @@ leonida::cached_pso::Key pipeline_key(const D3D12_COMPUTE_PIPELINE_STATE_DESC& d return leonida::sha256_hex(key.data()); } -leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc) { +// `extra` carries fields only a console stream has (back-face masks, depth bounds); they are hashed into the same key, +// as the cached blob holds just the first 64 characters. Without it the key is the plain description's. +leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, + const leonida::cached_pso::KeyBuilder* extra = nullptr) { leonida::cached_pso::KeyBuilder key; key.tag("graphics"); key_root_signature(key, desc.pRootSignature); @@ -7741,6 +7744,7 @@ leonida::cached_pso::Key pipeline_key(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& .u32(desc.NumRenderTargets); for (const DXGI_FORMAT format : desc.RTVFormats) key.u32(format); key.u32(desc.DSVFormat).u32(desc.SampleDesc.Count).u32(desc.SampleDesc.Quality).u32(desc.NodeMask); + if (extra) key.bytes(extra->data().data(), extra->data().size()); return leonida::sha256_hex(key.data()); } @@ -7784,6 +7788,10 @@ struct StencilFaceMasks { UINT8 write = D3D12_DEFAULT_STENCIL_WRITE_MASK; }; +struct GraphicsStreamExtras { + bool depth_bounds_test = false; +}; + struct PipelineStream { D3D12_GRAPHICS_PIPELINE_STATE_DESC graphics{}; bool back_masks = false; // DEPTH_STENCIL2: back_stencil holds the back face's masks @@ -7791,6 +7799,7 @@ struct PipelineStream { D3D12_COMPUTE_PIPELINE_STATE_DESC compute{}; D3D12_SHADER_BYTECODE ms{}; D3D12_SHADER_BYTECODE as{}; + GraphicsStreamExtras extras{}; bool has_cs = false; bool has_graphics_stage = false; // VS, PS, DS, HS, GS, AS or MS }; @@ -7931,9 +7940,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) { - return stream_unsupported("depth-bounds tests are not supported yet; stream subobject", int(type)); - } + out.extras.depth_bounds_test = ok && (d.DepthBoundsTestEnable != FALSE); g.DepthStencilState = {d.DepthEnable, d.DepthWriteMask, d.DepthFunc, d.StencilEnable, d.StencilReadMask, d.StencilWriteMask, d.FrontFace, d.BackFace}; break; @@ -7942,9 +7949,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) { - return stream_unsupported("depth-bounds tests are not supported yet; stream subobject", int(type)); - } + out.extras.depth_bounds_test = d.DepthBoundsTestEnable != FALSE; // Each face has its own read and write mask (OPTIONS14.IndependentFrontAndBackStencilRefMaskSupported): // the description takes the front face's, back_stencil the back face's. out.back_masks = true; @@ -8142,7 +8147,7 @@ class PipelineState final const auto* record=rt::d3d12_xs::find_pso_record(this); PipelineStream stream{}; HRESULT hr=record?parse_pipeline_stream(record->desc(),stream):E_INVALIDARG; - if(SUCCEEDED(hr))hr=stream.has_cs?create_compute(stream.compute):create_graphics(stream.graphics,&stream.ms,&stream.as); + if(SUCCEEDED(hr))hr=stream.has_cs?create_compute(stream.compute):create_graphics(stream.graphics,&stream.ms,&stream.as,stream.back_masks?&stream.back_stencil:nullptr,&stream.extras); }); } return registered_ && (is_graphics_==graphics); @@ -8203,7 +8208,9 @@ class PipelineState final if (seen.insert(line).second) std::fprintf(stderr, "%s\n", line); } HRESULT create_graphics(const D3D12_GRAPHICS_PIPELINE_STATE_DESC& desc, const D3D12_SHADER_BYTECODE* ms = nullptr, - const D3D12_SHADER_BYTECODE* as = nullptr, const StencilFaceMasks* back_masks = nullptr) { + const D3D12_SHADER_BYTECODE* as = nullptr, + const StencilFaceMasks* back_masks = nullptr, + const GraphicsStreamExtras* extras = nullptr) { auto& vk = device_.context->vk; const VkDevice dev = device_.info.device; const bool mesh = ms && ms->pShaderBytecode && ms->BytecodeLength; @@ -8708,6 +8715,7 @@ 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 = (extras && extras->depth_bounds_test) ? VK_TRUE : VK_FALSE; depth.stencilTestEnable = ds.StencilEnable; depth.front = stencil_face(ds.FrontFace, ds.StencilReadMask, ds.StencilWriteMask); depth.back = back_masks ? stencil_face(ds.BackFace, back_masks->read, back_masks->write) @@ -8779,6 +8787,8 @@ class PipelineState final return stream_unsupported("logic ops need logicOp, which the device lacks; op", int(first.LogicOp)); if (raster.depthClampEnable && !features.depthClamp) return stream_unsupported("DepthClipEnable = FALSE needs depthClamp, which the device lacks; flag", 0); + if (depth.depthBoundsTestEnable && !features.depthBounds) + return stream_unsupported("depth-bounds testing needs depthBounds, which the device lacks; enabled", 1); graphics_.viewport_with_count = features.multiViewport && vk.vkCmdSetViewportWithCount && vk.vkCmdSetScissorWithCount; @@ -8878,6 +8888,7 @@ class PipelineState final // Kept for variants: a draw that leaves a slot unbound needs this pipeline with that format undefined. graphics_.mesh = mesh; + graphics_.depth_bounds_test = depth.depthBoundsTestEnable == VK_TRUE; recipe_.mesh = mesh; recipe_.stages = std::move(stage_infos); recipe_.attributes = std::move(attributes); @@ -9324,7 +9335,7 @@ class PipelineState final // A mesh pipeline takes neither the topology nor the strides as dynamic state // (VUID-VkGraphicsPipelineCreateInfo-pDynamicStates-07065 and -07066). // With multiViewport enabled, VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT allow 1..16 dynamic viewports/scissors. - VkDynamicState dyn_list[8]{}; + VkDynamicState dyn_list[9]{}; std::uint32_t dyn_count = 0; dyn_list[dyn_count++] = graphics_.viewport_with_count ? VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT : VK_DYNAMIC_STATE_VIEWPORT; @@ -9336,6 +9347,7 @@ class PipelineState final } dyn_list[dyn_count++] = VK_DYNAMIC_STATE_STENCIL_REFERENCE; dyn_list[dyn_count++] = VK_DYNAMIC_STATE_BLEND_CONSTANTS; + if (r.depth.depthBoundsTestEnable == VK_TRUE) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_DEPTH_BOUNDS; if (!r.mesh && !r.bindings.empty()) dyn_list[dyn_count++] = VK_DYNAMIC_STATE_VERTEX_INPUT_BINDING_STRIDE; VkPipelineDynamicStateCreateInfo dynamic{VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO}; dynamic.dynamicStateCount = dyn_count; @@ -11638,7 +11650,17 @@ class Device final if(!record)return E_INVALIDARG; auto* pipeline=new PipelineState(state_); pipeline->attach_device(identity()); - pipeline->set_cache_key(stream.has_cs?pipeline_key(stream.compute):pipeline_key(stream.graphics)); + if(stream.has_cs) { + pipeline->set_cache_key(pipeline_key(stream.compute)); + } else { + // The stream description carries fields the plain graphics description cannot: + // DEPTH_STENCIL2 back-face masks and the depth-bounds enable. Mix them in so two + // console PSOs differing only in these do not share one cache key. + leonida::cached_pso::KeyBuilder extra; + extra.u32(stream.back_masks?1u:0u).u32(stream.back_stencil.read).u32(stream.back_stencil.write) + .u32(stream.extras.depth_bounds_test?1u:0u); + pipeline->set_cache_key(pipeline_key(stream.graphics,&extra)); + } if(!rt::d3d12_xs::attach_pso_record(pipeline,std::move(record))){pipeline->Release();return E_OUTOFMEMORY;} pipeline->set_pending_stream(stream.has_cs); return hand_out(pipeline,iid,out); @@ -13721,7 +13743,8 @@ class Device final pipeline->attach_device(identity()); const HRESULT hr = stream.has_cs ? pipeline->create_compute(stream.compute) : pipeline->create_graphics(stream.graphics, &stream.ms, &stream.as, - stream.back_masks ? &stream.back_stencil : nullptr); + stream.back_masks ? &stream.back_stencil : nullptr, + &stream.extras); if (FAILED(hr)) { pipeline->Release(); return finish(hr); @@ -15513,7 +15536,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); } @@ -18125,6 +18148,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin enable.logicOp = chosen_features.logicOp; enable.depthClamp = chosen_features.depthClamp; enable.multiViewport = chosen_features.multiViewport; + 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, formatless image diff --git a/runtime/src/command_list.h b/runtime/src/command_list.h index 3d7ed093bb..6bf5100578 100644 --- a/runtime/src/command_list.h +++ b/runtime/src/command_list.h @@ -454,6 +454,8 @@ struct Draw { std::array blend_factor{1, 1, 1, 1}; std::uint32_t stencil_front = 0; std::uint32_t stencil_back = 0; + float min_depth_bounds = 0.0f; + float max_depth_bounds = 1.0f; bool indexed = false; std::uint32_t count = 0; // vertices or indices per instance std::uint32_t instances = 0; diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 145fa11ee5..af2b4db3c8 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -125,7 +125,7 @@ struct VulkanDeviceFunctions { X(vkCmdBindVertexBuffers2) X(vkCmdBindIndexBuffer) X(vkCmdSetViewport) X(vkCmdSetScissor) \ X(vkCmdSetViewportWithCount) X(vkCmdSetScissorWithCount) \ X(vkCmdSetPrimitiveTopology) X(vkCmdSetPrimitiveRestartEnable) X(vkCmdSetStencilReference) \ - X(vkCmdSetBlendConstants) X(vkCmdDraw) \ + X(vkCmdSetDepthBounds) X(vkCmdSetBlendConstants) X(vkCmdDraw) \ X(vkCmdDrawIndexed) X(vkCreateGraphicsPipelines) X(vkQueueBindSparse) X(vkGetImageSparseMemoryRequirements) \ X(vkCreatePipelineCache) X(vkDestroyPipelineCache) X(vkGetPipelineCacheData) X(vkMergePipelineCaches) \ X(vkCreateEvent) X(vkDestroyEvent) X(vkResetEvent) X(vkCmdSetEvent) X(vkCmdWaitEvents) X(vkCmdDrawMeshTasksEXT) \ @@ -1147,6 +1147,7 @@ class VulkanContext { replay_.set_primitive_topology = vk.vkCmdSetPrimitiveTopology; replay_.set_primitive_restart_enable = vk.vkCmdSetPrimitiveRestartEnable; replay_.set_stencil_reference = vk.vkCmdSetStencilReference; + replay_.set_depth_bounds = vk.vkCmdSetDepthBounds; replay_.set_blend_constants = vk.vkCmdSetBlendConstants; replay_.draw = vk.vkCmdDraw; replay_.draw_mesh_tasks = vk.vkCmdDrawMeshTasksEXT; // null unless VK_EXT_mesh_shader is enabled diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index e20a291057..878b2639e1 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -867,6 +867,7 @@ struct GraphicsPipeline { 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 viewport_with_count = false; // pipeline uses VK_DYNAMIC_STATE_VIEWPORT_WITH_COUNT / SCISSOR_WITH_COUNT + bool depth_bounds_test = false; // VK_DYNAMIC_STATE_DEPTH_BOUNDS enabled on this pipeline // 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. @@ -1094,6 +1095,7 @@ struct ReplayFunctions { PFN_vkCmdSetPrimitiveTopology set_primitive_topology = nullptr; PFN_vkCmdSetPrimitiveRestartEnable set_primitive_restart_enable = nullptr; PFN_vkCmdSetStencilReference set_stencil_reference = nullptr; + PFN_vkCmdSetDepthBounds set_depth_bounds = nullptr; PFN_vkCmdSetBlendConstants set_blend_constants = nullptr; PFN_vkCmdDraw draw = nullptr; unsigned* mesh_draw_count = nullptr; // opt-in count for this submission @@ -2594,6 +2596,7 @@ ReplayResult prepare_draw(const Draw& draw, const IndirectDrawArgs* arguments, R if (draw.mesh && !vk.draw_mesh_tasks) return unsupported_at(__LINE__); if (pipeline->viewport_with_count && (!vk.set_viewport_with_count || !vk.set_scissor_with_count)) return unsupported_at(__LINE__); + if (pipeline->depth_bounds_test && !vk.set_depth_bounds) return unsupported_at(__LINE__); if (pipeline->bindless_set && !vk.bind_descriptor_sets) return unsupported_at(__LINE__); if (pipeline->color_formats.size() > max_render_targets) return unsupported_at(__LINE__); p.graphics = pipeline; @@ -5195,6 +5198,14 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ } 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); + if (graphics.depth_bounds_test && vk.set_depth_bounds) { + // D3D12: NaN converts to 0 silently; then the range is clamped, an inverted one is empty. + const float mn = draw->min_depth_bounds != draw->min_depth_bounds ? 0.0f : draw->min_depth_bounds; + const float mx = draw->max_depth_bounds != draw->max_depth_bounds ? 0.0f : draw->max_depth_bounds; + const bool empty = mn > mx || mn > 1.0f || mx < 0.0f; + vk.set_depth_bounds(command_buffer, empty ? 1.0f : (mn < 0.0f ? 0.0f : mn), + empty ? 0.0f : (mx > 1.0f ? 1.0f : mx)); + } vk.set_blend_constants(command_buffer, draw->blend_factor.data()); for (std::uint32_t slot = 0; slot < d.vertex_buffers.size(); ++slot) { if (!d.vertex_buffers[slot]) continue; diff --git a/runtime/test/d3d12_bundle_forbidden_test.cpp b/runtime/test/d3d12_bundle_forbidden_test.cpp index 3e3a5d1332..22c91ba1b4 100644 --- a/runtime/test/d3d12_bundle_forbidden_test.cpp +++ b/runtime/test/d3d12_bundle_forbidden_test.cpp @@ -120,6 +120,9 @@ std::vector calls() { {"OMSetBlendFactor", [](Env&, ID3D12GraphicsCommandList* b) { const FLOAT factor[4] = {1, 1, 1, 1}; b->OMSetBlendFactor(factor); }}, + {"OMSetDepthBounds", [](Env&, ID3D12GraphicsCommandList* b) { + Ref b1; + if (SUCCEEDED(b->QueryInterface(IID_PPV_ARGS(&b1.p)))) b1->OMSetDepthBounds(0.25f, 0.75f); }}, }; } diff --git a/runtime/test/d3d12_close_parity_test.cpp b/runtime/test/d3d12_close_parity_test.cpp index 7ae4f1e6d7..ae4f7c61b7 100644 --- a/runtime/test/d3d12_close_parity_test.cpp +++ b/runtime/test/d3d12_close_parity_test.cpp @@ -391,6 +391,41 @@ std::vector cases() { {"OMSetBlendFactor", true, match, CALLS(const FLOAT factor[4] = {0.25f, 0.5f, 0.75f, 1.0f}; e.list->OMSetBlendFactor(factor))}, {"OMSetStencilRef(7)", true, match, CALLS(e.list->OMSetStencilRef(7))}, + {"OMSetDepthBounds(0.25, 0.75)", true, match, + [](Env& e) { + Ref list1; + if (FAILED(e.list->QueryInterface(IID_PPV_ARGS(&list1.p)))) return false; + list1->OMSetDepthBounds(0.25f, 0.75f); + return true; + }}, + {"OMSetDepthBounds(-0.1, 0.5)", true, match, + [](Env& e) { + Ref list1; + if (FAILED(e.list->QueryInterface(IID_PPV_ARGS(&list1.p)))) return false; + list1->OMSetDepthBounds(-0.1f, 0.5f); + return true; + }}, + {"OMSetDepthBounds(0.5, 1.1)", true, match, + [](Env& e) { + Ref list1; + if (FAILED(e.list->QueryInterface(IID_PPV_ARGS(&list1.p)))) return false; + list1->OMSetDepthBounds(0.5f, 1.1f); + return true; + }}, + {"OMSetDepthBounds(0.8, 0.2)", true, match, + [](Env& e) { + Ref list1; + if (FAILED(e.list->QueryInterface(IID_PPV_ARGS(&list1.p)))) return false; + list1->OMSetDepthBounds(0.8f, 0.2f); + return true; + }}, + {"OMSetDepthBounds(NaN, 0.5)", true, match, + [](Env& e) { + Ref list1; + if (FAILED(e.list->QueryInterface(IID_PPV_ARGS(&list1.p)))) return false; + list1->OMSetDepthBounds(std::numeric_limits::quiet_NaN(), 0.5f); + return true; + }}, {"ExecuteBundle after post-close state calls on bundle", true, match, CALLS(const FLOAT factor[4] = {0.25f, 0.5f, 0.75f, 1.0f}; e.closed_bundle->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); diff --git a/runtime/test/d3d12_com_test.cpp b/runtime/test/d3d12_com_test.cpp index 8691e5a740..cf1c71890d 100644 --- a/runtime/test/d3d12_com_test.cpp +++ b/runtime/test/d3d12_com_test.cpp @@ -9,8 +9,10 @@ #include "../src/gfx10_descriptors.h" #include +#include #include #include +#include #define EXPECT(condition) do { \ if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ @@ -136,6 +138,9 @@ int main(int argc, char** argv) { Ref list8; EXPECT(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list8.p)))); list8->OMSetFrontAndBackStencilRef(1, 2); + list8->OMSetDepthBounds(0.25f, 0.75f); + float min_depth = 0.0f, max_depth = 0.0f; + EXPECT(rt::d3d12::depth_bounds(list8.p, min_depth, max_depth) && min_depth == 0.25f && max_depth == 0.75f); Ref bundle_allocator; Ref bundle; EXPECT(SUCCEEDED(rt::d3d12::create_command_allocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, @@ -153,10 +158,12 @@ int main(int argc, char** argv) { IID_PPV_ARGS(&wrong_bundle.p)) == E_INVALIDARG); EXPECT(!wrong_bundle.p); bundle->OMSetFrontAndBackStencilRef(7, 9); + bundle->OMSetDepthBounds(0.125f, 0.875f); EXPECT(bundle->Close() == S_OK); list8->ExecuteBundle(bundle.p); UINT front = 0, back = 0; EXPECT(rt::d3d12::front_back_stencil_ref(list8.p, front, back) && front == 7 && back == 9); + EXPECT(rt::d3d12::depth_bounds(list8.p, min_depth, max_depth) && min_depth == 0.125f && max_depth == 0.875f); // An untouched bundle inherits the parent's current sticky state; its default values must not overwrite it. Ref untouched_allocator; @@ -168,6 +175,7 @@ int main(int argc, char** argv) { EXPECT(untouched_bundle->Close() == S_OK); list8->ExecuteBundle(untouched_bundle.p); EXPECT(rt::d3d12::front_back_stencil_ref(list8.p, front, back) && front == 7 && back == 9); + EXPECT(rt::d3d12::depth_bounds(list8.p, min_depth, max_depth) && min_depth == 0.125f && max_depth == 0.875f); list8->ExecuteBundle(bundle.p); // the same closed bundle can be executed repeatedly ID3D12CommandList* bundle_submission[] = {bundle.p}; @@ -432,26 +440,32 @@ int main(int argc, char** argv) { list->ClearDepthStencilView(dsv_handle, D3D12_CLEAR_FLAG_DEPTH, 2.0f, 0, 0, nullptr); EXPECT(list->Close() == S_OK); - // State-setting calls on a closed direct list or bundle are ignored and do not mutate recorded state. + // Post-close state calls on a closed direct list or bundle are ignored and do not mutate recorded state. EXPECT(SUCCEEDED(allocator->Reset())); EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr))); + list8->OMSetDepthBounds(std::numeric_limits::quiet_NaN(), 0.5f); list->OMSetStencilRef(5); + list8->OMSetDepthBounds(0.25f, 0.75f); EXPECT(list->Close() == S_OK); EXPECT(rt::d3d12::front_back_stencil_ref(list8.p, front, back) && front == 5 && back == 5); + EXPECT(rt::d3d12::depth_bounds(list8.p, min_depth, max_depth) && min_depth == 0.25f && max_depth == 0.75f); const FLOAT post_close_factor[4] = {0.1f, 0.2f, 0.3f, 0.4f}; list->OMSetStencilRef(99); list->OMSetBlendFactor(post_close_factor); + list8->OMSetDepthBounds(0.9f, 0.95f); list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); list->IASetIndexBuffer(nullptr); list->SetPipelineState(nullptr); list->ClearState(nullptr); list->ExecuteBundle(bundle.p); EXPECT(rt::d3d12::front_back_stencil_ref(list8.p, front, back) && front == 5 && back == 5); + EXPECT(rt::d3d12::depth_bounds(list8.p, min_depth, max_depth) && min_depth == 0.25f && max_depth == 0.75f); EXPECT(list->Close() == E_FAIL); // Post-close state calls on a closed bundle do not append calls or invalidate the bundle. bundle->OMSetStencilRef(99); bundle->OMSetBlendFactor(post_close_factor); + bundle->OMSetDepthBounds(0.9f, 0.95f); bundle->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_LINELIST); bundle->IASetIndexBuffer(nullptr); bundle->SetPipelineState(nullptr); @@ -461,6 +475,7 @@ int main(int argc, char** argv) { EXPECT(SUCCEEDED(list->Reset(allocator.p, nullptr))); list->ExecuteBundle(bundle.p); EXPECT(rt::d3d12::front_back_stencil_ref(list8.p, front, back) && front == 7 && back == 9); + EXPECT(rt::d3d12::depth_bounds(list8.p, min_depth, max_depth) && min_depth == 0.125f && max_depth == 0.875f); EXPECT(list->Close() == S_OK); } diff --git a/runtime/test/d3d12_mesh_test.cpp b/runtime/test/d3d12_mesh_test.cpp index 2d9bc5212e..7ba970b1f7 100644 --- a/runtime/test/d3d12_mesh_test.cpp +++ b/runtime/test/d3d12_mesh_test.cpp @@ -59,6 +59,7 @@ using DepthStencilSubobject = Subobject; using Rasterizer2Subobject = Subobject; using MaskSubobject = Subobject; +using DsvFormatSubobject = Subobject; // Subobjects appended one after another: each is pointer-aligned and a multiple of the pointer size, so the bytes // are what a struct of the same members would hold. @@ -157,6 +158,7 @@ int main(int argc, char** argv) { info.queue_family = d.family; info.buffer_device_address = true; info.shader_dir = library.string(); + info.features.depthBounds = d.enabled.depthBounds; info.mesh_shader = mesh; info.mesh_shader_queries = mesh && d.mesh_shader_queries; info.pipeline_statistics_query = d.pipeline_statistics_query; @@ -317,7 +319,14 @@ int main(int argc, char** argv) { D3D12_DEPTH_STENCIL_DESC1 bounds{}; bounds.DepthBoundsTestEnable = TRUE; bad.add(DepthStencil1Subobject{.inner = bounds}); - EXPECT(create(device.p, bad, refused) == E_NOTIMPL && !refused.p); + D3D12_FEATURE_DATA_D3D12_OPTIONS2 o2{}; + EXPECT(SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS2, &o2, sizeof(o2)))); + if (o2.DepthBoundsTestSupported) { + 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); + } // A console stream: its numbering has no gap at 23, so its mesh shader is 24 (the PC's AS) and its // RASTERIZER1 26 (the PC's DEPTH_STENCIL2). An AS without an MS is no PC stream, so the layer reads it with the @@ -710,6 +719,104 @@ int main(int argc, char** argv) { std::printf("pipelineStatisticsQuery not available: the D3D-291 statistics check is skipped\n"); } + // RT-DEPTHBOUNDS-STREAM-EXTRAS: a console stream is realized on first use (PipelineState::resolve_type), not + // when it is created, and that path must carry DepthBoundsTestEnable like the eager one. The mesh shader + // writes depth 0. With the bounds on, OMSetDepthBounds(0.5, 1) must discard it (the target keeps its clear + // colour) and OMSetDepthBounds(0, 1) must keep it (red). A realization that drops the flag draws red twice. + if (!pc_only && o2.DepthBoundsTestSupported) { + D3D12_DEPTH_STENCIL_DESC1 bounded{}; + bounded.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ZERO; + bounded.DepthFunc = D3D12_COMPARISON_FUNC_ALWAYS; + bounded.DepthBoundsTestEnable = TRUE; + Stream console_bounds; + console_bounds.add(RootSubobject{.inner = root.p}); + console_bounds.add(Subobject{.inner = ms}); + console_bounds.add(PsSubobject{.inner = ps}).add(TargetsSubobject{.inner = one_target()}); + console_bounds.add(DepthStencil1Subobject{.inner = bounded}); + console_bounds.add(DsvFormatSubobject{.inner = DXGI_FORMAT_D32_FLOAT}); + console_bounds.add(MaskSubobject{.inner = UINT_MAX}); + Ref bounds_console; + using CreateConsole = HRESULT(WINAPI*)(ID3D12Device*, const D3D12_PIPELINE_STATE_STREAM_DESC*, REFIID, void**); + const auto bounds_desc = console_bounds.desc(); + EXPECT(SUCCEEDED(reinterpret_cast(slot(device.p, 86))(device.p, &bounds_desc, + IID_PPV_ARGS(&bounds_console.p))) && + bounds_console.p); + + Ref bounds_target, bounds_depth; + D3D12_RESOURCE_DESC bounds_rt_desc = rt_desc; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &bounds_rt_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&bounds_target.p)))); + D3D12_RESOURCE_DESC depth_desc = rt_desc; + depth_desc.Format = DXGI_FORMAT_D32_FLOAT; + depth_desc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; + EXPECT(SUCCEEDED(device->CreateCommittedResource(&default_heap, D3D12_HEAP_FLAG_NONE, &depth_desc, + D3D12_RESOURCE_STATE_COMMON, nullptr, + IID_PPV_ARGS(&bounds_depth.p)))); + const D3D12_DESCRIPTOR_HEAP_DESC dsv_heap_desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 1, + D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; + Ref dsv_heap, bounds_rtv_heap; + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&dsv_heap_desc, IID_PPV_ARGS(&dsv_heap.p)))); + EXPECT(SUCCEEDED(device->CreateDescriptorHeap(&heap_desc, IID_PPV_ARGS(&bounds_rtv_heap.p)))); + const D3D12_CPU_DESCRIPTOR_HANDLE dsv{ + SIZE_T(reinterpret_cast(slot(dsv_heap.p, 9))(dsv_heap.p))}; + const D3D12_CPU_DESCRIPTOR_HANDLE bounds_rtv{ + SIZE_T(reinterpret_cast(slot(bounds_rtv_heap.p, 9))(bounds_rtv_heap.p))}; + device->CreateDepthStencilView(bounds_depth.p, nullptr, dsv); + device->CreateRenderTargetView(bounds_target.p, nullptr, bounds_rtv); + + // One frame per bounds pair; returns the pixel at (8, 8), inside the quad the mesh draw covers. + bool first_frame = true; + UINT64 bounds_fence = 3; + const auto bounded_pixel = [&](float lo, float hi) { + std::array pixel{}; + if (!SUCCEEDED(allocator->Reset()) || !SUCCEEDED(list->Reset(allocator.p, bounds_console.p))) return pixel; + D3D12_RESOURCE_BARRIER in[2] = { + transition(bounds_target.p, first_frame ? D3D12_RESOURCE_STATE_COMMON : D3D12_RESOURCE_STATE_COPY_SOURCE, + D3D12_RESOURCE_STATE_RENDER_TARGET), + transition(bounds_depth.p, first_frame ? D3D12_RESOURCE_STATE_COMMON : D3D12_RESOURCE_STATE_DEPTH_WRITE, + D3D12_RESOURCE_STATE_DEPTH_WRITE)}; + list->ResourceBarrier(first_frame ? 2 : 1, in); + first_frame = false; + list->ClearRenderTargetView(bounds_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, &bounds_rtv, FALSE, &dsv); + list6->OMSetDepthBounds(lo, hi); + list->SetGraphicsRoot32BitConstants(0, 2, origin, 0); + list->SetGraphicsRootConstantBufferView(1, upload_va); + list6->DispatchMesh(2, 2, 1); + D3D12_RESOURCE_BARRIER out = transition(bounds_target.p, D3D12_RESOURCE_STATE_RENDER_TARGET, + D3D12_RESOURCE_STATE_COPY_SOURCE); + list->ResourceBarrier(1, &out); + D3D12_TEXTURE_COPY_LOCATION to{}; + to.pResource = readback.p; + to.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; + to.PlacedFootprint = {0, {DXGI_FORMAT_R8G8B8A8_UNORM, size, size, 1, 256}}; + D3D12_TEXTURE_COPY_LOCATION from{}; + from.pResource = bounds_target.p; + from.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; + list->CopyTextureRegion(&to, 0, 0, 0, &from, nullptr); + if (list->Close() != S_OK) return pixel; + ID3D12CommandList* one[] = {list.p}; + queue->ExecuteCommandLists(1, one); + if (rt::d3d12::last_execute_result(queue.p) != leonida::ReplayResult::success) return pixel; + ++bounds_fence; + if (FAILED(queue->Signal(fence.p, bounds_fence)) || FAILED(fence->SetEventOnCompletion(bounds_fence, nullptr))) + return pixel; + void* got = nullptr; + if (FAILED(readback->Map(0, nullptr, &got))) return pixel; + std::memcpy(pixel.data(), static_cast(got) + 8 * 256 + 8 * 4, 4); + readback->Unmap(0, nullptr); + return pixel; + }; + const std::array red{255, 0, 0, 255}, clear_blue{0, 0, 255, 255}; + EXPECT(bounded_pixel(0.0f, 1.0f) == red); // depth 0 is inside [0, 1] + EXPECT(bounded_pixel(0.5f, 1.0f) == clear_blue); // outside [0.5, 1]: discarded only if the flag arrived + } + for (auto* r : {&upload, &readback, &target}) r->reset(); for (auto* p : {&mesh_pso, &vs_pso, &refused}) p->reset(); root.reset(); diff --git a/runtime/test/d3d12_vulkan_device_test.cpp b/runtime/test/d3d12_vulkan_device_test.cpp index 9df462d3fd..4c4f239c1a 100644 --- a/runtime/test/d3d12_vulkan_device_test.cpp +++ b/runtime/test/d3d12_vulkan_device_test.cpp @@ -2423,7 +2423,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.CastingFullyTypedFormatSupported || @@ -5095,6 +5095,7 @@ int main() { info.queue_family = d.family; info.conditional_rendering = d.conditional_rendering; // SetPredication (the D24 tests check copies under it) info.texture_compression_bc = d.bc; + info.features.depthBounds = d.enabled.depthBounds; info.occlusion_query_precise = d.occlusion_query_precise; info.pipeline_statistics_query = d.pipeline_statistics_query; info.conditional_rendering = d.conditional_rendering; diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index fd63fca665..8c9124567d 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -490,6 +490,7 @@ class Device { enable_legacy_formatless_features && features.features.shaderStorageImageWriteWithoutFormat; enable.logicOp = features.features.logicOp; // d3d12_draw_test's logic-op draw, where available enable.multiViewport = features.features.multiViewport; // d3d12_multi_viewport_test, where available + enable.depthBounds = features.features.depthBounds; enable.geometryShader = features.features.geometryShader; // d3d12_geometry_test, where available enable.dualSrcBlend = features.features.dualSrcBlend; // d3d12_translated_draw_test --dual-source const char* conditional_extension = VK_EXT_CONDITIONAL_RENDERING_EXTENSION_NAME;