From d336fc99708bb704134d7e7951bf186485a7f6fa Mon Sep 17 00:00:00 2001 From: sodove Date: Tue, 24 Nov 2026 11:47:58 +0500 Subject: [PATCH 1/3] 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 3ae29163e5..fe9cea7a28 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2636,6 +2636,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 64e346f40a..5ebfe9b0b9 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2181,7 +2181,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 d0f3b528aa..b66b64e189 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 0273f9339b..9df5132632 100644 --- a/runtime/shims/d3d12_vulkan_device.cpp +++ b/runtime/shims/d3d12_vulkan_device.cpp @@ -5120,6 +5120,7 @@ class Device final info.conditional_rendering, info.ray_tracing, info.xdxr_acceleration_structures && info.buffer_device_address); + 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) { @@ -9723,6 +9724,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)) { @@ -9899,6 +9901,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 e3e4defe89..4ff70b11a1 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h @@ -1268,6 +1268,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 889bb71f72..651060bd38 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -810,6 +810,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; @@ -3615,10 +3616,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 5cccd1836e..97944f863c 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; @@ -567,6 +569,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 76033b01b51bb7033ba2cb3941b0a55fc1951e69 Mon Sep 17 00:00:00 2001 From: sodove Date: Tue, 24 Nov 2026 13:16:10 +0500 Subject: [PATCH 2/3] 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 fe9cea7a28..32e0f42854 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2636,9 +2636,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. From d4dec7a3d78dbcf1e96aa6eaa125eb80045da665 Mon Sep 17 00:00:00 2001 From: bvtterbroot <157503259+bvtterbroot@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:48:57 +0100 Subject: [PATCH 3/3] d3d12/vulkan: write NaN depth clears with a copy (D3D-CLEAR-NAN-DEPTH) The AMD Vulkan driver stores 1.0 for a NaN depth clear value in vkCmdClearDepthStencilImage and vkCmdClearAttachments, even with VK_EXT_depth_range_unrestricted (RX 7900 XTX on 26.11.2, RX 9070 XT on 26.10.1). D3D12 keeps the quiet NaN, so d3d12_clear fails on AMD. For a NaN clear of a D32_FLOAT or D32_FLOAT_S8X24_UINT depth plane, fill transfer scratch with the value's bits and copy it into each cleared rectangle (depth aspect only). The stencil aspect stays a clear. d3d12_clear_test gains D32_S8 cases with NaN and stencil, and checks stencil texels against WARP as well. Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 5 +++ runtime/src/vulkan_replay.h | 75 +++++++++++++++++++++++++++++-- runtime/test/d3d12_clear_test.cpp | 54 +++++++++++++++------- 3 files changed, 114 insertions(+), 20 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index 32e0f42854..c97e1fb501 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -2639,6 +2639,11 @@ The trace will enumerate them. - **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`. + AMD's Vulkan driver (26.11.2 on an RX 7900 XTX, 26.10.1 on an RX 9070 XT) stores `1.0` for a NaN depth clear + value in both `vkCmdClearDepthStencilImage` and `vkCmdClearAttachments`, even with the extension, so a NaN clear + of a `D32_FLOAT` or `D32_FLOAT_S8X24_UINT` depth plane is a `vkCmdFillBuffer` of the value's bits into transfer + scratch and a `vkCmdCopyBufferToImage` into each cleared rectangle, on every vendor (`nan_depth_copy` in + `runtime/src/vulkan_replay.h`); a stencil cleared with it is still a clear. D3D-CLEAR-NAN-DEPTH. 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 diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 651060bd38..c00e6cb378 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -1034,6 +1034,12 @@ struct Prepared { std::vector> fills; // ... buffer byte ranges, offset and size VkBuffer fill_scratch = VK_NULL_HANDLE; // short buffer clears: one repeated 8-byte pattern VkDeviceSize fill_scratch_offset = 0; + // A NaN depth clear of a float depth plane (prepare_nan_depth_clear): scratch filled with the value's bits, then + // copied into each cleared rectangle of the depth plane. + VkBuffer nan_depth = VK_NULL_HANDLE; + VkDeviceSize nan_depth_offset = 0, nan_depth_bytes = 0; + std::uint32_t nan_depth_bits = 0; + std::vector nan_depth_regions; QueryPool* queries = nullptr; // query commands: the heap ComputePipeline pipeline; // dispatches std::uint32_t draw = 0; // draws: its PreparedDraw in PreparedList::draws @@ -2086,6 +2092,46 @@ void round_trip(const ReplayFunctions& vk, VkCommandBuffer command_buffer, const } } +// Whether a depth clear writes NaN into a float depth plane. D3D12 (WARP, native) stores the quiet NaN as given, but +// AMD's Vulkan driver stores 1.0 for a NaN depth clear value in vkCmdClearDepthStencilImage and +// vkCmdClearAttachments, even with VK_EXT_depth_range_unrestricted (RX 9070 XT, RX 7900 XTX). A buffer-to-image copy +// writes the bits as they are, so such a clear is a copy of the value's bits instead (D3D-CLEAR-NAN-DEPTH). Without +// the extension a copy needs [0, 1] too, and the clear stays a clear of zero (see emit). +inline bool nan_depth_copy(const ReplayFunctions& vk, const ResourceBinding& resource, VkImageAspectFlags aspects, + float depth) { + return vk.depth_range_unrestricted && std::isnan(depth) && (aspects & VK_IMAGE_ASPECT_DEPTH_BIT) && + !resource.depth24 && + (resource.format == VK_FORMAT_D32_SFLOAT || resource.format == VK_FORMAT_D32_SFLOAT_S8_UINT); +} + +// The copies of a NaN depth clear: one region per cleared rectangle (the whole mip without rectangles), every one +// reading the same scratch, which holds the value for the largest rectangle (4 bytes a texel, also for D32_S8). +inline ReplayResult prepare_nan_depth_clear(const ReplayFunctions& vk, const ClearDepthStencil& clear, Prepared& p) { + if (!vk.copy_buffer_to_image || !vk.fill_buffer || !vk.copy_scratch) return ReplayResult::unsupported; + std::vector rects = p.rects; + if (rects.empty()) rects.push_back({{{0, 0}, mip_extent(p.binding, clear.target.mip)}, 0, p.range.layerCount}); + VkDeviceSize texels = 0; + for (const VkClearRect& rect : rects) + texels = std::max(texels, VkDeviceSize(rect.rect.extent.width) * rect.rect.extent.height); + if (!texels) return ReplayResult::success; + p.nan_depth_bytes = texels * 4; + if (!vk.copy_scratch(p.nan_depth_bytes, p.nan_depth, p.nan_depth_offset) || !p.nan_depth || + p.nan_depth_offset % 4) + return ReplayResult::unsupported; + std::memcpy(&p.nan_depth_bits, &clear.depth, 4); + for (const VkClearRect& rect : rects) { + if (!rect.rect.extent.width || !rect.rect.extent.height) continue; + VkBufferImageCopy region{}; + region.bufferOffset = p.nan_depth_offset; + region.imageSubresource = {VK_IMAGE_ASPECT_DEPTH_BIT, p.range.baseMipLevel, + p.range.baseArrayLayer + rect.baseArrayLayer, rect.layerCount}; + region.imageOffset = {rect.rect.offset.x, rect.rect.offset.y, 0}; + region.imageExtent = {rect.rect.extent.width, rect.rect.extent.height, 1}; + p.nan_depth_regions.push_back(region); + } + return ReplayResult::success; +} + } // namespace detail namespace detail { @@ -2679,6 +2725,9 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun r != ReplayResult::success) return r; if (!p.skip && p.rects.empty() && !vk.clear_depth_stencil_image) return ReplayResult::unsupported; + if (!p.skip && detail::nan_depth_copy(vk, p.binding, aspects, depth_clear->depth)) + if (const auto r = detail::prepare_nan_depth_clear(vk, *depth_clear, p); r != ReplayResult::success) + return r; } else if (const auto* uav = std::get_if(&command)) { const UavView& view = uav->view; if (!bind(view.resource)) return ReplayResult::unresolved_resource; @@ -3623,12 +3672,30 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c // 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); + VkImageSubresourceRange cleared = p.range; + if (p.nan_depth) { // see nan_depth_copy: the depth plane is written by a copy, any stencil by a clear + detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT, VK_ACCESS_TRANSFER_WRITE_BIT, + VK_PIPELINE_STAGE_TRANSFER_BIT); + vk.fill_buffer(command_buffer, p.nan_depth, p.nan_depth_offset, p.nan_depth_bytes, p.nan_depth_bits); + detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT, + VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); + detail::round_trip(vk, command_buffer, p.binding, pieces, image_layout(p.binding, transfer_dst.layout), + state, transfer_dst.access, transfer_dst.stages, [&] { + vk.copy_buffer_to_image(command_buffer, p.nan_depth, p.binding.image, + image_layout(p.binding, transfer_dst.layout), + std::uint32_t(p.nan_depth_regions.size()), p.nan_depth_regions.data()); + }); + cleared.aspectMask &= ~VkImageAspectFlags(VK_IMAGE_ASPECT_DEPTH_BIT); + if (!cleared.aspectMask) continue; + depth = 0.0f; // the stencil clear below leaves the depth plane alone + } 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), state, transfer_dst.access, transfer_dst.stages, [&] { vk.clear_depth_stencil_image(command_buffer, p.binding.image, - image_layout(p.binding, transfer_dst.layout), &value, 1, &p.range); + image_layout(p.binding, transfer_dst.layout), &value, 1, &cleared); }); continue; } @@ -3643,10 +3710,10 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c VkRenderingInfo rendering{VK_STRUCTURE_TYPE_RENDERING_INFO}; rendering.renderArea = {{0, 0}, detail::mip_extent(p.binding, depth_clear->target.mip)}; rendering.layerCount = depth_clear->target.layer_count; - if (p.range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) rendering.pDepthAttachment = &info; - if (p.range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) rendering.pStencilAttachment = &info; + if (cleared.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) rendering.pDepthAttachment = &info; + if (cleared.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) rendering.pStencilAttachment = &info; vk.begin_rendering(command_buffer, &rendering); - VkClearAttachment attachment_clear{p.range.aspectMask, 0, {}}; + VkClearAttachment attachment_clear{cleared.aspectMask, 0, {}}; attachment_clear.clearValue.depthStencil = value; vk.clear_attachments(command_buffer, 1, &attachment_clear, std::uint32_t(p.rects.size()), p.rects.data()); vk.end_rendering(command_buffer); diff --git a/runtime/test/d3d12_clear_test.cpp b/runtime/test/d3d12_clear_test.cpp index abc3b0bd79..2a23ad53ed 100644 --- a/runtime/test/d3d12_clear_test.cpp +++ b/runtime/test/d3d12_clear_test.cpp @@ -381,23 +381,41 @@ bool check_unlisted_rtvs(ID3D12Device* device) { // 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) { - std::vector stencil; // D32_FLOAT has no stencil plane - return clear_depth_and_read(device, DXGI_FORMAT_D32_FLOAT, 4, false, depth_full, 0, depth_part, 0, depth, - stencil, D3D12_CLEAR_FLAG_DEPTH, D3D12_CLEAR_FLAGS(0), part_no_flags); + // With a stencil plane (D32_FLOAT_S8X24_UINT) both planes are cleared: 0x11 everywhere, then 0x22 in the + // rectangle. A NaN depth is then written by a copy and the stencil by a clear (vulkan_replay.h, nan_depth_copy). + constexpr UINT8 stencil_full = 0x11, stencil_part = 0x22; + const auto read = [](ID3D12Device* device, bool has_stencil, FLOAT depth_full, FLOAT depth_part, + bool part_no_flags, std::vector& depth, std::vector& stencil) { + const D3D12_CLEAR_FLAGS flags = has_stencil ? D3D12_CLEAR_FLAG_DEPTH | D3D12_CLEAR_FLAG_STENCIL + : D3D12_CLEAR_FLAG_DEPTH; + return clear_depth_and_read(device, has_stencil ? DXGI_FORMAT_D32_FLOAT_S8X24_UINT : DXGI_FORMAT_D32_FLOAT, 4, + has_stencil, depth_full, stencil_full, depth_part, stencil_part, depth, stencil, + flags, flags, part_no_flags); }; - const auto compare = [&](const char* what, FLOAT depth_full, FLOAT depth_part, bool part_no_flags, FLOAT outside, - FLOAT inside) { - std::vector expected, got; - if (!read(host, depth_full, depth_part, part_no_flags, expected)) return false; - if (!read(ours, depth_full, depth_part, part_no_flags, got)) return false; + const auto compare = [&](const char* what, bool has_stencil, FLOAT depth_full, FLOAT depth_part, + bool part_no_flags, FLOAT outside, FLOAT inside) { + std::vector expected, got, expected_stencil, got_stencil; + if (!read(host, has_stencil, depth_full, depth_part, part_no_flags, expected, expected_stencil)) return false; + if (!read(ours, has_stencil, depth_full, depth_part, part_no_flags, got, got_stencil)) return false; CHECK(expected.size() == width * height * 4 && got.size() == expected.size()); + CHECK(expected_stencil.size() == (has_stencil ? width * height : 0u) && + got_stencil.size() == expected_stencil.size()); size_t wrong = 0; for (UINT y = 0; y < height; ++y) for (UINT x = 0; x < width; ++x) { const size_t at = (width * y + x) * 4; - const FLOAT want = (x >= 1 && x < 4 && y >= 1) ? inside : outside; + const bool in_rect = x >= 1 && x < 4 && y >= 1; + const FLOAT want = in_rect ? inside : outside; + if (has_stencil) { + const unsigned want_stencil = in_rect && !part_no_flags ? stencil_part : stencil_full; + const unsigned warp_stencil = expected_stencil[width * y + x]; + const unsigned layer_stencil = got_stencil[width * y + x]; + if (warp_stencil != want_stencil || layer_stencil != want_stencil) { + std::fprintf(stderr, "%s (%u,%u): stencil WARP %#x, layer %#x, expected %#x\n", what, x, y, + warp_stencil, layer_stencil, want_stencil); + ++wrong; + } + } if (std::memcmp(expected.data() + at, &want, 4)) { FLOAT warp_value; std::memcpy(&warp_value, expected.data() + at, 4); @@ -416,15 +434,19 @@ bool check_depth_clear_edge_values(ID3D12Device* host, ID3D12Device* ours, bool } } if (wrong) return false; - std::printf("%s: %ux%u depth texels matched (%s)\n", what, width, height, + std::printf("%s: %ux%u depth%s texels matched (%s)\n", what, width, height, has_stencil ? " and stencil" : "", unrestricted_depth ? "WARP parity" : "restricted Vulkan depth"); return true; }; - 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. + if (!compare("depth clear with 1.5 and -0.5", false, 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. AMD's Vulkan driver stores 1.0 for a + // NaN clear value, so the layer copies the value's bits instead (D3D-CLEAR-NAN-DEPTH). const FLOAT nan = std::numeric_limits::quiet_NaN(); - 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); + if (!compare("depth clear with NaN", false, nan, nan, false, nan, nan)) return false; + // The same with a stencil plane: the depth copy must leave the stencil clears alone, full and rectangle. + if (!compare("D32_S8 clear with NaN and stencil", true, nan, nan, false, nan, nan)) return false; + if (!compare("D32_S8 clear with NaN, then 0.5 in the rectangle", true, nan, 0.5f, false, nan, 0.5f)) return false; + return compare("flags 0 leaves the target alone", false, 0.25f, 0.75f, true, 0.25f, 0.25f); } int main(int argc, char** argv) {