天天看點

Optimizing Shader Load Time 優化Shader加載時間 性能系列6

Optimizing Shader Load Time 優化Shader加載時間

本文檔主要是對Unity官方手冊的個人了解與總結(其實以翻譯記錄為主:>)

僅作為個人學習使用,不得作為商業用途,歡迎轉載,并請注明出處。

文章中涉及到的操作都是基于Unity2018.3版本

參考連結:https://docs.unity3d.com/Manual/OptimizingShaderLoadTime.html

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.

着色器是在GPU上執行的小程式,加載它們需要一些時間。每個單獨的GPU程式通常不會花費太多的時間去加載,但是着色器内部通常有很多“變體”。

For example, the Standard shader, if fully compiled, ends up being many thousands of slightly different GPU programs. This creates two potential problems:

例如,标準(Standard ) 着色器,如果完全編譯,最終會變成成千上萬個稍微不同的GPU程式。這就産生了兩個潛在的問題:

  • 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.

    在遊戲中加載大量的着色器變體會很慢,并且會占用記憶體。

Shader build time stripping

剝離着色器建構時間

While building the game, Unity can detect that some of the internal shader variants are not used by the game, and skip them from build data. Build-time stripping is done for:

在建構遊戲時,Unity可以檢測到一些内部着色器變體沒有被遊戲使用,并從建構資料中跳過它們。剝離建構時間是做:

  • Individual shader features, for shaders that use #pragma shader_feature. If none of the used materials use a particular variant, then it is not included into the build. See internal shader variants documentation. Out of built-in shaders, the Standard shader uses this.

    獨立的着色器特性,着色器使用#pragma shader_feature。如果使用過的材質沒有使用特定的變體,那麼它就不被包含在建構中。請參閱内部着色器變體文檔。除了内置的着色器,标準的着色器也使用這個。

  • Shader variants to handle Fog and Lightmapping modes not used by any of the scenes are not included into the game data. See Graphics Settings if you want to override this behavior.

    如果所有場景都未使用霧和光照圖模式,那麼在遊戲資料中将不會包含相應的着色器變體。如果要重寫此行為,請檢視圖形設定。

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 is often compressed further by the application packaging process).

以上的組合通常會大大減少着色器資料的大小。例如,一個完全編譯的标準着色器需要幾百兆位元組,但是在典型的項目中,它通常隻需要幾兆位元組(并且經常被應用程式打包過程進一步壓縮)。

Default Unity shader loading behavior

預設的Unity着色器加載行為

Under all default settings, Unity loads the shaderlab Shader object into memory, but does not create the internal shader variants until they are actually needed.

在所有的預設設定下,Unity将shaderlab Shader對象加載到記憶體中,但是直到真正需要時才建立内部的Shader變體。

This means that shader variants that are included into the game build can still potentially be used, but there’s no memory or load time cost paid until they are needed. For example, shaders always include a variant to handle point lights with shadows, but if you never end up using a point light with shadows in your game, then there’s no point in loading this particular variant.

這意味着包含在遊戲建構中的着色器變體仍然可以被使用,但是在需要它們之前不需要支出記憶體或加載時間。例如,着色器總是包含一個變體來處理帶有陰影的點光源,但是如果你在遊戲中從未使用過帶有陰影的點光源,那麼就沒有加載這個特定的變體。

One downside of this default behavior, however, is a possible hiccup for when some shader variant is needed for the first time - since a new GPU program code has to be loaded into the graphics driver. This is often undesirable during gameplay, so Unity has ShaderVariantCollection assets to help solve that.

不過,這種預設行為的一個缺點是,當首次需要某些着色器變體時,可能會出現峰值——因為必須将新的GPU程式代碼加載到圖形驅動程式中。這在遊戲運作時中是不受歡迎的,是以Unity有ShaderVariantCollection資産來幫助解決這個問題。

Shader Variant Collections

Shader變體集

ShaderVariantCollection is an asset that is basically a list of Shaders, and for each of them, a list of Pass types and shader keyword combinations to load.

ShaderVariantCollection是一個資産,它基本上是一個着色器清單,對于每個着色器,一個通道類型清單和要加載的着色器關鍵字組合清單。

Optimizing Shader Load Time 優化Shader加載時間 性能系列6

To help with creating these assets based on actually used shaders and their variants, the editor can track which shaders and their variants are actually used. In Graphics Settings, there is a button to create a new ShaderVariantCollection out of currently tracked shaders, or to clear the currently tracked shader list.

為了幫助基于實際使用的着色器及其變體建立這些資産,編輯器可以跟蹤哪些着色器及其變體被實際使用。在圖形設定中,有一個按鈕可以從目前跟蹤的着色器中建立一個新的ShaderVariantCollection,或者清除目前跟蹤的着色器清單。

Optimizing Shader Load Time 優化Shader加載時間 性能系列6

Once you have some ShaderVariantCollection assets, you can set for these variants to be automatically preloaded while loading the application (under Preloaded Shaders list in Graphics Settings), or you can preload an individual shader variant collection from a script.

一旦你有了ShaderVariantCollection的資産,你可以設定這些變體在加載應用程式時自動預加載(在圖形設定的預加載着色器清單下),或者你可以從一個腳本預加載一個單獨的着色器變量集合。

The Preloaded Shaders list is intended for frequently used shaders. Shader variants that are listed there the are loaded into memory for entire lifetime of the application. This may use significant amount of memory for ShaderVariantCollections assets that include large number of variants. To avoid that, ShaderVariantCollection assets should be created at smaller granularity and loaded from a script. One strategy is to record used shader variants for each scene, save them into separate ShaderVariantCollections assets and load them on scene startup.

預加載着色器清單是為經常使用的着色器設計的。這裡列出的着色器變體将在應用程式的整個生命周期内加載到記憶體中。這可能會為包含大量變體的ShaderVariantCollections資産使用大量記憶體。為了避免這種情況,應該以更小的粒度建立ShaderVariantCollection資産,并從腳本加載。一種政策是記錄每個場景中使用的着色器變體,将它們儲存到單獨的ShaderVariantCollections資産中,并在場景啟動時加載它們。

繼續閱讀