/
d3d2glsl.c
748 lines (603 loc) · 18.2 KB
/
d3d2glsl.c
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/**
* d3d2glsl; generate GLSL 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.
*/
// Shader bytecode format is described at MSDN:
// http://msdn2.microsoft.com/en-us/library/ms800307.aspx
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "d3d2glsl.h"
typedef unsigned int uint; // this is a printf() helper. don't use for code.
typedef uint8_t uint8;
typedef uint32_t uint32;
// Context...this is state that changes as we parse through a shader...
typedef struct D3D2GLSL_context
{
const uint32 *tokens;
uint32 tokencount;
char buffers[128][5]; // scratch buffers we cycle through.
int bufidx; // current scratch buffer.
} D3D2GLSL_context;
// Byteswap magic...
#if ((defined __GNUC__) && (defined __POWERPC__))
static inline uint32 SWAP32(uint32 x)
{
__asm__ __volatile__("lwbrx %0,0,%1" : "=r" (x) : "r" (&x));
return x;
} // SWAP32
#elif defined(__POWERPC__)
static inline uint32 SWAP32(uint32 x)
{
return ( (((x) >> 24) & 0x000000FF) | (((x) >> 8) & 0x0000FF00) |
(((x) << 8) & 0x00FF0000) | (((x) << 24) & 0xFF000000) );
} // SWAP32
#else
# define SWAP32(x) (x)
#endif
// Special-case return values from the parsing pipeline...
#define END_OF_STREAM (-2)
//#define FAIL(x) (-1)
static int FAIL(const char *reason)
{
printf("%s FAIL.\n", reason);
return -1;
} // FAIL
// one function for each opcode...
typedef int (*parse_instruction_function)(D3D2GLSL_context *ctx);
static int parse_NOP(D3D2GLSL_context *ctx)
{
return 1; // no-op is a no-op. :)
} // parse_NOP
static int parse_MOV(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_MOV
static int parse_ADD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_ADD
static int parse_SUB(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_SUB
static int parse_MAD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_MAD
static int parse_MUL(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_MUL
static int parse_RCP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_RCP
static int parse_RSQ(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_RSQ
static int parse_DP3(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DP3
static int parse_DP4(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DP4
static int parse_MIN(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_MIN
static int parse_MAX(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_MAX
static int parse_SLT(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_SLT
static int parse_SGE(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_SGE
static int parse_EXP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_EXP
static int parse_LOG(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_LOG
static int parse_LIT(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_LIT
static int parse_DST(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DST
static int parse_LRP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_LRP
static int parse_FRC(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_FRC
static int parse_M4X4(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_M4X4
static int parse_M4X3(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_M4X3
static int parse_M3X4(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_M3X4
static int parse_M3X3(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_M3X3
static int parse_M3X2(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_M3X2
static int parse_CALL(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_CALL
static int parse_CALLNZ(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_CALLNZ
static int parse_LOOP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_LOOP
static int parse_RET(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_RET
static int parse_ENDLOOP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_ENDLOOP
static int parse_LABEL(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_LABEL
static int parse_DCL(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DCL
static int parse_POW(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_POW
static int parse_CRS(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_CRS
static int parse_SGN(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_SGN
static int parse_ABS(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_ABS
static int parse_NRM(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_NRM
static int parse_SINCOS(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_SINCOS
static int parse_REP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_REP
static int parse_ENDREP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_ENDREP
static int parse_IF(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_IF
static int parse_IFC(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_IFC
static int parse_ELSE(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_ELSE
static int parse_ENDIF(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_ENDIF
static int parse_BREAK(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_BREAK
static int parse_BREAKC(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_BREAKC
static int parse_MOVA(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_MOVA
static int parse_DEFB(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DEFB
static int parse_DEFI(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DEFI
static int parse_TEXCOORD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXCOORD
static int parse_TEXKILL(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXKILL
static int parse_TEX(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEX
static int parse_TEXBEM(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXBEM
static int parse_TEXBEML(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXBEML
static int parse_TEXREG2AR(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXREG2AR
static int parse_TEXREG2GB(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXREG2GB
static int parse_TEXM3X2PAD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X2PAD
static int parse_TEXM3X2TEX(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X2TEX
static int parse_TEXM3X3PAD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X3PAD
static int parse_TEXM3X3TEX(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X3TEX
static int parse_RESERVED0(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_RESERVED0
static int parse_TEXM3X3SPEC(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X3SPEC
static int parse_TEXM3X3VSPEC(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X3VSPEC
static int parse_EXPP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_EXPP
static int parse_LOGP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_LOGP
static int parse_CND(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_CND
static int parse_DEF(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DEF
static int parse_TEXREG2RGB(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXREG2RGB
static int parse_TEXDP3TEX(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXDP3TEX
static int parse_TEXM3X2DEPTH(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X2DEPTH
static int parse_TEXDP3(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXDP3
static int parse_TEXM3X3(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXM3X3
static int parse_TEXDEPTH(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXDEPTH
static int parse_CMP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_CMP
static int parse_BEM(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_BEM
static int parse_DP2ADD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DP2ADD
static int parse_DSX(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DSX
static int parse_DSY(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_DSY
static int parse_TEXLDD(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXLDD
static int parse_SETP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_SETP
static int parse_TEXLDL(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_TEXLDL
static int parse_BREAKP(D3D2GLSL_context *ctx)
{
return FAIL("unimplemented."); // !!! FIXME
} // parse_BREAKP
static int parse_RESERVED(D3D2GLSL_context *ctx)
{
return FAIL("Tried to use RESERVED opcode.");
} // parse_BREAKP
// Lookup table for instruction opcodes...
typedef struct
{
const char *opcode_string;
int arg_tokens;
//uint32 shader_requirements;
parse_instruction_function parser;
} Instruction;
// These have to be in the right order! This array is indexed by the value
// of the instruction token.
static Instruction instructions[] = {
#define INSTRUCTION(op, args) { #op, args, parse_##op }
INSTRUCTION(NOP, 0),
INSTRUCTION(MOV, 2),
INSTRUCTION(ADD, 3),
INSTRUCTION(SUB, 3),
INSTRUCTION(MAD, 4),
INSTRUCTION(MUL, 3),
INSTRUCTION(RCP, 2),
INSTRUCTION(RSQ, 2),
INSTRUCTION(DP3, 3),
INSTRUCTION(DP4, 3),
INSTRUCTION(MIN, 3),
INSTRUCTION(MAX, 3),
INSTRUCTION(SLT, 3),
INSTRUCTION(SGE, 3),
INSTRUCTION(EXP, 2),
INSTRUCTION(LOG, 2),
INSTRUCTION(LIT, 2),
INSTRUCTION(DST, 3),
INSTRUCTION(LRP, 4),
INSTRUCTION(FRC, 2),
INSTRUCTION(M4X4, 3),
INSTRUCTION(M4X3, 3),
INSTRUCTION(M3X4, 3),
INSTRUCTION(M3X3, 3),
INSTRUCTION(M3X2, 3),
INSTRUCTION(CALL, 1),
INSTRUCTION(CALLNZ, 2),
INSTRUCTION(LOOP, 2),
INSTRUCTION(RET, 0),
INSTRUCTION(ENDLOOP, 0),
INSTRUCTION(LABEL, 1),
INSTRUCTION(DCL, -1),
INSTRUCTION(POW, 3),
INSTRUCTION(CRS, 3),
INSTRUCTION(SGN, 4),
INSTRUCTION(ABS, 2),
INSTRUCTION(NRM, 2),
INSTRUCTION(SINCOS, 4),
INSTRUCTION(REP, 1),
INSTRUCTION(ENDREP, 0),
INSTRUCTION(IF, 1),
INSTRUCTION(IFC, 2),
INSTRUCTION(ELSE, 0),
INSTRUCTION(ENDIF, 0),
INSTRUCTION(BREAK, 0),
INSTRUCTION(BREAKC, 2),
INSTRUCTION(MOVA, 2),
INSTRUCTION(DEFB, 2),
INSTRUCTION(DEFI, 5),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(TEXCOORD, -1),
INSTRUCTION(TEXKILL, 1),
INSTRUCTION(TEX, -1),
INSTRUCTION(TEXBEM, 2),
INSTRUCTION(TEXBEML, 2),
INSTRUCTION(TEXREG2AR, 2),
INSTRUCTION(TEXREG2GB, 2),
INSTRUCTION(TEXM3X2PAD, 2),
INSTRUCTION(TEXM3X2TEX, 2),
INSTRUCTION(TEXM3X3PAD, 2),
INSTRUCTION(TEXM3X3TEX, 2),
INSTRUCTION(RESERVED, 0),
INSTRUCTION(TEXM3X3SPEC, 3),
INSTRUCTION(TEXM3X3VSPEC, 2),
INSTRUCTION(EXPP, 2),
INSTRUCTION(LOGP, 2),
INSTRUCTION(CND, 4),
INSTRUCTION(DEF, 5),
INSTRUCTION(TEXREG2RGB, 2),
INSTRUCTION(TEXDP3TEX, 2),
INSTRUCTION(TEXM3X2DEPTH, 2),
INSTRUCTION(TEXDP3, 2),
INSTRUCTION(TEXM3X3, 2),
INSTRUCTION(TEXDEPTH, 1),
INSTRUCTION(CMP, 4),
INSTRUCTION(BEM, 3),
INSTRUCTION(DP2ADD, 4),
INSTRUCTION(DSX, 2),
INSTRUCTION(DSY, 2),
INSTRUCTION(TEXLDD, 5),
INSTRUCTION(SETP, 3),
INSTRUCTION(TEXLDL, 3),
INSTRUCTION(BREAKP, 1), // src
#undef INSTRUCTION
};
// parse various token types...
static int parse_instruction_token(D3D2GLSL_context *ctx)
{
const uint32 token = SWAP32(*(ctx->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;
const Instruction *instruction = &instructions[opcode];
if ( opcode >= (sizeof (instructions) / sizeof (instructions[0])) )
return 0; // not an instruction token, or just not handled here.
if ((token & 0x80000000) != 0)
return FAIL("instruction token high bit must be zero."); // so says msdn.
printf("%s\n", instruction->opcode_string);
if (instruction->arg_tokens >= 0)
{
if (instruction->arg_tokens != insttoks)
return FAIL("unexpected number of tokens for instruction.");
else if (ctx->tokencount <= instruction->arg_tokens)
return FAIL("not enough tokens left in shader for instruction.");
} // if
return insttoks + 1;
// return instruction->parser(ctx, tokens + 1);
} // parse_instruction_token
static int parse_version_token(D3D2GLSL_context *ctx)
{
if (ctx->tokencount == 0)
return FAIL("Expected version token, got none at all.");
const uint32 token = SWAP32(*(ctx->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 FAIL("geometry shader? Unsupported at the moment.");
printf("Version %u.%u\n", (uint) major, (uint) minor);
return 1; // ate one token.
} // parse_version_token
static int parse_comment_token(D3D2GLSL_context *ctx)
{
const uint32 token = SWAP32(*(ctx->tokens));
if ((token & 0xFFFF) != 0xFFFE)
return 0; // not a comment token.
else if ((token & 0x80000000) != 0)
return FAIL("comment token high bit must be zero."); // so says msdn.
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 *) (ctx->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(D3D2GLSL_context *ctx)
{
if (SWAP32(*(ctx->tokens)) != 0x0000FFFF) // end token always 0x0000FFFF.
return 0; // not us, eat no tokens.
printf("END\n");
if (ctx->tokencount != 1) // we _must_ be last. If not: FAIL.
return FAIL("end token before end of stream");
return END_OF_STREAM;
} // parse_end_token
static int parse_phase_token(D3D2GLSL_context *ctx)
{
if (SWAP32(*(ctx->tokens)) != 0x0000FFFD) // phase token always 0x0000FFFD.
return 0; // not us, eat no tokens.
return FAIL("not sure what this thing is yet.");
} // parse_phase_token
static int parse_token(D3D2GLSL_context *ctx)
{
int rc = 0;
if (ctx->tokencount == 0)
return FAIL("unexpected end of shader.");
if ((rc = parse_comment_token(ctx)) != 0)
return rc;
if ((rc = parse_end_token(ctx)) != 0)
return rc;
if ((rc = parse_phase_token(ctx)) != 0)
return rc;
if ((rc = parse_instruction_token(ctx)) != 0)
return rc;
return FAIL("unknown token");
} // parse_token
// API entry point...
int D3D2GLSL_parse(const unsigned char *tokenbuf, const unsigned int bufsize)
{
D3D2GLSL_context ctx;
int rc = 0;
memset(&ctx, '\0', sizeof (D3D2GLSL_context));
ctx.tokens = (const uint32 *) tokenbuf;
ctx.tokencount = bufsize / sizeof (uint32);
rc = parse_version_token(&ctx);
// parse out the rest of the tokens after the version token...
while (rc > 0)
{
ctx.tokens += rc;
ctx.tokencount -= rc;
rc = parse_token(&ctx);
} // while
return (rc == END_OF_STREAM);
} // D3D2GLSL_parse
// end of parse.c ...