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 Type | Description |
---|---|
RenderState | Corresponds to the engine's RenderState API and can be directly configured in ShaderLab. |
Structs | Function similarly to GLSL structs and are also used for declaring Varying and Attribute variables. See documentation for details. |
Functions | Equivalent to global functions in GLSL. |
Single Variables | Used to define Uniform variables in shaders. |
Similar to other programming languages, global variables in ShaderLab
follow scope rules and name overriding principles. Specifically:
Shader
, SubShader
, or Pass
).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.Includes BlendState, DepthState, StencilState, RasterState, and renderQueueType.
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 state {
Enabled: bool;
WriteEnabled: bool;
CompareFunction: CompareFunction.XXX;
}
The CompareFunction enum is identical to the engine API.
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 state {
CullMode: CullMode.XXX;
DepthBias: float;
SlopeScaledDepthBias: float;
}
The CullMode enum is identical to the engine API.
RenderQueueType = Transparent;
RenderQueueType = Opaque;
RenderQueueType = AlphaTest;
For detailed information about these render states, refer to the documentation.
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;
};
...
}
}
...
}
Equivalent to GLSL syntax. All Uniform variables in ShaderLab
are declared using global single-variable syntax:
[lowp/mediump/highp] mat4 uMVPMatrix;