blob: 662b68d19f23a38e8e31b47c348eca7d8dcb9199 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenich56364b62020-03-01 04:51:40 -07004// Copyright (C) 2016-2020 Google, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00005//
John Kessenich927608b2017-01-06 12:34:14 -07006// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00007//
John Kessenich927608b2017-01-06 12:34:14 -07008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions
10// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000011//
12// Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following
17// disclaimer in the documentation and/or other materials provided
18// with the distribution.
19//
20// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21// contributors may be used to endorse or promote products derived
22// from this software without specific prior written permission.
23//
John Kessenich927608b2017-01-06 12:34:14 -070024// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000036//
John Kessenich05a70632013-09-17 19:26:08 +000037
38// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030039#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000040#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030041#endif
John Kessenich05a70632013-09-17 19:26:08 +000042
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040043#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000044#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060045#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000046#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060047#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000048#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000049#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060050#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000051#include "../SPIRV/doc.h"
52#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060053
John Kessenich66ec80e2016-08-05 14:04:23 -060054#include <cstring>
55#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060056#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060057#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060058#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060059#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020060#include <memory>
61#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000062
baldurk876a0e32015-11-16 18:03:28 +010063#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000064
65extern "C" {
Ben Claytond64e8592020-06-29 14:20:19 +010066 GLSLANG_EXPORT void ShOutputHtml();
John Kessenicha0af4732012-12-12 21:15:54 +000067}
68
John Kessenich94a81fb2013-08-31 02:41:30 +000069// Command-line options
70enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070071 EOptionNone = 0,
72 EOptionIntermediate = (1 << 0),
73 EOptionSuppressInfolog = (1 << 1),
74 EOptionMemoryLeakMode = (1 << 2),
75 EOptionRelaxedErrors = (1 << 3),
76 EOptionGiveWarnings = (1 << 4),
77 EOptionLinkProgram = (1 << 5),
78 EOptionMultiThreaded = (1 << 6),
79 EOptionDumpConfig = (1 << 7),
80 EOptionDumpReflection = (1 << 8),
81 EOptionSuppressWarnings = (1 << 9),
82 EOptionDumpVersions = (1 << 10),
83 EOptionSpv = (1 << 11),
84 EOptionHumanReadableSpv = (1 << 12),
85 EOptionVulkanRules = (1 << 13),
86 EOptionDefaultDesktop = (1 << 14),
87 EOptionOutputPreprocessed = (1 << 15),
88 EOptionOutputHexadecimal = (1 << 16),
89 EOptionReadHlsl = (1 << 17),
90 EOptionCascadingErrors = (1 << 18),
91 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060092 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070093 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070094 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060095 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060096 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060097 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060098 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +020099 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -0600100 EOptionOptimizeDisable = (1 << 28),
101 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700102 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700103 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000104};
John Kessenich5d610ee2018-03-07 18:05:55 -0700105bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600106bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600107bool SpvToolsValidate = false;
John Kessenich605afc72019-06-17 23:33:09 -0600108bool NaNClamp = false;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400109bool stripDebugInfo = false;
John Kessenich94a81fb2013-08-31 02:41:30 +0000110
John Kessenicha0af4732012-12-12 21:15:54 +0000111//
John Kessenich68d78fd2015-07-12 19:28:10 -0600112// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000113//
114enum TFailCode {
115 ESuccess = 0,
116 EFailUsage,
117 EFailCompile,
118 EFailLink,
119 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000120 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000121 EFailLinkerCreate
122};
123
124//
John Kessenich68d78fd2015-07-12 19:28:10 -0600125// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000126//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600127EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000128void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000129void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600130char* ReadFileData(const char* fileName);
131void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000132void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000133
John Kessenichc999ba22013-11-07 23:33:24 +0000134// Globally track if any compile or link failure.
135bool CompileFailed = false;
136bool LinkFailed = false;
137
John Kessenich94f28eb2017-11-13 01:32:06 -0700138// array of unique places to leave the shader names and infologs for the asynchronous compiles
139std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
140
John Kessenich05a70632013-09-17 19:26:08 +0000141TBuiltInResource Resources;
142std::string ConfigFile;
143
John Kessenicha0af4732012-12-12 21:15:54 +0000144//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600145// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000146//
147void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000148{
John Kessenich04acb1b2017-06-14 17:36:50 -0600149 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500150 Resources = glslang::DefaultTBuiltInResource;
John Kessenich23d27752019-07-28 02:12:10 -0600151#ifndef GLSLANG_WEB
John Kessenich04acb1b2017-06-14 17:36:50 -0600152 else {
153 char* configString = ReadFileData(ConfigFile.c_str());
154 glslang::DecodeResourceLimits(&Resources, configString);
155 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000156 }
John Kessenich23d27752019-07-28 02:12:10 -0600157#endif
John Kessenicha0af4732012-12-12 21:15:54 +0000158}
159
baldurk6d477852019-01-29 15:45:56 +0000160int ReflectOptions = EShReflectionDefault;
John Kessenich94a81fb2013-08-31 02:41:30 +0000161int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600162const char* ExecutableName = nullptr;
163const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700164const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600165const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400166const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800167const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800168bool HlslEnable16BitTypes = false;
John Kessenichbd1c1832018-12-07 18:38:26 -0700169bool HlslDX9compatible = false;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200170bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600171std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600172
173// Source environment
174// (source 'Client' is currently the same as target 'Client')
175int ClientInputSemanticsVersion = 100;
176
177// Target environment
178glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
179glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
180glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
181glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
182
John Kessenich66011cb2018-03-06 16:12:04 -0700183std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600184
LoopDawg08a14422017-10-17 19:27:14 -0600185// Per descriptor-set binding base data
186typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
187
Neil Roberts16f53472018-03-20 17:30:53 +0100188std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100189int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100190
LoopDawg08a14422017-10-17 19:27:14 -0600191std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
192std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900193std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600194
John Kessenicha9313662017-06-15 10:40:49 -0600195// Add things like "#define ..." to a preamble to use in the beginning of the shader.
196class TPreamble {
197public:
198 TPreamble() { }
199
200 bool isSet() const { return text.size() > 0; }
201 const char* get() const { return text.c_str(); }
202
203 // #define...
204 void addDef(std::string def)
205 {
206 text.append("#define ");
207 fixLine(def);
208
John Kessenich6f988922020-01-07 07:03:11 -0700209 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600210 Processes.back().append(def);
211
John Kessenicha9313662017-06-15 10:40:49 -0600212 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600213 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600214 if (equal != def.npos)
215 def[equal] = ' ';
216
217 text.append(def);
218 text.append("\n");
219 }
220
221 // #undef...
222 void addUndef(std::string undef)
223 {
224 text.append("#undef ");
225 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600226
John Kessenich6f988922020-01-07 07:03:11 -0700227 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600228 Processes.back().append(undef);
229
John Kessenicha9313662017-06-15 10:40:49 -0600230 text.append(undef);
231 text.append("\n");
232 }
233
234protected:
235 void fixLine(std::string& line)
236 {
237 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600238 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600239 if (end != line.npos)
240 line = line.substr(0, end);
241 }
242
243 std::string text; // contents of preamble
244};
245
John Kessenich83855b92020-03-30 00:27:31 -0600246// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600247TPreamble UserPreamble;
248
John Kessenich68d78fd2015-07-12 19:28:10 -0600249//
250// Create the default name for saving a binary if -o is not provided.
251//
252const char* GetBinaryName(EShLanguage stage)
253{
254 const char* name;
255 if (binaryFileName == nullptr) {
256 switch (stage) {
257 case EShLangVertex: name = "vert.spv"; break;
258 case EShLangTessControl: name = "tesc.spv"; break;
259 case EShLangTessEvaluation: name = "tese.spv"; break;
260 case EShLangGeometry: name = "geom.spv"; break;
261 case EShLangFragment: name = "frag.spv"; break;
262 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400263 case EShLangRayGen: name = "rgen.spv"; break;
264 case EShLangIntersect: name = "rint.spv"; break;
265 case EShLangAnyHit: name = "rahit.spv"; break;
266 case EShLangClosestHit: name = "rchit.spv"; break;
267 case EShLangMiss: name = "rmiss.spv"; break;
268 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700269 case EShLangMeshNV: name = "mesh.spv"; break;
270 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600271 default: name = "unknown"; break;
272 }
273 } else
274 name = binaryFileName;
275
276 return name;
277}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000278
John Kessenich05a70632013-09-17 19:26:08 +0000279//
280// *.conf => this is a config file that can set limits/resources
281//
282bool SetConfigFile(const std::string& name)
283{
284 if (name.size() < 5)
285 return false;
286
John Kessenich4c706852013-10-11 16:28:43 +0000287 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000288 ConfigFile = name;
289 return true;
290 }
291
292 return false;
293}
294
John Kessenich68d78fd2015-07-12 19:28:10 -0600295//
296// Give error and exit with failure code.
297//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700298void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600299{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700300 fprintf(stderr, "%s: Error: ", ExecutableName);
301 if (detail != nullptr)
302 fprintf(stderr, "%s: ", detail);
303 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600304 exit(EFailUsage);
305}
306
307//
LoopDawg08a14422017-10-17 19:27:14 -0600308// Process an optional binding base of one the forms:
309// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600310// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600311
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600312// Where stage is one of the forms accepted by FindLanguage, and base is an integer
313//
LoopDawg08a14422017-10-17 19:27:14 -0600314void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600315{
316 if (argc < 2)
317 usage();
318
LoopDawg08a14422017-10-17 19:27:14 -0600319 EShLanguage lang = EShLangCount;
320 int singleBase = 0;
321 TPerSetBaseBinding perSetBase;
322 int arg = 1;
323
324 // Parse stage, if given
325 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600326 if (argc < 3) // this form needs one more argument
327 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700328
LoopDawg08a14422017-10-17 19:27:14 -0600329 lang = FindLanguage(argv[arg++], false);
330 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600331
LoopDawg08a14422017-10-17 19:27:14 -0600332 if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
333 // Parse a per-set binding base
334 while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600335 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600336 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600337 perSetBase[setNum] = baseNum;
338 }
339 } else {
340 // Parse single binding base
341 singleBase = atoi(argv[arg++]);
342 }
343
344 argc -= (arg-1);
345 argv += (arg-1);
346
347 // Set one or all languages
348 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
349 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
350
351 for (int lang = langMin; lang < langMax; ++lang) {
352 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600353 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600354 else
355 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600356 }
357}
358
Hyangran Park36dc8292017-05-02 16:27:29 +0900359void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
360{
361 if (argc < 2)
362 usage();
363
364 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600365 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900366 usage();
367
LoopDawg52017192017-07-14 15:15:47 -0600368 // Parse form: --argname stage [regname set base...], or:
369 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900370 const EShLanguage lang = FindLanguage(argv[1], false);
371
LoopDawg52017192017-07-14 15:15:47 -0600372 argc--;
373 argv++;
374
375 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
376 base[lang].push_back(argv[1]);
377
378 argc--;
379 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900380 }
LoopDawg52017192017-07-14 15:15:47 -0600381
382 // Must have one arg, or a multiple of three (for [regname set binding] triples)
383 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
384 usage();
385
Hyangran Park36dc8292017-05-02 16:27:29 +0900386 } else {
LoopDawg52017192017-07-14 15:15:47 -0600387 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900388 for (int lang=0; lang<EShLangCount; ++lang)
389 base[lang].push_back(argv[1]);
390
391 argc--;
392 argv++;
393 }
394}
395
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600396//
John Kessenich68d78fd2015-07-12 19:28:10 -0600397// Do all command-line argument parsing. This includes building up the work-items
398// to be processed later, and saving all the command-line options.
399//
400// Does not return (it exits) if command-line is fatally flawed.
401//
Juan Lopeza558b262017-04-02 23:04:00 +0200402void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000403{
LoopDawg08a14422017-10-17 19:27:14 -0600404 for (int res = 0; res < glslang::EResCount; ++res)
405 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600406
John Kessenich38f3b892013-09-06 19:52:57 +0000407 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200408 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000409
John Kessenichc178f0a2017-06-23 10:58:31 -0600410 const auto bumpArg = [&]() {
411 if (argc > 0) {
412 argc--;
413 argv++;
414 }
415 };
416
417 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600418 const auto getStringOperand = [&](const char* desc) {
419 if (argv[0][2] == 0) {
420 printf("%s must immediately follow option (no spaces)\n", desc);
421 exit(EFailUsage);
422 }
423 return argv[0] + 2;
424 };
425
John Kessenich6353d552017-06-23 11:11:09 -0600426 // read a number attached to a single-letter option
427 const auto getAttachedNumber = [&](const char* desc) {
428 int num = atoi(argv[0] + 2);
429 if (num == 0) {
430 printf("%s: expected attached non-0 number\n", desc);
431 exit(EFailUsage);
432 }
433 return num;
434 };
435
436 // minimum needed (without overriding something else) to target Vulkan SPIR-V
437 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600438 if (Client == glslang::EShClientNone)
439 ClientVersion = glslang::EShTargetVulkan_1_0;
440 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600441 Options |= EOptionSpv;
442 Options |= EOptionVulkanRules;
443 Options |= EOptionLinkProgram;
444 };
445
446 // minimum needed (without overriding something else) to target OpenGL SPIR-V
447 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600448 if (Client == glslang::EShClientNone)
449 ClientVersion = glslang::EShTargetOpenGL_450;
450 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600451 Options |= EOptionSpv;
452 Options |= EOptionLinkProgram;
453 // undo a -H default to Vulkan
454 Options &= ~EOptionVulkanRules;
455 };
456
Neil Roberts16f53472018-03-20 17:30:53 +0100457 const auto getUniformOverride = [getStringOperand]() {
458 const char *arg = getStringOperand("-u<name>:<location>");
459 const char *split = strchr(arg, ':');
460 if (split == NULL) {
461 printf("%s: missing location\n", arg);
462 exit(EFailUsage);
463 }
464 errno = 0;
465 int location = ::strtol(split + 1, NULL, 10);
466 if (errno) {
467 printf("%s: invalid location\n", arg);
468 exit(EFailUsage);
469 }
470 return std::make_pair(std::string(arg, split - arg), location);
471 };
472
John Kessenichc178f0a2017-06-23 10:58:31 -0600473 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000474 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000475 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600476 case '-':
477 {
478 std::string lowerword(argv[0]+2);
479 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
480
481 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600482 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600483 lowerword == "auto-map-binding" ||
484 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600485 Options |= EOptionAutoMapBindings;
486 } else if (lowerword == "auto-map-locations" || // synonyms
487 lowerword == "aml") {
488 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100489 } else if (lowerword == "uniform-base") {
490 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700491 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100492 uniformBase = ::strtol(argv[1], NULL, 10);
493 bumpArg();
494 break;
John Kessenich6353d552017-06-23 11:11:09 -0600495 } else if (lowerword == "client") {
496 if (argc > 1) {
497 if (strcmp(argv[1], "vulkan100") == 0)
498 setVulkanSpv();
499 else if (strcmp(argv[1], "opengl100") == 0)
500 setOpenGlSpv();
501 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700502 Error("expects vulkan100 or opengl100", lowerword.c_str());
503 } else
504 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600505 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700506 } else if (lowerword == "define-macro" ||
507 lowerword == "d") {
508 if (argc > 1)
509 UserPreamble.addDef(argv[1]);
510 else
511 Error("expects <name[=def]>", argv[0]);
512 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200513 } else if (lowerword == "dump-builtin-symbols") {
514 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600515 } else if (lowerword == "entry-point") {
516 entryPointName = argv[1];
517 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700518 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600519 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600520 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
521 lowerword == "flatten-uniform-array" ||
522 lowerword == "fua") {
523 Options |= EOptionFlattenUniformArrays;
524 } else if (lowerword == "hlsl-offsets") {
525 Options |= EOptionHlslOffsets;
526 } else if (lowerword == "hlsl-iomap" ||
527 lowerword == "hlsl-iomapper" ||
528 lowerword == "hlsl-iomapping") {
529 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800530 } else if (lowerword == "hlsl-enable-16bit-types") {
531 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700532 } else if (lowerword == "hlsl-dx9-compatible") {
533 HlslDX9compatible = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700534 } else if (lowerword == "invert-y" || // synonyms
535 lowerword == "iy") {
536 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600537 } else if (lowerword == "keep-uncalled" || // synonyms
538 lowerword == "ku") {
539 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600540 } else if (lowerword == "nan-clamp") {
541 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600542 } else if (lowerword == "no-storage-format" || // synonyms
543 lowerword == "nsf") {
544 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600545 } else if (lowerword == "relaxed-errors") {
546 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000547 } else if (lowerword == "reflect-strict-array-suffix") {
548 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000549 } else if (lowerword == "reflect-basic-array-suffix") {
550 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000551 } else if (lowerword == "reflect-intermediate-io") {
552 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000553 } else if (lowerword == "reflect-separate-buffers") {
554 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000555 } else if (lowerword == "reflect-all-block-variables") {
556 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000557 } else if (lowerword == "reflect-unwrap-io-blocks") {
558 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800559 } else if (lowerword == "reflect-all-io-variables") {
560 ReflectOptions |= EShReflectionAllIOVariables;
561 } else if (lowerword == "reflect-shared-std140-ubo") {
562 ReflectOptions |= EShReflectionSharedStd140UBO;
563 } else if (lowerword == "reflect-shared-std140-ssbo") {
564 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600565 } else if (lowerword == "resource-set-bindings" || // synonyms
566 lowerword == "resource-set-binding" ||
567 lowerword == "rsb") {
568 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600569 } else if (lowerword == "shift-image-bindings" || // synonyms
570 lowerword == "shift-image-binding" ||
571 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600572 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600573 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600574 lowerword == "shift-sampler-binding" ||
575 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600576 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600577 } else if (lowerword == "shift-uav-bindings" || // synonyms
578 lowerword == "shift-uav-binding" ||
579 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600580 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600581 } else if (lowerword == "shift-texture-bindings" || // synonyms
582 lowerword == "shift-texture-binding" ||
583 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600584 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600585 } else if (lowerword == "shift-ubo-bindings" || // synonyms
586 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600587 lowerword == "shift-cbuffer-bindings" ||
588 lowerword == "shift-cbuffer-binding" ||
589 lowerword == "sub" ||
590 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600591 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700592 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
593 lowerword == "shift-ssbo-binding" ||
594 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600595 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600596 } else if (lowerword == "source-entrypoint" || // synonyms
597 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600598 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700599 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600600 sourceEntryPointName = argv[1];
601 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600602 break;
John Kesseniche2156222018-06-11 18:12:15 -0600603 } else if (lowerword == "spirv-dis") {
604 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600605 } else if (lowerword == "spirv-val") {
606 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200607 } else if (lowerword == "stdin") {
608 Options |= EOptionStdin;
609 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600610 } else if (lowerword == "suppress-warnings") {
611 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600612 } else if (lowerword == "target-env") {
613 if (argc > 1) {
614 if (strcmp(argv[1], "vulkan1.0") == 0) {
615 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600616 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700617 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
618 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600619 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700620 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
621 setVulkanSpv();
622 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600623 } else if (strcmp(argv[1], "opengl") == 0) {
624 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600625 ClientVersion = glslang::EShTargetOpenGL_450;
626 } else if (strcmp(argv[1], "spirv1.0") == 0) {
627 TargetLanguage = glslang::EShTargetSpv;
628 TargetVersion = glslang::EShTargetSpv_1_0;
629 } else if (strcmp(argv[1], "spirv1.1") == 0) {
630 TargetLanguage = glslang::EShTargetSpv;
631 TargetVersion = glslang::EShTargetSpv_1_1;
632 } else if (strcmp(argv[1], "spirv1.2") == 0) {
633 TargetLanguage = glslang::EShTargetSpv;
634 TargetVersion = glslang::EShTargetSpv_1_2;
635 } else if (strcmp(argv[1], "spirv1.3") == 0) {
636 TargetLanguage = glslang::EShTargetSpv;
637 TargetVersion = glslang::EShTargetSpv_1_3;
638 } else if (strcmp(argv[1], "spirv1.4") == 0) {
639 TargetLanguage = glslang::EShTargetSpv;
640 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600641 } else if (strcmp(argv[1], "spirv1.5") == 0) {
642 TargetLanguage = glslang::EShTargetSpv;
643 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600644 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700645 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600646 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600647 }
648 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700649 } else if (lowerword == "undef-macro" ||
650 lowerword == "u") {
651 if (argc > 1)
652 UserPreamble.addUndef(argv[1]);
653 else
654 Error("expects <name>", argv[0]);
655 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800656 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700657 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800658 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600659 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700660 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600661 variableName = argv[1];
662 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800663 break;
John Kessenichc6c80a62018-03-05 22:23:17 -0700664 } else if (lowerword == "version") {
665 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800666 } else if (lowerword == "help") {
667 usage();
668 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600669 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700670 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600671 }
672 }
673 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600674 case 'C':
675 Options |= EOptionCascadingErrors;
676 break;
677 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600678 if (argv[0][2] == 0)
679 Options |= EOptionReadHlsl;
680 else
John Kessenich6f988922020-01-07 07:03:11 -0700681 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600682 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100683 case 'u':
684 uniformLocationOverrides.push_back(getUniformOverride());
685 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600686 case 'E':
687 Options |= EOptionOutputPreprocessed;
688 break;
689 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600690 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600691 setOpenGlSpv();
692 if (argv[0][2] != 0)
693 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenich6263fb12017-06-14 15:52:44 -0600694 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000695 case 'H':
696 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600697 if ((Options & EOptionSpv) == 0) {
698 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600699 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600700 }
701 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600702 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600703 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600704 break;
GregFcd1f1692017-09-21 18:40:22 -0600705 case 'O':
706 if (argv[0][2] == 'd')
707 Options |= EOptionOptimizeDisable;
708 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600709#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600710 Options |= EOptionOptimizeSize;
711#else
712 Error("-Os not available; optimizer not linked");
713#endif
714 else
715 Error("unknown -O option");
716 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400717 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600718 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400719 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600720 shaderStageName = argv[1];
721 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400722 break;
John Kessenicha9313662017-06-15 10:40:49 -0600723 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700724 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600725 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600726 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600727 setVulkanSpv();
728 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500729 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
John Kessenichc555ddd2015-06-17 02:38:44 +0000730 break;
John Kessenich05a70632013-09-17 19:26:08 +0000731 case 'c':
732 Options |= EOptionDumpConfig;
733 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000734 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700735 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
736 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
737 Options |= EOptionDumpBareVersion;
738 else
739 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000740 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700741 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700742 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600743 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600744 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600745 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700746 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700747 case 'f':
748 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
749 targetHlslFunctionality1 = true;
750 else
751 Error("-f: expected hlsl_functionality1");
752 break;
John Kessenich121853f2017-05-31 17:11:16 -0600753 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400754 // Override previous -g or -g0 argument
755 stripDebugInfo = false;
756 Options &= ~EOptionDebug;
757 if (argv[0][2] == '0')
758 stripDebugInfo = true;
759 else
760 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600761 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600762 case 'h':
763 usage();
764 break;
John Kessenich05a70632013-09-17 19:26:08 +0000765 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000766 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000767 break;
768 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000769 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000770 break;
771 case 'm':
772 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000773 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600774 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600775 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600776 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600777 binaryFileName = argv[1];
778 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600779 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000780 case 'q':
781 Options |= EOptionDumpReflection;
782 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000783 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000784 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000785 break;
786 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000787 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000788 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000789 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200790 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000791 break;
John Kessenich319de232013-12-04 04:43:40 +0000792 case 'v':
793 Options |= EOptionDumpVersions;
794 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000795 case 'w':
796 Options |= EOptionSuppressWarnings;
797 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500798 case 'x':
799 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500800 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000801 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700802 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600803 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000804 }
John Kessenich38f3b892013-09-06 19:52:57 +0000805 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000806 std::string name(argv[0]);
807 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200808 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000809 }
John Kessenich38f3b892013-09-06 19:52:57 +0000810 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000811 }
812
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200813 // Make sure that -S is always specified if --stdin is specified
814 if ((Options & EOptionStdin) && shaderStageName == nullptr)
815 Error("must provide -S when --stdin is given");
816
John Kessenich68d78fd2015-07-12 19:28:10 -0600817 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600818 // Or things that require linking
819 if (Options & EOptionOutputPreprocessed) {
820 if (Options & EOptionLinkProgram)
821 Error("can't use -E when linking is selected");
822 if (Options & EOptionDumpReflection)
823 Error("reflection requires linking, which can't be used when -E when is selected");
824 }
825
826 // reflection requires linking
827 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
828 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000829
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500830 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600831 if (binaryFileName && (Options & EOptionSpv) == 0)
832 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600833
834 if ((Options & EOptionFlattenUniformArrays) != 0 &&
835 (Options & EOptionReadHlsl) == 0)
836 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600837
838 // rationalize client and target language
839 if (TargetLanguage == glslang::EShTargetNone) {
840 switch (ClientVersion) {
841 case glslang::EShTargetVulkan_1_0:
842 TargetLanguage = glslang::EShTargetSpv;
843 TargetVersion = glslang::EShTargetSpv_1_0;
844 break;
845 case glslang::EShTargetVulkan_1_1:
846 TargetLanguage = glslang::EShTargetSpv;
847 TargetVersion = glslang::EShTargetSpv_1_3;
848 break;
John Kessenich273d3a52020-01-15 00:10:41 -0700849 case glslang::EShTargetVulkan_1_2:
850 TargetLanguage = glslang::EShTargetSpv;
851 TargetVersion = glslang::EShTargetSpv_1_5;
852 break;
John Kessenich8717a5d2018-10-26 10:12:32 -0600853 case glslang::EShTargetOpenGL_450:
854 TargetLanguage = glslang::EShTargetSpv;
855 TargetVersion = glslang::EShTargetSpv_1_0;
856 break;
857 default:
858 break;
859 }
860 }
861 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
862 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000863}
864
John Kessenich68d78fd2015-07-12 19:28:10 -0600865//
866// Translate the meaningful subset of command-line options to parser-behavior options.
867//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000868void SetMessageOptions(EShMessages& messages)
869{
870 if (Options & EOptionRelaxedErrors)
871 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
872 if (Options & EOptionIntermediate)
873 messages = (EShMessages)(messages | EShMsgAST);
874 if (Options & EOptionSuppressWarnings)
875 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600876 if (Options & EOptionSpv)
877 messages = (EShMessages)(messages | EShMsgSpvRules);
878 if (Options & EOptionVulkanRules)
879 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400880 if (Options & EOptionOutputPreprocessed)
881 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700882 if (Options & EOptionReadHlsl)
883 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600884 if (Options & EOptionCascadingErrors)
885 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700886 if (Options & EOptionKeepUncalled)
887 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600888 if (Options & EOptionHlslOffsets)
889 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600890 if (Options & EOptionDebug)
891 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +0800892 if (HlslEnable16BitTypes)
893 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -0600894 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
895 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -0700896 if (HlslDX9compatible)
897 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200898 if (DumpBuiltinSymbols)
899 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000900}
901
John Kessenich68d78fd2015-07-12 19:28:10 -0600902//
John Kessenich69f4b512013-09-04 21:19:27 +0000903// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000904//
Juan Lopeza558b262017-04-02 23:04:00 +0200905void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000906{
John Kessenich7f0bcfd2018-04-05 19:00:01 -0600907 if (Options & EOptionDebug)
908 Error("cannot generate debug information unless linking to generate code");
909
John Kessenich38f3b892013-09-06 19:52:57 +0000910 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200911 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -0600912 if (worklist.remove(workItem)) {
913 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
914 if (compiler == nullptr)
915 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000916
John Kesseniche7f9cae2018-07-12 15:11:07 -0600917 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000918
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200919 if (! (Options & EOptionSuppressInfolog))
920 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000921
John Kesseniche7f9cae2018-07-12 15:11:07 -0600922 ShDestruct(compiler);
923 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200924 } else {
925 while (worklist.remove(workItem)) {
926 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
927 if (compiler == 0)
928 return;
929
930 CompileFile(workItem->name.c_str(), compiler);
931
932 if (! (Options & EOptionSuppressInfolog))
933 workItem->results = ShGetInfoLog(compiler);
934
935 ShDestruct(compiler);
936 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000937 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000938}
939
John Kessenich6626cad2015-06-19 05:14:19 +0000940// Outputs the given string, but only if it is non-null and non-empty.
941// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400942void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000943{
944 if (str && str[0]) {
945 puts(str);
946 }
947}
948
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400949// Outputs the given string to stderr, but only if it is non-null and non-empty.
950// This prevents erroneous newlines from appearing.
951void StderrIfNonEmpty(const char* str)
952{
John Kessenich04acb1b2017-06-14 17:36:50 -0600953 if (str && str[0])
954 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400955}
956
John Kessenichc57b2a92016-01-16 15:30:03 -0700957// Simple bundling of what makes a compilation unit for ease in passing around,
958// and separation of handling file IO versus API (programmatic) compilation.
959struct ShaderCompUnit {
960 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600961 static const int maxCount = 1;
962 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -0600963 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -0600964 std::string fileName[maxCount]; // hold's the memory, but...
965 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400966
John Kessenich04acb1b2017-06-14 17:36:50 -0600967 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400968
John Kessenich04acb1b2017-06-14 17:36:50 -0600969 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400970 {
971 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600972 count = rhs.count;
973 for (int i = 0; i < count; ++i) {
974 fileName[i] = rhs.fileName[i];
975 text[i] = rhs.text[i];
976 fileNameList[i] = rhs.fileName[i].c_str();
977 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400978 }
979
John Kessenicha9313662017-06-15 10:40:49 -0600980 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -0600981 {
982 assert(count < maxCount);
983 fileName[count] = ifileName;
984 text[count] = itext;
985 fileNameList[count] = fileName[count].c_str();
986 ++count;
987 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700988};
989
John Kessenich69f4b512013-09-04 21:19:27 +0000990//
John Kessenichc57b2a92016-01-16 15:30:03 -0700991// For linking mode: Will independently parse each compilation unit, but then put them
992// in the same program and link them together, making at most one linked module per
993// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000994//
995// Uses the new C++ interface instead of the old handle-based interface.
996//
John Kessenichc57b2a92016-01-16 15:30:03 -0700997
998void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000999{
1000 // keep track of what to free
1001 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001002
John Kessenich69f4b512013-09-04 21:19:27 +00001003 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001004 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001005
John Kessenich69f4b512013-09-04 21:19:27 +00001006 //
1007 // Per-shader processing...
1008 //
1009
John Kessenich5b0f13a2013-11-01 03:08:40 +00001010 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001011 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1012 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -07001013 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001014 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001015 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001016 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001017 if (sourceEntryPointName) {
1018 if (entryPointName == nullptr)
1019 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1020 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001021 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001022 }
John Kessenicha9313662017-06-15 10:40:49 -06001023 if (UserPreamble.isSet())
1024 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -06001025 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001026
John Kessenich155d3512019-08-08 23:29:20 -06001027#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001028 // Set IO mapper binding shift values
1029 for (int r = 0; r < glslang::EResCount; ++r) {
1030 const glslang::TResourceType res = glslang::TResourceType(r);
1031
1032 // Set base bindings
1033 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001034
LoopDawg08a14422017-10-17 19:27:14 -06001035 // Set bindings for particular resource sets
1036 // TODO: use a range based for loop here, when available in all environments.
1037 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1038 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001039 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001040 }
steve-lunargcce8d482016-10-14 18:36:42 -06001041 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001042 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001043
1044 if (Options & EOptionAutoMapBindings)
1045 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001046
John Kessenich71facdf2017-05-17 18:28:19 -06001047 if (Options & EOptionAutoMapLocations)
1048 shader->setAutoMapLocations(true);
1049
Neil Roberts16f53472018-03-20 17:30:53 +01001050 for (auto& uniOverride : uniformLocationOverrides) {
1051 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1052 uniOverride.second);
1053 }
1054
Neil Robertsb0f3d792018-03-20 17:41:05 +01001055 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001056#endif
1057
1058 shader->setNanMinMaxClamp(NaNClamp);
1059
1060#ifdef ENABLE_HLSL
1061 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1062 if (Options & EOptionHlslIoMapping)
1063 shader->setHlslIoMapping(true);
1064#endif
1065
1066 if (Options & EOptionInvertY)
1067 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001068
John Kessenich4be4aeb2017-06-23 10:50:22 -06001069 // Set up the environment, some subsettings take precedence over earlier
1070 // ways of setting things.
1071 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001072 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1073 : glslang::EShSourceGlsl,
1074 compUnit.stage, Client, ClientInputSemanticsVersion);
1075 shader->setEnvClient(Client, ClientVersion);
1076 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001077#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001078 if (targetHlslFunctionality1)
1079 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001080#endif
John Kessenich4be4aeb2017-06-23 10:50:22 -06001081 }
1082
John Kessenich69f4b512013-09-04 21:19:27 +00001083 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001084
John Kessenich4be4aeb2017-06-23 10:50:22 -06001085 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001086
John Kessenich3494b4d2017-05-22 15:00:42 -06001087 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -06001088 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1089 includer.pushExternalLocalDirectory(dir); });
John Kessenichdeec1932019-08-13 08:00:30 -06001090#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001091 if (Options & EOptionOutputPreprocessed) {
1092 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001093 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001094 PutsIfNonEmpty(str.c_str());
1095 } else {
1096 CompileFailed = true;
1097 }
1098 StderrIfNonEmpty(shader->getInfoLog());
1099 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001100 continue;
1101 }
John Kessenichdeec1932019-08-13 08:00:30 -06001102#endif
John Kessenich086febc2018-10-25 12:43:02 -06001103
John Kessenich3494b4d2017-05-22 15:00:42 -06001104 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001105 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001106
John Kessenich69f4b512013-09-04 21:19:27 +00001107 program.addShader(shader);
1108
John Kessenichc57b2a92016-01-16 15:30:03 -07001109 if (! (Options & EOptionSuppressInfolog) &&
1110 ! (Options & EOptionMemoryLeakMode)) {
John Kessenich04acb1b2017-06-14 17:36:50 -06001111 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001112 PutsIfNonEmpty(shader->getInfoLog());
1113 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001114 }
John Kessenich69f4b512013-09-04 21:19:27 +00001115 }
1116
1117 //
1118 // Program-level processing...
1119 //
1120
John Kessenich4d65ee32016-03-12 18:17:47 -07001121 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001122 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001123 LinkFailed = true;
1124
John Kessenichb6d3ee52019-08-06 02:20:45 -06001125#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001126 // Map IO
1127 if (Options & EOptionSpv) {
1128 if (!program.mapIO())
1129 LinkFailed = true;
1130 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001131#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001132
John Kessenich4d65ee32016-03-12 18:17:47 -07001133 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001134 if (! (Options & EOptionSuppressInfolog) &&
1135 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001136 PutsIfNonEmpty(program.getInfoLog());
1137 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001138 }
1139
John Kessenichb6d3ee52019-08-06 02:20:45 -06001140#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001141 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001142 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001143 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001144 program.dumpReflection();
1145 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001146#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001147
John Kessenich4d65ee32016-03-12 18:17:47 -07001148 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001149 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001150 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001151 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001152 else {
1153 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001154 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001155 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001156 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001157 glslang::SpvOptions spvOptions;
1158 if (Options & EOptionDebug)
1159 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001160 else if (stripDebugInfo)
1161 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001162 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1163 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001164 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001165 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001166 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001167
1168 // Dump the spv to a file or stdout, etc., but only if not doing
1169 // memory/perf testing, as it's not internal to programmatic use.
1170 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001171 printf("%s", logger.getAllMessages().c_str());
1172 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001173 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001174 } else {
1175 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1176 }
John Kessenich23d27752019-07-28 02:12:10 -06001177#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001178 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001179 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001180#endif
John Kessenichacba7722015-03-04 03:48:38 +00001181 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001182 }
John Kessenich92f90382014-07-28 04:21:04 +00001183 }
1184 }
1185 }
1186
John Kessenich5b0f13a2013-11-01 03:08:40 +00001187 // Free everything up, program has to go before the shaders
1188 // because it might have merged stuff from the shaders, and
1189 // the stuff from the shaders has to have its destructors called
1190 // before the pools holding the memory in the shaders is freed.
1191 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001192 while (shaders.size() > 0) {
1193 delete shaders.back();
1194 shaders.pop_back();
1195 }
John Kessenich69f4b512013-09-04 21:19:27 +00001196}
1197
John Kessenichc57b2a92016-01-16 15:30:03 -07001198//
1199// Do file IO part of compile and link, handing off the pure
1200// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1201// be put in a loop for testing memory footprint and performance.
1202//
1203// This is just for linking mode: meaning all the shaders will be put into the
1204// the same program linked together.
1205//
1206// This means there are a limited number of work items (not multi-threading mode)
1207// and that the point is testing at the linking level. Hence, to enable
1208// performance and memory testing, the actual compile/link can be put in
1209// a loop, independent of processing the work items and file IO.
1210//
Juan Lopeza558b262017-04-02 23:04:00 +02001211void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001212{
1213 std::vector<ShaderCompUnit> compUnits;
1214
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001215 // If this is using stdin, we can't really detect multiple different file
1216 // units by input type. We need to assume that we're just being given one
1217 // file of a certain type.
1218 if ((Options & EOptionStdin) != 0) {
1219 ShaderCompUnit compUnit(FindLanguage("stdin"));
1220 std::istreambuf_iterator<char> begin(std::cin), end;
1221 std::string tempString(begin, end);
1222 char* fileText = strdup(tempString.c_str());
1223 std::string fileName = "stdin";
1224 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001225 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001226 } else {
1227 // Transfer all the work items from to a simple list of
1228 // of compilation units. (We don't care about the thread
1229 // work-item distribution properties in this path, which
1230 // is okay due to the limited number of shaders, know since
1231 // they are all getting linked together.)
1232 glslang::TWorkItem* workItem;
1233 while (Worklist.remove(workItem)) {
1234 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1235 char* fileText = ReadFileData(workItem->name.c_str());
1236 if (fileText == nullptr)
1237 usage();
1238 compUnit.addString(workItem->name, fileText);
1239 compUnits.push_back(compUnit);
1240 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001241 }
1242
1243 // Actual call to programmatic processing of compile and link,
1244 // in a loop for testing memory and performance. This part contains
1245 // all the perf/memory that a programmatic consumer will care about.
1246 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1247 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1248 CompileAndLinkShaderUnits(compUnits);
1249
1250 if (Options & EOptionMemoryLeakMode)
1251 glslang::OS_DumpMemoryCounters();
1252 }
1253
John Kessenicha9313662017-06-15 10:40:49 -06001254 // free memory from ReadFileData, which got stored in a const char*
1255 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001256 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001257 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001258}
1259
John Kessenich94f28eb2017-11-13 01:32:06 -07001260int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001261{
Juan Lopeza558b262017-04-02 23:04:00 +02001262 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001263 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001264 assert(item);
1265 workList.add(item.get());
1266 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001267
John Kessenich23d27752019-07-28 02:12:10 -06001268#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001269 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001270 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001271 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001272 return ESuccess;
1273 }
John Kessenich23d27752019-07-28 02:12:10 -06001274#endif
John Kessenich05a70632013-09-17 19:26:08 +00001275
John Kessenichc6c80a62018-03-05 22:23:17 -07001276 if (Options & EOptionDumpBareVersion) {
1277 printf("%d.%d.%d\n",
1278 glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
1279 if (workList.empty())
1280 return ESuccess;
1281 } else if (Options & EOptionDumpVersions) {
1282 printf("Glslang Version: %d.%d.%d\n",
1283 glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
John Kessenich319de232013-12-04 04:43:40 +00001284 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1285 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001286 std::string spirvVersion;
1287 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001288 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001289 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001290 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001291 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001292 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1293 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001294 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001295 return ESuccess;
1296 }
1297
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001298 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001299 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001300 }
1301
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001302 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001303 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1304 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001305 }
1306
John Kessenich05a70632013-09-17 19:26:08 +00001307 ProcessConfigFile();
1308
John Kessenich086febc2018-10-25 12:43:02 -06001309 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001310 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001311
John Kessenich69f4b512013-09-04 21:19:27 +00001312 //
1313 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001314 // 1) linking all arguments together, single-threaded, new C++ interface
1315 // 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface
John Kessenich69f4b512013-09-04 21:19:27 +00001316 //
John Kessenich086febc2018-10-25 12:43:02 -06001317 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001318 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001319 glslang::InitializeProcess(); // also test reference counting of users
1320 glslang::InitializeProcess(); // also test reference counting of users
1321 glslang::FinalizeProcess(); // also test reference counting of users
1322 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001323 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001324 glslang::FinalizeProcess();
1325 } else {
1326 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001327 ShInitialize(); // also test reference counting of users
1328 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001329
Juan Lopeza558b262017-04-02 23:04:00 +02001330 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001331
John Kesseniche2c15b42017-11-16 22:48:41 -07001332 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001333 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001334 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001335 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001336 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001337 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001338 return EFailThreadCreate;
1339 }
John Kessenicha0af4732012-12-12 21:15:54 +00001340 }
Juan Lopeza558b262017-04-02 23:04:00 +02001341
1342 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001343 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001344 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001345
1346 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001347 for (size_t w = 0; w < WorkItems.size(); ++w) {
1348 if (WorkItems[w]) {
1349 if (printShaderNames || WorkItems[w]->results.size() > 0)
1350 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1351 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001352 }
1353 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001354
1355 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001356 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001357
John Kessenichc999ba22013-11-07 23:33:24 +00001358 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001359 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001360 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001361 return EFailLink;
1362
1363 return 0;
1364}
1365
John Kessenich94f28eb2017-11-13 01:32:06 -07001366int C_DECL main(int argc, char* argv[])
1367{
1368 ProcessArguments(WorkItems, argc, argv);
1369
1370 int ret = 0;
1371
1372 // Loop over the entire init/finalize cycle to watch memory changes
1373 const int iterations = 1;
1374 if (iterations > 1)
1375 glslang::OS_DumpMemoryCounters();
1376 for (int i = 0; i < iterations; ++i) {
1377 ret = singleMain();
1378 if (iterations > 1)
1379 glslang::OS_DumpMemoryCounters();
1380 }
1381
1382 return ret;
1383}
1384
John Kessenicha0af4732012-12-12 21:15:54 +00001385//
1386// Deduce the language from the filename. Files must end in one of the
1387// following extensions:
1388//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001389// .vert = vertex
1390// .tesc = tessellation control
1391// .tese = tessellation evaluation
1392// .geom = geometry
1393// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001394// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001395// .rgen = ray generation
1396// .rint = ray intersection
1397// .rahit = ray any hit
1398// .rchit = ray closest hit
1399// .rmiss = ray miss
1400// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001401// .mesh = mesh
1402// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001403// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1404// where <stage> is one of the stages listed above.
1405//
1406EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001407{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001408 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001409 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001410 stageName = shaderStageName;
1411 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001412 // Note: "first" extension means "first from the end", i.e.
1413 // if the file is named foo.vert.glsl, then "glsl" is first,
1414 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001415 size_t firstExtStart = name.find_last_of(".");
1416 bool hasFirstExt = firstExtStart != std::string::npos;
1417 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1418 bool hasSecondExt = secondExtStart != std::string::npos;
1419 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1420 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001421 if (usesUnifiedExt && firstExt == "hlsl")
1422 Options |= EOptionReadHlsl;
1423 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001424 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001425 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001426 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001427 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001428 usage();
1429 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001430 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001431 } else
1432 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001433
John Kessenichfccbb8b2018-04-17 17:24:03 -06001434 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001435 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001436 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001437 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001438 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001439 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001440 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001441 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001442 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001443 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001444 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001445 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001446 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001447 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001448 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001449 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001450 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001451 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001452 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001453 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001454 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001455 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001456 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001457 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001458 else if (stageName == "mesh")
1459 return EShLangMeshNV;
1460 else if (stageName == "task")
1461 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001462
1463 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001464 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001465}
1466
John Kessenicha0af4732012-12-12 21:15:54 +00001467//
John Kessenichecba76f2017-01-06 00:34:48 -07001468// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001469// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001470//
John Kessenich51cdd902014-02-18 23:37:57 +00001471void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001472{
John Kessenichca3457f2015-05-18 01:59:45 +00001473 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001474 char* shaderString;
1475 if ((Options & EOptionStdin) != 0) {
1476 std::istreambuf_iterator<char> begin(std::cin), end;
1477 std::string tempString(begin, end);
1478 shaderString = strdup(tempString.c_str());
1479 } else {
1480 shaderString = ReadFileData(fileName);
1481 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001482
1483 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001484 int* lengths = new int[1];
1485 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001486
John Kessenich52ac67e2013-05-05 23:46:22 +00001487 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001488 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001489
John Kessenicha9313662017-06-15 10:40:49 -06001490 if (UserPreamble.isSet())
1491 Error("-D and -U options require -l (linking)\n");
1492
John Kessenich94a81fb2013-08-31 02:41:30 +00001493 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1494 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001495 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001496 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001497 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001498 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001499 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001500 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1501 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001502 }
John Kessenicha0af4732012-12-12 21:15:54 +00001503
John Kessenich94a81fb2013-08-31 02:41:30 +00001504 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001505 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001506 }
John Kessenicha0af4732012-12-12 21:15:54 +00001507
John Kessenich41cf6b52013-06-25 18:10:05 +00001508 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001509 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001510
John Kessenichc999ba22013-11-07 23:33:24 +00001511 if (ret == 0)
1512 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001513}
1514
John Kessenicha0af4732012-12-12 21:15:54 +00001515//
1516// print usage to stdout
1517//
1518void usage()
1519{
John Kessenich319de232013-12-04 04:43:40 +00001520 printf("Usage: glslangValidator [option]... [file]...\n"
1521 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001522 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1523 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001524 " (see -c option below for generating a template)\n"
1525 " .vert for a vertex shader\n"
1526 " .tesc for a tessellation control shader\n"
1527 " .tese for a tessellation evaluation shader\n"
1528 " .geom for a geometry shader\n"
1529 " .frag for a fragment shader\n"
1530 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001531 " .mesh for a mesh shader\n"
1532 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001533 " .rgen for a ray generation shader\n"
1534 " .rint for a ray intersection shader\n"
1535 " .rahit for a ray any hit shader\n"
1536 " .rchit for a ray closest hit shader\n"
1537 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001538 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001539 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1540 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001541 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001542 "Options:\n"
1543 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001544 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001545 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1546 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001547 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001548 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001549 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001550 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001551 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001552 " which will appear in #define GL_SPIRV ver;\n"
1553 " '--client opengl100' is the same as -G100;\n"
John Kessenich6353d552017-06-23 11:11:09 -06001554 " a '--target-env' for OpenGL will also imply '-G'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001555 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001556 " -I<dir> add dir to the include search path; includer's directory\n"
1557 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001558 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1559 " -Os optimizes SPIR-V to minimize size\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001560 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001561 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001562 " -U<name> | --undef-macro <name> | --U <name>\n"
1563 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001564 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001565 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001566 " 'ver', when present, is the version of the input semantics,\n"
1567 " which will appear in #define VULKAN ver\n"
1568 " '--client vulkan100' is the same as -V100\n"
1569 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001570 " -c configuration dump;\n"
1571 " creates the default configuration file (redirect to a .conf file)\n"
1572 " -d default to desktop (#version 110) when there is no shader #version\n"
1573 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001574 " -e <name> | --entry-point <name>\n"
1575 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001576 " -f{hlsl_functionality1}\n"
1577 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001578 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001579 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001580 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001581 " -h print this usage message\n"
1582 " -i intermediate tree (glslang AST) is printed out\n"
1583 " -l link all input files together to form a single module\n"
1584 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001585 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001586 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001587 " -r | --relaxed-errors"
1588 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001589 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001590 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001591 " -v | --version\n"
1592 " print version strings\n"
1593 " -w | --suppress-warnings\n"
1594 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001595 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001596 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001597 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001598 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1599 " without explicit bindings\n"
1600 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1601 " 'location' (fragile, not cross stage)\n"
1602 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001603 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001604 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1605 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1606 " scalars\n"
1607 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1608 " works independently of source language\n"
1609 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1610 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001611 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001612 " texture/sampler combo like DirectX9 would,\n"
1613 " and recognizes DirectX9-specific semantics\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001614 " --invert-y | --iy invert position.Y output in vertex shader\n"
1615 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001616 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001617 " --no-storage-format | --nsf use Unknown image format\n"
baldurk4513df92019-02-08 10:48:48 +00001618 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1619 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001620 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001621 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1622 " shaders rather than just vertex/fragment\n"
1623 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1624 " separately to uniforms\n"
1625 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1626 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001627 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1628 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001629 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001630 " set descriptor set and binding for\n"
1631 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001632 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001633 " set descriptor set for all resources\n"
1634 " --rsb synonym for --resource-set-binding\n"
1635 " --shift-image-binding [stage] num\n"
1636 " base binding number for images (uav)\n"
1637 " --shift-image-binding [stage] [num set]...\n"
1638 " per-descriptor-set shift values\n"
1639 " --sib synonym for --shift-image-binding\n"
1640 " --shift-sampler-binding [stage] num\n"
1641 " base binding number for samplers\n"
1642 " --shift-sampler-binding [stage] [num set]...\n"
1643 " per-descriptor-set shift values\n"
1644 " --ssb synonym for --shift-sampler-binding\n"
1645 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1646 " --shift-ssbo-binding [stage] [num set]...\n"
1647 " per-descriptor-set shift values\n"
1648 " --sbb synonym for --shift-ssbo-binding\n"
1649 " --shift-texture-binding [stage] num\n"
1650 " base binding number for textures\n"
1651 " --shift-texture-binding [stage] [num set]...\n"
1652 " per-descriptor-set shift values\n"
1653 " --stb synonym for --shift-texture-binding\n"
1654 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1655 " --shift-uav-binding [stage] [num set]...\n"
1656 " per-descriptor-set shift values\n"
1657 " --suavb synonym for --shift-uav-binding\n"
1658 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1659 " --shift-UBO-binding [stage] [num set]...\n"
1660 " per-descriptor-set shift values\n"
1661 " --sub synonym for --shift-UBO-binding\n"
1662 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1663 " --spirv-dis output standard-form disassembly; works only\n"
1664 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001665 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001666 " --source-entrypoint <name> the given shader source function is\n"
1667 " renamed to be the <name> given in -e\n"
1668 " --sep synonym for --source-entrypoint\n"
1669 " --stdin read from stdin instead of from a file;\n"
1670 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001671 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001672 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001673 " Set the execution environment that the\n"
1674 " generated code will be executed in.\n"
1675 " Defaults to:\n"
1676 " * vulkan1.0 under --client vulkan<ver>\n"
1677 " * opengl under --client opengl<ver>\n"
1678 " * spirv1.0 under --target-env vulkan1.0\n"
1679 " * spirv1.3 under --target-env vulkan1.1\n"
1680 " * spirv1.5 under --target-env vulkan1.2\n"
1681 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001682 " --variable-name <name>\n"
1683 " --vn <name> creates a C header file that contains a\n"
1684 " uint32_t array named <name>\n"
1685 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001686 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001687
1688 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001689}
1690
John Kessenich3ce4e592014-10-06 19:57:34 +00001691#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001692
1693#include <errno.h>
1694
1695int fopen_s(
1696 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001697 const char* filename,
1698 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001699)
1700{
1701 if (!pFile || !filename || !mode) {
1702 return EINVAL;
1703 }
1704
1705 FILE* f = fopen(filename, mode);
1706 if (! f) {
1707 if (errno != 0) {
1708 return errno;
1709 } else {
1710 return ENOENT;
1711 }
1712 }
1713 *pFile = f;
1714
1715 return 0;
1716}
1717
1718#endif
1719
John Kessenicha0af4732012-12-12 21:15:54 +00001720//
1721// Malloc a string of sufficient size and read a string into it.
1722//
John Kessenich04acb1b2017-06-14 17:36:50 -06001723char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001724{
John Kessenichb3297152015-07-11 18:01:03 -06001725 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001726 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001727 if (errorCode || in == nullptr)
1728 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001729
John Kessenich04acb1b2017-06-14 17:36:50 -06001730 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001731 while (fgetc(in) != EOF)
1732 count++;
1733
John Kessenichd6c72a42014-08-18 19:42:35 +00001734 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001735
John Kessenich04acb1b2017-06-14 17:36:50 -06001736 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1737 if ((int)fread(return_data, 1, count, in) != count) {
1738 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001739 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001740 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001741
John Kessenich04acb1b2017-06-14 17:36:50 -06001742 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001743 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001744
John Kessenicha0af4732012-12-12 21:15:54 +00001745 return return_data;
1746}
1747
John Kessenich04acb1b2017-06-14 17:36:50 -06001748void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001749{
John Kessenichb3297152015-07-11 18:01:03 -06001750 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001751}
1752
John Kessenich54d8cda2013-02-11 22:36:01 +00001753void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001754{
John Kessenichfae38ee2015-06-10 23:23:12 +00001755 if (num >= 0 )
1756 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1757 else
1758 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001759}