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
3 changes: 3 additions & 0 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -2657,6 +2657,9 @@ The trace will enumerate them.

### 4.3 Barriers and layouts

Plain buffer copies order earlier transfer writes on the same queue before reading or writing the next range.
Set `LEONIDA_ORDER_BUFFER_COPIES=0` to omit this dependency when comparing replay behavior.

Track a current state/layout per subresource. D3D12 states are bitmasks; a combined state takes the union of the
mapped stages/accesses, and if no single layout serves all uses, use `GENERAL` and log it. Legacy
`D3D12_RESOURCE_STATE_*` maps to a (stage, access, layout) triple:
Expand Down
23 changes: 23 additions & 0 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <array>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <limits>
#include <map>
Expand Down Expand Up @@ -3475,6 +3476,20 @@ inline void close_labels(VkCommandBuffer command_buffer, const ReplayFunctions&
// `open_labels` counts the debug labels begun and not yet ended in this command buffer: an EndEvent with none open
// is dropped (Vulkan requires a matching begin), and the caller ends what is still open before it ends the buffer
// (close_labels). Without it, the list's own events are balanced at its end.
inline bool order_buffer_copies() {
static const bool enabled = [] {
#ifdef _WIN32
char value[2]{};
std::size_t length = 0;
return getenv_s(&length, value, sizeof value, "LEONIDA_ORDER_BUFFER_COPIES") != 0 || length != 2 || value[0] != '0';
#else
const char* value = std::getenv("LEONIDA_ORDER_BUFFER_COPIES");
return !value || value[0] != '0' || value[1];
#endif
}();
return enabled;
}

inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, const ReplayFunctions& vk,
LayoutTracker* tracker = nullptr, std::uint32_t* open_labels = nullptr) {
std::uint32_t own_labels = 0;
Expand Down Expand Up @@ -3774,6 +3789,14 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
vk.emit_predicated_copy(command_buffer, p.predicated_copy);
begin_statistics();
} else if (!p.buffer_copies.empty()) {
// Transfer dependency from natterman12, ubiquitarianism/leonida PR #354 (GPL-3.0).
// D3D12 orders buffer copies even across lists on one queue. A previous transfer write can
// overlap this copy's source or destination without an application barrier.
if (vk.pipeline_barrier && order_buffer_copies()) {
detail::memory_barrier(vk, command_buffer, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT);
}
vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()),
p.buffer_copies.data());
} else if (p.staged) {
Expand Down
43 changes: 38 additions & 5 deletions runtime/test/replay_copy_offset_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <limits>
#include <vector>

Expand All @@ -15,13 +16,27 @@ std::vector<VkBufferImageCopy> b2i_copies;
std::vector<VkBufferImageCopy> i2b_copies;
std::vector<leonida::Depth24Job> depth24_jobs;

VKAPI_ATTR void VKAPI_CALL noop_barrier(VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags, VkDependencyFlags,
std::uint32_t, const VkMemoryBarrier*, std::uint32_t,
const VkBufferMemoryBarrier*, std::uint32_t, const VkImageMemoryBarrier*) {}
// Whether a barrier since the last buffer copy ordered earlier transfer writes before transfer reads and writes, and
// that answer for each vkCmdCopyBuffer in turn: D3D12 orders back-to-back copies, Vulkan needs the barrier.
bool transfer_writes_ordered = false;
std::vector<bool> buffer_copy_ordered;

VKAPI_ATTR void VKAPI_CALL rec_barrier(VkCommandBuffer, VkPipelineStageFlags src_stages, VkPipelineStageFlags dst_stages,
VkDependencyFlags, std::uint32_t memory_count, const VkMemoryBarrier* memory,
std::uint32_t, const VkBufferMemoryBarrier*, std::uint32_t,
const VkImageMemoryBarrier*) {
const VkAccessFlags after = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_TRANSFER_WRITE_BIT;
for (std::uint32_t i = 0; i < memory_count; ++i)
if ((src_stages & VK_PIPELINE_STAGE_TRANSFER_BIT) && (dst_stages & VK_PIPELINE_STAGE_TRANSFER_BIT) &&
(memory[i].srcAccessMask & VK_ACCESS_TRANSFER_WRITE_BIT) && (memory[i].dstAccessMask & after) == after)
transfer_writes_ordered = true;
}

VKAPI_ATTR void VKAPI_CALL rec_copy_buffer(VkCommandBuffer, VkBuffer, VkBuffer, std::uint32_t count,
const VkBufferCopy* regions) {
for (std::uint32_t i = 0; i < count; ++i) buffer_copies.push_back(regions[i]);
buffer_copy_ordered.push_back(transfer_writes_ordered);
transfer_writes_ordered = false;
}

VKAPI_ATTR void VKAPI_CALL rec_copy_b2i(VkCommandBuffer, VkBuffer, VkImage, VkImageLayout, std::uint32_t count,
Expand Down Expand Up @@ -52,11 +67,12 @@ void expect(bool ok, const char* what) {

} // namespace

int main() {
int main(int argc, char** argv) {
using namespace leonida;
const bool expect_ordered = !(argc == 2 && std::strcmp(argv[1], "--expect-unordered") == 0);

ReplayFunctions vk{};
vk.pipeline_barrier = noop_barrier;
vk.pipeline_barrier = rec_barrier;
vk.copy_buffer = rec_copy_buffer;
vk.copy_buffer_to_image = rec_copy_b2i;
vk.copy_image_to_buffer = rec_copy_i2b;
Expand Down Expand Up @@ -135,6 +151,8 @@ int main() {

const auto run = [&](const CommandList& list) {
buffer_copies.clear();
buffer_copy_ordered.clear();
transfer_writes_ordered = false;
b2i_copies.clear();
i2b_copies.clear();
depth24_jobs.clear();
Expand All @@ -149,6 +167,21 @@ int main() {
expect(buffer_copies.size() == 1 && buffer_copies[0].srcOffset == src_buf.buffer_offset &&
buffer_copies[0].dstOffset == dst_buf.buffer_offset && buffer_copies[0].size == src_buf.size,
"CopyResource applies src and dst buffer_offset");
expect(buffer_copy_ordered == std::vector<bool>{expect_ordered},
"a buffer copy is ordered after earlier transfer writes (an earlier list on the queue may have written)");
}

// 1b. Back-to-back copies into the same buffer, with no barrier from the application: D3D12 runs them in order,
// so each vkCmdCopyBuffer needs its own barrier after the transfer write before it (one barrier ahead of the
// first copy would leave the second unordered against the first).
{
CommandList list;
expect(list.copy_buffer({2, 0, 1, 0, 256}) && list.copy_buffer({2, 128, 1, 512, 256}) && list.close(),
"record two overlapping CopyBufferRegion calls");
expect(run(list) == ReplayResult::success, "back-to-back buffer copies replay");
expect(buffer_copies.size() == 2, "both buffer copies are emitted");
expect(buffer_copy_ordered == (std::vector<bool>{expect_ordered, expect_ordered}),
"each back-to-back buffer copy is ordered after the transfer write before it");
}

// 2. CopyResource within a single shared backing VkBuffer (arena): disjoint & adjacent succeed, overlap fails.
Expand Down
Loading