1 //
2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013-2016 LunarG, Inc.
4 // Copyright (C) 2016-2020 Google, Inc.
5 // Modifications Copyright(C) 2021 Advanced Micro Devices, Inc.All rights reserved.
6 //
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions
11 // are met:
12 //
13 // Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // Redistributions in binary form must reproduce the above
17 // copyright notice, this list of conditions and the following
18 // disclaimer in the documentation and/or other materials provided
19 // with the distribution.
20 //
21 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
22 // contributors may be used to endorse or promote products derived
23 // from this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 // POSSIBILITY OF SUCH DAMAGE.
37 //
38
39 // this only applies to the standalone wrapper, not the front end in general
40 #ifndef _CRT_SECURE_NO_WARNINGS
41 #define _CRT_SECURE_NO_WARNINGS
42 #endif
43
44 #include "glslang/Public/ResourceLimits.h"
45 #include "Worklist.h"
46 #include "DirStackFileIncluder.h"
47 #include "./../glslang/Public/ShaderLang.h"
48 #include "../glslang/MachineIndependent/localintermediate.h"
49 #include "../SPIRV/GlslangToSpv.h"
50 #include "../SPIRV/GLSL.std.450.h"
51 #include "../SPIRV/disassemble.h"
52
53 #include <array>
54 #include <atomic>
55 #include <cctype>
56 #include <cmath>
57 #include <cstdlib>
58 #include <cstring>
59 #include <map>
60 #include <memory>
61 #include <set>
62 #include <thread>
63
64 #include "../glslang/OSDependent/osinclude.h"
65
66 // Build-time generated includes
67 #include "glslang/build_info.h"
68
69 #include "glslang/glsl_intrinsic_header.h"
70
71 extern "C" {
72 GLSLANG_EXPORT void ShOutputHtml();
73 }
74
75 // Command-line options
76 enum TOptions : uint64_t {
77 EOptionNone = 0,
78 EOptionIntermediate = (1ull << 0),
79 EOptionSuppressInfolog = (1ull << 1),
80 EOptionMemoryLeakMode = (1ull << 2),
81 EOptionRelaxedErrors = (1ull << 3),
82 EOptionGiveWarnings = (1ull << 4),
83 EOptionLinkProgram = (1ull << 5),
84 EOptionMultiThreaded = (1ull << 6),
85 EOptionDumpConfig = (1ull << 7),
86 EOptionDumpReflection = (1ull << 8),
87 EOptionSuppressWarnings = (1ull << 9),
88 EOptionDumpVersions = (1ull << 10),
89 EOptionSpv = (1ull << 11),
90 EOptionHumanReadableSpv = (1ull << 12),
91 EOptionVulkanRules = (1ull << 13),
92 EOptionDefaultDesktop = (1ull << 14),
93 EOptionOutputPreprocessed = (1ull << 15),
94 EOptionOutputHexadecimal = (1ull << 16),
95 EOptionReadHlsl = (1ull << 17),
96 EOptionCascadingErrors = (1ull << 18),
97 EOptionAutoMapBindings = (1ull << 19),
98 EOptionFlattenUniformArrays = (1ull << 20),
99 EOptionNoStorageFormat = (1ull << 21),
100 EOptionKeepUncalled = (1ull << 22),
101 EOptionHlslOffsets = (1ull << 23),
102 EOptionHlslIoMapping = (1ull << 24),
103 EOptionAutoMapLocations = (1ull << 25),
104 EOptionDebug = (1ull << 26),
105 EOptionStdin = (1ull << 27),
106 EOptionOptimizeDisable = (1ull << 28),
107 EOptionOptimizeSize = (1ull << 29),
108 EOptionInvertY = (1ull << 30),
109 EOptionDumpBareVersion = (1ull << 31),
110 EOptionCompileOnly = (1ull << 32),
111 EOptionDisplayErrorColumn = (1ull << 33),
112 EOptionLinkTimeOptimization = (1ull << 34),
113 };
114 bool targetHlslFunctionality1 = false;
115 bool SpvToolsDisassembler = false;
116 bool SpvToolsValidate = false;
117 bool NaNClamp = false;
118 bool stripDebugInfo = false;
119 bool emitNonSemanticShaderDebugInfo = false;
120 bool emitNonSemanticShaderDebugSource = false;
121 bool beQuiet = false;
122 bool VulkanRulesRelaxed = false;
123 bool autoSampledTextures = false;
124
125 //
126 // Return codes from main/exit().
127 //
128 enum TFailCode {
129 ESuccess = 0,
130 EFailUsage,
131 EFailCompile,
132 EFailLink,
133 EFailCompilerCreate,
134 EFailThreadCreate,
135 EFailLinkerCreate
136 };
137
138 //
139 // Forward declarations.
140 //
141 EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
142 void CompileFile(const char* fileName, ShHandle);
143 void usage();
144 char* ReadFileData(const char* fileName);
145 void FreeFileData(char* data);
146 void InfoLogMsg(const char* msg, const char* name, const int num);
147
148 // Globally track if any compile or link failure.
149 std::atomic<int8_t> CompileFailed{0};
150 std::atomic<int8_t> LinkFailed{0};
151 std::atomic<int8_t> CompileOrLinkFailed{0};
152
153 // array of unique places to leave the shader names and infologs for the asynchronous compiles
154 std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
155
156 std::string ConfigFile;
157
158 //
159 // Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
160 //
ProcessConfigFile()161 void ProcessConfigFile()
162 {
163 if (ConfigFile.size() == 0)
164 *GetResources() = *GetDefaultResources();
165 else {
166 char* configString = ReadFileData(ConfigFile.c_str());
167 DecodeResourceLimits(GetResources(), configString);
168 FreeFileData(configString);
169 }
170 }
171
172 int ReflectOptions = EShReflectionDefault;
173 std::underlying_type_t<TOptions> Options = EOptionNone;
174 const char* ExecutableName = nullptr;
175 const char* binaryFileName = nullptr;
176 const char* depencyFileName = nullptr;
177 const char* entryPointName = nullptr;
178 const char* sourceEntryPointName = nullptr;
179 const char* shaderStageName = nullptr;
180 const char* variableName = nullptr;
181 bool HlslEnable16BitTypes = false;
182 bool HlslDX9compatible = false;
183 bool HlslDxPositionW = false;
184 bool EnhancedMsgs = false;
185 bool AbsolutePath = false;
186 bool DumpBuiltinSymbols = false;
187 std::vector<std::string> IncludeDirectoryList;
188
189 // Source environment
190 // (source 'Client' is currently the same as target 'Client')
191 int ClientInputSemanticsVersion = 100;
192
193 // Target environment
194 glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
195 glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
196 glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
197 glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
198
199 // GLSL version
200 int GlslVersion = 0; // GLSL version specified on CLI, overrides #version in shader source
201
202 std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
203
204 // Per descriptor-set binding base data
205 typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
206
207 std::vector<std::pair<std::string, int>> uniformLocationOverrides;
208 int uniformBase = 0;
209
210 std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
211 std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
212 std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
213
214 std::vector<std::pair<std::string, glslang::TBlockStorageClass>> blockStorageOverrides;
215
216 bool setGlobalUniformBlock = false;
217 std::string globalUniformName;
218 unsigned int globalUniformBinding;
219 unsigned int globalUniformSet;
220
221 bool setGlobalBufferBlock = false;
222 std::string atomicCounterBlockName;
223 unsigned int atomicCounterBlockSet;
224
225 // Add things like "#define ..." to a preamble to use in the beginning of the shader.
226 class TPreamble {
227 public:
TPreamble()228 TPreamble() { }
229
isSet() const230 bool isSet() const { return text.size() > 0; }
get() const231 const char* get() const { return text.c_str(); }
232
233 // #define...
addDef(std::string def)234 void addDef(std::string def)
235 {
236 text.append("#define ");
237 fixLine(def);
238
239 Processes.push_back("define-macro ");
240 Processes.back().append(def);
241
242 // The first "=" needs to turn into a space
243 const size_t equal = def.find_first_of("=");
244 if (equal != def.npos)
245 def[equal] = ' ';
246
247 text.append(def);
248 text.append("\n");
249 }
250
251 // #undef...
addUndef(std::string undef)252 void addUndef(std::string undef)
253 {
254 text.append("#undef ");
255 fixLine(undef);
256
257 Processes.push_back("undef-macro ");
258 Processes.back().append(undef);
259
260 text.append(undef);
261 text.append("\n");
262 }
263
addText(std::string preambleText)264 void addText(std::string preambleText)
265 {
266 fixLine(preambleText);
267
268 Processes.push_back("preamble-text");
269 Processes.back().append(preambleText);
270
271 text.append(preambleText);
272 text.append("\n");
273 }
274
275 protected:
fixLine(std::string & line)276 void fixLine(std::string& line)
277 {
278 // Can't go past a newline in the line
279 const size_t end = line.find_first_of("\n");
280 if (end != line.npos)
281 line = line.substr(0, end);
282 }
283
284 std::string text; // contents of preamble
285 };
286
287 // Track the user's #define and #undef from the command line.
288 TPreamble UserPreamble;
289 std::string PreambleString;
290
291 //
292 // Create the default name for saving a binary if -o is not provided.
293 //
GetBinaryName(EShLanguage stage)294 const char* GetBinaryName(EShLanguage stage)
295 {
296 const char* name;
297 if (binaryFileName == nullptr) {
298 switch (stage) {
299 case EShLangVertex: name = "vert.spv"; break;
300 case EShLangTessControl: name = "tesc.spv"; break;
301 case EShLangTessEvaluation: name = "tese.spv"; break;
302 case EShLangGeometry: name = "geom.spv"; break;
303 case EShLangFragment: name = "frag.spv"; break;
304 case EShLangCompute: name = "comp.spv"; break;
305 case EShLangRayGen: name = "rgen.spv"; break;
306 case EShLangIntersect: name = "rint.spv"; break;
307 case EShLangAnyHit: name = "rahit.spv"; break;
308 case EShLangClosestHit: name = "rchit.spv"; break;
309 case EShLangMiss: name = "rmiss.spv"; break;
310 case EShLangCallable: name = "rcall.spv"; break;
311 case EShLangMesh : name = "mesh.spv"; break;
312 case EShLangTask : name = "task.spv"; break;
313 default: name = "unknown"; break;
314 }
315 } else
316 name = binaryFileName;
317
318 return name;
319 }
320
321 //
322 // *.conf => this is a config file that can set limits/resources
323 //
SetConfigFile(const std::string & name)324 bool SetConfigFile(const std::string& name)
325 {
326 if (name.size() < 5)
327 return false;
328
329 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
330 ConfigFile = name;
331 return true;
332 }
333
334 return false;
335 }
336
337 //
338 // Give error and exit with failure code.
339 //
Error(const char * message,const char * detail=nullptr)340 void Error(const char* message, const char* detail = nullptr)
341 {
342 fprintf(stderr, "%s: Error: ", ExecutableName);
343 if (detail != nullptr)
344 fprintf(stderr, "%s: ", detail);
345 fprintf(stderr, "%s (use -h for usage)\n", message);
346 exit(EFailUsage);
347 }
348
349 //
350 // Process an optional binding base of one the forms:
351 // --argname [stage] base // base for stage (if given) or all stages (if not)
352 // --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
353
354 // Where stage is one of the forms accepted by FindLanguage, and base is an integer
355 //
ProcessBindingBase(int & argc,char ** & argv,glslang::TResourceType res)356 void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
357 {
358 if (argc < 2)
359 usage();
360
361 EShLanguage lang = EShLangCount;
362 int singleBase = 0;
363 TPerSetBaseBinding perSetBase;
364 int arg = 1;
365
366 // Parse stage, if given
367 if (!isdigit(argv[arg][0])) {
368 if (argc < 3) // this form needs one more argument
369 usage();
370
371 lang = FindLanguage(argv[arg++], false);
372 }
373
374 if ((argc - arg) >= 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
375 // Parse a per-set binding base
376 do {
377 const int baseNum = atoi(argv[arg++]);
378 const int setNum = atoi(argv[arg++]);
379 perSetBase[setNum] = baseNum;
380 } while ((argc - arg) >= 2 && isdigit(argv[arg + 0][0]) && isdigit(argv[arg + 1][0]));
381 } else {
382 // Parse single binding base
383 singleBase = atoi(argv[arg++]);
384 }
385
386 argc -= (arg-1);
387 argv += (arg-1);
388
389 // Set one or all languages
390 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
391 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
392
393 for (int lang = langMin; lang < langMax; ++lang) {
394 if (!perSetBase.empty())
395 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
396 else
397 baseBinding[res][lang] = singleBase;
398 }
399 }
400
ProcessResourceSetBindingBase(int & argc,char ** & argv,std::array<std::vector<std::string>,EShLangCount> & base)401 void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
402 {
403 if (argc < 2)
404 usage();
405
406 if (!isdigit(argv[1][0])) {
407 if (argc < 3) // this form needs one more argument
408 usage();
409
410 // Parse form: --argname stage [regname set base...], or:
411 // --argname stage set
412 const EShLanguage lang = FindLanguage(argv[1], false);
413
414 argc--;
415 argv++;
416
417 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
418 base[lang].push_back(argv[1]);
419
420 argc--;
421 argv++;
422 }
423
424 // Must have one arg, or a multiple of three (for [regname set binding] triples)
425 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
426 usage();
427
428 } else {
429 // Parse form: --argname set
430 for (int lang=0; lang<EShLangCount; ++lang)
431 base[lang].push_back(argv[1]);
432
433 argc--;
434 argv++;
435 }
436 }
437
438 //
439 // Process an optional binding base of one the forms:
440 // --argname name {uniform|buffer|push_constant}
ProcessBlockStorage(int & argc,char ** & argv,std::vector<std::pair<std::string,glslang::TBlockStorageClass>> & storage)441 void ProcessBlockStorage(int& argc, char**& argv, std::vector<std::pair<std::string, glslang::TBlockStorageClass>>& storage)
442 {
443 if (argc < 3)
444 usage();
445
446 glslang::TBlockStorageClass blockStorage = glslang::EbsNone;
447
448 std::string strBacking(argv[2]);
449 if (strBacking == "uniform")
450 blockStorage = glslang::EbsUniform;
451 else if (strBacking == "buffer")
452 blockStorage = glslang::EbsStorageBuffer;
453 else if (strBacking == "push_constant")
454 blockStorage = glslang::EbsPushConstant;
455 else {
456 printf("%s: invalid block storage\n", strBacking.c_str());
457 usage();
458 }
459
460 storage.push_back(std::make_pair(std::string(argv[1]), blockStorage));
461
462 argc -= 2;
463 argv += 2;
464 }
465
isNonDigit(char c)466 inline bool isNonDigit(char c) {
467 // a non-digit character valid in a glsl identifier
468 return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
469 }
470
471 // whether string isa valid identifier to be used in glsl
isValidIdentifier(const char * str)472 bool isValidIdentifier(const char* str) {
473 std::string idn(str);
474
475 if (idn.length() == 0) {
476 return false;
477 }
478
479 if (idn.length() >= 3 && idn.substr(0, 3) == "gl_") {
480 // identifiers startin with "gl_" are reserved
481 return false;
482 }
483
484 if (!isNonDigit(idn[0])) {
485 return false;
486 }
487
488 for (unsigned int i = 1; i < idn.length(); ++i) {
489 if (!(isdigit(idn[i]) || isNonDigit(idn[i]))) {
490 return false;
491 }
492 }
493
494 return true;
495 }
496
497 // Process settings for either the global buffer block or global unfirom block
498 // of the form:
499 // --argname name set binding
ProcessGlobalBlockSettings(int & argc,char ** & argv,std::string * name,unsigned int * set,unsigned int * binding)500 void ProcessGlobalBlockSettings(int& argc, char**& argv, std::string* name, unsigned int* set, unsigned int* binding)
501 {
502 if (argc < 4)
503 usage();
504
505 unsigned int curArg = 1;
506
507 assert(name || set || binding);
508
509 if (name) {
510 if (!isValidIdentifier(argv[curArg])) {
511 printf("%s: invalid identifier\n", argv[curArg]);
512 usage();
513 }
514 *name = argv[curArg];
515
516 curArg++;
517 }
518
519 if (set) {
520 errno = 0;
521 int setVal = static_cast<int>(::strtol(argv[curArg], nullptr, 10));
522 if (errno || setVal < 0) {
523 printf("%s: invalid set\n", argv[curArg]);
524 usage();
525 }
526 *set = setVal;
527
528 curArg++;
529 }
530
531 if (binding) {
532 errno = 0;
533 int bindingVal = static_cast<int>(::strtol(argv[curArg], nullptr, 10));
534 if (errno || bindingVal < 0) {
535 printf("%s: invalid binding\n", argv[curArg]);
536 usage();
537 }
538 *binding = bindingVal;
539
540 curArg++;
541 }
542
543 argc -= (curArg - 1);
544 argv += (curArg - 1);
545 }
546
547 //
548 // Do all command-line argument parsing. This includes building up the work-items
549 // to be processed later, and saving all the command-line options.
550 //
551 // Does not return (it exits) if command-line is fatally flawed.
552 //
ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>> & workItems,int argc,char * argv[])553 void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
554 {
555 for (int res = 0; res < glslang::EResCount; ++res)
556 baseBinding[res].fill(0);
557
558 ExecutableName = argv[0];
559 workItems.reserve(argc);
560
561 const auto bumpArg = [&]() {
562 if (argc > 0) {
563 argc--;
564 argv++;
565 }
566 };
567
568 // read a string directly attached to a single-letter option
569 const auto getStringOperand = [&](const char* desc) {
570 if (argv[0][2] == 0) {
571 printf("%s must immediately follow option (no spaces)\n", desc);
572 exit(EFailUsage);
573 }
574 return argv[0] + 2;
575 };
576
577 // read a number attached to a single-letter option
578 const auto getAttachedNumber = [&](const char* desc) {
579 int num = atoi(argv[0] + 2);
580 if (num == 0) {
581 printf("%s: expected attached non-0 number\n", desc);
582 exit(EFailUsage);
583 }
584 return num;
585 };
586
587 // minimum needed (without overriding something else) to target Vulkan SPIR-V
588 const auto setVulkanSpv = []() {
589 if (Client == glslang::EShClientNone)
590 ClientVersion = glslang::EShTargetVulkan_1_0;
591 Client = glslang::EShClientVulkan;
592 Options |= EOptionSpv;
593 Options |= EOptionVulkanRules;
594 Options |= EOptionLinkProgram;
595 };
596
597 // minimum needed (without overriding something else) to target OpenGL SPIR-V
598 const auto setOpenGlSpv = []() {
599 if (Client == glslang::EShClientNone)
600 ClientVersion = glslang::EShTargetOpenGL_450;
601 Client = glslang::EShClientOpenGL;
602 Options |= EOptionSpv;
603 Options |= EOptionLinkProgram;
604 // undo a -H default to Vulkan
605 Options &= ~EOptionVulkanRules;
606 };
607
608 const auto getUniformOverride = [getStringOperand]() {
609 const char *arg = getStringOperand("-u<name>:<location>");
610 const char *split = strchr(arg, ':');
611 if (split == nullptr) {
612 printf("%s: missing location\n", arg);
613 exit(EFailUsage);
614 }
615 errno = 0;
616 int location = static_cast<int>(::strtol(split + 1, nullptr, 10));
617 if (errno) {
618 printf("%s: invalid location\n", arg);
619 exit(EFailUsage);
620 }
621 return std::make_pair(std::string(arg, split - arg), location);
622 };
623
624 for (bumpArg(); argc >= 1; bumpArg()) {
625 if (argv[0][0] == '-') {
626 switch (argv[0][1]) {
627 case '-':
628 {
629 std::string lowerword(argv[0]+2);
630 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
631
632 // handle --word style options
633 if (lowerword == "auto-map-bindings" || // synonyms
634 lowerword == "auto-map-binding" ||
635 lowerword == "amb") {
636 Options |= EOptionAutoMapBindings;
637 } else if (lowerword == "auto-map-locations" || // synonyms
638 lowerword == "aml") {
639 Options |= EOptionAutoMapLocations;
640 } else if (lowerword == "uniform-base") {
641 if (argc <= 1)
642 Error("no <base> provided", lowerword.c_str());
643 uniformBase = static_cast<int>(::strtol(argv[1], nullptr, 10));
644 bumpArg();
645 break;
646 } else if (lowerword == "client") {
647 if (argc > 1) {
648 if (strcmp(argv[1], "vulkan100") == 0)
649 setVulkanSpv();
650 else if (strcmp(argv[1], "opengl100") == 0)
651 setOpenGlSpv();
652 else
653 Error("expects vulkan100 or opengl100", lowerword.c_str());
654 } else
655 Error("expects vulkan100 or opengl100", lowerword.c_str());
656 bumpArg();
657 } else if (lowerword == "define-macro" ||
658 lowerword == "d") {
659 if (argc > 1)
660 UserPreamble.addDef(argv[1]);
661 else
662 Error("expects <name[=def]>", argv[0]);
663 bumpArg();
664 } else if (lowerword == "dump-builtin-symbols") {
665 DumpBuiltinSymbols = true;
666 } else if (lowerword == "entry-point") {
667 entryPointName = argv[1];
668 if (argc <= 1)
669 Error("no <name> provided", lowerword.c_str());
670 bumpArg();
671 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
672 lowerword == "flatten-uniform-array" ||
673 lowerword == "fua") {
674 Options |= EOptionFlattenUniformArrays;
675 } else if (lowerword == "glsl-version") {
676 if (argc > 1) {
677 if (strcmp(argv[1], "100") == 0) {
678 GlslVersion = 100;
679 } else if (strcmp(argv[1], "110") == 0) {
680 GlslVersion = 110;
681 } else if (strcmp(argv[1], "120") == 0) {
682 GlslVersion = 120;
683 } else if (strcmp(argv[1], "130") == 0) {
684 GlslVersion = 130;
685 } else if (strcmp(argv[1], "140") == 0) {
686 GlslVersion = 140;
687 } else if (strcmp(argv[1], "150") == 0) {
688 GlslVersion = 150;
689 } else if (strcmp(argv[1], "300es") == 0) {
690 GlslVersion = 300;
691 } else if (strcmp(argv[1], "310es") == 0) {
692 GlslVersion = 310;
693 } else if (strcmp(argv[1], "320es") == 0) {
694 GlslVersion = 320;
695 } else if (strcmp(argv[1], "330") == 0) {
696 GlslVersion = 330;
697 } else if (strcmp(argv[1], "400") == 0) {
698 GlslVersion = 400;
699 } else if (strcmp(argv[1], "410") == 0) {
700 GlslVersion = 410;
701 } else if (strcmp(argv[1], "420") == 0) {
702 GlslVersion = 420;
703 } else if (strcmp(argv[1], "430") == 0) {
704 GlslVersion = 430;
705 } else if (strcmp(argv[1], "440") == 0) {
706 GlslVersion = 440;
707 } else if (strcmp(argv[1], "450") == 0) {
708 GlslVersion = 450;
709 } else if (strcmp(argv[1], "460") == 0) {
710 GlslVersion = 460;
711 } else
712 Error("--glsl-version expected one of: 100, 110, 120, 130, 140, 150,\n"
713 "300es, 310es, 320es, 330\n"
714 "400, 410, 420, 430, 440, 450, 460");
715 }
716 bumpArg();
717 } else if (lowerword == "hlsl-offsets") {
718 Options |= EOptionHlslOffsets;
719 } else if (lowerword == "hlsl-iomap" ||
720 lowerword == "hlsl-iomapper" ||
721 lowerword == "hlsl-iomapping") {
722 Options |= EOptionHlslIoMapping;
723 } else if (lowerword == "hlsl-enable-16bit-types") {
724 HlslEnable16BitTypes = true;
725 } else if (lowerword == "hlsl-dx9-compatible") {
726 HlslDX9compatible = true;
727 } else if (lowerword == "hlsl-dx-position-w") {
728 HlslDxPositionW = true;
729 } else if (lowerword == "enhanced-msgs") {
730 EnhancedMsgs = true;
731 } else if (lowerword == "absolute-path") {
732 AbsolutePath = true;
733 } else if (lowerword == "auto-sampled-textures") {
734 autoSampledTextures = true;
735 } else if (lowerword == "invert-y" || // synonyms
736 lowerword == "iy") {
737 Options |= EOptionInvertY;
738 } else if (lowerword == "keep-uncalled" || // synonyms
739 lowerword == "ku") {
740 Options |= EOptionKeepUncalled;
741 } else if (lowerword == "nan-clamp") {
742 NaNClamp = true;
743 } else if (lowerword == "no-storage-format" || // synonyms
744 lowerword == "nsf") {
745 Options |= EOptionNoStorageFormat;
746 } else if (lowerword == "preamble-text" ||
747 lowerword == "p") {
748 if (argc > 1)
749 UserPreamble.addText(argv[1]);
750 else
751 Error("expects <text>", argv[0]);
752 bumpArg();
753 } else if (lowerword == "relaxed-errors") {
754 Options |= EOptionRelaxedErrors;
755 } else if (lowerword == "reflect-strict-array-suffix") {
756 ReflectOptions |= EShReflectionStrictArraySuffix;
757 } else if (lowerword == "reflect-basic-array-suffix") {
758 ReflectOptions |= EShReflectionBasicArraySuffix;
759 } else if (lowerword == "reflect-intermediate-io") {
760 ReflectOptions |= EShReflectionIntermediateIO;
761 } else if (lowerword == "reflect-separate-buffers") {
762 ReflectOptions |= EShReflectionSeparateBuffers;
763 } else if (lowerword == "reflect-all-block-variables") {
764 ReflectOptions |= EShReflectionAllBlockVariables;
765 } else if (lowerword == "reflect-unwrap-io-blocks") {
766 ReflectOptions |= EShReflectionUnwrapIOBlocks;
767 } else if (lowerword == "reflect-all-io-variables") {
768 ReflectOptions |= EShReflectionAllIOVariables;
769 } else if (lowerword == "reflect-shared-std140-ubo") {
770 ReflectOptions |= EShReflectionSharedStd140UBO;
771 } else if (lowerword == "reflect-shared-std140-ssbo") {
772 ReflectOptions |= EShReflectionSharedStd140SSBO;
773 } else if (lowerword == "resource-set-bindings" || // synonyms
774 lowerword == "resource-set-binding" ||
775 lowerword == "rsb") {
776 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
777 } else if (lowerword == "set-block-storage" ||
778 lowerword == "sbs") {
779 ProcessBlockStorage(argc, argv, blockStorageOverrides);
780 } else if (lowerword == "set-atomic-counter-block" ||
781 lowerword == "sacb") {
782 ProcessGlobalBlockSettings(argc, argv, &atomicCounterBlockName, &atomicCounterBlockSet, nullptr);
783 setGlobalBufferBlock = true;
784 } else if (lowerword == "set-default-uniform-block" ||
785 lowerword == "sdub") {
786 ProcessGlobalBlockSettings(argc, argv, &globalUniformName, &globalUniformSet, &globalUniformBinding);
787 setGlobalUniformBlock = true;
788 } else if (lowerword == "shift-image-bindings" || // synonyms
789 lowerword == "shift-image-binding" ||
790 lowerword == "sib") {
791 ProcessBindingBase(argc, argv, glslang::EResImage);
792 } else if (lowerword == "shift-sampler-bindings" || // synonyms
793 lowerword == "shift-sampler-binding" ||
794 lowerword == "ssb") {
795 ProcessBindingBase(argc, argv, glslang::EResSampler);
796 } else if (lowerword == "shift-uav-bindings" || // synonyms
797 lowerword == "shift-uav-binding" ||
798 lowerword == "suavb") {
799 ProcessBindingBase(argc, argv, glslang::EResUav);
800 } else if (lowerword == "shift-texture-bindings" || // synonyms
801 lowerword == "shift-texture-binding" ||
802 lowerword == "stb") {
803 ProcessBindingBase(argc, argv, glslang::EResTexture);
804 } else if (lowerword == "shift-ubo-bindings" || // synonyms
805 lowerword == "shift-ubo-binding" ||
806 lowerword == "shift-cbuffer-bindings" ||
807 lowerword == "shift-cbuffer-binding" ||
808 lowerword == "sub" ||
809 lowerword == "scb") {
810 ProcessBindingBase(argc, argv, glslang::EResUbo);
811 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
812 lowerword == "shift-ssbo-binding" ||
813 lowerword == "sbb") {
814 ProcessBindingBase(argc, argv, glslang::EResSsbo);
815 } else if (lowerword == "source-entrypoint" || // synonyms
816 lowerword == "sep") {
817 if (argc <= 1)
818 Error("no <entry-point> provided", lowerword.c_str());
819 sourceEntryPointName = argv[1];
820 bumpArg();
821 break;
822 } else if (lowerword == "spirv-dis") {
823 SpvToolsDisassembler = true;
824 } else if (lowerword == "spirv-val") {
825 SpvToolsValidate = true;
826 } else if (lowerword == "stdin") {
827 Options |= EOptionStdin;
828 shaderStageName = argv[1];
829 } else if (lowerword == "suppress-warnings") {
830 Options |= EOptionSuppressWarnings;
831 } else if (lowerword == "target-env") {
832 if (argc > 1) {
833 if (strcmp(argv[1], "vulkan1.0") == 0) {
834 setVulkanSpv();
835 ClientVersion = glslang::EShTargetVulkan_1_0;
836 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
837 setVulkanSpv();
838 ClientVersion = glslang::EShTargetVulkan_1_1;
839 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
840 setVulkanSpv();
841 ClientVersion = glslang::EShTargetVulkan_1_2;
842 } else if (strcmp(argv[1], "vulkan1.3") == 0) {
843 setVulkanSpv();
844 ClientVersion = glslang::EShTargetVulkan_1_3;
845 } else if (strcmp(argv[1], "vulkan1.4") == 0) {
846 setVulkanSpv();
847 ClientVersion = glslang::EShTargetVulkan_1_4;
848 } else if (strcmp(argv[1], "opengl") == 0) {
849 setOpenGlSpv();
850 ClientVersion = glslang::EShTargetOpenGL_450;
851 } else if (strcmp(argv[1], "spirv1.0") == 0) {
852 TargetLanguage = glslang::EShTargetSpv;
853 TargetVersion = glslang::EShTargetSpv_1_0;
854 } else if (strcmp(argv[1], "spirv1.1") == 0) {
855 TargetLanguage = glslang::EShTargetSpv;
856 TargetVersion = glslang::EShTargetSpv_1_1;
857 } else if (strcmp(argv[1], "spirv1.2") == 0) {
858 TargetLanguage = glslang::EShTargetSpv;
859 TargetVersion = glslang::EShTargetSpv_1_2;
860 } else if (strcmp(argv[1], "spirv1.3") == 0) {
861 TargetLanguage = glslang::EShTargetSpv;
862 TargetVersion = glslang::EShTargetSpv_1_3;
863 } else if (strcmp(argv[1], "spirv1.4") == 0) {
864 TargetLanguage = glslang::EShTargetSpv;
865 TargetVersion = glslang::EShTargetSpv_1_4;
866 } else if (strcmp(argv[1], "spirv1.5") == 0) {
867 TargetLanguage = glslang::EShTargetSpv;
868 TargetVersion = glslang::EShTargetSpv_1_5;
869 } else if (strcmp(argv[1], "spirv1.6") == 0) {
870 TargetLanguage = glslang::EShTargetSpv;
871 TargetVersion = glslang::EShTargetSpv_1_6;
872 } else
873 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2,\n"
874 "vulkan1.3, opengl, spirv1.0, spirv1.1, spirv1.2, spirv1.3,\n"
875 "spirv1.4, spirv1.5 or spirv1.6");
876 }
877 bumpArg();
878 } else if (lowerword == "undef-macro" ||
879 lowerword == "u") {
880 if (argc > 1)
881 UserPreamble.addUndef(argv[1]);
882 else
883 Error("expects <name>", argv[0]);
884 bumpArg();
885 } else if (lowerword == "variable-name" || // synonyms
886 lowerword == "vn") {
887 Options |= EOptionOutputHexadecimal;
888 if (argc <= 1)
889 Error("no <C-variable-name> provided", lowerword.c_str());
890 variableName = argv[1];
891 bumpArg();
892 break;
893 } else if (lowerword == "quiet") {
894 beQuiet = true;
895 } else if (lowerword == "depfile") {
896 if (argc <= 1)
897 Error("no <depfile-name> provided", lowerword.c_str());
898 depencyFileName = argv[1];
899 bumpArg();
900 } else if (lowerword == "version") {
901 Options |= EOptionDumpVersions;
902 } else if (lowerword == "no-link") {
903 Options |= EOptionCompileOnly;
904 } else if (lowerword == "error-column") {
905 Options |= EOptionDisplayErrorColumn;
906 } else if (lowerword == "lto") {
907 Options |= EOptionLinkTimeOptimization;
908 } else if (lowerword == "help") {
909 usage();
910 break;
911 } else {
912 Error("unrecognized command-line option", argv[0]);
913 }
914 }
915 break;
916 case 'C':
917 Options |= EOptionCascadingErrors;
918 break;
919 case 'D':
920 if (argv[0][2] == 0)
921 Options |= EOptionReadHlsl;
922 else
923 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
924 break;
925 case 'u':
926 uniformLocationOverrides.push_back(getUniformOverride());
927 break;
928 case 'E':
929 Options |= EOptionOutputPreprocessed;
930 break;
931 case 'G':
932 // OpenGL client
933 setOpenGlSpv();
934 if (argv[0][2] != 0)
935 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
936 if (ClientInputSemanticsVersion != 100)
937 Error("unknown client version for -G, should be 100");
938 break;
939 case 'H':
940 Options |= EOptionHumanReadableSpv;
941 if ((Options & EOptionSpv) == 0) {
942 // default to Vulkan
943 setVulkanSpv();
944 }
945 break;
946 case 'I':
947 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
948 break;
949 case 'O':
950 if (argv[0][2] == 'd')
951 Options |= EOptionOptimizeDisable;
952 else if (argv[0][2] == 's')
953 #if ENABLE_OPT
954 Options |= EOptionOptimizeSize;
955 #else
956 Error("-Os not available; optimizer not linked");
957 #endif
958 else
959 Error("unknown -O option");
960 break;
961 case 'P':
962 UserPreamble.addText(getStringOperand("-P<text>"));
963 break;
964 case 'R':
965 VulkanRulesRelaxed = true;
966 break;
967 case 'S':
968 if (argc <= 1)
969 Error("no <stage> specified for -S");
970 shaderStageName = argv[1];
971 bumpArg();
972 break;
973 case 'U':
974 UserPreamble.addUndef(getStringOperand("-U<name>"));
975 break;
976 case 'V':
977 setVulkanSpv();
978 if (argv[0][2] != 0)
979 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
980 if (ClientInputSemanticsVersion != 100)
981 Error("unknown client version for -V, should be 100");
982 break;
983 case 'c':
984 Options |= EOptionDumpConfig;
985 break;
986 case 'd':
987 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
988 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
989 Options |= EOptionDumpBareVersion;
990 else
991 Options |= EOptionDefaultDesktop;
992 break;
993 case 'e':
994 entryPointName = argv[1];
995 if (argc <= 1)
996 Error("no <name> provided for -e");
997 bumpArg();
998 break;
999 case 'f':
1000 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
1001 targetHlslFunctionality1 = true;
1002 else
1003 Error("-f: expected hlsl_functionality1");
1004 break;
1005 case 'g':
1006 // Override previous -g or -g0 argument
1007 stripDebugInfo = false;
1008 emitNonSemanticShaderDebugInfo = false;
1009 Options &= ~EOptionDebug;
1010 if (argv[0][2] == '0')
1011 stripDebugInfo = true;
1012 else {
1013 Options |= EOptionDebug;
1014 if (argv[0][2] == 'V') {
1015 emitNonSemanticShaderDebugInfo = true;
1016 if (argv[0][3] == 'S') {
1017 emitNonSemanticShaderDebugSource = true;
1018 } else {
1019 emitNonSemanticShaderDebugSource = false;
1020 }
1021 }
1022 }
1023 break;
1024 case 'h':
1025 usage();
1026 break;
1027 case 'i':
1028 Options |= EOptionIntermediate;
1029 break;
1030 case 'l':
1031 Options |= EOptionLinkProgram;
1032 break;
1033 case 'm':
1034 Options |= EOptionMemoryLeakMode;
1035 break;
1036 case 'o':
1037 if (argc <= 1)
1038 Error("no <file> provided for -o");
1039 binaryFileName = argv[1];
1040 bumpArg();
1041 break;
1042 case 'q':
1043 Options |= EOptionDumpReflection;
1044 break;
1045 case 'r':
1046 Options |= EOptionRelaxedErrors;
1047 break;
1048 case 's':
1049 Options |= EOptionSuppressInfolog;
1050 break;
1051 case 't':
1052 Options |= EOptionMultiThreaded;
1053 break;
1054 case 'v':
1055 Options |= EOptionDumpVersions;
1056 break;
1057 case 'w':
1058 Options |= EOptionSuppressWarnings;
1059 break;
1060 case 'x':
1061 Options |= EOptionOutputHexadecimal;
1062 break;
1063 default:
1064 Error("unrecognized command-line option", argv[0]);
1065 break;
1066 }
1067 } else {
1068 std::string name(argv[0]);
1069 if (! SetConfigFile(name)) {
1070 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
1071 }
1072 }
1073 }
1074
1075 // Make sure that -S is always specified if --stdin is specified
1076 if ((Options & EOptionStdin) && shaderStageName == nullptr)
1077 Error("must provide -S when --stdin is given");
1078
1079 // Make sure that -E is not specified alongside linking (which includes SPV generation)
1080 // Or things that require linking
1081 if (Options & EOptionOutputPreprocessed) {
1082 if (Options & EOptionLinkProgram)
1083 Error("can't use -E when linking is selected");
1084 if (Options & EOptionDumpReflection)
1085 Error("reflection requires linking, which can't be used when -E when is selected");
1086 }
1087
1088 // reflection requires linking
1089 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
1090 Error("reflection requires -l for linking");
1091
1092 // link time optimization makes no sense unless linking
1093 if ((Options & EOptionLinkTimeOptimization) && !(Options & EOptionLinkProgram))
1094 Error("link time optimization requires -l for linking");
1095
1096 // -o or -x makes no sense if there is no target binary
1097 if (binaryFileName && (Options & EOptionSpv) == 0)
1098 Error("no binary generation requested (e.g., -V)");
1099
1100 if ((Options & EOptionFlattenUniformArrays) != 0 &&
1101 (Options & EOptionReadHlsl) == 0)
1102 Error("uniform array flattening only valid when compiling HLSL source.");
1103
1104 if ((Options & EOptionReadHlsl) && (Client == glslang::EShClientOpenGL)) {
1105 Error("Using HLSL input under OpenGL semantics is not currently supported.");
1106 }
1107
1108 // rationalize client and target language
1109 if (TargetLanguage == glslang::EShTargetNone) {
1110 switch (ClientVersion) {
1111 case glslang::EShTargetVulkan_1_0:
1112 TargetLanguage = glslang::EShTargetSpv;
1113 TargetVersion = glslang::EShTargetSpv_1_0;
1114 break;
1115 case glslang::EShTargetVulkan_1_1:
1116 TargetLanguage = glslang::EShTargetSpv;
1117 TargetVersion = glslang::EShTargetSpv_1_3;
1118 break;
1119 case glslang::EShTargetVulkan_1_2:
1120 TargetLanguage = glslang::EShTargetSpv;
1121 TargetVersion = glslang::EShTargetSpv_1_5;
1122 break;
1123 case glslang::EShTargetVulkan_1_3:
1124 TargetLanguage = glslang::EShTargetSpv;
1125 TargetVersion = glslang::EShTargetSpv_1_6;
1126 break;
1127 case glslang::EShTargetVulkan_1_4:
1128 TargetLanguage = glslang::EShTargetSpv;
1129 TargetVersion = glslang::EShTargetSpv_1_6;
1130 break;
1131 case glslang::EShTargetOpenGL_450:
1132 TargetLanguage = glslang::EShTargetSpv;
1133 TargetVersion = glslang::EShTargetSpv_1_0;
1134 break;
1135 default:
1136 break;
1137 }
1138 }
1139 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
1140 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
1141 }
1142
1143 //
1144 // Translate the meaningful subset of command-line options to parser-behavior options.
1145 //
SetMessageOptions(EShMessages & messages)1146 void SetMessageOptions(EShMessages& messages)
1147 {
1148 if (Options & EOptionRelaxedErrors)
1149 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
1150 if (Options & EOptionIntermediate)
1151 messages = (EShMessages)(messages | EShMsgAST);
1152 if (Options & EOptionSuppressWarnings)
1153 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
1154 if (Options & EOptionSpv)
1155 messages = (EShMessages)(messages | EShMsgSpvRules);
1156 if (Options & EOptionVulkanRules)
1157 messages = (EShMessages)(messages | EShMsgVulkanRules);
1158 if (Options & EOptionOutputPreprocessed)
1159 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
1160 if (Options & EOptionReadHlsl)
1161 messages = (EShMessages)(messages | EShMsgReadHlsl);
1162 if (Options & EOptionCascadingErrors)
1163 messages = (EShMessages)(messages | EShMsgCascadingErrors);
1164 if (Options & EOptionKeepUncalled)
1165 messages = (EShMessages)(messages | EShMsgKeepUncalled);
1166 if (Options & EOptionHlslOffsets)
1167 messages = (EShMessages)(messages | EShMsgHlslOffsets);
1168 if (Options & EOptionDebug)
1169 messages = (EShMessages)(messages | EShMsgDebugInfo);
1170 if (HlslEnable16BitTypes)
1171 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
1172 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
1173 messages = (EShMessages)(messages | EShMsgHlslLegalization);
1174 if (HlslDX9compatible)
1175 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
1176 if (DumpBuiltinSymbols)
1177 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
1178 if (EnhancedMsgs)
1179 messages = (EShMessages)(messages | EShMsgEnhanced);
1180 if (AbsolutePath)
1181 messages = (EShMessages)(messages | EShMsgAbsolutePath);
1182 if (Options & EOptionDisplayErrorColumn)
1183 messages = (EShMessages)(messages | EShMsgDisplayErrorColumn);
1184 if (Options & EOptionLinkTimeOptimization)
1185 messages = (EShMessages)(messages | EShMsgLinkTimeOptimization);
1186 }
1187
1188 //
1189 // Thread entry point, for non-linking asynchronous mode.
1190 //
CompileShaders(glslang::TWorklist & worklist)1191 void CompileShaders(glslang::TWorklist& worklist)
1192 {
1193 if (Options & EOptionDebug)
1194 Error("cannot generate debug information unless linking to generate code");
1195
1196 // NOTE: TWorkList::remove is thread-safe
1197 glslang::TWorkItem* workItem;
1198 if (Options & EOptionStdin) {
1199 if (worklist.remove(workItem)) {
1200 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), 0);
1201 if (compiler == nullptr)
1202 return;
1203
1204 CompileFile("stdin", compiler);
1205
1206 if (! (Options & EOptionSuppressInfolog))
1207 workItem->results = ShGetInfoLog(compiler);
1208
1209 ShDestruct(compiler);
1210 }
1211 } else {
1212 while (worklist.remove(workItem)) {
1213 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), 0);
1214 if (compiler == nullptr)
1215 return;
1216
1217
1218 CompileFile(workItem->name.c_str(), compiler);
1219
1220 if (! (Options & EOptionSuppressInfolog))
1221 workItem->results = ShGetInfoLog(compiler);
1222
1223 ShDestruct(compiler);
1224 }
1225 }
1226 }
1227
1228 // Outputs the given string, but only if it is non-null and non-empty.
1229 // This prevents erroneous newlines from appearing.
PutsIfNonEmpty(const char * str)1230 void PutsIfNonEmpty(const char* str)
1231 {
1232 if (str && str[0]) {
1233 puts(str);
1234 }
1235 }
1236
1237 // Outputs the given string to stderr, but only if it is non-null and non-empty.
1238 // This prevents erroneous newlines from appearing.
StderrIfNonEmpty(const char * str)1239 void StderrIfNonEmpty(const char* str)
1240 {
1241 if (str && str[0])
1242 fprintf(stderr, "%s\n", str);
1243 }
1244
1245 // Simple bundling of what makes a compilation unit for ease in passing around,
1246 // and separation of handling file IO versus API (programmatic) compilation.
1247 struct ShaderCompUnit {
1248 EShLanguage stage;
1249 static const int maxCount = 1;
1250 int count; // live number of strings/names
1251 const char* text[maxCount]; // memory owned/managed externally
1252 std::string fileName[maxCount]; // hold's the memory, but...
1253 const char* fileNameList[maxCount]; // downstream interface wants pointers
1254
ShaderCompUnitShaderCompUnit1255 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
1256
ShaderCompUnitShaderCompUnit1257 ShaderCompUnit(const ShaderCompUnit& rhs)
1258 {
1259 stage = rhs.stage;
1260 count = rhs.count;
1261 for (int i = 0; i < count; ++i) {
1262 fileName[i] = rhs.fileName[i];
1263 text[i] = rhs.text[i];
1264 fileNameList[i] = rhs.fileName[i].c_str();
1265 }
1266 }
1267
addStringShaderCompUnit1268 void addString(std::string& ifileName, const char* itext)
1269 {
1270 assert(count < maxCount);
1271 fileName[count] = ifileName;
1272 text[count] = itext;
1273 fileNameList[count] = fileName[count].c_str();
1274 ++count;
1275 }
1276 };
1277
1278 // Writes a string into a depfile, escaping some special characters following the Makefile rules.
writeEscapedDepString(std::ofstream & file,const std::string & str)1279 static void writeEscapedDepString(std::ofstream& file, const std::string& str)
1280 {
1281 for (char c : str) {
1282 switch (c) {
1283 case ' ':
1284 case ':':
1285 case '#':
1286 case '[':
1287 case ']':
1288 case '\\':
1289 file << '\\';
1290 break;
1291 case '$':
1292 file << '$';
1293 break;
1294 }
1295 file << c;
1296 }
1297 }
1298
1299 // Writes a depfile similar to gcc -MMD foo.c
writeDepFile(std::string depfile,std::vector<std::string> & binaryFiles,const std::vector<std::string> & sources)1300 bool writeDepFile(std::string depfile, std::vector<std::string>& binaryFiles, const std::vector<std::string>& sources)
1301 {
1302 std::ofstream file(depfile);
1303 if (file.fail())
1304 return false;
1305
1306 for (auto binaryFile = binaryFiles.begin(); binaryFile != binaryFiles.end(); binaryFile++) {
1307 writeEscapedDepString(file, *binaryFile);
1308 file << ":";
1309 for (auto sourceFile = sources.begin(); sourceFile != sources.end(); sourceFile++) {
1310 file << " ";
1311 writeEscapedDepString(file, *sourceFile);
1312 }
1313 file << std::endl;
1314 }
1315 return true;
1316 }
1317
1318 //
1319 // For linking mode: Will independently parse each compilation unit, but then put them
1320 // in the same program and link them together, making at most one linked module per
1321 // pipeline stage.
1322 //
1323 // Uses the new C++ interface instead of the old handle-based interface.
1324 //
1325
CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)1326 void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
1327 {
1328 // keep track of what to free
1329 std::list<glslang::TShader*> shaders;
1330
1331 EShMessages messages = EShMsgDefault;
1332 SetMessageOptions(messages);
1333
1334 DirStackFileIncluder includer;
1335 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1336 includer.pushExternalLocalDirectory(dir); });
1337
1338 std::vector<std::string> sources;
1339
1340 //
1341 // Per-shader processing...
1342 //
1343
1344 glslang::TProgram& program = *new glslang::TProgram;
1345 const bool compileOnly = (Options & EOptionCompileOnly) != 0;
1346 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1347 const auto &compUnit = *it;
1348 for (int i = 0; i < compUnit.count; i++) {
1349 sources.push_back(compUnit.fileNameList[i]);
1350 }
1351 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
1352 shader->setStringsWithLengthsAndNames(compUnit.text, nullptr, compUnit.fileNameList, compUnit.count);
1353 if (entryPointName)
1354 shader->setEntryPoint(entryPointName);
1355 if (sourceEntryPointName) {
1356 if (entryPointName == nullptr)
1357 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1358 "Use '-e <name>'.\n");
1359 shader->setSourceEntryPoint(sourceEntryPointName);
1360 }
1361
1362 if (compileOnly)
1363 shader->setCompileOnly();
1364
1365 shader->setOverrideVersion(GlslVersion);
1366
1367 std::string intrinsicString = getIntrinsic(compUnit.text, compUnit.count);
1368
1369 PreambleString = "";
1370 if (UserPreamble.isSet())
1371 PreambleString.append(UserPreamble.get());
1372
1373 if (!intrinsicString.empty())
1374 PreambleString.append(intrinsicString);
1375
1376 shader->setPreamble(PreambleString.c_str());
1377 shader->addProcesses(Processes);
1378
1379 // Set IO mapper binding shift values
1380 for (int r = 0; r < glslang::EResCount; ++r) {
1381 const glslang::TResourceType res = glslang::TResourceType(r);
1382
1383 // Set base bindings
1384 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
1385
1386 // Set bindings for particular resource sets
1387 // TODO: use a range based for loop here, when available in all environments.
1388 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1389 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
1390 shader->setShiftBindingForSet(res, i->second, i->first);
1391 }
1392 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
1393 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
1394
1395 if (autoSampledTextures)
1396 shader->setTextureSamplerTransformMode(EShTexSampTransUpgradeTextureRemoveSampler);
1397
1398 if (Options & EOptionAutoMapBindings)
1399 shader->setAutoMapBindings(true);
1400
1401 if (Options & EOptionAutoMapLocations)
1402 shader->setAutoMapLocations(true);
1403
1404 for (auto& uniOverride : uniformLocationOverrides) {
1405 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1406 uniOverride.second);
1407 }
1408
1409 shader->setUniformLocationBase(uniformBase);
1410
1411 if (VulkanRulesRelaxed) {
1412 for (auto& storageOverride : blockStorageOverrides) {
1413 shader->addBlockStorageOverride(storageOverride.first.c_str(),
1414 storageOverride.second);
1415 }
1416
1417 if (setGlobalBufferBlock) {
1418 shader->setAtomicCounterBlockName(atomicCounterBlockName.c_str());
1419 shader->setAtomicCounterBlockSet(atomicCounterBlockSet);
1420 }
1421
1422 if (setGlobalUniformBlock) {
1423 shader->setGlobalUniformBlockName(globalUniformName.c_str());
1424 shader->setGlobalUniformSet(globalUniformSet);
1425 shader->setGlobalUniformBinding(globalUniformBinding);
1426 }
1427 }
1428
1429 shader->setNanMinMaxClamp(NaNClamp);
1430
1431 #ifdef ENABLE_HLSL
1432 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1433 if (Options & EOptionHlslIoMapping)
1434 shader->setHlslIoMapping(true);
1435 #endif
1436
1437 if (Options & EOptionInvertY)
1438 shader->setInvertY(true);
1439
1440 if (HlslDxPositionW)
1441 shader->setDxPositionW(true);
1442
1443 if (EnhancedMsgs)
1444 shader->setEnhancedMsgs();
1445
1446 if (emitNonSemanticShaderDebugInfo)
1447 shader->setDebugInfo(true);
1448
1449 // Set up the environment, some subsettings take precedence over earlier
1450 // ways of setting things.
1451 if (Options & EOptionSpv) {
1452 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1453 : glslang::EShSourceGlsl,
1454 compUnit.stage, Client, ClientInputSemanticsVersion);
1455 shader->setEnvClient(Client, ClientVersion);
1456 shader->setEnvTarget(TargetLanguage, TargetVersion);
1457 #ifdef ENABLE_HLSL
1458 if (targetHlslFunctionality1)
1459 shader->setEnvTargetHlslFunctionality1();
1460 #endif
1461 if (VulkanRulesRelaxed)
1462 shader->setEnvInputVulkanRulesRelaxed();
1463 }
1464
1465 shaders.push_back(shader);
1466
1467 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
1468
1469 if (Options & EOptionOutputPreprocessed) {
1470 std::string str;
1471 if (shader->preprocess(GetResources(), defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
1472 PutsIfNonEmpty(str.c_str());
1473 } else {
1474 CompileFailed = 1;
1475 }
1476 StderrIfNonEmpty(shader->getInfoLog());
1477 StderrIfNonEmpty(shader->getInfoDebugLog());
1478 continue;
1479 }
1480
1481 if (! shader->parse(GetResources(), defaultVersion, false, messages, includer))
1482 CompileFailed = 1;
1483
1484 if (!compileOnly)
1485 program.addShader(shader);
1486
1487 if (! (Options & EOptionSuppressInfolog) &&
1488 ! (Options & EOptionMemoryLeakMode)) {
1489 if (!beQuiet)
1490 PutsIfNonEmpty(compUnit.fileName[0].c_str());
1491 PutsIfNonEmpty(shader->getInfoLog());
1492 PutsIfNonEmpty(shader->getInfoDebugLog());
1493 }
1494 }
1495
1496 //
1497 // Program-level processing...
1498 //
1499
1500 if (!compileOnly) {
1501 // Link
1502 if (!(Options & EOptionOutputPreprocessed) && !program.link(messages))
1503 LinkFailed = true;
1504
1505 // Map IO
1506 if (Options & EOptionSpv) {
1507 if (!program.mapIO())
1508 LinkFailed = true;
1509 }
1510
1511 // Report
1512 if (!(Options & EOptionSuppressInfolog) && !(Options & EOptionMemoryLeakMode)) {
1513 PutsIfNonEmpty(program.getInfoLog());
1514 PutsIfNonEmpty(program.getInfoDebugLog());
1515 }
1516
1517 // Reflect
1518 if (Options & EOptionDumpReflection) {
1519 program.buildReflection(ReflectOptions);
1520 program.dumpReflection();
1521 }
1522 }
1523
1524 std::vector<std::string> outputFiles;
1525
1526 // Dump SPIR-V
1527 if (Options & EOptionSpv) {
1528 #ifdef ENABLE_SPIRV
1529 CompileOrLinkFailed.fetch_or(CompileFailed);
1530 CompileOrLinkFailed.fetch_or(LinkFailed);
1531 if (static_cast<bool>(CompileOrLinkFailed.load()))
1532 printf("SPIR-V is not generated for failed compile or link\n");
1533 else {
1534 std::vector<glslang::TIntermediate*> intermediates;
1535 if (!compileOnly) {
1536 for (int stage = 0; stage < EShLangCount; ++stage) {
1537 if (auto* i = program.getIntermediate((EShLanguage)stage)) {
1538 intermediates.emplace_back(i);
1539 }
1540 }
1541 } else {
1542 for (const auto* shader : shaders) {
1543 if (auto* i = shader->getIntermediate()) {
1544 intermediates.emplace_back(i);
1545 }
1546 }
1547 }
1548 for (auto* intermediate : intermediates) {
1549 std::vector<unsigned int> spirv;
1550 spv::SpvBuildLogger logger;
1551 glslang::SpvOptions spvOptions;
1552 if (Options & EOptionDebug) {
1553 spvOptions.generateDebugInfo = true;
1554 if (emitNonSemanticShaderDebugInfo) {
1555 spvOptions.emitNonSemanticShaderDebugInfo = true;
1556 if (emitNonSemanticShaderDebugSource) {
1557 spvOptions.emitNonSemanticShaderDebugSource = true;
1558 }
1559 }
1560 } else if (stripDebugInfo)
1561 spvOptions.stripDebugInfo = true;
1562 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1563 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
1564 spvOptions.disassemble = SpvToolsDisassembler;
1565 spvOptions.validate = SpvToolsValidate;
1566 spvOptions.compileOnly = compileOnly;
1567 glslang::GlslangToSpv(*intermediate, spirv, &logger, &spvOptions);
1568
1569 // Dump the spv to a file or stdout, etc., but only if not doing
1570 // memory/perf testing, as it's not internal to programmatic use.
1571 if (!(Options & EOptionMemoryLeakMode)) {
1572 printf("%s", logger.getAllMessages().c_str());
1573 const auto filename = GetBinaryName(intermediate->getStage());
1574 if (Options & EOptionOutputHexadecimal) {
1575 if (!glslang::OutputSpvHex(spirv, filename, variableName))
1576 exit(EFailUsage);
1577 } else {
1578 if (!glslang::OutputSpvBin(spirv, filename))
1579 exit(EFailUsage);
1580 }
1581
1582 outputFiles.push_back(filename);
1583 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
1584 spv::Disassemble(std::cout, spirv);
1585 }
1586 }
1587 }
1588 #else
1589 Error("This configuration of glslang does not have SPIR-V support");
1590 #endif
1591 }
1592
1593 CompileOrLinkFailed.fetch_or(CompileFailed);
1594 CompileOrLinkFailed.fetch_or(LinkFailed);
1595 if (depencyFileName && !static_cast<bool>(CompileOrLinkFailed.load())) {
1596 std::set<std::string> includedFiles = includer.getIncludedFiles();
1597 sources.insert(sources.end(), includedFiles.begin(), includedFiles.end());
1598
1599 writeDepFile(depencyFileName, outputFiles, sources);
1600 }
1601
1602 // Free everything up, program has to go before the shaders
1603 // because it might have merged stuff from the shaders, and
1604 // the stuff from the shaders has to have its destructors called
1605 // before the pools holding the memory in the shaders is freed.
1606 delete &program;
1607 while (shaders.size() > 0) {
1608 delete shaders.back();
1609 shaders.pop_back();
1610 }
1611 }
1612
1613 //
1614 // Do file IO part of compile and link, handing off the pure
1615 // API/programmatic mode to CompileAndLinkShaderUnits(), which can
1616 // be put in a loop for testing memory footprint and performance.
1617 //
1618 // This is just for linking mode: meaning all the shaders will be put into the
1619 // the same program linked together.
1620 //
1621 // This means there are a limited number of work items (not multi-threading mode)
1622 // and that the point is testing at the linking level. Hence, to enable
1623 // performance and memory testing, the actual compile/link can be put in
1624 // a loop, independent of processing the work items and file IO.
1625 //
CompileAndLinkShaderFiles(glslang::TWorklist & Worklist)1626 void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
1627 {
1628 std::vector<ShaderCompUnit> compUnits;
1629
1630 // If this is using stdin, we can't really detect multiple different file
1631 // units by input type. We need to assume that we're just being given one
1632 // file of a certain type.
1633 if ((Options & EOptionStdin) != 0) {
1634 ShaderCompUnit compUnit(FindLanguage("stdin"));
1635 std::istreambuf_iterator<char> begin(std::cin), end;
1636 std::string tempString(begin, end);
1637 char* fileText = strdup(tempString.c_str());
1638 std::string fileName = "stdin";
1639 compUnit.addString(fileName, fileText);
1640 compUnits.push_back(compUnit);
1641 } else {
1642 // Transfer all the work items from to a simple list of
1643 // of compilation units. (We don't care about the thread
1644 // work-item distribution properties in this path, which
1645 // is okay due to the limited number of shaders, know since
1646 // they are all getting linked together.)
1647 glslang::TWorkItem* workItem;
1648 while (Worklist.remove(workItem)) {
1649 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1650 char* fileText = ReadFileData(workItem->name.c_str());
1651 if (fileText == nullptr)
1652 usage();
1653 compUnit.addString(workItem->name, fileText);
1654 compUnits.push_back(compUnit);
1655 }
1656 }
1657
1658 // Actual call to programmatic processing of compile and link,
1659 // in a loop for testing memory and performance. This part contains
1660 // all the perf/memory that a programmatic consumer will care about.
1661 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1662 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1663 CompileAndLinkShaderUnits(compUnits);
1664
1665 if (Options & EOptionMemoryLeakMode)
1666 glslang::OS_DumpMemoryCounters();
1667 }
1668
1669 // free memory from ReadFileData, which got stored in a const char*
1670 // as the first string above
1671 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
1672 FreeFileData(const_cast<char*>(it->text[0]));
1673 }
1674
singleMain()1675 int singleMain()
1676 {
1677 glslang::TWorklist workList;
1678 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
1679 assert(item);
1680 workList.add(item.get());
1681 });
1682
1683 if (Options & EOptionDumpConfig) {
1684 printf("%s", GetDefaultTBuiltInResourceString().c_str());
1685 if (workList.empty())
1686 return ESuccess;
1687 }
1688
1689 if (Options & EOptionDumpBareVersion) {
1690 int spirvGeneratorVersion = 0;
1691 #ifdef ENABLE_SPIRV
1692 spirvGeneratorVersion = glslang::GetSpirvGeneratorVersion();
1693 #endif
1694 printf("%d:%d.%d.%d%s\n", spirvGeneratorVersion, GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1695 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
1696 if (workList.empty())
1697 return ESuccess;
1698 } else if (Options & EOptionDumpVersions) {
1699 int spirvGeneratorVersion = 0;
1700 #ifdef ENABLE_SPIRV
1701 spirvGeneratorVersion = glslang::GetSpirvGeneratorVersion();
1702 #endif
1703 printf("Glslang Version: %d:%d.%d.%d%s\n", spirvGeneratorVersion, GLSLANG_VERSION_MAJOR,
1704 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
1705 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1706 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
1707 std::string spirvVersion;
1708 #if ENABLE_SPIRV
1709 glslang::GetSpirvVersion(spirvVersion);
1710 #endif
1711 printf("SPIR-V Version %s\n", spirvVersion.c_str());
1712 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
1713 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
1714 printf("SPIR-V Generator Version %d\n", spirvGeneratorVersion);
1715 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1716 printf("ARB_GL_gl_spirv version %d\n", 100);
1717 if (workList.empty())
1718 return ESuccess;
1719 }
1720
1721 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
1722 usage();
1723 }
1724
1725 if (Options & EOptionStdin) {
1726 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1727 workList.add(WorkItems.back().get());
1728 }
1729
1730 ProcessConfigFile();
1731
1732 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
1733 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
1734
1735 //
1736 // Two modes:
1737 // 1) linking all arguments together, single-threaded, new C++ interface
1738 // 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface
1739 //
1740 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
1741 glslang::InitializeProcess();
1742 glslang::InitializeProcess(); // also test reference counting of users
1743 glslang::InitializeProcess(); // also test reference counting of users
1744 glslang::FinalizeProcess(); // also test reference counting of users
1745 glslang::FinalizeProcess(); // also test reference counting of users
1746 CompileAndLinkShaderFiles(workList);
1747 glslang::FinalizeProcess();
1748 } else {
1749 ShInitialize();
1750 ShInitialize(); // also test reference counting of users
1751 ShFinalize(); // also test reference counting of users
1752
1753 bool printShaderNames = workList.size() > 1;
1754
1755 if (Options & EOptionMultiThreaded) {
1756 std::array<std::thread, 16> threads;
1757 for (unsigned int t = 0; t < threads.size(); ++t) {
1758 threads[t] = std::thread(CompileShaders, std::ref(workList));
1759 if (threads[t].get_id() == std::thread::id()) {
1760 fprintf(stderr, "Failed to create thread\n");
1761 return EFailThreadCreate;
1762 }
1763 }
1764
1765 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
1766 } else
1767 CompileShaders(workList);
1768
1769 // Print out all the resulting infologs
1770 for (size_t w = 0; w < WorkItems.size(); ++w) {
1771 if (WorkItems[w]) {
1772 if (printShaderNames || WorkItems[w]->results.size() > 0)
1773 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1774 PutsIfNonEmpty(WorkItems[w]->results.c_str());
1775 }
1776 }
1777
1778 ShFinalize();
1779 }
1780
1781 if (CompileFailed.load())
1782 return EFailCompile;
1783 if (LinkFailed.load())
1784 return EFailLink;
1785
1786 return 0;
1787 }
1788
main(int argc,char * argv[])1789 int C_DECL main(int argc, char* argv[])
1790 {
1791 ProcessArguments(WorkItems, argc, argv);
1792
1793 int ret = 0;
1794
1795 // Loop over the entire init/finalize cycle to watch memory changes
1796 const int iterations = 1;
1797 if (iterations > 1)
1798 glslang::OS_DumpMemoryCounters();
1799 for (int i = 0; i < iterations; ++i) {
1800 ret = singleMain();
1801 if (iterations > 1)
1802 glslang::OS_DumpMemoryCounters();
1803 }
1804
1805 return ret;
1806 }
1807
1808 //
1809 // Deduce the language from the filename. Files must end in one of the
1810 // following extensions:
1811 //
1812 // .vert = vertex
1813 // .tesc = tessellation control
1814 // .tese = tessellation evaluation
1815 // .geom = geometry
1816 // .frag = fragment
1817 // .comp = compute
1818 // .rgen = ray generation
1819 // .rint = ray intersection
1820 // .rahit = ray any hit
1821 // .rchit = ray closest hit
1822 // .rmiss = ray miss
1823 // .rcall = ray callable
1824 // .mesh = mesh
1825 // .task = task
1826 // Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1827 // where <stage> is one of the stages listed above.
1828 //
FindLanguage(const std::string & name,bool parseStageName)1829 EShLanguage FindLanguage(const std::string& name, bool parseStageName)
1830 {
1831 std::string stageName;
1832 if (shaderStageName)
1833 stageName = shaderStageName;
1834 else if (parseStageName) {
1835 // Note: "first" extension means "first from the end", i.e.
1836 // if the file is named foo.vert.glsl, then "glsl" is first,
1837 // "vert" is second.
1838 size_t firstExtStart = name.find_last_of(".");
1839 bool hasFirstExt = firstExtStart != std::string::npos;
1840 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1841 bool hasSecondExt = secondExtStart != std::string::npos;
1842 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1843 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
1844 if (usesUnifiedExt && firstExt == "hlsl")
1845 Options |= EOptionReadHlsl;
1846 if (hasFirstExt && !usesUnifiedExt)
1847 stageName = firstExt;
1848 else if (usesUnifiedExt && hasSecondExt)
1849 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
1850 else {
1851 usage();
1852 return EShLangVertex;
1853 }
1854 } else
1855 stageName = name;
1856
1857 if (stageName == "vert")
1858 return EShLangVertex;
1859 else if (stageName == "tesc")
1860 return EShLangTessControl;
1861 else if (stageName == "tese")
1862 return EShLangTessEvaluation;
1863 else if (stageName == "geom")
1864 return EShLangGeometry;
1865 else if (stageName == "frag")
1866 return EShLangFragment;
1867 else if (stageName == "comp")
1868 return EShLangCompute;
1869 else if (stageName == "rgen")
1870 return EShLangRayGen;
1871 else if (stageName == "rint")
1872 return EShLangIntersect;
1873 else if (stageName == "rahit")
1874 return EShLangAnyHit;
1875 else if (stageName == "rchit")
1876 return EShLangClosestHit;
1877 else if (stageName == "rmiss")
1878 return EShLangMiss;
1879 else if (stageName == "rcall")
1880 return EShLangCallable;
1881 else if (stageName == "mesh")
1882 return EShLangMesh;
1883 else if (stageName == "task")
1884 return EShLangTask;
1885
1886 usage();
1887 return EShLangVertex;
1888 }
1889
1890 //
1891 // Read a file's data into a string, and compile it using the old interface ShCompile,
1892 // for non-linkable results.
1893 //
CompileFile(const char * fileName,ShHandle compiler)1894 void CompileFile(const char* fileName, ShHandle compiler)
1895 {
1896 int ret = 0;
1897 char* shaderString;
1898 if ((Options & EOptionStdin) != 0) {
1899 std::istreambuf_iterator<char> begin(std::cin), end;
1900 std::string tempString(begin, end);
1901 shaderString = strdup(tempString.c_str());
1902 } else {
1903 shaderString = ReadFileData(fileName);
1904 }
1905
1906 // move to length-based strings, rather than null-terminated strings
1907 int* lengths = new int[1];
1908 lengths[0] = (int)strlen(shaderString);
1909
1910 EShMessages messages = EShMsgDefault;
1911 SetMessageOptions(messages);
1912
1913 if (UserPreamble.isSet())
1914 Error("-D, -U and -P options require -l (linking)\n");
1915
1916 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1917 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
1918 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
1919 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, GetResources(), 0,
1920 (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages, fileName);
1921 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
1922 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
1923 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
1924 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1925 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
1926 }
1927
1928 if (Options & EOptionMemoryLeakMode)
1929 glslang::OS_DumpMemoryCounters();
1930 }
1931
1932 delete [] lengths;
1933 FreeFileData(shaderString);
1934
1935 if (ret == 0)
1936 CompileFailed = true;
1937 }
1938
1939 //
1940 // print usage to stdout
1941 //
usage()1942 void usage()
1943 {
1944 printf("Usage: glslang [option]... [file]...\n"
1945 "\n"
1946 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1947 " .conf to provide a config file that replaces the default configuration\n"
1948 " (see -c option below for generating a template)\n"
1949 " .vert for a vertex shader\n"
1950 " .tesc for a tessellation control shader\n"
1951 " .tese for a tessellation evaluation shader\n"
1952 " .geom for a geometry shader\n"
1953 " .frag for a fragment shader\n"
1954 " .comp for a compute shader\n"
1955 " .mesh for a mesh shader\n"
1956 " .task for a task shader\n"
1957 " .rgen for a ray generation shader\n"
1958 " .rint for a ray intersection shader\n"
1959 " .rahit for a ray any hit shader\n"
1960 " .rchit for a ray closest hit shader\n"
1961 " .rmiss for a ray miss shader\n"
1962 " .rcall for a ray callable shader\n"
1963 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1964 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
1965 "\n"
1966 "Options:\n"
1967 " -C cascading errors; risk crash from accumulation of error recoveries\n"
1968 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
1969 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1970 " define a pre-processor macro\n"
1971 " -E print pre-processed GLSL; cannot be used with -l;\n"
1972 " errors will appear on stderr\n"
1973 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1974 " default file name is <stage>.spv (-o overrides this);\n"
1975 " 'ver', when present, is the version of the input semantics,\n"
1976 " which will appear in #define GL_SPIRV ver;\n"
1977 " '--client opengl100' is the same as -G100;\n"
1978 " a '--target-env' for OpenGL will also imply '-G';\n"
1979 " currently only supports GLSL\n"
1980 " -H print human readable form of SPIR-V; turns on -V\n"
1981 " -I<dir> add dir to the include search path; includer's directory\n"
1982 " is searched first, followed by left-to-right order of -I\n"
1983 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1984 " -Os optimizes SPIR-V to minimize size\n"
1985 " -P<text> | --preamble-text <text> | --P <text>\n"
1986 " inject custom preamble text, which is treated as if it\n"
1987 " appeared immediately after the version declaration (if any).\n"
1988 " -R use relaxed verification rules for generating Vulkan SPIR-V,\n"
1989 " allowing the use of default uniforms, atomic_uints, and\n"
1990 " gl_VertexID and gl_InstanceID keywords.\n"
1991 " -S <stage> uses specified stage rather than parsing the file extension\n"
1992 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
1993 " -U<name> | --undef-macro <name> | --U <name>\n"
1994 " undefine a pre-processor macro\n"
1995 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1996 " default file name is <stage>.spv (-o overrides this)\n"
1997 " 'ver', when present, is the version of the input semantics,\n"
1998 " which will appear in #define VULKAN ver\n"
1999 " '--client vulkan100' is the same as -V100\n"
2000 " a '--target-env' for Vulkan will also imply '-V'\n"
2001 " -c configuration dump;\n"
2002 " creates the default configuration file (redirect to a .conf file)\n"
2003 " -d default to desktop (#version 110) when there is no shader #version\n"
2004 " (default is ES version 100)\n"
2005 " -e <name> | --entry-point <name>\n"
2006 " specify <name> as the entry-point function name\n"
2007 " -f{hlsl_functionality1}\n"
2008 " 'hlsl_functionality1' enables use of the\n"
2009 " SPV_GOOGLE_hlsl_functionality1 extension\n"
2010 " -g generate debug information\n"
2011 " -g0 strip debug information\n"
2012 " -gV generate nonsemantic shader debug information\n"
2013 " -gVS generate nonsemantic shader debug information with source\n"
2014 " -h print this usage message\n"
2015 " -i intermediate tree (glslang AST) is printed out\n"
2016 " -l link all input files together to form a single module\n"
2017 " -m memory leak mode\n"
2018 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
2019 " -q dump reflection query database; requires -l for linking\n"
2020 " -r | --relaxed-errors"
2021 " relaxed GLSL semantic error-checking mode\n"
2022 " -s silence syntax and semantic error reporting\n"
2023 " -t multi-threaded mode\n"
2024 " -v | --version\n"
2025 " print version strings\n"
2026 " -w | --suppress-warnings\n"
2027 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
2028 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
2029 " -u<name>:<loc> specify a uniform location override for --aml\n"
2030 " --uniform-base <base> set a base to use for generated uniform locations\n"
2031 " --auto-map-bindings | --amb automatically bind uniform variables\n"
2032 " without explicit bindings\n"
2033 " --auto-map-locations | --aml automatically locate input/output lacking\n"
2034 " 'location' (fragile, not cross stage)\n"
2035 " --absolute-path Prints absolute path for messages\n"
2036 " --auto-sampled-textures Removes sampler variables and converts\n"
2037 " existing textures to sampled textures\n"
2038 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
2039 " --depfile <file> writes depfile for build systems\n"
2040 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
2041 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
2042 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
2043 " scalars\n"
2044 " --glsl-version {100 | 110 | 120 | 130 | 140 | 150 |\n"
2045 " 300es | 310es | 320es | 330\n"
2046 " 400 | 410 | 420 | 430 | 440 | 450 | 460}\n"
2047 " set GLSL version, overrides #version\n"
2048 " in shader sourcen\n"
2049 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
2050 " works independently of source language\n"
2051 " --hlsl-iomap perform IO mapping in HLSL register space\n"
2052 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
2053 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
2054 " texture/sampler combo like DirectX9 would,\n"
2055 " and recognizes DirectX9-specific semantics\n"
2056 " --hlsl-dx-position-w W component of SV_Position in HLSL fragment\n"
2057 " shaders compatible with DirectX\n"
2058 " --invert-y | --iy invert position.Y output in vertex shader\n"
2059 " --enhanced-msgs print more readable error messages (GLSL only)\n"
2060 " --error-column display the column of the error along the line\n"
2061 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
2062 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
2063 " --no-storage-format | --nsf use Unknown image format\n"
2064 " --quiet do not print anything to stdout, unless\n"
2065 " requested by another option\n"
2066 " --reflect-strict-array-suffix use strict array suffix rules when\n"
2067 " reflecting\n"
2068 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
2069 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
2070 " shaders rather than just vertex/fragment\n"
2071 " --reflect-separate-buffers reflect buffer variables and blocks\n"
2072 " separately to uniforms\n"
2073 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
2074 " inactive or active\n"
2075 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
2076 " uniform blocks\n"
2077 " --resource-set-binding [stage] name set binding\n"
2078 " set descriptor set and binding for\n"
2079 " individual resources\n"
2080 " --resource-set-binding [stage] set\n"
2081 " set descriptor set for all resources\n"
2082 " --rsb synonym for --resource-set-binding\n"
2083 " --set-block-backing name {uniform|buffer|push_constant}\n"
2084 " changes the backing type of a uniform, buffer,\n"
2085 " or push_constant block declared in\n"
2086 " in the program, when using -R option.\n"
2087 " This can be used to change the backing\n"
2088 " for existing blocks as well as implicit ones\n"
2089 " such as 'gl_DefaultUniformBlock'.\n"
2090 " --sbs synonym for set-block-storage\n"
2091 " --set-atomic-counter-block name set\n"
2092 " set name, and descriptor set for\n"
2093 " atomic counter blocks, with -R opt\n"
2094 " --sacb synonym for set-atomic-counter-block\n"
2095 " --set-default-uniform-block name set binding\n"
2096 " set name, descriptor set, and binding for\n"
2097 " global default-uniform-block, with -R opt\n"
2098 " --sdub synonym for set-default-uniform-block\n"
2099 " --shift-image-binding [stage] num\n"
2100 " base binding number for images (uav)\n"
2101 " --shift-image-binding [stage] [num set]...\n"
2102 " per-descriptor-set shift values\n"
2103 " --sib synonym for --shift-image-binding\n"
2104 " --shift-sampler-binding [stage] num\n"
2105 " base binding number for samplers\n"
2106 " --shift-sampler-binding [stage] [num set]...\n"
2107 " per-descriptor-set shift values\n"
2108 " --ssb synonym for --shift-sampler-binding\n"
2109 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
2110 " --shift-ssbo-binding [stage] [num set]...\n"
2111 " per-descriptor-set shift values\n"
2112 " --sbb synonym for --shift-ssbo-binding\n"
2113 " --shift-texture-binding [stage] num\n"
2114 " base binding number for textures\n"
2115 " --shift-texture-binding [stage] [num set]...\n"
2116 " per-descriptor-set shift values\n"
2117 " --stb synonym for --shift-texture-binding\n"
2118 " --shift-uav-binding [stage] num base binding number for UAVs\n"
2119 " --shift-uav-binding [stage] [num set]...\n"
2120 " per-descriptor-set shift values\n"
2121 " --suavb synonym for --shift-uav-binding\n"
2122 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
2123 " --shift-UBO-binding [stage] [num set]...\n"
2124 " per-descriptor-set shift values\n"
2125 " --sub synonym for --shift-UBO-binding\n"
2126 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
2127 " --spirv-dis output standard-form disassembly; works only\n"
2128 " when a SPIR-V generation option is also used\n"
2129 " --spirv-val execute the SPIRV-Tools validator\n"
2130 " --source-entrypoint <name> the given shader source function is\n"
2131 " renamed to be the <name> given in -e\n"
2132 " --sep synonym for --source-entrypoint\n"
2133 " --stdin read from stdin instead of from a file;\n"
2134 " requires providing the shader stage using -S\n"
2135 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | vulkan1.3 | opengl |\n"
2136 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 |\n"
2137 " spirv1.5 | spirv1.6}\n"
2138 " Set the execution environment that the\n"
2139 " generated code will be executed in.\n"
2140 " Defaults to:\n"
2141 " * vulkan1.0 under --client vulkan<ver>\n"
2142 " * opengl under --client opengl<ver>\n"
2143 " * spirv1.0 under --target-env vulkan1.0\n"
2144 " * spirv1.3 under --target-env vulkan1.1\n"
2145 " * spirv1.5 under --target-env vulkan1.2\n"
2146 " * spirv1.6 under --target-env vulkan1.3\n"
2147 " Multiple --target-env can be specified.\n"
2148 " --variable-name <name>\n"
2149 " --vn <name> creates a C header file that contains a\n"
2150 " uint32_t array named <name>\n"
2151 " initialized with the shader binary code\n"
2152 " --no-link Only compile shader; do not link (GLSL-only)\n"
2153 " NOTE: this option will set the export linkage\n"
2154 " attribute on all functions\n"
2155 " --lto perform link time optimization\n");
2156
2157 exit(EFailUsage);
2158 }
2159
2160 #if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
2161
2162 #include <errno.h>
2163
fopen_s(FILE ** pFile,const char * filename,const char * mode)2164 int fopen_s(
2165 FILE** pFile,
2166 const char* filename,
2167 const char* mode
2168 )
2169 {
2170 if (!pFile || !filename || !mode) {
2171 return EINVAL;
2172 }
2173
2174 FILE* f = fopen(filename, mode);
2175 if (! f) {
2176 if (errno != 0) {
2177 return errno;
2178 } else {
2179 return ENOENT;
2180 }
2181 }
2182 *pFile = f;
2183
2184 return 0;
2185 }
2186
2187 #endif
2188
2189 //
2190 // Malloc a string of sufficient size and read a string into it.
2191 //
ReadFileData(const char * fileName)2192 char* ReadFileData(const char* fileName)
2193 {
2194 FILE *in = nullptr;
2195 int errorCode = fopen_s(&in, fileName, "r");
2196 if (errorCode || in == nullptr)
2197 Error("unable to open input file");
2198
2199 int count = 0;
2200 while (fgetc(in) != EOF)
2201 count++;
2202
2203 fseek(in, 0, SEEK_SET);
2204
2205 if (count > 3) {
2206 unsigned char head[3];
2207 if (fread(head, 1, 3, in) == 3) {
2208 if (head[0] == 0xef && head[1] == 0xbb && head[2] == 0xbf) {
2209 // skip BOM
2210 count -= 3;
2211 } else {
2212 fseek(in, 0, SEEK_SET);
2213 }
2214 } else {
2215 Error("can't read input file");
2216 }
2217 }
2218
2219 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
2220 if ((int)fread(return_data, 1, count, in) != count) {
2221 free(return_data);
2222 Error("can't read input file");
2223 }
2224
2225 return_data[count] = '\0';
2226 fclose(in);
2227
2228 return return_data;
2229 }
2230
FreeFileData(char * data)2231 void FreeFileData(char* data)
2232 {
2233 free(data);
2234 }
2235
InfoLogMsg(const char * msg,const char * name,const int num)2236 void InfoLogMsg(const char* msg, const char* name, const int num)
2237 {
2238 if (num >= 0 )
2239 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
2240 else
2241 printf("#### %s %s INFO LOG ####\n", msg, name);
2242 }
2243