blob: 9535b70da2c2085a2280b825864d60fa603dc135 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenich56364b62020-03-01 04:51:40 -07004// Copyright (C) 2016-2020 Google, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00005//
John Kessenich927608b2017-01-06 12:34:14 -07006// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00007//
John Kessenich927608b2017-01-06 12:34:14 -07008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions
10// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000011//
12// Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following
17// disclaimer in the documentation and/or other materials provided
18// with the distribution.
19//
20// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21// contributors may be used to endorse or promote products derived
22// from this software without specific prior written permission.
23//
John Kessenich927608b2017-01-06 12:34:14 -070024// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000036//
John Kessenich05a70632013-09-17 19:26:08 +000037
38// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030039#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000040#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030041#endif
John Kessenich05a70632013-09-17 19:26:08 +000042
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040043#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000044#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060045#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000046#include "./../glslang/Include/ShHandle.h"
47#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060058#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020059#include <memory>
60#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000061
baldurk876a0e32015-11-16 18:03:28 +010062#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000063
Ben Claytonfbe9a232020-06-17 11:17:19 +010064// Build-time generated includes
65#include "glslang/build_info.h"
66
John Kessenicha0af4732012-12-12 21:15:54 +000067extern "C" {
Ben Claytond64e8592020-06-29 14:20:19 +010068 GLSLANG_EXPORT void ShOutputHtml();
John Kessenicha0af4732012-12-12 21:15:54 +000069}
70
John Kessenich94a81fb2013-08-31 02:41:30 +000071// Command-line options
72enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070073 EOptionNone = 0,
74 EOptionIntermediate = (1 << 0),
75 EOptionSuppressInfolog = (1 << 1),
76 EOptionMemoryLeakMode = (1 << 2),
77 EOptionRelaxedErrors = (1 << 3),
78 EOptionGiveWarnings = (1 << 4),
79 EOptionLinkProgram = (1 << 5),
80 EOptionMultiThreaded = (1 << 6),
81 EOptionDumpConfig = (1 << 7),
82 EOptionDumpReflection = (1 << 8),
83 EOptionSuppressWarnings = (1 << 9),
84 EOptionDumpVersions = (1 << 10),
85 EOptionSpv = (1 << 11),
86 EOptionHumanReadableSpv = (1 << 12),
87 EOptionVulkanRules = (1 << 13),
88 EOptionDefaultDesktop = (1 << 14),
89 EOptionOutputPreprocessed = (1 << 15),
90 EOptionOutputHexadecimal = (1 << 16),
91 EOptionReadHlsl = (1 << 17),
92 EOptionCascadingErrors = (1 << 18),
93 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060094 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070095 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070096 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060097 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060098 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060099 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -0600100 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200101 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -0600102 EOptionOptimizeDisable = (1 << 28),
103 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700104 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700105 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000106};
John Kessenich5d610ee2018-03-07 18:05:55 -0700107bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600108bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600109bool SpvToolsValidate = false;
John Kessenich605afc72019-06-17 23:33:09 -0600110bool NaNClamp = false;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400111bool stripDebugInfo = false;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200112bool beQuiet = false;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600113bool VulkanRulesRelaxed = false;
Greg Fischerebfca602021-04-21 16:49:53 -0600114bool autoSampledTextures = 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;
John Kessenich4d65ee32016-03-12 18:17:47 -0700169const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600170const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400171const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800172const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800173bool HlslEnable16BitTypes = false;
John Kessenichbd1c1832018-12-07 18:38:26 -0700174bool HlslDX9compatible = false;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200175bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600176std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600177
178// Source environment
179// (source 'Client' is currently the same as target 'Client')
180int ClientInputSemanticsVersion = 100;
181
182// Target environment
183glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
184glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
185glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
186glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
187
John Kessenich66011cb2018-03-06 16:12:04 -0700188std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600189
LoopDawg08a14422017-10-17 19:27:14 -0600190// Per descriptor-set binding base data
191typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
192
Neil Roberts16f53472018-03-20 17:30:53 +0100193std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100194int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100195
LoopDawg08a14422017-10-17 19:27:14 -0600196std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
197std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900198std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600199
greg-lunarg4e064ee2021-03-15 11:26:11 -0600200std::vector<std::pair<std::string, glslang::TBlockStorageClass>> blockStorageOverrides;
201
202bool setGlobalUniformBlock = false;
203std::string globalUniformName;
204unsigned int globalUniformBinding;
205unsigned int globalUniformSet;
206
207bool setGlobalBufferBlock = false;
208std::string atomicCounterBlockName;
209unsigned int atomicCounterBlockSet;
210
John Kessenicha9313662017-06-15 10:40:49 -0600211// Add things like "#define ..." to a preamble to use in the beginning of the shader.
212class TPreamble {
213public:
214 TPreamble() { }
215
216 bool isSet() const { return text.size() > 0; }
217 const char* get() const { return text.c_str(); }
218
219 // #define...
220 void addDef(std::string def)
221 {
222 text.append("#define ");
223 fixLine(def);
224
John Kessenich6f988922020-01-07 07:03:11 -0700225 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600226 Processes.back().append(def);
227
John Kessenicha9313662017-06-15 10:40:49 -0600228 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600229 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600230 if (equal != def.npos)
231 def[equal] = ' ';
232
233 text.append(def);
234 text.append("\n");
235 }
236
237 // #undef...
238 void addUndef(std::string undef)
239 {
240 text.append("#undef ");
241 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600242
John Kessenich6f988922020-01-07 07:03:11 -0700243 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600244 Processes.back().append(undef);
245
John Kessenicha9313662017-06-15 10:40:49 -0600246 text.append(undef);
247 text.append("\n");
248 }
249
250protected:
251 void fixLine(std::string& line)
252 {
253 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600254 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600255 if (end != line.npos)
256 line = line.substr(0, end);
257 }
258
259 std::string text; // contents of preamble
260};
261
John Kessenich83855b92020-03-30 00:27:31 -0600262// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600263TPreamble UserPreamble;
264
John Kessenich68d78fd2015-07-12 19:28:10 -0600265//
266// Create the default name for saving a binary if -o is not provided.
267//
268const char* GetBinaryName(EShLanguage stage)
269{
270 const char* name;
271 if (binaryFileName == nullptr) {
272 switch (stage) {
273 case EShLangVertex: name = "vert.spv"; break;
274 case EShLangTessControl: name = "tesc.spv"; break;
275 case EShLangTessEvaluation: name = "tese.spv"; break;
276 case EShLangGeometry: name = "geom.spv"; break;
277 case EShLangFragment: name = "frag.spv"; break;
278 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400279 case EShLangRayGen: name = "rgen.spv"; break;
280 case EShLangIntersect: name = "rint.spv"; break;
281 case EShLangAnyHit: name = "rahit.spv"; break;
282 case EShLangClosestHit: name = "rchit.spv"; break;
283 case EShLangMiss: name = "rmiss.spv"; break;
284 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700285 case EShLangMeshNV: name = "mesh.spv"; break;
286 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600287 default: name = "unknown"; break;
288 }
289 } else
290 name = binaryFileName;
291
292 return name;
293}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000294
John Kessenich05a70632013-09-17 19:26:08 +0000295//
296// *.conf => this is a config file that can set limits/resources
297//
298bool SetConfigFile(const std::string& name)
299{
300 if (name.size() < 5)
301 return false;
302
John Kessenich4c706852013-10-11 16:28:43 +0000303 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000304 ConfigFile = name;
305 return true;
306 }
307
308 return false;
309}
310
John Kessenich68d78fd2015-07-12 19:28:10 -0600311//
312// Give error and exit with failure code.
313//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700314void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600315{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700316 fprintf(stderr, "%s: Error: ", ExecutableName);
317 if (detail != nullptr)
318 fprintf(stderr, "%s: ", detail);
319 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600320 exit(EFailUsage);
321}
322
323//
LoopDawg08a14422017-10-17 19:27:14 -0600324// Process an optional binding base of one the forms:
325// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600326// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600327
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600328// Where stage is one of the forms accepted by FindLanguage, and base is an integer
329//
LoopDawg08a14422017-10-17 19:27:14 -0600330void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600331{
332 if (argc < 2)
333 usage();
334
LoopDawg08a14422017-10-17 19:27:14 -0600335 EShLanguage lang = EShLangCount;
336 int singleBase = 0;
337 TPerSetBaseBinding perSetBase;
338 int arg = 1;
339
340 // Parse stage, if given
341 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600342 if (argc < 3) // this form needs one more argument
343 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700344
LoopDawg08a14422017-10-17 19:27:14 -0600345 lang = FindLanguage(argv[arg++], false);
346 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600347
LoopDawg08a14422017-10-17 19:27:14 -0600348 if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
349 // Parse a per-set binding base
350 while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600351 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600352 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600353 perSetBase[setNum] = baseNum;
354 }
355 } else {
356 // Parse single binding base
357 singleBase = atoi(argv[arg++]);
358 }
359
360 argc -= (arg-1);
361 argv += (arg-1);
362
363 // Set one or all languages
364 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
365 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
366
367 for (int lang = langMin; lang < langMax; ++lang) {
368 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600369 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600370 else
371 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600372 }
373}
374
Hyangran Park36dc8292017-05-02 16:27:29 +0900375void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
376{
377 if (argc < 2)
378 usage();
379
380 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600381 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900382 usage();
383
LoopDawg52017192017-07-14 15:15:47 -0600384 // Parse form: --argname stage [regname set base...], or:
385 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900386 const EShLanguage lang = FindLanguage(argv[1], false);
387
LoopDawg52017192017-07-14 15:15:47 -0600388 argc--;
389 argv++;
390
391 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
392 base[lang].push_back(argv[1]);
393
394 argc--;
395 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900396 }
LoopDawg52017192017-07-14 15:15:47 -0600397
398 // Must have one arg, or a multiple of three (for [regname set binding] triples)
399 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
400 usage();
401
Hyangran Park36dc8292017-05-02 16:27:29 +0900402 } else {
LoopDawg52017192017-07-14 15:15:47 -0600403 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900404 for (int lang=0; lang<EShLangCount; ++lang)
405 base[lang].push_back(argv[1]);
406
407 argc--;
408 argv++;
409 }
410}
411
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600412//
greg-lunarg4e064ee2021-03-15 11:26:11 -0600413// Process an optional binding base of one the forms:
414// --argname name {uniform|buffer|push_constant}
415void ProcessBlockStorage(int& argc, char**& argv, std::vector<std::pair<std::string, glslang::TBlockStorageClass>>& storage)
416{
417 if (argc < 3)
418 usage();
419
420 glslang::TBlockStorageClass blockStorage = glslang::EbsNone;
421
422 std::string strBacking(argv[2]);
423 if (strBacking == "uniform")
424 blockStorage = glslang::EbsUniform;
425 else if (strBacking == "buffer")
426 blockStorage = glslang::EbsStorageBuffer;
427 else if (strBacking == "push_constant")
428 blockStorage = glslang::EbsPushConstant;
429 else {
430 printf("%s: invalid block storage\n", strBacking.c_str());
431 usage();
432 }
433
434 storage.push_back(std::make_pair(std::string(argv[1]), blockStorage));
435
436 argc -= 2;
437 argv += 2;
438}
439
440inline bool isNonDigit(char c) {
441 // a non-digit character valid in a glsl identifier
442 return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
443}
444
445// whether string isa valid identifier to be used in glsl
446bool isValidIdentifier(const char* str) {
447 std::string idn(str);
448
449 if (idn.length() == 0) {
450 return false;
451 }
452
453 if (idn.length() >= 3 && idn.substr(0, 3) == "gl_") {
454 // identifiers startin with "gl_" are reserved
455 return false;
456 }
457
458 if (!isNonDigit(idn[0])) {
459 return false;
460 }
461
462 for (unsigned int i = 1; i < idn.length(); ++i) {
463 if (!(isdigit(idn[i]) || isNonDigit(idn[i]))) {
464 return false;
465 }
466 }
467
468 return true;
469}
470
471// Process settings for either the global buffer block or global unfirom block
472// of the form:
473// --argname name set binding
474void ProcessGlobalBlockSettings(int& argc, char**& argv, std::string* name, unsigned int* set, unsigned int* binding)
475{
476 if (argc < 4)
477 usage();
478
479 unsigned int curArg = 1;
480
481 assert(name || set || binding);
482
483 if (name) {
484 if (!isValidIdentifier(argv[curArg])) {
485 printf("%s: invalid identifier\n", argv[curArg]);
486 usage();
487 }
488 *name = argv[curArg];
489
490 curArg++;
491 }
492
493 if (set) {
494 errno = 0;
495 int setVal = ::strtol(argv[curArg], NULL, 10);
496 if (errno || setVal < 0) {
497 printf("%s: invalid set\n", argv[curArg]);
498 usage();
499 }
500 *set = setVal;
501
502 curArg++;
503 }
504
505 if (binding) {
506 errno = 0;
507 int bindingVal = ::strtol(argv[curArg], NULL, 10);
508 if (errno || bindingVal < 0) {
509 printf("%s: invalid binding\n", argv[curArg]);
510 usage();
511 }
512 *binding = bindingVal;
513
514 curArg++;
515 }
516
517 argc -= (curArg - 1);
518 argv += (curArg - 1);
519}
520
521//
John Kessenich68d78fd2015-07-12 19:28:10 -0600522// Do all command-line argument parsing. This includes building up the work-items
523// to be processed later, and saving all the command-line options.
524//
525// Does not return (it exits) if command-line is fatally flawed.
526//
Juan Lopeza558b262017-04-02 23:04:00 +0200527void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000528{
LoopDawg08a14422017-10-17 19:27:14 -0600529 for (int res = 0; res < glslang::EResCount; ++res)
530 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600531
John Kessenich38f3b892013-09-06 19:52:57 +0000532 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200533 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000534
John Kessenichc178f0a2017-06-23 10:58:31 -0600535 const auto bumpArg = [&]() {
536 if (argc > 0) {
537 argc--;
538 argv++;
539 }
540 };
541
542 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600543 const auto getStringOperand = [&](const char* desc) {
544 if (argv[0][2] == 0) {
545 printf("%s must immediately follow option (no spaces)\n", desc);
546 exit(EFailUsage);
547 }
548 return argv[0] + 2;
549 };
550
John Kessenich6353d552017-06-23 11:11:09 -0600551 // read a number attached to a single-letter option
552 const auto getAttachedNumber = [&](const char* desc) {
553 int num = atoi(argv[0] + 2);
554 if (num == 0) {
555 printf("%s: expected attached non-0 number\n", desc);
556 exit(EFailUsage);
557 }
558 return num;
559 };
560
561 // minimum needed (without overriding something else) to target Vulkan SPIR-V
562 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600563 if (Client == glslang::EShClientNone)
564 ClientVersion = glslang::EShTargetVulkan_1_0;
565 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600566 Options |= EOptionSpv;
567 Options |= EOptionVulkanRules;
568 Options |= EOptionLinkProgram;
569 };
570
571 // minimum needed (without overriding something else) to target OpenGL SPIR-V
572 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600573 if (Client == glslang::EShClientNone)
574 ClientVersion = glslang::EShTargetOpenGL_450;
575 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600576 Options |= EOptionSpv;
577 Options |= EOptionLinkProgram;
578 // undo a -H default to Vulkan
579 Options &= ~EOptionVulkanRules;
580 };
581
Neil Roberts16f53472018-03-20 17:30:53 +0100582 const auto getUniformOverride = [getStringOperand]() {
583 const char *arg = getStringOperand("-u<name>:<location>");
584 const char *split = strchr(arg, ':');
585 if (split == NULL) {
586 printf("%s: missing location\n", arg);
587 exit(EFailUsage);
588 }
589 errno = 0;
590 int location = ::strtol(split + 1, NULL, 10);
591 if (errno) {
592 printf("%s: invalid location\n", arg);
593 exit(EFailUsage);
594 }
595 return std::make_pair(std::string(arg, split - arg), location);
596 };
597
John Kessenichc178f0a2017-06-23 10:58:31 -0600598 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000599 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000600 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600601 case '-':
602 {
603 std::string lowerword(argv[0]+2);
604 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
605
606 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600607 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600608 lowerword == "auto-map-binding" ||
609 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600610 Options |= EOptionAutoMapBindings;
611 } else if (lowerword == "auto-map-locations" || // synonyms
612 lowerword == "aml") {
613 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100614 } else if (lowerword == "uniform-base") {
615 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700616 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100617 uniformBase = ::strtol(argv[1], NULL, 10);
618 bumpArg();
619 break;
John Kessenich6353d552017-06-23 11:11:09 -0600620 } else if (lowerword == "client") {
621 if (argc > 1) {
622 if (strcmp(argv[1], "vulkan100") == 0)
623 setVulkanSpv();
624 else if (strcmp(argv[1], "opengl100") == 0)
625 setOpenGlSpv();
626 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700627 Error("expects vulkan100 or opengl100", lowerword.c_str());
628 } else
629 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600630 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700631 } else if (lowerword == "define-macro" ||
632 lowerword == "d") {
633 if (argc > 1)
634 UserPreamble.addDef(argv[1]);
635 else
636 Error("expects <name[=def]>", argv[0]);
637 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200638 } else if (lowerword == "dump-builtin-symbols") {
639 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600640 } else if (lowerword == "entry-point") {
641 entryPointName = argv[1];
642 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700643 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600644 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600645 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
646 lowerword == "flatten-uniform-array" ||
647 lowerword == "fua") {
648 Options |= EOptionFlattenUniformArrays;
649 } else if (lowerword == "hlsl-offsets") {
650 Options |= EOptionHlslOffsets;
651 } else if (lowerword == "hlsl-iomap" ||
652 lowerword == "hlsl-iomapper" ||
653 lowerword == "hlsl-iomapping") {
654 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800655 } else if (lowerword == "hlsl-enable-16bit-types") {
656 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700657 } else if (lowerword == "hlsl-dx9-compatible") {
658 HlslDX9compatible = true;
Greg Fischerebfca602021-04-21 16:49:53 -0600659 } else if (lowerword == "auto-sampled-textures") {
660 autoSampledTextures = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700661 } else if (lowerword == "invert-y" || // synonyms
662 lowerword == "iy") {
663 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600664 } else if (lowerword == "keep-uncalled" || // synonyms
665 lowerword == "ku") {
666 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600667 } else if (lowerword == "nan-clamp") {
668 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600669 } else if (lowerword == "no-storage-format" || // synonyms
670 lowerword == "nsf") {
671 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600672 } else if (lowerword == "relaxed-errors") {
673 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000674 } else if (lowerword == "reflect-strict-array-suffix") {
675 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000676 } else if (lowerword == "reflect-basic-array-suffix") {
677 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000678 } else if (lowerword == "reflect-intermediate-io") {
679 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000680 } else if (lowerword == "reflect-separate-buffers") {
681 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000682 } else if (lowerword == "reflect-all-block-variables") {
683 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000684 } else if (lowerword == "reflect-unwrap-io-blocks") {
685 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800686 } else if (lowerword == "reflect-all-io-variables") {
687 ReflectOptions |= EShReflectionAllIOVariables;
688 } else if (lowerword == "reflect-shared-std140-ubo") {
689 ReflectOptions |= EShReflectionSharedStd140UBO;
690 } else if (lowerword == "reflect-shared-std140-ssbo") {
691 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600692 } else if (lowerword == "resource-set-bindings" || // synonyms
693 lowerword == "resource-set-binding" ||
694 lowerword == "rsb") {
695 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
greg-lunarg4e064ee2021-03-15 11:26:11 -0600696 } else if (lowerword == "set-block-storage" ||
697 lowerword == "sbs") {
698 ProcessBlockStorage(argc, argv, blockStorageOverrides);
699 } else if (lowerword == "set-atomic-counter-block" ||
700 lowerword == "sacb") {
701 ProcessGlobalBlockSettings(argc, argv, &atomicCounterBlockName, &atomicCounterBlockSet, nullptr);
702 setGlobalBufferBlock = true;
703 } else if (lowerword == "set-default-uniform-block" ||
704 lowerword == "sdub") {
705 ProcessGlobalBlockSettings(argc, argv, &globalUniformName, &globalUniformSet, &globalUniformBinding);
706 setGlobalUniformBlock = true;
steve-lunarg9088be42016-11-01 10:31:42 -0600707 } else if (lowerword == "shift-image-bindings" || // synonyms
708 lowerword == "shift-image-binding" ||
709 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600710 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600711 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600712 lowerword == "shift-sampler-binding" ||
713 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600714 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600715 } else if (lowerword == "shift-uav-bindings" || // synonyms
716 lowerword == "shift-uav-binding" ||
717 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600718 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600719 } else if (lowerword == "shift-texture-bindings" || // synonyms
720 lowerword == "shift-texture-binding" ||
721 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600722 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600723 } else if (lowerword == "shift-ubo-bindings" || // synonyms
724 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600725 lowerword == "shift-cbuffer-bindings" ||
726 lowerword == "shift-cbuffer-binding" ||
727 lowerword == "sub" ||
728 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600729 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700730 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
731 lowerword == "shift-ssbo-binding" ||
732 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600733 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600734 } else if (lowerword == "source-entrypoint" || // synonyms
735 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600736 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700737 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600738 sourceEntryPointName = argv[1];
739 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600740 break;
John Kesseniche2156222018-06-11 18:12:15 -0600741 } else if (lowerword == "spirv-dis") {
742 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600743 } else if (lowerword == "spirv-val") {
744 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200745 } else if (lowerword == "stdin") {
746 Options |= EOptionStdin;
747 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600748 } else if (lowerword == "suppress-warnings") {
749 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600750 } else if (lowerword == "target-env") {
751 if (argc > 1) {
752 if (strcmp(argv[1], "vulkan1.0") == 0) {
753 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600754 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700755 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
756 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600757 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700758 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
759 setVulkanSpv();
760 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600761 } else if (strcmp(argv[1], "opengl") == 0) {
762 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600763 ClientVersion = glslang::EShTargetOpenGL_450;
764 } else if (strcmp(argv[1], "spirv1.0") == 0) {
765 TargetLanguage = glslang::EShTargetSpv;
766 TargetVersion = glslang::EShTargetSpv_1_0;
767 } else if (strcmp(argv[1], "spirv1.1") == 0) {
768 TargetLanguage = glslang::EShTargetSpv;
769 TargetVersion = glslang::EShTargetSpv_1_1;
770 } else if (strcmp(argv[1], "spirv1.2") == 0) {
771 TargetLanguage = glslang::EShTargetSpv;
772 TargetVersion = glslang::EShTargetSpv_1_2;
773 } else if (strcmp(argv[1], "spirv1.3") == 0) {
774 TargetLanguage = glslang::EShTargetSpv;
775 TargetVersion = glslang::EShTargetSpv_1_3;
776 } else if (strcmp(argv[1], "spirv1.4") == 0) {
777 TargetLanguage = glslang::EShTargetSpv;
778 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600779 } else if (strcmp(argv[1], "spirv1.5") == 0) {
780 TargetLanguage = glslang::EShTargetSpv;
781 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600782 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700783 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600784 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600785 }
786 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700787 } else if (lowerword == "undef-macro" ||
788 lowerword == "u") {
789 if (argc > 1)
790 UserPreamble.addUndef(argv[1]);
791 else
792 Error("expects <name>", argv[0]);
793 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800794 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700795 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800796 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600797 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700798 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600799 variableName = argv[1];
800 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800801 break;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200802 } else if (lowerword == "quiet") {
803 beQuiet = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700804 } else if (lowerword == "version") {
805 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800806 } else if (lowerword == "help") {
807 usage();
808 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600809 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700810 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600811 }
812 }
813 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600814 case 'C':
815 Options |= EOptionCascadingErrors;
816 break;
817 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600818 if (argv[0][2] == 0)
819 Options |= EOptionReadHlsl;
820 else
John Kessenich6f988922020-01-07 07:03:11 -0700821 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600822 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100823 case 'u':
824 uniformLocationOverrides.push_back(getUniformOverride());
825 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600826 case 'E':
827 Options |= EOptionOutputPreprocessed;
828 break;
829 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600830 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600831 setOpenGlSpv();
832 if (argv[0][2] != 0)
833 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600834 if (ClientInputSemanticsVersion != 100)
835 Error("unknown client version for -G, should be 100");
John Kessenich6263fb12017-06-14 15:52:44 -0600836 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000837 case 'H':
838 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600839 if ((Options & EOptionSpv) == 0) {
840 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600841 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600842 }
843 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600844 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600845 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600846 break;
GregFcd1f1692017-09-21 18:40:22 -0600847 case 'O':
848 if (argv[0][2] == 'd')
849 Options |= EOptionOptimizeDisable;
850 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600851#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600852 Options |= EOptionOptimizeSize;
853#else
854 Error("-Os not available; optimizer not linked");
855#endif
856 else
857 Error("unknown -O option");
858 break;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600859 case 'R':
860 VulkanRulesRelaxed = true;
861 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400862 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600863 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400864 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600865 shaderStageName = argv[1];
866 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400867 break;
John Kessenicha9313662017-06-15 10:40:49 -0600868 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700869 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600870 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600871 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600872 setVulkanSpv();
873 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500874 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600875 if (ClientInputSemanticsVersion != 100)
876 Error("unknown client version for -V, should be 100");
John Kessenichc555ddd2015-06-17 02:38:44 +0000877 break;
John Kessenich05a70632013-09-17 19:26:08 +0000878 case 'c':
879 Options |= EOptionDumpConfig;
880 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000881 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700882 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
883 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
884 Options |= EOptionDumpBareVersion;
885 else
886 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000887 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700888 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700889 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600890 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600891 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600892 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700893 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700894 case 'f':
895 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
896 targetHlslFunctionality1 = true;
897 else
898 Error("-f: expected hlsl_functionality1");
899 break;
John Kessenich121853f2017-05-31 17:11:16 -0600900 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400901 // Override previous -g or -g0 argument
902 stripDebugInfo = false;
903 Options &= ~EOptionDebug;
904 if (argv[0][2] == '0')
905 stripDebugInfo = true;
906 else
907 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600908 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600909 case 'h':
910 usage();
911 break;
John Kessenich05a70632013-09-17 19:26:08 +0000912 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000913 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000914 break;
915 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000916 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000917 break;
918 case 'm':
919 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000920 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600921 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600922 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600923 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600924 binaryFileName = argv[1];
925 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600926 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000927 case 'q':
928 Options |= EOptionDumpReflection;
929 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000930 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000931 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000932 break;
933 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000934 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000935 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000936 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200937 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000938 break;
John Kessenich319de232013-12-04 04:43:40 +0000939 case 'v':
940 Options |= EOptionDumpVersions;
941 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000942 case 'w':
943 Options |= EOptionSuppressWarnings;
944 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500945 case 'x':
946 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500947 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000948 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700949 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600950 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000951 }
John Kessenich38f3b892013-09-06 19:52:57 +0000952 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000953 std::string name(argv[0]);
954 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200955 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000956 }
John Kessenich38f3b892013-09-06 19:52:57 +0000957 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000958 }
959
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200960 // Make sure that -S is always specified if --stdin is specified
961 if ((Options & EOptionStdin) && shaderStageName == nullptr)
962 Error("must provide -S when --stdin is given");
963
John Kessenich68d78fd2015-07-12 19:28:10 -0600964 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600965 // Or things that require linking
966 if (Options & EOptionOutputPreprocessed) {
967 if (Options & EOptionLinkProgram)
968 Error("can't use -E when linking is selected");
969 if (Options & EOptionDumpReflection)
970 Error("reflection requires linking, which can't be used when -E when is selected");
971 }
972
973 // reflection requires linking
974 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
975 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000976
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500977 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600978 if (binaryFileName && (Options & EOptionSpv) == 0)
979 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600980
981 if ((Options & EOptionFlattenUniformArrays) != 0 &&
982 (Options & EOptionReadHlsl) == 0)
983 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600984
Jeremy Hayesb73afa82021-01-05 15:54:41 -0700985 if ((Options & EOptionReadHlsl) && (Client == glslang::EShClientOpenGL)) {
986 Error("Using HLSL input under OpenGL semantics is not currently supported.");
987 }
988
John Kessenich8717a5d2018-10-26 10:12:32 -0600989 // rationalize client and target language
990 if (TargetLanguage == glslang::EShTargetNone) {
991 switch (ClientVersion) {
992 case glslang::EShTargetVulkan_1_0:
993 TargetLanguage = glslang::EShTargetSpv;
994 TargetVersion = glslang::EShTargetSpv_1_0;
995 break;
996 case glslang::EShTargetVulkan_1_1:
997 TargetLanguage = glslang::EShTargetSpv;
998 TargetVersion = glslang::EShTargetSpv_1_3;
999 break;
John Kessenich273d3a52020-01-15 00:10:41 -07001000 case glslang::EShTargetVulkan_1_2:
1001 TargetLanguage = glslang::EShTargetSpv;
1002 TargetVersion = glslang::EShTargetSpv_1_5;
1003 break;
John Kessenich8717a5d2018-10-26 10:12:32 -06001004 case glslang::EShTargetOpenGL_450:
1005 TargetLanguage = glslang::EShTargetSpv;
1006 TargetVersion = glslang::EShTargetSpv_1_0;
1007 break;
1008 default:
1009 break;
1010 }
1011 }
1012 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
1013 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +00001014}
1015
John Kessenich68d78fd2015-07-12 19:28:10 -06001016//
1017// Translate the meaningful subset of command-line options to parser-behavior options.
1018//
John Kessenichb0a7eb52013-11-07 17:44:20 +00001019void SetMessageOptions(EShMessages& messages)
1020{
1021 if (Options & EOptionRelaxedErrors)
1022 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
1023 if (Options & EOptionIntermediate)
1024 messages = (EShMessages)(messages | EShMsgAST);
1025 if (Options & EOptionSuppressWarnings)
1026 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -06001027 if (Options & EOptionSpv)
1028 messages = (EShMessages)(messages | EShMsgSpvRules);
1029 if (Options & EOptionVulkanRules)
1030 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001031 if (Options & EOptionOutputPreprocessed)
1032 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -07001033 if (Options & EOptionReadHlsl)
1034 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -06001035 if (Options & EOptionCascadingErrors)
1036 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -07001037 if (Options & EOptionKeepUncalled)
1038 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -06001039 if (Options & EOptionHlslOffsets)
1040 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -06001041 if (Options & EOptionDebug)
1042 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +08001043 if (HlslEnable16BitTypes)
1044 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -06001045 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
1046 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -07001047 if (HlslDX9compatible)
1048 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +02001049 if (DumpBuiltinSymbols)
1050 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +00001051}
1052
John Kessenich68d78fd2015-07-12 19:28:10 -06001053//
John Kessenich69f4b512013-09-04 21:19:27 +00001054// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +00001055//
Juan Lopeza558b262017-04-02 23:04:00 +02001056void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001057{
John Kessenich7f0bcfd2018-04-05 19:00:01 -06001058 if (Options & EOptionDebug)
1059 Error("cannot generate debug information unless linking to generate code");
1060
John Kessenich38f3b892013-09-06 19:52:57 +00001061 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001062 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -06001063 if (worklist.remove(workItem)) {
1064 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
1065 if (compiler == nullptr)
1066 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001067
John Kesseniche7f9cae2018-07-12 15:11:07 -06001068 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001069
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001070 if (! (Options & EOptionSuppressInfolog))
1071 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001072
John Kesseniche7f9cae2018-07-12 15:11:07 -06001073 ShDestruct(compiler);
1074 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001075 } else {
1076 while (worklist.remove(workItem)) {
1077 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
1078 if (compiler == 0)
1079 return;
1080
1081 CompileFile(workItem->name.c_str(), compiler);
1082
1083 if (! (Options & EOptionSuppressInfolog))
1084 workItem->results = ShGetInfoLog(compiler);
1085
1086 ShDestruct(compiler);
1087 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001088 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001089}
1090
John Kessenich6626cad2015-06-19 05:14:19 +00001091// Outputs the given string, but only if it is non-null and non-empty.
1092// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001093void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +00001094{
1095 if (str && str[0]) {
1096 puts(str);
1097 }
1098}
1099
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001100// Outputs the given string to stderr, but only if it is non-null and non-empty.
1101// This prevents erroneous newlines from appearing.
1102void StderrIfNonEmpty(const char* str)
1103{
John Kessenich04acb1b2017-06-14 17:36:50 -06001104 if (str && str[0])
1105 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001106}
1107
John Kessenichc57b2a92016-01-16 15:30:03 -07001108// Simple bundling of what makes a compilation unit for ease in passing around,
1109// and separation of handling file IO versus API (programmatic) compilation.
1110struct ShaderCompUnit {
1111 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001112 static const int maxCount = 1;
1113 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -06001114 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -06001115 std::string fileName[maxCount]; // hold's the memory, but...
1116 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -04001117
John Kessenich04acb1b2017-06-14 17:36:50 -06001118 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -04001119
John Kessenich04acb1b2017-06-14 17:36:50 -06001120 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -04001121 {
1122 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001123 count = rhs.count;
1124 for (int i = 0; i < count; ++i) {
1125 fileName[i] = rhs.fileName[i];
1126 text[i] = rhs.text[i];
1127 fileNameList[i] = rhs.fileName[i].c_str();
1128 }
dankbakerafe6e9c2016-08-21 12:29:08 -04001129 }
1130
John Kessenicha9313662017-06-15 10:40:49 -06001131 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -06001132 {
1133 assert(count < maxCount);
1134 fileName[count] = ifileName;
1135 text[count] = itext;
1136 fileNameList[count] = fileName[count].c_str();
1137 ++count;
1138 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001139};
1140
John Kessenich69f4b512013-09-04 21:19:27 +00001141//
John Kessenichc57b2a92016-01-16 15:30:03 -07001142// For linking mode: Will independently parse each compilation unit, but then put them
1143// in the same program and link them together, making at most one linked module per
1144// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +00001145//
1146// Uses the new C++ interface instead of the old handle-based interface.
1147//
John Kessenichc57b2a92016-01-16 15:30:03 -07001148
1149void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +00001150{
1151 // keep track of what to free
1152 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001153
John Kessenich69f4b512013-09-04 21:19:27 +00001154 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001155 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001156
John Kessenich69f4b512013-09-04 21:19:27 +00001157 //
1158 // Per-shader processing...
1159 //
1160
John Kessenich5b0f13a2013-11-01 03:08:40 +00001161 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001162 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1163 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -07001164 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001165 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001166 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001167 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001168 if (sourceEntryPointName) {
1169 if (entryPointName == nullptr)
1170 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1171 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001172 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001173 }
John Kessenicha9313662017-06-15 10:40:49 -06001174 if (UserPreamble.isSet())
1175 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -06001176 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001177
John Kessenich155d3512019-08-08 23:29:20 -06001178#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001179 // Set IO mapper binding shift values
1180 for (int r = 0; r < glslang::EResCount; ++r) {
1181 const glslang::TResourceType res = glslang::TResourceType(r);
1182
1183 // Set base bindings
1184 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001185
LoopDawg08a14422017-10-17 19:27:14 -06001186 // Set bindings for particular resource sets
1187 // TODO: use a range based for loop here, when available in all environments.
1188 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1189 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001190 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001191 }
steve-lunargcce8d482016-10-14 18:36:42 -06001192 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001193 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001194
Greg Fischerebfca602021-04-21 16:49:53 -06001195 if (autoSampledTextures)
Robin Quint219b7b92021-04-20 07:58:36 +02001196 shader->setTextureSamplerTransformMode(EShTexSampTransUpgradeTextureRemoveSampler);
1197
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001198 if (Options & EOptionAutoMapBindings)
1199 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001200
John Kessenich71facdf2017-05-17 18:28:19 -06001201 if (Options & EOptionAutoMapLocations)
1202 shader->setAutoMapLocations(true);
1203
Neil Roberts16f53472018-03-20 17:30:53 +01001204 for (auto& uniOverride : uniformLocationOverrides) {
1205 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1206 uniOverride.second);
1207 }
1208
Neil Robertsb0f3d792018-03-20 17:41:05 +01001209 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001210#endif
1211
greg-lunarg4e064ee2021-03-15 11:26:11 -06001212 if (VulkanRulesRelaxed) {
1213 for (auto& storageOverride : blockStorageOverrides) {
1214 shader->addBlockStorageOverride(storageOverride.first.c_str(),
1215 storageOverride.second);
1216 }
1217
1218 if (setGlobalBufferBlock) {
1219 shader->setAtomicCounterBlockName(atomicCounterBlockName.c_str());
1220 shader->setAtomicCounterBlockSet(atomicCounterBlockSet);
1221 }
1222
1223 if (setGlobalUniformBlock) {
1224 shader->setGlobalUniformBlockName(globalUniformName.c_str());
1225 shader->setGlobalUniformSet(globalUniformSet);
1226 shader->setGlobalUniformBinding(globalUniformBinding);
1227 }
1228 }
1229
John Kessenich155d3512019-08-08 23:29:20 -06001230 shader->setNanMinMaxClamp(NaNClamp);
1231
1232#ifdef ENABLE_HLSL
1233 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1234 if (Options & EOptionHlslIoMapping)
1235 shader->setHlslIoMapping(true);
1236#endif
1237
1238 if (Options & EOptionInvertY)
1239 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001240
John Kessenich4be4aeb2017-06-23 10:50:22 -06001241 // Set up the environment, some subsettings take precedence over earlier
1242 // ways of setting things.
1243 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001244 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1245 : glslang::EShSourceGlsl,
1246 compUnit.stage, Client, ClientInputSemanticsVersion);
1247 shader->setEnvClient(Client, ClientVersion);
1248 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001249#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001250 if (targetHlslFunctionality1)
1251 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001252#endif
greg-lunarg4e064ee2021-03-15 11:26:11 -06001253 if (VulkanRulesRelaxed)
1254 shader->setEnvInputVulkanRulesRelaxed();
John Kessenich4be4aeb2017-06-23 10:50:22 -06001255 }
1256
John Kessenich69f4b512013-09-04 21:19:27 +00001257 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001258
John Kessenich4be4aeb2017-06-23 10:50:22 -06001259 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001260
John Kessenich3494b4d2017-05-22 15:00:42 -06001261 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -06001262 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1263 includer.pushExternalLocalDirectory(dir); });
John Kessenichdeec1932019-08-13 08:00:30 -06001264#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001265 if (Options & EOptionOutputPreprocessed) {
1266 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001267 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001268 PutsIfNonEmpty(str.c_str());
1269 } else {
1270 CompileFailed = true;
1271 }
1272 StderrIfNonEmpty(shader->getInfoLog());
1273 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001274 continue;
1275 }
John Kessenichdeec1932019-08-13 08:00:30 -06001276#endif
John Kessenich086febc2018-10-25 12:43:02 -06001277
John Kessenich3494b4d2017-05-22 15:00:42 -06001278 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001279 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001280
John Kessenich69f4b512013-09-04 21:19:27 +00001281 program.addShader(shader);
1282
John Kessenichc57b2a92016-01-16 15:30:03 -07001283 if (! (Options & EOptionSuppressInfolog) &&
1284 ! (Options & EOptionMemoryLeakMode)) {
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001285 if (!beQuiet)
1286 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001287 PutsIfNonEmpty(shader->getInfoLog());
1288 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001289 }
John Kessenich69f4b512013-09-04 21:19:27 +00001290 }
1291
1292 //
1293 // Program-level processing...
1294 //
1295
John Kessenich4d65ee32016-03-12 18:17:47 -07001296 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001297 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001298 LinkFailed = true;
1299
John Kessenichb6d3ee52019-08-06 02:20:45 -06001300#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001301 // Map IO
1302 if (Options & EOptionSpv) {
1303 if (!program.mapIO())
1304 LinkFailed = true;
1305 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001306#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001307
John Kessenich4d65ee32016-03-12 18:17:47 -07001308 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001309 if (! (Options & EOptionSuppressInfolog) &&
1310 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001311 PutsIfNonEmpty(program.getInfoLog());
1312 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001313 }
1314
John Kessenichb6d3ee52019-08-06 02:20:45 -06001315#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001316 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001317 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001318 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001319 program.dumpReflection();
1320 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001321#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001322
John Kessenich4d65ee32016-03-12 18:17:47 -07001323 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001324 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001325 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001326 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001327 else {
1328 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001329 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001330 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001331 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001332 glslang::SpvOptions spvOptions;
1333 if (Options & EOptionDebug)
1334 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001335 else if (stripDebugInfo)
1336 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001337 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1338 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001339 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001340 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001341 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001342
1343 // Dump the spv to a file or stdout, etc., but only if not doing
1344 // memory/perf testing, as it's not internal to programmatic use.
1345 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001346 printf("%s", logger.getAllMessages().c_str());
1347 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001348 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001349 } else {
1350 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1351 }
John Kessenich23d27752019-07-28 02:12:10 -06001352#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001353 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001354 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001355#endif
John Kessenichacba7722015-03-04 03:48:38 +00001356 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001357 }
John Kessenich92f90382014-07-28 04:21:04 +00001358 }
1359 }
1360 }
1361
John Kessenich5b0f13a2013-11-01 03:08:40 +00001362 // Free everything up, program has to go before the shaders
1363 // because it might have merged stuff from the shaders, and
1364 // the stuff from the shaders has to have its destructors called
1365 // before the pools holding the memory in the shaders is freed.
1366 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001367 while (shaders.size() > 0) {
1368 delete shaders.back();
1369 shaders.pop_back();
1370 }
John Kessenich69f4b512013-09-04 21:19:27 +00001371}
1372
John Kessenichc57b2a92016-01-16 15:30:03 -07001373//
1374// Do file IO part of compile and link, handing off the pure
1375// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1376// be put in a loop for testing memory footprint and performance.
1377//
1378// This is just for linking mode: meaning all the shaders will be put into the
1379// the same program linked together.
1380//
1381// This means there are a limited number of work items (not multi-threading mode)
1382// and that the point is testing at the linking level. Hence, to enable
1383// performance and memory testing, the actual compile/link can be put in
1384// a loop, independent of processing the work items and file IO.
1385//
Juan Lopeza558b262017-04-02 23:04:00 +02001386void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001387{
1388 std::vector<ShaderCompUnit> compUnits;
1389
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001390 // If this is using stdin, we can't really detect multiple different file
1391 // units by input type. We need to assume that we're just being given one
1392 // file of a certain type.
1393 if ((Options & EOptionStdin) != 0) {
1394 ShaderCompUnit compUnit(FindLanguage("stdin"));
1395 std::istreambuf_iterator<char> begin(std::cin), end;
1396 std::string tempString(begin, end);
1397 char* fileText = strdup(tempString.c_str());
1398 std::string fileName = "stdin";
1399 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001400 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001401 } else {
1402 // Transfer all the work items from to a simple list of
1403 // of compilation units. (We don't care about the thread
1404 // work-item distribution properties in this path, which
1405 // is okay due to the limited number of shaders, know since
1406 // they are all getting linked together.)
1407 glslang::TWorkItem* workItem;
1408 while (Worklist.remove(workItem)) {
1409 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1410 char* fileText = ReadFileData(workItem->name.c_str());
1411 if (fileText == nullptr)
1412 usage();
1413 compUnit.addString(workItem->name, fileText);
1414 compUnits.push_back(compUnit);
1415 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001416 }
1417
1418 // Actual call to programmatic processing of compile and link,
1419 // in a loop for testing memory and performance. This part contains
1420 // all the perf/memory that a programmatic consumer will care about.
1421 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1422 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1423 CompileAndLinkShaderUnits(compUnits);
1424
1425 if (Options & EOptionMemoryLeakMode)
1426 glslang::OS_DumpMemoryCounters();
1427 }
1428
John Kessenicha9313662017-06-15 10:40:49 -06001429 // free memory from ReadFileData, which got stored in a const char*
1430 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001431 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001432 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001433}
1434
John Kessenich94f28eb2017-11-13 01:32:06 -07001435int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001436{
Juan Lopeza558b262017-04-02 23:04:00 +02001437 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001438 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001439 assert(item);
1440 workList.add(item.get());
1441 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001442
John Kessenich23d27752019-07-28 02:12:10 -06001443#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001444 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001445 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001446 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001447 return ESuccess;
1448 }
John Kessenich23d27752019-07-28 02:12:10 -06001449#endif
John Kessenich05a70632013-09-17 19:26:08 +00001450
John Kessenichc6c80a62018-03-05 22:23:17 -07001451 if (Options & EOptionDumpBareVersion) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001452 printf("%d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1453 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenichc6c80a62018-03-05 22:23:17 -07001454 if (workList.empty())
1455 return ESuccess;
1456 } else if (Options & EOptionDumpVersions) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001457 printf("Glslang Version: %d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR,
1458 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenich319de232013-12-04 04:43:40 +00001459 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1460 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001461 std::string spirvVersion;
1462 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001463 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001464 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001465 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001466 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001467 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1468 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001469 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001470 return ESuccess;
1471 }
1472
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001473 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001474 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001475 }
1476
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001477 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001478 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1479 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001480 }
1481
John Kessenich05a70632013-09-17 19:26:08 +00001482 ProcessConfigFile();
1483
John Kessenich086febc2018-10-25 12:43:02 -06001484 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001485 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001486
John Kessenich69f4b512013-09-04 21:19:27 +00001487 //
1488 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001489 // 1) linking all arguments together, single-threaded, new C++ interface
1490 // 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 +00001491 //
John Kessenich086febc2018-10-25 12:43:02 -06001492 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001493 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001494 glslang::InitializeProcess(); // also test reference counting of users
1495 glslang::InitializeProcess(); // also test reference counting of users
1496 glslang::FinalizeProcess(); // also test reference counting of users
1497 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001498 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001499 glslang::FinalizeProcess();
1500 } else {
1501 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001502 ShInitialize(); // also test reference counting of users
1503 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001504
Juan Lopeza558b262017-04-02 23:04:00 +02001505 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001506
John Kesseniche2c15b42017-11-16 22:48:41 -07001507 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001508 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001509 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001510 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001511 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001512 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001513 return EFailThreadCreate;
1514 }
John Kessenicha0af4732012-12-12 21:15:54 +00001515 }
Juan Lopeza558b262017-04-02 23:04:00 +02001516
1517 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001518 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001519 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001520
1521 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001522 for (size_t w = 0; w < WorkItems.size(); ++w) {
1523 if (WorkItems[w]) {
1524 if (printShaderNames || WorkItems[w]->results.size() > 0)
1525 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1526 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001527 }
1528 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001529
1530 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001531 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001532
John Kessenichc999ba22013-11-07 23:33:24 +00001533 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001534 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001535 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001536 return EFailLink;
1537
1538 return 0;
1539}
1540
John Kessenich94f28eb2017-11-13 01:32:06 -07001541int C_DECL main(int argc, char* argv[])
1542{
1543 ProcessArguments(WorkItems, argc, argv);
1544
1545 int ret = 0;
1546
1547 // Loop over the entire init/finalize cycle to watch memory changes
1548 const int iterations = 1;
1549 if (iterations > 1)
1550 glslang::OS_DumpMemoryCounters();
1551 for (int i = 0; i < iterations; ++i) {
1552 ret = singleMain();
1553 if (iterations > 1)
1554 glslang::OS_DumpMemoryCounters();
1555 }
1556
1557 return ret;
1558}
1559
John Kessenicha0af4732012-12-12 21:15:54 +00001560//
1561// Deduce the language from the filename. Files must end in one of the
1562// following extensions:
1563//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001564// .vert = vertex
1565// .tesc = tessellation control
1566// .tese = tessellation evaluation
1567// .geom = geometry
1568// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001569// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001570// .rgen = ray generation
1571// .rint = ray intersection
1572// .rahit = ray any hit
1573// .rchit = ray closest hit
1574// .rmiss = ray miss
1575// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001576// .mesh = mesh
1577// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001578// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1579// where <stage> is one of the stages listed above.
1580//
1581EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001582{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001583 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001584 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001585 stageName = shaderStageName;
1586 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001587 // Note: "first" extension means "first from the end", i.e.
1588 // if the file is named foo.vert.glsl, then "glsl" is first,
1589 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001590 size_t firstExtStart = name.find_last_of(".");
1591 bool hasFirstExt = firstExtStart != std::string::npos;
1592 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1593 bool hasSecondExt = secondExtStart != std::string::npos;
1594 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1595 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001596 if (usesUnifiedExt && firstExt == "hlsl")
1597 Options |= EOptionReadHlsl;
1598 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001599 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001600 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001601 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001602 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001603 usage();
1604 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001605 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001606 } else
1607 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001608
John Kessenichfccbb8b2018-04-17 17:24:03 -06001609 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001610 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001611 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001612 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001613 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001614 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001615 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001616 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001617 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001618 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001619 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001620 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001621 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001622 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001623 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001624 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001625 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001626 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001627 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001628 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001629 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001630 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001631 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001632 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001633 else if (stageName == "mesh")
1634 return EShLangMeshNV;
1635 else if (stageName == "task")
1636 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001637
1638 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001639 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001640}
1641
John Kessenicha0af4732012-12-12 21:15:54 +00001642//
John Kessenichecba76f2017-01-06 00:34:48 -07001643// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001644// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001645//
John Kessenich51cdd902014-02-18 23:37:57 +00001646void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001647{
John Kessenichca3457f2015-05-18 01:59:45 +00001648 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001649 char* shaderString;
1650 if ((Options & EOptionStdin) != 0) {
1651 std::istreambuf_iterator<char> begin(std::cin), end;
1652 std::string tempString(begin, end);
1653 shaderString = strdup(tempString.c_str());
1654 } else {
1655 shaderString = ReadFileData(fileName);
1656 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001657
1658 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001659 int* lengths = new int[1];
1660 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001661
John Kessenich52ac67e2013-05-05 23:46:22 +00001662 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001663 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001664
John Kessenicha9313662017-06-15 10:40:49 -06001665 if (UserPreamble.isSet())
1666 Error("-D and -U options require -l (linking)\n");
1667
John Kessenich94a81fb2013-08-31 02:41:30 +00001668 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1669 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001670 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001671 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001672 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001673 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001674 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001675 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1676 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001677 }
John Kessenicha0af4732012-12-12 21:15:54 +00001678
John Kessenich94a81fb2013-08-31 02:41:30 +00001679 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001680 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001681 }
John Kessenicha0af4732012-12-12 21:15:54 +00001682
John Kessenich41cf6b52013-06-25 18:10:05 +00001683 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001684 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001685
John Kessenichc999ba22013-11-07 23:33:24 +00001686 if (ret == 0)
1687 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001688}
1689
John Kessenicha0af4732012-12-12 21:15:54 +00001690//
1691// print usage to stdout
1692//
1693void usage()
1694{
John Kessenich319de232013-12-04 04:43:40 +00001695 printf("Usage: glslangValidator [option]... [file]...\n"
1696 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001697 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1698 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001699 " (see -c option below for generating a template)\n"
1700 " .vert for a vertex shader\n"
1701 " .tesc for a tessellation control shader\n"
1702 " .tese for a tessellation evaluation shader\n"
1703 " .geom for a geometry shader\n"
1704 " .frag for a fragment shader\n"
1705 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001706 " .mesh for a mesh shader\n"
1707 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001708 " .rgen for a ray generation shader\n"
1709 " .rint for a ray intersection shader\n"
1710 " .rahit for a ray any hit shader\n"
1711 " .rchit for a ray closest hit shader\n"
1712 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001713 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001714 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1715 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001716 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001717 "Options:\n"
1718 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001719 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001720 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1721 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001722 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001723 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001724 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001725 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001726 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001727 " which will appear in #define GL_SPIRV ver;\n"
1728 " '--client opengl100' is the same as -G100;\n"
Jeremy Hayesb73afa82021-01-05 15:54:41 -07001729 " a '--target-env' for OpenGL will also imply '-G';\n"
1730 " currently only supports GLSL\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001731 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001732 " -I<dir> add dir to the include search path; includer's directory\n"
1733 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001734 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1735 " -Os optimizes SPIR-V to minimize size\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001736 " -R use relaxed verification rules for generating Vulkan SPIR-V,\n"
1737 " allowing the use of default uniforms, atomic_uints, and\n"
1738 " gl_VertexID and gl_InstanceID keywords.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001739 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001740 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001741 " -U<name> | --undef-macro <name> | --U <name>\n"
1742 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001743 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001744 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001745 " 'ver', when present, is the version of the input semantics,\n"
1746 " which will appear in #define VULKAN ver\n"
1747 " '--client vulkan100' is the same as -V100\n"
1748 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001749 " -c configuration dump;\n"
1750 " creates the default configuration file (redirect to a .conf file)\n"
1751 " -d default to desktop (#version 110) when there is no shader #version\n"
1752 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001753 " -e <name> | --entry-point <name>\n"
1754 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001755 " -f{hlsl_functionality1}\n"
1756 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001757 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001758 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001759 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001760 " -h print this usage message\n"
1761 " -i intermediate tree (glslang AST) is printed out\n"
1762 " -l link all input files together to form a single module\n"
1763 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001764 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001765 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001766 " -r | --relaxed-errors"
1767 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001768 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001769 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001770 " -v | --version\n"
1771 " print version strings\n"
1772 " -w | --suppress-warnings\n"
1773 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001774 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001775 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001776 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001777 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1778 " without explicit bindings\n"
1779 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1780 " 'location' (fragile, not cross stage)\n"
1781 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001782 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001783 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1784 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1785 " scalars\n"
1786 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1787 " works independently of source language\n"
1788 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1789 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001790 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001791 " texture/sampler combo like DirectX9 would,\n"
1792 " and recognizes DirectX9-specific semantics\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001793 " --invert-y | --iy invert position.Y output in vertex shader\n"
1794 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001795 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001796 " --no-storage-format | --nsf use Unknown image format\n"
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001797 " --quiet do not print anything to stdout, unless\n"
1798 " requested by another option\n"
baldurk4513df92019-02-08 10:48:48 +00001799 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1800 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001801 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001802 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1803 " shaders rather than just vertex/fragment\n"
1804 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1805 " separately to uniforms\n"
1806 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1807 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001808 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1809 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001810 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001811 " set descriptor set and binding for\n"
1812 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001813 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001814 " set descriptor set for all resources\n"
1815 " --rsb synonym for --resource-set-binding\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001816 " --set-block-backing name {uniform|buffer|push_constant}\n"
1817 " changes the backing type of a uniform, buffer,\n"
1818 " or push_constant block declared in\n"
1819 " in the program, when using -R option.\n"
1820 " This can be used to change the backing\n"
1821 " for existing blocks as well as implicit ones\n"
1822 " such as 'gl_DefaultUniformBlock'.\n"
1823 " --sbs synonym for set-block-storage\n"
1824 " --set-atomic-counter-block name set\n"
1825 " set name, and descriptor set for\n"
1826 " atomic counter blocks, with -R opt\n"
1827 " --sacb synonym for set-atomic-counter-block\n"
1828 " --set-default-uniform-block name set binding\n"
1829 " set name, descriptor set, and binding for\n"
1830 " global default-uniform-block, with -R opt\n"
1831 " --sdub synonym for set-default-uniform-block\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001832 " --shift-image-binding [stage] num\n"
1833 " base binding number for images (uav)\n"
1834 " --shift-image-binding [stage] [num set]...\n"
1835 " per-descriptor-set shift values\n"
1836 " --sib synonym for --shift-image-binding\n"
1837 " --shift-sampler-binding [stage] num\n"
1838 " base binding number for samplers\n"
1839 " --shift-sampler-binding [stage] [num set]...\n"
1840 " per-descriptor-set shift values\n"
1841 " --ssb synonym for --shift-sampler-binding\n"
1842 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1843 " --shift-ssbo-binding [stage] [num set]...\n"
1844 " per-descriptor-set shift values\n"
1845 " --sbb synonym for --shift-ssbo-binding\n"
1846 " --shift-texture-binding [stage] num\n"
1847 " base binding number for textures\n"
1848 " --shift-texture-binding [stage] [num set]...\n"
1849 " per-descriptor-set shift values\n"
1850 " --stb synonym for --shift-texture-binding\n"
1851 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1852 " --shift-uav-binding [stage] [num set]...\n"
1853 " per-descriptor-set shift values\n"
1854 " --suavb synonym for --shift-uav-binding\n"
1855 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1856 " --shift-UBO-binding [stage] [num set]...\n"
1857 " per-descriptor-set shift values\n"
1858 " --sub synonym for --shift-UBO-binding\n"
1859 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1860 " --spirv-dis output standard-form disassembly; works only\n"
1861 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001862 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001863 " --source-entrypoint <name> the given shader source function is\n"
1864 " renamed to be the <name> given in -e\n"
1865 " --sep synonym for --source-entrypoint\n"
1866 " --stdin read from stdin instead of from a file;\n"
1867 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001868 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001869 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001870 " Set the execution environment that the\n"
1871 " generated code will be executed in.\n"
1872 " Defaults to:\n"
1873 " * vulkan1.0 under --client vulkan<ver>\n"
1874 " * opengl under --client opengl<ver>\n"
1875 " * spirv1.0 under --target-env vulkan1.0\n"
1876 " * spirv1.3 under --target-env vulkan1.1\n"
1877 " * spirv1.5 under --target-env vulkan1.2\n"
1878 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001879 " --variable-name <name>\n"
1880 " --vn <name> creates a C header file that contains a\n"
1881 " uint32_t array named <name>\n"
1882 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001883 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001884
1885 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001886}
1887
John Kessenich3ce4e592014-10-06 19:57:34 +00001888#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001889
1890#include <errno.h>
1891
1892int fopen_s(
1893 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001894 const char* filename,
1895 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001896)
1897{
1898 if (!pFile || !filename || !mode) {
1899 return EINVAL;
1900 }
1901
1902 FILE* f = fopen(filename, mode);
1903 if (! f) {
1904 if (errno != 0) {
1905 return errno;
1906 } else {
1907 return ENOENT;
1908 }
1909 }
1910 *pFile = f;
1911
1912 return 0;
1913}
1914
1915#endif
1916
John Kessenicha0af4732012-12-12 21:15:54 +00001917//
1918// Malloc a string of sufficient size and read a string into it.
1919//
John Kessenich04acb1b2017-06-14 17:36:50 -06001920char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001921{
John Kessenichb3297152015-07-11 18:01:03 -06001922 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001923 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001924 if (errorCode || in == nullptr)
1925 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001926
John Kessenich04acb1b2017-06-14 17:36:50 -06001927 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001928 while (fgetc(in) != EOF)
1929 count++;
1930
John Kessenichd6c72a42014-08-18 19:42:35 +00001931 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001932
John Kessenich04acb1b2017-06-14 17:36:50 -06001933 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1934 if ((int)fread(return_data, 1, count, in) != count) {
1935 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001936 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001937 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001938
John Kessenich04acb1b2017-06-14 17:36:50 -06001939 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001940 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001941
John Kessenicha0af4732012-12-12 21:15:54 +00001942 return return_data;
1943}
1944
John Kessenich04acb1b2017-06-14 17:36:50 -06001945void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001946{
John Kessenichb3297152015-07-11 18:01:03 -06001947 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001948}
1949
John Kessenich54d8cda2013-02-11 22:36:01 +00001950void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001951{
John Kessenichfae38ee2015-06-10 23:23:12 +00001952 if (num >= 0 )
1953 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1954 else
1955 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001956}