Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

simple geometry shader fails on Android, works on Windows

Discussion in 'Shaders' started by fortgreeneVR, Mar 20, 2019.

  1. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    I have a simple pass-through geometry shader that works fine on windows but fails on an Android device with the following message:

    The given primitive topology does not match with the topology expected by the geometry shader


    Here's the shader:
    Code (CSharp):
    1. Shader "FG/Test"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1, 1, 1, 1)
    6.     }
    7.  
    8.         SubShader
    9.         {
    10.                 Tags{ "RenderType" = "FGOpaque" }
    11.                 LOD 200
    12.  
    13.             Pass
    14.             {
    15.                 CGPROGRAM
    16.  
    17.                 #pragma vertex vert
    18.                 #pragma geometry geom
    19.                 #pragma fragment frag
    20.                 #pragma target 4.0
    21.                 #include "UnityCG.cginc"
    22.  
    23.                 struct appdata
    24.                 {
    25.                     float4 vertex : POSITION;
    26.                 };
    27.  
    28.                 struct v2f
    29.                 {
    30.                     float4 vertex : SV_POSITION;
    31.                 };
    32.  
    33.                 v2f vert (appdata v)
    34.                 {
    35.                     v2f o;
    36.                     o.vertex = UnityObjectToClipPos(v.vertex);
    37.                     return o;
    38.                 }
    39.  
    40.                 [maxvertexcount(3)]
    41.                 void geom (triangle v2f i[3], inout TriangleStream<v2f> stream)
    42.                 {
    43.                     stream.Append(i[0]);
    44.                     stream.Append(i[1]);
    45.                     stream.Append(i[2]);
    46.                 }
    47.  
    48.                 fixed4 frag(v2f i) : SV_Target
    49.                 {
    50.                     return fixed4(0.0,1.0,0.5,1.0);
    51.                 }
    52.  
    53.                 ENDCG
    54.             }
    55.         }
    56.         FallBack "Diffuse"
    57. }
    58.  
     
  2. takano_ch

    takano_ch

    Joined:
    Jan 21, 2019
    Posts:
    1
  3. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Does this happen on Windows if you switch the Editor to OpenGL?
     
  4. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    Hi Takano_ch and Aleksandrk,

    android version is [ro.build.version.release] = 7.1.1
    Unity version is 2018.3.4f1

    When I switch Edit->Project Settings->Player->Graphics APIs to OpenGLES3, Require ES3.1, Require ES3.1+AEP, and Edit->Graphics Emulation to OpenGL ES 3.0, Shader Hardware Tier 3, the shader doesn't work, it produces a gray color, but I don't see any errors in the console or in the Inspector with the shader selected. The disassembly of the compiled shader seems to have everything needed, see below.

    Thanks for looking.

    Code (CSharp):
    1. // Compiled shader for Android
    2.  
    3. //////////////////////////////////////////////////////////////////////////
    4. //
    5. // NOTE: This is *not* a valid shader file, the contents are provided just
    6. // for information and for debugging purposes only.
    7. //
    8. //////////////////////////////////////////////////////////////////////////
    9. // Skipping shader variants that would not be included into build of current scene.
    10.  
    11. Shader "FG/Test" {
    12. Properties {
    13. _Color ("Color", Color) = (1.000000,1.000000,1.000000,1.000000)
    14. }
    15. SubShader {
    16. LOD 200
    17. Tags { "RenderType"="FGOpaque" }
    18. Pass {
    19.   Tags { "RenderType"="FGOpaque" }
    20.   //////////////////////////////////
    21.   //                              //
    22.   //      Compiled programs       //
    23.   //                              //
    24.   //////////////////////////////////
    25. //////////////////////////////////////////////////////
    26. No keywords set in this variant.
    27. -- Hardware tier variant: Tier 1
    28. -- Vertex shader for "gles3":
    29. Shader Disassembly:
    30. #ifdef VERTEX
    31. #version 310 es
    32.  
    33. uniform     vec4 hlslcc_mtx4x4unity_ObjectToWorld[4];
    34. uniform     vec4 hlslcc_mtx4x4unity_MatrixVP[4];
    35. in highp vec4 in_POSITION0;
    36. vec4 u_xlat0;
    37. vec4 u_xlat1;
    38. void main()
    39. {
    40.     u_xlat0 = in_POSITION0.yyyy * hlslcc_mtx4x4unity_ObjectToWorld[1];
    41.     u_xlat0 = hlslcc_mtx4x4unity_ObjectToWorld[0] * in_POSITION0.xxxx + u_xlat0;
    42.     u_xlat0 = hlslcc_mtx4x4unity_ObjectToWorld[2] * in_POSITION0.zzzz + u_xlat0;
    43.     u_xlat0 = u_xlat0 + hlslcc_mtx4x4unity_ObjectToWorld[3];
    44.     u_xlat1 = u_xlat0.yyyy * hlslcc_mtx4x4unity_MatrixVP[1];
    45.     u_xlat1 = hlslcc_mtx4x4unity_MatrixVP[0] * u_xlat0.xxxx + u_xlat1;
    46.     u_xlat1 = hlslcc_mtx4x4unity_MatrixVP[2] * u_xlat0.zzzz + u_xlat1;
    47.     gl_Position = hlslcc_mtx4x4unity_MatrixVP[3] * u_xlat0.wwww + u_xlat1;
    48.     return;
    49. }
    50.  
    51. #endif
    52. #ifdef FRAGMENT
    53. #version 310 es
    54.  
    55. precision highp float;
    56. precision highp int;
    57. layout(location = 0) out mediump vec4 SV_Target0;
    58. void main()
    59. {
    60.     SV_Target0 = vec4(0.0, 1.0, 0.5, 1.0);
    61.     return;
    62. }
    63.  
    64. #endif
    65. #ifdef GEOMETRY
    66. #version 310 es
    67. #ifdef GL_ARB_geometry_shader
    68. #extension GL_ARB_geometry_shader : enable
    69. #endif
    70. #ifdef GL_OES_geometry_shader
    71. #extension GL_OES_geometry_shader : enable
    72. #endif
    73. #ifdef GL_EXT_geometry_shader
    74. #extension GL_EXT_geometry_shader : enable
    75. #endif
    76.  
    77. layout(triangles) in;
    78. layout(triangle_strip) out;
    79. layout(max_vertices = 3) out;
    80. void main()
    81. {
    82.     gl_Position = gl_in[0].gl_Position;
    83.     EmitVertex();
    84.     gl_Position = gl_in[1].gl_Position;
    85.     EmitVertex();
    86.     gl_Position = gl_in[2].gl_Position;
    87.     EmitVertex();
    88.     return;
    89. }
    90.  
    91. #endif
    92.  
    93.  
    94. -- Hardware tier variant: Tier 1
    95. -- Fragment shader for "gles3":
    96. Shader Disassembly:
    97. // All GLSL source is contained within the vertex program
    98.  
    99. -- Hardware tier variant: Tier 1
    100. -- Geometry shader for "gles3":
    101. Shader Disassembly:
    102. // All GLSL source is contained within the vertex program
    103.  
    104. }
    105. }
    106. Fallback "Diffuse"
    107. }
    108.  
     
  5. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    This is not what suggested to check :)
    When you go to Edit->Project Settings->Player, it shows your current build target (Android in your case). Go to standalone player tab, uncheck "Auto graphics API" for Windows and set OpenGL as the first API in the list.
    It should have the same error.
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    I suppose you're just missing
    Code (CSharp):
    1. stream.RestartStrip();
    after the last call to
    Code (CSharp):
    1. append();
     
  7. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    Hi Aleksandrk,

    Ok, got it, I was referring to the Android settings.

    So when I go to the 'pc, mac, linux standalone tab' and remove the DX options and add OpenGLES3, the shader works fine as originally listed.

    I don't have a plain OpenGL option, only OpenGLES3, OpenGLES2 and OpenGLCore.
    Using OpenGLES2 gives the gray result without errors. OpenGLCore works the same as OpenGLES3.

    Adding stream.RestartStrip doesn't make a difference in any of the cases.

    Thanks again.
     
  8. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    Ok, Good news / not so good news: In a small test example the geometry shader seems to work (to be sure I added per triangle per vertex colors), but the device log still has the same OpenGL error.
    So it seems a bit of a red-herring. Not sure if the larger app would fail because of it.
    I'll log a bug report.
     
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Yes, I meant OpenGLCore.
    Thanks! We'll take a look at what's happening there :)
     
  10. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    Great. Larger app does fail to produce a viable image, mostly black with noise.
    Thanks for looking.
     
  11. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    Hi Aleksandrk,
    I filed case 1139402 last week but haven't heard back, except for the auto-reply.
    Would you mind having a look?
    Let me know if we can assist in any way as the problem occurs on a device that is not for sale yet.
     
  12. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    I also submitted a more complete example as case 1141313.
    Thanks again.
     
  13. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
    Hi!
    The bugs are processed by our QA team first. Someone will take a look after that.
     
  14. superjayman

    superjayman

    Joined:
    May 31, 2013
    Posts:
    185
    This is still not fixed!!!! Any progress???
     
  15. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    The problem is caused by an interaction of MSAA and the geometry shader. Without turning on MSAA, the geometry shader looks fine. With MSAA, the screen goes (mostly) black. This happens on a Oculus Quest device, other Android devices might be fine.
     
    gevasaf likes this.
  16. mptp

    mptp

    Joined:
    May 30, 2014
    Posts:
    29
    I was having problems recently with geometry shaders on Quest.

    My contact at Oculus went and spoke to some techs there and the answer came back basically as 'geometry shaders will never be your friend, don't use them'. It may not be a Unity problem, it might just be a problem with the implementation of OpenGL or something on Quest.

    So I've recently transitioned everything from geometry shaders to compute shaders, worked a treat, and is more flexible to boot!
     
    gevasaf likes this.
  17. gevasaf

    gevasaf

    Joined:
    Jan 25, 2014
    Posts:
    6
    I'm also trying to get a geometry shader working on the Oculus Quest.
    How did you use a compute shader as a geometry shader substitute?
     
  18. gevasaf

    gevasaf

    Joined:
    Jan 25, 2014
    Posts:
    6
    I experience the "mostly black image" artifact even with MSAA turned off in quality settings.
    upload_2020-1-16_10-37-51.png

    Is there another setting that effects MSAA??
     
  19. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    I'm curious too about how to replace a geometry shader with a compute shader!

    As far as MSAA goes, it should be fairly easy to spot the difference between MSAA ON and OFF on a simple mesh.
    The camera has a setting too but the GUI warns it won't be honored if the Quality setting doesn't allow it.
    A script can probably set it as well. I hope the problem didn't get worse..

    Aleksandrk, any word on a resolution to the problem?
     
    gevasaf likes this.
  20. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,983
  21. fortgreeneVR

    fortgreeneVR

    Joined:
    Sep 5, 2018
    Posts:
    50
    FYI, the problem stayed the same in Unity 2019.1.0b , MSAA OFF + geometry shader works