With Shader Graph in Unity 2018, creating powerful and beautiful shaders has never been easier. But with great power, comes great responsibility. This intermediate-level session will explore best practices for rendering performance and shader creation workflow. We'll cover what happens under the hood, share tips to avoid common pitfalls, and highlight what to look for as Shader Graph readies for prime-time release in the near future.
Charles Sanglimsuwan - Unity Technologies
11. 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
Each Master 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
29. Unlit Master Node
Works with all Render Pipelines
Very lightweight
Good for effects, vertex modifications
29
30. Unlit Master Node
Possible to 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
33. Built-in Optimizations
1. Where ever 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: Coat Mask
34
No CoatMask code will be present in the output
ShaderLab code if the value is zero
36. Optimization Example: Emission
36
If the 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. Where ever 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
40. SRP Batcher
New batching solution, designed for SRP
Improved rendering performance
Works with SkinnedMeshRenderers!
Requires per-material parameters to be stored in a constant buffer
40
41. Render Faster with the SRP Batcher
41
0
5
10
15
20
25
30
35
Scene: 1024 materials Scene: 1 material
RenderTime(ms)
5.26x Faster 1.27x Faster
SRP
Legacy
Lower is better
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
51
53. 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
55. 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
57. 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. Prototype with 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