Files
mandy/runtime/test/descriptor_table_test.cpp
SuarezPM e6eaecd05a Runtime: the descriptor table lives in device-local memory (D3D-76)
AI-assisted. Set 0 binding 2 is the table every T#/S# lookup probes.
BindlessSet allocated it HOST_VISIBLE | HOST_COHERENT and flush() memcpy'd into
the mapping, so a divergent lookup read it over PCIe: 19.5x slower for T#+S#
pairs and 46.2x for a divergent miss at 100k entries on an RTX 2060 SUPER
(vk_descriptor_bench, #639; 58-796x on the 5080).

The table now lives in DEVICE_LOCAL (HOST_VISIBLE kept as a fallback for a device
without it). flush() takes the command buffer that will use the set and uploads
only what changed: the whole table from a mapped staging buffer when the buffers
were just created (first use or growth), and the changed slots with
vkCmdUpdateBuffer otherwise, which copies the data into the command buffer while
recording so no staging bytes are read at execution. A TRANSFER_WRITE ->
SHADER_READ barrier follows, before the draws. DescriptorTable tracks the slots
that insert, replace, erase, clear and grow touched, including both slots of a
relocation, so a flush no longer rebuilds an 80 MiB table for one descriptor.

A command buffer whose vkQueueSubmit is rejected never runs the upload, so the
set keeps the obligation: VulkanQueue::execute reports the submit result through
a commit callback, and the set rebuilds the table on the next flush, which also
reports flush_needs_idle() and makes the device wait before it. The same wait now
honours the pre-existing contract for replaced/erased entries and for growth
(the device ignored flush_needs_idle() before), and frees the retired buffers
once the queue is idle instead of at teardown.

Verified on an NVIDIA RTX 2060 SUPER, Linux 620.73.04, Vulkan 1.4.357:
- ctest 10/10, including descriptor_table cases for the change list, relocation
  marking and consume-once reads.
- vk_bindless_test with LEONIDA_VK_VALIDATION=1: 14 draws (probe depth, erase,
  relocation, growth 64 -> 512 slots, rebind, failed-submit retry, other view,
  removal) read back exact, table in device-local memory in both
  descriptor-indexing configurations. With the retry disabled the test is red.
- GPU suite 64/91 with the same 27 64-wide skips as main, 0 failures; 91/91 with
  LEONIDA_W64_EMULATE=1.

No game data; synthetic fixtures only.
2026-11-21 23:30:46 -03:00

374 lines
13 KiB
C++

// Unit tests for the descriptor hash table (set 0, binding 2 contract).
// Tests FNV-1a hashing, T#/S# key generation, probe resolution, dynamic resizing,
// GPU buffer serialization, and multithreaded insertion.
#include "../src/descriptor_table.h"
#include <cassert>
#include <cstdio>
#include <cstring>
#include <random>
#include <thread>
#include <vector>
#define EXPECT(condition) do { \
if (!(condition)) { \
std::fprintf(stderr, "assertion failed: %s at %s:%d\n", #condition, __FILE__, __LINE__); \
return 1; \
} \
} while (0)
using namespace leonida;
int test_fnv1a_hash_matches_contract() {
// Verified against vk_texture_test.cpp key_hash and rdna2spirv.py desc_probe
const std::uint32_t zeros[8] = {0, 0, 0, 0, 0, 0, 0, 0};
std::uint32_t expected_zero_hash = 2166136261u;
for (int i = 0; i < 8; ++i) expected_zero_hash = (expected_zero_hash ^ 0) * 16777619u;
EXPECT(hash_descriptor_key(zeros) == expected_zero_hash);
const std::uint32_t sample_key[8] = {
0x12345678, 0x9abcdef0, 0x11223344, 0x55667788,
0x99aabbcc, 0xddeeff00, 0x01020304, 0x05060708
};
std::uint32_t expected_hash = 2166136261u;
for (int i = 0; i < 8; ++i) expected_hash = (expected_hash ^ sample_key[i]) * 16777619u;
EXPECT(hash_descriptor_key(sample_key) == expected_hash);
return 0;
}
int test_ssharp_key_padding() {
const std::uint32_t ssharp[4] = {0x92, 0xc00000, 0x14500000, 0x0};
const auto key = make_ssharp_key(ssharp);
EXPECT(key[0] == 0x92);
EXPECT(key[1] == 0xc00000);
EXPECT(key[2] == 0x14500000);
EXPECT(key[3] == 0x0);
EXPECT(key[4] == 0xFFFFFFFFu);
EXPECT(key[5] == 0xFFFFFFFFu);
EXPECT(key[6] == 0xFFFFFFFFu);
EXPECT(key[7] == 0xFFFFFFFFu);
return 0;
}
int test_basic_insertion_and_lookup() {
DescriptorTable table(16);
EXPECT(table.capacity() == 16);
EXPECT(table.mask() == 15);
EXPECT(table.size() == 0);
const std::uint32_t tsharp[8] = {
0x1000, 0x2000, 0x3000, 0x4000,
0x5000, 0x6000, 0x7000, 0x8000
};
EXPECT(table.lookup(tsharp) == 0);
EXPECT(!table.contains(tsharp));
EXPECT(table.insert(tsharp, 1));
EXPECT(table.size() == 1);
EXPECT(table.contains(tsharp));
EXPECT(table.lookup(tsharp) == 1);
// Duplicate key updates in place
EXPECT(table.insert(tsharp, 42));
EXPECT(table.size() == 1);
EXPECT(table.lookup(tsharp) == 42);
// S# key
const std::uint32_t ssharp[4] = {0xa, 0xb, 0xc, 0xd};
const auto skey = make_ssharp_key(ssharp);
EXPECT(table.insert(skey, 7));
EXPECT(table.size() == 2);
EXPECT(table.lookup(skey) == 7);
EXPECT(table.lookup(tsharp) == 42);
return 0;
}
int test_load_factor_and_growth() {
// Start with 16 slots. Load factor threshold 50% means max 8 entries before growth.
DescriptorTable table(16);
EXPECT(table.capacity() == 16);
std::vector<DescriptorKey> keys;
for (std::uint32_t i = 1; i <= 20; ++i) {
std::uint32_t k[8] = {i, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7, i * 8};
keys.push_back(make_tsharp_key(k));
EXPECT(table.insert(keys.back(), i + 100));
}
EXPECT(table.size() == 20);
EXPECT(table.capacity() >= 64); // Doubled from 16 -> 32 -> 64
EXPECT(table.mask() == table.capacity() - 1);
// Verify all 20 keys resolve to their exact indices after growths
for (std::uint32_t i = 1; i <= 20; ++i) {
EXPECT(table.lookup(keys[i - 1]) == i + 100);
}
return 0;
}
int test_insert_reports_mapping_changes() {
DescriptorTable table;
const DescriptorKey key{1, 2, 3, 4, 5, 6, 7, 8};
using Result = DescriptorTable::InsertResult;
EXPECT(table.insert_or_update(key.data(), 0) == Result::added);
EXPECT(table.insert_or_update(key.data(), 0) == Result::unchanged);
EXPECT(table.insert_or_update(key.data(), 17) == Result::replaced);
EXPECT(table.insert_or_update(key.data(), 17) == Result::unchanged);
EXPECT(table.lookup(key) == 17);
EXPECT(table.size() == 1);
return 0;
}
int test_probe_collision_resolution() {
DescriptorTable table(64);
// Create 4 distinct keys that share the exact same hash
// We can forge keys that share hash or test linear collision handling
std::uint32_t k1[8] = {1, 0, 0, 0, 0, 0, 0, 0};
std::uint32_t k2[8] = {2, 0, 0, 0, 0, 0, 0, 0};
std::uint32_t k3[8] = {3, 0, 0, 0, 0, 0, 0, 0};
EXPECT(table.insert(k1, 10));
EXPECT(table.insert(k2, 20));
EXPECT(table.insert(k3, 30));
EXPECT(table.lookup(k1) == 10);
EXPECT(table.lookup(k2) == 20);
EXPECT(table.lookup(k3) == 30);
return 0;
}
int test_gpu_buffer_serialization() {
DescriptorTable table(16);
const std::uint32_t t1[8] = {10, 20, 30, 40, 50, 60, 70, 80};
const std::uint32_t s1[4] = {1, 2, 3, 4};
const auto skey = make_ssharp_key(s1);
EXPECT(table.insert(t1, 5));
EXPECT(table.insert(skey, 9));
const auto gpu_buf = table.build_gpu_buffer(false);
EXPECT(gpu_buf.size() == 1 + 16 * 10);
EXPECT(gpu_buf[0] == 15); // mask
// Verify lookup by traversing the generated buffer using the exact shader logic
const auto shader_lookup = [&](const std::uint32_t key[8]) -> std::uint32_t {
const std::uint32_t mask = gpu_buf[0];
const std::uint32_t h = hash_descriptor_key(key);
for (std::uint32_t k = 0; k < 8; ++k) {
const std::uint32_t slot = (h + k) & mask;
const std::size_t base = 1 + slot * 10;
bool match = true;
for (std::size_t j = 0; j < 8; ++j) {
if (gpu_buf[base + j] != key[j]) {
match = false;
break;
}
}
if (match) {
return gpu_buf[base + 8]; // index
}
}
return 0;
};
EXPECT(shader_lookup(t1) == 5);
EXPECT(shader_lookup(skey.data()) == 9);
const std::uint32_t missing[8] = {99, 99, 99, 99, 99, 99, 99, 99};
EXPECT(shader_lookup(missing) == 0);
// Test write_to_buffer
std::vector<std::uint32_t> raw_copy(gpu_buf.size());
table.write_to_buffer(raw_copy.data(), true);
EXPECT(raw_copy[0] == 15);
for (std::size_t s = 0; s < 16; ++s) {
EXPECT(raw_copy[1 + s * 10 + 9] == 0); // clear_pad = true
}
return 0;
}
int test_multithreaded_insert_and_lookup() {
DescriptorTable table(32);
constexpr int kThreads = 4;
constexpr int kPerThread = 50;
std::vector<std::thread> workers;
for (int t = 0; t < kThreads; ++t) {
workers.emplace_back([&table, t] {
for (int i = 0; i < kPerThread; ++i) {
const std::uint32_t id = static_cast<std::uint32_t>(t * kPerThread + i + 1);
std::uint32_t key[8] = {id, id * 3, id * 7, id * 11, id * 13, id * 17, id * 19, id * 23};
table.insert(key, id);
}
});
}
for (auto& w : workers) {
w.join();
}
EXPECT(table.size() == kThreads * kPerThread);
for (int t = 0; t < kThreads; ++t) {
for (int i = 0; i < kPerThread; ++i) {
const std::uint32_t id = static_cast<std::uint32_t>(t * kPerThread + i + 1);
std::uint32_t key[8] = {id, id * 3, id * 7, id * 11, id * 13, id * 17, id * 19, id * 23};
EXPECT(table.lookup(key) == id);
}
}
return 0;
}
int test_clear_resets_table() {
DescriptorTable table(16);
std::uint32_t k[8] = {1, 2, 3, 4, 5, 6, 7, 8};
table.insert(k, 100);
EXPECT(table.size() == 1);
EXPECT(table.lookup(k) == 100);
table.clear();
EXPECT(table.size() == 0);
EXPECT(table.lookup(k) == 0);
return 0;
}
int test_erase_and_cluster_compact() {
DescriptorTable table(16);
const std::uint32_t k1[8] = {1, 10, 100, 1000, 2, 20, 200, 2000};
const std::uint32_t k2[8] = {3, 30, 300, 3000, 4, 40, 400, 4000};
const std::uint32_t k3[8] = {5, 50, 500, 5000, 6, 60, 600, 6000};
// Erasing a non-existent key returns false
EXPECT(!table.erase(k1));
EXPECT(table.insert(k1, 101));
EXPECT(table.insert(k2, 102));
EXPECT(table.insert(k3, 103));
EXPECT(table.size() == 3);
// Erase middle element
EXPECT(table.erase(k2));
EXPECT(table.size() == 2);
EXPECT(!table.contains(k2));
EXPECT(table.lookup(k2) == 0);
EXPECT(table.contains(k1));
EXPECT(table.lookup(k1) == 101);
EXPECT(table.contains(k3));
EXPECT(table.lookup(k3) == 103);
// Double erase returns false
EXPECT(!table.erase(k2));
// Erase remaining entries
EXPECT(table.erase(k1));
EXPECT(table.size() == 1);
EXPECT(!table.contains(k1));
EXPECT(table.lookup(k1) == 0);
EXPECT(table.contains(k3));
EXPECT(table.lookup(k3) == 103);
EXPECT(table.erase(k3));
EXPECT(table.size() == 0);
EXPECT(!table.contains(k3));
EXPECT(table.lookup(k3) == 0);
// Re-inserting into erased slots
EXPECT(table.insert(k1, 201));
EXPECT(table.size() == 1);
EXPECT(table.contains(k1));
EXPECT(table.lookup(k1) == 201);
return 0;
}
int test_change_tracking() {
DescriptorTable table(16);
const std::uint32_t k1[8] = {1, 2, 3, 4, 5, 6, 7, 8};
const std::uint32_t k2[8] = {9, 10, 11, 12, 13, 14, 15, 16};
// A fresh table has nothing to upload.
DescriptorTable::Changes changes = table.take_changes();
EXPECT(!changes.all && changes.slots.empty());
// An insert marks exactly the slot it landed in, and read_slot agrees with build_gpu_buffer.
EXPECT(table.insert(k1, 42));
changes = table.take_changes();
EXPECT(!changes.all && changes.slots.size() == 1);
const std::uint32_t slot = changes.slots.front();
const auto words = table.build_gpu_buffer();
std::uint32_t read[10] = {};
EXPECT(table.read_slot(slot, read));
EXPECT(std::memcmp(read, &words[1 + slot * 10], sizeof(read)) == 0);
EXPECT(read[8] == 42 && read[9] == kSlotOccupiedMarker);
EXPECT(table.take_changes().slots.empty()); // taking consumes the list
// Replacing an index marks the same slot; read_slot sees the new index.
EXPECT(table.insert_or_update(k1, 43) == DescriptorTable::InsertResult::replaced);
changes = table.take_changes();
EXPECT(!changes.all && changes.slots.size() == 1 && changes.slots.front() == slot);
EXPECT(table.read_slot(slot, read) && read[8] == 43);
// Erasing marks the slot it was in and the ones the cluster compaction moved.
EXPECT(table.insert(k2, 7));
table.take_changes();
EXPECT(table.erase(k1));
changes = table.take_changes();
EXPECT(!changes.all && !changes.slots.empty() && changes.slots.front() == slot);
// Growth changes the mask, so everything re-uploads.
const std::size_t before = table.capacity();
for (std::uint32_t i = 0; i < 200 && table.capacity() == before; ++i) {
const std::uint32_t key[8] = {i, i * 2, i * 3, i * 4, i * 5, i * 6, i * 7, i * 8};
table.insert(key, 1000 + i);
}
EXPECT(table.capacity() > before);
EXPECT(table.take_changes().all);
// Out-of-range slots are refused rather than read past the table.
EXPECT(!table.read_slot(std::uint32_t(table.capacity()), read));
return 0;
}
int test_relocation_marks_both_slots() {
DescriptorTable table(64);
const std::uint32_t slots = std::uint32_t(table.capacity());
// Eight keys in eight adjacent home slots across the end of the table; the ninth shares the first home and can
// only fit by moving a resident (make_probe_slot), which changes two slots.
std::array<DescriptorKey, kDescriptorProbes + 1> keys{};
for (std::uint32_t i = 0; i < keys.size(); ++i) {
auto& key = keys[i];
key[0] = i + 1;
const std::uint32_t target = (slots - 4 + i % kDescriptorProbes) & (slots - 1);
while ((hash_descriptor_key(key) & (slots - 1)) != target) ++key[7];
}
for (std::uint32_t i = 0; i < kDescriptorProbes; ++i) EXPECT(table.insert(keys[i].data(), 100 + i));
table.take_changes();
EXPECT(table.insert_or_update(keys.back().data(), 999) == DescriptorTable::InsertResult::added_with_relocation);
const DescriptorTable::Changes changes = table.take_changes();
EXPECT(!changes.all);
EXPECT(changes.slots.size() >= 2); // the hole a resident moved into and the slot the new key took
for (std::uint32_t i = 0; i < kDescriptorProbes; ++i) EXPECT(table.lookup(keys[i]) == 100 + i);
EXPECT(table.lookup(keys.back()) == 999);
return 0;
}
int main() {
EXPECT(test_fnv1a_hash_matches_contract() == 0);
EXPECT(test_ssharp_key_padding() == 0);
EXPECT(test_basic_insertion_and_lookup() == 0);
EXPECT(test_insert_reports_mapping_changes() == 0);
EXPECT(test_load_factor_and_growth() == 0);
EXPECT(test_probe_collision_resolution() == 0);
EXPECT(test_gpu_buffer_serialization() == 0);
EXPECT(test_change_tracking() == 0);
EXPECT(test_relocation_marks_both_slots() == 0);
EXPECT(test_multithreaded_insert_and_lookup() == 0);
EXPECT(test_clear_resets_table() == 0);
EXPECT(test_erase_and_cluster_compact() == 0);
std::printf("descriptor_table_test: all tests passed (0 failures)\n");
return 0;
}