finderrors.c
author Ryan C. Gordon <icculus@icculus.org>
Mon, 21 Apr 2008 23:35:49 -0400
branchtrunk
changeset 169 7e9d90724cf8
parent 153 4410cc03dade
child 170 9d71c08c756e
permissions -rw-r--r--
Push parsed shaders through GLSL compiler to see what breaks. Multithreading is a real pain.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     1
#include <stdio.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     2
#include <stdlib.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     3
#include <string.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     4
#include <stdarg.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     5
#include <pthread.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     6
#include <sys/types.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     7
#include <dirent.h>
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
     8
#include <errno.h>
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
     9
#include <unistd.h>
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    10
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    11
#include "mojoshader.h"
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    12
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    13
#define FINDERRORS_COMPILE_SHADERS 1
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    14
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    15
#if FINDERRORS_COMPILE_SHADERS
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    16
#include "SDL.h"
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    17
#include <gl.h>
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    18
#include <glext.h>
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    19
#endif
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    20
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    21
static const char *profile = NULL;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    22
static volatile int die_threads = 0;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    23
static pthread_mutex_t grab_mutex;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    24
static pthread_mutex_t report_mutex;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    25
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    26
typedef struct ShaderBytecode
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    27
{
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    28
    void *name;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    29
    void *data;
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    30
    struct ShaderBytecode *next;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    31
} ShaderBytecode;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    32
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    33
static volatile ShaderBytecode *gbytecode = NULL;
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    34
static volatile ShaderBytecode *gparsed = NULL;
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    35
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    36
static void report(const char *fmt, ...)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    37
{
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    38
    va_list ap;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    39
    va_start(ap, fmt);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    40
    pthread_mutex_lock(&report_mutex);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    41
    vprintf(fmt, ap);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    42
    pthread_mutex_unlock(&report_mutex);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    43
    va_end(ap);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    44
} // report
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    45
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    46
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    47
static int compile_shader(const char *fname, const MOJOSHADER_parseData *pd)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    48
{
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    49
    int retval = 1;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    50
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    51
    #if FINDERRORS_COMPILE_SHADERS
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    52
    const GLenum shader_type = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? GL_FRAGMENT_SHADER_ARB : GL_VERTEX_SHADER_ARB;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    53
    GLint shaderlen = (GLint) pd->output_len;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    54
    GLhandleARB program = glCreateProgramObjectARB();
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    55
    GLhandleARB shader = glCreateShaderObjectARB(shader_type);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    56
    GLint ok = 0;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    57
    GLcharARB err[1024];
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    58
    GLsizei len = 0;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    59
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    60
    retval = 0;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    61
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    62
    glShaderSourceARB(shader, 1, (const GLcharARB **) &pd->output, &shaderlen);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    63
    glCompileShaderARB(shader);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    64
    glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &ok);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    65
    if (!ok)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    66
    {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    67
        glGetInfoLogARB(shader, sizeof (err), &len, err);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    68
        printf("FAIL: %s glsl compile: %s\n", fname, err);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    69
    } // if
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    70
    else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    71
    {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    72
        glAttachObjectARB(program, shader);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    73
        glLinkProgramARB(program);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    74
        glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &ok);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    75
        if (!ok)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    76
        {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    77
            glGetInfoLogARB(program, sizeof (err), &len, err);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    78
            printf("FAIL: %s glsl link: %s\n", fname, err);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    79
        } // if
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    80
        else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    81
        {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    82
            retval = 1;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    83
        } // else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    84
    } // else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    85
    glDeleteObjectARB(shader);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    86
    glDeleteObjectARB(program);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    87
    #endif
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    88
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    89
    return retval;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    90
} // compile_shader
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    91
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
    92
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    93
static void *worker(void *unused)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    94
{
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    95
    unsigned char buf[1024 * 256];
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    96
    while (!die_threads)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    97
    {
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    98
        ShaderBytecode *my_bytecode = NULL;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
    99
        pthread_mutex_lock(&grab_mutex);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   100
        if (gbytecode == NULL)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   101
            die_threads = 1;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   102
        else
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   103
        {
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   104
            my_bytecode = (ShaderBytecode *) gbytecode;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   105
            gbytecode = gbytecode->next;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   106
        } // else
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   107
        pthread_mutex_unlock(&grab_mutex);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   108
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   109
        if (my_bytecode == NULL)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   110
            break;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   111
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   112
        const char *fname = (const char *) my_bytecode->name;
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   113
        FILE *io = fopen(fname, "rb");
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   114
        if (io == NULL)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   115
            report("FAIL: %s fopen() failed.\n", fname);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   116
        else
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   117
        {
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   118
            int rc = fread(buf, 1, sizeof (buf), io);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   119
            fclose(io);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   120
            if (rc == -1)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   121
                report("FAIL: %s %s\n", fname, strerror(errno));
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   122
            else
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   123
                my_bytecode->data = (void *) MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   124
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   125
            pthread_mutex_lock(&grab_mutex);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   126
            my_bytecode->next = (ShaderBytecode *) gparsed;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   127
            gparsed = my_bytecode;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   128
            pthread_mutex_unlock(&grab_mutex);
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   129
        } // else
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   130
    } // while
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   131
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   132
    return NULL;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   133
} // worker
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   134
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   135
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   136
static int do_dir(const char *dname)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   137
{
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   138
    const int dirlen = strlen(dname) + 1;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   139
    int total = 0;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   140
    DIR *dirp = opendir(dname);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   141
    if (dirp != NULL)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   142
    {
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   143
        ShaderBytecode *bytecode = NULL;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   144
        struct dirent *dent;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   145
        while ((dent = readdir(dirp)) != NULL)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   146
        {
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   147
            if (strstr(dent->d_name, ".bytecode") == NULL)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   148
                continue;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   149
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   150
            total++;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   151
            bytecode = (ShaderBytecode *) malloc(sizeof (ShaderBytecode));
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   152
            bytecode->data = NULL;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   153
            bytecode->name = malloc(strlen(dent->d_name) + dirlen);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   154
            sprintf((char *) bytecode->name, "%s/%s", dname, dent->d_name);
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   155
            bytecode->next = (ShaderBytecode *) gbytecode;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   156
            gbytecode = bytecode;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   157
        } // while
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   158
        closedir(dirp);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   159
    } // if
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   160
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   161
    return total;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   162
} // do_dir
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   163
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   164
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   165
int main(int argc, char **argv)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   166
{
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   167
    //printf("MojoShader finderrors\n");
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   168
    //printf("Compiled against version %d\n", MOJOSHADER_VERSION);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   169
    //printf("Linked against version %d\n", MOJOSHADER_version());
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   170
    //printf("\n");
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   171
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   172
    if (argc <= 2)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   173
        printf("\n\nUSAGE: %s <profile> [dir1] ... [dirN]\n\n", argv[0]);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   174
    else
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   175
    {
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   176
        #define MAX_WORKERS 4
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   177
        pthread_t workers[MAX_WORKERS];
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   178
        int total = 0;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   179
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   180
        #if FINDERRORS_COMPILE_SHADERS
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   181
        SDL_Init(SDL_INIT_VIDEO);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   182
        SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   183
        #endif
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   184
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   185
        pthread_mutex_init(&grab_mutex, NULL);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   186
        pthread_mutex_init(&report_mutex, NULL);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   187
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   188
        profile = argv[1];
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   189
        int i;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   190
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   191
        for (i = 2; i < argc; i++)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   192
            total += do_dir(argv[i]);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   193
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   194
        printf("Saw %d bytecode files.\n", total);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   195
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   196
        for (i = 0; i < MAX_WORKERS; i++)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   197
            pthread_create(&workers[i], NULL, worker, NULL);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   198
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   199
        while (1)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   200
        {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   201
            ShaderBytecode *my_bytecode = NULL;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   202
            pthread_mutex_lock(&grab_mutex);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   203
            if (gparsed != NULL)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   204
            {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   205
                my_bytecode = (ShaderBytecode *) gparsed;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   206
                gparsed = gparsed->next;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   207
            } // if
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   208
            pthread_mutex_unlock(&grab_mutex);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   209
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   210
            if (my_bytecode == NULL)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   211
            {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   212
                if (gbytecode == NULL)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   213
                    break;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   214
                else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   215
                {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   216
                    usleep(10000);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   217
                    continue;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   218
                } // else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   219
            } // if
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   220
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   221
            const MOJOSHADER_parseData *pd = (const MOJOSHADER_parseData *)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   222
                                                my_bytecode->data;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   223
            const char *fname = my_bytecode->name;
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   224
            if (pd != NULL)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   225
            {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   226
                if (pd->error != NULL)
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   227
                    report("FAIL: %s %s\n", fname, pd->error);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   228
                else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   229
                {
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   230
                    if (compile_shader(fname, pd))
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   231
                        report("PASS: %s\n", fname);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   232
                } // else
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   233
                MOJOSHADER_freeParseData(pd);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   234
            } // if
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   235
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   236
            free(my_bytecode->name);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   237
            free(my_bytecode);
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   238
        } // while
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   239
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   240
        for (i = 0; i < MAX_WORKERS; i++)
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   241
            pthread_join(workers[i], NULL);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   242
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   243
        pthread_mutex_destroy(&report_mutex);
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   244
        pthread_mutex_destroy(&grab_mutex);
169
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   245
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   246
        #if FINDERRORS_COMPILE_SHADERS
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   247
        SDL_Quit();
7e9d90724cf8 Push parsed shaders through GLSL compiler to see what breaks.
Ryan C. Gordon <icculus@icculus.org>
parents: 153
diff changeset
   248
        #endif
153
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   249
    } // else
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   250
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   251
    return 0;
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   252
} // main
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   253
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   254
// end of finderrors.c ...
4410cc03dade Added finderrors.c to revision control.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
   255