/
testparse.c
188 lines (165 loc) · 5.22 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
static int do_parse(const unsigned char *buf, const int len, const char *prof)
58
59
{
const MOJOSHADER_parseData *pd;
60
int retval = 0;
61
62
pd = MOJOSHADER_parse(prof, buf, len, Malloc, Free, NULL);
63
64
65
printf("PROFILE: %s\n", prof);
if (pd->error != NULL)
printf("ERROR: %s\n", pd->error);
66
67
else
{
68
retval = 1;
69
70
71
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);
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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);
91
92
93
94
printf(" * %s%s", usagenames[(int) a->usage], numstr);
if (a->name != NULL)
printf(" (\"%s\")", a->name);
printf("\n");
95
96
97
} // for
} // else
98
99
100
101
102
103
104
105
106
printf("UNIFORMS:");
if (pd->uniform_count == 0)
printf(" (none.)\n");
else
{
int i;
printf("\n");
for (i = 0; i < pd->uniform_count; i++)
{
107
static const char *typenames[] = { "float", "int", "bool" };
108
const MOJOSHADER_uniform *u = &pd->uniforms[i];
109
110
111
112
printf(" * %d: %s", u->index, typenames[(int) u->type]);
if (u->name != NULL)
printf(" (\"%s\")", u->name);
printf("\n");
113
114
115
} // for
} // else
116
117
118
119
120
121
122
123
124
125
126
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];
127
128
129
130
printf(" * %d: %s", s->index, typenames[(int) s->type]);
if (s->name != NULL)
printf(" (\"%s\")", s->name);
printf("\n");
131
132
133
} // for
} // else
134
if (pd->output != NULL)
135
136
137
138
139
140
141
{
int i;
printf("OUTPUT:\n");
for (i = 0; i < pd->output_len; i++)
putchar((int) pd->output[i]);
printf("\n");
} // if
142
} // else
143
144
printf("\n\n");
MOJOSHADER_freeParseData(pd);
145
146
return retval;
147
148
149
} // do_parse
150
151
int main(int argc, char **argv)
{
152
153
int retval = 0;
154
printf("MojoShader testparse\n");
155
156
printf("Compiled against version %d\n", MOJOSHADER_VERSION);
printf("Linked against version %d\n", MOJOSHADER_version());
157
printf("\n");
158
159
160
if (argc <= 2)
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]);
161
else
162
{
163
164
165
166
const char *profile = argv[1];
int i;
for (i = 2; i < argc; i++)
167
{
168
169
170
FILE *io = fopen(argv[i], "rb");
printf("FILE: %s\n", argv[i]);
if (io == NULL)
171
printf(" ... fopen('%s') failed.\n", argv[i]);
172
173
174
175
176
else
{
unsigned char *buf = (unsigned char *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
177
178
if (!do_parse(buf, rc, profile))
retval = 1;
179
180
181
182
free(buf);
} // else
} // for
} // else
183
184
return retval;
185
186
187
} // main
// end of testparse.c ...