Write HLSL for different graphics APIs
Write HLSL to take into account how graphics rendering differs between different graphics APIs.
읽는 시간 5분최근 업데이트: 19일 전
There are differences in how graphics rendering behaves between different graphics APIs. Most of the time the Unity Editor hides the differences, but there are some situations where Unity can't do this for you.
Check floating point exception handling
Desktop platforms and mobile platforms can handle exceptions differently during floating point operations such as division by zero. Exceptions can result in , zero, or another value. Test your shaders on the different platforms you target.
NaNUse consistent semantics
Use the following semantics to ensure shaders work on all platforms:
- instead of
SV_POSITIONfor the position the vertex shader outputs.POSITION - instead of
SV_TargetorCOLORfor the output of the fragment shader.COLOR0 - if you render meshes as points. For example, set
PSIZEto 1. This avoids some platforms like Metal reading point size as undefined.PSIZE
For more information about semantics, refer to High-Level Shader Language (HLSL) semantics reference.
Use const for constants
Don't use for values that come from outside the shader or that you calculate at runtime, as this only works in HLSL, not OpenGL Shading Language (GLSL). Use only for compile-time constants.
constconstAvoid mismatched buffer layouts
If you use the standard HLSL type, or Unity's and macros, a might become a , or a might become a .
cbufferCBUFFERSTARTCBUFFERENDfloat3float4floatfloat2To ensure all graphics APIs compile a buffer with the same data layout, do the following:
- Use and
float4instead offloat4x4andfloat3, becausefloat3x3variables are the same size on all graphics APIs, whilefloat4variables can become a different size on some graphics APIs.float3 - Declare variables in decreasing size order, for example then
float4thenfloat2, so all graphics APIs structure the data in the same way.float
For example:
cbuffer myConstantBuffer { float4x4 matWorld; float4 vObjectPosition; // Uses a float4 instead of a float3 float arrayIndex;}
Handle reversed coordinate spaces
DirectX, Metal, and Vulkan use different coordinate systems to other graphics APIs. To make sure shaders work correctly on all platforms, adjust your code to handle reversed textures.
To check for reversed textures, use pre-processor conditionals like and with Unity's built-in macros or methods.
#if#ifdefCheck for upside-down render textures
If you sample a render texture at , DirectX, Metal, and Vulkan return the top-left of the texture. Other graphics APIs return the bottom-left of the texture. Unity handles this internally for most render textures by flipping render textures upside-down internally where needed, so coordinates you use return the same texels on all platforms.
(0,0)To check if the current platform uses reversed coordinates for render textures, use either of the following methods:
-
Check if Unity defines, which means the graphics API uses
UNITY_UV_STARTS_AT_TOPfor the top-left of the texture.(0,0)For example, to check for reversed render textures then flip vertical uv coordinates:#if UNITY_UV_STARTS_AT_TOP if (_MainTex_TexelSize.y < 0) uv.y = 1-uv.y;#endif -
Check the built-invariable. If the value is
ProjectionParams.x, Unity flipped the render texture, so you must also flip vertical UV coordinates.-1
Check for reversed depth direction
The direction of depth in clip space, the depth buffer, and depth textures is different depending on the graphics API you target.
Coordinate space | Z coordinate in DirectX, Metal, and Vulkan | Z coordinate in OpenGL ES and WebGL |
|---|---|---|
| Depth buffer and depth textures | | |
| Clip space | | |
Use the following in custom HLSL code to handle these differences:
-
Use SystemInfo.usesReversedZBuffer to check if the graphics API uses 1.0 to 0.0 for depth.
-
Check if Unity defines, which means the graphics API uses 1.0 to 0.0 for depth, then flip the z value.
UNITY_REVERSED_ZFor example, in the Built-In Render Pipeline:float z = tex2D(_CameraDepthTexture, uv);#if defined(UNITY_REVERSED_Z) z = 1.0f - z;#endif
You might also need to flip depth bias values if you use a code rendering plug-in.
For more macros that help with depth, refer to Shader methods in the Built-In Render Pipeline or Shader methods in the Universal Render Pipeline (URP).
Compiling on DirectX platforms
If you target the DirectX graphics API, the compiler is stricter than other compilers. Check the following:
- Set all the components of a vector. For example, if you declare a , ensure you set all four components.
float4 - Don't use the method in the vertex shader. Instead, use the
tex2Dmethod to sample a specific mip level, and usetex2Dlodto ensure compatibility.#pragma target 3.0 - If you use a vertex shader with an parameter in a surface shader, initialize the output using the
outmacro.UNITY_INITIALIZE_OUTPUT