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:
Diffstat (limited to 'runtime/test/submit_frame_test.cpp')
-rw-r--r--runtime/test/submit_frame_test.cpp261
1 files changed, 261 insertions, 0 deletions
diff --git a/runtime/test/submit_frame_test.cpp b/runtime/test/submit_frame_test.cpp
index 71417da31..7450724b1 100644
--- a/runtime/test/submit_frame_test.cpp
+++ b/runtime/test/submit_frame_test.cpp
@@ -2016,6 +2016,267 @@ int queue_sync_and_async() {
2016 std::printf("queue r6 contention: 24-wait frame 0 room stalls, frame-group bound, held run_ns & visibility coalescing ok\n"); 2016 std::printf("queue r6 contention: 24-wait frame 0 room stalls, frame-group bound, held run_ns & visibility coalescing ok\n");
2017 } 2017 }
2018 2018
2019 // 13. Round 7 sync & texture upload optimizations:
2020 // (a) Per-fence vis_seq in QueueHolds + GPU-only VulkanQueue::signal() does NOT bump visibility_seq_
2021 // (stopping cross-fence & GPU-signal false invalidations while preserving CPU Fence::Signal visibility,
2022 // including across Wait -> Signal -> Execute);
2023 // (b) Unlocked QueueHolds::run_barriers() under barrier_mutex_: strict FIFO Present ordering while
2024 // concurrent cross-queue pump_ready() never stalls behind a blocked barrier, plus cancel_after() wait;
2025 // (c) Lazy title-upload semaphore in VulkanQueue::submit_multi() + submit_mutex_ unlocked during
2026 // write_staged_title_images() recording.
2027 {
2028 // (a) Per-fence vis_seq & GPU-only signal visibility isolation.
2029 VulkanContext context(d.device_proc, d.device, true);
2030 EXPECT(context.loaded());
2031 VulkanQueue direct(context, d.queue, d.family, VulkanQueue::QueueKind::direct);
2032 VulkanQueue compute(context, d.queue, d.family, VulkanQueue::QueueKind::compute);
2033 EXPECT(direct.ok() && compute.ok());
2034 direct.set_async_depth(4);
2035 compute.set_async_depth(4);
2036
2037 auto empty = std::make_shared<CommandList>();
2038 EXPECT(empty->close());
2039 std::shared_ptr<const CommandList> snap = empty;
2040
2041 // GPU-only queue Signal must NOT bump visibility_seq_.
2042 VulkanFence gpu_f(context, 0);
2043 EXPECT(gpu_f.semaphore());
2044 const std::uint64_t vis_before_gpu_sig = context.current_visibility_seq();
2045 EXPECT(direct.signal(gpu_f.semaphore(), 1));
2046 EXPECT(gpu_f.wait(1, 5'000'000'000ull));
2047 EXPECT(direct.idle());
2048 EXPECT(context.current_visibility_seq() == vis_before_gpu_sig);
2049 EXPECT(context.holds().signaled_vis_seq(gpu_f.semaphore(), 1) == 0);
2050
2051 // Unrelated CPU Fence::Signal on fence_a MUST NOT invalidate a held batch waiting on GPU-signaled fence_b.
2052 std::mutex coal_m;
2053 std::uint64_t coal_completed = 0;
2054 std::atomic<unsigned> coal_ran{0}, coal_skipped{0};
2055 std::atomic<int> cpu_val{0}, seen_val{0};
2056 context.set_before_execute([&] {
2057 const std::uint64_t req = VulkanContext::current_before_execute_seq();
2058 std::unique_lock lock(coal_m);
2059 if (req && coal_completed >= req) {
2060 coal_skipped.fetch_add(1, std::memory_order_relaxed);
2061 return;
2062 }
2063 const std::uint64_t pass = (std::max)(req, context.current_visibility_seq());
2064 lock.unlock();
2065 seen_val.store(cpu_val.load(std::memory_order_acquire), std::memory_order_release);
2066 coal_ran.fetch_add(1, std::memory_order_relaxed);
2067 lock.lock();
2068 if (pass > coal_completed) coal_completed = pass;
2069 });
2070
2071 VulkanFence fence_a(context, 0), fence_b(context, 0), comp_done(context, 0);
2072 EXPECT(fence_a.semaphore() && fence_b.semaphore() && comp_done.semaphore());
2073
2074 // Hold compute batch on GPU fence_b.
2075 compute.wait(fence_b.semaphore(), 1);
2076 EXPECT(compute.execute({&snap, 1}) == ReplayResult::success);
2077 EXPECT(compute.signal(comp_done.semaphore(), 1));
2078 EXPECT(compute.idle());
2079
2080 // Direct runs a pass advancing coal_completed past compute's enqueue visibility_seq.
2081 EXPECT(direct.execute({&snap, 1}) == ReplayResult::success);
2082 EXPECT(direct.idle());
2083 const unsigned ran_before = coal_ran.load(std::memory_order_acquire);
2084 const unsigned skipped_before = coal_skipped.load(std::memory_order_acquire);
2085
2086 // Unrelated CPU signal on fence_a bumps visibility_seq_ and fence_a's per-fence vis_seq ONLY.
2087 EXPECT(fence_a.signal(1));
2088 EXPECT(context.holds().signaled_vis_seq(fence_a.semaphore(), 1) > 0);
2089 EXPECT(context.holds().signaled_vis_seq(fence_b.semaphore(), 1) == 0);
2090
2091 // GPU-only signal on fence_b releases compute: must COALESCE (not re-run) because fence_b has vis_seq == 0!
2092 EXPECT(direct.signal(fence_b.semaphore(), 1));
2093 EXPECT(comp_done.wait(1, 5'000'000'000ull));
2094 EXPECT(direct.idle() && compute.idle());
2095 EXPECT(coal_ran.load(std::memory_order_acquire) == ran_before);
2096 EXPECT(coal_skipped.load(std::memory_order_acquire) == skipped_before + 1);
2097
2098 // Wait(cpu_f, 1) -> Signal(mid, 1) -> Execute(B2): CPU Fence::Signal on cpu_f MUST still lift B2's req_seq!
2099 VulkanFence cpu_f(context, 0), mid_f(context, 0), done_f(context, 0);
2100 EXPECT(cpu_f.semaphore() && mid_f.semaphore() && done_f.semaphore());
2101 compute.wait(cpu_f.semaphore(), 1);
2102 EXPECT(compute.signal(mid_f.semaphore(), 1));
2103 EXPECT(compute.execute({&snap, 1}) == ReplayResult::success);
2104 EXPECT(compute.signal(done_f.semaphore(), 1));
2105 EXPECT(compute.idle());
2106
2107 EXPECT(direct.execute({&snap, 1}) == ReplayResult::success);
2108 EXPECT(direct.idle());
2109 const unsigned ran_before_cpu = coal_ran.load(std::memory_order_acquire);
2110 cpu_val.store(777, std::memory_order_release);
2111 EXPECT(cpu_f.signal(1));
2112 EXPECT(done_f.wait(1, 5'000'000'000ull));
2113 EXPECT(compute.idle());
2114 EXPECT(coal_ran.load(std::memory_order_acquire) == ran_before_cpu + 1);
2115 EXPECT(seen_val.load(std::memory_order_acquire) == 777);
2116
2117 // (b) Unlocked QueueHolds::run_barriers(): strict FIFO order + concurrent pump_ready() unblocked + cancel_after.
2118 context.set_before_execute({});
2119 VulkanFence hold_b1(context, 0), hold_comp(context, 0), comp_b_done(context, 0);
2120 EXPECT(hold_b1.semaphore() && hold_comp.semaphore() && comp_b_done.semaphore());
2121
2122 direct.wait(hold_b1.semaphore(), 1);
2123 EXPECT(direct.execute({&snap, 1}) == ReplayResult::success);
2124 EXPECT(direct.idle());
2125
2126 std::mutex fifo_m;
2127 std::vector<int> fifo_order;
2128 std::atomic<bool> in_b1{false}, release_b1{false}, b3_ran{false};
2129 int owner_fifo = 0, owner_cancel = 0;
2130 context.holds().after_held(&owner_fifo, [&] {
2131 in_b1.store(true, std::memory_order_release);
2132 while (!release_b1.load(std::memory_order_acquire)) std::this_thread::yield();
2133 std::lock_guard lk(fifo_m);
2134 fifo_order.push_back(1);
2135 });
2136 context.holds().after_held(&owner_fifo, [&] {
2137 std::lock_guard lk(fifo_m);
2138 fifo_order.push_back(2);
2139 });
2140 context.holds().after_held(&owner_cancel, [&] { b3_ran.store(true, std::memory_order_release); });
2141
2142 // Hold an independent compute batch behind hold_comp.
2143 compute.wait(hold_comp.semaphore(), 1);
2144 EXPECT(compute.execute({&snap, 1}) == ReplayResult::success);
2145 EXPECT(compute.signal(comp_b_done.semaphore(), 1));
2146 EXPECT(compute.idle());
2147
2148 // Release direct -> starts barrier 1 (blocks on release_b1).
2149 std::thread sig_b1([&] { (void)hold_b1.signal(1); });
2150 while (!in_b1.load(std::memory_order_acquire)) std::this_thread::yield();
2151
2152 // While barrier 1 is blocked inside barrier_mutex_, pump_mutex_ MUST be free so hold_comp.signal(1)
2153 // releases compute and completes comp_b_done immediately without stalling!
2154 EXPECT(hold_comp.signal(1));
2155 EXPECT(comp_b_done.wait(1, 2'000'000'000ull));
2156 EXPECT(compute.idle());
2157
2158 // Cancel owner_cancel while barrier 1 is running: b3 must be dropped and never run!
2159 std::atomic<bool> cancel_done{false};
2160 std::thread canceller([&] {
2161 context.holds().cancel_after(&owner_cancel);
2162 cancel_done.store(true, std::memory_order_release);
2163 });
2164 std::this_thread::sleep_for(std::chrono::milliseconds(10));
2165 // cancel_after waits for barrier_mutex_ (running barrier) before returning!
2166 EXPECT(!cancel_done.load(std::memory_order_acquire));
2167
2168 release_b1.store(true, std::memory_order_release);
2169 sig_b1.join();
2170 canceller.join();
2171 EXPECT(direct.idle());
2172 EXPECT(cancel_done.load(std::memory_order_acquire));
2173 EXPECT(!b3_ran.load(std::memory_order_acquire));
2174 EXPECT((fifo_order == std::vector<int>{1, 2}));
2175
2176 // (c) Lazy title-upload semaphore in submit_multi() + submit_mutex_ unlocked during write_staged_title_images()
2177 // recording.
2178 static PFN_vkQueueSubmit s_r7_real_submit = nullptr;
2179 static VkSemaphore s_r7_watch_sem = VK_NULL_HANDLE;
2180 static std::atomic<unsigned> s_r7_wait_hits{0};
2181 static std::atomic<std::uint64_t> s_r7_last_wait_val{0};
2182 struct R7HookSubmit {
2183 static VKAPI_ATTR VkResult VKAPI_CALL call(VkQueue q, std::uint32_t count, const VkSubmitInfo* infos,
2184 VkFence fence) {
2185 for (std::uint32_t i = 0; i < count; ++i) {
2186 const auto* tl = static_cast<const VkTimelineSemaphoreSubmitInfo*>(infos[i].pNext);
2187 for (std::uint32_t w = 0; w < infos[i].waitSemaphoreCount; ++w) {
2188 if (infos[i].pWaitSemaphores[w] == s_r7_watch_sem) {
2189 s_r7_wait_hits.fetch_add(1, std::memory_order_relaxed);
2190 if (tl && tl->sType == VK_STRUCTURE_TYPE_TIMELINE_SEMAPHORE_SUBMIT_INFO &&
2191 w < tl->waitSemaphoreValueCount) {
2192 s_r7_last_wait_val.store(tl->pWaitSemaphoreValues[w], std::memory_order_relaxed);
2193 }
2194 }
2195 }
2196 }
2197 return s_r7_real_submit(q, count, infos, fence);
2198 }
2199 };
2200
2201 VulkanContext ctx_up(d.device_proc, d.device, true);
2202 EXPECT(ctx_up.loaded());
2203 VulkanFence upload_tl(ctx_up, 0);
2204 EXPECT(upload_tl.semaphore());
2205 s_r7_real_submit = ctx_up.vk.vkQueueSubmit;
2206 s_r7_watch_sem = upload_tl.semaphore();
2207 s_r7_wait_hits.store(0, std::memory_order_relaxed);
2208 s_r7_last_wait_val.store(0, std::memory_order_relaxed);
2209 ctx_up.vk.vkQueueSubmit = &R7HookSubmit::call;
2210
2211 VulkanQueue q_dir(ctx_up, d.queue, d.family, VulkanQueue::QueueKind::direct);
2212 VulkanQueue q_cmp(ctx_up, d.queue, d.family, VulkanQueue::QueueKind::compute);
2213 EXPECT(q_dir.ok() && q_cmp.ok());
2214 q_dir.set_async_depth(0);
2215 q_cmp.set_async_depth(0);
2216
2217 // Register a dummy image so write_staged_title_images() exercises its split record/submit path.
2218 ResourceBinding img_b{};
2219 img_b.image = reinterpret_cast<VkImage>(0x1234);
2220 img_b.aspects = VK_IMAGE_ASPECT_COLOR_BIT;
2221 img_b.mip_levels = 1;
2222 img_b.array_layers = 1;
2223 ctx_up.add_resource(99, img_b, VK_IMAGE_LAYOUT_UNDEFINED);
2224
2225 bool submit_unlocked_in_record = false;
2226 bool submit_locked_in_submit = false;
2227 const VulkanContext::StagedImageTarget target{99, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1}};
2228 const VkResult staged_r = ctx_up.write_staged_title_images(
2229 {&target, 1},
2230 [&](const ReplayFunctions&, const std::vector<LayoutPieces>& pieces) -> VkResult {
2231 if (pieces.size() != 1 || pieces[0].empty()) return VK_ERROR_UNKNOWN;
2232 // submit_mutex_ MUST be unlocked during command recording!
2233 if (ctx_up.submit_mutex().try_lock()) {
2234 submit_unlocked_in_record = true;
2235 ctx_up.submit_mutex().unlock();
2236 }
2237 return VK_SUCCESS;
2238 },
2239 [&](const auto&) -> VkResult {
2240 // submit_mutex_ MUST be held during submit callback!
2241 submit_locked_in_submit = !ctx_up.submit_mutex().try_lock();
2242 ctx_up.note_title_upload_locked(upload_tl.semaphore(), 1);
2243 return VK_SUCCESS;
2244 });
2245 EXPECT(staged_r == VK_SUCCESS);
2246 EXPECT(submit_unlocked_in_record && submit_locked_in_submit);
2247 EXPECT(ctx_up.layout(99, 0) == VK_IMAGE_LAYOUT_GENERAL);
2248
2249 // Signal upload_tl to 1 so GPU waits complete immediately.
2250 EXPECT(upload_tl.signal(1));
2251
2252 // A signal-only submit on q_dir must NOT consume title_upload_seen_.
2253 VulkanFence sig_only(ctx_up, 0);
2254 EXPECT(sig_only.semaphore());
2255 EXPECT(q_dir.signal(sig_only.semaphore(), 1));
2256 EXPECT(sig_only.wait(1, 5'000'000'000ull));
2257 EXPECT(s_r7_wait_hits.load(std::memory_order_acquire) == 0);
2258
2259 // First command-buffer submit on q_dir MUST wait on (upload_tl, 1); second MUST NOT wait redundantly!
2260 EXPECT(q_dir.execute({&snap, 1}) == ReplayResult::success);
2261 EXPECT(s_r7_wait_hits.load(std::memory_order_acquire) == 1);
2262 EXPECT(s_r7_last_wait_val.load(std::memory_order_acquire) == 1);
2263 EXPECT(q_dir.execute({&snap, 1}) == ReplayResult::success);
2264 EXPECT(s_r7_wait_hits.load(std::memory_order_acquire) == 1);
2265
2266 // First command-buffer submit on q_cmp MUST ALSO wait on (upload_tl, 1) independently!
2267 EXPECT(q_cmp.execute({&snap, 1}) == ReplayResult::success);
2268 EXPECT(s_r7_wait_hits.load(std::memory_order_acquire) == 2);
2269 EXPECT(q_cmp.execute({&snap, 1}) == ReplayResult::success);
2270 EXPECT(s_r7_wait_hits.load(std::memory_order_acquire) == 2);
2271
2272 {
2273 std::lock_guard lk(ctx_up.submit_mutex());
2274 ctx_up.clear_title_upload_locked(upload_tl.semaphore());
2275 }
2276 EXPECT(q_dir.idle() && q_cmp.idle());
2277 std::printf("queue r7 sync-tex: per-fence vis_seq, unlocked run_barriers FIFO, lazy title_upload wait & unlocked record ok\n");
2278 }
2279
2019 return 0; 2280 return 0;
2020} 2281}
2021 2282