Low level shader program related functions
You should generally not need to use these functions. They are provided
for those cases where you're doing something out of the ordinary
and you need lower level access.
For backward compatibility they are available at both twgl.programs
and twgl
itself
See module:twgl
for core functions
Methods
(static) bindUniformBlock(gl, programInfo, uniformBlockInfo) → {bool}
Binds a uniform block to the matching uniform block point.
Matches by blocks by name so blocks must have the same name not just the same
structure.
If you have changed any values and you upload the values into the corresponding WebGLBuffer
call module:twgl.setUniformBlock
instead.
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGL2RenderingContext
|
A WebGL 2 rendering context. |
programInfo |
module:twgl.ProgramInfo
|
module:twgl.UniformBlockSpec
|
a |
uniformBlockInfo |
module:twgl.UniformBlockInfo
|
a |
Returns:
- Type:
-
bool
true if buffer was bound. If the programInfo has no block with the same block name
no buffer is bound.
(static) createAttributeSetters(gl, program) → {Object.<string, function()>}
Creates setter functions for all attributes of a shader
program. You can pass this to module:twgl.setBuffersAndAttributes
to set all your buffers and attributes.
- See:
-
module:twgl.setAttributes
for example
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext to use. |
program |
WebGLProgram
|
the program to create setters for. |
Returns:
- Type:
-
Object.<string, function()>
an object with a setter for each attribute by name.
(static) createProgram(gl, shaders, opt_attribsopt, opt_locationsopt, opt_errorCallbackopt) → (nullable) {WebGLProgram}
Creates a program, attaches (and/or compiles) shaders, binds attrib locations, links the
program.
NOTE: There are 4 signatures for this function
twgl.createProgram(gl, [vs, fs], options);
twgl.createProgram(gl, [vs, fs], opt_errFunc);
twgl.createProgram(gl, [vs, fs], opt_attribs, opt_errFunc);
twgl.createProgram(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext to use. |
|
shaders |
Array.<WebGLShader>
|
Array.<string>
|
The shaders to attach, or element ids for their source, or strings that contain their source |
|
opt_attribs |
module:twgl.ProgramOptions
|
Array.<string>
|
module:twgl.ErrorCallback
|
<optional> |
Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in |
opt_locations |
Array.<number>
|
module:twgl.ErrorCallback
|
<optional> |
The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback. |
opt_errorCallback |
module:twgl.ErrorCallback
|
<optional> |
callback for errors. By default it just prints an error to the console |
Returns:
- Type:
-
WebGLProgram
the created program or null if error of a callback was provided.
(static) createProgramAsync(gl, shaders, opt_attribsopt, opt_locationsopt, opt_errorCallbackopt) → {Promise.<WebGLProgram>}
Same as createProgram but returns a promise
NOTE: There are 4 signatures for this function
twgl.createProgramAsync(gl, [vs, fs], options);
twgl.createProgramAsync(gl, [vs, fs], opt_errFunc);
twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_errFunc);
twgl.createProgramAsync(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext to use. |
|
shaders |
Array.<WebGLShader>
|
Array.<string>
|
The shaders to attach, or element ids for their source, or strings that contain their source |
|
opt_attribs |
module:twgl.ProgramOptions
|
Array.<string>
|
module:twgl.ErrorCallback
|
<optional> |
Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in |
opt_locations |
Array.<number>
|
module:twgl.ErrorCallback
|
<optional> |
The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback. |
opt_errorCallback |
module:twgl.ErrorCallback
|
<optional> |
callback for errors. By default it just prints an error to the console |
Returns:
- Type:
-
Promise.<WebGLProgram>
The created program
(static) createProgramFromScripts(gl, shaderScriptIds, opt_attribsopt, opt_locationsopt, opt_errorCallbackopt) → (nullable) {WebGLProgram}
Creates a program from 2 script tags.
NOTE: There are 4 signatures for this function
twgl.createProgramFromScripts(gl, [vs, fs], opt_options);
twgl.createProgramFromScripts(gl, [vs, fs], opt_errFunc);
twgl.createProgramFromScripts(gl, [vs, fs], opt_attribs, opt_errFunc);
twgl.createProgramFromScripts(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext |
|
shaderScriptIds |
Array.<string>
|
Array of ids of the script |
|
opt_attribs |
module:twgl.ProgramOptions
|
Array.<string>
|
module:twgl.ErrorCallback
|
<optional> |
Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in |
opt_locations |
Array.<number>
|
module:twgl.ErrorCallback
|
<optional> |
The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback. |
opt_errorCallback |
module:twgl.ErrorCallback
|
<optional> |
callback for errors. By default it just prints an error to the console |
Returns:
- Type:
-
WebGLProgram
the created program or null if error or a callback was provided.
(static) createProgramFromSources(gl, shaderSources, opt_attribsopt, opt_locationsopt, opt_errorCallbackopt) → (nullable) {WebGLProgram}
Creates a program from 2 sources.
NOTE: There are 4 signatures for this function
twgl.createProgramFromSource(gl, [vs, fs], opt_options);
twgl.createProgramFromSource(gl, [vs, fs], opt_errFunc);
twgl.createProgramFromSource(gl, [vs, fs], opt_attribs, opt_errFunc);
twgl.createProgramFromSource(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext |
|
shaderSources |
Array.<string>
|
Array of sources for the |
|
opt_attribs |
module:twgl.ProgramOptions
|
Array.<string>
|
module:twgl.ErrorCallback
|
<optional> |
Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in |
opt_locations |
Array.<number>
|
module:twgl.ErrorCallback
|
<optional> |
The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback. |
opt_errorCallback |
module:twgl.ErrorCallback
|
<optional> |
callback for errors. By default it just prints an error to the console |
Returns:
- Type:
-
WebGLProgram
the created program or null if error or a callback was provided.
(static) createProgramInfo(gl, shaderSources, opt_attribsopt, opt_locationsopt, opt_errorCallbackopt) → (nullable) {module:twgl.ProgramInfo}
Creates a ProgramInfo from 2 sources.
A ProgramInfo contains
programInfo = {
program: WebGLProgram,
uniformSetters: object of setters as returned from createUniformSetters,
attribSetters: object of setters as returned from createAttribSetters,
}
NOTE: There are 4 signatures for this function
twgl.createProgramInfo(gl, [vs, fs], options);
twgl.createProgramInfo(gl, [vs, fs], opt_errFunc);
twgl.createProgramInfo(gl, [vs, fs], opt_attribs, opt_errFunc);
twgl.createProgramInfo(gl, [vs, fs], opt_attribs, opt_locations, opt_errFunc);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext |
|
shaderSources |
Array.<string>
|
Array of sources for the |
|
opt_attribs |
module:twgl.ProgramOptions
|
Array.<string>
|
module:twgl.ErrorCallback
|
<optional> |
Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in |
opt_locations |
Array.<number>
|
module:twgl.ErrorCallback
|
<optional> |
The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback. |
opt_errorCallback |
module:twgl.ErrorCallback
|
<optional> |
callback for errors. By default it just prints an error to the console |
Returns:
- Type:
-
module:twgl.ProgramInfo
The created ProgramInfo or null if it failed to link or compile
(static) createProgramInfoAsync(gl, shaderSources, opt_attribsopt, opt_locationsopt, opt_errorCallbackopt) → {Promise.<module:twgl.ProgramInfo>}
Same as createProgramInfo but returns a promise
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext |
|
shaderSources |
Array.<string>
|
Array of sources for the |
|
opt_attribs |
module:twgl.ProgramOptions
|
Array.<string>
|
module:twgl.ErrorCallback
|
<optional> |
Options for the program or an array of attribs names or an error callback. Locations will be assigned by index if not passed in |
opt_locations |
Array.<number>
|
module:twgl.ErrorCallback
|
<optional> |
The locations for the. A parallel array to opt_attribs letting you assign locations or an error callback. |
opt_errorCallback |
module:twgl.ErrorCallback
|
<optional> |
callback for errors. By default it just prints an error to the console |
(static) createProgramInfoFromProgram(gl, program) → {module:twgl.ProgramInfo}
Creates a ProgramInfo from an existing program.
A ProgramInfo contains
programInfo = {
program: WebGLProgram,
uniformSetters: object of setters as returned from createUniformSetters,
attribSetters: object of setters as returned from createAttribSetters,
}
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext |
program |
WebGLProgram
|
an existing WebGLProgram. |
(static) createUniformBlockInfo(gl, programInfo, blockName, optionsopt) → {module:twgl.UniformBlockInfo}
Creates a UniformBlockInfo
for the specified block
Note: If the blockName matches no existing blocks a warning is printed to the console and a dummy
UniformBlockInfo
is returned. This is because when debugging GLSL
it is common to comment out large portions of a shader or for example set
the final output to a constant. When that happens blocks get optimized out.
If this function did not create dummy blocks your code would crash when debugging.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGL2RenderingContext
|
A WebGL2RenderingContext |
|
programInfo |
module:twgl.ProgramInfo
|
a |
|
blockName |
string
|
The name of the block. |
|
options |
module:twgl.UniformBlockInfoOptions
|
<optional> |
Optional options for using existing an existing buffer and arrayBuffer |
(static) createUniformBlockInfoFromProgram(gl, program, blockName, optionsopt) → {module:twgl.UniformBlockInfo}
Creates a UniformBlockInfo
for the specified block
Note: If the blockName matches no existing blocks a warning is printed to the console and a dummy
UniformBlockInfo
is returned. This is because when debugging GLSL
it is common to comment out large portions of a shader or for example set
the final output to a constant. When that happens blocks get optimized out.
If this function did not create dummy blocks your code would crash when debugging.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGL2RenderingContext
|
A WebGL2RenderingContext |
|
program |
WebGLProgram
|
A WebGLProgram |
|
uniformBlockSpec. |
module:twgl.UniformBlockSpec
|
A UniformBlockSpec as returned |
|
blockName |
string
|
The name of the block. |
|
options |
module:twgl.UniformBlockInfoOptions
|
<optional> |
Optional options for using existing an existing buffer and arrayBuffer |
(static) createUniformBlockSpecFromProgram(gl, program) → {module:twgl.UniformBlockSpec}
Creates a UniformBlockSpec for the given program.
A UniformBlockSpec represents the data needed to create and bind
UniformBlockObjects
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGL2RenderingContext
|
A WebGL2 Rendering Context |
program |
WebGLProgram
|
A WebGLProgram for a successfully linked program |
(static) createUniformSetters(gl, program) → {Object.<string, function()>}
Creates setter functions for all uniforms of a shader
program.
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGLRenderingContext
|
The WebGLRenderingContext to use. |
program |
WebGLProgram
|
the program to create setters for. |
Returns:
- Type:
-
Object.<string, function()>
an object with a setter by name for each uniform
(static) setBlockUniforms(uniformBlockInfo, values)
Sets values of a uniform block object
Parameters:
Name | Type | Description |
---|---|---|
uniformBlockInfo |
module:twgl.UniformBlockInfo
|
A UniformBlockInfo as returned by |
values |
Object.<string, ?>
|
A uniform name to value map where the value is correct for the given
You can set the values of the uniform block with
Arrays can be JavaScript arrays or typed arrays You can also fill out structure and array values either via
or the more traditional way
You can also specify partial paths
But you can not specify leaf array indices.
IMPORTANT!, packing in a UniformBlock is unintuitive. If you want to deal with the padding yourself you can access the array
Any name that doesn't match will be ignored |
(static) setBuffersAndAttributes(gl, setters, buffers)
Sets attributes and buffers including the ELEMENT_ARRAY_BUFFER
if appropriate
Example:
const programInfo = createProgramInfo(
gl, ["some-vs", "some-fs");
const arrays = {
position: { numComponents: 3, data: [0, 0, 0, 10, 0, 0, 0, 10, 0, 10, 10, 0], },
texcoord: { numComponents: 2, data: [0, 0, 0, 1, 1, 0, 1, 1], },
};
const bufferInfo = createBufferInfoFromArrays(gl, arrays);
gl.useProgram(programInfo.program);
This will automatically bind the buffers AND set the
attributes.
setBuffersAndAttributes(gl, programInfo, bufferInfo);
For the example above it is equivalent to
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
gl.enableVertexAttribArray(a_positionLocation);
gl.vertexAttribPointer(a_positionLocation, 3, gl.FLOAT, false, 0, 0);
gl.bindBuffer(gl.ARRAY_BUFFER, texcoordBuffer);
gl.enableVertexAttribArray(a_texcoordLocation);
gl.vertexAttribPointer(a_texcoordLocation, 4, gl.FLOAT, false, 0, 0);
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGLRenderingContext
|
A WebGLRenderingContext. |
setters |
module:twgl.ProgramInfo
|
Object.<string, function()>
|
A |
buffers |
module:twgl.BufferInfo
|
module:twgl.VertexArrayInfo
|
a |
(static) setUniformBlock(gl, programInfo, uniformBlockInfo)
Uploads the current uniform values to the corresponding WebGLBuffer
and binds that buffer to the program's corresponding bind point for the uniform block object.
If you haven't changed any values and you only need to bind the uniform block object
call module:twgl.bindUniformBlock
instead.
Parameters:
Name | Type | Description |
---|---|---|
gl |
WebGL2RenderingContext
|
A WebGL 2 rendering context. |
programInfo |
module:twgl.ProgramInfo
|
module:twgl.UniformBlockSpec
|
a |
uniformBlockInfo |
module:twgl.UniformBlockInfo
|
a |
(static) setUniforms(setters, values)
Set uniforms and binds related textures.
example:
const programInfo = createProgramInfo(
gl, ["some-vs", "some-fs"]);
const tex1 = gl.createTexture();
const tex2 = gl.createTexture();
... assume we setup the textures with data ...
const uniforms = {
u_someSampler: tex1,
u_someOtherSampler: tex2,
u_someColor: [1,0,0,1],
u_somePosition: [0,1,1],
u_someMatrix: [
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,0,
],
};
gl.useProgram(programInfo.program);
This will automatically bind the textures AND set the
uniforms.
twgl.setUniforms(programInfo, uniforms);
For the example above it is equivalent to
let texUnit = 0;
gl.activeTexture(gl.TEXTURE0 + texUnit);
gl.bindTexture(gl.TEXTURE_2D, tex1);
gl.uniform1i(u_someSamplerLocation, texUnit++);
gl.activeTexture(gl.TEXTURE0 + texUnit);
gl.bindTexture(gl.TEXTURE_2D, tex2);
gl.uniform1i(u_someSamplerLocation, texUnit++);
gl.uniform4fv(u_someColorLocation, [1, 0, 0, 1]);
gl.uniform3fv(u_somePositionLocation, [0, 1, 1]);
gl.uniformMatrix4fv(u_someMatrix, false, [
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,0,
]);
Note it is perfectly reasonable to call setUniforms
multiple times. For example
const uniforms = {
u_someSampler: tex1,
u_someOtherSampler: tex2,
};
const moreUniforms {
u_someColor: [1,0,0,1],
u_somePosition: [0,1,1],
u_someMatrix: [
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,0,
],
};
twgl.setUniforms(programInfo, uniforms);
twgl.setUniforms(programInfo, moreUniforms);
You can also add WebGLSamplers to uniform samplers as in
const uniforms = {
u_someSampler: {
texture: someWebGLTexture,
sampler: someWebGLSampler,
},
};
In which case both the sampler and texture will be bound to the
same unit.
Parameters:
Name | Type | Description |
---|---|---|
setters |
module:twgl.ProgramInfo
|
Object.<string, function()>
|
a |
values |
Object.<string, ?>
|
an object with values for the
You can also fill out structure and array values either via
or the more traditional way
You can also specify partial paths
But you can not specify leaf array indices
|
(static) setUniformsAndBindTextures(setters, values)
Alias for setUniforms
Parameters:
Name | Type | Description |
---|---|---|
setters |
module:twgl.ProgramInfo
|
Object.<string, function()>
|
a |
values |
Object.<string, ?>
|
an object with values for the |
(inner) createProgramInfos(gl, programSpecs, programOptionsopt) → (nullable) {Object.<string, module:twgl.ProgramInfo>}
Creates multiple programInfos
Note: the reason this function exists is because the fastest way to create multiple
programs in WebGL is to create and compile all shaders and link all programs and only
afterwards check if they succeeded. In that way, giving all your shaders
- See:
-
-
Examples:
const programInfos = twgl.createProgramInfos(gl, { lambert: [lambertVS, lambertFS], phong: [phongVS, phoneFS], particles: { shaders: [particlesVS, particlesFS], transformFeedbackVaryings: ['position', 'velocity'], }, });
or
const {lambert, phong, particles} = twgl.createProgramInfos(gl, { lambert: [lambertVS, lambertFS], phong: [phongVS, phoneFS], particles: { shaders: [particlesVS, particlesFS], transformFeedbackVaryings: ['position', 'velocity'], }, });
-
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
the WebGLRenderingContext |
|
programSpecs |
Object.<string, module:twgl.ProgramSpec>
|
An object of ProgramSpecs, one per program. |
|
programOptions |
module:twgl.ProgramOptions
|
<optional> |
options to apply to all programs |
(inner) createProgramInfosAsync(gl, programSpecs, programOptionsopt) → {Promise.<Object.<string, module:twgl.ProgramInfo>>}
Creates multiple programInfos asynchronously
- See:
-
module:twgl.createProgramInfoAsync
Example:
const programInfos = await twgl.createProgramInfosAsync(gl, { lambert: [lambertVS, lambertFS], phong: [phongVS, phoneFS], particles: { shaders: [particlesVS, particlesFS], transformFeedbackVaryings: ['position', 'velocity'], }, });
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
the WebGLRenderingContext |
|
programSpecs |
Object.<string, module:twgl.ProgramSpec>
|
An object of ProgramSpecs, one per program. |
|
programOptions |
module:twgl.ProgramOptions
|
<optional> |
options to apply to all programs |
(inner) createPrograms(gl, programSpecs, programOptionsopt) → (nullable) {Object.<string, WebGLProgram>}
Creates multiple programs
Note: the reason this function exists is because the fastest way to create multiple
programs in WebGL is to create and compile all shaders and link all programs and only
afterwards check if they succeeded. In that way, giving all your shaders
- See:
-
module:twgl.createProgram
Example:
const programs = twgl.createPrograms(gl, { lambert: [lambertVS, lambertFS], phong: [phongVS, phoneFS], particles: { shaders: [particlesVS, particlesFS], transformFeedbackVaryings: ['position', 'velocity'], }, });
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
the WebGLRenderingContext |
|
programSpecs |
Object.<string, module:twgl.ProgramSpec>
|
An object of ProgramSpecs, one per program. |
|
programOptions |
module:twgl.ProgramOptions
|
<optional> |
options to apply to all programs |
Returns:
- Type:
-
Object.<string, WebGLProgram>
the created programInfos by name
(inner) createProgramsAsync(gl, programSpecs, programOptionsopt) → (nullable) {Object.<string, WebGLProgram>}
Creates multiple programs asynchronously
- See:
-
module:twgl.createProgramAsync
Example:
const programs = await twgl.createProgramsAsync(gl, { lambert: [lambertVS, lambertFS], phong: [phongVS, phoneFS], particles: { shaders: [particlesVS, particlesFS], transformFeedbackVaryings: ['position', 'velocity'], }, });
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
gl |
WebGLRenderingContext
|
the WebGLRenderingContext |
|
programSpecs |
Object.<string, module:twgl.ProgramSpec>
|
An object of ProgramSpecs, one per program. |
|
programOptions |
module:twgl.ProgramOptions
|
<optional> |
options to apply to all programs |
Returns:
- Type:
-
Object.<string, WebGLProgram>
the created programInfos by name
Type Definitions
ProgramCallback(erropt, resultopt)
Program Callback
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
err |
string
|
<optional> |
error message, falsy if no error |
result |
WebGLProgram
|
module:twgl.ProgramInfo
|
<optional> |
the program or programInfo |
UniformBlockInfoOptions
Options to allow createUniformBlockInfo to use an existing buffer and arrayBuffer at an offset
Properties:
Name | Type | Attributes | Description |
---|---|---|---|
array |
ArrayBuffer
|
<optional> |
an existing array buffer to use for values |
offset |
number
|
<optional> |
the offset in bytes to use in the array buffer (default = 0) |
buffer |
WebGLBuffer
|
<optional> |
the buffer to use for this uniform block info |
bufferOffset |
number
|
<optional> |
the offset in bytes in the buffer to use (default = use offset above) |
Type:
-
Object