upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/queue_holds.h
diff options
context:
space:
mode:
authorSuarezPM <SuarezPM@users.noreply.github.com>2026-11-22 19:04:22 -0300
committerSuarezPM <SuarezPM@users.noreply.github.com>2026-11-22 19:04:22 -0300
commit73bee00a64b1cd70ee12865b17bf62172dcf1111 (patch)
tree03a2b2b49ca8dbdf9aa38be1cdb6ad7a3c4aacaf /runtime/src/queue_holds.h
parent557a25ed3e06bf9b399ddebcea6abbd72f15e54e (diff)
parent4c52ce27a6cf4af2bbf07a343ede39c5f1572b8d (diff)
Merge upstream/main into rt-shader-real-bvh
Keep both heap loaders: main's VKC_HEAP_INIT (VA 0) and this branch's VKC_HEAP_FILE (offset, auto-sized).
Diffstat (limited to 'runtime/src/queue_holds.h')
-rw-r--r--runtime/src/queue_holds.h143
1 files changed, 143 insertions, 0 deletions
diff --git a/runtime/src/queue_holds.h b/runtime/src/queue_holds.h
new file mode 100644
index 000000000..eafe3ebc8
--- /dev/null
+++ b/runtime/src/queue_holds.h
@@ -0,0 +1,143 @@
1#pragma once
2
3#include <atomic>
4#include <cstdint>
5#include <cstdio>
6#include <mutex>
7#include <unordered_map>
8#include <vector>
9
10namespace leonida {
11
12// Cross-queue Wait-before-Signal (docs section 4.4, "Wait before Signal").
13//
14// 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// lists would be replayed against the layouts from before A's work. So a queue holds a batch (and everything it
17// is asked to do after it) while any fence it waits on has no submitted Signal of at least that value, and
18// releases it, in order, once one has been submitted: the layouts then follow the dependency order, while Vulkan's
19// own timeline semaphores still order the GPU work.
20//
21// 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// became ready. It owns no Vulkan objects. Semaphores it was never told about count as satisfied, so a queue used
24// with a plain VkSemaphore behaves as before.
25//
26// Threads. Nothing here blocks on the GPU. The thread that makes a wait satisfiable (a queue's Signal, a CPU
27// Signal) releases the ready batches itself, after it has released its own queue's lock: recording a held batch
28// costs what its own ExecuteCommandLists would have. Only one thread releases at a time; a thread that finds
29// another one releasing leaves a note and that one takes another pass, so no caller waits for another's recording.
30template<class Semaphore>
31class QueueHolds {
32public:
33 // A queue that can hold batches. Both calls take the queue's own lock; neither is called with it held.
34 struct Holder {
35 // Releases the held operations at the front of the queue whose waits are satisfied. True if any was.
36 virtual bool release_ready() = 0;
37 // Drops the held operations that wait on `semaphore` (its fence is gone), reporting each.
38 virtual void discard_waiting_on(Semaphore semaphore) = 0;
39
40 protected:
41 ~Holder() = default;
42 };
43
44 void add_fence(Semaphore semaphore, std::uint64_t initial) {
45 std::lock_guard lock(mutex_);
46 fences_[semaphore] = initial;
47 }
48
49 // The fence is released: held batches waiting on it can never run, so they are dropped and reported.
50 void remove_fence(Semaphore semaphore) {
51 {
52 std::lock_guard lock(mutex_);
53 fences_.erase(semaphore);
54 }
55 if (held_.load(std::memory_order_acquire) == 0) return;
56 {
57 std::lock_guard pump(pump_mutex_);
58 for (Holder* holder : snapshot()) holder->discard_waiting_on(semaphore);
59 }
60 pump_ready();
61 }
62
63 // A Signal of `value` was submitted to the GPU (or the CPU set the fence). Call after the submission, with no
64 // queue lock held; then pump_ready() to release what it satisfied.
65 void signaled(Semaphore semaphore, std::uint64_t value) {
66 std::lock_guard lock(mutex_);
67 const auto it = fences_.find(semaphore);
68 if (it != fences_.end() && it->second < value) it->second = value;
69 }
70
71 // true when no submitted Signal reaches `value` yet. Unknown semaphores are satisfied.
72 bool pending(Semaphore semaphore, std::uint64_t value) const {
73 std::lock_guard lock(mutex_);
74 const auto it = fences_.find(semaphore);
75 return it != fences_.end() && it->second < value;
76 }
77
78 void attach(Holder* holder) {
79 std::lock_guard lock(mutex_);
80 holders_.push_back(holder);
81 }
82
83 // Returns once no thread is using `holder`.
84 void detach(Holder* holder) {
85 {
86 std::lock_guard pump(pump_mutex_);
87 std::lock_guard lock(mutex_);
88 for (auto it = holders_.begin(); it != holders_.end(); ++it)
89 if (*it == holder) {
90 holders_.erase(it);
91 break;
92 }
93 }
94 pump_ready();
95 }
96
97 // 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); }
99 bool any_held() const { return held_.load(std::memory_order_acquire) > 0; }
100
101 // 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.
103 void pump_ready() {
104 if (!any_held()) return;
105 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
107 for (;;) {
108 std::unique_lock pump(pump_mutex_, std::try_to_lock);
109 if (!pump) return; // another thread is releasing and will see dirty_
110 in_pump() = true;
111 while (dirty_.exchange(false, std::memory_order_acq_rel)) {
112 bool progress = true;
113 while (progress) {
114 progress = false;
115 for (Holder* holder : snapshot()) progress |= holder->release_ready();
116 }
117 }
118 in_pump() = false;
119 pump.unlock();
120 if (!dirty_.load(std::memory_order_acquire)) return;
121 }
122 }
123
124private:
125 static bool& in_pump() {
126 static thread_local bool pumping = false;
127 return pumping;
128 }
129
130 std::vector<Holder*> snapshot() const {
131 std::lock_guard lock(mutex_);
132 return holders_;
133 }
134
135 mutable std::mutex mutex_; // leaf: fences_ and holders_
136 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_;
138 std::vector<Holder*> holders_;
139 std::atomic<std::int64_t> held_{0};
140 std::atomic<bool> dirty_{false};
141};
142
143} // namespace leonida