blob: 229d17db0e114184d02a0e72a5ad3e06cae06c85 [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;
300 binaryFileName = argv[1];
301 if (argc > 0) {
302 argc--;
303 argv++;
304 } else
305 Error("no <file> provided for -x");
306 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000307 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600308 usage();
309 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000310 }
John Kessenich38f3b892013-09-06 19:52:57 +0000311 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000312 std::string name(argv[0]);
313 if (! SetConfigFile(name)) {
314 Work[argc] = new glslang::TWorkItem(name);
315 Worklist.add(Work[argc]);
316 }
John Kessenich38f3b892013-09-06 19:52:57 +0000317 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000318 }
319
John Kessenich68d78fd2015-07-12 19:28:10 -0600320 // Make sure that -E is not specified alongside linking (which includes SPV generation)
321 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
322 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000323
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500324 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600325 if (binaryFileName && (Options & EOptionSpv) == 0)
326 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000327}
328
John Kessenich68d78fd2015-07-12 19:28:10 -0600329//
330// Translate the meaningful subset of command-line options to parser-behavior options.
331//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000332void SetMessageOptions(EShMessages& messages)
333{
334 if (Options & EOptionRelaxedErrors)
335 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
336 if (Options & EOptionIntermediate)
337 messages = (EShMessages)(messages | EShMsgAST);
338 if (Options & EOptionSuppressWarnings)
339 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600340 if (Options & EOptionSpv)
341 messages = (EShMessages)(messages | EShMsgSpvRules);
342 if (Options & EOptionVulkanRules)
343 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400344 if (Options & EOptionOutputPreprocessed)
345 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700346 if (Options & EOptionReadHlsl)
347 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000348}
349
John Kessenich68d78fd2015-07-12 19:28:10 -0600350//
John Kessenich69f4b512013-09-04 21:19:27 +0000351// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000352//
353// Return 0 for failure, 1 for success.
354//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800355unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000356{
John Kessenich38f3b892013-09-06 19:52:57 +0000357 glslang::TWorkItem* workItem;
358 while (Worklist.remove(workItem)) {
359 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000360 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000361 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000362
John Kessenichb0a7eb52013-11-07 17:44:20 +0000363 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000364
John Kessenich94a81fb2013-08-31 02:41:30 +0000365 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000366 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000367
368 ShDestruct(compiler);
369 }
370
371 return 0;
372}
373
John Kessenich6626cad2015-06-19 05:14:19 +0000374// Outputs the given string, but only if it is non-null and non-empty.
375// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400376void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000377{
378 if (str && str[0]) {
379 puts(str);
380 }
381}
382
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400383// Outputs the given string to stderr, but only if it is non-null and non-empty.
384// This prevents erroneous newlines from appearing.
385void StderrIfNonEmpty(const char* str)
386{
387 if (str && str[0]) {
388 fprintf(stderr, "%s\n", str);
389 }
390}
391
John Kessenichc57b2a92016-01-16 15:30:03 -0700392// Simple bundling of what makes a compilation unit for ease in passing around,
393// and separation of handling file IO versus API (programmatic) compilation.
394struct ShaderCompUnit {
395 EShLanguage stage;
396 std::string fileName;
397 char** text; // memory owned/managed externally
398};
399
John Kessenich69f4b512013-09-04 21:19:27 +0000400//
John Kessenichc57b2a92016-01-16 15:30:03 -0700401// For linking mode: Will independently parse each compilation unit, but then put them
402// in the same program and link them together, making at most one linked module per
403// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000404//
405// Uses the new C++ interface instead of the old handle-based interface.
406//
John Kessenichc57b2a92016-01-16 15:30:03 -0700407
408void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000409{
410 // keep track of what to free
411 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700412
John Kessenich69f4b512013-09-04 21:19:27 +0000413 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000414 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000415
John Kessenich69f4b512013-09-04 21:19:27 +0000416 //
417 // Per-shader processing...
418 //
419
John Kessenich5b0f13a2013-11-01 03:08:40 +0000420 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100421 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
422 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700423 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
424 shader->setStrings(compUnit.text, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700425 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
426 shader->setEntryPoint(entryPointName);
John Kessenich69f4b512013-09-04 21:19:27 +0000427 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600428
John Kessenichc555ddd2015-06-17 02:38:44 +0000429 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000430
John Kessenichc555ddd2015-06-17 02:38:44 +0000431 if (Options & EOptionOutputPreprocessed) {
432 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500433 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400434 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500435 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400436 PutsIfNonEmpty(str.c_str());
437 } else {
438 CompileFailed = true;
439 }
440 StderrIfNonEmpty(shader->getInfoLog());
441 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000442 continue;
443 }
444 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000445 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000446
John Kessenich69f4b512013-09-04 21:19:27 +0000447 program.addShader(shader);
448
John Kessenichc57b2a92016-01-16 15:30:03 -0700449 if (! (Options & EOptionSuppressInfolog) &&
450 ! (Options & EOptionMemoryLeakMode)) {
451 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400452 PutsIfNonEmpty(shader->getInfoLog());
453 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000454 }
John Kessenich69f4b512013-09-04 21:19:27 +0000455 }
456
457 //
458 // Program-level processing...
459 //
460
John Kessenich4d65ee32016-03-12 18:17:47 -0700461 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600462 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000463 LinkFailed = true;
464
John Kessenich4d65ee32016-03-12 18:17:47 -0700465 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700466 if (! (Options & EOptionSuppressInfolog) &&
467 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400468 PutsIfNonEmpty(program.getInfoLog());
469 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000470 }
471
John Kessenich4d65ee32016-03-12 18:17:47 -0700472 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000473 if (Options & EOptionDumpReflection) {
474 program.buildReflection();
475 program.dumpReflection();
476 }
477
John Kessenich4d65ee32016-03-12 18:17:47 -0700478 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000479 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000480 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600481 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000482 else {
483 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000484 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000485 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400486 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400487 spv::SpvBuildLogger logger;
488 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700489
490 // Dump the spv to a file or stdout, etc., but only if not doing
491 // memory/perf testing, as it's not internal to programmatic use.
492 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500493 printf("%s", logger.getAllMessages().c_str());
494 if (Options & EOptionOutputHexadecimal) {
495 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
496 } else {
497 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
498 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700499 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700500 spv::Disassemble(std::cout, spirv);
501 }
John Kessenichacba7722015-03-04 03:48:38 +0000502 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000503 }
John Kessenich92f90382014-07-28 04:21:04 +0000504 }
505 }
506 }
507
John Kessenich5b0f13a2013-11-01 03:08:40 +0000508 // Free everything up, program has to go before the shaders
509 // because it might have merged stuff from the shaders, and
510 // the stuff from the shaders has to have its destructors called
511 // before the pools holding the memory in the shaders is freed.
512 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000513 while (shaders.size() > 0) {
514 delete shaders.back();
515 shaders.pop_back();
516 }
John Kessenich69f4b512013-09-04 21:19:27 +0000517}
518
John Kessenichc57b2a92016-01-16 15:30:03 -0700519//
520// Do file IO part of compile and link, handing off the pure
521// API/programmatic mode to CompileAndLinkShaderUnits(), which can
522// be put in a loop for testing memory footprint and performance.
523//
524// This is just for linking mode: meaning all the shaders will be put into the
525// the same program linked together.
526//
527// This means there are a limited number of work items (not multi-threading mode)
528// and that the point is testing at the linking level. Hence, to enable
529// performance and memory testing, the actual compile/link can be put in
530// a loop, independent of processing the work items and file IO.
531//
532void CompileAndLinkShaderFiles()
533{
534 std::vector<ShaderCompUnit> compUnits;
535
536 // Transfer all the work items from to a simple list of
537 // of compilation units. (We don't care about the thread
538 // work-item distribution properties in this path, which
539 // is okay due to the limited number of shaders, know since
540 // they are all getting linked together.)
541 glslang::TWorkItem* workItem;
542 while (Worklist.remove(workItem)) {
543 ShaderCompUnit compUnit = {
544 FindLanguage(workItem->name),
545 workItem->name,
546 ReadFileData(workItem->name.c_str())
547 };
548
549 if (! compUnit.text) {
550 usage();
551 return;
552 }
553
554 compUnits.push_back(compUnit);
555 }
556
557 // Actual call to programmatic processing of compile and link,
558 // in a loop for testing memory and performance. This part contains
559 // all the perf/memory that a programmatic consumer will care about.
560 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
561 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
562 CompileAndLinkShaderUnits(compUnits);
563
564 if (Options & EOptionMemoryLeakMode)
565 glslang::OS_DumpMemoryCounters();
566 }
567
rdb32084e82016-02-23 22:17:38 +0100568 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
569 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700570}
571
John Kessenicha0af4732012-12-12 21:15:54 +0000572int C_DECL main(int argc, char* argv[])
573{
John Kessenich68d78fd2015-07-12 19:28:10 -0600574 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000575
John Kessenich05a70632013-09-17 19:26:08 +0000576 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500577 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000578 if (Worklist.empty())
579 return ESuccess;
580 }
581
John Kessenich0da9eaa2015-08-01 17:10:02 -0600582 if (Options & EOptionDumpVersions) {
583 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000584 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
585 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600586 std::string spirvVersion;
587 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600588 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600589 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700590 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenich319de232013-12-04 04:43:40 +0000591 if (Worklist.empty())
592 return ESuccess;
593 }
594
John Kessenich05a70632013-09-17 19:26:08 +0000595 if (Worklist.empty()) {
596 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000597 }
598
599 ProcessConfigFile();
600
John Kessenich69f4b512013-09-04 21:19:27 +0000601 //
602 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000603 // 1) linking all arguments together, single-threaded, new C++ interface
604 // 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 +0000605 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000606 if (Options & EOptionLinkProgram ||
607 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000608 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700609 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000610 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500611 for (int w = 0; w < NumWorkItems; ++w) {
612 if (Work[w]) {
613 delete Work[w];
614 }
615 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000616 } else {
617 ShInitialize();
618
John Kessenich38f3b892013-09-06 19:52:57 +0000619 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000620
John Kessenich38f3b892013-09-06 19:52:57 +0000621 if (Options & EOptionMultiThreaded) {
622 const int NumThreads = 16;
623 void* threads[NumThreads];
624 for (int t = 0; t < NumThreads; ++t) {
625 threads[t] = glslang::OS_CreateThread(&CompileShaders);
626 if (! threads[t]) {
627 printf("Failed to create thread\n");
628 return EFailThreadCreate;
629 }
John Kessenicha0af4732012-12-12 21:15:54 +0000630 }
John Kessenich38f3b892013-09-06 19:52:57 +0000631 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000632 } else
633 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000634
635 // Print out all the resulting infologs
636 for (int w = 0; w < NumWorkItems; ++w) {
637 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600638 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400639 PutsIfNonEmpty(Work[w]->name.c_str());
640 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000641 delete Work[w];
642 }
643 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000644
645 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000646 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000647
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500648 delete[] Work;
649
John Kessenichc999ba22013-11-07 23:33:24 +0000650 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000651 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000652 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000653 return EFailLink;
654
655 return 0;
656}
657
658//
659// Deduce the language from the filename. Files must end in one of the
660// following extensions:
661//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000662// .vert = vertex
663// .tesc = tessellation control
664// .tese = tessellation evaluation
665// .geom = geometry
666// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000667// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000668//
John Kessenichb603f912013-08-29 00:39:25 +0000669EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000670{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000671 size_t ext = name.rfind('.');
672 if (ext == std::string::npos) {
673 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000674 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000675 }
676
John Kessenich2b07c7e2013-07-31 18:44:13 +0000677 std::string suffix = name.substr(ext + 1, std::string::npos);
678 if (suffix == "vert")
679 return EShLangVertex;
680 else if (suffix == "tesc")
681 return EShLangTessControl;
682 else if (suffix == "tese")
683 return EShLangTessEvaluation;
684 else if (suffix == "geom")
685 return EShLangGeometry;
686 else if (suffix == "frag")
687 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000688 else if (suffix == "comp")
689 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000690
691 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000692 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000693}
694
John Kessenicha0af4732012-12-12 21:15:54 +0000695//
John Kessenich69f4b512013-09-04 21:19:27 +0000696// Read a file's data into a string, and compile it using the old interface ShCompile,
697// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000698//
John Kessenich51cdd902014-02-18 23:37:57 +0000699void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000700{
John Kessenichca3457f2015-05-18 01:59:45 +0000701 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000702 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000703 if (! shaderStrings) {
704 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000705 }
706
John Kessenich41cf6b52013-06-25 18:10:05 +0000707 int* lengths = new int[NumShaderStrings];
708
709 // move to length-based strings, rather than null-terminated strings
710 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000711 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000712
John Kessenichc999ba22013-11-07 23:33:24 +0000713 if (! shaderStrings) {
714 CompileFailed = true;
715 return;
716 }
John Kessenicha0af4732012-12-12 21:15:54 +0000717
John Kessenich52ac67e2013-05-05 23:46:22 +0000718 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000719 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000720
John Kessenich94a81fb2013-08-31 02:41:30 +0000721 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
722 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000723 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000724 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000725 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
726 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
727 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000728 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000729 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000730 }
John Kessenicha0af4732012-12-12 21:15:54 +0000731
John Kessenich94a81fb2013-08-31 02:41:30 +0000732 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000733 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000734 }
John Kessenicha0af4732012-12-12 21:15:54 +0000735
John Kessenich41cf6b52013-06-25 18:10:05 +0000736 delete [] lengths;
737 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000738
John Kessenichc999ba22013-11-07 23:33:24 +0000739 if (ret == 0)
740 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000741}
742
John Kessenicha0af4732012-12-12 21:15:54 +0000743//
744// print usage to stdout
745//
746void usage()
747{
John Kessenich319de232013-12-04 04:43:40 +0000748 printf("Usage: glslangValidator [option]... [file]...\n"
749 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000750 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600751 " .conf to provide an optional config file that replaces the default configuration\n"
752 " (see -c option below for generating a template)\n"
753 " .vert for a vertex shader\n"
754 " .tesc for a tessellation control shader\n"
755 " .tese for a tessellation evaluation shader\n"
756 " .geom for a geometry shader\n"
757 " .frag for a fragment shader\n"
758 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000759 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000760 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000761 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000762 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600763 "Each option must be specified separately.\n"
764 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
765 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600766 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
767 " default file name is <stage>.spv (-o overrides this)\n"
768 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400769 " -E print pre-processed GLSL; cannot be used with -l;\n"
770 " errors will appear on stderr.\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600771 " -c configuration dump;\n"
772 " creates the default configuration file (redirect to a .conf file)\n"
773 " -d default to desktop (#version 110) when there is no shader #version\n"
774 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700775 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700776 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600777 " -h print this usage message\n"
778 " -i intermediate tree (glslang AST) is printed out\n"
779 " -l link all input files together to form a single module\n"
780 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500781 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600782 " -q dump reflection query database\n"
783 " -r relaxed semantic error-checking mode\n"
784 " -s silent mode\n"
785 " -t multi-threaded mode\n"
786 " -v print version strings\n"
787 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500788 " -x <file> save 32-bit hex numbers as text to <file>, requires a binary option (e.g., -V)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000789 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600790
791 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000792}
793
John Kessenich3ce4e592014-10-06 19:57:34 +0000794#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000795
796#include <errno.h>
797
798int fopen_s(
799 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000800 const char* filename,
801 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000802)
803{
804 if (!pFile || !filename || !mode) {
805 return EINVAL;
806 }
807
808 FILE* f = fopen(filename, mode);
809 if (! f) {
810 if (errno != 0) {
811 return errno;
812 } else {
813 return ENOENT;
814 }
815 }
816 *pFile = f;
817
818 return 0;
819}
820
821#endif
822
John Kessenicha0af4732012-12-12 21:15:54 +0000823//
824// Malloc a string of sufficient size and read a string into it.
825//
John Kessenich51cdd902014-02-18 23:37:57 +0000826char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000827{
John Kessenichb3297152015-07-11 18:01:03 -0600828 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +0000829 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000830
John Kessenicha0af4732012-12-12 21:15:54 +0000831 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600832 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
833 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000834
John Kessenich68d78fd2015-07-12 19:28:10 -0600835 if (errorCode || in == nullptr)
836 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000837
838 while (fgetc(in) != EOF)
839 count++;
840
John Kessenichd6c72a42014-08-18 19:42:35 +0000841 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000842
John Kessenichb3297152015-07-11 18:01:03 -0600843 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -0600844 if (! fdata)
845 Error("can't allocate memory");
846
John Kessenichb3297152015-07-11 18:01:03 -0600847 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -0600848 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -0600849 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000850 }
John Kessenich68d78fd2015-07-12 19:28:10 -0600851
John Kessenicha0af4732012-12-12 21:15:54 +0000852 fdata[count] = '\0';
853 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -0600854
John Kessenichea869fb2013-10-28 18:12:06 +0000855 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -0600856 // recover from empty file
857 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000858 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000859 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600860 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +0000861
John Kessenichb3297152015-07-11 18:01:03 -0600862 return return_data;
863 } else
864 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
865
866 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +0000867 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -0600868 int ptr_len = 0;
869 int i = 0;
870 while (count > 0) {
871 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
872 memcpy(return_data[i], fdata + ptr_len, len);
873 return_data[i][len] = '\0';
874 count -= len;
875 ptr_len += len;
876 if (count < len) {
877 if (count == 0) {
878 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000879 break;
880 }
John Kessenichb3297152015-07-11 18:01:03 -0600881 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +0000882 }
883 ++i;
884 }
John Kessenichb3297152015-07-11 18:01:03 -0600885
886 free(fdata);
887
John Kessenicha0af4732012-12-12 21:15:54 +0000888 return return_data;
889}
890
John Kessenich51cdd902014-02-18 23:37:57 +0000891void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +0000892{
John Kessenichb3297152015-07-11 18:01:03 -0600893 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000894 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -0600895
896 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +0000897}
898
John Kessenich54d8cda2013-02-11 22:36:01 +0000899void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000900{
John Kessenichfae38ee2015-06-10 23:23:12 +0000901 if (num >= 0 )
902 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
903 else
904 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +0000905}