diff options
Diffstat (limited to 'runtime/src/vulkan_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 219 |
1 files changed, 169 insertions, 50 deletions
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h index 65d76532a..99add4faf 100644 --- a/runtime/src/vulkan_backend.h +++ b/runtime/src/vulkan_backend.h | |||
| @@ -1572,6 +1572,7 @@ public: | |||
| 1572 | auto& vk = context_.vk; | 1572 | auto& vk = context_.vk; |
| 1573 | context_.holds().detach(this); // no thread releases into this queue from here on | 1573 | context_.holds().detach(this); // no thread releases into this queue from here on |
| 1574 | report_held_at_teardown(); | 1574 | report_held_at_teardown(); |
| 1575 | context_.holds().pump_ready(); // presents queued behind what was just dropped can run | ||
| 1575 | if (queue_) { | 1576 | if (queue_) { |
| 1576 | std::lock_guard lock(context_.submit_mutex()); | 1577 | std::lock_guard lock(context_.submit_mutex()); |
| 1577 | for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); | 1578 | for (; open_labels_; --open_labels_) context_.labels_.queue_end(queue_); |
| @@ -1678,10 +1679,17 @@ public: | |||
| 1678 | // the layout tracker as it was. Pending Wait()s are attached to this submission. When one of them (or an | 1679 | // the layout tracker as it was. Pending Wait()s are attached to this submission. When one of them (or an |
| 1679 | // earlier held operation) waits for a fence value no Signal has been submitted for, the lists are copied and | 1680 | // earlier held operation) waits for a fence value no Signal has been submitted for, the lists are copied and |
| 1680 | // held instead, and the call succeeds: the batch is replayed by the thread that submits the Signal (or the CPU | 1681 | // held instead, and the call succeeds: the batch is replayed by the thread that submits the Signal (or the CPU |
| 1681 | // Signal), which is also where its errors are reported. | 1682 | // Signal), which is also where its errors are reported (stderr, and take_held_failure()). |
| 1682 | ReplayResult execute(std::span<const CommandList* const> lists) { | 1683 | // |
| 1684 | // `pin`, called only when the batch is held, returns what has to stay alive until the batch has run: the COM layer | ||
| 1685 | // passes the pipeline states and root signatures the lists named (the application may release them and Reset | ||
| 1686 | // the lists as soon as the call returns). | ||
| 1687 | using PinFn = std::function<std::shared_ptr<void>()>; | ||
| 1688 | |||
| 1689 | ReplayResult execute(std::span<const CommandList* const> lists, const PinFn& pin = {}) { | ||
| 1683 | ReplayResult result = ReplayResult::success; | 1690 | ReplayResult result = ReplayResult::success; |
| 1684 | bool held = false; | 1691 | bool held = false; |
| 1692 | std::vector<std::shared_ptr<void>> retired; // destroyed after state_mutex_ is released | ||
| 1685 | { | 1693 | { |
| 1686 | std::lock_guard state(state_mutex_); | 1694 | std::lock_guard state(state_mutex_); |
| 1687 | if (!must_hold()) { | 1695 | if (!must_hold()) { |
| @@ -1693,18 +1701,23 @@ public: | |||
| 1693 | op.kind = Deferred::Kind::batch; | 1701 | op.kind = Deferred::Kind::batch; |
| 1694 | op.lists.reserve(lists.size()); | 1702 | op.lists.reserve(lists.size()); |
| 1695 | for (const CommandList* list : lists) op.lists.push_back(*list); | 1703 | for (const CommandList* list : lists) op.lists.push_back(*list); |
| 1704 | if (pin) op.pins.push_back(pin()); | ||
| 1696 | hold(std::move(op)); | 1705 | hold(std::move(op)); |
| 1697 | held = true; | 1706 | held = true; |
| 1698 | } | 1707 | } |
| 1708 | drain_pins(retired); | ||
| 1699 | } | 1709 | } |
| 1710 | retired.clear(); | ||
| 1700 | if (held) context_.holds().pump_ready(); // a Signal may have been submitted since the check | 1711 | if (held) context_.holds().pump_ready(); // a Signal may have been submitted since the check |
| 1701 | return result; | 1712 | return result; |
| 1702 | } | 1713 | } |
| 1703 | 1714 | ||
| 1704 | // ID3D12CommandQueue::Signal: the fence reaches `value` after all work submitted so far. Held behind earlier | 1715 | // ID3D12CommandQueue::Signal: the fence reaches `value` after all work submitted so far. Held behind earlier |
| 1705 | // held operations, so the fence does not run ahead of the work it follows. | 1716 | // held operations, so the fence does not run ahead of the work it follows. `pin` (called only when the Signal is |
| 1706 | bool signal(VkSemaphore fence, std::uint64_t value) { | 1717 | // held) keeps the fence object alive until it has been submitted, as D3D12 does. |
| 1718 | bool signal(VkSemaphore fence, std::uint64_t value, const PinFn& pin = {}) { | ||
| 1707 | bool ok = true; | 1719 | bool ok = true; |
| 1720 | std::vector<std::shared_ptr<void>> retired; | ||
| 1708 | { | 1721 | { |
| 1709 | std::lock_guard state(state_mutex_); | 1722 | std::lock_guard state(state_mutex_); |
| 1710 | if (must_hold()) { | 1723 | if (must_hold()) { |
| @@ -1712,27 +1725,45 @@ public: | |||
| 1712 | op.kind = Deferred::Kind::signal; | 1725 | op.kind = Deferred::Kind::signal; |
| 1713 | op.fence = fence; | 1726 | op.fence = fence; |
| 1714 | op.value = value; | 1727 | op.value = value; |
| 1728 | if (pin) op.pins.push_back(pin()); | ||
| 1715 | hold(std::move(op)); | 1729 | hold(std::move(op)); |
| 1716 | } else { | 1730 | } else { |
| 1717 | ok = submit(VK_NULL_HANDLE, fence, value); | 1731 | ok = submit(VK_NULL_HANDLE, fence, value); |
| 1718 | } | 1732 | } |
| 1733 | drain_pins(retired); | ||
| 1719 | } | 1734 | } |
| 1735 | retired.clear(); | ||
| 1720 | context_.holds().pump_ready(); // this Signal, or the hold above, may have made a held batch ready | 1736 | context_.holds().pump_ready(); // this Signal, or the hold above, may have made a held batch ready |
| 1721 | return ok; | 1737 | return ok; |
| 1722 | } | 1738 | } |
| 1723 | 1739 | ||
| 1724 | // ID3D12CommandQueue::Wait: later submissions wait for the fence. A wait in a Vulkan batch only holds back that | 1740 | // ID3D12CommandQueue::Wait: later submissions wait for the fence. A wait in a Vulkan batch only holds back that |
| 1725 | // batch, so it is kept and attached to the next submission (held on the CPU if no Signal reaches it yet). | 1741 | // batch, so it is kept and attached to the next submission (held on the CPU if no Signal reaches it yet). |
| 1726 | void wait(VkSemaphore fence, std::uint64_t value) { | 1742 | // `pin` returns what keeps the fence object alive until then (a D3D12 queue holds its fences). |
| 1727 | std::lock_guard state(state_mutex_); | 1743 | void wait(VkSemaphore fence, std::uint64_t value, const PinFn& pin = {}) { |
| 1728 | waits_.push_back(fence); | 1744 | std::vector<std::shared_ptr<void>> retired; // fences whose submission has completed: released unlocked |
| 1729 | wait_values_.push_back(value); | 1745 | { |
| 1746 | std::lock_guard state(state_mutex_); | ||
| 1747 | waits_.push_back(fence); | ||
| 1748 | wait_values_.push_back(value); | ||
| 1749 | if (pin) wait_pins_.push_back(pin()); | ||
| 1750 | drain_pins(retired); | ||
| 1751 | } | ||
| 1752 | } | ||
| 1753 | |||
| 1754 | // The first failure of a held ExecuteCommandLists since the last call (what the call would have returned had it | ||
| 1755 | // not been held), or success. A Signal behind a failed batch still signals, as when the failure is not held: the | ||
| 1756 | // work did not run, the application is told here and on stderr, and nothing waits forever. | ||
| 1757 | ReplayResult take_held_failure() { | ||
| 1758 | return static_cast<ReplayResult>(held_failure_.exchange(static_cast<int>(ReplayResult::success))); | ||
| 1730 | } | 1759 | } |
| 1731 | 1760 | ||
| 1732 | // ID3D12CommandQueue::SetMarker/BeginEvent/EndEvent: queue labels, between the submissions around them. An end | 1761 | // ID3D12CommandQueue::SetMarker/BeginEvent/EndEvent: queue labels, between the submissions around them. An end |
| 1733 | // with no begin open on this queue is dropped, as Vulkan requires a matching begin. No-ops without the extension. | 1762 | // with no begin open on this queue is dropped, as Vulkan requires a matching begin. No-ops without the extension. |
| 1734 | void marker(DebugMarker::Kind kind, const char* label) { | 1763 | void marker(DebugMarker::Kind kind, const char* label) { |
| 1764 | std::vector<std::shared_ptr<void>> retired; | ||
| 1735 | std::lock_guard state(state_mutex_); | 1765 | std::lock_guard state(state_mutex_); |
| 1766 | drain_pins(retired); // destroyed after the lock (reverse declaration order) | ||
| 1736 | if (!held_.empty()) { // behind the held work, so behind it in the queue's labels too | 1767 | if (!held_.empty()) { // behind the held work, so behind it in the queue's labels too |
| 1737 | Deferred op; | 1768 | Deferred op; |
| 1738 | op.kind = Deferred::Kind::marker; | 1769 | op.kind = Deferred::Kind::marker; |
| @@ -2156,6 +2187,7 @@ private: | |||
| 2156 | struct Deferred { | 2187 | struct Deferred { |
| 2157 | enum class Kind : std::uint8_t { batch, signal, marker }; | 2188 | enum class Kind : std::uint8_t { batch, signal, marker }; |
| 2158 | Kind kind = Kind::batch; | 2189 | Kind kind = Kind::batch; |
| 2190 | std::uint64_t seq = 0; // QueueHolds::next_seq(): the order operations were held in, across queues | ||
| 2159 | std::vector<VkSemaphore> waits; | 2191 | std::vector<VkSemaphore> waits; |
| 2160 | std::vector<std::uint64_t> wait_values; | 2192 | std::vector<std::uint64_t> wait_values; |
| 2161 | std::vector<CommandList> lists; // batch: copies, so the application may Reset its lists after the call | 2193 | std::vector<CommandList> lists; // batch: copies, so the application may Reset its lists after the call |
| @@ -2163,6 +2195,10 @@ private: | |||
| 2163 | std::uint64_t value = 0; | 2195 | std::uint64_t value = 0; |
| 2164 | DebugMarker::Kind marker_kind = DebugMarker::Kind::set; // marker | 2196 | DebugMarker::Kind marker_kind = DebugMarker::Kind::set; // marker |
| 2165 | std::string label; | 2197 | std::string label; |
| 2198 | // Alive until the operation has run: the fences it waits on and signals, the pipeline states and root | ||
| 2199 | // signatures of the lists (the application may release them right after the call). | ||
| 2200 | std::vector<std::shared_ptr<void>> pins; | ||
| 2201 | std::vector<std::shared_ptr<void>> wait_pins; // the fences of `waits` | ||
| 2166 | }; | 2202 | }; |
| 2167 | 2203 | ||
| 2168 | bool waits_pending(const std::vector<VkSemaphore>& semaphores, const std::vector<std::uint64_t>& values) const { | 2204 | bool waits_pending(const std::vector<VkSemaphore>& semaphores, const std::vector<std::uint64_t>& values) const { |
| @@ -2175,32 +2211,75 @@ private: | |||
| 2175 | // has no submitted Signal yet. Called with state_mutex_ held. | 2211 | // has no submitted Signal yet. Called with state_mutex_ held. |
| 2176 | bool must_hold() const { return !held_.empty() || waits_pending(waits_, wait_values_); } | 2212 | bool must_hold() const { return !held_.empty() || waits_pending(waits_, wait_values_); } |
| 2177 | 2213 | ||
| 2214 | // The waits are consumed: submitted as `ticket` (0: dropped on device loss). What the application may release | ||
| 2215 | // (the fences they named) is kept until that submission has completed on the GPU, as a D3D12 queue does: | ||
| 2216 | // destroying a semaphore a pending submission uses is a validation error. Retired pins are destroyed by the | ||
| 2217 | // caller after state_mutex_ is released (drain_pins): the last release of a fence waits for a running release, | ||
| 2218 | // which must not be waiting for this lock. | ||
| 2219 | void clear_waits(std::uint64_t ticket = 0) { | ||
| 2220 | waits_.clear(); | ||
| 2221 | wait_values_.clear(); | ||
| 2222 | retire_pins(wait_pins_, ticket); | ||
| 2223 | } | ||
| 2224 | |||
| 2225 | void retire_pins(std::vector<std::shared_ptr<void>>& pins, std::uint64_t ticket) { | ||
| 2226 | for (auto& pin : pins) { | ||
| 2227 | if (ticket) pin_queue_.push_back({ticket, std::move(pin)}); | ||
| 2228 | else retired_pins_.push_back(std::move(pin)); | ||
| 2229 | } | ||
| 2230 | pins.clear(); | ||
| 2231 | } | ||
| 2232 | |||
| 2233 | // Moves the pins whose submission has completed (and the ones retired outright) to `out`. state_mutex_ held. | ||
| 2234 | void drain_pins(std::vector<std::shared_ptr<void>>& out) { | ||
| 2235 | out.swap(retired_pins_); | ||
| 2236 | if (pin_queue_.empty()) return; | ||
| 2237 | const std::uint64_t done = completed(); | ||
| 2238 | while (!pin_queue_.empty() && pin_queue_.front().ticket <= done) { | ||
| 2239 | out.push_back(std::move(pin_queue_.front().pin)); | ||
| 2240 | pin_queue_.pop_front(); | ||
| 2241 | } | ||
| 2242 | } | ||
| 2243 | |||
| 2178 | // Appends `op`, which takes the pending waits with it (a marker has none). Called with state_mutex_ held. | 2244 | // Appends `op`, which takes the pending waits with it (a marker has none). Called with state_mutex_ held. |
| 2179 | void hold(Deferred&& op) { | 2245 | void hold(Deferred&& op) { |
| 2246 | op.seq = context_.holds().next_seq(); | ||
| 2180 | if (op.kind != Deferred::Kind::marker) { | 2247 | if (op.kind != Deferred::Kind::marker) { |
| 2181 | op.waits = std::move(waits_); | 2248 | op.waits = std::move(waits_); |
| 2182 | op.wait_values = std::move(wait_values_); | 2249 | op.wait_values = std::move(wait_values_); |
| 2250 | op.wait_pins = std::move(wait_pins_); | ||
| 2183 | waits_.clear(); | 2251 | waits_.clear(); |
| 2184 | wait_values_.clear(); | 2252 | wait_values_.clear(); |
| 2253 | wait_pins_.clear(); | ||
| 2185 | } | 2254 | } |
| 2186 | held_.push_back(std::move(op)); | 2255 | held_.push_back(std::move(op)); |
| 2187 | context_.holds().held_changed(1); | 2256 | context_.holds().held_changed(1); |
| 2188 | } | 2257 | } |
| 2189 | 2258 | ||
| 2190 | // Runs one held operation as the call would have, with its own waits and none of the ones queued since. | 2259 | // Runs one held operation as the call would have, with its own waits and none of the ones queued since. |
| 2260 | // A submission that fails leaves its waits for the next one, as when the call is not held (a transient | ||
| 2261 | // vkQueueSubmit failure is retried by the application's next call), ahead of the waits asked for since. | ||
| 2191 | void run(Deferred& op) { | 2262 | void run(Deferred& op) { |
| 2192 | auto later_waits = std::move(waits_); | 2263 | auto later_waits = std::move(waits_); |
| 2193 | auto later_values = std::move(wait_values_); | 2264 | auto later_values = std::move(wait_values_); |
| 2265 | auto later_pins = std::move(wait_pins_); | ||
| 2194 | waits_ = std::move(op.waits); | 2266 | waits_ = std::move(op.waits); |
| 2195 | wait_values_ = std::move(op.wait_values); | 2267 | wait_values_ = std::move(op.wait_values); |
| 2268 | wait_pins_ = std::move(op.wait_pins); // the fences stay alive until the submission has consumed their waits | ||
| 2196 | switch (op.kind) { | 2269 | switch (op.kind) { |
| 2197 | case Deferred::Kind::batch: { | 2270 | case Deferred::Kind::batch: { |
| 2198 | std::vector<const CommandList*> lists; | 2271 | std::vector<const CommandList*> lists; |
| 2199 | for (const CommandList& list : op.lists) lists.push_back(&list); | 2272 | for (const CommandList& list : op.lists) lists.push_back(&list); |
| 2273 | // The pipeline states and root signatures of the lists are in op.pins (see execute()): they live until | ||
| 2274 | // the batch has been recorded and then, with the fences, until its submission has completed (below). | ||
| 2200 | const ReplayResult result = execute_now(lists); | 2275 | const ReplayResult result = execute_now(lists); |
| 2201 | if (result != ReplayResult::success) | 2276 | if (result != ReplayResult::success) { |
| 2277 | int none = static_cast<int>(ReplayResult::success); | ||
| 2278 | held_failure_.compare_exchange_strong(none, static_cast<int>(result)); | ||
| 2202 | std::fprintf(stderr, "[rt] d3d12: a held ExecuteCommandLists failed when it was released (result %d); " | 2279 | std::fprintf(stderr, "[rt] d3d12: a held ExecuteCommandLists failed when it was released (result %d); " |
| 2203 | "nothing was submitted\n", static_cast<int>(result)); | 2280 | "nothing was submitted for it, and a Signal behind it still signals\n", |
| 2281 | static_cast<int>(result)); | ||
| 2282 | } | ||
| 2204 | break; | 2283 | break; |
| 2205 | } | 2284 | } |
| 2206 | case Deferred::Kind::signal: | 2285 | case Deferred::Kind::signal: |
| @@ -2211,55 +2290,83 @@ private: | |||
| 2211 | marker_now(op.marker_kind, op.label.c_str()); | 2290 | marker_now(op.marker_kind, op.label.c_str()); |
| 2212 | break; | 2291 | break; |
| 2213 | } | 2292 | } |
| 2214 | waits_ = std::move(later_waits); // a failed submission leaves its waits behind: drop them with the batch | 2293 | retire_pins(op.pins, submitted_); // the fence it signals, the pipeline states: until its submission is done |
| 2215 | wait_values_ = std::move(later_values); | 2294 | waits_.insert(waits_.end(), later_waits.begin(), later_waits.end()); |
| 2295 | wait_values_.insert(wait_values_.end(), later_values.begin(), later_values.end()); | ||
| 2296 | for (auto& pin : later_pins) wait_pins_.push_back(std::move(pin)); | ||
| 2216 | } | 2297 | } |
| 2217 | 2298 | ||
| 2218 | bool ready(const Deferred& op) const { return !waits_pending(op.waits, op.wait_values); } | 2299 | bool ready(const Deferred& op) const { return !waits_pending(op.waits, op.wait_values); } |
| 2219 | 2300 | ||
| 2220 | // Holder: called by the thread whose Signal made a held operation ready (never with a queue lock held). | 2301 | // Holder: called by the thread whose Signal made a held operation ready (never with a queue lock held). |
| 2221 | bool release_ready() override { | 2302 | bool release_ready() override { |
| 2222 | std::lock_guard state(state_mutex_); | 2303 | std::vector<std::shared_ptr<void>> retired; |
| 2223 | bool released = false; | 2304 | bool released = false; |
| 2224 | while (!held_.empty() && (context_.is_device_lost() || ready(held_.front()))) { | 2305 | { |
| 2225 | Deferred op = std::move(held_.front()); | 2306 | std::lock_guard state(state_mutex_); |
| 2226 | held_.pop_front(); | 2307 | while (!held_.empty() && (context_.is_device_lost() || ready(held_.front()))) { |
| 2227 | context_.holds().held_changed(-1); | 2308 | Deferred op = std::move(held_.front()); |
| 2228 | if (!context_.is_device_lost()) run(op); | 2309 | held_.pop_front(); |
| 2229 | released = true; | 2310 | context_.holds().held_changed(-1); |
| 2311 | if (!context_.is_device_lost()) run(op); | ||
| 2312 | released = true; | ||
| 2313 | } | ||
| 2314 | drain_pins(retired); | ||
| 2230 | } | 2315 | } |
| 2231 | return released; | 2316 | return released; |
| 2232 | } | 2317 | } |
| 2233 | 2318 | ||
| 2234 | // Holder: the fence is released, so what waits on it can never run. | 2319 | // Holder: the fence is released, so what waits on it can never run. |
| 2235 | void discard_waiting_on(VkSemaphore semaphore) override { | 2320 | void discard_waiting_on(VkSemaphore semaphore) override { |
| 2236 | std::lock_guard state(state_mutex_); | 2321 | std::vector<Deferred> dropped; |
| 2237 | for (auto it = held_.begin(); it != held_.end();) { | 2322 | { |
| 2238 | if (std::find(it->waits.begin(), it->waits.end(), semaphore) == it->waits.end()) { | 2323 | std::lock_guard state(state_mutex_); |
| 2239 | ++it; | 2324 | for (auto it = held_.begin(); it != held_.end();) { |
| 2240 | continue; | 2325 | if (std::find(it->waits.begin(), it->waits.end(), semaphore) == it->waits.end() && |
| 2326 | !(it->kind == Deferred::Kind::signal && it->fence == semaphore)) { | ||
| 2327 | ++it; | ||
| 2328 | continue; | ||
| 2329 | } | ||
| 2330 | std::fprintf(stderr, "[rt] d3d12: a fence was released while a queue still held work that names it; " | ||
| 2331 | "that work is dropped\n"); | ||
| 2332 | dropped.push_back(std::move(*it)); | ||
| 2333 | it = held_.erase(it); | ||
| 2334 | context_.holds().held_changed(-1); | ||
| 2241 | } | 2335 | } |
| 2242 | std::fprintf(stderr, "[rt] d3d12: a fence was released while a queue still held work waiting on it; " | ||
| 2243 | "that work is dropped\n"); | ||
| 2244 | it = held_.erase(it); | ||
| 2245 | context_.holds().held_changed(-1); | ||
| 2246 | } | 2336 | } |
| 2247 | } | 2337 | } |
| 2248 | 2338 | ||
| 2249 | // A queue destroyed with held work: the fence it waits on was never signaled. Reported, not waited for; nothing | 2339 | // Holder: the sequence number of the oldest held operation. |
| 2250 | // was submitted for it, so the VkQueue is idle and the destructor goes on. | 2340 | std::uint64_t oldest_held_seq() override { |
| 2251 | void report_held_at_teardown() { | ||
| 2252 | std::lock_guard state(state_mutex_); | 2341 | std::lock_guard state(state_mutex_); |
| 2253 | if (held_.empty()) return; | 2342 | return held_.empty() ? 0 : held_.front().seq; |
| 2254 | std::uint64_t value = 0; | 2343 | } |
| 2255 | for (const Deferred& op : held_) | 2344 | |
| 2256 | for (std::size_t i = 0; i < op.waits.size() && !value; ++i) | 2345 | // A queue destroyed with held work. Reported, not waited for: nothing was submitted for it, so the VkQueue is |
| 2257 | if (context_.holds().pending(op.waits[i], op.wait_values[i])) value = op.wait_values[i]; | 2346 | // idle and the destructor goes on. The fences of held Signals will never reach their values, so what is waiting |
| 2258 | std::fprintf(stderr, "[rt] d3d12: queue destroyed with %zu held operation(s) never submitted: waits for a " | 2347 | // for them on the CPU (SetEventOnCompletion, as a released fence completes them) is woken, and the batches |
| 2259 | "fence value (%llu) that no Signal reached; the fence was never signaled\n", | 2348 | // that wait for them on other queues stay held (and are reported when those queues are destroyed). |
| 2260 | held_.size(), static_cast<unsigned long long>(value)); | 2349 | void report_held_at_teardown() { |
| 2261 | context_.holds().held_changed(-static_cast<std::int64_t>(held_.size())); | 2350 | std::deque<Deferred> dropped; |
| 2262 | held_.clear(); | 2351 | { |
| 2352 | std::lock_guard state(state_mutex_); | ||
| 2353 | if (held_.empty()) return; | ||
| 2354 | std::uint64_t value = 0; | ||
| 2355 | for (const Deferred& op : held_) | ||
| 2356 | for (std::size_t i = 0; i < op.waits.size() && !value; ++i) | ||
| 2357 | if (context_.holds().pending(op.waits[i], op.wait_values[i])) value = op.wait_values[i]; | ||
| 2358 | std::size_t signals = 0; | ||
| 2359 | for (const Deferred& op : held_) signals += op.kind == Deferred::Kind::signal; | ||
| 2360 | std::fprintf(stderr, "[rt] d3d12: queue destroyed with %zu held operation(s) never submitted: waits for a " | ||
| 2361 | "fence value (%llu) that no Signal reached; the fence was never signaled%s\n", | ||
| 2362 | held_.size(), static_cast<unsigned long long>(value), | ||
| 2363 | signals ? "; CPU waits on the fences of its held Signals are completed" : ""); | ||
| 2364 | for (const Deferred& op : held_) | ||
| 2365 | if (op.kind == Deferred::Kind::signal) context_.cancel_fence_waits(op.fence); | ||
| 2366 | context_.holds().held_changed(-static_cast<std::int64_t>(held_.size())); | ||
| 2367 | dropped.swap(held_); | ||
| 2368 | } | ||
| 2369 | // The pins (fences, pipeline states) are released here, outside the lock. | ||
| 2263 | } | 2370 | } |
| 2264 | 2371 | ||
| 2265 | struct CommandSlot { | 2372 | struct CommandSlot { |
| @@ -2396,8 +2503,7 @@ private: | |||
| 2396 | context_.record_submit_locked(progress_, ticket); | 2503 | context_.record_submit_locked(progress_, ticket); |
| 2397 | } | 2504 | } |
| 2398 | submitted_ = last_sparse_ = ticket; | 2505 | submitted_ = last_sparse_ = ticket; |
| 2399 | waits_.clear(); | 2506 | clear_waits(ticket); |
| 2400 | wait_values_.clear(); | ||
| 2401 | for (auto it = changes.begin(); it != changes.end();) { | 2507 | for (auto it = changes.begin(); it != changes.end();) { |
| 2402 | resource->mapped.erase(it->first); | 2508 | resource->mapped.erase(it->first); |
| 2403 | if (it->second.heap) ++it; | 2509 | if (it->second.heap) ++it; |
| @@ -2490,7 +2596,12 @@ private: | |||
| 2490 | VkSemaphore signals[2]{progress_, VK_NULL_HANDLE}; | 2596 | VkSemaphore signals[2]{progress_, VK_NULL_HANDLE}; |
| 2491 | std::uint64_t signal_values[2]{ticket, 0}; | 2597 | std::uint64_t signal_values[2]{ticket, 0}; |
| 2492 | std::uint32_t signal_count = 1; | 2598 | std::uint32_t signal_count = 1; |
| 2493 | if (fence) { | 2599 | // Decided under the submit lock, so the order of the checks is the order of the submissions. A Signal |
| 2600 | // that is not above what was already submitted for this fence (a held Signal(F, 2) released after an | ||
| 2601 | // unheld Signal(F, 3)) leaves the Vulkan signal out: the timeline cannot go down, and waits for 2 pass. | ||
| 2602 | // The value counts as submitted only after the submit succeeded (below). | ||
| 2603 | const bool signals_fence = fence && context_.holds().would_signal(fence, value); | ||
| 2604 | if (signals_fence) { | ||
| 2494 | signals[signal_count] = fence; | 2605 | signals[signal_count] = fence; |
| 2495 | signal_values[signal_count] = value; | 2606 | signal_values[signal_count] = value; |
| 2496 | ++signal_count; | 2607 | ++signal_count; |
| @@ -2509,20 +2620,20 @@ private: | |||
| 2509 | submit.signalSemaphoreCount = signal_count; | 2620 | submit.signalSemaphoreCount = signal_count; |
| 2510 | submit.pSignalSemaphores = signals; | 2621 | submit.pSignalSemaphores = signals; |
| 2511 | res = context_.vk.vkQueueSubmit(queue_, 1, &submit, VK_NULL_HANDLE); | 2622 | res = context_.vk.vkQueueSubmit(queue_, 1, &submit, VK_NULL_HANDLE); |
| 2512 | if (res == VK_SUCCESS) context_.record_submit_locked(progress_, ticket); | 2623 | if (res == VK_SUCCESS) { |
| 2624 | context_.record_submit_locked(progress_, ticket); | ||
| 2625 | if (signals_fence) context_.holds().signaled(fence, value); | ||
| 2626 | } | ||
| 2513 | } | 2627 | } |
| 2514 | if (res == VK_ERROR_DEVICE_LOST) { | 2628 | if (res == VK_ERROR_DEVICE_LOST) { |
| 2515 | context_.mark_device_lost(); | 2629 | context_.mark_device_lost(); |
| 2516 | waits_.clear(); | 2630 | clear_waits(); |
| 2517 | wait_values_.clear(); | ||
| 2518 | return false; | 2631 | return false; |
| 2519 | } | 2632 | } |
| 2520 | const bool ok = (res == VK_SUCCESS); | 2633 | const bool ok = (res == VK_SUCCESS); |
| 2521 | if (ok) { | 2634 | if (ok) { |
| 2522 | if (fence) context_.holds().signaled(fence, value); // held batches waiting for it: the caller pumps | ||
| 2523 | submitted_ = ticket; | 2635 | submitted_ = ticket; |
| 2524 | waits_.clear(); | 2636 | clear_waits(ticket); |
| 2525 | wait_values_.clear(); | ||
| 2526 | for (auto& slot : buffers_) | 2637 | for (auto& slot : buffers_) |
| 2527 | if (slot.buffer == command_buffer) slot.ticket = ticket; | 2638 | if (slot.buffer == command_buffer) slot.ticket = ticket; |
| 2528 | } | 2639 | } |
| @@ -2554,6 +2665,14 @@ private: | |||
| 2554 | // guards the members above that execute, signal, wait and marker touch. | 2665 | // guards the members above that execute, signal, wait and marker touch. |
| 2555 | std::mutex state_mutex_; | 2666 | std::mutex state_mutex_; |
| 2556 | std::deque<Deferred> held_; | 2667 | std::deque<Deferred> held_; |
| 2668 | std::vector<std::shared_ptr<void>> wait_pins_; // keep the fences of waits_ alive | ||
| 2669 | std::vector<std::shared_ptr<void>> retired_pins_; // released by the caller once state_mutex_ is dropped | ||
| 2670 | struct TicketedPin { | ||
| 2671 | std::uint64_t ticket; | ||
| 2672 | std::shared_ptr<void> pin; | ||
| 2673 | }; | ||
| 2674 | std::deque<TicketedPin> pin_queue_; // released once progress_ reaches the ticket | ||
| 2675 | std::atomic<int> held_failure_{0}; // a ReplayResult: the first held batch that failed | ||
| 2557 | }; | 2676 | }; |
| 2558 | 2677 | ||
| 2559 | } // namespace leonida | 2678 | } // namespace leonida |