Unity stereo output function

This is more on an information than a question. I was browsing through unitys UnityInstancing.cginc code to understand what is necessary to call to use stereo rendering correctly and stumbled upon this code:

////////////////////////////////////////////////////////
// basic stereo instancing setups
// - UNITY_VERTEX_OUTPUT_STEREO             Declare stereo target eye field in vertex shader output struct.
// - UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO  Assign the stereo target eye.
// - UNITY_TRANSFER_VERTEX_OUTPUT_STEREO    Copy stero target from input struct to output struct. Used in vertex shader.
#ifdef UNITY_STEREO_INSTANCING_ENABLED
    #define UNITY_VERTEX_OUTPUT_STEREO uint stereoTargetEyeIndex : SV_RenderTargetArrayIndex;
    #define UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndex = unity_StereoEyeIndex;
    #define UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndex = input.stereoTargetEyeIndex;
    #define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = input.stereoTargetEyeIndex;
#elif defined(UNITY_STEREO_MULTIVIEW_ENABLED)
    #define UNITY_VERTEX_OUTPUT_STEREO float stereoTargetEyeIndex : BLENDWEIGHT0;
    // HACK: Workaround for Mali shader compiler issues with directly using GL_ViewID_OVR (GL_OVR_multiview). This array just contains the values 0 and 1.
    #define UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output) output.stereoTargetEyeIndex = unity_StereoEyeIndices[unity_StereoEyeIndex].x;
    #define UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output) output.stereoTargetEyeIndex = input.stereoTargetEyeIndex;
    #if defined(SHADER_STAGE_VERTEX)
        #define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
    #else
        #define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input) unity_StereoEyeIndex = (uint) input.stereoTargetEyeIndex;
    #endif
#else
    #define UNITY_VERTEX_OUTPUT_STEREO
    #define UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output)
    #define UNITY_TRANSFER_VERTEX_OUTPUT_STEREO(input, output)
    #define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
#endif

Syntax highlighted

The description in line 66 (UnityInstancing.cginc from Unity 5.6.0p4) says to use UNITY_TRANSFER_VERTEX_OUTPUT_STEREO to pass the value to the frag shader. From what I see UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO does that already. I’m also not sure if input.stereoTargetEyeIndex is actually supported anymore.

Are there any plans to clean up the shader code a bit? I’ve got the feeling like there is quite some legacy stuff lying around that could be refactored/cleaned up.

1 Like

One sets the index based on a global value, the other sets the index based on a specified struct. Unity’s own shaders mainly use UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO, but UNITY_TRANSFER_VERTEX_OUTPUT_STEREO gets used in geometry shaders where multiple you’re dealing with vertices at a time. Seems like the comment in the code is either wrong or an old use case.

2 Likes

Ah okay, very interesting! Thanks for the enlightenment :slight_smile:

Also browsing this stuff, thanks for your share.