1Name 2 3 ARB_gl_spirv 4 5Name Strings 6 7 GL_ARB_gl_spirv 8 9Contributors 10 11 John Kessenich, Google 12 Daniel Koch, NVIDIA 13 Christophe Riccio, Unity 14 Graham Sellers, AMD 15 Alejandro Piñeiro, Igalia 16 17Contact Point for Bug Reports 18 19 https://github.com/KhronosGroup/OpenGL-Registry/issues/ 20 21Notice 22 23 Copyright (c) 2015-2019 The Khronos Group Inc. Copyright terms at 24 http://www.khronos.org/registry/speccopyright.html 25 26Specification Update Policy 27 28 Khronos-approved extension specifications are updated in response to 29 issues and bugs prioritized by the Khronos OpenGL Working Group. For 30 extensions which have been promoted to a core Specification, fixes will 31 first appear in the latest version of that core Specification, and will 32 eventually be backported to the extension document. This policy is 33 described in more detail at 34 https://www.khronos.org/registry/OpenGL/docs/update_policy.php 35 36Status 37 38 Complete. 39 Ratified by the Khronos Board of Promoters on July 22, 2016. 40 41Version 42 43 Last Modified Date: August 19, 2020 44 Revision: 46 45 46Number 47 48 ARB Extension #190 49 50Dependencies 51 52 This extension requires version 3.3 or later of The OpenGL Graphics System. 53 (It is not written for OpenGL ES.) 54 55 This extension is written against the following specifications: 56 - The GL_KHR_vulkan_glsl extension, Version 30, April 12, 2016. 57 - The OpenGL Graphics System, Version 4.5, Core Profile, May 28, 2015. 58 - The OpenGL Shading Language, Version 4.50, Revision 6, April 14, 2016. 59 - SPIR-V Specification, Version 1.00, Revision 5 60 61 This extension interacts with ARB_parallel_shader_compile. 62 63 This extension interacts with ARB_separate_shader_objects. 64 65 This extension interacts with ARB_program_interface_query. 66 67Overview 68 69 This is version 100 of the GL_ARB_gl_spirv extension. 70 71 This extension does two things: 72 73 1. Allows a SPIR-V module to be specified as containing a programmable 74 shader stage, rather than using GLSL, whatever the source language 75 was used to create the SPIR-V module. 76 77 2. Modifies GLSL to be a source language for creating SPIR-V modules 78 for OpenGL consumption. Such GLSL can be used to create such SPIR-V 79 modules, outside of the OpenGL runtime. 80 81 Enabling GLSL SPIR-V Features 82 ----------------------------- 83 84 This extension is not enabled with a #extension as other extensions are. 85 It is also not enabled through use of a profile or #version. The intended 86 level of GLSL features comes from the traditional use of #version, profile, 87 and #extension. 88 89 Instead, use of this extension is an effect of using a GLSL front-end in a 90 mode that has it generate SPIR-V for OpenGL. Such tool use is outside the 91 scope of using the OpenGL API and outside the definition of GLSL and this 92 extension. See the documentation of the compiler to see how to request 93 generation of SPIR-V for OpenGL. 94 95 When a front-end is used to accept the GLSL features in this extension, it 96 must error check and reject shaders not adhering to this specification, and 97 accept those that do. Implementation-dependent maximums and capabilities 98 are supplied to, or part of, the front-end, so it can do error checking 99 against them. 100 101 A shader can query the level of GLSL SPIR-V support available, using the 102 predefined 103 104 #define GL_SPIRV 100 105 106 This allows shader code to say, for example, 107 108 #ifdef GL_SPIRV 109 layout(constant_id = 11) const int count = 4; 110 #if GL_SPIRV > 100 111 ... 112 #endif 113 #else 114 const int count = 6; 115 #endif 116 117 SPIR-V Modules 118 -------------- 119 120 An entire stage is held in a single SPIR-V module. The SPIR-V model is 121 that multiple (e.g., GLSL) compilation units forming a single stage 122 in a high-level language are all compiled and linked together to form a 123 single entry point (and its call tree) in a SPIR-V module. This would be 124 done prior to using the OpenGL API to create a program object containing the 125 stage. 126 127 The OpenGL API expects the SPIR-V module to have already been validated, 128 and can return an error if it discovers anything invalid in 129 the module. An invalid SPIR-V module is allowed to result in undefined 130 behavior. 131 132 Specialization Constants 133 ------------------------ 134 135 SPIR-V specialization constants, which can be set later by the client API, 136 can be declared using "layout(constant_id=...)". For example, to make a 137 specialization constant with a default value of 12: 138 139 layout(constant_id = 17) const int arraySize = 12; 140 141 Above, "17" is the ID by which the API or other tools can later refer to 142 this specific specialization constant. The API or an intermediate tool can 143 then change its value to another constant integer before it is fully 144 lowered to executable code. If it is never changed before final lowering, 145 it will retain the value of 12. 146 147 Specialization constants have const semantics, except they don't fold. 148 Hence, an array can be declared with 'arraySize' from above: 149 150 vec4 data[arraySize]; // legal, even though arraySize might change 151 152 Specialization constants can be in expressions: 153 154 vec4 data2[arraySize + 2]; 155 156 This will make data2 be sized by 2 more than whatever constant value 157 'arraySize' has when it is time to lower the shader to executable code. 158 159 An expression formed with specialization constants also behaves in the 160 shader like a specialization constant, not a like a constant. 161 162 arraySize + 2 // a specialization constant (with no constant_id) 163 164 Such expressions can be used in the same places as a constant. 165 166 The constant_id can only be applied to a scalar *int*, a scalar *float* 167 or a scalar *bool*. 168 169 Only basic operators and constructors can be applied to a specialization 170 constant and still result in a specialization constant: 171 172 layout(constant_id = 17) const int arraySize = 12; 173 sin(float(arraySize)); // result is not a specialization constant 174 175 While SPIR-V specialization constants are only for scalars, a vector 176 can be made by operations on scalars: 177 178 layout(constant_id = 18) const int scX = 1; 179 layout(constant_id = 19) const int scZ = 1; 180 const vec3 scVec = vec3(scX, 1, scZ); // partially specialized vector 181 182 A built-in variable can have a 'constant_id' attached to it: 183 184 layout(constant_id = 18) gl_MaxImageUnits; 185 186 This makes it behave as a specialization constant. It is not a full 187 redeclaration; all other characteristics are left intact from the 188 original built-in declaration. 189 190 The built-in vector gl_WorkGroupSize can be specialized using special 191 layout local_size_{xyz}_id's applied to the "in" qualifier. For example: 192 193 layout(local_size_x_id = 18, local_size_z_id = 19) in; 194 195 This leaves gl_WorkGroupSize.y as a non-specialization constant, with 196 gl_WorkGroupSize being a partially specialized vector. Its x and z 197 components can be later specialized using the ID's 18 and 19. 198 199 gl_FragColor 200 ------------ 201 202 The fragment-stage built-in gl_FragColor, which implies a broadcast to all 203 outputs, is not present in SPIR-V. Shaders where writing to gl_FragColor 204 is allowed can still write to it, but it only means to write to an output: 205 - of the same type as gl_FragColor 206 - decorated with location 0 207 - not decorated as a built-in variable. 208 There is no implicit broadcast. 209 210 Mapping to SPIR-V 211 ----------------- 212 213 For informational purposes (non-specification), the following is an 214 expected way for an implementation to map GLSL constructs to SPIR-V 215 constructs: 216 217 Mapping of Storage Classes: 218 219 uniform sampler2D...; -> UniformConstant 220 uniform variable (non-block) -> UniformConstant 221 uniform blockN { ... } ...; -> Uniform, with Block decoration 222 in / out variable -> Input/Output, possibly with block (below) 223 in / out block... -> Input/Output, with Block decoration 224 buffer blockN { ... } ...; -> Uniform, with BufferBlock decoration 225 ... uniform atomic_uint ... -> AtomicCounter 226 shared -> Workgroup 227 <normal global> -> Private 228 229 Mapping of input/output blocks or variables is the same for all versions 230 of GLSL. To the extent variables or members are available in a 231 version and a stage, its location is as follows: 232 233 These are mapped to SPIR-V individual variables, with similarly spelled 234 built-in decorations (except as noted): 235 236 General: 237 238 in gl_VertexID 239 in gl_InstanceID 240 in gl_InvocationID 241 in gl_PatchVerticesIn (PatchVertices) 242 in gl_PrimitiveIDIn (PrimitiveID) 243 in/out gl_PrimitiveID (in/out based only on storage qualifier) 244 in gl_TessCoord 245 246 in/out gl_Layer 247 in/out gl_ViewportIndex 248 249 patch in/out gl_TessLevelOuter (uses Patch decoration) 250 patch in/out gl_TessLevelInner (uses Patch decoration) 251 252 Compute stage only: 253 254 in gl_NumWorkGroups 255 in gl_WorkGroupSize 256 in gl_WorkGroupID 257 in gl_LocalInvocationID 258 in gl_GlobalInvocationID 259 in gl_LocalInvocationIndex 260 261 Fragment stage only: 262 263 in gl_FragCoord 264 in gl_FrontFacing 265 in gl_ClipDistance 266 in gl_CullDistance 267 in gl_PointCoord 268 in gl_SampleID 269 in gl_SamplePosition 270 in gl_HelperInvocation 271 out gl_FragDepth 272 in gl_SampleMaskIn (SampleMask) 273 out gl_SampleMask (in/out based only on storage qualifier) 274 275 These are mapped to SPIR-V blocks, as implied by the pseudo code, with 276 the members decorated with similarly spelled built-in decorations: 277 278 Non-fragment stage: 279 280 in/out gl_PerVertex { // some subset of these members will be used 281 gl_Position 282 gl_PointSize 283 gl_ClipDistance 284 gl_CullDistance 285 } // name of block is for debug only 286 287 There is at most one input and one output block per stage in SPIR-V. 288 The subset and order of members will match between stages sharing an 289 interface. 290 291 Mapping of precise: 292 293 precise -> NoContraction 294 295 Mapping of atomic_uint /offset/ layout qualifier 296 297 offset -> Offset (decoration) 298 299 Mapping of images 300 301 imageLoad() -> OpImageRead 302 imageStore() -> OpImageWrite 303 texelFetch() -> OpImageFetch 304 305 imageAtomicXXX(params, data) -> %ptr = OpImageTexelPointer params 306 OpAtomicXXX %ptr, data 307 308 XXXQueryXXX(combined) -> %image = OpImage combined 309 OpXXXQueryXXX %image 310 311 Mapping of layouts 312 313 std140/std430 -> explicit *Offset*, *ArrayStride*, and *MatrixStride* 314 Decoration on struct members 315 shared/packed -> not allowed 316 <default> -> not shared, but std140 or std430 317 xfb_offset -> *Offset* Decoration on the object or struct member 318 xfb_buffer -> *XfbBuffer* Decoration on the object 319 xfb_stride -> *XfbStride* Decoration on the object 320 any xfb_* -> the *Xfb* Execution Mode is set 321 captured XFB -> has both *XfbBuffer* and *Offset* 322 non-captured -> lacking *XfbBuffer* or *Offset* 323 324 max_vertices -> OutputVertices 325 326 Mapping of barriers 327 328 barrier() (compute) -> OpControlBarrier(/*Execution*/Workgroup, 329 /*Memory*/Workgroup, 330 /*Semantics*/AcquireRelease | 331 WorkgroupMemory) 332 333 barrier() (tess control) -> OpControlBarrier(/*Execution*/Workgroup, 334 /*Memory*/Invocation, 335 /*Semantics*/None) 336 337 memoryBarrier() -> OpMemoryBarrier(/*Memory*/Device, 338 /*Semantics*/AcquireRelease | 339 UniformMemory | 340 WorkgroupMemory | 341 ImageMemory) 342 343 memoryBarrierBuffer() -> OpMemoryBarrier(/*Memory*/Device, 344 /*Semantics*/AcquireRelease | 345 UniformMemory) 346 347 memoryBarrierShared() -> OpMemoryBarrier(/*Memory*/Device, 348 /*Semantics*/AcquireRelease | 349 WorkgroupMemory) 350 351 memoryBarrierImage() -> OpMemoryBarrier(/*Memory*/Device, 352 /*Semantics*/AcquireRelease | 353 ImageMemory) 354 355 groupMemoryBarrier() -> OpMemoryBarrier(/*Memory*/Workgroup, 356 /*Semantics*/AcquireRelease | 357 UniformMemory | 358 WorkgroupMemory | 359 ImageMemory) 360 361 Mapping of atomics 362 363 all atomic builtin functions -> Semantics = None(Relaxed) 364 365 atomicExchange() -> OpAtomicExchange 366 imageAtomicExchange() -> OpAtomicExchange 367 atomicCompSwap() -> OpAtomicCompareExchange 368 imageAtomicCompSwap() -> OpAtomicCompareExchange 369 NA -> OpAtomicCompareExchangeWeak 370 371 atomicCounterIncrement -> OpAtomicIIncrement 372 atomicCounterDecrement -> OpAtomicIDecrement (with post decrement) 373 atomicCounter -> OpAtomicLoad 374 375 Mapping of uniform initializers 376 377 Using the OpVariable initializer logic, but only from a constant 378 instruction (not a global one). 379 380 Mapping of other instructions 381 382 % -> OpUMod/OpSMod 383 mod() -> OpFMod 384 NA -> OpSRem/OpFRem 385 386 pack/unpack (conversion) -> pack/unpack in GLSL extended instructions 387 pack/unpack (no conversion) -> OpBitcast 388 389 Differences Relative to Other Specifications 390 -------------------------------------------- 391 392 The following summarizes the set of differences to existing specifications. 393 394 Additional use of existing SPIR-V features, relative to Vulkan: 395 + The *UniformConstant* Storage Class can be used on individual 396 variables at global scope. (That is, uniforms don't have to be in a 397 block, unless they are built-in members that are in block in GLSL 398 version 4.5.) 399 + *AtomicCounter* Storage Class can use the *Offset* decoration 400 + OriginLowerLeft 401 + Uniforms support constant initializers. 402 403 Corresponding features that GLSL keeps, despite GL_KHR_vulkan_glsl removal: 404 . default uniforms (those not inside a uniform block) 405 . atomic-counter bindings have the /offset/ layout qualifier 406 . special rules for locations for input doubles in the vertex shader 407 . origin_lower_left 408 409 Addition of features to GLSL: 410 + specialization constants, as per GL_KHR_vulkan_glsl 411 + #define GL_SPIRV 100, when compiled for OpenGL and SPIR-V generation 412 + offset can organize members in a different order than declaration order 413 414 Non-acceptance of SPIR-V features, relative to Vulkan: 415 - VertexIndex and InstanceIndex built-in decorations cannot be used 416 - Push-constant buffers cannot be used 417 - *DescriptorSet* must always be 0, if present 418 - input targets and input attachments 419 - OpTypeSampler 420 421 Corresponding features not added to GLSL that GL_KHR_vulkan_glsl added: 422 . gl_VertexIndex and gl_InstanceIndex 423 (gl_VertexID and gl_InstanceID have same semantics as in GLSL) 424 . push_constant buffers 425 . descriptor sets 426 . input_attachment_index and accessing input targets 427 . shader-combining of textures and samplers 428 429 Removal of features from GLSL, as removed by GL_KHR_vulkan_glsl: 430 - subroutines 431 - the already deprecated texturing functions (e.g., texture2D()) 432 - the already deprecated noise functions (e.g., noise1()) 433 - compatibility profile features 434 - gl_DepthRangeParameters and gl_NumSamples 435 - *shared* and *packed* block layouts 436 437 Addition of features to The OpenGL Graphics System: 438 + a command to associate a SPIR-V module with a program (ShaderBinary) 439 + a command to select a SPIR-V entry point and set specialization 440 constants in a SPIR-V module (SpecializeShaderARB) 441 + a new appendix for SPIR-V validation rules, precision, etc. 442 443 Changes of system features, relative to Vulkan: 444 . Vulkan uses only one binding point for a resource array, while OpenGL 445 still uses multiple binding points, so binding numbers are counted 446 differently for SPIR-V used in Vulkan and OpenGL 447 . gl_FragColor can be written to, but it won't broadcast, for versions of 448 OpenGL that support gl_FragColor 449 . Vulkan does not allow multi-dimensional arrays of resources like 450 UBOs and SSBOs in its SPIR-V environment spec. SPIR-V supports 451 it and OpenGL already allows this for GLSL shaders. SPIR-V 452 for OpenGL also allows it. 453 454 Additions to the SPIR-V specification: 455 + *Offset* can also apply to an object, for transform feedback. 456 + *Offset* can also apply to a default uniform, for atomic_uint offset. 457 458New Procedures and Functions 459 460 void SpecializeShaderARB(uint shader, 461 const char* pEntryPoint, 462 uint numSpecializationConstants, 463 const uint* pConstantIndex, 464 const uint* pConstantValue); 465 466New Tokens 467 468 Accepted by the <binaryformat> parameter of ShaderBinary: 469 470 SHADER_BINARY_FORMAT_SPIR_V_ARB 0x9551 471 472 Accepted by the <pname> parameter of GetShaderiv: 473 474 SPIR_V_BINARY_ARB 0x9552 475 476Modifications to Chapter 7 of the OpenGL 4.5 (Core Profile) Specification 477(Programs and Shaders) 478 479 Additions to overview of Chapter 7, "Programs and Shaders" 480 481 Modify the 4th paragraph beginning with "Alternatively, pre-compiled 482 shader binary code..." as follows: 483 484 "Alternatively, pre-compiled shader binary code can be loaded 485 into a shader object. A SPIR-V module can also be associated with a 486 shader and then _specialized_. An implementation must..." 487 488 Additions to Section 7.1, "Shader Objects": 489 490 Add the following to the end of the description of ShaderSource: 491 492 "If <shader> was previously associated with a SPIR-V module (via the 493 ShaderBinary command), that association is broken. Upon successful 494 completion of this command the SPIR_V_BINARY_ARB state of <shader> 495 is set to FALSE." 496 497 Add a new error for the CompileShader command: 498 499 "An INVALID_OPERATION error is generated if the SPIR_V_BINARY_ARB 500 state of <shader> is TRUE." 501 502 [[ if ARB_parallel_shader_compile is supported: ]] 503 Modify the preamble to the MaxShaderCompilerThreadsARB command to read: 504 505 "Applications may use the following to hint to the driver the maximum 506 number of background threads it would like to be used in the process 507 of compiling or specializing shaders, or linking programs, ..." 508 509 Additions to Section 7.2, "Shader Binaries": 510 511 In the description of ShaderBinary, remove the text stating that GL 512 defines no specific binary formats and replace it with the following: 513 514 "GL defines an execution environment for shaders created from SPIR-V 515 modules. To load a SPIR-V binary into GL, set <binaryformat> to 516 SHADER_BINARY_FORMAT_SPIR_V_ARB. <binary> should point to the start 517 of a valid SPIR-V module binary and <length> should contain the length 518 of that binary, in bytes. Upon successful consumption of the SPIR-V 519 module: 520 * each entry of <shaders> will be associated with that SPIR-V module, 521 * the SPIR_V_BINARY_ARB state of each shader is set to TRUE, 522 * the COMPILE_STATUS of each of these shaders is set to FALSE, 523 * any existing source string (specified by ShaderSource) is removed, and 524 * any information about a previous compile is lost. 525 Shaders associated with SPIR-V modules must be finalized by calling 526 SpecializeShaderARB as described in Section 7.2.1. 527 528 GL also provides a mechanism to obtain token values for additional 529 binary formats provided by extensions. The number of binary formats..." 530 531 Replace the error condition for ShaderBinary which states: 532 533 "An INVALID_OPERATION error is generated if more than one of the 534 handles in shaders refers to the same type of shader object." 535 536 with the following: 537 538 "An INVALID_OPERATION error is generated if <binaryformat> is not 539 SHADER_BINARY_FORMAT_SPIR_V_ARB and more than one of the 540 handles in shaders refers to the same type of shader object." 541 542 Insert Subsection 7.2.1, "Shader Specialization": 543 544 "Shaders associated with SPIR-V modules must be specialized before they 545 can be linked into a program object. It is not necessary to specialize 546 the shader before it is attached to a program object. Once specialized, 547 a shader may not be specialized again without first re-associating the 548 original SPIR-V module with it, through ShaderBinary. 549 550 Specialization does two things: 551 552 * Selects the name of the entry point, for that shader's stage, from 553 the SPIR-V module. 554 * Sets the values of all, or a subset of, the specialization constants 555 in the SPIR-V module. 556 557 To specialize a shader created from a SPIR-V module, call: 558 559 void SpecializeShaderARB(uint shader, 560 const char* pEntryPoint, 561 uint numSpecializationConstants, 562 const uint* pConstantIndex, 563 const uint* pConstantValue); 564 565 <shader> is the name of a shader object containing unspecialized SPIR-V 566 as created from a successful call to ShaderBinary to which a SPIR-V 567 module was passed. <pEntryPoint> is a pointer to a NUL-terminated UTF-8 568 string specifying the name of the entry point in the SPIR-V module to 569 use for this shader. <numSpecializationConstants> is the number 570 of specialization constants whose values to set in this call. 571 <pConstantIndex> is a pointer to an array of 572 <numSpecializationConstants> unsigned integers, each holding the index 573 of a specialization constant in the SPIR-V module whose value to set. 574 The corresponding entry in <pConstantValue> is used to set the value of 575 the specialization constant indexed by the entry in <pConstantIndex>. 576 Although this array is of unsigned integer, each entry is bitcast to the 577 appropriate type for the module, and therefore, floating-point constants 578 may be set by including their IEEE-754 bit representation in the 579 <pConstantValue> array. Specialization constants not referenced by 580 <pConstantIndex> retain their default values as specified in the SPIR-V 581 module. 582 583 On successful shader specialization, the compile status for <shader> 584 is set to TRUE. On failure, the compile status for <shader> is set to 585 FALSE and additional information about the cause of the failure may 586 be available in the shader compilation log. Specialization can fail 587 if the SPIR-V module fails to meet the requirements listed in Appendix 588 "The OpenGL SPIR-V Execution Environment". 589 590 Errors 591 592 An INVALID_VALUE error is generated if <shader> is not the name 593 of either a program or shader object. 594 595 An INVALID_OPERATION error is generated if <shader> is the name 596 of a program object. 597 598 INVALID_OPERATION is generated if the value of SPIR_V_BINARY_ARB 599 for <shader> is not TRUE, or if the shader has already been 600 specialized. 601 602 INVALID_VALUE is generated if <pEntryPoint> does not match the 603 <Name> of any OpEntryPoint declaration in the SPIR-V module 604 associated with <shader>. 605 606 INVALID_OPERATION is generated if the <Execution Mode> of the 607 OpEntryPoint indicated by <pEntryPoint> does not match the type 608 of <shader>. 609 610 INVALID_VALUE is generated if any element of <pConstantIndex> 611 refers to a specialization constant that does not exist in the 612 shader module contained in <shader>." 613 614 Additions to Section 7.3, "Program Objects": 615 616 Modify the first paragraph after the AttachShader command to read: 617 618 "Shader objects may be attached to program objects before source code 619 or shader binary has been loaded into the shader object, or before the 620 shader object has been compiled or specialized. Multiple shader ..." 621 622 Modify the first paragraph after the LinkProgram command to read: 623 624 "Linking can fail for a variety of reasons as specified in the OpenGL 625 Shading language specification for source shaders, or if the requirements 626 in the Appendix "The OpenGL SPIR-V Execution Environment" are not met 627 for SPIR-V shaders, as well as any of the following reasons..." 628 629 Modify the second bullet point in the list of reasons LinkProgram can 630 fail: 631 632 "One or more of the shader objects attached to <program> are not compiled 633 or specialized successfully." 634 635 Add a new bullet point to this list: 636 637 "All the shader objects attached to <program> do not have the same value 638 for the SPIR_V_BINARY_ARB state." 639 640 Modifications to Section 7.3.1, "Program Interfaces" 641 642 Add the following paragraph after the list of rules describing how the 643 name string is generated, before the paragraph that begins "The order 644 of the active resource list...": 645 646 "For shaders constructed from SPIR-V binaries (that is with a state of 647 SPIR_V_BINARY_ARB equal to TRUE), variables may not have names associated 648 with them as the OpName and OpMemberName debug instructions are optional 649 and may not be present in a SPIR-V module. When the Op*Name instructions 650 are present, it is implementation-dependent if these are reported via 651 the name reflection APIs. If no _name reflection information_ is 652 available, the name string associated with each active variable is the 653 empty string (""). In this case, any queries that operate with a name 654 as input, will return INVALID_INDEX or -1 as appropriate, and any queries 655 that return information about the name of a resource will report a 656 name length of one (for the null character) and return an empty string 657 with a length of zero." 658 659 Add a new subsection "7.4.spv, SPIR-V Shader Interface Matching" after 660 section 7.4.1 "Shader Interface Matching": 661 662 "7.4.spv, SPIR-V Shader Interface Matching 663 664 SPIR-V shaders must also follow the rules in this section, whether they 665 add to or override those given in section 7.4.1 "Shader Interface 666 Matching." Most importantly, SPIR-V variables and structure members 667 do not have names and so no interface matching is done by name strings. 668 669 All variables forming the input or output interfaces of shader stages 670 must be listed as operands to the *OpEntryPoint* instruction and are 671 declared with the *Input* or *Output* Storage Classes, respectively, 672 in the SPIR-V module. 673 674 Shader built-in variables meeting the following requirements define the 675 built-in interface block. They must: 676 - be explicitly declared (there are no implicit built-ins), 677 - be decorated with the *BuiltIn* decoration, 678 - be declared in a block whose top-level members are the built-ins. 679 - not have any *Location* or *Component* decorations. 680 Built-ins only participate in interface matching if they are declared in 681 such a block. There must be no more than one built-in interface block 682 per shader per interface. 683 684 User-defined interface variables must be decorated with a *Location* 685 and can also be decorated with a *Component*. These correspond to the 686 location and component discussed in section 7.4.1 "Shader Interface 687 Matching". Uniform and shader storage block variables must also be 688 decorated with a *Binding*. 689 690 A user-defined output variable is considered to match an input variable 691 in the subsequent stage only if the two variables are declared with the 692 same *Location* and *Component* decoration and match in type and 693 decoration, except that interpolation decorations are not required to 694 match. 695 696 Variables or block members declared as structures are considered to 697 match in type if and only if the structure members match in type, 698 decoration, number, and declaration order. Variables or block members 699 declared as arrays are considered to match in type only if both 700 declarations specify the same element type and size. 701 702 At an interface between two non-fragment shader stages, the built-in 703 interface block must match exactly, as described above. At an interface 704 involving the fragment shader inputs, the presence or absence of any 705 built-in output does not affect the interface matching. 706 707 At an interface between two shader stages, the user-defined variable 708 interface must match exactly. Additionally, scalar and vector inputs 709 are well-defined if there is a corresponding output satisfying all of 710 the following conditions: 711 - the input and output match exactly in decoration, 712 - the output is a vector with the same basic type and has at least as 713 many components as the input, and 714 - the common component type of the input and output is 32-bit integer 715 or floating-point (64-bit component types are excluded). 716 In this case, the components of the input will be taken from the first 717 components of the output, and any extra components of the output will 718 be ignored." 719 720 Add a new subsection 7.6.2.spv "SPIR-V Uniform Offsets and Strides" after 721 section 7.6.2.2 "Standard Uniform Block Layout": 722 723 "7.6.2.spv SPIR-V Uniform Offsets and Strides 724 725 The SPIR-V decorations *GLSLShared* or *GLSLPacked* must not be used. A 726 variable in the *Uniform* Storage Class decorated as a *Block* or 727 *BufferBlock* must be explicitly laid out using the *Offset*, *ArrayStride*, 728 and *MatrixStride* decorations. These must follow the alignment rules listed 729 above, but not necessarily the packing rules. More specifically, explicit 730 SPIR-V offsets and strides must obey the following: 731 732 Define the /base alignment/ recursively as: 733 734 - A scalar of size N has a base alignment of N. 735 736 - A two-component vector, with components of size N, has a base alignment 737 of 2N. 738 739 - A three- or four-component vector, with components of size N, has a base 740 alignment of 4N. 741 742 - An array has a base alignment equal to the base alignment of its element 743 type, rounded up to a multiple of 16. 744 745 - A structure has a base alignment equal to the largest base alignment of 746 any of its members, rounded up to a multiple of 16. 747 748 - A row-major matrix of C columns has a base alignment equal to the base 749 alignment of a vector of C matrix components. 750 751 - A column-major matrix has a base alignment equal to the base alignment 752 of the matrix column type. 753 754 Given this definition, blocks must be laid out such that: 755 756 - The *Offset* decoration must be a multiple of its base alignment. 757 758 - Any *ArrayStride* or *MatrixStride* decoration must be an integer 759 multiple of the base alignment of the array or matrix. 760 761 - The *Offset* decoration of a member must not place it between the end of 762 a structure or an array and the next multiple of the base alignment of 763 that structure or array. 764 765 - The numeric order of *Offset* decorations need not follow member 766 declaration order. 767 768 With one exception: Variables in the *Uniform* storage class with a 769 decoration of *BufferBlock* do not need their base alignments rounded up 770 to a multiple of 16. 771 772 Modifications to Section 7.13 "Shader, Program, and Program Pipeline Queries" 773 774 Add the following to the list of <pname> that are accepted by GetShaderiv: 775 776 "If <pname> is SPIR_V_BINARY_ARB, TRUE is returned if the shader has been 777 successfully associated with a SPIR-V binary module by the ShaderBinary 778 command and FALSE is returned otherwise." 779 780 Modify the description of COMPILE_STATUS to be as follows: 781 782 "If <pname> is COMPILE_STATUS, TRUE is returned if the shader was last 783 compiled or specialized successfully, and FALSE is returned otherwise." 784 785 [[ if ARB_parallel_shader_compile is supported: ]] 786 Modify the description of COMPLETION_STATUS_ARB to be as follows: 787 788 "If <pname> is COMPLETION_STATUS_ARB, FALSE is returned if the shader is 789 currently being compiled or specialized by a background thread, TRUE 790 otherwise." 791 792 Modify the Errors list for GetShaderiv and add SPIR_V_BINARY_ARB to the 793 list of <pname> that are accepted. 794 795 In the description of the GetProgramiv command, modify the definitions of 796 the ACTIVE_ATTRIBUTE_MAX_LENGTH, ACTIVE_UNIFORM_MAX_LENGTH, 797 TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH, and 798 ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH <pname> values to be as follows: 799 800 "If pname is ACTIVE_ATTRIBUTE_MAX_LENGTH, the length of the longest 801 active attribute name, including a null terminator, is returned. 802 If no active attributes exist, zero is returned. If no name reflection 803 information is available, one is returned." 804 805 "If pname is ACTIVE_UNIFORM_MAX_LENGTH, the length of the longest active 806 uniform name, including a null terminator, is returned. If no active 807 uniforms exist, zero is returned. If no name reflection information is 808 available, one is returned." 809 810 "If pname is TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH, the length of the 811 longest output variable name specified to be used for transform feedback, 812 including a null terminator, is returned. If no outputs are used for 813 transform feedback, zero is returned. If no name reflection information 814 is available, one is returned." 815 816 "If pname is ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, the length of the 817 longest active uniform block name, including the null terminator, is 818 returned. If no active uniform blocks exist, zero is returned. If no 819 name reflection information is available, one is returned." 820 821 Modify the description of GetShaderInfoLog to read as follows: 822 823 "If <shader> is a shader object, GetShaderInfoLog will return either 824 an empty string or information about the last compilation or 825 specialization attempt for that object." 826 827 Modifications to Section 7.14 "Required State": 828 829 Add to the list of state per shader object: 830 831 "A boolean holding the SPIR-V binary status, initially FALSE." 832 833Modifications to Chapter 11 of the OpenGL 4.5 (Core Profile) Specification 834(Programmable Vertex Processing) 835 836 Modify Section 11.1.1 "Vertex Attributes" 837 838 Replace the last sentence in the first paragraph ("This binding can be..") 839 with the following: 840 841 "This binding can be specified in the shader text using the location 842 qualifier, in a SPIR-V shader using the *Location* decoration, by the 843 application before the program is linked, or automatically assigned by 844 the GL when the program is linked." 845 846 In the second paragraph of the definition of BindAttribLocation, replace 847 occurrences of "shader text" with "shader text or SPIR-V binary". 848 849 Add to the end of the third paragraph of the definition of 850 BindAttribLocation: 851 852 "BindAttribLocation has no effect on SPIR-V shaders as the locations 853 must always be fully specified in the SPIR-V shader as described in 854 section 11.1.1.1 (SPIR-V Vertex Input Interface)." 855 856 Add the following new subsection to the end of section 11.1.1 857 "Vertex Attributes": 858 859 "Section 11.1.1.1 SPIR-V Vertex Input interface 860 861 When a SPIR-V vertex stage is present, the vertex shader 862 variables listed by *OpEntryPoint* with the *Input* Storage Class 863 form the vertex input attribute interface. These inputs must be 864 decorated with a *Location* and can also be decorated with a 865 *Component* decoration. These correspond to the location and 866 component layout discussed in section 11.1.1 "Vertex Attributes." 867 868 The vertex shader input variables are matched only by the 869 *Location* and *Component* decorations, and must have a corresponding 870 attribute and binding in the pipeline." 871 872 Modify section 11.1.2.1 "Output Variables" 873 874 Replace the first sentence of the second paragraph describing transform 875 feedback mode ("The set of variables to record...") with the following: 876 877 "The set of variables to record can be specified in shader text using 878 xfb_buffer, xfb_offset, or xfb_stride layout qualifiers. For SPIR-V 879 shaders, these are specified by the *XfbBuffer*, *Offset*, and *XfbStride* 880 decorations, respectively. When a SPIR-V entry point is using any of these 881 for transform feedback, it must declare the *Xfb* Execution Mode." 882 883 Modify the last paragraph of the definition of TransformFeedbackVaryings 884 ("If the shader is used to record...") as follows: 885 886 "If the shader used to record output variables for transform feedback 887 varyings uses the xfb_buffer, xfb_offset, or xfb_stride layout qualifiers, 888 or its SPIR-V entry point declares the *Xfb* Execution Mode, the values 889 specified by TransformFeedbackVaryings are ignored, and the set of 890 variables captured for transform feedback is instead derived from the 891 specified layout qualifiers or SPIR-V decorations: outputs specifying both 892 an *XfbBuffer* and an *Offset* are captured, while outputs not specifying 893 both of these are not captured. Values are captured each time the shader 894 writes to such a decorated object." 895 896Modifications to Chapter 15 of the OpenGL 4.5 (Core Profile) Specification 897(Programmable Fragment Processing) 898 899 Modify section 15.2.3 "Shader Outputs" 900 901 Replace the sentence leading up to the definition of the 902 BindFragDataLocationIndexed command with the following: 903 904 "The binding of a user-defined output variable to components of a 905 fragment color number can be specified explicitly in the shader text, 906 SPIR-V shader, or using the command ..." 907 908 Modify the paragraph following the definition of BindFragDataLocation, 909 replacing the second and third sentences with the following: 910 911 "Output variables declared with location, component, or index layout 912 qualifiers will use the values specified in the shader text. For SPIR-V 913 shaders, these are specified by the *Location*, *Component*, and *Index* 914 decorations. Output variables without such layout or decoration qualifiers 915 will use the bindings specified by BindFragDataLocationIndexed or 916 BindFragDataLocation, if any. BindFragDataLocation* has no effect on 917 SPIR-V shaders as the locations must always be fully specified as 918 described in section 15.2.3.1 (SPIR-V Fragment Output Interface)." 919 920 Add the following new subsection to the end of 15.2.3 "Shader Outputs" 921 922 "Section 15.2.3.1 "SPIR-V Fragment Output Interface" 923 924 When a SPIR-V fragment stage is present, the variables listed by 925 *OpEntryPoint* with the *Output* Storage Class form the fragment 926 output interface. These variables must be decorated with a *Location* 927 They can also be decorated with a *Component* and/or an *Index*. 928 929 User-defined fragment shader output variables are matched only by their 930 *Location*, *Component*, and *Index* decorations. If two outputs 931 are placed within the same location, they must have the same underlying 932 type (floating-point or integer). No component aliasing of output 933 variables is allowed. That is, there must not be two output variables 934 which have the same location, component, and index, either explicitly 935 declared or implied. Output values written by a fragment shader must 936 be declared with either *OpTypeFloat* or *OpTypeInt*, and a *Width* of 937 32. Composites of these types are also permitted." 938 939 940Add Appendix A.spv "The OpenGL SPIR-V Execution Environment" to the OpenGL 4.5 941(Core Profile) Specification 942 943 "A.spv.1 (Required Versions and Formats) 944 945 An implementation of this extension must support the 1.0 version of 946 SPIR-V and the 1.0 version of the SPIR-V Extended Instructions for GLSL. 947 948 A SPIR-V module passed into ShaderBinary is interpreted as a series of 949 32-bit words in host endianness, with literal strings packed as described 950 in section 2.2 of the SPIR-V Specification. The first few words of the 951 SPIR-V module must be a magic number and a SPIR-V version number, as 952 described in section 2.3 of the SPIR-V Specification. 953 954 A.spv.2 (Valid SPIR-V Built-In Variable Decorations) 955 956 Implementations supporting GL 4.5 must support the following 957 built-in variable decorations. When used with earlier versions 958 of GL, this list may be subset to the functionality in that 959 version. Each built-in below lists the minimum OpenGL version 960 (or extension) that is required to use the built-in variable 961 decoration. 962 963 964 Built-in Variable Decoration Minimum GL version (Extension) 965 ---------------------------- ----------------------------- 966 NumWorkgroups GL 4.3 (ARB_compute_shader) 967 WorkgroupSize GL 4.3 (ARB_compute_shader) 968 WorkgroupId GL 4.3 (ARB_compute_shader) 969 LocalInvocationId GL 4.3 (ARB_compute_shader) 970 GlobalInvocationId GL 4.3 (ARB_compute_shader) 971 LocalInvocationIndex GL 4.3 (ARB_compute_shader) 972 973 VertexId 974 InstanceId 975 976 Position 977 PointSize 978 ClipDistance 979 CullDistance GL 4.5 (ARB_cull_distance) 980 981 PrimitiveId 982 InvocationId GL 4.0 (ARB_tessellation_shader) 983 984 Layer 985 ViewportIndex GL 4.1 (ARB_viewport_array) 986 987 PatchVertices GL 4.0 (ARB_tessellation_shader) 988 TessLevelOuter GL 4.0 (ARB_tessellation_shader) 989 TessLevelInner GL 4.0 (ARB_tessellation_shader) 990 TessCoord GL 4.0 (ARB_tessellation_shader) 991 992 FragCoord 993 FrontFacing 994 PointCoord 995 SampleId GL 4.0 (ARB_sample_shading) 996 SamplePosition GL 4.0 (ARB_sample_shading) 997 SampleMask GL 4.0 (ARB_sample_shading) 998 HelperInvocation GL 4.5 (ARB_ES3_1_compatibility) 999 FragDepth 1000 1001 A.spv.3 (Valid SPIR-V Capabilities) 1002 1003 Implementations supporting OpenGL 4.5 must support the following 1004 capability operands declared by OpCapability. 1005 1006 When used against earlier versions of GL, this list may be subset 1007 to the functionality in that version. Each capability below lists 1008 the OpenGL version (or extension) that is required to use the 1009 OpCapability. 1010 1011 OpCapability Minimum GL version (Extension) 1012 ------------------------------ ----------------------------- 1013 Matrix 1014 Shader 1015 Geometry 1016 Tessellation GL 4.0 (ARB_tessellation_shader) 1017 Float64 GL 4.0 (ARB_gpu_shader_fp64) 1018 AtomicStorage GL 4.2 (ARB_shader_atomic_counters) 1019 TessellationPointSize GL 4.0 (ARB_tessellation_shader) 1020 GeometryPointSize 1021 ImageGatherExtended GL 4.0 (ARB_gpu_shader5) 1022 StorageImageMultisample GL 4.2 (ARB_shader_image_load_store) 1023 UniformBufferArrayDynamicIndexing GL 4.0 (ARB_gpu_shader5) 1024 SampledImageArrayDynamicIndexing GL 4.0 (ARB_gpu_shader5) 1025 StorageBufferArrayDynamicIndexing GL 4.3 (ARB_shader_storage_buffer_object) 1026 StorageImageArrayDynamicIndexing GL 4.2 (ARB_shader_image_load_store) 1027 ClipDistance 1028 CullDistance GL 4.5 (ARB_cull_distance) 1029 ImageCubeArray GL 4.0 (ARB_texture_cube_map_array) 1030 SampleRateShading GL 4.0 (ARB_sample_shading) 1031 ImageRect 1032 SampledRect 1033 Sampled1D 1034 Image1D 1035 SampledCubeArray GL 4.0 (ARB_texture_cube_map_array) 1036 SampledBuffer 1037 ImageBuffer 1038 ImageMSArray 1039 StorageImageExtendedFormats GL 4.2 (ARB_shader_image_load_store) 1040 ImageQuery 1041 DerivativeControl GL 4.5 (ARB_derivative_control) 1042 InterpolationFunction GL 4.0 (ARB_gpu_shader5) 1043 TransformFeedback 1044 GeometryStreams GL 4.0 (ARB_gpu_shader5) 1045 StorageImageWriteWithoutFormat GL 4.2 (ARB_shader_image_load_store) 1046 MultiViewport GL 4.1 (ARB_viewport_array) 1047 1048 Implementations supporting ARB_gpu_shader_int64 must support the 1049 following operand declared by OpCapability: 1050 1051 Int64 1052 1053 Implementations supporting ARB_sparse_texture2 must support the 1054 following operand declared by OpCapability: 1055 1056 SparseResidency 1057 1058 Implementations supporting ARB_sparse_texture_clamp must support the 1059 following operand declared by OpCapability: 1060 1061 MinLod 1062 1063 Implementations supporting EXT_shader_image_load_formatted must 1064 support the following operand declared by OpCapability: 1065 1066 StorageImageReadWithoutFormat 1067 1068 Implementations supporting NV_shader_atomic_int64 must support the 1069 following operand declared by OpCapability: 1070 1071 Int64Atomics 1072 1073 A.spv.4 (Validation rules) 1074 1075 (in addition to what's allowed/disallowed above) 1076 1077 Every entry point must have no return value and accept no arguments. 1078 1079 The *Logical* addressing model must be selected. 1080 1081 *Scope* for execution must be limited to: 1082 - Workgroup 1083 - Subgroup 1084 1085 *Scope* for memory must be limited to: 1086 - Device 1087 - Workgroup 1088 - Invocation 1089 1090 *Storage Class* must be limited to: 1091 - *UniformConstant* 1092 - *Input* 1093 - *Uniform* 1094 - *Output* 1095 - *Workgroup* 1096 - *Private* 1097 - *Function* 1098 - *AtomicCounter* 1099 - *Image* 1100 1101 Images: 1102 - OpTypeImage must declare a scalar 32-bit float or 32-bit integer 1103 type for the /Sampled Type/. 1104 - OpSampledImage, OpImageQuerySizeLod, and OpImageQueryLevels must 1105 only consume an /Image/ operand whose type has its /Sampled/ 1106 operand set to 1. 1107 - The /Depth/ operand of OpTypeImage is ignored. 1108 - The Image Format of OpImageWrite must not be Unknown, unless the 1109 StorageImageWriteWithoutFormat OpCapability was declared. 1110 1111 *AtomicCounter* storage class: 1112 - Unless otherwise specified, variables declared in the AtomicCounter 1113 storage class must be unsigned 32-bit integers. 1114 - When using the Atomic Instructions, the 'Memory Semantics' operand 1115 must be *AtomicCounterMemory*, without inclusion of memory-ordering 1116 bits (implying *Relaxed* semantics). 1117 1118 Decorations: 1119 - The Flat, NoPerspective, Sample, and Centroid decorations must not 1120 be used on variables with Storage Class other than *Input* or on 1121 variables used in the interface of non-fragment shader entry points. 1122 - The Patch decoration must not be used on variables in the interface 1123 of a vertex, geometry, or fragment shader stage's entry point. 1124 - OpTypeRuntimeArray must only be used for the last member of an 1125 OpTypeStruct in the Uniform Storage Class and is decorated as 1126 BufferBlock. 1127 - If the *Xfb* Execution Mode is set, any output variable that is at 1128 least partially captured: 1129 * must be decorated with an *XfbBuffer*, declaring the capturing buffer 1130 * must have at least one captured output variable in the capturing 1131 buffer decorated with an *XfbStride* (and all such *XfbStride* values 1132 for the capturing buffer must be equal) 1133 - If the *Xfb* Execution Mode is set, any captured output: 1134 * must be a non-structure decorated with *Offset* or a member of a 1135 structure whose type member is decorated with *Offset* 1136 (not all members of such a struct need be captured) 1137 * must be a numerical type scalar, vector, matrix, or array of these 1138 * must have an *Offset* that is a multiple of its first component 1139 * must have an *Offset* that is a multiple of 8 if any captured output 1140 in the capturing buffer is a 64-bit type, in which case the 1141 corresponding *XfbStride* must also be a multiple of 8 1142 * must not overlap (i.e., alias in any capturing buffer) any other 1143 captured output 1144 * must not have an *Offset* that causes overflow of the *XfbStride* 1145 1146 Compute Shaders 1147 - For each compute shader entry point, either a LocalSize execution 1148 mode or an object decorated with the WorkgroupSize decoration must 1149 be specified. 1150 1151 Recursion: 1152 - For all entry points, the static function-call graph rooted at that 1153 entry point must not contain cycles. 1154 1155 User declared input and output variables: 1156 - Must not use OpTypeBool, either directly or as part of an aggregate. 1157 1158 Atomic Access Rules: 1159 - Atomic instructions must declare a scalar 32-bit integer type, or a 1160 scalar 64-bit integer type if the *Int64Atomics* capability is enabled, 1161 for the value pointed to by *Pointer* 1162 1163 A.spv.5 (Precision and Operation of SPIR-V Instructions) 1164 1165 The following rules apply to both single and double-precision floating 1166 point instructions: 1167 - Positive and negative infinities and positive and negative zeros are 1168 generated as dictated by [IEEE 754], but subject to the precisions 1169 allowed in the following table. 1170 - Dividing a non-zero by a zero results in the appropriately signed 1171 IEEE 754 infinity. 1172 - Any denormalized value input into a shader or potentially generated 1173 by any instruction in a shader may be flushed to 0. 1174 - The rounding mode cannot be set and is undefined. 1175 - NaNs are not required to be generated. Instructions that operate on 1176 a NaN are not required to return a NaN as the result. 1177 - Support for signaling NaNs is optional and exceptions are never 1178 raised. 1179 1180 The precision of double-precision instructions is at least that of 1181 single precision. For single precision (32 bit) instructions, 1182 precisions are required to be at least as follows: 1183 1184 Instruction Precision 1185 ----------- --------- 1186 OpFAdd Correctly rounded 1187 OpFSub Correctly rounded 1188 OpFMul Correctly rounded 1189 OpFOrdEqual, OpFUnordEqual Correct result 1190 OpFOrdLessThan, OpFUnordLessThan Correct result 1191 OpFOrdGreaterThan, OpFUnordGreaterThan Correct result 1192 OpFOrdLessThanEqual, OpFUnordLessThanEqual Correct result 1193 OpFOrdGreaterThanEqual, OpFUnordGreaterThanEqual Correct result 1194 OpFDiv 2.5 ULP for b in the 1195 range [2^(-126), 2^(126)] 1196 conversions between types Correctly rounded 1197 1198 A.spv.6 (Precision of GLSL.std.450 Instructions) 1199 1200 Instruction Precision 1201 ----------- --------- 1202 fma() Inherited from OpFMul followed by OpFAdd. 1203 exp(x), exp2(x) (3+2*|x|) ULP 1204 log(), log2() 3 ULP outside the range [0.5, 2.0] 1205 absolute error < 2^(-21) inside the range [0.5, 2.0] 1206 pow(x, y) Inherited from exp2(y * log2(x)) 1207 sqrt() Inherited from 1.0 / inversesqrt() 1208 inversesqrt() 2 ULP 1209 1210 GLSL.std.450 extended instructions specifically defined in terms of 1211 the above instructions inherit the above errors. GLSL.std.450 1212 extended instructions not listed above and not defined in terms of the 1213 above have undefined precision. These include, for example, the 1214 trigonometric functions and determinant. 1215 1216 For the OpSRem and OpSMod instructions, if either operand is negative 1217 the result is undefined. 1218 1219Changes to The OpenGL Shading Language Specification, Version 4.50 1220 1221 Changes to Chapter 1 of the OpenGL Shading Language 4.50 Specification 1222 (Introduction) 1223 1224 Add a paragraph: "The OpenGL API will specify the OpenGL commands used to 1225 manipulate SPIR-V shaders. Independent offline tool chains will compile 1226 GLSL down to the SPIR-V intermediate language. SPIR-V generation is not 1227 enabled with a #extension, #version, or a profile. Instead, use of GLSL 1228 for SPIR-V is determined by offline tool-chain use. See the documentation 1229 of such tools to see how to request generation of SPIR-V for OpenGL." 1230 1231 "GLSL -> SPIR-V compilers must be directed as to what SPIR-V *Capabilities* 1232 are legal at run-time and give errors for GLSL feature use outside those 1233 capabilities. This is also true for implementation-dependent limits that 1234 can be error checked by the front-end against constants present in the 1235 GLSL source: the front-end can be informed of such limits, and report 1236 errors when they are exceeded." 1237 1238 Changes to Chapter 3 of the OpenGL Shading Language 4.50 Specification 1239 (Basics) 1240 1241 After describing the compatibility profile rules, add: 1242 1243 "Compatibility-profile features are not available when generating SPIR-V." 1244 1245 Add a new paragraph at the end of section "3.3 Preprocessor": "When 1246 shaders are compiled for OpenGL SPIR-V, the following predefined macro is 1247 available: 1248 1249 #define GL_SPIRV 100 1250 1251 Changes to Chapter 4 of the OpenGL Shading Language 4.50 Specification 1252 (Variables and Types) 1253 1254 Change section 4.3.3 Constant Expressions: 1255 1256 Add a new very first sentence to this section: 1257 1258 "SPIR-V specialization constants are expressed in GLSL as const, with 1259 a layout qualifier identifier of constant_id, as described in section 1260 4.4.x Specialization-Constant Qualifier." 1261 1262 Add to this bullet 1263 1264 "A constant expression is one of... 1265 * a variable declared with the const qualifier and an initializer, 1266 where the initializer is a constant expression" 1267 1268 to make it say: 1269 1270 "A constant expression is one of... 1271 * a variable declared with the const qualifier and an initializer, 1272 where the initializer is a constant expression; this includes both 1273 const declared with a specialization-constant layout qualifier, 1274 e.g., 'layout(constant_id = ...)' and those declared without a 1275 specialization-constant layout qualifier" 1276 1277 Add to "including getting an element of a constant array," that 1278 1279 "an array access with a specialization constant as an index does 1280 not result in a constant expression" 1281 1282 Add to this bullet 1283 1284 "A constant expression is one of... 1285 * the value returned by a built-in function..." 1286 1287 to make it say: 1288 1289 "A constant expression is one of... 1290 * for non-specialization-constants only: the value returned by a 1291 built-in function... (when any function is called with an argument 1292 that is a specialization constant, the result is not a constant 1293 expression)" 1294 1295 Rewrite the last half of the last paragraph to be its own paragraph 1296 saying: 1297 1298 "Non-specialization constant expressions may be evaluated by the 1299 compiler's host platform, and are therefore not required ... 1300 [rest of paragraph stays the same]" 1301 1302 Add a paragraph 1303 1304 "Specialization constant expressions are never evaluated by the 1305 front-end, but instead retain the operations needed to evaluate them 1306 later on the host." 1307 1308 Add to the table in section 4.4 Layout Qualifiers: 1309 1310 | Individual Variable | Block | Allowed Interface 1311 ------------------------------------------------------------------------ 1312 constant_id = | scalar only | | const 1313 ------------------------------------------------------------------------ 1314 1315 (The other columns remain blank.) 1316 1317 Also add to this table: 1318 1319 | Qualifier Only | Allowed Interface 1320 ------------------------------------------------------- 1321 local_size_x_id = | X | in 1322 local_size_y_id = | X | in 1323 local_size_z_id = | X | in 1324 1325 (The other columns remain blank.) 1326 1327 Expand this sentence in section 4.4.1 Input Layout Qualifiers 1328 1329 "Where integral-constant-expression is defined in section 4.3.3 Constant 1330 Expressions as 'integral constant expression'" 1331 1332 to include the following: 1333 1334 ", with it being a compile-time error for integer-constant-expression to 1335 be a specialization constant: The constant used to set a layout 1336 identifier X in layout(layout-qualifier-name = X) must evaluate to a 1337 front-end constant containing no specialization constants." 1338 1339 At the end of the paragraphs describing the *location* rules, add this 1340 paragraph: 1341 1342 "When generating SPIR-V, all *in* and *out* qualified user-declared 1343 (non built-in) variables and blocks (or all their members) must have a 1344 shader-specified *location*. Otherwise, a compile-time error is 1345 generated." 1346 1347 [Note that an earlier existing rule just above this says "If a block has 1348 no block-level *location* layout qualifier, it is required that either all 1349 or none of its members have a *location* layout qualifier, or a compile- 1350 time error results."] 1351 1352 Add a new subsection at the end of section 4.4: 1353 1354 "4.4.x Specialization-Constant Qualifier 1355 1356 "Specialization constants are declared using "layout(constant_id=...)". 1357 For example: 1358 1359 layout(constant_id = 17) const int arraySize = 12; 1360 1361 "The above makes a specialization constant with a default value of 12. 1362 17 is the ID by which the API or other tools can later refer to 1363 this specific specialization constant. If it is never changed before 1364 final lowering, it will retain the value of 12. It is a compile-time 1365 error to use the constant_id qualifier on anything but a scalar bool, 1366 int, uint, float, or double. 1367 1368 "Built-in constants can be declared to be specialization constants. 1369 For example, 1370 1371 layout(constant_id = 31) gl_MaxClipDistances; // add specialization id 1372 1373 "The declaration uses just the name of the previously declared built-in 1374 variable, with a constant_id layout declaration. It is a compile-time 1375 error to do this after the constant has been used: Constants are strictly 1376 either non-specialization constants or specialization constants, not 1377 both. 1378 1379 "The built-in constant vector gl_WorkGroupSize can be specialized using 1380 the local_size_{xyz}_id qualifiers, to individually give the components 1381 an id. For example: 1382 1383 layout(local_size_x_id = 18, local_size_z_id = 19) in; 1384 1385 "This leaves gl_WorkGroupSize.y as a non-specialization constant, with 1386 gl_WorkGroupSize being a partially specialized vector. Its x and z 1387 components can be later specialized using the ids 18 and 19. These ids 1388 are declared independently from declaring the workgroup size: 1389 1390 layout(local_size_x = 32, local_size_y = 32) in; // size is (32,32,1) 1391 layout(local_size_x_id = 18) in; // constant_id for x 1392 layout(local_size_z_id = 19) in; // constant_id for z 1393 1394 "Existing rules for declaring local_size_x, local_size_y, and 1395 local_size_z are not changed by this extension. For the local-size ids, 1396 it is a compile-time error to provide different id values for the same 1397 local-size id, or to provide them after any use. Otherwise, order, 1398 placement, number of statements, and replication do not cause errors. 1399 1400 "Two arrays sized with specialization constants are the same type only if 1401 sized with the same symbol, involving no operations. 1402 1403 layout(constant_id = 51) const int aSize = 20; 1404 const int pad = 2; 1405 const int total = aSize + pad; // specialization constant 1406 int a[total], b[total]; // a and b have the same type 1407 int c[22]; // different type than a or b 1408 int d[aSize + pad]; // different type than a, b, or c 1409 int e[aSize + 2]; // different type than a, b, c, or d 1410 1411 "Types containing arrays sized with a specialization constant cannot be 1412 compared, assigned as aggregates, declared with an initializer, or used 1413 as an initializer. They can, however, be passed as arguments to 1414 functions having formal parameters of the same type. 1415 1416 "Arrays inside a block may be sized with a specialization constant, but 1417 the block will have a static layout. Changing the specialized size will 1418 not re-layout the block. In the absence of explicit offsets, the layout 1419 will be based on the default size of the array." 1420 1421 Change section 4.4.5 Uniform and Shader Storage Block Layout Qualifiers 1422 1423 Add 1424 1425 "The 'shared' and 'packed' layout qualifiers are not available when 1426 generating SPIR-V." 1427 1428 Change 1429 1430 "The initial state of compilation is as if the following were declared:" 1431 1432 To 1433 1434 "The initial state of compilation when generating SPIR-V is as if the 1435 following were declared:" 1436 1437 layout(std140, column_major) uniform; 1438 layout(std430, column_major) buffer; 1439 1440 "The initial state of compilation when not generating SPIR-V is as if 1441 the following were declared:..." 1442 1443 Change 1444 1445 "It is a compile-time error to specify an offset that is smaller than 1446 the offset of the previous member in the block or that lies within the 1447 previous member of the block." 1448 1449 To 1450 1451 "It is a compile-time error to have any offset, explicit or assigned, 1452 that lies within another member of the block. When not generating 1453 SPIR-V, it is a compile-time error to specify an offset that is smaller 1454 than the offset of the previous member in the block." 1455 1456 Changes to Chapter 5 of the OpenGL Shading Language 4.50 Specification 1457 (Operators and Expressions) 1458 1459 Add a section at the end of section 5 1460 1461 "5.x Specialization Constant Operations" 1462 1463 Only some operations discussed in this section may be applied to a 1464 specialization constant and still yield a result that is as 1465 specialization constant. The operations allowed are listed below. 1466 When a specialization constant is operated on with one of these 1467 operators and with another constant or specialization constant, the 1468 result is implicitly a specialization constant. 1469 1470 - int(), uint(), and bool() constructors for type conversions 1471 from any of the following types to any of the following types: 1472 * int 1473 * uint 1474 * bool 1475 - vector versions of the above conversion constructors 1476 - allowed implicit conversions of the above 1477 - swizzles (e.g., foo.yx) 1478 - The following when applied to integer or unsigned integer types: 1479 * unary negative ( - ) 1480 * binary operations ( + , - , * , / , % ) 1481 * shift ( <<, >> ) 1482 * bitwise operations ( & , | , ^ ) 1483 - The following when applied to integer or unsigned integer scalar types: 1484 * comparison ( == , != , > , >= , < , <= ) 1485 - The following when applied to the Boolean scalar type: 1486 * not ( ! ) 1487 * logical operations ( && , || , ^^ ) 1488 * comparison ( == , != ) 1489 - The ternary operator ( ? : ) 1490 1491 Changes to Chapter 6 of the OpenGL Shading Language 4.50 Specification 1492 1493 Add at the beginning of section 6.1.2 Subroutines: 1494 1495 "Subroutine functionality is not available when generating SPIR-V." 1496 1497 Changes to Chapter 7 of the OpenGL Shading Language 4.50 Specification 1498 (Built-In Variables) 1499 1500 Add to the beginning of section 7.4 Built-In Uniform State: 1501 1502 "Built-in uniform state is not available when generating SPIR-V." 1503 1504 Section 8.14 "Noise Functions" 1505 1506 Add: "Noise functions are not present when generating SPIR-V." 1507 1508 Changes to Chapter 9 of the OpenGL Shading Language 4.50 Specification 1509 (Shading Language Grammar for Core Profile) 1510 1511 Arrays can no longer require the size to be a compile-time folded constant 1512 expression. Change 1513 1514 | LEFT_BRACKET constant_expression RIGHT_BRACKET 1515 1516 to 1517 1518 | LEFT_BRACKET conditional_expression RIGHT_BRACKET 1519 1520 and change 1521 1522 | array_specifier LEFT_BRACKET constant_expression RIGHT_BRACKET 1523 1524 to 1525 1526 | array_specifier LEFT_BRACKET conditional_expression RIGHT_BRACKET 1527 1528Changes to the SPIR-V specification 1529 1530 Section 3.20. Decoration 1531 1532 *Offset* can also apply to an object, for transform feedback. 1533 1534 *Offset* can also apply to an object in AtomicCounter storage for an 1535 atomic counter. 1536 1537Dependencies on ARB_parallel_shader_compile 1538 1539 If ARB_parallel_shader_compile is supported, the shader specialization 1540 may occur on a background thread in the same manner that compiling and 1541 linking does. 1542 1543Dependencies on ARB_separate_shader_objects 1544 1545 If ARB_separate_shader_objects is not supported, ignore all references 1546 to separable program objects. 1547 1548Dependencies on ARB_program_interface_query 1549 1550 If ARB_prorgram_interface_query is not supported, ignore references 1551 to commands added by this extension, however other commands defined 1552 in terms of these functions still operate as specified before the 1553 addition of the program interface query extension. 1554 1555New State 1556 1557Add the following to Table 23.30 "Shader Object State" 1558 1559Get Value Type Get Command Initial Value Description Sec 1560----------------- ----- ------------ ------------- --------------------- --- 1561SPIR_V_BINARY_ARB B GetShaderiv FALSE Shader is associated 7.2 1562 with a SPIR-V module. 1563 1564Usage Example 1565 1566 const uint* pSpirVModule; // This points to the SPIR-V code in memory 1567 uint spirVLength; // Length of pSpirVModule in bytes. 1568 1569 // Create the shader object. 1570 GLuint shader = glCreateShader(GL_FRAGMENT_SHADER); 1571 1572 // Load the SPIR-V module into the shader object 1573 glShaderBinary(1, &shader, 1574 GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, 1575 pSpirVModule, spirVLength); 1576 1577 // This will now return FALSE 1578 GLint status; 1579 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 1580 1581 // Specialize the shader 1582 const GLuint constantIndices[] = { 0, 7, 3 }; 1583 const GLuint constantValues[] = { 1, 42, 0x3F800000 }; 1584 glSpecializeShaderARB(shader, 1585 "main", 1586 3, 1587 constantIndices, 1588 constantValues); 1589 1590 // This should now return TRUE 1591 glGetShaderiv(shader, GL_COMPILE_STATUS, &status); 1592 1593 // Create a program, attach our shader to it, and link 1594 GLuint program = glCreateProgram(); 1595 1596 glAttachShader(program, shader); 1597 1598 glLinkProgram(program); 1599 1600 1601Issues 1602 16031. Do we need separate extensions to expose legacy GLSL and ESSL legacy 1604 capabilities in SPIR-V? 1605 1606 DISCUSSION: 1607 1608 When GLSL was created, multiple extensions were simultaneously introduced 1609 to separate the API mechanism (ARB_shader_objects), the shading language 1610 (GL_ARB_shading_language_100) and the shader stages (GL_ARB_vertex_shader, 1611 GL_ARB_fragment_shader). 1612 1613 We can expect that future versions of OpenGL will support SPIR-V 1614 as well but with a different set of capabilities. 1615 1616 Additionally, it's probably enough for the ARB to only define a SPIR-V 1617 capabilities for OpenGL 4.5 but SPIR-V is not only going to be consumed by 1618 OpenGL implementation but also by engine tool chains which can't afford to 1619 only support the last version of OpenGL. As a result Unity would define its 1620 own private extensions at least to clearly define the capability sets our 1621 engine supports at time T, e.g.: GL_UNITY_spirv_webgl, 1622 GL_UNITY_spirv_base_level, GL_UNITY_spirv_base_compute, 1623 GL_UNITY_spirv_max_level, etc to target older versions of OpenGL without 1624 being forced to use a specific GLSL version. 1625 1626 For example, GL_UNITY_spirv_base_compute has a feature subset between 1627 GLSL 420 + GL_ARB_shader_storage_buffer_object + GL_ARB_compute_shader + 1628 GL_ARB_shader_image_load_store and GLES 310 would correspond to what we 1629 call an OpenGL device level in Unity 5.1. 1630 1631 We should except that many engines would have the need of such ISV 1632 specific capability sets. This said it's important for the Khronos Group 1633 to set future convergence point but OpenGL 4.5 is not realistic today, which 1634 is the domain where ISVs must work. 1635 1636 Possible Resolution: 1637 Two extensions: 1638 - GL_ARB_program_binary_spirv to specify the mechanism. 1639 - GL_ARB_spir_v_gl_450 to specify the required SPIR-V capabilities for 1640 OpenGL 4.5. 1641 1642 Key data needed: What will IHVs actually support? 1643 1644 RESOLVED: 1645 1646 Minimum: 3.3 for desktop 1647 3.1 for ES [ however, ES is not covered by this extension ] 1648 1649 Need to add capabilities needed to distinguish between 3.3 and 4.5. 1650 1651 See bug 14520 for data on fine-grained capabilities and other 1652 relevant details. 1653 16542. Do we want to look at SPIR-V as a shading language or a binary format? 1655 1656 DISCUSSION: 1657 1658 Practically, this issue is about whether we want to load SPIR-V 1659 through the shader object API, the program binary API or do we need 1660 a new construct. Unfortunately, there is a lot of issues with the 1661 two first options and the third might be a little too complex. 1662 1663 Support through program binary API: 1664 - SPIR-V is the standard binary format 1665 - It makes SPIR-V a post LinkProgram construct 1666 - No location remapping is possible (glBindAttribLocation, 1667 glBindFragDataLocation[Indexed]) 1668 - Can't support multiple OpEntryPoint instructions 1669 - Can't provide program arguments (alternative to #pragma) 1670 - Can't retrieve SPIR-V source. GL_NUM_PROGRAM_BINARY_FORMATS can 1671 enumerate multiple format but glGetProgramBinary doesn't allow us to 1672 select the format we want to query back. 1673 - Currently SPIR-V doesn't support multiple shader stages per module 1674 but this would be useful for linked programs. 1675 1676 Support through shader object API: 1677 - OpenGL API allows to attach multiple shader objects but SPIR-V modules 1678 are fully contained: A single module define the entire stage. 1679 - SPIR-V modules don't have to support multiple stage per module but we 1680 lose the represent linked program and perform offline cross shader stage 1681 dead code elimination. 1682 - Need a new glCompileShader variant to support multiple entry-points 1683 (OpEntryPoint) or compilation arguments (OpCompileFlag) 1684 - glShaderSource doesn't allow to specify the shading language. Is looking 1685 at the first bytes to check the magic string okay? 1686 - Reflection may be striped from SPIR-V modules 1687 1688 Support through new API, "modules": 1689 // The difference between a module and a shader: 1690 // - Can contain multiple entry points 1691 // - Could specify the language, support both GLSL and SPIR-V (maybe) 1692 // - Can contain multiple shader stages 1693 // - Cross shader stage dead code elimination could be perform offline. (How to hint the compiler? OpCompileFlag?) 1694 // - Could be faster to compile shader variants? 1695 // - Programs created with glLinkProgramModule don't need GL_PROGRAM_SEPARABLE 1696 // - No need to specify the shader stage at object creation, it's embedded in the SPIR-V module 1697 1698 GLint length = 0; 1699 GLbyte* binary = LoadBinary(Path, &length); 1700 1701 GLuint ModuleName = 0; 1702 glCreateModules(1, &ModuleName); 1703 glCompileModule(ModuleName, GL_SHADING_LANGUAGE_SPIR_V, &binary, &length); 1704 1705 GLboolean HasReflection = GL_FALSE; 1706 glGetModuleiv(ModuleName, GL_MODULE_REFLECTION, &HasReflection); 1707 1708 GLuint ProgramName[2]; 1709 glCreatePrograms(2, &ProgramName[0]); 1710 1711 assert(HasReflection == GL_TRUE); 1712 GLchar const * Strings[] = {"gl_Position", "block.Color"}; 1713 glTransformFeedbackVaryings(ProgramName[0], 2, Strings, GL_INTERLEAVED_ATTRIBS); 1714 glBindAttribLocation(ProgramName[0], 0, "Position"); 1715 glBindAttribLocation(ProgramName[0], 1, "Color"); 1716 glBindFragDataLocation(ProgramName[0], 0, "Color"); 1717 glLinkProgramModule(ProgramName[0], ModuleName, "mainA", "-pragma optimize(on)"); 1718 1719 glLinkProgramModule(ProgramName[1], ModuleName, "mainB", "-pragma optimize(on)"); 1720 1721 Other notes: 1722 1723 - API doesn't require GLSL -> SPIR-V followed by getProgramBinary() 1724 * need an ID for specifying you want a SPIR-V binary? 1725 (christophe) GetProgramBinary doesn't do conversion, currently we would retrieve a SPIR-V 1726 is the program binary was a SPIR-V binary. However, that would prevent query a of native 1727 binary cache. I guess we could add a program parameter or specific that if we use 1728 GL_PROGRAM_BINARY_RETRIEVABLE_HINT GetProgramBinary return a native program binary cache, 1729 otherwise we get a SPIR-V binary back. 1730 1731 - LoadProgramBinary() needs a flag to link by SSO or by name? 1732 * or does API spec. just say this comes from using SSO mode or...? 1733 (christophe) GL_PROGRAM_SEPARABLE doesn't change linking by name or by location. 1734 In GLSL 450, when locations are present, we link by location. Otherwise, we link by name. 1735 GL_PROGRAM_SEPARABLE protects the shader code against a backward compatibility change regarding 1736 gl_PerVertex block. With separated shader stages, the block must be declared in the shader will 1737 before it was optional. This behavior was requested by AMD during the separated program 1738 ratification process. 1739 1740 Neither the program binary API or the shader object API 1741 seem to match SPIR-V features. Creating a new module API has potential but maybe 1742 we could build something around the shader object. 1743 1744 RESOLVED: Use the existing ShaderBinary() and a new entry point, 1745 SpecializeShaderARB() to set specialization constants. 1746 17473. Allow more than one stage per SPIR-V module? 1748 1749 DISCUSSION: 1750 1751 SPIR-V supports multiple entry points per module, of any stage, or any 1752 number of instances of the same stage. However, the knowledge of which 1753 of those might be used together lies outside the module. 1754 1755 OpenGL supports both linked programs and separated programs. If we 1756 consider strict separated programs with a program per shader stage, we can 1757 rely on a SPIR-V module per program. However, linked programs allows 1758 combining multiple shader stages into a single program. Furthermore, 1759 linked programs are more efficient than separated programs as the 1760 implementation can perform cross shader stages optimizations and packing of 1761 inputs and outputs with linked programs. 1762 1763 Allowing multiple shader stage in a module would profile a semantic for 1764 linked programs and would allow performing dead code elimination and input 1765 and output location packing offline. 1766 1767 RESOLVED: Allow full generality of what SPIR-V supports, and have 1768 the new entry points in the API select the subset of a module that it 1769 cares about. 1770 17714. Do we want to expose multiple entry points? 1772 1773 DISCUSSION: 1774 1775 SPIR-V supports multiple entry points through the OpEntryPoint 1776 instruction. Do we want to expose this? 1777 1778 A way to expose it would be to introduce a variant of glLinkProgram 1779 which takes the entry point main as an argument. 1780 1781 Typically, engines such as UE4 and Unity are rebuilding a big shared shader 1782 library over and over again for each shader variation. 1783 1784 While orthogonal to GL_ARB_program_instance, this feature would work 1785 extremely well with program instance for an application. 1786 1787 We could defer but I'll rather have all the SPIR-V interactions 1788 mechanisms defined in one extension from start to provide first class 1789 citizenship to SPIR-V. 1790 1791 RESOLVED: This is mostly a duplicate of Issue 3, and a single resolution 1792 for both is needed there in that one place. 1793 17945. Do we want to add support for "constantId" in OpenGL? 1795 1796 RESOLVED: Have the new entry points in the API support specializing 1797 constants, as in Vulkan. 1798 17996. Do we want to support multiple modules per program 1800 1801 DISCUSSION: 1802 1803 A use case could be to compile subroutines into different modules 1804 and link a set of subroutines we want to use into a program canvas. 1805 In another use case, using multiple entry points we could build multiple 1806 code paths sharing the same shader library. 1807 1808 SPIR-V for Vulkan currently expects fully linked stages. 1809 1810 This could be part of a much needed rework or the subroutines. 1811 Okay to defer for now. 1812 1813 RESOLVED: Fully linked modules only, no direct "subroutine" support. 1814 18157. Do we want to be able to provide compilation arguments? 1816 1817 DISCUSSION: 1818 1819 SPIR-V supports the OpCompileFlag instructions to store 1820 compilation arguments however OpenGL doesn't support compilation 1821 arguments. If we add a new variant of glLinkProgram, it could be an 1822 opportunity to add a compilation arguments parameter to glLinkProgram. 1823 1824 Some use cases: 1825 - Compiling the shaders with different optimizations: It's currently 1826 supported with #pragma but SPIR-V doesn't contain preprocessor 1827 information 1828 - Vendors refused to fix GLSL bugs and there are a lot of them. The 1829 reason is that fixing these bugs could cause to break shader compilation 1830 hence applications. If we want to start a virtue circle, we need to 1831 give developers the opportunity to enforce conformant compilation. 1832 - Display on or off warnings in the compilation log. 1833 - All the cool stuff C++ compilers are using compilation arguments for. 1834 1835 Note that the instruction OpCompileFlag was removed from SPIR-V, and 1836 a Bug 13418 "Need a way to control target-compiler behavior from the API" 1837 was deferred to a future release. 1838 1839 RESOLVED: No flags in first release. 1840 18418. Do we want to build a reflection API on SPIR-V modules specifics? 1842 1843 - Retrieve compilation arguments 1844 - Retrieve shader stages included 1845 - Retrieve the list of entry points 1846 - Retrieve the list of required capabilities 1847 1848 Arguably, it's trivial to build a SPIR-V parser and figure this out 1849 ourselves. 1850 1851 DISCUSSION: 1852 1853 If drivers implement SPIR-V support by lowering it to the same 1854 representation that GLSL is lowered to, and that representation is the 1855 source of reflection information, much reflection would naturally be 1856 present. 1857 1858 RESOLVED: First start without reflection, then add later if needed. 1859 In the meantime, we have to document how the existing API operates 1860 with no reflection information. See Issue 22. 1861 18629. Separate programs require gl_PerVertex output blocks to be declared. 1863 How do we enforce this requirement in SPIR-V modules? 1864 1865 DISCUSSION: Force compiler to generate a gl_PerVertex block if the 1866 shader code writes vertex-shader output built-ins? 1867 1868 RESOLVED: (In the validation rules): SPIR-V should contain the 1869 same form of input/output block as expected by recent versions of GLSL. 1870 All in/out built-in show up in SPIR-V in blocks, as per Vulkan rules. 1871 GLSL can stay the same, front ends need to block non-blocked built-ins. 1872 187310. Do we want to support glBindAttribLocation and glBindFragDataLocation[Indexed] 1874 with SPIR-V? Also, what about glTransformFeedbackVaryings? 1875 1876 DISCUSSION: Locations can be explicitly put into the shader, and 1877 then the application uses the right location. 1878 1879 Note that SPIR-V 1.0 does not allow using specialization constants for 1880 1881 layout(location=X) ... // X must be a literal, not specialized 1882 1883 Alternatively, add a SPIR-V instruction that assigns a string name to an 1884 input which the API can use to link with. This would initially be an 1885 extension to SPIR-V as part of this. 1886 1887 RESOLVED: Don't support any of glBindAttribLocation, 1888 glBindFragDataLocation[Index], or glTransformFeedbackVaryings. 1889 189011. What capabilities do we need for GL 4.5? 1891 1892 RESOLVED: There is a Appendix A.spv for this. 1893 189412. How is user-linkage done between stages? 1895 1896 DISCUSSION: SPIR-V linkage is by location number and BuiltIn decoration. 1897 Does OpenGL need linkage by name? Is SSO required to work with SPIR-V? 1898 Require OpName can't be stripped out for linkage objects. If we use the 1899 program binary API, yes but this is not desirable. See issue 2. 1900 1901 RESOLVED: Link the Vulkan way: built-in decorations, location numbers, 1902 etc., never by name. No reflection required. This is a lot like SSO. 1903 190413. Can we really handle separate textures and samplers? 1905 1906 DISCUSSION: AMD: No, can't do this because OpenGL has features that need 1907 cross validation that can't be done. 1908 1909 RESOLVED: Remove separate textures and samplers. 1910 191114. Are there differences in binding counting between Vulkan and OpenGL? 1912 1913 DISCUSSION: Yes, OpenGL uses multiple binding points for a resource array 1914 while Vulkan uses just one. 1915 1916 RESOLVED: This leaves OpenGL as is, but state in the overview what this 1917 difference is. 1918 191915. What GL version(s) should be required for the ImageQuery OpCapability? 1920 1921 DISCUSSION: The GL features it corresponds with are: 1922 - textureSize - EXT_gpu_shader4 (GL 3.0) 1923 - textureQueryLod - ARB_texture_query_lod (GL 4.0) 1924 - textureQueryLevels - ARB_texture_query_levels (GL 4.3) 1925 - imageSize - ARB_shader_image_size (GL 4.3) 1926 - textureSamples, imageSamples - ARB_shader_texture_image_samples (GL 4.5) 1927 The belief is that these are largely software features and while some 1928 of them were not added until later API versions, it was not because of 1929 hardware reasons. 1930 1931 RESOLVED: Require ImageQuery to be supported for all versions of GL, 1932 as it is required for all Vulkan implementations. 1933 193416. At what point should an error due to an invalid SPIR-V module/capability 1935 be reported? ShaderBinary, SpecializeShaderARB, LinkProgram time? 1936 ShaderBinary says a "valid SPIR-V module binary" is required, but you 1937 can have a valid module that uses capabilities or extensions not 1938 supported by an implementation. 1939 1940 RESOLVED. ShaderBinary is expected to form an association between the 1941 SPIR-V module and likely would not parse the module as would be required 1942 to detect unsupported capabilities or other validation failures. In order 1943 to avoid requiring the implementation to parse the module multiples times, 1944 we allow this analysis to happen at either SpecializeShaderARB or 1945 LinkProgram time, similar to how many errors for source shaders are 1946 detected at either compile or link time. 1947 194817. Should we allow program objects to contain both source shaders and 1949 SPIR-V binary shaders, or should this be a link time failure? 1950 1951 RESOLVED. No. This would be needlessly complex to specify. Make this 1952 a link-time error. This can be determined by examining the 1953 SPIR_V_BINARY_ARB state of each shader. They must either: all be TRUE 1954 (SPIR-V shader) or all be FALSE (source shaders). If an application 1955 really wishes to mix source and SPIR-V binary shaders, this can be 1956 done at program object boundaries by using separable program objects 1957 (if supported). 1958 195918. Do we need a section for "SPIR-V Transform Feedback Interface"? 1960 This would discuss any requirements for the Xfb related decorations 1961 in SPIR-V. 1962 1963 RESOLVED. Yes. 1964 196519. How should the program interface query operations behave for program 1966 objects created from SPIR-V shaders? 1967 1968 DISCUSSION: we previously said we didn't need reflection to work 1969 for SPIR-V shaders (at least for the first version), however we 1970 are left with specifying how it should "not work". The primary issue 1971 is that SPIR-V binaries are not required to have names associated 1972 with variables. They can be associated in debug information, but there 1973 is no requirement for that to be present, and it should not be relied 1974 upon. 1975 1976 Options: 1977 A) Make it work. If debug names are present they are enumerated 1978 correctly with associated variables. If the names aren't present, 1979 the compiler or driver gets to make them up. Alternatively, we could 1980 augment SPIR-V to have a mode where all interface variables need to 1981 have names which must not be stripped. 1982 1983 B) Completely disallow it. All/Most such operations return an error. 1984 This may result in some implementation-dependent behavior which 1985 is impossible to know (although this problem may exist anyhow due 1986 to the offline-compilation step). This would likely include not 1987 being able to tell how many active resources there are and related 1988 problems. 1989 1990 C) Allow as much as possible to work "naturally". You can query for 1991 the number of active resources, and for details about them. Anything 1992 that doesn't query by name will work as expected. Queries for maximum 1993 length of names return one. Queries for anything "by name" return 1994 INVALID_INDEX (or -1). Querying the name property of a resource 1995 returns an empty string. This may allow many queries to work, but it's 1996 not clear how useful it would be if you can't actually know which 1997 specific variable you are retrieving information on. If everything 1998 is specified a-priori by location/binding/offset/index/component 1999 in the shader, this may be sufficient. 2000 2001 RESOLVED. Pick (c), but also allow debug names to be returned if an 2002 implementation wants to. 2003 200420. How should we deal with implementation-dependent behavior that 2005 must normally be queried after linking? Examples include: 2006 - set of active resources 2007 - offsets and strides of GLSLShared and GLSLPacked UBO/SSBOs 2008 - MATRIX_STRIDE, UNIFORM_ARRAY_STRIDE for UBOs (only relevant for 2009 packed/shared?) 2010 - UNIFORM_ARRAY_STRIDE for arrays of atomic counter buffers. 2011 2012 DISCUSSION: 2013 - Option (c) from issue 19 allows determination of active resources (by 2014 shader specified layouts, but not name). 2015 - GLSLShared and GLSLPacked should not be allowed in this 2016 extension as there is no way to sensibly support, short of Option (a) 2017 from Issue 19. 2018 - For arrays of atomic counters, Option (c) from Issue 19 may be enough 2019 to figure this out, but I'm not sure that will work for offline 2020 compilation. Do we need to define a standard "layout" (stride) for 2021 arrays of atomic counters? 2022 2023 RESOLVED: 2024 Picked (c) in issue 19, allowing determination of the number and types 2025 of active resources. 2026 Remove the shared and packed layouts and have the same behavior as in 2027 Vulkan. 2028 Atomic counter buffers don't have an associated name string already so 2029 there should be no issue with figuring out the UNIFORM_ARRAY_STRIDE for 2030 them. 2031 203221. What do we need to say about various linking rules related to "named 2033 uniform blocks" and "named shader storage blocks"? 2034 2035 RESOLVED. We don't need to say anything, as they won't have 2036 names in SPIR-V shaders, so they aren't really "named". Instead of 2037 being identified by name they are identified (and matched) by 2038 uniform block index and/or binding. 2039 204022. How do the program interface query APIs work when no name reflection 2041 information is available? 2042 2043 RESOLVED: The following naturally follows from the specification: 2044 2045 GetProgramInterfaceiv(.., pname=MAX_NAME_LENGTH, ..) -> params = 1 2046 GetProgramResourceIndex(.., name) -> INVALID_INDEX 2047 GetProgramResourceName(.., length, name) -> length=0, name = "" 2048 GetProgramResourceiv(.., props=NAME_LENGTH, ..) -> params = 1 2049 GetProgramResourceLocation(.., name) -> -1 2050 GetProgramResourceLocationIndex(.., name) -> -1 2051 GetUniformLocation(.., name) -> -1 2052 GetActiveUniformName(.., length, uniformName) -> length=0, uniformName = "" 2053 GetUniformIndices(..,uniformNames, uniformIndices) -> uniformIndices=INVALID_INDEX 2054 GetActiveUniform(..,length,.., name) -> length = 0, name = "" 2055 GetActiveUniformsiv(..,pname=UNIFORM_NAME_LENGTH,..) -> params = 1 2056 GetUniformBlockIndex(.., uniformBlockName) -> INVALID_INDEX 2057 GetActiveUniformBlockName(..,length,uniformBlockName) -> length=0, uniformBlockName="" 2058 GetActiveUniformBlockiv(.., pname=UNIFORM_BLOCK_NAME_LENGTH, ..) -> params = 1 2059 GetActiveAttrib(..) -> length = 0, name = "" 2060 GetAttribLocation(.., name) -> -1 2061 GetTransformFeedbackVarying(..) -> length = 0, name = "" 2062 GetFragDatatLocation(.., name) -> -1 2063 GetFragDataIndex(.., name) -> -1 2064 GetProgramiv(.., pname=ACTIVE_ATTRIBUTE_MAX_LENGTH, ..) -> params = 1 2065 GetProgramiv(.., pname=ACTIVE_UNIFORM_MAX_LENGTH, ..) -> params = 1 2066 GetProgramiv(.., pname=TRANSFORM_FEEDBACK_VARYING_MAX_LENGTH, ..) -> params = 1 2067 GetProgramiv(.., pname=ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH, ..) -> params = 1 2068 206923. How does setting uniforms work if we can't query for locations based on 2070 names? 2071 2072 RESOLVED. The shaders need to specify explicit locations for all uniform 2073 variables. The application can use offline reflection (or a reflection 2074 library) to know which variables are at which location, and then 2075 use that to specify the values of uniforms by location, as normal 2076 (e.g. Uniform*, ProgramUniform*). 2077 207824. What about UniformBlockBinding and ShaderStorageBlockBinding? 2079 2080 RESOLVED. Because it is not possible to obtain the uniform block index 2081 or storage block index from an unnamed block, the binding value remains 2082 as specified in the shader by the layout qualifier or decoration. 2083 If you are feeling lucky you could just guess and remap used index values 2084 [0, #active blocks), but you won't really know what is going 2085 where, so just treat it as an immutable binding, similar to the atomic 2086 counter buffer binding point. Really. 2087 208825. What about subroutine queries based on names? 2089 2090 RESOLVED. SPIR-V does not currently support subroutines, so it is not 2091 possibly to have any active subroutines from a SPIR-V based shader, 2092 and thus there is never anything to report. 2093 209426. How do we change the location of samplers and images? 2095 2096 RESOLVED. You don't. Well you can using Uniform1i as usual, but you have 2097 no way of knowing which location corresponds to which sampler or image 2098 variable as GetUniformLocation won't work without a named variable. Best 2099 to just treat them as immutable bindings which are specified in the 2100 shader source or binary. 2101 210227. Appendix A.spv.3 gives a list of required capabilities that correspond 2103 to the core GL 4.5 features as well as for *some* of the ARB extensions 2104 that require shading language support. What about the rest of the 2105 ARB and vendor extensions that require shading language support? 2106 2107 RESOLVED: Extensions that can be represented in terms of core 2108 SPIR-V 1.0 functionality don't need any specific consideration. 2109 Other ARB or vendor extensions that require SPIR-V functionality that 2110 can't be expressed in terms of SPIR-V 1.0 functionality will need to 2111 have SPIR-V extensions defined to add the required functionality. 2112 Further GL extensions can be defined to advertise support for 2113 consuming such SPIR-V capabilities once they have been defined. 2114 2115 A (partial) list of extensions that are expected to need some form 2116 of modification to SPIR-V follows. 2117 2118 ARB_shader_viewport_layer_array 2119 - need to allow ViewportIndex and Layer to be output by VS 2120 and TES. 2121 ARB_shader_clock 2122 - need clock() builtin function 2123 ARB_shader_ballot 2124 - use subgroup capability and add other builtins 2125 ARB_shader_atomic_counter_ops 2126 - need atomic counter builtin functions 2127 ARB_post_depth_coverage 2128 - need post_depth_coverage layout 2129 ARB_fragment_shader_interlock 2130 - need new layouts and interlock builtin functions 2131 KHR_blend_equation_advanced 2132 - need new layouts 2133 ARB_shader_group_vote 2134 - need new builtin functions 2135 ARB_shader_draw_parameters 2136 - need new builtin variables 2137 ARB_bindless_texture 2138 - need new layout qualifiers and probably other semantics 2139 ARB_compute_variable_group_size 2140 - need new layout qualifiers and builtin variables 2141 214228. Should a SPIR-V binary linked through this extension work with 2143 ARB_get_program_binary? Do we need to define what would happen 2144 with specialization constants? 2145 2146 RESOLVED. Yes it should work seamlessly, and no, it shouldn't be 2147 required to add more details on how to store specialization 2148 constants. It is expected that GetProgramBinary should operate 2149 irrespective of how the program was created. 2150 2151 (from GL 4.6 spec section 7.5, Program Binaries): 2152 2153 GetProgramBinary returns a binary representation of the 2154 program object's compiled and linked executable source, 2155 henceforth referred to as its program binary. 2156 2157 There are two ways you can end up with a linked executable. 2158 2159 GLSL sources: 2160 ShaderSource() -> CompileShader() -> AttachShader() -> LinkProgram() 2161 SPIR-V binaries: 2162 ShaderBinary() -> SpecializeShader() -> AttachShader() -> LinkProgram() 2163 2164 Exactly what is stored in a program binary is not defined by the 2165 GL spec. 2166 2167 If the driver is storing the final compiled machine assembly for 2168 the program in the program binary, it clearly shouldn't matter 2169 which path it takes to get to the LinkProgram step. 2170 2171 If instead the driver is storing some higher level representation 2172 of the shaders (say the original sources, or some IR) then it's up 2173 to the implementation to store whatever it needs to reconstitute 2174 the linked binary. If the given implementation happened to choose 2175 to store the SPIR-V code then it would also need to store any 2176 relevant specialization information with it as well. 2177 2178 2179Revision History 2180 2181 Rev. Date Author Changes 2182 ---- ----------- ------------ --------------------------------- 2183 46 19-Aug-2020 dgkoch Atomic pointers must be 32-bit or 2184 64-bit scalar integers (SPIR-V/254, 2185 internal API issue 132) 2186 45 17-Sep-2019 Jon Leech Add OpTypeBool constraint on user 2187 interface variables, and require the 2188 StorageImageWriteWithoutFormat 2189 capability for OpImageWrite to 2190 Unknown formats in the Validation 2191 Rules section (internal API issues 2192 111 and 112, respectively). 2193 44 15-Feb-2019 apinheiro Added issue 28, about interaction with 2194 ARB_get_program_binary (internal API issue 100) 2195 43 15-Feb-2019 apinheiro Add uniform initializers reference and 2196 mapping (internal API issue 99) 2197 42 9-Jan-2019 JohnK Explicitly give rules for SPIR-V 2198 uniform offsets (internal API issue 2199 92) 2200 41 10-Dec-2018 Jon Leech Use 'workgroup' consistently 2201 throughout (Bug 11723, internal API 2202 issue 87). 2203 40 29-May-2018 dgkoch note post decrement for atomicCounterDecrement 2204 mapping 2205 39 25-Apr-2018 JohnK add mappings of barriers and atomics 2206 38 10-Apr-2018 dgkoch OpImageQuerySizeLod and OpImageQuerylevels 2207 only work with Sampled images 2208 (SPIR-V/issues/280). 2209 37 20-Feb-2018 dgkoch Add whitelist for accepted storage 2210 classes (SPIR-V/issues/166). 2211 Require Binding for uniform and storage 2212 buffer blocks (opengl/api/issues/55). 2213 36 15-Nov-2017 dgkoch clarify error for glSpecializeShader 2214 and add new error if shader types 2215 mismatch (OpenGL-API/issues/16) 2216 35 25-Oct-2017 JohnK remove the already deprecated noise 2217 functions 2218 34 29-May-2017 dgkoch Fix typos. RuntimeArrays are only 2219 supported on SSBOs. 2220 33 26-May-2017 JohnK Require in/out explicit locations 2221 32 25-Apr-2017 JohnK Bring up-to-date: 2222 Out-of-order block offsets 2223 gl_NumSamples not supported 2224 atomic counter restrictions 2225 bug fixes 2226 31 21-Jul-2016 dgkoch Clarify string length on queries 2227 30 13-Jul-2016 JohnK SPIR-V Offset can also apply to an 2228 atomic_uint offset. 2229 29 04-Jul-2015 dgkoch Allow handles of the same shader types 2230 for ShaderBinary when using SPIRV. 2231 28 09-Jun-2016 dgkoch Move future extensions to Issue 27. 2232 27 08-Jun-2016 dgkoch Assign enums, add a couple missing 2233 errors. Editorial fixes. 2234 Specify required version and format 2235 of SPIRV. Add Issue 27. 2236 26 02-Jun-2016 JohnK Completion of draft. Pack/unpack, TBD, 2237 and resolutions. 2238 25 02-Jun-2016 JohnK Include XFB linking/interface rules 2239 24 02-Jun-2016 JohnK Remove use of GLSL shared/packed 2240 and SPIR-V GLSLShared/GLSLPacked, 2241 bringing in KHR_vulkan_glsl rules for 2242 std140 and std430 2243 23 01-Jun-2016 dgkoch Finish API edits. Cleanup editing 2244 pass on formatting and issue resolutions. 2245 Add issues 22-26 and resolve the rest. 2246 22 26-May-2016 dgkoch Add ARB suffix to SpecializeShader 2247 Add SPIR_V_BINARY_ARB shader state 2248 (and some but not all related rules) 2249 Add a bunch of API edits alluding to 2250 SPIR-V shaders. Lots of comments about 2251 outstanding API areas that still need 2252 to be addressed. 2253 Add unresolved issues 16-21. 2254 21 25-May-2016 JohnK Add interface matching rules 2255 20 19-May-2016 dgkoch Remove language about 'default entry point' 2256 Recast features in terms of GL version. 2257 19 19-May-2016 dgkoch Add min GLSL version required for 2258 built-in variable decorations and 2259 OpCapabilities. Fix various dates. 2260 18 13-May-2016 JohnK Bring in the actual correct subset of 2261 GL_KHR_vulkan_glsl, rather than refer 2262 to it with differences 2263 17 12-May-2016 dgkoch Verify capabilities for GLSL 4.50 2264 Add capabilities for non-core ARB 2265 extensions, and extensions that 2266 already have SPIR-V correspondence. 2267 16 12-May-2016 dgkoch grammatical fixes, replace non-ascii 2268 chars, formatting 2269 15 11-May-2016 JohnK Clear up misc. TBDs throughout 2270 14 11-May-2016 JohnK Flesh out GL_KHR_vulkan_glsl changes 2271 13 11-May-2016 JohnK Move to final organization to flesh out 2272 12 10-May-2016 JohnK Add the Vulkan validation rules that 2273 apply and the TBD from the f2f 2274 11 21-Apr-2016 JohnK Editorial update to API description 2275 10 11-Feb-2016 gsellers Add prototype API language. 2276 9 31-Jan-2016 JohnK Issues 13 & 14 2277 8 04-Dec-2015 JohnK Resolve issue 10 2278 7 05-Nov-2015 JohnK Remove gl_FragColor, update issue 10 2279 6 22-Oct-2015 JohnK Resolutions from Houston 2280 5 21-Oct-2015 JohnK Make into a consistent format 2281 4 16-Oct-2015 JohnK Added dependencies with GL_KHR_vulkan_glsl 2282 3 08-Oct-2015 JohnK Added exec. env. detail, updated issues 2283 2 22-Apr-2015 Christophe Added issues 2284 1 26-Mar-2015 JohnK Initial revision 2285