/
finderrors.c
276 lines (238 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <errno.h>
#include "mojoshader.h"
19
20
#if FINDERRORS_COMPILE_SHADERS
#include "SDL.h"
21
static SDL_Window *sdlwindow = NULL;
22
23
24
25
26
static void *lookup(const char *fnname, void *unused)
{
(void) unused;
return SDL_GL_GetProcAddress(fnname);
} // lookup
27
28
#endif
29
#ifdef _MSC_VER
30
31
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
32
#include <malloc.h> // for alloca().
33
#define snprintf _snprintf
34
35
#else
#include <dirent.h>
36
#include <sys/stat.h>
37
38
#endif
39
#define report printf
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
static int isdir(const char *dname)
{
#ifdef _MSC_VER
WIN32_FILE_ATTRIBUTE_DATA winstat;
if (!GetFileAttributesExA(dname, GetFileExInfoStandard, &winstat))
return 0;
return winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
#else
struct stat statbuf;
if (stat(dname, &statbuf) == -1)
return 0;
return S_ISDIR(statbuf.st_mode);
#endif
}
static int do_dir(const char *dname, const char *profile);
58
59
60
61
static int do_file(const char *profile, const char *dname, const char *fn, int *total)
{
int do_quit = 0;
62
63
64
if ((strcmp(fn, ".") == 0) || (strcmp(fn, "..") == 0))
return 1; // skip these.
65
66
67
68
69
70
71
#if FINDERRORS_COMPILE_SHADERS
SDL_Event e; // pump event queue to keep OS happy.
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
do_quit = 1;
} // while
72
SDL_GL_SwapWindow(sdlwindow);
73
74
75
76
77
78
79
80
#endif
if (do_quit)
{
report("FAIL: user requested quit!\n");
return 0;
} // if
81
82
83
84
85
86
87
88
89
char *fname = (char *) alloca(strlen(fn) + strlen(dname) + 2);
sprintf(fname, "%s/%s", dname, fn);
if (isdir(fname))
{
*total += do_dir(fname, profile);
return 1;
} // if
90
91
int assembly = 0;
if (strstr(fn, ".bytecode") != NULL)
92
assembly = 0;
93
94
95
else if (strstr(fn, ".disasm") != NULL)
assembly = 1;
else
96
97
98
99
100
101
102
103
104
105
106
107
return 1;
(*total)++;
FILE *io = fopen(fname, "rb");
if (io == NULL)
{
report("FAIL: %s fopen() failed.\n", fname);
return 1;
} // if
static unsigned char buf[1024 * 256];
108
int rc = fread(buf, 1, sizeof (buf), io);
109
110
111
112
113
114
115
fclose(io);
if (rc == -1)
{
report("FAIL: %s %s\n", fname, strerror(errno));
return 1;
} // if
116
117
118
119
if (assembly)
{
const MOJOSHADER_parseData *a;
120
buf[rc] = '\0'; // make sure the source is null-terminated.
121
a = MOJOSHADER_assemble(fname, (char *) buf, rc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
122
123
if (a->error_count > 0)
124
{
125
126
127
128
report("FAIL: %s (line %d) %s\n",
a->errors[0].filename ? a->errors[0].filename : "???",
a->errors[0].error_position,
a->errors[0].error);
129
130
131
132
133
134
135
136
137
138
139
140
141
142
return 1;
} // if
else if (a->output_len > sizeof (buf))
{
report("FAIL: %s buffer overflow in finderrors.c\n", fname);
return 1;
} // if
rc = a->output_len;
memcpy(buf, a->output, rc);
MOJOSHADER_freeParseData(a);
} // if
143
#if FINDERRORS_COMPILE_SHADERS
144
MOJOSHADER_glShader *shader = MOJOSHADER_glCompileShader(buf, rc, NULL, 0, NULL, 0);
145
146
147
if (shader == NULL)
report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
else
148
149
150
151
152
153
154
155
156
157
158
159
160
161
{
const MOJOSHADER_parseData *pd = MOJOSHADER_glGetShaderParseData(shader);
MOJOSHADER_glShader *v = (pd->shader_type == MOJOSHADER_TYPE_VERTEX) ? shader : NULL;
MOJOSHADER_glShader *p = (pd->shader_type == MOJOSHADER_TYPE_PIXEL) ? shader : NULL;
MOJOSHADER_glProgram *program = MOJOSHADER_glLinkProgram(v, p);
if (program == NULL)
report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
else
{
report("PASS: %s\n", fname);
MOJOSHADER_glDeleteProgram(program);
} // else
MOJOSHADER_glDeleteShader(shader);
}
162
#else
163
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, NULL, 0, NULL, 0, NULL, NULL, NULL);
164
if (pd->error_count == 0)
165
report("PASS: %s\n", fname);
166
167
168
169
170
171
172
173
174
else
{
int i;
for (i = 0; i < pd->error_count; i++)
{
report("FAIL: %s (position %d) %s\n", pd->errors[i].filename,
pd->errors[i].error_position, pd->errors[i].error);
} // for
} // else
175
176
177
178
179
180
181
MOJOSHADER_freeParseData(pd);
#endif
return 1;
} // do_file
182
static int do_dir(const char *dname, const char *profile)
183
184
{
int total = 0;
185
186
#ifdef _MSC_VER
187
188
189
190
191
192
const size_t wildcardlen = strlen(dname) + 3;
char *wildcard = (char *) alloca(wildcardlen);
SDL_snprintf(wildcard, wildcardlen, "%s\\*", dname);
WIN32_FIND_DATAA dent;
HANDLE dirp = FindFirstFileA(wildcard, &dent);
193
194
195
196
197
198
if (dirp != INVALID_HANDLE_VALUE)
{
do
{
if (!do_file(profile, dname, dent.cFileName, &total))
break;
199
} while (FindNextFileA(dirp, &dent) != 0);
200
FindClose(dirp);
201
202
203
} // if
#else
struct dirent *dent = NULL;
204
205
206
207
208
DIR *dirp = opendir(dname);
if (dirp != NULL)
{
while ((dent = readdir(dirp)) != NULL)
{
209
if (!do_file(profile, dname, dent->d_name, &total))
210
break;
211
212
213
} // while
closedir(dirp);
} // if
214
#endif
215
216
217
218
219
220
221
222
return total;
} // do_dir
int main(int argc, char **argv)
{
//printf("MojoShader finderrors\n");
223
224
//printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
//printf("Linked against changeset %s\n", MOJOSHADER_changeset());
225
226
227
228
229
230
//printf("\n");
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [dir1] ... [dirN]\n\n", argv[0]);
else
{
231
int okay = 0;
232
int total = 0;
233
int i;
234
const char *profile = argv[1];
235
236
#if FINDERRORS_COMPILE_SHADERS
237
238
239
240
241
242
243
244
245
246
247
248
MOJOSHADER_glContext *ctx = NULL;
if (SDL_Init(SDL_INIT_VIDEO) == -1)
fprintf(stderr, "SDL_Init() error: %s\n", SDL_GetError());
else if (SDL_GL_LoadLibrary(NULL) == -1)
fprintf(stderr, "SDL_GL_LoadLibrary() error: %s\n", SDL_GetError());
else if ((sdlwindow = SDL_CreateWindow(argv[0], SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL)) == NULL)
fprintf(stderr, "SDL_CreateWindow() error: %s\n", SDL_GetError());
else if (SDL_GL_CreateContext(sdlwindow) == NULL)
fprintf(stderr, "SDL_GL_CreateContext() error: %s\n", SDL_GetError());
else if ((ctx = MOJOSHADER_glCreateContext(profile, lookup, 0, 0, 0, 0)) == NULL)
fprintf(stderr, "MOJOSHADER_glCreateContext() fail: %s\n", MOJOSHADER_glGetError());
else
249
{
250
251
252
253
254
255
printf("Best profile is '%s'\n", MOJOSHADER_glBestProfile(lookup, 0, NULL, NULL, NULL));
MOJOSHADER_glMakeContextCurrent(ctx);
okay = 1;
}
#else
okay = 1;
256
257
#endif
258
259
260
261
262
263
if (okay)
{
for (i = 2; i < argc; i++)
total += do_dir(argv[i], profile);
printf("Saw %d files.\n", total);
} // if
264
265
#if FINDERRORS_COMPILE_SHADERS
266
267
if (ctx)
MOJOSHADER_glDestroyContext(ctx);
268
269
SDL_Quit();
#endif
270
271
272
273
274
275
} // else
return 0;
} // main
// end of finderrors.c ...