author | Ryan C. Gordon <icculus@icculus.org> |
Sun, 27 Apr 2008 22:43:02 -0400 | |
branch | trunk |
changeset 230 | 396aa372c7d8 |
parent 192 | e7c864575d1c |
child 244 | 30ceb789b9a1 |
permissions | -rw-r--r-- |
7 | 1 |
#include <stdio.h> |
9 | 2 |
#include <stdlib.h> |
37 | 3 |
#include "mojoshader.h" |
7 | 4 |
|
46 | 5 |
|
50 | 6 |
#if MOJOSHADER_DEBUG_MALLOC |
46 | 7 |
static void *Malloc(int len) |
8 |
{ |
|
9 |
void *ptr = malloc(len + sizeof (int)); |
|
10 |
int *store = (int *) ptr; |
|
11 |
printf("malloc() %d bytes (%p)\n", len, ptr); |
|
12 |
if (ptr == NULL) return NULL; |
|
13 |
*store = len; |
|
14 |
return (void *) (store + 1); |
|
15 |
} // Malloc |
|
16 |
||
17 |
||
18 |
static void Free(void *_ptr) |
|
19 |
{ |
|
50 | 20 |
int *ptr = (((int *) _ptr) - 1); |
21 |
int len = *ptr; |
|
46 | 22 |
printf("free() %d bytes (%p)\n", len, ptr); |
23 |
free(ptr); |
|
24 |
} // Free |
|
25 |
#else |
|
26 |
#define Malloc NULL |
|
27 |
#define Free NULL |
|
28 |
#endif |
|
29 |
||
48 | 30 |
static const char *shader_type(const MOJOSHADER_shaderType s) |
46 | 31 |
{ |
32 |
switch (s) |
|
33 |
{ |
|
34 |
case MOJOSHADER_TYPE_UNKNOWN: return "unknown"; |
|
35 |
case MOJOSHADER_TYPE_PIXEL: return "pixel"; |
|
36 |
case MOJOSHADER_TYPE_VERTEX: return "vertex"; |
|
37 |
case MOJOSHADER_TYPE_GEOMETRY: return "geometry"; |
|
48 | 38 |
default: return "(bogus value?)"; |
46 | 39 |
} // switch |
40 |
||
48 | 41 |
return NULL; // shouldn't hit this. |
46 | 42 |
} // shader_type |
43 |
||
44 |
||
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
45 |
static int do_parse(const unsigned char *buf, const int len, const char *prof) |
46 | 46 |
{ |
47 |
const MOJOSHADER_parseData *pd; |
|
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
48 |
int retval = 0; |
46 | 49 |
|
97
4a41e3d17297
Allow instance data to be passed to the allocator.
Ryan C. Gordon <icculus@icculus.org>
parents:
94
diff
changeset
|
50 |
pd = MOJOSHADER_parse(prof, buf, len, Malloc, Free, NULL); |
46 | 51 |
printf("PROFILE: %s\n", prof); |
52 |
if (pd->error != NULL) |
|
53 |
printf("ERROR: %s\n", pd->error); |
|
53 | 54 |
else |
55 |
{ |
|
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
56 |
retval = 1; |
53 | 57 |
printf("SHADER TYPE: %s\n", shader_type(pd->shader_type)); |
58 |
printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver); |
|
59 |
printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count); |
|
100
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
60 |
|
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
61 |
printf("ATTRIBUTES:"); |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
62 |
if (pd->attribute_count == 0) |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
63 |
printf(" (none.)\n"); |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
64 |
else |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
65 |
{ |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
66 |
int i; |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
67 |
printf("\n"); |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
68 |
for (i = 0; i < pd->attribute_count; i++) |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
69 |
{ |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
70 |
static const char *usagenames[] = { |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
71 |
"position", "blendweight", "blendindices", "normal", |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
72 |
"psize", "texcoord", "tangent", "binormal", "tessfactor", |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
73 |
"positiont", "color", "fog", "depth", "sample" |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
74 |
}; |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
75 |
const MOJOSHADER_attribute *a = &pd->attributes[i]; |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
76 |
char numstr[16] = { 0 }; |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
77 |
if (a->index != 0) |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
78 |
snprintf(numstr, sizeof (numstr), "%d", a->index); |
192
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
79 |
printf(" * %s%s", usagenames[(int) a->usage], numstr); |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
80 |
if (a->name != NULL) |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
81 |
printf(" (\"%s\")", a->name); |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
82 |
printf("\n"); |
100
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
83 |
} // for |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
84 |
} // else |
2b88649b6f98
First shot at attributes reporting API.
Ryan C. Gordon <icculus@icculus.org>
parents:
97
diff
changeset
|
85 |
|
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
86 |
printf("UNIFORMS:"); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
87 |
if (pd->uniform_count == 0) |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
88 |
printf(" (none.)\n"); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
89 |
else |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
90 |
{ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
91 |
int i; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
92 |
printf("\n"); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
93 |
for (i = 0; i < pd->uniform_count; i++) |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
94 |
{ |
94
57adfb4769a0
Simplifed public uniform information.
Ryan C. Gordon <icculus@icculus.org>
parents:
92
diff
changeset
|
95 |
static const char *typenames[] = { "float", "int", "bool" }; |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
96 |
const MOJOSHADER_uniform *u = &pd->uniforms[i]; |
192
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
97 |
printf(" * %d: %s", u->index, typenames[(int) u->type]); |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
98 |
if (u->name != NULL) |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
99 |
printf(" (\"%s\")", u->name); |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
100 |
printf("\n"); |
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
101 |
} // for |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
102 |
} // else |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
103 |
|
148
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
104 |
printf("SAMPLERS:"); |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
105 |
if (pd->sampler_count == 0) |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
106 |
printf(" (none.)\n"); |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
107 |
else |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
108 |
{ |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
109 |
int i; |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
110 |
printf("\n"); |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
111 |
for (i = 0; i < pd->sampler_count; i++) |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
112 |
{ |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
113 |
static const char *typenames[] = { "2d", "cube", "volume" }; |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
114 |
const MOJOSHADER_sampler *s = &pd->samplers[i]; |
192
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
115 |
printf(" * %d: %s", s->index, typenames[(int) s->type]); |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
116 |
if (s->name != NULL) |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
117 |
printf(" (\"%s\")", s->name); |
e7c864575d1c
Print variable names in testparse.c.
Ryan C. Gordon <icculus@icculus.org>
parents:
148
diff
changeset
|
118 |
printf("\n"); |
148
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
119 |
} // for |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
120 |
} // else |
645003ec6623
Hopefully sorted out the reported uniform/attribute mess.
Ryan C. Gordon <icculus@icculus.org>
parents:
142
diff
changeset
|
121 |
|
53 | 122 |
if (pd->output != NULL) |
113
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
123 |
{ |
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
124 |
int i; |
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
125 |
printf("OUTPUT:\n"); |
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
126 |
for (i = 0; i < pd->output_len; i++) |
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
127 |
putchar((int) pd->output[i]); |
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
128 |
printf("\n"); |
8ebf445c5305
Print output in testparse.c byte-by-byte instead of as an ASCIZ string.
Ryan C. Gordon <icculus@icculus.org>
parents:
100
diff
changeset
|
129 |
} // if |
53 | 130 |
} // else |
46 | 131 |
printf("\n\n"); |
132 |
MOJOSHADER_freeParseData(pd); |
|
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
133 |
|
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
134 |
return retval; |
46 | 135 |
} // do_parse |
136 |
||
137 |
||
7 | 138 |
int main(int argc, char **argv) |
139 |
{ |
|
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
140 |
int retval = 0; |
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
141 |
|
47 | 142 |
printf("MojoShader testparse\n"); |
37 | 143 |
printf("Compiled against version %d\n", MOJOSHADER_VERSION); |
144 |
printf("Linked against version %d\n", MOJOSHADER_version()); |
|
47 | 145 |
printf("\n"); |
37 | 146 |
|
53 | 147 |
if (argc <= 2) |
148 |
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]); |
|
47 | 149 |
else |
7 | 150 |
{ |
53 | 151 |
const char *profile = argv[1]; |
152 |
int i; |
|
153 |
||
154 |
for (i = 2; i < argc; i++) |
|
7 | 155 |
{ |
47 | 156 |
FILE *io = fopen(argv[i], "rb"); |
157 |
printf("FILE: %s\n", argv[i]); |
|
158 |
if (io == NULL) |
|
53 | 159 |
printf(" ... fopen('%s') failed.\n", argv[i]); |
47 | 160 |
else |
161 |
{ |
|
162 |
unsigned char *buf = (unsigned char *) malloc(1000000); |
|
163 |
int rc = fread(buf, 1, 1000000, io); |
|
164 |
fclose(io); |
|
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
165 |
if (!do_parse(buf, rc, profile)) |
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
166 |
retval = 1; |
47 | 167 |
free(buf); |
168 |
} // else |
|
169 |
} // for |
|
170 |
} // else |
|
7 | 171 |
|
142
e064b4cefb4e
Return a more-meaningful pass/fail error code from testparse main().
Ryan C. Gordon <icculus@icculus.org>
parents:
113
diff
changeset
|
172 |
return retval; |
7 | 173 |
} // main |
174 |
||
175 |
// end of testparse.c ... |
|
176 |