/
testparse.c
243 lines (218 loc) · 7.27 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
static const char *shader_type(const MOJOSHADER_shaderType s)
43
44
45
46
47
48
49
{
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";
50
default: return "(bogus value?)";
51
52
} // switch
53
return NULL; // shouldn't hit this.
54
55
56
} // shader_type
57
58
static int do_parse(const char *fname, const unsigned char *buf,
const int len, const char *prof)
59
60
{
const MOJOSHADER_parseData *pd;
61
int retval = 0;
62
63
pd = MOJOSHADER_parse(prof, buf, len, NULL, 0, Malloc, Free, NULL);
64
printf("PROFILE: %s\n", prof);
65
66
67
68
69
if (pd->error_count > 0)
{
int i;
for (i = 0; i < pd->error_count; i++)
{
70
printf("%s:%d: ERROR: %s\n",
71
pd->errors[i].filename ? pd->errors[i].filename : fname,
72
73
74
75
pd->errors[i].error_position,
pd->errors[i].error);
} // for
} // if
76
77
else
{
78
retval = 1;
79
80
81
printf("SHADER TYPE: %s\n", shader_type(pd->shader_type));
printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver);
printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count);
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
printf("ATTRIBUTES:");
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);
101
102
103
104
printf(" * %s%s", usagenames[(int) a->usage], numstr);
if (a->name != NULL)
printf(" (\"%s\")", a->name);
printf("\n");
105
106
107
} // for
} // else
108
109
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
138
139
140
141
printf("CONSTANTS:");
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];
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
142
143
144
145
146
147
148
149
150
printf("UNIFORMS:");
if (pd->uniform_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->uniform_count; i++)
{
151
static const char *typenames[] = { "float", "int", "bool" };
152
const MOJOSHADER_uniform *u = &pd->uniforms[i];
153
const char *arrayof = "";
154
const char *constant = u->constant ? "const " : "";
155
156
157
158
159
160
161
162
char arrayrange[64] = { '\0' };
if (u->array_count > 0)
{
arrayof = "array[";
snprintf(arrayrange, sizeof (arrayrange), "%d] ",
u->array_count);
} // if
163
164
printf(" * %d: %s%s%s%s", u->index, constant, arrayof,
arrayrange, typenames[(int) u->type]);
165
166
167
if (u->name != NULL)
printf(" (\"%s\")", u->name);
printf("\n");
168
169
170
} // for
} // else
171
172
173
174
175
176
177
178
179
180
181
printf("SAMPLERS:");
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];
182
183
184
185
printf(" * %d: %s", s->index, typenames[(int) s->type]);
if (s->name != NULL)
printf(" (\"%s\")", s->name);
printf("\n");
186
187
188
} // for
} // else
189
if (pd->output != NULL)
190
191
192
193
194
195
196
{
int i;
printf("OUTPUT:\n");
for (i = 0; i < pd->output_len; i++)
putchar((int) pd->output[i]);
printf("\n");
} // if
197
} // else
198
199
printf("\n\n");
MOJOSHADER_freeParseData(pd);
200
201
return retval;
202
203
204
} // do_parse
205
206
int main(int argc, char **argv)
{
207
208
int retval = 0;
209
printf("MojoShader testparse\n");
210
211
printf("Compiled against changeset %s\n", MOJOSHADER_CHANGESET);
printf("Linked against changeset %s\n", MOJOSHADER_changeset());
212
printf("\n");
213
214
215
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
216
else
217
{
218
219
220
221
const char *profile = argv[1];
int i;
for (i = 2; i < argc; i++)
222
{
223
224
225
FILE *io = fopen(argv[i], "rb");
printf("FILE: %s\n", argv[i]);
if (io == NULL)
226
printf(" ... fopen('%s') failed.\n", argv[i]);
227
228
229
230
231
else
{
unsigned char *buf = (unsigned char *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
232
if (!do_parse(argv[i], buf, rc, profile))
233
retval = 1;
234
235
236
237
free(buf);
} // else
} // for
} // else
238
239
return retval;
240
241
242
} // main
// end of testparse.c ...