StandAlone.cpp 65 KB

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