Skip to content

Latest commit

 

History

History
1776 lines (1475 loc) · 51.6 KB

mojoshader_assembler.c

File metadata and controls

1776 lines (1475 loc) · 51.6 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.
*/
Feb 24, 2009
Feb 24, 2009
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"
Dec 29, 2009
Dec 29, 2009
16
17
18
19
#if !SUPPORT_PROFILE_BYTECODE
#error Shader assembler needs bytecode profile. Fix your build.
#endif
Feb 28, 2009
Feb 28, 2009
20
#if DEBUG_ASSEMBLER_PARSER
Feb 12, 2009
Feb 12, 2009
21
22
23
24
25
26
#define print_debug_token(token, len, val) \
MOJOSHADER_print_debug_token("ASSEMBLER", token, len, val)
#else
#define print_debug_token(token, len, val)
#endif
Feb 12, 2009
Feb 12, 2009
27
Feb 3, 2009
Feb 3, 2009
28
29
30
31
32
33
typedef struct SourcePos
{
const char *filename;
uint32 line;
} SourcePos;
34
35
// Context...this is state that changes as we assemble a shader...
Feb 3, 2009
Feb 3, 2009
36
typedef struct Context
Feb 3, 2009
Feb 3, 2009
38
39
int isfail;
int out_of_memory;
40
41
42
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
Nov 11, 2010
Nov 11, 2010
43
44
const char *current_file;
int current_position;
Feb 3, 2009
Feb 3, 2009
45
ErrorList *errors;
Feb 11, 2009
Feb 11, 2009
46
Preprocessor *preprocessor;
47
48
49
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
Feb 11, 2009
Feb 11, 2009
50
51
52
53
54
55
56
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!
57
DestArgInfo dest_arg;
Nov 10, 2010
Nov 10, 2010
58
59
60
Buffer *output;
Buffer *token_to_source;
Buffer *ctab;
Feb 3, 2009
Feb 3, 2009
61
} Context;
Nov 4, 2010
Nov 4, 2010
64
65
66
67
// !!! FIXME: cut and paste between every damned source file follows...
// !!! FIXME: We need to make some sort of ContextBase that applies to all
// !!! FIXME: files and move this stuff to mojoshader_common.c ...
68
69
// Convenience functions for allocators...
Feb 3, 2009
Feb 3, 2009
70
static inline void out_of_memory(Context *ctx)
Feb 3, 2009
Feb 3, 2009
72
ctx->isfail = ctx->out_of_memory = 1;
73
74
75
76
77
78
79
80
81
82
} // 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
Feb 3, 2009
Feb 3, 2009
83
84
85
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
Feb 7, 2009
Feb 7, 2009
86
if (retval != NULL)
Feb 3, 2009
Feb 3, 2009
87
88
89
90
strcpy(retval, str);
return retval;
} // StrDup
91
92
static inline void Free(Context *ctx, void *ptr)
{
Nov 4, 2010
Nov 4, 2010
93
ctx->free(ptr, ctx->malloc_data);
94
95
} // Free
Nov 4, 2010
Nov 4, 2010
96
97
98
99
100
101
102
103
104
105
static void *MallocBridge(int bytes, void *data)
{
return Malloc((Context *) data, (size_t) bytes);
} // MallocBridge
static void FreeBridge(void *ptr, void *data)
{
Free((Context *) data, ptr);
} // FreeBridge
Nov 4, 2010
Nov 4, 2010
106
Feb 3, 2009
Feb 3, 2009
107
108
static void failf(Context *ctx, const char *fmt, ...) ISPRINTF(2,3);
static void failf(Context *ctx, const char *fmt, ...)
Feb 3, 2009
Feb 3, 2009
110
ctx->isfail = 1;
Nov 4, 2010
Nov 4, 2010
111
112
if (ctx->out_of_memory)
return;
Feb 3, 2009
Feb 3, 2009
113
Feb 3, 2009
Feb 3, 2009
114
115
va_list ap;
va_start(ap, fmt);
Nov 11, 2010
Nov 11, 2010
116
errorlist_add_va(ctx->errors, ctx->current_file, ctx->current_position, fmt, ap);
Feb 3, 2009
Feb 3, 2009
117
va_end(ap);
118
119
} // failf
Feb 3, 2009
Feb 3, 2009
120
static inline void fail(Context *ctx, const char *reason)
Feb 3, 2009
Feb 3, 2009
122
failf(ctx, "%s", reason);
123
124
125
126
} // fail
static inline int isfail(const Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
127
return ctx->isfail;
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
} // 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
Feb 11, 2009
Feb 11, 2009
154
155
static inline void pushback(Context *ctx)
{
Feb 28, 2009
Feb 28, 2009
156
#if DEBUG_ASSEMBLER_PARSER
Feb 12, 2009
Feb 12, 2009
157
158
printf("ASSEMBLER PUSHBACK\n");
#endif
Feb 11, 2009
Feb 11, 2009
159
160
161
162
163
164
165
166
167
assert(!ctx->pushedback);
ctx->pushedback = 1;
} // pushback
static Token nexttoken(Context *ctx)
{
if (ctx->pushedback)
ctx->pushedback = 0;
Feb 24, 2009
Feb 24, 2009
168
else
Feb 11, 2009
Feb 11, 2009
169
{
Feb 24, 2009
Feb 24, 2009
170
while (1)
Feb 11, 2009
Feb 11, 2009
171
{
Feb 24, 2009
Feb 24, 2009
172
173
174
ctx->token = preprocessor_nexttoken(ctx->preprocessor,
&ctx->tokenlen,
&ctx->tokenval);
Feb 11, 2009
Feb 11, 2009
175
Feb 24, 2009
Feb 24, 2009
176
177
178
179
180
181
182
183
if (preprocessor_outofmemory(ctx->preprocessor))
{
ctx->tokenval = TOKEN_EOI;
ctx->token = NULL;
ctx->tokenlen = 0;
break;
} // if
Nov 11, 2010
Nov 11, 2010
184
185
186
187
188
unsigned int line;
ctx->current_file = preprocessor_sourcepos(ctx->preprocessor,&line);
ctx->current_position = (int) line;
if (ctx->tokenval == TOKEN_BAD_CHARS)
Feb 21, 2010
Feb 21, 2010
189
190
191
192
193
{
fail(ctx, "Bad characters in source file");
continue;
} // else if
Feb 24, 2009
Feb 24, 2009
194
195
196
197
198
199
200
201
202
else if (ctx->tokenval == TOKEN_PREPROCESSING_ERROR)
{
fail(ctx, ctx->token);
continue;
} // else if
break;
} // while
} // else
Feb 11, 2009
Feb 11, 2009
203
Feb 12, 2009
Feb 12, 2009
204
print_debug_token(ctx->token, ctx->tokenlen, ctx->tokenval);
Feb 24, 2009
Feb 24, 2009
205
return ctx->tokenval;
Feb 11, 2009
Feb 11, 2009
206
207
} // nexttoken
Feb 3, 2009
Feb 3, 2009
208
209
210
static void output_token_noswap(Context *ctx, const uint32 token)
{
Nov 10, 2010
Nov 10, 2010
211
if (!isfail(ctx))
Dec 8, 2008
Dec 8, 2008
212
{
Nov 10, 2010
Nov 10, 2010
213
214
215
216
217
218
219
220
221
222
223
224
buffer_append(ctx->output, &token, sizeof (token));
// We only need a list of these that grows throughout processing, and
// is flattened for reference at the end of the run, so we use a
// Buffer. It's sneaky!
unsigned int pos = 0;
const char *fname = preprocessor_sourcepos(ctx->preprocessor, &pos);
SourcePos srcpos;
memset(&srcpos, '\0', sizeof (SourcePos));
srcpos.line = pos;
srcpos.filename = fname; // cached in preprocessor!
buffer_append(ctx->token_to_source, &srcpos, sizeof (SourcePos));
Dec 8, 2008
Dec 8, 2008
225
} // if
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
} // 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
Feb 11, 2009
Feb 11, 2009
267
static int require_comma(Context *ctx)
Feb 11, 2009
Feb 11, 2009
269
270
const Token token = nexttoken(ctx);
if (token != ((Token) ','))
Dec 8, 2008
Dec 8, 2008
271
{
Feb 11, 2009
Feb 11, 2009
272
273
fail(ctx, "Comma expected");
return 0;
Dec 8, 2008
Dec 8, 2008
274
} // if
Feb 11, 2009
Feb 11, 2009
275
276
return 1;
} // require_comma
Dec 10, 2008
Dec 10, 2008
277
Dec 13, 2008
Dec 13, 2008
278
Feb 11, 2009
Feb 11, 2009
279
static int check_token_segment(Context *ctx, const char *str)
Dec 13, 2008
Dec 13, 2008
280
{
Feb 11, 2009
Feb 11, 2009
281
282
283
284
285
286
287
288
// !!! 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
Feb 11, 2009
Feb 11, 2009
291
static int check_token(Context *ctx, const char *str)
Feb 11, 2009
Feb 11, 2009
293
294
const size_t len = strlen(str);
if ( (ctx->tokenlen != len) || (strncasecmp(ctx->token, str, len) != 0) )
Feb 3, 2009
Feb 3, 2009
295
return 0;
Feb 11, 2009
Feb 11, 2009
296
297
ctx->token += len;
ctx->tokenlen = 0;
Feb 3, 2009
Feb 3, 2009
298
return 1;
Feb 11, 2009
Feb 11, 2009
299
} // check_token
Dec 13, 2008
Dec 13, 2008
300
301
Feb 11, 2009
Feb 11, 2009
302
static int ui32fromtoken(Context *ctx, uint32 *_val)
Dec 13, 2008
Dec 13, 2008
303
{
Nov 15, 2009
Nov 15, 2009
304
unsigned int i;
Feb 11, 2009
Feb 11, 2009
305
306
307
308
309
for (i = 0; i < ctx->tokenlen; i++)
{
if ((ctx->token[i] < '0') || (ctx->token[i] > '9'))
break;
} // for
Feb 11, 2009
Feb 11, 2009
311
if (i == 0)
Feb 3, 2009
Feb 3, 2009
312
{
Feb 11, 2009
Feb 11, 2009
313
314
*_val = 0;
return 0;
Feb 3, 2009
Feb 3, 2009
315
316
} // if
Nov 15, 2009
Nov 15, 2009
317
const unsigned int len = i;
Feb 11, 2009
Feb 11, 2009
318
319
320
uint32 val = 0;
uint32 mult = 1;
while (i--)
Feb 3, 2009
Feb 3, 2009
321
{
Feb 11, 2009
Feb 11, 2009
322
323
324
val += ((uint32) (ctx->token[i] - '0')) * mult;
mult *= 10;
} // while
Feb 11, 2009
Feb 11, 2009
326
327
ctx->token += len;
ctx->tokenlen -= len;
Feb 11, 2009
Feb 11, 2009
329
*_val = val;
Feb 3, 2009
Feb 3, 2009
330
return 1;
Feb 11, 2009
Feb 11, 2009
331
} // ui32fromtoken
332
333
334
335
static int parse_register_name(Context *ctx, RegisterType *rtype, int *rnum)
{
Feb 11, 2009
Feb 11, 2009
336
337
338
if (nexttoken(ctx) != TOKEN_IDENTIFIER)
{
fail(ctx, "Expected register");
Feb 3, 2009
Feb 3, 2009
339
return 0;
Feb 11, 2009
Feb 11, 2009
340
} // if
341
342
343
344
int neednum = 1;
int regnum = 0;
RegisterType regtype = REG_TYPE_TEMP;
Feb 11, 2009
Feb 11, 2009
345
Feb 12, 2009
Feb 12, 2009
346
347
348
// Watch out for substrings! oDepth must be checked before oD, since
// the latter will match either case.
if (check_token_segment(ctx, "oDepth"))
Dec 13, 2008
Dec 13, 2008
349
350
351
352
{
regtype = REG_TYPE_DEPTHOUT;
neednum = 0;
} // else if
Feb 12, 2009
Feb 12, 2009
353
else if (check_token_segment(ctx, "vFace"))
Feb 12, 2009
Feb 12, 2009
355
356
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_FACE;
357
358
neednum = 0;
} // else if
Feb 11, 2009
Feb 11, 2009
359
else if (check_token_segment(ctx, "vPos"))
360
361
362
363
364
{
regtype = REG_TYPE_MISCTYPE;
regnum = (int) MISCTYPE_TYPE_POSITION;
neednum = 0;
} // else if
Feb 11, 2009
Feb 11, 2009
365
else if (check_token_segment(ctx, "oPos"))
366
367
368
369
370
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POSITION;
neednum = 0;
} // else if
Feb 11, 2009
Feb 11, 2009
371
else if (check_token_segment(ctx, "oFog"))
372
373
374
375
376
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_FOG;
neednum = 0;
} // else if
Feb 11, 2009
Feb 11, 2009
377
else if (check_token_segment(ctx, "oPts"))
378
379
380
381
382
{
regtype = REG_TYPE_RASTOUT;
regnum = (int) RASTOUT_TYPE_POINT_SIZE;
neednum = 0;
} // else if
Feb 12, 2009
Feb 12, 2009
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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;
416
417
418
419
420
//case REG_TYPE_TEMPFLOAT16: // !!! FIXME: don't know this asm string
else
{
Feb 3, 2009
Feb 3, 2009
421
422
423
424
fail(ctx, "expected register type");
regtype = REG_TYPE_CONST;
regnum = 0;
neednum = 0;
425
426
} // else
Feb 12, 2009
Feb 12, 2009
427
428
// "c[5]" is the same as "c5", so if the token is done, see if next is '['.
if ((neednum) && (ctx->tokenlen == 0))
Dec 10, 2008
Dec 10, 2008
429
{
Aug 1, 2011
Aug 1, 2011
430
const int tlen = ctx->tokenlen; // we need to protect this for later.
Feb 11, 2009
Feb 11, 2009
431
if (nexttoken(ctx) == ((Token) '['))
Feb 12, 2009
Feb 12, 2009
432
neednum = 0; // don't need a number on register name itself.
Feb 11, 2009
Feb 11, 2009
433
pushback(ctx);
Aug 1, 2011
Aug 1, 2011
434
ctx->tokenlen = tlen;
Dec 10, 2008
Dec 10, 2008
435
436
} // if
437
438
if (neednum)
{
Dec 10, 2008
Dec 10, 2008
439
uint32 ui32 = 0;
Feb 11, 2009
Feb 11, 2009
440
if (!ui32fromtoken(ctx, &ui32))
Feb 3, 2009
Feb 3, 2009
441
fail(ctx, "Invalid register index");
Dec 10, 2008
Dec 10, 2008
442
regnum = (int) ui32;
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
} // 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
{
Feb 3, 2009
Feb 3, 2009
470
fail(ctx, "Invalid const register index");
471
472
473
474
475
476
} // else
} // if
*rtype = regtype;
*rnum = regnum;
Feb 3, 2009
Feb 3, 2009
477
return 1;
478
479
480
} // parse_register_name
Feb 3, 2009
Feb 3, 2009
481
static void set_result_shift(Context *ctx, DestArgInfo *info, const int val)
Dec 8, 2008
Dec 8, 2008
483
if (info->result_shift != 0)
Feb 3, 2009
Feb 3, 2009
484
fail(ctx, "Multiple result shift modifiers");
Dec 8, 2008
Dec 8, 2008
485
486
info->result_shift = val;
} // set_result_shift
Dec 8, 2008
Dec 8, 2008
488
Nov 15, 2009
Nov 15, 2009
489
490
491
492
493
494
495
496
497
498
499
500
static inline int tokenbuf_overflow(Context *ctx)
{
if ( ctx->tokenbufpos >= ((int) (STATICARRAYLEN(ctx->tokenbuf))) )
{
fail(ctx, "Too many tokens");
return 1;
} // if
return 0;
} // tokenbuf_overflow
Dec 13, 2008
Dec 13, 2008
501
static int parse_destination_token(Context *ctx)
Dec 8, 2008
Dec 8, 2008
502
{
Dec 13, 2008
Dec 13, 2008
503
DestArgInfo *info = &ctx->dest_arg;
Dec 12, 2008
Dec 12, 2008
504
memset(info, '\0', sizeof (DestArgInfo));
Feb 11, 2009
Feb 11, 2009
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// 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"))
527
info->result_mod |= MOD_SATURATE;
Feb 11, 2009
Feb 11, 2009
528
else if (check_token_segment(ctx, "_pp"))
529
info->result_mod |= MOD_PP;
Feb 11, 2009
Feb 11, 2009
530
else if (check_token_segment(ctx, "_centroid"))
531
532
info->result_mod |= MOD_CENTROID;
else
Feb 11, 2009
Feb 11, 2009
533
invalid_modifier = 1;
534
535
} // while
Feb 11, 2009
Feb 11, 2009
536
537
if (invalid_modifier)
fail(ctx, "Invalid destination modifier");
538
539
// !!! FIXME: predicates.
Feb 11, 2009
Feb 11, 2009
540
if (nexttoken(ctx) == ((Token) '('))
Feb 3, 2009
Feb 3, 2009
541
542
fail(ctx, "Predicates unsupported at this time"); // !!! FIXME: ...
543
544
pushback(ctx); // parse_register_name calls nexttoken().
Feb 3, 2009
Feb 3, 2009
545
parse_register_name(ctx, &info->regtype, &info->regnum);
Feb 11, 2009
Feb 11, 2009
546
547
548
// parse_register_name() can't check this: dest regs might have modifiers.
if (ctx->tokenlen > 0)
fail(ctx, "invalid register name");
549
550
551
// !!! FIXME: can dest registers do relative addressing?
Feb 3, 2009
Feb 3, 2009
552
int invalid_writemask = 0;
Dec 14, 2008
Dec 14, 2008
553
int implicit_writemask = 0;
Feb 11, 2009
Feb 11, 2009
554
if (nexttoken(ctx) != ((Token) '.'))
Dec 14, 2008
Dec 14, 2008
556
implicit_writemask = 1;
557
558
559
560
info->writemask = 0xF;
info->writemask0 = info->writemask1 = info->writemask2 = info->writemask3 = 1;
pushback(ctx); // no explicit writemask; do full mask.
} // if
Feb 11, 2009
Feb 11, 2009
561
Dec 14, 2008
Dec 14, 2008
562
563
564
// !!! 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) )
Feb 3, 2009
Feb 3, 2009
565
fail(ctx, "Writemask specified for scalar register");
Feb 11, 2009
Feb 11, 2009
566
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
Feb 3, 2009
Feb 3, 2009
567
invalid_writemask = 1;
568
569
else
{
Feb 11, 2009
Feb 11, 2009
570
571
572
573
574
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;
Feb 28, 2009
Feb 28, 2009
575
576
577
578
if ((*ptr == 'r') || (*ptr == 'x')) { info->writemask0 = 1; ptr++; }
if ((*ptr == 'g') || (*ptr == 'y')) { info->writemask1 = 1; ptr++; }
if ((*ptr == 'b') || (*ptr == 'z')) { info->writemask2 = 1; ptr++; }
if ((*ptr == 'a') || (*ptr == 'w')) { info->writemask3 = 1; ptr++; }
579
580
if (*ptr != '\0')
Feb 3, 2009
Feb 3, 2009
581
invalid_writemask = 1;
582
583
584
585
586
587
588
info->writemask = ( ((info->writemask0 & 0x1) << 0) |
((info->writemask1 & 0x1) << 1) |
((info->writemask2 & 0x1) << 2) |
((info->writemask3 & 0x1) << 3) );
} // else
Feb 3, 2009
Feb 3, 2009
589
590
591
if (invalid_writemask)
fail(ctx, "Invalid writemask");
Dec 14, 2008
Dec 14, 2008
592
593
594
595
596
// !!! 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) )
Feb 3, 2009
Feb 3, 2009
597
fail(ctx, "Writemask specified for scalar register");
Dec 14, 2008
Dec 14, 2008
598
599
} // if
600
601
info->orig_writemask = info->writemask;
Nov 15, 2009
Nov 15, 2009
602
if (tokenbuf_overflow(ctx))
Feb 3, 2009
Feb 3, 2009
603
return 1;
604
605
ctx->tokenbuf[ctx->tokenbufpos++] =
Dec 8, 2008
Dec 8, 2008
606
( ((((uint32) 1)) << 31) |
607
608
609
610
((((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
611
((((uint32) info->writemask) & 0xF) << 16) |
612
613
614
615
616
617
618
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
Dec 10, 2008
Dec 10, 2008
619
620
621
static void set_source_mod(Context *ctx, const int negate,
const SourceMod norm, const SourceMod negated,
SourceMod *srcmod)
Dec 8, 2008
Dec 8, 2008
623
624
625
626
627
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
630
static int parse_source_token_maybe_relative(Context *ctx, const int relok)
Dec 8, 2008
Dec 8, 2008
632
633
int retval = 1;
Nov 15, 2009
Nov 15, 2009
634
if (tokenbuf_overflow(ctx))
Feb 3, 2009
Feb 3, 2009
635
return 0;
Dec 8, 2008
Dec 8, 2008
636
637
// mark this now, so optional relative addressing token is placed second.
Feb 11, 2009
Feb 11, 2009
638
639
uint32 *outtoken = &ctx->tokenbuf[ctx->tokenbufpos++];
*outtoken = 0;
Dec 8, 2008
Dec 8, 2008
640
641
642
SourceMod srcmod = SRCMOD_NONE;
int negate = 0;
Feb 11, 2009
Feb 11, 2009
643
644
645
646
647
648
649
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")) )
Dec 8, 2008
Dec 8, 2008
650
{
Feb 11, 2009
Feb 11, 2009
651
652
if (nexttoken(ctx) != ((Token) '-'))
fail(ctx, "Unexpected token");
Dec 8, 2008
Dec 8, 2008
653
654
655
656
else
srcmod = SRCMOD_COMPLEMENT;
} // else
else
Feb 11, 2009
Feb 11, 2009
657
{
Dec 8, 2008
Dec 8, 2008
658
pushback(ctx);
Feb 11, 2009
Feb 11, 2009
659
} // else
Dec 8, 2008
Dec 8, 2008
661
662
RegisterType regtype;
int regnum;
Feb 3, 2009
Feb 3, 2009
663
parse_register_name(ctx, &regtype, &regnum);
Feb 24, 2009
Feb 24, 2009
665
666
667
668
669
670
if (ctx->tokenlen == 0)
{
if (negate)
set_source_mod(ctx, negate, SRCMOD_NONE, SRCMOD_NEGATE, &srcmod);
} // if
else
Feb 11, 2009
Feb 11, 2009
671
{
Feb 24, 2009
Feb 24, 2009
672
assert(ctx->tokenlen > 0);
Feb 11, 2009
Feb 11, 2009
673
674
675
676
677
678
679
680
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);
Apr 18, 2012
Apr 18, 2012
681
682
else if (check_token_segment(ctx, "_db"))
set_source_mod(ctx, negate, SRCMOD_DZ, SRCMOD_NONE, &srcmod);
Feb 11, 2009
Feb 11, 2009
683
684
else if (check_token_segment(ctx, "_dw"))
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
Apr 18, 2012
Apr 18, 2012
685
686
else if (check_token_segment(ctx, "_da"))
set_source_mod(ctx, negate, SRCMOD_DW, SRCMOD_NONE, &srcmod);
Feb 11, 2009
Feb 11, 2009
687
688
689
690
else if (check_token_segment(ctx, "_abs"))
set_source_mod(ctx, negate, SRCMOD_ABS, SRCMOD_ABSNEGATE, &srcmod);
else
fail(ctx, "Invalid source modifier");
Feb 24, 2009
Feb 24, 2009
691
} // else
Dec 8, 2008
Dec 8, 2008
693
uint32 relative = 0;
Feb 11, 2009
Feb 11, 2009
694
if (nexttoken(ctx) != ((Token) '['))
Dec 8, 2008
Dec 8, 2008
695
696
697
pushback(ctx); // not relative addressing?
else
{
Feb 3, 2009
Feb 3, 2009
698
699
700
701
702
703
if (!relok)
fail(ctx, "Relative addressing not permitted here.");
else
retval++;
parse_source_token_maybe_relative(ctx, 0);
Dec 8, 2008
Dec 8, 2008
704
relative = 1;
Feb 11, 2009
Feb 11, 2009
705
706
if (nexttoken(ctx) != ((Token) '+'))
Dec 10, 2008
Dec 10, 2008
707
708
709
pushback(ctx);
else
{
Feb 11, 2009
Feb 11, 2009
710
711
// !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ?
if (regnum != 0)
Dec 10, 2008
Dec 10, 2008
712
fail(ctx, "Relative addressing with explicit register number.");
Feb 11, 2009
Feb 11, 2009
713
Dec 10, 2008
Dec 10, 2008
714
uint32 ui32 = 0;
Feb 11, 2009
Feb 11, 2009
715
716
717
718
if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) ||
(!ui32fromtoken(ctx, &ui32)) ||
(ctx->tokenlen != 0) )
{
Feb 3, 2009
Feb 3, 2009
719
fail(ctx, "Invalid relative addressing offset");
Feb 11, 2009
Feb 11, 2009
720
} // if
Dec 10, 2008
Dec 10, 2008
721
722
723
regnum += (int) ui32;
} // else
Feb 11, 2009
Feb 11, 2009
724
if (nexttoken(ctx) != ((Token) ']'))
Feb 3, 2009
Feb 3, 2009
725
fail(ctx, "Expected ']'");
Dec 8, 2008
Dec 8, 2008
726
} // else
Feb 3, 2009
Feb 3, 2009
728
int invalid_swizzle = 0;
Dec 8, 2008
Dec 8, 2008
729
uint32 swizzle = 0;
Feb 11, 2009
Feb 11, 2009
730
if (nexttoken(ctx) != ((Token) '.'))
Dec 8, 2008
Dec 8, 2008
731
732
733
734
{
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
735
else if (scalar_register(ctx->shader_type, regtype, regnum))
Feb 3, 2009
Feb 3, 2009
736
fail(ctx, "Swizzle specified for scalar register");
Feb 11, 2009
Feb 11, 2009
737
else if (nexttoken(ctx) != TOKEN_IDENTIFIER)
Feb 3, 2009
Feb 3, 2009
738
invalid_swizzle = 1;
Dec 8, 2008
Dec 8, 2008
739
740
else
{
Feb 11, 2009
Feb 11, 2009
741
742
743
744
char tokenbytes[5] = { '\0', '\0', '\0', '\0', '\0' };
const unsigned int tokenlen = ctx->tokenlen;
memcpy(tokenbytes, ctx->token, ((tokenlen < 4) ? tokenlen : 4));
Dec 8, 2008
Dec 8, 2008
745
// deal with shortened form (.x = .xxxx, etc).
Feb 11, 2009
Feb 11, 2009
746
747
748
749
750
751
752
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)
Feb 3, 2009
Feb 3, 2009
753
invalid_swizzle = 1;
Feb 11, 2009
Feb 11, 2009
754
tokenbytes[4] = '\0';
Dec 8, 2008
Dec 8, 2008
755
Feb 3, 2009
Feb 3, 2009
756
uint32 val = 0;
Dec 10, 2008
Dec 10, 2008
757
int i;
Dec 8, 2008
Dec 8, 2008
758
759
for (i = 0; i < 4; i++)
{
Feb 11, 2009
Feb 11, 2009
760
const int component = (int) tokenbytes[i];
Dec 8, 2008
Dec 8, 2008
761
762
switch (component)
{
Feb 28, 2009
Feb 28, 2009
763
764
765
766
case 'r': case 'x': val = 0; break;
case 'g': case 'y': val = 1; break;
case 'b': case 'z': val = 2; break;
case 'a': case 'w': val = 3; break;
Feb 3, 2009
Feb 3, 2009
767
default: invalid_swizzle = 1; break;
Dec 8, 2008
Dec 8, 2008
768
769
770
771
} // switch
swizzle |= (val << (i * 2));
} // for
} // else
Feb 3, 2009
Feb 3, 2009
773
774
775
if (invalid_swizzle)
fail(ctx, "Invalid swizzle");
Feb 11, 2009
Feb 11, 2009
776
777
778
779
780
781
782
*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) );
Dec 8, 2008
Dec 8, 2008
784
785
return retval;
} // parse_source_token_maybe_relative
Dec 8, 2008
Dec 8, 2008
788
static inline int parse_source_token(Context *ctx)
Dec 8, 2008
Dec 8, 2008
790
791
return parse_source_token_maybe_relative(ctx, 1);
} // parse_source_token
Dec 8, 2008
Dec 8, 2008
793
794
795
static int parse_args_NULL(Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
796
return 1;
Dec 8, 2008
Dec 8, 2008
797
} // parse_args_NULL
Feb 11, 2009
Feb 11, 2009
800
static int parse_num(Context *ctx, const int floatok, uint32 *value)
Dec 8, 2008
Dec 8, 2008
801
802
{
union { float f; int32 si32; uint32 ui32; } cvt;
Feb 12, 2009
Feb 12, 2009
803
804
805
806
807
808
809
810
int negative = 0;
Token token = nexttoken(ctx);
if (token == ((Token) '-'))
{
negative = 1;
token = nexttoken(ctx);
} // if
Dec 8, 2008
Dec 8, 2008
811
Feb 11, 2009
Feb 11, 2009
812
if (token == TOKEN_INT_LITERAL)
Dec 10, 2008
Dec 10, 2008
813
{
Feb 11, 2009
Feb 11, 2009
814
815
int d = 0;
sscanf(ctx->token, "%d", &d);
Feb 12, 2009
Feb 12, 2009
816
817
818
819
if (floatok)
cvt.f = (float) ((negative) ? -d : d);
else
cvt.si32 = (int32) ((negative) ? -d : d);
Feb 11, 2009
Feb 11, 2009
820
821
} // if
else if (token == TOKEN_FLOAT_LITERAL)
Dec 8, 2008
Dec 8, 2008
822
{
Feb 11, 2009
Feb 11, 2009
823
if (!floatok)
Dec 10, 2008
Dec 10, 2008
824
{
Feb 11, 2009
Feb 11, 2009
825
826
827
fail(ctx, "Expected whole number");
*value = 0;
return 0;
Dec 10, 2008
Dec 10, 2008
828
} // if
Feb 11, 2009
Feb 11, 2009
829
sscanf(ctx->token, "%f", &cvt.f);
Feb 12, 2009
Feb 12, 2009
830
831
if (negative)
cvt.f = -cvt.f;
Feb 11, 2009
Feb 11, 2009
832
833
834
835
836
837
} // if
else
{
fail(ctx, "Expected number");
*value = 0;
return 0;
Dec 8, 2008
Dec 8, 2008
838
} // else
Feb 11, 2009
Feb 11, 2009
840
*value = cvt.ui32;
Feb 3, 2009
Feb 3, 2009
841
return 1;
Dec 8, 2008
Dec 8, 2008
842
843
844
845
846
} // parse_num
static int parse_args_DEFx(Context *ctx, const int isflt)
{
Feb 3, 2009
Feb 3, 2009
847
848
849
850
851
852
853
854
855
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++]);
Dec 8, 2008
Dec 8, 2008
856
857
return 6;
} // parse_args_DEFx
Dec 8, 2008
Dec 8, 2008
860
861
862
863
static int parse_args_DEF(Context *ctx)
{
return parse_args_DEFx(ctx, 1);
} // parse_args_DEF
Dec 8, 2008
Dec 8, 2008
866
867
868
869
static int parse_args_DEFI(Context *ctx)
{
return parse_args_DEFx(ctx, 0);
} // parse_args_DEFI
Dec 8, 2008
Dec 8, 2008
872
873
static int parse_args_DEFB(Context *ctx)
{
Feb 3, 2009
Feb 3, 2009
874
875
parse_destination_token(ctx);
require_comma(ctx);
Feb 11, 2009
Feb 11, 2009
876
877
878
879
880
881
882
883
// !!! 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"))
Dec 8, 2008
Dec 8, 2008
884
ctx->tokenbuf[ctx->tokenbufpos++] = 1;
Feb 11, 2009
Feb 11, 2009
885
else if (check_token_segment(ctx, "false"))
Dec 8, 2008
Dec 8, 2008
886
887
ctx->tokenbuf[ctx->tokenbufpos++] = 0;
else
Feb 11, 2009
Feb 11, 2009
888
889
890
891
892
893
bad = 1;
if (ctx->tokenlen != 0)
bad = 1;
if (bad)
Feb 3, 2009
Feb 3, 2009
894
fail(ctx, "Expected 'true' or 'false'");
Feb 11, 2009
Feb 11, 2009
895
Dec 8, 2008
Dec 8, 2008
896
897
return 3;
} // parse_args_DEFB
Dec 12, 2008
Dec 12, 2008
900
static int parse_dcl_usage(Context *ctx, uint32 *val, int *issampler)
Dec 8, 2008
Dec 8, 2008
901
{
Nov 15, 2009
Nov 15, 2009
902
size_t i;
Feb 11, 2009
Feb 11, 2009
903
static const char *samplerusagestrs[] = { "_2d", "_cube", "_volume" };
Dec 8, 2008
Dec 8, 2008
904
static const char *usagestrs[] = {
Feb 11, 2009
Feb 11, 2009
905
906
907
"_position", "_blendweight", "_blendindices", "_normal", "_psize",
"_texcoord", "_tangent", "_binormal", "_tessfactor", "_positiont",
"_color", "_fog", "_depth", "_sample"
Dec 8, 2008
Dec 8, 2008
908
};
Dec 12, 2008
Dec 12, 2008
909
Dec 8, 2008
Dec 8, 2008
910
for (i = 0; i < STATICARRAYLEN(usagestrs); i++)
Feb 11, 2009
Feb 11, 2009
912
if (check_token_segment(ctx, usagestrs[i]))
Dec 8, 2008
Dec 8, 2008
914
915
*issampler = 0;
*val = i;
Feb 3, 2009
Feb 3, 2009
916
return 1;
Dec 8, 2008
Dec 8, 2008
918
} // for
Dec 8, 2008
Dec 8, 2008
920
for (i = 0; i < STATICARRAYLEN(samplerusagestrs); i++)
Feb 11, 2009
Feb 11, 2009
922
if (check_token_segment(ctx, samplerusagestrs[i]))
Dec 8, 2008
Dec 8, 2008
924
925
*issampler = 1;
*val = i + 2;
Feb 3, 2009
Feb 3, 2009
926
return 1;
Dec 8, 2008
Dec 8, 2008
928
} // for
Dec 13, 2008
Dec 13, 2008
930
931
*issampler = 0;
*val = 0;
Feb 3, 2009
Feb 3, 2009
932
return 0;
Dec 8, 2008
Dec 8, 2008
933
} // parse_dcl_usage
Dec 8, 2008
Dec 8, 2008
936
937
938
939
940
static int parse_args_DCL(Context *ctx)
{
int issampler = 0;
uint32 usage = 0;
uint32 index = 0;
Dec 8, 2008
Dec 8, 2008
942
ctx->tokenbufpos++; // save a spot for the usage/index token.
Feb 3, 2009
Feb 3, 2009
943
ctx->tokenbuf[0] = 0;
Dec 8, 2008
Dec 8, 2008
944
Feb 11, 2009
Feb 11, 2009
945
946
947
// 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...
Feb 3, 2009
Feb 3, 2009
948
Feb 11, 2009
Feb 11, 2009
949
950
if (parse_dcl_usage(ctx, &usage, &issampler))
{
Feb 12, 2009
Feb 12, 2009
951
952
953
954
955
if ((ctx->tokenlen > 0) && (*ctx->token != '_'))
{
if (!ui32fromtoken(ctx, &index))
fail(ctx, "Expected usage index");
} // if
Feb 11, 2009
Feb 11, 2009
956
} // if
Dec 8, 2008
Dec 8, 2008
957
Feb 3, 2009
Feb 3, 2009
958
parse_destination_token(ctx);
Dec 8, 2008
Dec 8, 2008
959
960
961
const int samplerreg = (ctx->dest_arg.regtype == REG_TYPE_SAMPLER);
if (issampler != samplerreg)
Feb 3, 2009
Feb 3, 2009
962
fail(ctx, "Invalid usage");
Dec 8, 2008
Dec 8, 2008
963
964
else if (samplerreg)
ctx->tokenbuf[0] = (usage << 27) | 0x80000000;
May 17, 2013
May 17, 2013
965
966
else if (shader_is_pixel(ctx)) // all other pixel shader types are zero'd.
ctx->tokenbuf[0] = 0x80000000;
Dec 8, 2008
Dec 8, 2008
967
968
else
ctx->tokenbuf[0] = usage | (index << 16) | 0x80000000;
969
970
971
972
973
974
975
976
return 3;
} // parse_args_DCL
static int parse_args_D(Context *ctx)
{
int retval = 1;
Dec 13, 2008
Dec 13, 2008
977
retval += parse_destination_token(ctx);
Feb 3, 2009
Feb 3, 2009
978
return retval;
979
980
981
982
983
984
} // parse_args_D
static int parse_args_S(Context *ctx)
{
int retval = 1;
Dec 8, 2008
Dec 8, 2008
985
retval += parse_source_token(ctx);
Feb 3, 2009
Feb 3, 2009
986
return retval;
987
988
989
990
991
992
} // parse_args_S
static int parse_args_SS(Context *ctx)
{
int retval = 1;
Dec 8, 2008
Dec 8, 2008
993
retval += parse_source_token(ctx);
Feb 3, 2009
Feb 3, 2009
994
require_comma(ctx);
Dec 8, 2008
Dec 8, 2008
995
retval += parse_source_token(ctx);
Feb 3, 2009
Feb 3, 2009
996
return retval;
997
998
999
1000
} // parse_args_SS
static int parse_args_DS(Context *ctx)