" Graphics performance optimization-Optimizing shader load Times" in the official unity document of thetranslation, e-link. Optimizing Shader Load Timeoptimizing shader load times
Shaders is small programs that execute on the GPU, and loading them can take some time. Each individual GPUs program typically does does take much time to load, but shaders often has a lot of "variants" internal Ly.
Shaders are small programs that execute on the GPU, and it takes some time to load them. Each individual GPU program typically does not take much time to load, but shaders often have many intrinsic "variants."
For example, the standard shader, if fully compiled, ends up being many thousands of slightly different GPU programs. This creates potential problems:
For example, if a standard shader is fully compiled, it will generate thousands of different small GPU programs. This can cause two potential problems:
- Large numbers of these shader variants increase game build time, and game data size.
- Loading large numbers of shader variants during game is slow and takes up memory.
- The large number of changes in these shaders increases the time to pack the game, making the game pack bigger.
- Loading A large number of shader variants in the game is slow and consumes memory.
Shader build Time strippingShader Generation Removal
While building the game, Unity can detect that some of the internal shader variants is not used by the game, and skip the m from build data. Build-time stripping is do for:
When you build a game pack, unity detects that some of the intrinsic shader variants are not being used, and then ignores them. Build-time removal can be used to:
- Individual shader features, for shaders
#pragma shader_feature
. If none of the used materials use a particular variant, then it's not included into the build. Seeinternal shader variants documentation. Out of built-in shaders, the standard shader uses this.
- Shader variants to handle FOG and lightmapping modes no used by any of the scenes is not included into the game data. See Graphics Settings If you want to override this behavior.
- < Span style= "Color:rgb (51,51,255); font-family: ' Open Sans ', Sans-serif; font-size:14px; line-height:21.6000003814697px; Background-color:initial "> Individual shader features, shaders using the #pragma shader feature. If a variant is not used by any of the materials used, it is not hit in the build. Refer to intrinsic shader document , the standard shader in the built-in shader is used this way.
- Shader variants that handle fog and light mapping patterns that are not used by any scene, and are not packaged into game data. If you want to change this behavior, refer to the document graphics settings.
Combination of the above often substantially cuts down on shader data size. For example, a fully compiled standard shader would take several hundred megabytes, but in typical projects it often ends Up taking just a couple megabytes (and are often compressed further by the application packaging process).
The combination of the above methods can greatly reduce the size of the shader data. For example, a fully compiled standard shader has hundreds of megabytes, but it is often only a few megabytes in a typical project (and is further compressed when the app is packaged).
Default Unity Shader loading behaviorDefault Unity shader load Behavior
Under all default settings, Unity loads the Shaderlab Shader object to memory, but does not create the internal Shader V Ariants until they is actually needed.
By default, unity loads Shaderlab shader objects into memory, but interior The shader variant is created only when it is actually used.
this means that shader variants that is included into the game build can still potentiall Y is used, but there's no memory or load time cost paid until they is needed. For example, shaders all include a variant to handle point lights with shadows, but if you never end up using a point L Ight with Shadows in your game, then there's no point in loading this particular variant.
This means that the shader variant in the game pack may be used, but there is no takes up memory or consumes time to load. For example, a shader often contains a variant that handles the shading of a point light, but if you never have a light shadow in your game, then loading this variant is not necessary.
one downside of this default behavior, however, was a possible hiccup for when some shader Variant is needed for the first time-since a new GPU program code have to being loaded into the graphics driver. This was often undesirable during gameplay, so Unity has shadervariantcollection assets to help solve.
However, one drawback of this default behavior is that some shader variants may be stuck when they are first used (hiccup)-Because a new GPU program code needs to be loaded into the graphics driver. This is unacceptable in the game, so unity has a resource called shadervariantcollection to help solve this problem.
Shader Variant CollectionsShader Variant Group
shadervariantcollection is a asset that's basically a list of shaders, and for each Of them, a list of Pass types and shader keyword combinations to load.
Shadervariantcollection basically is a shader list resource that loads a list each time it is combined by a channel type and a shader keyword.
Shader Variant Collection InspectorShader Variant Group Viewer
To and from creating these assets based on actually used shaders and their variants, the editor can track which shaders a nd their variants is actually used. In Graphics Settings, there are a button to create a new shadervariantcollection out of currently tracked shaders, or to CL Ear the currently tracked shader list.
to help create these resources based on the shaders and their variants that are actually used, the editor can track which shaders and their variants are actually used. on the graphical settings viewer for the current trace shader, there is a button to create a new shadervariantcollection, or to clear the list of currently traced shaders.
Creating shadervariantcollection from shaders used by editorcreate Shadervariantcollection from the shader used by the editor
once you had some shadervariantcollection assets, can set for these variants to be AU Tomatically preloaded while loading the game (under Preloaded Shaders list In graphics Settings), or can preload A individual shader variant collection from a script. See shadervariantcollection scripting class.
Once you have some shadervariantcollection resources, you can set these shader variants to automatically preload when the game loads (under the pre-loaded shader list for graphical settings), or you can preload a separate shader variant with a script. Refer to the Shadervariantcollection script class.
See Alsoalso refer to
- Optimizing Graphics performance. Chinese translation: Unity3d-graphics performance optimization.
- Graphics Settings. Graphics settings.
- Shaders Reference. Shader reference.
Unity3d-Graphics performance optimization: Optimizing Shader load times