blob: 60dbc4dae5f83dc64de6a28732b345555101ecf6 [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>
Juan Lopeza558b262017-04-02 23:04:00 +020054#include <memory>
55#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000056
baldurk876a0e32015-11-16 18:03:28 +010057#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000058
59extern "C" {
60 SH_IMPORT_EXPORT void ShOutputHtml();
61}
62
John Kessenich94a81fb2013-08-31 02:41:30 +000063// Command-line options
64enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070065 EOptionNone = 0,
66 EOptionIntermediate = (1 << 0),
67 EOptionSuppressInfolog = (1 << 1),
68 EOptionMemoryLeakMode = (1 << 2),
69 EOptionRelaxedErrors = (1 << 3),
70 EOptionGiveWarnings = (1 << 4),
71 EOptionLinkProgram = (1 << 5),
72 EOptionMultiThreaded = (1 << 6),
73 EOptionDumpConfig = (1 << 7),
74 EOptionDumpReflection = (1 << 8),
75 EOptionSuppressWarnings = (1 << 9),
76 EOptionDumpVersions = (1 << 10),
77 EOptionSpv = (1 << 11),
78 EOptionHumanReadableSpv = (1 << 12),
79 EOptionVulkanRules = (1 << 13),
80 EOptionDefaultDesktop = (1 << 14),
81 EOptionOutputPreprocessed = (1 << 15),
82 EOptionOutputHexadecimal = (1 << 16),
83 EOptionReadHlsl = (1 << 17),
84 EOptionCascadingErrors = (1 << 18),
85 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060086 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070087 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070088 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060089 EOptionHlslOffsets = (1 << 23),
John Kessenich94a81fb2013-08-31 02:41:30 +000090};
91
John Kessenicha0af4732012-12-12 21:15:54 +000092//
John Kessenich68d78fd2015-07-12 19:28:10 -060093// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000094//
95enum TFailCode {
96 ESuccess = 0,
97 EFailUsage,
98 EFailCompile,
99 EFailLink,
100 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000101 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000102 EFailLinkerCreate
103};
104
105//
John Kessenich68d78fd2015-07-12 19:28:10 -0600106// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000107//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600108EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000109void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000110void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000111void FreeFileData(char** data);
112char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000113void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000114
John Kessenichc999ba22013-11-07 23:33:24 +0000115// Globally track if any compile or link failure.
116bool CompileFailed = false;
117bool LinkFailed = false;
118
John Kessenich05a70632013-09-17 19:26:08 +0000119// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600120// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000121int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000122
John Kessenich05a70632013-09-17 19:26:08 +0000123TBuiltInResource Resources;
124std::string ConfigFile;
125
John Kessenicha0af4732012-12-12 21:15:54 +0000126//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600127// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000128//
129void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000130{
John Kessenich05a70632013-09-17 19:26:08 +0000131 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000132 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000133 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000134 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000135 if (configStrings)
136 config = *configStrings;
137 else {
138 printf("Error opening configuration file; will instead use the default configuration\n");
139 usage();
140 }
141 }
142
143 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500144 Resources = glslang::DefaultTBuiltInResource;
145 return;
John Kessenich05a70632013-09-17 19:26:08 +0000146 }
147
Lei Zhang1b141722016-05-19 13:50:49 -0400148 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000149
John Kessenich05a70632013-09-17 19:26:08 +0000150 if (configStrings)
151 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500152 else
153 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000154}
155
John Kessenich94a81fb2013-08-31 02:41:30 +0000156int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600157const char* ExecutableName = nullptr;
158const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700159const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600160const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400161const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800162const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600163
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600164std::array<unsigned int, EShLangCount> baseSamplerBinding;
165std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600166std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600167std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700168std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600169
John Kessenich68d78fd2015-07-12 19:28:10 -0600170//
171// Create the default name for saving a binary if -o is not provided.
172//
173const char* GetBinaryName(EShLanguage stage)
174{
175 const char* name;
176 if (binaryFileName == nullptr) {
177 switch (stage) {
178 case EShLangVertex: name = "vert.spv"; break;
179 case EShLangTessControl: name = "tesc.spv"; break;
180 case EShLangTessEvaluation: name = "tese.spv"; break;
181 case EShLangGeometry: name = "geom.spv"; break;
182 case EShLangFragment: name = "frag.spv"; break;
183 case EShLangCompute: name = "comp.spv"; break;
184 default: name = "unknown"; break;
185 }
186 } else
187 name = binaryFileName;
188
189 return name;
190}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000191
John Kessenich05a70632013-09-17 19:26:08 +0000192//
193// *.conf => this is a config file that can set limits/resources
194//
195bool SetConfigFile(const std::string& name)
196{
197 if (name.size() < 5)
198 return false;
199
John Kessenich4c706852013-10-11 16:28:43 +0000200 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000201 ConfigFile = name;
202 return true;
203 }
204
205 return false;
206}
207
John Kessenich68d78fd2015-07-12 19:28:10 -0600208//
209// Give error and exit with failure code.
210//
211void Error(const char* message)
212{
213 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
214 exit(EFailUsage);
215}
216
217//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600218// Process an optional binding base of the form:
219// --argname [stage] base
220// Where stage is one of the forms accepted by FindLanguage, and base is an integer
221//
222void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
223{
224 if (argc < 2)
225 usage();
226
227 if (!isdigit(argv[1][0])) {
228 if (argc < 3) // this form needs one more argument
229 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700230
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600231 // Parse form: --argname stage base
232 const EShLanguage lang = FindLanguage(argv[1], false);
233 base[lang] = atoi(argv[2]);
234 argc-= 2;
235 argv+= 2;
236 } else {
237 // Parse form: --argname base
238 for (int lang=0; lang<EShLangCount; ++lang)
239 base[lang] = atoi(argv[1]);
240
241 argc--;
242 argv++;
243 }
244}
245
246//
John Kessenich68d78fd2015-07-12 19:28:10 -0600247// Do all command-line argument parsing. This includes building up the work-items
248// to be processed later, and saving all the command-line options.
249//
250// Does not return (it exits) if command-line is fatally flawed.
251//
Juan Lopeza558b262017-04-02 23:04:00 +0200252void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000253{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600254 baseSamplerBinding.fill(0);
255 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600256 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600257 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700258 baseSsboBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600259
John Kessenich38f3b892013-09-06 19:52:57 +0000260 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200261 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000262
John Kessenich2b07c7e2013-07-31 18:44:13 +0000263 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700264 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000265 for (; argc >= 1; argc--, argv++) {
266 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000267 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600268 case '-':
269 {
270 std::string lowerword(argv[0]+2);
271 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
272
273 // handle --word style options
274 if (lowerword == "shift-sampler-bindings" || // synonyms
275 lowerword == "shift-sampler-binding" ||
276 lowerword == "ssb") {
277 ProcessBindingBase(argc, argv, baseSamplerBinding);
278 } else if (lowerword == "shift-texture-bindings" || // synonyms
279 lowerword == "shift-texture-binding" ||
280 lowerword == "stb") {
281 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600282 } else if (lowerword == "shift-image-bindings" || // synonyms
283 lowerword == "shift-image-binding" ||
284 lowerword == "sib") {
285 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600286 } else if (lowerword == "shift-ubo-bindings" || // synonyms
287 lowerword == "shift-ubo-binding" ||
288 lowerword == "sub") {
289 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700290 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
291 lowerword == "shift-ssbo-binding" ||
292 lowerword == "sbb") {
293 ProcessBindingBase(argc, argv, baseSsboBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600294 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700295 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600296 lowerword == "amb") {
297 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600298 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
299 lowerword == "flatten-uniform-array" ||
300 lowerword == "fua") {
301 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600302 } else if (lowerword == "no-storage-format" || // synonyms
303 lowerword == "nsf") {
304 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800305 } else if (lowerword == "variable-name" || // synonyms
306 lowerword == "vn") {
307 Options |= EOptionOutputHexadecimal;
308 variableName = argv[1];
309 if (argc > 0) {
310 argc--;
311 argv++;
312 } else
313 Error("no <C-variable-name> provided for --variable-name");
314 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600315 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600316 lowerword == "sep") {
317 sourceEntryPointName = argv[1];
318 if (argc > 0) {
319 argc--;
320 argv++;
321 } else
322 Error("no <entry-point> provided for --source-entrypoint");
323 break;
John Kessenich906cc212016-12-09 19:22:20 -0700324 } else if (lowerword == "keep-uncalled" || // synonyms
325 lowerword == "ku") {
326 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600327 } else if (lowerword == "hlsl-offsets") {
328 Options |= EOptionHlslOffsets;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600329 } else {
330 usage();
331 }
332 }
333 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000334 case 'H':
335 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600336 if ((Options & EOptionSpv) == 0) {
337 // default to Vulkan
338 Options |= EOptionSpv;
339 Options |= EOptionVulkanRules;
340 Options |= EOptionLinkProgram;
341 }
342 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000343 case 'V':
344 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600345 Options |= EOptionVulkanRules;
346 Options |= EOptionLinkProgram;
347 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400348 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400349 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400350 if (argc > 0) {
351 argc--;
352 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600353 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400354 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400355 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600356 case 'G':
357 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000358 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600359 // undo a -H default to Vulkan
360 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000361 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000362 case 'E':
363 Options |= EOptionOutputPreprocessed;
364 break;
John Kessenich05a70632013-09-17 19:26:08 +0000365 case 'c':
366 Options |= EOptionDumpConfig;
367 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600368 case 'C':
369 Options |= EOptionCascadingErrors;
370 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000371 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000372 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000373 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700374 case 'D':
375 Options |= EOptionReadHlsl;
376 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700377 case 'e':
378 // HLSL todo: entry point handle needs much more sophistication.
379 // This is okay for one compilation unit with one entry point.
380 entryPointName = argv[1];
381 if (argc > 0) {
382 argc--;
383 argv++;
384 } else
385 Error("no <entry-point> provided for -e");
386 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600387 case 'h':
388 usage();
389 break;
John Kessenich05a70632013-09-17 19:26:08 +0000390 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000391 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000392 break;
393 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000394 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000395 break;
396 case 'm':
397 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000398 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600399 case 'o':
400 binaryFileName = argv[1];
401 if (argc > 0) {
402 argc--;
403 argv++;
404 } else
405 Error("no <file> provided for -o");
406 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000407 case 'q':
408 Options |= EOptionDumpReflection;
409 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000410 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000411 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000412 break;
413 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000414 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000415 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000416 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200417 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000418 break;
John Kessenich319de232013-12-04 04:43:40 +0000419 case 'v':
420 Options |= EOptionDumpVersions;
421 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000422 case 'w':
423 Options |= EOptionSuppressWarnings;
424 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500425 case 'x':
426 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500427 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000428 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600429 usage();
430 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000431 }
John Kessenich38f3b892013-09-06 19:52:57 +0000432 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000433 std::string name(argv[0]);
434 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200435 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000436 }
John Kessenich38f3b892013-09-06 19:52:57 +0000437 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000438 }
439
John Kessenich68d78fd2015-07-12 19:28:10 -0600440 // Make sure that -E is not specified alongside linking (which includes SPV generation)
441 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
442 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000443
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500444 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600445 if (binaryFileName && (Options & EOptionSpv) == 0)
446 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600447
448 if ((Options & EOptionFlattenUniformArrays) != 0 &&
449 (Options & EOptionReadHlsl) == 0)
450 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000451}
452
John Kessenich68d78fd2015-07-12 19:28:10 -0600453//
454// Translate the meaningful subset of command-line options to parser-behavior options.
455//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000456void SetMessageOptions(EShMessages& messages)
457{
458 if (Options & EOptionRelaxedErrors)
459 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
460 if (Options & EOptionIntermediate)
461 messages = (EShMessages)(messages | EShMsgAST);
462 if (Options & EOptionSuppressWarnings)
463 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600464 if (Options & EOptionSpv)
465 messages = (EShMessages)(messages | EShMsgSpvRules);
466 if (Options & EOptionVulkanRules)
467 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400468 if (Options & EOptionOutputPreprocessed)
469 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700470 if (Options & EOptionReadHlsl)
471 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600472 if (Options & EOptionCascadingErrors)
473 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700474 if (Options & EOptionKeepUncalled)
475 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600476 if (Options & EOptionHlslOffsets)
477 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000478}
479
John Kessenich68d78fd2015-07-12 19:28:10 -0600480//
John Kessenich69f4b512013-09-04 21:19:27 +0000481// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000482//
Juan Lopeza558b262017-04-02 23:04:00 +0200483void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000484{
John Kessenich38f3b892013-09-06 19:52:57 +0000485 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200486 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000487 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000488 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200489 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000490
John Kessenichb0a7eb52013-11-07 17:44:20 +0000491 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000492
John Kessenich94a81fb2013-08-31 02:41:30 +0000493 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000494 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000495
496 ShDestruct(compiler);
497 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498}
499
John Kessenich6626cad2015-06-19 05:14:19 +0000500// Outputs the given string, but only if it is non-null and non-empty.
501// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400502void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000503{
504 if (str && str[0]) {
505 puts(str);
506 }
507}
508
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400509// Outputs the given string to stderr, but only if it is non-null and non-empty.
510// This prevents erroneous newlines from appearing.
511void StderrIfNonEmpty(const char* str)
512{
513 if (str && str[0]) {
514 fprintf(stderr, "%s\n", str);
515 }
516}
517
John Kessenichc57b2a92016-01-16 15:30:03 -0700518// Simple bundling of what makes a compilation unit for ease in passing around,
519// and separation of handling file IO versus API (programmatic) compilation.
520struct ShaderCompUnit {
521 EShLanguage stage;
522 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400523 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600524 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400525
John Kessenich219b0252016-08-23 17:51:13 -0600526 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400527 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
528 {
529 stage = istage;
530 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600531 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600532 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400533 }
534
535 ShaderCompUnit(const ShaderCompUnit &rhs)
536 {
537 stage = rhs.stage;
538 fileName = rhs.fileName;
539 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600540 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400541 }
542
John Kessenichc57b2a92016-01-16 15:30:03 -0700543};
544
John Kessenich69f4b512013-09-04 21:19:27 +0000545//
John Kessenichc57b2a92016-01-16 15:30:03 -0700546// For linking mode: Will independently parse each compilation unit, but then put them
547// in the same program and link them together, making at most one linked module per
548// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000549//
550// Uses the new C++ interface instead of the old handle-based interface.
551//
John Kessenichc57b2a92016-01-16 15:30:03 -0700552
553void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000554{
555 // keep track of what to free
556 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700557
John Kessenich69f4b512013-09-04 21:19:27 +0000558 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000559 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000560
John Kessenich69f4b512013-09-04 21:19:27 +0000561 //
562 // Per-shader processing...
563 //
564
John Kessenich5b0f13a2013-11-01 03:08:40 +0000565 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100566 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
567 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700568 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400569 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700570 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
571 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600572 if (sourceEntryPointName)
573 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600574
575 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
576 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600577 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600578 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700579 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600580 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600581 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600582
583 if (Options & EOptionAutoMapBindings)
584 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700585
John Kessenich69f4b512013-09-04 21:19:27 +0000586 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600587
John Kessenichc555ddd2015-06-17 02:38:44 +0000588 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000589
John Kessenichc555ddd2015-06-17 02:38:44 +0000590 if (Options & EOptionOutputPreprocessed) {
591 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700592 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400593 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500594 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400595 PutsIfNonEmpty(str.c_str());
596 } else {
597 CompileFailed = true;
598 }
599 StderrIfNonEmpty(shader->getInfoLog());
600 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000601 continue;
602 }
603 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000604 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000605
John Kessenich69f4b512013-09-04 21:19:27 +0000606 program.addShader(shader);
607
John Kessenichc57b2a92016-01-16 15:30:03 -0700608 if (! (Options & EOptionSuppressInfolog) &&
609 ! (Options & EOptionMemoryLeakMode)) {
610 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400611 PutsIfNonEmpty(shader->getInfoLog());
612 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000613 }
John Kessenich69f4b512013-09-04 21:19:27 +0000614 }
615
616 //
617 // Program-level processing...
618 //
619
John Kessenich4d65ee32016-03-12 18:17:47 -0700620 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600621 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000622 LinkFailed = true;
623
steve-lunarg9ae34742016-10-05 13:40:13 -0600624 // Map IO
625 if (Options & EOptionSpv) {
626 if (!program.mapIO())
627 LinkFailed = true;
628 }
John Kessenichecba76f2017-01-06 00:34:48 -0700629
John Kessenich4d65ee32016-03-12 18:17:47 -0700630 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700631 if (! (Options & EOptionSuppressInfolog) &&
632 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400633 PutsIfNonEmpty(program.getInfoLog());
634 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000635 }
636
John Kessenich4d65ee32016-03-12 18:17:47 -0700637 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000638 if (Options & EOptionDumpReflection) {
639 program.buildReflection();
640 program.dumpReflection();
641 }
642
John Kessenich4d65ee32016-03-12 18:17:47 -0700643 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000644 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000645 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600646 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000647 else {
648 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000649 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000650 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400651 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400652 spv::SpvBuildLogger logger;
653 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700654
655 // Dump the spv to a file or stdout, etc., but only if not doing
656 // memory/perf testing, as it's not internal to programmatic use.
657 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500658 printf("%s", logger.getAllMessages().c_str());
659 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700660 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500661 } else {
662 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
663 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700664 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700665 spv::Disassemble(std::cout, spirv);
666 }
John Kessenichacba7722015-03-04 03:48:38 +0000667 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000668 }
John Kessenich92f90382014-07-28 04:21:04 +0000669 }
670 }
671 }
672
John Kessenich5b0f13a2013-11-01 03:08:40 +0000673 // Free everything up, program has to go before the shaders
674 // because it might have merged stuff from the shaders, and
675 // the stuff from the shaders has to have its destructors called
676 // before the pools holding the memory in the shaders is freed.
677 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000678 while (shaders.size() > 0) {
679 delete shaders.back();
680 shaders.pop_back();
681 }
John Kessenich69f4b512013-09-04 21:19:27 +0000682}
683
John Kessenichc57b2a92016-01-16 15:30:03 -0700684//
685// Do file IO part of compile and link, handing off the pure
686// API/programmatic mode to CompileAndLinkShaderUnits(), which can
687// be put in a loop for testing memory footprint and performance.
688//
689// This is just for linking mode: meaning all the shaders will be put into the
690// the same program linked together.
691//
692// This means there are a limited number of work items (not multi-threading mode)
693// and that the point is testing at the linking level. Hence, to enable
694// performance and memory testing, the actual compile/link can be put in
695// a loop, independent of processing the work items and file IO.
696//
Juan Lopeza558b262017-04-02 23:04:00 +0200697void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700698{
699 std::vector<ShaderCompUnit> compUnits;
700
701 // Transfer all the work items from to a simple list of
702 // of compilation units. (We don't care about the thread
703 // work-item distribution properties in this path, which
704 // is okay due to the limited number of shaders, know since
705 // they are all getting linked together.)
706 glslang::TWorkItem* workItem;
707 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200708 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700709 FindLanguage(workItem->name),
710 workItem->name,
711 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200712 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700713
714 if (! compUnit.text) {
715 usage();
716 return;
717 }
718
719 compUnits.push_back(compUnit);
720 }
721
722 // Actual call to programmatic processing of compile and link,
723 // in a loop for testing memory and performance. This part contains
724 // all the perf/memory that a programmatic consumer will care about.
725 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
726 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
727 CompileAndLinkShaderUnits(compUnits);
728
729 if (Options & EOptionMemoryLeakMode)
730 glslang::OS_DumpMemoryCounters();
731 }
732
rdb32084e82016-02-23 22:17:38 +0100733 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
734 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700735}
736
John Kessenicha0af4732012-12-12 21:15:54 +0000737int C_DECL main(int argc, char* argv[])
738{
Juan Lopeza558b262017-04-02 23:04:00 +0200739 // array of unique places to leave the shader names and infologs for the asynchronous compiles
740 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
741 ProcessArguments(workItems, argc, argv);
742
743 glslang::TWorklist workList;
744 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
745 assert(item);
746 workList.add(item.get());
747 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000748
John Kessenich05a70632013-09-17 19:26:08 +0000749 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500750 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200751 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000752 return ESuccess;
753 }
754
John Kessenich0da9eaa2015-08-01 17:10:02 -0600755 if (Options & EOptionDumpVersions) {
756 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000757 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
758 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600759 std::string spirvVersion;
760 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600761 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600762 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700763 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600764 printf("GL_KHR_vulkan_glsl version %d\n", 100);
765 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200766 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000767 return ESuccess;
768 }
769
Juan Lopeza558b262017-04-02 23:04:00 +0200770 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000771 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000772 }
773
774 ProcessConfigFile();
775
John Kessenich69f4b512013-09-04 21:19:27 +0000776 //
777 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000778 // 1) linking all arguments together, single-threaded, new C++ interface
779 // 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 +0000780 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000781 if (Options & EOptionLinkProgram ||
782 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000783 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200784 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000785 glslang::FinalizeProcess();
786 } else {
787 ShInitialize();
788
Juan Lopeza558b262017-04-02 23:04:00 +0200789 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000790
Juan Lopeza558b262017-04-02 23:04:00 +0200791 if (Options & EOptionMultiThreaded)
792 {
793 std::array<std::thread, 16> threads;
794 for (unsigned int t = 0; t < threads.size(); ++t)
795 {
796 threads[t] = std::thread(CompileShaders, std::ref(workList));
797 if (threads[t].get_id() == std::thread::id())
798 {
John Kessenich38f3b892013-09-06 19:52:57 +0000799 printf("Failed to create thread\n");
800 return EFailThreadCreate;
801 }
John Kessenicha0af4732012-12-12 21:15:54 +0000802 }
Juan Lopeza558b262017-04-02 23:04:00 +0200803
804 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000805 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200806 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000807
808 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200809 for (size_t w = 0; w < workItems.size(); ++w) {
810 if (workItems[w]) {
811 if (printShaderNames || workItems[w]->results.size() > 0)
812 PutsIfNonEmpty(workItems[w]->name.c_str());
813 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000814 }
815 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000816
817 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000818 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000819
John Kessenichc999ba22013-11-07 23:33:24 +0000820 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000821 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000822 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000823 return EFailLink;
824
825 return 0;
826}
827
828//
829// Deduce the language from the filename. Files must end in one of the
830// following extensions:
831//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000832// .vert = vertex
833// .tesc = tessellation control
834// .tese = tessellation evaluation
835// .geom = geometry
836// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000837// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000838//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600839EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000840{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600841 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600842 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600843
Dan Bakerc6ede892016-08-11 14:06:06 -0400844 if (shaderStageName)
845 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600846 else {
847 // Search for a suffix on a filename: e.g, "myfile.frag". If given
848 // the suffix directly, we skip looking for the '.'
849 if (parseSuffix) {
850 ext = name.rfind('.');
851 if (ext == std::string::npos) {
852 usage();
853 return EShLangVertex;
854 }
855 ++ext;
856 }
857 suffix = name.substr(ext, std::string::npos);
858 }
dankbaker45d49bc2016-08-08 21:43:07 -0400859
John Kessenich2b07c7e2013-07-31 18:44:13 +0000860 if (suffix == "vert")
861 return EShLangVertex;
862 else if (suffix == "tesc")
863 return EShLangTessControl;
864 else if (suffix == "tese")
865 return EShLangTessEvaluation;
866 else if (suffix == "geom")
867 return EShLangGeometry;
868 else if (suffix == "frag")
869 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000870 else if (suffix == "comp")
871 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000872
873 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000874 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000875}
876
John Kessenicha0af4732012-12-12 21:15:54 +0000877//
John Kessenichecba76f2017-01-06 00:34:48 -0700878// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000879// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000880//
John Kessenich51cdd902014-02-18 23:37:57 +0000881void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000882{
John Kessenichca3457f2015-05-18 01:59:45 +0000883 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000884 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000885 if (! shaderStrings) {
886 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000887 }
888
John Kessenich41cf6b52013-06-25 18:10:05 +0000889 int* lengths = new int[NumShaderStrings];
890
891 // move to length-based strings, rather than null-terminated strings
892 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000893 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000894
John Kessenichc999ba22013-11-07 23:33:24 +0000895 if (! shaderStrings) {
896 CompileFailed = true;
897 return;
898 }
John Kessenicha0af4732012-12-12 21:15:54 +0000899
John Kessenich52ac67e2013-05-05 23:46:22 +0000900 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000901 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700902
John Kessenich94a81fb2013-08-31 02:41:30 +0000903 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
904 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700905 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000906 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700907 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700908 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000909 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700910 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
911 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000912 }
John Kessenicha0af4732012-12-12 21:15:54 +0000913
John Kessenich94a81fb2013-08-31 02:41:30 +0000914 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000915 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000916 }
John Kessenicha0af4732012-12-12 21:15:54 +0000917
John Kessenich41cf6b52013-06-25 18:10:05 +0000918 delete [] lengths;
919 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000920
John Kessenichc999ba22013-11-07 23:33:24 +0000921 if (ret == 0)
922 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000923}
924
John Kessenicha0af4732012-12-12 21:15:54 +0000925//
926// print usage to stdout
927//
928void usage()
929{
John Kessenich319de232013-12-04 04:43:40 +0000930 printf("Usage: glslangValidator [option]... [file]...\n"
931 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000932 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600933 " .conf to provide an optional config file that replaces the default configuration\n"
934 " (see -c option below for generating a template)\n"
935 " .vert for a vertex shader\n"
936 " .tesc for a tessellation control shader\n"
937 " .tese for a tessellation evaluation shader\n"
938 " .geom for a geometry shader\n"
939 " .frag for a fragment shader\n"
940 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000941 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000942 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000943 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000944 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600945 "Each option must be specified separately.\n"
946 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
947 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600948 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
949 " default file name is <stage>.spv (-o overrides this)\n"
950 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400951 " -E print pre-processed GLSL; cannot be used with -l;\n"
952 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -0600953 " -S <stage> uses specified stage rather than parsing the file extension\n"
954 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600955 " -c configuration dump;\n"
956 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600957 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600958 " -d default to desktop (#version 110) when there is no shader #version\n"
959 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700960 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700961 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600962 " -h print this usage message\n"
963 " -i intermediate tree (glslang AST) is printed out\n"
964 " -l link all input files together to form a single module\n"
965 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500966 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600967 " -q dump reflection query database\n"
968 " -r relaxed semantic error-checking mode\n"
969 " -s silent mode\n"
970 " -t multi-threaded mode\n"
971 " -v print version strings\n"
972 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500973 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600974 "\n"
975 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
976 " --ssb [stage] num synonym for --shift-sampler-binding\n"
977 "\n"
978 " --shift-texture-binding [stage] num set base binding number for textures\n"
979 " --stb [stage] num synonym for --shift-texture-binding\n"
980 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600981 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
982 " --sib [stage] num synonym for --shift-image-binding\n"
983 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600984 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
985 " --sub [stage] num synonym for --shift-UBO-binding\n"
986 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -0700987 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
988 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
989 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600990 " --auto-map-bindings automatically bind uniform variables without\n"
991 " explicit bindings.\n"
992 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600993 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -0600994 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600995 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -0600996 "\n"
997 " --no-storage-format use Unknown image format\n"
998 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -0600999 "\n"
1000 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1001 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001002 "\n"
1003 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1004 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001005 "\n"
1006 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1007 " initialized with the shader binary code.\n"
1008 " --vn <name> synonym for --variable-name <name>\n"
1009 "\n"
1010 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1011 " Works independently of source language.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001012 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001013
1014 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001015}
1016
John Kessenich3ce4e592014-10-06 19:57:34 +00001017#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001018
1019#include <errno.h>
1020
1021int fopen_s(
1022 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001023 const char* filename,
1024 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001025)
1026{
1027 if (!pFile || !filename || !mode) {
1028 return EINVAL;
1029 }
1030
1031 FILE* f = fopen(filename, mode);
1032 if (! f) {
1033 if (errno != 0) {
1034 return errno;
1035 } else {
1036 return ENOENT;
1037 }
1038 }
1039 *pFile = f;
1040
1041 return 0;
1042}
1043
1044#endif
1045
John Kessenicha0af4732012-12-12 21:15:54 +00001046//
1047// Malloc a string of sufficient size and read a string into it.
1048//
John Kessenichecba76f2017-01-06 00:34:48 -07001049char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001050{
John Kessenichb3297152015-07-11 18:01:03 -06001051 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001052 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001053
John Kessenicha0af4732012-12-12 21:15:54 +00001054 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001055 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1056 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001057
John Kessenich68d78fd2015-07-12 19:28:10 -06001058 if (errorCode || in == nullptr)
1059 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001060
John Kessenicha0af4732012-12-12 21:15:54 +00001061 while (fgetc(in) != EOF)
1062 count++;
1063
John Kessenichd6c72a42014-08-18 19:42:35 +00001064 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001065
John Kessenichb3297152015-07-11 18:01:03 -06001066 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001067 if (! fdata)
1068 Error("can't allocate memory");
1069
John Kessenichb3297152015-07-11 18:01:03 -06001070 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001071 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001072 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001073 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001074
John Kessenicha0af4732012-12-12 21:15:54 +00001075 fdata[count] = '\0';
1076 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001077
John Kessenichea869fb2013-10-28 18:12:06 +00001078 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001079 // recover from empty file
1080 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001081 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001082 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001083 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001084
John Kessenichb3297152015-07-11 18:01:03 -06001085 return return_data;
1086 } else
1087 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1088
1089 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001090 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001091 int ptr_len = 0;
1092 int i = 0;
1093 while (count > 0) {
1094 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1095 memcpy(return_data[i], fdata + ptr_len, len);
1096 return_data[i][len] = '\0';
1097 count -= len;
1098 ptr_len += len;
1099 if (count < len) {
1100 if (count == 0) {
1101 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001102 break;
1103 }
John Kessenichb3297152015-07-11 18:01:03 -06001104 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001105 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001106 ++i;
1107 }
John Kessenichb3297152015-07-11 18:01:03 -06001108
1109 free(fdata);
1110
John Kessenicha0af4732012-12-12 21:15:54 +00001111 return return_data;
1112}
1113
John Kessenich51cdd902014-02-18 23:37:57 +00001114void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001115{
John Kessenichb3297152015-07-11 18:01:03 -06001116 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001117 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001118
1119 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001120}
1121
John Kessenich54d8cda2013-02-11 22:36:01 +00001122void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001123{
John Kessenichfae38ee2015-06-10 23:23:12 +00001124 if (num >= 0 )
1125 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1126 else
1127 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001128}