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

Custom shader not working on Samsung S6

Discussion in 'Shaders' started by alvaro_perez_omnidrone, Mar 27, 2017.

  1. alvaro_perez_omnidrone

    alvaro_perez_omnidrone

    Joined:
    Jan 14, 2016
    Posts:
    2
    I am having problems with a custom shader only on Samsung S6 (also possibly some iOS devices, can't check right now for a number of reasons, but on other Android devices everything is fine). I've attached the file.

    Since it's a rather complicated shader (it maps sections of a texture along a given x range of UVs and has a lot of variants - the purpose of the shader is to paint the area of effect of an attack) I've tried to find the "minimal expression" that returns an error, to pinpoint the error (at first I thought it would be the usage of ddx and ddy, but it turns out it's not the case.

    So, this small shader already fails on the device (Of course, there's a lot of unused variables and #pragma, but that's beside the point):

    Code (csharp):
    1.  
    2.     Shader "Custom/Tiled hints"
    3.     {
    4.    
    5.         Properties
    6.         {
    7.             _MainTex ("Texture", 2D) = "white" {}
    8.             _Color ("Color", Color) = (1,1,1,0)
    9.             [HideInInspector] length ("length", Float) = 1
    10.             [HideInInspector] aspectRatio ("aspect ratio", Float) = 1
    11.             [HideInInspector] lMap ("begin", Float) = 0
    12.             [HideInInspector] rMap ("end", Float) = 1
    13.             [HideInInspector] lCut ("slice left", Range(0, 1)) = 0.25
    14.             [HideInInspector] rCut ("slice right", Range(0, 1)) = 0.75
    15.             [HideInInspector] lRemove ("remove left", Range(0, 1)) = 0
    16.             [HideInInspector] rRemove ("remove right", Range(0, 1)) = 1
    17.             [HideInInspector] lHeadTile ("head tile", Range(0, 1)) = 0
    18.             [HideInInspector] lHeadMapTile ("head tile", Float) = 0
    19.         }
    20.         SubShader
    21.         {
    22.             Tags {  "QUEUE"="Transparent-2" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }
    23.      
    24.             ZWrite Off
    25.             ZTest On
    26.             Cull Back
    27.             Blend SrcAlpha OneMinusSrcAlpha
    28.             AlphaTest Greater .01
    29.             Fog { Mode Off }
    30.            
    31.             LOD 100
    32.             Offset -1, -1
    33.    
    34.             Pass
    35.             {
    36.                 CGPROGRAM
    37.                 #pragma vertex vert
    38.                 #pragma fragment frag
    39.                 #pragma multi_compile_fog
    40.                
    41.                 #pragma multi_compile _ TILED_HEAD
    42.                 #pragma multi_compile TILED_MIDDLE_LEFT TILED_MIDDLE_RIGHT
    43.                 #pragma multi_compile _ TILED_HEAD_WITH_CUT
    44.                 #pragma multi_compile _ DEBUG_SHADER
    45.                 #pragma multi_compile _ UVS_01
    46.                 #pragma multi_compile _ PING_PONG_FMOD
    47.    
    48.                 #pragma target 3.0
    49.                
    50.                 #include "UnityCG.cginc"
    51.    
    52.                 struct appdata
    53.                 {
    54.                     float4 vertex : POSITION;
    55.                     float2 uv : TEXCOORD0;
    56.                 };
    57.    
    58.                 struct v2f
    59.                 {
    60.                     float2 uv : TEXCOORD0;
    61.                     UNITY_FOG_COORDS(1)
    62.                     float4 vertex : SV_POSITION;
    63.                 };
    64.                
    65.                 //Parameters
    66.                 float length;
    67.                 float aspectRatio;
    68.                 float lMap;
    69.                 float rMap;
    70.                 float lCut;
    71.                 float rCut;
    72.                 float lRemove;
    73.                 float rRemove;
    74.                 float lHeadTile;
    75.                 float lHeadMapTile;
    76.                 float4 _Color;
    77.    
    78.                 sampler2D _MainTex;
    79.                 float4 _MainTex_ST;
    80.                
    81.                 v2f vert (appdata v)
    82.                 {
    83.                     v2f o;
    84.                     UNITY_INITIALIZE_OUTPUT(v2f,o);
    85.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    86.                     o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    87.                     UNITY_TRANSFER_FOG(o,o.vertex);
    88.                     return o;
    89.                 }
    90.    
    91.                 float ilerp(float a, float b, float lerped)
    92.                 {
    93.                     return saturate((lerped - a) / (b - a));
    94.                 }
    95.    
    96.                 fixed4 frag (v2f i) : SV_Target
    97.                 {
    98.                     float2 orUV = i.uv;
    99.                     bool mirror = lMap > rMap;
    100.                     float lMapScaled = lMap / length;
    101.                     float rMapScaled = rMap / length;
    102.                     float lMapSide = min(lMapScaled , rMapScaled);
    103.                     float rMapSide = max(lMapScaled , rMapScaled);
    104.                     return ilerp(lMapSide, rMapSide, mirror ? 1 - orUV.x : orUV.x);
    105.                 }
    106.                 ENDCG
    107.             }
    108.         }
    109.         CustomEditor "TiledHintShaderInspector"
    110.     }
    111.  
    Smaller versions of the fragment shader seem to work just fine. I'm on Unity 5.3.6 (it's a rather big project, updating is a headache)

    The variable `length` is not 0 (I set it from code to nonzero values), and `lMap` and `rMap` are also set from code to different values. This is also true before changing the values (i.e. the material already has these values set like this.

    Any help? I don't really know what is going on. I've read that the Mali chips the Samsung s6 uses are known to give headaches, but we can't really drop it from the list of supported devices. Thanks!
     

    Attached Files:

  2. alvaro_perez_omnidrone

    alvaro_perez_omnidrone

    Joined:
    Jan 14, 2016
    Posts:
    2
    I finally found what the problem was! After comparing the compiled code of the shader I posted with one replacing `length` by a known possible value (which worked), I noticed that length got changed to xlat_varlength for some platforms. I didn't know it was an standard function. It seems that generally it just gets translated and the shader works anyway, but for this device, this doesn't seem to be very compatible with the way Unity handles the shader variables.

    So, changing `length` into some other name solved the problem completely.
     
    Last edited: Mar 27, 2017