blob: e7844eb21f6eb548f21368d38662c6dd689659ce [file] [log] [blame]
John Kessenicha0af4732012-12-12 21:15:54 +00001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3// Copyright (C) 2013-2016 LunarG, Inc.
John Kessenichbd0747d2013-02-17 06:01:50 +00004//
John Kessenich927608b2017-01-06 12:34:14 -07005// All rights reserved.
John Kessenicha0af4732012-12-12 21:15:54 +00006//
John Kessenich927608b2017-01-06 12:34:14 -07007// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
John Kessenicha0af4732012-12-12 21:15:54 +000010//
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//
John Kessenich927608b2017-01-06 12:34:14 -070023// 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.
John Kessenicha0af4732012-12-12 21:15:54 +000035//
John Kessenich05a70632013-09-17 19:26:08 +000036
37// this only applies to the standalone wrapper, not the front end in general
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030038#ifndef _CRT_SECURE_NO_WARNINGS
John Kessenich05a70632013-09-17 19:26:08 +000039#define _CRT_SECURE_NO_WARNINGS
Aras Pranckevicius8e204b22017-05-10 16:52:50 +030040#endif
John Kessenich05a70632013-09-17 19:26:08 +000041
Lei Zhang8a9b1ee2016-05-19 13:31:43 -040042#include "ResourceLimits.h"
John Kessenich2b07c7e2013-07-31 18:44:13 +000043#include "Worklist.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060044#include "DirStackFileIncluder.h"
John Kessenicha0af4732012-12-12 21:15:54 +000045#include "./../glslang/Include/ShHandle.h"
John Kessenich0da9eaa2015-08-01 17:10:02 -060046#include "./../glslang/Include/revision.h"
John Kessenicha0af4732012-12-12 21:15:54 +000047#include "./../glslang/Public/ShaderLang.h"
John Kessenich0df0cde2015-03-03 17:09:43 +000048#include "../SPIRV/GlslangToSpv.h"
John Kessenich5e4b1242015-08-06 22:53:06 -060049#include "../SPIRV/GLSL.std.450.h"
John Kessenichacba7722015-03-04 03:48:38 +000050#include "../SPIRV/doc.h"
51#include "../SPIRV/disassemble.h"
John Kessenich3494b4d2017-05-22 15:00:42 -060052
John Kessenich66ec80e2016-08-05 14:04:23 -060053#include <cstring>
54#include <cstdlib>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060055#include <cctype>
John Kessenich66ec80e2016-08-05 14:04:23 -060056#include <cmath>
steve-lunarg7f7c2ed2016-09-07 15:20:19 -060057#include <array>
LoopDawg08a14422017-10-17 19:27:14 -060058#include <map>
Juan Lopeza558b262017-04-02 23:04:00 +020059#include <memory>
60#include <thread>
John Kessenicha0af4732012-12-12 21:15:54 +000061
baldurk876a0e32015-11-16 18:03:28 +010062#include "../glslang/OSDependent/osinclude.h"
John Kessenicha0af4732012-12-12 21:15:54 +000063
64extern "C" {
65 SH_IMPORT_EXPORT void ShOutputHtml();
66}
67
John Kessenich94a81fb2013-08-31 02:41:30 +000068// Command-line options
69enum TOptions {
John Kessenich906cc212016-12-09 19:22:20 -070070 EOptionNone = 0,
71 EOptionIntermediate = (1 << 0),
72 EOptionSuppressInfolog = (1 << 1),
73 EOptionMemoryLeakMode = (1 << 2),
74 EOptionRelaxedErrors = (1 << 3),
75 EOptionGiveWarnings = (1 << 4),
76 EOptionLinkProgram = (1 << 5),
77 EOptionMultiThreaded = (1 << 6),
78 EOptionDumpConfig = (1 << 7),
79 EOptionDumpReflection = (1 << 8),
80 EOptionSuppressWarnings = (1 << 9),
81 EOptionDumpVersions = (1 << 10),
82 EOptionSpv = (1 << 11),
83 EOptionHumanReadableSpv = (1 << 12),
84 EOptionVulkanRules = (1 << 13),
85 EOptionDefaultDesktop = (1 << 14),
86 EOptionOutputPreprocessed = (1 << 15),
87 EOptionOutputHexadecimal = (1 << 16),
88 EOptionReadHlsl = (1 << 17),
89 EOptionCascadingErrors = (1 << 18),
90 EOptionAutoMapBindings = (1 << 19),
steve-lunarge0b9deb2016-09-16 13:26:37 -060091 EOptionFlattenUniformArrays = (1 << 20),
John Kessenich906cc212016-12-09 19:22:20 -070092 EOptionNoStorageFormat = (1 << 21),
John Kessenich20f01e72016-12-12 11:41:43 -070093 EOptionKeepUncalled = (1 << 22),
John Kessenich4f1403e2017-04-05 17:38:20 -060094 EOptionHlslOffsets = (1 << 23),
steve-lunargbe283552017-04-18 12:18:01 -060095 EOptionHlslIoMapping = (1 << 24),
John Kessenich71facdf2017-05-17 18:28:19 -060096 EOptionAutoMapLocations = (1 << 25),
John Kessenich121853f2017-05-31 17:11:16 -060097 EOptionDebug = (1 << 26),
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +020098 EOptionStdin = (1 << 27),
GregFcd1f1692017-09-21 18:40:22 -060099 EOptionOptimizeDisable = (1 << 28),
100 EOptionOptimizeSize = (1 << 29),
John Kessenich94a81fb2013-08-31 02:41:30 +0000101};
102
John Kessenicha0af4732012-12-12 21:15:54 +0000103//
John Kessenich68d78fd2015-07-12 19:28:10 -0600104// Return codes from main/exit().
John Kessenicha0af4732012-12-12 21:15:54 +0000105//
106enum TFailCode {
107 ESuccess = 0,
108 EFailUsage,
109 EFailCompile,
110 EFailLink,
111 EFailCompilerCreate,
John Kessenich2b07c7e2013-07-31 18:44:13 +0000112 EFailThreadCreate,
John Kessenicha0af4732012-12-12 21:15:54 +0000113 EFailLinkerCreate
114};
115
116//
John Kessenich68d78fd2015-07-12 19:28:10 -0600117// Forward declarations.
John Kessenicha0af4732012-12-12 21:15:54 +0000118//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600119EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
John Kessenich51cdd902014-02-18 23:37:57 +0000120void CompileFile(const char* fileName, ShHandle);
John Kessenicha0af4732012-12-12 21:15:54 +0000121void usage();
John Kessenich04acb1b2017-06-14 17:36:50 -0600122char* ReadFileData(const char* fileName);
123void FreeFileData(char* data);
John Kessenich54d8cda2013-02-11 22:36:01 +0000124void InfoLogMsg(const char* msg, const char* name, const int num);
John Kessenich41cf6b52013-06-25 18:10:05 +0000125
John Kessenichc999ba22013-11-07 23:33:24 +0000126// Globally track if any compile or link failure.
127bool CompileFailed = false;
128bool LinkFailed = false;
129
John Kessenich94f28eb2017-11-13 01:32:06 -0700130// array of unique places to leave the shader names and infologs for the asynchronous compiles
131std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
132
John Kessenich05a70632013-09-17 19:26:08 +0000133TBuiltInResource Resources;
134std::string ConfigFile;
135
John Kessenicha0af4732012-12-12 21:15:54 +0000136//
John Kessenichf0bcb0a2016-04-02 13:09:14 -0600137// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
John Kessenich05a70632013-09-17 19:26:08 +0000138//
139void ProcessConfigFile()
John Kessenichb51f62c2013-04-11 16:31:09 +0000140{
John Kessenich04acb1b2017-06-14 17:36:50 -0600141 if (ConfigFile.size() == 0)
Lei Zhang414eb602016-03-04 16:22:34 -0500142 Resources = glslang::DefaultTBuiltInResource;
John Kessenich04acb1b2017-06-14 17:36:50 -0600143 else {
144 char* configString = ReadFileData(ConfigFile.c_str());
145 glslang::DecodeResourceLimits(&Resources, configString);
146 FreeFileData(configString);
John Kessenich05a70632013-09-17 19:26:08 +0000147 }
John Kessenicha0af4732012-12-12 21:15:54 +0000148}
149
John Kessenich94a81fb2013-08-31 02:41:30 +0000150int Options = 0;
John Kessenich68d78fd2015-07-12 19:28:10 -0600151const char* ExecutableName = nullptr;
152const char* binaryFileName = nullptr;
John Kessenich4d65ee32016-03-12 18:17:47 -0700153const char* entryPointName = nullptr;
steve-lunargf1e0c872016-10-31 15:13:43 -0600154const char* sourceEntryPointName = nullptr;
Dan Bakerc6ede892016-08-11 14:06:06 -0400155const char* shaderStageName = nullptr;
Flavioaea3c892017-02-06 11:46:35 -0800156const char* variableName = nullptr;
John Kessenich971a0a82017-06-07 15:06:58 -0600157std::vector<std::string> IncludeDirectoryList;
John Kessenich6353d552017-06-23 11:11:09 -0600158int ClientInputSemanticsVersion = 100; // maps to, say, #define VULKAN 100
159int VulkanClientVersion = 100; // would map to, say, Vulkan 1.0
160int OpenGLClientVersion = 450; // doesn't influence anything yet, but maps to OpenGL 4.50
161unsigned int TargetVersion = 0x00001000; // maps to, say, SPIR-V 1.0
John Kessenich2a271162017-07-20 20:00:36 -0600162std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
John Kessenich68d78fd2015-07-12 19:28:10 -0600163
LoopDawg08a14422017-10-17 19:27:14 -0600164// Per descriptor-set binding base data
165typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
166
167std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
168std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
Hyangran Park36dc8292017-05-02 16:27:29 +0900169std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600170
John Kessenicha9313662017-06-15 10:40:49 -0600171// Add things like "#define ..." to a preamble to use in the beginning of the shader.
172class TPreamble {
173public:
174 TPreamble() { }
175
176 bool isSet() const { return text.size() > 0; }
177 const char* get() const { return text.c_str(); }
178
179 // #define...
180 void addDef(std::string def)
181 {
182 text.append("#define ");
183 fixLine(def);
184
John Kessenich2a271162017-07-20 20:00:36 -0600185 Processes.push_back("D");
186 Processes.back().append(def);
187
John Kessenicha9313662017-06-15 10:40:49 -0600188 // The first "=" needs to turn into a space
LoopDawgb97b25e2017-07-12 09:04:39 -0600189 const size_t equal = def.find_first_of("=");
John Kessenicha9313662017-06-15 10:40:49 -0600190 if (equal != def.npos)
191 def[equal] = ' ';
192
193 text.append(def);
194 text.append("\n");
195 }
196
197 // #undef...
198 void addUndef(std::string undef)
199 {
200 text.append("#undef ");
201 fixLine(undef);
John Kessenich2a271162017-07-20 20:00:36 -0600202
203 Processes.push_back("U");
204 Processes.back().append(undef);
205
John Kessenicha9313662017-06-15 10:40:49 -0600206 text.append(undef);
207 text.append("\n");
208 }
209
210protected:
211 void fixLine(std::string& line)
212 {
213 // Can't go past a newline in the line
LoopDawgb97b25e2017-07-12 09:04:39 -0600214 const size_t end = line.find_first_of("\n");
John Kessenicha9313662017-06-15 10:40:49 -0600215 if (end != line.npos)
216 line = line.substr(0, end);
217 }
218
219 std::string text; // contents of preamble
220};
221
222TPreamble UserPreamble;
223
John Kessenich68d78fd2015-07-12 19:28:10 -0600224//
225// Create the default name for saving a binary if -o is not provided.
226//
227const char* GetBinaryName(EShLanguage stage)
228{
229 const char* name;
230 if (binaryFileName == nullptr) {
231 switch (stage) {
232 case EShLangVertex: name = "vert.spv"; break;
233 case EShLangTessControl: name = "tesc.spv"; break;
234 case EShLangTessEvaluation: name = "tese.spv"; break;
235 case EShLangGeometry: name = "geom.spv"; break;
236 case EShLangFragment: name = "frag.spv"; break;
237 case EShLangCompute: name = "comp.spv"; break;
238 default: name = "unknown"; break;
239 }
240 } else
241 name = binaryFileName;
242
243 return name;
244}
John Kessenich2b07c7e2013-07-31 18:44:13 +0000245
John Kessenich05a70632013-09-17 19:26:08 +0000246//
247// *.conf => this is a config file that can set limits/resources
248//
249bool SetConfigFile(const std::string& name)
250{
251 if (name.size() < 5)
252 return false;
253
John Kessenich4c706852013-10-11 16:28:43 +0000254 if (name.compare(name.size() - 5, 5, ".conf") == 0) {
John Kessenich05a70632013-09-17 19:26:08 +0000255 ConfigFile = name;
256 return true;
257 }
258
259 return false;
260}
261
John Kessenich68d78fd2015-07-12 19:28:10 -0600262//
263// Give error and exit with failure code.
264//
265void Error(const char* message)
266{
John Kessenichaf527992017-11-02 22:48:15 -0600267 fprintf(stderr, "%s: Error %s (use -h for usage)\n", ExecutableName, message);
John Kessenich68d78fd2015-07-12 19:28:10 -0600268 exit(EFailUsage);
269}
270
271//
LoopDawg08a14422017-10-17 19:27:14 -0600272// Process an optional binding base of one the forms:
273// --argname [stage] base // base for stage (if given) or all stages (if not)
LoopDawge5709552017-10-21 10:46:39 -0600274// --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
LoopDawg08a14422017-10-17 19:27:14 -0600275
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600276// Where stage is one of the forms accepted by FindLanguage, and base is an integer
277//
LoopDawg08a14422017-10-17 19:27:14 -0600278void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600279{
280 if (argc < 2)
281 usage();
282
LoopDawg08a14422017-10-17 19:27:14 -0600283 EShLanguage lang = EShLangCount;
284 int singleBase = 0;
285 TPerSetBaseBinding perSetBase;
286 int arg = 1;
287
288 // Parse stage, if given
289 if (!isdigit(argv[arg][0])) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600290 if (argc < 3) // this form needs one more argument
291 usage();
John Kessenichecba76f2017-01-06 00:34:48 -0700292
LoopDawg08a14422017-10-17 19:27:14 -0600293 lang = FindLanguage(argv[arg++], false);
294 }
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600295
LoopDawg08a14422017-10-17 19:27:14 -0600296 if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
297 // Parse a per-set binding base
298 while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
LoopDawg08a14422017-10-17 19:27:14 -0600299 const int baseNum = atoi(argv[arg++]);
LoopDawge5709552017-10-21 10:46:39 -0600300 const int setNum = atoi(argv[arg++]);
LoopDawg08a14422017-10-17 19:27:14 -0600301 perSetBase[setNum] = baseNum;
302 }
303 } else {
304 // Parse single binding base
305 singleBase = atoi(argv[arg++]);
306 }
307
308 argc -= (arg-1);
309 argv += (arg-1);
310
311 // Set one or all languages
312 const int langMin = (lang < EShLangCount) ? lang+0 : 0;
313 const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
314
315 for (int lang = langMin; lang < langMax; ++lang) {
316 if (!perSetBase.empty())
317 baseBindingForSet[res][lang] = perSetBase;
318 else
319 baseBinding[res][lang] = singleBase;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600320 }
321}
322
Hyangran Park36dc8292017-05-02 16:27:29 +0900323void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
324{
325 if (argc < 2)
326 usage();
327
328 if (!isdigit(argv[1][0])) {
LoopDawg52017192017-07-14 15:15:47 -0600329 if (argc < 3) // this form needs one more argument
Hyangran Park36dc8292017-05-02 16:27:29 +0900330 usage();
331
LoopDawg52017192017-07-14 15:15:47 -0600332 // Parse form: --argname stage [regname set base...], or:
333 // --argname stage set
Hyangran Park36dc8292017-05-02 16:27:29 +0900334 const EShLanguage lang = FindLanguage(argv[1], false);
335
LoopDawg52017192017-07-14 15:15:47 -0600336 argc--;
337 argv++;
338
339 while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
340 base[lang].push_back(argv[1]);
341
342 argc--;
343 argv++;
Hyangran Park36dc8292017-05-02 16:27:29 +0900344 }
LoopDawg52017192017-07-14 15:15:47 -0600345
346 // Must have one arg, or a multiple of three (for [regname set binding] triples)
347 if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
348 usage();
349
Hyangran Park36dc8292017-05-02 16:27:29 +0900350 } else {
LoopDawg52017192017-07-14 15:15:47 -0600351 // Parse form: --argname set
Hyangran Park36dc8292017-05-02 16:27:29 +0900352 for (int lang=0; lang<EShLangCount; ++lang)
353 base[lang].push_back(argv[1]);
354
355 argc--;
356 argv++;
357 }
358}
359
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600360//
John Kessenich68d78fd2015-07-12 19:28:10 -0600361// Do all command-line argument parsing. This includes building up the work-items
362// to be processed later, and saving all the command-line options.
363//
364// Does not return (it exits) if command-line is fatally flawed.
365//
Juan Lopeza558b262017-04-02 23:04:00 +0200366void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
John Kessenich2b07c7e2013-07-31 18:44:13 +0000367{
LoopDawg08a14422017-10-17 19:27:14 -0600368 for (int res = 0; res < glslang::EResCount; ++res)
369 baseBinding[res].fill(0);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600370
John Kessenich38f3b892013-09-06 19:52:57 +0000371 ExecutableName = argv[0];
Juan Lopeza558b262017-04-02 23:04:00 +0200372 workItems.reserve(argc);
John Kessenich38f3b892013-09-06 19:52:57 +0000373
John Kessenichc178f0a2017-06-23 10:58:31 -0600374 const auto bumpArg = [&]() {
375 if (argc > 0) {
376 argc--;
377 argv++;
378 }
379 };
380
381 // read a string directly attached to a single-letter option
John Kessenich6263fb12017-06-14 15:52:44 -0600382 const auto getStringOperand = [&](const char* desc) {
383 if (argv[0][2] == 0) {
384 printf("%s must immediately follow option (no spaces)\n", desc);
385 exit(EFailUsage);
386 }
387 return argv[0] + 2;
388 };
389
John Kessenich6353d552017-06-23 11:11:09 -0600390 // read a number attached to a single-letter option
391 const auto getAttachedNumber = [&](const char* desc) {
392 int num = atoi(argv[0] + 2);
393 if (num == 0) {
394 printf("%s: expected attached non-0 number\n", desc);
395 exit(EFailUsage);
396 }
397 return num;
398 };
399
400 // minimum needed (without overriding something else) to target Vulkan SPIR-V
401 const auto setVulkanSpv = []() {
402 Options |= EOptionSpv;
403 Options |= EOptionVulkanRules;
404 Options |= EOptionLinkProgram;
405 };
406
407 // minimum needed (without overriding something else) to target OpenGL SPIR-V
408 const auto setOpenGlSpv = []() {
409 Options |= EOptionSpv;
410 Options |= EOptionLinkProgram;
411 // undo a -H default to Vulkan
412 Options &= ~EOptionVulkanRules;
413 };
414
John Kessenichc178f0a2017-06-23 10:58:31 -0600415 for (bumpArg(); argc >= 1; bumpArg()) {
John Kessenich2b07c7e2013-07-31 18:44:13 +0000416 if (argv[0][0] == '-') {
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000417 switch (argv[0][1]) {
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600418 case '-':
419 {
420 std::string lowerword(argv[0]+2);
421 std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
422
423 // handle --word style options
John Kessenich6263fb12017-06-14 15:52:44 -0600424 if (lowerword == "auto-map-bindings" || // synonyms
John Kessenich6353d552017-06-23 11:11:09 -0600425 lowerword == "auto-map-binding" ||
426 lowerword == "amb") {
John Kessenich6263fb12017-06-14 15:52:44 -0600427 Options |= EOptionAutoMapBindings;
428 } else if (lowerword == "auto-map-locations" || // synonyms
429 lowerword == "aml") {
430 Options |= EOptionAutoMapLocations;
John Kessenich6353d552017-06-23 11:11:09 -0600431 } else if (lowerword == "client") {
432 if (argc > 1) {
433 if (strcmp(argv[1], "vulkan100") == 0)
434 setVulkanSpv();
435 else if (strcmp(argv[1], "opengl100") == 0)
436 setOpenGlSpv();
437 else
438 Error("--client expects vulkan100 or opengl100");
439 }
440 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600441 } else if (lowerword == "flatten-uniform-arrays" || // synonyms
442 lowerword == "flatten-uniform-array" ||
443 lowerword == "fua") {
444 Options |= EOptionFlattenUniformArrays;
445 } else if (lowerword == "hlsl-offsets") {
446 Options |= EOptionHlslOffsets;
447 } else if (lowerword == "hlsl-iomap" ||
448 lowerword == "hlsl-iomapper" ||
449 lowerword == "hlsl-iomapping") {
450 Options |= EOptionHlslIoMapping;
451 } else if (lowerword == "keep-uncalled" || // synonyms
452 lowerword == "ku") {
453 Options |= EOptionKeepUncalled;
454 } else if (lowerword == "no-storage-format" || // synonyms
455 lowerword == "nsf") {
456 Options |= EOptionNoStorageFormat;
John Kessenich2a271162017-07-20 20:00:36 -0600457 } else if (lowerword == "relaxed-errors") {
458 Options |= EOptionRelaxedErrors;
John Kessenich6263fb12017-06-14 15:52:44 -0600459 } else if (lowerword == "resource-set-bindings" || // synonyms
460 lowerword == "resource-set-binding" ||
461 lowerword == "rsb") {
462 ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
steve-lunarg9088be42016-11-01 10:31:42 -0600463 } else if (lowerword == "shift-image-bindings" || // synonyms
464 lowerword == "shift-image-binding" ||
465 lowerword == "sib") {
LoopDawg08a14422017-10-17 19:27:14 -0600466 ProcessBindingBase(argc, argv, glslang::EResImage);
John Kessenich6263fb12017-06-14 15:52:44 -0600467 } else if (lowerword == "shift-sampler-bindings" || // synonyms
468 lowerword == "shift-sampler-binding" ||
469 lowerword == "ssb") {
LoopDawg08a14422017-10-17 19:27:14 -0600470 ProcessBindingBase(argc, argv, glslang::EResSampler);
John Kessenich6263fb12017-06-14 15:52:44 -0600471 } else if (lowerword == "shift-uav-bindings" || // synonyms
472 lowerword == "shift-uav-binding" ||
473 lowerword == "suavb") {
LoopDawg08a14422017-10-17 19:27:14 -0600474 ProcessBindingBase(argc, argv, glslang::EResUav);
John Kessenich6263fb12017-06-14 15:52:44 -0600475 } else if (lowerword == "shift-texture-bindings" || // synonyms
476 lowerword == "shift-texture-binding" ||
477 lowerword == "stb") {
LoopDawg08a14422017-10-17 19:27:14 -0600478 ProcessBindingBase(argc, argv, glslang::EResTexture);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600479 } else if (lowerword == "shift-ubo-bindings" || // synonyms
480 lowerword == "shift-ubo-binding" ||
steve-lunargbe283552017-04-18 12:18:01 -0600481 lowerword == "shift-cbuffer-bindings" ||
482 lowerword == "shift-cbuffer-binding" ||
483 lowerword == "sub" ||
484 lowerword == "scb") {
LoopDawg08a14422017-10-17 19:27:14 -0600485 ProcessBindingBase(argc, argv, glslang::EResUbo);
steve-lunarg932bb5c2017-02-21 17:19:08 -0700486 } else if (lowerword == "shift-ssbo-bindings" || // synonyms
487 lowerword == "shift-ssbo-binding" ||
488 lowerword == "sbb") {
LoopDawg08a14422017-10-17 19:27:14 -0600489 ProcessBindingBase(argc, argv, glslang::EResSsbo);
John Kessenich6263fb12017-06-14 15:52:44 -0600490 } else if (lowerword == "source-entrypoint" || // synonyms
491 lowerword == "sep") {
John Kessenichc178f0a2017-06-23 10:58:31 -0600492 if (argc <= 1)
John Kessenich6263fb12017-06-14 15:52:44 -0600493 Error("no <entry-point> provided for --source-entrypoint");
John Kessenichc178f0a2017-06-23 10:58:31 -0600494 sourceEntryPointName = argv[1];
495 bumpArg();
John Kessenich6263fb12017-06-14 15:52:44 -0600496 break;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200497 } else if (lowerword == "stdin") {
498 Options |= EOptionStdin;
499 shaderStageName = argv[1];
John Kessenich2a271162017-07-20 20:00:36 -0600500 } else if (lowerword == "suppress-warnings") {
501 Options |= EOptionSuppressWarnings;
John Kessenich6353d552017-06-23 11:11:09 -0600502 } else if (lowerword == "target-env") {
503 if (argc > 1) {
504 if (strcmp(argv[1], "vulkan1.0") == 0) {
505 setVulkanSpv();
506 VulkanClientVersion = 100;
507 } else if (strcmp(argv[1], "opengl") == 0) {
508 setOpenGlSpv();
509 OpenGLClientVersion = 450;
510 } else
511 Error("--target-env expected vulkan1.0 or opengl");
512 }
513 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800514 } else if (lowerword == "variable-name" || // synonyms
515 lowerword == "vn") {
516 Options |= EOptionOutputHexadecimal;
John Kessenichc178f0a2017-06-23 10:58:31 -0600517 if (argc <= 1)
Flavio15017db2017-02-15 14:29:33 -0800518 Error("no <C-variable-name> provided for --variable-name");
John Kessenichc178f0a2017-06-23 10:58:31 -0600519 variableName = argv[1];
520 bumpArg();
Flavio15017db2017-02-15 14:29:33 -0800521 break;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600522 } else {
523 usage();
524 }
525 }
526 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600527 case 'C':
528 Options |= EOptionCascadingErrors;
529 break;
530 case 'D':
John Kessenicha9313662017-06-15 10:40:49 -0600531 if (argv[0][2] == 0)
532 Options |= EOptionReadHlsl;
533 else
534 UserPreamble.addDef(getStringOperand("-D<macro> macro name"));
John Kessenich6263fb12017-06-14 15:52:44 -0600535 break;
536 case 'E':
537 Options |= EOptionOutputPreprocessed;
538 break;
539 case 'G':
John Kessenich6353d552017-06-23 11:11:09 -0600540 // OpenGL Client
541 setOpenGlSpv();
542 if (argv[0][2] != 0)
543 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenich6263fb12017-06-14 15:52:44 -0600544 break;
John Kessenich2aa7f3a2015-05-15 16:02:07 +0000545 case 'H':
546 Options |= EOptionHumanReadableSpv;
John Kessenich91e4aa52016-07-07 17:46:42 -0600547 if ((Options & EOptionSpv) == 0) {
548 // default to Vulkan
John Kessenich6353d552017-06-23 11:11:09 -0600549 setVulkanSpv();
John Kessenich91e4aa52016-07-07 17:46:42 -0600550 }
551 break;
John Kessenich971a0a82017-06-07 15:06:58 -0600552 case 'I':
John Kessenicha9313662017-06-15 10:40:49 -0600553 IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
John Kessenich68d78fd2015-07-12 19:28:10 -0600554 break;
GregFcd1f1692017-09-21 18:40:22 -0600555 case 'O':
556 if (argv[0][2] == 'd')
557 Options |= EOptionOptimizeDisable;
558 else if (argv[0][2] == 's')
559#ifdef ENABLE_OPT
560 Options |= EOptionOptimizeSize;
561#else
562 Error("-Os not available; optimizer not linked");
563#endif
564 else
565 Error("unknown -O option");
566 break;
Dan Baker5afdd782016-08-11 17:53:57 -0400567 case 'S':
John Kessenichc178f0a2017-06-23 10:58:31 -0600568 if (argc <= 1)
Dan Baker5afdd782016-08-11 17:53:57 -0400569 Error("no <stage> specified for -S");
John Kessenichc178f0a2017-06-23 10:58:31 -0600570 shaderStageName = argv[1];
571 bumpArg();
dankbaker45d49bc2016-08-08 21:43:07 -0400572 break;
John Kessenicha9313662017-06-15 10:40:49 -0600573 case 'U':
574 UserPreamble.addUndef(getStringOperand("-U<macro>: macro name"));
575 break;
John Kessenich6263fb12017-06-14 15:52:44 -0600576 case 'V':
John Kessenich6353d552017-06-23 11:11:09 -0600577 setVulkanSpv();
578 if (argv[0][2] != 0)
579 ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
John Kessenichc555ddd2015-06-17 02:38:44 +0000580 break;
John Kessenich05a70632013-09-17 19:26:08 +0000581 case 'c':
582 Options |= EOptionDumpConfig;
583 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000584 case 'd':
John Kessenich26ad2682014-08-13 20:17:19 +0000585 Options |= EOptionDefaultDesktop;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000586 break;
John Kessenich4d65ee32016-03-12 18:17:47 -0700587 case 'e':
588 // HLSL todo: entry point handle needs much more sophistication.
589 // This is okay for one compilation unit with one entry point.
590 entryPointName = argv[1];
John Kessenichc178f0a2017-06-23 10:58:31 -0600591 if (argc <= 1)
John Kessenicha25530c2017-09-11 20:13:49 -0600592 Error("no <name> provided for -e");
John Kessenichc178f0a2017-06-23 10:58:31 -0600593 bumpArg();
John Kessenich4d65ee32016-03-12 18:17:47 -0700594 break;
John Kessenich121853f2017-05-31 17:11:16 -0600595 case 'g':
596 Options |= EOptionDebug;
597 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600598 case 'h':
599 usage();
600 break;
John Kessenich05a70632013-09-17 19:26:08 +0000601 case 'i':
John Kessenich94a81fb2013-08-31 02:41:30 +0000602 Options |= EOptionIntermediate;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000603 break;
604 case 'l':
John Kessenichd78e3512014-08-25 20:07:55 +0000605 Options |= EOptionLinkProgram;
John Kessenich94a81fb2013-08-31 02:41:30 +0000606 break;
607 case 'm':
608 Options |= EOptionMemoryLeakMode;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000609 break;
John Kessenich68d78fd2015-07-12 19:28:10 -0600610 case 'o':
John Kessenichc178f0a2017-06-23 10:58:31 -0600611 if (argc <= 1)
John Kessenich68d78fd2015-07-12 19:28:10 -0600612 Error("no <file> provided for -o");
John Kessenichc178f0a2017-06-23 10:58:31 -0600613 binaryFileName = argv[1];
614 bumpArg();
John Kessenich68d78fd2015-07-12 19:28:10 -0600615 break;
John Kessenich11f9fc72013-11-07 01:06:34 +0000616 case 'q':
617 Options |= EOptionDumpReflection;
618 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000619 case 'r':
John Kessenich94a81fb2013-08-31 02:41:30 +0000620 Options |= EOptionRelaxedErrors;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000621 break;
622 case 's':
John Kessenich94a81fb2013-08-31 02:41:30 +0000623 Options |= EOptionSuppressInfolog;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000624 break;
John Kessenich38f3b892013-09-06 19:52:57 +0000625 case 't':
Juan Lopeza558b262017-04-02 23:04:00 +0200626 Options |= EOptionMultiThreaded;
John Kessenich38f3b892013-09-06 19:52:57 +0000627 break;
John Kessenich319de232013-12-04 04:43:40 +0000628 case 'v':
629 Options |= EOptionDumpVersions;
630 break;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000631 case 'w':
632 Options |= EOptionSuppressWarnings;
633 break;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500634 case 'x':
635 Options |= EOptionOutputHexadecimal;
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500636 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000637 default:
John Kessenich68d78fd2015-07-12 19:28:10 -0600638 usage();
639 break;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000640 }
John Kessenich38f3b892013-09-06 19:52:57 +0000641 } else {
John Kessenich05a70632013-09-17 19:26:08 +0000642 std::string name(argv[0]);
643 if (! SetConfigFile(name)) {
Juan Lopeza558b262017-04-02 23:04:00 +0200644 workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
John Kessenich05a70632013-09-17 19:26:08 +0000645 }
John Kessenich38f3b892013-09-06 19:52:57 +0000646 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000647 }
648
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200649 // Make sure that -S is always specified if --stdin is specified
650 if ((Options & EOptionStdin) && shaderStageName == nullptr)
651 Error("must provide -S when --stdin is given");
652
John Kessenich68d78fd2015-07-12 19:28:10 -0600653 // Make sure that -E is not specified alongside linking (which includes SPV generation)
654 if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
655 Error("can't use -E when linking is selected");
John Kessenichc555ddd2015-06-17 02:38:44 +0000656
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500657 // -o or -x makes no sense if there is no target binary
John Kessenich68d78fd2015-07-12 19:28:10 -0600658 if (binaryFileName && (Options & EOptionSpv) == 0)
659 Error("no binary generation requested (e.g., -V)");
steve-lunarge0b9deb2016-09-16 13:26:37 -0600660
661 if ((Options & EOptionFlattenUniformArrays) != 0 &&
662 (Options & EOptionReadHlsl) == 0)
663 Error("uniform array flattening only valid when compiling HLSL source.");
John Kessenich2b07c7e2013-07-31 18:44:13 +0000664}
665
John Kessenich68d78fd2015-07-12 19:28:10 -0600666//
667// Translate the meaningful subset of command-line options to parser-behavior options.
668//
John Kessenichb0a7eb52013-11-07 17:44:20 +0000669void SetMessageOptions(EShMessages& messages)
670{
671 if (Options & EOptionRelaxedErrors)
672 messages = (EShMessages)(messages | EShMsgRelaxedErrors);
673 if (Options & EOptionIntermediate)
674 messages = (EShMessages)(messages | EShMsgAST);
675 if (Options & EOptionSuppressWarnings)
676 messages = (EShMessages)(messages | EShMsgSuppressWarnings);
John Kessenich68d78fd2015-07-12 19:28:10 -0600677 if (Options & EOptionSpv)
678 messages = (EShMessages)(messages | EShMsgSpvRules);
679 if (Options & EOptionVulkanRules)
680 messages = (EShMessages)(messages | EShMsgVulkanRules);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400681 if (Options & EOptionOutputPreprocessed)
682 messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
John Kessenich66e2faf2016-03-12 18:34:36 -0700683 if (Options & EOptionReadHlsl)
684 messages = (EShMessages)(messages | EShMsgReadHlsl);
John Kessenicha86836e2016-07-09 14:50:57 -0600685 if (Options & EOptionCascadingErrors)
686 messages = (EShMessages)(messages | EShMsgCascadingErrors);
John Kessenich906cc212016-12-09 19:22:20 -0700687 if (Options & EOptionKeepUncalled)
688 messages = (EShMessages)(messages | EShMsgKeepUncalled);
John Kessenich4f1403e2017-04-05 17:38:20 -0600689 if (Options & EOptionHlslOffsets)
690 messages = (EShMessages)(messages | EShMsgHlslOffsets);
John Kessenich121853f2017-05-31 17:11:16 -0600691 if (Options & EOptionDebug)
692 messages = (EShMessages)(messages | EShMsgDebugInfo);
John Kessenichb0a7eb52013-11-07 17:44:20 +0000693}
694
John Kessenich68d78fd2015-07-12 19:28:10 -0600695//
John Kessenich69f4b512013-09-04 21:19:27 +0000696// Thread entry point, for non-linking asynchronous mode.
John Kessenichc999ba22013-11-07 23:33:24 +0000697//
Juan Lopeza558b262017-04-02 23:04:00 +0200698void CompileShaders(glslang::TWorklist& worklist)
John Kessenich2b07c7e2013-07-31 18:44:13 +0000699{
John Kessenich38f3b892013-09-06 19:52:57 +0000700 glslang::TWorkItem* workItem;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200701 if (Options & EOptionStdin) {
702 worklist.remove(workItem);
703 ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000704 if (compiler == 0)
Juan Lopeza558b262017-04-02 23:04:00 +0200705 return;
John Kessenich2b07c7e2013-07-31 18:44:13 +0000706
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200707 CompileFile("stdin", compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000708
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200709 if (! (Options & EOptionSuppressInfolog))
710 workItem->results = ShGetInfoLog(compiler);
John Kessenich2b07c7e2013-07-31 18:44:13 +0000711
712 ShDestruct(compiler);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200713 } else {
714 while (worklist.remove(workItem)) {
715 ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
716 if (compiler == 0)
717 return;
718
719 CompileFile(workItem->name.c_str(), compiler);
720
721 if (! (Options & EOptionSuppressInfolog))
722 workItem->results = ShGetInfoLog(compiler);
723
724 ShDestruct(compiler);
725 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000726 }
John Kessenich2b07c7e2013-07-31 18:44:13 +0000727}
728
John Kessenich6626cad2015-06-19 05:14:19 +0000729// Outputs the given string, but only if it is non-null and non-empty.
730// This prevents erroneous newlines from appearing.
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400731void PutsIfNonEmpty(const char* str)
John Kessenich6626cad2015-06-19 05:14:19 +0000732{
733 if (str && str[0]) {
734 puts(str);
735 }
736}
737
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400738// Outputs the given string to stderr, but only if it is non-null and non-empty.
739// This prevents erroneous newlines from appearing.
740void StderrIfNonEmpty(const char* str)
741{
John Kessenich04acb1b2017-06-14 17:36:50 -0600742 if (str && str[0])
743 fprintf(stderr, "%s\n", str);
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400744}
745
John Kessenichc57b2a92016-01-16 15:30:03 -0700746// Simple bundling of what makes a compilation unit for ease in passing around,
747// and separation of handling file IO versus API (programmatic) compilation.
748struct ShaderCompUnit {
749 EShLanguage stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600750 static const int maxCount = 1;
751 int count; // live number of strings/names
John Kessenicha9313662017-06-15 10:40:49 -0600752 const char* text[maxCount]; // memory owned/managed externally
John Kessenich04acb1b2017-06-14 17:36:50 -0600753 std::string fileName[maxCount]; // hold's the memory, but...
754 const char* fileNameList[maxCount]; // downstream interface wants pointers
dankbakerafe6e9c2016-08-21 12:29:08 -0400755
John Kessenich04acb1b2017-06-14 17:36:50 -0600756 ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
dankbakerafe6e9c2016-08-21 12:29:08 -0400757
John Kessenich04acb1b2017-06-14 17:36:50 -0600758 ShaderCompUnit(const ShaderCompUnit& rhs)
dankbakerafe6e9c2016-08-21 12:29:08 -0400759 {
760 stage = rhs.stage;
John Kessenich04acb1b2017-06-14 17:36:50 -0600761 count = rhs.count;
762 for (int i = 0; i < count; ++i) {
763 fileName[i] = rhs.fileName[i];
764 text[i] = rhs.text[i];
765 fileNameList[i] = rhs.fileName[i].c_str();
766 }
dankbakerafe6e9c2016-08-21 12:29:08 -0400767 }
768
John Kessenicha9313662017-06-15 10:40:49 -0600769 void addString(std::string& ifileName, const char* itext)
John Kessenich04acb1b2017-06-14 17:36:50 -0600770 {
771 assert(count < maxCount);
772 fileName[count] = ifileName;
773 text[count] = itext;
774 fileNameList[count] = fileName[count].c_str();
775 ++count;
776 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700777};
778
John Kessenich69f4b512013-09-04 21:19:27 +0000779//
John Kessenichc57b2a92016-01-16 15:30:03 -0700780// For linking mode: Will independently parse each compilation unit, but then put them
781// in the same program and link them together, making at most one linked module per
782// pipeline stage.
John Kessenich69f4b512013-09-04 21:19:27 +0000783//
784// Uses the new C++ interface instead of the old handle-based interface.
785//
John Kessenichc57b2a92016-01-16 15:30:03 -0700786
787void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
John Kessenich69f4b512013-09-04 21:19:27 +0000788{
789 // keep track of what to free
790 std::list<glslang::TShader*> shaders;
John Kessenichc57b2a92016-01-16 15:30:03 -0700791
John Kessenich69f4b512013-09-04 21:19:27 +0000792 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +0000793 SetMessageOptions(messages);
John Kessenich69f4b512013-09-04 21:19:27 +0000794
John Kessenich69f4b512013-09-04 21:19:27 +0000795 //
796 // Per-shader processing...
797 //
798
John Kessenich5b0f13a2013-11-01 03:08:40 +0000799 glslang::TProgram& program = *new glslang::TProgram;
rdb32084e82016-02-23 22:17:38 +0100800 for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
801 const auto &compUnit = *it;
John Kessenichc57b2a92016-01-16 15:30:03 -0700802 glslang::TShader* shader = new glslang::TShader(compUnit.stage);
John Kessenich04acb1b2017-06-14 17:36:50 -0600803 shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
John Kessenich4d65ee32016-03-12 18:17:47 -0700804 if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
805 shader->setEntryPoint(entryPointName);
John Kessenich3693e632017-09-29 17:51:52 -0600806 if (sourceEntryPointName) {
807 if (entryPointName == nullptr)
808 printf("Warning: Changing source entry point name without setting an entry-point name.\n"
809 "Use '-e <name>'.\n");
steve-lunargf1e0c872016-10-31 15:13:43 -0600810 shader->setSourceEntryPoint(sourceEntryPointName);
John Kessenich3693e632017-09-29 17:51:52 -0600811 }
John Kessenicha9313662017-06-15 10:40:49 -0600812 if (UserPreamble.isSet())
813 shader->setPreamble(UserPreamble.get());
John Kessenich2a271162017-07-20 20:00:36 -0600814 shader->addProcesses(Processes);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600815
LoopDawg08a14422017-10-17 19:27:14 -0600816 // Set IO mapper binding shift values
817 for (int r = 0; r < glslang::EResCount; ++r) {
818 const glslang::TResourceType res = glslang::TResourceType(r);
819
820 // Set base bindings
821 shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
822
823 // Set bindings for particular resource sets
824 // TODO: use a range based for loop here, when available in all environments.
825 for (auto i = baseBindingForSet[res][compUnit.stage].begin();
826 i != baseBindingForSet[res][compUnit.stage].end(); ++i)
LoopDawge5709552017-10-21 10:46:39 -0600827 shader->setShiftBindingForSet(res, i->second, i->first);
LoopDawg08a14422017-10-17 19:27:14 -0600828 }
829
steve-lunarge0b9deb2016-09-16 13:26:37 -0600830 shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
steve-lunargcce8d482016-10-14 18:36:42 -0600831 shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
Hyangran Park36dc8292017-05-02 16:27:29 +0900832 shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600833
steve-lunargbe283552017-04-18 12:18:01 -0600834 if (Options & EOptionHlslIoMapping)
835 shader->setHlslIoMapping(true);
836
steve-lunarg7f7c2ed2016-09-07 15:20:19 -0600837 if (Options & EOptionAutoMapBindings)
838 shader->setAutoMapBindings(true);
John Kessenichecba76f2017-01-06 00:34:48 -0700839
John Kessenich71facdf2017-05-17 18:28:19 -0600840 if (Options & EOptionAutoMapLocations)
841 shader->setAutoMapLocations(true);
842
John Kessenich4be4aeb2017-06-23 10:50:22 -0600843 // Set up the environment, some subsettings take precedence over earlier
844 // ways of setting things.
845 if (Options & EOptionSpv) {
846 if (Options & EOptionVulkanRules) {
847 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
848 : glslang::EShSourceGlsl,
John Kessenich6353d552017-06-23 11:11:09 -0600849 compUnit.stage, glslang::EShClientVulkan, ClientInputSemanticsVersion);
850 shader->setEnvClient(glslang::EShClientVulkan, VulkanClientVersion);
851 shader->setEnvTarget(glslang::EshTargetSpv, TargetVersion);
John Kessenich4be4aeb2017-06-23 10:50:22 -0600852 } else {
853 shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
854 : glslang::EShSourceGlsl,
John Kessenich6353d552017-06-23 11:11:09 -0600855 compUnit.stage, glslang::EShClientOpenGL, ClientInputSemanticsVersion);
856 shader->setEnvClient(glslang::EShClientOpenGL, OpenGLClientVersion);
857 shader->setEnvTarget(glslang::EshTargetSpv, TargetVersion);
John Kessenich4be4aeb2017-06-23 10:50:22 -0600858 }
859 }
860
John Kessenich69f4b512013-09-04 21:19:27 +0000861 shaders.push_back(shader);
John Kessenichb3297152015-07-11 18:01:03 -0600862
John Kessenich4be4aeb2017-06-23 10:50:22 -0600863 const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
John Kessenich69f4b512013-09-04 21:19:27 +0000864
John Kessenich3494b4d2017-05-22 15:00:42 -0600865 DirStackFileIncluder includer;
John Kessenich971a0a82017-06-07 15:06:58 -0600866 std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
867 includer.pushExternalLocalDirectory(dir); });
John Kessenichc555ddd2015-06-17 02:38:44 +0000868 if (Options & EOptionOutputPreprocessed) {
869 std::string str;
Dejan Mircevski7be4b822015-06-17 11:40:33 -0400870 if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
Andrew Woloszyna132af52016-03-07 13:23:09 -0500871 messages, &str, includer)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400872 PutsIfNonEmpty(str.c_str());
873 } else {
874 CompileFailed = true;
875 }
876 StderrIfNonEmpty(shader->getInfoLog());
877 StderrIfNonEmpty(shader->getInfoDebugLog());
John Kessenichc555ddd2015-06-17 02:38:44 +0000878 continue;
879 }
John Kessenich3494b4d2017-05-22 15:00:42 -0600880 if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
John Kessenichc999ba22013-11-07 23:33:24 +0000881 CompileFailed = true;
John Kessenichc555ddd2015-06-17 02:38:44 +0000882
John Kessenich69f4b512013-09-04 21:19:27 +0000883 program.addShader(shader);
884
John Kessenichc57b2a92016-01-16 15:30:03 -0700885 if (! (Options & EOptionSuppressInfolog) &&
886 ! (Options & EOptionMemoryLeakMode)) {
John Kessenich04acb1b2017-06-14 17:36:50 -0600887 PutsIfNonEmpty(compUnit.fileName[0].c_str());
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400888 PutsIfNonEmpty(shader->getInfoLog());
889 PutsIfNonEmpty(shader->getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000890 }
John Kessenich69f4b512013-09-04 21:19:27 +0000891 }
892
893 //
894 // Program-level processing...
895 //
896
John Kessenich4d65ee32016-03-12 18:17:47 -0700897 // Link
John Kessenich68d78fd2015-07-12 19:28:10 -0600898 if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
John Kessenichc999ba22013-11-07 23:33:24 +0000899 LinkFailed = true;
900
steve-lunarg9ae34742016-10-05 13:40:13 -0600901 // Map IO
902 if (Options & EOptionSpv) {
903 if (!program.mapIO())
904 LinkFailed = true;
905 }
John Kessenichecba76f2017-01-06 00:34:48 -0700906
John Kessenich4d65ee32016-03-12 18:17:47 -0700907 // Report
John Kessenichc57b2a92016-01-16 15:30:03 -0700908 if (! (Options & EOptionSuppressInfolog) &&
909 ! (Options & EOptionMemoryLeakMode)) {
Andrew Woloszynaae1ad82015-06-24 17:00:46 -0400910 PutsIfNonEmpty(program.getInfoLog());
911 PutsIfNonEmpty(program.getInfoDebugLog());
John Kessenich69f4b512013-09-04 21:19:27 +0000912 }
913
John Kessenich4d65ee32016-03-12 18:17:47 -0700914 // Reflect
John Kessenich11f9fc72013-11-07 01:06:34 +0000915 if (Options & EOptionDumpReflection) {
916 program.buildReflection();
917 program.dumpReflection();
918 }
919
John Kessenich4d65ee32016-03-12 18:17:47 -0700920 // Dump SPIR-V
John Kessenich0df0cde2015-03-03 17:09:43 +0000921 if (Options & EOptionSpv) {
John Kessenich92f90382014-07-28 04:21:04 +0000922 if (CompileFailed || LinkFailed)
John Kessenich68d78fd2015-07-12 19:28:10 -0600923 printf("SPIR-V is not generated for failed compile or link\n");
John Kessenich92f90382014-07-28 04:21:04 +0000924 else {
925 for (int stage = 0; stage < EShLangCount; ++stage) {
John Kessenicha7a68a92014-08-24 18:21:00 +0000926 if (program.getIntermediate((EShLanguage)stage)) {
John Kessenich0df0cde2015-03-03 17:09:43 +0000927 std::vector<unsigned int> spirv;
Lei Zhang09caf122016-05-02 18:11:54 -0400928 std::string warningsErrors;
Lei Zhang17535f72016-05-04 15:55:59 -0400929 spv::SpvBuildLogger logger;
John Kessenich121853f2017-05-31 17:11:16 -0600930 glslang::SpvOptions spvOptions;
931 if (Options & EOptionDebug)
932 spvOptions.generateDebugInfo = true;
GregF52fe3d52017-09-28 10:08:32 -0600933 spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
934 spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
John Kessenich121853f2017-05-31 17:11:16 -0600935 glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
John Kessenichc57b2a92016-01-16 15:30:03 -0700936
937 // Dump the spv to a file or stdout, etc., but only if not doing
938 // memory/perf testing, as it's not internal to programmatic use.
939 if (! (Options & EOptionMemoryLeakMode)) {
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500940 printf("%s", logger.getAllMessages().c_str());
941 if (Options & EOptionOutputHexadecimal) {
John Kessenich8f674e82017-02-18 09:45:40 -0700942 glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
Johannes van Waverenecb0f3b2016-05-27 12:55:53 -0500943 } else {
944 glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
945 }
John Kessenichc57b2a92016-01-16 15:30:03 -0700946 if (Options & EOptionHumanReadableSpv) {
John Kessenichc57b2a92016-01-16 15:30:03 -0700947 spv::Disassemble(std::cout, spirv);
948 }
John Kessenichacba7722015-03-04 03:48:38 +0000949 }
John Kessenicha7a68a92014-08-24 18:21:00 +0000950 }
John Kessenich92f90382014-07-28 04:21:04 +0000951 }
952 }
953 }
954
John Kessenich5b0f13a2013-11-01 03:08:40 +0000955 // Free everything up, program has to go before the shaders
956 // because it might have merged stuff from the shaders, and
957 // the stuff from the shaders has to have its destructors called
958 // before the pools holding the memory in the shaders is freed.
959 delete &program;
John Kessenich69f4b512013-09-04 21:19:27 +0000960 while (shaders.size() > 0) {
961 delete shaders.back();
962 shaders.pop_back();
963 }
John Kessenich69f4b512013-09-04 21:19:27 +0000964}
965
John Kessenichc57b2a92016-01-16 15:30:03 -0700966//
967// Do file IO part of compile and link, handing off the pure
968// API/programmatic mode to CompileAndLinkShaderUnits(), which can
969// be put in a loop for testing memory footprint and performance.
970//
971// This is just for linking mode: meaning all the shaders will be put into the
972// the same program linked together.
973//
974// This means there are a limited number of work items (not multi-threading mode)
975// and that the point is testing at the linking level. Hence, to enable
976// performance and memory testing, the actual compile/link can be put in
977// a loop, independent of processing the work items and file IO.
978//
Juan Lopeza558b262017-04-02 23:04:00 +0200979void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
John Kessenichc57b2a92016-01-16 15:30:03 -0700980{
981 std::vector<ShaderCompUnit> compUnits;
982
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200983 // If this is using stdin, we can't really detect multiple different file
984 // units by input type. We need to assume that we're just being given one
985 // file of a certain type.
986 if ((Options & EOptionStdin) != 0) {
987 ShaderCompUnit compUnit(FindLanguage("stdin"));
988 std::istreambuf_iterator<char> begin(std::cin), end;
989 std::string tempString(begin, end);
990 char* fileText = strdup(tempString.c_str());
991 std::string fileName = "stdin";
992 compUnit.addString(fileName, fileText);
John Kessenichc57b2a92016-01-16 15:30:03 -0700993 compUnits.push_back(compUnit);
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +0200994 } else {
995 // Transfer all the work items from to a simple list of
996 // of compilation units. (We don't care about the thread
997 // work-item distribution properties in this path, which
998 // is okay due to the limited number of shaders, know since
999 // they are all getting linked together.)
1000 glslang::TWorkItem* workItem;
1001 while (Worklist.remove(workItem)) {
1002 ShaderCompUnit compUnit(FindLanguage(workItem->name));
1003 char* fileText = ReadFileData(workItem->name.c_str());
1004 if (fileText == nullptr)
1005 usage();
1006 compUnit.addString(workItem->name, fileText);
1007 compUnits.push_back(compUnit);
1008 }
John Kessenichc57b2a92016-01-16 15:30:03 -07001009 }
1010
1011 // Actual call to programmatic processing of compile and link,
1012 // in a loop for testing memory and performance. This part contains
1013 // all the perf/memory that a programmatic consumer will care about.
1014 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1015 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
1016 CompileAndLinkShaderUnits(compUnits);
1017
1018 if (Options & EOptionMemoryLeakMode)
1019 glslang::OS_DumpMemoryCounters();
1020 }
1021
John Kessenicha9313662017-06-15 10:40:49 -06001022 // free memory from ReadFileData, which got stored in a const char*
1023 // as the first string above
rdb32084e82016-02-23 22:17:38 +01001024 for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
John Kessenicha9313662017-06-15 10:40:49 -06001025 FreeFileData(const_cast<char*>(it->text[0]));
John Kessenichc57b2a92016-01-16 15:30:03 -07001026}
1027
John Kessenich94f28eb2017-11-13 01:32:06 -07001028int singleMain()
John Kessenicha0af4732012-12-12 21:15:54 +00001029{
Juan Lopeza558b262017-04-02 23:04:00 +02001030 glslang::TWorklist workList;
John Kessenich94f28eb2017-11-13 01:32:06 -07001031 std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
Juan Lopeza558b262017-04-02 23:04:00 +02001032 assert(item);
1033 workList.add(item.get());
1034 });
John Kessenich2b07c7e2013-07-31 18:44:13 +00001035
John Kessenich05a70632013-09-17 19:26:08 +00001036 if (Options & EOptionDumpConfig) {
Lei Zhang414eb602016-03-04 16:22:34 -05001037 printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
Juan Lopeza558b262017-04-02 23:04:00 +02001038 if (workList.empty())
John Kessenich05a70632013-09-17 19:26:08 +00001039 return ESuccess;
1040 }
1041
John Kessenich0da9eaa2015-08-01 17:10:02 -06001042 if (Options & EOptionDumpVersions) {
1043 printf("Glslang Version: %s %s\n", GLSLANG_REVISION, GLSLANG_DATE);
John Kessenich319de232013-12-04 04:43:40 +00001044 printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
1045 printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
John Kessenich68d78fd2015-07-12 19:28:10 -06001046 std::string spirvVersion;
1047 glslang::GetSpirvVersion(spirvVersion);
John Kessenich0da9eaa2015-08-01 17:10:02 -06001048 printf("SPIR-V Version %s\n", spirvVersion.c_str());
John Kessenich5e4b1242015-08-06 22:53:06 -06001049 printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
John Kessenich55e7d112015-11-15 21:33:39 -07001050 printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
John Kessenicha372a3e2017-11-02 22:32:14 -06001051 printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
John Kessenichb84313d2016-07-20 16:03:29 -06001052 printf("GL_KHR_vulkan_glsl version %d\n", 100);
1053 printf("ARB_GL_gl_spirv version %d\n", 100);
Juan Lopeza558b262017-04-02 23:04:00 +02001054 if (workList.empty())
John Kessenich319de232013-12-04 04:43:40 +00001055 return ESuccess;
1056 }
1057
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001058 if (workList.empty() && ((Options & EOptionStdin) == 0)) {
John Kessenich05a70632013-09-17 19:26:08 +00001059 usage();
John Kessenich05a70632013-09-17 19:26:08 +00001060 }
1061
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001062 if (Options & EOptionStdin) {
John Kessenich94f28eb2017-11-13 01:32:06 -07001063 WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
1064 workList.add(WorkItems.back().get());
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001065 }
1066
John Kessenich05a70632013-09-17 19:26:08 +00001067 ProcessConfigFile();
1068
John Kessenich69f4b512013-09-04 21:19:27 +00001069 //
1070 // Two modes:
John Kessenich38f3b892013-09-06 19:52:57 +00001071 // 1) linking all arguments together, single-threaded, new C++ interface
1072 // 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 +00001073 //
John Kessenichc555ddd2015-06-17 02:38:44 +00001074 if (Options & EOptionLinkProgram ||
1075 Options & EOptionOutputPreprocessed) {
John Kessenichc36e1d82013-11-01 17:41:52 +00001076 glslang::InitializeProcess();
John Kesseniche2c15b42017-11-16 22:48:41 -07001077 glslang::InitializeProcess(); // also test reference counting of users
1078 glslang::InitializeProcess(); // also test reference counting of users
1079 glslang::FinalizeProcess(); // also test reference counting of users
1080 glslang::FinalizeProcess(); // also test reference counting of users
Juan Lopeza558b262017-04-02 23:04:00 +02001081 CompileAndLinkShaderFiles(workList);
John Kessenichc36e1d82013-11-01 17:41:52 +00001082 glslang::FinalizeProcess();
1083 } else {
1084 ShInitialize();
John Kesseniche2c15b42017-11-16 22:48:41 -07001085 ShInitialize(); // also test reference counting of users
1086 ShFinalize(); // also test reference counting of users
John Kessenichc36e1d82013-11-01 17:41:52 +00001087
Juan Lopeza558b262017-04-02 23:04:00 +02001088 bool printShaderNames = workList.size() > 1;
John Kessenich69f4b512013-09-04 21:19:27 +00001089
John Kesseniche2c15b42017-11-16 22:48:41 -07001090 if (Options & EOptionMultiThreaded) {
Juan Lopeza558b262017-04-02 23:04:00 +02001091 std::array<std::thread, 16> threads;
John Kesseniche2c15b42017-11-16 22:48:41 -07001092 for (unsigned int t = 0; t < threads.size(); ++t) {
Juan Lopeza558b262017-04-02 23:04:00 +02001093 threads[t] = std::thread(CompileShaders, std::ref(workList));
John Kesseniche2c15b42017-11-16 22:48:41 -07001094 if (threads[t].get_id() == std::thread::id()) {
John Kessenichaf527992017-11-02 22:48:15 -06001095 fprintf(stderr, "Failed to create thread\n");
John Kessenich38f3b892013-09-06 19:52:57 +00001096 return EFailThreadCreate;
1097 }
John Kessenicha0af4732012-12-12 21:15:54 +00001098 }
Juan Lopeza558b262017-04-02 23:04:00 +02001099
1100 std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
John Kessenichc999ba22013-11-07 23:33:24 +00001101 } else
Juan Lopeza558b262017-04-02 23:04:00 +02001102 CompileShaders(workList);
John Kessenich38f3b892013-09-06 19:52:57 +00001103
1104 // Print out all the resulting infologs
John Kessenich94f28eb2017-11-13 01:32:06 -07001105 for (size_t w = 0; w < WorkItems.size(); ++w) {
1106 if (WorkItems[w]) {
1107 if (printShaderNames || WorkItems[w]->results.size() > 0)
1108 PutsIfNonEmpty(WorkItems[w]->name.c_str());
1109 PutsIfNonEmpty(WorkItems[w]->results.c_str());
John Kessenich38f3b892013-09-06 19:52:57 +00001110 }
1111 }
John Kessenichc36e1d82013-11-01 17:41:52 +00001112
1113 ShFinalize();
John Kessenicha0af4732012-12-12 21:15:54 +00001114 }
John Kessenich2b07c7e2013-07-31 18:44:13 +00001115
John Kessenichc999ba22013-11-07 23:33:24 +00001116 if (CompileFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001117 return EFailCompile;
John Kessenichc999ba22013-11-07 23:33:24 +00001118 if (LinkFailed)
John Kessenicha0af4732012-12-12 21:15:54 +00001119 return EFailLink;
1120
1121 return 0;
1122}
1123
John Kessenich94f28eb2017-11-13 01:32:06 -07001124int C_DECL main(int argc, char* argv[])
1125{
1126 ProcessArguments(WorkItems, argc, argv);
1127
1128 int ret = 0;
1129
1130 // Loop over the entire init/finalize cycle to watch memory changes
1131 const int iterations = 1;
1132 if (iterations > 1)
1133 glslang::OS_DumpMemoryCounters();
1134 for (int i = 0; i < iterations; ++i) {
1135 ret = singleMain();
1136 if (iterations > 1)
1137 glslang::OS_DumpMemoryCounters();
1138 }
1139
1140 return ret;
1141}
1142
John Kessenicha0af4732012-12-12 21:15:54 +00001143//
1144// Deduce the language from the filename. Files must end in one of the
1145// following extensions:
1146//
John Kessenich2b07c7e2013-07-31 18:44:13 +00001147// .vert = vertex
1148// .tesc = tessellation control
1149// .tese = tessellation evaluation
1150// .geom = geometry
1151// .frag = fragment
John Kessenich94a81fb2013-08-31 02:41:30 +00001152// .comp = compute
John Kessenicha0af4732012-12-12 21:15:54 +00001153//
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001154EShLanguage FindLanguage(const std::string& name, bool parseSuffix)
John Kessenicha0af4732012-12-12 21:15:54 +00001155{
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001156 size_t ext = 0;
John Kesseniche751bca2017-03-16 11:20:38 -06001157 std::string suffix;
steve-lunarg7f7c2ed2016-09-07 15:20:19 -06001158
Dan Bakerc6ede892016-08-11 14:06:06 -04001159 if (shaderStageName)
1160 suffix = shaderStageName;
John Kesseniche751bca2017-03-16 11:20:38 -06001161 else {
1162 // Search for a suffix on a filename: e.g, "myfile.frag". If given
1163 // the suffix directly, we skip looking for the '.'
1164 if (parseSuffix) {
1165 ext = name.rfind('.');
1166 if (ext == std::string::npos) {
1167 usage();
1168 return EShLangVertex;
1169 }
1170 ++ext;
1171 }
1172 suffix = name.substr(ext, std::string::npos);
1173 }
dankbaker45d49bc2016-08-08 21:43:07 -04001174
John Kessenich2b07c7e2013-07-31 18:44:13 +00001175 if (suffix == "vert")
1176 return EShLangVertex;
1177 else if (suffix == "tesc")
1178 return EShLangTessControl;
1179 else if (suffix == "tese")
1180 return EShLangTessEvaluation;
1181 else if (suffix == "geom")
1182 return EShLangGeometry;
1183 else if (suffix == "frag")
1184 return EShLangFragment;
John Kessenichc0275792013-08-09 17:14:49 +00001185 else if (suffix == "comp")
1186 return EShLangCompute;
John Kessenich2b07c7e2013-07-31 18:44:13 +00001187
1188 usage();
John Kesseniche95ecc52012-12-12 21:34:14 +00001189 return EShLangVertex;
John Kessenicha0af4732012-12-12 21:15:54 +00001190}
1191
John Kessenicha0af4732012-12-12 21:15:54 +00001192//
John Kessenichecba76f2017-01-06 00:34:48 -07001193// Read a file's data into a string, and compile it using the old interface ShCompile,
John Kessenich69f4b512013-09-04 21:19:27 +00001194// for non-linkable results.
John Kessenicha0af4732012-12-12 21:15:54 +00001195//
John Kessenich51cdd902014-02-18 23:37:57 +00001196void CompileFile(const char* fileName, ShHandle compiler)
John Kessenicha0af4732012-12-12 21:15:54 +00001197{
John Kessenichca3457f2015-05-18 01:59:45 +00001198 int ret = 0;
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001199 char* shaderString;
1200 if ((Options & EOptionStdin) != 0) {
1201 std::istreambuf_iterator<char> begin(std::cin), end;
1202 std::string tempString(begin, end);
1203 shaderString = strdup(tempString.c_str());
1204 } else {
1205 shaderString = ReadFileData(fileName);
1206 }
John Kessenich41cf6b52013-06-25 18:10:05 +00001207
1208 // move to length-based strings, rather than null-terminated strings
John Kessenich04acb1b2017-06-14 17:36:50 -06001209 int* lengths = new int[1];
1210 lengths[0] = (int)strlen(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001211
John Kessenich52ac67e2013-05-05 23:46:22 +00001212 EShMessages messages = EShMsgDefault;
John Kessenichb0a7eb52013-11-07 17:44:20 +00001213 SetMessageOptions(messages);
John Kessenichecba76f2017-01-06 00:34:48 -07001214
John Kessenicha9313662017-06-15 10:40:49 -06001215 if (UserPreamble.isSet())
1216 Error("-D and -U options require -l (linking)\n");
1217
John Kessenich94a81fb2013-08-31 02:41:30 +00001218 for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
1219 for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
John Kessenich927608b2017-01-06 12:34:14 -07001220 // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich04acb1b2017-06-14 17:36:50 -06001221 ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich927608b2017-01-06 12:34:14 -07001222 // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
John Kessenichecba76f2017-01-06 00:34:48 -07001223 // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
John Kessenichea869fb2013-10-28 18:12:06 +00001224 // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
John Kessenich927608b2017-01-06 12:34:14 -07001225 // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
1226 // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
John Kessenich41cf6b52013-06-25 18:10:05 +00001227 }
John Kessenicha0af4732012-12-12 21:15:54 +00001228
John Kessenich94a81fb2013-08-31 02:41:30 +00001229 if (Options & EOptionMemoryLeakMode)
John Kessenich2b07c7e2013-07-31 18:44:13 +00001230 glslang::OS_DumpMemoryCounters();
John Kessenicha0af4732012-12-12 21:15:54 +00001231 }
John Kessenicha0af4732012-12-12 21:15:54 +00001232
John Kessenich41cf6b52013-06-25 18:10:05 +00001233 delete [] lengths;
John Kessenich04acb1b2017-06-14 17:36:50 -06001234 FreeFileData(shaderString);
John Kessenicha0af4732012-12-12 21:15:54 +00001235
John Kessenichc999ba22013-11-07 23:33:24 +00001236 if (ret == 0)
1237 CompileFailed = true;
John Kessenicha0af4732012-12-12 21:15:54 +00001238}
1239
John Kessenicha0af4732012-12-12 21:15:54 +00001240//
1241// print usage to stdout
1242//
1243void usage()
1244{
John Kessenich319de232013-12-04 04:43:40 +00001245 printf("Usage: glslangValidator [option]... [file]...\n"
1246 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001247 "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
1248 " .conf to provide a config file that replaces the default configuration\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001249 " (see -c option below for generating a template)\n"
1250 " .vert for a vertex shader\n"
1251 " .tesc for a tessellation control shader\n"
1252 " .tese for a tessellation evaluation shader\n"
1253 " .geom for a geometry shader\n"
1254 " .frag for a fragment shader\n"
1255 " .comp for a compute shader\n"
John Kessenich319de232013-12-04 04:43:40 +00001256 "\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001257 "Options:\n"
1258 " -C cascading errors; risk crash from accumulation of error recoveries\n"
1259 " -D input is HLSL\n"
John Kessenicha9313662017-06-15 10:40:49 -06001260 " -D<macro=def>\n"
1261 " -D<macro> define a pre-processor macro\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001262 " -E print pre-processed GLSL; cannot be used with -l;\n"
1263 " errors will appear on stderr.\n"
John Kessenich6353d552017-06-23 11:11:09 -06001264 " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001265 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001266 " 'ver', when present, is the version of the input semantics,\n"
1267 " which will appear in #define GL_SPIRV ver\n"
1268 " '--client opengl100' is the same as -G100\n"
1269 " a '--target-env' for OpenGL will also imply '-G'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001270 " -H print human readable form of SPIR-V; turns on -V\n"
John Kessenich971a0a82017-06-07 15:06:58 -06001271 " -I<dir> add dir to the include search path; includer's directory\n"
1272 " is searched first, followed by left-to-right order of -I\n"
GregFcd1f1692017-09-21 18:40:22 -06001273 " -Od disables optimization. May cause illegal SPIR-V for HLSL.\n"
1274 " -Os optimizes SPIR-V to minimize size.\n"
John Kesseniche751bca2017-03-16 11:20:38 -06001275 " -S <stage> uses specified stage rather than parsing the file extension\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001276 " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
John Kessenich6353d552017-06-23 11:11:09 -06001277 " -U<macro> undefine a pre-processor macro\n"
1278 " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001279 " default file name is <stage>.spv (-o overrides this)\n"
John Kessenich6353d552017-06-23 11:11:09 -06001280 " 'ver', when present, is the version of the input semantics,\n"
1281 " which will appear in #define VULKAN ver\n"
1282 " '--client vulkan100' is the same as -V100\n"
1283 " a '--target-env' for Vulkan will also imply '-V'\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001284 " -c configuration dump;\n"
1285 " creates the default configuration file (redirect to a .conf file)\n"
1286 " -d default to desktop (#version 110) when there is no shader #version\n"
1287 " (default is ES version 100)\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001288 " -e <name> specify <name> as the entry-point name\n"
John Kessenich121853f2017-05-31 17:11:16 -06001289 " -g generate debug information\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001290 " -h print this usage message\n"
1291 " -i intermediate tree (glslang AST) is printed out\n"
1292 " -l link all input files together to form a single module\n"
1293 " -m memory leak mode\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001294 " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001295 " -q dump reflection query database\n"
John Kessenich2a271162017-07-20 20:00:36 -06001296 " -r synonym for --relaxed-errors\n"
John Kessenichb63f4a32017-11-16 22:32:20 -07001297 " -s silence syntax and semantic error reporting\n"
John Kessenich68d78fd2015-07-12 19:28:10 -06001298 " -t multi-threaded mode\n"
1299 " -v print version strings\n"
John Kessenich2a271162017-07-20 20:00:36 -06001300 " -w synonym for --suppress-warnings\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001301 " -x save binary output as text-based 32-bit hexadecimal numbers\n"
1302 " --auto-map-bindings automatically bind uniform variables\n"
1303 " without explicit bindings.\n"
1304 " --amb synonym for --auto-map-bindings\n"
1305 " --auto-map-locations automatically locate input/output lacking\n"
John Kessenichc178f0a2017-06-23 10:58:31 -06001306 " 'location' (fragile, not cross stage)\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001307 " --aml synonym for --auto-map-locations\n"
John Kessenich6353d552017-06-23 11:11:09 -06001308 " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001309 " --flatten-uniform-arrays flatten uniform texture/sampler arrays to\n"
1310 " scalars\n"
1311 " --fua synonym for --flatten-uniform-arrays\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001312 " --hlsl-offsets Allow block offsets to follow HLSL rules\n"
1313 " Works independently of source language\n"
1314 " --hlsl-iomap Perform IO mapping in HLSL register space\n"
1315 " --keep-uncalled don't eliminate uncalled functions\n"
1316 " --ku synonym for --keep-uncalled\n"
1317 " --no-storage-format use Unknown image format\n"
1318 " --nsf synonym for --no-storage-format\n"
John Kessenich2a271162017-07-20 20:00:36 -06001319 " --relaxed-errors relaxed GLSL semantic error-checking mode\n"
LoopDawg52017192017-07-14 15:15:47 -06001320 " --resource-set-binding [stage] name set binding\n"
1321 " Set descriptor set and binding for individual resources\n"
1322 " --resource-set-binding [stage] set\n"
1323 " Set descriptor set for all resources\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001324 " --rsb [stage] type set binding synonym for --resource-set-binding\n"
1325 " --shift-image-binding [stage] num base binding number for images (uav)\n"
LoopDawge5709552017-10-21 10:46:39 -06001326 " --shift-image-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001327 " --sib [stage] num synonym for --shift-image-binding\n"
1328 " --shift-sampler-binding [stage] num base binding number for samplers\n"
LoopDawge5709552017-10-21 10:46:39 -06001329 " --shift-sampler-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001330 " --ssb [stage] num synonym for --shift-sampler-binding\n"
1331 " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
LoopDawge5709552017-10-21 10:46:39 -06001332 " --shift-ssbo-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001333 " --sbb [stage] num synonym for --shift-ssbo-binding\n"
1334 " --shift-texture-binding [stage] num base binding number for textures\n"
LoopDawge5709552017-10-21 10:46:39 -06001335 " --shift-texture-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001336 " --stb [stage] num synonym for --shift-texture-binding\n"
1337 " --shift-uav-binding [stage] num base binding number for UAVs\n"
LoopDawge5709552017-10-21 10:46:39 -06001338 " --shift-uav-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001339 " --suavb [stage] num synonym for --shift-uav-binding\n"
1340 " --shift-UBO-binding [stage] num base binding number for UBOs\n"
LoopDawge5709552017-10-21 10:46:39 -06001341 " --shift-UBO-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001342 " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
LoopDawge5709552017-10-21 10:46:39 -06001343 " --shift-cbuffer-binding [stage] [num set]... per-descriptor-set shift values\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001344 " --sub [stage] num synonym for --shift-UBO-binding\n"
John Kessenicha25530c2017-09-11 20:13:49 -06001345 " --source-entrypoint <name> the given shader source function is\n"
1346 " renamed to be the <name> given in -e\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001347 " --sep synonym for --source-entrypoint\n"
Sven-Hendrik Haase0dd12852017-09-02 19:34:54 +02001348 " --stdin Read from stdin instead of from a file.\n"
1349 " You'll have to provide the shader stage\n"
1350 " using -S.\n"
John Kessenich2a271162017-07-20 20:00:36 -06001351 " --suppress-warnings suppress GLSL warnings\n"
1352 " (except as required by #extension : warn)\n"
1353 " --target-env {vulkan1.0|opengl} set the execution environment code will\n"
1354 " execute in (as opposed to language\n"
1355 " semantics selected by --client) defaults:\n"
1356 " 'vulkan1.0' under '--client vulkan<ver>'\n"
1357 " 'opengl' under '--client opengl<ver>'\n"
John Kessenich6263fb12017-06-14 15:52:44 -06001358 " --variable-name <name> Creates a C header file that contains a\n"
1359 " uint32_t array named <name>\n"
1360 " initialized with the shader binary code.\n"
1361 " --vn <name> synonym for --variable-name <name>\n"
John Kessenichb0a7eb52013-11-07 17:44:20 +00001362 );
John Kessenich68d78fd2015-07-12 19:28:10 -06001363
1364 exit(EFailUsage);
John Kessenicha0af4732012-12-12 21:15:54 +00001365}
1366
John Kessenich3ce4e592014-10-06 19:57:34 +00001367#if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
John Kessenichcfd643e2013-03-08 23:14:42 +00001368
1369#include <errno.h>
1370
1371int fopen_s(
1372 FILE** pFile,
John Kessenich51cdd902014-02-18 23:37:57 +00001373 const char* filename,
1374 const char* mode
John Kessenichcfd643e2013-03-08 23:14:42 +00001375)
1376{
1377 if (!pFile || !filename || !mode) {
1378 return EINVAL;
1379 }
1380
1381 FILE* f = fopen(filename, mode);
1382 if (! f) {
1383 if (errno != 0) {
1384 return errno;
1385 } else {
1386 return ENOENT;
1387 }
1388 }
1389 *pFile = f;
1390
1391 return 0;
1392}
1393
1394#endif
1395
John Kessenicha0af4732012-12-12 21:15:54 +00001396//
1397// Malloc a string of sufficient size and read a string into it.
1398//
John Kessenich04acb1b2017-06-14 17:36:50 -06001399char* ReadFileData(const char* fileName)
John Kessenicha0af4732012-12-12 21:15:54 +00001400{
John Kessenichb3297152015-07-11 18:01:03 -06001401 FILE *in = nullptr;
John Kessenich3ce4e592014-10-06 19:57:34 +00001402 int errorCode = fopen_s(&in, fileName, "r");
John Kessenich68d78fd2015-07-12 19:28:10 -06001403 if (errorCode || in == nullptr)
1404 Error("unable to open input file");
John Kessenichecba76f2017-01-06 00:34:48 -07001405
John Kessenich04acb1b2017-06-14 17:36:50 -06001406 int count = 0;
John Kessenicha0af4732012-12-12 21:15:54 +00001407 while (fgetc(in) != EOF)
1408 count++;
1409
John Kessenichd6c72a42014-08-18 19:42:35 +00001410 fseek(in, 0, SEEK_SET);
John Kessenichca3457f2015-05-18 01:59:45 +00001411
John Kessenich04acb1b2017-06-14 17:36:50 -06001412 char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
1413 if ((int)fread(return_data, 1, count, in) != count) {
1414 free(return_data);
John Kessenich68d78fd2015-07-12 19:28:10 -06001415 Error("can't read input file");
John Kessenicha0af4732012-12-12 21:15:54 +00001416 }
John Kessenich68d78fd2015-07-12 19:28:10 -06001417
John Kessenich04acb1b2017-06-14 17:36:50 -06001418 return_data[count] = '\0';
John Kessenicha0af4732012-12-12 21:15:54 +00001419 fclose(in);
John Kessenichb3297152015-07-11 18:01:03 -06001420
John Kessenicha0af4732012-12-12 21:15:54 +00001421 return return_data;
1422}
1423
John Kessenich04acb1b2017-06-14 17:36:50 -06001424void FreeFileData(char* data)
John Kessenicha0af4732012-12-12 21:15:54 +00001425{
John Kessenichb3297152015-07-11 18:01:03 -06001426 free(data);
John Kessenicha0af4732012-12-12 21:15:54 +00001427}
1428
John Kessenich54d8cda2013-02-11 22:36:01 +00001429void InfoLogMsg(const char* msg, const char* name, const int num)
John Kessenicha0af4732012-12-12 21:15:54 +00001430{
John Kessenichfae38ee2015-06-10 23:23:12 +00001431 if (num >= 0 )
1432 printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
1433 else
1434 printf("#### %s %s INFO LOG ####\n", msg, name);
John Kessenicha0af4732012-12-12 21:15:54 +00001435}