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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
|
#include "internal_includes/tokens.h"
#include "internal_includes/structs.h"
#include "internal_includes/decode.h"
#include "stdlib.h"
#include "stdio.h"
#include "internal_includes/reflect.h"
#include "internal_includes/debug.h"
#include "internal_includes/hlslcc_malloc.h"
#define FOURCC(a, b, c, d) ((uint32_t)(uint8_t)(a) | ((uint32_t)(uint8_t)(b) << 8) | ((uint32_t)(uint8_t)(c) << 16) | ((uint32_t)(uint8_t)(d) << 24 ))
static enum {FOURCC_DXBC = FOURCC('D', 'X', 'B', 'C')}; //DirectX byte code
static enum {FOURCC_SHDR = FOURCC('S', 'H', 'D', 'R')}; //Shader model 4 code
static enum {FOURCC_SHEX = FOURCC('S', 'H', 'E', 'X')}; //Shader model 5 code
static enum {FOURCC_RDEF = FOURCC('R', 'D', 'E', 'F')}; //Resource definition (e.g. constant buffers)
static enum {FOURCC_ISGN = FOURCC('I', 'S', 'G', 'N')}; //Input signature
static enum {FOURCC_IFCE = FOURCC('I', 'F', 'C', 'E')}; //Interface (for dynamic linking)
static enum {FOURCC_OSGN = FOURCC('O', 'S', 'G', 'N')}; //Output signature
static enum {FOURCC_ISG1 = FOURCC('I', 'S', 'G', '1')}; //Input signature with Stream and MinPrecision
static enum {FOURCC_OSG1 = FOURCC('O', 'S', 'G', '1')}; //Output signature with Stream and MinPrecision
static enum {FOURCC_OSG5 = FOURCC('O', 'S', 'G', '5')}; //Output signature with Stream
typedef struct DXBCContainerHeaderTAG
{
unsigned fourcc;
uint32_t unk[4];
uint32_t one;
uint32_t totalSize;
uint32_t chunkCount;
} DXBCContainerHeader;
typedef struct DXBCChunkHeaderTAG
{
unsigned fourcc;
unsigned size;
} DXBCChunkHeader;
#ifdef _DEBUG
static uint64_t operandID = 0;
static uint64_t instructionID = 0;
#endif
#if defined(_WIN32)
#define osSprintf(dest, size, src) sprintf_s(dest, size, src)
#else
#define osSprintf(dest, size, src) sprintf(dest, src)
#endif
void DecodeNameToken(const uint32_t* pui32NameToken, Operand* psOperand)
{
const size_t MAX_BUFFER_SIZE = sizeof(psOperand->pszSpecialName);
psOperand->eSpecialName = DecodeOperandSpecialName(*pui32NameToken);
switch(psOperand->eSpecialName)
{
case NAME_UNDEFINED:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "undefined");
break;
}
case NAME_POSITION:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "position");
break;
}
case NAME_CLIP_DISTANCE:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "clipDistance");
break;
}
case NAME_CULL_DISTANCE:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "cullDistance");
break;
}
case NAME_RENDER_TARGET_ARRAY_INDEX:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "renderTargetArrayIndex");
break;
}
case NAME_VIEWPORT_ARRAY_INDEX:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "viewportArrayIndex");
break;
}
case NAME_VERTEX_ID:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "vertexID");
break;
}
case NAME_PRIMITIVE_ID:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "primitiveID");
break;
}
case NAME_INSTANCE_ID:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "instanceID");
break;
}
case NAME_IS_FRONT_FACE:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "isFrontFace");
break;
}
case NAME_SAMPLE_INDEX:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "sampleIndex");
break;
}
//For the quadrilateral domain, there are 6 factors (4 sides, 2 inner).
case NAME_FINAL_QUAD_U_EQ_0_EDGE_TESSFACTOR:
case NAME_FINAL_QUAD_V_EQ_0_EDGE_TESSFACTOR:
case NAME_FINAL_QUAD_U_EQ_1_EDGE_TESSFACTOR:
case NAME_FINAL_QUAD_V_EQ_1_EDGE_TESSFACTOR:
case NAME_FINAL_QUAD_U_INSIDE_TESSFACTOR:
case NAME_FINAL_QUAD_V_INSIDE_TESSFACTOR:
//For the triangular domain, there are 4 factors (3 sides, 1 inner)
case NAME_FINAL_TRI_U_EQ_0_EDGE_TESSFACTOR:
case NAME_FINAL_TRI_V_EQ_0_EDGE_TESSFACTOR:
case NAME_FINAL_TRI_W_EQ_0_EDGE_TESSFACTOR:
case NAME_FINAL_TRI_INSIDE_TESSFACTOR:
//For the isoline domain, there are 2 factors (detail and density).
case NAME_FINAL_LINE_DETAIL_TESSFACTOR:
case NAME_FINAL_LINE_DENSITY_TESSFACTOR:
{
osSprintf(psOperand->pszSpecialName, MAX_BUFFER_SIZE, "tessFactor");
break;
}
default:
{
ASSERT(0);
break;
}
}
return;
}
// Find the declaration of the texture described by psTextureOperand and
// mark it as a shadow type. (e.g. accessed via sampler2DShadow rather than sampler2D)
void MarkTextureAsShadow(ShaderInfo* psShaderInfo, Declaration* psDeclList, const uint32_t ui32DeclCount, const Operand* psTextureOperand)
{
ResourceBinding* psBinding = 0;
Declaration* psDecl = psDeclList;
uint32_t i;
ASSERT(psTextureOperand->eType == OPERAND_TYPE_RESOURCE);
for(i = 0; i < ui32DeclCount; ++i)
{
if(psDecl->eOpcode == OPCODE_DCL_RESOURCE)
{
if(psDecl->asOperands[0].eType == OPERAND_TYPE_RESOURCE &&
psDecl->asOperands[0].ui32RegisterNumber == psTextureOperand->ui32RegisterNumber)
{
psDecl->ui32IsShadowTex = 1;
break;
}
}
psDecl++;
}
}
uint32_t DecodeOperand (const uint32_t *pui32Tokens, Operand* psOperand)
{
int i;
uint32_t ui32NumTokens = 1;
OPERAND_NUM_COMPONENTS eNumComponents;
#ifdef _DEBUG
psOperand->id = operandID++;
#endif
//Some defaults
psOperand->iWriteMaskEnabled = 1;
psOperand->iGSInput = 0;
psOperand->aeDataType[0] = SVT_FLOAT;
psOperand->aeDataType[1] = SVT_FLOAT;
psOperand->aeDataType[2] = SVT_FLOAT;
psOperand->aeDataType[3] = SVT_FLOAT;
psOperand->iExtended = DecodeIsOperandExtended(*pui32Tokens);
psOperand->eModifier = OPERAND_MODIFIER_NONE;
psOperand->psSubOperand[0] = 0;
psOperand->psSubOperand[1] = 0;
psOperand->psSubOperand[2] = 0;
/* Check if this instruction is extended. If it is,
* we need to print the information first */
if (psOperand->iExtended)
{
/* OperandToken1 is the second token */
ui32NumTokens++;
if(DecodeExtendedOperandType(pui32Tokens[1]) == EXTENDED_OPERAND_MODIFIER)
{
psOperand->eModifier = DecodeExtendedOperandModifier(pui32Tokens[1]);
psOperand->eMinPrecision = DecodeOperandMinPrecision(pui32Tokens[1]);
}
}
psOperand->iIndexDims = DecodeOperandIndexDimension(*pui32Tokens);
psOperand->eType = DecodeOperandType(*pui32Tokens);
psOperand->ui32RegisterNumber = 0;
eNumComponents = DecodeOperandNumComponents(*pui32Tokens);
switch(eNumComponents)
{
case OPERAND_1_COMPONENT:
{
psOperand->iNumComponents = 1;
break;
}
case OPERAND_4_COMPONENT:
{
psOperand->iNumComponents = 4;
break;
}
default:
{
psOperand->iNumComponents = 0;
break;
}
}
if(psOperand->iWriteMaskEnabled &&
psOperand->iNumComponents == 4)
{
psOperand->eSelMode = DecodeOperand4CompSelMode(*pui32Tokens);
if(psOperand->eSelMode == OPERAND_4_COMPONENT_MASK_MODE)
{
psOperand->ui32CompMask = DecodeOperand4CompMask(*pui32Tokens);
}
else
if(psOperand->eSelMode == OPERAND_4_COMPONENT_SWIZZLE_MODE)
{
psOperand->ui32Swizzle = DecodeOperand4CompSwizzle(*pui32Tokens);
if(psOperand->ui32Swizzle != NO_SWIZZLE)
{
psOperand->aui32Swizzle[0] = DecodeOperand4CompSwizzleSource(*pui32Tokens, 0);
psOperand->aui32Swizzle[1] = DecodeOperand4CompSwizzleSource(*pui32Tokens, 1);
psOperand->aui32Swizzle[2] = DecodeOperand4CompSwizzleSource(*pui32Tokens, 2);
psOperand->aui32Swizzle[3] = DecodeOperand4CompSwizzleSource(*pui32Tokens, 3);
}
else
{
psOperand->aui32Swizzle[0] = OPERAND_4_COMPONENT_X;
psOperand->aui32Swizzle[1] = OPERAND_4_COMPONENT_Y;
psOperand->aui32Swizzle[2] = OPERAND_4_COMPONENT_Z;
psOperand->aui32Swizzle[3] = OPERAND_4_COMPONENT_W;
}
}
else
if(psOperand->eSelMode == OPERAND_4_COMPONENT_SELECT_1_MODE)
{
psOperand->aui32Swizzle[0] = DecodeOperand4CompSel1(*pui32Tokens);
}
}
//Set externally to this function based on the instruction opcode.
psOperand->iIntegerImmediate = 0;
if(psOperand->eType == OPERAND_TYPE_IMMEDIATE32)
{
for(i=0; i< psOperand->iNumComponents; ++i)
{
psOperand->afImmediates[i] = *((float*)(&pui32Tokens[ui32NumTokens]));
ui32NumTokens ++;
}
}
else
if(psOperand->eType == OPERAND_TYPE_IMMEDIATE64)
{
for(i=0; i< psOperand->iNumComponents; ++i)
{
psOperand->adImmediates[i] = *((double*)(&pui32Tokens[ui32NumTokens]));
ui32NumTokens +=2;
}
}
for(i=0; i <psOperand->iIndexDims; ++i)
{
OPERAND_INDEX_REPRESENTATION eRep = DecodeOperandIndexRepresentation(i ,*pui32Tokens);
psOperand->eIndexRep[i] = eRep;
psOperand->aui32ArraySizes[i] = 0;
psOperand->ui32RegisterNumber = 0;
switch(eRep)
{
case OPERAND_INDEX_IMMEDIATE32:
{
psOperand->ui32RegisterNumber = *(pui32Tokens+ui32NumTokens);
psOperand->aui32ArraySizes[i] = psOperand->ui32RegisterNumber;
break;
}
case OPERAND_INDEX_RELATIVE:
{
psOperand->psSubOperand[i] = hlslcc_malloc(sizeof(Operand));
DecodeOperand(pui32Tokens+ui32NumTokens, psOperand->psSubOperand[i]);
ui32NumTokens++;
break;
}
case OPERAND_INDEX_IMMEDIATE32_PLUS_RELATIVE:
{
psOperand->ui32RegisterNumber = *(pui32Tokens+ui32NumTokens);
psOperand->aui32ArraySizes[i] = psOperand->ui32RegisterNumber;
ui32NumTokens++;
psOperand->psSubOperand[i] = hlslcc_malloc(sizeof(Operand));
DecodeOperand(pui32Tokens+ui32NumTokens, psOperand->psSubOperand[i]);
ui32NumTokens++;
break;
}
default:
{
ASSERT(0);
break;
}
}
ui32NumTokens++;
}
psOperand->pszSpecialName[0] ='\0';
return ui32NumTokens;
}
const uint32_t* DecodeDeclaration(Shader* psShader, const uint32_t* pui32Token, Declaration* psDecl)
{
uint32_t ui32TokenLength = DecodeInstructionLength(*pui32Token);
const uint32_t bExtended = DecodeIsOpcodeExtended(*pui32Token);
const OPCODE_TYPE eOpcode = DecodeOpcodeType(*pui32Token);
uint32_t ui32OperandOffset = 1;
if(eOpcode < NUM_OPCODES && eOpcode >= 0)
{
psShader->aiOpcodeUsed[eOpcode] = 1;
}
psDecl->eOpcode = eOpcode;
psDecl->ui32IsShadowTex = 0;
if(bExtended)
{
ui32OperandOffset = 2;
}
switch (eOpcode)
{
case OPCODE_DCL_RESOURCE: // DCL* opcodes have
{
psDecl->value.eResourceDimension = DecodeResourceDimension(*pui32Token);
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_CONSTANT_BUFFER: // custom operand formats.
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_SAMPLER:
{
break;
}
case OPCODE_DCL_INDEX_RANGE:
{
psDecl->ui32NumOperands = 1;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
psDecl->value.ui32IndexRange = pui32Token[ui32OperandOffset];
if(psDecl->asOperands[0].eType == OPERAND_TYPE_INPUT)
{
uint32_t i;
const uint32_t indexRange = psDecl->value.ui32IndexRange;
const uint32_t reg = psDecl->asOperands[0].ui32RegisterNumber;
psShader->aIndexedInput[reg] = indexRange;
psShader->aIndexedInputParents[reg] = reg;
//-1 means don't declare this input because it falls in
//the range of an already declared array.
for(i=reg+1; i<reg+indexRange; ++i)
{
psShader->aIndexedInput[i] = -1;
psShader->aIndexedInputParents[i] = reg;
}
}
if(psDecl->asOperands[0].eType == OPERAND_TYPE_OUTPUT)
{
psShader->aIndexedOutput[psDecl->asOperands[0].ui32RegisterNumber] = psDecl->value.ui32IndexRange;
}
break;
}
case OPCODE_DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY:
{
psDecl->value.eOutputPrimitiveTopology = DecodeGSOutputPrimitiveTopology(*pui32Token);
break;
}
case OPCODE_DCL_GS_INPUT_PRIMITIVE:
{
psDecl->value.eInputPrimitive = DecodeGSInputPrimitive(*pui32Token);
break;
}
case OPCODE_DCL_MAX_OUTPUT_VERTEX_COUNT:
{
psDecl->value.ui32MaxOutputVertexCount = pui32Token[1];
break;
}
case OPCODE_DCL_TESS_PARTITIONING:
{
psDecl->value.eTessPartitioning = DecodeTessPartitioning(*pui32Token);
break;
}
case OPCODE_DCL_TESS_DOMAIN:
{
psDecl->value.eTessDomain = DecodeTessDomain(*pui32Token);
break;
}
case OPCODE_DCL_TESS_OUTPUT_PRIMITIVE:
{
psDecl->value.eTessOutPrim = DecodeTessOutPrim(*pui32Token);
break;
}
case OPCODE_DCL_THREAD_GROUP:
{
psDecl->value.aui32WorkGroupSize[0] = pui32Token[1];
psDecl->value.aui32WorkGroupSize[1] = pui32Token[2];
psDecl->value.aui32WorkGroupSize[2] = pui32Token[3];
break;
}
case OPCODE_DCL_INPUT:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_INPUT_SIV:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
if(psShader->eShaderType == PIXEL_SHADER)
{
psDecl->value.eInterpolation = DecodeInterpolationMode(*pui32Token);
}
break;
}
case OPCODE_DCL_INPUT_PS:
{
psDecl->ui32NumOperands = 1;
psDecl->value.eInterpolation = DecodeInterpolationMode(*pui32Token);
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_INPUT_SGV:
case OPCODE_DCL_INPUT_PS_SGV:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
DecodeNameToken(pui32Token + 3, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_INPUT_PS_SIV:
{
psDecl->ui32NumOperands = 1;
psDecl->value.eInterpolation = DecodeInterpolationMode(*pui32Token);
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
DecodeNameToken(pui32Token + 3, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_OUTPUT:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_OUTPUT_SGV:
{
break;
}
case OPCODE_DCL_OUTPUT_SIV:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
DecodeNameToken(pui32Token + 3, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_TEMPS:
{
psDecl->value.ui32NumTemps = *(pui32Token+ui32OperandOffset);
break;
}
case OPCODE_DCL_INDEXABLE_TEMP:
{
psDecl->sIdxTemp.ui32RegIndex = *(pui32Token+ui32OperandOffset);
psDecl->sIdxTemp.ui32RegCount = *(pui32Token+ui32OperandOffset+1);
psDecl->sIdxTemp.ui32RegComponentSize = *(pui32Token+ui32OperandOffset+2);
break;
}
case OPCODE_DCL_GLOBAL_FLAGS:
{
psDecl->value.ui32GlobalFlags = DecodeGlobalFlags(*pui32Token);
break;
}
case OPCODE_DCL_INTERFACE:
{
uint32_t func = 0, numClassesImplementingThisInterface, arrayLen, interfaceID;
interfaceID = pui32Token[ui32OperandOffset];
ui32OperandOffset++;
psDecl->ui32TableLength = pui32Token[ui32OperandOffset];
ui32OperandOffset++;
numClassesImplementingThisInterface = DecodeInterfaceTableLength(*(pui32Token+ui32OperandOffset));
arrayLen = DecodeInterfaceArrayLength(*(pui32Token+ui32OperandOffset));
ui32OperandOffset++;
psDecl->value.interface.ui32InterfaceID = interfaceID;
psDecl->value.interface.ui32NumFuncTables = numClassesImplementingThisInterface;
psDecl->value.interface.ui32ArraySize = arrayLen;
psShader->funcPointer[interfaceID].ui32NumBodiesPerTable = psDecl->ui32TableLength;
for(;func < numClassesImplementingThisInterface; ++func)
{
uint32_t ui32FuncTable = *(pui32Token+ui32OperandOffset);
psShader->aui32FuncTableToFuncPointer[ui32FuncTable] = interfaceID;
psShader->funcPointer[interfaceID].aui32FuncTables[func] = ui32FuncTable;
ui32OperandOffset++;
}
break;
}
case OPCODE_DCL_FUNCTION_BODY:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_FUNCTION_TABLE:
{
uint32_t ui32Func;
const uint32_t ui32FuncTableID = pui32Token[ui32OperandOffset++];
const uint32_t ui32NumFuncsInTable = pui32Token[ui32OperandOffset++];
for(ui32Func=0; ui32Func<ui32NumFuncsInTable;++ui32Func)
{
const uint32_t ui32FuncBodyID = pui32Token[ui32OperandOffset++];
psShader->aui32FuncBodyToFuncTable[ui32FuncBodyID] = ui32FuncTableID;
psShader->funcTable[ui32FuncTableID].aui32FuncBodies[ui32Func] = ui32FuncBodyID;
}
// OpcodeToken0 is followed by a DWORD that represents the function table
// identifier and another DWORD (TableLength) that gives the number of
// functions in the table.
//
// This is followed by TableLength DWORDs which are function body indices.
//
break;
}
case OPCODE_DCL_INPUT_CONTROL_POINT_COUNT:
{
break;
}
case OPCODE_HS_DECLS:
{
break;
}
case OPCODE_DCL_OUTPUT_CONTROL_POINT_COUNT:
{
psDecl->value.ui32MaxOutputVertexCount = DecodeOutputControlPointCount(*pui32Token);
break;
}
case OPCODE_HS_JOIN_PHASE:
case OPCODE_HS_FORK_PHASE:
case OPCODE_HS_CONTROL_POINT_PHASE:
{
break;
}
case OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT:
{
ASSERT(psShader->ui32ForkPhaseCount != 0);//Check for wrapping when we decrement.
psDecl->value.aui32HullPhaseInstanceInfo[0] = psShader->ui32ForkPhaseCount-1;
psDecl->value.aui32HullPhaseInstanceInfo[1] = pui32Token[1];
break;
}
case OPCODE_CUSTOMDATA:
{
ui32TokenLength = pui32Token[1];
{
int iTupleSrc = 0, iTupleDest = 0;
//const uint32_t ui32ConstCount = pui32Token[1] - 2;
//const uint32_t ui32TupleCount = (ui32ConstCount / 4);
CUSTOMDATA_CLASS eClass = DecodeCustomDataClass(pui32Token[0]);
const uint32_t ui32NumVec4 = (ui32TokenLength - 2) / 4;
uint32_t uIdx = 0;
ICBVec4 const *pVec4Array = (void*) (pui32Token + 2);
//The buffer will contain at least one value, but not more than 4096 scalars/1024 vec4's.
ASSERT(ui32NumVec4 < MAX_IMMEDIATE_CONST_BUFFER_VEC4_SIZE);
/* must be a multiple of 4 */
ASSERT(((ui32TokenLength - 2) % 4) == 0);
for (uIdx = 0; uIdx < ui32NumVec4; uIdx++)
{
psDecl->asImmediateConstBuffer[uIdx] = pVec4Array[uIdx];
}
psDecl->ui32NumOperands = ui32NumVec4;
}
break;
}
case OPCODE_DCL_HS_MAX_TESSFACTOR:
{
psDecl->value.fMaxTessFactor = *((float*)&pui32Token[1]);
break;
}
case OPCODE_DCL_UNORDERED_ACCESS_VIEW_TYPED:
{
psDecl->ui32NumOperands = 2;
psDecl->value.eResourceDimension = DecodeResourceDimension(*pui32Token);
psDecl->sUAV.ui32GloballyCoherentAccess = DecodeAccessCoherencyFlags(*pui32Token);
psDecl->sUAV.bCounter = 0;
psDecl->sUAV.ui32BufferSize = 0;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
psDecl->sUAV.Type = DecodeResourceReturnType(0, pui32Token[ui32OperandOffset]);
break;
}
case OPCODE_DCL_UNORDERED_ACCESS_VIEW_RAW:
{
ResourceBinding* psBinding = NULL;
ConstantBuffer* psBuffer = NULL;
psDecl->ui32NumOperands = 1;
psDecl->sUAV.ui32GloballyCoherentAccess = DecodeAccessCoherencyFlags(*pui32Token);
psDecl->sUAV.bCounter = 0;
psDecl->sUAV.ui32BufferSize = 0;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
//This should be a RTYPE_UAV_RWBYTEADDRESS buffer. It is memory backed by
//a shader storage buffer whose is unknown at compile time.
psDecl->sUAV.ui32BufferSize = 0;
break;
}
case OPCODE_DCL_UNORDERED_ACCESS_VIEW_STRUCTURED:
{
ResourceBinding* psBinding = NULL;
ConstantBuffer* psBuffer = NULL;
psDecl->ui32NumOperands = 1;
psDecl->sUAV.ui32GloballyCoherentAccess = DecodeAccessCoherencyFlags(*pui32Token);
psDecl->sUAV.bCounter = 0;
psDecl->sUAV.ui32BufferSize = 0;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
GetResourceFromBindingPoint(RGROUP_UAV, psDecl->asOperands[0].ui32RegisterNumber, &psShader->sInfo, &psBinding);
GetConstantBufferFromBindingPoint(RGROUP_UAV, psBinding->ui32BindPoint, &psShader->sInfo, &psBuffer);
psDecl->sUAV.ui32BufferSize = psBuffer->ui32TotalSizeInBytes;
switch(psBinding->eType)
{
case RTYPE_UAV_RWSTRUCTURED_WITH_COUNTER:
case RTYPE_UAV_APPEND_STRUCTURED:
case RTYPE_UAV_CONSUME_STRUCTURED:
psDecl->sUAV.bCounter = 1;
break;
default:
break;
}
break;
}
case OPCODE_DCL_RESOURCE_STRUCTURED:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_RESOURCE_RAW:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED:
{
ResourceBinding* psBinding = NULL;
ConstantBuffer* psBuffer = NULL;
psDecl->ui32NumOperands = 1;
psDecl->sUAV.ui32GloballyCoherentAccess = 0;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
psDecl->sTGSM.ui32Stride = pui32Token[ui32OperandOffset++];
psDecl->sTGSM.ui32Count = pui32Token[ui32OperandOffset++];
break;
}
case OPCODE_DCL_THREAD_GROUP_SHARED_MEMORY_RAW:
{
ResourceBinding* psBinding = NULL;
ConstantBuffer* psBuffer = NULL;
psDecl->ui32NumOperands = 1;
psDecl->sUAV.ui32GloballyCoherentAccess = 0;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
psDecl->sTGSM.ui32Stride = 4;
psDecl->sTGSM.ui32Count = pui32Token[ui32OperandOffset++];
break;
}
case OPCODE_DCL_STREAM:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
case OPCODE_DCL_GS_INSTANCE_COUNT:
{
psDecl->ui32NumOperands = 0;
psDecl->value.ui32GSInstanceCount = pui32Token[1];
break;
}
default:
{
//Reached end of declarations
return 0;
}
}
return pui32Token + ui32TokenLength;
}
const uint32_t* DeocdeInstruction(const uint32_t* pui32Token, Instruction* psInst, Shader* psShader)
{
uint32_t ui32TokenLength = DecodeInstructionLength(*pui32Token);
const uint32_t bExtended = DecodeIsOpcodeExtended(*pui32Token);
const OPCODE_TYPE eOpcode = DecodeOpcodeType(*pui32Token);
uint32_t ui32OperandOffset = 1;
#ifdef _DEBUG
psInst->id = instructionID++;
#endif
psInst->eOpcode = eOpcode;
psInst->bSaturate = DecodeInstructionSaturate(*pui32Token);
psInst->bAddressOffset = 0;
psInst->ui32FirstSrc = 1;
if(bExtended)
{
do {
const uint32_t ui32ExtOpcodeToken = pui32Token[ui32OperandOffset];
const EXTENDED_OPCODE_TYPE eExtType = DecodeExtendedOpcodeType(ui32ExtOpcodeToken);
if(eExtType == EXTENDED_OPCODE_SAMPLE_CONTROLS)
{
struct {int i4:4;} sU;
struct {int i4:4;} sV;
struct {int i4:4;} sW;
psInst->bAddressOffset = 1;
sU.i4 = DecodeImmediateAddressOffset(
IMMEDIATE_ADDRESS_OFFSET_U, ui32ExtOpcodeToken);
sV.i4 = DecodeImmediateAddressOffset(
IMMEDIATE_ADDRESS_OFFSET_V, ui32ExtOpcodeToken);
sW.i4 = DecodeImmediateAddressOffset(
IMMEDIATE_ADDRESS_OFFSET_W, ui32ExtOpcodeToken);
psInst->iUAddrOffset = sU.i4;
psInst->iVAddrOffset = sV.i4;
psInst->iWAddrOffset = sW.i4;
}
else if(eExtType == EXTENDED_OPCODE_RESOURCE_RETURN_TYPE)
{
psInst->xType = DecodeExtendedResourceReturnType(0, ui32ExtOpcodeToken);
psInst->yType = DecodeExtendedResourceReturnType(1, ui32ExtOpcodeToken);
psInst->zType = DecodeExtendedResourceReturnType(2, ui32ExtOpcodeToken);
psInst->wType = DecodeExtendedResourceReturnType(3, ui32ExtOpcodeToken);
}
else if(eExtType == EXTENDED_OPCODE_RESOURCE_DIM)
{
psInst->eResDim = DecodeExtendedResourceDimension(ui32ExtOpcodeToken);
}
ui32OperandOffset++;
}
while(DecodeIsOpcodeExtended(pui32Token[ui32OperandOffset-1]));
}
if(eOpcode < NUM_OPCODES && eOpcode >= 0)
{
psShader->aiOpcodeUsed[eOpcode] = 1;
}
switch (eOpcode)
{
//no operands
case OPCODE_CUT:
case OPCODE_EMIT:
case OPCODE_EMITTHENCUT:
case OPCODE_RET:
case OPCODE_LOOP:
case OPCODE_ENDLOOP:
case OPCODE_BREAK:
case OPCODE_ELSE:
case OPCODE_ENDIF:
case OPCODE_CONTINUE:
case OPCODE_DEFAULT:
case OPCODE_ENDSWITCH:
case OPCODE_NOP:
case OPCODE_HS_CONTROL_POINT_PHASE:
case OPCODE_HS_FORK_PHASE:
case OPCODE_HS_JOIN_PHASE:
{
psInst->ui32NumOperands = 0;
psInst->ui32FirstSrc = 0;
break;
}
case OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT:
{
psInst->ui32NumOperands = 0;
psInst->ui32FirstSrc = 0;
break;
}
case OPCODE_SYNC:
{
psInst->ui32NumOperands = 0;
psInst->ui32FirstSrc = 0;
psInst->ui32SyncFlags = DecodeSyncFlags(*pui32Token);
break;
}
//1 operand
case OPCODE_EMIT_STREAM:
case OPCODE_CUT_STREAM:
case OPCODE_EMITTHENCUT_STREAM:
case OPCODE_CASE:
case OPCODE_SWITCH:
case OPCODE_LABEL:
{
psInst->ui32NumOperands = 1;
psInst->ui32FirstSrc = 0;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
if(eOpcode == OPCODE_CASE)
{
psInst->asOperands[0].iIntegerImmediate = 1;
}
break;
}
case OPCODE_INTERFACE_CALL:
{
psInst->ui32NumOperands = 1;
psInst->ui32FirstSrc = 0;
psInst->ui32FuncIndexWithinInterface = pui32Token[ui32OperandOffset];
ui32OperandOffset++;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
break;
}
/* Floating point instruction decodes */
//Instructions with two operands go here
case OPCODE_MOV:
{
psInst->ui32NumOperands = 2;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
//Mov with an integer dest. If src is an immediate then it must be encoded as an integer.
if(psInst->asOperands[0].eMinPrecision == OPERAND_MIN_PRECISION_SINT_16 ||
psInst->asOperands[0].eMinPrecision == OPERAND_MIN_PRECISION_UINT_16)
{
psInst->asOperands[1].iIntegerImmediate = 1;
}
break;
}
case OPCODE_LOG:
case OPCODE_RSQ:
case OPCODE_EXP:
case OPCODE_SQRT:
case OPCODE_ROUND_PI:
case OPCODE_ROUND_NI:
case OPCODE_ROUND_Z:
case OPCODE_ROUND_NE:
case OPCODE_FRC:
case OPCODE_FTOU:
case OPCODE_FTOI:
case OPCODE_UTOF:
case OPCODE_ITOF:
case OPCODE_INEG:
case OPCODE_IMM_ATOMIC_ALLOC:
case OPCODE_IMM_ATOMIC_CONSUME:
case OPCODE_DMOV:
case OPCODE_DTOF:
case OPCODE_FTOD:
case OPCODE_DRCP:
case OPCODE_COUNTBITS:
case OPCODE_FIRSTBIT_HI:
case OPCODE_FIRSTBIT_LO:
case OPCODE_FIRSTBIT_SHI:
case OPCODE_BFREV:
case OPCODE_F32TOF16:
case OPCODE_F16TOF32:
case OPCODE_RCP:
case OPCODE_DERIV_RTX:
case OPCODE_DERIV_RTY:
case OPCODE_DERIV_RTX_COARSE:
case OPCODE_DERIV_RTX_FINE:
case OPCODE_DERIV_RTY_COARSE:
case OPCODE_DERIV_RTY_FINE:
case OPCODE_NOT:
{
psInst->ui32NumOperands = 2;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
break;
}
//Instructions with three operands go here
case OPCODE_SINCOS:
{
psInst->ui32FirstSrc = 2;
//Intentional fall-through
}
case OPCODE_IMIN:
case OPCODE_MIN:
case OPCODE_IMAX:
case OPCODE_MAX:
case OPCODE_MUL:
case OPCODE_DIV:
case OPCODE_ADD:
case OPCODE_DP2:
case OPCODE_DP3:
case OPCODE_DP4:
case OPCODE_NE:
case OPCODE_OR:
case OPCODE_XOR:
case OPCODE_LT:
case OPCODE_IEQ:
case OPCODE_IADD:
case OPCODE_AND:
case OPCODE_GE:
case OPCODE_IGE:
case OPCODE_EQ:
case OPCODE_USHR:
case OPCODE_ISHL:
case OPCODE_ISHR:
case OPCODE_LD:
case OPCODE_ILT:
case OPCODE_INE:
case OPCODE_UGE:
case OPCODE_ULT:
case OPCODE_ATOMIC_AND:
case OPCODE_ATOMIC_IADD:
case OPCODE_ATOMIC_OR:
case OPCODE_ATOMIC_XOR:
case OPCODE_ATOMIC_IMAX:
case OPCODE_ATOMIC_IMIN:
case OPCODE_ATOMIC_UMAX:
case OPCODE_ATOMIC_UMIN:
case OPCODE_DADD:
case OPCODE_DMAX:
case OPCODE_DMIN:
case OPCODE_DMUL:
case OPCODE_DEQ:
case OPCODE_DGE:
case OPCODE_DLT:
case OPCODE_DNE:
case OPCODE_DDIV:
{
psInst->ui32NumOperands = 3;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
break;
}
//Instructions with four operands go here
case OPCODE_MAD:
case OPCODE_MOVC:
case OPCODE_IMAD:
case OPCODE_UDIV:
case OPCODE_LOD:
case OPCODE_SAMPLE:
case OPCODE_GATHER4:
case OPCODE_LD_MS:
case OPCODE_UBFE:
case OPCODE_IBFE:
case OPCODE_ATOMIC_CMP_STORE:
case OPCODE_IMM_ATOMIC_IADD:
case OPCODE_IMM_ATOMIC_AND:
case OPCODE_IMM_ATOMIC_OR:
case OPCODE_IMM_ATOMIC_XOR:
case OPCODE_IMM_ATOMIC_EXCH:
case OPCODE_IMM_ATOMIC_IMAX:
case OPCODE_IMM_ATOMIC_IMIN:
case OPCODE_IMM_ATOMIC_UMAX:
case OPCODE_IMM_ATOMIC_UMIN:
case OPCODE_DMOVC:
case OPCODE_DFMA:
case OPCODE_IMUL:
{
psInst->ui32NumOperands = 4;
if(eOpcode == OPCODE_IMUL)
{
psInst->ui32FirstSrc = 2;
}
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[3]);
break;
}
case OPCODE_GATHER4_PO:
case OPCODE_SAMPLE_L:
case OPCODE_BFI:
case OPCODE_SWAPC:
case OPCODE_IMM_ATOMIC_CMP_EXCH:
{
psInst->ui32NumOperands = 5;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[3]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[4]);
break;
}
case OPCODE_GATHER4_C:
case OPCODE_SAMPLE_C:
case OPCODE_SAMPLE_C_LZ:
case OPCODE_SAMPLE_B:
{
psInst->ui32NumOperands = 5;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[3]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[4]);
MarkTextureAsShadow(&psShader->sInfo, psShader->psDecl, psShader->ui32DeclCount, &psInst->asOperands[2]);
break;
}
case OPCODE_GATHER4_PO_C:
case OPCODE_SAMPLE_D:
{
psInst->ui32NumOperands = 6;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[3]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[4]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[5]);
MarkTextureAsShadow(&psShader->sInfo, psShader->psDecl, psShader->ui32DeclCount, &psInst->asOperands[2]);
break;
}
case OPCODE_IF:
case OPCODE_BREAKC:
case OPCODE_CONTINUEC:
case OPCODE_RETC:
case OPCODE_DISCARD:
{
psInst->eBooleanTestType = DecodeInstrTestBool(*pui32Token);
psInst->ui32NumOperands = 1;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
break;
}
case OPCODE_CALLC:
{
psInst->eBooleanTestType = DecodeInstrTestBool(*pui32Token);
psInst->ui32NumOperands = 2;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
break;
}
case OPCODE_CUSTOMDATA:
{
psInst->ui32NumOperands = 0;
ui32TokenLength = pui32Token[1];
break;
}
case OPCODE_EVAL_CENTROID:
{
psInst->ui32NumOperands = 2;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
break;
}
case OPCODE_EVAL_SAMPLE_INDEX:
case OPCODE_EVAL_SNAPPED:
case OPCODE_STORE_UAV_TYPED:
case OPCODE_LD_UAV_TYPED:
case OPCODE_LD_RAW:
case OPCODE_STORE_RAW:
{
psInst->ui32NumOperands = 3;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
break;
}
case OPCODE_STORE_STRUCTURED:
case OPCODE_LD_STRUCTURED:
{
psInst->ui32NumOperands = 4;
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[3]);
break;
}
case OPCODE_RESINFO:
{
psInst->ui32NumOperands = 3;
psInst->eResInfoReturnType = DecodeResInfoReturnType(pui32Token[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[0]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[1]);
ui32OperandOffset += DecodeOperand(pui32Token+ui32OperandOffset, &psInst->asOperands[2]);
break;
}
case OPCODE_MSAD:
default:
{
ASSERT(0);
break;
}
}
UpdateOperandReferences(psShader, psInst);
return pui32Token + ui32TokenLength;
}
void UpdateOperandReferences(Shader* psShader, Instruction* psInst)
{
uint32_t ui32Operand;
const uint32_t ui32NumOperands = psInst->ui32NumOperands;
for(ui32Operand = 0; ui32Operand < ui32NumOperands; ++ui32Operand)
{
Operand* psOperand = &psInst->asOperands[ui32Operand];
if(psOperand->eType == OPERAND_TYPE_INPUT ||
psOperand->eType == OPERAND_TYPE_INPUT_CONTROL_POINT)
{
if(psOperand->iIndexDims == INDEX_2D)
{
if(psOperand->aui32ArraySizes[1] != 0)//gl_in[].gl_Position
{
psShader->abInputReferencedByInstruction[psOperand->ui32RegisterNumber] = 1;
}
}
else
{
psShader->abInputReferencedByInstruction[psOperand->ui32RegisterNumber] = 1;
}
}
}
}
const uint32_t* DecodeHullShaderJoinPhase(const uint32_t* pui32Tokens, Shader* psShader)
{
const uint32_t* pui32CurrentToken = pui32Tokens;
const uint32_t ui32ShaderLength = psShader->ui32ShaderLength;
Instruction* psInst;
//Declarations
Declaration* psDecl;
psDecl = hlslcc_malloc(sizeof(Declaration) * ui32ShaderLength);
psShader->psHSJoinPhaseDecl = psDecl;
psShader->ui32HSJoinDeclCount = 0;
while(1) //Keep going until we reach the first non-declaration token, or the end of the shader.
{
const uint32_t* pui32Result = DecodeDeclaration(psShader, pui32CurrentToken, psDecl);
if(pui32Result)
{
pui32CurrentToken = pui32Result;
psShader->ui32HSJoinDeclCount++;
psDecl++;
if(pui32CurrentToken >= (psShader->pui32FirstToken + ui32ShaderLength))
{
break;
}
}
else
{
break;
}
}
//Instructions
psInst = hlslcc_malloc(sizeof(Instruction) * ui32ShaderLength);
psShader->psHSJoinPhaseInstr = psInst;
psShader->ui32HSJoinInstrCount = 0;
while (pui32CurrentToken < (psShader->pui32FirstToken + ui32ShaderLength))
{
const uint32_t* nextInstr = DeocdeInstruction(pui32CurrentToken, psInst, psShader);
#ifdef _DEBUG
if(nextInstr == pui32CurrentToken)
{
ASSERT(0);
break;
}
#endif
pui32CurrentToken = nextInstr;
psShader->ui32HSJoinInstrCount++;
psInst++;
}
return pui32CurrentToken;
}
const uint32_t* DecodeHullShaderForkPhase(const uint32_t* pui32Tokens, Shader* psShader)
{
const uint32_t* pui32CurrentToken = pui32Tokens;
const uint32_t ui32ShaderLength = psShader->ui32ShaderLength;
const uint32_t ui32ForkPhaseIndex = psShader->ui32ForkPhaseCount;
Instruction* psInst;
//Declarations
Declaration* psDecl;
psDecl = hlslcc_malloc(sizeof(Declaration) * ui32ShaderLength);
ASSERT(ui32ForkPhaseIndex < MAX_FORK_PHASES);
psShader->ui32ForkPhaseCount++;
psShader->apsHSForkPhaseDecl[ui32ForkPhaseIndex] = psDecl;
psShader->aui32HSForkDeclCount[ui32ForkPhaseIndex] = 0;
while(1) //Keep going until we reach the first non-declaration token, or the end of the shader.
{
const uint32_t* pui32Result = DecodeDeclaration(psShader, pui32CurrentToken, psDecl);
if(pui32Result)
{
pui32CurrentToken = pui32Result;
psShader->aui32HSForkDeclCount[ui32ForkPhaseIndex]++;
psDecl++;
if(pui32CurrentToken >= (psShader->pui32FirstToken + ui32ShaderLength))
{
break;
}
}
else
{
break;
}
}
//Instructions
psInst = hlslcc_malloc(sizeof(Instruction) * ui32ShaderLength);
psShader->apsHSForkPhaseInstr[ui32ForkPhaseIndex] = psInst;
psShader->aui32HSForkInstrCount[ui32ForkPhaseIndex] = 0;
while (pui32CurrentToken < (psShader->pui32FirstToken + ui32ShaderLength))
{
const uint32_t* nextInstr = DeocdeInstruction(pui32CurrentToken, psInst, psShader);
#ifdef _DEBUG
if(nextInstr == pui32CurrentToken)
{
ASSERT(0);
break;
}
#endif
pui32CurrentToken = nextInstr;
if(psInst->eOpcode == OPCODE_HS_FORK_PHASE)
{
pui32CurrentToken = DecodeHullShaderForkPhase(pui32CurrentToken, psShader);
return pui32CurrentToken;
}
psShader->aui32HSForkInstrCount[ui32ForkPhaseIndex]++;
psInst++;
}
return pui32CurrentToken;
}
const uint32_t* DecodeHullShaderControlPointPhase(const uint32_t* pui32Tokens, Shader* psShader)
{
const uint32_t* pui32CurrentToken = pui32Tokens;
const uint32_t ui32ShaderLength = psShader->ui32ShaderLength;
Instruction* psInst;
//TODO one block of memory for instructions and declarions to reduce memory usage and number of allocs.
//hlscc_malloc max(sizeof(declaration), sizeof(instruction) * shader length; or sizeof(DeclInst) - unifying both structs.
//Declarations
Declaration* psDecl;
psDecl = hlslcc_malloc(sizeof(Declaration) * ui32ShaderLength);
psShader->psHSControlPointPhaseDecl = psDecl;
psShader->ui32HSControlPointDeclCount = 0;
while(1) //Keep going until we reach the first non-declaration token, or the end of the shader.
{
const uint32_t* pui32Result = DecodeDeclaration(psShader, pui32CurrentToken, psDecl);
if(pui32Result)
{
pui32CurrentToken = pui32Result;
psShader->ui32HSControlPointDeclCount++;
psDecl++;
if(pui32CurrentToken >= (psShader->pui32FirstToken + ui32ShaderLength))
{
break;
}
}
else
{
break;
}
}
//Instructions
psInst = hlslcc_malloc(sizeof(Instruction) * ui32ShaderLength);
psShader->psHSControlPointPhaseInstr = psInst;
psShader->ui32HSControlPointInstrCount = 0;
while (pui32CurrentToken < (psShader->pui32FirstToken + ui32ShaderLength))
{
const uint32_t* nextInstr = DeocdeInstruction(pui32CurrentToken, psInst, psShader);
#ifdef _DEBUG
if(nextInstr == pui32CurrentToken)
{
ASSERT(0);
break;
}
#endif
pui32CurrentToken = nextInstr;
if(psInst->eOpcode == OPCODE_HS_FORK_PHASE)
{
pui32CurrentToken = DecodeHullShaderForkPhase(pui32CurrentToken, psShader);
return pui32CurrentToken;
}
if(psInst->eOpcode == OPCODE_HS_JOIN_PHASE)
{
pui32CurrentToken = DecodeHullShaderJoinPhase(pui32CurrentToken, psShader);
return pui32CurrentToken;
}
psInst++;
psShader->ui32HSControlPointInstrCount++;
}
return pui32CurrentToken;
}
const uint32_t* DecodeHullShader(const uint32_t* pui32Tokens, Shader* psShader)
{
const uint32_t* pui32CurrentToken = pui32Tokens;
const uint32_t ui32ShaderLength = psShader->ui32ShaderLength;
Declaration* psDecl;
psDecl = hlslcc_malloc(sizeof(Declaration) * ui32ShaderLength);
psShader->psHSDecl = psDecl;
psShader->ui32HSDeclCount = 0;
while(1) //Keep going until we reach the first non-declaration token, or the end of the shader.
{
const uint32_t* pui32Result = DecodeDeclaration(psShader, pui32CurrentToken, psDecl);
if(pui32Result)
{
pui32CurrentToken = pui32Result;
if(psDecl->eOpcode == OPCODE_HS_CONTROL_POINT_PHASE)
{
pui32CurrentToken = DecodeHullShaderControlPointPhase(pui32CurrentToken, psShader);
return pui32CurrentToken;
}
if(psDecl->eOpcode == OPCODE_HS_FORK_PHASE)
{
pui32CurrentToken = DecodeHullShaderForkPhase(pui32CurrentToken, psShader);
return pui32CurrentToken;
}
if(psDecl->eOpcode == OPCODE_HS_JOIN_PHASE)
{
pui32CurrentToken = DecodeHullShaderJoinPhase(pui32CurrentToken, psShader);
return pui32CurrentToken;
}
psDecl++;
psShader->ui32HSDeclCount++;
if(pui32CurrentToken >= (psShader->pui32FirstToken + ui32ShaderLength))
{
break;
}
}
else
{
break;
}
}
return pui32CurrentToken;
}
void Decode(const uint32_t* pui32Tokens, Shader* psShader)
{
const uint32_t* pui32CurrentToken = pui32Tokens;
const uint32_t ui32ShaderLength = pui32Tokens[1];
Instruction* psInst;
Declaration* psDecl;
psShader->ui32MajorVersion = DecodeProgramMajorVersion(*pui32CurrentToken);
psShader->ui32MinorVersion = DecodeProgramMinorVersion(*pui32CurrentToken);
psShader->eShaderType = DecodeShaderType(*pui32CurrentToken);
pui32CurrentToken++;//Move to shader length
psShader->ui32ShaderLength = ui32ShaderLength;
pui32CurrentToken++;//Move to after shader length (usually a declaration)
psShader->pui32FirstToken = pui32Tokens;
if(psShader->eShaderType == HULL_SHADER)
{
pui32CurrentToken = DecodeHullShader(pui32CurrentToken, psShader);
return;
}
//Using ui32ShaderLength as the instruction count
//will allocate more than enough memory. Avoids having to
//traverse the entire shader just to get the real instruction count.
psInst = hlslcc_malloc(sizeof(Instruction) * ui32ShaderLength);
psShader->psInst = psInst;
psShader->ui32InstCount = 0;
psDecl = hlslcc_malloc(sizeof(Declaration) * ui32ShaderLength);
psShader->psDecl = psDecl;
psShader->ui32DeclCount = 0;
while(1) //Keep going until we reach the first non-declaration token, or the end of the shader.
{
const uint32_t* pui32Result = DecodeDeclaration(psShader, pui32CurrentToken, psDecl);
if(pui32Result)
{
pui32CurrentToken = pui32Result;
psShader->ui32DeclCount++;
psDecl++;
if(pui32CurrentToken >= (psShader->pui32FirstToken + ui32ShaderLength))
{
break;
}
}
else
{
break;
}
}
while (pui32CurrentToken < (psShader->pui32FirstToken + ui32ShaderLength))
{
const uint32_t* nextInstr = DeocdeInstruction(pui32CurrentToken, psInst, psShader);
#ifdef _DEBUG
if(nextInstr == pui32CurrentToken)
{
ASSERT(0);
break;
}
#endif
pui32CurrentToken = nextInstr;
psShader->ui32InstCount++;
psInst++;
}
}
Shader* DecodeDXBC(uint32_t* data)
{
Shader* psShader;
DXBCContainerHeader* header = (DXBCContainerHeader*)data;
uint32_t i;
uint32_t chunkCount;
uint32_t* chunkOffsets;
ReflectionChunks refChunks;
uint32_t* shaderChunk = 0;
if(header->fourcc != FOURCC_DXBC)
{
//Could be SM1/2/3. If the shader type token
//looks valid then we continue
uint32_t type = DecodeShaderTypeDX9(data[0]);
if(type != INVALID_SHADER)
{
return DecodeDX9BC(data);
}
return 0;
}
refChunks.pui32Inputs = NULL;
refChunks.pui32Interfaces = NULL;
refChunks.pui32Outputs = NULL;
refChunks.pui32Resources = NULL;
refChunks.pui32Inputs11 = NULL;
refChunks.pui32Outputs11 = NULL;
refChunks.pui32OutputsWithStreams = NULL;
chunkOffsets = (uint32_t*)(header + 1);
chunkCount = header->chunkCount;
for(i = 0; i < chunkCount; ++i)
{
uint32_t offset = chunkOffsets[i];
DXBCChunkHeader* chunk = (DXBCChunkHeader*)((char*)data + offset);
switch(chunk->fourcc)
{
case FOURCC_ISGN:
{
refChunks.pui32Inputs = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_ISG1:
{
refChunks.pui32Inputs11 = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_RDEF:
{
refChunks.pui32Resources = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_IFCE:
{
refChunks.pui32Interfaces = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_OSGN:
{
refChunks.pui32Outputs = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_OSG1:
{
refChunks.pui32Outputs11 = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_OSG5:
{
refChunks.pui32OutputsWithStreams = (uint32_t*)(chunk + 1);
break;
}
case FOURCC_SHDR:
case FOURCC_SHEX:
{
shaderChunk = (uint32_t*)(chunk + 1);
break;
}
default:
{
break;
}
}
}
if(shaderChunk)
{
uint32_t ui32MajorVersion;
uint32_t ui32MinorVersion;
psShader = hlslcc_calloc(1, sizeof(Shader));
ui32MajorVersion = DecodeProgramMajorVersion(*shaderChunk);
ui32MinorVersion = DecodeProgramMinorVersion(*shaderChunk);
LoadShaderInfo(ui32MajorVersion,
ui32MinorVersion,
&refChunks,
&psShader->sInfo);
Decode(shaderChunk, psShader);
return psShader;
}
return 0;
}
|