Skip to content

Latest commit

 

History

History
320 lines (280 loc) · 7.66 KB

parse.c

File metadata and controls

320 lines (280 loc) · 7.66 KB
 
Feb 9, 2008
Feb 9, 2008
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <stdio.h>
#include <stdlib.h>
#define SWAP32(x) (x)
typedef unsigned int uint;
typedef unsigned char uint8;
typedef unsigned int uint32;
// This enum complements of Filip Navara's public domain dev-cpp d3d9 header.
typedef enum
{
OPCODE_NOP = 0,
OPCODE_MOV = 1,
OPCODE_ADD = 2,
OPCODE_SUB = 3,
OPCODE_MAD = 4,
OPCODE_MUL = 5,
OPCODE_RCP = 6,
OPCODE_RSQ = 7,
OPCODE_DP3 = 8,
OPCODE_DP4 = 9,
OPCODE_MIN = 10,
OPCODE_MAX = 11,
OPCODE_SLT = 12,
OPCODE_SGE = 13,
OPCODE_EXP = 14,
OPCODE_LOG = 15,
OPCODE_LIT = 16,
OPCODE_DST = 17,
OPCODE_LRP = 18,
OPCODE_FRC = 19,
OPCODE_M4x4 = 20,
OPCODE_M4x3 = 21,
OPCODE_M3x4 = 22,
OPCODE_M3x3 = 23,
OPCODE_M3x2 = 24,
OPCODE_CALL = 25,
OPCODE_CALLNZ = 26,
OPCODE_LOOP = 27,
OPCODE_RET = 28,
OPCODE_ENDLOOP = 29,
OPCODE_LABEL = 30,
OPCODE_DCL = 31,
OPCODE_POW = 32,
OPCODE_CRS = 33,
OPCODE_SGN = 34,
OPCODE_ABS = 35,
OPCODE_NRM = 36,
OPCODE_SINCOS = 37,
OPCODE_REP = 38,
OPCODE_ENDREP = 39,
OPCODE_IF = 40,
OPCODE_IFC = 41,
OPCODE_ELSE = 42,
OPCODE_ENDIF = 43,
OPCODE_BREAK = 44,
OPCODE_BREAKC = 45,
OPCODE_MOVA = 46,
OPCODE_DEFB = 47,
OPCODE_DEFI = 48,
OPCODE_TEXCOORD = 64,
OPCODE_TEXKILL = 65,
OPCODE_TEX = 66,
OPCODE_TEXBEM = 67,
OPCODE_TEXBEML = 68,
OPCODE_TEXREG2AR = 69,
OPCODE_TEXREG2GB = 70,
OPCODE_TEXM3x2PAD = 71,
OPCODE_TEXM3x2TEX = 72,
OPCODE_TEXM3x3PAD = 73,
OPCODE_TEXM3x3TEX = 74,
OPCODE_RESERVED0 = 75,
OPCODE_TEXM3x3SPEC = 76,
OPCODE_TEXM3x3VSPEC = 77,
OPCODE_EXPP = 78,
OPCODE_LOGP = 79,
OPCODE_CND = 80,
OPCODE_DEF = 81,
OPCODE_TEXREG2RGB = 82,
OPCODE_TEXDP3TEX = 83,
OPCODE_TEXM3x2DEPTH = 84,
OPCODE_TEXDP3 = 85,
OPCODE_TEXM3x3 = 86,
OPCODE_TEXDEPTH = 87,
OPCODE_CMP = 88,
OPCODE_BEM = 89,
OPCODE_DP2ADD = 90,
OPCODE_DSX = 91,
OPCODE_DSY = 92,
OPCODE_TEXLDD = 93,
OPCODE_SETP = 94,
OPCODE_TEXLDL = 95,
OPCODE_BREAKP = 96,
OPCODE_PHASE = 0xfffd,
} OpcodeVal;
static int parse_version_token(const uint32 *tokens, const uint32 tokencount)
{
if (tokencount == 0)
return -1; // no tokens at all?
const uint32 token = SWAP32(*tokens);
const uint32 shadertype = ((token >> 16) & 0xFFFF);
const uint32 major = ((token >> 8) & 0xFF);
const uint32 minor = (token & 0xFF);
if (shadertype == 0xFFFF)
printf("Pixel shader\n");
else if (shadertype == 0xFFFE)
printf("Vertex shader\n");
else
return -1; // geometry shader? Unsupported at the moment. FAIL.
printf("Version %u.%u\n", (uint) major, (uint) minor);
return 1; // ate one token.
} // parse_version_token
static int parse_comment_token(const uint32 *tokens, const uint32 tokencount)
{
const uint32 token = SWAP32(*tokens);
if ((token & 0xFFFF) != 0xFFFE)
return 0; // not a comment token.
else if ((token & 0x80000000) != 0)
return -1; // msdn docs say high bit must be zero. FAIL.
const uint32 commenttoks = ((token >> 16) & 0xFFFF);
const uint32 commentlen = commenttoks * sizeof (uint32);
printf("Comment (%u tokens, %u bytes): ",
(uint) commenttoks, (uint) commentlen);
uint32 i = 0;
const char *comment = (const char *) (tokens+1);
while (i < commentlen)
fputc(comment[i++], stdout);
printf("\n");
return commenttoks + 1; // comment data plus the initial token.
} // parse_comment_token
static int parse_end_token(const uint32 *tokens, const uint32 tokencount)
{
if (SWAP32(*tokens) != 0x0000FFFF) // end token is always 0x0000FFFF.
return 0; // not us, eat no tokens.
printf("END\n");
// we _must_ be last. If so, eat the token. Otherwise: FAIL.
return (tokencount == 1) ? 1 : -1;
} // parse_end_token
static int parse_instruction_token(const uint32 *tokens, const uint32 tokencount)
{
const uint32 token = SWAP32(*tokens);
const uint32 opcode = (token & 0xFFFF);
const uint32 controls = ((token >> 16) & 0xFF);
const uint32 insttoks = ((token >> 24) & 0x0F);
const int coissue = (token & 0x40000000) ? 1 : 0;
const int predicated = (token & 0x10000000) ? 1 : 0;
if ((token & 0x80000000) != 0)
return -1; // msdn docs say high bit must be zero. FAIL.
#define PARSE_OP(op) printf("OPCODE %s\n", #op);
//case OPCODE_##op:
// parse_op_##op(tokens+1, opcode, controls, insttoks, coissue, predicated);
// break;
PARSE_OP(NOP);
PARSE_OP(MOV);
PARSE_OP(ADD);
PARSE_OP(SUB);
PARSE_OP(MAD);
PARSE_OP(MUL);
PARSE_OP(RCP);
PARSE_OP(RSQ);
PARSE_OP(DP3);
PARSE_OP(DP4);
PARSE_OP(MIN);
PARSE_OP(MAX);
PARSE_OP(SLT);
PARSE_OP(SGE);
PARSE_OP(EXP);
PARSE_OP(LOG);
PARSE_OP(LIT);
PARSE_OP(DST);
PARSE_OP(LRP);
PARSE_OP(FRC);
PARSE_OP(M4x4);
PARSE_OP(M4x3);
PARSE_OP(M3x4);
PARSE_OP(M3x3);
PARSE_OP(M3x2);
PARSE_OP(CALL);
PARSE_OP(CALLNZ);
PARSE_OP(LOOP);
PARSE_OP(RET);
PARSE_OP(ENDLOOP);
PARSE_OP(LABEL);
PARSE_OP(DCL);
PARSE_OP(POW);
PARSE_OP(CRS);
PARSE_OP(SGN);
PARSE_OP(ABS);
PARSE_OP(NRM);
PARSE_OP(SINCOS);
PARSE_OP(REP);
PARSE_OP(ENDREP);
PARSE_OP(IF);
PARSE_OP(IFC);
PARSE_OP(ELSE);
PARSE_OP(ENDIF);
PARSE_OP(BREAK);
PARSE_OP(BREAKC);
PARSE_OP(MOVA);
PARSE_OP(DEFB);
PARSE_OP(DEFI);
PARSE_OP(TEXCOORD);
PARSE_OP(TEXKILL);
PARSE_OP(TEX);
PARSE_OP(TEXBEM);
PARSE_OP(TEXBEML);
PARSE_OP(TEXREG2AR);
PARSE_OP(TEXREG2GB);
PARSE_OP(TEXM3x2PAD);
PARSE_OP(TEXM3x2TEX);
PARSE_OP(TEXM3x3PAD);
PARSE_OP(TEXM3x3TEX);
PARSE_OP(RESERVED0);
PARSE_OP(TEXM3x3SPEC);
PARSE_OP(TEXM3x3VSPEC);
PARSE_OP(EXPP);
PARSE_OP(LOGP);
PARSE_OP(CND);
PARSE_OP(DEF);
PARSE_OP(TEXREG2RGB);
PARSE_OP(TEXDP3TEX);
PARSE_OP(TEXM3x2DEPTH);
PARSE_OP(TEXDP3);
PARSE_OP(TEXM3x3);
PARSE_OP(TEXDEPTH);
PARSE_OP(CMP);
PARSE_OP(BEM);
PARSE_OP(DP2ADD);
PARSE_OP(DSX);
PARSE_OP(DSY);
PARSE_OP(TEXLDD);
PARSE_OP(SETP);
PARSE_OP(TEXLDL);
PARSE_OP(BREAKP);
PARSE_OP(PHASE);
#undef PARSE_OP
} // parse_instruction_token
static int parse_token(const uint32 *tokens, const uint32 tokencount)
{
int retval = -1;
int rc = 0;
if (tokencount == 0)
return -1; // shouldn't happen, but just in case...
if ((rc = parse_comment_token(tokens, tokencount)) != 0)
return rc;
if ((rc = parse_end_token(tokens, tokencount)) != 0)
return rc;
if ((rc = parse_instruction_token(tokens, tokencount)) != 0)
return rc;
return -1; // nothing handled this? FAIL.
} // parse_token
int D3D2GLSL_parse(const uint8 *tokenbuf, const uint32 bufsize)
{
const uint32 *tokens = (const uint32 *) tokenbuf;
uint32 tokencount = bufsize / sizeof (uint32);
int rc = parse_version_token(tokens, tokencount);
// parse out the rest of the tokens after the version token...
while ((rc > 0) && (tokencount > 0))
{
tokens += rc;
tokencount -= rc;
rc = parse_token(tokens, tokencount);
} // while
return ((rc <= 0) || (tokencount > 0)) ? 0 : 1;
} // D3D2GLSL_parse
int main(int argc, char **argv)
{
if (argv[1] != NULL)
{
FILE *io = fopen(argv[1], "rb");
if (io != NULL)
{
uint8 *buf = (uint8 *) malloc(1000000);
int rc = fread(buf, 1, 1000000, io);
fclose(io);
D3D2GLSL_parse(buf, rc);
free(buf);
} // if
} // if
return 0;
} // main
// end of parse.c ...