diff options
| author | tedin7 <tommasoderosa94@gmail.com> | 2026-11-23 01:10:49 +0100 |
|---|---|---|
| committer | tedin7 <tommasoderosa94@gmail.com> | 2026-11-23 01:10:49 +0100 |
| commit | a3481c0a1870b1037085d71a32d4bfecc5bf9db7 (patch) | |
| tree | 947f5146e955bdbdefe2e6bc27765f30d6469865 /runtime/src | |
| parent | 2e8eed4b285010fc3b5decddafbc8040f21d3e60 (diff) | |
runtime: keep the command-buffer cap when the wait for room times out (review)
A timeout retries the wait in kQueuedWaitNs slices instead of allocating past the cap; a device lost while waiting makes execute() submit nothing. vk_queue_test makes the first three waits time out and checks the cap still holds.
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 15cda116c..76503af73 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1443,8 +1443,9 @@ public: | |||
| 1443 | enum class QueueKind : std::uint8_t { direct, compute, copy }; | 1443 | enum class QueueKind : std::uint8_t { direct, compute, copy }; |
| 1444 | 1444 | ||
| 1445 | // The most command buffers a queue holds at once. At that many, a caller that outruns the GPU waits for the oldest | 1445 | // The most command buffers a queue holds at once. At that many, a caller that outruns the GPU waits for the oldest |
| 1446 | // submitted one to finish (before taking any context lock, see wait_for_room) instead of allocating without bound. A GPU that makes no progress for kQueuedWaitNs does | 1446 | // submitted one to finish (before taking any context lock, see wait_for_room) instead of allocating without bound. The |
| 1447 | // not block the caller for good: the buffer is allocated anyway, as it was before the cap. | 1447 | // cap holds while the GPU is slow too: a wait that times out is retried, in kQueuedWaitNs slices so a device lost |
| 1448 | // meanwhile ends it, and a device lost makes execute() submit nothing. | ||
| 1448 | static constexpr std::size_t kMaxQueuedCommandBuffers = 64; | 1449 | static constexpr std::size_t kMaxQueuedCommandBuffers = 64; |
| 1449 | static constexpr std::uint64_t kQueuedWaitNs = 5'000'000'000; | 1450 | static constexpr std::uint64_t kQueuedWaitNs = 5'000'000'000; |
| 1450 | 1451 | ||
| @@ -1647,8 +1648,7 @@ public: | |||
| 1647 | 1648 | ||
| 1648 | private: | 1649 | private: |
| 1649 | ReplayResult execute_now(std::span<const CommandList* const> lists) { | 1650 | ReplayResult execute_now(std::span<const CommandList* const> lists) { |
| 1650 | if (context_.is_device_lost()) return ReplayResult::unsupported; | 1651 | if (context_.is_device_lost() || !wait_for_room()) return ReplayResult::unsupported; |
| 1651 | wait_for_room(); | ||
| 1652 | if (kind_ != QueueKind::direct) | 1652 | if (kind_ != QueueKind::direct) |
| 1653 | for (const auto* list : lists) | 1653 | for (const auto* list : lists) |
| 1654 | for (const auto& command : list->commands()) | 1654 | for (const auto& command : list->commands()) |
| @@ -2236,25 +2236,40 @@ private: | |||
| 2236 | // A queue at the cap with nothing finished waits here for its oldest submission. execute_now() calls it before it | 2236 | // A queue at the cap with nothing finished waits here for its oldest submission. execute_now() calls it before it |
| 2237 | // takes resource_mutex_ or tracker_mutex_: the submission waited for may itself wait on another queue's work, and | 2237 | // takes resource_mutex_ or tracker_mutex_: the submission waited for may itself wait on another queue's work, and |
| 2238 | // that queue needs those locks to submit it. One execute runs at a time on a queue, so buffers_ is this caller's. | 2238 | // that queue needs those locks to submit it. One execute runs at a time on a queue, so buffers_ is this caller's. |
| 2239 | void wait_for_room() { | 2239 | // A timeout only means the GPU is slow: wait again rather than allocate past the cap. False when the device is lost |
| 2240 | if (buffers_.size() < kMaxQueuedCommandBuffers) return; | 2240 | // (now or while waiting), so the caller submits nothing. |
| 2241 | const auto done = completed(); | 2241 | bool wait_for_room() { |
| 2242 | for (const auto& slot : buffers_) | 2242 | for (;;) { |
| 2243 | if (slot.ticket <= done) return; | 2243 | if (context_.is_device_lost()) return false; |
| 2244 | (void)wait_oldest_submitted(); | 2244 | if (buffers_.size() < kMaxQueuedCommandBuffers) return true; |
| 2245 | const auto done = completed(); | ||
| 2246 | for (const auto& slot : buffers_) | ||
| 2247 | if (slot.ticket <= done) return true; | ||
| 2248 | switch (wait_oldest_submitted()) { | ||
| 2249 | case VK_SUCCESS: | ||
| 2250 | return true; | ||
| 2251 | case VK_TIMEOUT: | ||
| 2252 | continue; | ||
| 2253 | case VK_ERROR_DEVICE_LOST: | ||
| 2254 | context_.mark_device_lost(); | ||
| 2255 | return false; | ||
| 2256 | default: | ||
| 2257 | return true; // nothing submitted to wait for (or a host error): no wait can make room, allocate | ||
| 2258 | } | ||
| 2259 | } | ||
| 2245 | } | 2260 | } |
| 2246 | 2261 | ||
| 2247 | // Waits for the oldest submitted command buffer. False when none is submitted yet, or it does not finish in time. | 2262 | // Waits up to kQueuedWaitNs for the oldest submitted command buffer. VK_INCOMPLETE when none is submitted yet. |
| 2248 | bool wait_oldest_submitted() { | 2263 | VkResult wait_oldest_submitted() { |
| 2249 | std::uint64_t oldest = UINT64_MAX; | 2264 | std::uint64_t oldest = UINT64_MAX; |
| 2250 | for (const auto& slot : buffers_) | 2265 | for (const auto& slot : buffers_) |
| 2251 | if (slot.ticket < oldest) oldest = slot.ticket; | 2266 | if (slot.ticket < oldest) oldest = slot.ticket; |
| 2252 | if (oldest == UINT64_MAX) return false; // all still being recorded | 2267 | if (oldest == UINT64_MAX) return VK_INCOMPLETE; // all still being recorded |
| 2253 | VkSemaphoreWaitInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO}; | 2268 | VkSemaphoreWaitInfo info{VK_STRUCTURE_TYPE_SEMAPHORE_WAIT_INFO}; |
| 2254 | info.semaphoreCount = 1; | 2269 | info.semaphoreCount = 1; |
| 2255 | info.pSemaphores = &progress_; | 2270 | info.pSemaphores = &progress_; |
| 2256 | info.pValues = &oldest; | 2271 | info.pValues = &oldest; |
| 2257 | return context_.vk.vkWaitSemaphores(context_.device(), &info, kQueuedWaitNs) == VK_SUCCESS; | 2272 | return context_.vk.vkWaitSemaphores(context_.device(), &info, kQueuedWaitNs); |
| 2258 | } | 2273 | } |
| 2259 | 2274 | ||
| 2260 | CommandSlot* acquire() { | 2275 | CommandSlot* acquire() { |