SlideShare a Scribd company logo
1 of 68
Download to read offline
1
GenerativeArt–MadewithUnity
Best Practices for
Shader Graph
2
Charles Sanglimsuwan
Developer Relations Engineer
What we'll cover today
 Motivations
 Demo
 How Shader Graph works
 Choosing the right Master Node
 Optimizations
 Workflow
 What’s next
3
Motivations
4
5
Democratize
Development
Solve Hard
Problems
Enable
Success
6
Brief History
 Early work started in May 2017
 Preview package released in Unity 2018.1
 23 updates since
7
How Shader Graph works
8
Scriptable Render Pipeline
 Shader Graph requires SRP
 High Definition Render Pipeline (HDRP)
 Lightweight Render Pipeline (LWRP)
 VR Lightweight Render Pipeline (VR LWRP)
9
The Master Node
10
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
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
Shader Anatomy
13
Properties
Subshader
{
Tags
Pass
{
Material Options
Includes & Defines
Graph Functions
Surface Description Input Struct
Surface Description Output Struct
Surface Description Function
Vertex Input Struct
Vertex Output Struct
Vertex Shader
Fragment Shader
}
}
14
Shader Graph
15
Properties
Subshader
{
Tags
Pass
{
Material Options
Includes & Defines
Graph Functions
Surface Description Input Struct
Surface Description Output Struct
Surface Description Function
Vertex Input Struct
Vertex Output Struct
Vertex Shader
Fragment Shader
}
}
Shader Graph
16
Properties
Subshader
{
Tags
Pass
{
Material Options
Includes & Defines
Graph Functions
Surface Description Input Struct
Surface Description Output Struct
Surface Description Function
Vertex Input Struct
Vertex Output Struct
Vertex Shader
Fragment Shader
}
}
Shader Graph
17
Properties
Subshader
{
Tags
Pass
{
Material Options
Includes & Defines
Graph Functions
Surface Description Input Struct
Surface Description Output Struct
Surface Description Function
Vertex Input Struct
Vertex Output Struct
Vertex Shader
Fragment Shader
}
}
Shader Graph
18
void Unity_Multiply_float (float3 A, float3 B, out float3 Out)
{
Out = A * B;
}
struct SurfaceDescriptionInputs
{
float3 WorldSpaceTangent;
}
struct SurfaceDescription
{
float3 Albedo;
}
SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float _Vector1_1EF2E600_Out = 2;
float3 _Multiply_E1E37A64_Out;
Unity_Multiply_float(IN.WorldSpaceTangent,
_Vector1_1EF2E600_Out.xxx,
_Multiply_E1E37A64_Out)
surface.Albedo = _Multiply_E1E37A64_Out;
return surface;
}
Example: Surface Description
19
void Unity_Multiply_float (float3 A, float3 B, out float3 Out)
{
Out = A * B;
}
struct SurfaceDescriptionInputs
{
float3 WorldSpaceTangent;
}
struct SurfaceDescription
{
float3 Albedo;
}
SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float _Vector1_1EF2E600_Out = 2;
float3 _Multiply_E1E37A64_Out;
Unity_Multiply_float(IN.WorldSpaceTangent,
_Vector1_1EF2E600_Out.xxx,
_Multiply_E1E37A64_Out)
surface.Albedo = _Multiply_E1E37A64_Out;
return surface;
}
Example: Surface Description
20
void Unity_Multiply_float (float3 A, float3 B, out float3 Out)
{
Out = A * B;
}
struct SurfaceDescriptionInputs
{
float3 WorldSpaceTangent;
}
struct SurfaceDescription
{
float3 Albedo;
}
SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float _Vector1_1EF2E600_Out = 2;
float3 _Multiply_E1E37A64_Out;
Unity_Multiply_float(IN.WorldSpaceTangent,
_Vector1_1EF2E600_Out.xxx,
_Multiply_E1E37A64_Out)
surface.Albedo = _Multiply_E1E37A64_Out;
return surface;
}
Example: Surface Description
21
void Unity_Multiply_float (float3 A, float3 B, out float3 Out)
{
Out = A * B;
}
struct SurfaceDescriptionInputs
{
float3 WorldSpaceTangent;
}
struct SurfaceDescription
{
float3 Albedo;
}
SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN)
{
SurfaceDescription surface = (SurfaceDescription)0;
float _Vector1_1EF2E600_Out = 2;
float3 _Multiply_E1E37A64_Out;
Unity_Multiply_float(IN.WorldSpaceTangent,
_Vector1_1EF2E600_Out.xxx,
_Multiply_E1E37A64_Out)
surface.Albedo = _Multiply_E1E37A64_Out;
return surface;
}
Example: Surface Description
22
Properties
Subshader
{
Tags
Pass
{
Material Options
Includes & Defines
Graph Functions
Surface Description Input Struct
Surface Description Output Struct
Surface Description Function
Vertex Input Struct
Vertex Output Struct
Vertex Shader
Fragment Shader
}
}
Shader Graph
23
Properties
Subshader
{
Tags
Pass
{
Material Options
Includes & Defines
Graph Functions
Surface Description Input Struct
Surface Description Output Struct
Surface Description Function
Vertex Input Struct
Vertex Output Struct
Vertex Shader
Fragment Shader
}
}
Shader Graph
Choosing a Master Node
24
Master Node Overview
25
UnlitHD Lit PBR
PBR Master Node
 Works with all Render Pipelines
 Good starting point for prototyping
 Feature ready for many scenarios
