Skip to content

Latest commit

 

History

History
1654 lines (1401 loc) · 48.3 KB

mojoshader_assembler.c

File metadata and controls

1654 lines (1401 loc) · 48.3 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
/**
* 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.
*/
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
Dec 10, 2008
Dec 10, 2008
13
#define DEBUG_TOKENIZER 0
Dec 10, 2008
Dec 10, 2008
15
// !!! FIXME: no #define support yet.
16
17
18
19
20
21
22
23
24
25
26
typedef struct Context Context;
// Context...this is state that changes as we assemble a shader...
struct Context
{
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
const char *failstr;
const char *source;
Dec 10, 2008
Dec 10, 2008
27
int started_parsing;
28
29
30
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
Dec 8, 2008
Dec 8, 2008
31
int on_endline;
32
33
34
35
36
37
38
unsigned int linenum;
char prevchar;
char token[64];
char pushedback;
uint32 tokenbuf[16];
int tokenbufpos;
DestArgInfo dest_arg;
Dec 8, 2008
Dec 8, 2008
39
uint32 *output;
Dec 10, 2008
Dec 10, 2008
40
uint32 *token_to_line;
Dec 8, 2008
Dec 8, 2008
41
42
size_t output_len;
size_t output_allocation;
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
};
// 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
// 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
Dec 10, 2008
Dec 10, 2008
128
129
130
131
132
133
134
135
136
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
137
138
139
140
141
142
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
Dec 8, 2008
Dec 8, 2008
143
144
145
146
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;
Dec 10, 2008
Dec 10, 2008
147
148
149
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
Dec 8, 2008
Dec 8, 2008
150
151
152
153
154
155
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;
Dec 10, 2008
Dec 10, 2008
156
157
158
159
160
161
162
163
164
165
ptr = Malloc(ctx, newsize * sizeof (uint32));
if (ptr == NULL)
return;
if (ctx->output_len > 0)
memcpy(ptr, ctx->token_to_line, ctx->output_len * sizeof (uint32));
Free(ctx, ctx->token_to_line);
ctx->token_to_line = (uint32 *) ptr;
ctx->output_allocation = newsize;
Dec 8, 2008
Dec 8, 2008
166
167
} // if
Dec 10, 2008
Dec 10, 2008
168
169
170
ctx->output[ctx->output_len] = token;
ctx->token_to_line[ctx->output_len] = ctx->linenum;
ctx->output_len++;
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
} // 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
static int _tokenize(Context *ctx)
{
int idx = 0;
if (isfail(ctx))
return FAIL;
if (ctx->pushedback)
{
ctx->pushedback = 0;
return NOFAIL;
} // if
Dec 8, 2008
Dec 8, 2008
225
226
227
228
229
230
if (ctx->on_endline)
{
ctx->on_endline = 0;
ctx->linenum++; // passed a newline, update.
} // if
231
232
while (1)
{
Dec 10, 2008
Dec 10, 2008
233
// !!! FIXME: carefully crafted (but legal) comments can trigger this.
234
235
236
237
238
239
240
241
242
243
244
245
246
if (idx >= sizeof (ctx->token))
return fail(ctx, "buffer overflow");
char ch = *ctx->source;
if (ch == '\t')
ch = ' '; // collapse tabs into single spaces.
else if (ch == '\r')
{
if (ctx->source[1] == '\n')
continue; // ignore '\r' if this is "\r\n" ...
ch = '\n';
} // else if
Dec 10, 2008
Dec 10, 2008
247
if ((ch >= '0') && (ch <= '9'))
248
249
250
251
252
253
254
255
256
257
258
{
// starting a number, but rest of current token was not number.
if ((idx > 0) && ((ctx->prevchar < '0') || (ctx->prevchar > '9')))
{
ctx->token[idx++] = '\0';
return NOFAIL;
} // if
} // if
else
{
// starting a non-number, but rest of current token was numbers.
Dec 10, 2008
Dec 10, 2008
259
if ((idx > 0) && ((ctx->prevchar >= '0') && (ctx->prevchar <= '9')))
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
{
ctx->token[idx++] = '\0';
return NOFAIL;
} // if
} // else
switch (ch)
{
case '/':
case ';': // !!! FIXME: comment, right?
if (idx != 0) // finish off existing token.
ctx->token[idx] = '\0';
else
{
ctx->token[idx++] = ch;
ctx->source++;
Dec 10, 2008
Dec 10, 2008
276
if ((ch == '/') && (*ctx->source == '/'))
277
278
279
280
281
282
283
284
285
{
ctx->token[idx++] = '/';
ctx->source++;
} // if
ctx->token[idx++] = '\0';
} // else
return NOFAIL;
case ' ':
Dec 8, 2008
Dec 8, 2008
286
if (ctx->prevchar == ' ')
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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.
ctx->token[idx] = '\0';
else // this is a token in itself.
{
if (ch == '\n')
Dec 8, 2008
Dec 8, 2008
306
ctx->on_endline = 1;
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
ctx->source++;
ctx->token[idx++] = ch;
ctx->token[idx++] = '\0';
} // else
return NOFAIL;
case '\0':
ctx->token[idx] = '\0';
if (idx != 0) // had any chars? It's a token.
return NOFAIL;
return END_OF_STREAM;
default:
ctx->source++;
ctx->token[idx++] = ch;
break;
} // switch
ctx->prevchar = ch;
} // while
return fail(ctx, "???"); // shouldn't hit this.
} // _tokenize
static inline int tokenize(Context *ctx)
{
const int rc = _tokenize(ctx);
#if DEBUG_TOKENIZER
printf("TOKENIZE: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
Dec 10, 2008
Dec 10, 2008
340
(ctx->token[0] == '\n') ? "\\n" : ctx->token);
341
342
343
344
345
#endif
return rc;
} // tokenize
Dec 10, 2008
Dec 10, 2008
346
static inline int pushback(Context *ctx)
347
348
349
350
351
352
{
#if DEBUG_TOKENIZER
printf("PUSHBACK\n");
#endif
if (ctx->pushedback)
Dec 10, 2008
Dec 10, 2008
353
return fail(ctx, "BUG: Double pushback in parser");
354
355
else
ctx->pushedback = 1;
Dec 10, 2008
Dec 10, 2008
356
357
return NOFAIL;
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
} // pushback
static int nexttoken(Context *ctx, const int ignoreeol,
const int ignorewhitespace, const int eolok,
const int eosok)
{
int rc = NOFAIL;
while ((rc = tokenize(ctx)) == NOFAIL)
{
if (strcmp(ctx->token, "\n") == 0)
{
if (ignoreeol)
continue;
else if (!eolok)
return fail(ctx, "Unexpected EOL");
} // if
else if (strcmp(ctx->token, " ") == 0)
{
if (ignorewhitespace)
continue;
} // else if
// skip comments...
else if ((strcmp(ctx->token, "//") == 0) || (strcmp(ctx->token, ";") == 0))
{
while ((rc = tokenize(ctx)) == NOFAIL)
{
if (strcmp(ctx->token, "\n") == 0)
Dec 10, 2008
Dec 10, 2008
389
390
{
pushback(ctx);
Dec 10, 2008
Dec 10, 2008
392
} // if
393
} // while
Dec 10, 2008
Dec 10, 2008
394
continue; // pick up from newline, go again.
395
396
397
398
399
400
401
402
403
404
} // if
break;
} // while
#if DEBUG_TOKENIZER
printf("NEXTTOKEN: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
Dec 10, 2008
Dec 10, 2008
405
(ctx->token[0] == '\n') ? "\\n" : ctx->token);
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#endif
if ((rc == END_OF_STREAM) && (!eosok))
return fail(ctx, "Unexpected EOF");
return rc;
} // nexttoken
static int require_endline(Context *ctx)
{
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.
else if (strcmp(ctx->token, "\n") != 0)
return fail(ctx, "Endline expected");
return NOFAIL;
} // require_endline
Dec 8, 2008
Dec 8, 2008
428
static int require_comma(Context *ctx)
Dec 8, 2008
Dec 8, 2008
430
431
432
433
434
const int rc = nexttoken(ctx, 0, 1, 0, 0);
if (rc == FAIL)
return FAIL;
else if (strcmp(ctx->token, ",") != 0)
return fail(ctx, "Comma expected");
435
return NOFAIL;
Dec 8, 2008
Dec 8, 2008
436
} // require_comma
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
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
// !!! FIXME: some of these registers are only valid for some shader types.
int neednum = 1;
int regnum = 0;
const char *t = ctx->token;
RegisterType regtype = REG_TYPE_TEMP;
if (strcasecmp(t, "r") == 0)
regtype = REG_TYPE_TEMP;
else if (strcasecmp(t, "v") == 0)
regtype = REG_TYPE_INPUT;
else if (strcasecmp(t, "c") == 0)
regtype = REG_TYPE_CONST;
else if (strcasecmp(t, "i") == 0)
regtype = REG_TYPE_CONSTINT;
else if (strcasecmp(t, "b") == 0)
regtype = REG_TYPE_CONSTBOOL;
else if (strcasecmp(t, "oC") == 0)
regtype = REG_TYPE_COLOROUT;
else if (strcasecmp(t, "oDepth") == 0)
regtype = REG_TYPE_DEPTHOUT;
else if (strcasecmp(t, "s") == 0)
regtype = REG_TYPE_SAMPLER;
else if (strcasecmp(t, "oD") == 0)
regtype = REG_TYPE_ATTROUT;
else if (strcasecmp(t, "l") == 0)
regtype = REG_TYPE_LABEL;
else if (strcasecmp(t, "p") == 0)
regtype = REG_TYPE_PREDICATE;
else if (strcasecmp(t, "aL") == 0)
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
else if (strcasecmp(t, "o") == 0)
{
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
else if (strcasecmp(t, "oT") == 0)
{
Dec 12, 2008
Dec 12, 2008
484
if (shader_is_vertex(ctx) && shader_version_atleast(ctx, 3, 0))
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
return fail(ctx, "Output register not valid in this shader type");
regtype = REG_TYPE_OUTPUT;
} // else if
else if (strcasecmp(t, "a") == 0)
{
if (!shader_is_vertex(ctx))
return fail(ctx, "Address register only valid in vertex shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
else if (strcasecmp(t, "t") == 0)
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Address register only valid in pixel shaders.");
regtype = REG_TYPE_ADDRESS;
} // else if
else if (strcasecmp(t, "vPos") == 0)
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
else if (strcasecmp(t, "vFace") == 0)
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
neednum = 0;
} // else if
else if (strcasecmp(t, "oPos") == 0)
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
else if (strcasecmp(t, "oFog") == 0)
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
else if (strcasecmp(t, "oPts") == 0)
{
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
Dec 10, 2008
Dec 10, 2008
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
if (neednum)
{
// cheat the pushback.
const char *origsrc = ctx->source;
const int origonendline = ctx->on_endline;
const int origlinenum = ctx->linenum;
const int origprevchar = ctx->prevchar;
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "[") == 0)
neednum = 0;
ctx->source = origsrc;
ctx->on_endline = origonendline;
ctx->linenum = origlinenum;
ctx->prevchar = origprevchar;
} // if
557
558
559
560
561
if (neednum)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 10, 2008
Dec 10, 2008
562
563
564
565
uint32 ui32 = 0;
if (!ui32fromstr(ctx->token, &ui32))
return fail(ctx, "Invalid register index");
regnum = (int) ui32;
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
} // 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
Dec 8, 2008
Dec 8, 2008
604
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val)
Dec 8, 2008
Dec 8, 2008
606
607
608
609
610
if (info->result_shift != 0)
return fail(ctx, "Multiple result shift modifiers");
info->result_shift = val;
return NOFAIL;
} // set_result_shift
Dec 8, 2008
Dec 8, 2008
612
613
614
static int parse_destination_token(Context *ctx, DestArgInfo *info)
{
Dec 12, 2008
Dec 12, 2008
615
memset(info, '\0', sizeof (DestArgInfo));
616
617
618
619
620
621
622
623
624
625
626
627
// See if there are destination modifiers on the instruction itself...
while (1)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, " ") == 0)
break; // done with modifiers.
else if (strcmp(ctx->token, "_") != 0)
return fail(ctx, "Expected modifier or whitespace");
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
Dec 12, 2008
Dec 12, 2008
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
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
else if (strcasecmp(ctx->token, "x") == 0)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "2") == 0)
set_result_shift(ctx, info, 0x1);
else if (strcmp(ctx->token, "4") == 0)
set_result_shift(ctx, info, 0x2);
else if (strcmp(ctx->token, "8") == 0)
set_result_shift(ctx, info, 0x3);
else
return fail(ctx, "Expected modifier");
} // else if
// !!! FIXME: this can be cleaned up when tokenizer is fixed.
else if (strcasecmp(ctx->token, "d") == 0)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "8") == 0)
set_result_shift(ctx, info, 0xD);
else if (strcmp(ctx->token, "4") == 0)
set_result_shift(ctx, info, 0xE);
else if (strcmp(ctx->token, "2") == 0)
set_result_shift(ctx, info, 0xF);
else
return fail(ctx, "Expected modifier");
} // else if
656
657
658
659
660
661
662
663
664
665
else if (strcasecmp(ctx->token, "sat") == 0)
info->result_mod |= MOD_SATURATE;
else if (strcasecmp(ctx->token, "pp") == 0)
info->result_mod |= MOD_PP;
else if (strcasecmp(ctx->token, "centroid") == 0)
info->result_mod |= MOD_CENTROID;
else
return fail(ctx, "Expected modifier");
} // while
Dec 8, 2008
Dec 8, 2008
666
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
667
668
669
670
671
672
673
674
675
676
return FAIL;
// !!! FIXME: predicates.
if (strcmp(ctx->token, "(") == 0)
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;
Dec 8, 2008
Dec 8, 2008
677
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
678
679
680
681
682
683
684
685
686
687
return FAIL;
// !!! FIXME: can dest registers do relative addressing?
if (strcmp(ctx->token, ".") != 0)
{
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
Dec 10, 2008
Dec 10, 2008
688
else if (scalar_register(ctx->shader_type, info->regtype, info->regnum))
689
return fail(ctx, "Writemask specified for scalar register");
Dec 8, 2008
Dec 8, 2008
690
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
691
692
693
694
695
696
697
return FAIL;
else if (ctx->token[0] == '\0')
return fail(ctx, "Invalid writemask");
else
{
char *ptr = ctx->token;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
Dec 8, 2008
Dec 8, 2008
698
699
700
701
702
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++; }
if ((ptr == ctx->token) && (shader_is_pixel(ctx)))
Dec 8, 2008
Dec 8, 2008
704
705
706
707
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++; }
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
} // 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
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
ctx->tokenbuf[ctx->tokenbufpos++] =
Dec 8, 2008
Dec 8, 2008
725
( ((((uint32) 1)) << 31) |
726
727
728
729
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
Dec 11, 2008
Dec 11, 2008
730
((((uint32) info->writemask) & 0xF) << 16) |
731
732
733
734
735
736
737
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
Dec 10, 2008
Dec 10, 2008
738
739
740
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
Dec 8, 2008
Dec 8, 2008
742
743
744
745
746
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
Dec 8, 2008
Dec 8, 2008
749
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
Dec 8, 2008
Dec 8, 2008
751
752
753
754
755
756
757
758
759
760
761
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)
762
return FAIL;
Dec 8, 2008
Dec 8, 2008
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
else if (strcmp(ctx->token, "1") == 0)
{
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "-") != 0)
return fail(ctx, "Unexpected value");
else
srcmod = SRCMOD_COMPLEMENT;
} // else
else if (strcmp(ctx->token, "!") == 0)
srcmod = SRCMOD_NOT;
else if (strcmp(ctx->token, "-") == 0)
negate = 1;
else
pushback(ctx);
Dec 8, 2008
Dec 8, 2008
779
780
781
782
783
784
785
786
787
788
789
RegisterType regtype;
int regnum;
if (parse_register_name(ctx, &regtype, &regnum) == FAIL)
return FAIL;
else if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "_") != 0)
pushback(ctx);
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else if (strcasecmp(ctx->token, "bias") == 0)
Dec 10, 2008
Dec 10, 2008
790
set_source_mod(ctx, negate, SRCMOD_BIAS, SRCMOD_BIASNEGATE, &srcmod);
Dec 8, 2008
Dec 8, 2008
791
else if (strcasecmp(ctx->token, "bx2") == 0)
Dec 10, 2008
Dec 10, 2008
792
set_source_mod(ctx, negate, SRCMOD_SIGN, SRCMOD_SIGNNEGATE, &srcmod);
Dec 8, 2008
Dec 8, 2008
793
else if (strcasecmp(ctx->token, "x2") == 0)
Dec 10, 2008
Dec 10, 2008
794
set_source_mod(ctx, negate, SRCMOD_X2, SRCMOD_X2NEGATE, &srcmod);
Dec 8, 2008
Dec 8, 2008
795
else if (strcasecmp(ctx->token, "dz") == 0)
Dec 10, 2008
Dec 10, 2008
796
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
Dec 8, 2008
Dec 8, 2008
797
else if (strcasecmp(ctx->token, "dw") == 0)
Dec 10, 2008
Dec 10, 2008
798
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
Dec 8, 2008
Dec 8, 2008
799
else if (strcasecmp(ctx->token, "abs") == 0)
Dec 10, 2008
Dec 10, 2008
800
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
Dec 8, 2008
Dec 8, 2008
801
802
else
return fail(ctx, "Invalid source modifier");
Dec 8, 2008
Dec 8, 2008
804
805
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
Dec 8, 2008
Dec 8, 2008
807
808
809
810
811
812
813
814
815
816
817
818
uint32 relative = 0;
if (strcmp(ctx->token, "[") != 0)
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;
Dec 10, 2008
Dec 10, 2008
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "+") != 0)
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;
if (!ui32fromstr(ctx->token, &ui32))
return fail(ctx, "Invalid relative addressing offset");
regnum += (int) ui32;
} // else
Dec 8, 2008
Dec 8, 2008
835
836
837
838
839
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "]") != 0)
return fail(ctx, "Expected ']'");
} // else
Dec 8, 2008
Dec 8, 2008
841
842
843
844
845
846
847
848
849
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
uint32 swizzle = 0;
if (strcmp(ctx->token, ".") != 0)
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
Dec 10, 2008
Dec 10, 2008
850
else if (scalar_register(ctx->shader_type, regtype, regnum))
Dec 8, 2008
Dec 8, 2008
851
852
return fail(ctx, "Swizzle specified for scalar register");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
853
return FAIL;
Dec 8, 2008
Dec 8, 2008
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
else if (ctx->token[0] == '\0')
return fail(ctx, "Invalid swizzle");
else
{
// deal with shortened form (.x = .xxxx, etc).
if (ctx->token[1] == '\0')
ctx->token[1] = ctx->token[2] = ctx->token[3] = ctx->token[0];
else if (ctx->token[2] == '\0')
ctx->token[2] = ctx->token[3] = ctx->token[1];
else if (ctx->token[3] == '\0')
ctx->token[3] = ctx->token[2];
else if (ctx->token[4] != '\0')
return fail(ctx, "Invalid swizzle");
ctx->token[4] = '\0';
uint32 val;
int saw_xyzw = 0;
int saw_rgba = 0;
Dec 10, 2008
Dec 10, 2008
872
int i;
Dec 8, 2008
Dec 8, 2008
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
for (i = 0; i < 4; i++)
{
const int component = (int) ctx->token[i];
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
Dec 8, 2008
Dec 8, 2008
891
892
893
if (saw_xyzw && saw_rgba)
return fail(ctx, "Invalid swizzle");
} // else
Dec 8, 2008
Dec 8, 2008
895
896
897
898
899
900
901
*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) );
Dec 8, 2008
Dec 8, 2008
903
904
return retval;
} // parse_source_token_maybe_relative
Dec 8, 2008
Dec 8, 2008
907
static inline int parse_source_token(Context *ctx)
Dec 8, 2008
Dec 8, 2008
909
910
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
Dec 8, 2008
Dec 8, 2008
912
913
914
915
916
static int parse_args_NULL(Context *ctx)
{
return (isfail(ctx) ? FAIL : 1);
} // parse_args_NULL
Dec 8, 2008
Dec 8, 2008
919
920
921
922
923
static int parse_num(Context *ctx, const int floatok, uint32 *token)
{
int32 negative = 1;
union { float f; int32 si32; uint32 ui32; } cvt;
cvt.si32 = 0;
Dec 8, 2008
Dec 8, 2008
925
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
926
return FAIL;
Dec 8, 2008
Dec 8, 2008
927
928
929
else if (strcmp(ctx->token, "-") == 0)
negative = -1;
else
930
pushback(ctx);
Dec 8, 2008
Dec 8, 2008
931
932
933
934
935
936
937
938
uint32 val = 0;
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else if (!ui32fromstr(ctx->token, &val))
return fail(ctx, "Expected number");
uint32 fraction = 0;
Dec 10, 2008
Dec 10, 2008
939
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
Dec 8, 2008
Dec 8, 2008
940
941
942
943
944
945
return FAIL;
else if (strcmp(ctx->token, ".") != 0)
pushback(ctx); // whole number
else if (!floatok)
return fail(ctx, "Expected whole number");
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
946
return FAIL;
Dec 8, 2008
Dec 8, 2008
947
948
949
else if (!ui32fromstr(ctx->token, &fraction))
return fail(ctx, "Expected number");
Dec 10, 2008
Dec 10, 2008
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
uint32 exponent = 0;
int negexp = 0;
if (nexttoken(ctx, 0, 1, 1, 1) == FAIL)
return FAIL;
else if (strcmp(ctx->token, "e") != 0)
pushback(ctx);
else if (!floatok)
return fail(ctx, "Exponent on whole number"); // !!! FIXME: illegal?
else if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else
{
if (strcmp(ctx->token, "-") != 0)
pushback(ctx);
else
negexp = 1;
if (nexttoken(ctx, 0, 1, 0, 0) == FAIL)
return FAIL;
else if (!ui32fromstr(ctx->token, &exponent))
return fail(ctx, "Expected exponent");
} // else
Dec 8, 2008
Dec 8, 2008
973
974
if (!floatok)
cvt.si32 = ((int32) val) * negative;
Dec 8, 2008
Dec 8, 2008
976
977
978
{
// !!! FIXME: this is lame.
char buf[128];
Dec 10, 2008
Dec 10, 2008
979
snprintf(buf, sizeof (buf), "%s%u.%u", (negative < 0) ? "-" : "",
Dec 8, 2008
Dec 8, 2008
980
981
(uint) val, (uint) fraction);
sscanf(buf, "%f", &cvt.f);
Dec 10, 2008
Dec 10, 2008
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
cvt.f *= (float) negative;
if (exponent)
{
int i;
if (negexp)
{
for (i = 0; i > exponent; i--)
cvt.f /= 10.0f;
} // if
else
{
for (i = 0; i < exponent; i++)
cvt.f *= 10.0f;
} // else
} // if
Dec 8, 2008
Dec 8, 2008
998
} // else
Dec 8, 2008
Dec 8, 2008
1000
*token = cvt.ui32;