yum-mirror/glslang

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

Diego NovilloInitial support for NonSemantic.Shader.DebugInfo 101 (#4220)0971d794

16.3.0
37.8 KiB892 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 8:
386                case 16:
387                    if (numOperands > 1) {
388                        switch (stream[word+1]) {
389                        default:
390                            assert(0); [[fallthrough]];
391                        case (int)spv::FPEncoding::BFloat16KHR:
392                            idDescriptor[resultId] = "bfloat16_t";
393                            break;
394                        case (int)spv::FPEncoding::Float8E4M3EXT:
395                            idDescriptor[resultId] = "floate4m3_t";
396                            break;
397                        case (int)spv::FPEncoding::Float8E5M2EXT:
398                            idDescriptor[resultId] = "floate5m2_t";
399                            break;
400                        }
401                    } else {
402                        idDescriptor[resultId] = "float16_t";
403                    }
404                    break;
405                default: assert(0); [[fallthrough]];
406                case 32: idDescriptor[resultId] = "float"; break;
407                case 64: idDescriptor[resultId] = "float64_t"; break;
408                }
409                break;
410            case Op::OpTypeBool:
411                idDescriptor[resultId] = "bool";
412                break;
413            case Op::OpTypeStruct:
414                idDescriptor[resultId] = "struct";
415                break;
416            case Op::OpTypePointer:
417            case Op::OpTypeUntypedPointerKHR:
418                idDescriptor[resultId] = "ptr";
419                break;
420            case Op::OpTypeVector:
421                if (idDescriptor[stream[word]].size() > 0) {
422                    if (idDescriptor[stream[word]].substr(0,2) == "bf") {
423                        idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 2);
424                    } else {
425                        idDescriptor[resultId].append(idDescriptor[stream[word]].begin(), idDescriptor[stream[word]].begin() + 1);
426                    }
427                    if (strstr(idDescriptor[stream[word]].c_str(), "8")) {
428                        idDescriptor[resultId].append("8");
429                    }
430                    if (strstr(idDescriptor[stream[word]].c_str(), "16")) {
431                        idDescriptor[resultId].append("16");
432                    }
433                    if (strstr(idDescriptor[stream[word]].c_str(), "64")) {
434                        idDescriptor[resultId].append("64");
435                    }
436                }
437                idDescriptor[resultId].append("vec");
438                switch (stream[word + 1]) {
439                case 2:   idDescriptor[resultId].append("2");   break;
440                case 3:   idDescriptor[resultId].append("3");   break;
441                case 4:   idDescriptor[resultId].append("4");   break;
442                case 8:   idDescriptor[resultId].append("8");   break;
443                case 16:  idDescriptor[resultId].append("16");  break;
444                case 32:  idDescriptor[resultId].append("32");  break;
445                default: break;
446                }
447                break;
448            default:
449                break;
450            }
451        }
452    }
453
454    // Process the operands.  Note, a new context-dependent set could be
455    // swapped in mid-traversal.
456
457    // Handle images specially, so can put out helpful strings.
458    if (opCode == Op::OpTypeImage) {
459        out << " ";
460        disassembleIds(1);
461        out << " " << DimensionString((int)(Dim)stream[word++]);
462        out << (stream[word++] != 0 ? " depth" : "");
463        out << (stream[word++] != 0 ? " array" : "");
464        out << (stream[word++] != 0 ? " multi-sampled" : "");
465        switch (stream[word++]) {
466        case 0: out << " runtime";    break;
467        case 1: out << " sampled";    break;
468        case 2: out << " nonsampled"; break;
469        }
470        out << " format:" << ImageFormatString((int)(ImageFormat)stream[word++]);
471
472        if (numOperands == 8) {
473            out << " " << AccessQualifierString(stream[word++]);
474        }
475        return;
476    }
477
478    // Handle all the parameterized operands
479    for (int op = 0; op < InstructionDesc[enumCast(opCode)].operands.getNum() && numOperands > 0; ++op) {
480        out << " ";
481        OperandClass operandClass = InstructionDesc[enumCast(opCode)].operands.getClass(op);
482        switch (operandClass) {
483        case OperandId:
484        case OperandScope:
485        case OperandMemorySemantics:
486            disassembleIds(1);
487            --numOperands;
488            // Get names for printing "(XXX)" for readability, *after* this id
489            if (opCode == Op::OpName)
490                idDescriptor[stream[word - 1]] = decodeString().second;
491            break;
492        case OperandVariableIds:
493            disassembleIds(numOperands);
494            return;
495        case OperandImageOperands:
496            outputMask(OperandImageOperands, stream[word++]);
497            --numOperands;
498            disassembleIds(numOperands);
499            return;
500        case OperandOptionalLiteral:
501        case OperandVariableLiterals:
502            if ((opCode == Op::OpDecorate && stream[word - 1] == Decoration::BuiltIn) ||
503                (opCode == Op::OpMemberDecorate && stream[word - 1] == Decoration::BuiltIn)) {
504                out << BuiltInString(stream[word++]);
505                --numOperands;
506                ++op;
507            }
508            disassembleImmediates(numOperands);
509            return;
510        case OperandVariableIdLiteral:
511            while (numOperands > 0) {
512                out << std::endl;
513                outputResultId(0);
514                outputTypeId(0);
515                outputIndent();
516                out << "     Type ";
517                disassembleIds(1);
518                out << ", member ";
519                disassembleImmediates(1);
520                numOperands -= 2;
521            }
522            return;
523        case OperandVariableLiteralId:
524            while (numOperands > 0) {
525                out << std::endl;
526                outputResultId(0);
527                outputTypeId(0);
528                outputIndent();
529                out << "     case ";
530                disassembleImmediates(1);
531                out << ": ";
532                disassembleIds(1);
533                numOperands -= 2;
534            }
535            return;
536        case OperandLiteralNumber:
537            disassembleImmediates(1);
538            --numOperands;
539            if (opCode == Op::OpExtInst) {
540                ExtInstSet extInstSet = GLSL450Inst;
541                const char* name = idDescriptor[stream[word - 2]].c_str();
542                if (strcmp("OpenCL.std", name) == 0) {
543                    extInstSet = OpenCLExtInst;
544                } else if (strcmp("OpenCL.DebugInfo.100", name) == 0) {
545                    extInstSet = OpenCLExtInst;
546                } else if (strcmp("NonSemantic.DebugPrintf", name) == 0) {
547                    extInstSet = NonSemanticDebugPrintfExtInst;
548                } else if (strcmp("NonSemantic.DebugBreak", name) == 0) {
549                    extInstSet = NonSemanticDebugBreakExtInst;
550                } else if (strncmp("NonSemantic.Shader.DebugInfo.", name, strlen("NonSemantic.Shader.DebugInfo.")) ==
551                           0) {
552                    extInstSet = NonSemanticShaderDebugInfo;
553                } else if (strcmp(spv::E_SPV_AMD_shader_ballot, name) == 0 ||
554                           strcmp(spv::E_SPV_AMD_shader_trinary_minmax, name) == 0 ||
555                           strcmp(spv::E_SPV_AMD_shader_explicit_vertex_parameter, name) == 0 ||
556                           strcmp(spv::E_SPV_AMD_gcn_shader, name) == 0) {
557                    extInstSet = GLSLextAMDInst;
558                } else if (strcmp(spv::E_SPV_NV_sample_mask_override_coverage, name) == 0 ||
559                          strcmp(spv::E_SPV_NV_geometry_shader_passthrough, name) == 0 ||
560                          strcmp(spv::E_SPV_NV_viewport_array2, name) == 0 ||
561                          strcmp(spv::E_SPV_NVX_multiview_per_view_attributes, name) == 0 ||
562                          strcmp(spv::E_SPV_NV_fragment_shader_barycentric, name) == 0 ||
563                          strcmp(spv::E_SPV_NV_mesh_shader, name) == 0) {
564                    extInstSet = GLSLextNVInst;
565                }
566                unsigned entrypoint = stream[word - 1];
567                if (extInstSet == GLSL450Inst) {
568                    if (entrypoint < GLSLstd450Count) {
569                        out << "(" << GlslStd450DebugNames[entrypoint] << ")";
570                    }
571                } else if (extInstSet == GLSLextAMDInst) {
572                    out << "(" << GLSLextAMDGetDebugNames(name, entrypoint) << ")";
573                }
574                else if (extInstSet == GLSLextNVInst) {
575                    out << "(" << GLSLextNVGetDebugNames(name, entrypoint) << ")";
576                } else if (extInstSet == NonSemanticDebugPrintfExtInst) {
577                    out << "(DebugPrintf)";
578                } else if (extInstSet == NonSemanticDebugBreakExtInst) {
579                    out << "(DebugBreak)";
580                } else if (extInstSet == NonSemanticShaderDebugInfo) {
581                    out << "(" << NonSemanticShaderDebugInfoGetDebugNames(entrypoint) << ")";
582                }
583            }
584            break;
585        case OperandOptionalLiteralString:
586        case OperandLiteralString:
587            numOperands -= disassembleString();
588            break;
589        case OperandVariableLiteralStrings:
590            while (numOperands > 0)
591                numOperands -= disassembleString();
592            return;
593        case OperandMemoryAccess:
594            {
595                outputMask(OperandMemoryAccess, stream[word++]);
596                --numOperands;
597                // Put a space after "None" if there are any remaining operands
598                if (numOperands && stream[word-1] == 0) {
599                    out << " ";
600                }
601                uint32_t mask = stream[word-1];
602                // Aligned is the only memory access operand that uses an immediate
603                // value, and it is also the first operand that uses a value at all.
604                if (mask & (uint32_t)MemoryAccessMask::Aligned) {
605                    disassembleImmediates(1);
606                    numOperands--;
607                    if (numOperands)
608                        out << " ";
609                }
610
611                uint32_t bitCount = popcount(mask & (uint32_t)(MemoryAccessMask::MakePointerAvailable | MemoryAccessMask::MakePointerVisible));
612                disassembleIds(bitCount);
613                numOperands -= bitCount;
614            }
615            break;
616        case OperandTensorAddressingOperands:
617            {
618                outputMask(OperandTensorAddressingOperands, stream[word++]);
619                --numOperands;
620                // Put a space after "None" if there are any remaining operands
621                if (numOperands && stream[word-1] == 0) {
622                    out << " ";
623                }
624                uint32_t bitCount = popcount(stream[word-1]);
625                disassembleIds(bitCount);
626                numOperands -= bitCount;
627            }
628            break;
629        default:
630            assert(operandClass >= OperandSource && operandClass < OperandOpcode);
631
632            if (OperandClassParams[operandClass].bitmask)
633                outputMask(operandClass, stream[word++]);
634            else
635                out << OperandClassParams[operandClass].getName(stream[word++]);
636            --numOperands;
637
638            break;
639        }
640    }
641
642    return;
643}
644
645static void GLSLstd450GetDebugNames(const char** names)
646{
647    for (int i = 0; i < GLSLstd450Count; ++i)
648        names[i] = "Unknown";
649
650    names[GLSLstd450Round]                   = "Round";
651    names[GLSLstd450RoundEven]               = "RoundEven";
652    names[GLSLstd450Trunc]                   = "Trunc";
653    names[GLSLstd450FAbs]                    = "FAbs";
654    names[GLSLstd450SAbs]                    = "SAbs";
655    names[GLSLstd450FSign]                   = "FSign";
656    names[GLSLstd450SSign]                   = "SSign";
657    names[GLSLstd450Floor]                   = "Floor";
658    names[GLSLstd450Ceil]                    = "Ceil";
659    names[GLSLstd450Fract]                   = "Fract";
660    names[GLSLstd450Radians]                 = "Radians";
661    names[GLSLstd450Degrees]                 = "Degrees";
662    names[GLSLstd450Sin]                     = "Sin";
663    names[GLSLstd450Cos]                     = "Cos";
664    names[GLSLstd450Tan]                     = "Tan";
665    names[GLSLstd450Asin]                    = "Asin";
666    names[GLSLstd450Acos]                    = "Acos";
667    names[GLSLstd450Atan]                    = "Atan";
668    names[GLSLstd450Sinh]                    = "Sinh";
669    names[GLSLstd450Cosh]                    = "Cosh";
670    names[GLSLstd450Tanh]                    = "Tanh";
671    names[GLSLstd450Asinh]                   = "Asinh";
672    names[GLSLstd450Acosh]                   = "Acosh";
673    names[GLSLstd450Atanh]                   = "Atanh";
674    names[GLSLstd450Atan2]                   = "Atan2";
675    names[GLSLstd450Pow]                     = "Pow";
676    names[GLSLstd450Exp]                     = "Exp";
677    names[GLSLstd450Log]                     = "Log";
678    names[GLSLstd450Exp2]                    = "Exp2";
679    names[GLSLstd450Log2]                    = "Log2";
680    names[GLSLstd450Sqrt]                    = "Sqrt";
681    names[GLSLstd450InverseSqrt]             = "InverseSqrt";
682    names[GLSLstd450Determinant]             = "Determinant";
683    names[GLSLstd450MatrixInverse]           = "MatrixInverse";
684    names[GLSLstd450Modf]                    = "Modf";
685    names[GLSLstd450ModfStruct]              = "ModfStruct";
686    names[GLSLstd450FMin]                    = "FMin";
687    names[GLSLstd450SMin]                    = "SMin";
688    names[GLSLstd450UMin]                    = "UMin";
689    names[GLSLstd450FMax]                    = "FMax";
690    names[GLSLstd450SMax]                    = "SMax";
691    names[GLSLstd450UMax]                    = "UMax";
692    names[GLSLstd450FClamp]                  = "FClamp";
693    names[GLSLstd450SClamp]                  = "SClamp";
694    names[GLSLstd450UClamp]                  = "UClamp";
695    names[GLSLstd450FMix]                    = "FMix";
696    names[GLSLstd450Step]                    = "Step";
697    names[GLSLstd450SmoothStep]              = "SmoothStep";
698    names[GLSLstd450Fma]                     = "Fma";
699    names[GLSLstd450Frexp]                   = "Frexp";
700    names[GLSLstd450FrexpStruct]             = "FrexpStruct";
701    names[GLSLstd450Ldexp]                   = "Ldexp";
702    names[GLSLstd450PackSnorm4x8]            = "PackSnorm4x8";
703    names[GLSLstd450PackUnorm4x8]            = "PackUnorm4x8";
704    names[GLSLstd450PackSnorm2x16]           = "PackSnorm2x16";
705    names[GLSLstd450PackUnorm2x16]           = "PackUnorm2x16";
706    names[GLSLstd450PackHalf2x16]            = "PackHalf2x16";
707    names[GLSLstd450PackDouble2x32]          = "PackDouble2x32";
708    names[GLSLstd450UnpackSnorm2x16]         = "UnpackSnorm2x16";
709    names[GLSLstd450UnpackUnorm2x16]         = "UnpackUnorm2x16";
710    names[GLSLstd450UnpackHalf2x16]          = "UnpackHalf2x16";
711    names[GLSLstd450UnpackSnorm4x8]          = "UnpackSnorm4x8";
712    names[GLSLstd450UnpackUnorm4x8]          = "UnpackUnorm4x8";
713    names[GLSLstd450UnpackDouble2x32]        = "UnpackDouble2x32";
714    names[GLSLstd450Length]                  = "Length";
715    names[GLSLstd450Distance]                = "Distance";
716    names[GLSLstd450Cross]                   = "Cross";
717    names[GLSLstd450Normalize]               = "Normalize";
718    names[GLSLstd450FaceForward]             = "FaceForward";
719    names[GLSLstd450Reflect]                 = "Reflect";
720    names[GLSLstd450Refract]                 = "Refract";
721    names[GLSLstd450FindILsb]                = "FindILsb";
722    names[GLSLstd450FindSMsb]                = "FindSMsb";
723    names[GLSLstd450FindUMsb]                = "FindUMsb";
724    names[GLSLstd450InterpolateAtCentroid]   = "InterpolateAtCentroid";
725    names[GLSLstd450InterpolateAtSample]     = "InterpolateAtSample";
726    names[GLSLstd450InterpolateAtOffset]     = "InterpolateAtOffset";
727    names[GLSLstd450NMin]                    = "NMin";
728    names[GLSLstd450NMax]                    = "NMax";
729    names[GLSLstd450NClamp]                  = "NClamp";
730}
731
732static const char* GLSLextAMDGetDebugNames(const char* name, unsigned entrypoint)
733{
734    if (strcmp(name, spv::E_SPV_AMD_shader_ballot) == 0) {
735        switch (entrypoint) {
736        case SwizzleInvocationsAMD:         return "SwizzleInvocationsAMD";
737        case SwizzleInvocationsMaskedAMD:   return "SwizzleInvocationsMaskedAMD";
738        case WriteInvocationAMD:            return "WriteInvocationAMD";
739        case MbcntAMD:                      return "MbcntAMD";
740        default:                            return "Bad";
741        }
742    } else if (strcmp(name, spv::E_SPV_AMD_shader_trinary_minmax) == 0) {
743        switch (entrypoint) {
744        case FMin3AMD:      return "FMin3AMD";
745        case UMin3AMD:      return "UMin3AMD";
746        case SMin3AMD:      return "SMin3AMD";
747        case FMax3AMD:      return "FMax3AMD";
748        case UMax3AMD:      return "UMax3AMD";
749        case SMax3AMD:      return "SMax3AMD";
750        case FMid3AMD:      return "FMid3AMD";
751        case UMid3AMD:      return "UMid3AMD";
752        case SMid3AMD:      return "SMid3AMD";
753        default:            return "Bad";
754        }
755    } else if (strcmp(name, spv::E_SPV_AMD_shader_explicit_vertex_parameter) == 0) {
756        switch (entrypoint) {
757        case InterpolateAtVertexAMD:    return "InterpolateAtVertexAMD";
758        default:                        return "Bad";
759        }
760    }
761    else if (strcmp(name, spv::E_SPV_AMD_gcn_shader) == 0) {
762        switch (entrypoint) {
763        case CubeFaceIndexAMD:      return "CubeFaceIndexAMD";
764        case CubeFaceCoordAMD:      return "CubeFaceCoordAMD";
765        case TimeAMD:               return "TimeAMD";
766        default:
767            break;
768        }
769    }
770
771    return "Bad";
772}
773
774static const char* GLSLextNVGetDebugNames(const char* name, unsigned entrypoint)
775{
776    if (strcmp(name, spv::E_SPV_NV_sample_mask_override_coverage) == 0 ||
777        strcmp(name, spv::E_SPV_NV_geometry_shader_passthrough) == 0 ||
778        strcmp(name, spv::E_ARB_shader_viewport_layer_array) == 0 ||
779        strcmp(name, spv::E_SPV_NV_viewport_array2) == 0 ||
780        strcmp(name, spv::E_SPV_NVX_multiview_per_view_attributes) == 0 ||
781        strcmp(name, spv::E_SPV_NV_fragment_shader_barycentric) == 0 ||
782        strcmp(name, spv::E_SPV_NV_mesh_shader) == 0 ||
783        strcmp(name, spv::E_SPV_NV_shader_image_footprint) == 0) {
784        switch (entrypoint) {
785        // NV builtins
786        case (unsigned)BuiltIn::ViewportMaskNV:                 return "ViewportMaskNV";
787        case (unsigned)BuiltIn::SecondaryPositionNV:            return "SecondaryPositionNV";
788        case (unsigned)BuiltIn::SecondaryViewportMaskNV:        return "SecondaryViewportMaskNV";
789        case (unsigned)BuiltIn::PositionPerViewNV:              return "PositionPerViewNV";
790        case (unsigned)BuiltIn::ViewportMaskPerViewNV:          return "ViewportMaskPerViewNV";
791        case (unsigned)BuiltIn::BaryCoordNV:                    return "BaryCoordNV";
792        case (unsigned)BuiltIn::BaryCoordNoPerspNV:             return "BaryCoordNoPerspNV";
793        case (unsigned)BuiltIn::TaskCountNV:                    return "TaskCountNV";
794        case (unsigned)BuiltIn::PrimitiveCountNV:               return "PrimitiveCountNV";
795        case (unsigned)BuiltIn::PrimitiveIndicesNV:             return "PrimitiveIndicesNV";
796        case (unsigned)BuiltIn::ClipDistancePerViewNV:          return "ClipDistancePerViewNV";
797        case (unsigned)BuiltIn::CullDistancePerViewNV:          return "CullDistancePerViewNV";
798        case (unsigned)BuiltIn::LayerPerViewNV:                 return "LayerPerViewNV";
799        case (unsigned)BuiltIn::MeshViewCountNV:                return "MeshViewCountNV";
800        case (unsigned)BuiltIn::MeshViewIndicesNV:              return "MeshViewIndicesNV";
801
802        // NV Capabilities
803        case (unsigned)Capability::GeometryShaderPassthroughNV: return "GeometryShaderPassthroughNV";
804        case (unsigned)Capability::ShaderViewportMaskNV:        return "ShaderViewportMaskNV";
805        case (unsigned)Capability::ShaderStereoViewNV:          return "ShaderStereoViewNV";
806        case (unsigned)Capability::PerViewAttributesNV:         return "PerViewAttributesNV";
807        case (unsigned)Capability::FragmentBarycentricNV:       return "FragmentBarycentricNV";
808        case (unsigned)Capability::MeshShadingNV:               return "MeshShadingNV";
809        case (unsigned)Capability::ImageFootprintNV:            return "ImageFootprintNV";
810        case (unsigned)Capability::SampleMaskOverrideCoverageNV:return "SampleMaskOverrideCoverageNV";
811
812        // NV Decorations
813        case (unsigned)Decoration::OverrideCoverageNV:          return "OverrideCoverageNV";
814        case (unsigned)Decoration::PassthroughNV:               return "PassthroughNV";
815        case (unsigned)Decoration::ViewportRelativeNV:          return "ViewportRelativeNV";
816        case (unsigned)Decoration::SecondaryViewportRelativeNV: return "SecondaryViewportRelativeNV";
817        case (unsigned)Decoration::PerVertexNV:                 return "PerVertexNV";
818        case (unsigned)Decoration::PerPrimitiveNV:              return "PerPrimitiveNV";
819        case (unsigned)Decoration::PerViewNV:                   return "PerViewNV";
820        case (unsigned)Decoration::PerTaskNV:                   return "PerTaskNV";
821
822        default:                                    return "Bad";
823        }
824    }
825    return "Bad";
826}
827
828static const char* NonSemanticShaderDebugInfoGetDebugNames(unsigned entrypoint)
829{
830    switch (entrypoint) {
831        case NonSemanticShaderDebugInfoDebugInfoNone:                        return "DebugInfoNone";
832        case NonSemanticShaderDebugInfoDebugCompilationUnit:                 return "DebugCompilationUnit";
833        case NonSemanticShaderDebugInfoDebugTypeBasic:                       return "DebugTypeBasic";
834        case NonSemanticShaderDebugInfoDebugTypePointer:                     return "DebugTypePointer";
835        case NonSemanticShaderDebugInfoDebugTypeQualifier:                   return "DebugTypeQualifier";
836        case NonSemanticShaderDebugInfoDebugTypeArray:                       return "DebugTypeArray";
837        case NonSemanticShaderDebugInfoDebugTypeVector:                      return "DebugTypeVector";
838        case NonSemanticShaderDebugInfoDebugTypedef:                         return "DebugTypedef";
839        case NonSemanticShaderDebugInfoDebugTypeFunction:                    return "DebugTypeFunction";
840        case NonSemanticShaderDebugInfoDebugTypeEnum:                        return "DebugTypeEnum";
841        case NonSemanticShaderDebugInfoDebugTypeComposite:                   return "DebugTypeComposite";
842        case NonSemanticShaderDebugInfoDebugTypeMember:                      return "DebugTypeMember";
843        case NonSemanticShaderDebugInfoDebugTypeInheritance:                 return "DebugTypeInheritance";
844        case NonSemanticShaderDebugInfoDebugTypePtrToMember:                 return "DebugTypePtrToMember";
845        case NonSemanticShaderDebugInfoDebugTypeTemplate:                    return "DebugTypeTemplate";
846        case NonSemanticShaderDebugInfoDebugTypeTemplateParameter:           return "DebugTypeTemplateParameter";
847        case NonSemanticShaderDebugInfoDebugTypeTemplateTemplateParameter:   return "DebugTypeTemplateTemplateParameter";
848        case NonSemanticShaderDebugInfoDebugTypeTemplateParameterPack:       return "DebugTypeTemplateParameterPack";
849        case NonSemanticShaderDebugInfoDebugGlobalVariable:                  return "DebugGlobalVariable";
850        case NonSemanticShaderDebugInfoDebugFunctionDeclaration:             return "DebugFunctionDeclaration";
851        case NonSemanticShaderDebugInfoDebugFunction:                        return "DebugFunction";
852        case NonSemanticShaderDebugInfoDebugLexicalBlock:                    return "DebugLexicalBlock";
853        case NonSemanticShaderDebugInfoDebugLexicalBlockDiscriminator:       return "DebugLexicalBlockDiscriminator";
854        case NonSemanticShaderDebugInfoDebugScope:                           return "DebugScope";
855        case NonSemanticShaderDebugInfoDebugNoScope:                         return "DebugNoScope";
856        case NonSemanticShaderDebugInfoDebugInlinedAt:                       return "DebugInlinedAt";
857        case NonSemanticShaderDebugInfoDebugLocalVariable:                   return "DebugLocalVariable";
858        case NonSemanticShaderDebugInfoDebugInlinedVariable:                 return "DebugInlinedVariable";
859        case NonSemanticShaderDebugInfoDebugDeclare:                         return "DebugDeclare";
860        case NonSemanticShaderDebugInfoDebugValue:                           return "DebugValue";
861        case NonSemanticShaderDebugInfoDebugOperation:                       return "DebugOperation";
862        case NonSemanticShaderDebugInfoDebugExpression:                      return "DebugExpression";
863        case NonSemanticShaderDebugInfoDebugMacroDef:                        return "DebugMacroDef";
864        case NonSemanticShaderDebugInfoDebugMacroUndef:                      return "DebugMacroUndef";
865        case NonSemanticShaderDebugInfoDebugImportedEntity:                  return "DebugImportedEntity";
866        case NonSemanticShaderDebugInfoDebugSource:                          return "DebugSource";
867        case NonSemanticShaderDebugInfoDebugFunctionDefinition:              return "DebugFunctionDefinition";
868        case NonSemanticShaderDebugInfoDebugSourceContinued:                 return "DebugSourceContinued";
869        case NonSemanticShaderDebugInfoDebugLine:                            return "DebugLine";
870        case NonSemanticShaderDebugInfoDebugNoLine:                          return "DebugNoLine";
871        case NonSemanticShaderDebugInfoDebugBuildIdentifier:                 return "DebugBuildIdentifier";
872        case NonSemanticShaderDebugInfoDebugStoragePath:                     return "DebugStoragePath";
873        case NonSemanticShaderDebugInfoDebugEntryPoint:                      return "DebugEntryPoint";
874        case NonSemanticShaderDebugInfoDebugTypeMatrix:                      return "DebugTypeMatrix";
875        case NonSemanticShaderDebugInfoDebugTypeVectorIdEXT:                 return "DebugTypeVectorIdEXT";
876        case NonSemanticShaderDebugInfoDebugTypeCooperativeMatrixKHR:        return "DebugTypeCooperativeMatrixKHR";
877        default:                                                                return "Bad";
878    }
879
880    return "Bad";
881}
882
883void Disassemble(std::ostream& out, const std::vector<unsigned int>& stream)
884{
885    SpirvStream SpirvStream(out, stream);
886    spv::Parameterize();
887    GLSLstd450GetDebugNames(GlslStd450DebugNames);
888    SpirvStream.validate();
889    SpirvStream.processInstructions();
890}
891
892} // end namespace spv