upleb.uk

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

summaryrefslogtreecommitdiff
path: root/runtime/test/vk_console_heap_gpu_test.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/test/vk_console_heap_gpu_test.cpp')
-rw-r--r--runtime/test/vk_console_heap_gpu_test.cpp584
1 files changed, 584 insertions, 0 deletions
diff --git a/runtime/test/vk_console_heap_gpu_test.cpp b/runtime/test/vk_console_heap_gpu_test.cpp
new file mode 100644
index 000000000..9ac34f52b
--- /dev/null
+++ b/runtime/test/vk_console_heap_gpu_test.cpp
@@ -0,0 +1,584 @@
1// The console heap translation pass (src/console_heap_fix.comp, src/console_heap_gpu.h) against its CPU reference:
2// synthetic descriptor words only (no game data). Checks the key table mirror on the CPU first, then that the GPU
3// writes every slot exactly as console_heap_translate_slot does and records the same misses, through key table
4// updates, a new range table, a submission that failed (its work queued again) and a miss list that overflows.
5// Exits 77 without a Vulkan device with bufferDeviceAddress.
6#include "vk_test_device.h"
7#include "../src/console_heap_gpu.h"
8
9#include <cstdio>
10#include <cstring>
11#include <random>
12#include <array>
13#include <set>
14#include <vector>
15
16#define EXPECT(x) \
17 do { \
18 if (!(x)) { \
19 std::fprintf(stderr, "line %d: %s\n", __LINE__, #x); \
20 return 1; \
21 } \
22 } while (0)
23
24namespace {
25
26using leonida::ConsoleHeapKeys;
27using leonida::ConsoleHeapRange;
28using leonida::ConsoleHeapTables;
29
30// A buffer V# half: base, stride, records and dword 3 with TYPE bits 30-31 clear.
31void vsharp(std::uint32_t* w, std::uint64_t base, std::uint32_t stride, std::uint32_t records, std::uint32_t dword3) {
32 w[0] = std::uint32_t(base);
33 w[1] = std::uint32_t((base >> 32) & 0xffff) | (stride << 16);
34 w[2] = records;
35 w[3] = dword3 & 0x3fffffffu;
36}
37
38int keys_on_cpu() {
39 ConsoleHeapKeys keys(6); // 64 entries: probe chains overlap
40 const ConsoleHeapKeys::Key a{1, 2, 3, 4}, b{5, 6, 7, 8};
41 EXPECT(keys.find(a) < 0);
42 EXPECT(keys.put(a, 0x1000, 0x100, false));
43 EXPECT(keys.put(b, 0, 0x40, true));
44 std::uint64_t to = 0;
45 bool none = true;
46 EXPECT(keys.lookup(a, to, none) && to == 0x1000 && !none);
47 EXPECT(keys.lookup(b, to, none) && none);
48 EXPECT(keys.size() == 2);
49 // Only positive answers fall in a released shader range; the edge is exclusive.
50 EXPECT(keys.erase_overlapping(0x1100, 0x2000) == 0);
51 EXPECT(keys.erase_overlapping(0x10ff, 0x1100) == 1);
52 EXPECT(keys.find(a) < 0 && keys.find(b) >= 0);
53 // Replacing an answer keeps one entry and moves its index.
54 EXPECT(keys.put(b, 0x5000, 0x10, false));
55 EXPECT(keys.size() == 1);
56 EXPECT(keys.erase_overlapping(0x5008, 0x5009) == 1);
57 EXPECT(keys.size() == 0);
58 // Fill one probe chain: the 17th key with the same start finds no slot.
59 // V#s whose words differ only in high bits (bases megabytes apart): the hash must still spread them. Bare FNV-1a
60 // put all of these in one probe chain.
61 {
62 ConsoleHeapKeys big(20);
63 for (std::uint32_t i = 0; i < 20000; ++i) {
64 const ConsoleHeapKeys::Key k{(i & 0xfffu) << 20, ((i >> 12) << 8) | (64u << 16), 256, 0x2000};
65 EXPECT(big.put(k, 0x100000ull * i, 256, false));
66 }
67 EXPECT(big.size() == 20000);
68 }
69 std::vector<ConsoleHeapKeys::Key> same;
70 for (std::uint32_t i = 0; same.size() < ConsoleHeapKeys::kProbes + 1 && i < 1u << 20; ++i) {
71 const ConsoleHeapKeys::Key k{i, 0, 1, 0};
72 if ((ConsoleHeapKeys::hash(k) & keys.mask()) == 7) same.push_back(k);
73 }
74 EXPECT(same.size() == ConsoleHeapKeys::kProbes + 1);
75 for (std::size_t i = 0; i < ConsoleHeapKeys::kProbes; ++i) EXPECT(keys.put(same[i], 0x100 * i, 1, false));
76 EXPECT(!keys.put(same.back(), 1, 1, false));
77 std::vector<std::uint32_t> updates;
78 keys.take_updates(updates);
79 EXPECT(!updates.empty() && updates.size() % ConsoleHeapKeys::kUpdateWords == 0);
80 EXPECT(!keys.has_updates());
81 // The reference: ranges first (checked only), then keys, else a miss.
82 ConsoleHeapTables ranges;
83 ranges.ranges = {{0x10000, 0x20000, 0x400000, true}, {0x30000, 0x40000, 0x500000, false}};
84 ConsoleHeapKeys k2(8);
85 std::uint32_t w[8] = {};
86 vsharp(w, 0x10100, 16, 4, 0);
87 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 0);
88 EXPECT(w[0] == 0x400100 && (w[1] & 0xffff) == 0 && w[2] == 4);
89 vsharp(w, 0x1ff00, 16, 32, 0); // 512 bytes past a range with 256 left: no records
90 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 0 && !w[0] && !w[1] && !w[2]);
91 vsharp(w, 0x30000, 0, 64, 0); // an unchecked range: the key table, then a miss
92 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 1 && !w[2]);
93 // Backed spans leave a base as it is (before the key table); outside the ask table nothing is a miss.
94 ranges.backed = {{0x80000, 0x90000}};
95 ranges.ask = {{0x30000, 0x40000}};
96 ranges.use_ask = true;
97 vsharp(w, 0x80100, 0, 64, 0);
98 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 0 && w[0] == 0x80100 && w[2] == 64);
99 vsharp(w, 0x8ffc0, 0, 128, 0); // runs past the span: not backed, outside ask: no records, no miss
100 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 0 && !w[2]);
101 vsharp(w, 0x30000, 0, 64, 0); // in ask: a miss
102 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 1);
103 // Signed offsets: a base with bit 47 set is the negative shader VA it names.
104 ranges.signed_offsets = true;
105 ranges.backed = {{0xffff800000000000ull, 0xffff800000010000ull}};
106 vsharp(w, 0x800000000100ull, 0, 64, 0);
107 EXPECT(leonida::console_heap_translate_slot(w, ranges, k2) == 0 && w[2] == 64);
108 std::vector<std::pair<std::uint64_t, std::uint64_t>> spans{{10, 20}, {15, 30}, {30, 40}, {50, 60}, {5, 5}};
109 leonida::merge_console_heap_spans(spans);
110 EXPECT(spans.size() == 2 && spans[0].first == 10 && spans[0].second == 40 && spans[1].first == 50);
111 return 0;
112}
113
114struct Case {
115 ConsoleHeapTables t;
116 std::vector<ConsoleHeapRange>& ranges = t.ranges;
117 ConsoleHeapKeys keys{14};
118};
119
120// Random slot words over a mix of classes; bases drawn from the ranges and keys so every path is taken.
121void make_slots(std::mt19937& rng, std::uint32_t* words, std::size_t slots, Case& c,
122 std::vector<ConsoleHeapKeys::Key>& entered) {
123 std::uniform_int_distribution<std::uint32_t> any;
124 for (std::size_t s = 0; s < slots; ++s) {
125 std::uint32_t* w = words + 8 * s;
126 for (int i = 0; i < 8; ++i) w[i] = any(rng);
127 const std::uint32_t kind = any(rng) % 10;
128 if (kind == 0) { // T#: TYPE >= 8
129 w[3] = (w[3] & 0x0fffffffu) | 0x80000000u;
130 continue;
131 }
132 if (kind == 1) { // other words with bits 30-31 set
133 w[3] |= 0x40000000u;
134 continue;
135 }
136 if (kind == 2) {
137 std::memset(w, 0, 32);
138 continue;
139 }
140 for (int half = 0; half < 2; ++half) {
141 std::uint32_t* k = w + 4 * half;
142 const std::uint32_t pick = any(rng) % 8;
143 const std::uint32_t stride = any(rng) % 3 == 0 ? 0 : any(rng) % 64;
144 const std::uint32_t records = 1 + any(rng) % 256;
145 if (pick < 3 && !c.ranges.empty()) {
146 const auto& r = c.ranges[any(rng) % c.ranges.size()];
147 vsharp(k, r.title_va + any(rng) % (r.end - r.title_va), stride, records, any(rng));
148 } else if (pick < 5) {
149 vsharp(k, 0x200000000ull + (any(rng) % 4096) * 64, stride, records, any(rng));
150 if (pick == 3) {
151 const ConsoleHeapKeys::Key key{k[0], k[1], k[2], k[3]};
152 const bool none = any(rng) % 4 == 0;
153 if (c.keys.put(key, 0x10000000ull + (any(rng) % 65536) * 256, 4096, none)) entered.push_back(key);
154 }
155 } else if (pick == 5) {
156 vsharp(k, 0, stride, records, any(rng));
157 } else if (pick == 6) {
158 vsharp(k, 0x300000000ull + any(rng), stride, 0, any(rng)); // no records: left as it is
159 } else if (half == 1) {
160 k[3] |= 0x40000000u; // a counter half that is no V#
161 } else {
162 vsharp(k, 0x400000000ull + any(rng), stride, records, any(rng)); // nowhere: a miss
163 }
164 }
165 }
166}
167
168} // namespace
169
170int main() {
171 if (const int failed = keys_on_cpu()) return failed;
172 std::puts("console heap keys (CPU): ok");
173
174 vktest::Device d;
175 d.request_buffer_device_address = true;
176 const auto status = d.initialize();
177 if (status == vktest::DeviceStatus::unavailable || (status == vktest::DeviceStatus::ready && !d.buffer_device_address)) {
178 std::printf("vk_console_heap_gpu_test: no Vulkan device with bufferDeviceAddress, skipped\n");
179 return 77;
180 }
181 if (status != vktest::DeviceStatus::ready) return 1;
182 VkPhysicalDeviceMemoryProperties memory{};
183 d.instance_fn<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(d.physical, &memory);
184
185 leonida::ConsoleHeapGpu gpu(d.device_proc, d.device, memory, 14);
186 EXPECT(gpu.init());
187
188 constexpr std::uint32_t kSlots = 6000;
189 void* input_data = nullptr;
190 void* copy_data = nullptr;
191 const VkBuffer input = d.buffer(VkDeviceSize(kSlots) * 32, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &input_data);
192 const VkBuffer copy = d.buffer(VkDeviceSize(kSlots) * 32, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, &copy_data);
193 EXPECT(input && copy && input_data && copy_data);
194 const int heap = gpu.add_heap(d.device_address(input), d.device_address(copy), kSlots);
195 EXPECT(heap >= 0);
196 auto* in = static_cast<std::uint32_t*>(input_data);
197 auto* out = static_cast<std::uint32_t*>(copy_data);
198
199 std::mt19937 rng(1234);
200 Case c;
201 c.ranges = {{0x10000, 0x30000, 0x100000, true},
202 {0x40000, 0x48000, 0x0000ffff00000000ull, true}, // bits 32-47 of the answer
203 {0x50000, 0x60000, 0x200000, false},
204 {0x100000000ull, 0x100100000ull, 0x300000, true}};
205 std::vector<ConsoleHeapKeys::Key> entered;
206 make_slots(rng, in, kSlots, c, entered);
207 // The GPU's mirror gets the same keys.
208 gpu.with_keys([&](ConsoleHeapKeys& k) {
209 for (const auto& key : entered) {
210 std::uint64_t to = 0;
211 bool none = false;
212 c.keys.lookup(key, to, none);
213 k.put(key, to, 4096, none);
214 }
215 return 0;
216 });
217 gpu.set_ranges(c.ranges);
218
219 // One round: the listed slots are written as the reference says, the others keep the marker, and the misses
220 // are the reference's.
221 auto round = [&](const std::vector<std::uint32_t>& listed, std::uint64_t ticket, bool expect_overflow) -> int {
222 std::memset(out, 0xcd, std::size_t(kSlots) * 32);
223 gpu.queue(heap, listed.begin(), listed.end());
224 bool recorded = false;
225 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
226 EXPECT(recorded);
227 gpu.commit(true, ticket);
228 std::set<std::uint32_t> misses;
229 bool overflowed = false;
230 gpu.drain(ticket, [&](int h, std::uint32_t slot, std::uint32_t half) {
231 if (h == heap) misses.insert(slot * 2 + half);
232 }, [&](int) { overflowed = true; });
233 EXPECT(gpu.in_flight() == 0);
234 EXPECT(overflowed == expect_overflow);
235 std::set<std::uint32_t> expected_misses;
236 std::vector<std::uint8_t> is_listed(kSlots, 0);
237 for (const std::uint32_t s : listed) is_listed[s] = 1;
238 for (std::uint32_t s = 0; s < kSlots; ++s) {
239 std::uint32_t w[8];
240 std::memcpy(w, in + 8 * s, 32);
241 if (!is_listed[s]) {
242 for (int i = 0; i < 8; ++i) EXPECT(out[8 * s + i] == 0xcdcdcdcdu);
243 continue;
244 }
245 const unsigned missed = leonida::console_heap_translate_slot(w, c.t, c.keys);
246 if (missed & 1) expected_misses.insert(s * 2);
247 if (missed & 2) expected_misses.insert(s * 2 + 1);
248 if (std::memcmp(w, out + 8 * s, 32)) {
249 std::fprintf(stderr, "slot %u: in %08x %08x %08x %08x %08x %08x %08x %08x\n", s, in[8 * s],
250 in[8 * s + 1], in[8 * s + 2], in[8 * s + 3], in[8 * s + 4], in[8 * s + 5], in[8 * s + 6],
251 in[8 * s + 7]);
252 std::fprintf(stderr, " gpu %08x %08x %08x %08x %08x %08x %08x %08x\n", out[8 * s], out[8 * s + 1],
253 out[8 * s + 2], out[8 * s + 3], out[8 * s + 4], out[8 * s + 5], out[8 * s + 6],
254 out[8 * s + 7]);
255 std::fprintf(stderr, " cpu %08x %08x %08x %08x %08x %08x %08x %08x\n", w[0], w[1], w[2], w[3], w[4],
256 w[5], w[6], w[7]);
257 return 1;
258 }
259 }
260 if (!expect_overflow) EXPECT(misses == expected_misses);
261 else EXPECT(misses.size() == leonida::ConsoleHeapGpu::kMissCapacity);
262 std::printf(" %zu slots, %zu misses\n", listed.size(), misses.size());
263 return 0;
264 };
265
266 std::vector<std::uint32_t> all(kSlots);
267 for (std::uint32_t s = 0; s < kSlots; ++s) all[s] = s;
268 EXPECT(round(all, 1, false) == 0);
269 std::puts("console heap pass, every slot: ok");
270
271 // Key table updates and a new range table: erase some keys, enter answers for some misses, move a range.
272 std::vector<std::uint32_t> some;
273 for (std::uint32_t s = 0; s < kSlots; s += 3) some.push_back(s);
274 std::size_t erased = 0, added = 0;
275 for (std::size_t i = 0; i < entered.size(); i += 2) {
276 c.keys.erase(entered[i]);
277 gpu.with_keys([&](ConsoleHeapKeys& k) { return k.erase(entered[i]); });
278 ++erased;
279 }
280 for (std::uint32_t s = 0; s < kSlots && added < 200; ++s) {
281 std::uint32_t w[8];
282 std::memcpy(w, in + 8 * s, 32);
283 std::uint32_t copy_w[8];
284 std::memcpy(copy_w, w, 32);
285 const unsigned missed = leonida::console_heap_translate_slot(copy_w, c.t, c.keys);
286 if (!(missed & 1)) continue;
287 const ConsoleHeapKeys::Key key{w[0], w[1], w[2], w[3]};
288 c.keys.put(key, 0x7000000000ull + added * 64, 64, added % 5 == 0);
289 gpu.with_keys([&](ConsoleHeapKeys& k) { return k.put(key, 0x7000000000ull + added * 64, 64, added % 5 == 0); });
290 ++added;
291 }
292 c.ranges[0].shader_va = 0x900000;
293 c.ranges[2].checked = true;
294 gpu.set_ranges(c.ranges);
295 EXPECT(round(some, 2, false) == 0);
296 std::printf("console heap pass, %zu keys erased, %zu entered, ranges moved: ok\n", erased, added);
297
298 // A submission that did not reach the queue: its slots and updates come back for the next pass.
299 {
300 const ConsoleHeapKeys::Key key{in[8 * 5], in[8 * 5 + 1], in[8 * 5 + 2], in[8 * 5 + 3]};
301 gpu.queue(heap, 5);
302 bool recorded = false;
303 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
304 EXPECT(recorded && !gpu.pending());
305 gpu.commit(false, 0);
306 EXPECT(gpu.pending() && gpu.in_flight() == 0);
307 (void)key;
308 }
309 EXPECT(round({5}, 3, false) == 0);
310 // Backed and ask tables: part of the keyed bases become backed (answered before the key table), and only bases
311 // in the ask span miss.
312 c.t.backed = {{0x200000000ull, 0x200008000ull}, {0x10000ull, 0x18000ull}};
313 c.t.ask = {{0x400000000ull, 0x500000000ull}};
314 c.t.use_ask = true;
315 gpu.set_spans(leonida::ConsoleHeapGpu::backed_table, c.t.backed);
316 gpu.set_spans(leonida::ConsoleHeapGpu::ask_table, c.t.ask);
317 EXPECT(round(all, 7, false) == 0);
318 std::puts("console heap pass, backed and ask tables: ok");
319 {
320 // kZeroFirst: words that are no buffer pair and no T# get dwords 0-3 zeroed; the last queue() decides.
321 std::vector<std::uint32_t> flagged;
322 for (std::uint32_t s2 = 0; s2 < kSlots && flagged.size() < 64; ++s2)
323 if (in[8 * s2 + 3] >> 30) flagged.push_back(s2);
324 EXPECT(flagged.size() == 64);
325 std::memset(out, 0xcd, std::size_t(kSlots) * 32);
326 for (std::size_t i = 0; i < flagged.size(); ++i) {
327 gpu.queue(heap, flagged[i] | leonida::ConsoleHeapGpu::kZeroFirst);
328 if (i % 2) gpu.queue(heap, flagged[i]); // queued again without the bit: copied as it is
329 }
330 bool recorded = false;
331 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
332 gpu.commit(true, 8);
333 gpu.drain(8, [](int, std::uint32_t, std::uint32_t) {}, [](int) {});
334 EXPECT(recorded);
335 for (std::size_t i = 0; i < flagged.size(); ++i) {
336 std::uint32_t w[8];
337 std::memcpy(w, in + 8 * flagged[i], 32);
338 leonida::console_heap_translate_slot(w, c.t, c.keys, i % 2 == 0);
339 EXPECT(!std::memcmp(w, out + 8 * flagged[i], 32));
340 if (i % 2 == 0) EXPECT(!out[8 * flagged[i]] && !out[8 * flagged[i] + 3]);
341 }
342 }
343 std::puts("console heap pass, words that are no descriptor: ok");
344 {
345 // rescan(): one range dispatch rewrites the buffer pairs of [0, n) and leaves every other slot as it was.
346 constexpr std::uint32_t n = 4000;
347 std::memset(out, 0xcd, std::size_t(kSlots) * 32);
348 gpu.rescan(heap, n);
349 EXPECT(gpu.pending());
350 bool recorded = false;
351 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
352 gpu.commit(true, 9);
353 std::set<std::uint32_t> misses;
354 gpu.drain(9, [&](int h, std::uint32_t slot, std::uint32_t half) { if (h == heap) misses.insert(slot * 2 + half); },
355 [](int) {});
356 EXPECT(recorded);
357 std::set<std::uint32_t> expected;
358 std::size_t pairs = 0;
359 for (std::uint32_t s2 = 0; s2 < kSlots; ++s2) {
360 std::uint32_t w[8];
361 std::memcpy(w, in + 8 * s2, 32);
362 if (s2 >= n || (w[3] >> 30)) {
363 for (int i = 0; i < 8; ++i) EXPECT(out[8 * s2 + i] == 0xcdcdcdcdu);
364 continue;
365 }
366 ++pairs;
367 const unsigned missed = leonida::console_heap_translate_slot(w, c.t, c.keys);
368 if (missed & 1) expected.insert(s2 * 2);
369 if (missed & 2) expected.insert(s2 * 2 + 1);
370 EXPECT(!std::memcmp(w, out + 8 * s2, 32));
371 }
372 EXPECT(misses == expected && pairs > 1000);
373 EXPECT(!gpu.pending());
374 }
375 std::puts("console heap pass, range rescan: ok");
376 {
377 // rescan_filtered(): only the slots whose metadata (left by the last pass over them, the rescan above) meets a
378 // change span are translated again: a title span holding one slot's base, a shader span holding another's
379 // result. The rest keep what they hold, without the title's words being read.
380 constexpr std::uint32_t n = 4000;
381 struct Half {
382 std::uint64_t base, to, span;
383 };
384 std::vector<std::array<Half, 2>> meta(n);
385 std::vector<bool> pair(n, false);
386 for (std::uint32_t s2 = 0; s2 < n; ++s2) {
387 std::uint32_t w[8];
388 std::memcpy(w, in + 8 * s2, 32);
389 if (w[3] >> 30) continue;
390 pair[s2] = true;
391 std::uint32_t before[8];
392 std::memcpy(before, w, 32);
393 leonida::console_heap_translate_slot(w, c.t, c.keys);
394 for (int h = 0; h < 2; ++h) {
395 const std::uint32_t* b = before + 4 * h;
396 const std::uint32_t* a = w + 4 * h;
397 if (!b[2]) {
398 meta[s2][h] = {};
399 continue;
400 }
401 const std::uint64_t stride = (b[1] >> 16) & 0x3fff;
402 const std::uint64_t span = std::min<std::uint64_t>(std::uint64_t(b[2]) * std::max<std::uint64_t>(stride, 1),
403 0xffffffffull);
404 meta[s2][h] = {b[0] | (std::uint64_t(b[1] & 0xffff) << 32),
405 a[2] ? a[0] | (std::uint64_t(a[1] & 0xffff) << 32) : 0, span};
406 }
407 }
408 std::uint32_t by_title = UINT32_MAX, by_result = UINT32_MAX;
409 for (std::uint32_t s2 = 0; s2 < n; ++s2) {
410 if (!pair[s2] || !meta[s2][0].span) continue;
411 if (by_title == UINT32_MAX) by_title = s2;
412 else if (meta[s2][0].to && meta[s2][0].to != meta[by_title][0].base) {
413 by_result = s2;
414 break;
415 }
416 }
417 EXPECT(by_title != UINT32_MAX && by_result != UINT32_MAX);
418 const leonida::ConsoleHeapSpans title{{meta[by_title][0].base, meta[by_title][0].base + 1}};
419 const leonida::ConsoleHeapSpans shader{{meta[by_result][0].to, meta[by_result][0].to + 1}};
420 const auto meets = [](const leonida::ConsoleHeapSpans& spans, std::uint64_t v, std::uint64_t bytes) {
421 for (const auto& [a, b] : spans)
422 if (v < b && a < v + bytes) return true;
423 return false;
424 };
425 std::set<std::uint32_t> touched;
426 for (std::uint32_t s2 = 0; s2 < n; ++s2)
427 for (const Half& h : meta[s2]) {
428 if (!pair[s2] || !h.span) continue;
429 if (meets(title, h.base, 1) || meets(shader, h.base, h.span) || (h.to && meets(shader, h.to, h.span)))
430 touched.insert(s2);
431 }
432 EXPECT(touched.count(by_title) && touched.count(by_result) && touched.size() < 1000);
433 std::memset(out, 0xcd, std::size_t(kSlots) * 32);
434 const auto before = gpu.take_stats();
435 (void)before;
436 gpu.rescan_filtered(heap, n, shader, title);
437 EXPECT(gpu.pending());
438 bool recorded = false;
439 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
440 gpu.commit(true, 10);
441 gpu.drain(10, [](int, std::uint32_t, std::uint32_t) {}, [](int) {});
442 EXPECT(recorded && !gpu.pending());
443 for (std::uint32_t s2 = 0; s2 < kSlots; ++s2) {
444 if (!touched.count(s2)) {
445 for (int i = 0; i < 8; ++i) EXPECT(out[8 * s2 + i] == 0xcdcdcdcdu);
446 continue;
447 }
448 std::uint32_t w[8];
449 std::memcpy(w, in + 8 * s2, 32);
450 leonida::console_heap_translate_slot(w, c.t, c.keys);
451 EXPECT(!std::memcmp(w, out + 8 * s2, 32));
452 }
453 EXPECT(gpu.take_stats().filtered == n);
454 // A failed submission: the filtered rescan and its spans come back, and the next pass does them.
455 std::memset(out, 0xcd, std::size_t(kSlots) * 32);
456 gpu.rescan_filtered(heap, n, shader, title);
457 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
458 gpu.commit(false, 0);
459 EXPECT(gpu.pending());
460 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
461 gpu.commit(true, 11);
462 gpu.drain(11, [](int, std::uint32_t, std::uint32_t) {}, [](int) {});
463 std::size_t rewritten = 0;
464 for (std::uint32_t s2 = 0; s2 < n; ++s2) rewritten += out[8 * s2] != 0xcdcdcdcdu || out[8 * s2 + 3] != 0xcdcdcdcdu;
465 EXPECT(rewritten == touched.size());
466 std::printf("console heap pass, filtered rescan: ok (%zu of %u slots)\n", touched.size(), n);
467 }
468 {
469 // queue_copy(): buffer copies recorded at the start of the pass; their keepalive goes once the pass retires.
470 constexpr VkDeviceSize kBytes = 64 * 1024;
471 void* src_data = nullptr;
472 void* dst_data = nullptr;
473 const VkBuffer src = d.buffer(kBytes, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, &src_data);
474 const VkBuffer dst = d.buffer(kBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &dst_data);
475 EXPECT(src && dst && src_data && dst_data);
476 auto* sw = static_cast<std::uint32_t*>(src_data);
477 for (std::size_t i = 0; i < kBytes / 4; ++i) sw[i] = std::uint32_t(i * 2654435761u);
478 std::memset(dst_data, 0, kBytes);
479 auto keep = std::make_shared<int>(7);
480 std::weak_ptr<int> watch = keep;
481 gpu.queue_copy(src, 4096, dst, 0, 8192, keep);
482 gpu.queue_copy(src, 0, dst, 32768, 4096, keep);
483 keep.reset();
484 EXPECT(gpu.pending() && !watch.expired());
485 bool recorded = false;
486 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
487 EXPECT(recorded && !watch.expired());
488 gpu.commit(true, 10);
489 gpu.drain(10, [](int, std::uint32_t, std::uint32_t) {}, [](int) {});
490 EXPECT(watch.expired());
491 const auto* dw = static_cast<const std::uint8_t*>(dst_data);
492 EXPECT(!std::memcmp(dw, static_cast<const std::uint8_t*>(src_data) + 4096, 8192));
493 EXPECT(!std::memcmp(dw + 32768, src_data, 4096));
494 EXPECT(dw[8192] == 0 && dw[32768 + 4096] == 0);
495 }
496 std::puts("console heap pass, queued copies: ok");
497 c.t.backed.clear();
498 c.t.ask = {{0, ~0ull}}; // everything asks again: the overflow cases below need their misses
499 gpu.set_spans(leonida::ConsoleHeapGpu::backed_table, c.t.backed);
500 gpu.set_spans(leonida::ConsoleHeapGpu::ask_table, c.t.ask);
501 std::puts("console heap pass, failed submission queued again: ok");
502
503 // More misses than the list holds: the overflow is reported, and the slots are still written.
504 {
505 constexpr std::uint32_t kStride = 0;
506 for (std::uint32_t s = 0; s < kSlots; ++s) {
507 vsharp(in + 8 * s, 0x500000000ull + s * 0x100, kStride, 16, 0);
508 vsharp(in + 8 * s + 4, 0x600000000ull + s * 0x100, kStride, 16, 0);
509 }
510 std::vector<std::uint32_t> many;
511 for (std::uint32_t s = 0; s < kSlots; ++s) many.push_back(s);
512 // 12,000 misses fit; list them twice over with a second heap view of the same buffers to pass 65,536.
513 const int heap2 = gpu.add_heap(d.device_address(input), d.device_address(copy), kSlots);
514 EXPECT(heap2 >= 0);
515 std::size_t total = 0;
516 for (int h = 0; h < 6; ++h) { // 6 x 12,000 = 72,000 misses in one pass over the two heaps
517 gpu.queue(h % 2 ? heap2 : heap, many.begin(), many.end());
518 total += 2 * many.size();
519 }
520 (void)total;
521 bool recorded = false;
522 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
523 // Queued twice per heap only counts once: 2 heaps x 12,000 = 24,000 misses, under the list. Queue a heap of
524 // its own instead to pass it.
525 gpu.commit(true, 4);
526 std::size_t misses = 0;
527 bool overflowed = false;
528 gpu.drain(4, [&](int, std::uint32_t, std::uint32_t) { ++misses; }, [&](int) { overflowed = true; });
529 EXPECT(recorded && misses == 24000 && !overflowed);
530 gpu.remove_heap(heap2);
531 }
532 std::puts("console heap pass, a slot queued twice is translated once: ok");
533 {
534 // Six heap views of the same buffers: 6 x 12,000 = 72,000 misses, past the list's 65,536.
535 std::vector<int> views;
536 for (int i = 0; i < 5; ++i) views.push_back(gpu.add_heap(d.device_address(input), d.device_address(copy), kSlots));
537 std::vector<std::uint32_t> many;
538 for (std::uint32_t s = 0; s < kSlots; ++s) many.push_back(s);
539 gpu.queue(heap, many.begin(), many.end());
540 for (const int v : views) {
541 EXPECT(v >= 0);
542 gpu.queue(v, many.begin(), many.end());
543 }
544 bool recorded = false;
545 EXPECT(d.run([&](VkCommandBuffer cb) { return recorded = gpu.record(cb); }));
546 gpu.commit(true, 5);
547 std::size_t misses = 0;
548 bool overflowed = false;
549 gpu.drain(5, [&](int, std::uint32_t, std::uint32_t) { ++misses; }, [&](int) { overflowed = true; });
550 EXPECT(recorded && overflowed && misses == leonida::ConsoleHeapGpu::kMissCapacity);
551 for (std::uint32_t s = 0; s < kSlots; ++s) EXPECT(out[8 * s] == 0 && out[8 * s + 2] == 0);
552 for (const int v : views) gpu.remove_heap(v);
553 }
554 std::puts("console heap pass, miss list overflow: ok");
555 {
556 // Clustered words (bases a megabyte apart) entered in both tables: every slot answered, none missed.
557 Case clustered;
558 for (std::uint32_t s2 = 0; s2 < 3000; ++s2) {
559 for (int half = 0; half < 2; ++half) {
560 const std::uint32_t i = 2 * s2 + std::uint32_t(half);
561 std::uint32_t* k = in + 8 * s2 + 4 * half;
562 vsharp(k, 0x700000000ull + (std::uint64_t(i) << 20), 16, 8, 0x2000);
563 const ConsoleHeapKeys::Key key{k[0], k[1], k[2], k[3]};
564 EXPECT(clustered.keys.put(key, 0x1000000ull + i * 256ull, 128, (i % 7) == 0));
565 EXPECT(gpu.with_keys([&](ConsoleHeapKeys& keys) {
566 return keys.put(key, 0x1000000ull + i * 256ull, 128, (i % 7) == 0);
567 }));
568 }
569 }
570 std::swap(c.keys, clustered.keys);
571 c.ranges.clear();
572 gpu.set_ranges(c.ranges);
573 EXPECT(round(all, 6, false) == 0);
574 }
575 std::puts("console heap pass, clustered words: ok");
576 const auto stats = gpu.take_stats();
577 std::printf("console heap pass: %llu passes, %llu slots, %llu updates, %llu misses, %llu overflows, %llu failed\n",
578 (unsigned long long)stats.passes, (unsigned long long)stats.slots, (unsigned long long)stats.updates,
579 (unsigned long long)stats.misses, (unsigned long long)stats.overflows,
580 (unsigned long long)stats.failed);
581 EXPECT(d.validation_errors() == 0);
582 std::puts("vk_console_heap_gpu_test: ok");
583 return 0;
584}