Path: blob/main/docs/EMSCRIPTEN_explicit_uniform_location.txt
4128 views
Name12EMSCRIPTEN_explicit_uniform_location34Name Strings56GL_EMSCRIPTEN_explicit_uniform_location78Contributors910Jukka 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.17 and newer for OpenGL ES 2 and21OpenGL ES 3 contexts. (March 2021)2223Version2425Date: March 21, 202126Revision: 12728Dependencies2930Emscripten 2.0.17 compiler targeting OpenGL ES 2.0 or OpenGL ES 3.031versions.3233Written against the OpenGL ES 2.0.25 and WebGL 1 specifications.3435Overview3637WebGL API utilizes opaque WebGLUniformLocation interface objects to38represent locations of uniforms in shader programs. When translating39the WebGL API to be utilized from WebAssembly applications, these40interface objects must be tabulated to an array of integer-based locations41since WebAssembly programs are unable to hold references to JavaScript42types.4344This integer mapping matches the native OpenGL and OpenGL ES utilization45of integers to represent locations of shader uniforms.4647Further, in native OpenGL and OpenGL ES programs, applications may48perform integer arithmetic on the shader uniform locations to compute49index locations of other shader uniforms, subject to the packing rules50of the shader variables for arrays and structs.5152For example, an array of uniforms "vec4 colors[3];" would consist of53three consecutive uniform index integers in the compiler shader program.54An application may call glGetUniformLocation() for uniform "colors[0]",55and then use integer arithmetic in C/C++ program code to upload data for56uniforms "colors[1]" and "colors[2]" individually, or choose to upload57a sub-range of the array collectively.5859The Emscripten WebGL runtime library ensures that this kind of uniform60location integer arithmetic is supported, even though WebGL61implementation itself in browsers has no concept of this, due to the62opaque WebGLUniformLocation objects being employed.6364In native OpenGL and OpenGL ES applications, a programmer may choose65to specify the integer location of a shader uniform in advance directly66in the submitted shader code. The GLSL directive6768layout(location = x) uniform mat4 world;6970specifies that the given uniform should be accessible via location 'x',71where x is an integer in the range [0, GL_MAX_UNIFORM_LOCATIONS-1].7273Since WebGL specification does not recognize uniform locations as74integers, the notion of layout location qualifiers do not apply to WebGL.75However for WebAssembly-based applications they do, and can provide a76meaningful way for applications to pre-layout the uniforms in a shader to77simplify program structure.7879This extension adds support for specifying layout location qualifiers in80GLSL shaders in OpenGL and OpenGL ES programs compiled via Emscripten.8182The main benefit of layout location qualifiers in shaders is to enable83aligning shader uniforms across multiple compiled shader programs. For84example, if several compiler programs had a common shader uniform variable85"vec4 fogColor", without explicit location qualifiers, C/C++ code would86have to manage a mapping table that would track the uniform location of87fogColor in each shader program. With explicit uniform locations, all shaders88can be compiled to locate fogColor in the same location index, thus greatly89simplifying C/C++ engine code.9091In sibling specifications, support for explicit uniform locations exist in92OpenGL ES 3.1 and Desktop OpenGL 4.3 core specifications. Additionally,93Desktop OpenGL 3.3 and newer may support the ARB_explicit_uniform_location94extension that also offers the same functionality as this extension95EMSCRIPTEN_explicit_uniform_location does.9697New Procedures and Functions9899None.100101New Tokens102103GL_MAX_UNIFORM_LOCATIONS 0x826E104105Additions to Chapter 6 of the OpenGL 2.0 Specification (State and State Requests)106107-- Section 6.1.1, Simple Queries108109In OpenGL ES 2.0 and OpenGL ES 3.0 contexts, the GetIntegerv, entry110point parameter 'value' accepts the token GL_MAX_UNIFORM_LOCATIONS, which111returns the upper limit, exclusive, that can be specified for integer x112in the GLSL uniform variable qualifier "layout(location = x)".113114The value returned by GL_MAX_UNIFORM_LOCATIONS must be at least 1024.115116Additions to OpenGL ES Shading Language 1.00 Specification117118The qualifier "layout(location = x)" can be prepended to a uniform119variable directive to bind the integer mapping location for the given120uniform throughout the lifetime of the shader program:121122layout(location = x) uniform mat4 world;123124Additions to Emscripten compiler125126Instead of utilizing the runtime method WebGLRenderingContext.getExtension()127to coordinate enabling the extension, EMSCRIPTEN_explicit_uniform_location128activation is controlled at compile time by specifying the flag129130-sGL_EXPLICIT_UNIFORM_LOCATION131132to 'emcc' or 'em++' linker command line in the Emscripten compiler.133134When this flag is passed, the extension EMSCRIPTEN_explicit_uniform_location135is enabled for all contexts created in the application.136137The choice of compile time activation is due to the considerable increase in138code size that is involved in enabling this extension.139140Revision History141142Revision 1.0, March 21, 2021: juj143- First version144145146