7
|
1 |
#include <stdio.h>
|
9
|
2 |
#include <stdlib.h>
|
37
|
3 |
#include "mojoshader.h"
|
7
|
4 |
|
46
|
5 |
|
|
6 |
#if DEBUG_MALLOC
|
|
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 |
{
|
|
20 |
int *ptr = (((int *) ptr) - 1);
|
|
21 |
int *store = (int *) ptr;
|
|
22 |
int len = *store;
|
|
23 |
printf("free() %d bytes (%p)\n", len, ptr);
|
|
24 |
free(ptr);
|
|
25 |
} // Free
|
|
26 |
#else
|
|
27 |
#define Malloc NULL
|
|
28 |
#define Free NULL
|
|
29 |
#endif
|
|
30 |
|
|
31 |
static const char *shader_type(MOJOSHADER_shaderType s)
|
|
32 |
{
|
|
33 |
switch (s)
|
|
34 |
{
|
|
35 |
case MOJOSHADER_TYPE_UNKNOWN: return "unknown";
|
|
36 |
case MOJOSHADER_TYPE_PIXEL: return "pixel";
|
|
37 |
case MOJOSHADER_TYPE_VERTEX: return "vertex";
|
|
38 |
case MOJOSHADER_TYPE_GEOMETRY: return "geometry";
|
|
39 |
} // switch
|
|
40 |
|
|
41 |
return "(bogus value?)";
|
|
42 |
} // shader_type
|
|
43 |
|
|
44 |
|
|
45 |
static void do_parse(unsigned char *buf, int len, const char *prof)
|
|
46 |
{
|
|
47 |
const MOJOSHADER_parseData *pd;
|
|
48 |
|
|
49 |
pd = MOJOSHADER_parse(prof, buf, len, Malloc, Free);
|
|
50 |
printf("PROFILE: %s\n", prof);
|
|
51 |
printf("SHADER TYPE: %s\n", shader_type(pd->shader_type));
|
|
52 |
printf("VERSION: %d.%d\n", pd->major_ver, pd->minor_ver);
|
|
53 |
printf("INSTRUCTION COUNT: %d\n", (int) pd->instruction_count);
|
|
54 |
if (pd->error != NULL)
|
|
55 |
printf("ERROR: %s\n", pd->error);
|
|
56 |
if (pd->output != NULL)
|
|
57 |
printf("OUTPUT:\n%s\n", pd->output);
|
|
58 |
printf("\n\n");
|
|
59 |
MOJOSHADER_freeParseData(pd);
|
|
60 |
} // do_parse
|
|
61 |
|
|
62 |
|
7
|
63 |
int main(int argc, char **argv)
|
|
64 |
{
|
46
|
65 |
int i;
|
|
66 |
|
37
|
67 |
printf("Compiled against version %d\n", MOJOSHADER_VERSION);
|
|
68 |
printf("Linked against version %d\n", MOJOSHADER_version());
|
|
69 |
|
46
|
70 |
for (i = 1; i < argc; i++)
|
7
|
71 |
{
|
46
|
72 |
FILE *io = fopen(argv[i], "rb");
|
7
|
73 |
if (io != NULL)
|
|
74 |
{
|
9
|
75 |
unsigned char *buf = (unsigned char *) malloc(1000000);
|
7
|
76 |
int rc = fread(buf, 1, 1000000, io);
|
|
77 |
fclose(io);
|
46
|
78 |
do_parse(buf, rc, MOJOSHADER_PROFILE_D3D);
|
|
79 |
do_parse(buf, rc, MOJOSHADER_PROFILE_GLSL);
|
7
|
80 |
free(buf);
|
|
81 |
} // if
|
|
82 |
} // if
|
|
83 |
|
|
84 |
return 0;
|
|
85 |
} // main
|
|
86 |
|
|
87 |
// end of testparse.c ...
|
|
88 |
|