blob: 3c36025bf9624c7aa35a4291ff0453456fd572e4 [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
38#define _CRT_SECURE_NO_WARNINGS
39
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040040#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000041#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000042#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060043#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000045#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060046#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000047#include "../SPIRV/doc.h"
48#include "../SPIRV/disassemble.h"
John Kessenich66ec80e2016-08-05 14:04:23 -060049#include <cstring>
50#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060051#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060052#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060053#include <array>
John Kessenicha0af4732012-12-12 21:15:54 +000054
baldurk876a0e32015-11-16 18:03:28 +010055#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000056
57extern "C" {
58 SH_IMPORT_EXPORT void ShOutputHtml();
59}
60
John Kessenich94a81fb2013-08-31 02:41:30 +000061// Command-line options
62enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070063 EOptionNone = 0,
64 EOptionIntermediate = (1 << 0),
65 EOptionSuppressInfolog = (1 << 1),
66 EOptionMemoryLeakMode = (1 << 2),
67 EOptionRelaxedErrors = (1 << 3),
68 EOptionGiveWarnings = (1 << 4),
69 EOptionLinkProgram = (1 << 5),
70 EOptionMultiThreaded = (1 << 6),
71 EOptionDumpConfig = (1 << 7),
72 EOptionDumpReflection = (1 << 8),
73 EOptionSuppressWarnings = (1 << 9),
74 EOptionDumpVersions = (1 << 10),
75 EOptionSpv = (1 << 11),
76 EOptionHumanReadableSpv = (1 << 12),
77 EOptionVulkanRules = (1 << 13),
78 EOptionDefaultDesktop = (1 << 14),
79 EOptionOutputPreprocessed = (1 << 15),
80 EOptionOutputHexadecimal = (1 << 16),
81 EOptionReadHlsl = (1 << 17),
82 EOptionCascadingErrors = (1 << 18),
83 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060084 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070085 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070086 EOptionKeepUncalled = (1 << 22),
John Kessenich94a81fb2013-08-31 02:41:30 +000087};
88
John Kessenicha0af4732012-12-12 21:15:54 +000089//
John Kessenich68d78fd2015-07-12 19:28:10 -060090// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000091//
92enum TFailCode {
93 ESuccess = 0,
94 EFailUsage,
95 EFailCompile,
96 EFailLink,
97 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000098 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000099 EFailLinkerCreate
100};
101
102//
John Kessenich68d78fd2015-07-12 19:28:10 -0600103// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000104//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600105EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000106void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000107void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000108void FreeFileData(char** data);
109char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000110void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000111
John Kessenichc999ba22013-11-07 23:33:24 +0000112// Globally track if any compile or link failure.
113bool CompileFailed = false;
114bool LinkFailed = false;
115
John Kessenich05a70632013-09-17 19:26:08 +0000116// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600117// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000118int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000119
John Kessenich05a70632013-09-17 19:26:08 +0000120TBuiltInResource Resources;
121std::string ConfigFile;
122
John Kessenicha0af4732012-12-12 21:15:54 +0000123//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600124// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000125//
126void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000127{
John Kessenich05a70632013-09-17 19:26:08 +0000128 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000129 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000130 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000131 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000132 if (configStrings)
133 config = *configStrings;
134 else {
135 printf("Error opening configuration file; will instead use the default configuration\n");
136 usage();
137 }
138 }
139
140 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500141 Resources = glslang::DefaultTBuiltInResource;
142 return;
John Kessenich05a70632013-09-17 19:26:08 +0000143 }
144
Lei Zhang1b141722016-05-19 13:50:49 -0400145 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000146
John Kessenich05a70632013-09-17 19:26:08 +0000147 if (configStrings)
148 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500149 else
150 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000151}
152
John Kessenich38f3b892013-09-06 19:52:57 +0000153// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000154glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000155
156// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000157glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000158int NumWorkItems = 0;
159
John Kessenich94a81fb2013-08-31 02:41:30 +0000160int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600161const char* ExecutableName = nullptr;
162const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700163const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600164const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400165const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800166const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600167
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600168std::array<unsigned int, EShLangCount> baseSamplerBinding;
169std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600170std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600171std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700172std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600173
John Kessenich68d78fd2015-07-12 19:28:10 -0600174//
175// Create the default name for saving a binary if -o is not provided.
176//
177const char* GetBinaryName(EShLanguage stage)
178{
179 const char* name;
180 if (binaryFileName == nullptr) {
181 switch (stage) {
182 case EShLangVertex: name = "vert.spv"; break;
183 case EShLangTessControl: name = "tesc.spv"; break;
184 case EShLangTessEvaluation: name = "tese.spv"; break;
185 case EShLangGeometry: name = "geom.spv"; break;
186 case EShLangFragment: name = "frag.spv"; break;
187 case EShLangCompute: name = "comp.spv"; break;
188 default: name = "unknown"; break;
189 }
190 } else
191 name = binaryFileName;
192
193 return name;
194}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000195
John Kessenich05a70632013-09-17 19:26:08 +0000196//
197// *.conf => this is a config file that can set limits/resources
198//
199bool SetConfigFile(const std::string& name)
200{
201 if (name.size() < 5)
202 return false;
203
John Kessenich4c706852013-10-11 16:28:43 +0000204 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000205 ConfigFile = name;
206 return true;
207 }
208
209 return false;
210}
211
John Kessenich68d78fd2015-07-12 19:28:10 -0600212//
213// Give error and exit with failure code.
214//
215void Error(const char* message)
216{
217 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
218 exit(EFailUsage);
219}
220
221//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600222// Process an optional binding base of the form:
223// --argname [stage] base
224// Where stage is one of the forms accepted by FindLanguage, and base is an integer
225//
226void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
227{
228 if (argc < 2)
229 usage();
230
231 if (!isdigit(argv[1][0])) {
232 if (argc < 3) // this form needs one more argument
233 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700234
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600235 // Parse form: --argname stage base
236 const EShLanguage lang = FindLanguage(argv[1], false);
237 base[lang] = atoi(argv[2]);
238 argc-= 2;
239 argv+= 2;
240 } else {
241 // Parse form: --argname base
242 for (int lang=0; lang<EShLangCount; ++lang)
243 base[lang] = atoi(argv[1]);
244
245 argc--;
246 argv++;
247 }
248}
249
250//
John Kessenich68d78fd2015-07-12 19:28:10 -0600251// Do all command-line argument parsing. This includes building up the work-items
252// to be processed later, and saving all the command-line options.
253//
254// Does not return (it exits) if command-line is fatally flawed.
255//
256void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000257{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600258 baseSamplerBinding.fill(0);
259 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600260 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600261 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700262 baseSsboBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600263
John Kessenich38f3b892013-09-06 19:52:57 +0000264 ExecutableName = argv[0];
265 NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0
John Kessenichfd305422014-06-05 16:30:53 +0000266 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600267 for (int w = 0; w < NumWorkItems; ++w)
268 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000269
John Kessenich2b07c7e2013-07-31 18:44:13 +0000270 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700271 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000272 for (; argc >= 1; argc--, argv++) {
273 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000274 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600275 case '-':
276 {
277 std::string lowerword(argv[0]+2);
278 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
279
280 // handle --word style options
281 if (lowerword == "shift-sampler-bindings" || // synonyms
282 lowerword == "shift-sampler-binding" ||
283 lowerword == "ssb") {
284 ProcessBindingBase(argc, argv, baseSamplerBinding);
285 } else if (lowerword == "shift-texture-bindings" || // synonyms
286 lowerword == "shift-texture-binding" ||
287 lowerword == "stb") {
288 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600289 } else if (lowerword == "shift-image-bindings" || // synonyms
290 lowerword == "shift-image-binding" ||
291 lowerword == "sib") {
292 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600293 } else if (lowerword == "shift-ubo-bindings" || // synonyms
294 lowerword == "shift-ubo-binding" ||
295 lowerword == "sub") {
296 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700297 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
298 lowerword == "shift-ssbo-binding" ||
299 lowerword == "sbb") {
300 ProcessBindingBase(argc, argv, baseSsboBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600301 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700302 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600303 lowerword == "amb") {
304 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600305 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
306 lowerword == "flatten-uniform-array" ||
307 lowerword == "fua") {
308 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600309 } else if (lowerword == "no-storage-format" || // synonyms
310 lowerword == "nsf") {
311 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800312 } else if (lowerword == "variable-name" || // synonyms
313 lowerword == "vn") {
314 Options |= EOptionOutputHexadecimal;
315 variableName = argv[1];
316 if (argc > 0) {
317 argc--;
318 argv++;
319 } else
320 Error("no <C-variable-name> provided for --variable-name");
321 break;
322 }
323 else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600324 lowerword == "sep") {
325 sourceEntryPointName = argv[1];
326 if (argc > 0) {
327 argc--;
328 argv++;
329 } else
330 Error("no <entry-point> provided for --source-entrypoint");
331 break;
John Kessenich906cc212016-12-09 19:22:20 -0700332 } else if (lowerword == "keep-uncalled" || // synonyms
333 lowerword == "ku") {
334 Options |= EOptionKeepUncalled;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600335 } else {
336 usage();
337 }
338 }
339 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000340 case 'H':
341 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600342 if ((Options & EOptionSpv) == 0) {
343 // default to Vulkan
344 Options |= EOptionSpv;
345 Options |= EOptionVulkanRules;
346 Options |= EOptionLinkProgram;
347 }
348 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000349 case 'V':
350 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600351 Options |= EOptionVulkanRules;
352 Options |= EOptionLinkProgram;
353 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400354 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400355 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400356 if (argc > 0) {
357 argc--;
358 argv++;
359 }
360 else
Dan Baker5afdd782016-08-11 17:53:57 -0400361 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400362 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600363 case 'G':
364 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000365 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600366 // undo a -H default to Vulkan
367 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000368 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000369 case 'E':
370 Options |= EOptionOutputPreprocessed;
371 break;
John Kessenich05a70632013-09-17 19:26:08 +0000372 case 'c':
373 Options |= EOptionDumpConfig;
374 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600375 case 'C':
376 Options |= EOptionCascadingErrors;
377 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000378 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000379 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000380 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700381 case 'D':
382 Options |= EOptionReadHlsl;
383 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700384 case 'e':
385 // HLSL todo: entry point handle needs much more sophistication.
386 // This is okay for one compilation unit with one entry point.
387 entryPointName = argv[1];
388 if (argc > 0) {
389 argc--;
390 argv++;
391 } else
392 Error("no <entry-point> provided for -e");
393 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600394 case 'h':
395 usage();
396 break;
John Kessenich05a70632013-09-17 19:26:08 +0000397 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000398 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000399 break;
400 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000401 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000402 break;
403 case 'm':
404 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000405 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600406 case 'o':
407 binaryFileName = argv[1];
408 if (argc > 0) {
409 argc--;
410 argv++;
411 } else
412 Error("no <file> provided for -o");
413 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000414 case 'q':
415 Options |= EOptionDumpReflection;
416 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000417 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000418 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000419 break;
420 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000421 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000422 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000423 case 't':
424 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000425 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000426 #endif
427 break;
John Kessenich319de232013-12-04 04:43:40 +0000428 case 'v':
429 Options |= EOptionDumpVersions;
430 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000431 case 'w':
432 Options |= EOptionSuppressWarnings;
433 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500434 case 'x':
435 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500436 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000437 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600438 usage();
439 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000440 }
John Kessenich38f3b892013-09-06 19:52:57 +0000441 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000442 std::string name(argv[0]);
443 if (! SetConfigFile(name)) {
444 Work[argc] = new glslang::TWorkItem(name);
445 Worklist.add(Work[argc]);
446 }
John Kessenich38f3b892013-09-06 19:52:57 +0000447 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000448 }
449
John Kessenich68d78fd2015-07-12 19:28:10 -0600450 // Make sure that -E is not specified alongside linking (which includes SPV generation)
451 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
452 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000453
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500454 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600455 if (binaryFileName && (Options & EOptionSpv) == 0)
456 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600457
458 if ((Options & EOptionFlattenUniformArrays) != 0 &&
459 (Options & EOptionReadHlsl) == 0)
460 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000461}
462
John Kessenich68d78fd2015-07-12 19:28:10 -0600463//
464// Translate the meaningful subset of command-line options to parser-behavior options.
465//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000466void SetMessageOptions(EShMessages& messages)
467{
468 if (Options & EOptionRelaxedErrors)
469 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
470 if (Options & EOptionIntermediate)
471 messages = (EShMessages)(messages | EShMsgAST);
472 if (Options & EOptionSuppressWarnings)
473 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600474 if (Options & EOptionSpv)
475 messages = (EShMessages)(messages | EShMsgSpvRules);
476 if (Options & EOptionVulkanRules)
477 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400478 if (Options & EOptionOutputPreprocessed)
479 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700480 if (Options & EOptionReadHlsl)
481 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600482 if (Options & EOptionCascadingErrors)
483 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700484 if (Options & EOptionKeepUncalled)
485 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000486}
487
John Kessenich68d78fd2015-07-12 19:28:10 -0600488//
John Kessenich69f4b512013-09-04 21:19:27 +0000489// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000490//
491// Return 0 for failure, 1 for success.
492//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800493unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000494{
John Kessenich38f3b892013-09-06 19:52:57 +0000495 glslang::TWorkItem* workItem;
496 while (Worklist.remove(workItem)) {
497 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000499 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000500
John Kessenichb0a7eb52013-11-07 17:44:20 +0000501 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000502
John Kessenich94a81fb2013-08-31 02:41:30 +0000503 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000504 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000505
506 ShDestruct(compiler);
507 }
508
509 return 0;
510}
511
John Kessenich6626cad2015-06-19 05:14:19 +0000512// Outputs the given string, but only if it is non-null and non-empty.
513// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400514void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000515{
516 if (str && str[0]) {
517 puts(str);
518 }
519}
520
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400521// Outputs the given string to stderr, but only if it is non-null and non-empty.
522// This prevents erroneous newlines from appearing.
523void StderrIfNonEmpty(const char* str)
524{
525 if (str && str[0]) {
526 fprintf(stderr, "%s\n", str);
527 }
528}
529
John Kessenichc57b2a92016-01-16 15:30:03 -0700530// Simple bundling of what makes a compilation unit for ease in passing around,
531// and separation of handling file IO versus API (programmatic) compilation.
532struct ShaderCompUnit {
533 EShLanguage stage;
534 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400535 char** text; // memory owned/managed externally
536 const char* fileNameList[1];
537
John Kessenich219b0252016-08-23 17:51:13 -0600538 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400539 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
540 {
541 stage = istage;
542 fileName = ifileName;
543 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600544 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400545 }
546
547 ShaderCompUnit(const ShaderCompUnit &rhs)
548 {
549 stage = rhs.stage;
550 fileName = rhs.fileName;
551 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600552 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400553 }
554
John Kessenichc57b2a92016-01-16 15:30:03 -0700555};
556
John Kessenich69f4b512013-09-04 21:19:27 +0000557//
John Kessenichc57b2a92016-01-16 15:30:03 -0700558// For linking mode: Will independently parse each compilation unit, but then put them
559// in the same program and link them together, making at most one linked module per
560// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000561//
562// Uses the new C++ interface instead of the old handle-based interface.
563//
John Kessenichc57b2a92016-01-16 15:30:03 -0700564
565void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000566{
567 // keep track of what to free
568 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700569
John Kessenich69f4b512013-09-04 21:19:27 +0000570 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000571 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000572
John Kessenich69f4b512013-09-04 21:19:27 +0000573 //
574 // Per-shader processing...
575 //
576
John Kessenich5b0f13a2013-11-01 03:08:40 +0000577 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100578 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
579 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700580 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400581 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700582 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
583 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600584 if (sourceEntryPointName)
585 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600586
587 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
588 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600589 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600590 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700591 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600592 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600593 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600594
595 if (Options & EOptionAutoMapBindings)
596 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700597
John Kessenich69f4b512013-09-04 21:19:27 +0000598 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600599
John Kessenichc555ddd2015-06-17 02:38:44 +0000600 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000601
John Kessenichc555ddd2015-06-17 02:38:44 +0000602 if (Options & EOptionOutputPreprocessed) {
603 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700604 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400605 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500606 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400607 PutsIfNonEmpty(str.c_str());
608 } else {
609 CompileFailed = true;
610 }
611 StderrIfNonEmpty(shader->getInfoLog());
612 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000613 continue;
614 }
615 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000616 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000617
John Kessenich69f4b512013-09-04 21:19:27 +0000618 program.addShader(shader);
619
John Kessenichc57b2a92016-01-16 15:30:03 -0700620 if (! (Options & EOptionSuppressInfolog) &&
621 ! (Options & EOptionMemoryLeakMode)) {
622 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400623 PutsIfNonEmpty(shader->getInfoLog());
624 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000625 }
John Kessenich69f4b512013-09-04 21:19:27 +0000626 }
627
628 //
629 // Program-level processing...
630 //
631
John Kessenich4d65ee32016-03-12 18:17:47 -0700632 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600633 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000634 LinkFailed = true;
635
steve-lunarg9ae34742016-10-05 13:40:13 -0600636 // Map IO
637 if (Options & EOptionSpv) {
638 if (!program.mapIO())
639 LinkFailed = true;
640 }
John Kessenichecba76f2017-01-06 00:34:48 -0700641
John Kessenich4d65ee32016-03-12 18:17:47 -0700642 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700643 if (! (Options & EOptionSuppressInfolog) &&
644 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400645 PutsIfNonEmpty(program.getInfoLog());
646 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000647 }
648
John Kessenich4d65ee32016-03-12 18:17:47 -0700649 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000650 if (Options & EOptionDumpReflection) {
651 program.buildReflection();
652 program.dumpReflection();
653 }
654
John Kessenich4d65ee32016-03-12 18:17:47 -0700655 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000656 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000657 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600658 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000659 else {
660 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000661 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000662 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400663 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400664 spv::SpvBuildLogger logger;
665 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700666
667 // Dump the spv to a file or stdout, etc., but only if not doing
668 // memory/perf testing, as it's not internal to programmatic use.
669 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500670 printf("%s", logger.getAllMessages().c_str());
671 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700672 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500673 } else {
674 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
675 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700676 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700677 spv::Disassemble(std::cout, spirv);
678 }
John Kessenichacba7722015-03-04 03:48:38 +0000679 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000680 }
John Kessenich92f90382014-07-28 04:21:04 +0000681 }
682 }
683 }
684
John Kessenich5b0f13a2013-11-01 03:08:40 +0000685 // Free everything up, program has to go before the shaders
686 // because it might have merged stuff from the shaders, and
687 // the stuff from the shaders has to have its destructors called
688 // before the pools holding the memory in the shaders is freed.
689 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000690 while (shaders.size() > 0) {
691 delete shaders.back();
692 shaders.pop_back();
693 }
John Kessenich69f4b512013-09-04 21:19:27 +0000694}
695
John Kessenichc57b2a92016-01-16 15:30:03 -0700696//
697// Do file IO part of compile and link, handing off the pure
698// API/programmatic mode to CompileAndLinkShaderUnits(), which can
699// be put in a loop for testing memory footprint and performance.
700//
701// This is just for linking mode: meaning all the shaders will be put into the
702// the same program linked together.
703//
704// This means there are a limited number of work items (not multi-threading mode)
705// and that the point is testing at the linking level. Hence, to enable
706// performance and memory testing, the actual compile/link can be put in
707// a loop, independent of processing the work items and file IO.
708//
709void CompileAndLinkShaderFiles()
710{
711 std::vector<ShaderCompUnit> compUnits;
712
713 // Transfer all the work items from to a simple list of
714 // of compilation units. (We don't care about the thread
715 // work-item distribution properties in this path, which
716 // is okay due to the limited number of shaders, know since
717 // they are all getting linked together.)
718 glslang::TWorkItem* workItem;
719 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200720 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700721 FindLanguage(workItem->name),
722 workItem->name,
723 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200724 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700725
726 if (! compUnit.text) {
727 usage();
728 return;
729 }
730
731 compUnits.push_back(compUnit);
732 }
733
734 // Actual call to programmatic processing of compile and link,
735 // in a loop for testing memory and performance. This part contains
736 // all the perf/memory that a programmatic consumer will care about.
737 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
738 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
739 CompileAndLinkShaderUnits(compUnits);
740
741 if (Options & EOptionMemoryLeakMode)
742 glslang::OS_DumpMemoryCounters();
743 }
744
rdb32084e82016-02-23 22:17:38 +0100745 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
746 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700747}
748
John Kessenicha0af4732012-12-12 21:15:54 +0000749int C_DECL main(int argc, char* argv[])
750{
John Kessenich68d78fd2015-07-12 19:28:10 -0600751 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000752
John Kessenich05a70632013-09-17 19:26:08 +0000753 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500754 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000755 if (Worklist.empty())
756 return ESuccess;
757 }
758
John Kessenich0da9eaa2015-08-01 17:10:02 -0600759 if (Options & EOptionDumpVersions) {
760 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000761 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
762 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600763 std::string spirvVersion;
764 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600765 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600766 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700767 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600768 printf("GL_KHR_vulkan_glsl version %d\n", 100);
769 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000770 if (Worklist.empty())
771 return ESuccess;
772 }
773
John Kessenich05a70632013-09-17 19:26:08 +0000774 if (Worklist.empty()) {
775 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000776 }
777
778 ProcessConfigFile();
779
John Kessenich69f4b512013-09-04 21:19:27 +0000780 //
781 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000782 // 1) linking all arguments together, single-threaded, new C++ interface
783 // 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 +0000784 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000785 if (Options & EOptionLinkProgram ||
786 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000787 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700788 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000789 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500790 for (int w = 0; w < NumWorkItems; ++w) {
791 if (Work[w]) {
792 delete Work[w];
793 }
794 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000795 } else {
796 ShInitialize();
797
John Kessenich38f3b892013-09-06 19:52:57 +0000798 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000799
John Kessenich38f3b892013-09-06 19:52:57 +0000800 if (Options & EOptionMultiThreaded) {
801 const int NumThreads = 16;
802 void* threads[NumThreads];
803 for (int t = 0; t < NumThreads; ++t) {
804 threads[t] = glslang::OS_CreateThread(&CompileShaders);
805 if (! threads[t]) {
806 printf("Failed to create thread\n");
807 return EFailThreadCreate;
808 }
John Kessenicha0af4732012-12-12 21:15:54 +0000809 }
John Kessenich38f3b892013-09-06 19:52:57 +0000810 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000811 } else
812 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000813
814 // Print out all the resulting infologs
815 for (int w = 0; w < NumWorkItems; ++w) {
816 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600817 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400818 PutsIfNonEmpty(Work[w]->name.c_str());
819 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000820 delete Work[w];
821 }
822 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000823
824 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000825 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000826
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500827 delete[] Work;
828
John Kessenichc999ba22013-11-07 23:33:24 +0000829 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000830 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000831 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000832 return EFailLink;
833
834 return 0;
835}
836
837//
838// Deduce the language from the filename. Files must end in one of the
839// following extensions:
840//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000841// .vert = vertex
842// .tesc = tessellation control
843// .tese = tessellation evaluation
844// .geom = geometry
845// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000846// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000847//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600848EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000849{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600850 size_t ext = 0;
851
852 // Search for a suffix on a filename: e.g, "myfile.frag". If given
853 // the suffix directly, we skip looking the '.'
854 if (parseSuffix) {
855 ext = name.rfind('.');
856 if (ext == std::string::npos) {
857 usage();
858 return EShLangVertex;
859 }
860 ++ext;
John Kessenicha0af4732012-12-12 21:15:54 +0000861 }
862
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600863 std::string suffix = name.substr(ext, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400864 if (shaderStageName)
865 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400866
John Kessenich2b07c7e2013-07-31 18:44:13 +0000867 if (suffix == "vert")
868 return EShLangVertex;
869 else if (suffix == "tesc")
870 return EShLangTessControl;
871 else if (suffix == "tese")
872 return EShLangTessEvaluation;
873 else if (suffix == "geom")
874 return EShLangGeometry;
875 else if (suffix == "frag")
876 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000877 else if (suffix == "comp")
878 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000879
880 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000881 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000882}
883
John Kessenicha0af4732012-12-12 21:15:54 +0000884//
John Kessenichecba76f2017-01-06 00:34:48 -0700885// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000886// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000887//
John Kessenich51cdd902014-02-18 23:37:57 +0000888void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000889{
John Kessenichca3457f2015-05-18 01:59:45 +0000890 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000891 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000892 if (! shaderStrings) {
893 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000894 }
895
John Kessenich41cf6b52013-06-25 18:10:05 +0000896 int* lengths = new int[NumShaderStrings];
897
898 // move to length-based strings, rather than null-terminated strings
899 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000900 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000901
John Kessenichc999ba22013-11-07 23:33:24 +0000902 if (! shaderStrings) {
903 CompileFailed = true;
904 return;
905 }
John Kessenicha0af4732012-12-12 21:15:54 +0000906
John Kessenich52ac67e2013-05-05 23:46:22 +0000907 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000908 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700909
John Kessenich94a81fb2013-08-31 02:41:30 +0000910 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
911 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700912 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000913 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700914 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700915 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000916 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700917 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
918 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000919 }
John Kessenicha0af4732012-12-12 21:15:54 +0000920
John Kessenich94a81fb2013-08-31 02:41:30 +0000921 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000922 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000923 }
John Kessenicha0af4732012-12-12 21:15:54 +0000924
John Kessenich41cf6b52013-06-25 18:10:05 +0000925 delete [] lengths;
926 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000927
John Kessenichc999ba22013-11-07 23:33:24 +0000928 if (ret == 0)
929 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000930}
931
John Kessenicha0af4732012-12-12 21:15:54 +0000932//
933// print usage to stdout
934//
935void usage()
936{
John Kessenich319de232013-12-04 04:43:40 +0000937 printf("Usage: glslangValidator [option]... [file]...\n"
938 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000939 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600940 " .conf to provide an optional config file that replaces the default configuration\n"
941 " (see -c option below for generating a template)\n"
942 " .vert for a vertex shader\n"
943 " .tesc for a tessellation control shader\n"
944 " .tese for a tessellation evaluation shader\n"
945 " .geom for a geometry shader\n"
946 " .frag for a fragment shader\n"
947 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000948 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000949 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000950 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000951 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600952 "Each option must be specified separately.\n"
953 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
954 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600955 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
956 " default file name is <stage>.spv (-o overrides this)\n"
957 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400958 " -E print pre-processed GLSL; cannot be used with -l;\n"
959 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400960 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
Dan Baker895275e2016-08-11 14:55:49 -0400961 " valid choices are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600962 " -c configuration dump;\n"
963 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600964 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600965 " -d default to desktop (#version 110) when there is no shader #version\n"
966 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700967 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700968 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600969 " -h print this usage message\n"
970 " -i intermediate tree (glslang AST) is printed out\n"
971 " -l link all input files together to form a single module\n"
972 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500973 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600974 " -q dump reflection query database\n"
975 " -r relaxed semantic error-checking mode\n"
976 " -s silent mode\n"
977 " -t multi-threaded mode\n"
978 " -v print version strings\n"
979 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500980 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600981 "\n"
982 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
983 " --ssb [stage] num synonym for --shift-sampler-binding\n"
984 "\n"
985 " --shift-texture-binding [stage] num set base binding number for textures\n"
986 " --stb [stage] num synonym for --shift-texture-binding\n"
987 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600988 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
989 " --sib [stage] num synonym for --shift-image-binding\n"
990 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600991 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
992 " --sub [stage] num synonym for --shift-UBO-binding\n"
993 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -0700994 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
995 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
996 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600997 " --auto-map-bindings automatically bind uniform variables without\n"
998 " explicit bindings.\n"
999 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001000 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001001 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001002 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001003 "\n"
1004 " --no-storage-format use Unknown image format\n"
1005 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001006 "\n"
1007 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1008 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001009 "\n"
1010 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1011 " --ku synonym for --keep-uncalled\n"
John Kessenich8f674e82017-02-18 09:45:40 -07001012 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name> initialized with the shader binary code.\n"
1013 " --vn <name> synonym for --variable-name <name>.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001014 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001015
1016 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001017}
1018
John Kessenich3ce4e592014-10-06 19:57:34 +00001019#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001020
1021#include <errno.h>
1022
1023int fopen_s(
1024 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001025 const char* filename,
1026 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001027)
1028{
1029 if (!pFile || !filename || !mode) {
1030 return EINVAL;
1031 }
1032
1033 FILE* f = fopen(filename, mode);
1034 if (! f) {
1035 if (errno != 0) {
1036 return errno;
1037 } else {
1038 return ENOENT;
1039 }
1040 }
1041 *pFile = f;
1042
1043 return 0;
1044}
1045
1046#endif
1047
John Kessenicha0af4732012-12-12 21:15:54 +00001048//
1049// Malloc a string of sufficient size and read a string into it.
1050//
John Kessenichecba76f2017-01-06 00:34:48 -07001051char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001052{
John Kessenichb3297152015-07-11 18:01:03 -06001053 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001054 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001055
John Kessenicha0af4732012-12-12 21:15:54 +00001056 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001057 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1058 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001059
John Kessenich68d78fd2015-07-12 19:28:10 -06001060 if (errorCode || in == nullptr)
1061 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001062
John Kessenicha0af4732012-12-12 21:15:54 +00001063 while (fgetc(in) != EOF)
1064 count++;
1065
John Kessenichd6c72a42014-08-18 19:42:35 +00001066 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001067
John Kessenichb3297152015-07-11 18:01:03 -06001068 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001069 if (! fdata)
1070 Error("can't allocate memory");
1071
John Kessenichb3297152015-07-11 18:01:03 -06001072 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001073 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001074 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001075 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001076
John Kessenicha0af4732012-12-12 21:15:54 +00001077 fdata[count] = '\0';
1078 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001079
John Kessenichea869fb2013-10-28 18:12:06 +00001080 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001081 // recover from empty file
1082 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001083 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001084 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001085 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001086
John Kessenichb3297152015-07-11 18:01:03 -06001087 return return_data;
1088 } else
1089 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1090
1091 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001092 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001093 int ptr_len = 0;
1094 int i = 0;
1095 while (count > 0) {
1096 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1097 memcpy(return_data[i], fdata + ptr_len, len);
1098 return_data[i][len] = '\0';
1099 count -= len;
1100 ptr_len += len;
1101 if (count < len) {
1102 if (count == 0) {
1103 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001104 break;
1105 }
John Kessenichb3297152015-07-11 18:01:03 -06001106 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001107 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001108 ++i;
1109 }
John Kessenichb3297152015-07-11 18:01:03 -06001110
1111 free(fdata);
1112
John Kessenicha0af4732012-12-12 21:15:54 +00001113 return return_data;
1114}
1115
John Kessenich51cdd902014-02-18 23:37:57 +00001116void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001117{
John Kessenichb3297152015-07-11 18:01:03 -06001118 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001119 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001120
1121 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001122}
1123
John Kessenich54d8cda2013-02-11 22:36:01 +00001124void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001125{
John Kessenichfae38ee2015-06-10 23:23:12 +00001126 if (num >= 0 )
1127 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1128 else
1129 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001130}