blob: 40110e35d67d5c0014e4e1e0a77b3c6a274e990c [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000010//
11// Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13//
14// Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following
16// disclaimer in the documentation and/or other materials provided
17// with the distribution.
18//
19// Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20// contributors may be used to endorse or promote products derived
21// from this software without specific prior written permission.
22//
John Kessenich927608b2017-01-06 12:34:14 -070023// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34// POSSIBILITY OF SUCH DAMAGE.
John Kessenicha0af4732012-12-12 21:15:54 +000035//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030038#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000039#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030040#endif
John Kessenich05a70632013-09-17 19:26:08 +000041
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040042#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000043#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060044#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000045#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060046#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
Juan Lopeza558b262017-04-02 23:04:00 +020058#include <memory>
59#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000060
baldurk876a0e32015-11-16 18:03:28 +010061#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000062
63extern "C" {
64 SH_IMPORT_EXPORT void ShOutputHtml();
65}
66
John Kessenich94a81fb2013-08-31 02:41:30 +000067// Command-line options
68enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070069 EOptionNone = 0,
70 EOptionIntermediate = (1 << 0),
71 EOptionSuppressInfolog = (1 << 1),
72 EOptionMemoryLeakMode = (1 << 2),
73 EOptionRelaxedErrors = (1 << 3),
74 EOptionGiveWarnings = (1 << 4),
75 EOptionLinkProgram = (1 << 5),
76 EOptionMultiThreaded = (1 << 6),
77 EOptionDumpConfig = (1 << 7),
78 EOptionDumpReflection = (1 << 8),
79 EOptionSuppressWarnings = (1 << 9),
80 EOptionDumpVersions = (1 << 10),
81 EOptionSpv = (1 << 11),
82 EOptionHumanReadableSpv = (1 << 12),
83 EOptionVulkanRules = (1 << 13),
84 EOptionDefaultDesktop = (1 << 14),
85 EOptionOutputPreprocessed = (1 << 15),
86 EOptionOutputHexadecimal = (1 << 16),
87 EOptionReadHlsl = (1 << 17),
88 EOptionCascadingErrors = (1 << 18),
89 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060090 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070091 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070092 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060093 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060094 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060095 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060096 EOptionDebug = (1 << 26),
John Kessenich94a81fb2013-08-31 02:41:30 +000097};
98
John Kessenicha0af4732012-12-12 21:15:54 +000099//
John Kessenich68d78fd2015-07-12 19:28:10 -0600100// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000101//
102enum TFailCode {
103 ESuccess = 0,
104 EFailUsage,
105 EFailCompile,
106 EFailLink,
107 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000108 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000109 EFailLinkerCreate
110};
111
112//
John Kessenich68d78fd2015-07-12 19:28:10 -0600113// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000114//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600115EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000116void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000117void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000118void FreeFileData(char** data);
119char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000120void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000121
John Kessenichc999ba22013-11-07 23:33:24 +0000122// Globally track if any compile or link failure.
123bool CompileFailed = false;
124bool LinkFailed = false;
125
John Kessenich05a70632013-09-17 19:26:08 +0000126// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600127// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000128int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000129
John Kessenich05a70632013-09-17 19:26:08 +0000130TBuiltInResource Resources;
131std::string ConfigFile;
132
John Kessenicha0af4732012-12-12 21:15:54 +0000133//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600134// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000135//
136void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000137{
John Kessenich05a70632013-09-17 19:26:08 +0000138 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000139 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000140 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000141 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000142 if (configStrings)
143 config = *configStrings;
144 else {
145 printf("Error opening configuration file; will instead use the default configuration\n");
146 usage();
147 }
148 }
149
150 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500151 Resources = glslang::DefaultTBuiltInResource;
152 return;
John Kessenich05a70632013-09-17 19:26:08 +0000153 }
154
Lei Zhang1b141722016-05-19 13:50:49 -0400155 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000156
John Kessenich05a70632013-09-17 19:26:08 +0000157 if (configStrings)
158 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500159 else
160 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000161}
162
John Kessenich94a81fb2013-08-31 02:41:30 +0000163int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600164const char* ExecutableName = nullptr;
165const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700166const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600167const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400168const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800169const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600170
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600171std::array<unsigned int, EShLangCount> baseSamplerBinding;
172std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600173std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600174std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700175std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600176std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900177std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600178
John Kessenich68d78fd2015-07-12 19:28:10 -0600179//
180// Create the default name for saving a binary if -o is not provided.
181//
182const char* GetBinaryName(EShLanguage stage)
183{
184 const char* name;
185 if (binaryFileName == nullptr) {
186 switch (stage) {
187 case EShLangVertex: name = "vert.spv"; break;
188 case EShLangTessControl: name = "tesc.spv"; break;
189 case EShLangTessEvaluation: name = "tese.spv"; break;
190 case EShLangGeometry: name = "geom.spv"; break;
191 case EShLangFragment: name = "frag.spv"; break;
192 case EShLangCompute: name = "comp.spv"; break;
193 default: name = "unknown"; break;
194 }
195 } else
196 name = binaryFileName;
197
198 return name;
199}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000200
John Kessenich05a70632013-09-17 19:26:08 +0000201//
202// *.conf => this is a config file that can set limits/resources
203//
204bool SetConfigFile(const std::string& name)
205{
206 if (name.size() < 5)
207 return false;
208
John Kessenich4c706852013-10-11 16:28:43 +0000209 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000210 ConfigFile = name;
211 return true;
212 }
213
214 return false;
215}
216
John Kessenich68d78fd2015-07-12 19:28:10 -0600217//
218// Give error and exit with failure code.
219//
220void Error(const char* message)
221{
222 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
223 exit(EFailUsage);
224}
225
226//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600227// Process an optional binding base of the form:
228// --argname [stage] base
229// Where stage is one of the forms accepted by FindLanguage, and base is an integer
230//
231void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
232{
233 if (argc < 2)
234 usage();
235
236 if (!isdigit(argv[1][0])) {
237 if (argc < 3) // this form needs one more argument
238 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700239
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600240 // Parse form: --argname stage base
241 const EShLanguage lang = FindLanguage(argv[1], false);
242 base[lang] = atoi(argv[2]);
243 argc-= 2;
244 argv+= 2;
245 } else {
246 // Parse form: --argname base
247 for (int lang=0; lang<EShLangCount; ++lang)
248 base[lang] = atoi(argv[1]);
249
250 argc--;
251 argv++;
252 }
253}
254
Hyangran Park36dc8292017-05-02 16:27:29 +0900255void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
256{
257 if (argc < 2)
258 usage();
259
260 if (!isdigit(argv[1][0])) {
261 if (argc < 5) // this form needs one more argument
262 usage();
263
264 // Parse form: --argname stage base
265 const EShLanguage lang = FindLanguage(argv[1], false);
266
267 base[lang].push_back(argv[2]);
268 base[lang].push_back(argv[3]);
269 base[lang].push_back(argv[4]);
270 argc-= 4;
271 argv+= 4;
272 while(argv[1] != NULL) {
273 if(argv[1][0] != '-') {
274 base[lang].push_back(argv[1]);
275 base[lang].push_back(argv[2]);
276 base[lang].push_back(argv[3]);
277 argc-= 3;
278 argv+= 3;
279 }
280 else {
281 break;
282 }
283 }
284 } else {
285 // Parse form: --argname base
286 for (int lang=0; lang<EShLangCount; ++lang)
287 base[lang].push_back(argv[1]);
288
289 argc--;
290 argv++;
291 }
292}
293
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600294//
John Kessenich68d78fd2015-07-12 19:28:10 -0600295// Do all command-line argument parsing. This includes building up the work-items
296// to be processed later, and saving all the command-line options.
297//
298// Does not return (it exits) if command-line is fatally flawed.
299//
Juan Lopeza558b262017-04-02 23:04:00 +0200300void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000301{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600302 baseSamplerBinding.fill(0);
303 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600304 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600305 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700306 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600307 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600308
John Kessenich38f3b892013-09-06 19:52:57 +0000309 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200310 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000311
John Kessenich2b07c7e2013-07-31 18:44:13 +0000312 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700313 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000314 for (; argc >= 1; argc--, argv++) {
315 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000316 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600317 case '-':
318 {
319 std::string lowerword(argv[0]+2);
320 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
321
322 // handle --word style options
323 if (lowerword == "shift-sampler-bindings" || // synonyms
324 lowerword == "shift-sampler-binding" ||
325 lowerword == "ssb") {
326 ProcessBindingBase(argc, argv, baseSamplerBinding);
327 } else if (lowerword == "shift-texture-bindings" || // synonyms
328 lowerword == "shift-texture-binding" ||
329 lowerword == "stb") {
330 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600331 } else if (lowerword == "shift-image-bindings" || // synonyms
332 lowerword == "shift-image-binding" ||
333 lowerword == "sib") {
334 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600335 } else if (lowerword == "shift-ubo-bindings" || // synonyms
336 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600337 lowerword == "shift-cbuffer-bindings" ||
338 lowerword == "shift-cbuffer-binding" ||
339 lowerword == "sub" ||
340 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600341 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700342 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
343 lowerword == "shift-ssbo-binding" ||
344 lowerword == "sbb") {
345 ProcessBindingBase(argc, argv, baseSsboBinding);
Hyangran Park36dc8292017-05-02 16:27:29 +0900346 } else if (lowerword == "resource-set-bindings" || // synonyms
347 lowerword == "resource-set-binding" ||
348 lowerword == "rsb") {
349 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600350 } else if (lowerword == "shift-uav-bindings" || // synonyms
351 lowerword == "shift-uav-binding" ||
352 lowerword == "suavb") {
353 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600354 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700355 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600356 lowerword == "amb") {
357 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600358 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
359 lowerword == "flatten-uniform-array" ||
360 lowerword == "fua") {
361 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600362 } else if (lowerword == "no-storage-format" || // synonyms
363 lowerword == "nsf") {
364 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800365 } else if (lowerword == "variable-name" || // synonyms
366 lowerword == "vn") {
367 Options |= EOptionOutputHexadecimal;
368 variableName = argv[1];
369 if (argc > 0) {
370 argc--;
371 argv++;
372 } else
373 Error("no <C-variable-name> provided for --variable-name");
374 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600375 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600376 lowerword == "sep") {
377 sourceEntryPointName = argv[1];
378 if (argc > 0) {
379 argc--;
380 argv++;
381 } else
382 Error("no <entry-point> provided for --source-entrypoint");
383 break;
John Kessenich906cc212016-12-09 19:22:20 -0700384 } else if (lowerword == "keep-uncalled" || // synonyms
385 lowerword == "ku") {
386 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600387 } else if (lowerword == "hlsl-offsets") {
388 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600389 } else if (lowerword == "hlsl-iomap" ||
390 lowerword == "hlsl-iomapper" ||
391 lowerword == "hlsl-iomapping") {
392 Options |= EOptionHlslIoMapping;
John Kessenich71facdf2017-05-17 18:28:19 -0600393 } else if (lowerword == "auto-map-locations" || // synonyms
394 lowerword == "aml") {
395 Options |= EOptionAutoMapLocations;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600396 } else {
397 usage();
398 }
399 }
400 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000401 case 'H':
402 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600403 if ((Options & EOptionSpv) == 0) {
404 // default to Vulkan
405 Options |= EOptionSpv;
406 Options |= EOptionVulkanRules;
407 Options |= EOptionLinkProgram;
408 }
409 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000410 case 'V':
411 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600412 Options |= EOptionVulkanRules;
413 Options |= EOptionLinkProgram;
414 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400415 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400416 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400417 if (argc > 0) {
418 argc--;
419 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600420 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400421 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400422 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600423 case 'G':
424 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000425 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600426 // undo a -H default to Vulkan
427 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000428 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000429 case 'E':
430 Options |= EOptionOutputPreprocessed;
431 break;
John Kessenich05a70632013-09-17 19:26:08 +0000432 case 'c':
433 Options |= EOptionDumpConfig;
434 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600435 case 'C':
436 Options |= EOptionCascadingErrors;
437 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000438 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000439 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000440 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700441 case 'D':
442 Options |= EOptionReadHlsl;
443 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700444 case 'e':
445 // HLSL todo: entry point handle needs much more sophistication.
446 // This is okay for one compilation unit with one entry point.
447 entryPointName = argv[1];
448 if (argc > 0) {
449 argc--;
450 argv++;
451 } else
452 Error("no <entry-point> provided for -e");
453 break;
John Kessenich121853f2017-05-31 17:11:16 -0600454 case 'g':
455 Options |= EOptionDebug;
456 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600457 case 'h':
458 usage();
459 break;
John Kessenich05a70632013-09-17 19:26:08 +0000460 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000461 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000462 break;
463 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000464 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000465 break;
466 case 'm':
467 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000468 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600469 case 'o':
470 binaryFileName = argv[1];
471 if (argc > 0) {
472 argc--;
473 argv++;
474 } else
475 Error("no <file> provided for -o");
476 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000477 case 'q':
478 Options |= EOptionDumpReflection;
479 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000480 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000481 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000482 break;
483 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000484 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000485 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000486 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200487 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000488 break;
John Kessenich319de232013-12-04 04:43:40 +0000489 case 'v':
490 Options |= EOptionDumpVersions;
491 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000492 case 'w':
493 Options |= EOptionSuppressWarnings;
494 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500495 case 'x':
496 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500497 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000498 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600499 usage();
500 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000501 }
John Kessenich38f3b892013-09-06 19:52:57 +0000502 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000503 std::string name(argv[0]);
504 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200505 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000506 }
John Kessenich38f3b892013-09-06 19:52:57 +0000507 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000508 }
509
John Kessenich68d78fd2015-07-12 19:28:10 -0600510 // Make sure that -E is not specified alongside linking (which includes SPV generation)
511 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
512 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000513
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500514 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600515 if (binaryFileName && (Options & EOptionSpv) == 0)
516 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600517
518 if ((Options & EOptionFlattenUniformArrays) != 0 &&
519 (Options & EOptionReadHlsl) == 0)
520 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000521}
522
John Kessenich68d78fd2015-07-12 19:28:10 -0600523//
524// Translate the meaningful subset of command-line options to parser-behavior options.
525//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000526void SetMessageOptions(EShMessages& messages)
527{
528 if (Options & EOptionRelaxedErrors)
529 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
530 if (Options & EOptionIntermediate)
531 messages = (EShMessages)(messages | EShMsgAST);
532 if (Options & EOptionSuppressWarnings)
533 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600534 if (Options & EOptionSpv)
535 messages = (EShMessages)(messages | EShMsgSpvRules);
536 if (Options & EOptionVulkanRules)
537 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400538 if (Options & EOptionOutputPreprocessed)
539 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700540 if (Options & EOptionReadHlsl)
541 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600542 if (Options & EOptionCascadingErrors)
543 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700544 if (Options & EOptionKeepUncalled)
545 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600546 if (Options & EOptionHlslOffsets)
547 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600548 if (Options & EOptionDebug)
549 messages = (EShMessages)(messages | EShMsgDebugInfo);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000550}
551
John Kessenich68d78fd2015-07-12 19:28:10 -0600552//
John Kessenich69f4b512013-09-04 21:19:27 +0000553// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000554//
Juan Lopeza558b262017-04-02 23:04:00 +0200555void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000556{
John Kessenich38f3b892013-09-06 19:52:57 +0000557 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200558 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000559 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000560 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200561 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000562
John Kessenichb0a7eb52013-11-07 17:44:20 +0000563 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000564
John Kessenich94a81fb2013-08-31 02:41:30 +0000565 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000566 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000567
568 ShDestruct(compiler);
569 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000570}
571
John Kessenich6626cad2015-06-19 05:14:19 +0000572// Outputs the given string, but only if it is non-null and non-empty.
573// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400574void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000575{
576 if (str && str[0]) {
577 puts(str);
578 }
579}
580
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400581// Outputs the given string to stderr, but only if it is non-null and non-empty.
582// This prevents erroneous newlines from appearing.
583void StderrIfNonEmpty(const char* str)
584{
585 if (str && str[0]) {
586 fprintf(stderr, "%s\n", str);
587 }
588}
589
John Kessenichc57b2a92016-01-16 15:30:03 -0700590// Simple bundling of what makes a compilation unit for ease in passing around,
591// and separation of handling file IO versus API (programmatic) compilation.
592struct ShaderCompUnit {
593 EShLanguage stage;
594 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400595 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600596 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400597
John Kessenich219b0252016-08-23 17:51:13 -0600598 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400599 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
600 {
601 stage = istage;
602 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600603 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600604 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400605 }
606
607 ShaderCompUnit(const ShaderCompUnit &rhs)
608 {
609 stage = rhs.stage;
610 fileName = rhs.fileName;
611 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600612 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400613 }
614
John Kessenichc57b2a92016-01-16 15:30:03 -0700615};
616
John Kessenich69f4b512013-09-04 21:19:27 +0000617//
John Kessenichc57b2a92016-01-16 15:30:03 -0700618// For linking mode: Will independently parse each compilation unit, but then put them
619// in the same program and link them together, making at most one linked module per
620// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000621//
622// Uses the new C++ interface instead of the old handle-based interface.
623//
John Kessenichc57b2a92016-01-16 15:30:03 -0700624
625void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000626{
627 // keep track of what to free
628 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700629
John Kessenich69f4b512013-09-04 21:19:27 +0000630 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000631 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000632
John Kessenich69f4b512013-09-04 21:19:27 +0000633 //
634 // Per-shader processing...
635 //
636
John Kessenich5b0f13a2013-11-01 03:08:40 +0000637 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100638 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
639 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700640 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400641 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700642 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
643 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600644 if (sourceEntryPointName)
645 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600646
647 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
648 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600649 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600650 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700651 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600652 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600653 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600654 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900655 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600656
steve-lunargbe283552017-04-18 12:18:01 -0600657 if (Options & EOptionHlslIoMapping)
658 shader->setHlslIoMapping(true);
659
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600660 if (Options & EOptionAutoMapBindings)
661 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700662
John Kessenich71facdf2017-05-17 18:28:19 -0600663 if (Options & EOptionAutoMapLocations)
664 shader->setAutoMapLocations(true);
665
John Kessenich69f4b512013-09-04 21:19:27 +0000666 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600667
John Kessenichc555ddd2015-06-17 02:38:44 +0000668 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000669
John Kessenich3494b4d2017-05-22 15:00:42 -0600670 DirStackFileIncluder includer;
John Kessenichc555ddd2015-06-17 02:38:44 +0000671 if (Options & EOptionOutputPreprocessed) {
672 std::string str;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400673 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500674 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400675 PutsIfNonEmpty(str.c_str());
676 } else {
677 CompileFailed = true;
678 }
679 StderrIfNonEmpty(shader->getInfoLog());
680 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000681 continue;
682 }
John Kessenich3494b4d2017-05-22 15:00:42 -0600683 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +0000684 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000685
John Kessenich69f4b512013-09-04 21:19:27 +0000686 program.addShader(shader);
687
John Kessenichc57b2a92016-01-16 15:30:03 -0700688 if (! (Options & EOptionSuppressInfolog) &&
689 ! (Options & EOptionMemoryLeakMode)) {
690 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400691 PutsIfNonEmpty(shader->getInfoLog());
692 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000693 }
John Kessenich69f4b512013-09-04 21:19:27 +0000694 }
695
696 //
697 // Program-level processing...
698 //
699
John Kessenich4d65ee32016-03-12 18:17:47 -0700700 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600701 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000702 LinkFailed = true;
703
steve-lunarg9ae34742016-10-05 13:40:13 -0600704 // Map IO
705 if (Options & EOptionSpv) {
706 if (!program.mapIO())
707 LinkFailed = true;
708 }
John Kessenichecba76f2017-01-06 00:34:48 -0700709
John Kessenich4d65ee32016-03-12 18:17:47 -0700710 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700711 if (! (Options & EOptionSuppressInfolog) &&
712 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400713 PutsIfNonEmpty(program.getInfoLog());
714 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000715 }
716
John Kessenich4d65ee32016-03-12 18:17:47 -0700717 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000718 if (Options & EOptionDumpReflection) {
719 program.buildReflection();
720 program.dumpReflection();
721 }
722
John Kessenich4d65ee32016-03-12 18:17:47 -0700723 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000724 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000725 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600726 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000727 else {
728 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000729 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000730 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400731 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400732 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -0600733 glslang::SpvOptions spvOptions;
734 if (Options & EOptionDebug)
735 spvOptions.generateDebugInfo = true;
736 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -0700737
738 // Dump the spv to a file or stdout, etc., but only if not doing
739 // memory/perf testing, as it's not internal to programmatic use.
740 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500741 printf("%s", logger.getAllMessages().c_str());
742 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700743 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500744 } else {
745 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
746 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700747 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700748 spv::Disassemble(std::cout, spirv);
749 }
John Kessenichacba7722015-03-04 03:48:38 +0000750 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000751 }
John Kessenich92f90382014-07-28 04:21:04 +0000752 }
753 }
754 }
755
John Kessenich5b0f13a2013-11-01 03:08:40 +0000756 // Free everything up, program has to go before the shaders
757 // because it might have merged stuff from the shaders, and
758 // the stuff from the shaders has to have its destructors called
759 // before the pools holding the memory in the shaders is freed.
760 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000761 while (shaders.size() > 0) {
762 delete shaders.back();
763 shaders.pop_back();
764 }
John Kessenich69f4b512013-09-04 21:19:27 +0000765}
766
John Kessenichc57b2a92016-01-16 15:30:03 -0700767//
768// Do file IO part of compile and link, handing off the pure
769// API/programmatic mode to CompileAndLinkShaderUnits(), which can
770// be put in a loop for testing memory footprint and performance.
771//
772// This is just for linking mode: meaning all the shaders will be put into the
773// the same program linked together.
774//
775// This means there are a limited number of work items (not multi-threading mode)
776// and that the point is testing at the linking level. Hence, to enable
777// performance and memory testing, the actual compile/link can be put in
778// a loop, independent of processing the work items and file IO.
779//
Juan Lopeza558b262017-04-02 23:04:00 +0200780void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700781{
782 std::vector<ShaderCompUnit> compUnits;
783
784 // Transfer all the work items from to a simple list of
785 // of compilation units. (We don't care about the thread
786 // work-item distribution properties in this path, which
787 // is okay due to the limited number of shaders, know since
788 // they are all getting linked together.)
789 glslang::TWorkItem* workItem;
790 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200791 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700792 FindLanguage(workItem->name),
793 workItem->name,
794 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200795 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700796
797 if (! compUnit.text) {
798 usage();
799 return;
800 }
801
802 compUnits.push_back(compUnit);
803 }
804
805 // Actual call to programmatic processing of compile and link,
806 // in a loop for testing memory and performance. This part contains
807 // all the perf/memory that a programmatic consumer will care about.
808 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
809 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
810 CompileAndLinkShaderUnits(compUnits);
811
812 if (Options & EOptionMemoryLeakMode)
813 glslang::OS_DumpMemoryCounters();
814 }
815
rdb32084e82016-02-23 22:17:38 +0100816 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
817 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700818}
819
John Kessenicha0af4732012-12-12 21:15:54 +0000820int C_DECL main(int argc, char* argv[])
821{
Juan Lopeza558b262017-04-02 23:04:00 +0200822 // array of unique places to leave the shader names and infologs for the asynchronous compiles
823 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
824 ProcessArguments(workItems, argc, argv);
825
826 glslang::TWorklist workList;
827 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
828 assert(item);
829 workList.add(item.get());
830 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000831
John Kessenich05a70632013-09-17 19:26:08 +0000832 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500833 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200834 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000835 return ESuccess;
836 }
837
John Kessenich0da9eaa2015-08-01 17:10:02 -0600838 if (Options & EOptionDumpVersions) {
839 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000840 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
841 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600842 std::string spirvVersion;
843 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600844 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600845 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700846 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600847 printf("GL_KHR_vulkan_glsl version %d\n", 100);
848 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200849 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000850 return ESuccess;
851 }
852
Juan Lopeza558b262017-04-02 23:04:00 +0200853 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000854 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000855 }
856
857 ProcessConfigFile();
858
John Kessenich69f4b512013-09-04 21:19:27 +0000859 //
860 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000861 // 1) linking all arguments together, single-threaded, new C++ interface
862 // 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 +0000863 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000864 if (Options & EOptionLinkProgram ||
865 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000866 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200867 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000868 glslang::FinalizeProcess();
869 } else {
870 ShInitialize();
871
Juan Lopeza558b262017-04-02 23:04:00 +0200872 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000873
Juan Lopeza558b262017-04-02 23:04:00 +0200874 if (Options & EOptionMultiThreaded)
875 {
876 std::array<std::thread, 16> threads;
877 for (unsigned int t = 0; t < threads.size(); ++t)
878 {
879 threads[t] = std::thread(CompileShaders, std::ref(workList));
880 if (threads[t].get_id() == std::thread::id())
881 {
John Kessenich38f3b892013-09-06 19:52:57 +0000882 printf("Failed to create thread\n");
883 return EFailThreadCreate;
884 }
John Kessenicha0af4732012-12-12 21:15:54 +0000885 }
Juan Lopeza558b262017-04-02 23:04:00 +0200886
887 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000888 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200889 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000890
891 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200892 for (size_t w = 0; w < workItems.size(); ++w) {
893 if (workItems[w]) {
894 if (printShaderNames || workItems[w]->results.size() > 0)
895 PutsIfNonEmpty(workItems[w]->name.c_str());
896 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000897 }
898 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000899
900 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000901 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000902
John Kessenichc999ba22013-11-07 23:33:24 +0000903 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000904 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000905 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000906 return EFailLink;
907
908 return 0;
909}
910
911//
912// Deduce the language from the filename. Files must end in one of the
913// following extensions:
914//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000915// .vert = vertex
916// .tesc = tessellation control
917// .tese = tessellation evaluation
918// .geom = geometry
919// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000920// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000921//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600922EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000923{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600924 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600925 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600926
Dan Bakerc6ede892016-08-11 14:06:06 -0400927 if (shaderStageName)
928 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600929 else {
930 // Search for a suffix on a filename: e.g, "myfile.frag". If given
931 // the suffix directly, we skip looking for the '.'
932 if (parseSuffix) {
933 ext = name.rfind('.');
934 if (ext == std::string::npos) {
935 usage();
936 return EShLangVertex;
937 }
938 ++ext;
939 }
940 suffix = name.substr(ext, std::string::npos);
941 }
dankbaker45d49bc2016-08-08 21:43:07 -0400942
John Kessenich2b07c7e2013-07-31 18:44:13 +0000943 if (suffix == "vert")
944 return EShLangVertex;
945 else if (suffix == "tesc")
946 return EShLangTessControl;
947 else if (suffix == "tese")
948 return EShLangTessEvaluation;
949 else if (suffix == "geom")
950 return EShLangGeometry;
951 else if (suffix == "frag")
952 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000953 else if (suffix == "comp")
954 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000955
956 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000957 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000958}
959
John Kessenicha0af4732012-12-12 21:15:54 +0000960//
John Kessenichecba76f2017-01-06 00:34:48 -0700961// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000962// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000963//
John Kessenich51cdd902014-02-18 23:37:57 +0000964void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000965{
John Kessenichca3457f2015-05-18 01:59:45 +0000966 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000967 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000968 if (! shaderStrings) {
969 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000970 }
971
John Kessenich41cf6b52013-06-25 18:10:05 +0000972 int* lengths = new int[NumShaderStrings];
973
974 // move to length-based strings, rather than null-terminated strings
975 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000976 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000977
John Kessenichc999ba22013-11-07 23:33:24 +0000978 if (! shaderStrings) {
979 CompileFailed = true;
980 return;
981 }
John Kessenicha0af4732012-12-12 21:15:54 +0000982
John Kessenich52ac67e2013-05-05 23:46:22 +0000983 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000984 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700985
John Kessenich94a81fb2013-08-31 02:41:30 +0000986 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
987 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700988 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000989 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700990 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700991 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000992 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700993 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
994 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000995 }
John Kessenicha0af4732012-12-12 21:15:54 +0000996
John Kessenich94a81fb2013-08-31 02:41:30 +0000997 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000998 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000999 }
John Kessenicha0af4732012-12-12 21:15:54 +00001000
John Kessenich41cf6b52013-06-25 18:10:05 +00001001 delete [] lengths;
1002 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +00001003
John Kessenichc999ba22013-11-07 23:33:24 +00001004 if (ret == 0)
1005 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001006}
1007
John Kessenicha0af4732012-12-12 21:15:54 +00001008//
1009// print usage to stdout
1010//
1011void usage()
1012{
John Kessenich319de232013-12-04 04:43:40 +00001013 printf("Usage: glslangValidator [option]... [file]...\n"
1014 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +00001015 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001016 " .conf to provide an optional config file that replaces the default configuration\n"
1017 " (see -c option below for generating a template)\n"
1018 " .vert for a vertex shader\n"
1019 " .tesc for a tessellation control shader\n"
1020 " .tese for a tessellation evaluation shader\n"
1021 " .geom for a geometry shader\n"
1022 " .frag for a fragment shader\n"
1023 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001024 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001025 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +00001026 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001027 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001028 "Each option must be specified separately.\n"
1029 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1030 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001031 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1032 " default file name is <stage>.spv (-o overrides this)\n"
1033 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001034 " -E print pre-processed GLSL; cannot be used with -l;\n"
1035 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001036 " -S <stage> uses specified stage rather than parsing the file extension\n"
1037 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001038 " -c configuration dump;\n"
1039 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -06001040 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001041 " -d default to desktop (#version 110) when there is no shader #version\n"
1042 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -07001043 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001044 " -e specify entry-point name\n"
John Kessenich121853f2017-05-31 17:11:16 -06001045 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001046 " -h print this usage message\n"
1047 " -i intermediate tree (glslang AST) is printed out\n"
1048 " -l link all input files together to form a single module\n"
1049 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001050 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001051 " -q dump reflection query database\n"
1052 " -r relaxed semantic error-checking mode\n"
1053 " -s silent mode\n"
1054 " -t multi-threaded mode\n"
1055 " -v print version strings\n"
1056 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -05001057 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001058 "\n"
1059 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
1060 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1061 "\n"
1062 " --shift-texture-binding [stage] num set base binding number for textures\n"
1063 " --stb [stage] num synonym for --shift-texture-binding\n"
1064 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -06001065 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1066 " --sib [stage] num synonym for --shift-image-binding\n"
1067 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001068 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001069 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001070 " --sub [stage] num synonym for --shift-UBO-binding\n"
1071 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001072 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1073 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1074 "\n"
Hyangran Park36dc8292017-05-02 16:27:29 +09001075 " --resource-set-binding [stage] num set descriptor set and binding number for resources\n"
1076 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1077 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001078 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1079 " --suavb [stage] num synonym for --shift-uav-binding\n"
1080 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001081 " --auto-map-bindings automatically bind uniform variables without\n"
1082 " explicit bindings.\n"
1083 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001084 "\n"
John Kessenich71facdf2017-05-17 18:28:19 -06001085 " --auto-map-locations automatically locate input/output lacking 'location'\n"
1086 " (fragile, not cross stage: recommend explicit\n"
1087 " 'location' use in shader)\n"
1088 " --aml synonym for --auto-map-locations\n"
1089 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001090 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001091 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001092 "\n"
1093 " --no-storage-format use Unknown image format\n"
1094 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001095 "\n"
1096 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1097 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001098 "\n"
1099 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1100 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001101 "\n"
1102 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1103 " initialized with the shader binary code.\n"
1104 " --vn <name> synonym for --variable-name <name>\n"
1105 "\n"
1106 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1107 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001108 "\n"
1109 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001110 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001111
1112 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001113}
1114
John Kessenich3ce4e592014-10-06 19:57:34 +00001115#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001116
1117#include <errno.h>
1118
1119int fopen_s(
1120 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001121 const char* filename,
1122 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001123)
1124{
1125 if (!pFile || !filename || !mode) {
1126 return EINVAL;
1127 }
1128
1129 FILE* f = fopen(filename, mode);
1130 if (! f) {
1131 if (errno != 0) {
1132 return errno;
1133 } else {
1134 return ENOENT;
1135 }
1136 }
1137 *pFile = f;
1138
1139 return 0;
1140}
1141
1142#endif
1143
John Kessenicha0af4732012-12-12 21:15:54 +00001144//
1145// Malloc a string of sufficient size and read a string into it.
1146//
John Kessenichecba76f2017-01-06 00:34:48 -07001147char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001148{
John Kessenichb3297152015-07-11 18:01:03 -06001149 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001150 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001151
John Kessenicha0af4732012-12-12 21:15:54 +00001152 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001153 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1154 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001155
John Kessenich68d78fd2015-07-12 19:28:10 -06001156 if (errorCode || in == nullptr)
1157 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001158
John Kessenicha0af4732012-12-12 21:15:54 +00001159 while (fgetc(in) != EOF)
1160 count++;
1161
John Kessenichd6c72a42014-08-18 19:42:35 +00001162 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001163
John Kessenichb3297152015-07-11 18:01:03 -06001164 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001165 if (! fdata)
1166 Error("can't allocate memory");
1167
John Kessenichb3297152015-07-11 18:01:03 -06001168 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001169 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001170 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001171 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001172
John Kessenicha0af4732012-12-12 21:15:54 +00001173 fdata[count] = '\0';
1174 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001175
John Kessenichea869fb2013-10-28 18:12:06 +00001176 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001177 // recover from empty file
1178 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001179 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001180 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001181 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001182
John Kessenichb3297152015-07-11 18:01:03 -06001183 return return_data;
1184 } else
1185 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1186
1187 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001188 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001189 int ptr_len = 0;
1190 int i = 0;
1191 while (count > 0) {
1192 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1193 memcpy(return_data[i], fdata + ptr_len, len);
1194 return_data[i][len] = '\0';
1195 count -= len;
1196 ptr_len += len;
1197 if (count < len) {
1198 if (count == 0) {
1199 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001200 break;
1201 }
John Kessenichb3297152015-07-11 18:01:03 -06001202 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001203 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001204 ++i;
1205 }
John Kessenichb3297152015-07-11 18:01:03 -06001206
1207 free(fdata);
1208
John Kessenicha0af4732012-12-12 21:15:54 +00001209 return return_data;
1210}
1211
John Kessenich51cdd902014-02-18 23:37:57 +00001212void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001213{
John Kessenichb3297152015-07-11 18:01:03 -06001214 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001215 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001216
1217 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001218}
1219
John Kessenich54d8cda2013-02-11 22:36:01 +00001220void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001221{
John Kessenichfae38ee2015-06-10 23:23:12 +00001222 if (num >= 0 )
1223 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1224 else
1225 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001226}