Skip to content
Permalink
Browse files
Ripped out the multithreading from finderrors.c ...
Parsing 15,000 shaders takes under a second anyhow, and stupid OpenGL isn't
 thread safe, so you eventually bottleneck down to the main thread for the
 expensive part anyhow...so might as well make this code readable.

--HG--
branch : trunk
  • Loading branch information
icculus committed Apr 22, 2008
1 parent 3306bcd commit 913e851ff28125d92bfa138c6d633de4ce4c8b0f
Showing with 33 additions and 135 deletions.
  1. +33 −135 finderrors.c
@@ -2,7 +2,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <pthread.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
@@ -18,31 +17,7 @@
#include <glext.h>
#endif

static const char *profile = NULL;
static volatile int die_threads = 0;
static pthread_mutex_t grab_mutex;
static pthread_mutex_t report_mutex;

typedef struct ShaderBytecode
{
void *name;
void *data;
struct ShaderBytecode *next;
} ShaderBytecode;

static volatile ShaderBytecode *gbytecode = NULL;
static volatile ShaderBytecode *gparsed = NULL;

static void report(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
pthread_mutex_lock(&report_mutex);
vprintf(fmt, ap);
pthread_mutex_unlock(&report_mutex);
va_end(ap);
} // report

#define report printf

static int compile_shader(const char *fname, const MOJOSHADER_parseData *pd)
{
@@ -90,70 +65,48 @@ static int compile_shader(const char *fname, const MOJOSHADER_parseData *pd)
} // compile_shader


static void *worker(void *unused)
{
unsigned char buf[1024 * 256];
while (!die_threads)
{
ShaderBytecode *my_bytecode = NULL;
pthread_mutex_lock(&grab_mutex);
if (gbytecode == NULL)
die_threads = 1;
else
{
my_bytecode = (ShaderBytecode *) gbytecode;
gbytecode = gbytecode->next;
} // else
pthread_mutex_unlock(&grab_mutex);

if (my_bytecode == NULL)
break;

const char *fname = (const char *) my_bytecode->name;
FILE *io = fopen(fname, "rb");
if (io == NULL)
report("FAIL: %s fopen() failed.\n", fname);
else
{
int rc = fread(buf, 1, sizeof (buf), io);
fclose(io);
if (rc == -1)
report("FAIL: %s %s\n", fname, strerror(errno));
else
my_bytecode->data = (void *) MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);

pthread_mutex_lock(&grab_mutex);
my_bytecode->next = (ShaderBytecode *) gparsed;
gparsed = my_bytecode;
pthread_mutex_unlock(&grab_mutex);
} // else
} // while

return NULL;
} // worker


static int do_dir(const char *dname)
static int do_dir(const char *dname, const char *profile)
{
const int dirlen = strlen(dname) + 1;
int total = 0;
DIR *dirp = opendir(dname);
if (dirp != NULL)
{
ShaderBytecode *bytecode = NULL;
struct dirent *dent;
while ((dent = readdir(dirp)) != NULL)
{
if (strstr(dent->d_name, ".bytecode") == NULL)
continue;

total++;
bytecode = (ShaderBytecode *) malloc(sizeof (ShaderBytecode));
bytecode->data = NULL;
bytecode->name = malloc(strlen(dent->d_name) + dirlen);
sprintf((char *) bytecode->name, "%s/%s", dname, dent->d_name);
bytecode->next = (ShaderBytecode *) gbytecode;
gbytecode = bytecode;

char *fname = (char *) alloca(strlen(dent->d_name) + dirlen);
sprintf(fname, "%s/%s", dname, dent->d_name);
FILE *io = fopen(fname, "rb");
if (io == NULL)
{
report("FAIL: %s fopen() failed.\n", fname);
continue;
} // if

static unsigned char buf[1024 * 256];
int rc = fread(buf, 1, sizeof (buf), io);
fclose(io);
if (rc == -1)
{
report("FAIL: %s %s\n", fname, strerror(errno));
continue;
} // if

const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);
if (pd->error != NULL)
report("FAIL: %s %s\n", fname, pd->error);
else
{
if (compile_shader(fname, pd))
report("PASS: %s\n", fname);
} // else
MOJOSHADER_freeParseData(pd);
} // while
closedir(dirp);
} // if
@@ -173,76 +126,21 @@ int main(int argc, char **argv)
printf("\n\nUSAGE: %s <profile> [dir1] ... [dirN]\n\n", argv[0]);
else
{
#define MAX_WORKERS 4
pthread_t workers[MAX_WORKERS];
int total = 0;
int i;

#if FINDERRORS_COMPILE_SHADERS
SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
#endif

pthread_mutex_init(&grab_mutex, NULL);
pthread_mutex_init(&report_mutex, NULL);

profile = argv[1];
int i;
const char *profile = argv[1];

for (i = 2; i < argc; i++)
total += do_dir(argv[i]);
total += do_dir(argv[i], profile);

printf("Saw %d bytecode files.\n", total);

for (i = 0; i < MAX_WORKERS; i++)
pthread_create(&workers[i], NULL, worker, NULL);

while (1)
{
ShaderBytecode *my_bytecode = NULL;
pthread_mutex_lock(&grab_mutex);
if (gparsed != NULL)
{
my_bytecode = (ShaderBytecode *) gparsed;
gparsed = gparsed->next;
} // if
pthread_mutex_unlock(&grab_mutex);

if (my_bytecode == NULL)
{
if (gbytecode == NULL)
break;
else
{
usleep(10000);
continue;
} // else
} // if

const MOJOSHADER_parseData *pd = (const MOJOSHADER_parseData *)
my_bytecode->data;
const char *fname = my_bytecode->name;
if (pd != NULL)
{
if (pd->error != NULL)
report("FAIL: %s %s\n", fname, pd->error);
else
{
if (compile_shader(fname, pd))
report("PASS: %s\n", fname);
} // else
MOJOSHADER_freeParseData(pd);
} // if

free(my_bytecode->name);
free(my_bytecode);
} // while

for (i = 0; i < MAX_WORKERS; i++)
pthread_join(workers[i], NULL);

pthread_mutex_destroy(&report_mutex);
pthread_mutex_destroy(&grab_mutex);

#if FINDERRORS_COMPILE_SHADERS
SDL_Quit();
#endif

0 comments on commit 913e851

Please sign in to comment.