blob: 0e4e73827bdbd18d59fcb4136496181ce2c28ad8 [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;
John Kessenich91e4aa52016-07-07 17:46:42 -0600225 if ((Options & EOptionSpv) == 0) {
226 // default to Vulkan
227 Options |= EOptionSpv;
228 Options |= EOptionVulkanRules;
229 Options |= EOptionLinkProgram;
230 }
231 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000232 case 'V':
233 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600234 Options |= EOptionVulkanRules;
235 Options |= EOptionLinkProgram;
236 break;
237 case 'G':
238 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000239 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600240 // undo a -H default to Vulkan
241 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000242 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000243 case 'E':
244 Options |= EOptionOutputPreprocessed;
245 break;
John Kessenich05a70632013-09-17 19:26:08 +0000246 case 'c':
247 Options |= EOptionDumpConfig;
248 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000249 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000250 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000251 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700252 case 'D':
253 Options |= EOptionReadHlsl;
254 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700255 case 'e':
256 // HLSL todo: entry point handle needs much more sophistication.
257 // This is okay for one compilation unit with one entry point.
258 entryPointName = argv[1];
259 if (argc > 0) {
260 argc--;
261 argv++;
262 } else
263 Error("no <entry-point> provided for -e");
264 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600265 case 'h':
266 usage();
267 break;
John Kessenich05a70632013-09-17 19:26:08 +0000268 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000269 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000270 break;
271 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000272 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000273 break;
274 case 'm':
275 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000276 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600277 case 'o':
278 binaryFileName = argv[1];
279 if (argc > 0) {
280 argc--;
281 argv++;
282 } else
283 Error("no <file> provided for -o");
284 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000285 case 'q':
286 Options |= EOptionDumpReflection;
287 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000288 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000289 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000290 break;
291 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000292 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000293 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000294 case 't':
295 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000296 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000297 #endif
298 break;
John Kessenich319de232013-12-04 04:43:40 +0000299 case 'v':
300 Options |= EOptionDumpVersions;
301 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000302 case 'w':
303 Options |= EOptionSuppressWarnings;
304 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000305 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600306 usage();
307 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000308 }
John Kessenich38f3b892013-09-06 19:52:57 +0000309 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000310 std::string name(argv[0]);
311 if (! SetConfigFile(name)) {
312 Work[argc] = new glslang::TWorkItem(name);
313 Worklist.add(Work[argc]);
314 }
John Kessenich38f3b892013-09-06 19:52:57 +0000315 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000316 }
317
John Kessenich68d78fd2015-07-12 19:28:10 -0600318 // Make sure that -E is not specified alongside linking (which includes SPV generation)
319 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
320 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000321
John Kessenich68d78fd2015-07-12 19:28:10 -0600322 // -o makes no sense if there is no target binary
323 if (binaryFileName && (Options & EOptionSpv) == 0)
324 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000325}
326
John Kessenich68d78fd2015-07-12 19:28:10 -0600327//
328// Translate the meaningful subset of command-line options to parser-behavior options.
329//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000330void SetMessageOptions(EShMessages& messages)
331{
332 if (Options & EOptionRelaxedErrors)
333 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
334 if (Options & EOptionIntermediate)
335 messages = (EShMessages)(messages | EShMsgAST);
336 if (Options & EOptionSuppressWarnings)
337 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600338 if (Options & EOptionSpv)
339 messages = (EShMessages)(messages | EShMsgSpvRules);
340 if (Options & EOptionVulkanRules)
341 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400342 if (Options & EOptionOutputPreprocessed)
343 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700344 if (Options & EOptionReadHlsl)
345 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000346}
347
John Kessenich68d78fd2015-07-12 19:28:10 -0600348//
John Kessenich69f4b512013-09-04 21:19:27 +0000349// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000350//
351// Return 0 for failure, 1 for success.
352//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800353unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000354{
John Kessenich38f3b892013-09-06 19:52:57 +0000355 glslang::TWorkItem* workItem;
356 while (Worklist.remove(workItem)) {
357 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000358 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000359 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000360
John Kessenichb0a7eb52013-11-07 17:44:20 +0000361 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000362
John Kessenich94a81fb2013-08-31 02:41:30 +0000363 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000364 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000365
366 ShDestruct(compiler);
367 }
368
369 return 0;
370}
371
John Kessenich6626cad2015-06-19 05:14:19 +0000372// Outputs the given string, but only if it is non-null and non-empty.
373// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400374void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000375{
376 if (str && str[0]) {
377 puts(str);
378 }
379}
380
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400381// Outputs the given string to stderr, but only if it is non-null and non-empty.
382// This prevents erroneous newlines from appearing.
383void StderrIfNonEmpty(const char* str)
384{
385 if (str && str[0]) {
386 fprintf(stderr, "%s\n", str);
387 }
388}
389
John Kessenichc57b2a92016-01-16 15:30:03 -0700390// Simple bundling of what makes a compilation unit for ease in passing around,
391// and separation of handling file IO versus API (programmatic) compilation.
392struct ShaderCompUnit {
393 EShLanguage stage;
394 std::string fileName;
395 char** text; // memory owned/managed externally
396};
397
John Kessenich69f4b512013-09-04 21:19:27 +0000398//
John Kessenichc57b2a92016-01-16 15:30:03 -0700399// For linking mode: Will independently parse each compilation unit, but then put them
400// in the same program and link them together, making at most one linked module per
401// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000402//
403// Uses the new C++ interface instead of the old handle-based interface.
404//
John Kessenichc57b2a92016-01-16 15:30:03 -0700405
406void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000407{
408 // keep track of what to free
409 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700410
John Kessenich69f4b512013-09-04 21:19:27 +0000411 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000412 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000413
John Kessenich69f4b512013-09-04 21:19:27 +0000414 //
415 // Per-shader processing...
416 //
417
John Kessenich5b0f13a2013-11-01 03:08:40 +0000418 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100419 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
420 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700421 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
422 shader->setStrings(compUnit.text, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700423 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
424 shader->setEntryPoint(entryPointName);
John Kessenich69f4b512013-09-04 21:19:27 +0000425 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600426
John Kessenichc555ddd2015-06-17 02:38:44 +0000427 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000428
John Kessenichc555ddd2015-06-17 02:38:44 +0000429 if (Options & EOptionOutputPreprocessed) {
430 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500431 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400432 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500433 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400434 PutsIfNonEmpty(str.c_str());
435 } else {
436 CompileFailed = true;
437 }
438 StderrIfNonEmpty(shader->getInfoLog());
439 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000440 continue;
441 }
442 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000443 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000444
John Kessenich69f4b512013-09-04 21:19:27 +0000445 program.addShader(shader);
446
John Kessenichc57b2a92016-01-16 15:30:03 -0700447 if (! (Options & EOptionSuppressInfolog) &&
448 ! (Options & EOptionMemoryLeakMode)) {
449 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400450 PutsIfNonEmpty(shader->getInfoLog());
451 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000452 }
John Kessenich69f4b512013-09-04 21:19:27 +0000453 }
454
455 //
456 // Program-level processing...
457 //
458
John Kessenich4d65ee32016-03-12 18:17:47 -0700459 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600460 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000461 LinkFailed = true;
462
John Kessenich4d65ee32016-03-12 18:17:47 -0700463 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700464 if (! (Options & EOptionSuppressInfolog) &&
465 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400466 PutsIfNonEmpty(program.getInfoLog());
467 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000468 }
469
John Kessenich4d65ee32016-03-12 18:17:47 -0700470 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000471 if (Options & EOptionDumpReflection) {
472 program.buildReflection();
473 program.dumpReflection();
474 }
475
John Kessenich4d65ee32016-03-12 18:17:47 -0700476 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000477 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000478 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600479 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000480 else {
481 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000482 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000483 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400484 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400485 spv::SpvBuildLogger logger;
486 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700487
488 // Dump the spv to a file or stdout, etc., but only if not doing
489 // memory/perf testing, as it's not internal to programmatic use.
490 if (! (Options & EOptionMemoryLeakMode)) {
Lei Zhang17535f72016-05-04 15:55:59 -0400491 printf("%s", logger.getAllMessages().c_str());
John Kessenichc57b2a92016-01-16 15:30:03 -0700492 glslang::OutputSpv(spirv, GetBinaryName((EShLanguage)stage));
493 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"
760 " (unless -o is specified, which overrides the default file name)\n"
761 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
762 " default file name is <stage>.spv (-o overrides this)\n"
763 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400764 " -E print pre-processed GLSL; cannot be used with -l;\n"
765 " errors will appear on stderr.\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600766 " -c configuration dump;\n"
767 " creates the default configuration file (redirect to a .conf file)\n"
768 " -d default to desktop (#version 110) when there is no shader #version\n"
769 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700770 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700771 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600772 " -h print this usage message\n"
773 " -i intermediate tree (glslang AST) is printed out\n"
774 " -l link all input files together to form a single module\n"
775 " -m memory leak mode\n"
776 " -o <file> save binary into <file>, requires a binary option (e.g., -V)\n"
777 " -q dump reflection query database\n"
778 " -r relaxed semantic error-checking mode\n"
779 " -s silent mode\n"
780 " -t multi-threaded mode\n"
781 " -v print version strings\n"
782 " -w suppress warnings (except as required by #extension : warn)\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}