GPU instancing shader reference for the Built-In Render Pipeline
Explore the built-in defines and macros you can use to support GPU Instancing in custom shaders.
Read time 4 minutesLast updated 8 days ago
This section contains information about shader additions that relate to GPU instancing.
Addition | Description |
|---|---|
| Generates instancing variants. This is required for fragment and vertex shaders. It is optional for Surface Shaders. |
| Specifies options that Unity uses for instances. For information on the option switches available, see |
| Defines an instance ID in the vertex shader input/output structure. To use this macro, enable the INSTANCING_ON shader keyword. Otherwise, Unity doesn't set up the instance ID. To access the instance ID, use |
| Declares the start of a per-instance constant buffer named |
| Declares the end of a per-instance constant buffer named |
| Defines a per-instance shader property with the specified type and name. In the examples below, the |
| Allows shader functions to access the instance ID. For vertex shaders, this macro is required at the beginning. For fragment shaders, this addition is optional. For an example, see Vertex and fragment shader. |
| Copies the instance ID from the input structure to the output structure in the vertex shader. Use this macro if you need to access per-instance data in the fragment shader. |
| Accesses a per-instance shader property in an instancing constant buffer. Unity uses the instance ID to index into the instance data array. |
Instancing_options switches
The # pragma instancing_options directive can use the following switches:
Switch | Description |
|---|---|
| On most platforms, Unity automatically calculates the instancing data array size. It divides the maximum constant buffer size on the target device with the size of the structure that contains all per-instance properties. Generally, you don’t need to worry about the batch size. However, some platforms require a fixed array size. To specify the batch size for those platforms, use the |
| Instructs Unity to assume that all the instances have uniform scalings (the same scale for all X, Y, and Z axes). |
| Makes Unity not apply GPU instancing to LOD fade values. |
| Prevents Unity from applying GPU instancing to Light Probe values and their occlusion data. Setting this option to |
| Prevents Unity from applying GPU instancing to lightmap atlas information values. Setting this option to |
| Generates an additional variant for use with Graphics.RenderMeshIndirect. At the beginning of the vertex shader stage, Unity calls the function specified after the colon. To set up the instance data manually, add per-instance data to this function in the same way you would normally add per-instance data to a shader. Unity also calls this function at the beginning of a fragment shader if any of the fetched instance properties are included in the fragment shader. |