Version: 2020.3
셰이더 에셋
Asynchronous shader compilation

Shader compilation

개요

Every time you build your project, the Unity Editor compiles all the shaders that your build requires: every required shader variant, for every required graphics API.

When you’re working in the Unity Editor, the Editor does not compile everything upfront. This is because compiling every variant for every graphics API can take a very long time.

Instead, Unity Editor does this:

  • When it imports a shader asset, it performs some minimal processing (such as Surface Shader generation).
  • When it needs to show a shader variant, it checks the Library/ShaderCache folder.
  • If it finds a previously compiled shader variant that uses identicial source code, it uses that.
  • If it does not find a match, it compiles the required shader variant and saves the result to the cache.

Shader compilation is carried out using a process called UnityShaderCompiler. Multiple UnityShaderCompiler processes can be started (generally one per CPU core in your machine), so that at player build time shader compilation can be done in parallel. While the Editor is not compiling shaders, the compiler processes do nothing and do not consume computer resources.

The shader cache folder can become quite large, if you have a lot of shaders that are changed often. It is safe to delete this folder; it just causes Unity to recompile the shader variants.

At player build time, all the “not yet compiled” shader variants are compiled, so that they are in the game data even if the editor did not happen to use them.

Different shader compilers

다음과 같이 플랫폼마다 서로 다른 셰이더 컴파일러를 셰이더 프로그램 컴파일에 사용합니다.

  • Platforms that use DirectX use Microsoft’s FXC HLSL compiler.
  • Platforms that use OpenGL (Core & ES) use Microsoft’s FXC HLSL compiler, followed by bytecode translation into GLSL using HLSLcc.
  • Platforms that use Metal use Microsoft’s FXC HLSL compiler, followed by bytecode translation into Metal, using HLSLcc.
  • Platforms that use Vulkan use Microsoft’s FXC HLSL compiler, followed by bytecode translation into SPIR-V, using HLSLcc.
  • Other platforms, such as console platforms, use their respective compilers.
  • 표면 셰이더는 코드 생성 분석 단계에 HLSL 및 MojoShader를 사용합니다.

You can configure various shader compiler settings using pragma directives.

The Caching Shader Preprocessor

Shader compilation involves several steps. One of the first steps is preprocessing. During this step, a program called a preprocessor prepares the shader source code for the compiler.

In previous versions of Unity, the Editor used the preprocessor provided by the shader compiler for the current platform. Now, you can choose whether to use Unity’s Caching Shader Preprocessor, or revert to the previous behavior. Unless you experience problems, you should use the Caching Shader Preprocessor.

The Caching Shader Preprocessor is optimized for faster shader import and compilation; it is up to 25% faster. It works by caching intermediate preprocessing data, so the Editor only needs to parse include files when their contents change. This makes compiling multiple variants of the same shader more efficient. Enabling the Caching Shader Preprocessor has the most noticeable effect when shaders within a project use a large set of common include files.

As well as improved performance, the Caching Shader Preprocessor adds the following features:

  • Limited support for #pragma directives inside conditionals.
  • Support for the #pragma warning directive.
  • Support for the #include_with_pragmas directive, which allows you to put #pragma directives in include files.
  • The Preprocess Only checkbox in the shader Inspector, which lets you view the preprocessed source for that shader asset.

For detailed information on the differences between the Caching Shader Preprocessor and the previous behavior, see the Unity forum: New shader preprocessor.

You can enable or disable the Caching Shader Preprocessor with the Caching Shader Preprocessor checkbox in the Shader Compilation section of the Editor settings window, or with the EditorSettings.cachingShaderPreprocessor API.

빌드 타임 스트리핑

Unity는 게임 빌드 중에 게임에서 사용되지 않는 몇몇 내부 셰이더 배리언트를 찾아 빌드 데이터에서 제외(“스트리핑”)할 수 있습니다. 빌드 타임 스트리핑은 다음을 제거하기 위해 수행됩니다.

  • #pragma shader_feature를 사용하는 셰이더의 경우 Unity는 배리언트가 사용되는지 여부를 자동으로 확인합니다. 빌드의 아무 머티리얼도 배리언트를 사용하지 않는 경우 해당 배리언트는 빌드에 포함되지 않습니다. 내부 셰이더 배리언트 문서를 참조하십시오. 이는 스탠다드 셰이더에서 사용됩니다.
  • Fog 및 Lightmapping 모드를 취급하는 셰이더 배리언트. 어떠한 씬에서도 사용되지 않는 배리언트는 게임 데이터에 포함하지 않습니다. 이 동작을 오버라이드하려면 Graphics 창을 참조하십시오.
  • 또한 수동으로 배리언트를 식별한 후 OnProcessShader API를 사용하여 해당 배리언트를 빌드에서 제외하도록 Unity에 지시할 수 있습니다.

위의 조합을 사용하면 셰이더의 데이터 크기가 상당히 줄어듭니다. 예를 들어 완전히 컴파일된 스탠다드 셰이더는 수백 메가바이트를 차지하지만 일반적인 프로젝트에서는 몇 메가바이트만 차지합니다. 또한 애플리케이션 패키징 과정에서 이 용량은 더욱 압축됩니다.

셰이더 에셋
Asynchronous shader compilation
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961