AddComputeVariant(ComputeShader, int, LocalKeyword[])
Adds a new compute shader variant to the collection.
Read time 1 minuteLast updated 5 days ago
Definition
- Type: Method
- Namespace: UnityEngine.Rendering
- Assembly: UnityEngine.CoreModule
public bool AddComputeVariant(ComputeShader shader, int kernelIndex, LocalKeyword[] keywords)
Parameters
The ComputeShader of the variant.
The index of the kernel in .
shaderThe 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 |
|---|---|
| bool | Returns |
Remarks
If is 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 .
shadernullkeywordsKernel indices are positional, so if you add, remove, or reorder the lines of the compute shader, variants that you recorded earlier can become invalid.
#pragma kernelUnity doesn't validate when it adds the variant. If the index doesn't match a kernel of , Unity adds the variant without keywords. GraphicsStateCollection.WarmUp then skips the variant and counts it as warmed up without creating a GPU representation.
kernelIndexshaderAdditional Resources: GraphicsStateCollection.ContainsComputeVariant, GraphicsStateCollection.RemoveComputeVariant, GraphicsStateCollection.GetComputeVariants.
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."); }}