blob: 56d6619492356dfeceea4c33b61b32d42d983859 [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 Kessenich94a81fb2013-08-31 02:41:30 +000089};
90
John Kessenicha0af4732012-12-12 21:15:54 +000091//
John Kessenich68d78fd2015-07-12 19:28:10 -060092// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000093//
94enum TFailCode {
95 ESuccess = 0,
96 EFailUsage,
97 EFailCompile,
98 EFailLink,
99 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000100 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000101 EFailLinkerCreate
102};
103
104//
John Kessenich68d78fd2015-07-12 19:28:10 -0600105// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000106//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600107EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000108void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000109void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000110void FreeFileData(char** data);
111char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000112void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000113
John Kessenichc999ba22013-11-07 23:33:24 +0000114// Globally track if any compile or link failure.
115bool CompileFailed = false;
116bool LinkFailed = false;
117
John Kessenich05a70632013-09-17 19:26:08 +0000118// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600119// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000120int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000121
John Kessenich05a70632013-09-17 19:26:08 +0000122TBuiltInResource Resources;
123std::string ConfigFile;
124
John Kessenicha0af4732012-12-12 21:15:54 +0000125//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600126// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000127//
128void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000129{
John Kessenich05a70632013-09-17 19:26:08 +0000130 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000131 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000132 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000133 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000134 if (configStrings)
135 config = *configStrings;
136 else {
137 printf("Error opening configuration file; will instead use the default configuration\n");
138 usage();
139 }
140 }
141
142 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500143 Resources = glslang::DefaultTBuiltInResource;
144 return;
John Kessenich05a70632013-09-17 19:26:08 +0000145 }
146
Lei Zhang1b141722016-05-19 13:50:49 -0400147 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000148
John Kessenich05a70632013-09-17 19:26:08 +0000149 if (configStrings)
150 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500151 else
152 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000153}
154
John Kessenich94a81fb2013-08-31 02:41:30 +0000155int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600156const char* ExecutableName = nullptr;
157const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700158const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600159const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400160const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800161const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600162
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600163std::array<unsigned int, EShLangCount> baseSamplerBinding;
164std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600165std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600166std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700167std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600168
John Kessenich68d78fd2015-07-12 19:28:10 -0600169//
170// Create the default name for saving a binary if -o is not provided.
171//
172const char* GetBinaryName(EShLanguage stage)
173{
174 const char* name;
175 if (binaryFileName == nullptr) {
176 switch (stage) {
177 case EShLangVertex: name = "vert.spv"; break;
178 case EShLangTessControl: name = "tesc.spv"; break;
179 case EShLangTessEvaluation: name = "tese.spv"; break;
180 case EShLangGeometry: name = "geom.spv"; break;
181 case EShLangFragment: name = "frag.spv"; break;
182 case EShLangCompute: name = "comp.spv"; break;
183 default: name = "unknown"; break;
184 }
185 } else
186 name = binaryFileName;
187
188 return name;
189}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000190
John Kessenich05a70632013-09-17 19:26:08 +0000191//
192// *.conf => this is a config file that can set limits/resources
193//
194bool SetConfigFile(const std::string& name)
195{
196 if (name.size() < 5)
197 return false;
198
John Kessenich4c706852013-10-11 16:28:43 +0000199 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000200 ConfigFile = name;
201 return true;
202 }
203
204 return false;
205}
206
John Kessenich68d78fd2015-07-12 19:28:10 -0600207//
208// Give error and exit with failure code.
209//
210void Error(const char* message)
211{
212 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
213 exit(EFailUsage);
214}
215
216//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600217// Process an optional binding base of the form:
218// --argname [stage] base
219// Where stage is one of the forms accepted by FindLanguage, and base is an integer
220//
221void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
222{
223 if (argc < 2)
224 usage();
225
226 if (!isdigit(argv[1][0])) {
227 if (argc < 3) // this form needs one more argument
228 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700229
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600230 // Parse form: --argname stage base
231 const EShLanguage lang = FindLanguage(argv[1], false);
232 base[lang] = atoi(argv[2]);
233 argc-= 2;
234 argv+= 2;
235 } else {
236 // Parse form: --argname base
237 for (int lang=0; lang<EShLangCount; ++lang)
238 base[lang] = atoi(argv[1]);
239
240 argc--;
241 argv++;
242 }
243}
244
245//
John Kessenich68d78fd2015-07-12 19:28:10 -0600246// Do all command-line argument parsing. This includes building up the work-items
247// to be processed later, and saving all the command-line options.
248//
249// Does not return (it exits) if command-line is fatally flawed.
250//
Juan Lopeza558b262017-04-02 23:04:00 +0200251void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000252{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600253 baseSamplerBinding.fill(0);
254 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600255 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600256 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700257 baseSsboBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600258
John Kessenich38f3b892013-09-06 19:52:57 +0000259 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200260 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000261
John Kessenich2b07c7e2013-07-31 18:44:13 +0000262 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700263 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000264 for (; argc >= 1; argc--, argv++) {
265 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000266 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600267 case '-':
268 {
269 std::string lowerword(argv[0]+2);
270 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
271
272 // handle --word style options
273 if (lowerword == "shift-sampler-bindings" || // synonyms
274 lowerword == "shift-sampler-binding" ||
275 lowerword == "ssb") {
276 ProcessBindingBase(argc, argv, baseSamplerBinding);
277 } else if (lowerword == "shift-texture-bindings" || // synonyms
278 lowerword == "shift-texture-binding" ||
279 lowerword == "stb") {
280 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600281 } else if (lowerword == "shift-image-bindings" || // synonyms
282 lowerword == "shift-image-binding" ||
283 lowerword == "sib") {
284 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600285 } else if (lowerword == "shift-ubo-bindings" || // synonyms
286 lowerword == "shift-ubo-binding" ||
287 lowerword == "sub") {
288 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700289 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
290 lowerword == "shift-ssbo-binding" ||
291 lowerword == "sbb") {
292 ProcessBindingBase(argc, argv, baseSsboBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600293 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700294 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600295 lowerword == "amb") {
296 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600297 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
298 lowerword == "flatten-uniform-array" ||
299 lowerword == "fua") {
300 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600301 } else if (lowerword == "no-storage-format" || // synonyms
302 lowerword == "nsf") {
303 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800304 } else if (lowerword == "variable-name" || // synonyms
305 lowerword == "vn") {
306 Options |= EOptionOutputHexadecimal;
307 variableName = argv[1];
308 if (argc > 0) {
309 argc--;
310 argv++;
311 } else
312 Error("no <C-variable-name> provided for --variable-name");
313 break;
314 }
315 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;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600327 } else {
328 usage();
329 }
330 }
331 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000332 case 'H':
333 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600334 if ((Options & EOptionSpv) == 0) {
335 // default to Vulkan
336 Options |= EOptionSpv;
337 Options |= EOptionVulkanRules;
338 Options |= EOptionLinkProgram;
339 }
340 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000341 case 'V':
342 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600343 Options |= EOptionVulkanRules;
344 Options |= EOptionLinkProgram;
345 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400346 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400347 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400348 if (argc > 0) {
349 argc--;
350 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600351 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400352 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400353 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600354 case 'G':
355 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000356 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600357 // undo a -H default to Vulkan
358 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000359 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000360 case 'E':
361 Options |= EOptionOutputPreprocessed;
362 break;
John Kessenich05a70632013-09-17 19:26:08 +0000363 case 'c':
364 Options |= EOptionDumpConfig;
365 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600366 case 'C':
367 Options |= EOptionCascadingErrors;
368 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000369 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000370 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000371 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700372 case 'D':
373 Options |= EOptionReadHlsl;
374 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700375 case 'e':
376 // HLSL todo: entry point handle needs much more sophistication.
377 // This is okay for one compilation unit with one entry point.
378 entryPointName = argv[1];
379 if (argc > 0) {
380 argc--;
381 argv++;
382 } else
383 Error("no <entry-point> provided for -e");
384 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600385 case 'h':
386 usage();
387 break;
John Kessenich05a70632013-09-17 19:26:08 +0000388 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000389 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000390 break;
391 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000392 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000393 break;
394 case 'm':
395 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000396 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600397 case 'o':
398 binaryFileName = argv[1];
399 if (argc > 0) {
400 argc--;
401 argv++;
402 } else
403 Error("no <file> provided for -o");
404 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000405 case 'q':
406 Options |= EOptionDumpReflection;
407 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000408 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000409 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000410 break;
411 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000412 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000413 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000414 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200415 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000416 break;
John Kessenich319de232013-12-04 04:43:40 +0000417 case 'v':
418 Options |= EOptionDumpVersions;
419 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000420 case 'w':
421 Options |= EOptionSuppressWarnings;
422 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500423 case 'x':
424 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500425 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000426 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600427 usage();
428 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000429 }
John Kessenich38f3b892013-09-06 19:52:57 +0000430 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000431 std::string name(argv[0]);
432 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200433 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000434 }
John Kessenich38f3b892013-09-06 19:52:57 +0000435 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000436 }
437
John Kessenich68d78fd2015-07-12 19:28:10 -0600438 // Make sure that -E is not specified alongside linking (which includes SPV generation)
439 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
440 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000441
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500442 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600443 if (binaryFileName && (Options & EOptionSpv) == 0)
444 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600445
446 if ((Options & EOptionFlattenUniformArrays) != 0 &&
447 (Options & EOptionReadHlsl) == 0)
448 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000449}
450
John Kessenich68d78fd2015-07-12 19:28:10 -0600451//
452// Translate the meaningful subset of command-line options to parser-behavior options.
453//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000454void SetMessageOptions(EShMessages& messages)
455{
456 if (Options & EOptionRelaxedErrors)
457 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
458 if (Options & EOptionIntermediate)
459 messages = (EShMessages)(messages | EShMsgAST);
460 if (Options & EOptionSuppressWarnings)
461 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600462 if (Options & EOptionSpv)
463 messages = (EShMessages)(messages | EShMsgSpvRules);
464 if (Options & EOptionVulkanRules)
465 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400466 if (Options & EOptionOutputPreprocessed)
467 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700468 if (Options & EOptionReadHlsl)
469 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600470 if (Options & EOptionCascadingErrors)
471 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700472 if (Options & EOptionKeepUncalled)
473 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000474}
475
John Kessenich68d78fd2015-07-12 19:28:10 -0600476//
John Kessenich69f4b512013-09-04 21:19:27 +0000477// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000478//
Juan Lopeza558b262017-04-02 23:04:00 +0200479void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000480{
John Kessenich38f3b892013-09-06 19:52:57 +0000481 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200482 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000483 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000484 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200485 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000486
John Kessenichb0a7eb52013-11-07 17:44:20 +0000487 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000488
John Kessenich94a81fb2013-08-31 02:41:30 +0000489 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000490 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000491
492 ShDestruct(compiler);
493 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000494}
495
John Kessenich6626cad2015-06-19 05:14:19 +0000496// Outputs the given string, but only if it is non-null and non-empty.
497// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400498void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000499{
500 if (str && str[0]) {
501 puts(str);
502 }
503}
504
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400505// Outputs the given string to stderr, but only if it is non-null and non-empty.
506// This prevents erroneous newlines from appearing.
507void StderrIfNonEmpty(const char* str)
508{
509 if (str && str[0]) {
510 fprintf(stderr, "%s\n", str);
511 }
512}
513
John Kessenichc57b2a92016-01-16 15:30:03 -0700514// Simple bundling of what makes a compilation unit for ease in passing around,
515// and separation of handling file IO versus API (programmatic) compilation.
516struct ShaderCompUnit {
517 EShLanguage stage;
518 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400519 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600520 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400521
John Kessenich219b0252016-08-23 17:51:13 -0600522 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400523 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
524 {
525 stage = istage;
526 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600527 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600528 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400529 }
530
531 ShaderCompUnit(const ShaderCompUnit &rhs)
532 {
533 stage = rhs.stage;
534 fileName = rhs.fileName;
535 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600536 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400537 }
538
John Kessenichc57b2a92016-01-16 15:30:03 -0700539};
540
John Kessenich69f4b512013-09-04 21:19:27 +0000541//
John Kessenichc57b2a92016-01-16 15:30:03 -0700542// For linking mode: Will independently parse each compilation unit, but then put them
543// in the same program and link them together, making at most one linked module per
544// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000545//
546// Uses the new C++ interface instead of the old handle-based interface.
547//
John Kessenichc57b2a92016-01-16 15:30:03 -0700548
549void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000550{
551 // keep track of what to free
552 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700553
John Kessenich69f4b512013-09-04 21:19:27 +0000554 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000555 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000556
John Kessenich69f4b512013-09-04 21:19:27 +0000557 //
558 // Per-shader processing...
559 //
560
John Kessenich5b0f13a2013-11-01 03:08:40 +0000561 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100562 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
563 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700564 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400565 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700566 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
567 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600568 if (sourceEntryPointName)
569 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600570
571 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
572 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600573 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600574 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700575 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600576 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600577 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600578
579 if (Options & EOptionAutoMapBindings)
580 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700581
John Kessenich69f4b512013-09-04 21:19:27 +0000582 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600583
John Kessenichc555ddd2015-06-17 02:38:44 +0000584 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000585
John Kessenichc555ddd2015-06-17 02:38:44 +0000586 if (Options & EOptionOutputPreprocessed) {
587 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700588 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400589 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500590 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400591 PutsIfNonEmpty(str.c_str());
592 } else {
593 CompileFailed = true;
594 }
595 StderrIfNonEmpty(shader->getInfoLog());
596 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000597 continue;
598 }
599 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000600 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000601
John Kessenich69f4b512013-09-04 21:19:27 +0000602 program.addShader(shader);
603
John Kessenichc57b2a92016-01-16 15:30:03 -0700604 if (! (Options & EOptionSuppressInfolog) &&
605 ! (Options & EOptionMemoryLeakMode)) {
606 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400607 PutsIfNonEmpty(shader->getInfoLog());
608 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000609 }
John Kessenich69f4b512013-09-04 21:19:27 +0000610 }
611
612 //
613 // Program-level processing...
614 //
615
John Kessenich4d65ee32016-03-12 18:17:47 -0700616 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600617 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000618 LinkFailed = true;
619
steve-lunarg9ae34742016-10-05 13:40:13 -0600620 // Map IO
621 if (Options & EOptionSpv) {
622 if (!program.mapIO())
623 LinkFailed = true;
624 }
John Kessenichecba76f2017-01-06 00:34:48 -0700625
John Kessenich4d65ee32016-03-12 18:17:47 -0700626 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700627 if (! (Options & EOptionSuppressInfolog) &&
628 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400629 PutsIfNonEmpty(program.getInfoLog());
630 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000631 }
632
John Kessenich4d65ee32016-03-12 18:17:47 -0700633 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000634 if (Options & EOptionDumpReflection) {
635 program.buildReflection();
636 program.dumpReflection();
637 }
638
John Kessenich4d65ee32016-03-12 18:17:47 -0700639 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000640 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000641 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600642 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000643 else {
644 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000645 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000646 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400647 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400648 spv::SpvBuildLogger logger;
649 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700650
651 // Dump the spv to a file or stdout, etc., but only if not doing
652 // memory/perf testing, as it's not internal to programmatic use.
653 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500654 printf("%s", logger.getAllMessages().c_str());
655 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700656 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500657 } else {
658 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
659 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700660 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700661 spv::Disassemble(std::cout, spirv);
662 }
John Kessenichacba7722015-03-04 03:48:38 +0000663 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000664 }
John Kessenich92f90382014-07-28 04:21:04 +0000665 }
666 }
667 }
668
John Kessenich5b0f13a2013-11-01 03:08:40 +0000669 // Free everything up, program has to go before the shaders
670 // because it might have merged stuff from the shaders, and
671 // the stuff from the shaders has to have its destructors called
672 // before the pools holding the memory in the shaders is freed.
673 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000674 while (shaders.size() > 0) {
675 delete shaders.back();
676 shaders.pop_back();
677 }
John Kessenich69f4b512013-09-04 21:19:27 +0000678}
679
John Kessenichc57b2a92016-01-16 15:30:03 -0700680//
681// Do file IO part of compile and link, handing off the pure
682// API/programmatic mode to CompileAndLinkShaderUnits(), which can
683// be put in a loop for testing memory footprint and performance.
684//
685// This is just for linking mode: meaning all the shaders will be put into the
686// the same program linked together.
687//
688// This means there are a limited number of work items (not multi-threading mode)
689// and that the point is testing at the linking level. Hence, to enable
690// performance and memory testing, the actual compile/link can be put in
691// a loop, independent of processing the work items and file IO.
692//
Juan Lopeza558b262017-04-02 23:04:00 +0200693void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700694{
695 std::vector<ShaderCompUnit> compUnits;
696
697 // Transfer all the work items from to a simple list of
698 // of compilation units. (We don't care about the thread
699 // work-item distribution properties in this path, which
700 // is okay due to the limited number of shaders, know since
701 // they are all getting linked together.)
702 glslang::TWorkItem* workItem;
703 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200704 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700705 FindLanguage(workItem->name),
706 workItem->name,
707 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200708 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700709
710 if (! compUnit.text) {
711 usage();
712 return;
713 }
714
715 compUnits.push_back(compUnit);
716 }
717
718 // Actual call to programmatic processing of compile and link,
719 // in a loop for testing memory and performance. This part contains
720 // all the perf/memory that a programmatic consumer will care about.
721 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
722 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
723 CompileAndLinkShaderUnits(compUnits);
724
725 if (Options & EOptionMemoryLeakMode)
726 glslang::OS_DumpMemoryCounters();
727 }
728
rdb32084e82016-02-23 22:17:38 +0100729 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
730 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700731}
732
John Kessenicha0af4732012-12-12 21:15:54 +0000733int C_DECL main(int argc, char* argv[])
734{
Juan Lopeza558b262017-04-02 23:04:00 +0200735 // array of unique places to leave the shader names and infologs for the asynchronous compiles
736 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
737 ProcessArguments(workItems, argc, argv);
738
739 glslang::TWorklist workList;
740 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
741 assert(item);
742 workList.add(item.get());
743 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000744
John Kessenich05a70632013-09-17 19:26:08 +0000745 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500746 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200747 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000748 return ESuccess;
749 }
750
John Kessenich0da9eaa2015-08-01 17:10:02 -0600751 if (Options & EOptionDumpVersions) {
752 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000753 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
754 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600755 std::string spirvVersion;
756 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600757 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600758 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700759 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600760 printf("GL_KHR_vulkan_glsl version %d\n", 100);
761 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200762 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000763 return ESuccess;
764 }
765
Juan Lopeza558b262017-04-02 23:04:00 +0200766 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000767 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000768 }
769
770 ProcessConfigFile();
771
John Kessenich69f4b512013-09-04 21:19:27 +0000772 //
773 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000774 // 1) linking all arguments together, single-threaded, new C++ interface
775 // 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 +0000776 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000777 if (Options & EOptionLinkProgram ||
778 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000779 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200780 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000781 glslang::FinalizeProcess();
782 } else {
783 ShInitialize();
784
Juan Lopeza558b262017-04-02 23:04:00 +0200785 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000786
Juan Lopeza558b262017-04-02 23:04:00 +0200787 if (Options & EOptionMultiThreaded)
788 {
789 std::array<std::thread, 16> threads;
790 for (unsigned int t = 0; t < threads.size(); ++t)
791 {
792 threads[t] = std::thread(CompileShaders, std::ref(workList));
793 if (threads[t].get_id() == std::thread::id())
794 {
John Kessenich38f3b892013-09-06 19:52:57 +0000795 printf("Failed to create thread\n");
796 return EFailThreadCreate;
797 }
John Kessenicha0af4732012-12-12 21:15:54 +0000798 }
Juan Lopeza558b262017-04-02 23:04:00 +0200799
800 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000801 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200802 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000803
804 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200805 for (size_t w = 0; w < workItems.size(); ++w) {
806 if (workItems[w]) {
807 if (printShaderNames || workItems[w]->results.size() > 0)
808 PutsIfNonEmpty(workItems[w]->name.c_str());
809 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000810 }
811 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000812
813 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000814 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000815
John Kessenichc999ba22013-11-07 23:33:24 +0000816 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000817 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000818 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000819 return EFailLink;
820
821 return 0;
822}
823
824//
825// Deduce the language from the filename. Files must end in one of the
826// following extensions:
827//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000828// .vert = vertex
829// .tesc = tessellation control
830// .tese = tessellation evaluation
831// .geom = geometry
832// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000833// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000834//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600835EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000836{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600837 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600838 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600839
Dan Bakerc6ede892016-08-11 14:06:06 -0400840 if (shaderStageName)
841 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600842 else {
843 // Search for a suffix on a filename: e.g, "myfile.frag". If given
844 // the suffix directly, we skip looking for the '.'
845 if (parseSuffix) {
846 ext = name.rfind('.');
847 if (ext == std::string::npos) {
848 usage();
849 return EShLangVertex;
850 }
851 ++ext;
852 }
853 suffix = name.substr(ext, std::string::npos);
854 }
dankbaker45d49bc2016-08-08 21:43:07 -0400855
John Kessenich2b07c7e2013-07-31 18:44:13 +0000856 if (suffix == "vert")
857 return EShLangVertex;
858 else if (suffix == "tesc")
859 return EShLangTessControl;
860 else if (suffix == "tese")
861 return EShLangTessEvaluation;
862 else if (suffix == "geom")
863 return EShLangGeometry;
864 else if (suffix == "frag")
865 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000866 else if (suffix == "comp")
867 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000868
869 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000870 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000871}
872
John Kessenicha0af4732012-12-12 21:15:54 +0000873//
John Kessenichecba76f2017-01-06 00:34:48 -0700874// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000875// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000876//
John Kessenich51cdd902014-02-18 23:37:57 +0000877void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000878{
John Kessenichca3457f2015-05-18 01:59:45 +0000879 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000880 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000881 if (! shaderStrings) {
882 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000883 }
884
John Kessenich41cf6b52013-06-25 18:10:05 +0000885 int* lengths = new int[NumShaderStrings];
886
887 // move to length-based strings, rather than null-terminated strings
888 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000889 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000890
John Kessenichc999ba22013-11-07 23:33:24 +0000891 if (! shaderStrings) {
892 CompileFailed = true;
893 return;
894 }
John Kessenicha0af4732012-12-12 21:15:54 +0000895
John Kessenich52ac67e2013-05-05 23:46:22 +0000896 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000897 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700898
John Kessenich94a81fb2013-08-31 02:41:30 +0000899 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
900 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700901 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000902 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700903 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700904 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000905 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700906 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
907 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000908 }
John Kessenicha0af4732012-12-12 21:15:54 +0000909
John Kessenich94a81fb2013-08-31 02:41:30 +0000910 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000911 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000912 }
John Kessenicha0af4732012-12-12 21:15:54 +0000913
John Kessenich41cf6b52013-06-25 18:10:05 +0000914 delete [] lengths;
915 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000916
John Kessenichc999ba22013-11-07 23:33:24 +0000917 if (ret == 0)
918 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000919}
920
John Kessenicha0af4732012-12-12 21:15:54 +0000921//
922// print usage to stdout
923//
924void usage()
925{
John Kessenich319de232013-12-04 04:43:40 +0000926 printf("Usage: glslangValidator [option]... [file]...\n"
927 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000928 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600929 " .conf to provide an optional config file that replaces the default configuration\n"
930 " (see -c option below for generating a template)\n"
931 " .vert for a vertex shader\n"
932 " .tesc for a tessellation control shader\n"
933 " .tese for a tessellation evaluation shader\n"
934 " .geom for a geometry shader\n"
935 " .frag for a fragment shader\n"
936 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000937 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000938 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000939 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000940 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600941 "Each option must be specified separately.\n"
942 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
943 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600944 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
945 " default file name is <stage>.spv (-o overrides this)\n"
946 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400947 " -E print pre-processed GLSL; cannot be used with -l;\n"
948 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -0600949 " -S <stage> uses specified stage rather than parsing the file extension\n"
950 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600951 " -c configuration dump;\n"
952 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600953 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600954 " -d default to desktop (#version 110) when there is no shader #version\n"
955 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700956 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700957 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600958 " -h print this usage message\n"
959 " -i intermediate tree (glslang AST) is printed out\n"
960 " -l link all input files together to form a single module\n"
961 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500962 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600963 " -q dump reflection query database\n"
964 " -r relaxed semantic error-checking mode\n"
965 " -s silent mode\n"
966 " -t multi-threaded mode\n"
967 " -v print version strings\n"
968 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500969 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600970 "\n"
971 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
972 " --ssb [stage] num synonym for --shift-sampler-binding\n"
973 "\n"
974 " --shift-texture-binding [stage] num set base binding number for textures\n"
975 " --stb [stage] num synonym for --shift-texture-binding\n"
976 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600977 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
978 " --sib [stage] num synonym for --shift-image-binding\n"
979 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600980 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
981 " --sub [stage] num synonym for --shift-UBO-binding\n"
982 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -0700983 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
984 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
985 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600986 " --auto-map-bindings automatically bind uniform variables without\n"
987 " explicit bindings.\n"
988 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600989 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -0600990 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600991 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -0600992 "\n"
993 " --no-storage-format use Unknown image format\n"
994 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -0600995 "\n"
996 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
997 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -0700998 "\n"
999 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1000 " --ku synonym for --keep-uncalled\n"
John Kessenich8f674e82017-02-18 09:45:40 -07001001 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name> initialized with the shader binary code.\n"
1002 " --vn <name> synonym for --variable-name <name>.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001003 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001004
1005 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001006}
1007
John Kessenich3ce4e592014-10-06 19:57:34 +00001008#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001009
1010#include <errno.h>
1011
1012int fopen_s(
1013 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001014 const char* filename,
1015 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001016)
1017{
1018 if (!pFile || !filename || !mode) {
1019 return EINVAL;
1020 }
1021
1022 FILE* f = fopen(filename, mode);
1023 if (! f) {
1024 if (errno != 0) {
1025 return errno;
1026 } else {
1027 return ENOENT;
1028 }
1029 }
1030 *pFile = f;
1031
1032 return 0;
1033}
1034
1035#endif
1036
John Kessenicha0af4732012-12-12 21:15:54 +00001037//
1038// Malloc a string of sufficient size and read a string into it.
1039//
John Kessenichecba76f2017-01-06 00:34:48 -07001040char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001041{
John Kessenichb3297152015-07-11 18:01:03 -06001042 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001043 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001044
John Kessenicha0af4732012-12-12 21:15:54 +00001045 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001046 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1047 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001048
John Kessenich68d78fd2015-07-12 19:28:10 -06001049 if (errorCode || in == nullptr)
1050 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001051
John Kessenicha0af4732012-12-12 21:15:54 +00001052 while (fgetc(in) != EOF)
1053 count++;
1054
John Kessenichd6c72a42014-08-18 19:42:35 +00001055 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001056
John Kessenichb3297152015-07-11 18:01:03 -06001057 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001058 if (! fdata)
1059 Error("can't allocate memory");
1060
John Kessenichb3297152015-07-11 18:01:03 -06001061 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001062 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001063 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001064 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001065
John Kessenicha0af4732012-12-12 21:15:54 +00001066 fdata[count] = '\0';
1067 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001068
John Kessenichea869fb2013-10-28 18:12:06 +00001069 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001070 // recover from empty file
1071 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001072 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001073 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001074 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001075
John Kessenichb3297152015-07-11 18:01:03 -06001076 return return_data;
1077 } else
1078 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1079
1080 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001081 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001082 int ptr_len = 0;
1083 int i = 0;
1084 while (count > 0) {
1085 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1086 memcpy(return_data[i], fdata + ptr_len, len);
1087 return_data[i][len] = '\0';
1088 count -= len;
1089 ptr_len += len;
1090 if (count < len) {
1091 if (count == 0) {
1092 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001093 break;
1094 }
John Kessenichb3297152015-07-11 18:01:03 -06001095 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001096 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001097 ++i;
1098 }
John Kessenichb3297152015-07-11 18:01:03 -06001099
1100 free(fdata);
1101
John Kessenicha0af4732012-12-12 21:15:54 +00001102 return return_data;
1103}
1104
John Kessenich51cdd902014-02-18 23:37:57 +00001105void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001106{
John Kessenichb3297152015-07-11 18:01:03 -06001107 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001108 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001109
1110 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001111}
1112
John Kessenich54d8cda2013-02-11 22:36:01 +00001113void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001114{
John Kessenichfae38ee2015-06-10 23:23:12 +00001115 if (num >= 0 )
1116 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1117 else
1118 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001119}