Debian Codegraph @

utils/testparse.c

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