WebglRenderPipeline

View source

Represents a linked WebGL program and the reflection metadata needed to upload uniforms and bind named uniform blocks.

Initialize a pipeline with a WebGL 2 context and a UBOCache after linking its program. A WebglProgramCache can store these values by material type, but a pipeline can also be used directly. The caller must make the program current and bind textures before drawing.

Examples

// `program` is a linked WebGLProgram and `ubos` is a UBOCache.
const pipeline = new WebglRenderPipeline(program)
pipeline.init(gl, ubos)

gl.useProgram(pipeline.program)
pipeline.setUniformMatrix4(gl, 'model', modelMatrix)

Constructor


constructor(program: WebGLProgram)

View source

Creates a pipeline for an existing linked WebGL program.

The program is stored as supplied. Uniform and uniform-block metadata are empty until WebglRenderPipeline#init reflects the program.

Parameters

NameDescriptionDefault
program

Program to use for rendering.

—

Properties


readonlyprogram: WebGLProgram

View source

Linked WebGL program used by this pipeline.

Make this program current with gl.useProgram(pipeline.program) before calling a uniform setter or issuing a draw. The pipeline stores the program but does not bind it or bind textures on its own.

Methods


init(gl: WebGL2RenderingContext, ubos: UBOCache): void

View source

Reflects the program’s uniforms and binds its uniform blocks.

Active uniforms with no returned metadata or no location are skipped. Named uniform blocks are recorded in WebglRenderPipeline#uniformBuffers, matched by name through UBOCache#get, checked against the registered buffer size, and connected to that buffer’s binding point with gl.uniformBlockBinding. This method does not call gl.useProgram, bind a uniform buffer object, or bind a texture. Make those bindings current before drawing.

Parameters

NameDescriptionDefault
gl

Context that owns the program.

—
ubos

Cache containing a buffer for each reflected block.

—

Throws

If a reflected uniform block has no matching UBOCache entry.

If a registered UBO size differs from the shader block’s data size.

setUniformFloat(gl: WebGL2RenderingContext, name: string, value: number): void

View source

Sets a scalar floating-point uniform.

The exact name must be present in the reflected WebglRenderPipeline#uniforms map; unknown names are ignored. This delegates to gl.uniform1f and does not bind the program first.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Floating-point value.

—

setUniformInt(gl: WebGL2RenderingContext, name: string, value: number): void

View source

Sets a scalar signed-integer uniform.

Unknown names are ignored. For a sampler uniform, value can be the texture-unit index; this method writes the sampler value but does not bind the texture or select the texture unit.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Signed integer or sampler texture-unit index.

—

setUniformUint(gl: WebGL2RenderingContext, name: string, value: number): void

View source

Sets a scalar unsigned-integer uniform.

Unknown names are ignored. This delegates to gl.uniform1ui and does not bind the program first.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Unsigned integer value.

—

setUniformVector2(gl: WebGL2RenderingContext, name: string, value: Vector2): void

View source

Sets a two-component floating-point uniform.

Unknown names are ignored. The vector’s x and y fields are passed to gl.uniform2f.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Vector whose x and y components are uploaded.

—

setUniformVector3(gl: WebGL2RenderingContext, name: string, value: Vector3): void

View source

Sets a three-component floating-point uniform.

Unknown names are ignored. The vector’s x, y, and z fields are passed to gl.uniform3f.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Vector whose x, y, and z components are uploaded.

—

setUniformVector4(gl: WebGL2RenderingContext, name: string, value: Vector4): void

View source

Sets a four-component floating-point uniform.

Unknown names are ignored. The vector’s x, y, z, and w fields are passed to gl.uniform4f.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Vector whose x, y, z, and w components are uploaded.

—

setUniformMatrix2(gl: WebGL2RenderingContext, name: string, value: Matrix2): void

View source

Sets a 2x2 matrix uniform.

Unknown names are ignored. The iterable matrix is copied to a new Float32Array and passed to gl.uniformMatrix2fv with transposition disabled.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Matrix values in the order expected by WebGL.

—

setUniformMatrix3(gl: WebGL2RenderingContext, name: string, value: Matrix3): void

View source

Sets a 3x3 matrix uniform.

Unknown names are ignored. The iterable matrix is copied to a new Float32Array and passed to gl.uniformMatrix3fv with transposition disabled.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Matrix values in the order expected by WebGL.

—

setUniformMatrix4(gl: WebGL2RenderingContext, name: string, value: Matrix4): void

View source

Sets a 4x4 matrix uniform.

Unknown names are ignored. The iterable matrix is copied to a new Float32Array and passed to gl.uniformMatrix4fv with transposition disabled.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Matrix values in the order expected by WebGL.

—

setUniformMatrix2x3(gl: WebGL2RenderingContext, name: string, value: Affine2): void

View source

Sets a 2x3 matrix uniform.

Unknown names are ignored. The iterable affine matrix is copied to a new Float32Array and passed to gl.uniformMatrix2x3fv with transposition disabled.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Affine matrix values in the order expected by WebGL.

—

setUniformMatrix3x4(gl: WebGL2RenderingContext, name: string, value: Affine3): void

View source

Sets a 3x4 matrix uniform.

Unknown names are ignored. The iterable affine matrix is copied to a new Float32Array and passed to gl.uniformMatrix3x4fv with transposition disabled.

Parameters

NameDescriptionDefault
gl

Context with this pipeline’s program current.

—
name

Exact active uniform name.

—
value

Affine matrix values in the order expected by WebGL.

—