blob: 0da690e9a81c081c69e19af0b2943467411001f4 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000035//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
38#define _CRT_SECURE_NO_WARNINGS
39
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040040#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000041#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000042#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060043#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000045#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060046#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000047#include "../SPIRV/doc.h"
48#include "../SPIRV/disassemble.h"
John Kessenich66ec80e2016-08-05 14:04:23 -060049#include <cstring>
50#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060051#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060052#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060053#include <array>
John Kessenicha0af4732012-12-12 21:15:54 +000054
baldurk876a0e32015-11-16 18:03:28 +010055#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000056
57extern "C" {
58 SH_IMPORT_EXPORT void ShOutputHtml();
59}
60
John Kessenich94a81fb2013-08-31 02:41:30 +000061// Command-line options
62enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070063 EOptionNone = 0,
64 EOptionIntermediate = (1 << 0),
65 EOptionSuppressInfolog = (1 << 1),
66 EOptionMemoryLeakMode = (1 << 2),
67 EOptionRelaxedErrors = (1 << 3),
68 EOptionGiveWarnings = (1 << 4),
69 EOptionLinkProgram = (1 << 5),
70 EOptionMultiThreaded = (1 << 6),
71 EOptionDumpConfig = (1 << 7),
72 EOptionDumpReflection = (1 << 8),
73 EOptionSuppressWarnings = (1 << 9),
74 EOptionDumpVersions = (1 << 10),
75 EOptionSpv = (1 << 11),
76 EOptionHumanReadableSpv = (1 << 12),
77 EOptionVulkanRules = (1 << 13),
78 EOptionDefaultDesktop = (1 << 14),
79 EOptionOutputPreprocessed = (1 << 15),
80 EOptionOutputHexadecimal = (1 << 16),
81 EOptionReadHlsl = (1 << 17),
82 EOptionCascadingErrors = (1 << 18),
83 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060084 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070085 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070086 EOptionKeepUncalled = (1 << 22),
John Kessenich94a81fb2013-08-31 02:41:30 +000087};
88
John Kessenicha0af4732012-12-12 21:15:54 +000089//
John Kessenich68d78fd2015-07-12 19:28:10 -060090// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000091//
92enum TFailCode {
93 ESuccess = 0,
94 EFailUsage,
95 EFailCompile,
96 EFailLink,
97 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000098 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000099 EFailLinkerCreate
100};
101
102//
John Kessenich68d78fd2015-07-12 19:28:10 -0600103// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000104//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600105EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000106void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000107void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000108void FreeFileData(char** data);
109char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000110void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000111
John Kessenichc999ba22013-11-07 23:33:24 +0000112// Globally track if any compile or link failure.
113bool CompileFailed = false;
114bool LinkFailed = false;
115
John Kessenich05a70632013-09-17 19:26:08 +0000116// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600117// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000118int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000119
John Kessenich05a70632013-09-17 19:26:08 +0000120TBuiltInResource Resources;
121std::string ConfigFile;
122
John Kessenicha0af4732012-12-12 21:15:54 +0000123//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600124// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000125//
126void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000127{
John Kessenich05a70632013-09-17 19:26:08 +0000128 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000129 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000130 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000131 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000132 if (configStrings)
133 config = *configStrings;
134 else {
135 printf("Error opening configuration file; will instead use the default configuration\n");
136 usage();
137 }
138 }
139
140 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500141 Resources = glslang::DefaultTBuiltInResource;
142 return;
John Kessenich05a70632013-09-17 19:26:08 +0000143 }
144
Lei Zhang1b141722016-05-19 13:50:49 -0400145 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000146
John Kessenich05a70632013-09-17 19:26:08 +0000147 if (configStrings)
148 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500149 else
150 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000151}
152
John Kessenich38f3b892013-09-06 19:52:57 +0000153// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000154glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000155
156// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000157glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000158int NumWorkItems = 0;
159
John Kessenich94a81fb2013-08-31 02:41:30 +0000160int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600161const char* ExecutableName = nullptr;
162const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700163const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600164const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400165const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800166const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600167
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600168std::array<unsigned int, EShLangCount> baseSamplerBinding;
169std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600170std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600171std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700172std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600173
John Kessenich68d78fd2015-07-12 19:28:10 -0600174//
175// Create the default name for saving a binary if -o is not provided.
176//
177const char* GetBinaryName(EShLanguage stage)
178{
179 const char* name;
180 if (binaryFileName == nullptr) {
181 switch (stage) {
182 case EShLangVertex: name = "vert.spv"; break;
183 case EShLangTessControl: name = "tesc.spv"; break;
184 case EShLangTessEvaluation: name = "tese.spv"; break;
185 case EShLangGeometry: name = "geom.spv"; break;
186 case EShLangFragment: name = "frag.spv"; break;
187 case EShLangCompute: name = "comp.spv"; break;
188 default: name = "unknown"; break;
189 }
190 } else
191 name = binaryFileName;
192
193 return name;
194}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000195
John Kessenich05a70632013-09-17 19:26:08 +0000196//
197// *.conf => this is a config file that can set limits/resources
198//
199bool SetConfigFile(const std::string& name)
200{
201 if (name.size() < 5)
202 return false;
203
John Kessenich4c706852013-10-11 16:28:43 +0000204 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000205 ConfigFile = name;
206 return true;
207 }
208
209 return false;
210}
211
John Kessenich68d78fd2015-07-12 19:28:10 -0600212//
213// Give error and exit with failure code.
214//
215void Error(const char* message)
216{
217 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
218 exit(EFailUsage);
219}
220
221//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600222// Process an optional binding base of the form:
223// --argname [stage] base
224// Where stage is one of the forms accepted by FindLanguage, and base is an integer
225//
226void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
227{
228 if (argc < 2)
229 usage();
230
231 if (!isdigit(argv[1][0])) {
232 if (argc < 3) // this form needs one more argument
233 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700234
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600235 // Parse form: --argname stage base
236 const EShLanguage lang = FindLanguage(argv[1], false);
237 base[lang] = atoi(argv[2]);
238 argc-= 2;
239 argv+= 2;
240 } else {
241 // Parse form: --argname base
242 for (int lang=0; lang<EShLangCount; ++lang)
243 base[lang] = atoi(argv[1]);
244
245 argc--;
246 argv++;
247 }
248}
249
250//
John Kessenich68d78fd2015-07-12 19:28:10 -0600251// Do all command-line argument parsing. This includes building up the work-items
252// to be processed later, and saving all the command-line options.
253//
254// Does not return (it exits) if command-line is fatally flawed.
255//
256void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000257{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600258 baseSamplerBinding.fill(0);
259 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600260 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600261 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700262 baseSsboBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600263
John Kessenich38f3b892013-09-06 19:52:57 +0000264 ExecutableName = argv[0];
265 NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0
John Kessenichfd305422014-06-05 16:30:53 +0000266 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600267 for (int w = 0; w < NumWorkItems; ++w)
268 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000269
John Kessenich2b07c7e2013-07-31 18:44:13 +0000270 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700271 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000272 for (; argc >= 1; argc--, argv++) {
273 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000274 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600275 case '-':
276 {
277 std::string lowerword(argv[0]+2);
278 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
279
280 // handle --word style options
281 if (lowerword == "shift-sampler-bindings" || // synonyms
282 lowerword == "shift-sampler-binding" ||
283 lowerword == "ssb") {
284 ProcessBindingBase(argc, argv, baseSamplerBinding);
285 } else if (lowerword == "shift-texture-bindings" || // synonyms
286 lowerword == "shift-texture-binding" ||
287 lowerword == "stb") {
288 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600289 } else if (lowerword == "shift-image-bindings" || // synonyms
290 lowerword == "shift-image-binding" ||
291 lowerword == "sib") {
292 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600293 } else if (lowerword == "shift-ubo-bindings" || // synonyms
294 lowerword == "shift-ubo-binding" ||
295 lowerword == "sub") {
296 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700297 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
298 lowerword == "shift-ssbo-binding" ||
299 lowerword == "sbb") {
300 ProcessBindingBase(argc, argv, baseSsboBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600301 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700302 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600303 lowerword == "amb") {
304 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600305 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
306 lowerword == "flatten-uniform-array" ||
307 lowerword == "fua") {
308 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600309 } else if (lowerword == "no-storage-format" || // synonyms
310 lowerword == "nsf") {
311 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800312 } else if (lowerword == "variable-name" || // synonyms
313 lowerword == "vn") {
314 Options |= EOptionOutputHexadecimal;
315 variableName = argv[1];
316 if (argc > 0) {
317 argc--;
318 argv++;
319 } else
320 Error("no <C-variable-name> provided for --variable-name");
321 break;
322 }
323 else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600324 lowerword == "sep") {
325 sourceEntryPointName = argv[1];
326 if (argc > 0) {
327 argc--;
328 argv++;
329 } else
330 Error("no <entry-point> provided for --source-entrypoint");
331 break;
John Kessenich906cc212016-12-09 19:22:20 -0700332 } else if (lowerword == "keep-uncalled" || // synonyms
333 lowerword == "ku") {
334 Options |= EOptionKeepUncalled;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600335 } else {
336 usage();
337 }
338 }
339 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000340 case 'H':
341 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600342 if ((Options & EOptionSpv) == 0) {
343 // default to Vulkan
344 Options |= EOptionSpv;
345 Options |= EOptionVulkanRules;
346 Options |= EOptionLinkProgram;
347 }
348 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000349 case 'V':
350 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600351 Options |= EOptionVulkanRules;
352 Options |= EOptionLinkProgram;
353 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400354 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400355 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400356 if (argc > 0) {
357 argc--;
358 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600359 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400360 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400361 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600362 case 'G':
363 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000364 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600365 // undo a -H default to Vulkan
366 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000367 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000368 case 'E':
369 Options |= EOptionOutputPreprocessed;
370 break;
John Kessenich05a70632013-09-17 19:26:08 +0000371 case 'c':
372 Options |= EOptionDumpConfig;
373 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600374 case 'C':
375 Options |= EOptionCascadingErrors;
376 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000377 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000378 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000379 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700380 case 'D':
381 Options |= EOptionReadHlsl;
382 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700383 case 'e':
384 // HLSL todo: entry point handle needs much more sophistication.
385 // This is okay for one compilation unit with one entry point.
386 entryPointName = argv[1];
387 if (argc > 0) {
388 argc--;
389 argv++;
390 } else
391 Error("no <entry-point> provided for -e");
392 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600393 case 'h':
394 usage();
395 break;
John Kessenich05a70632013-09-17 19:26:08 +0000396 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000397 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000398 break;
399 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000400 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000401 break;
402 case 'm':
403 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000404 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600405 case 'o':
406 binaryFileName = argv[1];
407 if (argc > 0) {
408 argc--;
409 argv++;
410 } else
411 Error("no <file> provided for -o");
412 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000413 case 'q':
414 Options |= EOptionDumpReflection;
415 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000416 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000417 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000418 break;
419 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000420 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000421 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000422 case 't':
423 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000424 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000425 #endif
426 break;
John Kessenich319de232013-12-04 04:43:40 +0000427 case 'v':
428 Options |= EOptionDumpVersions;
429 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000430 case 'w':
431 Options |= EOptionSuppressWarnings;
432 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500433 case 'x':
434 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500435 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000436 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600437 usage();
438 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000439 }
John Kessenich38f3b892013-09-06 19:52:57 +0000440 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000441 std::string name(argv[0]);
442 if (! SetConfigFile(name)) {
443 Work[argc] = new glslang::TWorkItem(name);
444 Worklist.add(Work[argc]);
445 }
John Kessenich38f3b892013-09-06 19:52:57 +0000446 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000447 }
448
John Kessenich68d78fd2015-07-12 19:28:10 -0600449 // Make sure that -E is not specified alongside linking (which includes SPV generation)
450 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
451 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000452
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500453 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600454 if (binaryFileName && (Options & EOptionSpv) == 0)
455 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600456
457 if ((Options & EOptionFlattenUniformArrays) != 0 &&
458 (Options & EOptionReadHlsl) == 0)
459 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000460}
461
John Kessenich68d78fd2015-07-12 19:28:10 -0600462//
463// Translate the meaningful subset of command-line options to parser-behavior options.
464//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000465void SetMessageOptions(EShMessages& messages)
466{
467 if (Options & EOptionRelaxedErrors)
468 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
469 if (Options & EOptionIntermediate)
470 messages = (EShMessages)(messages | EShMsgAST);
471 if (Options & EOptionSuppressWarnings)
472 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600473 if (Options & EOptionSpv)
474 messages = (EShMessages)(messages | EShMsgSpvRules);
475 if (Options & EOptionVulkanRules)
476 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400477 if (Options & EOptionOutputPreprocessed)
478 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700479 if (Options & EOptionReadHlsl)
480 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600481 if (Options & EOptionCascadingErrors)
482 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700483 if (Options & EOptionKeepUncalled)
484 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000485}
486
John Kessenich68d78fd2015-07-12 19:28:10 -0600487//
John Kessenich69f4b512013-09-04 21:19:27 +0000488// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000489//
490// Return 0 for failure, 1 for success.
491//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800492unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000493{
John Kessenich38f3b892013-09-06 19:52:57 +0000494 glslang::TWorkItem* workItem;
495 while (Worklist.remove(workItem)) {
496 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000497 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000498 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000499
John Kessenichb0a7eb52013-11-07 17:44:20 +0000500 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000501
John Kessenich94a81fb2013-08-31 02:41:30 +0000502 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000503 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000504
505 ShDestruct(compiler);
506 }
507
508 return 0;
509}
510
John Kessenich6626cad2015-06-19 05:14:19 +0000511// Outputs the given string, but only if it is non-null and non-empty.
512// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400513void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000514{
515 if (str && str[0]) {
516 puts(str);
517 }
518}
519
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400520// Outputs the given string to stderr, but only if it is non-null and non-empty.
521// This prevents erroneous newlines from appearing.
522void StderrIfNonEmpty(const char* str)
523{
524 if (str && str[0]) {
525 fprintf(stderr, "%s\n", str);
526 }
527}
528
John Kessenichc57b2a92016-01-16 15:30:03 -0700529// Simple bundling of what makes a compilation unit for ease in passing around,
530// and separation of handling file IO versus API (programmatic) compilation.
531struct ShaderCompUnit {
532 EShLanguage stage;
533 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400534 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600535 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400536
John Kessenich219b0252016-08-23 17:51:13 -0600537 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400538 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
539 {
540 stage = istage;
541 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600542 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600543 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400544 }
545
546 ShaderCompUnit(const ShaderCompUnit &rhs)
547 {
548 stage = rhs.stage;
549 fileName = rhs.fileName;
550 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600551 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400552 }
553
John Kessenichc57b2a92016-01-16 15:30:03 -0700554};
555
John Kessenich69f4b512013-09-04 21:19:27 +0000556//
John Kessenichc57b2a92016-01-16 15:30:03 -0700557// For linking mode: Will independently parse each compilation unit, but then put them
558// in the same program and link them together, making at most one linked module per
559// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000560//
561// Uses the new C++ interface instead of the old handle-based interface.
562//
John Kessenichc57b2a92016-01-16 15:30:03 -0700563
564void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000565{
566 // keep track of what to free
567 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700568
John Kessenich69f4b512013-09-04 21:19:27 +0000569 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000570 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000571
John Kessenich69f4b512013-09-04 21:19:27 +0000572 //
573 // Per-shader processing...
574 //
575
John Kessenich5b0f13a2013-11-01 03:08:40 +0000576 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100577 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
578 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700579 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400580 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700581 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
582 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600583 if (sourceEntryPointName)
584 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600585
586 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
587 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600588 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600589 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700590 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600591 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600592 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600593
594 if (Options & EOptionAutoMapBindings)
595 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700596
John Kessenich69f4b512013-09-04 21:19:27 +0000597 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600598
John Kessenichc555ddd2015-06-17 02:38:44 +0000599 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000600
John Kessenichc555ddd2015-06-17 02:38:44 +0000601 if (Options & EOptionOutputPreprocessed) {
602 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700603 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400604 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500605 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400606 PutsIfNonEmpty(str.c_str());
607 } else {
608 CompileFailed = true;
609 }
610 StderrIfNonEmpty(shader->getInfoLog());
611 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000612 continue;
613 }
614 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000615 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000616
John Kessenich69f4b512013-09-04 21:19:27 +0000617 program.addShader(shader);
618
John Kessenichc57b2a92016-01-16 15:30:03 -0700619 if (! (Options & EOptionSuppressInfolog) &&
620 ! (Options & EOptionMemoryLeakMode)) {
621 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400622 PutsIfNonEmpty(shader->getInfoLog());
623 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000624 }
John Kessenich69f4b512013-09-04 21:19:27 +0000625 }
626
627 //
628 // Program-level processing...
629 //
630
John Kessenich4d65ee32016-03-12 18:17:47 -0700631 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600632 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000633 LinkFailed = true;
634
steve-lunarg9ae34742016-10-05 13:40:13 -0600635 // Map IO
636 if (Options & EOptionSpv) {
637 if (!program.mapIO())
638 LinkFailed = true;
639 }
John Kessenichecba76f2017-01-06 00:34:48 -0700640
John Kessenich4d65ee32016-03-12 18:17:47 -0700641 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700642 if (! (Options & EOptionSuppressInfolog) &&
643 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400644 PutsIfNonEmpty(program.getInfoLog());
645 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000646 }
647
John Kessenich4d65ee32016-03-12 18:17:47 -0700648 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000649 if (Options & EOptionDumpReflection) {
650 program.buildReflection();
651 program.dumpReflection();
652 }
653
John Kessenich4d65ee32016-03-12 18:17:47 -0700654 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000655 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000656 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600657 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000658 else {
659 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000660 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000661 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400662 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400663 spv::SpvBuildLogger logger;
664 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700665
666 // Dump the spv to a file or stdout, etc., but only if not doing
667 // memory/perf testing, as it's not internal to programmatic use.
668 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500669 printf("%s", logger.getAllMessages().c_str());
670 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700671 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500672 } else {
673 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
674 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700675 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700676 spv::Disassemble(std::cout, spirv);
677 }
John Kessenichacba7722015-03-04 03:48:38 +0000678 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000679 }
John Kessenich92f90382014-07-28 04:21:04 +0000680 }
681 }
682 }
683
John Kessenich5b0f13a2013-11-01 03:08:40 +0000684 // Free everything up, program has to go before the shaders
685 // because it might have merged stuff from the shaders, and
686 // the stuff from the shaders has to have its destructors called
687 // before the pools holding the memory in the shaders is freed.
688 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000689 while (shaders.size() > 0) {
690 delete shaders.back();
691 shaders.pop_back();
692 }
John Kessenich69f4b512013-09-04 21:19:27 +0000693}
694
John Kessenichc57b2a92016-01-16 15:30:03 -0700695//
696// Do file IO part of compile and link, handing off the pure
697// API/programmatic mode to CompileAndLinkShaderUnits(), which can
698// be put in a loop for testing memory footprint and performance.
699//
700// This is just for linking mode: meaning all the shaders will be put into the
701// the same program linked together.
702//
703// This means there are a limited number of work items (not multi-threading mode)
704// and that the point is testing at the linking level. Hence, to enable
705// performance and memory testing, the actual compile/link can be put in
706// a loop, independent of processing the work items and file IO.
707//
708void CompileAndLinkShaderFiles()
709{
710 std::vector<ShaderCompUnit> compUnits;
711
712 // Transfer all the work items from to a simple list of
713 // of compilation units. (We don't care about the thread
714 // work-item distribution properties in this path, which
715 // is okay due to the limited number of shaders, know since
716 // they are all getting linked together.)
717 glslang::TWorkItem* workItem;
718 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200719 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700720 FindLanguage(workItem->name),
721 workItem->name,
722 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200723 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700724
725 if (! compUnit.text) {
726 usage();
727 return;
728 }
729
730 compUnits.push_back(compUnit);
731 }
732
733 // Actual call to programmatic processing of compile and link,
734 // in a loop for testing memory and performance. This part contains
735 // all the perf/memory that a programmatic consumer will care about.
736 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
737 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
738 CompileAndLinkShaderUnits(compUnits);
739
740 if (Options & EOptionMemoryLeakMode)
741 glslang::OS_DumpMemoryCounters();
742 }
743
rdb32084e82016-02-23 22:17:38 +0100744 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
745 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700746}
747
John Kessenicha0af4732012-12-12 21:15:54 +0000748int C_DECL main(int argc, char* argv[])
749{
John Kessenich68d78fd2015-07-12 19:28:10 -0600750 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000751
John Kessenich05a70632013-09-17 19:26:08 +0000752 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500753 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000754 if (Worklist.empty())
755 return ESuccess;
756 }
757
John Kessenich0da9eaa2015-08-01 17:10:02 -0600758 if (Options & EOptionDumpVersions) {
759 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000760 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
761 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600762 std::string spirvVersion;
763 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600764 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600765 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700766 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600767 printf("GL_KHR_vulkan_glsl version %d\n", 100);
768 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000769 if (Worklist.empty())
770 return ESuccess;
771 }
772
John Kessenich05a70632013-09-17 19:26:08 +0000773 if (Worklist.empty()) {
774 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000775 }
776
777 ProcessConfigFile();
778
John Kessenich69f4b512013-09-04 21:19:27 +0000779 //
780 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000781 // 1) linking all arguments together, single-threaded, new C++ interface
782 // 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 +0000783 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000784 if (Options & EOptionLinkProgram ||
785 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000786 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700787 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000788 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500789 for (int w = 0; w < NumWorkItems; ++w) {
790 if (Work[w]) {
791 delete Work[w];
792 }
793 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000794 } else {
795 ShInitialize();
796
John Kessenich38f3b892013-09-06 19:52:57 +0000797 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000798
John Kessenich38f3b892013-09-06 19:52:57 +0000799 if (Options & EOptionMultiThreaded) {
800 const int NumThreads = 16;
801 void* threads[NumThreads];
802 for (int t = 0; t < NumThreads; ++t) {
803 threads[t] = glslang::OS_CreateThread(&CompileShaders);
804 if (! threads[t]) {
805 printf("Failed to create thread\n");
806 return EFailThreadCreate;
807 }
John Kessenicha0af4732012-12-12 21:15:54 +0000808 }
John Kessenich38f3b892013-09-06 19:52:57 +0000809 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000810 } else
811 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000812
813 // Print out all the resulting infologs
814 for (int w = 0; w < NumWorkItems; ++w) {
815 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600816 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400817 PutsIfNonEmpty(Work[w]->name.c_str());
818 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000819 delete Work[w];
820 }
821 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000822
823 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000824 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000825
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500826 delete[] Work;
827
John Kessenichc999ba22013-11-07 23:33:24 +0000828 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000829 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000830 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000831 return EFailLink;
832
833 return 0;
834}
835
836//
837// Deduce the language from the filename. Files must end in one of the
838// following extensions:
839//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000840// .vert = vertex
841// .tesc = tessellation control
842// .tese = tessellation evaluation
843// .geom = geometry
844// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000845// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000846//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600847EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000848{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600849 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600850 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600851
Dan Bakerc6ede892016-08-11 14:06:06 -0400852 if (shaderStageName)
853 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600854 else {
855 // Search for a suffix on a filename: e.g, "myfile.frag". If given
856 // the suffix directly, we skip looking for the '.'
857 if (parseSuffix) {
858 ext = name.rfind('.');
859 if (ext == std::string::npos) {
860 usage();
861 return EShLangVertex;
862 }
863 ++ext;
864 }
865 suffix = name.substr(ext, std::string::npos);
866 }
dankbaker45d49bc2016-08-08 21:43:07 -0400867
John Kessenich2b07c7e2013-07-31 18:44:13 +0000868 if (suffix == "vert")
869 return EShLangVertex;
870 else if (suffix == "tesc")
871 return EShLangTessControl;
872 else if (suffix == "tese")
873 return EShLangTessEvaluation;
874 else if (suffix == "geom")
875 return EShLangGeometry;
876 else if (suffix == "frag")
877 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000878 else if (suffix == "comp")
879 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000880
881 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000882 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000883}
884
John Kessenicha0af4732012-12-12 21:15:54 +0000885//
John Kessenichecba76f2017-01-06 00:34:48 -0700886// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000887// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000888//
John Kessenich51cdd902014-02-18 23:37:57 +0000889void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000890{
John Kessenichca3457f2015-05-18 01:59:45 +0000891 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000892 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000893 if (! shaderStrings) {
894 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000895 }
896
John Kessenich41cf6b52013-06-25 18:10:05 +0000897 int* lengths = new int[NumShaderStrings];
898
899 // move to length-based strings, rather than null-terminated strings
900 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000901 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000902
John Kessenichc999ba22013-11-07 23:33:24 +0000903 if (! shaderStrings) {
904 CompileFailed = true;
905 return;
906 }
John Kessenicha0af4732012-12-12 21:15:54 +0000907
John Kessenich52ac67e2013-05-05 23:46:22 +0000908 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000909 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700910
John Kessenich94a81fb2013-08-31 02:41:30 +0000911 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
912 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700913 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000914 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700915 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700916 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000917 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700918 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
919 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000920 }
John Kessenicha0af4732012-12-12 21:15:54 +0000921
John Kessenich94a81fb2013-08-31 02:41:30 +0000922 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000923 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000924 }
John Kessenicha0af4732012-12-12 21:15:54 +0000925
John Kessenich41cf6b52013-06-25 18:10:05 +0000926 delete [] lengths;
927 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000928
John Kessenichc999ba22013-11-07 23:33:24 +0000929 if (ret == 0)
930 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000931}
932
John Kessenicha0af4732012-12-12 21:15:54 +0000933//
934// print usage to stdout
935//
936void usage()
937{
John Kessenich319de232013-12-04 04:43:40 +0000938 printf("Usage: glslangValidator [option]... [file]...\n"
939 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000940 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600941 " .conf to provide an optional config file that replaces the default configuration\n"
942 " (see -c option below for generating a template)\n"
943 " .vert for a vertex shader\n"
944 " .tesc for a tessellation control shader\n"
945 " .tese for a tessellation evaluation shader\n"
946 " .geom for a geometry shader\n"
947 " .frag for a fragment shader\n"
948 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000949 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000950 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000951 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000952 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600953 "Each option must be specified separately.\n"
954 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
955 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600956 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
957 " default file name is <stage>.spv (-o overrides this)\n"
958 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400959 " -E print pre-processed GLSL; cannot be used with -l;\n"
960 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -0600961 " -S <stage> uses specified stage rather than parsing the file extension\n"
962 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600963 " -c configuration dump;\n"
964 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600965 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600966 " -d default to desktop (#version 110) when there is no shader #version\n"
967 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700968 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700969 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600970 " -h print this usage message\n"
971 " -i intermediate tree (glslang AST) is printed out\n"
972 " -l link all input files together to form a single module\n"
973 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500974 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600975 " -q dump reflection query database\n"
976 " -r relaxed semantic error-checking mode\n"
977 " -s silent mode\n"
978 " -t multi-threaded mode\n"
979 " -v print version strings\n"
980 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500981 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600982 "\n"
983 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
984 " --ssb [stage] num synonym for --shift-sampler-binding\n"
985 "\n"
986 " --shift-texture-binding [stage] num set base binding number for textures\n"
987 " --stb [stage] num synonym for --shift-texture-binding\n"
988 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600989 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
990 " --sib [stage] num synonym for --shift-image-binding\n"
991 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600992 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
993 " --sub [stage] num synonym for --shift-UBO-binding\n"
994 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -0700995 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
996 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
997 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600998 " --auto-map-bindings automatically bind uniform variables without\n"
999 " explicit bindings.\n"
1000 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001001 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001002 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001003 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001004 "\n"
1005 " --no-storage-format use Unknown image format\n"
1006 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001007 "\n"
1008 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1009 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001010 "\n"
1011 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1012 " --ku synonym for --keep-uncalled\n"
John Kessenich8f674e82017-02-18 09:45:40 -07001013 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name> initialized with the shader binary code.\n"
1014 " --vn <name> synonym for --variable-name <name>.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001015 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001016
1017 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001018}
1019
John Kessenich3ce4e592014-10-06 19:57:34 +00001020#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001021
1022#include <errno.h>
1023
1024int fopen_s(
1025 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001026 const char* filename,
1027 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001028)
1029{
1030 if (!pFile || !filename || !mode) {
1031 return EINVAL;
1032 }
1033
1034 FILE* f = fopen(filename, mode);
1035 if (! f) {
1036 if (errno != 0) {
1037 return errno;
1038 } else {
1039 return ENOENT;
1040 }
1041 }
1042 *pFile = f;
1043
1044 return 0;
1045}
1046
1047#endif
1048
John Kessenicha0af4732012-12-12 21:15:54 +00001049//
1050// Malloc a string of sufficient size and read a string into it.
1051//
John Kessenichecba76f2017-01-06 00:34:48 -07001052char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001053{
John Kessenichb3297152015-07-11 18:01:03 -06001054 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001055 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001056
John Kessenicha0af4732012-12-12 21:15:54 +00001057 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001058 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1059 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001060
John Kessenich68d78fd2015-07-12 19:28:10 -06001061 if (errorCode || in == nullptr)
1062 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001063
John Kessenicha0af4732012-12-12 21:15:54 +00001064 while (fgetc(in) != EOF)
1065 count++;
1066
John Kessenichd6c72a42014-08-18 19:42:35 +00001067 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001068
John Kessenichb3297152015-07-11 18:01:03 -06001069 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001070 if (! fdata)
1071 Error("can't allocate memory");
1072
John Kessenichb3297152015-07-11 18:01:03 -06001073 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001074 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001075 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001076 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001077
John Kessenicha0af4732012-12-12 21:15:54 +00001078 fdata[count] = '\0';
1079 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001080
John Kessenichea869fb2013-10-28 18:12:06 +00001081 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001082 // recover from empty file
1083 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001084 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001085 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001086 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001087
John Kessenichb3297152015-07-11 18:01:03 -06001088 return return_data;
1089 } else
1090 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1091
1092 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001093 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001094 int ptr_len = 0;
1095 int i = 0;
1096 while (count > 0) {
1097 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1098 memcpy(return_data[i], fdata + ptr_len, len);
1099 return_data[i][len] = '\0';
1100 count -= len;
1101 ptr_len += len;
1102 if (count < len) {
1103 if (count == 0) {
1104 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001105 break;
1106 }
John Kessenichb3297152015-07-11 18:01:03 -06001107 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001108 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001109 ++i;
1110 }
John Kessenichb3297152015-07-11 18:01:03 -06001111
1112 free(fdata);
1113
John Kessenicha0af4732012-12-12 21:15:54 +00001114 return return_data;
1115}
1116
John Kessenich51cdd902014-02-18 23:37:57 +00001117void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001118{
John Kessenichb3297152015-07-11 18:01:03 -06001119 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001120 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001121
1122 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001123}
1124
John Kessenich54d8cda2013-02-11 22:36:01 +00001125void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001126{
John Kessenichfae38ee2015-06-10 23:23:12 +00001127 if (num >= 0 )
1128 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1129 else
1130 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001131}