hashtable: Redesign the hashtable API.

This was intended to make the API public, so SDL_hashtable.h got an extreme
documentation makeover, but for now this remains a private header.

This makes several significant interface changes to SDL_HashTable, and
improves code that makes use of it in various ways.

- The ability to make "stackable" tables is removed. Apparently this still
  worked with the current implementation, but I could see a future
  implementation struggle mightily to support this. It'll be better for
  something external to build on top of the table if it needs it, inserting a
  linked list of stacked items as the hash values and managing them separately.
  There was only one place in SDL using this, unnecessarily, and that has also
  been cleaned up to not need it.
- You no longer specify "buckets" when creating a table, but rather an
  estimated number of items the table is meant to hold. The bucket count was
  crucial to our classic hashtable implementation, but meant less once we
  moved to an Open Addressing implementation anyhow, since the bucket count
  isn't static (and they aren't really "buckets" anymore either). Now you
  can just report how many items you think the hash will hold and SDL will
  allocate a reasonable default for you...or 0 to not guess, and SDL will
  start small and grow as necessary, which is often the correct thing to do.
- There's no more SDL_IterateHashTableKey because there's no more "stackable"
  hash tables.
- SDL_IterateHashTable() now uses a callback, which matches other parts of SDL,
  and also lets us hold the read-lock for the entire iteration and get rid of
  the goofy iterator state variable.
- SDL_InsertIntoHashTable() now lets you specify whether to replace existing
  keys or fail if the key already exists.
- Callbacks now use SDL conventions (userdata as the first param).
- Other naming convention fixes.

I discovered we use a lot of hash tables in SDL3 internally. :) So the bulk
of this work is fixing up that code to use the new interfaces, and simplifying
things (like checking for an item to remove it if it already exists before
inserting a replacement...just do the insert atomically, it'll do all that
for you!).
This commit is contained in:
Ryan C. Gordon
2025-02-13 16:13:43 -05:00
parent 4a9b579195
commit 84a236c92e
13 changed files with 1344 additions and 866 deletions

View File

