From daf31b820b5b9fe5d7d119836657bff1f8fd1ea0 Mon Sep 17 00:00:00 2001 From: OreoFR <29048345+OreoFR@users.noreply.github.com> Date: Wed, 25 Nov 2026 15:48:26 +0100 Subject: [PATCH] replay: copies between depth formats that differ go through transfer scratch (RT-DEPTH-FORMAT-COPY-STAGED) A CopyTextureRegion of the depth plane from a D32_SFLOAT_S8_UINT image into a D32_SFLOAT one was replayed as vkCmdCopyImage. Vulkan requires depth/stencil formats to match exactly (VUID-vkCmdCopyImage-srcImage-01548), and the NVIDIA driver (RTX 4070 Laptop, 621.40) wrote past the end of the D32 image: the world-start WRITE_INVALID device loss, with the fault at the first byte after a 2560x1440 D32_SFLOAT image (VK_EXT_device_address_binding_report) and this VUID reported by the validation layer just before the loss. depth_format_pair: two depth formats with the same depth encoding but not the same VkFormat (D32 / D32S8, D16 / D16S8, X8_D24 / D24S8) take the image -> scratch -> image path depth <-> colour and A8 copies already use; the depth aspect has the same texel layout in a buffer on both sides. replay_copy_offset_test: the D32S8 -> D32 and D32 -> D32S8 depth-plane copies are staged (no vkCmdCopyImage, depth aspect, 4 bytes a texel), and D32 -> D32 keeps vkCmdCopyImage. Fails without the change (6 checks). Co-Authored-By: Claude Opus 5.5 --- docs/d3d12_vulkan.md | 7 +++ runtime/src/vulkan_replay.h | 24 +++++++- runtime/test/replay_copy_offset_test.cpp | 74 +++++++++++++++++++++++- 3 files changed, 103 insertions(+), 2 deletions(-) diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md index aa7913060a..c2e36319e2 100644 --- a/docs/d3d12_vulkan.md +++ b/docs/d3d12_vulkan.md @@ -484,6 +484,13 @@ What is implemented (all of it testable without the game): `E_INVALIDARG` for such a render target, since no other format can view its image. Without that format, render targets are refused as before; depth-stencil and UAV creation are unsupported for this format. + - A `CopyTextureRegion` of the depth plane between two depth formats with the same depth encoding but not the + same `VkFormat` (`D32_SFLOAT_S8_UINT` and `D32_SFLOAT`, `D16_UNORM_S8_UINT` and `D16_UNORM`, + `D24_UNORM_S8_UINT` and `X8_D24_UNORM_PACK32`) goes image → transfer scratch → image too, depth aspect on both + sides (`vulkan_replay.h`, `depth_format_pair`; `replay_copy_offset_test`). `vkCmdCopyImage` requires + 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. - 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 e20a291057..0caf3bbc19 100644 --- a/runtime/src/vulkan_replay.h +++ b/runtime/src/vulkan_replay.h @@ -548,6 +548,27 @@ inline bool a8_mixed_backing(VkFormat a, VkFormat b) { #endif } +// Two depth formats whose depth aspect has the same encoding but which are not the same VkFormat: D32_SFLOAT and +// D32_SFLOAT_S8_UINT, D16_UNORM and D16_UNORM_S8_UINT, X8_D24_UNORM_PACK32 and D24_UNORM_S8_UINT. D3D12 copies the +// depth plane between them; Vulkan's vkCmdCopyImage requires depth/stencil formats to match exactly +// (VUID-vkCmdCopyImage-srcImage-01548), and an NVIDIA driver handed D32_SFLOAT_S8_UINT -> D32_SFLOAT writes past the +// end of the D32 image (the world-start WRITE_INVALID). The replay stages these through a buffer, where the depth +// aspect has the same texel layout on both sides. +inline bool depth_format_pair(VkFormat a, VkFormat b) { + const auto encoding = [](VkFormat format) -> int { + switch (format) { + case VK_FORMAT_D32_SFLOAT: + case VK_FORMAT_D32_SFLOAT_S8_UINT: return 32; + case VK_FORMAT_X8_D24_UNORM_PACK32: + case VK_FORMAT_D24_UNORM_S8_UINT: return 24; + case VK_FORMAT_D16_UNORM: + case VK_FORMAT_D16_UNORM_S8_UINT: return 16; + default: return 0; + } + }; + return a != b && encoding(a) && encoding(a) == encoding(b); +} + inline bool depth_colour_pair(const ResourceBinding& a, const ResourceBinding& b) { const auto family = [](VkFormat format) -> int { switch (format) { @@ -3627,7 +3648,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun } 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))) { + a8_mixed_backing(p.source.format, p.binding.format) || + depth_format_pair(p.source.format, p.binding.format))) { // 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 diff --git a/runtime/test/replay_copy_offset_test.cpp b/runtime/test/replay_copy_offset_test.cpp index 515befb3cd..6fb26718ed 100644 --- a/runtime/test/replay_copy_offset_test.cpp +++ b/runtime/test/replay_copy_offset_test.cpp @@ -49,8 +49,12 @@ VKAPI_ATTR void VKAPI_CALL rec_copy_i2b(VkCommandBuffer, VkImage, VkImageLayout, for (std::uint32_t i = 0; i < count; ++i) i2b_copies.push_back(regions[i]); } +std::vector image_copies; + VKAPI_ATTR void VKAPI_CALL noop_copy_image(VkCommandBuffer, VkImage, VkImageLayout, VkImage, VkImageLayout, - std::uint32_t, const VkImageCopy*) {} + std::uint32_t count, const VkImageCopy* regions) { + for (std::uint32_t i = 0; i < count; ++i) image_copies.push_back(regions[i]); +} template T handle(std::uintptr_t n) { @@ -138,6 +142,25 @@ int main(int argc, char** argv) { rgb96_img.samples = 1; rgb96_img.rgb96 = true; + // A depth-stencil texture and a depth-only one of the same size, both 32-bit float depth (D32_FLOAT_S8X24 and + // D32_FLOAT in D3D12). + ResourceBinding d32s8_img{}; + d32s8_img.image = handle(6); + d32s8_img.format = VK_FORMAT_D32_SFLOAT_S8_UINT; + d32s8_img.aspects = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT; + d32s8_img.width = 64; + d32s8_img.height = 32; + d32s8_img.mip_levels = 1; + d32s8_img.array_layers = 1; + d32s8_img.depth = 1; + d32s8_img.samples = 1; + ResourceBinding d32_img = d32s8_img; + d32_img.image = handle(7); + d32_img.format = VK_FORMAT_D32_SFLOAT; + d32_img.aspects = VK_IMAGE_ASPECT_DEPTH_BIT; + ResourceBinding d32_img2 = d32_img; + d32_img2.image = handle(8); + const auto resolve = [&](ResourceId id) -> const ResourceBinding* { switch (id) { case 1: return &src_buf; @@ -145,6 +168,9 @@ int main(int argc, char** argv) { case 3: return &rgba_img; case 4: return &d24_img; case 5: return &rgb96_img; + case 6: return &d32s8_img; + case 7: return &d32_img; + case 8: return &d32_img2; default: return nullptr; } }; @@ -155,6 +181,7 @@ int main(int argc, char** argv) { transfer_writes_ordered = false; b2i_copies.clear(); i2b_copies.clear(); + image_copies.clear(); depth24_jobs.clear(); return replay(list, VK_NULL_HANDLE, resolve, vk); }; @@ -440,6 +467,51 @@ int main(int argc, char** argv) { dst_buf = saved_dst; } + // 7. CopyTextureRegion of the depth plane between two depth formats with the same encoding but not the same + // VkFormat (D32_SFLOAT_S8_UINT and D32_SFLOAT): vkCmdCopyImage requires depth/stencil formats to match exactly + // (VUID-vkCmdCopyImage-srcImage-01548), and an NVIDIA driver handed the D32S8 -> D32 pair wrote past the end of the + // D32 image (the world-start WRITE_INVALID). The copy goes image -> transfer scratch -> image, depth aspect on + // both sides, 4 bytes a texel. The same format on both sides keeps vkCmdCopyImage. + { + VkDeviceSize scratch_bytes = 0; + vk.copy_scratch = [&](VkDeviceSize bytes, VkBuffer& out, VkDeviceSize& base) { + scratch_bytes = bytes; + out = handle(0x9100); + base = 256; + return true; + }; + const auto staged_depth = [&](ResourceId dst, ResourceId src, const char* what) { + 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(), what); + expect(run(list) == ReplayResult::success, what); + expect(image_copies.empty(), "a depth copy between different depth formats does not use vkCmdCopyImage"); + expect(i2b_copies.size() == 1 && b2i_copies.size() == 1, "the depth plane is staged through a buffer"); + if (i2b_copies.size() == 1 && b2i_copies.size() == 1) { + const VkBufferImageCopy &read = i2b_copies[0], &write = b2i_copies[0]; + expect(read.imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT && + write.imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT, + "both staged halves name the depth aspect"); + expect(read.imageExtent.width == 64 && read.imageExtent.height == 32 && read.imageExtent.depth == 1 && + write.imageExtent.width == 64 && write.imageExtent.height == 32, + "the staged depth copy covers the whole 64x32 plane"); + expect(read.bufferOffset == 256 && write.bufferOffset == 256, + "the read and the write share one scratch range"); + } + expect(scratch_bytes >= VkDeviceSize(64) * 32 * 4, "the scratch holds 4 bytes a depth texel"); + }; + 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"); + + 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"); + expect(run(same) == ReplayResult::success, "D32_SFLOAT into D32_SFLOAT replays"); + expect(image_copies.size() == 1 && i2b_copies.empty() && b2i_copies.empty(), + "the same depth format on both sides keeps vkCmdCopyImage"); + vk.copy_scratch = {}; + } + if (failures) return 1; std::printf("replay_copy_offset: CopyResource and CopyTexture apply ResourceBinding::buffer_offset\n"); return 0;