26
HD Lit Master Node
 Compatible only with HDRP
 More features than PBR
 Easy to add needless complexity
27
28
Unlit Master Node
 Works with all Render Pipelines
 Very lightweight
 Good for effects, vertex modifications
29
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
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
Unity Optimizations
32
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
Optimization Example: Coat Mask
34
No CoatMask code will be present in the output
ShaderLab code if the value is zero
Optimization Example: Metallic
35
If the Metallic is set to zero, that code block will
be compiled out by the shader compiler
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
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
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
GPU Instancing
39
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
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
Graph Optimizations
42
Node Decimation
 Eliminate no-op nodes
 Bake values back into the textures
 Examples: gradients, noise
 Combine nodes when possible
43
Example: Texture Modification
44
Example: Power
45
46
47
48
Data Formats
 Each node has a memory cost
 Use smaller data structures when possible
 Modifying precision
 Requires code modification
49
50
public enum OutputPrecision
{
@fixed,
@half,
@float
}
private OutputPrecision m_OutputPrecision = OutputPrecision.@float;
Modifying Precision
AbstractMaterialNode.cs
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
Example: Reciprocal
52
Fast mode requires Shader Model 5
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
Example: Properties
54
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: Manual Optimizations
56
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
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
Exploratory
59
Custom API
 Create your own nodes
 Examples: specialized noise functions, custom lighting, etc.
 Full documentation available on GitHub
60
Vertex Modification
 Low-impact on CPU performance
 Good for animating foliage, water, or cloth
61
62
Foliage
63
Animating Water
What’s next
64
Roadmap
2019.1
 First official, verified release
 Completely overhauled Node API
 Key bug fixes, UX improvements
65
Available Resources
 Plentiful samples abound:
 Andy Touch
 Keijiro Takahashi
 Video tutorials on YouTube
 Source available on GitHub
66
Questions?
67
68
Thank you

More Related Content

What's hot

Deferred shading
Deferred shadingDeferred shading
Deferred shading
Frank Chao
 
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
Johan Andersson
 
A Bit More Deferred Cry Engine3
A Bit More Deferred   Cry Engine3A Bit More Deferred   Cry Engine3
A Bit More Deferred Cry Engine3
guest11b095
 
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
henjeon
 
Forward+ (EUROGRAPHICS 2012)
Forward+ (EUROGRAPHICS 2012)Forward+ (EUROGRAPHICS 2012)
Forward+ (EUROGRAPHICS 2012)
Takahiro Harada
 

What's hot (20)

Rendering AAA-Quality Characters of Project A1
Rendering AAA-Quality Characters of Project A1Rendering AAA-Quality Characters of Project A1
Rendering AAA-Quality Characters of Project A1
 
Unreal Open Day 2017 UE4 for Mobile: The Future of High Quality Mobile Games
Unreal Open Day 2017 UE4 for Mobile: The Future of High Quality Mobile GamesUnreal Open Day 2017 UE4 for Mobile: The Future of High Quality Mobile Games
Unreal Open Day 2017 UE4 for Mobile: The Future of High Quality Mobile Games
 
