/
mojoshader_assembler.c
1836 lines (1542 loc) · 53.1 KB
1
2
3
4
5
6
7
8
9
/**
* MojoShader; generate shader 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.
*/
10
11
12
// !!! FIXME: this should probably use a formal grammar and not a hand-written
// !!! FIXME: pile of C code.
13
14
15
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
16
#if DEBUG_ASSEMBLER_PARSER
17
18
19
20
21
22
#define print_debug_token(token, len, val) \
MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
#else
#define print_debug_token(token, len, val)
#endif
23
24
25
26
27
28
29
typedef struct SourcePos
{
const char *filename;
uint32 line;
} SourcePos;
30
31
// Context...this is state that changes as we assemble a shader...
32
typedef struct Context
33
{
34
35
int isfail;
int out_of_memory;
36
37
38
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
39
40
int error_count;
ErrorList *errors;
41
Preprocessor *preprocessor;
42
MOJOSHADER_parsePhase parse_phase;
43
44
45
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
46
47
48
49
50
51
52
int pushedback;
const char *token; // assembler token!
unsigned int tokenlen; // assembler token!
Token tokenval; // assembler token!
uint32 version_token; // bytecode token!
uint32 tokenbuf[16]; // bytecode tokens!
int tokenbufpos; // bytecode tokens!
53
DestArgInfo dest_arg;
54
uint32 *output;
55
SourcePos *token_to_source;
56
57
58
uint8 *ctab;
uint32 ctab_len;
uint32 ctab_allocation;
59
60
size_t output_len;
size_t output_allocation;
61
} Context;
62
63
64
65
// Convenience functions for allocators...
66
static inline void out_of_memory(Context *ctx)
67
{
68
ctx->isfail = ctx->out_of_memory = 1;
69
70
71
72
73
74
75
76
77
78
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
79
80
81
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
82
if (retval != NULL)
83
84
85
86
strcpy(retval, str);
return retval;
} // StrDup
87
88
89
90
91
92
static inline void Free(Context *ctx, void *ptr)
{
if (ptr != NULL) // check for NULL in case of dumb free() impl.
ctx->free(ptr, ctx->malloc_data);
} // Free
93
94
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
95
{
96
97
98
99
const char *fname = NULL;
unsigned int linenum = 0;
int error_position = 0;
100
101
ctx->isfail = 1;
102
103
104
105
106
107
switch (ctx->parse_phase)
{
case MOJOSHADER_PARSEPHASE_NOTSTARTED:
error_position = -2;
break;
case MOJOSHADER_PARSEPHASE_WORKING:
108
109
fname = preprocessor_sourcepos(ctx->preprocessor, &linenum);
error_position = (int) linenum;
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
break;
case MOJOSHADER_PARSEPHASE_DONE:
error_position = -1;
break;
default:
assert(0 && "Unexpected value");
return;
} // switch
ErrorList *error = (ErrorList *) Malloc(ctx, sizeof (ErrorList));
if (error == NULL)
return;
char scratch = 0;
va_list ap;
va_start(ap, fmt);
const int len = vsnprintf(&scratch, sizeof (scratch), fmt, ap);
va_end(ap);
char *failstr = (char *) Malloc(ctx, len + 1);
if (failstr == NULL)
Free(ctx, error);
else
133
134
{
va_start(ap, fmt);
135
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
136
137
va_end(ap);
138
139
140
error->error.error = failstr;
error->error.filename = fname ? StrDup(ctx, fname) : NULL;
error->error.error_position = error_position;
141
error->next = NULL;
142
143
ErrorList *prev = NULL;
144
145
ErrorList *item = ctx->errors;
while (item != NULL)
146
{
147
prev = item;
148
item = item->next;
149
150
} // while
151
if (prev == NULL)
152
ctx->errors = error;
153
154
else
prev->next = error;
155
156
157
ctx->error_count++;
} // else
158
159
} // failf
160
static inline void fail(Context *ctx, const char *reason)
161
{
162
failf(ctx, "%s", reason);
163
164
165
166
} // fail
static inline int isfail(const Context *ctx)
{
167
return ctx->isfail;
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
} // isfail
// Shader model version magic...
static inline uint32 ver_ui32(const uint8 major, const uint8 minor)
{
return ( (((uint32) major) << 16) | (((minor) == 0xFF) ? 0 : (minor)) );
} // version_ui32
static inline int shader_version_atleast(const Context *ctx, const uint8 maj,
const uint8 min)
{
return (ver_ui32(ctx->major_ver, ctx->minor_ver) >= ver_ui32(maj, min));
} // shader_version_atleast
static inline int shader_is_pixel(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_PIXEL);
} // shader_is_pixel
static inline int shader_is_vertex(const Context *ctx)
{
return (ctx->shader_type == MOJOSHADER_TYPE_VERTEX);
} // shader_is_vertex
194
195
static inline void pushback(Context *ctx)
{
196
#if DEBUG_ASSEMBLER_PARSER
197
198
printf("ASSEMBLER PUSHBACK\n");
#endif
199
200
201
202
203
204
205
206
207
assert(!ctx->pushedback);
ctx->pushedback = 1;
} // pushback
static Token nexttoken(Context *ctx)
{
if (ctx->pushedback)
ctx->pushedback = 0;
208
else
209
{
210
while (1)
211
{
212
213
214
ctx->token = preprocessor_nexttoken(ctx->preprocessor,
&ctx->tokenlen,
&ctx->tokenval);
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
if (preprocessor_outofmemory(ctx->preprocessor))
{
out_of_memory(ctx);
ctx->tokenval = TOKEN_EOI;
ctx->token = NULL;
ctx->tokenlen = 0;
break;
} // if
else if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR)
{
fail(ctx, ctx->token);
continue;
} // else if
break;
} // while
} // else
234
235
print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
236
return ctx->tokenval;
237
238
239
} // nexttoken
240
241
static inline void add_token_sourcepos(Context *ctx, const size_t idx)
{
242
243
244
unsigned int pos = 0;
const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
ctx->token_to_source[idx].line = pos;
245
ctx->token_to_source[idx].filename = fname; // cached in preprocessor!
246
247
248
} // add_token_sourcepos
249
250
251
252
253
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
254
255
256
257
if (ctx->output_len >= ctx->output_allocation)
{
const size_t output_alloc_bump = 1024; // that's tokens, not bytes.
const size_t newsize = ctx->output_allocation + output_alloc_bump;
258
259
260
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
261
262
263
264
265
266
if (ptr == NULL)
return;
if (ctx->output_len > 0)
memcpy(ptr, ctx->output, ctx->output_len * sizeof (uint32));
Free(ctx, ctx->output);
ctx->output = (uint32 *) ptr;
267
268
ptr = Malloc(ctx, newsize * sizeof (SourcePos));
269
270
271
if (ptr == NULL)
return;
if (ctx->output_len > 0)
272
273
274
memcpy(ptr, ctx->token_to_source, ctx->output_len * sizeof (SourcePos));
Free(ctx, ctx->token_to_source);
ctx->token_to_source = (SourcePos *) ptr;
275
276
ctx->output_allocation = newsize;
277
278
} // if
279
ctx->output[ctx->output_len] = token;
280
add_token_sourcepos(ctx, ctx->output_len);
281
ctx->output_len++;
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
} // output_token_noswap
static inline void output_token(Context *ctx, const uint32 token)
{
output_token_noswap(ctx, SWAP32(token));
} // output_token
static void output_comment_bytes(Context *ctx, const uint8 *buf, size_t len)
{
if (len > (0xFFFF * 4)) // length is stored as token count, in 16 bits.
fail(ctx, "Comment field is too big");
else if (!isfail(ctx))
{
const uint32 tokencount = (len / 4) + ((len % 4) ? 1 : 0);
output_token(ctx, 0xFFFE | (tokencount << 16));
while (len >= 4)
{
output_token_noswap(ctx, *((const uint32 *) buf));
len -= 4;
buf += 4;
} // while
if (len > 0) // handle spillover...
{
union { uint8 ui8[4]; uint32 ui32; } overflow;
overflow.ui32 = 0;
memcpy(overflow.ui8, buf, len);
output_token_noswap(ctx, overflow.ui32);
} // if
} // else if
} // output_comment_bytes
static inline void output_comment_string(Context *ctx, const char *str)
{
output_comment_bytes(ctx, (const uint8 *) str, strlen(str));
} // output_comment_string
323
static int require_comma(Context *ctx)
324
{
325
326
const Token token = nexttoken(ctx);
if (token != ((Token) ','))
327
{
328
329
fail(ctx, "Comma expected");
return 0;
330
} // if
331
332
return 1;
} // require_comma
333
334
335
static int check_token_segment(Context *ctx, const char *str)
336
{
337
338
339
340
341
342
343
344
// !!! FIXME: these are case-insensitive, right?
const size_t len = strlen(str);
if ( (ctx->tokenlen < len) || (strncasecmp(ctx->token, str, len) != 0) )
return 0;
ctx->token += len;
ctx->tokenlen -= len;
return 1;
} // check_token_segment
345
346
347
static int check_token(Context *ctx, const char *str)
348
{
349
350
const size_t len = strlen(str);
if ( (ctx->tokenlen != len) || (strncasecmp(ctx->token, str, len) != 0) )
351
return 0;
352
353
ctx->token += len;
ctx->tokenlen = 0;
354
return 1;
355
} // check_token
356
357
358
static int ui32fromtoken(Context *ctx, uint32 *_val)
359
{
360
361
362
363
364
365
int i;
for (i = 0; i < ctx->tokenlen; i++)
{
if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
break;
} // for
366
367
if (i == 0)
368
{
369
370
*_val = 0;
return 0;
371
372
} // if
373
374
375
376
const int len = i;
uint32 val = 0;
uint32 mult = 1;
while (i--)
377
{
378
379
380
val += ((uint32) (ctx->token[i] - '0')) * mult;
mult *= 10;
} // while
381
382
383
ctx->token += len;
ctx->tokenlen -= len;
384
385
*_val = val;
386
return 1;
387
} // ui32fromtoken
388
389
390
391
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
392
393
394
if (nexttoken(ctx) != TOKEN_IDENTIFIER)
{
fail(ctx, "Expected register");
395
return 0;
396
} // if
397
398
399
400
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
401
402
403
404
// Watch out for substrings! oDepth must be checked before oD, since
// the latter will match either case.
if (check_token_segment(ctx, "oDepth"))
405
406
407
408
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
409
else if (check_token_segment(ctx, "vFace"))
410
{
411
412
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
413
414
neednum = 0;
} // else if
415
else if (check_token_segment(ctx, "vPos"))
416
417
418
419
420
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
421
else if (check_token_segment(ctx, "oPos"))
422
423
424
425
426
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
427
else if (check_token_segment(ctx, "oFog"))
428
429
430
431
432
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
433
else if (check_token_segment(ctx, "oPts"))
434
435
436
437
438
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POINT_SIZE;
neednum = 0;
} // else if
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
else if (check_token_segment(ctx, "aL"))
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
else if (check_token_segment(ctx, "oC"))
regtype = REG_TYPE_COLOROUT;
else if (check_token_segment(ctx, "oT"))
regtype = REG_TYPE_OUTPUT;
else if (check_token_segment(ctx, "oD"))
regtype = REG_TYPE_ATTROUT;
else if (check_token_segment(ctx, "r"))
regtype = REG_TYPE_TEMP;
else if (check_token_segment(ctx, "v"))
regtype = REG_TYPE_INPUT;
else if (check_token_segment(ctx, "c"))
regtype = REG_TYPE_CONST;
else if (check_token_segment(ctx, "i"))
regtype = REG_TYPE_CONSTINT;
else if (check_token_segment(ctx, "b"))
regtype = REG_TYPE_CONSTBOOL;
else if (check_token_segment(ctx, "s"))
regtype = REG_TYPE_SAMPLER;
else if (check_token_segment(ctx, "l"))
regtype = REG_TYPE_LABEL;
else if (check_token_segment(ctx, "p"))
regtype = REG_TYPE_PREDICATE;
else if (check_token_segment(ctx, "o"))
regtype = REG_TYPE_OUTPUT;
else if (check_token_segment(ctx, "a"))
regtype = REG_TYPE_ADDRESS;
else if (check_token_segment(ctx, "t"))
regtype = REG_TYPE_ADDRESS;
472
473
474
475
476
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
else
{
477
478
479
480
fail(ctx, "expected register type");
regtype = REG_TYPE_CONST;
regnum = 0;
neednum = 0;
481
482
} // else
483
484
// "c[5]" is the same as "c5", so if the token is done, see if next is '['.
if ((neednum) && (ctx->tokenlen == 0))
485
{
486
if (nexttoken(ctx) == ((Token) '['))
487
neednum = 0; // don't need a number on register name itself.
488
pushback(ctx);
489
490
} // if
491
492
if (neednum)
{
493
uint32 ui32 = 0;
494
if (!ui32fromtoken(ctx, &ui32))
495
fail(ctx, "Invalid register index");
496
regnum = (int) ui32;
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
} // if
// split up REG_TYPE_CONST
if (regtype == REG_TYPE_CONST)
{
if (regnum < 2048)
{
regtype = REG_TYPE_CONST;
regnum -= 0;
} // if
else if (regnum < 4096)
{
regtype = REG_TYPE_CONST2;
regnum -= 2048;
} // if
else if (regnum < 6144)
{
regtype = REG_TYPE_CONST3;
regnum -= 4096;
} // if
else if (regnum < 8192)
{
regtype = REG_TYPE_CONST4;
regnum -= 6144;
} // if
else
{
524
fail(ctx, "Invalid const register index");
525
526
527
528
529
530
} // else
} // if
*rtype = regtype;
*rnum = regnum;
531
return 1;
532
533
534
} // parse_register_name
535
static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
536
{
537
if (info->result_shift != 0)
538
fail(ctx, "Multiple result shift modifiers");
539
540
info->result_shift = val;
} // set_result_shift
541
542
543
static int parse_destination_token(Context *ctx)
544
{
545
DestArgInfo *info = &ctx->dest_arg;
546
memset(info, '\0', sizeof (DestArgInfo));
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// parse_instruction_token() sets ctx->token to the end of the instruction
// so we can see if there are destination modifiers on the instruction
// itself...
int invalid_modifier = 0;
while ((ctx->tokenlen > 0) && (!invalid_modifier))
{
if (check_token_segment(ctx, "_x2"))
set_result_shift(ctx, info, 0x1);
else if (check_token_segment(ctx, "_x4"))
set_result_shift(ctx, info, 0x2);
else if (check_token_segment(ctx, "_x8"))
set_result_shift(ctx, info, 0x3);
else if (check_token_segment(ctx, "_d8"))
set_result_shift(ctx, info, 0xD);
else if (check_token_segment(ctx, "_d4"))
set_result_shift(ctx, info, 0xE);
else if (check_token_segment(ctx, "_d2"))
set_result_shift(ctx, info, 0xF);
else if (check_token_segment(ctx, "_sat"))
569
info->result_mod |= MOD_SATURATE;
570
else if (check_token_segment(ctx, "_pp"))
571
info->result_mod |= MOD_PP;
572
else if (check_token_segment(ctx, "_centroid"))
573
574
info->result_mod |= MOD_CENTROID;
else
575
invalid_modifier = 1;
576
577
} // while
578
579
if (invalid_modifier)
fail(ctx, "Invalid destination modifier");
580
581
// !!! FIXME: predicates.
582
if (nexttoken(ctx) == ((Token) '('))
583
584
fail(ctx, "Predicates unsupported at this time"); // !!! FIXME: ...
585
586
pushback(ctx); // parse_register_name calls nexttoken().
587
parse_register_name(ctx, &info->regtype, &info->regnum);
588
589
590
// parse_register_name() can't check this: dest regs might have modifiers.
if (ctx->tokenlen > 0)
fail(ctx, "invalid register name");
591
592
593
// !!! FIXME: can dest registers do relative addressing?
594
int invalid_writemask = 0;
595
int implicit_writemask = 0;
596
if (nexttoken(ctx) != ((Token) '.'))
597
{
598
implicit_writemask = 1;
599
600
601
602
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
603
604
605
606
// !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
//else if (scalar_register(ctx->shader_type, info->regtype, info->regnum))
else if ( (scalar_register(ctx->shader_type, info->regtype, info->regnum)) && (info->regtype != REG_TYPE_DEPTHOUT) )
607
fail(ctx, "Writemask specified for scalar register");
608
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
609
invalid_writemask = 1;
610
611
else
{
612
613
614
615
616
617
// !!! FIXME: is out-of-order okay (yxzw instead of xyzw?)
char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
char *ptr = tokenbytes;
618
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
619
620
621
622
if (*ptr == 'x') { info->writemask0 = 1; ptr++; }
if (*ptr == 'y') { info->writemask1 = 1; ptr++; }
if (*ptr == 'z') { info->writemask2 = 1; ptr++; }
if (*ptr == 'w') { info->writemask3 = 1; ptr++; }
623
if ((ptr == ctx->token) && (shader_is_pixel(ctx)))
624
{
625
626
627
628
if (*ptr == 'r') { info->writemask0 = 1; ptr++; }
if (*ptr == 'g') { info->writemask1 = 1; ptr++; }
if (*ptr == 'b') { info->writemask2 = 1; ptr++; }
if (*ptr == 'a') { info->writemask3 = 1; ptr++; }
629
630
631
} // if
if (*ptr != '\0')
632
invalid_writemask = 1;
633
634
635
636
637
638
639
info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );
} // else
640
641
642
if (invalid_writemask)
fail(ctx, "Invalid writemask");
643
644
645
646
647
// !!! FIXME: Cg generates code with oDepth.z ... this is a bug, I think.
if (info->regtype == REG_TYPE_DEPTHOUT)
{
if ( (!implicit_writemask) && ((info->writemask0 + info->writemask1 +
info->writemask2 + info->writemask3) > 1) )
648
fail(ctx, "Writemask specified for scalar register");
649
650
} // if
651
652
653
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
654
655
656
657
{
fail(ctx, "Too many tokens");
return 1;
} // if
658
659
ctx->tokenbuf[ctx->tokenbufpos++] =
660
( ((((uint32) 1)) << 31) |
661
662
663
664
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
665
((((uint32) info->writemask) & 0xF) << 16) |
666
667
668
669
670
671
672
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
673
674
675
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
676
{
677
678
679
680
681
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
682
683
684
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
685
{
686
687
688
int retval = 1;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
689
690
691
692
{
fail(ctx, "Too many tokens");
return 0;
} // if
693
694
// mark this now, so optional relative addressing token is placed second.
695
696
uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
*outtoken = 0;
697
698
699
SourceMod srcmod = SRCMOD_NONE;
int negate = 0;
700
701
702
703
704
705
706
Token token = nexttoken(ctx);
if (token == ((Token) '!'))
srcmod = SRCMOD_NOT;
else if (token == ((Token) '-'))
negate = 1;
else if ( (token == TOKEN_INT_LITERAL) && (check_token(ctx, "1")) )
707
{
708
709
if (nexttoken(ctx) != ((Token) '-'))
fail(ctx, "Unexpected token");
710
711
712
713
else
srcmod = SRCMOD_COMPLEMENT;
} // else
else
714
{
715
pushback(ctx);
716
} // else
717
718
719
RegisterType regtype;
int regnum;
720
parse_register_name(ctx, ®type, ®num);
721
722
723
724
725
726
727
if (ctx->tokenlen == 0)
{
if (negate)
set_source_mod(ctx, negate, SRCMOD_NONE, SRCMOD_NEGATE, &srcmod);
} // if
else
728
{
729
assert(ctx->tokenlen > 0);
730
731
732
733
734
735
736
737
738
739
740
741
742
743
if (check_token_segment(ctx, "_bias"))
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
else if (check_token_segment(ctx, "_bx2"))
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
else if (check_token_segment(ctx, "_x2"))
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
else if (check_token_segment(ctx, "_dz"))
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
else if (check_token_segment(ctx, "_dw"))
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
else if (check_token_segment(ctx, "_abs"))
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
else
fail(ctx, "Invalid source modifier");
744
} // else
745
746
uint32 relative = 0;
747
if (nexttoken(ctx) != ((Token) '['))
748
749
750
pushback(ctx); // not relative addressing?
else
{
751
752
753
754
755
756
if (!relok)
fail(ctx, "Relative addressing not permitted here.");
else
retval++;
parse_source_token_maybe_relative(ctx, 0);
757
relative = 1;
758
759
if (nexttoken(ctx) != ((Token) '+'))
760
761
762
pushback(ctx);
else
{
763
764
// !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
if (regnum != 0)
765
fail(ctx, "Relative addressing with explicit register number.");
766
767
uint32 ui32 = 0;
768
769
770
771
if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) ||
(!ui32fromtoken(ctx, &ui32)) ||
(ctx->tokenlen != 0) )
{
772
fail(ctx, "Invalid relative addressing offset");
773
} // if
774
775
776
regnum += (int) ui32;
} // else
777
if (nexttoken(ctx) != ((Token) ']'))
778
fail(ctx, "Expected ']'");
779
} // else
780
781
int invalid_swizzle = 0;
782
uint32 swizzle = 0;
783
if (nexttoken(ctx) != ((Token) '.'))
784
785
786
787
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
788
else if (scalar_register(ctx->shader_type, regtype, regnum))
789
fail(ctx, "Swizzle specified for scalar register");
790
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
791
invalid_swizzle = 1;
792
793
else
{
794
795
796
797
char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
798
// deal with shortened form (.x = .xxxx, etc).
799
800
801
802
803
804
805
if (tokenlen == 1)
tokenbytes[1] = tokenbytes[2] = tokenbytes[3] = tokenbytes[0];
else if (tokenlen == 2)
tokenbytes[2] = tokenbytes[3] = tokenbytes[1];
else if (tokenlen == 3)
tokenbytes[3] = tokenbytes[2];
else if (tokenlen != 4)
806
invalid_swizzle = 1;
807
tokenbytes[4] = '\0';
808
809
uint32 val = 0;
810
811
int saw_xyzw = 0;
int saw_rgba = 0;
812
int i;
813
814
for (i = 0; i < 4; i++)
{
815
const int component = (int) tokenbytes[i];
816
817
818
819
820
821
822
823
824
825
switch (component)
{
case 'x': val = 0; saw_xyzw = 1; break;
case 'y': val = 1; saw_xyzw = 1; break;
case 'z': val = 2; saw_xyzw = 1; break;
case 'w': val = 3; saw_xyzw = 1; break;
case 'r': val = 0; saw_rgba = 1; break;
case 'g': val = 1; saw_rgba = 1; break;
case 'b': val = 2; saw_rgba = 1; break;
case 'a': val = 3; saw_rgba = 1; break;
826
default: invalid_swizzle = 1; break;
827
828
829
} // switch
swizzle |= (val << (i * 2));
} // for
830
831
if (saw_xyzw && saw_rgba)
832
invalid_swizzle = 1;
833
834
else if (saw_rgba && !shader_is_pixel(ctx))
invalid_swizzle = 1;
835
} // else
836
837
838
839
if (invalid_swizzle)
fail(ctx, "Invalid swizzle");
840
841
842
843
844
845
846
*outtoken = ( ((((uint32) 1)) << 31) |
((((uint32) regnum) & 0x7ff) << 0) |
((((uint32) relative) & 0x1) << 13) |
((((uint32) swizzle) & 0xFF) << 16) |
((((uint32) srcmod) & 0xF) << 24) |
((((uint32) regtype) & 0x7) << 28) |
((((uint32) regtype) & 0x18) << 8) );
847
848
849
return retval;
} // parse_source_token_maybe_relative
850
851
852
static inline int parse_source_token(Context *ctx)
853
{
854
855
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
856
857
858
859
static int parse_args_NULL(Context *ctx)
{
860
return 1;
861
} // parse_args_NULL
862
863
864
static int parse_num(Context *ctx, const int floatok, uint32 *value)
865
866
{
union { float f; int32 si32; uint32 ui32; } cvt;
867
868
869
870
871
872
873
874
int negative = 0;
Token token = nexttoken(ctx);
if (token == ((Token) '-'))
{
negative = 1;
token = nexttoken(ctx);
} // if
875
876
if (token == TOKEN_INT_LITERAL)
877
{
878
879
int d = 0;
sscanf(ctx->token, "%d", &d);
880
881
882
883
if (floatok)
cvt.f = (float) ((negative) ? -d : d);
else
cvt.si32 = (int32) ((negative) ? -d : d);
884
885
} // if
else if (token == TOKEN_FLOAT_LITERAL)
886
{
887
if (!floatok)
888
{
889
890
891
fail(ctx, "Expected whole number");
*value = 0;
return 0;
892
} // if
893
sscanf(ctx->token, "%f", &cvt.f);
894
895
if (negative)
cvt.f = -cvt.f;
896
897
898
899
900
901
} // if
else
{
fail(ctx, "Expected number");
*value = 0;
return 0;
902
} // else
903
904
*value = cvt.ui32;
905
return 1;
906
907
908
909
910
} // parse_num
static int parse_args_DEFx(Context *ctx, const int isflt)
{
911
912
913
914
915
916
917
918
919
parse_destination_token(ctx);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
require_comma(ctx);
parse_num(ctx, isflt, &ctx->tokenbuf[ctx->tokenbufpos++]);
920
921
return 6;
} // parse_args_DEFx
922
923
924
925
926
927
static int parse_args_DEF(Context *ctx)
{
return parse_args_DEFx(ctx, 1);
} // parse_args_DEF
928
929
930
931
932
933
static int parse_args_DEFI(Context *ctx)
{
return parse_args_DEFx(ctx, 0);
} // parse_args_DEFI
934
935
936
937
static int parse_args_DEFB(Context *ctx)
{
938
939
parse_destination_token(ctx);
require_comma(ctx);
940
941
942
943
944
945
946
947
// !!! FIXME: do a TOKEN_TRUE and TOKEN_FALSE? Is this case-sensitive?
const Token token = nexttoken(ctx);
int bad = 0;
if (token != TOKEN_IDENTIFIER)
bad = 1;
else if (check_token_segment(ctx, "true"))
948
ctx->tokenbuf[ctx->tokenbufpos++] = 1;
949
else if (check_token_segment(ctx, "false"))
950
951
ctx->tokenbuf[ctx->tokenbufpos++] = 0;
else
952
953
954
955
956
957
bad = 1;
if (ctx->tokenlen != 0)
bad = 1;
if (bad)
958
fail(ctx, "Expected 'true' or 'false'");
959
960
961
return 3;
} // parse_args_DEFB
962
963
964
static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler)
965
966
{
int i;
967
static const char *samplerusagestrs[] = { "_2d", "_cube", "_volume" };
968
static const char *usagestrs[] = {
969
970
971
"_position", "_blendweight", "_blendindices", "_normal", "_psize",
"_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
"_color", "_fog", "_depth", "_sample"
972
};
973
974
for (i = 0; i < STATICARRAYLEN(usagestrs); i++)
975
{
976
if (check_token_segment(ctx, usagestrs[i]))
977
{
978
979
*issampler = 0;
*val = i;
980
return 1;
981
} // if
982
} // for
983
984
for (i = 0; i < STATICARRAYLEN(samplerusagestrs); i++)
985
{
986
if (check_token_segment(ctx, samplerusagestrs[i]))
987
{
988
989
*issampler = 1;
*val = i + 2;
990
return 1;
991
} // if
992
} // for
993
994
995
*issampler = 0;
*val = 0;
996
return 0;
997
} // parse_dcl_usage
998
999
1000
static int parse_args_DCL(Context *ctx)