blob: 060428bf131ecfa08b88fc22c4b916803eabf05b [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 Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000035//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030038#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000039#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030040#endif
John Kessenich05a70632013-09-17 19:26:08 +000041
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040042#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000043#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060044#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000045#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060046#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060058#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020059#include <memory>
60#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000061
baldurk876a0e32015-11-16 18:03:28 +010062#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000063
64extern "C" {
65 SH_IMPORT_EXPORT void ShOutputHtml();
66}
67
John Kessenich94a81fb2013-08-31 02:41:30 +000068// Command-line options
69enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070070 EOptionNone = 0,
71 EOptionIntermediate = (1 << 0),
72 EOptionSuppressInfolog = (1 << 1),
73 EOptionMemoryLeakMode = (1 << 2),
74 EOptionRelaxedErrors = (1 << 3),
75 EOptionGiveWarnings = (1 << 4),
76 EOptionLinkProgram = (1 << 5),
77 EOptionMultiThreaded = (1 << 6),
78 EOptionDumpConfig = (1 << 7),
79 EOptionDumpReflection = (1 << 8),
80 EOptionSuppressWarnings = (1 << 9),
81 EOptionDumpVersions = (1 << 10),
82 EOptionSpv = (1 << 11),
83 EOptionHumanReadableSpv = (1 << 12),
84 EOptionVulkanRules = (1 << 13),
85 EOptionDefaultDesktop = (1 << 14),
86 EOptionOutputPreprocessed = (1 << 15),
87 EOptionOutputHexadecimal = (1 << 16),
88 EOptionReadHlsl = (1 << 17),
89 EOptionCascadingErrors = (1 << 18),
90 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060091 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070092 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070093 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060094 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060095 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060096 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060097 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +020098 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -060099 EOptionOptimizeDisable = (1 << 28),
100 EOptionOptimizeSize = (1 << 29),
LoopDawgb22c0692017-12-06 16:52:03 -0700101 EOptionInvertY = (1 << 30),
John Kessenichc6c80a62018-03-05 22:23:17 -0700102 EOptionDumpBareVersion = (1 << 31),
John Kessenich94a81fb2013-08-31 02:41:30 +0000103};
John Kessenich5d610ee2018-03-07 18:05:55 -0700104bool targetHlslFunctionality1 = false;
John Kesseniche2156222018-06-11 18:12:15 -0600105bool SpvToolsDisassembler = false;
John Kessenichc3404252018-08-23 15:29:08 -0600106bool SpvToolsValidate = false;
John Kessenich94a81fb2013-08-31 02:41:30 +0000107
John Kessenicha0af4732012-12-12 21:15:54 +0000108//
John Kessenich68d78fd2015-07-12 19:28:10 -0600109// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000110//
111enum TFailCode {
112 ESuccess = 0,
113 EFailUsage,
114 EFailCompile,
115 EFailLink,
116 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000117 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000118 EFailLinkerCreate
119};
120
121//
John Kessenich68d78fd2015-07-12 19:28:10 -0600122// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000123//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600124EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000125void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000126void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600127char* ReadFileData(const char* fileName);
128void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000129void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000130
John Kessenichc999ba22013-11-07 23:33:24 +0000131// Globally track if any compile or link failure.
132bool CompileFailed = false;
133bool LinkFailed = false;
134
John Kessenich94f28eb2017-11-13 01:32:06 -0700135// array of unique places to leave the shader names and infologs for the asynchronous compiles
136std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
137
John Kessenich05a70632013-09-17 19:26:08 +0000138TBuiltInResource Resources;
139std::string ConfigFile;
140
John Kessenicha0af4732012-12-12 21:15:54 +0000141//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600142// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000143//
144void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000145{
John Kessenich04acb1b2017-06-14 17:36:50 -0600146 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500147 Resources = glslang::DefaultTBuiltInResource;
John Kessenich04acb1b2017-06-14 17:36:50 -0600148 else {
149 char* configString = ReadFileData(ConfigFile.c_str());
150 glslang::DecodeResourceLimits(&Resources, configString);
151 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000152 }
John Kessenicha0af4732012-12-12 21:15:54 +0000153}
154
John Kessenich94a81fb2013-08-31 02:41:30 +0000155int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600156const char* ExecutableName = nullptr;
157const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700158const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600159const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400160const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800161const char* variableName = nullptr;
Rex Xucb61eec2018-03-07 13:10:01 +0800162bool HlslEnable16BitTypes = false;
John Kessenich971a0a82017-06-07 15:06:58 -0600163std::vector<std::string> IncludeDirectoryList;
John Kessenich8717a5d2018-10-26 10:12:32 -0600164
165// Source environment
166// (source 'Client' is currently the same as target 'Client')
167int ClientInputSemanticsVersion = 100;
168
169// Target environment
170glslang::EShClient Client = glslang::EShClientNone; // will stay EShClientNone if only validating
171glslang::EShTargetClientVersion ClientVersion; // not valid until Client is set
172glslang::EShTargetLanguage TargetLanguage = glslang::EShTargetNone;
173glslang::EShTargetLanguageVersion TargetVersion; // not valid until TargetLanguage is set
174
John Kessenich66011cb2018-03-06 16:12:04 -0700175std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600176
LoopDawg08a14422017-10-17 19:27:14 -0600177// Per descriptor-set binding base data
178typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
179
Neil Roberts16f53472018-03-20 17:30:53 +0100180std::vector<std::pair<std::string, int>> uniformLocationOverrides;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100181int uniformBase = 0;
Neil Roberts16f53472018-03-20 17:30:53 +0100182
LoopDawg08a14422017-10-17 19:27:14 -0600183std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
184std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900185std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600186
John Kessenicha9313662017-06-15 10:40:49 -0600187// Add things like "#define ..." to a preamble to use in the beginning of the shader.
188class TPreamble {
189public:
190 TPreamble() { }
191
192 bool isSet() const { return text.size() > 0; }
193 const char* get() const { return text.c_str(); }
194
195 // #define...
196 void addDef(std::string def)
197 {
198 text.append("#define ");
199 fixLine(def);
200
John Kessenich2a271162017-07-20 20:00:36 -0600201 Processes.push_back("D");
202 Processes.back().append(def);
203
John Kessenicha9313662017-06-15 10:40:49 -0600204 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600205 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600206 if (equal != def.npos)
207 def[equal] = ' ';
208
209 text.append(def);
210 text.append("\n");
211 }
212
213 // #undef...
214 void addUndef(std::string undef)
215 {
216 text.append("#undef ");
217 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600218
219 Processes.push_back("U");
220 Processes.back().append(undef);
221
John Kessenicha9313662017-06-15 10:40:49 -0600222 text.append(undef);
223 text.append("\n");
224 }
225
226protected:
227 void fixLine(std::string& line)
228 {
229 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600230 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600231 if (end != line.npos)
232 line = line.substr(0, end);
233 }
234
235 std::string text; // contents of preamble
236};
237
238TPreamble UserPreamble;
239
John Kessenich68d78fd2015-07-12 19:28:10 -0600240//
241// Create the default name for saving a binary if -o is not provided.
242//
243const char* GetBinaryName(EShLanguage stage)
244{
245 const char* name;
246 if (binaryFileName == nullptr) {
247 switch (stage) {
248 case EShLangVertex: name = "vert.spv"; break;
249 case EShLangTessControl: name = "tesc.spv"; break;
250 case EShLangTessEvaluation: name = "tese.spv"; break;
251 case EShLangGeometry: name = "geom.spv"; break;
252 case EShLangFragment: name = "frag.spv"; break;
253 case EShLangCompute: name = "comp.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700254#ifdef NV_EXTENSIONS
Chao Chenb50c02e2018-09-19 11:42:24 -0700255 case EShLangRayGenNV: name = "rgen.spv"; break;
256 case EShLangIntersectNV: name = "rint.spv"; break;
257 case EShLangAnyHitNV: name = "rahit.spv"; break;
258 case EShLangClosestHitNV: name = "rchit.spv"; break;
259 case EShLangMissNV: name = "rmiss.spv"; break;
260 case EShLangCallableNV: name = "rcall.spv"; break;
Chao Chen3c366992018-09-19 11:41:59 -0700261 case EShLangMeshNV: name = "mesh.spv"; break;
262 case EShLangTaskNV: name = "task.spv"; break;
263#endif
John Kessenich68d78fd2015-07-12 19:28:10 -0600264 default: name = "unknown"; break;
265 }
266 } else
267 name = binaryFileName;
268
269 return name;
270}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000271
John Kessenich05a70632013-09-17 19:26:08 +0000272//
273// *.conf => this is a config file that can set limits/resources
274//
275bool SetConfigFile(const std::string& name)
276{
277 if (name.size() < 5)
278 return false;
279
John Kessenich4c706852013-10-11 16:28:43 +0000280 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000281 ConfigFile = name;
282 return true;
283 }
284
285 return false;
286}
287
John Kessenich68d78fd2015-07-12 19:28:10 -0600288//
289// Give error and exit with failure code.
290//
291void Error(const char* message)
292{
John Kessenichaf527992017-11-02 22:48:15 -0600293 fprintf(stderr, "%s: Error %s (use -h for usage)\n", ExecutableName, message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600294 exit(EFailUsage);
295}
296
297//
LoopDawg08a14422017-10-17 19:27:14 -0600298// Process an optional binding base of one the forms:
299// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600300// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600301
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600302// Where stage is one of the forms accepted by FindLanguage, and base is an integer
303//
LoopDawg08a14422017-10-17 19:27:14 -0600304void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600305{
306 if (argc < 2)
307 usage();
308
LoopDawg08a14422017-10-17 19:27:14 -0600309 EShLanguage lang = EShLangCount;
310 int singleBase = 0;
311 TPerSetBaseBinding perSetBase;
312 int arg = 1;
313
314 // Parse stage, if given
315 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600316 if (argc < 3) // this form needs one more argument
317 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700318
LoopDawg08a14422017-10-17 19:27:14 -0600319 lang = FindLanguage(argv[arg++], false);
320 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600321
LoopDawg08a14422017-10-17 19:27:14 -0600322 if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
323 // Parse a per-set binding base
324 while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600325 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600326 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600327 perSetBase[setNum] = baseNum;
328 }
329 } else {
330 // Parse single binding base
331 singleBase = atoi(argv[arg++]);
332 }
333
334 argc -= (arg-1);
335 argv += (arg-1);
336
337 // Set one or all languages
338 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
339 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
340
341 for (int lang = langMin; lang < langMax; ++lang) {
342 if (!perSetBase.empty())
John Kessenich251901a2018-08-12 22:05:59 -0600343 baseBindingForSet[res][lang].insert(perSetBase.begin(), perSetBase.end());
LoopDawg08a14422017-10-17 19:27:14 -0600344 else
345 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600346 }
347}
348
Hyangran Park36dc8292017-05-02 16:27:29 +0900349void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
350{
351 if (argc < 2)
352 usage();
353
354 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600355 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900356 usage();
357
LoopDawg52017192017-07-14 15:15:47 -0600358 // Parse form: --argname stage [regname set base...], or:
359 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900360 const EShLanguage lang = FindLanguage(argv[1], false);
361
LoopDawg52017192017-07-14 15:15:47 -0600362 argc--;
363 argv++;
364
365 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
366 base[lang].push_back(argv[1]);
367
368 argc--;
369 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900370 }
LoopDawg52017192017-07-14 15:15:47 -0600371
372 // Must have one arg, or a multiple of three (for [regname set binding] triples)
373 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
374 usage();
375
Hyangran Park36dc8292017-05-02 16:27:29 +0900376 } else {
LoopDawg52017192017-07-14 15:15:47 -0600377 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900378 for (int lang=0; lang<EShLangCount; ++lang)
379 base[lang].push_back(argv[1]);
380
381 argc--;
382 argv++;
383 }
384}
385
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600386//
John Kessenich68d78fd2015-07-12 19:28:10 -0600387// Do all command-line argument parsing. This includes building up the work-items
388// to be processed later, and saving all the command-line options.
389//
390// Does not return (it exits) if command-line is fatally flawed.
391//
Juan Lopeza558b262017-04-02 23:04:00 +0200392void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000393{
LoopDawg08a14422017-10-17 19:27:14 -0600394 for (int res = 0; res < glslang::EResCount; ++res)
395 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600396
John Kessenich38f3b892013-09-06 19:52:57 +0000397 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200398 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000399
John Kessenichc178f0a2017-06-23 10:58:31 -0600400 const auto bumpArg = [&]() {
401 if (argc > 0) {
402 argc--;
403 argv++;
404 }
405 };
406
407 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600408 const auto getStringOperand = [&](const char* desc) {
409 if (argv[0][2] == 0) {
410 printf("%s must immediately follow option (no spaces)\n", desc);
411 exit(EFailUsage);
412 }
413 return argv[0] + 2;
414 };
415
John Kessenich6353d552017-06-23 11:11:09 -0600416 // read a number attached to a single-letter option
417 const auto getAttachedNumber = [&](const char* desc) {
418 int num = atoi(argv[0] + 2);
419 if (num == 0) {
420 printf("%s: expected attached non-0 number\n", desc);
421 exit(EFailUsage);
422 }
423 return num;
424 };
425
426 // minimum needed (without overriding something else) to target Vulkan SPIR-V
427 const auto setVulkanSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600428 if (Client == glslang::EShClientNone)
429 ClientVersion = glslang::EShTargetVulkan_1_0;
430 Client = glslang::EShClientVulkan;
John Kessenich6353d552017-06-23 11:11:09 -0600431 Options |= EOptionSpv;
432 Options |= EOptionVulkanRules;
433 Options |= EOptionLinkProgram;
434 };
435
436 // minimum needed (without overriding something else) to target OpenGL SPIR-V
437 const auto setOpenGlSpv = []() {
John Kessenich8717a5d2018-10-26 10:12:32 -0600438 if (Client == glslang::EShClientNone)
439 ClientVersion = glslang::EShTargetOpenGL_450;
440 Client = glslang::EShClientOpenGL;
John Kessenich6353d552017-06-23 11:11:09 -0600441 Options |= EOptionSpv;
442 Options |= EOptionLinkProgram;
443 // undo a -H default to Vulkan
444 Options &= ~EOptionVulkanRules;
445 };
446
Neil Roberts16f53472018-03-20 17:30:53 +0100447 const auto getUniformOverride = [getStringOperand]() {
448 const char *arg = getStringOperand("-u<name>:<location>");
449 const char *split = strchr(arg, ':');
450 if (split == NULL) {
451 printf("%s: missing location\n", arg);
452 exit(EFailUsage);
453 }
454 errno = 0;
455 int location = ::strtol(split + 1, NULL, 10);
456 if (errno) {
457 printf("%s: invalid location\n", arg);
458 exit(EFailUsage);
459 }
460 return std::make_pair(std::string(arg, split - arg), location);
461 };
462
John Kessenichc178f0a2017-06-23 10:58:31 -0600463 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000464 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000465 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600466 case '-':
467 {
468 std::string lowerword(argv[0]+2);
469 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
470
471 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600472 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600473 lowerword == "auto-map-binding" ||
474 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600475 Options |= EOptionAutoMapBindings;
476 } else if (lowerword == "auto-map-locations" || // synonyms
477 lowerword == "aml") {
478 Options |= EOptionAutoMapLocations;
Neil Robertsb0f3d792018-03-20 17:41:05 +0100479 } else if (lowerword == "uniform-base") {
480 if (argc <= 1)
481 Error("no <base> provided for --uniform-base");
482 uniformBase = ::strtol(argv[1], NULL, 10);
483 bumpArg();
484 break;
John Kessenich6353d552017-06-23 11:11:09 -0600485 } else if (lowerword == "client") {
486 if (argc > 1) {
487 if (strcmp(argv[1], "vulkan100") == 0)
488 setVulkanSpv();
489 else if (strcmp(argv[1], "opengl100") == 0)
490 setOpenGlSpv();
491 else
492 Error("--client expects vulkan100 or opengl100");
493 }
494 bumpArg();
John Kessenich640bd092018-08-09 14:15:00 -0600495 } else if (lowerword == "entry-point") {
496 entryPointName = argv[1];
497 if (argc <= 1)
498 Error("no <name> provided for --entry-point");
499 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600500 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
501 lowerword == "flatten-uniform-array" ||
502 lowerword == "fua") {
503 Options |= EOptionFlattenUniformArrays;
504 } else if (lowerword == "hlsl-offsets") {
505 Options |= EOptionHlslOffsets;
506 } else if (lowerword == "hlsl-iomap" ||
507 lowerword == "hlsl-iomapper" ||
508 lowerword == "hlsl-iomapping") {
509 Options |= EOptionHlslIoMapping;
Rex Xucb61eec2018-03-07 13:10:01 +0800510 } else if (lowerword == "hlsl-enable-16bit-types") {
511 HlslEnable16BitTypes = true;
John Kessenichc6c80a62018-03-05 22:23:17 -0700512 } else if (lowerword == "invert-y" || // synonyms
513 lowerword == "iy") {
514 Options |= EOptionInvertY;
John Kessenich6263fb12017-06-14 15:52:44 -0600515 } else if (lowerword == "keep-uncalled" || // synonyms
516 lowerword == "ku") {
517 Options |= EOptionKeepUncalled;
518 } else if (lowerword == "no-storage-format" || // synonyms
519 lowerword == "nsf") {
520 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600521 } else if (lowerword == "relaxed-errors") {
522 Options |= EOptionRelaxedErrors;
John Kessenich6263fb12017-06-14 15:52:44 -0600523 } else if (lowerword == "resource-set-bindings" || // synonyms
524 lowerword == "resource-set-binding" ||
525 lowerword == "rsb") {
526 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600527 } else if (lowerword == "shift-image-bindings" || // synonyms
528 lowerword == "shift-image-binding" ||
529 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600530 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600531 } else if (lowerword == "shift-sampler-bindings" || // synonyms
John Kessenichade8bbb2018-08-12 21:24:55 -0600532 lowerword == "shift-sampler-binding" ||
533 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600534 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600535 } else if (lowerword == "shift-uav-bindings" || // synonyms
536 lowerword == "shift-uav-binding" ||
537 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600538 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600539 } else if (lowerword == "shift-texture-bindings" || // synonyms
540 lowerword == "shift-texture-binding" ||
541 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600542 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600543 } else if (lowerword == "shift-ubo-bindings" || // synonyms
544 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600545 lowerword == "shift-cbuffer-bindings" ||
546 lowerword == "shift-cbuffer-binding" ||
547 lowerword == "sub" ||
548 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600549 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700550 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
551 lowerword == "shift-ssbo-binding" ||
552 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600553 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600554 } else if (lowerword == "source-entrypoint" || // synonyms
555 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600556 if (argc <= 1)
John Kessenich6263fb12017-06-14 15:52:44 -0600557 Error("no <entry-point> provided for --source-entrypoint");
John Kessenichc178f0a2017-06-23 10:58:31 -0600558 sourceEntryPointName = argv[1];
559 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600560 break;
John Kesseniche2156222018-06-11 18:12:15 -0600561 } else if (lowerword == "spirv-dis") {
562 SpvToolsDisassembler = true;
John Kessenichc3404252018-08-23 15:29:08 -0600563 } else if (lowerword == "spirv-val") {
564 SpvToolsValidate = true;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200565 } else if (lowerword == "stdin") {
566 Options |= EOptionStdin;
567 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600568 } else if (lowerword == "suppress-warnings") {
569 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600570 } else if (lowerword == "target-env") {
571 if (argc > 1) {
572 if (strcmp(argv[1], "vulkan1.0") == 0) {
573 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600574 ClientVersion = glslang::EShTargetVulkan_1_0;
John Kessenich66011cb2018-03-06 16:12:04 -0700575 } else if (strcmp(argv[1], "vulkan1.1") == 0) {
576 setVulkanSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600577 ClientVersion = glslang::EShTargetVulkan_1_1;
John Kessenich6353d552017-06-23 11:11:09 -0600578 } else if (strcmp(argv[1], "opengl") == 0) {
579 setOpenGlSpv();
John Kessenich8717a5d2018-10-26 10:12:32 -0600580 ClientVersion = glslang::EShTargetOpenGL_450;
581 } else if (strcmp(argv[1], "spirv1.0") == 0) {
582 TargetLanguage = glslang::EShTargetSpv;
583 TargetVersion = glslang::EShTargetSpv_1_0;
584 } else if (strcmp(argv[1], "spirv1.1") == 0) {
585 TargetLanguage = glslang::EShTargetSpv;
586 TargetVersion = glslang::EShTargetSpv_1_1;
587 } else if (strcmp(argv[1], "spirv1.2") == 0) {
588 TargetLanguage = glslang::EShTargetSpv;
589 TargetVersion = glslang::EShTargetSpv_1_2;
590 } else if (strcmp(argv[1], "spirv1.3") == 0) {
591 TargetLanguage = glslang::EShTargetSpv;
592 TargetVersion = glslang::EShTargetSpv_1_3;
593 } else if (strcmp(argv[1], "spirv1.4") == 0) {
594 TargetLanguage = glslang::EShTargetSpv;
595 TargetVersion = glslang::EShTargetSpv_1_4;
John Kessenich6353d552017-06-23 11:11:09 -0600596 } else
John Kessenich8717a5d2018-10-26 10:12:32 -0600597 Error("--target-env expected one of: vulkan1.0, vulkan1.1, opengl, spirv1.0, spirv1.1, spirv1.2, or spirv1.3");
John Kessenich6353d552017-06-23 11:11:09 -0600598 }
599 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800600 } else if (lowerword == "variable-name" || // synonyms
John Kessenich66011cb2018-03-06 16:12:04 -0700601 lowerword == "vn") {
Flavio15017db2017-02-15 14:29:33 -0800602 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600603 if (argc <= 1)
Flavio15017db2017-02-15 14:29:33 -0800604 Error("no <C-variable-name> provided for --variable-name");
John Kessenichc178f0a2017-06-23 10:58:31 -0600605 variableName = argv[1];
606 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800607 break;
John Kessenichc6c80a62018-03-05 22:23:17 -0700608 } else if (lowerword == "version") {
609 Options |= EOptionDumpVersions;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600610 } else {
611 usage();
612 }
613 }
614 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600615 case 'C':
616 Options |= EOptionCascadingErrors;
617 break;
618 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600619 if (argv[0][2] == 0)
620 Options |= EOptionReadHlsl;
621 else
622 UserPreamble.addDef(getStringOperand("-D<macro> macro name"));
John Kessenich6263fb12017-06-14 15:52:44 -0600623 break;
Neil Roberts16f53472018-03-20 17:30:53 +0100624 case 'u':
625 uniformLocationOverrides.push_back(getUniformOverride());
626 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600627 case 'E':
628 Options |= EOptionOutputPreprocessed;
629 break;
630 case 'G':
John Kessenich8717a5d2018-10-26 10:12:32 -0600631 // OpenGL client
John Kessenich6353d552017-06-23 11:11:09 -0600632 setOpenGlSpv();
633 if (argv[0][2] != 0)
634 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenich6263fb12017-06-14 15:52:44 -0600635 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000636 case 'H':
637 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600638 if ((Options & EOptionSpv) == 0) {
639 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600640 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600641 }
642 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600643 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600644 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600645 break;
GregFcd1f1692017-09-21 18:40:22 -0600646 case 'O':
647 if (argv[0][2] == 'd')
648 Options |= EOptionOptimizeDisable;
649 else if (argv[0][2] == 's')
GregFfb03a552018-03-29 11:49:14 -0600650#if ENABLE_OPT
GregFcd1f1692017-09-21 18:40:22 -0600651 Options |= EOptionOptimizeSize;
652#else
653 Error("-Os not available; optimizer not linked");
654#endif
655 else
656 Error("unknown -O option");
657 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400658 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600659 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400660 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600661 shaderStageName = argv[1];
662 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400663 break;
John Kessenicha9313662017-06-15 10:40:49 -0600664 case 'U':
665 UserPreamble.addUndef(getStringOperand("-U<macro>: macro name"));
666 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600667 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600668 setVulkanSpv();
669 if (argv[0][2] != 0)
David Neto506d2c22018-02-27 21:55:23 -0500670 ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
John Kessenichc555ddd2015-06-17 02:38:44 +0000671 break;
John Kessenich05a70632013-09-17 19:26:08 +0000672 case 'c':
673 Options |= EOptionDumpConfig;
674 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000675 case 'd':
John Kessenichc6c80a62018-03-05 22:23:17 -0700676 if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
677 strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
678 Options |= EOptionDumpBareVersion;
679 else
680 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000681 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700682 case 'e':
John Kessenich4d65ee32016-03-12 18:17:47 -0700683 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600684 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600685 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600686 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700687 break;
John Kessenich5d610ee2018-03-07 18:05:55 -0700688 case 'f':
689 if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
690 targetHlslFunctionality1 = true;
691 else
692 Error("-f: expected hlsl_functionality1");
693 break;
John Kessenich121853f2017-05-31 17:11:16 -0600694 case 'g':
695 Options |= EOptionDebug;
696 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600697 case 'h':
698 usage();
699 break;
John Kessenich05a70632013-09-17 19:26:08 +0000700 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000701 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000702 break;
703 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000704 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000705 break;
706 case 'm':
707 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000708 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600709 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600710 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600711 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600712 binaryFileName = argv[1];
713 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600714 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000715 case 'q':
716 Options |= EOptionDumpReflection;
717 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000718 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000719 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000720 break;
721 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000722 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000723 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000724 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200725 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000726 break;
John Kessenich319de232013-12-04 04:43:40 +0000727 case 'v':
728 Options |= EOptionDumpVersions;
729 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000730 case 'w':
731 Options |= EOptionSuppressWarnings;
732 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500733 case 'x':
734 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500735 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000736 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600737 usage();
738 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000739 }
John Kessenich38f3b892013-09-06 19:52:57 +0000740 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000741 std::string name(argv[0]);
742 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200743 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000744 }
John Kessenich38f3b892013-09-06 19:52:57 +0000745 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000746 }
747
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200748 // Make sure that -S is always specified if --stdin is specified
749 if ((Options & EOptionStdin) && shaderStageName == nullptr)
750 Error("must provide -S when --stdin is given");
751
John Kessenich68d78fd2015-07-12 19:28:10 -0600752 // Make sure that -E is not specified alongside linking (which includes SPV generation)
753 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
754 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000755
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500756 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600757 if (binaryFileName && (Options & EOptionSpv) == 0)
758 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600759
760 if ((Options & EOptionFlattenUniformArrays) != 0 &&
761 (Options & EOptionReadHlsl) == 0)
762 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich8717a5d2018-10-26 10:12:32 -0600763
764 // rationalize client and target language
765 if (TargetLanguage == glslang::EShTargetNone) {
766 switch (ClientVersion) {
767 case glslang::EShTargetVulkan_1_0:
768 TargetLanguage = glslang::EShTargetSpv;
769 TargetVersion = glslang::EShTargetSpv_1_0;
770 break;
771 case glslang::EShTargetVulkan_1_1:
772 TargetLanguage = glslang::EShTargetSpv;
773 TargetVersion = glslang::EShTargetSpv_1_3;
774 break;
775 case glslang::EShTargetOpenGL_450:
776 TargetLanguage = glslang::EShTargetSpv;
777 TargetVersion = glslang::EShTargetSpv_1_0;
778 break;
779 default:
780 break;
781 }
782 }
783 if (TargetLanguage != glslang::EShTargetNone && Client == glslang::EShClientNone)
784 Error("To generate SPIR-V, also specify client semantics. See -G and -V.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000785}
786
John Kessenich68d78fd2015-07-12 19:28:10 -0600787//
788// Translate the meaningful subset of command-line options to parser-behavior options.
789//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000790void SetMessageOptions(EShMessages& messages)
791{
792 if (Options & EOptionRelaxedErrors)
793 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
794 if (Options & EOptionIntermediate)
795 messages = (EShMessages)(messages | EShMsgAST);
796 if (Options & EOptionSuppressWarnings)
797 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600798 if (Options & EOptionSpv)
799 messages = (EShMessages)(messages | EShMsgSpvRules);
800 if (Options & EOptionVulkanRules)
801 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400802 if (Options & EOptionOutputPreprocessed)
803 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700804 if (Options & EOptionReadHlsl)
805 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600806 if (Options & EOptionCascadingErrors)
807 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700808 if (Options & EOptionKeepUncalled)
809 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600810 if (Options & EOptionHlslOffsets)
811 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600812 if (Options & EOptionDebug)
813 messages = (EShMessages)(messages | EShMsgDebugInfo);
Rex Xucb61eec2018-03-07 13:10:01 +0800814 if (HlslEnable16BitTypes)
815 messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
GregFfb03a552018-03-29 11:49:14 -0600816 if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
817 messages = (EShMessages)(messages | EShMsgHlslLegalization);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000818}
819
John Kessenich68d78fd2015-07-12 19:28:10 -0600820//
John Kessenich69f4b512013-09-04 21:19:27 +0000821// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000822//
Juan Lopeza558b262017-04-02 23:04:00 +0200823void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000824{
John Kessenich7f0bcfd2018-04-05 19:00:01 -0600825 if (Options & EOptionDebug)
826 Error("cannot generate debug information unless linking to generate code");
827
John Kessenich38f3b892013-09-06 19:52:57 +0000828 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200829 if (Options & EOptionStdin) {
John Kesseniche7f9cae2018-07-12 15:11:07 -0600830 if (worklist.remove(workItem)) {
831 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
832 if (compiler == nullptr)
833 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000834
John Kesseniche7f9cae2018-07-12 15:11:07 -0600835 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000836
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200837 if (! (Options & EOptionSuppressInfolog))
838 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000839
John Kesseniche7f9cae2018-07-12 15:11:07 -0600840 ShDestruct(compiler);
841 }
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200842 } else {
843 while (worklist.remove(workItem)) {
844 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
845 if (compiler == 0)
846 return;
847
848 CompileFile(workItem->name.c_str(), compiler);
849
850 if (! (Options & EOptionSuppressInfolog))
851 workItem->results = ShGetInfoLog(compiler);
852
853 ShDestruct(compiler);
854 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000855 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000856}
857
John Kessenich6626cad2015-06-19 05:14:19 +0000858// Outputs the given string, but only if it is non-null and non-empty.
859// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400860void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000861{
862 if (str && str[0]) {
863 puts(str);
864 }
865}
866
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400867// Outputs the given string to stderr, but only if it is non-null and non-empty.
868// This prevents erroneous newlines from appearing.
869void StderrIfNonEmpty(const char* str)
870{
John Kessenich04acb1b2017-06-14 17:36:50 -0600871 if (str && str[0])
872 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400873}
874
John Kessenichc57b2a92016-01-16 15:30:03 -0700875// Simple bundling of what makes a compilation unit for ease in passing around,
876// and separation of handling file IO versus API (programmatic) compilation.
877struct ShaderCompUnit {
878 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600879 static const int maxCount = 1;
880 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -0600881 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -0600882 std::string fileName[maxCount]; // hold's the memory, but...
883 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400884
John Kessenich04acb1b2017-06-14 17:36:50 -0600885 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400886
John Kessenich04acb1b2017-06-14 17:36:50 -0600887 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400888 {
889 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600890 count = rhs.count;
891 for (int i = 0; i < count; ++i) {
892 fileName[i] = rhs.fileName[i];
893 text[i] = rhs.text[i];
894 fileNameList[i] = rhs.fileName[i].c_str();
895 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400896 }
897
John Kessenicha9313662017-06-15 10:40:49 -0600898 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -0600899 {
900 assert(count < maxCount);
901 fileName[count] = ifileName;
902 text[count] = itext;
903 fileNameList[count] = fileName[count].c_str();
904 ++count;
905 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700906};
907
John Kessenich69f4b512013-09-04 21:19:27 +0000908//
John Kessenichc57b2a92016-01-16 15:30:03 -0700909// For linking mode: Will independently parse each compilation unit, but then put them
910// in the same program and link them together, making at most one linked module per
911// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000912//
913// Uses the new C++ interface instead of the old handle-based interface.
914//
John Kessenichc57b2a92016-01-16 15:30:03 -0700915
916void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000917{
918 // keep track of what to free
919 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700920
John Kessenich69f4b512013-09-04 21:19:27 +0000921 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000922 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000923
John Kessenich69f4b512013-09-04 21:19:27 +0000924 //
925 // Per-shader processing...
926 //
927
John Kessenich5b0f13a2013-11-01 03:08:40 +0000928 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100929 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
930 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700931 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -0600932 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich640bd092018-08-09 14:15:00 -0600933 if (entryPointName)
John Kessenich4d65ee32016-03-12 18:17:47 -0700934 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -0600935 if (sourceEntryPointName) {
936 if (entryPointName == nullptr)
937 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
938 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -0600939 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -0600940 }
John Kessenicha9313662017-06-15 10:40:49 -0600941 if (UserPreamble.isSet())
942 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -0600943 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600944
LoopDawg08a14422017-10-17 19:27:14 -0600945 // Set IO mapper binding shift values
946 for (int r = 0; r < glslang::EResCount; ++r) {
947 const glslang::TResourceType res = glslang::TResourceType(r);
948
949 // Set base bindings
950 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
951
952 // Set bindings for particular resource sets
953 // TODO: use a range based for loop here, when available in all environments.
954 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
955 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -0600956 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -0600957 }
958
steve-lunarge0b9deb2016-09-16 13:26:37 -0600959 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600960 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900961 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600962
steve-lunargbe283552017-04-18 12:18:01 -0600963 if (Options & EOptionHlslIoMapping)
964 shader->setHlslIoMapping(true);
965
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600966 if (Options & EOptionAutoMapBindings)
967 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700968
John Kessenich71facdf2017-05-17 18:28:19 -0600969 if (Options & EOptionAutoMapLocations)
970 shader->setAutoMapLocations(true);
971
LoopDawgb22c0692017-12-06 16:52:03 -0700972 if (Options & EOptionInvertY)
973 shader->setInvertY(true);
974
Neil Roberts16f53472018-03-20 17:30:53 +0100975 for (auto& uniOverride : uniformLocationOverrides) {
976 shader->addUniformLocationOverride(uniOverride.first.c_str(),
977 uniOverride.second);
978 }
979
Neil Robertsb0f3d792018-03-20 17:41:05 +0100980 shader->setUniformLocationBase(uniformBase);
981
John Kessenich4be4aeb2017-06-23 10:50:22 -0600982 // Set up the environment, some subsettings take precedence over earlier
983 // ways of setting things.
984 if (Options & EOptionSpv) {
John Kessenich8717a5d2018-10-26 10:12:32 -0600985 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
986 : glslang::EShSourceGlsl,
987 compUnit.stage, Client, ClientInputSemanticsVersion);
988 shader->setEnvClient(Client, ClientVersion);
989 shader->setEnvTarget(TargetLanguage, TargetVersion);
John Kessenich5d610ee2018-03-07 18:05:55 -0700990 if (targetHlslFunctionality1)
991 shader->setEnvTargetHlslFunctionality1();
John Kessenich4be4aeb2017-06-23 10:50:22 -0600992 }
993
John Kessenich69f4b512013-09-04 21:19:27 +0000994 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600995
John Kessenich4be4aeb2017-06-23 10:50:22 -0600996 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000997
John Kessenich3494b4d2017-05-22 15:00:42 -0600998 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -0600999 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
1000 includer.pushExternalLocalDirectory(dir); });
John Kessenichc555ddd2015-06-17 02:38:44 +00001001 if (Options & EOptionOutputPreprocessed) {
1002 std::string str;
John Kessenich086febc2018-10-25 12:43:02 -06001003 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false, messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001004 PutsIfNonEmpty(str.c_str());
1005 } else {
1006 CompileFailed = true;
1007 }
1008 StderrIfNonEmpty(shader->getInfoLog());
1009 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +00001010 continue;
1011 }
John Kessenich086febc2018-10-25 12:43:02 -06001012
John Kessenich3494b4d2017-05-22 15:00:42 -06001013 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +00001014 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +00001015
John Kessenich69f4b512013-09-04 21:19:27 +00001016 program.addShader(shader);
1017
John Kessenichc57b2a92016-01-16 15:30:03 -07001018 if (! (Options & EOptionSuppressInfolog) &&
1019 ! (Options & EOptionMemoryLeakMode)) {
John Kessenich04acb1b2017-06-14 17:36:50 -06001020 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001021 PutsIfNonEmpty(shader->getInfoLog());
1022 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001023 }
John Kessenich69f4b512013-09-04 21:19:27 +00001024 }
1025
1026 //
1027 // Program-level processing...
1028 //
1029
John Kessenich4d65ee32016-03-12 18:17:47 -07001030 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -06001031 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +00001032 LinkFailed = true;
1033
steve-lunarg9ae34742016-10-05 13:40:13 -06001034 // Map IO
1035 if (Options & EOptionSpv) {
1036 if (!program.mapIO())
1037 LinkFailed = true;
1038 }
John Kessenichecba76f2017-01-06 00:34:48 -07001039
John Kessenich4d65ee32016-03-12 18:17:47 -07001040 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -07001041 if (! (Options & EOptionSuppressInfolog) &&
1042 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001043 PutsIfNonEmpty(program.getInfoLog());
1044 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +00001045 }
1046
John Kessenich4d65ee32016-03-12 18:17:47 -07001047 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +00001048 if (Options & EOptionDumpReflection) {
1049 program.buildReflection();
1050 program.dumpReflection();
1051 }
1052
John Kessenich4d65ee32016-03-12 18:17:47 -07001053 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +00001054 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +00001055 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -06001056 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +00001057 else {
1058 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +00001059 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +00001060 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -04001061 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -04001062 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -06001063 glslang::SpvOptions spvOptions;
1064 if (Options & EOptionDebug)
1065 spvOptions.generateDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -06001066 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
1067 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich717c80a2018-08-23 15:17:10 -06001068 spvOptions.disassemble = SpvToolsDisassembler;
John Kessenichc3404252018-08-23 15:29:08 -06001069 spvOptions.validate = SpvToolsValidate;
John Kessenich121853f2017-05-31 17:11:16 -06001070 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -07001071
1072 // Dump the spv to a file or stdout, etc., but only if not doing
1073 // memory/perf testing, as it's not internal to programmatic use.
1074 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001075 printf("%s", logger.getAllMessages().c_str());
1076 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -07001077 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001078 } else {
1079 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
1080 }
John Kesseniche2156222018-06-11 18:12:15 -06001081 if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
John Kessenichc57b2a92016-01-16 15:30:03 -07001082 spv::Disassemble(std::cout, spirv);
John Kessenichacba7722015-03-04 03:48:38 +00001083 }
John Kessenicha7a68a92014-08-24 18:21:00 +00001084 }
John Kessenich92f90382014-07-28 04:21:04 +00001085 }
1086 }
1087 }
1088
John Kessenich5b0f13a2013-11-01 03:08:40 +00001089 // Free everything up, program has to go before the shaders
1090 // because it might have merged stuff from the shaders, and
1091 // the stuff from the shaders has to have its destructors called
1092 // before the pools holding the memory in the shaders is freed.
1093 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +00001094 while (shaders.size() > 0) {
1095 delete shaders.back();
1096 shaders.pop_back();
1097 }
John Kessenich69f4b512013-09-04 21:19:27 +00001098}
1099
John Kessenichc57b2a92016-01-16 15:30:03 -07001100//
1101// Do file IO part of compile and link, handing off the pure
1102// API/programmatic mode to CompileAndLinkShaderUnits(), which can
1103// be put in a loop for testing memory footprint and performance.
1104//
1105// This is just for linking mode: meaning all the shaders will be put into the
1106// the same program linked together.
1107//
1108// This means there are a limited number of work items (not multi-threading mode)
1109// and that the point is testing at the linking level. Hence, to enable
1110// performance and memory testing, the actual compile/link can be put in
1111// a loop, independent of processing the work items and file IO.
1112//
Juan Lopeza558b262017-04-02 23:04:00 +02001113void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -07001114{
1115 std::vector<ShaderCompUnit> compUnits;
1116
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001117 // If this is using stdin, we can't really detect multiple different file
1118 // units by input type. We need to assume that we're just being given one
1119 // file of a certain type.
1120 if ((Options & EOptionStdin) != 0) {
1121 ShaderCompUnit compUnit(FindLanguage("stdin"));
1122 std::istreambuf_iterator<char> begin(std::cin), end;
1123 std::string tempString(begin, end);
1124 char* fileText = strdup(tempString.c_str());
1125 std::string fileName = "stdin";
1126 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -07001127 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001128 } else {
1129 // Transfer all the work items from to a simple list of
1130 // of compilation units. (We don't care about the thread
1131 // work-item distribution properties in this path, which
1132 // is okay due to the limited number of shaders, know since
1133 // they are all getting linked together.)
1134 glslang::TWorkItem* workItem;
1135 while (Worklist.remove(workItem)) {
1136 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1137 char* fileText = ReadFileData(workItem->name.c_str());
1138 if (fileText == nullptr)
1139 usage();
1140 compUnit.addString(workItem->name, fileText);
1141 compUnits.push_back(compUnit);
1142 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001143 }
1144
1145 // Actual call to programmatic processing of compile and link,
1146 // in a loop for testing memory and performance. This part contains
1147 // all the perf/memory that a programmatic consumer will care about.
1148 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1149 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1150 CompileAndLinkShaderUnits(compUnits);
1151
1152 if (Options & EOptionMemoryLeakMode)
1153 glslang::OS_DumpMemoryCounters();
1154 }
1155
John Kessenicha9313662017-06-15 10:40:49 -06001156 // free memory from ReadFileData, which got stored in a const char*
1157 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001158 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001159 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001160}
1161
John Kessenich94f28eb2017-11-13 01:32:06 -07001162int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001163{
Juan Lopeza558b262017-04-02 23:04:00 +02001164 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001165 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001166 assert(item);
1167 workList.add(item.get());
1168 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001169
John Kessenich05a70632013-09-17 19:26:08 +00001170 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001171 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001172 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001173 return ESuccess;
1174 }
1175
John Kessenichc6c80a62018-03-05 22:23:17 -07001176 if (Options & EOptionDumpBareVersion) {
1177 printf("%d.%d.%d\n",
1178 glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
1179 if (workList.empty())
1180 return ESuccess;
1181 } else if (Options & EOptionDumpVersions) {
1182 printf("Glslang Version: %d.%d.%d\n",
1183 glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
John Kessenich319de232013-12-04 04:43:40 +00001184 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1185 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001186 std::string spirvVersion;
1187 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001188 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001189 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001190 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001191 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001192 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1193 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001194 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001195 return ESuccess;
1196 }
1197
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001198 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001199 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001200 }
1201
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001202 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001203 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1204 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001205 }
1206
John Kessenich05a70632013-09-17 19:26:08 +00001207 ProcessConfigFile();
1208
John Kessenich086febc2018-10-25 12:43:02 -06001209 if ((Options & EOptionReadHlsl) && !((Options & EOptionOutputPreprocessed) || (Options & EOptionSpv)))
1210 Error("ERROR: HLSL requires SPIR-V code generation (or preprocessing only)");
1211
John Kessenich69f4b512013-09-04 21:19:27 +00001212 //
1213 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001214 // 1) linking all arguments together, single-threaded, new C++ interface
1215 // 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 +00001216 //
John Kessenich086febc2018-10-25 12:43:02 -06001217 if (Options & (EOptionLinkProgram | EOptionOutputPreprocessed)) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001218 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001219 glslang::InitializeProcess(); // also test reference counting of users
1220 glslang::InitializeProcess(); // also test reference counting of users
1221 glslang::FinalizeProcess(); // also test reference counting of users
1222 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001223 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001224 glslang::FinalizeProcess();
1225 } else {
1226 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001227 ShInitialize(); // also test reference counting of users
1228 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001229
Juan Lopeza558b262017-04-02 23:04:00 +02001230 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001231
John Kesseniche2c15b42017-11-16 22:48:41 -07001232 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001233 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001234 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001235 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001236 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001237 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001238 return EFailThreadCreate;
1239 }
John Kessenicha0af4732012-12-12 21:15:54 +00001240 }
Juan Lopeza558b262017-04-02 23:04:00 +02001241
1242 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001243 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001244 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001245
1246 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001247 for (size_t w = 0; w < WorkItems.size(); ++w) {
1248 if (WorkItems[w]) {
1249 if (printShaderNames || WorkItems[w]->results.size() > 0)
1250 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1251 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001252 }
1253 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001254
1255 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001256 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001257
John Kessenichc999ba22013-11-07 23:33:24 +00001258 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001259 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001260 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001261 return EFailLink;
1262
1263 return 0;
1264}
1265
John Kessenich94f28eb2017-11-13 01:32:06 -07001266int C_DECL main(int argc, char* argv[])
1267{
1268 ProcessArguments(WorkItems, argc, argv);
1269
1270 int ret = 0;
1271
1272 // Loop over the entire init/finalize cycle to watch memory changes
1273 const int iterations = 1;
1274 if (iterations > 1)
1275 glslang::OS_DumpMemoryCounters();
1276 for (int i = 0; i < iterations; ++i) {
1277 ret = singleMain();
1278 if (iterations > 1)
1279 glslang::OS_DumpMemoryCounters();
1280 }
1281
1282 return ret;
1283}
1284
John Kessenicha0af4732012-12-12 21:15:54 +00001285//
1286// Deduce the language from the filename. Files must end in one of the
1287// following extensions:
1288//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001289// .vert = vertex
1290// .tesc = tessellation control
1291// .tese = tessellation evaluation
1292// .geom = geometry
1293// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001294// .comp = compute
Chao Chenb50c02e2018-09-19 11:42:24 -07001295// .rgen = ray generation
1296// .rint = ray intersection
1297// .rahit = ray any hit
1298// .rchit = ray closest hit
1299// .rmiss = ray miss
1300// .rcall = ray callable
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001301// .mesh = mesh
1302// .task = task
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001303// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
1304// where <stage> is one of the stages listed above.
1305//
1306EShLanguage FindLanguage(const std::string& name, bool parseStageName)
John Kessenicha0af4732012-12-12 21:15:54 +00001307{
John Kessenichfccbb8b2018-04-17 17:24:03 -06001308 std::string stageName;
Dan Bakerc6ede892016-08-11 14:06:06 -04001309 if (shaderStageName)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001310 stageName = shaderStageName;
1311 else if (parseStageName) {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001312 // Note: "first" extension means "first from the end", i.e.
1313 // if the file is named foo.vert.glsl, then "glsl" is first,
1314 // "vert" is second.
John Kessenichfccbb8b2018-04-17 17:24:03 -06001315 size_t firstExtStart = name.find_last_of(".");
1316 bool hasFirstExt = firstExtStart != std::string::npos;
1317 size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
1318 bool hasSecondExt = secondExtStart != std::string::npos;
1319 std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
1320 bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
John Kessenich3beac942018-04-17 21:02:19 -06001321 if (usesUnifiedExt && firstExt == "hlsl")
1322 Options |= EOptionReadHlsl;
1323 if (hasFirstExt && !usesUnifiedExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001324 stageName = firstExt;
John Kessenich3beac942018-04-17 21:02:19 -06001325 else if (usesUnifiedExt && hasSecondExt)
John Kessenichfccbb8b2018-04-17 17:24:03 -06001326 stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
John Kessenich3beac942018-04-17 21:02:19 -06001327 else {
Grigory Dzhavadyan33507412018-04-12 14:35:24 -07001328 usage();
1329 return EShLangVertex;
John Kesseniche751bca2017-03-16 11:20:38 -06001330 }
John Kessenichfccbb8b2018-04-17 17:24:03 -06001331 } else
1332 stageName = name;
dankbaker45d49bc2016-08-08 21:43:07 -04001333
John Kessenichfccbb8b2018-04-17 17:24:03 -06001334 if (stageName == "vert")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001335 return EShLangVertex;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001336 else if (stageName == "tesc")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001337 return EShLangTessControl;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001338 else if (stageName == "tese")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001339 return EShLangTessEvaluation;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001340 else if (stageName == "geom")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001341 return EShLangGeometry;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001342 else if (stageName == "frag")
John Kessenich2b07c7e2013-07-31 18:44:13 +00001343 return EShLangFragment;
John Kessenichfccbb8b2018-04-17 17:24:03 -06001344 else if (stageName == "comp")
John Kessenichc0275792013-08-09 17:14:49 +00001345 return EShLangCompute;
Chao Chen3c366992018-09-19 11:41:59 -07001346#ifdef NV_EXTENSIONS
Chao Chenb50c02e2018-09-19 11:42:24 -07001347 else if (stageName == "rgen")
1348 return EShLangRayGenNV;
1349 else if (stageName == "rint")
1350 return EShLangIntersectNV;
1351 else if (stageName == "rahit")
1352 return EShLangAnyHitNV;
1353 else if (stageName == "rchit")
1354 return EShLangClosestHitNV;
1355 else if (stageName == "rmiss")
1356 return EShLangMissNV;
1357 else if (stageName == "rcall")
1358 return EShLangCallableNV;
Chao Chen3c366992018-09-19 11:41:59 -07001359 else if (stageName == "mesh")
1360 return EShLangMeshNV;
1361 else if (stageName == "task")
1362 return EShLangTaskNV;
1363#endif
John Kessenich2b07c7e2013-07-31 18:44:13 +00001364
1365 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001366 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001367}
1368
John Kessenicha0af4732012-12-12 21:15:54 +00001369//
John Kessenichecba76f2017-01-06 00:34:48 -07001370// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001371// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001372//
John Kessenich51cdd902014-02-18 23:37:57 +00001373void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001374{
John Kessenichca3457f2015-05-18 01:59:45 +00001375 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001376 char* shaderString;
1377 if ((Options & EOptionStdin) != 0) {
1378 std::istreambuf_iterator<char> begin(std::cin), end;
1379 std::string tempString(begin, end);
1380 shaderString = strdup(tempString.c_str());
1381 } else {
1382 shaderString = ReadFileData(fileName);
1383 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001384
1385 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001386 int* lengths = new int[1];
1387 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001388
John Kessenich52ac67e2013-05-05 23:46:22 +00001389 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001390 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001391
John Kessenicha9313662017-06-15 10:40:49 -06001392 if (UserPreamble.isSet())
1393 Error("-D and -U options require -l (linking)\n");
1394
John Kessenich94a81fb2013-08-31 02:41:30 +00001395 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1396 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001397 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001398 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001399 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001400 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001401 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001402 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1403 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001404 }
John Kessenicha0af4732012-12-12 21:15:54 +00001405
John Kessenich94a81fb2013-08-31 02:41:30 +00001406 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001407 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001408 }
John Kessenicha0af4732012-12-12 21:15:54 +00001409
John Kessenich41cf6b52013-06-25 18:10:05 +00001410 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001411 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001412
John Kessenichc999ba22013-11-07 23:33:24 +00001413 if (ret == 0)
1414 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001415}
1416
John Kessenicha0af4732012-12-12 21:15:54 +00001417//
1418// print usage to stdout
1419//
1420void usage()
1421{
John Kessenich319de232013-12-04 04:43:40 +00001422 printf("Usage: glslangValidator [option]... [file]...\n"
1423 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001424 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1425 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001426 " (see -c option below for generating a template)\n"
1427 " .vert for a vertex shader\n"
1428 " .tesc for a tessellation control shader\n"
1429 " .tese for a tessellation evaluation shader\n"
1430 " .geom for a geometry shader\n"
1431 " .frag for a fragment shader\n"
1432 " .comp for a compute shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001433#ifdef NV_EXTENSIONS
1434 " .mesh for a mesh shader\n"
1435 " .task for a task shader\n"
Chao Chenb50c02e2018-09-19 11:42:24 -07001436 " .rgen for a ray generation shader\n"
1437 " .rint for a ray intersection shader\n"
1438 " .rahit for a ray any hit shader\n"
1439 " .rchit for a ray closest hit shader\n"
1440 " .rmiss for a ray miss shader\n"
Sahil Parmar035cbbe2018-10-04 16:39:18 -07001441 " .rcall for a ray callable shader\n"
Chao Chen3c366992018-09-19 11:41:59 -07001442#endif
John Kessenich2ead40f2018-04-17 17:44:11 -06001443 " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
1444 " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
John Kessenich319de232013-12-04 04:43:40 +00001445 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001446 "Options:\n"
1447 " -C cascading errors; risk crash from accumulation of error recoveries\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001448 " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
John Kessenicha9313662017-06-15 10:40:49 -06001449 " -D<macro=def>\n"
1450 " -D<macro> define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001451 " -E print pre-processed GLSL; cannot be used with -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001452 " errors will appear on stderr\n"
John Kessenich6353d552017-06-23 11:11:09 -06001453 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001454 " default file name is <stage>.spv (-o overrides this);\n"
John Kessenich6353d552017-06-23 11:11:09 -06001455 " 'ver', when present, is the version of the input semantics,\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001456 " which will appear in #define GL_SPIRV ver;\n"
1457 " '--client opengl100' is the same as -G100;\n"
John Kessenich6353d552017-06-23 11:11:09 -06001458 " a '--target-env' for OpenGL will also imply '-G'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001459 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001460 " -I<dir> add dir to the include search path; includer's directory\n"
1461 " is searched first, followed by left-to-right order of -I\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001462 " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
1463 " -Os optimizes SPIR-V to minimize size\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001464 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001465 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6353d552017-06-23 11:11:09 -06001466 " -U<macro> undefine a pre-processor macro\n"
1467 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001468 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001469 " 'ver', when present, is the version of the input semantics,\n"
1470 " which will appear in #define VULKAN ver\n"
1471 " '--client vulkan100' is the same as -V100\n"
1472 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001473 " -c configuration dump;\n"
1474 " creates the default configuration file (redirect to a .conf file)\n"
1475 " -d default to desktop (#version 110) when there is no shader #version\n"
1476 " (default is ES version 100)\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001477 " -e <name> | --entry-point <name>\n"
1478 " specify <name> as the entry-point function name\n"
John Kessenich5d610ee2018-03-07 18:05:55 -07001479 " -f{hlsl_functionality1}\n"
1480 " 'hlsl_functionality1' enables use of the\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001481 " SPV_GOOGLE_hlsl_functionality1 extension\n"
John Kessenich121853f2017-05-31 17:11:16 -06001482 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001483 " -h print this usage message\n"
1484 " -i intermediate tree (glslang AST) is printed out\n"
1485 " -l link all input files together to form a single module\n"
1486 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001487 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001488 " -q dump reflection query database\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001489 " -r | --relaxed-errors"
1490 " relaxed GLSL semantic error-checking mode\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001491 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001492 " -t multi-threaded mode\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001493 " -v | --version\n"
1494 " print version strings\n"
1495 " -w | --suppress-warnings\n"
1496 " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001497 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
Neil Roberts16f53472018-03-20 17:30:53 +01001498 " -u<name>:<loc> specify a uniform location override for --aml\n"
Neil Robertsb0f3d792018-03-20 17:41:05 +01001499 " --uniform-base <base> set a base to use for generated uniform locations\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001500 " --auto-map-bindings | --amb automatically bind uniform variables\n"
1501 " without explicit bindings\n"
1502 " --auto-map-locations | --aml automatically locate input/output lacking\n"
1503 " 'location' (fragile, not cross stage)\n"
1504 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
1505 " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
1506 " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
1507 " scalars\n"
1508 " --hlsl-offsets allow block offsets to follow HLSL rules\n"
1509 " works independently of source language\n"
1510 " --hlsl-iomap perform IO mapping in HLSL register space\n"
1511 " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
1512 " --invert-y | --iy invert position.Y output in vertex shader\n"
1513 " --keep-uncalled | --ku don't eliminate uncalled functions\n"
1514 " --no-storage-format | --nsf use Unknown image format\n"
LoopDawg52017192017-07-14 15:15:47 -06001515 " --resource-set-binding [stage] name set binding\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001516 " set descriptor set and binding for\n"
1517 " individual resources\n"
LoopDawg52017192017-07-14 15:15:47 -06001518 " --resource-set-binding [stage] set\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001519 " set descriptor set for all resources\n"
1520 " --rsb synonym for --resource-set-binding\n"
1521 " --shift-image-binding [stage] num\n"
1522 " base binding number for images (uav)\n"
1523 " --shift-image-binding [stage] [num set]...\n"
1524 " per-descriptor-set shift values\n"
1525 " --sib synonym for --shift-image-binding\n"
1526 " --shift-sampler-binding [stage] num\n"
1527 " base binding number for samplers\n"
1528 " --shift-sampler-binding [stage] [num set]...\n"
1529 " per-descriptor-set shift values\n"
1530 " --ssb synonym for --shift-sampler-binding\n"
1531 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1532 " --shift-ssbo-binding [stage] [num set]...\n"
1533 " per-descriptor-set shift values\n"
1534 " --sbb synonym for --shift-ssbo-binding\n"
1535 " --shift-texture-binding [stage] num\n"
1536 " base binding number for textures\n"
1537 " --shift-texture-binding [stage] [num set]...\n"
1538 " per-descriptor-set shift values\n"
1539 " --stb synonym for --shift-texture-binding\n"
1540 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1541 " --shift-uav-binding [stage] [num set]...\n"
1542 " per-descriptor-set shift values\n"
1543 " --suavb synonym for --shift-uav-binding\n"
1544 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1545 " --shift-UBO-binding [stage] [num set]...\n"
1546 " per-descriptor-set shift values\n"
1547 " --sub synonym for --shift-UBO-binding\n"
1548 " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
1549 " --spirv-dis output standard-form disassembly; works only\n"
1550 " when a SPIR-V generation option is also used\n"
John Kessenichc3404252018-08-23 15:29:08 -06001551 " --spirv-val execute the SPIRV-Tools validator\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001552 " --source-entrypoint <name> the given shader source function is\n"
1553 " renamed to be the <name> given in -e\n"
1554 " --sep synonym for --source-entrypoint\n"
1555 " --stdin read from stdin instead of from a file;\n"
1556 " requires providing the shader stage using -S\n"
John Kessenich8717a5d2018-10-26 10:12:32 -06001557 " --target-env {vulkan1.0 | vulkan1.1 | opengl | \n"
1558 " spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3}\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001559 " set execution environment that emitted code\n"
John Kessenich8717a5d2018-10-26 10:12:32 -06001560 " will execute in (versus source language\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001561 " semantics selected by --client) defaults:\n"
1562 " * 'vulkan1.0' under '--client vulkan<ver>'\n"
1563 " * 'opengl' under '--client opengl<ver>'\n"
John Kessenich8717a5d2018-10-26 10:12:32 -06001564 " * 'spirv1.0' under --target-env vulkan1.0\n"
1565 " * 'spirv1.3' under --target-env vulkan1.1\n"
1566 " multiple --targen-env can be specified.\n"
John Kessenichade8bbb2018-08-12 21:24:55 -06001567 " --variable-name <name>\n"
1568 " --vn <name> creates a C header file that contains a\n"
1569 " uint32_t array named <name>\n"
1570 " initialized with the shader binary code\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001571 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001572
1573 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001574}
1575
John Kessenich3ce4e592014-10-06 19:57:34 +00001576#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001577
1578#include <errno.h>
1579
1580int fopen_s(
1581 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001582 const char* filename,
1583 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001584)
1585{
1586 if (!pFile || !filename || !mode) {
1587 return EINVAL;
1588 }
1589
1590 FILE* f = fopen(filename, mode);
1591 if (! f) {
1592 if (errno != 0) {
1593 return errno;
1594 } else {
1595 return ENOENT;
1596 }
1597 }
1598 *pFile = f;
1599
1600 return 0;
1601}
1602
1603#endif
1604
John Kessenicha0af4732012-12-12 21:15:54 +00001605//
1606// Malloc a string of sufficient size and read a string into it.
1607//
John Kessenich04acb1b2017-06-14 17:36:50 -06001608char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001609{
John Kessenichb3297152015-07-11 18:01:03 -06001610 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001611 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001612 if (errorCode || in == nullptr)
1613 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001614
John Kessenich04acb1b2017-06-14 17:36:50 -06001615 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001616 while (fgetc(in) != EOF)
1617 count++;
1618
John Kessenichd6c72a42014-08-18 19:42:35 +00001619 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001620
John Kessenich04acb1b2017-06-14 17:36:50 -06001621 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1622 if ((int)fread(return_data, 1, count, in) != count) {
1623 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001624 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001625 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001626
John Kessenich04acb1b2017-06-14 17:36:50 -06001627 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001628 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001629
John Kessenicha0af4732012-12-12 21:15:54 +00001630 return return_data;
1631}
1632
John Kessenich04acb1b2017-06-14 17:36:50 -06001633void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001634{
John Kessenichb3297152015-07-11 18:01:03 -06001635 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001636}
1637
John Kessenich54d8cda2013-02-11 22:36:01 +00001638void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001639{
John Kessenichfae38ee2015-06-10 23:23:12 +00001640 if (num >= 0 )
1641 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1642 else
1643 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001644}