Class: GpuProgram

GpuProgram(gl, vertexShaderSource, fragmentShaderSource, attributeBindings)

Represents an OpenGL shading language (GLSL) shader program and provides methods for identifying and accessing shader variables. Shader programs are created by instances of this class and made current when the DrawContext.bindProgram function is invoked.

This is an abstract class and not intended to be created directly.

Constructor

new GpuProgram(gl, vertexShaderSource, fragmentShaderSource, attributeBindings)

Constructs a GPU program with specified source code for vertex and fragment shaders. This constructor is intended to be called only by subclasses.

This constructor creates WebGL shaders for the specified shader sources and attaches them to a new GLSL program. The method compiles the shaders and then links the program if compilation is successful. Use the DrawContext.bindProgram function to make the program current during rendering.

Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
vertexShaderSource String The source code for the vertex shader.
fragmentShaderSource String The source code for the fragment shader.
attributeBindings Array.<String> An array of attribute variable names whose bindings are to be explicitly specified. Each name is bound to its corresponding index in the array. May be null, in which case the linker determines all the bindings.
Source:
Throws:
If either source is null or undefined, the shaders cannot be compiled, or linking of the compiled shaders into a program fails.
Type
ArgumentError

Members

(readonly) programId :WebGLProgram

Indicates the WebGL program object associated with this GPU program.
Type:
  • WebGLProgram
Source:

Methods

attributeLocation(gl, attributeName) → {Number}

Returns the GLSL attribute location of a specified attribute name.
Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
attributeName String The name of the attribute whose location is determined.
Source:
Throws:
If the specified attribute name is null, empty or undefined.
Type
ArgumentError
Returns:
The WebGL attribute location of the specified attribute, or -1 if the attribute is not found.
Type
Number

dispose(gl)

Releases this GPU program's WebGL program and associated shaders. Upon return this GPU program's WebGL program ID is 0 as is that of the associated shaders.
Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
Source:
Links a specified GLSL program. This method is not meant to be called by applications. It is called internally as needed.
Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
program WebGLProgram The WebGL program.
Source:
Returns:
true if linking was successful, otherwise false.
Type
Boolean

loadUniformColor(gl, color, location)

Loads a specified color as the value of a GLSL vec4 uniform variable with the specified location.

This function multiplies the red, green and blue components by the alpha component prior to loading the color in the GLSL uniform variable, but does not modify the specified color.

Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
color Color The color to load.
location WebGLUniformLocation The location of the uniform variable in the currently bound GLSL program.
Source:
Throws:
If the specified color is null or undefined.
Type
ArgumentError

loadUniformColorComponents(gl, red, green, blue, alpha, location)

Loads the specified RGBA color components as the value of a GLSL vec4 uniform variable with the specified location.

This function multiplies the red, green and blue components by the alpha component prior to loading the color in the GLSL uniform variable.

Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
red Number The red component, a number between 0 and 1.
green Number The green component, a number between 0 and 1.
blue Number The blue component, a number between 0 and 1.
alpha Number The alpha component, a number between 0 and 1.
location WebGLUniformLocation The location of the uniform variable in the currently bound GLSL program.
Source:

loadUniformMatrix(gl, matrix, location)

Loads a specified matrix as the value of a GLSL 4x4 matrix uniform variable with the specified location.

This functions converts the matrix into column-major order prior to loading its components into the GLSL uniform variable, but does not modify the specified matrix.

Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
matrix Matrix The matrix to load.
location WebGLUniformLocation The location of the uniform variable in the currently bound GLSL program.
Source:
Throws:
If the specified matrix is null or undefined.
Type
ArgumentError

uniformLocation(gl, uniformName) → {WebGLUniformLocation}

Returns the GLSL uniform location of a specified uniform name.
Parameters:
Name Type Description
gl WebGLRenderingContext The current WebGL context.
uniformName String The name of the uniform variable whose location is determined.
Source:
Throws:
If the specified uniform name is null, empty or undefined.
Type
ArgumentError
Returns:
The WebGL uniform location of the specified uniform variable, or -1 if the uniform is not found.
Type
WebGLUniformLocation