Single-pass stereo rendering for Android
Explains how to use single-pass stereo rendering in your Android application.
Read time 2 minutesLast updated 10 days ago
Unity supports single-pass stereo rendering for Android devices that support multiview. Multiview consists of the GL_OVR_multiview2 and GL_OVR_multiview_multisampled_render_to_texture OpenGL ES extensions. These extensions require shaders to use a 2D texture array that consists of two slices, one slice per eye.
Shader code requirements
To use single-pass stereo rendering with custom shaders, you may need to include additional shader code. You don't need to include additional code if your custom shaders are:
- Surface Shaders that don't have custom vertex processing.
- Fixed-function pipeline shaders.
Modify your shaders
If you want to use the built-in shader variable to know which eye the GPU is rendering to, you must declare in any shader stage output structs that you have. For example:
unity_StereoEyeIndexUNITY_VERTEX_OUTPUT_STEREOstruct v2f { float2 uv : TEXCOOR0; float4 vertex : SV_POSITION; UNITY_VERTEX_OUTPUT_STEREO};
To initialize the output data, use in the vertex shader function. For example:
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO()v2f vert (appdata v){ v2f o; UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o;}
To initialize in subsequent stages, add at the beginning. For example:
unity_StereoEyeIndexUNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX()half4 frag (v2f i) : SV_Target{ UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); // sample the texture half4 col = tex2D(_MainTex, i.uv); // apply fog UNITY_APPLY_FOG(i.fogCoord, col); return col;}
If your shaders use other shader stages, use the macro to transfer the eye index to the subsequent stages.
UNITY_TRANSFER_VERTEX_OUTPUT_STEREO()Post-Processing Shaders
You must update post-processing shaders to deal with the eye textures being a 2D texture array. To help with this, Unity includes the macro. To make textures work in both multi-pass and single-pass modes, wrap each textures in this macro. Also, when you sample the texture, use the macro.
UNITY_DECLARE_SCREENSPACE_TEXTURE()UNITY_SAMPLE_SCREENSPACE_TEXTURE()This macro requires that you call beforehand when in single-pass mode. Unity also includes similar macros for depth textures and screen space shadow maps. You can see the full list at the bottom of .
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX()HLSLSupport.cginc