blob: d713e760d93ad0c1a1aa37f0b2f437afd42d6103 [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 Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060045#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000046#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000047#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060048#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000049#include "../SPIRV/doc.h"
50#include "../SPIRV/disassemble.h"
John Kessenich66ec80e2016-08-05 14:04:23 -060051#include <cstring>
52#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060053#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060054#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <array>
Juan Lopeza558b262017-04-02 23:04:00 +020056#include <memory>
57#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000058
baldurk876a0e32015-11-16 18:03:28 +010059#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000060
61extern "C" {
62 SH_IMPORT_EXPORT void ShOutputHtml();
63}
64
John Kessenich94a81fb2013-08-31 02:41:30 +000065// Command-line options
66enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070067 EOptionNone = 0,
68 EOptionIntermediate = (1 << 0),
69 EOptionSuppressInfolog = (1 << 1),
70 EOptionMemoryLeakMode = (1 << 2),
71 EOptionRelaxedErrors = (1 << 3),
72 EOptionGiveWarnings = (1 << 4),
73 EOptionLinkProgram = (1 << 5),
74 EOptionMultiThreaded = (1 << 6),
75 EOptionDumpConfig = (1 << 7),
76 EOptionDumpReflection = (1 << 8),
77 EOptionSuppressWarnings = (1 << 9),
78 EOptionDumpVersions = (1 << 10),
79 EOptionSpv = (1 << 11),
80 EOptionHumanReadableSpv = (1 << 12),
81 EOptionVulkanRules = (1 << 13),
82 EOptionDefaultDesktop = (1 << 14),
83 EOptionOutputPreprocessed = (1 << 15),
84 EOptionOutputHexadecimal = (1 << 16),
85 EOptionReadHlsl = (1 << 17),
86 EOptionCascadingErrors = (1 << 18),
87 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060088 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070089 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070090 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060091 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060092 EOptionHlslIoMapping = (1 << 24),
John Kessenich94a81fb2013-08-31 02:41:30 +000093};
94
John Kessenicha0af4732012-12-12 21:15:54 +000095//
John Kessenich68d78fd2015-07-12 19:28:10 -060096// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000097//
98enum TFailCode {
99 ESuccess = 0,
100 EFailUsage,
101 EFailCompile,
102 EFailLink,
103 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000104 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000105 EFailLinkerCreate
106};
107
108//
John Kessenich68d78fd2015-07-12 19:28:10 -0600109// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000110//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600111EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000112void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000113void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000114void FreeFileData(char** data);
115char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000116void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000117
John Kessenichc999ba22013-11-07 23:33:24 +0000118// Globally track if any compile or link failure.
119bool CompileFailed = false;
120bool LinkFailed = false;
121
John Kessenich05a70632013-09-17 19:26:08 +0000122// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600123// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000124int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000125
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 Kessenich05a70632013-09-17 19:26:08 +0000134 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000135 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000136 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000137 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000138 if (configStrings)
139 config = *configStrings;
140 else {
141 printf("Error opening configuration file; will instead use the default configuration\n");
142 usage();
143 }
144 }
145
146 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500147 Resources = glslang::DefaultTBuiltInResource;
148 return;
John Kessenich05a70632013-09-17 19:26:08 +0000149 }
150
Lei Zhang1b141722016-05-19 13:50:49 -0400151 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000152
John Kessenich05a70632013-09-17 19:26:08 +0000153 if (configStrings)
154 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500155 else
156 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000157}
158
John Kessenich94a81fb2013-08-31 02:41:30 +0000159int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600160const char* ExecutableName = nullptr;
161const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700162const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600163const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400164const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800165const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600166
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600167std::array<unsigned int, EShLangCount> baseSamplerBinding;
168std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600169std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600170std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700171std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600172std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900173std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600174
John Kessenich68d78fd2015-07-12 19:28:10 -0600175//
176// Create the default name for saving a binary if -o is not provided.
177//
178const char* GetBinaryName(EShLanguage stage)
179{
180 const char* name;
181 if (binaryFileName == nullptr) {
182 switch (stage) {
183 case EShLangVertex: name = "vert.spv"; break;
184 case EShLangTessControl: name = "tesc.spv"; break;
185 case EShLangTessEvaluation: name = "tese.spv"; break;
186 case EShLangGeometry: name = "geom.spv"; break;
187 case EShLangFragment: name = "frag.spv"; break;
188 case EShLangCompute: name = "comp.spv"; break;
189 default: name = "unknown"; break;
190 }
191 } else
192 name = binaryFileName;
193
194 return name;
195}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000196
John Kessenich05a70632013-09-17 19:26:08 +0000197//
198// *.conf => this is a config file that can set limits/resources
199//
200bool SetConfigFile(const std::string& name)
201{
202 if (name.size() < 5)
203 return false;
204
John Kessenich4c706852013-10-11 16:28:43 +0000205 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000206 ConfigFile = name;
207 return true;
208 }
209
210 return false;
211}
212
John Kessenich68d78fd2015-07-12 19:28:10 -0600213//
214// Give error and exit with failure code.
215//
216void Error(const char* message)
217{
218 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
219 exit(EFailUsage);
220}
221
222//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600223// Process an optional binding base of the form:
224// --argname [stage] base
225// Where stage is one of the forms accepted by FindLanguage, and base is an integer
226//
227void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
228{
229 if (argc < 2)
230 usage();
231
232 if (!isdigit(argv[1][0])) {
233 if (argc < 3) // this form needs one more argument
234 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700235
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600236 // Parse form: --argname stage base
237 const EShLanguage lang = FindLanguage(argv[1], false);
238 base[lang] = atoi(argv[2]);
239 argc-= 2;
240 argv+= 2;
241 } else {
242 // Parse form: --argname base
243 for (int lang=0; lang<EShLangCount; ++lang)
244 base[lang] = atoi(argv[1]);
245
246 argc--;
247 argv++;
248 }
249}
250
Hyangran Park36dc8292017-05-02 16:27:29 +0900251void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
252{
253 if (argc < 2)
254 usage();
255
256 if (!isdigit(argv[1][0])) {
257 if (argc < 5) // this form needs one more argument
258 usage();
259
260 // Parse form: --argname stage base
261 const EShLanguage lang = FindLanguage(argv[1], false);
262
263 base[lang].push_back(argv[2]);
264 base[lang].push_back(argv[3]);
265 base[lang].push_back(argv[4]);
266 argc-= 4;
267 argv+= 4;
268 while(argv[1] != NULL) {
269 if(argv[1][0] != '-') {
270 base[lang].push_back(argv[1]);
271 base[lang].push_back(argv[2]);
272 base[lang].push_back(argv[3]);
273 argc-= 3;
274 argv+= 3;
275 }
276 else {
277 break;
278 }
279 }
280 } else {
281 // Parse form: --argname base
282 for (int lang=0; lang<EShLangCount; ++lang)
283 base[lang].push_back(argv[1]);
284
285 argc--;
286 argv++;
287 }
288}
289
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600290//
John Kessenich68d78fd2015-07-12 19:28:10 -0600291// Do all command-line argument parsing. This includes building up the work-items
292// to be processed later, and saving all the command-line options.
293//
294// Does not return (it exits) if command-line is fatally flawed.
295//
Juan Lopeza558b262017-04-02 23:04:00 +0200296void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000297{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600298 baseSamplerBinding.fill(0);
299 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600300 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600301 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700302 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600303 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600304
John Kessenich38f3b892013-09-06 19:52:57 +0000305 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200306 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000307
John Kessenich2b07c7e2013-07-31 18:44:13 +0000308 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700309 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000310 for (; argc >= 1; argc--, argv++) {
311 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000312 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600313 case '-':
314 {
315 std::string lowerword(argv[0]+2);
316 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
317
318 // handle --word style options
319 if (lowerword == "shift-sampler-bindings" || // synonyms
320 lowerword == "shift-sampler-binding" ||
321 lowerword == "ssb") {
322 ProcessBindingBase(argc, argv, baseSamplerBinding);
323 } else if (lowerword == "shift-texture-bindings" || // synonyms
324 lowerword == "shift-texture-binding" ||
325 lowerword == "stb") {
326 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600327 } else if (lowerword == "shift-image-bindings" || // synonyms
328 lowerword == "shift-image-binding" ||
329 lowerword == "sib") {
330 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600331 } else if (lowerword == "shift-ubo-bindings" || // synonyms
332 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600333 lowerword == "shift-cbuffer-bindings" ||
334 lowerword == "shift-cbuffer-binding" ||
335 lowerword == "sub" ||
336 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600337 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700338 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
339 lowerword == "shift-ssbo-binding" ||
340 lowerword == "sbb") {
341 ProcessBindingBase(argc, argv, baseSsboBinding);
Hyangran Park36dc8292017-05-02 16:27:29 +0900342 } else if (lowerword == "resource-set-bindings" || // synonyms
343 lowerword == "resource-set-binding" ||
344 lowerword == "rsb") {
345 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600346 } else if (lowerword == "shift-uav-bindings" || // synonyms
347 lowerword == "shift-uav-binding" ||
348 lowerword == "suavb") {
349 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600350 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700351 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600352 lowerword == "amb") {
353 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600354 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
355 lowerword == "flatten-uniform-array" ||
356 lowerword == "fua") {
357 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600358 } else if (lowerword == "no-storage-format" || // synonyms
359 lowerword == "nsf") {
360 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800361 } else if (lowerword == "variable-name" || // synonyms
362 lowerword == "vn") {
363 Options |= EOptionOutputHexadecimal;
364 variableName = argv[1];
365 if (argc > 0) {
366 argc--;
367 argv++;
368 } else
369 Error("no <C-variable-name> provided for --variable-name");
370 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600371 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600372 lowerword == "sep") {
373 sourceEntryPointName = argv[1];
374 if (argc > 0) {
375 argc--;
376 argv++;
377 } else
378 Error("no <entry-point> provided for --source-entrypoint");
379 break;
John Kessenich906cc212016-12-09 19:22:20 -0700380 } else if (lowerword == "keep-uncalled" || // synonyms
381 lowerword == "ku") {
382 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600383 } else if (lowerword == "hlsl-offsets") {
384 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600385 } else if (lowerword == "hlsl-iomap" ||
386 lowerword == "hlsl-iomapper" ||
387 lowerword == "hlsl-iomapping") {
388 Options |= EOptionHlslIoMapping;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600389 } else {
390 usage();
391 }
392 }
393 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000394 case 'H':
395 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600396 if ((Options & EOptionSpv) == 0) {
397 // default to Vulkan
398 Options |= EOptionSpv;
399 Options |= EOptionVulkanRules;
400 Options |= EOptionLinkProgram;
401 }
402 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000403 case 'V':
404 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600405 Options |= EOptionVulkanRules;
406 Options |= EOptionLinkProgram;
407 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400408 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400409 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400410 if (argc > 0) {
411 argc--;
412 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600413 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400414 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400415 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600416 case 'G':
417 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000418 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600419 // undo a -H default to Vulkan
420 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000421 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000422 case 'E':
423 Options |= EOptionOutputPreprocessed;
424 break;
John Kessenich05a70632013-09-17 19:26:08 +0000425 case 'c':
426 Options |= EOptionDumpConfig;
427 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600428 case 'C':
429 Options |= EOptionCascadingErrors;
430 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000431 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000432 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000433 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700434 case 'D':
435 Options |= EOptionReadHlsl;
436 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700437 case 'e':
438 // HLSL todo: entry point handle needs much more sophistication.
439 // This is okay for one compilation unit with one entry point.
440 entryPointName = argv[1];
441 if (argc > 0) {
442 argc--;
443 argv++;
444 } else
445 Error("no <entry-point> provided for -e");
446 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600447 case 'h':
448 usage();
449 break;
John Kessenich05a70632013-09-17 19:26:08 +0000450 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000451 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000452 break;
453 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000454 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000455 break;
456 case 'm':
457 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000458 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600459 case 'o':
460 binaryFileName = argv[1];
461 if (argc > 0) {
462 argc--;
463 argv++;
464 } else
465 Error("no <file> provided for -o");
466 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000467 case 'q':
468 Options |= EOptionDumpReflection;
469 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000470 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000471 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000472 break;
473 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000474 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000475 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000476 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200477 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000478 break;
John Kessenich319de232013-12-04 04:43:40 +0000479 case 'v':
480 Options |= EOptionDumpVersions;
481 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000482 case 'w':
483 Options |= EOptionSuppressWarnings;
484 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500485 case 'x':
486 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500487 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000488 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600489 usage();
490 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000491 }
John Kessenich38f3b892013-09-06 19:52:57 +0000492 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000493 std::string name(argv[0]);
494 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200495 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000496 }
John Kessenich38f3b892013-09-06 19:52:57 +0000497 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498 }
499
John Kessenich68d78fd2015-07-12 19:28:10 -0600500 // Make sure that -E is not specified alongside linking (which includes SPV generation)
501 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
502 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000503
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500504 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600505 if (binaryFileName && (Options & EOptionSpv) == 0)
506 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600507
508 if ((Options & EOptionFlattenUniformArrays) != 0 &&
509 (Options & EOptionReadHlsl) == 0)
510 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000511}
512
John Kessenich68d78fd2015-07-12 19:28:10 -0600513//
514// Translate the meaningful subset of command-line options to parser-behavior options.
515//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000516void SetMessageOptions(EShMessages& messages)
517{
518 if (Options & EOptionRelaxedErrors)
519 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
520 if (Options & EOptionIntermediate)
521 messages = (EShMessages)(messages | EShMsgAST);
522 if (Options & EOptionSuppressWarnings)
523 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600524 if (Options & EOptionSpv)
525 messages = (EShMessages)(messages | EShMsgSpvRules);
526 if (Options & EOptionVulkanRules)
527 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400528 if (Options & EOptionOutputPreprocessed)
529 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700530 if (Options & EOptionReadHlsl)
531 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600532 if (Options & EOptionCascadingErrors)
533 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700534 if (Options & EOptionKeepUncalled)
535 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600536 if (Options & EOptionHlslOffsets)
537 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000538}
539
John Kessenich68d78fd2015-07-12 19:28:10 -0600540//
John Kessenich69f4b512013-09-04 21:19:27 +0000541// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000542//
Juan Lopeza558b262017-04-02 23:04:00 +0200543void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000544{
John Kessenich38f3b892013-09-06 19:52:57 +0000545 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200546 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000547 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000548 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200549 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000550
John Kessenichb0a7eb52013-11-07 17:44:20 +0000551 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000552
John Kessenich94a81fb2013-08-31 02:41:30 +0000553 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000554 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000555
556 ShDestruct(compiler);
557 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000558}
559
John Kessenich6626cad2015-06-19 05:14:19 +0000560// Outputs the given string, but only if it is non-null and non-empty.
561// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400562void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000563{
564 if (str && str[0]) {
565 puts(str);
566 }
567}
568
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400569// Outputs the given string to stderr, but only if it is non-null and non-empty.
570// This prevents erroneous newlines from appearing.
571void StderrIfNonEmpty(const char* str)
572{
573 if (str && str[0]) {
574 fprintf(stderr, "%s\n", str);
575 }
576}
577
John Kessenichc57b2a92016-01-16 15:30:03 -0700578// Simple bundling of what makes a compilation unit for ease in passing around,
579// and separation of handling file IO versus API (programmatic) compilation.
580struct ShaderCompUnit {
581 EShLanguage stage;
582 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400583 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600584 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400585
John Kessenich219b0252016-08-23 17:51:13 -0600586 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400587 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
588 {
589 stage = istage;
590 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600591 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600592 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400593 }
594
595 ShaderCompUnit(const ShaderCompUnit &rhs)
596 {
597 stage = rhs.stage;
598 fileName = rhs.fileName;
599 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600600 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400601 }
602
John Kessenichc57b2a92016-01-16 15:30:03 -0700603};
604
John Kessenich69f4b512013-09-04 21:19:27 +0000605//
John Kessenichc57b2a92016-01-16 15:30:03 -0700606// For linking mode: Will independently parse each compilation unit, but then put them
607// in the same program and link them together, making at most one linked module per
608// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000609//
610// Uses the new C++ interface instead of the old handle-based interface.
611//
John Kessenichc57b2a92016-01-16 15:30:03 -0700612
613void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000614{
615 // keep track of what to free
616 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700617
John Kessenich69f4b512013-09-04 21:19:27 +0000618 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000619 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000620
John Kessenich69f4b512013-09-04 21:19:27 +0000621 //
622 // Per-shader processing...
623 //
624
John Kessenich5b0f13a2013-11-01 03:08:40 +0000625 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100626 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
627 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700628 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400629 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700630 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
631 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600632 if (sourceEntryPointName)
633 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600634
635 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
636 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600637 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600638 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700639 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600640 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600641 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600642 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900643 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600644
steve-lunargbe283552017-04-18 12:18:01 -0600645 if (Options & EOptionHlslIoMapping)
646 shader->setHlslIoMapping(true);
647
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600648 if (Options & EOptionAutoMapBindings)
649 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700650
John Kessenich69f4b512013-09-04 21:19:27 +0000651 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600652
John Kessenichc555ddd2015-06-17 02:38:44 +0000653 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000654
John Kessenichc555ddd2015-06-17 02:38:44 +0000655 if (Options & EOptionOutputPreprocessed) {
656 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700657 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400658 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500659 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400660 PutsIfNonEmpty(str.c_str());
661 } else {
662 CompileFailed = true;
663 }
664 StderrIfNonEmpty(shader->getInfoLog());
665 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000666 continue;
667 }
668 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000669 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000670
John Kessenich69f4b512013-09-04 21:19:27 +0000671 program.addShader(shader);
672
John Kessenichc57b2a92016-01-16 15:30:03 -0700673 if (! (Options & EOptionSuppressInfolog) &&
674 ! (Options & EOptionMemoryLeakMode)) {
675 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400676 PutsIfNonEmpty(shader->getInfoLog());
677 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000678 }
John Kessenich69f4b512013-09-04 21:19:27 +0000679 }
680
681 //
682 // Program-level processing...
683 //
684
John Kessenich4d65ee32016-03-12 18:17:47 -0700685 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600686 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000687 LinkFailed = true;
688
steve-lunarg9ae34742016-10-05 13:40:13 -0600689 // Map IO
690 if (Options & EOptionSpv) {
691 if (!program.mapIO())
692 LinkFailed = true;
693 }
John Kessenichecba76f2017-01-06 00:34:48 -0700694
John Kessenich4d65ee32016-03-12 18:17:47 -0700695 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700696 if (! (Options & EOptionSuppressInfolog) &&
697 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400698 PutsIfNonEmpty(program.getInfoLog());
699 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000700 }
701
John Kessenich4d65ee32016-03-12 18:17:47 -0700702 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000703 if (Options & EOptionDumpReflection) {
704 program.buildReflection();
705 program.dumpReflection();
706 }
707
John Kessenich4d65ee32016-03-12 18:17:47 -0700708 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000709 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000710 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600711 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000712 else {
713 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000714 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000715 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400716 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400717 spv::SpvBuildLogger logger;
718 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700719
720 // Dump the spv to a file or stdout, etc., but only if not doing
721 // memory/perf testing, as it's not internal to programmatic use.
722 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500723 printf("%s", logger.getAllMessages().c_str());
724 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700725 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500726 } else {
727 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
728 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700729 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700730 spv::Disassemble(std::cout, spirv);
731 }
John Kessenichacba7722015-03-04 03:48:38 +0000732 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000733 }
John Kessenich92f90382014-07-28 04:21:04 +0000734 }
735 }
736 }
737
John Kessenich5b0f13a2013-11-01 03:08:40 +0000738 // Free everything up, program has to go before the shaders
739 // because it might have merged stuff from the shaders, and
740 // the stuff from the shaders has to have its destructors called
741 // before the pools holding the memory in the shaders is freed.
742 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000743 while (shaders.size() > 0) {
744 delete shaders.back();
745 shaders.pop_back();
746 }
John Kessenich69f4b512013-09-04 21:19:27 +0000747}
748
John Kessenichc57b2a92016-01-16 15:30:03 -0700749//
750// Do file IO part of compile and link, handing off the pure
751// API/programmatic mode to CompileAndLinkShaderUnits(), which can
752// be put in a loop for testing memory footprint and performance.
753//
754// This is just for linking mode: meaning all the shaders will be put into the
755// the same program linked together.
756//
757// This means there are a limited number of work items (not multi-threading mode)
758// and that the point is testing at the linking level. Hence, to enable
759// performance and memory testing, the actual compile/link can be put in
760// a loop, independent of processing the work items and file IO.
761//
Juan Lopeza558b262017-04-02 23:04:00 +0200762void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700763{
764 std::vector<ShaderCompUnit> compUnits;
765
766 // Transfer all the work items from to a simple list of
767 // of compilation units. (We don't care about the thread
768 // work-item distribution properties in this path, which
769 // is okay due to the limited number of shaders, know since
770 // they are all getting linked together.)
771 glslang::TWorkItem* workItem;
772 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200773 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700774 FindLanguage(workItem->name),
775 workItem->name,
776 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200777 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700778
779 if (! compUnit.text) {
780 usage();
781 return;
782 }
783
784 compUnits.push_back(compUnit);
785 }
786
787 // Actual call to programmatic processing of compile and link,
788 // in a loop for testing memory and performance. This part contains
789 // all the perf/memory that a programmatic consumer will care about.
790 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
791 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
792 CompileAndLinkShaderUnits(compUnits);
793
794 if (Options & EOptionMemoryLeakMode)
795 glslang::OS_DumpMemoryCounters();
796 }
797
rdb32084e82016-02-23 22:17:38 +0100798 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
799 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700800}
801
John Kessenicha0af4732012-12-12 21:15:54 +0000802int C_DECL main(int argc, char* argv[])
803{
Juan Lopeza558b262017-04-02 23:04:00 +0200804 // array of unique places to leave the shader names and infologs for the asynchronous compiles
805 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
806 ProcessArguments(workItems, argc, argv);
807
808 glslang::TWorklist workList;
809 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
810 assert(item);
811 workList.add(item.get());
812 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000813
John Kessenich05a70632013-09-17 19:26:08 +0000814 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500815 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200816 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000817 return ESuccess;
818 }
819
John Kessenich0da9eaa2015-08-01 17:10:02 -0600820 if (Options & EOptionDumpVersions) {
821 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000822 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
823 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600824 std::string spirvVersion;
825 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600826 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600827 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700828 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600829 printf("GL_KHR_vulkan_glsl version %d\n", 100);
830 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200831 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000832 return ESuccess;
833 }
834
Juan Lopeza558b262017-04-02 23:04:00 +0200835 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000836 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000837 }
838
839 ProcessConfigFile();
840
John Kessenich69f4b512013-09-04 21:19:27 +0000841 //
842 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000843 // 1) linking all arguments together, single-threaded, new C++ interface
844 // 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 +0000845 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000846 if (Options & EOptionLinkProgram ||
847 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000848 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200849 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000850 glslang::FinalizeProcess();
851 } else {
852 ShInitialize();
853
Juan Lopeza558b262017-04-02 23:04:00 +0200854 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000855
Juan Lopeza558b262017-04-02 23:04:00 +0200856 if (Options & EOptionMultiThreaded)
857 {
858 std::array<std::thread, 16> threads;
859 for (unsigned int t = 0; t < threads.size(); ++t)
860 {
861 threads[t] = std::thread(CompileShaders, std::ref(workList));
862 if (threads[t].get_id() == std::thread::id())
863 {
John Kessenich38f3b892013-09-06 19:52:57 +0000864 printf("Failed to create thread\n");
865 return EFailThreadCreate;
866 }
John Kessenicha0af4732012-12-12 21:15:54 +0000867 }
Juan Lopeza558b262017-04-02 23:04:00 +0200868
869 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000870 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200871 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000872
873 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200874 for (size_t w = 0; w < workItems.size(); ++w) {
875 if (workItems[w]) {
876 if (printShaderNames || workItems[w]->results.size() > 0)
877 PutsIfNonEmpty(workItems[w]->name.c_str());
878 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000879 }
880 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000881
882 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000883 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000884
John Kessenichc999ba22013-11-07 23:33:24 +0000885 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000886 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000887 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000888 return EFailLink;
889
890 return 0;
891}
892
893//
894// Deduce the language from the filename. Files must end in one of the
895// following extensions:
896//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000897// .vert = vertex
898// .tesc = tessellation control
899// .tese = tessellation evaluation
900// .geom = geometry
901// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000902// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000903//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600904EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000905{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600906 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600907 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600908
Dan Bakerc6ede892016-08-11 14:06:06 -0400909 if (shaderStageName)
910 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600911 else {
912 // Search for a suffix on a filename: e.g, "myfile.frag". If given
913 // the suffix directly, we skip looking for the '.'
914 if (parseSuffix) {
915 ext = name.rfind('.');
916 if (ext == std::string::npos) {
917 usage();
918 return EShLangVertex;
919 }
920 ++ext;
921 }
922 suffix = name.substr(ext, std::string::npos);
923 }
dankbaker45d49bc2016-08-08 21:43:07 -0400924
John Kessenich2b07c7e2013-07-31 18:44:13 +0000925 if (suffix == "vert")
926 return EShLangVertex;
927 else if (suffix == "tesc")
928 return EShLangTessControl;
929 else if (suffix == "tese")
930 return EShLangTessEvaluation;
931 else if (suffix == "geom")
932 return EShLangGeometry;
933 else if (suffix == "frag")
934 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000935 else if (suffix == "comp")
936 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000937
938 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000939 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000940}
941
John Kessenicha0af4732012-12-12 21:15:54 +0000942//
John Kessenichecba76f2017-01-06 00:34:48 -0700943// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000944// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000945//
John Kessenich51cdd902014-02-18 23:37:57 +0000946void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000947{
John Kessenichca3457f2015-05-18 01:59:45 +0000948 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000949 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000950 if (! shaderStrings) {
951 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000952 }
953
John Kessenich41cf6b52013-06-25 18:10:05 +0000954 int* lengths = new int[NumShaderStrings];
955
956 // move to length-based strings, rather than null-terminated strings
957 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000958 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000959
John Kessenichc999ba22013-11-07 23:33:24 +0000960 if (! shaderStrings) {
961 CompileFailed = true;
962 return;
963 }
John Kessenicha0af4732012-12-12 21:15:54 +0000964
John Kessenich52ac67e2013-05-05 23:46:22 +0000965 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000966 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700967
John Kessenich94a81fb2013-08-31 02:41:30 +0000968 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
969 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700970 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000971 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700972 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700973 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000974 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700975 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
976 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000977 }
John Kessenicha0af4732012-12-12 21:15:54 +0000978
John Kessenich94a81fb2013-08-31 02:41:30 +0000979 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000980 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000981 }
John Kessenicha0af4732012-12-12 21:15:54 +0000982
John Kessenich41cf6b52013-06-25 18:10:05 +0000983 delete [] lengths;
984 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000985
John Kessenichc999ba22013-11-07 23:33:24 +0000986 if (ret == 0)
987 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000988}
989
John Kessenicha0af4732012-12-12 21:15:54 +0000990//
991// print usage to stdout
992//
993void usage()
994{
John Kessenich319de232013-12-04 04:43:40 +0000995 printf("Usage: glslangValidator [option]... [file]...\n"
996 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000997 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600998 " .conf to provide an optional config file that replaces the default configuration\n"
999 " (see -c option below for generating a template)\n"
1000 " .vert for a vertex shader\n"
1001 " .tesc for a tessellation control shader\n"
1002 " .tese for a tessellation evaluation shader\n"
1003 " .geom for a geometry shader\n"
1004 " .frag for a fragment shader\n"
1005 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001006 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001007 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +00001008 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001009 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001010 "Each option must be specified separately.\n"
1011 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1012 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001013 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1014 " default file name is <stage>.spv (-o overrides this)\n"
1015 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001016 " -E print pre-processed GLSL; cannot be used with -l;\n"
1017 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001018 " -S <stage> uses specified stage rather than parsing the file extension\n"
1019 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001020 " -c configuration dump;\n"
1021 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -06001022 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001023 " -d default to desktop (#version 110) when there is no shader #version\n"
1024 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -07001025 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001026 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001027 " -h print this usage message\n"
1028 " -i intermediate tree (glslang AST) is printed out\n"
1029 " -l link all input files together to form a single module\n"
1030 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001031 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001032 " -q dump reflection query database\n"
1033 " -r relaxed semantic error-checking mode\n"
1034 " -s silent mode\n"
1035 " -t multi-threaded mode\n"
1036 " -v print version strings\n"
1037 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -05001038 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001039 "\n"
1040 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
1041 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1042 "\n"
1043 " --shift-texture-binding [stage] num set base binding number for textures\n"
1044 " --stb [stage] num synonym for --shift-texture-binding\n"
1045 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -06001046 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1047 " --sib [stage] num synonym for --shift-image-binding\n"
1048 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001049 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001050 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001051 " --sub [stage] num synonym for --shift-UBO-binding\n"
1052 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001053 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1054 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1055 "\n"
Hyangran Park36dc8292017-05-02 16:27:29 +09001056 " --resource-set-binding [stage] num set descriptor set and binding number for resources\n"
1057 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1058 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001059 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1060 " --suavb [stage] num synonym for --shift-uav-binding\n"
1061 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001062 " --auto-map-bindings automatically bind uniform variables without\n"
1063 " explicit bindings.\n"
1064 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001065 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001066 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001067 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001068 "\n"
1069 " --no-storage-format use Unknown image format\n"
1070 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001071 "\n"
1072 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1073 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001074 "\n"
1075 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1076 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001077 "\n"
1078 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1079 " initialized with the shader binary code.\n"
1080 " --vn <name> synonym for --variable-name <name>\n"
1081 "\n"
1082 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1083 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001084 "\n"
1085 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001086 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001087
1088 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001089}
1090
John Kessenich3ce4e592014-10-06 19:57:34 +00001091#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001092
1093#include <errno.h>
1094
1095int fopen_s(
1096 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001097 const char* filename,
1098 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001099)
1100{
1101 if (!pFile || !filename || !mode) {
1102 return EINVAL;
1103 }
1104
1105 FILE* f = fopen(filename, mode);
1106 if (! f) {
1107 if (errno != 0) {
1108 return errno;
1109 } else {
1110 return ENOENT;
1111 }
1112 }
1113 *pFile = f;
1114
1115 return 0;
1116}
1117
1118#endif
1119
John Kessenicha0af4732012-12-12 21:15:54 +00001120//
1121// Malloc a string of sufficient size and read a string into it.
1122//
John Kessenichecba76f2017-01-06 00:34:48 -07001123char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001124{
John Kessenichb3297152015-07-11 18:01:03 -06001125 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001126 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001127
John Kessenicha0af4732012-12-12 21:15:54 +00001128 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001129 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1130 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001131
John Kessenich68d78fd2015-07-12 19:28:10 -06001132 if (errorCode || in == nullptr)
1133 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001134
John Kessenicha0af4732012-12-12 21:15:54 +00001135 while (fgetc(in) != EOF)
1136 count++;
1137
John Kessenichd6c72a42014-08-18 19:42:35 +00001138 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001139
John Kessenichb3297152015-07-11 18:01:03 -06001140 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001141 if (! fdata)
1142 Error("can't allocate memory");
1143
John Kessenichb3297152015-07-11 18:01:03 -06001144 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001145 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001146 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001147 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001148
John Kessenicha0af4732012-12-12 21:15:54 +00001149 fdata[count] = '\0';
1150 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001151
John Kessenichea869fb2013-10-28 18:12:06 +00001152 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001153 // recover from empty file
1154 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001155 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001156 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001157 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001158
John Kessenichb3297152015-07-11 18:01:03 -06001159 return return_data;
1160 } else
1161 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1162
1163 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001164 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001165 int ptr_len = 0;
1166 int i = 0;
1167 while (count > 0) {
1168 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1169 memcpy(return_data[i], fdata + ptr_len, len);
1170 return_data[i][len] = '\0';
1171 count -= len;
1172 ptr_len += len;
1173 if (count < len) {
1174 if (count == 0) {
1175 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001176 break;
1177 }
John Kessenichb3297152015-07-11 18:01:03 -06001178 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001179 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001180 ++i;
1181 }
John Kessenichb3297152015-07-11 18:01:03 -06001182
1183 free(fdata);
1184
John Kessenicha0af4732012-12-12 21:15:54 +00001185 return return_data;
1186}
1187
John Kessenich51cdd902014-02-18 23:37:57 +00001188void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001189{
John Kessenichb3297152015-07-11 18:01:03 -06001190 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001191 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001192
1193 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001194}
1195
John Kessenich54d8cda2013-02-11 22:36:01 +00001196void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001197{
John Kessenichfae38ee2015-06-10 23:23:12 +00001198 if (num >= 0 )
1199 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1200 else
1201 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001202}