Skip to content

Latest commit

 

History

History
533 lines (467 loc) · 16.5 KB

testparse.c

File metadata and controls

533 lines (467 loc) · 16.5 KB
 
Apr 29, 2008
Apr 29, 2008
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.
*/
Feb 10, 2008
Feb 10, 2008
10
#include <stdio.h>
Feb 12, 2008
Feb 12, 2008
11
#include <stdlib.h>
Mar 22, 2008
Mar 22, 2008
12
#include "mojoshader.h"
Feb 10, 2008
Feb 10, 2008
13
Apr 30, 2008
Apr 30, 2008
14
15
16
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
Mar 28, 2008
Mar 28, 2008
17
Mar 28, 2008
Mar 28, 2008
18
#if MOJOSHADER_DEBUG_MALLOC
Mar 28, 2008
Mar 28, 2008
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)
{
Mar 28, 2008
Mar 28, 2008
32
33
int *ptr = (((int *) _ptr) - 1);
int len = *ptr;
Mar 28, 2008
Mar 28, 2008
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
May 22, 2011
May 22, 2011
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)
Mar 28, 2008
Mar 28, 2008
52
static const char *shader_type(const MOJOSHADER_shaderType s)
Mar 28, 2008
Mar 28, 2008
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";
Mar 28, 2008
Mar 28, 2008
60
default: return "(bogus value?)";
Mar 28, 2008
Mar 28, 2008
61
62
} // switch
Mar 28, 2008
Mar 28, 2008
63
return NULL; // shouldn't hit this.
Mar 28, 2008
Mar 28, 2008
64
65
66
} // shader_type
May 29, 2011
May 29, 2011
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
106
107
108
109
static void print_typeinfo(const MOJOSHADER_symbolTypeInfo *info,
unsigned int indent)
{
static const char *symclasses[] = {
"scalar", "vector", "row-major matrix",
"column-major matrix", "object", "struct"
};
static const char *symtypes[] = {
"void", "bool", "int", "float", "string", "texture",
"texture1d", "texture2d", "texture3d", "texturecube",
"sampler", "sampler1d", "sampler2d", "sampler3d",
"samplercube", "pixelshader", "vertexshader", "unsupported"
};
INDENT();
printf(" symbol class %s\n", symclasses[info->parameter_class]);
INDENT();
printf(" symbol type %s\n", symtypes[info->parameter_type]);
INDENT();
printf(" rows %u\n", info->rows);
INDENT();
printf(" columns %u\n", info->columns);
INDENT();
printf(" elements %u\n", info->elements);
if (info->member_count > 0)
{
int i;
INDENT(); printf(" MEMBERS:\n");
for (i = 0; i < info->member_count; i++)
{
const MOJOSHADER_symbolStructMember *member = &info->members[i];
INDENT(); printf(" MEMBERS:\n");
indent++;
INDENT(); printf(" * %d: \"%s\"\n", i, member->name);
print_typeinfo(&member->info, indent);
indent--;
} // for
} // if
} // print_typeinfo
Jun 1, 2011
Jun 1, 2011
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
static void print_symbols(const MOJOSHADER_symbol *sym,
const unsigned int symbol_count,
const unsigned int indent)
{
INDENT(); printf("SYMBOLS:");
if (symbol_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < symbol_count; i++, sym++)
{
static const char *regsets[] = {
"bool", "int4", "float4", "sampler"
};
INDENT(); printf(" * %d: \"%s\"\n", i, sym->name);
INDENT(); printf(" register set %s\n", regsets[sym->register_set]);
INDENT(); printf(" register index %u\n", sym->register_index);
INDENT(); printf(" register count %u\n", sym->register_count);
print_typeinfo(&sym->info, indent);
} // for
printf("\n");
} // else
} // print_symbols
Jun 1, 2011
Jun 1, 2011
138
139
140
141
142
143
144
145
static void print_preshader_operand(const MOJOSHADER_preshader *preshader,
const int instidx, const int opidx)
{
static char mask[] = { 'x', 'y', 'z', 'w' };
const MOJOSHADER_preshaderInstruction *inst = &preshader->instructions[instidx];
const MOJOSHADER_preshaderOperand *operand = &inst->operands[opidx];
const int elems = inst->element_count;
const int isscalarop = (inst->opcode >= MOJOSHADER_PRESHADEROP_SCALAR_OPS);
Jun 2, 2011
Jun 2, 2011
146
const int isscalar = ((isscalarop) && (opidx == 0)); // probably wrong.
Jun 1, 2011
Jun 1, 2011
147
148
149
150
151
152
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
193
194
195
196
197
198
int i;
switch (operand->type)
{
case MOJOSHADER_PRESHADEROPERAND_LITERAL:
{
const double *lit = &preshader->literals[operand->index];
printf("(");
if (isscalar)
{
const double val = *lit;
for (i = 0; i < elems-1; i++)
printf("%g, ", val);
printf("%g)", val);
} // if
else
{
for (i = 0; i < elems-1; i++, lit++)
printf("%g, ", *lit);
printf("%g)", *lit);
} // else
break;
} // case
case MOJOSHADER_PRESHADEROPERAND_INPUT:
case MOJOSHADER_PRESHADEROPERAND_OUTPUT:
case MOJOSHADER_PRESHADEROPERAND_TEMP:
{
int idx = operand->index % 4;
char regch = 'c';
if (operand->type == MOJOSHADER_PRESHADEROPERAND_TEMP)
regch = 'r';
printf("%c%d", regch, operand->index / 4);
if (isscalar)
printf(".%c", mask[idx]);
else if (elems != 4)
{
printf(".");
for (i = 0; i < elems; i++)
printf("%c", mask[idx++]);
} // else if
break;
} // case
default:
printf("[???{%d, %u}???]", (int) operand->type, operand->index);
break;
} // switch
} // print_preshader_operand
May 30, 2011
May 30, 2011
199
200
201
202
static void print_preshader(const MOJOSHADER_preshader *preshader,
const int indent)
{
MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
Jun 1, 2011
Jun 1, 2011
203
int i, j;
May 30, 2011
May 30, 2011
204
205
static const char *opcodestr[] = {
May 30, 2011
May 30, 2011
206
"nop", "mov", "neg", "rcp", "frc", "exp", "log", "rsq", "sin", "cos",
May 31, 2011
May 31, 2011
207
208
209
"asin", "acos", "atan", "min", "max", "lt", "ge", "add", "mul",
"atan2", "div", "cmp", "movc", "dot", "noise", "min", "max", "lt",
"ge", "add", "mul", "atan2", "div", "dot", "noise"
May 30, 2011
May 30, 2011
210
211
212
};
INDENT(); printf("PRESHADER:\n");
Jun 1, 2011
Jun 1, 2011
213
214
215
print_symbols(preshader->symbols, preshader->symbol_count, indent + 1);
May 30, 2011
May 30, 2011
216
217
for (i = 0; i < preshader->instruction_count; i++, inst++)
{
Jun 1, 2011
Jun 1, 2011
218
INDENT(); printf(" %s ", opcodestr[inst->opcode]);
May 30, 2011
May 30, 2011
219
Jun 1, 2011
Jun 1, 2011
220
221
// print dest register first...
print_preshader_operand(preshader, i, inst->operand_count - 1);
May 30, 2011
May 30, 2011
222
Jun 1, 2011
Jun 1, 2011
223
224
// ...then the source registers.
for (j = 0; j < inst->operand_count - 1; j++)
May 30, 2011
May 30, 2011
225
{
Jun 1, 2011
Jun 1, 2011
226
227
printf(", ");
print_preshader_operand(preshader, i, j);
May 30, 2011
May 30, 2011
228
229
230
231
232
233
234
235
236
} // for
printf("\n");
} // for
printf("\n");
} // print_preshader
May 22, 2011
May 22, 2011
237
238
static void print_shader(const char *fname, const MOJOSHADER_parseData *pd,
unsigned int indent)
Mar 28, 2008
Mar 28, 2008
239
{
May 22, 2011
May 22, 2011
240
INDENT(); printf("PROFILE: %s\n", pd->profile);
Feb 3, 2009
Feb 3, 2009
241
242
243
244
245
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
May 22, 2011
May 22, 2011
246
247
const MOJOSHADER_error *err = &pd->errors[i];
INDENT();
Feb 12, 2009
Feb 12, 2009
248
printf("%s:%d: ERROR: %s\n",
May 22, 2011
May 22, 2011
249
250
err->filename ? err->filename : fname,
err->error_position, err->error);
Feb 3, 2009
Feb 3, 2009
251
252
} // for
} // if
Mar 28, 2008
Mar 28, 2008
253
254
else
{
May 22, 2011
May 22, 2011
255
256
257
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);
Apr 5, 2008
Apr 5, 2008
258
May 22, 2011
May 22, 2011
259
INDENT(); printf("ATTRIBUTES:");
Apr 5, 2008
Apr 5, 2008
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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);
May 22, 2011
May 22, 2011
277
INDENT();
Apr 25, 2008
Apr 25, 2008
278
279
280
281
printf(" * %s%s", usagenames[(int) a->usage], numstr);
if (a->name != NULL)
printf(" (\"%s\")", a->name);
printf("\n");
Apr 5, 2008
Apr 5, 2008
282
283
284
} // for
} // else
May 22, 2011
May 22, 2011
285
INDENT(); printf("CONSTANTS:");
May 3, 2008
May 3, 2008
286
287
288
289
290
291
292
293
294
295
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];
May 22, 2011
May 22, 2011
296
INDENT();
May 3, 2008
May 3, 2008
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
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
May 22, 2011
May 22, 2011
320
INDENT(); printf("UNIFORMS:");
Apr 4, 2008
Apr 4, 2008
321
322
323
324
325
326
327
328
if (pd->uniform_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->uniform_count; i++)
{
Apr 4, 2008
Apr 4, 2008
329
static const char *typenames[] = { "float", "int", "bool" };
Apr 4, 2008
Apr 4, 2008
330
const MOJOSHADER_uniform *u = &pd->uniforms[i];
May 5, 2008
May 5, 2008
331
const char *arrayof = "";
Jul 31, 2008
Jul 31, 2008
332
const char *constant = u->constant ? "const " : "";
May 5, 2008
May 5, 2008
333
334
335
336
337
338
339
340
char arrayrange[64] = { '\0' };
if (u->array_count > 0)
{
arrayof = "array[";
snprintf(arrayrange, sizeof (arrayrange), "%d] ",
u->array_count);
} // if
May 22, 2011
May 22, 2011
341
INDENT();
Jul 31, 2008
Jul 31, 2008
342
343
printf(" * %d: %s%s%s%s", u->index, constant, arrayof,
arrayrange, typenames[(int) u->type]);
Apr 25, 2008
Apr 25, 2008
344
345
346
if (u->name != NULL)
printf(" (\"%s\")", u->name);
printf("\n");
Apr 4, 2008
Apr 4, 2008
347
348
349
} // for
} // else
May 22, 2011
May 22, 2011
350
INDENT(); printf("SAMPLERS:");
Apr 19, 2008
Apr 19, 2008
351
352
353
354
355
356
357
358
359
360
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];
May 22, 2011
May 22, 2011
361
INDENT();
Apr 25, 2008
Apr 25, 2008
362
363
364
365
printf(" * %d: %s", s->index, typenames[(int) s->type]);
if (s->name != NULL)
printf(" (\"%s\")", s->name);
printf("\n");
Apr 19, 2008
Apr 19, 2008
366
367
368
} // for
} // else
Jun 1, 2011
Jun 1, 2011
369
print_symbols(pd->symbols, pd->symbol_count, indent);
May 29, 2011
May 29, 2011
370
May 30, 2011
May 30, 2011
371
372
373
if (pd->preshader != NULL)
print_preshader(pd->preshader, indent);
Mar 28, 2008
Mar 28, 2008
374
if (pd->output != NULL)
Apr 6, 2008
Apr 6, 2008
375
376
{
int i;
May 22, 2011
May 22, 2011
377
INDENT();
Apr 6, 2008
Apr 6, 2008
378
printf("OUTPUT:\n");
May 22, 2011
May 22, 2011
379
380
indent++;
INDENT();
Apr 6, 2008
Apr 6, 2008
381
for (i = 0; i < pd->output_len; i++)
May 22, 2011
May 22, 2011
382
{
Apr 6, 2008
Apr 6, 2008
383
putchar((int) pd->output[i]);
May 22, 2011
May 22, 2011
384
385
386
if (pd->output[i] == '\n')
INDENT();
} // for
Apr 6, 2008
Apr 6, 2008
387
printf("\n");
May 22, 2011
May 22, 2011
388
indent--;
Apr 6, 2008
Apr 6, 2008
389
} // if
Mar 28, 2008
Mar 28, 2008
390
} // else
May 30, 2011
May 30, 2011
391
Mar 28, 2008
Mar 28, 2008
392
printf("\n\n");
May 22, 2011
May 22, 2011
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
} // 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;
May 31, 2011
May 31, 2011
419
420
421
422
423
424
425
426
427
const MOJOSHADER_effectParam *param = effect->params;
for (i = 0; i < effect->param_count; i++, param++)
{
INDENT();
printf("PARAM #%d '%s' -> '%s'\n", i, param->name, param->semantic);
} // for
printf("\n");
May 22, 2011
May 22, 2011
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
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
Apr 19, 2008
Apr 19, 2008
491
492
return retval;
Mar 28, 2008
Mar 28, 2008
493
494
495
} // do_parse
Feb 10, 2008
Feb 10, 2008
496
497
int main(int argc, char **argv)
{
Apr 19, 2008
Apr 19, 2008
498
499
int retval = 0;
Mar 28, 2008
Mar 28, 2008
500
printf("MojoShader testparse\n");
Mar 24, 2010
Mar 24, 2010
501
502
printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
printf("Linked against changeset %s\n", MOJOSHADER_changeset());
Mar 28, 2008
Mar 28, 2008
503
printf("\n");
Mar 22, 2008
Mar 22, 2008
504
Mar 28, 2008
Mar 28, 2008
505
506
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
Mar 28, 2008
Mar 28, 2008
507
else
Feb 10, 2008
Feb 10, 2008
508
{
Mar 28, 2008
Mar 28, 2008
509
510
511
512
const char *profile = argv[1];
int i;
for (i = 2; i < argc; i++)
Feb 10, 2008
Feb 10, 2008
513
{
Mar 28, 2008
Mar 28, 2008
514
515
FILE *io = fopen(argv[i], "rb");
if (io == NULL)
Mar 28, 2008
Mar 28, 2008
516
printf(" ... fopen('%s') failed.\n", argv[i]);
Mar 28, 2008
Mar 28, 2008
517
518
519
520
521
else
{
unsigned char *buf = (unsigned char *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
Mar 24, 2010
Mar 24, 2010
522
if (!do_parse(argv[i], buf, rc, profile))
Apr 19, 2008
Apr 19, 2008
523
retval = 1;
Mar 28, 2008
Mar 28, 2008
524
525
526
527
free(buf);
} // else
} // for
} // else
Feb 10, 2008
Feb 10, 2008
528
Apr 19, 2008
Apr 19, 2008
529
return retval;
Feb 10, 2008
Feb 10, 2008
530
531
532
} // main
// end of testparse.c ...