blob: 646994362d776877de8a9117935de44de48098c2 [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 Kessenich3494b4d2017-05-22 15:00:42 -060044#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000045#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060046#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
Juan Lopeza558b262017-04-02 23:04:00 +020058#include <memory>
59#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000060
baldurk876a0e32015-11-16 18:03:28 +010061#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000062
63extern "C" {
64 SH_IMPORT_EXPORT void ShOutputHtml();
65}
66
John Kessenich94a81fb2013-08-31 02:41:30 +000067// Command-line options
68enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070069 EOptionNone = 0,
70 EOptionIntermediate = (1 << 0),
71 EOptionSuppressInfolog = (1 << 1),
72 EOptionMemoryLeakMode = (1 << 2),
73 EOptionRelaxedErrors = (1 << 3),
74 EOptionGiveWarnings = (1 << 4),
75 EOptionLinkProgram = (1 << 5),
76 EOptionMultiThreaded = (1 << 6),
77 EOptionDumpConfig = (1 << 7),
78 EOptionDumpReflection = (1 << 8),
79 EOptionSuppressWarnings = (1 << 9),
80 EOptionDumpVersions = (1 << 10),
81 EOptionSpv = (1 << 11),
82 EOptionHumanReadableSpv = (1 << 12),
83 EOptionVulkanRules = (1 << 13),
84 EOptionDefaultDesktop = (1 << 14),
85 EOptionOutputPreprocessed = (1 << 15),
86 EOptionOutputHexadecimal = (1 << 16),
87 EOptionReadHlsl = (1 << 17),
88 EOptionCascadingErrors = (1 << 18),
89 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060090 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070091 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070092 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060093 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060094 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060095 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060096 EOptionDebug = (1 << 26),
John Kessenich94a81fb2013-08-31 02:41:30 +000097};
98
John Kessenicha0af4732012-12-12 21:15:54 +000099//
John Kessenich68d78fd2015-07-12 19:28:10 -0600100// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000101//
102enum TFailCode {
103 ESuccess = 0,
104 EFailUsage,
105 EFailCompile,
106 EFailLink,
107 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000108 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000109 EFailLinkerCreate
110};
111
112//
John Kessenich68d78fd2015-07-12 19:28:10 -0600113// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000114//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600115EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000116void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000117void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600118char* ReadFileData(const char* fileName);
119void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000120void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000121
John Kessenichc999ba22013-11-07 23:33:24 +0000122// Globally track if any compile or link failure.
123bool CompileFailed = false;
124bool LinkFailed = false;
125
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 Kessenich04acb1b2017-06-14 17:36:50 -0600134 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500135 Resources = glslang::DefaultTBuiltInResource;
John Kessenich04acb1b2017-06-14 17:36:50 -0600136 else {
137 char* configString = ReadFileData(ConfigFile.c_str());
138 glslang::DecodeResourceLimits(&Resources, configString);
139 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000140 }
John Kessenicha0af4732012-12-12 21:15:54 +0000141}
142
John Kessenich94a81fb2013-08-31 02:41:30 +0000143int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600144const char* ExecutableName = nullptr;
145const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700146const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600147const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400148const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800149const char* variableName = nullptr;
John Kessenich971a0a82017-06-07 15:06:58 -0600150std::vector<std::string> IncludeDirectoryList;
John Kessenich68d78fd2015-07-12 19:28:10 -0600151
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600152std::array<unsigned int, EShLangCount> baseSamplerBinding;
153std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600154std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600155std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700156std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600157std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900158std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600159
John Kessenich68d78fd2015-07-12 19:28:10 -0600160//
161// Create the default name for saving a binary if -o is not provided.
162//
163const char* GetBinaryName(EShLanguage stage)
164{
165 const char* name;
166 if (binaryFileName == nullptr) {
167 switch (stage) {
168 case EShLangVertex: name = "vert.spv"; break;
169 case EShLangTessControl: name = "tesc.spv"; break;
170 case EShLangTessEvaluation: name = "tese.spv"; break;
171 case EShLangGeometry: name = "geom.spv"; break;
172 case EShLangFragment: name = "frag.spv"; break;
173 case EShLangCompute: name = "comp.spv"; break;
174 default: name = "unknown"; break;
175 }
176 } else
177 name = binaryFileName;
178
179 return name;
180}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000181
John Kessenich05a70632013-09-17 19:26:08 +0000182//
183// *.conf => this is a config file that can set limits/resources
184//
185bool SetConfigFile(const std::string& name)
186{
187 if (name.size() < 5)
188 return false;
189
John Kessenich4c706852013-10-11 16:28:43 +0000190 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000191 ConfigFile = name;
192 return true;
193 }
194
195 return false;
196}
197
John Kessenich68d78fd2015-07-12 19:28:10 -0600198//
199// Give error and exit with failure code.
200//
201void Error(const char* message)
202{
203 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
204 exit(EFailUsage);
205}
206
207//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600208// Process an optional binding base of the form:
209// --argname [stage] base
210// Where stage is one of the forms accepted by FindLanguage, and base is an integer
211//
212void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
213{
214 if (argc < 2)
215 usage();
216
217 if (!isdigit(argv[1][0])) {
218 if (argc < 3) // this form needs one more argument
219 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700220
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600221 // Parse form: --argname stage base
222 const EShLanguage lang = FindLanguage(argv[1], false);
223 base[lang] = atoi(argv[2]);
224 argc-= 2;
225 argv+= 2;
226 } else {
227 // Parse form: --argname base
228 for (int lang=0; lang<EShLangCount; ++lang)
229 base[lang] = atoi(argv[1]);
230
231 argc--;
232 argv++;
233 }
234}
235
Hyangran Park36dc8292017-05-02 16:27:29 +0900236void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
237{
238 if (argc < 2)
239 usage();
240
241 if (!isdigit(argv[1][0])) {
242 if (argc < 5) // this form needs one more argument
243 usage();
244
245 // Parse form: --argname stage base
246 const EShLanguage lang = FindLanguage(argv[1], false);
247
248 base[lang].push_back(argv[2]);
249 base[lang].push_back(argv[3]);
250 base[lang].push_back(argv[4]);
251 argc-= 4;
252 argv+= 4;
253 while(argv[1] != NULL) {
254 if(argv[1][0] != '-') {
255 base[lang].push_back(argv[1]);
256 base[lang].push_back(argv[2]);
257 base[lang].push_back(argv[3]);
258 argc-= 3;
259 argv+= 3;
260 }
261 else {
262 break;
263 }
264 }
265 } else {
266 // Parse form: --argname base
267 for (int lang=0; lang<EShLangCount; ++lang)
268 base[lang].push_back(argv[1]);
269
270 argc--;
271 argv++;
272 }
273}
274
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600275//
John Kessenich68d78fd2015-07-12 19:28:10 -0600276// Do all command-line argument parsing. This includes building up the work-items
277// to be processed later, and saving all the command-line options.
278//
279// Does not return (it exits) if command-line is fatally flawed.
280//
Juan Lopeza558b262017-04-02 23:04:00 +0200281void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000282{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600283 baseSamplerBinding.fill(0);
284 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600285 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600286 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700287 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600288 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600289
John Kessenich38f3b892013-09-06 19:52:57 +0000290 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200291 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000292
John Kessenich6263fb12017-06-14 15:52:44 -0600293 const auto getStringOperand = [&](const char* desc) {
294 if (argv[0][2] == 0) {
295 printf("%s must immediately follow option (no spaces)\n", desc);
296 exit(EFailUsage);
297 }
298 return argv[0] + 2;
299 };
300
John Kessenich2b07c7e2013-07-31 18:44:13 +0000301 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700302 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000303 for (; argc >= 1; argc--, argv++) {
304 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000305 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600306 case '-':
307 {
308 std::string lowerword(argv[0]+2);
309 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
310
311 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600312 if (lowerword == "auto-map-bindings" || // synonyms
313 lowerword == "auto-map-binding" ||
314 lowerword == "amb") {
315 Options |= EOptionAutoMapBindings;
316 } else if (lowerword == "auto-map-locations" || // synonyms
317 lowerword == "aml") {
318 Options |= EOptionAutoMapLocations;
319 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
320 lowerword == "flatten-uniform-array" ||
321 lowerword == "fua") {
322 Options |= EOptionFlattenUniformArrays;
323 } else if (lowerword == "hlsl-offsets") {
324 Options |= EOptionHlslOffsets;
325 } else if (lowerword == "hlsl-iomap" ||
326 lowerword == "hlsl-iomapper" ||
327 lowerword == "hlsl-iomapping") {
328 Options |= EOptionHlslIoMapping;
329 } else if (lowerword == "keep-uncalled" || // synonyms
330 lowerword == "ku") {
331 Options |= EOptionKeepUncalled;
332 } else if (lowerword == "no-storage-format" || // synonyms
333 lowerword == "nsf") {
334 Options |= EOptionNoStorageFormat;
335 } else if (lowerword == "resource-set-bindings" || // synonyms
336 lowerword == "resource-set-binding" ||
337 lowerword == "rsb") {
338 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600339 } else if (lowerword == "shift-image-bindings" || // synonyms
340 lowerword == "shift-image-binding" ||
341 lowerword == "sib") {
342 ProcessBindingBase(argc, argv, baseImageBinding);
John Kessenich6263fb12017-06-14 15:52:44 -0600343 } else if (lowerword == "shift-sampler-bindings" || // synonyms
344 lowerword == "shift-sampler-binding" ||
345 lowerword == "ssb") {
346 ProcessBindingBase(argc, argv, baseSamplerBinding);
347 } else if (lowerword == "shift-uav-bindings" || // synonyms
348 lowerword == "shift-uav-binding" ||
349 lowerword == "suavb") {
350 ProcessBindingBase(argc, argv, baseUavBinding);
351 } else if (lowerword == "shift-texture-bindings" || // synonyms
352 lowerword == "shift-texture-binding" ||
353 lowerword == "stb") {
354 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600355 } else if (lowerword == "shift-ubo-bindings" || // synonyms
356 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600357 lowerword == "shift-cbuffer-bindings" ||
358 lowerword == "shift-cbuffer-binding" ||
359 lowerword == "sub" ||
360 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600361 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700362 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
363 lowerword == "shift-ssbo-binding" ||
364 lowerword == "sbb") {
365 ProcessBindingBase(argc, argv, baseSsboBinding);
John Kessenich6263fb12017-06-14 15:52:44 -0600366 } else if (lowerword == "source-entrypoint" || // synonyms
367 lowerword == "sep") {
368 sourceEntryPointName = argv[1];
369 if (argc > 0) {
370 argc--;
371 argv++;
372 } else
373 Error("no <entry-point> provided for --source-entrypoint");
374 break;
Flavio15017db2017-02-15 14:29:33 -0800375 } else if (lowerword == "variable-name" || // synonyms
376 lowerword == "vn") {
377 Options |= EOptionOutputHexadecimal;
378 variableName = argv[1];
379 if (argc > 0) {
380 argc--;
381 argv++;
382 } else
383 Error("no <C-variable-name> provided for --variable-name");
384 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600385 } else {
386 usage();
387 }
388 }
389 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600390 case 'C':
391 Options |= EOptionCascadingErrors;
392 break;
393 case 'D':
394 Options |= EOptionReadHlsl;
395 break;
396 case 'E':
397 Options |= EOptionOutputPreprocessed;
398 break;
399 case 'G':
400 Options |= EOptionSpv;
401 Options |= EOptionLinkProgram;
402 // undo a -H default to Vulkan
403 Options &= ~EOptionVulkanRules;
404 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000405 case 'H':
406 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600407 if ((Options & EOptionSpv) == 0) {
408 // default to Vulkan
409 Options |= EOptionSpv;
410 Options |= EOptionVulkanRules;
411 Options |= EOptionLinkProgram;
412 }
413 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600414 case 'I':
John Kessenich6263fb12017-06-14 15:52:44 -0600415 IncludeDirectoryList.push_back(getStringOperand("-I include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600416 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400417 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400418 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400419 if (argc > 0) {
420 argc--;
421 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600422 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400423 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400424 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600425 case 'V':
John Kessenich68d78fd2015-07-12 19:28:10 -0600426 Options |= EOptionSpv;
John Kessenich6263fb12017-06-14 15:52:44 -0600427 Options |= EOptionVulkanRules;
John Kessenichd78e3512014-08-25 20:07:55 +0000428 Options |= EOptionLinkProgram;
John Kessenichc555ddd2015-06-17 02:38:44 +0000429 break;
John Kessenich05a70632013-09-17 19:26:08 +0000430 case 'c':
431 Options |= EOptionDumpConfig;
432 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000433 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000434 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000435 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700436 case 'e':
437 // HLSL todo: entry point handle needs much more sophistication.
438 // This is okay for one compilation unit with one entry point.
439 entryPointName = argv[1];
440 if (argc > 0) {
441 argc--;
442 argv++;
443 } else
444 Error("no <entry-point> provided for -e");
445 break;
John Kessenich121853f2017-05-31 17:11:16 -0600446 case 'g':
447 Options |= EOptionDebug;
448 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600449 case 'h':
450 usage();
451 break;
John Kessenich05a70632013-09-17 19:26:08 +0000452 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000453 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000454 break;
455 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000456 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000457 break;
458 case 'm':
459 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000460 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600461 case 'o':
462 binaryFileName = argv[1];
463 if (argc > 0) {
464 argc--;
465 argv++;
466 } else
467 Error("no <file> provided for -o");
468 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000469 case 'q':
470 Options |= EOptionDumpReflection;
471 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000472 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000473 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000474 break;
475 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000476 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000477 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000478 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200479 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000480 break;
John Kessenich319de232013-12-04 04:43:40 +0000481 case 'v':
482 Options |= EOptionDumpVersions;
483 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000484 case 'w':
485 Options |= EOptionSuppressWarnings;
486 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500487 case 'x':
488 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500489 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000490 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600491 usage();
492 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000493 }
John Kessenich38f3b892013-09-06 19:52:57 +0000494 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000495 std::string name(argv[0]);
496 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200497 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000498 }
John Kessenich38f3b892013-09-06 19:52:57 +0000499 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000500 }
501
John Kessenich68d78fd2015-07-12 19:28:10 -0600502 // Make sure that -E is not specified alongside linking (which includes SPV generation)
503 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
504 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000505
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500506 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600507 if (binaryFileName && (Options & EOptionSpv) == 0)
508 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600509
510 if ((Options & EOptionFlattenUniformArrays) != 0 &&
511 (Options & EOptionReadHlsl) == 0)
512 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000513}
514
John Kessenich68d78fd2015-07-12 19:28:10 -0600515//
516// Translate the meaningful subset of command-line options to parser-behavior options.
517//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000518void SetMessageOptions(EShMessages& messages)
519{
520 if (Options & EOptionRelaxedErrors)
521 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
522 if (Options & EOptionIntermediate)
523 messages = (EShMessages)(messages | EShMsgAST);
524 if (Options & EOptionSuppressWarnings)
525 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600526 if (Options & EOptionSpv)
527 messages = (EShMessages)(messages | EShMsgSpvRules);
528 if (Options & EOptionVulkanRules)
529 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400530 if (Options & EOptionOutputPreprocessed)
531 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700532 if (Options & EOptionReadHlsl)
533 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600534 if (Options & EOptionCascadingErrors)
535 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700536 if (Options & EOptionKeepUncalled)
537 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600538 if (Options & EOptionHlslOffsets)
539 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600540 if (Options & EOptionDebug)
541 messages = (EShMessages)(messages | EShMsgDebugInfo);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000542}
543
John Kessenich68d78fd2015-07-12 19:28:10 -0600544//
John Kessenich69f4b512013-09-04 21:19:27 +0000545// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000546//
Juan Lopeza558b262017-04-02 23:04:00 +0200547void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000548{
John Kessenich38f3b892013-09-06 19:52:57 +0000549 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200550 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000551 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000552 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200553 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000554
John Kessenichb0a7eb52013-11-07 17:44:20 +0000555 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000556
John Kessenich94a81fb2013-08-31 02:41:30 +0000557 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000558 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000559
560 ShDestruct(compiler);
561 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000562}
563
John Kessenich6626cad2015-06-19 05:14:19 +0000564// Outputs the given string, but only if it is non-null and non-empty.
565// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400566void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000567{
568 if (str && str[0]) {
569 puts(str);
570 }
571}
572
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400573// Outputs the given string to stderr, but only if it is non-null and non-empty.
574// This prevents erroneous newlines from appearing.
575void StderrIfNonEmpty(const char* str)
576{
John Kessenich04acb1b2017-06-14 17:36:50 -0600577 if (str && str[0])
578 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400579}
580
John Kessenichc57b2a92016-01-16 15:30:03 -0700581// Simple bundling of what makes a compilation unit for ease in passing around,
582// and separation of handling file IO versus API (programmatic) compilation.
583struct ShaderCompUnit {
584 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600585 static const int maxCount = 1;
586 int count; // live number of strings/names
587 char* text[maxCount]; // memory owned/managed externally
588 std::string fileName[maxCount]; // hold's the memory, but...
589 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400590
John Kessenich04acb1b2017-06-14 17:36:50 -0600591 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400592
John Kessenich04acb1b2017-06-14 17:36:50 -0600593 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400594 {
595 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600596 count = rhs.count;
597 for (int i = 0; i < count; ++i) {
598 fileName[i] = rhs.fileName[i];
599 text[i] = rhs.text[i];
600 fileNameList[i] = rhs.fileName[i].c_str();
601 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400602 }
603
John Kessenich04acb1b2017-06-14 17:36:50 -0600604 void addString(std::string& ifileName, char* itext)
605 {
606 assert(count < maxCount);
607 fileName[count] = ifileName;
608 text[count] = itext;
609 fileNameList[count] = fileName[count].c_str();
610 ++count;
611 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700612};
613
John Kessenich69f4b512013-09-04 21:19:27 +0000614//
John Kessenichc57b2a92016-01-16 15:30:03 -0700615// For linking mode: Will independently parse each compilation unit, but then put them
616// in the same program and link them together, making at most one linked module per
617// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000618//
619// Uses the new C++ interface instead of the old handle-based interface.
620//
John Kessenichc57b2a92016-01-16 15:30:03 -0700621
622void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000623{
624 // keep track of what to free
625 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700626
John Kessenich69f4b512013-09-04 21:19:27 +0000627 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000628 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000629
John Kessenich69f4b512013-09-04 21:19:27 +0000630 //
631 // Per-shader processing...
632 //
633
John Kessenich5b0f13a2013-11-01 03:08:40 +0000634 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100635 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
636 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700637 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -0600638 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich4d65ee32016-03-12 18:17:47 -0700639 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
640 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600641 if (sourceEntryPointName)
642 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600643
644 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
645 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600646 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600647 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700648 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600649 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600650 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600651 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900652 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600653
steve-lunargbe283552017-04-18 12:18:01 -0600654 if (Options & EOptionHlslIoMapping)
655 shader->setHlslIoMapping(true);
656
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600657 if (Options & EOptionAutoMapBindings)
658 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700659
John Kessenich71facdf2017-05-17 18:28:19 -0600660 if (Options & EOptionAutoMapLocations)
661 shader->setAutoMapLocations(true);
662
John Kessenich69f4b512013-09-04 21:19:27 +0000663 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600664
John Kessenichc555ddd2015-06-17 02:38:44 +0000665 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000666
John Kessenich3494b4d2017-05-22 15:00:42 -0600667 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -0600668 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
669 includer.pushExternalLocalDirectory(dir); });
John Kessenichc555ddd2015-06-17 02:38:44 +0000670 if (Options & EOptionOutputPreprocessed) {
671 std::string str;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400672 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500673 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400674 PutsIfNonEmpty(str.c_str());
675 } else {
676 CompileFailed = true;
677 }
678 StderrIfNonEmpty(shader->getInfoLog());
679 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000680 continue;
681 }
John Kessenich3494b4d2017-05-22 15:00:42 -0600682 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +0000683 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000684
John Kessenich69f4b512013-09-04 21:19:27 +0000685 program.addShader(shader);
686
John Kessenichc57b2a92016-01-16 15:30:03 -0700687 if (! (Options & EOptionSuppressInfolog) &&
688 ! (Options & EOptionMemoryLeakMode)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600689 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400690 PutsIfNonEmpty(shader->getInfoLog());
691 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000692 }
John Kessenich69f4b512013-09-04 21:19:27 +0000693 }
694
695 //
696 // Program-level processing...
697 //
698
John Kessenich4d65ee32016-03-12 18:17:47 -0700699 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600700 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000701 LinkFailed = true;
702
steve-lunarg9ae34742016-10-05 13:40:13 -0600703 // Map IO
704 if (Options & EOptionSpv) {
705 if (!program.mapIO())
706 LinkFailed = true;
707 }
John Kessenichecba76f2017-01-06 00:34:48 -0700708
John Kessenich4d65ee32016-03-12 18:17:47 -0700709 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700710 if (! (Options & EOptionSuppressInfolog) &&
711 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400712 PutsIfNonEmpty(program.getInfoLog());
713 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000714 }
715
John Kessenich4d65ee32016-03-12 18:17:47 -0700716 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000717 if (Options & EOptionDumpReflection) {
718 program.buildReflection();
719 program.dumpReflection();
720 }
721
John Kessenich4d65ee32016-03-12 18:17:47 -0700722 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000723 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000724 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600725 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000726 else {
727 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000728 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000729 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400730 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400731 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -0600732 glslang::SpvOptions spvOptions;
733 if (Options & EOptionDebug)
734 spvOptions.generateDebugInfo = true;
735 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -0700736
737 // Dump the spv to a file or stdout, etc., but only if not doing
738 // memory/perf testing, as it's not internal to programmatic use.
739 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500740 printf("%s", logger.getAllMessages().c_str());
741 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700742 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500743 } else {
744 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
745 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700746 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700747 spv::Disassemble(std::cout, spirv);
748 }
John Kessenichacba7722015-03-04 03:48:38 +0000749 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000750 }
John Kessenich92f90382014-07-28 04:21:04 +0000751 }
752 }
753 }
754
John Kessenich5b0f13a2013-11-01 03:08:40 +0000755 // Free everything up, program has to go before the shaders
756 // because it might have merged stuff from the shaders, and
757 // the stuff from the shaders has to have its destructors called
758 // before the pools holding the memory in the shaders is freed.
759 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000760 while (shaders.size() > 0) {
761 delete shaders.back();
762 shaders.pop_back();
763 }
John Kessenich69f4b512013-09-04 21:19:27 +0000764}
765
John Kessenichc57b2a92016-01-16 15:30:03 -0700766//
767// Do file IO part of compile and link, handing off the pure
768// API/programmatic mode to CompileAndLinkShaderUnits(), which can
769// be put in a loop for testing memory footprint and performance.
770//
771// This is just for linking mode: meaning all the shaders will be put into the
772// the same program linked together.
773//
774// This means there are a limited number of work items (not multi-threading mode)
775// and that the point is testing at the linking level. Hence, to enable
776// performance and memory testing, the actual compile/link can be put in
777// a loop, independent of processing the work items and file IO.
778//
Juan Lopeza558b262017-04-02 23:04:00 +0200779void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700780{
781 std::vector<ShaderCompUnit> compUnits;
782
783 // Transfer all the work items from to a simple list of
784 // of compilation units. (We don't care about the thread
785 // work-item distribution properties in this path, which
786 // is okay due to the limited number of shaders, know since
787 // they are all getting linked together.)
788 glslang::TWorkItem* workItem;
789 while (Worklist.remove(workItem)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600790 ShaderCompUnit compUnit(FindLanguage(workItem->name));
791 char* fileText = ReadFileData(workItem->name.c_str());
792 if (fileText == nullptr)
John Kessenichc57b2a92016-01-16 15:30:03 -0700793 usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600794 compUnit.addString(workItem->name, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -0700795 compUnits.push_back(compUnit);
796 }
797
798 // Actual call to programmatic processing of compile and link,
799 // in a loop for testing memory and performance. This part contains
800 // all the perf/memory that a programmatic consumer will care about.
801 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
802 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
803 CompileAndLinkShaderUnits(compUnits);
804
805 if (Options & EOptionMemoryLeakMode)
806 glslang::OS_DumpMemoryCounters();
807 }
808
rdb32084e82016-02-23 22:17:38 +0100809 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenich04acb1b2017-06-14 17:36:50 -0600810 FreeFileData(it->text[0]);
John Kessenichc57b2a92016-01-16 15:30:03 -0700811}
812
John Kessenicha0af4732012-12-12 21:15:54 +0000813int C_DECL main(int argc, char* argv[])
814{
Juan Lopeza558b262017-04-02 23:04:00 +0200815 // array of unique places to leave the shader names and infologs for the asynchronous compiles
816 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
817 ProcessArguments(workItems, argc, argv);
818
819 glslang::TWorklist workList;
820 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
821 assert(item);
822 workList.add(item.get());
823 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000824
John Kessenich05a70632013-09-17 19:26:08 +0000825 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500826 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200827 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000828 return ESuccess;
829 }
830
John Kessenich0da9eaa2015-08-01 17:10:02 -0600831 if (Options & EOptionDumpVersions) {
832 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000833 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
834 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600835 std::string spirvVersion;
836 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600837 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600838 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700839 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600840 printf("GL_KHR_vulkan_glsl version %d\n", 100);
841 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200842 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000843 return ESuccess;
844 }
845
Juan Lopeza558b262017-04-02 23:04:00 +0200846 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000847 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000848 }
849
850 ProcessConfigFile();
851
John Kessenich69f4b512013-09-04 21:19:27 +0000852 //
853 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000854 // 1) linking all arguments together, single-threaded, new C++ interface
855 // 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 +0000856 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000857 if (Options & EOptionLinkProgram ||
858 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000859 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200860 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000861 glslang::FinalizeProcess();
862 } else {
863 ShInitialize();
864
Juan Lopeza558b262017-04-02 23:04:00 +0200865 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000866
Juan Lopeza558b262017-04-02 23:04:00 +0200867 if (Options & EOptionMultiThreaded)
868 {
869 std::array<std::thread, 16> threads;
870 for (unsigned int t = 0; t < threads.size(); ++t)
871 {
872 threads[t] = std::thread(CompileShaders, std::ref(workList));
873 if (threads[t].get_id() == std::thread::id())
874 {
John Kessenich38f3b892013-09-06 19:52:57 +0000875 printf("Failed to create thread\n");
876 return EFailThreadCreate;
877 }
John Kessenicha0af4732012-12-12 21:15:54 +0000878 }
Juan Lopeza558b262017-04-02 23:04:00 +0200879
880 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000881 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200882 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000883
884 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200885 for (size_t w = 0; w < workItems.size(); ++w) {
886 if (workItems[w]) {
887 if (printShaderNames || workItems[w]->results.size() > 0)
888 PutsIfNonEmpty(workItems[w]->name.c_str());
889 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000890 }
891 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000892
893 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000894 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000895
John Kessenichc999ba22013-11-07 23:33:24 +0000896 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000897 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000898 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000899 return EFailLink;
900
901 return 0;
902}
903
904//
905// Deduce the language from the filename. Files must end in one of the
906// following extensions:
907//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000908// .vert = vertex
909// .tesc = tessellation control
910// .tese = tessellation evaluation
911// .geom = geometry
912// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000913// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000914//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600915EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000916{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600917 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600918 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600919
Dan Bakerc6ede892016-08-11 14:06:06 -0400920 if (shaderStageName)
921 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600922 else {
923 // Search for a suffix on a filename: e.g, "myfile.frag". If given
924 // the suffix directly, we skip looking for the '.'
925 if (parseSuffix) {
926 ext = name.rfind('.');
927 if (ext == std::string::npos) {
928 usage();
929 return EShLangVertex;
930 }
931 ++ext;
932 }
933 suffix = name.substr(ext, std::string::npos);
934 }
dankbaker45d49bc2016-08-08 21:43:07 -0400935
John Kessenich2b07c7e2013-07-31 18:44:13 +0000936 if (suffix == "vert")
937 return EShLangVertex;
938 else if (suffix == "tesc")
939 return EShLangTessControl;
940 else if (suffix == "tese")
941 return EShLangTessEvaluation;
942 else if (suffix == "geom")
943 return EShLangGeometry;
944 else if (suffix == "frag")
945 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000946 else if (suffix == "comp")
947 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000948
949 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000950 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000951}
952
John Kessenicha0af4732012-12-12 21:15:54 +0000953//
John Kessenichecba76f2017-01-06 00:34:48 -0700954// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000955// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000956//
John Kessenich51cdd902014-02-18 23:37:57 +0000957void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000958{
John Kessenichca3457f2015-05-18 01:59:45 +0000959 int ret = 0;
John Kessenich04acb1b2017-06-14 17:36:50 -0600960 char* shaderString = ReadFileData(fileName);
John Kessenich41cf6b52013-06-25 18:10:05 +0000961
962 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -0600963 int* lengths = new int[1];
964 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +0000965
John Kessenich52ac67e2013-05-05 23:46:22 +0000966 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000967 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700968
John Kessenich94a81fb2013-08-31 02:41:30 +0000969 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
970 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700971 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -0600972 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700973 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700974 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000975 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700976 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
977 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000978 }
John Kessenicha0af4732012-12-12 21:15:54 +0000979
John Kessenich94a81fb2013-08-31 02:41:30 +0000980 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000981 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000982 }
John Kessenicha0af4732012-12-12 21:15:54 +0000983
John Kessenich41cf6b52013-06-25 18:10:05 +0000984 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -0600985 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +0000986
John Kessenichc999ba22013-11-07 23:33:24 +0000987 if (ret == 0)
988 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000989}
990
John Kessenicha0af4732012-12-12 21:15:54 +0000991//
992// print usage to stdout
993//
994void usage()
995{
John Kessenich319de232013-12-04 04:43:40 +0000996 printf("Usage: glslangValidator [option]... [file]...\n"
997 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -0600998 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
999 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001000 " (see -c option below for generating a template)\n"
1001 " .vert for a vertex shader\n"
1002 " .tesc for a tessellation control shader\n"
1003 " .tese for a tessellation evaluation shader\n"
1004 " .geom for a geometry shader\n"
1005 " .frag for a fragment shader\n"
1006 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001007 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001008 "Options:\n"
1009 " -C cascading errors; risk crash from accumulation of error recoveries\n"
1010 " -D input is HLSL\n"
1011 " -E print pre-processed GLSL; cannot be used with -l;\n"
1012 " errors will appear on stderr.\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001013 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1014 " default file name is <stage>.spv (-o overrides this)\n"
1015 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001016 " -I<dir> add dir to the include search path; includer's directory\n"
1017 " is searched first, followed by left-to-right order of -I\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001018 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001019 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
1020 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1021 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001022 " -c configuration dump;\n"
1023 " creates the default configuration file (redirect to a .conf file)\n"
1024 " -d default to desktop (#version 110) when there is no shader #version\n"
1025 " (default is ES version 100)\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001026 " -e specify entry-point name\n"
John Kessenich121853f2017-05-31 17:11:16 -06001027 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001028 " -h print this usage message\n"
1029 " -i intermediate tree (glslang AST) is printed out\n"
1030 " -l link all input files together to form a single module\n"
1031 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001032 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001033 " -q dump reflection query database\n"
1034 " -r relaxed semantic error-checking mode\n"
1035 " -s silent mode\n"
1036 " -t multi-threaded mode\n"
1037 " -v print version strings\n"
1038 " -w suppress warnings (except as required by #extension : warn)\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001039 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
1040 " --auto-map-bindings automatically bind uniform variables\n"
1041 " without explicit bindings.\n"
1042 " --amb synonym for --auto-map-bindings\n"
1043 " --auto-map-locations automatically locate input/output lacking\n"
1044 " 'location'\n (fragile, not cross stage)\n"
1045 " --aml synonym for --auto-map-locations\n"
1046 " --flatten-uniform-arrays flatten uniform texture/sampler arrays to\n"
1047 " scalars\n"
1048 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001049 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001050 " --hlsl-offsets Allow block offsets to follow HLSL rules\n"
1051 " Works independently of source language\n"
1052 " --hlsl-iomap Perform IO mapping in HLSL register space\n"
1053 " --keep-uncalled don't eliminate uncalled functions\n"
1054 " --ku synonym for --keep-uncalled\n"
1055 " --no-storage-format use Unknown image format\n"
1056 " --nsf synonym for --no-storage-format\n"
1057 " --resource-set-binding [stage] num descriptor set and binding for resources\n"
1058 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1059 " --shift-image-binding [stage] num base binding number for images (uav)\n"
1060 " --sib [stage] num synonym for --shift-image-binding\n"
1061 " --shift-sampler-binding [stage] num base binding number for samplers\n"
1062 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1063 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1064 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1065 " --shift-texture-binding [stage] num base binding number for textures\n"
1066 " --stb [stage] num synonym for --shift-texture-binding\n"
1067 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1068 " --suavb [stage] num synonym for --shift-uav-binding\n"
1069 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1070 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
1071 " --sub [stage] num synonym for --shift-UBO-binding\n"
1072 " --source-entrypoint name the given shader source function is\n"
1073 " renamed to be the entry point given in -e\n"
1074 " --sep synonym for --source-entrypoint\n"
1075 " --variable-name <name> Creates a C header file that contains a\n"
1076 " uint32_t array named <name>\n"
1077 " initialized with the shader binary code.\n"
1078 " --vn <name> synonym for --variable-name <name>\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001079 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001080
1081 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001082}
1083
John Kessenich3ce4e592014-10-06 19:57:34 +00001084#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001085
1086#include <errno.h>
1087
1088int fopen_s(
1089 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001090 const char* filename,
1091 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001092)
1093{
1094 if (!pFile || !filename || !mode) {
1095 return EINVAL;
1096 }
1097
1098 FILE* f = fopen(filename, mode);
1099 if (! f) {
1100 if (errno != 0) {
1101 return errno;
1102 } else {
1103 return ENOENT;
1104 }
1105 }
1106 *pFile = f;
1107
1108 return 0;
1109}
1110
1111#endif
1112
John Kessenicha0af4732012-12-12 21:15:54 +00001113//
1114// Malloc a string of sufficient size and read a string into it.
1115//
John Kessenich04acb1b2017-06-14 17:36:50 -06001116char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001117{
John Kessenichb3297152015-07-11 18:01:03 -06001118 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001119 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001120 if (errorCode || in == nullptr)
1121 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001122
John Kessenich04acb1b2017-06-14 17:36:50 -06001123 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001124 while (fgetc(in) != EOF)
1125 count++;
1126
John Kessenichd6c72a42014-08-18 19:42:35 +00001127 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001128
John Kessenich04acb1b2017-06-14 17:36:50 -06001129 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1130 if ((int)fread(return_data, 1, count, in) != count) {
1131 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001132 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001133 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001134
John Kessenich04acb1b2017-06-14 17:36:50 -06001135 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001136 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001137
John Kessenicha0af4732012-12-12 21:15:54 +00001138 return return_data;
1139}
1140
John Kessenich04acb1b2017-06-14 17:36:50 -06001141void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001142{
John Kessenichb3297152015-07-11 18:01:03 -06001143 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}