The Rendering Technology of Killzone 2
The Rendering Technology of Killzone 2The Rendering Technology of Killzone 2
The Rendering Technology of Killzone 2
 
Lighting the City of Glass
Lighting the City of GlassLighting the City of Glass
Lighting the City of Glass
 
Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019Cross-scene references: A shock to the system - Unite Copenhagen 2019
Cross-scene references: A shock to the system - Unite Copenhagen 2019
 
제노블레이도 2 ray marching을사용한 구름 표현
제노블레이도 2 ray marching을사용한 구름 표현제노블레이도 2 ray marching을사용한 구름 표현
제노블레이도 2 ray marching을사용한 구름 표현
 
Deferred shading
Deferred shadingDeferred shading
Deferred shading
 
A Scalable Real-Time Many-Shadowed-Light Rendering System
A Scalable Real-Time Many-Shadowed-Light Rendering SystemA Scalable Real-Time Many-Shadowed-Light Rendering System
A Scalable Real-Time Many-Shadowed-Light Rendering System
 
Tales from the Optimization Trenches - Unite Copenhagen 2019
Tales from the Optimization Trenches - Unite Copenhagen 2019Tales from the Optimization Trenches - Unite Copenhagen 2019
Tales from the Optimization Trenches - Unite Copenhagen 2019
 
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
Frostbite Rendering Architecture and Real-time Procedural Shading & Texturing...
 
A Bit More Deferred Cry Engine3
A Bit More Deferred   Cry Engine3A Bit More Deferred   Cry Engine3
A Bit More Deferred Cry Engine3
 
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술Ndc2010 전형규   마비노기2 캐릭터 렌더링 기술
Ndc2010 전형규 마비노기2 캐릭터 렌더링 기술
 
Decima Engine: Visibility in Horizon Zero Dawn
Decima Engine: Visibility in Horizon Zero DawnDecima Engine: Visibility in Horizon Zero Dawn
Decima Engine: Visibility in Horizon Zero Dawn
 
Custom fabric shader for unreal engine 4
Custom fabric shader for unreal engine 4Custom fabric shader for unreal engine 4
Custom fabric shader for unreal engine 4
 
Physically Based Lighting in Unreal Engine 4
Physically Based Lighting in Unreal Engine 4Physically Based Lighting in Unreal Engine 4
Physically Based Lighting in Unreal Engine 4
 
CEDEC 2018 - Towards Effortless Photorealism Through Real-Time Raytracing
CEDEC 2018 - Towards Effortless Photorealism Through Real-Time RaytracingCEDEC 2018 - Towards Effortless Photorealism Through Real-Time Raytracing
CEDEC 2018 - Towards Effortless Photorealism Through Real-Time Raytracing
 
The Rendering Pipeline - Challenges & Next Steps
The Rendering Pipeline - Challenges & Next StepsThe Rendering Pipeline - Challenges & Next Steps
The Rendering Pipeline - Challenges & Next Steps
 
Forward+ (EUROGRAPHICS 2012)
Forward+ (EUROGRAPHICS 2012)Forward+ (EUROGRAPHICS 2012)
Forward+ (EUROGRAPHICS 2012)
 
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
Vertex Shader Tricks by Bill Bilodeau - AMD at GDC14
 
Shadow mapping 정리
Shadow mapping 정리Shadow mapping 정리
Shadow mapping 정리
 

Similar to Best Practices for Shader Graph

mloc.js 2014 - JavaScript and the browser as a platform for game development
mloc.js 2014 - JavaScript and the browser as a platform for game developmentmloc.js 2014 - JavaScript and the browser as a platform for game development
mloc.js 2014 - JavaScript and the browser as a platform for game development
David Galeano
 
new_age_graphics_android_x86
new_age_graphics_android_x86new_age_graphics_android_x86
new_age_graphics_android_x86
Droidcon Berlin
 
thu-blake-gdc-2014-final
thu-blake-gdc-2014-finalthu-blake-gdc-2014-final
thu-blake-gdc-2014-final
Robert Taylor
 

Similar to Best Practices for Shader Graph (20)

5 Ways to Improve Your LiDAR Workflows
5 Ways to Improve Your LiDAR Workflows5 Ways to Improve Your LiDAR Workflows
5 Ways to Improve Your LiDAR Workflows
 
