---
title: AddComputeVariant(ComputeShader, int, LocalKeyword[])
product: Unity Editor
version: '7000.0'
latest_version: '7000.0'
status: This page documents version 7000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/en-us/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/addcomputevariant
latest_url: https://docs.unity.com/en-us/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/addcomputevariant.md
last_updated: 2026-11-13
generated_at: 2026-11-18T08:38:14.098Z
locale: en-us
---

# AddComputeVariant(ComputeShader, int, LocalKeyword[])

> Adds a new compute shader variant to the collection.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.Rendering](/engine/7000.0/script-reference/unityengine/rendering.md)
* **Assembly:** UnityEngine.CoreModule

```csharp
public bool AddComputeVariant(ComputeShader shader, int kernelIndex, LocalKeyword[] keywords)
```

### Parameters

**** (\[ComputeShader]\(/engine/7000.0/script-reference/unityengine/computeshader)): The [ComputeShader](/engine/7000.0/script-reference/unityengine/computeshader.md) of the variant.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The index of the kernel in `shader`.**** (\[LocalKeyword\[]]\(/engine/7000.0/script-reference/unityengine/rendering/localkeyword)): The [LocalKeyword](/engine/7000.0/script-reference/unityengine/rendering/localkeyword.md) array of the variant. The order of the keywords doesn't matter. Unity ignores keywords that the kernel doesn't declare and keywords that only select a dynamic branch.

### Returns

| Type                                                          | Description                                                                            |
| ------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| [bool](https://learn.microsoft.com/dotnet/api/system.boolean) | Returns `true` if Unity adds a new compute shader variant. Otherwise, returns `false`. |

### Remarks

If `shader` is `null` or the variant already exists in the collection, Unity doesn't add a variant. [GraphicsStateCollection.WarmUp](/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/warmup.md) creates the GPU representation only for the kernel permutation that matches `keywords`.

Kernel indices are positional, so if you add, remove, or reorder the `#pragma kernel` lines of the compute shader, variants that you recorded earlier can become invalid.

Unity doesn't validate `kernelIndex` when it adds the variant. If the index doesn't match a kernel of `shader`, Unity adds the variant without keywords. [GraphicsStateCollection.WarmUp](/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/warmup.md) then skips the variant and counts it as warmed up without creating a GPU representation.

Additional Resources: [GraphicsStateCollection.ContainsComputeVariant](/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/containscomputevariant.md), [GraphicsStateCollection.RemoveComputeVariant](/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/removecomputevariant.md), [GraphicsStateCollection.GetComputeVariants](/engine/7000.0/script-reference/unityengine/rendering/graphicsstatecollection/getcomputevariants.md).

### Examples

```csharp
using UnityEngine;
using UnityEngine.Rendering;

// Attach this script to a GameObject, then assign a graphics state collection and a compute shader in the Inspector.
public class AddComputeVariantExample : MonoBehaviour
{
    public GraphicsStateCollection graphicsStateCollection;
    public ComputeShader computeShader;

    void Start()
    {
        // Uses the CSMain kernel with the keywords currently enabled on the compute shader.
        int kernelIndex = computeShader.FindKernel("CSMain");
        bool added = graphicsStateCollection.AddComputeVariant(computeShader, kernelIndex, computeShader.enabledKeywords);
        Debug.Log(added ? "Added the compute shader variant." : "The compute shader variant is already in the collection.");
    }
}
```
