Navigation Menu

Skip to content

Latest commit

 

History

History
873 lines (781 loc) · 27.7 KB

testparse.c

File metadata and controls

873 lines (781 loc) · 27.7 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>
Dec 31, 2019
Dec 31, 2019
12
13
14
15
16
17
18
19
#include <string.h>
#include <assert.h>
#include "../mojoshader.h"
#define __MOJOSHADER_INTERNAL__ 1
#include "../mojoshader_internal.h"
#ifdef MOJOSHADER_HAS_SPIRV_TOOLS
#include "spirv-tools/libspirv.h"
#endif
Feb 10, 2008
Feb 10, 2008
20
Apr 30, 2008
Apr 30, 2008
21
22
23
#ifdef _MSC_VER
#define snprintf _snprintf
#endif
Mar 28, 2008
Mar 28, 2008
24
Mar 28, 2008
Mar 28, 2008
25
#if MOJOSHADER_DEBUG_MALLOC
Mar 28, 2008
Mar 28, 2008
26
27
28
29
30
31
32
33
34
35
36
37
38
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
39
40
int *ptr = (((int *) _ptr) - 1);
int len = *ptr;
Mar 28, 2008
Mar 28, 2008
41
42
43
44
45
46
47
48
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
49
50
51
52
53
54
55
56
57
58
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
59
static const char *shader_type(const MOJOSHADER_shaderType s)
Mar 28, 2008
Mar 28, 2008
60
61
62
63
64
65
66
{
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
67
default: return "(bogus value?)";
Mar 28, 2008
Mar 28, 2008
68
69
} // switch
Mar 28, 2008
Mar 28, 2008
70
return NULL; // shouldn't hit this.
Mar 28, 2008
Mar 28, 2008
71
72
73
} // shader_type
May 29, 2011
May 29, 2011
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
110
111
112
113
114
115
116
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
145
146
147
148
149
150
151
152
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
153
const int isscalar = ((isscalarop) && (opidx == 0)); // probably wrong.
Jun 1, 2011
Jun 1, 2011
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
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';
Jan 1, 2016
Jan 1, 2016
187
188
189
190
191
192
193
194
195
if (operand->array_register_count > 0)
{
for (i = operand->array_register_count - 1; i >= 0; i--)
printf("c%d[", operand->array_registers[i]);
printf("%c%d.%c", regch, operand->index / 4, mask[idx]);
for (i = 0; i < operand->array_register_count; i++)
printf("]");
break;
} // if
Jun 1, 2011
Jun 1, 2011
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
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
215
216
217
218
static void print_preshader(const MOJOSHADER_preshader *preshader,
const int indent)
{
MOJOSHADER_preshaderInstruction *inst = preshader->instructions;
Jun 1, 2011
Jun 1, 2011
219
int i, j;
May 30, 2011
May 30, 2011
220
221
static const char *opcodestr[] = {
May 30, 2011
May 30, 2011
222
"nop", "mov", "neg", "rcp", "frc", "exp", "log", "rsq", "sin", "cos",
May 31, 2011
May 31, 2011
223
224
225
"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
226
227
228
};
INDENT(); printf("PRESHADER:\n");
Jun 1, 2011
Jun 1, 2011
229
230
231
print_symbols(preshader->symbols, preshader->symbol_count, indent + 1);
May 30, 2011
May 30, 2011
232
233
for (i = 0; i < preshader->instruction_count; i++, inst++)
{
Jun 1, 2011
Jun 1, 2011
234
INDENT(); printf(" %s ", opcodestr[inst->opcode]);
May 30, 2011
May 30, 2011
235
Jun 1, 2011
Jun 1, 2011
236
237
// print dest register first...
print_preshader_operand(preshader, i, inst->operand_count - 1);
May 30, 2011
May 30, 2011
238
Jun 1, 2011
Jun 1, 2011
239
240
// ...then the source registers.
for (j = 0; j < inst->operand_count - 1; j++)
May 30, 2011
May 30, 2011
241
{
Jun 1, 2011
Jun 1, 2011
242
243
printf(", ");
print_preshader_operand(preshader, i, j);
May 30, 2011
May 30, 2011
244
245
246
247
248
249
250
251
252
} // for
printf("\n");
} // for
printf("\n");
} // print_preshader
Jun 20, 2011
Jun 20, 2011
253
254
255
256
257
258
259
260
261
262
263
264
265
266
static void print_attrs(const char *category, const int count,
const MOJOSHADER_attribute *attributes,
const int indent)
{
INDENT(); printf("%s:", category);
if (count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < count; i++)
{
static const char *usagenames[] = {
Dec 31, 2019
Dec 31, 2019
267
"<unknown>",
Jun 20, 2011
Jun 20, 2011
268
269
270
271
272
273
274
275
276
"position", "blendweight", "blendindices", "normal",
"psize", "texcoord", "tangent", "binormal", "tessfactor",
"positiont", "color", "fog", "depth", "sample"
};
const MOJOSHADER_attribute *a = &attributes[i];
char numstr[16] = { 0 };
if (a->index != 0)
snprintf(numstr, sizeof (numstr), "%d", a->index);
INDENT();
Dec 31, 2019
Dec 31, 2019
277
printf(" * %s%s", usagenames[1 + (int) a->usage], numstr);
Jun 20, 2011
Jun 20, 2011
278
279
280
281
282
283
284
285
if (a->name != NULL)
printf(" (\"%s\")", a->name);
printf("\n");
} // for
} // else
} // print_attrs
May 22, 2011
May 22, 2011
286
287
static void print_shader(const char *fname, const MOJOSHADER_parseData *pd,
unsigned int indent)
Mar 28, 2008
Mar 28, 2008
288
{
May 22, 2011
May 22, 2011
289
INDENT(); printf("PROFILE: %s\n", pd->profile);
Feb 3, 2009
Feb 3, 2009
290
291
292
293
294
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
May 22, 2011
May 22, 2011
295
296
const MOJOSHADER_error *err = &pd->errors[i];
INDENT();
Feb 12, 2009
Feb 12, 2009
297
printf("%s:%d: ERROR: %s\n",
Dec 31, 2019
Dec 31, 2019
298
299
err->filename ? err->filename : fname,
err->error_position, err->error);
Feb 3, 2009
Feb 3, 2009
300
301
} // for
} // if
Mar 28, 2008
Mar 28, 2008
302
303
else
{
May 22, 2011
May 22, 2011
304
305
306
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);
May 29, 2016
May 29, 2016
307
INDENT(); printf("MAIN FUNCTION: %s\n", pd->mainfn);
Jun 20, 2011
Jun 20, 2011
308
309
print_attrs("INPUTS", pd->attribute_count, pd->attributes, indent);
print_attrs("OUTPUTS", pd->output_count, pd->outputs, indent);
Apr 5, 2008
Apr 5, 2008
310
May 22, 2011
May 22, 2011
311
INDENT(); printf("CONSTANTS:");
May 3, 2008
May 3, 2008
312
313
314
315
316
317
318
319
320
321
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];
Dec 31, 2019
Dec 31, 2019
322
INDENT();
May 3, 2008
May 3, 2008
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
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
346
INDENT(); printf("UNIFORMS:");
Apr 4, 2008
Apr 4, 2008
347
348
349
350
351
352
353
354
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
355
static const char *typenames[] = { "float", "int", "bool" };
Apr 4, 2008
Apr 4, 2008
356
const MOJOSHADER_uniform *u = &pd->uniforms[i];
May 5, 2008
May 5, 2008
357
const char *arrayof = "";
Jul 31, 2008
Jul 31, 2008
358
const char *constant = u->constant ? "const " : "";
May 5, 2008
May 5, 2008
359
360
361
362
363
364
365
366
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
367
INDENT();
Jul 31, 2008
Jul 31, 2008
368
printf(" * %d: %s%s%s%s", u->index, constant, arrayof,
Dec 31, 2019
Dec 31, 2019
369
arrayrange, typenames[(int) u->type]);
Apr 25, 2008
Apr 25, 2008
370
371
372
if (u->name != NULL)
printf(" (\"%s\")", u->name);
printf("\n");
Apr 4, 2008
Apr 4, 2008
373
374
375
} // for
} // else
May 22, 2011
May 22, 2011
376
INDENT(); printf("SAMPLERS:");
Apr 19, 2008
Apr 19, 2008
377
378
379
380
381
382
383
384
385
386
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
387
INDENT();
Apr 25, 2008
Apr 25, 2008
388
389
390
printf(" * %d: %s", s->index, typenames[(int) s->type]);
if (s->name != NULL)
printf(" (\"%s\")", s->name);
Apr 17, 2012
Apr 17, 2012
391
392
if (s->texbem)
printf(" [TEXBEM]");
Apr 25, 2008
Apr 25, 2008
393
printf("\n");
Apr 19, 2008
Apr 19, 2008
394
395
396
} // for
} // else
Jun 1, 2011
Jun 1, 2011
397
print_symbols(pd->symbols, pd->symbol_count, indent);
May 29, 2011
May 29, 2011
398
May 30, 2011
May 30, 2011
399
400
401
if (pd->preshader != NULL)
print_preshader(pd->preshader, indent);
Mar 28, 2008
Mar 28, 2008
402
if (pd->output != NULL)
Apr 6, 2008
Apr 6, 2008
403
{
Dec 31, 2019
Dec 31, 2019
404
405
const char *output;
int output_len;
Apr 6, 2008
Apr 6, 2008
406
int i;
Dec 31, 2019
Dec 31, 2019
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
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
if (strcmp(pd->profile, "spirv") == 0)
{
#if SUPPORT_PROFILE_SPIRV && defined(MOJOSHADER_HAS_SPIRV_TOOLS)
int binary_len = pd->output_len - sizeof(SpirvPatchTable);
uint32_t *words = (uint32_t *) pd->output;
size_t word_count = binary_len / 4;
spv_text text;
spv_diagnostic diagnostic;
spv_context ctx = spvContextCreate(SPV_ENV_UNIVERSAL_1_0);
int options = /*SPV_BINARY_TO_TEXT_OPTION_COLOR |*/ SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES;
spv_result_t disResult = spvBinaryToText(ctx, words, word_count, options, &text, &diagnostic);
if (disResult == SPV_SUCCESS)
{
output = text->str;
output_len = text->length;
} // if
else
{
fprintf(stderr, "\nERROR DIAGNOSTIC: %s\n\n", diagnostic->error);
} // else
spv_result_t validateResult = spvValidateBinary(ctx, words, word_count, &diagnostic);
if (validateResult != SPV_SUCCESS)
{
fprintf(stderr, "\nVALIDATION FAILURE: %s\n\n", diagnostic->error);
} // if
if (disResult != SPV_SUCCESS || validateResult != SPV_SUCCESS)
{
exit(EXIT_FAILURE);
} // if
// FIXME: we're currently just leaking this disassembly...
#else
output = pd->output;
output_len = pd->output_len;
#endif
} // if
else
{
output = pd->output;
output_len = pd->output_len;
} // else
May 22, 2011
May 22, 2011
454
INDENT();
Apr 6, 2008
Apr 6, 2008
455
printf("OUTPUT:\n");
May 22, 2011
May 22, 2011
456
457
indent++;
INDENT();
Dec 31, 2019
Dec 31, 2019
458
for (i = 0; i < output_len; i++)
May 22, 2011
May 22, 2011
459
{
Dec 31, 2019
Dec 31, 2019
460
461
putchar((int) output[i]);
if (output[i] == '\n')
May 22, 2011
May 22, 2011
462
463
INDENT();
} // for
Apr 6, 2008
Apr 6, 2008
464
printf("\n");
May 22, 2011
May 22, 2011
465
indent--;
Apr 6, 2008
Apr 6, 2008
466
} // if
Mar 28, 2008
Mar 28, 2008
467
} // else
May 30, 2011
May 30, 2011
468
Mar 28, 2008
Mar 28, 2008
469
printf("\n\n");
May 22, 2011
May 22, 2011
470
471
472
} // print_shader
Jan 1, 2016
Jan 1, 2016
473
474
475
476
477
478
#ifdef MOJOSHADER_EFFECT_SUPPORT
static void print_value(const MOJOSHADER_effectValue *value,
const unsigned int indent)
{
May 29, 2016
May 29, 2016
479
int i, r, c;
Jan 1, 2016
Jan 1, 2016
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
INDENT();
printf("VALUE: %s -> %s\n", value->name, value->semantic);
static const char *classes[] =
{
"SCALAR",
"VECTOR",
"ROW-MAJOR MATRIX",
"COLUMN-MAJOR MATRIX",
"OBJECT",
"STRUCT"
};
static const char *types[] =
{
"VOID",
"BOOL",
"INT",
"FLOAT",
"STRING",
"TEXTURE",
"TEXTURE1D",
"TEXTURE2D",
"TEXTURE3D",
"TEXTURECUBE",
"SAMPLER",
"SAMPLER1D",
"SAMPLER2D",
"SAMPLER3D",
"SAMPLERCUBE",
"PIXELSHADER",
"VERTEXSHADER",
"UNSUPPORTED"
};
do_indent(indent + 1);
Feb 9, 2016
Feb 9, 2016
515
printf("CLASS: %s\n", classes[value->type.parameter_class]);
Jan 1, 2016
Jan 1, 2016
516
do_indent(indent + 1);
Feb 9, 2016
Feb 9, 2016
517
printf("TYPE: %s\n", types[value->type.parameter_type]);
Jan 1, 2016
Jan 1, 2016
518
519
520
do_indent(indent + 1);
printf("ROWS/COLUMNS/ELEMENTS: %d, %d, %d\n",
Feb 9, 2016
Feb 9, 2016
521
value->type.rows, value->type.columns, value->type.elements);
Jan 1, 2016
Jan 1, 2016
522
523
524
do_indent(indent + 1);
printf("TOTAL VALUES: %d\n", value->value_count);
Feb 9, 2016
Feb 9, 2016
525
526
527
528
529
if (value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER
|| value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER1D
|| value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER2D
|| value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLER3D
|| value->type.parameter_type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
Jan 1, 2016
Jan 1, 2016
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
{
do_indent(indent + 1);
printf("SAMPLER VALUES:\n");
for (i = 0; i < value->value_count; i++)
{
MOJOSHADER_effectSamplerState *state = &value->valuesSS[i];
static const char *samplerstatetypes[] =
{
"UNKNOWN0",
"UNKNOWN1",
"UNKNOWN2",
"UNKNOWN3",
"TEXTURE",
"ADDRESSU",
"ADDRESSV",
"ADDRESSW",
"BORDERCOLOR",
"MAGFILTER",
"MINFILTER",
"MIPFILTER",
"MIPMAPLODBIAS",
"MAXMIPLEVEL",
"MAXANISOTROPY",
"SRGBTEXTURE",
"ELEMENTINDEX",
"DMAPOFFSET",
};
do_indent(indent + 2);
printf("TYPE: %s -> ", samplerstatetypes[state->type]);
/* Assuming only one value per state! */
if (state->type == MOJOSHADER_SAMP_MIPMAPLODBIAS)
{
/* float types */
printf("%.2f\n", *state->value.valuesF);
} // if
else
{
/* int/enum types */
printf("%d\n", *state->value.valuesI);
} // else
} // for
} // if
else
{
do_indent(indent + 1);
Feb 9, 2016
Feb 9, 2016
577
printf("%s VALUES:\n", types[value->type.parameter_type]);
May 29, 2016
May 29, 2016
578
579
i = 0;
do
Jan 1, 2016
Jan 1, 2016
580
581
582
{
static const char *prints[] =
{
May 29, 2016
May 29, 2016
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
"%X ",
"%d ",
"%d ",
"%.2f ",
"%d ",
"%d ",
"%d ",
"%d ",
"%d ",
"%d ",
"SAMPLER?! ",
"SAMPLER?! ",
"SAMPLER?! ",
"SAMPLER?! ",
"SAMPLER?! ",
"%d ",
"%d ",
"%X "
Jan 1, 2016
Jan 1, 2016
601
};
May 29, 2016
May 29, 2016
602
603
604
605
606
607
608
609
610
611
612
613
614
615
for (r = 0; r < value->type.rows; r++)
{
do_indent(indent + 2);
for (c = 0; c < value->type.columns; c++)
{
const int offset = (i * value->type.rows * 4) + (r * 4) + c;
if (value->type.parameter_type == MOJOSHADER_SYMTYPE_FLOAT)
printf(prints[value->type.parameter_type], value->valuesF[offset]);
else
printf(prints[value->type.parameter_type], value->valuesI[offset]);
} // for
printf("\n");
} // for
} while (++i < value->type.elements);
Jan 1, 2016
Jan 1, 2016
616
617
618
619
} // else
} // print_value
May 22, 2011
May 22, 2011
620
621
622
static void print_effect(const char *fname, const MOJOSHADER_effect *effect,
const unsigned int indent)
{
Jan 1, 2016
Jan 1, 2016
623
INDENT();
May 22, 2011
May 22, 2011
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
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;
Jan 1, 2016
Jan 1, 2016
641
const MOJOSHADER_effectObject *object = effect->objects;
May 31, 2011
May 31, 2011
642
643
644
645
646
const MOJOSHADER_effectParam *param = effect->params;
for (i = 0; i < effect->param_count; i++, param++)
{
INDENT();
Jan 1, 2016
Jan 1, 2016
647
648
printf("PARAM #%d\n", i);
print_value(&param->value, indent + 1);
May 31, 2011
May 31, 2011
649
Jan 1, 2016
Jan 1, 2016
650
651
652
653
654
655
656
657
658
659
if (param->annotation_count > 0)
{
do_indent(indent + 1);
printf("ANNOTATIONS:\n");
} // if
for (j = 0; j < param->annotation_count; j++)
{
print_value(&param->annotations[j], indent + 2);
} // for
} // for
May 31, 2011
May 31, 2011
660
printf("\n");
May 22, 2011
May 22, 2011
661
662
663
664
for (i = 0; i < effect->technique_count; i++, technique++)
{
const MOJOSHADER_effectPass *pass = technique->passes;
Jan 1, 2016
Jan 1, 2016
665
666
INDENT();
printf("TECHNIQUE #%d ('%s'):\n", i, technique->name);
May 22, 2011
May 22, 2011
667
668
669
for (j = 0; j < technique->pass_count; j++, pass++)
{
const MOJOSHADER_effectState *state = pass->states;
Jan 1, 2016
Jan 1, 2016
670
671
do_indent(indent + 1);
printf("PASS #%d ('%s'):\n", j, pass->name);
May 22, 2011
May 22, 2011
672
673
for (k = 0; k < pass->state_count; k++, state++)
{
Jan 1, 2016
Jan 1, 2016
674
675
676
do_indent(indent + 2);
printf("STATE %d:\n", state->type);
print_value(&state->value, indent + 3);
May 22, 2011
May 22, 2011
677
678
679
680
681
} // for
} // for
} // for
printf("\n");
Jan 1, 2016
Jan 1, 2016
682
683
684
/* Start at index 1, 0 is always empty (thanks Microsoft!) */
object++;
for (i = 1; i < effect->object_count; i++, object++)
May 22, 2011
May 22, 2011
685
686
{
INDENT();
Jan 1, 2016
Jan 1, 2016
687
688
689
690
691
692
693
694
695
696
697
698
699
700
if (object->type == MOJOSHADER_SYMTYPE_PIXELSHADER
|| object->type == MOJOSHADER_SYMTYPE_VERTEXSHADER)
{
if (object->shader.is_preshader)
{
printf("OBJECT #%d: PRESHADER, technique %u, pass %u, param %s\n", i,
object->shader.technique, object->shader.pass,
effect->params[object->shader.params[0]].value.name);
print_preshader(object->shader.preshader, indent + 1);
} // if
else
{
printf("OBJECT #%d: SHADER, technique %u, pass %u\n", i,
object->shader.technique, object->shader.pass);
Apr 24, 2020
Apr 24, 2020
701
702
703
print_shader(fname,
(MOJOSHADER_parseData*) object->shader.shader,
indent + 1);
Jan 1, 2016
Jan 1, 2016
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
} // else
} // if
else if (object->type == MOJOSHADER_SYMTYPE_STRING)
printf("OBJECT #%d: STRING, '%s'\n", i,
object->string.string);
else if (object->type == MOJOSHADER_SYMTYPE_SAMPLER
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER1D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER2D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLER3D
|| object->type == MOJOSHADER_SYMTYPE_SAMPLERCUBE)
printf("OBJECT #%d: MAPPING, '%s'\n", i,
object->mapping.name);
else if (object->type == MOJOSHADER_SYMTYPE_TEXTURE
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE1D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE2D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURE3D
|| object->type == MOJOSHADER_SYMTYPE_TEXTURECUBE)
printf("OBJECT #%d: TEXTURE\n", i);
else
printf("UNKNOWN OBJECT: #%d\n", i);
May 22, 2011
May 22, 2011
724
725
726
727
728
} // for
} // else
} // print_effect
Apr 24, 2020
Apr 24, 2020
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
static const char *effect_profile = NULL;
static void* MOJOSHADERCALL effect_compile_shader(
const char *mainfn,
const unsigned char *tokenbuf,
const unsigned int bufsize,
const MOJOSHADER_swizzle *swiz,
const unsigned int swizcount,
const MOJOSHADER_samplerMap *smap,
const unsigned int smapcount
) {
return (MOJOSHADER_parseData*) MOJOSHADER_parse(effect_profile, mainfn,
tokenbuf, bufsize,
swiz, swizcount,
smap, smapcount,
Malloc, Free, NULL);
} // effect_compile_shader
static void MOJOSHADERCALL effect_delete_shader(void *shader)
{
MOJOSHADER_freeParseData((MOJOSHADER_parseData*) shader);
} // effect_delete_shader
static MOJOSHADER_parseData* MOJOSHADERCALL effect_get_parse_data(void *shader)
{
return (MOJOSHADER_parseData*) shader;
} // effect_get_parse_data
Jan 1, 2016
Jan 1, 2016
761
762
763
#endif // MOJOSHADER_EFFECT_SUPPORT
May 22, 2011
May 22, 2011
764
765
766
static int do_parse(const char *fname, const unsigned char *buf,
const int len, const char *prof)
{
Dec 31, 2019
Dec 31, 2019
767
int i;
May 22, 2011
May 22, 2011
768
769
770
int retval = 0;
// magic for an effects file (!!! FIXME: I _think_).
Jan 1, 2016
Jan 1, 2016
771
772
773
774
if ( ((buf[0] == 0x01) && (buf[1] == 0x09) &&
(buf[2] == 0xFF) && (buf[3] == 0xFE)) ||
((buf[0] == 0xCF) && (buf[1] == 0x0B) &&
(buf[2] == 0xF0) && (buf[3] == 0xBC)) )
May 22, 2011
May 22, 2011
775
{
Jan 1, 2016
Jan 1, 2016
776
#ifdef MOJOSHADER_EFFECT_SUPPORT
May 22, 2011
May 22, 2011
777
const MOJOSHADER_effect *effect;
Apr 24, 2020
Apr 24, 2020
778
779
780
781
782
783
784
785
786
787
788
789
790
791
const MOJOSHADER_effectShaderContext ctx =
{
effect_compile_shader,
NULL, /* Meh! */
effect_delete_shader,
effect_get_parse_data,
/* Meh! */
NULL,
NULL,
NULL,
NULL
};
effect_profile = prof;
effect = MOJOSHADER_compileEffect(buf, len, NULL, 0, NULL, 0, &ctx);
Dec 31, 2019
Dec 31, 2019
792
793
794
795
796
797
798
799
800
801
int error_count = effect->error_count;
for (i = 0; i < effect->object_count; i++)
{
MOJOSHADER_effectObject *object = &effect->objects[i];
switch (object->type)
{
case MOJOSHADER_SYMTYPE_VERTEXSHADER:
case MOJOSHADER_SYMTYPE_PIXELSHADER:
if (!object->shader.is_preshader)
{
Apr 24, 2020
Apr 24, 2020
802
803
const MOJOSHADER_parseData *shader =
ctx.getParseData(object->shader.shader);
Dec 31, 2019
Dec 31, 2019
804
805
806
807
808
809
810
811
812
if (shader)
error_count += shader->error_count;
} // if
break;
default:
break;
}
}
retval = (error_count == 0);
May 22, 2011
May 22, 2011
813
814
printf("EFFECT: %s\n", fname);
print_effect(fname, effect, 1);
Apr 24, 2020
Apr 24, 2020
815
MOJOSHADER_deleteEffect(effect);
Jan 1, 2016
Jan 1, 2016
816
817
818
#else
printf("Is an effect, but effect support is disabled!\n");
#endif
May 22, 2011
May 22, 2011
819
820
821
822
823
} // if
else // do it as a regular compiled shader.
{
const MOJOSHADER_parseData *pd;
Apr 25, 2016
Apr 25, 2016
824
pd = MOJOSHADER_parse(prof, NULL, buf, len, NULL, 0,
May 29, 2012
May 29, 2012
825
NULL, 0, Malloc, Free, NULL);
May 22, 2011
May 22, 2011
826
827
828
829
830
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
831
832
return retval;
Mar 28, 2008
Mar 28, 2008
833
834
835
} // do_parse
Feb 10, 2008
Feb 10, 2008
836
837
int main(int argc, char **argv)
{
Apr 19, 2008
Apr 19, 2008
838
839
int retval = 0;
Mar 28, 2008
Mar 28, 2008
840
printf("MojoShader testparse\n");
Mar 24, 2010
Mar 24, 2010
841
842
printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
printf("Linked against changeset %s\n", MOJOSHADER_changeset());
Mar 28, 2008
Mar 28, 2008
843
printf("\n");
Mar 22, 2008
Mar 22, 2008
844
Mar 28, 2008
Mar 28, 2008
845
846
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
Mar 28, 2008
Mar 28, 2008
847
else
Feb 10, 2008
Feb 10, 2008
848
{
Mar 28, 2008
Mar 28, 2008
849
850
851
852
const char *profile = argv[1];
int i;
for (i = 2; i < argc; i++)
Feb 10, 2008
Feb 10, 2008
853
{
Mar 28, 2008
Mar 28, 2008
854
855
FILE *io = fopen(argv[i], "rb");
if (io == NULL)
Mar 28, 2008
Mar 28, 2008
856
printf(" ... fopen('%s') failed.\n", argv[i]);
Mar 28, 2008
Mar 28, 2008
857
858
859
860
861
else
{
unsigned char *buf = (unsigned char *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
Mar 24, 2010
Mar 24, 2010
862
if (!do_parse(argv[i], buf, rc, profile))
Apr 19, 2008
Apr 19, 2008
863
retval = 1;
Mar 28, 2008
Mar 28, 2008
864
865
866
867
free(buf);
} // else
} // for
} // else
Feb 10, 2008
Feb 10, 2008
868
Apr 19, 2008
Apr 19, 2008
869
return retval;
Feb 10, 2008
Feb 10, 2008
870
871
872
} // main
// end of testparse.c ...