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
6 changes: 5 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2679,7 +2679,11 @@ mapped stages/accesses, and if no single layout serves all uses, use `GENERAL` a
- Enhanced barriers (`D3D12_BARRIER_SYNC_*`, `D3D12_BARRIER_ACCESS_*`, `D3D12_BARRIER_LAYOUT_*`) map directly to
`VkPipelineStageFlags2`, `VkAccessFlags2` and `VkImageLayout` (`vulkan_sync`, `vulkan_barrier_access`,
`vulkan_layout`). Queue-specific layouts map like their generic forms; ray tracing, video and split scopes use
`ALL_COMMANDS`; `LayoutBefore = UNDEFINED` or the discard flag becomes `oldLayout = UNDEFINED`.
`ALL_COMMANDS`; `LayoutBefore = UNDEFINED` or the discard flag becomes `oldLayout = UNDEFINED`. `SYNC_COPY` is
`COPY | CLEAR`, not `COPY` alone: `WriteBufferImmediate` is recorded as `vkCmdUpdateBuffer` and
`ResolveQueryData`'s never-written queries as `vkCmdFillBuffer`, which synchronization2 runs in the `CLEAR` stage
(D3D-838; `vk_command_replay_test` part K is a read-after-write hazard under synchronization validation without
it).
- `DiscardResource` (D3D-26): each discarded image subresource gets a barrier from `oldLayout = UNDEFINED` to the
layout the tracker already has for it, so the driver may drop the contents while later commands see no layout
change. The other cases are no-ops, which "contents undefined" allows: buffers, discards with rectangles (only
Expand Down
5 changes: 4 additions & 1 deletion runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ inline VkPipelineStageFlags2 vulkan_sync(std::uint32_t sync) {
VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT},
{y::render_target, VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT},
{y::compute_shading, VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT},
{y::copy, VK_PIPELINE_STAGE_2_COPY_BIT},
// CLEAR as well: two D3D12 copy-scope commands are recorded as Vulkan clear commands, WriteBufferImmediate
// as vkCmdUpdateBuffer and ResolveQueryData's never-written queries as vkCmdFillBuffer, and synchronization2
// puts both in the CLEAR stage, not COPY.
{y::copy, VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT},
{y::resolve, VK_PIPELINE_STAGE_2_RESOLVE_BIT},
{y::execute_indirect, VK_PIPELINE_STAGE_2_DRAW_INDIRECT_BIT},
{y::all_shading, pre_raster | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT},
Expand Down
8 changes: 6 additions & 2 deletions runtime/test/d3d12_state_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,10 @@ int main() {
CHECK(vulkan_sync(y::split) == VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT);
CHECK(vulkan_sync(y::render_target) == VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT);
CHECK(vulkan_sync(y::copy | y::pixel_shading) ==
(VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT));
(VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT | VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT));
// SYNC_COPY covers WriteBufferImmediate (vkCmdUpdateBuffer) and ResolveQueryData's zero fill (vkCmdFillBuffer),
// which synchronization2 runs in the CLEAR stage, not COPY (D3D-838).
CHECK(vulkan_sync(y::copy) == (VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT));
CHECK(vulkan_sync(y::raytracing) == VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT);
CHECK(vulkan_barrier_access(a::no_access) == VK_ACCESS_2_NONE);
const auto cb_scope = vulkan_barrier_scope(y::pixel_shading, a::constant_buffer);
Expand All @@ -427,7 +430,8 @@ int main() {
widened = scope(y::clear_unordered_access_view, a::copy_source); // CLEAR only writes
CHECK(widened.stages & VK_PIPELINE_STAGE_2_ALL_TRANSFER_BIT);
widened = scope(y::copy, a::unordered_access);
CHECK(widened.stages == (VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_PRE_RASTERIZATION_SHADERS_BIT |
CHECK(widened.stages == (VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_CLEAR_BIT |
VK_PIPELINE_STAGE_2_PRE_RASTERIZATION_SHADERS_BIT |
VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT | VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT));
// Pairs Vulkan already accepts are left exactly as vulkan_sync / vulkan_barrier_access map them.
const std::pair<std::uint32_t, std::uint32_t> unchanged[] = {
Expand Down
77 changes: 76 additions & 1 deletion runtime/test/vk_command_replay_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,81 @@ int part_j(vktest::Device& d) {
return 0;
}

// Part K (D3D-838): an enhanced barrier with SYNC_COPY orders a WriteBufferImmediate before it. The write is recorded
// as vkCmdUpdateBuffer, which synchronization2 runs in the CLEAR stage, not COPY, and a DEFAULT-mode write gets no
// barrier of its own (replay leaves it to the application's). So: the write puts a word in src, a SYNC_COPY ->
// SYNC_COPY buffer barrier (COPY_DEST -> COPY_SOURCE) follows, and CopyBufferRegion copies the word to dst. Under
// LEONIDA_VK_VALIDATION=1 synchronization validation reports the read-after-write hazard when the barrier's source
// stages leave CLEAR out; in every run the copied word is checked.
int part_k(vktest::Device& d) {
constexpr VkDeviceSize bytes = 256;
constexpr std::uint64_t va_src = 0x50000;
constexpr std::uint32_t value = 0x5eed1234;
constexpr VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
void *src_mapped = nullptr, *dst_mapped = nullptr;
const VkBuffer src = d.buffer(bytes, usage, &src_mapped);
const VkBuffer dst = d.buffer(bytes, usage, &dst_mapped);
EXPECT(src && dst && src_mapped && dst_mapped);
std::memset(src_mapped, 0, bytes);
std::memset(dst_mapped, 0, bytes);

constexpr ResourceId src_id = 50, dst_id = 51;
ResourceBinding src_binding{}, dst_binding{};
src_binding.buffer = src;
src_binding.size = bytes;
src_binding.va = va_src;
dst_binding.buffer = dst;
dst_binding.size = bytes;
const auto resolve = [&](ResourceId id) -> const ResourceBinding* {
if (id == src_id) return &src_binding;
if (id == dst_id) return &dst_binding;
return nullptr;
};
ReplayFunctions vk{};
vk.pipeline_barrier = d.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier");
vk.pipeline_barrier2 = d.fn<PFN_vkCmdPipelineBarrier2>("vkCmdPipelineBarrier2");
vk.copy_buffer = d.fn<PFN_vkCmdCopyBuffer>("vkCmdCopyBuffer"); // replay wants all four copy functions
vk.copy_buffer_to_image = d.fn<PFN_vkCmdCopyBufferToImage>("vkCmdCopyBufferToImage");
vk.copy_image_to_buffer = d.fn<PFN_vkCmdCopyImageToBuffer>("vkCmdCopyImageToBuffer");
vk.copy_image = d.fn<PFN_vkCmdCopyImage>("vkCmdCopyImage");
vk.update_buffer = d.fn<PFN_vkCmdUpdateBuffer>("vkCmdUpdateBuffer");
vk.buffer_at = [&](std::uint64_t va, std::uint64_t length, VkBuffer& buffer, VkDeviceSize& offset) {
if (va < va_src || va - va_src >= bytes || length > bytes - (va - va_src)) return false;
buffer = src;
offset = va - va_src;
return true;
};

namespace ac = d3d12_access;
namespace y = d3d12_sync;
CommandList list;
EXPECT(list.write_immediate({va_src + 8, value, write_immediate_mode::normal}));
EXPECT(list.barrier(BufferBarrier{y::copy, y::copy, ac::copy_dest, ac::copy_source, src_id}));
EXPECT(list.copy_buffer({dst_id, 16, src_id, 8, 4}));
EXPECT(list.close());

const unsigned errors_before = d.validation_errors();
ReplayResult result = ReplayResult::success;
EXPECT(d.run([&](VkCommandBuffer cb) {
result = replay(list, cb, resolve, vk);
if (result != ReplayResult::success) std::fprintf(stderr, "K: replay returned %d\n", int(result));
return result == ReplayResult::success;
}));
const unsigned errors = d.validation_errors() - errors_before;
std::uint32_t copied = 0;
std::memcpy(&copied, static_cast<const std::uint8_t*>(dst_mapped) + 16, 4);
if (copied != value) {
std::fprintf(stderr, "K: copied 0x%08x, want 0x%08x\n", copied, value);
++g_mismatches;
}
if (errors) {
std::fprintf(stderr, "K: %u validation errors: a SYNC_COPY barrier does not cover vkCmdUpdateBuffer\n", errors);
return 1;
}
std::printf("K: SYNC_COPY orders WriteBufferImmediate (vkCmdUpdateBuffer, CLEAR stage) before a copy\n");
return 0;
}

} // namespace

int main() {
Expand All @@ -1276,7 +1351,7 @@ int main() {
}
EXPECT(status == vktest::DeviceStatus::ready);
if (part_a(device) || part_b(device) || part_c(device) || part_d(device) || part_e(device) || part_f(device) ||
part_g(device) || part_h(device) || part_i(device) || part_j(device))
part_g(device) || part_h(device) || part_i(device) || part_j(device) || part_k(device))
return 1;
if (g_mismatches) return 1;
if (device.validation_errors()) {
Expand Down
Loading