Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/docs/EMSCRIPTEN_explicit_uniform_location.txt
4128 views
1
Name
2
3
EMSCRIPTEN_explicit_uniform_location
4
5
Name Strings
6
7
GL_EMSCRIPTEN_explicit_uniform_location
8
9
Contributors
10
11
Jukka Jylänki, Unity Technologies
12
Brendan Duncan, Unity Technologies
13
14
Contact
15
16
Jukka Jylänki, Unity Technologies (jukkaj 'at' unity3d.com)
17
Brendan Duncan, Unity Technologies (brendan.duncan 'at' unity3d.com)
18
19
Status
20
21
Implemented in Emscripten 2.0.17 and newer for OpenGL ES 2 and
22
OpenGL ES 3 contexts. (March 2021)
23
24
Version
25
26
Date: March 21, 2021
27
Revision: 1
28
29
Dependencies
30
31
Emscripten 2.0.17 compiler targeting OpenGL ES 2.0 or OpenGL ES 3.0
32
versions.
33
34
Written against the OpenGL ES 2.0.25 and WebGL 1 specifications.
35
36
Overview
37
38
WebGL API utilizes opaque WebGLUniformLocation interface objects to
39
represent locations of uniforms in shader programs. When translating
40
the WebGL API to be utilized from WebAssembly applications, these
41
interface objects must be tabulated to an array of integer-based locations
42
since WebAssembly programs are unable to hold references to JavaScript
43
types.
44
45
This integer mapping matches the native OpenGL and OpenGL ES utilization
46
of integers to represent locations of shader uniforms.
47
48
Further, in native OpenGL and OpenGL ES programs, applications may
49
perform integer arithmetic on the shader uniform locations to compute
50
index locations of other shader uniforms, subject to the packing rules
51
of the shader variables for arrays and structs.
52
53
For example, an array of uniforms "vec4 colors[3];" would consist of
54
three consecutive uniform index integers in the compiler shader program.
55
An application may call glGetUniformLocation() for uniform "colors[0]",
56
and then use integer arithmetic in C/C++ program code to upload data for
57
uniforms "colors[1]" and "colors[2]" individually, or choose to upload
58
a sub-range of the array collectively.
59
60
The Emscripten WebGL runtime library ensures that this kind of uniform
61
location integer arithmetic is supported, even though WebGL
62
implementation itself in browsers has no concept of this, due to the
63
opaque WebGLUniformLocation objects being employed.
64
65
In native OpenGL and OpenGL ES applications, a programmer may choose
66
to specify the integer location of a shader uniform in advance directly
67
in the submitted shader code. The GLSL directive
68
69
layout(location = x) uniform mat4 world;
70
71
specifies that the given uniform should be accessible via location 'x',
72
where x is an integer in the range [0, GL_MAX_UNIFORM_LOCATIONS-1].
73
74
Since WebGL specification does not recognize uniform locations as
75
integers, the notion of layout location qualifiers do not apply to WebGL.
76
However for WebAssembly-based applications they do, and can provide a
77
meaningful way for applications to pre-layout the uniforms in a shader to
78
simplify program structure.
79
80
This extension adds support for specifying layout location qualifiers in
81
GLSL shaders in OpenGL and OpenGL ES programs compiled via Emscripten.
82
83
The main benefit of layout location qualifiers in shaders is to enable
84
aligning shader uniforms across multiple compiled shader programs. For
85
example, if several compiler programs had a common shader uniform variable
86
"vec4 fogColor", without explicit location qualifiers, C/C++ code would
87
have to manage a mapping table that would track the uniform location of
88
fogColor in each shader program. With explicit uniform locations, all shaders
89
can be compiled to locate fogColor in the same location index, thus greatly
90
simplifying C/C++ engine code.
91
92
In sibling specifications, support for explicit uniform locations exist in
93
OpenGL ES 3.1 and Desktop OpenGL 4.3 core specifications. Additionally,
94
Desktop OpenGL 3.3 and newer may support the ARB_explicit_uniform_location
95
extension that also offers the same functionality as this extension
96
EMSCRIPTEN_explicit_uniform_location does.
97
98
New Procedures and Functions
99
100
None.
101
102
New Tokens
103
104
GL_MAX_UNIFORM_LOCATIONS 0x826E
105
106
Additions to Chapter 6 of the OpenGL 2.0 Specification (State and State Requests)
107
108
-- Section 6.1.1, Simple Queries
109
110
In OpenGL ES 2.0 and OpenGL ES 3.0 contexts, the GetIntegerv, entry
111
point parameter 'value' accepts the token GL_MAX_UNIFORM_LOCATIONS, which
112
returns the upper limit, exclusive, that can be specified for integer x
113
in the GLSL uniform variable qualifier "layout(location = x)".
114
115
The value returned by GL_MAX_UNIFORM_LOCATIONS must be at least 1024.
116
117
Additions to OpenGL ES Shading Language 1.00 Specification
118
119
The qualifier "layout(location = x)" can be prepended to a uniform
120
variable directive to bind the integer mapping location for the given
121
uniform throughout the lifetime of the shader program:
122
123
layout(location = x) uniform mat4 world;
124
125
Additions to Emscripten compiler
126
127
Instead of utilizing the runtime method WebGLRenderingContext.getExtension()
128
to coordinate enabling the extension, EMSCRIPTEN_explicit_uniform_location
129
activation is controlled at compile time by specifying the flag
130
131
-sGL_EXPLICIT_UNIFORM_LOCATION
132
133
to 'emcc' or 'em++' linker command line in the Emscripten compiler.
134
135
When this flag is passed, the extension EMSCRIPTEN_explicit_uniform_location
136
is enabled for all contexts created in the application.
137
138
The choice of compile time activation is due to the considerable increase in
139
code size that is involved in enabling this extension.
140
141
Revision History
142
143
Revision 1.0, March 21, 2021: juj
144
- First version
145
146