66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
#include "pxl8_shader_runtime.h"
|
|
#include "pxl8_log.h"
|
|
|
|
#include <dlfcn.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
struct pxl8_shader_lib {
|
|
void* handle;
|
|
char path[256];
|
|
};
|
|
|
|
pxl8_shader_lib* pxl8_shader_lib_load(const char* path) {
|
|
void* handle = dlopen(path, RTLD_NOW);
|
|
if (!handle) {
|
|
pxl8_error("Failed to load shader library: %s - %s", path, dlerror());
|
|
return NULL;
|
|
}
|
|
|
|
pxl8_shader_lib* lib = malloc(sizeof(pxl8_shader_lib));
|
|
lib->handle = handle;
|
|
strncpy(lib->path, path, sizeof(lib->path) - 1);
|
|
lib->path[sizeof(lib->path) - 1] = '\0';
|
|
|
|
return lib;
|
|
}
|
|
|
|
void pxl8_shader_lib_unload(pxl8_shader_lib* lib) {
|
|
if (!lib) return;
|
|
if (lib->handle) {
|
|
dlclose(lib->handle);
|
|
}
|
|
free(lib);
|
|
}
|
|
|
|
bool pxl8_shader_lib_reload(pxl8_shader_lib* lib) {
|
|
if (!lib) return false;
|
|
|
|
if (lib->handle) {
|
|
dlclose(lib->handle);
|
|
}
|
|
|
|
lib->handle = dlopen(lib->path, RTLD_NOW);
|
|
if (!lib->handle) {
|
|
pxl8_error("Failed to reload shader library: %s - %s", lib->path, dlerror());
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
pxl8_shader_fn pxl8_shader_lib_get(pxl8_shader_lib* lib, const char* name) {
|
|
if (!lib || !lib->handle) return NULL;
|
|
|
|
char symbol[128];
|
|
snprintf(symbol, sizeof(symbol), "pxl8_shader_%s", name);
|
|
|
|
void* fn = dlsym(lib->handle, symbol);
|
|
if (!fn) {
|
|
pxl8_error("Failed to find shader: %s - %s", symbol, dlerror());
|
|
return NULL;
|
|
}
|
|
|
|
return (pxl8_shader_fn)fn;
|
|
}
|