wip multithreaded texture load

This commit is contained in:
veclavtalica
2025-02-09 07:34:16 +03:00
parent 037548436d
commit 5a7d7433d1
5 changed files with 158 additions and 52 deletions

View File

@ -175,7 +175,7 @@ static void finally_use_2d_pipeline(void) {
pipeline_last_used = PIPELINE_2D;
}
/* TODO: ensure we minimize depth func switching to enable Hi-Z (hierarchical depth) optimizations */
static void finally_use_texture_mode(TextureMode mode) {
if (texture_mode_last_used == mode)
return;

View File

@ -1,6 +1,7 @@
#include "twn_draw_c.h"
#include "twn_draw.h"
#include "twn_workers_c.h"
#include "twn_textures_c.h"
#define FAST_OBJ_IMPLEMENTATION
#define FAST_OBJ_REALLOC SDL_realloc
@ -29,6 +30,7 @@ static struct ModelDrawCommand {
/* deferred queue of model files to load from worker threads */
static SDL_mutex *model_load_mutex;
static char const **model_load_queue;
static size_t model_load_queued;
static bool model_load_initialized;
/* use streaming via callbacks to reduce memory congestion */
@ -55,10 +57,11 @@ static unsigned long model_load_callback_size(void *handle, void *udata) {
/* TODO: is there a way to do this nicely while locking main thread? */
/* sleeping over atomic counter might be good enough i guess */
/* it's safe to access everything without lock after this returns true and no public api is possible to call */
static bool model_load_workers_finished(void) {
bool result;
SDL_LockMutex(model_load_mutex);
result = arrlenu(model_load_queue) == 0;
result = model_load_queued == 0;
SDL_UnlockMutex(model_load_mutex);
return result;
}
@ -84,12 +87,12 @@ bool model_load_workers_thread(void) {
.file_size = model_load_callback_size
};
/* TODO: immediately create jobs for missing textures */
fastObjMesh *mesh = fast_obj_read_with_callbacks(load_request, &callbacks, NULL);
fastObjMesh *const mesh = fast_obj_read_with_callbacks(load_request, &callbacks, NULL);
SDL_LockMutex(model_load_mutex);
struct ModelCacheItem *item = shgetp(model_cache, load_request);
item->value.mesh = mesh;
model_load_queued--;
SDL_UnlockMutex(model_load_mutex);
return true;
@ -108,12 +111,14 @@ void draw_model(const char *model,
struct ModelCacheItem const *item;
/* TODO: make it lockless */
/* if model is missing, queue it up for loading */
SDL_LockMutex(model_load_mutex);
if (!(item = shgetp_null(model_cache, model))) {
model = SDL_strdup(model);
shput(model_cache, model, (struct ModelCacheItemValue){0});
arrpush(model_load_queue, model);
model_load_queued++;
SDL_SemPost(workers_job_semaphore);
} else
model = item->key;
@ -130,14 +135,14 @@ void draw_model(const char *model,
void finally_draw_models(void) {
while (!model_load_workers_finished()) {
(void)0;
}
while (!model_load_workers_finished())
SDL_Delay(1);
/* TODO: have special path for them, preserving the buffers and potentially using instanced draw */
for (int i = 0; i < arrlen(model_draw_commands); ++i) {
struct ModelDrawCommand const *const command = &model_draw_commands[i];
fastObjMesh const *const mesh = model_cache[shgeti(model_cache, command->model)].value.mesh;
SDL_assert(mesh);
for (unsigned int g = 0; g < mesh->group_count; ++g) {
fastObjGroup const *const group = &mesh->groups[g];
unsigned int idx = 0;
@ -208,9 +213,8 @@ void finally_draw_models(void) {
/* drop model caches */
void free_model_cache(void) {
while (!model_load_workers_finished()) {
(void)0;
}
while (!model_load_workers_finished())
SDL_Delay(1);
for (size_t i = 0; i < shlenu(model_cache); ++i) {
fast_obj_destroy(model_cache[i].value.mesh);