// CopyMemoryToMemoryX (CommandList::copy_memory + vulkan_replay.h) unit and GPU replay test. // Part 1 checks recording rules, split-chunk preparation, overlap staging, readback stage tracking, // unresolved/out-of-range graceful skipping, and CommandList::draw(Draw&&) zero-Z mesh draw no-op // without needing a GPU. Part 2 replays CopyMemoryCmd on a real Vulkan device (including split-chunk // and overlapping forward/backward memmove via scratch) and verifies every byte. No game data. #include "vk_test_device.h" #include "../src/vulkan_replay.h" #include #include #include #include #include #define EXPECT(condition) \ do { \ if (!(condition)) { \ std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \ return 1; \ } \ } while (0) namespace { using namespace leonida; template T fake_fn() { return reinterpret_cast(+[]() {}); } template T fake_handle(std::uintptr_t n) { return reinterpret_cast(n); } int test_headless() { // 1. CommandList::copy_memory recording rules. { CommandList list; EXPECT(!list.copy_memory(0, 0x1000, 64)); EXPECT(!list.copy_memory(0x2000, 0, 64)); EXPECT(!list.copy_memory(UINT64_MAX - 16, 0x1000, 64)); EXPECT(!list.copy_memory(0x2000, UINT64_MAX - 16, 64)); EXPECT(list.copy_memory(0x2000, 0x1000, 0)); // zero-size no-op EXPECT(list.copy_memory(0x2000, 0x2000, 64)); // self-copy no-op EXPECT(list.commands().empty()); EXPECT(list.copy_memory(0x2000, 0x1000, 64)); EXPECT(list.commands().size() == 1); EXPECT(list.close()); EXPECT(!list.copy_memory(0x2000, 0x1000, 64)); } // 2. CommandList::draw(Draw&&) skips zero-Z mesh draws just like draw(const Draw&). { CommandList list; Draw mesh_zero_z{}; mesh_zero_z.pipeline = 0x1234; mesh_zero_z.mesh = true; mesh_zero_z.count = 92; mesh_zero_z.instances = 1; mesh_zero_z.first = 0; // Z == 0 EXPECT(list.draw(std::move(mesh_zero_z))); EXPECT(list.commands().empty()); Draw mesh_ok{}; mesh_ok.pipeline = 0x1234; mesh_ok.mesh = true; mesh_ok.count = 92; mesh_ok.instances = 1; mesh_ok.first = 1; EXPECT(list.draw(std::move(mesh_ok))); EXPECT(list.commands().size() == 1); EXPECT(list.close()); } // 3. prepare() for split-chunk, overlap, unresolved, and readback_src_stages. ReplayFunctions vk{}; vk.pipeline_barrier = fake_fn(); vk.copy_buffer = fake_fn(); constexpr std::uint64_t kSrcA = 0x100400000000ull; constexpr std::uint64_t kSrcB = 0x100400001000ull; constexpr std::uint64_t kDstA = 0x100800000000ull; constexpr std::uint64_t kDstB = 0x100800000800ull; constexpr std::uint64_t kOverlap = 0x100900000000ull; const VkBuffer buf_src_a = fake_handle(0x10); const VkBuffer buf_src_b = fake_handle(0x11); const VkBuffer buf_dst_a = fake_handle(0x20); const VkBuffer buf_dst_b = fake_handle(0x21); const VkBuffer buf_overlap = fake_handle(0x30); const VkBuffer buf_scratch = fake_handle(0x40); vk.buffer_at = [&](std::uint64_t va, std::uint64_t& size, VkBuffer& buf, VkDeviceSize& off) { auto hit = [&](std::uint64_t base, std::uint64_t len, VkBuffer b) { if (va >= base && va < base + len) { buf = b; off = va - base; size = std::min(size, len - off); return true; } return false; }; return hit(kSrcA, 0x1000, buf_src_a) || hit(kSrcB, 0x1000, buf_src_b) || hit(kDstA, 0x800, buf_dst_a) || hit(kDstB, 0x1800, buf_dst_b) || hit(kOverlap, 0x2000, buf_overlap); }; bool allow_scratch = true; vk.copy_scratch = [&](VkDeviceSize, VkBuffer& buf, VkDeviceSize& off) { if (!allow_scratch) return false; buf = buf_scratch; off = 0x100; return true; }; const auto resolve = [](ResourceId) -> const ResourceBinding* { return nullptr; }; PreparedList out; // Split-chunk across misaligned source and destination chunk boundaries: 0x1800 bytes -> 3 steps // ([0, 0x800), [0x800, 0x1000), [0x1000, 0x1800)). { CommandList list; EXPECT(list.copy_memory(kDstA, kSrcA, 0x1800) && list.close()); EXPECT(prepare(list, resolve, vk, out) == ReplayResult::success); EXPECT(out.commands.size() == 1); EXPECT(out.commands[0].memory_copies.size() == 3); EXPECT(out.commands[0].memory_copies[0].size == 0x800 && out.commands[0].memory_copies[0].dst == buf_dst_a && out.commands[0].memory_copies[0].src == buf_src_a); EXPECT(out.commands[0].memory_copies[1].size == 0x800 && out.commands[0].memory_copies[1].dst == buf_dst_b && out.commands[0].memory_copies[1].src == buf_src_a); EXPECT(out.commands[0].memory_copies[2].size == 0x800 && out.commands[0].memory_copies[2].dst == buf_dst_b && out.commands[0].memory_copies[2].src == buf_src_b); EXPECT(out.wrote_readback); EXPECT((out.readback_src_stages & (VK_PIPELINE_STAGE_2_COPY_BIT | VK_PIPELINE_STAGE_2_TRANSFER_BIT)) != 0); } // Overlapping same-buffer copy stages through scratch; without scratch it skips cleanly without error. { CommandList list; EXPECT(list.copy_memory(kOverlap + 0x100, kOverlap, 0x400) && list.close()); EXPECT(prepare(list, resolve, vk, out) == ReplayResult::success); EXPECT(out.commands[0].memory_copies.size() == 1 && out.commands[0].memory_copies[0].scratch == buf_scratch && out.commands[0].memory_copies[0].scratch_offset == 0x100); allow_scratch = false; EXPECT(prepare(list, resolve, vk, out) == ReplayResult::success); EXPECT(out.commands[0].skip && out.commands[0].memory_copies.empty()); allow_scratch = true; } // Completely unresolved VA skips cleanly without failing ExecuteCommandLists. { CommandList list; EXPECT(list.copy_memory(0x100999000000ull, 0x100998000000ull, 0x4000) && list.close()); EXPECT(prepare(list, resolve, vk, out) == ReplayResult::success); EXPECT(out.commands[0].skip && out.commands[0].memory_copies.empty() && !out.wrote_readback); } return 0; } int test_gpu() { vktest::Device d; const auto status = d.initialize(); if (status == vktest::DeviceStatus::unavailable) { std::fprintf(stderr, "replay_copy_memory_test: no Vulkan device, skipping GPU part\n"); return 0; } EXPECT(status == vktest::DeviceStatus::ready); constexpr VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; void* src_a_map = nullptr; void* src_b_map = nullptr; void* dst_a_map = nullptr; void* dst_b_map = nullptr; void* overlap_map = nullptr; void* scratch_map = nullptr; const VkBuffer src_a = d.buffer(0x4000, usage, &src_a_map); const VkBuffer src_b = d.buffer(0x4000, usage, &src_b_map); const VkBuffer dst_a = d.buffer(0x2800, usage, &dst_a_map); const VkBuffer dst_b = d.buffer(0x4000, usage, &dst_b_map); const VkBuffer overlap_buf = d.buffer(0x2000, usage, &overlap_map); const VkBuffer scratch_buf = d.buffer(0x4000, usage, &scratch_map); EXPECT(src_a && src_b && dst_a && dst_b && overlap_buf && scratch_buf); auto* src_a_bytes = static_cast(src_a_map); auto* src_b_bytes = static_cast(src_b_map); auto* dst_a_bytes = static_cast(dst_a_map); auto* dst_b_bytes = static_cast(dst_b_map); auto* overlap_bytes = static_cast(overlap_map); for (std::size_t i = 0; i < 0x4000; ++i) { src_a_bytes[i] = static_cast((i * 131 + 17) & 0xff); src_b_bytes[i] = static_cast((i * 73 + 99) & 0xff); } std::memset(dst_a_bytes, 0, 0x2800); std::memset(dst_b_bytes, 0, 0x4000); for (std::size_t i = 0; i < 0x2000; ++i) overlap_bytes[i] = static_cast((i * 37 + 5) & 0xff); std::vector expected_overlap(overlap_bytes, overlap_bytes + 0x2000); constexpr std::uint64_t kVaSrcA = 0x10045db70000ull; constexpr std::uint64_t kVaSrcB = 0x10045db74000ull; constexpr std::uint64_t kVaDstA = 0x100829c80000ull; constexpr std::uint64_t kVaDstB = 0x100829c82800ull; constexpr std::uint64_t kVaOverlap = 0x100900000000ull; ReplayFunctions vk{}; vk.pipeline_barrier = d.fn("vkCmdPipelineBarrier"); vk.copy_buffer = d.fn("vkCmdCopyBuffer"); vk.buffer_at = [&](std::uint64_t va, std::uint64_t& size, VkBuffer& buf, VkDeviceSize& off) { auto hit = [&](std::uint64_t base, std::uint64_t len, VkBuffer b) { if (va >= base && va < base + len) { buf = b; off = va - base; size = std::min(size, len - off); return true; } return false; }; return hit(kVaSrcA, 0x4000, src_a) || hit(kVaSrcB, 0x4000, src_b) || hit(kVaDstA, 0x2800, dst_a) || hit(kVaDstB, 0x4000, dst_b) || hit(kVaOverlap, 0x2000, overlap_buf); }; VkDeviceSize scratch_cursor = 0; vk.copy_scratch = [&](VkDeviceSize bytes, VkBuffer& buf, VkDeviceSize& off) { if (scratch_cursor + bytes > 0x4000) return false; buf = scratch_buf; off = scratch_cursor; scratch_cursor += (bytes + 15) & ~VkDeviceSize(15); return true; }; const auto resolve = [](ResourceId) -> const ResourceBinding* { return nullptr; }; CommandList list; // 1. Cross-chunk linear copy: 0x5000 bytes spanning src_a (0x4000) + src_b (0x1000) into dst_a (0x2800) + dst_b (0x2800). EXPECT(list.copy_memory(kVaDstA, kVaSrcA, 0x5000)); // 2. Overlapping forward copy (dst > src) inside overlap_buf: [0x100, 0x900) <- [0x0, 0x800). EXPECT(list.copy_memory(kVaOverlap + 0x100, kVaOverlap, 0x800)); // 3. Overlapping backward copy (dst < src) inside overlap_buf: [0x1000, 0x1600) <- [0x1100, 0x1700). EXPECT(list.copy_memory(kVaOverlap + 0x1000, kVaOverlap + 0x1100, 0x600)); // 4. Unresolved copy in the same list must skip cleanly without aborting the list. EXPECT(list.copy_memory(0x100ffff00000ull, 0x100fffe00000ull, 0x100)); EXPECT(list.close()); std::memmove(expected_overlap.data() + 0x100, expected_overlap.data(), 0x800); std::memmove(expected_overlap.data() + 0x1000, expected_overlap.data() + 0x1100, 0x600); const bool ok = d.run([&](VkCommandBuffer cb) { return replay(list, cb, resolve, vk) == ReplayResult::success; }); EXPECT(ok); // Verify split-chunk copy bytes. for (std::size_t i = 0; i < 0x2800; ++i) EXPECT(dst_a_bytes[i] == src_a_bytes[i]); for (std::size_t i = 0; i < 0x1800; ++i) EXPECT(dst_b_bytes[i] == src_a_bytes[0x2800 + i]); for (std::size_t i = 0; i < 0x1000; ++i) EXPECT(dst_b_bytes[0x1800 + i] == src_b_bytes[i]); // Verify forward & backward overlapping copies. EXPECT(std::memcmp(overlap_bytes, expected_overlap.data(), 0x2000) == 0); EXPECT(d.validation_errors() == 0); return 0; } } // namespace int main() { if (const int r = test_headless()) return r; if (const int r = test_gpu()) return r; std::printf("replay_copy_memory_test: passed\n"); return 0; }