author | Ryan C. Gordon <icculus@icculus.org> |
Fri, 04 Apr 2008 09:45:04 -0400 | |
branch | trunk |
changeset 92 | bc1bb138e855 |
parent 53 | 8c856f73908e |
child 94 | 57adfb4769a0 |
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 |
||
53 | 45 |
static void do_parse(const unsigned char *buf, const int len, const char *prof) |
46 | 46 |
{ |
47 |
const MOJOSHADER_parseData *pd; |
|
48 |
||
49 |
pd = MOJOSHADER_parse(prof, buf, len, Malloc, Free); |
|
50 |
printf("PROFILE: %s\n", prof); |
|
51 |
if (pd->error != NULL) |
|
52 |
printf("ERROR: %s\n", pd->error); |
|
53 | 53 |
else |
54 |
{ |
|
55 |
printf("SHADER TYPE: %s\n", shader_type(pd->shader_type)); |
|
56 |
printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver); |
|
57 |
printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count); |
|
92
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
58 |
printf("UNIFORMS:"); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
59 |
if (pd->uniform_count == 0) |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
60 |
printf(" (none.)\n"); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
61 |
else |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
62 |
{ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
63 |
static const char *typenames[] = { "float", "int", "bool" }; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
64 |
int i; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
65 |
printf("\n"); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
66 |
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
|
67 |
{ |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
68 |
const MOJOSHADER_uniform *u = &pd->uniforms[i]; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
69 |
const char *name = u->name ? u->name : ""; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
70 |
const char *typestr = typenames[(int) u->type]; |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
71 |
printf(" * %d: %s %s\n", u->index, typestr, name); |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
72 |
} // for |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
73 |
} // else |
bc1bb138e855
Implemented foundation for reporting uniforms to calling app.
Ryan C. Gordon <icculus@icculus.org>
parents:
53
diff
changeset
|
74 |
|
53 | 75 |
if (pd->output != NULL) |
76 |
printf("OUTPUT:\n%s\n", pd->output); |
|
77 |
} // else |
|
46 | 78 |
printf("\n\n"); |
79 |
MOJOSHADER_freeParseData(pd); |
|
80 |
} // do_parse |
|
81 |
||
82 |
||
7 | 83 |
int main(int argc, char **argv) |
84 |
{ |
|
47 | 85 |
printf("MojoShader testparse\n"); |
37 | 86 |
printf("Compiled against version %d\n", MOJOSHADER_VERSION); |
87 |
printf("Linked against version %d\n", MOJOSHADER_version()); |
|
47 | 88 |
printf("\n"); |
37 | 89 |
|
53 | 90 |
if (argc <= 2) |
91 |
printf("\n\nUSAGE: %s <profile> [file1] ... [fileN]\n\n", argv[0]); |
|
47 | 92 |
else |
7 | 93 |
{ |
53 | 94 |
const char *profile = argv[1]; |
95 |
int i; |
|
96 |
||
97 |
for (i = 2; i < argc; i++) |
|
7 | 98 |
{ |
47 | 99 |
FILE *io = fopen(argv[i], "rb"); |
100 |
printf("FILE: %s\n", argv[i]); |
|
101 |
if (io == NULL) |
|
53 | 102 |
printf(" ... fopen('%s') failed.\n", argv[i]); |
47 | 103 |
else |
104 |
{ |
|
105 |
unsigned char *buf = (unsigned char *) malloc(1000000); |
|
106 |
int rc = fread(buf, 1, 1000000, io); |
|
107 |
fclose(io); |
|
53 | 108 |
do_parse(buf, rc, profile); |
47 | 109 |
free(buf); |
110 |
} // else |
|
111 |
} // for |
|
112 |
} // else |
|
7 | 113 |
|
114 |
return 0; |
|
115 |
} // main |
|
116 |
||
117 |
// end of testparse.c ... |
|
118 |