5 Ways to Optimize Your LiDAR Data
5 Ways to Optimize Your LiDAR Data5 Ways to Optimize Your LiDAR Data
5 Ways to Optimize Your LiDAR Data
 
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
Everyday I'm Shuffling - Tips for Writing Better Spark Programs, Strata San J...
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 
Parallel program design
Parallel program designParallel program design
Parallel program design
 
Computer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming IComputer Graphics - Lecture 01 - 3D Programming I
Computer Graphics - Lecture 01 - 3D Programming I
 
mapReduce.pptx
mapReduce.pptxmapReduce.pptx
mapReduce.pptx
 
Skiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in DSkiron - Experiments in CPU Design in D
Skiron - Experiments in CPU Design in D
 
mloc.js 2014 - JavaScript and the browser as a platform for game development
mloc.js 2014 - JavaScript and the browser as a platform for game developmentmloc.js 2014 - JavaScript and the browser as a platform for game development
mloc.js 2014 - JavaScript and the browser as a platform for game development
 
Android RenderScript
Android RenderScriptAndroid RenderScript
Android RenderScript
 
new_age_graphics_android_x86
new_age_graphics_android_x86new_age_graphics_android_x86
new_age_graphics_android_x86
 
CS 354 Pixel Updating
CS 354 Pixel UpdatingCS 354 Pixel Updating
CS 354 Pixel Updating
 
A rendering architecture
A rendering architectureA rendering architecture
A rendering architecture
 
CS 354 Programmable Shading
CS 354 Programmable ShadingCS 354 Programmable Shading
CS 354 Programmable Shading
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
thu-blake-gdc-2014-final
thu-blake-gdc-2014-finalthu-blake-gdc-2014-final
thu-blake-gdc-2014-final
 
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
MITx 6.00.1x Introduction to Computer Science and Programming Using Python - ...
 
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
OpenGL Fixed Function to Shaders - Porting a fixed function application to “m...
 
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeksBeginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
Beginning direct3d gameprogramming09_shaderprogramming_20160505_jintaeks
 
Introduction to Blackfin BF532 DSP
Introduction to Blackfin BF532 DSPIntroduction to Blackfin BF532 DSP
Introduction to Blackfin BF532 DSP
 

More from Unity Technologies

More from Unity Technologies (20)

Build Immersive Worlds in Virtual Reality
Build Immersive Worlds  in Virtual RealityBuild Immersive Worlds  in Virtual Reality
Build Immersive Worlds in Virtual Reality
 
Augmenting reality: Bring digital objects into the real world
Augmenting reality: Bring digital objects into the real worldAugmenting reality: Bring digital objects into the real world
Augmenting reality: Bring digital objects into the real world
 
Let’s get real: An introduction to AR, VR, MR, XR and more
Let’s get real: An introduction to AR, VR, MR, XR and moreLet’s get real: An introduction to AR, VR, MR, XR and more
Let’s get real: An introduction to AR, VR, MR, XR and more
 
Using synthetic data for computer vision model training
Using synthetic data for computer vision model trainingUsing synthetic data for computer vision model training
Using synthetic data for computer vision model training
 
Unity Roadmap 2020: Live games
Unity Roadmap 2020: Live games Unity Roadmap 2020: Live games
Unity Roadmap 2020: Live games
 
Unity Roadmap 2020: Core Engine & Creator Tools
Unity Roadmap 2020: Core Engine & Creator ToolsUnity Roadmap 2020: Core Engine & Creator Tools
Unity Roadmap 2020: Core Engine & Creator Tools
 
How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...
How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...
How ABB shapes the future of industry with Microsoft HoloLens and Unity - Uni...
 
Unity XR platform has a new architecture – Unite Copenhagen 2019
Unity XR platform has a new architecture – Unite Copenhagen 2019Unity XR platform has a new architecture – Unite Copenhagen 2019
Unity XR platform has a new architecture – Unite Copenhagen 2019
 
Turn Revit Models into real-time 3D experiences
Turn Revit Models into real-time 3D experiencesTurn Revit Models into real-time 3D experiences
Turn Revit Models into real-time 3D experiences
 
How Daimler uses mobile mixed realities for training and sales - Unite Copenh...
How Daimler uses mobile mixed realities for training and sales - Unite Copenh...How Daimler uses mobile mixed realities for training and sales - Unite Copenh...
How Daimler uses mobile mixed realities for training and sales - Unite Copenh...
 
