upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/src/descriptor_table.h
diff options
context:
space:
mode:
authorTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
committerTauwni <102470988+Tauwni@users.noreply.github.com>2026-11-26 12:19:12 +0100
commitbeffaa9a476ca1b3dfd9cfa55e39eafbb1f5eec6 (patch)
tree32877805bf4eb46d9a89d5753db6b5a2b06dc845 /runtime/src/descriptor_table.h
parentf29ca7bacae9507f83e8c56abdb604bd4efb4cf1 (diff)
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
Merge main into fix/null-descriptor-generated-dir
Diffstat (limited to 'runtime/src/descriptor_table.h')
-rw-r--r--runtime/src/descriptor_table.h81
1 files changed, 52 insertions, 29 deletions
diff --git a/runtime/src/descriptor_table.h b/runtime/src/descriptor_table.h
index a0d80968a..82fa19dbf 100644
--- a/runtime/src/descriptor_table.h
+++ b/runtime/src/descriptor_table.h
@@ -19,7 +19,21 @@ namespace leonida {
19inline constexpr std::uint32_t kFnvOffsetBasis = 2166136261u; 19inline constexpr std::uint32_t kFnvOffsetBasis = 2166136261u;
20inline constexpr std::uint32_t kFnvPrime = 16777619u; 20inline constexpr std::uint32_t kFnvPrime = 16777619u;
21inline constexpr std::uint32_t kDescriptorProbes = 8; 21inline constexpr std::uint32_t kDescriptorProbes = 8;
22inline constexpr std::uint32_t kSlotOccupiedMarker = 0xA11CA7EDu; 22// An entry's last dword is 0 while the slot is empty. This bit marks it occupied, and the low bits (kKindMask) say
23// what the index is good for: one bit per array the image is bound in, sampled and storage.
24inline constexpr std::uint32_t kSlotOccupiedMarker = 0x80000000u;
25inline constexpr std::uint32_t kKindMask = 0x1FFFu; // bits 0..6 sampled, 8..12 storage
26
27// What an index is bound in, for the library whose modules read it (`descriptor_table` tagged in their index.json
28// meta; tools/rdna2spirv.py reads the same bits with OpBitFieldUExtract). Bit `dimension` is that dimension's
29// sampled array (dimensions 0..6, ImageDimension in vulkan_bindless.h); bit 8 + dimension its storage arrays
30// (dimensions 0..4 have them). A table for a plain library leaves every kind bit zero.
31inline constexpr std::uint32_t kind_sampled_bit(std::uint32_t dimension) {
32 return dimension < 7 ? (1u << dimension) : 0u;
33}
34inline constexpr std::uint32_t kind_storage_bit(std::uint32_t dimension) {
35 return dimension < 5 ? (1u << (8 + dimension)) : 0u;
36}
23 37
24// Key types: 38// Key types:
25// T# key is 8 dwords. 39// T# key is 8 dwords.
@@ -93,14 +107,20 @@ inline DescriptorKey make_ssharp_key(const std::uint32_t ssharp[4]) {
93 return k; 107 return k;
94} 108}
95 109
96// Memory layout of each entry in the table: 10 uint32s = 40 bytes. 110// Memory layout of each entry in the table: 10 uint32s = 40 bytes. The last word is the kind a translated shader
111// reads next to the index, and its occupied bit; a module that does not read it never looks at it.
97struct DescriptorEntry { 112struct DescriptorEntry {
98 std::uint32_t key[8] = {}; 113 std::uint32_t key[8] = {};
99 std::uint32_t index = 0; 114 std::uint32_t index = 0;
100 std::uint32_t pad = 0; // 0 when empty, kSlotOccupiedMarker when occupied 115 std::uint32_t kind = 0; // 0 when empty, kSlotOccupiedMarker | (kKindMask bits) when occupied
101}; 116};
102static_assert(sizeof(DescriptorEntry) == 40); 117static_assert(sizeof(DescriptorEntry) == 40);
103 118
119inline constexpr bool slot_occupied(const DescriptorEntry& entry) {
120 return (entry.kind & kSlotOccupiedMarker) != 0;
121}
122inline constexpr std::uint32_t slot_kind(const DescriptorEntry& entry) { return entry.kind & kKindMask; }
123
104class DescriptorTable { 124class DescriptorTable {
105public: 125public:
106 enum class InsertResult { added, unchanged, replaced, added_with_relocation }; 126 enum class InsertResult { added, unchanged, replaced, added_with_relocation };
@@ -122,22 +142,23 @@ public:
122 // The kind this table places and finds keys with (immutable after construction). 142 // The kind this table places and finds keys with (immutable after construction).
123 DescriptorHashKind hash_kind() const { return hash_kind_; } 143 DescriptorHashKind hash_kind() const { return hash_kind_; }
124 144
125 // Inserts or updates a key -> index mapping. 145 // Inserts or updates a key -> (index, kind) mapping. If the key already exists in one of the 8 probe slots,
126 // If the key already exists in one of the 8 probe slots, its index is updated. 146 // its entry is updated. Try one bounded relocation before growing. Load never exceeds 50%. `kind` is what a
127 // Try one bounded relocation before growing. Load never exceeds 50%. 147 // shader that reads it (a `descriptor_table` tagged module) uses to tell which array the index is bound in; a
128 bool insert(const std::uint32_t key[8], std::uint32_t index) { 148 // sampler's entry has none.
129 insert_or_update(key, index); 149 bool insert(const std::uint32_t key[8], std::uint32_t index, std::uint32_t kind = 0) {
150 insert_or_update(key, index, kind);
130 return true; 151 return true;
131 } 152 }
132 153
133 // Report the change under the same lock so the GPU caller can synchronize writes to live slots. 154 // Report the change under the same lock so the GPU caller can synchronize writes to live slots.
134 InsertResult insert_or_update(const std::uint32_t key[8], std::uint32_t index) { 155 InsertResult insert_or_update(const std::uint32_t key[8], std::uint32_t index, std::uint32_t kind = 0) {
135 std::lock_guard lock(mutex_); 156 std::lock_guard lock(mutex_);
136 return insert_locked(key, index); 157 return insert_locked(key, index, kind);
137 } 158 }
138 159
139 bool insert(const DescriptorKey& key, std::uint32_t index) { 160 bool insert(const DescriptorKey& key, std::uint32_t index, std::uint32_t kind = 0) {
140 return insert(key.data(), index); 161 return insert(key.data(), index, kind);
141 } 162 }
142 163
143 // Queries the index associated with key. Returns 0 if not found. 164 // Queries the index associated with key. Returns 0 if not found.
@@ -147,7 +168,7 @@ public:
147 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { 168 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
148 const std::uint32_t slot = (h + k) & mask_; 169 const std::uint32_t slot = (h + k) & mask_;
149 const auto& entry = entries_[slot]; 170 const auto& entry = entries_[slot];
150 if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { 171 if (slot_occupied(entry) && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
151 return entry.index; 172 return entry.index;
152 } 173 }
153 } 174 }
@@ -164,7 +185,7 @@ public:
164 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { 185 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
165 const std::uint32_t slot = (h + k) & mask_; 186 const std::uint32_t slot = (h + k) & mask_;
166 const auto& entry = entries_[slot]; 187 const auto& entry = entries_[slot];
167 if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { 188 if (slot_occupied(entry) && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
168 return true; 189 return true;
169 } 190 }
170 } 191 }
@@ -222,8 +243,8 @@ public:
222 } 243 }
223 244
224 // Exports the table buffer format consumed by SPIR-V set 0, binding 2. 245 // Exports the table buffer format consumed by SPIR-V set 0, binding 2.
225 // Word 0 is mask; followed by entries[mask + 1] of 10 dwords: key[8], index, pad. 246 // Word 0 is mask; followed by entries[mask + 1] of 10 dwords: key[8], index, kind.
226 std::vector<std::uint32_t> build_gpu_buffer(bool clear_pad = false) const { 247 std::vector<std::uint32_t> build_gpu_buffer(bool clear_occupied = false) const {
227 std::lock_guard lock(mutex_); 248 std::lock_guard lock(mutex_);
228 std::vector<std::uint32_t> out(1 + entries_.size() * 10, 0); 249 std::vector<std::uint32_t> out(1 + entries_.size() * 10, 0);
229 out[0] = mask_; 250 out[0] = mask_;
@@ -231,7 +252,7 @@ public:
231 const std::size_t base = 1 + s * 10; 252 const std::size_t base = 1 + s * 10;
232 std::memcpy(&out[base], entries_[s].key, sizeof(entries_[s].key)); 253 std::memcpy(&out[base], entries_[s].key, sizeof(entries_[s].key));
233 out[base + 8] = entries_[s].index; 254 out[base + 8] = entries_[s].index;
234 out[base + 9] = clear_pad ? 0 : entries_[s].pad; 255 out[base + 9] = clear_occupied ? slot_kind(entries_[s]) : entries_[s].kind;
235 } 256 }
236 return out; 257 return out;
237 } 258 }
@@ -252,7 +273,7 @@ public:
252 return changes; 273 return changes;
253 } 274 }
254 275
255 // One slot's 10 dwords (key[8], index, pad) to `dest`; false when the slot is out of range. 276 // One slot's 10 dwords (key[8], index, kind) to `dest`; false when the slot is out of range.
256 bool read_slot(std::uint32_t slot, std::uint32_t* dest) const { return read_slots(slot, 1, dest); } 277 bool read_slot(std::uint32_t slot, std::uint32_t* dest) const { return read_slots(slot, 1, dest); }
257 278
258 // `count` consecutive slots' 10 dwords each to `dest`, under one lock; false when any is out of range. 279 // `count` consecutive slots' 10 dwords each to `dest`, under one lock; false when any is out of range.
@@ -264,13 +285,13 @@ public:
264 const DescriptorEntry& entry = entries_[first + i]; 285 const DescriptorEntry& entry = entries_[first + i];
265 std::memcpy(dest + i * 10, entry.key, sizeof(entry.key)); 286 std::memcpy(dest + i * 10, entry.key, sizeof(entry.key));
266 dest[i * 10 + 8] = entry.index; 287 dest[i * 10 + 8] = entry.index;
267 dest[i * 10 + 9] = entry.pad; 288 dest[i * 10 + 9] = entry.kind;
268 } 289 }
269 return true; 290 return true;
270 } 291 }
271 292
272 // Serializes directly into `dest` (must hold at least size_bytes()) without allocating a temporary vector. 293 // Serializes directly into `dest` (must hold at least size_bytes()) without allocating a temporary vector.
273 void write_to_buffer(void* dest, bool clear_pad = false) const { 294 void write_to_buffer(void* dest, bool clear_occupied = false) const {
274 if (!dest) return; 295 if (!dest) return;
275 std::lock_guard lock(mutex_); 296 std::lock_guard lock(mutex_);
276 auto* out = static_cast<std::uint8_t*>(dest); 297 auto* out = static_cast<std::uint8_t*>(dest);
@@ -280,7 +301,7 @@ public:
280 std::uint32_t slot[10]; 301 std::uint32_t slot[10];
281 std::memcpy(slot, entry.key, sizeof(entry.key)); 302 std::memcpy(slot, entry.key, sizeof(entry.key));
282 slot[8] = entry.index; 303 slot[8] = entry.index;
283 slot[9] = clear_pad ? 0 : entry.pad; 304 slot[9] = clear_occupied ? slot_kind(entry) : entry.kind;
284 std::memcpy(out, slot, sizeof(slot)); 305 std::memcpy(out, slot, sizeof(slot));
285 out += sizeof(slot); 306 out += sizeof(slot);
286 } 307 }
@@ -298,7 +319,7 @@ private:
298 const auto mask = static_cast<std::uint32_t>(entries.size() - 1); 319 const auto mask = static_cast<std::uint32_t>(entries.size() - 1);
299 for (auto offset = kDescriptorProbes; offset < 2 * kDescriptorProbes - 1; ++offset) { 320 for (auto offset = kDescriptorProbes; offset < 2 * kDescriptorProbes - 1; ++offset) {
300 const auto hole = (hash + offset) & mask; 321 const auto hole = (hash + offset) & mask;
301 if (entries[hole].pad == kSlotOccupiedMarker) continue; 322 if (slot_occupied(entries[hole])) continue;
302 for (auto k = kDescriptorProbes; k > 0; --k) { 323 for (auto k = kDescriptorProbes; k > 0; --k) {
303 const auto slot = (hash + k - 1) & mask; 324 const auto slot = (hash + k - 1) & mask;
304 const auto home = hash_descriptor_key(entries[slot].key, kind); 325 const auto home = hash_descriptor_key(entries[slot].key, kind);
@@ -313,7 +334,7 @@ private:
313 return -1; 334 return -1;
314 } 335 }
315 336
316 InsertResult insert_locked(const std::uint32_t key[8], std::uint32_t index) { 337 InsertResult insert_locked(const std::uint32_t key[8], std::uint32_t index, std::uint32_t kind) {
317 while (true) { 338 while (true) {
318 const std::uint32_t h = hash_descriptor_key(key, hash_kind_); 339 const std::uint32_t h = hash_descriptor_key(key, hash_kind_);
319 int empty_slot = -1; 340 int empty_slot = -1;
@@ -321,10 +342,12 @@ private:
321 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { 342 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
322 const std::uint32_t slot = (h + k) & mask_; 343 const std::uint32_t slot = (h + k) & mask_;
323 auto& entry = entries_[slot]; 344 auto& entry = entries_[slot];
324 if (entry.pad == kSlotOccupiedMarker) { 345 if (slot_occupied(entry)) {
325 if (std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { 346 if (std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
326 if (entry.index == index) return InsertResult::unchanged; 347 if (entry.index == index && slot_kind(entry) == kind) return InsertResult::unchanged;
348 // A new index or a new kind: either way the slot's copy of the entry must reach the GPU.
327 entry.index = index; 349 entry.index = index;
350 entry.kind = kSlotOccupiedMarker | (kind & kKindMask);
328 mark(slot); 351 mark(slot);
329 return InsertResult::replaced; 352 return InsertResult::replaced;
330 } 353 }
@@ -356,7 +379,7 @@ private:
356 auto& entry = entries_[empty_slot]; 379 auto& entry = entries_[empty_slot];
357 std::memcpy(entry.key, key, sizeof(entry.key)); 380 std::memcpy(entry.key, key, sizeof(entry.key));
358 entry.index = index; 381 entry.index = index;
359 entry.pad = kSlotOccupiedMarker; 382 entry.kind = kSlotOccupiedMarker | (kind & kKindMask);
360 ++occupied_count_; 383 ++occupied_count_;
361 if (relocate) mark(moved_to); 384 if (relocate) mark(moved_to);
362 mark(std::uint32_t(empty_slot)); 385 mark(std::uint32_t(empty_slot));
@@ -374,7 +397,7 @@ private:
374 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { 397 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
375 const std::uint32_t slot = (h + k) & mask_; 398 const std::uint32_t slot = (h + k) & mask_;
376 const auto& entry = entries_[slot]; 399 const auto& entry = entries_[slot];
377 if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) { 400 if (slot_occupied(entry) && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
378 found_slot = slot; 401 found_slot = slot;
379 found = true; 402 found = true;
380 break; 403 break;
@@ -398,12 +421,12 @@ private:
398 bool rehash_ok = true; 421 bool rehash_ok = true;
399 422
400 for (const auto& old : entries_) { 423 for (const auto& old : entries_) {
401 if (old.pad != kSlotOccupiedMarker) continue; 424 if (!slot_occupied(old)) continue;
402 const std::uint32_t h = hash_descriptor_key(old.key, hash_kind_); 425 const std::uint32_t h = hash_descriptor_key(old.key, hash_kind_);
403 int slot = -1; 426 int slot = -1;
404 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) { 427 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
405 const std::uint32_t s = (h + k) & new_mask; 428 const std::uint32_t s = (h + k) & new_mask;
406 if (new_entries[s].pad != kSlotOccupiedMarker) { 429 if (!slot_occupied(new_entries[s])) {
407 slot = static_cast<int>(s); 430 slot = static_cast<int>(s);
408 break; 431 break;
409 } 432 }