Skip to content

Latest commit

 

History

History
234 lines (201 loc) · 5.92 KB

finderrors.c

File metadata and controls

234 lines (201 loc) · 5.92 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"
Mar 31, 2010
Mar 31, 2010
21
22
23
24
25
static void *lookup(const char *fnname, void *unused)
{
(void) unused;
return SDL_GL_GetProcAddress(fnname);
} // lookup
Apr 22, 2008
Apr 22, 2008
26
27
#endif
Apr 30, 2008
Apr 30, 2008
28
#ifdef _MSC_VER
Apr 30, 2008
Apr 30, 2008
29
30
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
Apr 30, 2008
Apr 30, 2008
31
#include <malloc.h> // for alloca().
Apr 30, 2008
Apr 30, 2008
32
#define snprintf _snprintf
Apr 30, 2008
Apr 30, 2008
33
34
#else
#include <dirent.h>
Apr 30, 2008
Apr 30, 2008
35
36
#endif
Apr 22, 2008
Apr 22, 2008
37
#define report printf
Apr 22, 2008
Apr 22, 2008
38
Apr 30, 2008
Apr 30, 2008
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
static int do_file(const char *profile, const char *dname, const char *fn, int *total)
{
int do_quit = 0;
#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
SDL_GL_SwapBuffers();
#endif
if (do_quit)
{
report("FAIL: user requested quit!\n");
return 0;
} // if
Dec 12, 2008
Dec 12, 2008
59
60
int assembly = 0;
if (strstr(fn, ".bytecode") != NULL)
Dec 13, 2008
Dec 13, 2008
61
assembly = 0;
Dec 12, 2008
Dec 12, 2008
62
63
64
else if (strstr(fn, ".disasm") != NULL)
assembly = 1;
else
Apr 30, 2008
Apr 30, 2008
65
66
67
68
69
70
71
72
73
74
75
76
77
78
return 1;
(*total)++;
char *fname = (char *) alloca(strlen(fn) + strlen(dname) + 1);
sprintf(fname, "%s/%s", dname, fn);
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
79
int rc = fread(buf, 1, sizeof (buf), io);
Apr 30, 2008
Apr 30, 2008
80
81
82
83
84
85
86
fclose(io);
if (rc == -1)
{
report("FAIL: %s %s\n", fname, strerror(errno));
return 1;
} // if
Dec 12, 2008
Dec 12, 2008
87
88
89
90
if (assembly)
{
const MOJOSHADER_parseData *a;
Dec 12, 2008
Dec 12, 2008
91
buf[rc] = '\0'; // make sure the source is null-terminated.
Feb 12, 2009
Feb 12, 2009
92
a = MOJOSHADER_assemble(fname, (char *) buf, rc, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
Feb 11, 2009
Feb 11, 2009
93
Feb 3, 2009
Feb 3, 2009
94
if (a->error_count > 0)
Dec 12, 2008
Dec 12, 2008
95
{
Feb 12, 2009
Feb 12, 2009
96
97
98
99
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
114
#if FINDERRORS_COMPILE_SHADERS
May 29, 2012
May 29, 2012
115
MOJOSHADER_glShader *shader = MOJOSHADER_glCompileShader(buf, rc, NULL, 0, NULL, 0);
Apr 30, 2008
Apr 30, 2008
116
117
118
if (shader == NULL)
report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
else
Jul 31, 2008
Jul 31, 2008
119
120
121
122
123
124
125
126
127
128
129
130
131
132
{
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
133
#else
Nov 6, 2008
Nov 6, 2008
134
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, NULL, 0, NULL, NULL, NULL);
Nov 19, 2010
Nov 19, 2010
135
if (pd->error_count == 0)
Apr 30, 2008
Apr 30, 2008
136
report("PASS: %s\n", fname);
Nov 19, 2010
Nov 19, 2010
137
138
139
140
141
142
143
144
145
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
146
147
148
149
150
151
152
MOJOSHADER_freeParseData(pd);
#endif
return 1;
} // do_file
Apr 22, 2008
Apr 22, 2008
153
static int do_dir(const char *dname, const char *profile)
154
155
{
int total = 0;
Apr 30, 2008
Apr 30, 2008
156
157
158
159
#ifdef _MSC_VER
WIN32_FIND_DATA dent;
HANDLE dirp = INVALID_HANDLE_VALUE;
Apr 30, 2008
Apr 30, 2008
160
FindFirstFileA(dname, &dent);
Apr 30, 2008
Apr 30, 2008
161
162
163
164
165
166
if (dirp != INVALID_HANDLE_VALUE)
{
do
{
if (!do_file(profile, dname, dent.cFileName, &total))
break;
Apr 30, 2008
Apr 30, 2008
167
} while (FindNextFileA(dirp, &dent) != 0);
Apr 30, 2008
Apr 30, 2008
168
169
170
171
CloseHandle(dirp);
} // if
#else
struct dirent *dent = NULL;
172
173
174
175
176
DIR *dirp = opendir(dname);
if (dirp != NULL)
{
while ((dent = readdir(dirp)) != NULL)
{
Apr 30, 2008
Apr 30, 2008
177
if (!do_file(profile, dname, dent->d_name, &total))
Apr 22, 2008
Apr 22, 2008
178
break;
179
180
181
} // while
closedir(dirp);
} // if
Apr 30, 2008
Apr 30, 2008
182
#endif
183
184
185
186
187
188
189
190
return total;
} // do_dir
int main(int argc, char **argv)
{
//printf("MojoShader finderrors\n");
Mar 24, 2010
Mar 24, 2010
191
192
//printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
//printf("Linked against changeset %s\n", MOJOSHADER_changeset());
193
194
195
196
197
198
199
//printf("\n");
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [dir1] ... [dirN]\n\n", argv[0]);
else
{
int total = 0;
Apr 22, 2008
Apr 22, 2008
200
int i;
Apr 27, 2008
Apr 27, 2008
201
const char *profile = argv[1];
202
Apr 22, 2008
Apr 22, 2008
203
204
#if FINDERRORS_COMPILE_SHADERS
SDL_Init(SDL_INIT_VIDEO);
Apr 27, 2008
Apr 27, 2008
205
SDL_GL_LoadLibrary(NULL);
Apr 22, 2008
Apr 22, 2008
206
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
Oct 11, 2012
Oct 11, 2012
207
printf("Best profile is '%s'\n", MOJOSHADER_glBestProfile(lookup, 0, NULL, NULL, NULL));
Apr 28, 2008
Apr 28, 2008
208
MOJOSHADER_glContext *ctx;
Mar 31, 2010
Mar 31, 2010
209
ctx = MOJOSHADER_glCreateContext(profile, lookup, 0, 0, 0, 0);
Apr 28, 2008
Apr 28, 2008
210
if (ctx == NULL)
Apr 27, 2008
Apr 27, 2008
211
{
Apr 28, 2008
Apr 28, 2008
212
printf("MOJOSHADER_glCreateContext() fail: %s\n", MOJOSHADER_glGetError());
Apr 27, 2008
Apr 27, 2008
213
214
215
SDL_Quit();
return 1;
} // if
Apr 28, 2008
Apr 28, 2008
216
MOJOSHADER_glMakeContextCurrent(ctx);
Apr 22, 2008
Apr 22, 2008
217
218
#endif
219
for (i = 2; i < argc; i++)
Apr 22, 2008
Apr 22, 2008
220
total += do_dir(argv[i], profile);
221
Dec 12, 2008
Dec 12, 2008
222
printf("Saw %d files.\n", total);
223
Apr 22, 2008
Apr 22, 2008
224
#if FINDERRORS_COMPILE_SHADERS
Apr 28, 2008
Apr 28, 2008
225
MOJOSHADER_glDestroyContext(ctx);
Apr 22, 2008
Apr 22, 2008
226
227
SDL_Quit();
#endif
228
229
230
231
232
233
} // else
return 0;
} // main
// end of finderrors.c ...