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:
authorDiesel <dev.diesel@proton.me>2026-11-21 22:44:07 +0100
committerDiesel <dev.diesel@proton.me>2026-11-21 22:44:07 +0100
commit5c1a1fbb6bf6f609eb4e61baa4961ce382743a4e (patch)
treeac8ad90058b97ab241fdc65edae3d4628d20ee1e /runtime/src/descriptor_table.h
parentc45309e29f21c1d5b75678db0e2dc2dd11cff25b (diff)
parent4e390e0871d7265e629b51aca8ff99a15a6a6845 (diff)
Merge main to include the compute Y/Z harness test fix
Diffstat (limited to 'runtime/src/descriptor_table.h')
-rw-r--r--runtime/src/descriptor_table.h252
1 files changed, 252 insertions, 0 deletions
diff --git a/runtime/src/descriptor_table.h b/runtime/src/descriptor_table.h
new file mode 100644
index 000000000..2fc9e99cb
--- /dev/null
+++ b/runtime/src/descriptor_table.h
@@ -0,0 +1,252 @@
1#pragma once
2
3// The descriptor hash table for set 0, binding 2 (docs/d3d12_vulkan.md section 5.1 and docs/design.md):
4// Shaders look up bindless resource/sampler indices by FNV-1a hashing the 8-dword descriptor key (T# or padded S#)
5// and probing up to 8 slots in this table. Miss or fallback yields index 0.
6
7#include <algorithm>
8#include <array>
9#include <cstdint>
10#include <cstring>
11#include <mutex>
12#include <span>
13#include <vector>
14
15namespace leonida {
16
17// FNV-1a constants for 32-bit dwords
18inline constexpr std::uint32_t kFnvOffsetBasis = 2166136261u;
19inline constexpr std::uint32_t kFnvPrime = 16777619u;
20inline constexpr std::uint32_t kDescriptorProbes = 8;
21inline constexpr std::uint32_t kSlotOccupiedMarker = 0xA11CA7EDu;
22
23// Key types:
24// T# key is 8 dwords.
25// S# key is 4 dwords followed by 4x 0xFFFFFFFF.
26using DescriptorKey = std::array<std::uint32_t, 8>;
27
28inline std::uint32_t hash_descriptor_key(const std::uint32_t key[8]) {
29 std::uint32_t h = kFnvOffsetBasis;
30 for (int i = 0; i < 8; ++i) {
31 h = (h ^ key[i]) * kFnvPrime;
32 }
33 return h;
34}
35
36inline std::uint32_t hash_descriptor_key(const DescriptorKey& key) {
37 return hash_descriptor_key(key.data());
38}
39
40inline DescriptorKey make_tsharp_key(const std::uint32_t tsharp[8]) {
41 DescriptorKey k{};
42 std::memcpy(k.data(), tsharp, sizeof(DescriptorKey));
43 return k;
44}
45
46inline DescriptorKey make_ssharp_key(const std::uint32_t ssharp[4]) {
47 DescriptorKey k{};
48 k[0] = ssharp[0];
49 k[1] = ssharp[1];
50 k[2] = ssharp[2];
51 k[3] = ssharp[3];
52 k[4] = 0xFFFFFFFFu;
53 k[5] = 0xFFFFFFFFu;
54 k[6] = 0xFFFFFFFFu;
55 k[7] = 0xFFFFFFFFu;
56 return k;
57}
58
59// Memory layout of each entry in the table: 10 uint32s = 40 bytes.
60struct DescriptorEntry {
61 std::uint32_t key[8] = {};
62 std::uint32_t index = 0;
63 std::uint32_t pad = 0; // 0 when empty, kSlotOccupiedMarker when occupied
64};
65static_assert(sizeof(DescriptorEntry) == 40);
66
67class DescriptorTable {
68public:
69 explicit DescriptorTable(std::uint32_t initial_capacity = 16) {
70 std::uint32_t cap = 16;
71 while (cap < initial_capacity) cap <<= 1;
72 mask_ = cap - 1;
73 entries_.resize(cap);
74 }
75
76 // Inserts or updates a key -> index mapping.
77 // 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.
79 bool insert(const std::uint32_t key[8], std::uint32_t index) {
80 std::lock_guard lock(mutex_);
81 return insert_locked(key, index);
82 }
83
84 bool insert(const DescriptorKey& key, std::uint32_t index) {
85 return insert(key.data(), index);
86 }
87
88 // Queries the index associated with key. Returns 0 if not found.
89 std::uint32_t lookup(const std::uint32_t key[8]) const {
90 std::lock_guard lock(mutex_);
91 const std::uint32_t h = hash_descriptor_key(key);
92 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
93 const std::uint32_t slot = (h + k) & mask_;
94 const auto& entry = entries_[slot];
95 if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
96 return entry.index;
97 }
98 if (entry.pad != kSlotOccupiedMarker) {
99 return 0; // Empty slot encountered: key is definitely absent
100 }
101 }
102 return 0; // Exceeded 8 probes without a match
103 }
104
105 std::uint32_t lookup(const DescriptorKey& key) const {
106 return lookup(key.data());
107 }
108
109 bool contains(const std::uint32_t key[8]) const {
110 std::lock_guard lock(mutex_);
111 const std::uint32_t h = hash_descriptor_key(key);
112 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
113 const std::uint32_t slot = (h + k) & mask_;
114 const auto& entry = entries_[slot];
115 if (entry.pad == kSlotOccupiedMarker && std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
116 return true;
117 }
118 if (entry.pad != kSlotOccupiedMarker) return false;
119 }
120 return false;
121 }
122
123 bool contains(const DescriptorKey& key) const {
124 return contains(key.data());
125 }
126
127 void clear() {
128 std::lock_guard lock(mutex_);
129 std::fill(entries_.begin(), entries_.end(), DescriptorEntry{});
130 occupied_count_ = 0;
131 }
132
133 std::size_t size() const {
134 std::lock_guard lock(mutex_);
135 return occupied_count_;
136 }
137
138 std::size_t capacity() const {
139 std::lock_guard lock(mutex_);
140 return entries_.size();
141 }
142
143 std::uint32_t mask() const {
144 std::lock_guard lock(mutex_);
145 return mask_;
146 }
147
148 std::size_t size_dwords() const {
149 std::lock_guard lock(mutex_);
150 return 1 + entries_.size() * 10;
151 }
152
153 std::size_t size_bytes() const {
154 return size_dwords() * sizeof(std::uint32_t);
155 }
156
157 // Exports the table buffer format consumed by SPIR-V set 0, binding 2.
158 // Word 0 is mask; followed by entries[mask + 1] of 10 dwords: key[8], index, pad.
159 std::vector<std::uint32_t> build_gpu_buffer(bool clear_pad = false) const {
160 std::lock_guard lock(mutex_);
161 std::vector<std::uint32_t> out(1 + entries_.size() * 10, 0);
162 out[0] = mask_;
163 for (std::size_t s = 0; s < entries_.size(); ++s) {
164 const std::size_t base = 1 + s * 10;
165 std::memcpy(&out[base], entries_[s].key, sizeof(entries_[s].key));
166 out[base + 8] = entries_[s].index;
167 out[base + 9] = clear_pad ? 0 : entries_[s].pad;
168 }
169 return out;
170 }
171
172 void write_to_buffer(void* dest, bool clear_pad = false) const {
173 if (!dest) return;
174 const auto buf = build_gpu_buffer(clear_pad);
175 std::memcpy(dest, buf.data(), buf.size() * sizeof(std::uint32_t));
176 }
177
178private:
179 bool insert_locked(const std::uint32_t key[8], std::uint32_t index) {
180 while (true) {
181 const std::uint32_t h = hash_descriptor_key(key);
182 int empty_slot = -1;
183
184 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
185 const std::uint32_t slot = (h + k) & mask_;
186 auto& entry = entries_[slot];
187 if (entry.pad == kSlotOccupiedMarker) {
188 if (std::memcmp(entry.key, key, sizeof(entry.key)) == 0) {
189 entry.index = index;
190 return true;
191 }
192 } else if (empty_slot < 0) {
193 empty_slot = static_cast<int>(slot);
194 }
195 }
196
197 // Check if we can place in an empty slot without exceeding 50% load
198 if (empty_slot >= 0 && (occupied_count_ + 1) * 2 <= entries_.size()) {
199 auto& entry = entries_[empty_slot];
200 std::memcpy(entry.key, key, sizeof(entry.key));
201 entry.index = index;
202 entry.pad = kSlotOccupiedMarker;
203 ++occupied_count_;
204 return true;
205 }
206
207 // Table is full (>50% load) or no slot found in 8 probes: grow and retry
208 grow_locked();
209 }
210 }
211
212 void grow_locked() {
213 std::uint32_t new_cap = static_cast<std::uint32_t>(entries_.size() * 2);
214 while (true) {
215 std::vector<DescriptorEntry> new_entries(new_cap);
216 const std::uint32_t new_mask = new_cap - 1;
217 bool rehash_ok = true;
218
219 for (const auto& old : entries_) {
220 if (old.pad != kSlotOccupiedMarker) continue;
221 const std::uint32_t h = hash_descriptor_key(old.key);
222 bool placed = false;
223 for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
224 const std::uint32_t s = (h + k) & new_mask;
225 if (new_entries[s].pad != kSlotOccupiedMarker) {
226 new_entries[s] = old;
227 placed = true;
228 break;
229 }
230 }
231 if (!placed) {
232 rehash_ok = false;
233 break;
234 }
235 }
236
237 if (rehash_ok) {
238 entries_ = std::move(new_entries);
239 mask_ = new_mask;
240 break;
241 }
242 new_cap *= 2;
243 }
244 }
245
246 mutable std::mutex mutex_;
247 std::uint32_t mask_ = 15;
248 std::vector<DescriptorEntry> entries_;
249 std::size_t occupied_count_ = 0;
250};
251
252} // namespace leonida