blob: e62f92f7193e78cf3e111fbdb8a5d3da2fce83bd [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenich56364b62020-03-01 04:51:40 -07004// Copyright (C) 2016-2020 Google, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00005//
John Kessenich927608b2017-01-06 12:34:14 -07006// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00007//
John Kessenich927608b2017-01-06 12:34:14 -07008// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions
10// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000011//
12// Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//
15// Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following
17// disclaimer in the documentation and/or other materials provided
18// with the distribution.
19//
20// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21// contributors may be used to endorse or promote products derived
22// from this software without specific prior written permission.
23//
John Kessenich927608b2017-01-06 12:34:14 -070024// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000036//
John Kessenich05a70632013-09-17 19:26:08 +000037
38// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030039#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000040#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030041#endif
John Kessenich05a70632013-09-17 19:26:08 +000042
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040043#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000044#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060045#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000046#include "./../glslang/Include/ShHandle.h"
47#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060058#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020059#include <memory>
60#include <thread>
Georg Lehmannf0502092021-04-20 00:22:15 +020061#include <set>
John Kessenicha0af4732012-12-12 21:15:54 +000062
baldurk876a0e32015-11-16 18:03:28 +010063#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000064
Ben Claytonfbe9a232020-06-17 11:17:19 +010065// Build-time generated includes
66#include "glslang/build_info.h"
67
John Kessenicha0af4732012-12-12 21:15:54 +000068extern "C" {
Ben Claytond64e8592020-06-29 14:20:19 +010069 GLSLANG_EXPORT void ShOutputHtml();
John Kessenicha0af4732012-12-12 21:15:54 +000070}
71
John Kessenich94a81fb2013-08-31 02:41:30 +000072// Command-line options
73enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070074 EOptionNone = 0,
75 EOptionIntermediate = (1 << 0),
76 EOptionSuppressInfolog = (1 << 1),
77 EOptionMemoryLeakMode = (1 << 2),
78 EOptionRelaxedErrors = (1 << 3),
79 EOptionGiveWarnings = (1 << 4),
80 EOptionLinkProgram = (1 << 5),
81 EOptionMultiThreaded = (1 << 6),
82 EOptionDumpConfig = (1 << 7),
83 EOptionDumpReflection = (1 << 8),
84 EOptionSuppressWarnings = (1 << 9),
85 EOptionDumpVersions = (1 << 10),
86 EOptionSpv = (1 << 11),
87 EOptionHumanReadableSpv = (1 << 12),
88 EOptionVulkanRules = (1 << 13),
89 EOptionDefaultDesktop = (1 << 14),
90 EOptionOutputPreprocessed = (1 << 15),
91 EOptionOutputHexadecimal = (1 << 16),
92 EOptionReadHlsl = (1 << 17),
93 EOptionCascadingErrors = (1 << 18),
94 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060095 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070096 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070097 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060098 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060099 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -0600100 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -0600101 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200102 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -0600103 EOptionOptimizeDisable = (1 << 28),
104 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700105 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700106 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000107};
John Kessenich5d610ee2018-03-07 18:05:55 -0700108bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600109bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600110bool SpvToolsValidate = false;
John Kessenich605afc72019-06-17 23:33:09 -0600111bool NaNClamp = false;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400112bool stripDebugInfo = false;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200113bool beQuiet = false;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600114bool VulkanRulesRelaxed = false;
Greg Fischerebfca602021-04-21 16:49:53 -0600115bool autoSampledTextures = false;
John Kessenich94a81fb2013-08-31 02:41:30 +0000116
John Kessenicha0af4732012-12-12 21:15:54 +0000117//
John Kessenich68d78fd2015-07-12 19:28:10 -0600118// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000119//
120enum TFailCode {
121 ESuccess = 0,
122 EFailUsage,
123 EFailCompile,
124 EFailLink,
125 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000126 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000127 EFailLinkerCreate
128};
129
130//
John Kessenich68d78fd2015-07-12 19:28:10 -0600131// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000132//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600133EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000134void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000135void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600136char* ReadFileData(const char* fileName);
137void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000138void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000139
John Kessenichc999ba22013-11-07 23:33:24 +0000140// Globally track if any compile or link failure.
141bool CompileFailed = false;
142bool LinkFailed = false;
143
John Kessenich94f28eb2017-11-13 01:32:06 -0700144// array of unique places to leave the shader names and infologs for the asynchronous compiles
145std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
146
John Kessenich05a70632013-09-17 19:26:08 +0000147TBuiltInResource Resources;
148std::string ConfigFile;
149
John Kessenicha0af4732012-12-12 21:15:54 +0000150//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600151// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000152//
153void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000154{
John Kessenich04acb1b2017-06-14 17:36:50 -0600155 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500156 Resources = glslang::DefaultTBuiltInResource;
John Kessenich23d27752019-07-28 02:12:10 -0600157#ifndef GLSLANG_WEB
John Kessenich04acb1b2017-06-14 17:36:50 -0600158 else {
159 char* configString = ReadFileData(ConfigFile.c_str());
160 glslang::DecodeResourceLimits(&Resources, configString);
161 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000162 }
John Kessenich23d27752019-07-28 02:12:10 -0600163#endif
John Kessenicha0af4732012-12-12 21:15:54 +0000164}
165
baldurk6d477852019-01-29 15:45:56 +0000166int ReflectOptions = EShReflectionDefault;
John Kessenich94a81fb2013-08-31 02:41:30 +0000167int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600168const char* ExecutableName = nullptr;
169const char* binaryFileName = nullptr;
Georg Lehmannf0502092021-04-20 00:22:15 +0200170const char* depencyFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700171const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600172const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400173const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800174const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800175bool HlslEnable16BitTypes = false;
John Kessenichbd1c1832018-12-07 18:38:26 -0700176bool HlslDX9compatible = false;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200177bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600178std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600179
180// Source environment
181// (source 'Client' is currently the same as target 'Client')
182int ClientInputSemanticsVersion = 100;
183
184// Target environment
185glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
186glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
187glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
188glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
189
John Kessenich66011cb2018-03-06 16:12:04 -0700190std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600191
LoopDawg08a14422017-10-17 19:27:14 -0600192// Per descriptor-set binding base data
193typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
194
Neil Roberts16f53472018-03-20 17:30:53 +0100195std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100196int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100197
LoopDawg08a14422017-10-17 19:27:14 -0600198std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
199std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900200std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600201
greg-lunarg4e064ee2021-03-15 11:26:11 -0600202std::vector<std::pair<std::string, glslang::TBlockStorageClass>> blockStorageOverrides;
203
204bool setGlobalUniformBlock = false;
205std::string globalUniformName;
206unsigned int globalUniformBinding;
207unsigned int globalUniformSet;
208
209bool setGlobalBufferBlock = false;
210std::string atomicCounterBlockName;
211unsigned int atomicCounterBlockSet;
212
John Kessenicha9313662017-06-15 10:40:49 -0600213// Add things like "#define ..." to a preamble to use in the beginning of the shader.
214class TPreamble {
215public:
216 TPreamble() { }
217
218 bool isSet() const { return text.size() > 0; }
219 const char* get() const { return text.c_str(); }
220
221 // #define...
222 void addDef(std::string def)
223 {
224 text.append("#define ");
225 fixLine(def);
226
John Kessenich6f988922020-01-07 07:03:11 -0700227 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600228 Processes.back().append(def);
229
John Kessenicha9313662017-06-15 10:40:49 -0600230 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600231 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600232 if (equal != def.npos)
233 def[equal] = ' ';
234
235 text.append(def);
236 text.append("\n");
237 }
238
239 // #undef...
240 void addUndef(std::string undef)
241 {
242 text.append("#undef ");
243 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600244
John Kessenich6f988922020-01-07 07:03:11 -0700245 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600246 Processes.back().append(undef);
247
John Kessenicha9313662017-06-15 10:40:49 -0600248 text.append(undef);
249 text.append("\n");
250 }
251
252protected:
253 void fixLine(std::string& line)
254 {
255 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600256 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600257 if (end != line.npos)
258 line = line.substr(0, end);
259 }
260
261 std::string text; // contents of preamble
262};
263
John Kessenich83855b92020-03-30 00:27:31 -0600264// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600265TPreamble UserPreamble;
266
John Kessenich68d78fd2015-07-12 19:28:10 -0600267//
268// Create the default name for saving a binary if -o is not provided.
269//
270const char* GetBinaryName(EShLanguage stage)
271{
272 const char* name;
273 if (binaryFileName == nullptr) {
274 switch (stage) {
275 case EShLangVertex: name = "vert.spv"; break;
276 case EShLangTessControl: name = "tesc.spv"; break;
277 case EShLangTessEvaluation: name = "tese.spv"; break;
278 case EShLangGeometry: name = "geom.spv"; break;
279 case EShLangFragment: name = "frag.spv"; break;
280 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400281 case EShLangRayGen: name = "rgen.spv"; break;
282 case EShLangIntersect: name = "rint.spv"; break;
283 case EShLangAnyHit: name = "rahit.spv"; break;
284 case EShLangClosestHit: name = "rchit.spv"; break;
285 case EShLangMiss: name = "rmiss.spv"; break;
286 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700287 case EShLangMeshNV: name = "mesh.spv"; break;
288 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600289 default: name = "unknown"; break;
290 }
291 } else
292 name = binaryFileName;
293
294 return name;
295}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000296
John Kessenich05a70632013-09-17 19:26:08 +0000297//
298// *.conf => this is a config file that can set limits/resources
299//
300bool SetConfigFile(const std::string& name)
301{
302 if (name.size() < 5)
303 return false;
304
John Kessenich4c706852013-10-11 16:28:43 +0000305 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000306 ConfigFile = name;
307 return true;
308 }
309
310 return false;
311}
312
John Kessenich68d78fd2015-07-12 19:28:10 -0600313//
314// Give error and exit with failure code.
315//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700316void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600317{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700318 fprintf(stderr, "%s: Error: ", ExecutableName);
319 if (detail != nullptr)
320 fprintf(stderr, "%s: ", detail);
321 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600322 exit(EFailUsage);
323}
324
325//
LoopDawg08a14422017-10-17 19:27:14 -0600326// Process an optional binding base of one the forms:
327// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600328// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600329
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600330// Where stage is one of the forms accepted by FindLanguage, and base is an integer
331//
LoopDawg08a14422017-10-17 19:27:14 -0600332void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600333{
334 if (argc < 2)
335 usage();
336
LoopDawg08a14422017-10-17 19:27:14 -0600337 EShLanguage lang = EShLangCount;
338 int singleBase = 0;
339 TPerSetBaseBinding perSetBase;
340 int arg = 1;
341
342 // Parse stage, if given
343 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600344 if (argc < 3) // this form needs one more argument
345 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700346
LoopDawg08a14422017-10-17 19:27:14 -0600347 lang = FindLanguage(argv[arg++], false);
348 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600349
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800350 if ((argc - arg) >= 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600351 // Parse a per-set binding base
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800352 do {
LoopDawg08a14422017-10-17 19:27:14 -0600353 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600354 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600355 perSetBase[setNum] = baseNum;
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800356 } while ((argc - arg) >= 2 && isdigit(argv[arg + 0][0]) && isdigit(argv[arg + 1][0]));
LoopDawg08a14422017-10-17 19:27:14 -0600357 } else {
358 // Parse single binding base
359 singleBase = atoi(argv[arg++]);
360 }
361
362 argc -= (arg-1);
363 argv += (arg-1);
364
365 // Set one or all languages
366 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
367 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
368
369 for (int lang = langMin; lang < langMax; ++lang) {
370 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600371 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600372 else
373 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600374 }
375}
376
Hyangran Park36dc8292017-05-02 16:27:29 +0900377void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
378{
379 if (argc < 2)
380 usage();
381
382 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600383 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900384 usage();
385
LoopDawg52017192017-07-14 15:15:47 -0600386 // Parse form: --argname stage [regname set base...], or:
387 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900388 const EShLanguage lang = FindLanguage(argv[1], false);
389
LoopDawg52017192017-07-14 15:15:47 -0600390 argc--;
391 argv++;
392
393 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
394 base[lang].push_back(argv[1]);
395
396 argc--;
397 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900398 }
LoopDawg52017192017-07-14 15:15:47 -0600399
400 // Must have one arg, or a multiple of three (for [regname set binding] triples)
401 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
402 usage();
403
Hyangran Park36dc8292017-05-02 16:27:29 +0900404 } else {
LoopDawg52017192017-07-14 15:15:47 -0600405 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900406 for (int lang=0; lang<EShLangCount; ++lang)
407 base[lang].push_back(argv[1]);
408
409 argc--;
410 argv++;
411 }
412}
413
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600414//
greg-lunarg4e064ee2021-03-15 11:26:11 -0600415// Process an optional binding base of one the forms:
416// --argname name {uniform|buffer|push_constant}
417void ProcessBlockStorage(int& argc, char**& argv, std::vector<std::pair<std::string, glslang::TBlockStorageClass>>& storage)
418{
419 if (argc < 3)
420 usage();
421
422 glslang::TBlockStorageClass blockStorage = glslang::EbsNone;
423
424 std::string strBacking(argv[2]);
425 if (strBacking == "uniform")
426 blockStorage = glslang::EbsUniform;
427 else if (strBacking == "buffer")
428 blockStorage = glslang::EbsStorageBuffer;
429 else if (strBacking == "push_constant")
430 blockStorage = glslang::EbsPushConstant;
431 else {
432 printf("%s: invalid block storage\n", strBacking.c_str());
433 usage();
434 }
435
436 storage.push_back(std::make_pair(std::string(argv[1]), blockStorage));
437
438 argc -= 2;
439 argv += 2;
440}
441
442inline bool isNonDigit(char c) {
443 // a non-digit character valid in a glsl identifier
444 return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
445}
446
447// whether string isa valid identifier to be used in glsl
448bool isValidIdentifier(const char* str) {
449 std::string idn(str);
450
451 if (idn.length() == 0) {
452 return false;
453 }
454
455 if (idn.length() >= 3 && idn.substr(0, 3) == "gl_") {
456 // identifiers startin with "gl_" are reserved
457 return false;
458 }
459
460 if (!isNonDigit(idn[0])) {
461 return false;
462 }
463
464 for (unsigned int i = 1; i < idn.length(); ++i) {
465 if (!(isdigit(idn[i]) || isNonDigit(idn[i]))) {
466 return false;
467 }
468 }
469
470 return true;
471}
472
473// Process settings for either the global buffer block or global unfirom block
474// of the form:
475// --argname name set binding
476void ProcessGlobalBlockSettings(int& argc, char**& argv, std::string* name, unsigned int* set, unsigned int* binding)
477{
478 if (argc < 4)
479 usage();
480
481 unsigned int curArg = 1;
482
483 assert(name || set || binding);
484
485 if (name) {
486 if (!isValidIdentifier(argv[curArg])) {
487 printf("%s: invalid identifier\n", argv[curArg]);
488 usage();
489 }
490 *name = argv[curArg];
491
492 curArg++;
493 }
494
495 if (set) {
496 errno = 0;
497 int setVal = ::strtol(argv[curArg], NULL, 10);
498 if (errno || setVal < 0) {
499 printf("%s: invalid set\n", argv[curArg]);
500 usage();
501 }
502 *set = setVal;
503
504 curArg++;
505 }
506
507 if (binding) {
508 errno = 0;
509 int bindingVal = ::strtol(argv[curArg], NULL, 10);
510 if (errno || bindingVal < 0) {
511 printf("%s: invalid binding\n", argv[curArg]);
512 usage();
513 }
514 *binding = bindingVal;
515
516 curArg++;
517 }
518
519 argc -= (curArg - 1);
520 argv += (curArg - 1);
521}
522
523//
John Kessenich68d78fd2015-07-12 19:28:10 -0600524// Do all command-line argument parsing. This includes building up the work-items
525// to be processed later, and saving all the command-line options.
526//
527// Does not return (it exits) if command-line is fatally flawed.
528//
Juan Lopeza558b262017-04-02 23:04:00 +0200529void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000530{
LoopDawg08a14422017-10-17 19:27:14 -0600531 for (int res = 0; res < glslang::EResCount; ++res)
532 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600533
John Kessenich38f3b892013-09-06 19:52:57 +0000534 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200535 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000536
John Kessenichc178f0a2017-06-23 10:58:31 -0600537 const auto bumpArg = [&]() {
538 if (argc > 0) {
539 argc--;
540 argv++;
541 }
542 };
543
544 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600545 const auto getStringOperand = [&](const char* desc) {
546 if (argv[0][2] == 0) {
547 printf("%s must immediately follow option (no spaces)\n", desc);
548 exit(EFailUsage);
549 }
550 return argv[0] + 2;
551 };
552
John Kessenich6353d552017-06-23 11:11:09 -0600553 // read a number attached to a single-letter option
554 const auto getAttachedNumber = [&](const char* desc) {
555 int num = atoi(argv[0] + 2);
556 if (num == 0) {
557 printf("%s: expected attached non-0 number\n", desc);
558 exit(EFailUsage);
559 }
560 return num;
561 };
562
563 // minimum needed (without overriding something else) to target Vulkan SPIR-V
564 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600565 if (Client == glslang::EShClientNone)
566 ClientVersion = glslang::EShTargetVulkan_1_0;
567 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600568 Options |= EOptionSpv;
569 Options |= EOptionVulkanRules;
570 Options |= EOptionLinkProgram;
571 };
572
573 // minimum needed (without overriding something else) to target OpenGL SPIR-V
574 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600575 if (Client == glslang::EShClientNone)
576 ClientVersion = glslang::EShTargetOpenGL_450;
577 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600578 Options |= EOptionSpv;
579 Options |= EOptionLinkProgram;
580 // undo a -H default to Vulkan
581 Options &= ~EOptionVulkanRules;
582 };
583
Neil Roberts16f53472018-03-20 17:30:53 +0100584 const auto getUniformOverride = [getStringOperand]() {
585 const char *arg = getStringOperand("-u<name>:<location>");
586 const char *split = strchr(arg, ':');
587 if (split == NULL) {
588 printf("%s: missing location\n", arg);
589 exit(EFailUsage);
590 }
591 errno = 0;
592 int location = ::strtol(split + 1, NULL, 10);
593 if (errno) {
594 printf("%s: invalid location\n", arg);
595 exit(EFailUsage);
596 }
597 return std::make_pair(std::string(arg, split - arg), location);
598 };
599
John Kessenichc178f0a2017-06-23 10:58:31 -0600600 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000601 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000602 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600603 case '-':
604 {
605 std::string lowerword(argv[0]+2);
606 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
607
608 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600609 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600610 lowerword == "auto-map-binding" ||
611 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600612 Options |= EOptionAutoMapBindings;
613 } else if (lowerword == "auto-map-locations" || // synonyms
614 lowerword == "aml") {
615 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100616 } else if (lowerword == "uniform-base") {
617 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700618 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100619 uniformBase = ::strtol(argv[1], NULL, 10);
620 bumpArg();
621 break;
John Kessenich6353d552017-06-23 11:11:09 -0600622 } else if (lowerword == "client") {
623 if (argc > 1) {
624 if (strcmp(argv[1], "vulkan100") == 0)
625 setVulkanSpv();
626 else if (strcmp(argv[1], "opengl100") == 0)
627 setOpenGlSpv();
628 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700629 Error("expects vulkan100 or opengl100", lowerword.c_str());
630 } else
631 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600632 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700633 } else if (lowerword == "define-macro" ||
634 lowerword == "d") {
635 if (argc > 1)
636 UserPreamble.addDef(argv[1]);
637 else
638 Error("expects <name[=def]>", argv[0]);
639 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200640 } else if (lowerword == "dump-builtin-symbols") {
641 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600642 } else if (lowerword == "entry-point") {
643 entryPointName = argv[1];
644 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700645 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600646 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600647 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
648 lowerword == "flatten-uniform-array" ||
649 lowerword == "fua") {
650 Options |= EOptionFlattenUniformArrays;
651 } else if (lowerword == "hlsl-offsets") {
652 Options |= EOptionHlslOffsets;
653 } else if (lowerword == "hlsl-iomap" ||
654 lowerword == "hlsl-iomapper" ||
655 lowerword == "hlsl-iomapping") {
656 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800657 } else if (lowerword == "hlsl-enable-16bit-types") {
658 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700659 } else if (lowerword == "hlsl-dx9-compatible") {
660 HlslDX9compatible = true;
Greg Fischerebfca602021-04-21 16:49:53 -0600661 } else if (lowerword == "auto-sampled-textures") {
662 autoSampledTextures = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700663 } else if (lowerword == "invert-y" || // synonyms
664 lowerword == "iy") {
665 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600666 } else if (lowerword == "keep-uncalled" || // synonyms
667 lowerword == "ku") {
668 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600669 } else if (lowerword == "nan-clamp") {
670 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600671 } else if (lowerword == "no-storage-format" || // synonyms
672 lowerword == "nsf") {
673 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600674 } else if (lowerword == "relaxed-errors") {
675 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000676 } else if (lowerword == "reflect-strict-array-suffix") {
677 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000678 } else if (lowerword == "reflect-basic-array-suffix") {
679 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000680 } else if (lowerword == "reflect-intermediate-io") {
681 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000682 } else if (lowerword == "reflect-separate-buffers") {
683 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000684 } else if (lowerword == "reflect-all-block-variables") {
685 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000686 } else if (lowerword == "reflect-unwrap-io-blocks") {
687 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800688 } else if (lowerword == "reflect-all-io-variables") {
689 ReflectOptions |= EShReflectionAllIOVariables;
690 } else if (lowerword == "reflect-shared-std140-ubo") {
691 ReflectOptions |= EShReflectionSharedStd140UBO;
692 } else if (lowerword == "reflect-shared-std140-ssbo") {
693 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600694 } else if (lowerword == "resource-set-bindings" || // synonyms
695 lowerword == "resource-set-binding" ||
696 lowerword == "rsb") {
697 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
greg-lunarg4e064ee2021-03-15 11:26:11 -0600698 } else if (lowerword == "set-block-storage" ||
699 lowerword == "sbs") {
700 ProcessBlockStorage(argc, argv, blockStorageOverrides);
701 } else if (lowerword == "set-atomic-counter-block" ||
702 lowerword == "sacb") {
703 ProcessGlobalBlockSettings(argc, argv, &atomicCounterBlockName, &atomicCounterBlockSet, nullptr);
704 setGlobalBufferBlock = true;
705 } else if (lowerword == "set-default-uniform-block" ||
706 lowerword == "sdub") {
707 ProcessGlobalBlockSettings(argc, argv, &globalUniformName, &globalUniformSet, &globalUniformBinding);
708 setGlobalUniformBlock = true;
steve-lunarg9088be42016-11-01 10:31:42 -0600709 } else if (lowerword == "shift-image-bindings" || // synonyms
710 lowerword == "shift-image-binding" ||
711 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600712 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600713 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600714 lowerword == "shift-sampler-binding" ||
715 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600716 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600717 } else if (lowerword == "shift-uav-bindings" || // synonyms
718 lowerword == "shift-uav-binding" ||
719 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600720 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600721 } else if (lowerword == "shift-texture-bindings" || // synonyms
722 lowerword == "shift-texture-binding" ||
723 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600724 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600725 } else if (lowerword == "shift-ubo-bindings" || // synonyms
726 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600727 lowerword == "shift-cbuffer-bindings" ||
728 lowerword == "shift-cbuffer-binding" ||
729 lowerword == "sub" ||
730 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600731 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700732 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
733 lowerword == "shift-ssbo-binding" ||
734 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600735 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600736 } else if (lowerword == "source-entrypoint" || // synonyms
737 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600738 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700739 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600740 sourceEntryPointName = argv[1];
741 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600742 break;
John Kesseniche2156222018-06-11 18:12:15 -0600743 } else if (lowerword == "spirv-dis") {
744 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600745 } else if (lowerword == "spirv-val") {
746 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200747 } else if (lowerword == "stdin") {
748 Options |= EOptionStdin;
749 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600750 } else if (lowerword == "suppress-warnings") {
751 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600752 } else if (lowerword == "target-env") {
753 if (argc > 1) {
754 if (strcmp(argv[1], "vulkan1.0") == 0) {
755 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600756 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700757 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
758 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600759 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700760 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
761 setVulkanSpv();
762 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600763 } else if (strcmp(argv[1], "opengl") == 0) {
764 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600765 ClientVersion = glslang::EShTargetOpenGL_450;
766 } else if (strcmp(argv[1], "spirv1.0") == 0) {
767 TargetLanguage = glslang::EShTargetSpv;
768 TargetVersion = glslang::EShTargetSpv_1_0;
769 } else if (strcmp(argv[1], "spirv1.1") == 0) {
770 TargetLanguage = glslang::EShTargetSpv;
771 TargetVersion = glslang::EShTargetSpv_1_1;
772 } else if (strcmp(argv[1], "spirv1.2") == 0) {
773 TargetLanguage = glslang::EShTargetSpv;
774 TargetVersion = glslang::EShTargetSpv_1_2;
775 } else if (strcmp(argv[1], "spirv1.3") == 0) {
776 TargetLanguage = glslang::EShTargetSpv;
777 TargetVersion = glslang::EShTargetSpv_1_3;
778 } else if (strcmp(argv[1], "spirv1.4") == 0) {
779 TargetLanguage = glslang::EShTargetSpv;
780 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600781 } else if (strcmp(argv[1], "spirv1.5") == 0) {
782 TargetLanguage = glslang::EShTargetSpv;
783 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600784 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700785 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600786 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600787 }
788 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700789 } else if (lowerword == "undef-macro" ||
790 lowerword == "u") {
791 if (argc > 1)
792 UserPreamble.addUndef(argv[1]);
793 else
794 Error("expects <name>", argv[0]);
795 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800796 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700797 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800798 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600799 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700800 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600801 variableName = argv[1];
802 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800803 break;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200804 } else if (lowerword == "quiet") {
805 beQuiet = true;
Georg Lehmannf0502092021-04-20 00:22:15 +0200806 } else if (lowerword == "depfile") {
807 if (argc <= 1)
808 Error("no <depfile-name> provided", lowerword.c_str());
809 depencyFileName = argv[1];
810 bumpArg();
John Kessenichc6c80a62018-03-05 22:23:17 -0700811 } else if (lowerword == "version") {
812 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800813 } else if (lowerword == "help") {
814 usage();
815 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600816 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700817 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600818 }
819 }
820 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600821 case 'C':
822 Options |= EOptionCascadingErrors;
823 break;
824 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600825 if (argv[0][2] == 0)
826 Options |= EOptionReadHlsl;
827 else
John Kessenich6f988922020-01-07 07:03:11 -0700828 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600829 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100830 case 'u':
831 uniformLocationOverrides.push_back(getUniformOverride());
832 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600833 case 'E':
834 Options |= EOptionOutputPreprocessed;
835 break;
836 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600837 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600838 setOpenGlSpv();
839 if (argv[0][2] != 0)
840 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600841 if (ClientInputSemanticsVersion != 100)
842 Error("unknown client version for -G, should be 100");
John Kessenich6263fb12017-06-14 15:52:44 -0600843 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000844 case 'H':
845 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600846 if ((Options & EOptionSpv) == 0) {
847 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600848 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600849 }
850 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600851 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600852 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600853 break;
GregFcd1f1692017-09-21 18:40:22 -0600854 case 'O':
855 if (argv[0][2] == 'd')
856 Options |= EOptionOptimizeDisable;
857 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600858#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600859 Options |= EOptionOptimizeSize;
860#else
861 Error("-Os not available; optimizer not linked");
862#endif
863 else
864 Error("unknown -O option");
865 break;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600866 case 'R':
867 VulkanRulesRelaxed = true;
868 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400869 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600870 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400871 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600872 shaderStageName = argv[1];
873 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400874 break;
John Kessenicha9313662017-06-15 10:40:49 -0600875 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700876 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600877 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600878 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600879 setVulkanSpv();
880 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500881 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600882 if (ClientInputSemanticsVersion != 100)
883 Error("unknown client version for -V, should be 100");
John Kessenichc555ddd2015-06-17 02:38:44 +0000884 break;
John Kessenich05a70632013-09-17 19:26:08 +0000885 case 'c':
886 Options |= EOptionDumpConfig;
887 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000888 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700889 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
890 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
891 Options |= EOptionDumpBareVersion;
892 else
893 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000894 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700895 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700896 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600897 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600898 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600899 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700900 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700901 case 'f':
902 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
903 targetHlslFunctionality1 = true;
904 else
905 Error("-f: expected hlsl_functionality1");
906 break;
John Kessenich121853f2017-05-31 17:11:16 -0600907 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400908 // Override previous -g or -g0 argument
909 stripDebugInfo = false;
910 Options &= ~EOptionDebug;
911 if (argv[0][2] == '0')
912 stripDebugInfo = true;
913 else
914 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600915 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600916 case 'h':
917 usage();
918 break;
John Kessenich05a70632013-09-17 19:26:08 +0000919 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000920 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000921 break;
922 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000923 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000924 break;
925 case 'm':
926 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000927 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600928 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600929 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600930 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600931 binaryFileName = argv[1];
932 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600933 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000934 case 'q':
935 Options |= EOptionDumpReflection;
936 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000937 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000938 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000939 break;
940 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000941 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000942 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000943 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200944 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000945 break;
John Kessenich319de232013-12-04 04:43:40 +0000946 case 'v':
947 Options |= EOptionDumpVersions;
948 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000949 case 'w':
950 Options |= EOptionSuppressWarnings;
951 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500952 case 'x':
953 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500954 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000955 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700956 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600957 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000958 }
John Kessenich38f3b892013-09-06 19:52:57 +0000959 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000960 std::string name(argv[0]);
961 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200962 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000963 }
John Kessenich38f3b892013-09-06 19:52:57 +0000964 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000965 }
966
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200967 // Make sure that -S is always specified if --stdin is specified
968 if ((Options & EOptionStdin) && shaderStageName == nullptr)
969 Error("must provide -S when --stdin is given");
970
John Kessenich68d78fd2015-07-12 19:28:10 -0600971 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600972 // Or things that require linking
973 if (Options & EOptionOutputPreprocessed) {
974 if (Options & EOptionLinkProgram)
975 Error("can't use -E when linking is selected");
976 if (Options & EOptionDumpReflection)
977 Error("reflection requires linking, which can't be used when -E when is selected");
978 }
979
980 // reflection requires linking
981 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
982 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000983
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500984 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600985 if (binaryFileName && (Options & EOptionSpv) == 0)
986 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600987
988 if ((Options & EOptionFlattenUniformArrays) != 0 &&
989 (Options & EOptionReadHlsl) == 0)
990 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600991
Jeremy Hayesb73afa82021-01-05 15:54:41 -0700992 if ((Options & EOptionReadHlsl) && (Client == glslang::EShClientOpenGL)) {
993 Error("Using HLSL input under OpenGL semantics is not currently supported.");
994 }
995
John Kessenich8717a5d2018-10-26 10:12:32 -0600996 // rationalize client and target language
997 if (TargetLanguage == glslang::EShTargetNone) {
998 switch (ClientVersion) {
999 case glslang::EShTargetVulkan_1_0:
1000 TargetLanguage = glslang::EShTargetSpv;
1001 TargetVersion = glslang::EShTargetSpv_1_0;
1002 break;
1003 case glslang::EShTargetVulkan_1_1:
1004 TargetLanguage = glslang::EShTargetSpv;
1005 TargetVersion = glslang::EShTargetSpv_1_3;
1006 break;
John Kessenich273d3a52020-01-15 00:10:41 -07001007 case glslang::EShTargetVulkan_1_2:
1008 TargetLanguage = glslang::EShTargetSpv;
1009 TargetVersion = glslang::EShTargetSpv_1_5;
1010 break;
John Kessenich8717a5d2018-10-26 10:12:32 -06001011 case glslang::EShTargetOpenGL_450:
1012 TargetLanguage = glslang::EShTargetSpv;
1013 TargetVersion = glslang::EShTargetSpv_1_0;
1014 break;
1015 default:
1016 break;
1017 }
1018 }
1019 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
1020 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +00001021}
1022
John Kessenich68d78fd2015-07-12 19:28:10 -06001023//
1024// Translate the meaningful subset of command-line options to parser-behavior options.
1025//
John Kessenichb0a7eb52013-11-07 17:44:20 +00001026void SetMessageOptions(EShMessages& messages)
1027{
1028 if (Options & EOptionRelaxedErrors)
1029 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
1030 if (Options & EOptionIntermediate)
1031 messages = (EShMessages)(messages | EShMsgAST);
1032 if (Options & EOptionSuppressWarnings)
1033 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -06001034 if (Options & EOptionSpv)
1035 messages = (EShMessages)(messages | EShMsgSpvRules);
1036 if (Options & EOptionVulkanRules)
1037 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001038 if (Options & EOptionOutputPreprocessed)
1039 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -07001040 if (Options & EOptionReadHlsl)
1041 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -06001042 if (Options & EOptionCascadingErrors)
1043 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -07001044 if (Options & EOptionKeepUncalled)
1045 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -06001046 if (Options & EOptionHlslOffsets)
1047 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -06001048 if (Options & EOptionDebug)
1049 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +08001050 if (HlslEnable16BitTypes)
1051 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -06001052 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
1053 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -07001054 if (HlslDX9compatible)
1055 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +02001056 if (DumpBuiltinSymbols)
1057 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +00001058}
1059
John Kessenich68d78fd2015-07-12 19:28:10 -06001060//
John Kessenich69f4b512013-09-04 21:19:27 +00001061// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +00001062//
Juan Lopeza558b262017-04-02 23:04:00 +02001063void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001064{
John Kessenich7f0bcfd2018-04-05 19:00:01 -06001065 if (Options & EOptionDebug)
1066 Error("cannot generate debug information unless linking to generate code");
1067
John Kessenich38f3b892013-09-06 19:52:57 +00001068 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001069 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -06001070 if (worklist.remove(workItem)) {
1071 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
1072 if (compiler == nullptr)
1073 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001074
John Kesseniche7f9cae2018-07-12 15:11:07 -06001075 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001076
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001077 if (! (Options & EOptionSuppressInfolog))
1078 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001079
John Kesseniche7f9cae2018-07-12 15:11:07 -06001080 ShDestruct(compiler);
1081 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001082 } else {
1083 while (worklist.remove(workItem)) {
1084 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
1085 if (compiler == 0)
1086 return;
1087
1088 CompileFile(workItem->name.c_str(), compiler);
1089
1090 if (! (Options & EOptionSuppressInfolog))
1091 workItem->results = ShGetInfoLog(compiler);
1092
1093 ShDestruct(compiler);
1094 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001095 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001096}
1097
John Kessenich6626cad2015-06-19 05:14:19 +00001098// Outputs the given string, but only if it is non-null and non-empty.
1099// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001100void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +00001101{
1102 if (str && str[0]) {
1103 puts(str);
1104 }
1105}
1106
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001107// Outputs the given string to stderr, but only if it is non-null and non-empty.
1108// This prevents erroneous newlines from appearing.
1109void StderrIfNonEmpty(const char* str)
1110{
John Kessenich04acb1b2017-06-14 17:36:50 -06001111 if (str && str[0])
1112 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001113}
1114
John Kessenichc57b2a92016-01-16 15:30:03 -07001115// Simple bundling of what makes a compilation unit for ease in passing around,
1116// and separation of handling file IO versus API (programmatic) compilation.
1117struct ShaderCompUnit {
1118 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001119 static const int maxCount = 1;
1120 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -06001121 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -06001122 std::string fileName[maxCount]; // hold's the memory, but...
1123 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -04001124
John Kessenich04acb1b2017-06-14 17:36:50 -06001125 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -04001126
John Kessenich04acb1b2017-06-14 17:36:50 -06001127 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -04001128 {
1129 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001130 count = rhs.count;
1131 for (int i = 0; i < count; ++i) {
1132 fileName[i] = rhs.fileName[i];
1133 text[i] = rhs.text[i];
1134 fileNameList[i] = rhs.fileName[i].c_str();
1135 }
dankbakerafe6e9c2016-08-21 12:29:08 -04001136 }
1137
John Kessenicha9313662017-06-15 10:40:49 -06001138 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -06001139 {
1140 assert(count < maxCount);
1141 fileName[count] = ifileName;
1142 text[count] = itext;
1143 fileNameList[count] = fileName[count].c_str();
1144 ++count;
1145 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001146};
1147
Georg Lehmannf0502092021-04-20 00:22:15 +02001148// Writes a depfile similar to gcc -MMD foo.c
1149bool writeDepFile(std::string depfile, std::vector<std::string>& binaryFiles, const std::vector<std::string>& sources)
1150{
1151 std::ofstream file(depfile);
1152 if (file.fail())
1153 return false;
1154
1155 for (auto it = binaryFiles.begin(); it != binaryFiles.end(); it++) {
1156 file << *it << ":";
1157 for (auto it = sources.begin(); it != sources.end(); it++) {
1158 file << " " << *it;
1159 }
1160 file << std::endl;
1161 }
1162 return true;
1163}
1164
John Kessenich69f4b512013-09-04 21:19:27 +00001165//
John Kessenichc57b2a92016-01-16 15:30:03 -07001166// For linking mode: Will independently parse each compilation unit, but then put them
1167// in the same program and link them together, making at most one linked module per
1168// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +00001169//
1170// Uses the new C++ interface instead of the old handle-based interface.
1171//
John Kessenichc57b2a92016-01-16 15:30:03 -07001172
1173void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +00001174{
1175 // keep track of what to free
1176 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001177
John Kessenich69f4b512013-09-04 21:19:27 +00001178 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001179 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001180
Georg Lehmannf0502092021-04-20 00:22:15 +02001181 DirStackFileIncluder includer;
1182 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1183 includer.pushExternalLocalDirectory(dir); });
1184
1185 std::vector<std::string> sources;
1186
John Kessenich69f4b512013-09-04 21:19:27 +00001187 //
1188 // Per-shader processing...
1189 //
1190
John Kessenich5b0f13a2013-11-01 03:08:40 +00001191 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001192 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1193 const auto &compUnit = *it;
Georg Lehmannf0502092021-04-20 00:22:15 +02001194 for (int i = 0; i < compUnit.count; i++) {
1195 sources.push_back(compUnit.fileNameList[i]);
1196 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001197 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001198 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001199 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001200 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001201 if (sourceEntryPointName) {
1202 if (entryPointName == nullptr)
1203 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1204 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001205 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001206 }
John Kessenicha9313662017-06-15 10:40:49 -06001207 if (UserPreamble.isSet())
1208 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -06001209 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001210
John Kessenich155d3512019-08-08 23:29:20 -06001211#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001212 // Set IO mapper binding shift values
1213 for (int r = 0; r < glslang::EResCount; ++r) {
1214 const glslang::TResourceType res = glslang::TResourceType(r);
1215
1216 // Set base bindings
1217 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001218
LoopDawg08a14422017-10-17 19:27:14 -06001219 // Set bindings for particular resource sets
1220 // TODO: use a range based for loop here, when available in all environments.
1221 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1222 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001223 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001224 }
steve-lunargcce8d482016-10-14 18:36:42 -06001225 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001226 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001227
Greg Fischerebfca602021-04-21 16:49:53 -06001228 if (autoSampledTextures)
Robin Quint219b7b92021-04-20 07:58:36 +02001229 shader->setTextureSamplerTransformMode(EShTexSampTransUpgradeTextureRemoveSampler);
1230
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001231 if (Options & EOptionAutoMapBindings)
1232 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001233
John Kessenich71facdf2017-05-17 18:28:19 -06001234 if (Options & EOptionAutoMapLocations)
1235 shader->setAutoMapLocations(true);
1236
Neil Roberts16f53472018-03-20 17:30:53 +01001237 for (auto& uniOverride : uniformLocationOverrides) {
1238 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1239 uniOverride.second);
1240 }
1241
Neil Robertsb0f3d792018-03-20 17:41:05 +01001242 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001243#endif
1244
greg-lunarg4e064ee2021-03-15 11:26:11 -06001245 if (VulkanRulesRelaxed) {
1246 for (auto& storageOverride : blockStorageOverrides) {
1247 shader->addBlockStorageOverride(storageOverride.first.c_str(),
1248 storageOverride.second);
1249 }
1250
1251 if (setGlobalBufferBlock) {
1252 shader->setAtomicCounterBlockName(atomicCounterBlockName.c_str());
1253 shader->setAtomicCounterBlockSet(atomicCounterBlockSet);
1254 }
1255
1256 if (setGlobalUniformBlock) {
1257 shader->setGlobalUniformBlockName(globalUniformName.c_str());
1258 shader->setGlobalUniformSet(globalUniformSet);
1259 shader->setGlobalUniformBinding(globalUniformBinding);
1260 }
1261 }
1262
John Kessenich155d3512019-08-08 23:29:20 -06001263 shader->setNanMinMaxClamp(NaNClamp);
1264
1265#ifdef ENABLE_HLSL
1266 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1267 if (Options & EOptionHlslIoMapping)
1268 shader->setHlslIoMapping(true);
1269#endif
1270
1271 if (Options & EOptionInvertY)
1272 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001273
John Kessenich4be4aeb2017-06-23 10:50:22 -06001274 // Set up the environment, some subsettings take precedence over earlier
1275 // ways of setting things.
1276 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001277 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1278 : glslang::EShSourceGlsl,
1279 compUnit.stage, Client, ClientInputSemanticsVersion);
1280 shader->setEnvClient(Client, ClientVersion);
1281 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001282#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001283 if (targetHlslFunctionality1)
1284 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001285#endif
greg-lunarg4e064ee2021-03-15 11:26:11 -06001286 if (VulkanRulesRelaxed)
1287 shader->setEnvInputVulkanRulesRelaxed();
John Kessenich4be4aeb2017-06-23 10:50:22 -06001288 }
1289
John Kessenich69f4b512013-09-04 21:19:27 +00001290 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001291
John Kessenich4be4aeb2017-06-23 10:50:22 -06001292 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001293
John Kessenichdeec1932019-08-13 08:00:30 -06001294#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001295 if (Options & EOptionOutputPreprocessed) {
1296 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001297 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001298 PutsIfNonEmpty(str.c_str());
1299 } else {
1300 CompileFailed = true;
1301 }
1302 StderrIfNonEmpty(shader->getInfoLog());
1303 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001304 continue;
1305 }
John Kessenichdeec1932019-08-13 08:00:30 -06001306#endif
John Kessenich086febc2018-10-25 12:43:02 -06001307
John Kessenich3494b4d2017-05-22 15:00:42 -06001308 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001309 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001310
John Kessenich69f4b512013-09-04 21:19:27 +00001311 program.addShader(shader);
1312
John Kessenichc57b2a92016-01-16 15:30:03 -07001313 if (! (Options & EOptionSuppressInfolog) &&
1314 ! (Options & EOptionMemoryLeakMode)) {
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001315 if (!beQuiet)
1316 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001317 PutsIfNonEmpty(shader->getInfoLog());
1318 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001319 }
John Kessenich69f4b512013-09-04 21:19:27 +00001320 }
1321
1322 //
1323 // Program-level processing...
1324 //
1325
John Kessenich4d65ee32016-03-12 18:17:47 -07001326 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001327 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001328 LinkFailed = true;
1329
John Kessenichb6d3ee52019-08-06 02:20:45 -06001330#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001331 // Map IO
1332 if (Options & EOptionSpv) {
1333 if (!program.mapIO())
1334 LinkFailed = true;
1335 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001336#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001337
John Kessenich4d65ee32016-03-12 18:17:47 -07001338 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001339 if (! (Options & EOptionSuppressInfolog) &&
1340 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001341 PutsIfNonEmpty(program.getInfoLog());
1342 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001343 }
1344
John Kessenichb6d3ee52019-08-06 02:20:45 -06001345#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001346 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001347 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001348 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001349 program.dumpReflection();
1350 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001351#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001352
Georg Lehmannf0502092021-04-20 00:22:15 +02001353 std::vector<std::string> outputFiles;
1354
John Kessenich4d65ee32016-03-12 18:17:47 -07001355 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001356 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001357 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001358 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001359 else {
1360 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001361 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001362 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001363 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001364 glslang::SpvOptions spvOptions;
1365 if (Options & EOptionDebug)
1366 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001367 else if (stripDebugInfo)
1368 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001369 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1370 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001371 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001372 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001373 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001374
1375 // Dump the spv to a file or stdout, etc., but only if not doing
1376 // memory/perf testing, as it's not internal to programmatic use.
1377 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001378 printf("%s", logger.getAllMessages().c_str());
1379 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001380 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001381 } else {
1382 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1383 }
Georg Lehmannf0502092021-04-20 00:22:15 +02001384
1385 outputFiles.push_back(GetBinaryName((EShLanguage)stage));
John Kessenich23d27752019-07-28 02:12:10 -06001386#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001387 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001388 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001389#endif
John Kessenichacba7722015-03-04 03:48:38 +00001390 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001391 }
John Kessenich92f90382014-07-28 04:21:04 +00001392 }
1393 }
1394 }
1395
Georg Lehmannf0502092021-04-20 00:22:15 +02001396 if (depencyFileName && !(CompileFailed || LinkFailed)) {
1397 std::set<std::string> includedFiles = includer.getIncludedFiles();
1398 sources.insert(sources.end(), includedFiles.begin(), includedFiles.end());
1399
1400 writeDepFile(depencyFileName, outputFiles, sources);
1401 }
1402
John Kessenich5b0f13a2013-11-01 03:08:40 +00001403 // Free everything up, program has to go before the shaders
1404 // because it might have merged stuff from the shaders, and
1405 // the stuff from the shaders has to have its destructors called
1406 // before the pools holding the memory in the shaders is freed.
1407 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001408 while (shaders.size() > 0) {
1409 delete shaders.back();
1410 shaders.pop_back();
1411 }
John Kessenich69f4b512013-09-04 21:19:27 +00001412}
1413
John Kessenichc57b2a92016-01-16 15:30:03 -07001414//
1415// Do file IO part of compile and link, handing off the pure
1416// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1417// be put in a loop for testing memory footprint and performance.
1418//
1419// This is just for linking mode: meaning all the shaders will be put into the
1420// the same program linked together.
1421//
1422// This means there are a limited number of work items (not multi-threading mode)
1423// and that the point is testing at the linking level. Hence, to enable
1424// performance and memory testing, the actual compile/link can be put in
1425// a loop, independent of processing the work items and file IO.
1426//
Juan Lopeza558b262017-04-02 23:04:00 +02001427void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001428{
1429 std::vector<ShaderCompUnit> compUnits;
1430
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001431 // If this is using stdin, we can't really detect multiple different file
1432 // units by input type. We need to assume that we're just being given one
1433 // file of a certain type.
1434 if ((Options & EOptionStdin) != 0) {
1435 ShaderCompUnit compUnit(FindLanguage("stdin"));
1436 std::istreambuf_iterator<char> begin(std::cin), end;
1437 std::string tempString(begin, end);
1438 char* fileText = strdup(tempString.c_str());
1439 std::string fileName = "stdin";
1440 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001441 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001442 } else {
1443 // Transfer all the work items from to a simple list of
1444 // of compilation units. (We don't care about the thread
1445 // work-item distribution properties in this path, which
1446 // is okay due to the limited number of shaders, know since
1447 // they are all getting linked together.)
1448 glslang::TWorkItem* workItem;
1449 while (Worklist.remove(workItem)) {
1450 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1451 char* fileText = ReadFileData(workItem->name.c_str());
1452 if (fileText == nullptr)
1453 usage();
1454 compUnit.addString(workItem->name, fileText);
1455 compUnits.push_back(compUnit);
1456 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001457 }
1458
1459 // Actual call to programmatic processing of compile and link,
1460 // in a loop for testing memory and performance. This part contains
1461 // all the perf/memory that a programmatic consumer will care about.
1462 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1463 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1464 CompileAndLinkShaderUnits(compUnits);
1465
1466 if (Options & EOptionMemoryLeakMode)
1467 glslang::OS_DumpMemoryCounters();
1468 }
1469
John Kessenicha9313662017-06-15 10:40:49 -06001470 // free memory from ReadFileData, which got stored in a const char*
1471 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001472 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001473 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001474}
1475
John Kessenich94f28eb2017-11-13 01:32:06 -07001476int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001477{
Juan Lopeza558b262017-04-02 23:04:00 +02001478 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001479 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001480 assert(item);
1481 workList.add(item.get());
1482 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001483
John Kessenich23d27752019-07-28 02:12:10 -06001484#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001485 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001486 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001487 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001488 return ESuccess;
1489 }
John Kessenich23d27752019-07-28 02:12:10 -06001490#endif
John Kessenich05a70632013-09-17 19:26:08 +00001491
John Kessenichc6c80a62018-03-05 22:23:17 -07001492 if (Options & EOptionDumpBareVersion) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001493 printf("%d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1494 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenichc6c80a62018-03-05 22:23:17 -07001495 if (workList.empty())
1496 return ESuccess;
1497 } else if (Options & EOptionDumpVersions) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001498 printf("Glslang Version: %d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR,
1499 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenich319de232013-12-04 04:43:40 +00001500 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1501 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001502 std::string spirvVersion;
1503 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001504 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001505 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001506 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001507 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001508 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1509 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001510 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001511 return ESuccess;
1512 }
1513
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001514 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001515 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001516 }
1517
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001518 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001519 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1520 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001521 }
1522
John Kessenich05a70632013-09-17 19:26:08 +00001523 ProcessConfigFile();
1524
John Kessenich086febc2018-10-25 12:43:02 -06001525 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001526 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001527
John Kessenich69f4b512013-09-04 21:19:27 +00001528 //
1529 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001530 // 1) linking all arguments together, single-threaded, new C++ interface
1531 // 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 +00001532 //
John Kessenich086febc2018-10-25 12:43:02 -06001533 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001534 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001535 glslang::InitializeProcess(); // also test reference counting of users
1536 glslang::InitializeProcess(); // also test reference counting of users
1537 glslang::FinalizeProcess(); // also test reference counting of users
1538 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001539 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001540 glslang::FinalizeProcess();
1541 } else {
1542 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001543 ShInitialize(); // also test reference counting of users
1544 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001545
Juan Lopeza558b262017-04-02 23:04:00 +02001546 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001547
John Kesseniche2c15b42017-11-16 22:48:41 -07001548 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001549 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001550 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001551 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001552 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001553 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001554 return EFailThreadCreate;
1555 }
John Kessenicha0af4732012-12-12 21:15:54 +00001556 }
Juan Lopeza558b262017-04-02 23:04:00 +02001557
1558 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001559 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001560 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001561
1562 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001563 for (size_t w = 0; w < WorkItems.size(); ++w) {
1564 if (WorkItems[w]) {
1565 if (printShaderNames || WorkItems[w]->results.size() > 0)
1566 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1567 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001568 }
1569 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001570
1571 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001572 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001573
John Kessenichc999ba22013-11-07 23:33:24 +00001574 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001575 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001576 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001577 return EFailLink;
1578
1579 return 0;
1580}
1581
John Kessenich94f28eb2017-11-13 01:32:06 -07001582int C_DECL main(int argc, char* argv[])
1583{
1584 ProcessArguments(WorkItems, argc, argv);
1585
1586 int ret = 0;
1587
1588 // Loop over the entire init/finalize cycle to watch memory changes
1589 const int iterations = 1;
1590 if (iterations > 1)
1591 glslang::OS_DumpMemoryCounters();
1592 for (int i = 0; i < iterations; ++i) {
1593 ret = singleMain();
1594 if (iterations > 1)
1595 glslang::OS_DumpMemoryCounters();
1596 }
1597
1598 return ret;
1599}
1600
John Kessenicha0af4732012-12-12 21:15:54 +00001601//
1602// Deduce the language from the filename. Files must end in one of the
1603// following extensions:
1604//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001605// .vert = vertex
1606// .tesc = tessellation control
1607// .tese = tessellation evaluation
1608// .geom = geometry
1609// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001610// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001611// .rgen = ray generation
1612// .rint = ray intersection
1613// .rahit = ray any hit
1614// .rchit = ray closest hit
1615// .rmiss = ray miss
1616// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001617// .mesh = mesh
1618// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001619// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1620// where <stage> is one of the stages listed above.
1621//
1622EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001623{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001624 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001625 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001626 stageName = shaderStageName;
1627 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001628 // Note: "first" extension means "first from the end", i.e.
1629 // if the file is named foo.vert.glsl, then "glsl" is first,
1630 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001631 size_t firstExtStart = name.find_last_of(".");
1632 bool hasFirstExt = firstExtStart != std::string::npos;
1633 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1634 bool hasSecondExt = secondExtStart != std::string::npos;
1635 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1636 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001637 if (usesUnifiedExt && firstExt == "hlsl")
1638 Options |= EOptionReadHlsl;
1639 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001640 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001641 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001642 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001643 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001644 usage();
1645 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001646 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001647 } else
1648 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001649
John Kessenichfccbb8b2018-04-17 17:24:03 -06001650 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001651 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001652 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001653 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001654 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001655 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001656 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001657 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001658 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001659 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001660 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001661 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001662 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001663 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001664 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001665 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001666 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001667 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001668 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001669 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001670 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001671 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001672 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001673 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001674 else if (stageName == "mesh")
1675 return EShLangMeshNV;
1676 else if (stageName == "task")
1677 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001678
1679 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001680 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001681}
1682
John Kessenicha0af4732012-12-12 21:15:54 +00001683//
John Kessenichecba76f2017-01-06 00:34:48 -07001684// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001685// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001686//
John Kessenich51cdd902014-02-18 23:37:57 +00001687void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001688{
John Kessenichca3457f2015-05-18 01:59:45 +00001689 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001690 char* shaderString;
1691 if ((Options & EOptionStdin) != 0) {
1692 std::istreambuf_iterator<char> begin(std::cin), end;
1693 std::string tempString(begin, end);
1694 shaderString = strdup(tempString.c_str());
1695 } else {
1696 shaderString = ReadFileData(fileName);
1697 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001698
1699 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001700 int* lengths = new int[1];
1701 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001702
John Kessenich52ac67e2013-05-05 23:46:22 +00001703 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001704 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001705
John Kessenicha9313662017-06-15 10:40:49 -06001706 if (UserPreamble.isSet())
1707 Error("-D and -U options require -l (linking)\n");
1708
John Kessenich94a81fb2013-08-31 02:41:30 +00001709 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1710 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001711 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001712 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001713 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001714 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001715 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001716 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1717 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001718 }
John Kessenicha0af4732012-12-12 21:15:54 +00001719
John Kessenich94a81fb2013-08-31 02:41:30 +00001720 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001721 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001722 }
John Kessenicha0af4732012-12-12 21:15:54 +00001723
John Kessenich41cf6b52013-06-25 18:10:05 +00001724 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001725 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001726
John Kessenichc999ba22013-11-07 23:33:24 +00001727 if (ret == 0)
1728 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001729}
1730
John Kessenicha0af4732012-12-12 21:15:54 +00001731//
1732// print usage to stdout
1733//
1734void usage()
1735{
John Kessenich319de232013-12-04 04:43:40 +00001736 printf("Usage: glslangValidator [option]... [file]...\n"
1737 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001738 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1739 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001740 " (see -c option below for generating a template)\n"
1741 " .vert for a vertex shader\n"
1742 " .tesc for a tessellation control shader\n"
1743 " .tese for a tessellation evaluation shader\n"
1744 " .geom for a geometry shader\n"
1745 " .frag for a fragment shader\n"
1746 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001747 " .mesh for a mesh shader\n"
1748 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001749 " .rgen for a ray generation shader\n"
1750 " .rint for a ray intersection shader\n"
1751 " .rahit for a ray any hit shader\n"
1752 " .rchit for a ray closest hit shader\n"
1753 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001754 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001755 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1756 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001757 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001758 "Options:\n"
1759 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001760 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001761 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1762 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001763 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001764 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001765 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001766 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001767 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001768 " which will appear in #define GL_SPIRV ver;\n"
1769 " '--client opengl100' is the same as -G100;\n"
Jeremy Hayesb73afa82021-01-05 15:54:41 -07001770 " a '--target-env' for OpenGL will also imply '-G';\n"
1771 " currently only supports GLSL\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001772 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001773 " -I<dir> add dir to the include search path; includer's directory\n"
1774 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001775 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1776 " -Os optimizes SPIR-V to minimize size\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001777 " -R use relaxed verification rules for generating Vulkan SPIR-V,\n"
1778 " allowing the use of default uniforms, atomic_uints, and\n"
1779 " gl_VertexID and gl_InstanceID keywords.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001780 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001781 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001782 " -U<name> | --undef-macro <name> | --U <name>\n"
1783 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001784 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001785 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001786 " 'ver', when present, is the version of the input semantics,\n"
1787 " which will appear in #define VULKAN ver\n"
1788 " '--client vulkan100' is the same as -V100\n"
1789 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001790 " -c configuration dump;\n"
1791 " creates the default configuration file (redirect to a .conf file)\n"
1792 " -d default to desktop (#version 110) when there is no shader #version\n"
1793 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001794 " -e <name> | --entry-point <name>\n"
1795 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001796 " -f{hlsl_functionality1}\n"
1797 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001798 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001799 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001800 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001801 " -h print this usage message\n"
1802 " -i intermediate tree (glslang AST) is printed out\n"
1803 " -l link all input files together to form a single module\n"
1804 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001805 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001806 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001807 " -r | --relaxed-errors"
1808 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001809 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001810 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001811 " -v | --version\n"
1812 " print version strings\n"
1813 " -w | --suppress-warnings\n"
1814 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001815 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001816 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001817 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001818 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1819 " without explicit bindings\n"
1820 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1821 " 'location' (fragile, not cross stage)\n"
Robin Quinte50b0a82021-04-22 13:13:38 +02001822 " --auto-sampled-textures Removes sampler variables and converts\n"
Robin Quint4bf373e2021-04-27 16:06:06 +02001823 " existing textures to sampled textures\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001824 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Georg Lehmannf0502092021-04-20 00:22:15 +02001825 " --depfile <file> writes depfile for build systems\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001826 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001827 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1828 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1829 " scalars\n"
1830 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1831 " works independently of source language\n"
1832 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1833 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001834 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001835 " texture/sampler combo like DirectX9 would,\n"
1836 " and recognizes DirectX9-specific semantics\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001837 " --invert-y | --iy invert position.Y output in vertex shader\n"
1838 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001839 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001840 " --no-storage-format | --nsf use Unknown image format\n"
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001841 " --quiet do not print anything to stdout, unless\n"
1842 " requested by another option\n"
baldurk4513df92019-02-08 10:48:48 +00001843 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1844 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001845 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001846 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1847 " shaders rather than just vertex/fragment\n"
1848 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1849 " separately to uniforms\n"
1850 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1851 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001852 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1853 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001854 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001855 " set descriptor set and binding for\n"
1856 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001857 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001858 " set descriptor set for all resources\n"
1859 " --rsb synonym for --resource-set-binding\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001860 " --set-block-backing name {uniform|buffer|push_constant}\n"
1861 " changes the backing type of a uniform, buffer,\n"
1862 " or push_constant block declared in\n"
1863 " in the program, when using -R option.\n"
1864 " This can be used to change the backing\n"
1865 " for existing blocks as well as implicit ones\n"
1866 " such as 'gl_DefaultUniformBlock'.\n"
1867 " --sbs synonym for set-block-storage\n"
1868 " --set-atomic-counter-block name set\n"
1869 " set name, and descriptor set for\n"
1870 " atomic counter blocks, with -R opt\n"
1871 " --sacb synonym for set-atomic-counter-block\n"
1872 " --set-default-uniform-block name set binding\n"
1873 " set name, descriptor set, and binding for\n"
1874 " global default-uniform-block, with -R opt\n"
1875 " --sdub synonym for set-default-uniform-block\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001876 " --shift-image-binding [stage] num\n"
1877 " base binding number for images (uav)\n"
1878 " --shift-image-binding [stage] [num set]...\n"
1879 " per-descriptor-set shift values\n"
1880 " --sib synonym for --shift-image-binding\n"
1881 " --shift-sampler-binding [stage] num\n"
1882 " base binding number for samplers\n"
1883 " --shift-sampler-binding [stage] [num set]...\n"
1884 " per-descriptor-set shift values\n"
1885 " --ssb synonym for --shift-sampler-binding\n"
1886 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1887 " --shift-ssbo-binding [stage] [num set]...\n"
1888 " per-descriptor-set shift values\n"
1889 " --sbb synonym for --shift-ssbo-binding\n"
1890 " --shift-texture-binding [stage] num\n"
1891 " base binding number for textures\n"
1892 " --shift-texture-binding [stage] [num set]...\n"
1893 " per-descriptor-set shift values\n"
1894 " --stb synonym for --shift-texture-binding\n"
1895 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1896 " --shift-uav-binding [stage] [num set]...\n"
1897 " per-descriptor-set shift values\n"
1898 " --suavb synonym for --shift-uav-binding\n"
1899 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1900 " --shift-UBO-binding [stage] [num set]...\n"
1901 " per-descriptor-set shift values\n"
1902 " --sub synonym for --shift-UBO-binding\n"
1903 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1904 " --spirv-dis output standard-form disassembly; works only\n"
1905 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001906 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001907 " --source-entrypoint <name> the given shader source function is\n"
1908 " renamed to be the <name> given in -e\n"
1909 " --sep synonym for --source-entrypoint\n"
1910 " --stdin read from stdin instead of from a file;\n"
1911 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001912 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001913 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001914 " Set the execution environment that the\n"
1915 " generated code will be executed in.\n"
1916 " Defaults to:\n"
1917 " * vulkan1.0 under --client vulkan<ver>\n"
1918 " * opengl under --client opengl<ver>\n"
1919 " * spirv1.0 under --target-env vulkan1.0\n"
1920 " * spirv1.3 under --target-env vulkan1.1\n"
1921 " * spirv1.5 under --target-env vulkan1.2\n"
1922 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001923 " --variable-name <name>\n"
1924 " --vn <name> creates a C header file that contains a\n"
1925 " uint32_t array named <name>\n"
1926 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001927 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001928
1929 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001930}
1931
John Kessenich3ce4e592014-10-06 19:57:34 +00001932#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001933
1934#include <errno.h>
1935
1936int fopen_s(
1937 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001938 const char* filename,
1939 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001940)
1941{
1942 if (!pFile || !filename || !mode) {
1943 return EINVAL;
1944 }
1945
1946 FILE* f = fopen(filename, mode);
1947 if (! f) {
1948 if (errno != 0) {
1949 return errno;
1950 } else {
1951 return ENOENT;
1952 }
1953 }
1954 *pFile = f;
1955
1956 return 0;
1957}
1958
1959#endif
1960
John Kessenicha0af4732012-12-12 21:15:54 +00001961//
1962// Malloc a string of sufficient size and read a string into it.
1963//
John Kessenich04acb1b2017-06-14 17:36:50 -06001964char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001965{
John Kessenichb3297152015-07-11 18:01:03 -06001966 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001967 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001968 if (errorCode || in == nullptr)
1969 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001970
John Kessenich04acb1b2017-06-14 17:36:50 -06001971 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001972 while (fgetc(in) != EOF)
1973 count++;
1974
John Kessenichd6c72a42014-08-18 19:42:35 +00001975 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001976
John Kessenich04acb1b2017-06-14 17:36:50 -06001977 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1978 if ((int)fread(return_data, 1, count, in) != count) {
1979 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001980 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001981 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001982
John Kessenich04acb1b2017-06-14 17:36:50 -06001983 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001984 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001985
John Kessenicha0af4732012-12-12 21:15:54 +00001986 return return_data;
1987}
1988
John Kessenich04acb1b2017-06-14 17:36:50 -06001989void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001990{
John Kessenichb3297152015-07-11 18:01:03 -06001991 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001992}
1993
John Kessenich54d8cda2013-02-11 22:36:01 +00001994void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001995{
John Kessenichfae38ee2015-06-10 23:23:12 +00001996 if (num >= 0 )
1997 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1998 else
1999 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00002000}