blob: 23e510c2815fa521a05d0b0289697bf8cd8c0030 [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;
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200180bool DumpBuiltinSymbols = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600181std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600182
183// Source environment
184// (source 'Client' is currently the same as target 'Client')
185int ClientInputSemanticsVersion = 100;
186
187// Target environment
188glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
189glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
190glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
191glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
192
John Kessenich66011cb2018-03-06 16:12:04 -0700193std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600194
LoopDawg08a14422017-10-17 19:27:14 -0600195// Per descriptor-set binding base data
196typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
197
Neil Roberts16f53472018-03-20 17:30:53 +0100198std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100199int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100200
LoopDawg08a14422017-10-17 19:27:14 -0600201std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
202std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900203std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600204
greg-lunarg4e064ee2021-03-15 11:26:11 -0600205std::vector<std::pair<std::string, glslang::TBlockStorageClass>> blockStorageOverrides;
206
207bool setGlobalUniformBlock = false;
208std::string globalUniformName;
209unsigned int globalUniformBinding;
210unsigned int globalUniformSet;
211
212bool setGlobalBufferBlock = false;
213std::string atomicCounterBlockName;
214unsigned int atomicCounterBlockSet;
215
John Kessenicha9313662017-06-15 10:40:49 -0600216// Add things like "#define ..." to a preamble to use in the beginning of the shader.
217class TPreamble {
218public:
219 TPreamble() { }
220
221 bool isSet() const { return text.size() > 0; }
222 const char* get() const { return text.c_str(); }
223
224 // #define...
225 void addDef(std::string def)
226 {
227 text.append("#define ");
228 fixLine(def);
229
John Kessenich6f988922020-01-07 07:03:11 -0700230 Processes.push_back("define-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600231 Processes.back().append(def);
232
John Kessenicha9313662017-06-15 10:40:49 -0600233 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600234 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600235 if (equal != def.npos)
236 def[equal] = ' ';
237
238 text.append(def);
239 text.append("\n");
240 }
241
242 // #undef...
243 void addUndef(std::string undef)
244 {
245 text.append("#undef ");
246 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600247
John Kessenich6f988922020-01-07 07:03:11 -0700248 Processes.push_back("undef-macro ");
John Kessenich2a271162017-07-20 20:00:36 -0600249 Processes.back().append(undef);
250
John Kessenicha9313662017-06-15 10:40:49 -0600251 text.append(undef);
252 text.append("\n");
253 }
254
255protected:
256 void fixLine(std::string& line)
257 {
258 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600259 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600260 if (end != line.npos)
261 line = line.substr(0, end);
262 }
263
264 std::string text; // contents of preamble
265};
266
John Kessenich83855b92020-03-30 00:27:31 -0600267// Track the user's #define and #undef from the command line.
John Kessenicha9313662017-06-15 10:40:49 -0600268TPreamble UserPreamble;
amhagan035a3bb2021-09-01 11:33:21 -0400269std::string PreambleString;
John Kessenicha9313662017-06-15 10:40:49 -0600270
John Kessenich68d78fd2015-07-12 19:28:10 -0600271//
272// Create the default name for saving a binary if -o is not provided.
273//
274const char* GetBinaryName(EShLanguage stage)
275{
276 const char* name;
277 if (binaryFileName == nullptr) {
278 switch (stage) {
279 case EShLangVertex: name = "vert.spv"; break;
280 case EShLangTessControl: name = "tesc.spv"; break;
281 case EShLangTessEvaluation: name = "tese.spv"; break;
282 case EShLangGeometry: name = "geom.spv"; break;
283 case EShLangFragment: name = "frag.spv"; break;
284 case EShLangCompute: name = "comp.spv"; break;
Daniel Kochdb32b242020-03-17 20:42:47 -0400285 case EShLangRayGen: name = "rgen.spv"; break;
286 case EShLangIntersect: name = "rint.spv"; break;
287 case EShLangAnyHit: name = "rahit.spv"; break;
288 case EShLangClosestHit: name = "rchit.spv"; break;
289 case EShLangMiss: name = "rmiss.spv"; break;
290 case EShLangCallable: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700291 case EShLangMeshNV: name = "mesh.spv"; break;
292 case EShLangTaskNV: name = "task.spv"; break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600293 default: name = "unknown"; break;
294 }
295 } else
296 name = binaryFileName;
297
298 return name;
299}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000300
John Kessenich05a70632013-09-17 19:26:08 +0000301//
302// *.conf => this is a config file that can set limits/resources
303//
304bool SetConfigFile(const std::string& name)
305{
306 if (name.size() < 5)
307 return false;
308
John Kessenich4c706852013-10-11 16:28:43 +0000309 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000310 ConfigFile = name;
311 return true;
312 }
313
314 return false;
315}
316
John Kessenich68d78fd2015-07-12 19:28:10 -0600317//
318// Give error and exit with failure code.
319//
John Kessenichbd97b6f2019-12-20 10:33:13 -0700320void Error(const char* message, const char* detail = nullptr)
John Kessenich68d78fd2015-07-12 19:28:10 -0600321{
John Kessenichbd97b6f2019-12-20 10:33:13 -0700322 fprintf(stderr, "%s: Error: ", ExecutableName);
323 if (detail != nullptr)
324 fprintf(stderr, "%s: ", detail);
325 fprintf(stderr, "%s (use -h for usage)\n", message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600326 exit(EFailUsage);
327}
328
329//
LoopDawg08a14422017-10-17 19:27:14 -0600330// Process an optional binding base of one the forms:
331// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600332// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600333
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600334// Where stage is one of the forms accepted by FindLanguage, and base is an integer
335//
LoopDawg08a14422017-10-17 19:27:14 -0600336void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600337{
338 if (argc < 2)
339 usage();
340
LoopDawg08a14422017-10-17 19:27:14 -0600341 EShLanguage lang = EShLangCount;
342 int singleBase = 0;
343 TPerSetBaseBinding perSetBase;
344 int arg = 1;
345
346 // Parse stage, if given
347 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600348 if (argc < 3) // this form needs one more argument
349 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700350
LoopDawg08a14422017-10-17 19:27:14 -0600351 lang = FindLanguage(argv[arg++], false);
352 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600353
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800354 if ((argc - arg) >= 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600355 // Parse a per-set binding base
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800356 do {
LoopDawg08a14422017-10-17 19:27:14 -0600357 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600358 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600359 perSetBase[setNum] = baseNum;
Yun Hsiao Wuae4305d2021-07-26 16:21:12 +0800360 } while ((argc - arg) >= 2 && isdigit(argv[arg + 0][0]) && isdigit(argv[arg + 1][0]));
LoopDawg08a14422017-10-17 19:27:14 -0600361 } else {
362 // Parse single binding base
363 singleBase = atoi(argv[arg++]);
364 }
365
366 argc -= (arg-1);
367 argv += (arg-1);
368
369 // Set one or all languages
370 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
371 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
372
373 for (int lang = langMin; lang < langMax; ++lang) {
374 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600375 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600376 else
377 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600378 }
379}
380
Hyangran Park36dc8292017-05-02 16:27:29 +0900381void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
382{
383 if (argc < 2)
384 usage();
385
386 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600387 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900388 usage();
389
LoopDawg52017192017-07-14 15:15:47 -0600390 // Parse form: --argname stage [regname set base...], or:
391 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900392 const EShLanguage lang = FindLanguage(argv[1], false);
393
LoopDawg52017192017-07-14 15:15:47 -0600394 argc--;
395 argv++;
396
397 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
398 base[lang].push_back(argv[1]);
399
400 argc--;
401 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900402 }
LoopDawg52017192017-07-14 15:15:47 -0600403
404 // Must have one arg, or a multiple of three (for [regname set binding] triples)
405 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
406 usage();
407
Hyangran Park36dc8292017-05-02 16:27:29 +0900408 } else {
LoopDawg52017192017-07-14 15:15:47 -0600409 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900410 for (int lang=0; lang<EShLangCount; ++lang)
411 base[lang].push_back(argv[1]);
412
413 argc--;
414 argv++;
415 }
416}
417
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600418//
greg-lunarg4e064ee2021-03-15 11:26:11 -0600419// Process an optional binding base of one the forms:
420// --argname name {uniform|buffer|push_constant}
421void ProcessBlockStorage(int& argc, char**& argv, std::vector<std::pair<std::string, glslang::TBlockStorageClass>>& storage)
422{
423 if (argc < 3)
424 usage();
425
426 glslang::TBlockStorageClass blockStorage = glslang::EbsNone;
427
428 std::string strBacking(argv[2]);
429 if (strBacking == "uniform")
430 blockStorage = glslang::EbsUniform;
431 else if (strBacking == "buffer")
432 blockStorage = glslang::EbsStorageBuffer;
433 else if (strBacking == "push_constant")
434 blockStorage = glslang::EbsPushConstant;
435 else {
436 printf("%s: invalid block storage\n", strBacking.c_str());
437 usage();
438 }
439
440 storage.push_back(std::make_pair(std::string(argv[1]), blockStorage));
441
442 argc -= 2;
443 argv += 2;
444}
445
446inline bool isNonDigit(char c) {
447 // a non-digit character valid in a glsl identifier
448 return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
449}
450
451// whether string isa valid identifier to be used in glsl
452bool isValidIdentifier(const char* str) {
453 std::string idn(str);
454
455 if (idn.length() == 0) {
456 return false;
457 }
458
459 if (idn.length() >= 3 && idn.substr(0, 3) == "gl_") {
460 // identifiers startin with "gl_" are reserved
461 return false;
462 }
463
464 if (!isNonDigit(idn[0])) {
465 return false;
466 }
467
468 for (unsigned int i = 1; i < idn.length(); ++i) {
469 if (!(isdigit(idn[i]) || isNonDigit(idn[i]))) {
470 return false;
471 }
472 }
473
474 return true;
475}
476
477// Process settings for either the global buffer block or global unfirom block
478// of the form:
479// --argname name set binding
480void ProcessGlobalBlockSettings(int& argc, char**& argv, std::string* name, unsigned int* set, unsigned int* binding)
481{
482 if (argc < 4)
483 usage();
484
485 unsigned int curArg = 1;
486
487 assert(name || set || binding);
488
489 if (name) {
490 if (!isValidIdentifier(argv[curArg])) {
491 printf("%s: invalid identifier\n", argv[curArg]);
492 usage();
493 }
494 *name = argv[curArg];
495
496 curArg++;
497 }
498
499 if (set) {
500 errno = 0;
501 int setVal = ::strtol(argv[curArg], NULL, 10);
502 if (errno || setVal < 0) {
503 printf("%s: invalid set\n", argv[curArg]);
504 usage();
505 }
506 *set = setVal;
507
508 curArg++;
509 }
510
511 if (binding) {
512 errno = 0;
513 int bindingVal = ::strtol(argv[curArg], NULL, 10);
514 if (errno || bindingVal < 0) {
515 printf("%s: invalid binding\n", argv[curArg]);
516 usage();
517 }
518 *binding = bindingVal;
519
520 curArg++;
521 }
522
523 argc -= (curArg - 1);
524 argv += (curArg - 1);
525}
526
527//
John Kessenich68d78fd2015-07-12 19:28:10 -0600528// Do all command-line argument parsing. This includes building up the work-items
529// to be processed later, and saving all the command-line options.
530//
531// Does not return (it exits) if command-line is fatally flawed.
532//
Juan Lopeza558b262017-04-02 23:04:00 +0200533void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000534{
LoopDawg08a14422017-10-17 19:27:14 -0600535 for (int res = 0; res < glslang::EResCount; ++res)
536 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600537
John Kessenich38f3b892013-09-06 19:52:57 +0000538 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200539 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000540
John Kessenichc178f0a2017-06-23 10:58:31 -0600541 const auto bumpArg = [&]() {
542 if (argc > 0) {
543 argc--;
544 argv++;
545 }
546 };
547
548 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600549 const auto getStringOperand = [&](const char* desc) {
550 if (argv[0][2] == 0) {
551 printf("%s must immediately follow option (no spaces)\n", desc);
552 exit(EFailUsage);
553 }
554 return argv[0] + 2;
555 };
556
John Kessenich6353d552017-06-23 11:11:09 -0600557 // read a number attached to a single-letter option
558 const auto getAttachedNumber = [&](const char* desc) {
559 int num = atoi(argv[0] + 2);
560 if (num == 0) {
561 printf("%s: expected attached non-0 number\n", desc);
562 exit(EFailUsage);
563 }
564 return num;
565 };
566
567 // minimum needed (without overriding something else) to target Vulkan SPIR-V
568 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600569 if (Client == glslang::EShClientNone)
570 ClientVersion = glslang::EShTargetVulkan_1_0;
571 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600572 Options |= EOptionSpv;
573 Options |= EOptionVulkanRules;
574 Options |= EOptionLinkProgram;
575 };
576
577 // minimum needed (without overriding something else) to target OpenGL SPIR-V
578 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600579 if (Client == glslang::EShClientNone)
580 ClientVersion = glslang::EShTargetOpenGL_450;
581 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600582 Options |= EOptionSpv;
583 Options |= EOptionLinkProgram;
584 // undo a -H default to Vulkan
585 Options &= ~EOptionVulkanRules;
586 };
587
Neil Roberts16f53472018-03-20 17:30:53 +0100588 const auto getUniformOverride = [getStringOperand]() {
589 const char *arg = getStringOperand("-u<name>:<location>");
590 const char *split = strchr(arg, ':');
591 if (split == NULL) {
592 printf("%s: missing location\n", arg);
593 exit(EFailUsage);
594 }
595 errno = 0;
596 int location = ::strtol(split + 1, NULL, 10);
597 if (errno) {
598 printf("%s: invalid location\n", arg);
599 exit(EFailUsage);
600 }
601 return std::make_pair(std::string(arg, split - arg), location);
602 };
603
John Kessenichc178f0a2017-06-23 10:58:31 -0600604 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000605 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000606 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600607 case '-':
608 {
609 std::string lowerword(argv[0]+2);
610 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
611
612 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600613 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600614 lowerword == "auto-map-binding" ||
615 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600616 Options |= EOptionAutoMapBindings;
617 } else if (lowerword == "auto-map-locations" || // synonyms
618 lowerword == "aml") {
619 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100620 } else if (lowerword == "uniform-base") {
621 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700622 Error("no <base> provided", lowerword.c_str());
Neil Robertsb0f3d792018-03-20 17:41:05 +0100623 uniformBase = ::strtol(argv[1], NULL, 10);
624 bumpArg();
625 break;
John Kessenich6353d552017-06-23 11:11:09 -0600626 } else if (lowerword == "client") {
627 if (argc > 1) {
628 if (strcmp(argv[1], "vulkan100") == 0)
629 setVulkanSpv();
630 else if (strcmp(argv[1], "opengl100") == 0)
631 setOpenGlSpv();
632 else
John Kessenichbd97b6f2019-12-20 10:33:13 -0700633 Error("expects vulkan100 or opengl100", lowerword.c_str());
634 } else
635 Error("expects vulkan100 or opengl100", lowerword.c_str());
John Kessenich6353d552017-06-23 11:11:09 -0600636 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700637 } else if (lowerword == "define-macro" ||
638 lowerword == "d") {
639 if (argc > 1)
640 UserPreamble.addDef(argv[1]);
641 else
642 Error("expects <name[=def]>", argv[0]);
643 bumpArg();
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +0200644 } else if (lowerword == "dump-builtin-symbols") {
645 DumpBuiltinSymbols = true;
John Kessenich640bd092018-08-09 14:15:00 -0600646 } else if (lowerword == "entry-point") {
647 entryPointName = argv[1];
648 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700649 Error("no <name> provided", lowerword.c_str());
John Kessenich640bd092018-08-09 14:15:00 -0600650 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600651 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
652 lowerword == "flatten-uniform-array" ||
653 lowerword == "fua") {
654 Options |= EOptionFlattenUniformArrays;
655 } else if (lowerword == "hlsl-offsets") {
656 Options |= EOptionHlslOffsets;
657 } else if (lowerword == "hlsl-iomap" ||
658 lowerword == "hlsl-iomapper" ||
659 lowerword == "hlsl-iomapping") {
660 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800661 } else if (lowerword == "hlsl-enable-16bit-types") {
662 HlslEnable16BitTypes = true;
John Kessenichbd1c1832018-12-07 18:38:26 -0700663 } else if (lowerword == "hlsl-dx9-compatible") {
664 HlslDX9compatible = true;
Greg Fischerebfca602021-04-21 16:49:53 -0600665 } else if (lowerword == "auto-sampled-textures") {
666 autoSampledTextures = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700667 } else if (lowerword == "invert-y" || // synonyms
668 lowerword == "iy") {
669 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600670 } else if (lowerword == "keep-uncalled" || // synonyms
671 lowerword == "ku") {
672 Options |= EOptionKeepUncalled;
John Kessenich605afc72019-06-17 23:33:09 -0600673 } else if (lowerword == "nan-clamp") {
674 NaNClamp = true;
John Kessenich6263fb12017-06-14 15:52:44 -0600675 } else if (lowerword == "no-storage-format" || // synonyms
676 lowerword == "nsf") {
677 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600678 } else if (lowerword == "relaxed-errors") {
679 Options |= EOptionRelaxedErrors;
baldurk15c37f72019-01-29 12:12:59 +0000680 } else if (lowerword == "reflect-strict-array-suffix") {
681 ReflectOptions |= EShReflectionStrictArraySuffix;
baldurkedf82122019-01-29 15:49:00 +0000682 } else if (lowerword == "reflect-basic-array-suffix") {
683 ReflectOptions |= EShReflectionBasicArraySuffix;
baldurk0af5e3e2019-01-29 16:04:44 +0000684 } else if (lowerword == "reflect-intermediate-io") {
685 ReflectOptions |= EShReflectionIntermediateIO;
baldurk657acc02019-01-30 14:18:43 +0000686 } else if (lowerword == "reflect-separate-buffers") {
687 ReflectOptions |= EShReflectionSeparateBuffers;
baldurka972e732019-01-30 15:34:02 +0000688 } else if (lowerword == "reflect-all-block-variables") {
689 ReflectOptions |= EShReflectionAllBlockVariables;
baldurk19050692019-02-11 11:50:24 +0000690 } else if (lowerword == "reflect-unwrap-io-blocks") {
691 ReflectOptions |= EShReflectionUnwrapIOBlocks;
Chow81112682020-06-04 15:47:18 +0800692 } else if (lowerword == "reflect-all-io-variables") {
693 ReflectOptions |= EShReflectionAllIOVariables;
694 } else if (lowerword == "reflect-shared-std140-ubo") {
695 ReflectOptions |= EShReflectionSharedStd140UBO;
696 } else if (lowerword == "reflect-shared-std140-ssbo") {
697 ReflectOptions |= EShReflectionSharedStd140SSBO;
John Kessenich6263fb12017-06-14 15:52:44 -0600698 } else if (lowerword == "resource-set-bindings" || // synonyms
699 lowerword == "resource-set-binding" ||
700 lowerword == "rsb") {
701 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
greg-lunarg4e064ee2021-03-15 11:26:11 -0600702 } else if (lowerword == "set-block-storage" ||
703 lowerword == "sbs") {
704 ProcessBlockStorage(argc, argv, blockStorageOverrides);
705 } else if (lowerword == "set-atomic-counter-block" ||
706 lowerword == "sacb") {
707 ProcessGlobalBlockSettings(argc, argv, &atomicCounterBlockName, &atomicCounterBlockSet, nullptr);
708 setGlobalBufferBlock = true;
709 } else if (lowerword == "set-default-uniform-block" ||
710 lowerword == "sdub") {
711 ProcessGlobalBlockSettings(argc, argv, &globalUniformName, &globalUniformSet, &globalUniformBinding);
712 setGlobalUniformBlock = true;
steve-lunarg9088be42016-11-01 10:31:42 -0600713 } else if (lowerword == "shift-image-bindings" || // synonyms
714 lowerword == "shift-image-binding" ||
715 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600716 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600717 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600718 lowerword == "shift-sampler-binding" ||
719 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600720 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600721 } else if (lowerword == "shift-uav-bindings" || // synonyms
722 lowerword == "shift-uav-binding" ||
723 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600724 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600725 } else if (lowerword == "shift-texture-bindings" || // synonyms
726 lowerword == "shift-texture-binding" ||
727 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600728 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600729 } else if (lowerword == "shift-ubo-bindings" || // synonyms
730 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600731 lowerword == "shift-cbuffer-bindings" ||
732 lowerword == "shift-cbuffer-binding" ||
733 lowerword == "sub" ||
734 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600735 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700736 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
737 lowerword == "shift-ssbo-binding" ||
738 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600739 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600740 } else if (lowerword == "source-entrypoint" || // synonyms
741 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600742 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700743 Error("no <entry-point> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600744 sourceEntryPointName = argv[1];
745 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600746 break;
John Kesseniche2156222018-06-11 18:12:15 -0600747 } else if (lowerword == "spirv-dis") {
748 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600749 } else if (lowerword == "spirv-val") {
750 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200751 } else if (lowerword == "stdin") {
752 Options |= EOptionStdin;
753 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600754 } else if (lowerword == "suppress-warnings") {
755 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600756 } else if (lowerword == "target-env") {
757 if (argc > 1) {
758 if (strcmp(argv[1], "vulkan1.0") == 0) {
759 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600760 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700761 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
762 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600763 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich273d3a52020-01-15 00:10:41 -0700764 } else if (strcmp(argv[1], "vulkan1.2") == 0) {
765 setVulkanSpv();
766 ClientVersion = glslang::EShTargetVulkan_1_2;
John Kessenich6353d552017-06-23 11:11:09 -0600767 } else if (strcmp(argv[1], "opengl") == 0) {
768 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600769 ClientVersion = glslang::EShTargetOpenGL_450;
770 } else if (strcmp(argv[1], "spirv1.0") == 0) {
771 TargetLanguage = glslang::EShTargetSpv;
772 TargetVersion = glslang::EShTargetSpv_1_0;
773 } else if (strcmp(argv[1], "spirv1.1") == 0) {
774 TargetLanguage = glslang::EShTargetSpv;
775 TargetVersion = glslang::EShTargetSpv_1_1;
776 } else if (strcmp(argv[1], "spirv1.2") == 0) {
777 TargetLanguage = glslang::EShTargetSpv;
778 TargetVersion = glslang::EShTargetSpv_1_2;
779 } else if (strcmp(argv[1], "spirv1.3") == 0) {
780 TargetLanguage = glslang::EShTargetSpv;
781 TargetVersion = glslang::EShTargetSpv_1_3;
782 } else if (strcmp(argv[1], "spirv1.4") == 0) {
783 TargetLanguage = glslang::EShTargetSpv;
784 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich8317e6c2019-08-18 23:58:08 -0600785 } else if (strcmp(argv[1], "spirv1.5") == 0) {
786 TargetLanguage = glslang::EShTargetSpv;
787 TargetVersion = glslang::EShTargetSpv_1_5;
John Kessenich6353d552017-06-23 11:11:09 -0600788 } else
John Kessenich273d3a52020-01-15 00:10:41 -0700789 Error("--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, opengl,\n"
John Kessenich8317e6c2019-08-18 23:58:08 -0600790 "spirv1.0, spirv1.1, spirv1.2, spirv1.3, spirv1.4, or spirv1.5");
John Kessenich6353d552017-06-23 11:11:09 -0600791 }
792 bumpArg();
John Kessenich6f988922020-01-07 07:03:11 -0700793 } else if (lowerword == "undef-macro" ||
794 lowerword == "u") {
795 if (argc > 1)
796 UserPreamble.addUndef(argv[1]);
797 else
798 Error("expects <name>", argv[0]);
799 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800800 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700801 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800802 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600803 if (argc <= 1)
John Kessenichbd97b6f2019-12-20 10:33:13 -0700804 Error("no <C-variable-name> provided", lowerword.c_str());
John Kessenichc178f0a2017-06-23 10:58:31 -0600805 variableName = argv[1];
806 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800807 break;
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +0200808 } else if (lowerword == "quiet") {
809 beQuiet = true;
Georg Lehmannf0502092021-04-20 00:22:15 +0200810 } else if (lowerword == "depfile") {
811 if (argc <= 1)
812 Error("no <depfile-name> provided", lowerword.c_str());
813 depencyFileName = argv[1];
814 bumpArg();
John Kessenichc6c80a62018-03-05 22:23:17 -0700815 } else if (lowerword == "version") {
816 Options |= EOptionDumpVersions;
Jordan Justen6ad120e2020-01-28 12:18:12 -0800817 } else if (lowerword == "help") {
818 usage();
819 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600820 } else {
John Kessenichbd97b6f2019-12-20 10:33:13 -0700821 Error("unrecognized command-line option", argv[0]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600822 }
823 }
824 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600825 case 'C':
826 Options |= EOptionCascadingErrors;
827 break;
828 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600829 if (argv[0][2] == 0)
830 Options |= EOptionReadHlsl;
831 else
John Kessenich6f988922020-01-07 07:03:11 -0700832 UserPreamble.addDef(getStringOperand("-D<name[=def]>"));
John Kessenich6263fb12017-06-14 15:52:44 -0600833 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100834 case 'u':
835 uniformLocationOverrides.push_back(getUniformOverride());
836 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600837 case 'E':
838 Options |= EOptionOutputPreprocessed;
839 break;
840 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600841 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600842 setOpenGlSpv();
843 if (argv[0][2] != 0)
844 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600845 if (ClientInputSemanticsVersion != 100)
846 Error("unknown client version for -G, should be 100");
John Kessenich6263fb12017-06-14 15:52:44 -0600847 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000848 case 'H':
849 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600850 if ((Options & EOptionSpv) == 0) {
851 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600852 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600853 }
854 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600855 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600856 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600857 break;
GregFcd1f1692017-09-21 18:40:22 -0600858 case 'O':
859 if (argv[0][2] == 'd')
860 Options |= EOptionOptimizeDisable;
861 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600862#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600863 Options |= EOptionOptimizeSize;
864#else
865 Error("-Os not available; optimizer not linked");
866#endif
867 else
868 Error("unknown -O option");
869 break;
greg-lunarg4e064ee2021-03-15 11:26:11 -0600870 case 'R':
871 VulkanRulesRelaxed = true;
872 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400873 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600874 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400875 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600876 shaderStageName = argv[1];
877 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400878 break;
John Kessenicha9313662017-06-15 10:40:49 -0600879 case 'U':
John Kessenich6f988922020-01-07 07:03:11 -0700880 UserPreamble.addUndef(getStringOperand("-U<name>"));
John Kessenicha9313662017-06-15 10:40:49 -0600881 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600882 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600883 setVulkanSpv();
884 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500885 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
johnkslang2de6d652020-08-04 07:17:39 -0600886 if (ClientInputSemanticsVersion != 100)
887 Error("unknown client version for -V, should be 100");
John Kessenichc555ddd2015-06-17 02:38:44 +0000888 break;
John Kessenich05a70632013-09-17 19:26:08 +0000889 case 'c':
890 Options |= EOptionDumpConfig;
891 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000892 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700893 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
894 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
895 Options |= EOptionDumpBareVersion;
896 else
897 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000898 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700899 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700900 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600901 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600902 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600903 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700904 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700905 case 'f':
906 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
907 targetHlslFunctionality1 = true;
908 else
909 Error("-f: expected hlsl_functionality1");
910 break;
John Kessenich121853f2017-05-31 17:11:16 -0600911 case 'g':
Shahbaz Youssefid52dce52020-06-17 12:47:44 -0400912 // Override previous -g or -g0 argument
913 stripDebugInfo = false;
914 Options &= ~EOptionDebug;
915 if (argv[0][2] == '0')
916 stripDebugInfo = true;
917 else
918 Options |= EOptionDebug;
John Kessenich121853f2017-05-31 17:11:16 -0600919 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600920 case 'h':
921 usage();
922 break;
John Kessenich05a70632013-09-17 19:26:08 +0000923 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000924 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000925 break;
926 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000927 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000928 break;
929 case 'm':
930 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000931 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600932 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600933 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600934 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600935 binaryFileName = argv[1];
936 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600937 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000938 case 'q':
939 Options |= EOptionDumpReflection;
940 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000941 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000942 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000943 break;
944 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000945 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000946 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000947 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200948 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000949 break;
John Kessenich319de232013-12-04 04:43:40 +0000950 case 'v':
951 Options |= EOptionDumpVersions;
952 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000953 case 'w':
954 Options |= EOptionSuppressWarnings;
955 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500956 case 'x':
957 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500958 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000959 default:
John Kessenichbd97b6f2019-12-20 10:33:13 -0700960 Error("unrecognized command-line option", argv[0]);
John Kessenich68d78fd2015-07-12 19:28:10 -0600961 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000962 }
John Kessenich38f3b892013-09-06 19:52:57 +0000963 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000964 std::string name(argv[0]);
965 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200966 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000967 }
John Kessenich38f3b892013-09-06 19:52:57 +0000968 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000969 }
970
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200971 // Make sure that -S is always specified if --stdin is specified
972 if ((Options & EOptionStdin) && shaderStageName == nullptr)
973 Error("must provide -S when --stdin is given");
974
John Kessenich68d78fd2015-07-12 19:28:10 -0600975 // Make sure that -E is not specified alongside linking (which includes SPV generation)
John Kesseniche5c394b2019-07-02 09:32:48 -0600976 // Or things that require linking
977 if (Options & EOptionOutputPreprocessed) {
978 if (Options & EOptionLinkProgram)
979 Error("can't use -E when linking is selected");
980 if (Options & EOptionDumpReflection)
981 Error("reflection requires linking, which can't be used when -E when is selected");
982 }
983
984 // reflection requires linking
985 if ((Options & EOptionDumpReflection) && !(Options & EOptionLinkProgram))
986 Error("reflection requires -l for linking");
John Kessenichc555ddd2015-06-17 02:38:44 +0000987
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500988 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600989 if (binaryFileName && (Options & EOptionSpv) == 0)
990 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600991
992 if ((Options & EOptionFlattenUniformArrays) != 0 &&
993 (Options & EOptionReadHlsl) == 0)
994 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600995
Jeremy Hayesb73afa82021-01-05 15:54:41 -0700996 if ((Options & EOptionReadHlsl) && (Client == glslang::EShClientOpenGL)) {
997 Error("Using HLSL input under OpenGL semantics is not currently supported.");
998 }
999
John Kessenich8717a5d2018-10-26 10:12:32 -06001000 // rationalize client and target language
1001 if (TargetLanguage == glslang::EShTargetNone) {
1002 switch (ClientVersion) {
1003 case glslang::EShTargetVulkan_1_0:
1004 TargetLanguage = glslang::EShTargetSpv;
1005 TargetVersion = glslang::EShTargetSpv_1_0;
1006 break;
1007 case glslang::EShTargetVulkan_1_1:
1008 TargetLanguage = glslang::EShTargetSpv;
1009 TargetVersion = glslang::EShTargetSpv_1_3;
1010 break;
John Kessenich273d3a52020-01-15 00:10:41 -07001011 case glslang::EShTargetVulkan_1_2:
1012 TargetLanguage = glslang::EShTargetSpv;
1013 TargetVersion = glslang::EShTargetSpv_1_5;
1014 break;
John Kessenich8717a5d2018-10-26 10:12:32 -06001015 case glslang::EShTargetOpenGL_450:
1016 TargetLanguage = glslang::EShTargetSpv;
1017 TargetVersion = glslang::EShTargetSpv_1_0;
1018 break;
1019 default:
1020 break;
1021 }
1022 }
1023 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
1024 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +00001025}
1026
John Kessenich68d78fd2015-07-12 19:28:10 -06001027//
1028// Translate the meaningful subset of command-line options to parser-behavior options.
1029//
John Kessenichb0a7eb52013-11-07 17:44:20 +00001030void SetMessageOptions(EShMessages& messages)
1031{
1032 if (Options & EOptionRelaxedErrors)
1033 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
1034 if (Options & EOptionIntermediate)
1035 messages = (EShMessages)(messages | EShMsgAST);
1036 if (Options & EOptionSuppressWarnings)
1037 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -06001038 if (Options & EOptionSpv)
1039 messages = (EShMessages)(messages | EShMsgSpvRules);
1040 if (Options & EOptionVulkanRules)
1041 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001042 if (Options & EOptionOutputPreprocessed)
1043 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -07001044 if (Options & EOptionReadHlsl)
1045 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -06001046 if (Options & EOptionCascadingErrors)
1047 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -07001048 if (Options & EOptionKeepUncalled)
1049 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -06001050 if (Options & EOptionHlslOffsets)
1051 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -06001052 if (Options & EOptionDebug)
1053 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +08001054 if (HlslEnable16BitTypes)
1055 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -06001056 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
1057 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichbd1c1832018-12-07 18:38:26 -07001058 if (HlslDX9compatible)
1059 messages = (EShMessages)(messages | EShMsgHlslDX9Compatible);
Christoph Kubisch55ba3ea2019-04-13 22:18:16 +02001060 if (DumpBuiltinSymbols)
1061 messages = (EShMessages)(messages | EShMsgBuiltinSymbolTable);
John Kessenichb0a7eb52013-11-07 17:44:20 +00001062}
1063
John Kessenich68d78fd2015-07-12 19:28:10 -06001064//
John Kessenich69f4b512013-09-04 21:19:27 +00001065// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +00001066//
Juan Lopeza558b262017-04-02 23:04:00 +02001067void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001068{
John Kessenich7f0bcfd2018-04-05 19:00:01 -06001069 if (Options & EOptionDebug)
1070 Error("cannot generate debug information unless linking to generate code");
1071
John Kessenich38f3b892013-09-06 19:52:57 +00001072 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001073 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -06001074 if (worklist.remove(workItem)) {
1075 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
1076 if (compiler == nullptr)
1077 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001078
John Kesseniche7f9cae2018-07-12 15:11:07 -06001079 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001080
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001081 if (! (Options & EOptionSuppressInfolog))
1082 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +00001083
John Kesseniche7f9cae2018-07-12 15:11:07 -06001084 ShDestruct(compiler);
1085 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001086 } else {
1087 while (worklist.remove(workItem)) {
1088 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
1089 if (compiler == 0)
1090 return;
1091
1092 CompileFile(workItem->name.c_str(), compiler);
1093
1094 if (! (Options & EOptionSuppressInfolog))
1095 workItem->results = ShGetInfoLog(compiler);
1096
1097 ShDestruct(compiler);
1098 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001099 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001100}
1101
John Kessenich6626cad2015-06-19 05:14:19 +00001102// Outputs the given string, but only if it is non-null and non-empty.
1103// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001104void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +00001105{
1106 if (str && str[0]) {
1107 puts(str);
1108 }
1109}
1110
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001111// Outputs the given string to stderr, but only if it is non-null and non-empty.
1112// This prevents erroneous newlines from appearing.
1113void StderrIfNonEmpty(const char* str)
1114{
John Kessenich04acb1b2017-06-14 17:36:50 -06001115 if (str && str[0])
1116 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001117}
1118
John Kessenichc57b2a92016-01-16 15:30:03 -07001119// Simple bundling of what makes a compilation unit for ease in passing around,
1120// and separation of handling file IO versus API (programmatic) compilation.
1121struct ShaderCompUnit {
1122 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001123 static const int maxCount = 1;
1124 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -06001125 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -06001126 std::string fileName[maxCount]; // hold's the memory, but...
1127 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -04001128
John Kessenich04acb1b2017-06-14 17:36:50 -06001129 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -04001130
John Kessenich04acb1b2017-06-14 17:36:50 -06001131 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -04001132 {
1133 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -06001134 count = rhs.count;
1135 for (int i = 0; i < count; ++i) {
1136 fileName[i] = rhs.fileName[i];
1137 text[i] = rhs.text[i];
1138 fileNameList[i] = rhs.fileName[i].c_str();
1139 }
dankbakerafe6e9c2016-08-21 12:29:08 -04001140 }
1141
John Kessenicha9313662017-06-15 10:40:49 -06001142 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -06001143 {
1144 assert(count < maxCount);
1145 fileName[count] = ifileName;
1146 text[count] = itext;
1147 fileNameList[count] = fileName[count].c_str();
1148 ++count;
1149 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001150};
1151
Georg Lehmannf0502092021-04-20 00:22:15 +02001152// Writes a depfile similar to gcc -MMD foo.c
1153bool writeDepFile(std::string depfile, std::vector<std::string>& binaryFiles, const std::vector<std::string>& sources)
1154{
1155 std::ofstream file(depfile);
1156 if (file.fail())
1157 return false;
1158
1159 for (auto it = binaryFiles.begin(); it != binaryFiles.end(); it++) {
1160 file << *it << ":";
1161 for (auto it = sources.begin(); it != sources.end(); it++) {
1162 file << " " << *it;
1163 }
1164 file << std::endl;
1165 }
1166 return true;
1167}
1168
John Kessenich69f4b512013-09-04 21:19:27 +00001169//
John Kessenichc57b2a92016-01-16 15:30:03 -07001170// For linking mode: Will independently parse each compilation unit, but then put them
1171// in the same program and link them together, making at most one linked module per
1172// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +00001173//
1174// Uses the new C++ interface instead of the old handle-based interface.
1175//
John Kessenichc57b2a92016-01-16 15:30:03 -07001176
1177void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +00001178{
1179 // keep track of what to free
1180 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -07001181
John Kessenich69f4b512013-09-04 21:19:27 +00001182 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001183 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +00001184
Georg Lehmannf0502092021-04-20 00:22:15 +02001185 DirStackFileIncluder includer;
1186 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1187 includer.pushExternalLocalDirectory(dir); });
1188
1189 std::vector<std::string> sources;
1190
John Kessenich69f4b512013-09-04 21:19:27 +00001191 //
1192 // Per-shader processing...
1193 //
1194
John Kessenich5b0f13a2013-11-01 03:08:40 +00001195 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +01001196 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
1197 const auto &compUnit = *it;
Georg Lehmannf0502092021-04-20 00:22:15 +02001198 for (int i = 0; i < compUnit.count; i++) {
1199 sources.push_back(compUnit.fileNameList[i]);
1200 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001201 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -06001202 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -06001203 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -07001204 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001205 if (sourceEntryPointName) {
1206 if (entryPointName == nullptr)
1207 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
1208 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -06001209 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -06001210 }
amhagan035a3bb2021-09-01 11:33:21 -04001211
1212 std::string intrinsicString = getIntrinsic(compUnit.text, compUnit.count);
1213
1214 PreambleString = "";
John Kessenicha9313662017-06-15 10:40:49 -06001215 if (UserPreamble.isSet())
amhagan035a3bb2021-09-01 11:33:21 -04001216 PreambleString.append(UserPreamble.get());
1217
1218 if (!intrinsicString.empty())
1219 PreambleString.append(intrinsicString);
1220
1221 shader->setPreamble(PreambleString.c_str());
John Kessenich2a271162017-07-20 20:00:36 -06001222 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001223
John Kessenich155d3512019-08-08 23:29:20 -06001224#ifndef GLSLANG_WEB
LoopDawg08a14422017-10-17 19:27:14 -06001225 // Set IO mapper binding shift values
1226 for (int r = 0; r < glslang::EResCount; ++r) {
1227 const glslang::TResourceType res = glslang::TResourceType(r);
1228
1229 // Set base bindings
1230 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
David Neto8c3d5b42019-10-21 14:50:31 -04001231
LoopDawg08a14422017-10-17 19:27:14 -06001232 // Set bindings for particular resource sets
1233 // TODO: use a range based for loop here, when available in all environments.
1234 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
1235 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -06001236 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -06001237 }
steve-lunargcce8d482016-10-14 18:36:42 -06001238 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +09001239 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001240
Greg Fischerebfca602021-04-21 16:49:53 -06001241 if (autoSampledTextures)
Robin Quint219b7b92021-04-20 07:58:36 +02001242 shader->setTextureSamplerTransformMode(EShTexSampTransUpgradeTextureRemoveSampler);
1243
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001244 if (Options & EOptionAutoMapBindings)
1245 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -07001246
John Kessenich71facdf2017-05-17 18:28:19 -06001247 if (Options & EOptionAutoMapLocations)
1248 shader->setAutoMapLocations(true);
1249
Neil Roberts16f53472018-03-20 17:30:53 +01001250 for (auto& uniOverride : uniformLocationOverrides) {
1251 shader->addUniformLocationOverride(uniOverride.first.c_str(),
1252 uniOverride.second);
1253 }
1254
Neil Robertsb0f3d792018-03-20 17:41:05 +01001255 shader->setUniformLocationBase(uniformBase);
John Kessenich155d3512019-08-08 23:29:20 -06001256#endif
1257
greg-lunarg4e064ee2021-03-15 11:26:11 -06001258 if (VulkanRulesRelaxed) {
1259 for (auto& storageOverride : blockStorageOverrides) {
1260 shader->addBlockStorageOverride(storageOverride.first.c_str(),
1261 storageOverride.second);
1262 }
1263
1264 if (setGlobalBufferBlock) {
1265 shader->setAtomicCounterBlockName(atomicCounterBlockName.c_str());
1266 shader->setAtomicCounterBlockSet(atomicCounterBlockSet);
1267 }
1268
1269 if (setGlobalUniformBlock) {
1270 shader->setGlobalUniformBlockName(globalUniformName.c_str());
1271 shader->setGlobalUniformSet(globalUniformSet);
1272 shader->setGlobalUniformBinding(globalUniformBinding);
1273 }
1274 }
1275
John Kessenich155d3512019-08-08 23:29:20 -06001276 shader->setNanMinMaxClamp(NaNClamp);
1277
1278#ifdef ENABLE_HLSL
1279 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
1280 if (Options & EOptionHlslIoMapping)
1281 shader->setHlslIoMapping(true);
1282#endif
1283
1284 if (Options & EOptionInvertY)
1285 shader->setInvertY(true);
Neil Robertsb0f3d792018-03-20 17:41:05 +01001286
John Kessenich4be4aeb2017-06-23 10:50:22 -06001287 // Set up the environment, some subsettings take precedence over earlier
1288 // ways of setting things.
1289 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -06001290 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
1291 : glslang::EShSourceGlsl,
1292 compUnit.stage, Client, ClientInputSemanticsVersion);
1293 shader->setEnvClient(Client, ClientVersion);
1294 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenichd4ed5152019-07-27 05:22:30 -06001295#ifdef ENABLE_HLSL
John Kessenich5d610ee2018-03-07 18:05:55 -07001296 if (targetHlslFunctionality1)
1297 shader->setEnvTargetHlslFunctionality1();
John Kessenichd4ed5152019-07-27 05:22:30 -06001298#endif
greg-lunarg4e064ee2021-03-15 11:26:11 -06001299 if (VulkanRulesRelaxed)
1300 shader->setEnvInputVulkanRulesRelaxed();
John Kessenich4be4aeb2017-06-23 10:50:22 -06001301 }
1302
John Kessenich69f4b512013-09-04 21:19:27 +00001303 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -06001304
John Kessenich4be4aeb2017-06-23 10:50:22 -06001305 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +00001306
John Kessenichdeec1932019-08-13 08:00:30 -06001307#ifndef GLSLANG_WEB
John Kessenichc555ddd2015-06-17 02:38:44 +00001308 if (Options & EOptionOutputPreprocessed) {
1309 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001310 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001311 PutsIfNonEmpty(str.c_str());
1312 } else {
1313 CompileFailed = true;
1314 }
1315 StderrIfNonEmpty(shader->getInfoLog());
1316 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001317 continue;
1318 }
John Kessenichdeec1932019-08-13 08:00:30 -06001319#endif
John Kessenich086febc2018-10-25 12:43:02 -06001320
John Kessenich3494b4d2017-05-22 15:00:42 -06001321 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001322 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001323
John Kessenich69f4b512013-09-04 21:19:27 +00001324 program.addShader(shader);
1325
John Kessenichc57b2a92016-01-16 15:30:03 -07001326 if (! (Options & EOptionSuppressInfolog) &&
1327 ! (Options & EOptionMemoryLeakMode)) {
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001328 if (!beQuiet)
1329 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001330 PutsIfNonEmpty(shader->getInfoLog());
1331 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001332 }
John Kessenich69f4b512013-09-04 21:19:27 +00001333 }
1334
1335 //
1336 // Program-level processing...
1337 //
1338
John Kessenich4d65ee32016-03-12 18:17:47 -07001339 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001340 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001341 LinkFailed = true;
1342
John Kessenichb6d3ee52019-08-06 02:20:45 -06001343#ifndef GLSLANG_WEB
steve-lunarg9ae34742016-10-05 13:40:13 -06001344 // Map IO
1345 if (Options & EOptionSpv) {
1346 if (!program.mapIO())
1347 LinkFailed = true;
1348 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001349#endif
John Kessenichecba76f2017-01-06 00:34:48 -07001350
John Kessenich4d65ee32016-03-12 18:17:47 -07001351 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001352 if (! (Options & EOptionSuppressInfolog) &&
1353 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001354 PutsIfNonEmpty(program.getInfoLog());
1355 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001356 }
1357
John Kessenichb6d3ee52019-08-06 02:20:45 -06001358#ifndef GLSLANG_WEB
John Kessenich4d65ee32016-03-12 18:17:47 -07001359 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001360 if (Options & EOptionDumpReflection) {
baldurk6d477852019-01-29 15:45:56 +00001361 program.buildReflection(ReflectOptions);
John Kessenich11f9fc72013-11-07 01:06:34 +00001362 program.dumpReflection();
1363 }
John Kessenichb6d3ee52019-08-06 02:20:45 -06001364#endif
John Kessenich11f9fc72013-11-07 01:06:34 +00001365
Georg Lehmannf0502092021-04-20 00:22:15 +02001366 std::vector<std::string> outputFiles;
1367
John Kessenich4d65ee32016-03-12 18:17:47 -07001368 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001369 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001370 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001371 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001372 else {
1373 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001374 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001375 std::vector<unsigned int> spirv;
Lei Zhang17535f72016-05-04 15:55:59 -04001376 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001377 glslang::SpvOptions spvOptions;
1378 if (Options & EOptionDebug)
1379 spvOptions.generateDebugInfo = true;
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001380 else if (stripDebugInfo)
1381 spvOptions.stripDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001382 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1383 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001384 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001385 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001386 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001387
1388 // Dump the spv to a file or stdout, etc., but only if not doing
1389 // memory/perf testing, as it's not internal to programmatic use.
1390 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001391 printf("%s", logger.getAllMessages().c_str());
1392 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001393 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001394 } else {
1395 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1396 }
Georg Lehmannf0502092021-04-20 00:22:15 +02001397
1398 outputFiles.push_back(GetBinaryName((EShLanguage)stage));
John Kessenich23d27752019-07-28 02:12:10 -06001399#ifndef GLSLANG_WEB
John Kesseniche2156222018-06-11 18:12:15 -06001400 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001401 spv::Disassemble(std::cout, spirv);
John Kessenich23d27752019-07-28 02:12:10 -06001402#endif
John Kessenichacba7722015-03-04 03:48:38 +00001403 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001404 }
John Kessenich92f90382014-07-28 04:21:04 +00001405 }
1406 }
1407 }
1408
Georg Lehmannf0502092021-04-20 00:22:15 +02001409 if (depencyFileName && !(CompileFailed || LinkFailed)) {
1410 std::set<std::string> includedFiles = includer.getIncludedFiles();
1411 sources.insert(sources.end(), includedFiles.begin(), includedFiles.end());
1412
1413 writeDepFile(depencyFileName, outputFiles, sources);
1414 }
1415
John Kessenich5b0f13a2013-11-01 03:08:40 +00001416 // Free everything up, program has to go before the shaders
1417 // because it might have merged stuff from the shaders, and
1418 // the stuff from the shaders has to have its destructors called
1419 // before the pools holding the memory in the shaders is freed.
1420 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001421 while (shaders.size() > 0) {
1422 delete shaders.back();
1423 shaders.pop_back();
1424 }
John Kessenich69f4b512013-09-04 21:19:27 +00001425}
1426
John Kessenichc57b2a92016-01-16 15:30:03 -07001427//
1428// Do file IO part of compile and link, handing off the pure
1429// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1430// be put in a loop for testing memory footprint and performance.
1431//
1432// This is just for linking mode: meaning all the shaders will be put into the
1433// the same program linked together.
1434//
1435// This means there are a limited number of work items (not multi-threading mode)
1436// and that the point is testing at the linking level. Hence, to enable
1437// performance and memory testing, the actual compile/link can be put in
1438// a loop, independent of processing the work items and file IO.
1439//
Juan Lopeza558b262017-04-02 23:04:00 +02001440void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001441{
1442 std::vector<ShaderCompUnit> compUnits;
1443
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001444 // If this is using stdin, we can't really detect multiple different file
1445 // units by input type. We need to assume that we're just being given one
1446 // file of a certain type.
1447 if ((Options & EOptionStdin) != 0) {
1448 ShaderCompUnit compUnit(FindLanguage("stdin"));
1449 std::istreambuf_iterator<char> begin(std::cin), end;
1450 std::string tempString(begin, end);
1451 char* fileText = strdup(tempString.c_str());
1452 std::string fileName = "stdin";
1453 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001454 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001455 } else {
1456 // Transfer all the work items from to a simple list of
1457 // of compilation units. (We don't care about the thread
1458 // work-item distribution properties in this path, which
1459 // is okay due to the limited number of shaders, know since
1460 // they are all getting linked together.)
1461 glslang::TWorkItem* workItem;
1462 while (Worklist.remove(workItem)) {
1463 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1464 char* fileText = ReadFileData(workItem->name.c_str());
1465 if (fileText == nullptr)
1466 usage();
1467 compUnit.addString(workItem->name, fileText);
1468 compUnits.push_back(compUnit);
1469 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001470 }
1471
1472 // Actual call to programmatic processing of compile and link,
1473 // in a loop for testing memory and performance. This part contains
1474 // all the perf/memory that a programmatic consumer will care about.
1475 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1476 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1477 CompileAndLinkShaderUnits(compUnits);
1478
1479 if (Options & EOptionMemoryLeakMode)
1480 glslang::OS_DumpMemoryCounters();
1481 }
1482
John Kessenicha9313662017-06-15 10:40:49 -06001483 // free memory from ReadFileData, which got stored in a const char*
1484 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001485 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001486 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001487}
1488
John Kessenich94f28eb2017-11-13 01:32:06 -07001489int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001490{
Juan Lopeza558b262017-04-02 23:04:00 +02001491 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001492 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001493 assert(item);
1494 workList.add(item.get());
1495 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001496
John Kessenich23d27752019-07-28 02:12:10 -06001497#ifndef GLSLANG_WEB
John Kessenich05a70632013-09-17 19:26:08 +00001498 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001499 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001500 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001501 return ESuccess;
1502 }
John Kessenich23d27752019-07-28 02:12:10 -06001503#endif
John Kessenich05a70632013-09-17 19:26:08 +00001504
John Kessenichc6c80a62018-03-05 22:23:17 -07001505 if (Options & EOptionDumpBareVersion) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001506 printf("%d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR, GLSLANG_VERSION_MINOR,
1507 GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenichc6c80a62018-03-05 22:23:17 -07001508 if (workList.empty())
1509 return ESuccess;
1510 } else if (Options & EOptionDumpVersions) {
Ben Claytonfbe9a232020-06-17 11:17:19 +01001511 printf("Glslang Version: %d:%d.%d.%d%s\n", glslang::GetSpirvGeneratorVersion(), GLSLANG_VERSION_MAJOR,
1512 GLSLANG_VERSION_MINOR, GLSLANG_VERSION_PATCH, GLSLANG_VERSION_FLAVOR);
John Kessenich319de232013-12-04 04:43:40 +00001513 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1514 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001515 std::string spirvVersion;
1516 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001517 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001518 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001519 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001520 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001521 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1522 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001523 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001524 return ESuccess;
1525 }
1526
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001527 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001528 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001529 }
1530
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001531 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001532 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1533 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001534 }
1535
John Kessenich05a70632013-09-17 19:26:08 +00001536 ProcessConfigFile();
1537
John Kessenich086febc2018-10-25 12:43:02 -06001538 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
John Kessenichbd97b6f2019-12-20 10:33:13 -07001539 Error("HLSL requires SPIR-V code generation (or preprocessing only)");
John Kessenich086febc2018-10-25 12:43:02 -06001540
John Kessenich69f4b512013-09-04 21:19:27 +00001541 //
1542 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001543 // 1) linking all arguments together, single-threaded, new C++ interface
1544 // 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 +00001545 //
John Kessenich086febc2018-10-25 12:43:02 -06001546 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001547 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001548 glslang::InitializeProcess(); // also test reference counting of users
1549 glslang::InitializeProcess(); // also test reference counting of users
1550 glslang::FinalizeProcess(); // also test reference counting of users
1551 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001552 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001553 glslang::FinalizeProcess();
1554 } else {
1555 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001556 ShInitialize(); // also test reference counting of users
1557 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001558
Juan Lopeza558b262017-04-02 23:04:00 +02001559 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001560
John Kesseniche2c15b42017-11-16 22:48:41 -07001561 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001562 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001563 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001564 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001565 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001566 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001567 return EFailThreadCreate;
1568 }
John Kessenicha0af4732012-12-12 21:15:54 +00001569 }
Juan Lopeza558b262017-04-02 23:04:00 +02001570
1571 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001572 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001573 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001574
1575 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001576 for (size_t w = 0; w < WorkItems.size(); ++w) {
1577 if (WorkItems[w]) {
1578 if (printShaderNames || WorkItems[w]->results.size() > 0)
1579 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1580 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001581 }
1582 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001583
1584 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001585 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001586
John Kessenichc999ba22013-11-07 23:33:24 +00001587 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001588 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001589 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001590 return EFailLink;
1591
1592 return 0;
1593}
1594
John Kessenich94f28eb2017-11-13 01:32:06 -07001595int C_DECL main(int argc, char* argv[])
1596{
1597 ProcessArguments(WorkItems, argc, argv);
1598
1599 int ret = 0;
1600
1601 // Loop over the entire init/finalize cycle to watch memory changes
1602 const int iterations = 1;
1603 if (iterations > 1)
1604 glslang::OS_DumpMemoryCounters();
1605 for (int i = 0; i < iterations; ++i) {
1606 ret = singleMain();
1607 if (iterations > 1)
1608 glslang::OS_DumpMemoryCounters();
1609 }
1610
1611 return ret;
1612}
1613
John Kessenicha0af4732012-12-12 21:15:54 +00001614//
1615// Deduce the language from the filename. Files must end in one of the
1616// following extensions:
1617//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001618// .vert = vertex
1619// .tesc = tessellation control
1620// .tese = tessellation evaluation
1621// .geom = geometry
1622// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001623// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001624// .rgen = ray generation
1625// .rint = ray intersection
1626// .rahit = ray any hit
1627// .rchit = ray closest hit
1628// .rmiss = ray miss
1629// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001630// .mesh = mesh
1631// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001632// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1633// where <stage> is one of the stages listed above.
1634//
1635EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001636{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001637 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001638 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001639 stageName = shaderStageName;
1640 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001641 // Note: "first" extension means "first from the end", i.e.
1642 // if the file is named foo.vert.glsl, then "glsl" is first,
1643 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001644 size_t firstExtStart = name.find_last_of(".");
1645 bool hasFirstExt = firstExtStart != std::string::npos;
1646 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1647 bool hasSecondExt = secondExtStart != std::string::npos;
1648 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1649 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001650 if (usesUnifiedExt && firstExt == "hlsl")
1651 Options |= EOptionReadHlsl;
1652 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001653 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001654 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001655 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001656 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001657 usage();
1658 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001659 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001660 } else
1661 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001662
John Kessenichfccbb8b2018-04-17 17:24:03 -06001663 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001664 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001665 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001666 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001667 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001668 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001669 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001670 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001671 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001672 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001673 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001674 return EShLangCompute;
Chao Chenb50c02e2018-09-19 11:42:24 -07001675 else if (stageName == "rgen")
Daniel Kochdb32b242020-03-17 20:42:47 -04001676 return EShLangRayGen;
Chao Chenb50c02e2018-09-19 11:42:24 -07001677 else if (stageName == "rint")
Daniel Kochdb32b242020-03-17 20:42:47 -04001678 return EShLangIntersect;
Chao Chenb50c02e2018-09-19 11:42:24 -07001679 else if (stageName == "rahit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001680 return EShLangAnyHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001681 else if (stageName == "rchit")
Daniel Kochdb32b242020-03-17 20:42:47 -04001682 return EShLangClosestHit;
Chao Chenb50c02e2018-09-19 11:42:24 -07001683 else if (stageName == "rmiss")
Daniel Kochdb32b242020-03-17 20:42:47 -04001684 return EShLangMiss;
Chao Chenb50c02e2018-09-19 11:42:24 -07001685 else if (stageName == "rcall")
Daniel Kochdb32b242020-03-17 20:42:47 -04001686 return EShLangCallable;
Chao Chen3c366992018-09-19 11:41:59 -07001687 else if (stageName == "mesh")
1688 return EShLangMeshNV;
1689 else if (stageName == "task")
1690 return EShLangTaskNV;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001691
1692 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001693 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001694}
1695
John Kessenicha0af4732012-12-12 21:15:54 +00001696//
John Kessenichecba76f2017-01-06 00:34:48 -07001697// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001698// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001699//
John Kessenich51cdd902014-02-18 23:37:57 +00001700void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001701{
John Kessenichca3457f2015-05-18 01:59:45 +00001702 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001703 char* shaderString;
1704 if ((Options & EOptionStdin) != 0) {
1705 std::istreambuf_iterator<char> begin(std::cin), end;
1706 std::string tempString(begin, end);
1707 shaderString = strdup(tempString.c_str());
1708 } else {
1709 shaderString = ReadFileData(fileName);
1710 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001711
1712 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001713 int* lengths = new int[1];
1714 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001715
John Kessenich52ac67e2013-05-05 23:46:22 +00001716 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001717 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001718
John Kessenicha9313662017-06-15 10:40:49 -06001719 if (UserPreamble.isSet())
1720 Error("-D and -U options require -l (linking)\n");
1721
John Kessenich94a81fb2013-08-31 02:41:30 +00001722 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1723 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001724 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001725 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001726 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001727 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001728 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001729 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1730 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001731 }
John Kessenicha0af4732012-12-12 21:15:54 +00001732
John Kessenich94a81fb2013-08-31 02:41:30 +00001733 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001734 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001735 }
John Kessenicha0af4732012-12-12 21:15:54 +00001736
John Kessenich41cf6b52013-06-25 18:10:05 +00001737 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001738 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001739
John Kessenichc999ba22013-11-07 23:33:24 +00001740 if (ret == 0)
1741 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001742}
1743
John Kessenicha0af4732012-12-12 21:15:54 +00001744//
1745// print usage to stdout
1746//
1747void usage()
1748{
John Kessenich319de232013-12-04 04:43:40 +00001749 printf("Usage: glslangValidator [option]... [file]...\n"
1750 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001751 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1752 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001753 " (see -c option below for generating a template)\n"
1754 " .vert for a vertex shader\n"
1755 " .tesc for a tessellation control shader\n"
1756 " .tese for a tessellation evaluation shader\n"
1757 " .geom for a geometry shader\n"
1758 " .frag for a fragment shader\n"
1759 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001760 " .mesh for a mesh shader\n"
1761 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001762 " .rgen for a ray generation shader\n"
1763 " .rint for a ray intersection shader\n"
1764 " .rahit for a ray any hit shader\n"
1765 " .rchit for a ray closest hit shader\n"
1766 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001767 " .rcall for a ray callable shader\n"
John Kessenich2ead40f2018-04-17 17:44:11 -06001768 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1769 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001770 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001771 "Options:\n"
1772 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001773 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenich6f988922020-01-07 07:03:11 -07001774 " -D<name[=def]> | --define-macro <name[=def]> | --D <name[=def]>\n"
1775 " define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001776 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001777 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001778 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001779 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001780 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001781 " which will appear in #define GL_SPIRV ver;\n"
1782 " '--client opengl100' is the same as -G100;\n"
Jeremy Hayesb73afa82021-01-05 15:54:41 -07001783 " a '--target-env' for OpenGL will also imply '-G';\n"
1784 " currently only supports GLSL\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001785 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001786 " -I<dir> add dir to the include search path; includer's directory\n"
1787 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001788 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1789 " -Os optimizes SPIR-V to minimize size\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001790 " -R use relaxed verification rules for generating Vulkan SPIR-V,\n"
1791 " allowing the use of default uniforms, atomic_uints, and\n"
1792 " gl_VertexID and gl_InstanceID keywords.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001793 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001794 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6f988922020-01-07 07:03:11 -07001795 " -U<name> | --undef-macro <name> | --U <name>\n"
1796 " undefine a pre-processor macro\n"
John Kessenich6353d552017-06-23 11:11:09 -06001797 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001798 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001799 " 'ver', when present, is the version of the input semantics,\n"
1800 " which will appear in #define VULKAN ver\n"
1801 " '--client vulkan100' is the same as -V100\n"
1802 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001803 " -c configuration dump;\n"
1804 " creates the default configuration file (redirect to a .conf file)\n"
1805 " -d default to desktop (#version 110) when there is no shader #version\n"
1806 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001807 " -e <name> | --entry-point <name>\n"
1808 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001809 " -f{hlsl_functionality1}\n"
1810 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001811 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001812 " -g generate debug information\n"
Shahbaz Youssefid52dce52020-06-17 12:47:44 -04001813 " -g0 strip debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001814 " -h print this usage message\n"
1815 " -i intermediate tree (glslang AST) is printed out\n"
1816 " -l link all input files together to form a single module\n"
1817 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001818 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kesseniche5c394b2019-07-02 09:32:48 -06001819 " -q dump reflection query database; requires -l for linking\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001820 " -r | --relaxed-errors"
1821 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001822 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001823 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001824 " -v | --version\n"
1825 " print version strings\n"
1826 " -w | --suppress-warnings\n"
1827 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001828 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001829 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001830 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001831 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1832 " without explicit bindings\n"
1833 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1834 " 'location' (fragile, not cross stage)\n"
Robin Quinte50b0a82021-04-22 13:13:38 +02001835 " --auto-sampled-textures Removes sampler variables and converts\n"
Robin Quint4bf373e2021-04-27 16:06:06 +02001836 " existing textures to sampled textures\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001837 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
Georg Lehmannf0502092021-04-20 00:22:15 +02001838 " --depfile <file> writes depfile for build systems\n"
Christoph Kubisch412ff6e2019-04-13 22:57:33 +02001839 " --dump-builtin-symbols prints builtin symbol table prior each compile\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001840 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1841 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1842 " scalars\n"
1843 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1844 " works independently of source language\n"
1845 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1846 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
John Kessenich605afc72019-06-17 23:33:09 -06001847 " --hlsl-dx9-compatible interprets sampler declarations as a\n"
rdbb56e0e42020-06-02 08:30:50 +02001848 " texture/sampler combo like DirectX9 would,\n"
1849 " and recognizes DirectX9-specific semantics\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001850 " --invert-y | --iy invert position.Y output in vertex shader\n"
1851 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
John Kessenich605afc72019-06-17 23:33:09 -06001852 " --nan-clamp favor non-NaN operand in min, max, and clamp\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001853 " --no-storage-format | --nsf use Unknown image format\n"
Marcin Åšlusarz245c30c2020-07-03 14:55:29 +02001854 " --quiet do not print anything to stdout, unless\n"
1855 " requested by another option\n"
baldurk4513df92019-02-08 10:48:48 +00001856 " --reflect-strict-array-suffix use strict array suffix rules when\n"
1857 " reflecting\n"
baldurkedf82122019-01-29 15:49:00 +00001858 " --reflect-basic-array-suffix arrays of basic types will have trailing [0]\n"
baldurk4513df92019-02-08 10:48:48 +00001859 " --reflect-intermediate-io reflection includes inputs/outputs of linked\n"
1860 " shaders rather than just vertex/fragment\n"
1861 " --reflect-separate-buffers reflect buffer variables and blocks\n"
1862 " separately to uniforms\n"
1863 " --reflect-all-block-variables reflect all variables in blocks, whether\n"
1864 " inactive or active\n"
baldurk19050692019-02-11 11:50:24 +00001865 " --reflect-unwrap-io-blocks unwrap input/output blocks the same as\n"
1866 " uniform blocks\n"
LoopDawg52017192017-07-14 15:15:47 -06001867 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001868 " set descriptor set and binding for\n"
1869 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001870 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001871 " set descriptor set for all resources\n"
1872 " --rsb synonym for --resource-set-binding\n"
greg-lunarg4e064ee2021-03-15 11:26:11 -06001873 " --set-block-backing name {uniform|buffer|push_constant}\n"
1874 " changes the backing type of a uniform, buffer,\n"
1875 " or push_constant block declared in\n"
1876 " in the program, when using -R option.\n"
1877 " This can be used to change the backing\n"
1878 " for existing blocks as well as implicit ones\n"
1879 " such as 'gl_DefaultUniformBlock'.\n"
1880 " --sbs synonym for set-block-storage\n"
1881 " --set-atomic-counter-block name set\n"
1882 " set name, and descriptor set for\n"
1883 " atomic counter blocks, with -R opt\n"
1884 " --sacb synonym for set-atomic-counter-block\n"
1885 " --set-default-uniform-block name set binding\n"
1886 " set name, descriptor set, and binding for\n"
1887 " global default-uniform-block, with -R opt\n"
1888 " --sdub synonym for set-default-uniform-block\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001889 " --shift-image-binding [stage] num\n"
1890 " base binding number for images (uav)\n"
1891 " --shift-image-binding [stage] [num set]...\n"
1892 " per-descriptor-set shift values\n"
1893 " --sib synonym for --shift-image-binding\n"
1894 " --shift-sampler-binding [stage] num\n"
1895 " base binding number for samplers\n"
1896 " --shift-sampler-binding [stage] [num set]...\n"
1897 " per-descriptor-set shift values\n"
1898 " --ssb synonym for --shift-sampler-binding\n"
1899 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1900 " --shift-ssbo-binding [stage] [num set]...\n"
1901 " per-descriptor-set shift values\n"
1902 " --sbb synonym for --shift-ssbo-binding\n"
1903 " --shift-texture-binding [stage] num\n"
1904 " base binding number for textures\n"
1905 " --shift-texture-binding [stage] [num set]...\n"
1906 " per-descriptor-set shift values\n"
1907 " --stb synonym for --shift-texture-binding\n"
1908 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1909 " --shift-uav-binding [stage] [num set]...\n"
1910 " per-descriptor-set shift values\n"
1911 " --suavb synonym for --shift-uav-binding\n"
1912 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1913 " --shift-UBO-binding [stage] [num set]...\n"
1914 " per-descriptor-set shift values\n"
1915 " --sub synonym for --shift-UBO-binding\n"
1916 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1917 " --spirv-dis output standard-form disassembly; works only\n"
1918 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001919 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001920 " --source-entrypoint <name> the given shader source function is\n"
1921 " renamed to be the <name> given in -e\n"
1922 " --sep synonym for --source-entrypoint\n"
1923 " --stdin read from stdin instead of from a file;\n"
1924 " requires providing the shader stage using -S\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001925 " --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | opengl | \n"
John Kessenich8317e6c2019-08-18 23:58:08 -06001926 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | spirv1.4 | spirv1.5}\n"
John Kessenich273d3a52020-01-15 00:10:41 -07001927 " Set the execution environment that the\n"
1928 " generated code will be executed in.\n"
1929 " Defaults to:\n"
1930 " * vulkan1.0 under --client vulkan<ver>\n"
1931 " * opengl under --client opengl<ver>\n"
1932 " * spirv1.0 under --target-env vulkan1.0\n"
1933 " * spirv1.3 under --target-env vulkan1.1\n"
1934 " * spirv1.5 under --target-env vulkan1.2\n"
1935 " Multiple --target-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001936 " --variable-name <name>\n"
1937 " --vn <name> creates a C header file that contains a\n"
1938 " uint32_t array named <name>\n"
1939 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001940 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001941
1942 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001943}
1944
John Kessenich3ce4e592014-10-06 19:57:34 +00001945#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001946
1947#include <errno.h>
1948
1949int fopen_s(
1950 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001951 const char* filename,
1952 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001953)
1954{
1955 if (!pFile || !filename || !mode) {
1956 return EINVAL;
1957 }
1958
1959 FILE* f = fopen(filename, mode);
1960 if (! f) {
1961 if (errno != 0) {
1962 return errno;
1963 } else {
1964 return ENOENT;
1965 }
1966 }
1967 *pFile = f;
1968
1969 return 0;
1970}
1971
1972#endif
1973
John Kessenicha0af4732012-12-12 21:15:54 +00001974//
1975// Malloc a string of sufficient size and read a string into it.
1976//
John Kessenich04acb1b2017-06-14 17:36:50 -06001977char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001978{
John Kessenichb3297152015-07-11 18:01:03 -06001979 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001980 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001981 if (errorCode || in == nullptr)
1982 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001983
John Kessenich04acb1b2017-06-14 17:36:50 -06001984 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001985 while (fgetc(in) != EOF)
1986 count++;
1987
John Kessenichd6c72a42014-08-18 19:42:35 +00001988 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001989
John Kessenich04acb1b2017-06-14 17:36:50 -06001990 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1991 if ((int)fread(return_data, 1, count, in) != count) {
1992 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001993 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001994 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001995
John Kessenich04acb1b2017-06-14 17:36:50 -06001996 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001997 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001998
John Kessenicha0af4732012-12-12 21:15:54 +00001999 return return_data;
2000}
2001
John Kessenich04acb1b2017-06-14 17:36:50 -06002002void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00002003{
John Kessenichb3297152015-07-11 18:01:03 -06002004 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00002005}
2006
John Kessenich54d8cda2013-02-11 22:36:01 +00002007void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00002008{
John Kessenichfae38ee2015-06-10 23:23:12 +00002009 if (num >= 0 )
2010 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
2011 else
2012 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00002013}