Version: 2019.4
Shading language used in Unity
Tipos de dato Shader y precisión

Niveles Objetivo de Compilación Shader

When writing either Surface Shaders or regular Shader Programs, the HLSL source can be compiled into different “shader models”. To allow the use of more modern GPI functionality, you must use higher shader compilation targets.

Note: Using higher shader compilation targets may prevent the shader from working on older GPUs or platforms.

Indicate the compilation target by using the #pragma target name directive or the more specific #pragma require feature … directive. For example:

#pragma target 3.5
#pragma require integers 2darray instancing

Objetivo de compilación predeterminado

De forma predeterminada, Unity compila los shaders en casi el objetivo de soporte más bajo (“2.5”); Entre los modelos shader DirectX 2.0 y 3.0. Algunas otras directivas de compilación hacen que el shader sea automáticamente compilado en un objetivo mayor:

  • Using a geometry shader (#pragma geometry) sets the compilation target to 4.0.
  • Using tessellation shaders (#pragma hull or #pragma domain) sets the compilation target to 4.6.

Any shader not explicitly setting a function entry point through #pragma for geometry, hull or domain shaders will downgrade internal shader capability requirements. This allows non-DX11 targets with broader run-time and feature differences to be more compatible with the existing shader content.

For example, Unity supports tessellation shaders on Metal graphics, but Metal doesn’t support geometry shaders. Using #pragma target 5.0 is still valid, as long as you don’t use geometry shaders.

Supported ‘#pragma target’ names

Aquí está la lista de modelos de shaders compatibles, con un conjunto de capacidades cada vez mayor (y en algunos casos mayores requisitos de plataforma / GPU):

#pragma objetivo 2.0

  • Funciona en todas las plataformas soportadas por Unity. Modelo shader DX9 2.0.
  • Cantidad limitada de instrucciones de aritmética y textura; 8 interpoladores; Sin muestreo de textura de vértices; Sin derivados en shaders de fragmentos; No hay muestreo de textura LOD explícito.

#pragma objetivo 2.5 (predeterminado)

  • Casi igual que el objetivo 3.0 (ver más abajo), excepto solo tiene 8 interpoladores, y no tiene un muestreo de textura LOD explícito.
  • Compiles into DX11 feature level 9.3 on Windows Phone.

#pragma objetivo 3.0

  • Modelo shader DX9 3.0: instrucciones derivadas, muestreo LOD de textura, 10 interpoladores, más instrucciones de matemáticas/textura permitidas.
  • No soportado en las GPUs de nivel 9.x de la característica DX11 (por ejemplo, la mayoría de los dispositivos Windows Phone).
  • Puede no ser totalmente compatible con algunos dispositivos OpenGL ES 2.0, dependiendo de las extensiones de controlador presentes y las características utilizadas.

#pragma objetivo 3.5 (o es3.0)

  • Capacidades OpenGL ES 3.0 (DX10 SM4.0 en plataformas D3D, sólo sin shaders de geometría).
  • Not supported on DX11 9.x (WinPhone), OpenGL ES 2.0.
  • Supported on DX11+, OpenGL 3.2+, OpenGL ES 3+, Metal, Vulkan, PS4/XB1 consoles.
  • Native integer operations in shaders, texture arrays and so on.

#pragma objetivo 4.0

  • Modelo shader DX11 4.0.
  • Not supported on DX11 9.x (WinPhone), OpenGL ES 2.0/3.0/3.1, Metal.
  • Supported on DX11+, OpenGL 3.2+, OpenGL ES 3.1+AEP, Vulkan, PS4/XB1 consoles.
  • Tiene shaders de geometría y todo lo que `es3.0’ objetivo tiene.

#pragma objetivo 4.5 (o es3.1)

  • Capacidades OpenGL ES 3.1 (DX11 SM5.0 en plataformas D3D, sólo sin shaders de tessellation).
  • Not supported on DX11 before SM5.0, OpenGL before 4.3 (i.e. Mac), OpenGL ES 2.0/3.0.
  • Supported on DX11+ SM5.0, OpenGL 4.3+, OpenGL ES 3.1, Metal, Vulkan, PS4/XB1 consoles.
  • Has compute shaders, random access texture writes, atomics and so on. No geometry or tessellation shaders.

#pragma objetivo 4.6 (o gl4.1)

  • OpenGL 4.1 (DX11 SM5.0 en plataformas D3D, sólo sin shaders de cálculo). Este es básicamente el más alto nivel de OpenGL soportado por Macs.
  • Not supported on DX11 before SM5.0, OpenGL before 4.1, OpenGL ES 2.0/3.0/3.1, Metal.
  • Supported on DX11+ SM5.0, OpenGL 4.1+, OpenGL ES 3.1+AEP, Vulkan, Metal (without geometry), PS4/XB1 consoles.

#pragma objetivo 5.0

  • shader model DX11 5.0.
  • Not supported on DX11 before SM5.0, OpenGL before 4.3 (i.e. Mac), OpenGL ES 2.0/3.0/3.1, Metal.
  • Supported on DX11+ SM5.0, OpenGL 4.3+, OpenGL ES 3.1+AEP, Vulkan, Metal (without geometry), PS4/XB1 consoles.

Tenga en cuenta que todas las plataformas similares a OpenGL (incluyendo móviles) son tratadas como “capaz de modelo shader 3.0”. Las plataformas WP8/WinRT (nivel 9.x de la característica DX11) se tratan como sólo capaces del modelo shader 2.5.

Supported ‘#pragma require’ names

List of supported feature names for the #pragma require directive:

  • interpolators10: At least 10 vertex-to-fragment interpolators (“varyings”) are available.
  • interpolators15: At least 15 vertex-to-fragment interpolators (“varyings”) are available.
  • interpolators32: At least 32 vertex-to-fragment interpolators (“varyings”) are available.
  • mrt4: Multiple Render Targets, at least 4.
  • mrt8: Multiple Render Targets, at least 8.
  • derivatives: Pixel shader derivative instructions (ddx/ddy).
  • samplelod: Explicit texture LOD sampling (tex2Dlod / SampleLevel).
  • fragcoord: Pixel location (XY on screen, ZW depth in clip space) input in pixel shader.
  • integers: Integers are an actual data type, including bit/shift operations.
  • 2darray: 2D texture arrays (Texture2DArray).
  • cubearray: Cubemap arrays (CubemapArray).
  • instancing: SV_InstanceID input system value.
  • geometry: DX10 geometry shaders.
  • compute: Compute shaders, structured buffers, atomic operations.
  • randomwrite: “random write” (UAV) textures.
  • tesshw: GPU support for hardware tessellation, but not necessarily tessellation shader stages (e.g. Metal supports tessellation, but not via shader stages).
  • tessellation: Tessellation hull/domain shader stages.
  • msaatex: Ability to access multi-sampled textures (Texture2DMS in HLSL).
  • sparsetex: Sparse textures with residency info (“Tier2” support in D3D terms; CheckAccessFullyMapped HLSL function). Note that currently this is only implemented on DX11/12.
  • framebufferfetch: Framebuffer fetch – ability to read input pixel color in the pixel shader.

The broad #pragma target directives are shorthands for the requirements above, and they correspond to:

  • 2.5: derivatives
  • 3.0: 2.5 + interpolators10 + samplelod + fragcoord
  • 3.5: 3.0 + interpolators15 + mrt4 + integers + 2darray + instancing
  • 4.0: 3.5 + geometry
  • 5.0: 4.0 + compute + randomwrite + tesshw + tessellation
  • 4.5: 3.5 + compute + randomwrite
  • 4.6: 4.0 + cubearray + tesshw + tessellation

Note that in Direct3D terms shader model 4.0 also implies “mrt8”; and shader model 5.0 implies “interpolators32” and “cubearray”. However, these are not guaranteed to be available on many mobile platforms. So for backwards compatibility with existing shaders, writing #pragma target 4.0 does not automatically require 8 MRTs support; and writing #pragma target 5.0 does not require 32 interpolators nor cubemap arrays.

Véase también


  • 2018–03–20 Page amended
  • Shader #pragma directives added in Unity 2018.1 NewIn20181
  • Tessellation for Metal added in 2018.1
Shading language used in Unity
Tipos de dato Shader y precisión