blob: f560847af105b7564b1f253eb35b8aa1e099ab39 [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 Kessenicha86836e2016-07-09 14:50:57 -060061 EOptionNone = 0,
62 EOptionIntermediate = (1 << 0),
63 EOptionSuppressInfolog = (1 << 1),
64 EOptionMemoryLeakMode = (1 << 2),
65 EOptionRelaxedErrors = (1 << 3),
66 EOptionGiveWarnings = (1 << 4),
67 EOptionLinkProgram = (1 << 5),
68 EOptionMultiThreaded = (1 << 6),
69 EOptionDumpConfig = (1 << 7),
70 EOptionDumpReflection = (1 << 8),
71 EOptionSuppressWarnings = (1 << 9),
72 EOptionDumpVersions = (1 << 10),
73 EOptionSpv = (1 << 11),
74 EOptionHumanReadableSpv = (1 << 12),
75 EOptionVulkanRules = (1 << 13),
76 EOptionDefaultDesktop = (1 << 14),
77 EOptionOutputPreprocessed = (1 << 15),
78 EOptionOutputHexadecimal = (1 << 16),
79 EOptionReadHlsl = (1 << 17),
80 EOptionCascadingErrors = (1 << 18),
John Kessenich94a81fb2013-08-31 02:41:30 +000081};
82
John Kessenicha0af4732012-12-12 21:15:54 +000083//
John Kessenich68d78fd2015-07-12 19:28:10 -060084// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +000085//
86enum TFailCode {
87 ESuccess = 0,
88 EFailUsage,
89 EFailCompile,
90 EFailLink,
91 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +000092 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +000093 EFailLinkerCreate
94};
95
96//
John Kessenich68d78fd2015-07-12 19:28:10 -060097// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +000098//
John Kessenichb603f912013-08-29 00:39:25 +000099EShLanguage FindLanguage(const std::string& name);
John Kessenich51cdd902014-02-18 23:37:57 +0000100void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000101void usage();
John Kessenichea869fb2013-10-28 18:12:06 +0000102void FreeFileData(char** data);
103char** ReadFileData(const char* fileName);
John Kessenich54d8cda2013-02-11 22:36:01 +0000104void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000105
John Kessenichc999ba22013-11-07 23:33:24 +0000106// Globally track if any compile or link failure.
107bool CompileFailed = false;
108bool LinkFailed = false;
109
John Kessenich05a70632013-09-17 19:26:08 +0000110// Use to test breaking up a single shader file into multiple strings.
John Kessenich68d78fd2015-07-12 19:28:10 -0600111// Set in ReadFileData().
John Kessenichea869fb2013-10-28 18:12:06 +0000112int NumShaderStrings;
John Kessenicha0af4732012-12-12 21:15:54 +0000113
John Kessenich05a70632013-09-17 19:26:08 +0000114TBuiltInResource Resources;
115std::string ConfigFile;
116
John Kessenicha0af4732012-12-12 21:15:54 +0000117//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600118// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000119//
120void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000121{
John Kessenich05a70632013-09-17 19:26:08 +0000122 char** configStrings = 0;
John Kessenich51cdd902014-02-18 23:37:57 +0000123 char* config = 0;
John Kessenich05a70632013-09-17 19:26:08 +0000124 if (ConfigFile.size() > 0) {
John Kessenichea869fb2013-10-28 18:12:06 +0000125 configStrings = ReadFileData(ConfigFile.c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000126 if (configStrings)
127 config = *configStrings;
128 else {
129 printf("Error opening configuration file; will instead use the default configuration\n");
130 usage();
131 }
132 }
133
134 if (config == 0) {
Lei Zhang414eb602016-03-04 16:22:34 -0500135 Resources = glslang::DefaultTBuiltInResource;
136 return;
John Kessenich05a70632013-09-17 19:26:08 +0000137 }
138
Lei Zhang1b141722016-05-19 13:50:49 -0400139 glslang::DecodeResourceLimits(&Resources, config);
John Kessenich05a70632013-09-17 19:26:08 +0000140
John Kessenich05a70632013-09-17 19:26:08 +0000141 if (configStrings)
142 FreeFileData(configStrings);
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500143 else
144 delete[] config;
John Kessenicha0af4732012-12-12 21:15:54 +0000145}
146
John Kessenich38f3b892013-09-06 19:52:57 +0000147// thread-safe list of shaders to asynchronously grab and compile
John Kessenich2b07c7e2013-07-31 18:44:13 +0000148glslang::TWorklist Worklist;
John Kessenich38f3b892013-09-06 19:52:57 +0000149
150// array of unique places to leave the shader names and infologs for the asynchronous compiles
John Kessenichfd305422014-06-05 16:30:53 +0000151glslang::TWorkItem** Work = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000152int NumWorkItems = 0;
153
John Kessenich94a81fb2013-08-31 02:41:30 +0000154int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600155const char* ExecutableName = nullptr;
156const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700157const char* entryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400158const char* shaderStageName = nullptr;
John Kessenich68d78fd2015-07-12 19:28:10 -0600159
160//
161// Create the default name for saving a binary if -o is not provided.
162//
163const char* GetBinaryName(EShLanguage stage)
164{
165 const char* name;
166 if (binaryFileName == nullptr) {
167 switch (stage) {
168 case EShLangVertex: name = "vert.spv"; break;
169 case EShLangTessControl: name = "tesc.spv"; break;
170 case EShLangTessEvaluation: name = "tese.spv"; break;
171 case EShLangGeometry: name = "geom.spv"; break;
172 case EShLangFragment: name = "frag.spv"; break;
173 case EShLangCompute: name = "comp.spv"; break;
174 default: name = "unknown"; break;
175 }
176 } else
177 name = binaryFileName;
178
179 return name;
180}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000181
John Kessenich05a70632013-09-17 19:26:08 +0000182//
183// *.conf => this is a config file that can set limits/resources
184//
185bool SetConfigFile(const std::string& name)
186{
187 if (name.size() < 5)
188 return false;
189
John Kessenich4c706852013-10-11 16:28:43 +0000190 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000191 ConfigFile = name;
192 return true;
193 }
194
195 return false;
196}
197
John Kessenich68d78fd2015-07-12 19:28:10 -0600198//
199// Give error and exit with failure code.
200//
201void Error(const char* message)
202{
203 printf("%s: Error %s (use -h for usage)\n", ExecutableName, message);
204 exit(EFailUsage);
205}
206
207//
208// Do all command-line argument parsing. This includes building up the work-items
209// to be processed later, and saving all the command-line options.
210//
211// Does not return (it exits) if command-line is fatally flawed.
212//
213void ProcessArguments(int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000214{
John Kessenich38f3b892013-09-06 19:52:57 +0000215 ExecutableName = argv[0];
216 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 +0000217 Work = new glslang::TWorkItem*[NumWorkItems];
John Kessenich68d78fd2015-07-12 19:28:10 -0600218 for (int w = 0; w < NumWorkItems; ++w)
219 Work[w] = 0;
John Kessenich38f3b892013-09-06 19:52:57 +0000220
John Kessenich2b07c7e2013-07-31 18:44:13 +0000221 argc--;
222 argv++;
223 for (; argc >= 1; argc--, argv++) {
224 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000225 switch (argv[0][1]) {
226 case 'H':
227 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600228 if ((Options & EOptionSpv) == 0) {
229 // default to Vulkan
230 Options |= EOptionSpv;
231 Options |= EOptionVulkanRules;
232 Options |= EOptionLinkProgram;
233 }
234 break;
John Kessenich0df0cde2015-03-03 17:09:43 +0000235 case 'V':
236 Options |= EOptionSpv;
John Kessenich68d78fd2015-07-12 19:28:10 -0600237 Options |= EOptionVulkanRules;
238 Options |= EOptionLinkProgram;
239 break;
dankbaker45d49bc2016-08-08 21:43:07 -0400240 case 'T':
Dan Bakerc6ede892016-08-11 14:06:06 -0400241 shaderStageName = argv[1];
dankbaker45d49bc2016-08-08 21:43:07 -0400242 if (argc > 0) {
243 argc--;
244 argv++;
245 }
246 else
247 Error("no <target> specified for -T");
248 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600249 case 'G':
250 Options |= EOptionSpv;
John Kessenichd78e3512014-08-25 20:07:55 +0000251 Options |= EOptionLinkProgram;
John Kessenich91e4aa52016-07-07 17:46:42 -0600252 // undo a -H default to Vulkan
253 Options &= ~EOptionVulkanRules;
John Kessenich92f90382014-07-28 04:21:04 +0000254 break;
John Kessenichc555ddd2015-06-17 02:38:44 +0000255 case 'E':
256 Options |= EOptionOutputPreprocessed;
257 break;
John Kessenich05a70632013-09-17 19:26:08 +0000258 case 'c':
259 Options |= EOptionDumpConfig;
260 break;
John Kessenicha86836e2016-07-09 14:50:57 -0600261 case 'C':
262 Options |= EOptionCascadingErrors;
263 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000264 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000265 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000266 break;
John Kessenich66e2faf2016-03-12 18:34:36 -0700267 case 'D':
268 Options |= EOptionReadHlsl;
269 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700270 case 'e':
271 // HLSL todo: entry point handle needs much more sophistication.
272 // This is okay for one compilation unit with one entry point.
dankbaker45d49bc2016-08-08 21:43:07 -0400273 // dankbaker - not sure it needs to be, fxc has no more sophistication then this
John Kessenich4d65ee32016-03-12 18:17:47 -0700274 entryPointName = argv[1];
275 if (argc > 0) {
276 argc--;
277 argv++;
278 } else
279 Error("no <entry-point> provided for -e");
280 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600281 case 'h':
282 usage();
283 break;
John Kessenich05a70632013-09-17 19:26:08 +0000284 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000285 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000286 break;
287 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000288 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000289 break;
290 case 'm':
291 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000292 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600293 case 'o':
294 binaryFileName = argv[1];
295 if (argc > 0) {
296 argc--;
297 argv++;
298 } else
299 Error("no <file> provided for -o");
300 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000301 case 'q':
302 Options |= EOptionDumpReflection;
303 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000304 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000305 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000306 break;
307 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000308 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000309 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000310 case 't':
311 #ifdef _WIN32
John Kessenichb0a7eb52013-11-07 17:44:20 +0000312 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000313 #endif
314 break;
John Kessenich319de232013-12-04 04:43:40 +0000315 case 'v':
316 Options |= EOptionDumpVersions;
317 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000318 case 'w':
319 Options |= EOptionSuppressWarnings;
320 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500321 case 'x':
322 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500323 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000324 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600325 usage();
326 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000327 }
John Kessenich38f3b892013-09-06 19:52:57 +0000328 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000329 std::string name(argv[0]);
330 if (! SetConfigFile(name)) {
331 Work[argc] = new glslang::TWorkItem(name);
332 Worklist.add(Work[argc]);
333 }
John Kessenich38f3b892013-09-06 19:52:57 +0000334 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000335 }
336
John Kessenich68d78fd2015-07-12 19:28:10 -0600337 // Make sure that -E is not specified alongside linking (which includes SPV generation)
338 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
339 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000340
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500341 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600342 if (binaryFileName && (Options & EOptionSpv) == 0)
343 Error("no binary generation requested (e.g., -V)");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000344}
345
John Kessenich68d78fd2015-07-12 19:28:10 -0600346//
347// Translate the meaningful subset of command-line options to parser-behavior options.
348//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000349void SetMessageOptions(EShMessages& messages)
350{
351 if (Options & EOptionRelaxedErrors)
352 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
353 if (Options & EOptionIntermediate)
354 messages = (EShMessages)(messages | EShMsgAST);
355 if (Options & EOptionSuppressWarnings)
356 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600357 if (Options & EOptionSpv)
358 messages = (EShMessages)(messages | EShMsgSpvRules);
359 if (Options & EOptionVulkanRules)
360 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400361 if (Options & EOptionOutputPreprocessed)
362 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700363 if (Options & EOptionReadHlsl)
364 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600365 if (Options & EOptionCascadingErrors)
366 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000367}
368
John Kessenich68d78fd2015-07-12 19:28:10 -0600369//
John Kessenich69f4b512013-09-04 21:19:27 +0000370// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000371//
372// Return 0 for failure, 1 for success.
373//
Pyry Haulos5f6892e2015-12-01 12:59:53 -0800374unsigned int CompileShaders(void*)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000375{
John Kessenich38f3b892013-09-06 19:52:57 +0000376 glslang::TWorkItem* workItem;
377 while (Worklist.remove(workItem)) {
378 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000379 if (compiler == 0)
John Kessenichc999ba22013-11-07 23:33:24 +0000380 return 0;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000381
John Kessenichb0a7eb52013-11-07 17:44:20 +0000382 CompileFile(workItem->name.c_str(), compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000383
John Kessenich94a81fb2013-08-31 02:41:30 +0000384 if (! (Options & EOptionSuppressInfolog))
John Kessenich38f3b892013-09-06 19:52:57 +0000385 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000386
387 ShDestruct(compiler);
388 }
389
390 return 0;
391}
392
John Kessenich6626cad2015-06-19 05:14:19 +0000393// Outputs the given string, but only if it is non-null and non-empty.
394// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400395void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000396{
397 if (str && str[0]) {
398 puts(str);
399 }
400}
401
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400402// Outputs the given string to stderr, but only if it is non-null and non-empty.
403// This prevents erroneous newlines from appearing.
404void StderrIfNonEmpty(const char* str)
405{
406 if (str && str[0]) {
407 fprintf(stderr, "%s\n", str);
408 }
409}
410
John Kessenichc57b2a92016-01-16 15:30:03 -0700411// Simple bundling of what makes a compilation unit for ease in passing around,
412// and separation of handling file IO versus API (programmatic) compilation.
413struct ShaderCompUnit {
414 EShLanguage stage;
415 std::string fileName;
416 char** text; // memory owned/managed externally
417};
418
John Kessenich69f4b512013-09-04 21:19:27 +0000419//
John Kessenichc57b2a92016-01-16 15:30:03 -0700420// For linking mode: Will independently parse each compilation unit, but then put them
421// in the same program and link them together, making at most one linked module per
422// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000423//
424// Uses the new C++ interface instead of the old handle-based interface.
425//
John Kessenichc57b2a92016-01-16 15:30:03 -0700426
427void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000428{
429 // keep track of what to free
430 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700431
John Kessenich69f4b512013-09-04 21:19:27 +0000432 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000433 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000434
John Kessenich69f4b512013-09-04 21:19:27 +0000435 //
436 // Per-shader processing...
437 //
438
John Kessenich5b0f13a2013-11-01 03:08:40 +0000439 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100440 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
441 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700442 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
443 shader->setStrings(compUnit.text, 1);
John Kessenich4d65ee32016-03-12 18:17:47 -0700444 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
445 shader->setEntryPoint(entryPointName);
John Kessenich69f4b512013-09-04 21:19:27 +0000446 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600447
John Kessenichc555ddd2015-06-17 02:38:44 +0000448 const int defaultVersion = Options & EOptionDefaultDesktop? 110: 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000449
John Kessenichc555ddd2015-06-17 02:38:44 +0000450 if (Options & EOptionOutputPreprocessed) {
451 std::string str;
Andrew Woloszyna132af52016-03-07 13:23:09 -0500452 glslang::TShader::ForbidInclude includer;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400453 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500454 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400455 PutsIfNonEmpty(str.c_str());
456 } else {
457 CompileFailed = true;
458 }
459 StderrIfNonEmpty(shader->getInfoLog());
460 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000461 continue;
462 }
463 if (! shader->parse(&Resources, defaultVersion, false, messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000464 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000465
John Kessenich69f4b512013-09-04 21:19:27 +0000466 program.addShader(shader);
467
John Kessenichc57b2a92016-01-16 15:30:03 -0700468 if (! (Options & EOptionSuppressInfolog) &&
469 ! (Options & EOptionMemoryLeakMode)) {
470 PutsIfNonEmpty(compUnit.fileName.c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400471 PutsIfNonEmpty(shader->getInfoLog());
472 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000473 }
John Kessenich69f4b512013-09-04 21:19:27 +0000474 }
475
476 //
477 // Program-level processing...
478 //
479
John Kessenich4d65ee32016-03-12 18:17:47 -0700480 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600481 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000482 LinkFailed = true;
483
John Kessenich4d65ee32016-03-12 18:17:47 -0700484 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700485 if (! (Options & EOptionSuppressInfolog) &&
486 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400487 PutsIfNonEmpty(program.getInfoLog());
488 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000489 }
490
John Kessenich4d65ee32016-03-12 18:17:47 -0700491 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000492 if (Options & EOptionDumpReflection) {
493 program.buildReflection();
494 program.dumpReflection();
495 }
496
John Kessenich4d65ee32016-03-12 18:17:47 -0700497 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000498 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000499 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600500 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000501 else {
502 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000503 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000504 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400505 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400506 spv::SpvBuildLogger logger;
507 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger);
John Kessenichc57b2a92016-01-16 15:30:03 -0700508
509 // Dump the spv to a file or stdout, etc., but only if not doing
510 // memory/perf testing, as it's not internal to programmatic use.
511 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500512 printf("%s", logger.getAllMessages().c_str());
513 if (Options & EOptionOutputHexadecimal) {
514 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage));
515 } else {
516 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
517 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700518 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700519 spv::Disassemble(std::cout, spirv);
520 }
John Kessenichacba7722015-03-04 03:48:38 +0000521 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000522 }
John Kessenich92f90382014-07-28 04:21:04 +0000523 }
524 }
525 }
526
John Kessenich5b0f13a2013-11-01 03:08:40 +0000527 // Free everything up, program has to go before the shaders
528 // because it might have merged stuff from the shaders, and
529 // the stuff from the shaders has to have its destructors called
530 // before the pools holding the memory in the shaders is freed.
531 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000532 while (shaders.size() > 0) {
533 delete shaders.back();
534 shaders.pop_back();
535 }
John Kessenich69f4b512013-09-04 21:19:27 +0000536}
537
John Kessenichc57b2a92016-01-16 15:30:03 -0700538//
539// Do file IO part of compile and link, handing off the pure
540// API/programmatic mode to CompileAndLinkShaderUnits(), which can
541// be put in a loop for testing memory footprint and performance.
542//
543// This is just for linking mode: meaning all the shaders will be put into the
544// the same program linked together.
545//
546// This means there are a limited number of work items (not multi-threading mode)
547// and that the point is testing at the linking level. Hence, to enable
548// performance and memory testing, the actual compile/link can be put in
549// a loop, independent of processing the work items and file IO.
550//
551void CompileAndLinkShaderFiles()
552{
553 std::vector<ShaderCompUnit> compUnits;
554
555 // Transfer all the work items from to a simple list of
556 // of compilation units. (We don't care about the thread
557 // work-item distribution properties in this path, which
558 // is okay due to the limited number of shaders, know since
559 // they are all getting linked together.)
560 glslang::TWorkItem* workItem;
561 while (Worklist.remove(workItem)) {
562 ShaderCompUnit compUnit = {
563 FindLanguage(workItem->name),
564 workItem->name,
565 ReadFileData(workItem->name.c_str())
566 };
567
568 if (! compUnit.text) {
569 usage();
570 return;
571 }
572
573 compUnits.push_back(compUnit);
574 }
575
576 // Actual call to programmatic processing of compile and link,
577 // in a loop for testing memory and performance. This part contains
578 // all the perf/memory that a programmatic consumer will care about.
579 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
580 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
581 CompileAndLinkShaderUnits(compUnits);
582
583 if (Options & EOptionMemoryLeakMode)
584 glslang::OS_DumpMemoryCounters();
585 }
586
rdb32084e82016-02-23 22:17:38 +0100587 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
588 FreeFileData(it->text);
John Kessenichc57b2a92016-01-16 15:30:03 -0700589}
590
John Kessenicha0af4732012-12-12 21:15:54 +0000591int C_DECL main(int argc, char* argv[])
592{
John Kessenich68d78fd2015-07-12 19:28:10 -0600593 ProcessArguments(argc, argv);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000594
John Kessenich05a70632013-09-17 19:26:08 +0000595 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -0500596 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
John Kessenich05a70632013-09-17 19:26:08 +0000597 if (Worklist.empty())
598 return ESuccess;
599 }
600
John Kessenich0da9eaa2015-08-01 17:10:02 -0600601 if (Options & EOptionDumpVersions) {
602 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +0000603 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
604 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -0600605 std::string spirvVersion;
606 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -0600607 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -0600608 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -0700609 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenichb84313d2016-07-20 16:03:29 -0600610 printf("GL_KHR_vulkan_glsl version %d\n", 100);
611 printf("ARB_GL_gl_spirv version %d\n", 100);
John Kessenich319de232013-12-04 04:43:40 +0000612 if (Worklist.empty())
613 return ESuccess;
614 }
615
John Kessenich05a70632013-09-17 19:26:08 +0000616 if (Worklist.empty()) {
617 usage();
John Kessenich05a70632013-09-17 19:26:08 +0000618 }
619
620 ProcessConfigFile();
621
John Kessenich69f4b512013-09-04 21:19:27 +0000622 //
623 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +0000624 // 1) linking all arguments together, single-threaded, new C++ interface
625 // 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 +0000626 //
John Kessenichc555ddd2015-06-17 02:38:44 +0000627 if (Options & EOptionLinkProgram ||
628 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +0000629 glslang::InitializeProcess();
John Kessenichc57b2a92016-01-16 15:30:03 -0700630 CompileAndLinkShaderFiles();
John Kessenichc36e1d82013-11-01 17:41:52 +0000631 glslang::FinalizeProcess();
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500632 for (int w = 0; w < NumWorkItems; ++w) {
633 if (Work[w]) {
634 delete Work[w];
635 }
636 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000637 } else {
638 ShInitialize();
639
John Kessenich38f3b892013-09-06 19:52:57 +0000640 bool printShaderNames = Worklist.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +0000641
John Kessenich38f3b892013-09-06 19:52:57 +0000642 if (Options & EOptionMultiThreaded) {
643 const int NumThreads = 16;
644 void* threads[NumThreads];
645 for (int t = 0; t < NumThreads; ++t) {
646 threads[t] = glslang::OS_CreateThread(&CompileShaders);
647 if (! threads[t]) {
648 printf("Failed to create thread\n");
649 return EFailThreadCreate;
650 }
John Kessenicha0af4732012-12-12 21:15:54 +0000651 }
John Kessenich38f3b892013-09-06 19:52:57 +0000652 glslang::OS_WaitForAllThreads(threads, NumThreads);
John Kessenichc999ba22013-11-07 23:33:24 +0000653 } else
654 CompileShaders(0);
John Kessenich38f3b892013-09-06 19:52:57 +0000655
656 // Print out all the resulting infologs
657 for (int w = 0; w < NumWorkItems; ++w) {
658 if (Work[w]) {
Kenneth Perryb07e6be2015-12-15 10:52:34 -0600659 if (printShaderNames || Work[w]->results.size() > 0)
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400660 PutsIfNonEmpty(Work[w]->name.c_str());
661 PutsIfNonEmpty(Work[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +0000662 delete Work[w];
663 }
664 }
John Kessenichc36e1d82013-11-01 17:41:52 +0000665
666 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +0000667 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000668
Andrew Woloszynb891c2b2016-01-18 09:26:25 -0500669 delete[] Work;
670
John Kessenichc999ba22013-11-07 23:33:24 +0000671 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000672 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +0000673 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +0000674 return EFailLink;
675
676 return 0;
677}
678
679//
680// Deduce the language from the filename. Files must end in one of the
681// following extensions:
682//
John Kessenich2b07c7e2013-07-31 18:44:13 +0000683// .vert = vertex
684// .tesc = tessellation control
685// .tese = tessellation evaluation
686// .geom = geometry
687// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +0000688// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +0000689//
John Kessenichb603f912013-08-29 00:39:25 +0000690EShLanguage FindLanguage(const std::string& name)
John Kessenicha0af4732012-12-12 21:15:54 +0000691{
John Kessenich2b07c7e2013-07-31 18:44:13 +0000692 size_t ext = name.rfind('.');
693 if (ext == std::string::npos) {
694 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000695 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000696 }
697
John Kessenich2b07c7e2013-07-31 18:44:13 +0000698 std::string suffix = name.substr(ext + 1, std::string::npos);
Dan Bakerc6ede892016-08-11 14:06:06 -0400699 if (shaderStageName)
700 suffix = shaderStageName;
dankbaker45d49bc2016-08-08 21:43:07 -0400701
John Kessenich2b07c7e2013-07-31 18:44:13 +0000702 if (suffix == "vert")
703 return EShLangVertex;
704 else if (suffix == "tesc")
705 return EShLangTessControl;
706 else if (suffix == "tese")
707 return EShLangTessEvaluation;
708 else if (suffix == "geom")
709 return EShLangGeometry;
710 else if (suffix == "frag")
711 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +0000712 else if (suffix == "comp")
713 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000714
715 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +0000716 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +0000717}
718
John Kessenicha0af4732012-12-12 21:15:54 +0000719//
John Kessenich69f4b512013-09-04 21:19:27 +0000720// Read a file's data into a string, and compile it using the old interface ShCompile,
721// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +0000722//
John Kessenich51cdd902014-02-18 23:37:57 +0000723void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +0000724{
John Kessenichca3457f2015-05-18 01:59:45 +0000725 int ret = 0;
John Kessenich41cf6b52013-06-25 18:10:05 +0000726 char** shaderStrings = ReadFileData(fileName);
John Kessenichdb4cd542013-06-26 22:42:55 +0000727 if (! shaderStrings) {
728 usage();
John Kessenichdb4cd542013-06-26 22:42:55 +0000729 }
730
John Kessenich41cf6b52013-06-25 18:10:05 +0000731 int* lengths = new int[NumShaderStrings];
732
733 // move to length-based strings, rather than null-terminated strings
734 for (int s = 0; s < NumShaderStrings; ++s)
John Kessenich35f04bd2014-02-19 02:47:20 +0000735 lengths[s] = (int)strlen(shaderStrings[s]);
John Kessenich09da79e2013-04-17 19:34:23 +0000736
John Kessenichc999ba22013-11-07 23:33:24 +0000737 if (! shaderStrings) {
738 CompileFailed = true;
739 return;
740 }
John Kessenicha0af4732012-12-12 21:15:54 +0000741
John Kessenich52ac67e2013-05-05 23:46:22 +0000742 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000743 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000744
John Kessenich94a81fb2013-08-31 02:41:30 +0000745 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
746 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich26ad2682014-08-13 20:17:19 +0000747 //ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichca3457f2015-05-18 01:59:45 +0000748 ret = ShCompile(compiler, shaderStrings, NumShaderStrings, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenichea869fb2013-10-28 18:12:06 +0000749 //const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
750 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
751 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich41cf6b52013-06-25 18:10:05 +0000752 //const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
John Kessenichca3457f2015-05-18 01:59:45 +0000753 //ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +0000754 }
John Kessenicha0af4732012-12-12 21:15:54 +0000755
John Kessenich94a81fb2013-08-31 02:41:30 +0000756 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000757 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +0000758 }
John Kessenicha0af4732012-12-12 21:15:54 +0000759
John Kessenich41cf6b52013-06-25 18:10:05 +0000760 delete [] lengths;
761 FreeFileData(shaderStrings);
John Kessenicha0af4732012-12-12 21:15:54 +0000762
John Kessenichc999ba22013-11-07 23:33:24 +0000763 if (ret == 0)
764 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +0000765}
766
John Kessenicha0af4732012-12-12 21:15:54 +0000767//
768// print usage to stdout
769//
770void usage()
771{
John Kessenich319de232013-12-04 04:43:40 +0000772 printf("Usage: glslangValidator [option]... [file]...\n"
773 "\n"
John Kessenich0df0cde2015-03-03 17:09:43 +0000774 "Where: each 'file' ends in .<stage>, where <stage> is one of\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600775 " .conf to provide an optional config file that replaces the default configuration\n"
776 " (see -c option below for generating a template)\n"
777 " .vert for a vertex shader\n"
778 " .tesc for a tessellation control shader\n"
779 " .tese for a tessellation evaluation shader\n"
780 " .geom for a geometry shader\n"
781 " .frag for a fragment shader\n"
782 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +0000783 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000784 "Compilation warnings and errors will be printed to stdout.\n"
John Kessenich319de232013-12-04 04:43:40 +0000785 "\n"
John Kessenich4586dbd2013-08-05 15:52:03 +0000786 "To get other information, use one of the following options:\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600787 "Each option must be specified separately.\n"
788 " -V create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
789 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600790 " -G create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
791 " default file name is <stage>.spv (-o overrides this)\n"
792 " -H print human readable form of SPIR-V; turns on -V\n"
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400793 " -E print pre-processed GLSL; cannot be used with -l;\n"
794 " errors will appear on stderr.\n"
Dan Bakerc6ede892016-08-11 14:06:06 -0400795 " -S <stage> uses explicit stage specified, rather then the file extension.\n"
dankbaker45d49bc2016-08-08 21:43:07 -0400796 " valid choices are vert,tesc, tese,geom, rag,comp\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600797 " -c configuration dump;\n"
798 " creates the default configuration file (redirect to a .conf file)\n"
John Kessenicha86836e2016-07-09 14:50:57 -0600799 " -C cascading errors; risks crashes from accumulation of error recoveries\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600800 " -d default to desktop (#version 110) when there is no shader #version\n"
801 " (default is ES version 100)\n"
John Kessenich66e2faf2016-03-12 18:34:36 -0700802 " -D input is HLSL\n"
John Kessenich4d65ee32016-03-12 18:17:47 -0700803 " -e specify entry-point name\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600804 " -h print this usage message\n"
805 " -i intermediate tree (glslang AST) is printed out\n"
806 " -l link all input files together to form a single module\n"
807 " -m memory leak mode\n"
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500808 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -0600809 " -q dump reflection query database\n"
810 " -r relaxed semantic error-checking mode\n"
811 " -s silent mode\n"
812 " -t multi-threaded mode\n"
813 " -v print version strings\n"
814 " -w suppress warnings (except as required by #extension : warn)\n"
Johannes van Waveren1fd01752016-05-31 08:39:41 -0500815 " -x save 32-bit hexadecimal numbers as text, requires a binary option (e.g., -V)\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +0000816 );
John Kessenich68d78fd2015-07-12 19:28:10 -0600817
818 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +0000819}
820
John Kessenich3ce4e592014-10-06 19:57:34 +0000821#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +0000822
823#include <errno.h>
824
825int fopen_s(
826 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +0000827 const char* filename,
828 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +0000829)
830{
831 if (!pFile || !filename || !mode) {
832 return EINVAL;
833 }
834
835 FILE* f = fopen(filename, mode);
836 if (! f) {
837 if (errno != 0) {
838 return errno;
839 } else {
840 return ENOENT;
841 }
842 }
843 *pFile = f;
844
845 return 0;
846}
847
848#endif
849
John Kessenicha0af4732012-12-12 21:15:54 +0000850//
851// Malloc a string of sufficient size and read a string into it.
852//
John Kessenich51cdd902014-02-18 23:37:57 +0000853char** ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +0000854{
John Kessenichb3297152015-07-11 18:01:03 -0600855 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +0000856 int errorCode = fopen_s(&in, fileName, "r");
John Kessenichd6c72a42014-08-18 19:42:35 +0000857
John Kessenicha0af4732012-12-12 21:15:54 +0000858 int count = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600859 const int maxSourceStrings = 5; // for testing splitting shader/tokens across multiple strings
860 char** return_data = (char**)malloc(sizeof(char *) * (maxSourceStrings+1)); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000861
John Kessenich68d78fd2015-07-12 19:28:10 -0600862 if (errorCode || in == nullptr)
863 Error("unable to open input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000864
865 while (fgetc(in) != EOF)
866 count++;
867
John Kessenichd6c72a42014-08-18 19:42:35 +0000868 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +0000869
John Kessenichb3297152015-07-11 18:01:03 -0600870 char *fdata = (char*)malloc(count+2); // freed before return of this function
John Kessenich68d78fd2015-07-12 19:28:10 -0600871 if (! fdata)
872 Error("can't allocate memory");
873
John Kessenichb3297152015-07-11 18:01:03 -0600874 if ((int)fread(fdata, 1, count, in) != count) {
John Kessenichb3297152015-07-11 18:01:03 -0600875 free(fdata);
John Kessenich68d78fd2015-07-12 19:28:10 -0600876 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +0000877 }
John Kessenich68d78fd2015-07-12 19:28:10 -0600878
John Kessenicha0af4732012-12-12 21:15:54 +0000879 fdata[count] = '\0';
880 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -0600881
John Kessenichea869fb2013-10-28 18:12:06 +0000882 if (count == 0) {
John Kessenichb3297152015-07-11 18:01:03 -0600883 // recover from empty file
884 return_data[0] = (char*)malloc(count+2); // freed in FreeFileData()
John Kessenicha0af4732012-12-12 21:15:54 +0000885 return_data[0][0]='\0';
John Kessenichea869fb2013-10-28 18:12:06 +0000886 NumShaderStrings = 0;
John Kessenichb3297152015-07-11 18:01:03 -0600887 free(fdata);
John Kessenicha0af4732012-12-12 21:15:54 +0000888
John Kessenichb3297152015-07-11 18:01:03 -0600889 return return_data;
890 } else
891 NumShaderStrings = 1; // Set to larger than 1 for testing multiple strings
892
893 // compute how to split up the file into multiple strings, for testing multiple strings
John Kessenichd6c72a42014-08-18 19:42:35 +0000894 int len = (int)(ceil)((float)count/(float)NumShaderStrings);
John Kessenichb3297152015-07-11 18:01:03 -0600895 int ptr_len = 0;
896 int i = 0;
897 while (count > 0) {
898 return_data[i] = (char*)malloc(len + 2); // freed in FreeFileData()
899 memcpy(return_data[i], fdata + ptr_len, len);
900 return_data[i][len] = '\0';
901 count -= len;
902 ptr_len += len;
903 if (count < len) {
904 if (count == 0) {
905 NumShaderStrings = i + 1;
John Kessenicha0af4732012-12-12 21:15:54 +0000906 break;
907 }
John Kessenichb3297152015-07-11 18:01:03 -0600908 len = count;
John Kessenichd6c72a42014-08-18 19:42:35 +0000909 }
910 ++i;
911 }
John Kessenichb3297152015-07-11 18:01:03 -0600912
913 free(fdata);
914
John Kessenicha0af4732012-12-12 21:15:54 +0000915 return return_data;
916}
917
John Kessenich51cdd902014-02-18 23:37:57 +0000918void FreeFileData(char** data)
John Kessenicha0af4732012-12-12 21:15:54 +0000919{
John Kessenichb3297152015-07-11 18:01:03 -0600920 for(int i = 0; i < NumShaderStrings; i++)
John Kessenicha0af4732012-12-12 21:15:54 +0000921 free(data[i]);
John Kessenichb3297152015-07-11 18:01:03 -0600922
923 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +0000924}
925
John Kessenich54d8cda2013-02-11 22:36:01 +0000926void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +0000927{
John Kessenichfae38ee2015-06-10 23:23:12 +0000928 if (num >= 0 )
929 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
930 else
931 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +0000932}