# Compute

Class · category: Graphics

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/platform/graphics/compute.js#L34

A representation of a compute shader with the associated resources, that can be executed on the
GPU. Only supported on WebGPU platform.

Call [Compute#destroy](https://api.playcanvas.com/engine/classes/Compute.md#destroy) when no longer needed. The graphics device retains compute
instances for device recovery until they are explicitly destroyed.

## Constructors

### constructor

```ts
new Compute(graphicsDevice: GraphicsDevice, shader: Shader, name?: string)
```

Create a compute instance. Note that this is supported on WebGPU only and is a no-op on
other platforms.

**Parameters**

- `graphicsDevice` ([`GraphicsDevice`](https://api.playcanvas.com/engine/classes/GraphicsDevice.md)): The graphics device.
- `shader` ([`Shader`](https://api.playcanvas.com/engine/classes/Shader.md)): The compute shader.
- `name` (`string`, optional, default `'Unnamed'`): The name of the compute instance, used for debugging only.

## Properties

### name

```ts
name: string
```

The non-unique name of an instance of the class. Defaults to 'Unnamed'.

## Methods

### deleteParameter

```ts
deleteParameter(name: string): void
```

Deletes a shader parameter from the compute instance.

**Parameters**

- `name` (`string`): The name of the parameter to delete.

### destroy

```ts
destroy(): void
```

Frees resources associated with this compute instance.

### getParameter

```ts
getParameter(name: string): number | number[] | Float32Array<ArrayBufferLike> | Texture | StorageBuffer | VertexBuffer | IndexBuffer | TextureView | undefined
```

Returns the value of a shader parameter from the compute instance.

**Parameters**

- `name` (`string`): The name of the parameter to get.

**Returns** `number | number[] | Float32Array<ArrayBufferLike> |` [`Texture`](https://api.playcanvas.com/engine/classes/Texture.md) `|` [`StorageBuffer`](https://api.playcanvas.com/engine/classes/StorageBuffer.md) `|` [`VertexBuffer`](https://api.playcanvas.com/engine/classes/VertexBuffer.md) `|` [`IndexBuffer`](https://api.playcanvas.com/engine/classes/IndexBuffer.md) `|` [`TextureView`](https://api.playcanvas.com/engine/classes/TextureView.md) `| undefined`: The value of the specified parameter.

### setParameter

```ts
setParameter(name: string, value: number | number[] | Float32Array<ArrayBufferLike> | Texture | StorageBuffer | VertexBuffer | IndexBuffer | TextureView): void
```

Sets a shader parameter on a compute instance.

**Parameters**

- `name` (`string`): The name of the parameter to set.
- `value` (`number | number[] | Float32Array<ArrayBufferLike> |` [`Texture`](https://api.playcanvas.com/engine/classes/Texture.md) `|` [`StorageBuffer`](https://api.playcanvas.com/engine/classes/StorageBuffer.md) `|` [`VertexBuffer`](https://api.playcanvas.com/engine/classes/VertexBuffer.md) `|` [`IndexBuffer`](https://api.playcanvas.com/engine/classes/IndexBuffer.md) `|` [`TextureView`](https://api.playcanvas.com/engine/classes/TextureView.md)): The value for the specified parameter.

### setupDispatch

```ts
setupDispatch(x: number, y?: number, z?: number): void
```

Prepare the compute work dispatch.

**Parameters**

- `x` (`number`): X dimension of the grid of work-groups to dispatch.
- `y` (`number`, optional): Y dimension of the grid of work-groups to dispatch.
- `z` (`number`, optional): Z dimension of the grid of work-groups to dispatch.

### setupIndirectDispatch

```ts
setupIndirectDispatch(slotIndex: number, buffer?: StorageBuffer | null): void
```

Prepare the compute work dispatch to use indirect parameters from a buffer. The dispatch
parameters (x, y, z workgroup counts) are read from the buffer at the specified slot index.

When using the device's built-in buffer (buffer parameter is null), this method must be
called each frame as slots are only valid for the current frame.

**Parameters**

- `slotIndex` (`number`): Slot index in the indirect dispatch buffer. When using the
  device's built-in buffer, obtain this by calling [GraphicsDevice#getIndirectDispatchSlot](https://api.playcanvas.com/engine/classes/GraphicsDevice.md#getindirectdispatchslot).
- `buffer` ([`StorageBuffer`](https://api.playcanvas.com/engine/classes/StorageBuffer.md) `| null`, optional, default `null`): Optional custom storage buffer containing dispatch
  parameters. If not provided, uses the device's built-in [GraphicsDevice#indirectDispatchBuffer](https://api.playcanvas.com/engine/classes/GraphicsDevice.md#indirectdispatchbuffer).
  When providing a custom buffer, the user is responsible for its lifetime and contents.

**Example**

```ts
// Reserve a slot in the indirect dispatch buffer
const slot = device.getIndirectDispatchSlot();

// First compute shader writes dispatch parameters to the buffer
prepareCompute.setParameter('indirectBuffer', device.indirectDispatchBuffer);
prepareCompute.setParameter('slot', slot);
prepareCompute.setupDispatch(1, 1, 1);
device.computeDispatch([prepareCompute]);

// Second compute shader uses indirect dispatch
processCompute.setupIndirectDispatch(slot);
device.computeDispatch([processCompute]);
```
