The document outlines best practices for using Shader Graph, a tool in Unity for creating shaders, detailing its history, functionalities, and optimization techniques. Key aspects include selecting the appropriate master node for different use cases and methods for optimizing shader performance, including built-in features and manual adjustments. The roadmap for future developments and available resources for further learning are also discussed.
Shader Generation Process
Each Render Pipeline has a shader backend
Shader backends include subshader generators for supported Master
Nodes
Not all Master Nodes supported by every RP
11
12.
Subshader Generators
EachMaster Node has a per-pipeline template
Templates have many injection points
Your Shader Graph is traversed, and injection points are replaced with
node values
Final outcome is a shader (written in ShaderLab)
12
Unlit Master Node
Works with all Render Pipelines
Very lightweight
Good for effects, vertex modifications
29
30.
Unlit Master Node
Possibleto add custom lighting:
1. Create nodes to calculate lighting
from desired shading model (e.g., Blinn-Phong)
2. Connect output color to the Color port
30
31.
Master Node Review
PBR is the most versatile node
Use HD Lit only when applicable and necessary
Consider Unlit for particle effects, UI, etc.
31
Built-in Optimizations
1. Whereever possible, Shader Graph will not add unused features to
output shader
Changing the default value will mark the feature active
Connecting any node will mark the feature active
2. The shader compiler will compile out no-op features
33
34.
Optimization Example: CoatMask
34
No CoatMask code will be present in the output
ShaderLab code if the value is zero
Optimization Example: Emission
36
Ifthe Emission is set to Black, and disabled in
Material, that code block will be compiled out by
the shader compiler
37.
Built-in Optimizations
1. Whereever possible, Shader Graph will not add unused features to
output shader
Changing the default value will mark the feature active
Connecting any node will mark the feature active
2. The shader compiler will compile out no-op features
37
38.
Draw Call Batching
GPU instancing is supported in shaders
Needs to be enabled on the material
Requires hardware support
All generated shaders are compatible with the SRP Batcher (2018.3)
38
SRP Batcher
Newbatching solution, designed for SRP
Improved rendering performance
Works with SkinnedMeshRenderers!
Requires per-material parameters to be stored in a constant buffer
40
Reduce Math Operations
Multiply scalar values before vector values
Prefer blending results, instead of branching
Research impact of certain math operations
Prefer constants when possible
51
Reduce Math Operations
Multiply scalar values before vector values
Prefer blending results, instead of branching
Research impact of certain math operations
Prefer constants when possible
53
Workflow: Manual Optimizations
Hand-optimizing shaders is a one-way path
Don’t manually optimize too early
Copy or view shader code in Master Node menu
55
Workflow: Faster Iterations
Nodes not connected to Master Node aren’t evaluated
Can keep them in view for quick iterations
Create a Preview node that isn’t attached to the Master Node
Editing these nodes will be much faster
57
58.
Workflow Summary
1. Prototypewith PBR Master Node
Move to HD Lit if necessary
2. Start optimization
1. Optimize nodes
2. Convert properties to inline values
3. Near the end of the project, start hand optimizing shaders
58