blob: bff5dcdb398b0b905e99eaa1fedaf608637bd606 [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
2//Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
John Kessenichbd0747d2013-02-17 06:01:50 +00003//Copyright (C) 2013 LunarG, Inc.
4//
John Kessenicha0af4732012-12-12 21:15:54 +00005//All rights reserved.
6//
7//Redistribution and use in source and binary forms, with or without
8//modification, are permitted provided that the following conditions
9//are met:
10//
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//
23//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.
35//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
38#define _CRT_SECURE_NO_WARNINGS
39
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040040#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000041#include "Worklist.h"
John Kessenicha0af4732012-12-12 21:15:54 +000042#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060043#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000044#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000045#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060046#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000047#include "../SPIRV/doc.h"
48#include "../SPIRV/disassemble.h"
John Kessenicha0af4732012-12-12 21:15:54 +000049#include <string.h>
John Kessenichcfd643e2013-03-08 23:14:42 +000050#include <stdlib.h>
John Kessenicha0af4732012-12-12 21:15:54 +000051#include <math.h>
52
baldurk876a0e32015-11-16 18:03:28 +010053#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000054
55extern "C" {
56 SH_IMPORT_EXPORT void ShOutputHtml();
57}
58
John Kessenich94a81fb2013-08-31 02:41:30 +000059// Command-line options
60enum TOptions {
John Kessenichacba7722015-03-04 03:48:38 +000061 EOptionNone = 0x0000,
62 EOptionIntermediate = 0x0001,
63 EOptionSuppressInfolog = 0x0002,
64 EOptionMemoryLeakMode = 0x0004,
65 EOptionRelaxedErrors = 0x0008,
66 EOptionGiveWarnings = 0x0010,
67 EOptionLinkProgram = 0x0020,
68 EOptionMultiThreaded = 0x0040,
69 EOptionDumpConfig = 0x0080,
70 EOptionDumpReflection = 0x0100,
71 EOptionSuppressWarnings = 0x0200,
72 EOptionDumpVersions = 0x0400,
73 EOptionSpv = 0x0800,
74 EOptionHumanReadableSpv = 0x1000,
John Kessenich68d78fd2015-07-12 19:28:10 -060075 EOptionVulkanRules = 0x2000,
76 EOptionDefaultDesktop = 0x4000,
77 EOptionOutputPreprocessed = 0x8000,
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -050078 EOptionOutputHexadecimal = 0x10000,
79 EOptionReadHlsl = 0x20000,
John Kessenich94a81fb2013-08-31 02:41:30 +000080};
81
John Kessenicha0af4732012-12-12 21:15:54 +000082//
John Kessenich68d78fd2015-07-12 19:28:10 -060083// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000084//
85enum TFailCode {
86 ESuccess = 0,
87 EFailUsage,
88 EFailCompile,
89 EFailLink,
90 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000091 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000092 EFailLinkerCreate
93};
94
95//
John Kessenich68d78fd2015-07-12 19:28:10 -060096// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +000097//
John Kessenichb603f912013-08-29 00:39:25 +000098EShLanguage FindLanguage(const std::string& name);
John Kessenich51cdd902014-02-18 23:37:57 +000099void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000100void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000101void FreeFileData(char** data);
102char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000103void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000104
John Kessenichc999ba22013-11-07 23:33:24 +0000105// Globally track if any compile or link failure.
106bool CompileFailed = false;
107bool LinkFailed = false;
108
John Kessenich05a70632013-09-17 19:26:08 +0000109// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600110// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000111int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000112
John Kessenich05a70632013-09-17 19:26:08 +0000113TBuiltInResource Resources;
114std::string ConfigFile;
115
John Kessenicha0af4732012-12-12 21:15:54 +0000116//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600117// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000118//
119void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000120{
John Kessenich05a70632013-09-17 19:26:08 +0000121 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000122 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000123 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000124 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000125 if (configStrings)
126 config = *configStrings;
127 else {
128 printf("Error opening configuration file; will instead use the default configuration\n");
129 usage();
130 }
131 }
132
133 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500134 Resources = glslang::DefaultTBuiltInResource;
135 return;
John Kessenich05a70632013-09-17 19:26:08 +0000136 }
137
Lei Zhang1b141722016-05-19 13:50:49 -0400138 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000139
John Kessenich05a70632013-09-17 19:26:08 +0000140 if (configStrings)
141 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500142 else
143 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000144}
145
John Kessenich38f3b892013-09-06 19:52:57 +0000146// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000147glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000148
149// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000150glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000151int NumWorkItems = 0;
152
John Kessenich94a81fb2013-08-31 02:41:30 +0000153int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600154const char* ExecutableName = nullptr;
155const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700156const char* entryPointName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600157
158//
159// Create the default name for saving a binary if -o is not provided.
160//
161const char* GetBinaryName(EShLanguage stage)
162{
163 const char* name;
164 if (binaryFileName == nullptr) {
165 switch (stage) {
166 case EShLangVertex: name = "vert.spv"; break;
167 case EShLangTessControl: name = "tesc.spv"; break;
168 case EShLangTessEvaluation: name = "tese.spv"; break;
169 case EShLangGeometry: name = "geom.spv"; break;
170 case EShLangFragment: name = "frag.spv"; break;
171 case EShLangCompute: name = "comp.spv"; break;
172 default: name = "unknown"; break;
173 }
174 } else
175 name = binaryFileName;
176
177 return name;
178}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000179
John Kessenich05a70632013-09-17 19:26:08 +0000180//
181// *.conf => this is a config file that can set limits/resources
182//
183bool SetConfigFile(const std::string& name)
184{
185 if (name.size() < 5)
186 return false;
187
John Kessenich4c706852013-10-11 16:28:43 +0000188 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000189 ConfigFile = name;
190 return true;
191 }
192
193 return false;
194}
195
John Kessenich68d78fd2015-07-12 19:28:10 -0600196//
197// Give error and exit with failure code.
198//
199void Error(const char* message)
200{
201 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
202 exit(EFailUsage);
203}
204
205//
206// Do all command-line argument parsing. This includes building up the work-items
207// to be processed later, and saving all the command-line options.
208//
209// Does not return (it exits) if command-line is fatally flawed.
210//
211void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000212{
John Kessenich38f3b892013-09-06 19:52:57 +0000213 ExecutableName = argv[0];
214 NumWorkItems = argc; // will include some empties where the '-' options were, but it doesn't matter, they'll be 0
John Kessenichfd305422014-06-05 16:30:53 +0000215 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600216 for (int w = 0; w < NumWorkItems; ++w)
217 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000218
John Kessenich2b07c7e2013-07-31 18:44:13 +0000219 argc--;
220 argv++;
221 for (; argc >= 1; argc--, argv++) {
222 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000223 switch (argv[0][1]) {
224 case 'H':
225 Options |= EOptionHumanReadableSpv;
226 // fall through to -V
John Kessenich0df0cde2015-03-03 17:09:43 +0000227 case 'V':
228 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600229 Options |= EOptionVulkanRules;
230 Options |= EOptionLinkProgram;
231 break;
232 case 'G':
233 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000234 Options |= EOptionLinkProgram;
John Kessenich92f90382014-07-28 04:21:04 +0000235 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000236 case 'E':
237 Options |= EOptionOutputPreprocessed;
238 break;
John Kessenich05a70632013-09-17 19:26:08 +0000239 case 'c':
240 Options |= EOptionDumpConfig;
241 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000242 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000243 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000244 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700245 case 'D':
246 Options |= EOptionReadHlsl;
247 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700248 case 'e':
249 // HLSL todo: entry point handle needs much more sophistication.
250 // This is okay for one compilation unit with one entry point.
251 entryPointName = argv[1];
252 if (argc > 0) {
253 argc--;
254 argv++;
255 } else
256 Error("no <entry-point> provided for -e");
257 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600258 case 'h':
259 usage();
260 break;
John Kessenich05a70632013-09-17 19:26:08 +0000261 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000262 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000263 break;
264 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000265 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000266 break;
267 case 'm':
268 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000269 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600270 case 'o':
271 binaryFileName = argv[1];
272 if (argc > 0) {
273 argc--;
274 argv++;
275 } else
276 Error("no <file> provided for -o");
277 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000278 case 'q':
279 Options |= EOptionDumpReflection;
280 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000281 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000282 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000283 break;
284 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000285 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000286 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000287 case 't':
288 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000289 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000290 #endif
291 break;
John Kessenich319de232013-12-04 04:43:40 +0000292 case 'v':
293 Options |= EOptionDumpVersions;
294 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000295 case 'w':
296 Options |= EOptionSuppressWarnings;
297 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500298 case 'x':
299 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500300 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000301 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600302 usage();
303 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000304 }
John Kessenich38f3b892013-09-06 19:52:57 +0000305 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000306 std::string name(argv[0]);
307 if (! SetConfigFile(name)) {
308 Work[argc] = new glslang::TWorkItem(name);
309 Worklist.add(Work[argc]);
310 }
John Kessenich38f3b892013-09-06 19:52:57 +0000311 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000312 }
313
John Kessenich68d78fd2015-07-12 19:28:10 -0600314 // Make sure that -E is not specified alongside linking (which includes SPV generation)
315 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
316 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000317
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500318 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600319 if (binaryFileName && (Options & EOptionSpv) == 0)
320 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000321}
322
John Kessenich68d78fd2015-07-12 19:28:10 -0600323//
324// Translate the meaningful subset of command-line options to parser-behavior options.
325//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000326void SetMessageOptions(EShMessages& messages)
327{
328 if (Options & EOptionRelaxedErrors)
329 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
330 if (Options & EOptionIntermediate)
331 messages = (EShMessages)(messages | EShMsgAST);
332 if (Options & EOptionSuppressWarnings)
333 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600334 if (Options & EOptionSpv)
335 messages = (EShMessages)(messages | EShMsgSpvRules);
336 if (Options & EOptionVulkanRules)
337 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400338 if (Options & EOptionOutputPreprocessed)
339 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700340 if (Options & EOptionReadHlsl)
341 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000342}
343
John Kessenich68d78fd2015-07-12 19:28:10 -0600344//
John Kessenich69f4b512013-09-04 21:19:27 +0000345// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000346//
347// Return 0 for failure, 1 for success.
348//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800349unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000350{
John Kessenich38f3b892013-09-06 19:52:57 +0000351 glslang::TWorkItem* workItem;
352 while (Worklist.remove(workItem)) {
353 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000354 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000355 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000356
John Kessenichb0a7eb52013-11-07 17:44:20 +0000357 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000358
John Kessenich94a81fb2013-08-31 02:41:30 +0000359 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000360 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000361
362 ShDestruct(compiler);
363 }
364
365 return 0;
366}
367
John Kessenich6626cad2015-06-19 05:14:19 +0000368// Outputs the given string, but only if it is non-null and non-empty.
369// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400370void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000371{
372 if (str && str[0]) {
373 puts(str);
374 }
375}
376
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400377// Outputs the given string to stderr, but only if it is non-null and non-empty.
378// This prevents erroneous newlines from appearing.
379void StderrIfNonEmpty(const char* str)
380{
381 if (str && str[0]) {
382 fprintf(stderr, "%s\n", str);
383 }
384}
385
John Kessenichc57b2a92016-01-16 15:30:03 -0700386// Simple bundling of what makes a compilation unit for ease in passing around,
387// and separation of handling file IO versus API (programmatic) compilation.
388struct ShaderCompUnit {
389 EShLanguage stage;
390 std::string fileName;
391 char** text; // memory owned/managed externally
392};
393
John Kessenich69f4b512013-09-04 21:19:27 +0000394//
John Kessenichc57b2a92016-01-16 15:30:03 -0700395// For linking mode: Will independently parse each compilation unit, but then put them
396// in the same program and link them together, making at most one linked module per
397// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000398//
399// Uses the new C++ interface instead of the old handle-based interface.
400//
John Kessenichc57b2a92016-01-16 15:30:03 -0700401
402void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000403{
404 // keep track of what to free
405 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700406
John Kessenich69f4b512013-09-04 21:19:27 +0000407 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000408 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000409
John Kessenich69f4b512013-09-04 21:19:27 +0000410 //
411 // Per-shader processing...
412 //
413
John Kessenich5b0f13a2013-11-01 03:08:40 +0000414 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100415 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
416 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700417 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
418 shader->setStrings(compUnit.text, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700419 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
420 shader->setEntryPoint(entryPointName);
John Kessenich69f4b512013-09-04 21:19:27 +0000421 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600422
John Kessenichc555ddd2015-06-17 02:38:44 +0000423 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000424
John Kessenichc555ddd2015-06-17 02:38:44 +0000425 if (Options & EOptionOutputPreprocessed) {
426 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500427 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400428 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500429 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400430 PutsIfNonEmpty(str.c_str());
431 } else {
432 CompileFailed = true;
433 }
434 StderrIfNonEmpty(shader->getInfoLog());
435 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000436 continue;
437 }
438 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000439 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000440
John Kessenich69f4b512013-09-04 21:19:27 +0000441 program.addShader(shader);
442
John Kessenichc57b2a92016-01-16 15:30:03 -0700443 if (! (Options & EOptionSuppressInfolog) &&
444 ! (Options & EOptionMemoryLeakMode)) {
445 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400446 PutsIfNonEmpty(shader->getInfoLog());
447 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000448 }
John Kessenich69f4b512013-09-04 21:19:27 +0000449 }
450
451 //
452 // Program-level processing...
453 //
454
John Kessenich4d65ee32016-03-12 18:17:47 -0700455 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600456 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000457 LinkFailed = true;
458
John Kessenich4d65ee32016-03-12 18:17:47 -0700459 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700460 if (! (Options & EOptionSuppressInfolog) &&
461 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400462 PutsIfNonEmpty(program.getInfoLog());
463 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000464 }
465
John Kessenich4d65ee32016-03-12 18:17:47 -0700466 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000467 if (Options & EOptionDumpReflection) {
468 program.buildReflection();
469 program.dumpReflection();
470 }
471
John Kessenich4d65ee32016-03-12 18:17:47 -0700472 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000473 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000474 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600475 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000476 else {
477 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000478 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000479 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400480 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400481 spv::SpvBuildLogger logger;
482 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700483
484 // Dump the spv to a file or stdout, etc., but only if not doing
485 // memory/perf testing, as it's not internal to programmatic use.
486 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500487 printf("%s", logger.getAllMessages().c_str());
488 if (Options & EOptionOutputHexadecimal) {
489 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
490 } else {
491 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
492 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700493 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700494 spv::Disassemble(std::cout, spirv);
495 }
John Kessenichacba7722015-03-04 03:48:38 +0000496 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000497 }
John Kessenich92f90382014-07-28 04:21:04 +0000498 }
499 }
500 }
501
John Kessenich5b0f13a2013-11-01 03:08:40 +0000502 // Free everything up, program has to go before the shaders
503 // because it might have merged stuff from the shaders, and
504 // the stuff from the shaders has to have its destructors called
505 // before the pools holding the memory in the shaders is freed.
506 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000507 while (shaders.size() > 0) {
508 delete shaders.back();
509 shaders.pop_back();
510 }
John Kessenich69f4b512013-09-04 21:19:27 +0000511}
512
John Kessenichc57b2a92016-01-16 15:30:03 -0700513//
514// Do file IO part of compile and link, handing off the pure
515// API/programmatic mode to CompileAndLinkShaderUnits(), which can
516// be put in a loop for testing memory footprint and performance.
517//
518// This is just for linking mode: meaning all the shaders will be put into the
519// the same program linked together.
520//
521// This means there are a limited number of work items (not multi-threading mode)
522// and that the point is testing at the linking level. Hence, to enable
523// performance and memory testing, the actual compile/link can be put in
524// a loop, independent of processing the work items and file IO.
525//
526void CompileAndLinkShaderFiles()
527{
528 std::vector<ShaderCompUnit> compUnits;
529
530 // Transfer all the work items from to a simple list of
531 // of compilation units. (We don't care about the thread
532 // work-item distribution properties in this path, which
533 // is okay due to the limited number of shaders, know since
534 // they are all getting linked together.)
535 glslang::TWorkItem* workItem;
536 while (Worklist.remove(workItem)) {
537 ShaderCompUnit compUnit = {
538 FindLanguage(workItem->name),
539 workItem->name,
540 ReadFileData(workItem->name.c_str())
541 };
542
543 if (! compUnit.text) {
544 usage();
545 return;
546 }
547
548 compUnits.push_back(compUnit);
549 }
550
551 // Actual call to programmatic processing of compile and link,
552 // in a loop for testing memory and performance. This part contains
553 // all the perf/memory that a programmatic consumer will care about.
554 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
555 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
556 CompileAndLinkShaderUnits(compUnits);
557
558 if (Options & EOptionMemoryLeakMode)
559 glslang::OS_DumpMemoryCounters();
560 }
561
rdb32084e82016-02-23 22:17:38 +0100562 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
563 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700564}
565
John Kessenicha0af4732012-12-12 21:15:54 +0000566int C_DECL main(int argc, char* argv[])
567{
John Kessenich68d78fd2015-07-12 19:28:10 -0600568 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000569
John Kessenich05a70632013-09-17 19:26:08 +0000570 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500571 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000572 if (Worklist.empty())
573 return ESuccess;
574 }
575
John Kessenich0da9eaa2015-08-01 17:10:02 -0600576 if (Options & EOptionDumpVersions) {
577 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000578 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
579 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600580 std::string spirvVersion;
581 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600582 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600583 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700584 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenich319de232013-12-04 04:43:40 +0000585 if (Worklist.empty())
586 return ESuccess;
587 }
588
John Kessenich05a70632013-09-17 19:26:08 +0000589 if (Worklist.empty()) {
590 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000591 }
592
593 ProcessConfigFile();
594
John Kessenich69f4b512013-09-04 21:19:27 +0000595 //
596 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000597 // 1) linking all arguments together, single-threaded, new C++ interface
598 // 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 +0000599 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000600 if (Options & EOptionLinkProgram ||
601 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000602 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700603 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000604 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500605 for (int w = 0; w < NumWorkItems; ++w) {
606 if (Work[w]) {
607 delete Work[w];
608 }
609 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000610 } else {
611 ShInitialize();
612
John Kessenich38f3b892013-09-06 19:52:57 +0000613 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000614
John Kessenich38f3b892013-09-06 19:52:57 +0000615 if (Options & EOptionMultiThreaded) {
616 const int NumThreads = 16;
617 void* threads[NumThreads];
618 for (int t = 0; t < NumThreads; ++t) {
619 threads[t] = glslang::OS_CreateThread(&CompileShaders);
620 if (! threads[t]) {
621 printf("Failed to create thread\n");
622 return EFailThreadCreate;
623 }
John Kessenicha0af4732012-12-12 21:15:54 +0000624 }
John Kessenich38f3b892013-09-06 19:52:57 +0000625 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000626 } else
627 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000628
629 // Print out all the resulting infologs
630 for (int w = 0; w < NumWorkItems; ++w) {
631 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600632 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400633 PutsIfNonEmpty(Work[w]->name.c_str());
634 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000635 delete Work[w];
636 }
637 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000638
639 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000640 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000641
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500642 delete[] Work;
643
John Kessenichc999ba22013-11-07 23:33:24 +0000644 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000645 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000646 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000647 return EFailLink;
648
649 return 0;
650}
651
652//
653// Deduce the language from the filename. Files must end in one of the
654// following extensions:
655//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000656// .vert = vertex
657// .tesc = tessellation control
658// .tese = tessellation evaluation
659// .geom = geometry
660// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000661// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000662//
John Kessenichb603f912013-08-29 00:39:25 +0000663EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000664{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000665 size_t ext = name.rfind('.');
666 if (ext == std::string::npos) {
667 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000668 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000669 }
670
John Kessenich2b07c7e2013-07-31 18:44:13 +0000671 std::string suffix = name.substr(ext + 1, std::string::npos);
672 if (suffix == "vert")
673 return EShLangVertex;
674 else if (suffix == "tesc")
675 return EShLangTessControl;
676 else if (suffix == "tese")
677 return EShLangTessEvaluation;
678 else if (suffix == "geom")
679 return EShLangGeometry;
680 else if (suffix == "frag")
681 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000682 else if (suffix == "comp")
683 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000684
685 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000686 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000687}
688
John Kessenicha0af4732012-12-12 21:15:54 +0000689//
John Kessenich69f4b512013-09-04 21:19:27 +0000690// Read a file's data into a string, and compile it using the old interface ShCompile,
691// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000692//
John Kessenich51cdd902014-02-18 23:37:57 +0000693void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000694{
John Kessenichca3457f2015-05-18 01:59:45 +0000695 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000696 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000697 if (! shaderStrings) {
698 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000699 }
700
John Kessenich41cf6b52013-06-25 18:10:05 +0000701 int* lengths = new int[NumShaderStrings];
702
703 // move to length-based strings, rather than null-terminated strings
704 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000705 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000706
John Kessenichc999ba22013-11-07 23:33:24 +0000707 if (! shaderStrings) {
708 CompileFailed = true;
709 return;
710 }
John Kessenicha0af4732012-12-12 21:15:54 +0000711
John Kessenich52ac67e2013-05-05 23:46:22 +0000712 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000713 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000714
John Kessenich94a81fb2013-08-31 02:41:30 +0000715 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
716 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000717 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000718 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000719 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
720 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
721 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000722 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000723 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000724 }
John Kessenicha0af4732012-12-12 21:15:54 +0000725
John Kessenich94a81fb2013-08-31 02:41:30 +0000726 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000727 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000728 }
John Kessenicha0af4732012-12-12 21:15:54 +0000729
John Kessenich41cf6b52013-06-25 18:10:05 +0000730 delete [] lengths;
731 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000732
John Kessenichc999ba22013-11-07 23:33:24 +0000733 if (ret == 0)
734 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000735}
736
John Kessenicha0af4732012-12-12 21:15:54 +0000737//
738// print usage to stdout
739//
740void usage()
741{
John Kessenich319de232013-12-04 04:43:40 +0000742 printf("Usage: glslangValidator [option]... [file]...\n"
743 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000744 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600745 " .conf to provide an optional config file that replaces the default configuration\n"
746 " (see -c option below for generating a template)\n"
747 " .vert for a vertex shader\n"
748 " .tesc for a tessellation control shader\n"
749 " .tese for a tessellation evaluation shader\n"
750 " .geom for a geometry shader\n"
751 " .frag for a fragment shader\n"
752 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000753 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000754 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000755 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000756 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600757 "Each option must be specified separately.\n"
758 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
759 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600760 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
761 " default file name is <stage>.spv (-o overrides this)\n"
762 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400763 " -E print pre-processed GLSL; cannot be used with -l;\n"
764 " errors will appear on stderr.\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600765 " -c configuration dump;\n"
766 " creates the default configuration file (redirect to a .conf file)\n"
767 " -d default to desktop (#version 110) when there is no shader #version\n"
768 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700769 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700770 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600771 " -h print this usage message\n"
772 " -i intermediate tree (glslang AST) is printed out\n"
773 " -l link all input files together to form a single module\n"
774 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500775 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600776 " -q dump reflection query database\n"
777 " -r relaxed semantic error-checking mode\n"
778 " -s silent mode\n"
779 " -t multi-threaded mode\n"
780 " -v print version strings\n"
781 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500782 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000783 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600784
785 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000786}
787
John Kessenich3ce4e592014-10-06 19:57:34 +0000788#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000789
790#include <errno.h>
791
792int fopen_s(
793 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000794 const char* filename,
795 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000796)
797{
798 if (!pFile || !filename || !mode) {
799 return EINVAL;
800 }
801
802 FILE* f = fopen(filename, mode);
803 if (! f) {
804 if (errno != 0) {
805 return errno;
806 } else {
807 return ENOENT;
808 }
809 }
810 *pFile = f;
811
812 return 0;
813}
814
815#endif
816
John Kessenicha0af4732012-12-12 21:15:54 +0000817//
818// Malloc a string of sufficient size and read a string into it.
819//
John Kessenich51cdd902014-02-18 23:37:57 +0000820char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000821{
John Kessenichb3297152015-07-11 18:01:03 -0600822 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +0000823 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000824
John Kessenicha0af4732012-12-12 21:15:54 +0000825 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600826 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
827 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000828
John Kessenich68d78fd2015-07-12 19:28:10 -0600829 if (errorCode || in == nullptr)
830 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000831
832 while (fgetc(in) != EOF)
833 count++;
834
John Kessenichd6c72a42014-08-18 19:42:35 +0000835 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000836
John Kessenichb3297152015-07-11 18:01:03 -0600837 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -0600838 if (! fdata)
839 Error("can't allocate memory");
840
John Kessenichb3297152015-07-11 18:01:03 -0600841 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -0600842 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -0600843 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000844 }
John Kessenich68d78fd2015-07-12 19:28:10 -0600845
John Kessenicha0af4732012-12-12 21:15:54 +0000846 fdata[count] = '\0';
847 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -0600848
John Kessenichea869fb2013-10-28 18:12:06 +0000849 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -0600850 // recover from empty file
851 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000852 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000853 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600854 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +0000855
John Kessenichb3297152015-07-11 18:01:03 -0600856 return return_data;
857 } else
858 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
859
860 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +0000861 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -0600862 int ptr_len = 0;
863 int i = 0;
864 while (count > 0) {
865 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
866 memcpy(return_data[i], fdata + ptr_len, len);
867 return_data[i][len] = '\0';
868 count -= len;
869 ptr_len += len;
870 if (count < len) {
871 if (count == 0) {
872 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000873 break;
874 }
John Kessenichb3297152015-07-11 18:01:03 -0600875 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +0000876 }
877 ++i;
878 }
John Kessenichb3297152015-07-11 18:01:03 -0600879
880 free(fdata);
881
John Kessenicha0af4732012-12-12 21:15:54 +0000882 return return_data;
883}
884
John Kessenich51cdd902014-02-18 23:37:57 +0000885void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +0000886{
John Kessenichb3297152015-07-11 18:01:03 -0600887 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000888 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -0600889
890 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +0000891}
892
John Kessenich54d8cda2013-02-11 22:36:01 +0000893void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000894{
John Kessenichfae38ee2015-06-10 23:23:12 +0000895 if (num >= 0 )
896 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
897 else
898 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +0000899}