Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Shader

WebGL shader wrapper encapsulating shader creation, compilation, and deletion. A shader can be attached to multiple Programs for linking, and can be deleted if detached from all (linked) programs. The expected default behavior is to create a shader, attach it to programs, and discard is immediately after all programs are created (linked). If, however, the source of a shader needs to be changed, e.g., for replacements or other modifications, the shader object should be kept and, on change, all programs that have the shader attached have to be invalidated/relinked manually.

var frag = new gloperate.Shader(context, context.gl.FRAGMENT_SHADER, 'EmptyFragmentShader');
var vert = new gloperate.Shader(context, context.gl.VERTEX_SHADER, 'EmptyVertexShader');
vert.initialize('void main() { }');
frag.initialize('void main() { }');

var prog = new gloperate.Program(context, 'EmptyProgram');
prog.initialize([frag, vert]);

Hierarchy

  • AbstractObject<WebGLShader>
    • Shader

Index

Constructors

constructor

  • new Shader(context: Context, type: number, identifier?: string): Shader
  • Object constructor, requires a context and a valid identifier.

    Parameters

    • context: Context

      Valid context to create the object for.

    • type: number

      Either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.

    • Optional identifier: string

      Meaningful name for identification of this instance.

    Returns Shader

Properties

Protected _compiled

_compiled: boolean = false
see

compiled

Protected _context

_context: Context
see

context

Protected _identifier

_identifier: string

Protected _object

_object: undefined | WebGLShader
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 _replacements

_replacements: undefined | Map<string, string>

Map of replacement strings and the value to replace them with.

Protected _source

_source: string
see

source

Protected _type

_type: number
see

type

Protected _valid

_valid: boolean = false
see

valid

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.

Type declaration

    • (): void
    • Returns void

Protected assertUninitialized

assertUninitialized: () => void = ...

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.

Type declaration

    • (): void
    • Returns void

Accessors

compiled

  • get compiled(): boolean
  • Read access the the shader's compile status. True if last compilation was successful.

    Returns boolean

context

  • Read-only access to the objects context, used to get context information and WebGL API access.

    Returns 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
  • Property getter for readonly access to the initialization status of an initializable instance.

    Returns boolean

object

  • get object(): T

source

  • get source(): string
  • set source(source: string): void
  • Read access to the shader's source (without replacements applied).

    Returns string

  • Allows to change the shader's source. Note that this will not recompile the shader.

    Parameters

    • source: string

    Returns void

sourceWithReplacements

  • get sourceWithReplacements(): string
  • Processes all search values and replaces them with the replace value on the source.

    Returns string

    The source with all replacements applied.

type

  • get type(): number

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

compile

  • compile(): void
  • Triggers recompilation of a shader. This is usually used internally automatically, but exposed here for leaky abstraction. It should not be required to invoke this manually in most cases. The shader object is marked as compiled iff the source compiled successfully. Note that invalidation of all programs this shader is attached to needs to be done manually.

    Returns void

Protected create

  • create(source?: string, compile?: boolean): undefined | WebGLShader
  • Creates a shader, sets the shader source, and compiles the shader. If the shader source cannot be compiled, the identifier and an info log are logged to console and the shader object is deleted. Note that a '#version 300 es' is added in case the shader source is compiled in a WebGL2 context.

    Parameters

    • Optional source: string

      Shader source.

    • compile: boolean = true

      Whether or not to compile the shader immediately if a source is provided.

    Returns undefined | WebGLShader

    • Either a new shader or undefined if compilation failed.

Protected delete

  • delete(): void
  • Delete the shader object. This should have the reverse effect of create.

    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

ref

  • ref(): void
  • Increment the reference count of this object.

    Returns void

replace

  • replace(searchValue: string, replaceValue: string): void
  • Adds a search-replacement-pair that is processed every time the shaders is recompiled. Note that recompilation has to be triggered manually. Internally, all replacements are stored as a Map of search and replacement values. Thus, specifying a replacement value overrides an existing search value.

    Parameters

    • searchValue: string

      String that is to be searched (all occurrences) and replaced by replace value.

    • replaceValue: string

      The value to be used as replacement for all search value occurrences.

    Returns void

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
  • Decrement the reference count of this object.

    Returns void

Static Protected Readonly assertInitializedFalse

Static Protected Readonly assertUninitializedFalse

Static assert_initialized

  • assert_initialized(): MethodDecorator
  • Method decorator for asserting the initialization status of an initializable to be true.

    see

    assertInitialized

    Returns MethodDecorator

Static assert_uninitialized

  • assert_uninitialized(): MethodDecorator

Static discard

  • discard(): MethodDecorator
  • Method decorator for discarding of Initializable inheritors. This decorator asserts the initialization status of the instance that is to be discarded, 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

Static initialize

  • initialize(): MethodDecorator
  • 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

  • uninitialize(): MethodDecorator
  • 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