forked from etnlgd/HLSLDecompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode.cpp
More file actions
1571 lines (1388 loc) · 50 KB
/
decode.cpp
File metadata and controls
1571 lines (1388 loc) · 50 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
#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 "log.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 ))
enum {FOURCC_DXBC = FOURCC('D', 'X', 'B', 'C')}; //DirectX byte code
enum {FOURCC_SHDR = FOURCC('S', 'H', 'D', 'R')}; //Shader model 4 code
enum {FOURCC_SHEX = FOURCC('S', 'H', 'E', 'X')}; //Shader model 5 code
enum {FOURCC_RDEF = FOURCC('R', 'D', 'E', 'F')}; //Resource definition (e.g. constant buffers)
enum {FOURCC_ISGN = FOURCC('I', 'S', 'G', 'N')}; //Input signature
enum {FOURCC_IFCE = FOURCC('I', 'F', 'C', 'E')}; //Interface (for dynamic linking)
enum {FOURCC_OSGN = FOURCC('O', 'S', 'G', 'N')}; //Output signature
enum {FOURCC_ISG1 = FOURCC('I', 'S', 'G', '1')}; //Input signature with Stream and MinPrecision
enum {FOURCC_OSG1 = FOURCC('O', 'S', 'G', '1')}; //Output signature with Stream and MinPrecision
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
class DecompileError: public std::exception {} decompileError;
void DecodeNameToken(const uint32_t* pui32NameToken, Operand* psOperand)
{
psOperand->eSpecialName = DecodeOperandSpecialName(*pui32NameToken);
switch(psOperand->eSpecialName)
{
case NAME_UNDEFINED:
{
psOperand->specialName = "undefined";
break;
}
case NAME_POSITION:
{
psOperand->specialName = "position";
break;
}
case NAME_CLIP_DISTANCE:
{
psOperand->specialName = "clipDistance";
break;
}
case NAME_CULL_DISTANCE:
{
psOperand->specialName = "cullDistance";
break;
}
case NAME_RENDER_TARGET_ARRAY_INDEX:
{
psOperand->specialName = "renderTargetArrayIndex";
break;
}
case NAME_VIEWPORT_ARRAY_INDEX:
{
psOperand->specialName = "viewportArrayIndex";
break;
}
case NAME_VERTEX_ID:
{
psOperand->specialName = "vertexID";
break;
}
case NAME_PRIMITIVE_ID:
{
psOperand->specialName = "primitiveID";
break;
}
case NAME_INSTANCE_ID:
{
psOperand->specialName = "instanceID";
break;
}
case NAME_IS_FRONT_FACE:
{
psOperand->specialName = "isFrontFace";
break;
}
case NAME_SAMPLE_INDEX:
{
psOperand->specialName = "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:
{
psOperand->specialName = "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, std::vector<Declaration> &psDeclList, const Operand* psTextureOperand)
{
ResourceBinding* psBinding = 0;
int found;
ASSERT(psTextureOperand->eType == OPERAND_TYPE_RESOURCE);
found = GetResourceFromBindingPoint(RTYPE_TEXTURE, psTextureOperand->ui32RegisterNumber, psShaderInfo, &psBinding);
if(found)
{
for (std::vector<Declaration>::iterator psDecl = psDeclList.begin(); psDecl != psDeclList.end(); ++psDecl)
{
if(psDecl->eOpcode == OPCODE_DCL_RESOURCE)
{
if(psDecl->asOperands[0].eType == OPERAND_TYPE_RESOURCE &&
psDecl->asOperands[0].ui32RegisterNumber == psTextureOperand->ui32RegisterNumber)
{
psDecl->ui32IsShadowTex = 1;
break;
}
}
}
}
}
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 = (OPERAND_MIN_PRECISION) 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
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] = new 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] = new Operand();
DecodeOperand(pui32Tokens+ui32NumTokens, psOperand->psSubOperand[i]);
ui32NumTokens++;
break;
}
default:
{
ASSERT(0);
break;
}
}
ui32NumTokens++;
}
psOperand->specialName.clear();
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;
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->value.eInterpolation = DecodeInterpolationMode(*pui32Token);
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:
{
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 = (ICBVec4 const *) (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 = 1;
psDecl->value.eResourceDimension = DecodeResourceDimension(*pui32Token);
psDecl->sUAV.ui32GloballyCoherentAccess = DecodeAccessCoherencyFlags(*pui32Token);
psDecl->sUAV.bCounter = 0;
psDecl->sUAV.ui32BufferSize = 0;
DecodeOperand(pui32Token+ui32OperandOffset, &psDecl->asOperands[0]);
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]);
if(GetResourceFromBindingPoint(RTYPE_UAV_RWSTRUCTURED, psDecl->asOperands[0].ui32RegisterNumber, psShader->sInfo, &psBinding))
{
GetUAVBufferFromBindingPoint(psBinding->ui32BindPoint, psShader->sInfo, &psBuffer);
psDecl->sUAV.ui32BufferSize = psBuffer->ui32TotalSizeInBytes;
}
else if(GetResourceFromBindingPoint(RTYPE_UAV_RWSTRUCTURED_WITH_COUNTER, psDecl->asOperands[0].ui32RegisterNumber, psShader->sInfo, &psBinding) ||
GetResourceFromBindingPoint(RTYPE_UAV_APPEND_STRUCTURED, psDecl->asOperands[0].ui32RegisterNumber, psShader->sInfo, &psBinding) ||
GetResourceFromBindingPoint(RTYPE_UAV_CONSUME_STRUCTURED, psDecl->asOperands[0].ui32RegisterNumber, psShader->sInfo, &psBinding))
{
GetUAVBufferFromBindingPoint(psBinding->ui32BindPoint, psShader->sInfo, &psBuffer);
psDecl->sUAV.ui32BufferSize = psBuffer->ui32TotalSizeInBytes;
psDecl->sUAV.bCounter = 1;
}
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;
}
// Two manually added from newer version of HLSLCrossCompiler.
case OPCODE_DCL_STREAM:
{
psDecl->ui32NumOperands = 1;
DecodeOperand(pui32Token + ui32OperandOffset, &psDecl->asOperands[0]);
break;
}
// Not seen yet, needs mod to structs.h-- REALLY need to update to latest JJ.
//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;
if(bExtended)
{
do {
const uint32_t ui32ExtOpcodeToken = pui32Token[ui32OperandOffset];
const EXTENDED_OPCODE_TYPE eExtType = DecodeExtendedOpcodeType(ui32ExtOpcodeToken);
if(eExtType == EXTENDED_OPCODE_SAMPLE_CONTROLS)
{
psInst->bAddressOffset = 1;
psInst->iUAddrOffset = DecodeImmediateAddressOffset(
IMMEDIATE_ADDRESS_OFFSET_U, ui32ExtOpcodeToken);
psInst->iVAddrOffset = DecodeImmediateAddressOffset(
IMMEDIATE_ADDRESS_OFFSET_V, ui32ExtOpcodeToken);
psInst->iWAddrOffset = DecodeImmediateAddressOffset(
IMMEDIATE_ADDRESS_OFFSET_W, ui32ExtOpcodeToken);
}
ui32OperandOffset++;
}
while(DecodeIsOpcodeExtended(pui32Token[ui32OperandOffset-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;
break;
}
case OPCODE_DCL_HS_FORK_PHASE_INSTANCE_COUNT:
{
psInst->ui32NumOperands = 0;
break;
}
case OPCODE_SYNC:
{
psInst->ui32NumOperands = 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;
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->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_NOT:
case OPCODE_DERIV_RTX_COARSE:
case OPCODE_DERIV_RTY_COARSE:
case OPCODE_DERIV_RTX_FINE:
case OPCODE_DERIV_RTY_FINE:
case OPCODE_DERIV_RTX:
case OPCODE_DERIV_RTY:
{
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:
case OPCODE_MIN:
case OPCODE_UMIN: // missing opcode
case OPCODE_UMAX: // missing opcode
case OPCODE_IMAX:
case OPCODE_IMIN:
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_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_IMUL:
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:
case OPCODE_XOR:
case OPCODE_SAMPLE_POS: // bo3b: added for WatchDogs
{
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:
{
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_GATHER4_PO:
case OPCODE_SAMPLE_L:
case OPCODE_BFI:
case OPCODE_SWAPC: