Options
All
  • Public
  • Public/Protected
  • All
Menu

Class UnifiedBuffer

Class to encapsulating a WebGL buffer adding functionality to record changes to the underlying buffer without instantly propagating them to the WebGL buffer. It is intended to be a direct replacement for Buffer. When calling subData the data will be copied to an internal, CPU-sided buffer and the affecting range will be recorded. If the newly recorded range overrides a part of or the whole of a previously recorded range the older one will either be discarded completely or merged with the new one according to a specifiable merge threshold. To propagate all recorded changes to the GPU-sided buffer, update has to be called. This will take care of both the allocation of the GPU-sided buffer and the transfer of the changed data. While update can must only be called after initializing the object, subData, mergeSubDataRanges and resizing (@seesize) can be called on the unitialized object. The GPU-sided buffer is created on initialization and deleted on uninitialization. A typical usage could look like this:

``` // Create a unified buffer with the size of 1028 bytes and a mergeThreshold of 32 bytes const unifiedBuffer = new UnifiedBuffer(context, 1028, gl.STATIC_DRAW, 32, 'UnifiedBuffer'); unifiedBuffer.initialize(gl.ARRAY_BUFFER); unifiedBuffer.attribEnable(0, 1, gl.FLOAT, gl.FALSE, 0, 0, true, false); unifiedBuffer.attribEnable(1, 1, gl.SHORT, gl.FALSE, 0, 512, false, true);

unifiedBuffer.subData(0, new Float32Array(64).fill(3.14)); unifiedBuffer.subData(512, new Int16Array(128).fill(1200));

unifiedBuffer.update(true, true);

unifiedBuffer.subData(128, new Float32Array(32).fill(1.57)); unifiedBuffer.subData(640, new Int36Array(64).fill(600));

unifiedBuffer.mergeThreshold = -1; // This will merge both already existing ranges resulting in a single update unifiedBuffer.mergeSubDataRanges();

unifiedBuffer.bind(); unifiedBuffer.update(); unifiedBuffer.unbind();

Hierarchy

Index

Constructors

constructor

  • new UnifiedBuffer(context: Context, sizeInBytes: number, usage: GLenum, mergeThreshold?: number, identifier?: undefined | string): UnifiedBuffer
  • Creates a zero initialized buffer with the given size.

    Parameters

    • context: Context

      Context used for all GPU operations.

    • sizeInBytes: number

      Size of the buffer in bytes.

    • usage: GLenum

      Usage hint for allocation of the GPU-sided buffer.

    • Default value mergeThreshold: number = 0

      Threshold in which all updates will get merged.

    • Optional identifier: undefined | string

      Unique identifier for this UnifiedBuffer.

    Returns UnifiedBuffer

Properties

Protected _cpuBuffer

_cpuBuffer: ArrayBuffer

Protected _gpuBuffer

_gpuBuffer: Buffer

Protected _mergeThreshold

_mergeThreshold: number

Protected _updates

_updates: Update[] = new Array<Update>()

Protected _usage

_usage: GLenum
see

usage

Accessors

initialized

  • get initialized(): boolean

mergeThreshold

  • get mergeThreshold(): number
  • set mergeThreshold(mergeThreshold: number): void
  • Returns the threshold used to determine whether two ranges have to be merged.

    Returns number

  • Sets the threshold determining whether two ranges have to be merged. If the mergeThreshold is set to -1 all ranges will get merged disregarding their distance to each other.

    Parameters

    • mergeThreshold: number

    Returns void

size

  • get size(): number
  • set size(sizeInBytes: number): void
  • Returns the size of the CPU-sided buffer. This is not necessarily the same as the size of the GPU-sided buffer, if update has not been called after resizing the buffer.

    Returns number

  • Resizes the buffer. Note, that this does not resize the GPU-sided buffer. To update the size of the GPU-sided buffer update has to be called.

    Parameters

    • sizeInBytes: number

    Returns void

target

  • get target(): GLenum | undefined
  • Target to which the buffer object is bound (either GL_ARRAY_BUFFER or GL_ELEMENT_ARRAY_BUFFER). Readonly access to the target (as specified on initialization) the buffer will be bound to.

    Returns GLenum | undefined

usage

  • get usage(): GLenum
  • set usage(usage: GLenum): void

Methods

Protected addUpdate

  • addUpdate(update: Update): void
  • Adds a range to recorded updates. Transitively merges all already recorded updates within the mergeThreshold of the added range.

    Parameters

    • update: Update

      Range to add to the updates.

    Returns void

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

attribDisable

  • attribDisable(index: GLuint, bind?: boolean, unbind?: boolean): void
  • Disables a buffer binding point.

    Parameters

    • index: GLuint

      Index of the vertex attribute that is to be disabled.

    • Default value bind: boolean = true

      Allows to skip binding the object (e.g., when binding is handled outside).

    • Default value unbind: boolean = true

      Allows to skip unbinding the object (e.g., when binding is handled outside).

    Returns void

attribEnable

  • attribEnable(index: GLuint, size: GLint, type: GLenum, normalized?: GLboolean, stride?: GLsizei, offset?: GLintptr, bind?: boolean, unbind?: boolean): void
  • Specifies the memory layout of the buffer for a binding point.

    Parameters

    • index: GLuint

      Index of the vertex attribute that is to be setup and enabled.

    • size: GLint

      Number of components per vertex attribute.

    • type: GLenum

      Data type of each component in the array.

    • Default value normalized: GLboolean = false

      Whether integer data values should be normalized when being casted to a float.

    • Default value stride: GLsizei = 0

      Offset in bytes between the beginning of consecutive vertex attributes.

    • Default value offset: GLintptr = 0

      Offset in bytes of the first component in the vertex attribute array.

    • Default value bind: boolean = true

      Allows to skip binding the object (e.g., when binding is handled outside).

    • Default value unbind: boolean = true

      Allows to skip unbinding the object (e.g., when binding is handled outside).

    Returns void

bind

  • bind(): void

initialize

  • initialize(target: GLenum): boolean

mergeSubDataRanges

  • mergeSubDataRanges(): void

Protected mergeUpdatesLeft

  • mergeUpdatesLeft(index: number): number
  • Merges all updates left of index transitively with the update at index until there are no more updates within the merge threshold.

    Parameters

    • index: number

      Index of the update that should get merged.

    Returns number

    • Number of merged updates.

Protected mergeUpdatesRight

  • mergeUpdatesRight(index: number): number
  • Merges all updates right of index transitively with the update at index until there are no more updates within the merge threshold.

    Parameters

    • index: number

      Index of the update that should get merged.

    Returns number

    • Number of merged updates.

subData

  • subData(dstByteOffset: GLintptr, srcData: ArrayBufferView | ArrayBuffer): void
  • Copies the new data into the CPU-sided buffer and records the range as changed. All previously added ranges will get merged transitively with the new one, if they are within the set mergeThreshold. Note: This does not transfer anything to the GPU-sided buffer yet.

    Parameters

    • dstByteOffset: GLintptr

      Offset of bytes into the destination buffer.

    • srcData: ArrayBufferView | ArrayBuffer

      Data that will be copied into the destination buffer.

    Returns void

unbind

  • unbind(): void

uninitialize

  • uninitialize(): void

update

  • update(bind?: boolean, unbind?: boolean): void
  • Copies all previously recorded ranges and their data to the GPU. (Re-)Allocate the GPU-sided buffer if the size changed or the object was reinitialized.

    Parameters

    • Default value bind: boolean = false

      Allows to skip binding the object (e.g., when binding is handled outside).

    • Default value unbind: boolean = false

      Allows to skip unbinding the object (e.g., when binding is handled outside).

    Returns void

Static Protected Readonly assertInitializedFalse

Static Protected Readonly assertUninitializedFalse

Static assert_initialized

Static assert_uninitialized

Static discard

  • 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

  • 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

Static Protected updatesNeedMerge

  • updatesNeedMerge(lhsUpdate: Update, rhsUpdate: Update, mergeThreshold: number): boolean
  • Checks if two updates have to be merged according to the mergeThreshold. Note: lhsUpdate.begin has to be smaller than rhsUpdate.begin.

    Parameters

    • lhsUpdate: Update

      First update

    • rhsUpdate: Update

      Second update

    • mergeThreshold: number

      Threshold considered for merging.

    Returns boolean

    • True if mergeThreshold == -1 or distance between the two updates <= mergeThreshold, false otherwise.