blob: 063e416d35a1e91209944f96746097eaf864a482 [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),
John Kessenich94a81fb2013-08-31 02:41:30 +000084};
85
John Kessenicha0af4732012-12-12 21:15:54 +000086//
John Kessenich68d78fd2015-07-12 19:28:10 -060087// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000088//
89enum TFailCode {
90 ESuccess = 0,
91 EFailUsage,
92 EFailCompile,
93 EFailLink,
94 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000095 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000096 EFailLinkerCreate
97};
98
99//
John Kessenich68d78fd2015-07-12 19:28:10 -0600100// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000101//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600102EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000103void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000104void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000105void FreeFileData(char** data);
106char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000107void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000108
John Kessenichc999ba22013-11-07 23:33:24 +0000109// Globally track if any compile or link failure.
110bool CompileFailed = false;
111bool LinkFailed = false;
112
John Kessenich05a70632013-09-17 19:26:08 +0000113// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600114// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000115int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000116
John Kessenich05a70632013-09-17 19:26:08 +0000117TBuiltInResource Resources;
118std::string ConfigFile;
119
John Kessenicha0af4732012-12-12 21:15:54 +0000120//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600121// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000122//
123void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000124{
John Kessenich05a70632013-09-17 19:26:08 +0000125 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000126 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000127 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000128 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000129 if (configStrings)
130 config = *configStrings;
131 else {
132 printf("Error opening configuration file; will instead use the default configuration\n");
133 usage();
134 }
135 }
136
137 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500138 Resources = glslang::DefaultTBuiltInResource;
139 return;
John Kessenich05a70632013-09-17 19:26:08 +0000140 }
141
Lei Zhang1b141722016-05-19 13:50:49 -0400142 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000143
John Kessenich05a70632013-09-17 19:26:08 +0000144 if (configStrings)
145 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500146 else
147 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000148}
149
John Kessenich38f3b892013-09-06 19:52:57 +0000150// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000151glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000152
153// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000154glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000155int NumWorkItems = 0;
156
John Kessenich94a81fb2013-08-31 02:41:30 +0000157int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600158const char* ExecutableName = nullptr;
159const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700160const char* entryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400161const char* shaderStageName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600162
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600163std::array<unsigned int, EShLangCount> baseSamplerBinding;
164std::array<unsigned int, EShLangCount> baseTextureBinding;
165std::array<unsigned int, EShLangCount> baseUboBinding;
166
John Kessenich68d78fd2015-07-12 19:28:10 -0600167//
168// Create the default name for saving a binary if -o is not provided.
169//
170const char* GetBinaryName(EShLanguage stage)
171{
172 const char* name;
173 if (binaryFileName == nullptr) {
174 switch (stage) {
175 case EShLangVertex: name = "vert.spv"; break;
176 case EShLangTessControl: name = "tesc.spv"; break;
177 case EShLangTessEvaluation: name = "tese.spv"; break;
178 case EShLangGeometry: name = "geom.spv"; break;
179 case EShLangFragment: name = "frag.spv"; break;
180 case EShLangCompute: name = "comp.spv"; break;
181 default: name = "unknown"; break;
182 }
183 } else
184 name = binaryFileName;
185
186 return name;
187}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000188
John Kessenich05a70632013-09-17 19:26:08 +0000189//
190// *.conf => this is a config file that can set limits/resources
191//
192bool SetConfigFile(const std::string& name)
193{
194 if (name.size() < 5)
195 return false;
196
John Kessenich4c706852013-10-11 16:28:43 +0000197 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000198 ConfigFile = name;
199 return true;
200 }
201
202 return false;
203}
204
John Kessenich68d78fd2015-07-12 19:28:10 -0600205//
206// Give error and exit with failure code.
207//
208void Error(const char* message)
209{
210 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
211 exit(EFailUsage);
212}
213
214//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600215// Process an optional binding base of the form:
216// --argname [stage] base
217// Where stage is one of the forms accepted by FindLanguage, and base is an integer
218//
219void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
220{
221 if (argc < 2)
222 usage();
223
224 if (!isdigit(argv[1][0])) {
225 if (argc < 3) // this form needs one more argument
226 usage();
227
228 // Parse form: --argname stage base
229 const EShLanguage lang = FindLanguage(argv[1], false);
230 base[lang] = atoi(argv[2]);
231 argc-= 2;
232 argv+= 2;
233 } else {
234 // Parse form: --argname base
235 for (int lang=0; lang<EShLangCount; ++lang)
236 base[lang] = atoi(argv[1]);
237
238 argc--;
239 argv++;
240 }
241}
242
243//
John Kessenich68d78fd2015-07-12 19:28:10 -0600244// Do all command-line argument parsing. This includes building up the work-items
245// to be processed later, and saving all the command-line options.
246//
247// Does not return (it exits) if command-line is fatally flawed.
248//
249void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000250{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600251 baseSamplerBinding.fill(0);
252 baseTextureBinding.fill(0);
253 baseUboBinding.fill(0);
254
John Kessenich38f3b892013-09-06 19:52:57 +0000255 ExecutableName = argv[0];
256 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 +0000257 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600258 for (int w = 0; w < NumWorkItems; ++w)
259 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000260
John Kessenich2b07c7e2013-07-31 18:44:13 +0000261 argc--;
262 argv++;
263 for (; argc >= 1; argc--, argv++) {
264 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000265 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600266 case '-':
267 {
268 std::string lowerword(argv[0]+2);
269 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
270
271 // handle --word style options
272 if (lowerword == "shift-sampler-bindings" || // synonyms
273 lowerword == "shift-sampler-binding" ||
274 lowerword == "ssb") {
275 ProcessBindingBase(argc, argv, baseSamplerBinding);
276 } else if (lowerword == "shift-texture-bindings" || // synonyms
277 lowerword == "shift-texture-binding" ||
278 lowerword == "stb") {
279 ProcessBindingBase(argc, argv, baseTextureBinding);
280 } else if (lowerword == "shift-ubo-bindings" || // synonyms
281 lowerword == "shift-ubo-binding" ||
282 lowerword == "sub") {
283 ProcessBindingBase(argc, argv, baseUboBinding);
284 } else if (lowerword == "auto-map-bindings" || // synonyms
285 lowerword == "auto-map-binding" ||
286 lowerword == "amb") {
287 Options |= EOptionAutoMapBindings;
288 } else {
289 usage();
290 }
291 }
292 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000293 case 'H':
294 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600295 if ((Options & EOptionSpv) == 0) {
296 // default to Vulkan
297 Options |= EOptionSpv;
298 Options |= EOptionVulkanRules;
299 Options |= EOptionLinkProgram;
300 }
301 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000302 case 'V':
303 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600304 Options |= EOptionVulkanRules;
305 Options |= EOptionLinkProgram;
306 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400307 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400308 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400309 if (argc > 0) {
310 argc--;
311 argv++;
312 }
313 else
Dan Baker5afdd782016-08-11 17:53:57 -0400314 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400315 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600316 case 'G':
317 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000318 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600319 // undo a -H default to Vulkan
320 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000321 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000322 case 'E':
323 Options |= EOptionOutputPreprocessed;
324 break;
John Kessenich05a70632013-09-17 19:26:08 +0000325 case 'c':
326 Options |= EOptionDumpConfig;
327 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600328 case 'C':
329 Options |= EOptionCascadingErrors;
330 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000331 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000332 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000333 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700334 case 'D':
335 Options |= EOptionReadHlsl;
336 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700337 case 'e':
338 // HLSL todo: entry point handle needs much more sophistication.
339 // This is okay for one compilation unit with one entry point.
340 entryPointName = argv[1];
341 if (argc > 0) {
342 argc--;
343 argv++;
344 } else
345 Error("no <entry-point> provided for -e");
346 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600347 case 'h':
348 usage();
349 break;
John Kessenich05a70632013-09-17 19:26:08 +0000350 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000351 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000352 break;
353 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000354 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000355 break;
356 case 'm':
357 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000358 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600359 case 'o':
360 binaryFileName = argv[1];
361 if (argc > 0) {
362 argc--;
363 argv++;
364 } else
365 Error("no <file> provided for -o");
366 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000367 case 'q':
368 Options |= EOptionDumpReflection;
369 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000370 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000371 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000372 break;
373 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000374 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000375 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000376 case 't':
377 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000378 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000379 #endif
380 break;
John Kessenich319de232013-12-04 04:43:40 +0000381 case 'v':
382 Options |= EOptionDumpVersions;
383 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000384 case 'w':
385 Options |= EOptionSuppressWarnings;
386 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500387 case 'x':
388 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500389 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000390 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600391 usage();
392 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000393 }
John Kessenich38f3b892013-09-06 19:52:57 +0000394 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000395 std::string name(argv[0]);
396 if (! SetConfigFile(name)) {
397 Work[argc] = new glslang::TWorkItem(name);
398 Worklist.add(Work[argc]);
399 }
John Kessenich38f3b892013-09-06 19:52:57 +0000400 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000401 }
402
John Kessenich68d78fd2015-07-12 19:28:10 -0600403 // Make sure that -E is not specified alongside linking (which includes SPV generation)
404 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
405 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000406
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500407 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600408 if (binaryFileName && (Options & EOptionSpv) == 0)
409 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000410}
411
John Kessenich68d78fd2015-07-12 19:28:10 -0600412//
413// Translate the meaningful subset of command-line options to parser-behavior options.
414//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000415void SetMessageOptions(EShMessages& messages)
416{
417 if (Options & EOptionRelaxedErrors)
418 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
419 if (Options & EOptionIntermediate)
420 messages = (EShMessages)(messages | EShMsgAST);
421 if (Options & EOptionSuppressWarnings)
422 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600423 if (Options & EOptionSpv)
424 messages = (EShMessages)(messages | EShMsgSpvRules);
425 if (Options & EOptionVulkanRules)
426 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400427 if (Options & EOptionOutputPreprocessed)
428 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700429 if (Options & EOptionReadHlsl)
430 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600431 if (Options & EOptionCascadingErrors)
432 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000433}
434
John Kessenich68d78fd2015-07-12 19:28:10 -0600435//
John Kessenich69f4b512013-09-04 21:19:27 +0000436// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000437//
438// Return 0 for failure, 1 for success.
439//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800440unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000441{
John Kessenich38f3b892013-09-06 19:52:57 +0000442 glslang::TWorkItem* workItem;
443 while (Worklist.remove(workItem)) {
444 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000445 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000446 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000447
John Kessenichb0a7eb52013-11-07 17:44:20 +0000448 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000449
John Kessenich94a81fb2013-08-31 02:41:30 +0000450 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000451 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000452
453 ShDestruct(compiler);
454 }
455
456 return 0;
457}
458
John Kessenich6626cad2015-06-19 05:14:19 +0000459// Outputs the given string, but only if it is non-null and non-empty.
460// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400461void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000462{
463 if (str && str[0]) {
464 puts(str);
465 }
466}
467
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400468// Outputs the given string to stderr, but only if it is non-null and non-empty.
469// This prevents erroneous newlines from appearing.
470void StderrIfNonEmpty(const char* str)
471{
472 if (str && str[0]) {
473 fprintf(stderr, "%s\n", str);
474 }
475}
476
John Kessenichc57b2a92016-01-16 15:30:03 -0700477// Simple bundling of what makes a compilation unit for ease in passing around,
478// and separation of handling file IO versus API (programmatic) compilation.
479struct ShaderCompUnit {
480 EShLanguage stage;
481 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400482 char** text; // memory owned/managed externally
483 const char* fileNameList[1];
484
John Kessenich219b0252016-08-23 17:51:13 -0600485 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400486 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
487 {
488 stage = istage;
489 fileName = ifileName;
490 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600491 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400492 }
493
494 ShaderCompUnit(const ShaderCompUnit &rhs)
495 {
496 stage = rhs.stage;
497 fileName = rhs.fileName;
498 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600499 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400500 }
501
John Kessenichc57b2a92016-01-16 15:30:03 -0700502};
503
John Kessenich69f4b512013-09-04 21:19:27 +0000504//
John Kessenichc57b2a92016-01-16 15:30:03 -0700505// For linking mode: Will independently parse each compilation unit, but then put them
506// in the same program and link them together, making at most one linked module per
507// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000508//
509// Uses the new C++ interface instead of the old handle-based interface.
510//
John Kessenichc57b2a92016-01-16 15:30:03 -0700511
512void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000513{
514 // keep track of what to free
515 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700516
John Kessenich69f4b512013-09-04 21:19:27 +0000517 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000518 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000519
John Kessenich69f4b512013-09-04 21:19:27 +0000520 //
521 // Per-shader processing...
522 //
523
John Kessenich5b0f13a2013-11-01 03:08:40 +0000524 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100525 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
526 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700527 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400528 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700529 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
530 shader->setEntryPoint(entryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600531
532 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
533 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
534 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
535
536 if (Options & EOptionAutoMapBindings)
537 shader->setAutoMapBindings(true);
538
John Kessenich69f4b512013-09-04 21:19:27 +0000539 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600540
John Kessenichc555ddd2015-06-17 02:38:44 +0000541 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000542
John Kessenichc555ddd2015-06-17 02:38:44 +0000543 if (Options & EOptionOutputPreprocessed) {
544 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500545 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400546 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500547 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400548 PutsIfNonEmpty(str.c_str());
549 } else {
550 CompileFailed = true;
551 }
552 StderrIfNonEmpty(shader->getInfoLog());
553 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000554 continue;
555 }
556 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000557 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000558
John Kessenich69f4b512013-09-04 21:19:27 +0000559 program.addShader(shader);
560
John Kessenichc57b2a92016-01-16 15:30:03 -0700561 if (! (Options & EOptionSuppressInfolog) &&
562 ! (Options & EOptionMemoryLeakMode)) {
563 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400564 PutsIfNonEmpty(shader->getInfoLog());
565 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000566 }
John Kessenich69f4b512013-09-04 21:19:27 +0000567 }
568
569 //
570 // Program-level processing...
571 //
572
John Kessenich4d65ee32016-03-12 18:17:47 -0700573 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600574 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000575 LinkFailed = true;
576
John Kessenich4d65ee32016-03-12 18:17:47 -0700577 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700578 if (! (Options & EOptionSuppressInfolog) &&
579 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400580 PutsIfNonEmpty(program.getInfoLog());
581 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000582 }
583
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600584 // Map IO
585 if (Options & EOptionSpv)
586 program.mapIO();
587
John Kessenich4d65ee32016-03-12 18:17:47 -0700588 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000589 if (Options & EOptionDumpReflection) {
590 program.buildReflection();
591 program.dumpReflection();
592 }
593
John Kessenich4d65ee32016-03-12 18:17:47 -0700594 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000595 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000596 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600597 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000598 else {
599 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000600 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000601 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400602 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400603 spv::SpvBuildLogger logger;
604 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700605
606 // Dump the spv to a file or stdout, etc., but only if not doing
607 // memory/perf testing, as it's not internal to programmatic use.
608 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500609 printf("%s", logger.getAllMessages().c_str());
610 if (Options & EOptionOutputHexadecimal) {
611 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
612 } else {
613 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
614 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700615 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700616 spv::Disassemble(std::cout, spirv);
617 }
John Kessenichacba7722015-03-04 03:48:38 +0000618 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000619 }
John Kessenich92f90382014-07-28 04:21:04 +0000620 }
621 }
622 }
623
John Kessenich5b0f13a2013-11-01 03:08:40 +0000624 // Free everything up, program has to go before the shaders
625 // because it might have merged stuff from the shaders, and
626 // the stuff from the shaders has to have its destructors called
627 // before the pools holding the memory in the shaders is freed.
628 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000629 while (shaders.size() > 0) {
630 delete shaders.back();
631 shaders.pop_back();
632 }
John Kessenich69f4b512013-09-04 21:19:27 +0000633}
634
John Kessenichc57b2a92016-01-16 15:30:03 -0700635//
636// Do file IO part of compile and link, handing off the pure
637// API/programmatic mode to CompileAndLinkShaderUnits(), which can
638// be put in a loop for testing memory footprint and performance.
639//
640// This is just for linking mode: meaning all the shaders will be put into the
641// the same program linked together.
642//
643// This means there are a limited number of work items (not multi-threading mode)
644// and that the point is testing at the linking level. Hence, to enable
645// performance and memory testing, the actual compile/link can be put in
646// a loop, independent of processing the work items and file IO.
647//
648void CompileAndLinkShaderFiles()
649{
650 std::vector<ShaderCompUnit> compUnits;
651
652 // Transfer all the work items from to a simple list of
653 // of compilation units. (We don't care about the thread
654 // work-item distribution properties in this path, which
655 // is okay due to the limited number of shaders, know since
656 // they are all getting linked together.)
657 glslang::TWorkItem* workItem;
658 while (Worklist.remove(workItem)) {
659 ShaderCompUnit compUnit = {
660 FindLanguage(workItem->name),
661 workItem->name,
662 ReadFileData(workItem->name.c_str())
663 };
664
665 if (! compUnit.text) {
666 usage();
667 return;
668 }
669
670 compUnits.push_back(compUnit);
671 }
672
673 // Actual call to programmatic processing of compile and link,
674 // in a loop for testing memory and performance. This part contains
675 // all the perf/memory that a programmatic consumer will care about.
676 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
677 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
678 CompileAndLinkShaderUnits(compUnits);
679
680 if (Options & EOptionMemoryLeakMode)
681 glslang::OS_DumpMemoryCounters();
682 }
683
rdb32084e82016-02-23 22:17:38 +0100684 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
685 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700686}
687
John Kessenicha0af4732012-12-12 21:15:54 +0000688int C_DECL main(int argc, char* argv[])
689{
John Kessenich68d78fd2015-07-12 19:28:10 -0600690 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000691
John Kessenich05a70632013-09-17 19:26:08 +0000692 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500693 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000694 if (Worklist.empty())
695 return ESuccess;
696 }
697
John Kessenich0da9eaa2015-08-01 17:10:02 -0600698 if (Options & EOptionDumpVersions) {
699 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000700 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
701 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600702 std::string spirvVersion;
703 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600704 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600705 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700706 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600707 printf("GL_KHR_vulkan_glsl version %d\n", 100);
708 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000709 if (Worklist.empty())
710 return ESuccess;
711 }
712
John Kessenich05a70632013-09-17 19:26:08 +0000713 if (Worklist.empty()) {
714 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000715 }
716
717 ProcessConfigFile();
718
John Kessenich69f4b512013-09-04 21:19:27 +0000719 //
720 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000721 // 1) linking all arguments together, single-threaded, new C++ interface
722 // 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 +0000723 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000724 if (Options & EOptionLinkProgram ||
725 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000726 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700727 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000728 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500729 for (int w = 0; w < NumWorkItems; ++w) {
730 if (Work[w]) {
731 delete Work[w];
732 }
733 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000734 } else {
735 ShInitialize();
736
John Kessenich38f3b892013-09-06 19:52:57 +0000737 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000738
John Kessenich38f3b892013-09-06 19:52:57 +0000739 if (Options & EOptionMultiThreaded) {
740 const int NumThreads = 16;
741 void* threads[NumThreads];
742 for (int t = 0; t < NumThreads; ++t) {
743 threads[t] = glslang::OS_CreateThread(&CompileShaders);
744 if (! threads[t]) {
745 printf("Failed to create thread\n");
746 return EFailThreadCreate;
747 }
John Kessenicha0af4732012-12-12 21:15:54 +0000748 }
John Kessenich38f3b892013-09-06 19:52:57 +0000749 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000750 } else
751 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000752
753 // Print out all the resulting infologs
754 for (int w = 0; w < NumWorkItems; ++w) {
755 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600756 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400757 PutsIfNonEmpty(Work[w]->name.c_str());
758 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000759 delete Work[w];
760 }
761 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000762
763 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000764 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000765
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500766 delete[] Work;
767
John Kessenichc999ba22013-11-07 23:33:24 +0000768 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000769 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000770 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000771 return EFailLink;
772
773 return 0;
774}
775
776//
777// Deduce the language from the filename. Files must end in one of the
778// following extensions:
779//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000780// .vert = vertex
781// .tesc = tessellation control
782// .tese = tessellation evaluation
783// .geom = geometry
784// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000785// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000786//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600787EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000788{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600789 size_t ext = 0;
790
791 // Search for a suffix on a filename: e.g, "myfile.frag". If given
792 // the suffix directly, we skip looking the '.'
793 if (parseSuffix) {
794 ext = name.rfind('.');
795 if (ext == std::string::npos) {
796 usage();
797 return EShLangVertex;
798 }
799 ++ext;
John Kessenicha0af4732012-12-12 21:15:54 +0000800 }
801
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600802 std::string suffix = name.substr(ext, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400803 if (shaderStageName)
804 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400805
John Kessenich2b07c7e2013-07-31 18:44:13 +0000806 if (suffix == "vert")
807 return EShLangVertex;
808 else if (suffix == "tesc")
809 return EShLangTessControl;
810 else if (suffix == "tese")
811 return EShLangTessEvaluation;
812 else if (suffix == "geom")
813 return EShLangGeometry;
814 else if (suffix == "frag")
815 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000816 else if (suffix == "comp")
817 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000818
819 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000820 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000821}
822
John Kessenicha0af4732012-12-12 21:15:54 +0000823//
John Kessenich69f4b512013-09-04 21:19:27 +0000824// Read a file's data into a string, and compile it using the old interface ShCompile,
825// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000826//
John Kessenich51cdd902014-02-18 23:37:57 +0000827void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000828{
John Kessenichca3457f2015-05-18 01:59:45 +0000829 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000830 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000831 if (! shaderStrings) {
832 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000833 }
834
John Kessenich41cf6b52013-06-25 18:10:05 +0000835 int* lengths = new int[NumShaderStrings];
836
837 // move to length-based strings, rather than null-terminated strings
838 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000839 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000840
John Kessenichc999ba22013-11-07 23:33:24 +0000841 if (! shaderStrings) {
842 CompileFailed = true;
843 return;
844 }
John Kessenicha0af4732012-12-12 21:15:54 +0000845
John Kessenich52ac67e2013-05-05 23:46:22 +0000846 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000847 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000848
John Kessenich94a81fb2013-08-31 02:41:30 +0000849 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
850 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000851 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000852 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000853 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
854 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
855 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000856 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000857 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000858 }
John Kessenicha0af4732012-12-12 21:15:54 +0000859
John Kessenich94a81fb2013-08-31 02:41:30 +0000860 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000861 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000862 }
John Kessenicha0af4732012-12-12 21:15:54 +0000863
John Kessenich41cf6b52013-06-25 18:10:05 +0000864 delete [] lengths;
865 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000866
John Kessenichc999ba22013-11-07 23:33:24 +0000867 if (ret == 0)
868 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000869}
870
John Kessenicha0af4732012-12-12 21:15:54 +0000871//
872// print usage to stdout
873//
874void usage()
875{
John Kessenich319de232013-12-04 04:43:40 +0000876 printf("Usage: glslangValidator [option]... [file]...\n"
877 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000878 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600879 " .conf to provide an optional config file that replaces the default configuration\n"
880 " (see -c option below for generating a template)\n"
881 " .vert for a vertex shader\n"
882 " .tesc for a tessellation control shader\n"
883 " .tese for a tessellation evaluation shader\n"
884 " .geom for a geometry shader\n"
885 " .frag for a fragment shader\n"
886 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000887 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000888 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000889 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000890 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600891 "Each option must be specified separately.\n"
892 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
893 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600894 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
895 " default file name is <stage>.spv (-o overrides this)\n"
896 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400897 " -E print pre-processed GLSL; cannot be used with -l;\n"
898 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400899 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
Dan Baker895275e2016-08-11 14:55:49 -0400900 " valid choices are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600901 " -c configuration dump;\n"
902 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600903 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600904 " -d default to desktop (#version 110) when there is no shader #version\n"
905 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700906 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700907 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600908 " -h print this usage message\n"
909 " -i intermediate tree (glslang AST) is printed out\n"
910 " -l link all input files together to form a single module\n"
911 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500912 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600913 " -q dump reflection query database\n"
914 " -r relaxed semantic error-checking mode\n"
915 " -s silent mode\n"
916 " -t multi-threaded mode\n"
917 " -v print version strings\n"
918 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500919 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600920 "\n"
921 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
922 " --ssb [stage] num synonym for --shift-sampler-binding\n"
923 "\n"
924 " --shift-texture-binding [stage] num set base binding number for textures\n"
925 " --stb [stage] num synonym for --shift-texture-binding\n"
926 "\n"
927 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
928 " --sub [stage] num synonym for --shift-UBO-binding\n"
929 "\n"
930 " --auto-map-bindings automatically bind uniform variables without\n"
931 " explicit bindings.\n"
932 " --amb synonym for --auto-map-bindings\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000933 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600934
935 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000936}
937
John Kessenich3ce4e592014-10-06 19:57:34 +0000938#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000939
940#include <errno.h>
941
942int fopen_s(
943 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000944 const char* filename,
945 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000946)
947{
948 if (!pFile || !filename || !mode) {
949 return EINVAL;
950 }
951
952 FILE* f = fopen(filename, mode);
953 if (! f) {
954 if (errno != 0) {
955 return errno;
956 } else {
957 return ENOENT;
958 }
959 }
960 *pFile = f;
961
962 return 0;
963}
964
965#endif
966
John Kessenicha0af4732012-12-12 21:15:54 +0000967//
968// Malloc a string of sufficient size and read a string into it.
969//
John Kessenich51cdd902014-02-18 23:37:57 +0000970char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000971{
John Kessenichb3297152015-07-11 18:01:03 -0600972 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +0000973 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000974
John Kessenicha0af4732012-12-12 21:15:54 +0000975 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600976 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
977 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000978
John Kessenich68d78fd2015-07-12 19:28:10 -0600979 if (errorCode || in == nullptr)
980 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000981
982 while (fgetc(in) != EOF)
983 count++;
984
John Kessenichd6c72a42014-08-18 19:42:35 +0000985 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000986
John Kessenichb3297152015-07-11 18:01:03 -0600987 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -0600988 if (! fdata)
989 Error("can't allocate memory");
990
John Kessenichb3297152015-07-11 18:01:03 -0600991 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -0600992 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -0600993 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000994 }
John Kessenich68d78fd2015-07-12 19:28:10 -0600995
John Kessenicha0af4732012-12-12 21:15:54 +0000996 fdata[count] = '\0';
997 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -0600998
John Kessenichea869fb2013-10-28 18:12:06 +0000999 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001000 // recover from empty file
1001 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001002 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001003 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001004 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001005
John Kessenichb3297152015-07-11 18:01:03 -06001006 return return_data;
1007 } else
1008 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1009
1010 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001011 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001012 int ptr_len = 0;
1013 int i = 0;
1014 while (count > 0) {
1015 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1016 memcpy(return_data[i], fdata + ptr_len, len);
1017 return_data[i][len] = '\0';
1018 count -= len;
1019 ptr_len += len;
1020 if (count < len) {
1021 if (count == 0) {
1022 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001023 break;
1024 }
John Kessenichb3297152015-07-11 18:01:03 -06001025 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +00001026 }
1027 ++i;
1028 }
John Kessenichb3297152015-07-11 18:01:03 -06001029
1030 free(fdata);
1031
John Kessenicha0af4732012-12-12 21:15:54 +00001032 return return_data;
1033}
1034
John Kessenich51cdd902014-02-18 23:37:57 +00001035void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001036{
John Kessenichb3297152015-07-11 18:01:03 -06001037 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001038 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001039
1040 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001041}
1042
John Kessenich54d8cda2013-02-11 22:36:01 +00001043void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001044{
John Kessenichfae38ee2015-06-10 23:23:12 +00001045 if (num >= 0 )
1046 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1047 else
1048 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001049}