Branch based on platform or graphics API
Use SHADER_API macros to make shader behaviour conditional on the platform or graphics API.
Read time 2 minutesLast updated a month ago
To create optimized shader code for a platform, branch in your shader code using one of the preprocessor macros Unity defines.
#if defined(SHADER_API_DX11) // Shader code for when the target graphics API is DirectX 11#endif
Branch based on the build target
Unity defines the following macros when you make the corresponding platform the target for your build.
Macro | Platform |
|---|---|
| Android devices |
| Embedded Linux devices |
| XBOX One Game Core |
| iOS devices |
| Linux devices |
| Meta Quest devices. If you target Meta Quest, Unity also defines the |
| macOS devices |
| PlayStation®4 |
| PlayStation®5 |
| QNX devices |
| Nintendo Switch™ |
| Nintendo Switch™ 2 |
| tvOS devices |
| Universal Windows Platform (UWP) devices |
| visionOS devices |
| WebGL devices |
| Windows devices |
| XBOX One |
Branch within a platform branch
To branch again within platform-specific branching code, declare a shader keyword after you test for the platform, then branch using the keyword. For example:
// Test if the build target is Meta Quest#if defined(UNITY_PLATFORM_META_QUEST) // Add a keyword only if the build target is Meta Quest #pragma shader_feature _ META_QUEST_REFLECTIONS#endif...fixed4 frag (v2f i) : SV_Target { // Test if the build target is Meta Quest #if defined(UNITY_PLATFORM_META_QUEST) // Test if your custom keyword is enabled #if META_QUEST_REFLECTIONS // Shader code for Meta Quest with reflections #else // Shader code for Meta Quest without reflections #endif #endif ... }
For more information, refer to Changing how shaders work using keywords.
Branch based on the target graphics API
Unity defines the following macros when Unity compiles shaders for the corresponding graphics API.
Macro | Graphics API |
|---|---|
| Direct3D 11 |
| Direct3D 12 |
| Direct3D 11 and Direct3D 12 (marked for deprecation) |
| DirectX 11 feature level 9, for Universal Windows Platform (UWP) devices |
| Graphics APIs used by Windows devices, macOS devices, Linux desktop platforms, and web devices |
| OpenGL 3.x, OpenGL 4.x |
| OpenGL ES 3.x |
| Metal on iOS or macOS |
| Graphics APIs used by iOS and Android mobile devices, and tvOS devices |
| Vulkan |
| WebGPU |
| OpenGL and GLES platforms where Unity compiles HLSL to GLSL |