From 673dd15d0ed6cf41428d1931601a69e9f2233ee1 Mon Sep 17 00:00:00 2001 From: sodove Date: Tue, 24 Nov 2026 11:47:58 +0500 Subject: [PATCH 1/2] d3d12: use legal Vulkan depth clear values (D3D-CLEAR-DEPTH-RANGE) --- docs/d3d12_vulkan.md | 3 ++ runtime/CMakeLists.txt | 3 +- runtime/shims/d3d12_com.h | 1 + runtime/shims/d3d12_vulkan_device.cpp | 3 ++ runtime/src/vulkan_backend.h | 5 ++++ runtime/src/vulkan_replay.h | 12 +++++--- runtime/test/d3d12_clear_test.cpp | 42 +++++++++++++++++---------- runtime/test/vk_test_device.h | 7 +++++ 8 files changed, 55 insertions(+), 21 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 8bf2bd38ea..3eb0fbe371 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2645,6 +2645,9 @@ The trace will enumerate them. stores `0x7fff` vs WARP `0x8000` (all on `main` before this change; same 1-ulp class as SRGB, not addressed here). `D32_FLOAT` and `D32_FLOAT_S8X24_UINT` match WARP. This does not add typed-view reinterpretation of typeless resources. +- **Depth clears** use `VK_EXT_depth_range_unrestricted` when the Vulkan device supports it, allowing floating-point + depth outside `[0, 1]` and NaN to reach the driver for both full and rectangle clears. Without the extension, replay + clamps the clear depth to `[0, 1]` and uses zero for NaN. The UNORM depth format conversions still apply. - `D3D12XBOX_RESOURCE_FLAG_FORCE_TEXTURE_COMPATIBILITY` (public release note) and DCC: ignore the flag, let the Vulkan driver choose compression, and fail loudly if the game aliases a buffer over a compressed image or reads raw DCC metadata. diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index a23b79290f..ac84677954 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2247,7 +2247,8 @@ if(TARGET Vulkan::Headers) add_test(NAME d3d12_rgb9e5_rtv COMMAND d3d12_clear_test --rgb9e5) set_tests_properties(d3d12_rgb9e5_rtv PROPERTIES SKIP_RETURN_CODE 77) add_test(NAME d3d12_clear COMMAND d3d12_clear_test) - set_tests_properties(d3d12_clear PROPERTIES SKIP_RETURN_CODE 77) + add_test(NAME d3d12_clear_restricted_depth COMMAND d3d12_clear_test --restricted-depth) + set_tests_properties(d3d12_clear d3d12_clear_restricted_depth PROPERTIES SKIP_RETURN_CODE 77) # Synthetic typed-view readbacks against Windows WARP; run explicitly on a Vulkan GPU. add_executable(d3d12_typeless_rtv_test test/d3d12_typeless_rtv_test.cpp) diff --git a/runtime/shims/d3d12_com.h b/runtime/shims/d3d12_com.h index c0ff3cf8da..07233494bf 100644 --- a/runtime/shims/d3d12_com.h +++ b/runtime/shims/d3d12_com.h @@ -87,6 +87,7 @@ struct VulkanDeviceInfo { std::vector compute_queues; std::vector copy_queues; bool separate_depth_stencil_layouts = true; + bool depth_range_unrestricted = false; // VK_EXT_depth_range_unrestricted enabled on this device. std::uint32_t max_push_constants_size = 0; bool push_constants_need_fallback = false; // maxBufferSize and maxMemoryAllocationSize, which cap the GPU address arena (src/arena_size.h). 0: read from diff --git a/runtime/shims/d3d12_vulkan_device.cpp b/runtime/shims/d3d12_vulkan_device.cpp index 97ed334a11..e9bc191952 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5121,6 +5121,7 @@ class Device final info.xdxr_acceleration_structures && info.buffer_device_address); state_.context->mesh_queries = info.mesh_shader_queries; + state_.context->set_depth_range_unrestricted(info.depth_range_unrestricted); if (info.physical && state_.format_properties) { state_.context->set_color_attachment_format( [physical = info.physical, get_props = state_.format_properties](VkFormat format) { @@ -9735,6 +9736,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin return std::any_of(extensions.begin(), extensions.end(), [&](const auto& e) { return std::strcmp(e.extensionName, name) == 0; }); }; + info.depth_range_unrestricted = has_device_extension(VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME); VkPhysicalDeviceFragmentShaderBarycentricFeaturesKHR barycentric{ VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FRAGMENT_SHADER_BARYCENTRIC_FEATURES_KHR}; if (has_device_extension(VK_KHR_FRAGMENT_SHADER_BARYCENTRIC_EXTENSION_NAME)) { @@ -9911,6 +9913,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin device_info.pEnabledFeatures = &enable; std::vector device_extensions; v13.add_extensions(device_extensions); + if (info.depth_range_unrestricted) device_extensions.push_back(VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME); if (info.conservative_rasterization) device_extensions.push_back(VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME); if (info.image_view_min_lod) device_extensions.push_back(VK_EXT_IMAGE_VIEW_MIN_LOD_EXTENSION_NAME); if (info.vertex_attribute_divisor) device_extensions.push_back(divisor_extension); diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index f90dd6a3ff..d73b8dccaa 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -1285,6 +1285,11 @@ class VulkanContext { replay_.zero_size = size; } + void set_depth_range_unrestricted(bool enabled) { + std::unique_lock lock(resource_mutex_); + replay_.depth_range_unrestricted = enabled; + } + // Physical-device COLOR_ATTACHMENT format check for reinterpreted UAV clears. void set_color_attachment_format(std::function check) { std::unique_lock lock(resource_mutex_); diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index bda530a7a6..22b93db138 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -814,6 +814,7 @@ struct ReplayFunctions { PFN_vkCmdPipelineBarrier pipeline_barrier = nullptr; PFN_vkCmdClearColorImage clear_color_image = nullptr; PFN_vkCmdClearDepthStencilImage clear_depth_stencil_image = nullptr; + bool depth_range_unrestricted = false; // Allows out-of-range float depth clear values. // Optional; a list that needs a missing one is rejected with ReplayResult::unsupported. PFN_vkCmdPipelineBarrier2 pipeline_barrier2 = nullptr; // enhanced barriers VkPipelineStageFlags2 queue_stages = 0; @@ -3654,10 +3655,13 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c const VulkanAccess state = vulkan_access(d3d12_state::depth_write, false); auto pieces = detail::current(tracker, id, range, image_layout(p.binding, state.layout)); detail::initialize(vk, command_buffer, p.binding, id, pieces, image_layout(p.binding, state.layout), tracker); - const float depth = p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h - : p.binding.format == VK_FORMAT_D16_UNORM - ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm - : depth_clear->depth; + float depth = p.binding.depth24 ? depth24_clear_value(depth_clear->depth) // see depth24.h + : p.binding.format == VK_FORMAT_D16_UNORM + ? detail::quantize_unorm(depth_clear->depth, 65535.0) // see quantize_unorm + : depth_clear->depth; + // Without the extension both clear commands require a depth in [0, 1], including for float images. + if (!vk.depth_range_unrestricted) + depth = std::isnan(depth) ? 0.0f : std::clamp(depth, 0.0f, 1.0f); const VkClearDepthStencilValue value{depth, depth_clear->stencil}; if (p.rects.empty()) { detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), diff --git a/runtime/test/d3d12_clear_test.cpp b/runtime/test/d3d12_clear_test.cpp index 46c467ca9e..abc3b0bd79 100644 --- a/runtime/test/d3d12_clear_test.cpp +++ b/runtime/test/d3d12_clear_test.cpp @@ -6,7 +6,9 @@ #include "vk_test_device.h" #include "../shims/d3d12_com.h" #include +#include #include +#include #include #include #include @@ -375,10 +377,9 @@ bool check_unlisted_rtvs(ID3D12Device* device) { } // namespace // ClearDepthStencilView values at the edge of what D3D12 accepts: on a D32_FLOAT target a depth outside [0, 1] and a -// NaN are stored exactly as given, and flags that name no plane clear nothing (the target keeps the value of the clear -// before it). WARP shows both in the read-back, and the layer must show the same texels; a call the layer refuses fails -// Close() inside clear_depth_and_read. -bool check_depth_clear_edge_values(ID3D12Device* host, ID3D12Device* ours) { +// NaN are stored exactly as given by WARP; flags that name no plane clear nothing. On Vulkan the float values +// can be preserved with unrestricted depth. Also check the legal [0, 1] clear values on a restricted device. +bool check_depth_clear_edge_values(ID3D12Device* host, ID3D12Device* ours, bool unrestricted_depth) { constexpr UINT width = 5, height = 3; const auto read = [](ID3D12Device* device, FLOAT depth_full, FLOAT depth_part, bool part_no_flags, std::vector& depth) { @@ -402,34 +403,40 @@ bool check_depth_clear_edge_values(ID3D12Device* host, ID3D12Device* ours) { std::memcpy(&warp_value, expected.data() + at, 4); std::fprintf(stderr, "%s (%u,%u): WARP cleared %g, expected %g\n", what, x, y, warp_value, want); ++wrong; - } else if (std::memcmp(got.data() + at, expected.data() + at, 4)) { - FLOAT warp_value, layer_value; - std::memcpy(&warp_value, expected.data() + at, 4); + } + FLOAT want_layer = want; + if (!unrestricted_depth) + want_layer = std::isnan(want) ? 0.0f : std::clamp(want, 0.0f, 1.0f); + if (std::memcmp(got.data() + at, &want_layer, 4)) { + FLOAT layer_value; std::memcpy(&layer_value, got.data() + at, 4); - std::fprintf(stderr, "%s (%u,%u): layer cleared %g, WARP %g\n", what, x, y, layer_value, - warp_value); + std::fprintf(stderr, "%s (%u,%u): layer cleared %g, expected %g\n", what, x, y, layer_value, + want_layer); ++wrong; } } if (wrong) return false; - std::printf("%s: %ux%u depth texels match WARP\n", what, width, height); + std::printf("%s: %ux%u depth texels matched (%s)\n", what, width, height, + unrestricted_depth ? "WARP parity" : "restricted Vulkan depth"); return true; }; - if (!compare("depth 1.5 and -0.5 are stored as given", 1.5f, -0.5f, false, 1.5f, -0.5f)) return false; + if (!compare("depth clear with 1.5 and -0.5", 1.5f, -0.5f, false, 1.5f, -0.5f)) return false; // NaN is likewise not clamped away; WARP stores the quiet NaN it was given. const FLOAT nan = std::numeric_limits::quiet_NaN(); - if (!compare("a NaN depth stays NaN", nan, nan, false, nan, nan)) return false; + if (!compare("depth clear with NaN", nan, nan, false, nan, nan)) return false; return compare("flags 0 leaves the target alone", 0.25f, 0.75f, true, 0.25f, 0.25f); } int main(int argc, char** argv) { const bool volume_only = argc == 2 && std::strcmp(argv[1], "--volume") == 0; const bool rgb9e5_only = argc == 2 && std::strcmp(argv[1], "--rgb9e5") == 0; - if (argc != 1 && !volume_only && !rgb9e5_only) { - std::fprintf(stderr, "usage: d3d12_clear_test [--volume|--rgb9e5]\n"); + const bool restricted_depth = argc == 2 && std::strcmp(argv[1], "--restricted-depth") == 0; + if (argc != 1 && !volume_only && !rgb9e5_only && !restricted_depth) { + std::fprintf(stderr, "usage: d3d12_clear_test [--volume|--rgb9e5|--restricted-depth]\n"); return 2; } vktest::Device vk; + vk.request_depth_range_unrestricted = !restricted_depth && !volume_only && !rgb9e5_only; const auto device_status = vk.initialize(); if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips if (device_status != vktest::DeviceStatus::ready) return 1; @@ -444,6 +451,7 @@ int main(int argc, char** argv) { info.device = vk.device; info.queue = vk.queue; info.queue_family = vk.family; + info.depth_range_unrestricted = vk.depth_range_unrestricted; if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1; if (rgb9e5_only) { D3D12_FEATURE_DATA_FORMAT_SUPPORT fs{DXGI_FORMAT_R9G9B9E5_SHAREDEXP}; @@ -470,10 +478,12 @@ int main(int argc, char** argv) { std::fprintf(stderr, "WARP reference unavailable: differential test cannot run\n"); return 1; } + if (restricted_depth) + return check_depth_clear_edge_values(host.p, ours.p, false) ? 0 : 1; if (!check_volume_rtvs(host.p, ours.p)) return 1; if (volume_only) return 0; if (!check_unlisted_rtvs(ours.p)) return 1; - if (!check_depth_clear_edge_values(host.p, ours.p)) return 1; + if (!check_depth_clear_edge_values(host.p, ours.p, vk.depth_range_unrestricted)) return 1; const float nan = std::numeric_limits::quiet_NaN(), inf = std::numeric_limits::infinity(); // Integer-format values: negatives, fractions, NaN and overflow exercise the numeric float->int conversion. const std::array int_values[] = { @@ -677,7 +687,7 @@ int main(int argc, char** argv) { static_cast(validation_errors)); if (validation_errors) return 1; if (compare_failed) return compare_failed; - if (volume_only || rgb9e5_only) return 0; + if (volume_only || rgb9e5_only || restricted_depth) return 0; std::printf("RTV and DSV clears: %u full/rectangle readbacks matched WARP\n", cases_checked); return 0; } diff --git a/runtime/test/vk_test_device.h b/runtime/test/vk_test_device.h index 678d369e28..f1083231b1 100644 --- a/runtime/test/vk_test_device.h +++ b/runtime/test/vk_test_device.h @@ -106,6 +106,8 @@ class Device { bool storage_buffer_8bit = false; // storageBuffer8BitAccess enabled bool conditional_rendering = false; // VK_EXT_conditional_rendering enabled, where available bool host_image_copy = false; // VK_EXT_host_image_copy and hostImageCopy enabled + bool request_depth_range_unrestricted = false; + bool depth_range_unrestricted = false; // VK_EXT_depth_range_unrestricted enabled // create(true) only: VK_KHR (else EXT) swapchain_maintenance1 and its feature enabled, with the surface half on the // instance, so a vsync swapchain can switch to IMMEDIATE or FIFO_RELAXED per present. bool swapchain_maintenance1 = false; @@ -571,6 +573,11 @@ class Device { enabled = enable; std::vector device_extensions; v13_.add_extensions(device_extensions); + depth_range_unrestricted = request_depth_range_unrestricted && + std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { + return std::strcmp(e.extensionName, VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME) == 0; + }); + if (depth_range_unrestricted) device_extensions.push_back(VK_EXT_DEPTH_RANGE_UNRESTRICTED_EXTENSION_NAME); conservative_rasterization = std::any_of(extensions.begin(), extensions.end(), [](const auto& e) { return std::strcmp(e.extensionName, VK_EXT_CONSERVATIVE_RASTERIZATION_EXTENSION_NAME) == 0; }); From 86b069f1d953d3422435b08e9fd9302e3de9104c Mon Sep 17 00:00:00 2001 From: sodove Date: Tue, 24 Nov 2026 13:16:10 +0500 Subject: [PATCH 2/2] docs: record observed D3D12 depth clear semantics --- docs/d3d12_vulkan.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 3eb0fbe371..5fad2d84cb 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2645,9 +2645,11 @@ The trace will enumerate them. stores `0x7fff` vs WARP `0x8000` (all on `main` before this change; same 1-ulp class as SRGB, not addressed here). `D32_FLOAT` and `D32_FLOAT_S8X24_UINT` match WARP. This does not add typed-view reinterpretation of typeless resources. -- **Depth clears** use `VK_EXT_depth_range_unrestricted` when the Vulkan device supports it, allowing floating-point - depth outside `[0, 1]` and NaN to reach the driver for both full and rectangle clears. Without the extension, replay - clamps the clear depth to `[0, 1]` and uses zero for NaN. The UNORM depth format conversions still apply. +- **Depth clears.** Microsoft documents a `[0, 1]` clamp. On Windows 11 build 26100, both WARP and native D3D12 on + an RTX 5070 Ti (610.74) preserved finite out-of-range `D32_FLOAT` values and quiet NaN in full and rectangle + clears. Replay preserves those values when the Vulkan device has enabled `VK_EXT_depth_range_unrestricted`. + With the extension disabled, replay clamps finite values to `[0, 1]` and uses zero for NaN. The UNORM depth + format conversions still apply. [D3D12 reference](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-id3d12graphicscommandlist-cleardepthstencilview) - `D3D12XBOX_RESOURCE_FLAG_FORCE_TEXTURE_COMPATIBILITY` (public release note) and DCC: ignore the flag, let the Vulkan driver choose compression, and fail loudly if the game aliases a buffer over a compressed image or reads raw DCC metadata.