blob: 8836812742677bdc242ef42c2ba6ae577fcd5ddd [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"
47#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060058#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020059#include <memory>
60#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000061
baldurk876a0e32015-11-16 18:03:28 +010062#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000063
Ben Claytonfbe9a232020-06-17 11:17:19 +010064// Build-time generated includes
65#include "glslang/build_info.h"
66
John Kessenicha0af4732012-12-12 21:15:54 +000067extern "C" {
Ben Claytond64e8592020-06-29 14:20:19 +010068 GLSLANG_EXPORT void ShOutputHtml();
John Kessenicha0af4732012-12-12 21:15:54 +000069}
70
John Kessenich94a81fb2013-08-31 02:41:30 +000071// Command-line options
72enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070073 EOptionNone = 0,
74 EOptionIntermediate = (1 << 0),
75 EOptionSuppressInfolog = (1 << 1),
76 EOptionMemoryLeakMode = (1 << 2),
77 EOptionRelaxedErrors = (1 << 3),
78 EOptionGiveWarnings = (1 << 4),
79 EOptionLinkProgram = (1 << 5),
80 EOptionMultiThreaded = (1 << 6),
81 EOptionDumpConfig = (1 << 7),
82 EOptionDumpReflection = (1 << 8),
83 EOptionSuppressWarnings = (1 << 9),
84 EOptionDumpVersions = (1 << 10),
85 EOptionSpv = (1 << 11),
86 EOptionHumanReadableSpv = (1 << 12),
87 EOptionVulkanRules = (1 << 13),
88 EOptionDefaultDesktop = (1 << 14),
89 EOptionOutputPreprocessed = (1 << 15),
90 EOptionOutputHexadecimal = (1 << 16),
91 EOptionReadHlsl = (1 << 17),
92 EOptionCascadingErrors = (1 << 18),
93 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060094 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070095 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070096 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060097 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060098 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060099 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -0600100 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200101 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -0600102 EOptionOptimizeDisable = (1 << 28),
103 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700104 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700105 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000106};
John Kessenich5d610ee2018-03-07 18:05:55 -0700107bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600108bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600109bool SpvToolsValidate = false;
John Kessenich605afc72019-06-17 23:33:09 -0600110bool NaNClamp = false;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400111bool stripDebugInfo = false;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200112bool beQuiet = false;
John Kessenich94a81fb2013-08-31 02:41:30 +0000113
John Kessenicha0af4732012-12-12 21:15:54 +0000114//
John Kessenich68d78fd2015-07-12 19:28:10 -0600115// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000116//
117enum TFailCode {
118 ESuccess = 0,
119 EFailUsage,
120 EFailCompile,
121 EFailLink,
122 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000123 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000124 EFailLinkerCreate
125};
126
127//
John Kessenich68d78fd2015-07-12 19:28:10 -0600128// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000129//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600130EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000131void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000132void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600133char* ReadFileData(const char* fileName);
134void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000135void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000136
John Kessenichc999ba22013-11-07 23:33:24 +0000137// Globally track if any compile or link failure.
138bool CompileFailed = false;
139bool LinkFailed = false;
140
John Kessenich94f28eb2017-11-13 01:32:06 -0700141// array of unique places to leave the shader names and infologs for the asynchronous compiles
142std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
143
John Kessenich05a70632013-09-17 19:26:08 +0000144TBuiltInResource Resources;
145std::string ConfigFile;
146
John Kessenicha0af4732012-12-12 21:15:54 +0000147//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600148// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000149//
150void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000151{
John Kessenich04acb1b2017-06-14 17:36:50 -0600152 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500153 Resources = glslang::DefaultTBuiltInResource;
John Kessenich23d27752019-07-28 02:12:10 -0600154#ifndef GLSLANG_WEB
John Kessenich04acb1b2017-06-14 17:36:50 -0600155 else {
156 char* configString = ReadFileData(ConfigFile.c_str());
157 glslang::DecodeResourceLimits(&Resources, configString);
158 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000159 }
John Kessenich23d27752019-07-28 02:12:10 -0600160#endif
John Kessenicha0af4732012-12-12 21:15:54 +0000161}
162
baldurk6d477852019-01-29 15:45:56 +0000163int ReflectOptions = EShReflectionDefault;
John Kessenich94a81fb2013-08-31 02:41:30 +0000164int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600165const char* ExecutableName = nullptr;
166const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700167const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600168const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400169const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800170const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800171bool HlslEnable16BitTypes = false;
John Kessenichbd1c1832018-12-07 18:38:26 -0700172bool HlslDX9compatible = false;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200173bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600174std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600175
176// Source environment
177// (source 'Client' is currently the same as target 'Client')
178int ClientInputSemanticsVersion = 100;
179
180// Target environment
181glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
182glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
183glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
184glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
185
John Kessenich66011cb2018-03-06 16:12:04 -0700186std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600187
LoopDawg08a14422017-10-17 19:27:14 -0600188// Per descriptor-set binding base data
189typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
190
Neil Roberts16f53472018-03-20 17:30:53 +0100191std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100192int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100193
LoopDawg08a14422017-10-17 19:27:14 -0600194std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
195std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900196std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600197
John Kessenicha9313662017-06-15 10:40:49 -0600198// Add things like "#define ..." to a preamble to use in the beginning of the shader.
199class TPreamble {
200public:
201 TPreamble() { }
202
203 bool isSet() const { return text.size() > 0; }
204 const char* get() const { return text.c_str(); }
205
206 // #define...
207 void addDef(std::string def)
208 {
209 text.append("#define ");
210 fixLine(def);
211
John Kessenich6f988922020-01-07 07:03:11 -0700212 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600213 Processes.back().append(def);
214
John Kessenicha9313662017-06-15 10:40:49 -0600215 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600216 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600217 if (equal != def.npos)
218 def[equal] = ' ';
219
220 text.append(def);
221 text.append("\n");
222 }
223
224 // #undef...
225 void addUndef(std::string undef)
226 {
227 text.append("#undef ");
228 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600229
John Kessenich6f988922020-01-07 07:03:11 -0700230 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600231 Processes.back().append(undef);
232
John Kessenicha9313662017-06-15 10:40:49 -0600233 text.append(undef);
234 text.append("\n");
235 }
236
237protected:
238 void fixLine(std::string& line)
239 {
240 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600241 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600242 if (end != line.npos)
243 line = line.substr(0, end);
244 }
245
246 std::string text; // contents of preamble
247};
248
John Kessenich83855b92020-03-30 00:27:31 -0600249// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600250TPreamble UserPreamble;
251
John Kessenich68d78fd2015-07-12 19:28:10 -0600252//
253// Create the default name for saving a binary if -o is not provided.
254//
255const char* GetBinaryName(EShLanguage stage)
256{
257 const char* name;
258 if (binaryFileName == nullptr) {
259 switch (stage) {
260 case EShLangVertex: name = "vert.spv"; break;
261 case EShLangTessControl: name = "tesc.spv"; break;
262 case EShLangTessEvaluation: name = "tese.spv"; break;
263 case EShLangGeometry: name = "geom.spv"; break;
264 case EShLangFragment: name = "frag.spv"; break;
265 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400266 case EShLangRayGen: name = "rgen.spv"; break;
267 case EShLangIntersect: name = "rint.spv"; break;
268 case EShLangAnyHit: name = "rahit.spv"; break;
269 case EShLangClosestHit: name = "rchit.spv"; break;
270 case EShLangMiss: name = "rmiss.spv"; break;
271 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700272 case EShLangMeshNV: name = "mesh.spv"; break;
273 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600274 default: name = "unknown"; break;
275 }
276 } else
277 name = binaryFileName;
278
279 return name;
280}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000281
John Kessenich05a70632013-09-17 19:26:08 +0000282//
283// *.conf => this is a config file that can set limits/resources
284//
285bool SetConfigFile(const std::string& name)
286{
287 if (name.size() < 5)
288 return false;
289
John Kessenich4c706852013-10-11 16:28:43 +0000290 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000291 ConfigFile = name;
292 return true;
293 }
294
295 return false;
296}
297
John Kessenich68d78fd2015-07-12 19:28:10 -0600298//
299// Give error and exit with failure code.
300//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700301void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600302{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700303 fprintf(stderr, "%s: Error: ", ExecutableName);
304 if (detail != nullptr)
305 fprintf(stderr, "%s: ", detail);
306 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600307 exit(EFailUsage);
308}
309
310//
LoopDawg08a14422017-10-17 19:27:14 -0600311// Process an optional binding base of one the forms:
312// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600313// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600314
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600315// Where stage is one of the forms accepted by FindLanguage, and base is an integer
316//
LoopDawg08a14422017-10-17 19:27:14 -0600317void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600318{
319 if (argc < 2)
320 usage();
321
LoopDawg08a14422017-10-17 19:27:14 -0600322 EShLanguage lang = EShLangCount;
323 int singleBase = 0;
324 TPerSetBaseBinding perSetBase;
325 int arg = 1;
326
327 // Parse stage, if given
328 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600329 if (argc < 3) // this form needs one more argument
330 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700331
LoopDawg08a14422017-10-17 19:27:14 -0600332 lang = FindLanguage(argv[arg++], false);
333 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600334
LoopDawg08a14422017-10-17 19:27:14 -0600335 if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
336 // Parse a per-set binding base
337 while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600338 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600339 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600340 perSetBase[setNum] = baseNum;
341 }
342 } else {
343 // Parse single binding base
344 singleBase = atoi(argv[arg++]);
345 }
346
347 argc -= (arg-1);
348 argv += (arg-1);
349
350 // Set one or all languages
351 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
352 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
353
354 for (int lang = langMin; lang < langMax; ++lang) {
355 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600356 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600357 else
358 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600359 }
360}
361
Hyangran Park36dc8292017-05-02 16:27:29 +0900362void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
363{
364 if (argc < 2)
365 usage();
366
367 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600368 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900369 usage();
370
LoopDawg52017192017-07-14 15:15:47 -0600371 // Parse form: --argname stage [regname set base...], or:
372 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900373 const EShLanguage lang = FindLanguage(argv[1], false);
374
LoopDawg52017192017-07-14 15:15:47 -0600375 argc--;
376 argv++;
377
378 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
379 base[lang].push_back(argv[1]);
380
381 argc--;
382 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900383 }
LoopDawg52017192017-07-14 15:15:47 -0600384
385 // Must have one arg, or a multiple of three (for [regname set binding] triples)
386 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
387 usage();
388
Hyangran Park36dc8292017-05-02 16:27:29 +0900389 } else {
LoopDawg52017192017-07-14 15:15:47 -0600390 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900391 for (int lang=0; lang<EShLangCount; ++lang)
392 base[lang].push_back(argv[1]);
393
394 argc--;
395 argv++;
396 }
397}
398
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600399//
John Kessenich68d78fd2015-07-12 19:28:10 -0600400// Do all command-line argument parsing. This includes building up the work-items
401// to be processed later, and saving all the command-line options.
402//
403// Does not return (it exits) if command-line is fatally flawed.
404//
Juan Lopeza558b262017-04-02 23:04:00 +0200405void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000406{
LoopDawg08a14422017-10-17 19:27:14 -0600407 for (int res = 0; res < glslang::EResCount; ++res)
408 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600409
John Kessenich38f3b892013-09-06 19:52:57 +0000410 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200411 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000412
John Kessenichc178f0a2017-06-23 10:58:31 -0600413 const auto bumpArg = [&]() {
414 if (argc > 0) {
415 argc--;
416 argv++;
417 }
418 };
419
420 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600421 const auto getStringOperand = [&](const char* desc) {
422 if (argv[0][2] == 0) {
423 printf("%s must immediately follow option (no spaces)\n", desc);
424 exit(EFailUsage);
425 }
426 return argv[0] + 2;
427 };
428
John Kessenich6353d552017-06-23 11:11:09 -0600429 // read a number attached to a single-letter option
430 const auto getAttachedNumber = [&](const char* desc) {
431 int num = atoi(argv[0] + 2);
432 if (num == 0) {
433 printf("%s: expected attached non-0 number\n", desc);
434 exit(EFailUsage);
435 }
436 return num;
437 };
438
439 // minimum needed (without overriding something else) to target Vulkan SPIR-V
440 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600441 if (Client == glslang::EShClientNone)
442 ClientVersion = glslang::EShTargetVulkan_1_0;
443 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600444 Options |= EOptionSpv;
445 Options |= EOptionVulkanRules;
446 Options |= EOptionLinkProgram;
447 };
448
449 // minimum needed (without overriding something else) to target OpenGL SPIR-V
450 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600451 if (Client == glslang::EShClientNone)
452 ClientVersion = glslang::EShTargetOpenGL_450;
453 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600454 Options |= EOptionSpv;
455 Options |= EOptionLinkProgram;
456 // undo a -H default to Vulkan
457 Options &= ~EOptionVulkanRules;
458 };
459
Neil Roberts16f53472018-03-20 17:30:53 +0100460 const auto getUniformOverride = [getStringOperand]() {
461 const char *arg = getStringOperand("-u<name>:<location>");
462 const char *split = strchr(arg, ':');
463 if (split == NULL) {
464 printf("%s: missing location\n", arg);
465 exit(EFailUsage);
466 }
467 errno = 0;
468 int location = ::strtol(split + 1, NULL, 10);
469 if (errno) {
470 printf("%s: invalid location\n", arg);
471 exit(EFailUsage);
472 }
473 return std::make_pair(std::string(arg, split - arg), location);
474 };
475
John Kessenichc178f0a2017-06-23 10:58:31 -0600476 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000477 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000478 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600479 case '-':
480 {
481 std::string lowerword(argv[0]+2);
482 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
483
484 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600485 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600486 lowerword == "auto-map-binding" ||
487 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600488 Options |= EOptionAutoMapBindings;
489 } else if (lowerword == "auto-map-locations" || // synonyms
490 lowerword == "aml") {
491 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100492 } else if (lowerword == "uniform-base") {
493 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700494 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100495 uniformBase = ::strtol(argv[1], NULL, 10);
496 bumpArg();
497 break;
John Kessenich6353d552017-06-23 11:11:09 -0600498 } else if (lowerword == "client") {
499 if (argc > 1) {
500 if (strcmp(argv[1], "vulkan100") == 0)
501 setVulkanSpv();
502 else if (strcmp(argv[1], "opengl100") == 0)
503 setOpenGlSpv();
504 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700505 Error("expects vulkan100 or opengl100", lowerword.c_str());
506 } else
507 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600508 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700509 } else if (lowerword == "define-macro" ||
510 lowerword == "d") {
511 if (argc > 1)
512 UserPreamble.addDef(argv[1]);
513 else
514 Error("expects <name[=def]>", argv[0]);
515 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200516 } else if (lowerword == "dump-builtin-symbols") {
517 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600518 } else if (lowerword == "entry-point") {
519 entryPointName = argv[1];
520 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700521 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600522 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600523 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
524 lowerword == "flatten-uniform-array" ||
525 lowerword == "fua") {
526 Options |= EOptionFlattenUniformArrays;
527 } else if (lowerword == "hlsl-offsets") {
528 Options |= EOptionHlslOffsets;
529 } else if (lowerword == "hlsl-iomap" ||
530 lowerword == "hlsl-iomapper" ||
531 lowerword == "hlsl-iomapping") {
532 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800533 } else if (lowerword == "hlsl-enable-16bit-types") {
534 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700535 } else if (lowerword == "hlsl-dx9-compatible") {
536 HlslDX9compatible = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700537 } else if (lowerword == "invert-y" || // synonyms
538 lowerword == "iy") {
539 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600540 } else if (lowerword == "keep-uncalled" || // synonyms
541 lowerword == "ku") {
542 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600543 } else if (lowerword == "nan-clamp") {
544 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600545 } else if (lowerword == "no-storage-format" || // synonyms
546 lowerword == "nsf") {
547 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600548 } else if (lowerword == "relaxed-errors") {
549 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000550 } else if (lowerword == "reflect-strict-array-suffix") {
551 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000552 } else if (lowerword == "reflect-basic-array-suffix") {
553 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000554 } else if (lowerword == "reflect-intermediate-io") {
555 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000556 } else if (lowerword == "reflect-separate-buffers") {
557 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000558 } else if (lowerword == "reflect-all-block-variables") {
559 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000560 } else if (lowerword == "reflect-unwrap-io-blocks") {
561 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800562 } else if (lowerword == "reflect-all-io-variables") {
563 ReflectOptions |= EShReflectionAllIOVariables;
564 } else if (lowerword == "reflect-shared-std140-ubo") {
565 ReflectOptions |= EShReflectionSharedStd140UBO;
566 } else if (lowerword == "reflect-shared-std140-ssbo") {
567 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600568 } else if (lowerword == "resource-set-bindings" || // synonyms
569 lowerword == "resource-set-binding" ||
570 lowerword == "rsb") {
571 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600572 } else if (lowerword == "shift-image-bindings" || // synonyms
573 lowerword == "shift-image-binding" ||
574 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600575 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600576 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600577 lowerword == "shift-sampler-binding" ||
578 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600579 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600580 } else if (lowerword == "shift-uav-bindings" || // synonyms
581 lowerword == "shift-uav-binding" ||
582 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600583 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600584 } else if (lowerword == "shift-texture-bindings" || // synonyms
585 lowerword == "shift-texture-binding" ||
586 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600587 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600588 } else if (lowerword == "shift-ubo-bindings" || // synonyms
589 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600590 lowerword == "shift-cbuffer-bindings" ||
591 lowerword == "shift-cbuffer-binding" ||
592 lowerword == "sub" ||
593 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600594 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700595 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
596 lowerword == "shift-ssbo-binding" ||
597 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600598 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600599 } else if (lowerword == "source-entrypoint" || // synonyms
600 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600601 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700602 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600603 sourceEntryPointName = argv[1];
604 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600605 break;
John Kesseniche2156222018-06-11 18:12:15 -0600606 } else if (lowerword == "spirv-dis") {
607 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600608 } else if (lowerword == "spirv-val") {
609 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200610 } else if (lowerword == "stdin") {
611 Options |= EOptionStdin;
612 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600613 } else if (lowerword == "suppress-warnings") {
614 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600615 } else if (lowerword == "target-env") {
616 if (argc > 1) {
617 if (strcmp(argv[1], "vulkan1.0") == 0) {
618 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600619 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700620 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
621 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600622 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700623 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
624 setVulkanSpv();
625 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600626 } else if (strcmp(argv[1], "opengl") == 0) {
627 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600628 ClientVersion = glslang::EShTargetOpenGL_450;
629 } else if (strcmp(argv[1], "spirv1.0") == 0) {
630 TargetLanguage = glslang::EShTargetSpv;
631 TargetVersion = glslang::EShTargetSpv_1_0;
632 } else if (strcmp(argv[1], "spirv1.1") == 0) {
633 TargetLanguage = glslang::EShTargetSpv;
634 TargetVersion = glslang::EShTargetSpv_1_1;
635 } else if (strcmp(argv[1], "spirv1.2") == 0) {
636 TargetLanguage = glslang::EShTargetSpv;
637 TargetVersion = glslang::EShTargetSpv_1_2;
638 } else if (strcmp(argv[1], "spirv1.3") == 0) {
639 TargetLanguage = glslang::EShTargetSpv;
640 TargetVersion = glslang::EShTargetSpv_1_3;
641 } else if (strcmp(argv[1], "spirv1.4") == 0) {
642 TargetLanguage = glslang::EShTargetSpv;
643 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600644 } else if (strcmp(argv[1], "spirv1.5") == 0) {
645 TargetLanguage = glslang::EShTargetSpv;
646 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600647 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700648 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600649 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600650 }
651 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700652 } else if (lowerword == "undef-macro" ||
653 lowerword == "u") {
654 if (argc > 1)
655 UserPreamble.addUndef(argv[1]);
656 else
657 Error("expects <name>", argv[0]);
658 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800659 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700660 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800661 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600662 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700663 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600664 variableName = argv[1];
665 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800666 break;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200667 } else if (lowerword == "quiet") {
668 beQuiet = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700669 } else if (lowerword == "version") {
670 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800671 } else if (lowerword == "help") {
672 usage();
673 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600674 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700675 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600676 }
677 }
678 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600679 case 'C':
680 Options |= EOptionCascadingErrors;
681 break;
682 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600683 if (argv[0][2] == 0)
684 Options |= EOptionReadHlsl;
685 else
John Kessenich6f988922020-01-07 07:03:11 -0700686 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600687 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100688 case 'u':
689 uniformLocationOverrides.push_back(getUniformOverride());
690 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600691 case 'E':
692 Options |= EOptionOutputPreprocessed;
693 break;
694 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600695 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600696 setOpenGlSpv();
697 if (argv[0][2] != 0)
698 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenich6263fb12017-06-14 15:52:44 -0600699 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000700 case 'H':
701 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600702 if ((Options & EOptionSpv) == 0) {
703 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600704 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600705 }
706 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600707 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600708 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600709 break;
GregFcd1f1692017-09-21 18:40:22 -0600710 case 'O':
711 if (argv[0][2] == 'd')
712 Options |= EOptionOptimizeDisable;
713 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600714#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600715 Options |= EOptionOptimizeSize;
716#else
717 Error("-Os not available; optimizer not linked");
718#endif
719 else
720 Error("unknown -O option");
721 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400722 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600723 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400724 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600725 shaderStageName = argv[1];
726 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400727 break;
John Kessenicha9313662017-06-15 10:40:49 -0600728 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700729 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600730 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600731 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600732 setVulkanSpv();
733 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500734 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
John Kessenichc555ddd2015-06-17 02:38:44 +0000735 break;
John Kessenich05a70632013-09-17 19:26:08 +0000736 case 'c':
737 Options |= EOptionDumpConfig;
738 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000739 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700740 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
741 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
742 Options |= EOptionDumpBareVersion;
743 else
744 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000745 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700746 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700747 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600748 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600749 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600750 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700751 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700752 case 'f':
753 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
754 targetHlslFunctionality1 = true;
755 else
756 Error("-f: expected hlsl_functionality1");
757 break;
John Kessenich121853f2017-05-31 17:11:16 -0600758 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400759 // Override previous -g or -g0 argument
760 stripDebugInfo = false;
761 Options &= ~EOptionDebug;
762 if (argv[0][2] == '0')
763 stripDebugInfo = true;
764 else
765 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600766 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600767 case 'h':
768 usage();
769 break;
John Kessenich05a70632013-09-17 19:26:08 +0000770 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000771 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000772 break;
773 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000774 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000775 break;
776 case 'm':
777 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000778 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600779 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600780 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600781 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600782 binaryFileName = argv[1];
783 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600784 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000785 case 'q':
786 Options |= EOptionDumpReflection;
787 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000788 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000789 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000790 break;
791 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000792 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000793 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000794 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200795 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000796 break;
John Kessenich319de232013-12-04 04:43:40 +0000797 case 'v':
798 Options |= EOptionDumpVersions;
799 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000800 case 'w':
801 Options |= EOptionSuppressWarnings;
802 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500803 case 'x':
804 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500805 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000806 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700807 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600808 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000809 }
John Kessenich38f3b892013-09-06 19:52:57 +0000810 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000811 std::string name(argv[0]);
812 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200813 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000814 }
John Kessenich38f3b892013-09-06 19:52:57 +0000815 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000816 }
817
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200818 // Make sure that -S is always specified if --stdin is specified
819 if ((Options & EOptionStdin) && shaderStageName == nullptr)
820 Error("must provide -S when --stdin is given");
821
John Kessenich68d78fd2015-07-12 19:28:10 -0600822 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600823 // Or things that require linking
824 if (Options & EOptionOutputPreprocessed) {
825 if (Options & EOptionLinkProgram)
826 Error("can't use -E when linking is selected");
827 if (Options & EOptionDumpReflection)
828 Error("reflection requires linking, which can't be used when -E when is selected");
829 }
830
831 // reflection requires linking
832 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
833 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000834
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500835 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600836 if (binaryFileName && (Options & EOptionSpv) == 0)
837 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600838
839 if ((Options & EOptionFlattenUniformArrays) != 0 &&
840 (Options & EOptionReadHlsl) == 0)
841 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600842
843 // rationalize client and target language
844 if (TargetLanguage == glslang::EShTargetNone) {
845 switch (ClientVersion) {
846 case glslang::EShTargetVulkan_1_0:
847 TargetLanguage = glslang::EShTargetSpv;
848 TargetVersion = glslang::EShTargetSpv_1_0;
849 break;
850 case glslang::EShTargetVulkan_1_1:
851 TargetLanguage = glslang::EShTargetSpv;
852 TargetVersion = glslang::EShTargetSpv_1_3;
853 break;
John Kessenich273d3a52020-01-15 00:10:41 -0700854 case glslang::EShTargetVulkan_1_2:
855 TargetLanguage = glslang::EShTargetSpv;
856 TargetVersion = glslang::EShTargetSpv_1_5;
857 break;
John Kessenich8717a5d2018-10-26 10:12:32 -0600858 case glslang::EShTargetOpenGL_450:
859 TargetLanguage = glslang::EShTargetSpv;
860 TargetVersion = glslang::EShTargetSpv_1_0;
861 break;
862 default:
863 break;
864 }
865 }
866 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
867 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000868}
869
John Kessenich68d78fd2015-07-12 19:28:10 -0600870//
871// Translate the meaningful subset of command-line options to parser-behavior options.
872//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000873void SetMessageOptions(EShMessages& messages)
874{
875 if (Options & EOptionRelaxedErrors)
876 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
877 if (Options & EOptionIntermediate)
878 messages = (EShMessages)(messages | EShMsgAST);
879 if (Options & EOptionSuppressWarnings)
880 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600881 if (Options & EOptionSpv)
882 messages = (EShMessages)(messages | EShMsgSpvRules);
883 if (Options & EOptionVulkanRules)
884 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400885 if (Options & EOptionOutputPreprocessed)
886 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700887 if (Options & EOptionReadHlsl)
888 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600889 if (Options & EOptionCascadingErrors)
890 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700891 if (Options & EOptionKeepUncalled)
892 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600893 if (Options & EOptionHlslOffsets)
894 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600895 if (Options & EOptionDebug)
896 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +0800897 if (HlslEnable16BitTypes)
898 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -0600899 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
900 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -0700901 if (HlslDX9compatible)
902 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200903 if (DumpBuiltinSymbols)
904 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000905}
906
John Kessenich68d78fd2015-07-12 19:28:10 -0600907//
John Kessenich69f4b512013-09-04 21:19:27 +0000908// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000909//
Juan Lopeza558b262017-04-02 23:04:00 +0200910void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000911{
John Kessenich7f0bcfd2018-04-05 19:00:01 -0600912 if (Options & EOptionDebug)
913 Error("cannot generate debug information unless linking to generate code");
914
John Kessenich38f3b892013-09-06 19:52:57 +0000915 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200916 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -0600917 if (worklist.remove(workItem)) {
918 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
919 if (compiler == nullptr)
920 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000921
John Kesseniche7f9cae2018-07-12 15:11:07 -0600922 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000923
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200924 if (! (Options & EOptionSuppressInfolog))
925 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000926
John Kesseniche7f9cae2018-07-12 15:11:07 -0600927 ShDestruct(compiler);
928 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200929 } else {
930 while (worklist.remove(workItem)) {
931 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
932 if (compiler == 0)
933 return;
934
935 CompileFile(workItem->name.c_str(), compiler);
936
937 if (! (Options & EOptionSuppressInfolog))
938 workItem->results = ShGetInfoLog(compiler);
939
940 ShDestruct(compiler);
941 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000942 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000943}
944
John Kessenich6626cad2015-06-19 05:14:19 +0000945// Outputs the given string, but only if it is non-null and non-empty.
946// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400947void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000948{
949 if (str && str[0]) {
950 puts(str);
951 }
952}
953
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400954// Outputs the given string to stderr, but only if it is non-null and non-empty.
955// This prevents erroneous newlines from appearing.
956void StderrIfNonEmpty(const char* str)
957{
John Kessenich04acb1b2017-06-14 17:36:50 -0600958 if (str && str[0])
959 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400960}
961
John Kessenichc57b2a92016-01-16 15:30:03 -0700962// Simple bundling of what makes a compilation unit for ease in passing around,
963// and separation of handling file IO versus API (programmatic) compilation.
964struct ShaderCompUnit {
965 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600966 static const int maxCount = 1;
967 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -0600968 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -0600969 std::string fileName[maxCount]; // hold's the memory, but...
970 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400971
John Kessenich04acb1b2017-06-14 17:36:50 -0600972 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400973
John Kessenich04acb1b2017-06-14 17:36:50 -0600974 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400975 {
976 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600977 count = rhs.count;
978 for (int i = 0; i < count; ++i) {
979 fileName[i] = rhs.fileName[i];
980 text[i] = rhs.text[i];
981 fileNameList[i] = rhs.fileName[i].c_str();
982 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400983 }
984
John Kessenicha9313662017-06-15 10:40:49 -0600985 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -0600986 {
987 assert(count < maxCount);
988 fileName[count] = ifileName;
989 text[count] = itext;
990 fileNameList[count] = fileName[count].c_str();
991 ++count;
992 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700993};
994
John Kessenich69f4b512013-09-04 21:19:27 +0000995//
John Kessenichc57b2a92016-01-16 15:30:03 -0700996// For linking mode: Will independently parse each compilation unit, but then put them
997// in the same program and link them together, making at most one linked module per
998// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000999//
1000// Uses the new C++ interface instead of the old handle-based interface.
1001//
John Kessenichc57b2a92016-01-16 15:30:03 -07001002
1003void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +00001004{
1005 // keep track of what to free
1006 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001007
John Kessenich69f4b512013-09-04 21:19:27 +00001008 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001009 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001010
John Kessenich69f4b512013-09-04 21:19:27 +00001011 //
1012 // Per-shader processing...
1013 //
1014
John Kessenich5b0f13a2013-11-01 03:08:40 +00001015 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001016 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1017 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -07001018 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001019 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001020 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001021 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001022 if (sourceEntryPointName) {
1023 if (entryPointName == nullptr)
1024 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1025 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001026 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001027 }
John Kessenicha9313662017-06-15 10:40:49 -06001028 if (UserPreamble.isSet())
1029 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -06001030 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001031
John Kessenich155d3512019-08-08 23:29:20 -06001032#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001033 // Set IO mapper binding shift values
1034 for (int r = 0; r < glslang::EResCount; ++r) {
1035 const glslang::TResourceType res = glslang::TResourceType(r);
1036
1037 // Set base bindings
1038 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001039
LoopDawg08a14422017-10-17 19:27:14 -06001040 // Set bindings for particular resource sets
1041 // TODO: use a range based for loop here, when available in all environments.
1042 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1043 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001044 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001045 }
steve-lunargcce8d482016-10-14 18:36:42 -06001046 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001047 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001048
1049 if (Options & EOptionAutoMapBindings)
1050 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001051
John Kessenich71facdf2017-05-17 18:28:19 -06001052 if (Options & EOptionAutoMapLocations)
1053 shader->setAutoMapLocations(true);
1054
Neil Roberts16f53472018-03-20 17:30:53 +01001055 for (auto& uniOverride : uniformLocationOverrides) {
1056 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1057 uniOverride.second);
1058 }
1059
Neil Robertsb0f3d792018-03-20 17:41:05 +01001060 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001061#endif
1062
1063 shader->setNanMinMaxClamp(NaNClamp);
1064
1065#ifdef ENABLE_HLSL
1066 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1067 if (Options & EOptionHlslIoMapping)
1068 shader->setHlslIoMapping(true);
1069#endif
1070
1071 if (Options & EOptionInvertY)
1072 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001073
John Kessenich4be4aeb2017-06-23 10:50:22 -06001074 // Set up the environment, some subsettings take precedence over earlier
1075 // ways of setting things.
1076 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001077 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1078 : glslang::EShSourceGlsl,
1079 compUnit.stage, Client, ClientInputSemanticsVersion);
1080 shader->setEnvClient(Client, ClientVersion);
1081 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001082#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001083 if (targetHlslFunctionality1)
1084 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001085#endif
John Kessenich4be4aeb2017-06-23 10:50:22 -06001086 }
1087
John Kessenich69f4b512013-09-04 21:19:27 +00001088 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001089
John Kessenich4be4aeb2017-06-23 10:50:22 -06001090 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001091
John Kessenich3494b4d2017-05-22 15:00:42 -06001092 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -06001093 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1094 includer.pushExternalLocalDirectory(dir); });
John Kessenichdeec1932019-08-13 08:00:30 -06001095#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001096 if (Options & EOptionOutputPreprocessed) {
1097 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001098 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001099 PutsIfNonEmpty(str.c_str());
1100 } else {
1101 CompileFailed = true;
1102 }
1103 StderrIfNonEmpty(shader->getInfoLog());
1104 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001105 continue;
1106 }
John Kessenichdeec1932019-08-13 08:00:30 -06001107#endif
John Kessenich086febc2018-10-25 12:43:02 -06001108
John Kessenich3494b4d2017-05-22 15:00:42 -06001109 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001110 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001111
John Kessenich69f4b512013-09-04 21:19:27 +00001112 program.addShader(shader);
1113
John Kessenichc57b2a92016-01-16 15:30:03 -07001114 if (! (Options & EOptionSuppressInfolog) &&
1115 ! (Options & EOptionMemoryLeakMode)) {
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001116 if (!beQuiet)
1117 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001118 PutsIfNonEmpty(shader->getInfoLog());
1119 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001120 }
John Kessenich69f4b512013-09-04 21:19:27 +00001121 }
1122
1123 //
1124 // Program-level processing...
1125 //
1126
John Kessenich4d65ee32016-03-12 18:17:47 -07001127 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001128 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001129 LinkFailed = true;
1130
John Kessenichb6d3ee52019-08-06 02:20:45 -06001131#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001132 // Map IO
1133 if (Options & EOptionSpv) {
1134 if (!program.mapIO())
1135 LinkFailed = true;
1136 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001137#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001138
John Kessenich4d65ee32016-03-12 18:17:47 -07001139 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001140 if (! (Options & EOptionSuppressInfolog) &&
1141 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001142 PutsIfNonEmpty(program.getInfoLog());
1143 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001144 }
1145
John Kessenichb6d3ee52019-08-06 02:20:45 -06001146#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001147 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001148 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001149 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001150 program.dumpReflection();
1151 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001152#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001153
John Kessenich4d65ee32016-03-12 18:17:47 -07001154 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001155 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001156 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001157 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001158 else {
1159 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001160 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001161 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001162 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001163 glslang::SpvOptions spvOptions;
1164 if (Options & EOptionDebug)
1165 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001166 else if (stripDebugInfo)
1167 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001168 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1169 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001170 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001171 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001172 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001173
1174 // Dump the spv to a file or stdout, etc., but only if not doing
1175 // memory/perf testing, as it's not internal to programmatic use.
1176 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001177 printf("%s", logger.getAllMessages().c_str());
1178 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001179 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001180 } else {
1181 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1182 }
John Kessenich23d27752019-07-28 02:12:10 -06001183#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001184 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001185 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001186#endif
John Kessenichacba7722015-03-04 03:48:38 +00001187 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001188 }
John Kessenich92f90382014-07-28 04:21:04 +00001189 }
1190 }
1191 }
1192
John Kessenich5b0f13a2013-11-01 03:08:40 +00001193 // Free everything up, program has to go before the shaders
1194 // because it might have merged stuff from the shaders, and
1195 // the stuff from the shaders has to have its destructors called
1196 // before the pools holding the memory in the shaders is freed.
1197 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001198 while (shaders.size() > 0) {
1199 delete shaders.back();
1200 shaders.pop_back();
1201 }
John Kessenich69f4b512013-09-04 21:19:27 +00001202}
1203
John Kessenichc57b2a92016-01-16 15:30:03 -07001204//
1205// Do file IO part of compile and link, handing off the pure
1206// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1207// be put in a loop for testing memory footprint and performance.
1208//
1209// This is just for linking mode: meaning all the shaders will be put into the
1210// the same program linked together.
1211//
1212// This means there are a limited number of work items (not multi-threading mode)
1213// and that the point is testing at the linking level. Hence, to enable
1214// performance and memory testing, the actual compile/link can be put in
1215// a loop, independent of processing the work items and file IO.
1216//
Juan Lopeza558b262017-04-02 23:04:00 +02001217void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001218{
1219 std::vector<ShaderCompUnit> compUnits;
1220
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001221 // If this is using stdin, we can't really detect multiple different file
1222 // units by input type. We need to assume that we're just being given one
1223 // file of a certain type.
1224 if ((Options & EOptionStdin) != 0) {
1225 ShaderCompUnit compUnit(FindLanguage("stdin"));
1226 std::istreambuf_iterator<char> begin(std::cin), end;
1227 std::string tempString(begin, end);
1228 char* fileText = strdup(tempString.c_str());
1229 std::string fileName = "stdin";
1230 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001231 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001232 } else {
1233 // Transfer all the work items from to a simple list of
1234 // of compilation units. (We don't care about the thread
1235 // work-item distribution properties in this path, which
1236 // is okay due to the limited number of shaders, know since
1237 // they are all getting linked together.)
1238 glslang::TWorkItem* workItem;
1239 while (Worklist.remove(workItem)) {
1240 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1241 char* fileText = ReadFileData(workItem->name.c_str());
1242 if (fileText == nullptr)
1243 usage();
1244 compUnit.addString(workItem->name, fileText);
1245 compUnits.push_back(compUnit);
1246 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001247 }
1248
1249 // Actual call to programmatic processing of compile and link,
1250 // in a loop for testing memory and performance. This part contains
1251 // all the perf/memory that a programmatic consumer will care about.
1252 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1253 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1254 CompileAndLinkShaderUnits(compUnits);
1255
1256 if (Options & EOptionMemoryLeakMode)
1257 glslang::OS_DumpMemoryCounters();
1258 }
1259
John Kessenicha9313662017-06-15 10:40:49 -06001260 // free memory from ReadFileData, which got stored in a const char*
1261 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001262 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001263 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001264}
1265
John Kessenich94f28eb2017-11-13 01:32:06 -07001266int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001267{
Juan Lopeza558b262017-04-02 23:04:00 +02001268 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001269 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001270 assert(item);
1271 workList.add(item.get());
1272 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001273
John Kessenich23d27752019-07-28 02:12:10 -06001274#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001275 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001276 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001277 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001278 return ESuccess;
1279 }
John Kessenich23d27752019-07-28 02:12:10 -06001280#endif
John Kessenich05a70632013-09-17 19:26:08 +00001281
John Kessenichc6c80a62018-03-05 22:23:17 -07001282 if (Options & EOptionDumpBareVersion) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001283 printf("%d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1284 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenichc6c80a62018-03-05 22:23:17 -07001285 if (workList.empty())
1286 return ESuccess;
1287 } else if (Options & EOptionDumpVersions) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001288 printf("Glslang Version: %d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR,
1289 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenich319de232013-12-04 04:43:40 +00001290 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1291 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001292 std::string spirvVersion;
1293 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001294 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001295 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001296 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001297 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001298 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1299 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001300 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001301 return ESuccess;
1302 }
1303
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001304 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001305 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001306 }
1307
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001308 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001309 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1310 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001311 }
1312
John Kessenich05a70632013-09-17 19:26:08 +00001313 ProcessConfigFile();
1314
John Kessenich086febc2018-10-25 12:43:02 -06001315 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001316 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001317
John Kessenich69f4b512013-09-04 21:19:27 +00001318 //
1319 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001320 // 1) linking all arguments together, single-threaded, new C++ interface
1321 // 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 +00001322 //
John Kessenich086febc2018-10-25 12:43:02 -06001323 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001324 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001325 glslang::InitializeProcess(); // also test reference counting of users
1326 glslang::InitializeProcess(); // also test reference counting of users
1327 glslang::FinalizeProcess(); // also test reference counting of users
1328 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001329 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001330 glslang::FinalizeProcess();
1331 } else {
1332 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001333 ShInitialize(); // also test reference counting of users
1334 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001335
Juan Lopeza558b262017-04-02 23:04:00 +02001336 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001337
John Kesseniche2c15b42017-11-16 22:48:41 -07001338 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001339 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001340 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001341 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001342 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001343 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001344 return EFailThreadCreate;
1345 }
John Kessenicha0af4732012-12-12 21:15:54 +00001346 }
Juan Lopeza558b262017-04-02 23:04:00 +02001347
1348 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001349 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001350 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001351
1352 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001353 for (size_t w = 0; w < WorkItems.size(); ++w) {
1354 if (WorkItems[w]) {
1355 if (printShaderNames || WorkItems[w]->results.size() > 0)
1356 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1357 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001358 }
1359 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001360
1361 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001362 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001363
John Kessenichc999ba22013-11-07 23:33:24 +00001364 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001365 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001366 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001367 return EFailLink;
1368
1369 return 0;
1370}
1371
John Kessenich94f28eb2017-11-13 01:32:06 -07001372int C_DECL main(int argc, char* argv[])
1373{
1374 ProcessArguments(WorkItems, argc, argv);
1375
1376 int ret = 0;
1377
1378 // Loop over the entire init/finalize cycle to watch memory changes
1379 const int iterations = 1;
1380 if (iterations > 1)
1381 glslang::OS_DumpMemoryCounters();
1382 for (int i = 0; i < iterations; ++i) {
1383 ret = singleMain();
1384 if (iterations > 1)
1385 glslang::OS_DumpMemoryCounters();
1386 }
1387
1388 return ret;
1389}
1390
John Kessenicha0af4732012-12-12 21:15:54 +00001391//
1392// Deduce the language from the filename. Files must end in one of the
1393// following extensions:
1394//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001395// .vert = vertex
1396// .tesc = tessellation control
1397// .tese = tessellation evaluation
1398// .geom = geometry
1399// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001400// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001401// .rgen = ray generation
1402// .rint = ray intersection
1403// .rahit = ray any hit
1404// .rchit = ray closest hit
1405// .rmiss = ray miss
1406// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001407// .mesh = mesh
1408// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001409// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1410// where <stage> is one of the stages listed above.
1411//
1412EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001413{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001414 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001415 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001416 stageName = shaderStageName;
1417 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001418 // Note: "first" extension means "first from the end", i.e.
1419 // if the file is named foo.vert.glsl, then "glsl" is first,
1420 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001421 size_t firstExtStart = name.find_last_of(".");
1422 bool hasFirstExt = firstExtStart != std::string::npos;
1423 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1424 bool hasSecondExt = secondExtStart != std::string::npos;
1425 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1426 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001427 if (usesUnifiedExt && firstExt == "hlsl")
1428 Options |= EOptionReadHlsl;
1429 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001430 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001431 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001432 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001433 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001434 usage();
1435 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001436 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001437 } else
1438 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001439
John Kessenichfccbb8b2018-04-17 17:24:03 -06001440 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001441 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001442 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001443 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001444 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001445 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001446 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001447 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001448 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001449 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001450 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001451 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001452 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001453 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001454 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001455 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001456 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001457 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001458 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001459 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001460 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001461 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001462 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001463 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001464 else if (stageName == "mesh")
1465 return EShLangMeshNV;
1466 else if (stageName == "task")
1467 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001468
1469 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001470 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001471}
1472
John Kessenicha0af4732012-12-12 21:15:54 +00001473//
John Kessenichecba76f2017-01-06 00:34:48 -07001474// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001475// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001476//
John Kessenich51cdd902014-02-18 23:37:57 +00001477void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001478{
John Kessenichca3457f2015-05-18 01:59:45 +00001479 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001480 char* shaderString;
1481 if ((Options & EOptionStdin) != 0) {
1482 std::istreambuf_iterator<char> begin(std::cin), end;
1483 std::string tempString(begin, end);
1484 shaderString = strdup(tempString.c_str());
1485 } else {
1486 shaderString = ReadFileData(fileName);
1487 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001488
1489 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001490 int* lengths = new int[1];
1491 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001492
John Kessenich52ac67e2013-05-05 23:46:22 +00001493 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001494 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001495
John Kessenicha9313662017-06-15 10:40:49 -06001496 if (UserPreamble.isSet())
1497 Error("-D and -U options require -l (linking)\n");
1498
John Kessenich94a81fb2013-08-31 02:41:30 +00001499 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1500 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001501 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001502 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001503 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001504 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001505 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001506 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1507 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001508 }
John Kessenicha0af4732012-12-12 21:15:54 +00001509
John Kessenich94a81fb2013-08-31 02:41:30 +00001510 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001511 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001512 }
John Kessenicha0af4732012-12-12 21:15:54 +00001513
John Kessenich41cf6b52013-06-25 18:10:05 +00001514 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001515 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001516
John Kessenichc999ba22013-11-07 23:33:24 +00001517 if (ret == 0)
1518 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001519}
1520
John Kessenicha0af4732012-12-12 21:15:54 +00001521//
1522// print usage to stdout
1523//
1524void usage()
1525{
John Kessenich319de232013-12-04 04:43:40 +00001526 printf("Usage: glslangValidator [option]... [file]...\n"
1527 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001528 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1529 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001530 " (see -c option below for generating a template)\n"
1531 " .vert for a vertex shader\n"
1532 " .tesc for a tessellation control shader\n"
1533 " .tese for a tessellation evaluation shader\n"
1534 " .geom for a geometry shader\n"
1535 " .frag for a fragment shader\n"
1536 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001537 " .mesh for a mesh shader\n"
1538 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001539 " .rgen for a ray generation shader\n"
1540 " .rint for a ray intersection shader\n"
1541 " .rahit for a ray any hit shader\n"
1542 " .rchit for a ray closest hit shader\n"
1543 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001544 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001545 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1546 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001547 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001548 "Options:\n"
1549 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001550 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001551 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1552 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001553 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001554 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001555 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001556 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001557 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001558 " which will appear in #define GL_SPIRV ver;\n"
1559 " '--client opengl100' is the same as -G100;\n"
John Kessenich6353d552017-06-23 11:11:09 -06001560 " a '--target-env' for OpenGL will also imply '-G'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001561 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001562 " -I<dir> add dir to the include search path; includer's directory\n"
1563 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001564 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1565 " -Os optimizes SPIR-V to minimize size\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001566 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001567 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001568 " -U<name> | --undef-macro <name> | --U <name>\n"
1569 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001570 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001571 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001572 " 'ver', when present, is the version of the input semantics,\n"
1573 " which will appear in #define VULKAN ver\n"
1574 " '--client vulkan100' is the same as -V100\n"
1575 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001576 " -c configuration dump;\n"
1577 " creates the default configuration file (redirect to a .conf file)\n"
1578 " -d default to desktop (#version 110) when there is no shader #version\n"
1579 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001580 " -e <name> | --entry-point <name>\n"
1581 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001582 " -f{hlsl_functionality1}\n"
1583 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001584 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001585 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001586 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001587 " -h print this usage message\n"
1588 " -i intermediate tree (glslang AST) is printed out\n"
1589 " -l link all input files together to form a single module\n"
1590 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001591 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001592 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001593 " -r | --relaxed-errors"
1594 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001595 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001596 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001597 " -v | --version\n"
1598 " print version strings\n"
1599 " -w | --suppress-warnings\n"
1600 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001601 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001602 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001603 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001604 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1605 " without explicit bindings\n"
1606 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1607 " 'location' (fragile, not cross stage)\n"
1608 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001609 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001610 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1611 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1612 " scalars\n"
1613 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1614 " works independently of source language\n"
1615 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1616 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001617 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001618 " texture/sampler combo like DirectX9 would,\n"
1619 " and recognizes DirectX9-specific semantics\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001620 " --invert-y | --iy invert position.Y output in vertex shader\n"
1621 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001622 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001623 " --no-storage-format | --nsf use Unknown image format\n"
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001624 " --quiet do not print anything to stdout, unless\n"
1625 " requested by another option\n"
baldurk4513df92019-02-08 10:48:48 +00001626 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1627 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001628 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001629 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1630 " shaders rather than just vertex/fragment\n"
1631 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1632 " separately to uniforms\n"
1633 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1634 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001635 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1636 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001637 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001638 " set descriptor set and binding for\n"
1639 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001640 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001641 " set descriptor set for all resources\n"
1642 " --rsb synonym for --resource-set-binding\n"
1643 " --shift-image-binding [stage] num\n"
1644 " base binding number for images (uav)\n"
1645 " --shift-image-binding [stage] [num set]...\n"
1646 " per-descriptor-set shift values\n"
1647 " --sib synonym for --shift-image-binding\n"
1648 " --shift-sampler-binding [stage] num\n"
1649 " base binding number for samplers\n"
1650 " --shift-sampler-binding [stage] [num set]...\n"
1651 " per-descriptor-set shift values\n"
1652 " --ssb synonym for --shift-sampler-binding\n"
1653 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1654 " --shift-ssbo-binding [stage] [num set]...\n"
1655 " per-descriptor-set shift values\n"
1656 " --sbb synonym for --shift-ssbo-binding\n"
1657 " --shift-texture-binding [stage] num\n"
1658 " base binding number for textures\n"
1659 " --shift-texture-binding [stage] [num set]...\n"
1660 " per-descriptor-set shift values\n"
1661 " --stb synonym for --shift-texture-binding\n"
1662 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1663 " --shift-uav-binding [stage] [num set]...\n"
1664 " per-descriptor-set shift values\n"
1665 " --suavb synonym for --shift-uav-binding\n"
1666 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1667 " --shift-UBO-binding [stage] [num set]...\n"
1668 " per-descriptor-set shift values\n"
1669 " --sub synonym for --shift-UBO-binding\n"
1670 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1671 " --spirv-dis output standard-form disassembly; works only\n"
1672 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001673 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001674 " --source-entrypoint <name> the given shader source function is\n"
1675 " renamed to be the <name> given in -e\n"
1676 " --sep synonym for --source-entrypoint\n"
1677 " --stdin read from stdin instead of from a file;\n"
1678 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001679 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001680 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001681 " Set the execution environment that the\n"
1682 " generated code will be executed in.\n"
1683 " Defaults to:\n"
1684 " * vulkan1.0 under --client vulkan<ver>\n"
1685 " * opengl under --client opengl<ver>\n"
1686 " * spirv1.0 under --target-env vulkan1.0\n"
1687 " * spirv1.3 under --target-env vulkan1.1\n"
1688 " * spirv1.5 under --target-env vulkan1.2\n"
1689 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001690 " --variable-name <name>\n"
1691 " --vn <name> creates a C header file that contains a\n"
1692 " uint32_t array named <name>\n"
1693 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001694 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001695
1696 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001697}
1698
John Kessenich3ce4e592014-10-06 19:57:34 +00001699#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001700
1701#include <errno.h>
1702
1703int fopen_s(
1704 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001705 const char* filename,
1706 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001707)
1708{
1709 if (!pFile || !filename || !mode) {
1710 return EINVAL;
1711 }
1712
1713 FILE* f = fopen(filename, mode);
1714 if (! f) {
1715 if (errno != 0) {
1716 return errno;
1717 } else {
1718 return ENOENT;
1719 }
1720 }
1721 *pFile = f;
1722
1723 return 0;
1724}
1725
1726#endif
1727
John Kessenicha0af4732012-12-12 21:15:54 +00001728//
1729// Malloc a string of sufficient size and read a string into it.
1730//
John Kessenich04acb1b2017-06-14 17:36:50 -06001731char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001732{
John Kessenichb3297152015-07-11 18:01:03 -06001733 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001734 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001735 if (errorCode || in == nullptr)
1736 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001737
John Kessenich04acb1b2017-06-14 17:36:50 -06001738 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001739 while (fgetc(in) != EOF)
1740 count++;
1741
John Kessenichd6c72a42014-08-18 19:42:35 +00001742 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001743
John Kessenich04acb1b2017-06-14 17:36:50 -06001744 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1745 if ((int)fread(return_data, 1, count, in) != count) {
1746 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001747 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001748 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001749
John Kessenich04acb1b2017-06-14 17:36:50 -06001750 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001751 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001752
John Kessenicha0af4732012-12-12 21:15:54 +00001753 return return_data;
1754}
1755
John Kessenich04acb1b2017-06-14 17:36:50 -06001756void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001757{
John Kessenichb3297152015-07-11 18:01:03 -06001758 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001759}
1760
John Kessenich54d8cda2013-02-11 22:36:01 +00001761void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001762{
John Kessenichfae38ee2015-06-10 23:23:12 +00001763 if (num >= 0 )
1764 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1765 else
1766 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001767}