blob: e6c0ab702cc2c52332a399fa1f12b31dd112a317 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
2//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06003//Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenicha0af4732012-12-12 21:15:54 +00005//All rights reserved.
6//
7//Redistribution and use in source and binary forms, with or without
8//modification, are permitted provided that the following conditions
9//are met:
10//
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//
23//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.
35//
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 Kessenicha86836e2016-07-09 14:50:57 -060063 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),
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060083 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060084 EOptionFlattenUniformArrays = (1 << 20),
steve-lunargcce8d482016-10-14 18:36:42 -060085 EOptionNoStorageFormat = (1 << 21),
John Kessenich94a81fb2013-08-31 02:41:30 +000086};
87
John Kessenicha0af4732012-12-12 21:15:54 +000088//
John Kessenich68d78fd2015-07-12 19:28:10 -060089// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000090//
91enum TFailCode {
92 ESuccess = 0,
93 EFailUsage,
94 EFailCompile,
95 EFailLink,
96 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000097 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000098 EFailLinkerCreate
99};
100
101//
John Kessenich68d78fd2015-07-12 19:28:10 -0600102// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000103//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600104EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000105void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000106void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000107void FreeFileData(char** data);
108char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000109void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000110
John Kessenichc999ba22013-11-07 23:33:24 +0000111// Globally track if any compile or link failure.
112bool CompileFailed = false;
113bool LinkFailed = false;
114
John Kessenich05a70632013-09-17 19:26:08 +0000115// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600116// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000117int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000118
John Kessenich05a70632013-09-17 19:26:08 +0000119TBuiltInResource Resources;
120std::string ConfigFile;
121
John Kessenicha0af4732012-12-12 21:15:54 +0000122//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600123// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000124//
125void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000126{
John Kessenich05a70632013-09-17 19:26:08 +0000127 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000128 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000129 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000130 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000131 if (configStrings)
132 config = *configStrings;
133 else {
134 printf("Error opening configuration file; will instead use the default configuration\n");
135 usage();
136 }
137 }
138
139 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500140 Resources = glslang::DefaultTBuiltInResource;
141 return;
John Kessenich05a70632013-09-17 19:26:08 +0000142 }
143
Lei Zhang1b141722016-05-19 13:50:49 -0400144 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000145
John Kessenich05a70632013-09-17 19:26:08 +0000146 if (configStrings)
147 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500148 else
149 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000150}
151
John Kessenich38f3b892013-09-06 19:52:57 +0000152// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000153glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000154
155// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000156glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000157int NumWorkItems = 0;
158
John Kessenich94a81fb2013-08-31 02:41:30 +0000159int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600160const char* ExecutableName = nullptr;
161const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700162const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600163const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400164const char* shaderStageName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600165
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600166std::array<unsigned int, EShLangCount> baseSamplerBinding;
167std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600168std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600169std::array<unsigned int, EShLangCount> baseUboBinding;
170
John Kessenich68d78fd2015-07-12 19:28:10 -0600171//
172// Create the default name for saving a binary if -o is not provided.
173//
174const char* GetBinaryName(EShLanguage stage)
175{
176 const char* name;
177 if (binaryFileName == nullptr) {
178 switch (stage) {
179 case EShLangVertex: name = "vert.spv"; break;
180 case EShLangTessControl: name = "tesc.spv"; break;
181 case EShLangTessEvaluation: name = "tese.spv"; break;
182 case EShLangGeometry: name = "geom.spv"; break;
183 case EShLangFragment: name = "frag.spv"; break;
184 case EShLangCompute: name = "comp.spv"; break;
185 default: name = "unknown"; break;
186 }
187 } else
188 name = binaryFileName;
189
190 return name;
191}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000192
John Kessenich05a70632013-09-17 19:26:08 +0000193//
194// *.conf => this is a config file that can set limits/resources
195//
196bool SetConfigFile(const std::string& name)
197{
198 if (name.size() < 5)
199 return false;
200
John Kessenich4c706852013-10-11 16:28:43 +0000201 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000202 ConfigFile = name;
203 return true;
204 }
205
206 return false;
207}
208
John Kessenich68d78fd2015-07-12 19:28:10 -0600209//
210// Give error and exit with failure code.
211//
212void Error(const char* message)
213{
214 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
215 exit(EFailUsage);
216}
217
218//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600219// Process an optional binding base of the form:
220// --argname [stage] base
221// Where stage is one of the forms accepted by FindLanguage, and base is an integer
222//
223void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
224{
225 if (argc < 2)
226 usage();
227
228 if (!isdigit(argv[1][0])) {
229 if (argc < 3) // this form needs one more argument
230 usage();
231
232 // Parse form: --argname stage base
233 const EShLanguage lang = FindLanguage(argv[1], false);
234 base[lang] = atoi(argv[2]);
235 argc-= 2;
236 argv+= 2;
237 } else {
238 // Parse form: --argname base
239 for (int lang=0; lang<EShLangCount; ++lang)
240 base[lang] = atoi(argv[1]);
241
242 argc--;
243 argv++;
244 }
245}
246
247//
John Kessenich68d78fd2015-07-12 19:28:10 -0600248// Do all command-line argument parsing. This includes building up the work-items
249// to be processed later, and saving all the command-line options.
250//
251// Does not return (it exits) if command-line is fatally flawed.
252//
253void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000254{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600255 baseSamplerBinding.fill(0);
256 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600257 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600258 baseUboBinding.fill(0);
259
John Kessenich38f3b892013-09-06 19:52:57 +0000260 ExecutableName = argv[0];
261 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 +0000262 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600263 for (int w = 0; w < NumWorkItems; ++w)
264 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000265
John Kessenich2b07c7e2013-07-31 18:44:13 +0000266 argc--;
267 argv++;
268 for (; argc >= 1; argc--, argv++) {
269 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000270 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600271 case '-':
272 {
273 std::string lowerword(argv[0]+2);
274 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
275
276 // handle --word style options
277 if (lowerword == "shift-sampler-bindings" || // synonyms
278 lowerword == "shift-sampler-binding" ||
279 lowerword == "ssb") {
280 ProcessBindingBase(argc, argv, baseSamplerBinding);
281 } else if (lowerword == "shift-texture-bindings" || // synonyms
282 lowerword == "shift-texture-binding" ||
283 lowerword == "stb") {
284 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600285 } else if (lowerword == "shift-image-bindings" || // synonyms
286 lowerword == "shift-image-binding" ||
287 lowerword == "sib") {
288 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600289 } else if (lowerword == "shift-ubo-bindings" || // synonyms
290 lowerword == "shift-ubo-binding" ||
291 lowerword == "sub") {
292 ProcessBindingBase(argc, argv, baseUboBinding);
293 } else if (lowerword == "auto-map-bindings" || // synonyms
294 lowerword == "auto-map-binding" ||
295 lowerword == "amb") {
296 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600297 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
298 lowerword == "flatten-uniform-array" ||
299 lowerword == "fua") {
300 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600301 } else if (lowerword == "no-storage-format" || // synonyms
302 lowerword == "nsf") {
303 Options |= EOptionNoStorageFormat;
steve-lunargf1e0c872016-10-31 15:13:43 -0600304 } else if (lowerword == "source-entrypoint" || // synonyms
305 lowerword == "sep") {
306 sourceEntryPointName = argv[1];
307 if (argc > 0) {
308 argc--;
309 argv++;
310 } else
311 Error("no <entry-point> provided for --source-entrypoint");
312 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600313 } else {
314 usage();
315 }
316 }
317 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000318 case 'H':
319 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600320 if ((Options & EOptionSpv) == 0) {
321 // default to Vulkan
322 Options |= EOptionSpv;
323 Options |= EOptionVulkanRules;
324 Options |= EOptionLinkProgram;
325 }
326 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000327 case 'V':
328 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600329 Options |= EOptionVulkanRules;
330 Options |= EOptionLinkProgram;
331 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400332 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400333 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400334 if (argc > 0) {
335 argc--;
336 argv++;
337 }
338 else
Dan Baker5afdd782016-08-11 17:53:57 -0400339 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400340 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600341 case 'G':
342 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000343 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600344 // undo a -H default to Vulkan
345 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000346 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000347 case 'E':
348 Options |= EOptionOutputPreprocessed;
349 break;
John Kessenich05a70632013-09-17 19:26:08 +0000350 case 'c':
351 Options |= EOptionDumpConfig;
352 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600353 case 'C':
354 Options |= EOptionCascadingErrors;
355 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000356 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000357 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000358 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700359 case 'D':
360 Options |= EOptionReadHlsl;
361 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700362 case 'e':
363 // HLSL todo: entry point handle needs much more sophistication.
364 // This is okay for one compilation unit with one entry point.
365 entryPointName = argv[1];
366 if (argc > 0) {
367 argc--;
368 argv++;
369 } else
370 Error("no <entry-point> provided for -e");
371 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600372 case 'h':
373 usage();
374 break;
John Kessenich05a70632013-09-17 19:26:08 +0000375 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000376 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000377 break;
378 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000379 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000380 break;
381 case 'm':
382 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000383 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600384 case 'o':
385 binaryFileName = argv[1];
386 if (argc > 0) {
387 argc--;
388 argv++;
389 } else
390 Error("no <file> provided for -o");
391 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000392 case 'q':
393 Options |= EOptionDumpReflection;
394 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000395 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000396 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000397 break;
398 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000399 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000400 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000401 case 't':
402 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000403 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000404 #endif
405 break;
John Kessenich319de232013-12-04 04:43:40 +0000406 case 'v':
407 Options |= EOptionDumpVersions;
408 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000409 case 'w':
410 Options |= EOptionSuppressWarnings;
411 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500412 case 'x':
413 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500414 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000415 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600416 usage();
417 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000418 }
John Kessenich38f3b892013-09-06 19:52:57 +0000419 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000420 std::string name(argv[0]);
421 if (! SetConfigFile(name)) {
422 Work[argc] = new glslang::TWorkItem(name);
423 Worklist.add(Work[argc]);
424 }
John Kessenich38f3b892013-09-06 19:52:57 +0000425 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000426 }
427
John Kessenich68d78fd2015-07-12 19:28:10 -0600428 // Make sure that -E is not specified alongside linking (which includes SPV generation)
429 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
430 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000431
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500432 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600433 if (binaryFileName && (Options & EOptionSpv) == 0)
434 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600435
436 if ((Options & EOptionFlattenUniformArrays) != 0 &&
437 (Options & EOptionReadHlsl) == 0)
438 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000439}
440
John Kessenich68d78fd2015-07-12 19:28:10 -0600441//
442// Translate the meaningful subset of command-line options to parser-behavior options.
443//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000444void SetMessageOptions(EShMessages& messages)
445{
446 if (Options & EOptionRelaxedErrors)
447 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
448 if (Options & EOptionIntermediate)
449 messages = (EShMessages)(messages | EShMsgAST);
450 if (Options & EOptionSuppressWarnings)
451 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600452 if (Options & EOptionSpv)
453 messages = (EShMessages)(messages | EShMsgSpvRules);
454 if (Options & EOptionVulkanRules)
455 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400456 if (Options & EOptionOutputPreprocessed)
457 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700458 if (Options & EOptionReadHlsl)
459 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600460 if (Options & EOptionCascadingErrors)
461 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000462}
463
John Kessenich68d78fd2015-07-12 19:28:10 -0600464//
John Kessenich69f4b512013-09-04 21:19:27 +0000465// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000466//
467// Return 0 for failure, 1 for success.
468//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800469unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000470{
John Kessenich38f3b892013-09-06 19:52:57 +0000471 glslang::TWorkItem* workItem;
472 while (Worklist.remove(workItem)) {
473 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000474 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000475 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000476
John Kessenichb0a7eb52013-11-07 17:44:20 +0000477 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000478
John Kessenich94a81fb2013-08-31 02:41:30 +0000479 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000480 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000481
482 ShDestruct(compiler);
483 }
484
485 return 0;
486}
487
John Kessenich6626cad2015-06-19 05:14:19 +0000488// Outputs the given string, but only if it is non-null and non-empty.
489// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400490void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000491{
492 if (str && str[0]) {
493 puts(str);
494 }
495}
496
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400497// Outputs the given string to stderr, but only if it is non-null and non-empty.
498// This prevents erroneous newlines from appearing.
499void StderrIfNonEmpty(const char* str)
500{
501 if (str && str[0]) {
502 fprintf(stderr, "%s\n", str);
503 }
504}
505
John Kessenichc57b2a92016-01-16 15:30:03 -0700506// Simple bundling of what makes a compilation unit for ease in passing around,
507// and separation of handling file IO versus API (programmatic) compilation.
508struct ShaderCompUnit {
509 EShLanguage stage;
510 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400511 char** text; // memory owned/managed externally
512 const char* fileNameList[1];
513
John Kessenich219b0252016-08-23 17:51:13 -0600514 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400515 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
516 {
517 stage = istage;
518 fileName = ifileName;
519 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600520 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400521 }
522
523 ShaderCompUnit(const ShaderCompUnit &rhs)
524 {
525 stage = rhs.stage;
526 fileName = rhs.fileName;
527 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600528 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400529 }
530
John Kessenichc57b2a92016-01-16 15:30:03 -0700531};
532
John Kessenich69f4b512013-09-04 21:19:27 +0000533//
John Kessenichc57b2a92016-01-16 15:30:03 -0700534// For linking mode: Will independently parse each compilation unit, but then put them
535// in the same program and link them together, making at most one linked module per
536// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000537//
538// Uses the new C++ interface instead of the old handle-based interface.
539//
John Kessenichc57b2a92016-01-16 15:30:03 -0700540
541void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000542{
543 // keep track of what to free
544 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700545
John Kessenich69f4b512013-09-04 21:19:27 +0000546 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000547 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000548
John Kessenich69f4b512013-09-04 21:19:27 +0000549 //
550 // Per-shader processing...
551 //
552
John Kessenich5b0f13a2013-11-01 03:08:40 +0000553 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100554 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
555 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700556 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400557 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700558 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
559 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600560 if (sourceEntryPointName)
561 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600562
563 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
564 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600565 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600566 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600567 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600568 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600569
570 if (Options & EOptionAutoMapBindings)
571 shader->setAutoMapBindings(true);
572
John Kessenich69f4b512013-09-04 21:19:27 +0000573 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600574
John Kessenichc555ddd2015-06-17 02:38:44 +0000575 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000576
John Kessenichc555ddd2015-06-17 02:38:44 +0000577 if (Options & EOptionOutputPreprocessed) {
578 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500579 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400580 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500581 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400582 PutsIfNonEmpty(str.c_str());
583 } else {
584 CompileFailed = true;
585 }
586 StderrIfNonEmpty(shader->getInfoLog());
587 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000588 continue;
589 }
590 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000591 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000592
John Kessenich69f4b512013-09-04 21:19:27 +0000593 program.addShader(shader);
594
John Kessenichc57b2a92016-01-16 15:30:03 -0700595 if (! (Options & EOptionSuppressInfolog) &&
596 ! (Options & EOptionMemoryLeakMode)) {
597 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400598 PutsIfNonEmpty(shader->getInfoLog());
599 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000600 }
John Kessenich69f4b512013-09-04 21:19:27 +0000601 }
602
603 //
604 // Program-level processing...
605 //
606
John Kessenich4d65ee32016-03-12 18:17:47 -0700607 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600608 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000609 LinkFailed = true;
610
steve-lunarg9ae34742016-10-05 13:40:13 -0600611 // Map IO
612 if (Options & EOptionSpv) {
613 if (!program.mapIO())
614 LinkFailed = true;
615 }
616
John Kessenich4d65ee32016-03-12 18:17:47 -0700617 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700618 if (! (Options & EOptionSuppressInfolog) &&
619 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400620 PutsIfNonEmpty(program.getInfoLog());
621 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000622 }
623
John Kessenich4d65ee32016-03-12 18:17:47 -0700624 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000625 if (Options & EOptionDumpReflection) {
626 program.buildReflection();
627 program.dumpReflection();
628 }
629
John Kessenich4d65ee32016-03-12 18:17:47 -0700630 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000631 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000632 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600633 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000634 else {
635 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000636 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000637 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400638 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400639 spv::SpvBuildLogger logger;
640 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700641
642 // Dump the spv to a file or stdout, etc., but only if not doing
643 // memory/perf testing, as it's not internal to programmatic use.
644 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500645 printf("%s", logger.getAllMessages().c_str());
646 if (Options & EOptionOutputHexadecimal) {
647 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
648 } else {
649 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
650 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700651 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700652 spv::Disassemble(std::cout, spirv);
653 }
John Kessenichacba7722015-03-04 03:48:38 +0000654 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000655 }
John Kessenich92f90382014-07-28 04:21:04 +0000656 }
657 }
658 }
659
John Kessenich5b0f13a2013-11-01 03:08:40 +0000660 // Free everything up, program has to go before the shaders
661 // because it might have merged stuff from the shaders, and
662 // the stuff from the shaders has to have its destructors called
663 // before the pools holding the memory in the shaders is freed.
664 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000665 while (shaders.size() > 0) {
666 delete shaders.back();
667 shaders.pop_back();
668 }
John Kessenich69f4b512013-09-04 21:19:27 +0000669}
670
John Kessenichc57b2a92016-01-16 15:30:03 -0700671//
672// Do file IO part of compile and link, handing off the pure
673// API/programmatic mode to CompileAndLinkShaderUnits(), which can
674// be put in a loop for testing memory footprint and performance.
675//
676// This is just for linking mode: meaning all the shaders will be put into the
677// the same program linked together.
678//
679// This means there are a limited number of work items (not multi-threading mode)
680// and that the point is testing at the linking level. Hence, to enable
681// performance and memory testing, the actual compile/link can be put in
682// a loop, independent of processing the work items and file IO.
683//
684void CompileAndLinkShaderFiles()
685{
686 std::vector<ShaderCompUnit> compUnits;
687
688 // Transfer all the work items from to a simple list of
689 // of compilation units. (We don't care about the thread
690 // work-item distribution properties in this path, which
691 // is okay due to the limited number of shaders, know since
692 // they are all getting linked together.)
693 glslang::TWorkItem* workItem;
694 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200695 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700696 FindLanguage(workItem->name),
697 workItem->name,
698 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200699 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700700
701 if (! compUnit.text) {
702 usage();
703 return;
704 }
705
706 compUnits.push_back(compUnit);
707 }
708
709 // Actual call to programmatic processing of compile and link,
710 // in a loop for testing memory and performance. This part contains
711 // all the perf/memory that a programmatic consumer will care about.
712 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
713 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
714 CompileAndLinkShaderUnits(compUnits);
715
716 if (Options & EOptionMemoryLeakMode)
717 glslang::OS_DumpMemoryCounters();
718 }
719
rdb32084e82016-02-23 22:17:38 +0100720 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
721 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700722}
723
John Kessenicha0af4732012-12-12 21:15:54 +0000724int C_DECL main(int argc, char* argv[])
725{
John Kessenich68d78fd2015-07-12 19:28:10 -0600726 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000727
John Kessenich05a70632013-09-17 19:26:08 +0000728 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500729 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000730 if (Worklist.empty())
731 return ESuccess;
732 }
733
John Kessenich0da9eaa2015-08-01 17:10:02 -0600734 if (Options & EOptionDumpVersions) {
735 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000736 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
737 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600738 std::string spirvVersion;
739 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600740 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600741 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700742 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600743 printf("GL_KHR_vulkan_glsl version %d\n", 100);
744 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000745 if (Worklist.empty())
746 return ESuccess;
747 }
748
John Kessenich05a70632013-09-17 19:26:08 +0000749 if (Worklist.empty()) {
750 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000751 }
752
753 ProcessConfigFile();
754
John Kessenich69f4b512013-09-04 21:19:27 +0000755 //
756 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000757 // 1) linking all arguments together, single-threaded, new C++ interface
758 // 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 +0000759 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000760 if (Options & EOptionLinkProgram ||
761 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000762 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700763 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000764 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500765 for (int w = 0; w < NumWorkItems; ++w) {
766 if (Work[w]) {
767 delete Work[w];
768 }
769 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000770 } else {
771 ShInitialize();
772
John Kessenich38f3b892013-09-06 19:52:57 +0000773 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000774
John Kessenich38f3b892013-09-06 19:52:57 +0000775 if (Options & EOptionMultiThreaded) {
776 const int NumThreads = 16;
777 void* threads[NumThreads];
778 for (int t = 0; t < NumThreads; ++t) {
779 threads[t] = glslang::OS_CreateThread(&CompileShaders);
780 if (! threads[t]) {
781 printf("Failed to create thread\n");
782 return EFailThreadCreate;
783 }
John Kessenicha0af4732012-12-12 21:15:54 +0000784 }
John Kessenich38f3b892013-09-06 19:52:57 +0000785 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000786 } else
787 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000788
789 // Print out all the resulting infologs
790 for (int w = 0; w < NumWorkItems; ++w) {
791 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600792 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400793 PutsIfNonEmpty(Work[w]->name.c_str());
794 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000795 delete Work[w];
796 }
797 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000798
799 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000800 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000801
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500802 delete[] Work;
803
John Kessenichc999ba22013-11-07 23:33:24 +0000804 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000805 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000806 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000807 return EFailLink;
808
809 return 0;
810}
811
812//
813// Deduce the language from the filename. Files must end in one of the
814// following extensions:
815//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000816// .vert = vertex
817// .tesc = tessellation control
818// .tese = tessellation evaluation
819// .geom = geometry
820// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000821// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000822//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600823EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000824{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600825 size_t ext = 0;
826
827 // Search for a suffix on a filename: e.g, "myfile.frag". If given
828 // the suffix directly, we skip looking the '.'
829 if (parseSuffix) {
830 ext = name.rfind('.');
831 if (ext == std::string::npos) {
832 usage();
833 return EShLangVertex;
834 }
835 ++ext;
John Kessenicha0af4732012-12-12 21:15:54 +0000836 }
837
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600838 std::string suffix = name.substr(ext, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400839 if (shaderStageName)
840 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400841
John Kessenich2b07c7e2013-07-31 18:44:13 +0000842 if (suffix == "vert")
843 return EShLangVertex;
844 else if (suffix == "tesc")
845 return EShLangTessControl;
846 else if (suffix == "tese")
847 return EShLangTessEvaluation;
848 else if (suffix == "geom")
849 return EShLangGeometry;
850 else if (suffix == "frag")
851 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000852 else if (suffix == "comp")
853 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000854
855 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000856 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000857}
858
John Kessenicha0af4732012-12-12 21:15:54 +0000859//
John Kessenich69f4b512013-09-04 21:19:27 +0000860// Read a file's data into a string, and compile it using the old interface ShCompile,
861// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000862//
John Kessenich51cdd902014-02-18 23:37:57 +0000863void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000864{
John Kessenichca3457f2015-05-18 01:59:45 +0000865 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000866 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000867 if (! shaderStrings) {
868 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000869 }
870
John Kessenich41cf6b52013-06-25 18:10:05 +0000871 int* lengths = new int[NumShaderStrings];
872
873 // move to length-based strings, rather than null-terminated strings
874 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000875 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000876
John Kessenichc999ba22013-11-07 23:33:24 +0000877 if (! shaderStrings) {
878 CompileFailed = true;
879 return;
880 }
John Kessenicha0af4732012-12-12 21:15:54 +0000881
John Kessenich52ac67e2013-05-05 23:46:22 +0000882 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000883 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000884
John Kessenich94a81fb2013-08-31 02:41:30 +0000885 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
886 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000887 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000888 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000889 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
890 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
891 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000892 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000893 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000894 }
John Kessenicha0af4732012-12-12 21:15:54 +0000895
John Kessenich94a81fb2013-08-31 02:41:30 +0000896 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000897 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000898 }
John Kessenicha0af4732012-12-12 21:15:54 +0000899
John Kessenich41cf6b52013-06-25 18:10:05 +0000900 delete [] lengths;
901 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000902
John Kessenichc999ba22013-11-07 23:33:24 +0000903 if (ret == 0)
904 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000905}
906
John Kessenicha0af4732012-12-12 21:15:54 +0000907//
908// print usage to stdout
909//
910void usage()
911{
John Kessenich319de232013-12-04 04:43:40 +0000912 printf("Usage: glslangValidator [option]... [file]...\n"
913 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000914 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600915 " .conf to provide an optional config file that replaces the default configuration\n"
916 " (see -c option below for generating a template)\n"
917 " .vert for a vertex shader\n"
918 " .tesc for a tessellation control shader\n"
919 " .tese for a tessellation evaluation shader\n"
920 " .geom for a geometry shader\n"
921 " .frag for a fragment shader\n"
922 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000923 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000924 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000925 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000926 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600927 "Each option must be specified separately.\n"
928 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
929 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600930 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
931 " default file name is <stage>.spv (-o overrides this)\n"
932 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400933 " -E print pre-processed GLSL; cannot be used with -l;\n"
934 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400935 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
Dan Baker895275e2016-08-11 14:55:49 -0400936 " valid choices are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600937 " -c configuration dump;\n"
938 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600939 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600940 " -d default to desktop (#version 110) when there is no shader #version\n"
941 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700942 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700943 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600944 " -h print this usage message\n"
945 " -i intermediate tree (glslang AST) is printed out\n"
946 " -l link all input files together to form a single module\n"
947 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500948 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600949 " -q dump reflection query database\n"
950 " -r relaxed semantic error-checking mode\n"
951 " -s silent mode\n"
952 " -t multi-threaded mode\n"
953 " -v print version strings\n"
954 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500955 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600956 "\n"
957 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
958 " --ssb [stage] num synonym for --shift-sampler-binding\n"
959 "\n"
960 " --shift-texture-binding [stage] num set base binding number for textures\n"
961 " --stb [stage] num synonym for --shift-texture-binding\n"
962 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600963 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
964 " --sib [stage] num synonym for --shift-image-binding\n"
965 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600966 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
967 " --sub [stage] num synonym for --shift-UBO-binding\n"
968 "\n"
969 " --auto-map-bindings automatically bind uniform variables without\n"
970 " explicit bindings.\n"
971 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600972 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -0600973 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600974 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -0600975 "\n"
976 " --no-storage-format use Unknown image format\n"
977 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -0600978 "\n"
979 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
980 " --sep synonym for --source-entrypoint\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000981 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600982
983 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000984}
985
John Kessenich3ce4e592014-10-06 19:57:34 +0000986#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000987
988#include <errno.h>
989
990int fopen_s(
991 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000992 const char* filename,
993 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000994)
995{
996 if (!pFile || !filename || !mode) {
997 return EINVAL;
998 }
999
1000 FILE* f = fopen(filename, mode);
1001 if (! f) {
1002 if (errno != 0) {
1003 return errno;
1004 } else {
1005 return ENOENT;
1006 }
1007 }
1008 *pFile = f;
1009
1010 return 0;
1011}
1012
1013#endif
1014
John Kessenicha0af4732012-12-12 21:15:54 +00001015//
1016// Malloc a string of sufficient size and read a string into it.
1017//
John Kessenich51cdd902014-02-18 23:37:57 +00001018char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001019{
John Kessenichb3297152015-07-11 18:01:03 -06001020 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001021 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001022
John Kessenicha0af4732012-12-12 21:15:54 +00001023 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001024 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1025 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001026
John Kessenich68d78fd2015-07-12 19:28:10 -06001027 if (errorCode || in == nullptr)
1028 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001029
1030 while (fgetc(in) != EOF)
1031 count++;
1032
John Kessenichd6c72a42014-08-18 19:42:35 +00001033 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001034
John Kessenichb3297152015-07-11 18:01:03 -06001035 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001036 if (! fdata)
1037 Error("can't allocate memory");
1038
John Kessenichb3297152015-07-11 18:01:03 -06001039 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001040 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001041 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001042 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001043
John Kessenicha0af4732012-12-12 21:15:54 +00001044 fdata[count] = '\0';
1045 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001046
John Kessenichea869fb2013-10-28 18:12:06 +00001047 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001048 // recover from empty file
1049 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001050 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001051 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001052 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001053
John Kessenichb3297152015-07-11 18:01:03 -06001054 return return_data;
1055 } else
1056 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1057
1058 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001059 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001060 int ptr_len = 0;
1061 int i = 0;
1062 while (count > 0) {
1063 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1064 memcpy(return_data[i], fdata + ptr_len, len);
1065 return_data[i][len] = '\0';
1066 count -= len;
1067 ptr_len += len;
1068 if (count < len) {
1069 if (count == 0) {
1070 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001071 break;
1072 }
John Kessenichb3297152015-07-11 18:01:03 -06001073 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +00001074 }
1075 ++i;
1076 }
John Kessenichb3297152015-07-11 18:01:03 -06001077
1078 free(fdata);
1079
John Kessenicha0af4732012-12-12 21:15:54 +00001080 return return_data;
1081}
1082
John Kessenich51cdd902014-02-18 23:37:57 +00001083void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001084{
John Kessenichb3297152015-07-11 18:01:03 -06001085 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001086 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001087
1088 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001089}
1090
John Kessenich54d8cda2013-02-11 22:36:01 +00001091void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001092{
John Kessenichfae38ee2015-06-10 23:23:12 +00001093 if (num >= 0 )
1094 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1095 else
1096 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001097}