Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Program

WebGL Program wrapper encapsulating program creation, shader attachment, linking, binding, as well as attribute and uniform location retrieval. A program is intended to be used as follows:

const vert = new Shader(this._context, gl.VERTEX_SHADER, 'ndcvertices.vert (blit)');
vert.initialize(require('./shaders/ndcvertices.vert'));
const frag = new Shader(this._context, gl.FRAGMENT_SHADER, 'blit.frag');
frag.initialize(require('./shaders/blit.frag'));

this._program = new Program(this._context, 'BlitProgram');
this._program.initialize([vert, frag]);

this.aVertex = this._program.attribute('a_vertex');
const uTexture = this._program.uniform('u_texture');

this._program.bind();
gl.uniform1i(uTexture, 0);
// ... draw
this._program.unbind();

Hierarchy

Implements

Index

Constructors

constructor

  • new Program(context: Context, identifier?: undefined | string): Program
  • Object constructor, requires a context and a valid identifier.

    Parameters

    • context: Context

      Valid context to create the object for.

    • Optional identifier: undefined | string

      Meaningful name for identification of this instance.

    Returns Program

Properties

Protected _context

_context: Context
see

context

Protected _identifier

_identifier: string

Protected _linked

_linked: boolean = false
see

linked

Protected _object

_object: WebGLProgram | undefined
see

object

Protected _referenceCount

_referenceCount: number = 0

Number of references to this object. If at least a single reference was counted, this object can neither be initialized (and thus created) nor uninitialized (and thus deleted). The reference count is controlled via ref() and unref() functions.

Protected _shaders

_shaders: Shader[] = new Array<Shader>()
see

shaders

Protected _valid

_valid: boolean = false
see

valid

Static DEFAULT_PROGRAM

DEFAULT_PROGRAM: undefined = undefined

Default program, e.g., used for unbind.

Accessors

context

identifier

  • get identifier(): string
  • Every GPU asset that allocates memory should provide a human readable identifier for GPU allocation tracking and debugging purposes. Please note that the identifier might changed on initialization due to the generation and assignment of a unique identifier.

    Returns string

    • This assets identifier used for gpu allocation tracking and debugging.

initialized

  • get initialized(): boolean

linked

  • get linked(): boolean
  • Read access the the program's link status. True if last linking was successful.

    Returns boolean

object

  • get object(): WebGLProgram

shaders

  • get shaders(): Array<Shader>
  • Provides access (leaky abstraction) to all shaders attached to this program.

    Returns Array<Shader>

valid

  • get valid(): boolean
  • Cached object status used to derive validity when initialized.

    Returns boolean

    • True if the object status is complete, false otherwise.

Methods

Protected assertInitialized

  • assertInitialized(): void
  • Asserts the objects initialization status to be true. Note that the implementation is cached and forwarded to either an empty function when initialized and to an acutal assert(false) otherwise.

    Returns void

Protected assertUninitialized

  • assertUninitialized(): undefined
  • Asserts the objects initialization status to be false. Note that the implementation is cached and forwarded to either an empty function when uninitialized and to an acutal assert(false) otherwise.

    Returns undefined

attach

  • attach(shaders: Shader | Array<Shader>, link?: boolean): boolean
  • Attaches and references all given shaders. Attach is expected to be called once within creation of a Program. Shaders that are not initialized will be skipped/not attached.

    Parameters

    • shaders: Shader | Array<Shader>

      All shaders to be attached to the program for linking.

    • Default value link: boolean = false

      Whether or not to link the program again after attaching the shader(s).

    Returns boolean

    • True if attaching all shaders and linking succeeded, false otherwise.

attribute

  • attribute(attribute: string, location?: GLuint): GLint
  • Requests the location of an attribute of the program.

    Parameters

    • attribute: string

      Attribute identifier to request location of.

    • Optional location: GLuint

      Attribute location (if WebGL2 location is used)

    Returns GLint

    • Location of the attribute (or location parameter if provided).

bind

  • bind(): void
  • Activates this program for use.

    Returns void

Protected create

  • create(shaders?: Array<Shader>, link?: boolean): WebGLProgram | undefined
  • Creates a WebGLProgram object and attaches, and references all shaders to it. The program is then linked. All shaders have to be initialized in order to be attached and at least on vertex and one fragment shader has to be present. Note that the shaders are not detached by default. If neither the shader objects nor recompilation is required all shaders should be detached manually after initialization/creation.

    Parameters

    • Default value shaders: Array<Shader> = new Array<Shader>()

      Vertex and fragment shaders that are to be attached to the program.

    • Default value link: boolean = true

      Whether or not to immediately link the program iff provided shader(s) are attached successfully.

    Returns WebGLProgram | undefined

    • Either a new program or undefined if linking failed or one of the shaders is invalid/not compiled.

Protected delete

  • delete(): void

detach

  • Detaches one or multiple shaders from the program. Note that relinking is not invoked automatically.

    Parameters

    • shaders: Shader | Array<Shader>

      Shaders that are to be deleted.

    Returns void

initialize

  • initialize(...args: any[]): boolean
  • override

    Ensure that an object handle is created at the point of initialization. When overriding this function super.initialize() has to be invoked immediately/first. Please note that initialization of invalid object raises an assertion in order to prevent further actions without a valid WebGL object. After object creation the valid property is expected to be set accordingly.

    Parameters

    • Rest ...args: any[]

    Returns boolean

link

  • link(): boolean
  • Links the program with all its already attached shaders. If linking fails, a developer log with additional information is provided.

    Returns boolean

    • True if linking the program succeeded, false otherwise.

ref

  • ref(): void

unbind

  • unbind(): void
  • Deactivates this/any program for use.

    Returns void

uniform

  • uniform(uniform: string): WebGLUniformLocation
  • Requests the location of a uniform of the program.

    Parameters

    • uniform: string

      Uniform identifier to request location of.

    Returns WebGLUniformLocation

uninitialize

  • uninitialize(): void
  • override

    Ensure that an object handle is deleted, invalidated, and its allocated GPU resources are set to zero. When overriding this function super.uninitialize() has to be invoked last/at the end. Note that an object cannot be uninitialized if it is referenced (reference count > 0).

    Returns void

unref

  • unref(): void

Static Protected assertInitializedFalse

Static Protected assertUninitializedFalse

Static assert_initialized

Static assert_uninitialized

Static initialize

  • Method decorator for initialization of Initializable inheritors. This decorator asserts the initialization status of the instance that is to be initialized, invokes its initialization with arbitrary number of parameters, and sets the initialization status to the initialization success (either false or true). In order to encourage the use of assertInitialized and assertUninitialized they are dynamically bound to either a static, always-failing assert or an empty/undefined function.

    Returns MethodDecorator

Static uninitialize

  • Method decorator for uninitialization of Initializable inheritors. This decorator asserts the initialization status of the instance that is to be uninitialized, invokes its uninitialization, and falsifies the initialization status. In order to encourage the use of assertInitialized and assertUninitialized they are dynamically bound to a static, always-failing assert and an empty/undefined function respectively.

    Returns MethodDecorator