Debian Codegraph @

utils/finderrors.c

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