diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:06:28 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 23:06:28 -0500 |
| commit | 95b2e2c8bd7d20019231eda0007628fa2f2b5350 (patch) | |
| tree | 588294b0fe61c6a9d21ada1f56277d000a4d47d6 /runtime/src/vulkan_present.h | |
| parent | 1af1f9e39344ca73bfec9a0e3536fa4254a00b13 (diff) | |
| parent | 003baa94c1fcba61e324dbeb947e8bd401f4f837 (diff) | |
Merge pull request #2951 from AlxcBTW
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 | 125 |
1 files changed, 115 insertions, 10 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index a29cce3fa..deeb3ca5c 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -35,13 +35,18 @@ | |||
| 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> |
| 41 | #include <cstring> | 43 | #include <cstring> |
| 42 | #include <functional> | 44 | #include <functional> |
| 43 | #include <map> | 45 | #include <map> |
| 46 | #include <deque> | ||
| 44 | #include <memory> | 47 | #include <memory> |
| 48 | #include <mutex> | ||
| 49 | #include <thread> | ||
| 45 | #include <vector> | 50 | #include <vector> |
| 46 | 51 | ||
| 47 | namespace leonida { | 52 | namespace leonida { |
| @@ -161,6 +166,7 @@ public: | |||
| 161 | } | 166 | } |
| 162 | 167 | ||
| 163 | ~VulkanPresenter() { | 168 | ~VulkanPresenter() { |
| 169 | context_.holds().cancel_after(this); // presents still queued behind held work are dropped | ||
| 164 | auto& vk = context_.vk; | 170 | auto& vk = context_.vk; |
| 165 | const VkDevice device = context_.device(); | 171 | const VkDevice device = context_.device(); |
| 166 | { | 172 | { |
| @@ -186,7 +192,10 @@ public: | |||
| 186 | VulkanPresenter& operator=(const VulkanPresenter&) = delete; | 192 | VulkanPresenter& operator=(const VulkanPresenter&) = delete; |
| 187 | 193 | ||
| 188 | bool ok() const { return ok_; } | 194 | bool ok() const { return ok_; } |
| 189 | VkExtent2D extent() const { return extent_; } | 195 | VkExtent2D extent() const { |
| 196 | const std::uint64_t packed = extent_packed_; | ||
| 197 | return {std::uint32_t(packed >> 32), std::uint32_t(packed)}; | ||
| 198 | } | ||
| 190 | VkFormat format() const { return format_; } | 199 | VkFormat format() const { return format_; } |
| 191 | std::uint64_t presented() const { return presented_; } | 200 | std::uint64_t presented() const { return presented_; } |
| 192 | // The present mode a tearing present uses (IMMEDIATE, else FIFO_RELAXED) while the current swapchain was created | 201 | // The present mode a tearing present uses (IMMEDIATE, else FIFO_RELAXED) while the current swapchain was created |
| @@ -194,6 +203,7 @@ public: | |||
| 194 | VkPresentModeKHR tear_mode() const { return tear_mode_; } | 203 | VkPresentModeKHR tear_mode() const { return tear_mode_; } |
| 195 | bool tear_supported() const { return tear_mode_ != VK_PRESENT_MODE_MAX_ENUM_KHR; } | 204 | bool tear_supported() const { return tear_mode_ != VK_PRESENT_MODE_MAX_ENUM_KHR; } |
| 196 | // Whether the last present was queued in the tearing mode, and how many were. | 205 | // Whether the last present was queued in the tearing mode, and how many were. |
| 206 | // A present queued behind held work (see submit()) is reported as not tearing. | ||
| 197 | bool last_present_tore() const { return last_tore_; } | 207 | bool last_present_tore() const { return last_tore_; } |
| 198 | std::uint64_t tear_presents() const { return tear_presents_; } | 208 | std::uint64_t tear_presents() const { return tear_presents_; } |
| 199 | 209 | ||
| @@ -279,7 +289,10 @@ public: | |||
| 279 | 289 | ||
| 280 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with | 290 | // Polled while a present waits for a swapchain image (every 50 ms): when it returns true the present gives up with |
| 281 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. | 291 | // VK_NOT_READY, so a caller that is being suspended is never held by a window that stopped taking frames. |
| 282 | void set_abort(std::function<bool()> abort) { abort_ = std::move(abort); } | 292 | void set_abort(std::function<bool()> abort) { |
| 293 | std::lock_guard lock(abort_mutex_); | ||
| 294 | abort_ = std::move(abort); | ||
| 295 | } | ||
| 283 | 296 | ||
| 284 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. | 297 | // Presents a black frame: the swapchain image cleared, nothing read. PresentX with no planes. |
| 285 | VkResult present_blank(bool vsync, bool tear = false) { | 298 | VkResult present_blank(bool vsync, bool tear = false) { |
| @@ -290,6 +303,7 @@ public: | |||
| 290 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. | 303 | // read_back(). False when the surface cannot do it or its format is not 8 bits per channel with 4 channels. |
| 291 | // Call before the first present. | 304 | // Call before the first present. |
| 292 | bool enable_capture() { | 305 | bool enable_capture() { |
| 306 | std::lock_guard lock(present_mutex_); | ||
| 293 | if (!ok_) return false; | 307 | if (!ok_) return false; |
| 294 | capture_ = true; | 308 | capture_ = true; |
| 295 | stale_ = true; | 309 | stale_ = true; |
| @@ -305,6 +319,7 @@ public: | |||
| 305 | // that was skipped because the window is minimised (it returned VK_SUCCESS but showed nothing), until the next | 319 | // that was skipped because the window is minimised (it returned VK_SUCCESS but showed nothing), until the next |
| 306 | // frame that is shown (the frame before it is not what the window was last asked to show), or when the wait fails. | 320 | // frame that is shown (the frame before it is not what the window was last asked to show), or when the wait fails. |
| 307 | bool read_back(std::vector<std::uint8_t>& pixels, VkExtent2D* extent, VkFormat* format) { | 321 | bool read_back(std::vector<std::uint8_t>& pixels, VkExtent2D* extent, VkFormat* format) { |
| 322 | std::lock_guard lock(present_mutex_); | ||
| 308 | if (!capture_ || !capture_ptr_ || !captured_) return false; | 323 | if (!capture_ || !capture_ptr_ || !captured_) return false; |
| 309 | const Frame& frame = frames_[(frame_index_ + frames_.size() - 1) % frames_.size()]; | 324 | const Frame& frame = frames_[(frame_index_ + frames_.size() - 1) % frames_.size()]; |
| 310 | if (vkWaitForFences_(context_.device(), 1, &frame.done, VK_TRUE, UINT64_MAX) != VK_SUCCESS) | 325 | if (vkWaitForFences_(context_.device(), 1, &frame.done, VK_TRUE, UINT64_MAX) != VK_SUCCESS) |
| @@ -315,10 +330,95 @@ public: | |||
| 315 | return true; | 330 | return true; |
| 316 | } | 331 | } |
| 317 | 332 | ||
| 318 | // Shared by present(), present_blank() and present_planes(). `binding` null and `planes` null: clear instead of blit; | 333 | // Shared by present(), present_blank() and present_planes(). |
| 319 | // `planes`: compose them (`binding`, `source`, `source_rect` unused). | 334 | // |
| 335 | // A Present is ordered behind the work the queues hold (a batch that waits for a Signal not yet submitted, | ||
| 336 | // queue_holds.h): it would otherwise blit whatever the source texture held before that batch ran. It is not | ||
| 337 | // blocked: it is queued, returns VK_SUCCESS as ExecuteCommandLists does for a held batch, and is submitted, | ||
| 338 | // in order with the presents before it, by the thread that releases the last operation held when it was called | ||
| 339 | // (its failure is printed there). Held work that is never released holds back the presents after it, as it | ||
| 340 | // holds back the queue's own work; with nothing held this is the plain path. A blank present reads nothing and | ||
| 341 | // is only ordered behind earlier presents. Waiting instead was rejected: the Signal that releases the work may | ||
| 342 | // be one the application's own thread makes after Present returns. Like a full frame-latency queue, a Present that | ||
| 343 | // finds max_deferred_presents queued ahead of it does wait, for held work to release (logged once; set_abort ends | ||
| 344 | // the wait), so the queue behind never-released work is bounded. A queued Present reports its tear as false | ||
| 345 | // (last_present_tore()): the frame pacer's default sync. | ||
| 346 | static constexpr std::size_t max_deferred_presents = 8; | ||
| 347 | |||
| 320 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | 348 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, |
| 321 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { | 349 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { |
| 350 | wait_for_room(); | ||
| 351 | std::lock_guard lock(present_mutex_); | ||
| 352 | // `binding` null and `planes` null: clear instead of blit; `planes`: compose them (`binding`, `source`, | ||
| 353 | // `source_rect` unused). Only a present that reads a texture waits for held work. | ||
| 354 | if (deferred_.empty() && !((binding || planes) && context_.holds().any_held())) | ||
| 355 | return submit_now(binding, source, vsync, source_rect, srgb, tear, planes); | ||
| 356 | PendingPresent pending{binding != nullptr, source, vsync, source_rect != nullptr, {}, srgb, tear, | ||
| 357 | planes != nullptr, {}}; | ||
| 358 | if (source_rect) pending.rect = *source_rect; | ||
| 359 | if (planes) pending.planes = *planes; | ||
| 360 | deferred_.push_back(pending); | ||
| 361 | last_tore_ = false; | ||
| 362 | context_.holds().after_held(this, [this] { run_deferred(); }); | ||
| 363 | return VK_SUCCESS; | ||
| 364 | } | ||
| 365 | |||
| 366 | private: | ||
| 367 | bool should_abort() { | ||
| 368 | std::function<bool()> abort; | ||
| 369 | { | ||
| 370 | std::lock_guard lock(abort_mutex_); | ||
| 371 | abort = abort_; | ||
| 372 | } | ||
| 373 | return abort && abort(); | ||
| 374 | } | ||
| 375 | |||
| 376 | // Called without present_mutex_: the thread that releases the queued presents needs it. | ||
| 377 | void wait_for_room() { | ||
| 378 | bool logged = false; | ||
| 379 | for (;;) { | ||
| 380 | { | ||
| 381 | std::lock_guard lock(present_mutex_); | ||
| 382 | if (deferred_.size() < max_deferred_presents) return; | ||
| 383 | } | ||
| 384 | if (!logged) { | ||
| 385 | logged = true; | ||
| 386 | std::fprintf(stderr, "[rt] d3d12: %zu Presents are queued behind held work; this one waits for it\n", | ||
| 387 | max_deferred_presents); | ||
| 388 | } | ||
| 389 | if (should_abort()) return; | ||
| 390 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| 391 | } | ||
| 392 | } | ||
| 393 | |||
| 394 | struct PendingPresent { | ||
| 395 | bool has_source; | ||
| 396 | ResourceId source; | ||
| 397 | bool vsync; | ||
| 398 | bool has_rect; | ||
| 399 | PresentRect rect; | ||
| 400 | bool srgb; | ||
| 401 | bool tear; | ||
| 402 | bool has_planes; | ||
| 403 | PresentPlanes planes; | ||
| 404 | }; | ||
| 405 | |||
| 406 | void run_deferred() { | ||
| 407 | std::lock_guard lock(present_mutex_); | ||
| 408 | if (deferred_.empty()) return; | ||
| 409 | const PendingPresent p = deferred_.front(); | ||
| 410 | deferred_.pop_front(); | ||
| 411 | ResourceBinding binding{}; | ||
| 412 | const VkResult result = submit_now(p.has_source ? &binding : nullptr, p.source, p.vsync, | ||
| 413 | p.has_rect ? &p.rect : nullptr, p.srgb, p.tear, | ||
| 414 | p.has_planes ? &p.planes : nullptr); | ||
| 415 | if (result != VK_SUCCESS) | ||
| 416 | std::fprintf(stderr, "[rt] d3d12: a Present held behind queued work failed when it was released (%d)\n", | ||
| 417 | int(result)); | ||
| 418 | } | ||
| 419 | |||
| 420 | VkResult submit_now(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | ||
| 421 | bool srgb, bool tear = false, const PresentPlanes* planes = nullptr) { | ||
| 322 | last_tore_ = false; | 422 | last_tore_ = false; |
| 323 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | 423 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; |
| 324 | // The swapchain's HDR10 format is only for composed planes that ask for it; a blit of an SDR source or a blank | 424 | // The swapchain's HDR10 format is only for composed planes that ask for it; a blit of an SDR source or a blank |
| @@ -741,7 +841,7 @@ private: | |||
| 741 | const VkResult result = vkAcquireNextImageKHR_(context_.device(), swapchain_, 50'000'000, semaphore, | 841 | const VkResult result = vkAcquireNextImageKHR_(context_.device(), swapchain_, 50'000'000, semaphore, |
| 742 | VK_NULL_HANDLE, image); | 842 | VK_NULL_HANDLE, image); |
| 743 | if (result != VK_TIMEOUT && result != VK_NOT_READY) return result; | 843 | if (result != VK_TIMEOUT && result != VK_NOT_READY) return result; |
| 744 | if (abort_ && abort_()) return VK_NOT_READY; | 844 | if (should_abort()) return VK_NOT_READY; |
| 745 | } | 845 | } |
| 746 | } | 846 | } |
| 747 | 847 | ||
| @@ -1029,6 +1129,7 @@ private: | |||
| 1029 | ready_ = std::move(ready); | 1129 | ready_ = std::move(ready); |
| 1030 | format_ = chosen.format; | 1130 | format_ = chosen.format; |
| 1031 | extent_ = extent; | 1131 | extent_ = extent; |
| 1132 | extent_packed_ = (std::uint64_t(extent.width) << 32) | extent.height; | ||
| 1032 | hdr_active_ = chose_hdr; | 1133 | hdr_active_ = chose_hdr; |
| 1033 | if (capture_) { | 1134 | if (capture_) { |
| 1034 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. | 1135 | // A capture that cannot be made fails the recreation with the swapchain in place and stale_ still set. |
| @@ -1112,11 +1213,15 @@ private: | |||
| 1112 | VkInstance instance_; | 1213 | VkInstance instance_; |
| 1113 | VkPhysicalDevice physical_; | 1214 | VkPhysicalDevice physical_; |
| 1114 | VkQueue queue_; | 1215 | VkQueue queue_; |
| 1216 | mutable std::recursive_mutex present_mutex_; // all presenter state: submit(), a deferred present, the accessors | ||
| 1217 | mutable std::mutex abort_mutex_; // abort_ alone: set_abort() must work while a present waits | ||
| 1218 | std::deque<PendingPresent> deferred_; // presents queued behind held work, in call order | ||
| 1115 | VkSurfaceKHR surface_; | 1219 | VkSurfaceKHR surface_; |
| 1116 | bool maintenance1_ = false; // VK_KHR/EXT_swapchain_maintenance1 (and the surface extension) are enabled | 1220 | bool maintenance1_ = false; // VK_KHR/EXT_swapchain_maintenance1 (and the surface extension) are enabled |
| 1117 | VkPresentModeKHR tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; // set with the swapchain, see tear_mode() | 1221 | VkPresentModeKHR tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; // set with the swapchain, see tear_mode() |
| 1118 | bool last_tore_ = false; | 1222 | std::atomic<bool> last_tore_{false}; |
| 1119 | std::uint64_t tear_presents_ = 0; | 1223 | std::atomic<std::uint64_t> extent_packed_{0}; // extent_ for readers that must not wait for a present |
| 1224 | std::atomic<std::uint64_t> tear_presents_{0}; | ||
| 1120 | VkSwapchainKHR swapchain_ = VK_NULL_HANDLE; | 1225 | VkSwapchainKHR swapchain_ = VK_NULL_HANDLE; |
| 1121 | VkFormat format_ = VK_FORMAT_UNDEFINED; | 1226 | VkFormat format_ = VK_FORMAT_UNDEFINED; |
| 1122 | VkExtent2D extent_{}; | 1227 | VkExtent2D extent_{}; |
| @@ -1125,15 +1230,15 @@ private: | |||
| 1125 | std::array<Frame, 2> frames_{}; | 1230 | std::array<Frame, 2> frames_{}; |
| 1126 | VkCommandPool pool_ = VK_NULL_HANDLE; | 1231 | VkCommandPool pool_ = VK_NULL_HANDLE; |
| 1127 | std::uint64_t frame_index_ = 0; | 1232 | std::uint64_t frame_index_ = 0; |
| 1128 | std::uint64_t presented_ = 0; | 1233 | std::atomic<std::uint64_t> presented_{0}; |
| 1129 | bool vsync_ = true; | 1234 | bool vsync_ = true; |
| 1130 | bool stale_ = false; | 1235 | bool stale_ = false; |
| 1131 | bool ok_ = false; | 1236 | bool ok_ = false; |
| 1132 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) | 1237 | bool swapchain_srgb_ = false; // the swapchain format follows the source being presented (set once it exists) |
| 1133 | bool swapchain_hdr_req_ = false; // the swapchain was built for an HDR10 request (it has the format only if hdr_active_) | 1238 | bool swapchain_hdr_req_ = false; // the swapchain was built for an HDR10 request (it has the format only if hdr_active_) |
| 1134 | bool hdr_active_ = false; | 1239 | bool hdr_active_ = false; |
| 1135 | std::uint64_t composed_ = 0; | 1240 | std::atomic<std::uint64_t> composed_{0}; |
| 1136 | bool force_hdr_encoding_ = false; | 1241 | std::atomic<bool> force_hdr_encoding_{false}; |
| 1137 | bool warned_hdr_ = false; | 1242 | bool warned_hdr_ = false; |
| 1138 | std::unique_ptr<PresentComposer> composer_; | 1243 | std::unique_ptr<PresentComposer> composer_; |
| 1139 | std::function<bool()> abort_; | 1244 | std::function<bool()> abort_; |