API

Class ComputeShader

The ComputeShader object lets you execute a compute shader on your GPU (if supported by the engine)

Index
  • Instantiates a new compute shader.

    Parameters

    • name: string

      Defines the name of the compute shader in the scene

    • engine: AbstractEngine

      Defines the engine the compute shader belongs to

    • shaderPath: string | IComputeShaderPath

      Defines the route to the shader code in one of three ways:

      • object: { compute: "custom" }, used with ShaderStore.ShadersStoreWGSL["customComputeShader"]
      • object: { computeElement: "HTMLElementId" }, used with shader code in script tags
      • object: { computeSource: "compute shader code string" }, where the string contains the shader code
      • string: try first to find the code in ShaderStore.ShadersStoreWGSL[shaderPath + "ComputeShader"]. If not, assumes it is a file with name shaderPath.compute.fx in index.html folder.
    • options: Partial<IComputeShaderOptions> = {}

      Define the options used to create the shader

    Returns ComputeShader

fastMode: boolean = false

When set to true, dispatch won't call isReady anymore and won't check if the underlying GPU resources should be (re)created because of a change in the inputs (texture, uniform buffer, etc.) If you know that your inputs did not change since last time dispatch was called and that isReady() returns true, set this flag to true to improve performance

gpuTimeInFrame?: any

Gets the GPU time spent running the compute shader for the last frame rendered (in nanoseconds). You have to enable the "timestamp-query" extension in the engine constructor options and set engine.enableGPUTimingMeasurements = true.

name: string

The name of the shader

onCompiled: Nullable<(effect: ComputeEffect) => void> = null

Callback triggered when the shader is compiled

onError: Nullable<(effect: ComputeEffect, errors: string) => void> = null

Callback triggered when an error occurs

triggerContextRebuild: boolean = false

If set to true, the compute context will be rebuilt at the next dispatch even if in fast mode

uniqueId: number

Gets the unique id of the compute shader

  • Dispatches (executes) the compute shader

    Parameters

    • x: number

      Number of workgroups to execute on the X dimension

    • Optionaly: number

      Number of workgroups to execute on the Y dimension (default: 1)

    • Optionalz: number

      Number of workgroups to execute on the Z dimension (default: 1)

    Returns boolean

    True if the dispatch could be done, else false (meaning either the compute effect or at least one of the bound resources was not ready)

  • Dispatches (executes) the compute shader.

    Parameters

    • buffer: any

      Buffer containing the number of workgroups to execute on the X, Y and Z dimensions

    • offset: number = 0

      Offset in the buffer where the workgroup counts are stored (default: 0)

    Returns boolean

    True if the dispatch could be done, else false (meaning either the compute effect or at least one of the bound resources was not ready)

  • Waits for the compute shader to be ready and executes it

    Parameters

    • x: number

      Number of workgroups to execute on the X dimension

    • Optionaly: number

      Number of workgroups to execute on the Y dimension (default: 1)

    • Optionalz: number

      Number of workgroups to execute on the Z dimension (default: 1)

    • delay: number = 10

      Delay between the retries while the shader is not ready (in milliseconds - 10 by default)

    Returns Promise<void>

    A promise that is resolved once the shader has been sent to the GPU. Note that it does not mean that the shader execution itself is finished!

  • Binds an external texture to the shader

    Parameters

    • name: string

      Binding name of the texture

    • texture: ExternalTexture

      Texture to bind

    Returns void

  • Binds an internal texture to the shader

    Parameters

    • name: string

      Binding name of the texture

    • texture: InternalTexture

      Texture to bind

    Returns void

  • Binds a texture to the shader

    Parameters

    • name: string

      Binding name of the texture

    • texture: BaseTexture

      Texture to bind

    • bindSampler: boolean = true

      Bind the sampler corresponding to the texture (default: true). The sampler will be bound just before the binding index of the texture

    Returns void

  • Binds a video texture to the shader (by binding the external texture attached to this video)

    Parameters

    • name: string

      Binding name of the texture

    • texture: VideoTexture

      Texture to bind

    Returns boolean

    true if the video texture was successfully bound, else false. false will be returned if the current engine does not support external textures