/
testparse.c
348 lines (311 loc) · 10.6 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
#include <stdio.h>
11
#include <stdlib.h>
12
#include "mojoshader.h"
13
14
15
16
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
17
18
#if MOJOSHADER_DEBUG_MALLOC
19
20
21
22
23
24
25
26
27
28
29
30
31
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)
{
32
33
int *ptr = (((int *) _ptr) - 1);
int len = *ptr;
34
35
36
37
38
39
40
41
printf("free() %d bytes (%p)\n", len, ptr);
free(ptr);
} // Free
#else
#define Malloc NULL
#define Free NULL
#endif
42
43
44
45
46
47
48
49
50
51
static inline void do_indent(const unsigned int indent)
{
unsigned int i;
for (i = 0; i < indent; i++)
printf(" ");
}
#define INDENT() do { if (indent) { do_indent(indent); } } while (0)
52
static const char *shader_type(const MOJOSHADER_shaderType s)
53
54
55
56
57
58
59
{
switch (s)
{
case MOJOSHADER_TYPE_UNKNOWN: return "unknown";
case MOJOSHADER_TYPE_PIXEL: return "pixel";
case MOJOSHADER_TYPE_VERTEX: return "vertex";
case MOJOSHADER_TYPE_GEOMETRY: return "geometry";
60
default: return "(bogus value?)";
61
62
} // switch
63
return NULL; // shouldn't hit this.
64
65
66
} // shader_type
67
68
static void print_shader(const char *fname, const MOJOSHADER_parseData *pd,
unsigned int indent)
69
{
70
INDENT(); printf("PROFILE: %s\n", pd->profile);
71
72
73
74
75
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
76
77
const MOJOSHADER_error *err = &pd->errors[i];
INDENT();
78
printf("%s:%d: ERROR: %s\n",
79
80
err->filename ? err->filename : fname,
err->error_position, err->error);
81
82
} // for
} // if
83
84
else
{
85
86
87
INDENT(); printf("SHADER TYPE: %s\n", shader_type(pd->shader_type));
INDENT(); printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver);
INDENT(); printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count);
88
89
INDENT(); printf("ATTRIBUTES:");
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
if (pd->attribute_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->attribute_count; i++)
{
static const char *usagenames[] = {
"position", "blendweight", "blendindices", "normal",
"psize", "texcoord", "tangent", "binormal", "tessfactor",
"positiont", "color", "fog", "depth", "sample"
};
const MOJOSHADER_attribute *a = &pd->attributes[i];
char numstr[16] = { 0 };
if (a->index != 0)
snprintf(numstr, sizeof (numstr), "%d", a->index);
107
INDENT();
108
109
110
111
printf(" * %s%s", usagenames[(int) a->usage], numstr);
if (a->name != NULL)
printf(" (\"%s\")", a->name);
printf("\n");
112
113
114
} // for
} // else
115
INDENT(); printf("CONSTANTS:");
116
117
118
119
120
121
122
123
124
125
if (pd->constant_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->constant_count; i++)
{
static const char *typenames[] = { "float", "int", "bool" };
const MOJOSHADER_constant *c = &pd->constants[i];
126
INDENT();
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
printf(" * %d: %s (", c->index, typenames[(int) c->type]);
if (c->type == MOJOSHADER_UNIFORM_FLOAT)
{
printf("%f %f %f %f", c->value.f[0], c->value.f[1],
c->value.f[2], c->value.f[3]);
} // if
else if (c->type == MOJOSHADER_UNIFORM_INT)
{
printf("%d %d %d %d", c->value.i[0], c->value.i[1],
c->value.i[2], c->value.i[3]);
} // else if
else if (c->type == MOJOSHADER_UNIFORM_BOOL)
{
printf("%s", c->value.b ? "true" : "false");
} // else if
else
{
printf("???");
} // else
printf(")\n");
} // for
} // else
150
INDENT(); printf("UNIFORMS:");
151
152
153
154
155
156
157
158
if (pd->uniform_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->uniform_count; i++)
{
159
static const char *typenames[] = { "float", "int", "bool" };
160
const MOJOSHADER_uniform *u = &pd->uniforms[i];
161
const char *arrayof = "";
162
const char *constant = u->constant ? "const " : "";
163
164
165
166
167
168
169
170
char arrayrange[64] = { '\0' };
if (u->array_count > 0)
{
arrayof = "array[";
snprintf(arrayrange, sizeof (arrayrange), "%d] ",
u->array_count);
} // if
171
INDENT();
172
173
printf(" * %d: %s%s%s%s", u->index, constant, arrayof,
arrayrange, typenames[(int) u->type]);
174
175
176
if (u->name != NULL)
printf(" (\"%s\")", u->name);
printf("\n");
177
178
179
} // for
} // else
180
INDENT(); printf("SAMPLERS:");
181
182
183
184
185
186
187
188
189
190
if (pd->sampler_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->sampler_count; i++)
{
static const char *typenames[] = { "2d", "cube", "volume" };
const MOJOSHADER_sampler *s = &pd->samplers[i];
191
INDENT();
192
193
194
195
printf(" * %d: %s", s->index, typenames[(int) s->type]);
if (s->name != NULL)
printf(" (\"%s\")", s->name);
printf("\n");
196
197
198
} // for
} // else
199
if (pd->output != NULL)
200
201
{
int i;
202
INDENT();
203
printf("OUTPUT:\n");
204
205
indent++;
INDENT();
206
for (i = 0; i < pd->output_len; i++)
207
{
208
putchar((int) pd->output[i]);
209
210
211
if (pd->output[i] == '\n')
INDENT();
} // for
212
printf("\n");
213
indent--;
214
} // if
215
} // else
216
printf("\n\n");
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
} // print_shader
static void print_effect(const char *fname, const MOJOSHADER_effect *effect,
const unsigned int indent)
{
INDENT(); printf("PROFILE: %s\n", effect->profile);
printf("\n");
if (effect->error_count > 0)
{
int i;
for (i = 0; i < effect->error_count; i++)
{
const MOJOSHADER_error *err = &effect->errors[i];
INDENT();
printf("%s:%d: ERROR: %s\n",
err->filename ? err->filename : fname,
err->error_position, err->error);
} // for
} // if
else
{
int i, j, k;
const MOJOSHADER_effectTechnique *technique = effect->techniques;
const MOJOSHADER_effectTexture *texture = effect->textures;
const MOJOSHADER_effectShader *shader = effect->shaders;
for (i = 0; i < effect->technique_count; i++, technique++)
{
const MOJOSHADER_effectPass *pass = technique->passes;
INDENT(); printf("TECHNIQUE #%d ('%s'):\n", i, technique->name);
for (j = 0; j < technique->pass_count; j++, pass++)
{
const MOJOSHADER_effectState *state = pass->states;
INDENT(); printf(" PASS #%d ('%s'):\n", j, pass->name);
for (k = 0; k < pass->state_count; k++, state++)
{
INDENT(); printf(" STATE 0x%X\n", state->type);
} // for
} // for
printf("\n");
} // for
for (i = 0; i < effect->texture_count; i++, texture++)
{
INDENT();
printf("TEXTURE #%d ('%s'): %u\n", i,
texture->name, texture->param);
} // for
printf("\n");
for (i = 0; i < effect->shader_count; i++, shader++)
{
INDENT();
printf("SHADER #%d: technique %u, pass %u\n", i,
shader->technique, shader->pass);
print_shader(fname, shader->shader, indent + 1);
} // for
} // else
} // print_effect
static int do_parse(const char *fname, const unsigned char *buf,
const int len, const char *prof)
{
int retval = 0;
// magic for an effects file (!!! FIXME: I _think_).
if ( (buf[0] == 0x01) && (buf[1] == 0x09) &&
(buf[2] == 0xFF) && (buf[3] == 0xFE) )
{
const MOJOSHADER_effect *effect;
effect = MOJOSHADER_parseEffect(prof, buf, len, 0, 0, Malloc, Free, 0);
retval = (effect->error_count == 0);
printf("EFFECT: %s\n", fname);
print_effect(fname, effect, 1);
MOJOSHADER_freeEffect(effect);
} // if
else // do it as a regular compiled shader.
{
const MOJOSHADER_parseData *pd;
pd = MOJOSHADER_parse(prof, buf, len, NULL, 0, Malloc, Free, NULL);
retval = (pd->error_count == 0);
printf("SHADER: %s\n", fname);
print_shader(fname, pd, 1);
MOJOSHADER_freeParseData(pd);
} // else
306
307
return retval;
308
309
310
} // do_parse
311
312
int main(int argc, char **argv)
{
313
314
int retval = 0;
315
printf("MojoShader testparse\n");
316
317
printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
printf("Linked against changeset %s\n", MOJOSHADER_changeset());
318
printf("\n");
319
320
321
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
322
else
323
{
324
325
326
327
const char *profile = argv[1];
int i;
for (i = 2; i < argc; i++)
328
{
329
330
FILE *io = fopen(argv[i], "rb");
if (io == NULL)
331
printf(" ... fopen('%s') failed.\n", argv[i]);
332
333
334
335
336
else
{
unsigned char *buf = (unsigned char *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
337
if (!do_parse(argv[i], buf, rc, profile))
338
retval = 1;
339
340
341
342
free(buf);
} // else
} // for
} // else
343
344
return retval;
345
346
347
} // main
// end of testparse.c ...