mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 15:18:38 +00:00
Fall back to a dedicated buffer allocation when the shared arena fills. Update console descriptors and resource backing, preserve pipeline cache entries, and replay mesh records using bounded indirect arguments. Keep scripted input available until read and cover the runtime and translator paths with synthetic tests. Co-authored-by: Plaaasma <47513553+Plaaasma@users.noreply.github.com> Co-authored-by: ivanepsilondelta <264061546+ivanepsilondelta@users.noreply.github.com>
616 lines
26 KiB
C++
616 lines
26 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;
|
|
|
|
// The shared contract vectors (the translator's desc_probe and this table must agree on both kinds): 512 slots, mask
|
|
// 511. Each row is a key -> the bare FNV-1a hash (slot 345 and friends) and the fnv1a_fmix32 hash (spread).
|
|
struct HashVector {
|
|
DescriptorKey key;
|
|
std::uint32_t fnv1a;
|
|
std::uint32_t fnv1a_slot;
|
|
std::uint32_t fnv1a_fmix32;
|
|
std::uint32_t fnv1a_fmix32_slot;
|
|
};
|
|
|
|
constexpr std::array<HashVector, 8> kHashVectors{{
|
|
{{0x00100000u, 0x00000000u, 0x90500FACu, 0x00000000u, 0u, 0u, 0u, 0u}, 0x1CD4CD59u, 345, 0x7CE036C5u, 197},
|
|
{{0x10100000u, 0x00000000u, 0x90500FACu, 0x00000000u, 0u, 0u, 0u, 0u}, 0x0CD4CD59u, 345, 0x646B8915u, 277},
|
|
{{0x20100000u, 0x00000000u, 0x90500FACu, 0x00000000u, 0u, 0u, 0u, 0u}, 0x3CD4CD59u, 345, 0xC2E843B6u, 438},
|
|
{{0x30100000u, 0x00000000u, 0x90500FACu, 0x00000000u, 0u, 0u, 0u, 0u}, 0x2CD4CD59u, 345, 0xE11420E3u, 227},
|
|
{{0x12345678u, 0x9ABCDEF0u, 0x0FEDCBA9u, 0x87654321u, 0x11111111u, 0x22222222u, 0x33333333u, 0x44444444u},
|
|
0xBD4A977Fu, 383, 0x3AE592B1u, 177},
|
|
{{0u, 0u, 0u, 0u, 0u, 0u, 0u, 0u}, 0x9BE17165u, 357, 0xBB10DC19u, 25},
|
|
{{0x00010001u, 0x00400040u, 0x10014FACu, 0x00000000u, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu},
|
|
0x814225E4u, 484, 0xD5398A35u, 53},
|
|
{{0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu, 0xFFFFFFFFu},
|
|
0xB0E4285Du, 93, 0x98A3F73Au, 314},
|
|
}};
|
|
|
|
int test_shared_hash_vectors() {
|
|
for (const HashVector& v : kHashVectors) {
|
|
EXPECT(hash_descriptor_key(v.key, DescriptorHashKind::fnv1a) == v.fnv1a);
|
|
EXPECT(hash_descriptor_key(v.key, DescriptorHashKind::fnv1a_fmix32) == v.fnv1a_fmix32);
|
|
EXPECT((v.fnv1a & 511u) == v.fnv1a_slot);
|
|
EXPECT((v.fnv1a_fmix32 & 511u) == v.fnv1a_fmix32_slot);
|
|
// fnv1a_fmix32 is the same FNV loop plus the finalizer, and the old default overload is still bare FNV-1a.
|
|
EXPECT(hash_descriptor_key_fnv1a(v.key.data()) == v.fnv1a);
|
|
EXPECT(finalize_descriptor_hash(v.fnv1a) == v.fnv1a_fmix32);
|
|
EXPECT(hash_descriptor_key(v.key) == v.fnv1a);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// The clustering the fmix32 finalizer removes: T#s that differ only in the top nibble of dword 0 share slot 345 under
|
|
// bare FNV-1a (four keys in one probe run), and land in four different home slots under fnv1a_fmix32.
|
|
int test_fmix32_spreads_the_clustered_keys() {
|
|
constexpr std::uint32_t mask = 511;
|
|
std::array<std::uint32_t, 4> fnv1a_slots{}, fmix32_slots{};
|
|
for (std::size_t i = 0; i < fnv1a_slots.size(); ++i) {
|
|
fnv1a_slots[i] = hash_descriptor_key(kHashVectors[i].key, DescriptorHashKind::fnv1a) & mask;
|
|
fmix32_slots[i] = hash_descriptor_key(kHashVectors[i].key, DescriptorHashKind::fnv1a_fmix32) & mask;
|
|
EXPECT(fnv1a_slots[i] == 345u);
|
|
}
|
|
for (std::size_t i = 0; i < fmix32_slots.size(); ++i)
|
|
for (std::size_t j = i + 1; j < fmix32_slots.size(); ++j) EXPECT(fmix32_slots[i] != fmix32_slots[j]);
|
|
return 0;
|
|
}
|
|
|
|
// A table finds every key it holds with the kind it was built with, at that kind's home slot and probe window; a table
|
|
// built with the other kind does not answer them (the minority of a mixed library misses to index 0).
|
|
int test_table_uses_the_kind_it_was_built_with() {
|
|
constexpr std::uint32_t slots = 512;
|
|
constexpr std::uint32_t mask = slots - 1;
|
|
for (const DescriptorHashKind kind : {DescriptorHashKind::fnv1a, DescriptorHashKind::fnv1a_fmix32}) {
|
|
DescriptorTable table(slots, kind);
|
|
EXPECT(table.capacity() == slots && table.mask() == mask && table.hash_kind() == kind);
|
|
for (std::size_t i = 0; i < kHashVectors.size(); ++i) EXPECT(table.insert(kHashVectors[i].key, i + 1));
|
|
EXPECT(table.size() == kHashVectors.size());
|
|
for (std::size_t i = 0; i < kHashVectors.size(); ++i) {
|
|
EXPECT(table.lookup(kHashVectors[i].key) == i + 1);
|
|
EXPECT(table.contains(kHashVectors[i].key));
|
|
// The key is in its own kind's probe window, with its index and the occupied marker.
|
|
const std::uint32_t home = hash_descriptor_key(kHashVectors[i].key, kind) & mask;
|
|
bool found = false;
|
|
for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
|
|
std::uint32_t words[10] = {};
|
|
EXPECT(table.read_slot((home + k) & mask, words));
|
|
if (std::memcmp(words, kHashVectors[i].key.data(), sizeof(DescriptorKey)) == 0) {
|
|
EXPECT(words[8] == i + 1 && words[9] == kSlotOccupiedMarker);
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
EXPECT(found);
|
|
}
|
|
}
|
|
// Lookups placed with fnv1a_fmix32 fall outside the bare FNV-1a window (345..352) of the clustered keys.
|
|
DescriptorTable fmix32_table(slots, DescriptorHashKind::fnv1a_fmix32);
|
|
for (std::size_t i = 0; i < 4; ++i) EXPECT(fmix32_table.insert(kHashVectors[i].key, i + 1));
|
|
for (std::size_t i = 0; i < 4; ++i) {
|
|
const std::uint32_t fnv1a_home = hash_descriptor_key(kHashVectors[i].key, DescriptorHashKind::fnv1a) & mask;
|
|
bool found = false;
|
|
for (std::uint32_t k = 0; k < kDescriptorProbes; ++k) {
|
|
std::uint32_t words[10] = {};
|
|
EXPECT(fmix32_table.read_slot((fnv1a_home + k) & mask, words));
|
|
found |= std::memcmp(words, kHashVectors[i].key.data(), sizeof(DescriptorKey)) == 0;
|
|
}
|
|
EXPECT(!found);
|
|
}
|
|
// The table's own kind still finds them (the same table, looked up as the device would).
|
|
for (std::size_t i = 0; i < 4; ++i) EXPECT(fmix32_table.lookup(kHashVectors[i].key) == i + 1);
|
|
return 0;
|
|
}// The bare FNV-1a contract the translator's desc_probe has always used (the default, no marker).
|
|
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_reinsert() {
|
|
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 only the slot it was in: no other entry moves.
|
|
EXPECT(table.insert(k2, 7));
|
|
table.take_changes();
|
|
EXPECT(table.erase(k1));
|
|
changes = table.take_changes();
|
|
EXPECT(!changes.all && changes.slots.size() == 1 && 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 and wrapping uint32 slot ranges are refused rather than read past the table.
|
|
EXPECT(!table.read_slot(std::uint32_t(table.capacity()), read));
|
|
EXPECT(!table.read_slots(UINT32_MAX, 1, read));
|
|
EXPECT(!table.read_slots(UINT32_MAX - 2, 4, read));
|
|
EXPECT(!table.read_slots(0, 1, nullptr));
|
|
EXPECT(table.read_slots(0, 0, nullptr));
|
|
|
|
// Direct write_to_buffer matches build_gpu_buffer byte-for-byte for both clear_pad modes.
|
|
for (bool clear_pad : {false, true}) {
|
|
const auto expected = table.build_gpu_buffer(clear_pad);
|
|
std::vector<std::uint32_t> direct(table.size_dwords(), 0xccccccccu);
|
|
table.write_to_buffer(direct.data(), clear_pad);
|
|
EXPECT(direct == expected);
|
|
}
|
|
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;
|
|
}
|
|
|
|
// Shaders keep probing the GPU copy of the table while an erase is uploaded without waiting for idle
|
|
// (BindlessSet::flush_replaces_table), so an erase must leave every other key's slot exactly as it was.
|
|
int test_erase_moves_no_other_entry() {
|
|
DescriptorTable table(64);
|
|
const std::uint32_t slots = std::uint32_t(table.capacity());
|
|
// Five keys with one home: they fill five consecutive slots, the cluster the old erase compacted.
|
|
std::array<DescriptorKey, 5> keys{};
|
|
for (std::uint32_t i = 0; i < keys.size(); ++i) {
|
|
auto& key = keys[i];
|
|
key[0] = i + 1;
|
|
while ((hash_descriptor_key(key) & (slots - 1)) != slots - 2) ++key[7]; // the cluster wraps the end
|
|
EXPECT(table.insert_or_update(key.data(), 200 + i) == DescriptorTable::InsertResult::added);
|
|
}
|
|
table.take_changes();
|
|
const auto before = table.build_gpu_buffer();
|
|
|
|
EXPECT(table.erase(keys[1].data()));
|
|
const DescriptorTable::Changes changes = table.take_changes();
|
|
EXPECT(!changes.all && changes.slots.size() == 1);
|
|
const std::uint32_t hole = changes.slots.front();
|
|
const auto after = table.build_gpu_buffer();
|
|
for (std::uint32_t s = 0; s < slots; ++s) {
|
|
const bool same = std::memcmp(&before[1 + s * 10], &after[1 + s * 10], 10 * sizeof(std::uint32_t)) == 0;
|
|
EXPECT(same == (s != hole));
|
|
}
|
|
for (std::uint32_t w = 0; w < 10; ++w) EXPECT(after[1 + hole * 10 + w] == 0); // an empty slot: a miss
|
|
for (std::uint32_t i = 0; i < keys.size(); ++i) EXPECT(table.lookup(keys[i]) == (i == 1 ? 0u : 200 + i));
|
|
|
|
// Keys past the hole are still found after more erases, and a new key of the same home reuses the hole.
|
|
EXPECT(table.erase(keys[0].data()));
|
|
EXPECT(table.lookup(keys[4]) == 204 && table.lookup(keys[3]) == 203);
|
|
DescriptorKey extra = keys[0];
|
|
extra[0] = 100;
|
|
while ((hash_descriptor_key(extra) & (slots - 1)) != slots - 2) ++extra[7];
|
|
table.take_changes();
|
|
EXPECT(table.insert_or_update(extra.data(), 300) == DescriptorTable::InsertResult::added);
|
|
const DescriptorTable::Changes reuse = table.take_changes();
|
|
EXPECT(reuse.slots.size() == 1 && reuse.slots.front() == slots - 2); // the first hole of the probe window
|
|
EXPECT(table.lookup(extra) == 300 && table.size() == 4);
|
|
return 0;
|
|
}
|
|
|
|
int test_unresolvable_hash_collision() {
|
|
DescriptorTable table(32);
|
|
std::array<DescriptorKey, 9> keys{};
|
|
for (std::uint32_t i = 0; i < keys.size(); ++i) {
|
|
keys[i][0] = i;
|
|
// Invert the final FNV multiplication to make every complete hash zero.
|
|
std::uint32_t prefix = kFnvOffsetBasis;
|
|
for (int word = 0; word < 7; ++word) prefix = (prefix ^ keys[i][word]) * kFnvPrime;
|
|
keys[i][7] = prefix;
|
|
EXPECT(hash_descriptor_key(keys[i]) == 0);
|
|
}
|
|
for (std::uint32_t i = 0; i < 8; ++i) table.insert(keys[i], i + 1);
|
|
const auto before = table.build_gpu_buffer();
|
|
bool rejected = false;
|
|
try { table.insert(keys[8], 9); }
|
|
catch (const std::length_error&) { rejected = true; }
|
|
EXPECT(rejected && table.size() == 8 && table.capacity() == 32);
|
|
EXPECT(table.build_gpu_buffer() == before);
|
|
for (std::uint32_t i = 0; i < 8; ++i) EXPECT(table.lookup(keys[i]) == i + 1);
|
|
EXPECT(table.insert_or_update(keys[0].data(), 99) == DescriptorTable::InsertResult::replaced);
|
|
EXPECT(table.lookup(keys[0]) == 99);
|
|
EXPECT(table.erase(keys[0]));
|
|
EXPECT(table.insert(keys[8], 9) && table.lookup(keys[8]) == 9);
|
|
return 0;
|
|
}
|
|
|
|
int test_capacity_overflow() {
|
|
for (std::uint32_t requested : {0x80000001u, 0xffffffffu}) {
|
|
bool rejected = false;
|
|
try { DescriptorTable table(requested); }
|
|
catch (const std::length_error&) { rejected = true; }
|
|
EXPECT(rejected);
|
|
}
|
|
for (std::uint32_t requested : {0u, 1u, 16u, 17u, 31u, 32u}) {
|
|
DescriptorTable table(requested);
|
|
EXPECT(table.capacity() == (requested <= 16 ? 16 : 32));
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// The entry's last dword is what a `descriptor_table` tagged module reads next to the index: the occupied bit and
|
|
// the arrays the index is bound in (kind_sampled_bit / kind_storage_bit). A plain module never reads it, so a table
|
|
// inserted without a kind must serialize exactly as before (marker, no kind bits).
|
|
int test_kind_word() {
|
|
DescriptorTable table(16);
|
|
const DescriptorKey image = make_tsharp_key(std::array<std::uint32_t, 8>{0x00001000u, 0x00000000u, 0u, 0u,
|
|
0u, 0u, 0u, 0u}.data());
|
|
const std::uint32_t kind = kind_sampled_bit(1) | kind_storage_bit(1);
|
|
EXPECT(table.insert(image, 5, kind));
|
|
EXPECT(table.lookup(image) == 5);
|
|
const auto words = table.build_gpu_buffer();
|
|
EXPECT(words[0] == 15);
|
|
const std::uint32_t slot = (hash_descriptor_key(image.data(), DescriptorHashKind::fnv1a) & 15);
|
|
if (words[1 + slot * 10 + 8] != 5) return 1; // find the slot the key landed in
|
|
EXPECT((words[1 + slot * 10 + 9] & kSlotOccupiedMarker) != 0);
|
|
EXPECT((words[1 + slot * 10 + 9] & kKindMask) == kind);
|
|
|
|
// A plain entry: the marker and no kind bits, as before the marker existed.
|
|
const DescriptorKey plain = make_tsharp_key(std::array<std::uint32_t, 8>{0x00002000u, 0u, 0u, 0u, 0u, 0u, 0u,
|
|
0u}.data());
|
|
EXPECT(table.insert(plain, 6));
|
|
std::uint32_t found[10];
|
|
EXPECT(table.read_slot(slot, found));
|
|
(void)found;
|
|
DescriptorEntry entry{};
|
|
entry.kind = kSlotOccupiedMarker;
|
|
EXPECT(slot_occupied(entry) && slot_kind(entry) == 0);
|
|
|
|
// The same index with a new kind is a change the GPU copy needs.
|
|
EXPECT(table.insert_or_update(image.data(), 5, kind_sampled_bit(2)) ==
|
|
DescriptorTable::InsertResult::replaced);
|
|
EXPECT(table.insert_or_update(image.data(), 5, kind_sampled_bit(2)) ==
|
|
DescriptorTable::InsertResult::unchanged);
|
|
const auto changed = table.take_changes();
|
|
EXPECT(!changed.slots.empty() || changed.all);
|
|
|
|
// clear_occupied drops the marker but keeps the kind, which is what a shader reads.
|
|
const auto gpu = table.build_gpu_buffer(true);
|
|
EXPECT((gpu[1 + slot * 10 + 9] & kSlotOccupiedMarker) == 0);
|
|
EXPECT((gpu[1 + slot * 10 + 9] & kKindMask) == kind_sampled_bit(2));
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
EXPECT(test_unresolvable_hash_collision() == 0);
|
|
EXPECT(test_capacity_overflow() == 0);
|
|
EXPECT(test_fnv1a_hash_matches_contract() == 0);
|
|
EXPECT(test_shared_hash_vectors() == 0);
|
|
EXPECT(test_fmix32_spreads_the_clustered_keys() == 0);
|
|
EXPECT(test_table_uses_the_kind_it_was_built_with() == 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_erase_moves_no_other_entry() == 0);
|
|
EXPECT(test_multithreaded_insert_and_lookup() == 0);
|
|
EXPECT(test_clear_resets_table() == 0);
|
|
EXPECT(test_erase_and_reinsert() == 0);
|
|
EXPECT(test_kind_word() == 0);
|
|
|
|
std::printf("descriptor_table_test: all tests passed (0 failures)\n");
|
|
return 0;
|
|
}
|