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_backend.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_backend.h')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 199 |
1 files changed, 153 insertions, 46 deletions
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 |