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. Dismiss Notice

Alpha To Coverage Performance

Discussion in 'Shaders' started by FiveFingerStudios, Nov 20, 2021.

  1. FiveFingerStudios

    FiveFingerStudios

    Joined:
    Apr 22, 2016
    Posts:
    508
    I've been using an Alpha to Coverage shader for foliage in the mobile version of my game. I've gotten great results with how it looks, but I've been wondering....how does it perform?

    I've been searching everywhere but can't find the answer...how it performs compared to a normal cutout or transparency shader.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    The answer is: it depends.

    Compared to alpha testing, alpha to coverage is always more expensive. But the degree to how much more expensive ranges from imperceptibly to significantly, and depends almost entirely on that else you're rendering.

    If you have a single object on front of another entirely opaque wall, you'll have a hard time calculating the performance difference between alpha test and alpha to coverage. Over more complex geometry, it might be measurable. If you’re using this for something like grass or dense foliage the performance difference can be significant.

    Alpha blending vs alpha testing vs alpha to coverage, in the cases where alpha testing is faster than alpha blending, like when there are a lot of overlapping layers, then alpha to coverage will also likely be faster. Similarly in cases where alpha blending outperforms alpha testing, like can be the case when the texture is very noisy or mostly very thin lines, then similarly alpha to coverage will be much slower.


    To understand where alpha to coverage might be is expensive you need to understand what makes MSAA more expensive, and why alpha testing is sometimes more expensive than alpha blending.

    First, I’m not going to dive too deep into MSAA, but I’m going to try and focus on the high level. For normal opaque geometry, MSAA effectively renders the depth at a higher resolution that the color resolution, then keeps track of the color of any triangles are visible within a single pixel in the higher resolution depth, and how many of those depth “samples” are for each triangle. If you’re using 4x MSAA you can “see” up to 4 triangles in the final image. But the advantage of MSAA over just rendering at a higher resolution is the cost of rendering multiple surfaces per pixel mostly happens at the triangle edges or intersections.

    Alpha testing doesn’t interact with MSAA. Any pixel that is opaque via alpha testing is opaque for that entire pixel. So you only one triangle will be visible for that pixel (ignoring intersecting geometry). Alpha to coverage on the other hand is like adding more MSAA-ified “edges” everywhere, meaning multiple triangles are visible and rendered anywhere the alpha to coverage isn’t completely opaque or completely invisible.

    Additionally both alpha testing and alpha to coverage can have an additional cost on some mobile hardware that desktop doesn’t have. This is a combination of them both potentially disabling the depth buffer compression some mobile GPUs use. And tile based deferred GPUs may have to render alpha tested or alpha to coverage surfaces twice, once for the hardware deferred pass, and again for the color.

    But where both alpha testing and alpha to coverage have an advantage over alpha blending on mobile is that tile based deferred rendering. Any surfaces the fully opaque areas hide won’t get rendered.
     
  3. FiveFingerStudios

    FiveFingerStudios

    Joined:
    Apr 22, 2016
    Posts:
    508
    Thanks a lot for the detailed answer. It’s a lot and I needed to reread it a few times, but I’m starting to understand it a bit more.

    If I’m understanding it correctly for mobile, it’s highly scene dependent and hardware dependent.


    My game is being ported to the Oculus Quest so it’s critical I get this right…thanks again.
    .
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,236
    For Falcon Age I changed all of the smoke FX in the game from alpha blended to “alpha to coverage” (actually explicitly setting the mask bits using a blur noise dither). Comparing the original FX to the new ones in an empty scene, the alpha blended originals actually perform slightly better on average. The problem was if you stood inside of an alpha blended explosion such that the smoke particles entirely covered your screen, the frame rate dropped below 50 fps, even when using level 5 FFR! The “alpha to coverage” replacement on the other hand maintained frame rate, and in some complex scenes the frame times got better when standing inside a full screen explosion.
    https://64.media.tumblr.com/f8a8ce5...bb7e5bea3f36090e472e5bed00df14ff93078f98.gifv

    The basic reason being rendering the smoke particles could be cheaper than rendering the rest of the scene. This really only works on mobile GPUs like the one on the Quest & Quest 2. And because the smoke puffs are big blobby shapes that can block a lot.

    On the other hand we basically removed all grass and foliage from the Quest build of the game because it was too expensive to render.
     
  5. FiveFingerStudios

    FiveFingerStudios

    Joined:
    Apr 22, 2016
    Posts:
    508
    Wow...huge performance difference...thanks for sharing that!
     
  6. jubaerjams8548

    jubaerjams8548

    Joined:
    Jun 8, 2020
    Posts:
    33
    Hey bgolus I am using a Foliage sway shader which is using alpha blend, It was totally fine but when i get closer to the foliage, performance drops, what could be the problem with it? and is Src Alpha One be better than alpha blend in case of performance?