Skip to content

Latest commit

 

History

History
362 lines (301 loc) · 9.83 KB

mojoshader-compiler.c

File metadata and controls

362 lines (301 loc) · 9.83 KB
 
1
2
3
4
5
6
7
8
9
10
11
/**
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
Feb 19, 2009
Feb 19, 2009
12
#include <string.h>
13
14
#include "mojoshader.h"
Feb 19, 2009
Feb 19, 2009
15
16
17
static const char **include_paths = NULL;
static unsigned int include_path_count = 0;
Feb 19, 2009
Feb 19, 2009
18
#define MOJOSHADER_DEBUG_MALLOC 0
Feb 19, 2009
Feb 19, 2009
19
Feb 19, 2009
Feb 19, 2009
20
#if MOJOSHADER_DEBUG_MALLOC
Feb 19, 2009
Feb 19, 2009
21
static void *Malloc(int len, void *d)
Feb 19, 2009
Feb 19, 2009
22
23
24
25
26
27
28
29
30
31
{
void *ptr = malloc(len + sizeof (int));
int *store = (int *) ptr;
printf("malloc() %d bytes (%p)\n", len, ptr);
if (ptr == NULL) return NULL;
*store = len;
return (void *) (store + 1);
} // Malloc
Feb 19, 2009
Feb 19, 2009
32
static void Free(void *_ptr, void *d)
Feb 19, 2009
Feb 19, 2009
33
34
35
36
37
38
39
40
41
42
{
int *ptr = (((int *) _ptr) - 1);
int len = *ptr;
printf("free() %d bytes (%p)\n", len, ptr);
free(ptr);
} // Free
#else
#define Malloc NULL
#define Free NULL
#endif
Feb 19, 2009
Feb 19, 2009
43
Feb 19, 2009
Feb 19, 2009
44
45
46
47
48
49
50
51
static void fail(const char *err)
{
printf("%s.\n", err);
exit(1);
} // fail
Feb 19, 2009
Feb 19, 2009
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
static int open_include(MOJOSHADER_includeType inctype, const char *fname,
const char *parent, const char **outdata,
unsigned int *outbytes, MOJOSHADER_malloc m,
MOJOSHADER_free f, void *d)
{
int i;
for (i = 0; i < include_path_count; i++)
{
const char *path = include_paths[i];
const size_t len = strlen(path) + strlen(fname) + 2;
char *buf = (char *) m(len, d);
if (buf == NULL)
return 0;
snprintf(buf, len, "%s/%s", path, fname);
FILE *io = fopen(buf, "rb");
f(buf, d);
if (io == NULL)
continue;
if (fseek(io, 0, SEEK_END) != -1)
{
const long fsize = ftell(io);
if ((fsize == -1) || (fseek(io, 0, SEEK_SET) == -1))
{
fclose(io);
return 0;
} // if
char *data = (char *) m(fsize, d);
if (data == NULL)
{
fclose(io);
return 0;
} // if
if (fread(data, fsize, 1, io) != 1)
{
f(data, d);
fclose(io);
return 0;
} // if
fclose(io);
*outdata = data;
*outbytes = (unsigned int) fsize;
return 1;
} // if
} // for
return 0;
} // open_include
Feb 19, 2009
Feb 19, 2009
106
107
static void close_include(const char *data, MOJOSHADER_malloc m,
MOJOSHADER_free f, void *d)
Feb 19, 2009
Feb 19, 2009
108
109
110
111
112
{
f((void *) data, d);
} // close_include
Feb 12, 2009
Feb 12, 2009
113
static int preprocess(const char *fname, const char *buf, int len,
Feb 19, 2009
Feb 19, 2009
114
115
const char *outfile,
const MOJOSHADER_preprocessorDefine *defs,
Feb 19, 2009
Feb 19, 2009
116
unsigned int defcount, FILE *io)
117
118
119
120
{
const MOJOSHADER_preprocessData *pd;
int retval = 0;
Feb 19, 2009
Feb 19, 2009
121
122
pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, open_include,
close_include, Malloc, Free, NULL);
123
124
125
126
127
128
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
Feb 12, 2009
Feb 12, 2009
129
130
printf("%s:%d: ERROR: %s\n",
pd->errors[i].filename ? pd->errors[i].filename : "???",
131
132
133
134
135
136
137
138
139
140
pd->errors[i].error_position,
pd->errors[i].error);
} // for
} // if
else
{
if (pd->output != NULL)
{
if (fwrite(pd->output, pd->output_len, 1, io) != 1)
printf(" ... fwrite('%s') failed.\n", outfile);
Feb 19, 2009
Feb 19, 2009
141
else if ((outfile != NULL) && (fclose(io) == EOF))
142
143
144
145
146
147
148
149
printf(" ... fclose('%s') failed.\n", outfile);
else
retval = 1;
} // if
} // else
MOJOSHADER_freePreprocessData(pd);
return retval;
Feb 10, 2009
Feb 10, 2009
150
} // preprocess
Feb 19, 2009
Feb 19, 2009
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
static int assemble(const char *fname, const char *buf, int len,
const char *outfile,
const MOJOSHADER_preprocessorDefine *defs,
unsigned int defcount, FILE *io)
{
const MOJOSHADER_parseData *pd;
int retval = 0;
pd = MOJOSHADER_assemble(fname, buf, len, NULL, 0, NULL, 0,
defs, defcount, open_include, close_include,
Malloc, Free, NULL);
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
printf("%s:%d: ERROR: %s\n",
pd->errors[i].filename ? pd->errors[i].filename : "???",
pd->errors[i].error_position,
pd->errors[i].error);
} // for
} // if
else
{
if (pd->output != NULL)
{
if (fwrite(pd->output, pd->output_len, 1, io) != 1)
printf(" ... fwrite('%s') failed.\n", outfile);
else if ((outfile != NULL) && (fclose(io) == EOF))
printf(" ... fclose('%s') failed.\n", outfile);
else
retval = 1;
} // if
} // else
MOJOSHADER_freeParseData(pd);
return retval;
} // assemble
Feb 28, 2009
Feb 28, 2009
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
void MOJOSHADER_compile(const char *filename,
const char *source, unsigned int sourcelen,
const MOJOSHADER_preprocessorDefine *defines,
unsigned int define_count,
MOJOSHADER_includeOpen include_open,
MOJOSHADER_includeClose include_close,
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d);
static int compile(const char *fname, const char *buf, int len,
const char *outfile,
const MOJOSHADER_preprocessorDefine *defs,
unsigned int defcount, FILE *io)
{
const MOJOSHADER_parseData *pd;
int retval = 0;
MOJOSHADER_compile(fname, buf, len, defs, defcount,
open_include, close_include,
Malloc, Free, NULL);
return 1;
} // compile
Feb 19, 2009
Feb 19, 2009
214
215
216
217
218
219
typedef enum
{
ACTION_UNKNOWN,
ACTION_PREPROCESS,
ACTION_ASSEMBLE,
Feb 28, 2009
Feb 28, 2009
220
ACTION_COMPILE,
Feb 19, 2009
Feb 19, 2009
221
222
223
} Action;
224
225
int main(int argc, char **argv)
{
Feb 19, 2009
Feb 19, 2009
226
Action action = ACTION_UNKNOWN;
227
int retval = 1;
Feb 19, 2009
Feb 19, 2009
228
229
230
const char *infile = NULL;
const char *outfile = NULL;
int i;
Feb 19, 2009
Feb 19, 2009
232
233
234
MOJOSHADER_preprocessorDefine *defs = NULL;
unsigned int defcount = 0;
Feb 23, 2009
Feb 23, 2009
235
236
237
238
include_paths = (const char **) malloc(sizeof (char *));
include_paths[0] = ".";
include_path_count = 1;
Feb 19, 2009
Feb 19, 2009
239
240
241
242
for (i = 1; i < argc; i++)
{
const char *arg = argv[i];
Feb 19, 2009
Feb 19, 2009
243
244
if (strcmp(arg, "-P") == 0)
{
Feb 24, 2009
Feb 24, 2009
245
if ((action != ACTION_UNKNOWN) && (action != ACTION_PREPROCESS))
Feb 19, 2009
Feb 19, 2009
246
247
248
249
250
251
fail("Multiple actions specified");
action = ACTION_PREPROCESS;
} // if
else if (strcmp(arg, "-A") == 0)
{
Feb 24, 2009
Feb 24, 2009
252
if ((action != ACTION_UNKNOWN) && (action != ACTION_ASSEMBLE))
Feb 19, 2009
Feb 19, 2009
253
254
255
256
fail("Multiple actions specified");
action = ACTION_ASSEMBLE;
} // else if
Feb 28, 2009
Feb 28, 2009
257
258
259
260
261
262
263
else if (strcmp(arg, "-C") == 0)
{
if ((action != ACTION_UNKNOWN) && (action != ACTION_COMPILE))
fail("Multiple actions specified");
action = ACTION_COMPILE;
} // else if
Feb 19, 2009
Feb 19, 2009
264
else if (strcmp(arg, "-o") == 0)
Feb 19, 2009
Feb 19, 2009
265
266
{
if (outfile != NULL)
Feb 19, 2009
Feb 19, 2009
267
fail("multiple output files specified");
Feb 19, 2009
Feb 19, 2009
268
269
270
arg = argv[++i];
if (arg == NULL)
Feb 19, 2009
Feb 19, 2009
271
fail("no filename after '-o'");
Feb 19, 2009
Feb 19, 2009
272
273
274
outfile = arg;
} // if
Feb 19, 2009
Feb 19, 2009
275
else if (strcmp(arg, "-I") == 0)
Feb 19, 2009
Feb 19, 2009
276
277
278
{
arg = argv[++i];
if (arg == NULL)
Feb 19, 2009
Feb 19, 2009
279
280
fail("no path after '-I'");
Feb 19, 2009
Feb 19, 2009
281
282
283
284
285
286
include_paths = (const char **) realloc(include_paths,
(include_path_count+1) * sizeof (char *));
include_paths[include_path_count] = arg;
include_path_count++;
} // if
Feb 19, 2009
Feb 19, 2009
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
else if (strncmp(arg, "-D", 2) == 0)
{
arg += 2;
char *ident = strdup(arg);
char *ptr = strchr(ident, '=');
const char *val = "";
if (ptr)
{
*ptr = '\0';
val = ptr+1;
} // if
defs = (MOJOSHADER_preprocessorDefine *) realloc(defs,
(defcount+1) * sizeof (MOJOSHADER_preprocessorDefine));
defs[defcount].identifier = ident;
defs[defcount].definition = val;
defcount++;
} // else if
else
{
if (infile != NULL)
Feb 19, 2009
Feb 19, 2009
309
fail("multiple input files specified");
Feb 19, 2009
Feb 19, 2009
310
311
312
313
infile = arg;
} // else
} // for
Feb 19, 2009
Feb 19, 2009
314
315
316
if (action == ACTION_UNKNOWN)
action = ACTION_ASSEMBLE;
Feb 19, 2009
Feb 19, 2009
317
if (infile == NULL)
Feb 19, 2009
Feb 19, 2009
318
fail("no input file specified");
Feb 19, 2009
Feb 19, 2009
319
320
321
FILE *io = fopen(infile, "rb");
if (io == NULL)
Feb 19, 2009
Feb 19, 2009
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
fail("failed to open input file");
fseek(io, 0, SEEK_END);
long fsize = ftell(io);
fseek(io, 0, SEEK_SET);
if (fsize == -1)
fsize = 1000000;
char *buf = (char *) malloc(fsize);
const int rc = fread(buf, 1, fsize, io);
fclose(io);
if (rc == EOF)
fail("failed to read input file");
FILE *outio = outfile ? fopen(outfile, "wb") : stdout;
if (outio == NULL)
fail("failed to open output file");
if (action == ACTION_PREPROCESS)
retval = (!preprocess(infile, buf, rc, outfile, defs, defcount, outio));
else if (action == ACTION_ASSEMBLE)
retval = (!assemble(infile, buf, rc, outfile, defs, defcount, outio));
Feb 28, 2009
Feb 28, 2009
344
345
else if (action == ACTION_COMPILE)
retval = (!compile(infile, buf, rc, outfile, defs, defcount, outio));
Feb 19, 2009
Feb 19, 2009
346
347
348
349
350
if ((retval != 0) && (outfile != NULL))
remove(outfile);
free(buf);
Feb 19, 2009
Feb 19, 2009
352
for (i = 0; i < defcount; i++)
Feb 19, 2009
Feb 19, 2009
353
free((void *) defs[i].identifier);
Feb 19, 2009
Feb 19, 2009
354
355
free(defs);
Feb 19, 2009
Feb 19, 2009
356
357
free(include_paths);
358
359
360
return retval;
} // main
Feb 19, 2009
Feb 19, 2009
361
// end of mojoshader-compiler.c ...