-
Notifications
You must be signed in to change notification settings - Fork 862
/
Copy pathStandAlone.cpp
1767 lines (1599 loc) · 71.8 KB
/
StandAlone.cpp
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 (C) 2002-2005 3Dlabs Inc. Ltd.
// Copyright (C) 2013-2016 LunarG, Inc.
// Copyright (C) 2016-2020 Google, Inc.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
//
// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// this only applies to the standalone wrapper, not the front end in general
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "ResourceLimits.h"
#include "Worklist.h"
#include "DirStackFileIncluder.h"
#include "./../glslang/Include/ShHandle.h"
#include "./../glslang/Public/ShaderLang.h"
#include "../SPIRV/GlslangToSpv.h"
#include "../SPIRV/GLSL.std.450.h"
#include "../SPIRV/doc.h"
#include "../SPIRV/disassemble.h"
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <array>
#include <map>
#include <memory>
#include <thread>
#include "../glslang/OSDependent/osinclude.h"
// Build-time generated includes
#include "glslang/build_info.h"
extern "C" {
GLSLANG_EXPORT void ShOutputHtml();
}
// Command-line options
enum TOptions {
EOptionNone = 0,
EOptionIntermediate = (1 << 0),
EOptionSuppressInfolog = (1 << 1),
EOptionMemoryLeakMode = (1 << 2),
EOptionRelaxedErrors = (1 << 3),
EOptionGiveWarnings = (1 << 4),
EOptionLinkProgram = (1 << 5),
EOptionMultiThreaded = (1 << 6),
EOptionDumpConfig = (1 << 7),
EOptionDumpReflection = (1 << 8),
EOptionSuppressWarnings = (1 << 9),
EOptionDumpVersions = (1 << 10),
EOptionSpv = (1 << 11),
EOptionHumanReadableSpv = (1 << 12),
EOptionVulkanRules = (1 << 13),
EOptionDefaultDesktop = (1 << 14),
EOptionOutputPreprocessed = (1 << 15),
EOptionOutputHexadecimal = (1 << 16),
EOptionReadHlsl = (1 << 17),
EOptionCascadingErrors = (1 << 18),
EOptionAutoMapBindings = (1 << 19),
EOptionFlattenUniformArrays = (1 << 20),
EOptionNoStorageFormat = (1 << 21),
EOptionKeepUncalled = (1 << 22),
EOptionHlslOffsets = (1 << 23),
EOptionHlslIoMapping = (1 << 24),
EOptionAutoMapLocations = (1 << 25),
EOptionDebug = (1 << 26),
EOptionStdin = (1 << 27),
EOptionOptimizeDisable = (1 << 28),
EOptionOptimizeSize = (1 << 29),
EOptionInvertY = (1 << 30),
EOptionDumpBareVersion = (1 << 31),
};
bool targetHlslFunctionality1 = false;
bool SpvToolsDisassembler = false;
bool SpvToolsValidate = false;
bool NaNClamp = false;
bool stripDebugInfo = false;
bool beQuiet = false;
//
// Return codes from main/exit().
//
enum TFailCode {
ESuccess = 0,
EFailUsage,
EFailCompile,
EFailLink,
EFailCompilerCreate,
EFailThreadCreate,
EFailLinkerCreate
};
//
// Forward declarations.
//
EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
void CompileFile(const char* fileName, ShHandle);
void usage();
char* ReadFileData(const char* fileName);
void FreeFileData(char* data);
void InfoLogMsg(const char* msg, const char* name, const int num);
// Globally track if any compile or link failure.
bool CompileFailed = false;
bool LinkFailed = false;
// array of unique places to leave the shader names and infologs for the asynchronous compiles
std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
TBuiltInResource Resources;
std::string ConfigFile;
//
// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
//
void ProcessConfigFile()
{
if (ConfigFile.size() == 0)
Resources = glslang::DefaultTBuiltInResource;
#ifndef GLSLANG_WEB
else {
char* configString = ReadFileData(ConfigFile.c_str());
glslang::DecodeResourceLimits(&Resources, configString);
FreeFileData(configString);
}
#endif
}
int ReflectOptions = EShReflectionDefault;
int Options = 0;
const char* ExecutableName = nullptr;
const char* binaryFileName = nullptr;
const char* entryPointName = nullptr;
const char* sourceEntryPointName = nullptr;
const char* shaderStageName = nullptr;
const char* variableName = nullptr;
bool HlslEnable16BitTypes = false;
bool HlslDX9compatible = false;
bool DumpBuiltinSymbols = false;
std::vector<std::string> IncludeDirectoryList;
// Source environment
// (source 'Client' is currently the same as target 'Client')
int ClientInputSemanticsVersion = 100;
// Target environment
glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
// Per descriptor-set binding base data
typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
std::vector<std::pair<std::string, int>> uniformLocationOverrides;
int uniformBase = 0;
std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
// Add things like "#define ..." to a preamble to use in the beginning of the shader.
class TPreamble {
public:
TPreamble() { }
bool isSet() const { return text.size() > 0; }
const char* get() const { return text.c_str(); }
// #define...
void addDef(std::string def)
{
text.append("#define ");
fixLine(def);
Processes.push_back("define-macro ");
Processes.back().append(def);
// The first "=" needs to turn into a space
const size_t equal = def.find_first_of("=");
if (equal != def.npos)
def[equal] = ' ';
text.append(def);
text.append("\n");
}
// #undef...
void addUndef(std::string undef)
{
text.append("#undef ");
fixLine(undef);
Processes.push_back("undef-macro ");
Processes.back().append(undef);
text.append(undef);
text.append("\n");
}
protected:
void fixLine(std::string& line)
{
// Can't go past a newline in the line
const size_t end = line.find_first_of("\n");
if (end != line.npos)
line = line.substr(0, end);
}
std::string text; // contents of preamble
};
// Track the user's #define and #undef from the command line.
TPreamble UserPreamble;
//
// Create the default name for saving a binary if -o is not provided.
//
const char* GetBinaryName(EShLanguage stage)
{
const char* name;
if (binaryFileName == nullptr) {
switch (stage) {
case EShLangVertex: name = "vert.spv"; break;
case EShLangTessControl: name = "tesc.spv"; break;
case EShLangTessEvaluation: name = "tese.spv"; break;
case EShLangGeometry: name = "geom.spv"; break;
case EShLangFragment: name = "frag.spv"; break;
case EShLangCompute: name = "comp.spv"; break;
case EShLangRayGen: name = "rgen.spv"; break;
case EShLangIntersect: name = "rint.spv"; break;
case EShLangAnyHit: name = "rahit.spv"; break;
case EShLangClosestHit: name = "rchit.spv"; break;
case EShLangMiss: name = "rmiss.spv"; break;
case EShLangCallable: name = "rcall.spv"; break;
case EShLangMeshNV: name = "mesh.spv"; break;
case EShLangTaskNV: name = "task.spv"; break;
default: name = "unknown"; break;
}
} else
name = binaryFileName;
return name;
}
//
// *.conf => this is a config file that can set limits/resources
//
bool SetConfigFile(const std::string& name)
{
if (name.size() < 5)
return false;
if (name.compare(name.size() - 5, 5, ".conf") == 0) {
ConfigFile = name;
return true;
}
return false;
}
//
// Give error and exit with failure code.
//
void Error(const char* message, const char* detail = nullptr)
{
fprintf(stderr, "%s: Error: ", ExecutableName);
if (detail != nullptr)
fprintf(stderr, "%s: ", detail);
fprintf(stderr, "%s (use -h for usage)\n", message);
exit(EFailUsage);
}
//
// Process an optional binding base of one the forms:
// --argname [stage] base // base for stage (if given) or all stages (if not)
// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
// Where stage is one of the forms accepted by FindLanguage, and base is an integer
//
void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
{
if (argc < 2)
usage();
EShLanguage lang = EShLangCount;
int singleBase = 0;
TPerSetBaseBinding perSetBase;
int arg = 1;
// Parse stage, if given
if (!isdigit(argv[arg][0])) {
if (argc < 3) // this form needs one more argument
usage();
lang = FindLanguage(argv[arg++], false);
}
if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
// Parse a per-set binding base
while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
const int baseNum = atoi(argv[arg++]);
const int setNum = atoi(argv[arg++]);
perSetBase[setNum] = baseNum;
}
} else {
// Parse single binding base
singleBase = atoi(argv[arg++]);
}
argc -= (arg-1);
argv += (arg-1);
// Set one or all languages
const int langMin = (lang < EShLangCount) ? lang+0 : 0;
const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
for (int lang = langMin; lang < langMax; ++lang) {
if (!perSetBase.empty())
baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
else
baseBinding[res][lang] = singleBase;
}
}
void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
{
if (argc < 2)
usage();
if (!isdigit(argv[1][0])) {
if (argc < 3) // this form needs one more argument
usage();
// Parse form: --argname stage [regname set base...], or:
// --argname stage set
const EShLanguage lang = FindLanguage(argv[1], false);
argc--;
argv++;
while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
base[lang].push_back(argv[1]);
argc--;
argv++;
}
// Must have one arg, or a multiple of three (for [regname set binding] triples)
if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
usage();
} else {
// Parse form: --argname set
for (int lang=0; lang<EShLangCount; ++lang)
base[lang].push_back(argv[1]);
argc--;
argv++;
}
}
//
// Do all command-line argument parsing. This includes building up the work-items
// to be processed later, and saving all the command-line options.
//
// Does not return (it exits) if command-line is fatally flawed.
//
void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
{
for (int res = 0; res < glslang::EResCount; ++res)
baseBinding[res].fill(0);
ExecutableName = argv[0];
workItems.reserve(argc);
const auto bumpArg = [&]() {
if (argc > 0) {
argc--;
argv++;
}
};
// read a string directly attached to a single-letter option
const auto getStringOperand = [&](const char* desc) {
if (argv[0][2] == 0) {
printf("%s must immediately follow option (no spaces)\n", desc);
exit(EFailUsage);
}
return argv[0] + 2;
};
// read a number attached to a single-letter option
const auto getAttachedNumber = [&](const char* desc) {
int num = atoi(argv[0] + 2);
if (num == 0) {
printf("%s: expected attached non-0 number\n", desc);
exit(EFailUsage);
}
return num;
};
// minimum needed (without overriding something else) to target Vulkan SPIR-V
const auto setVulkanSpv = []() {
if (Client == glslang::EShClientNone)
ClientVersion = glslang::EShTargetVulkan_1_0;
Client = glslang::EShClientVulkan;
Options |= EOptionSpv;
Options |= EOptionVulkanRules;
Options |= EOptionLinkProgram;
};
// minimum needed (without overriding something else) to target OpenGL SPIR-V
const auto setOpenGlSpv = []() {
if (Client == glslang::EShClientNone)
ClientVersion = glslang::EShTargetOpenGL_450;
Client = glslang::EShClientOpenGL;
Options |= EOptionSpv;
Options |= EOptionLinkProgram;
// undo a -H default to Vulkan
Options &= ~EOptionVulkanRules;
};
const auto getUniformOverride = [getStringOperand]() {
const char *arg = getStringOperand("-u<name>:<location>");
const char *split = strchr(arg, ':');
if (split == NULL) {
printf("%s: missing location\n", arg);
exit(EFailUsage);
}
errno = 0;
int location = ::strtol(split + 1, NULL, 10);
if (errno) {
printf("%s: invalid location\n", arg);
exit(EFailUsage);
}
return std::make_pair(std::string(arg, split - arg), location);
};
for (bumpArg(); argc >= 1; bumpArg()) {
if (argv[0][0] == '-') {
switch (argv[0][1]) {
case '-':
{
std::string lowerword(argv[0]+2);
std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
// handle --word style options
if (lowerword == "auto-map-bindings" || // synonyms
lowerword == "auto-map-binding" ||
lowerword == "amb") {
Options |= EOptionAutoMapBindings;
} else if (lowerword == "auto-map-locations" || // synonyms
lowerword == "aml") {
Options |= EOptionAutoMapLocations;
} else if (lowerword == "uniform-base") {
if (argc <= 1)
Error("no <base> provided", lowerword.c_str());
uniformBase = ::strtol(argv[1], NULL, 10);
bumpArg();
break;
} else if (lowerword == "client") {
if (argc > 1) {
if (strcmp(argv[1], "vulkan100") == 0)
setVulkanSpv();
else if (strcmp(argv[1], "opengl100") == 0)
setOpenGlSpv();
else
Error("expects vulkan100 or opengl100", lowerword.c_str());
} else
Error("expects vulkan100 or opengl100", lowerword.c_str());
bumpArg();
} else if (lowerword == "define-macro" ||
lowerword == "d") {
if (argc > 1)
UserPreamble.addDef(argv[1]);
else
Error("expects <name[=def]>", argv[0]);
bumpArg();
} else if (lowerword == "dump-builtin-symbols") {
DumpBuiltinSymbols = true;
} else if (lowerword == "entry-point") {
entryPointName = argv[1];
if (argc <= 1)
Error("no <name> provided", lowerword.c_str());
bumpArg();
} else if (lowerword == "flatten-uniform-arrays" || // synonyms
lowerword == "flatten-uniform-array" ||
lowerword == "fua") {
Options |= EOptionFlattenUniformArrays;
} else if (lowerword == "hlsl-offsets") {
Options |= EOptionHlslOffsets;
} else if (lowerword == "hlsl-iomap" ||
lowerword == "hlsl-iomapper" ||
lowerword == "hlsl-iomapping") {
Options |= EOptionHlslIoMapping;
} else if (lowerword == "hlsl-enable-16bit-types") {
HlslEnable16BitTypes = true;
} else if (lowerword == "hlsl-dx9-compatible") {
HlslDX9compatible = true;
} else if (lowerword == "invert-y" || // synonyms
lowerword == "iy") {
Options |= EOptionInvertY;
} else if (lowerword == "keep-uncalled" || // synonyms
lowerword == "ku") {
Options |= EOptionKeepUncalled;
} else if (lowerword == "nan-clamp") {
NaNClamp = true;
} else if (lowerword == "no-storage-format" || // synonyms
lowerword == "nsf") {
Options |= EOptionNoStorageFormat;
} else if (lowerword == "relaxed-errors") {
Options |= EOptionRelaxedErrors;
} else if (lowerword == "reflect-strict-array-suffix") {
ReflectOptions |= EShReflectionStrictArraySuffix;
} else if (lowerword == "reflect-basic-array-suffix") {
ReflectOptions |= EShReflectionBasicArraySuffix;
} else if (lowerword == "reflect-intermediate-io") {
ReflectOptions |= EShReflectionIntermediateIO;
} else if (lowerword == "reflect-separate-buffers") {
ReflectOptions |= EShReflectionSeparateBuffers;
} else if (lowerword == "reflect-all-block-variables") {
ReflectOptions |= EShReflectionAllBlockVariables;
} else if (lowerword == "reflect-unwrap-io-blocks") {
ReflectOptions |= EShReflectionUnwrapIOBlocks;
} else if (lowerword == "reflect-all-io-variables") {
ReflectOptions |= EShReflectionAllIOVariables;
} else if (lowerword == "reflect-shared-std140-ubo") {
ReflectOptions |= EShReflectionSharedStd140UBO;
} else if (lowerword == "reflect-shared-std140-ssbo") {
ReflectOptions |= EShReflectionSharedStd140SSBO;
} else if (lowerword == "resource-set-bindings" || // synonyms
lowerword == "resource-set-binding" ||
lowerword == "rsb") {
ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
} else if (lowerword == "shift-image-bindings" || // synonyms
lowerword == "shift-image-binding" ||
lowerword == "sib") {
ProcessBindingBase(argc, argv, glslang::EResImage);
} else if (lowerword == "shift-sampler-bindings" || // synonyms
lowerword == "shift-sampler-binding" ||
lowerword == "ssb") {
ProcessBindingBase(argc, argv, glslang::EResSampler);
} else if (lowerword == "shift-uav-bindings" || // synonyms
lowerword == "shift-uav-binding" ||
lowerword == "suavb") {
ProcessBindingBase(argc, argv, glslang::EResUav);
} else if (lowerword == "shift-texture-bindings" || // synonyms
lowerword == "shift-texture-binding" ||
lowerword == "stb") {
ProcessBindingBase(argc, argv, glslang::EResTexture);
} else if (lowerword == "shift-ubo-bindings" || // synonyms
lowerword == "shift-ubo-binding" ||
lowerword == "shift-cbuffer-bindings" ||
lowerword == "shift-cbuffer-binding" ||
lowerword == "sub" ||
lowerword == "scb") {
ProcessBindingBase(argc, argv, glslang::EResUbo);
} else if (lowerword == "shift-ssbo-bindings" || // synonyms
lowerword == "shift-ssbo-binding" ||
lowerword == "sbb") {
ProcessBindingBase(argc, argv, glslang::EResSsbo);
} else if (lowerword == "source-entrypoint" || // synonyms
lowerword == "sep") {
if (argc <= 1)
Error("no <entry-point> provided", lowerword.c_str());
sourceEntryPointName = argv[1];
bumpArg();
break;
} else if (lowerword == "spirv-dis") {
SpvToolsDisassembler = true;
} else if (lowerword == "spirv-val") {
SpvToolsValidate = true;
} else if (lowerword == "stdin") {
Options |= EOptionStdin;
shaderStageName = argv[1];
} else if (lowerword == "suppress-warnings") {
Options |= EOptionSuppressWarnings;
} else if (lowerword == "target-env") {
if (argc > 1) {
if (strcmp(argv[1], "vulkan1.0") == 0) {
setVulkanSpv();
ClientVersion = glslang::EShTargetVulkan_1_0;
} else if (strcmp(argv[1], "vulkan1.1") == 0) {
setVulkanSpv();
ClientVersion = glslang::EShTargetVulkan_1_1;
} else if (strcmp(argv[1], "vulkan1.2") == 0) {
setVulkanSpv();
ClientVersion = glslang::EShTargetVulkan_1_2;
} else if (strcmp(argv[1], "opengl") == 0) {
setOpenGlSpv();
ClientVersion = glslang::EShTargetOpenGL_450;
} else if (strcmp(argv[1], "spirv1.0") == 0) {
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_0;
} else if (strcmp(argv[1], "spirv1.1") == 0) {
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_1;
} else if (strcmp(argv[1], "spirv1.2") == 0) {
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_2;
} else if (strcmp(argv[1], "spirv1.3") == 0) {
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_3;
} else if (strcmp(argv[1], "spirv1.4") == 0) {
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_4;
} else if (strcmp(argv[1], "spirv1.5") == 0) {
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_5;
} else
Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
"spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
}
bumpArg();
} else if (lowerword == "undef-macro" ||
lowerword == "u") {
if (argc > 1)
UserPreamble.addUndef(argv[1]);
else
Error("expects <name>", argv[0]);
bumpArg();
} else if (lowerword == "variable-name" || // synonyms
lowerword == "vn") {
Options |= EOptionOutputHexadecimal;
if (argc <= 1)
Error("no <C-variable-name> provided", lowerword.c_str());
variableName = argv[1];
bumpArg();
break;
} else if (lowerword == "quiet") {
beQuiet = true;
} else if (lowerword == "version") {
Options |= EOptionDumpVersions;
} else if (lowerword == "help") {
usage();
break;
} else {
Error("unrecognized command-line option", argv[0]);
}
}
break;
case 'C':
Options |= EOptionCascadingErrors;
break;
case 'D':
if (argv[0][2] == 0)
Options |= EOptionReadHlsl;
else
UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
break;
case 'u':
uniformLocationOverrides.push_back(getUniformOverride());
break;
case 'E':
Options |= EOptionOutputPreprocessed;
break;
case 'G':
// OpenGL client
setOpenGlSpv();
if (argv[0][2] != 0)
ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
break;
case 'H':
Options |= EOptionHumanReadableSpv;
if ((Options & EOptionSpv) == 0) {
// default to Vulkan
setVulkanSpv();
}
break;
case 'I':
IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
break;
case 'O':
if (argv[0][2] == 'd')
Options |= EOptionOptimizeDisable;
else if (argv[0][2] == 's')
#if ENABLE_OPT
Options |= EOptionOptimizeSize;
#else
Error("-Os not available; optimizer not linked");
#endif
else
Error("unknown -O option");
break;
case 'S':
if (argc <= 1)
Error("no <stage> specified for -S");
shaderStageName = argv[1];
bumpArg();
break;
case 'U':
UserPreamble.addUndef(getStringOperand("-U<name>"));
break;
case 'V':
setVulkanSpv();
if (argv[0][2] != 0)
ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
break;
case 'c':
Options |= EOptionDumpConfig;
break;
case 'd':
if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
Options |= EOptionDumpBareVersion;
else
Options |= EOptionDefaultDesktop;
break;
case 'e':
entryPointName = argv[1];
if (argc <= 1)
Error("no <name> provided for -e");
bumpArg();
break;
case 'f':
if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
targetHlslFunctionality1 = true;
else
Error("-f: expected hlsl_functionality1");
break;
case 'g':
// Override previous -g or -g0 argument
stripDebugInfo = false;
Options &= ~EOptionDebug;
if (argv[0][2] == '0')
stripDebugInfo = true;
else
Options |= EOptionDebug;
break;
case 'h':
usage();
break;
case 'i':
Options |= EOptionIntermediate;
break;
case 'l':
Options |= EOptionLinkProgram;
break;
case 'm':
Options |= EOptionMemoryLeakMode;
break;
case 'o':
if (argc <= 1)
Error("no <file> provided for -o");
binaryFileName = argv[1];
bumpArg();
break;
case 'q':
Options |= EOptionDumpReflection;
break;
case 'r':
Options |= EOptionRelaxedErrors;
break;
case 's':
Options |= EOptionSuppressInfolog;
break;
case 't':
Options |= EOptionMultiThreaded;
break;
case 'v':
Options |= EOptionDumpVersions;
break;
case 'w':
Options |= EOptionSuppressWarnings;
break;
case 'x':
Options |= EOptionOutputHexadecimal;
break;
default:
Error("unrecognized command-line option", argv[0]);
break;
}
} else {
std::string name(argv[0]);
if (! SetConfigFile(name)) {
workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
}
}
}
// Make sure that -S is always specified if --stdin is specified
if ((Options & EOptionStdin) && shaderStageName == nullptr)
Error("must provide -S when --stdin is given");
// Make sure that -E is not specified alongside linking (which includes SPV generation)
// Or things that require linking
if (Options & EOptionOutputPreprocessed) {
if (Options & EOptionLinkProgram)
Error("can't use -E when linking is selected");
if (Options & EOptionDumpReflection)
Error("reflection requires linking, which can't be used when -E when is selected");
}
// reflection requires linking
if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
Error("reflection requires -l for linking");
// -o or -x makes no sense if there is no target binary
if (binaryFileName && (Options & EOptionSpv) == 0)
Error("no binary generation requested (e.g., -V)");
if ((Options & EOptionFlattenUniformArrays) != 0 &&
(Options & EOptionReadHlsl) == 0)
Error("uniform array flattening only valid when compiling HLSL source.");
// rationalize client and target language
if (TargetLanguage == glslang::EShTargetNone) {
switch (ClientVersion) {
case glslang::EShTargetVulkan_1_0:
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_0;
break;
case glslang::EShTargetVulkan_1_1:
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_3;
break;
case glslang::EShTargetVulkan_1_2:
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_5;
break;
case glslang::EShTargetOpenGL_450:
TargetLanguage = glslang::EShTargetSpv;
TargetVersion = glslang::EShTargetSpv_1_0;
break;
default:
break;
}
}
if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
}
//
// Translate the meaningful subset of command-line options to parser-behavior options.
//
void SetMessageOptions(EShMessages& messages)
{
if (Options & EOptionRelaxedErrors)
messages = (EShMessages)(messages | EShMsgRelaxedErrors);
if (Options & EOptionIntermediate)
messages = (EShMessages)(messages | EShMsgAST);
if (Options & EOptionSuppressWarnings)
messages = (EShMessages)(messages | EShMsgSuppressWarnings);
if (Options & EOptionSpv)
messages = (EShMessages)(messages | EShMsgSpvRules);
if (Options & EOptionVulkanRules)
messages = (EShMessages)(messages | EShMsgVulkanRules);
if (Options & EOptionOutputPreprocessed)
messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
if (Options & EOptionReadHlsl)
messages = (EShMessages)(messages | EShMsgReadHlsl);
if (Options & EOptionCascadingErrors)
messages = (EShMessages)(messages | EShMsgCascadingErrors);
if (Options & EOptionKeepUncalled)
messages = (EShMessages)(messages | EShMsgKeepUncalled);
if (Options & EOptionHlslOffsets)
messages = (EShMessages)(messages | EShMsgHlslOffsets);
if (Options & EOptionDebug)
messages = (EShMessages)(messages | EShMsgDebugInfo);
if (HlslEnable16BitTypes)
messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
messages = (EShMessages)(messages | EShMsgHlslLegalization);
if (HlslDX9compatible)
messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
if (DumpBuiltinSymbols)
messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
}
//
// Thread entry point, for non-linking asynchronous mode.
//
void CompileShaders(glslang::TWorklist& worklist)
{
if (Options & EOptionDebug)
Error("cannot generate debug information unless linking to generate code");
glslang::TWorkItem* workItem;
if (Options & EOptionStdin) {
if (worklist.remove(workItem)) {
ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
if (compiler == nullptr)
return;
CompileFile("stdin", compiler);
if (! (Options & EOptionSuppressInfolog))
workItem->results = ShGetInfoLog(compiler);
ShDestruct(compiler);
}
} else {
while (worklist.remove(workItem)) {
ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
if (compiler == 0)
return;
CompileFile(workItem->name.c_str(), compiler);
if (! (Options & EOptionSuppressInfolog))
workItem->results = ShGetInfoLog(compiler);
ShDestruct(compiler);
}
}
}
// Outputs the given string, but only if it is non-null and non-empty.
// This prevents erroneous newlines from appearing.
void PutsIfNonEmpty(const char* str)
{
if (str && str[0]) {
puts(str);
}
}
// Outputs the given string to stderr, but only if it is non-null and non-empty.
// This prevents erroneous newlines from appearing.
void StderrIfNonEmpty(const char* str)
{
if (str && str[0])
fprintf(stderr, "%s\n", str);
}
// Simple bundling of what makes a compilation unit for ease in passing around,
// and separation of handling file IO versus API (programmatic) compilation.
struct ShaderCompUnit {
EShLanguage stage;
static const int maxCount = 1;
int count; // live number of strings/names
const char* text[maxCount]; // memory owned/managed externally
std::string fileName[maxCount]; // hold's the memory, but...
const char* fileNameList[maxCount]; // downstream interface wants pointers
ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
ShaderCompUnit(const ShaderCompUnit& rhs)
{
stage = rhs.stage;
count = rhs.count;
for (int i = 0; i < count; ++i) {
fileName[i] = rhs.fileName[i];
text[i] = rhs.text[i];
fileNameList[i] = rhs.fileName[i].c_str();
}
}
void addString(std::string& ifileName, const char* itext)
{
assert(count < maxCount);
fileName[count] = ifileName;
text[count] = itext;
fileNameList[count] = fileName[count].c_str();
++count;
}
};
//
// For linking mode: Will independently parse each compilation unit, but then put them
// in the same program and link them together, making at most one linked module per
// pipeline stage.
//
// Uses the new C++ interface instead of the old handle-based interface.