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