Version: Unity 6.6 Beta (6000.6)
Language : English
Shader compilation
Reducing shader variants

Compile shaders with the DirectX 12 compiler

By default, Unity compiles shadersA program that runs on the GPU. More info
See in Glossary
using the DirectX 11 (DX11) compiler FXC. To speed up compilation and use the latest shader features on some platforms, use the DirectX 12 (DX12) compiler DXC instead.

Note: DXC in Unity is supported only on Windows platforms using the DX12 graphics API. On Vulkan and Metal platforms, DXC support in Unity is experimental and not ready for production use. DXC isn’t compatible with DX11.

Using DXC has the following advantages over FXC:

  • DXC supports shader model 6 features such as 16-bit data types and wave operations.
  • The shaders compile more quickly.
  • The compiled shaders are smaller.
  • The compiled shaders work better with external GPU debugging and profiling tools such as Microsoft PIX and RenderDoc.

For more information about the different shader compilers, refer to shader compilation.

Enable the DXC compiler for all shaders on Windows platforms

If you build your project for Windows platforms and use the DirectX 12 (DX12) graphics API, you can enable the DXC compiler for all shaders.

Important: This is recommended if you build only for DX12. If you build for both DX11 and DX12, Unity compiles two sets of shaders by using FXC for DX11 and DXC for DX12.

Follow these steps:

  1. Open the Build ProfilesA set of customizable configuration settings to use when creating a build for your target platform. More info
    See in Glossary
    window. For more information, refer to Introduction to build profiles.
  2. Select a Windows platform profile, or create a new one.
  3. Add Graphics Settings. For more information, refer to Build Profiles window reference.
  4. In the Shader Build Settings section, open the Shader Compiler Backend Selection dropdown.
  5. Select Add (+).
  6. Set Graphics API to Direct3D12.
  7. Set Compiler to DirectX 12 Compiler (DXC).

For more information about the DirectX graphics APIs, refer to DirectX.

Use a specific compiler for a shader pass

To use either the FXC or DXC compiler for a specific shader pass in a custom shader on any platform, add one of the following #pragma directives to the shader code:

  • #pragma use_dxc to use the DXC compiler for the shader pass.
  • #pragma never_use_dxc to use the FXC compiler for the shader pass.

To restrict the directive to specific platforms or graphics APIs, add the platform or graphics API name after the directive. For example, #pragma use_dxc d3d12. For a full list of names, refer to Set the graphics API or platform for a shader.

The #pragma directives override the value you set in your build profile.

For more information about #pragma directives, refer to Pass information to shader compilers in HLSL.

Upgrade shaders for the DXC compiler on Windows

Unity’s built-in shaders are already compatible with the DXC compiler.

If you have custom shaders, make the following changes to make sure they’re compatible with the DXC compiler:

  1. If you need to modify a value from a constant buffer, copy it into a local variable first. DXC doesn’t allow you to write directly to constant buffer variables.
  2. Replace the COLOR semantic with SV_Target.
  3. Replace the POSITION semantic with SV_Position.
  4. Replace the VFACE float semantic with the SV_IsFrontFace boolean.
  5. Replace implicit vector truncation statements with explicit statements. For example, instead of float a = uv.xy, use float a = uv.x or float a = uv.y.
  6. If you use Texture2DMS to declare a multisampled texture, make sure you include a sample count. For example Texture2DMS<float4, 4> instead of only Texture2DMS<float4>.
  7. If you declare an unordered access view (UAV) resource such as RWBuffer, add : register(uN) to the declaration, where N is the index in your Graphics.SetRandomWriteTarget call.
  8. Don’t declare resources such as StructuredBuffer in the argument list of a shader method. Declare them outside the method instead.

You might also need to make changes to your shader code based on the following:

  • DXC calculates atan2(0,0) as π ÷ 2, but FXC calculates it as NaN.
  • DXC calculates round(0.5) as 0.0, but FXC calculates it as 1.0 for constants and 0.0 for non-constants.

For a full list of HLSL differences between the two compilers, refer to Porting shaders from FXC to DXC on the Microsoft website.

Additional resources

Shader compilation
Reducing shader variants