diff options
| author | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 21:02:33 +0100 |
|---|---|---|
| committer | AlxcBTW <187763517+AlxcBTW@users.noreply.github.com> | 2026-11-22 21:02:33 +0100 |
| commit | f8007dda67f9c45757b63df5c5fd8377647da4ae (patch) | |
| tree | cdffc5cc7c81dcbbf683af9c0df6077c6587ac0d /runtime/src/vulkan_present.h | |
| parent | 9bebfb107070d84609e303009b0f91b011b9aa2e (diff) | |
Queue holds: review fixes (Present ordering, pins, timeline monotonicity, fence lifetime, teardown, docs)
- Present is queued behind held work (QueueHolds::after_held) and submitted in order by the releasing thread.
- Held batches keep the pipeline states and root signatures of their lists; fences a queue waits on or signals are
kept until the submission has completed on the GPU (a D3D12 queue holds them).
- A held Signal released below an unheld one on the same fence leaves the Vulkan signal out (claimed under the submit lock).
- A queue destroyed with held Signals completes the CPU waits on their fences and reports it.
- A failed held batch is reported (take_held_failure, last_execute_result), its waits stay for the next submission,
and the Signal behind it still signals.
- d3d12_queue_handover_test: pairs, two-thread signalling stress, held Signal of a released fence, shared fence,
teardown with a CPU waiter; --two-queues and --shared-queue modes on a caller-owned device.
- Tests for each fix in vk_presenter_sync_test, vk_queue_query_sync_test and d3d12_m4_compute_test; docs section 4.4.
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 | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/runtime/src/vulkan_present.h b/runtime/src/vulkan_present.h index 9a73d2f82..0bda6f853 100644 --- a/runtime/src/vulkan_present.h +++ b/runtime/src/vulkan_present.h | |||
| @@ -34,6 +34,8 @@ | |||
| 34 | #include <cstring> | 34 | #include <cstring> |
| 35 | #include <functional> | 35 | #include <functional> |
| 36 | #include <map> | 36 | #include <map> |
| 37 | #include <deque> | ||
| 38 | #include <mutex> | ||
| 37 | #include <vector> | 39 | #include <vector> |
| 38 | 40 | ||
| 39 | namespace leonida { | 41 | namespace leonida { |
| @@ -131,6 +133,7 @@ public: | |||
| 131 | } | 133 | } |
| 132 | 134 | ||
| 133 | ~VulkanPresenter() { | 135 | ~VulkanPresenter() { |
| 136 | context_.holds().cancel_after(this); // presents still queued behind held work are dropped | ||
| 134 | auto& vk = context_.vk; | 137 | auto& vk = context_.vk; |
| 135 | const VkDevice device = context_.device(); | 138 | const VkDevice device = context_.device(); |
| 136 | { | 139 | { |
| @@ -237,8 +240,53 @@ public: | |||
| 237 | } | 240 | } |
| 238 | 241 | ||
| 239 | // Shared by present() and present_blank(). `binding` null: clear instead of blit. | 242 | // Shared by present() and present_blank(). `binding` null: clear instead of blit. |
| 243 | // | ||
| 244 | // A Present is ordered behind the work the queues hold (a batch that waits for a Signal not yet submitted, | ||
| 245 | // queue_holds.h): it would otherwise blit whatever the source texture held before that batch ran. It is not | ||
| 246 | // blocked: it is queued, returns VK_SUCCESS as ExecuteCommandLists does for a held batch, and is submitted, | ||
| 247 | // in order with the presents before it, by the thread that releases the last operation held when it was called | ||
| 248 | // (its failure is printed there). Held work that is never released holds back the presents after it, as it | ||
| 249 | // holds back the queue's own work; with nothing held this is the plain path. A blank present reads nothing and | ||
| 250 | // is only ordered behind earlier presents. Waiting instead was rejected: the Signal that releases the work may | ||
| 251 | // be one the application's own thread makes after Present returns. | ||
| 240 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | 252 | VkResult submit(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, |
| 241 | bool srgb, bool tear = false) { | 253 | bool srgb, bool tear = false) { |
| 254 | std::lock_guard lock(present_mutex_); | ||
| 255 | if (deferred_.empty() && !(binding && context_.holds().any_held())) | ||
| 256 | return submit_now(binding, source, vsync, source_rect, srgb, tear); | ||
| 257 | PendingPresent pending{binding != nullptr, source, vsync, source_rect != nullptr, {}, srgb, tear}; | ||
| 258 | if (source_rect) pending.rect = *source_rect; | ||
| 259 | deferred_.push_back(pending); | ||
| 260 | context_.holds().after_held(this, [this] { run_deferred(); }); | ||
| 261 | return VK_SUCCESS; | ||
| 262 | } | ||
| 263 | |||
| 264 | private: | ||
| 265 | struct PendingPresent { | ||
| 266 | bool has_source; | ||
| 267 | ResourceId source; | ||
| 268 | bool vsync; | ||
| 269 | bool has_rect; | ||
| 270 | PresentRect rect; | ||
| 271 | bool srgb; | ||
| 272 | bool tear; | ||
| 273 | }; | ||
| 274 | |||
| 275 | void run_deferred() { | ||
| 276 | std::lock_guard lock(present_mutex_); | ||
| 277 | if (deferred_.empty()) return; | ||
| 278 | const PendingPresent p = deferred_.front(); | ||
| 279 | deferred_.pop_front(); | ||
| 280 | ResourceBinding binding{}; | ||
| 281 | const VkResult result = submit_now(p.has_source ? &binding : nullptr, p.source, p.vsync, | ||
| 282 | p.has_rect ? &p.rect : nullptr, p.srgb, p.tear); | ||
| 283 | if (result != VK_SUCCESS) | ||
| 284 | std::fprintf(stderr, "[rt] d3d12: a Present held behind queued work failed when it was released (%d)\n", | ||
| 285 | int(result)); | ||
| 286 | } | ||
| 287 | |||
| 288 | VkResult submit_now(const ResourceBinding* binding, ResourceId source, bool vsync, const PresentRect* source_rect, | ||
| 289 | bool srgb, bool tear = false) { | ||
| 242 | last_tore_ = false; | 290 | last_tore_ = false; |
| 243 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; | 291 | if (!ok_) return VK_ERROR_INITIALIZATION_FAILED; |
| 244 | if (vsync != vsync_ || stale_ || srgb != swapchain_srgb_) { | 292 | if (vsync != vsync_ || stale_ || srgb != swapchain_srgb_) { |
| @@ -818,6 +866,8 @@ private: | |||
| 818 | VkInstance instance_; | 866 | VkInstance instance_; |
| 819 | VkPhysicalDevice physical_; | 867 | VkPhysicalDevice physical_; |
| 820 | VkQueue queue_; | 868 | VkQueue queue_; |
| 869 | std::recursive_mutex present_mutex_; // submit() and a deferred present on the releasing thread | ||
| 870 | std::deque<PendingPresent> deferred_; // presents queued behind held work, in call order | ||
| 821 | VkSurfaceKHR surface_; | 871 | VkSurfaceKHR surface_; |
| 822 | bool maintenance1_ = false; // VK_KHR/EXT_swapchain_maintenance1 (and the surface extension) are enabled | 872 | bool maintenance1_ = false; // VK_KHR/EXT_swapchain_maintenance1 (and the surface extension) are enabled |
| 823 | VkPresentModeKHR tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; // set with the swapchain, see tear_mode() | 873 | VkPresentModeKHR tear_mode_ = VK_PRESENT_MODE_MAX_ENUM_KHR; // set with the swapchain, see tear_mode() |