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
7 changes: 7 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 23 additions & 1 deletion runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
74 changes: 73 additions & 1 deletion runtime/test/replay_copy_offset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<VkImageCopy> 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<typename T>
T handle(std::uintptr_t n) {
Expand Down Expand Up @@ -138,13 +142,35 @@ 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<VkImage>(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<VkImage>(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<VkImage>(8);

const auto resolve = [&](ResourceId id) -> const ResourceBinding* {
switch (id) {
case 1: return &src_buf;
case 2: return &dst_buf;
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;
}
};
Expand All @@ -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);
};
Expand Down Expand Up @@ -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<VkBuffer>(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;
Expand Down
Loading