yum-mirror/glslang

git clone https://git.yummers.dev/yum-mirror/glslang

Jeff BolzImplement GL_EXT_ocp_microscaling_types62f5430e

vulkan-sdk-1.4.357.0
38.7 KiB909 linesraw
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//
36// Disassembler for SPIR-V.
37//
38
39#include <cstdint>
40#include <cstdlib>
41#include <cstring>
42#include <cassert>
43#include <iomanip>
44#include <stack>
45#include <sstream>
46#include <cstring>
47#include <utility>
48
49#include "disassemble.h"
50#include "doc.h"
51#include "spvUtil.h"
52
53namespace spv {
54    extern "C" {
55        // Include C-based headers that don't have a namespace
56        #include "GLSL.std.450.h"
57        #include "GLSL.ext.AMD.h"
58        #include "GLSL.ext.NV.h"
59        #include "GLSL.ext.ARM.h"
60        #include "NonSemanticShaderDebugInfo.h"
61        #include "GLSL.ext.QCOM.h"
62    }
63}
64static const char* GlslStd450DebugNames[spv::GLSLstd450Count];
65
66namespace spv {
67
68static const char* GLSLextAMDGetDebugNames(const char*, unsigned);
69static const char* GLSLextNVGetDebugNames(const char*, unsigned);
70static const char* NonSemanticShaderDebugInfoGetDebugNames(unsigned);
71
72static void Kill(std::ostream& out, const char* message)
73{
74    out << std::endl << "Disassembly failed: " << message << std::endl;
75    exit(1);
76}
77
78// used to identify the extended instruction library imported when printing
79enum ExtInstSet {
80    GLSL450Inst,
81    GLSLextAMDInst,
82    GLSLextNVInst,
83    OpenCLExtInst,
84    NonSemanticDebugPrintfExtInst,
85    NonSemanticDebugBreakExtInst,
86    NonSemanticShaderDebugInfo
87};
88
89// Container class for a single instance of a SPIR-V stream, with methods for disassembly.
90class SpirvStream {
91public:
92    SpirvStream(std::ostream& out, const std::vector<unsigned int>& stream) : out(out), stream(stream), word(0), nextNestedControl(0) { }
93    virtual ~SpirvStream() { }
94
95    void validate();
96    void processInstructions();
97
98protected:
99    SpirvStream(const SpirvStream&);
100    SpirvStream& operator=(const SpirvStream&);
101    Op getOpCode(int id) const { return idInstruction[id] ? (Op)(stream[idInstruction[id]] & OpCodeMask) : Op::OpNop; }
102
103    // Output methods
104    void outputIndent();
105    void formatId(Id id, std::stringstream&);
106    void outputResultId(Id id);
107    void outputTypeId(Id id);
108    void outputId(Id id);
109    void outputMask(OperandClass operandClass, unsigned mask);
110    void disassembleImmediates(int numOperands);
111    void disassembleIds(int numOperands);
112    std::pair<int, std::string> decodeString();
113    int disassembleString();
114    void disassembleInstruction(Id resultId, Id typeId, Op opCode, int numOperands);
115
116    // Data
117    std::ostream& out;                       // where to write the disassembly
118    const std::vector<unsigned int>& stream; // the actual word stream
119    int size;                                // the size of the word stream
120    int word;                                // the next word of the stream to read
121
122    // map each <id> to the instruction that created it
123    Id bound;
124    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)
125
126    std::vector<std::string> idDescriptor;    // the best text string known for explaining the <id>
127
128    // schema
129    unsigned int schema;
130
131    // stack of structured-merge points
132    std::stack<Id> nestedControl;
133    Id nextNestedControl;         // need a slight delay for when we are nested
134};
135
136void SpirvStream::validate()
137{
138    size = (int)stream.size();
139    if (size < 4)
140        Kill(out, "stream is too short");
141
142    // Magic number
143    if (stream[word++] != MagicNumber) {
144        out << "Bad magic number";
145        return;
146    }
147
148    // Version
149    out << "// Module Version " << std::hex << stream[word++] << std::endl;
150
151    // Generator's magic number
152    out << "// Generated by (magic number): " << std::hex << stream[word++] << std::dec << std::endl;
153
154    // Result <id> bound
155    bound = stream[word++];
156    idInstruction.resize(bound);
157    idDescriptor.resize(bound);
158    out << "// Id's are bound by " << bound << std::endl;
159    out << std::endl;
160
161    // Reserved schema, must be 0 for now
162    schema = stream[word++];
163    if (schema != 0)
164        Kill(out, "bad schema, must be 0");
165}
166
167// Loop over all the instructions, in order, processing each.
168// Boiler plate for each is handled here directly, the rest is dispatched.
169void SpirvStream::processInstructions()
170{
171    // Instructions
172    while (word < size) {
173        int instructionStart = word;
174
175        // Instruction wordCount and opcode
176        unsigned int firstWord = stream[word];
177        unsigned wordCount = firstWord >> WordCountShift;
178        Op opCode = (Op)(firstWord & OpCodeMask);
179        int nextInst = word + wordCount;
180        ++word;
181
182        // Presence of full instruction
183        if (nextInst > size)
184            Kill(out, "stream instruction terminated too early");
185
186        // Base for computing number of operands; will be updated as more is learned
187        unsigned numOperands = wordCount - 1;
188
189        // Type <id>
190        Id typeId = 0;
191        if (InstructionDesc[enumCast(opCode)].hasType()) {
192            typeId = stream[word++];
193            --numOperands;
194        }
195
196        // Result <id>
197        Id resultId = 0;
198        if (InstructionDesc[enumCast(opCode)].hasResult()) {
199            resultId = stream[word++];
200            --numOperands;
201
202            // save instruction for future reference
203            idInstruction[resultId] = instructionStart;
204        }
205
206        outputResultId(resultId);
207        outputTypeId(typeId);
208        outputIndent();
209
210        // Hand off the Op and all its operands
211        disassembleInstruction(resultId, typeId, opCode, numOperands);
212        if (word != nextInst) {
213            out << " ERROR, incorrect number of operands consumed.  At " << word << " instead of " << nextInst << " instruction start was " << instructionStart;
214            word = nextInst;
215        }
216        out << std::endl;
217    }
218}
219
220void SpirvStream::outputIndent()
221{
222    for (int i = 0; i < (int)nestedControl.size(); ++i)
223        out << "  ";
224}
225
226void SpirvStream::formatId(Id id, std::stringstream& idStream)
227{
228    if (id != 0) {
229        // On instructions with no IDs, this is called with "0", which does not
230        // have to be within ID bounds on null shaders.
231        if (id >= bound)
232            Kill(out, "Bad <id>");
233
234        idStream << id;
235        if (idDescriptor[id].size() > 0)
236            idStream << "(" << idDescriptor[id] << ")";
237    }
238}
239
240void SpirvStream::outputResultId(Id id)
241{
242    const int width = 16;
243    std::stringstream idStream;
244    formatId(id, idStream);
245    out << std::setw(width) << std::right << idStream.str();
246    if (id != 0)
247        out << ":";
248    else
249        out << " ";
250
251    if (nestedControl.size() && id == nestedControl.top())
252        nestedControl.pop();
253}
254
255void SpirvStream::outputTypeId(Id id)
256{
257    const int width = 12;
258    std::stringstream idStream;
259    formatId(id, idStream);
260    out << std::setw(width) << std::right << idStream.str() << " ";
261}
262
263void SpirvStream::outputId(Id id)
264{
265    if (id >= bound)
266        Kill(out, "Bad <id>");
267
268    out << id;
269    if (idDescriptor[id].size() > 0)
270        out << "(" << idDescriptor[id] << ")";
271}
272
273void SpirvStream::outputMask(OperandClass operandClass, unsigned mask)
274{
275    if (mask == 0)
276        out << "None";
277    else {
278        for (int m = 0; m < OperandClassParams[operandClass].ceiling; ++m) {
279            if (mask & (1 << m))
280                out << OperandClassParams[operandClass].getName(m) << " ";
281        }
282    }
283}
284
285void SpirvStream::disassembleImmediates(int numOperands)
286{
287    for (int i = 0; i < numOperands; ++i) {
288        out << stream[word++];
289        if (i < numOperands - 1)
290            out << " ";
291    }
292}
293
294void SpirvStream::disassembleIds(int numOperands)
295{
296    for (int i = 0; i < numOperands; ++i) {
297        outputId(stream[word++]);
298        if (i < numOperands - 1)
299            out << " ";
300    }
301}
302
303// decode string from words at current position (non-consuming)
304std::pair<int, std::string> SpirvStream::decodeString()
305{
306    std::string res;
307    int wordPos = word;
308    char c;
309    bool done = false;
310
311    do {
312        unsigned int content = stream[wordPos];
313        for (int charCount = 0; charCount < 4; ++charCount) {
314            c = content & 0xff;
315            content >>= 8;
316            if (c == '\0') {
317                done = true;
318                break;
319            }
320            res += c;
321        }
322        ++wordPos;
323    } while(! done);
324
325    return std::make_pair(wordPos - word, res);
326}
327
328// return the number of operands consumed by the string
329int SpirvStream::disassembleString()
330{
331    out << " \"";
332
333    std::pair<int, std::string> decoderes = decodeString();
334
335    out << decoderes.second;
336    out << "\"";
337
338    word += decoderes.first;
339
340    return decoderes.first;
341}
342
343static uint32_t popcount(uint32_t mask)
344{
345    uint32_t count = 0;
346    while (mask) {
347        if (mask & 1) {
348            count++;
349        }
350        mask >>= 1;
351    }
352    return count;
353}
354
355void SpirvStream::disassembleInstruction(Id resultId, Id /*typeId*/, Op opCode, int numOperands)
356{
357    // Process the opcode
358
359    out << (OpcodeString((int)opCode) + 2);  // leave out the "Op"
360
361    if (opCode == Op::OpLoopMerge || opCode == Op::OpSelectionMerge)
362        nextNestedControl = stream[word];
363    else if (opCode == Op::OpBranchConditional || opCode == Op::OpSwitch) {
364        if (nextNestedControl) {
365            nestedControl.push(nextNestedControl);
366            nextNestedControl = 0;
367        }
368    } else if (opCode == Op::OpExtInstImport) {
369        idDescriptor[resultId] = decodeString().second;
370    }
371    else {
372        if (resultId != 0 && idDescriptor[resultId].size() == 0) {
373            switch (opCode) {
374            case Op::OpTypeInt:
375                switch (stream[word]) {
376                case 8:  idDescriptor[resultId] = "int8_t"; break;
377                case 16: idDescriptor[resultId] = "int16_t"; break;
378                default: assert(0); [[fallthrough]];
379                case 32: idDescriptor[resultId] = "int"; break;
380                case 64: idDescriptor[resultId] = "int64_t"; break;
381                }
382                break;
383            case Op::OpTypeFloat:
384                switch (stream[word]) {
385                case 4:
386                case 6:
387                case 8:
388                case 16:
389                    if (numOperands > 1) {
390                        switch (stream[word+1]) {
391                        default:
392                            assert(0); [[fallthrough]];
393                        case (int)spv::FPEncoding::BFloat16KHR:
394                            idDescriptor[resultId] = "bfloat16_t";
395                            break;
396                        case (int)spv::FPEncoding::Float8E4M3EXT:
397                            idDescriptor[resultId] = "floate4m3_t";
398                            break;
399                        case (int)spv::FPEncoding::Float8E5M2EXT:
400                            idDescriptor[resultId] = "floate5m2_t";
401                            break;
402                        case (int)spv::FPEncoding::Float4E2M1EXT:
403                            idDescriptor[resultId] = "floate2m1_t";
404                            break;
405                        case (int)spv::FPEncoding::Float6E3M2EXT:
406                            idDescriptor[resultId] = "floate3m2_t";
407                            break;
408                        case (int)spv::FPEncoding::Float6E2M3EXT:
409                            idDescriptor[resultId] = "floate2m3_t";
410                            break;
411                        case (int)spv::FPEncoding::Float8UnsignedE8M0EXT:
412                            idDescriptor[resultId] = "floatue8m0_t";
413                            break;
414                        case (int)spv::FPEncoding::MXInt8EXT:
415                            idDescriptor[resultId] = "floatmxint8_t";
416                            break;
417                        }
418                    } else {
419                        idDescriptor[resultId] = "float16_t";
420                    }
421                    break;
422                default: assert(0); [[fallthrough]];
423                case 32: idDescriptor[resultId] = "float"; break;
424                case 64: idDescriptor[resultId] = "float64_t"; break;
425                }
426                break;
427            case Op::OpTypeBool:
428                idDescriptor[resultId] = "bool";
429                break;
430            case Op::OpTypeStruct:
431                idDescriptor[resultId] = "struct";
432                break;
433            case Op::OpTypePointer:
434            case Op::OpTypeUntypedPointerKHR:
435                idDescriptor[resultId] = "ptr";
436                break;
437            case Op::OpTypeVector:
438                if (idDescriptor[stream[word]].size() > 0) {
439                    if (idDescriptor[stream[word]].substr(0,2) == "bf") {
440                        idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 2);
441                    } else {
442                        idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 1);
443                    }
444                    if (strstr(idDescriptor[stream[word]].c_str(), "8")) {
445                        idDescriptor[resultId].append("8");
446                    }
447                    if (strstr(idDescriptor[stream[word]].c_str(), "16")) {
448                        idDescriptor[resultId].append("16");
449                    }
450                    if (strstr(idDescriptor[stream[word]].c_str(), "64")) {
451                        idDescriptor[resultId].append("64");
452                    }
453                }
454                idDescriptor[resultId].append("vec");
455                switch (stream[word + 1]) {
456                case 2:   idDescriptor[resultId].append("2");   break;
457                case 3:   idDescriptor[resultId].append("3");   break;
458                case 4:   idDescriptor[resultId].append("4");   break;
459                case 8:   idDescriptor[resultId].append("8");   break;
460                case 16:  idDescriptor[resultId].append("16");  break;
461                case 32:  idDescriptor[resultId].append("32");  break;
462                default: break;
463                }
464                break;
465            default:
466                break;
467            }
468        }
469    }
470
471    // Process the operands.  Note, a new context-dependent set could be
472    // swapped in mid-traversal.
473
474    // Handle images specially, so can put out helpful strings.
475    if (opCode == Op::OpTypeImage) {
476        out << " ";
477        disassembleIds(1);
478        out << " " << DimensionString((int)(Dim)stream[word++]);
479        out << (stream[word++] != 0 ? " depth" : "");
480        out << (stream[word++] != 0 ? " array" : "");
481        out << (stream[word++] != 0 ? " multi-sampled" : "");
482        switch (stream[word++]) {
483        case 0: out << " runtime";    break;
484        case 1: out << " sampled";    break;
485        case 2: out << " nonsampled"; break;
486        }
487        out << " format:" << ImageFormatString((int)(ImageFormat)stream[word++]);
488
489        if (numOperands == 8) {
490            out << " " << AccessQualifierString(stream[word++]);
491        }
492        return;
493    }
494
495    // Handle all the parameterized operands
496    for (int op = 0; op < InstructionDesc[enumCast(opCode)].operands.getNum() && numOperands > 0; ++op) {
497        out << " ";
498        OperandClass operandClass = InstructionDesc[enumCast(opCode)].operands.getClass(op);
499        switch (operandClass) {
500        case OperandId:
501        case OperandScope:
502        case OperandMemorySemantics:
503            disassembleIds(1);
504            --numOperands;
505            // Get names for printing "(XXX)" for readability, *after* this id
506            if (opCode == Op::OpName)
507                idDescriptor[stream[word - 1]] = decodeString().second;
508            break;
509        case OperandVariableIds:
510            disassembleIds(numOperands);
511            return;
512        case OperandImageOperands:
513            outputMask(OperandImageOperands, stream[word++]);
514            --numOperands;
515            disassembleIds(numOperands);
516            return;
517        case OperandOptionalLiteral:
518        case OperandVariableLiterals:
519            if ((opCode == Op::OpDecorate && stream[word - 1] == Decoration::BuiltIn) ||
520                (opCode == Op::OpMemberDecorate && stream[word - 1] == Decoration::BuiltIn)) {
521                out << BuiltInString(stream[word++]);
522                --numOperands;
523                ++op;
524            }
525            disassembleImmediates(numOperands);
526            return;
527        case OperandVariableIdLiteral:
528            while (numOperands > 0) {
529                out << std::endl;
530                outputResultId(0);
531                outputTypeId(0);
532                outputIndent();
533                out << "     Type ";
534                disassembleIds(1);
535                out << ", member ";
536                disassembleImmediates(1);
537                numOperands -= 2;
538            }
539            return;
540        case OperandVariableLiteralId:
541            while (numOperands > 0) {
542                out << std::endl;
543                outputResultId(0);
544                outputTypeId(0);
545                outputIndent();
546                out << "     case ";
547                disassembleImmediates(1);
548                out << ": ";
549                disassembleIds(1);
550                numOperands -= 2;
551            }
552            return;
553        case OperandLiteralNumber:
554            disassembleImmediates(1);
555            --numOperands;
556            if (opCode == Op::OpExtInst) {
557                ExtInstSet extInstSet = GLSL450Inst;
558                const char* name = idDescriptor[stream[word - 2]].c_str();
559                if (strcmp("OpenCL.std", name) == 0) {
560                    extInstSet = OpenCLExtInst;
561                } else if (strcmp("OpenCL.DebugInfo.100", name) == 0) {
562                    extInstSet = OpenCLExtInst;
563                } else if (strcmp("NonSemantic.DebugPrintf", name) == 0) {
564                    extInstSet = NonSemanticDebugPrintfExtInst;
565                } else if (strcmp("NonSemantic.DebugBreak", name) == 0) {
566                    extInstSet = NonSemanticDebugBreakExtInst;
567                } else if (strncmp("NonSemantic.Shader.DebugInfo.", name, strlen("NonSemantic.Shader.DebugInfo.")) ==
568                           0) {
569                    extInstSet = NonSemanticShaderDebugInfo;
570                } else if (strcmp(spv::E_SPV_AMD_shader_ballot, name) == 0 ||
571                           strcmp(spv::E_SPV_AMD_shader_trinary_minmax, name) == 0 ||
572                           strcmp(spv::E_SPV_AMD_shader_explicit_vertex_parameter, name) == 0 ||
573                           strcmp(spv::E_SPV_AMD_gcn_shader, name) == 0) {
574                    extInstSet = GLSLextAMDInst;
575                } else if (strcmp(spv::E_SPV_NV_sample_mask_override_coverage, name) == 0 ||
576                          strcmp(spv::E_SPV_NV_geometry_shader_passthrough, name) == 0 ||
577                          strcmp(spv::E_SPV_NV_viewport_array2, name) == 0 ||
578                          strcmp(spv::E_SPV_NVX_multiview_per_view_attributes, name) == 0 ||
579                          strcmp(spv::E_SPV_NV_fragment_shader_barycentric, name) == 0 ||
580                          strcmp(spv::E_SPV_NV_mesh_shader, name) == 0) {
581                    extInstSet = GLSLextNVInst;
582                }
583                unsigned entrypoint = stream[word - 1];
584                if (extInstSet == GLSL450Inst) {
585                    if (entrypoint < GLSLstd450Count) {
586                        out << "(" << GlslStd450DebugNames[entrypoint] << ")";
587                    }
588                } else if (extInstSet == GLSLextAMDInst) {
589                    out << "(" << GLSLextAMDGetDebugNames(name, entrypoint) << ")";
590                }
591                else if (extInstSet == GLSLextNVInst) {
592                    out << "(" << GLSLextNVGetDebugNames(name, entrypoint) << ")";
593                } else if (extInstSet == NonSemanticDebugPrintfExtInst) {
594                    out << "(DebugPrintf)";
595                } else if (extInstSet == NonSemanticDebugBreakExtInst) {
596                    out << "(DebugBreak)";
597                } else if (extInstSet == NonSemanticShaderDebugInfo) {
598                    out << "(" << NonSemanticShaderDebugInfoGetDebugNames(entrypoint) << ")";
599                }
600            }
601            break;
602        case OperandOptionalLiteralString:
603        case OperandLiteralString:
604            numOperands -= disassembleString();
605            break;
606        case OperandVariableLiteralStrings:
607            while (numOperands > 0)
608                numOperands -= disassembleString();
609            return;
610        case OperandMemoryAccess:
611            {
612                outputMask(OperandMemoryAccess, stream[word++]);
613                --numOperands;
614                // Put a space after "None" if there are any remaining operands
615                if (numOperands && stream[word-1] == 0) {
616                    out << " ";
617                }
618                uint32_t mask = stream[word-1];
619                // Aligned is the only memory access operand that uses an immediate
620                // value, and it is also the first operand that uses a value at all.
621                if (mask & (uint32_t)MemoryAccessMask::Aligned) {
622                    disassembleImmediates(1);
623                    numOperands--;
624                    if (numOperands)
625                        out << " ";
626                }
627
628                uint32_t bitCount = popcount(mask & (uint32_t)(MemoryAccessMask::MakePointerAvailable | MemoryAccessMask::MakePointerVisible));
629                disassembleIds(bitCount);
630                numOperands -= bitCount;
631            }
632            break;
633        case OperandTensorAddressingOperands:
634            {
635                outputMask(OperandTensorAddressingOperands, stream[word++]);
636                --numOperands;
637                // Put a space after "None" if there are any remaining operands
638                if (numOperands && stream[word-1] == 0) {
639                    out << " ";
640                }
641                uint32_t bitCount = popcount(stream[word-1]);
642                disassembleIds(bitCount);
643                numOperands -= bitCount;
644            }
645            break;
646        default:
647            assert(operandClass >= OperandSource && operandClass < OperandOpcode);
648
649            if (OperandClassParams[operandClass].bitmask)
650                outputMask(operandClass, stream[word++]);
651            else
652                out << OperandClassParams[operandClass].getName(stream[word++]);
653            --numOperands;
654
655            break;
656        }
657    }
658
659    return;
660}
661
662static void GLSLstd450GetDebugNames(const char** names)
663{
664    for (int i = 0; i < GLSLstd450Count; ++i)
665        names[i] = "Unknown";
666
667    names[GLSLstd450Round]                   = "Round";
668    names[GLSLstd450RoundEven]               = "RoundEven";
669    names[GLSLstd450Trunc]                   = "Trunc";
670    names[GLSLstd450FAbs]                    = "FAbs";
671    names[GLSLstd450SAbs]                    = "SAbs";
672    names[GLSLstd450FSign]                   = "FSign";
673    names[GLSLstd450SSign]                   = "SSign";
674    names[GLSLstd450Floor]                   = "Floor";
675    names[GLSLstd450Ceil]                    = "Ceil";
676    names[GLSLstd450Fract]                   = "Fract";
677    names[GLSLstd450Radians]                 = "Radians";
678    names[GLSLstd450Degrees]                 = "Degrees";
679    names[GLSLstd450Sin]                     = "Sin";
680    names[GLSLstd450Cos]                     = "Cos";
681    names[GLSLstd450Tan]                     = "Tan";
682    names[GLSLstd450Asin]                    = "Asin";
683    names[GLSLstd450Acos]                    = "Acos";
684    names[GLSLstd450Atan]                    = "Atan";
685    names[GLSLstd450Sinh]                    = "Sinh";
686    names[GLSLstd450Cosh]                    = "Cosh";
687    names[GLSLstd450Tanh]                    = "Tanh";
688    names[GLSLstd450Asinh]                   = "Asinh";
689    names[GLSLstd450Acosh]                   = "Acosh";
690    names[GLSLstd450Atanh]                   = "Atanh";
691    names[GLSLstd450Atan2]                   = "Atan2";
692    names[GLSLstd450Pow]                     = "Pow";
693    names[GLSLstd450Exp]                     = "Exp";
694    names[GLSLstd450Log]                     = "Log";
695    names[GLSLstd450Exp2]                    = "Exp2";
696    names[GLSLstd450Log2]                    = "Log2";
697    names[GLSLstd450Sqrt]                    = "Sqrt";
698    names[GLSLstd450InverseSqrt]             = "InverseSqrt";
699    names[GLSLstd450Determinant]             = "Determinant";
700    names[GLSLstd450MatrixInverse]           = "MatrixInverse";
701    names[GLSLstd450Modf]                    = "Modf";
702    names[GLSLstd450ModfStruct]              = "ModfStruct";
703    names[GLSLstd450FMin]                    = "FMin";
704    names[GLSLstd450SMin]                    = "SMin";
705    names[GLSLstd450UMin]                    = "UMin";
706    names[GLSLstd450FMax]                    = "FMax";
707    names[GLSLstd450SMax]                    = "SMax";
708    names[GLSLstd450UMax]                    = "UMax";
709    names[GLSLstd450FClamp]                  = "FClamp";
710    names[GLSLstd450SClamp]                  = "SClamp";
711    names[GLSLstd450UClamp]                  = "UClamp";
712    names[GLSLstd450FMix]                    = "FMix";
713    names[GLSLstd450Step]                    = "Step";
714    names[GLSLstd450SmoothStep]              = "SmoothStep";
715    names[GLSLstd450Fma]                     = "Fma";
716    names[GLSLstd450Frexp]                   = "Frexp";
717    names[GLSLstd450FrexpStruct]             = "FrexpStruct";
718    names[GLSLstd450Ldexp]                   = "Ldexp";
719    names[GLSLstd450PackSnorm4x8]            = "PackSnorm4x8";
720    names[GLSLstd450PackUnorm4x8]            = "PackUnorm4x8";
721    names[GLSLstd450PackSnorm2x16]           = "PackSnorm2x16";
722    names[GLSLstd450PackUnorm2x16]           = "PackUnorm2x16";
723    names[GLSLstd450PackHalf2x16]            = "PackHalf2x16";
724    names[GLSLstd450PackDouble2x32]          = "PackDouble2x32";
725    names[GLSLstd450UnpackSnorm2x16]         = "UnpackSnorm2x16";
726    names[GLSLstd450UnpackUnorm2x16]         = "UnpackUnorm2x16";
727    names[GLSLstd450UnpackHalf2x16]          = "UnpackHalf2x16";
728    names[GLSLstd450UnpackSnorm4x8]          = "UnpackSnorm4x8";
729    names[GLSLstd450UnpackUnorm4x8]          = "UnpackUnorm4x8";
730    names[GLSLstd450UnpackDouble2x32]        = "UnpackDouble2x32";
731    names[GLSLstd450Length]                  = "Length";
732    names[GLSLstd450Distance]                = "Distance";
733    names[GLSLstd450Cross]                   = "Cross";
734    names[GLSLstd450Normalize]               = "Normalize";
735    names[GLSLstd450FaceForward]             = "FaceForward";
736    names[GLSLstd450Reflect]                 = "Reflect";
737    names[GLSLstd450Refract]                 = "Refract";
738    names[GLSLstd450FindILsb]                = "FindILsb";
739    names[GLSLstd450FindSMsb]                = "FindSMsb";
740    names[GLSLstd450FindUMsb]                = "FindUMsb";
741    names[GLSLstd450InterpolateAtCentroid]   = "InterpolateAtCentroid";
742    names[GLSLstd450InterpolateAtSample]     = "InterpolateAtSample";
743    names[GLSLstd450InterpolateAtOffset]     = "InterpolateAtOffset";
744    names[GLSLstd450NMin]                    = "NMin";
745    names[GLSLstd450NMax]                    = "NMax";
746    names[GLSLstd450NClamp]                  = "NClamp";
747}
748
749static const char* GLSLextAMDGetDebugNames(const char* name, unsigned entrypoint)
750{
751    if (strcmp(name, spv::E_SPV_AMD_shader_ballot) == 0) {
752        switch (entrypoint) {
753        case SwizzleInvocationsAMD:         return "SwizzleInvocationsAMD";
754        case SwizzleInvocationsMaskedAMD:   return "SwizzleInvocationsMaskedAMD";
755        case WriteInvocationAMD:            return "WriteInvocationAMD";
756        case MbcntAMD:                      return "MbcntAMD";
757        default:                            return "Bad";
758        }
759    } else if (strcmp(name, spv::E_SPV_AMD_shader_trinary_minmax) == 0) {
760        switch (entrypoint) {
761        case FMin3AMD:      return "FMin3AMD";
762        case UMin3AMD:      return "UMin3AMD";
763        case SMin3AMD:      return "SMin3AMD";
764        case FMax3AMD:      return "FMax3AMD";
765        case UMax3AMD:      return "UMax3AMD";
766        case SMax3AMD:      return "SMax3AMD";
767        case FMid3AMD:      return "FMid3AMD";
768        case UMid3AMD:      return "UMid3AMD";
769        case SMid3AMD:      return "SMid3AMD";
770        default:            return "Bad";
771        }
772    } else if (strcmp(name, spv::E_SPV_AMD_shader_explicit_vertex_parameter) == 0) {
773        switch (entrypoint) {
774        case InterpolateAtVertexAMD:    return "InterpolateAtVertexAMD";
775        default:                        return "Bad";
776        }
777    }
778    else if (strcmp(name, spv::E_SPV_AMD_gcn_shader) == 0) {
779        switch (entrypoint) {
780        case CubeFaceIndexAMD:      return "CubeFaceIndexAMD";
781        case CubeFaceCoordAMD:      return "CubeFaceCoordAMD";
782        case TimeAMD:               return "TimeAMD";
783        default:
784            break;
785        }
786    }
787
788    return "Bad";
789}
790
791static const char* GLSLextNVGetDebugNames(const char* name, unsigned entrypoint)
792{
793    if (strcmp(name, spv::E_SPV_NV_sample_mask_override_coverage) == 0 ||
794        strcmp(name, spv::E_SPV_NV_geometry_shader_passthrough) == 0 ||
795        strcmp(name, spv::E_ARB_shader_viewport_layer_array) == 0 ||
796        strcmp(name, spv::E_SPV_NV_viewport_array2) == 0 ||
797        strcmp(name, spv::E_SPV_NVX_multiview_per_view_attributes) == 0 ||
798        strcmp(name, spv::E_SPV_NV_fragment_shader_barycentric) == 0 ||
799        strcmp(name, spv::E_SPV_NV_mesh_shader) == 0 ||
800        strcmp(name, spv::E_SPV_NV_shader_image_footprint) == 0) {
801        switch (entrypoint) {
802        // NV builtins
803        case (unsigned)BuiltIn::ViewportMaskNV:                 return "ViewportMaskNV";
804        case (unsigned)BuiltIn::SecondaryPositionNV:            return "SecondaryPositionNV";
805        case (unsigned)BuiltIn::SecondaryViewportMaskNV:        return "SecondaryViewportMaskNV";
806        case (unsigned)BuiltIn::PositionPerViewNV:              return "PositionPerViewNV";
807        case (unsigned)BuiltIn::ViewportMaskPerViewNV:          return "ViewportMaskPerViewNV";
808        case (unsigned)BuiltIn::BaryCoordNV:                    return "BaryCoordNV";
809        case (unsigned)BuiltIn::BaryCoordNoPerspNV:             return "BaryCoordNoPerspNV";
810        case (unsigned)BuiltIn::TaskCountNV:                    return "TaskCountNV";
811        case (unsigned)BuiltIn::PrimitiveCountNV:               return "PrimitiveCountNV";
812        case (unsigned)BuiltIn::PrimitiveIndicesNV:             return "PrimitiveIndicesNV";
813        case (unsigned)BuiltIn::ClipDistancePerViewNV:          return "ClipDistancePerViewNV";
814        case (unsigned)BuiltIn::CullDistancePerViewNV:          return "CullDistancePerViewNV";
815        case (unsigned)BuiltIn::LayerPerViewNV:                 return "LayerPerViewNV";
816        case (unsigned)BuiltIn::MeshViewCountNV:                return "MeshViewCountNV";
817        case (unsigned)BuiltIn::MeshViewIndicesNV:              return "MeshViewIndicesNV";
818
819        // NV Capabilities
820        case (unsigned)Capability::GeometryShaderPassthroughNV: return "GeometryShaderPassthroughNV";
821        case (unsigned)Capability::ShaderViewportMaskNV:        return "ShaderViewportMaskNV";
822        case (unsigned)Capability::ShaderStereoViewNV:          return "ShaderStereoViewNV";
823        case (unsigned)Capability::PerViewAttributesNV:         return "PerViewAttributesNV";
824        case (unsigned)Capability::FragmentBarycentricNV:       return "FragmentBarycentricNV";
825        case (unsigned)Capability::MeshShadingNV:               return "MeshShadingNV";
826        case (unsigned)Capability::ImageFootprintNV:            return "ImageFootprintNV";
827        case (unsigned)Capability::SampleMaskOverrideCoverageNV:return "SampleMaskOverrideCoverageNV";
828
829        // NV Decorations
830        case (unsigned)Decoration::OverrideCoverageNV:          return "OverrideCoverageNV";
831        case (unsigned)Decoration::PassthroughNV:               return "PassthroughNV";
832        case (unsigned)Decoration::ViewportRelativeNV:          return "ViewportRelativeNV";
833        case (unsigned)Decoration::SecondaryViewportRelativeNV: return "SecondaryViewportRelativeNV";
834        case (unsigned)Decoration::PerVertexNV:                 return "PerVertexNV";
835        case (unsigned)Decoration::PerPrimitiveNV:              return "PerPrimitiveNV";
836        case (unsigned)Decoration::PerViewNV:                   return "PerViewNV";
837        case (unsigned)Decoration::PerTaskNV:                   return "PerTaskNV";
838
839        default:                                    return "Bad";
840        }
841    }
842    return "Bad";
843}
844
845static const char* NonSemanticShaderDebugInfoGetDebugNames(unsigned entrypoint)
846{
847    switch (entrypoint) {
848        case NonSemanticShaderDebugInfoDebugInfoNone:                        return "DebugInfoNone";
849        case NonSemanticShaderDebugInfoDebugCompilationUnit:                 return "DebugCompilationUnit";
850        case NonSemanticShaderDebugInfoDebugTypeBasic:                       return "DebugTypeBasic";
851        case NonSemanticShaderDebugInfoDebugTypePointer:                     return "DebugTypePointer";
852        case NonSemanticShaderDebugInfoDebugTypeQualifier:                   return "DebugTypeQualifier";
853        case NonSemanticShaderDebugInfoDebugTypeArray:                       return "DebugTypeArray";
854        case NonSemanticShaderDebugInfoDebugTypeVector:                      return "DebugTypeVector";
855        case NonSemanticShaderDebugInfoDebugTypedef:                         return "DebugTypedef";
856        case NonSemanticShaderDebugInfoDebugTypeFunction:                    return "DebugTypeFunction";
857        case NonSemanticShaderDebugInfoDebugTypeEnum:                        return "DebugTypeEnum";
858        case NonSemanticShaderDebugInfoDebugTypeComposite:                   return "DebugTypeComposite";
859        case NonSemanticShaderDebugInfoDebugTypeMember:                      return "DebugTypeMember";
860        case NonSemanticShaderDebugInfoDebugTypeInheritance:                 return "DebugTypeInheritance";
861        case NonSemanticShaderDebugInfoDebugTypePtrToMember:                 return "DebugTypePtrToMember";
862        case NonSemanticShaderDebugInfoDebugTypeTemplate:                    return "DebugTypeTemplate";
863        case NonSemanticShaderDebugInfoDebugTypeTemplateParameter:           return "DebugTypeTemplateParameter";
864        case NonSemanticShaderDebugInfoDebugTypeTemplateTemplateParameter:   return "DebugTypeTemplateTemplateParameter";
865        case NonSemanticShaderDebugInfoDebugTypeTemplateParameterPack:       return "DebugTypeTemplateParameterPack";
866        case NonSemanticShaderDebugInfoDebugGlobalVariable:                  return "DebugGlobalVariable";
867        case NonSemanticShaderDebugInfoDebugFunctionDeclaration:             return "DebugFunctionDeclaration";
868        case NonSemanticShaderDebugInfoDebugFunction:                        return "DebugFunction";
869        case NonSemanticShaderDebugInfoDebugLexicalBlock:                    return "DebugLexicalBlock";
870        case NonSemanticShaderDebugInfoDebugLexicalBlockDiscriminator:       return "DebugLexicalBlockDiscriminator";
871        case NonSemanticShaderDebugInfoDebugScope:                           return "DebugScope";
872        case NonSemanticShaderDebugInfoDebugNoScope:                         return "DebugNoScope";
873        case NonSemanticShaderDebugInfoDebugInlinedAt:                       return "DebugInlinedAt";
874        case NonSemanticShaderDebugInfoDebugLocalVariable:                   return "DebugLocalVariable";
875        case NonSemanticShaderDebugInfoDebugInlinedVariable:                 return "DebugInlinedVariable";
876        case NonSemanticShaderDebugInfoDebugDeclare:                         return "DebugDeclare";
877        case NonSemanticShaderDebugInfoDebugValue:                           return "DebugValue";
878        case NonSemanticShaderDebugInfoDebugOperation:                       return "DebugOperation";
879        case NonSemanticShaderDebugInfoDebugExpression:                      return "DebugExpression";
880        case NonSemanticShaderDebugInfoDebugMacroDef:                        return "DebugMacroDef";
881        case NonSemanticShaderDebugInfoDebugMacroUndef:                      return "DebugMacroUndef";
882        case NonSemanticShaderDebugInfoDebugImportedEntity:                  return "DebugImportedEntity";
883        case NonSemanticShaderDebugInfoDebugSource:                          return "DebugSource";
884        case NonSemanticShaderDebugInfoDebugFunctionDefinition:              return "DebugFunctionDefinition";
885        case NonSemanticShaderDebugInfoDebugSourceContinued:                 return "DebugSourceContinued";
886        case NonSemanticShaderDebugInfoDebugLine:                            return "DebugLine";
887        case NonSemanticShaderDebugInfoDebugNoLine:                          return "DebugNoLine";
888        case NonSemanticShaderDebugInfoDebugBuildIdentifier:                 return "DebugBuildIdentifier";
889        case NonSemanticShaderDebugInfoDebugStoragePath:                     return "DebugStoragePath";
890        case NonSemanticShaderDebugInfoDebugEntryPoint:                      return "DebugEntryPoint";
891        case NonSemanticShaderDebugInfoDebugTypeMatrix:                      return "DebugTypeMatrix";
892        case NonSemanticShaderDebugInfoDebugTypeVectorIdEXT:                 return "DebugTypeVectorIdEXT";
893        case NonSemanticShaderDebugInfoDebugTypeCooperativeMatrixKHR:        return "DebugTypeCooperativeMatrixKHR";
894        default:                                                                return "Bad";
895    }
896
897    return "Bad";
898}
899
900void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
901{
902    SpirvStream SpirvStream(out, stream);
903    spv::Parameterize();
904    GLSLstd450GetDebugNames(GlslStd450DebugNames);
905    SpirvStream.validate();
906    SpirvStream.processInstructions();
907}
908
909} // end namespace spv