Memory Unity Built-In Shader surge caused by the operation

  Use a PC project in a large number of shaders, and use the built-in Standard Shader, when the editor is running, everything is good, only about running memory 1G, but after performing AssetBundle package, EXE running memory 20G have not soared to enter the scene, is simply inconceivable.

  Look SVN art people added SpeedTree, all kinds of flowers and trees, taking into account the shader compiler is not the problem of shader variants lead to too much of it all at the beginning of the first Nature built-in shader added to GraphicSettings go inside the AlwaysIncludedShaders

  After the addition of such a scenario can open, run memory is still racing to 12G ...... developerment build connections to the Profiler view, just ShaderLab accounted for 5.7G ... So it really was caused by the shader, then check the resources in each Shader Asset discovery

Shader not reuse the same on each object was repeated compilation !!! 

(Note that the display size of the shader only reference size, the actual size of the compiler are considered to ShaderLab go inside)

  I guess this will not be because each object variant runtime compiler confusion caused is considered to be the only variant, resulting in each object has been compiled? So run scene in the editor, and then put all variants are automatically recorded, automatically load try running time.

(After running scene creation Preload Shader Variants Collection in the operating state)

  However, there is nothing with, or as memory.

  Try to Standard Shader added to AlwaysIncludedShaders go inside, with official results said the same, when Build Exe on the card is dead, tens of minutes that did not respond, this can not be added to the list.

  Unity official forum to find it, no one reply, find an old post, which has said:

https://forum.unity.com/threads/standard-shader-duplicated-in-asset-bundle-build.593248/

  Unity团队的这个人叫我们自己把Built-In Shader下载了放到工程里面去, 替换掉原来使用的Standard Shader, 可是为什么? 也不说明原因, 然后后面也没有什么有用的回复了, 

这不是多此一举吗? 所以根据他的说法, 我猜测在使用AssetBundle时, Built-In Shader的封包方式应该跟未命名assetBundleName 的资源一样, 哪个包需要它, 它就被封到哪个包里去, 

然后在实例化的时候, 直接从那个包里对shader读取然后编译, 因为很多材质是被交叉使用的, 很多材质都单独成包, 所以会造成明明是相同的shader并且变体都一样仍然被多次编译的BUG.

  

  既然这样猜测了, 那就实测一下吧, 新建一个工程, 拖进去一些建筑之类的, 首先测试Built-In 的Standard Shader.

 (场景)(材质, 每个都单独成包)

  因为材质都单独成包了, 所以运行时应该Standard Shader应该会每个材质对应一次编译

(每个单独包还挺大)

  这里看到了ShaderLab相当大, 并且还真是每个编译的Standard Shader 对应一个材质... 查看文件的话, 每个材质把Shader封进去了之后, 达到98KB大小, 非常大.

总之就是最坑人的情况 : 文件又大, 运行又占内存, 运行时编译又花时间!!!

 

  接下来我把所有材质封到一个包里, 这样理论上来说所有用到Standard Shader 的材质都封一个包, 也就是运行时根据变体数量来编译, 不会进行重复编译了吧.

 (全都在standard这个包里了)

 (打包后运行跟预想一样, 感觉没有重复编译了)

 (明显这些同一个包里的材质共用了一个编译后的Shader)

(所有材质打成的包, 只有206KB)

不信的话使用解包软件打开看

 

 

Guess you like

Origin www.cnblogs.com/tiancaiwrk/p/11346375.html