@@ -36,58 +36,50 @@ struct GPU_PipelineCacheKeyStruct
Uint64 primitive_type : 3;
};
typedef union GPU_PipelineCacheKey
typedef union GPU_PipelineCacheKeyConverter
{
struct GPU_PipelineCacheKeyStruct as_struct;
Uint64 as_uint64;
} GPU_PipelineCacheKey;
} GPU_PipelineCacheKeyConverter;
SDL_COMPILE_TIME_ASSERT(GPU_PipelineCacheKey_Size, sizeof(GPU_PipelineCacheKey) <= sizeof(Uint64));
SDL_COMPILE_TIME_ASSERT(GPU_PipelineCacheKeyConverter_Size, sizeof(GPU_PipelineCacheKeyConverter) <= sizeof(Uint64));
typedef struct GPU_PipelineCacheEntry
static Uint32 SDLCALL HashPipelineCacheKey(void *userdata, const void *key)
{
GPU_PipelineCacheKey key;
SDL_GPUGraphicsPipeline *pipeline;
} GPU_PipelineCacheEntry;
const GPU_PipelineParameters *params = (const GPU_PipelineParameters *) key;
GPU_PipelineCacheKeyConverter cvt;
cvt.as_uint64 = 0;
cvt.as_struct.blend_mode = params->blend_mode;
cvt.as_struct.frag_shader = params->frag_shader;
cvt.as_struct.vert_shader = params->vert_shader;
cvt.as_struct.attachment_format = params->attachment_format;
cvt.as_struct.primitive_type = params->primitive_type;
static Uint32 HashPipelineCacheKey(const GPU_PipelineCacheKey *key)
{
Uint64 x = key->as_uint64;
// 64-bit uint hash function stolen from taisei (which stole it from somewhere else)
Uint64 x = cvt.as_uint64;
x = (x ^ (x >> 30)) * UINT64_C(0xbf58476d1ce4e5b9);
x = (x ^ (x >> 27)) * UINT64_C(0x94d049bb133111eb);
x = x ^ (x >> 31);
return (Uint32)(x & 0xffffffff);
}
static Uint32 HashPassthrough(const void *key, void *data)
static bool SDLCALL MatchPipelineCacheKey(void *userdata, const void *a, const void *b)
{
// double-cast to silence a clang warning
return (Uint32)(uintptr_t)key;
return (SDL_memcmp(a, b, sizeof (GPU_PipelineParameters)) == 0);
}
static bool MatchPipelineCacheKey(const void *a, const void *b, void *data)
static void SDLCALL DestroyPipelineCacheHashItem(void *userdata, const void *key, const void *value)
{
return a == b;
}
static void NukePipelineCacheEntry(const void *key, const void *value, void *data)
{
GPU_PipelineCacheEntry *entry = (GPU_PipelineCacheEntry *)value;
SDL_GPUDevice *device = data;
SDL_ReleaseGPUGraphicsPipeline(device, entry->pipeline);
SDL_free(entry);
SDL_GPUGraphicsPipeline *pipeline = (SDL_GPUGraphicsPipeline *) value;
SDL_GPUDevice *device = (SDL_GPUDevice *) userdata;
SDL_ReleaseGPUGraphicsPipeline(device, pipeline);
SDL_free((GPU_PipelineParameters *) key);
}
bool GPU_InitPipelineCache(GPU_PipelineCache *cache, SDL_GPUDevice *device)
{
// FIXME how many buckets do we need?
cache->table = SDL_CreateHashTable(device, 32, HashPassthrough, MatchPipelineCacheKey, NukePipelineCacheEntry, false, true);
if (!cache->table) {
return false;
}
return true;
cache->table = SDL_CreateHashTable(0, false, HashPipelineCacheKey, MatchPipelineCacheKey, DestroyPipelineCacheHashItem, device);
return (cache->table != NULL);
}
void GPU_DestroyPipelineCache(GPU_PipelineCache *cache)
@@ -180,45 +172,30 @@ static SDL_GPUGraphicsPipeline *MakePipeline(SDL_GPUDevice *device, GPU_Shaders
return SDL_CreateGPUGraphicsPipeline(device, &pci);
}
static GPU_PipelineCacheKey MakePipelineCacheKey(const GPU_PipelineParameters *params)
{
GPU_PipelineCacheKey key;
SDL_zero(key);
key.as_struct.blend_mode = params->blend_mode;
key.as_struct.frag_shader = params->frag_shader;
key.as_struct.vert_shader = params->vert_shader;
key.as_struct.attachment_format = params->attachment_format;
key.as_struct.primitive_type = params->primitive_type;
return key;
}
SDL_GPUGraphicsPipeline *GPU_GetPipeline(GPU_PipelineCache *cache, GPU_Shaders *shaders, SDL_GPUDevice *device, const GPU_PipelineParameters *params)
{
GPU_PipelineCacheKey key = MakePipelineCacheKey(params);
void *keyval = (void *)(uintptr_t)HashPipelineCacheKey(&key);
SDL_GPUGraphicsPipeline *pipeline = NULL;
if (!SDL_FindInHashTable(cache->table, params, (const void **) &pipeline)) {
bool inserted = false;
// !!! FIXME: why don't we have an SDL_alloc_copy function/macro?
GPU_PipelineParameters *paramscpy = (GPU_PipelineParameters *) SDL_malloc(sizeof (*paramscpy));
if (paramscpy) {
SDL_copyp(paramscpy, params);
pipeline = MakePipeline(device, shaders, params);
if (pipeline) {
inserted = SDL_InsertIntoHashTable(cache->table, paramscpy, pipeline, false);
}
}
void *iter = NULL;
GPU_PipelineCacheEntry *entry = NULL;
while (SDL_IterateHashTableKey(cache->table, keyval, (const void **)&entry, &iter)) {
if (entry->key.as_uint64 == key.as_uint64) {
return entry->pipeline;
if (!inserted) {
SDL_free(paramscpy);
if (pipeline) {
SDL_ReleaseGPUGraphicsPipeline(device, pipeline);
pipeline = NULL;
}
}
}
pipeline = MakePipeline(device, shaders, params);
if (pipeline == NULL) {
return NULL;
}
entry = SDL_malloc(sizeof(*entry));
entry->key = key;
entry->pipeline = pipeline;
SDL_InsertIntoHashTable(cache->table, keyval, entry);
return pipeline;
}