Skip to content
Closed
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
10 changes: 10 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2645,6 +2645,16 @@ 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`.
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
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 @@ -2245,7 +2245,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 @@ -9734,6 +9735,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 @@ -9910,6 +9912,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
87 changes: 79 additions & 8 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 @@ -1042,6 +1043,12 @@ struct Prepared {
std::vector<std::array<VkDeviceSize, 2>> 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<VkBufferImageCopy> nan_depth_regions;
QueryPool* queries = nullptr; // query commands: the heap
ComputePipeline pipeline; // dispatches
std::uint32_t draw = 0; // draws: its PreparedDraw in PreparedList::draws
Expand Down Expand Up @@ -2094,6 +2101,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<VkClearRect> 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 {
Expand Down Expand Up @@ -2705,6 +2752,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<ClearUnorderedAccess>(&command)) {
const UavView& view = uav->view;
if (!bind(view.resource)) return ReplayResult::unresolved_resource;
Expand Down Expand Up @@ -3654,16 +3704,37 @@ 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);
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;
}
Expand All @@ -3678,10 +3749,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);
Expand Down
Loading