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