Class ComputeShader
Constructors
constructor
-
new ComputeShader(
name: string,
engine: AbstractEngine,
shaderPath: string | IComputeShaderPath,
options?: Partial<IComputeShaderOptions>,
): ComputeShaderInstantiates 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
- name: string
Properties
fast Mode
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
Optional Readonlygpu Time In Frame
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
The name of the shader
on Compiled
Callback triggered when the shader is compiled
on Error
Callback triggered when an error occurs
trigger Context Rebuild
If set to true, the compute context will be rebuilt at the next dispatch even if in fast mode
Readonlyunique Id
Gets the unique id of the compute shader
Accessors
options
-
The options used to create the shader
Returns IComputeShaderOptions
shader Path
-
The shaderPath used to create the shader
Returns string | IComputeShaderPath
Methods
dispatch
-
Dispatches (executes) the compute shader
Parameters
- x: number
Number of workgroups to execute on the X dimension
Optionaly: numberNumber of workgroups to execute on the Y dimension (default: 1)
Optionalz: numberNumber 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)
- x: number
dispatch Indirect
-
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)
- buffer: any
dispatch When 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: numberNumber of workgroups to execute on the Y dimension (default: 1)
Optionalz: numberNumber 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!
- x: number
get Class Name
-
Gets the current class name of the material e.g. "ComputeShader" Mainly use in serialization.
Returns string
the class name
is Ready
-
Specifies that the compute shader is ready to be executed (the compute effect and all the resources are ready)
Returns boolean
true if the compute shader is ready to be executed
serialize
-
Serializes this compute shader in a JSON representation
Returns any
the serialized compute shader object
set External Texture
-
Binds an external texture to the shader
Parameters
- name: string
Binding name of the texture
- texture: ExternalTexture
Texture to bind
Returns void
- name: string
set Internal Texture
-
Binds an internal texture to the shader
Parameters
- name: string
Binding name of the texture
- texture: InternalTexture
Texture to bind
Returns void
- name: string
set Storage Buffer
-
Binds a storage buffer to the shader
Parameters
- name: string
Binding name of the buffer
- buffer: any
Buffer to bind
Returns void
- name: string
set Storage Texture
-
Binds a storage texture to the shader
Parameters
- name: string
Binding name of the texture
- texture: BaseTexture
Texture to bind
Returns void
- name: string
set Texture
-
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
- name: string
set Texture Sampler
-
Binds a texture sampler to the shader
Parameters
- name: string
Binding name of the sampler
- sampler: TextureSampler
Sampler to bind
Returns void
- name: string
set Uniform Buffer
-
Binds a uniform buffer to the shader
Parameters
- name: string
Binding name of the buffer
- buffer: any
Buffer to bind
Returns void
- name: string
set Video Texture
-
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
- name: string
The ComputeShader object lets you execute a compute shader on your GPU (if supported by the engine)