Table of Contents

What's not supported

Shader code runs on the GPU, which has no heap, no strings and no exceptions. This page lists the C# features shader code can't use, and what to use instead. Using any of them is reported as a compiler error, with a message that says what to change.

Types

Not supported Use instead
Classes (other than static [ShaderCode] classes), object, interfaces [ShaderCode] structs
string, char, nameof —
C# arrays T[], params array4<T> and the other array types
Arrays of arrays A single larger array, indexed as row * width + column
long, short, byte, decimal int, uint, float
double float. Most GPUs don't support double precision (see Scalar types)
Nullable types, ??, ?. —
Tuples A [ShaderCode] struct, or out parameters
Delegates, lambdas, Func and Action Methods
typeof, is, as —

Statements and expressions

Not supported Use instead
foreach for with the array's Length
goto break, continue, or a flag
try, catch, finally, throw —
switch expressions (x switch { ... }) A switch statement, or the conditional operator
default, default(T) new T(), which fills everything with zeros
checked, unchecked —
Object initializers (new S { A = 1 }) new S(), then assign the fields
Declarations in arguments (F(out float x)) Declare the variable first
Discards (F(out _), _ = F()) A variable whose value you ignore
Arrays with elements outside a variable declaration Declare the array variable first

Members and methods

Not supported Use instead
Fields in a shader class const fields, or local variables
static readonly fields const fields; for a vector or another value C# doesn't allow in a const, a method that returns it
Properties with a body or a setter in a shader class { get; } properties, and methods to compute values
Properties, constructors, indexers, operators and events in shared code Methods
Generic methods Overloads
Local functions Methods of the class
Methods returning an array An out parameter
Recursion Loops
Optional parameters with a default value other than a number, a bool or an enum value Pass the argument explicitly

Recursion isn't reported by the C# compiler: the shader fails to compile with an error such as "recursive functions not allowed".

Names

Names in shader code can't contain underscores, whether of types, methods, properties, local variables, parameters, fields or constants, since Shader Script uses underscores in the names it generates.

Some property names are reserved for the material properties Shader Script generates itself for the surface settings, such as Surface, Blend or Cull. You don't need to know the list: if a property uses a reserved name, the compiler reports an error that names the property and asks you to rename it.