ShaderVariantCollection best practises?

ShaderVariantCollection best practises?

https://forum.unity.com/threads/shadervariantcollection-best-practises.455447/

I just learned that some framerate spikes were being caused by shader variants being copied to the GPU only when they are used for the first time. The solution is to use Shader Variant Collections. The Unity docs are here: https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.html I think this thread is part of the history.

In my case it was when calling SetActive() on a bunch of objects at the same time, I was getting a very long frame. Drilling down into the profiler, I saw that all the time in that long frame was spent in Shader.EditorLoadVariant. So after learning about Shader Variant Collections, I can do this to create a collection and preload it at boot, preventing the glitch runtime:
 
  • Run the game to the point where all the required shaders are being used.
  • Edit->Project Settings->Graphics
  • Scroll to the Shader preloading section at the bottom
  • Hit Clear <- I learned through trial and error you need to do this. I think Unity adds to the Currently Tracked count everytime it sees a shader. So when I was doing a test with a new blank project, imported Effects package, it was saying it was tracking 37 variants, even with nothing in the scene.
  • Save to asset
  • Add that asset to the Preloaded shaders array

That all works fine. My questions are:
 
  • How is this managed in a production environment?
  • Does anyone automate the creation of the Shader Variant Collections? Or do you have to manually go into each scene or level, clear the tracked, then create a new asset? I can see that getting very tedious and be error prone as new shaders and materials are added to a scene.
  • What about subtle variations from one level to another? Rather than having a separate collection for each level/scene, they could be combined to create a collection that covers all cases.
  • Do shader variants ever get unloaded from GPU? So that when used again, do they get recompiled? It appears not from my testing.

Yep I have the same questions as you.

I found this. Seems to let us load \ unload shader variants on demand. So would be useful when certain levels need certain shaders.
https://docs.unity3d.com/ScriptReference/ShaderVariantCollection.Add.html

Just gotta figure out how to use it now lol.

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值