How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...
How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...
How Volvo embraced real-time 3D and shook up the auto industry- Unite Copenha...
 
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019QA your code: The new Unity Test Framework – Unite Copenhagen 2019
QA your code: The new Unity Test Framework – Unite Copenhagen 2019
 
Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...
Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...
Engineering.com webinar: Real-time 3D and digital twins: The power of a virtu...
 
Supplying scalable VR training applications with Innoactive - Unite Copenhage...
Supplying scalable VR training applications with Innoactive - Unite Copenhage...Supplying scalable VR training applications with Innoactive - Unite Copenhage...
Supplying scalable VR training applications with Innoactive - Unite Copenhage...
 
XR and real-time 3D in automotive digital marketing strategies | Visionaries ...
XR and real-time 3D in automotive digital marketing strategies | Visionaries ...XR and real-time 3D in automotive digital marketing strategies | Visionaries ...
XR and real-time 3D in automotive digital marketing strategies | Visionaries ...
 
Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...
Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...
Real-time CG animation in Unity: unpacking the Sherman project - Unite Copenh...
 
Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...
Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...
Creating next-gen VR and MR experiences using Varjo VR-1 and XR-1 - Unite Cop...
 
What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019
What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019
What's ahead for film and animation with Unity 2020 - Unite Copenhagen 2019
 
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
How to Improve Visual Rendering Quality in VR - Unite Copenhagen 2019
 
Digital twins: the power of a virtual visual copy - Unite Copenhagen 2019
Digital twins: the power of a virtual visual copy - Unite Copenhagen 2019Digital twins: the power of a virtual visual copy - Unite Copenhagen 2019
Digital twins: the power of a virtual visual copy - Unite Copenhagen 2019
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 

Recently uploaded (20)

Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 

