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