blob: fb37f8f2b705e488e2a33c9459950dde041f1e3b [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;
172
John Kessenich68d78fd2015-07-12 19:28:10 -0600173//
174// Create the default name for saving a binary if -o is not provided.
175//
176const char* GetBinaryName(EShLanguage stage)
177{
178 const char* name;
179 if (binaryFileName == nullptr) {
180 switch (stage) {
181 case EShLangVertex: name = "vert.spv"; break;
182 case EShLangTessControl: name = "tesc.spv"; break;
183 case EShLangTessEvaluation: name = "tese.spv"; break;
184 case EShLangGeometry: name = "geom.spv"; break;
185 case EShLangFragment: name = "frag.spv"; break;
186 case EShLangCompute: name = "comp.spv"; break;
187 default: name = "unknown"; break;
188 }
189 } else
190 name = binaryFileName;
191
192 return name;
193}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000194
John Kessenich05a70632013-09-17 19:26:08 +0000195//
196// *.conf => this is a config file that can set limits/resources
197//
198bool SetConfigFile(const std::string& name)
199{
200 if (name.size() < 5)
201 return false;
202
John Kessenich4c706852013-10-11 16:28:43 +0000203 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000204 ConfigFile = name;
205 return true;
206 }
207
208 return false;
209}
210
John Kessenich68d78fd2015-07-12 19:28:10 -0600211//
212// Give error and exit with failure code.
213//
214void Error(const char* message)
215{
216 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
217 exit(EFailUsage);
218}
219
220//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600221// Process an optional binding base of the form:
222// --argname [stage] base
223// Where stage is one of the forms accepted by FindLanguage, and base is an integer
224//
225void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
226{
227 if (argc < 2)
228 usage();
229
230 if (!isdigit(argv[1][0])) {
231 if (argc < 3) // this form needs one more argument
232 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700233
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600234 // Parse form: --argname stage base
235 const EShLanguage lang = FindLanguage(argv[1], false);
236 base[lang] = atoi(argv[2]);
237 argc-= 2;
238 argv+= 2;
239 } else {
240 // Parse form: --argname base
241 for (int lang=0; lang<EShLangCount; ++lang)
242 base[lang] = atoi(argv[1]);
243
244 argc--;
245 argv++;
246 }
247}
248
249//
John Kessenich68d78fd2015-07-12 19:28:10 -0600250// Do all command-line argument parsing. This includes building up the work-items
251// to be processed later, and saving all the command-line options.
252//
253// Does not return (it exits) if command-line is fatally flawed.
254//
255void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000256{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600257 baseSamplerBinding.fill(0);
258 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600259 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600260 baseUboBinding.fill(0);
261
John Kessenich38f3b892013-09-06 19:52:57 +0000262 ExecutableName = argv[0];
263 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 +0000264 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600265 for (int w = 0; w < NumWorkItems; ++w)
266 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000267
John Kessenich2b07c7e2013-07-31 18:44:13 +0000268 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700269 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000270 for (; argc >= 1; argc--, argv++) {
271 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000272 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600273 case '-':
274 {
275 std::string lowerword(argv[0]+2);
276 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
277
278 // handle --word style options
279 if (lowerword == "shift-sampler-bindings" || // synonyms
280 lowerword == "shift-sampler-binding" ||
281 lowerword == "ssb") {
282 ProcessBindingBase(argc, argv, baseSamplerBinding);
283 } else if (lowerword == "shift-texture-bindings" || // synonyms
284 lowerword == "shift-texture-binding" ||
285 lowerword == "stb") {
286 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600287 } else if (lowerword == "shift-image-bindings" || // synonyms
288 lowerword == "shift-image-binding" ||
289 lowerword == "sib") {
290 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600291 } else if (lowerword == "shift-ubo-bindings" || // synonyms
292 lowerword == "shift-ubo-binding" ||
293 lowerword == "sub") {
294 ProcessBindingBase(argc, argv, baseUboBinding);
295 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700296 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600297 lowerword == "amb") {
298 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600299 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
300 lowerword == "flatten-uniform-array" ||
301 lowerword == "fua") {
302 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600303 } else if (lowerword == "no-storage-format" || // synonyms
304 lowerword == "nsf") {
305 Options |= EOptionNoStorageFormat;
Flavioaea3c892017-02-06 11:46:35 -0800306 }
307 else if (lowerword == "variable-name" || // synonyms
308 lowerword == "vn") {
309 Options |= EOptionOutputHexadecimal;
310 variableName = argv[1];
311 if (argc > 0) {
312 argc--;
313 argv++;
314 }
315 else
316 Error("no <C-variable-name> provided for --variable-name");
317 break;
318 }
319 else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600320 lowerword == "sep") {
321 sourceEntryPointName = argv[1];
322 if (argc > 0) {
323 argc--;
324 argv++;
325 } else
326 Error("no <entry-point> provided for --source-entrypoint");
327 break;
John Kessenich906cc212016-12-09 19:22:20 -0700328 } else if (lowerword == "keep-uncalled" || // synonyms
329 lowerword == "ku") {
330 Options |= EOptionKeepUncalled;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600331 } else {
332 usage();
333 }
334 }
335 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000336 case 'H':
337 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600338 if ((Options & EOptionSpv) == 0) {
339 // default to Vulkan
340 Options |= EOptionSpv;
341 Options |= EOptionVulkanRules;
342 Options |= EOptionLinkProgram;
343 }
344 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000345 case 'V':
346 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600347 Options |= EOptionVulkanRules;
348 Options |= EOptionLinkProgram;
349 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400350 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400351 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400352 if (argc > 0) {
353 argc--;
354 argv++;
355 }
356 else
Dan Baker5afdd782016-08-11 17:53:57 -0400357 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400358 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600359 case 'G':
360 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000361 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600362 // undo a -H default to Vulkan
363 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000364 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000365 case 'E':
366 Options |= EOptionOutputPreprocessed;
367 break;
John Kessenich05a70632013-09-17 19:26:08 +0000368 case 'c':
369 Options |= EOptionDumpConfig;
370 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600371 case 'C':
372 Options |= EOptionCascadingErrors;
373 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000374 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000375 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000376 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700377 case 'D':
378 Options |= EOptionReadHlsl;
379 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700380 case 'e':
381 // HLSL todo: entry point handle needs much more sophistication.
382 // This is okay for one compilation unit with one entry point.
383 entryPointName = argv[1];
384 if (argc > 0) {
385 argc--;
386 argv++;
387 } else
388 Error("no <entry-point> provided for -e");
389 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600390 case 'h':
391 usage();
392 break;
John Kessenich05a70632013-09-17 19:26:08 +0000393 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000394 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000395 break;
396 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000397 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000398 break;
399 case 'm':
400 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000401 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600402 case 'o':
403 binaryFileName = argv[1];
404 if (argc > 0) {
405 argc--;
406 argv++;
407 } else
408 Error("no <file> provided for -o");
409 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000410 case 'q':
411 Options |= EOptionDumpReflection;
412 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000413 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000414 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000415 break;
416 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000417 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000418 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000419 case 't':
420 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000421 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000422 #endif
423 break;
John Kessenich319de232013-12-04 04:43:40 +0000424 case 'v':
425 Options |= EOptionDumpVersions;
426 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000427 case 'w':
428 Options |= EOptionSuppressWarnings;
429 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500430 case 'x':
431 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500432 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000433 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600434 usage();
435 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000436 }
John Kessenich38f3b892013-09-06 19:52:57 +0000437 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000438 std::string name(argv[0]);
439 if (! SetConfigFile(name)) {
440 Work[argc] = new glslang::TWorkItem(name);
441 Worklist.add(Work[argc]);
442 }
John Kessenich38f3b892013-09-06 19:52:57 +0000443 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000444 }
445
John Kessenich68d78fd2015-07-12 19:28:10 -0600446 // Make sure that -E is not specified alongside linking (which includes SPV generation)
447 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
448 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000449
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500450 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600451 if (binaryFileName && (Options & EOptionSpv) == 0)
452 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600453
454 if ((Options & EOptionFlattenUniformArrays) != 0 &&
455 (Options & EOptionReadHlsl) == 0)
456 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000457}
458
John Kessenich68d78fd2015-07-12 19:28:10 -0600459//
460// Translate the meaningful subset of command-line options to parser-behavior options.
461//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000462void SetMessageOptions(EShMessages& messages)
463{
464 if (Options & EOptionRelaxedErrors)
465 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
466 if (Options & EOptionIntermediate)
467 messages = (EShMessages)(messages | EShMsgAST);
468 if (Options & EOptionSuppressWarnings)
469 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600470 if (Options & EOptionSpv)
471 messages = (EShMessages)(messages | EShMsgSpvRules);
472 if (Options & EOptionVulkanRules)
473 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400474 if (Options & EOptionOutputPreprocessed)
475 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700476 if (Options & EOptionReadHlsl)
477 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600478 if (Options & EOptionCascadingErrors)
479 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700480 if (Options & EOptionKeepUncalled)
481 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000482}
483
John Kessenich68d78fd2015-07-12 19:28:10 -0600484//
John Kessenich69f4b512013-09-04 21:19:27 +0000485// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000486//
487// Return 0 for failure, 1 for success.
488//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800489unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000490{
John Kessenich38f3b892013-09-06 19:52:57 +0000491 glslang::TWorkItem* workItem;
492 while (Worklist.remove(workItem)) {
493 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000494 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000495 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000496
John Kessenichb0a7eb52013-11-07 17:44:20 +0000497 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498
John Kessenich94a81fb2013-08-31 02:41:30 +0000499 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000500 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000501
502 ShDestruct(compiler);
503 }
504
505 return 0;
506}
507
John Kessenich6626cad2015-06-19 05:14:19 +0000508// Outputs the given string, but only if it is non-null and non-empty.
509// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400510void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000511{
512 if (str && str[0]) {
513 puts(str);
514 }
515}
516
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400517// Outputs the given string to stderr, but only if it is non-null and non-empty.
518// This prevents erroneous newlines from appearing.
519void StderrIfNonEmpty(const char* str)
520{
521 if (str && str[0]) {
522 fprintf(stderr, "%s\n", str);
523 }
524}
525
John Kessenichc57b2a92016-01-16 15:30:03 -0700526// Simple bundling of what makes a compilation unit for ease in passing around,
527// and separation of handling file IO versus API (programmatic) compilation.
528struct ShaderCompUnit {
529 EShLanguage stage;
530 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400531 char** text; // memory owned/managed externally
532 const char* fileNameList[1];
533
John Kessenich219b0252016-08-23 17:51:13 -0600534 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400535 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
536 {
537 stage = istage;
538 fileName = ifileName;
539 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600540 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400541 }
542
543 ShaderCompUnit(const ShaderCompUnit &rhs)
544 {
545 stage = rhs.stage;
546 fileName = rhs.fileName;
547 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600548 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400549 }
550
John Kessenichc57b2a92016-01-16 15:30:03 -0700551};
552
John Kessenich69f4b512013-09-04 21:19:27 +0000553//
John Kessenichc57b2a92016-01-16 15:30:03 -0700554// For linking mode: Will independently parse each compilation unit, but then put them
555// in the same program and link them together, making at most one linked module per
556// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000557//
558// Uses the new C++ interface instead of the old handle-based interface.
559//
John Kessenichc57b2a92016-01-16 15:30:03 -0700560
561void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000562{
563 // keep track of what to free
564 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700565
John Kessenich69f4b512013-09-04 21:19:27 +0000566 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000567 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000568
John Kessenich69f4b512013-09-04 21:19:27 +0000569 //
570 // Per-shader processing...
571 //
572
John Kessenich5b0f13a2013-11-01 03:08:40 +0000573 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100574 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
575 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700576 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400577 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700578 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
579 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600580 if (sourceEntryPointName)
581 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600582
583 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
584 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600585 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600586 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600587 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600588 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600589
590 if (Options & EOptionAutoMapBindings)
591 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700592
John Kessenich69f4b512013-09-04 21:19:27 +0000593 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600594
John Kessenichc555ddd2015-06-17 02:38:44 +0000595 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000596
John Kessenichc555ddd2015-06-17 02:38:44 +0000597 if (Options & EOptionOutputPreprocessed) {
598 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700599 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400600 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500601 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400602 PutsIfNonEmpty(str.c_str());
603 } else {
604 CompileFailed = true;
605 }
606 StderrIfNonEmpty(shader->getInfoLog());
607 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000608 continue;
609 }
610 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000611 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000612
John Kessenich69f4b512013-09-04 21:19:27 +0000613 program.addShader(shader);
614
John Kessenichc57b2a92016-01-16 15:30:03 -0700615 if (! (Options & EOptionSuppressInfolog) &&
616 ! (Options & EOptionMemoryLeakMode)) {
617 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400618 PutsIfNonEmpty(shader->getInfoLog());
619 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000620 }
John Kessenich69f4b512013-09-04 21:19:27 +0000621 }
622
623 //
624 // Program-level processing...
625 //
626
John Kessenich4d65ee32016-03-12 18:17:47 -0700627 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600628 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000629 LinkFailed = true;
630
steve-lunarg9ae34742016-10-05 13:40:13 -0600631 // Map IO
632 if (Options & EOptionSpv) {
633 if (!program.mapIO())
634 LinkFailed = true;
635 }
John Kessenichecba76f2017-01-06 00:34:48 -0700636
John Kessenich4d65ee32016-03-12 18:17:47 -0700637 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700638 if (! (Options & EOptionSuppressInfolog) &&
639 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400640 PutsIfNonEmpty(program.getInfoLog());
641 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000642 }
643
John Kessenich4d65ee32016-03-12 18:17:47 -0700644 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000645 if (Options & EOptionDumpReflection) {
646 program.buildReflection();
647 program.dumpReflection();
648 }
649
John Kessenich4d65ee32016-03-12 18:17:47 -0700650 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000651 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000652 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600653 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000654 else {
655 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000656 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000657 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400658 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400659 spv::SpvBuildLogger logger;
660 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700661
662 // Dump the spv to a file or stdout, etc., but only if not doing
663 // memory/perf testing, as it's not internal to programmatic use.
664 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500665 printf("%s", logger.getAllMessages().c_str());
666 if (Options & EOptionOutputHexadecimal) {
Flavioaea3c892017-02-06 11:46:35 -0800667 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500668 } else {
669 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
670 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700671 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700672 spv::Disassemble(std::cout, spirv);
673 }
John Kessenichacba7722015-03-04 03:48:38 +0000674 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000675 }
John Kessenich92f90382014-07-28 04:21:04 +0000676 }
677 }
678 }
679
John Kessenich5b0f13a2013-11-01 03:08:40 +0000680 // Free everything up, program has to go before the shaders
681 // because it might have merged stuff from the shaders, and
682 // the stuff from the shaders has to have its destructors called
683 // before the pools holding the memory in the shaders is freed.
684 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000685 while (shaders.size() > 0) {
686 delete shaders.back();
687 shaders.pop_back();
688 }
John Kessenich69f4b512013-09-04 21:19:27 +0000689}
690
John Kessenichc57b2a92016-01-16 15:30:03 -0700691//
692// Do file IO part of compile and link, handing off the pure
693// API/programmatic mode to CompileAndLinkShaderUnits(), which can
694// be put in a loop for testing memory footprint and performance.
695//
696// This is just for linking mode: meaning all the shaders will be put into the
697// the same program linked together.
698//
699// This means there are a limited number of work items (not multi-threading mode)
700// and that the point is testing at the linking level. Hence, to enable
701// performance and memory testing, the actual compile/link can be put in
702// a loop, independent of processing the work items and file IO.
703//
704void CompileAndLinkShaderFiles()
705{
706 std::vector<ShaderCompUnit> compUnits;
707
708 // Transfer all the work items from to a simple list of
709 // of compilation units. (We don't care about the thread
710 // work-item distribution properties in this path, which
711 // is okay due to the limited number of shaders, know since
712 // they are all getting linked together.)
713 glslang::TWorkItem* workItem;
714 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200715 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700716 FindLanguage(workItem->name),
717 workItem->name,
718 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200719 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700720
721 if (! compUnit.text) {
722 usage();
723 return;
724 }
725
726 compUnits.push_back(compUnit);
727 }
728
729 // Actual call to programmatic processing of compile and link,
730 // in a loop for testing memory and performance. This part contains
731 // all the perf/memory that a programmatic consumer will care about.
732 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
733 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
734 CompileAndLinkShaderUnits(compUnits);
735
736 if (Options & EOptionMemoryLeakMode)
737 glslang::OS_DumpMemoryCounters();
738 }
739
rdb32084e82016-02-23 22:17:38 +0100740 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
741 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700742}
743
John Kessenicha0af4732012-12-12 21:15:54 +0000744int C_DECL main(int argc, char* argv[])
745{
John Kessenich68d78fd2015-07-12 19:28:10 -0600746 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000747
John Kessenich05a70632013-09-17 19:26:08 +0000748 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500749 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000750 if (Worklist.empty())
751 return ESuccess;
752 }
753
John Kessenich0da9eaa2015-08-01 17:10:02 -0600754 if (Options & EOptionDumpVersions) {
755 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000756 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
757 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600758 std::string spirvVersion;
759 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600760 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600761 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700762 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600763 printf("GL_KHR_vulkan_glsl version %d\n", 100);
764 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000765 if (Worklist.empty())
766 return ESuccess;
767 }
768
John Kessenich05a70632013-09-17 19:26:08 +0000769 if (Worklist.empty()) {
770 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000771 }
772
773 ProcessConfigFile();
774
John Kessenich69f4b512013-09-04 21:19:27 +0000775 //
776 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000777 // 1) linking all arguments together, single-threaded, new C++ interface
778 // 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 +0000779 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000780 if (Options & EOptionLinkProgram ||
781 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000782 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700783 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000784 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500785 for (int w = 0; w < NumWorkItems; ++w) {
786 if (Work[w]) {
787 delete Work[w];
788 }
789 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000790 } else {
791 ShInitialize();
792
John Kessenich38f3b892013-09-06 19:52:57 +0000793 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000794
John Kessenich38f3b892013-09-06 19:52:57 +0000795 if (Options & EOptionMultiThreaded) {
796 const int NumThreads = 16;
797 void* threads[NumThreads];
798 for (int t = 0; t < NumThreads; ++t) {
799 threads[t] = glslang::OS_CreateThread(&CompileShaders);
800 if (! threads[t]) {
801 printf("Failed to create thread\n");
802 return EFailThreadCreate;
803 }
John Kessenicha0af4732012-12-12 21:15:54 +0000804 }
John Kessenich38f3b892013-09-06 19:52:57 +0000805 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000806 } else
807 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000808
809 // Print out all the resulting infologs
810 for (int w = 0; w < NumWorkItems; ++w) {
811 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600812 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400813 PutsIfNonEmpty(Work[w]->name.c_str());
814 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000815 delete Work[w];
816 }
817 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000818
819 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000820 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000821
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500822 delete[] Work;
823
John Kessenichc999ba22013-11-07 23:33:24 +0000824 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000825 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000826 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000827 return EFailLink;
828
829 return 0;
830}
831
832//
833// Deduce the language from the filename. Files must end in one of the
834// following extensions:
835//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000836// .vert = vertex
837// .tesc = tessellation control
838// .tese = tessellation evaluation
839// .geom = geometry
840// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000841// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000842//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600843EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000844{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600845 size_t ext = 0;
846
847 // Search for a suffix on a filename: e.g, "myfile.frag". If given
848 // the suffix directly, we skip looking the '.'
849 if (parseSuffix) {
850 ext = name.rfind('.');
851 if (ext == std::string::npos) {
852 usage();
853 return EShLangVertex;
854 }
855 ++ext;
John Kessenicha0af4732012-12-12 21:15:54 +0000856 }
857
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600858 std::string suffix = name.substr(ext, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400859 if (shaderStageName)
860 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400861
John Kessenich2b07c7e2013-07-31 18:44:13 +0000862 if (suffix == "vert")
863 return EShLangVertex;
864 else if (suffix == "tesc")
865 return EShLangTessControl;
866 else if (suffix == "tese")
867 return EShLangTessEvaluation;
868 else if (suffix == "geom")
869 return EShLangGeometry;
870 else if (suffix == "frag")
871 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000872 else if (suffix == "comp")
873 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000874
875 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000876 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000877}
878
John Kessenicha0af4732012-12-12 21:15:54 +0000879//
John Kessenichecba76f2017-01-06 00:34:48 -0700880// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000881// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000882//
John Kessenich51cdd902014-02-18 23:37:57 +0000883void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000884{
John Kessenichca3457f2015-05-18 01:59:45 +0000885 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000886 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000887 if (! shaderStrings) {
888 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000889 }
890
John Kessenich41cf6b52013-06-25 18:10:05 +0000891 int* lengths = new int[NumShaderStrings];
892
893 // move to length-based strings, rather than null-terminated strings
894 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000895 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000896
John Kessenichc999ba22013-11-07 23:33:24 +0000897 if (! shaderStrings) {
898 CompileFailed = true;
899 return;
900 }
John Kessenicha0af4732012-12-12 21:15:54 +0000901
John Kessenich52ac67e2013-05-05 23:46:22 +0000902 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000903 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700904
John Kessenich94a81fb2013-08-31 02:41:30 +0000905 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
906 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700907 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000908 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700909 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700910 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000911 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700912 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
913 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000914 }
John Kessenicha0af4732012-12-12 21:15:54 +0000915
John Kessenich94a81fb2013-08-31 02:41:30 +0000916 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000917 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000918 }
John Kessenicha0af4732012-12-12 21:15:54 +0000919
John Kessenich41cf6b52013-06-25 18:10:05 +0000920 delete [] lengths;
921 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000922
John Kessenichc999ba22013-11-07 23:33:24 +0000923 if (ret == 0)
924 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000925}
926
John Kessenicha0af4732012-12-12 21:15:54 +0000927//
928// print usage to stdout
929//
930void usage()
931{
John Kessenich319de232013-12-04 04:43:40 +0000932 printf("Usage: glslangValidator [option]... [file]...\n"
933 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000934 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600935 " .conf to provide an optional config file that replaces the default configuration\n"
936 " (see -c option below for generating a template)\n"
937 " .vert for a vertex shader\n"
938 " .tesc for a tessellation control shader\n"
939 " .tese for a tessellation evaluation shader\n"
940 " .geom for a geometry shader\n"
941 " .frag for a fragment shader\n"
942 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000943 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000944 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000945 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000946 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600947 "Each option must be specified separately.\n"
948 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
949 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600950 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
951 " default file name is <stage>.spv (-o overrides this)\n"
952 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400953 " -E print pre-processed GLSL; cannot be used with -l;\n"
954 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400955 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
Dan Baker895275e2016-08-11 14:55:49 -0400956 " valid choices are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600957 " -c configuration dump;\n"
958 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600959 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600960 " -d default to desktop (#version 110) when there is no shader #version\n"
961 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700962 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700963 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600964 " -h print this usage message\n"
965 " -i intermediate tree (glslang AST) is printed out\n"
966 " -l link all input files together to form a single module\n"
967 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500968 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600969 " -q dump reflection query database\n"
970 " -r relaxed semantic error-checking mode\n"
971 " -s silent mode\n"
972 " -t multi-threaded mode\n"
973 " -v print version strings\n"
974 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500975 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600976 "\n"
977 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
978 " --ssb [stage] num synonym for --shift-sampler-binding\n"
979 "\n"
980 " --shift-texture-binding [stage] num set base binding number for textures\n"
981 " --stb [stage] num synonym for --shift-texture-binding\n"
982 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600983 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
984 " --sib [stage] num synonym for --shift-image-binding\n"
985 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600986 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
987 " --sub [stage] num synonym for --shift-UBO-binding\n"
988 "\n"
989 " --auto-map-bindings automatically bind uniform variables without\n"
990 " explicit bindings.\n"
991 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600992 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -0600993 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600994 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -0600995 "\n"
996 " --no-storage-format use Unknown image format\n"
997 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -0600998 "\n"
999 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1000 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001001 "\n"
1002 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1003 " --ku synonym for --keep-uncalled\n"
Flavioaea3c892017-02-06 11:46:35 -08001004 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name> initialized with the shader binary code.\n"
1005 " --vn <name> synonym for --variable-name <name>.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001006 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001007
1008 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001009}
1010
John Kessenich3ce4e592014-10-06 19:57:34 +00001011#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001012
1013#include <errno.h>
1014
1015int fopen_s(
1016 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001017 const char* filename,
1018 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001019)
1020{
1021 if (!pFile || !filename || !mode) {
1022 return EINVAL;
1023 }
1024
1025 FILE* f = fopen(filename, mode);
1026 if (! f) {
1027 if (errno != 0) {
1028 return errno;
1029 } else {
1030 return ENOENT;
1031 }
1032 }
1033 *pFile = f;
1034
1035 return 0;
1036}
1037
1038#endif
1039
John Kessenicha0af4732012-12-12 21:15:54 +00001040//
1041// Malloc a string of sufficient size and read a string into it.
1042//
John Kessenichecba76f2017-01-06 00:34:48 -07001043char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001044{
John Kessenichb3297152015-07-11 18:01:03 -06001045 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001046 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001047
John Kessenicha0af4732012-12-12 21:15:54 +00001048 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001049 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1050 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001051
John Kessenich68d78fd2015-07-12 19:28:10 -06001052 if (errorCode || in == nullptr)
1053 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001054
John Kessenicha0af4732012-12-12 21:15:54 +00001055 while (fgetc(in) != EOF)
1056 count++;
1057
John Kessenichd6c72a42014-08-18 19:42:35 +00001058 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001059
John Kessenichb3297152015-07-11 18:01:03 -06001060 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001061 if (! fdata)
1062 Error("can't allocate memory");
1063
John Kessenichb3297152015-07-11 18:01:03 -06001064 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001065 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001066 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001067 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001068
John Kessenicha0af4732012-12-12 21:15:54 +00001069 fdata[count] = '\0';
1070 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001071
John Kessenichea869fb2013-10-28 18:12:06 +00001072 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001073 // recover from empty file
1074 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001075 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001076 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001077 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001078
John Kessenichb3297152015-07-11 18:01:03 -06001079 return return_data;
1080 } else
1081 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1082
1083 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001084 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001085 int ptr_len = 0;
1086 int i = 0;
1087 while (count > 0) {
1088 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1089 memcpy(return_data[i], fdata + ptr_len, len);
1090 return_data[i][len] = '\0';
1091 count -= len;
1092 ptr_len += len;
1093 if (count < len) {
1094 if (count == 0) {
1095 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001096 break;
1097 }
John Kessenichb3297152015-07-11 18:01:03 -06001098 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001099 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001100 ++i;
1101 }
John Kessenichb3297152015-07-11 18:01:03 -06001102
1103 free(fdata);
1104
John Kessenicha0af4732012-12-12 21:15:54 +00001105 return return_data;
1106}
1107
John Kessenich51cdd902014-02-18 23:37:57 +00001108void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001109{
John Kessenichb3297152015-07-11 18:01:03 -06001110 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001111 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001112
1113 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001114}
1115
John Kessenich54d8cda2013-02-11 22:36:01 +00001116void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001117{
John Kessenichfae38ee2015-06-10 23:23:12 +00001118 if (num >= 0 )
1119 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1120 else
1121 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001122}