blob: 3faadecfc35df4f3f483f7ff0aaffa21f5253138 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000035//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
38#define _CRT_SECURE_NO_WARNINGS
39
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040040#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000041#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000042#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060043#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000045#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060046#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000047#include "../SPIRV/doc.h"
48#include "../SPIRV/disassemble.h"
John Kessenich66ec80e2016-08-05 14:04:23 -060049#include <cstring>
50#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060051#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060052#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060053#include <array>
Juan Lopeza558b262017-04-02 23:04:00 +020054#include <memory>
55#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000056
baldurk876a0e32015-11-16 18:03:28 +010057#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000058
59extern "C" {
60 SH_IMPORT_EXPORT void ShOutputHtml();
61}
62
John Kessenich94a81fb2013-08-31 02:41:30 +000063// Command-line options
64enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070065 EOptionNone = 0,
66 EOptionIntermediate = (1 << 0),
67 EOptionSuppressInfolog = (1 << 1),
68 EOptionMemoryLeakMode = (1 << 2),
69 EOptionRelaxedErrors = (1 << 3),
70 EOptionGiveWarnings = (1 << 4),
71 EOptionLinkProgram = (1 << 5),
72 EOptionMultiThreaded = (1 << 6),
73 EOptionDumpConfig = (1 << 7),
74 EOptionDumpReflection = (1 << 8),
75 EOptionSuppressWarnings = (1 << 9),
76 EOptionDumpVersions = (1 << 10),
77 EOptionSpv = (1 << 11),
78 EOptionHumanReadableSpv = (1 << 12),
79 EOptionVulkanRules = (1 << 13),
80 EOptionDefaultDesktop = (1 << 14),
81 EOptionOutputPreprocessed = (1 << 15),
82 EOptionOutputHexadecimal = (1 << 16),
83 EOptionReadHlsl = (1 << 17),
84 EOptionCascadingErrors = (1 << 18),
85 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060086 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070087 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070088 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060089 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060090 EOptionHlslIoMapping = (1 << 24),
John Kessenich94a81fb2013-08-31 02:41:30 +000091};
92
John Kessenicha0af4732012-12-12 21:15:54 +000093//
John Kessenich68d78fd2015-07-12 19:28:10 -060094// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000095//
96enum TFailCode {
97 ESuccess = 0,
98 EFailUsage,
99 EFailCompile,
100 EFailLink,
101 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000102 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000103 EFailLinkerCreate
104};
105
106//
John Kessenich68d78fd2015-07-12 19:28:10 -0600107// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000108//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600109EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000110void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000111void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000112void FreeFileData(char** data);
113char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000114void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000115
John Kessenichc999ba22013-11-07 23:33:24 +0000116// Globally track if any compile or link failure.
117bool CompileFailed = false;
118bool LinkFailed = false;
119
John Kessenich05a70632013-09-17 19:26:08 +0000120// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600121// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000122int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000123
John Kessenich05a70632013-09-17 19:26:08 +0000124TBuiltInResource Resources;
125std::string ConfigFile;
126
John Kessenicha0af4732012-12-12 21:15:54 +0000127//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600128// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000129//
130void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000131{
John Kessenich05a70632013-09-17 19:26:08 +0000132 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000133 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000134 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000135 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000136 if (configStrings)
137 config = *configStrings;
138 else {
139 printf("Error opening configuration file; will instead use the default configuration\n");
140 usage();
141 }
142 }
143
144 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500145 Resources = glslang::DefaultTBuiltInResource;
146 return;
John Kessenich05a70632013-09-17 19:26:08 +0000147 }
148
Lei Zhang1b141722016-05-19 13:50:49 -0400149 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000150
John Kessenich05a70632013-09-17 19:26:08 +0000151 if (configStrings)
152 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500153 else
154 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000155}
156
John Kessenich94a81fb2013-08-31 02:41:30 +0000157int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600158const char* ExecutableName = nullptr;
159const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700160const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600161const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400162const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800163const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600164
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600165std::array<unsigned int, EShLangCount> baseSamplerBinding;
166std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600167std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600168std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700169std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600170std::array<unsigned int, EShLangCount> baseUavBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600171
John Kessenich68d78fd2015-07-12 19:28:10 -0600172//
173// Create the default name for saving a binary if -o is not provided.
174//
175const char* GetBinaryName(EShLanguage stage)
176{
177 const char* name;
178 if (binaryFileName == nullptr) {
179 switch (stage) {
180 case EShLangVertex: name = "vert.spv"; break;
181 case EShLangTessControl: name = "tesc.spv"; break;
182 case EShLangTessEvaluation: name = "tese.spv"; break;
183 case EShLangGeometry: name = "geom.spv"; break;
184 case EShLangFragment: name = "frag.spv"; break;
185 case EShLangCompute: name = "comp.spv"; break;
186 default: name = "unknown"; break;
187 }
188 } else
189 name = binaryFileName;
190
191 return name;
192}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000193
John Kessenich05a70632013-09-17 19:26:08 +0000194//
195// *.conf => this is a config file that can set limits/resources
196//
197bool SetConfigFile(const std::string& name)
198{
199 if (name.size() < 5)
200 return false;
201
John Kessenich4c706852013-10-11 16:28:43 +0000202 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000203 ConfigFile = name;
204 return true;
205 }
206
207 return false;
208}
209
John Kessenich68d78fd2015-07-12 19:28:10 -0600210//
211// Give error and exit with failure code.
212//
213void Error(const char* message)
214{
215 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
216 exit(EFailUsage);
217}
218
219//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600220// Process an optional binding base of the form:
221// --argname [stage] base
222// Where stage is one of the forms accepted by FindLanguage, and base is an integer
223//
224void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
225{
226 if (argc < 2)
227 usage();
228
229 if (!isdigit(argv[1][0])) {
230 if (argc < 3) // this form needs one more argument
231 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700232
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600233 // Parse form: --argname stage base
234 const EShLanguage lang = FindLanguage(argv[1], false);
235 base[lang] = atoi(argv[2]);
236 argc-= 2;
237 argv+= 2;
238 } else {
239 // Parse form: --argname base
240 for (int lang=0; lang<EShLangCount; ++lang)
241 base[lang] = atoi(argv[1]);
242
243 argc--;
244 argv++;
245 }
246}
247
248//
John Kessenich68d78fd2015-07-12 19:28:10 -0600249// Do all command-line argument parsing. This includes building up the work-items
250// to be processed later, and saving all the command-line options.
251//
252// Does not return (it exits) if command-line is fatally flawed.
253//
Juan Lopeza558b262017-04-02 23:04:00 +0200254void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000255{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600256 baseSamplerBinding.fill(0);
257 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600258 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600259 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700260 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600261 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600262
John Kessenich38f3b892013-09-06 19:52:57 +0000263 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200264 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000265
John Kessenich2b07c7e2013-07-31 18:44:13 +0000266 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700267 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000268 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" ||
steve-lunargbe283552017-04-18 12:18:01 -0600291 lowerword == "shift-cbuffer-bindings" ||
292 lowerword == "shift-cbuffer-binding" ||
293 lowerword == "sub" ||
294 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600295 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700296 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
297 lowerword == "shift-ssbo-binding" ||
298 lowerword == "sbb") {
299 ProcessBindingBase(argc, argv, baseSsboBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600300 } else if (lowerword == "shift-uav-bindings" || // synonyms
301 lowerword == "shift-uav-binding" ||
302 lowerword == "suavb") {
303 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600304 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700305 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600306 lowerword == "amb") {
307 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600308 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
309 lowerword == "flatten-uniform-array" ||
310 lowerword == "fua") {
311 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600312 } else if (lowerword == "no-storage-format" || // synonyms
313 lowerword == "nsf") {
314 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800315 } else if (lowerword == "variable-name" || // synonyms
316 lowerword == "vn") {
317 Options |= EOptionOutputHexadecimal;
318 variableName = argv[1];
319 if (argc > 0) {
320 argc--;
321 argv++;
322 } else
323 Error("no <C-variable-name> provided for --variable-name");
324 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600325 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600326 lowerword == "sep") {
327 sourceEntryPointName = argv[1];
328 if (argc > 0) {
329 argc--;
330 argv++;
331 } else
332 Error("no <entry-point> provided for --source-entrypoint");
333 break;
John Kessenich906cc212016-12-09 19:22:20 -0700334 } else if (lowerword == "keep-uncalled" || // synonyms
335 lowerword == "ku") {
336 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600337 } else if (lowerword == "hlsl-offsets") {
338 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600339 } else if (lowerword == "hlsl-iomap" ||
340 lowerword == "hlsl-iomapper" ||
341 lowerword == "hlsl-iomapping") {
342 Options |= EOptionHlslIoMapping;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600343 } else {
344 usage();
345 }
346 }
347 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000348 case 'H':
349 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600350 if ((Options & EOptionSpv) == 0) {
351 // default to Vulkan
352 Options |= EOptionSpv;
353 Options |= EOptionVulkanRules;
354 Options |= EOptionLinkProgram;
355 }
356 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000357 case 'V':
358 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600359 Options |= EOptionVulkanRules;
360 Options |= EOptionLinkProgram;
361 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400362 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400363 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400364 if (argc > 0) {
365 argc--;
366 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600367 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400368 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400369 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600370 case 'G':
371 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000372 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600373 // undo a -H default to Vulkan
374 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000375 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000376 case 'E':
377 Options |= EOptionOutputPreprocessed;
378 break;
John Kessenich05a70632013-09-17 19:26:08 +0000379 case 'c':
380 Options |= EOptionDumpConfig;
381 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600382 case 'C':
383 Options |= EOptionCascadingErrors;
384 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000385 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000386 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000387 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700388 case 'D':
389 Options |= EOptionReadHlsl;
390 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700391 case 'e':
392 // HLSL todo: entry point handle needs much more sophistication.
393 // This is okay for one compilation unit with one entry point.
394 entryPointName = argv[1];
395 if (argc > 0) {
396 argc--;
397 argv++;
398 } else
399 Error("no <entry-point> provided for -e");
400 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600401 case 'h':
402 usage();
403 break;
John Kessenich05a70632013-09-17 19:26:08 +0000404 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000405 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000406 break;
407 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000408 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000409 break;
410 case 'm':
411 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000412 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600413 case 'o':
414 binaryFileName = argv[1];
415 if (argc > 0) {
416 argc--;
417 argv++;
418 } else
419 Error("no <file> provided for -o");
420 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000421 case 'q':
422 Options |= EOptionDumpReflection;
423 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000424 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000425 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000426 break;
427 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000428 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000429 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000430 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200431 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000432 break;
John Kessenich319de232013-12-04 04:43:40 +0000433 case 'v':
434 Options |= EOptionDumpVersions;
435 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000436 case 'w':
437 Options |= EOptionSuppressWarnings;
438 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500439 case 'x':
440 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500441 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000442 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600443 usage();
444 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000445 }
John Kessenich38f3b892013-09-06 19:52:57 +0000446 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000447 std::string name(argv[0]);
448 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200449 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000450 }
John Kessenich38f3b892013-09-06 19:52:57 +0000451 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000452 }
453
John Kessenich68d78fd2015-07-12 19:28:10 -0600454 // Make sure that -E is not specified alongside linking (which includes SPV generation)
455 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
456 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000457
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500458 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600459 if (binaryFileName && (Options & EOptionSpv) == 0)
460 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600461
462 if ((Options & EOptionFlattenUniformArrays) != 0 &&
463 (Options & EOptionReadHlsl) == 0)
464 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000465}
466
John Kessenich68d78fd2015-07-12 19:28:10 -0600467//
468// Translate the meaningful subset of command-line options to parser-behavior options.
469//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000470void SetMessageOptions(EShMessages& messages)
471{
472 if (Options & EOptionRelaxedErrors)
473 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
474 if (Options & EOptionIntermediate)
475 messages = (EShMessages)(messages | EShMsgAST);
476 if (Options & EOptionSuppressWarnings)
477 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600478 if (Options & EOptionSpv)
479 messages = (EShMessages)(messages | EShMsgSpvRules);
480 if (Options & EOptionVulkanRules)
481 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400482 if (Options & EOptionOutputPreprocessed)
483 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700484 if (Options & EOptionReadHlsl)
485 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600486 if (Options & EOptionCascadingErrors)
487 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700488 if (Options & EOptionKeepUncalled)
489 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600490 if (Options & EOptionHlslOffsets)
491 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000492}
493
John Kessenich68d78fd2015-07-12 19:28:10 -0600494//
John Kessenich69f4b512013-09-04 21:19:27 +0000495// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000496//
Juan Lopeza558b262017-04-02 23:04:00 +0200497void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498{
John Kessenich38f3b892013-09-06 19:52:57 +0000499 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200500 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000501 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000502 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200503 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000504
John Kessenichb0a7eb52013-11-07 17:44:20 +0000505 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000506
John Kessenich94a81fb2013-08-31 02:41:30 +0000507 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000508 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000509
510 ShDestruct(compiler);
511 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000512}
513
John Kessenich6626cad2015-06-19 05:14:19 +0000514// Outputs the given string, but only if it is non-null and non-empty.
515// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400516void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000517{
518 if (str && str[0]) {
519 puts(str);
520 }
521}
522
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400523// Outputs the given string to stderr, but only if it is non-null and non-empty.
524// This prevents erroneous newlines from appearing.
525void StderrIfNonEmpty(const char* str)
526{
527 if (str && str[0]) {
528 fprintf(stderr, "%s\n", str);
529 }
530}
531
John Kessenichc57b2a92016-01-16 15:30:03 -0700532// Simple bundling of what makes a compilation unit for ease in passing around,
533// and separation of handling file IO versus API (programmatic) compilation.
534struct ShaderCompUnit {
535 EShLanguage stage;
536 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400537 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600538 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400539
John Kessenich219b0252016-08-23 17:51:13 -0600540 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400541 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
542 {
543 stage = istage;
544 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600545 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600546 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400547 }
548
549 ShaderCompUnit(const ShaderCompUnit &rhs)
550 {
551 stage = rhs.stage;
552 fileName = rhs.fileName;
553 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600554 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400555 }
556
John Kessenichc57b2a92016-01-16 15:30:03 -0700557};
558
John Kessenich69f4b512013-09-04 21:19:27 +0000559//
John Kessenichc57b2a92016-01-16 15:30:03 -0700560// For linking mode: Will independently parse each compilation unit, but then put them
561// in the same program and link them together, making at most one linked module per
562// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000563//
564// Uses the new C++ interface instead of the old handle-based interface.
565//
John Kessenichc57b2a92016-01-16 15:30:03 -0700566
567void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000568{
569 // keep track of what to free
570 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700571
John Kessenich69f4b512013-09-04 21:19:27 +0000572 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000573 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000574
John Kessenich69f4b512013-09-04 21:19:27 +0000575 //
576 // Per-shader processing...
577 //
578
John Kessenich5b0f13a2013-11-01 03:08:40 +0000579 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100580 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
581 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700582 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400583 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700584 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
585 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600586 if (sourceEntryPointName)
587 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600588
589 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
590 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600591 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600592 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700593 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600594 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600595 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600596 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600597
steve-lunargbe283552017-04-18 12:18:01 -0600598 if (Options & EOptionHlslIoMapping)
599 shader->setHlslIoMapping(true);
600
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600601 if (Options & EOptionAutoMapBindings)
602 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700603
John Kessenich69f4b512013-09-04 21:19:27 +0000604 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600605
John Kessenichc555ddd2015-06-17 02:38:44 +0000606 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000607
John Kessenichc555ddd2015-06-17 02:38:44 +0000608 if (Options & EOptionOutputPreprocessed) {
609 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700610 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400611 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500612 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400613 PutsIfNonEmpty(str.c_str());
614 } else {
615 CompileFailed = true;
616 }
617 StderrIfNonEmpty(shader->getInfoLog());
618 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000619 continue;
620 }
621 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000622 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000623
John Kessenich69f4b512013-09-04 21:19:27 +0000624 program.addShader(shader);
625
John Kessenichc57b2a92016-01-16 15:30:03 -0700626 if (! (Options & EOptionSuppressInfolog) &&
627 ! (Options & EOptionMemoryLeakMode)) {
628 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400629 PutsIfNonEmpty(shader->getInfoLog());
630 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000631 }
John Kessenich69f4b512013-09-04 21:19:27 +0000632 }
633
634 //
635 // Program-level processing...
636 //
637
John Kessenich4d65ee32016-03-12 18:17:47 -0700638 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600639 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000640 LinkFailed = true;
641
steve-lunarg9ae34742016-10-05 13:40:13 -0600642 // Map IO
643 if (Options & EOptionSpv) {
644 if (!program.mapIO())
645 LinkFailed = true;
646 }
John Kessenichecba76f2017-01-06 00:34:48 -0700647
John Kessenich4d65ee32016-03-12 18:17:47 -0700648 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700649 if (! (Options & EOptionSuppressInfolog) &&
650 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400651 PutsIfNonEmpty(program.getInfoLog());
652 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000653 }
654
John Kessenich4d65ee32016-03-12 18:17:47 -0700655 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000656 if (Options & EOptionDumpReflection) {
657 program.buildReflection();
658 program.dumpReflection();
659 }
660
John Kessenich4d65ee32016-03-12 18:17:47 -0700661 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000662 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000663 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600664 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000665 else {
666 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000667 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000668 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400669 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400670 spv::SpvBuildLogger logger;
671 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700672
673 // Dump the spv to a file or stdout, etc., but only if not doing
674 // memory/perf testing, as it's not internal to programmatic use.
675 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500676 printf("%s", logger.getAllMessages().c_str());
677 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700678 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500679 } else {
680 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
681 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700682 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700683 spv::Disassemble(std::cout, spirv);
684 }
John Kessenichacba7722015-03-04 03:48:38 +0000685 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000686 }
John Kessenich92f90382014-07-28 04:21:04 +0000687 }
688 }
689 }
690
John Kessenich5b0f13a2013-11-01 03:08:40 +0000691 // Free everything up, program has to go before the shaders
692 // because it might have merged stuff from the shaders, and
693 // the stuff from the shaders has to have its destructors called
694 // before the pools holding the memory in the shaders is freed.
695 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000696 while (shaders.size() > 0) {
697 delete shaders.back();
698 shaders.pop_back();
699 }
John Kessenich69f4b512013-09-04 21:19:27 +0000700}
701
John Kessenichc57b2a92016-01-16 15:30:03 -0700702//
703// Do file IO part of compile and link, handing off the pure
704// API/programmatic mode to CompileAndLinkShaderUnits(), which can
705// be put in a loop for testing memory footprint and performance.
706//
707// This is just for linking mode: meaning all the shaders will be put into the
708// the same program linked together.
709//
710// This means there are a limited number of work items (not multi-threading mode)
711// and that the point is testing at the linking level. Hence, to enable
712// performance and memory testing, the actual compile/link can be put in
713// a loop, independent of processing the work items and file IO.
714//
Juan Lopeza558b262017-04-02 23:04:00 +0200715void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700716{
717 std::vector<ShaderCompUnit> compUnits;
718
719 // Transfer all the work items from to a simple list of
720 // of compilation units. (We don't care about the thread
721 // work-item distribution properties in this path, which
722 // is okay due to the limited number of shaders, know since
723 // they are all getting linked together.)
724 glslang::TWorkItem* workItem;
725 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200726 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700727 FindLanguage(workItem->name),
728 workItem->name,
729 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200730 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700731
732 if (! compUnit.text) {
733 usage();
734 return;
735 }
736
737 compUnits.push_back(compUnit);
738 }
739
740 // Actual call to programmatic processing of compile and link,
741 // in a loop for testing memory and performance. This part contains
742 // all the perf/memory that a programmatic consumer will care about.
743 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
744 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
745 CompileAndLinkShaderUnits(compUnits);
746
747 if (Options & EOptionMemoryLeakMode)
748 glslang::OS_DumpMemoryCounters();
749 }
750
rdb32084e82016-02-23 22:17:38 +0100751 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
752 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700753}
754
John Kessenicha0af4732012-12-12 21:15:54 +0000755int C_DECL main(int argc, char* argv[])
756{
Juan Lopeza558b262017-04-02 23:04:00 +0200757 // array of unique places to leave the shader names and infologs for the asynchronous compiles
758 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
759 ProcessArguments(workItems, argc, argv);
760
761 glslang::TWorklist workList;
762 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
763 assert(item);
764 workList.add(item.get());
765 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000766
John Kessenich05a70632013-09-17 19:26:08 +0000767 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500768 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200769 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000770 return ESuccess;
771 }
772
John Kessenich0da9eaa2015-08-01 17:10:02 -0600773 if (Options & EOptionDumpVersions) {
774 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000775 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
776 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600777 std::string spirvVersion;
778 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600779 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600780 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700781 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600782 printf("GL_KHR_vulkan_glsl version %d\n", 100);
783 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200784 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000785 return ESuccess;
786 }
787
Juan Lopeza558b262017-04-02 23:04:00 +0200788 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000789 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000790 }
791
792 ProcessConfigFile();
793
John Kessenich69f4b512013-09-04 21:19:27 +0000794 //
795 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000796 // 1) linking all arguments together, single-threaded, new C++ interface
797 // 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 +0000798 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000799 if (Options & EOptionLinkProgram ||
800 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000801 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200802 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000803 glslang::FinalizeProcess();
804 } else {
805 ShInitialize();
806
Juan Lopeza558b262017-04-02 23:04:00 +0200807 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000808
Juan Lopeza558b262017-04-02 23:04:00 +0200809 if (Options & EOptionMultiThreaded)
810 {
811 std::array<std::thread, 16> threads;
812 for (unsigned int t = 0; t < threads.size(); ++t)
813 {
814 threads[t] = std::thread(CompileShaders, std::ref(workList));
815 if (threads[t].get_id() == std::thread::id())
816 {
John Kessenich38f3b892013-09-06 19:52:57 +0000817 printf("Failed to create thread\n");
818 return EFailThreadCreate;
819 }
John Kessenicha0af4732012-12-12 21:15:54 +0000820 }
Juan Lopeza558b262017-04-02 23:04:00 +0200821
822 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000823 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200824 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000825
826 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200827 for (size_t w = 0; w < workItems.size(); ++w) {
828 if (workItems[w]) {
829 if (printShaderNames || workItems[w]->results.size() > 0)
830 PutsIfNonEmpty(workItems[w]->name.c_str());
831 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000832 }
833 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000834
835 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000836 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000837
John Kessenichc999ba22013-11-07 23:33:24 +0000838 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000839 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000840 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000841 return EFailLink;
842
843 return 0;
844}
845
846//
847// Deduce the language from the filename. Files must end in one of the
848// following extensions:
849//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000850// .vert = vertex
851// .tesc = tessellation control
852// .tese = tessellation evaluation
853// .geom = geometry
854// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000855// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000856//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600857EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000858{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600859 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600860 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600861
Dan Bakerc6ede892016-08-11 14:06:06 -0400862 if (shaderStageName)
863 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600864 else {
865 // Search for a suffix on a filename: e.g, "myfile.frag". If given
866 // the suffix directly, we skip looking for the '.'
867 if (parseSuffix) {
868 ext = name.rfind('.');
869 if (ext == std::string::npos) {
870 usage();
871 return EShLangVertex;
872 }
873 ++ext;
874 }
875 suffix = name.substr(ext, std::string::npos);
876 }
dankbaker45d49bc2016-08-08 21:43:07 -0400877
John Kessenich2b07c7e2013-07-31 18:44:13 +0000878 if (suffix == "vert")
879 return EShLangVertex;
880 else if (suffix == "tesc")
881 return EShLangTessControl;
882 else if (suffix == "tese")
883 return EShLangTessEvaluation;
884 else if (suffix == "geom")
885 return EShLangGeometry;
886 else if (suffix == "frag")
887 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000888 else if (suffix == "comp")
889 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000890
891 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000892 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000893}
894
John Kessenicha0af4732012-12-12 21:15:54 +0000895//
John Kessenichecba76f2017-01-06 00:34:48 -0700896// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000897// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000898//
John Kessenich51cdd902014-02-18 23:37:57 +0000899void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000900{
John Kessenichca3457f2015-05-18 01:59:45 +0000901 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000902 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000903 if (! shaderStrings) {
904 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000905 }
906
John Kessenich41cf6b52013-06-25 18:10:05 +0000907 int* lengths = new int[NumShaderStrings];
908
909 // move to length-based strings, rather than null-terminated strings
910 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000911 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000912
John Kessenichc999ba22013-11-07 23:33:24 +0000913 if (! shaderStrings) {
914 CompileFailed = true;
915 return;
916 }
John Kessenicha0af4732012-12-12 21:15:54 +0000917
John Kessenich52ac67e2013-05-05 23:46:22 +0000918 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000919 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700920
John Kessenich94a81fb2013-08-31 02:41:30 +0000921 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
922 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700923 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000924 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700925 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700926 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000927 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700928 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
929 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000930 }
John Kessenicha0af4732012-12-12 21:15:54 +0000931
John Kessenich94a81fb2013-08-31 02:41:30 +0000932 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000933 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000934 }
John Kessenicha0af4732012-12-12 21:15:54 +0000935
John Kessenich41cf6b52013-06-25 18:10:05 +0000936 delete [] lengths;
937 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000938
John Kessenichc999ba22013-11-07 23:33:24 +0000939 if (ret == 0)
940 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000941}
942
John Kessenicha0af4732012-12-12 21:15:54 +0000943//
944// print usage to stdout
945//
946void usage()
947{
John Kessenich319de232013-12-04 04:43:40 +0000948 printf("Usage: glslangValidator [option]... [file]...\n"
949 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000950 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600951 " .conf to provide an optional config file that replaces the default configuration\n"
952 " (see -c option below for generating a template)\n"
953 " .vert for a vertex shader\n"
954 " .tesc for a tessellation control shader\n"
955 " .tese for a tessellation evaluation shader\n"
956 " .geom for a geometry shader\n"
957 " .frag for a fragment shader\n"
958 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000959 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000960 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000961 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000962 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600963 "Each option must be specified separately.\n"
964 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
965 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600966 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
967 " default file name is <stage>.spv (-o overrides this)\n"
968 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400969 " -E print pre-processed GLSL; cannot be used with -l;\n"
970 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -0600971 " -S <stage> uses specified stage rather than parsing the file extension\n"
972 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600973 " -c configuration dump;\n"
974 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600975 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600976 " -d default to desktop (#version 110) when there is no shader #version\n"
977 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700978 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700979 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600980 " -h print this usage message\n"
981 " -i intermediate tree (glslang AST) is printed out\n"
982 " -l link all input files together to form a single module\n"
983 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500984 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600985 " -q dump reflection query database\n"
986 " -r relaxed semantic error-checking mode\n"
987 " -s silent mode\n"
988 " -t multi-threaded mode\n"
989 " -v print version strings\n"
990 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500991 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600992 "\n"
993 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
994 " --ssb [stage] num synonym for --shift-sampler-binding\n"
995 "\n"
996 " --shift-texture-binding [stage] num set base binding number for textures\n"
997 " --stb [stage] num synonym for --shift-texture-binding\n"
998 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600999 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1000 " --sib [stage] num synonym for --shift-image-binding\n"
1001 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001002 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001003 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001004 " --sub [stage] num synonym for --shift-UBO-binding\n"
1005 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001006 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1007 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1008 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001009 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1010 " --suavb [stage] num synonym for --shift-uav-binding\n"
1011 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001012 " --auto-map-bindings automatically bind uniform variables without\n"
1013 " explicit bindings.\n"
1014 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001015 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001016 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001017 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001018 "\n"
1019 " --no-storage-format use Unknown image format\n"
1020 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001021 "\n"
1022 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1023 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001024 "\n"
1025 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1026 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001027 "\n"
1028 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1029 " initialized with the shader binary code.\n"
1030 " --vn <name> synonym for --variable-name <name>\n"
1031 "\n"
1032 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1033 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001034 "\n"
1035 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001036 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001037
1038 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001039}
1040
John Kessenich3ce4e592014-10-06 19:57:34 +00001041#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001042
1043#include <errno.h>
1044
1045int fopen_s(
1046 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001047 const char* filename,
1048 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001049)
1050{
1051 if (!pFile || !filename || !mode) {
1052 return EINVAL;
1053 }
1054
1055 FILE* f = fopen(filename, mode);
1056 if (! f) {
1057 if (errno != 0) {
1058 return errno;
1059 } else {
1060 return ENOENT;
1061 }
1062 }
1063 *pFile = f;
1064
1065 return 0;
1066}
1067
1068#endif
1069
John Kessenicha0af4732012-12-12 21:15:54 +00001070//
1071// Malloc a string of sufficient size and read a string into it.
1072//
John Kessenichecba76f2017-01-06 00:34:48 -07001073char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001074{
John Kessenichb3297152015-07-11 18:01:03 -06001075 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001076 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001077
John Kessenicha0af4732012-12-12 21:15:54 +00001078 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001079 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1080 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001081
John Kessenich68d78fd2015-07-12 19:28:10 -06001082 if (errorCode || in == nullptr)
1083 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001084
John Kessenicha0af4732012-12-12 21:15:54 +00001085 while (fgetc(in) != EOF)
1086 count++;
1087
John Kessenichd6c72a42014-08-18 19:42:35 +00001088 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001089
John Kessenichb3297152015-07-11 18:01:03 -06001090 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001091 if (! fdata)
1092 Error("can't allocate memory");
1093
John Kessenichb3297152015-07-11 18:01:03 -06001094 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001095 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001096 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001097 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001098
John Kessenicha0af4732012-12-12 21:15:54 +00001099 fdata[count] = '\0';
1100 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001101
John Kessenichea869fb2013-10-28 18:12:06 +00001102 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001103 // recover from empty file
1104 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001105 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001106 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001107 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001108
John Kessenichb3297152015-07-11 18:01:03 -06001109 return return_data;
1110 } else
1111 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1112
1113 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001114 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001115 int ptr_len = 0;
1116 int i = 0;
1117 while (count > 0) {
1118 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1119 memcpy(return_data[i], fdata + ptr_len, len);
1120 return_data[i][len] = '\0';
1121 count -= len;
1122 ptr_len += len;
1123 if (count < len) {
1124 if (count == 0) {
1125 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001126 break;
1127 }
John Kessenichb3297152015-07-11 18:01:03 -06001128 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001129 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001130 ++i;
1131 }
John Kessenichb3297152015-07-11 18:01:03 -06001132
1133 free(fdata);
1134
John Kessenicha0af4732012-12-12 21:15:54 +00001135 return return_data;
1136}
1137
John Kessenich51cdd902014-02-18 23:37:57 +00001138void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001139{
John Kessenichb3297152015-07-11 18:01:03 -06001140 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001141 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001142
1143 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001144}
1145
John Kessenich54d8cda2013-02-11 22:36:01 +00001146void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001147{
John Kessenichfae38ee2015-06-10 23:23:12 +00001148 if (num >= 0 )
1149 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1150 else
1151 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001152}