blob: f76fc05de4d28efcee11b680374051298721cecc [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>
Georg Lehmannf0502092021-04-20 00:22:15 +020061#include <set>
John Kessenicha0af4732012-12-12 21:15:54 +000062
baldurk876a0e32015-11-16 18:03:28 +010063#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000064
Ben Claytonfbe9a232020-06-17 11:17:19 +010065// Build-time generated includes
66#include "glslang/build_info.h"
67
John Kessenicha0af4732012-12-12 21:15:54 +000068extern "C" {
Ben Claytond64e8592020-06-29 14:20:19 +010069 GLSLANG_EXPORT void ShOutputHtml();
John Kessenicha0af4732012-12-12 21:15:54 +000070}
71
John Kessenich94a81fb2013-08-31 02:41:30 +000072// Command-line options
73enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070074 EOptionNone = 0,
75 EOptionIntermediate = (1 << 0),
76 EOptionSuppressInfolog = (1 << 1),
77 EOptionMemoryLeakMode = (1 << 2),
78 EOptionRelaxedErrors = (1 << 3),
79 EOptionGiveWarnings = (1 << 4),
80 EOptionLinkProgram = (1 << 5),
81 EOptionMultiThreaded = (1 << 6),
82 EOptionDumpConfig = (1 << 7),
83 EOptionDumpReflection = (1 << 8),
84 EOptionSuppressWarnings = (1 << 9),
85 EOptionDumpVersions = (1 << 10),
86 EOptionSpv = (1 << 11),
87 EOptionHumanReadableSpv = (1 << 12),
88 EOptionVulkanRules = (1 << 13),
89 EOptionDefaultDesktop = (1 << 14),
90 EOptionOutputPreprocessed = (1 << 15),
91 EOptionOutputHexadecimal = (1 << 16),
92 EOptionReadHlsl = (1 << 17),
93 EOptionCascadingErrors = (1 << 18),
94 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060095 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070096 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070097 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060098 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060099 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -0600100 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -0600101 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200102 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -0600103 EOptionOptimizeDisable = (1 << 28),
104 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700105 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700106 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000107};
John Kessenich5d610ee2018-03-07 18:05:55 -0700108bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600109bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600110bool SpvToolsValidate = false;
John Kessenich605afc72019-06-17 23:33:09 -0600111bool NaNClamp = false;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400112bool stripDebugInfo = false;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200113bool beQuiet = false;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600114bool VulkanRulesRelaxed = false;
John Kessenich94a81fb2013-08-31 02:41:30 +0000115
John Kessenicha0af4732012-12-12 21:15:54 +0000116//
John Kessenich68d78fd2015-07-12 19:28:10 -0600117// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000118//
119enum TFailCode {
120 ESuccess = 0,
121 EFailUsage,
122 EFailCompile,
123 EFailLink,
124 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000125 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000126 EFailLinkerCreate
127};
128
129//
John Kessenich68d78fd2015-07-12 19:28:10 -0600130// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000131//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600132EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000133void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000134void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600135char* ReadFileData(const char* fileName);
136void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000137void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000138
John Kessenichc999ba22013-11-07 23:33:24 +0000139// Globally track if any compile or link failure.
140bool CompileFailed = false;
141bool LinkFailed = false;
142
John Kessenich94f28eb2017-11-13 01:32:06 -0700143// array of unique places to leave the shader names and infologs for the asynchronous compiles
144std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
145
John Kessenich05a70632013-09-17 19:26:08 +0000146TBuiltInResource Resources;
147std::string ConfigFile;
148
John Kessenicha0af4732012-12-12 21:15:54 +0000149//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600150// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000151//
152void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000153{
John Kessenich04acb1b2017-06-14 17:36:50 -0600154 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500155 Resources = glslang::DefaultTBuiltInResource;
John Kessenich23d27752019-07-28 02:12:10 -0600156#ifndef GLSLANG_WEB
John Kessenich04acb1b2017-06-14 17:36:50 -0600157 else {
158 char* configString = ReadFileData(ConfigFile.c_str());
159 glslang::DecodeResourceLimits(&Resources, configString);
160 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000161 }
John Kessenich23d27752019-07-28 02:12:10 -0600162#endif
John Kessenicha0af4732012-12-12 21:15:54 +0000163}
164
baldurk6d477852019-01-29 15:45:56 +0000165int ReflectOptions = EShReflectionDefault;
John Kessenich94a81fb2013-08-31 02:41:30 +0000166int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600167const char* ExecutableName = nullptr;
168const char* binaryFileName = nullptr;
Georg Lehmannf0502092021-04-20 00:22:15 +0200169const char* depencyFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700170const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600171const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400172const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800173const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800174bool HlslEnable16BitTypes = false;
John Kessenichbd1c1832018-12-07 18:38:26 -0700175bool HlslDX9compatible = false;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200176bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600177std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600178
179// Source environment
180// (source 'Client' is currently the same as target 'Client')
181int ClientInputSemanticsVersion = 100;
182
183// Target environment
184glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
185glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
186glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
187glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
188
John Kessenich66011cb2018-03-06 16:12:04 -0700189std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600190
LoopDawg08a14422017-10-17 19:27:14 -0600191// Per descriptor-set binding base data
192typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
193
Neil Roberts16f53472018-03-20 17:30:53 +0100194std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100195int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100196
LoopDawg08a14422017-10-17 19:27:14 -0600197std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
198std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900199std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600200
greg-lunarg4e064ee2021-03-15 11:26:11 -0600201std::vector<std::pair<std::string, glslang::TBlockStorageClass>> blockStorageOverrides;
202
203bool setGlobalUniformBlock = false;
204std::string globalUniformName;
205unsigned int globalUniformBinding;
206unsigned int globalUniformSet;
207
208bool setGlobalBufferBlock = false;
209std::string atomicCounterBlockName;
210unsigned int atomicCounterBlockSet;
211
John Kessenicha9313662017-06-15 10:40:49 -0600212// Add things like "#define ..." to a preamble to use in the beginning of the shader.
213class TPreamble {
214public:
215 TPreamble() { }
216
217 bool isSet() const { return text.size() > 0; }
218 const char* get() const { return text.c_str(); }
219
220 // #define...
221 void addDef(std::string def)
222 {
223 text.append("#define ");
224 fixLine(def);
225
John Kessenich6f988922020-01-07 07:03:11 -0700226 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600227 Processes.back().append(def);
228
John Kessenicha9313662017-06-15 10:40:49 -0600229 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600230 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600231 if (equal != def.npos)
232 def[equal] = ' ';
233
234 text.append(def);
235 text.append("\n");
236 }
237
238 // #undef...
239 void addUndef(std::string undef)
240 {
241 text.append("#undef ");
242 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600243
John Kessenich6f988922020-01-07 07:03:11 -0700244 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600245 Processes.back().append(undef);
246
John Kessenicha9313662017-06-15 10:40:49 -0600247 text.append(undef);
248 text.append("\n");
249 }
250
251protected:
252 void fixLine(std::string& line)
253 {
254 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600255 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600256 if (end != line.npos)
257 line = line.substr(0, end);
258 }
259
260 std::string text; // contents of preamble
261};
262
John Kessenich83855b92020-03-30 00:27:31 -0600263// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600264TPreamble UserPreamble;
265
John Kessenich68d78fd2015-07-12 19:28:10 -0600266//
267// Create the default name for saving a binary if -o is not provided.
268//
269const char* GetBinaryName(EShLanguage stage)
270{
271 const char* name;
272 if (binaryFileName == nullptr) {
273 switch (stage) {
274 case EShLangVertex: name = "vert.spv"; break;
275 case EShLangTessControl: name = "tesc.spv"; break;
276 case EShLangTessEvaluation: name = "tese.spv"; break;
277 case EShLangGeometry: name = "geom.spv"; break;
278 case EShLangFragment: name = "frag.spv"; break;
279 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400280 case EShLangRayGen: name = "rgen.spv"; break;
281 case EShLangIntersect: name = "rint.spv"; break;
282 case EShLangAnyHit: name = "rahit.spv"; break;
283 case EShLangClosestHit: name = "rchit.spv"; break;
284 case EShLangMiss: name = "rmiss.spv"; break;
285 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700286 case EShLangMeshNV: name = "mesh.spv"; break;
287 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600288 default: name = "unknown"; break;
289 }
290 } else
291 name = binaryFileName;
292
293 return name;
294}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000295
John Kessenich05a70632013-09-17 19:26:08 +0000296//
297// *.conf => this is a config file that can set limits/resources
298//
299bool SetConfigFile(const std::string& name)
300{
301 if (name.size() < 5)
302 return false;
303
John Kessenich4c706852013-10-11 16:28:43 +0000304 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000305 ConfigFile = name;
306 return true;
307 }
308
309 return false;
310}
311
John Kessenich68d78fd2015-07-12 19:28:10 -0600312//
313// Give error and exit with failure code.
314//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700315void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600316{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700317 fprintf(stderr, "%s: Error: ", ExecutableName);
318 if (detail != nullptr)
319 fprintf(stderr, "%s: ", detail);
320 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600321 exit(EFailUsage);
322}
323
324//
LoopDawg08a14422017-10-17 19:27:14 -0600325// Process an optional binding base of one the forms:
326// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600327// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600328
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600329// Where stage is one of the forms accepted by FindLanguage, and base is an integer
330//
LoopDawg08a14422017-10-17 19:27:14 -0600331void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600332{
333 if (argc < 2)
334 usage();
335
LoopDawg08a14422017-10-17 19:27:14 -0600336 EShLanguage lang = EShLangCount;
337 int singleBase = 0;
338 TPerSetBaseBinding perSetBase;
339 int arg = 1;
340
341 // Parse stage, if given
342 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600343 if (argc < 3) // this form needs one more argument
344 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700345
LoopDawg08a14422017-10-17 19:27:14 -0600346 lang = FindLanguage(argv[arg++], false);
347 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600348
LoopDawg08a14422017-10-17 19:27:14 -0600349 if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
350 // Parse a per-set binding base
351 while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600352 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600353 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600354 perSetBase[setNum] = baseNum;
355 }
356 } else {
357 // Parse single binding base
358 singleBase = atoi(argv[arg++]);
359 }
360
361 argc -= (arg-1);
362 argv += (arg-1);
363
364 // Set one or all languages
365 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
366 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
367
368 for (int lang = langMin; lang < langMax; ++lang) {
369 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600370 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600371 else
372 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600373 }
374}
375
Hyangran Park36dc8292017-05-02 16:27:29 +0900376void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
377{
378 if (argc < 2)
379 usage();
380
381 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600382 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900383 usage();
384
LoopDawg52017192017-07-14 15:15:47 -0600385 // Parse form: --argname stage [regname set base...], or:
386 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900387 const EShLanguage lang = FindLanguage(argv[1], false);
388
LoopDawg52017192017-07-14 15:15:47 -0600389 argc--;
390 argv++;
391
392 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
393 base[lang].push_back(argv[1]);
394
395 argc--;
396 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900397 }
LoopDawg52017192017-07-14 15:15:47 -0600398
399 // Must have one arg, or a multiple of three (for [regname set binding] triples)
400 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
401 usage();
402
Hyangran Park36dc8292017-05-02 16:27:29 +0900403 } else {
LoopDawg52017192017-07-14 15:15:47 -0600404 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900405 for (int lang=0; lang<EShLangCount; ++lang)
406 base[lang].push_back(argv[1]);
407
408 argc--;
409 argv++;
410 }
411}
412
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600413//
greg-lunarg4e064ee2021-03-15 11:26:11 -0600414// Process an optional binding base of one the forms:
415// --argname name {uniform|buffer|push_constant}
416void ProcessBlockStorage(int& argc, char**& argv, std::vector<std::pair<std::string, glslang::TBlockStorageClass>>& storage)
417{
418 if (argc < 3)
419 usage();
420
421 glslang::TBlockStorageClass blockStorage = glslang::EbsNone;
422
423 std::string strBacking(argv[2]);
424 if (strBacking == "uniform")
425 blockStorage = glslang::EbsUniform;
426 else if (strBacking == "buffer")
427 blockStorage = glslang::EbsStorageBuffer;
428 else if (strBacking == "push_constant")
429 blockStorage = glslang::EbsPushConstant;
430 else {
431 printf("%s: invalid block storage\n", strBacking.c_str());
432 usage();
433 }
434
435 storage.push_back(std::make_pair(std::string(argv[1]), blockStorage));
436
437 argc -= 2;
438 argv += 2;
439}
440
441inline bool isNonDigit(char c) {
442 // a non-digit character valid in a glsl identifier
443 return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
444}
445
446// whether string isa valid identifier to be used in glsl
447bool isValidIdentifier(const char* str) {
448 std::string idn(str);
449
450 if (idn.length() == 0) {
451 return false;
452 }
453
454 if (idn.length() >= 3 && idn.substr(0, 3) == "gl_") {
455 // identifiers startin with "gl_" are reserved
456 return false;
457 }
458
459 if (!isNonDigit(idn[0])) {
460 return false;
461 }
462
463 for (unsigned int i = 1; i < idn.length(); ++i) {
464 if (!(isdigit(idn[i]) || isNonDigit(idn[i]))) {
465 return false;
466 }
467 }
468
469 return true;
470}
471
472// Process settings for either the global buffer block or global unfirom block
473// of the form:
474// --argname name set binding
475void ProcessGlobalBlockSettings(int& argc, char**& argv, std::string* name, unsigned int* set, unsigned int* binding)
476{
477 if (argc < 4)
478 usage();
479
480 unsigned int curArg = 1;
481
482 assert(name || set || binding);
483
484 if (name) {
485 if (!isValidIdentifier(argv[curArg])) {
486 printf("%s: invalid identifier\n", argv[curArg]);
487 usage();
488 }
489 *name = argv[curArg];
490
491 curArg++;
492 }
493
494 if (set) {
495 errno = 0;
496 int setVal = ::strtol(argv[curArg], NULL, 10);
497 if (errno || setVal < 0) {
498 printf("%s: invalid set\n", argv[curArg]);
499 usage();
500 }
501 *set = setVal;
502
503 curArg++;
504 }
505
506 if (binding) {
507 errno = 0;
508 int bindingVal = ::strtol(argv[curArg], NULL, 10);
509 if (errno || bindingVal < 0) {
510 printf("%s: invalid binding\n", argv[curArg]);
511 usage();
512 }
513 *binding = bindingVal;
514
515 curArg++;
516 }
517
518 argc -= (curArg - 1);
519 argv += (curArg - 1);
520}
521
522//
John Kessenich68d78fd2015-07-12 19:28:10 -0600523// Do all command-line argument parsing. This includes building up the work-items
524// to be processed later, and saving all the command-line options.
525//
526// Does not return (it exits) if command-line is fatally flawed.
527//
Juan Lopeza558b262017-04-02 23:04:00 +0200528void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000529{
LoopDawg08a14422017-10-17 19:27:14 -0600530 for (int res = 0; res < glslang::EResCount; ++res)
531 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600532
John Kessenich38f3b892013-09-06 19:52:57 +0000533 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200534 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000535
John Kessenichc178f0a2017-06-23 10:58:31 -0600536 const auto bumpArg = [&]() {
537 if (argc > 0) {
538 argc--;
539 argv++;
540 }
541 };
542
543 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600544 const auto getStringOperand = [&](const char* desc) {
545 if (argv[0][2] == 0) {
546 printf("%s must immediately follow option (no spaces)\n", desc);
547 exit(EFailUsage);
548 }
549 return argv[0] + 2;
550 };
551
John Kessenich6353d552017-06-23 11:11:09 -0600552 // read a number attached to a single-letter option
553 const auto getAttachedNumber = [&](const char* desc) {
554 int num = atoi(argv[0] + 2);
555 if (num == 0) {
556 printf("%s: expected attached non-0 number\n", desc);
557 exit(EFailUsage);
558 }
559 return num;
560 };
561
562 // minimum needed (without overriding something else) to target Vulkan SPIR-V
563 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600564 if (Client == glslang::EShClientNone)
565 ClientVersion = glslang::EShTargetVulkan_1_0;
566 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600567 Options |= EOptionSpv;
568 Options |= EOptionVulkanRules;
569 Options |= EOptionLinkProgram;
570 };
571
572 // minimum needed (without overriding something else) to target OpenGL SPIR-V
573 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600574 if (Client == glslang::EShClientNone)
575 ClientVersion = glslang::EShTargetOpenGL_450;
576 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600577 Options |= EOptionSpv;
578 Options |= EOptionLinkProgram;
579 // undo a -H default to Vulkan
580 Options &= ~EOptionVulkanRules;
581 };
582
Neil Roberts16f53472018-03-20 17:30:53 +0100583 const auto getUniformOverride = [getStringOperand]() {
584 const char *arg = getStringOperand("-u<name>:<location>");
585 const char *split = strchr(arg, ':');
586 if (split == NULL) {
587 printf("%s: missing location\n", arg);
588 exit(EFailUsage);
589 }
590 errno = 0;
591 int location = ::strtol(split + 1, NULL, 10);
592 if (errno) {
593 printf("%s: invalid location\n", arg);
594 exit(EFailUsage);
595 }
596 return std::make_pair(std::string(arg, split - arg), location);
597 };
598
John Kessenichc178f0a2017-06-23 10:58:31 -0600599 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000600 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000601 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600602 case '-':
603 {
604 std::string lowerword(argv[0]+2);
605 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
606
607 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600608 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600609 lowerword == "auto-map-binding" ||
610 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600611 Options |= EOptionAutoMapBindings;
612 } else if (lowerword == "auto-map-locations" || // synonyms
613 lowerword == "aml") {
614 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100615 } else if (lowerword == "uniform-base") {
616 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700617 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100618 uniformBase = ::strtol(argv[1], NULL, 10);
619 bumpArg();
620 break;
John Kessenich6353d552017-06-23 11:11:09 -0600621 } else if (lowerword == "client") {
622 if (argc > 1) {
623 if (strcmp(argv[1], "vulkan100") == 0)
624 setVulkanSpv();
625 else if (strcmp(argv[1], "opengl100") == 0)
626 setOpenGlSpv();
627 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700628 Error("expects vulkan100 or opengl100", lowerword.c_str());
629 } else
630 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600631 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700632 } else if (lowerword == "define-macro" ||
633 lowerword == "d") {
634 if (argc > 1)
635 UserPreamble.addDef(argv[1]);
636 else
637 Error("expects <name[=def]>", argv[0]);
638 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200639 } else if (lowerword == "dump-builtin-symbols") {
640 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600641 } else if (lowerword == "entry-point") {
642 entryPointName = argv[1];
643 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700644 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600645 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600646 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
647 lowerword == "flatten-uniform-array" ||
648 lowerword == "fua") {
649 Options |= EOptionFlattenUniformArrays;
650 } else if (lowerword == "hlsl-offsets") {
651 Options |= EOptionHlslOffsets;
652 } else if (lowerword == "hlsl-iomap" ||
653 lowerword == "hlsl-iomapper" ||
654 lowerword == "hlsl-iomapping") {
655 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800656 } else if (lowerword == "hlsl-enable-16bit-types") {
657 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700658 } else if (lowerword == "hlsl-dx9-compatible") {
659 HlslDX9compatible = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700660 } else if (lowerword == "invert-y" || // synonyms
661 lowerword == "iy") {
662 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600663 } else if (lowerword == "keep-uncalled" || // synonyms
664 lowerword == "ku") {
665 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600666 } else if (lowerword == "nan-clamp") {
667 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600668 } else if (lowerword == "no-storage-format" || // synonyms
669 lowerword == "nsf") {
670 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600671 } else if (lowerword == "relaxed-errors") {
672 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000673 } else if (lowerword == "reflect-strict-array-suffix") {
674 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000675 } else if (lowerword == "reflect-basic-array-suffix") {
676 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000677 } else if (lowerword == "reflect-intermediate-io") {
678 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000679 } else if (lowerword == "reflect-separate-buffers") {
680 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000681 } else if (lowerword == "reflect-all-block-variables") {
682 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000683 } else if (lowerword == "reflect-unwrap-io-blocks") {
684 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800685 } else if (lowerword == "reflect-all-io-variables") {
686 ReflectOptions |= EShReflectionAllIOVariables;
687 } else if (lowerword == "reflect-shared-std140-ubo") {
688 ReflectOptions |= EShReflectionSharedStd140UBO;
689 } else if (lowerword == "reflect-shared-std140-ssbo") {
690 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600691 } else if (lowerword == "resource-set-bindings" || // synonyms
692 lowerword == "resource-set-binding" ||
693 lowerword == "rsb") {
694 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
greg-lunarg4e064ee2021-03-15 11:26:11 -0600695 } else if (lowerword == "set-block-storage" ||
696 lowerword == "sbs") {
697 ProcessBlockStorage(argc, argv, blockStorageOverrides);
698 } else if (lowerword == "set-atomic-counter-block" ||
699 lowerword == "sacb") {
700 ProcessGlobalBlockSettings(argc, argv, &atomicCounterBlockName, &atomicCounterBlockSet, nullptr);
701 setGlobalBufferBlock = true;
702 } else if (lowerword == "set-default-uniform-block" ||
703 lowerword == "sdub") {
704 ProcessGlobalBlockSettings(argc, argv, &globalUniformName, &globalUniformSet, &globalUniformBinding);
705 setGlobalUniformBlock = true;
steve-lunarg9088be42016-11-01 10:31:42 -0600706 } else if (lowerword == "shift-image-bindings" || // synonyms
707 lowerword == "shift-image-binding" ||
708 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600709 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600710 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600711 lowerword == "shift-sampler-binding" ||
712 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600713 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600714 } else if (lowerword == "shift-uav-bindings" || // synonyms
715 lowerword == "shift-uav-binding" ||
716 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600717 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600718 } else if (lowerword == "shift-texture-bindings" || // synonyms
719 lowerword == "shift-texture-binding" ||
720 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600721 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600722 } else if (lowerword == "shift-ubo-bindings" || // synonyms
723 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600724 lowerword == "shift-cbuffer-bindings" ||
725 lowerword == "shift-cbuffer-binding" ||
726 lowerword == "sub" ||
727 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600728 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700729 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
730 lowerword == "shift-ssbo-binding" ||
731 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600732 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600733 } else if (lowerword == "source-entrypoint" || // synonyms
734 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600735 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700736 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600737 sourceEntryPointName = argv[1];
738 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600739 break;
John Kesseniche2156222018-06-11 18:12:15 -0600740 } else if (lowerword == "spirv-dis") {
741 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600742 } else if (lowerword == "spirv-val") {
743 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200744 } else if (lowerword == "stdin") {
745 Options |= EOptionStdin;
746 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600747 } else if (lowerword == "suppress-warnings") {
748 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600749 } else if (lowerword == "target-env") {
750 if (argc > 1) {
751 if (strcmp(argv[1], "vulkan1.0") == 0) {
752 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600753 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700754 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
755 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600756 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700757 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
758 setVulkanSpv();
759 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600760 } else if (strcmp(argv[1], "opengl") == 0) {
761 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600762 ClientVersion = glslang::EShTargetOpenGL_450;
763 } else if (strcmp(argv[1], "spirv1.0") == 0) {
764 TargetLanguage = glslang::EShTargetSpv;
765 TargetVersion = glslang::EShTargetSpv_1_0;
766 } else if (strcmp(argv[1], "spirv1.1") == 0) {
767 TargetLanguage = glslang::EShTargetSpv;
768 TargetVersion = glslang::EShTargetSpv_1_1;
769 } else if (strcmp(argv[1], "spirv1.2") == 0) {
770 TargetLanguage = glslang::EShTargetSpv;
771 TargetVersion = glslang::EShTargetSpv_1_2;
772 } else if (strcmp(argv[1], "spirv1.3") == 0) {
773 TargetLanguage = glslang::EShTargetSpv;
774 TargetVersion = glslang::EShTargetSpv_1_3;
775 } else if (strcmp(argv[1], "spirv1.4") == 0) {
776 TargetLanguage = glslang::EShTargetSpv;
777 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600778 } else if (strcmp(argv[1], "spirv1.5") == 0) {
779 TargetLanguage = glslang::EShTargetSpv;
780 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600781 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700782 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600783 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600784 }
785 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700786 } else if (lowerword == "undef-macro" ||
787 lowerword == "u") {
788 if (argc > 1)
789 UserPreamble.addUndef(argv[1]);
790 else
791 Error("expects <name>", argv[0]);
792 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800793 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700794 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800795 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600796 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700797 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600798 variableName = argv[1];
799 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800800 break;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200801 } else if (lowerword == "quiet") {
802 beQuiet = true;
Georg Lehmannf0502092021-04-20 00:22:15 +0200803 } else if (lowerword == "depfile") {
804 if (argc <= 1)
805 Error("no <depfile-name> provided", lowerword.c_str());
806 depencyFileName = argv[1];
807 bumpArg();
John Kessenichc6c80a62018-03-05 22:23:17 -0700808 } else if (lowerword == "version") {
809 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800810 } else if (lowerword == "help") {
811 usage();
812 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600813 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700814 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600815 }
816 }
817 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600818 case 'C':
819 Options |= EOptionCascadingErrors;
820 break;
821 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600822 if (argv[0][2] == 0)
823 Options |= EOptionReadHlsl;
824 else
John Kessenich6f988922020-01-07 07:03:11 -0700825 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600826 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100827 case 'u':
828 uniformLocationOverrides.push_back(getUniformOverride());
829 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600830 case 'E':
831 Options |= EOptionOutputPreprocessed;
832 break;
833 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600834 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600835 setOpenGlSpv();
836 if (argv[0][2] != 0)
837 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600838 if (ClientInputSemanticsVersion != 100)
839 Error("unknown client version for -G, should be 100");
John Kessenich6263fb12017-06-14 15:52:44 -0600840 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000841 case 'H':
842 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600843 if ((Options & EOptionSpv) == 0) {
844 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600845 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600846 }
847 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600848 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600849 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600850 break;
GregFcd1f1692017-09-21 18:40:22 -0600851 case 'O':
852 if (argv[0][2] == 'd')
853 Options |= EOptionOptimizeDisable;
854 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600855#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600856 Options |= EOptionOptimizeSize;
857#else
858 Error("-Os not available; optimizer not linked");
859#endif
860 else
861 Error("unknown -O option");
862 break;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600863 case 'R':
864 VulkanRulesRelaxed = true;
865 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400866 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600867 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400868 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600869 shaderStageName = argv[1];
870 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400871 break;
John Kessenicha9313662017-06-15 10:40:49 -0600872 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700873 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600874 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600875 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600876 setVulkanSpv();
877 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500878 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600879 if (ClientInputSemanticsVersion != 100)
880 Error("unknown client version for -V, should be 100");
John Kessenichc555ddd2015-06-17 02:38:44 +0000881 break;
John Kessenich05a70632013-09-17 19:26:08 +0000882 case 'c':
883 Options |= EOptionDumpConfig;
884 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000885 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700886 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
887 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
888 Options |= EOptionDumpBareVersion;
889 else
890 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000891 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700892 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700893 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600894 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600895 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600896 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700897 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700898 case 'f':
899 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
900 targetHlslFunctionality1 = true;
901 else
902 Error("-f: expected hlsl_functionality1");
903 break;
John Kessenich121853f2017-05-31 17:11:16 -0600904 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400905 // Override previous -g or -g0 argument
906 stripDebugInfo = false;
907 Options &= ~EOptionDebug;
908 if (argv[0][2] == '0')
909 stripDebugInfo = true;
910 else
911 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600912 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600913 case 'h':
914 usage();
915 break;
John Kessenich05a70632013-09-17 19:26:08 +0000916 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000917 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000918 break;
919 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000920 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000921 break;
922 case 'm':
923 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000924 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600925 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600926 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600927 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600928 binaryFileName = argv[1];
929 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600930 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000931 case 'q':
932 Options |= EOptionDumpReflection;
933 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000934 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000935 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000936 break;
937 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000938 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000939 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000940 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200941 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000942 break;
John Kessenich319de232013-12-04 04:43:40 +0000943 case 'v':
944 Options |= EOptionDumpVersions;
945 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000946 case 'w':
947 Options |= EOptionSuppressWarnings;
948 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500949 case 'x':
950 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500951 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000952 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700953 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600954 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000955 }
John Kessenich38f3b892013-09-06 19:52:57 +0000956 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000957 std::string name(argv[0]);
958 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200959 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000960 }
John Kessenich38f3b892013-09-06 19:52:57 +0000961 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000962 }
963
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200964 // Make sure that -S is always specified if --stdin is specified
965 if ((Options & EOptionStdin) && shaderStageName == nullptr)
966 Error("must provide -S when --stdin is given");
967
John Kessenich68d78fd2015-07-12 19:28:10 -0600968 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600969 // Or things that require linking
970 if (Options & EOptionOutputPreprocessed) {
971 if (Options & EOptionLinkProgram)
972 Error("can't use -E when linking is selected");
973 if (Options & EOptionDumpReflection)
974 Error("reflection requires linking, which can't be used when -E when is selected");
975 }
976
977 // reflection requires linking
978 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
979 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000980
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500981 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600982 if (binaryFileName && (Options & EOptionSpv) == 0)
983 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600984
985 if ((Options & EOptionFlattenUniformArrays) != 0 &&
986 (Options & EOptionReadHlsl) == 0)
987 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600988
Jeremy Hayesb73afa82021-01-05 15:54:41 -0700989 if ((Options & EOptionReadHlsl) && (Client == glslang::EShClientOpenGL)) {
990 Error("Using HLSL input under OpenGL semantics is not currently supported.");
991 }
992
John Kessenich8717a5d2018-10-26 10:12:32 -0600993 // rationalize client and target language
994 if (TargetLanguage == glslang::EShTargetNone) {
995 switch (ClientVersion) {
996 case glslang::EShTargetVulkan_1_0:
997 TargetLanguage = glslang::EShTargetSpv;
998 TargetVersion = glslang::EShTargetSpv_1_0;
999 break;
1000 case glslang::EShTargetVulkan_1_1:
1001 TargetLanguage = glslang::EShTargetSpv;
1002 TargetVersion = glslang::EShTargetSpv_1_3;
1003 break;
John Kessenich273d3a52020-01-15 00:10:41 -07001004 case glslang::EShTargetVulkan_1_2:
1005 TargetLanguage = glslang::EShTargetSpv;
1006 TargetVersion = glslang::EShTargetSpv_1_5;
1007 break;
John Kessenich8717a5d2018-10-26 10:12:32 -06001008 case glslang::EShTargetOpenGL_450:
1009 TargetLanguage = glslang::EShTargetSpv;
1010 TargetVersion = glslang::EShTargetSpv_1_0;
1011 break;
1012 default:
1013 break;
1014 }
1015 }
1016 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
1017 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +00001018}
1019
John Kessenich68d78fd2015-07-12 19:28:10 -06001020//
1021// Translate the meaningful subset of command-line options to parser-behavior options.
1022//
John Kessenichb0a7eb52013-11-07 17:44:20 +00001023void SetMessageOptions(EShMessages& messages)
1024{
1025 if (Options & EOptionRelaxedErrors)
1026 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
1027 if (Options & EOptionIntermediate)
1028 messages = (EShMessages)(messages | EShMsgAST);
1029 if (Options & EOptionSuppressWarnings)
1030 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -06001031 if (Options & EOptionSpv)
1032 messages = (EShMessages)(messages | EShMsgSpvRules);
1033 if (Options & EOptionVulkanRules)
1034 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001035 if (Options & EOptionOutputPreprocessed)
1036 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -07001037 if (Options & EOptionReadHlsl)
1038 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -06001039 if (Options & EOptionCascadingErrors)
1040 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -07001041 if (Options & EOptionKeepUncalled)
1042 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -06001043 if (Options & EOptionHlslOffsets)
1044 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -06001045 if (Options & EOptionDebug)
1046 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +08001047 if (HlslEnable16BitTypes)
1048 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -06001049 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
1050 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -07001051 if (HlslDX9compatible)
1052 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +02001053 if (DumpBuiltinSymbols)
1054 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +00001055}
1056
John Kessenich68d78fd2015-07-12 19:28:10 -06001057//
John Kessenich69f4b512013-09-04 21:19:27 +00001058// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +00001059//
Juan Lopeza558b262017-04-02 23:04:00 +02001060void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001061{
John Kessenich7f0bcfd2018-04-05 19:00:01 -06001062 if (Options & EOptionDebug)
1063 Error("cannot generate debug information unless linking to generate code");
1064
John Kessenich38f3b892013-09-06 19:52:57 +00001065 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001066 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -06001067 if (worklist.remove(workItem)) {
1068 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
1069 if (compiler == nullptr)
1070 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001071
John Kesseniche7f9cae2018-07-12 15:11:07 -06001072 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001073
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001074 if (! (Options & EOptionSuppressInfolog))
1075 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001076
John Kesseniche7f9cae2018-07-12 15:11:07 -06001077 ShDestruct(compiler);
1078 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001079 } else {
1080 while (worklist.remove(workItem)) {
1081 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
1082 if (compiler == 0)
1083 return;
1084
1085 CompileFile(workItem->name.c_str(), compiler);
1086
1087 if (! (Options & EOptionSuppressInfolog))
1088 workItem->results = ShGetInfoLog(compiler);
1089
1090 ShDestruct(compiler);
1091 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001092 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001093}
1094
John Kessenich6626cad2015-06-19 05:14:19 +00001095// Outputs the given string, but only if it is non-null and non-empty.
1096// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001097void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +00001098{
1099 if (str && str[0]) {
1100 puts(str);
1101 }
1102}
1103
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001104// Outputs the given string to stderr, but only if it is non-null and non-empty.
1105// This prevents erroneous newlines from appearing.
1106void StderrIfNonEmpty(const char* str)
1107{
John Kessenich04acb1b2017-06-14 17:36:50 -06001108 if (str && str[0])
1109 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001110}
1111
John Kessenichc57b2a92016-01-16 15:30:03 -07001112// Simple bundling of what makes a compilation unit for ease in passing around,
1113// and separation of handling file IO versus API (programmatic) compilation.
1114struct ShaderCompUnit {
1115 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001116 static const int maxCount = 1;
1117 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -06001118 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -06001119 std::string fileName[maxCount]; // hold's the memory, but...
1120 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -04001121
John Kessenich04acb1b2017-06-14 17:36:50 -06001122 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -04001123
John Kessenich04acb1b2017-06-14 17:36:50 -06001124 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -04001125 {
1126 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001127 count = rhs.count;
1128 for (int i = 0; i < count; ++i) {
1129 fileName[i] = rhs.fileName[i];
1130 text[i] = rhs.text[i];
1131 fileNameList[i] = rhs.fileName[i].c_str();
1132 }
dankbakerafe6e9c2016-08-21 12:29:08 -04001133 }
1134
John Kessenicha9313662017-06-15 10:40:49 -06001135 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -06001136 {
1137 assert(count < maxCount);
1138 fileName[count] = ifileName;
1139 text[count] = itext;
1140 fileNameList[count] = fileName[count].c_str();
1141 ++count;
1142 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001143};
1144
Georg Lehmannf0502092021-04-20 00:22:15 +02001145// Writes a depfile similar to gcc -MMD foo.c
1146bool writeDepFile(std::string depfile, std::vector<std::string>& binaryFiles, const std::vector<std::string>& sources)
1147{
1148 std::ofstream file(depfile);
1149 if (file.fail())
1150 return false;
1151
1152 for (auto it = binaryFiles.begin(); it != binaryFiles.end(); it++) {
1153 file << *it << ":";
1154 for (auto it = sources.begin(); it != sources.end(); it++) {
1155 file << " " << *it;
1156 }
1157 file << std::endl;
1158 }
1159 return true;
1160}
1161
John Kessenich69f4b512013-09-04 21:19:27 +00001162//
John Kessenichc57b2a92016-01-16 15:30:03 -07001163// For linking mode: Will independently parse each compilation unit, but then put them
1164// in the same program and link them together, making at most one linked module per
1165// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +00001166//
1167// Uses the new C++ interface instead of the old handle-based interface.
1168//
John Kessenichc57b2a92016-01-16 15:30:03 -07001169
1170void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +00001171{
1172 // keep track of what to free
1173 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001174
John Kessenich69f4b512013-09-04 21:19:27 +00001175 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001176 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001177
Georg Lehmannf0502092021-04-20 00:22:15 +02001178 DirStackFileIncluder includer;
1179 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1180 includer.pushExternalLocalDirectory(dir); });
1181
1182 std::vector<std::string> sources;
1183
John Kessenich69f4b512013-09-04 21:19:27 +00001184 //
1185 // Per-shader processing...
1186 //
1187
John Kessenich5b0f13a2013-11-01 03:08:40 +00001188 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001189 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1190 const auto &compUnit = *it;
Georg Lehmannf0502092021-04-20 00:22:15 +02001191 for (int i = 0; i < compUnit.count; i++) {
1192 sources.push_back(compUnit.fileNameList[i]);
1193 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001194 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001195 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001196 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001197 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001198 if (sourceEntryPointName) {
1199 if (entryPointName == nullptr)
1200 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1201 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001202 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001203 }
John Kessenicha9313662017-06-15 10:40:49 -06001204 if (UserPreamble.isSet())
1205 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -06001206 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001207
John Kessenich155d3512019-08-08 23:29:20 -06001208#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001209 // Set IO mapper binding shift values
1210 for (int r = 0; r < glslang::EResCount; ++r) {
1211 const glslang::TResourceType res = glslang::TResourceType(r);
1212
1213 // Set base bindings
1214 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001215
LoopDawg08a14422017-10-17 19:27:14 -06001216 // Set bindings for particular resource sets
1217 // TODO: use a range based for loop here, when available in all environments.
1218 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1219 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001220 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001221 }
steve-lunargcce8d482016-10-14 18:36:42 -06001222 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001223 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001224
1225 if (Options & EOptionAutoMapBindings)
1226 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001227
John Kessenich71facdf2017-05-17 18:28:19 -06001228 if (Options & EOptionAutoMapLocations)
1229 shader->setAutoMapLocations(true);
1230
Neil Roberts16f53472018-03-20 17:30:53 +01001231 for (auto& uniOverride : uniformLocationOverrides) {
1232 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1233 uniOverride.second);
1234 }
1235
Neil Robertsb0f3d792018-03-20 17:41:05 +01001236 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001237#endif
1238
greg-lunarg4e064ee2021-03-15 11:26:11 -06001239 if (VulkanRulesRelaxed) {
1240 for (auto& storageOverride : blockStorageOverrides) {
1241 shader->addBlockStorageOverride(storageOverride.first.c_str(),
1242 storageOverride.second);
1243 }
1244
1245 if (setGlobalBufferBlock) {
1246 shader->setAtomicCounterBlockName(atomicCounterBlockName.c_str());
1247 shader->setAtomicCounterBlockSet(atomicCounterBlockSet);
1248 }
1249
1250 if (setGlobalUniformBlock) {
1251 shader->setGlobalUniformBlockName(globalUniformName.c_str());
1252 shader->setGlobalUniformSet(globalUniformSet);
1253 shader->setGlobalUniformBinding(globalUniformBinding);
1254 }
1255 }
1256
John Kessenich155d3512019-08-08 23:29:20 -06001257 shader->setNanMinMaxClamp(NaNClamp);
1258
1259#ifdef ENABLE_HLSL
1260 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1261 if (Options & EOptionHlslIoMapping)
1262 shader->setHlslIoMapping(true);
1263#endif
1264
1265 if (Options & EOptionInvertY)
1266 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001267
John Kessenich4be4aeb2017-06-23 10:50:22 -06001268 // Set up the environment, some subsettings take precedence over earlier
1269 // ways of setting things.
1270 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001271 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1272 : glslang::EShSourceGlsl,
1273 compUnit.stage, Client, ClientInputSemanticsVersion);
1274 shader->setEnvClient(Client, ClientVersion);
1275 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001276#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001277 if (targetHlslFunctionality1)
1278 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001279#endif
greg-lunarg4e064ee2021-03-15 11:26:11 -06001280 if (VulkanRulesRelaxed)
1281 shader->setEnvInputVulkanRulesRelaxed();
John Kessenich4be4aeb2017-06-23 10:50:22 -06001282 }
1283
John Kessenich69f4b512013-09-04 21:19:27 +00001284 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001285
John Kessenich4be4aeb2017-06-23 10:50:22 -06001286 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001287
John Kessenichdeec1932019-08-13 08:00:30 -06001288#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001289 if (Options & EOptionOutputPreprocessed) {
1290 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001291 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001292 PutsIfNonEmpty(str.c_str());
1293 } else {
1294 CompileFailed = true;
1295 }
1296 StderrIfNonEmpty(shader->getInfoLog());
1297 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001298 continue;
1299 }
John Kessenichdeec1932019-08-13 08:00:30 -06001300#endif
John Kessenich086febc2018-10-25 12:43:02 -06001301
John Kessenich3494b4d2017-05-22 15:00:42 -06001302 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001303 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001304
John Kessenich69f4b512013-09-04 21:19:27 +00001305 program.addShader(shader);
1306
John Kessenichc57b2a92016-01-16 15:30:03 -07001307 if (! (Options & EOptionSuppressInfolog) &&
1308 ! (Options & EOptionMemoryLeakMode)) {
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001309 if (!beQuiet)
1310 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001311 PutsIfNonEmpty(shader->getInfoLog());
1312 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001313 }
John Kessenich69f4b512013-09-04 21:19:27 +00001314 }
1315
1316 //
1317 // Program-level processing...
1318 //
1319
John Kessenich4d65ee32016-03-12 18:17:47 -07001320 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001321 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001322 LinkFailed = true;
1323
John Kessenichb6d3ee52019-08-06 02:20:45 -06001324#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001325 // Map IO
1326 if (Options & EOptionSpv) {
1327 if (!program.mapIO())
1328 LinkFailed = true;
1329 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001330#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001331
John Kessenich4d65ee32016-03-12 18:17:47 -07001332 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001333 if (! (Options & EOptionSuppressInfolog) &&
1334 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001335 PutsIfNonEmpty(program.getInfoLog());
1336 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001337 }
1338
John Kessenichb6d3ee52019-08-06 02:20:45 -06001339#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001340 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001341 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001342 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001343 program.dumpReflection();
1344 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001345#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001346
Georg Lehmannf0502092021-04-20 00:22:15 +02001347 std::vector<std::string> outputFiles;
1348
John Kessenich4d65ee32016-03-12 18:17:47 -07001349 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001350 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001351 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001352 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001353 else {
1354 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001355 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001356 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001357 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001358 glslang::SpvOptions spvOptions;
1359 if (Options & EOptionDebug)
1360 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001361 else if (stripDebugInfo)
1362 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001363 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1364 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001365 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001366 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001367 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001368
1369 // Dump the spv to a file or stdout, etc., but only if not doing
1370 // memory/perf testing, as it's not internal to programmatic use.
1371 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001372 printf("%s", logger.getAllMessages().c_str());
1373 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001374 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001375 } else {
1376 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1377 }
Georg Lehmannf0502092021-04-20 00:22:15 +02001378
1379 outputFiles.push_back(GetBinaryName((EShLanguage)stage));
John Kessenich23d27752019-07-28 02:12:10 -06001380#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001381 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001382 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001383#endif
John Kessenichacba7722015-03-04 03:48:38 +00001384 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001385 }
John Kessenich92f90382014-07-28 04:21:04 +00001386 }
1387 }
1388 }
1389
Georg Lehmannf0502092021-04-20 00:22:15 +02001390 if (depencyFileName && !(CompileFailed || LinkFailed)) {
1391 std::set<std::string> includedFiles = includer.getIncludedFiles();
1392 sources.insert(sources.end(), includedFiles.begin(), includedFiles.end());
1393
1394 writeDepFile(depencyFileName, outputFiles, sources);
1395 }
1396
John Kessenich5b0f13a2013-11-01 03:08:40 +00001397 // Free everything up, program has to go before the shaders
1398 // because it might have merged stuff from the shaders, and
1399 // the stuff from the shaders has to have its destructors called
1400 // before the pools holding the memory in the shaders is freed.
1401 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001402 while (shaders.size() > 0) {
1403 delete shaders.back();
1404 shaders.pop_back();
1405 }
John Kessenich69f4b512013-09-04 21:19:27 +00001406}
1407
John Kessenichc57b2a92016-01-16 15:30:03 -07001408//
1409// Do file IO part of compile and link, handing off the pure
1410// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1411// be put in a loop for testing memory footprint and performance.
1412//
1413// This is just for linking mode: meaning all the shaders will be put into the
1414// the same program linked together.
1415//
1416// This means there are a limited number of work items (not multi-threading mode)
1417// and that the point is testing at the linking level. Hence, to enable
1418// performance and memory testing, the actual compile/link can be put in
1419// a loop, independent of processing the work items and file IO.
1420//
Juan Lopeza558b262017-04-02 23:04:00 +02001421void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001422{
1423 std::vector<ShaderCompUnit> compUnits;
1424
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001425 // If this is using stdin, we can't really detect multiple different file
1426 // units by input type. We need to assume that we're just being given one
1427 // file of a certain type.
1428 if ((Options & EOptionStdin) != 0) {
1429 ShaderCompUnit compUnit(FindLanguage("stdin"));
1430 std::istreambuf_iterator<char> begin(std::cin), end;
1431 std::string tempString(begin, end);
1432 char* fileText = strdup(tempString.c_str());
1433 std::string fileName = "stdin";
1434 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001435 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001436 } else {
1437 // Transfer all the work items from to a simple list of
1438 // of compilation units. (We don't care about the thread
1439 // work-item distribution properties in this path, which
1440 // is okay due to the limited number of shaders, know since
1441 // they are all getting linked together.)
1442 glslang::TWorkItem* workItem;
1443 while (Worklist.remove(workItem)) {
1444 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1445 char* fileText = ReadFileData(workItem->name.c_str());
1446 if (fileText == nullptr)
1447 usage();
1448 compUnit.addString(workItem->name, fileText);
1449 compUnits.push_back(compUnit);
1450 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001451 }
1452
1453 // Actual call to programmatic processing of compile and link,
1454 // in a loop for testing memory and performance. This part contains
1455 // all the perf/memory that a programmatic consumer will care about.
1456 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1457 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1458 CompileAndLinkShaderUnits(compUnits);
1459
1460 if (Options & EOptionMemoryLeakMode)
1461 glslang::OS_DumpMemoryCounters();
1462 }
1463
John Kessenicha9313662017-06-15 10:40:49 -06001464 // free memory from ReadFileData, which got stored in a const char*
1465 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001466 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001467 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001468}
1469
John Kessenich94f28eb2017-11-13 01:32:06 -07001470int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001471{
Juan Lopeza558b262017-04-02 23:04:00 +02001472 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001473 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001474 assert(item);
1475 workList.add(item.get());
1476 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001477
John Kessenich23d27752019-07-28 02:12:10 -06001478#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001479 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001480 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001481 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001482 return ESuccess;
1483 }
John Kessenich23d27752019-07-28 02:12:10 -06001484#endif
John Kessenich05a70632013-09-17 19:26:08 +00001485
John Kessenichc6c80a62018-03-05 22:23:17 -07001486 if (Options & EOptionDumpBareVersion) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001487 printf("%d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1488 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenichc6c80a62018-03-05 22:23:17 -07001489 if (workList.empty())
1490 return ESuccess;
1491 } else if (Options & EOptionDumpVersions) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001492 printf("Glslang Version: %d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR,
1493 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenich319de232013-12-04 04:43:40 +00001494 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1495 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001496 std::string spirvVersion;
1497 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001498 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001499 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001500 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001501 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001502 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1503 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001504 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001505 return ESuccess;
1506 }
1507
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001508 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001509 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001510 }
1511
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001512 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001513 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1514 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001515 }
1516
John Kessenich05a70632013-09-17 19:26:08 +00001517 ProcessConfigFile();
1518
John Kessenich086febc2018-10-25 12:43:02 -06001519 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001520 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001521
John Kessenich69f4b512013-09-04 21:19:27 +00001522 //
1523 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001524 // 1) linking all arguments together, single-threaded, new C++ interface
1525 // 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 +00001526 //
John Kessenich086febc2018-10-25 12:43:02 -06001527 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001528 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001529 glslang::InitializeProcess(); // also test reference counting of users
1530 glslang::InitializeProcess(); // also test reference counting of users
1531 glslang::FinalizeProcess(); // also test reference counting of users
1532 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001533 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001534 glslang::FinalizeProcess();
1535 } else {
1536 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001537 ShInitialize(); // also test reference counting of users
1538 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001539
Juan Lopeza558b262017-04-02 23:04:00 +02001540 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001541
John Kesseniche2c15b42017-11-16 22:48:41 -07001542 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001543 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001544 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001545 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001546 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001547 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001548 return EFailThreadCreate;
1549 }
John Kessenicha0af4732012-12-12 21:15:54 +00001550 }
Juan Lopeza558b262017-04-02 23:04:00 +02001551
1552 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001553 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001554 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001555
1556 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001557 for (size_t w = 0; w < WorkItems.size(); ++w) {
1558 if (WorkItems[w]) {
1559 if (printShaderNames || WorkItems[w]->results.size() > 0)
1560 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1561 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001562 }
1563 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001564
1565 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001566 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001567
John Kessenichc999ba22013-11-07 23:33:24 +00001568 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001569 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001570 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001571 return EFailLink;
1572
1573 return 0;
1574}
1575
John Kessenich94f28eb2017-11-13 01:32:06 -07001576int C_DECL main(int argc, char* argv[])
1577{
1578 ProcessArguments(WorkItems, argc, argv);
1579
1580 int ret = 0;
1581
1582 // Loop over the entire init/finalize cycle to watch memory changes
1583 const int iterations = 1;
1584 if (iterations > 1)
1585 glslang::OS_DumpMemoryCounters();
1586 for (int i = 0; i < iterations; ++i) {
1587 ret = singleMain();
1588 if (iterations > 1)
1589 glslang::OS_DumpMemoryCounters();
1590 }
1591
1592 return ret;
1593}
1594
John Kessenicha0af4732012-12-12 21:15:54 +00001595//
1596// Deduce the language from the filename. Files must end in one of the
1597// following extensions:
1598//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001599// .vert = vertex
1600// .tesc = tessellation control
1601// .tese = tessellation evaluation
1602// .geom = geometry
1603// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001604// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001605// .rgen = ray generation
1606// .rint = ray intersection
1607// .rahit = ray any hit
1608// .rchit = ray closest hit
1609// .rmiss = ray miss
1610// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001611// .mesh = mesh
1612// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001613// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1614// where <stage> is one of the stages listed above.
1615//
1616EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001617{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001618 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001619 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001620 stageName = shaderStageName;
1621 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001622 // Note: "first" extension means "first from the end", i.e.
1623 // if the file is named foo.vert.glsl, then "glsl" is first,
1624 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001625 size_t firstExtStart = name.find_last_of(".");
1626 bool hasFirstExt = firstExtStart != std::string::npos;
1627 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1628 bool hasSecondExt = secondExtStart != std::string::npos;
1629 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1630 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001631 if (usesUnifiedExt && firstExt == "hlsl")
1632 Options |= EOptionReadHlsl;
1633 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001634 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001635 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001636 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001637 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001638 usage();
1639 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001640 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001641 } else
1642 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001643
John Kessenichfccbb8b2018-04-17 17:24:03 -06001644 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001645 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001646 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001647 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001648 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001649 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001650 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001651 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001652 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001653 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001654 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001655 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001656 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001657 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001658 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001659 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001660 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001661 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001662 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001663 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001664 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001665 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001666 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001667 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001668 else if (stageName == "mesh")
1669 return EShLangMeshNV;
1670 else if (stageName == "task")
1671 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001672
1673 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001674 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001675}
1676
John Kessenicha0af4732012-12-12 21:15:54 +00001677//
John Kessenichecba76f2017-01-06 00:34:48 -07001678// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001679// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001680//
John Kessenich51cdd902014-02-18 23:37:57 +00001681void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001682{
John Kessenichca3457f2015-05-18 01:59:45 +00001683 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001684 char* shaderString;
1685 if ((Options & EOptionStdin) != 0) {
1686 std::istreambuf_iterator<char> begin(std::cin), end;
1687 std::string tempString(begin, end);
1688 shaderString = strdup(tempString.c_str());
1689 } else {
1690 shaderString = ReadFileData(fileName);
1691 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001692
1693 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001694 int* lengths = new int[1];
1695 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001696
John Kessenich52ac67e2013-05-05 23:46:22 +00001697 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001698 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001699
John Kessenicha9313662017-06-15 10:40:49 -06001700 if (UserPreamble.isSet())
1701 Error("-D and -U options require -l (linking)\n");
1702
John Kessenich94a81fb2013-08-31 02:41:30 +00001703 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1704 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001705 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001706 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001707 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001708 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001709 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001710 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1711 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001712 }
John Kessenicha0af4732012-12-12 21:15:54 +00001713
John Kessenich94a81fb2013-08-31 02:41:30 +00001714 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001715 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001716 }
John Kessenicha0af4732012-12-12 21:15:54 +00001717
John Kessenich41cf6b52013-06-25 18:10:05 +00001718 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001719 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001720
John Kessenichc999ba22013-11-07 23:33:24 +00001721 if (ret == 0)
1722 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001723}
1724
John Kessenicha0af4732012-12-12 21:15:54 +00001725//
1726// print usage to stdout
1727//
1728void usage()
1729{
John Kessenich319de232013-12-04 04:43:40 +00001730 printf("Usage: glslangValidator [option]... [file]...\n"
1731 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001732 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1733 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001734 " (see -c option below for generating a template)\n"
1735 " .vert for a vertex shader\n"
1736 " .tesc for a tessellation control shader\n"
1737 " .tese for a tessellation evaluation shader\n"
1738 " .geom for a geometry shader\n"
1739 " .frag for a fragment shader\n"
1740 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001741 " .mesh for a mesh shader\n"
1742 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001743 " .rgen for a ray generation shader\n"
1744 " .rint for a ray intersection shader\n"
1745 " .rahit for a ray any hit shader\n"
1746 " .rchit for a ray closest hit shader\n"
1747 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001748 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001749 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1750 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001751 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001752 "Options:\n"
1753 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001754 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001755 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1756 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001757 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001758 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001759 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001760 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001761 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001762 " which will appear in #define GL_SPIRV ver;\n"
1763 " '--client opengl100' is the same as -G100;\n"
Jeremy Hayesb73afa82021-01-05 15:54:41 -07001764 " a '--target-env' for OpenGL will also imply '-G';\n"
1765 " currently only supports GLSL\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001766 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001767 " -I<dir> add dir to the include search path; includer's directory\n"
1768 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001769 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1770 " -Os optimizes SPIR-V to minimize size\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001771 " -R use relaxed verification rules for generating Vulkan SPIR-V,\n"
1772 " allowing the use of default uniforms, atomic_uints, and\n"
1773 " gl_VertexID and gl_InstanceID keywords.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001774 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001775 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001776 " -U<name> | --undef-macro <name> | --U <name>\n"
1777 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001778 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001779 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001780 " 'ver', when present, is the version of the input semantics,\n"
1781 " which will appear in #define VULKAN ver\n"
1782 " '--client vulkan100' is the same as -V100\n"
1783 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001784 " -c configuration dump;\n"
1785 " creates the default configuration file (redirect to a .conf file)\n"
1786 " -d default to desktop (#version 110) when there is no shader #version\n"
1787 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001788 " -e <name> | --entry-point <name>\n"
1789 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001790 " -f{hlsl_functionality1}\n"
1791 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001792 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001793 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001794 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001795 " -h print this usage message\n"
1796 " -i intermediate tree (glslang AST) is printed out\n"
1797 " -l link all input files together to form a single module\n"
1798 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001799 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001800 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001801 " -r | --relaxed-errors"
1802 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001803 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001804 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001805 " -v | --version\n"
1806 " print version strings\n"
1807 " -w | --suppress-warnings\n"
1808 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001809 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001810 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001811 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001812 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1813 " without explicit bindings\n"
1814 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1815 " 'location' (fragile, not cross stage)\n"
1816 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Georg Lehmannf0502092021-04-20 00:22:15 +02001817 " --depfile <file> writes depfile for build systems\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001818 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001819 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1820 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1821 " scalars\n"
1822 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1823 " works independently of source language\n"
1824 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1825 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001826 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001827 " texture/sampler combo like DirectX9 would,\n"
1828 " and recognizes DirectX9-specific semantics\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001829 " --invert-y | --iy invert position.Y output in vertex shader\n"
1830 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001831 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001832 " --no-storage-format | --nsf use Unknown image format\n"
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001833 " --quiet do not print anything to stdout, unless\n"
1834 " requested by another option\n"
baldurk4513df92019-02-08 10:48:48 +00001835 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1836 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001837 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001838 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1839 " shaders rather than just vertex/fragment\n"
1840 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1841 " separately to uniforms\n"
1842 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1843 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001844 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1845 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001846 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001847 " set descriptor set and binding for\n"
1848 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001849 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001850 " set descriptor set for all resources\n"
1851 " --rsb synonym for --resource-set-binding\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001852 " --set-block-backing name {uniform|buffer|push_constant}\n"
1853 " changes the backing type of a uniform, buffer,\n"
1854 " or push_constant block declared in\n"
1855 " in the program, when using -R option.\n"
1856 " This can be used to change the backing\n"
1857 " for existing blocks as well as implicit ones\n"
1858 " such as 'gl_DefaultUniformBlock'.\n"
1859 " --sbs synonym for set-block-storage\n"
1860 " --set-atomic-counter-block name set\n"
1861 " set name, and descriptor set for\n"
1862 " atomic counter blocks, with -R opt\n"
1863 " --sacb synonym for set-atomic-counter-block\n"
1864 " --set-default-uniform-block name set binding\n"
1865 " set name, descriptor set, and binding for\n"
1866 " global default-uniform-block, with -R opt\n"
1867 " --sdub synonym for set-default-uniform-block\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001868 " --shift-image-binding [stage] num\n"
1869 " base binding number for images (uav)\n"
1870 " --shift-image-binding [stage] [num set]...\n"
1871 " per-descriptor-set shift values\n"
1872 " --sib synonym for --shift-image-binding\n"
1873 " --shift-sampler-binding [stage] num\n"
1874 " base binding number for samplers\n"
1875 " --shift-sampler-binding [stage] [num set]...\n"
1876 " per-descriptor-set shift values\n"
1877 " --ssb synonym for --shift-sampler-binding\n"
1878 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1879 " --shift-ssbo-binding [stage] [num set]...\n"
1880 " per-descriptor-set shift values\n"
1881 " --sbb synonym for --shift-ssbo-binding\n"
1882 " --shift-texture-binding [stage] num\n"
1883 " base binding number for textures\n"
1884 " --shift-texture-binding [stage] [num set]...\n"
1885 " per-descriptor-set shift values\n"
1886 " --stb synonym for --shift-texture-binding\n"
1887 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1888 " --shift-uav-binding [stage] [num set]...\n"
1889 " per-descriptor-set shift values\n"
1890 " --suavb synonym for --shift-uav-binding\n"
1891 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1892 " --shift-UBO-binding [stage] [num set]...\n"
1893 " per-descriptor-set shift values\n"
1894 " --sub synonym for --shift-UBO-binding\n"
1895 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1896 " --spirv-dis output standard-form disassembly; works only\n"
1897 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001898 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001899 " --source-entrypoint <name> the given shader source function is\n"
1900 " renamed to be the <name> given in -e\n"
1901 " --sep synonym for --source-entrypoint\n"
1902 " --stdin read from stdin instead of from a file;\n"
1903 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001904 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001905 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001906 " Set the execution environment that the\n"
1907 " generated code will be executed in.\n"
1908 " Defaults to:\n"
1909 " * vulkan1.0 under --client vulkan<ver>\n"
1910 " * opengl under --client opengl<ver>\n"
1911 " * spirv1.0 under --target-env vulkan1.0\n"
1912 " * spirv1.3 under --target-env vulkan1.1\n"
1913 " * spirv1.5 under --target-env vulkan1.2\n"
1914 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001915 " --variable-name <name>\n"
1916 " --vn <name> creates a C header file that contains a\n"
1917 " uint32_t array named <name>\n"
1918 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001919 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001920
1921 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001922}
1923
John Kessenich3ce4e592014-10-06 19:57:34 +00001924#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001925
1926#include <errno.h>
1927
1928int fopen_s(
1929 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001930 const char* filename,
1931 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001932)
1933{
1934 if (!pFile || !filename || !mode) {
1935 return EINVAL;
1936 }
1937
1938 FILE* f = fopen(filename, mode);
1939 if (! f) {
1940 if (errno != 0) {
1941 return errno;
1942 } else {
1943 return ENOENT;
1944 }
1945 }
1946 *pFile = f;
1947
1948 return 0;
1949}
1950
1951#endif
1952
John Kessenicha0af4732012-12-12 21:15:54 +00001953//
1954// Malloc a string of sufficient size and read a string into it.
1955//
John Kessenich04acb1b2017-06-14 17:36:50 -06001956char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001957{
John Kessenichb3297152015-07-11 18:01:03 -06001958 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001959 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001960 if (errorCode || in == nullptr)
1961 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001962
John Kessenich04acb1b2017-06-14 17:36:50 -06001963 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001964 while (fgetc(in) != EOF)
1965 count++;
1966
John Kessenichd6c72a42014-08-18 19:42:35 +00001967 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001968
John Kessenich04acb1b2017-06-14 17:36:50 -06001969 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1970 if ((int)fread(return_data, 1, count, in) != count) {
1971 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001972 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001973 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001974
John Kessenich04acb1b2017-06-14 17:36:50 -06001975 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001976 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001977
John Kessenicha0af4732012-12-12 21:15:54 +00001978 return return_data;
1979}
1980
John Kessenich04acb1b2017-06-14 17:36:50 -06001981void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001982{
John Kessenichb3297152015-07-11 18:01:03 -06001983 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001984}
1985
John Kessenich54d8cda2013-02-11 22:36:01 +00001986void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001987{
John Kessenichfae38ee2015-06-10 23:23:12 +00001988 if (num >= 0 )
1989 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1990 else
1991 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001992}