Global Variable Declaration

In the overview section, we mentioned that the overall structure of ShaderLab consists of three nested modules: Shader, SubShader, and Pass. Each module has its own global variable scope. Within the global scope, you can declare four types of global variables: RenderState, Structs, Functions, and Single Variables.

Variable TypeDescription
RenderStateCorresponds to the engine's RenderState API and can be directly configured in ShaderLab.
StructsFunction similarly to GLSL structs and are also used for declaring Varying and Attribute variables. See documentation for details.
FunctionsEquivalent to global functions in GLSL.
Single VariablesUsed to define Uniform variables in shaders.

Global Variable Scope

Similar to other programming languages, global variables in ShaderLab follow scope rules and name overriding principles. Specifically:

  • Global variables are only visible and valid within the module they are declared in (Shader, SubShader, or Pass).
  • Name Overriding Rule: If a global variable with the same name exists in a Pass module as one in its parent SubShader module, the Pass-level variable will override the SubShader-level variable. The same logic applies between SubShader and its parent Shader module.

RenderState

Includes BlendState, DepthState, StencilState, RasterState, and renderQueueType.

BlendState

BlendState state {  
  Enabled: bool;  
  ColorBlendOperation: BlendOperation.XXX;  
  AlphaBlendOperation: BlendOperation.XXX;  
  SourceColorBlendFactor: BlendFactor.XXX;  
  SourceAlphaBlendFactor: BlendFactor.XXX;  
  DestinationColorBlendFactor: BlendFactor.XXX;  
  DestinationAlphaBlendFactor: BlendFactor.XXX;  
  ColorWriteMask: float // 0xffffffff  
  BlendColor: vec4;  
  AlphaToCoverage: bool;  
}  

The BlendOperation and BlendFactor enums are identical to engine APIs.

DepthState

DepthState state {  
  Enabled: bool;  
  WriteEnabled: bool;  
  CompareFunction: CompareFunction.XXX;  
}  

The CompareFunction enum is identical to the engine API.

StencilState

StencilState state {  
  Enabled: bool;  
  ReferenceValue: int;  
  Mask: float; // 0xffffffff  
  WriteMask: float; // 0xffffffff  
  CompareFunctionFront: CompareFunction.XXX;  
  CompareFunctionBack: CompareFunction.XXX;  
  PassOperationFront: StencilOperation.XXX;  
  PassOperationBack: StencilOperation.XXX;  
  FailOperationFront: StencilOperation.XXX;  
  FailOperationBack: StencilOperation.XXX;  
  ZFailOperationFront: StencilOperation.XXX;  
  ZFailOperationBack: StencilOperation.XXX;  
}  

The CompareFunction and StencilOperation enums are identical to engine APIs.

RasterState

RasterState state {  
  CullMode: CullMode.XXX;  
  DepthBias: float;  
  SlopeScaledDepthBias: float;  
}  

The CullMode enum is identical to the engine API.

renderQueueType

RenderQueueType = Transparent;  
RenderQueueType = Opaque;  
RenderQueueType = AlphaTest;  

For detailed information about these render states, refer to the documentation.

Structs and Functions

Equivalent to GLSL syntax. The code block below demonstrates functions and structs declared in Shader, SubShader, and Pass scopes:

Shader "PlanarShadow" {  
 
  ...  
  // Scope: Entire Shader module  
  mat4 getJointMatrix(sampler2D smp, float index) {  
    float base = index / renderer_JointCount;  
    ...  
    return mat4(m0, m1, m2, m3);  
  }  
  ...  
 
  SubShader "Default" {  
    ...  
    // Scope: "Default" SubShader module  
    vec3 ShadowProjectPos(vec4 vertPos) {  
      vec3 shadowPos;  
      ...  
      return shadowPos;  
    }  
    ...  
 
    Pass "0" {  
      ...  
      // a2v struct is only visible in Pass "0"  
      struct a2v {  
        vec4 POSITION;  
        vec4 JOINTS_0;  
        vec4 WEIGHTS_0;  
      };  
      ...  
    }  
  }  
  ...  
}  

Single Variables

Equivalent to GLSL syntax. All Uniform variables in ShaderLab are declared using global single-variable syntax:

[lowp/mediump/highp] mat4 uMVPMatrix;  

Was this page helpful?