blob: 8a6b12dc456aa3437dcfb2b46e4edd44894d53cb [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 Kessenich94a81fb2013-08-31 02:41:30 +000094};
95
John Kessenicha0af4732012-12-12 21:15:54 +000096//
John Kessenich68d78fd2015-07-12 19:28:10 -060097// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000098//
99enum TFailCode {
100 ESuccess = 0,
101 EFailUsage,
102 EFailCompile,
103 EFailLink,
104 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000105 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000106 EFailLinkerCreate
107};
108
109//
John Kessenich68d78fd2015-07-12 19:28:10 -0600110// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000111//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600112EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000113void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000114void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000115void FreeFileData(char** data);
116char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000117void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000118
John Kessenichc999ba22013-11-07 23:33:24 +0000119// Globally track if any compile or link failure.
120bool CompileFailed = false;
121bool LinkFailed = false;
122
John Kessenich05a70632013-09-17 19:26:08 +0000123// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600124// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000125int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000126
John Kessenich05a70632013-09-17 19:26:08 +0000127TBuiltInResource Resources;
128std::string ConfigFile;
129
John Kessenicha0af4732012-12-12 21:15:54 +0000130//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600131// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000132//
133void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000134{
John Kessenich05a70632013-09-17 19:26:08 +0000135 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000136 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000137 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000138 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000139 if (configStrings)
140 config = *configStrings;
141 else {
142 printf("Error opening configuration file; will instead use the default configuration\n");
143 usage();
144 }
145 }
146
147 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500148 Resources = glslang::DefaultTBuiltInResource;
149 return;
John Kessenich05a70632013-09-17 19:26:08 +0000150 }
151
Lei Zhang1b141722016-05-19 13:50:49 -0400152 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000153
John Kessenich05a70632013-09-17 19:26:08 +0000154 if (configStrings)
155 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500156 else
157 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000158}
159
John Kessenich94a81fb2013-08-31 02:41:30 +0000160int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600161const char* ExecutableName = nullptr;
162const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700163const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600164const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400165const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800166const char* variableName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600167
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600168std::array<unsigned int, EShLangCount> baseSamplerBinding;
169std::array<unsigned int, EShLangCount> baseTextureBinding;
steve-lunarg9088be42016-11-01 10:31:42 -0600170std::array<unsigned int, EShLangCount> baseImageBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600171std::array<unsigned int, EShLangCount> baseUboBinding;
steve-lunarg932bb5c2017-02-21 17:19:08 -0700172std::array<unsigned int, EShLangCount> baseSsboBinding;
steve-lunargbe283552017-04-18 12:18:01 -0600173std::array<unsigned int, EShLangCount> baseUavBinding;
Hyangran Park36dc8292017-05-02 16:27:29 +0900174std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600175
John Kessenich68d78fd2015-07-12 19:28:10 -0600176//
177// Create the default name for saving a binary if -o is not provided.
178//
179const char* GetBinaryName(EShLanguage stage)
180{
181 const char* name;
182 if (binaryFileName == nullptr) {
183 switch (stage) {
184 case EShLangVertex: name = "vert.spv"; break;
185 case EShLangTessControl: name = "tesc.spv"; break;
186 case EShLangTessEvaluation: name = "tese.spv"; break;
187 case EShLangGeometry: name = "geom.spv"; break;
188 case EShLangFragment: name = "frag.spv"; break;
189 case EShLangCompute: name = "comp.spv"; break;
190 default: name = "unknown"; break;
191 }
192 } else
193 name = binaryFileName;
194
195 return name;
196}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000197
John Kessenich05a70632013-09-17 19:26:08 +0000198//
199// *.conf => this is a config file that can set limits/resources
200//
201bool SetConfigFile(const std::string& name)
202{
203 if (name.size() < 5)
204 return false;
205
John Kessenich4c706852013-10-11 16:28:43 +0000206 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000207 ConfigFile = name;
208 return true;
209 }
210
211 return false;
212}
213
John Kessenich68d78fd2015-07-12 19:28:10 -0600214//
215// Give error and exit with failure code.
216//
217void Error(const char* message)
218{
219 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
220 exit(EFailUsage);
221}
222
223//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600224// Process an optional binding base of the form:
225// --argname [stage] base
226// Where stage is one of the forms accepted by FindLanguage, and base is an integer
227//
228void ProcessBindingBase(int& argc, char**& argv, std::array<unsigned int, EShLangCount>& base)
229{
230 if (argc < 2)
231 usage();
232
233 if (!isdigit(argv[1][0])) {
234 if (argc < 3) // this form needs one more argument
235 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700236
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600237 // Parse form: --argname stage base
238 const EShLanguage lang = FindLanguage(argv[1], false);
239 base[lang] = atoi(argv[2]);
240 argc-= 2;
241 argv+= 2;
242 } else {
243 // Parse form: --argname base
244 for (int lang=0; lang<EShLangCount; ++lang)
245 base[lang] = atoi(argv[1]);
246
247 argc--;
248 argv++;
249 }
250}
251
Hyangran Park36dc8292017-05-02 16:27:29 +0900252void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
253{
254 if (argc < 2)
255 usage();
256
257 if (!isdigit(argv[1][0])) {
258 if (argc < 5) // this form needs one more argument
259 usage();
260
261 // Parse form: --argname stage base
262 const EShLanguage lang = FindLanguage(argv[1], false);
263
264 base[lang].push_back(argv[2]);
265 base[lang].push_back(argv[3]);
266 base[lang].push_back(argv[4]);
267 argc-= 4;
268 argv+= 4;
269 while(argv[1] != NULL) {
270 if(argv[1][0] != '-') {
271 base[lang].push_back(argv[1]);
272 base[lang].push_back(argv[2]);
273 base[lang].push_back(argv[3]);
274 argc-= 3;
275 argv+= 3;
276 }
277 else {
278 break;
279 }
280 }
281 } else {
282 // Parse form: --argname base
283 for (int lang=0; lang<EShLangCount; ++lang)
284 base[lang].push_back(argv[1]);
285
286 argc--;
287 argv++;
288 }
289}
290
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600291//
John Kessenich68d78fd2015-07-12 19:28:10 -0600292// Do all command-line argument parsing. This includes building up the work-items
293// to be processed later, and saving all the command-line options.
294//
295// Does not return (it exits) if command-line is fatally flawed.
296//
Juan Lopeza558b262017-04-02 23:04:00 +0200297void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000298{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600299 baseSamplerBinding.fill(0);
300 baseTextureBinding.fill(0);
steve-lunarg9088be42016-11-01 10:31:42 -0600301 baseImageBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600302 baseUboBinding.fill(0);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700303 baseSsboBinding.fill(0);
steve-lunargbe283552017-04-18 12:18:01 -0600304 baseUavBinding.fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600305
John Kessenich38f3b892013-09-06 19:52:57 +0000306 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200307 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000308
John Kessenich2b07c7e2013-07-31 18:44:13 +0000309 argc--;
John Kessenichecba76f2017-01-06 00:34:48 -0700310 argv++;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000311 for (; argc >= 1; argc--, argv++) {
312 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000313 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600314 case '-':
315 {
316 std::string lowerword(argv[0]+2);
317 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
318
319 // handle --word style options
320 if (lowerword == "shift-sampler-bindings" || // synonyms
321 lowerword == "shift-sampler-binding" ||
322 lowerword == "ssb") {
323 ProcessBindingBase(argc, argv, baseSamplerBinding);
324 } else if (lowerword == "shift-texture-bindings" || // synonyms
325 lowerword == "shift-texture-binding" ||
326 lowerword == "stb") {
327 ProcessBindingBase(argc, argv, baseTextureBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600328 } else if (lowerword == "shift-image-bindings" || // synonyms
329 lowerword == "shift-image-binding" ||
330 lowerword == "sib") {
331 ProcessBindingBase(argc, argv, baseImageBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600332 } else if (lowerword == "shift-ubo-bindings" || // synonyms
333 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600334 lowerword == "shift-cbuffer-bindings" ||
335 lowerword == "shift-cbuffer-binding" ||
336 lowerword == "sub" ||
337 lowerword == "scb") {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600338 ProcessBindingBase(argc, argv, baseUboBinding);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700339 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
340 lowerword == "shift-ssbo-binding" ||
341 lowerword == "sbb") {
342 ProcessBindingBase(argc, argv, baseSsboBinding);
Hyangran Park36dc8292017-05-02 16:27:29 +0900343 } else if (lowerword == "resource-set-bindings" || // synonyms
344 lowerword == "resource-set-binding" ||
345 lowerword == "rsb") {
346 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunargbe283552017-04-18 12:18:01 -0600347 } else if (lowerword == "shift-uav-bindings" || // synonyms
348 lowerword == "shift-uav-binding" ||
349 lowerword == "suavb") {
350 ProcessBindingBase(argc, argv, baseUavBinding);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600351 } else if (lowerword == "auto-map-bindings" || // synonyms
John Kessenichecba76f2017-01-06 00:34:48 -0700352 lowerword == "auto-map-binding" ||
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600353 lowerword == "amb") {
354 Options |= EOptionAutoMapBindings;
steve-lunarge0b9deb2016-09-16 13:26:37 -0600355 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
356 lowerword == "flatten-uniform-array" ||
357 lowerword == "fua") {
358 Options |= EOptionFlattenUniformArrays;
steve-lunargcce8d482016-10-14 18:36:42 -0600359 } else if (lowerword == "no-storage-format" || // synonyms
360 lowerword == "nsf") {
361 Options |= EOptionNoStorageFormat;
Flavio15017db2017-02-15 14:29:33 -0800362 } else if (lowerword == "variable-name" || // synonyms
363 lowerword == "vn") {
364 Options |= EOptionOutputHexadecimal;
365 variableName = argv[1];
366 if (argc > 0) {
367 argc--;
368 argv++;
369 } else
370 Error("no <C-variable-name> provided for --variable-name");
371 break;
John Kessenich4f1403e2017-04-05 17:38:20 -0600372 } else if (lowerword == "source-entrypoint" || // synonyms
steve-lunargf1e0c872016-10-31 15:13:43 -0600373 lowerword == "sep") {
374 sourceEntryPointName = argv[1];
375 if (argc > 0) {
376 argc--;
377 argv++;
378 } else
379 Error("no <entry-point> provided for --source-entrypoint");
380 break;
John Kessenich906cc212016-12-09 19:22:20 -0700381 } else if (lowerword == "keep-uncalled" || // synonyms
382 lowerword == "ku") {
383 Options |= EOptionKeepUncalled;
John Kessenich4f1403e2017-04-05 17:38:20 -0600384 } else if (lowerword == "hlsl-offsets") {
385 Options |= EOptionHlslOffsets;
steve-lunargbe283552017-04-18 12:18:01 -0600386 } else if (lowerword == "hlsl-iomap" ||
387 lowerword == "hlsl-iomapper" ||
388 lowerword == "hlsl-iomapping") {
389 Options |= EOptionHlslIoMapping;
John Kessenich71facdf2017-05-17 18:28:19 -0600390 } else if (lowerword == "auto-map-locations" || // synonyms
391 lowerword == "aml") {
392 Options |= EOptionAutoMapLocations;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600393 } else {
394 usage();
395 }
396 }
397 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000398 case 'H':
399 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600400 if ((Options & EOptionSpv) == 0) {
401 // default to Vulkan
402 Options |= EOptionSpv;
403 Options |= EOptionVulkanRules;
404 Options |= EOptionLinkProgram;
405 }
406 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000407 case 'V':
408 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600409 Options |= EOptionVulkanRules;
410 Options |= EOptionLinkProgram;
411 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400412 case 'S':
Dan Bakerc6ede892016-08-11 14:06:06 -0400413 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400414 if (argc > 0) {
415 argc--;
416 argv++;
John Kesseniche751bca2017-03-16 11:20:38 -0600417 } else
Dan Baker5afdd782016-08-11 17:53:57 -0400418 Error("no <stage> specified for -S");
dankbaker45d49bc2016-08-08 21:43:07 -0400419 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600420 case 'G':
421 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000422 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600423 // undo a -H default to Vulkan
424 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000425 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000426 case 'E':
427 Options |= EOptionOutputPreprocessed;
428 break;
John Kessenich05a70632013-09-17 19:26:08 +0000429 case 'c':
430 Options |= EOptionDumpConfig;
431 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600432 case 'C':
433 Options |= EOptionCascadingErrors;
434 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000435 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000436 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000437 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700438 case 'D':
439 Options |= EOptionReadHlsl;
440 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700441 case 'e':
442 // HLSL todo: entry point handle needs much more sophistication.
443 // This is okay for one compilation unit with one entry point.
444 entryPointName = argv[1];
445 if (argc > 0) {
446 argc--;
447 argv++;
448 } else
449 Error("no <entry-point> provided for -e");
450 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600451 case 'h':
452 usage();
453 break;
John Kessenich05a70632013-09-17 19:26:08 +0000454 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000455 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000456 break;
457 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000458 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000459 break;
460 case 'm':
461 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000462 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600463 case 'o':
464 binaryFileName = argv[1];
465 if (argc > 0) {
466 argc--;
467 argv++;
468 } else
469 Error("no <file> provided for -o");
470 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000471 case 'q':
472 Options |= EOptionDumpReflection;
473 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000474 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000475 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000476 break;
477 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000478 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000479 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000480 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200481 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000482 break;
John Kessenich319de232013-12-04 04:43:40 +0000483 case 'v':
484 Options |= EOptionDumpVersions;
485 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000486 case 'w':
487 Options |= EOptionSuppressWarnings;
488 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500489 case 'x':
490 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500491 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000492 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600493 usage();
494 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000495 }
John Kessenich38f3b892013-09-06 19:52:57 +0000496 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000497 std::string name(argv[0]);
498 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200499 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000500 }
John Kessenich38f3b892013-09-06 19:52:57 +0000501 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000502 }
503
John Kessenich68d78fd2015-07-12 19:28:10 -0600504 // Make sure that -E is not specified alongside linking (which includes SPV generation)
505 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
506 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000507
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500508 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600509 if (binaryFileName && (Options & EOptionSpv) == 0)
510 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600511
512 if ((Options & EOptionFlattenUniformArrays) != 0 &&
513 (Options & EOptionReadHlsl) == 0)
514 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000515}
516
John Kessenich68d78fd2015-07-12 19:28:10 -0600517//
518// Translate the meaningful subset of command-line options to parser-behavior options.
519//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000520void SetMessageOptions(EShMessages& messages)
521{
522 if (Options & EOptionRelaxedErrors)
523 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
524 if (Options & EOptionIntermediate)
525 messages = (EShMessages)(messages | EShMsgAST);
526 if (Options & EOptionSuppressWarnings)
527 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600528 if (Options & EOptionSpv)
529 messages = (EShMessages)(messages | EShMsgSpvRules);
530 if (Options & EOptionVulkanRules)
531 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400532 if (Options & EOptionOutputPreprocessed)
533 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700534 if (Options & EOptionReadHlsl)
535 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600536 if (Options & EOptionCascadingErrors)
537 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700538 if (Options & EOptionKeepUncalled)
539 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600540 if (Options & EOptionHlslOffsets)
541 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000542}
543
John Kessenich68d78fd2015-07-12 19:28:10 -0600544//
John Kessenich69f4b512013-09-04 21:19:27 +0000545// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000546//
Juan Lopeza558b262017-04-02 23:04:00 +0200547void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000548{
John Kessenich38f3b892013-09-06 19:52:57 +0000549 glslang::TWorkItem* workItem;
Juan Lopeza558b262017-04-02 23:04:00 +0200550 while (worklist.remove(workItem)) {
John Kessenich38f3b892013-09-06 19:52:57 +0000551 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000552 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200553 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000554
John Kessenichb0a7eb52013-11-07 17:44:20 +0000555 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000556
John Kessenich94a81fb2013-08-31 02:41:30 +0000557 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000558 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000559
560 ShDestruct(compiler);
561 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000562}
563
John Kessenich6626cad2015-06-19 05:14:19 +0000564// Outputs the given string, but only if it is non-null and non-empty.
565// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400566void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000567{
568 if (str && str[0]) {
569 puts(str);
570 }
571}
572
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400573// Outputs the given string to stderr, but only if it is non-null and non-empty.
574// This prevents erroneous newlines from appearing.
575void StderrIfNonEmpty(const char* str)
576{
577 if (str && str[0]) {
578 fprintf(stderr, "%s\n", str);
579 }
580}
581
John Kessenichc57b2a92016-01-16 15:30:03 -0700582// Simple bundling of what makes a compilation unit for ease in passing around,
583// and separation of handling file IO versus API (programmatic) compilation.
584struct ShaderCompUnit {
585 EShLanguage stage;
586 std::string fileName;
dankbakerafe6e9c2016-08-21 12:29:08 -0400587 char** text; // memory owned/managed externally
John Kesseniche751bca2017-03-16 11:20:38 -0600588 const char* fileNameList[1];
dankbakerafe6e9c2016-08-21 12:29:08 -0400589
John Kessenich219b0252016-08-23 17:51:13 -0600590 // Need to have a special constructors to adjust the fileNameList, since back end needs a list of ptrs
dankbakerafe6e9c2016-08-21 12:29:08 -0400591 ShaderCompUnit(EShLanguage istage, std::string &ifileName, char** itext)
592 {
593 stage = istage;
594 fileName = ifileName;
John Kesseniche751bca2017-03-16 11:20:38 -0600595 text = itext;
John Kessenich219b0252016-08-23 17:51:13 -0600596 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400597 }
598
599 ShaderCompUnit(const ShaderCompUnit &rhs)
600 {
601 stage = rhs.stage;
602 fileName = rhs.fileName;
603 text = rhs.text;
John Kessenich219b0252016-08-23 17:51:13 -0600604 fileNameList[0] = fileName.c_str();
dankbakerafe6e9c2016-08-21 12:29:08 -0400605 }
606
John Kessenichc57b2a92016-01-16 15:30:03 -0700607};
608
John Kessenich69f4b512013-09-04 21:19:27 +0000609//
John Kessenichc57b2a92016-01-16 15:30:03 -0700610// For linking mode: Will independently parse each compilation unit, but then put them
611// in the same program and link them together, making at most one linked module per
612// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000613//
614// Uses the new C++ interface instead of the old handle-based interface.
615//
John Kessenichc57b2a92016-01-16 15:30:03 -0700616
617void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000618{
619 // keep track of what to free
620 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700621
John Kessenich69f4b512013-09-04 21:19:27 +0000622 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000623 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000624
John Kessenich69f4b512013-09-04 21:19:27 +0000625 //
626 // Per-shader processing...
627 //
628
John Kessenich5b0f13a2013-11-01 03:08:40 +0000629 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100630 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
631 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700632 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
dankbakerafe6e9c2016-08-21 12:29:08 -0400633 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700634 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
635 shader->setEntryPoint(entryPointName);
steve-lunargf1e0c872016-10-31 15:13:43 -0600636 if (sourceEntryPointName)
637 shader->setSourceEntryPoint(sourceEntryPointName);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600638
639 shader->setShiftSamplerBinding(baseSamplerBinding[compUnit.stage]);
640 shader->setShiftTextureBinding(baseTextureBinding[compUnit.stage]);
steve-lunarg9088be42016-11-01 10:31:42 -0600641 shader->setShiftImageBinding(baseImageBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600642 shader->setShiftUboBinding(baseUboBinding[compUnit.stage]);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700643 shader->setShiftSsboBinding(baseSsboBinding[compUnit.stage]);
steve-lunargbe283552017-04-18 12:18:01 -0600644 shader->setShiftUavBinding(baseUavBinding[compUnit.stage]);
steve-lunarge0b9deb2016-09-16 13:26:37 -0600645 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600646 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900647 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600648
steve-lunargbe283552017-04-18 12:18:01 -0600649 if (Options & EOptionHlslIoMapping)
650 shader->setHlslIoMapping(true);
651
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600652 if (Options & EOptionAutoMapBindings)
653 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700654
John Kessenich71facdf2017-05-17 18:28:19 -0600655 if (Options & EOptionAutoMapLocations)
656 shader->setAutoMapLocations(true);
657
John Kessenich69f4b512013-09-04 21:19:27 +0000658 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600659
John Kessenichc555ddd2015-06-17 02:38:44 +0000660 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000661
John Kessenichc555ddd2015-06-17 02:38:44 +0000662 if (Options & EOptionOutputPreprocessed) {
663 std::string str;
John Kessenichfacde2c2017-01-06 16:48:18 -0700664 glslang::TShader::ForbidIncluder includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400665 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500666 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400667 PutsIfNonEmpty(str.c_str());
668 } else {
669 CompileFailed = true;
670 }
671 StderrIfNonEmpty(shader->getInfoLog());
672 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000673 continue;
674 }
675 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000676 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000677
John Kessenich69f4b512013-09-04 21:19:27 +0000678 program.addShader(shader);
679
John Kessenichc57b2a92016-01-16 15:30:03 -0700680 if (! (Options & EOptionSuppressInfolog) &&
681 ! (Options & EOptionMemoryLeakMode)) {
682 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400683 PutsIfNonEmpty(shader->getInfoLog());
684 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000685 }
John Kessenich69f4b512013-09-04 21:19:27 +0000686 }
687
688 //
689 // Program-level processing...
690 //
691
John Kessenich4d65ee32016-03-12 18:17:47 -0700692 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600693 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000694 LinkFailed = true;
695
steve-lunarg9ae34742016-10-05 13:40:13 -0600696 // Map IO
697 if (Options & EOptionSpv) {
698 if (!program.mapIO())
699 LinkFailed = true;
700 }
John Kessenichecba76f2017-01-06 00:34:48 -0700701
John Kessenich4d65ee32016-03-12 18:17:47 -0700702 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700703 if (! (Options & EOptionSuppressInfolog) &&
704 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400705 PutsIfNonEmpty(program.getInfoLog());
706 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000707 }
708
John Kessenich4d65ee32016-03-12 18:17:47 -0700709 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000710 if (Options & EOptionDumpReflection) {
711 program.buildReflection();
712 program.dumpReflection();
713 }
714
John Kessenich4d65ee32016-03-12 18:17:47 -0700715 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000716 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000717 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600718 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000719 else {
720 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000721 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000722 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400723 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400724 spv::SpvBuildLogger logger;
725 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700726
727 // Dump the spv to a file or stdout, etc., but only if not doing
728 // memory/perf testing, as it's not internal to programmatic use.
729 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500730 printf("%s", logger.getAllMessages().c_str());
731 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700732 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500733 } else {
734 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
735 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700736 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700737 spv::Disassemble(std::cout, spirv);
738 }
John Kessenichacba7722015-03-04 03:48:38 +0000739 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000740 }
John Kessenich92f90382014-07-28 04:21:04 +0000741 }
742 }
743 }
744
John Kessenich5b0f13a2013-11-01 03:08:40 +0000745 // Free everything up, program has to go before the shaders
746 // because it might have merged stuff from the shaders, and
747 // the stuff from the shaders has to have its destructors called
748 // before the pools holding the memory in the shaders is freed.
749 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000750 while (shaders.size() > 0) {
751 delete shaders.back();
752 shaders.pop_back();
753 }
John Kessenich69f4b512013-09-04 21:19:27 +0000754}
755
John Kessenichc57b2a92016-01-16 15:30:03 -0700756//
757// Do file IO part of compile and link, handing off the pure
758// API/programmatic mode to CompileAndLinkShaderUnits(), which can
759// be put in a loop for testing memory footprint and performance.
760//
761// This is just for linking mode: meaning all the shaders will be put into the
762// the same program linked together.
763//
764// This means there are a limited number of work items (not multi-threading mode)
765// and that the point is testing at the linking level. Hence, to enable
766// performance and memory testing, the actual compile/link can be put in
767// a loop, independent of processing the work items and file IO.
768//
Juan Lopeza558b262017-04-02 23:04:00 +0200769void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700770{
771 std::vector<ShaderCompUnit> compUnits;
772
773 // Transfer all the work items from to a simple list of
774 // of compilation units. (We don't care about the thread
775 // work-item distribution properties in this path, which
776 // is okay due to the limited number of shaders, know since
777 // they are all getting linked together.)
778 glslang::TWorkItem* workItem;
779 while (Worklist.remove(workItem)) {
baldurk31d5d482016-10-13 19:25:52 +0200780 ShaderCompUnit compUnit(
John Kessenichc57b2a92016-01-16 15:30:03 -0700781 FindLanguage(workItem->name),
782 workItem->name,
783 ReadFileData(workItem->name.c_str())
baldurk31d5d482016-10-13 19:25:52 +0200784 );
John Kessenichc57b2a92016-01-16 15:30:03 -0700785
786 if (! compUnit.text) {
787 usage();
788 return;
789 }
790
791 compUnits.push_back(compUnit);
792 }
793
794 // Actual call to programmatic processing of compile and link,
795 // in a loop for testing memory and performance. This part contains
796 // all the perf/memory that a programmatic consumer will care about.
797 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
798 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
799 CompileAndLinkShaderUnits(compUnits);
800
801 if (Options & EOptionMemoryLeakMode)
802 glslang::OS_DumpMemoryCounters();
803 }
804
rdb32084e82016-02-23 22:17:38 +0100805 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
806 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700807}
808
John Kessenicha0af4732012-12-12 21:15:54 +0000809int C_DECL main(int argc, char* argv[])
810{
Juan Lopeza558b262017-04-02 23:04:00 +0200811 // array of unique places to leave the shader names and infologs for the asynchronous compiles
812 std::vector<std::unique_ptr<glslang::TWorkItem>> workItems;
813 ProcessArguments(workItems, argc, argv);
814
815 glslang::TWorklist workList;
816 std::for_each(workItems.begin(), workItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
817 assert(item);
818 workList.add(item.get());
819 });
John Kessenich2b07c7e2013-07-31 18:44:13 +0000820
John Kessenich05a70632013-09-17 19:26:08 +0000821 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500822 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +0200823 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +0000824 return ESuccess;
825 }
826
John Kessenich0da9eaa2015-08-01 17:10:02 -0600827 if (Options & EOptionDumpVersions) {
828 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000829 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
830 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600831 std::string spirvVersion;
832 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600833 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600834 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700835 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600836 printf("GL_KHR_vulkan_glsl version %d\n", 100);
837 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +0200838 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +0000839 return ESuccess;
840 }
841
Juan Lopeza558b262017-04-02 23:04:00 +0200842 if (workList.empty()) {
John Kessenich05a70632013-09-17 19:26:08 +0000843 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000844 }
845
846 ProcessConfigFile();
847
John Kessenich69f4b512013-09-04 21:19:27 +0000848 //
849 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000850 // 1) linking all arguments together, single-threaded, new C++ interface
851 // 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 +0000852 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000853 if (Options & EOptionLinkProgram ||
854 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000855 glslang::InitializeProcess();
Juan Lopeza558b262017-04-02 23:04:00 +0200856 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +0000857 glslang::FinalizeProcess();
858 } else {
859 ShInitialize();
860
Juan Lopeza558b262017-04-02 23:04:00 +0200861 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000862
Juan Lopeza558b262017-04-02 23:04:00 +0200863 if (Options & EOptionMultiThreaded)
864 {
865 std::array<std::thread, 16> threads;
866 for (unsigned int t = 0; t < threads.size(); ++t)
867 {
868 threads[t] = std::thread(CompileShaders, std::ref(workList));
869 if (threads[t].get_id() == std::thread::id())
870 {
John Kessenich38f3b892013-09-06 19:52:57 +0000871 printf("Failed to create thread\n");
872 return EFailThreadCreate;
873 }
John Kessenicha0af4732012-12-12 21:15:54 +0000874 }
Juan Lopeza558b262017-04-02 23:04:00 +0200875
876 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +0000877 } else
Juan Lopeza558b262017-04-02 23:04:00 +0200878 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +0000879
880 // Print out all the resulting infologs
Juan Lopeza558b262017-04-02 23:04:00 +0200881 for (size_t w = 0; w < workItems.size(); ++w) {
882 if (workItems[w]) {
883 if (printShaderNames || workItems[w]->results.size() > 0)
884 PutsIfNonEmpty(workItems[w]->name.c_str());
885 PutsIfNonEmpty(workItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000886 }
887 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000888
889 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000890 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000891
John Kessenichc999ba22013-11-07 23:33:24 +0000892 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000893 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000894 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000895 return EFailLink;
896
897 return 0;
898}
899
900//
901// Deduce the language from the filename. Files must end in one of the
902// following extensions:
903//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000904// .vert = vertex
905// .tesc = tessellation control
906// .tese = tessellation evaluation
907// .geom = geometry
908// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000909// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000910//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600911EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +0000912{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600913 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -0600914 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600915
Dan Bakerc6ede892016-08-11 14:06:06 -0400916 if (shaderStageName)
917 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -0600918 else {
919 // Search for a suffix on a filename: e.g, "myfile.frag". If given
920 // the suffix directly, we skip looking for the '.'
921 if (parseSuffix) {
922 ext = name.rfind('.');
923 if (ext == std::string::npos) {
924 usage();
925 return EShLangVertex;
926 }
927 ++ext;
928 }
929 suffix = name.substr(ext, std::string::npos);
930 }
dankbaker45d49bc2016-08-08 21:43:07 -0400931
John Kessenich2b07c7e2013-07-31 18:44:13 +0000932 if (suffix == "vert")
933 return EShLangVertex;
934 else if (suffix == "tesc")
935 return EShLangTessControl;
936 else if (suffix == "tese")
937 return EShLangTessEvaluation;
938 else if (suffix == "geom")
939 return EShLangGeometry;
940 else if (suffix == "frag")
941 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000942 else if (suffix == "comp")
943 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000944
945 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000946 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000947}
948
John Kessenicha0af4732012-12-12 21:15:54 +0000949//
John Kessenichecba76f2017-01-06 00:34:48 -0700950// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +0000951// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000952//
John Kessenich51cdd902014-02-18 23:37:57 +0000953void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000954{
John Kessenichca3457f2015-05-18 01:59:45 +0000955 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000956 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000957 if (! shaderStrings) {
958 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000959 }
960
John Kessenich41cf6b52013-06-25 18:10:05 +0000961 int* lengths = new int[NumShaderStrings];
962
963 // move to length-based strings, rather than null-terminated strings
964 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000965 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000966
John Kessenichc999ba22013-11-07 23:33:24 +0000967 if (! shaderStrings) {
968 CompileFailed = true;
969 return;
970 }
John Kessenicha0af4732012-12-12 21:15:54 +0000971
John Kessenich52ac67e2013-05-05 23:46:22 +0000972 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000973 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -0700974
John Kessenich94a81fb2013-08-31 02:41:30 +0000975 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
976 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -0700977 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000978 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -0700979 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -0700980 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +0000981 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -0700982 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
983 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000984 }
John Kessenicha0af4732012-12-12 21:15:54 +0000985
John Kessenich94a81fb2013-08-31 02:41:30 +0000986 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000987 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000988 }
John Kessenicha0af4732012-12-12 21:15:54 +0000989
John Kessenich41cf6b52013-06-25 18:10:05 +0000990 delete [] lengths;
991 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000992
John Kessenichc999ba22013-11-07 23:33:24 +0000993 if (ret == 0)
994 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000995}
996
John Kessenicha0af4732012-12-12 21:15:54 +0000997//
998// print usage to stdout
999//
1000void usage()
1001{
John Kessenich319de232013-12-04 04:43:40 +00001002 printf("Usage: glslangValidator [option]... [file]...\n"
1003 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +00001004 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001005 " .conf to provide an optional config file that replaces the default configuration\n"
1006 " (see -c option below for generating a template)\n"
1007 " .vert for a vertex shader\n"
1008 " .tesc for a tessellation control shader\n"
1009 " .tese for a tessellation evaluation shader\n"
1010 " .geom for a geometry shader\n"
1011 " .frag for a fragment shader\n"
1012 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001013 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001014 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +00001015 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +00001016 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001017 "Each option must be specified separately.\n"
1018 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
1019 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001020 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
1021 " default file name is <stage>.spv (-o overrides this)\n"
1022 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -04001023 " -E print pre-processed GLSL; cannot be used with -l;\n"
1024 " errors will appear on stderr.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001025 " -S <stage> uses specified stage rather than parsing the file extension\n"
1026 " valid choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001027 " -c configuration dump;\n"
1028 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -06001029 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001030 " -d default to desktop (#version 110) when there is no shader #version\n"
1031 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -07001032 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -07001033 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001034 " -h print this usage message\n"
1035 " -i intermediate tree (glslang AST) is printed out\n"
1036 " -l link all input files together to form a single module\n"
1037 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -05001038 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001039 " -q dump reflection query database\n"
1040 " -r relaxed semantic error-checking mode\n"
1041 " -s silent mode\n"
1042 " -t multi-threaded mode\n"
1043 " -v print version strings\n"
1044 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -05001045 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001046 "\n"
1047 " --shift-sampler-binding [stage] num set base binding number for samplers\n"
1048 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1049 "\n"
1050 " --shift-texture-binding [stage] num set base binding number for textures\n"
1051 " --stb [stage] num synonym for --shift-texture-binding\n"
1052 "\n"
steve-lunarg9088be42016-11-01 10:31:42 -06001053 " --shift-image-binding [stage] num set base binding number for images (uav)\n"
1054 " --sib [stage] num synonym for --shift-image-binding\n"
1055 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001056 " --shift-UBO-binding [stage] num set base binding number for UBOs\n"
steve-lunargbe283552017-04-18 12:18:01 -06001057 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001058 " --sub [stage] num synonym for --shift-UBO-binding\n"
1059 "\n"
steve-lunarg932bb5c2017-02-21 17:19:08 -07001060 " --shift-ssbo-binding [stage] num set base binding number for SSBOs\n"
1061 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1062 "\n"
Hyangran Park36dc8292017-05-02 16:27:29 +09001063 " --resource-set-binding [stage] num set descriptor set and binding number for resources\n"
1064 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1065 "\n"
steve-lunargbe283552017-04-18 12:18:01 -06001066 " --shift-uav-binding [stage] num set base binding number for UAVs\n"
1067 " --suavb [stage] num synonym for --shift-uav-binding\n"
1068 "\n"
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001069 " --auto-map-bindings automatically bind uniform variables without\n"
1070 " explicit bindings.\n"
1071 " --amb synonym for --auto-map-bindings\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001072 "\n"
John Kessenich71facdf2017-05-17 18:28:19 -06001073 " --auto-map-locations automatically locate input/output lacking 'location'\n"
1074 " (fragile, not cross stage: recommend explicit\n"
1075 " 'location' use in shader)\n"
1076 " --aml synonym for --auto-map-locations\n"
1077 "\n"
steve-lunargbc9b7652016-09-29 08:43:22 -06001078 " --flatten-uniform-arrays flatten uniform texture & sampler arrays to scalars\n"
steve-lunarge0b9deb2016-09-16 13:26:37 -06001079 " --fua synonym for --flatten-uniform-arrays\n"
steve-lunargcce8d482016-10-14 18:36:42 -06001080 "\n"
1081 " --no-storage-format use Unknown image format\n"
1082 " --nsf synonym for --no-storage-format\n"
steve-lunargf1e0c872016-10-31 15:13:43 -06001083 "\n"
1084 " --source-entrypoint name the given shader source function is renamed to be the entry point given in -e\n"
1085 " --sep synonym for --source-entrypoint\n"
John Kessenich906cc212016-12-09 19:22:20 -07001086 "\n"
1087 " --keep-uncalled don't eliminate uncalled functions when linking\n"
1088 " --ku synonym for --keep-uncalled\n"
John Kessenich4f1403e2017-04-05 17:38:20 -06001089 "\n"
1090 " --variable-name <name> Creates a C header file that contains a uint32_t array named <name>\n"
1091 " initialized with the shader binary code.\n"
1092 " --vn <name> synonym for --variable-name <name>\n"
1093 "\n"
1094 " --hlsl-offsets Allow block offsets to follow HLSL rules instead of GLSL rules.\n"
1095 " Works independently of source language.\n"
steve-lunargbe283552017-04-18 12:18:01 -06001096 "\n"
1097 " --hlsl-iomap Perform IO mapping in HLSL register space.\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001098 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001099
1100 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001101}
1102
John Kessenich3ce4e592014-10-06 19:57:34 +00001103#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001104
1105#include <errno.h>
1106
1107int fopen_s(
1108 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001109 const char* filename,
1110 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001111)
1112{
1113 if (!pFile || !filename || !mode) {
1114 return EINVAL;
1115 }
1116
1117 FILE* f = fopen(filename, mode);
1118 if (! f) {
1119 if (errno != 0) {
1120 return errno;
1121 } else {
1122 return ENOENT;
1123 }
1124 }
1125 *pFile = f;
1126
1127 return 0;
1128}
1129
1130#endif
1131
John Kessenicha0af4732012-12-12 21:15:54 +00001132//
1133// Malloc a string of sufficient size and read a string into it.
1134//
John Kessenichecba76f2017-01-06 00:34:48 -07001135char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001136{
John Kessenichb3297152015-07-11 18:01:03 -06001137 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001138 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +00001139
John Kessenicha0af4732012-12-12 21:15:54 +00001140 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001141 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
1142 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001143
John Kessenich68d78fd2015-07-12 19:28:10 -06001144 if (errorCode || in == nullptr)
1145 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001146
John Kessenicha0af4732012-12-12 21:15:54 +00001147 while (fgetc(in) != EOF)
1148 count++;
1149
John Kessenichd6c72a42014-08-18 19:42:35 +00001150 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001151
John Kessenichb3297152015-07-11 18:01:03 -06001152 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -06001153 if (! fdata)
1154 Error("can't allocate memory");
1155
John Kessenichb3297152015-07-11 18:01:03 -06001156 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -06001157 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -06001158 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001159 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001160
John Kessenicha0af4732012-12-12 21:15:54 +00001161 fdata[count] = '\0';
1162 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001163
John Kessenichea869fb2013-10-28 18:12:06 +00001164 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -06001165 // recover from empty file
1166 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +00001167 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +00001168 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -06001169 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +00001170
John Kessenichb3297152015-07-11 18:01:03 -06001171 return return_data;
1172 } else
1173 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
1174
1175 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +00001176 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -06001177 int ptr_len = 0;
1178 int i = 0;
1179 while (count > 0) {
1180 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
1181 memcpy(return_data[i], fdata + ptr_len, len);
1182 return_data[i][len] = '\0';
1183 count -= len;
1184 ptr_len += len;
1185 if (count < len) {
1186 if (count == 0) {
1187 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +00001188 break;
1189 }
John Kessenichb3297152015-07-11 18:01:03 -06001190 len = count;
John Kessenichecba76f2017-01-06 00:34:48 -07001191 }
John Kessenichd6c72a42014-08-18 19:42:35 +00001192 ++i;
1193 }
John Kessenichb3297152015-07-11 18:01:03 -06001194
1195 free(fdata);
1196
John Kessenicha0af4732012-12-12 21:15:54 +00001197 return return_data;
1198}
1199
John Kessenich51cdd902014-02-18 23:37:57 +00001200void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +00001201{
John Kessenichb3297152015-07-11 18:01:03 -06001202 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +00001203 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -06001204
1205 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001206}
1207
John Kessenich54d8cda2013-02-11 22:36:01 +00001208void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001209{
John Kessenichfae38ee2015-06-10 23:23:12 +00001210 if (num >= 0 )
1211 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1212 else
1213 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001214}