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 | |
| 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')
| -rw-r--r-- | runtime/src/queue_holds.h | 124 | ||||
| -rw-r--r-- | runtime/src/vulkan_backend.h | 199 | ||||
| -rw-r--r-- | runtime/src/vulkan_present.h | 50 |
3 files changed, 312 insertions, 61 deletions
diff --git a/runtime/src/queue_holds.h b/runtime/src/queue_holds.h index eafe3ebc8..4a1f990d8 100644 --- a/runtime/src/queue_holds.h +++ b/runtime/src/queue_holds.h | |||
| @@ -2,14 +2,15 @@ | |||
| 2 | 2 | ||
| 3 | #include <atomic> | 3 | #include <atomic> |
| 4 | #include <cstdint> | 4 | #include <cstdint> |
| 5 | #include <cstdio> | 5 | #include <deque> |
| 6 | #include <functional> | ||
| 6 | #include <mutex> | 7 | #include <mutex> |
| 7 | #include <unordered_map> | 8 | #include <unordered_map> |
| 8 | #include <vector> | 9 | #include <vector> |
| 9 | 10 | ||
| 10 | namespace leonida { | 11 | namespace leonida { |
| 11 | 12 | ||
| 12 | // Cross-queue Wait-before-Signal (docs section 4.4, "Wait before Signal"). | 13 | // Cross-queue Wait-before-Signal (docs section 4.4, "Wait before Signal across queues"). |
| 13 | // | 14 | // |
| 14 | // A queue replays its ExecuteCommandLists against one device-wide layout tracker, in the order the calls reach the | 15 | // A queue replays its ExecuteCommandLists against one device-wide layout tracker, in the order the calls reach the |
| 15 | // layer. When queue B calls Wait(fence, v) and the Signal(fence, v) on queue A is only submitted afterwards, B's | 16 | // layer. When queue B calls Wait(fence, v) and the Signal(fence, v) on queue A is only submitted afterwards, B's |
| @@ -19,23 +20,30 @@ namespace leonida { | |||
| 19 | // own timeline semaphores still order the GPU work. | 20 | // own timeline semaphores still order the GPU work. |
| 20 | // | 21 | // |
| 21 | // This class is the device-wide half: for every fence, the highest value a Signal has been submitted for (or the | 22 | // This class is the device-wide half: for every fence, the highest value a Signal has been submitted for (or the |
| 22 | // CPU has set it to), which is not the completed value; and the held queues, which it asks to release whatever | 23 | // CPU has set it to), which is not the completed value; the held queues, which it asks to release whatever became |
| 23 | // became ready. It owns no Vulkan objects. Semaphores it was never told about count as satisfied, so a queue used | 24 | // ready; and "after held work" callbacks (the presenter), which run once every operation held when they were |
| 24 | // with a plain VkSemaphore behaves as before. | 25 | // registered has been released. It owns no Vulkan objects. Semaphores it was never told about count as satisfied, |
| 26 | // so a queue used with a plain VkSemaphore behaves as before. | ||
| 25 | // | 27 | // |
| 26 | // Threads. Nothing here blocks on the GPU. The thread that makes a wait satisfiable (a queue's Signal, a CPU | 28 | // Threads. No GPU wait happens here, but the thread that makes a wait satisfiable (a queue's Signal, a CPU Signal) |
| 27 | // Signal) releases the ready batches itself, after it has released its own queue's lock: recording a held batch | 29 | // does the release itself, after it has dropped its own queue's state lock: it records and submits the held |
| 28 | // costs what its own ExecuteCommandLists would have. Only one thread releases at a time; a thread that finds | 30 | // batches (a held ExecuteCommandLists costs what it would have cost when called, which includes whatever |
| 29 | // another one releasing leaves a note and that one takes another pass, so no caller waits for another's recording. | 31 | // execute_now does: the bindless upload's wait for the device is one) and runs the callbacks. The COM wrapper |
| 32 | // still holds the mutex of the queue whose call that thread is in, so that queue's other calls wait meanwhile. | ||
| 33 | // One thread releases at a time; a thread that finds another one releasing leaves a note and that one takes | ||
| 34 | // another pass. detach() and cancel_after() (and remove_fence() from outside a release) wait for a running | ||
| 35 | // release. | ||
| 30 | template<class Semaphore> | 36 | template<class Semaphore> |
| 31 | class QueueHolds { | 37 | class QueueHolds { |
| 32 | public: | 38 | public: |
| 33 | // A queue that can hold batches. Both calls take the queue's own lock; neither is called with it held. | 39 | // A queue that can hold batches. The calls take the queue's own lock; none is made with it held. |
| 34 | struct Holder { | 40 | struct Holder { |
| 35 | // Releases the held operations at the front of the queue whose waits are satisfied. True if any was. | 41 | // Releases the held operations at the front of the queue whose waits are satisfied. True if any was. |
| 36 | virtual bool release_ready() = 0; | 42 | virtual bool release_ready() = 0; |
| 37 | // Drops the held operations that wait on `semaphore` (its fence is gone), reporting each. | 43 | // Drops the held operations that wait on `semaphore` (its fence is gone), reporting each. |
| 38 | virtual void discard_waiting_on(Semaphore semaphore) = 0; | 44 | virtual void discard_waiting_on(Semaphore semaphore) = 0; |
| 45 | // Sequence number (next_seq()) of the oldest held operation, 0 when nothing is held. | ||
| 46 | virtual std::uint64_t oldest_held_seq() = 0; | ||
| 39 | 47 | ||
| 40 | protected: | 48 | protected: |
| 41 | ~Holder() = default; | 49 | ~Holder() = default; |
| @@ -46,16 +54,21 @@ public: | |||
| 46 | fences_[semaphore] = initial; | 54 | fences_[semaphore] = initial; |
| 47 | } | 55 | } |
| 48 | 56 | ||
| 49 | // The fence is released: held batches waiting on it can never run, so they are dropped and reported. | 57 | // The fence is gone. Operations that still reference it are dropped and reported (the COM layer keeps a fence |
| 58 | // alive while a held operation names it, so this is a safety net for direct users of VulkanFence). Not done | ||
| 59 | // from inside a release: the releasing thread already holds the lock, and what it is releasing cannot | ||
| 60 | // reference a fence whose last reference it is dropping, since that reference is the one it holds. | ||
| 50 | void remove_fence(Semaphore semaphore) { | 61 | void remove_fence(Semaphore semaphore) { |
| 51 | { | 62 | { |
| 52 | std::lock_guard lock(mutex_); | 63 | std::lock_guard lock(mutex_); |
| 53 | fences_.erase(semaphore); | 64 | fences_.erase(semaphore); |
| 54 | } | 65 | } |
| 55 | if (held_.load(std::memory_order_acquire) == 0) return; | 66 | if (held_.load(std::memory_order_acquire) == 0 || in_pump()) return; |
| 56 | { | 67 | { |
| 57 | std::lock_guard pump(pump_mutex_); | 68 | std::lock_guard pump(pump_mutex_); |
| 69 | in_pump() = true; // what is dropped may release other fences: they must not wait for this lock | ||
| 58 | for (Holder* holder : snapshot()) holder->discard_waiting_on(semaphore); | 70 | for (Holder* holder : snapshot()) holder->discard_waiting_on(semaphore); |
| 71 | in_pump() = false; | ||
| 59 | } | 72 | } |
| 60 | pump_ready(); | 73 | pump_ready(); |
| 61 | } | 74 | } |
| @@ -68,6 +81,20 @@ public: | |||
| 68 | if (it != fences_.end() && it->second < value) it->second = value; | 81 | if (it != fences_.end() && it->second < value) it->second = value; |
| 69 | } | 82 | } |
| 70 | 83 | ||
| 84 | // A queue is about to submit a Signal of `value`; call inside the device submit lock, so the order of the | ||
| 85 | // calls is the order of the vkQueueSubmits. False when a Signal of at least `value` was already submitted: the | ||
| 86 | // timeline cannot go down or stay (VUID-VkSubmitInfo-pSignalSemaphores-03242 and the timeline rules), and a wait | ||
| 87 | // for `value` is already satisfied, so the Vulkan signal is left out. This happens when a held Signal(F, 2) is | ||
| 88 | // released after an unheld Signal(F, 3). Unknown semaphores are always signaled. | ||
| 89 | bool claim_signal(Semaphore semaphore, std::uint64_t value) { | ||
| 90 | std::lock_guard lock(mutex_); | ||
| 91 | const auto it = fences_.find(semaphore); | ||
| 92 | if (it == fences_.end()) return true; | ||
| 93 | if (it->second >= value) return false; | ||
| 94 | it->second = value; | ||
| 95 | return true; | ||
| 96 | } | ||
| 97 | |||
| 71 | // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied. | 98 | // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied. |
| 72 | bool pending(Semaphore semaphore, std::uint64_t value) const { | 99 | bool pending(Semaphore semaphore, std::uint64_t value) const { |
| 73 | std::lock_guard lock(mutex_); | 100 | std::lock_guard lock(mutex_); |
| @@ -94,14 +121,47 @@ public: | |||
| 94 | pump_ready(); | 121 | pump_ready(); |
| 95 | } | 122 | } |
| 96 | 123 | ||
| 124 | // Sequence numbers of held operations, in the order they were held (across queues). | ||
| 125 | std::uint64_t next_seq() { return seq_.fetch_add(1, std::memory_order_acq_rel) + 1; } | ||
| 126 | std::uint64_t current_seq() const { return seq_.load(std::memory_order_acquire); } | ||
| 127 | |||
| 128 | // Runs `fn` once every operation held up to now has been released (or dropped), on the thread that releases the | ||
| 129 | // last of them, or right here if none is held. Callbacks of one owner run in registration order. Used by the | ||
| 130 | // presenter: a Present queued behind held work shows what that work draws. | ||
| 131 | void after_held(const void* owner, std::function<void()> fn) { | ||
| 132 | { | ||
| 133 | std::lock_guard lock(mutex_); | ||
| 134 | barriers_.push_back({owner, current_seq(), std::move(fn)}); | ||
| 135 | barrier_count_.fetch_add(1, std::memory_order_acq_rel); | ||
| 136 | } | ||
| 137 | pump_ready(); | ||
| 138 | } | ||
| 139 | |||
| 140 | // Drops the callbacks of `owner` that have not run, and waits for one that is running. | ||
| 141 | void cancel_after(const void* owner) { | ||
| 142 | { | ||
| 143 | std::lock_guard pump(pump_mutex_); | ||
| 144 | std::lock_guard lock(mutex_); | ||
| 145 | for (auto it = barriers_.begin(); it != barriers_.end();) | ||
| 146 | if (it->owner == owner) { | ||
| 147 | it = barriers_.erase(it); | ||
| 148 | barrier_count_.fetch_sub(1, std::memory_order_acq_rel); | ||
| 149 | } else { | ||
| 150 | ++it; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | pump_ready(); | ||
| 154 | } | ||
| 155 | |||
| 97 | // Held operations over all queues, for the cheap "is there anything to do" test. | 156 | // Held operations over all queues, for the cheap "is there anything to do" test. |
| 98 | void held_changed(std::int64_t delta) { held_.fetch_add(delta, std::memory_order_acq_rel); } | 157 | void held_changed(std::int64_t delta) { held_.fetch_add(delta, std::memory_order_acq_rel); } |
| 99 | bool any_held() const { return held_.load(std::memory_order_acquire) > 0; } | 158 | bool any_held() const { return held_.load(std::memory_order_acquire) > 0; } |
| 100 | 159 | ||
| 101 | // Releases every held operation that has become ready, in dependency order: a release can submit a Signal | 160 | // Releases every held operation that has become ready, in dependency order: a release can submit a Signal |
| 102 | // that makes another queue's front ready, so passes repeat until one finds nothing new. | 161 | // that makes another queue's front ready, so passes repeat until one finds nothing new. Then the callbacks |
| 162 | // whose held work is all gone. | ||
| 103 | void pump_ready() { | 163 | void pump_ready() { |
| 104 | if (!any_held()) return; | 164 | if (!any_held() && barrier_count_.load(std::memory_order_acquire) == 0) return; |
| 105 | dirty_.store(true, std::memory_order_release); | 165 | dirty_.store(true, std::memory_order_release); |
| 106 | if (in_pump()) return; // a release on this thread submitted a Signal: the running pass will go again | 166 | if (in_pump()) return; // a release on this thread submitted a Signal: the running pass will go again |
| 107 | for (;;) { | 167 | for (;;) { |
| @@ -114,6 +174,7 @@ public: | |||
| 114 | progress = false; | 174 | progress = false; |
| 115 | for (Holder* holder : snapshot()) progress |= holder->release_ready(); | 175 | for (Holder* holder : snapshot()) progress |= holder->release_ready(); |
| 116 | } | 176 | } |
| 177 | run_barriers(); | ||
| 117 | } | 178 | } |
| 118 | in_pump() = false; | 179 | in_pump() = false; |
| 119 | pump.unlock(); | 180 | pump.unlock(); |
| @@ -122,6 +183,12 @@ public: | |||
| 122 | } | 183 | } |
| 123 | 184 | ||
| 124 | private: | 185 | private: |
| 186 | struct Barrier { | ||
| 187 | const void* owner; | ||
| 188 | std::uint64_t seq; | ||
| 189 | std::function<void()> fn; | ||
| 190 | }; | ||
| 191 | |||
| 125 | static bool& in_pump() { | 192 | static bool& in_pump() { |
| 126 | static thread_local bool pumping = false; | 193 | static thread_local bool pumping = false; |
| 127 | return pumping; | 194 | return pumping; |
| @@ -132,11 +199,38 @@ private: | |||
| 132 | return holders_; | 199 | return holders_; |
| 133 | } | 200 | } |
| 134 | 201 | ||
| 135 | mutable std::mutex mutex_; // leaf: fences_ and holders_ | 202 | // Called with pump_mutex_ held. A callback runs when no held operation is older than the moment it was asked. |
| 203 | void run_barriers() { | ||
| 204 | for (;;) { | ||
| 205 | Barrier front; | ||
| 206 | { | ||
| 207 | std::lock_guard lock(mutex_); | ||
| 208 | if (barriers_.empty()) return; | ||
| 209 | front = barriers_.front(); | ||
| 210 | } | ||
| 211 | for (Holder* holder : snapshot()) { | ||
| 212 | const std::uint64_t oldest = holder->oldest_held_seq(); | ||
| 213 | if (oldest && oldest <= front.seq) return; | ||
| 214 | } | ||
| 215 | { | ||
| 216 | std::lock_guard lock(mutex_); | ||
| 217 | if (barriers_.empty() || barriers_.front().seq != front.seq || barriers_.front().owner != front.owner) | ||
| 218 | continue; // cancelled meanwhile | ||
| 219 | barriers_.pop_front(); | ||
| 220 | barrier_count_.fetch_sub(1, std::memory_order_acq_rel); | ||
| 221 | } | ||
| 222 | front.fn(); | ||
| 223 | } | ||
| 224 | } | ||
| 225 | |||
| 226 | mutable std::mutex mutex_; // leaf: fences_, holders_ and barriers_ | ||
| 136 | std::mutex pump_mutex_; // one releasing thread; held across release_ready(), so detach() waits for it | 227 | std::mutex pump_mutex_; // one releasing thread; held across release_ready(), so detach() waits for it |
| 137 | std::unordered_map<Semaphore, std::uint64_t> fences_; | 228 | std::unordered_map<Semaphore, std::uint64_t> fences_; |
| 138 | std::vector<Holder*> holders_; | 229 | std::vector<Holder*> holders_; |
| 230 | std::deque<Barrier> barriers_; | ||
| 139 | std::atomic<std::int64_t> held_{0}; | 231 | std::atomic<std::int64_t> held_{0}; |
| 232 | std::atomic<std::int64_t> barrier_count_{0}; | ||
| 233 | std::atomic<std::uint64_t> seq_{0}; | ||
| 140 | std::atomic<bool> dirty_{false}; | 234 | std::atomic<bool> dirty_{false}; |
| 141 | }; | 235 | }; |
| 142 | 236 | ||
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 54602c5fc..a2f6bad21 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1499,10 +1499,17 @@ public: | |||
| 1499 | // the layout tracker as it was. Pending Wait()s are attached to this submission. When one of them (or an | 1499 | // the layout tracker as it was. Pending Wait()s are attached to this submission. When one of them (or an |
| 1500 | // earlier held operation) waits for a fence value no Signal has been submitted for, the lists are copied and | 1500 | // earlier held operation) waits for a fence value no Signal has been submitted for, the lists are copied and |
| 1501 | // held instead, and the call succeeds: the batch is replayed by the thread that submits the Signal (or the CPU | 1501 | // held instead, and the call succeeds: the batch is replayed by the thread that submits the Signal (or the CPU |
| 1502 | // Signal), which is also where its errors are reported. | 1502 | // Signal), which is also where its errors are reported (stderr, and take_held_failure()). |
| 1503 | ReplayResult execute(std::span<const CommandList* const> lists) { | 1503 | // |
| 1504 | // `pin`, called only when the batch is held, returns what has to stay alive until the batch has run: the COM layer | ||
| 1505 | // passes the pipeline states and root signatures the lists named (the application may release them and Reset | ||
| 1506 | // the lists as soon as the call returns). | ||
| 1507 | using PinFn = std::function<std::shared_ptr<void>()>; | ||
| 1508 | |||
| 1509 | ReplayResult execute(std::span<const CommandList* const> lists, const PinFn& pin = {}) { | ||
| 1504 | ReplayResult result = ReplayResult::success; | 1510 | ReplayResult result = ReplayResult::success; |
| 1505 | bool held = false; | 1511 | bool held = false; |
| 1512 | std::vector<std::shared_ptr<void>> retired; // destroyed after state_mutex_ is released | ||
| 1506 | { | 1513 | { |
| 1507 | std::lock_guard state(state_mutex_); | 1514 | std::lock_guard state(state_mutex_); |
| 1508 | if (!must_hold()) { | 1515 | if (!must_hold()) { |
| @@ -1514,18 +1521,23 @@ public: | |||
| 1514 | op.kind = Deferred::Kind::batch; | 1521 | op.kind = Deferred::Kind::batch; |
| 1515 | op.lists.reserve(lists.size()); | 1522 | op.lists.reserve(lists.size()); |
| 1516 | for (const CommandList* list : lists) op.lists.push_back(*list); | 1523 | for (const CommandList* list : lists) op.lists.push_back(*list); |
| 1524 | if (pin) op.pins.push_back(pin()); | ||
| 1517 | hold(std::move(op)); | 1525 | hold(std::move(op)); |
| 1518 | held = true; | 1526 | held = true; |
| 1519 | } | 1527 | } |
| 1528 | drain_pins(retired); | ||
| 1520 | } | 1529 | } |
| 1530 | retired.clear(); | ||
| 1521 | if (held) context_.holds().pump_ready(); // a Signal may have been submitted since the check | 1531 | if (held) context_.holds().pump_ready(); // a Signal may have been submitted since the check |
| 1522 | return result; | 1532 | return result; |
| 1523 | } | 1533 | } |
| 1524 | 1534 | ||
| 1525 | // ID3D12CommandQueue::Signal: the fence reaches `value` after all work submitted so far. Held behind earlier | 1535 | // ID3D12CommandQueue::Signal: the fence reaches `value` after all work submitted so far. Held behind earlier |
| 1526 | // held operations, so the fence does not run ahead of the work it follows. | 1536 | // held operations, so the fence does not run ahead of the work it follows. `pin` (called only when the Signal is |
| 1527 | bool signal(VkSemaphore fence, std::uint64_t value) { | 1537 | // held) keeps the fence object alive until it has been submitted, as D3D12 does. |
| 1538 | bool signal(VkSemaphore fence, std::uint64_t value, const PinFn& pin = {}) { | ||
| 1528 | bool ok = true; | 1539 | bool ok = true; |
| 1540 | std::vector<std::shared_ptr<void>> retired; | ||
| 1529 | { | 1541 | { |
| 1530 | std::lock_guard state(state_mutex_); | 1542 | std::lock_guard state(state_mutex_); |
| 1531 | if (must_hold()) { | 1543 | if (must_hold()) { |
| @@ -1533,21 +1545,33 @@ public: | |||
| 1533 | op.kind = Deferred::Kind::signal; | 1545 | op.kind = Deferred::Kind::signal; |
| 1534 | op.fence = fence; | 1546 | op.fence = fence; |
| 1535 | op.value = value; | 1547 | op.value = value; |
| 1548 | if (pin) op.pins.push_back(pin()); | ||
| 1536 | hold(std::move(op)); | 1549 | hold(std::move(op)); |
| 1537 | } else { | 1550 | } else { |
| 1538 | ok = submit(VK_NULL_HANDLE, fence, value); | 1551 | ok = submit(VK_NULL_HANDLE, fence, value); |
| 1539 | } | 1552 | } |
| 1553 | drain_pins(retired); | ||
| 1540 | } | 1554 | } |
| 1555 | retired.clear(); | ||
| 1541 | context_.holds().pump_ready(); // this Signal, or the hold above, may have made a held batch ready | 1556 | context_.holds().pump_ready(); // this Signal, or the hold above, may have made a held batch ready |
| 1542 | return ok; | 1557 | return ok; |
| 1543 | } | 1558 | } |
| 1544 | 1559 | ||
| 1545 | // ID3D12CommandQueue::Wait: later submissions wait for the fence. A wait in a Vulkan batch only holds back that | 1560 | // ID3D12CommandQueue::Wait: later submissions wait for the fence. A wait in a Vulkan batch only holds back that |
| 1546 | // batch, so it is kept and attached to the next submission (held on the CPU if no Signal reaches it yet). | 1561 | // batch, so it is kept and attached to the next submission (held on the CPU if no Signal reaches it yet). |
| 1547 | void wait(VkSemaphore fence, std::uint64_t value) { | 1562 | // `pin` returns what keeps the fence object alive until then (a D3D12 queue holds its fences). |
| 1563 | void wait(VkSemaphore fence, std::uint64_t value, const PinFn& pin = {}) { | ||
| 1548 | std::lock_guard state(state_mutex_); | 1564 | std::lock_guard state(state_mutex_); |
| 1549 | waits_.push_back(fence); | 1565 | waits_.push_back(fence); |
| 1550 | wait_values_.push_back(value); | 1566 | wait_values_.push_back(value); |
| 1567 | if (pin) wait_pins_.push_back(pin()); | ||
| 1568 | } | ||
| 1569 | |||
| 1570 | // The first failure of a held ExecuteCommandLists since the last call (what the call would have returned had it | ||
| 1571 | // not been held), or success. A Signal behind a failed batch still signals, as when the failure is not held: the | ||
| 1572 | // work did not run, the application is told here and on stderr, and nothing waits forever. | ||
| 1573 | ReplayResult take_held_failure() { | ||
| 1574 | return static_cast<ReplayResult>(held_failure_.exchange(static_cast<int>(ReplayResult::success))); | ||
| 1551 | } | 1575 | } |
| 1552 | 1576 | ||
| 1553 | // ID3D12CommandQueue::SetMarker/BeginEvent/EndEvent: queue labels, between the submissions around them. An end | 1577 | // ID3D12CommandQueue::SetMarker/BeginEvent/EndEvent: queue labels, between the submissions around them. An end |
| @@ -1887,6 +1911,7 @@ private: | |||
| 1887 | struct Deferred { | 1911 | struct Deferred { |
| 1888 | enum class Kind : std::uint8_t { batch, signal, marker }; | 1912 | enum class Kind : std::uint8_t { batch, signal, marker }; |
| 1889 | Kind kind = Kind::batch; | 1913 | Kind kind = Kind::batch; |
| 1914 | std::uint64_t seq = 0; // QueueHolds::next_seq(): the order operations were held in, across queues | ||
| 1890 | std::vector<VkSemaphore> waits; | 1915 | std::vector<VkSemaphore> waits; |
| 1891 | std::vector<std::uint64_t> wait_values; | 1916 | std::vector<std::uint64_t> wait_values; |
| 1892 | std::vector<CommandList> lists; // batch: copies, so the application may Reset its lists after the call | 1917 | std::vector<CommandList> lists; // batch: copies, so the application may Reset its lists after the call |
| @@ -1894,6 +1919,10 @@ private: | |||
| 1894 | std::uint64_t value = 0; | 1919 | std::uint64_t value = 0; |
| 1895 | DebugMarker::Kind marker_kind = DebugMarker::Kind::set; // marker | 1920 | DebugMarker::Kind marker_kind = DebugMarker::Kind::set; // marker |
| 1896 | std::string label; | 1921 | std::string label; |
| 1922 | // Alive until the operation has run: the fences it waits on and signals, the pipeline states and root | ||
| 1923 | // signatures of the lists (the application may release them right after the call). | ||
| 1924 | std::vector<std::shared_ptr<void>> pins; | ||
| 1925 | std::vector<std::shared_ptr<void>> wait_pins; // the fences of `waits` | ||
| 1897 | }; | 1926 | }; |
| 1898 | 1927 | ||
| 1899 | bool waits_pending(const std::vector<VkSemaphore>& semaphores, const std::vector<std::uint64_t>& values) const { | 1928 | bool waits_pending(const std::vector<VkSemaphore>& semaphores, const std::vector<std::uint64_t>& values) const { |
| @@ -1906,32 +1935,75 @@ private: | |||
| 1906 | // has no submitted Signal yet. Called with state_mutex_ held. | 1935 | // has no submitted Signal yet. Called with state_mutex_ held. |
| 1907 | bool must_hold() const { return !held_.empty() || waits_pending(waits_, wait_values_); } | 1936 | bool must_hold() const { return !held_.empty() || waits_pending(waits_, wait_values_); } |
| 1908 | 1937 | ||
| 1938 | // The waits are consumed: submitted as `ticket` (0: dropped on device loss). What the application may release | ||
| 1939 | // (the fences they named) is kept until that submission has completed on the GPU, as a D3D12 queue does: | ||
| 1940 | // destroying a semaphore a pending submission uses is a validation error. Retired pins are destroyed by the | ||
| 1941 | // caller after state_mutex_ is released (drain_pins): the last release of a fence waits for a running release, | ||
| 1942 | // which must not be waiting for this lock. | ||
| 1943 | void clear_waits(std::uint64_t ticket = 0) { | ||
| 1944 | waits_.clear(); | ||
| 1945 | wait_values_.clear(); | ||
| 1946 | retire_pins(wait_pins_, ticket); | ||
| 1947 | } | ||
| 1948 | |||
| 1949 | void retire_pins(std::vector<std::shared_ptr<void>>& pins, std::uint64_t ticket) { | ||
| 1950 | for (auto& pin : pins) { | ||
| 1951 | if (ticket) pin_queue_.push_back({ticket, std::move(pin)}); | ||
| 1952 | else retired_pins_.push_back(std::move(pin)); | ||
| 1953 | } | ||
| 1954 | pins.clear(); | ||
| 1955 | } | ||
| 1956 | |||
| 1957 | // Moves the pins whose submission has completed (and the ones retired outright) to `out`. state_mutex_ held. | ||
| 1958 | void drain_pins(std::vector<std::shared_ptr<void>>& out) { | ||
| 1959 | out.swap(retired_pins_); | ||
| 1960 | if (pin_queue_.empty()) return; | ||
| 1961 | const std::uint64_t done = completed(); | ||
| 1962 | while (!pin_queue_.empty() && pin_queue_.front().ticket <= done) { | ||
| 1963 | out.push_back(std::move(pin_queue_.front().pin)); | ||
| 1964 | pin_queue_.pop_front(); | ||
| 1965 | } | ||
| 1966 | } | ||
| 1967 | |||
| 1909 | // Appends `op`, which takes the pending waits with it (a marker has none). Called with state_mutex_ held. | 1968 | // Appends `op`, which takes the pending waits with it (a marker has none). Called with state_mutex_ held. |
| 1910 | void hold(Deferred&& op) { | 1969 | void hold(Deferred&& op) { |
| 1970 | op.seq = context_.holds().next_seq(); | ||
| 1911 | if (op.kind != Deferred::Kind::marker) { | 1971 | if (op.kind != Deferred::Kind::marker) { |
| 1912 | op.waits = std::move(waits_); | 1972 | op.waits = std::move(waits_); |
| 1913 | op.wait_values = std::move(wait_values_); | 1973 | op.wait_values = std::move(wait_values_); |
| 1974 | op.wait_pins = std::move(wait_pins_); | ||
| 1914 | waits_.clear(); | 1975 | waits_.clear(); |
| 1915 | wait_values_.clear(); | 1976 | wait_values_.clear(); |
| 1977 | wait_pins_.clear(); | ||
| 1916 | } | 1978 | } |
| 1917 | held_.push_back(std::move(op)); | 1979 | held_.push_back(std::move(op)); |
| 1918 | context_.holds().held_changed(1); | 1980 | context_.holds().held_changed(1); |
| 1919 | } | 1981 | } |
| 1920 | 1982 | ||
| 1921 | // Runs one held operation as the call would have, with its own waits and none of the ones queued since. | 1983 | // Runs one held operation as the call would have, with its own waits and none of the ones queued since. |
| 1984 | // A submission that fails leaves its waits for the next one, as when the call is not held (a transient | ||
| 1985 | // vkQueueSubmit failure is retried by the application's next call), ahead of the waits asked for since. | ||
| 1922 | void run(Deferred& op) { | 1986 | void run(Deferred& op) { |
| 1923 | auto later_waits = std::move(waits_); | 1987 | auto later_waits = std::move(waits_); |
| 1924 | auto later_values = std::move(wait_values_); | 1988 | auto later_values = std::move(wait_values_); |
| 1989 | auto later_pins = std::move(wait_pins_); | ||
| 1925 | waits_ = std::move(op.waits); | 1990 | waits_ = std::move(op.waits); |
| 1926 | wait_values_ = std::move(op.wait_values); | 1991 | wait_values_ = std::move(op.wait_values); |
| 1992 | wait_pins_ = std::move(op.wait_pins); // the fences stay alive until the submission has consumed their waits | ||
| 1927 | switch (op.kind) { | 1993 | switch (op.kind) { |
| 1928 | case Deferred::Kind::batch: { | 1994 | case Deferred::Kind::batch: { |
| 1929 | std::vector<const CommandList*> lists; | 1995 | std::vector<const CommandList*> lists; |
| 1930 | for (const CommandList& list : op.lists) lists.push_back(&list); | 1996 | for (const CommandList& list : op.lists) lists.push_back(&list); |
| 1997 | // The pipeline states and root signatures of the lists are in wait_pins_ too (see execute()): they live | ||
| 1998 | // until here, and are released once the batch has been recorded. | ||
| 1931 | const ReplayResult result = execute_now(lists); | 1999 | const ReplayResult result = execute_now(lists); |
| 1932 | if (result != ReplayResult::success) | 2000 | if (result != ReplayResult::success) { |
| 2001 | int none = static_cast<int>(ReplayResult::success); | ||
| 2002 | held_failure_.compare_exchange_strong(none, static_cast<int>(result)); | ||
| 1933 | std::fprintf(stderr, "[rt] d3d12: a held ExecuteCommandLists failed when it was released (result %d); " | 2003 | std::fprintf(stderr, "[rt] d3d12: a held ExecuteCommandLists failed when it was released (result %d); " |
| 1934 | "nothing was submitted\n", static_cast<int>(result)); | 2004 | "nothing was submitted for it, and a Signal behind it still signals\n", |
| 2005 | static_cast<int>(result)); | ||
| 2006 | } | ||
| 1935 | break; | 2007 | break; |
| 1936 | } | 2008 | } |
| 1937 | case Deferred::Kind::signal: | 2009 | case Deferred::Kind::signal: |
| @@ -1942,55 +2014,83 @@ private: | |||
| 1942 | marker_now(op.marker_kind, op.label.c_str()); | 2014 | marker_now(op.marker_kind, op.label.c_str()); |
| 1943 | break; | 2015 | break; |
| 1944 | } | 2016 | } |
| 1945 | waits_ = std::move(later_waits); // a failed submission leaves its waits behind: drop them with the batch | 2017 | retire_pins(op.pins, submitted_); // the fence it signals, the pipeline states: until its submission is done |
| 1946 | wait_values_ = std::move(later_values); | 2018 | waits_.insert(waits_.end(), later_waits.begin(), later_waits.end()); |
| 2019 | wait_values_.insert(wait_values_.end(), later_values.begin(), later_values.end()); | ||
| 2020 | for (auto& pin : later_pins) wait_pins_.push_back(std::move(pin)); | ||
| 1947 | } | 2021 | } |
| 1948 | 2022 | ||
| 1949 | bool ready(const Deferred& op) const { return !waits_pending(op.waits, op.wait_values); } | 2023 | bool ready(const Deferred& op) const { return !waits_pending(op.waits, op.wait_values); } |
| 1950 | 2024 | ||
| 1951 | // Holder: called by the thread whose Signal made a held operation ready (never with a queue lock held). | 2025 | // Holder: called by the thread whose Signal made a held operation ready (never with a queue lock held). |
| 1952 | bool release_ready() override { | 2026 | bool release_ready() override { |
| 1953 | std::lock_guard state(state_mutex_); | 2027 | std::vector<std::shared_ptr<void>> retired; |
| 1954 | bool released = false; | 2028 | bool released = false; |
| 1955 | while (!held_.empty() && (context_.is_device_lost() || ready(held_.front()))) { | 2029 | { |
| 1956 | Deferred op = std::move(held_.front()); | 2030 | std::lock_guard state(state_mutex_); |
| 1957 | held_.pop_front(); | 2031 | while (!held_.empty() && (context_.is_device_lost() || ready(held_.front()))) { |
| 1958 | context_.holds().held_changed(-1); | 2032 | Deferred op = std::move(held_.front()); |
| 1959 | if (!context_.is_device_lost()) run(op); | 2033 | held_.pop_front(); |
| 1960 | released = true; | 2034 | context_.holds().held_changed(-1); |
| 2035 | if (!context_.is_device_lost()) run(op); | ||
| 2036 | released = true; | ||
| 2037 | } | ||
| 2038 | drain_pins(retired); | ||
| 1961 | } | 2039 | } |
| 1962 | return released; | 2040 | return released; |
| 1963 | } | 2041 | } |
| 1964 | 2042 | ||
| 1965 | // Holder: the fence is released, so what waits on it can never run. | 2043 | // Holder: the fence is released, so what waits on it can never run. |
| 1966 | void discard_waiting_on(VkSemaphore semaphore) override { | 2044 | void discard_waiting_on(VkSemaphore semaphore) override { |
| 1967 | std::lock_guard state(state_mutex_); | 2045 | std::vector<Deferred> dropped; |
| 1968 | for (auto it = held_.begin(); it != held_.end();) { | 2046 | { |
| 1969 | if (std::find(it->waits.begin(), it->waits.end(), semaphore) == it->waits.end()) { | 2047 | std::lock_guard state(state_mutex_); |
| 1970 | ++it; | 2048 | for (auto it = held_.begin(); it != held_.end();) { |
| 1971 | continue; | 2049 | if (std::find(it->waits.begin(), it->waits.end(), semaphore) == it->waits.end() && |
| 2050 | !(it->kind == Deferred::Kind::signal && it->fence == semaphore)) { | ||
| 2051 | ++it; | ||
| 2052 | continue; | ||
| 2053 | } | ||
| 2054 | std::fprintf(stderr, "[rt] d3d12: a fence was released while a queue still held work that names it; " | ||
| 2055 | "that work is dropped\n"); | ||
| 2056 | dropped.push_back(std::move(*it)); | ||
| 2057 | it = held_.erase(it); | ||
| 2058 | context_.holds().held_changed(-1); | ||
| 1972 | } | 2059 | } |
| 1973 | std::fprintf(stderr, "[rt] d3d12: a fence was released while a queue still held work waiting on it; " | ||
| 1974 | "that work is dropped\n"); | ||
| 1975 | it = held_.erase(it); | ||
| 1976 | context_.holds().held_changed(-1); | ||
| 1977 | } | 2060 | } |
| 1978 | } | 2061 | } |
| 1979 | 2062 | ||
| 1980 | // A queue destroyed with held work: the fence it waits on was never signaled. Reported, not waited for; nothing | 2063 | // Holder: the sequence number of the oldest held operation. |
| 1981 | // was submitted for it, so the VkQueue is idle and the destructor goes on. | 2064 | std::uint64_t oldest_held_seq() override { |
| 1982 | void report_held_at_teardown() { | ||
| 1983 | std::lock_guard state(state_mutex_); | 2065 | std::lock_guard state(state_mutex_); |
| 1984 | if (held_.empty()) return; | 2066 | return held_.empty() ? 0 : held_.front().seq; |
| 1985 | std::uint64_t value = 0; | 2067 | } |
| 1986 | for (const Deferred& op : held_) | 2068 | |
| 1987 | for (std::size_t i = 0; i < op.waits.size() && !value; ++i) | 2069 | // A queue destroyed with held work. Reported, not waited for: nothing was submitted for it, so the VkQueue is |
| 1988 | if (context_.holds().pending(op.waits[i], op.wait_values[i])) value = op.wait_values[i]; | 2070 | // idle and the destructor goes on. The fences of held Signals will never reach their values, so what is waiting |
| 1989 | std::fprintf(stderr, "[rt] d3d12: queue destroyed with %zu held operation(s) never submitted: waits for a " | 2071 | // for them on the CPU (SetEventOnCompletion, as a released fence completes them) is woken, and the batches |
| 1990 | "fence value (%llu) that no Signal reached; the fence was never signaled\n", | 2072 | // that wait for them on other queues stay held (and are reported when those queues are destroyed). |
| 1991 | held_.size(), static_cast<unsigned long long>(value)); | 2073 | void report_held_at_teardown() { |
| 1992 | context_.holds().held_changed(-static_cast<std::int64_t>(held_.size())); | 2074 | std::deque<Deferred> dropped; |
| 1993 | held_.clear(); | 2075 | { |
| 2076 | std::lock_guard state(state_mutex_); | ||
| 2077 | if (held_.empty()) return; | ||
| 2078 | std::uint64_t value = 0; | ||
| 2079 | for (const Deferred& op : held_) | ||
| 2080 | for (std::size_t i = 0; i < op.waits.size() && !value; ++i) | ||
| 2081 | if (context_.holds().pending(op.waits[i], op.wait_values[i])) value = op.wait_values[i]; | ||
| 2082 | std::size_t signals = 0; | ||
| 2083 | for (const Deferred& op : held_) signals += op.kind == Deferred::Kind::signal; | ||
| 2084 | std::fprintf(stderr, "[rt] d3d12: queue destroyed with %zu held operation(s) never submitted: waits for a " | ||
| 2085 | "fence value (%llu) that no Signal reached; the fence was never signaled%s\n", | ||
| 2086 | held_.size(), static_cast<unsigned long long>(value), | ||
| 2087 | signals ? "; CPU waits on the fences of its held Signals are completed" : ""); | ||
| 2088 | for (const Deferred& op : held_) | ||
| 2089 | if (op.kind == Deferred::Kind::signal) context_.cancel_fence_waits(op.fence); | ||
| 2090 | context_.holds().held_changed(-static_cast<std::int64_t>(held_.size())); | ||
| 2091 | dropped.swap(held_); | ||
| 2092 | } | ||
| 2093 | // The pins (fences, pipeline states) are released here, outside the lock. | ||
| 1994 | } | 2094 | } |
| 1995 | 2095 | ||
| 1996 | struct CommandSlot { | 2096 | struct CommandSlot { |
| @@ -2074,8 +2174,7 @@ private: | |||
| 2074 | context_.record_submit_locked(progress_, ticket); | 2174 | context_.record_submit_locked(progress_, ticket); |
| 2075 | } | 2175 | } |
| 2076 | submitted_ = last_sparse_ = ticket; | 2176 | submitted_ = last_sparse_ = ticket; |
| 2077 | waits_.clear(); | 2177 | clear_waits(ticket); |
| 2078 | wait_values_.clear(); | ||
| 2079 | for (auto it = changes.begin(); it != changes.end();) { | 2178 | for (auto it = changes.begin(); it != changes.end();) { |
| 2080 | buffer->mapped.erase(it->first); | 2179 | buffer->mapped.erase(it->first); |
| 2081 | if (it->second.heap) ++it; | 2180 | if (it->second.heap) ++it; |
| @@ -2149,7 +2248,10 @@ private: | |||
| 2149 | VkSemaphore signals[2]{progress_, VK_NULL_HANDLE}; | 2248 | VkSemaphore signals[2]{progress_, VK_NULL_HANDLE}; |
| 2150 | std::uint64_t signal_values[2]{ticket, 0}; | 2249 | std::uint64_t signal_values[2]{ticket, 0}; |
| 2151 | std::uint32_t signal_count = 1; | 2250 | std::uint32_t signal_count = 1; |
| 2152 | if (fence) { | 2251 | // Claimed under the submit lock, so the order of the claims is the order of the submissions. A Signal |
| 2252 | // that is not above what was already submitted for this fence (a held Signal(F, 2) released after an | ||
| 2253 | // unheld Signal(F, 3)) leaves the Vulkan signal out: the timeline cannot go down, and waits for 2 pass. | ||
| 2254 | if (fence && context_.holds().claim_signal(fence, value)) { | ||
| 2153 | signals[signal_count] = fence; | 2255 | signals[signal_count] = fence; |
| 2154 | signal_values[signal_count] = value; | 2256 | signal_values[signal_count] = value; |
| 2155 | ++signal_count; | 2257 | ++signal_count; |
| @@ -2172,16 +2274,13 @@ private: | |||
| 2172 | } | 2274 | } |
| 2173 | if (res == VK_ERROR_DEVICE_LOST) { | 2275 | if (res == VK_ERROR_DEVICE_LOST) { |
| 2174 | context_.mark_device_lost(); | 2276 | context_.mark_device_lost(); |
| 2175 | waits_.clear(); | 2277 | clear_waits(); |
| 2176 | wait_values_.clear(); | ||
| 2177 | return false; | 2278 | return false; |
| 2178 | } | 2279 | } |
| 2179 | const bool ok = (res == VK_SUCCESS); | 2280 | const bool ok = (res == VK_SUCCESS); |
| 2180 | if (ok) { | 2281 | if (ok) { |
| 2181 | if (fence) context_.holds().signaled(fence, value); // held batches waiting for it: the caller pumps | ||
| 2182 | submitted_ = ticket; | 2282 | submitted_ = ticket; |
| 2183 | waits_.clear(); | 2283 | clear_waits(ticket); |
| 2184 | wait_values_.clear(); | ||
| 2185 | for (auto& slot : buffers_) | 2284 | for (auto& slot : buffers_) |
| 2186 | if (slot.buffer == command_buffer) slot.ticket = ticket; | 2285 | if (slot.buffer == command_buffer) slot.ticket = ticket; |
| 2187 | } | 2286 | } |
| @@ -2211,6 +2310,14 @@ private: | |||
| 2211 | // guards the members above that execute, signal, wait and marker touch. | 2310 | // guards the members above that execute, signal, wait and marker touch. |
| 2212 | std::mutex state_mutex_; | 2311 | std::mutex state_mutex_; |
| 2213 | std::deque<Deferred> held_; | 2312 | std::deque<Deferred> held_; |
| 2313 | std::vector<std::shared_ptr<void>> wait_pins_; // keep the fences of waits_ alive | ||
| 2314 | std::vector<std::shared_ptr<void>> retired_pins_; // released by the caller once state_mutex_ is dropped | ||
| 2315 | struct TicketedPin { | ||
| 2316 | std::uint64_t ticket; | ||
| 2317 | std::shared_ptr<void> pin; | ||
| 2318 | }; | ||
| 2319 | std::deque<TicketedPin> pin_queue_; // released once progress_ reaches the ticket | ||
| 2320 | std::atomic<int> held_failure_{0}; // a ReplayResult: the first held batch that failed | ||
| 2214 | }; | 2321 | }; |
| 2215 | 2322 | ||
| 2216 | } // namespace leonida | 2323 | } // namespace leonida |
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() |