forked from carbonengine/trinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffectCompilerDX11.cpp
More file actions
1923 lines (1776 loc) · 55.5 KB
/
Copy pathEffectCompilerDX11.cpp
File metadata and controls
1923 lines (1776 loc) · 55.5 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
// Copyright © 2023 CCP ehf.
#include "stdafx.h"
#if _WIN32
#include "EffectCompilerDX11.h"
#include "CompileMessageQueue.h"
#include "StringTable.h"
#include "EffectData.h"
#include "SymbolTable.h"
#include "ASTNode.h"
#include "ParserUtils.h"
#include "HLSLParser.h"
#include "ParserState.h"
#include "FXAnalyzer.h"
#include "TextureFunctionConversionDX11.h"
#include "YamlOutput.h"
#include "Macro.h"
#include "OutputHLSL.h"
#include "DxReflection.h"
#include <WorkQueue.h>
#define DXIL_FOURCC( ch0, ch1, ch2, ch3 ) ( \
(uint32_t)(uint8_t)( ch0 ) | (uint32_t)(uint8_t)( ch1 ) << 8 | \
(uint32_t)(uint8_t)( ch2 ) << 16 | (uint32_t)(uint8_t)( ch3 ) << 24 )
enum DxilFourCC
{
DFCC_Container = DXIL_FOURCC( 'D', 'X', 'B', 'C' ), // for back-compat with tools that look for DXBC containers
DFCC_ResourceDef = DXIL_FOURCC( 'R', 'D', 'E', 'F' ),
DFCC_InputSignature = DXIL_FOURCC( 'I', 'S', 'G', '1' ),
DFCC_OutputSignature = DXIL_FOURCC( 'O', 'S', 'G', '1' ),
DFCC_PatchConstantSignature = DXIL_FOURCC( 'P', 'S', 'G', '1' ),
DFCC_ShaderStatistics = DXIL_FOURCC( 'S', 'T', 'A', 'T' ),
DFCC_ShaderDebugInfoDXIL = DXIL_FOURCC( 'I', 'L', 'D', 'B' ),
DFCC_ShaderDebugName = DXIL_FOURCC( 'I', 'L', 'D', 'N' ),
DFCC_FeatureInfo = DXIL_FOURCC( 'S', 'F', 'I', '0' ),
DFCC_PrivateData = DXIL_FOURCC( 'P', 'R', 'I', 'V' ),
DFCC_RootSignature = DXIL_FOURCC( 'R', 'T', 'S', '0' ),
DFCC_DXIL = DXIL_FOURCC( 'D', 'X', 'I', 'L' ),
DFCC_PipelineStateValidation = DXIL_FOURCC( 'P', 'S', 'V', '0' ),
DFCC_RuntimeData = DXIL_FOURCC( 'R', 'D', 'A', 'T' ),
DFCC_ShaderHash = DXIL_FOURCC( 'H', 'A', 'S', 'H' ),
};
extern CompileMessageQueue g_messages;
extern StringTable g_stringTable;
extern bool g_printWarnings;
extern unsigned g_optimizationLevel;
extern bool g_avoidFlowControl;
extern bool g_generatePDB;
extern bool g_skipOptimization;
static bool FindParameterBySemantics( ASTNode* node, const char** semantics, std::vector<Symbol*>* path, bool outParameter = false )
{
for( unsigned j = 0; j < node->GetChildrenCount(); ++j )
{
Symbol* symbol = node->GetChild( j )->GetSymbol();
if( symbol == nullptr )
{
continue;
}
if( outParameter )
{
if( node->GetChild( j )->GetToken() == 0 || node->GetChild( j )->GetToken()->type == OP_IN )
{
continue;
}
}
for( unsigned k = 0; semantics[k]; ++k )
{
if( _stricmp( ToString( symbol->semantic ).c_str(), semantics[k] ) == 0 )
{
if( path )
{
path->clear();
path->push_back( symbol );
}
return true;
}
}
if( node->GetChild( j )->GetType().symbol && node->GetChild( j )->GetType().symbol->definition )
{
for( unsigned i = 0; i < node->GetChild( j )->GetType().symbol->definition->GetChildrenCount(); ++i )
{
if( FindParameterBySemantics( node->GetChild( j )->GetType().symbol->definition->GetChild( i ), semantics, path ) )
{
if( path )
{
path->insert( path->begin(), symbol );
}
return true;
}
}
}
}
return false;
}
bool FindOutputBySemantics( ASTNode* node, const char** semantics, std::vector<Symbol*>* path )
{
// check "out" function arguments
if( FindParameterBySemantics( node, semantics, path, true ) )
{
return true;
}
// check return value if it has semantics
if( node->GetSymbol() && node->GetSymbol()->semantic.start )
{
std::string sematic = ToString( node->GetSymbol()->semantic );
for( unsigned i = 0; semantics[i]; ++i )
{
if( _stricmp( sematic.c_str(), semantics[i] ) == 0 )
{
if( path )
{
path->push_back( nullptr );
}
return true;
}
}
}
// finally check return value as a structure
if( node->GetType().symbol &&
node->GetType().symbol->definition )
{
for( unsigned i = 0; i < node->GetType().symbol->definition->GetChildrenCount(); ++i )
{
if( FindParameterBySemantics( node->GetType().symbol->definition->GetChild( i ), semantics, path ) )
{
if( path )
{
path->insert( path->begin(), nullptr );
}
return true;
}
}
}
return false;
}
void PrintValuePath( std::ostream& os, const std::vector<Symbol*>& path, const char* functionSymbol )
{
bool first = true;
for( auto it = path.begin(); it != path.end(); ++it )
{
if( first )
{
first = false;
}
else
{
os << ".";
}
if( *it )
{
os << ( *it )->name;
}
else
{
os << functionSymbol;
}
}
}
static void PatchSemantics( InputStageType shaderStage, ASTNode* callNode )
{
Symbol* entryPointSymbol = callNode->GetSymbol();
if( !entryPointSymbol )
{
return;
}
ASTNode* functionHeader = entryPointSymbol->definition->GetChildOrNull( 0 );
if( functionHeader == nullptr )
{
return;
}
std::vector<Symbol*> targetPath;
switch( shaderStage )
{
case VERTEX_STAGE: {
const char* semantics[] = { "position", nullptr };
if( FindOutputBySemantics( functionHeader, semantics, &targetPath ) )
{
if( targetPath.back() )
{
targetPath.back()->semantic = MakeInlineString( "SV_Position" );
}
else
{
entryPointSymbol->semantic = MakeInlineString( "SV_Position" );
}
}
break;
}
case PIXEL_STAGE: {
{
const char* semantics[] = { "color", "color0", nullptr };
if( FindOutputBySemantics( functionHeader, semantics, &targetPath ) )
{
if( targetPath.back() )
{
targetPath.back()->semantic = MakeInlineString( "SV_Target0" );
}
else
{
entryPointSymbol->semantic = MakeInlineString( "SV_Target0" );
}
}
}
{
const char* semantics[] = { "color1", nullptr };
if( FindOutputBySemantics( functionHeader, semantics, &targetPath ) )
{
if( targetPath.back() )
{
targetPath.back()->semantic = MakeInlineString( "SV_Target1" );
}
else
{
entryPointSymbol->semantic = MakeInlineString( "SV_Target1" );
}
}
}
break;
}
}
}
static PatchAction PatchShader( InputStageType shaderStage, ASTNode* callNode, ParserState& state, CodeStream& os, std::string& entryPointName )
{
ZoneScoped;
// 1. wrap uniforms
// 2. fix VPOS
bool wrapUniforms = false;
bool fixVPOS = false;
bool fixVPOSType = false;
Symbol* entryPointSymbol = callNode->GetSymbol();
if( entryPointSymbol == nullptr || entryPointSymbol->definition == nullptr )
{
return PATCH_ERROR;
}
ASTNode* functionHeader = entryPointSymbol->definition->GetChildOrNull( 0 );
if( functionHeader == nullptr )
{
return PATCH_ERROR;
}
std::vector<Symbol*> targetPath;
std::vector<Symbol*> outPositionPath;
std::vector<Symbol*> vfacePath;
if( shaderStage == VERTEX_STAGE )
{
const char* position[] = { "position", "sv_position", nullptr };
FindOutputBySemantics( functionHeader, position, &outPositionPath );
}
// check for uniform parameters
if( callNode->GetChildrenCount() )
{
unsigned count = 0;
for( unsigned i = 0; i < functionHeader->GetChildrenCount(); ++i )
{
if( IsUniformInputArgument( functionHeader->GetChild( i ) ) )
{
++count;
}
}
if( count != callNode->GetChildrenCount() )
{
state.ShowMessage( callNode->GetLocation(), EC_NO_OVERRIDE, ToString( functionHeader->GetToken()->stringValue ).c_str(), "" );
return PATCH_ERROR;
}
wrapUniforms = true;
}
// find VPOS+POSITION parameters
std::vector<Symbol*> positionPath;
std::vector<Symbol*> vposPath;
const char* vpos[] = { "vpos", nullptr };
fixVPOSType = fixVPOS = FindParameterBySemantics( functionHeader, vpos, &vposPath );
if( fixVPOS )
{
const char* position[] = { "position", "sv_position", nullptr };
bool foundPosition = FindParameterBySemantics( functionHeader, position, &positionPath );
fixVPOS &= foundPosition;
}
if( fixVPOSType )
{
if( vposPath.back() )
{
if( vposPath.back()->type.symbol ||
( vposPath.back()->type.builtInType == OP_FLOAT && vposPath.back()->type.width == 4 && vposPath.back()->type.height == 1 ) )
{
fixVPOSType = false;
}
}
}
if( !wrapUniforms && !fixVPOS && !fixVPOSType && ( shaderStage != VERTEX_STAGE || outPositionPath.empty() ) )
{
entryPointName = ToString( entryPointSymbol->name );
return PATCH_USE;
}
if( entryPointSymbol->definition->GetChildOrNull( 2 ) )
{
os << HLSL{ entryPointSymbol->definition->GetChildOrNull( 2 ), nullptr };
}
// return type
os << functionHeader->GetType();
// name
InlineString name = state.AllocateName();
os << ' ' << name << "( ";
bool first = true;
for( size_t i = 0; i < functionHeader->GetChildrenCount(); ++i )
{
if( IsUniformInputArgument( functionHeader->GetChild( i ) ) )
{
continue;
}
Symbol* symbol = functionHeader->GetChild( i )->GetSymbol();
if( symbol == nullptr )
{
return PATCH_ERROR;
}
if( fixVPOS && symbol->semantic.start && _stricmp( ToString( symbol->semantic ).c_str(), "vpos" ) == 0 )
{
continue;
}
if( !first )
{
os << ", ";
}
else
{
first = false;
}
if( fixVPOSType && symbol->semantic.start && _stricmp( ToString( symbol->semantic ).c_str(), "vpos" ) == 0 )
{
os << "float4 " << functionHeader->GetChild( i )->GetSymbol()->name << ": VPOS";
}
else
{
os << HLSL{ functionHeader->GetChild( i ), nullptr };
}
}
os << " )";
// semantics
if( functionHeader->GetSymbol()->semantic.start )
{
os << " : " << functionHeader->GetSymbol()->semantic;
}
os << "\n{\n";
unsigned uniformIndex = 0;
std::map<size_t, InlineString> defaultUniformArguments;
for( size_t i = 0; i < functionHeader->GetChildrenCount(); ++i )
{
if( IsUniformInputArgument( functionHeader->GetChild( i ) ) )
{
if( callNode->GetChildrenCount() <= uniformIndex && functionHeader->GetChild( i )->GetChildOrNull( 1 ) != nullptr )
{
InlineString uniformName = state.AllocateName();
Type type = functionHeader->GetChild( i )->GetType();
type.storageClass = 0;
os << type << ' ' << uniformName << " = " << HLSL{ functionHeader->GetChild( i )->GetChildOrNull( 1 ), nullptr };
os << ";\n";
defaultUniformArguments[i] = uniformName;
}
uniformIndex++;
}
}
if( functionHeader->GetSymbol()->type.symbol || functionHeader->GetSymbol()->type.builtInType != OP_VOID )
{
os << functionHeader->GetType() << " __returnValue = ";
}
// call original function
uniformIndex = 0;
os << entryPointSymbol->name << "(";
for( size_t i = 0; i < functionHeader->GetChildrenCount(); ++i )
{
if( i > 0 )
{
os << ", ";
}
if( IsUniformInputArgument( functionHeader->GetChild( i ) ) )
{
if( callNode->GetChildrenCount() > uniformIndex )
{
os << HLSL{ callNode->GetChild( uniformIndex ), nullptr };
}
else if( functionHeader->GetChild( i )->GetChildOrNull( 1 ) != nullptr )
{
os << defaultUniformArguments[i];
}
uniformIndex++;
}
else
{
Symbol* symbol = functionHeader->GetChild( i )->GetSymbol();
if( symbol == nullptr )
{
return PATCH_ERROR;
}
if( fixVPOS && symbol->semantic.start && _stricmp( ToString( symbol->semantic ).c_str(), "vpos" ) == 0 )
{
// pass VPOS value from POSITION
PrintValuePath( os, positionPath, "" );
if( symbol->type.symbol == nullptr && symbol->type.builtInType == OP_FLOAT && symbol->type.height == 1 &&
positionPath.back()->type.symbol == nullptr && positionPath.back()->type.builtInType == OP_FLOAT &&
positionPath.back()->type.height == 1 && positionPath.back()->type.width != symbol->type.width )
{
const char* swizzle = "xyzw";
os << ".";
for( int k = 0; k < symbol->type.width; ++k )
{
os << swizzle[std::min( k, positionPath.back()->type.width - 1 )];
}
}
}
else if( fixVPOSType && symbol->semantic.start && _stricmp( ToString( symbol->semantic ).c_str(), "vpos" ) == 0 )
{
Type type = symbol->type;
os << type << "( " << symbol->name;
if( type.width != 4 )
{
const char* swizzle = "xyzw";
os << ".";
for( int k = 0; k < symbol->type.width; ++k )
{
os << swizzle[std::min( k, symbol->type.width - 1 )];
}
}
os << " )";
}
else
{
os << symbol->name;
}
}
}
os << ");\n";
if( functionHeader->GetSymbol()->type.symbol || functionHeader->GetSymbol()->type.builtInType != OP_VOID )
{
os << "return __returnValue;\n";
}
os << "}\n";
entryPointName = ToString( name );
return PATCH_USE;
}
bool EffectCompilerDX11::Create()
{
ZoneScoped;
return SUCCEEDED( ::DxcCreateInstance( CLSID_DxcUtils, IID_PPV_ARGS( &m_dxilUtils ) ) );
}
bool MatchShaderInputOutput( ID3D11ShaderReflection* output, ID3D11ShaderReflection* input )
{
D3D11_SHADER_DESC vsReflDesc;
if( FAILED( output->GetDesc( &vsReflDesc ) ) )
{
g_messages.AddMessage( "\\memory(0): error X0000: Could not get shader reflection description" );
return false;
}
D3D11_SHADER_DESC psReflDesc;
if( FAILED( input->GetDesc( &psReflDesc ) ) )
{
g_messages.AddMessage( "\\memory(0): error X0000: Could not get shader reflection description" );
return false;
}
for( unsigned k = 0; k < psReflDesc.InputParameters; ++k )
{
D3D11_SIGNATURE_PARAMETER_DESC psDesc;
if( FAILED( input->GetInputParameterDesc( k, &psDesc ) ) )
{
g_messages.AddMessage( "\\memory(0): error X0000: Could not get shader input parameter description" );
return false;
}
if( psDesc.SystemValueType != D3D10_NAME_UNDEFINED )
{
continue;
}
bool found = false;
for( unsigned n = 0; n < vsReflDesc.OutputParameters; ++n )
{
D3D11_SIGNATURE_PARAMETER_DESC vsDesc;
if( FAILED( output->GetOutputParameterDesc( n, &vsDesc ) ) )
{
g_messages.AddMessage( "\\memory(0): error X0000: Could not get shader output parameter description" );
return false;
}
if( vsDesc.Register == psDesc.Register && ( ( ~vsDesc.Mask & psDesc.Mask ) == 0 ) )
{
if( _stricmp( vsDesc.SemanticName, psDesc.SemanticName ) || vsDesc.SemanticIndex != psDesc.SemanticIndex )
{
g_messages.AddMessage( "\\memory(0): error X0000: Vertex/pixel shader signature mismatch" );
return false;
}
found = true;
break;
}
}
if( !found )
{
g_messages.AddMessage( "\\memory(0): error X0000: Vertex/pixel shader signature mismatch" );
return false;
}
}
return true;
}
std::string PrintPrettyCode( const char* code, const char* indent )
{
std::stringstream os;
while( *code )
{
const char* end = code;
while( *end && *end != '\n' )
{
++end;
}
if( strncmp( code, "#line", 5 ) && code != end )
{
os << indent;
os.write( code, end - code );
os << std::endl;
}
code = end + 1;
if( *end == 0 )
{
break;
}
}
return os.str();
}
void PrintShaderOutListing( YamlOutput& listing, ID3DBlob* effectData, ID3D11ShaderReflection* reflection )
{
ZoneScoped;
if( !listing.enabled() )
{
return;
}
CComPtr<ID3DBlob> disassembly;
if( SUCCEEDED( D3DDisassemble( effectData->GetBufferPointer(), effectData->GetBufferSize(), D3D_DISASM_ENABLE_DEFAULT_VALUE_PRINTS, nullptr, &disassembly ) ) )
{
listing.literal( "asm" ).literal( reinterpret_cast<const char*>( disassembly->GetBufferPointer() ) );
}
D3D11_SHADER_DESC desc;
if( reflection && SUCCEEDED( reflection->GetDesc( &desc ) ) )
{
listing.literal( "stats" ).dict().literal( "Resources" ).dict().literal( "constantBuffers" ).literal( desc.ConstantBuffers ).literal( "boundResources" ).literal( desc.BoundResources ).literal( "inputParameters" ).literal( desc.InputParameters ).literal( "outputParameters" ).literal( desc.OutputParameters ).literal( "tempRegisterCount" ).literal( desc.TempRegisterCount ).literal( "tempArrayCount" ).literal( desc.TempArrayCount ).end().literal( "Instructions" ).dict().literal( "instructionCount" ).literal( desc.InstructionCount ).literal( "defCount" ).literal( desc.DefCount ).literal( "textureNormalInstructions" ).literal( desc.TextureNormalInstructions ).literal( "textureLoadInstructions" ).literal( desc.TextureLoadInstructions ).literal( "textureCompInstructions" ).literal( desc.TextureCompInstructions ).literal( "textureBiasInstructions" ).literal( desc.TextureBiasInstructions ).literal( "textureGradientInstructions" ).literal( desc.TextureGradientInstructions ).literal( "floatInstructionCount" ).literal( desc.FloatInstructionCount ).literal( "intInstructionCount" ).literal( desc.IntInstructionCount ).literal( "uintInstructionCount" ).literal( desc.UintInstructionCount ).literal( "staticFlowControlCount" ).literal( desc.StaticFlowControlCount ).literal( "dynamicFlowControlCount" ).literal( desc.DynamicFlowControlCount ).literal( "macroInstructionCount" ).literal( desc.MacroInstructionCount ).literal( "arrayInstructionCount" ).literal( desc.ArrayInstructionCount ).literal( "cutInstructionCount" ).literal( desc.CutInstructionCount ).literal( "emitInstructionCount" ).literal( desc.EmitInstructionCount ).literal( "cBarrierInstructions" ).literal( desc.cBarrierInstructions ).literal( "cInterlockedInstructions" ).literal( desc.cInterlockedInstructions ).literal( "cTextureStoreInstructions" ).literal( desc.cTextureStoreInstructions ).end().literal( "Misc" ).dict().literal( "GSOutputTopology" ).literal( desc.GSOutputTopology ).literal( "inputPrimitive" ).literal( desc.InputPrimitive ).literal( "GSMaxOutputVertexCount" ).literal( desc.GSMaxOutputVertexCount ).literal( "patchConstantParameters" ).literal( desc.PatchConstantParameters ).literal( "cGSInstanceCount" ).literal( desc.cGSInstanceCount ).literal( "HSOutputPrimitive" ).literal( desc.HSOutputPrimitive ).literal( "HSPartitioning" ).literal( desc.HSPartitioning ).literal( "tessellatorDomain" ).literal( desc.TessellatorDomain ).end().end();
}
}
void PrintAnnotations( YamlOutput& listing, const std::map<StringReference, Annotation>& annotations )
{
if( !listing.enabled() )
{
return;
}
listing.literal( "annotations" ).list();
for( auto a = annotations.begin(); a != annotations.end(); ++a )
{
listing.dict()
.literal( "name" )
.literal( g_stringTable.GetString( a->first ) )
.literal( "annotationType" );
switch( a->second.type )
{
case ANNOTATION_TYPE_BOOL:
listing
.literal( "bool" )
.literal( "value" )
.literal( a->second.intValue != 0 );
break;
case ANNOTATION_TYPE_INT:
listing
.literal( "int" )
.literal( "value" )
.literal( a->second.intValue );
break;
case ANNOTATION_TYPE_FLOAT:
listing
.literal( "float" )
.literal( "value" )
.literal( a->second.floatValue );
break;
default:
listing
.literal( "string" )
.literal( "value" )
.literal( g_stringTable.GetString( a->second.stringValue ) );
break;
}
listing.end();
}
listing.end();
}
YamlOutput& YamlTextureType( YamlOutput& listing, TextureType type )
{
switch( type )
{
case TEX_TYPE_1D:
listing.literal( "1D texture" );
return listing;
case TEX_TYPE_2D:
listing.literal( "2D texture" );
return listing;
case TEX_TYPE_3D:
listing.literal( "3D texture" );
return listing;
case TEX_TYPE_CUBE:
listing.literal( "CUBE texture" );
return listing;
case TEX_TYPE_TYPELESS:
listing.literal( "typeless texture" );
return listing;
case TEX_TYPE_BUFFER:
listing.literal( "buffer" );
return listing;
case TEX_TYPE_STRUCTURED_BUFFER:
listing.literal( "structured buffer" );
return listing;
case TEX_TYPE_TBUFFER:
listing.literal( "TBuffer" );
return listing;
case TEX_TYPE_BYTEADDRESS_BUFFER:
listing.literal( "byte address buffer" );
return listing;
case TEX_TYPE_UAV_RWTYPED:
listing.literal( "UAV typed" );
return listing;
case TEX_TYPE_UAV_RWSTRUCTURED:
listing.literal( "UAV structured" );
return listing;
case TEX_TYPE_UAV_RWBYTEADDRESS:
listing.literal( "UAV RW byte address" );
return listing;
case TEX_TYPE_UAV_APPEND_STRUCTURED:
listing.literal( "UAV append structured" );
return listing;
case TEX_TYPE_UAV_CONSUME_STRUCTURED:
listing.literal( "UAV consume structured" );
return listing;
case TEX_TYPE_UAV_RWSTRUCTURED_WITH_COUNTER:
listing.literal( "UAV structured with counter" );
return listing;
default:
listing.literal( "other" );
return listing;
}
}
void PrintStageInfo( YamlOutput& listing, const StageData& stage, const EffectData& result )
{
if( !listing.enabled() )
{
return;
}
if( !stage.constants.empty() )
{
listing.literal( "constants" ).list();
for( auto it = stage.constants.begin(); it != stage.constants.end(); ++it )
{
listing.dict()
.literal( "name" )
.literal( g_stringTable.GetString( it->name ) )
.literal( "constantType" )
.literal( ToString( it->type ) )
.literal( "dimension" )
.literal( it->dimension )
.literal( "arrayElements" )
.literal( it->elements )
.literal( "autoregister" )
.literal( it->isAutoregister )
.literal( "sRGB" )
.literal( it->isSRGB );
auto annotations = result.annotations.find( it->name );
if( annotations != result.annotations.end() )
{
PrintAnnotations( listing, annotations->second.annotations );
}
listing.end();
}
listing.end();
}
if( !stage.samplers.empty() )
{
listing.literal( "samplers" ).list();
for( auto it = stage.samplers.begin(); it != stage.samplers.end(); ++it )
{
listing.dict()
.literal( "register" )
.literal( it->first )
.literal( "name" )
.literal( g_stringTable.GetString( it->second.name ) )
.literal( "filter" )
.literal( int( it->second.filter ) )
.literal( "comparison" )
.literal( int( it->second.comparison ) )
.literal( "minFilter" )
.literal( int( it->second.minFilter ) )
.literal( "magFilter" )
.literal( int( it->second.magFilter ) )
.literal( "mipFilter" )
.literal( int( it->second.mipFilter ) )
.literal( "addressU" )
.literal( int( it->second.addressU ) )
.literal( "addressV" )
.literal( int( it->second.addressV ) )
.literal( "addressW" )
.literal( int( it->second.addressW ) )
.literal( "mipLODBias" )
.literal( it->second.mipLODBias )
.literal( "maxAnisotropy" )
.literal( int( it->second.maxAnisotropy ) )
.literal( "comparisonFunc" )
.literal( int( it->second.comparisonFunc ) )
.literal( "borderColor" )
.list()
.literal( it->second.borderColor.x )
.literal( it->second.borderColor.y )
.literal( it->second.borderColor.z )
.literal( it->second.borderColor.w )
.end()
.literal( "minLOD" )
.literal( it->second.minLOD )
.literal( "maxLOD" )
.literal( it->second.maxLOD )
.literal( "srgbTexture" )
.literal( it->second.srgbTexture != 0 )
.literal( "isDynamic" )
.literal( it->second.isDynamic != 0 );
auto annotations = result.annotations.find( it->second.name );
if( annotations != result.annotations.end() )
{
PrintAnnotations( listing, annotations->second.annotations );
}
listing.end();
}
listing.end();
}
if( !stage.textures.empty() )
{
listing.literal( "textures" ).list();
for( auto it = stage.textures.begin(); it != stage.textures.end(); ++it )
{
listing.dict()
.literal( "register" )
.literal( it->first )
.literal( "name" )
.literal( g_stringTable.GetString( it->second.name ) )
.literal( "textureType" )
.literal( it->second.type )
.literal( "autoregister" )
.literal( it->second.isAutoregister )
.literal( "sRGB" )
.literal( it->second.isSRGB );
auto annotations = result.annotations.find( it->second.name );
if( annotations != result.annotations.end() )
{
PrintAnnotations( listing, annotations->second.annotations );
}
listing.end();
}
listing.end();
}
if( !stage.uavs.empty() )
{
listing.literal( "uavs" ).list();
for( auto it = stage.uavs.begin(); it != stage.uavs.end(); ++it )
{
listing.dict()
.literal( "register" )
.literal( it->first )
.literal( "name" )
.literal( g_stringTable.GetString( it->second.name ) )
.literal( "resourceType" )
.literal( it->second.type )
.literal( "autoregister" )
.literal( it->second.isAutoregister );
auto annotations = result.annotations.find( it->second.name );
if( annotations != result.annotations.end() )
{
PrintAnnotations( listing, annotations->second.annotations );
}
listing.end();
}
listing.end();
}
if( !stage.registerInputs.empty() )
{
listing.literal( "registers" ).list();
for( auto it = stage.registerInputs.begin(); it != stage.registerInputs.end(); ++it )
{
listing.dict()
.literal( "registerType" )
.literal( it->registerType )
.literal( "register" )
.literal( it->registerIndex )
.end();
}
listing.end();
}
}
void PrintStageInfo( YamlOutput& listing, const StageInput& stage, const EffectData& result )
{
PrintStageInfo( listing, static_cast<const StageData&>( stage ), result );
if( !stage.pipelineInputs.empty() )
{
listing.literal( "inputs" ).list();
for( auto it = stage.pipelineInputs.begin(); it != stage.pipelineInputs.end(); ++it )
{
listing.dict()
.literal( "register" )
.literal( it->registerIndex )
.literal( "name" )
.literal( it->name )
.literal( "index" )
.literal( it->index )
.literal( "usedMask" )
.literal( it->usedMask )
.end();
}
listing.end(); // inputs
}
if( !stage.staticSamplers.empty() )
{
listing.literal( "staticSamplers" ).list();
for( auto& it : stage.staticSamplers )
{
listing.dict()
.literal( "register" )
.literal( it.registerIndex )
.literal( "space" )
.literal( it.registerSpace )
.literal( "comparison" )
.literal( int( it.comparison ) )
.literal( "minFilter" )
.literal( int( it.minFilter ) )
.literal( "magFilter" )
.literal( int( it.magFilter ) )
.literal( "mipFilter" )
.literal( int( it.mipFilter ) )
.literal( "addressU" )
.literal( int( it.addressU ) )
.literal( "addressV" )
.literal( int( it.addressV ) )
.literal( "addressW" )
.literal( int( it.addressW ) )
.literal( "mipLODBias" )
.literal( it.mipLODBias )
.literal( "maxAnisotropy" )
.literal( int( it.maxAnisotropy ) )
.literal( "comparisonFunc" )
.literal( int( it.comparisonFunc ) )
.literal( "borderColor" )
.literal( it.borderColor )
.literal( "minLOD" )
.literal( it.minLOD )
.literal( "maxLOD" )
.literal( it.maxLOD )
.end();
}
listing.end(); //staticSamplers
}
}
std::string SanitizeCode( const std::string& src )
{
ZoneScoped;
std::regex line( "#line[^\\n]*\\n?\\n" );
return std::regex_replace( src, line, std::string( "" ) );
}
bool ParseShaderName( const InlineString& name, InputStageType& type )
{
if( _stricmp( ToString( name ).c_str(), "vertexshader" ) == 0 )
{
type = VERTEX_STAGE;
}
else if( _stricmp( ToString( name ).c_str(), "pixelshader" ) == 0 )
{
type = PIXEL_STAGE;
}
else if( _stricmp( ToString( name ).c_str(), "computeshader" ) == 0 )
{
type = COMPUTE_STAGE;
}
else if( _stricmp( ToString( name ).c_str(), "geometryshader" ) == 0 )
{
type = GEOMETRY_STAGE;
}
else if( _stricmp( ToString( name ).c_str(), "hullshader" ) == 0 )
{
type = HULL_STAGE;
}
else if( _stricmp( ToString( name ).c_str(), "domainshader" ) == 0 )
{
type = DOMAIN_STAGE;
}
else
{
return false;
}
return true;
}
bool EffectCompilerDX11::CompileEffect( const char* source, size_t sourceLength, const std::vector<Macro>& defines, EffectData& result, IWorkQueue* workQueue )
{
return CompileEffect( source, sourceLength, defines, result, { nullptr, false }, workQueue );
}
DWORD GetOptimizationLevel()
{
switch( g_optimizationLevel )
{
case 0:
return D3DCOMPILE_OPTIMIZATION_LEVEL0;
case 1:
return D3DCOMPILE_OPTIMIZATION_LEVEL1;
case 2:
return D3DCOMPILE_OPTIMIZATION_LEVEL2;
default:
return D3DCOMPILE_OPTIMIZATION_LEVEL3;
}
}
RegisterInputDescription GetRegisterInputDescription( const Type& type, const RegisterSpecifier& reg )
{
RegisterInputDescription desc;
switch( type.builtInType )
{
case OP_BUFFER:
case OP_RAYTRACING_ACCELERATION_STRUCTURE:
desc.registerType = RT_SRV_BUFFER;
break;
case OP_STRUCTUREDBUFFER:
desc.registerType = RT_SRV_STRUCTURED_BUFFER;
break;
case OP_TEXTURE1D:
desc.registerType = RT_SRV_TEXTURE1D;
break;
case OP_TEXTURE1DARRAY:
desc.registerType = RT_SRV_TEXTURE1DARRAY;
break;
case OP_TEXTURE:
case OP_TEXTURE2D:
desc.registerType = RT_SRV_TEXTURE2D;
break;
case OP_TEXTURE2DARRAY:
desc.registerType = RT_SRV_TEXTURE2DARRAY;
break;
case OP_TEXTURE2DMS:
desc.registerType = RT_SRV_TEXTURE2DMS;
break;
case OP_TEXTURE2DMSARRAY:
desc.registerType = RT_SRV_TEXTURE2DMSARRAY;
break;
case OP_TEXTURE3D:
desc.registerType = RT_SRV_TEXTURE3D;
break;
case OP_TEXTURECUBE:
desc.registerType = RT_SRV_TEXTURECUBE;
break;
case OP_TEXTURECUBEARRAY:
desc.registerType = RT_SRV_TEXTURECUBEARRAY;
break;
case OP_RWBUFFER:
desc.registerType = RT_UAV_BUFFER;