Search Unity

how to turn off Sprite/Default's shader keyword "ETC1_EXTERNAL_ALPHA" in android?

Discussion in 'General Graphics' started by colin299, Oct 17, 2017.

  1. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    Hi, in my project when I am using the frame debugger,
    I found that all my sprite renderer's render passes has keyword ETC1_EXTERNAL_ALPHA turned on.
    I am very sure that all my sprite atla(created by sprite packer) are ETC2 RGBA compressed.
    (I understand that I can create ETC1 split alpha sprite atla,
    by choosing ETC1 RGB compression for all sprites within a packing tag)
    //-------------------------------------------------------------------------
    the following 3 images are editor information when the game is running in editor
    1.frame debugger(key word ETC1_EXTERNAL_ALPHA is on, but it should not)
    2017-10-17 11_14_15-.jpg
    2.frame debugger(becuase ETC1_EXTERNAL_ALPHA is on, a 1x1 default white tex is used)
    2017-10-17 11_52_37-.jpg
    3.sprite packer(this atla is ETC2 RGBA compressed, so keyword ETC1_EXTERNAL_ALPHA should not be ON when rendering)
    2017-10-17 11_46_28-.jpg

    what this ETC1_EXTERNAL_ALPHA keyword does in the actual shader, from UnitySprites.cginc:
    it always sample a 1x1 white texture, and try to replace original sprite's alpha by the r channel of that split alpha texture(a 1x1 white texture), but "_EnableExternalAlpha" is set to 0 so it actually just doing nothing but wasting GPU fragment time.
    Code (CSharp):
    1. fixed4 SampleSpriteTexture (float2 uv)
    2. {
    3.     fixed4 color = tex2D (_MainTex, uv);
    4.  
    5. #if ETC1_EXTERNAL_ALPHA
    6.     fixed4 alpha = tex2D (_AlphaTex, uv);
    7.     color.a = lerp (color.a, alpha.r, _EnableExternalAlpha);
    8. #endif
    9.  
    10.     return color;
    11. }
    //----------------------------------------------------------------------------
    Questions:
    a)Is this an intended behavior? or is ETC1_EXTERNAL_ALPHA always turned on because I am inside the editor but not an actual android device?

    b)how can I turn this off? I just want to improve sprite renderer's performance in actual android devices -> "saving 1 tex2D() & 1 lerp()"
     
    Last edited: Oct 17, 2017
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Hi!
    a) Yes, this is intended behaviour. If it's not turned on always, it's breaking sprite batching.
    b) You can't turn it off at the moment. A temporary solution would be to write your own shader without ETC1_EXTERNAL_ALPHA defined.
    Please submit a bug, we will decide what the best solution would be.
     
    colin299 likes this.
  3. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    181
    thanks,
    does that mean I should
    [solution A]
    a1 write my new Sprite/Default shader, without ETC1_EXTERNAL_ALPHA defined
    a2 create 1 singleton material using that shader in (a1)
    a3.write an editor script that,
    a3-1.detect every sprite renderer in scene & project
    a3-2.if any sprite renderer don't have a material, assign my new singleton SpriteDefault material to it?
    ---------------------------------
    or
    ---------------------------------
    [solution B]
    just write the shader and replace unity's Sprite/Default shader? (I am not sure if it is possible or how to replace it)
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    3,014
    Well, definitely not solution B :)
    I'd say that the script you're proposing to write in (a3-2) should also replace the default sprite material with your custom one.
     
    colin299 likes this.