blob: 377e7cb33bc56a40f3257156c678710d2013cbba [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
2//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06003//Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenicha0af4732012-12-12 21:15:54 +00005//All rights reserved.
6//
7//Redistribution and use in source and binary forms, with or without
8//modification, are permitted provided that the following conditions
9//are met:
10//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
23//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24//"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25//LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26//FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27//COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28//INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29//BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30//LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31//CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32//LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33//ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34//POSSIBILITY OF SUCH DAMAGE.
35//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
38#define _CRT_SECURE_NO_WARNINGS
39
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040040#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000041#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000042#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060043#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000045#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060046#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000047#include "../SPIRV/doc.h"
48#include "../SPIRV/disassemble.h"
John Kessenich66ec80e2016-08-05 14:04:23 -060049#include <cstring>
50#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060051#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060052#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060053#include <array>
John Kessenicha0af4732012-12-12 21:15:54 +000054
baldurk876a0e32015-11-16 18:03:28 +010055#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000056
57extern "C" {
58 SH_IMPORT_EXPORT void ShOutputHtml();
59}
60
John Kessenich94a81fb2013-08-31 02:41:30 +000061// Command-line options
62enum TOptions {
John Kessenicha86836e2016-07-09 14:50:57 -060063 EOptionNone = 0,
64 EOptionIntermediate = (1 << 0),
65 EOptionSuppressInfolog = (1 << 1),
66 EOptionMemoryLeakMode = (1 << 2),
67 EOptionRelaxedErrors = (1 << 3),
68 EOptionGiveWarnings = (1 << 4),
69 EOptionLinkProgram = (1 << 5),
70 EOptionMultiThreaded = (1 << 6),
71 EOptionDumpConfig = (1 << 7),
72 EOptionDumpReflection = (1 << 8),
73 EOptionSuppressWarnings = (1 << 9),
74 EOptionDumpVersions = (1 << 10),
75 EOptionSpv = (1 << 11),
76 EOptionHumanReadableSpv = (1 << 12),
77 EOptionVulkanRules = (1 << 13),
78 EOptionDefaultDesktop = (1 << 14),
79 EOptionOutputPreprocessed = (1 << 15),
80 EOptionOutputHexadecimal = (1 << 16),
81 EOptionReadHlsl = (1 << 17),
82 EOptionCascadingErrors = (1 << 18),
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060083 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060084 EOptionFlattenUniformArrays = (1 << 20),
steve-lunargcce8d482016-10-14 18:36:42 -060085 EOptionNoStorageFormat = (1 << 21),
John Kessenich94a81fb2013-08-31 02:41:30 +000086};
87
John Kessenicha0af4732012-12-12 21:15:54 +000088//
John Kessenich68d78fd2015-07-12 19:28:10 -060089// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000090//
91enum TFailCode {
92 ESuccess = 0,
93 EFailUsage,
94 EFailCompile,
95 EFailLink,
96 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000097 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000098 EFailLinkerCreate
99};
100
101//
John Kessenich68d78fd2015-07-12 19:28:10 -0600102// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000103//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600104EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000105void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000106void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000107void FreeFileData(char** data);
108char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000109void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000110
John Kessenichc999ba22013-11-07 23:33:24 +0000111// Globally track if any compile or link failure.
112bool CompileFailed = false;
113bool LinkFailed = false;
114
John Kessenich05a70632013-09-17 19:26:08 +0000115// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600116// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000117int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000118
John Kessenich05a70632013-09-17 19:26:08 +0000119TBuiltInResource Resources;
120std::string ConfigFile;
121
John Kessenicha0af4732012-12-12 21:15:54 +0000122//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600123// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000124//
125void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000126{
John Kessenich05a70632013-09-17 19:26:08 +0000127 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000128 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000129 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000130 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000131 if (configStrings)
132 config = *configStrings;
133 else {
134 printf("Error opening configuration file; will instead use the default configuration\n");
135 usage();
136 }
137 }
138
139 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500140 Resources = glslang::DefaultTBuiltInResource;
141 return;
John Kessenich05a70632013-09-17 19:26:08 +0000142 }
143
Lei Zhang1b141722016-05-19 13:50:49 -0400144 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000145
John Kessenich05a70632013-09-17 19:26:08 +0000146 if (configStrings)
147 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500148 else
149 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000150}
151
John Kessenich38f3b892013-09-06 19:52:57 +0000152// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000153glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000154
155// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000156glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000157int NumWorkItems = 0;
158
John Kessenich94a81fb2013-08-31 02:41:30 +0000159int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600160const char* ExecutableName = nullptr;
161const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700162const char* entryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400163const char* shaderStageName = 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;
169
John Kessenich68d78fd2015-07-12 19:28:10 -0600170//
171// Create the default name for saving a binary if -o is not provided.
172//
173const char* GetBinaryName(EShLanguage stage)
174{
175 const char* name;
176 if (binaryFileName == nullptr) {
177 switch (stage) {
178 case EShLangVertex: name = "vert.spv"; break;
179 case EShLangTessControl: name = "tesc.spv"; break;
180 case EShLangTessEvaluation: name = "tese.spv"; break;
181 case EShLangGeometry: name = "geom.spv"; break;
182 case EShLangFragment: name = "frag.spv"; break;
183 case EShLangCompute: name = "comp.spv"; break;
184 default: name = "unknown"; break;
185 }
186 } else
187 name = binaryFileName;
188
189 return name;
190}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000191
John Kessenich05a70632013-09-17 19:26:08 +0000192//
193// *.conf => this is a config file that can set limits/resources
194//
195bool SetConfigFile(const std::string& name)
196{
197 if (name.size() < 5)
198 return false;
199
John Kessenich4c706852013-10-11 16:28:43 +0000200 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000201 ConfigFile = name;
202 return true;
203 }
204
205 return false;
206}
207
John Kessenich68d78fd2015-07-12 19:28:10 -0600208//
209// Give error and exit with failure code.
210//
211void Error(const char* message)
212{
213 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
214 exit(EFailUsage);
215}
216
217//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600218// Process an optional binding base of the form:
219// --argname [stage] base
220// Where stage is one of the forms accepted by FindLanguage, and base is an integer
221//
222void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
223{
224 if (argc < 2)
225 usage();
226
227 if (!isdigit(argv[1][0])) {
228 if (argc < 3) // this form needs one more argument
229 usage();
230
231 // Parse form: --argname stage base
232 const EShLanguage lang = FindLanguage(argv[1], false);
233 base[lang] = atoi(argv[2]);
234 argc-= 2;
235 argv+= 2;
236 } else {
237 // Parse form: --argname base
238 for (int lang=0; lang<EShLangCount; ++lang)
239 base[lang] = atoi(argv[1]);
240
241 argc--;
242 argv++;
243 }
244}
245
246//
John Kessenich68d78fd2015-07-12 19:28:10 -0600247// Do all command-line argument parsing. This includes building up the work-items
248// to be processed later, and saving all the command-line options.
249//
250// Does not return (it exits) if command-line is fatally flawed.
251//
252void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000253{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600254 baseSamplerBinding.fill(0);
255 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600256 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600257 baseUboBinding.fill(0);
258
John Kessenich38f3b892013-09-06 19:52:57 +0000259 ExecutableName = argv[0];
260 NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0
John Kessenichfd305422014-06-05 16:30:53 +0000261 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600262 for (int w = 0; w < NumWorkItems; ++w)
263 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000264
John Kessenich2b07c7e2013-07-31 18:44:13 +0000265 argc--;
266 argv++;
267 for (; argc >= 1; argc--, argv++) {
268 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000269 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600270 case '-':
271 {
272 std::string lowerword(argv[0]+2);
273 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
274
275 // handle --word style options
276 if (lowerword == "shift-sampler-bindings" || // synonyms
277 lowerword == "shift-sampler-binding" ||
278 lowerword == "ssb") {
279 ProcessBindingBase(argc, argv, baseSamplerBinding);
280 } else if (lowerword == "shift-texture-bindings" || // synonyms
281 lowerword == "shift-texture-binding" ||
282 lowerword == "stb") {
283 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600284 } else if (lowerword == "shift-image-bindings" || // synonyms
285 lowerword == "shift-image-binding" ||
286 lowerword == "sib") {
287 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600288 } else if (lowerword == "shift-ubo-bindings" || // synonyms
289 lowerword == "shift-ubo-binding" ||
290 lowerword == "sub") {
291 ProcessBindingBase(argc, argv, baseUboBinding);
292 } else if (lowerword == "auto-map-bindings" || // synonyms
293 lowerword == "auto-map-binding" ||
294 lowerword == "amb") {
295 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600296 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
297 lowerword == "flatten-uniform-array" ||
298 lowerword == "fua") {
299 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600300 } else if (lowerword == "no-storage-format" || // synonyms
301 lowerword == "nsf") {
302 Options |= EOptionNoStorageFormat;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600303 } else {
304 usage();
305 }
306 }
307 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000308 case 'H':
309 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600310 if ((Options & EOptionSpv) == 0) {
311 // default to Vulkan
312 Options |= EOptionSpv;
313 Options |= EOptionVulkanRules;
314 Options |= EOptionLinkProgram;
315 }
316 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000317 case 'V':
318 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600319 Options |= EOptionVulkanRules;
320 Options |= EOptionLinkProgram;
321 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400322 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400323 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400324 if (argc > 0) {
325 argc--;
326 argv++;
327 }
328 else
Dan Baker5afdd782016-08-11 17:53:57 -0400329 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400330 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600331 case 'G':
332 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000333 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600334 // undo a -H default to Vulkan
335 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000336 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000337 case 'E':
338 Options |= EOptionOutputPreprocessed;
339 break;
John Kessenich05a70632013-09-17 19:26:08 +0000340 case 'c':
341 Options |= EOptionDumpConfig;
342 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600343 case 'C':
344 Options |= EOptionCascadingErrors;
345 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000346 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000347 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000348 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700349 case 'D':
350 Options |= EOptionReadHlsl;
351 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700352 case 'e':
353 // HLSL todo: entry point handle needs much more sophistication.
354 // This is okay for one compilation unit with one entry point.
355 entryPointName = argv[1];
356 if (argc > 0) {
357 argc--;
358 argv++;
359 } else
360 Error("no <entry-point> provided for -e");
361 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600362 case 'h':
363 usage();
364 break;
John Kessenich05a70632013-09-17 19:26:08 +0000365 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000366 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000367 break;
368 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000369 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000370 break;
371 case 'm':
372 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000373 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600374 case 'o':
375 binaryFileName = argv[1];
376 if (argc > 0) {
377 argc--;
378 argv++;
379 } else
380 Error("no <file> provided for -o");
381 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000382 case 'q':
383 Options |= EOptionDumpReflection;
384 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000385 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000386 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000387 break;
388 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000389 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000390 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000391 case 't':
392 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000393 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000394 #endif
395 break;
John Kessenich319de232013-12-04 04:43:40 +0000396 case 'v':
397 Options |= EOptionDumpVersions;
398 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000399 case 'w':
400 Options |= EOptionSuppressWarnings;
401 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500402 case 'x':
403 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500404 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000405 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600406 usage();
407 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000408 }
John Kessenich38f3b892013-09-06 19:52:57 +0000409 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000410 std::string name(argv[0]);
411 if (! SetConfigFile(name)) {
412 Work[argc] = new glslang::TWorkItem(name);
413 Worklist.add(Work[argc]);
414 }
John Kessenich38f3b892013-09-06 19:52:57 +0000415 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000416 }
417
John Kessenich68d78fd2015-07-12 19:28:10 -0600418 // Make sure that -E is not specified alongside linking (which includes SPV generation)
419 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
420 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000421
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500422 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600423 if (binaryFileName && (Options & EOptionSpv) == 0)
424 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600425
426 if ((Options & EOptionFlattenUniformArrays) != 0 &&
427 (Options & EOptionReadHlsl) == 0)
428 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000429}
430
John Kessenich68d78fd2015-07-12 19:28:10 -0600431//
432// Translate the meaningful subset of command-line options to parser-behavior options.
433//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000434void SetMessageOptions(EShMessages& messages)
435{
436 if (Options & EOptionRelaxedErrors)
437 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
438 if (Options & EOptionIntermediate)
439 messages = (EShMessages)(messages | EShMsgAST);
440 if (Options & EOptionSuppressWarnings)
441 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600442 if (Options & EOptionSpv)
443 messages = (EShMessages)(messages | EShMsgSpvRules);
444 if (Options & EOptionVulkanRules)
445 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400446 if (Options & EOptionOutputPreprocessed)
447 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700448 if (Options & EOptionReadHlsl)
449 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600450 if (Options & EOptionCascadingErrors)
451 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000452}
453
John Kessenich68d78fd2015-07-12 19:28:10 -0600454//
John Kessenich69f4b512013-09-04 21:19:27 +0000455// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000456//
457// Return 0 for failure, 1 for success.
458//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800459unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000460{
John Kessenich38f3b892013-09-06 19:52:57 +0000461 glslang::TWorkItem* workItem;
462 while (Worklist.remove(workItem)) {
463 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000464 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000465 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000466
John Kessenichb0a7eb52013-11-07 17:44:20 +0000467 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000468
John Kessenich94a81fb2013-08-31 02:41:30 +0000469 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000470 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000471
472 ShDestruct(compiler);
473 }
474
475 return 0;
476}
477
John Kessenich6626cad2015-06-19 05:14:19 +0000478// Outputs the given string, but only if it is non-null and non-empty.
479// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400480void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000481{
482 if (str && str[0]) {
483 puts(str);
484 }
485}
486
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400487// Outputs the given string to stderr, but only if it is non-null and non-empty.
488// This prevents erroneous newlines from appearing.
489void StderrIfNonEmpty(const char* str)
490{
491 if (str && str[0]) {
492 fprintf(stderr, "%s\n", str);
493 }
494}
495
John Kessenichc57b2a92016-01-16 15:30:03 -0700496// Simple bundling of what makes a compilation unit for ease in passing around,
497// and separation of handling file IO versus API (programmatic) compilation.
498struct ShaderCompUnit {
499 EShLanguage stage;
500 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400501 char** text; // memory owned/managed externally
502 const char* fileNameList[1];
503
John Kessenich219b0252016-08-23 17:51:13 -0600504 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400505 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
506 {
507 stage = istage;
508 fileName = ifileName;
509 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600510 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400511 }
512
513 ShaderCompUnit(const ShaderCompUnit &rhs)
514 {
515 stage = rhs.stage;
516 fileName = rhs.fileName;
517 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600518 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400519 }
520
John Kessenichc57b2a92016-01-16 15:30:03 -0700521};
522
John Kessenich69f4b512013-09-04 21:19:27 +0000523//
John Kessenichc57b2a92016-01-16 15:30:03 -0700524// For linking mode: Will independently parse each compilation unit, but then put them
525// in the same program and link them together, making at most one linked module per
526// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000527//
528// Uses the new C++ interface instead of the old handle-based interface.
529//
John Kessenichc57b2a92016-01-16 15:30:03 -0700530
531void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000532{
533 // keep track of what to free
534 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700535
John Kessenich69f4b512013-09-04 21:19:27 +0000536 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000537 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000538
John Kessenich69f4b512013-09-04 21:19:27 +0000539 //
540 // Per-shader processing...
541 //
542
John Kessenich5b0f13a2013-11-01 03:08:40 +0000543 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100544 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
545 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700546 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400547 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700548 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
549 shader->setEntryPoint(entryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600550
551 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
552 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600553 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600554 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600555 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600556 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600557
558 if (Options & EOptionAutoMapBindings)
559 shader->setAutoMapBindings(true);
560
John Kessenich69f4b512013-09-04 21:19:27 +0000561 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600562
John Kessenichc555ddd2015-06-17 02:38:44 +0000563 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000564
John Kessenichc555ddd2015-06-17 02:38:44 +0000565 if (Options & EOptionOutputPreprocessed) {
566 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500567 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400568 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500569 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400570 PutsIfNonEmpty(str.c_str());
571 } else {
572 CompileFailed = true;
573 }
574 StderrIfNonEmpty(shader->getInfoLog());
575 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000576 continue;
577 }
578 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000579 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000580
John Kessenich69f4b512013-09-04 21:19:27 +0000581 program.addShader(shader);
582
John Kessenichc57b2a92016-01-16 15:30:03 -0700583 if (! (Options & EOptionSuppressInfolog) &&
584 ! (Options & EOptionMemoryLeakMode)) {
585 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400586 PutsIfNonEmpty(shader->getInfoLog());
587 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000588 }
John Kessenich69f4b512013-09-04 21:19:27 +0000589 }
590
591 //
592 // Program-level processing...
593 //
594
John Kessenich4d65ee32016-03-12 18:17:47 -0700595 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600596 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000597 LinkFailed = true;
598
steve-lunarg9ae34742016-10-05 13:40:13 -0600599 // Map IO
600 if (Options & EOptionSpv) {
601 if (!program.mapIO())
602 LinkFailed = true;
603 }
604
John Kessenich4d65ee32016-03-12 18:17:47 -0700605 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700606 if (! (Options & EOptionSuppressInfolog) &&
607 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400608 PutsIfNonEmpty(program.getInfoLog());
609 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000610 }
611
John Kessenich4d65ee32016-03-12 18:17:47 -0700612 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000613 if (Options & EOptionDumpReflection) {
614 program.buildReflection();
615 program.dumpReflection();
616 }
617
John Kessenich4d65ee32016-03-12 18:17:47 -0700618 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000619 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000620 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600621 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000622 else {
623 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000624 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000625 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400626 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400627 spv::SpvBuildLogger logger;
628 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700629
630 // Dump the spv to a file or stdout, etc., but only if not doing
631 // memory/perf testing, as it's not internal to programmatic use.
632 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500633 printf("%s", logger.getAllMessages().c_str());
634 if (Options & EOptionOutputHexadecimal) {
635 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
636 } else {
637 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
638 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700639 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700640 spv::Disassemble(std::cout, spirv);
641 }
John Kessenichacba7722015-03-04 03:48:38 +0000642 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000643 }
John Kessenich92f90382014-07-28 04:21:04 +0000644 }
645 }
646 }
647
John Kessenich5b0f13a2013-11-01 03:08:40 +0000648 // Free everything up, program has to go before the shaders
649 // because it might have merged stuff from the shaders, and
650 // the stuff from the shaders has to have its destructors called
651 // before the pools holding the memory in the shaders is freed.
652 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000653 while (shaders.size() > 0) {
654 delete shaders.back();
655 shaders.pop_back();
656 }
John Kessenich69f4b512013-09-04 21:19:27 +0000657}
658
John Kessenichc57b2a92016-01-16 15:30:03 -0700659//
660// Do file IO part of compile and link, handing off the pure
661// API/programmatic mode to CompileAndLinkShaderUnits(), which can
662// be put in a loop for testing memory footprint and performance.
663//
664// This is just for linking mode: meaning all the shaders will be put into the
665// the same program linked together.
666//
667// This means there are a limited number of work items (not multi-threading mode)
668// and that the point is testing at the linking level. Hence, to enable
669// performance and memory testing, the actual compile/link can be put in
670// a loop, independent of processing the work items and file IO.
671//
672void CompileAndLinkShaderFiles()
673{
674 std::vector<ShaderCompUnit> compUnits;
675
676 // Transfer all the work items from to a simple list of
677 // of compilation units. (We don't care about the thread
678 // work-item distribution properties in this path, which
679 // is okay due to the limited number of shaders, know since
680 // they are all getting linked together.)
681 glslang::TWorkItem* workItem;
682 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200683 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700684 FindLanguage(workItem->name),
685 workItem->name,
686 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200687 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700688
689 if (! compUnit.text) {
690 usage();
691 return;
692 }
693
694 compUnits.push_back(compUnit);
695 }
696
697 // Actual call to programmatic processing of compile and link,
698 // in a loop for testing memory and performance. This part contains
699 // all the perf/memory that a programmatic consumer will care about.
700 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
701 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
702 CompileAndLinkShaderUnits(compUnits);
703
704 if (Options & EOptionMemoryLeakMode)
705 glslang::OS_DumpMemoryCounters();
706 }
707
rdb32084e82016-02-23 22:17:38 +0100708 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
709 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700710}
711
John Kessenicha0af4732012-12-12 21:15:54 +0000712int C_DECL main(int argc, char* argv[])
713{
John Kessenich68d78fd2015-07-12 19:28:10 -0600714 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000715
John Kessenich05a70632013-09-17 19:26:08 +0000716 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500717 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000718 if (Worklist.empty())
719 return ESuccess;
720 }
721
John Kessenich0da9eaa2015-08-01 17:10:02 -0600722 if (Options & EOptionDumpVersions) {
723 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000724 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
725 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600726 std::string spirvVersion;
727 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600728 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600729 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700730 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600731 printf("GL_KHR_vulkan_glsl version %d\n", 100);
732 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000733 if (Worklist.empty())
734 return ESuccess;
735 }
736
John Kessenich05a70632013-09-17 19:26:08 +0000737 if (Worklist.empty()) {
738 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000739 }
740
741 ProcessConfigFile();
742
John Kessenich69f4b512013-09-04 21:19:27 +0000743 //
744 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000745 // 1) linking all arguments together, single-threaded, new C++ interface
746 // 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 +0000747 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000748 if (Options & EOptionLinkProgram ||
749 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000750 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700751 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000752 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500753 for (int w = 0; w < NumWorkItems; ++w) {
754 if (Work[w]) {
755 delete Work[w];
756 }
757 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000758 } else {
759 ShInitialize();
760
John Kessenich38f3b892013-09-06 19:52:57 +0000761 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000762
John Kessenich38f3b892013-09-06 19:52:57 +0000763 if (Options & EOptionMultiThreaded) {
764 const int NumThreads = 16;
765 void* threads[NumThreads];
766 for (int t = 0; t < NumThreads; ++t) {
767 threads[t] = glslang::OS_CreateThread(&CompileShaders);
768 if (! threads[t]) {
769 printf("Failed to create thread\n");
770 return EFailThreadCreate;
771 }
John Kessenicha0af4732012-12-12 21:15:54 +0000772 }
John Kessenich38f3b892013-09-06 19:52:57 +0000773 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000774 } else
775 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000776
777 // Print out all the resulting infologs
778 for (int w = 0; w < NumWorkItems; ++w) {
779 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600780 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400781 PutsIfNonEmpty(Work[w]->name.c_str());
782 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000783 delete Work[w];
784 }
785 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000786
787 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000788 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000789
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500790 delete[] Work;
791
John Kessenichc999ba22013-11-07 23:33:24 +0000792 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000793 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000794 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000795 return EFailLink;
796
797 return 0;
798}
799
800//
801// Deduce the language from the filename. Files must end in one of the
802// following extensions:
803//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000804// .vert = vertex
805// .tesc = tessellation control
806// .tese = tessellation evaluation
807// .geom = geometry
808// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000809// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000810//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600811EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000812{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600813 size_t ext = 0;
814
815 // Search for a suffix on a filename: e.g, "myfile.frag". If given
816 // the suffix directly, we skip looking the '.'
817 if (parseSuffix) {
818 ext = name.rfind('.');
819 if (ext == std::string::npos) {
820 usage();
821 return EShLangVertex;
822 }
823 ++ext;
John Kessenicha0af4732012-12-12 21:15:54 +0000824 }
825
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600826 std::string suffix = name.substr(ext, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400827 if (shaderStageName)
828 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400829
John Kessenich2b07c7e2013-07-31 18:44:13 +0000830 if (suffix == "vert")
831 return EShLangVertex;
832 else if (suffix == "tesc")
833 return EShLangTessControl;
834 else if (suffix == "tese")
835 return EShLangTessEvaluation;
836 else if (suffix == "geom")
837 return EShLangGeometry;
838 else if (suffix == "frag")
839 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000840 else if (suffix == "comp")
841 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000842
843 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000844 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000845}
846
John Kessenicha0af4732012-12-12 21:15:54 +0000847//
John Kessenich69f4b512013-09-04 21:19:27 +0000848// Read a file's data into a string, and compile it using the old interface ShCompile,
849// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000850//
John Kessenich51cdd902014-02-18 23:37:57 +0000851void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000852{
John Kessenichca3457f2015-05-18 01:59:45 +0000853 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000854 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000855 if (! shaderStrings) {
856 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000857 }
858
John Kessenich41cf6b52013-06-25 18:10:05 +0000859 int* lengths = new int[NumShaderStrings];
860
861 // move to length-based strings, rather than null-terminated strings
862 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000863 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000864
John Kessenichc999ba22013-11-07 23:33:24 +0000865 if (! shaderStrings) {
866 CompileFailed = true;
867 return;
868 }
John Kessenicha0af4732012-12-12 21:15:54 +0000869
John Kessenich52ac67e2013-05-05 23:46:22 +0000870 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000871 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000872
John Kessenich94a81fb2013-08-31 02:41:30 +0000873 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
874 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000875 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000876 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000877 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
878 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
879 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000880 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000881 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000882 }
John Kessenicha0af4732012-12-12 21:15:54 +0000883
John Kessenich94a81fb2013-08-31 02:41:30 +0000884 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000885 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000886 }
John Kessenicha0af4732012-12-12 21:15:54 +0000887
John Kessenich41cf6b52013-06-25 18:10:05 +0000888 delete [] lengths;
889 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000890
John Kessenichc999ba22013-11-07 23:33:24 +0000891 if (ret == 0)
892 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000893}
894
John Kessenicha0af4732012-12-12 21:15:54 +0000895//
896// print usage to stdout
897//
898void usage()
899{
John Kessenich319de232013-12-04 04:43:40 +0000900 printf("Usage: glslangValidator [option]... [file]...\n"
901 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000902 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600903 " .conf to provide an optional config file that replaces the default configuration\n"
904 " (see -c option below for generating a template)\n"
905 " .vert for a vertex shader\n"
906 " .tesc for a tessellation control shader\n"
907 " .tese for a tessellation evaluation shader\n"
908 " .geom for a geometry shader\n"
909 " .frag for a fragment shader\n"
910 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000911 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000912 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000913 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000914 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600915 "Each option must be specified separately.\n"
916 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
917 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600918 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
919 " default file name is <stage>.spv (-o overrides this)\n"
920 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400921 " -E print pre-processed GLSL; cannot be used with -l;\n"
922 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400923 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
Dan Baker895275e2016-08-11 14:55:49 -0400924 " valid choices are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600925 " -c configuration dump;\n"
926 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600927 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600928 " -d default to desktop (#version 110) when there is no shader #version\n"
929 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700930 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700931 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600932 " -h print this usage message\n"
933 " -i intermediate tree (glslang AST) is printed out\n"
934 " -l link all input files together to form a single module\n"
935 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500936 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600937 " -q dump reflection query database\n"
938 " -r relaxed semantic error-checking mode\n"
939 " -s silent mode\n"
940 " -t multi-threaded mode\n"
941 " -v print version strings\n"
942 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500943 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600944 "\n"
945 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
946 " --ssb [stage] num synonym for --shift-sampler-binding\n"
947 "\n"
948 " --shift-texture-binding [stage] num set base binding number for textures\n"
949 " --stb [stage] num synonym for --shift-texture-binding\n"
950 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -0600951 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
952 " --sib [stage] num synonym for --shift-image-binding\n"
953 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600954 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
955 " --sub [stage] num synonym for --shift-UBO-binding\n"
956 "\n"
957 " --auto-map-bindings automatically bind uniform variables without\n"
958 " explicit bindings.\n"
959 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600960 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -0600961 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600962 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -0600963 "\n"
964 " --no-storage-format use Unknown image format\n"
965 " --nsf synonym for --no-storage-format\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000966 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600967
968 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000969}
970
John Kessenich3ce4e592014-10-06 19:57:34 +0000971#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000972
973#include <errno.h>
974
975int fopen_s(
976 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000977 const char* filename,
978 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000979)
980{
981 if (!pFile || !filename || !mode) {
982 return EINVAL;
983 }
984
985 FILE* f = fopen(filename, mode);
986 if (! f) {
987 if (errno != 0) {
988 return errno;
989 } else {
990 return ENOENT;
991 }
992 }
993 *pFile = f;
994
995 return 0;
996}
997
998#endif
999
John Kessenicha0af4732012-12-12 21:15:54 +00001000//
1001// Malloc a string of sufficient size and read a string into it.
1002//
John Kessenich51cdd902014-02-18 23:37:57 +00001003char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001004{
John Kessenichb3297152015-07-11 18:01:03 -06001005 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001006 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001007
John Kessenicha0af4732012-12-12 21:15:54 +00001008 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001009 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1010 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001011
John Kessenich68d78fd2015-07-12 19:28:10 -06001012 if (errorCode || in == nullptr)
1013 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001014
1015 while (fgetc(in) != EOF)
1016 count++;
1017
John Kessenichd6c72a42014-08-18 19:42:35 +00001018 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001019
John Kessenichb3297152015-07-11 18:01:03 -06001020 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001021 if (! fdata)
1022 Error("can't allocate memory");
1023
John Kessenichb3297152015-07-11 18:01:03 -06001024 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001025 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001026 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001027 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001028
John Kessenicha0af4732012-12-12 21:15:54 +00001029 fdata[count] = '\0';
1030 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001031
John Kessenichea869fb2013-10-28 18:12:06 +00001032 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001033 // recover from empty file
1034 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001035 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001036 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001037 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001038
John Kessenichb3297152015-07-11 18:01:03 -06001039 return return_data;
1040 } else
1041 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1042
1043 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001044 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001045 int ptr_len = 0;
1046 int i = 0;
1047 while (count > 0) {
1048 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1049 memcpy(return_data[i], fdata + ptr_len, len);
1050 return_data[i][len] = '\0';
1051 count -= len;
1052 ptr_len += len;
1053 if (count < len) {
1054 if (count == 0) {
1055 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001056 break;
1057 }
John Kessenichb3297152015-07-11 18:01:03 -06001058 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +00001059 }
1060 ++i;
1061 }
John Kessenichb3297152015-07-11 18:01:03 -06001062
1063 free(fdata);
1064
John Kessenicha0af4732012-12-12 21:15:54 +00001065 return return_data;
1066}
1067
John Kessenich51cdd902014-02-18 23:37:57 +00001068void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001069{
John Kessenichb3297152015-07-11 18:01:03 -06001070 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001071 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001072
1073 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001074}
1075
John Kessenich54d8cda2013-02-11 22:36:01 +00001076void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001077{
John Kessenichfae38ee2015-06-10 23:23:12 +00001078 if (num >= 0 )
1079 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1080 else
1081 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001082}