blob: b21f20d4c6f084e9d99774e6641d3eb990ff95bd [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
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>
Juan Lopeza558b262017-04-02 23:04:00 +020054#include <memory>
55#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000056
baldurk876a0e32015-11-16 18:03:28 +010057#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000058
59extern "C" {
60 SH_IMPORT_EXPORT void ShOutputHtml();
61}
62
John Kessenich94a81fb2013-08-31 02:41:30 +000063// Command-line options
64enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070065 EOptionNone = 0,
66 EOptionIntermediate = (1 << 0),
67 EOptionSuppressInfolog = (1 << 1),
68 EOptionMemoryLeakMode = (1 << 2),
69 EOptionRelaxedErrors = (1 << 3),
70 EOptionGiveWarnings = (1 << 4),
71 EOptionLinkProgram = (1 << 5),
72 EOptionMultiThreaded = (1 << 6),
73 EOptionDumpConfig = (1 << 7),
74 EOptionDumpReflection = (1 << 8),
75 EOptionSuppressWarnings = (1 << 9),
76 EOptionDumpVersions = (1 << 10),
77 EOptionSpv = (1 << 11),
78 EOptionHumanReadableSpv = (1 << 12),
79 EOptionVulkanRules = (1 << 13),
80 EOptionDefaultDesktop = (1 << 14),
81 EOptionOutputPreprocessed = (1 << 15),
82 EOptionOutputHexadecimal = (1 << 16),
83 EOptionReadHlsl = (1 << 17),
84 EOptionCascadingErrors = (1 << 18),
85 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060086 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070087 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070088 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060089 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060090 EOptionHlslIoMapping = (1 << 24),
John Kessenich94a81fb2013-08-31 02:41:30 +000091};
92
John Kessenicha0af4732012-12-12 21:15:54 +000093//
John Kessenich68d78fd2015-07-12 19:28:10 -060094// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000095//
96enum TFailCode {
97 ESuccess = 0,
98 EFailUsage,
99 EFailCompile,
100 EFailLink,
101 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000102 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000103 EFailLinkerCreate
104};
105
106//
John Kessenich68d78fd2015-07-12 19:28:10 -0600107// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000108//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600109EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000110void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000111void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000112void FreeFileData(char** data);
113char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000114void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000115
John Kessenichc999ba22013-11-07 23:33:24 +0000116// Globally track if any compile or link failure.
117bool CompileFailed = false;
118bool LinkFailed = false;
119
John Kessenich05a70632013-09-17 19:26:08 +0000120// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600121// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000122int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000123
John Kessenich05a70632013-09-17 19:26:08 +0000124TBuiltInResource Resources;
125std::string ConfigFile;
126
John Kessenicha0af4732012-12-12 21:15:54 +0000127//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600128// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000129//
130void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000131{
John Kessenich05a70632013-09-17 19:26:08 +0000132 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000133 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000134 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000135 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000136 if (configStrings)
137 config = *configStrings;
138 else {
139 printf("Error opening configuration file; will instead use the default configuration\n");
140 usage();
141 }
142 }
143
144 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500145 Resources = glslang::DefaultTBuiltInResource;
146 return;
John Kessenich05a70632013-09-17 19:26:08 +0000147 }
148
Lei Zhang1b141722016-05-19 13:50:49 -0400149 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000150
John Kessenich05a70632013-09-17 19:26:08 +0000151 if (configStrings)
152 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500153 else
154 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000155}
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;
steve-lunargf1e0c872016-10-31 15:13:43 -0600161const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400162const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800163const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600164
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600165std::array<unsigned int, EShLangCount> baseSamplerBinding;
166std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600167std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600168std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700169std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600170std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900171std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600172
John Kessenich68d78fd2015-07-12 19:28:10 -0600173//
174// Create the default name for saving a binary if -o is not provided.
175//
176const char* GetBinaryName(EShLanguage stage)
177{
178 const char* name;
179 if (binaryFileName == nullptr) {
180 switch (stage) {
181 case EShLangVertex: name = "vert.spv"; break;
182 case EShLangTessControl: name = "tesc.spv"; break;
183 case EShLangTessEvaluation: name = "tese.spv"; break;
184 case EShLangGeometry: name = "geom.spv"; break;
185 case EShLangFragment: name = "frag.spv"; break;
186 case EShLangCompute: name = "comp.spv"; break;
187 default: name = "unknown"; break;
188 }
189 } else
190 name = binaryFileName;
191
192 return name;
193}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000194
John Kessenich05a70632013-09-17 19:26:08 +0000195//
196// *.conf => this is a config file that can set limits/resources
197//
198bool SetConfigFile(const std::string& name)
199{
200 if (name.size() < 5)
201 return false;
202
John Kessenich4c706852013-10-11 16:28:43 +0000203 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000204 ConfigFile = name;
205 return true;
206 }
207
208 return false;
209}
210
John Kessenich68d78fd2015-07-12 19:28:10 -0600211//
212// Give error and exit with failure code.
213//
214void Error(const char* message)
215{
216 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
217 exit(EFailUsage);
218}
219
220//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600221// Process an optional binding base of the form:
222// --argname [stage] base
223// Where stage is one of the forms accepted by FindLanguage, and base is an integer
224//
225void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
226{
227 if (argc < 2)
228 usage();
229
230 if (!isdigit(argv[1][0])) {
231 if (argc < 3) // this form needs one more argument
232 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700233
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600234 // Parse form: --argname stage base
235 const EShLanguage lang = FindLanguage(argv[1], false);
236 base[lang] = atoi(argv[2]);
237 argc-= 2;
238 argv+= 2;
239 } else {
240 // Parse form: --argname base
241 for (int lang=0; lang<EShLangCount; ++lang)
242 base[lang] = atoi(argv[1]);
243
244 argc--;
245 argv++;
246 }
247}
248
Hyangran Park36dc8292017-05-02 16:27:29 +0900249void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
250{
251 if (argc < 2)
252 usage();
253
254 if (!isdigit(argv[1][0])) {
255 if (argc < 5) // this form needs one more argument
256 usage();
257
258 // Parse form: --argname stage base
259 const EShLanguage lang = FindLanguage(argv[1], false);
260
261 base[lang].push_back(argv[2]);
262 base[lang].push_back(argv[3]);
263 base[lang].push_back(argv[4]);
264 argc-= 4;
265 argv+= 4;
266 while(argv[1] != NULL) {
267 if(argv[1][0] != '-') {
268 base[lang].push_back(argv[1]);
269 base[lang].push_back(argv[2]);
270 base[lang].push_back(argv[3]);
271 argc-= 3;
272 argv+= 3;
273 }
274 else {
275 break;
276 }
277 }
278 } else {
279 // Parse form: --argname base
280 for (int lang=0; lang<EShLangCount; ++lang)
281 base[lang].push_back(argv[1]);
282
283 argc--;
284 argv++;
285 }
286}
287
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600288//
John Kessenich68d78fd2015-07-12 19:28:10 -0600289// Do all command-line argument parsing. This includes building up the work-items
290// to be processed later, and saving all the command-line options.
291//
292// Does not return (it exits) if command-line is fatally flawed.
293//
Juan Lopeza558b262017-04-02 23:04:00 +0200294void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000295{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600296 baseSamplerBinding.fill(0);
297 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600298 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600299 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700300 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600301 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600302
John Kessenich38f3b892013-09-06 19:52:57 +0000303 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200304 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000305
John Kessenich2b07c7e2013-07-31 18:44:13 +0000306 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700307 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000308 for (; argc >= 1; argc--, argv++) {
309 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000310 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600311 case '-':
312 {
313 std::string lowerword(argv[0]+2);
314 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
315
316 // handle --word style options
317 if (lowerword == "shift-sampler-bindings" || // synonyms
318 lowerword == "shift-sampler-binding" ||
319 lowerword == "ssb") {
320 ProcessBindingBase(argc, argv, baseSamplerBinding);
321 } else if (lowerword == "shift-texture-bindings" || // synonyms
322 lowerword == "shift-texture-binding" ||
323 lowerword == "stb") {
324 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600325 } else if (lowerword == "shift-image-bindings" || // synonyms
326 lowerword == "shift-image-binding" ||
327 lowerword == "sib") {
328 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600329 } else if (lowerword == "shift-ubo-bindings" || // synonyms
330 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600331 lowerword == "shift-cbuffer-bindings" ||
332 lowerword == "shift-cbuffer-binding" ||
333 lowerword == "sub" ||
334 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600335 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700336 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
337 lowerword == "shift-ssbo-binding" ||
338 lowerword == "sbb") {
339 ProcessBindingBase(argc, argv, baseSsboBinding);
Hyangran Park36dc8292017-05-02 16:27:29 +0900340 } else if (lowerword == "resource-set-bindings" || // synonyms
341 lowerword == "resource-set-binding" ||
342 lowerword == "rsb") {
343 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600344 } else if (lowerword == "shift-uav-bindings" || // synonyms
345 lowerword == "shift-uav-binding" ||
346 lowerword == "suavb") {
347 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600348 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700349 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600350 lowerword == "amb") {
351 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600352 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
353 lowerword == "flatten-uniform-array" ||
354 lowerword == "fua") {
355 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600356 } else if (lowerword == "no-storage-format" || // synonyms
357 lowerword == "nsf") {
358 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800359 } else if (lowerword == "variable-name" || // synonyms
360 lowerword == "vn") {
361 Options |= EOptionOutputHexadecimal;
362 variableName = argv[1];
363 if (argc > 0) {
364 argc--;
365 argv++;
366 } else
367 Error("no <C-variable-name> provided for --variable-name");
368 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600369 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600370 lowerword == "sep") {
371 sourceEntryPointName = argv[1];
372 if (argc > 0) {
373 argc--;
374 argv++;
375 } else
376 Error("no <entry-point> provided for --source-entrypoint");
377 break;
John Kessenich906cc212016-12-09 19:22:20 -0700378 } else if (lowerword == "keep-uncalled" || // synonyms
379 lowerword == "ku") {
380 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600381 } else if (lowerword == "hlsl-offsets") {
382 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600383 } else if (lowerword == "hlsl-iomap" ||
384 lowerword == "hlsl-iomapper" ||
385 lowerword == "hlsl-iomapping") {
386 Options |= EOptionHlslIoMapping;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600387 } else {
388 usage();
389 }
390 }
391 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000392 case 'H':
393 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600394 if ((Options & EOptionSpv) == 0) {
395 // default to Vulkan
396 Options |= EOptionSpv;
397 Options |= EOptionVulkanRules;
398 Options |= EOptionLinkProgram;
399 }
400 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000401 case 'V':
402 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600403 Options |= EOptionVulkanRules;
404 Options |= EOptionLinkProgram;
405 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400406 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400407 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400408 if (argc > 0) {
409 argc--;
410 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600411 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400412 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400413 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600414 case 'G':
415 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000416 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600417 // undo a -H default to Vulkan
418 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000419 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000420 case 'E':
421 Options |= EOptionOutputPreprocessed;
422 break;
John Kessenich05a70632013-09-17 19:26:08 +0000423 case 'c':
424 Options |= EOptionDumpConfig;
425 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600426 case 'C':
427 Options |= EOptionCascadingErrors;
428 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000429 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000430 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000431 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700432 case 'D':
433 Options |= EOptionReadHlsl;
434 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700435 case 'e':
436 // HLSL todo: entry point handle needs much more sophistication.
437 // This is okay for one compilation unit with one entry point.
438 entryPointName = argv[1];
439 if (argc > 0) {
440 argc--;
441 argv++;
442 } else
443 Error("no <entry-point> provided for -e");
444 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600445 case 'h':
446 usage();
447 break;
John Kessenich05a70632013-09-17 19:26:08 +0000448 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000449 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000450 break;
451 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000452 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000453 break;
454 case 'm':
455 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000456 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600457 case 'o':
458 binaryFileName = argv[1];
459 if (argc > 0) {
460 argc--;
461 argv++;
462 } else
463 Error("no <file> provided for -o");
464 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000465 case 'q':
466 Options |= EOptionDumpReflection;
467 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000468 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000469 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000470 break;
471 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000472 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000473 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000474 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200475 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000476 break;
John Kessenich319de232013-12-04 04:43:40 +0000477 case 'v':
478 Options |= EOptionDumpVersions;
479 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000480 case 'w':
481 Options |= EOptionSuppressWarnings;
482 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500483 case 'x':
484 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500485 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000486 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600487 usage();
488 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000489 }
John Kessenich38f3b892013-09-06 19:52:57 +0000490 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000491 std::string name(argv[0]);
492 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200493 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000494 }
John Kessenich38f3b892013-09-06 19:52:57 +0000495 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000496 }
497
John Kessenich68d78fd2015-07-12 19:28:10 -0600498 // Make sure that -E is not specified alongside linking (which includes SPV generation)
499 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
500 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000501
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500502 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600503 if (binaryFileName && (Options & EOptionSpv) == 0)
504 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600505
506 if ((Options & EOptionFlattenUniformArrays) != 0 &&
507 (Options & EOptionReadHlsl) == 0)
508 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000509}
510
John Kessenich68d78fd2015-07-12 19:28:10 -0600511//
512// Translate the meaningful subset of command-line options to parser-behavior options.
513//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000514void SetMessageOptions(EShMessages& messages)
515{
516 if (Options & EOptionRelaxedErrors)
517 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
518 if (Options & EOptionIntermediate)
519 messages = (EShMessages)(messages | EShMsgAST);
520 if (Options & EOptionSuppressWarnings)
521 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600522 if (Options & EOptionSpv)
523 messages = (EShMessages)(messages | EShMsgSpvRules);
524 if (Options & EOptionVulkanRules)
525 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400526 if (Options & EOptionOutputPreprocessed)
527 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700528 if (Options & EOptionReadHlsl)
529 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600530 if (Options & EOptionCascadingErrors)
531 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700532 if (Options & EOptionKeepUncalled)
533 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600534 if (Options & EOptionHlslOffsets)
535 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000536}
537
John Kessenich68d78fd2015-07-12 19:28:10 -0600538//
John Kessenich69f4b512013-09-04 21:19:27 +0000539// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000540//
Juan Lopeza558b262017-04-02 23:04:00 +0200541void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000542{
John Kessenich38f3b892013-09-06 19:52:57 +0000543 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200544 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000545 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000546 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200547 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000548
John Kessenichb0a7eb52013-11-07 17:44:20 +0000549 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000550
John Kessenich94a81fb2013-08-31 02:41:30 +0000551 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000552 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000553
554 ShDestruct(compiler);
555 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000556}
557
John Kessenich6626cad2015-06-19 05:14:19 +0000558// Outputs the given string, but only if it is non-null and non-empty.
559// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400560void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000561{
562 if (str && str[0]) {
563 puts(str);
564 }
565}
566
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400567// Outputs the given string to stderr, but only if it is non-null and non-empty.
568// This prevents erroneous newlines from appearing.
569void StderrIfNonEmpty(const char* str)
570{
571 if (str && str[0]) {
572 fprintf(stderr, "%s\n", str);
573 }
574}
575
John Kessenichc57b2a92016-01-16 15:30:03 -0700576// Simple bundling of what makes a compilation unit for ease in passing around,
577// and separation of handling file IO versus API (programmatic) compilation.
578struct ShaderCompUnit {
579 EShLanguage stage;
580 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400581 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600582 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400583
John Kessenich219b0252016-08-23 17:51:13 -0600584 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400585 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
586 {
587 stage = istage;
588 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600589 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600590 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400591 }
592
593 ShaderCompUnit(const ShaderCompUnit &rhs)
594 {
595 stage = rhs.stage;
596 fileName = rhs.fileName;
597 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600598 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400599 }
600
John Kessenichc57b2a92016-01-16 15:30:03 -0700601};
602
John Kessenich69f4b512013-09-04 21:19:27 +0000603//
John Kessenichc57b2a92016-01-16 15:30:03 -0700604// For linking mode: Will independently parse each compilation unit, but then put them
605// in the same program and link them together, making at most one linked module per
606// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000607//
608// Uses the new C++ interface instead of the old handle-based interface.
609//
John Kessenichc57b2a92016-01-16 15:30:03 -0700610
611void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000612{
613 // keep track of what to free
614 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700615
John Kessenich69f4b512013-09-04 21:19:27 +0000616 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000617 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000618
John Kessenich69f4b512013-09-04 21:19:27 +0000619 //
620 // Per-shader processing...
621 //
622
John Kessenich5b0f13a2013-11-01 03:08:40 +0000623 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100624 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
625 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700626 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400627 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700628 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
629 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600630 if (sourceEntryPointName)
631 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600632
633 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
634 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600635 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600636 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700637 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600638 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600639 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600640 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900641 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600642
steve-lunargbe283552017-04-18 12:18:01 -0600643 if (Options & EOptionHlslIoMapping)
644 shader->setHlslIoMapping(true);
645
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600646 if (Options & EOptionAutoMapBindings)
647 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700648
John Kessenich69f4b512013-09-04 21:19:27 +0000649 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600650
John Kessenichc555ddd2015-06-17 02:38:44 +0000651 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000652
John Kessenichc555ddd2015-06-17 02:38:44 +0000653 if (Options & EOptionOutputPreprocessed) {
654 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700655 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400656 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500657 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400658 PutsIfNonEmpty(str.c_str());
659 } else {
660 CompileFailed = true;
661 }
662 StderrIfNonEmpty(shader->getInfoLog());
663 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000664 continue;
665 }
666 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000667 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000668
John Kessenich69f4b512013-09-04 21:19:27 +0000669 program.addShader(shader);
670
John Kessenichc57b2a92016-01-16 15:30:03 -0700671 if (! (Options & EOptionSuppressInfolog) &&
672 ! (Options & EOptionMemoryLeakMode)) {
673 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400674 PutsIfNonEmpty(shader->getInfoLog());
675 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000676 }
John Kessenich69f4b512013-09-04 21:19:27 +0000677 }
678
679 //
680 // Program-level processing...
681 //
682
John Kessenich4d65ee32016-03-12 18:17:47 -0700683 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600684 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000685 LinkFailed = true;
686
steve-lunarg9ae34742016-10-05 13:40:13 -0600687 // Map IO
688 if (Options & EOptionSpv) {
689 if (!program.mapIO())
690 LinkFailed = true;
691 }
John Kessenichecba76f2017-01-06 00:34:48 -0700692
John Kessenich4d65ee32016-03-12 18:17:47 -0700693 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700694 if (! (Options & EOptionSuppressInfolog) &&
695 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400696 PutsIfNonEmpty(program.getInfoLog());
697 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000698 }
699
John Kessenich4d65ee32016-03-12 18:17:47 -0700700 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000701 if (Options & EOptionDumpReflection) {
702 program.buildReflection();
703 program.dumpReflection();
704 }
705
John Kessenich4d65ee32016-03-12 18:17:47 -0700706 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000707 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000708 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600709 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000710 else {
711 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000712 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000713 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400714 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400715 spv::SpvBuildLogger logger;
716 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700717
718 // Dump the spv to a file or stdout, etc., but only if not doing
719 // memory/perf testing, as it's not internal to programmatic use.
720 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500721 printf("%s", logger.getAllMessages().c_str());
722 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700723 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500724 } else {
725 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
726 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700727 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700728 spv::Disassemble(std::cout, spirv);
729 }
John Kessenichacba7722015-03-04 03:48:38 +0000730 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000731 }
John Kessenich92f90382014-07-28 04:21:04 +0000732 }
733 }
734 }
735
John Kessenich5b0f13a2013-11-01 03:08:40 +0000736 // Free everything up, program has to go before the shaders
737 // because it might have merged stuff from the shaders, and
738 // the stuff from the shaders has to have its destructors called
739 // before the pools holding the memory in the shaders is freed.
740 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000741 while (shaders.size() > 0) {
742 delete shaders.back();
743 shaders.pop_back();
744 }
John Kessenich69f4b512013-09-04 21:19:27 +0000745}
746
John Kessenichc57b2a92016-01-16 15:30:03 -0700747//
748// Do file IO part of compile and link, handing off the pure
749// API/programmatic mode to CompileAndLinkShaderUnits(), which can
750// be put in a loop for testing memory footprint and performance.
751//
752// This is just for linking mode: meaning all the shaders will be put into the
753// the same program linked together.
754//
755// This means there are a limited number of work items (not multi-threading mode)
756// and that the point is testing at the linking level. Hence, to enable
757// performance and memory testing, the actual compile/link can be put in
758// a loop, independent of processing the work items and file IO.
759//
Juan Lopeza558b262017-04-02 23:04:00 +0200760void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700761{
762 std::vector<ShaderCompUnit> compUnits;
763
764 // Transfer all the work items from to a simple list of
765 // of compilation units. (We don't care about the thread
766 // work-item distribution properties in this path, which
767 // is okay due to the limited number of shaders, know since
768 // they are all getting linked together.)
769 glslang::TWorkItem* workItem;
770 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200771 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700772 FindLanguage(workItem->name),
773 workItem->name,
774 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200775 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700776
777 if (! compUnit.text) {
778 usage();
779 return;
780 }
781
782 compUnits.push_back(compUnit);
783 }
784
785 // Actual call to programmatic processing of compile and link,
786 // in a loop for testing memory and performance. This part contains
787 // all the perf/memory that a programmatic consumer will care about.
788 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
789 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
790 CompileAndLinkShaderUnits(compUnits);
791
792 if (Options & EOptionMemoryLeakMode)
793 glslang::OS_DumpMemoryCounters();
794 }
795
rdb32084e82016-02-23 22:17:38 +0100796 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
797 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700798}
799
John Kessenicha0af4732012-12-12 21:15:54 +0000800int C_DECL main(int argc, char* argv[])
801{
Juan Lopeza558b262017-04-02 23:04:00 +0200802 // array of unique places to leave the shader names and infologs for the asynchronous compiles
803 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
804 ProcessArguments(workItems, argc, argv);
805
806 glslang::TWorklist workList;
807 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
808 assert(item);
809 workList.add(item.get());
810 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000811
John Kessenich05a70632013-09-17 19:26:08 +0000812 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500813 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200814 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000815 return ESuccess;
816 }
817
John Kessenich0da9eaa2015-08-01 17:10:02 -0600818 if (Options & EOptionDumpVersions) {
819 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000820 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
821 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600822 std::string spirvVersion;
823 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600824 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600825 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700826 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600827 printf("GL_KHR_vulkan_glsl version %d\n", 100);
828 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200829 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000830 return ESuccess;
831 }
832
Juan Lopeza558b262017-04-02 23:04:00 +0200833 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000834 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000835 }
836
837 ProcessConfigFile();
838
John Kessenich69f4b512013-09-04 21:19:27 +0000839 //
840 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000841 // 1) linking all arguments together, single-threaded, new C++ interface
842 // 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 +0000843 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000844 if (Options & EOptionLinkProgram ||
845 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000846 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200847 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000848 glslang::FinalizeProcess();
849 } else {
850 ShInitialize();
851
Juan Lopeza558b262017-04-02 23:04:00 +0200852 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000853
Juan Lopeza558b262017-04-02 23:04:00 +0200854 if (Options & EOptionMultiThreaded)
855 {
856 std::array<std::thread, 16> threads;
857 for (unsigned int t = 0; t < threads.size(); ++t)
858 {
859 threads[t] = std::thread(CompileShaders, std::ref(workList));
860 if (threads[t].get_id() == std::thread::id())
861 {
John Kessenich38f3b892013-09-06 19:52:57 +0000862 printf("Failed to create thread\n");
863 return EFailThreadCreate;
864 }
John Kessenicha0af4732012-12-12 21:15:54 +0000865 }
Juan Lopeza558b262017-04-02 23:04:00 +0200866
867 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000868 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200869 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000870
871 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200872 for (size_t w = 0; w < workItems.size(); ++w) {
873 if (workItems[w]) {
874 if (printShaderNames || workItems[w]->results.size() > 0)
875 PutsIfNonEmpty(workItems[w]->name.c_str());
876 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000877 }
878 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000879
880 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000881 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000882
John Kessenichc999ba22013-11-07 23:33:24 +0000883 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000884 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000885 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000886 return EFailLink;
887
888 return 0;
889}
890
891//
892// Deduce the language from the filename. Files must end in one of the
893// following extensions:
894//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000895// .vert = vertex
896// .tesc = tessellation control
897// .tese = tessellation evaluation
898// .geom = geometry
899// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000900// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000901//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600902EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000903{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600904 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600905 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600906
Dan Bakerc6ede892016-08-11 14:06:06 -0400907 if (shaderStageName)
908 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600909 else {
910 // Search for a suffix on a filename: e.g, "myfile.frag". If given
911 // the suffix directly, we skip looking for the '.'
912 if (parseSuffix) {
913 ext = name.rfind('.');
914 if (ext == std::string::npos) {
915 usage();
916 return EShLangVertex;
917 }
918 ++ext;
919 }
920 suffix = name.substr(ext, std::string::npos);
921 }
dankbaker45d49bc2016-08-08 21:43:07 -0400922
John Kessenich2b07c7e2013-07-31 18:44:13 +0000923 if (suffix == "vert")
924 return EShLangVertex;
925 else if (suffix == "tesc")
926 return EShLangTessControl;
927 else if (suffix == "tese")
928 return EShLangTessEvaluation;
929 else if (suffix == "geom")
930 return EShLangGeometry;
931 else if (suffix == "frag")
932 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000933 else if (suffix == "comp")
934 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000935
936 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000937 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000938}
939
John Kessenicha0af4732012-12-12 21:15:54 +0000940//
John Kessenichecba76f2017-01-06 00:34:48 -0700941// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000942// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000943//
John Kessenich51cdd902014-02-18 23:37:57 +0000944void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000945{
John Kessenichca3457f2015-05-18 01:59:45 +0000946 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000947 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000948 if (! shaderStrings) {
949 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000950 }
951
John Kessenich41cf6b52013-06-25 18:10:05 +0000952 int* lengths = new int[NumShaderStrings];
953
954 // move to length-based strings, rather than null-terminated strings
955 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000956 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000957
John Kessenichc999ba22013-11-07 23:33:24 +0000958 if (! shaderStrings) {
959 CompileFailed = true;
960 return;
961 }
John Kessenicha0af4732012-12-12 21:15:54 +0000962
John Kessenich52ac67e2013-05-05 23:46:22 +0000963 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000964 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700965
John Kessenich94a81fb2013-08-31 02:41:30 +0000966 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
967 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700968 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000969 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700970 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700971 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000972 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700973 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
974 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000975 }
John Kessenicha0af4732012-12-12 21:15:54 +0000976
John Kessenich94a81fb2013-08-31 02:41:30 +0000977 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000978 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000979 }
John Kessenicha0af4732012-12-12 21:15:54 +0000980
John Kessenich41cf6b52013-06-25 18:10:05 +0000981 delete [] lengths;
982 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000983
John Kessenichc999ba22013-11-07 23:33:24 +0000984 if (ret == 0)
985 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000986}
987
John Kessenicha0af4732012-12-12 21:15:54 +0000988//
989// print usage to stdout
990//
991void usage()
992{
John Kessenich319de232013-12-04 04:43:40 +0000993 printf("Usage: glslangValidator [option]... [file]...\n"
994 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000995 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600996 " .conf to provide an optional config file that replaces the default configuration\n"
997 " (see -c option below for generating a template)\n"
998 " .vert for a vertex shader\n"
999 " .tesc for a tessellation control shader\n"
1000 " .tese for a tessellation evaluation shader\n"
1001 " .geom for a geometry shader\n"
1002 " .frag for a fragment shader\n"
1003 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001004 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001005 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +00001006 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001007 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001008 "Each option must be specified separately.\n"
1009 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1010 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001011 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1012 " default file name is <stage>.spv (-o overrides this)\n"
1013 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001014 " -E print pre-processed GLSL; cannot be used with -l;\n"
1015 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001016 " -S <stage> uses specified stage rather than parsing the file extension\n"
1017 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001018 " -c configuration dump;\n"
1019 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -06001020 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001021 " -d default to desktop (#version 110) when there is no shader #version\n"
1022 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -07001023 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001024 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001025 " -h print this usage message\n"
1026 " -i intermediate tree (glslang AST) is printed out\n"
1027 " -l link all input files together to form a single module\n"
1028 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001029 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001030 " -q dump reflection query database\n"
1031 " -r relaxed semantic error-checking mode\n"
1032 " -s silent mode\n"
1033 " -t multi-threaded mode\n"
1034 " -v print version strings\n"
1035 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -05001036 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001037 "\n"
1038 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
1039 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1040 "\n"
1041 " --shift-texture-binding [stage] num set base binding number for textures\n"
1042 " --stb [stage] num synonym for --shift-texture-binding\n"
1043 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -06001044 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1045 " --sib [stage] num synonym for --shift-image-binding\n"
1046 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001047 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001048 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001049 " --sub [stage] num synonym for --shift-UBO-binding\n"
1050 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001051 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1052 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1053 "\n"
Hyangran Park36dc8292017-05-02 16:27:29 +09001054 " --resource-set-binding [stage] num set descriptor set and binding number for resources\n"
1055 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1056 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001057 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1058 " --suavb [stage] num synonym for --shift-uav-binding\n"
1059 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001060 " --auto-map-bindings automatically bind uniform variables without\n"
1061 " explicit bindings.\n"
1062 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001063 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001064 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001065 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001066 "\n"
1067 " --no-storage-format use Unknown image format\n"
1068 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001069 "\n"
1070 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1071 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001072 "\n"
1073 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1074 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001075 "\n"
1076 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1077 " initialized with the shader binary code.\n"
1078 " --vn <name> synonym for --variable-name <name>\n"
1079 "\n"
1080 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1081 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001082 "\n"
1083 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001084 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001085
1086 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001087}
1088
John Kessenich3ce4e592014-10-06 19:57:34 +00001089#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001090
1091#include <errno.h>
1092
1093int fopen_s(
1094 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001095 const char* filename,
1096 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001097)
1098{
1099 if (!pFile || !filename || !mode) {
1100 return EINVAL;
1101 }
1102
1103 FILE* f = fopen(filename, mode);
1104 if (! f) {
1105 if (errno != 0) {
1106 return errno;
1107 } else {
1108 return ENOENT;
1109 }
1110 }
1111 *pFile = f;
1112
1113 return 0;
1114}
1115
1116#endif
1117
John Kessenicha0af4732012-12-12 21:15:54 +00001118//
1119// Malloc a string of sufficient size and read a string into it.
1120//
John Kessenichecba76f2017-01-06 00:34:48 -07001121char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001122{
John Kessenichb3297152015-07-11 18:01:03 -06001123 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001124 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001125
John Kessenicha0af4732012-12-12 21:15:54 +00001126 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001127 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1128 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001129
John Kessenich68d78fd2015-07-12 19:28:10 -06001130 if (errorCode || in == nullptr)
1131 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001132
John Kessenicha0af4732012-12-12 21:15:54 +00001133 while (fgetc(in) != EOF)
1134 count++;
1135
John Kessenichd6c72a42014-08-18 19:42:35 +00001136 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001137
John Kessenichb3297152015-07-11 18:01:03 -06001138 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001139 if (! fdata)
1140 Error("can't allocate memory");
1141
John Kessenichb3297152015-07-11 18:01:03 -06001142 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001143 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001144 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001145 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001146
John Kessenicha0af4732012-12-12 21:15:54 +00001147 fdata[count] = '\0';
1148 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001149
John Kessenichea869fb2013-10-28 18:12:06 +00001150 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001151 // recover from empty file
1152 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001153 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001154 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001155 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001156
John Kessenichb3297152015-07-11 18:01:03 -06001157 return return_data;
1158 } else
1159 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1160
1161 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001162 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001163 int ptr_len = 0;
1164 int i = 0;
1165 while (count > 0) {
1166 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1167 memcpy(return_data[i], fdata + ptr_len, len);
1168 return_data[i][len] = '\0';
1169 count -= len;
1170 ptr_len += len;
1171 if (count < len) {
1172 if (count == 0) {
1173 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001174 break;
1175 }
John Kessenichb3297152015-07-11 18:01:03 -06001176 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001177 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001178 ++i;
1179 }
John Kessenichb3297152015-07-11 18:01:03 -06001180
1181 free(fdata);
1182
John Kessenicha0af4732012-12-12 21:15:54 +00001183 return return_data;
1184}
1185
John Kessenich51cdd902014-02-18 23:37:57 +00001186void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001187{
John Kessenichb3297152015-07-11 18:01:03 -06001188 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001189 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001190
1191 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001192}
1193
John Kessenich54d8cda2013-02-11 22:36:01 +00001194void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001195{
John Kessenichfae38ee2015-06-10 23:23:12 +00001196 if (num >= 0 )
1197 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1198 else
1199 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001200}