blob: ef47662cd12f99064102e8bd4c98b442c3d122d2 [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 Kessenich71facdf2017-05-17 18:28:19 -060093 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060094 EOptionDebug = (1 << 26),
John Kessenich94a81fb2013-08-31 02:41:30 +000095};
96
John Kessenicha0af4732012-12-12 21:15:54 +000097//
John Kessenich68d78fd2015-07-12 19:28:10 -060098// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000099//
100enum TFailCode {
101 ESuccess = 0,
102 EFailUsage,
103 EFailCompile,
104 EFailLink,
105 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000106 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000107 EFailLinkerCreate
108};
109
110//
John Kessenich68d78fd2015-07-12 19:28:10 -0600111// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000112//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600113EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000114void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000115void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000116void FreeFileData(char** data);
117char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000118void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000119
John Kessenichc999ba22013-11-07 23:33:24 +0000120// Globally track if any compile or link failure.
121bool CompileFailed = false;
122bool LinkFailed = false;
123
John Kessenich05a70632013-09-17 19:26:08 +0000124// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600125// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000126int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000127
John Kessenich05a70632013-09-17 19:26:08 +0000128TBuiltInResource Resources;
129std::string ConfigFile;
130
John Kessenicha0af4732012-12-12 21:15:54 +0000131//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600132// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000133//
134void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000135{
John Kessenich05a70632013-09-17 19:26:08 +0000136 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000137 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000138 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000139 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000140 if (configStrings)
141 config = *configStrings;
142 else {
143 printf("Error opening configuration file; will instead use the default configuration\n");
144 usage();
145 }
146 }
147
148 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500149 Resources = glslang::DefaultTBuiltInResource;
150 return;
John Kessenich05a70632013-09-17 19:26:08 +0000151 }
152
Lei Zhang1b141722016-05-19 13:50:49 -0400153 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000154
John Kessenich05a70632013-09-17 19:26:08 +0000155 if (configStrings)
156 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500157 else
158 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000159}
160
John Kessenich94a81fb2013-08-31 02:41:30 +0000161int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600162const char* ExecutableName = nullptr;
163const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700164const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600165const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400166const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800167const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600168
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600169std::array<unsigned int, EShLangCount> baseSamplerBinding;
170std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600171std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600172std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700173std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600174std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900175std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600176
John Kessenich68d78fd2015-07-12 19:28:10 -0600177//
178// Create the default name for saving a binary if -o is not provided.
179//
180const char* GetBinaryName(EShLanguage stage)
181{
182 const char* name;
183 if (binaryFileName == nullptr) {
184 switch (stage) {
185 case EShLangVertex: name = "vert.spv"; break;
186 case EShLangTessControl: name = "tesc.spv"; break;
187 case EShLangTessEvaluation: name = "tese.spv"; break;
188 case EShLangGeometry: name = "geom.spv"; break;
189 case EShLangFragment: name = "frag.spv"; break;
190 case EShLangCompute: name = "comp.spv"; break;
191 default: name = "unknown"; break;
192 }
193 } else
194 name = binaryFileName;
195
196 return name;
197}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000198
John Kessenich05a70632013-09-17 19:26:08 +0000199//
200// *.conf => this is a config file that can set limits/resources
201//
202bool SetConfigFile(const std::string& name)
203{
204 if (name.size() < 5)
205 return false;
206
John Kessenich4c706852013-10-11 16:28:43 +0000207 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000208 ConfigFile = name;
209 return true;
210 }
211
212 return false;
213}
214
John Kessenich68d78fd2015-07-12 19:28:10 -0600215//
216// Give error and exit with failure code.
217//
218void Error(const char* message)
219{
220 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
221 exit(EFailUsage);
222}
223
224//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600225// Process an optional binding base of the form:
226// --argname [stage] base
227// Where stage is one of the forms accepted by FindLanguage, and base is an integer
228//
229void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
230{
231 if (argc < 2)
232 usage();
233
234 if (!isdigit(argv[1][0])) {
235 if (argc < 3) // this form needs one more argument
236 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700237
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600238 // Parse form: --argname stage base
239 const EShLanguage lang = FindLanguage(argv[1], false);
240 base[lang] = atoi(argv[2]);
241 argc-= 2;
242 argv+= 2;
243 } else {
244 // Parse form: --argname base
245 for (int lang=0; lang<EShLangCount; ++lang)
246 base[lang] = atoi(argv[1]);
247
248 argc--;
249 argv++;
250 }
251}
252
Hyangran Park36dc8292017-05-02 16:27:29 +0900253void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
254{
255 if (argc < 2)
256 usage();
257
258 if (!isdigit(argv[1][0])) {
259 if (argc < 5) // this form needs one more argument
260 usage();
261
262 // Parse form: --argname stage base
263 const EShLanguage lang = FindLanguage(argv[1], false);
264
265 base[lang].push_back(argv[2]);
266 base[lang].push_back(argv[3]);
267 base[lang].push_back(argv[4]);
268 argc-= 4;
269 argv+= 4;
270 while(argv[1] != NULL) {
271 if(argv[1][0] != '-') {
272 base[lang].push_back(argv[1]);
273 base[lang].push_back(argv[2]);
274 base[lang].push_back(argv[3]);
275 argc-= 3;
276 argv+= 3;
277 }
278 else {
279 break;
280 }
281 }
282 } else {
283 // Parse form: --argname base
284 for (int lang=0; lang<EShLangCount; ++lang)
285 base[lang].push_back(argv[1]);
286
287 argc--;
288 argv++;
289 }
290}
291
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600292//
John Kessenich68d78fd2015-07-12 19:28:10 -0600293// Do all command-line argument parsing. This includes building up the work-items
294// to be processed later, and saving all the command-line options.
295//
296// Does not return (it exits) if command-line is fatally flawed.
297//
Juan Lopeza558b262017-04-02 23:04:00 +0200298void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000299{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600300 baseSamplerBinding.fill(0);
301 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600302 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600303 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700304 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600305 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600306
John Kessenich38f3b892013-09-06 19:52:57 +0000307 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200308 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000309
John Kessenich2b07c7e2013-07-31 18:44:13 +0000310 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700311 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000312 for (; argc >= 1; argc--, argv++) {
313 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000314 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600315 case '-':
316 {
317 std::string lowerword(argv[0]+2);
318 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
319
320 // handle --word style options
321 if (lowerword == "shift-sampler-bindings" || // synonyms
322 lowerword == "shift-sampler-binding" ||
323 lowerword == "ssb") {
324 ProcessBindingBase(argc, argv, baseSamplerBinding);
325 } else if (lowerword == "shift-texture-bindings" || // synonyms
326 lowerword == "shift-texture-binding" ||
327 lowerword == "stb") {
328 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600329 } else if (lowerword == "shift-image-bindings" || // synonyms
330 lowerword == "shift-image-binding" ||
331 lowerword == "sib") {
332 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600333 } else if (lowerword == "shift-ubo-bindings" || // synonyms
334 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600335 lowerword == "shift-cbuffer-bindings" ||
336 lowerword == "shift-cbuffer-binding" ||
337 lowerword == "sub" ||
338 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600339 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700340 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
341 lowerword == "shift-ssbo-binding" ||
342 lowerword == "sbb") {
343 ProcessBindingBase(argc, argv, baseSsboBinding);
Hyangran Park36dc8292017-05-02 16:27:29 +0900344 } else if (lowerword == "resource-set-bindings" || // synonyms
345 lowerword == "resource-set-binding" ||
346 lowerword == "rsb") {
347 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600348 } else if (lowerword == "shift-uav-bindings" || // synonyms
349 lowerword == "shift-uav-binding" ||
350 lowerword == "suavb") {
351 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600352 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700353 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600354 lowerword == "amb") {
355 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600356 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
357 lowerword == "flatten-uniform-array" ||
358 lowerword == "fua") {
359 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600360 } else if (lowerword == "no-storage-format" || // synonyms
361 lowerword == "nsf") {
362 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800363 } else if (lowerword == "variable-name" || // synonyms
364 lowerword == "vn") {
365 Options |= EOptionOutputHexadecimal;
366 variableName = argv[1];
367 if (argc > 0) {
368 argc--;
369 argv++;
370 } else
371 Error("no <C-variable-name> provided for --variable-name");
372 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600373 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600374 lowerword == "sep") {
375 sourceEntryPointName = argv[1];
376 if (argc > 0) {
377 argc--;
378 argv++;
379 } else
380 Error("no <entry-point> provided for --source-entrypoint");
381 break;
John Kessenich906cc212016-12-09 19:22:20 -0700382 } else if (lowerword == "keep-uncalled" || // synonyms
383 lowerword == "ku") {
384 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600385 } else if (lowerword == "hlsl-offsets") {
386 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600387 } else if (lowerword == "hlsl-iomap" ||
388 lowerword == "hlsl-iomapper" ||
389 lowerword == "hlsl-iomapping") {
390 Options |= EOptionHlslIoMapping;
John Kessenich71facdf2017-05-17 18:28:19 -0600391 } else if (lowerword == "auto-map-locations" || // synonyms
392 lowerword == "aml") {
393 Options |= EOptionAutoMapLocations;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600394 } else {
395 usage();
396 }
397 }
398 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000399 case 'H':
400 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600401 if ((Options & EOptionSpv) == 0) {
402 // default to Vulkan
403 Options |= EOptionSpv;
404 Options |= EOptionVulkanRules;
405 Options |= EOptionLinkProgram;
406 }
407 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000408 case 'V':
409 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600410 Options |= EOptionVulkanRules;
411 Options |= EOptionLinkProgram;
412 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400413 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400414 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400415 if (argc > 0) {
416 argc--;
417 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600418 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400419 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400420 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600421 case 'G':
422 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000423 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600424 // undo a -H default to Vulkan
425 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000426 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000427 case 'E':
428 Options |= EOptionOutputPreprocessed;
429 break;
John Kessenich05a70632013-09-17 19:26:08 +0000430 case 'c':
431 Options |= EOptionDumpConfig;
432 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600433 case 'C':
434 Options |= EOptionCascadingErrors;
435 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000436 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000437 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000438 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700439 case 'D':
440 Options |= EOptionReadHlsl;
441 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700442 case 'e':
443 // HLSL todo: entry point handle needs much more sophistication.
444 // This is okay for one compilation unit with one entry point.
445 entryPointName = argv[1];
446 if (argc > 0) {
447 argc--;
448 argv++;
449 } else
450 Error("no <entry-point> provided for -e");
451 break;
John Kessenich121853f2017-05-31 17:11:16 -0600452 case 'g':
453 Options |= EOptionDebug;
454 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600455 case 'h':
456 usage();
457 break;
John Kessenich05a70632013-09-17 19:26:08 +0000458 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000459 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000460 break;
461 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000462 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000463 break;
464 case 'm':
465 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000466 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600467 case 'o':
468 binaryFileName = argv[1];
469 if (argc > 0) {
470 argc--;
471 argv++;
472 } else
473 Error("no <file> provided for -o");
474 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000475 case 'q':
476 Options |= EOptionDumpReflection;
477 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000478 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000479 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000480 break;
481 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000482 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000483 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000484 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200485 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000486 break;
John Kessenich319de232013-12-04 04:43:40 +0000487 case 'v':
488 Options |= EOptionDumpVersions;
489 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000490 case 'w':
491 Options |= EOptionSuppressWarnings;
492 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500493 case 'x':
494 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500495 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000496 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600497 usage();
498 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000499 }
John Kessenich38f3b892013-09-06 19:52:57 +0000500 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000501 std::string name(argv[0]);
502 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200503 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000504 }
John Kessenich38f3b892013-09-06 19:52:57 +0000505 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000506 }
507
John Kessenich68d78fd2015-07-12 19:28:10 -0600508 // Make sure that -E is not specified alongside linking (which includes SPV generation)
509 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
510 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000511
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500512 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600513 if (binaryFileName && (Options & EOptionSpv) == 0)
514 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600515
516 if ((Options & EOptionFlattenUniformArrays) != 0 &&
517 (Options & EOptionReadHlsl) == 0)
518 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000519}
520
John Kessenich68d78fd2015-07-12 19:28:10 -0600521//
522// Translate the meaningful subset of command-line options to parser-behavior options.
523//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000524void SetMessageOptions(EShMessages& messages)
525{
526 if (Options & EOptionRelaxedErrors)
527 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
528 if (Options & EOptionIntermediate)
529 messages = (EShMessages)(messages | EShMsgAST);
530 if (Options & EOptionSuppressWarnings)
531 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600532 if (Options & EOptionSpv)
533 messages = (EShMessages)(messages | EShMsgSpvRules);
534 if (Options & EOptionVulkanRules)
535 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400536 if (Options & EOptionOutputPreprocessed)
537 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700538 if (Options & EOptionReadHlsl)
539 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600540 if (Options & EOptionCascadingErrors)
541 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700542 if (Options & EOptionKeepUncalled)
543 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600544 if (Options & EOptionHlslOffsets)
545 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600546 if (Options & EOptionDebug)
547 messages = (EShMessages)(messages | EShMsgDebugInfo);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000548}
549
John Kessenich68d78fd2015-07-12 19:28:10 -0600550//
John Kessenich69f4b512013-09-04 21:19:27 +0000551// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000552//
Juan Lopeza558b262017-04-02 23:04:00 +0200553void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000554{
John Kessenich38f3b892013-09-06 19:52:57 +0000555 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200556 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000557 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000558 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200559 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000560
John Kessenichb0a7eb52013-11-07 17:44:20 +0000561 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000562
John Kessenich94a81fb2013-08-31 02:41:30 +0000563 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000564 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000565
566 ShDestruct(compiler);
567 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000568}
569
John Kessenich6626cad2015-06-19 05:14:19 +0000570// Outputs the given string, but only if it is non-null and non-empty.
571// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400572void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000573{
574 if (str && str[0]) {
575 puts(str);
576 }
577}
578
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400579// Outputs the given string to stderr, but only if it is non-null and non-empty.
580// This prevents erroneous newlines from appearing.
581void StderrIfNonEmpty(const char* str)
582{
583 if (str && str[0]) {
584 fprintf(stderr, "%s\n", str);
585 }
586}
587
John Kessenichc57b2a92016-01-16 15:30:03 -0700588// Simple bundling of what makes a compilation unit for ease in passing around,
589// and separation of handling file IO versus API (programmatic) compilation.
590struct ShaderCompUnit {
591 EShLanguage stage;
592 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400593 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600594 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400595
John Kessenich219b0252016-08-23 17:51:13 -0600596 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400597 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
598 {
599 stage = istage;
600 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600601 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600602 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400603 }
604
605 ShaderCompUnit(const ShaderCompUnit &rhs)
606 {
607 stage = rhs.stage;
608 fileName = rhs.fileName;
609 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600610 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400611 }
612
John Kessenichc57b2a92016-01-16 15:30:03 -0700613};
614
John Kessenich69f4b512013-09-04 21:19:27 +0000615//
John Kessenichc57b2a92016-01-16 15:30:03 -0700616// For linking mode: Will independently parse each compilation unit, but then put them
617// in the same program and link them together, making at most one linked module per
618// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000619//
620// Uses the new C++ interface instead of the old handle-based interface.
621//
John Kessenichc57b2a92016-01-16 15:30:03 -0700622
623void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000624{
625 // keep track of what to free
626 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700627
John Kessenich69f4b512013-09-04 21:19:27 +0000628 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000629 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000630
John Kessenich69f4b512013-09-04 21:19:27 +0000631 //
632 // Per-shader processing...
633 //
634
John Kessenich5b0f13a2013-11-01 03:08:40 +0000635 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100636 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
637 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700638 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400639 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700640 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
641 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600642 if (sourceEntryPointName)
643 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600644
645 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
646 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600647 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600648 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700649 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600650 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600651 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600652 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900653 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600654
steve-lunargbe283552017-04-18 12:18:01 -0600655 if (Options & EOptionHlslIoMapping)
656 shader->setHlslIoMapping(true);
657
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600658 if (Options & EOptionAutoMapBindings)
659 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700660
John Kessenich71facdf2017-05-17 18:28:19 -0600661 if (Options & EOptionAutoMapLocations)
662 shader->setAutoMapLocations(true);
663
John Kessenich69f4b512013-09-04 21:19:27 +0000664 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600665
John Kessenichc555ddd2015-06-17 02:38:44 +0000666 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000667
John Kessenichc555ddd2015-06-17 02:38:44 +0000668 if (Options & EOptionOutputPreprocessed) {
669 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700670 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400671 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500672 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400673 PutsIfNonEmpty(str.c_str());
674 } else {
675 CompileFailed = true;
676 }
677 StderrIfNonEmpty(shader->getInfoLog());
678 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000679 continue;
680 }
681 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000682 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000683
John Kessenich69f4b512013-09-04 21:19:27 +0000684 program.addShader(shader);
685
John Kessenichc57b2a92016-01-16 15:30:03 -0700686 if (! (Options & EOptionSuppressInfolog) &&
687 ! (Options & EOptionMemoryLeakMode)) {
688 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400689 PutsIfNonEmpty(shader->getInfoLog());
690 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000691 }
John Kessenich69f4b512013-09-04 21:19:27 +0000692 }
693
694 //
695 // Program-level processing...
696 //
697
John Kessenich4d65ee32016-03-12 18:17:47 -0700698 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600699 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000700 LinkFailed = true;
701
steve-lunarg9ae34742016-10-05 13:40:13 -0600702 // Map IO
703 if (Options & EOptionSpv) {
704 if (!program.mapIO())
705 LinkFailed = true;
706 }
John Kessenichecba76f2017-01-06 00:34:48 -0700707
John Kessenich4d65ee32016-03-12 18:17:47 -0700708 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700709 if (! (Options & EOptionSuppressInfolog) &&
710 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400711 PutsIfNonEmpty(program.getInfoLog());
712 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000713 }
714
John Kessenich4d65ee32016-03-12 18:17:47 -0700715 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000716 if (Options & EOptionDumpReflection) {
717 program.buildReflection();
718 program.dumpReflection();
719 }
720
John Kessenich4d65ee32016-03-12 18:17:47 -0700721 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000722 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000723 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600724 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000725 else {
726 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000727 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000728 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400729 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400730 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -0600731 glslang::SpvOptions spvOptions;
732 if (Options & EOptionDebug)
733 spvOptions.generateDebugInfo = true;
734 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -0700735
736 // Dump the spv to a file or stdout, etc., but only if not doing
737 // memory/perf testing, as it's not internal to programmatic use.
738 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500739 printf("%s", logger.getAllMessages().c_str());
740 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700741 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500742 } else {
743 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
744 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700745 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700746 spv::Disassemble(std::cout, spirv);
747 }
John Kessenichacba7722015-03-04 03:48:38 +0000748 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000749 }
John Kessenich92f90382014-07-28 04:21:04 +0000750 }
751 }
752 }
753
John Kessenich5b0f13a2013-11-01 03:08:40 +0000754 // Free everything up, program has to go before the shaders
755 // because it might have merged stuff from the shaders, and
756 // the stuff from the shaders has to have its destructors called
757 // before the pools holding the memory in the shaders is freed.
758 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000759 while (shaders.size() > 0) {
760 delete shaders.back();
761 shaders.pop_back();
762 }
John Kessenich69f4b512013-09-04 21:19:27 +0000763}
764
John Kessenichc57b2a92016-01-16 15:30:03 -0700765//
766// Do file IO part of compile and link, handing off the pure
767// API/programmatic mode to CompileAndLinkShaderUnits(), which can
768// be put in a loop for testing memory footprint and performance.
769//
770// This is just for linking mode: meaning all the shaders will be put into the
771// the same program linked together.
772//
773// This means there are a limited number of work items (not multi-threading mode)
774// and that the point is testing at the linking level. Hence, to enable
775// performance and memory testing, the actual compile/link can be put in
776// a loop, independent of processing the work items and file IO.
777//
Juan Lopeza558b262017-04-02 23:04:00 +0200778void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700779{
780 std::vector<ShaderCompUnit> compUnits;
781
782 // Transfer all the work items from to a simple list of
783 // of compilation units. (We don't care about the thread
784 // work-item distribution properties in this path, which
785 // is okay due to the limited number of shaders, know since
786 // they are all getting linked together.)
787 glslang::TWorkItem* workItem;
788 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200789 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700790 FindLanguage(workItem->name),
791 workItem->name,
792 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200793 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700794
795 if (! compUnit.text) {
796 usage();
797 return;
798 }
799
800 compUnits.push_back(compUnit);
801 }
802
803 // Actual call to programmatic processing of compile and link,
804 // in a loop for testing memory and performance. This part contains
805 // all the perf/memory that a programmatic consumer will care about.
806 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
807 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
808 CompileAndLinkShaderUnits(compUnits);
809
810 if (Options & EOptionMemoryLeakMode)
811 glslang::OS_DumpMemoryCounters();
812 }
813
rdb32084e82016-02-23 22:17:38 +0100814 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
815 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700816}
817
John Kessenicha0af4732012-12-12 21:15:54 +0000818int C_DECL main(int argc, char* argv[])
819{
Juan Lopeza558b262017-04-02 23:04:00 +0200820 // array of unique places to leave the shader names and infologs for the asynchronous compiles
821 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
822 ProcessArguments(workItems, argc, argv);
823
824 glslang::TWorklist workList;
825 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
826 assert(item);
827 workList.add(item.get());
828 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000829
John Kessenich05a70632013-09-17 19:26:08 +0000830 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500831 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200832 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000833 return ESuccess;
834 }
835
John Kessenich0da9eaa2015-08-01 17:10:02 -0600836 if (Options & EOptionDumpVersions) {
837 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000838 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
839 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600840 std::string spirvVersion;
841 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600842 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600843 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700844 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600845 printf("GL_KHR_vulkan_glsl version %d\n", 100);
846 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200847 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000848 return ESuccess;
849 }
850
Juan Lopeza558b262017-04-02 23:04:00 +0200851 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000852 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000853 }
854
855 ProcessConfigFile();
856
John Kessenich69f4b512013-09-04 21:19:27 +0000857 //
858 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000859 // 1) linking all arguments together, single-threaded, new C++ interface
860 // 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 +0000861 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000862 if (Options & EOptionLinkProgram ||
863 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000864 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200865 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000866 glslang::FinalizeProcess();
867 } else {
868 ShInitialize();
869
Juan Lopeza558b262017-04-02 23:04:00 +0200870 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000871
Juan Lopeza558b262017-04-02 23:04:00 +0200872 if (Options & EOptionMultiThreaded)
873 {
874 std::array<std::thread, 16> threads;
875 for (unsigned int t = 0; t < threads.size(); ++t)
876 {
877 threads[t] = std::thread(CompileShaders, std::ref(workList));
878 if (threads[t].get_id() == std::thread::id())
879 {
John Kessenich38f3b892013-09-06 19:52:57 +0000880 printf("Failed to create thread\n");
881 return EFailThreadCreate;
882 }
John Kessenicha0af4732012-12-12 21:15:54 +0000883 }
Juan Lopeza558b262017-04-02 23:04:00 +0200884
885 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000886 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200887 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000888
889 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200890 for (size_t w = 0; w < workItems.size(); ++w) {
891 if (workItems[w]) {
892 if (printShaderNames || workItems[w]->results.size() > 0)
893 PutsIfNonEmpty(workItems[w]->name.c_str());
894 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000895 }
896 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000897
898 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000899 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000900
John Kessenichc999ba22013-11-07 23:33:24 +0000901 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000902 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000903 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000904 return EFailLink;
905
906 return 0;
907}
908
909//
910// Deduce the language from the filename. Files must end in one of the
911// following extensions:
912//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000913// .vert = vertex
914// .tesc = tessellation control
915// .tese = tessellation evaluation
916// .geom = geometry
917// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000918// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000919//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600920EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000921{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600922 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600923 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600924
Dan Bakerc6ede892016-08-11 14:06:06 -0400925 if (shaderStageName)
926 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600927 else {
928 // Search for a suffix on a filename: e.g, "myfile.frag". If given
929 // the suffix directly, we skip looking for the '.'
930 if (parseSuffix) {
931 ext = name.rfind('.');
932 if (ext == std::string::npos) {
933 usage();
934 return EShLangVertex;
935 }
936 ++ext;
937 }
938 suffix = name.substr(ext, std::string::npos);
939 }
dankbaker45d49bc2016-08-08 21:43:07 -0400940
John Kessenich2b07c7e2013-07-31 18:44:13 +0000941 if (suffix == "vert")
942 return EShLangVertex;
943 else if (suffix == "tesc")
944 return EShLangTessControl;
945 else if (suffix == "tese")
946 return EShLangTessEvaluation;
947 else if (suffix == "geom")
948 return EShLangGeometry;
949 else if (suffix == "frag")
950 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000951 else if (suffix == "comp")
952 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000953
954 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000955 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000956}
957
John Kessenicha0af4732012-12-12 21:15:54 +0000958//
John Kessenichecba76f2017-01-06 00:34:48 -0700959// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000960// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000961//
John Kessenich51cdd902014-02-18 23:37:57 +0000962void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000963{
John Kessenichca3457f2015-05-18 01:59:45 +0000964 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000965 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000966 if (! shaderStrings) {
967 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000968 }
969
John Kessenich41cf6b52013-06-25 18:10:05 +0000970 int* lengths = new int[NumShaderStrings];
971
972 // move to length-based strings, rather than null-terminated strings
973 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000974 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000975
John Kessenichc999ba22013-11-07 23:33:24 +0000976 if (! shaderStrings) {
977 CompileFailed = true;
978 return;
979 }
John Kessenicha0af4732012-12-12 21:15:54 +0000980
John Kessenich52ac67e2013-05-05 23:46:22 +0000981 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000982 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700983
John Kessenich94a81fb2013-08-31 02:41:30 +0000984 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
985 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700986 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000987 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700988 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700989 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000990 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700991 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
992 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000993 }
John Kessenicha0af4732012-12-12 21:15:54 +0000994
John Kessenich94a81fb2013-08-31 02:41:30 +0000995 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000996 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000997 }
John Kessenicha0af4732012-12-12 21:15:54 +0000998
John Kessenich41cf6b52013-06-25 18:10:05 +0000999 delete [] lengths;
1000 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +00001001
John Kessenichc999ba22013-11-07 23:33:24 +00001002 if (ret == 0)
1003 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001004}
1005
John Kessenicha0af4732012-12-12 21:15:54 +00001006//
1007// print usage to stdout
1008//
1009void usage()
1010{
John Kessenich319de232013-12-04 04:43:40 +00001011 printf("Usage: glslangValidator [option]... [file]...\n"
1012 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +00001013 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001014 " .conf to provide an optional config file that replaces the default configuration\n"
1015 " (see -c option below for generating a template)\n"
1016 " .vert for a vertex shader\n"
1017 " .tesc for a tessellation control shader\n"
1018 " .tese for a tessellation evaluation shader\n"
1019 " .geom for a geometry shader\n"
1020 " .frag for a fragment shader\n"
1021 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001022 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001023 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +00001024 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001025 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001026 "Each option must be specified separately.\n"
1027 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1028 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001029 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1030 " default file name is <stage>.spv (-o overrides this)\n"
1031 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001032 " -E print pre-processed GLSL; cannot be used with -l;\n"
1033 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001034 " -S <stage> uses specified stage rather than parsing the file extension\n"
1035 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001036 " -c configuration dump;\n"
1037 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -06001038 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001039 " -d default to desktop (#version 110) when there is no shader #version\n"
1040 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -07001041 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001042 " -e specify entry-point name\n"
John Kessenich121853f2017-05-31 17:11:16 -06001043 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001044 " -h print this usage message\n"
1045 " -i intermediate tree (glslang AST) is printed out\n"
1046 " -l link all input files together to form a single module\n"
1047 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001048 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001049 " -q dump reflection query database\n"
1050 " -r relaxed semantic error-checking mode\n"
1051 " -s silent mode\n"
1052 " -t multi-threaded mode\n"
1053 " -v print version strings\n"
1054 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -05001055 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001056 "\n"
1057 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
1058 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1059 "\n"
1060 " --shift-texture-binding [stage] num set base binding number for textures\n"
1061 " --stb [stage] num synonym for --shift-texture-binding\n"
1062 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -06001063 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1064 " --sib [stage] num synonym for --shift-image-binding\n"
1065 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001066 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001067 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001068 " --sub [stage] num synonym for --shift-UBO-binding\n"
1069 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001070 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1071 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1072 "\n"
Hyangran Park36dc8292017-05-02 16:27:29 +09001073 " --resource-set-binding [stage] num set descriptor set and binding number for resources\n"
1074 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1075 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001076 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1077 " --suavb [stage] num synonym for --shift-uav-binding\n"
1078 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001079 " --auto-map-bindings automatically bind uniform variables without\n"
1080 " explicit bindings.\n"
1081 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001082 "\n"
John Kessenich71facdf2017-05-17 18:28:19 -06001083 " --auto-map-locations automatically locate input/output lacking 'location'\n"
1084 " (fragile, not cross stage: recommend explicit\n"
1085 " 'location' use in shader)\n"
1086 " --aml synonym for --auto-map-locations\n"
1087 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001088 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001089 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001090 "\n"
1091 " --no-storage-format use Unknown image format\n"
1092 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001093 "\n"
1094 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1095 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001096 "\n"
1097 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1098 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001099 "\n"
1100 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1101 " initialized with the shader binary code.\n"
1102 " --vn <name> synonym for --variable-name <name>\n"
1103 "\n"
1104 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1105 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001106 "\n"
1107 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001108 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001109
1110 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001111}
1112
John Kessenich3ce4e592014-10-06 19:57:34 +00001113#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001114
1115#include <errno.h>
1116
1117int fopen_s(
1118 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001119 const char* filename,
1120 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001121)
1122{
1123 if (!pFile || !filename || !mode) {
1124 return EINVAL;
1125 }
1126
1127 FILE* f = fopen(filename, mode);
1128 if (! f) {
1129 if (errno != 0) {
1130 return errno;
1131 } else {
1132 return ENOENT;
1133 }
1134 }
1135 *pFile = f;
1136
1137 return 0;
1138}
1139
1140#endif
1141
John Kessenicha0af4732012-12-12 21:15:54 +00001142//
1143// Malloc a string of sufficient size and read a string into it.
1144//
John Kessenichecba76f2017-01-06 00:34:48 -07001145char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001146{
John Kessenichb3297152015-07-11 18:01:03 -06001147 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001148 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001149
John Kessenicha0af4732012-12-12 21:15:54 +00001150 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001151 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1152 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001153
John Kessenich68d78fd2015-07-12 19:28:10 -06001154 if (errorCode || in == nullptr)
1155 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001156
John Kessenicha0af4732012-12-12 21:15:54 +00001157 while (fgetc(in) != EOF)
1158 count++;
1159
John Kessenichd6c72a42014-08-18 19:42:35 +00001160 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001161
John Kessenichb3297152015-07-11 18:01:03 -06001162 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001163 if (! fdata)
1164 Error("can't allocate memory");
1165
John Kessenichb3297152015-07-11 18:01:03 -06001166 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001167 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001168 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001169 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001170
John Kessenicha0af4732012-12-12 21:15:54 +00001171 fdata[count] = '\0';
1172 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001173
John Kessenichea869fb2013-10-28 18:12:06 +00001174 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001175 // recover from empty file
1176 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001177 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001178 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001179 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001180
John Kessenichb3297152015-07-11 18:01:03 -06001181 return return_data;
1182 } else
1183 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1184
1185 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001186 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001187 int ptr_len = 0;
1188 int i = 0;
1189 while (count > 0) {
1190 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1191 memcpy(return_data[i], fdata + ptr_len, len);
1192 return_data[i][len] = '\0';
1193 count -= len;
1194 ptr_len += len;
1195 if (count < len) {
1196 if (count == 0) {
1197 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001198 break;
1199 }
John Kessenichb3297152015-07-11 18:01:03 -06001200 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001201 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001202 ++i;
1203 }
John Kessenichb3297152015-07-11 18:01:03 -06001204
1205 free(fdata);
1206
John Kessenicha0af4732012-12-12 21:15:54 +00001207 return return_data;
1208}
1209
John Kessenich51cdd902014-02-18 23:37:57 +00001210void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001211{
John Kessenichb3297152015-07-11 18:01:03 -06001212 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001213 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001214
1215 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001216}
1217
John Kessenich54d8cda2013-02-11 22:36:01 +00001218void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001219{
John Kessenichfae38ee2015-06-10 23:23:12 +00001220 if (num >= 0 )
1221 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1222 else
1223 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001224}