WebGLRenderingContext: getActiveUniform() method
The WebGLRenderingContext.getActiveUniform() method of
the WebGL API returns a
WebGLActiveInfo object containing size, type, and name of a uniform
attribute. It is generally used when querying unknown uniforms either for debugging or
generic library creation.
Syntax
getActiveUniform(program, index)
Parameters
program-
A
WebGLProgramspecifying the WebGL shader program from which to obtain the uniform variable's information. index-
A
GLuintspecifying the index of the uniform attribute to get. This value is an index 0 to N - 1 as returned bygl.getProgramParameter(program, gl.ACTIVE_UNIFORMS).
Return value
A WebGLActiveInfo object describing the uniform.
The type attribute of the return value will be one of the following:
gl.FLOATgl.FLOAT_VEC2gl.FLOAT_VEC3gl.FLOAT_VEC4gl.INTgl.INT_VEC2gl.INT_VEC3gl.INT_VEC4gl.BOOLgl.BOOL_VEC2gl.BOOL_VEC3gl.BOOL_VEC4gl.FLOAT_MAT2gl.FLOAT_MAT3gl.FLOAT_MAT4gl.SAMPLER_2Dgl.SAMPLER_CUBE-
When using a WebGL 2 context, the
following values are possible additionally:
gl.UNSIGNED_INTgl.UNSIGNED_INT_VEC2gl.UNSIGNED_INT_VEC3gl.UNSIGNED_INT_VEC4gl.FLOAT_MAT2x3gl.FLOAT_MAT2x4gl.FLOAT_MAT3x2gl.FLOAT_MAT3x4gl.FLOAT_MAT4x2gl.FLOAT_MAT4x3gl.SAMPLER_3Dgl.SAMPLER_2D_SHADOWgl.SAMPLER_2D_ARRAYgl.SAMPLER_2D_ARRAY_SHADOWgl.SAMPLER_CUBE_SHADOWgl.INT_SAMPLER_2Dgl.INT_SAMPLER_3Dgl.INT_SAMPLER_CUBEgl.INT_SAMPLER_2D_ARRAYgl.UNSIGNED_INT_SAMPLER_2Dgl.UNSIGNED_INT_SAMPLER_3Dgl.UNSIGNED_INT_SAMPLER_CUBEgl.UNSIGNED_INT_SAMPLER_2D_ARRAY
When gl.linkProgram is called, WebGL creates a list of active uniforms.
These are possible values of the name attribute of return values of
getActiveUniform. WebGL
generates one or more entries in the list depending on the declared type of the uniform
in the shader:
-
Single basic type: one entry with the name of the uniform. E.g.
uniform vec4 a;will result ina. -
Array of basic type: one entry with the name of the uniform suffixed with
[0]. E.g.uniform vec4 b[];will result inb[0]. -
Struct type: one entry for each member of the struct. E.g.
uniform struct { float foo; vec4 bar; } c;will result inc.fooandc.bar. -
Arrays of structs or arrays: each entry of the array will generate its own entries.
E.g.
uniform struct { float foo; vec4 bar; } d[2];will result in:d[0].food[0].bard[1].food[1].bar
-
Uniform blocks: one entry for each member. If the uniform block has an instance
name, the block name is prefixed. E.g.
uniform Block { float foo; };will result infoo, anduniform Block { float bar; } e;will result ine.bar.
The size attribute of the return value corresponds to the length of the
array for uniforms declared as arrays. Otherwise, it is 1 (this includes interface
blocks instanced with arrays).
Exceptions
-
gl.INVALID_VALUEis generated if the programWebGLProgramis invalid (not linked, deleted, etc.). -
gl.INVALID_VALUEis generated if index is not in the range [0,gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS)- 1].
Examples
const numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS);
for (let i = 0; i < numUniforms; ++i) {
const info = gl.getActiveUniform(program, i);
console.log("name:", info.name, "type:", info.type, "size:", info.size);
}
Specifications
| Specification |
|---|
| WebGL Specification # 5.14.10 |
Browser compatibility
BCD tables only load in the browser