Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

unity_stereoEyeIndex with GLSL (Single Pass Implementation details)

Discussion in 'AR/VR (XR) Discussion' started by not_a_valid_username, Dec 4, 2018.

  1. not_a_valid_username

    not_a_valid_username

    Joined:
    Jul 28, 2018
    Posts:
    11
    I'm trying to play a 3D video on a texture in Unity on Android. Because the video is a high resolution and framerate, I need to use OES textures for the video, which forces me to use a GLSL shader (as OES texture sampling is not currently supported in CG).

    My question is, how do I get the current eye being rendered when using GLSL? With CG, this can be easily accessed with unity_StereoEyeIndex, but I don't seem to be able to access that function when the shader is directly written in GLSL.
     
  2. not_a_valid_username

    not_a_valid_username

    Joined:
    Jul 28, 2018
    Posts:
    11
    By viewing a compiled CG shader, I see that unity_StereoEyeIndex is a uniform int in the fragment shader. Putting this in my GLSL shader seemed to do the trick
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    7,833
    Thanks for returning with the answer. Any chance you'll share your shader here, for posterity?
     
  4. not_a_valid_username

    not_a_valid_username

    Joined:
    Jul 28, 2018
    Posts:
    11
    The shader that I was using this for is pretty bespoke and messy, but a brief example would be:

    #ifdef FRAGMENT
    uniform highp int unity_StereoEyeIndex;
    void main ()
    {
    gl_FragColor = vec4 (unity_StereoEyeIndex, 0.0, 0.0, 1.0);
    }
    #endif
     
  5. Mandelboxed

    Mandelboxed

    Joined:
    Apr 17, 2015
    Posts:
    25
    I've had trouble sampling from an OES texture using single pass, how did you manage to get it to work? Would you be willing to post or send a more complete example? I understand if not, but it would be very much appreciated! It's no problem at all if it's messy!
     
  6. not_a_valid_username

    not_a_valid_username

    Joined:
    Jul 28, 2018
    Posts:
    11
    I actually have not yet tested with single pass enabled, but if I get it working I'll post back here
     
  7. Mandelboxed

    Mandelboxed

    Joined:
    Apr 17, 2015
    Posts:
    25
    You actually inspired me. I took a look at a compiled version of a simple unlit shader and managed to get it working. I was not able to get unity_StereoEyeIndex to work but I found another variable (u_xlati1) in the compiled code that worked the same way. This looks like it's Oculus specific so your mileage may vary.

    Code (CSharp):
    1. Shader "Unlit/UnlitTexNoFogOes" {
    2.     Properties{
    3.         _MainTex("Texture", 2D) = "white" { }
    4.         [KeywordEnum(Monoscopic, Top_Bottom)] Stereo("Stereo mode", Float) = 0
    5.     }
    6.         SubShader{
    7.         LOD 100
    8.         Lighting Off
    9.         Cull Off
    10.  
    11.         Tags { "RenderType" = "Opaque" }
    12.         Pass {
    13.         GLSLPROGRAM
    14.                 #ifdef VERTEX
    15.                 #version 300 es
    16.                 #extension GL_OVR_multiview2 : require
    17.                 #extension GL_OES_EGL_image_external : require
    18.                 #extension GL_OES_EGL_image_external_essl3 : enable
    19.  
    20.                 #pragma multi_compile MONOSCOPIC STEREO_TOP_BOTTOM
    21.  
    22.                 uniform     vec4 hlslcc_mtx4x4unity_ObjectToWorld[4];
    23.                 uniform     vec4 _MainTex_ST;
    24.                 layout(std140) uniform UnityStereoGlobals {
    25.                     vec4 hlslcc_mtx4x4unity_StereoMatrixP[8];
    26.                     vec4 hlslcc_mtx4x4unity_StereoMatrixV[8];
    27.                     vec4 hlslcc_mtx4x4unity_StereoMatrixInvV[8];
    28.                     vec4 hlslcc_mtx4x4unity_StereoMatrixVP[8];
    29.                     vec4 hlslcc_mtx4x4unity_StereoCameraProjection[8];
    30.                     vec4 hlslcc_mtx4x4unity_StereoCameraInvProjection[8];
    31.                     vec4 hlslcc_mtx4x4unity_StereoWorldToCamera[8];
    32.                     vec4 hlslcc_mtx4x4unity_StereoCameraToWorld[8];
    33.                     vec3 unity_StereoWorldSpaceCameraPos[2];
    34.                     vec4 unity_StereoScaleOffset[2];
    35.                 };
    36.                 layout(num_views = 2) in;
    37.                 in highp vec4 in_POSITION0;
    38.                 in highp vec2 in_TEXCOORD0;
    39.                 out highp vec2 vs_TEXCOORD0;
    40.                 vec4 u_xlat0;
    41.                 int u_xlati1;
    42.                 vec4 u_xlat2;
    43.  
    44.                 void main()
    45.                 {
    46.                     vs_TEXCOORD0.xy = in_TEXCOORD0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
    47.                     u_xlat0 = in_POSITION0.yyyy * hlslcc_mtx4x4unity_ObjectToWorld[1];
    48.                     u_xlat0 = hlslcc_mtx4x4unity_ObjectToWorld[0] * in_POSITION0.xxxx + u_xlat0;
    49.                     u_xlat0 = hlslcc_mtx4x4unity_ObjectToWorld[2] * in_POSITION0.zzzz + u_xlat0;
    50.                     u_xlat0 = u_xlat0 + hlslcc_mtx4x4unity_ObjectToWorld[3];
    51.                     u_xlati1 = int(gl_ViewID_OVR) << 2;
    52.                     u_xlat2 = u_xlat0.yyyy * hlslcc_mtx4x4unity_StereoMatrixVP[(u_xlati1 + 1)];
    53.                     u_xlat2 = hlslcc_mtx4x4unity_StereoMatrixVP[u_xlati1] * u_xlat0.xxxx + u_xlat2;
    54.                     u_xlat2 = hlslcc_mtx4x4unity_StereoMatrixVP[(u_xlati1 + 2)] * u_xlat0.zzzz + u_xlat2;
    55.                     gl_Position = hlslcc_mtx4x4unity_StereoMatrixVP[(u_xlati1 + 3)] * u_xlat0.wwww + u_xlat2;
    56.  
    57.                     vs_TEXCOORD0.y = 1.0 - vs_TEXCOORD0.y;
    58.  
    59. #if defined(STEREO_TOP_BOTTOM)
    60.                     vs_TEXCOORD0.y *= 0.5;
    61.                     if (u_xlati1 == 0) {
    62.                         vs_TEXCOORD0.y += 0.5;
    63.                     }
    64. #endif
    65.  
    66.                     return;
    67.                 }
    68.  
    69.                 #endif
    70.                 #ifdef FRAGMENT
    71.                 #version 300 es
    72.  
    73.                 precision highp int;
    74.                 uniform samplerExternalOES _MainTex;
    75.                 in highp vec2 vs_TEXCOORD0;
    76.                 layout(location = 0) out mediump vec4 SV_Target0;
    77.                 lowp vec4 u_xlat10_0;
    78.                 void main()
    79.                 {
    80.                     u_xlat10_0 = texture(_MainTex, vs_TEXCOORD0.xy);
    81.                     SV_Target0 = u_xlat10_0;
    82.                     return;
    83.                 }
    84.  
    85.                 #endif
    86.                 ENDGLSL
    87.         }
    88.     }
    89. }