Documentation

Unity Engine

Download Unity

Unity Engine

AddComputeVariant(ComputeShader, int, LocalKeyword[])

Adds a new compute shader variant to the collection.
Read time 1 minuteLast updated 5 days ago

Definition

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

Parameters

The ComputeShader of the variant.

kernelIndex

The index of the kernel in
shader
.

The LocalKeyword 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

boolReturns
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 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 then skips the variant and counts it as warmed up without creating a GPU representation.

Examples

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."); }}