Version: 2021.3
Language : English
ShaderLab: adding shader programs
ShaderLab: commands

ShaderLab: specifying package requirements

Some shadersA program that runs on the GPU. More info
See in Glossary
need to support multiple render pipelinesA series of operations that take the contents of a Scene, and displays them on a screen. Unity lets you choose from pre-built render pipelines, or write your own. More info
See in Glossary
simultaneously. Adding package requirements to SubShaders and Passes enables you to avoid compilation errors when shader code uses an include file from a package that is not installed or requires a specific version of a package to compile.

Render pipeline compatibility

Feature name Built-in Render Pipeline Universal Render Pipeline (URP) High Definition Render Pipeline (HDRP) Custom SRP
ShaderLabUnity’s language for defining the structure of Shader objects. More info
See in Glossary
: PackageRequirements block
Yes Yes Yes Yes

Using the PackageRequirements block

To specify package requirements for a SubShader or a Pass, you use the PackageRequirements block. ShaderLab supports a single PackageRequirements block per SubShader or Pass, but each block can specify multiple package requirements.

Note: if you provide a PackageRequirements block, it must come before all other declarations inside the SubShader or Pass.

Signature Function
PackageRequirements{ [requirement definition]} Defines the package requirements for the Pass or SubShader.

There are multiple ways to declare package requirements. Each one provides a different behavior. They are:

  • “<package name>”: Specifies that the SubShader or Pass works with any version of the package.
  • “<package name>”: “<version restrictions>”: Specifies that the SubShader or Pass only works with a subset of package versions.
  • “<package name>”: “unity=<version restrictions>”: Specifies that the SubShader or Pass only works with a subset of Unity versions and requires a package with the given name.
  • “unity”:“<version restrictions>”: Specifies that the SubShader or Pass only works with a subset of Unity versions.

Version restrictions define a set of version ranges. If the installed version of a required package is not inside any of the ranges, the package requirement is not met. Similarly, if a requirement specifies a set of Unity version restrictions, the same applies to the current version of Unity. For information about the syntax of version restrictions, see Version syntax.

If a SubShader or Pass declares package requirements that the project does not meet, Unity excludes the SubShader or Pass from further processing and compilation. This happens if the project does not include the required packages, or includes them but not with a compatible version. If a shader does not contain a single SubShader that meets the requirements, or if no SubShader contains Passes that meet the requirements, the Console windowA Unity Editor window that shows errors, warnings and other messages generated by Unity, or your own scripts. More info
See in Glossary
shows a warning message.

Note: The PackageRequirements block must come before all other declarations inside the SubShader or Pass.

Version syntax

In ShaderLab’s package requirements, a version uses the major.minor or major.minor.patch format. If you only use major.minor, Unity uses 0 for the patch. Package versions can also include a -preview or a -preview.n postfix where -preview is equivalent to -preview.0. Preview versions come before non-preview versions, so 1.2.3-preview.4 comes after 1.2.2 but before 1.2.3.

There are multiple ways to specify a version range. Each one provides a different behavior. They are:

  • <version>: Includes the version you enter and all versions after that. For example, 1.2.3 includes all versions starting with 1.2.3;
  • [<version>]: Specifies the exact version. For example, [1.2.3] only includes version 1.2.3;
  • [<version1>,<version2>]: Specifies a range between <version1> and <version2>. Using square brackets and round brackets causes the range to include or exclude to version respectively. The opening bracket affects <version1> and the closing bracket affects <version2>. Example: [1.2.3,2.3.4) includes all versions from 1.2.3 to 2.3.3.

You can also specify sets of version ranges for a single package. To create a set of version ranges from individual ranges, use a semicolon as a separator. For example, [2.0,3.4.5];[3.7];4.0 includes versions from 2.0.0 to 3.4.5, version 3.7.0, and version 4.0.0 and above.

When you set the versions for a package, be aware of the following:

  • Versions, version ranges, and sets of version ranges cannot contain any extra characters.
  • Version ranges cannot be empty.
  • Sets of version ranges cannot have intersections.
  • Package requirements are sensitive to whitespace.

If the syntax does not adhere to the above, the version restriction is invalid. For more information on what happens with invalid package requirements, see Error checking.

Example

The following code example shows how to specify package requirements in both a SubShader and a Pass. The SubShader declares a single package requirement for a package called “com.my.package” and works with any version of this package starting from 2.2.0. The SubShader has two Passes:

  • The first Pass requires:

  • The Universal Render Pipeline package with a version between 10.2.1 and 11.0.

  • The Text MeshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
    See in Glossary
    Pro package with a version of 3.2 or above.

  • The second Pass requires:

  • The High-Definition Render Pipeline package with a version between 8.0 to 8.5

Shader "Examples/ExampleShader"
{
    SubShader
    {
        PackageRequirements
        {
            "com.my.package": "2.2"
        }
        Pass
        {
            PackageRequirements
            {
                "com.unity.render-pipelines.universal": "[10.2.1,11.0]"
                "com.unity.textmeshpro": "3.2"
            }

            // The rest of the code for the Pass
        }
        Pass
        {
            PackageRequirements
            {
                "com.unity.render-pipelines.high-definition": "[8.0,8.5]"
            }

            // The rest of the code for the Pass

        }
    }
}

Error checking

If you define package requirements that can never be satisfied, the shader import process fails with an error. This section provides examples of the most common invalid package requirement definitions.

Malformed versions or empty package name

PackageRequirements {
  "com.some.package.x": "[10.2.1,9.0]"        // Error, empty version range
  "com.some.package.y": "[10.2.1.9,11.0]"     // Error, incorrect version format
  "com.some.package.z": "[2.3,3.5],[3.0,4.0]" // Error, ranges intersect
  "com.some.package.z" : "[10.2.1,11.0]"      // Error, extra whitespace after the package name
  "" : "[2.3.4,3.4.5]"                        // Error, no package name provided
}

Multiple dependencies on the same package

PackageRequirements {
  "com.some.package.x": "[10.2.1,11.0]"
  "com.some.package.x": "[11.2.1,12.0]" // Error, dependency on "com.some.package.x" declared twice
  "unity" : "2021.2"
  "unity" : "2021.3" // Error, dependency on Unity version declared twice
}

Conflicting dependency declarations

PackageRequirements {
  "com.some.package.x": "unity=[2021.2.1,2021.3.3]"
  "unity" : "2021.2"    // Error: Unity version requirement cannot be declared on a package and on its own simultaneously
}

Conflicting dependency declarations between SubShaders and Passes

SubShader {
  PackageRequirements {
    "com.some.package.x": "[2.3.4,3.4.5]"
    "com.some.package.z": "[1.1,3.2]"
    "unity": "2021.2"
  }
  Pass {
    PackageRequirements {
      "com.some.package.y": "[1.2.2,2.5]"               // Fine, SubShader doesn’t declare a dependency on "com.some.package.y"
      "com.some.package.z": "[2.0,3.1]"                 // Fine, SubShader dependency intersects the provided version range
      "com.some.package.x": "[1.1.1, 2.2.2]"            // Error, SubShader version range for "com.some.package.x" does not intersect the provided range
      "com.some.package.w": "unity=[2021.2.1,2021.2.5]" // Fine, SubShader dependency intersects the provided version range
      "com.some.package.u": "unity=[2020.2.1,2020.2.5]" // Error, SubShader Unity version range does not intersect the provided range
    }
  }
}
ShaderLab: adding shader programs
ShaderLab: commands