upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src
diff options
context:
space:
mode:
authorCodex <codex@localhost>2026-11-22 00:21:48 +0100
committerCodex <codex@localhost>2026-11-22 00:21:48 +0100
commitbb65ea6c35b9e67e7d6883a647a013fbc580c708 (patch)
treee28d27caf360158aa68df38f3c33c0adf328185b /runtime/src
parent8bd0087d2d0e45fc18fa78fb40b70ecfc6648854 (diff)
runtime: require idle for bindless key replacements
Diffstat (limited to 'runtime/src')
-rw-r--r--runtime/src/descriptor_table.h15
-rw-r--r--runtime/src/vulkan_bindless.h18
2 files changed, 22 insertions, 11 deletions
diff --git a/runtime/src/descriptor_table.h b/runtime/src/descriptor_table.h
index c2e89afe5..97c82ae1b 100644
--- a/runtime/src/descriptor_table.h
+++ b/runtime/src/descriptor_table.h
@@ -66,6 +66,8 @@ static_assert(sizeof(DescriptorEntry) == 40);
66 66
67class DescriptorTable { 67class DescriptorTable {
68public: 68public:
69 enum class InsertResult { added, unchanged, replaced };
70
69 explicit DescriptorTable(std::uint32_t initial_capacity = 16) { 71 explicit DescriptorTable(std::uint32_t initial_capacity = 16) {
70 std::uint32_t cap = 16; 72 std::uint32_t cap = 16;
71 while (cap < initial_capacity) cap <<= 1; 73 while (cap < initial_capacity) cap <<= 1;
@@ -77,6 +79,12 @@ public:
77 // If the key already exists in one of the 8 probe slots, its index is updated. 79 // If the key already exists in one of the 8 probe slots, its index is updated.
78 // If insertion would exceed 8 probes or 50% load, the table grows and re-hashes. 80 // If insertion would exceed 8 probes or 50% load, the table grows and re-hashes.
79 bool insert(const std::uint32_t key[8], std::uint32_t index) { 81 bool insert(const std::uint32_t key[8], std::uint32_t index) {
82 insert_or_update(key, index);
83 return true;
84 }
85
86 // Report the change under the same lock as the insert, so callers can synchronize a replacement.
87 InsertResult insert_or_update(const std::uint32_t key[8], std::uint32_t index) {
80 std::lock_guard lock(mutex_); 88 std::lock_guard lock(mutex_);
81 return insert_locked(key, index); 89 return insert_locked(key, index);
82 } 90 }
@@ -183,7 +191,7 @@ public:
183 } 191 }
184 192
185private: 193private:
186 bool insert_locked(const std::uint32_t key[8], std::uint32_t index) { 194 InsertResult insert_locked(const std::uint32_t key[8], std::uint32_t index) {
187 while (true) { 195 while (true) {
188 const std::uint32_t h = hash_descriptor_key(key); 196 const std::uint32_t h = hash_descriptor_key(key);
189 int empty_slot = -1; 197 int empty_slot = -1;
@@ -193,8 +201,9 @@ private:
193 auto& entry = entries_[slot]; 201 auto& entry = entries_[slot];
194 if (entry.pad == kSlotOccupiedMarker) { 202 if (entry.pad == kSlotOccupiedMarker) {
195 if (std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { 203 if (std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
204 if (entry.index == index) return InsertResult::unchanged;
196 entry.index = index; 205 entry.index = index;
197 return true; 206 return InsertResult::replaced;
198 } 207 }
199 } else if (empty_slot < 0) { 208 } else if (empty_slot < 0) {
200 empty_slot = static_cast<int>(slot); 209 empty_slot = static_cast<int>(slot);
@@ -208,7 +217,7 @@ private:
208 entry.index = index; 217 entry.index = index;
209 entry.pad = kSlotOccupiedMarker; 218 entry.pad = kSlotOccupiedMarker;
210 ++occupied_count_; 219 ++occupied_count_;
211 return true; 220 return InsertResult::added;
212 } 221 }
213 222
214 // Table is full (>50% load) or no slot found in 8 probes: grow and retry 223 // Table is full (>50% load) or no slot found in 8 probes: grow and retry
diff --git a/runtime/src/vulkan_bindless.h b/runtime/src/vulkan_bindless.h
index 8cb895adb..fc7682e2f 100644
--- a/runtime/src/vulkan_bindless.h
+++ b/runtime/src/vulkan_bindless.h
@@ -211,22 +211,24 @@ public:
211 // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders 211 // T# and S# keys to the indices above: the table of binding 2 (descriptor_table.h). Changes reach the shaders
212 // at flush(). 212 // at flush().
213 bool insert_key(const DescriptorKey& key, std::uint32_t index) { 213 bool insert_key(const DescriptorKey& key, std::uint32_t index) {
214 table_dirty_ = true; 214 const auto result = table_.insert_or_update(key.data(), index);
215 return table_.insert(key, index); 215 table_dirty_ |= result != DescriptorTable::InsertResult::unchanged;
216 existing_entries_changed_ |= result == DescriptorTable::InsertResult::replaced;
217 return true;
216 } 218 }
217 // Erasing compacts the key's probe cluster, so other keys' entries may change slots. 219 // Erasing compacts the key's probe cluster, so other keys' entries may change slots.
218 bool erase_key(const DescriptorKey& key) { 220 bool erase_key(const DescriptorKey& key) {
219 if (!table_.erase(key)) return false; 221 if (!table_.erase(key)) return false;
220 table_dirty_ = entries_moved_ = true; 222 table_dirty_ = existing_entries_changed_ = true;
221 return true; 223 return true;
222 } 224 }
223 const DescriptorTable& table() const { return table_; } 225 const DescriptorTable& table() const { return table_; }
224 226
225 // True when the next flush() will move entries a running shader may be probing, or replace the table's 227 // True when the next flush() will change entries a running shader may be probing, or replace the table's
226 // buffer: the caller then flushes only once no command buffer that uses the set is still executing. Without 228 // buffer: the caller then flushes only once no command buffer that uses the set is still executing. Without
227 // it the flush only adds entries to the buffer in place, which a running lookup cannot see half done. 229 // it the flush only adds entries to the buffer in place, which a running lookup cannot see half done.
228 bool flush_needs_idle() const { 230 bool flush_needs_idle() const {
229 return table_dirty_ && (entries_moved_ || table_.size_bytes() > table_buffer_.size); 231 return table_dirty_ && (existing_entries_changed_ || table_.size_bytes() > table_buffer_.size);
230 } 232 }
231 233
232 // Writes the pending descriptor changes and the table: before the commands that use the set are executed. 234 // Writes the pending descriptor changes and the table: before the commands that use the set are executed.
@@ -250,7 +252,7 @@ public:
250 } 252 }
251 if (written) { 253 if (written) {
252 std::memcpy(table_buffer_.mapped, words.data(), bytes); 254 std::memcpy(table_buffer_.mapped, words.data(), bytes);
253 table_dirty_ = entries_moved_ = false; 255 table_dirty_ = existing_entries_changed_ = false;
254 } 256 }
255 } 257 }
256 if (!pending_.empty()) { 258 if (!pending_.empty()) {
@@ -468,7 +470,7 @@ private:
468 const VkDeviceSize bytes = words.size() * sizeof(std::uint32_t); 470 const VkDeviceSize bytes = words.size() * sizeof(std::uint32_t);
469 if (!create(table_buffer_, bytes)) return false; 471 if (!create(table_buffer_, bytes)) return false;
470 std::memcpy(table_buffer_.mapped, words.data(), bytes); 472 std::memcpy(table_buffer_.mapped, words.data(), bytes);
471 table_dirty_ = entries_moved_ = false; 473 table_dirty_ = existing_entries_changed_ = false;
472 return true; 474 return true;
473 } 475 }
474 476
@@ -572,7 +574,7 @@ private:
572 DescriptorTable table_{64}; 574 DescriptorTable table_{64};
573 TableBuffer table_buffer_; 575 TableBuffer table_buffer_;
574 bool table_dirty_ = false; // the table differs from the buffer 576 bool table_dirty_ = false; // the table differs from the buffer
575 bool entries_moved_ = false; // ... and not only by entries added 577 bool existing_entries_changed_ = false; // ... and not only by entries added
576 std::vector<TableBuffer> retired_; 578 std::vector<TableBuffer> retired_;
577}; 579};
578 580