Unity Shader Optimization

Unity Shader Optimization

https://www.jianshu.com/p/429d91e63103

(1) a step () instead of other conditions if else statements: (edge ​​= step (edge, _Edge); represents if (edge ​​<= _ Edge) edge = 1, else edge = 0)

After, if this pixel is determined as an edge, then directly back edge color, it is no longer carried out; in the fragment shader, the normal CPU programmed logic to optimize, for example, if (edge ​​<_EdgeColor) {return _EdgeColor} the operation. However, this optimization for GPU programming is concerned is invalid. Then the fragment shader, each fragment shader operations hundreds of pixels each instruction, a branch to take if some fragments and some fragments without using the other branch, the two branches of all fragments will be executed, but only in each segment the points that should be taken into the register. Further, if / endif higher control operation processes such as overhead (four clock cycles, Gefirce6 series (Nvidia)). Therefore, GPU programming, ifelse, switchcase statements and other conditions are too complex logic is not recommended. Can be replaced with a corresponding step function step () function and the like, so that all threads are exactly the same code execution.

(2) Shader resolve resource optimization . In general, a physical Size Shader resources only a few KB, in memory but also dozens of KB. Therefore, the load efficiency bottleneck Shader resources on its own is not the size of the load, but the content of the Shader resolved. In general, the number of time-consuming CPU load related to its Keyword Shader, the more Keyword, then load the overhead is greater. Keyword Shader number of settings will vary from scene varies. (Note: In the Unity 5.x, Unity default settings according to the scene, Shader

Pass the like to adjust Keyword Shader, for example if the use of Lightmap exists, the corresponding default Keyword open, but not for use Fog items will be directly related to Keyword closed. ) Reduction Keyword can try the following:

minimize shader_feature #pragma multi_compile and keywords like, time-consuming analytical reduced, while reducing the number of shader varaints (Shader variant thereof), thereby reducing the memory footprint.

By skip_variants removed directly related operation in Shader Keyword using the following example:

 

direct removal Fallback options in Shader. For projects using the Mobile Shader, you can try it directly Fallback remove substantially reduce the number of keyword.

using resources loading AssetBundle embodiment, in order to avoid a large number of the same loading Shader is repeatedly loaded and unloaded, can be used: by dependency package, all items in the disengagement and Shader labeled AssetBundle a separate file, with its other AssetBundle establish dependency. Shader's AssetBundle file in after the game starts to load and permanent memory, because a number of types of projects Shader generally ranging from 50 to 100, and each are very small, even if all of the resident memory, the total memory footprint does not exceed 2MB; Prefab after subsequent loading and instantiating, by Unity engines will find the dependencies between the resources corresponding to AssetBundle Shader be directly used without further loading and parsing operations. (Note: For Unity4.x version of AssetBundle Shader loaded after loading is completed and just LoadAll resolve all Shader, but for Unity5.x version, in addition to the implementation of LoadAllAssets operations, but also need to Shader.WarmupAllShaders operation (because Unity5.x version, Shader parsing and CreateGPUProgram operations are split), ensure that the actual needs of Shader variants are fully loaded, avoid during the game hiccups (Caton) compiled Shader caused new version of Unity has been ShaderVariantCollection. WarmUp () instead of Shader.WarmupAllShaders operation.)



Author: not serious move brickwork
link: https: //www.jianshu.com/p/429d91e63103
Source: Jane books
are copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

 

 

 

 

 

 

 

 

 

 

 

发布了64 篇原创文章 · 获赞 36 · 访问量 3万+

Guess you like

Origin blog.csdn.net/kuangben2000/article/details/104086275