Skip to content

Latest commit

 

History

History
255 lines (213 loc) · 6.69 KB

finderrors.c

File metadata and controls

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