upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/submit_frame_test.cpp
diff options
context:
space:
mode:
authorJan Trencansky <jan.trencansky@protonmail.com>2026-11-28 00:14:10 -0500
committerJan Trencansky <jan.trencansky@protonmail.com>2026-11-28 00:14:10 -0500
commit89fe359ee0040b81e751862b06d1f13375526e00 (patch)
treea69baac01d611cab0390095f34f2c19a69097655 /runtime/test/submit_frame_test.cpp
parent65acf7324632faab3dd951be6c4cf5b7280af17e (diff)
runtime: VulkanQueue execute+signal coalescing, fast heap filtering, and batch-parallel sync compiles
Cuts queue submit overhead, descriptor-heap filtering cost, and synchronous cold pipeline compile latency: - VulkanQueue: adds pending-wait tracking (has_unsatisfied_waiter), deduplicated submit_multi(), execute+signal coalescing with cross-queue/CPU waiter splits, and coalescing-group-aware async room gating. - d3d12_com.cpp: caches descriptor heap VAs/counts on SetDescriptorHeaps, pre-tracks command-list barrier/query/indirect/draw flags, single-alloc PinnedObjects, and linear table dedup. - d3d12_vulkan_device.cpp: AVX2 32-byte slot equality, 8 KiB L1d seen_keys, stack-local FilterPageCache, 64 KiB page-pointer caching, and T# dedup in finish_in_place. - vulkan_replay.h / d3d12_vulkan_device.cpp: lightweight collect_unbuilt_pipelines() pre-pass and cancel-safe batch-parallel sync compile runner backed by PipelineCompiler. AI-assisted: Antigravity (Gemini 4 Neon).
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
-rw-r--r--runtime/test/submit_frame_test.cpp253
1 files changed, 253 insertions, 0 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp
index 2d5478af6..4e2f59be6 100644
--- a/runtime/test/submit_frame_test.cpp
+++ b/runtime/test/submit_frame_test.cpp
@@ -1569,6 +1569,259 @@ int queue_sync_and_async() {
1569 std::printf("note_mirror_bind ticket & submit wait ordering: ok\n"); 1569 std::printf("note_mirror_bind ticket & submit wait ordering: ok\n");
1570 } 1570 }
1571 1571
1572 // 11. Round 5 same-queue execute+signal coalescing, depth=4 frame-burst room gating, cross-queue Wait split,
1573 // Present barrier split, and coalesced error fallback isolation (LEONIDA_VK_VALIDATION=1 clean).
1574 {
1575 static PFN_vkQueueSubmit s_real_submit = nullptr;
1576 static std::atomic<unsigned> s_cb_submits{0};
1577 static std::atomic<unsigned> s_total_submits{0};
1578 struct HookSubmit {
1579 static VKAPI_ATTR VkResult VKAPI_CALL call(VkQueue q, std::uint32_t count, const VkSubmitInfo* infos,
1580 VkFence fence) {
1581 for (std::uint32_t i = 0; i < count; ++i) {
1582 s_total_submits.fetch_add(1, std::memory_order_relaxed);
1583 if (infos[i].commandBufferCount > 0) s_cb_submits.fetch_add(1, std::memory_order_relaxed);
1584 }
1585 return s_real_submit(q, count, infos, fence);
1586 }
1587 };
1588
1589 VulkanContext context(d.device_proc, d.device, true);
1590 EXPECT(context.loaded());
1591 s_real_submit = context.vk.vkQueueSubmit;
1592 s_cb_submits.store(0, std::memory_order_relaxed);
1593 s_total_submits.store(0, std::memory_order_relaxed);
1594 context.vk.vkQueueSubmit = &HookSubmit::call;
1595
1596 VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
1597 VulkanQueue compute(context, d.queue, d.family, VulkanQueue::QueueKind::compute);
1598 VulkanQueue copy_q(context, d.queue, d.family, VulkanQueue::QueueKind::copy);
1599 EXPECT(direct.ok() && compute.ok() && copy_q.ok());
1600 direct.set_async_depth(4);
1601 compute.set_async_depth(4);
1602 copy_q.set_async_depth(4);
1603
1604 constexpr VkDeviceSize kBurstBufBytes = 512;
1605 void* src_map = nullptr;
1606 void* dst_map = nullptr;
1607 VkBuffer src = d.buffer(kBurstBufBytes, usage, &src_map);
1608 VkBuffer dst = d.buffer(kBurstBufBytes, usage, &dst_map);
1609 EXPECT(src && dst);
1610 auto* sb = static_cast<std::uint8_t*>(src_map);
1611 auto* db = static_cast<std::uint8_t*>(dst_map);
1612 std::memset(sb, 0, kBurstBufBytes);
1613 std::memset(db, 0, kBurstBufBytes);
1614 constexpr int kSteps = 18;
1615 for (int i = 0; i < kSteps; ++i) sb[i * 16] = std::uint8_t(0x40 + i);
1616
1617 ResourceBinding r_src{}, r_dst{};
1618 r_src.buffer = src;
1619 r_src.size = kBurstBufBytes;
1620 r_dst.buffer = dst;
1621 r_dst.size = kBurstBufBytes;
1622 r_dst.host_readable = true;
1623 context.add_resource(1, r_src);
1624 context.add_resource(2, r_dst);
1625
1626 std::shared_ptr<const CommandList> steps[kSteps];
1627 for (int i = 0; i < kSteps; ++i) {
1628 auto l = std::make_shared<CommandList>();
1629 EXPECT(l->copy_buffer({2, std::uint64_t((i + 1) * 16), 1, std::uint64_t(i * 16), 1}) &&
1630 l->copy_buffer({2, 0, 1, std::uint64_t(i * 16), 1}) && l->close());
1631 steps[i] = std::move(l);
1632 }
1633
1634 // (a) Unwaited [execute, signal(F, 1), execute, signal(F, 2), execute, signal(F, 3), execute, signal(F, 4)]
1635 // plus trailing signal(F2, 7) coalesces into ONE before_execute_() + ONE vkQueueSubmit (deduping F -> 4).
1636 std::atomic<unsigned> before_calls{0};
1637 std::atomic<bool> in_first{false}, go_first{false};
1638 context.set_before_execute([&] {
1639 const unsigned n = before_calls.fetch_add(1, std::memory_order_acq_rel) + 1;
1640 if (n == 1) {
1641 in_first.store(true, std::memory_order_release);
1642 while (!go_first.load(std::memory_order_acquire)) std::this_thread::yield();
1643 }
1644 });
1645
1646 VulkanFence f_a(context, 0), f_a2(context, 0);
1647 EXPECT(f_a.semaphore() && f_a2.semaphore());
1648
1649 EXPECT(direct.execute({&steps[0], 1}) == ReplayResult::success);
1650 while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();
1651
1652 for (int i = 1; i <= 4; ++i) {
1653 EXPECT(direct.execute({&steps[i], 1}) == ReplayResult::success);
1654 EXPECT(direct.signal(f_a.semaphore(), std::uint64_t(i)));
1655 }
1656 EXPECT(direct.signal(f_a2.semaphore(), 7));
1657
1658 go_first.store(true, std::memory_order_release);
1659 EXPECT(f_a.wait(4, 5'000'000'000ull));
1660 EXPECT(f_a2.wait(7, 5'000'000'000ull));
1661 EXPECT(direct.idle());
1662
1663 EXPECT(before_calls.load(std::memory_order_acquire) == 2);
1664 EXPECT(s_cb_submits.load(std::memory_order_acquire) == 2);
1665 EXPECT(s_total_submits.load(std::memory_order_acquire) == 2); // zero separate signal-only vkQueueSubmits!
1666 for (int i = 0; i < 5; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0x40 + i));
1667 EXPECT(db[0] == std::uint8_t(0x44));
1668
1669 // (b) 17-batch + 17-signal single-frame burst on depth=4 completes without stalling on room_cv_ and merges
1670 // all 17 batches + 17 signals into ONE vkQueueSubmit.
1671 before_calls.store(0, std::memory_order_release);
1672 s_cb_submits.store(0, std::memory_order_release);
1673 s_total_submits.store(0, std::memory_order_release);
1674 in_first.store(false, std::memory_order_release);
1675 go_first.store(false, std::memory_order_release);
1676 std::memset(db, 0, kBurstBufBytes);
1677
1678 VulkanFence f_burst(context, 0);
1679 EXPECT(f_burst.semaphore());
1680 EXPECT(direct.execute({&steps[0], 1}) == ReplayResult::success);
1681 while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();
1682
1683 std::atomic<bool> burst_queued{false};
1684 std::thread burst_producer([&] {
1685 bool ok = true;
1686 for (int i = 1; i <= 17 && ok; ++i) {
1687 ok = (direct.execute({&steps[i], 1}) == ReplayResult::success) &&
1688 direct.signal(f_burst.semaphore(), std::uint64_t(i));
1689 }
1690 if (ok) burst_queued.store(true, std::memory_order_release);
1691 });
1692 const auto burst_deadline = std::chrono::steady_clock::now() + std::chrono::milliseconds(500);
1693 while (!burst_queued.load(std::memory_order_acquire) && std::chrono::steady_clock::now() < burst_deadline)
1694 std::this_thread::sleep_for(std::chrono::milliseconds(1));
1695 const bool burst_no_stall = burst_queued.load(std::memory_order_acquire);
1696 go_first.store(true, std::memory_order_release);
1697 burst_producer.join();
1698 EXPECT(burst_no_stall);
1699 EXPECT(f_burst.wait(17, 5'000'000'000ull));
1700 EXPECT(direct.idle());
1701
1702 EXPECT(before_calls.load(std::memory_order_acquire) == 2);
1703 EXPECT(s_cb_submits.load(std::memory_order_acquire) == 2);
1704 EXPECT(s_total_submits.load(std::memory_order_acquire) == 2);
1705 for (int i = 0; i < 18; ++i) EXPECT(db[(i + 1) * 16] == std::uint8_t(0x40 + i));
1706 EXPECT(db[0] == std::uint8_t(0x40 + 17));
1707
1708 // (c) Cross-queue Wait(cross_f, 1) on `compute` splits `direct` right after Signal(cross_f, 1) (attaching
1709 // Signal(cross_f, 1) to B1's submit rather than merging B2 ahead of it) and unblocks `compute` cleanly.
1710 before_calls.store(0, std::memory_order_release);
1711 s_cb_submits.store(0, std::memory_order_release);
1712 s_total_submits.store(0, std::memory_order_release);
1713 in_first.store(false, std::memory_order_release);
1714 go_first.store(false, std::memory_order_release);
1715 std::memset(db, 0, kBurstBufBytes);
1716
1717 VulkanFence cross_f(context, 0), compute_done(context, 0), direct_done(context, 0);
1718 EXPECT(cross_f.semaphore() && compute_done.semaphore() && direct_done.semaphore());
1719
1720 auto compute_step = make_copy_list(2, 320, 1, 16, 1); // copies 0x41 -> db[320]
1721 EXPECT(compute_step != nullptr);
1722 std::shared_ptr<const CommandList> s_comp = compute_step;
1723
1724 EXPECT(direct.execute({&steps[0], 1}) == ReplayResult::success);
1725 while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();
1726
1727 // Register cross-queue Wait(cross_f, 1) on compute queue and flush into held_.
1728 compute.wait(cross_f.semaphore(), 1);
1729 EXPECT(compute.execute({&s_comp, 1}) == ReplayResult::success);
1730 EXPECT(compute.signal(compute_done.semaphore(), 1));
1731 EXPECT(compute.idle());
1732
1733 // Queue B1 + Signal(cross_f, 1) + B2 + Signal(direct_done, 1) on direct.
1734 EXPECT(direct.execute({&steps[1], 1}) == ReplayResult::success);
1735 EXPECT(direct.signal(cross_f.semaphore(), 1));
1736 EXPECT(direct.execute({&steps[2], 1}) == ReplayResult::success);
1737 EXPECT(direct.signal(direct_done.semaphore(), 1));
1738
1739 go_first.store(true, std::memory_order_release);
1740 EXPECT(compute_done.wait(1, 5'000'000'000ull));
1741 EXPECT(direct_done.wait(1, 5'000'000'000ull));
1742 EXPECT(direct.idle() && compute.idle());
1743
1744 // Direct split into {B0}, {B1 + sig(cross_f)}, {B2 + sig(direct_done)} (3 submits) + compute {s_comp + sig} (1) = 4.
1745 EXPECT(before_calls.load(std::memory_order_acquire) == 4);
1746 EXPECT(s_cb_submits.load(std::memory_order_acquire) == 4);
1747 EXPECT(s_total_submits.load(std::memory_order_acquire) == 4);
1748 EXPECT(db[16] == 0x40 && db[32] == 0x41 && db[48] == 0x42 && db[0] == 0x42 && db[320] == 0x41);
1749
1750 // (d) Present (after_held) barrier strictly splits [B1, signal(F, 1)] from [B2, signal(F, 2)].
1751 before_calls.store(0, std::memory_order_release);
1752 s_cb_submits.store(0, std::memory_order_release);
1753 s_total_submits.store(0, std::memory_order_release);
1754 in_first.store(false, std::memory_order_release);
1755 go_first.store(false, std::memory_order_release);
1756 std::memset(db, 0, kBurstBufBytes);
1757
1758 VulkanFence f_pres(context, 0);
1759 EXPECT(f_pres.semaphore());
1760 EXPECT(direct.execute({&steps[0], 1}) == ReplayResult::success);
1761 while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();
1762
1763 EXPECT(direct.execute({&steps[1], 1}) == ReplayResult::success);
1764 EXPECT(direct.signal(f_pres.semaphore(), 1));
1765
1766 std::atomic<bool> pres_ran{false};
1767 std::atomic<unsigned> pres_seen_before{0}, pres_seen_total{0};
1768 int pres_owner = 0;
1769 context.holds().after_held(&pres_owner, [&] {
1770 pres_seen_before.store(before_calls.load(std::memory_order_acquire), std::memory_order_release);
1771 pres_seen_total.store(s_total_submits.load(std::memory_order_acquire), std::memory_order_release);
1772 pres_ran.store(true, std::memory_order_release);
1773 });
1774
1775 EXPECT(direct.execute({&steps[2], 1}) == ReplayResult::success);
1776 EXPECT(direct.signal(f_pres.semaphore(), 2));
1777
1778 go_first.store(true, std::memory_order_release);
1779 EXPECT(f_pres.wait(2, 5'000'000'000ull));
1780 EXPECT(direct.idle());
1781
1782 EXPECT(pres_ran.load(std::memory_order_acquire));
1783 EXPECT(pres_seen_before.load(std::memory_order_acquire) == 2);
1784 EXPECT(pres_seen_total.load(std::memory_order_acquire) == 2);
1785 EXPECT(before_calls.load(std::memory_order_acquire) == 3);
1786 EXPECT(s_cb_submits.load(std::memory_order_acquire) == 3);
1787 EXPECT(s_total_submits.load(std::memory_order_acquire) == 3);
1788 EXPECT(db[16] == 0x40 && db[32] == 0x41 && db[48] == 0x42 && db[0] == 0x42);
1789
1790 // (e) Coalesced [valid1, signal(s1, 1), bad_clear, valid2, signal(s2, 1)] on async copy queue falls back
1791 // per-item in exact FIFO order without dropping valid peers or signals.
1792 before_calls.store(0, std::memory_order_release);
1793 in_first.store(false, std::memory_order_release);
1794 go_first.store(false, std::memory_order_release);
1795 std::memset(db, 0, kBurstBufBytes);
1796
1797 auto bad_clear = std::make_shared<CommandList>();
1798 TargetView bad_tv{};
1799 bad_tv.resource = 2;
1800 bad_tv.layer_count = 1;
1801 EXPECT(bad_clear->clear_render_target(bad_tv, ClearColor{}) && bad_clear->close());
1802 std::shared_ptr<const CommandList> bad_snap = bad_clear;
1803
1804 VulkanFence err_s1(context, 0), err_s2(context, 0);
1805 EXPECT(err_s1.semaphore() && err_s2.semaphore());
1806
1807 EXPECT(copy_q.execute({&steps[0], 1}) == ReplayResult::success);
1808 while (!in_first.load(std::memory_order_acquire)) std::this_thread::yield();
1809
1810 EXPECT(copy_q.execute({&steps[1], 1}) == ReplayResult::success);
1811 EXPECT(copy_q.signal(err_s1.semaphore(), 1));
1812 EXPECT(copy_q.execute({&bad_snap, 1}) == ReplayResult::success);
1813 EXPECT(copy_q.execute({&steps[2], 1}) == ReplayResult::success);
1814 EXPECT(copy_q.signal(err_s2.semaphore(), 1));
1815
1816 go_first.store(true, std::memory_order_release);
1817 EXPECT(err_s1.wait(1, 5'000'000'000ull));
1818 EXPECT(err_s2.wait(1, 5'000'000'000ull));
1819 EXPECT(copy_q.idle());
1820 EXPECT(copy_q.take_held_failure() == ReplayResult::unsupported);
1821 EXPECT(db[16] == 0x40 && db[32] == 0x41 && db[48] == 0x42 && db[0] == 0x42);
1822 std::printf("queue execute+signal coalescing: unwaited merge, 17-batch burst, cross-queue Wait split, Present split & error fallback ok\n");
1823 }
1824
1572 return 0; 1825 return 0;
1573} 1826}
1574 1827