/
finderrors.c
190 lines (162 loc) · 4.73 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
21
22
#if FINDERRORS_COMPILE_SHADERS
#include "SDL.h"
#endif
23
#ifdef _MSC_VER
24
25
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
26
#include <malloc.h> // for alloca().
27
#define snprintf _snprintf
28
29
#else
#include <dirent.h>
30
31
#endif
32
#define report printf
33
34
35
36
37
38
39
40
41
42
43
44
45
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
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
if (strstr(fn, ".bytecode") == NULL)
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];
int rc = fread(buf, 1, sizeof (buf), io);
fclose(io);
if (rc == -1)
{
report("FAIL: %s %s\n", fname, strerror(errno));
return 1;
} // if
#if FINDERRORS_COMPILE_SHADERS
MOJOSHADER_glShader *shader = MOJOSHADER_glCompileShader(buf, rc);
if (shader == NULL)
report("FAIL: %s %s\n", fname, MOJOSHADER_glGetError());
else
82
83
84
85
86
87
88
89
90
91
92
93
94
95
{
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);
}
96
97
98
99
100
101
102
103
104
105
106
107
108
#else
const MOJOSHADER_parseData *pd = MOJOSHADER_parse(profile, buf, rc, 0, 0, 0);
if (pd->error != NULL)
report("FAIL: %s %s\n", fname, pd->error);
else
report("PASS: %s\n", fname);
MOJOSHADER_freeParseData(pd);
#endif
return 1;
} // do_file
109
static int do_dir(const char *dname, const char *profile)
110
111
{
int total = 0;
112
113
114
115
#ifdef _MSC_VER
WIN32_FIND_DATA dent;
HANDLE dirp = INVALID_HANDLE_VALUE;
116
FindFirstFileA(dname, &dent);
117
118
119
120
121
122
if (dirp != INVALID_HANDLE_VALUE)
{
do
{
if (!do_file(profile, dname, dent.cFileName, &total))
break;
123
} while (FindNextFileA(dirp, &dent) != 0);
124
125
126
127
CloseHandle(dirp);
} // if
#else
struct dirent *dent = NULL;
128
129
130
131
132
DIR *dirp = opendir(dname);
if (dirp != NULL)
{
while ((dent = readdir(dirp)) != NULL)
{
133
if (!do_file(profile, dname, dent->d_name, &total))
134
break;
135
136
137
} // while
closedir(dirp);
} // if
138
#endif
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
{
int total = 0;
156
int i;
157
const char *profile = argv[1];
158
159
160
#if FINDERRORS_COMPILE_SHADERS
SDL_Init(SDL_INIT_VIDEO);
161
SDL_GL_LoadLibrary(NULL);
162
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL);
163
printf("Best profile is '%s'\n", MOJOSHADER_glBestProfile(SDL_GL_GetProcAddress));
164
MOJOSHADER_glContext *ctx;
165
ctx = MOJOSHADER_glCreateContext(profile, SDL_GL_GetProcAddress, 0, 0, 0);
166
if (ctx == NULL)
167
{
168
printf("MOJOSHADER_glCreateContext() fail: %s\n", MOJOSHADER_glGetError());
169
170
171
SDL_Quit();
return 1;
} // if
172
MOJOSHADER_glMakeContextCurrent(ctx);
173
174
#endif
175
for (i = 2; i < argc; i++)
176
total += do_dir(argv[i], profile);
177
178
179
printf("Saw %d bytecode files.\n", total);
180
#if FINDERRORS_COMPILE_SHADERS
181
MOJOSHADER_glDestroyContext(ctx);
182
183
SDL_Quit();
#endif
184
185
186
187
188
189
} // else
return 0;
} // main
// end of finderrors.c ...