blob: 4deaf4d9e48b6cfff526e3cd286e25d1eaf79205 [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.
amhagan035a3bb2021-09-01 11:33:21 -04005// Modifications Copyright(C) 2021 Advanced Micro Devices, Inc.All rights reserved.
John Kessenichbd0747d2013-02-17 06:01:50 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00008//
John Kessenich927608b2017-01-06 12:34:14 -07009// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions
11// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000012//
13// Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// Redistributions in binary form must reproduce the above
17// copyright notice, this list of conditions and the following
18// disclaimer in the documentation and/or other materials provided
19// with the distribution.
20//
21// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
22// contributors may be used to endorse or promote products derived
23// from this software without specific prior written permission.
24//
John Kessenich927608b2017-01-06 12:34:14 -070025// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000037//
John Kessenich05a70632013-09-17 19:26:08 +000038
39// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030040#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000041#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030042#endif
John Kessenich05a70632013-09-17 19:26:08 +000043
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040044#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000045#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060046#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include "./../glslang/Include/ShHandle.h"
48#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000049#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060050#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000051#include "../SPIRV/doc.h"
52#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060053
John Kessenich66ec80e2016-08-05 14:04:23 -060054#include <cstring>
55#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060056#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060057#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060058#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060059#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020060#include <memory>
61#include <thread>
Georg Lehmannf0502092021-04-20 00:22:15 +020062#include <set>
John Kessenicha0af4732012-12-12 21:15:54 +000063
baldurk876a0e32015-11-16 18:03:28 +010064#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000065
Ben Claytonfbe9a232020-06-17 11:17:19 +010066// Build-time generated includes
67#include "glslang/build_info.h"
68
amhagan035a3bb2021-09-01 11:33:21 -040069#include "glslang/glsl_intrinsic_header.h"
70
John Kessenicha0af4732012-12-12 21:15:54 +000071extern "C" {
Ben Claytond64e8592020-06-29 14:20:19 +010072 GLSLANG_EXPORT void ShOutputHtml();
John Kessenicha0af4732012-12-12 21:15:54 +000073}
74
John Kessenich94a81fb2013-08-31 02:41:30 +000075// Command-line options
76enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070077 EOptionNone = 0,
78 EOptionIntermediate = (1 << 0),
79 EOptionSuppressInfolog = (1 << 1),
80 EOptionMemoryLeakMode = (1 << 2),
81 EOptionRelaxedErrors = (1 << 3),
82 EOptionGiveWarnings = (1 << 4),
83 EOptionLinkProgram = (1 << 5),
84 EOptionMultiThreaded = (1 << 6),
85 EOptionDumpConfig = (1 << 7),
86 EOptionDumpReflection = (1 << 8),
87 EOptionSuppressWarnings = (1 << 9),
88 EOptionDumpVersions = (1 << 10),
89 EOptionSpv = (1 << 11),
90 EOptionHumanReadableSpv = (1 << 12),
91 EOptionVulkanRules = (1 << 13),
92 EOptionDefaultDesktop = (1 << 14),
93 EOptionOutputPreprocessed = (1 << 15),
94 EOptionOutputHexadecimal = (1 << 16),
95 EOptionReadHlsl = (1 << 17),
96 EOptionCascadingErrors = (1 << 18),
97 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060098 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070099 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -0700100 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -0600101 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -0600102 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -0600103 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -0600104 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200105 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -0600106 EOptionOptimizeDisable = (1 << 28),
107 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700108 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700109 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000110};
John Kessenich5d610ee2018-03-07 18:05:55 -0700111bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600112bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600113bool SpvToolsValidate = false;
John Kessenich605afc72019-06-17 23:33:09 -0600114bool NaNClamp = false;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400115bool stripDebugInfo = false;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200116bool beQuiet = false;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600117bool VulkanRulesRelaxed = false;
Greg Fischerebfca602021-04-21 16:49:53 -0600118bool autoSampledTextures = false;
John Kessenich94a81fb2013-08-31 02:41:30 +0000119
John Kessenicha0af4732012-12-12 21:15:54 +0000120//
John Kessenich68d78fd2015-07-12 19:28:10 -0600121// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000122//
123enum TFailCode {
124 ESuccess = 0,
125 EFailUsage,
126 EFailCompile,
127 EFailLink,
128 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000129 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000130 EFailLinkerCreate
131};
132
133//
John Kessenich68d78fd2015-07-12 19:28:10 -0600134// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000135//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600136EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000137void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000138void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600139char* ReadFileData(const char* fileName);
140void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000141void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000142
John Kessenichc999ba22013-11-07 23:33:24 +0000143// Globally track if any compile or link failure.
144bool CompileFailed = false;
145bool LinkFailed = false;
146
John Kessenich94f28eb2017-11-13 01:32:06 -0700147// array of unique places to leave the shader names and infologs for the asynchronous compiles
148std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
149
John Kessenich05a70632013-09-17 19:26:08 +0000150TBuiltInResource Resources;
151std::string ConfigFile;
152
John Kessenicha0af4732012-12-12 21:15:54 +0000153//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600154// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000155//
156void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000157{
John Kessenich04acb1b2017-06-14 17:36:50 -0600158 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500159 Resources = glslang::DefaultTBuiltInResource;
John Kessenich23d27752019-07-28 02:12:10 -0600160#ifndef GLSLANG_WEB
John Kessenich04acb1b2017-06-14 17:36:50 -0600161 else {
162 char* configString = ReadFileData(ConfigFile.c_str());
163 glslang::DecodeResourceLimits(&Resources, configString);
164 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000165 }
John Kessenich23d27752019-07-28 02:12:10 -0600166#endif
John Kessenicha0af4732012-12-12 21:15:54 +0000167}
168
baldurk6d477852019-01-29 15:45:56 +0000169int ReflectOptions = EShReflectionDefault;
John Kessenich94a81fb2013-08-31 02:41:30 +0000170int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600171const char* ExecutableName = nullptr;
172const char* binaryFileName = nullptr;
Georg Lehmannf0502092021-04-20 00:22:15 +0200173const char* depencyFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700174const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600175const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400176const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800177const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800178bool HlslEnable16BitTypes = false;
John Kessenichbd1c1832018-12-07 18:38:26 -0700179bool HlslDX9compatible = false;
Greg Fischere9564fe2021-11-16 18:42:12 -0700180bool HlslDxPositionW = false;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200181bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600182std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600183
184// Source environment
185// (source 'Client' is currently the same as target 'Client')
186int ClientInputSemanticsVersion = 100;
187
188// Target environment
189glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
190glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
191glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
192glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
193
John Kessenich66011cb2018-03-06 16:12:04 -0700194std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600195
LoopDawg08a14422017-10-17 19:27:14 -0600196// Per descriptor-set binding base data
197typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
198
Neil Roberts16f53472018-03-20 17:30:53 +0100199std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100200int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100201
LoopDawg08a14422017-10-17 19:27:14 -0600202std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
203std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900204std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600205
greg-lunarg4e064ee2021-03-15 11:26:11 -0600206std::vector<std::pair<std::string, glslang::TBlockStorageClass>> blockStorageOverrides;
207
208bool setGlobalUniformBlock = false;
209std::string globalUniformName;
210unsigned int globalUniformBinding;
211unsigned int globalUniformSet;
212
213bool setGlobalBufferBlock = false;
214std::string atomicCounterBlockName;
215unsigned int atomicCounterBlockSet;
216
John Kessenicha9313662017-06-15 10:40:49 -0600217// Add things like "#define ..." to a preamble to use in the beginning of the shader.
218class TPreamble {
219public:
220 TPreamble() { }
221
222 bool isSet() const { return text.size() > 0; }
223 const char* get() const { return text.c_str(); }
224
225 // #define...
226 void addDef(std::string def)
227 {
228 text.append("#define ");
229 fixLine(def);
230
John Kessenich6f988922020-01-07 07:03:11 -0700231 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600232 Processes.back().append(def);
233
John Kessenicha9313662017-06-15 10:40:49 -0600234 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600235 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600236 if (equal != def.npos)
237 def[equal] = ' ';
238
239 text.append(def);
240 text.append("\n");
241 }
242
243 // #undef...
244 void addUndef(std::string undef)
245 {
246 text.append("#undef ");
247 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600248
John Kessenich6f988922020-01-07 07:03:11 -0700249 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600250 Processes.back().append(undef);
251
John Kessenicha9313662017-06-15 10:40:49 -0600252 text.append(undef);
253 text.append("\n");
254 }
255
256protected:
257 void fixLine(std::string& line)
258 {
259 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600260 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600261 if (end != line.npos)
262 line = line.substr(0, end);
263 }
264
265 std::string text; // contents of preamble
266};
267
John Kessenich83855b92020-03-30 00:27:31 -0600268// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600269TPreamble UserPreamble;
amhagan035a3bb2021-09-01 11:33:21 -0400270std::string PreambleString;
John Kessenicha9313662017-06-15 10:40:49 -0600271
John Kessenich68d78fd2015-07-12 19:28:10 -0600272//
273// Create the default name for saving a binary if -o is not provided.
274//
275const char* GetBinaryName(EShLanguage stage)
276{
277 const char* name;
278 if (binaryFileName == nullptr) {
279 switch (stage) {
280 case EShLangVertex: name = "vert.spv"; break;
281 case EShLangTessControl: name = "tesc.spv"; break;
282 case EShLangTessEvaluation: name = "tese.spv"; break;
283 case EShLangGeometry: name = "geom.spv"; break;
284 case EShLangFragment: name = "frag.spv"; break;
285 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400286 case EShLangRayGen: name = "rgen.spv"; break;
287 case EShLangIntersect: name = "rint.spv"; break;
288 case EShLangAnyHit: name = "rahit.spv"; break;
289 case EShLangClosestHit: name = "rchit.spv"; break;
290 case EShLangMiss: name = "rmiss.spv"; break;
291 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700292 case EShLangMeshNV: name = "mesh.spv"; break;
293 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600294 default: name = "unknown"; break;
295 }
296 } else
297 name = binaryFileName;
298
299 return name;
300}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000301
John Kessenich05a70632013-09-17 19:26:08 +0000302//
303// *.conf => this is a config file that can set limits/resources
304//
305bool SetConfigFile(const std::string& name)
306{
307 if (name.size() < 5)
308 return false;
309
John Kessenich4c706852013-10-11 16:28:43 +0000310 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000311 ConfigFile = name;
312 return true;
313 }
314
315 return false;
316}
317
John Kessenich68d78fd2015-07-12 19:28:10 -0600318//
319// Give error and exit with failure code.
320//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700321void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600322{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700323 fprintf(stderr, "%s: Error: ", ExecutableName);
324 if (detail != nullptr)
325 fprintf(stderr, "%s: ", detail);
326 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600327 exit(EFailUsage);
328}
329
330//
LoopDawg08a14422017-10-17 19:27:14 -0600331// Process an optional binding base of one the forms:
332// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600333// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600334
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600335// Where stage is one of the forms accepted by FindLanguage, and base is an integer
336//
LoopDawg08a14422017-10-17 19:27:14 -0600337void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600338{
339 if (argc < 2)
340 usage();
341
LoopDawg08a14422017-10-17 19:27:14 -0600342 EShLanguage lang = EShLangCount;
343 int singleBase = 0;
344 TPerSetBaseBinding perSetBase;
345 int arg = 1;
346
347 // Parse stage, if given
348 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600349 if (argc < 3) // this form needs one more argument
350 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700351
LoopDawg08a14422017-10-17 19:27:14 -0600352 lang = FindLanguage(argv[arg++], false);
353 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600354
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800355 if ((argc - arg) >= 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600356 // Parse a per-set binding base
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800357 do {
LoopDawg08a14422017-10-17 19:27:14 -0600358 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600359 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600360 perSetBase[setNum] = baseNum;
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800361 } while ((argc - arg) >= 2 && isdigit(argv[arg + 0][0]) && isdigit(argv[arg + 1][0]));
LoopDawg08a14422017-10-17 19:27:14 -0600362 } else {
363 // Parse single binding base
364 singleBase = atoi(argv[arg++]);
365 }
366
367 argc -= (arg-1);
368 argv += (arg-1);
369
370 // Set one or all languages
371 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
372 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
373
374 for (int lang = langMin; lang < langMax; ++lang) {
375 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600376 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600377 else
378 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600379 }
380}
381
Hyangran Park36dc8292017-05-02 16:27:29 +0900382void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
383{
384 if (argc < 2)
385 usage();
386
387 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600388 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900389 usage();
390
LoopDawg52017192017-07-14 15:15:47 -0600391 // Parse form: --argname stage [regname set base...], or:
392 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900393 const EShLanguage lang = FindLanguage(argv[1], false);
394
LoopDawg52017192017-07-14 15:15:47 -0600395 argc--;
396 argv++;
397
398 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
399 base[lang].push_back(argv[1]);
400
401 argc--;
402 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900403 }
LoopDawg52017192017-07-14 15:15:47 -0600404
405 // Must have one arg, or a multiple of three (for [regname set binding] triples)
406 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
407 usage();
408
Hyangran Park36dc8292017-05-02 16:27:29 +0900409 } else {
LoopDawg52017192017-07-14 15:15:47 -0600410 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900411 for (int lang=0; lang<EShLangCount; ++lang)
412 base[lang].push_back(argv[1]);
413
414 argc--;
415 argv++;
416 }
417}
418
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600419//
greg-lunarg4e064ee2021-03-15 11:26:11 -0600420// Process an optional binding base of one the forms:
421// --argname name {uniform|buffer|push_constant}
422void ProcessBlockStorage(int& argc, char**& argv, std::vector<std::pair<std::string, glslang::TBlockStorageClass>>& storage)
423{
424 if (argc < 3)
425 usage();
426
427 glslang::TBlockStorageClass blockStorage = glslang::EbsNone;
428
429 std::string strBacking(argv[2]);
430 if (strBacking == "uniform")
431 blockStorage = glslang::EbsUniform;
432 else if (strBacking == "buffer")
433 blockStorage = glslang::EbsStorageBuffer;
434 else if (strBacking == "push_constant")
435 blockStorage = glslang::EbsPushConstant;
436 else {
437 printf("%s: invalid block storage\n", strBacking.c_str());
438 usage();
439 }
440
441 storage.push_back(std::make_pair(std::string(argv[1]), blockStorage));
442
443 argc -= 2;
444 argv += 2;
445}
446
447inline bool isNonDigit(char c) {
448 // a non-digit character valid in a glsl identifier
449 return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
450}
451
452// whether string isa valid identifier to be used in glsl
453bool isValidIdentifier(const char* str) {
454 std::string idn(str);
455
456 if (idn.length() == 0) {
457 return false;
458 }
459
460 if (idn.length() >= 3 && idn.substr(0, 3) == "gl_") {
461 // identifiers startin with "gl_" are reserved
462 return false;
463 }
464
465 if (!isNonDigit(idn[0])) {
466 return false;
467 }
468
469 for (unsigned int i = 1; i < idn.length(); ++i) {
470 if (!(isdigit(idn[i]) || isNonDigit(idn[i]))) {
471 return false;
472 }
473 }
474
475 return true;
476}
477
478// Process settings for either the global buffer block or global unfirom block
479// of the form:
480// --argname name set binding
481void ProcessGlobalBlockSettings(int& argc, char**& argv, std::string* name, unsigned int* set, unsigned int* binding)
482{
483 if (argc < 4)
484 usage();
485
486 unsigned int curArg = 1;
487
488 assert(name || set || binding);
489
490 if (name) {
491 if (!isValidIdentifier(argv[curArg])) {
492 printf("%s: invalid identifier\n", argv[curArg]);
493 usage();
494 }
495 *name = argv[curArg];
496
497 curArg++;
498 }
499
500 if (set) {
501 errno = 0;
502 int setVal = ::strtol(argv[curArg], NULL, 10);
503 if (errno || setVal < 0) {
504 printf("%s: invalid set\n", argv[curArg]);
505 usage();
506 }
507 *set = setVal;
508
509 curArg++;
510 }
511
512 if (binding) {
513 errno = 0;
514 int bindingVal = ::strtol(argv[curArg], NULL, 10);
515 if (errno || bindingVal < 0) {
516 printf("%s: invalid binding\n", argv[curArg]);
517 usage();
518 }
519 *binding = bindingVal;
520
521 curArg++;
522 }
523
524 argc -= (curArg - 1);
525 argv += (curArg - 1);
526}
527
528//
John Kessenich68d78fd2015-07-12 19:28:10 -0600529// Do all command-line argument parsing. This includes building up the work-items
530// to be processed later, and saving all the command-line options.
531//
532// Does not return (it exits) if command-line is fatally flawed.
533//
Juan Lopeza558b262017-04-02 23:04:00 +0200534void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000535{
LoopDawg08a14422017-10-17 19:27:14 -0600536 for (int res = 0; res < glslang::EResCount; ++res)
537 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600538
John Kessenich38f3b892013-09-06 19:52:57 +0000539 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200540 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000541
John Kessenichc178f0a2017-06-23 10:58:31 -0600542 const auto bumpArg = [&]() {
543 if (argc > 0) {
544 argc--;
545 argv++;
546 }
547 };
548
549 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600550 const auto getStringOperand = [&](const char* desc) {
551 if (argv[0][2] == 0) {
552 printf("%s must immediately follow option (no spaces)\n", desc);
553 exit(EFailUsage);
554 }
555 return argv[0] + 2;
556 };
557
John Kessenich6353d552017-06-23 11:11:09 -0600558 // read a number attached to a single-letter option
559 const auto getAttachedNumber = [&](const char* desc) {
560 int num = atoi(argv[0] + 2);
561 if (num == 0) {
562 printf("%s: expected attached non-0 number\n", desc);
563 exit(EFailUsage);
564 }
565 return num;
566 };
567
568 // minimum needed (without overriding something else) to target Vulkan SPIR-V
569 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600570 if (Client == glslang::EShClientNone)
571 ClientVersion = glslang::EShTargetVulkan_1_0;
572 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600573 Options |= EOptionSpv;
574 Options |= EOptionVulkanRules;
575 Options |= EOptionLinkProgram;
576 };
577
578 // minimum needed (without overriding something else) to target OpenGL SPIR-V
579 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600580 if (Client == glslang::EShClientNone)
581 ClientVersion = glslang::EShTargetOpenGL_450;
582 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600583 Options |= EOptionSpv;
584 Options |= EOptionLinkProgram;
585 // undo a -H default to Vulkan
586 Options &= ~EOptionVulkanRules;
587 };
588
Neil Roberts16f53472018-03-20 17:30:53 +0100589 const auto getUniformOverride = [getStringOperand]() {
590 const char *arg = getStringOperand("-u<name>:<location>");
591 const char *split = strchr(arg, ':');
592 if (split == NULL) {
593 printf("%s: missing location\n", arg);
594 exit(EFailUsage);
595 }
596 errno = 0;
597 int location = ::strtol(split + 1, NULL, 10);
598 if (errno) {
599 printf("%s: invalid location\n", arg);
600 exit(EFailUsage);
601 }
602 return std::make_pair(std::string(arg, split - arg), location);
603 };
604
John Kessenichc178f0a2017-06-23 10:58:31 -0600605 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000606 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000607 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600608 case '-':
609 {
610 std::string lowerword(argv[0]+2);
611 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
612
613 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600614 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600615 lowerword == "auto-map-binding" ||
616 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600617 Options |= EOptionAutoMapBindings;
618 } else if (lowerword == "auto-map-locations" || // synonyms
619 lowerword == "aml") {
620 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100621 } else if (lowerword == "uniform-base") {
622 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700623 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100624 uniformBase = ::strtol(argv[1], NULL, 10);
625 bumpArg();
626 break;
John Kessenich6353d552017-06-23 11:11:09 -0600627 } else if (lowerword == "client") {
628 if (argc > 1) {
629 if (strcmp(argv[1], "vulkan100") == 0)
630 setVulkanSpv();
631 else if (strcmp(argv[1], "opengl100") == 0)
632 setOpenGlSpv();
633 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700634 Error("expects vulkan100 or opengl100", lowerword.c_str());
635 } else
636 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600637 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700638 } else if (lowerword == "define-macro" ||
639 lowerword == "d") {
640 if (argc > 1)
641 UserPreamble.addDef(argv[1]);
642 else
643 Error("expects <name[=def]>", argv[0]);
644 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200645 } else if (lowerword == "dump-builtin-symbols") {
646 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600647 } else if (lowerword == "entry-point") {
648 entryPointName = argv[1];
649 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700650 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600651 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600652 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
653 lowerword == "flatten-uniform-array" ||
654 lowerword == "fua") {
655 Options |= EOptionFlattenUniformArrays;
656 } else if (lowerword == "hlsl-offsets") {
657 Options |= EOptionHlslOffsets;
658 } else if (lowerword == "hlsl-iomap" ||
659 lowerword == "hlsl-iomapper" ||
660 lowerword == "hlsl-iomapping") {
661 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800662 } else if (lowerword == "hlsl-enable-16bit-types") {
663 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700664 } else if (lowerword == "hlsl-dx9-compatible") {
665 HlslDX9compatible = true;
Greg Fischere9564fe2021-11-16 18:42:12 -0700666 } else if (lowerword == "hlsl-dx-position-w") {
667 HlslDxPositionW = true;
Greg Fischerebfca602021-04-21 16:49:53 -0600668 } else if (lowerword == "auto-sampled-textures") {
669 autoSampledTextures = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700670 } else if (lowerword == "invert-y" || // synonyms
671 lowerword == "iy") {
672 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600673 } else if (lowerword == "keep-uncalled" || // synonyms
674 lowerword == "ku") {
675 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600676 } else if (lowerword == "nan-clamp") {
677 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600678 } else if (lowerword == "no-storage-format" || // synonyms
679 lowerword == "nsf") {
680 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600681 } else if (lowerword == "relaxed-errors") {
682 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000683 } else if (lowerword == "reflect-strict-array-suffix") {
684 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000685 } else if (lowerword == "reflect-basic-array-suffix") {
686 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000687 } else if (lowerword == "reflect-intermediate-io") {
688 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000689 } else if (lowerword == "reflect-separate-buffers") {
690 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000691 } else if (lowerword == "reflect-all-block-variables") {
692 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000693 } else if (lowerword == "reflect-unwrap-io-blocks") {
694 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800695 } else if (lowerword == "reflect-all-io-variables") {
696 ReflectOptions |= EShReflectionAllIOVariables;
697 } else if (lowerword == "reflect-shared-std140-ubo") {
698 ReflectOptions |= EShReflectionSharedStd140UBO;
699 } else if (lowerword == "reflect-shared-std140-ssbo") {
700 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600701 } else if (lowerword == "resource-set-bindings" || // synonyms
702 lowerword == "resource-set-binding" ||
703 lowerword == "rsb") {
704 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
greg-lunarg4e064ee2021-03-15 11:26:11 -0600705 } else if (lowerword == "set-block-storage" ||
706 lowerword == "sbs") {
707 ProcessBlockStorage(argc, argv, blockStorageOverrides);
708 } else if (lowerword == "set-atomic-counter-block" ||
709 lowerword == "sacb") {
710 ProcessGlobalBlockSettings(argc, argv, &atomicCounterBlockName, &atomicCounterBlockSet, nullptr);
711 setGlobalBufferBlock = true;
712 } else if (lowerword == "set-default-uniform-block" ||
713 lowerword == "sdub") {
714 ProcessGlobalBlockSettings(argc, argv, &globalUniformName, &globalUniformSet, &globalUniformBinding);
715 setGlobalUniformBlock = true;
steve-lunarg9088be42016-11-01 10:31:42 -0600716 } else if (lowerword == "shift-image-bindings" || // synonyms
717 lowerword == "shift-image-binding" ||
718 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600719 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600720 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600721 lowerword == "shift-sampler-binding" ||
722 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600723 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600724 } else if (lowerword == "shift-uav-bindings" || // synonyms
725 lowerword == "shift-uav-binding" ||
726 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600727 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600728 } else if (lowerword == "shift-texture-bindings" || // synonyms
729 lowerword == "shift-texture-binding" ||
730 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600731 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600732 } else if (lowerword == "shift-ubo-bindings" || // synonyms
733 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600734 lowerword == "shift-cbuffer-bindings" ||
735 lowerword == "shift-cbuffer-binding" ||
736 lowerword == "sub" ||
737 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600738 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700739 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
740 lowerword == "shift-ssbo-binding" ||
741 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600742 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600743 } else if (lowerword == "source-entrypoint" || // synonyms
744 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600745 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700746 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600747 sourceEntryPointName = argv[1];
748 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600749 break;
John Kesseniche2156222018-06-11 18:12:15 -0600750 } else if (lowerword == "spirv-dis") {
751 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600752 } else if (lowerword == "spirv-val") {
753 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200754 } else if (lowerword == "stdin") {
755 Options |= EOptionStdin;
756 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600757 } else if (lowerword == "suppress-warnings") {
758 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600759 } else if (lowerword == "target-env") {
760 if (argc > 1) {
761 if (strcmp(argv[1], "vulkan1.0") == 0) {
762 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600763 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700764 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
765 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600766 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700767 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
768 setVulkanSpv();
769 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600770 } else if (strcmp(argv[1], "opengl") == 0) {
771 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600772 ClientVersion = glslang::EShTargetOpenGL_450;
773 } else if (strcmp(argv[1], "spirv1.0") == 0) {
774 TargetLanguage = glslang::EShTargetSpv;
775 TargetVersion = glslang::EShTargetSpv_1_0;
776 } else if (strcmp(argv[1], "spirv1.1") == 0) {
777 TargetLanguage = glslang::EShTargetSpv;
778 TargetVersion = glslang::EShTargetSpv_1_1;
779 } else if (strcmp(argv[1], "spirv1.2") == 0) {
780 TargetLanguage = glslang::EShTargetSpv;
781 TargetVersion = glslang::EShTargetSpv_1_2;
782 } else if (strcmp(argv[1], "spirv1.3") == 0) {
783 TargetLanguage = glslang::EShTargetSpv;
784 TargetVersion = glslang::EShTargetSpv_1_3;
785 } else if (strcmp(argv[1], "spirv1.4") == 0) {
786 TargetLanguage = glslang::EShTargetSpv;
787 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600788 } else if (strcmp(argv[1], "spirv1.5") == 0) {
789 TargetLanguage = glslang::EShTargetSpv;
790 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600791 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700792 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600793 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600794 }
795 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700796 } else if (lowerword == "undef-macro" ||
797 lowerword == "u") {
798 if (argc > 1)
799 UserPreamble.addUndef(argv[1]);
800 else
801 Error("expects <name>", argv[0]);
802 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800803 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700804 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800805 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600806 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700807 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600808 variableName = argv[1];
809 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800810 break;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200811 } else if (lowerword == "quiet") {
812 beQuiet = true;
Georg Lehmannf0502092021-04-20 00:22:15 +0200813 } else if (lowerword == "depfile") {
814 if (argc <= 1)
815 Error("no <depfile-name> provided", lowerword.c_str());
816 depencyFileName = argv[1];
817 bumpArg();
John Kessenichc6c80a62018-03-05 22:23:17 -0700818 } else if (lowerword == "version") {
819 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800820 } else if (lowerword == "help") {
821 usage();
822 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600823 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700824 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600825 }
826 }
827 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600828 case 'C':
829 Options |= EOptionCascadingErrors;
830 break;
831 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600832 if (argv[0][2] == 0)
833 Options |= EOptionReadHlsl;
834 else
John Kessenich6f988922020-01-07 07:03:11 -0700835 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600836 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100837 case 'u':
838 uniformLocationOverrides.push_back(getUniformOverride());
839 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600840 case 'E':
841 Options |= EOptionOutputPreprocessed;
842 break;
843 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600844 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600845 setOpenGlSpv();
846 if (argv[0][2] != 0)
847 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600848 if (ClientInputSemanticsVersion != 100)
849 Error("unknown client version for -G, should be 100");
John Kessenich6263fb12017-06-14 15:52:44 -0600850 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000851 case 'H':
852 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600853 if ((Options & EOptionSpv) == 0) {
854 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600855 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600856 }
857 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600858 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600859 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600860 break;
GregFcd1f1692017-09-21 18:40:22 -0600861 case 'O':
862 if (argv[0][2] == 'd')
863 Options |= EOptionOptimizeDisable;
864 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600865#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600866 Options |= EOptionOptimizeSize;
867#else
868 Error("-Os not available; optimizer not linked");
869#endif
870 else
871 Error("unknown -O option");
872 break;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600873 case 'R':
874 VulkanRulesRelaxed = true;
875 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400876 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600877 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400878 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600879 shaderStageName = argv[1];
880 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400881 break;
John Kessenicha9313662017-06-15 10:40:49 -0600882 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700883 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600884 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600885 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600886 setVulkanSpv();
887 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500888 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600889 if (ClientInputSemanticsVersion != 100)
890 Error("unknown client version for -V, should be 100");
John Kessenichc555ddd2015-06-17 02:38:44 +0000891 break;
John Kessenich05a70632013-09-17 19:26:08 +0000892 case 'c':
893 Options |= EOptionDumpConfig;
894 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000895 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700896 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
897 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
898 Options |= EOptionDumpBareVersion;
899 else
900 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000901 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700902 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700903 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600904 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600905 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600906 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700907 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700908 case 'f':
909 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
910 targetHlslFunctionality1 = true;
911 else
912 Error("-f: expected hlsl_functionality1");
913 break;
John Kessenich121853f2017-05-31 17:11:16 -0600914 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400915 // Override previous -g or -g0 argument
916 stripDebugInfo = false;
917 Options &= ~EOptionDebug;
918 if (argv[0][2] == '0')
919 stripDebugInfo = true;
920 else
921 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600922 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600923 case 'h':
924 usage();
925 break;
John Kessenich05a70632013-09-17 19:26:08 +0000926 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000927 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000928 break;
929 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000930 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000931 break;
932 case 'm':
933 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000934 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600935 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600936 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600937 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600938 binaryFileName = argv[1];
939 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600940 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000941 case 'q':
942 Options |= EOptionDumpReflection;
943 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000944 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000945 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000946 break;
947 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000948 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000949 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000950 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200951 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000952 break;
John Kessenich319de232013-12-04 04:43:40 +0000953 case 'v':
954 Options |= EOptionDumpVersions;
955 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000956 case 'w':
957 Options |= EOptionSuppressWarnings;
958 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500959 case 'x':
960 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500961 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000962 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700963 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600964 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000965 }
John Kessenich38f3b892013-09-06 19:52:57 +0000966 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000967 std::string name(argv[0]);
968 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200969 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000970 }
John Kessenich38f3b892013-09-06 19:52:57 +0000971 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000972 }
973
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200974 // Make sure that -S is always specified if --stdin is specified
975 if ((Options & EOptionStdin) && shaderStageName == nullptr)
976 Error("must provide -S when --stdin is given");
977
John Kessenich68d78fd2015-07-12 19:28:10 -0600978 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600979 // Or things that require linking
980 if (Options & EOptionOutputPreprocessed) {
981 if (Options & EOptionLinkProgram)
982 Error("can't use -E when linking is selected");
983 if (Options & EOptionDumpReflection)
984 Error("reflection requires linking, which can't be used when -E when is selected");
985 }
986
987 // reflection requires linking
988 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
989 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000990
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500991 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600992 if (binaryFileName && (Options & EOptionSpv) == 0)
993 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600994
995 if ((Options & EOptionFlattenUniformArrays) != 0 &&
996 (Options & EOptionReadHlsl) == 0)
997 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600998
Jeremy Hayesb73afa82021-01-05 15:54:41 -0700999 if ((Options & EOptionReadHlsl) && (Client == glslang::EShClientOpenGL)) {
1000 Error("Using HLSL input under OpenGL semantics is not currently supported.");
1001 }
1002
John Kessenich8717a5d2018-10-26 10:12:32 -06001003 // rationalize client and target language
1004 if (TargetLanguage == glslang::EShTargetNone) {
1005 switch (ClientVersion) {
1006 case glslang::EShTargetVulkan_1_0:
1007 TargetLanguage = glslang::EShTargetSpv;
1008 TargetVersion = glslang::EShTargetSpv_1_0;
1009 break;
1010 case glslang::EShTargetVulkan_1_1:
1011 TargetLanguage = glslang::EShTargetSpv;
1012 TargetVersion = glslang::EShTargetSpv_1_3;
1013 break;
John Kessenich273d3a52020-01-15 00:10:41 -07001014 case glslang::EShTargetVulkan_1_2:
1015 TargetLanguage = glslang::EShTargetSpv;
1016 TargetVersion = glslang::EShTargetSpv_1_5;
1017 break;
John Kessenich8717a5d2018-10-26 10:12:32 -06001018 case glslang::EShTargetOpenGL_450:
1019 TargetLanguage = glslang::EShTargetSpv;
1020 TargetVersion = glslang::EShTargetSpv_1_0;
1021 break;
1022 default:
1023 break;
1024 }
1025 }
1026 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
1027 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +00001028}
1029
John Kessenich68d78fd2015-07-12 19:28:10 -06001030//
1031// Translate the meaningful subset of command-line options to parser-behavior options.
1032//
John Kessenichb0a7eb52013-11-07 17:44:20 +00001033void SetMessageOptions(EShMessages& messages)
1034{
1035 if (Options & EOptionRelaxedErrors)
1036 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
1037 if (Options & EOptionIntermediate)
1038 messages = (EShMessages)(messages | EShMsgAST);
1039 if (Options & EOptionSuppressWarnings)
1040 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -06001041 if (Options & EOptionSpv)
1042 messages = (EShMessages)(messages | EShMsgSpvRules);
1043 if (Options & EOptionVulkanRules)
1044 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001045 if (Options & EOptionOutputPreprocessed)
1046 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -07001047 if (Options & EOptionReadHlsl)
1048 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -06001049 if (Options & EOptionCascadingErrors)
1050 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -07001051 if (Options & EOptionKeepUncalled)
1052 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -06001053 if (Options & EOptionHlslOffsets)
1054 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -06001055 if (Options & EOptionDebug)
1056 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +08001057 if (HlslEnable16BitTypes)
1058 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -06001059 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
1060 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -07001061 if (HlslDX9compatible)
1062 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +02001063 if (DumpBuiltinSymbols)
1064 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +00001065}
1066
John Kessenich68d78fd2015-07-12 19:28:10 -06001067//
John Kessenich69f4b512013-09-04 21:19:27 +00001068// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +00001069//
Juan Lopeza558b262017-04-02 23:04:00 +02001070void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001071{
John Kessenich7f0bcfd2018-04-05 19:00:01 -06001072 if (Options & EOptionDebug)
1073 Error("cannot generate debug information unless linking to generate code");
1074
John Kessenich38f3b892013-09-06 19:52:57 +00001075 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001076 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -06001077 if (worklist.remove(workItem)) {
1078 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
1079 if (compiler == nullptr)
1080 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001081
John Kesseniche7f9cae2018-07-12 15:11:07 -06001082 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001083
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001084 if (! (Options & EOptionSuppressInfolog))
1085 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001086
John Kesseniche7f9cae2018-07-12 15:11:07 -06001087 ShDestruct(compiler);
1088 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001089 } else {
1090 while (worklist.remove(workItem)) {
1091 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
1092 if (compiler == 0)
1093 return;
1094
1095 CompileFile(workItem->name.c_str(), compiler);
1096
1097 if (! (Options & EOptionSuppressInfolog))
1098 workItem->results = ShGetInfoLog(compiler);
1099
1100 ShDestruct(compiler);
1101 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001102 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001103}
1104
John Kessenich6626cad2015-06-19 05:14:19 +00001105// Outputs the given string, but only if it is non-null and non-empty.
1106// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001107void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +00001108{
1109 if (str && str[0]) {
1110 puts(str);
1111 }
1112}
1113
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001114// Outputs the given string to stderr, but only if it is non-null and non-empty.
1115// This prevents erroneous newlines from appearing.
1116void StderrIfNonEmpty(const char* str)
1117{
John Kessenich04acb1b2017-06-14 17:36:50 -06001118 if (str && str[0])
1119 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001120}
1121
John Kessenichc57b2a92016-01-16 15:30:03 -07001122// Simple bundling of what makes a compilation unit for ease in passing around,
1123// and separation of handling file IO versus API (programmatic) compilation.
1124struct ShaderCompUnit {
1125 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001126 static const int maxCount = 1;
1127 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -06001128 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -06001129 std::string fileName[maxCount]; // hold's the memory, but...
1130 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -04001131
John Kessenich04acb1b2017-06-14 17:36:50 -06001132 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -04001133
John Kessenich04acb1b2017-06-14 17:36:50 -06001134 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -04001135 {
1136 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001137 count = rhs.count;
1138 for (int i = 0; i < count; ++i) {
1139 fileName[i] = rhs.fileName[i];
1140 text[i] = rhs.text[i];
1141 fileNameList[i] = rhs.fileName[i].c_str();
1142 }
dankbakerafe6e9c2016-08-21 12:29:08 -04001143 }
1144
John Kessenicha9313662017-06-15 10:40:49 -06001145 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -06001146 {
1147 assert(count < maxCount);
1148 fileName[count] = ifileName;
1149 text[count] = itext;
1150 fileNameList[count] = fileName[count].c_str();
1151 ++count;
1152 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001153};
1154
Georg Lehmannf0502092021-04-20 00:22:15 +02001155// Writes a depfile similar to gcc -MMD foo.c
1156bool writeDepFile(std::string depfile, std::vector<std::string>& binaryFiles, const std::vector<std::string>& sources)
1157{
1158 std::ofstream file(depfile);
1159 if (file.fail())
1160 return false;
1161
1162 for (auto it = binaryFiles.begin(); it != binaryFiles.end(); it++) {
1163 file << *it << ":";
1164 for (auto it = sources.begin(); it != sources.end(); it++) {
1165 file << " " << *it;
1166 }
1167 file << std::endl;
1168 }
1169 return true;
1170}
1171
John Kessenich69f4b512013-09-04 21:19:27 +00001172//
John Kessenichc57b2a92016-01-16 15:30:03 -07001173// For linking mode: Will independently parse each compilation unit, but then put them
1174// in the same program and link them together, making at most one linked module per
1175// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +00001176//
1177// Uses the new C++ interface instead of the old handle-based interface.
1178//
John Kessenichc57b2a92016-01-16 15:30:03 -07001179
1180void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +00001181{
1182 // keep track of what to free
1183 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001184
John Kessenich69f4b512013-09-04 21:19:27 +00001185 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001186 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001187
Georg Lehmannf0502092021-04-20 00:22:15 +02001188 DirStackFileIncluder includer;
1189 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1190 includer.pushExternalLocalDirectory(dir); });
1191
1192 std::vector<std::string> sources;
1193
John Kessenich69f4b512013-09-04 21:19:27 +00001194 //
1195 // Per-shader processing...
1196 //
1197
John Kessenich5b0f13a2013-11-01 03:08:40 +00001198 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001199 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1200 const auto &compUnit = *it;
Georg Lehmannf0502092021-04-20 00:22:15 +02001201 for (int i = 0; i < compUnit.count; i++) {
1202 sources.push_back(compUnit.fileNameList[i]);
1203 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001204 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001205 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001206 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001207 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001208 if (sourceEntryPointName) {
1209 if (entryPointName == nullptr)
1210 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1211 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001212 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001213 }
amhagan035a3bb2021-09-01 11:33:21 -04001214
1215 std::string intrinsicString = getIntrinsic(compUnit.text, compUnit.count);
1216
1217 PreambleString = "";
John Kessenicha9313662017-06-15 10:40:49 -06001218 if (UserPreamble.isSet())
amhagan035a3bb2021-09-01 11:33:21 -04001219 PreambleString.append(UserPreamble.get());
1220
1221 if (!intrinsicString.empty())
1222 PreambleString.append(intrinsicString);
1223
1224 shader->setPreamble(PreambleString.c_str());
John Kessenich2a271162017-07-20 20:00:36 -06001225 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001226
John Kessenich155d3512019-08-08 23:29:20 -06001227#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001228 // Set IO mapper binding shift values
1229 for (int r = 0; r < glslang::EResCount; ++r) {
1230 const glslang::TResourceType res = glslang::TResourceType(r);
1231
1232 // Set base bindings
1233 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001234
LoopDawg08a14422017-10-17 19:27:14 -06001235 // Set bindings for particular resource sets
1236 // TODO: use a range based for loop here, when available in all environments.
1237 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1238 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001239 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001240 }
steve-lunargcce8d482016-10-14 18:36:42 -06001241 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001242 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001243
Greg Fischerebfca602021-04-21 16:49:53 -06001244 if (autoSampledTextures)
Robin Quint219b7b92021-04-20 07:58:36 +02001245 shader->setTextureSamplerTransformMode(EShTexSampTransUpgradeTextureRemoveSampler);
1246
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001247 if (Options & EOptionAutoMapBindings)
1248 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001249
John Kessenich71facdf2017-05-17 18:28:19 -06001250 if (Options & EOptionAutoMapLocations)
1251 shader->setAutoMapLocations(true);
1252
Neil Roberts16f53472018-03-20 17:30:53 +01001253 for (auto& uniOverride : uniformLocationOverrides) {
1254 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1255 uniOverride.second);
1256 }
1257
Neil Robertsb0f3d792018-03-20 17:41:05 +01001258 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001259#endif
1260
greg-lunarg4e064ee2021-03-15 11:26:11 -06001261 if (VulkanRulesRelaxed) {
1262 for (auto& storageOverride : blockStorageOverrides) {
1263 shader->addBlockStorageOverride(storageOverride.first.c_str(),
1264 storageOverride.second);
1265 }
1266
1267 if (setGlobalBufferBlock) {
1268 shader->setAtomicCounterBlockName(atomicCounterBlockName.c_str());
1269 shader->setAtomicCounterBlockSet(atomicCounterBlockSet);
1270 }
1271
1272 if (setGlobalUniformBlock) {
1273 shader->setGlobalUniformBlockName(globalUniformName.c_str());
1274 shader->setGlobalUniformSet(globalUniformSet);
1275 shader->setGlobalUniformBinding(globalUniformBinding);
1276 }
1277 }
1278
John Kessenich155d3512019-08-08 23:29:20 -06001279 shader->setNanMinMaxClamp(NaNClamp);
1280
1281#ifdef ENABLE_HLSL
1282 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1283 if (Options & EOptionHlslIoMapping)
1284 shader->setHlslIoMapping(true);
1285#endif
1286
1287 if (Options & EOptionInvertY)
1288 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001289
Greg Fischere9564fe2021-11-16 18:42:12 -07001290 if (HlslDxPositionW)
1291 shader->setDxPositionW(true);
1292
John Kessenich4be4aeb2017-06-23 10:50:22 -06001293 // Set up the environment, some subsettings take precedence over earlier
1294 // ways of setting things.
1295 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001296 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1297 : glslang::EShSourceGlsl,
1298 compUnit.stage, Client, ClientInputSemanticsVersion);
1299 shader->setEnvClient(Client, ClientVersion);
1300 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001301#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001302 if (targetHlslFunctionality1)
1303 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001304#endif
greg-lunarg4e064ee2021-03-15 11:26:11 -06001305 if (VulkanRulesRelaxed)
1306 shader->setEnvInputVulkanRulesRelaxed();
John Kessenich4be4aeb2017-06-23 10:50:22 -06001307 }
1308
John Kessenich69f4b512013-09-04 21:19:27 +00001309 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001310
John Kessenich4be4aeb2017-06-23 10:50:22 -06001311 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001312
John Kessenichdeec1932019-08-13 08:00:30 -06001313#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001314 if (Options & EOptionOutputPreprocessed) {
1315 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001316 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001317 PutsIfNonEmpty(str.c_str());
1318 } else {
1319 CompileFailed = true;
1320 }
1321 StderrIfNonEmpty(shader->getInfoLog());
1322 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001323 continue;
1324 }
John Kessenichdeec1932019-08-13 08:00:30 -06001325#endif
John Kessenich086febc2018-10-25 12:43:02 -06001326
John Kessenich3494b4d2017-05-22 15:00:42 -06001327 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001328 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001329
John Kessenich69f4b512013-09-04 21:19:27 +00001330 program.addShader(shader);
1331
John Kessenichc57b2a92016-01-16 15:30:03 -07001332 if (! (Options & EOptionSuppressInfolog) &&
1333 ! (Options & EOptionMemoryLeakMode)) {
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001334 if (!beQuiet)
1335 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001336 PutsIfNonEmpty(shader->getInfoLog());
1337 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001338 }
John Kessenich69f4b512013-09-04 21:19:27 +00001339 }
1340
1341 //
1342 // Program-level processing...
1343 //
1344
John Kessenich4d65ee32016-03-12 18:17:47 -07001345 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001346 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001347 LinkFailed = true;
1348
John Kessenichb6d3ee52019-08-06 02:20:45 -06001349#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001350 // Map IO
1351 if (Options & EOptionSpv) {
1352 if (!program.mapIO())
1353 LinkFailed = true;
1354 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001355#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001356
John Kessenich4d65ee32016-03-12 18:17:47 -07001357 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001358 if (! (Options & EOptionSuppressInfolog) &&
1359 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001360 PutsIfNonEmpty(program.getInfoLog());
1361 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001362 }
1363
John Kessenichb6d3ee52019-08-06 02:20:45 -06001364#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001365 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001366 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001367 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001368 program.dumpReflection();
1369 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001370#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001371
Georg Lehmannf0502092021-04-20 00:22:15 +02001372 std::vector<std::string> outputFiles;
1373
John Kessenich4d65ee32016-03-12 18:17:47 -07001374 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001375 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001376 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001377 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001378 else {
1379 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001380 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001381 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001382 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001383 glslang::SpvOptions spvOptions;
1384 if (Options & EOptionDebug)
1385 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001386 else if (stripDebugInfo)
1387 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001388 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1389 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001390 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001391 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001392 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001393
1394 // Dump the spv to a file or stdout, etc., but only if not doing
1395 // memory/perf testing, as it's not internal to programmatic use.
1396 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001397 printf("%s", logger.getAllMessages().c_str());
1398 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001399 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001400 } else {
1401 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1402 }
Georg Lehmannf0502092021-04-20 00:22:15 +02001403
1404 outputFiles.push_back(GetBinaryName((EShLanguage)stage));
John Kessenich23d27752019-07-28 02:12:10 -06001405#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001406 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001407 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001408#endif
John Kessenichacba7722015-03-04 03:48:38 +00001409 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001410 }
John Kessenich92f90382014-07-28 04:21:04 +00001411 }
1412 }
1413 }
1414
Georg Lehmannf0502092021-04-20 00:22:15 +02001415 if (depencyFileName && !(CompileFailed || LinkFailed)) {
1416 std::set<std::string> includedFiles = includer.getIncludedFiles();
1417 sources.insert(sources.end(), includedFiles.begin(), includedFiles.end());
1418
1419 writeDepFile(depencyFileName, outputFiles, sources);
1420 }
1421
John Kessenich5b0f13a2013-11-01 03:08:40 +00001422 // Free everything up, program has to go before the shaders
1423 // because it might have merged stuff from the shaders, and
1424 // the stuff from the shaders has to have its destructors called
1425 // before the pools holding the memory in the shaders is freed.
1426 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001427 while (shaders.size() > 0) {
1428 delete shaders.back();
1429 shaders.pop_back();
1430 }
John Kessenich69f4b512013-09-04 21:19:27 +00001431}
1432
John Kessenichc57b2a92016-01-16 15:30:03 -07001433//
1434// Do file IO part of compile and link, handing off the pure
1435// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1436// be put in a loop for testing memory footprint and performance.
1437//
1438// This is just for linking mode: meaning all the shaders will be put into the
1439// the same program linked together.
1440//
1441// This means there are a limited number of work items (not multi-threading mode)
1442// and that the point is testing at the linking level. Hence, to enable
1443// performance and memory testing, the actual compile/link can be put in
1444// a loop, independent of processing the work items and file IO.
1445//
Juan Lopeza558b262017-04-02 23:04:00 +02001446void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001447{
1448 std::vector<ShaderCompUnit> compUnits;
1449
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001450 // If this is using stdin, we can't really detect multiple different file
1451 // units by input type. We need to assume that we're just being given one
1452 // file of a certain type.
1453 if ((Options & EOptionStdin) != 0) {
1454 ShaderCompUnit compUnit(FindLanguage("stdin"));
1455 std::istreambuf_iterator<char> begin(std::cin), end;
1456 std::string tempString(begin, end);
1457 char* fileText = strdup(tempString.c_str());
1458 std::string fileName = "stdin";
1459 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001460 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001461 } else {
1462 // Transfer all the work items from to a simple list of
1463 // of compilation units. (We don't care about the thread
1464 // work-item distribution properties in this path, which
1465 // is okay due to the limited number of shaders, know since
1466 // they are all getting linked together.)
1467 glslang::TWorkItem* workItem;
1468 while (Worklist.remove(workItem)) {
1469 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1470 char* fileText = ReadFileData(workItem->name.c_str());
1471 if (fileText == nullptr)
1472 usage();
1473 compUnit.addString(workItem->name, fileText);
1474 compUnits.push_back(compUnit);
1475 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001476 }
1477
1478 // Actual call to programmatic processing of compile and link,
1479 // in a loop for testing memory and performance. This part contains
1480 // all the perf/memory that a programmatic consumer will care about.
1481 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1482 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1483 CompileAndLinkShaderUnits(compUnits);
1484
1485 if (Options & EOptionMemoryLeakMode)
1486 glslang::OS_DumpMemoryCounters();
1487 }
1488
John Kessenicha9313662017-06-15 10:40:49 -06001489 // free memory from ReadFileData, which got stored in a const char*
1490 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001491 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001492 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001493}
1494
John Kessenich94f28eb2017-11-13 01:32:06 -07001495int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001496{
Juan Lopeza558b262017-04-02 23:04:00 +02001497 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001498 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001499 assert(item);
1500 workList.add(item.get());
1501 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001502
John Kessenich23d27752019-07-28 02:12:10 -06001503#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001504 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001505 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001506 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001507 return ESuccess;
1508 }
John Kessenich23d27752019-07-28 02:12:10 -06001509#endif
John Kessenich05a70632013-09-17 19:26:08 +00001510
John Kessenichc6c80a62018-03-05 22:23:17 -07001511 if (Options & EOptionDumpBareVersion) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001512 printf("%d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1513 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenichc6c80a62018-03-05 22:23:17 -07001514 if (workList.empty())
1515 return ESuccess;
1516 } else if (Options & EOptionDumpVersions) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001517 printf("Glslang Version: %d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR,
1518 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenich319de232013-12-04 04:43:40 +00001519 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1520 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001521 std::string spirvVersion;
1522 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001523 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001524 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001525 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001526 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001527 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1528 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001529 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001530 return ESuccess;
1531 }
1532
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001533 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001534 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001535 }
1536
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001537 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001538 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1539 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001540 }
1541
John Kessenich05a70632013-09-17 19:26:08 +00001542 ProcessConfigFile();
1543
John Kessenich086febc2018-10-25 12:43:02 -06001544 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001545 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001546
John Kessenich69f4b512013-09-04 21:19:27 +00001547 //
1548 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001549 // 1) linking all arguments together, single-threaded, new C++ interface
1550 // 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 +00001551 //
John Kessenich086febc2018-10-25 12:43:02 -06001552 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001553 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001554 glslang::InitializeProcess(); // also test reference counting of users
1555 glslang::InitializeProcess(); // also test reference counting of users
1556 glslang::FinalizeProcess(); // also test reference counting of users
1557 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001558 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001559 glslang::FinalizeProcess();
1560 } else {
1561 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001562 ShInitialize(); // also test reference counting of users
1563 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001564
Juan Lopeza558b262017-04-02 23:04:00 +02001565 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001566
John Kesseniche2c15b42017-11-16 22:48:41 -07001567 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001568 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001569 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001570 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001571 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001572 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001573 return EFailThreadCreate;
1574 }
John Kessenicha0af4732012-12-12 21:15:54 +00001575 }
Juan Lopeza558b262017-04-02 23:04:00 +02001576
1577 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001578 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001579 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001580
1581 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001582 for (size_t w = 0; w < WorkItems.size(); ++w) {
1583 if (WorkItems[w]) {
1584 if (printShaderNames || WorkItems[w]->results.size() > 0)
1585 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1586 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001587 }
1588 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001589
1590 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001591 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001592
John Kessenichc999ba22013-11-07 23:33:24 +00001593 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001594 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001595 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001596 return EFailLink;
1597
1598 return 0;
1599}
1600
John Kessenich94f28eb2017-11-13 01:32:06 -07001601int C_DECL main(int argc, char* argv[])
1602{
1603 ProcessArguments(WorkItems, argc, argv);
1604
1605 int ret = 0;
1606
1607 // Loop over the entire init/finalize cycle to watch memory changes
1608 const int iterations = 1;
1609 if (iterations > 1)
1610 glslang::OS_DumpMemoryCounters();
1611 for (int i = 0; i < iterations; ++i) {
1612 ret = singleMain();
1613 if (iterations > 1)
1614 glslang::OS_DumpMemoryCounters();
1615 }
1616
1617 return ret;
1618}
1619
John Kessenicha0af4732012-12-12 21:15:54 +00001620//
1621// Deduce the language from the filename. Files must end in one of the
1622// following extensions:
1623//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001624// .vert = vertex
1625// .tesc = tessellation control
1626// .tese = tessellation evaluation
1627// .geom = geometry
1628// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001629// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001630// .rgen = ray generation
1631// .rint = ray intersection
1632// .rahit = ray any hit
1633// .rchit = ray closest hit
1634// .rmiss = ray miss
1635// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001636// .mesh = mesh
1637// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001638// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1639// where <stage> is one of the stages listed above.
1640//
1641EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001642{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001643 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001644 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001645 stageName = shaderStageName;
1646 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001647 // Note: "first" extension means "first from the end", i.e.
1648 // if the file is named foo.vert.glsl, then "glsl" is first,
1649 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001650 size_t firstExtStart = name.find_last_of(".");
1651 bool hasFirstExt = firstExtStart != std::string::npos;
1652 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1653 bool hasSecondExt = secondExtStart != std::string::npos;
1654 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1655 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001656 if (usesUnifiedExt && firstExt == "hlsl")
1657 Options |= EOptionReadHlsl;
1658 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001659 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001660 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001661 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001662 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001663 usage();
1664 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001665 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001666 } else
1667 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001668
John Kessenichfccbb8b2018-04-17 17:24:03 -06001669 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001670 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001671 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001672 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001673 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001674 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001675 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001676 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001677 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001678 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001679 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001680 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001681 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001682 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001683 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001684 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001685 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001686 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001687 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001688 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001689 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001690 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001691 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001692 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001693 else if (stageName == "mesh")
1694 return EShLangMeshNV;
1695 else if (stageName == "task")
1696 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001697
1698 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001699 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001700}
1701
John Kessenicha0af4732012-12-12 21:15:54 +00001702//
John Kessenichecba76f2017-01-06 00:34:48 -07001703// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001704// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001705//
John Kessenich51cdd902014-02-18 23:37:57 +00001706void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001707{
John Kessenichca3457f2015-05-18 01:59:45 +00001708 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001709 char* shaderString;
1710 if ((Options & EOptionStdin) != 0) {
1711 std::istreambuf_iterator<char> begin(std::cin), end;
1712 std::string tempString(begin, end);
1713 shaderString = strdup(tempString.c_str());
1714 } else {
1715 shaderString = ReadFileData(fileName);
1716 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001717
1718 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001719 int* lengths = new int[1];
1720 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001721
John Kessenich52ac67e2013-05-05 23:46:22 +00001722 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001723 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001724
John Kessenicha9313662017-06-15 10:40:49 -06001725 if (UserPreamble.isSet())
1726 Error("-D and -U options require -l (linking)\n");
1727
John Kessenich94a81fb2013-08-31 02:41:30 +00001728 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1729 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001730 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001731 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001732 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001733 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001734 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001735 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1736 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001737 }
John Kessenicha0af4732012-12-12 21:15:54 +00001738
John Kessenich94a81fb2013-08-31 02:41:30 +00001739 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001740 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001741 }
John Kessenicha0af4732012-12-12 21:15:54 +00001742
John Kessenich41cf6b52013-06-25 18:10:05 +00001743 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001744 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001745
John Kessenichc999ba22013-11-07 23:33:24 +00001746 if (ret == 0)
1747 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001748}
1749
John Kessenicha0af4732012-12-12 21:15:54 +00001750//
1751// print usage to stdout
1752//
1753void usage()
1754{
John Kessenich319de232013-12-04 04:43:40 +00001755 printf("Usage: glslangValidator [option]... [file]...\n"
1756 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001757 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1758 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001759 " (see -c option below for generating a template)\n"
1760 " .vert for a vertex shader\n"
1761 " .tesc for a tessellation control shader\n"
1762 " .tese for a tessellation evaluation shader\n"
1763 " .geom for a geometry shader\n"
1764 " .frag for a fragment shader\n"
1765 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001766 " .mesh for a mesh shader\n"
1767 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001768 " .rgen for a ray generation shader\n"
1769 " .rint for a ray intersection shader\n"
1770 " .rahit for a ray any hit shader\n"
1771 " .rchit for a ray closest hit shader\n"
1772 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001773 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001774 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1775 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001776 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001777 "Options:\n"
1778 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001779 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001780 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1781 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001782 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001783 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001784 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -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"
John Kessenichade8bbb2018-08-12 21:24:55 -06001787 " which will appear in #define GL_SPIRV ver;\n"
1788 " '--client opengl100' is the same as -G100;\n"
Jeremy Hayesb73afa82021-01-05 15:54:41 -07001789 " a '--target-env' for OpenGL will also imply '-G';\n"
1790 " currently only supports GLSL\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001791 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001792 " -I<dir> add dir to the include search path; includer's directory\n"
1793 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001794 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1795 " -Os optimizes SPIR-V to minimize size\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001796 " -R use relaxed verification rules for generating Vulkan SPIR-V,\n"
1797 " allowing the use of default uniforms, atomic_uints, and\n"
1798 " gl_VertexID and gl_InstanceID keywords.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001799 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001800 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001801 " -U<name> | --undef-macro <name> | --U <name>\n"
1802 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001803 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001804 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001805 " 'ver', when present, is the version of the input semantics,\n"
1806 " which will appear in #define VULKAN ver\n"
1807 " '--client vulkan100' is the same as -V100\n"
1808 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001809 " -c configuration dump;\n"
1810 " creates the default configuration file (redirect to a .conf file)\n"
1811 " -d default to desktop (#version 110) when there is no shader #version\n"
1812 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001813 " -e <name> | --entry-point <name>\n"
1814 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001815 " -f{hlsl_functionality1}\n"
1816 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001817 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001818 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001819 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001820 " -h print this usage message\n"
1821 " -i intermediate tree (glslang AST) is printed out\n"
1822 " -l link all input files together to form a single module\n"
1823 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001824 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001825 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001826 " -r | --relaxed-errors"
1827 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001828 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001829 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001830 " -v | --version\n"
1831 " print version strings\n"
1832 " -w | --suppress-warnings\n"
1833 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001834 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001835 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001836 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001837 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1838 " without explicit bindings\n"
1839 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1840 " 'location' (fragile, not cross stage)\n"
Robin Quinte50b0a82021-04-22 13:13:38 +02001841 " --auto-sampled-textures Removes sampler variables and converts\n"
Robin Quint4bf373e2021-04-27 16:06:06 +02001842 " existing textures to sampled textures\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001843 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Georg Lehmannf0502092021-04-20 00:22:15 +02001844 " --depfile <file> writes depfile for build systems\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001845 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001846 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1847 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1848 " scalars\n"
1849 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1850 " works independently of source language\n"
1851 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1852 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001853 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001854 " texture/sampler combo like DirectX9 would,\n"
1855 " and recognizes DirectX9-specific semantics\n"
Greg Fischere9564fe2021-11-16 18:42:12 -07001856 " --hlsl-dx-position-w W component of SV_Position in HLSL fragment\n"
1857 " shaders compatible with DirectX\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001858 " --invert-y | --iy invert position.Y output in vertex shader\n"
1859 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001860 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001861 " --no-storage-format | --nsf use Unknown image format\n"
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001862 " --quiet do not print anything to stdout, unless\n"
1863 " requested by another option\n"
baldurk4513df92019-02-08 10:48:48 +00001864 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1865 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001866 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001867 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1868 " shaders rather than just vertex/fragment\n"
1869 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1870 " separately to uniforms\n"
1871 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1872 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001873 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1874 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001875 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001876 " set descriptor set and binding for\n"
1877 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001878 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001879 " set descriptor set for all resources\n"
1880 " --rsb synonym for --resource-set-binding\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001881 " --set-block-backing name {uniform|buffer|push_constant}\n"
1882 " changes the backing type of a uniform, buffer,\n"
1883 " or push_constant block declared in\n"
1884 " in the program, when using -R option.\n"
1885 " This can be used to change the backing\n"
1886 " for existing blocks as well as implicit ones\n"
1887 " such as 'gl_DefaultUniformBlock'.\n"
1888 " --sbs synonym for set-block-storage\n"
1889 " --set-atomic-counter-block name set\n"
1890 " set name, and descriptor set for\n"
1891 " atomic counter blocks, with -R opt\n"
1892 " --sacb synonym for set-atomic-counter-block\n"
1893 " --set-default-uniform-block name set binding\n"
1894 " set name, descriptor set, and binding for\n"
1895 " global default-uniform-block, with -R opt\n"
1896 " --sdub synonym for set-default-uniform-block\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001897 " --shift-image-binding [stage] num\n"
1898 " base binding number for images (uav)\n"
1899 " --shift-image-binding [stage] [num set]...\n"
1900 " per-descriptor-set shift values\n"
1901 " --sib synonym for --shift-image-binding\n"
1902 " --shift-sampler-binding [stage] num\n"
1903 " base binding number for samplers\n"
1904 " --shift-sampler-binding [stage] [num set]...\n"
1905 " per-descriptor-set shift values\n"
1906 " --ssb synonym for --shift-sampler-binding\n"
1907 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1908 " --shift-ssbo-binding [stage] [num set]...\n"
1909 " per-descriptor-set shift values\n"
1910 " --sbb synonym for --shift-ssbo-binding\n"
1911 " --shift-texture-binding [stage] num\n"
1912 " base binding number for textures\n"
1913 " --shift-texture-binding [stage] [num set]...\n"
1914 " per-descriptor-set shift values\n"
1915 " --stb synonym for --shift-texture-binding\n"
1916 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1917 " --shift-uav-binding [stage] [num set]...\n"
1918 " per-descriptor-set shift values\n"
1919 " --suavb synonym for --shift-uav-binding\n"
1920 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1921 " --shift-UBO-binding [stage] [num set]...\n"
1922 " per-descriptor-set shift values\n"
1923 " --sub synonym for --shift-UBO-binding\n"
1924 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1925 " --spirv-dis output standard-form disassembly; works only\n"
1926 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001927 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001928 " --source-entrypoint <name> the given shader source function is\n"
1929 " renamed to be the <name> given in -e\n"
1930 " --sep synonym for --source-entrypoint\n"
1931 " --stdin read from stdin instead of from a file;\n"
1932 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001933 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001934 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001935 " Set the execution environment that the\n"
1936 " generated code will be executed in.\n"
1937 " Defaults to:\n"
1938 " * vulkan1.0 under --client vulkan<ver>\n"
1939 " * opengl under --client opengl<ver>\n"
1940 " * spirv1.0 under --target-env vulkan1.0\n"
1941 " * spirv1.3 under --target-env vulkan1.1\n"
1942 " * spirv1.5 under --target-env vulkan1.2\n"
1943 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001944 " --variable-name <name>\n"
1945 " --vn <name> creates a C header file that contains a\n"
1946 " uint32_t array named <name>\n"
1947 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001948 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001949
1950 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001951}
1952
John Kessenich3ce4e592014-10-06 19:57:34 +00001953#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001954
1955#include <errno.h>
1956
1957int fopen_s(
1958 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001959 const char* filename,
1960 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001961)
1962{
1963 if (!pFile || !filename || !mode) {
1964 return EINVAL;
1965 }
1966
1967 FILE* f = fopen(filename, mode);
1968 if (! f) {
1969 if (errno != 0) {
1970 return errno;
1971 } else {
1972 return ENOENT;
1973 }
1974 }
1975 *pFile = f;
1976
1977 return 0;
1978}
1979
1980#endif
1981
John Kessenicha0af4732012-12-12 21:15:54 +00001982//
1983// Malloc a string of sufficient size and read a string into it.
1984//
John Kessenich04acb1b2017-06-14 17:36:50 -06001985char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001986{
John Kessenichb3297152015-07-11 18:01:03 -06001987 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001988 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001989 if (errorCode || in == nullptr)
1990 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001991
John Kessenich04acb1b2017-06-14 17:36:50 -06001992 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001993 while (fgetc(in) != EOF)
1994 count++;
1995
John Kessenichd6c72a42014-08-18 19:42:35 +00001996 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001997
John Kessenich04acb1b2017-06-14 17:36:50 -06001998 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1999 if ((int)fread(return_data, 1, count, in) != count) {
2000 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06002001 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00002002 }
John Kessenich68d78fd2015-07-12 19:28:10 -06002003
John Kessenich04acb1b2017-06-14 17:36:50 -06002004 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00002005 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06002006
John Kessenicha0af4732012-12-12 21:15:54 +00002007 return return_data;
2008}
2009
John Kessenich04acb1b2017-06-14 17:36:50 -06002010void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00002011{
John Kessenichb3297152015-07-11 18:01:03 -06002012 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00002013}
2014
John Kessenich54d8cda2013-02-11 22:36:01 +00002015void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00002016{
John Kessenichfae38ee2015-06-10 23:23:12 +00002017 if (num >= 0 )
2018 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
2019 else
2020 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00002021}