/
mojoshader_assembler.c
1876 lines (1573 loc) · 53.6 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"
13
14
#define DEBUG_ASSEMBLY_PARSER 1
15
16
17
// Simple linked list to cache source filenames, so we don't have to copy
// the same string over and over for each opcode.
typedef struct FilenameCache
18
{
19
20
21
char *filename;
struct FilenameCache *next;
} FilenameCache;
22
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
FilenameCache *filename_cache;
62
} Context;
63
64
65
66
// Convenience functions for allocators...
67
static inline void out_of_memory(Context *ctx)
68
{
69
ctx->isfail = ctx->out_of_memory = 1;
70
71
72
73
74
75
76
77
78
79
} // 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
80
81
82
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
83
if (retval != NULL)
84
85
86
87
strcpy(retval, str);
return retval;
} // StrDup
88
89
90
91
92
93
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
94
95
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
96
{
97
98
99
100
const char *fname = NULL;
unsigned int linenum = 0;
int error_position = 0;
101
102
ctx->isfail = 1;
103
104
105
106
107
108
switch (ctx->parse_phase)
{
case MOJOSHADER_PARSEPHASE_NOTSTARTED:
error_position = -2;
break;
case MOJOSHADER_PARSEPHASE_WORKING:
109
110
fname = preprocessor_sourcepos(ctx->preprocessor, &linenum);
error_position = (int) linenum;
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
134
135
{
va_start(ap, fmt);
136
vsnprintf(failstr, len + 1, fmt, ap); // rebuild it.
137
138
va_end(ap);
139
140
141
error->error.error = failstr;
error->error.filename = fname ? StrDup(ctx, fname) : NULL;
error->error.error_position = error_position;
142
error->next = NULL;
143
144
ErrorList *prev = NULL;
145
146
ErrorList *item = ctx->errors;
while (item != NULL)
147
{
148
prev = item;
149
item = item->next;
150
151
} // while
152
if (prev == NULL)
153
ctx->errors = error;
154
155
else
prev->next = error;
156
157
158
ctx->error_count++;
} // else
159
160
} // failf
161
static inline void fail(Context *ctx, const char *reason)
162
{
163
failf(ctx, "%s", reason);
164
165
166
167
} // fail
static inline int isfail(const Context *ctx)
{
168
return ctx->isfail;
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
} // 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
195
196
static inline void pushback(Context *ctx)
{
197
198
199
#if DEBUG_ASSEMBLY_PARSER
printf("ASSEMBLER PUSHBACK\n");
#endif
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
assert(!ctx->pushedback);
ctx->pushedback = 1;
} // pushback
static Token _nexttoken(Context *ctx)
{
ctx->token = preprocessor_nexttoken(ctx->preprocessor, &ctx->tokenlen,
&ctx->tokenval);
if (preprocessor_outofmemory(ctx->preprocessor))
{
out_of_memory(ctx);
ctx->tokenval = TOKEN_EOI;
ctx->token = NULL;
ctx->tokenlen = 0;
} // if
else
{
const char *err = preprocessor_error(ctx->preprocessor);
if (err)
fail(ctx, err);
} // else
return ctx->tokenval;
} // _nexttoken
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
// !!! FIXME: cut-and-paste from preprocessor.
#if DEBUG_ASSEMBLY_PARSER
static void print_debug_token(Context *ctx)
{
printf("ASSEMBLER TOKEN: \"");
unsigned int i;
for (i = 0; i < ctx->tokenlen; i++)
{
if (ctx->token[i] == '\n')
printf("\\n");
else
printf("%c", ctx->token[i]);
} // for
printf("\" (");
switch (ctx->tokenval)
{
#define TOKENCASE(x) case x: printf("%s", #x); break
TOKENCASE(TOKEN_UNKNOWN);
TOKENCASE(TOKEN_IDENTIFIER);
TOKENCASE(TOKEN_INT_LITERAL);
TOKENCASE(TOKEN_FLOAT_LITERAL);
TOKENCASE(TOKEN_STRING_LITERAL);
TOKENCASE(TOKEN_ELLIPSIS);
TOKENCASE(TOKEN_RSHIFT);
TOKENCASE(TOKEN_LSHIFT);
TOKENCASE(TOKEN_ANDAND);
TOKENCASE(TOKEN_OROR);
TOKENCASE(TOKEN_LEQ);
TOKENCASE(TOKEN_GEQ);
TOKENCASE(TOKEN_EQL);
TOKENCASE(TOKEN_NEQ);
TOKENCASE(TOKEN_HASHHASH);
TOKENCASE(TOKEN_PP_INCLUDE);
TOKENCASE(TOKEN_PP_LINE);
TOKENCASE(TOKEN_PP_DEFINE);
TOKENCASE(TOKEN_PP_UNDEF);
TOKENCASE(TOKEN_PP_IF);
TOKENCASE(TOKEN_PP_IFDEF);
TOKENCASE(TOKEN_PP_IFNDEF);
TOKENCASE(TOKEN_PP_ELSE);
TOKENCASE(TOKEN_PP_ELIF);
TOKENCASE(TOKEN_PP_ENDIF);
TOKENCASE(TOKEN_PP_ERROR);
TOKENCASE(TOKEN_PP_INCOMPLETE_COMMENT);
TOKENCASE(TOKEN_EOI);
#undef TOKENCASE
case ((Token) '\n'):
printf("'\\n'");
break;
default:
assert(((int)ctx->tokenval) < 256);
printf("'%c'", (char) ctx->tokenval);
break;
} // switch
printf(")\n");
}
#endif
287
288
289
290
static Token nexttoken(Context *ctx)
{
if (ctx->pushedback)
{
291
print_debug_token(ctx);
292
293
294
295
296
ctx->pushedback = 0;
return ctx->tokenval;
} // if
Token token = _nexttoken(ctx);
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
while (token == ((Token) '\n'))
token = _nexttoken(ctx); // skip endlines.
if (token == ((Token) ';')) // single line comment in assembler.
{
do
{
token = _nexttoken(ctx);
} while ((token != ((Token) '\n')) && (token != TOKEN_EOI));
while (token == ((Token) '\n'))
token = _nexttoken(ctx); // skip endlines.
} // if
312
print_debug_token(ctx);
313
314
315
316
317
return token;
} // nexttoken
static const char *cache_filename(Context *ctx, const char *fname)
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
if (fname == NULL)
return NULL;
// !!! FIXME: this could be optimized into a hash table, but oh well.
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
if (strcmp(item->filename, fname) == 0)
return item->filename;
item = item->next;
} // while
// new cache item.
item = (FilenameCache *) Malloc(ctx, sizeof (FilenameCache));
if (item == NULL)
return NULL;
item->filename = (char *) Malloc(ctx, strlen(fname) + 1);
if (item->filename == NULL)
{
Free(ctx, item);
return NULL;
} // if
strcpy(item->filename, fname);
item->next = ctx->filename_cache;
ctx->filename_cache = item;
return item->filename;
} // cache_filename
static void free_filename_cache(Context *ctx)
{
FilenameCache *item = ctx->filename_cache;
while (item != NULL)
{
FilenameCache *next = item->next;
Free(ctx, item->filename);
Free(ctx, item);
item = next;
} // while
} // free_filename_cache
362
363
364
365
static inline void add_token_sourcepos(Context *ctx, const size_t idx)
{
366
367
368
369
unsigned int pos = 0;
const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
ctx->token_to_source[idx].line = pos;
ctx->token_to_source[idx].filename = cache_filename(ctx, fname);
370
371
372
} // add_token_sourcepos
373
374
375
376
377
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
378
379
380
381
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;
382
383
384
void *ptr;
ptr = Malloc(ctx, newsize * sizeof (uint32));
385
386
387
388
389
390
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;
391
392
ptr = Malloc(ctx, newsize * sizeof (SourcePos));
393
394
395
if (ptr == NULL)
return;
if (ctx->output_len > 0)
396
397
398
memcpy(ptr, ctx->token_to_source, ctx->output_len * sizeof (SourcePos));
Free(ctx, ctx->token_to_source);
ctx->token_to_source = (SourcePos *) ptr;
399
400
ctx->output_allocation = newsize;
401
402
} // if
403
ctx->output[ctx->output_len] = token;
404
add_token_sourcepos(ctx, ctx->output_len);
405
ctx->output_len++;
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
} // 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
447
static int require_comma(Context *ctx)
448
{
449
450
const Token token = nexttoken(ctx);
if (token != ((Token) ','))
451
{
452
453
fail(ctx, "Comma expected");
return 0;
454
} // if
455
456
return 1;
} // require_comma
457
458
459
static int check_token_segment(Context *ctx, const char *str)
460
{
461
462
463
464
465
466
467
468
// !!! 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
469
470
471
static int check_token(Context *ctx, const char *str)
472
{
473
474
const size_t len = strlen(str);
if ( (ctx->tokenlen != len) || (strncasecmp(ctx->token, str, len) != 0) )
475
return 0;
476
477
ctx->token += len;
ctx->tokenlen = 0;
478
return 1;
479
} // check_token
480
481
482
static int ui32fromtoken(Context *ctx, uint32 *_val)
483
{
484
485
486
487
488
489
int i;
for (i = 0; i < ctx->tokenlen; i++)
{
if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
break;
} // for
490
491
if (i == 0)
492
{
493
494
*_val = 0;
return 0;
495
496
} // if
497
498
499
500
const int len = i;
uint32 val = 0;
uint32 mult = 1;
while (i--)
501
{
502
503
504
val += ((uint32) (ctx->token[i] - '0')) * mult;
mult *= 10;
} // while
505
506
507
ctx->token += len;
ctx->tokenlen -= len;
508
509
*_val = val;
510
return 1;
511
} // ui32fromtoken
512
513
514
515
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
516
517
518
if (nexttoken(ctx) != TOKEN_IDENTIFIER)
{
fail(ctx, "Expected register");
519
return 0;
520
} // if
521
522
523
524
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
525
526
if (check_token_segment(ctx, "r"))
527
regtype = REG_TYPE_TEMP;
528
else if (check_token_segment(ctx, "v"))
529
regtype = REG_TYPE_INPUT;
530
else if (check_token_segment(ctx, "c"))
531
regtype = REG_TYPE_CONST;
532
else if (check_token_segment(ctx, "i"))
533
regtype = REG_TYPE_CONSTINT;
534
else if (check_token_segment(ctx, "b"))
535
regtype = REG_TYPE_CONSTBOOL;
536
else if (check_token_segment(ctx, "oC"))
537
regtype = REG_TYPE_COLOROUT;
538
else if (check_token_segment(ctx, "s"))
539
regtype = REG_TYPE_SAMPLER;
540
else if (check_token_segment(ctx, "oD"))
541
regtype = REG_TYPE_ATTROUT;
542
else if (check_token_segment(ctx, "l"))
543
regtype = REG_TYPE_LABEL;
544
else if (check_token_segment(ctx, "p"))
545
regtype = REG_TYPE_PREDICATE;
546
547
548
549
550
551
552
553
554
else if (check_token_segment(ctx, "o"))
regtype = REG_TYPE_OUTPUT;
else if (check_token_segment(ctx, "oT"))
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;
else if (check_token_segment(ctx, "oDepth"))
555
556
557
558
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
559
else if (check_token_segment(ctx, "aL"))
560
561
562
563
{
regtype = REG_TYPE_LOOP;
neednum = 0;
} // else if
564
else if (check_token_segment(ctx, "vPos"))
565
566
567
568
569
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
570
else if (check_token_segment(ctx, "vFace"))
571
572
573
574
575
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
neednum = 0;
} // else if
576
else if (check_token_segment(ctx, "oPos"))
577
578
579
580
581
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
582
else if (check_token_segment(ctx, "oFog"))
583
584
585
586
587
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
588
else if (check_token_segment(ctx, "oPts"))
589
590
591
592
593
594
595
596
597
598
{
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
{
599
600
601
602
fail(ctx, "expected register type");
regtype = REG_TYPE_CONST;
regnum = 0;
neednum = 0;
603
604
} // else
605
606
if (neednum)
{
607
608
609
if (nexttoken(ctx) == ((Token) '['))
neednum = 0; // "c[5]" is the same as "c5".
pushback(ctx);
610
611
} // if
612
613
if (neednum)
{
614
uint32 ui32 = 0;
615
if (!ui32fromtoken(ctx, &ui32))
616
fail(ctx, "Invalid register index");
617
regnum = (int) ui32;
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
} // 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
{
645
fail(ctx, "Invalid const register index");
646
647
648
649
650
651
} // else
} // if
*rtype = regtype;
*rnum = regnum;
652
return 1;
653
654
655
} // parse_register_name
656
static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
657
{
658
if (info->result_shift != 0)
659
fail(ctx, "Multiple result shift modifiers");
660
661
info->result_shift = val;
} // set_result_shift
662
663
664
static int parse_destination_token(Context *ctx)
665
{
666
DestArgInfo *info = &ctx->dest_arg;
667
memset(info, '\0', sizeof (DestArgInfo));
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
// 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"))
690
info->result_mod |= MOD_SATURATE;
691
else if (check_token_segment(ctx, "_pp"))
692
info->result_mod |= MOD_PP;
693
else if (check_token_segment(ctx, "_centroid"))
694
695
info->result_mod |= MOD_CENTROID;
else
696
invalid_modifier = 1;
697
698
} // while
699
700
if (invalid_modifier)
fail(ctx, "Invalid destination modifier");
701
702
// !!! FIXME: predicates.
703
if (nexttoken(ctx) == ((Token) '('))
704
705
fail(ctx, "Predicates unsupported at this time"); // !!! FIXME: ...
706
707
pushback(ctx); // parse_register_name calls nexttoken().
708
parse_register_name(ctx, &info->regtype, &info->regnum);
709
710
711
// parse_register_name() can't check this: dest regs might have modifiers.
if (ctx->tokenlen > 0)
fail(ctx, "invalid register name");
712
713
714
// !!! FIXME: can dest registers do relative addressing?
715
int invalid_writemask = 0;
716
int implicit_writemask = 0;
717
if (nexttoken(ctx) != ((Token) '.'))
718
{
719
implicit_writemask = 1;
720
721
722
723
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
724
725
726
727
// !!! 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) )
728
fail(ctx, "Writemask specified for scalar register");
729
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
730
invalid_writemask = 1;
731
732
else
{
733
734
735
736
737
738
// !!! 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;
739
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 0;
740
741
742
743
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++; }
744
if ((ptr == ctx->token) && (shader_is_pixel(ctx)))
745
{
746
747
748
749
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++; }
750
751
752
} // if
if (*ptr != '\0')
753
invalid_writemask = 1;
754
755
756
757
758
759
760
info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );
} // else
761
762
763
if (invalid_writemask)
fail(ctx, "Invalid writemask");
764
765
766
767
768
// !!! 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) )
769
fail(ctx, "Writemask specified for scalar register");
770
771
} // if
772
773
774
info->orig_writemask = info->writemask;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
775
776
777
778
{
fail(ctx, "Too many tokens");
return 1;
} // if
779
780
ctx->tokenbuf[ctx->tokenbufpos++] =
781
( ((((uint32) 1)) << 31) |
782
783
784
785
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
786
((((uint32) info->writemask) & 0xF) << 16) |
787
788
789
790
791
792
793
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
794
795
796
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
797
{
798
799
800
801
802
if ( (*srcmod != SRCMOD_NONE) || (negate && (negated == SRCMOD_NONE)) )
fail(ctx, "Incompatible source modifiers");
else
*srcmod = ((negate) ? negated : norm);
} // set_source_mod
803
804
805
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
806
{
807
808
809
int retval = 1;
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
810
811
812
813
{
fail(ctx, "Too many tokens");
return 0;
} // if
814
815
// mark this now, so optional relative addressing token is placed second.
816
817
uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
*outtoken = 0;
818
819
820
SourceMod srcmod = SRCMOD_NONE;
int negate = 0;
821
822
823
824
825
826
827
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")) )
828
{
829
830
if (nexttoken(ctx) != ((Token) '-'))
fail(ctx, "Unexpected token");
831
832
833
834
else
srcmod = SRCMOD_COMPLEMENT;
} // else
else
835
{
836
pushback(ctx);
837
} // else
838
839
840
RegisterType regtype;
int regnum;
841
parse_register_name(ctx, ®type, ®num);
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
if (ctx->tokenlen > 0)
{
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");
} // if
860
861
uint32 relative = 0;
862
if (nexttoken(ctx) != ((Token) '['))
863
864
865
pushback(ctx); // not relative addressing?
else
{
866
867
868
869
870
871
if (!relok)
fail(ctx, "Relative addressing not permitted here.");
else
retval++;
parse_source_token_maybe_relative(ctx, 0);
872
relative = 1;
873
874
if (nexttoken(ctx) != ((Token) '+'))
875
876
877
pushback(ctx);
else
{
878
879
// !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
if (regnum != 0)
880
fail(ctx, "Relative addressing with explicit register number.");
881
882
uint32 ui32 = 0;
883
884
885
886
if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) ||
(!ui32fromtoken(ctx, &ui32)) ||
(ctx->tokenlen != 0) )
{
887
fail(ctx, "Invalid relative addressing offset");
888
} // if
889
890
891
regnum += (int) ui32;
} // else
892
if (nexttoken(ctx) != ((Token) ']'))
893
fail(ctx, "Expected ']'");
894
} // else
895
896
int invalid_swizzle = 0;
897
uint32 swizzle = 0;
898
if (nexttoken(ctx) != ((Token) '.'))
899
900
901
902
{
swizzle = 0xE4; // 0xE4 == 11100100 ... 0 1 2 3. No swizzle.
pushback(ctx); // no explicit writemask; do full mask.
} // if
903
else if (scalar_register(ctx->shader_type, regtype, regnum))
904
fail(ctx, "Swizzle specified for scalar register");
905
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
906
invalid_swizzle = 1;
907
908
else
{
909
910
911
912
char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
913
// deal with shortened form (.x = .xxxx, etc).
914
915
916
917
918
919
920
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)
921
invalid_swizzle = 1;
922
tokenbytes[4] = '\0';
923
924
uint32 val = 0;
925
926
int saw_xyzw = 0;
int saw_rgba = 0;
927
int i;
928
929
for (i = 0; i < 4; i++)
{
930
const int component = (int) tokenbytes[i];
931
932
933
934
935
936
937
938
939
940
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;
941
default: invalid_swizzle = 1; break;
942
943
944
} // switch
swizzle |= (val << (i * 2));
} // for
945
946
if (saw_xyzw && saw_rgba)
947
invalid_swizzle = 1;
948
949
else if (saw_rgba && !shader_is_pixel(ctx))
invalid_swizzle = 1;
950
} // else
951
952
953
954
if (invalid_swizzle)
fail(ctx, "Invalid swizzle");
955
956
957
958
959
960
961
*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) );
962
963
964
return retval;
} // parse_source_token_maybe_relative
965
966
967
static inline int parse_source_token(Context *ctx)
968
{
969
970
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
971
972
973
974
static int parse_args_NULL(Context *ctx)
{
975
return 1;
976
} // parse_args_NULL
977
978
979
static int parse_num(Context *ctx, const int floatok, uint32 *value)
980
981
982
{
union { float f; int32 si32; uint32 ui32; } cvt;
983
984
const Token token = nexttoken(ctx);
if (token == TOKEN_INT_LITERAL)
985
{
986
987
988
989
990
int d = 0;
sscanf(ctx->token, "%d", &d);
cvt.si32 = (int32) d;
} // if
else if (token == TOKEN_FLOAT_LITERAL)
991
{
992
if (!floatok)
993
{
994
995
996
fail(ctx, "Expected whole number");
*value = 0;
return 0;
997
} // if
998
999
1000
sscanf(ctx->token, "%f", &cvt.f);
} // if
else