forked from bkaradzic/bgfx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shader_dxbc.h
620 lines (562 loc) · 9.99 KB
/
shader_dxbc.h
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
/*
* Copyright 2011-2017 Branimir Karadzic. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#ifndef BGFX_SHADER_DXBC_H
#define BGFX_SHADER_DXBC_H
#include <bx/readerwriter.h>
#define DXBC_CHUNK_HEADER BX_MAKEFOURCC('D', 'X', 'B', 'C')
namespace bgfx
{
struct DxbcOpcode
{
enum Enum
{
ADD,
AND,
BREAK,
BREAKC,
CALL,
CALLC,
CASE,
CONTINUE,
CONTINUEC,
CUT,
DEFAULT,
DERIV_RTX,
DERIV_RTY,
DISCARD,
DIV,
DP2,
DP3,
DP4,
ELSE,
EMIT,
EMITTHENCUT,
ENDIF,
ENDLOOP,
ENDSWITCH,
EQ,
EXP,
FRC,
FTOI,
FTOU,
GE,
IADD,
IF,
IEQ,
IGE,
ILT,
IMAD,
IMAX,
IMIN,
IMUL,
INE,
INEG,
ISHL,
ISHR,
ITOF,
LABEL,
LD,
LD_MS,
LOG,
LOOP,
LT,
MAD,
MIN,
MAX,
CUSTOMDATA,
MOV,
MOVC,
MUL,
NE,
NOP,
NOT,
OR,
RESINFO,
RET,
RETC,
ROUND_NE,
ROUND_NI,
ROUND_PI,
ROUND_Z,
RSQ,
SAMPLE,
SAMPLE_C,
SAMPLE_C_LZ,
SAMPLE_L,
SAMPLE_D,
SAMPLE_B,
SQRT,
SWITCH,
SINCOS,
UDIV,
ULT,
UGE,
UMUL,
UMAD,
UMAX,
UMIN,
USHR,
UTOF,
XOR,
DCL_RESOURCE,
DCL_CONSTANT_BUFFER,
DCL_SAMPLER,
DCL_INDEX_RANGE,
DCL_GS_OUTPUT_PRIMITIVE_TOPOLOGY,
DCL_GS_INPUT_PRIMITIVE,
DCL_MAX_OUTPUT_VERTEX_COUNT,
DCL_INPUT,
DCL_INPUT_SGV,
DCL_INPUT_SIV,
DCL_INPUT_PS,
DCL_INPUT_PS_SGV,
DCL_INPUT_PS_SIV,
DCL_OUTPUT,
DCL_OUTPUT_SGV,
DCL_OUTPUT_SIV,
DCL_TEMPS,
DCL_INDEXABLE_TEMP,
DCL_GLOBAL_FLAGS,
UnknownD3D10,
LOD,
GATHER4,
SAMPLE_POS,
SAMPLE_INFO,
UnknownD3D10_1,
HS_DECLS,
HS_CONTROL_POINT_PHASE,
HS_FORK_PHASE,
HS_JOIN_PHASE,
EMIT_STREAM,
CUT_STREAM,
EMITTHENCUT_STREAM,
INTERFACE_CALL,
BUFINFO,
DERIV_RTX_COARSE,
DERIV_RTX_FINE,
DERIV_RTY_COARSE,
DERIV_RTY_FINE,
GATHER4_C,
GATHER4_PO,
GATHER4_PO_C,
RCP,
F32TOF16,
F16TOF32,
UADDC,
USUBB,
COUNTBITS,
FIRSTBIT_HI,
FIRSTBIT_LO,
FIRSTBIT_SHI,
UBFE,
IBFE,
BFI,
BFREV,
SWAPC,
DCL_STREAM,
DCL_FUNCTION_BODY,
DCL_FUNCTION_TABLE,
DCL_INTERFACE,
DCL_INPUT_CONTROL_POINT_COUNT,
DCL_OUTPUT_CONTROL_POINT_COUNT,
DCL_TESS_DOMAIN,
DCL_TESS_PARTITIONING,
DCL_TESS_OUTPUT_PRIMITIVE,
DCL_HS_MAX_TESSFACTOR,
DCL_HS_FORK_PHASE_INSTANCE_COUNT,
DCL_HS_JOIN_PHASE_INSTANCE_COUNT,
DCL_THREAD_GROUP,
DCL_UNORDERED_ACCESS_VIEW_TYPED,
DCL_UNORDERED_ACCESS_VIEW_RAW,
DCL_UNORDERED_ACCESS_VIEW_STRUCTURED,
DCL_THREAD_GROUP_SHARED_MEMORY_RAW,
DCL_THREAD_GROUP_SHARED_MEMORY_STRUCTURED,
DCL_RESOURCE_RAW,
DCL_RESOURCE_STRUCTURED,
LD_UAV_TYPED,
STORE_UAV_TYPED,
LD_RAW,
STORE_RAW,
LD_STRUCTURED,
STORE_STRUCTURED,
ATOMIC_AND,
ATOMIC_OR,
ATOMIC_XOR,
ATOMIC_CMP_STORE,
ATOMIC_IADD,
ATOMIC_IMAX,
ATOMIC_IMIN,
ATOMIC_UMAX,
ATOMIC_UMIN,
IMM_ATOMIC_ALLOC,
IMM_ATOMIC_CONSUME,
IMM_ATOMIC_IADD,
IMM_ATOMIC_AND,
IMM_ATOMIC_OR,
IMM_ATOMIC_XOR,
IMM_ATOMIC_EXCH,
IMM_ATOMIC_CMP_EXCH,
IMM_ATOMIC_IMAX,
IMM_ATOMIC_IMIN,
IMM_ATOMIC_UMAX,
IMM_ATOMIC_UMIN,
SYNC,
DADD,
DMAX,
DMIN,
DMUL,
DEQ,
DGE,
DLT,
DNE,
DMOV,
DMOVC,
DTOF,
FTOD,
EVAL_SNAPPED,
EVAL_SAMPLE_INDEX,
EVAL_CENTROID,
DCL_GS_INSTANCE_COUNT,
ABORT,
DEBUG_BREAK,
UnknownD3D11,
DDIV,
DFMA,
DRCP,
MSAD,
DTOI,
DTOU,
ITOD,
UTOD,
Count
};
};
const char* getName(DxbcOpcode::Enum _opcode);
struct DxbcBuiltin
{
// D3D_NAME
// https://msdn.microsoft.com/en-us/library/windows/desktop/ff728724%28v=vs.85%29.aspx
// mesa/src/gallium/state_trackers/d3d1x/d3d1xshader/defs/svs.txt
enum Enum
{
Undefined,
Position,
ClipDistance,
CullDistance,
RenderTargetArrayIndex,
ViewportArrayIndex,
VertexId,
PrimitiveId,
InstanceId,
IsFrontFace,
SampleIndex,
FinalQuadUEq0EdgeTessFactor,
FinalQuadVEq0EdgeTessFactor,
FinalQuadUEq1EdgeTessFactor,
FinalQuadVEq1EdgeTessFactor,
FinalQuadUInsideTessFactor,
FinalQuadVInsideTessFactor,
FinalTriUEq0EdgeTessFactor,
FinalTriVEq0EdgeTessFactor,
FinalTriWEq0EdgeTessFactor,
FinalTriInsideTessFactor,
FinalLineDetailTessFactor,
FinalLineDensityTessFactor,
Target = 64,
Depth,
Coverage,
DepthGreaterEqual,
DepthLessEqual,
StencilRef,
InnerCoverage,
};
};
struct DxbcResourceDim
{
// D3D_SRV_DIMENSION
// https://msdn.microsoft.com/en-us/library/windows/desktop/ff728736%28v=vs.85%29.aspx
// mesa/src/gallium/state_trackers/d3d1x/d3d1xshader/defs/targets.txt
enum Enum
{
Unknown,
Buffer,
Texture1D,
Texture2D,
Texture2DMS,
Texture3D,
TextureCube,
Texture1DArray,
Texture2DArray,
Texture2DMSArray,
TextureCubearray,
RawBuffer,
StructuredBuffer,
Count
};
};
struct DxbcInterpolation
{
enum Enum
{
Unknown,
Constant,
Linear,
LinearCentroid,
LinearNoPerspective,
LinearNoPerspectiveCentroid,
LinearSample,
LinearNoPerspectiveSample,
Count
};
};
struct DxbcResourceReturnType
{
enum Enum
{
Unorm,
Snorm,
Sint,
Uint,
Float,
Mixed,
Double,
Continued,
Unused,
Count
};
};
struct DxbcComponentType
{
enum Enum
{
Unknown,
Uint32,
Int32,
Float,
Count
};
};
struct DxbcPrecision
{
enum Enum
{
Default,
Half,
Float2_8,
Reserved,
Int16,
Uint16,
Any16 = 0xf0,
Any10 = 0xf1,
};
};
struct DxbcOperandType
{
enum Enum
{
Temp,
Input,
Output,
TempArray,
Imm32,
Imm64,
Sampler,
Resource,
ConstantBuffer,
ImmConstantBuffer,
Label,
PrimitiveID,
OutputDepth,
Null,
Rasterizer,
CoverageMask,
Stream,
FunctionBody,
FunctionTable,
Interface,
FunctionInput,
FunctionOutput,
OutputControlPointId,
InputForkInstanceId,
InputJoinInstanceId,
InputControlPoint,
OutputControlPoint,
InputPatchConstant,
InputDomainPoint,
ThisPointer,
UnorderedAccessView,
ThreadGroupSharedMemory,
InputThreadId,
InputThreadGroupId,
InputThreadIdInGroup,
InputCoverageMask,
InputThreadIdInGroupFlattened,
InputGsInstanceId,
OutputDepthGreaterEqual,
OutputDepthLessEqual,
CycleCounter,
Count
};
};
struct DxbcOperandAddrMode
{
enum Enum
{
Imm32,
Imm64,
Reg,
RegImm32,
RegImm64,
Count
};
};
struct DxbcOperandMode
{
enum Enum
{
Mask,
Swizzle,
Scalar,
Count
};
};
struct DxbcSubOperand
{
DxbcSubOperand() { /* not pod */ }
DxbcOperandType::Enum type;
uint8_t mode;
uint8_t modeBits;
uint8_t num;
uint8_t numAddrModes;
uint8_t addrMode;
uint32_t regIndex;
};
struct DxbcOperand
{
DxbcOperand() { /* not pod */ }
DxbcOperandType::Enum type;
DxbcOperandMode::Enum mode;
uint8_t modeBits;
uint8_t num;
bool extended;
uint32_t extBits;
uint8_t numAddrModes;
uint8_t addrMode[3];
uint32_t regIndex[3];
DxbcSubOperand subOperand[3];
union
{
uint32_t imm32[4];
uint64_t imm64[4];
} un;
};
struct DxbcInstruction
{
DxbcInstruction() { /* not pod */ }
struct ExtendedType
{
enum Enum
{
Empty,
SampleControls,
ResourceDim,
ResourceReturnType,
Count
};
};
DxbcOpcode::Enum opcode;
uint32_t value[3];
uint32_t length;
uint8_t numOperands;
ExtendedType::Enum extended[3];
//
DxbcResourceDim::Enum srv;
uint8_t samples;
//
DxbcInterpolation::Enum interpolation;
//
bool shadow;
bool mono;
//
bool allowRefactoring;
bool fp64;
bool earlyDepth;
bool enableBuffers;
bool skipOptimization;
bool enableMinPrecision;
bool enableDoubleExtensions;
bool enableShaderExtensions;
//
bool threadsInGroup;
bool sharedMemory;
bool uavGroup;
bool uavGlobal;
//
DxbcResourceReturnType::Enum retType;
bool saturate;
uint8_t testNZ;
//
uint8_t sampleOffsets[3];
uint8_t resourceTarget;
uint8_t resourceStride;
DxbcResourceReturnType::Enum resourceReturnTypes[4];
DxbcOperand operand[6];
};
int32_t read(bx::ReaderI* _reader, DxbcInstruction& _instruction, bx::Error* _err);
int32_t write(bx::WriterI* _writer, const DxbcInstruction& _instruction, bx::Error* _err);
int32_t toString(char* _out, int32_t _size, const DxbcInstruction& _instruction);
struct DxbcSignature
{
DxbcSignature() { /* not pod */ }
struct Element
{
stl::string name;
uint32_t semanticIndex;
DxbcBuiltin::Enum valueType;
DxbcComponentType::Enum componentType;
uint32_t registerIndex;
uint8_t mask;
uint8_t readWriteMask;
uint8_t stream;
};
uint32_t key;
stl::vector<Element> elements;
};
int32_t read(bx::ReaderSeekerI* _reader, DxbcSignature& _signature, bx::Error* _err);
int32_t write(bx::WriterI* _writer, const DxbcSignature& _signature, bx::Error* _err);
struct DxbcShader
{
uint32_t version;
stl::vector<uint8_t> byteCode;
bool shex;
bool aon9;
};
int32_t read(bx::ReaderSeekerI* _reader, DxbcShader& _shader, bx::Error* _err);
int32_t write(bx::WriterI* _writer, const DxbcShader& _shader, bx::Error* _err);
typedef bool (*DxbcParseFn)(uint32_t _offset, const DxbcInstruction& _instruction, void* _userData);
void parse(const DxbcShader& _src, DxbcParseFn _fn, void* _userData, bx::Error* _err = NULL);
typedef void (*DxbcFilterFn)(DxbcInstruction& _instruction, void* _userData);
void filter(DxbcShader& _dst, const DxbcShader& _src, DxbcFilterFn _fn, void* _userData, bx::Error* _err = NULL);
struct DxbcContext
{
struct Header
{
uint32_t magic;
uint8_t hash[16];
uint32_t version;
uint32_t size;
uint32_t numChunks;
};
Header header;
DxbcSignature inputSignature;
DxbcSignature outputSignature;
DxbcShader shader;
};
int32_t read(bx::ReaderSeekerI* _reader, DxbcContext& _dxbc, bx::Error* _err);
int32_t write(bx::WriterSeekerI* _writer, const DxbcContext& _dxbc, bx::Error* _err);
/// Calculate DXBC hash from data.
void dxbcHash(const void* _data, uint32_t _size, void* _digest);
} // namespace bgfx
#endif // BGFX_SHADER_DXBC_H