disassemble.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. //
  2. // Copyright (C) 2014-2015 LunarG, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. //
  35. // Disassembler for SPIR-V.
  36. //
  37. #include <cstdlib>
  38. #include <cstring>
  39. #include <cassert>
  40. #include <iomanip>
  41. #include <stack>
  42. #include <sstream>
  43. #include <cstring>
  44. #include "disassemble.h"
  45. #include "doc.h"
  46. namespace spv {
  47. extern "C" {
  48. // Include C-based headers that don't have a namespace
  49. #include "GLSL.std.450.h"
  50. #include "GLSL.ext.AMD.h"
  51. #include "GLSL.ext.NV.h"
  52. }
  53. }
  54. const char* GlslStd450DebugNames[spv::GLSLstd450Count];
  55. namespace spv {
  56. static const char* GLSLextAMDGetDebugNames(const char*, unsigned);
  57. static const char* GLSLextNVGetDebugNames(const char*, unsigned);
  58. static void Kill(std::ostream& out, const char* message)
  59. {
  60. out << std::endl << "Disassembly failed: " << message << std::endl;
  61. exit(1);
  62. }
  63. // used to identify the extended instruction library imported when printing
  64. enum ExtInstSet {
  65. GLSL450Inst,
  66. GLSLextAMDInst,
  67. GLSLextNVInst,
  68. OpenCLExtInst,
  69. NonSemanticDebugPrintfExtInst,
  70. };
  71. // Container class for a single instance of a SPIR-V stream, with methods for disassembly.
  72. class SpirvStream {
  73. public:
  74. SpirvStream(std::ostream& out, const std::vector<unsigned int>& stream) : out(out), stream(stream), word(0), nextNestedControl(0) { }
  75. virtual ~SpirvStream() { }
  76. void validate();
  77. void processInstructions();
  78. protected:
  79. SpirvStream(const SpirvStream&);
  80. SpirvStream& operator=(const SpirvStream&);
  81. Op getOpCode(int id) const { return idInstruction[id] ? (Op)(stream[idInstruction[id]] & OpCodeMask) : OpNop; }
  82. // Output methods
  83. void outputIndent();
  84. void formatId(Id id, std::stringstream&);
  85. void outputResultId(Id id);
  86. void outputTypeId(Id id);
  87. void outputId(Id id);
  88. void outputMask(OperandClass operandClass, unsigned mask);
  89. void disassembleImmediates(int numOperands);
  90. void disassembleIds(int numOperands);
  91. int disassembleString();
  92. void disassembleInstruction(Id resultId, Id typeId, Op opCode, int numOperands);
  93. // Data
  94. std::ostream& out; // where to write the disassembly
  95. const std::vector<unsigned int>& stream; // the actual word stream
  96. int size; // the size of the word stream
  97. int word; // the next word of the stream to read
  98. // map each <id> to the instruction that created it
  99. Id bound;
  100. std::vector<unsigned int> idInstruction; // the word offset into the stream where the instruction for result [id] starts; 0 if not yet seen (forward reference or function parameter)
  101. std::vector<std::string> idDescriptor; // the best text string known for explaining the <id>
  102. // schema
  103. unsigned int schema;
  104. // stack of structured-merge points
  105. std::stack<Id> nestedControl;
  106. Id nextNestedControl; // need a slight delay for when we are nested
  107. };
  108. void SpirvStream::validate()
  109. {
  110. size = (int)stream.size();
  111. if (size < 4)
  112. Kill(out, "stream is too short");
  113. // Magic number
  114. if (stream[word++] != MagicNumber) {
  115. out << "Bad magic number";
  116. return;
  117. }
  118. // Version
  119. out << "// Module Version " << std::hex << stream[word++] << std::endl;
  120. // Generator's magic number
  121. out << "// Generated by (magic number): " << std::hex << stream[word++] << std::dec << std::endl;
  122. // Result <id> bound
  123. bound = stream[word++];
  124. idInstruction.resize(bound);
  125. idDescriptor.resize(bound);
  126. out << "// Id's are bound by " << bound << std::endl;
  127. out << std::endl;
  128. // Reserved schema, must be 0 for now
  129. schema = stream[word++];
  130. if (schema != 0)
  131. Kill(out, "bad schema, must be 0");
  132. }
  133. // Loop over all the instructions, in order, processing each.
  134. // Boiler plate for each is handled here directly, the rest is dispatched.
  135. void SpirvStream::processInstructions()
  136. {
  137. // Instructions
  138. while (word < size) {
  139. int instructionStart = word;
  140. // Instruction wordCount and opcode
  141. unsigned int firstWord = stream[word];
  142. unsigned wordCount = firstWord >> WordCountShift;
  143. Op opCode = (Op)(firstWord & OpCodeMask);
  144. int nextInst = word + wordCount;
  145. ++word;
  146. // Presence of full instruction
  147. if (nextInst > size)
  148. Kill(out, "stream instruction terminated too early");
  149. // Base for computing number of operands; will be updated as more is learned
  150. unsigned numOperands = wordCount - 1;
  151. // Type <id>
  152. Id typeId = 0;
  153. if (InstructionDesc[opCode].hasType()) {
  154. typeId = stream[word++];
  155. --numOperands;
  156. }
  157. // Result <id>
  158. Id resultId = 0;
  159. if (InstructionDesc[opCode].hasResult()) {
  160. resultId = stream[word++];
  161. --numOperands;
  162. // save instruction for future reference
  163. idInstruction[resultId] = instructionStart;
  164. }
  165. outputResultId(resultId);
  166. outputTypeId(typeId);
  167. outputIndent();
  168. // Hand off the Op and all its operands
  169. disassembleInstruction(resultId, typeId, opCode, numOperands);
  170. if (word != nextInst) {
  171. out << " ERROR, incorrect number of operands consumed. At " << word << " instead of " << nextInst << " instruction start was " << instructionStart;
  172. word = nextInst;
  173. }
  174. out << std::endl;
  175. }
  176. }
  177. void SpirvStream::outputIndent()
  178. {
  179. for (int i = 0; i < (int)nestedControl.size(); ++i)
  180. out << " ";
  181. }
  182. void SpirvStream::formatId(Id id, std::stringstream& idStream)
  183. {
  184. if (id != 0) {
  185. // On instructions with no IDs, this is called with "0", which does not
  186. // have to be within ID bounds on null shaders.
  187. if (id >= bound)
  188. Kill(out, "Bad <id>");
  189. idStream << id;
  190. if (idDescriptor[id].size() > 0)
  191. idStream << "(" << idDescriptor[id] << ")";
  192. }
  193. }
  194. void SpirvStream::outputResultId(Id id)
  195. {
  196. const int width = 16;
  197. std::stringstream idStream;
  198. formatId(id, idStream);
  199. out << std::setw(width) << std::right << idStream.str();
  200. if (id != 0)
  201. out << ":";
  202. else
  203. out << " ";
  204. if (nestedControl.size() && id == nestedControl.top())
  205. nestedControl.pop();
  206. }
  207. void SpirvStream::outputTypeId(Id id)
  208. {
  209. const int width = 12;
  210. std::stringstream idStream;
  211. formatId(id, idStream);
  212. out << std::setw(width) << std::right << idStream.str() << " ";
  213. }
  214. void SpirvStream::outputId(Id id)
  215. {
  216. if (id >= bound)
  217. Kill(out, "Bad <id>");
  218. out << id;
  219. if (idDescriptor[id].size() > 0)
  220. out << "(" << idDescriptor[id] << ")";
  221. }
  222. void SpirvStream::outputMask(OperandClass operandClass, unsigned mask)
  223. {
  224. if (mask == 0)
  225. out << "None";
  226. else {
  227. for (int m = 0; m < OperandClassParams[operandClass].ceiling; ++m) {
  228. if (mask & (1 << m))
  229. out << OperandClassParams[operandClass].getName(m) << " ";
  230. }
  231. }
  232. }
  233. void SpirvStream::disassembleImmediates(int numOperands)
  234. {
  235. for (int i = 0; i < numOperands; ++i) {
  236. out << stream[word++];
  237. if (i < numOperands - 1)
  238. out << " ";
  239. }
  240. }
  241. void SpirvStream::disassembleIds(int numOperands)
  242. {
  243. for (int i = 0; i < numOperands; ++i) {
  244. outputId(stream[word++]);
  245. if (i < numOperands - 1)
  246. out << " ";
  247. }
  248. }
  249. // return the number of operands consumed by the string
  250. int SpirvStream::disassembleString()
  251. {
  252. int startWord = word;
  253. out << " \"";
  254. const char* wordString;
  255. bool done = false;
  256. do {
  257. unsigned int content = stream[word];
  258. wordString = (const char*)&content;
  259. for (int charCount = 0; charCount < 4; ++charCount) {
  260. if (*wordString == 0) {
  261. done = true;
  262. break;
  263. }
  264. out << *(wordString++);
  265. }
  266. ++word;
  267. } while (! done);
  268. out << "\"";
  269. return word - startWord;
  270. }
  271. void SpirvStream::disassembleInstruction(Id resultId, Id /*typeId*/, Op opCode, int numOperands)
  272. {
  273. // Process the opcode
  274. out << (OpcodeString(opCode) + 2); // leave out the "Op"
  275. if (opCode == OpLoopMerge || opCode == OpSelectionMerge)
  276. nextNestedControl = stream[word];
  277. else if (opCode == OpBranchConditional || opCode == OpSwitch) {
  278. if (nextNestedControl) {
  279. nestedControl.push(nextNestedControl);
  280. nextNestedControl = 0;
  281. }
  282. } else if (opCode == OpExtInstImport) {
  283. idDescriptor[resultId] = (const char*)(&stream[word]);
  284. }
  285. else {
  286. if (resultId != 0 && idDescriptor[resultId].size() == 0) {
  287. switch (opCode) {
  288. case OpTypeInt:
  289. switch (stream[word]) {
  290. case 8: idDescriptor[resultId] = "int8_t"; break;
  291. case 16: idDescriptor[resultId] = "int16_t"; break;
  292. default: assert(0); // fallthrough
  293. case 32: idDescriptor[resultId] = "int"; break;
  294. case 64: idDescriptor[resultId] = "int64_t"; break;
  295. }
  296. break;
  297. case OpTypeFloat:
  298. switch (stream[word]) {
  299. case 16: idDescriptor[resultId] = "float16_t"; break;
  300. default: assert(0); // fallthrough
  301. case 32: idDescriptor[resultId] = "float"; break;
  302. case 64: idDescriptor[resultId] = "float64_t"; break;
  303. }
  304. break;
  305. case OpTypeBool:
  306. idDescriptor[resultId] = "bool";
  307. break;
  308. case OpTypeStruct:
  309. idDescriptor[resultId] = "struct";
  310. break;
  311. case OpTypePointer:
  312. idDescriptor[resultId] = "ptr";
  313. break;
  314. case OpTypeVector:
  315. if (idDescriptor[stream[word]].size() > 0) {
  316. idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 1);
  317. if (strstr(idDescriptor[stream[word]].c_str(), "8")) {
  318. idDescriptor[resultId].append("8");
  319. }
  320. if (strstr(idDescriptor[stream[word]].c_str(), "16")) {
  321. idDescriptor[resultId].append("16");
  322. }
  323. if (strstr(idDescriptor[stream[word]].c_str(), "64")) {
  324. idDescriptor[resultId].append("64");
  325. }
  326. }
  327. idDescriptor[resultId].append("vec");
  328. switch (stream[word + 1]) {
  329. case 2: idDescriptor[resultId].append("2"); break;
  330. case 3: idDescriptor[resultId].append("3"); break;
  331. case 4: idDescriptor[resultId].append("4"); break;
  332. case 8: idDescriptor[resultId].append("8"); break;
  333. case 16: idDescriptor[resultId].append("16"); break;
  334. case 32: idDescriptor[resultId].append("32"); break;
  335. default: break;
  336. }
  337. break;
  338. default:
  339. break;
  340. }
  341. }
  342. }
  343. // Process the operands. Note, a new context-dependent set could be
  344. // swapped in mid-traversal.
  345. // Handle images specially, so can put out helpful strings.
  346. if (opCode == OpTypeImage) {
  347. out << " ";
  348. disassembleIds(1);
  349. out << " " << DimensionString((Dim)stream[word++]);
  350. out << (stream[word++] != 0 ? " depth" : "");
  351. out << (stream[word++] != 0 ? " array" : "");
  352. out << (stream[word++] != 0 ? " multi-sampled" : "");
  353. switch (stream[word++]) {
  354. case 0: out << " runtime"; break;
  355. case 1: out << " sampled"; break;
  356. case 2: out << " nonsampled"; break;
  357. }
  358. out << " format:" << ImageFormatString((ImageFormat)stream[word++]);
  359. if (numOperands == 8) {
  360. out << " " << AccessQualifierString(stream[word++]);
  361. }
  362. return;
  363. }
  364. // Handle all the parameterized operands
  365. for (int op = 0; op < InstructionDesc[opCode].operands.getNum() && numOperands > 0; ++op) {
  366. out << " ";
  367. OperandClass operandClass = InstructionDesc[opCode].operands.getClass(op);
  368. switch (operandClass) {
  369. case OperandId:
  370. case OperandScope:
  371. case OperandMemorySemantics:
  372. disassembleIds(1);
  373. --numOperands;
  374. // Get names for printing "(XXX)" for readability, *after* this id
  375. if (opCode == OpName)
  376. idDescriptor[stream[word - 1]] = (const char*)(&stream[word]);
  377. break;
  378. case OperandVariableIds:
  379. disassembleIds(numOperands);
  380. return;
  381. case OperandImageOperands:
  382. outputMask(OperandImageOperands, stream[word++]);
  383. --numOperands;
  384. disassembleIds(numOperands);
  385. return;
  386. case OperandOptionalLiteral:
  387. case OperandVariableLiterals:
  388. if ((opCode == OpDecorate && stream[word - 1] == DecorationBuiltIn) ||
  389. (opCode == OpMemberDecorate && stream[word - 1] == DecorationBuiltIn)) {
  390. out << BuiltInString(stream[word++]);
  391. --numOperands;
  392. ++op;
  393. }
  394. disassembleImmediates(numOperands);
  395. return;
  396. case OperandVariableIdLiteral:
  397. while (numOperands > 0) {
  398. out << std::endl;
  399. outputResultId(0);
  400. outputTypeId(0);
  401. outputIndent();
  402. out << " Type ";
  403. disassembleIds(1);
  404. out << ", member ";
  405. disassembleImmediates(1);
  406. numOperands -= 2;
  407. }
  408. return;
  409. case OperandVariableLiteralId:
  410. while (numOperands > 0) {
  411. out << std::endl;
  412. outputResultId(0);
  413. outputTypeId(0);
  414. outputIndent();
  415. out << " case ";
  416. disassembleImmediates(1);
  417. out << ": ";
  418. disassembleIds(1);
  419. numOperands -= 2;
  420. }
  421. return;
  422. case OperandLiteralNumber:
  423. disassembleImmediates(1);
  424. --numOperands;
  425. if (opCode == OpExtInst) {
  426. ExtInstSet extInstSet = GLSL450Inst;
  427. const char* name = idDescriptor[stream[word - 2]].c_str();
  428. if (strcmp("OpenCL.std", name) == 0) {
  429. extInstSet = OpenCLExtInst;
  430. } else if (strcmp("OpenCL.DebugInfo.100", name) == 0) {
  431. extInstSet = OpenCLExtInst;
  432. } else if (strcmp("NonSemantic.DebugPrintf", name) == 0) {
  433. extInstSet = NonSemanticDebugPrintfExtInst;
  434. } else if (strcmp(spv::E_SPV_AMD_shader_ballot, name) == 0 ||
  435. strcmp(spv::E_SPV_AMD_shader_trinary_minmax, name) == 0 ||
  436. strcmp(spv::E_SPV_AMD_shader_explicit_vertex_parameter, name) == 0 ||
  437. strcmp(spv::E_SPV_AMD_gcn_shader, name) == 0) {
  438. extInstSet = GLSLextAMDInst;
  439. } else if (strcmp(spv::E_SPV_NV_sample_mask_override_coverage, name) == 0 ||
  440. strcmp(spv::E_SPV_NV_geometry_shader_passthrough, name) == 0 ||
  441. strcmp(spv::E_SPV_NV_viewport_array2, name) == 0 ||
  442. strcmp(spv::E_SPV_NVX_multiview_per_view_attributes, name) == 0 ||
  443. strcmp(spv::E_SPV_NV_fragment_shader_barycentric, name) == 0 ||
  444. strcmp(spv::E_SPV_NV_mesh_shader, name) == 0) {
  445. extInstSet = GLSLextNVInst;
  446. }
  447. unsigned entrypoint = stream[word - 1];
  448. if (extInstSet == GLSL450Inst) {
  449. if (entrypoint < GLSLstd450Count) {
  450. out << "(" << GlslStd450DebugNames[entrypoint] << ")";
  451. }
  452. } else if (extInstSet == GLSLextAMDInst) {
  453. out << "(" << GLSLextAMDGetDebugNames(name, entrypoint) << ")";
  454. }
  455. else if (extInstSet == GLSLextNVInst) {
  456. out << "(" << GLSLextNVGetDebugNames(name, entrypoint) << ")";
  457. } else if (extInstSet == NonSemanticDebugPrintfExtInst) {
  458. out << "(DebugPrintf)";
  459. }
  460. }
  461. break;
  462. case OperandOptionalLiteralString:
  463. case OperandLiteralString:
  464. numOperands -= disassembleString();
  465. break;
  466. case OperandVariableLiteralStrings:
  467. while (numOperands > 0)
  468. numOperands -= disassembleString();
  469. return;
  470. case OperandMemoryAccess:
  471. outputMask(OperandMemoryAccess, stream[word++]);
  472. --numOperands;
  473. // Aligned is the only memory access operand that uses an immediate
  474. // value, and it is also the first operand that uses a value at all.
  475. if (stream[word-1] & MemoryAccessAlignedMask) {
  476. disassembleImmediates(1);
  477. numOperands--;
  478. if (numOperands)
  479. out << " ";
  480. }
  481. disassembleIds(numOperands);
  482. return;
  483. default:
  484. assert(operandClass >= OperandSource && operandClass < OperandOpcode);
  485. if (OperandClassParams[operandClass].bitmask)
  486. outputMask(operandClass, stream[word++]);
  487. else
  488. out << OperandClassParams[operandClass].getName(stream[word++]);
  489. --numOperands;
  490. break;
  491. }
  492. }
  493. return;
  494. }
  495. static void GLSLstd450GetDebugNames(const char** names)
  496. {
  497. for (int i = 0; i < GLSLstd450Count; ++i)
  498. names[i] = "Unknown";
  499. names[GLSLstd450Round] = "Round";
  500. names[GLSLstd450RoundEven] = "RoundEven";
  501. names[GLSLstd450Trunc] = "Trunc";
  502. names[GLSLstd450FAbs] = "FAbs";
  503. names[GLSLstd450SAbs] = "SAbs";
  504. names[GLSLstd450FSign] = "FSign";
  505. names[GLSLstd450SSign] = "SSign";
  506. names[GLSLstd450Floor] = "Floor";
  507. names[GLSLstd450Ceil] = "Ceil";
  508. names[GLSLstd450Fract] = "Fract";
  509. names[GLSLstd450Radians] = "Radians";
  510. names[GLSLstd450Degrees] = "Degrees";
  511. names[GLSLstd450Sin] = "Sin";
  512. names[GLSLstd450Cos] = "Cos";
  513. names[GLSLstd450Tan] = "Tan";
  514. names[GLSLstd450Asin] = "Asin";
  515. names[GLSLstd450Acos] = "Acos";
  516. names[GLSLstd450Atan] = "Atan";
  517. names[GLSLstd450Sinh] = "Sinh";
  518. names[GLSLstd450Cosh] = "Cosh";
  519. names[GLSLstd450Tanh] = "Tanh";
  520. names[GLSLstd450Asinh] = "Asinh";
  521. names[GLSLstd450Acosh] = "Acosh";
  522. names[GLSLstd450Atanh] = "Atanh";
  523. names[GLSLstd450Atan2] = "Atan2";
  524. names[GLSLstd450Pow] = "Pow";
  525. names[GLSLstd450Exp] = "Exp";
  526. names[GLSLstd450Log] = "Log";
  527. names[GLSLstd450Exp2] = "Exp2";
  528. names[GLSLstd450Log2] = "Log2";
  529. names[GLSLstd450Sqrt] = "Sqrt";
  530. names[GLSLstd450InverseSqrt] = "InverseSqrt";
  531. names[GLSLstd450Determinant] = "Determinant";
  532. names[GLSLstd450MatrixInverse] = "MatrixInverse";
  533. names[GLSLstd450Modf] = "Modf";
  534. names[GLSLstd450ModfStruct] = "ModfStruct";
  535. names[GLSLstd450FMin] = "FMin";
  536. names[GLSLstd450SMin] = "SMin";
  537. names[GLSLstd450UMin] = "UMin";
  538. names[GLSLstd450FMax] = "FMax";
  539. names[GLSLstd450SMax] = "SMax";
  540. names[GLSLstd450UMax] = "UMax";
  541. names[GLSLstd450FClamp] = "FClamp";
  542. names[GLSLstd450SClamp] = "SClamp";
  543. names[GLSLstd450UClamp] = "UClamp";
  544. names[GLSLstd450FMix] = "FMix";
  545. names[GLSLstd450Step] = "Step";
  546. names[GLSLstd450SmoothStep] = "SmoothStep";
  547. names[GLSLstd450Fma] = "Fma";
  548. names[GLSLstd450Frexp] = "Frexp";
  549. names[GLSLstd450FrexpStruct] = "FrexpStruct";
  550. names[GLSLstd450Ldexp] = "Ldexp";
  551. names[GLSLstd450PackSnorm4x8] = "PackSnorm4x8";
  552. names[GLSLstd450PackUnorm4x8] = "PackUnorm4x8";
  553. names[GLSLstd450PackSnorm2x16] = "PackSnorm2x16";
  554. names[GLSLstd450PackUnorm2x16] = "PackUnorm2x16";
  555. names[GLSLstd450PackHalf2x16] = "PackHalf2x16";
  556. names[GLSLstd450PackDouble2x32] = "PackDouble2x32";
  557. names[GLSLstd450UnpackSnorm2x16] = "UnpackSnorm2x16";
  558. names[GLSLstd450UnpackUnorm2x16] = "UnpackUnorm2x16";
  559. names[GLSLstd450UnpackHalf2x16] = "UnpackHalf2x16";
  560. names[GLSLstd450UnpackSnorm4x8] = "UnpackSnorm4x8";
  561. names[GLSLstd450UnpackUnorm4x8] = "UnpackUnorm4x8";
  562. names[GLSLstd450UnpackDouble2x32] = "UnpackDouble2x32";
  563. names[GLSLstd450Length] = "Length";
  564. names[GLSLstd450Distance] = "Distance";
  565. names[GLSLstd450Cross] = "Cross";
  566. names[GLSLstd450Normalize] = "Normalize";
  567. names[GLSLstd450FaceForward] = "FaceForward";
  568. names[GLSLstd450Reflect] = "Reflect";
  569. names[GLSLstd450Refract] = "Refract";
  570. names[GLSLstd450FindILsb] = "FindILsb";
  571. names[GLSLstd450FindSMsb] = "FindSMsb";
  572. names[GLSLstd450FindUMsb] = "FindUMsb";
  573. names[GLSLstd450InterpolateAtCentroid] = "InterpolateAtCentroid";
  574. names[GLSLstd450InterpolateAtSample] = "InterpolateAtSample";
  575. names[GLSLstd450InterpolateAtOffset] = "InterpolateAtOffset";
  576. names[GLSLstd450NMin] = "NMin";
  577. names[GLSLstd450NMax] = "NMax";
  578. names[GLSLstd450NClamp] = "NClamp";
  579. }
  580. static const char* GLSLextAMDGetDebugNames(const char* name, unsigned entrypoint)
  581. {
  582. if (strcmp(name, spv::E_SPV_AMD_shader_ballot) == 0) {
  583. switch (entrypoint) {
  584. case SwizzleInvocationsAMD: return "SwizzleInvocationsAMD";
  585. case SwizzleInvocationsMaskedAMD: return "SwizzleInvocationsMaskedAMD";
  586. case WriteInvocationAMD: return "WriteInvocationAMD";
  587. case MbcntAMD: return "MbcntAMD";
  588. default: return "Bad";
  589. }
  590. } else if (strcmp(name, spv::E_SPV_AMD_shader_trinary_minmax) == 0) {
  591. switch (entrypoint) {
  592. case FMin3AMD: return "FMin3AMD";
  593. case UMin3AMD: return "UMin3AMD";
  594. case SMin3AMD: return "SMin3AMD";
  595. case FMax3AMD: return "FMax3AMD";
  596. case UMax3AMD: return "UMax3AMD";
  597. case SMax3AMD: return "SMax3AMD";
  598. case FMid3AMD: return "FMid3AMD";
  599. case UMid3AMD: return "UMid3AMD";
  600. case SMid3AMD: return "SMid3AMD";
  601. default: return "Bad";
  602. }
  603. } else if (strcmp(name, spv::E_SPV_AMD_shader_explicit_vertex_parameter) == 0) {
  604. switch (entrypoint) {
  605. case InterpolateAtVertexAMD: return "InterpolateAtVertexAMD";
  606. default: return "Bad";
  607. }
  608. }
  609. else if (strcmp(name, spv::E_SPV_AMD_gcn_shader) == 0) {
  610. switch (entrypoint) {
  611. case CubeFaceIndexAMD: return "CubeFaceIndexAMD";
  612. case CubeFaceCoordAMD: return "CubeFaceCoordAMD";
  613. case TimeAMD: return "TimeAMD";
  614. default:
  615. break;
  616. }
  617. }
  618. return "Bad";
  619. }
  620. static const char* GLSLextNVGetDebugNames(const char* name, unsigned entrypoint)
  621. {
  622. if (strcmp(name, spv::E_SPV_NV_sample_mask_override_coverage) == 0 ||
  623. strcmp(name, spv::E_SPV_NV_geometry_shader_passthrough) == 0 ||
  624. strcmp(name, spv::E_ARB_shader_viewport_layer_array) == 0 ||
  625. strcmp(name, spv::E_SPV_NV_viewport_array2) == 0 ||
  626. strcmp(name, spv::E_SPV_NVX_multiview_per_view_attributes) == 0 ||
  627. strcmp(name, spv::E_SPV_NV_fragment_shader_barycentric) == 0 ||
  628. strcmp(name, spv::E_SPV_NV_mesh_shader) == 0 ||
  629. strcmp(name, spv::E_SPV_NV_shader_image_footprint) == 0) {
  630. switch (entrypoint) {
  631. // NV builtins
  632. case BuiltInViewportMaskNV: return "ViewportMaskNV";
  633. case BuiltInSecondaryPositionNV: return "SecondaryPositionNV";
  634. case BuiltInSecondaryViewportMaskNV: return "SecondaryViewportMaskNV";
  635. case BuiltInPositionPerViewNV: return "PositionPerViewNV";
  636. case BuiltInViewportMaskPerViewNV: return "ViewportMaskPerViewNV";
  637. case BuiltInBaryCoordNV: return "BaryCoordNV";
  638. case BuiltInBaryCoordNoPerspNV: return "BaryCoordNoPerspNV";
  639. case BuiltInTaskCountNV: return "TaskCountNV";
  640. case BuiltInPrimitiveCountNV: return "PrimitiveCountNV";
  641. case BuiltInPrimitiveIndicesNV: return "PrimitiveIndicesNV";
  642. case BuiltInClipDistancePerViewNV: return "ClipDistancePerViewNV";
  643. case BuiltInCullDistancePerViewNV: return "CullDistancePerViewNV";
  644. case BuiltInLayerPerViewNV: return "LayerPerViewNV";
  645. case BuiltInMeshViewCountNV: return "MeshViewCountNV";
  646. case BuiltInMeshViewIndicesNV: return "MeshViewIndicesNV";
  647. // NV Capabilities
  648. case CapabilityGeometryShaderPassthroughNV: return "GeometryShaderPassthroughNV";
  649. case CapabilityShaderViewportMaskNV: return "ShaderViewportMaskNV";
  650. case CapabilityShaderStereoViewNV: return "ShaderStereoViewNV";
  651. case CapabilityPerViewAttributesNV: return "PerViewAttributesNV";
  652. case CapabilityFragmentBarycentricNV: return "FragmentBarycentricNV";
  653. case CapabilityMeshShadingNV: return "MeshShadingNV";
  654. case CapabilityImageFootprintNV: return "ImageFootprintNV";
  655. case CapabilitySampleMaskOverrideCoverageNV:return "SampleMaskOverrideCoverageNV";
  656. // NV Decorations
  657. case DecorationOverrideCoverageNV: return "OverrideCoverageNV";
  658. case DecorationPassthroughNV: return "PassthroughNV";
  659. case DecorationViewportRelativeNV: return "ViewportRelativeNV";
  660. case DecorationSecondaryViewportRelativeNV: return "SecondaryViewportRelativeNV";
  661. case DecorationPerVertexNV: return "PerVertexNV";
  662. case DecorationPerPrimitiveNV: return "PerPrimitiveNV";
  663. case DecorationPerViewNV: return "PerViewNV";
  664. case DecorationPerTaskNV: return "PerTaskNV";
  665. default: return "Bad";
  666. }
  667. }
  668. return "Bad";
  669. }
  670. void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
  671. {
  672. SpirvStream SpirvStream(out, stream);
  673. spv::Parameterize();
  674. GLSLstd450GetDebugNames(GlslStd450DebugNames);
  675. SpirvStream.validate();
  676. SpirvStream.processInstructions();
  677. }
  678. }; // end namespace spv