Skip to content

ShaderProgram

Generic image processing shader program

Helper class encapsulating all instances of a simple image processing shaders, including vertex & fragment shaders, output texture, frame buffer, and uniforms. ShaderProgram#process method applies shaders to input textures, results are written to the output texture, ShaderProgram#render method renders directly on the canvas element providing WebGL context. Simplest program processes input texture covering it it by 2 triangles and passing texture coordinates to a fragment shader where all image processing happens.

new ShaderProgram(gl, size, inputs, uniforms, fragSrc, vertSrc, outGrayscale, outLinear, outMipmap): ShaderProgram

Constructor

WebGL2RenderingContext

WebGL context where program is instantiated

Size = ...

Size of processed (input & output) image

string[] = ...

Shader texture inputs (names of sampler uniforms)

Shader uniforms as name-type map object

string = fragSrcDefault

Fragment shader source (copy shader by default)

string = vertSrcDefault

Vertex shader source (copy shader by default)

boolean = false

Grayscale output texture

boolean = false

Output texture read interpolation mode

boolean = false

ShaderProgram

protected fragShader: null | WebGLShader = null

Fragment shader


protected frameBuffer: null | WebGLFramebuffer = null

Frame buffer (output)


protected gl: WebGL2RenderingContext

WebGL context where program is instantiated


protected inputs: string[]

Shader texture inputs (names of sampler uniforms)


protected optional outputTexture: ImageTexture

Output image texture


protected shaderProgram: null | WebGLProgram = null

Shader program


protected size: Size

Size of processed (input & output) image


protected uniforms: object = {}

Shader uniforms as name-type map object

[key: string]: UniformType


protected uniformsLoc: object = {}

Unifrom locations

[key: string]: null | WebGLUniformLocation


protected vertAttrs: null | WebGLVertexArrayObject = null

Vertex attributes


protected vertBuffer: null | WebGLBuffer = null

Vertex buffer


protected vertShader: null | WebGLShader = null

Vertex shader

protected compile(fragSrc, vertSrc): void

Compile shader program

Allocates and sets up all resources required for shader program, compiles and links shaders.

string

Fragment shader source (copy shader by default)

string

Vertex shader source (copy shader by default)

void


dispose(): void

Dispose program object

Releases resources and instances allocated by program. Program object cannot be used after calling dispose().

void


output(): null | WebGLTexture

Output image texture

null | WebGLTexture

Image texture


protected prepare(gl, inputs, uniforms): void

Prepare execution of the shader

Set input textures and provided uniform values, bind program and setup vertex attribute arrays.

WebGL2RenderingContext

Context of shader program

(null | WebGLTexture)[]

Input image textures

Values of shader uniforms

void


process(inputs, uniforms): null | WebGLTexture

Process input image

Applies shader program to input image, output is written to image texture using framebuffer.

(null | WebGLTexture)[]

Input image textures

Values of shader uniforms

null | WebGLTexture

Output image texture


program(): null | WebGLProgram

Shader program instance

null | WebGLProgram

Instance of shader program


read(): null | Uint8Array<ArrayBuffer>

Read image texture

null | Uint8Array<ArrayBuffer>

Texture data


render(inputs, uniforms): void

Process input image and render the result

Applies shader program to input image, output is renderer to canvas providing WebGL context.

(null | WebGLTexture)[]

Input image textures

Values of shader uniforms

void


resize(size): void

Resize

Resizes output image texture and updates uniforms.

Size

Main/output shader size

void


protected restore(gl, state): void

Restore context state

Restores state of WebGL context to recorded checkpoint. This allows to safely share context with other engines.

WebGL2RenderingContext

Context to restore state

Previously recorded state

number = ...

null | WebGLBuffer = ...

boolean = ...

boolean[] = ...

boolean = ...

null | WebGLFramebuffer = ...

null | WebGLProgram = ...

Int32Array<ArrayBufferLike> = ...

(null | WebGLTexture)[] = ...

null | WebGLVertexArrayObject = ...

Int32Array<ArrayBufferLike> = ...

void


protected save(gl): object

Save context state

Saves state of WebGL including program in use, bound array buffers, framebuffer, and textures. Returned state can be restored later to safely share context with other frameworks or renderers.

WebGL2RenderingContext

Context which state will be recorded

object

Current state of WebGL context

activeTexture: number

arrayBuffer: null | WebGLBuffer

blend: boolean

colorMask: boolean[]

cullFace: boolean

framebuffer: null | WebGLFramebuffer

program: null | WebGLProgram

scissor: Int32Array<ArrayBufferLike>

textures: (null | WebGLTexture)[]

vertexArray: null | WebGLVertexArrayObject

viewport: Int32Array<ArrayBufferLike>


setUniform(uniform, val): void

Set uniform value

string

Uniform name

number[]

Uniform value

void


protected setUniformUnsafe(uniform, val): void

Set uniform value unsafely

string

Uniform name

number[]

Uniform value

void