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