blob: 92991d1bc06785a91ae5ff180779b2f76b29d47e [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000035//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030038#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000039#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030040#endif
John Kessenich05a70632013-09-17 19:26:08 +000041
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040042#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000043#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060044#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000045#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060046#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
Juan Lopeza558b262017-04-02 23:04:00 +020058#include <memory>
59#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000060
baldurk876a0e32015-11-16 18:03:28 +010061#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000062
63extern "C" {
64 SH_IMPORT_EXPORT void ShOutputHtml();
65}
66
John Kessenich94a81fb2013-08-31 02:41:30 +000067// Command-line options
68enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070069 EOptionNone = 0,
70 EOptionIntermediate = (1 << 0),
71 EOptionSuppressInfolog = (1 << 1),
72 EOptionMemoryLeakMode = (1 << 2),
73 EOptionRelaxedErrors = (1 << 3),
74 EOptionGiveWarnings = (1 << 4),
75 EOptionLinkProgram = (1 << 5),
76 EOptionMultiThreaded = (1 << 6),
77 EOptionDumpConfig = (1 << 7),
78 EOptionDumpReflection = (1 << 8),
79 EOptionSuppressWarnings = (1 << 9),
80 EOptionDumpVersions = (1 << 10),
81 EOptionSpv = (1 << 11),
82 EOptionHumanReadableSpv = (1 << 12),
83 EOptionVulkanRules = (1 << 13),
84 EOptionDefaultDesktop = (1 << 14),
85 EOptionOutputPreprocessed = (1 << 15),
86 EOptionOutputHexadecimal = (1 << 16),
87 EOptionReadHlsl = (1 << 17),
88 EOptionCascadingErrors = (1 << 18),
89 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060090 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070091 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070092 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060093 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060094 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060095 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060096 EOptionDebug = (1 << 26),
John Kessenich94a81fb2013-08-31 02:41:30 +000097};
98
John Kessenicha0af4732012-12-12 21:15:54 +000099//
John Kessenich68d78fd2015-07-12 19:28:10 -0600100// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000101//
102enum TFailCode {
103 ESuccess = 0,
104 EFailUsage,
105 EFailCompile,
106 EFailLink,
107 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000108 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000109 EFailLinkerCreate
110};
111
112//
John Kessenich68d78fd2015-07-12 19:28:10 -0600113// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000114//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600115EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000116void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000117void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600118char* ReadFileData(const char* fileName);
119void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000120void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000121
John Kessenichc999ba22013-11-07 23:33:24 +0000122// Globally track if any compile or link failure.
123bool CompileFailed = false;
124bool LinkFailed = false;
125
John Kessenich05a70632013-09-17 19:26:08 +0000126TBuiltInResource Resources;
127std::string ConfigFile;
128
John Kessenicha0af4732012-12-12 21:15:54 +0000129//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600130// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000131//
132void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000133{
John Kessenich04acb1b2017-06-14 17:36:50 -0600134 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500135 Resources = glslang::DefaultTBuiltInResource;
John Kessenich04acb1b2017-06-14 17:36:50 -0600136 else {
137 char* configString = ReadFileData(ConfigFile.c_str());
138 glslang::DecodeResourceLimits(&Resources, configString);
139 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000140 }
John Kessenicha0af4732012-12-12 21:15:54 +0000141}
142
John Kessenich94a81fb2013-08-31 02:41:30 +0000143int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600144const char* ExecutableName = nullptr;
145const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700146const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600147const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400148const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800149const char* variableName = nullptr;
John Kessenich971a0a82017-06-07 15:06:58 -0600150std::vector<std::string> IncludeDirectoryList;
John Kessenich68d78fd2015-07-12 19:28:10 -0600151
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600152std::array<unsigned int, EShLangCount> baseSamplerBinding;
153std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600154std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600155std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700156std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600157std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900158std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600159
John Kessenicha9313662017-06-15 10:40:49 -0600160// Add things like "#define ..." to a preamble to use in the beginning of the shader.
161class TPreamble {
162public:
163 TPreamble() { }
164
165 bool isSet() const { return text.size() > 0; }
166 const char* get() const { return text.c_str(); }
167
168 // #define...
169 void addDef(std::string def)
170 {
171 text.append("#define ");
172 fixLine(def);
173
174 // The first "=" needs to turn into a space
175 int equal = def.find_first_of("=");
176 if (equal != def.npos)
177 def[equal] = ' ';
178
179 text.append(def);
180 text.append("\n");
181 }
182
183 // #undef...
184 void addUndef(std::string undef)
185 {
186 text.append("#undef ");
187 fixLine(undef);
188 text.append(undef);
189 text.append("\n");
190 }
191
192protected:
193 void fixLine(std::string& line)
194 {
195 // Can't go past a newline in the line
196 int end = line.find_first_of("\n");
197 if (end != line.npos)
198 line = line.substr(0, end);
199 }
200
201 std::string text; // contents of preamble
202};
203
204TPreamble UserPreamble;
205
John Kessenich68d78fd2015-07-12 19:28:10 -0600206//
207// Create the default name for saving a binary if -o is not provided.
208//
209const char* GetBinaryName(EShLanguage stage)
210{
211 const char* name;
212 if (binaryFileName == nullptr) {
213 switch (stage) {
214 case EShLangVertex: name = "vert.spv"; break;
215 case EShLangTessControl: name = "tesc.spv"; break;
216 case EShLangTessEvaluation: name = "tese.spv"; break;
217 case EShLangGeometry: name = "geom.spv"; break;
218 case EShLangFragment: name = "frag.spv"; break;
219 case EShLangCompute: name = "comp.spv"; break;
220 default: name = "unknown"; break;
221 }
222 } else
223 name = binaryFileName;
224
225 return name;
226}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000227
John Kessenich05a70632013-09-17 19:26:08 +0000228//
229// *.conf => this is a config file that can set limits/resources
230//
231bool SetConfigFile(const std::string& name)
232{
233 if (name.size() < 5)
234 return false;
235
John Kessenich4c706852013-10-11 16:28:43 +0000236 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000237 ConfigFile = name;
238 return true;
239 }
240
241 return false;
242}
243
John Kessenich68d78fd2015-07-12 19:28:10 -0600244//
245// Give error and exit with failure code.
246//
247void Error(const char* message)
248{
249 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
250 exit(EFailUsage);
251}
252
253//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600254// Process an optional binding base of the form:
255// --argname [stage] base
256// Where stage is one of the forms accepted by FindLanguage, and base is an integer
257//
258void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
259{
260 if (argc < 2)
261 usage();
262
263 if (!isdigit(argv[1][0])) {
264 if (argc < 3) // this form needs one more argument
265 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700266
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600267 // Parse form: --argname stage base
268 const EShLanguage lang = FindLanguage(argv[1], false);
269 base[lang] = atoi(argv[2]);
270 argc-= 2;
271 argv+= 2;
272 } else {
273 // Parse form: --argname base
274 for (int lang=0; lang<EShLangCount; ++lang)
275 base[lang] = atoi(argv[1]);
276
277 argc--;
278 argv++;
279 }
280}
281
Hyangran Park36dc8292017-05-02 16:27:29 +0900282void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
283{
284 if (argc < 2)
285 usage();
286
287 if (!isdigit(argv[1][0])) {
288 if (argc < 5) // this form needs one more argument
289 usage();
290
291 // Parse form: --argname stage base
292 const EShLanguage lang = FindLanguage(argv[1], false);
293
294 base[lang].push_back(argv[2]);
295 base[lang].push_back(argv[3]);
296 base[lang].push_back(argv[4]);
297 argc-= 4;
298 argv+= 4;
299 while(argv[1] != NULL) {
300 if(argv[1][0] != '-') {
301 base[lang].push_back(argv[1]);
302 base[lang].push_back(argv[2]);
303 base[lang].push_back(argv[3]);
304 argc-= 3;
305 argv+= 3;
306 }
307 else {
308 break;
309 }
310 }
311 } else {
312 // Parse form: --argname base
313 for (int lang=0; lang<EShLangCount; ++lang)
314 base[lang].push_back(argv[1]);
315
316 argc--;
317 argv++;
318 }
319}
320
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600321//
John Kessenich68d78fd2015-07-12 19:28:10 -0600322// Do all command-line argument parsing. This includes building up the work-items
323// to be processed later, and saving all the command-line options.
324//
325// Does not return (it exits) if command-line is fatally flawed.
326//
Juan Lopeza558b262017-04-02 23:04:00 +0200327void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000328{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600329 baseSamplerBinding.fill(0);
330 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600331 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600332 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700333 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600334 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600335
John Kessenich38f3b892013-09-06 19:52:57 +0000336 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200337 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000338
John Kessenichc178f0a2017-06-23 10:58:31 -0600339 const auto bumpArg = [&]() {
340 if (argc > 0) {
341 argc--;
342 argv++;
343 }
344 };
345
346 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600347 const auto getStringOperand = [&](const char* desc) {
348 if (argv[0][2] == 0) {
349 printf("%s must immediately follow option (no spaces)\n", desc);
350 exit(EFailUsage);
351 }
352 return argv[0] + 2;
353 };
354
John Kessenichc178f0a2017-06-23 10:58:31 -0600355 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000356 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000357 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600358 case '-':
359 {
360 std::string lowerword(argv[0]+2);
361 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
362
363 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600364 if (lowerword == "auto-map-bindings" || // synonyms
365 lowerword == "auto-map-binding" ||
366 lowerword == "amb") {
367 Options |= EOptionAutoMapBindings;
368 } else if (lowerword == "auto-map-locations" || // synonyms
369 lowerword == "aml") {
370 Options |= EOptionAutoMapLocations;
371 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
372 lowerword == "flatten-uniform-array" ||
373 lowerword == "fua") {
374 Options |= EOptionFlattenUniformArrays;
375 } else if (lowerword == "hlsl-offsets") {
376 Options |= EOptionHlslOffsets;
377 } else if (lowerword == "hlsl-iomap" ||
378 lowerword == "hlsl-iomapper" ||
379 lowerword == "hlsl-iomapping") {
380 Options |= EOptionHlslIoMapping;
381 } else if (lowerword == "keep-uncalled" || // synonyms
382 lowerword == "ku") {
383 Options |= EOptionKeepUncalled;
384 } else if (lowerword == "no-storage-format" || // synonyms
385 lowerword == "nsf") {
386 Options |= EOptionNoStorageFormat;
387 } else if (lowerword == "resource-set-bindings" || // synonyms
388 lowerword == "resource-set-binding" ||
389 lowerword == "rsb") {
390 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600391 } else if (lowerword == "shift-image-bindings" || // synonyms
392 lowerword == "shift-image-binding" ||
393 lowerword == "sib") {
394 ProcessBindingBase(argc, argv, baseImageBinding);
John Kessenich6263fb12017-06-14 15:52:44 -0600395 } else if (lowerword == "shift-sampler-bindings" || // synonyms
396 lowerword == "shift-sampler-binding" ||
397 lowerword == "ssb") {
398 ProcessBindingBase(argc, argv, baseSamplerBinding);
399 } else if (lowerword == "shift-uav-bindings" || // synonyms
400 lowerword == "shift-uav-binding" ||
401 lowerword == "suavb") {
402 ProcessBindingBase(argc, argv, baseUavBinding);
403 } else if (lowerword == "shift-texture-bindings" || // synonyms
404 lowerword == "shift-texture-binding" ||
405 lowerword == "stb") {
406 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600407 } else if (lowerword == "shift-ubo-bindings" || // synonyms
408 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600409 lowerword == "shift-cbuffer-bindings" ||
410 lowerword == "shift-cbuffer-binding" ||
411 lowerword == "sub" ||
412 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600413 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700414 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
415 lowerword == "shift-ssbo-binding" ||
416 lowerword == "sbb") {
417 ProcessBindingBase(argc, argv, baseSsboBinding);
John Kessenich6263fb12017-06-14 15:52:44 -0600418 } else if (lowerword == "source-entrypoint" || // synonyms
419 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600420 if (argc <= 1)
John Kessenich6263fb12017-06-14 15:52:44 -0600421 Error("no <entry-point> provided for --source-entrypoint");
John Kessenichc178f0a2017-06-23 10:58:31 -0600422 sourceEntryPointName = argv[1];
423 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600424 break;
Flavio15017db2017-02-15 14:29:33 -0800425 } else if (lowerword == "variable-name" || // synonyms
426 lowerword == "vn") {
427 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600428 if (argc <= 1)
Flavio15017db2017-02-15 14:29:33 -0800429 Error("no <C-variable-name> provided for --variable-name");
John Kessenichc178f0a2017-06-23 10:58:31 -0600430 variableName = argv[1];
431 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800432 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600433 } else {
434 usage();
435 }
436 }
437 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600438 case 'C':
439 Options |= EOptionCascadingErrors;
440 break;
441 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600442 if (argv[0][2] == 0)
443 Options |= EOptionReadHlsl;
444 else
445 UserPreamble.addDef(getStringOperand("-D<macro> macro name"));
John Kessenich6263fb12017-06-14 15:52:44 -0600446 break;
447 case 'E':
448 Options |= EOptionOutputPreprocessed;
449 break;
450 case 'G':
451 Options |= EOptionSpv;
452 Options |= EOptionLinkProgram;
453 // undo a -H default to Vulkan
454 Options &= ~EOptionVulkanRules;
455 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000456 case 'H':
457 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600458 if ((Options & EOptionSpv) == 0) {
459 // default to Vulkan
460 Options |= EOptionSpv;
461 Options |= EOptionVulkanRules;
462 Options |= EOptionLinkProgram;
463 }
464 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600465 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600466 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600467 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400468 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600469 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400470 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600471 shaderStageName = argv[1];
472 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400473 break;
John Kessenicha9313662017-06-15 10:40:49 -0600474 case 'U':
475 UserPreamble.addUndef(getStringOperand("-U<macro>: macro name"));
476 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600477 case 'V':
John Kessenich68d78fd2015-07-12 19:28:10 -0600478 Options |= EOptionSpv;
John Kessenich6263fb12017-06-14 15:52:44 -0600479 Options |= EOptionVulkanRules;
John Kessenichd78e3512014-08-25 20:07:55 +0000480 Options |= EOptionLinkProgram;
John Kessenichc555ddd2015-06-17 02:38:44 +0000481 break;
John Kessenich05a70632013-09-17 19:26:08 +0000482 case 'c':
483 Options |= EOptionDumpConfig;
484 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000485 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000486 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000487 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700488 case 'e':
489 // HLSL todo: entry point handle needs much more sophistication.
490 // This is okay for one compilation unit with one entry point.
491 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600492 if (argc <= 1)
John Kessenich4d65ee32016-03-12 18:17:47 -0700493 Error("no <entry-point> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600494 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700495 break;
John Kessenich121853f2017-05-31 17:11:16 -0600496 case 'g':
497 Options |= EOptionDebug;
498 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600499 case 'h':
500 usage();
501 break;
John Kessenich05a70632013-09-17 19:26:08 +0000502 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000503 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000504 break;
505 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000506 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000507 break;
508 case 'm':
509 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000510 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600511 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600512 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600513 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600514 binaryFileName = argv[1];
515 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600516 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000517 case 'q':
518 Options |= EOptionDumpReflection;
519 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000520 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000521 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000522 break;
523 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000524 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000525 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000526 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200527 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000528 break;
John Kessenich319de232013-12-04 04:43:40 +0000529 case 'v':
530 Options |= EOptionDumpVersions;
531 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000532 case 'w':
533 Options |= EOptionSuppressWarnings;
534 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500535 case 'x':
536 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500537 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000538 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600539 usage();
540 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000541 }
John Kessenich38f3b892013-09-06 19:52:57 +0000542 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000543 std::string name(argv[0]);
544 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200545 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000546 }
John Kessenich38f3b892013-09-06 19:52:57 +0000547 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000548 }
549
John Kessenich68d78fd2015-07-12 19:28:10 -0600550 // Make sure that -E is not specified alongside linking (which includes SPV generation)
551 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
552 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000553
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500554 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600555 if (binaryFileName && (Options & EOptionSpv) == 0)
556 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600557
558 if ((Options & EOptionFlattenUniformArrays) != 0 &&
559 (Options & EOptionReadHlsl) == 0)
560 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000561}
562
John Kessenich68d78fd2015-07-12 19:28:10 -0600563//
564// Translate the meaningful subset of command-line options to parser-behavior options.
565//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000566void SetMessageOptions(EShMessages& messages)
567{
568 if (Options & EOptionRelaxedErrors)
569 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
570 if (Options & EOptionIntermediate)
571 messages = (EShMessages)(messages | EShMsgAST);
572 if (Options & EOptionSuppressWarnings)
573 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600574 if (Options & EOptionSpv)
575 messages = (EShMessages)(messages | EShMsgSpvRules);
576 if (Options & EOptionVulkanRules)
577 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400578 if (Options & EOptionOutputPreprocessed)
579 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700580 if (Options & EOptionReadHlsl)
581 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600582 if (Options & EOptionCascadingErrors)
583 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700584 if (Options & EOptionKeepUncalled)
585 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600586 if (Options & EOptionHlslOffsets)
587 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600588 if (Options & EOptionDebug)
589 messages = (EShMessages)(messages | EShMsgDebugInfo);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000590}
591
John Kessenich68d78fd2015-07-12 19:28:10 -0600592//
John Kessenich69f4b512013-09-04 21:19:27 +0000593// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000594//
Juan Lopeza558b262017-04-02 23:04:00 +0200595void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000596{
John Kessenich38f3b892013-09-06 19:52:57 +0000597 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200598 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000599 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000600 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200601 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000602
John Kessenichb0a7eb52013-11-07 17:44:20 +0000603 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000604
John Kessenich94a81fb2013-08-31 02:41:30 +0000605 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000606 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000607
608 ShDestruct(compiler);
609 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000610}
611
John Kessenich6626cad2015-06-19 05:14:19 +0000612// Outputs the given string, but only if it is non-null and non-empty.
613// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400614void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000615{
616 if (str && str[0]) {
617 puts(str);
618 }
619}
620
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400621// Outputs the given string to stderr, but only if it is non-null and non-empty.
622// This prevents erroneous newlines from appearing.
623void StderrIfNonEmpty(const char* str)
624{
John Kessenich04acb1b2017-06-14 17:36:50 -0600625 if (str && str[0])
626 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400627}
628
John Kessenichc57b2a92016-01-16 15:30:03 -0700629// Simple bundling of what makes a compilation unit for ease in passing around,
630// and separation of handling file IO versus API (programmatic) compilation.
631struct ShaderCompUnit {
632 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600633 static const int maxCount = 1;
634 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -0600635 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -0600636 std::string fileName[maxCount]; // hold's the memory, but...
637 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400638
John Kessenich04acb1b2017-06-14 17:36:50 -0600639 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400640
John Kessenich04acb1b2017-06-14 17:36:50 -0600641 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400642 {
643 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600644 count = rhs.count;
645 for (int i = 0; i < count; ++i) {
646 fileName[i] = rhs.fileName[i];
647 text[i] = rhs.text[i];
648 fileNameList[i] = rhs.fileName[i].c_str();
649 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400650 }
651
John Kessenicha9313662017-06-15 10:40:49 -0600652 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -0600653 {
654 assert(count < maxCount);
655 fileName[count] = ifileName;
656 text[count] = itext;
657 fileNameList[count] = fileName[count].c_str();
658 ++count;
659 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700660};
661
John Kessenich69f4b512013-09-04 21:19:27 +0000662//
John Kessenichc57b2a92016-01-16 15:30:03 -0700663// For linking mode: Will independently parse each compilation unit, but then put them
664// in the same program and link them together, making at most one linked module per
665// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000666//
667// Uses the new C++ interface instead of the old handle-based interface.
668//
John Kessenichc57b2a92016-01-16 15:30:03 -0700669
670void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000671{
672 // keep track of what to free
673 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700674
John Kessenich69f4b512013-09-04 21:19:27 +0000675 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000676 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000677
John Kessenich69f4b512013-09-04 21:19:27 +0000678 //
679 // Per-shader processing...
680 //
681
John Kessenich5b0f13a2013-11-01 03:08:40 +0000682 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100683 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
684 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700685 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -0600686 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich4d65ee32016-03-12 18:17:47 -0700687 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
688 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600689 if (sourceEntryPointName)
690 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenicha9313662017-06-15 10:40:49 -0600691 if (UserPreamble.isSet())
692 shader->setPreamble(UserPreamble.get());
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600693
694 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
695 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600696 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600697 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700698 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600699 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600700 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600701 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900702 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600703
steve-lunargbe283552017-04-18 12:18:01 -0600704 if (Options & EOptionHlslIoMapping)
705 shader->setHlslIoMapping(true);
706
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600707 if (Options & EOptionAutoMapBindings)
708 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700709
John Kessenich71facdf2017-05-17 18:28:19 -0600710 if (Options & EOptionAutoMapLocations)
711 shader->setAutoMapLocations(true);
712
John Kessenich4be4aeb2017-06-23 10:50:22 -0600713 // Set up the environment, some subsettings take precedence over earlier
714 // ways of setting things.
715 if (Options & EOptionSpv) {
716 if (Options & EOptionVulkanRules) {
717 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
718 : glslang::EShSourceGlsl,
719 compUnit.stage, glslang::EShClientVulkan, 100);
720 shader->setEnvClient(glslang::EShClientVulkan, 100);
721 shader->setEnvTarget(glslang::EshTargetSpv, 0x00001000);
722 } else {
723 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
724 : glslang::EShSourceGlsl,
725 compUnit.stage, glslang::EShClientOpenGL, 100);
726 shader->setEnvClient(glslang::EShClientOpenGL, 450);
727 shader->setEnvTarget(glslang::EshTargetSpv, 0x00001000);
728 }
729 }
730
John Kessenich69f4b512013-09-04 21:19:27 +0000731 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600732
John Kessenich4be4aeb2017-06-23 10:50:22 -0600733 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000734
John Kessenich3494b4d2017-05-22 15:00:42 -0600735 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -0600736 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
737 includer.pushExternalLocalDirectory(dir); });
John Kessenichc555ddd2015-06-17 02:38:44 +0000738 if (Options & EOptionOutputPreprocessed) {
739 std::string str;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400740 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500741 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400742 PutsIfNonEmpty(str.c_str());
743 } else {
744 CompileFailed = true;
745 }
746 StderrIfNonEmpty(shader->getInfoLog());
747 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000748 continue;
749 }
John Kessenich3494b4d2017-05-22 15:00:42 -0600750 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +0000751 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000752
John Kessenich69f4b512013-09-04 21:19:27 +0000753 program.addShader(shader);
754
John Kessenichc57b2a92016-01-16 15:30:03 -0700755 if (! (Options & EOptionSuppressInfolog) &&
756 ! (Options & EOptionMemoryLeakMode)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600757 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400758 PutsIfNonEmpty(shader->getInfoLog());
759 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000760 }
John Kessenich69f4b512013-09-04 21:19:27 +0000761 }
762
763 //
764 // Program-level processing...
765 //
766
John Kessenich4d65ee32016-03-12 18:17:47 -0700767 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600768 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000769 LinkFailed = true;
770
steve-lunarg9ae34742016-10-05 13:40:13 -0600771 // Map IO
772 if (Options & EOptionSpv) {
773 if (!program.mapIO())
774 LinkFailed = true;
775 }
John Kessenichecba76f2017-01-06 00:34:48 -0700776
John Kessenich4d65ee32016-03-12 18:17:47 -0700777 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700778 if (! (Options & EOptionSuppressInfolog) &&
779 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400780 PutsIfNonEmpty(program.getInfoLog());
781 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000782 }
783
John Kessenich4d65ee32016-03-12 18:17:47 -0700784 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000785 if (Options & EOptionDumpReflection) {
786 program.buildReflection();
787 program.dumpReflection();
788 }
789
John Kessenich4d65ee32016-03-12 18:17:47 -0700790 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000791 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000792 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600793 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000794 else {
795 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000796 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000797 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400798 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400799 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -0600800 glslang::SpvOptions spvOptions;
801 if (Options & EOptionDebug)
802 spvOptions.generateDebugInfo = true;
803 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -0700804
805 // Dump the spv to a file or stdout, etc., but only if not doing
806 // memory/perf testing, as it's not internal to programmatic use.
807 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500808 printf("%s", logger.getAllMessages().c_str());
809 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700810 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500811 } else {
812 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
813 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700814 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700815 spv::Disassemble(std::cout, spirv);
816 }
John Kessenichacba7722015-03-04 03:48:38 +0000817 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000818 }
John Kessenich92f90382014-07-28 04:21:04 +0000819 }
820 }
821 }
822
John Kessenich5b0f13a2013-11-01 03:08:40 +0000823 // Free everything up, program has to go before the shaders
824 // because it might have merged stuff from the shaders, and
825 // the stuff from the shaders has to have its destructors called
826 // before the pools holding the memory in the shaders is freed.
827 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000828 while (shaders.size() > 0) {
829 delete shaders.back();
830 shaders.pop_back();
831 }
John Kessenich69f4b512013-09-04 21:19:27 +0000832}
833
John Kessenichc57b2a92016-01-16 15:30:03 -0700834//
835// Do file IO part of compile and link, handing off the pure
836// API/programmatic mode to CompileAndLinkShaderUnits(), which can
837// be put in a loop for testing memory footprint and performance.
838//
839// This is just for linking mode: meaning all the shaders will be put into the
840// the same program linked together.
841//
842// This means there are a limited number of work items (not multi-threading mode)
843// and that the point is testing at the linking level. Hence, to enable
844// performance and memory testing, the actual compile/link can be put in
845// a loop, independent of processing the work items and file IO.
846//
Juan Lopeza558b262017-04-02 23:04:00 +0200847void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700848{
849 std::vector<ShaderCompUnit> compUnits;
850
851 // Transfer all the work items from to a simple list of
852 // of compilation units. (We don't care about the thread
853 // work-item distribution properties in this path, which
854 // is okay due to the limited number of shaders, know since
855 // they are all getting linked together.)
856 glslang::TWorkItem* workItem;
857 while (Worklist.remove(workItem)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600858 ShaderCompUnit compUnit(FindLanguage(workItem->name));
859 char* fileText = ReadFileData(workItem->name.c_str());
860 if (fileText == nullptr)
John Kessenichc57b2a92016-01-16 15:30:03 -0700861 usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600862 compUnit.addString(workItem->name, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -0700863 compUnits.push_back(compUnit);
864 }
865
866 // Actual call to programmatic processing of compile and link,
867 // in a loop for testing memory and performance. This part contains
868 // all the perf/memory that a programmatic consumer will care about.
869 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
870 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
871 CompileAndLinkShaderUnits(compUnits);
872
873 if (Options & EOptionMemoryLeakMode)
874 glslang::OS_DumpMemoryCounters();
875 }
876
John Kessenicha9313662017-06-15 10:40:49 -0600877 // free memory from ReadFileData, which got stored in a const char*
878 // as the first string above
rdb32084e82016-02-23 22:17:38 +0100879 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -0600880 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -0700881}
882
John Kessenicha0af4732012-12-12 21:15:54 +0000883int C_DECL main(int argc, char* argv[])
884{
Juan Lopeza558b262017-04-02 23:04:00 +0200885 // array of unique places to leave the shader names and infologs for the asynchronous compiles
886 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
887 ProcessArguments(workItems, argc, argv);
888
889 glslang::TWorklist workList;
890 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
891 assert(item);
892 workList.add(item.get());
893 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000894
John Kessenich05a70632013-09-17 19:26:08 +0000895 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500896 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200897 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000898 return ESuccess;
899 }
900
John Kessenich0da9eaa2015-08-01 17:10:02 -0600901 if (Options & EOptionDumpVersions) {
902 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000903 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
904 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600905 std::string spirvVersion;
906 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600907 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600908 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700909 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600910 printf("GL_KHR_vulkan_glsl version %d\n", 100);
911 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200912 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000913 return ESuccess;
914 }
915
Juan Lopeza558b262017-04-02 23:04:00 +0200916 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000917 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000918 }
919
920 ProcessConfigFile();
921
John Kessenich69f4b512013-09-04 21:19:27 +0000922 //
923 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000924 // 1) linking all arguments together, single-threaded, new C++ interface
925 // 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 +0000926 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000927 if (Options & EOptionLinkProgram ||
928 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000929 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200930 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000931 glslang::FinalizeProcess();
932 } else {
933 ShInitialize();
934
Juan Lopeza558b262017-04-02 23:04:00 +0200935 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000936
Juan Lopeza558b262017-04-02 23:04:00 +0200937 if (Options & EOptionMultiThreaded)
938 {
939 std::array<std::thread, 16> threads;
940 for (unsigned int t = 0; t < threads.size(); ++t)
941 {
942 threads[t] = std::thread(CompileShaders, std::ref(workList));
943 if (threads[t].get_id() == std::thread::id())
944 {
John Kessenich38f3b892013-09-06 19:52:57 +0000945 printf("Failed to create thread\n");
946 return EFailThreadCreate;
947 }
John Kessenicha0af4732012-12-12 21:15:54 +0000948 }
Juan Lopeza558b262017-04-02 23:04:00 +0200949
950 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000951 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200952 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000953
954 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200955 for (size_t w = 0; w < workItems.size(); ++w) {
956 if (workItems[w]) {
957 if (printShaderNames || workItems[w]->results.size() > 0)
958 PutsIfNonEmpty(workItems[w]->name.c_str());
959 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000960 }
961 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000962
963 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000964 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000965
John Kessenichc999ba22013-11-07 23:33:24 +0000966 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000967 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000968 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000969 return EFailLink;
970
971 return 0;
972}
973
974//
975// Deduce the language from the filename. Files must end in one of the
976// following extensions:
977//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000978// .vert = vertex
979// .tesc = tessellation control
980// .tese = tessellation evaluation
981// .geom = geometry
982// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000983// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000984//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600985EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000986{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600987 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600988 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600989
Dan Bakerc6ede892016-08-11 14:06:06 -0400990 if (shaderStageName)
991 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600992 else {
993 // Search for a suffix on a filename: e.g, "myfile.frag". If given
994 // the suffix directly, we skip looking for the '.'
995 if (parseSuffix) {
996 ext = name.rfind('.');
997 if (ext == std::string::npos) {
998 usage();
999 return EShLangVertex;
1000 }
1001 ++ext;
1002 }
1003 suffix = name.substr(ext, std::string::npos);
1004 }
dankbaker45d49bc2016-08-08 21:43:07 -04001005
John Kessenich2b07c7e2013-07-31 18:44:13 +00001006 if (suffix == "vert")
1007 return EShLangVertex;
1008 else if (suffix == "tesc")
1009 return EShLangTessControl;
1010 else if (suffix == "tese")
1011 return EShLangTessEvaluation;
1012 else if (suffix == "geom")
1013 return EShLangGeometry;
1014 else if (suffix == "frag")
1015 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +00001016 else if (suffix == "comp")
1017 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001018
1019 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001020 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001021}
1022
John Kessenicha0af4732012-12-12 21:15:54 +00001023//
John Kessenichecba76f2017-01-06 00:34:48 -07001024// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001025// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001026//
John Kessenich51cdd902014-02-18 23:37:57 +00001027void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001028{
John Kessenichca3457f2015-05-18 01:59:45 +00001029 int ret = 0;
John Kessenich04acb1b2017-06-14 17:36:50 -06001030 char* shaderString = ReadFileData(fileName);
John Kessenich41cf6b52013-06-25 18:10:05 +00001031
1032 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001033 int* lengths = new int[1];
1034 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001035
John Kessenich52ac67e2013-05-05 23:46:22 +00001036 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001037 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001038
John Kessenicha9313662017-06-15 10:40:49 -06001039 if (UserPreamble.isSet())
1040 Error("-D and -U options require -l (linking)\n");
1041
John Kessenich94a81fb2013-08-31 02:41:30 +00001042 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1043 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001044 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001045 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001046 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001047 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001048 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001049 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1050 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001051 }
John Kessenicha0af4732012-12-12 21:15:54 +00001052
John Kessenich94a81fb2013-08-31 02:41:30 +00001053 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001054 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001055 }
John Kessenicha0af4732012-12-12 21:15:54 +00001056
John Kessenich41cf6b52013-06-25 18:10:05 +00001057 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001058 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001059
John Kessenichc999ba22013-11-07 23:33:24 +00001060 if (ret == 0)
1061 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001062}
1063
John Kessenicha0af4732012-12-12 21:15:54 +00001064//
1065// print usage to stdout
1066//
1067void usage()
1068{
John Kessenich319de232013-12-04 04:43:40 +00001069 printf("Usage: glslangValidator [option]... [file]...\n"
1070 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001071 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1072 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001073 " (see -c option below for generating a template)\n"
1074 " .vert for a vertex shader\n"
1075 " .tesc for a tessellation control shader\n"
1076 " .tese for a tessellation evaluation shader\n"
1077 " .geom for a geometry shader\n"
1078 " .frag for a fragment shader\n"
1079 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001080 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001081 "Options:\n"
1082 " -C cascading errors; risk crash from accumulation of error recoveries\n"
1083 " -D input is HLSL\n"
John Kessenicha9313662017-06-15 10:40:49 -06001084 " -D<macro=def>\n"
1085 " -D<macro> define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001086 " -E print pre-processed GLSL; cannot be used with -l;\n"
1087 " errors will appear on stderr.\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001088 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1089 " default file name is <stage>.spv (-o overrides this)\n"
1090 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001091 " -I<dir> add dir to the include search path; includer's directory\n"
1092 " is searched first, followed by left-to-right order of -I\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001093 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001094 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenicha9313662017-06-15 10:40:49 -06001095 " -U<macro> undefine a pre-precossor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001096 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1097 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001098 " -c configuration dump;\n"
1099 " creates the default configuration file (redirect to a .conf file)\n"
1100 " -d default to desktop (#version 110) when there is no shader #version\n"
1101 " (default is ES version 100)\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001102 " -e specify entry-point name\n"
John Kessenich121853f2017-05-31 17:11:16 -06001103 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001104 " -h print this usage message\n"
1105 " -i intermediate tree (glslang AST) is printed out\n"
1106 " -l link all input files together to form a single module\n"
1107 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001108 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001109 " -q dump reflection query database\n"
1110 " -r relaxed semantic error-checking mode\n"
1111 " -s silent mode\n"
1112 " -t multi-threaded mode\n"
1113 " -v print version strings\n"
1114 " -w suppress warnings (except as required by #extension : warn)\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001115 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
1116 " --auto-map-bindings automatically bind uniform variables\n"
1117 " without explicit bindings.\n"
1118 " --amb synonym for --auto-map-bindings\n"
1119 " --auto-map-locations automatically locate input/output lacking\n"
John Kessenichc178f0a2017-06-23 10:58:31 -06001120 " 'location' (fragile, not cross stage)\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001121 " --aml synonym for --auto-map-locations\n"
1122 " --flatten-uniform-arrays flatten uniform texture/sampler arrays to\n"
1123 " scalars\n"
1124 " --fua synonym for --flatten-uniform-arrays\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001125 " --hlsl-offsets Allow block offsets to follow HLSL rules\n"
1126 " Works independently of source language\n"
1127 " --hlsl-iomap Perform IO mapping in HLSL register space\n"
1128 " --keep-uncalled don't eliminate uncalled functions\n"
1129 " --ku synonym for --keep-uncalled\n"
1130 " --no-storage-format use Unknown image format\n"
1131 " --nsf synonym for --no-storage-format\n"
1132 " --resource-set-binding [stage] num descriptor set and binding for resources\n"
1133 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1134 " --shift-image-binding [stage] num base binding number for images (uav)\n"
1135 " --sib [stage] num synonym for --shift-image-binding\n"
1136 " --shift-sampler-binding [stage] num base binding number for samplers\n"
1137 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1138 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
1139 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1140 " --shift-texture-binding [stage] num base binding number for textures\n"
1141 " --stb [stage] num synonym for --shift-texture-binding\n"
1142 " --shift-uav-binding [stage] num base binding number for UAVs\n"
1143 " --suavb [stage] num synonym for --shift-uav-binding\n"
1144 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
1145 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
1146 " --sub [stage] num synonym for --shift-UBO-binding\n"
1147 " --source-entrypoint name the given shader source function is\n"
1148 " renamed to be the entry point given in -e\n"
1149 " --sep synonym for --source-entrypoint\n"
1150 " --variable-name <name> Creates a C header file that contains a\n"
1151 " uint32_t array named <name>\n"
1152 " initialized with the shader binary code.\n"
1153 " --vn <name> synonym for --variable-name <name>\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001154 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001155
1156 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001157}
1158
John Kessenich3ce4e592014-10-06 19:57:34 +00001159#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001160
1161#include <errno.h>
1162
1163int fopen_s(
1164 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001165 const char* filename,
1166 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001167)
1168{
1169 if (!pFile || !filename || !mode) {
1170 return EINVAL;
1171 }
1172
1173 FILE* f = fopen(filename, mode);
1174 if (! f) {
1175 if (errno != 0) {
1176 return errno;
1177 } else {
1178 return ENOENT;
1179 }
1180 }
1181 *pFile = f;
1182
1183 return 0;
1184}
1185
1186#endif
1187
John Kessenicha0af4732012-12-12 21:15:54 +00001188//
1189// Malloc a string of sufficient size and read a string into it.
1190//
John Kessenich04acb1b2017-06-14 17:36:50 -06001191char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001192{
John Kessenichb3297152015-07-11 18:01:03 -06001193 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001194 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001195 if (errorCode || in == nullptr)
1196 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001197
John Kessenich04acb1b2017-06-14 17:36:50 -06001198 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001199 while (fgetc(in) != EOF)
1200 count++;
1201
John Kessenichd6c72a42014-08-18 19:42:35 +00001202 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001203
John Kessenich04acb1b2017-06-14 17:36:50 -06001204 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1205 if ((int)fread(return_data, 1, count, in) != count) {
1206 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001207 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001208 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001209
John Kessenich04acb1b2017-06-14 17:36:50 -06001210 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001211 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001212
John Kessenicha0af4732012-12-12 21:15:54 +00001213 return return_data;
1214}
1215
John Kessenich04acb1b2017-06-14 17:36:50 -06001216void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001217{
John Kessenichb3297152015-07-11 18:01:03 -06001218 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001219}
1220
John Kessenich54d8cda2013-02-11 22:36:01 +00001221void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001222{
John Kessenichfae38ee2015-06-10 23:23:12 +00001223 if (num >= 0 )
1224 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1225 else
1226 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001227}