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

Index

Constructors

constructor

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

    Parameters

    • context: Context

      Valid context to create the object for.

    • type: GLenum

      Either GL_VERTEX_SHADER or GL_FRAGMENT_SHADER.

    • Optional identifier: undefined | 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: WebGLShader | 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 _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: GLenum
see

type

Protected _valid

_valid: boolean = false
see

valid

Accessors

compiled

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

    Returns boolean

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

object

  • get object(): WebGLShader

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(): GLenum
  • Either VERTEX_SHADER or FRAGMENT_SHADER.

    Returns GLenum

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

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?: undefined | string, compile?: boolean): WebGLShader | undefined
  • 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: undefined | string

      Shader source.

    • Default value compile: boolean = true

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

    Returns WebGLShader | undefined

    • Either a new shader or undefined if compilation failed.

Protected delete

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

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

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