Skip to content

Latest commit

 

History

History
1424 lines (1182 loc) · 41.2 KB

mojoshader_assembler.c

File metadata and controls

1424 lines (1182 loc) · 41.2 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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
225
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
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
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
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
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
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
484
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* 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"
#define DEBUG_TOKENIZER 1
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;
MOJOSHADER_shaderType shader_type;
uint8 major_ver;
uint8 minor_ver;
unsigned int instruction_count;
unsigned int linenum;
char prevchar;
char token[64];
char pushedback;
char pushback_token[64];
uint32 tokenbuf[16];
int tokenbufpos;
int centroid_allowed;
DestArgInfo dest_arg;
};
// Convenience functions for allocators...
static MOJOSHADER_assembleData out_of_mem_data = {
"Out of memory", 0, 0, 0, MOJOSHADER_TYPE_UNKNOWN, 0, 0, 0, 0, 0
};
static const char *out_of_mem_str = "Out of memory";
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_supported(const uint8 maj, const uint8 min)
{
return (ver_ui32(maj,min) <= ver_ui32(MAX_SHADER_MAJOR, MAX_SHADER_MINOR));
} // shader_version_supported
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_version_exactly(const Context *ctx, const uint8 maj,
const uint8 min)
{
return ((ctx->major_ver == maj) && (ctx->minor_ver == min));
} // shader_version_exactly
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
extern void writeme(void);
static void output_token_noswap(Context *ctx, const uint32 token)
{
if (isfail(ctx))
return;
writeme();
} // 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
while (1)
{
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
if ((ch > '0') && (ch < '9'))
{
// 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.
if ((idx > 0) && ((ctx->prevchar >= '0') || (ctx->prevchar <= '9')))
{
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++;
if ((ch == '/') && (ctx->source[1] == '/'))
{
ctx->token[idx++] = '/';
ctx->source++;
} // if
ctx->token[idx++] = '\0';
} // else
return NOFAIL;
case ' ':
if (ctx->prevch == ' ')
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')
ctx->linenum++;
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" : "???",
ctx->token);
#endif
return rc;
} // tokenize
static inline void pushback(Context *ctx)
{
#if DEBUG_TOKENIZER
printf("PUSHBACK\n");
#endif
if (ctx->pushedback)
fail(ctx, "BUG: Double pushback in parser");
else
ctx->pushedback = 1;
} // 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)
break;
} // while
} // if
break;
} // while
#if DEBUG_TOKENIZER
printf("NEXTTOKEN: %s '%s'\n",
(rc == END_OF_STREAM) ? "END_OF_STREAM" :
(rc == FAIL) ? "FAIL" :
(rc == NOFAIL) ? "NOFAIL" : "???",
ctx->token);
#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
// !!! FIXME: merge parse_* into mojoshader.c to reduce cut-and-paste.
// !!! FIXME: we need to merge Context, which is the nastiest part.
static int set_result_shift(Context *ctx, DestArgInfo *info, const int val)
{
if (info->result_shift != 0)
return fail(ctx, "Multiple result shift modifiers");
info->result_shift = val;
return NOFAIL;
} // set_result_shift
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)
{
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, "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
if (neednum)
{
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
//minor = atoi(ctx->token);
char *endptr = NULL;
const long val = strtol(ctx->token, &endptr, 10);
regnum = (int) val;
if ((*ctx->token == '\0') || (*endptr != '\0'))
return fail(ctx, "Invalid version string");
} // 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
static int parse_destination_token(Context *ctx, DestArgInfo *info)
{
// !!! FIXME: recheck against the spec for ranges (like RASTOUT values, etc).
memset(info, '\0', sizeof (info));
info->token = token;
// 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;
else if (strcasecmp(ctx->token, "x2") == 0)
set_result_shift(ctx, info, 0x1);
else if (strcasecmp(ctx->token, "x4") == 0)
set_result_shift(ctx, info, 0x2);
else if (strcasecmp(ctx->token, "x8") == 0)
set_result_shift(ctx, info, 0x3);
else if (strcasecmp(ctx->token, "d8") == 0)
set_result_shift(ctx, info, 0xD);
else if (strcasecmp(ctx->token, "d4") == 0)
set_result_shift(ctx, info, 0xE);
else if (strcasecmp(ctx->token, "d2") == 0)
set_result_shift(ctx, info, 0xF);
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
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
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;
if (nexttoken(ctx, 0, 0, 1, 1) == FAIL)
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
else if (scalar_register(info->regtype, info->regnum))
return fail(ctx, "Writemask specified for scalar register");
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
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;
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) && (is_pixel_shader(ctx))
{
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++; }
} // 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;
// !!! FIXME: cut and paste from mojoshader.c ...
if (info->relative)
{
if (!shader_is_vertex(ctx))
return fail(ctx, "Relative addressing in non-vertex shader");
else if (!shader_version_atleast(ctx, 3, 0))
return fail(ctx, "Relative addressing in vertex shader version < 3.0");
else if (!ctx->have_ctab) // it's hard to do this efficiently without!
return fail(ctx, "relative addressing unsupported without a CTAB");
// !!! FIXME: I don't have a shader that has a relative dest currently.
return fail(ctx, "Relative addressing of dest tokens is unsupported");
} // if
const int s = info->result_shift;
if (s != 0)
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Result shift scale in non-pixel shader");
else if (shader_version_atleast(ctx, 2, 0))
return fail(ctx, "Result shift scale in pixel shader version >= 2.0");
else if ( ! (((s >= 1) && (s <= 3)) || ((s >= 0xD) && (s <= 0xF))) )
return fail(ctx, "Result shift scale isn't 1 to 3, or 13 to 15.");
} // if
if (info->result_mod & MOD_PP) // Partial precision (pixel shaders only)
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Partial precision result mod in non-pixel shader");
} // if
if (info->result_mod & MOD_CENTROID) // Centroid (pixel shaders only)
{
if (!shader_is_pixel(ctx))
return fail(ctx, "Centroid result mod in non-pixel shader");
else if (!ctx->centroid_allowed) // only on DCL opcodes!
return fail(ctx, "Centroid modifier not allowed here");
} // if
// !!! FIXME: from msdn:
// "_sat cannot be used with instructions writing to output o# registers."
// !!! FIXME: actually, just go over this page:
// http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/directx9_c/directx/graphics/reference/shaders/ps_instructionmodifiers.asp
if (ctx->tokenbufpos >= STATICARRAYLEN(ctx->tokenbuf))
return fail(ctx, "Too many tokens");
ctx->tokenbuf[ctx->tokenbufpos++] =
( ((((uint32) 0x80000000)) << 0) |
((((uint32) info->regnum) & 0x7ff) << 0) |
((((uint32) info->relative) & 0x1) << 13) |
((((uint32) info->result_mod) & 0xF) << 20) |
((((uint32) info->result_shift) & 0xF) << 24) |
((((uint32) info->regtype) & 0x7) << 28) |
((((uint32) info->regtype) & 0x18) << 8) );
return 1;
} // parse_destination_token
static int parse_args_NULL(Context *ctx)
{
return (isfail(ctx) ? FAIL : 1);
} // parse_args_NULL
static int parse_args_DEF(Context *ctx)
{
if (parse_destination_token(ctx, &ctx->dest_arg) == FAIL)
return FAIL;
if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
return fail(ctx, "relative addressing in DEFB");
sdfsdf
// !!! FIXME: parse out def.
ctx->dwords[0] = SWAP32(ctx->tokens[0]);
ctx->dwords[1] = SWAP32(ctx->tokens[1]);
ctx->dwords[2] = SWAP32(ctx->tokens[2]);
ctx->dwords[3] = SWAP32(ctx->tokens[3]);
return 6;
} // parse_args_DEF
static int parse_args_DEFB(Context *ctx)
{
if (parse_destination_token(ctx, &ctx->dest_arg) == FAIL)
return FAIL;
if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
return fail(ctx, "relative addressing in DEFB");
ctx->dwords[0] = *(ctx->tokens) ? 1 : 0;
return 3;
} // parse_args_DEFB
static int valid_texture_type(const uint32 ttype)
{
switch ((const TextureType) ttype)
{
case TEXTURE_TYPE_2D:
case TEXTURE_TYPE_CUBE:
case TEXTURE_TYPE_VOLUME:
return 1; // it's okay.
} // switch
return 0;
} // valid_texture_type
// !!! FIXME: this function is kind of a mess.
// !!! FIXME: cut-and-paste from mojoshader.c ...
static int parse_args_DCL(Context *ctx)
{
int unsupported = 0;
char usage[sizeof (ctx->token)];
static int nexttoken(Context *ctx, const int ignoreeol,
const int ignorewhitespace, const int eolok,
const int eosok)
char usagestr[sizeof (ctx->token)];
static const char *usagestrs[] = {
"position", "blendweight", "blendindices", "normal", "psize",
"texcoord", "tangent", "binormal", "tessfactor", "positiont",
"color", "fog", "depth", "sample"
};
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, " ") == 0)
{
pushback(ctx);
usagestr[0] = '\0';
} // else if
else if (strcmp(ctx->token, "_") != 0)
return fail(ctx, "Expected register or usage");
else if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else
strcpy(usagestr, ctx->token);
if (nexttoken(ctx, 0, 0, 0, 0) == FAIL)
return FAIL;
else if (strcmp(ctx->token, " ") == 0)
return fail(ctx, "Expected whitespace");
ctx->centroid_allowed = 1;
ctx->tokenbufpos++;
const int parse_dest_rc = parse_destination_token(ctx, &ctx->dest_arg);
ctx->centroid_allowed = 0;
if (parse_dest_rc == FAIL)
return FAIL;
if (ctx->dest_arg.result_shift != 0) // I'm pretty sure this is illegal...?
return fail(ctx, "shift scale in DCL");
else if (ctx->dest_arg.relative) // I'm pretty sure this is illegal...?
return fail(ctx, "relative addressing in DCL");
const RegisterType regtype = ctx->dest_arg.regtype;
const int regnum = ctx->dest_arg.regnum;
if ( (shader_is_pixel(ctx)) && (shader_version_atleast(ctx, 3, 0)) )
{
if (regtype == REG_TYPE_INPUT)
{
token |=
const uint32 usage = (token & 0xF);
const uint32 index = ((token >> 16) & 0xF);
reserved_mask = 0x7FF0FFE0;
ctx->dwords[0] = usage;
ctx->dwords[1] = index;
} // if
else if (regtype == REG_TYPE_MISCTYPE)
{
const MiscTypeType mt = (MiscTypeType) regnum;
if (mt == MISCTYPE_TYPE_POSITION)
reserved_mask = 0x7FFFFFFF;
else if (mt == MISCTYPE_TYPE_FACE)
{
reserved_mask = 0x7FFFFFFF;
if (!writemask_xyzw(ctx->dest_arg.orig_writemask))
return fail(ctx, "DCL face writemask must be full");
else if (ctx->dest_arg.result_mod != 0)
return fail(ctx, "DCL face result modifier must be zero");
else if (ctx->dest_arg.result_shift != 0)
return fail(ctx, "DCL face shift scale must be zero");
} // else if
else
{
unsupported = 1;
} // else
ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_UNKNOWN;
ctx->dwords[1] = 0;
} // else if
else if (regtype == REG_TYPE_TEXTURE)
{
const uint32 usage = (token & 0xF);
const uint32 index = ((token >> 16) & 0xF);
if (usage == MOJOSHADER_USAGE_TEXCOORD)
{
if (index > 7)
return fail(ctx, "DCL texcoord usage must have 0-7 index");
} // if
else if (usage == MOJOSHADER_USAGE_COLOR)
{
if (index != 0)
return fail(ctx, "DCL color usage must have 0 index");
} // else if
else
{
return fail(ctx, "Invalid DCL texture usage");
} // else
reserved_mask = 0x7FF0FFE0;
ctx->dwords[0] = usage;
ctx->dwords[1] = index;
} // else if
else if (regtype == REG_TYPE_SAMPLER)
{
const uint32 ttype = ((token >> 27) & 0xF);
if (!valid_texture_type(ttype))
return fail(ctx, "unknown sampler texture type");
reserved_mask = 0x7FFFFFF;
ctx->dwords[0] = ttype;
} // else if
else
{
unsupported = 1;
} // else
} // if
else if ( (shader_is_pixel(ctx)) && (shader_version_atleast(ctx, 2, 0)) )
{
if (regtype == REG_TYPE_INPUT)
{
ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_COLOR;
ctx->dwords[1] = regnum;
reserved_mask = 0x7FFFFFFF;
} // if
else if (regtype == REG_TYPE_TEXTURE)
{
ctx->dwords[0] = (uint32) MOJOSHADER_USAGE_TEXCOORD;
ctx->dwords[1] = regnum;
reserved_mask = 0x7FFFFFFF;
} // else if
else if (regtype == REG_TYPE_SAMPLER)
{
const uint32 ttype = ((token >> 27) & 0xF);
if (!valid_texture_type(ttype))
return fail(ctx, "unknown sampler texture type");
reserved_mask = 0x7FFFFFF;
ctx->dwords[0] = ttype;
} // else if
else
{
unsupported = 1;
} // else
} // if
else if ( (shader_is_vertex(ctx)) && (shader_version_atleast(ctx, 3, 0)) )
{
if ((regtype == REG_TYPE_INPUT) || (regtype == REG_TYPE_OUTPUT))
{
const uint32 usage = (token & 0xF);
const uint32 index = ((token >> 16) & 0xF);
reserved_mask = 0x7FF0FFE0;
ctx->dwords[0] = usage;
ctx->dwords[1] = index;
} // if
else
{
unsupported = 1;
} // else
} // else if
else if ( (shader_is_vertex(ctx)) && (shader_version_atleast(ctx, 2, 0)) )
{
if (regtype == REG_TYPE_INPUT)
{
const uint32 usage = (token & 0xF);
const uint32 index = ((token >> 16) & 0xF);
reserved_mask = 0x7FF0FFE0;
ctx->dwords[0] = usage;
ctx->dwords[1] = index;
} // if
else
{
unsupported = 1;
} // else
} // else if
else
{
unsupported = 1;
} // else
if (unsupported)
return fail(ctx, "invalid DCL register type for this shader model");
if ((token & reserved_mask) != 0)
return fail(ctx, "reserved bits in DCL dword aren't zero");
return 3;
} // parse_args_DCL
static int parse_args_D(Context *ctx)
{
int retval = 1;
retval += parse_destination_token(ctx, &ctx->dest_arg);
return isfail(ctx) ? FAIL : retval;
} // parse_args_D
static int parse_args_S(Context *ctx)
{
int retval = 1;
retval += parse_source_token(ctx, &ctx->source_args[0]);
return isfail(ctx) ? FAIL : retval;
} // parse_args_S
static int parse_args_SS(Context *ctx)
{
int retval = 1;
retval += parse_source_token(ctx, &ctx->source_args[0]);
retval += parse_source_token(ctx, &ctx->source_args[1]);
return isfail(ctx) ? FAIL : retval;
} // parse_args_SS
static int parse_args_DS(Context *ctx)
{
int retval = 1;
retval += parse_destination_token(ctx, &ctx->dest_arg);
retval += parse_source_token(ctx, &ctx->source_args[0]);
return isfail(ctx) ? FAIL : retval;
} // parse_args_DS
static int parse_args_DSS(Context *ctx)
{
int retval = 1;
retval += parse_destination_token(ctx, &ctx->dest_arg);
retval += parse_source_token(ctx, &ctx->source_args[0]);