diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 22:20:36 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 22:20:36 +0100 |
| commit | 48cee7c2c69e502cc864b0789a9cf5bb88e98658 (patch) | |
| tree | 52fd1d94d86ee5fe479111579e48c696e18d42cf /runtime/src/vulkan_present.h | |
| parent | 0d75876278a3f6b238a8667cf42c66cf1ae1d99a (diff) | |
Queue holds: second review fixes (claim after submit, presenter locking and bound, teardown pump, tests)
- A Signal counts as submitted only after vkQueueSubmit succeeded: a failed submit releases no waiter and the retry goes through.
- ~VulkanQueue pumps after dropping held work; fence keep-alives are also drained in wait() and marker().
- VulkanPresenter: counters are atomics, read_back/enable_capture take the presenter mutex, set_abort has its own mutex,
a deferred present reports not tearing, and at most 8 presents queue up before the caller waits.
- Tests: three presents (blit, blank, composed) queued behind held work run in order, a present behind a destroyed
queue's held work runs, a failed Signal does not release its waiters; the handover modes also run on the host runtime.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/vulkan_present.h')
| -rw-r--r-- | runtime/src/vulkan_present.h | 69 |
1 files changed, 59 insertions, 10 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 4e35774c4..8c3329c1e 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -35,6 +35,8 @@ | |||
| 35 | 35 | ||
| 36 | #include <algorithm> | 36 | #include <algorithm> |
| 37 | #include <array> | 37 | #include <array> |
| 38 | #include <atomic> | ||
| 39 | #include <chrono> | ||
| 38 | #include <cmath> | 40 | #include <cmath> |
| 39 | #include <cstdint> | 41 | #include <cstdint> |
| 40 | #include <cstdio> | 42 | #include <cstdio> |
| @@ -44,6 +46,7 @@ | |||
| 44 | #include <deque> | 46 | #include <deque> |
| 45 | #include <memory> | 47 | #include <memory> |
| 46 | #include <mutex> | 48 | #include <mutex> |
| 49 | #include <thread> | ||
| 47 | #include <vector> | 50 | #include <vector> |
| 48 | 51 | ||
| 49 | namespace leonida { | 52 | namespace leonida { |
| @@ -181,7 +184,10 @@ public: | |||
| 181 | VulkanPresenter& operator=(const VulkanPresenter&) = delete; | 184 | VulkanPresenter& operator=(const VulkanPresenter&) = delete; |
| 182 | 185 | ||
| 183 | bool ok() const { return ok_; } | 186 | bool ok() const { return ok_; } |
| 184 | VkExtent2D extent() const { return extent_; } | 187 | VkExtent2D extent() const { |
| 188 | const std::uint64_t packed = extent_packed_; | ||
| 189 | return {std::uint32_t(packed >> 32), std::uint32_t(packed)}; | ||
| 190 | } | ||
| 185 | VkFormat format() const { return format_; } | 191 | VkFormat format() const { return format_; } |
| 186 | std::uint64_t presented() const { return presented_; } | 192 | std::uint64_t presented() const { return presented_; } |
| 187 | // The present mode a tearing present uses (IMMEDIATE, else FIFO_RELAXED) while the current swapchain was created | 193 | // The present mode a tearing present uses (IMMEDIATE, else FIFO_RELAXED) while the current swapchain was created |
| @@ -189,6 +195,7 @@ public: | |||
| 189 | VkPresentModeKHR tear_mode() const { return tear_mode_; } | 195 | VkPresentModeKHR tear_mode() const { return tear_mode_; } |
| 190 | bool tear_supported() const { return tear_mode_ != VK_PRESENT_MODE_MAX_ENUM_KHR; } | 196 | bool tear_supported() const { return tear_mode_ != VK_PRESENT_MODE_MAX_ENUM_KHR; } |
| 191 | // Whether the last present was queued in the tearing mode, and how many were. | 197 | // Whether the last present was queued in the tearing mode, and how many were. |
| 198 | // A present queued behind held work (see submit()) is reported as not tearing. | ||
| 192 | bool last_present_tore() const { return last_tore_; } | 199 | bool last_present_tore() const { return last_tore_; } |
| 193 | std::uint64_t tear_presents() const { return tear_presents_; } | 200 | std::uint64_t tear_presents() const { return tear_presents_; } |
| 194 | 201 | ||
| @@ -272,7 +279,10 @@ public: | |||
| 272 | 279 | ||
| 273 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with | 280 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with |
| 274 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. | 281 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. |
| 275 | void set_abort(std::function<bool()> abort) { abort_ = std::move(abort); } | 282 | void set_abort(std::function<bool()> abort) { |
| 283 | std::lock_guard lock(abort_mutex_); | ||
| 284 | abort_ = std::move(abort); | ||
| 285 | } | ||
| 276 | 286 | ||
| 277 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. | 287 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. |
| 278 | VkResult present_blank(bool vsync, bool tear = false) { | 288 | VkResult present_blank(bool vsync, bool tear = false) { |
| @@ -283,6 +293,7 @@ public: | |||
| 283 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. | 293 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. |
| 284 | // Call before the first present. | 294 | // Call before the first present. |
| 285 | bool enable_capture() { | 295 | bool enable_capture() { |
| 296 | std::lock_guard lock(present_mutex_); | ||
| 286 | if (!ok_) return false; | 297 | if (!ok_) return false; |
| 287 | capture_ = true; | 298 | capture_ = true; |
| 288 | stale_ = true; | 299 | stale_ = true; |
| @@ -296,6 +307,7 @@ public: | |||
| 296 | // The last presented frame as the swapchain stores it (4 bytes a texel, rows tightly packed, format() order). | 307 | // The last presented frame as the swapchain stores it (4 bytes a texel, rows tightly packed, format() order). |
| 297 | // Waits for the frame to finish. False if no captured frame is available or the wait fails. | 308 | // Waits for the frame to finish. False if no captured frame is available or the wait fails. |
| 298 | bool read_back(std::vector<std::uint8_t>& pixels, VkExtent2D* extent, VkFormat* format) { | 309 | bool read_back(std::vector<std::uint8_t>& pixels, VkExtent2D* extent, VkFormat* format) { |
| 310 | std::lock_guard lock(present_mutex_); | ||
| 299 | if (!capture_ || !capture_ptr_ || !captured_) return false; | 311 | if (!capture_ || !capture_ptr_ || !captured_) return false; |
| 300 | const Frame& frame = frames_[(frame_index_ + frames_.size() - 1) % frames_.size()]; | 312 | const Frame& frame = frames_[(frame_index_ + frames_.size() - 1) % frames_.size()]; |
| 301 | if (vkWaitForFences_(context_.device(), 1, &frame.done, VK_TRUE, UINT64_MAX) != VK_SUCCESS) | 313 | if (vkWaitForFences_(context_.device(), 1, &frame.done, VK_TRUE, UINT64_MAX) != VK_SUCCESS) |
| @@ -315,9 +327,15 @@ public: | |||
| 315 | // (its failure is printed there). Held work that is never released holds back the presents after it, as it | 327 | // (its failure is printed there). Held work that is never released holds back the presents after it, as it |
| 316 | // holds back the queue's own work; with nothing held this is the plain path. A blank present reads nothing and | 328 | // holds back the queue's own work; with nothing held this is the plain path. A blank present reads nothing and |
| 317 | // is only ordered behind earlier presents. Waiting instead was rejected: the Signal that releases the work may | 329 | // is only ordered behind earlier presents. Waiting instead was rejected: the Signal that releases the work may |
| 318 | // be one the application's own thread makes after Present returns. | 330 | // be one the application's own thread makes after Present returns. Like a full frame-latency queue, a Present that |
| 331 | // finds max_deferred_presents queued ahead of it does wait, for held work to release (logged once; set_abort ends | ||
| 332 | // the wait), so the queue behind never-released work is bounded. A queued Present reports its tear as false | ||
| 333 | // (last_present_tore()): the frame pacer's default sync. | ||
| 334 | static constexpr std::size_t max_deferred_presents = 8; | ||
| 335 | |||
| 319 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | 336 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, |
| 320 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { | 337 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { |
| 338 | wait_for_room(); | ||
| 321 | std::lock_guard lock(present_mutex_); | 339 | std::lock_guard lock(present_mutex_); |
| 322 | // `binding` null and `planes` null: clear instead of blit; `planes`: compose them (`binding`, `source`, | 340 | // `binding` null and `planes` null: clear instead of blit; `planes`: compose them (`binding`, `source`, |
| 323 | // `source_rect` unused). Only a present that reads a texture waits for held work. | 341 | // `source_rect` unused). Only a present that reads a texture waits for held work. |
| @@ -328,11 +346,39 @@ public: | |||
| 328 | if (source_rect) pending.rect = *source_rect; | 346 | if (source_rect) pending.rect = *source_rect; |
| 329 | if (planes) pending.planes = *planes; | 347 | if (planes) pending.planes = *planes; |
| 330 | deferred_.push_back(pending); | 348 | deferred_.push_back(pending); |
| 349 | last_tore_ = false; | ||
| 331 | context_.holds().after_held(this, [this] { run_deferred(); }); | 350 | context_.holds().after_held(this, [this] { run_deferred(); }); |
| 332 | return VK_SUCCESS; | 351 | return VK_SUCCESS; |
| 333 | } | 352 | } |
| 334 | 353 | ||
| 335 | private: | 354 | private: |
| 355 | bool should_abort() { | ||
| 356 | std::function<bool()> abort; | ||
| 357 | { | ||
| 358 | std::lock_guard lock(abort_mutex_); | ||
| 359 | abort = abort_; | ||
| 360 | } | ||
| 361 | return abort && abort(); | ||
| 362 | } | ||
| 363 | |||
| 364 | // Called without present_mutex_: the thread that releases the queued presents needs it. | ||
| 365 | void wait_for_room() { | ||
| 366 | bool logged = false; | ||
| 367 | for (;;) { | ||
| 368 | { | ||
| 369 | std::lock_guard lock(present_mutex_); | ||
| 370 | if (deferred_.size() < max_deferred_presents) return; | ||
| 371 | } | ||
| 372 | if (!logged) { | ||
| 373 | logged = true; | ||
| 374 | std::fprintf(stderr, "[rt] d3d12: %zu Presents are queued behind held work; this one waits for it\n", | ||
| 375 | max_deferred_presents); | ||
| 376 | } | ||
| 377 | if (should_abort()) return; | ||
| 378 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 336 | struct PendingPresent { | 382 | struct PendingPresent { |
| 337 | bool has_source; | 383 | bool has_source; |
| 338 | ResourceId source; | 384 | ResourceId source; |
| @@ -777,7 +823,7 @@ private: | |||
| 777 | const VkResult result = vkAcquireNextImageKHR_(context_.device(), swapchain_, 50'000'000, semaphore, | 823 | const VkResult result = vkAcquireNextImageKHR_(context_.device(), swapchain_, 50'000'000, semaphore, |
| 778 | VK_NULL_HANDLE, image); | 824 | VK_NULL_HANDLE, image); |
| 779 | if (result != VK_TIMEOUT && result != VK_NOT_READY) return result; | 825 | if (result != VK_TIMEOUT && result != VK_NOT_READY) return result; |
| 780 | if (abort_ && abort_()) return VK_NOT_READY; | 826 | if (should_abort()) return VK_NOT_READY; |
| 781 | } | 827 | } |
| 782 | } | 828 | } |
| 783 | 829 | ||
| @@ -1057,6 +1103,7 @@ private: | |||
| 1057 | ready_ = std::move(ready); | 1103 | ready_ = std::move(ready); |
| 1058 | format_ = chosen.format; | 1104 | format_ = chosen.format; |
| 1059 | extent_ = extent; | 1105 | extent_ = extent; |
| 1106 | extent_packed_ = (std::uint64_t(extent.width) << 32) | extent.height; | ||
| 1060 | hdr_active_ = chose_hdr; | 1107 | hdr_active_ = chose_hdr; |
| 1061 | if (capture_) { | 1108 | if (capture_) { |
| 1062 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. | 1109 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. |
| @@ -1140,13 +1187,15 @@ private: | |||
| 1140 | VkInstance instance_; | 1187 | VkInstance instance_; |
| 1141 | VkPhysicalDevice physical_; | 1188 | VkPhysicalDevice physical_; |
| 1142 | VkQueue queue_; | 1189 | VkQueue queue_; |
| 1143 | std::recursive_mutex present_mutex_; // submit() and a deferred present on the releasing thread | 1190 | mutable std::recursive_mutex present_mutex_; // all presenter state: submit(), a deferred present, the accessors |
| 1191 | mutable std::mutex abort_mutex_; // abort_ alone: set_abort() must work while a present waits | ||
| 1144 | std::deque<PendingPresent> deferred_; // presents queued behind held work, in call order | 1192 | std::deque<PendingPresent> deferred_; // presents queued behind held work, in call order |
| 1145 | VkSurfaceKHR surface_; | 1193 | VkSurfaceKHR surface_; |
| 1146 | bool maintenance1_ = false; // VK_KHR/EXT_swapchain_maintenance1 (and the surface extension) are enabled | 1194 | bool maintenance1_ = false; // VK_KHR/EXT_swapchain_maintenance1 (and the surface extension) are enabled |
| 1147 | VkPresentModeKHR tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; // set with the swapchain, see tear_mode() | 1195 | VkPresentModeKHR tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; // set with the swapchain, see tear_mode() |
| 1148 | bool last_tore_ = false; | 1196 | std::atomic<bool> last_tore_{false}; |
| 1149 | std::uint64_t tear_presents_ = 0; | 1197 | std::atomic<std::uint64_t> extent_packed_{0}; // extent_ for readers that must not wait for a present |
| 1198 | std::atomic<std::uint64_t> tear_presents_{0}; | ||
| 1150 | VkSwapchainKHR swapchain_ = VK_NULL_HANDLE; | 1199 | VkSwapchainKHR swapchain_ = VK_NULL_HANDLE; |
| 1151 | VkFormat format_ = VK_FORMAT_UNDEFINED; | 1200 | VkFormat format_ = VK_FORMAT_UNDEFINED; |
| 1152 | VkExtent2D extent_{}; | 1201 | VkExtent2D extent_{}; |
| @@ -1155,15 +1204,15 @@ private: | |||
| 1155 | std::array<Frame, 2> frames_{}; | 1204 | std::array<Frame, 2> frames_{}; |
| 1156 | VkCommandPool pool_ = VK_NULL_HANDLE; | 1205 | VkCommandPool pool_ = VK_NULL_HANDLE; |
| 1157 | std::uint64_t frame_index_ = 0; | 1206 | std::uint64_t frame_index_ = 0; |
| 1158 | std::uint64_t presented_ = 0; | 1207 | std::atomic<std::uint64_t> presented_{0}; |
| 1159 | bool vsync_ = true; | 1208 | bool vsync_ = true; |
| 1160 | bool stale_ = false; | 1209 | bool stale_ = false; |
| 1161 | bool ok_ = false; | 1210 | bool ok_ = false; |
| 1162 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) | 1211 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) |
| 1163 | bool swapchain_hdr_req_ = false; // the swapchain was built for an HDR10 request (it has the format only if hdr_active_) | 1212 | bool swapchain_hdr_req_ = false; // the swapchain was built for an HDR10 request (it has the format only if hdr_active_) |
| 1164 | bool hdr_active_ = false; | 1213 | bool hdr_active_ = false; |
| 1165 | std::uint64_t composed_ = 0; | 1214 | std::atomic<std::uint64_t> composed_{0}; |
| 1166 | bool force_hdr_encoding_ = false; | 1215 | std::atomic<bool> force_hdr_encoding_{false}; |
| 1167 | bool warned_hdr_ = false; | 1216 | bool warned_hdr_ = false; |
| 1168 | std::unique_ptr<PresentComposer> composer_; | 1217 | std::unique_ptr<PresentComposer> composer_; |
| 1169 | std::function<bool()> abort_; | 1218 | std::function<bool()> abort_; |