Skip to content

Latest commit

 

History

History
276 lines (238 loc) · 7.35 KB

finderrors.c

File metadata and controls

276 lines (238 loc) · 7.35 KB
 
Apr 29, 2008
Apr 29, 2008
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"
Apr 22, 2008
Apr 22, 2008
19
20
#if FINDERRORS_COMPILE_SHADERS
#include "SDL.h"
Feb 21, 2014
Feb 21, 2014
21
static SDL_Window *sdlwindow = NULL;
Mar 31, 2010
Mar 31, 2010
22
23
24
25
26
static void *lookup(const char *fnname, void *unused)
{
(void) unused;
return SDL_GL_GetProcAddress(fnname);
} // lookup
Apr 22, 2008
Apr 22, 2008
27
28
#endif
Apr 30, 2008
Apr 30, 2008
29
#ifdef _MSC_VER
Apr 30, 2008
Apr 30, 2008
30
31
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
Apr 30, 2008
Apr 30, 2008
32
#include <malloc.h> // for alloca().
Apr 30, 2008
Apr 30, 2008
33
#define snprintf _snprintf
Apr 30, 2008
Apr 30, 2008
34
35
#else
#include <dirent.h>
Feb 25, 2014
Feb 25, 2014
36
#include <sys/stat.h>
Apr 30, 2008
Apr 30, 2008
37
38
#endif
Apr 22, 2008
Apr 22, 2008
39
#define report printf
Apr 22, 2008
Apr 22, 2008
40
Feb 25, 2014
Feb 25, 2014
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);
Apr 30, 2008
Apr 30, 2008
58
59
60
61
static int do_file(const char *profile, const char *dname, const char *fn, int *total)
{
int do_quit = 0;
Feb 25, 2014
Feb 25, 2014
62
63
64
if ((strcmp(fn, ".") == 0) || (strcmp(fn, "..") == 0))
return 1; // skip these.
Apr 30, 2008
Apr 30, 2008
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
Feb 21, 2014
Feb 21, 2014
72
SDL_GL_SwapWindow(sdlwindow);
Apr 30, 2008
Apr 30, 2008
73
74
75
76
77
78
79
80
#endif
if (do_quit)
{
report("FAIL: user requested quit!\n");
return 0;
} // if
Feb 25, 2014
Feb 25, 2014
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
Dec 12, 2008
Dec 12, 2008
90
91
int assembly = 0;
if (strstr(fn, ".bytecode") != NULL)
Dec 13, 2008
Dec 13, 2008
92
assembly = 0;
Dec 12, 2008
Dec 12, 2008
93
94
95
else if (strstr(fn, ".disasm") != NULL)
assembly = 1;
else
Apr 30, 2008
Apr 30, 2008
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];
Feb 11, 2009
Feb 11, 2009
108
int rc = fread(buf, 1, sizeof (buf), io);
Apr 30, 2008
Apr 30, 2008
109
110
111
112
113
114
115
fclose(io);
if (rc == -1)
{
report("FAIL: %s %s\n", fname, strerror(errno));
return 1;
} // if
Dec 12, 2008
Dec 12, 2008
116
117
118
119
if (assembly)
{
const MOJOSHADER_parseData *a;
Dec 12, 2008
Dec 12, 2008
120
buf[rc] = '\0'; // make sure the source is null-terminated.
Feb 12, 2009
Feb 12, 2009
121
a = MOJOSHADER_assemble(fname, (char *) buf, rc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Feb 11, 2009
Feb 11, 2009
122
Feb 3, 2009
Feb 3, 2009
123
if (a->error_count > 0)
Dec 12, 2008
Dec 12, 2008
124
{
Feb 12, 2009
Feb 12, 2009
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);
Dec 12, 2008
Dec 12, 2008
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
Apr 30, 2008
Apr 30, 2008
143
#if FINDERRORS_COMPILE_SHADERS
May 29, 2012
May 29, 2012
144
MOJOSHADER_glShader *shader = MOJOSHADER_glCompileShader(buf, rc, NULL, 0, NULL, 0);
Apr 30, 2008
Apr 30, 2008
145
146
147
if (shader == NULL)
report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
else
Jul 31, 2008
Jul 31, 2008
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);
}
Apr 30, 2008
Apr 30, 2008
162
#else
Apr 25, 2016
Apr 25, 2016
163
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, NULL, buf, rc, NULL, 0, NULL, 0, NULL, NULL, NULL);
Nov 19, 2010
Nov 19, 2010
164
if (pd->error_count == 0)
Apr 30, 2008
Apr 30, 2008
165
report("PASS: %s\n", fname);
Nov 19, 2010
Nov 19, 2010
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
Apr 30, 2008
Apr 30, 2008
175
176
177
178
179
180
181
MOJOSHADER_freeParseData(pd);
#endif
return 1;
} // do_file
Apr 22, 2008
Apr 22, 2008
182
static int do_dir(const char *dname, const char *profile)
183
184
{
int total = 0;
Apr 30, 2008
Apr 30, 2008
185
186
#ifdef _MSC_VER
Feb 25, 2014
Feb 25, 2014
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);
Apr 30, 2008
Apr 30, 2008
193
194
195
196
197
198
if (dirp != INVALID_HANDLE_VALUE)
{
do
{
if (!do_file(profile, dname, dent.cFileName, &total))
break;
Apr 30, 2008
Apr 30, 2008
199
} while (FindNextFileA(dirp, &dent) != 0);
Feb 25, 2014
Feb 25, 2014
200
FindClose(dirp);
Apr 30, 2008
Apr 30, 2008
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)
{
Apr 30, 2008
Apr 30, 2008
209
if (!do_file(profile, dname, dent->d_name, &total))
Apr 22, 2008
Apr 22, 2008
210
break;
211
212
213
} // while
closedir(dirp);
} // if
Apr 30, 2008
Apr 30, 2008
214
#endif
215
216
217
218
219
220
221
222
return total;
} // do_dir
int main(int argc, char **argv)
{
//printf("MojoShader finderrors\n");
Mar 24, 2010
Mar 24, 2010
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
{
Feb 21, 2014
Feb 21, 2014
231
int okay = 0;
232
int total = 0;
Apr 22, 2008
Apr 22, 2008
233
int i;
Apr 27, 2008
Apr 27, 2008
234
const char *profile = argv[1];
235
Apr 22, 2008
Apr 22, 2008
236
#if FINDERRORS_COMPILE_SHADERS
Feb 21, 2014
Feb 21, 2014
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
Apr 27, 2008
Apr 27, 2008
249
{
Feb 21, 2014
Feb 21, 2014
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;
Apr 22, 2008
Apr 22, 2008
256
257
#endif
Feb 21, 2014
Feb 21, 2014
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
Apr 22, 2008
Apr 22, 2008
265
#if FINDERRORS_COMPILE_SHADERS
Feb 21, 2014
Feb 21, 2014
266
267
if (ctx)
MOJOSHADER_glDestroyContext(ctx);
Apr 22, 2008
Apr 22, 2008
268
269
SDL_Quit();
#endif
270
271
272
273
274
275
} // else
return 0;
} // main
// end of finderrors.c ...