找回密码
 立即注册
查看: 426|回复: 0

unity ShaderVariants处理

[复制链接]
发表于 2021-5-10 09:24 | 显示全部楼层 |阅读模式
首先看unity manual给的说明
Optimizing shader variants
Shaders are small programs that execute on the GPU, and loading them can take some time. Each individual GPU program typically does not take much time to load, but shaders often have a lot of “variants” internally.
For example, the Standard shader, if fully compiled, ends up being many thousands of slightly different GPU programs. This creates two potential problems:


    Large numbers of these shader variants increase game build time, and game data size.Loading large numbers of shader variants at runtime is slow and takes up memory.
大概的意思是:shader是运行在GPU上面的programs,并且加载它们会消耗一些时间,单个并不需要消耗很长的时间,但是shader在compiled以后会产生很多的变体(可以理解成因为一些属性的不同,产生出不同的执行片段);带来的问题就是消耗build的时间还有资源大小,在运行时加载的话会影响游戏的运行速度和内存的大小。
所以我们希望做到的就是:将所有用到的shadervariants 收集起来在进入游戏前预加载
shader变体(shader variants)和 Keywards
我们首先来了解一下shader variants 以及 keywards 是什么东西,以及它是如何产生和定义的
shader variants
You can write shader snippets that share common code, but have different functionality when a given keyword is enabled or disabled. When Unity compiles these shader snippets, it creates separate shader programs for the different combinations of enabled and disabled keywords. These individual shader programs are called shader variants.
这么大一长串的意思就是在不同的keywards组合下会产生不同的shader progeams 就称作shader variants
keywards
就是这个东西,然后在shader中可以根据keyward执行不同的逻辑,当不同keywards组合的时候会生成不同的shadervariants
现在了解到它们之间的相互关系,以及会产生的影响,所以我们就要对他们进行一个处理,避免生成好多好多变体,其实也就是把我们工程中所需要用到的shader variants 全部收集起来然后进行预加载

unity为我们提供了一个收集工具:Shader Variant Collections
相当于一个shader variants的容器
在graphics 选项里面有个这个,可以检测当前用到的shader(就只是当前scene里面所有引用到的shader)所生成的变体,然后save为一个Shader Variant Collections
所以还是要按照自己的规则来手动收集,然后在游戏的main里面预加载。
可以参考BDFramework中的生成shaderVariantsCollections的代码
gayhub链接:https://github.com/yimengfan/BDFramework.Core/blob/master/Packages/com.bdframework.core/Editor/AssetEditor/ArtEditor/AssetBundleEditorToolsV2.cs

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-21 02:41 , Processed in 0.094405 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表