yum-mirror/glslang
git clone https://git.yummers.dev/yum-mirror/glslang
4ede04ac
16.6.0
1// 2// Copyright (C) 2002-2005 3Dlabs Inc. Ltd. 3// Copyright (C) 2013-2016 LunarG, Inc. 4// Copyright (C) 2016-2020 Google, Inc. 5// Modifications Copyright(C) 2021 Advanced Micro Devices, Inc.All rights reserved. 6// 7// All rights reserved. 8// 9// Redistribution and use in source and binary forms, with or without 10// modification, are permitted provided that the following conditions 11// are met: 12// 13// Redistributions of source code must retain the above copyright 14// notice, this list of conditions and the following disclaimer. 15// 16// Redistributions in binary form must reproduce the above 17// copyright notice, this list of conditions and the following 18// disclaimer in the documentation and/or other materials provided 19// with the distribution. 20// 21// Neither the name of 3Dlabs Inc. Ltd. nor the names of its 22// contributors may be used to endorse or promote products derived 23// from this software without specific prior written permission. 24// 25// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 28// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 29// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 30// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 31// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 32// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 33// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 35// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36// POSSIBILITY OF SUCH DAMAGE. 37// 38 39// this only applies to the standalone wrapper, not the front end in general 40#ifndef _CRT_SECURE_NO_WARNINGS 41#define _CRT_SECURE_NO_WARNINGS 42#endif 43 44#include "glslang/Public/ResourceLimits.h" 45#include "Worklist.h" 46#include "DirStackFileIncluder.h" 47#include "./../glslang/Public/ShaderLang.h" 48#include "../glslang/MachineIndependent/localintermediate.h" 49#include "../SPIRV/GlslangToSpv.h" 50#include "../SPIRV/GLSL.std.450.h" 51#include "../SPIRV/disassemble.h" 52 53#include <array> 54#include <atomic> 55#include <cctype> 56#include <cmath> 57#include <cstdlib> 58#include <cstring> 59#include <filesystem> 60#include <iterator> 61#include <map> 62#include <memory> 63#include <set> 64#include <thread> 65#include <type_traits> 66 67#include "../glslang/OSDependent/osinclude.h" 68 69// Build-time generated includes 70#include "glslang/build_info.h" 71 72#include "glslang/glsl_intrinsic_header.h" 73 74extern "C" { 75GLSLANG_EXPORT void ShOutputHtml () ; 76} 77 78// Command-line options 79enum TOptions : uint64_t { 80EOptionNone = 0 , 81EOptionIntermediate = ( 1ull << 0 ) , 82EOptionSuppressInfolog = ( 1ull << 1 ) , 83EOptionMemoryLeakMode = ( 1ull << 2 ) , 84EOptionRelaxedErrors = ( 1ull << 3 ) , 85EOptionGiveWarnings = ( 1ull << 4 ) , 86EOptionLinkProgram = ( 1ull << 5 ) , 87EOptionMultiThreaded = ( 1ull << 6 ) , 88EOptionDumpConfig = ( 1ull << 7 ) , 89EOptionDumpReflection = ( 1ull << 8 ) , 90EOptionSuppressWarnings = ( 1ull << 9 ) , 91EOptionDumpVersions = ( 1ull << 10 ) , 92EOptionSpv = ( 1ull << 11 ) , 93EOptionHumanReadableSpv = ( 1ull << 12 ) , 94EOptionVulkanRules = ( 1ull << 13 ) , 95EOptionDefaultDesktop = ( 1ull << 14 ) , 96EOptionOutputPreprocessed = ( 1ull << 15 ) , 97EOptionOutputHexadecimal = ( 1ull << 16 ) , 98EOptionReadHlsl = ( 1ull << 17 ) , 99EOptionCascadingErrors = ( 1ull << 18 ) , 100EOptionAutoMapBindings = ( 1ull << 19 ) , 101EOptionFlattenUniformArrays = ( 1ull << 20 ) , 102EOptionNoStorageFormat = ( 1ull << 21 ) , 103EOptionKeepUncalled = ( 1ull << 22 ) , 104EOptionHlslOffsets = ( 1ull << 23 ) , 105EOptionHlslIoMapping = ( 1ull << 24 ) , 106EOptionAutoMapLocations = ( 1ull << 25 ) , 107EOptionDebug = ( 1ull << 26 ) , 108EOptionStdin = ( 1ull << 27 ) , 109EOptionOptimizeDisable = ( 1ull << 28 ) , 110EOptionOptimizeSize = ( 1ull << 29 ) , 111EOptionInvertY = ( 1ull << 30 ) , 112EOptionDumpBareVersion = ( 1ull << 31 ) , 113EOptionCompileOnly = ( 1ull << 32 ) , 114EOptionDisplayErrorColumn = ( 1ull << 33 ) , 115EOptionLinkTimeOptimization = ( 1ull << 34 ) , 116EOptionValidateCrossStageIO = ( 1ull << 35 ) , 117EOptionBindingsPerResourceType = ( 1ull << 36 ) , 118EOptionRelaxSetBindingLimits = ( 1ull << 37 ) , 119EOptionDiscardIsTerminate = ( 1ull << 38 ) , 120EOptionOptimizePerformance = ( 1ull << 39 ) , 121} ; 122bool targetHlslFunctionality1 = false ; 123bool SpvToolsDisassembler = false ; 124bool SpvToolsValidate = false ; 125bool NaNClamp = false ; 126bool stripDebugInfo = false ; 127bool emitNonSemanticShaderDebugInfo = false ; 128bool emitNonSemanticShaderDebugSource = false ; 129bool beQuiet = false ; 130bool VulkanRulesRelaxed = false ; 131bool autoSampledTextures = false ; 132 133// 134// Return codes from main/exit(). 135// 136enum TFailCode { 137ESuccess = 0 , 138EFailUsage , 139EFailCompile , 140EFailLink , 141EFailCompilerCreate , 142EFailThreadCreate , 143EFailLinkerCreate 144} ; 145 146// 147// Forward declarations. 148// 149EShLanguage FindLanguage ( const std :: string & name , bool parseSuffix = true ) ; 150void CompileFile ( const char * fileName , ShHandle ) ; 151void usage () ; 152char * ReadFileData ( const char * fileName ) ; 153void FreeFileData ( char * data ) ; 154void InfoLogMsg ( const char * msg , const char * name , const int num ) ; 155 156// Globally track if any compile or link failure. 157std :: atomic < int8_t > CompileFailed { 0 } ; 158std :: atomic < int8_t > LinkFailed { 0 } ; 159std :: atomic < int8_t > CompileOrLinkFailed { 0 } ; 160 161// array of unique places to leave the shader names and infologs for the asynchronous compiles 162std :: vector < std :: unique_ptr < glslang :: TWorkItem >> WorkItems ; 163 164std :: string ConfigFile ; 165 166// 167// Parse either a .conf file provided by the user or the default from glslang::DefaultTBuiltInResource 168// 169void ProcessConfigFile () 170{ 171if ( ConfigFile . size () == 0 ) 172* GetResources () = * GetDefaultResources () ; 173else { 174char * configString = ReadFileData ( ConfigFile . c_str ()) ; 175DecodeResourceLimits ( GetResources () , configString ) ; 176FreeFileData ( configString ) ; 177} 178} 179 180int ReflectOptions = EShReflectionDefault ; 181std :: underlying_type_t < TOptions > Options = EOptionNone ; 182const char * ExecutableName = nullptr ; 183const char * binaryFileName = nullptr ; 184const char * depencyFileName = nullptr ; 185const char * entryPointName = nullptr ; 186const char * sourceEntryPointName = nullptr ; 187const char * shaderStageName = nullptr ; 188const char * variableName = nullptr ; 189bool HlslEnable16BitTypes = false ; 190bool HlslDX9compatible = false ; 191bool HlslDxPositionW = false ; 192bool EnhancedMsgs = false ; 193bool AbsolutePath = false ; 194bool DumpBuiltinSymbols = false ; 195std :: vector < std :: string > IncludeDirectoryList ; 196 197// Source environment 198// (source 'Client' is currently the same as target 'Client') 199int ClientInputSemanticsVersion = 100 ; 200 201// Target environment 202glslang :: EShClient Client = glslang :: EShClientNone ; // will stay EShClientNone if only validating 203glslang :: EShTargetClientVersion ClientVersion ; // not valid until Client is set 204glslang :: EShTargetLanguage TargetLanguage = glslang :: EShTargetNone ; 205glslang :: EShTargetLanguageVersion TargetVersion ; // not valid until TargetLanguage is set 206 207// GLSL version 208int GlslVersion = 0 ; // GLSL version specified on CLI, overrides #version in shader source 209 210std :: vector < std :: string > Processes ; // what should be recorded by OpModuleProcessed, or equivalent 211std :: string CommandLineArguments ; // original arguments passed to the standalone compiler 212 213// Per descriptor-set binding base data 214typedef std :: map < unsigned int , unsigned int > TPerSetBaseBinding ; 215 216std :: vector < std :: pair < std :: string , int >> uniformLocationOverrides ; 217int uniformBase = 0 ; 218 219std :: array < std :: array < unsigned int , EShLangCount > , glslang :: EResCount > baseBinding ; 220std :: array < std :: array < TPerSetBaseBinding , EShLangCount > , glslang :: EResCount > baseBindingForSet ; 221std :: array < std :: vector < std :: string > , EShLangCount > baseResourceSetBinding ; 222 223std :: vector < std :: pair < std :: string , glslang :: TBlockStorageClass >> blockStorageOverrides ; 224 225bool setGlobalUniformBlock = false ; 226std :: string globalUniformName ; 227unsigned int globalUniformBinding ; 228unsigned int globalUniformSet ; 229 230bool setGlobalBufferBlock = false ; 231std :: string atomicCounterBlockName ; 232unsigned int atomicCounterBlockSet ; 233 234// Add things like "#define ..." to a preamble to use in the beginning of the shader. 235class TPreamble { 236public : 237TPreamble () { } 238 239bool isSet () const { return text . size () > 0 ; } 240const char * get () const { return text . c_str () ; } 241 242// #define... 243void addDef ( std :: string def ) 244{ 245text . append ( "#define " ) ; 246fixLine ( def ) ; 247 248Processes . push_back ( "define-macro " ) ; 249Processes . back () . append ( def ) ; 250 251// The first "=" needs to turn into a space 252const size_t equal = def . find_first_of ( "=" ) ; 253if ( equal != def . npos ) 254def [ equal ] = ' ' ; 255 256text . append ( def ) ; 257text . append ( " \n " ) ; 258} 259 260// #undef... 261void addUndef ( std :: string undef ) 262{ 263text . append ( "#undef " ) ; 264fixLine ( undef ) ; 265 266Processes . push_back ( "undef-macro " ) ; 267Processes . back () . append ( undef ) ; 268 269text . append ( undef ) ; 270text . append ( " \n " ) ; 271} 272 273void addText ( std :: string preambleText ) 274{ 275fixLine ( preambleText ) ; 276 277Processes . push_back ( "preamble-text" ) ; 278Processes . back () . append ( preambleText ) ; 279 280text . append ( preambleText ) ; 281text . append ( " \n " ) ; 282} 283 284protected : 285void fixLine ( std :: string & line ) 286{ 287// Can't go past a newline in the line 288const size_t end = line . find_first_of ( " \n " ) ; 289if ( end != line . npos ) 290line = line . substr ( 0 , end ) ; 291} 292 293std :: string text ; // contents of preamble 294} ; 295 296// Track the user's #define and #undef from the command line. 297TPreamble UserPreamble ; 298std :: string PreambleString ; 299 300// 301// Create the default name for saving a binary if -o is not provided. 302// 303const char * GetBinaryName ( EShLanguage stage ) 304{ 305const char * name ; 306if ( binaryFileName == nullptr ) { 307switch ( stage ) { 308case EShLangVertex : name = "vert.spv" ; break ; 309case EShLangTessControl : name = "tesc.spv" ; break ; 310case EShLangTessEvaluation : name = "tese.spv" ; break ; 311case EShLangGeometry : name = "geom.spv" ; break ; 312case EShLangFragment : name = "frag.spv" ; break ; 313case EShLangCompute : name = "comp.spv" ; break ; 314case EShLangRayGen : name = "rgen.spv" ; break ; 315case EShLangIntersect : name = "rint.spv" ; break ; 316case EShLangAnyHit : name = "rahit.spv" ; break ; 317case EShLangClosestHit : name = "rchit.spv" ; break ; 318case EShLangMiss : name = "rmiss.spv" ; break ; 319case EShLangCallable : name = "rcall.spv" ; break ; 320case EShLangMesh : name = "mesh.spv" ; break ; 321case EShLangTask : name = "task.spv" ; break ; 322default : name = "unknown" ; break ; 323} 324} else 325name = binaryFileName ; 326 327return name ; 328} 329 330// 331// *.conf => this is a config file that can set limits/resources 332// 333bool SetConfigFile ( const std :: string & name ) 334{ 335if ( name . size () < 5 ) 336return false ; 337 338if ( name . compare ( name . size () - 5 , 5 , ".conf" ) == 0 ) { 339ConfigFile = name ; 340return true ; 341} 342 343return false ; 344} 345 346// 347// Give error and exit with failure code. 348// 349void Error ( const char * message , const char * detail = nullptr ) 350{ 351fprintf ( stderr , "%s: Error: " , ExecutableName ) ; 352if ( detail != nullptr ) 353fprintf ( stderr , "%s: " , detail ) ; 354fprintf ( stderr , "%s (use -h for usage) \n " , message ) ; 355exit ( EFailUsage ) ; 356} 357 358// 359// Process an optional binding base of one the forms: 360// --argname [stage] base // base for stage (if given) or all stages (if not) 361// --argname [stage] [base set]... // set/base pairs: set the base for given binding set. 362 363// Where stage is one of the forms accepted by FindLanguage, and base is an integer 364// 365void ProcessBindingBase ( int & argc , char **& argv , glslang :: TResourceType res ) 366{ 367if ( argc < 2 ) 368usage () ; 369 370EShLanguage lang = EShLangCount ; 371int singleBase = 0 ; 372TPerSetBaseBinding perSetBase ; 373int arg = 1 ; 374 375// Parse stage, if given 376if ( ! isdigit ( argv [ arg ][ 0 ])) { 377if ( argc < 3 ) // this form needs one more argument 378usage () ; 379 380lang = FindLanguage ( argv [ arg ++ ] , false ) ; 381} 382 383if (( argc - arg ) >= 2 && isdigit ( argv [ arg + 0 ][ 0 ]) && isdigit ( argv [ arg + 1 ][ 0 ])) { 384// Parse a per-set binding base 385do { 386const int baseNum = atoi ( argv [ arg ++ ]) ; 387const int setNum = atoi ( argv [ arg ++ ]) ; 388perSetBase [ setNum ] = baseNum ; 389} while (( argc - arg ) >= 2 && isdigit ( argv [ arg + 0 ][ 0 ]) && isdigit ( argv [ arg + 1 ][ 0 ])) ; 390} else { 391// Parse single binding base 392singleBase = atoi ( argv [ arg ++ ]) ; 393} 394 395argc -= ( arg - 1 ) ; 396argv += ( arg - 1 ) ; 397 398// Set one or all languages 399const int langMin = ( lang < EShLangCount ) ? lang + 0 : 0 ; 400const int langMax = ( lang < EShLangCount ) ? lang + 1 : EShLangCount ; 401 402for ( int lang = langMin ; lang < langMax ; ++ lang ) { 403if ( ! perSetBase . empty ()) 404baseBindingForSet [ res ][ lang ] . insert ( perSetBase . begin () , perSetBase . end ()) ; 405else 406baseBinding [ res ][ lang ] = singleBase ; 407} 408} 409 410void ProcessResourceSetBindingBase ( int & argc , char **& argv , std :: array < std :: vector < std :: string > , EShLangCount >& base ) 411{ 412if ( argc < 2 ) 413usage () ; 414 415if ( ! isdigit ( argv [ 1 ][ 0 ])) { 416if ( argc < 3 ) // this form needs one more argument 417usage () ; 418 419// Parse form: --argname stage [regname set base...], or: 420// --argname stage set 421const EShLanguage lang = FindLanguage ( argv [ 1 ] , false ) ; 422 423argc -- ; 424argv ++ ; 425 426while ( argc > 1 && argv [ 1 ] != nullptr && argv [ 1 ][ 0 ] != '-' ) { 427base [ lang ] . push_back ( argv [ 1 ]) ; 428 429argc -- ; 430argv ++ ; 431} 432 433// Must have one arg, or a multiple of three (for [regname set binding] triples) 434if ( base [ lang ] . size () != 1 && ( base [ lang ] . size () % 3 ) != 0 ) 435usage () ; 436 437} else { 438// Parse form: --argname set 439for ( int lang = 0 ; lang < EShLangCount ; ++ lang ) 440base [ lang ] . push_back ( argv [ 1 ]) ; 441 442argc -- ; 443argv ++ ; 444} 445} 446 447// 448// Process an optional binding base of one the forms: 449// --argname name {uniform|buffer|push_constant} 450void ProcessBlockStorage ( int & argc , char **& argv , std :: vector < std :: pair < std :: string , glslang :: TBlockStorageClass >>& storage ) 451{ 452if ( argc < 3 ) 453usage () ; 454 455glslang :: TBlockStorageClass blockStorage = glslang :: EbsNone ; 456 457std :: string strBacking ( argv [ 2 ]) ; 458if ( strBacking == "uniform" ) 459blockStorage = glslang :: EbsUniform ; 460else if ( strBacking == "buffer" ) 461blockStorage = glslang :: EbsStorageBuffer ; 462else if ( strBacking == "push_constant" ) 463blockStorage = glslang :: EbsPushConstant ; 464else { 465printf ( "%s: invalid block storage \n " , strBacking . c_str ()) ; 466usage () ; 467} 468 469storage . push_back ( std :: make_pair ( std :: string ( argv [ 1 ]) , blockStorage )) ; 470 471argc -= 2 ; 472argv += 2 ; 473} 474 475inline bool isNonDigit ( char c ) { 476// a non-digit character valid in a glsl identifier 477return ( c == '_' ) || ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) ; 478} 479 480// whether string isa valid identifier to be used in glsl 481bool isValidIdentifier ( const char * str ) { 482std :: string idn ( str ) ; 483 484if ( idn . length () == 0 ) { 485return false ; 486} 487 488if ( idn . length () >= 3 && idn . substr ( 0 , 3 ) == "gl_" ) { 489// identifiers startin with "gl_" are reserved 490return false ; 491} 492 493if ( ! isNonDigit ( idn [ 0 ])) { 494return false ; 495} 496 497for ( unsigned int i = 1 ; i < idn . length () ; ++ i ) { 498if ( ! ( isdigit ( idn [ i ]) || isNonDigit ( idn [ i ]))) { 499return false ; 500} 501} 502 503return true ; 504} 505 506// Process settings for either the global buffer block or global unfirom block 507// of the form: 508// --argname name set binding 509void ProcessGlobalBlockSettings ( int & argc , char **& argv , std :: string * name , unsigned int * set , unsigned int * binding ) 510{ 511if ( argc < 4 ) 512usage () ; 513 514unsigned int curArg = 1 ; 515 516assert ( name || set || binding ) ; 517 518if ( name ) { 519if ( ! isValidIdentifier ( argv [ curArg ])) { 520printf ( "%s: invalid identifier \n " , argv [ curArg ]) ; 521usage () ; 522} 523* name = argv [ curArg ] ; 524 525curArg ++ ; 526} 527 528if ( set ) { 529errno = 0 ; 530int setVal = static_cast < int > ( :: strtol ( argv [ curArg ] , nullptr , 10 )) ; 531if ( errno || setVal < 0 ) { 532printf ( "%s: invalid set \n " , argv [ curArg ]) ; 533usage () ; 534} 535* set = setVal ; 536 537curArg ++ ; 538} 539 540if ( binding ) { 541errno = 0 ; 542int bindingVal = static_cast < int > ( :: strtol ( argv [ curArg ] , nullptr , 10 )) ; 543if ( errno || bindingVal < 0 ) { 544printf ( "%s: invalid binding \n " , argv [ curArg ]) ; 545usage () ; 546} 547* binding = bindingVal ; 548 549curArg ++ ; 550} 551 552argc -= ( curArg - 1 ) ; 553argv += ( curArg - 1 ) ; 554} 555 556// 557// Do all command-line argument parsing. This includes building up the work-items 558// to be processed later, and saving all the command-line options. 559// 560// Does not return (it exits) if command-line is fatally flawed. 561// 562void ProcessArguments ( std :: vector < std :: unique_ptr < glslang :: TWorkItem >>& workItems , int argc , char * argv []) 563{ 564for ( int res = 0 ; res < glslang :: EResCount ; ++ res ) 565baseBinding [ res ] . fill ( 0 ) ; 566 567ExecutableName = argv [ 0 ] ; 568workItems . reserve ( argc ) ; 569 570CommandLineArguments . clear () ; 571for ( int arg = 1 ; arg < argc ; ++ arg ) { 572if ( arg > 1 ) 573CommandLineArguments += ' ' ; 574CommandLineArguments += argv [ arg ] ; 575} 576 577const auto bumpArg = [ & ]() { 578if ( argc > 0 ) { 579argc -- ; 580argv ++ ; 581} 582} ; 583 584// read a string directly attached to a single-letter option 585const auto getStringOperand = [ & ]( const char * desc ) { 586if ( argv [ 0 ][ 2 ] == 0 ) { 587printf ( "%s must immediately follow option (no spaces) \n " , desc ) ; 588exit ( EFailUsage ) ; 589} 590return argv [ 0 ] + 2 ; 591} ; 592 593// read a number attached to a single-letter option 594const auto getAttachedNumber = [ & ]( const char * desc ) { 595int num = atoi ( argv [ 0 ] + 2 ) ; 596if ( num == 0 ) { 597printf ( "%s: expected attached non-0 number \n " , desc ) ; 598exit ( EFailUsage ) ; 599} 600return num ; 601} ; 602 603// minimum needed (without overriding something else) to target Vulkan SPIR-V 604const auto setVulkanSpv = []() { 605if ( Client == glslang :: EShClientNone ) 606ClientVersion = glslang :: EShTargetVulkan_1_0 ; 607Client = glslang :: EShClientVulkan ; 608Options |= EOptionSpv ; 609Options |= EOptionVulkanRules ; 610Options |= EOptionLinkProgram ; 611} ; 612 613// minimum needed (without overriding something else) to target OpenGL SPIR-V 614const auto setOpenGlSpv = []() { 615if ( Client == glslang :: EShClientNone ) 616ClientVersion = glslang :: EShTargetOpenGL_450 ; 617Client = glslang :: EShClientOpenGL ; 618Options |= EOptionSpv ; 619Options |= EOptionLinkProgram ; 620// undo a -H default to Vulkan 621Options &= ~ EOptionVulkanRules ; 622} ; 623 624const auto getUniformOverride = [ getStringOperand ]() { 625const char * arg = getStringOperand ( "-u<name>:<location>" ) ; 626const char * split = strchr ( arg , ':' ) ; 627if ( split == nullptr ) { 628printf ( "%s: missing location \n " , arg ) ; 629exit ( EFailUsage ) ; 630} 631errno = 0 ; 632int location = static_cast < int > ( :: strtol ( split + 1 , nullptr , 10 )) ; 633if ( errno ) { 634printf ( "%s: invalid location \n " , arg ) ; 635exit ( EFailUsage ) ; 636} 637return std :: make_pair ( std :: string ( arg , split - arg ) , location ) ; 638} ; 639 640for ( bumpArg () ; argc >= 1 ; bumpArg ()) { 641if ( argv [ 0 ][ 0 ] == '-' ) { 642switch ( argv [ 0 ][ 1 ]) { 643case '-' : 644{ 645std :: string lowerword ( argv [ 0 ] + 2 ) ; 646std :: transform ( lowerword . begin () , lowerword . end () , lowerword . begin () , :: tolower ) ; 647 648// handle --word style options 649if ( lowerword == "auto-map-bindings" || // synonyms 650lowerword == "auto-map-binding" || 651lowerword == "amb" ) { 652Options |= EOptionAutoMapBindings ; 653} else if ( lowerword == "relax-set-binding-limits" ) { 654Options |= EOptionRelaxSetBindingLimits ; 655} else if ( lowerword == "auto-map-locations" || // synonyms 656lowerword == "aml" ) { 657Options |= EOptionAutoMapLocations ; 658} else if ( lowerword == "uniform-base" ) { 659if ( argc <= 1 ) 660Error ( "no <base> provided" , lowerword . c_str ()) ; 661uniformBase = static_cast < int > ( :: strtol ( argv [ 1 ] , nullptr , 10 )) ; 662bumpArg () ; 663break ; 664} else if ( lowerword == "client" ) { 665if ( argc > 1 ) { 666if ( strcmp ( argv [ 1 ] , "vulkan100" ) == 0 ) 667setVulkanSpv () ; 668else if ( strcmp ( argv [ 1 ] , "opengl100" ) == 0 ) 669setOpenGlSpv () ; 670else 671Error ( "expects vulkan100 or opengl100" , lowerword . c_str ()) ; 672} else 673Error ( "expects vulkan100 or opengl100" , lowerword . c_str ()) ; 674bumpArg () ; 675} else if ( lowerword == "define-macro" || 676lowerword == "d" ) { 677if ( argc > 1 ) 678UserPreamble . addDef ( argv [ 1 ]) ; 679else 680Error ( "expects <name[=def]>" , argv [ 0 ]) ; 681bumpArg () ; 682} else if ( lowerword == "dump-builtin-symbols" ) { 683DumpBuiltinSymbols = true ; 684} else if ( lowerword == "entry-point" ) { 685entryPointName = argv [ 1 ] ; 686if ( argc <= 1 ) 687Error ( "no <name> provided" , lowerword . c_str ()) ; 688bumpArg () ; 689} else if ( lowerword == "flatten-uniform-arrays" || // synonyms 690lowerword == "flatten-uniform-array" || 691lowerword == "fua" ) { 692Options |= EOptionFlattenUniformArrays ; 693} else if ( lowerword == "glsl-version" ) { 694if ( argc > 1 ) { 695if ( strcmp ( argv [ 1 ] , "100" ) == 0 ) { 696GlslVersion = 100 ; 697} else if ( strcmp ( argv [ 1 ] , "110" ) == 0 ) { 698GlslVersion = 110 ; 699} else if ( strcmp ( argv [ 1 ] , "120" ) == 0 ) { 700GlslVersion = 120 ; 701} else if ( strcmp ( argv [ 1 ] , "130" ) == 0 ) { 702GlslVersion = 130 ; 703} else if ( strcmp ( argv [ 1 ] , "140" ) == 0 ) { 704GlslVersion = 140 ; 705} else if ( strcmp ( argv [ 1 ] , "150" ) == 0 ) { 706GlslVersion = 150 ; 707} else if ( strcmp ( argv [ 1 ] , "300es" ) == 0 ) { 708GlslVersion = 300 ; 709} else if ( strcmp ( argv [ 1 ] , "310es" ) == 0 ) { 710GlslVersion = 310 ; 711} else if ( strcmp ( argv [ 1 ] , "320es" ) == 0 ) { 712GlslVersion = 320 ; 713} else if ( strcmp ( argv [ 1 ] , "330" ) == 0 ) { 714GlslVersion = 330 ; 715} else if ( strcmp ( argv [ 1 ] , "400" ) == 0 ) { 716GlslVersion = 400 ; 717} else if ( strcmp ( argv [ 1 ] , "410" ) == 0 ) { 718GlslVersion = 410 ; 719} else if ( strcmp ( argv [ 1 ] , "420" ) == 0 ) { 720GlslVersion = 420 ; 721} else if ( strcmp ( argv [ 1 ] , "430" ) == 0 ) { 722GlslVersion = 430 ; 723} else if ( strcmp ( argv [ 1 ] , "440" ) == 0 ) { 724GlslVersion = 440 ; 725} else if ( strcmp ( argv [ 1 ] , "450" ) == 0 ) { 726GlslVersion = 450 ; 727} else if ( strcmp ( argv [ 1 ] , "460" ) == 0 ) { 728GlslVersion = 460 ; 729} else 730Error ( "--glsl-version expected one of: 100, 110, 120, 130, 140, 150, \n " 731"300es, 310es, 320es, 330 \n " 732"400, 410, 420, 430, 440, 450, 460" ) ; 733} 734bumpArg () ; 735} else if ( lowerword == "hlsl-offsets" ) { 736Options |= EOptionHlslOffsets ; 737} else if ( lowerword == "hlsl-iomap" || 738lowerword == "hlsl-iomapper" || 739lowerword == "hlsl-iomapping" ) { 740Options |= EOptionHlslIoMapping ; 741} else if ( lowerword == "hlsl-enable-16bit-types" ) { 742HlslEnable16BitTypes = true ; 743} else if ( lowerword == "hlsl-dx9-compatible" ) { 744HlslDX9compatible = true ; 745} else if ( lowerword == "hlsl-dx-position-w" ) { 746HlslDxPositionW = true ; 747} else if ( lowerword == "enhanced-msgs" ) { 748EnhancedMsgs = true ; 749} else if ( lowerword == "absolute-path" ) { 750AbsolutePath = true ; 751} else if ( lowerword == "auto-sampled-textures" ) { 752autoSampledTextures = true ; 753} else if ( lowerword == "discard-is-terminate" ) { 754Options |= EOptionDiscardIsTerminate ; 755} else if ( lowerword == "invert-y" || // synonyms 756lowerword == "iy" ) { 757Options |= EOptionInvertY ; 758} else if ( lowerword == "keep-uncalled" || // synonyms 759lowerword == "ku" ) { 760Options |= EOptionKeepUncalled ; 761} else if ( lowerword == "nan-clamp" ) { 762NaNClamp = true ; 763} else if ( lowerword == "no-storage-format" || // synonyms 764lowerword == "nsf" ) { 765Options |= EOptionNoStorageFormat ; 766} else if ( lowerword == "preamble-text" || 767lowerword == "p" ) { 768if ( argc > 1 ) 769UserPreamble . addText ( argv [ 1 ]) ; 770else 771Error ( "expects <text>" , argv [ 0 ]) ; 772bumpArg () ; 773} else if ( lowerword == "relaxed-errors" ) { 774Options |= EOptionRelaxedErrors ; 775} else if ( lowerword == "reflect-strict-array-suffix" ) { 776ReflectOptions |= EShReflectionStrictArraySuffix ; 777} else if ( lowerword == "reflect-basic-array-suffix" ) { 778ReflectOptions |= EShReflectionBasicArraySuffix ; 779} else if ( lowerword == "reflect-intermediate-io" ) { 780ReflectOptions |= EShReflectionIntermediateIO ; 781} else if ( lowerword == "reflect-separate-buffers" ) { 782ReflectOptions |= EShReflectionSeparateBuffers ; 783} else if ( lowerword == "reflect-all-block-variables" ) { 784ReflectOptions |= EShReflectionAllBlockVariables ; 785} else if ( lowerword == "reflect-unwrap-io-blocks" ) { 786ReflectOptions |= EShReflectionUnwrapIOBlocks ; 787} else if ( lowerword == "reflect-all-io-variables" ) { 788ReflectOptions |= EShReflectionAllIOVariables ; 789} else if ( lowerword == "reflect-shared-std140-ubo" ) { 790ReflectOptions |= EShReflectionSharedStd140UBO ; 791} else if ( lowerword == "reflect-shared-std140-ssbo" ) { 792ReflectOptions |= EShReflectionSharedStd140SSBO ; 793} else if ( lowerword == "resource-set-bindings" || // synonyms 794lowerword == "resource-set-binding" || 795lowerword == "rsb" ) { 796ProcessResourceSetBindingBase ( argc , argv , baseResourceSetBinding ) ; 797} else if ( lowerword == "set-block-storage" || 798lowerword == "sbs" ) { 799ProcessBlockStorage ( argc , argv , blockStorageOverrides ) ; 800} else if ( lowerword == "set-atomic-counter-block" || 801lowerword == "sacb" ) { 802ProcessGlobalBlockSettings ( argc , argv , & atomicCounterBlockName , & atomicCounterBlockSet , nullptr ) ; 803setGlobalBufferBlock = true ; 804} else if ( lowerword == "set-default-uniform-block" || 805lowerword == "sdub" ) { 806ProcessGlobalBlockSettings ( argc , argv , & globalUniformName , & globalUniformSet , & globalUniformBinding ) ; 807setGlobalUniformBlock = true ; 808} else if ( lowerword == "shift-image-bindings" || // synonyms 809lowerword == "shift-image-binding" || 810lowerword == "sib" ) { 811ProcessBindingBase ( argc , argv , glslang :: EResImage ) ; 812} else if ( lowerword == "shift-sampler-bindings" || // synonyms 813lowerword == "shift-sampler-binding" || 814lowerword == "ssb" ) { 815ProcessBindingBase ( argc , argv , glslang :: EResSampler ) ; 816} else if ( lowerword == "shift-uav-bindings" || // synonyms 817lowerword == "shift-uav-binding" || 818lowerword == "suavb" ) { 819ProcessBindingBase ( argc , argv , glslang :: EResUav ) ; 820} else if ( lowerword == "shift-texture-bindings" || // synonyms 821lowerword == "shift-texture-binding" || 822lowerword == "stb" ) { 823ProcessBindingBase ( argc , argv , glslang :: EResTexture ) ; 824} else if ( lowerword == "shift-ubo-bindings" || // synonyms 825lowerword == "shift-ubo-binding" || 826lowerword == "shift-cbuffer-bindings" || 827lowerword == "shift-cbuffer-binding" || 828lowerword == "sub" || 829lowerword == "scb" ) { 830ProcessBindingBase ( argc , argv , glslang :: EResUbo ) ; 831} else if ( lowerword == "shift-ssbo-bindings" || // synonyms 832lowerword == "shift-ssbo-binding" || 833lowerword == "sbb" ) { 834ProcessBindingBase ( argc , argv , glslang :: EResSsbo ) ; 835} else if ( lowerword == "shift-combined-sampler-bindings" || 836lowerword == "shift-combined-sampler-binding" || 837lowerword == "scsb" ) { 838ProcessBindingBase ( argc , argv , glslang :: EResCombinedSampler ) ; 839} else if ( lowerword == "shift-as-bindings" || 840lowerword == "shift-as-binding" || 841lowerword == "sab" ) { 842ProcessBindingBase ( argc , argv , glslang :: EResAs ) ; 843} else if ( lowerword == "source-entrypoint" || // synonyms 844lowerword == "sep" ) { 845if ( argc <= 1 ) 846Error ( "no <entry-point> provided" , lowerword . c_str ()) ; 847sourceEntryPointName = argv [ 1 ] ; 848bumpArg () ; 849break ; 850} else if ( lowerword == "spirv-dis" ) { 851SpvToolsDisassembler = true ; 852} else if ( lowerword == "spirv-val" ) { 853SpvToolsValidate = true ; 854} else if ( lowerword == "stdin" ) { 855Options |= EOptionStdin ; 856shaderStageName = argv [ 1 ] ; 857} else if ( lowerword == "suppress-warnings" ) { 858Options |= EOptionSuppressWarnings ; 859} else if ( lowerword == "target-env" ) { 860if ( argc > 1 ) { 861if ( strcmp ( argv [ 1 ] , "vulkan1.0" ) == 0 ) { 862setVulkanSpv () ; 863ClientVersion = glslang :: EShTargetVulkan_1_0 ; 864} else if ( strcmp ( argv [ 1 ] , "vulkan1.1" ) == 0 ) { 865setVulkanSpv () ; 866ClientVersion = glslang :: EShTargetVulkan_1_1 ; 867} else if ( strcmp ( argv [ 1 ] , "vulkan1.2" ) == 0 ) { 868setVulkanSpv () ; 869ClientVersion = glslang :: EShTargetVulkan_1_2 ; 870} else if ( strcmp ( argv [ 1 ] , "vulkan1.3" ) == 0 ) { 871setVulkanSpv () ; 872ClientVersion = glslang :: EShTargetVulkan_1_3 ; 873} else if ( strcmp ( argv [ 1 ] , "vulkan1.4" ) == 0 ) { 874setVulkanSpv () ; 875ClientVersion = glslang :: EShTargetVulkan_1_4 ; 876} else if ( strcmp ( argv [ 1 ] , "opengl" ) == 0 ) { 877setOpenGlSpv () ; 878ClientVersion = glslang :: EShTargetOpenGL_450 ; 879} else if ( strcmp ( argv [ 1 ] , "spirv1.0" ) == 0 ) { 880TargetLanguage = glslang :: EShTargetSpv ; 881TargetVersion = glslang :: EShTargetSpv_1_0 ; 882} else if ( strcmp ( argv [ 1 ] , "spirv1.1" ) == 0 ) { 883TargetLanguage = glslang :: EShTargetSpv ; 884TargetVersion = glslang :: EShTargetSpv_1_1 ; 885} else if ( strcmp ( argv [ 1 ] , "spirv1.2" ) == 0 ) { 886TargetLanguage = glslang :: EShTargetSpv ; 887TargetVersion = glslang :: EShTargetSpv_1_2 ; 888} else if ( strcmp ( argv [ 1 ] , "spirv1.3" ) == 0 ) { 889TargetLanguage = glslang :: EShTargetSpv ; 890TargetVersion = glslang :: EShTargetSpv_1_3 ; 891} else if ( strcmp ( argv [ 1 ] , "spirv1.4" ) == 0 ) { 892TargetLanguage = glslang :: EShTargetSpv ; 893TargetVersion = glslang :: EShTargetSpv_1_4 ; 894} else if ( strcmp ( argv [ 1 ] , "spirv1.5" ) == 0 ) { 895TargetLanguage = glslang :: EShTargetSpv ; 896TargetVersion = glslang :: EShTargetSpv_1_5 ; 897} else if ( strcmp ( argv [ 1 ] , "spirv1.6" ) == 0 ) { 898TargetLanguage = glslang :: EShTargetSpv ; 899TargetVersion = glslang :: EShTargetSpv_1_6 ; 900} else 901Error ( "--target-env expected one of: vulkan1.0, vulkan1.1, vulkan1.2, \n " 902"vulkan1.3, vulkan1.4, opengl, spirv1.0, spirv1.1, spirv1.2, spirv1.3, \n " 903"spirv1.4, spirv1.5 or spirv1.6" ) ; 904} 905bumpArg () ; 906} else if ( lowerword == "undef-macro" || 907lowerword == "u" ) { 908if ( argc > 1 ) 909UserPreamble . addUndef ( argv [ 1 ]) ; 910else 911Error ( "expects <name>" , argv [ 0 ]) ; 912bumpArg () ; 913} else if ( lowerword == "variable-name" || // synonyms 914lowerword == "vn" ) { 915Options |= EOptionOutputHexadecimal ; 916if ( argc <= 1 ) 917Error ( "no <C-variable-name> provided" , lowerword . c_str ()) ; 918variableName = argv [ 1 ] ; 919bumpArg () ; 920break ; 921} else if ( lowerword == "quiet" ) { 922beQuiet = true ; 923} else if ( lowerword == "depfile" ) { 924if ( argc <= 1 ) 925Error ( "no <depfile-name> provided" , lowerword . c_str ()) ; 926depencyFileName = argv [ 1 ] ; 927bumpArg () ; 928} else if ( lowerword == "version" ) { 929Options |= EOptionDumpVersions ; 930} else if ( lowerword == "no-link" ) { 931Options |= EOptionCompileOnly ; 932} else if ( lowerword == "error-column" ) { 933Options |= EOptionDisplayErrorColumn ; 934} else if ( lowerword == "lto" ) { 935Options |= EOptionLinkTimeOptimization ; 936} else if ( lowerword == "validate-io" ) { 937Options |= EOptionValidateCrossStageIO ; 938} else if ( lowerword == "bindings-per-resource-type" ) { 939Options |= EOptionBindingsPerResourceType ; 940} else if ( lowerword == "help" ) { 941usage () ; 942break ; 943} else { 944Error ( "unrecognized command-line option" , argv [ 0 ]) ; 945} 946} 947break ; 948case 'C' : 949Options |= EOptionCascadingErrors ; 950break ; 951case 'D' : 952if ( argv [ 0 ][ 2 ] == 0 ) 953Options |= EOptionReadHlsl ; 954else 955UserPreamble . addDef ( getStringOperand ( "-D<name[=def]>" )) ; 956break ; 957case 'u' : 958uniformLocationOverrides . push_back ( getUniformOverride ()) ; 959break ; 960case 'E' : 961Options |= EOptionOutputPreprocessed ; 962break ; 963case 'G' : 964// OpenGL client 965setOpenGlSpv () ; 966if ( argv [ 0 ][ 2 ] != 0 ) 967ClientInputSemanticsVersion = getAttachedNumber ( "-G<num> client input semantics" ) ; 968if ( ClientInputSemanticsVersion != 100 ) 969Error ( "unknown client version for -G, should be 100" ) ; 970break ; 971case 'H' : 972Options |= EOptionHumanReadableSpv ; 973if (( Options & EOptionSpv ) == 0 ) { 974// default to Vulkan 975setVulkanSpv () ; 976} 977break ; 978case 'I' : 979IncludeDirectoryList . push_back ( getStringOperand ( "-I<dir> include path" )) ; 980break ; 981case 'O' : 982if ( argv [ 0 ][ 2 ] == 'd' ) 983Options |= EOptionOptimizeDisable ; 984else if ( argv [ 0 ][ 2 ] == 's' ) 985#if ENABLE_OPT 986Options |= EOptionOptimizeSize ; 987#else 988Error ( "-Os not available; optimizer not linked" ) ; 989#endif 990else if ( argv [ 0 ][ 2 ] == ' \0 ' ) 991#if ENABLE_OPT 992Options |= EOptionOptimizePerformance ; 993#else 994Error ( "-O not available; optimizer not linked" ) ; 995#endif 996else 997Error ( "unknown -O option" ) ; 998break ; 999case 'P' : 1000UserPreamble . addText ( getStringOperand ( "-P<text>" )) ; 1001break ; 1002case 'R' : 1003VulkanRulesRelaxed = true ; 1004break ; 1005case 'S' : 1006if ( argc <= 1 ) 1007Error ( "no <stage> specified for -S" ) ; 1008shaderStageName = argv [ 1 ] ; 1009bumpArg () ; 1010break ; 1011case 'U' : 1012UserPreamble . addUndef ( getStringOperand ( "-U<name>" )) ; 1013break ; 1014case 'V' : 1015setVulkanSpv () ; 1016if ( argv [ 0 ][ 2 ] != 0 ) 1017ClientInputSemanticsVersion = getAttachedNumber ( "-V<num> client input semantics" ) ; 1018if ( ClientInputSemanticsVersion != 100 ) 1019Error ( "unknown client version for -V, should be 100" ) ; 1020break ; 1021case 'c' : 1022Options |= EOptionDumpConfig ; 1023break ; 1024case 'd' : 1025if ( strncmp ( & argv [ 0 ][ 1 ] , "dumpversion" , strlen ( & argv [ 0 ][ 1 ]) + 1 ) == 0 || 1026strncmp ( & argv [ 0 ][ 1 ] , "dumpfullversion" , strlen ( & argv [ 0 ][ 1 ]) + 1 ) == 0 ) 1027Options |= EOptionDumpBareVersion ; 1028else 1029Options |= EOptionDefaultDesktop ; 1030break ; 1031case 'e' : 1032entryPointName = argv [ 1 ] ; 1033if ( argc <= 1 ) 1034Error ( "no <name> provided for -e" ) ; 1035bumpArg () ; 1036break ; 1037case 'f' : 1038if ( strcmp ( & argv [ 0 ][ 2 ] , "hlsl_functionality1" ) == 0 ) 1039targetHlslFunctionality1 = true ; 1040else 1041Error ( "-f: expected hlsl_functionality1" ) ; 1042break ; 1043case 'g' : 1044// Override previous -g or -g0 argument 1045stripDebugInfo = false ; 1046emitNonSemanticShaderDebugInfo = false ; 1047Options &= ~ EOptionDebug ; 1048if ( argv [ 0 ][ 2 ] == '0' ) 1049stripDebugInfo = true ; 1050else { 1051Options |= EOptionDebug ; 1052if ( argv [ 0 ][ 2 ] == 'V' ) { 1053emitNonSemanticShaderDebugInfo = true ; 1054if ( argv [ 0 ][ 3 ] == 'S' ) { 1055emitNonSemanticShaderDebugSource = true ; 1056} else { 1057emitNonSemanticShaderDebugSource = false ; 1058} 1059} 1060} 1061break ; 1062case 'h' : 1063usage () ; 1064break ; 1065case 'i' : 1066Options |= EOptionIntermediate ; 1067break ; 1068case 'l' : 1069Options |= EOptionLinkProgram ; 1070break ; 1071case 'm' : 1072Options |= EOptionMemoryLeakMode ; 1073break ; 1074case 'o' : 1075if ( argc <= 1 ) 1076Error ( "no <file> provided for -o" ) ; 1077binaryFileName = argv [ 1 ] ; 1078bumpArg () ; 1079break ; 1080case 'q' : 1081Options |= EOptionDumpReflection ; 1082break ; 1083case 'r' : 1084Options |= EOptionRelaxedErrors ; 1085break ; 1086case 's' : 1087Options |= EOptionSuppressInfolog ; 1088break ; 1089case 't' : 1090Options |= EOptionMultiThreaded ; 1091break ; 1092case 'v' : 1093Options |= EOptionDumpVersions ; 1094break ; 1095case 'w' : 1096Options |= EOptionSuppressWarnings ; 1097break ; 1098case 'x' : 1099Options |= EOptionOutputHexadecimal ; 1100break ; 1101default : 1102Error ( "unrecognized command-line option" , argv [ 0 ]) ; 1103break ; 1104} 1105} else { 1106std :: string name ( argv [ 0 ]) ; 1107if ( ! SetConfigFile ( name )) { 1108workItems . push_back ( std :: unique_ptr < glslang :: TWorkItem > ( new glslang :: TWorkItem ( name ))) ; 1109} 1110} 1111} 1112 1113// Make sure that -S is always specified if --stdin is specified 1114if (( Options & EOptionStdin ) && shaderStageName == nullptr ) 1115Error ( "must provide -S when --stdin is given" ) ; 1116 1117// Make sure that -E is not specified alongside linking (which includes SPV generation) 1118// Or things that require linking 1119if ( Options & EOptionOutputPreprocessed ) { 1120if ( Options & EOptionLinkProgram ) 1121Error ( "can't use -E when linking is selected" ) ; 1122if ( Options & EOptionDumpReflection ) 1123Error ( "reflection requires linking, which can't be used when -E when is selected" ) ; 1124} 1125 1126// reflection requires linking 1127if (( Options & EOptionDumpReflection ) && ! ( Options & EOptionLinkProgram )) 1128Error ( "reflection requires -l for linking" ) ; 1129 1130// link time optimization makes no sense unless linking 1131if (( Options & EOptionLinkTimeOptimization ) && ! ( Options & EOptionLinkProgram )) 1132Error ( "link time optimization requires -l for linking" ) ; 1133 1134// cross stage IO validation makes no sense unless linking 1135if (( Options & EOptionValidateCrossStageIO ) && ! ( Options & EOptionLinkProgram )) 1136Error ( "cross stage IO validation requires -l for linking" ) ; 1137 1138// -o or -x makes no sense if there is no target binary 1139if ( binaryFileName && ( Options & EOptionSpv ) == 0 ) 1140Error ( "no binary generation requested (e.g., -V)" ) ; 1141 1142if (( Options & EOptionFlattenUniformArrays ) != 0 && 1143( Options & EOptionReadHlsl ) == 0 ) 1144Error ( "uniform array flattening only valid when compiling HLSL source." ) ; 1145 1146if (( Options & EOptionReadHlsl ) && ( Client == glslang :: EShClientOpenGL )) { 1147Error ( "Using HLSL input under OpenGL semantics is not currently supported." ) ; 1148} 1149 1150// rationalize client and target language 1151if ( TargetLanguage == glslang :: EShTargetNone ) { 1152switch ( ClientVersion ) { 1153case glslang :: EShTargetVulkan_1_0 : 1154TargetLanguage = glslang :: EShTargetSpv ; 1155TargetVersion = glslang :: EShTargetSpv_1_0 ; 1156break ; 1157case glslang :: EShTargetVulkan_1_1 : 1158TargetLanguage = glslang :: EShTargetSpv ; 1159TargetVersion = glslang :: EShTargetSpv_1_3 ; 1160break ; 1161case glslang :: EShTargetVulkan_1_2 : 1162TargetLanguage = glslang :: EShTargetSpv ; 1163TargetVersion = glslang :: EShTargetSpv_1_5 ; 1164break ; 1165case glslang :: EShTargetVulkan_1_3 : 1166TargetLanguage = glslang :: EShTargetSpv ; 1167TargetVersion = glslang :: EShTargetSpv_1_6 ; 1168break ; 1169case glslang :: EShTargetVulkan_1_4 : 1170TargetLanguage = glslang :: EShTargetSpv ; 1171TargetVersion = glslang :: EShTargetSpv_1_6 ; 1172break ; 1173case glslang :: EShTargetOpenGL_450 : 1174TargetLanguage = glslang :: EShTargetSpv ; 1175TargetVersion = glslang :: EShTargetSpv_1_0 ; 1176break ; 1177default : 1178break ; 1179} 1180} 1181if ( TargetLanguage != glslang :: EShTargetNone && Client == glslang :: EShClientNone ) 1182Error ( "To generate SPIR-V, also specify client semantics. See -G and -V." ) ; 1183} 1184 1185// 1186// Translate the meaningful subset of command-line options to parser-behavior options. 1187// 1188void SetMessageOptions ( EShMessages & messages ) 1189{ 1190if ( Options & EOptionRelaxedErrors ) 1191messages = ( EShMessages )( messages | EShMsgRelaxedErrors ) ; 1192if ( Options & EOptionIntermediate ) 1193messages = ( EShMessages )( messages | EShMsgAST ) ; 1194if ( Options & EOptionSuppressWarnings ) 1195messages = ( EShMessages )( messages | EShMsgSuppressWarnings ) ; 1196if ( Options & EOptionSpv ) 1197messages = ( EShMessages )( messages | EShMsgSpvRules ) ; 1198if ( Options & EOptionVulkanRules ) 1199messages = ( EShMessages )( messages | EShMsgVulkanRules ) ; 1200if ( Options & EOptionOutputPreprocessed ) 1201messages = ( EShMessages )( messages | EShMsgOnlyPreprocessor ) ; 1202if ( Options & EOptionReadHlsl ) 1203messages = ( EShMessages )( messages | EShMsgReadHlsl ) ; 1204if ( Options & EOptionCascadingErrors ) 1205messages = ( EShMessages )( messages | EShMsgCascadingErrors ) ; 1206if ( Options & EOptionKeepUncalled ) 1207messages = ( EShMessages )( messages | EShMsgKeepUncalled ) ; 1208if ( Options & EOptionHlslOffsets ) 1209messages = ( EShMessages )( messages | EShMsgHlslOffsets ) ; 1210if ( Options & EOptionDebug ) 1211messages = ( EShMessages )( messages | EShMsgDebugInfo ) ; 1212if ( HlslEnable16BitTypes ) 1213messages = ( EShMessages )( messages | EShMsgHlslEnable16BitTypes ) ; 1214if (( Options & EOptionOptimizeDisable ) || ! ENABLE_OPT ) 1215messages = ( EShMessages )( messages | EShMsgHlslLegalization ) ; 1216if ( HlslDX9compatible ) 1217messages = ( EShMessages )( messages | EShMsgHlslDX9Compatible ) ; 1218if ( DumpBuiltinSymbols ) 1219messages = ( EShMessages )( messages | EShMsgBuiltinSymbolTable ) ; 1220if ( EnhancedMsgs ) 1221messages = ( EShMessages )( messages | EShMsgEnhanced ) ; 1222if ( AbsolutePath ) 1223messages = ( EShMessages )( messages | EShMsgAbsolutePath ) ; 1224if ( Options & EOptionDisplayErrorColumn ) 1225messages = ( EShMessages )( messages | EShMsgDisplayErrorColumn ) ; 1226if ( Options & EOptionLinkTimeOptimization ) 1227messages = ( EShMessages )( messages | EShMsgLinkTimeOptimization ) ; 1228if ( Options & EOptionValidateCrossStageIO ) 1229messages = ( EShMessages )( messages | EShMsgValidateCrossStageIO ) ; 1230if ( Options & EOptionRelaxSetBindingLimits ) 1231messages = ( EShMessages )( messages | EShMsgRelaxSetBindingLimits ) ; 1232} 1233 1234// 1235// Thread entry point, for non-linking asynchronous mode. 1236// 1237void CompileShaders ( glslang :: TWorklist & worklist ) 1238{ 1239if ( Options & EOptionDebug ) 1240Error ( "cannot generate debug information unless linking to generate code" ) ; 1241 1242// NOTE: TWorkList::remove is thread-safe 1243glslang :: TWorkItem * workItem ; 1244if ( Options & EOptionStdin ) { 1245if ( worklist . remove ( workItem )) { 1246ShHandle compiler = ShConstructCompiler ( FindLanguage ( "stdin" ) , 0 ) ; 1247if ( compiler == nullptr ) 1248return ; 1249 1250CompileFile ( "stdin" , compiler ) ; 1251 1252if ( ! ( Options & EOptionSuppressInfolog )) 1253workItem -> results = ShGetInfoLog ( compiler ) ; 1254 1255ShDestruct ( compiler ) ; 1256} 1257} else { 1258while ( worklist . remove ( workItem )) { 1259ShHandle compiler = ShConstructCompiler ( FindLanguage ( workItem -> name ) , 0 ) ; 1260if ( compiler == nullptr ) 1261return ; 1262 1263 1264CompileFile ( workItem -> name . c_str () , compiler ) ; 1265 1266if ( ! ( Options & EOptionSuppressInfolog )) 1267workItem -> results = ShGetInfoLog ( compiler ) ; 1268 1269ShDestruct ( compiler ) ; 1270} 1271} 1272} 1273 1274// Outputs the given string, but only if it is non-null and non-empty. 1275// This prevents erroneous newlines from appearing. 1276void PutsIfNonEmpty ( const char * str ) 1277{ 1278if ( str && str [ 0 ]) { 1279puts ( str ) ; 1280} 1281} 1282 1283// Outputs the given string to stderr, but only if it is non-null and non-empty. 1284// This prevents erroneous newlines from appearing. 1285void StderrIfNonEmpty ( const char * str ) 1286{ 1287if ( str && str [ 0 ]) 1288fprintf ( stderr , "%s \n " , str ) ; 1289} 1290 1291// Simple bundling of what makes a compilation unit for ease in passing around, 1292// and separation of handling file IO versus API (programmatic) compilation. 1293struct ShaderCompUnit { 1294EShLanguage stage ; 1295static const int maxCount = 1 ; 1296int count ; // live number of strings/names 1297const char * text [ maxCount ] ; // memory owned/managed externally 1298std :: string fileName [ maxCount ] ; // hold's the memory, but... 1299const char * fileNameList [ maxCount ] ; // downstream interface wants pointers 1300 1301ShaderCompUnit ( EShLanguage stage ) : stage ( stage ) , count ( 0 ) { } 1302 1303ShaderCompUnit ( const ShaderCompUnit & rhs ) 1304{ 1305stage = rhs . stage ; 1306count = rhs . count ; 1307for ( int i = 0 ; i < count ; ++ i ) { 1308fileName [ i ] = rhs . fileName [ i ] ; 1309text [ i ] = rhs . text [ i ] ; 1310fileNameList [ i ] = rhs . fileName [ i ] . c_str () ; 1311} 1312} 1313 1314void addString ( std :: string & ifileName , const char * itext ) 1315{ 1316assert ( count < maxCount ) ; 1317fileName [ count ] = ifileName ; 1318text [ count ] = itext ; 1319fileNameList [ count ] = fileName [ count ] . c_str () ; 1320++ count ; 1321} 1322} ; 1323 1324// Writes a string into a depfile, escaping some special characters following the Makefile rules. 1325static void writeEscapedDepString ( std :: ofstream & file , const std :: string & str ) 1326{ 1327for ( char c : str ) { 1328switch ( c ) { 1329case ' ' : 1330case ':' : 1331case '#' : 1332case '[' : 1333case ']' : 1334case ' \\ ' : 1335file << ' \\ ' ; 1336break ; 1337case '$' : 1338file << '$' ; 1339break ; 1340} 1341file << c ; 1342} 1343} 1344 1345// Writes a depfile similar to gcc -MMD foo.c 1346bool writeDepFile ( std :: string depfile , std :: vector < std :: string >& binaryFiles , const std :: vector < std :: string >& sources ) 1347{ 1348std :: ofstream file ( depfile ) ; 1349if ( file . fail ()) 1350return false ; 1351 1352for ( auto binaryFile = binaryFiles . begin () ; binaryFile != binaryFiles . end () ; binaryFile ++ ) { 1353writeEscapedDepString ( file , * binaryFile ) ; 1354file << ":" ; 1355for ( auto sourceFile = sources . begin () ; sourceFile != sources . end () ; sourceFile ++ ) { 1356file << " " ; 1357writeEscapedDepString ( file , * sourceFile ) ; 1358} 1359file << std :: endl ; 1360} 1361return true ; 1362} 1363 1364// 1365// For linking mode: Will independently parse each compilation unit, but then put them 1366// in the same program and link them together, making at most one linked module per 1367// pipeline stage. 1368// 1369// Uses the new C++ interface instead of the old handle-based interface. 1370// 1371 1372void CompileAndLinkShaderUnits ( std :: vector < ShaderCompUnit > compUnits ) 1373{ 1374// keep track of what to free 1375std :: list < glslang :: TShader *> shaders ; 1376 1377EShMessages messages = EShMsgDefault ; 1378SetMessageOptions ( messages ) ; 1379 1380DirStackFileIncluder includer ; 1381std :: for_each ( IncludeDirectoryList . begin () , IncludeDirectoryList . end () , [ & includer ]( const std :: string & dir ) { 1382includer . pushExternalDirectory ( dir ) ; }) ; 1383 1384std :: vector < std :: string > sources ; 1385 1386// 1387// Per-shader processing... 1388// 1389 1390glslang :: TProgram & program = * new glslang :: TProgram ; 1391const bool compileOnly = ( Options & EOptionCompileOnly ) != 0 ; 1392for ( auto it = compUnits . cbegin () ; it != compUnits . cend () ; ++ it ) { 1393const auto & compUnit = * it ; 1394for ( int i = 0 ; i < compUnit . count ; i ++ ) { 1395sources . push_back ( compUnit . fileNameList [ i ]) ; 1396} 1397glslang :: TShader * shader = new glslang :: TShader ( compUnit . stage ) ; 1398shader -> setStringsWithLengthsAndNames ( compUnit . text , nullptr , compUnit . fileNameList , compUnit . count ) ; 1399if ( entryPointName ) 1400shader -> setEntryPoint ( entryPointName ) ; 1401if ( sourceEntryPointName ) { 1402if ( entryPointName == nullptr ) 1403printf ( "Warning: Changing source entry point name without setting an entry-point name. \n " 1404"Use '-e <name>'. \n " ) ; 1405shader -> setSourceEntryPoint ( sourceEntryPointName ) ; 1406} 1407 1408if ( compileOnly ) 1409shader -> setCompileOnly () ; 1410 1411shader -> setOverrideVersion ( GlslVersion ) ; 1412 1413std :: string intrinsicString = getIntrinsic ( compUnit . text , compUnit . count ) ; 1414 1415PreambleString = "" ; 1416if ( UserPreamble . isSet ()) 1417PreambleString . append ( UserPreamble . get ()) ; 1418 1419if ( ! intrinsicString . empty ()) 1420PreambleString . append ( intrinsicString ) ; 1421 1422shader -> setPreamble ( PreambleString . c_str ()) ; 1423shader -> addProcesses ( Processes ) ; 1424 1425// Set IO mapper binding shift values 1426for ( int r = 0 ; r < glslang :: EResCount ; ++ r ) { 1427const glslang :: TResourceType res = glslang :: TResourceType ( r ) ; 1428 1429// Set base bindings 1430shader -> setShiftBinding ( res , baseBinding [ res ][ compUnit . stage ]) ; 1431 1432// Set bindings for particular resource sets 1433// TODO: use a range based for loop here, when available in all environments. 1434for ( auto i = baseBindingForSet [ res ][ compUnit . stage ] . begin () ; 1435i != baseBindingForSet [ res ][ compUnit . stage ] . end () ; ++ i ) 1436shader -> setShiftBindingForSet ( res , i -> second , i -> first ) ; 1437} 1438shader -> setNoStorageFormat (( Options & EOptionNoStorageFormat ) != 0 ) ; 1439shader -> setResourceSetBinding ( baseResourceSetBinding [ compUnit . stage ]) ; 1440 1441if ( autoSampledTextures ) 1442shader -> setTextureSamplerTransformMode ( EShTexSampTransUpgradeTextureRemoveSampler ) ; 1443 1444if ( Options & EOptionAutoMapBindings ) 1445shader -> setAutoMapBindings ( true ) ; 1446 1447if ( Options & EOptionAutoMapLocations ) 1448shader -> setAutoMapLocations ( true ) ; 1449 1450for ( auto & uniOverride : uniformLocationOverrides ) { 1451shader -> addUniformLocationOverride ( uniOverride . first . c_str () , 1452uniOverride . second ) ; 1453} 1454 1455shader -> setUniformLocationBase ( uniformBase ) ; 1456 1457if ( VulkanRulesRelaxed ) { 1458for ( auto & storageOverride : blockStorageOverrides ) { 1459shader -> addBlockStorageOverride ( storageOverride . first . c_str () , 1460storageOverride . second ) ; 1461} 1462 1463if ( setGlobalBufferBlock ) { 1464shader -> setAtomicCounterBlockName ( atomicCounterBlockName . c_str ()) ; 1465shader -> setAtomicCounterBlockSet ( atomicCounterBlockSet ) ; 1466} 1467 1468if ( setGlobalUniformBlock ) { 1469shader -> setGlobalUniformBlockName ( globalUniformName . c_str ()) ; 1470shader -> setGlobalUniformSet ( globalUniformSet ) ; 1471shader -> setGlobalUniformBinding ( globalUniformBinding ) ; 1472} 1473} 1474 1475shader -> setNanMinMaxClamp ( NaNClamp ) ; 1476 1477#ifdef ENABLE_HLSL 1478shader -> setFlattenUniformArrays (( Options & EOptionFlattenUniformArrays ) != 0 ) ; 1479if ( Options & EOptionHlslIoMapping ) 1480shader -> setHlslIoMapping ( true ) ; 1481#endif 1482 1483if ( Options & EOptionInvertY ) 1484shader -> setInvertY ( true ) ; 1485 1486if ( HlslDxPositionW ) 1487shader -> setDxPositionW ( true ) ; 1488 1489if ( EnhancedMsgs ) 1490shader -> setEnhancedMsgs () ; 1491 1492if ( emitNonSemanticShaderDebugInfo ) 1493shader -> setDebugInfo ( true ) ; 1494 1495if ( Options & EOptionBindingsPerResourceType ) 1496shader -> setBindingsPerResourceType () ; 1497 1498if ( Options & EOptionDiscardIsTerminate ) 1499shader -> setDiscardIsTerminate ( true ) ; 1500 1501// Set up the environment, some subsettings take precedence over earlier 1502// ways of setting things. 1503if ( Options & EOptionSpv ) { 1504shader -> setEnvInput (( Options & EOptionReadHlsl ) ? glslang :: EShSourceHlsl 1505: glslang :: EShSourceGlsl , 1506compUnit . stage , Client , ClientInputSemanticsVersion ) ; 1507shader -> setEnvClient ( Client , ClientVersion ) ; 1508shader -> setEnvTarget ( TargetLanguage , TargetVersion ) ; 1509#ifdef ENABLE_HLSL 1510if ( targetHlslFunctionality1 ) 1511shader -> setEnvTargetHlslFunctionality1 () ; 1512#endif 1513if ( VulkanRulesRelaxed ) 1514shader -> setEnvInputVulkanRulesRelaxed () ; 1515} 1516 1517shaders . push_back ( shader ) ; 1518 1519const int defaultVersion = Options & EOptionDefaultDesktop ? 110 : 100 ; 1520 1521if ( Options & EOptionOutputPreprocessed ) { 1522std :: string str ; 1523if ( shader -> preprocess ( GetResources () , defaultVersion , ENoProfile , false , false , messages , & str , includer )) { 1524PutsIfNonEmpty ( str . c_str ()) ; 1525} else { 1526CompileFailed = 1 ; 1527} 1528StderrIfNonEmpty ( shader -> getInfoLog ()) ; 1529StderrIfNonEmpty ( shader -> getInfoDebugLog ()) ; 1530continue ; 1531} 1532 1533if ( ! shader -> parse ( GetResources () , defaultVersion , false , messages , includer )) 1534CompileFailed = 1 ; 1535 1536if ( ! compileOnly ) 1537program . addShader ( shader ) ; 1538 1539if ( ! ( Options & EOptionSuppressInfolog ) && 1540! ( Options & EOptionMemoryLeakMode )) { 1541if ( ! beQuiet ) 1542StderrIfNonEmpty ( compUnit . fileName [ 0 ] . c_str ()) ; 1543StderrIfNonEmpty ( shader -> getInfoLog ()) ; 1544StderrIfNonEmpty ( shader -> getInfoDebugLog ()) ; 1545} 1546} 1547 1548// 1549// Program-level processing... 1550// 1551 1552if ( ! compileOnly ) { 1553// Link 1554if ( ! ( Options & EOptionOutputPreprocessed ) && ! program . link ( messages )) 1555LinkFailed = true ; 1556 1557// Map IO 1558if ( Options & EOptionSpv ) { 1559if ( ! program . mapIO ()) 1560LinkFailed = true ; 1561} 1562 1563// Report 1564if ( ! ( Options & EOptionSuppressInfolog ) && ! ( Options & EOptionMemoryLeakMode )) { 1565StderrIfNonEmpty ( program . getInfoLog ()) ; 1566StderrIfNonEmpty ( program . getInfoDebugLog ()) ; 1567} 1568 1569// Reflect 1570if ( Options & EOptionDumpReflection ) { 1571program . buildReflection ( ReflectOptions ) ; 1572program . dumpReflection () ; 1573} 1574} 1575 1576std :: vector < std :: string > outputFiles ; 1577 1578// Dump SPIR-V 1579if ( Options & EOptionSpv ) { 1580#ifdef ENABLE_SPIRV 1581CompileOrLinkFailed . fetch_or ( CompileFailed ) ; 1582CompileOrLinkFailed . fetch_or ( LinkFailed ) ; 1583if ( static_cast < bool > ( CompileOrLinkFailed . load ())) 1584printf ( "SPIR-V is not generated for failed compile or link \n " ) ; 1585else { 1586std :: vector < glslang :: TIntermediate *> intermediates ; 1587if ( ! compileOnly ) { 1588for ( int stage = 0 ; stage < EShLangCount ; ++ stage ) { 1589if ( auto * i = program . getIntermediate (( EShLanguage ) stage )) { 1590intermediates . emplace_back ( i ) ; 1591} 1592} 1593} else { 1594for ( const auto * shader : shaders ) { 1595if ( auto * i = shader -> getIntermediate ()) { 1596intermediates . emplace_back ( i ) ; 1597} 1598} 1599} 1600for ( auto * intermediate : intermediates ) { 1601std :: vector < unsigned int > spirv ; 1602spv :: SpvBuildLogger logger ; 1603glslang :: SpvOptions spvOptions ; 1604if ( Options & EOptionDebug ) { 1605spvOptions . generateDebugInfo = true ; 1606if ( emitNonSemanticShaderDebugInfo ) { 1607spvOptions . emitNonSemanticShaderDebugInfo = true ; 1608if ( emitNonSemanticShaderDebugSource ) { 1609spvOptions . emitNonSemanticShaderDebugSource = true ; 1610} 1611} 1612} else if ( stripDebugInfo ) 1613spvOptions . stripDebugInfo = true ; 1614spvOptions . disableOptimizer = ( Options & EOptionOptimizeDisable ) != 0 ; 1615spvOptions . optimizeSize = ( Options & EOptionOptimizeSize ) != 0 ; 1616spvOptions . optimizePerformance = ( Options & EOptionOptimizePerformance ) != 0 ; 1617spvOptions . disassemble = SpvToolsDisassembler ; 1618spvOptions . validate = SpvToolsValidate ; 1619spvOptions . compileOnly = compileOnly ; 1620spvOptions . compilerSignature = "glslang" ; 1621spvOptions . commandLineArguments = CommandLineArguments . c_str () ; 1622std :: error_code currentPathError ; 1623const std :: string currentWorkingDirectory = std :: filesystem :: current_path ( currentPathError ) . string () ; 1624if ( ! currentPathError ) 1625spvOptions . currentWorkingDirectory = currentWorkingDirectory . c_str () ; 1626glslang :: GlslangToSpv ( * intermediate , spirv , & logger , & spvOptions ) ; 1627 1628// Dump the spv to a file or stdout, etc., but only if not doing 1629// memory/perf testing, as it's not internal to programmatic use. 1630if ( ! ( Options & EOptionMemoryLeakMode )) { 1631printf ( "%s" , logger . getAllMessages () . c_str ()) ; 1632const auto filename = GetBinaryName ( intermediate -> getStage ()) ; 1633if ( Options & EOptionOutputHexadecimal ) { 1634if ( ! glslang :: OutputSpvHex ( spirv , filename , variableName )) 1635exit ( EFailUsage ) ; 1636} else { 1637if ( ! glslang :: OutputSpvBin ( spirv , filename )) 1638exit ( EFailUsage ) ; 1639} 1640 1641outputFiles . push_back ( filename ) ; 1642if ( ! SpvToolsDisassembler && ( Options & EOptionHumanReadableSpv )) 1643spv :: Disassemble ( std :: cout , spirv ) ; 1644} 1645} 1646} 1647#else 1648Error ( "This configuration of glslang does not have SPIR-V support" ) ; 1649#endif 1650} 1651 1652CompileOrLinkFailed . fetch_or ( CompileFailed ) ; 1653CompileOrLinkFailed . fetch_or ( LinkFailed ) ; 1654if ( depencyFileName && ! static_cast < bool > ( CompileOrLinkFailed . load ())) { 1655std :: set < std :: string > includedFiles = includer . getIncludedFiles () ; 1656sources . insert ( sources . end () , includedFiles . begin () , includedFiles . end ()) ; 1657 1658writeDepFile ( depencyFileName , outputFiles , sources ) ; 1659} 1660 1661// Free everything up, program has to go before the shaders 1662// because it might have merged stuff from the shaders, and 1663// the stuff from the shaders has to have its destructors called 1664// before the pools holding the memory in the shaders is freed. 1665delete & program ; 1666while ( shaders . size () > 0 ) { 1667delete shaders . back () ; 1668shaders . pop_back () ; 1669} 1670} 1671 1672// 1673// Do file IO part of compile and link, handing off the pure 1674// API/programmatic mode to CompileAndLinkShaderUnits(), which can 1675// be put in a loop for testing memory footprint and performance. 1676// 1677// This is just for linking mode: meaning all the shaders will be put into the 1678// the same program linked together. 1679// 1680// This means there are a limited number of work items (not multi-threading mode) 1681// and that the point is testing at the linking level. Hence, to enable 1682// performance and memory testing, the actual compile/link can be put in 1683// a loop, independent of processing the work items and file IO. 1684// 1685void CompileAndLinkShaderFiles ( glslang :: TWorklist & Worklist ) 1686{ 1687std :: vector < ShaderCompUnit > compUnits ; 1688 1689// If this is using stdin, we can't really detect multiple different file 1690// units by input type. We need to assume that we're just being given one 1691// file of a certain type. 1692if (( Options & EOptionStdin ) != 0 ) { 1693ShaderCompUnit compUnit ( FindLanguage ( "stdin" )) ; 1694std :: istreambuf_iterator < char > begin ( std :: cin ) , end ; 1695std :: string tempString ( begin , end ) ; 1696char * fileText = strdup ( tempString . c_str ()) ; 1697std :: string fileName = "stdin" ; 1698compUnit . addString ( fileName , fileText ) ; 1699compUnits . push_back ( compUnit ) ; 1700} else { 1701// Transfer all the work items from to a simple list of 1702// of compilation units. (We don't care about the thread 1703// work-item distribution properties in this path, which 1704// is okay due to the limited number of shaders, know since 1705// they are all getting linked together.) 1706glslang :: TWorkItem * workItem ; 1707while ( Worklist . remove ( workItem )) { 1708ShaderCompUnit compUnit ( FindLanguage ( workItem -> name )) ; 1709char * fileText = ReadFileData ( workItem -> name . c_str ()) ; 1710if ( fileText == nullptr ) 1711usage () ; 1712compUnit . addString ( workItem -> name , fileText ) ; 1713compUnits . push_back ( compUnit ) ; 1714} 1715} 1716 1717// Actual call to programmatic processing of compile and link, 1718// in a loop for testing memory and performance. This part contains 1719// all the perf/memory that a programmatic consumer will care about. 1720for ( int i = 0 ; i < (( Options & EOptionMemoryLeakMode ) ? 100 : 1 ) ; ++ i ) { 1721for ( int j = 0 ; j < (( Options & EOptionMemoryLeakMode ) ? 100 : 1 ) ; ++ j ) 1722CompileAndLinkShaderUnits ( compUnits ) ; 1723 1724if ( Options & EOptionMemoryLeakMode ) 1725glslang :: OS_DumpMemoryCounters () ; 1726} 1727 1728// free memory from ReadFileData, which got stored in a const char* 1729// as the first string above 1730for ( auto it = compUnits . begin () ; it != compUnits . end () ; ++ it ) 1731FreeFileData ( const_cast < char *> ( it -> text [ 0 ])) ; 1732} 1733 1734int singleMain () 1735{ 1736glslang :: TWorklist workList ; 1737std :: for_each ( WorkItems . begin () , WorkItems . end () , [ & workList ]( std :: unique_ptr < glslang :: TWorkItem >& item ) { 1738assert ( item ) ; 1739workList . add ( item . get ()) ; 1740}) ; 1741 1742if ( Options & EOptionDumpConfig ) { 1743printf ( "%s" , GetDefaultTBuiltInResourceString () . c_str ()) ; 1744if ( workList . empty ()) 1745return ESuccess ; 1746} 1747 1748if ( Options & EOptionDumpBareVersion ) { 1749int spirvGeneratorVersion = 0 ; 1750#ifdef ENABLE_SPIRV 1751spirvGeneratorVersion = glslang :: GetSpirvGeneratorVersion () ; 1752#endif 1753printf ( "%d:%d.%d.%d%s \n " , spirvGeneratorVersion , GLSLANG_VERSION_MAJOR , GLSLANG_VERSION_MINOR , 1754GLSLANG_VERSION_PATCH , GLSLANG_VERSION_FLAVOR ) ; 1755if ( workList . empty ()) 1756return ESuccess ; 1757} else if ( Options & EOptionDumpVersions ) { 1758int spirvGeneratorVersion = 0 ; 1759#ifdef ENABLE_SPIRV 1760spirvGeneratorVersion = glslang :: GetSpirvGeneratorVersion () ; 1761#endif 1762printf ( "Glslang Version: %d:%d.%d.%d%s \n " , spirvGeneratorVersion , GLSLANG_VERSION_MAJOR , 1763GLSLANG_VERSION_MINOR , GLSLANG_VERSION_PATCH , GLSLANG_VERSION_FLAVOR ) ; 1764printf ( "ESSL Version: %s \n " , glslang :: GetEsslVersionString ()) ; 1765printf ( "GLSL Version: %s \n " , glslang :: GetGlslVersionString ()) ; 1766std :: string spirvVersion ; 1767#if ENABLE_SPIRV 1768glslang :: GetSpirvVersion ( spirvVersion ) ; 1769#endif 1770printf ( "SPIR-V Version %s \n " , spirvVersion . c_str ()) ; 1771printf ( "GLSL.std.450 Version %d, Revision %d \n " , GLSLstd450Version , GLSLstd450Revision ) ; 1772printf ( "Khronos Tool ID %d \n " , glslang :: GetKhronosToolId ()) ; 1773printf ( "SPIR-V Generator Version %d \n " , spirvGeneratorVersion ) ; 1774printf ( "GL_KHR_vulkan_glsl version %d \n " , 100 ) ; 1775printf ( "ARB_GL_gl_spirv version %d \n " , 100 ) ; 1776if ( workList . empty ()) 1777return ESuccess ; 1778} 1779 1780if ( workList . empty () && (( Options & EOptionStdin ) == 0 )) { 1781usage () ; 1782} 1783 1784if ( Options & EOptionStdin ) { 1785WorkItems . push_back ( std :: unique_ptr < glslang :: TWorkItem > { new glslang :: TWorkItem ( "stdin" )}) ; 1786workList . add ( WorkItems . back () . get ()) ; 1787} 1788 1789ProcessConfigFile () ; 1790 1791if (( Options & EOptionReadHlsl ) && ! (( Options & EOptionOutputPreprocessed ) || ( Options & EOptionSpv ))) 1792Error ( "HLSL requires SPIR-V code generation (or preprocessing only)" ) ; 1793 1794// 1795// Two modes: 1796// 1) linking all arguments together, single-threaded, new C++ interface 1797// 2) independent arguments, can be tackled by multiple asynchronous threads, for testing thread safety, using the old handle interface 1798// 1799if ( Options & ( EOptionLinkProgram | EOptionOutputPreprocessed )) { 1800glslang :: InitializeProcess () ; 1801glslang :: InitializeProcess () ; // also test reference counting of users 1802glslang :: InitializeProcess () ; // also test reference counting of users 1803glslang :: FinalizeProcess () ; // also test reference counting of users 1804glslang :: FinalizeProcess () ; // also test reference counting of users 1805CompileAndLinkShaderFiles ( workList ) ; 1806glslang :: FinalizeProcess () ; 1807} else { 1808ShInitialize () ; 1809ShInitialize () ; // also test reference counting of users 1810ShFinalize () ; // also test reference counting of users 1811 1812bool printShaderNames = workList . size () > 1 ; 1813 1814if ( Options & EOptionMultiThreaded ) { 1815std :: array < std :: thread , 16 > threads ; 1816for ( unsigned int t = 0 ; t < threads . size () ; ++ t ) { 1817threads [ t ] = std :: thread ( CompileShaders , std :: ref ( workList )) ; 1818if ( threads [ t ] . get_id () == std :: thread :: id ()) { 1819fprintf ( stderr , "Failed to create thread \n " ) ; 1820return EFailThreadCreate ; 1821} 1822} 1823 1824std :: for_each ( threads . begin () , threads . end () , []( std :: thread & t ) { t . join () ; }) ; 1825} else 1826CompileShaders ( workList ) ; 1827 1828// Print out all the resulting infologs 1829for ( size_t w = 0 ; w < WorkItems . size () ; ++ w ) { 1830if ( WorkItems [ w ]) { 1831if ( printShaderNames || WorkItems [ w ] -> results . size () > 0 ) 1832PutsIfNonEmpty ( WorkItems [ w ] -> name . c_str ()) ; 1833PutsIfNonEmpty ( WorkItems [ w ] -> results . c_str ()) ; 1834} 1835} 1836 1837ShFinalize () ; 1838} 1839 1840if ( CompileFailed . load ()) 1841return EFailCompile ; 1842if ( LinkFailed . load ()) 1843return EFailLink ; 1844 1845return 0 ; 1846} 1847 1848int C_DECL main ( int argc , char * argv []) 1849{ 1850ProcessArguments ( WorkItems , argc , argv ) ; 1851 1852int ret = 0 ; 1853 1854// Loop over the entire init/finalize cycle to watch memory changes 1855const int iterations = 1 ; 1856if ( iterations > 1 ) 1857glslang :: OS_DumpMemoryCounters () ; 1858for ( int i = 0 ; i < iterations ; ++ i ) { 1859ret = singleMain () ; 1860if ( iterations > 1 ) 1861glslang :: OS_DumpMemoryCounters () ; 1862} 1863 1864return ret ; 1865} 1866 1867// 1868// Deduce the language from the filename. Files must end in one of the 1869// following extensions: 1870// 1871// .vert = vertex 1872// .tesc = tessellation control 1873// .tese = tessellation evaluation 1874// .geom = geometry 1875// .frag = fragment 1876// .comp = compute 1877// .rgen = ray generation 1878// .rint = ray intersection 1879// .rahit = ray any hit 1880// .rchit = ray closest hit 1881// .rmiss = ray miss 1882// .rcall = ray callable 1883// .mesh = mesh 1884// .task = task 1885// Additionally, the file names may end in .<stage>.glsl and .<stage>.hlsl 1886// where <stage> is one of the stages listed above. 1887// 1888EShLanguage FindLanguage ( const std :: string & name , bool parseStageName ) 1889{ 1890std :: string stageName ; 1891if ( shaderStageName ) 1892stageName = shaderStageName ; 1893else if ( parseStageName ) { 1894// Note: "first" extension means "first from the end", i.e. 1895// if the file is named foo.vert.glsl, then "glsl" is first, 1896// "vert" is second. 1897size_t firstExtStart = name . find_last_of ( "." ) ; 1898bool hasFirstExt = firstExtStart != std :: string :: npos ; 1899size_t secondExtStart = hasFirstExt ? name . find_last_of ( "." , firstExtStart - 1 ) : std :: string :: npos ; 1900bool hasSecondExt = secondExtStart != std :: string :: npos ; 1901std :: string firstExt = name . substr ( firstExtStart + 1 , std :: string :: npos ) ; 1902bool usesUnifiedExt = hasFirstExt && ( firstExt == "glsl" || firstExt == "hlsl" ) ; 1903if ( usesUnifiedExt && firstExt == "hlsl" ) 1904Options |= EOptionReadHlsl ; 1905if ( hasFirstExt && ! usesUnifiedExt ) 1906stageName = firstExt ; 1907else if ( usesUnifiedExt && hasSecondExt ) 1908stageName = name . substr ( secondExtStart + 1 , firstExtStart - secondExtStart - 1 ) ; 1909else { 1910usage () ; 1911return EShLangVertex ; 1912} 1913} else 1914stageName = name ; 1915 1916if ( stageName == "vert" ) 1917return EShLangVertex ; 1918else if ( stageName == "tesc" ) 1919return EShLangTessControl ; 1920else if ( stageName == "tese" ) 1921return EShLangTessEvaluation ; 1922else if ( stageName == "geom" ) 1923return EShLangGeometry ; 1924else if ( stageName == "frag" ) 1925return EShLangFragment ; 1926else if ( stageName == "comp" ) 1927return EShLangCompute ; 1928else if ( stageName == "rgen" ) 1929return EShLangRayGen ; 1930else if ( stageName == "rint" ) 1931return EShLangIntersect ; 1932else if ( stageName == "rahit" ) 1933return EShLangAnyHit ; 1934else if ( stageName == "rchit" ) 1935return EShLangClosestHit ; 1936else if ( stageName == "rmiss" ) 1937return EShLangMiss ; 1938else if ( stageName == "rcall" ) 1939return EShLangCallable ; 1940else if ( stageName == "mesh" ) 1941return EShLangMesh ; 1942else if ( stageName == "task" ) 1943return EShLangTask ; 1944 1945usage () ; 1946return EShLangVertex ; 1947} 1948 1949// 1950// Read a file's data into a string, and compile it using the old interface ShCompile, 1951// for non-linkable results. 1952// 1953void CompileFile ( const char * fileName , ShHandle compiler ) 1954{ 1955int ret = 0 ; 1956char * shaderString ; 1957if (( Options & EOptionStdin ) != 0 ) { 1958std :: istreambuf_iterator < char > begin ( std :: cin ) , end ; 1959std :: string tempString ( begin , end ) ; 1960shaderString = strdup ( tempString . c_str ()) ; 1961} else { 1962shaderString = ReadFileData ( fileName ) ; 1963} 1964 1965// move to length-based strings, rather than null-terminated strings 1966int * lengths = new int [ 1 ] ; 1967lengths [ 0 ] = ( int ) strlen ( shaderString ) ; 1968 1969EShMessages messages = EShMsgDefault ; 1970SetMessageOptions ( messages ) ; 1971 1972if ( UserPreamble . isSet ()) 1973Error ( "-D, -U and -P options require -l (linking) \n " ) ; 1974 1975for ( int i = 0 ; i < (( Options & EOptionMemoryLeakMode ) ? 100 : 1 ) ; ++ i ) { 1976for ( int j = 0 ; j < (( Options & EOptionMemoryLeakMode ) ? 100 : 1 ) ; ++ j ) { 1977// ret = ShCompile(compiler, shaderStrings, NumShaderStrings, lengths, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); 1978ret = ShCompile ( compiler , & shaderString , 1 , nullptr , EShOptNone , GetResources () , 0 , 1979( Options & EOptionDefaultDesktop ) ? 110 : 100 , false , messages , fileName ) ; 1980// const char* multi[12] = { "# ve", "rsion", " 300 e", "s", "\n#err", 1981// "or should be l", "ine 1", "string 5\n", "float glo", "bal", 1982// ";\n#error should be line 2\n void main() {", "global = 2.3;}" }; 1983// const char* multi[7] = { "/", "/", "\\", "\n", "\n", "#", "version 300 es" }; 1984// ret = ShCompile(compiler, multi, 7, nullptr, EShOptNone, &Resources, Options, (Options & EOptionDefaultDesktop) ? 110 : 100, false, messages); 1985} 1986 1987if ( Options & EOptionMemoryLeakMode ) 1988glslang :: OS_DumpMemoryCounters () ; 1989} 1990 1991delete [] lengths ; 1992FreeFileData ( shaderString ) ; 1993 1994if ( ret == 0 ) 1995CompileFailed = true ; 1996} 1997 1998// 1999// print usage to stdout 2000// 2001void usage () 2002{ 2003printf ( "Usage: glslang [option]... [file]... \n " 2004" \n " 2005"'file' with one of the following three endings can be auto-classified: \n " 2006"1) .<stage>, where <stage> is one of: \n " 2007" vert for a vertex shader \n " 2008" tesc for a tessellation control shader \n " 2009" tese for a tessellation evaluation shader \n " 2010" geom for a geometry shader \n " 2011" frag for a fragment shader \n " 2012" comp for a compute shader \n " 2013" mesh for a mesh shader \n " 2014" task for a task shader \n " 2015" rgen for a ray generation shader \n " 2016" rint for a ray intersection shader \n " 2017" rahit for a ray any hit shader \n " 2018" rchit for a ray closest hit shader \n " 2019" rmiss for a ray miss shader \n " 2020" rcall for a ray callable shader \n " 2021"2) .<stage>.glsl or .<stage>.hlsl compound suffix, where stage options are \n " 2022" described above \n " 2023"3) .conf, to provide a config file that replaces the default configuration (see \n " 2024" -c option below for generating a template) \n " 2025" \n " 2026"Options: \n " 2027" -C cascading errors; risk crash from accumulation of error recoveries \n " 2028" -D input is HLSL (this is the default when any suffix is .hlsl) \n " 2029" -D<name[=def]> | --D <name[=def]> | --define-macro <name[=def]> \n " 2030" define a pre-processor macro \n " 2031" -E print pre-processed GLSL; cannot be used with -l; errors will \n " 2032" appear on stderr \n " 2033" -G[ver] create SPIR-V binary under OpenGL semantics; turns on -l; default \n " 2034" file name is <stage>.spv (-o overrides this); ver, when present, \n " 2035" is the version of the input semantics which will appear in \n " 2036" '#define GL_SPIRV ver'; --client opengl100 is the same as -G100; a \n " 2037" --target-env for OpenGL will also imply -G; currently only \n " 2038" supports GLSL \n " 2039" -H print human readable form of SPIR-V; turns on -V \n " 2040" -I<dir> add <dir> to the include search path; includer's directory is \n " 2041" searched first, followed by left-to-right order of -I \n " 2042" -O optimizes SPIR-V for performance \n " 2043" -Os optimizes SPIR-V to minimize size \n " 2044" -Od disables optimization; may cause illegal SPIR-V for HLSL \n " 2045" -P<text> | --P <text> | --preamble-text <text> \n " 2046" inject custom preamble text which is treated as if it appeared \n " 2047" immediately after the version declaration (if any) \n " 2048" -R use relaxed verification rules for generating Vulkan SPIR-V, \n " 2049" allowing the use of default uniforms, atomic_uints, and the \n " 2050" gl_VertexID and gl_InstanceID keywords \n " 2051" -S <stage> uses the specified <stage> rather than parsing the file extension; \n " 2052" choices for <stage> include vert, tesc, tese, geom, frag, and \n " 2053" comp; a full list of options is given above \n " 2054" -U<name> | --undef-macro <name> | --U <name> \n " 2055" undefine a pre-processor macro \n " 2056" -V[ver] create SPIR-V binary under Vulkan semantics; turns on -l; default \n " 2057" file name is <stage>.spv (-o overrides this); ver, when present, \n " 2058" is the version of the input semantics which will appear in \n " 2059" '#define VULKAN ver'; --client vulkan100 is the same as -V100; a \n " 2060" '--target-env' for Vulkan will also imply '-V' \n " 2061" -c configuration dump; creates the default configuration file \n " 2062" (redirect to a .conf file) \n " 2063" -d default to desktop (#version 110) when there is no shader #version \n " 2064" (default is ES version 100) \n " 2065" -e <name> | --entry-point <name> \n " 2066" specify <name> as the entry-point function name \n " 2067" -f{hlsl_functionality1} \n " 2068" 'hlsl_functionality1' enables use of the \n " 2069" SPV_GOOGLE_hlsl_functionality1 extension \n " 2070" -g generate debug information \n " 2071" -g0 strip debug information \n " 2072" -gV generate nonsemantic shader debug information \n " 2073" -gVS generate nonsemantic shader debug information with source \n " 2074" -h | --help print this usage message \n " 2075" -i intermediate tree (glslang AST) is printed out \n " 2076" -l link all input files together to form a single module \n " 2077" -m memory leak mode \n " 2078" -o <file> save binary to <file>; requires a binary option (e.g., -V) \n " 2079" -q dump reflection query database; requires -l for linking \n " 2080" -r | --relaxed-errors \n " 2081" relaxed GLSL semantic error-checking mode \n " 2082" -s silence syntax and semantic error reporting \n " 2083" -t multi-threaded mode \n " 2084" -u<name>:<loc> \n " 2085" specify a uniform location override for --aml \n " 2086" -v | --version \n " 2087" print version strings \n " 2088" -w | --suppress-warnings \n " 2089" suppress GLSL warnings, except as required by '#extension : warn' \n " 2090" -x save binary output as text-based 32-bit hexadecimal numbers \n " 2091" --absolute-path prints absolute path for messages \n " 2092" --relax-set-binding-limits allow larger layout(set) and \n " 2093" layout(binding) values \n " 2094" --auto-map-binding | --auto-map-bindings | --amb \n " 2095" automatically bind uniform variables without \n " 2096" explicit bindings \n " 2097" --auto-map-locations | --aml automatically locate input/output lacking \n " 2098" 'location' (fragile, not cross stage) \n " 2099" --auto-sampled-textures removes sampler variables and converts \n " 2100" existing textures to sampled textures \n " 2101" --client {vulkan<ver> | opengl<ver>} \n " 2102" see -V and -G \n " 2103" --depfile <file> writes depfile for build systems \n " 2104" --discard-is-terminate map GLSL discard to OpTerminateInvocation \n " 2105" instead of OpDemoteToHelperInvocation \n " 2106" --dump-builtin-symbols prints builtin symbol table prior each \n " 2107" compile \n " 2108" -dumpfullversion | -dumpversion print bare major.minor.patchlevel \n " 2109" --enhanced-msgs print more readable error messages (GLSL \n " 2110" only) \n " 2111" --error-column display the column of the error along the \n " 2112" line \n " 2113" --flatten-uniform-array | --flatten-uniform-arrays | --fua \n " 2114" flatten uniform texture/sampler arrays to \n " 2115" scalars \n " 2116" --glsl-version {100 | 110 | 120 | 130 | 140 | 150 | \n " 2117" 300es | 310es | 320es | 330 | \n " 2118" 400 | 410 | 420 | 430 | 440 | 450 | 460} \n " 2119" set GLSL version; overrides #version in \n " 2120" shader source \n " 2121" --hlsl-dx-position-w W component of SV_Position in HLSL fragment \n " 2122" shaders compatible with DirectX \n " 2123" --hlsl-dx9-compatible interprets sampler declarations as a \n " 2124" texture/sampler combo like DirectX9 would, \n " 2125" and recognizes DirectX9-specific semantics \n " 2126" --hlsl-enable-16bit-types allow 16-bit types in SPIR-V for HLSL \n " 2127" --hlsl-iomap | --hlsl-iomapper | --hlsl-iomapping \n " 2128" perform IO mapping in HLSL register space \n " 2129" --hlsl-offsets allow block offsets to follow HLSL rules; \n " 2130" works independently of source language \n " 2131" --invert-y | --iy invert position.Y output in vertex shader \n " 2132" --keep-uncalled | --ku don't eliminate uncalled functions \n " 2133" --lto perform link time optimization \n " 2134" --nan-clamp favor non-NaN operand in min, max, and clamp \n " 2135" --no-link only compile shader; do not link (GLSL only) \n " 2136" NOTE: this option will set the export \n " 2137" linkage attribute on all functions \n " 2138" --no-storage-format | --nsf use Unknown image format \n " 2139" --quiet do not print anything to stdout unless \n " 2140" requested by another option \n " 2141" --reflect-all-block-variables reflect all variables in blocks, whether \n " 2142" inactive or active \n " 2143" --reflect-basic-array-suffix arrays of basic types will have trailing [0] \n " 2144" --reflect-intermediate-io reflection includes inputs/outputs of linked \n " 2145" shaders rather than just vertex/fragment \n " 2146" --reflect-separate-buffers reflect buffer variables and blocks \n " 2147" separately to uniforms \n " 2148" --reflect-strict-array-suffix use strict array suffix rules when \n " 2149" reflecting \n " 2150" --reflect-unwrap-io-blocks unwrap input/output blocks the same as \n " 2151" uniform blocks \n " 2152" --resource-set-binding <stage> [name] <set> [binding]... \n " 2153" set descriptor set and binding for \n " 2154" individual resources \n " 2155" --resource-set-binding [stage] <set> \n " 2156" set descriptor set for all resources \n " 2157" --resource-set-bindings | --rsb synonyms for --resource-set-binding \n " 2158" --set-atomic-counter-block <name> <set> \n " 2159" set name and descriptor set for atomic \n " 2160" counter blocks with -R opt \n " 2161" --sacb synonym for set-atomic-counter-block \n " 2162" --set-block-backing name {uniform | buffer | push_constant} \n " 2163" changes the backing type of a uniform, \n " 2164" buffer, or push_constant block declared in \n " 2165" the program when using the -R option; this \n " 2166" can be used to change the backing for \n " 2167" existing blocks as well as implicit ones \n " 2168" such as 'gl_DefaultUniformBlock' \n " 2169" --set-default-uniform-block <name> <set> <binding> \n " 2170" set name, descriptor set, and binding for \n " 2171" global default-uniform-block with -R opt \n " 2172" --sdub synonym for set-default-uniform-block \n " 2173" --shift-image-binding [stage] <num> \n " 2174" base binding number for images (UAV) \n " 2175" --shift-image-binding [stage] <num> <set>... \n " 2176" per-descriptor-set shift values \n " 2177" --shift-image-bindings | --sib synonyms for --shift-image-binding \n " 2178" --shift-sampler-binding [stage] <num> \n " 2179" base binding number for samplers \n " 2180" --shift-sampler-binding [stage] <num> <set>... \n " 2181" per-descriptor-set shift values \n " 2182" --shift-sampler-bindings | --ssb synonyms for --shift-sampler-binding \n " 2183" --shift-ssbo-binding [stage] <num> \n " 2184" base binding number for SSBOs \n " 2185" --shift-ssbo-binding [stage] <num> <set>... \n " 2186" per-descriptor-set shift values \n " 2187" --shift-ssbo-bindings | --sbb synonyms for --shift-ssbo-binding \n " 2188" --shift-texture-binding [stage] <num> \n " 2189" base binding number for textures \n " 2190" --shift-texture-binding [stage] <num> <set>... \n " 2191" per-descriptor-set shift values \n " 2192" --shift-texture-bindings | --stb synonyms for --shift-texture-binding \n " 2193" --shift-uav-binding [stage] <num> base binding number for UAVs \n " 2194" --shift-uav-binding [stage] <num> <set>... \n " 2195" per-descriptor-set shift values \n " 2196" --shift-uav-bindings | --suavb synonyms for --shift-uav-binding \n " 2197" --shift-ubo-binding [stage] <num> base binding number for UBOs \n " 2198" --shift-ubo-binding [stage] <num> <set>... \n " 2199" per-descriptor-set shift values \n " 2200" --shift-ubo-bindings | --sub | \n " 2201" --shift-cbuffer-binding | --shift-cbuffer-bindings | --scb \n " 2202" synonyms for --shift-ubo-binding \n " 2203" --shift-combined-sampler-binding [stage] <num> \n " 2204" base binding number for combined samplers \n " 2205" --shift-combined-sampler-binding [stage] <num> <set>... \n " 2206" per-descriptor-set shift values \n " 2207" --shift-combined-sampler-bindings | --scsb \n " 2208" synonyms for --shift-combined-sampler-binding \n " 2209" --shift-as-binding [stage] <num> \n " 2210" base binding number for acceleration structures \n " 2211" --shift-as-binding [stage] <num> <set>... \n " 2212" per-descriptor-set shift values \n " 2213" --shift-as-bindings | --sab \n " 2214" synonyms for --shift-as-binding \n " 2215" --spirv-dis output standard-form disassembly; works only \n " 2216" when a SPIR-V generation option is also used \n " 2217" --spirv-val execute the SPIRV-Tools validator \n " 2218" --source-entrypoint <name> | --sep <name> \n " 2219" the given shader source function is renamed \n " 2220" to be the <name> given in -e \n " 2221" --stdin read from stdin instead of from a file; \n " 2222" requires providing the shader stage using -S \n " 2223" --target-env {vulkan1.0 | vulkan1.1 | vulkan1.2 | vulkan1.3 | vulkan1.4 | \n " 2224" opengl | spirv1.0 | spirv1.1 | spirv1.2 | spirv1.3 | \n " 2225" spirv1.4 | spirv1.5 | spirv1.6} \n " 2226" set the execution environment the generated \n " 2227" code will be executed in; defaults to: \n " 2228" * vulkan1.0 under --client vulkan<ver> \n " 2229" * opengl under --client opengl<ver> \n " 2230" * spirv1.0 under --target-env vulkan1.0 \n " 2231" * spirv1.3 under --target-env vulkan1.1 \n " 2232" * spirv1.5 under --target-env vulkan1.2 \n " 2233" * spirv1.6 under --target-env vulkan1.3 \n " 2234" * spirv1.6 under --target-env vulkan1.4 \n " 2235" multiple --target-env can be specified \n " 2236" --uniform-base <base> set a base to use for generated uniform \n " 2237" locations \n " 2238" --variable-name <name> | --vn <name> \n " 2239" creates a C header file that contains a \n " 2240" uint32_t array named <name> initialized with \n " 2241" the shader binary code \n " 2242" --validate-io validate cross stage IO \n " 2243" --bindings-per-resource-type \n " ) ; 2244 2245exit ( EFailUsage ) ; 2246} 2247 2248#if ! defined _MSC_VER && ! defined MINGW_HAS_SECURE_API 2249 2250#include <errno.h> 2251 2252int fopen_s ( 2253FILE ** pFile , 2254const char * filename , 2255const char * mode 2256) 2257{ 2258if ( ! pFile || ! filename || ! mode ) { 2259return EINVAL ; 2260} 2261 2262FILE * f = fopen ( filename , mode ) ; 2263if ( ! f ) { 2264if ( errno != 0 ) { 2265return errno ; 2266} else { 2267return ENOENT ; 2268} 2269} 2270* pFile = f ; 2271 2272return 0 ; 2273} 2274 2275#endif 2276 2277// 2278// Malloc a string of sufficient size and read a string into it. 2279// 2280char * ReadFileData ( const char * fileName ) 2281{ 2282FILE * in = nullptr ; 2283int errorCode = fopen_s ( & in , fileName , "r" ) ; 2284if ( errorCode || in == nullptr ) 2285Error ( "unable to open input file" ) ; 2286 2287int count = 0 ; 2288while ( fgetc ( in ) != EOF ) 2289count ++ ; 2290 2291fseek ( in , 0 , SEEK_SET ) ; 2292 2293if ( count > 3 ) { 2294unsigned char head [ 3 ] ; 2295if ( fread ( head , 1 , 3 , in ) == 3 ) { 2296if ( head [ 0 ] == 0xef && head [ 1 ] == 0xbb && head [ 2 ] == 0xbf ) { 2297// skip BOM 2298count -= 3 ; 2299} else { 2300fseek ( in , 0 , SEEK_SET ) ; 2301} 2302} else { 2303Error ( "can't read input file" ) ; 2304} 2305} 2306 2307char * return_data = ( char * ) malloc ( count + 1 ) ; // freed in FreeFileData() 2308if (( int ) fread ( return_data , 1 , count , in ) != count ) { 2309free ( return_data ) ; 2310Error ( "can't read input file" ) ; 2311} 2312 2313return_data [ count ] = ' \0 ' ; 2314fclose ( in ) ; 2315 2316return return_data ; 2317} 2318 2319void FreeFileData ( char * data ) 2320{ 2321free ( data ) ; 2322} 2323 2324void InfoLogMsg ( const char * msg , const char * name , const int num ) 2325{ 2326if ( num >= 0 ) 2327printf ( "#### %s %s %d INFO LOG #### \n " , msg , name , num ) ; 2328else 2329printf ( "#### %s %s INFO LOG #### \n " , msg , name ) ; 2330}