Class: GPULayer

January 16, 2023 · View on GitHub

gpu-io / GPULayer

Class: GPULayer

Table of contents

Properties

Methods

Constructors

Accessors

Properties

name

Readonly name: string

Name of GPULayer, used for error logging.


type

Readonly type: GPULayerType

Data type represented by GPULayer.


numComponents

Readonly numComponents: GPULayerNumComponents

Number of RGBA elements represented by each pixel in the GPULayer (1-4).


filter

Readonly filter: GPULayerFilter

Interpolation filter for GPULayer, defaults to LINEAR for 2D FLOAT/HALF_FLOAT GPULayers, otherwise defaults to NEAREST.


wrapX

Readonly wrapX: GPULayerWrap

Horizontal wrapping style for GPULayer, defaults to CLAMP_TO_EDGE.


wrapY

Readonly wrapY: GPULayerWrap

Vertical wrapping style for GPULayer, defaults to CLAMP_TO_EDGE.


numBuffers

Readonly numBuffers: number

Methods

initFromImageURL

Static initFromImageURL(composer, params): Promise<GPULayer>

Create a GPULayer from an image url.

Parameters

NameTypeDescription
composerGPUComposerThe current GPUComposer instance.
paramsObjectGPULayer parameters.
params.namestringName of GPULayer, used for error logging.
params.urlstringURL of the image source.
params.type?ImageTypeData type represented by GPULayer.
params.format?ImageFormatImage format, either RGB or RGBA.
params.filter?GPULayerFilterInterpolation filter for GPULayer, defaults to LINEAR for FLOAT/HALF_FLOAT Images, otherwise defaults to NEAREST.
params.wrapX?GPULayerWrapHorizontal wrapping style for GPULayer, defaults to CLAMP_TO_EDGE.
params.wrapY?GPULayerWrapVertical wrapping style for GPULayer, defaults to CLAMP_TO_EDGE.
params.clearValue?number | number[]Value to write to GPULayer when GPULayer.clear() is called.

Returns

Promise<GPULayer>


is1D

is1D(): boolean

Returns whether the GPULayer was inited as a 1D array (rather than 2D).

Returns

boolean

  • true if GPULayer is 1D, else false.

is2D

is2D(): boolean

Returns whether the GPULayer was inited as a 2D array (rather than 1D).

Returns

boolean

  • true if GPULayer is 2D, else false.

incrementBufferIndex

incrementBufferIndex(): void

Increment buffer index by 1.

Returns

void


decrementBufferIndex

decrementBufferIndex(): void

Decrement buffer index by 1.

Returns

void


getStateAtIndex

getStateAtIndex(index): GPULayerState

Get the state at a specified index as a GPULayerState object.

Parameters

NameType
indexnumber

Returns

GPULayerState


setFromArray

setFromArray(array): void

Parameters

NameType
arraynumber[] | GPULayerArray

Returns

void


resize

resize(dimensions, arrayOrImage?): void

Parameters

NameType
dimensionsnumber | number[]
arrayOrImage?number[] | HTMLImageElement | GPULayerArray

Returns

void


clear

clear(applyToAllBuffers?): void

Clear all data in GPULayer to GPULayer.clearValue.

Parameters

NameTypeDefault valueDescription
applyToAllBuffersbooleanfalseFlag to apply to all buffers of GPULayer, or just the current output buffer.

Returns

void


getValues

getValues(): GPULayerArray

Returns the current values of the GPULayer as a TypedArray.

Returns

GPULayerArray

  • A TypedArray containing current state of GPULayer.

getValuesAsync

getValuesAsync(): Promise<GPULayerArray>

Non-blocking function to return the current values of the GPULayer as a TypedArray. This only works for WebGL2 contexts, will fall back to getValues() if WebGL1 context.

Returns

Promise<GPULayerArray>

  • A TypedArray containing current state of GPULayer.

getImage

getImage(params?): HTMLImageElement

Get the current state of this GPULayer as an Image.

Parameters

NameTypeDescription
params?ObjectImage parameters.
params.multiplier?numberMultiplier to apply to data (defaults to 255 for FLOAT and HALF_FLOAT types, else 1).

Returns

HTMLImageElement


savePNG

savePNG(params?): void

Save the current state of this GPULayer to png.

Parameters

NameTypeDescription
paramsObjectPNG parameters.
params.filename?stringPNG filename (no extension, defaults to the name of the GPULayer).
params.dpi?numberPNG dpi (defaults to 72dpi).
params.multiplier?numberMultiplier to apply to data before saving PNG (defaults to 255 for FLOAT and HALF_FLOAT types, else 1).
params.callback?(blob: Blob, filename: string) => voidOptional callback when Blob is ready, default behavior saves the PNG using file-saver.

Returns

void


attachToThreeTexture

attachToThreeTexture(texture): void

Attach the output buffer of this GPULayer to a Threejs Texture object.

Parameters

NameTypeDescription
textureTextureThreejs texture object.

Returns

void


clone

clone(name?): GPULayer

Create a deep copy of GPULayer with current state copied over.

Parameters

NameTypeDescription
name?stringName of new GPULayer as string.

Returns

GPULayer

  • Deep copy of GPULayer.

dispose

dispose(): void

Deallocate GPULayer instance and associated WebGL properties.

Returns

void

Constructors

constructor

new GPULayer(composer, params)

Create a GPULayer.

Parameters

NameTypeDescription
composerGPUComposerThe current GPUComposer instance.
paramsObjectGPULayer parameters.
params.namestringName of GPULayer, used for error logging.
params.typeGPULayerTypeData type represented by GPULayer.
params.numComponentsGPULayerNumComponentsNumber of RGBA elements represented by each pixel in the GPULayer (1-4).
params.dimensionsnumber | number[]Dimensions of 1D or 2D GPULayer.
params.array?number[] | GPULayerArrayArray to initialize GPULayer.
params.filter?GPULayerFilterInterpolation filter for GPULayer, defaults to LINEAR for 2D FLOAT/HALF_FLOAT GPULayers, otherwise defaults to NEAREST.
params.wrapX?GPULayerWrapHorizontal wrapping style for GPULayer, defaults to CLAMP_TO_EDGE.
params.wrapY?GPULayerWrapVertical wrapping style for GPULayer, defaults to CLAMP_TO_EDGE.
params.numBuffers?numberHow may buffers to allocate, defaults to 1. If you intend to use the current state of this GPULayer as an input to generate a new state, you will need at least 2 buffers.
params.clearValue?number | number[]Value to write to GPULayer when GPULayer.clear() is called.

Accessors

width

get width(): number

The width of the GPULayer array.

Returns

number


height

get height(): number

The height of the GPULayer array.

Returns

number


length

get length(): number

The length of the GPULayer array (only available to 1D GPULayers).

Returns

number


bufferIndex

get bufferIndex(): number

Get buffer index of the current state.

Returns

number


currentState

get currentState(): GPULayerState

Get the current state as a GPULayerState object.

Returns

GPULayerState


lastState

get lastState(): GPULayerState

Get the previous state as a GPULayerState object (only available for GPULayers with numBuffers > 1).

Returns

GPULayerState


clearValue

get clearValue(): number | number[]

Get the clearValue of the GPULayer.

Returns

number | number[]

set clearValue(clearValue): void

Set the clearValue of the GPULayer, which is applied during GPULayer.clear().

Parameters

NameType
clearValuenumber | number[]

Returns

void