upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/d3d12_queue_handover_test.cpp
diff options
context:
space:
mode:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 21:02:33 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 21:02:33 +0100
commitf8007dda67f9c45757b63df5c5fd8377647da4ae (patch)
treecdffc5cc7c81dcbbf683af9c0df6077c6587ac0d /runtime/test/d3d12_queue_handover_test.cpp
parent9bebfb107070d84609e303009b0f91b011b9aa2e (diff)
Queue holds: review fixes (Present ordering, pins, timeline monotonicity, fence lifetime, teardown, docs)
- Present is queued behind held work (QueueHolds::after_held) and submitted in order by the releasing thread. - Held batches keep the pipeline states and root signatures of their lists; fences a queue waits on or signals are kept until the submission has completed on the GPU (a D3D12 queue holds them). - A held Signal released below an unheld one on the same fence leaves the Vulkan signal out (claimed under the submit lock). - A queue destroyed with held Signals completes the CPU waits on their fences and reports it. - A failed held batch is reported (take_held_failure, last_execute_result), its waits stay for the next submission, and the Signal behind it still signals. - d3d12_queue_handover_test: pairs, two-thread signalling stress, held Signal of a released fence, shared fence, teardown with a CPU waiter; --two-queues and --shared-queue modes on a caller-owned device. - Tests for each fix in vk_presenter_sync_test, vk_queue_query_sync_test and d3d12_m4_compute_test; docs section 4.4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/d3d12_queue_handover_test.cpp')
-rw-r--r--runtime/test/d3d12_queue_handover_test.cpp247
1 files changed, 237 insertions, 10 deletions
diff --git a/runtime/test/d3d12_queue_handover_test.cpp b/runtime/test/d3d12_queue_handover_test.cpp
index c445fdfe6..d3ed188f6 100644
--- a/runtime/test/d3d12_queue_handover_test.cpp
+++ b/runtime/test/d3d12_queue_handover_test.cpp
@@ -17,6 +17,7 @@
17// compared byte for byte with the data written by the CPU: a consumer replayed against the layout from before its 17// compared byte for byte with the data written by the CPU: a consumer replayed against the layout from before its
18// producer ran reads a zero-filled texture and fails here (and with LEONIDA_VK_VALIDATION=1 under sync validation). 18// producer ran reads a zero-filled texture and fails here (and with LEONIDA_VK_VALIDATION=1 under sync validation).
19// No game data. 19// No game data.
20#include "vk_test_device.h"
20#include "../shims/d3d12_com.h" 21#include "../shims/d3d12_com.h"
21 22
22#include <dxgi1_4.h> 23#include <dxgi1_4.h>
@@ -364,6 +365,7 @@ bool cpu_released_chain(Rig& rig, const Roles& roles, int seed) {
364 365
365// A CPU Signal that comes before the Wait (nothing is held), and one that comes after the batch was submitted. 366// A CPU Signal that comes before the Wait (nothing is held), and one that comes after the batch was submitted.
366bool cpu_gate(Rig& rig, Type producer, Type consumer, bool signal_first, int seed) { 367bool cpu_gate(Rig& rig, Type producer, Type consumer, bool signal_first, int seed) {
368 if (!rig.available[producer] || !rig.available[consumer]) return true;
367 if (producer == consumer && rig.available[producer] < 2) return true; 369 if (producer == consumer && rig.available[producer] < 2) return true;
368 const int cslot = producer == consumer ? 1 : 0; 370 const int cslot = producer == consumer ? 1 : 0;
369 std::string name = std::string("CPU Signal ") + (signal_first ? "before" : "after") + " the consumer's Wait, " + 371 std::string name = std::string("CPU Signal ") + (signal_first ? "before" : "after") + " the consumer's Wait, " +
@@ -413,6 +415,7 @@ std::string capture_stderr(const std::function<void()>& body) {
413// A batch waits for a fence nobody signals. The other queues keep working, the batch never runs, destroying the queue 415// A batch waits for a fence nobody signals. The other queues keep working, the batch never runs, destroying the queue
414// does not hang and the layer says why. Layer only: the host runtime has nothing to report. 416// does not hang and the layer says why. Layer only: the host runtime has nothing to report.
415bool never_signaled(Rig& rig, int seed) { 417bool never_signaled(Rig& rig, int seed) {
418 if (!rig.available[copy_q] || !rig.available[direct_q]) return true;
416 arm("fence nobody signals"); 419 arm("fence nobody signals");
417 Case c(rig, seed); 420 Case c(rig, seed);
418 if (!c.make_resources()) return false; 421 if (!c.make_resources()) return false;
@@ -463,31 +466,210 @@ bool fence_released(Rig& rig, int seed) {
463 ID3D12CommandList* batch[] = {held}; 466 ID3D12CommandList* batch[] = {held};
464 if (FAILED(victim->Wait(doomed.Get(), 9))) return c.fail("Wait", __LINE__); 467 if (FAILED(victim->Wait(doomed.Get(), 9))) return c.fail("Wait", __LINE__);
465 victim->ExecuteCommandLists(1, batch); 468 victim->ExecuteCommandLists(1, batch);
466 const std::string log = capture_stderr([&] { doomed.Reset(); victim.Reset(); }); 469 // The queue holds the fence it waits on, as D3D12 does: the application's release does not free it under the held
467 if (log.find("fence was released while a queue still held work") == std::string::npos) { 470 // batch. The batch stays held (nothing ran) and is reported when the queue goes.
471 doomed.Reset();
472 Sleep(50);
473 const std::string log = capture_stderr([&] { victim.Reset(); });
474 if (log.find("queue destroyed with") == std::string::npos) {
475 std::fprintf(stderr, "stderr was: \"%s\"\n", log.c_str());
476 return c.fail("no report for the held batch", __LINE__);
477 }
478 ++cases_run;
479 return true;
480}
481
482// Review item 4: a held Signal names a fence the application has released. The fence lives until the Signal has been
483// submitted (a queue behind it that waits for the fence proves it was signaled), with nothing used after free.
484bool held_signal_survives_release(Rig& rig, int seed) {
485 if (!rig.available[direct_q] || !rig.available[compute_q]) return true;
486 arm("held Signal of a released fence");
487 Case c(rig, seed);
488 if (!c.make_resources()) return false;
489 ComPtr<ID3D12Fence> gate, target;
490 if (FAILED(rig.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&gate))) ||
491 FAILED(rig.device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&target))))
492 return false;
493 ID3D12CommandQueue* a = rig.queues[direct_q][0].Get();
494 ID3D12CommandQueue* b = rig.queues[compute_q][0].Get();
495 // a: Wait(gate), copy upload -> stage[0], Signal(target) (held until the CPU signals the gate)
496 ID3D12GraphicsCommandList* produce = c.make_list(direct_q, nullptr, &c.stage[0], 3);
497 ID3D12GraphicsCommandList* consume = c.make_list(compute_q, &c.stage[0], nullptr, 3);
498 if (!produce || !consume) return c.fail("setup", __LINE__);
499 if (FAILED(a->Wait(gate.Get(), 1))) return c.fail("Wait", __LINE__);
500 ID3D12CommandList* pb[] = {produce};
501 a->ExecuteCommandLists(1, pb);
502 if (FAILED(a->Signal(target.Get(), 1))) return c.fail("Signal", __LINE__);
503 if (FAILED(b->Wait(target.Get(), 1))) return c.fail("Wait", __LINE__);
504 ID3D12CommandList* cb[] = {consume};
505 b->ExecuteCommandLists(1, cb);
506 if (FAILED(b->Signal(c.fence(2), 1))) return c.fail("Signal", __LINE__);
507 target.Reset(); // the application is done with it; both queues still name it
508 Sleep(20);
509 if (FAILED(gate->Signal(1))) return c.fail("CPU Signal", __LINE__);
510 ++cases_run;
511 return c.wait_fence(2, 1) && c.verify();
512}
513
514// Review item 3: Signal(F, 2) is held, Signal(F, 3) on another queue is not. When the held one is released the
515// timeline is already at 3 and must not be signaled lower; a wait for 2 passes and F reads 3. Layer only (D3D12 sets
516// the fence to the lower value).
517bool monotonic_shared_fence(Rig& rig, int seed) {
518 if (!rig.available[direct_q] || !rig.available[copy_q] || !rig.available[compute_q]) return true;
519 arm("held Signal below an unheld one");
520 Case c(rig, seed);
521 if (!c.make_resources()) return false;
522 ID3D12CommandQueue* a = rig.queues[direct_q][0].Get();
523 ID3D12CommandQueue* b = rig.queues[copy_q][0].Get();
524 ID3D12CommandQueue* w = rig.queues[compute_q][0].Get();
525 ID3D12Fence *gate = c.fence(0), *shared = c.fence(1);
526 ID3D12GraphicsCommandList* produce = c.make_list(copy_q, nullptr, &c.stage[0], 3);
527 ID3D12GraphicsCommandList* consume = c.make_list(compute_q, &c.stage[0], nullptr, 3);
528 if (!gate || !shared || !produce || !consume) return c.fail("setup", __LINE__);
529 if (FAILED(a->Wait(gate, 1)) || FAILED(a->Signal(shared, 2))) return c.fail("held Signal", __LINE__); // held
530 ID3D12CommandList* pb[] = {produce};
531 b->ExecuteCommandLists(1, pb);
532 if (FAILED(b->Signal(shared, 3))) return c.fail("Signal", __LINE__); // not held
533 if (c.wait_fence(1, 3) == false) return false;
534 if (FAILED(gate->Signal(1))) return c.fail("CPU Signal", __LINE__); // releases a
535 // A queue that waits for 2 now: satisfied, and the data the copy queue wrote is what it reads.
536 if (FAILED(w->Wait(shared, 2))) return c.fail("Wait", __LINE__);
537 ID3D12CommandList* cb[] = {consume};
538 w->ExecuteCommandLists(1, cb);
539 if (FAILED(w->Signal(c.fence(2), 1))) return c.fail("Signal", __LINE__);
540 if (!c.wait_fence(2, 1) || !c.verify()) return false;
541 Sleep(50);
542 if (shared->GetCompletedValue() != 3) return c.fail("the fence went below 3", __LINE__);
543 ++cases_run;
544 return true;
545}
546
547// Review item 5: a queue destroyed with a held Signal wakes the CPU wait on that fence (with a report) instead of
548// leaving it hanging.
549bool teardown_wakes_cpu_waiters(Rig& rig, int seed) {
550 arm("teardown with a held Signal");
551 Case c(rig, seed);
552 if (!c.make_resources()) return false;
553 ComPtr<ID3D12CommandQueue> victim;
554 const D3D12_COMMAND_QUEUE_DESC desc{D3D12_COMMAND_LIST_TYPE_DIRECT};
555 if (FAILED(rig.device->CreateCommandQueue(&desc, IID_PPV_ARGS(&victim)))) return true;
556 ID3D12Fence *never = c.fence(0), *target = c.fence(1);
557 if (FAILED(victim->Wait(never, 1)) || FAILED(victim->Signal(target, 1))) return c.fail("setup", __LINE__);
558 HANDLE event = CreateEventW(nullptr, FALSE, FALSE, nullptr);
559 if (FAILED(target->SetEventOnCompletion(1, event))) return c.fail("SetEventOnCompletion", __LINE__);
560 const std::string log = capture_stderr([&] { victim.Reset(); });
561 const bool woke = WaitForSingleObject(event, 5000) == WAIT_OBJECT_0;
562 CloseHandle(event);
563 if (!woke) return c.fail("the CPU wait on the held Signal's fence was never woken", __LINE__);
564 if (log.find("CPU waits on the fences of its held Signals are completed") == std::string::npos) {
468 std::fprintf(stderr, "stderr was: \"%s\"\n", log.c_str()); 565 std::fprintf(stderr, "stderr was: \"%s\"\n", log.c_str());
469 return c.fail("no report for the dropped batch", __LINE__); 566 return c.fail("no report", __LINE__);
470 } 567 }
471 ++cases_run; 568 ++cases_run;
472 return true; 569 return true;
473} 570}
474 571
572// Two threads: one submits a consumer and a relay (both held, waiting for fences that are signaled by queues it does
573// not own), the other submits the producer and signals a CPU gate the producer waits on, at the same moment, many
574// times over. Everything is built up front; the threads only make queue calls.
575bool concurrent_signalling(Rig& rig, int iterations, int seed) {
576 if (!Roles::make(copy_q, compute_q, direct_q).fits(rig)) return true; // needs one queue of each type
577 arm("two threads signalling and releasing", 120000);
578 struct Round {
579 std::unique_ptr<Case> c;
580 Case::Job jobs[3];
581 ID3D12GraphicsCommandList* lists[3];
582 };
583 std::vector<Round> rounds(iterations);
584 const Roles roles = Roles::make(copy_q, compute_q, direct_q);
585 for (int i = 0; i < iterations; ++i) {
586 Round& r = rounds[i];
587 r.c = std::make_unique<Case>(rig, seed + i);
588 Case& c = *r.c;
589 if (!c.make_resources()) return false;
590 const bool gated = i % 2 == 0;
591 r.jobs[0] = {roles.type[0], roles.slot[0], nullptr, &c.stage[0], 3, gated ? std::vector<std::pair<int, UINT64>>{{3, 1}} : std::vector<std::pair<int, UINT64>>{}, 0};
592 r.jobs[1] = {roles.type[1], roles.slot[1], &c.stage[0], &c.stage[1], 3, {{0, 1}}, 1};
593 r.jobs[2] = {roles.type[2], roles.slot[2], &c.stage[1], nullptr, 3, {{1, 1}}, 2};
594 for (int j = 0; j < 3; ++j) {
595 r.lists[j] = c.make_list(r.jobs[j].type, r.jobs[j].src, r.jobs[j].dst, r.jobs[j].mask);
596 if (!r.lists[j]) return false;
597 }
598 if (!c.fence(3)) return false;
599 }
600 std::atomic<int> turn{-1}, ready{0}, failed{0};
601 const auto fire = [&](Case& c, const Case::Job& job, ID3D12GraphicsCommandList* list) {
602 ID3D12CommandQueue* queue = rig.queues[job.type][job.slot].Get();
603 for (const auto& [f, v] : job.waits)
604 if (FAILED(queue->Wait(c.fences[f].Get(), v))) failed++;
605 ID3D12CommandList* batch[] = {list};
606 queue->ExecuteCommandLists(1, batch);
607 if (job.signal >= 0 && FAILED(queue->Signal(c.fences[job.signal].Get(), 1))) failed++;
608 };
609 std::thread consumer_side([&] {
610 for (int i = 0; i < iterations; ++i) {
611 ready++;
612 while (turn.load() < i) std::this_thread::yield();
613 fire(*rounds[i].c, rounds[i].jobs[2], rounds[i].lists[2]);
614 fire(*rounds[i].c, rounds[i].jobs[1], rounds[i].lists[1]);
615 }
616 });
617 std::thread producer_side([&] {
618 for (int i = 0; i < iterations; ++i) {
619 while (ready.load() <= i) std::this_thread::yield();
620 turn = i;
621 fire(*rounds[i].c, rounds[i].jobs[0], rounds[i].lists[0]);
622 if (i % 2 == 0 && FAILED(rounds[i].c->fences[3]->Signal(1))) failed++;
623 }
624 });
625 producer_side.join();
626 consumer_side.join();
627 if (failed) return rounds[0].c->fail("a queue call failed", __LINE__);
628 bool ok = true;
629 for (int i = 0; i < iterations; ++i) {
630 arm("two threads signalling and releasing", 20000);
631 ok &= rounds[i].c->wait_fence(2, 1) && rounds[i].c->verify();
632 }
633 cases_run += iterations;
634 return ok;
635}
636
637// Producer and consumer on two queues, in both submission orders: what fits when the device hands over only two
638// VkQueues (the D3D12 queues share them) and what M4's copy -> compute -> copy path does.
639bool pair_case(Rig& rig, Type a, Type b, bool consumer_first, int seed) {
640 const int sb = a == b ? 1 : 0;
641 if (!rig.available[a] || sb >= rig.available[b]) return true;
642 std::string name = std::string("pair ") + type_name[a] + "->" + type_name[b] +
643 (consumer_first ? " consumer first" : " producer first");
644 arm(name.c_str());
645 Case c(rig, seed);
646 if (!c.make_resources()) return false;
647 const Case::Job produce{a, 0, nullptr, &c.stage[0], 3, {}, 0};
648 const Case::Job consume{b, sb, &c.stage[0], nullptr, 3, {{0, 1}}, 2};
649 if (consumer_first ? !(c.submit(consume) && c.submit(produce)) : !(c.submit(produce) && c.submit(consume)))
650 return false;
651 ++cases_run;
652 return c.wait_fence(2, 1) && c.verify();
653}
654
475bool run_all(ID3D12Device* device, bool layer) { 655bool run_all(ID3D12Device* device, bool layer) {
476 Rig rig; 656 Rig rig;
477 rig.device = device; 657 rig.device = device;
478 for (int t = 0; t < 3; ++t) 658 for (int i = 0; i < pool_size; ++i) // one of each type first: a caller-owned device may hand over only a few
479 for (int i = 0; i < pool_size; ++i) { 659 for (int t = 0; t < 3; ++t) {
660 if (rig.available[t] != i) continue;
480 D3D12_COMMAND_QUEUE_DESC desc{}; 661 D3D12_COMMAND_QUEUE_DESC desc{};
481 desc.Type = list_type[t]; 662 desc.Type = list_type[t];
482 if (FAILED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&rig.queues[t][i])))) break; 663 if (SUCCEEDED(device->CreateCommandQueue(&desc, IID_PPV_ARGS(&rig.queues[t][i])))) ++rig.available[t];
483 ++rig.available[t];
484 } 664 }
485 std::printf("queues available per type (COPY/COMPUTE/DIRECT): %d/%d/%d\n", rig.available[0], rig.available[1], 665 std::printf("queues available per type (COPY/COMPUTE/DIRECT): %d/%d/%d\n", rig.available[0], rig.available[1],
486 rig.available[2]); 666 rig.available[2]);
487 if (!rig.available[0] || !rig.available[1] || !rig.available[2]) return true; // nothing to hand over between
488 667
489 bool ok = true; 668 bool ok = true;
490 int seed = 100, skipped = 0; 669 int seed = 100, skipped = 0;
670 for (int a = 0; a < 3; ++a)
671 for (int b = 0; b < 3; ++b)
672 for (bool consumer_first : {true, false}) ok &= pair_case(rig, Type(a), Type(b), consumer_first, ++seed);
491 std::array<int, 3> order{0, 1, 2}; 673 std::array<int, 3> order{0, 1, 2};
492 for (int a = 0; a < 3; ++a) 674 for (int a = 0; a < 3; ++a)
493 for (int b = 0; b < 3; ++b) 675 for (int b = 0; b < 3; ++b)
@@ -511,12 +693,16 @@ bool run_all(ID3D12Device* device, bool layer) {
511 for (int p = 0; p < 3; ++p) 693 for (int p = 0; p < 3; ++p)
512 for (int q = 0; q < 3; ++q) 694 for (int q = 0; q < 3; ++q)
513 for (bool first : {true, false}) ok &= cpu_gate(rig, Type(p), Type(q), first, ++seed); 695 for (bool first : {true, false}) ok &= cpu_gate(rig, Type(p), Type(q), first, ++seed);
696 ok &= concurrent_signalling(rig, 200, seed += 1000);
514 if (layer) { 697 if (layer) {
515 // The layer hands out a limited number of VkQueues: give the spare ones back for the two cases below. 698 // The layer hands out a limited number of VkQueues: give the spare ones back for the two cases below.
516 for (auto& type_queues : rig.queues) 699 for (auto& type_queues : rig.queues)
517 for (int i = 1; i < pool_size; ++i) type_queues[i].Reset(); 700 for (int i = 1; i < pool_size; ++i) type_queues[i].Reset();
518 ok &= never_signaled(rig, ++seed); 701 ok &= never_signaled(rig, ++seed);
519 ok &= fence_released(rig, ++seed); 702 ok &= fence_released(rig, ++seed);
703 ok &= held_signal_survives_release(rig, ++seed);
704 ok &= monotonic_shared_fence(rig, ++seed);
705 ok &= teardown_wakes_cpu_waiters(rig, ++seed);
520 } 706 }
521 if (skipped) std::printf("(%d type assignments need more queues than the device gave)\n", skipped); 707 if (skipped) std::printf("(%d type assignments need more queues than the device gave)\n", skipped);
522 return ok; 708 return ok;
@@ -539,7 +725,15 @@ ComPtr<ID3D12Device> native_device(std::wstring& name) {
539 725
540} // namespace 726} // namespace
541 727
542int main() { 728// Modes (argv[1]):
729// (none) the standalone device, which gives each D3D12 queue its own VkQueue; then the host runtime
730// --two-queues a caller-owned device with two VkQueues of the family handed over (VulkanDeviceInfo::command_queues):
731// the D3D12 queues of a type share them, so a Wait and the Signal it needs can land on one VkQueue
732// --shared-queue a caller-owned device that hands over its one VkQueue: every D3D12 queue shares it (the legacy
733// single-queue behaviour), where a Wait submitted ahead of its Signal would never progress on the GPU
734int main(int argc, char** argv) {
735 const bool two_queues = argc > 1 && std::strcmp(argv[1], "--two-queues") == 0;
736 const bool shared_queue = argc > 1 && std::strcmp(argv[1], "--shared-queue") == 0;
543 std::thread([] { 737 std::thread([] {
544 for (;;) { 738 for (;;) {
545 Sleep(100); 739 Sleep(100);
@@ -552,8 +746,32 @@ int main() {
552 } 746 }
553 }).detach(); 747 }).detach();
554 748
749 vktest::Device vk;
555 ComPtr<ID3D12Device> layer; 750 ComPtr<ID3D12Device> layer;
556 if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&layer)))) { 751 if (two_queues || shared_queue) {
752 vk.queue_count_wanted = two_queues ? 2 : 1;
753 const auto status = vk.initialize();
754 if (status == vktest::DeviceStatus::unavailable) return 77;
755 if (status != vktest::DeviceStatus::ready) return 1;
756 if (two_queues && vk.queues.size() < 2) {
757 std::puts("the queue family has one queue: two-queue mode skipped");
758 return 77;
759 }
760 rt::d3d12::VulkanDeviceInfo info{};
761 info.get_instance_proc = vk.instance_proc;
762 info.instance = vk.instance;
763 info.physical = vk.physical;
764 info.device = vk.device;
765 info.queue = vk.queue;
766 info.queue_family = vk.family;
767 if (two_queues) info.command_queues = vk.queues;
768 if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&layer)))) {
769 std::puts("Vulkan device unavailable");
770 return 77;
771 }
772 std::printf("caller-owned device, %zu VkQueue(s) handed to the layer\n",
773 two_queues ? vk.queues.size() : size_t(1));
774 } else if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&layer)))) {
557 std::puts("Vulkan standalone device unavailable"); 775 std::puts("Vulkan standalone device unavailable");
558 return 77; 776 return 77;
559 } 777 }
@@ -562,6 +780,15 @@ int main() {
562 if (!run_all(layer.Get(), true)) return 1; 780 if (!run_all(layer.Get(), true)) return 1;
563 const int layer_cases = cases_run; 781 const int layer_cases = cases_run;
564 std::printf("Vulkan layer: %d cases passed, every one exact\n", layer_cases); 782 std::printf("Vulkan layer: %d cases passed, every one exact\n", layer_cases);
783 layer.Reset();
784 if (vk.validation_errors()) {
785 std::fprintf(stderr, "%u validation errors\n", vk.validation_errors());
786 return 1;
787 }
788 if (two_queues || shared_queue) {
789 deadline = 0;
790 return 0;
791 }
565 792
566 std::wstring name; 793 std::wstring name;
567 if (auto host = native_device(name)) { 794 if (auto host = native_device(name)) {