Shader Graph最佳实践 (Best practices for Shader Graph - Unite 2018 洛杉矶)

ppt:   https://www.slideshare.net/unity3d/best-practices-for-shader-graph

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

         "使用Unity 2018中的Shader Graph,创建功能强大且美观的着色器从未如此简单。 但是,有了强大的力量,责任就大了。 此中级会话将探索渲染性能和着色器创建工作流的最佳实践。 我们将介绍在幕后发生的事情,分享提示以避免常见的陷阱,并突出显示在不久的将来Shader Graph准备黄金时段发布的内容。

Charles Sanglimsuwan  -  Unity Technologies"

视频: 

https://www.bilibili.com/video/av56852704?from=search&seid=17853366928719766269

https://www.youtube.com/watch?v=Y6WfgFI5H90

今天要介绍的内容

Motivations初衷

Demo

Shader Graph的工作原理

Choosing the right Master Node选择正确的主节点

Optimizations优化

Workflow工作流程

What’s next下一步是什么

 

初衷:

image.png

      可以解放程序,Shader可以大众化了。     

 

历史简介

早期工作于2017年5月开始

在Unity 2018.1中发布的预览包

自那以来有23次更新

 

 

Shader Graph的工作原理:

可编程脚本的渲染管道Scriptable Render Pipeline

Shader Graph 需要SRP

高清渲染管道(HDRP)

轻量级渲染管道(LWRP)

VR轻量级渲染管道(VR LWRP)

 

 

Shader Generation Process着色器生成过程

每个Render Pipeline都有一个着色器后端shader backend

着色器后端包括支持的Master Nodes的 subshader generators

并非每个RP都支持所有主节点Master Nodes

 

Subshader Generators

每个主节点Master Node都有一个每个管道模板per-pipeline template

模板有很多注入点injection points

遍历您的Shader Graph,注入点将替换为节点值node values

最终结果是着色器(用ShaderLab编写)

 

 

Shader Anatomy着色器解剖

image.png   

image.png

image.png

image.png

 

          Shader Graph  和 跟生成的Code的对应关系: 

image.png

 

image.png

 

image.png

 

image.png

             代码注入

image.png

 

image.png

 

 

 

Choosing the right Master Node选择正确的主节点

        主节点概述

image.png

PBR是最通用的节点   PBR is the most versatile node

仅在适用且必要时使用HD Lit         Use HD Lit only when applicable and necessary

考虑使用Unlit进行粒子效果,UI等        Consider Unlit for particle effects, UI, etc

          现在他们官方建议使用PBR 原型,功能很多。 

 

PBR Master Node

Works with all Render Pipelines适用于所有渲染管道

Good starting point for prototyping原型设计的良好起点

Feature ready for many scenarios功能已准备好用于许多场景,  遮挡,自发光等

 

 

Unlit Master Node

Works with all Render Pipelines适用于所有渲染管道

Very lightweight非常轻巧

Good for effects, vertex modifications适用于效果,顶点修改

可以添加自定义照明:Possible to add custom lighting:

1.创建节点以根据所需的着色模型计算光照(例如,Blinn-Phong)Create nodes to calculate lighting from desired shading model (e.g., Blinn-Phong)

2.将输出颜色连接到颜色端口 Connect output color to the Color port

image.png

 

 

 

HD Lit Master Node

 Compatible only with HDRP仅与HDRP兼容

More features than PBR比PBR更多的功能

Easy to add needless complexity容易增加不必要的复杂性

image.png

 

 

 

Unity Optimizations

Built-in Optimizations内置优化

1. Where ever possible, Shader Graph will not add unused features to output shader尽可能,Shader Graph不会将未使用的功能添加到输出着色器

 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着色器编译器将编译无操作功能

 

Optimization Example: Coat Mask

         如果值为零,则输出ShaderLab代码中不会出现CoatMask代码

image.png

 

Optimization Example: Metallic

       如果Metallic设置为零,那么该着色块将由着色器编译器compiled out

image.png

 

 

Optimization Example: Emission

        如果Emission设置为Black,并在Material中禁用,则该着色块将由着色器编译器compiled out

image.png

image.png

 

 

Draw Call Batching

 GPU instancing is supported in shaders   着色器支持GPU实例化

 Needs to be enabled on the material        需要在材料上启用

 Requires hardware support                     需要硬件支持

 All generated shaders are compatible with the SRP Batcher (2018.3) 所有生成的着色器都与SRP Batcher兼容(2018.3)

image.png

 

 

SRP Batcher

 New batching solution, designed for SRP      专为SRP设计的新batching 解决方案

 Improved rendering performance                  改进了渲染性能

 Works with SkinnedMeshRenderers!                 适用于SkinnedMeshRenderers!

 Requires per-material parameters to be stored in a constant buffer    要求将每个材料参数存储在常量缓冲区中

 

 

Render Faster with the SRP Batcher

image.png

 

 

 

Graph Optimizations

        Profiler Performance  First 

image.png

 

 

Node Decimation

 Eliminate no-op nodes               消除无操作节点

 Bake values back into the textures          将值烘焙回纹理

 Examples: gradients, noise                      示例:渐变,噪音

 Combine nodes when possible                尽可能组合节点

 

Example: Texture Modification

image.png

          这个乘法可以删除, 通过增加 亮度的基础纹理。 每帧的数学运算就可以减少了。 

 

 

Example: Power         光线衰减效果    或者某种类似玻璃的表面

image.png

image.png

image.png

            多个节点  弄成一个节点。 

image.png

 

 

 

Data Formats    数据格式

 Each node has a memory cost        每个节点都有内存成本,   降低Shader内存成本,有助于GPU带宽

 Use smaller data structures when possible    尽可能使用较小的数据结构

 Modifying precision                                修改精度

 Requires code modification               需要修改代码

 

修改精度: 

image.png

 

 

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                        尽可能选择常量

 

 

Example: Reciprocal    Reciprocal

image.png

Fast mode requires Shader Model 5

 

 

Example: Properties

image.png

           内联节点   可以暴露在材质Inspector (属性节点), 带来了很大的方便,  但是要注意转换属性节点消除了着色器编译优化的可能性。          属性很好 对于原型设计,   告别原型阶段,  希望将属性节点转换回内联节点, 这将允许着色器编译器生成更好的优化代码

 

 

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   在主节点菜单中复制或查看着色器代码

image.png

 

 

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编辑这些节点会快得多

 

Workflow Summary工作流程摘要

1. Prototype with PBR Master Node使用PBR主节点的原型

 Move to HD Lit if necessary必要时才移至HD Lit

2. Start optimization开始优化

1. Optimize nodes优化节点

2. Convert properties to inline values将属性转换为内联值

3. Near the end of the project, start hand optimizing shaders在项目结束时,开始优化着色器

 

 

Custom API

 Create your own nodes创建自己的节点

 Examples: specialized noise functions, custom lighting, etc.示例:专门的噪音功能,定制照明等。

 Full documentation available on GitHubGitHub上提供完整的文档

 

 

 

Vertex Modification      顶点修改

 Low-impact on CPU performance         对CPU性能影响很小

 Good for animating foliage, water, or cloth       适用于动画树叶,水或布料

 

 

What’s next

路线图

2019.1

First official, verified release首次正式,经过验证的版本

完全彻底改造Node API

关键错误修复,UX改进

 

 

Available Resources

 Plentiful samples abound:

Andy Touch

Keijiro Takahashi

 Video tutorials on YouTube

 Source available on GitHub

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值