Search Unity

Simple 2D Shader - Performance issue on Android?

Discussion in 'Shaders' started by odysoftware, Dec 4, 2018.

  1. odysoftware

    odysoftware

    Joined:
    Jul 21, 2015
    Posts:
    84
    Hello,

    Shaders are a small mystery to me - and I never had the time to dive into them - so I have been fine using what I find available on the net.

    The following shader is a generated one and it is working totally fine on a lot of devices. But for example on a Samsung SM-T580 tablet I can see that just when using this shader instead of the default Sprite Shader - I get around 8ms more render time in unity profiler (!).

    Now I wonder what is causing this and since I dont know much about shaders - maybe someone can tell me what is so expensive here?

    What the shader does is adding an image (some kind of cloud image - _NewText_1) over the current image sprite and it does this only on the part where the current sprite image alpha is not zero. And then it animates this image with some uv distortion and a simple uv movement in x and y direction.

    I think the transparency may be the most expensive task - but we are talking about 64x64 images - so it is not required on a lot of pixels.

    Area1 copy.png

    The strange thing is even when I am using a 512x512 image - I don't get any difference in render times -even though we would have around the 8x pixel requirement (?)

    To measure performance I was using unitiys profiler and looked at the graph with those 2 shaders used. And when using a default shader - I can see an almost 8ms VSync wait time on the renderer - so a lot of performance left.... And with the shader I can see no vsync wait anymore - but a 8ms Render Time - bringen the FPS from steady 60fps to 55 to 60 swinging...

    So maybe someone can enlighten me here what might be happening here or why the res of the texture does not effect performance or maybe if there is a way to improve this shader or to make it more simple so it is more performing. For example the alpha values would only need to check if it is black or transparent - nothing inbetween - I am only using this to determine where I will draw the fog map on with black or transparent pixels.

    Afterwards this whole 64x64 will be stretched over a large detailed map image to display the fog on special areas. As soon as I use the Standard Sprite/Shader or FastUIDefault shader - everything is super fast and no issues. So overdrawing with a low res pic is not an issue here (fill rate related etc.). But as soon as I change to this shader 8ms render time increase... regardless of the texture size 64 or 512....

    Thanks in advance for your time,
    Oliver


    Code (CSharp):
    1.  
    2. Tags {"Queue" = "Transparent" "IgnoreProjector" = "true" "RenderType" = "Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" }
    3. ZWrite Off Blend SrcAlpha OneMinusSrcAlpha Cull Off
    4.  
    5. // required for UI.Mask
    6. Stencil
    7. {
    8. Ref [_Stencil]
    9. Comp [_StencilComp]
    10. Pass [_StencilOp]
    11. ReadMask [_StencilReadMask]
    12. WriteMask [_StencilWriteMask]
    13. }
    14.  
    15. Pass
    16. {
    17.  
    18. CGPROGRAM
    19. #pragma vertex vert
    20. #pragma fragment frag
    21. #pragma fragmentoption ARB_precision_hint_fastest
    22. #include "UnityCG.cginc"
    23.  
    24. struct appdata_t{
    25. float4 vertex   : POSITION;
    26. float4 color    : COLOR;
    27. float2 texcoord : TEXCOORD0;
    28. };
    29.  
    30. struct v2f
    31. {
    32. float2 texcoord  : TEXCOORD0;
    33. float4 vertex   : SV_POSITION;
    34. float4 color    : COLOR;
    35. };
    36.  
    37. sampler2D _MainTex;
    38. float _SpriteFade;
    39. sampler2D _NewTex_1;
    40.  
    41. v2f vert(appdata_t IN)
    42. {
    43. v2f OUT;
    44. OUT.vertex = UnityObjectToClipPos(IN.vertex);
    45. OUT.texcoord = IN.texcoord;
    46. OUT.color = IN.color;
    47. return OUT;
    48. }
    49.  
    50.  
    51. float2 AnimatedOffsetUV(float2 uv, float offsetx, float offsety, float zoomx, float zoomy, float speed)
    52. {
    53. speed *=_Time*25;
    54. uv += float2(offsetx*speed, offsety*speed);
    55. uv = fmod(uv * float2(zoomx, zoomy), 1);
    56. return uv;
    57. }
    58. float2 DistortionUV(float2 p, float WaveX, float WaveY, float DistanceX, float DistanceY, float Speed)
    59. {
    60. Speed *=_Time*100;
    61. p.x= p.x+sin(p.y*WaveX + Speed)*DistanceX*0.05;
    62. p.y= p.y+cos(p.x*WaveY + Speed)*DistanceY*0.05;
    63. return p;
    64. }
    65. float4 frag (v2f i) : COLOR
    66. {
    67. float2 DistortionUV_1 = DistortionUV(i.texcoord,5,5,0.3,0.3,0.01);
    68. float2 AnimatedOffsetUV_1 = AnimatedOffsetUV(DistortionUV_1,0.1,0.1,1,1,0.01);
    69. float4 NewTex_1 = tex2D(_NewTex_1,AnimatedOffsetUV_1);
    70. float4 _MainTex_1 = tex2D(_MainTex, i.texcoord);
    71. float4 MaskAlpha_1=NewTex_1;
    72. MaskAlpha_1.a = lerp(_MainTex_1.a, 1 - _MainTex_1.a ,0);
    73. float4 FinalResult = MaskAlpha_1;
    74. FinalResult.rgb *= i.color.rgb;
    75. FinalResult.a = FinalResult.a * _SpriteFade * i.color.a;
    76. return FinalResult;
    77. }
    78.  
     
    Last edited: Dec 4, 2018