Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question ShaderLab ZClip False produces unexpected result on Android with OpenGLES3

Discussion in 'Shaders' started by heuriko, Jul 7, 2023.

  1. heuriko

    heuriko

    Joined:
    Nov 5, 2021
    Posts:
    3
    Dear Forum,

    I have run into an issue with ZClip False. The shader behaves as expected in the Editor and on iOS. However behavior is different for Android OpenGLES3.

    GOAL: Write a shader that also renders triangles behind the clip plane

    Using below shader, triangles will be drawn when they are behind the near clip plane and part of the renderer is at least in front of the near clip plane.

    This can be achieved using ZClip False.

    Code (Boo):
    1. Shader "NoZClip"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1)
    6.     }
    7.         SubShader
    8.     {
    9.         Tags { "RenderType" = "Opaque" "Queue" = "Geometry"}
    10.         Pass
    11.         {
    12.             Name "DrawWithoutZTest"
    13.             Cull Off
    14.             ZClip False
    15.  
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.             #include "UnityCG.cginc"
    20.  
    21.             half4 _Color;
    22.  
    23.             half4 vert(float4 v : POSITION) : SV_POSITION
    24.             {
    25.                 return UnityObjectToClipPos(v);
    26.             }
    27.              
    28.             half4 frag() : SV_Target
    29.             {
    30.                 return _Color;
    31.             }
    32.             ENDCG
    33.         }
    34.     }
    35. }
    36.  
    EXPECTED BEHAVIOR: Renderer should NOT be clipped
    - Unity 2022.3.4f1

    The shader works as expected in Unity Editor on Windows.

    The test scene includes 2 duplicate quads: one is fully inside the viewing frustum whereas the other cuts the near clip plane.

    scene_2.png

    screen_expected.png

    UNEXPECTED BEHAVIOR: Renderer gets clipped on Android phone

    - Samsung Galaxy S20 FE
    - OpenGLES3

    The renderer gets clipped. The result is different from the editor, and also from iOS builds.

    screen_android_gles3.jpg

    QUESTIONS:
    - Is the shown behavior expected behavior on Android OpenGLES3?
    - If so are there any options to replicate Editor behavior on Android?

    Thank you so much for your feedback and ideas!
     

    Attached Files:

    Last edited: Jul 7, 2023
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,835
    Hi!
    OpenGL ES only supports it using the GL_EXT_depth_clamp extension. It's available in ~0.5% devices on the market.
    It's also not universally supported on Android if you switch to Vulkan either.
     
    heuriko likes this.
  3. heuriko

    heuriko

    Joined:
    Nov 5, 2021
    Posts:
    3
    Thank you very much for your prompt and informative reply @aleksandrk

    So if I understand correctly Shaderlab's ZClip=False will translate into "GL_EXT_depth_clamp" when using OpenGLES3.x and feature "depthClamp" for Vulkan API respectively? Or in other words availability of the respective feature on the target hardware would guarantee the shader to work?
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,835
    Yes and no. Unity doesn't support GL_EXT_depth_clamp extension since it's so rarely present.
    So only with Vulkan.
     
  5. heuriko

    heuriko

    Joined:
    Nov 5, 2021
    Posts:
    3
    OK thanks a lot! You definitely helped me with the issue!