blob: 3fd7e7d2adaa3dc10b5ef6c021fddad8b44dabee [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,
John Kessenich66e2faf2016-03-12 18:34:36 -070078 EOptionReadHlsl = 0x10000,
John Kessenich94a81fb2013-08-31 02:41:30 +000079};
80
John Kessenicha0af4732012-12-12 21:15:54 +000081//
John Kessenich68d78fd2015-07-12 19:28:10 -060082// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000083//
84enum TFailCode {
85 ESuccess = 0,
86 EFailUsage,
87 EFailCompile,
88 EFailLink,
89 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000090 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000091 EFailLinkerCreate
92};
93
94//
John Kessenich68d78fd2015-07-12 19:28:10 -060095// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +000096//
John Kessenichb603f912013-08-29 00:39:25 +000097EShLanguage FindLanguage(const std::string& name);
John Kessenich51cdd902014-02-18 23:37:57 +000098void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +000099void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000100void FreeFileData(char** data);
101char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000102void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000103
John Kessenichc999ba22013-11-07 23:33:24 +0000104// Globally track if any compile or link failure.
105bool CompileFailed = false;
106bool LinkFailed = false;
107
John Kessenich05a70632013-09-17 19:26:08 +0000108// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600109// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000110int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000111
John Kessenich05a70632013-09-17 19:26:08 +0000112TBuiltInResource Resources;
113std::string ConfigFile;
114
John Kessenicha0af4732012-12-12 21:15:54 +0000115//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600116// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000117//
118void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000119{
John Kessenich05a70632013-09-17 19:26:08 +0000120 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000121 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000122 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000123 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000124 if (configStrings)
125 config = *configStrings;
126 else {
127 printf("Error opening configuration file; will instead use the default configuration\n");
128 usage();
129 }
130 }
131
132 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500133 Resources = glslang::DefaultTBuiltInResource;
134 return;
John Kessenich05a70632013-09-17 19:26:08 +0000135 }
136
Lei Zhang1b141722016-05-19 13:50:49 -0400137 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000138
John Kessenich05a70632013-09-17 19:26:08 +0000139 if (configStrings)
140 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500141 else
142 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000143}
144
John Kessenich38f3b892013-09-06 19:52:57 +0000145// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000146glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000147
148// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000149glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000150int NumWorkItems = 0;
151
John Kessenich94a81fb2013-08-31 02:41:30 +0000152int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600153const char* ExecutableName = nullptr;
154const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700155const char* entryPointName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600156
157//
158// Create the default name for saving a binary if -o is not provided.
159//
160const char* GetBinaryName(EShLanguage stage)
161{
162 const char* name;
163 if (binaryFileName == nullptr) {
164 switch (stage) {
165 case EShLangVertex: name = "vert.spv"; break;
166 case EShLangTessControl: name = "tesc.spv"; break;
167 case EShLangTessEvaluation: name = "tese.spv"; break;
168 case EShLangGeometry: name = "geom.spv"; break;
169 case EShLangFragment: name = "frag.spv"; break;
170 case EShLangCompute: name = "comp.spv"; break;
171 default: name = "unknown"; break;
172 }
173 } else
174 name = binaryFileName;
175
176 return name;
177}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000178
John Kessenich05a70632013-09-17 19:26:08 +0000179//
180// *.conf => this is a config file that can set limits/resources
181//
182bool SetConfigFile(const std::string& name)
183{
184 if (name.size() < 5)
185 return false;
186
John Kessenich4c706852013-10-11 16:28:43 +0000187 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000188 ConfigFile = name;
189 return true;
190 }
191
192 return false;
193}
194
John Kessenich68d78fd2015-07-12 19:28:10 -0600195//
196// Give error and exit with failure code.
197//
198void Error(const char* message)
199{
200 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
201 exit(EFailUsage);
202}
203
204//
205// Do all command-line argument parsing. This includes building up the work-items
206// to be processed later, and saving all the command-line options.
207//
208// Does not return (it exits) if command-line is fatally flawed.
209//
210void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000211{
John Kessenich38f3b892013-09-06 19:52:57 +0000212 ExecutableName = argv[0];
213 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 +0000214 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600215 for (int w = 0; w < NumWorkItems; ++w)
216 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000217
John Kessenich2b07c7e2013-07-31 18:44:13 +0000218 argc--;
219 argv++;
220 for (; argc >= 1; argc--, argv++) {
221 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000222 switch (argv[0][1]) {
223 case 'H':
224 Options |= EOptionHumanReadableSpv;
225 // fall through to -V
John Kessenich0df0cde2015-03-03 17:09:43 +0000226 case 'V':
227 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600228 Options |= EOptionVulkanRules;
229 Options |= EOptionLinkProgram;
230 break;
231 case 'G':
232 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000233 Options |= EOptionLinkProgram;
John Kessenich92f90382014-07-28 04:21:04 +0000234 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000235 case 'E':
236 Options |= EOptionOutputPreprocessed;
237 break;
John Kessenich05a70632013-09-17 19:26:08 +0000238 case 'c':
239 Options |= EOptionDumpConfig;
240 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000241 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000242 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000243 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700244 case 'D':
245 Options |= EOptionReadHlsl;
246 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700247 case 'e':
248 // HLSL todo: entry point handle needs much more sophistication.
249 // This is okay for one compilation unit with one entry point.
250 entryPointName = argv[1];
251 if (argc > 0) {
252 argc--;
253 argv++;
254 } else
255 Error("no <entry-point> provided for -e");
256 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600257 case 'h':
258 usage();
259 break;
John Kessenich05a70632013-09-17 19:26:08 +0000260 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000261 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000262 break;
263 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000264 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000265 break;
266 case 'm':
267 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000268 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600269 case 'o':
270 binaryFileName = argv[1];
271 if (argc > 0) {
272 argc--;
273 argv++;
274 } else
275 Error("no <file> provided for -o");
276 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000277 case 'q':
278 Options |= EOptionDumpReflection;
279 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000280 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000281 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000282 break;
283 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000284 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000285 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000286 case 't':
287 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000288 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000289 #endif
290 break;
John Kessenich319de232013-12-04 04:43:40 +0000291 case 'v':
292 Options |= EOptionDumpVersions;
293 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000294 case 'w':
295 Options |= EOptionSuppressWarnings;
296 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000297 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600298 usage();
299 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000300 }
John Kessenich38f3b892013-09-06 19:52:57 +0000301 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000302 std::string name(argv[0]);
303 if (! SetConfigFile(name)) {
304 Work[argc] = new glslang::TWorkItem(name);
305 Worklist.add(Work[argc]);
306 }
John Kessenich38f3b892013-09-06 19:52:57 +0000307 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000308 }
309
John Kessenich68d78fd2015-07-12 19:28:10 -0600310 // Make sure that -E is not specified alongside linking (which includes SPV generation)
311 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
312 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000313
John Kessenich68d78fd2015-07-12 19:28:10 -0600314 // -o makes no sense if there is no target binary
315 if (binaryFileName && (Options & EOptionSpv) == 0)
316 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000317}
318
John Kessenich68d78fd2015-07-12 19:28:10 -0600319//
320// Translate the meaningful subset of command-line options to parser-behavior options.
321//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000322void SetMessageOptions(EShMessages& messages)
323{
324 if (Options & EOptionRelaxedErrors)
325 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
326 if (Options & EOptionIntermediate)
327 messages = (EShMessages)(messages | EShMsgAST);
328 if (Options & EOptionSuppressWarnings)
329 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600330 if (Options & EOptionSpv)
331 messages = (EShMessages)(messages | EShMsgSpvRules);
332 if (Options & EOptionVulkanRules)
333 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400334 if (Options & EOptionOutputPreprocessed)
335 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700336 if (Options & EOptionReadHlsl)
337 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000338}
339
John Kessenich68d78fd2015-07-12 19:28:10 -0600340//
John Kessenich69f4b512013-09-04 21:19:27 +0000341// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000342//
343// Return 0 for failure, 1 for success.
344//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800345unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000346{
John Kessenich38f3b892013-09-06 19:52:57 +0000347 glslang::TWorkItem* workItem;
348 while (Worklist.remove(workItem)) {
349 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000350 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000351 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000352
John Kessenichb0a7eb52013-11-07 17:44:20 +0000353 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000354
John Kessenich94a81fb2013-08-31 02:41:30 +0000355 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000356 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000357
358 ShDestruct(compiler);
359 }
360
361 return 0;
362}
363
John Kessenich6626cad2015-06-19 05:14:19 +0000364// Outputs the given string, but only if it is non-null and non-empty.
365// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400366void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000367{
368 if (str && str[0]) {
369 puts(str);
370 }
371}
372
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400373// Outputs the given string to stderr, but only if it is non-null and non-empty.
374// This prevents erroneous newlines from appearing.
375void StderrIfNonEmpty(const char* str)
376{
377 if (str && str[0]) {
378 fprintf(stderr, "%s\n", str);
379 }
380}
381
John Kessenichc57b2a92016-01-16 15:30:03 -0700382// Simple bundling of what makes a compilation unit for ease in passing around,
383// and separation of handling file IO versus API (programmatic) compilation.
384struct ShaderCompUnit {
385 EShLanguage stage;
386 std::string fileName;
387 char** text; // memory owned/managed externally
388};
389
John Kessenich69f4b512013-09-04 21:19:27 +0000390//
John Kessenichc57b2a92016-01-16 15:30:03 -0700391// For linking mode: Will independently parse each compilation unit, but then put them
392// in the same program and link them together, making at most one linked module per
393// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000394//
395// Uses the new C++ interface instead of the old handle-based interface.
396//
John Kessenichc57b2a92016-01-16 15:30:03 -0700397
398void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000399{
400 // keep track of what to free
401 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700402
John Kessenich69f4b512013-09-04 21:19:27 +0000403 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000404 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000405
John Kessenich69f4b512013-09-04 21:19:27 +0000406 //
407 // Per-shader processing...
408 //
409
John Kessenich5b0f13a2013-11-01 03:08:40 +0000410 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100411 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
412 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700413 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
414 shader->setStrings(compUnit.text, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700415 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
416 shader->setEntryPoint(entryPointName);
John Kessenich69f4b512013-09-04 21:19:27 +0000417 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600418
John Kessenichc555ddd2015-06-17 02:38:44 +0000419 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000420
John Kessenichc555ddd2015-06-17 02:38:44 +0000421 if (Options & EOptionOutputPreprocessed) {
422 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500423 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400424 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500425 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400426 PutsIfNonEmpty(str.c_str());
427 } else {
428 CompileFailed = true;
429 }
430 StderrIfNonEmpty(shader->getInfoLog());
431 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000432 continue;
433 }
434 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000435 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000436
John Kessenich69f4b512013-09-04 21:19:27 +0000437 program.addShader(shader);
438
John Kessenichc57b2a92016-01-16 15:30:03 -0700439 if (! (Options & EOptionSuppressInfolog) &&
440 ! (Options & EOptionMemoryLeakMode)) {
441 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400442 PutsIfNonEmpty(shader->getInfoLog());
443 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000444 }
John Kessenich69f4b512013-09-04 21:19:27 +0000445 }
446
447 //
448 // Program-level processing...
449 //
450
John Kessenich4d65ee32016-03-12 18:17:47 -0700451 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600452 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000453 LinkFailed = true;
454
John Kessenich4d65ee32016-03-12 18:17:47 -0700455 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700456 if (! (Options & EOptionSuppressInfolog) &&
457 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400458 PutsIfNonEmpty(program.getInfoLog());
459 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000460 }
461
John Kessenich4d65ee32016-03-12 18:17:47 -0700462 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000463 if (Options & EOptionDumpReflection) {
464 program.buildReflection();
465 program.dumpReflection();
466 }
467
John Kessenich4d65ee32016-03-12 18:17:47 -0700468 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000469 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000470 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600471 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000472 else {
473 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000474 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000475 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400476 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400477 spv::SpvBuildLogger logger;
478 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700479
480 // Dump the spv to a file or stdout, etc., but only if not doing
481 // memory/perf testing, as it's not internal to programmatic use.
482 if (! (Options & EOptionMemoryLeakMode)) {
Lei Zhang17535f72016-05-04 15:55:59 -0400483 printf("%s", logger.getAllMessages().c_str());
John Kessenichc57b2a92016-01-16 15:30:03 -0700484 glslang::OutputSpv(spirv, GetBinaryName((EShLanguage)stage));
485 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700486 spv::Disassemble(std::cout, spirv);
487 }
John Kessenichacba7722015-03-04 03:48:38 +0000488 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000489 }
John Kessenich92f90382014-07-28 04:21:04 +0000490 }
491 }
492 }
493
John Kessenich5b0f13a2013-11-01 03:08:40 +0000494 // Free everything up, program has to go before the shaders
495 // because it might have merged stuff from the shaders, and
496 // the stuff from the shaders has to have its destructors called
497 // before the pools holding the memory in the shaders is freed.
498 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000499 while (shaders.size() > 0) {
500 delete shaders.back();
501 shaders.pop_back();
502 }
John Kessenich69f4b512013-09-04 21:19:27 +0000503}
504
John Kessenichc57b2a92016-01-16 15:30:03 -0700505//
506// Do file IO part of compile and link, handing off the pure
507// API/programmatic mode to CompileAndLinkShaderUnits(), which can
508// be put in a loop for testing memory footprint and performance.
509//
510// This is just for linking mode: meaning all the shaders will be put into the
511// the same program linked together.
512//
513// This means there are a limited number of work items (not multi-threading mode)
514// and that the point is testing at the linking level. Hence, to enable
515// performance and memory testing, the actual compile/link can be put in
516// a loop, independent of processing the work items and file IO.
517//
518void CompileAndLinkShaderFiles()
519{
520 std::vector<ShaderCompUnit> compUnits;
521
522 // Transfer all the work items from to a simple list of
523 // of compilation units. (We don't care about the thread
524 // work-item distribution properties in this path, which
525 // is okay due to the limited number of shaders, know since
526 // they are all getting linked together.)
527 glslang::TWorkItem* workItem;
528 while (Worklist.remove(workItem)) {
529 ShaderCompUnit compUnit = {
530 FindLanguage(workItem->name),
531 workItem->name,
532 ReadFileData(workItem->name.c_str())
533 };
534
535 if (! compUnit.text) {
536 usage();
537 return;
538 }
539
540 compUnits.push_back(compUnit);
541 }
542
543 // Actual call to programmatic processing of compile and link,
544 // in a loop for testing memory and performance. This part contains
545 // all the perf/memory that a programmatic consumer will care about.
546 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
547 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
548 CompileAndLinkShaderUnits(compUnits);
549
550 if (Options & EOptionMemoryLeakMode)
551 glslang::OS_DumpMemoryCounters();
552 }
553
rdb32084e82016-02-23 22:17:38 +0100554 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
555 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700556}
557
John Kessenicha0af4732012-12-12 21:15:54 +0000558int C_DECL main(int argc, char* argv[])
559{
John Kessenich68d78fd2015-07-12 19:28:10 -0600560 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000561
John Kessenich05a70632013-09-17 19:26:08 +0000562 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500563 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000564 if (Worklist.empty())
565 return ESuccess;
566 }
567
John Kessenich0da9eaa2015-08-01 17:10:02 -0600568 if (Options & EOptionDumpVersions) {
569 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000570 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
571 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600572 std::string spirvVersion;
573 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600574 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600575 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700576 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenich319de232013-12-04 04:43:40 +0000577 if (Worklist.empty())
578 return ESuccess;
579 }
580
John Kessenich05a70632013-09-17 19:26:08 +0000581 if (Worklist.empty()) {
582 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000583 }
584
585 ProcessConfigFile();
586
John Kessenich69f4b512013-09-04 21:19:27 +0000587 //
588 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000589 // 1) linking all arguments together, single-threaded, new C++ interface
590 // 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 +0000591 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000592 if (Options & EOptionLinkProgram ||
593 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000594 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700595 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000596 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500597 for (int w = 0; w < NumWorkItems; ++w) {
598 if (Work[w]) {
599 delete Work[w];
600 }
601 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000602 } else {
603 ShInitialize();
604
John Kessenich38f3b892013-09-06 19:52:57 +0000605 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000606
John Kessenich38f3b892013-09-06 19:52:57 +0000607 if (Options & EOptionMultiThreaded) {
608 const int NumThreads = 16;
609 void* threads[NumThreads];
610 for (int t = 0; t < NumThreads; ++t) {
611 threads[t] = glslang::OS_CreateThread(&CompileShaders);
612 if (! threads[t]) {
613 printf("Failed to create thread\n");
614 return EFailThreadCreate;
615 }
John Kessenicha0af4732012-12-12 21:15:54 +0000616 }
John Kessenich38f3b892013-09-06 19:52:57 +0000617 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000618 } else
619 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000620
621 // Print out all the resulting infologs
622 for (int w = 0; w < NumWorkItems; ++w) {
623 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600624 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400625 PutsIfNonEmpty(Work[w]->name.c_str());
626 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000627 delete Work[w];
628 }
629 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000630
631 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000632 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000633
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500634 delete[] Work;
635
John Kessenichc999ba22013-11-07 23:33:24 +0000636 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000637 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000638 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000639 return EFailLink;
640
641 return 0;
642}
643
644//
645// Deduce the language from the filename. Files must end in one of the
646// following extensions:
647//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000648// .vert = vertex
649// .tesc = tessellation control
650// .tese = tessellation evaluation
651// .geom = geometry
652// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000653// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000654//
John Kessenichb603f912013-08-29 00:39:25 +0000655EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000656{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000657 size_t ext = name.rfind('.');
658 if (ext == std::string::npos) {
659 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000660 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000661 }
662
John Kessenich2b07c7e2013-07-31 18:44:13 +0000663 std::string suffix = name.substr(ext + 1, std::string::npos);
664 if (suffix == "vert")
665 return EShLangVertex;
666 else if (suffix == "tesc")
667 return EShLangTessControl;
668 else if (suffix == "tese")
669 return EShLangTessEvaluation;
670 else if (suffix == "geom")
671 return EShLangGeometry;
672 else if (suffix == "frag")
673 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000674 else if (suffix == "comp")
675 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000676
677 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000678 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000679}
680
John Kessenicha0af4732012-12-12 21:15:54 +0000681//
John Kessenich69f4b512013-09-04 21:19:27 +0000682// Read a file's data into a string, and compile it using the old interface ShCompile,
683// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000684//
John Kessenich51cdd902014-02-18 23:37:57 +0000685void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000686{
John Kessenichca3457f2015-05-18 01:59:45 +0000687 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000688 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000689 if (! shaderStrings) {
690 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000691 }
692
John Kessenich41cf6b52013-06-25 18:10:05 +0000693 int* lengths = new int[NumShaderStrings];
694
695 // move to length-based strings, rather than null-terminated strings
696 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000697 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000698
John Kessenichc999ba22013-11-07 23:33:24 +0000699 if (! shaderStrings) {
700 CompileFailed = true;
701 return;
702 }
John Kessenicha0af4732012-12-12 21:15:54 +0000703
John Kessenich52ac67e2013-05-05 23:46:22 +0000704 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000705 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000706
John Kessenich94a81fb2013-08-31 02:41:30 +0000707 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
708 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000709 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000710 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000711 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
712 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
713 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000714 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000715 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000716 }
John Kessenicha0af4732012-12-12 21:15:54 +0000717
John Kessenich94a81fb2013-08-31 02:41:30 +0000718 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000719 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000720 }
John Kessenicha0af4732012-12-12 21:15:54 +0000721
John Kessenich41cf6b52013-06-25 18:10:05 +0000722 delete [] lengths;
723 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000724
John Kessenichc999ba22013-11-07 23:33:24 +0000725 if (ret == 0)
726 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000727}
728
John Kessenicha0af4732012-12-12 21:15:54 +0000729//
730// print usage to stdout
731//
732void usage()
733{
John Kessenich319de232013-12-04 04:43:40 +0000734 printf("Usage: glslangValidator [option]... [file]...\n"
735 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000736 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600737 " .conf to provide an optional config file that replaces the default configuration\n"
738 " (see -c option below for generating a template)\n"
739 " .vert for a vertex shader\n"
740 " .tesc for a tessellation control shader\n"
741 " .tese for a tessellation evaluation shader\n"
742 " .geom for a geometry shader\n"
743 " .frag for a fragment shader\n"
744 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000745 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000746 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000747 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000748 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600749 "Each option must be specified separately.\n"
750 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
751 " default file name is <stage>.spv (-o overrides this)\n"
752 " (unless -o is specified, which overrides the default file name)\n"
753 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
754 " default file name is <stage>.spv (-o overrides this)\n"
755 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400756 " -E print pre-processed GLSL; cannot be used with -l;\n"
757 " errors will appear on stderr.\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600758 " -c configuration dump;\n"
759 " creates the default configuration file (redirect to a .conf file)\n"
760 " -d default to desktop (#version 110) when there is no shader #version\n"
761 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700762 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700763 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600764 " -h print this usage message\n"
765 " -i intermediate tree (glslang AST) is printed out\n"
766 " -l link all input files together to form a single module\n"
767 " -m memory leak mode\n"
768 " -o <file> save binary into <file>, requires a binary option (e.g., -V)\n"
769 " -q dump reflection query database\n"
770 " -r relaxed semantic error-checking mode\n"
771 " -s silent mode\n"
772 " -t multi-threaded mode\n"
773 " -v print version strings\n"
774 " -w suppress warnings (except as required by #extension : warn)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000775 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600776
777 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000778}
779
John Kessenich3ce4e592014-10-06 19:57:34 +0000780#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000781
782#include <errno.h>
783
784int fopen_s(
785 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000786 const char* filename,
787 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000788)
789{
790 if (!pFile || !filename || !mode) {
791 return EINVAL;
792 }
793
794 FILE* f = fopen(filename, mode);
795 if (! f) {
796 if (errno != 0) {
797 return errno;
798 } else {
799 return ENOENT;
800 }
801 }
802 *pFile = f;
803
804 return 0;
805}
806
807#endif
808
John Kessenicha0af4732012-12-12 21:15:54 +0000809//
810// Malloc a string of sufficient size and read a string into it.
811//
John Kessenich51cdd902014-02-18 23:37:57 +0000812char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000813{
John Kessenichb3297152015-07-11 18:01:03 -0600814 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +0000815 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000816
John Kessenicha0af4732012-12-12 21:15:54 +0000817 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600818 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
819 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000820
John Kessenich68d78fd2015-07-12 19:28:10 -0600821 if (errorCode || in == nullptr)
822 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000823
824 while (fgetc(in) != EOF)
825 count++;
826
John Kessenichd6c72a42014-08-18 19:42:35 +0000827 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000828
John Kessenichb3297152015-07-11 18:01:03 -0600829 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -0600830 if (! fdata)
831 Error("can't allocate memory");
832
John Kessenichb3297152015-07-11 18:01:03 -0600833 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -0600834 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -0600835 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000836 }
John Kessenich68d78fd2015-07-12 19:28:10 -0600837
John Kessenicha0af4732012-12-12 21:15:54 +0000838 fdata[count] = '\0';
839 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -0600840
John Kessenichea869fb2013-10-28 18:12:06 +0000841 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -0600842 // recover from empty file
843 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000844 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000845 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600846 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +0000847
John Kessenichb3297152015-07-11 18:01:03 -0600848 return return_data;
849 } else
850 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
851
852 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +0000853 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -0600854 int ptr_len = 0;
855 int i = 0;
856 while (count > 0) {
857 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
858 memcpy(return_data[i], fdata + ptr_len, len);
859 return_data[i][len] = '\0';
860 count -= len;
861 ptr_len += len;
862 if (count < len) {
863 if (count == 0) {
864 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000865 break;
866 }
John Kessenichb3297152015-07-11 18:01:03 -0600867 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +0000868 }
869 ++i;
870 }
John Kessenichb3297152015-07-11 18:01:03 -0600871
872 free(fdata);
873
John Kessenicha0af4732012-12-12 21:15:54 +0000874 return return_data;
875}
876
John Kessenich51cdd902014-02-18 23:37:57 +0000877void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +0000878{
John Kessenichb3297152015-07-11 18:01:03 -0600879 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000880 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -0600881
882 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +0000883}
884
John Kessenich54d8cda2013-02-11 22:36:01 +0000885void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000886{
John Kessenichfae38ee2015-06-10 23:23:12 +0000887 if (num >= 0 )
888 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
889 else
890 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +0000891}