diff options
| author | lumenpearson <139921639+lumenpearson@users.noreply.github.com> | 2026-11-22 23:25:25 +0300 |
|---|---|---|
| committer | lumenpearson <139921639+lumenpearson@users.noreply.github.com> | 2026-11-22 23:25:25 +0300 |
| commit | f324bfbc71f0eabf9515bcd7c8b7c9e4b89448bf (patch) | |
| tree | 2db4ae6fd5412abbbc3e1894f8bc7ed88c8ae514 /runtime/test/vk_queue_test.cpp | |
| parent | 18380c6d2baf1422109b369860a6e8be5e185b05 (diff) | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
Merge upstream main into SHADER-537
Diffstat (limited to 'runtime/test/vk_queue_test.cpp')
| -rw-r--r-- | runtime/test/vk_queue_test.cpp | 210 |
1 files changed, 210 insertions, 0 deletions
diff --git a/runtime/test/vk_queue_test.cpp b/runtime/test/vk_queue_test.cpp new file mode 100644 index 000000000..84289198c --- /dev/null +++ b/runtime/test/vk_queue_test.cpp | |||
| @@ -0,0 +1,210 @@ | |||
| 1 | // D3D12 copy and compute queues on their own Vulkan queues, with cross-queue fences. No shaders or game data. | ||
| 2 | // | ||
| 3 | // The device gets a queue per D3D12 type (dedicated families when the GPU has them). A copy queue uploads a buffer | ||
| 4 | // and a texture, the way asset streaming does, signals a fence, and the direct queue waits on that fence on the GPU | ||
| 5 | // before reading both back. The compute queue does the same handoff the other way. A copy queue must refuse a | ||
| 6 | // render-target clear. Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error | ||
| 7 | // fails the test. | ||
| 8 | #include "vk_test_device.h" | ||
| 9 | #include "../src/vulkan_backend.h" | ||
| 10 | |||
| 11 | #include <array> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <cstring> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | #define EXPECT(condition) do { \ | ||
| 17 | if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ | ||
| 18 | } while (0) | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | using namespace leonida; | ||
| 23 | using Kind = VulkanQueue::QueueKind; | ||
| 24 | |||
| 25 | const char* flags_text(VkQueueFlags f) { | ||
| 26 | static char buf[32]; | ||
| 27 | std::snprintf(buf, sizeof buf, "%s%s%s", f & VK_QUEUE_GRAPHICS_BIT ? "G" : "", f & VK_QUEUE_COMPUTE_BIT ? "C" : "", | ||
| 28 | f & VK_QUEUE_TRANSFER_BIT ? "T" : ""); | ||
| 29 | return buf; | ||
| 30 | } | ||
| 31 | |||
| 32 | int check_picker() { | ||
| 33 | // Synthetic family layouts: the picker's choices don't depend on the GPU running the test. | ||
| 34 | auto fam = [](VkQueueFlags flags, std::uint32_t granularity = 1) { | ||
| 35 | VkQueueFamilyProperties p{}; | ||
| 36 | p.queueFlags = flags; | ||
| 37 | p.queueCount = 1; | ||
| 38 | p.minImageTransferGranularity = {granularity, granularity, granularity}; | ||
| 39 | return p; | ||
| 40 | }; | ||
| 41 | constexpr VkQueueFlags G = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT; | ||
| 42 | constexpr VkQueueFlags C = VK_QUEUE_COMPUTE_BIT | VK_QUEUE_TRANSFER_BIT; | ||
| 43 | constexpr VkQueueFlags T = VK_QUEUE_TRANSFER_BIT; | ||
| 44 | { | ||
| 45 | const std::array f{fam(G), fam(T), fam(C)}; // the common NVIDIA layout | ||
| 46 | const auto q = pick_queue_families(f, 0); | ||
| 47 | EXPECT(q.direct == 0 && q.compute == 2 && q.copy == 1); | ||
| 48 | } | ||
| 49 | { | ||
| 50 | const std::array f{fam(G)}; // one family for everything (integrated GPUs, llvmpipe) | ||
| 51 | const auto q = pick_queue_families(f, 0); | ||
| 52 | EXPECT(q.direct == 0 && q.compute == 0 && q.copy == 0); | ||
| 53 | } | ||
| 54 | { | ||
| 55 | const std::array f{fam(G), fam(C)}; // no DMA family: copies go to async compute | ||
| 56 | const auto q = pick_queue_families(f, 0); | ||
| 57 | EXPECT(q.compute == 1 && q.copy == 1); | ||
| 58 | } | ||
| 59 | { | ||
| 60 | const std::array f{fam(G), fam(T, 8), fam(C)}; // a coarse DMA family is skipped | ||
| 61 | const auto q = pick_queue_families(f, 0); | ||
| 62 | EXPECT(q.copy == 2); | ||
| 63 | } | ||
| 64 | std::printf("queue family picker: 4 layouts ok\n"); | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | constexpr std::uint32_t kSize = 64 * 1024; | ||
| 69 | constexpr std::uint32_t kTex = 64; // 64x64 RGBA8: row pitch 256, already aligned | ||
| 70 | |||
| 71 | std::uint8_t pattern(std::uint32_t i, std::uint8_t seed) { return std::uint8_t(i * 31 + seed); } | ||
| 72 | |||
| 73 | } // namespace | ||
| 74 | |||
| 75 | int main() { | ||
| 76 | if (check_picker()) return 1; | ||
| 77 | |||
| 78 | vktest::Device d; | ||
| 79 | d.all_queue_types = true; | ||
| 80 | if (!d.create()) { | ||
| 81 | std::printf("SKIP: no usable Vulkan device\n"); | ||
| 82 | return 77; | ||
| 83 | } | ||
| 84 | std::printf("families: direct %u (%s)", d.family, flags_text(d.families[d.family].queueFlags)); | ||
| 85 | std::printf(", compute %u (%s)", d.compute_family, flags_text(d.families[d.compute_family].queueFlags)); | ||
| 86 | std::printf(", copy %u (%s)\n", d.copy_family, flags_text(d.families[d.copy_family].queueFlags)); | ||
| 87 | |||
| 88 | VulkanContext context(d.device_proc, d.device, true); | ||
| 89 | EXPECT(context.loaded()); | ||
| 90 | VulkanQueue direct(context, d.queue, d.family, Kind::direct); | ||
| 91 | VulkanQueue compute(context, d.compute_queue, d.compute_family, Kind::compute); | ||
| 92 | VulkanQueue copy(context, d.copy_queue, d.copy_family, Kind::copy); | ||
| 93 | EXPECT(direct.ok() && compute.ok() && copy.ok()); | ||
| 94 | |||
| 95 | // Buffers: an upload source, a GPU-side destination and a readback, all host-visible for the test. | ||
| 96 | void *upload_map = nullptr, *mid_map = nullptr, *readback_map = nullptr, *tex_readback_map = nullptr; | ||
| 97 | const VkBufferUsageFlags usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; | ||
| 98 | VkBuffer upload = d.buffer(kSize, usage, &upload_map); | ||
| 99 | VkBuffer mid = d.buffer(kSize, usage, &mid_map); | ||
| 100 | VkBuffer readback = d.buffer(kSize, usage, &readback_map); | ||
| 101 | VkBuffer tex_readback = d.buffer(kTex * kTex * 4, usage, &tex_readback_map); | ||
| 102 | VkImage texture = d.image(VK_FORMAT_R8G8B8A8_UNORM, kTex, kTex, 1, VK_IMAGE_USAGE_SAMPLED_BIT); | ||
| 103 | EXPECT(upload && mid && readback && tex_readback && texture); | ||
| 104 | auto* up = static_cast<std::uint8_t*>(upload_map); | ||
| 105 | for (std::uint32_t i = 0; i < kSize; ++i) up[i] = pattern(i, 7); | ||
| 106 | |||
| 107 | const ResourceId upload_id = 1, mid_id = 2, readback_id = 3, tex_id = 4, tex_readback_id = 5; | ||
| 108 | auto add_buffer = [&](ResourceId id, VkBuffer b, VkDeviceSize size) { | ||
| 109 | ResourceBinding r; | ||
| 110 | r.buffer = b; | ||
| 111 | r.size = size; | ||
| 112 | context.add_resource(id, r); | ||
| 113 | }; | ||
| 114 | add_buffer(upload_id, upload, kSize); | ||
| 115 | add_buffer(mid_id, mid, kSize); | ||
| 116 | add_buffer(readback_id, readback, kSize); | ||
| 117 | add_buffer(tex_readback_id, tex_readback, kTex * kTex * 4); | ||
| 118 | ResourceBinding tex; | ||
| 119 | tex.image = texture; | ||
| 120 | tex.format = VK_FORMAT_R8G8B8A8_UNORM; | ||
| 121 | tex.width = kTex; | ||
| 122 | tex.height = kTex; | ||
| 123 | // As the device registers a texture: tracked, still undefined, in D3D12's common state. Its first use is the | ||
| 124 | // copy-queue upload, which can't zero-fill it first. | ||
| 125 | context.add_resource(tex_id, tex, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 126 | |||
| 127 | VulkanFence fence(context, 0); | ||
| 128 | EXPECT(fence.semaphore()); | ||
| 129 | |||
| 130 | // 1. Copy queue: buffer upload and a texture upload from the same staging buffer (footprint copy), then signal. | ||
| 131 | CommandList on_copy; | ||
| 132 | EXPECT(on_copy.copy_buffer({mid_id, 0, upload_id, 0, kSize})); | ||
| 133 | CopyTexture upload_tex{}; | ||
| 134 | upload_tex.dst = {tex_id, false, 0, 0, 0, 0, 0, 0}; | ||
| 135 | upload_tex.src = {upload_id, true, 0, 0, kTex, kTex, 1, kTex * 4}; | ||
| 136 | EXPECT(on_copy.copy_texture(upload_tex)); | ||
| 137 | EXPECT(on_copy.close()); | ||
| 138 | const CommandList* copy_lists[] = {&on_copy}; | ||
| 139 | EXPECT(copy.execute(copy_lists) == ReplayResult::success); | ||
| 140 | EXPECT(copy.signal(fence.semaphore(), 1)); | ||
| 141 | |||
| 142 | // 2. Direct queue: wait for the copy queue on the GPU, then read both back. | ||
| 143 | direct.wait(fence.semaphore(), 1); | ||
| 144 | CommandList on_direct; | ||
| 145 | EXPECT(on_direct.copy_buffer({readback_id, 0, mid_id, 0, kSize})); | ||
| 146 | CopyTexture read_tex{}; | ||
| 147 | read_tex.dst = {tex_readback_id, true, 0, 0, kTex, kTex, 1, kTex * 4}; | ||
| 148 | read_tex.src = {tex_id, false, 0, 0, 0, 0, 0, 0}; | ||
| 149 | EXPECT(on_direct.copy_texture(read_tex)); | ||
| 150 | EXPECT(on_direct.close()); | ||
| 151 | const CommandList* direct_lists[] = {&on_direct}; | ||
| 152 | EXPECT(direct.execute(direct_lists) == ReplayResult::success); | ||
| 153 | EXPECT(direct.signal(fence.semaphore(), 2)); | ||
| 154 | EXPECT(fence.wait(2, 5'000'000'000ull)); | ||
| 155 | |||
| 156 | std::uint32_t bad = 0; | ||
| 157 | auto* rb = static_cast<std::uint8_t*>(readback_map); | ||
| 158 | for (std::uint32_t i = 0; i < kSize; ++i) bad += rb[i] != pattern(i, 7); | ||
| 159 | auto* tr = static_cast<std::uint8_t*>(tex_readback_map); | ||
| 160 | for (std::uint32_t i = 0; i < kTex * kTex * 4; ++i) bad += tr[i] != pattern(i, 7); | ||
| 161 | std::printf("copy -> direct: buffer and texture upload read back, %u bad bytes\n", bad); | ||
| 162 | EXPECT(bad == 0); | ||
| 163 | |||
| 164 | // 3. Compute queue the other way: wait for the direct queue, copy, signal; the host waits on the fence. | ||
| 165 | for (std::uint32_t i = 0; i < kSize; ++i) up[i] = pattern(i, 99); | ||
| 166 | CommandList on_direct2; | ||
| 167 | EXPECT(on_direct2.copy_buffer({mid_id, 0, upload_id, 0, kSize})); | ||
| 168 | EXPECT(on_direct2.close()); | ||
| 169 | const CommandList* direct_lists2[] = {&on_direct2}; | ||
| 170 | EXPECT(direct.execute(direct_lists2) == ReplayResult::success); | ||
| 171 | EXPECT(direct.signal(fence.semaphore(), 3)); | ||
| 172 | compute.wait(fence.semaphore(), 3); | ||
| 173 | CommandList on_compute; | ||
| 174 | EXPECT(on_compute.copy_buffer({readback_id, 0, mid_id, 0, kSize})); | ||
| 175 | EXPECT(on_compute.close()); | ||
| 176 | const CommandList* compute_lists[] = {&on_compute}; | ||
| 177 | EXPECT(compute.execute(compute_lists) == ReplayResult::success); | ||
| 178 | EXPECT(compute.signal(fence.semaphore(), 4)); | ||
| 179 | EXPECT(fence.wait(4, 5'000'000'000ull)); | ||
| 180 | bad = 0; | ||
| 181 | for (std::uint32_t i = 0; i < kSize; ++i) bad += rb[i] != pattern(i, 99); | ||
| 182 | std::printf("direct -> compute: %u bad bytes\n", bad); | ||
| 183 | EXPECT(bad == 0); | ||
| 184 | |||
| 185 | // 4. A copy queue refuses a render-target clear and submits nothing. | ||
| 186 | VkImage rt_image = d.image(VK_FORMAT_R8G8B8A8_UNORM, 8, 8, 1, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT); | ||
| 187 | ResourceBinding rt; | ||
| 188 | rt.image = rt_image; | ||
| 189 | rt.format = VK_FORMAT_R8G8B8A8_UNORM; | ||
| 190 | rt.width = rt.height = 8; | ||
| 191 | context.add_resource(6, rt, VK_IMAGE_LAYOUT_UNDEFINED); | ||
| 192 | CommandList clear; | ||
| 193 | TargetView view{}; | ||
| 194 | view.resource = 6; | ||
| 195 | view.layer_count = 1; | ||
| 196 | EXPECT(clear.clear_render_target(view, ClearColor{})); | ||
| 197 | EXPECT(clear.close()); | ||
| 198 | const CommandList* clear_lists[] = {&clear}; | ||
| 199 | EXPECT(copy.execute(clear_lists) == ReplayResult::unsupported); | ||
| 200 | EXPECT(compute.execute(clear_lists) == ReplayResult::unsupported); | ||
| 201 | std::printf("copy and compute queues refuse a render-target clear\n"); | ||
| 202 | |||
| 203 | EXPECT(direct.idle() && compute.idle() && copy.idle()); | ||
| 204 | if (d.validation) { | ||
| 205 | std::printf("validation errors: %u\n", d.validation_errors()); | ||
| 206 | EXPECT(d.validation_errors() == 0); | ||
| 207 | } | ||
| 208 | std::printf("vk_queue_test: OK\n"); | ||
| 209 | return 0; | ||
| 210 | } | ||