blob: 30af4fe44abcd429dc524f046f1113284df707b8 [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),
John Kessenich94a81fb2013-08-31 02:41:30 +000085};
86
John Kessenicha0af4732012-12-12 21:15:54 +000087//
John Kessenich68d78fd2015-07-12 19:28:10 -060088// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000089//
90enum TFailCode {
91 ESuccess = 0,
92 EFailUsage,
93 EFailCompile,
94 EFailLink,
95 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000096 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000097 EFailLinkerCreate
98};
99
100//
John Kessenich68d78fd2015-07-12 19:28:10 -0600101// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000102//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600103EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000104void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000105void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000106void FreeFileData(char** data);
107char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000108void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000109
John Kessenichc999ba22013-11-07 23:33:24 +0000110// Globally track if any compile or link failure.
111bool CompileFailed = false;
112bool LinkFailed = false;
113
John Kessenich05a70632013-09-17 19:26:08 +0000114// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600115// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000116int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000117
John Kessenich05a70632013-09-17 19:26:08 +0000118TBuiltInResource Resources;
119std::string ConfigFile;
120
John Kessenicha0af4732012-12-12 21:15:54 +0000121//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600122// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000123//
124void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000125{
John Kessenich05a70632013-09-17 19:26:08 +0000126 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000127 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000128 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000129 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000130 if (configStrings)
131 config = *configStrings;
132 else {
133 printf("Error opening configuration file; will instead use the default configuration\n");
134 usage();
135 }
136 }
137
138 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500139 Resources = glslang::DefaultTBuiltInResource;
140 return;
John Kessenich05a70632013-09-17 19:26:08 +0000141 }
142
Lei Zhang1b141722016-05-19 13:50:49 -0400143 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000144
John Kessenich05a70632013-09-17 19:26:08 +0000145 if (configStrings)
146 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500147 else
148 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000149}
150
John Kessenich38f3b892013-09-06 19:52:57 +0000151// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000152glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000153
154// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000155glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000156int NumWorkItems = 0;
157
John Kessenich94a81fb2013-08-31 02:41:30 +0000158int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600159const char* ExecutableName = nullptr;
160const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700161const char* entryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400162const char* shaderStageName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600163
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600164std::array<unsigned int, EShLangCount> baseSamplerBinding;
165std::array<unsigned int, EShLangCount> baseTextureBinding;
166std::array<unsigned int, EShLangCount> baseUboBinding;
167
John Kessenich68d78fd2015-07-12 19:28:10 -0600168//
169// Create the default name for saving a binary if -o is not provided.
170//
171const char* GetBinaryName(EShLanguage stage)
172{
173 const char* name;
174 if (binaryFileName == nullptr) {
175 switch (stage) {
176 case EShLangVertex: name = "vert.spv"; break;
177 case EShLangTessControl: name = "tesc.spv"; break;
178 case EShLangTessEvaluation: name = "tese.spv"; break;
179 case EShLangGeometry: name = "geom.spv"; break;
180 case EShLangFragment: name = "frag.spv"; break;
181 case EShLangCompute: name = "comp.spv"; break;
182 default: name = "unknown"; break;
183 }
184 } else
185 name = binaryFileName;
186
187 return name;
188}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000189
John Kessenich05a70632013-09-17 19:26:08 +0000190//
191// *.conf => this is a config file that can set limits/resources
192//
193bool SetConfigFile(const std::string& name)
194{
195 if (name.size() < 5)
196 return false;
197
John Kessenich4c706852013-10-11 16:28:43 +0000198 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000199 ConfigFile = name;
200 return true;
201 }
202
203 return false;
204}
205
John Kessenich68d78fd2015-07-12 19:28:10 -0600206//
207// Give error and exit with failure code.
208//
209void Error(const char* message)
210{
211 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
212 exit(EFailUsage);
213}
214
215//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600216// Process an optional binding base of the form:
217// --argname [stage] base
218// Where stage is one of the forms accepted by FindLanguage, and base is an integer
219//
220void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
221{
222 if (argc < 2)
223 usage();
224
225 if (!isdigit(argv[1][0])) {
226 if (argc < 3) // this form needs one more argument
227 usage();
228
229 // Parse form: --argname stage base
230 const EShLanguage lang = FindLanguage(argv[1], false);
231 base[lang] = atoi(argv[2]);
232 argc-= 2;
233 argv+= 2;
234 } else {
235 // Parse form: --argname base
236 for (int lang=0; lang<EShLangCount; ++lang)
237 base[lang] = atoi(argv[1]);
238
239 argc--;
240 argv++;
241 }
242}
243
244//
John Kessenich68d78fd2015-07-12 19:28:10 -0600245// Do all command-line argument parsing. This includes building up the work-items
246// to be processed later, and saving all the command-line options.
247//
248// Does not return (it exits) if command-line is fatally flawed.
249//
250void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000251{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600252 baseSamplerBinding.fill(0);
253 baseTextureBinding.fill(0);
254 baseUboBinding.fill(0);
255
John Kessenich38f3b892013-09-06 19:52:57 +0000256 ExecutableName = argv[0];
257 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 +0000258 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600259 for (int w = 0; w < NumWorkItems; ++w)
260 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000261
John Kessenich2b07c7e2013-07-31 18:44:13 +0000262 argc--;
263 argv++;
264 for (; argc >= 1; argc--, argv++) {
265 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000266 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600267 case '-':
268 {
269 std::string lowerword(argv[0]+2);
270 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
271
272 // handle --word style options
273 if (lowerword == "shift-sampler-bindings" || // synonyms
274 lowerword == "shift-sampler-binding" ||
275 lowerword == "ssb") {
276 ProcessBindingBase(argc, argv, baseSamplerBinding);
277 } else if (lowerword == "shift-texture-bindings" || // synonyms
278 lowerword == "shift-texture-binding" ||
279 lowerword == "stb") {
280 ProcessBindingBase(argc, argv, baseTextureBinding);
281 } else if (lowerword == "shift-ubo-bindings" || // synonyms
282 lowerword == "shift-ubo-binding" ||
283 lowerword == "sub") {
284 ProcessBindingBase(argc, argv, baseUboBinding);
285 } else if (lowerword == "auto-map-bindings" || // synonyms
286 lowerword == "auto-map-binding" ||
287 lowerword == "amb") {
288 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600289 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
290 lowerword == "flatten-uniform-array" ||
291 lowerword == "fua") {
292 Options |= EOptionFlattenUniformArrays;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600293 } else {
294 usage();
295 }
296 }
297 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000298 case 'H':
299 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600300 if ((Options & EOptionSpv) == 0) {
301 // default to Vulkan
302 Options |= EOptionSpv;
303 Options |= EOptionVulkanRules;
304 Options |= EOptionLinkProgram;
305 }
306 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000307 case 'V':
308 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600309 Options |= EOptionVulkanRules;
310 Options |= EOptionLinkProgram;
311 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400312 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400313 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400314 if (argc > 0) {
315 argc--;
316 argv++;
317 }
318 else
Dan Baker5afdd782016-08-11 17:53:57 -0400319 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400320 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600321 case 'G':
322 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000323 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600324 // undo a -H default to Vulkan
325 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000326 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000327 case 'E':
328 Options |= EOptionOutputPreprocessed;
329 break;
John Kessenich05a70632013-09-17 19:26:08 +0000330 case 'c':
331 Options |= EOptionDumpConfig;
332 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600333 case 'C':
334 Options |= EOptionCascadingErrors;
335 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000336 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000337 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000338 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700339 case 'D':
340 Options |= EOptionReadHlsl;
341 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700342 case 'e':
343 // HLSL todo: entry point handle needs much more sophistication.
344 // This is okay for one compilation unit with one entry point.
345 entryPointName = argv[1];
346 if (argc > 0) {
347 argc--;
348 argv++;
349 } else
350 Error("no <entry-point> provided for -e");
351 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600352 case 'h':
353 usage();
354 break;
John Kessenich05a70632013-09-17 19:26:08 +0000355 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000356 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000357 break;
358 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000359 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000360 break;
361 case 'm':
362 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000363 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600364 case 'o':
365 binaryFileName = argv[1];
366 if (argc > 0) {
367 argc--;
368 argv++;
369 } else
370 Error("no <file> provided for -o");
371 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000372 case 'q':
373 Options |= EOptionDumpReflection;
374 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000375 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000376 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000377 break;
378 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000379 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000380 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000381 case 't':
382 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000383 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000384 #endif
385 break;
John Kessenich319de232013-12-04 04:43:40 +0000386 case 'v':
387 Options |= EOptionDumpVersions;
388 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000389 case 'w':
390 Options |= EOptionSuppressWarnings;
391 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500392 case 'x':
393 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500394 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000395 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600396 usage();
397 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000398 }
John Kessenich38f3b892013-09-06 19:52:57 +0000399 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000400 std::string name(argv[0]);
401 if (! SetConfigFile(name)) {
402 Work[argc] = new glslang::TWorkItem(name);
403 Worklist.add(Work[argc]);
404 }
John Kessenich38f3b892013-09-06 19:52:57 +0000405 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000406 }
407
John Kessenich68d78fd2015-07-12 19:28:10 -0600408 // Make sure that -E is not specified alongside linking (which includes SPV generation)
409 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
410 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000411
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500412 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600413 if (binaryFileName && (Options & EOptionSpv) == 0)
414 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600415
416 if ((Options & EOptionFlattenUniformArrays) != 0 &&
417 (Options & EOptionReadHlsl) == 0)
418 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000419}
420
John Kessenich68d78fd2015-07-12 19:28:10 -0600421//
422// Translate the meaningful subset of command-line options to parser-behavior options.
423//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000424void SetMessageOptions(EShMessages& messages)
425{
426 if (Options & EOptionRelaxedErrors)
427 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
428 if (Options & EOptionIntermediate)
429 messages = (EShMessages)(messages | EShMsgAST);
430 if (Options & EOptionSuppressWarnings)
431 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600432 if (Options & EOptionSpv)
433 messages = (EShMessages)(messages | EShMsgSpvRules);
434 if (Options & EOptionVulkanRules)
435 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400436 if (Options & EOptionOutputPreprocessed)
437 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700438 if (Options & EOptionReadHlsl)
439 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600440 if (Options & EOptionCascadingErrors)
441 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000442}
443
John Kessenich68d78fd2015-07-12 19:28:10 -0600444//
John Kessenich69f4b512013-09-04 21:19:27 +0000445// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000446//
447// Return 0 for failure, 1 for success.
448//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800449unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000450{
John Kessenich38f3b892013-09-06 19:52:57 +0000451 glslang::TWorkItem* workItem;
452 while (Worklist.remove(workItem)) {
453 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000454 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000455 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000456
John Kessenichb0a7eb52013-11-07 17:44:20 +0000457 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000458
John Kessenich94a81fb2013-08-31 02:41:30 +0000459 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000460 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000461
462 ShDestruct(compiler);
463 }
464
465 return 0;
466}
467
John Kessenich6626cad2015-06-19 05:14:19 +0000468// Outputs the given string, but only if it is non-null and non-empty.
469// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400470void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000471{
472 if (str && str[0]) {
473 puts(str);
474 }
475}
476
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400477// Outputs the given string to stderr, but only if it is non-null and non-empty.
478// This prevents erroneous newlines from appearing.
479void StderrIfNonEmpty(const char* str)
480{
481 if (str && str[0]) {
482 fprintf(stderr, "%s\n", str);
483 }
484}
485
John Kessenichc57b2a92016-01-16 15:30:03 -0700486// Simple bundling of what makes a compilation unit for ease in passing around,
487// and separation of handling file IO versus API (programmatic) compilation.
488struct ShaderCompUnit {
489 EShLanguage stage;
490 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400491 char** text; // memory owned/managed externally
492 const char* fileNameList[1];
493
John Kessenich219b0252016-08-23 17:51:13 -0600494 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400495 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
496 {
497 stage = istage;
498 fileName = ifileName;
499 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600500 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400501 }
502
503 ShaderCompUnit(const ShaderCompUnit &rhs)
504 {
505 stage = rhs.stage;
506 fileName = rhs.fileName;
507 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600508 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400509 }
510
John Kessenichc57b2a92016-01-16 15:30:03 -0700511};
512
John Kessenich69f4b512013-09-04 21:19:27 +0000513//
John Kessenichc57b2a92016-01-16 15:30:03 -0700514// For linking mode: Will independently parse each compilation unit, but then put them
515// in the same program and link them together, making at most one linked module per
516// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000517//
518// Uses the new C++ interface instead of the old handle-based interface.
519//
John Kessenichc57b2a92016-01-16 15:30:03 -0700520
521void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000522{
523 // keep track of what to free
524 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700525
John Kessenich69f4b512013-09-04 21:19:27 +0000526 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000527 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000528
John Kessenich69f4b512013-09-04 21:19:27 +0000529 //
530 // Per-shader processing...
531 //
532
John Kessenich5b0f13a2013-11-01 03:08:40 +0000533 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100534 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
535 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700536 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400537 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700538 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
539 shader->setEntryPoint(entryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600540
541 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
542 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
543 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600544 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600545
546 if (Options & EOptionAutoMapBindings)
547 shader->setAutoMapBindings(true);
548
John Kessenich69f4b512013-09-04 21:19:27 +0000549 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600550
John Kessenichc555ddd2015-06-17 02:38:44 +0000551 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000552
John Kessenichc555ddd2015-06-17 02:38:44 +0000553 if (Options & EOptionOutputPreprocessed) {
554 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500555 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400556 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500557 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400558 PutsIfNonEmpty(str.c_str());
559 } else {
560 CompileFailed = true;
561 }
562 StderrIfNonEmpty(shader->getInfoLog());
563 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000564 continue;
565 }
566 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000567 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000568
John Kessenich69f4b512013-09-04 21:19:27 +0000569 program.addShader(shader);
570
John Kessenichc57b2a92016-01-16 15:30:03 -0700571 if (! (Options & EOptionSuppressInfolog) &&
572 ! (Options & EOptionMemoryLeakMode)) {
573 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400574 PutsIfNonEmpty(shader->getInfoLog());
575 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000576 }
John Kessenich69f4b512013-09-04 21:19:27 +0000577 }
578
579 //
580 // Program-level processing...
581 //
582
John Kessenich4d65ee32016-03-12 18:17:47 -0700583 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600584 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000585 LinkFailed = true;
586
John Kessenich4d65ee32016-03-12 18:17:47 -0700587 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700588 if (! (Options & EOptionSuppressInfolog) &&
589 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400590 PutsIfNonEmpty(program.getInfoLog());
591 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000592 }
593
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600594 // Map IO
595 if (Options & EOptionSpv)
596 program.mapIO();
597
John Kessenich4d65ee32016-03-12 18:17:47 -0700598 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000599 if (Options & EOptionDumpReflection) {
600 program.buildReflection();
601 program.dumpReflection();
602 }
603
John Kessenich4d65ee32016-03-12 18:17:47 -0700604 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000605 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000606 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600607 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000608 else {
609 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000610 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000611 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400612 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400613 spv::SpvBuildLogger logger;
614 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700615
616 // Dump the spv to a file or stdout, etc., but only if not doing
617 // memory/perf testing, as it's not internal to programmatic use.
618 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500619 printf("%s", logger.getAllMessages().c_str());
620 if (Options & EOptionOutputHexadecimal) {
621 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
622 } else {
623 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
624 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700625 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700626 spv::Disassemble(std::cout, spirv);
627 }
John Kessenichacba7722015-03-04 03:48:38 +0000628 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000629 }
John Kessenich92f90382014-07-28 04:21:04 +0000630 }
631 }
632 }
633
John Kessenich5b0f13a2013-11-01 03:08:40 +0000634 // Free everything up, program has to go before the shaders
635 // because it might have merged stuff from the shaders, and
636 // the stuff from the shaders has to have its destructors called
637 // before the pools holding the memory in the shaders is freed.
638 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000639 while (shaders.size() > 0) {
640 delete shaders.back();
641 shaders.pop_back();
642 }
John Kessenich69f4b512013-09-04 21:19:27 +0000643}
644
John Kessenichc57b2a92016-01-16 15:30:03 -0700645//
646// Do file IO part of compile and link, handing off the pure
647// API/programmatic mode to CompileAndLinkShaderUnits(), which can
648// be put in a loop for testing memory footprint and performance.
649//
650// This is just for linking mode: meaning all the shaders will be put into the
651// the same program linked together.
652//
653// This means there are a limited number of work items (not multi-threading mode)
654// and that the point is testing at the linking level. Hence, to enable
655// performance and memory testing, the actual compile/link can be put in
656// a loop, independent of processing the work items and file IO.
657//
658void CompileAndLinkShaderFiles()
659{
660 std::vector<ShaderCompUnit> compUnits;
661
662 // Transfer all the work items from to a simple list of
663 // of compilation units. (We don't care about the thread
664 // work-item distribution properties in this path, which
665 // is okay due to the limited number of shaders, know since
666 // they are all getting linked together.)
667 glslang::TWorkItem* workItem;
668 while (Worklist.remove(workItem)) {
669 ShaderCompUnit compUnit = {
670 FindLanguage(workItem->name),
671 workItem->name,
672 ReadFileData(workItem->name.c_str())
673 };
674
675 if (! compUnit.text) {
676 usage();
677 return;
678 }
679
680 compUnits.push_back(compUnit);
681 }
682
683 // Actual call to programmatic processing of compile and link,
684 // in a loop for testing memory and performance. This part contains
685 // all the perf/memory that a programmatic consumer will care about.
686 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
687 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
688 CompileAndLinkShaderUnits(compUnits);
689
690 if (Options & EOptionMemoryLeakMode)
691 glslang::OS_DumpMemoryCounters();
692 }
693
rdb32084e82016-02-23 22:17:38 +0100694 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
695 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700696}
697
John Kessenicha0af4732012-12-12 21:15:54 +0000698int C_DECL main(int argc, char* argv[])
699{
John Kessenich68d78fd2015-07-12 19:28:10 -0600700 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000701
John Kessenich05a70632013-09-17 19:26:08 +0000702 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500703 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000704 if (Worklist.empty())
705 return ESuccess;
706 }
707
John Kessenich0da9eaa2015-08-01 17:10:02 -0600708 if (Options & EOptionDumpVersions) {
709 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000710 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
711 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600712 std::string spirvVersion;
713 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600714 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600715 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700716 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600717 printf("GL_KHR_vulkan_glsl version %d\n", 100);
718 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000719 if (Worklist.empty())
720 return ESuccess;
721 }
722
John Kessenich05a70632013-09-17 19:26:08 +0000723 if (Worklist.empty()) {
724 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000725 }
726
727 ProcessConfigFile();
728
John Kessenich69f4b512013-09-04 21:19:27 +0000729 //
730 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000731 // 1) linking all arguments together, single-threaded, new C++ interface
732 // 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 +0000733 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000734 if (Options & EOptionLinkProgram ||
735 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000736 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700737 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000738 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500739 for (int w = 0; w < NumWorkItems; ++w) {
740 if (Work[w]) {
741 delete Work[w];
742 }
743 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000744 } else {
745 ShInitialize();
746
John Kessenich38f3b892013-09-06 19:52:57 +0000747 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000748
John Kessenich38f3b892013-09-06 19:52:57 +0000749 if (Options & EOptionMultiThreaded) {
750 const int NumThreads = 16;
751 void* threads[NumThreads];
752 for (int t = 0; t < NumThreads; ++t) {
753 threads[t] = glslang::OS_CreateThread(&CompileShaders);
754 if (! threads[t]) {
755 printf("Failed to create thread\n");
756 return EFailThreadCreate;
757 }
John Kessenicha0af4732012-12-12 21:15:54 +0000758 }
John Kessenich38f3b892013-09-06 19:52:57 +0000759 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000760 } else
761 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000762
763 // Print out all the resulting infologs
764 for (int w = 0; w < NumWorkItems; ++w) {
765 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600766 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400767 PutsIfNonEmpty(Work[w]->name.c_str());
768 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000769 delete Work[w];
770 }
771 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000772
773 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000774 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000775
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500776 delete[] Work;
777
John Kessenichc999ba22013-11-07 23:33:24 +0000778 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000779 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000780 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000781 return EFailLink;
782
783 return 0;
784}
785
786//
787// Deduce the language from the filename. Files must end in one of the
788// following extensions:
789//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000790// .vert = vertex
791// .tesc = tessellation control
792// .tese = tessellation evaluation
793// .geom = geometry
794// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000795// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000796//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600797EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000798{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600799 size_t ext = 0;
800
801 // Search for a suffix on a filename: e.g, "myfile.frag". If given
802 // the suffix directly, we skip looking the '.'
803 if (parseSuffix) {
804 ext = name.rfind('.');
805 if (ext == std::string::npos) {
806 usage();
807 return EShLangVertex;
808 }
809 ++ext;
John Kessenicha0af4732012-12-12 21:15:54 +0000810 }
811
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600812 std::string suffix = name.substr(ext, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400813 if (shaderStageName)
814 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400815
John Kessenich2b07c7e2013-07-31 18:44:13 +0000816 if (suffix == "vert")
817 return EShLangVertex;
818 else if (suffix == "tesc")
819 return EShLangTessControl;
820 else if (suffix == "tese")
821 return EShLangTessEvaluation;
822 else if (suffix == "geom")
823 return EShLangGeometry;
824 else if (suffix == "frag")
825 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000826 else if (suffix == "comp")
827 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000828
829 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000830 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000831}
832
John Kessenicha0af4732012-12-12 21:15:54 +0000833//
John Kessenich69f4b512013-09-04 21:19:27 +0000834// Read a file's data into a string, and compile it using the old interface ShCompile,
835// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000836//
John Kessenich51cdd902014-02-18 23:37:57 +0000837void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000838{
John Kessenichca3457f2015-05-18 01:59:45 +0000839 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000840 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000841 if (! shaderStrings) {
842 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000843 }
844
John Kessenich41cf6b52013-06-25 18:10:05 +0000845 int* lengths = new int[NumShaderStrings];
846
847 // move to length-based strings, rather than null-terminated strings
848 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000849 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000850
John Kessenichc999ba22013-11-07 23:33:24 +0000851 if (! shaderStrings) {
852 CompileFailed = true;
853 return;
854 }
John Kessenicha0af4732012-12-12 21:15:54 +0000855
John Kessenich52ac67e2013-05-05 23:46:22 +0000856 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000857 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000858
John Kessenich94a81fb2013-08-31 02:41:30 +0000859 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
860 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000861 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000862 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000863 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
864 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
865 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000866 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000867 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000868 }
John Kessenicha0af4732012-12-12 21:15:54 +0000869
John Kessenich94a81fb2013-08-31 02:41:30 +0000870 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000871 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000872 }
John Kessenicha0af4732012-12-12 21:15:54 +0000873
John Kessenich41cf6b52013-06-25 18:10:05 +0000874 delete [] lengths;
875 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000876
John Kessenichc999ba22013-11-07 23:33:24 +0000877 if (ret == 0)
878 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000879}
880
John Kessenicha0af4732012-12-12 21:15:54 +0000881//
882// print usage to stdout
883//
884void usage()
885{
John Kessenich319de232013-12-04 04:43:40 +0000886 printf("Usage: glslangValidator [option]... [file]...\n"
887 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000888 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600889 " .conf to provide an optional config file that replaces the default configuration\n"
890 " (see -c option below for generating a template)\n"
891 " .vert for a vertex shader\n"
892 " .tesc for a tessellation control shader\n"
893 " .tese for a tessellation evaluation shader\n"
894 " .geom for a geometry shader\n"
895 " .frag for a fragment shader\n"
896 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000897 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000898 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000899 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000900 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600901 "Each option must be specified separately.\n"
902 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
903 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600904 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
905 " default file name is <stage>.spv (-o overrides this)\n"
906 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400907 " -E print pre-processed GLSL; cannot be used with -l;\n"
908 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400909 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
Dan Baker895275e2016-08-11 14:55:49 -0400910 " valid choices are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600911 " -c configuration dump;\n"
912 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600913 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600914 " -d default to desktop (#version 110) when there is no shader #version\n"
915 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700916 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700917 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600918 " -h print this usage message\n"
919 " -i intermediate tree (glslang AST) is printed out\n"
920 " -l link all input files together to form a single module\n"
921 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500922 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600923 " -q dump reflection query database\n"
924 " -r relaxed semantic error-checking mode\n"
925 " -s silent mode\n"
926 " -t multi-threaded mode\n"
927 " -v print version strings\n"
928 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500929 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600930 "\n"
931 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
932 " --ssb [stage] num synonym for --shift-sampler-binding\n"
933 "\n"
934 " --shift-texture-binding [stage] num set base binding number for textures\n"
935 " --stb [stage] num synonym for --shift-texture-binding\n"
936 "\n"
937 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
938 " --sub [stage] num synonym for --shift-UBO-binding\n"
939 "\n"
940 " --auto-map-bindings automatically bind uniform variables without\n"
941 " explicit bindings.\n"
942 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -0600943 "\n"
944 " --flatten-uniform-arrays flatten uniform array references to scalars\n"
945 " --fua synonym for --flatten-uniform-arrays\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000946 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600947
948 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000949}
950
John Kessenich3ce4e592014-10-06 19:57:34 +0000951#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000952
953#include <errno.h>
954
955int fopen_s(
956 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000957 const char* filename,
958 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000959)
960{
961 if (!pFile || !filename || !mode) {
962 return EINVAL;
963 }
964
965 FILE* f = fopen(filename, mode);
966 if (! f) {
967 if (errno != 0) {
968 return errno;
969 } else {
970 return ENOENT;
971 }
972 }
973 *pFile = f;
974
975 return 0;
976}
977
978#endif
979
John Kessenicha0af4732012-12-12 21:15:54 +0000980//
981// Malloc a string of sufficient size and read a string into it.
982//
John Kessenich51cdd902014-02-18 23:37:57 +0000983char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000984{
John Kessenichb3297152015-07-11 18:01:03 -0600985 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +0000986 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000987
John Kessenicha0af4732012-12-12 21:15:54 +0000988 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600989 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
990 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000991
John Kessenich68d78fd2015-07-12 19:28:10 -0600992 if (errorCode || in == nullptr)
993 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000994
995 while (fgetc(in) != EOF)
996 count++;
997
John Kessenichd6c72a42014-08-18 19:42:35 +0000998 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000999
John Kessenichb3297152015-07-11 18:01:03 -06001000 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001001 if (! fdata)
1002 Error("can't allocate memory");
1003
John Kessenichb3297152015-07-11 18:01:03 -06001004 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001005 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001006 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001007 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001008
John Kessenicha0af4732012-12-12 21:15:54 +00001009 fdata[count] = '\0';
1010 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001011
John Kessenichea869fb2013-10-28 18:12:06 +00001012 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001013 // recover from empty file
1014 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001015 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001016 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001017 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001018
John Kessenichb3297152015-07-11 18:01:03 -06001019 return return_data;
1020 } else
1021 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1022
1023 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001024 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001025 int ptr_len = 0;
1026 int i = 0;
1027 while (count > 0) {
1028 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1029 memcpy(return_data[i], fdata + ptr_len, len);
1030 return_data[i][len] = '\0';
1031 count -= len;
1032 ptr_len += len;
1033 if (count < len) {
1034 if (count == 0) {
1035 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001036 break;
1037 }
John Kessenichb3297152015-07-11 18:01:03 -06001038 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +00001039 }
1040 ++i;
1041 }
John Kessenichb3297152015-07-11 18:01:03 -06001042
1043 free(fdata);
1044
John Kessenicha0af4732012-12-12 21:15:54 +00001045 return return_data;
1046}
1047
John Kessenich51cdd902014-02-18 23:37:57 +00001048void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001049{
John Kessenichb3297152015-07-11 18:01:03 -06001050 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001051 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001052
1053 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001054}
1055
John Kessenich54d8cda2013-02-11 22:36:01 +00001056void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001057{
John Kessenichfae38ee2015-06-10 23:23:12 +00001058 if (num >= 0 )
1059 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1060 else
1061 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001062}