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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Shader showing black on low end mobile devices

Discussion in 'Shaders' started by NonSpicyBurrito, Aug 9, 2021.

  1. NonSpicyBurrito

    NonSpicyBurrito

    Joined:
    Apr 4, 2020
    Posts:
    14
    I have various shaders all showing black on some low end Android mobile/tablet devices.
    Unfortunately I do not own one that can reproduce the issue, and users with this issue come far and few between so I could not get too much helpful information from them.
    Here is one of the shaders, extremely simple code:
    Code (CSharp):
    1. Shader "Quad/Perspective Unlit" {
    2.  
    3.     Properties {
    4.         [NoScaleOffset] _MainTex ("Texture", 2D) = "white" {}
    5.     }
    6.  
    7.     SubShader {
    8.         Tags {
    9.             "RenderType" = "Transparent"
    10.             "Queue" = "Transparent"
    11.         }
    12.  
    13.         Blend SrcAlpha OneMinusSrcAlpha
    14.         ZWrite off
    15.         Cull off
    16.  
    17.         Pass {
    18.             CGPROGRAM
    19.  
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.  
    23.             #include "UnityCG.cginc"
    24.  
    25.             sampler2D_float _MainTex;
    26.  
    27.             struct appdata {
    28.                 float4 pos : POSITION;
    29.                 float3 uvq : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f {
    33.                 float4 pos : SV_POSITION;
    34.                 float3 uvq : TEXCOORD0;
    35.             };
    36.  
    37.             v2f vert (appdata v) {
    38.                 v2f o;
    39.                 o.pos = UnityObjectToClipPos(v.pos);
    40.                 o.uvq = v.uvq;
    41.                 return o;
    42.             }
    43.  
    44.             fixed4 frag (v2f i) : SV_Target {
    45.                 fixed4 color = tex2D(_MainTex, i.uvq.xy / i.uvq.z);
    46.                 return color;
    47.             }
    48.  
    49.             ENDCG
    50.         }
    51.     }
    52. }
    Any idea what could be the issue with the code? Or maybe the issue is somewhere else?
     
  2. aybe

    aybe

    Joined:
    Feb 20, 2019
    Posts:
    53
  3. NonSpicyBurrito

    NonSpicyBurrito

    Joined:
    Apr 4, 2020
    Posts:
    14
    Thanks but unfortunately none of these help.
    The issue seems not to be on the shader itself, as it works perfectly fine for most devices including my own, and only a small selection of Android devices (Ghia tablet for example)
    First link deals with fallback which is not the issue as it works perfectly fine for vast majority of the devices but the problematic few; I've already tried auto graphics API solution in the second link with no luck; third link deals with game wide black screen which is not the case for me (only things using my specific shader is showing black); and lastly, I've tried just about everything I can find on Google, it is not a lack of effort to solve it on my own.
    Any suggestion is appreciated, at this point I'll give anything a try.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,064
    How is i.uvq.z fed to the shader? Is this a procedural mesh?
     
  5. NonSpicyBurrito

    NonSpicyBurrito

    Joined:
    Apr 4, 2020
    Posts:
    14
    Correct. Do note that it works perfectly fine on most devices so I suspect it's not an issue of the call site, but here are relevant code:

    Code (CSharp):
    1. // Create and reuse one mesh
    2. _mesh = new Mesh();
    3. _mesh.MarkDynamic();
    4.  
    5. _vertices = new VertexBufferData[4];
    6.  
    7. _mesh.SetVertexBufferParams(4, new[] {
    8.     new VertexAttributeDescriptor(VertexAttribute.Position, VertexAttributeFormat.Float32, 2),
    9.     new VertexAttributeDescriptor(VertexAttribute.TexCoord0, VertexAttributeFormat.Float32, 3),
    10. });
    11.  
    12. _mesh.SetIndexBufferParams(6, IndexFormat.UInt32);
    13. _mesh.SetIndexBufferData(
    14.     new uint[] { 0, 1, 2, 2, 3, 0 }, 0, 0, 6,
    15.     MeshUpdateFlags.DontNotifyMeshUsers | MeshUpdateFlags.DontResetBoneBounds | MeshUpdateFlags.DontValidateIndices
    16. );
    17.  
    18. _mesh.subMeshCount = 1;
    19. _mesh.SetSubMesh(
    20.     0, new SubMeshDescriptor(0, 6),
    21.     MeshUpdateFlags.DontNotifyMeshUsers | MeshUpdateFlags.DontRecalculateBounds
    22. );
    23.  
    24. // On each update
    25. _vertices[0] = ...;
    26. _vertices[1] = ...;
    27. _vertices[2] = ...;
    28. _vertices[3] = ...;
    29.  
    30. _mesh.SetVertexBufferData(
    31.     _vertices, 0, 0, 4, 0,
    32.     MeshUpdateFlags.DontNotifyMeshUsers | MeshUpdateFlags.DontResetBoneBounds
    33. );
    34.  
    35. Graphics.DrawMesh(_mesh, Matrix4x4.identity, BackgroundMaterial, 0);