Path: blob/main/docs/EMSCRIPTEN_explicit_uniform_binding.txt
4128 views
Name12EMSCRIPTEN_explicit_uniform_binding34Name Strings56GL_EMSCRIPTEN_explicit_uniform_binding78Contributors910Jukka Jylänki, Unity Technologies11Brendan Duncan, Unity Technologies1213Contact1415Jukka Jylänki, Unity Technologies (jukkaj 'at' unity3d.com)16Brendan Duncan, Unity Technologies (brendan.duncan 'at' unity3d.com)1718Status1920Implemented in Emscripten 2.0.18 and newer for OpenGL ES 2 and21OpenGL ES 3 contexts. (April 2021)2223Version2425Date: April 13, 202126Revision: 12728Dependencies2930Emscripten 2.0.18 compiler targeting OpenGL ES 2.0 or OpenGL ES 3.031versions.3233Written against the OpenGL ES 2.0.25 and WebGL 1 specifications.3435Overview3637WebGL 1 API provides a special type of uniforms called texture samplers.38The organization of these samplers is designed around the concept of39a file of device global multitexturing binding points. This provides a40layer of indirection: instead of directly connecting texture objects to41sampler uniforms in the shader, textures are set active in multitexturing42binding points, and samplers uniforms then each select a binding point to43sample from.4445WebGL 2 API provides support for Uniform Buffer Objects (UBOs). Again,46instead of directly connecting a GPU buffer to a uniform block in the47shader, each UBO variable is connected to a slot in a file of uniform48block binding points, and the uniform block variables are assigned to49read from one of these uniform block binding points.5051Sampler uniforms are assigned a multitexturing unit by calling the52function gl.uniform1i(). Uniform blocks are assigned a uniform block53binding point by calling the function gl.uniformBlockBinding().5455By default after a successful shader link, all samplers and uniform blocks56are initially assigned the binding point zero. Instead of programmatically57reassigning the binding points in Wasm/JS code, it can be useful to specify58the default binding point to use in GLSL shader code. This allows a shader59compilation backend to generate multiple shader programs that have mutually60compatible layouts, without needing further coordination in application61code.6263This extension adds support for specifying the initial multitexturing and64uniform block binding points for texture samplers and uniform blocks. This65is achieved via the same syntax that is available in Desktop OpenGL 4.2 and66the extension ARB_shading_language_420pack. There, a programmer may choose67to specify the integer binding point of a texture sampler or a uniform68block in advance directly in the submitted shader code. The GLSL directive6970layout(binding = x) uniform sampler2D diffuse;7172specifies that the sampler 'diffuse' should sample a texture located in73multitexturing unit 'x'.7475Likewise, specifying7677layout(binding = x) uniform myBlock { vec4 color; }7879specifies that the uniform block 'myBlock' should read from the Uniform80Buffer Object located at uniform block binding point 'x'.8182In sibling specifications, support for explicit binding points exist in83Desktop OpenGL 4.2 core specification and in the extension84ARB_shading_language_420pack.8586New Procedures and Functions8788None.8990New Tokens9192None.9394Additions to OpenGL ES Shading Language 1.00 Specification9596Add to sampler qualifiers:9798The qualifier "layout(binding = x)" can be prepended to a texture sampler99variable directive to bind the integer multitexturing unit that the given100sampler reads from:101102layout(binding = x) uniform sampler2D diffuse;103104Only one layout qualifier may appear in a single declaration.105106Any sampler declared without a binding identifier is initially assigned to107sample from multitexturing unit zero. After a program is linked, the binding108points used for samplers with or without a binding identifier can be updated109by the OpenGL API.110111If the binding identifier is used with an array, the first element of the112array takes the specified unit and each subsequent element takes the next113consecutive unit.114115If the binding is less than zero, or greater than or equal to the116implementation-dependent maximum supported number of units, a compilation117error will occur. When the binding identifier is used with an array of size118N, all elements of the array from binding through binding + N - 1 must be119within this range.120121Additions to OpenGL ES Shading Language 3.00 Specification122123Add to Layout Qualifiers:124125The qualifier "layout(binding = x)" can be prepended to a uniform block126variable directive:127128layout(binding = x) uniform myBlock { vec4 color; }129130The binding identifier specifies the uniform buffer binding point131corresponding to the uniform block, which will be used to obtain the132values of the member variables of the block.133134Only one layout qualifier may appear in a single declaration.135136Any uniform block declared without a binding identifier is initially137assigned to block binding point zero. After a program is linked, the138binding points used for uniform blocks declared with or without a binding139identifier can be updated by the OpenGL API.140141If the binding identifier is used with a uniform block instanced as an142array then the first element of the array takes the specified block binding143and each subsequent element takes the next consecutive uniform block144binding point.145146If the binding point for any uniform block instance is less than zero, or147greater than or equal to the implementation-dependent maximum number of148uniform buffer bindings, a compilation error will occur. When the binding149identifier is used with a uniform block instanced as an array of size N,150all elements of the array from binding through binding + N - 1 must be151within this range.152153Additions to Emscripten compiler154155Instead of utilizing the runtime method WebGLRenderingContext.getExtension()156to coordinate enabling the extension, EMSCRIPTEN_explicit_uniform_binding157activation is controlled at compile time by specifying the flag158159-sGL_EXPLICIT_UNIFORM_BINDING160161to 'emcc' or 'em++' linker command line in the Emscripten compiler.162163When this flag is passed, the extension EMSCRIPTEN_explicit_uniform_binding164is enabled for all contexts created in the application.165166The choice of compile time activation is due to the considerable increase in167code size that is involved in enabling this extension.168169Revision History170171Revision 1.0, April 13, 2021: juj172- First version173174175