disassemble.cpp 29 KB

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