diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index c2e36319e2..65fd8f5b9c 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -491,6 +491,10 @@ What is implemented (all of it testable without the game): depth/stencil formats to match exactly (`VUID-vkCmdCopyImage-srcImage-01548`), and an NVIDIA driver (RTX 4070 Laptop, 621.40) handed the `D32_SFLOAT_S8_UINT` → `D32_SFLOAT` pair wrote past the end of the `D32_SFLOAT` image: the world-start `WRITE_INVALID` device loss, with the fault at the first byte after the image. + - The stencil plane between two depth-stencil formats (8 bits in each) is staged the same way. A depth plane whose + encodings differ (`D24_UNORM_S8_UINT` into `D16_UNORM` or `D32_SFLOAT`), or a multisampled pair, has no Vulkan copy: + that one copy is left out with a log line and the list replays (`depth_stencil_copy`). The layer can meet one, + since the console's D16S8 is made as D24S8 on NVIDIA; no title has been seen asking for it. - reserved resources and `GetResourceTiling` (section 4.2); - query heaps for occlusion, timestamps and pipeline statistics, and `GetTimestampFrequency` on the queue (section 4.6); diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h index 0caf3bbc19..eae98ae43f 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -569,6 +569,23 @@ inline bool depth_format_pair(VkFormat a, VkFormat b) { return a != b && encoding(a) && encoding(a) == encoding(b); } +// What becomes of an image copy that keeps its aspect and has a depth-stencil format on both sides. vkCmdCopyImage takes +// it only between equal formats, whichever aspect it names (VUID-vkCmdCopyImage-srcImage-01548). Between different ones +// a depth plane of one encoding (depth_format_pair) and the stencil plane (8 bits in every format) have one texel layout +// in a buffer and are staged. Any other pair is left out: D24_UNORM_S8_UINT into D16_UNORM or D32_SFLOAT would be a +// conversion, which a D3D12 copy never is, and a multisampled image has no buffer copy. +enum class DepthStencilCopy { direct, staged, left_out }; +inline DepthStencilCopy depth_stencil_copy(const ResourceBinding& source, const ResourceBinding& destination, + VkImageAspectFlags aspects) { + constexpr VkImageAspectFlags depth_stencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; + if (!(aspects & depth_stencil) || source.format == destination.format) return DepthStencilCopy::direct; + const bool one_layout = aspects == VK_IMAGE_ASPECT_STENCIL_BIT || + (aspects == VK_IMAGE_ASPECT_DEPTH_BIT && + depth_format_pair(source.format, destination.format)); + return one_layout && source.samples == 1 && destination.samples == 1 ? DepthStencilCopy::staged + : DepthStencilCopy::left_out; +} + inline bool depth_colour_pair(const ResourceBinding& a, const ResourceBinding& b) { const auto family = [](VkFormat format) -> int { switch (format) { @@ -1311,6 +1328,7 @@ struct Prepared { std::vector tile_rows; std::vector image_copies; bool same_copy_subresource = false; + bool copy_left_out = false; // an image copy no Vulkan command can make (depth_stencil_copy) VkImageResolve resolve{}; // resolves // Depth or stencil resolves: the mode, and the destination view (`view` is the source's). VkResolveModeFlagBits resolve_mode = VK_RESOLVE_MODE_NONE; @@ -3646,10 +3664,27 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun } else { result = detail::prepare_resource_copy(p); } + p.copy_left_out = false; + auto depth_copy = DepthStencilCopy::direct; + if (result == ReplayResult::success && !p.image_copies.empty() && + p.image_copies[0].srcSubresource.aspectMask == p.image_copies[0].dstSubresource.aspectMask) { + VkImageAspectFlags aspects = 0; + for (const VkImageCopy& c : p.image_copies) aspects |= c.srcSubresource.aspectMask; + depth_copy = depth_stencil_copy(p.source, p.binding, aspects); + } + if (depth_copy == DepthStencilCopy::left_out) { + static std::atomic logged{0}; + if (logged.fetch_add(1, std::memory_order_relaxed) < 8) + std::fprintf(stderr, "[rt] replay: a copy from depth-stencil VkFormat %d into %d (%u and %u samples) " + "was left out: no Vulkan copy makes it\n", + int(p.source.format), int(p.binding.format), unsigned(p.source.samples), + unsigned(p.binding.samples)); + p.image_copies.clear(); + p.copy_left_out = true; + } if (result == ReplayResult::success && !p.image_copies.empty() && (p.image_copies[0].srcSubresource.aspectMask != p.image_copies[0].dstSubresource.aspectMask || - a8_mixed_backing(p.source.format, p.binding.format) || - depth_format_pair(p.source.format, p.binding.format))) { + a8_mixed_backing(p.source.format, p.binding.format) || depth_copy == DepthStencilCopy::staged)) { // Depth <-> colour: vkCmdCopyImage cannot mix aspects, so each region goes image -> scratch -> image, // tightly packed. Depth aspects want 4-byte buffer offsets; 16 covers every texel size here. The two // backings of A8_UNORM take the same route: one byte per texel on both sides, but Vulkan does not @@ -4671,7 +4706,9 @@ inline void emit_commands(const PreparedList& prepared, VkCommandBuffer command_ std::holds_alternative(command)) { if constexpr (Checkpoints) checkpoints->record(command_buffer, i, CheckpointKind::copy); const auto [dst_id, src_id] = copy_ids(command); - if (!p.predicated_copy.empty()) { + if (p.copy_left_out) { + // prepare() logged it. + } else if (!p.predicated_copy.empty()) { end_statistics(); vk.emit_predicated_copy(command_buffer, p.predicated_copy); begin_statistics(); diff --git a/runtime/test/replay_copy_offset_test.cpp b/runtime/test/replay_copy_offset_test.cpp index 6fb26718ed..edd6439831 100644 --- a/runtime/test/replay_copy_offset_test.cpp +++ b/runtime/test/replay_copy_offset_test.cpp @@ -161,8 +161,22 @@ int main(int argc, char** argv) { ResourceBinding d32_img2 = d32_img; d32_img2.image = handle(8); + ResourceBinding d24s8_img = d32s8_img; // D24_UNORM_S8_UINT, which the console's D16S8 is made as on NVIDIA + d24s8_img.image = handle(9); + d24s8_img.format = VK_FORMAT_D24_UNORM_S8_UINT; + ResourceBinding d16_img = d32_img; + d16_img.image = handle(10); + d16_img.format = VK_FORMAT_D16_UNORM; + ResourceBinding d32s8_ms = d32s8_img, d32_ms = d32_img; + d32s8_ms.image = handle(11); + d32_ms.image = handle(12); + d32s8_ms.samples = d32_ms.samples = 4; const auto resolve = [&](ResourceId id) -> const ResourceBinding* { switch (id) { + case 9: return &d24s8_img; + case 10: return &d16_img; + case 11: return &d32s8_ms; + case 12: return &d32_ms; case 1: return &src_buf; case 2: return &dst_buf; case 3: return &rgba_img; @@ -503,6 +517,31 @@ int main(int argc, char** argv) { staged_depth(7, 6, "D32_SFLOAT_S8_UINT depth plane into D32_SFLOAT"); staged_depth(6, 7, "D32_SFLOAT into the depth plane of D32_SFLOAT_S8_UINT"); + // Depth planes of different encodings, and a multisampled pair: no Vulkan copy makes them, so nothing is + // recorded for that one command and the list still replays. + const std::pair left_out[] = {{10, 9}, {7, 9}, {10, 6}, {9, 7}, {12, 11}}; + for (const auto& [dst, src] : left_out) { + const CopyLocation to{dst, false, 0, 0, 0, 0, 0, 0}, from{src, false, 0, 0, 0, 0, 0, 0}; + CommandList list; + expect(list.copy_texture({to, 0, 0, 0, from, false, {}}) && list.close(), "record a depth conversion"); + expect(run(list) == ReplayResult::success, "a depth copy that is left out does not fail the list"); + expect(image_copies.empty() && i2b_copies.empty() && b2i_copies.empty(), + "a depth copy between different encodings (or multisampled formats) records no copy"); + } + // The stencil plane (subresource 1 of these single-mip images) is 8 bits in both formats. + { + const CopyLocation to{6, false, 1, 0, 0, 0, 0, 0}, from{9, false, 1, 0, 0, 0, 0, 0}; + CommandList list; + expect(list.copy_texture({to, 0, 0, 0, from, false, {}}) && list.close(), "record a stencil plane copy"); + expect(run(list) == ReplayResult::success, "D24S8 stencil into D32S8 stencil replays"); + expect(image_copies.empty() && i2b_copies.size() == 1 && b2i_copies.size() == 1 && + i2b_copies[0].imageSubresource.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT && + b2i_copies[0].imageSubresource.aspectMask == VK_IMAGE_ASPECT_STENCIL_BIT, + "a stencil plane copy between different formats is staged, stencil aspect on both sides"); + expect(scratch_bytes >= VkDeviceSize(64) * 32 && scratch_bytes < VkDeviceSize(64) * 32 * 2, + "the stencil scratch holds one byte a texel"); + } + const CopyLocation to{8, false, 0, 0, 0, 0, 0, 0}, from{7, false, 0, 0, 0, 0, 0, 0}; CommandList same; expect(same.copy_texture({to, 0, 0, 0, from, false, {}}) && same.close(), "record D32_SFLOAT copy");