blob: 76af8fe3372ec2a22a83449d230d15c091909a45 [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
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030038#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000039#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030040#endif
John Kessenich05a70632013-09-17 19:26:08 +000041
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040042#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000043#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060045#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000046#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000047#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060048#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000049#include "../SPIRV/doc.h"
50#include "../SPIRV/disassemble.h"
John Kessenich66ec80e2016-08-05 14:04:23 -060051#include <cstring>
52#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060053#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060054#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <array>
Juan Lopeza558b262017-04-02 23:04:00 +020056#include <memory>
57#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000058
baldurk876a0e32015-11-16 18:03:28 +010059#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000060
61extern "C" {
62 SH_IMPORT_EXPORT void ShOutputHtml();
63}
64
John Kessenich94a81fb2013-08-31 02:41:30 +000065// Command-line options
66enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070067 EOptionNone = 0,
68 EOptionIntermediate = (1 << 0),
69 EOptionSuppressInfolog = (1 << 1),
70 EOptionMemoryLeakMode = (1 << 2),
71 EOptionRelaxedErrors = (1 << 3),
72 EOptionGiveWarnings = (1 << 4),
73 EOptionLinkProgram = (1 << 5),
74 EOptionMultiThreaded = (1 << 6),
75 EOptionDumpConfig = (1 << 7),
76 EOptionDumpReflection = (1 << 8),
77 EOptionSuppressWarnings = (1 << 9),
78 EOptionDumpVersions = (1 << 10),
79 EOptionSpv = (1 << 11),
80 EOptionHumanReadableSpv = (1 << 12),
81 EOptionVulkanRules = (1 << 13),
82 EOptionDefaultDesktop = (1 << 14),
83 EOptionOutputPreprocessed = (1 << 15),
84 EOptionOutputHexadecimal = (1 << 16),
85 EOptionReadHlsl = (1 << 17),
86 EOptionCascadingErrors = (1 << 18),
87 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060088 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070089 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070090 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060091 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060092 EOptionHlslIoMapping = (1 << 24),
John Kessenich94a81fb2013-08-31 02:41:30 +000093};
94
John Kessenicha0af4732012-12-12 21:15:54 +000095//
John Kessenich68d78fd2015-07-12 19:28:10 -060096// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000097//
98enum TFailCode {
99 ESuccess = 0,
100 EFailUsage,
101 EFailCompile,
102 EFailLink,
103 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000104 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000105 EFailLinkerCreate
106};
107
108//
John Kessenich68d78fd2015-07-12 19:28:10 -0600109// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000110//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600111EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000112void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000113void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000114void FreeFileData(char** data);
115char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000116void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000117
John Kessenichc999ba22013-11-07 23:33:24 +0000118// Globally track if any compile or link failure.
119bool CompileFailed = false;
120bool LinkFailed = false;
121
John Kessenich05a70632013-09-17 19:26:08 +0000122// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600123// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000124int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000125
John Kessenich05a70632013-09-17 19:26:08 +0000126TBuiltInResource Resources;
127std::string ConfigFile;
128
John Kessenicha0af4732012-12-12 21:15:54 +0000129//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600130// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000131//
132void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000133{
John Kessenich05a70632013-09-17 19:26:08 +0000134 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000135 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000136 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000137 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000138 if (configStrings)
139 config = *configStrings;
140 else {
141 printf("Error opening configuration file; will instead use the default configuration\n");
142 usage();
143 }
144 }
145
146 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500147 Resources = glslang::DefaultTBuiltInResource;
148 return;
John Kessenich05a70632013-09-17 19:26:08 +0000149 }
150
Lei Zhang1b141722016-05-19 13:50:49 -0400151 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000152
John Kessenich05a70632013-09-17 19:26:08 +0000153 if (configStrings)
154 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500155 else
156 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000157}
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;
Flavioaea3c892017-02-06 11:46:35 -0800165const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600166
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600167std::array<unsigned int, EShLangCount> baseSamplerBinding;
168std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600169std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600170std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700171std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600172std::array<unsigned int, EShLangCount> baseUavBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600173
John Kessenich68d78fd2015-07-12 19:28:10 -0600174//
175// Create the default name for saving a binary if -o is not provided.
176//
177const char* GetBinaryName(EShLanguage stage)
178{
179 const char* name;
180 if (binaryFileName == nullptr) {
181 switch (stage) {
182 case EShLangVertex: name = "vert.spv"; break;
183 case EShLangTessControl: name = "tesc.spv"; break;
184 case EShLangTessEvaluation: name = "tese.spv"; break;
185 case EShLangGeometry: name = "geom.spv"; break;
186 case EShLangFragment: name = "frag.spv"; break;
187 case EShLangCompute: name = "comp.spv"; break;
188 default: name = "unknown"; break;
189 }
190 } else
191 name = binaryFileName;
192
193 return name;
194}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000195
John Kessenich05a70632013-09-17 19:26:08 +0000196//
197// *.conf => this is a config file that can set limits/resources
198//
199bool SetConfigFile(const std::string& name)
200{
201 if (name.size() < 5)
202 return false;
203
John Kessenich4c706852013-10-11 16:28:43 +0000204 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000205 ConfigFile = name;
206 return true;
207 }
208
209 return false;
210}
211
John Kessenich68d78fd2015-07-12 19:28:10 -0600212//
213// Give error and exit with failure code.
214//
215void Error(const char* message)
216{
217 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
218 exit(EFailUsage);
219}
220
221//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600222// Process an optional binding base of the form:
223// --argname [stage] base
224// Where stage is one of the forms accepted by FindLanguage, and base is an integer
225//
226void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
227{
228 if (argc < 2)
229 usage();
230
231 if (!isdigit(argv[1][0])) {
232 if (argc < 3) // this form needs one more argument
233 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700234
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600235 // Parse form: --argname stage base
236 const EShLanguage lang = FindLanguage(argv[1], false);
237 base[lang] = atoi(argv[2]);
238 argc-= 2;
239 argv+= 2;
240 } else {
241 // Parse form: --argname base
242 for (int lang=0; lang<EShLangCount; ++lang)
243 base[lang] = atoi(argv[1]);
244
245 argc--;
246 argv++;
247 }
248}
249
250//
John Kessenich68d78fd2015-07-12 19:28:10 -0600251// Do all command-line argument parsing. This includes building up the work-items
252// to be processed later, and saving all the command-line options.
253//
254// Does not return (it exits) if command-line is fatally flawed.
255//
Juan Lopeza558b262017-04-02 23:04:00 +0200256void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000257{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600258 baseSamplerBinding.fill(0);
259 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600260 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600261 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700262 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600263 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600264
John Kessenich38f3b892013-09-06 19:52:57 +0000265 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200266 workItems.reserve(argc);
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" ||
steve-lunargbe283552017-04-18 12:18:01 -0600293 lowerword == "shift-cbuffer-bindings" ||
294 lowerword == "shift-cbuffer-binding" ||
295 lowerword == "sub" ||
296 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600297 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700298 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
299 lowerword == "shift-ssbo-binding" ||
300 lowerword == "sbb") {
301 ProcessBindingBase(argc, argv, baseSsboBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600302 } else if (lowerword == "shift-uav-bindings" || // synonyms
303 lowerword == "shift-uav-binding" ||
304 lowerword == "suavb") {
305 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600306 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700307 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600308 lowerword == "amb") {
309 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600310 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
311 lowerword == "flatten-uniform-array" ||
312 lowerword == "fua") {
313 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600314 } else if (lowerword == "no-storage-format" || // synonyms
315 lowerword == "nsf") {
316 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800317 } else if (lowerword == "variable-name" || // synonyms
318 lowerword == "vn") {
319 Options |= EOptionOutputHexadecimal;
320 variableName = argv[1];
321 if (argc > 0) {
322 argc--;
323 argv++;
324 } else
325 Error("no <C-variable-name> provided for --variable-name");
326 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600327 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600328 lowerword == "sep") {
329 sourceEntryPointName = argv[1];
330 if (argc > 0) {
331 argc--;
332 argv++;
333 } else
334 Error("no <entry-point> provided for --source-entrypoint");
335 break;
John Kessenich906cc212016-12-09 19:22:20 -0700336 } else if (lowerword == "keep-uncalled" || // synonyms
337 lowerword == "ku") {
338 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600339 } else if (lowerword == "hlsl-offsets") {
340 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600341 } else if (lowerword == "hlsl-iomap" ||
342 lowerword == "hlsl-iomapper" ||
343 lowerword == "hlsl-iomapping") {
344 Options |= EOptionHlslIoMapping;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600345 } else {
346 usage();
347 }
348 }
349 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000350 case 'H':
351 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600352 if ((Options & EOptionSpv) == 0) {
353 // default to Vulkan
354 Options |= EOptionSpv;
355 Options |= EOptionVulkanRules;
356 Options |= EOptionLinkProgram;
357 }
358 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000359 case 'V':
360 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600361 Options |= EOptionVulkanRules;
362 Options |= EOptionLinkProgram;
363 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400364 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400365 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400366 if (argc > 0) {
367 argc--;
368 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600369 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400370 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400371 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600372 case 'G':
373 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000374 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600375 // undo a -H default to Vulkan
376 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000377 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000378 case 'E':
379 Options |= EOptionOutputPreprocessed;
380 break;
John Kessenich05a70632013-09-17 19:26:08 +0000381 case 'c':
382 Options |= EOptionDumpConfig;
383 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600384 case 'C':
385 Options |= EOptionCascadingErrors;
386 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000387 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000388 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000389 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700390 case 'D':
391 Options |= EOptionReadHlsl;
392 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700393 case 'e':
394 // HLSL todo: entry point handle needs much more sophistication.
395 // This is okay for one compilation unit with one entry point.
396 entryPointName = argv[1];
397 if (argc > 0) {
398 argc--;
399 argv++;
400 } else
401 Error("no <entry-point> provided for -e");
402 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600403 case 'h':
404 usage();
405 break;
John Kessenich05a70632013-09-17 19:26:08 +0000406 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000407 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000408 break;
409 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000410 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000411 break;
412 case 'm':
413 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000414 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600415 case 'o':
416 binaryFileName = argv[1];
417 if (argc > 0) {
418 argc--;
419 argv++;
420 } else
421 Error("no <file> provided for -o");
422 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000423 case 'q':
424 Options |= EOptionDumpReflection;
425 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000426 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000427 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000428 break;
429 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000430 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000431 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000432 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200433 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000434 break;
John Kessenich319de232013-12-04 04:43:40 +0000435 case 'v':
436 Options |= EOptionDumpVersions;
437 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000438 case 'w':
439 Options |= EOptionSuppressWarnings;
440 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500441 case 'x':
442 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500443 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000444 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600445 usage();
446 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000447 }
John Kessenich38f3b892013-09-06 19:52:57 +0000448 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000449 std::string name(argv[0]);
450 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200451 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000452 }
John Kessenich38f3b892013-09-06 19:52:57 +0000453 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000454 }
455
John Kessenich68d78fd2015-07-12 19:28:10 -0600456 // Make sure that -E is not specified alongside linking (which includes SPV generation)
457 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
458 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000459
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500460 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600461 if (binaryFileName && (Options & EOptionSpv) == 0)
462 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600463
464 if ((Options & EOptionFlattenUniformArrays) != 0 &&
465 (Options & EOptionReadHlsl) == 0)
466 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000467}
468
John Kessenich68d78fd2015-07-12 19:28:10 -0600469//
470// Translate the meaningful subset of command-line options to parser-behavior options.
471//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000472void SetMessageOptions(EShMessages& messages)
473{
474 if (Options & EOptionRelaxedErrors)
475 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
476 if (Options & EOptionIntermediate)
477 messages = (EShMessages)(messages | EShMsgAST);
478 if (Options & EOptionSuppressWarnings)
479 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600480 if (Options & EOptionSpv)
481 messages = (EShMessages)(messages | EShMsgSpvRules);
482 if (Options & EOptionVulkanRules)
483 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400484 if (Options & EOptionOutputPreprocessed)
485 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700486 if (Options & EOptionReadHlsl)
487 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600488 if (Options & EOptionCascadingErrors)
489 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700490 if (Options & EOptionKeepUncalled)
491 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600492 if (Options & EOptionHlslOffsets)
493 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000494}
495
John Kessenich68d78fd2015-07-12 19:28:10 -0600496//
John Kessenich69f4b512013-09-04 21:19:27 +0000497// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000498//
Juan Lopeza558b262017-04-02 23:04:00 +0200499void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000500{
John Kessenich38f3b892013-09-06 19:52:57 +0000501 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200502 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000503 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000504 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200505 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000506
John Kessenichb0a7eb52013-11-07 17:44:20 +0000507 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000508
John Kessenich94a81fb2013-08-31 02:41:30 +0000509 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000510 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000511
512 ShDestruct(compiler);
513 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000514}
515
John Kessenich6626cad2015-06-19 05:14:19 +0000516// Outputs the given string, but only if it is non-null and non-empty.
517// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400518void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000519{
520 if (str && str[0]) {
521 puts(str);
522 }
523}
524
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400525// Outputs the given string to stderr, but only if it is non-null and non-empty.
526// This prevents erroneous newlines from appearing.
527void StderrIfNonEmpty(const char* str)
528{
529 if (str && str[0]) {
530 fprintf(stderr, "%s\n", str);
531 }
532}
533
John Kessenichc57b2a92016-01-16 15:30:03 -0700534// Simple bundling of what makes a compilation unit for ease in passing around,
535// and separation of handling file IO versus API (programmatic) compilation.
536struct ShaderCompUnit {
537 EShLanguage stage;
538 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400539 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600540 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400541
John Kessenich219b0252016-08-23 17:51:13 -0600542 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400543 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
544 {
545 stage = istage;
546 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600547 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600548 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400549 }
550
551 ShaderCompUnit(const ShaderCompUnit &rhs)
552 {
553 stage = rhs.stage;
554 fileName = rhs.fileName;
555 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600556 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400557 }
558
John Kessenichc57b2a92016-01-16 15:30:03 -0700559};
560
John Kessenich69f4b512013-09-04 21:19:27 +0000561//
John Kessenichc57b2a92016-01-16 15:30:03 -0700562// For linking mode: Will independently parse each compilation unit, but then put them
563// in the same program and link them together, making at most one linked module per
564// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000565//
566// Uses the new C++ interface instead of the old handle-based interface.
567//
John Kessenichc57b2a92016-01-16 15:30:03 -0700568
569void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000570{
571 // keep track of what to free
572 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700573
John Kessenich69f4b512013-09-04 21:19:27 +0000574 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000575 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000576
John Kessenich69f4b512013-09-04 21:19:27 +0000577 //
578 // Per-shader processing...
579 //
580
John Kessenich5b0f13a2013-11-01 03:08:40 +0000581 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100582 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
583 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700584 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400585 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700586 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
587 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600588 if (sourceEntryPointName)
589 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600590
591 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
592 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600593 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600594 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700595 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600596 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600597 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600598 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600599
steve-lunargbe283552017-04-18 12:18:01 -0600600 if (Options & EOptionHlslIoMapping)
601 shader->setHlslIoMapping(true);
602
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600603 if (Options & EOptionAutoMapBindings)
604 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700605
John Kessenich69f4b512013-09-04 21:19:27 +0000606 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600607
John Kessenichc555ddd2015-06-17 02:38:44 +0000608 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000609
John Kessenichc555ddd2015-06-17 02:38:44 +0000610 if (Options & EOptionOutputPreprocessed) {
611 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700612 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400613 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500614 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400615 PutsIfNonEmpty(str.c_str());
616 } else {
617 CompileFailed = true;
618 }
619 StderrIfNonEmpty(shader->getInfoLog());
620 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000621 continue;
622 }
623 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000624 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000625
John Kessenich69f4b512013-09-04 21:19:27 +0000626 program.addShader(shader);
627
John Kessenichc57b2a92016-01-16 15:30:03 -0700628 if (! (Options & EOptionSuppressInfolog) &&
629 ! (Options & EOptionMemoryLeakMode)) {
630 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400631 PutsIfNonEmpty(shader->getInfoLog());
632 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000633 }
John Kessenich69f4b512013-09-04 21:19:27 +0000634 }
635
636 //
637 // Program-level processing...
638 //
639
John Kessenich4d65ee32016-03-12 18:17:47 -0700640 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600641 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000642 LinkFailed = true;
643
steve-lunarg9ae34742016-10-05 13:40:13 -0600644 // Map IO
645 if (Options & EOptionSpv) {
646 if (!program.mapIO())
647 LinkFailed = true;
648 }
John Kessenichecba76f2017-01-06 00:34:48 -0700649
John Kessenich4d65ee32016-03-12 18:17:47 -0700650 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700651 if (! (Options & EOptionSuppressInfolog) &&
652 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400653 PutsIfNonEmpty(program.getInfoLog());
654 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000655 }
656
John Kessenich4d65ee32016-03-12 18:17:47 -0700657 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000658 if (Options & EOptionDumpReflection) {
659 program.buildReflection();
660 program.dumpReflection();
661 }
662
John Kessenich4d65ee32016-03-12 18:17:47 -0700663 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000664 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000665 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600666 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000667 else {
668 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000669 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000670 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400671 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400672 spv::SpvBuildLogger logger;
673 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700674
675 // Dump the spv to a file or stdout, etc., but only if not doing
676 // memory/perf testing, as it's not internal to programmatic use.
677 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500678 printf("%s", logger.getAllMessages().c_str());
679 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700680 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500681 } else {
682 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
683 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700684 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700685 spv::Disassemble(std::cout, spirv);
686 }
John Kessenichacba7722015-03-04 03:48:38 +0000687 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000688 }
John Kessenich92f90382014-07-28 04:21:04 +0000689 }
690 }
691 }
692
John Kessenich5b0f13a2013-11-01 03:08:40 +0000693 // Free everything up, program has to go before the shaders
694 // because it might have merged stuff from the shaders, and
695 // the stuff from the shaders has to have its destructors called
696 // before the pools holding the memory in the shaders is freed.
697 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000698 while (shaders.size() > 0) {
699 delete shaders.back();
700 shaders.pop_back();
701 }
John Kessenich69f4b512013-09-04 21:19:27 +0000702}
703
John Kessenichc57b2a92016-01-16 15:30:03 -0700704//
705// Do file IO part of compile and link, handing off the pure
706// API/programmatic mode to CompileAndLinkShaderUnits(), which can
707// be put in a loop for testing memory footprint and performance.
708//
709// This is just for linking mode: meaning all the shaders will be put into the
710// the same program linked together.
711//
712// This means there are a limited number of work items (not multi-threading mode)
713// and that the point is testing at the linking level. Hence, to enable
714// performance and memory testing, the actual compile/link can be put in
715// a loop, independent of processing the work items and file IO.
716//
Juan Lopeza558b262017-04-02 23:04:00 +0200717void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700718{
719 std::vector<ShaderCompUnit> compUnits;
720
721 // Transfer all the work items from to a simple list of
722 // of compilation units. (We don't care about the thread
723 // work-item distribution properties in this path, which
724 // is okay due to the limited number of shaders, know since
725 // they are all getting linked together.)
726 glslang::TWorkItem* workItem;
727 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200728 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700729 FindLanguage(workItem->name),
730 workItem->name,
731 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200732 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700733
734 if (! compUnit.text) {
735 usage();
736 return;
737 }
738
739 compUnits.push_back(compUnit);
740 }
741
742 // Actual call to programmatic processing of compile and link,
743 // in a loop for testing memory and performance. This part contains
744 // all the perf/memory that a programmatic consumer will care about.
745 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
746 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
747 CompileAndLinkShaderUnits(compUnits);
748
749 if (Options & EOptionMemoryLeakMode)
750 glslang::OS_DumpMemoryCounters();
751 }
752
rdb32084e82016-02-23 22:17:38 +0100753 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
754 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700755}
756
John Kessenicha0af4732012-12-12 21:15:54 +0000757int C_DECL main(int argc, char* argv[])
758{
Juan Lopeza558b262017-04-02 23:04:00 +0200759 // array of unique places to leave the shader names and infologs for the asynchronous compiles
760 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
761 ProcessArguments(workItems, argc, argv);
762
763 glslang::TWorklist workList;
764 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
765 assert(item);
766 workList.add(item.get());
767 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000768
John Kessenich05a70632013-09-17 19:26:08 +0000769 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500770 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200771 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000772 return ESuccess;
773 }
774
John Kessenich0da9eaa2015-08-01 17:10:02 -0600775 if (Options & EOptionDumpVersions) {
776 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000777 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
778 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600779 std::string spirvVersion;
780 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600781 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600782 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700783 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600784 printf("GL_KHR_vulkan_glsl version %d\n", 100);
785 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200786 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000787 return ESuccess;
788 }
789
Juan Lopeza558b262017-04-02 23:04:00 +0200790 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000791 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000792 }
793
794 ProcessConfigFile();
795
John Kessenich69f4b512013-09-04 21:19:27 +0000796 //
797 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000798 // 1) linking all arguments together, single-threaded, new C++ interface
799 // 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 +0000800 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000801 if (Options & EOptionLinkProgram ||
802 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000803 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200804 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000805 glslang::FinalizeProcess();
806 } else {
807 ShInitialize();
808
Juan Lopeza558b262017-04-02 23:04:00 +0200809 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000810
Juan Lopeza558b262017-04-02 23:04:00 +0200811 if (Options & EOptionMultiThreaded)
812 {
813 std::array<std::thread, 16> threads;
814 for (unsigned int t = 0; t < threads.size(); ++t)
815 {
816 threads[t] = std::thread(CompileShaders, std::ref(workList));
817 if (threads[t].get_id() == std::thread::id())
818 {
John Kessenich38f3b892013-09-06 19:52:57 +0000819 printf("Failed to create thread\n");
820 return EFailThreadCreate;
821 }
John Kessenicha0af4732012-12-12 21:15:54 +0000822 }
Juan Lopeza558b262017-04-02 23:04:00 +0200823
824 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000825 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200826 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000827
828 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200829 for (size_t w = 0; w < workItems.size(); ++w) {
830 if (workItems[w]) {
831 if (printShaderNames || workItems[w]->results.size() > 0)
832 PutsIfNonEmpty(workItems[w]->name.c_str());
833 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000834 }
835 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000836
837 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000838 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000839
John Kessenichc999ba22013-11-07 23:33:24 +0000840 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000841 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000842 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000843 return EFailLink;
844
845 return 0;
846}
847
848//
849// Deduce the language from the filename. Files must end in one of the
850// following extensions:
851//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000852// .vert = vertex
853// .tesc = tessellation control
854// .tese = tessellation evaluation
855// .geom = geometry
856// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000857// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000858//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600859EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000860{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600861 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600862 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600863
Dan Bakerc6ede892016-08-11 14:06:06 -0400864 if (shaderStageName)
865 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600866 else {
867 // Search for a suffix on a filename: e.g, "myfile.frag". If given
868 // the suffix directly, we skip looking for the '.'
869 if (parseSuffix) {
870 ext = name.rfind('.');
871 if (ext == std::string::npos) {
872 usage();
873 return EShLangVertex;
874 }
875 ++ext;
876 }
877 suffix = name.substr(ext, std::string::npos);
878 }
dankbaker45d49bc2016-08-08 21:43:07 -0400879
John Kessenich2b07c7e2013-07-31 18:44:13 +0000880 if (suffix == "vert")
881 return EShLangVertex;
882 else if (suffix == "tesc")
883 return EShLangTessControl;
884 else if (suffix == "tese")
885 return EShLangTessEvaluation;
886 else if (suffix == "geom")
887 return EShLangGeometry;
888 else if (suffix == "frag")
889 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000890 else if (suffix == "comp")
891 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000892
893 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000894 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000895}
896
John Kessenicha0af4732012-12-12 21:15:54 +0000897//
John Kessenichecba76f2017-01-06 00:34:48 -0700898// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000899// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000900//
John Kessenich51cdd902014-02-18 23:37:57 +0000901void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000902{
John Kessenichca3457f2015-05-18 01:59:45 +0000903 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000904 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000905 if (! shaderStrings) {
906 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000907 }
908
John Kessenich41cf6b52013-06-25 18:10:05 +0000909 int* lengths = new int[NumShaderStrings];
910
911 // move to length-based strings, rather than null-terminated strings
912 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000913 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000914
John Kessenichc999ba22013-11-07 23:33:24 +0000915 if (! shaderStrings) {
916 CompileFailed = true;
917 return;
918 }
John Kessenicha0af4732012-12-12 21:15:54 +0000919
John Kessenich52ac67e2013-05-05 23:46:22 +0000920 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000921 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700922
John Kessenich94a81fb2013-08-31 02:41:30 +0000923 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
924 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700925 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000926 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700927 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700928 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000929 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700930 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
931 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000932 }
John Kessenicha0af4732012-12-12 21:15:54 +0000933
John Kessenich94a81fb2013-08-31 02:41:30 +0000934 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000935 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000936 }
John Kessenicha0af4732012-12-12 21:15:54 +0000937
John Kessenich41cf6b52013-06-25 18:10:05 +0000938 delete [] lengths;
939 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000940
John Kessenichc999ba22013-11-07 23:33:24 +0000941 if (ret == 0)
942 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000943}
944
John Kessenicha0af4732012-12-12 21:15:54 +0000945//
946// print usage to stdout
947//
948void usage()
949{
John Kessenich319de232013-12-04 04:43:40 +0000950 printf("Usage: glslangValidator [option]... [file]...\n"
951 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000952 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600953 " .conf to provide an optional config file that replaces the default configuration\n"
954 " (see -c option below for generating a template)\n"
955 " .vert for a vertex shader\n"
956 " .tesc for a tessellation control shader\n"
957 " .tese for a tessellation evaluation shader\n"
958 " .geom for a geometry shader\n"
959 " .frag for a fragment shader\n"
960 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000961 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000962 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000963 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000964 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600965 "Each option must be specified separately.\n"
966 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
967 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600968 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
969 " default file name is <stage>.spv (-o overrides this)\n"
970 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400971 " -E print pre-processed GLSL; cannot be used with -l;\n"
972 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -0600973 " -S <stage> uses specified stage rather than parsing the file extension\n"
974 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600975 " -c configuration dump;\n"
976 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600977 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600978 " -d default to desktop (#version 110) when there is no shader #version\n"
979 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700980 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700981 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600982 " -h print this usage message\n"
983 " -i intermediate tree (glslang AST) is printed out\n"
984 " -l link all input files together to form a single module\n"
985 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500986 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600987 " -q dump reflection query database\n"
988 " -r relaxed semantic error-checking mode\n"
989 " -s silent mode\n"
990 " -t multi-threaded mode\n"
991 " -v print version strings\n"
992 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500993 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600994 "\n"
995 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
996 " --ssb [stage] num synonym for --shift-sampler-binding\n"
997 "\n"
998 " --shift-texture-binding [stage] num set base binding number for textures\n"
999 " --stb [stage] num synonym for --shift-texture-binding\n"
1000 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -06001001 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1002 " --sib [stage] num synonym for --shift-image-binding\n"
1003 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001004 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001005 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001006 " --sub [stage] num synonym for --shift-UBO-binding\n"
1007 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001008 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1009 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1010 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001011 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1012 " --suavb [stage] num synonym for --shift-uav-binding\n"
1013 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001014 " --auto-map-bindings automatically bind uniform variables without\n"
1015 " explicit bindings.\n"
1016 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001017 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001018 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001019 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001020 "\n"
1021 " --no-storage-format use Unknown image format\n"
1022 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001023 "\n"
1024 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1025 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001026 "\n"
1027 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1028 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001029 "\n"
1030 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1031 " initialized with the shader binary code.\n"
1032 " --vn <name> synonym for --variable-name <name>\n"
1033 "\n"
1034 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1035 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001036 "\n"
1037 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001038 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001039
1040 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001041}
1042
John Kessenich3ce4e592014-10-06 19:57:34 +00001043#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001044
1045#include <errno.h>
1046
1047int fopen_s(
1048 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001049 const char* filename,
1050 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001051)
1052{
1053 if (!pFile || !filename || !mode) {
1054 return EINVAL;
1055 }
1056
1057 FILE* f = fopen(filename, mode);
1058 if (! f) {
1059 if (errno != 0) {
1060 return errno;
1061 } else {
1062 return ENOENT;
1063 }
1064 }
1065 *pFile = f;
1066
1067 return 0;
1068}
1069
1070#endif
1071
John Kessenicha0af4732012-12-12 21:15:54 +00001072//
1073// Malloc a string of sufficient size and read a string into it.
1074//
John Kessenichecba76f2017-01-06 00:34:48 -07001075char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001076{
John Kessenichb3297152015-07-11 18:01:03 -06001077 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001078 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001079
John Kessenicha0af4732012-12-12 21:15:54 +00001080 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001081 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1082 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001083
John Kessenich68d78fd2015-07-12 19:28:10 -06001084 if (errorCode || in == nullptr)
1085 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001086
John Kessenicha0af4732012-12-12 21:15:54 +00001087 while (fgetc(in) != EOF)
1088 count++;
1089
John Kessenichd6c72a42014-08-18 19:42:35 +00001090 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001091
John Kessenichb3297152015-07-11 18:01:03 -06001092 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001093 if (! fdata)
1094 Error("can't allocate memory");
1095
John Kessenichb3297152015-07-11 18:01:03 -06001096 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001097 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001098 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001099 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001100
John Kessenicha0af4732012-12-12 21:15:54 +00001101 fdata[count] = '\0';
1102 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001103
John Kessenichea869fb2013-10-28 18:12:06 +00001104 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001105 // recover from empty file
1106 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001107 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001108 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001109 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001110
John Kessenichb3297152015-07-11 18:01:03 -06001111 return return_data;
1112 } else
1113 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1114
1115 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001116 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001117 int ptr_len = 0;
1118 int i = 0;
1119 while (count > 0) {
1120 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1121 memcpy(return_data[i], fdata + ptr_len, len);
1122 return_data[i][len] = '\0';
1123 count -= len;
1124 ptr_len += len;
1125 if (count < len) {
1126 if (count == 0) {
1127 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001128 break;
1129 }
John Kessenichb3297152015-07-11 18:01:03 -06001130 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001131 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001132 ++i;
1133 }
John Kessenichb3297152015-07-11 18:01:03 -06001134
1135 free(fdata);
1136
John Kessenicha0af4732012-12-12 21:15:54 +00001137 return return_data;
1138}
1139
John Kessenich51cdd902014-02-18 23:37:57 +00001140void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001141{
John Kessenichb3297152015-07-11 18:01:03 -06001142 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001143 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001144
1145 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001146}
1147
John Kessenich54d8cda2013-02-11 22:36:01 +00001148void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001149{
John Kessenichfae38ee2015-06-10 23:23:12 +00001150 if (num >= 0 )
1151 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1152 else
1153 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001154}