StandAlone.cpp 58 KB

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