Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +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.** 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.
Expand Down
3 changes: 2 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions runtime/shims/d3d12_com.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct VulkanDeviceInfo {
std::vector<VkQueue> compute_queues;
std::vector<VkQueue> 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
Expand Down
3 changes: 3 additions & 0 deletions runtime/shims/d3d12_vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -9911,6 +9913,7 @@ HRESULT create_vulkan_device_standalone(REFIID riid, void** out, QueueTestSettin
device_info.pEnabledFeatures = &enable;
std::vector<const char*> 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);
Expand Down
5 changes: 5 additions & 0 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool(VkFormat)> check) {
std::unique_lock lock(resource_mutex_);
Expand Down
12 changes: 8 additions & 4 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
42 changes: 26 additions & 16 deletions runtime/test/d3d12_clear_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#include "vk_test_device.h"
#include "../shims/d3d12_com.h"
#include <dxgi1_4.h>
#include <algorithm>
#include <array>
#include <cmath>
#include <vector>
#include <cstring>
#include <cstdint>
Expand Down Expand Up @@ -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<unsigned char>& depth) {
Expand All @@ -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<float>::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;
Expand All @@ -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};
Expand All @@ -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<float>::quiet_NaN(), inf = std::numeric_limits<float>::infinity();
// Integer-format values: negatives, fractions, NaN and overflow exercise the numeric float->int conversion.
const std::array<float, 4> int_values[] = {
Expand Down Expand Up @@ -677,7 +687,7 @@ int main(int argc, char** argv) {
static_cast<unsigned long long>(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;
}
7 changes: 7 additions & 0 deletions runtime/test/vk_test_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -571,6 +573,11 @@ class Device {
enabled = enable;
std::vector<const char*> 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;
});
Expand Down