StandAlone.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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].insert(perSetBase.begin(), perSetBase.end());
  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 == "entry-point") {
  390. entryPointName = argv[1];
  391. if (argc <= 1)
  392. Error("no <name> provided for --entry-point");
  393. bumpArg();
  394. } else if (lowerword == "flatten-uniform-arrays" || // synonyms
  395. lowerword == "flatten-uniform-array" ||
  396. lowerword == "fua") {
  397. Options |= EOptionFlattenUniformArrays;
  398. } else if (lowerword == "hlsl-offsets") {
  399. Options |= EOptionHlslOffsets;
  400. } else if (lowerword == "hlsl-iomap" ||
  401. lowerword == "hlsl-iomapper" ||
  402. lowerword == "hlsl-iomapping") {
  403. Options |= EOptionHlslIoMapping;
  404. } else if (lowerword == "hlsl-enable-16bit-types") {
  405. HlslEnable16BitTypes = true;
  406. } else if (lowerword == "invert-y" || // synonyms
  407. lowerword == "iy") {
  408. Options |= EOptionInvertY;
  409. } else if (lowerword == "keep-uncalled" || // synonyms
  410. lowerword == "ku") {
  411. Options |= EOptionKeepUncalled;
  412. } else if (lowerword == "no-storage-format" || // synonyms
  413. lowerword == "nsf") {
  414. Options |= EOptionNoStorageFormat;
  415. } else if (lowerword == "relaxed-errors") {
  416. Options |= EOptionRelaxedErrors;
  417. } else if (lowerword == "resource-set-bindings" || // synonyms
  418. lowerword == "resource-set-binding" ||
  419. lowerword == "rsb") {
  420. ProcessResourceSetBindingBase(argc, argv, baseResourceSetBinding);
  421. } else if (lowerword == "shift-image-bindings" || // synonyms
  422. lowerword == "shift-image-binding" ||
  423. lowerword == "sib") {
  424. ProcessBindingBase(argc, argv, glslang::EResImage);
  425. } else if (lowerword == "shift-sampler-bindings" || // synonyms
  426. lowerword == "shift-sampler-binding" ||
  427. lowerword == "ssb") {
  428. ProcessBindingBase(argc, argv, glslang::EResSampler);
  429. } else if (lowerword == "shift-uav-bindings" || // synonyms
  430. lowerword == "shift-uav-binding" ||
  431. lowerword == "suavb") {
  432. ProcessBindingBase(argc, argv, glslang::EResUav);
  433. } else if (lowerword == "shift-texture-bindings" || // synonyms
  434. lowerword == "shift-texture-binding" ||
  435. lowerword == "stb") {
  436. ProcessBindingBase(argc, argv, glslang::EResTexture);
  437. } else if (lowerword == "shift-ubo-bindings" || // synonyms
  438. lowerword == "shift-ubo-binding" ||
  439. lowerword == "shift-cbuffer-bindings" ||
  440. lowerword == "shift-cbuffer-binding" ||
  441. lowerword == "sub" ||
  442. lowerword == "scb") {
  443. ProcessBindingBase(argc, argv, glslang::EResUbo);
  444. } else if (lowerword == "shift-ssbo-bindings" || // synonyms
  445. lowerword == "shift-ssbo-binding" ||
  446. lowerword == "sbb") {
  447. ProcessBindingBase(argc, argv, glslang::EResSsbo);
  448. } else if (lowerword == "source-entrypoint" || // synonyms
  449. lowerword == "sep") {
  450. if (argc <= 1)
  451. Error("no <entry-point> provided for --source-entrypoint");
  452. sourceEntryPointName = argv[1];
  453. bumpArg();
  454. break;
  455. } else if (lowerword == "spirv-dis") {
  456. SpvToolsDisassembler = true;
  457. } else if (lowerword == "stdin") {
  458. Options |= EOptionStdin;
  459. shaderStageName = argv[1];
  460. } else if (lowerword == "suppress-warnings") {
  461. Options |= EOptionSuppressWarnings;
  462. } else if (lowerword == "target-env") {
  463. if (argc > 1) {
  464. if (strcmp(argv[1], "vulkan1.0") == 0) {
  465. setVulkanSpv();
  466. VulkanClientVersion = glslang::EShTargetVulkan_1_0;
  467. } else if (strcmp(argv[1], "vulkan1.1") == 0) {
  468. setVulkanSpv();
  469. TargetVersion = glslang::EShTargetSpv_1_3;
  470. VulkanClientVersion = glslang::EShTargetVulkan_1_1;
  471. } else if (strcmp(argv[1], "opengl") == 0) {
  472. setOpenGlSpv();
  473. OpenGLClientVersion = glslang::EShTargetOpenGL_450;
  474. } else
  475. Error("--target-env expected vulkan1.0, vulkan1.1, or opengl");
  476. }
  477. bumpArg();
  478. } else if (lowerword == "variable-name" || // synonyms
  479. lowerword == "vn") {
  480. Options |= EOptionOutputHexadecimal;
  481. if (argc <= 1)
  482. Error("no <C-variable-name> provided for --variable-name");
  483. variableName = argv[1];
  484. bumpArg();
  485. break;
  486. } else if (lowerword == "version") {
  487. Options |= EOptionDumpVersions;
  488. } else {
  489. usage();
  490. }
  491. }
  492. break;
  493. case 'C':
  494. Options |= EOptionCascadingErrors;
  495. break;
  496. case 'D':
  497. if (argv[0][2] == 0)
  498. Options |= EOptionReadHlsl;
  499. else
  500. UserPreamble.addDef(getStringOperand("-D<macro> macro name"));
  501. break;
  502. case 'E':
  503. Options |= EOptionOutputPreprocessed;
  504. break;
  505. case 'G':
  506. // OpenGL Client
  507. setOpenGlSpv();
  508. if (argv[0][2] != 0)
  509. ClientInputSemanticsVersion = getAttachedNumber("-G<num> client input semantics");
  510. break;
  511. case 'H':
  512. Options |= EOptionHumanReadableSpv;
  513. if ((Options & EOptionSpv) == 0) {
  514. // default to Vulkan
  515. setVulkanSpv();
  516. }
  517. break;
  518. case 'I':
  519. IncludeDirectoryList.push_back(getStringOperand("-I<dir> include path"));
  520. break;
  521. case 'O':
  522. if (argv[0][2] == 'd')
  523. Options |= EOptionOptimizeDisable;
  524. else if (argv[0][2] == 's')
  525. #if ENABLE_OPT
  526. Options |= EOptionOptimizeSize;
  527. #else
  528. Error("-Os not available; optimizer not linked");
  529. #endif
  530. else
  531. Error("unknown -O option");
  532. break;
  533. case 'S':
  534. if (argc <= 1)
  535. Error("no <stage> specified for -S");
  536. shaderStageName = argv[1];
  537. bumpArg();
  538. break;
  539. case 'U':
  540. UserPreamble.addUndef(getStringOperand("-U<macro>: macro name"));
  541. break;
  542. case 'V':
  543. setVulkanSpv();
  544. if (argv[0][2] != 0)
  545. ClientInputSemanticsVersion = getAttachedNumber("-V<num> client input semantics");
  546. break;
  547. case 'c':
  548. Options |= EOptionDumpConfig;
  549. break;
  550. case 'd':
  551. if (strncmp(&argv[0][1], "dumpversion", strlen(&argv[0][1]) + 1) == 0 ||
  552. strncmp(&argv[0][1], "dumpfullversion", strlen(&argv[0][1]) + 1) == 0)
  553. Options |= EOptionDumpBareVersion;
  554. else
  555. Options |= EOptionDefaultDesktop;
  556. break;
  557. case 'e':
  558. entryPointName = argv[1];
  559. if (argc <= 1)
  560. Error("no <name> provided for -e");
  561. bumpArg();
  562. break;
  563. case 'f':
  564. if (strcmp(&argv[0][2], "hlsl_functionality1") == 0)
  565. targetHlslFunctionality1 = true;
  566. else
  567. Error("-f: expected hlsl_functionality1");
  568. break;
  569. case 'g':
  570. Options |= EOptionDebug;
  571. break;
  572. case 'h':
  573. usage();
  574. break;
  575. case 'i':
  576. Options |= EOptionIntermediate;
  577. break;
  578. case 'l':
  579. Options |= EOptionLinkProgram;
  580. break;
  581. case 'm':
  582. Options |= EOptionMemoryLeakMode;
  583. break;
  584. case 'o':
  585. if (argc <= 1)
  586. Error("no <file> provided for -o");
  587. binaryFileName = argv[1];
  588. bumpArg();
  589. break;
  590. case 'q':
  591. Options |= EOptionDumpReflection;
  592. break;
  593. case 'r':
  594. Options |= EOptionRelaxedErrors;
  595. break;
  596. case 's':
  597. Options |= EOptionSuppressInfolog;
  598. break;
  599. case 't':
  600. Options |= EOptionMultiThreaded;
  601. break;
  602. case 'v':
  603. Options |= EOptionDumpVersions;
  604. break;
  605. case 'w':
  606. Options |= EOptionSuppressWarnings;
  607. break;
  608. case 'x':
  609. Options |= EOptionOutputHexadecimal;
  610. break;
  611. default:
  612. usage();
  613. break;
  614. }
  615. } else {
  616. std::string name(argv[0]);
  617. if (! SetConfigFile(name)) {
  618. workItems.push_back(std::unique_ptr<glslang::TWorkItem>(new glslang::TWorkItem(name)));
  619. }
  620. }
  621. }
  622. // Make sure that -S is always specified if --stdin is specified
  623. if ((Options & EOptionStdin) && shaderStageName == nullptr)
  624. Error("must provide -S when --stdin is given");
  625. // Make sure that -E is not specified alongside linking (which includes SPV generation)
  626. if ((Options & EOptionOutputPreprocessed) && (Options & EOptionLinkProgram))
  627. Error("can't use -E when linking is selected");
  628. // -o or -x makes no sense if there is no target binary
  629. if (binaryFileName && (Options & EOptionSpv) == 0)
  630. Error("no binary generation requested (e.g., -V)");
  631. if ((Options & EOptionFlattenUniformArrays) != 0 &&
  632. (Options & EOptionReadHlsl) == 0)
  633. Error("uniform array flattening only valid when compiling HLSL source.");
  634. }
  635. //
  636. // Translate the meaningful subset of command-line options to parser-behavior options.
  637. //
  638. void SetMessageOptions(EShMessages& messages)
  639. {
  640. if (Options & EOptionRelaxedErrors)
  641. messages = (EShMessages)(messages | EShMsgRelaxedErrors);
  642. if (Options & EOptionIntermediate)
  643. messages = (EShMessages)(messages | EShMsgAST);
  644. if (Options & EOptionSuppressWarnings)
  645. messages = (EShMessages)(messages | EShMsgSuppressWarnings);
  646. if (Options & EOptionSpv)
  647. messages = (EShMessages)(messages | EShMsgSpvRules);
  648. if (Options & EOptionVulkanRules)
  649. messages = (EShMessages)(messages | EShMsgVulkanRules);
  650. if (Options & EOptionOutputPreprocessed)
  651. messages = (EShMessages)(messages | EShMsgOnlyPreprocessor);
  652. if (Options & EOptionReadHlsl)
  653. messages = (EShMessages)(messages | EShMsgReadHlsl);
  654. if (Options & EOptionCascadingErrors)
  655. messages = (EShMessages)(messages | EShMsgCascadingErrors);
  656. if (Options & EOptionKeepUncalled)
  657. messages = (EShMessages)(messages | EShMsgKeepUncalled);
  658. if (Options & EOptionHlslOffsets)
  659. messages = (EShMessages)(messages | EShMsgHlslOffsets);
  660. if (Options & EOptionDebug)
  661. messages = (EShMessages)(messages | EShMsgDebugInfo);
  662. if (HlslEnable16BitTypes)
  663. messages = (EShMessages)(messages | EShMsgHlslEnable16BitTypes);
  664. if ((Options & EOptionOptimizeDisable) || !ENABLE_OPT)
  665. messages = (EShMessages)(messages | EShMsgHlslLegalization);
  666. }
  667. //
  668. // Thread entry point, for non-linking asynchronous mode.
  669. //
  670. void CompileShaders(glslang::TWorklist& worklist)
  671. {
  672. if (Options & EOptionDebug)
  673. Error("cannot generate debug information unless linking to generate code");
  674. glslang::TWorkItem* workItem;
  675. if (Options & EOptionStdin) {
  676. if (worklist.remove(workItem)) {
  677. ShHandle compiler = ShConstructCompiler(FindLanguage("stdin"), Options);
  678. if (compiler == nullptr)
  679. return;
  680. CompileFile("stdin", compiler);
  681. if (! (Options & EOptionSuppressInfolog))
  682. workItem->results = ShGetInfoLog(compiler);
  683. ShDestruct(compiler);
  684. }
  685. } else {
  686. while (worklist.remove(workItem)) {
  687. ShHandle compiler = ShConstructCompiler(FindLanguage(workItem->name), Options);
  688. if (compiler == 0)
  689. return;
  690. CompileFile(workItem->name.c_str(), compiler);
  691. if (! (Options & EOptionSuppressInfolog))
  692. workItem->results = ShGetInfoLog(compiler);
  693. ShDestruct(compiler);
  694. }
  695. }
  696. }
  697. // Outputs the given string, but only if it is non-null and non-empty.
  698. // This prevents erroneous newlines from appearing.
  699. void PutsIfNonEmpty(const char* str)
  700. {
  701. if (str && str[0]) {
  702. puts(str);
  703. }
  704. }
  705. // Outputs the given string to stderr, but only if it is non-null and non-empty.
  706. // This prevents erroneous newlines from appearing.
  707. void StderrIfNonEmpty(const char* str)
  708. {
  709. if (str && str[0])
  710. fprintf(stderr, "%s\n", str);
  711. }
  712. // Simple bundling of what makes a compilation unit for ease in passing around,
  713. // and separation of handling file IO versus API (programmatic) compilation.
  714. struct ShaderCompUnit {
  715. EShLanguage stage;
  716. static const int maxCount = 1;
  717. int count; // live number of strings/names
  718. const char* text[maxCount]; // memory owned/managed externally
  719. std::string fileName[maxCount]; // hold's the memory, but...
  720. const char* fileNameList[maxCount]; // downstream interface wants pointers
  721. ShaderCompUnit(EShLanguage stage) : stage(stage), count(0) { }
  722. ShaderCompUnit(const ShaderCompUnit& rhs)
  723. {
  724. stage = rhs.stage;
  725. count = rhs.count;
  726. for (int i = 0; i < count; ++i) {
  727. fileName[i] = rhs.fileName[i];
  728. text[i] = rhs.text[i];
  729. fileNameList[i] = rhs.fileName[i].c_str();
  730. }
  731. }
  732. void addString(std::string& ifileName, const char* itext)
  733. {
  734. assert(count < maxCount);
  735. fileName[count] = ifileName;
  736. text[count] = itext;
  737. fileNameList[count] = fileName[count].c_str();
  738. ++count;
  739. }
  740. };
  741. //
  742. // For linking mode: Will independently parse each compilation unit, but then put them
  743. // in the same program and link them together, making at most one linked module per
  744. // pipeline stage.
  745. //
  746. // Uses the new C++ interface instead of the old handle-based interface.
  747. //
  748. void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits)
  749. {
  750. // keep track of what to free
  751. std::list<glslang::TShader*> shaders;
  752. EShMessages messages = EShMsgDefault;
  753. SetMessageOptions(messages);
  754. //
  755. // Per-shader processing...
  756. //
  757. glslang::TProgram& program = *new glslang::TProgram;
  758. for (auto it = compUnits.cbegin(); it != compUnits.cend(); ++it) {
  759. const auto &compUnit = *it;
  760. glslang::TShader* shader = new glslang::TShader(compUnit.stage);
  761. shader->setStringsWithLengthsAndNames(compUnit.text, NULL, compUnit.fileNameList, compUnit.count);
  762. if (entryPointName)
  763. shader->setEntryPoint(entryPointName);
  764. if (sourceEntryPointName) {
  765. if (entryPointName == nullptr)
  766. printf("Warning: Changing source entry point name without setting an entry-point name.\n"
  767. "Use '-e <name>'.\n");
  768. shader->setSourceEntryPoint(sourceEntryPointName);
  769. }
  770. if (UserPreamble.isSet())
  771. shader->setPreamble(UserPreamble.get());
  772. shader->addProcesses(Processes);
  773. // Set IO mapper binding shift values
  774. for (int r = 0; r < glslang::EResCount; ++r) {
  775. const glslang::TResourceType res = glslang::TResourceType(r);
  776. // Set base bindings
  777. shader->setShiftBinding(res, baseBinding[res][compUnit.stage]);
  778. // Set bindings for particular resource sets
  779. // TODO: use a range based for loop here, when available in all environments.
  780. for (auto i = baseBindingForSet[res][compUnit.stage].begin();
  781. i != baseBindingForSet[res][compUnit.stage].end(); ++i)
  782. shader->setShiftBindingForSet(res, i->second, i->first);
  783. }
  784. shader->setFlattenUniformArrays((Options & EOptionFlattenUniformArrays) != 0);
  785. shader->setNoStorageFormat((Options & EOptionNoStorageFormat) != 0);
  786. shader->setResourceSetBinding(baseResourceSetBinding[compUnit.stage]);
  787. if (Options & EOptionHlslIoMapping)
  788. shader->setHlslIoMapping(true);
  789. if (Options & EOptionAutoMapBindings)
  790. shader->setAutoMapBindings(true);
  791. if (Options & EOptionAutoMapLocations)
  792. shader->setAutoMapLocations(true);
  793. if (Options & EOptionInvertY)
  794. shader->setInvertY(true);
  795. // Set up the environment, some subsettings take precedence over earlier
  796. // ways of setting things.
  797. if (Options & EOptionSpv) {
  798. if (Options & EOptionVulkanRules) {
  799. shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
  800. : glslang::EShSourceGlsl,
  801. compUnit.stage, glslang::EShClientVulkan, ClientInputSemanticsVersion);
  802. shader->setEnvClient(glslang::EShClientVulkan, VulkanClientVersion);
  803. } else {
  804. shader->setEnvInput((Options & EOptionReadHlsl) ? glslang::EShSourceHlsl
  805. : glslang::EShSourceGlsl,
  806. compUnit.stage, glslang::EShClientOpenGL, ClientInputSemanticsVersion);
  807. shader->setEnvClient(glslang::EShClientOpenGL, OpenGLClientVersion);
  808. }
  809. shader->setEnvTarget(glslang::EShTargetSpv, TargetVersion);
  810. if (targetHlslFunctionality1)
  811. shader->setEnvTargetHlslFunctionality1();
  812. }
  813. shaders.push_back(shader);
  814. const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100;
  815. DirStackFileIncluder includer;
  816. std::for_each(IncludeDirectoryList.rbegin(), IncludeDirectoryList.rend(), [&includer](const std::string& dir) {
  817. includer.pushExternalLocalDirectory(dir); });
  818. if (Options & EOptionOutputPreprocessed) {
  819. std::string str;
  820. if (shader->preprocess(&Resources, defaultVersion, ENoProfile, false, false,
  821. messages, &str, includer)) {
  822. PutsIfNonEmpty(str.c_str());
  823. } else {
  824. CompileFailed = true;
  825. }
  826. StderrIfNonEmpty(shader->getInfoLog());
  827. StderrIfNonEmpty(shader->getInfoDebugLog());
  828. continue;
  829. }
  830. if (! shader->parse(&Resources, defaultVersion, false, messages, includer))
  831. CompileFailed = true;
  832. program.addShader(shader);
  833. if (! (Options & EOptionSuppressInfolog) &&
  834. ! (Options & EOptionMemoryLeakMode)) {
  835. PutsIfNonEmpty(compUnit.fileName[0].c_str());
  836. PutsIfNonEmpty(shader->getInfoLog());
  837. PutsIfNonEmpty(shader->getInfoDebugLog());
  838. }
  839. }
  840. //
  841. // Program-level processing...
  842. //
  843. // Link
  844. if (! (Options & EOptionOutputPreprocessed) && ! program.link(messages))
  845. LinkFailed = true;
  846. // Map IO
  847. if (Options & EOptionSpv) {
  848. if (!program.mapIO())
  849. LinkFailed = true;
  850. }
  851. // Report
  852. if (! (Options & EOptionSuppressInfolog) &&
  853. ! (Options & EOptionMemoryLeakMode)) {
  854. PutsIfNonEmpty(program.getInfoLog());
  855. PutsIfNonEmpty(program.getInfoDebugLog());
  856. }
  857. // Reflect
  858. if (Options & EOptionDumpReflection) {
  859. program.buildReflection();
  860. program.dumpReflection();
  861. }
  862. // Dump SPIR-V
  863. if (Options & EOptionSpv) {
  864. if (CompileFailed || LinkFailed)
  865. printf("SPIR-V is not generated for failed compile or link\n");
  866. else {
  867. for (int stage = 0; stage < EShLangCount; ++stage) {
  868. if (program.getIntermediate((EShLanguage)stage)) {
  869. std::vector<unsigned int> spirv;
  870. std::string warningsErrors;
  871. spv::SpvBuildLogger logger;
  872. glslang::SpvOptions spvOptions;
  873. if (Options & EOptionDebug)
  874. spvOptions.generateDebugInfo = true;
  875. spvOptions.disableOptimizer = (Options & EOptionOptimizeDisable) != 0;
  876. spvOptions.optimizeSize = (Options & EOptionOptimizeSize) != 0;
  877. glslang::GlslangToSpv(*program.getIntermediate((EShLanguage)stage), spirv, &logger, &spvOptions);
  878. // Dump the spv to a file or stdout, etc., but only if not doing
  879. // memory/perf testing, as it's not internal to programmatic use.
  880. if (! (Options & EOptionMemoryLeakMode)) {
  881. printf("%s", logger.getAllMessages().c_str());
  882. if (Options & EOptionOutputHexadecimal) {
  883. glslang::OutputSpvHex(spirv, GetBinaryName((EShLanguage)stage), variableName);
  884. } else {
  885. glslang::OutputSpvBin(spirv, GetBinaryName((EShLanguage)stage));
  886. }
  887. #if ENABLE_OPT
  888. if (SpvToolsDisassembler)
  889. spv::SpirvToolsDisassemble(std::cout, spirv);
  890. #else
  891. if (SpvToolsDisassembler)
  892. printf("SPIRV-Tools is not enabled; use -H for human readable SPIR-V\n");
  893. #endif
  894. if (!SpvToolsDisassembler && (Options & EOptionHumanReadableSpv))
  895. spv::Disassemble(std::cout, spirv);
  896. }
  897. }
  898. }
  899. }
  900. }
  901. // Free everything up, program has to go before the shaders
  902. // because it might have merged stuff from the shaders, and
  903. // the stuff from the shaders has to have its destructors called
  904. // before the pools holding the memory in the shaders is freed.
  905. delete &program;
  906. while (shaders.size() > 0) {
  907. delete shaders.back();
  908. shaders.pop_back();
  909. }
  910. }
  911. //
  912. // Do file IO part of compile and link, handing off the pure
  913. // API/programmatic mode to CompileAndLinkShaderUnits(), which can
  914. // be put in a loop for testing memory footprint and performance.
  915. //
  916. // This is just for linking mode: meaning all the shaders will be put into the
  917. // the same program linked together.
  918. //
  919. // This means there are a limited number of work items (not multi-threading mode)
  920. // and that the point is testing at the linking level. Hence, to enable
  921. // performance and memory testing, the actual compile/link can be put in
  922. // a loop, independent of processing the work items and file IO.
  923. //
  924. void CompileAndLinkShaderFiles(glslang::TWorklist& Worklist)
  925. {
  926. std::vector<ShaderCompUnit> compUnits;
  927. // If this is using stdin, we can't really detect multiple different file
  928. // units by input type. We need to assume that we're just being given one
  929. // file of a certain type.
  930. if ((Options & EOptionStdin) != 0) {
  931. ShaderCompUnit compUnit(FindLanguage("stdin"));
  932. std::istreambuf_iterator<char> begin(std::cin), end;
  933. std::string tempString(begin, end);
  934. char* fileText = strdup(tempString.c_str());
  935. std::string fileName = "stdin";
  936. compUnit.addString(fileName, fileText);
  937. compUnits.push_back(compUnit);
  938. } else {
  939. // Transfer all the work items from to a simple list of
  940. // of compilation units. (We don't care about the thread
  941. // work-item distribution properties in this path, which
  942. // is okay due to the limited number of shaders, know since
  943. // they are all getting linked together.)
  944. glslang::TWorkItem* workItem;
  945. while (Worklist.remove(workItem)) {
  946. ShaderCompUnit compUnit(FindLanguage(workItem->name));
  947. char* fileText = ReadFileData(workItem->name.c_str());
  948. if (fileText == nullptr)
  949. usage();
  950. compUnit.addString(workItem->name, fileText);
  951. compUnits.push_back(compUnit);
  952. }
  953. }
  954. // Actual call to programmatic processing of compile and link,
  955. // in a loop for testing memory and performance. This part contains
  956. // all the perf/memory that a programmatic consumer will care about.
  957. for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
  958. for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j)
  959. CompileAndLinkShaderUnits(compUnits);
  960. if (Options & EOptionMemoryLeakMode)
  961. glslang::OS_DumpMemoryCounters();
  962. }
  963. // free memory from ReadFileData, which got stored in a const char*
  964. // as the first string above
  965. for (auto it = compUnits.begin(); it != compUnits.end(); ++it)
  966. FreeFileData(const_cast<char*>(it->text[0]));
  967. }
  968. int singleMain()
  969. {
  970. glslang::TWorklist workList;
  971. std::for_each(WorkItems.begin(), WorkItems.end(), [&workList](std::unique_ptr<glslang::TWorkItem>& item) {
  972. assert(item);
  973. workList.add(item.get());
  974. });
  975. if (Options & EOptionDumpConfig) {
  976. printf("%s", glslang::GetDefaultTBuiltInResourceString().c_str());
  977. if (workList.empty())
  978. return ESuccess;
  979. }
  980. if (Options & EOptionDumpBareVersion) {
  981. printf("%d.%d.%d\n",
  982. glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
  983. if (workList.empty())
  984. return ESuccess;
  985. } else if (Options & EOptionDumpVersions) {
  986. printf("Glslang Version: %d.%d.%d\n",
  987. glslang::GetSpirvGeneratorVersion(), GLSLANG_MINOR_VERSION, GLSLANG_PATCH_LEVEL);
  988. printf("ESSL Version: %s\n", glslang::GetEsslVersionString());
  989. printf("GLSL Version: %s\n", glslang::GetGlslVersionString());
  990. std::string spirvVersion;
  991. glslang::GetSpirvVersion(spirvVersion);
  992. printf("SPIR-V Version %s\n", spirvVersion.c_str());
  993. printf("GLSL.std.450 Version %d, Revision %d\n", GLSLstd450Version, GLSLstd450Revision);
  994. printf("Khronos Tool ID %d\n", glslang::GetKhronosToolId());
  995. printf("SPIR-V Generator Version %d\n", glslang::GetSpirvGeneratorVersion());
  996. printf("GL_KHR_vulkan_glsl version %d\n", 100);
  997. printf("ARB_GL_gl_spirv version %d\n", 100);
  998. if (workList.empty())
  999. return ESuccess;
  1000. }
  1001. if (workList.empty() && ((Options & EOptionStdin) == 0)) {
  1002. usage();
  1003. }
  1004. if (Options & EOptionStdin) {
  1005. WorkItems.push_back(std::unique_ptr<glslang::TWorkItem>{new glslang::TWorkItem("stdin")});
  1006. workList.add(WorkItems.back().get());
  1007. }
  1008. ProcessConfigFile();
  1009. //
  1010. // Two modes:
  1011. // 1) linking all arguments together, single-threaded, new C++ interface
  1012. // 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface
  1013. //
  1014. if (Options & EOptionLinkProgram ||
  1015. Options & EOptionOutputPreprocessed) {
  1016. glslang::InitializeProcess();
  1017. glslang::InitializeProcess(); // also test reference counting of users
  1018. glslang::InitializeProcess(); // also test reference counting of users
  1019. glslang::FinalizeProcess(); // also test reference counting of users
  1020. glslang::FinalizeProcess(); // also test reference counting of users
  1021. CompileAndLinkShaderFiles(workList);
  1022. glslang::FinalizeProcess();
  1023. } else {
  1024. ShInitialize();
  1025. ShInitialize(); // also test reference counting of users
  1026. ShFinalize(); // also test reference counting of users
  1027. bool printShaderNames = workList.size() > 1;
  1028. if (Options & EOptionMultiThreaded) {
  1029. std::array<std::thread, 16> threads;
  1030. for (unsigned int t = 0; t < threads.size(); ++t) {
  1031. threads[t] = std::thread(CompileShaders, std::ref(workList));
  1032. if (threads[t].get_id() == std::thread::id()) {
  1033. fprintf(stderr, "Failed to create thread\n");
  1034. return EFailThreadCreate;
  1035. }
  1036. }
  1037. std::for_each(threads.begin(), threads.end(), [](std::thread& t) { t.join(); });
  1038. } else
  1039. CompileShaders(workList);
  1040. // Print out all the resulting infologs
  1041. for (size_t w = 0; w < WorkItems.size(); ++w) {
  1042. if (WorkItems[w]) {
  1043. if (printShaderNames || WorkItems[w]->results.size() > 0)
  1044. PutsIfNonEmpty(WorkItems[w]->name.c_str());
  1045. PutsIfNonEmpty(WorkItems[w]->results.c_str());
  1046. }
  1047. }
  1048. ShFinalize();
  1049. }
  1050. if (CompileFailed)
  1051. return EFailCompile;
  1052. if (LinkFailed)
  1053. return EFailLink;
  1054. return 0;
  1055. }
  1056. int C_DECL main(int argc, char* argv[])
  1057. {
  1058. ProcessArguments(WorkItems, argc, argv);
  1059. int ret = 0;
  1060. // Loop over the entire init/finalize cycle to watch memory changes
  1061. const int iterations = 1;
  1062. if (iterations > 1)
  1063. glslang::OS_DumpMemoryCounters();
  1064. for (int i = 0; i < iterations; ++i) {
  1065. ret = singleMain();
  1066. if (iterations > 1)
  1067. glslang::OS_DumpMemoryCounters();
  1068. }
  1069. return ret;
  1070. }
  1071. //
  1072. // Deduce the language from the filename. Files must end in one of the
  1073. // following extensions:
  1074. //
  1075. // .vert = vertex
  1076. // .tesc = tessellation control
  1077. // .tese = tessellation evaluation
  1078. // .geom = geometry
  1079. // .frag = fragment
  1080. // .comp = compute
  1081. //
  1082. // Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl
  1083. // where <stage> is one of the stages listed above.
  1084. //
  1085. EShLanguage FindLanguage(const std::string& name, bool parseStageName)
  1086. {
  1087. std::string stageName;
  1088. if (shaderStageName)
  1089. stageName = shaderStageName;
  1090. else if (parseStageName) {
  1091. // Note: "first" extension means "first from the end", i.e.
  1092. // if the file is named foo.vert.glsl, then "glsl" is first,
  1093. // "vert" is second.
  1094. size_t firstExtStart = name.find_last_of(".");
  1095. bool hasFirstExt = firstExtStart != std::string::npos;
  1096. size_t secondExtStart = hasFirstExt ? name.find_last_of(".", firstExtStart - 1) : std::string::npos;
  1097. bool hasSecondExt = secondExtStart != std::string::npos;
  1098. std::string firstExt = name.substr(firstExtStart + 1, std::string::npos);
  1099. bool usesUnifiedExt = hasFirstExt && (firstExt == "glsl" || firstExt == "hlsl");
  1100. if (usesUnifiedExt && firstExt == "hlsl")
  1101. Options |= EOptionReadHlsl;
  1102. if (hasFirstExt && !usesUnifiedExt)
  1103. stageName = firstExt;
  1104. else if (usesUnifiedExt && hasSecondExt)
  1105. stageName = name.substr(secondExtStart + 1, firstExtStart - secondExtStart - 1);
  1106. else {
  1107. usage();
  1108. return EShLangVertex;
  1109. }
  1110. } else
  1111. stageName = name;
  1112. if (stageName == "vert")
  1113. return EShLangVertex;
  1114. else if (stageName == "tesc")
  1115. return EShLangTessControl;
  1116. else if (stageName == "tese")
  1117. return EShLangTessEvaluation;
  1118. else if (stageName == "geom")
  1119. return EShLangGeometry;
  1120. else if (stageName == "frag")
  1121. return EShLangFragment;
  1122. else if (stageName == "comp")
  1123. return EShLangCompute;
  1124. usage();
  1125. return EShLangVertex;
  1126. }
  1127. //
  1128. // Read a file's data into a string, and compile it using the old interface ShCompile,
  1129. // for non-linkable results.
  1130. //
  1131. void CompileFile(const char* fileName, ShHandle compiler)
  1132. {
  1133. int ret = 0;
  1134. char* shaderString;
  1135. if ((Options & EOptionStdin) != 0) {
  1136. std::istreambuf_iterator<char> begin(std::cin), end;
  1137. std::string tempString(begin, end);
  1138. shaderString = strdup(tempString.c_str());
  1139. } else {
  1140. shaderString = ReadFileData(fileName);
  1141. }
  1142. // move to length-based strings, rather than null-terminated strings
  1143. int* lengths = new int[1];
  1144. lengths[0] = (int)strlen(shaderString);
  1145. EShMessages messages = EShMsgDefault;
  1146. SetMessageOptions(messages);
  1147. if (UserPreamble.isSet())
  1148. Error("-D and -U options require -l (linking)\n");
  1149. for (int i = 0; i < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++i) {
  1150. for (int j = 0; j < ((Options & EOptionMemoryLeakMode) ? 100 : 1); ++j) {
  1151. // ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
  1152. ret = ShCompile(compiler, &shaderString, 1, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
  1153. // const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err",
  1154. // "or should be l", "ine 1", "string 5\n", "float glo", "bal",
  1155. // ";\n#error should be line 2\n void main() {", "global = 2.3;}" };
  1156. // const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" };
  1157. // ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages);
  1158. }
  1159. if (Options & EOptionMemoryLeakMode)
  1160. glslang::OS_DumpMemoryCounters();
  1161. }
  1162. delete [] lengths;
  1163. FreeFileData(shaderString);
  1164. if (ret == 0)
  1165. CompileFailed = true;
  1166. }
  1167. //
  1168. // print usage to stdout
  1169. //
  1170. void usage()
  1171. {
  1172. printf("Usage: glslangValidator [option]... [file]...\n"
  1173. "\n"
  1174. "'file' can end in .<stage> for auto-stage classification, where <stage> is:\n"
  1175. " .conf to provide a config file that replaces the default configuration\n"
  1176. " (see -c option below for generating a template)\n"
  1177. " .vert for a vertex shader\n"
  1178. " .tesc for a tessellation control shader\n"
  1179. " .tese for a tessellation evaluation shader\n"
  1180. " .geom for a geometry shader\n"
  1181. " .frag for a fragment shader\n"
  1182. " .comp for a compute shader\n"
  1183. " .glsl for .vert.glsl, .tesc.glsl, ..., .comp.glsl compound suffixes\n"
  1184. " .hlsl for .vert.hlsl, .tesc.hlsl, ..., .comp.hlsl compound suffixes\n"
  1185. "\n"
  1186. "Options:\n"
  1187. " -C cascading errors; risk crash from accumulation of error recoveries\n"
  1188. " -D input is HLSL (this is the default when any suffix is .hlsl)\n"
  1189. " -D<macro=def>\n"
  1190. " -D<macro> define a pre-processor macro\n"
  1191. " -E print pre-processed GLSL; cannot be used with -l;\n"
  1192. " errors will appear on stderr\n"
  1193. " -G[ver] create SPIR-V binary, under OpenGL semantics; turns on -l;\n"
  1194. " default file name is <stage>.spv (-o overrides this);\n"
  1195. " 'ver', when present, is the version of the input semantics,\n"
  1196. " which will appear in #define GL_SPIRV ver;\n"
  1197. " '--client opengl100' is the same as -G100;\n"
  1198. " a '--target-env' for OpenGL will also imply '-G'\n"
  1199. " -H print human readable form of SPIR-V; turns on -V\n"
  1200. " -I<dir> add dir to the include search path; includer's directory\n"
  1201. " is searched first, followed by left-to-right order of -I\n"
  1202. " -Od disables optimization; may cause illegal SPIR-V for HLSL\n"
  1203. " -Os optimizes SPIR-V to minimize size\n"
  1204. " -S <stage> uses specified stage rather than parsing the file extension\n"
  1205. " choices for <stage> are vert, tesc, tese, geom, frag, or comp\n"
  1206. " -U<macro> undefine a pre-processor macro\n"
  1207. " -V[ver] create SPIR-V binary, under Vulkan semantics; turns on -l;\n"
  1208. " default file name is <stage>.spv (-o overrides this)\n"
  1209. " 'ver', when present, is the version of the input semantics,\n"
  1210. " which will appear in #define VULKAN ver\n"
  1211. " '--client vulkan100' is the same as -V100\n"
  1212. " a '--target-env' for Vulkan will also imply '-V'\n"
  1213. " -c configuration dump;\n"
  1214. " creates the default configuration file (redirect to a .conf file)\n"
  1215. " -d default to desktop (#version 110) when there is no shader #version\n"
  1216. " (default is ES version 100)\n"
  1217. " -e <name> | --entry-point <name>\n"
  1218. " specify <name> as the entry-point function name\n"
  1219. " -f{hlsl_functionality1}\n"
  1220. " 'hlsl_functionality1' enables use of the\n"
  1221. " SPV_GOOGLE_hlsl_functionality1 extension\n"
  1222. " -g generate debug information\n"
  1223. " -h print this usage message\n"
  1224. " -i intermediate tree (glslang AST) is printed out\n"
  1225. " -l link all input files together to form a single module\n"
  1226. " -m memory leak mode\n"
  1227. " -o <file> save binary to <file>, requires a binary option (e.g., -V)\n"
  1228. " -q dump reflection query database\n"
  1229. " -r | --relaxed-errors"
  1230. " relaxed GLSL semantic error-checking mode\n"
  1231. " -s silence syntax and semantic error reporting\n"
  1232. " -t multi-threaded mode\n"
  1233. " -v | --version\n"
  1234. " print version strings\n"
  1235. " -w | --suppress-warnings\n"
  1236. " suppress GLSL warnings, except as required by \"#extension : warn\"\n"
  1237. " -x save binary output as text-based 32-bit hexadecimal numbers\n"
  1238. " --auto-map-bindings | --amb automatically bind uniform variables\n"
  1239. " without explicit bindings\n"
  1240. " --auto-map-locations | --aml automatically locate input/output lacking\n"
  1241. " 'location' (fragile, not cross stage)\n"
  1242. " --client {vulkan<ver>|opengl<ver>} see -V and -G\n"
  1243. " -dumpfullversion | -dumpversion print bare major.minor.patchlevel\n"
  1244. " --flatten-uniform-arrays | --fua flatten uniform texture/sampler arrays to\n"
  1245. " scalars\n"
  1246. " --hlsl-offsets allow block offsets to follow HLSL rules\n"
  1247. " works independently of source language\n"
  1248. " --hlsl-iomap perform IO mapping in HLSL register space\n"
  1249. " --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL\n"
  1250. " --invert-y | --iy invert position.Y output in vertex shader\n"
  1251. " --keep-uncalled | --ku don't eliminate uncalled functions\n"
  1252. " --no-storage-format | --nsf use Unknown image format\n"
  1253. " --resource-set-binding [stage] name set binding\n"
  1254. " set descriptor set and binding for\n"
  1255. " individual resources\n"
  1256. " --resource-set-binding [stage] set\n"
  1257. " set descriptor set for all resources\n"
  1258. " --rsb synonym for --resource-set-binding\n"
  1259. " --shift-image-binding [stage] num\n"
  1260. " base binding number for images (uav)\n"
  1261. " --shift-image-binding [stage] [num set]...\n"
  1262. " per-descriptor-set shift values\n"
  1263. " --sib synonym for --shift-image-binding\n"
  1264. " --shift-sampler-binding [stage] num\n"
  1265. " base binding number for samplers\n"
  1266. " --shift-sampler-binding [stage] [num set]...\n"
  1267. " per-descriptor-set shift values\n"
  1268. " --ssb synonym for --shift-sampler-binding\n"
  1269. " --shift-ssbo-binding [stage] num base binding number for SSBOs\n"
  1270. " --shift-ssbo-binding [stage] [num set]...\n"
  1271. " per-descriptor-set shift values\n"
  1272. " --sbb synonym for --shift-ssbo-binding\n"
  1273. " --shift-texture-binding [stage] num\n"
  1274. " base binding number for textures\n"
  1275. " --shift-texture-binding [stage] [num set]...\n"
  1276. " per-descriptor-set shift values\n"
  1277. " --stb synonym for --shift-texture-binding\n"
  1278. " --shift-uav-binding [stage] num base binding number for UAVs\n"
  1279. " --shift-uav-binding [stage] [num set]...\n"
  1280. " per-descriptor-set shift values\n"
  1281. " --suavb synonym for --shift-uav-binding\n"
  1282. " --shift-UBO-binding [stage] num base binding number for UBOs\n"
  1283. " --shift-UBO-binding [stage] [num set]...\n"
  1284. " per-descriptor-set shift values\n"
  1285. " --sub synonym for --shift-UBO-binding\n"
  1286. " --shift-cbuffer-binding | --scb synonyms for --shift-UBO-binding\n"
  1287. " --spirv-dis output standard-form disassembly; works only\n"
  1288. " when a SPIR-V generation option is also used\n"
  1289. " --source-entrypoint <name> the given shader source function is\n"
  1290. " renamed to be the <name> given in -e\n"
  1291. " --sep synonym for --source-entrypoint\n"
  1292. " --stdin read from stdin instead of from a file;\n"
  1293. " requires providing the shader stage using -S\n"
  1294. " --target-env {vulkan1.0 | vulkan1.1 | opengl} \n"
  1295. " set execution environment that emitted code\n"
  1296. " will execute in (as opposed to the language\n"
  1297. " semantics selected by --client) defaults:\n"
  1298. " * 'vulkan1.0' under '--client vulkan<ver>'\n"
  1299. " * 'opengl' under '--client opengl<ver>'\n"
  1300. " --variable-name <name>\n"
  1301. " --vn <name> creates a C header file that contains a\n"
  1302. " uint32_t array named <name>\n"
  1303. " initialized with the shader binary code\n"
  1304. );
  1305. exit(EFailUsage);
  1306. }
  1307. #if !defined _MSC_VER && !defined MINGW_HAS_SECURE_API
  1308. #include <errno.h>
  1309. int fopen_s(
  1310. FILE** pFile,
  1311. const char* filename,
  1312. const char* mode
  1313. )
  1314. {
  1315. if (!pFile || !filename || !mode) {
  1316. return EINVAL;
  1317. }
  1318. FILE* f = fopen(filename, mode);
  1319. if (! f) {
  1320. if (errno != 0) {
  1321. return errno;
  1322. } else {
  1323. return ENOENT;
  1324. }
  1325. }
  1326. *pFile = f;
  1327. return 0;
  1328. }
  1329. #endif
  1330. //
  1331. // Malloc a string of sufficient size and read a string into it.
  1332. //
  1333. char* ReadFileData(const char* fileName)
  1334. {
  1335. FILE *in = nullptr;
  1336. int errorCode = fopen_s(&in, fileName, "r");
  1337. if (errorCode || in == nullptr)
  1338. Error("unable to open input file");
  1339. int count = 0;
  1340. while (fgetc(in) != EOF)
  1341. count++;
  1342. fseek(in, 0, SEEK_SET);
  1343. char* return_data = (char*)malloc(count + 1); // freed in FreeFileData()
  1344. if ((int)fread(return_data, 1, count, in) != count) {
  1345. free(return_data);
  1346. Error("can't read input file");
  1347. }
  1348. return_data[count] = '\0';
  1349. fclose(in);
  1350. return return_data;
  1351. }
  1352. void FreeFileData(char* data)
  1353. {
  1354. free(data);
  1355. }
  1356. void InfoLogMsg(const char* msg, const char* name, const int num)
  1357. {
  1358. if (num >= 0 )
  1359. printf("#### %s %s %d INFO LOG ####\n", msg, name, num);
  1360. else
  1361. printf("#### %s %s INFO LOG ####\n", msg, name);
  1362. }