Search Unity

alpha test performance

Discussion in 'Shaders' started by flapyfox, Apr 7, 2012.

  1. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    using the transparency/cutout shader is lowering my fps by 7 frames, isn't alpha test cheap to render? is there any guidelines when using alpha test in Unity? Its even slower than the transparent /diffuse shader which uses much more expensive alpha blending.
     
    Last edited: Apr 7, 2012
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    What GPU?
     
  3. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    As far as I remember, there is no hardware support for alpha testing in OpenGL ES 2.x; thus, Unity adds some code to the fragment program in order to implement it, presumably discard instructions, which are rather expensive on some GPUs (specifically the GPUs in iOS devices) because certain optimizations are deactivated as soon as you use a discard instruction.
     
  4. speps

    speps

    Joined:
    Oct 13, 2009
    Posts:
    22
    So what is the proper way of replacing alpha test if you have no alpha test acceleration hardware and discard is expensive? I find it rather intriguing that there is no way to do alpha test efficiently.
     
  5. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    iPad1
     
  6. Martin-Kraus

    Martin-Kraus

    Joined:
    Feb 18, 2011
    Posts:
    617
    It depends.

    As royter mentioned, alpha blending is usually more efficient.

    In some cases, using more complex geometry might be better than using alpha masks.
     
    Last edited: Apr 7, 2012
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I recommend a combination. Don't do the 1-quad approach. Instead, model the geometry, use an opaque material for the appropriate areas, and use a blending or cutout one where it can't be opaque. You don't have to go overboard with the vertex count for this to yield a huge performance improvement. (Red material is alpha blended, blue is opaque.)
     

    Attached Files:

  8. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    would you recommend this method for a plant with hundreds of big leaves?
     
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    depends how much screen space in total the plants take up and how many there are. Surely you know this?

    Jessy's suggestion works best of all for large gui's or large screen fills. Probably medium too. 42.
     
    Jessy likes this.
  10. flapyfox

    flapyfox

    Joined:
    Sep 9, 2008
    Posts:
    408
    The plants are taking about 40 percent of screen space in worst cases.
     
  11. Bero

    Bero

    Joined:
    Oct 16, 2012
    Posts:
    5
    Hi!

    Did anyone maybe found a solution for warning:

    "Shader is using clip instruction (usually caused by alpha test)"

    I can say, that even with this warning, on Android Samsung Galaxy S and Samsung Galaxy S Advance, scene works fast.
    By fast I mean as fast as using mobile/diffuse shader. If I use alpha test shader, there is no performance drop.

    It might happened that on some other Android device FPS drops...
     
  12. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    As a workaround you can use an IF statement in the shader. While this has the same negative impact as discard, it doesn't discard the entire occlusion pipeline for tile deferred renderers (so I imagine) so should still be quicker than a discard on Power VR GPUs... Although you'll have to test this!

    Better still use alpha blend. Fast on desktop (come on, it's desktop), fastest way on mobile.

    it *WILL* have a performance hit on quite a few droid devices. Anything with a Power VR GPU for a start.

    Don't forget its not just screen space, but how much is overdrawing, it's probably just one of those things you're going to have to make compromises on.
     
    Last edited: Oct 16, 2012