blob: 53a33b8a79efc4a039fcd5babc2386f68fc36dbb [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>
Juan Lopeza558b262017-04-02 23:04:00 +020058#include <memory>
59#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000060
baldurk876a0e32015-11-16 18:03:28 +010061#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000062
63extern "C" {
64 SH_IMPORT_EXPORT void ShOutputHtml();
65}
66
John Kessenich94a81fb2013-08-31 02:41:30 +000067// Command-line options
68enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070069 EOptionNone = 0,
70 EOptionIntermediate = (1 << 0),
71 EOptionSuppressInfolog = (1 << 1),
72 EOptionMemoryLeakMode = (1 << 2),
73 EOptionRelaxedErrors = (1 << 3),
74 EOptionGiveWarnings = (1 << 4),
75 EOptionLinkProgram = (1 << 5),
76 EOptionMultiThreaded = (1 << 6),
77 EOptionDumpConfig = (1 << 7),
78 EOptionDumpReflection = (1 << 8),
79 EOptionSuppressWarnings = (1 << 9),
80 EOptionDumpVersions = (1 << 10),
81 EOptionSpv = (1 << 11),
82 EOptionHumanReadableSpv = (1 << 12),
83 EOptionVulkanRules = (1 << 13),
84 EOptionDefaultDesktop = (1 << 14),
85 EOptionOutputPreprocessed = (1 << 15),
86 EOptionOutputHexadecimal = (1 << 16),
87 EOptionReadHlsl = (1 << 17),
88 EOptionCascadingErrors = (1 << 18),
89 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060090 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070091 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070092 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060093 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060094 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060095 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060096 EOptionDebug = (1 << 26),
John Kessenich94a81fb2013-08-31 02:41:30 +000097};
98
John Kessenicha0af4732012-12-12 21:15:54 +000099//
John Kessenich68d78fd2015-07-12 19:28:10 -0600100// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000101//
102enum TFailCode {
103 ESuccess = 0,
104 EFailUsage,
105 EFailCompile,
106 EFailLink,
107 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000108 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000109 EFailLinkerCreate
110};
111
112//
John Kessenich68d78fd2015-07-12 19:28:10 -0600113// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000114//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600115EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000116void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000117void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600118char* ReadFileData(const char* fileName);
119void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000120void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000121
John Kessenichc999ba22013-11-07 23:33:24 +0000122// Globally track if any compile or link failure.
123bool CompileFailed = false;
124bool LinkFailed = false;
125
John Kessenich05a70632013-09-17 19:26:08 +0000126TBuiltInResource Resources;
127std::string ConfigFile;
128
John Kessenicha0af4732012-12-12 21:15:54 +0000129//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600130// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000131//
132void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000133{
John Kessenich04acb1b2017-06-14 17:36:50 -0600134 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500135 Resources = glslang::DefaultTBuiltInResource;
John Kessenich04acb1b2017-06-14 17:36:50 -0600136 else {
137 char* configString = ReadFileData(ConfigFile.c_str());
138 glslang::DecodeResourceLimits(&Resources, configString);
139 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000140 }
John Kessenicha0af4732012-12-12 21:15:54 +0000141}
142
John Kessenich94a81fb2013-08-31 02:41:30 +0000143int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600144const char* ExecutableName = nullptr;
145const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700146const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600147const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400148const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800149const char* variableName = nullptr;
John Kessenich971a0a82017-06-07 15:06:58 -0600150std::vector<std::string> IncludeDirectoryList;
John Kessenich6353d552017-06-23 11:11:09 -0600151int ClientInputSemanticsVersion = 100; // maps to, say, #define VULKAN 100
152int VulkanClientVersion = 100; // would map to, say, Vulkan 1.0
153int OpenGLClientVersion = 450; // doesn't influence anything yet, but maps to OpenGL 4.50
154unsigned int TargetVersion = 0x00001000; // maps to, say, SPIR-V 1.0
John Kessenich2a271162017-07-20 20:00:36 -0600155std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600156
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600157std::array<unsigned int, EShLangCount> baseSamplerBinding;
158std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600159std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600160std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700161std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600162std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900163std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600164
John Kessenicha9313662017-06-15 10:40:49 -0600165// Add things like "#define ..." to a preamble to use in the beginning of the shader.
166class TPreamble {
167public:
168 TPreamble() { }
169
170 bool isSet() const { return text.size() > 0; }
171 const char* get() const { return text.c_str(); }
172
173 // #define...
174 void addDef(std::string def)
175 {
176 text.append("#define ");
177 fixLine(def);
178
John Kessenich2a271162017-07-20 20:00:36 -0600179 Processes.push_back("D");
180 Processes.back().append(def);
181
John Kessenicha9313662017-06-15 10:40:49 -0600182 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600183 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600184 if (equal != def.npos)
185 def[equal] = ' ';
186
187 text.append(def);
188 text.append("\n");
189 }
190
191 // #undef...
192 void addUndef(std::string undef)
193 {
194 text.append("#undef ");
195 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600196
197 Processes.push_back("U");
198 Processes.back().append(undef);
199
John Kessenicha9313662017-06-15 10:40:49 -0600200 text.append(undef);
201 text.append("\n");
202 }
203
204protected:
205 void fixLine(std::string& line)
206 {
207 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600208 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600209 if (end != line.npos)
210 line = line.substr(0, end);
211 }
212
213 std::string text; // contents of preamble
214};
215
216TPreamble UserPreamble;
217
John Kessenich68d78fd2015-07-12 19:28:10 -0600218//
219// Create the default name for saving a binary if -o is not provided.
220//
221const char* GetBinaryName(EShLanguage stage)
222{
223 const char* name;
224 if (binaryFileName == nullptr) {
225 switch (stage) {
226 case EShLangVertex: name = "vert.spv"; break;
227 case EShLangTessControl: name = "tesc.spv"; break;
228 case EShLangTessEvaluation: name = "tese.spv"; break;
229 case EShLangGeometry: name = "geom.spv"; break;
230 case EShLangFragment: name = "frag.spv"; break;
231 case EShLangCompute: name = "comp.spv"; break;
232 default: name = "unknown"; break;
233 }
234 } else
235 name = binaryFileName;
236
237 return name;
238}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000239
John Kessenich05a70632013-09-17 19:26:08 +0000240//
241// *.conf => this is a config file that can set limits/resources
242//
243bool SetConfigFile(const std::string& name)
244{
245 if (name.size() < 5)
246 return false;
247
John Kessenich4c706852013-10-11 16:28:43 +0000248 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000249 ConfigFile = name;
250 return true;
251 }
252
253 return false;
254}
255
John Kessenich68d78fd2015-07-12 19:28:10 -0600256//
257// Give error and exit with failure code.
258//
259void Error(const char* message)
260{
261 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
262 exit(EFailUsage);
263}
264
265//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600266// Process an optional binding base of the form:
267// --argname [stage] base
268// Where stage is one of the forms accepted by FindLanguage, and base is an integer
269//
270void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
271{
272 if (argc < 2)
273 usage();
274
275 if (!isdigit(argv[1][0])) {
276 if (argc < 3) // this form needs one more argument
277 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700278
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600279 // Parse form: --argname stage base
280 const EShLanguage lang = FindLanguage(argv[1], false);
281 base[lang] = atoi(argv[2]);
282 argc-= 2;
283 argv+= 2;
284 } else {
285 // Parse form: --argname base
286 for (int lang=0; lang<EShLangCount; ++lang)
287 base[lang] = atoi(argv[1]);
288
289 argc--;
290 argv++;
291 }
292}
293
Hyangran Park36dc8292017-05-02 16:27:29 +0900294void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
295{
296 if (argc < 2)
297 usage();
298
299 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600300 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900301 usage();
302
LoopDawg52017192017-07-14 15:15:47 -0600303 // Parse form: --argname stage [regname set base...], or:
304 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900305 const EShLanguage lang = FindLanguage(argv[1], false);
306
LoopDawg52017192017-07-14 15:15:47 -0600307 argc--;
308 argv++;
309
310 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
311 base[lang].push_back(argv[1]);
312
313 argc--;
314 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900315 }
LoopDawg52017192017-07-14 15:15:47 -0600316
317 // Must have one arg, or a multiple of three (for [regname set binding] triples)
318 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
319 usage();
320
Hyangran Park36dc8292017-05-02 16:27:29 +0900321 } else {
LoopDawg52017192017-07-14 15:15:47 -0600322 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900323 for (int lang=0; lang<EShLangCount; ++lang)
324 base[lang].push_back(argv[1]);
325
326 argc--;
327 argv++;
328 }
329}
330
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600331//
John Kessenich68d78fd2015-07-12 19:28:10 -0600332// Do all command-line argument parsing. This includes building up the work-items
333// to be processed later, and saving all the command-line options.
334//
335// Does not return (it exits) if command-line is fatally flawed.
336//
Juan Lopeza558b262017-04-02 23:04:00 +0200337void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000338{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600339 baseSamplerBinding.fill(0);
340 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600341 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600342 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700343 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600344 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600345
John Kessenich38f3b892013-09-06 19:52:57 +0000346 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200347 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000348
John Kessenichc178f0a2017-06-23 10:58:31 -0600349 const auto bumpArg = [&]() {
350 if (argc > 0) {
351 argc--;
352 argv++;
353 }
354 };
355
356 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600357 const auto getStringOperand = [&](const char* desc) {
358 if (argv[0][2] == 0) {
359 printf("%s must immediately follow option (no spaces)\n", desc);
360 exit(EFailUsage);
361 }
362 return argv[0] + 2;
363 };
364
John Kessenich6353d552017-06-23 11:11:09 -0600365 // read a number attached to a single-letter option
366 const auto getAttachedNumber = [&](const char* desc) {
367 int num = atoi(argv[0] + 2);
368 if (num == 0) {
369 printf("%s: expected attached non-0 number\n", desc);
370 exit(EFailUsage);
371 }
372 return num;
373 };
374
375 // minimum needed (without overriding something else) to target Vulkan SPIR-V
376 const auto setVulkanSpv = []() {
377 Options |= EOptionSpv;
378 Options |= EOptionVulkanRules;
379 Options |= EOptionLinkProgram;
380 };
381
382 // minimum needed (without overriding something else) to target OpenGL SPIR-V
383 const auto setOpenGlSpv = []() {
384 Options |= EOptionSpv;
385 Options |= EOptionLinkProgram;
386 // undo a -H default to Vulkan
387 Options &= ~EOptionVulkanRules;
388 };
389
John Kessenichc178f0a2017-06-23 10:58:31 -0600390 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000391 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000392 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600393 case '-':
394 {
395 std::string lowerword(argv[0]+2);
396 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
397
398 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600399 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600400 lowerword == "auto-map-binding" ||
401 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600402 Options |= EOptionAutoMapBindings;
403 } else if (lowerword == "auto-map-locations" || // synonyms
404 lowerword == "aml") {
405 Options |= EOptionAutoMapLocations;
John Kessenich6353d552017-06-23 11:11:09 -0600406 } else if (lowerword == "client") {
407 if (argc > 1) {
408 if (strcmp(argv[1], "vulkan100") == 0)
409 setVulkanSpv();
410 else if (strcmp(argv[1], "opengl100") == 0)
411 setOpenGlSpv();
412 else
413 Error("--client expects vulkan100 or opengl100");
414 }
415 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600416 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
417 lowerword == "flatten-uniform-array" ||
418 lowerword == "fua") {
419 Options |= EOptionFlattenUniformArrays;
420 } else if (lowerword == "hlsl-offsets") {
421 Options |= EOptionHlslOffsets;
422 } else if (lowerword == "hlsl-iomap" ||
423 lowerword == "hlsl-iomapper" ||
424 lowerword == "hlsl-iomapping") {
425 Options |= EOptionHlslIoMapping;
426 } else if (lowerword == "keep-uncalled" || // synonyms
427 lowerword == "ku") {
428 Options |= EOptionKeepUncalled;
429 } else if (lowerword == "no-storage-format" || // synonyms
430 lowerword == "nsf") {
431 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600432 } else if (lowerword == "relaxed-errors") {
433 Options |= EOptionRelaxedErrors;
John Kessenich6263fb12017-06-14 15:52:44 -0600434 } else if (lowerword == "resource-set-bindings" || // synonyms
435 lowerword == "resource-set-binding" ||
436 lowerword == "rsb") {
437 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600438 } else if (lowerword == "shift-image-bindings" || // synonyms
439 lowerword == "shift-image-binding" ||
440 lowerword == "sib") {
441 ProcessBindingBase(argc, argv, baseImageBinding);
John Kessenich6263fb12017-06-14 15:52:44 -0600442 } else if (lowerword == "shift-sampler-bindings" || // synonyms
443 lowerword == "shift-sampler-binding" ||
444 lowerword == "ssb") {
445 ProcessBindingBase(argc, argv, baseSamplerBinding);
446 } else if (lowerword == "shift-uav-bindings" || // synonyms
447 lowerword == "shift-uav-binding" ||
448 lowerword == "suavb") {
449 ProcessBindingBase(argc, argv, baseUavBinding);
450 } else if (lowerword == "shift-texture-bindings" || // synonyms
451 lowerword == "shift-texture-binding" ||
452 lowerword == "stb") {
453 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600454 } else if (lowerword == "shift-ubo-bindings" || // synonyms
455 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600456 lowerword == "shift-cbuffer-bindings" ||
457 lowerword == "shift-cbuffer-binding" ||
458 lowerword == "sub" ||
459 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600460 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700461 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
462 lowerword == "shift-ssbo-binding" ||
463 lowerword == "sbb") {
464 ProcessBindingBase(argc, argv, baseSsboBinding);
John Kessenich6263fb12017-06-14 15:52:44 -0600465 } else if (lowerword == "source-entrypoint" || // synonyms
466 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600467 if (argc <= 1)
John Kessenich6263fb12017-06-14 15:52:44 -0600468 Error("no <entry-point> provided for --source-entrypoint");
John Kessenichc178f0a2017-06-23 10:58:31 -0600469 sourceEntryPointName = argv[1];
470 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600471 break;
John Kessenich2a271162017-07-20 20:00:36 -0600472 } else if (lowerword == "suppress-warnings") {
473 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600474 } else if (lowerword == "target-env") {
475 if (argc > 1) {
476 if (strcmp(argv[1], "vulkan1.0") == 0) {
477 setVulkanSpv();
478 VulkanClientVersion = 100;
479 } else if (strcmp(argv[1], "opengl") == 0) {
480 setOpenGlSpv();
481 OpenGLClientVersion = 450;
482 } else
483 Error("--target-env expected vulkan1.0 or opengl");
484 }
485 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800486 } else if (lowerword == "variable-name" || // synonyms
487 lowerword == "vn") {
488 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600489 if (argc <= 1)
Flavio15017db2017-02-15 14:29:33 -0800490 Error("no <C-variable-name> provided for --variable-name");
John Kessenichc178f0a2017-06-23 10:58:31 -0600491 variableName = argv[1];
492 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800493 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600494 } else {
495 usage();
496 }
497 }
498 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600499 case 'C':
500 Options |= EOptionCascadingErrors;
501 break;
502 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600503 if (argv[0][2] == 0)
504 Options |= EOptionReadHlsl;
505 else
506 UserPreamble.addDef(getStringOperand("-D<macro> macro name"));
John Kessenich6263fb12017-06-14 15:52:44 -0600507 break;
508 case 'E':
509 Options |= EOptionOutputPreprocessed;
510 break;
511 case 'G':
John Kessenich6353d552017-06-23 11:11:09 -0600512 // OpenGL Client
513 setOpenGlSpv();
514 if (argv[0][2] != 0)
515 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenich6263fb12017-06-14 15:52:44 -0600516 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000517 case 'H':
518 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600519 if ((Options & EOptionSpv) == 0) {
520 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600521 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600522 }
523 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600524 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600525 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600526 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400527 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600528 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400529 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600530 shaderStageName = argv[1];
531 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400532 break;
John Kessenicha9313662017-06-15 10:40:49 -0600533 case 'U':
534 UserPreamble.addUndef(getStringOperand("-U<macro>: macro name"));
535 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600536 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600537 setVulkanSpv();
538 if (argv[0][2] != 0)
539 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenichc555ddd2015-06-17 02:38:44 +0000540 break;
John Kessenich05a70632013-09-17 19:26:08 +0000541 case 'c':
542 Options |= EOptionDumpConfig;
543 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000544 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000545 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000546 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700547 case 'e':
548 // HLSL todo: entry point handle needs much more sophistication.
549 // This is okay for one compilation unit with one entry point.
550 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600551 if (argc <= 1)
John Kessenich4d65ee32016-03-12 18:17:47 -0700552 Error("no <entry-point> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600553 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700554 break;
John Kessenich121853f2017-05-31 17:11:16 -0600555 case 'g':
556 Options |= EOptionDebug;
557 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600558 case 'h':
559 usage();
560 break;
John Kessenich05a70632013-09-17 19:26:08 +0000561 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000562 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000563 break;
564 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000565 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000566 break;
567 case 'm':
568 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000569 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600570 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600571 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600572 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600573 binaryFileName = argv[1];
574 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600575 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000576 case 'q':
577 Options |= EOptionDumpReflection;
578 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000579 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000580 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000581 break;
582 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000583 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000584 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000585 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200586 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000587 break;
John Kessenich319de232013-12-04 04:43:40 +0000588 case 'v':
589 Options |= EOptionDumpVersions;
590 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000591 case 'w':
592 Options |= EOptionSuppressWarnings;
593 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500594 case 'x':
595 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500596 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000597 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600598 usage();
599 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000600 }
John Kessenich38f3b892013-09-06 19:52:57 +0000601 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000602 std::string name(argv[0]);
603 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200604 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000605 }
John Kessenich38f3b892013-09-06 19:52:57 +0000606 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000607 }
608
John Kessenich68d78fd2015-07-12 19:28:10 -0600609 // Make sure that -E is not specified alongside linking (which includes SPV generation)
610 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
611 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000612
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500613 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600614 if (binaryFileName && (Options & EOptionSpv) == 0)
615 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600616
617 if ((Options & EOptionFlattenUniformArrays) != 0 &&
618 (Options & EOptionReadHlsl) == 0)
619 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000620}
621
John Kessenich68d78fd2015-07-12 19:28:10 -0600622//
623// Translate the meaningful subset of command-line options to parser-behavior options.
624//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000625void SetMessageOptions(EShMessages& messages)
626{
627 if (Options & EOptionRelaxedErrors)
628 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
629 if (Options & EOptionIntermediate)
630 messages = (EShMessages)(messages | EShMsgAST);
631 if (Options & EOptionSuppressWarnings)
632 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600633 if (Options & EOptionSpv)
634 messages = (EShMessages)(messages | EShMsgSpvRules);
635 if (Options & EOptionVulkanRules)
636 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400637 if (Options & EOptionOutputPreprocessed)
638 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700639 if (Options & EOptionReadHlsl)
640 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600641 if (Options & EOptionCascadingErrors)
642 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700643 if (Options & EOptionKeepUncalled)
644 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600645 if (Options & EOptionHlslOffsets)
646 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600647 if (Options & EOptionDebug)
648 messages = (EShMessages)(messages | EShMsgDebugInfo);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000649}
650
John Kessenich68d78fd2015-07-12 19:28:10 -0600651//
John Kessenich69f4b512013-09-04 21:19:27 +0000652// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000653//
Juan Lopeza558b262017-04-02 23:04:00 +0200654void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000655{
John Kessenich38f3b892013-09-06 19:52:57 +0000656 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200657 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000658 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000659 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200660 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000661
John Kessenichb0a7eb52013-11-07 17:44:20 +0000662 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000663
John Kessenich94a81fb2013-08-31 02:41:30 +0000664 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000665 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000666
667 ShDestruct(compiler);
668 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000669}
670
John Kessenich6626cad2015-06-19 05:14:19 +0000671// Outputs the given string, but only if it is non-null and non-empty.
672// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400673void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000674{
675 if (str && str[0]) {
676 puts(str);
677 }
678}
679
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400680// Outputs the given string to stderr, but only if it is non-null and non-empty.
681// This prevents erroneous newlines from appearing.
682void StderrIfNonEmpty(const char* str)
683{
John Kessenich04acb1b2017-06-14 17:36:50 -0600684 if (str && str[0])
685 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400686}
687
John Kessenichc57b2a92016-01-16 15:30:03 -0700688// Simple bundling of what makes a compilation unit for ease in passing around,
689// and separation of handling file IO versus API (programmatic) compilation.
690struct ShaderCompUnit {
691 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600692 static const int maxCount = 1;
693 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -0600694 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -0600695 std::string fileName[maxCount]; // hold's the memory, but...
696 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400697
John Kessenich04acb1b2017-06-14 17:36:50 -0600698 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400699
John Kessenich04acb1b2017-06-14 17:36:50 -0600700 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400701 {
702 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600703 count = rhs.count;
704 for (int i = 0; i < count; ++i) {
705 fileName[i] = rhs.fileName[i];
706 text[i] = rhs.text[i];
707 fileNameList[i] = rhs.fileName[i].c_str();
708 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400709 }
710
John Kessenicha9313662017-06-15 10:40:49 -0600711 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -0600712 {
713 assert(count < maxCount);
714 fileName[count] = ifileName;
715 text[count] = itext;
716 fileNameList[count] = fileName[count].c_str();
717 ++count;
718 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700719};
720
John Kessenich69f4b512013-09-04 21:19:27 +0000721//
John Kessenichc57b2a92016-01-16 15:30:03 -0700722// For linking mode: Will independently parse each compilation unit, but then put them
723// in the same program and link them together, making at most one linked module per
724// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000725//
726// Uses the new C++ interface instead of the old handle-based interface.
727//
John Kessenichc57b2a92016-01-16 15:30:03 -0700728
729void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000730{
731 // keep track of what to free
732 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700733
John Kessenich69f4b512013-09-04 21:19:27 +0000734 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000735 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000736
John Kessenich69f4b512013-09-04 21:19:27 +0000737 //
738 // Per-shader processing...
739 //
740
John Kessenich5b0f13a2013-11-01 03:08:40 +0000741 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100742 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
743 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700744 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -0600745 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich4d65ee32016-03-12 18:17:47 -0700746 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
747 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600748 if (sourceEntryPointName)
749 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenicha9313662017-06-15 10:40:49 -0600750 if (UserPreamble.isSet())
751 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -0600752 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600753
754 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
755 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600756 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600757 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700758 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600759 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600760 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600761 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900762 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600763
steve-lunargbe283552017-04-18 12:18:01 -0600764 if (Options & EOptionHlslIoMapping)
765 shader->setHlslIoMapping(true);
766
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600767 if (Options & EOptionAutoMapBindings)
768 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700769
John Kessenich71facdf2017-05-17 18:28:19 -0600770 if (Options & EOptionAutoMapLocations)
771 shader->setAutoMapLocations(true);
772
John Kessenich4be4aeb2017-06-23 10:50:22 -0600773 // Set up the environment, some subsettings take precedence over earlier
774 // ways of setting things.
775 if (Options & EOptionSpv) {
776 if (Options & EOptionVulkanRules) {
777 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
778 : glslang::EShSourceGlsl,
John Kessenich6353d552017-06-23 11:11:09 -0600779 compUnit.stage, glslang::EShClientVulkan, ClientInputSemanticsVersion);
780 shader->setEnvClient(glslang::EShClientVulkan, VulkanClientVersion);
781 shader->setEnvTarget(glslang::EshTargetSpv, TargetVersion);
John Kessenich4be4aeb2017-06-23 10:50:22 -0600782 } else {
783 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
784 : glslang::EShSourceGlsl,
John Kessenich6353d552017-06-23 11:11:09 -0600785 compUnit.stage, glslang::EShClientOpenGL, ClientInputSemanticsVersion);
786 shader->setEnvClient(glslang::EShClientOpenGL, OpenGLClientVersion);
787 shader->setEnvTarget(glslang::EshTargetSpv, TargetVersion);
John Kessenich4be4aeb2017-06-23 10:50:22 -0600788 }
789 }
790
John Kessenich69f4b512013-09-04 21:19:27 +0000791 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600792
John Kessenich4be4aeb2017-06-23 10:50:22 -0600793 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000794
John Kessenich3494b4d2017-05-22 15:00:42 -0600795 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -0600796 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
797 includer.pushExternalLocalDirectory(dir); });
John Kessenichc555ddd2015-06-17 02:38:44 +0000798 if (Options & EOptionOutputPreprocessed) {
799 std::string str;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400800 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500801 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400802 PutsIfNonEmpty(str.c_str());
803 } else {
804 CompileFailed = true;
805 }
806 StderrIfNonEmpty(shader->getInfoLog());
807 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000808 continue;
809 }
John Kessenich3494b4d2017-05-22 15:00:42 -0600810 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +0000811 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000812
John Kessenich69f4b512013-09-04 21:19:27 +0000813 program.addShader(shader);
814
John Kessenichc57b2a92016-01-16 15:30:03 -0700815 if (! (Options & EOptionSuppressInfolog) &&
816 ! (Options & EOptionMemoryLeakMode)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600817 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400818 PutsIfNonEmpty(shader->getInfoLog());
819 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000820 }
John Kessenich69f4b512013-09-04 21:19:27 +0000821 }
822
823 //
824 // Program-level processing...
825 //
826
John Kessenich4d65ee32016-03-12 18:17:47 -0700827 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600828 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000829 LinkFailed = true;
830
steve-lunarg9ae34742016-10-05 13:40:13 -0600831 // Map IO
832 if (Options & EOptionSpv) {
833 if (!program.mapIO())
834 LinkFailed = true;
835 }
John Kessenichecba76f2017-01-06 00:34:48 -0700836
John Kessenich4d65ee32016-03-12 18:17:47 -0700837 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700838 if (! (Options & EOptionSuppressInfolog) &&
839 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400840 PutsIfNonEmpty(program.getInfoLog());
841 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000842 }
843
John Kessenich4d65ee32016-03-12 18:17:47 -0700844 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000845 if (Options & EOptionDumpReflection) {
846 program.buildReflection();
847 program.dumpReflection();
848 }
849
John Kessenich4d65ee32016-03-12 18:17:47 -0700850 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000851 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000852 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600853 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000854 else {
855 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000856 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000857 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400858 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400859 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -0600860 glslang::SpvOptions spvOptions;
861 if (Options & EOptionDebug)
862 spvOptions.generateDebugInfo = true;
863 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -0700864
865 // Dump the spv to a file or stdout, etc., but only if not doing
866 // memory/perf testing, as it's not internal to programmatic use.
867 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500868 printf("%s", logger.getAllMessages().c_str());
869 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700870 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500871 } else {
872 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
873 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700874 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700875 spv::Disassemble(std::cout, spirv);
876 }
John Kessenichacba7722015-03-04 03:48:38 +0000877 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000878 }
John Kessenich92f90382014-07-28 04:21:04 +0000879 }
880 }
881 }
882
John Kessenich5b0f13a2013-11-01 03:08:40 +0000883 // Free everything up, program has to go before the shaders
884 // because it might have merged stuff from the shaders, and
885 // the stuff from the shaders has to have its destructors called
886 // before the pools holding the memory in the shaders is freed.
887 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000888 while (shaders.size() > 0) {
889 delete shaders.back();
890 shaders.pop_back();
891 }
John Kessenich69f4b512013-09-04 21:19:27 +0000892}
893
John Kessenichc57b2a92016-01-16 15:30:03 -0700894//
895// Do file IO part of compile and link, handing off the pure
896// API/programmatic mode to CompileAndLinkShaderUnits(), which can
897// be put in a loop for testing memory footprint and performance.
898//
899// This is just for linking mode: meaning all the shaders will be put into the
900// the same program linked together.
901//
902// This means there are a limited number of work items (not multi-threading mode)
903// and that the point is testing at the linking level. Hence, to enable
904// performance and memory testing, the actual compile/link can be put in
905// a loop, independent of processing the work items and file IO.
906//
Juan Lopeza558b262017-04-02 23:04:00 +0200907void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700908{
909 std::vector<ShaderCompUnit> compUnits;
910
911 // Transfer all the work items from to a simple list of
912 // of compilation units. (We don't care about the thread
913 // work-item distribution properties in this path, which
914 // is okay due to the limited number of shaders, know since
915 // they are all getting linked together.)
916 glslang::TWorkItem* workItem;
917 while (Worklist.remove(workItem)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600918 ShaderCompUnit compUnit(FindLanguage(workItem->name));
919 char* fileText = ReadFileData(workItem->name.c_str());
920 if (fileText == nullptr)
John Kessenichc57b2a92016-01-16 15:30:03 -0700921 usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600922 compUnit.addString(workItem->name, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -0700923 compUnits.push_back(compUnit);
924 }
925
926 // Actual call to programmatic processing of compile and link,
927 // in a loop for testing memory and performance. This part contains
928 // all the perf/memory that a programmatic consumer will care about.
929 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
930 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
931 CompileAndLinkShaderUnits(compUnits);
932
933 if (Options & EOptionMemoryLeakMode)
934 glslang::OS_DumpMemoryCounters();
935 }
936
John Kessenicha9313662017-06-15 10:40:49 -0600937 // free memory from ReadFileData, which got stored in a const char*
938 // as the first string above
rdb32084e82016-02-23 22:17:38 +0100939 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -0600940 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -0700941}
942
John Kessenicha0af4732012-12-12 21:15:54 +0000943int C_DECL main(int argc, char* argv[])
944{
Juan Lopeza558b262017-04-02 23:04:00 +0200945 // array of unique places to leave the shader names and infologs for the asynchronous compiles
946 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
947 ProcessArguments(workItems, argc, argv);
948
949 glslang::TWorklist workList;
950 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
951 assert(item);
952 workList.add(item.get());
953 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000954
John Kessenich05a70632013-09-17 19:26:08 +0000955 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500956 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200957 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000958 return ESuccess;
959 }
960
John Kessenich0da9eaa2015-08-01 17:10:02 -0600961 if (Options & EOptionDumpVersions) {
962 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000963 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
964 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600965 std::string spirvVersion;
966 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600967 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600968 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700969 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600970 printf("GL_KHR_vulkan_glsl version %d\n", 100);
971 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200972 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000973 return ESuccess;
974 }
975
Juan Lopeza558b262017-04-02 23:04:00 +0200976 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000977 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000978 }
979
980 ProcessConfigFile();
981
John Kessenich69f4b512013-09-04 21:19:27 +0000982 //
983 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000984 // 1) linking all arguments together, single-threaded, new C++ interface
985 // 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 +0000986 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000987 if (Options & EOptionLinkProgram ||
988 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000989 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200990 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000991 glslang::FinalizeProcess();
992 } else {
993 ShInitialize();
994
Juan Lopeza558b262017-04-02 23:04:00 +0200995 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000996
Juan Lopeza558b262017-04-02 23:04:00 +0200997 if (Options & EOptionMultiThreaded)
998 {
999 std::array<std::thread, 16> threads;
1000 for (unsigned int t = 0; t < threads.size(); ++t)
1001 {
1002 threads[t] = std::thread(CompileShaders, std::ref(workList));
1003 if (threads[t].get_id() == std::thread::id())
1004 {
John Kessenich38f3b892013-09-06 19:52:57 +00001005 printf("Failed to create thread\n");
1006 return EFailThreadCreate;
1007 }
John Kessenicha0af4732012-12-12 21:15:54 +00001008 }
Juan Lopeza558b262017-04-02 23:04:00 +02001009
1010 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001011 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001012 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001013
1014 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +02001015 for (size_t w = 0; w < workItems.size(); ++w) {
1016 if (workItems[w]) {
1017 if (printShaderNames || workItems[w]->results.size() > 0)
1018 PutsIfNonEmpty(workItems[w]->name.c_str());
1019 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001020 }
1021 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001022
1023 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001024 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001025
John Kessenichc999ba22013-11-07 23:33:24 +00001026 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001027 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001028 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001029 return EFailLink;
1030
1031 return 0;
1032}
1033
1034//
1035// Deduce the language from the filename. Files must end in one of the
1036// following extensions:
1037//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001038// .vert = vertex
1039// .tesc = tessellation control
1040// .tese = tessellation evaluation
1041// .geom = geometry
1042// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001043// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +00001044//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001045EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +00001046{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001047 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -06001048 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001049
Dan Bakerc6ede892016-08-11 14:06:06 -04001050 if (shaderStageName)
1051 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -06001052 else {
1053 // Search for a suffix on a filename: e.g, "myfile.frag". If given
1054 // the suffix directly, we skip looking for the '.'
1055 if (parseSuffix) {
1056 ext = name.rfind('.');
1057 if (ext == std::string::npos) {
1058 usage();
1059 return EShLangVertex;
1060 }
1061 ++ext;
1062 }
1063 suffix = name.substr(ext, std::string::npos);
1064 }
dankbaker45d49bc2016-08-08 21:43:07 -04001065
John Kessenich2b07c7e2013-07-31 18:44:13 +00001066 if (suffix == "vert")
1067 return EShLangVertex;
1068 else if (suffix == "tesc")
1069 return EShLangTessControl;
1070 else if (suffix == "tese")
1071 return EShLangTessEvaluation;
1072 else if (suffix == "geom")
1073 return EShLangGeometry;
1074 else if (suffix == "frag")
1075 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +00001076 else if (suffix == "comp")
1077 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001078
1079 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001080 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001081}
1082
John Kessenicha0af4732012-12-12 21:15:54 +00001083//
John Kessenichecba76f2017-01-06 00:34:48 -07001084// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001085// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001086//
John Kessenich51cdd902014-02-18 23:37:57 +00001087void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001088{
John Kessenichca3457f2015-05-18 01:59:45 +00001089 int ret = 0;
John Kessenich04acb1b2017-06-14 17:36:50 -06001090 char* shaderString = ReadFileData(fileName);
John Kessenich41cf6b52013-06-25 18:10:05 +00001091
1092 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001093 int* lengths = new int[1];
1094 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001095
John Kessenich52ac67e2013-05-05 23:46:22 +00001096 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001097 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001098
John Kessenicha9313662017-06-15 10:40:49 -06001099 if (UserPreamble.isSet())
1100 Error("-D and -U options require -l (linking)\n");
1101
John Kessenich94a81fb2013-08-31 02:41:30 +00001102 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1103 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001104 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001105 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001106 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001107 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001108 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001109 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1110 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001111 }
John Kessenicha0af4732012-12-12 21:15:54 +00001112
John Kessenich94a81fb2013-08-31 02:41:30 +00001113 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001114 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001115 }
John Kessenicha0af4732012-12-12 21:15:54 +00001116
John Kessenich41cf6b52013-06-25 18:10:05 +00001117 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001118 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001119
John Kessenichc999ba22013-11-07 23:33:24 +00001120 if (ret == 0)
1121 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001122}
1123
John Kessenicha0af4732012-12-12 21:15:54 +00001124//
1125// print usage to stdout
1126//
1127void usage()
1128{
John Kessenich319de232013-12-04 04:43:40 +00001129 printf("Usage: glslangValidator [option]... [file]...\n"
1130 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001131 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1132 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001133 " (see -c option below for generating a template)\n"
1134 " .vert for a vertex shader\n"
1135 " .tesc for a tessellation control shader\n"
1136 " .tese for a tessellation evaluation shader\n"
1137 " .geom for a geometry shader\n"
1138 " .frag for a fragment shader\n"
1139 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001140 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001141 "Options:\n"
1142 " -C cascading errors; risk crash from accumulation of error recoveries\n"
1143 " -D input is HLSL\n"
John Kessenicha9313662017-06-15 10:40:49 -06001144 " -D<macro=def>\n"
1145 " -D<macro> define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001146 " -E print pre-processed GLSL; cannot be used with -l;\n"
1147 " errors will appear on stderr.\n"
John Kessenich6353d552017-06-23 11:11:09 -06001148 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001149 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001150 " 'ver', when present, is the version of the input semantics,\n"
1151 " which will appear in #define GL_SPIRV ver\n"
1152 " '--client opengl100' is the same as -G100\n"
1153 " a '--target-env' for OpenGL will also imply '-G'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001154 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001155 " -I<dir> add dir to the include search path; includer's directory\n"
1156 " is searched first, followed by left-to-right order of -I\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001157 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001158 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6353d552017-06-23 11:11:09 -06001159 " -U<macro> undefine a pre-processor macro\n"
1160 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001161 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001162 " 'ver', when present, is the version of the input semantics,\n"
1163 " which will appear in #define VULKAN ver\n"
1164 " '--client vulkan100' is the same as -V100\n"
1165 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001166 " -c configuration dump;\n"
1167 " creates the default configuration file (redirect to a .conf file)\n"
1168 " -d default to desktop (#version 110) when there is no shader #version\n"
1169 " (default is ES version 100)\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001170 " -e specify entry-point name\n"
John Kessenich121853f2017-05-31 17:11:16 -06001171 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001172 " -h print this usage message\n"
1173 " -i intermediate tree (glslang AST) is printed out\n"
1174 " -l link all input files together to form a single module\n"
1175 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001176 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001177 " -q dump reflection query database\n"
John Kessenich2a271162017-07-20 20:00:36 -06001178 " -r synonym for --relaxed-errors\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001179 " -s silent mode\n"
1180 " -t multi-threaded mode\n"
1181 " -v print version strings\n"
John Kessenich2a271162017-07-20 20:00:36 -06001182 " -w synonym for --suppress-warnings\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001183 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
1184 " --auto-map-bindings automatically bind uniform variables\n"
1185 " without explicit bindings.\n"
1186 " --amb synonym for --auto-map-bindings\n"
1187 " --auto-map-locations automatically locate input/output lacking\n"
John Kessenichc178f0a2017-06-23 10:58:31 -06001188 " 'location' (fragile, not cross stage)\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001189 " --aml synonym for --auto-map-locations\n"
John Kessenich6353d552017-06-23 11:11:09 -06001190 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001191 " --flatten-uniform-arrays flatten uniform texture/sampler arrays to\n"
1192 " scalars\n"
1193 " --fua synonym for --flatten-uniform-arrays\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001194 " --hlsl-offsets Allow block offsets to follow HLSL rules\n"
1195 " Works independently of source language\n"
1196 " --hlsl-iomap Perform IO mapping in HLSL register space\n"
1197 " --keep-uncalled don't eliminate uncalled functions\n"
1198 " --ku synonym for --keep-uncalled\n"
1199 " --no-storage-format use Unknown image format\n"
1200 " --nsf synonym for --no-storage-format\n"
John Kessenich2a271162017-07-20 20:00:36 -06001201 " --relaxed-errors relaxed GLSL semantic error-checking mode\n"
LoopDawg52017192017-07-14 15:15:47 -06001202 " --resource-set-binding [stage] name set binding\n"
1203 " Set descriptor set and binding for individual resources\n"
1204 " --resource-set-binding [stage] set\n"
1205 " Set descriptor set for all resources\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001206 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1207 " --shift-image-binding [stage] num base binding number for images (uav)\n"
1208 " --sib [stage] num synonym for --shift-image-binding\n"
1209 " --shift-sampler-binding [stage] num base binding number for samplers\n"
1210 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1211 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1212 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1213 " --shift-texture-binding [stage] num base binding number for textures\n"
1214 " --stb [stage] num synonym for --shift-texture-binding\n"
1215 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1216 " --suavb [stage] num synonym for --shift-uav-binding\n"
1217 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1218 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
1219 " --sub [stage] num synonym for --shift-UBO-binding\n"
1220 " --source-entrypoint name the given shader source function is\n"
1221 " renamed to be the entry point given in -e\n"
1222 " --sep synonym for --source-entrypoint\n"
John Kessenich2a271162017-07-20 20:00:36 -06001223 " --suppress-warnings suppress GLSL warnings\n"
1224 " (except as required by #extension : warn)\n"
1225 " --target-env {vulkan1.0|opengl} set the execution environment code will\n"
1226 " execute in (as opposed to language\n"
1227 " semantics selected by --client) defaults:\n"
1228 " 'vulkan1.0' under '--client vulkan<ver>'\n"
1229 " 'opengl' under '--client opengl<ver>'\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001230 " --variable-name <name> Creates a C header file that contains a\n"
1231 " uint32_t array named <name>\n"
1232 " initialized with the shader binary code.\n"
1233 " --vn <name> synonym for --variable-name <name>\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001234 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001235
1236 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001237}
1238
John Kessenich3ce4e592014-10-06 19:57:34 +00001239#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001240
1241#include <errno.h>
1242
1243int fopen_s(
1244 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001245 const char* filename,
1246 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001247)
1248{
1249 if (!pFile || !filename || !mode) {
1250 return EINVAL;
1251 }
1252
1253 FILE* f = fopen(filename, mode);
1254 if (! f) {
1255 if (errno != 0) {
1256 return errno;
1257 } else {
1258 return ENOENT;
1259 }
1260 }
1261 *pFile = f;
1262
1263 return 0;
1264}
1265
1266#endif
1267
John Kessenicha0af4732012-12-12 21:15:54 +00001268//
1269// Malloc a string of sufficient size and read a string into it.
1270//
John Kessenich04acb1b2017-06-14 17:36:50 -06001271char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001272{
John Kessenichb3297152015-07-11 18:01:03 -06001273 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001274 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001275 if (errorCode || in == nullptr)
1276 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001277
John Kessenich04acb1b2017-06-14 17:36:50 -06001278 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001279 while (fgetc(in) != EOF)
1280 count++;
1281
John Kessenichd6c72a42014-08-18 19:42:35 +00001282 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001283
John Kessenich04acb1b2017-06-14 17:36:50 -06001284 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1285 if ((int)fread(return_data, 1, count, in) != count) {
1286 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001287 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001288 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001289
John Kessenich04acb1b2017-06-14 17:36:50 -06001290 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001291 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001292
John Kessenicha0af4732012-12-12 21:15:54 +00001293 return return_data;
1294}
1295
John Kessenich04acb1b2017-06-14 17:36:50 -06001296void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001297{
John Kessenichb3297152015-07-11 18:01:03 -06001298 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001299}
1300
John Kessenich54d8cda2013-02-11 22:36:01 +00001301void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001302{
John Kessenichfae38ee2015-06-10 23:23:12 +00001303 if (num >= 0 )
1304 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1305 else
1306 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001307}