StandAlone.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. //
  2. // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
  3. // Copyright (C) 2013-2016 LunarG, Inc.
  4. //
  5. // 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. //
  36. // this only applies to the standalone wrapper, not the front end in general
  37. #ifndef _CRT_SECURE_NO_WARNINGS
  38. #define _CRT_SECURE_NO_WARNINGS
  39. #endif
  40. #include "ResourceLimits.h"
  41. #include "Worklist.h"
  42. #include "DirStackFileIncluder.h"
  43. #include "./../glslang/Include/ShHandle.h"
  44. #include "./../glslang/Include/revision.h"
  45. #include "./../glslang/Public/ShaderLang.h"
  46. #include "../SPIRV/GlslangToSpv.h"
  47. #include "../SPIRV/GLSL.std.450.h"
  48. #include "../SPIRV/doc.h"
  49. #include "../SPIRV/disassemble.h"
  50. #include <cstring>
  51. #include <cstdlib>
  52. #include <cctype>
  53. #include <cmath>
  54. #include <array>
  55. #include <map>
  56. #include <memory>
  57. #include <thread>
  58. #include "../glslang/OSDependent/osinclude.h"
  59. extern "C" {
  60. SH_IMPORT_EXPORT void ShOutputHtml();
  61. }
  62. // Command-line options
  63. enum TOptions {
  64. EOptionNone = 0,
  65. EOptionIntermediate = (1 << 0),
  66. EOptionSuppressInfolog = (1 << 1),
  67. EOptionMemoryLeakMode = (1 << 2),
  68. EOptionRelaxedErrors = (1 << 3),
  69. EOptionGiveWarnings = (1 << 4),
  70. EOptionLinkProgram = (1 << 5),
  71. EOptionMultiThreaded = (1 << 6),
  72. EOptionDumpConfig = (1 << 7),
  73. EOptionDumpReflection = (1 << 8),
  74. EOptionSuppressWarnings = (1 << 9),
  75. EOptionDumpVersions = (1 << 10),
  76. EOptionSpv = (1 << 11),
  77. EOptionHumanReadableSpv = (1 << 12),
  78. EOptionVulkanRules = (1 << 13),
  79. EOptionDefaultDesktop = (1 << 14),
  80. EOptionOutputPreprocessed = (1 << 15),
  81. EOptionOutputHexadecimal = (1 << 16),
  82. EOptionReadHlsl = (1 << 17),
  83. EOptionCascadingErrors = (1 << 18),
  84. EOptionAutoMapBindings = (1 << 19),
  85. EOptionFlattenUniformArrays = (1 << 20),
  86. EOptionNoStorageFormat = (1 << 21),
  87. EOptionKeepUncalled = (1 << 22),
  88. EOptionHlslOffsets = (1 << 23),
  89. EOptionHlslIoMapping = (1 << 24),
  90. EOptionAutoMapLocations = (1 << 25),
  91. EOptionDebug = (1 << 26),
  92. EOptionStdin = (1 << 27),
  93. EOptionOptimizeDisable = (1 << 28),
  94. EOptionOptimizeSize = (1 << 29),
  95. EOptionInvertY = (1 << 30),
  96. EOptionDumpBareVersion = (1 << 31),
  97. };
  98. bool targetHlslFunctionality1 = false;
  99. bool SpvToolsDisassembler = false;
  100. //
  101. // Return codes from main/exit().
  102. //
  103. enum TFailCode {
  104. ESuccess = 0,
  105. EFailUsage,
  106. EFailCompile,
  107. EFailLink,
  108. EFailCompilerCreate,
  109. EFailThreadCreate,
  110. EFailLinkerCreate
  111. };
  112. //
  113. // Forward declarations.
  114. //
  115. EShLanguage FindLanguage(const std::string& name, bool parseSuffix=true);
  116. void CompileFile(const char* fileName, ShHandle);
  117. void usage();
  118. char* ReadFileData(const char* fileName);
  119. void FreeFileData(char* data);
  120. void InfoLogMsg(const char* msg, const char* name, const int num);
  121. // Globally track if any compile or link failure.
  122. bool CompileFailed = false;
  123. bool LinkFailed = false;
  124. // array of unique places to leave the shader names and infologs for the asynchronous compiles
  125. std::vector<std::unique_ptr<glslang::TWorkItem>> WorkItems;
  126. TBuiltInResource Resources;
  127. std::string ConfigFile;
  128. //
  129. // Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource
  130. //
  131. void ProcessConfigFile()
  132. {
  133. if (ConfigFile.size() == 0)
  134. Resources = glslang::DefaultTBuiltInResource;
  135. else {
  136. char* configString = ReadFileData(ConfigFile.c_str());
  137. glslang::DecodeResourceLimits(&Resources, configString);
  138. FreeFileData(configString);
  139. }
  140. }
  141. int Options = 0;
  142. const char* ExecutableName = nullptr;
  143. const char* binaryFileName = nullptr;
  144. const char* entryPointName = nullptr;
  145. const char* sourceEntryPointName = nullptr;
  146. const char* shaderStageName = nullptr;
  147. const char* variableName = nullptr;
  148. bool HlslEnable16BitTypes = false;
  149. std::vector<std::string> IncludeDirectoryList;
  150. int ClientInputSemanticsVersion = 100; // maps to, say, #define VULKAN 100
  151. glslang::EShTargetClientVersion VulkanClientVersion =
  152. glslang::EShTargetVulkan_1_0; // would map to, say, Vulkan 1.0
  153. glslang::EShTargetClientVersion OpenGLClientVersion =
  154. glslang::EShTargetOpenGL_450; // doesn't influence anything yet, but maps to OpenGL 4.50
  155. glslang::EShTargetLanguageVersion TargetVersion =
  156. glslang::EShTargetSpv_1_0; // maps to, say, SPIR-V 1.0
  157. std::vector<std::string> Processes; // what should be recorded by OpModuleProcessed, or equivalent
  158. // Per descriptor-set binding base data
  159. typedef std::map<unsigned int, unsigned int> TPerSetBaseBinding;
  160. std::array<std::array<unsigned int, EShLangCount>, glslang::EResCount> baseBinding;
  161. std::array<std::array<TPerSetBaseBinding, EShLangCount>, glslang::EResCount> baseBindingForSet;
  162. std::array<std::vector<std::string>, EShLangCount> baseResourceSetBinding;
  163. // Add things like "#define ..." to a preamble to use in the beginning of the shader.
  164. class TPreamble {
  165. public:
  166. TPreamble() { }
  167. bool isSet() const { return text.size() > 0; }
  168. const char* get() const { return text.c_str(); }
  169. // #define...
  170. void addDef(std::string def)
  171. {
  172. text.append("#define ");
  173. fixLine(def);
  174. Processes.push_back("D");
  175. Processes.back().append(def);
  176. // The first "=" needs to turn into a space
  177. const size_t equal = def.find_first_of("=");
  178. if (equal != def.npos)
  179. def[equal] = ' ';
  180. text.append(def);
  181. text.append("\n");
  182. }
  183. // #undef...
  184. void addUndef(std::string undef)
  185. {
  186. text.append("#undef ");
  187. fixLine(undef);
  188. Processes.push_back("U");
  189. Processes.back().append(undef);
  190. text.append(undef);
  191. text.append("\n");
  192. }
  193. protected:
  194. void fixLine(std::string& line)
  195. {
  196. // Can't go past a newline in the line
  197. const size_t end = line.find_first_of("\n");
  198. if (end != line.npos)
  199. line = line.substr(0, end);
  200. }
  201. std::string text; // contents of preamble
  202. };
  203. TPreamble UserPreamble;
  204. //
  205. // Create the default name for saving a binary if -o is not provided.
  206. //
  207. const char* GetBinaryName(EShLanguage stage)
  208. {
  209. const char* name;
  210. if (binaryFileName == nullptr) {
  211. switch (stage) {
  212. case EShLangVertex: name = "vert.spv"; break;
  213. case EShLangTessControl: name = "tesc.spv"; break;
  214. case EShLangTessEvaluation: name = "tese.spv"; break;
  215. case EShLangGeometry: name = "geom.spv"; break;
  216. case EShLangFragment: name = "frag.spv"; break;
  217. case EShLangCompute: name = "comp.spv"; break;
  218. default: name = "unknown"; break;
  219. }
  220. } else
  221. name = binaryFileName;
  222. return name;
  223. }
  224. //
  225. // *.conf => this is a config file that can set limits/resources
  226. //
  227. bool SetConfigFile(const std::string& name)
  228. {
  229. if (name.size() < 5)
  230. return false;
  231. if (name.compare(name.size() - 5, 5, ".conf") == 0) {
  232. ConfigFile = name;
  233. return true;
  234. }
  235. return false;
  236. }
  237. //
  238. // Give error and exit with failure code.
  239. //
  240. void Error(const char* message)
  241. {
  242. fprintf(stderr, "%s: Error %s (use -h for usage)\n", ExecutableName, message);
  243. exit(EFailUsage);
  244. }
  245. //
  246. // Process an optional binding base of one the forms:
  247. // --argname [stage] base // base for stage (if given) or all stages (if not)
  248. // --argname [stage] [base set]... // set/base pairs: set the base for given binding set.
  249. // Where stage is one of the forms accepted by FindLanguage, and base is an integer
  250. //
  251. void ProcessBindingBase(int& argc, char**& argv, glslang::TResourceType res)
  252. {
  253. if (argc < 2)
  254. usage();
  255. EShLanguage lang = EShLangCount;
  256. int singleBase = 0;
  257. TPerSetBaseBinding perSetBase;
  258. int arg = 1;
  259. // Parse stage, if given
  260. if (!isdigit(argv[arg][0])) {
  261. if (argc < 3) // this form needs one more argument
  262. usage();
  263. lang = FindLanguage(argv[arg++], false);
  264. }
  265. if ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
  266. // Parse a per-set binding base
  267. while ((argc - arg) > 2 && isdigit(argv[arg+0][0]) && isdigit(argv[arg+1][0])) {
  268. const int baseNum = atoi(argv[arg++]);
  269. const int setNum = atoi(argv[arg++]);
  270. perSetBase[setNum] = baseNum;
  271. }
  272. } else {
  273. // Parse single binding base
  274. singleBase = atoi(argv[arg++]);
  275. }
  276. argc -= (arg-1);
  277. argv += (arg-1);
  278. // Set one or all languages
  279. const int langMin = (lang < EShLangCount) ? lang+0 : 0;
  280. const int langMax = (lang < EShLangCount) ? lang+1 : EShLangCount;
  281. for (int lang = langMin; lang < langMax; ++lang) {
  282. if (!perSetBase.empty())
  283. baseBindingForSet[res][lang] = perSetBase;
  284. else
  285. baseBinding[res][lang] = singleBase;
  286. }
  287. }
  288. void ProcessResourceSetBindingBase(int& argc, char**& argv, std::array<std::vector<std::string>, EShLangCount>& base)
  289. {
  290. if (argc < 2)
  291. usage();
  292. if (!isdigit(argv[1][0])) {
  293. if (argc < 3) // this form needs one more argument
  294. usage();
  295. // Parse form: --argname stage [regname set base...], or:
  296. // --argname stage set
  297. const EShLanguage lang = FindLanguage(argv[1], false);
  298. argc--;
  299. argv++;
  300. while (argc > 1 && argv[1] != nullptr && argv[1][0] != '-') {
  301. base[lang].push_back(argv[1]);
  302. argc--;
  303. argv++;
  304. }
  305. // Must have one arg, or a multiple of three (for [regname set binding] triples)
  306. if (base[lang].size() != 1 && (base[lang].size() % 3) != 0)
  307. usage();
  308. } else {
  309. // Parse form: --argname set
  310. for (int lang=0; lang<EShLangCount; ++lang)
  311. base[lang].push_back(argv[1]);
  312. argc--;
  313. argv++;
  314. }
  315. }
  316. //
  317. // Do all command-line argument parsing. This includes building up the work-items
  318. // to be processed later, and saving all the command-line options.
  319. //
  320. // Does not return (it exits) if command-line is fatally flawed.
  321. //
  322. void ProcessArguments(std::vector<std::unique_ptr<glslang::TWorkItem>>& workItems, int argc, char* argv[])
  323. {
  324. for (int res = 0; res < glslang::EResCount; ++res)
  325. baseBinding[res].fill(0);
  326. ExecutableName = argv[0];
  327. workItems.reserve(argc);
  328. const auto bumpArg = [&]() {
  329. if (argc > 0) {
  330. argc--;
  331. argv++;
  332. }
  333. };
  334. // read a string directly attached to a single-letter option
  335. const auto getStringOperand = [&](const char* desc) {
  336. if (argv[0][2] == 0) {
  337. printf("%s must immediately follow option (no spaces)\n", desc);
  338. exit(EFailUsage);
  339. }
  340. return argv[0] + 2;
  341. };
  342. // read a number attached to a single-letter option
  343. const auto getAttachedNumber = [&](const char* desc) {
  344. int num = atoi(argv[0] + 2);
  345. if (num == 0) {
  346. printf("%s: expected attached non-0 number\n", desc);
  347. exit(EFailUsage);
  348. }
  349. return num;
  350. };
  351. // minimum needed (without overriding something else) to target Vulkan SPIR-V
  352. const auto setVulkanSpv = []() {
  353. Options |= EOptionSpv;
  354. Options |= EOptionVulkanRules;
  355. Options |= EOptionLinkProgram;
  356. };
  357. // minimum needed (without overriding something else) to target OpenGL SPIR-V
  358. const auto setOpenGlSpv = []() {
  359. Options |= EOptionSpv;
  360. Options |= EOptionLinkProgram;
  361. // undo a -H default to Vulkan
  362. Options &= ~EOptionVulkanRules;
  363. };
  364. for (bumpArg(); argc >= 1; bumpArg()) {
  365. if (argv[0][0] == '-') {
  366. switch (argv[0][1]) {
  367. case '-':
  368. {
  369. std::string lowerword(argv[0]+2);
  370. std::transform(lowerword.begin(), lowerword.end(), lowerword.begin(), ::tolower);
  371. // handle --word style options
  372. if (lowerword == "auto-map-bindings" || // synonyms
  373. lowerword == "auto-map-binding" ||
  374. lowerword == "amb") {
  375. Options |= EOptionAutoMapBindings;
  376. } else if (lowerword == "auto-map-locations" || // synonyms
  377. lowerword == "aml") {
  378. Options |= EOptionAutoMapLocations;
  379. } else if (lowerword == "client") {
  380. if (argc > 1) {
  381. if (strcmp(argv[1], "vulkan100") == 0)
  382. setVulkanSpv();
  383. else if (strcmp(argv[1], "opengl100") == 0)
  384. setOpenGlSpv();
  385. else
  386. Error("--client expects vulkan100 or opengl100");
  387. }
  388. bumpArg();
  389. } else if (lowerword == "flatten-uniform-arrays" || // synonyms
  390. lowerword == "flatten-uniform-array" ||
  391. lowerword == "fua") {
  392. Options |= EOptionFlattenUniformArrays;
  393. } else if (lowerword == "hlsl-offsets") {
  394. Options |= EOptionHlslOffsets;
  395. } else if (lowerword == "hlsl-iomap" ||
  396. lowerword == "hlsl-iomapper" ||
  397. lowerword == "hlsl-iomapping") {
  398. Options |= EOptionHlslIoMapping;
  399. } else if (lowerword == "hlsl-enable-16bit-types") {
  400. HlslEnable16BitTypes = true;
  401. } else if (lowerword == "invert-y" || // synonyms
  402. lowerword == "iy") {
  403. Options |= EOptionInvertY;
  404. } else if (lowerword == "keep-uncalled" || // synonyms
  405. lowerword == "ku") {
  406. Options |= EOptionKeepUncalled;
  407. } else if (lowerword == "no-storage-format" || // synonyms
  408. lowerword == "nsf") {
  409. Options |= EOptionNoStorageFormat;
  410. } else if (lowerword == "relaxed-errors") {
  411. Options |= EOptionRelaxedErrors;
  412. } else if (lowerword == "resource-set-bindings" || // synonyms
  413. lowerword == "resource-set-binding" ||
  414. lowerword == "rsb") {
  415. ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
  416. } else if (lowerword == "shift-image-bindings" || // synonyms
  417. lowerword == "shift-image-binding" ||
  418. lowerword == "sib") {
  419. ProcessBindingBase(argc, argv, glslang::EResImage);
  420. } else if (lowerword == "shift-sampler-bindings" || // synonyms
  421. lowerword == "shift-sampler-binding" ||
  422. lowerword == "ssb") {
  423. ProcessBindingBase(argc, argv, glslang::EResSampler);
  424. } else if (lowerword == "shift-uav-bindings" || // synonyms
  425. lowerword == "shift-uav-binding" ||
  426. lowerword == "suavb") {
  427. ProcessBindingBase(argc, argv, glslang::EResUav);
  428. } else if (lowerword == "shift-texture-bindings" || // synonyms
  429. lowerword == "shift-texture-binding" ||
  430. lowerword == "stb") {
  431. ProcessBindingBase(argc, argv, glslang::EResTexture);
  432. } else if (lowerword == "shift-ubo-bindings" || // synonyms
  433. lowerword == "shift-ubo-binding" ||
  434. lowerword == "shift-cbuffer-bindings" ||
  435. lowerword == "shift-cbuffer-binding" ||
  436. lowerword == "sub" ||
  437. lowerword == "scb") {
  438. ProcessBindingBase(argc, argv, glslang::EResUbo);
  439. } else if (lowerword == "shift-ssbo-bindings" || // synonyms
  440. lowerword == "shift-ssbo-binding" ||
  441. lowerword == "sbb") {
  442. ProcessBindingBase(argc, argv, glslang::EResSsbo);
  443. } else if (lowerword == "source-entrypoint" || // synonyms
  444. lowerword == "sep") {
  445. if (argc <= 1)
  446. Error("no <entry-point> provided for --source-entrypoint");
  447. sourceEntryPointName = argv[1];
  448. bumpArg();
  449. break;
  450. } else if (lowerword == "spirv-dis") {
  451. SpvToolsDisassembler = true;
  452. } else if (lowerword == "stdin") {
  453. Options |= EOptionStdin;
  454. shaderStageName = argv[1];
  455. } else if (lowerword == "suppress-warnings") {
  456. Options |= EOptionSuppressWarnings;
  457. } else if (lowerword == "target-env") {
  458. if (argc > 1) {
  459. if (strcmp(argv[1], "vulkan1.0") == 0) {
  460. setVulkanSpv();
  461. VulkanClientVersion = glslang::EShTargetVulkan_1_0;
  462. } else if (strcmp(argv[1], "vulkan1.1") == 0) {
  463. setVulkanSpv();
  464. TargetVersion = glslang::EShTargetSpv_1_3;
  465. VulkanClientVersion = glslang::EShTargetVulkan_1_1;
  466. } else if (strcmp(argv[1], "opengl") == 0) {
  467. setOpenGlSpv();
  468. OpenGLClientVersion = glslang::EShTargetOpenGL_450;
  469. } else
  470. Error("--target-env expected vulkan1.0, vulkan1.1, or opengl");
  471. }
  472. bumpArg();
  473. } else if (lowerword == "variable-name" || // synonyms
  474. lowerword == "vn") {
  475. Options |= EOptionOutputHexadecimal;
  476. if (argc <= 1)
  477. Error("no <C-variable-name> provided for --variable-name");
  478. variableName = argv[1];
  479. bumpArg();
  480. break;
  481. } else if (lowerword == "version") {
  482. Options |= EOptionDumpVersions;
  483. } else {
  484. usage();
  485. }
  486. }
  487. break;
  488. case 'C':
  489. Options |= EOptionCascadingErrors;
  490. break;
  491. case 'D':
  492. if (argv[0][2] == 0)
  493. Options |= EOptionReadHlsl;
  494. else
  495. UserPreamble.addDef(getStringOperand("-D<macro> macro name"));
  496. break;
  497. case 'E':
  498. Options |= EOptionOutputPreprocessed;
  499. break;
  500. case 'G':
  501. // OpenGL Client
  502. setOpenGlSpv();
  503. if (argv[0][2] != 0)
  504. ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
  505. break;
  506. case 'H':
  507. Options |= EOptionHumanReadableSpv;
  508. if ((Options & EOptionSpv) == 0) {
  509. // default to Vulkan
  510. setVulkanSpv();
  511. }
  512. break;
  513. case 'I':
  514. IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
  515. break;
  516. case 'O':
  517. if (argv[0][2] == 'd')
  518. Options |= EOptionOptimizeDisable;
  519. else if (argv[0][2] == 's')
  520. #if ENABLE_OPT
  521. Options |= EOptionOptimizeSize;
  522. #else
  523. Error("-Os not available; optimizer not linked");
  524. #endif
  525. else
  526. Error("unknown -O option");
  527. break;
  528. case 'S':
  529. if (argc <= 1)
  530. Error("no <stage> specified for -S");
  531. shaderStageName = argv[1];
  532. bumpArg();
  533. break;
  534. case 'U':
  535. UserPreamble.addUndef(getStringOperand("-U<macro>: macro name"));
  536. break;
  537. case 'V':
  538. setVulkanSpv();
  539. if (argv[0][2] != 0)
  540. ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
  541. break;
  542. case 'c':
  543. Options |= EOptionDumpConfig;
  544. break;
  545. case 'd':
  546. if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
  547. strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
  548. Options |= EOptionDumpBareVersion;
  549. else
  550. Options |= EOptionDefaultDesktop;
  551. break;
  552. case 'e':
  553. // HLSL todo: entry point handle needs much more sophistication.
  554. // This is okay for one compilation unit with one entry point.
  555. entryPointName = argv[1];
  556. if (argc <= 1)
  557. Error("no <name> provided for -e");
  558. bumpArg();
  559. break;
  560. case 'f':
  561. if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
  562. targetHlslFunctionality1 = true;
  563. else
  564. Error("-f: expected hlsl_functionality1");
  565. break;
  566. case 'g':
  567. Options |= EOptionDebug;
  568. break;
  569. case 'h':
  570. usage();
  571. break;
  572. case 'i':
  573. Options |= EOptionIntermediate;
  574. break;
  575. case 'l':
  576. Options |= EOptionLinkProgram;
  577. break;
  578. case 'm':
  579. Options |= EOptionMemoryLeakMode;
  580. break;
  581. case 'o':
  582. if (argc <= 1)
  583. Error("no <file> provided for -o");
  584. binaryFileName = argv[1];
  585. bumpArg();
  586. break;
  587. case 'q':
  588. Options |= EOptionDumpReflection;
  589. break;
  590. case 'r':
  591. Options |= EOptionRelaxedErrors;
  592. break;
  593. case 's':
  594. Options |= EOptionSuppressInfolog;
  595. break;
  596. case 't':
  597. Options |= EOptionMultiThreaded;
  598. break;
  599. case 'v':
  600. Options |= EOptionDumpVersions;
  601. break;
  602. case 'w':
  603. Options |= EOptionSuppressWarnings;
  604. break;
  605. case 'x':
  606. Options |= EOptionOutputHexadecimal;
  607. break;
  608. default:
  609. usage();
  610. break;
  611. }
  612. } else {
  613. std::string name(argv[0]);
  614. if (! SetConfigFile(name)) {
  615. workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
  616. }
  617. }
  618. }
  619. // Make sure that -S is always specified if --stdin is specified
  620. if ((Options & EOptionStdin) && shaderStageName == nullptr)
  621. Error("must provide -S when --stdin is given");
  622. // Make sure that -E is not specified alongside linking (which includes SPV generation)
  623. if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
  624. Error("can't use -E when linking is selected");
  625. // -o or -x makes no sense if there is no target binary
  626. if (binaryFileName && (Options & EOptionSpv) == 0)
  627. Error("no binary generation requested (e.g., -V)");
  628. if ((Options & EOptionFlattenUniformArrays) != 0 &&
  629. (Options & EOptionReadHlsl) == 0)
  630. Error("uniform array flattening only valid when compiling HLSL source.");
  631. }
  632. //
  633. // Translate the meaningful subset of command-line options to parser-behavior options.
  634. //
  635. void SetMessageOptions(EShMessages& messages)
  636. {
  637. if (Options & EOptionRelaxedErrors)
  638. messages = (EShMessages)(messages | EShMsgRelaxedErrors);
  639. if (Options & EOptionIntermediate)
  640. messages = (EShMessages)(messages | EShMsgAST);
  641. if (Options & EOptionSuppressWarnings)
  642. messages = (EShMessages)(messages | EShMsgSuppressWarnings);
  643. if (Options & EOptionSpv)
  644. messages = (EShMessages)(messages | EShMsgSpvRules);
  645. if (Options & EOptionVulkanRules)
  646. messages = (EShMessages)(messages | EShMsgVulkanRules);
  647. if (Options & EOptionOutputPreprocessed)
  648. messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
  649. if (Options & EOptionReadHlsl)
  650. messages = (EShMessages)(messages | EShMsgReadHlsl);
  651. if (Options & EOptionCascadingErrors)
  652. messages = (EShMessages)(messages | EShMsgCascadingErrors);
  653. if (Options & EOptionKeepUncalled)
  654. messages = (EShMessages)(messages | EShMsgKeepUncalled);
  655. if (Options & EOptionHlslOffsets)
  656. messages = (EShMessages)(messages | EShMsgHlslOffsets);
  657. if (Options & EOptionDebug)
  658. messages = (EShMessages)(messages | EShMsgDebugInfo);
  659. if (HlslEnable16BitTypes)
  660. messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
  661. if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
  662. messages = (EShMessages)(messages | EShMsgHlslLegalization);
  663. }
  664. //
  665. // Thread entry point, for non-linking asynchronous mode.
  666. //
  667. void CompileShaders(glslang::TWorklist& worklist)
  668. {
  669. if (Options & EOptionDebug)
  670. Error("cannot generate debug information unless linking to generate code");
  671. glslang::TWorkItem* workItem;
  672. if (Options & EOptionStdin) {
  673. if (worklist.remove(workItem)) {
  674. ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
  675. if (compiler == nullptr)
  676. return;
  677. CompileFile("stdin", compiler);
  678. if (! (Options & EOptionSuppressInfolog))
  679. workItem->results = ShGetInfoLog(compiler);
  680. ShDestruct(compiler);
  681. }
  682. } else {
  683. while (worklist.remove(workItem)) {
  684. ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
  685. if (compiler == 0)
  686. return;
  687. CompileFile(workItem->name.c_str(), compiler);
  688. if (! (Options & EOptionSuppressInfolog))
  689. workItem->results = ShGetInfoLog(compiler);
  690. ShDestruct(compiler);
  691. }
  692. }
  693. }
  694. // Outputs the given string, but only if it is non-null and non-empty.
  695. // This prevents erroneous newlines from appearing.
  696. void PutsIfNonEmpty(const char* str)
  697. {
  698. if (str && str[0]) {
  699. puts(str);
  700. }
  701. }
  702. // Outputs the given string to stderr, but only if it is non-null and non-empty.
  703. // This prevents erroneous newlines from appearing.
  704. void StderrIfNonEmpty(const char* str)
  705. {
  706. if (str && str[0])
  707. fprintf(stderr, "%s\n", str);
  708. }
  709. // Simple bundling of what makes a compilation unit for ease in passing around,
  710. // and separation of handling file IO versus API (programmatic) compilation.
  711. struct ShaderCompUnit {
  712. EShLanguage stage;
  713. static const int maxCount = 1;
  714. int count; // live number of strings/names
  715. const char* text[maxCount]; // memory owned/managed externally
  716. std::string fileName[maxCount]; // hold's the memory, but...
  717. const char* fileNameList[maxCount]; // downstream interface wants pointers
  718. ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
  719. ShaderCompUnit(const ShaderCompUnit& rhs)
  720. {
  721. stage = rhs.stage;
  722. count = rhs.count;
  723. for (int i = 0; i < count; ++i) {
  724. fileName[i] = rhs.fileName[i];
  725. text[i] = rhs.text[i];
  726. fileNameList[i] = rhs.fileName[i].c_str();
  727. }
  728. }
  729. void addString(std::string& ifileName, const char* itext)
  730. {
  731. assert(count < maxCount);
  732. fileName[count] = ifileName;
  733. text[count] = itext;
  734. fileNameList[count] = fileName[count].c_str();
  735. ++count;
  736. }
  737. };
  738. //
  739. // For linking mode: Will independently parse each compilation unit, but then put them
  740. // in the same program and link them together, making at most one linked module per
  741. // pipeline stage.
  742. //
  743. // Uses the new C++ interface instead of the old handle-based interface.
  744. //
  745. void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
  746. {
  747. // keep track of what to free
  748. std::list<glslang::TShader*> shaders;
  749. EShMessages messages = EShMsgDefault;
  750. SetMessageOptions(messages);
  751. //
  752. // Per-shader processing...
  753. //
  754. glslang::TProgram& program = *new glslang::TProgram;
  755. for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
  756. const auto &compUnit = *it;
  757. glslang::TShader* shader = new glslang::TShader(compUnit.stage);
  758. shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
  759. if (entryPointName) // HLSL todo: this needs to be tracked per compUnits
  760. shader->setEntryPoint(entryPointName);
  761. if (sourceEntryPointName) {
  762. if (entryPointName == nullptr)
  763. printf("Warning: Changing source entry point name without setting an entry-point name.\n"
  764. "Use '-e <name>'.\n");
  765. shader->setSourceEntryPoint(sourceEntryPointName);
  766. }
  767. if (UserPreamble.isSet())
  768. shader->setPreamble(UserPreamble.get());
  769. shader->addProcesses(Processes);
  770. // Set IO mapper binding shift values
  771. for (int r = 0; r < glslang::EResCount; ++r) {
  772. const glslang::TResourceType res = glslang::TResourceType(r);
  773. // Set base bindings
  774. shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
  775. // Set bindings for particular resource sets
  776. // TODO: use a range based for loop here, when available in all environments.
  777. for (auto i = baseBindingForSet[res][compUnit.stage].begin();
  778. i != baseBindingForSet[res][compUnit.stage].end(); ++i)
  779. shader->setShiftBindingForSet(res, i->second, i->first);
  780. }
  781. shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
  782. shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
  783. shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
  784. if (Options & EOptionHlslIoMapping)
  785. shader->setHlslIoMapping(true);
  786. if (Options & EOptionAutoMapBindings)
  787. shader->setAutoMapBindings(true);
  788. if (Options & EOptionAutoMapLocations)
  789. shader->setAutoMapLocations(true);
  790. if (Options & EOptionInvertY)
  791. shader->setInvertY(true);
  792. // Set up the environment, some subsettings take precedence over earlier
  793. // ways of setting things.
  794. if (Options & EOptionSpv) {
  795. if (Options & EOptionVulkanRules) {
  796. shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
  797. : glslang::EShSourceGlsl,
  798. compUnit.stage, glslang::EShClientVulkan, ClientInputSemanticsVersion);
  799. shader->setEnvClient(glslang::EShClientVulkan, VulkanClientVersion);
  800. } else {
  801. shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
  802. : glslang::EShSourceGlsl,
  803. compUnit.stage, glslang::EShClientOpenGL, ClientInputSemanticsVersion);
  804. shader->setEnvClient(glslang::EShClientOpenGL, OpenGLClientVersion);
  805. }
  806. shader->setEnvTarget(glslang::EShTargetSpv, TargetVersion);
  807. if (targetHlslFunctionality1)
  808. shader->setEnvTargetHlslFunctionality1();
  809. }
  810. shaders.push_back(shader);
  811. const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
  812. DirStackFileIncluder includer;
  813. std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
  814. includer.pushExternalLocalDirectory(dir); });
  815. if (Options & EOptionOutputPreprocessed) {
  816. std::string str;
  817. if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
  818. messages, &str, includer)) {
  819. PutsIfNonEmpty(str.c_str());
  820. } else {
  821. CompileFailed = true;
  822. }
  823. StderrIfNonEmpty(shader->getInfoLog());
  824. StderrIfNonEmpty(shader->getInfoDebugLog());
  825. continue;
  826. }
  827. if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
  828. CompileFailed = true;
  829. program.addShader(shader);
  830. if (! (Options & EOptionSuppressInfolog) &&
  831. ! (Options & EOptionMemoryLeakMode)) {
  832. PutsIfNonEmpty(compUnit.fileName[0].c_str());
  833. PutsIfNonEmpty(shader->getInfoLog());
  834. PutsIfNonEmpty(shader->getInfoDebugLog());
  835. }
  836. }
  837. //
  838. // Program-level processing...
  839. //
  840. // Link
  841. if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
  842. LinkFailed = true;
  843. // Map IO
  844. if (Options & EOptionSpv) {
  845. if (!program.mapIO())
  846. LinkFailed = true;
  847. }
  848. // Report
  849. if (! (Options & EOptionSuppressInfolog) &&
  850. ! (Options & EOptionMemoryLeakMode)) {
  851. PutsIfNonEmpty(program.getInfoLog());
  852. PutsIfNonEmpty(program.getInfoDebugLog());
  853. }
  854. // Reflect
  855. if (Options & EOptionDumpReflection) {
  856. program.buildReflection();
  857. program.dumpReflection();
  858. }
  859. // Dump SPIR-V
  860. if (Options & EOptionSpv) {
  861. if (CompileFailed || LinkFailed)
  862. printf("SPIR-V is not generated for failed compile or link\n");
  863. else {
  864. for (int stage = 0; stage < EShLangCount; ++stage) {
  865. if (program.getIntermediate((EShLanguage)stage)) {
  866. std::vector<unsigned int> spirv;
  867. std::string warningsErrors;
  868. spv::SpvBuildLogger logger;
  869. glslang::SpvOptions spvOptions;
  870. if (Options & EOptionDebug)
  871. spvOptions.generateDebugInfo = true;
  872. spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
  873. spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
  874. glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
  875. // Dump the spv to a file or stdout, etc., but only if not doing
  876. // memory/perf testing, as it's not internal to programmatic use.
  877. if (! (Options & EOptionMemoryLeakMode)) {
  878. printf("%s", logger.getAllMessages().c_str());
  879. if (Options & EOptionOutputHexadecimal) {
  880. glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
  881. } else {
  882. glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
  883. }
  884. #if ENABLE_OPT
  885. if (SpvToolsDisassembler)
  886. spv::SpirvToolsDisassemble(std::cout, spirv);
  887. #else
  888. if (SpvToolsDisassembler)
  889. printf("SPIRV-Tools is not enabled; use -H for human readable SPIR-V\n");
  890. #endif
  891. if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
  892. spv::Disassemble(std::cout, spirv);
  893. }
  894. }
  895. }
  896. }
  897. }
  898. // Free everything up, program has to go before the shaders
  899. // because it might have merged stuff from the shaders, and
  900. // the stuff from the shaders has to have its destructors called
  901. // before the pools holding the memory in the shaders is freed.
  902. delete &program;
  903. while (shaders.size() > 0) {
  904. delete shaders.back();
  905. shaders.pop_back();
  906. }
  907. }
  908. //
  909. // Do file IO part of compile and link, handing off the pure
  910. // API/programmatic mode to CompileAndLinkShaderUnits(), which can
  911. // be put in a loop for testing memory footprint and performance.
  912. //
  913. // This is just for linking mode: meaning all the shaders will be put into the
  914. // the same program linked together.
  915. //
  916. // This means there are a limited number of work items (not multi-threading mode)
  917. // and that the point is testing at the linking level. Hence, to enable
  918. // performance and memory testing, the actual compile/link can be put in
  919. // a loop, independent of processing the work items and file IO.
  920. //
  921. void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
  922. {
  923. std::vector<ShaderCompUnit> compUnits;
  924. // If this is using stdin, we can't really detect multiple different file
  925. // units by input type. We need to assume that we're just being given one
  926. // file of a certain type.
  927. if ((Options & EOptionStdin) != 0) {
  928. ShaderCompUnit compUnit(FindLanguage("stdin"));
  929. std::istreambuf_iterator<char> begin(std::cin), end;
  930. std::string tempString(begin, end);
  931. char* fileText = strdup(tempString.c_str());
  932. std::string fileName = "stdin";
  933. compUnit.addString(fileName, fileText);
  934. compUnits.push_back(compUnit);
  935. } else {
  936. // Transfer all the work items from to a simple list of
  937. // of compilation units. (We don't care about the thread
  938. // work-item distribution properties in this path, which
  939. // is okay due to the limited number of shaders, know since
  940. // they are all getting linked together.)
  941. glslang::TWorkItem* workItem;
  942. while (Worklist.remove(workItem)) {
  943. ShaderCompUnit compUnit(FindLanguage(workItem->name));
  944. char* fileText = ReadFileData(workItem->name.c_str());
  945. if (fileText == nullptr)
  946. usage();
  947. compUnit.addString(workItem->name, fileText);
  948. compUnits.push_back(compUnit);
  949. }
  950. }
  951. // Actual call to programmatic processing of compile and link,
  952. // in a loop for testing memory and performance. This part contains
  953. // all the perf/memory that a programmatic consumer will care about.
  954. for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
  955. for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
  956. CompileAndLinkShaderUnits(compUnits);
  957. if (Options & EOptionMemoryLeakMode)
  958. glslang::OS_DumpMemoryCounters();
  959. }
  960. // free memory from ReadFileData, which got stored in a const char*
  961. // as the first string above
  962. for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
  963. FreeFileData(const_cast<char*>(it->text[0]));
  964. }
  965. int singleMain()
  966. {
  967. glslang::TWorklist workList;
  968. std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
  969. assert(item);
  970. workList.add(item.get());
  971. });
  972. if (Options & EOptionDumpConfig) {
  973. printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
  974. if (workList.empty())
  975. return ESuccess;
  976. }
  977. if (Options & EOptionDumpBareVersion) {
  978. printf("%d.%d.%d\n",
  979. glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
  980. if (workList.empty())
  981. return ESuccess;
  982. } else if (Options & EOptionDumpVersions) {
  983. printf("Glslang Version: %d.%d.%d\n",
  984. glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
  985. printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
  986. printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
  987. std::string spirvVersion;
  988. glslang::GetSpirvVersion(spirvVersion);
  989. printf("SPIR-V Version %s\n", spirvVersion.c_str());
  990. printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
  991. printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
  992. printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
  993. printf("GL_KHR_vulkan_glsl version %d\n", 100);
  994. printf("ARB_GL_gl_spirv version %d\n", 100);
  995. if (workList.empty())
  996. return ESuccess;
  997. }
  998. if (workList.empty() && ((Options & EOptionStdin) == 0)) {
  999. usage();
  1000. }
  1001. if (Options & EOptionStdin) {
  1002. WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
  1003. workList.add(WorkItems.back().get());
  1004. }
  1005. ProcessConfigFile();
  1006. //
  1007. // Two modes:
  1008. // 1) linking all arguments together, single-threaded, new C++ interface
  1009. // 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface
  1010. //
  1011. if (Options & EOptionLinkProgram ||
  1012. Options & EOptionOutputPreprocessed) {
  1013. glslang::InitializeProcess();
  1014. glslang::InitializeProcess(); // also test reference counting of users
  1015. glslang::InitializeProcess(); // also test reference counting of users
  1016. glslang::FinalizeProcess(); // also test reference counting of users
  1017. glslang::FinalizeProcess(); // also test reference counting of users
  1018. CompileAndLinkShaderFiles(workList);
  1019. glslang::FinalizeProcess();
  1020. } else {
  1021. ShInitialize();
  1022. ShInitialize(); // also test reference counting of users
  1023. ShFinalize(); // also test reference counting of users
  1024. bool printShaderNames = workList.size() > 1;
  1025. if (Options & EOptionMultiThreaded) {
  1026. std::array<std::thread, 16> threads;
  1027. for (unsigned int t = 0; t < threads.size(); ++t) {
  1028. threads[t] = std::thread(CompileShaders, std::ref(workList));
  1029. if (threads[t].get_id() == std::thread::id()) {
  1030. fprintf(stderr, "Failed to create thread\n");
  1031. return EFailThreadCreate;
  1032. }
  1033. }
  1034. std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
  1035. } else
  1036. CompileShaders(workList);
  1037. // Print out all the resulting infologs
  1038. for (size_t w = 0; w < WorkItems.size(); ++w) {
  1039. if (WorkItems[w]) {
  1040. if (printShaderNames || WorkItems[w]->results.size() > 0)
  1041. PutsIfNonEmpty(WorkItems[w]->name.c_str());
  1042. PutsIfNonEmpty(WorkItems[w]->results.c_str());
  1043. }
  1044. }
  1045. ShFinalize();
  1046. }
  1047. if (CompileFailed)
  1048. return EFailCompile;
  1049. if (LinkFailed)
  1050. return EFailLink;
  1051. return 0;
  1052. }
  1053. int C_DECL main(int argc, char* argv[])
  1054. {
  1055. ProcessArguments(WorkItems, argc, argv);
  1056. int ret = 0;
  1057. // Loop over the entire init/finalize cycle to watch memory changes
  1058. const int iterations = 1;
  1059. if (iterations > 1)
  1060. glslang::OS_DumpMemoryCounters();
  1061. for (int i = 0; i < iterations; ++i) {
  1062. ret = singleMain();
  1063. if (iterations > 1)
  1064. glslang::OS_DumpMemoryCounters();
  1065. }
  1066. return ret;
  1067. }
  1068. //
  1069. // Deduce the language from the filename. Files must end in one of the
  1070. // following extensions:
  1071. //
  1072. // .vert = vertex
  1073. // .tesc = tessellation control
  1074. // .tese = tessellation evaluation
  1075. // .geom = geometry
  1076. // .frag = fragment
  1077. // .comp = compute
  1078. //
  1079. // Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
  1080. // where <stage> is one of the stages listed above.
  1081. //
  1082. EShLanguage FindLanguage(const std::string& name, bool parseStageName)
  1083. {
  1084. std::string stageName;
  1085. if (shaderStageName)
  1086. stageName = shaderStageName;
  1087. else if (parseStageName) {
  1088. // Note: "first" extension means "first from the end", i.e.
  1089. // if the file is named foo.vert.glsl, then "glsl" is first,
  1090. // "vert" is second.
  1091. size_t firstExtStart = name.find_last_of(".");
  1092. bool hasFirstExt = firstExtStart != std::string::npos;
  1093. size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
  1094. bool hasSecondExt = secondExtStart != std::string::npos;
  1095. std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
  1096. bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
  1097. if (usesUnifiedExt && firstExt == "hlsl")
  1098. Options |= EOptionReadHlsl;
  1099. if (hasFirstExt && !usesUnifiedExt)
  1100. stageName = firstExt;
  1101. else if (usesUnifiedExt && hasSecondExt)
  1102. stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
  1103. else {
  1104. usage();
  1105. return EShLangVertex;
  1106. }
  1107. } else
  1108. stageName = name;
  1109. if (stageName == "vert")
  1110. return EShLangVertex;
  1111. else if (stageName == "tesc")
  1112. return EShLangTessControl;
  1113. else if (stageName == "tese")
  1114. return EShLangTessEvaluation;
  1115. else if (stageName == "geom")
  1116. return EShLangGeometry;
  1117. else if (stageName == "frag")
  1118. return EShLangFragment;
  1119. else if (stageName == "comp")
  1120. return EShLangCompute;
  1121. usage();
  1122. return EShLangVertex;
  1123. }
  1124. //
  1125. // Read a file's data into a string, and compile it using the old interface ShCompile,
  1126. // for non-linkable results.
  1127. //
  1128. void CompileFile(const char* fileName, ShHandle compiler)
  1129. {
  1130. int ret = 0;
  1131. char* shaderString;
  1132. if ((Options & EOptionStdin) != 0) {
  1133. std::istreambuf_iterator<char> begin(std::cin), end;
  1134. std::string tempString(begin, end);
  1135. shaderString = strdup(tempString.c_str());
  1136. } else {
  1137. shaderString = ReadFileData(fileName);
  1138. }
  1139. // move to length-based strings, rather than null-terminated strings
  1140. int* lengths = new int[1];
  1141. lengths[0] = (int)strlen(shaderString);
  1142. EShMessages messages = EShMsgDefault;
  1143. SetMessageOptions(messages);
  1144. if (UserPreamble.isSet())
  1145. Error("-D and -U options require -l (linking)\n");
  1146. for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
  1147. for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
  1148. // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
  1149. ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
  1150. // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
  1151. // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
  1152. // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
  1153. // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
  1154. // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
  1155. }
  1156. if (Options & EOptionMemoryLeakMode)
  1157. glslang::OS_DumpMemoryCounters();
  1158. }
  1159. delete [] lengths;
  1160. FreeFileData(shaderString);
  1161. if (ret == 0)
  1162. CompileFailed = true;
  1163. }
  1164. //
  1165. // print usage to stdout
  1166. //
  1167. void usage()
  1168. {
  1169. printf("Usage: glslangValidator [option]... [file]...\n"
  1170. "\n"
  1171. "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
  1172. " .conf to provide a config file that replaces the default configuration\n"
  1173. " (see -c option below for generating a template)\n"
  1174. " .vert for a vertex shader\n"
  1175. " .tesc for a tessellation control shader\n"
  1176. " .tese for a tessellation evaluation shader\n"
  1177. " .geom for a geometry shader\n"
  1178. " .frag for a fragment shader\n"
  1179. " .comp for a compute shader\n"
  1180. " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
  1181. " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
  1182. "\n"
  1183. "Options:\n"
  1184. " -C cascading errors; risk crash from accumulation of error recoveries\n"
  1185. " -D input is HLSL (default when any suffix is .hlsl)\n"
  1186. " -D<macro=def>\n"
  1187. " -D<macro> define a pre-processor macro\n"
  1188. " -E print pre-processed GLSL; cannot be used with -l;\n"
  1189. " errors will appear on stderr.\n"
  1190. " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
  1191. " default file name is <stage>.spv (-o overrides this)\n"
  1192. " 'ver', when present, is the version of the input semantics,\n"
  1193. " which will appear in #define GL_SPIRV ver\n"
  1194. " '--client opengl100' is the same as -G100\n"
  1195. " a '--target-env' for OpenGL will also imply '-G'\n"
  1196. " -H print human readable form of SPIR-V; turns on -V\n"
  1197. " -I<dir> add dir to the include search path; includer's directory\n"
  1198. " is searched first, followed by left-to-right order of -I\n"
  1199. " -Od disables optimization. May cause illegal SPIR-V for HLSL.\n"
  1200. " -Os optimizes SPIR-V to minimize size.\n"
  1201. " -S <stage> uses specified stage rather than parsing the file extension\n"
  1202. " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
  1203. " -U<macro> undefine a pre-processor macro\n"
  1204. " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
  1205. " default file name is <stage>.spv (-o overrides this)\n"
  1206. " 'ver', when present, is the version of the input semantics,\n"
  1207. " which will appear in #define VULKAN ver\n"
  1208. " '--client vulkan100' is the same as -V100\n"
  1209. " a '--target-env' for Vulkan will also imply '-V'\n"
  1210. " -c configuration dump;\n"
  1211. " creates the default configuration file (redirect to a .conf file)\n"
  1212. " -d default to desktop (#version 110) when there is no shader #version\n"
  1213. " (default is ES version 100)\n"
  1214. " -e <name> specify <name> as the entry-point name\n"
  1215. " -f{hlsl_functionality1}\n"
  1216. " 'hlsl_functionality1' enables use of the\n"
  1217. " SPV_GOOGLE_hlsl_functionality1 extension\n"
  1218. " -g generate debug information\n"
  1219. " -h print this usage message\n"
  1220. " -i intermediate tree (glslang AST) is printed out\n"
  1221. " -l link all input files together to form a single module\n"
  1222. " -m memory leak mode\n"
  1223. " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
  1224. " -q dump reflection query database\n"
  1225. " -r synonym for --relaxed-errors\n"
  1226. " -s silence syntax and semantic error reporting\n"
  1227. " -t multi-threaded mode\n"
  1228. " -v print version strings\n"
  1229. " -w synonym for --suppress-warnings\n"
  1230. " -x save binary output as text-based 32-bit hexadecimal numbers\n"
  1231. " --auto-map-bindings automatically bind uniform variables\n"
  1232. " without explicit bindings.\n"
  1233. " --amb synonym for --auto-map-bindings\n"
  1234. " --auto-map-locations automatically locate input/output lacking\n"
  1235. " 'location' (fragile, not cross stage)\n"
  1236. " --aml synonym for --auto-map-locations\n"
  1237. " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
  1238. " -dumpfullversion print bare major.minor.patchlevel\n"
  1239. " -dumpversion same as -dumpfullversion\n"
  1240. " --flatten-uniform-arrays flatten uniform texture/sampler arrays to\n"
  1241. " scalars\n"
  1242. " --fua synonym for --flatten-uniform-arrays\n"
  1243. " --hlsl-offsets Allow block offsets to follow HLSL rules\n"
  1244. " Works independently of source language\n"
  1245. " --hlsl-iomap Perform IO mapping in HLSL register space\n"
  1246. " --hlsl-enable-16bit-types Allow use of 16-bit types in SPIR-V for HLSL\n"
  1247. " --invert-y | --iy invert position.Y output in vertex shader\n"
  1248. " --keep-uncalled don't eliminate uncalled functions\n"
  1249. " --ku synonym for --keep-uncalled\n"
  1250. " --no-storage-format use Unknown image format\n"
  1251. " --nsf synonym for --no-storage-format\n"
  1252. " --relaxed-errors relaxed GLSL semantic error-checking mode\n"
  1253. " --resource-set-binding [stage] name set binding\n"
  1254. " Set descriptor set and binding for individual resources\n"
  1255. " --resource-set-binding [stage] set\n"
  1256. " Set descriptor set for all resources\n"
  1257. " --rsb [stage] type set binding synonym for --resource-set-binding\n"
  1258. " --shift-image-binding [stage] num base binding number for images (uav)\n"
  1259. " --shift-image-binding [stage] [num set]... per-descriptor-set shift values\n"
  1260. " --sib [stage] num synonym for --shift-image-binding\n"
  1261. " --shift-sampler-binding [stage] num base binding number for samplers\n"
  1262. " --shift-sampler-binding [stage] [num set]... per-descriptor-set shift values\n"
  1263. " --ssb [stage] num synonym for --shift-sampler-binding\n"
  1264. " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
  1265. " --shift-ssbo-binding [stage] [num set]... per-descriptor-set shift values\n"
  1266. " --sbb [stage] num synonym for --shift-ssbo-binding\n"
  1267. " --shift-texture-binding [stage] num base binding number for textures\n"
  1268. " --shift-texture-binding [stage] [num set]... per-descriptor-set shift values\n"
  1269. " --stb [stage] num synonym for --shift-texture-binding\n"
  1270. " --shift-uav-binding [stage] num base binding number for UAVs\n"
  1271. " --shift-uav-binding [stage] [num set]... per-descriptor-set shift values\n"
  1272. " --suavb [stage] num synonym for --shift-uav-binding\n"
  1273. " --shift-UBO-binding [stage] num base binding number for UBOs\n"
  1274. " --shift-UBO-binding [stage] [num set]... per-descriptor-set shift values\n"
  1275. " --shift-cbuffer-binding [stage] num synonym for --shift-UBO-binding\n"
  1276. " --shift-cbuffer-binding [stage] [num set]... per-descriptor-set shift values\n"
  1277. " --spirv-dis output standard form disassembly; works only\n"
  1278. " when a SPIR-V generation option is also used\n"
  1279. " --sub [stage] num synonym for --shift-UBO-binding\n"
  1280. " --source-entrypoint <name> the given shader source function is\n"
  1281. " renamed to be the <name> given in -e\n"
  1282. " --sep synonym for --source-entrypoint\n"
  1283. " --stdin Read from stdin instead of from a file.\n"
  1284. " You'll have to provide the shader stage\n"
  1285. " using -S.\n"
  1286. " --suppress-warnings suppress GLSL warnings\n"
  1287. " (except as required by #extension : warn)\n"
  1288. " --target-env {vulkan1.0 | vulkan1.1 | opengl} \n"
  1289. " set execution environment that emitted code\n"
  1290. " will execute in (as opposed to the language\n"
  1291. " semantics selected by --client) defaults:\n"
  1292. " 'vulkan1.0' under '--client vulkan<ver>'\n"
  1293. " 'opengl' under '--client opengl<ver>'\n"
  1294. " --variable-name <name> Creates a C header file that contains a\n"
  1295. " uint32_t array named <name>\n"
  1296. " initialized with the shader binary code.\n"
  1297. " --version synonym for -v\n"
  1298. " --vn <name> synonym for --variable-name <name>\n"
  1299. );
  1300. exit(EFailUsage);
  1301. }
  1302. #if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
  1303. #include <errno.h>
  1304. int fopen_s(
  1305. FILE** pFile,
  1306. const char* filename,
  1307. const char* mode
  1308. )
  1309. {
  1310. if (!pFile || !filename || !mode) {
  1311. return EINVAL;
  1312. }
  1313. FILE* f = fopen(filename, mode);
  1314. if (! f) {
  1315. if (errno != 0) {
  1316. return errno;
  1317. } else {
  1318. return ENOENT;
  1319. }
  1320. }
  1321. *pFile = f;
  1322. return 0;
  1323. }
  1324. #endif
  1325. //
  1326. // Malloc a string of sufficient size and read a string into it.
  1327. //
  1328. char* ReadFileData(const char* fileName)
  1329. {
  1330. FILE *in = nullptr;
  1331. int errorCode = fopen_s(&in, fileName, "r");
  1332. if (errorCode || in == nullptr)
  1333. Error("unable to open input file");
  1334. int count = 0;
  1335. while (fgetc(in) != EOF)
  1336. count++;
  1337. fseek(in, 0, SEEK_SET);
  1338. char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
  1339. if ((int)fread(return_data, 1, count, in) != count) {
  1340. free(return_data);
  1341. Error("can't read input file");
  1342. }
  1343. return_data[count] = '\0';
  1344. fclose(in);
  1345. return return_data;
  1346. }
  1347. void FreeFileData(char* data)
  1348. {
  1349. free(data);
  1350. }
  1351. void InfoLogMsg(const char* msg, const char* name, const int num)
  1352. {
  1353. if (num >= 0 )
  1354. printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
  1355. else
  1356. printf("#### %s %s INFO LOG ####\n", msg, name);
  1357. }