/
mojoshader_assembler.c
1957 lines (1657 loc) · 57.4 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
// !!! FIXME: this should report all errors, not quit on the first fail().
12
13
14
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
15
#define DEBUG_TOKENIZER 0
16
17
// !!! FIXME: no #define support yet.
18
19
typedef struct TokenizerContext
20
21
22
23
24
25
26
{
const char *source;
int on_endline;
unsigned int linenum;
char prevchar;
char token[64];
char pushedback;
27
} TokenizerContext;
28
29
30
31
32
33
34
35
typedef struct SourcePos
{
const char *filename;
uint32 line;
} SourcePos;
36
37
// Context...this is state that changes as we assemble a shader...
38
typedef struct Context
39
40
41
42
43
{
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
const char *failstr;
44
TokenizerContext tctx;
45
MOJOSHADER_parsePhase parse_phase;
46
47
48
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
49
uint32 version_token;
50
51
52
uint32 tokenbuf[16];
int tokenbufpos;
DestArgInfo dest_arg;
53
uint32 *output;
54
SourcePos *token_to_source;
55
56
57
uint8 *ctab;
uint32 ctab_len;
uint32 ctab_allocation;
58
59
size_t output_len;
size_t output_allocation;
60
} Context;
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
// Convenience functions for allocators...
static inline int out_of_memory(Context *ctx)
{
if (ctx->failstr == NULL)
ctx->failstr = out_of_mem_str; // fail() would call malloc().
return FAIL;
} // 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
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
static int failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static int failf(Context *ctx, const char *fmt, ...)
{
if (ctx->failstr == NULL) // don't change existing error.
{
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)
{
va_start(ap, fmt);
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
va_end(ap);
ctx->failstr = failstr;
} // if
} // if
return FAIL;
} // failf
static inline int fail(Context *ctx, const char *reason)
{
return failf(ctx, "%s", reason);
} // fail
static inline int isfail(const Context *ctx)
{
return (ctx->failstr != NULL);
} // isfail
121
122
123
124
125
126
static inline int tokeq(const TokenizerContext *tctx, const char *token)
{
return (strcasecmp(tctx->token, token) == 0);
} // tokeq
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// 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
151
152
153
154
155
156
157
158
159
static int ui32fromstr(const char *str, uint32 *ui32)
{
//*ui32 = (uint32) atoi(minstr);
char *endptr = NULL;
const long val = strtol(str, &endptr, 10);
*ui32 = (uint32) val;
return ((val >= 0) && (*str != '\0') && (*endptr == '\0'));
} // ui32fromstr
160
161
162
163
164
165
166
167
static inline void add_token_sourcepos(Context *ctx, const size_t idx)
{
ctx->token_to_source[idx].line = ctx->tctx.linenum;
ctx->token_to_source[idx].filename = NULL;
} // add_token_sourcepos
168
169
170
171
172
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
173
174
175
176
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;
177
178
179
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
180
181
182
183
184
185
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;
186
187
ptr = Malloc(ctx, newsize * sizeof (SourcePos));
188
189
190
if (ptr == NULL)
return;
if (ctx->output_len > 0)
191
192
193
memcpy(ptr, ctx->token_to_source, ctx->output_len * sizeof (SourcePos));
Free(ctx, ctx->token_to_source);
ctx->token_to_source = (SourcePos *) ptr;
194
195
ctx->output_allocation = newsize;
196
197
} // if
198
ctx->output[ctx->output_len] = token;
199
add_token_sourcepos(ctx, ctx->output_len);
200
ctx->output_len++;
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
} // 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
242
static int tokenize_ctx(Context *ctx, TokenizerContext *tctx)
243
244
245
246
247
248
{
int idx = 0;
if (isfail(ctx))
return FAIL;
249
if (tctx->pushedback)
250
{
251
tctx->pushedback = 0;
252
253
254
return NOFAIL;
} // if
255
if (tctx->on_endline)
256
{
257
258
tctx->on_endline = 0;
tctx->linenum++; // passed a newline, update.
259
260
} // if
261
262
while (1)
{
263
// !!! FIXME: carefully crafted (but legal) comments can trigger this.
264
if (idx >= sizeof (tctx->token))
265
266
return fail(ctx, "buffer overflow");
267
char ch = *tctx->source;
268
269
270
271
if (ch == '\t')
ch = ' '; // collapse tabs into single spaces.
else if (ch == '\r')
{
272
if (tctx->source[1] == '\n')
273
274
275
276
continue; // ignore '\r' if this is "\r\n" ...
ch = '\n';
} // else if
277
if ((ch >= '0') && (ch <= '9'))
278
279
{
// starting a number, but rest of current token was not number.
280
if ((idx > 0) && ((tctx->prevchar < '0') || (tctx->prevchar > '9')))
281
{
282
tctx->token[idx++] = '\0';
283
284
285
286
287
288
return NOFAIL;
} // if
} // if
else
{
// starting a non-number, but rest of current token was numbers.
289
if ((idx > 0) && ((tctx->prevchar >= '0') && (tctx->prevchar <= '9')))
290
{
291
tctx->token[idx++] = '\0';
292
293
294
295
296
297
298
299
300
return NOFAIL;
} // if
} // else
switch (ch)
{
case '/':
case ';': // !!! FIXME: comment, right?
if (idx != 0) // finish off existing token.
301
tctx->token[idx] = '\0';
302
303
else
{
304
305
306
tctx->token[idx++] = ch;
tctx->source++;
if ((ch == '/') && (*tctx->source == '/'))
307
{
308
309
tctx->token[idx++] = '/';
tctx->source++;
310
} // if
311
tctx->token[idx++] = '\0';
312
313
314
315
} // else
return NOFAIL;
case ' ':
316
if (tctx->prevchar == ' ')
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
break; // multiple whitespace collapses into one.
// intentional fall-through...
case '_':
case '[':
case ']':
case '(':
case ')':
case '!':
case '+':
case '-':
case ',':
case '.':
case '\n':
if (idx != 0) // finish off existing token.
332
tctx->token[idx] = '\0';
333
334
335
else // this is a token in itself.
{
if (ch == '\n')
336
337
338
339
tctx->on_endline = 1;
tctx->source++;
tctx->token[idx++] = ch;
tctx->token[idx++] = '\0';
340
341
342
343
} // else
return NOFAIL;
case '\0':
344
tctx->token[idx] = '\0';
345
346
347
348
349
if (idx != 0) // had any chars? It's a token.
return NOFAIL;
return END_OF_STREAM;
default:
350
351
tctx->source++;
tctx->token[idx++] = ch;
352
353
354
break;
} // switch
355
tctx->prevchar = ch;
356
357
358
} // while
return fail(ctx, "???"); // shouldn't hit this.
359
} // tokenize_ctx
360
361
362
363
static inline int tokenize(Context *ctx)
{
364
365
const int rc = tokenize_ctx(ctx, &ctx->tctx);
366
367
368
369
370
#if DEBUG_TOKENIZER
printf("TOKENIZE: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
371
(ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
372
#endif
373
374
375
376
377
return rc;
} // tokenize
378
static int pushback_ctx(Context *ctx, TokenizerContext *tctx)
379
{
380
if (tctx->pushedback)
381
return fail(ctx, "BUG: Double pushback in parser");
382
else
383
tctx->pushedback = 1;
384
385
return NOFAIL;
386
387
388
389
390
391
392
393
394
395
396
397
398
399
}
static inline int pushback(Context *ctx)
{
const int rc = pushback_ctx(ctx, &ctx->tctx);
#if DEBUG_TOKENIZER
printf("PUSHBACK: %s\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???");
#endif
return rc;
400
401
402
} // pushback
403
404
405
static int nexttoken_ctx(Context *ctx, TokenizerContext *tctx,
const int ignoreeol, const int ignorewhitespace,
const int eolok, const int eosok)
406
407
408
{
int rc = NOFAIL;
409
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
410
{
411
if (tokeq(tctx, "\n"))
412
413
414
415
416
417
418
{
if (ignoreeol)
continue;
else if (!eolok)
return fail(ctx, "Unexpected EOL");
} // if
419
else if (tokeq(tctx, " "))
420
421
422
423
424
425
{
if (ignorewhitespace)
continue;
} // else if
// skip comments...
426
else if (tokeq(tctx, "//") || tokeq(tctx, ";"))
427
{
428
while ((rc = tokenize_ctx(ctx, tctx)) == NOFAIL)
429
{
430
if (tokeq(tctx, "\n"))
431
{
432
pushback_ctx(ctx, tctx);
433
break;
434
} // if
435
} // while
436
continue; // pick up from newline, go again.
437
438
439
440
441
} // if
break;
} // while
442
443
444
445
446
447
448
449
450
451
452
453
454
455
if ((rc == END_OF_STREAM) && (!eosok))
return fail(ctx, "Unexpected EOF");
return rc;
} // nexttoken_ctx
static inline int nexttoken(Context *ctx, const int ignoreeol,
const int ignorewhitespace, const int eolok,
const int eosok)
{
const int rc = nexttoken_ctx(ctx, &ctx->tctx, ignoreeol,
ignorewhitespace, eolok, eosok);
456
457
458
459
460
#if DEBUG_TOKENIZER
printf("NEXTTOKEN: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
461
(ctx->tctx.token[0] == '\n') ? "\\n" : ctx->tctx.token);
462
463
464
465
466
467
468
469
#endif
return rc;
} // nexttoken
static int require_endline(Context *ctx)
{
470
TokenizerContext *tctx = &ctx->tctx;
471
472
473
474
475
const int rc = nexttoken(ctx, 0, 1, 1, 1);
if (rc == FAIL)
return FAIL;
else if (rc == END_OF_STREAM)
return NOFAIL; // we'll call this an EOL.
476
else if (!tokeq(tctx, "\n"))
477
478
479
480
481
return fail(ctx, "Endline expected");
return NOFAIL;
} // require_endline
482
static int require_comma(Context *ctx)
483
{
484
TokenizerContext *tctx = &ctx->tctx;
485
486
487
const int rc = nexttoken(ctx, 0, 1, 0, 0);
if (rc == FAIL)
return FAIL;
488
else if (!tokeq(tctx, ","))
489
return fail(ctx, "Comma expected");
490
return NOFAIL;
491
} // require_comma
492
493
494
495
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
496
TokenizerContext *tctx = &ctx->tctx;
497
498
499
500
501
502
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
503
if (tokeq(tctx, "r"))
504
regtype = REG_TYPE_TEMP;
505
else if (tokeq(tctx, "v"))
506
regtype = REG_TYPE_INPUT;
507
else if (tokeq(tctx, "c"))
508
regtype = REG_TYPE_CONST;
509
else if (tokeq(tctx, "i"))
510
regtype = REG_TYPE_CONSTINT;
511
else if (tokeq(tctx, "b"))
512
regtype = REG_TYPE_CONSTBOOL;
513
else if (tokeq(tctx, "oC"))
514
regtype = REG_TYPE_COLOROUT;
515
else if (tokeq(tctx, "s"))
516
regtype = REG_TYPE_SAMPLER;
517
else if (tokeq(tctx, "oD"))
518
regtype = REG_TYPE_ATTROUT;
519
else if (tokeq(tctx, "l"))
520
regtype = REG_TYPE_LABEL;
521
else if (tokeq(tctx, "p"))
522
regtype = REG_TYPE_PREDICATE;
523
else if (tokeq(tctx, "oDepth"))
524
525
526
527
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
528
else if (tokeq(tctx, "aL"))
529
530
531
532
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
533
else if (tokeq(tctx, "o"))
534
535
536
537
538
{
if (!shader_is_vertex(ctx) || !shader_version_atleast(ctx, 3, 0))
return fail(ctx, "Output register not valid in this shader type");
regtype = REG_TYPE_OUTPUT;
} // else if
539
else if (tokeq(tctx, "oT"))
540
{
541
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
542
543
544
return fail(ctx, "Output register not valid in this shader type");
regtype = REG_TYPE_OUTPUT;
} // else if
545
else if (tokeq(tctx, "a"))
546
547
548
549
550
{
if (!shader_is_vertex(ctx))
return fail(ctx, "Address register only valid in vertex shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
551
else if (tokeq(tctx, "t"))
552
553
554
555
556
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Address register only valid in pixel shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
557
else if (tokeq(tctx, "vPos"))
558
559
560
561
562
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
563
else if (tokeq(tctx, "vFace"))
564
565
566
567
568
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
neednum = 0;
} // else if
569
else if (tokeq(tctx, "oPos"))
570
571
572
573
574
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
575
else if (tokeq(tctx, "oFog"))
576
577
578
579
580
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
581
else if (tokeq(tctx, "oPts"))
582
583
584
585
586
587
588
589
590
591
592
593
594
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POINT_SIZE;
neednum = 0;
} // else if
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
else
{
return fail(ctx, "expected register type");
} // else
595
596
if (neednum)
{
597
598
599
600
601
602
// Make a temp TokenizerContext, since we need to skip whitespace here,
// but if the next non-whitespace token isn't '[', we'll want to get
// that whitespace back.
TokenizerContext tmptctx;
memcpy(&tmptctx, tctx, sizeof (TokenizerContext));
if (nexttoken_ctx(ctx, &tmptctx, 0, 1, 1, 1) == FAIL)
603
return FAIL;
604
else if (tokeq(&tmptctx, "["))
605
606
607
neednum = 0;
} // if
608
609
610
611
612
if (neednum)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
613
uint32 ui32 = 0;
614
if (!ui32fromstr(tctx->token, &ui32))
615
616
return fail(ctx, "Invalid register index");
regnum = (int) ui32;
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
} // 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
{
return fail(ctx, "Invalid const register index");
} // else
} // if
*rtype = regtype;
*rnum = regnum;
return NOFAIL;
} // parse_register_name
655
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val)
656
{
657
658
659
660
661
if (info->result_shift != 0)
return fail(ctx, "Multiple result shift modifiers");
info->result_shift = val;
return NOFAIL;
} // set_result_shift
662
663
664
static int parse_destination_token(Context *ctx)
665
{
666
667
668
TokenizerContext *tctx = &ctx->tctx;
DestArgInfo *info = &ctx->dest_arg;
669
memset(info, '\0', sizeof (DestArgInfo));
670
671
672
673
674
675
// See if there are destination modifiers on the instruction itself...
while (1)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
676
else if (tokeq(tctx, " "))
677
break; // done with modifiers.
678
else if (!tokeq(tctx, "_"))
679
680
681
return fail(ctx, "Expected modifier or whitespace");
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
682
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
683
else if (tokeq(tctx, "x"))
684
685
686
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
687
else if (tokeq(tctx, "2"))
688
set_result_shift(ctx, info, 0x1);
689
else if (tokeq(tctx, "4"))
690
set_result_shift(ctx, info, 0x2);
691
else if (tokeq(tctx, "8"))
692
693
694
695
696
set_result_shift(ctx, info, 0x3);
else
return fail(ctx, "Expected modifier");
} // else if
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
697
else if (tokeq(tctx, "d"))
698
699
700
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
701
else if (tokeq(tctx, "8"))
702
set_result_shift(ctx, info, 0xD);
703
else if (tokeq(tctx, "4"))
704
set_result_shift(ctx, info, 0xE);
705
else if (tokeq(tctx, "2"))
706
707
708
709
set_result_shift(ctx, info, 0xF);
else
return fail(ctx, "Expected modifier");
} // else if
710
else if (tokeq(tctx, "sat"))
711
info->result_mod |= MOD_SATURATE;
712
else if (tokeq(tctx, "pp"))
713
info->result_mod |= MOD_PP;
714
else if (tokeq(tctx, "centroid"))
715
716
717
718
719
info->result_mod |= MOD_CENTROID;
else
return fail(ctx, "Expected modifier");
} // while
720
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
721
722
723
return FAIL;
// !!! FIXME: predicates.
724
if (tokeq(tctx, "("))
725
726
727
728
729
730
return fail(ctx, "Predicates unsupported at this time");
pushback(ctx); // parse_register_name calls nexttoken().
if (parse_register_name(ctx, &info->regtype, &info->regnum) == FAIL)
return FAIL;
731
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
732
733
734
735
return FAIL;
// !!! FIXME: can dest registers do relative addressing?
736
int implicit_writemask = 0;
737
if (!tokeq(tctx, "."))
738
{
739
implicit_writemask = 1;
740
741
742
743
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
744
745
746
// !!! 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) )
747
return fail(ctx, "Writemask specified for scalar register");
748
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
749
return FAIL;
750
else if (tokeq(tctx, ""))
751
752
753
return fail(ctx, "Invalid writemask");
else
{
754
char *ptr = tctx->token;
755
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
756
757
758
759
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++; }
760
if ((ptr == tctx->token) && (shader_is_pixel(ctx)))
761
{
762
763
764
765
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++; }
766
767
768
769
770
771
772
773
774
775
776
} // if
if (*ptr != '\0')
return fail(ctx, "Invalid writemask");
info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );
} // else
777
778
779
780
781
782
783
784
// !!! 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) )
return fail(ctx, "Writemask specified for scalar register");
} // if
785
786
787
788
789
790
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
ctx->tokenbuf[ctx->tokenbufpos++] =
791
( ((((uint32) 1)) << 31) |
792
793
794
795
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
796
((((uint32) info->writemask) & 0xF) << 16) |
797
798
799
800
801
802
803
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
804
805
806
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
807
{
808
809
810
811
812
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
813
814
815
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
816
{
817
TokenizerContext *tctx = &ctx->tctx;
818
819
820
821
822
823
824
825
826
827
828
int retval = 1;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
// mark this now, so optional relative addressing token is placed second.
uint32 *token = &ctx->tokenbuf[ctx->tokenbufpos++];
SourceMod srcmod = SRCMOD_NONE;
int negate = 0;
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
829
return FAIL;
830
else if (tokeq(tctx, "1"))
831
832
833
{
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
834
else if (!tokeq(tctx, "-"))
835
836
837
838
return fail(ctx, "Unexpected value");
else
srcmod = SRCMOD_COMPLEMENT;
} // else
839
else if (tokeq(tctx, "!"))
840
srcmod = SRCMOD_NOT;
841
else if (tokeq(tctx, "-"))
842
843
844
negate = 1;
else
pushback(ctx);
845
846
847
848
849
850
851
RegisterType regtype;
int regnum;
if (parse_register_name(ctx, ®type, ®num) == FAIL)
return FAIL;
else if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
852
else if (!tokeq(tctx, "_"))
853
854
855
pushback(ctx);
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
856
else if (tokeq(tctx, "bias"))
857
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
858
else if (tokeq(tctx, "bx2"))
859
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
860
else if (tokeq(tctx, "x2"))
861
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
862
else if (tokeq(tctx, "dz"))
863
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
864
else if (tokeq(tctx, "dw"))
865
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
866
else if (tokeq(tctx, "abs"))
867
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
868
869
else
return fail(ctx, "Invalid source modifier");
870
871
872
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
873
874
uint32 relative = 0;
875
if (!tokeq(tctx, "["))
876
877
878
879
880
881
882
883
884
885
pushback(ctx); // not relative addressing?
else if (!relok)
return fail(ctx, "Relative addressing not permitted here.");
else
{
const int rc = parse_source_token_maybe_relative(ctx, 0);
if (rc == FAIL)
return FAIL;
retval += rc;
relative = 1;
886
887
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
888
else if (!tokeq(tctx, "+"))
889
890
891
892
893
894
895
896
pushback(ctx);
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else
{
if (regnum != 0) // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
fail(ctx, "Relative addressing with explicit register number.");
uint32 ui32 = 0;
897
if (!ui32fromstr(tctx->token, &ui32))
898
899
900
901
return fail(ctx, "Invalid relative addressing offset");
regnum += (int) ui32;
} // else
902
903
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
904
else if (!tokeq(tctx, "]"))
905
906
return fail(ctx, "Expected ']'");
} // else
907
908
909
910
911
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
uint32 swizzle = 0;
912
if (!tokeq(tctx, "."))
913
914
915
916
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
917
else if (scalar_register(ctx->shader_type, regtype, regnum))
918
919
return fail(ctx, "Swizzle specified for scalar register");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
920
return FAIL;
921
else if (tokeq(tctx, ""))
922
923
924
925
return fail(ctx, "Invalid swizzle");
else
{
// deal with shortened form (.x = .xxxx, etc).
926
927
928
929
930
931
932
if (tctx->token[1] == '\0')
tctx->token[1] = tctx->token[2] = tctx->token[3] = tctx->token[0];
else if (tctx->token[2] == '\0')
tctx->token[2] = tctx->token[3] = tctx->token[1];
else if (tctx->token[3] == '\0')
tctx->token[3] = tctx->token[2];
else if (tctx->token[4] != '\0')
933
return fail(ctx, "Invalid swizzle");
934
tctx->token[4] = '\0';
935
936
937
938
uint32 val;
int saw_xyzw = 0;
int saw_rgba = 0;
939
int i;
940
941
for (i = 0; i < 4; i++)
{
942
const int component = (int) tctx->token[i];
943
944
945
946
947
948
949
950
951
952
953
954
955
956
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;
default: return fail(ctx, "Invalid swizzle");
} // switch
swizzle |= (val << (i * 2));
} // for
957
958
959
960
if (saw_xyzw && saw_rgba)
return fail(ctx, "Invalid swizzle");
} // else
961
962
963
964
965
966
967
968
*token = ( ((((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) );
969
970
971
return retval;
} // parse_source_token_maybe_relative
972
973
974
static inline int parse_source_token(Context *ctx)
975
{
976
977
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
978
979
980
981
982
983
static int parse_args_NULL(Context *ctx)
{
return (isfail(ctx) ? FAIL : 1);
} // parse_args_NULL
984
985
986
987
static int parse_num(Context *ctx, const int floatok, uint32 *token)
{
988
TokenizerContext *tctx = &ctx->tctx;
989
990
991
int32 negative = 1;
union { float f; int32 si32; uint32 ui32; } cvt;
cvt.si32 = 0;
992
993
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
994
return FAIL;
995
else if (tokeq(tctx, "-"))
996
997
negative = -1;
else
998
pushback(ctx);
999
1000
uint32 val = 0;