Skip to content

Latest commit

 

History

History
269 lines (232 loc) · 6.75 KB

mojoshader-compiler.c

File metadata and controls

269 lines (232 loc) · 6.75 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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#if MOJOSHADER_DEBUG_MALLOC
static void *Malloc(int len)
{
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
static void Free(void *_ptr)
{
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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
96
97
static void close_include(const char *data, MOJOSHADER_malloc m,
MOJOSHADER_free f, void *d)
Feb 19, 2009
Feb 19, 2009
98
99
100
101
102
{
f((void *) data, d);
} // close_include
Feb 12, 2009
Feb 12, 2009
103
static int preprocess(const char *fname, const char *buf, int len,
Feb 19, 2009
Feb 19, 2009
104
105
106
const char *outfile,
const MOJOSHADER_preprocessorDefine *defs,
unsigned int defcount)
Feb 19, 2009
Feb 19, 2009
108
FILE *io = outfile ? fopen(outfile, "wb") : stdout;
109
110
111
112
113
114
115
116
117
if (io == NULL)
{
printf(" ... fopen('%s') failed.\n", outfile);
return 0;
} // if
const MOJOSHADER_preprocessData *pd;
int retval = 0;
Feb 19, 2009
Feb 19, 2009
118
119
pd = MOJOSHADER_preprocess(fname, buf, len, defs, defcount, open_include,
close_include, Malloc, Free, NULL);
120
121
122
123
124
125
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
Feb 12, 2009
Feb 12, 2009
126
127
printf("%s:%d: ERROR: %s\n",
pd->errors[i].filename ? pd->errors[i].filename : "???",
128
129
130
131
132
133
134
135
136
137
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
138
else if ((outfile != NULL) && (fclose(io) == EOF))
139
140
141
142
143
144
145
146
printf(" ... fclose('%s') failed.\n", outfile);
else
retval = 1;
} // if
} // else
MOJOSHADER_freePreprocessData(pd);
return retval;
Feb 10, 2009
Feb 10, 2009
147
} // preprocess
148
149
150
151
152
int main(int argc, char **argv)
{
int retval = 1;
Feb 19, 2009
Feb 19, 2009
153
154
155
const char *infile = NULL;
const char *outfile = NULL;
int i;
Feb 19, 2009
Feb 19, 2009
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
MOJOSHADER_preprocessorDefine *defs = NULL;
unsigned int defcount = 0;
for (i = 1; i < argc; i++)
{
const char *arg = argv[i];
if (strcmp(arg, "-o") == 0)
{
if (outfile != NULL)
{
printf("multiple output files specified.\n");
exit(1);
} // if
arg = argv[++i];
if (arg == NULL)
{
printf("no filename after '-o'\n");
exit(1);
} // if
outfile = arg;
} // if
Feb 19, 2009
Feb 19, 2009
181
182
183
184
185
186
187
188
189
190
191
192
193
194
if (strcmp(arg, "-I") == 0)
{
arg = argv[++i];
if (arg == NULL)
{
printf("no path after '-I'\n");
exit(1);
} // if
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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)
{
printf("multiple input files specified.\n");
exit(1);
} // if
infile = arg;
} // else
} // for
if (infile == NULL)
{
printf("no input file specified.\n");
exit(1);
} // if
FILE *io = fopen(infile, "rb");
if (io == NULL)
printf(" ... fopen('%s') failed.\n", infile);
234
235
else
{
Feb 19, 2009
Feb 19, 2009
236
237
238
239
240
241
242
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);
Feb 19, 2009
Feb 19, 2009
243
244
245
fclose(io);
if (rc == EOF)
printf(" ... fread('%s') failed.\n", infile);
246
247
else
{
Feb 19, 2009
Feb 19, 2009
248
249
if (preprocess(infile, buf, rc, outfile, defs, defcount))
retval = 0;
250
251
else
{
Feb 19, 2009
Feb 19, 2009
252
if (outfile != NULL)
253
254
remove(outfile);
} // else
Feb 19, 2009
Feb 19, 2009
255
256
free(buf);
} // else
257
258
} // else
Feb 19, 2009
Feb 19, 2009
259
for (i = 0; i < defcount; i++)
Feb 19, 2009
Feb 19, 2009
260
free((void *) defs[i].identifier);
Feb 19, 2009
Feb 19, 2009
261
262
free(defs);
Feb 19, 2009
Feb 19, 2009
263
264
free(include_paths);
265
266
267
return retval;
} // main
Feb 10, 2009
Feb 10, 2009
268
// end of preprocess.c ...