Best Practices for Shader Graph

  • 1. 1
  • 2. GenerativeArt–MadewithUnity Best Practices for Shader Graph 2 Charles Sanglimsuwan Developer Relations Engineer
  • 3. What we'll cover today  Motivations  Demo  How Shader Graph works  Choosing the right Master Node  Optimizations  Workflow  What’s next 3
  • 6. 6
  • 7. Brief History  Early work started in May 2017  Preview package released in Unity 2018.1  23 updates since 7
  • 9. Scriptable Render Pipeline  Shader Graph requires SRP  High Definition Render Pipeline (HDRP)  Lightweight Render Pipeline (LWRP)  VR Lightweight Render Pipeline (VR LWRP) 9
  • 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
  • 14. Properties Subshader { Tags Pass { Material Options Includes & Defines Graph Functions Surface Description Input Struct Surface Description Output Struct Surface Description Function Vertex Input Struct Vertex Output Struct Vertex Shader Fragment Shader } } 14 Shader Graph
  • 15. 15 Properties Subshader { Tags Pass { Material Options Includes & Defines Graph Functions Surface Description Input Struct Surface Description Output Struct Surface Description Function Vertex Input Struct Vertex Output Struct Vertex Shader Fragment Shader } } Shader Graph
  • 16. 16 Properties Subshader { Tags Pass { Material Options Includes & Defines Graph Functions Surface Description Input Struct Surface Description Output Struct Surface Description Function Vertex Input Struct Vertex Output Struct Vertex Shader Fragment Shader } } Shader Graph
  • 17. 17 Properties Subshader { Tags Pass { Material Options Includes & Defines Graph Functions Surface Description Input Struct Surface Description Output Struct Surface Description Function Vertex Input Struct Vertex Output Struct Vertex Shader Fragment Shader } } Shader Graph
  • 18. 18 void Unity_Multiply_float (float3 A, float3 B, out float3 Out) { Out = A * B; } struct SurfaceDescriptionInputs { float3 WorldSpaceTangent; } struct SurfaceDescription { float3 Albedo; } SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN) { SurfaceDescription surface = (SurfaceDescription)0; float _Vector1_1EF2E600_Out = 2; float3 _Multiply_E1E37A64_Out; Unity_Multiply_float(IN.WorldSpaceTangent, _Vector1_1EF2E600_Out.xxx, _Multiply_E1E37A64_Out) surface.Albedo = _Multiply_E1E37A64_Out; return surface; } Example: Surface Description
  • 19. 19 void Unity_Multiply_float (float3 A, float3 B, out float3 Out) { Out = A * B; } struct SurfaceDescriptionInputs { float3 WorldSpaceTangent; } struct SurfaceDescription { float3 Albedo; } SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN) { SurfaceDescription surface = (SurfaceDescription)0; float _Vector1_1EF2E600_Out = 2; float3 _Multiply_E1E37A64_Out; Unity_Multiply_float(IN.WorldSpaceTangent, _Vector1_1EF2E600_Out.xxx, _Multiply_E1E37A64_Out) surface.Albedo = _Multiply_E1E37A64_Out; return surface; } Example: Surface Description
  • 20. 20 void Unity_Multiply_float (float3 A, float3 B, out float3 Out) { Out = A * B; } struct SurfaceDescriptionInputs { float3 WorldSpaceTangent; } struct SurfaceDescription { float3 Albedo; } SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN) { SurfaceDescription surface = (SurfaceDescription)0; float _Vector1_1EF2E600_Out = 2; float3 _Multiply_E1E37A64_Out; Unity_Multiply_float(IN.WorldSpaceTangent, _Vector1_1EF2E600_Out.xxx, _Multiply_E1E37A64_Out) surface.Albedo = _Multiply_E1E37A64_Out; return surface; } Example: Surface Description
  • 21. 21 void Unity_Multiply_float (float3 A, float3 B, out float3 Out) { Out = A * B; } struct SurfaceDescriptionInputs { float3 WorldSpaceTangent; } struct SurfaceDescription { float3 Albedo; } SurfaceDescription PopulateSurfaceData (SurfaceDescriptionInputs IN) { SurfaceDescription surface = (SurfaceDescription)0; float _Vector1_1EF2E600_Out = 2; float3 _Multiply_E1E37A64_Out; Unity_Multiply_float(IN.WorldSpaceTangent, _Vector1_1EF2E600_Out.xxx, _Multiply_E1E37A64_Out) surface.Albedo = _Multiply_E1E37A64_Out; return surface; } Example: Surface Description
  • 22. 22 Properties Subshader { Tags Pass { Material Options Includes & Defines Graph Functions Surface Description Input Struct Surface Description Output Struct Surface Description Function Vertex Input Struct Vertex Output Struct Vertex Shader Fragment Shader } } Shader Graph
  • 23. 23 Properties Subshader { Tags Pass { Material Options Includes & Defines Graph Functions Surface Description Input Struct Surface Description Output Struct Surface Description Function Vertex Input Struct Vertex Output Struct Vertex Shader Fragment Shader } } Shader Graph
  • 24. Choosing a Master Node 24
  • 26. PBR Master Node  Works with all Render Pipelines  Good starting point for prototyping  Feature ready for many scenarios 26
  • 27. HD Lit Master Node  Compatible only with HDRP  More features than PBR  Easy to add needless complexity 27
  • 28. 28
  • 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
  • 35. Optimization Example: Metallic 35 If the Metallic is set to zero, that code block will be compiled out by the shader compiler
  • 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
  • 43. Node Decimation  Eliminate no-op nodes  Bake values back into the textures  Examples: gradients, noise  Combine nodes when possible 43
  • 46. 46
  • 47. 47
  • 48. 48
  • 49. Data Formats  Each node has a memory cost  Use smaller data structures when possible  Modifying precision  Requires code modification 49
  • 50. 50 public enum OutputPrecision { @fixed, @half, @float } private OutputPrecision m_OutputPrecision = OutputPrecision.@float; Modifying Precision AbstractMaterialNode.cs
  • 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
  • 52. Example: Reciprocal 52 Fast mode requires Shader Model 5
  • 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
  • 60. Custom API  Create your own nodes  Examples: specialized noise functions, custom lighting, etc.  Full documentation available on GitHub 60
  • 61. Vertex Modification  Low-impact on CPU performance  Good for animating foliage, water, or cloth 61
  • 65. Roadmap 2019.1  First official, verified release  Completely overhauled Node API  Key bug fixes, UX improvements 65
  • 66. Available Resources  Plentiful samples abound:  Andy Touch  Keijiro Takahashi  Video tutorials on YouTube  Source available on GitHub 66