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.
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new ShaderProgram(
gl
,size
,inputs
,uniforms
,fragSrc
,vertSrc
,outGrayscale
,outLinear
,outMipmap
):ShaderProgram
Constructor
Parameters
Section titled “Parameters”WebGL2RenderingContext
WebGL context where program is instantiated
Size
= ...
Size of processed (input & output) image
inputs
Section titled “inputs”string
[] = ...
Shader texture inputs (names of sampler uniforms)
uniforms
Section titled “uniforms”Shader uniforms as name-type map object
fragSrc
Section titled “fragSrc”string
= fragSrcDefault
Fragment shader source (copy shader by default)
vertSrc
Section titled “vertSrc”string
= vertSrcDefault
Vertex shader source (copy shader by default)
outGrayscale
Section titled “outGrayscale”boolean
= false
Grayscale output texture
outLinear
Section titled “outLinear”boolean
= false
Output texture read interpolation mode
outMipmap
Section titled “outMipmap”boolean
= false
Returns
Section titled “Returns”ShaderProgram
Properties
Section titled “Properties”fragShader
Section titled “fragShader”
protected
fragShader:null
|WebGLShader
=null
Fragment shader
frameBuffer
Section titled “frameBuffer”
protected
frameBuffer:null
|WebGLFramebuffer
=null
Frame buffer (output)
protected
gl:WebGL2RenderingContext
WebGL context where program is instantiated
inputs
Section titled “inputs”
protected
inputs:string
[]
Shader texture inputs (names of sampler uniforms)
outputTexture?
Section titled “outputTexture?”
protected
optional
outputTexture:ImageTexture
Output image texture
shaderProgram
Section titled “shaderProgram”
protected
shaderProgram:null
|WebGLProgram
=null
Shader program
protected
size:Size
Size of processed (input & output) image
uniforms
Section titled “uniforms”
protected
uniforms:object
={}
Shader uniforms as name-type map object
Index Signature
Section titled “Index Signature”[key
: string
]: UniformType
uniformsLoc
Section titled “uniformsLoc”
protected
uniformsLoc:object
={}
Unifrom locations
Index Signature
Section titled “Index Signature”[key
: string
]: null
| WebGLUniformLocation
vertAttrs
Section titled “vertAttrs”
protected
vertAttrs:null
|WebGLVertexArrayObject
=null
Vertex attributes
vertBuffer
Section titled “vertBuffer”
protected
vertBuffer:null
|WebGLBuffer
=null
Vertex buffer
vertShader
Section titled “vertShader”
protected
vertShader:null
|WebGLShader
=null
Vertex shader
Methods
Section titled “Methods”compile()
Section titled “compile()”
protected
compile(fragSrc
,vertSrc
):void
Compile shader program
Allocates and sets up all resources required for shader program, compiles and links shaders.
Parameters
Section titled “Parameters”fragSrc
Section titled “fragSrc”string
Fragment shader source (copy shader by default)
vertSrc
Section titled “vertSrc”string
Vertex shader source (copy shader by default)
Returns
Section titled “Returns”void
dispose()
Section titled “dispose()”dispose():
void
Dispose program object
Releases resources and instances allocated by program. Program object cannot be used after calling dispose().
Returns
Section titled “Returns”void
output()
Section titled “output()”output():
null
|WebGLTexture
Output image texture
Returns
Section titled “Returns”null
| WebGLTexture
Image texture
prepare()
Section titled “prepare()”
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.
Parameters
Section titled “Parameters”WebGL2RenderingContext
Context of shader program
inputs
Section titled “inputs”(null
| WebGLTexture
)[]
Input image textures
uniforms
Section titled “uniforms”Values of shader uniforms
Returns
Section titled “Returns”void
process()
Section titled “process()”process(
inputs
,uniforms
):null
|WebGLTexture
Process input image
Applies shader program to input image, output is written to image texture using framebuffer.
Parameters
Section titled “Parameters”inputs
Section titled “inputs”(null
| WebGLTexture
)[]
Input image textures
uniforms
Section titled “uniforms”Values of shader uniforms
Returns
Section titled “Returns”null
| WebGLTexture
Output image texture
program()
Section titled “program()”program():
null
|WebGLProgram
Shader program instance
Returns
Section titled “Returns”null
| WebGLProgram
Instance of shader program
read()
Section titled “read()”read():
null
|Uint8Array
<ArrayBuffer
>
Read image texture
Returns
Section titled “Returns”null
| Uint8Array
<ArrayBuffer
>
Texture data
render()
Section titled “render()”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.
Parameters
Section titled “Parameters”inputs
Section titled “inputs”(null
| WebGLTexture
)[]
Input image textures
uniforms
Section titled “uniforms”Values of shader uniforms
Returns
Section titled “Returns”void
resize()
Section titled “resize()”resize(
size
):void
Resize
Resizes output image texture and updates uniforms.
Parameters
Section titled “Parameters”Main/output shader size
Returns
Section titled “Returns”void
restore()
Section titled “restore()”
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.
Parameters
Section titled “Parameters”WebGL2RenderingContext
Context to restore state
Previously recorded state
activeTexture
Section titled “activeTexture”number
= ...
arrayBuffer
Section titled “arrayBuffer”null
| WebGLBuffer
= ...
boolean
= ...
colorMask
Section titled “colorMask”boolean
[] = ...
cullFace
Section titled “cullFace”boolean
= ...
framebuffer
Section titled “framebuffer”null
| WebGLFramebuffer
= ...
program
Section titled “program”null
| WebGLProgram
= ...
scissor
Section titled “scissor”Int32Array
<ArrayBufferLike
> = ...
textures
Section titled “textures”(null
| WebGLTexture
)[] = ...
vertexArray
Section titled “vertexArray”null
| WebGLVertexArrayObject
= ...
viewport
Section titled “viewport”Int32Array
<ArrayBufferLike
> = ...
Returns
Section titled “Returns”void
save()
Section titled “save()”
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.
Parameters
Section titled “Parameters”WebGL2RenderingContext
Context which state will be recorded
Returns
Section titled “Returns”object
Current state of WebGL context
activeTexture
Section titled “activeTexture”activeTexture:
number
arrayBuffer
Section titled “arrayBuffer”arrayBuffer:
null
|WebGLBuffer
blend:
boolean
colorMask
Section titled “colorMask”colorMask:
boolean
[]
cullFace
Section titled “cullFace”cullFace:
boolean
framebuffer
Section titled “framebuffer”framebuffer:
null
|WebGLFramebuffer
program
Section titled “program”program:
null
|WebGLProgram
scissor
Section titled “scissor”scissor:
Int32Array
<ArrayBufferLike
>
textures
Section titled “textures”textures: (
null
|WebGLTexture
)[]
vertexArray
Section titled “vertexArray”vertexArray:
null
|WebGLVertexArrayObject
viewport
Section titled “viewport”viewport:
Int32Array
<ArrayBufferLike
>
setUniform()
Section titled “setUniform()”setUniform(
uniform
,val
):void
Set uniform value
Parameters
Section titled “Parameters”uniform
Section titled “uniform”string
Uniform name
number
[]
Uniform value
Returns
Section titled “Returns”void
setUniformUnsafe()
Section titled “setUniformUnsafe()”
protected
setUniformUnsafe(uniform
,val
):void
Set uniform value unsafely
Parameters
Section titled “Parameters”uniform
Section titled “uniform”string
Uniform name
number
[]
Uniform value
Returns
Section titled “Returns”void