Pre-multiplied Alpha will keep getting rediscovered

When blending transparent objects in graphics, we need to combine the color of the current pixel (the “source”) with the background color (the “destination”), so a pixel with 25% transparent will mix: out.rgb = src.rgb*0.25 + dst.rgb*0.75. This is normally calculated by a fixed-function state called the blend unit, and there’s only a limited configuration for how to set things up. Normally, we put the “transparency amount” inside the alpha channel of the pixel, and set SrcFactor = SrcAlpha and DstFactor = OneMinusSrcAlpha, which gives us a generalized version of the equation above: out.rgb = src.rgb*src.a + dst.rgb*(1-src.a).

In a classic fixed-function world, this makes perfect sense, but in a world with powerful programmable shaders, you might notice that src.rgb*src.a as being a bit funny, as that’s something we can do ourselves in our shaders. So, from a code cleanliness perspective, moving the src.rgb*src.a part into the shader is easy to do, and relatively free. We cannot do the same for the dst.rgb part, though, since a shader itself cannot access the destination framebuffer contents on most GPUs, so we still need that as part of the blend unit configuration. After moving src.rgb*src.a into the shader, we don’t want to “double-count” the alpha, so we end up with a blend configuration of SrcFactor = One, DstFactor = OneMinusSrcAlpha, which gives us this expression: out.rgb = src.rgb + dst.rgb*(1-src.a).

First off, this setup actually lets us handle additive blending in the same shader: if we output src.a = 0 from our shader, then after simplifying some terms, we end up with out.rgb = src.rgb + dst.rgb. So this setup handles both transparent blending and emissive blending in one equation, just by adjusting the shader. This setup was famously used by Diablo III as detailed in Julian Love’s 2013 GDC Talk around the 20 minute mark, and given the name “BlendAdd”. By using this combined with a specially authored output alpha channel, they could even mix different parts of transparent and additive blending within the same draw call, and end up somewhere in between.

However, the more standard name for this is “pre-multiplied alpha”, which I think is a poor name for it. This name comes from Porter and Duff’s classic ACM paper Compositing Digital Images. In that world, they stored images on disk as RGBA data, would store a 25% full red pixel as (0.25, 0, 0, 0.25): the colors themselves are stored “pre-multiplied” together with the alpha. In the film and VFX industry, this convention eventually became known as “associated alpha”. Unfortunately, the first widespread image format with transparency, the PNG format, chose not to use such a convention, which I suspect is one of the reasons premultiplied alpha is not as well-known.

When rendering graphics at real-time, the choice of whether to put the multiplication in the shader or the blend unit is a lot closer together, and I don’t think “pre-multiplied” is as good a name. However, this convention still has advantages, and is worth using.

In this scheme, rather than think of the output alpha of our pixel shade not as our pixel’s transparency, but instead, how much of destination we would like to block. This is what lets us combine the additive and transparent features in the same draw: additive draws would like all of the destination framebuffer, transparent draws would like some of the destination framebuffer, and of course, opaque objects would like none of the destination framebuffer. By combining all of this in one blend mode, premultiplied alpha (or BlendAdd) can be considered the universal blend mode.

If you’ve ever tried to do work rendering parts of a scene to an off-screen render target and composite it back later the normal way, you might have run into cases where it doesn’t work. For instance, rendering emissive particles offscreen will leave the alpha channel at 0, so it won’t get blended back in later. Or cases where transparency was “double-applied”: once when blending into the offscreen target, and once again when blending back into the main target. A common way to solve this is to use “split blend functions” which handle the color and alpha blend functions separately. However, premultiplied alpha solves this one too: by separating out the destination framebuffer obscurance from the color to blend in, we can combine additive, transparent, and opaque in one image, so everything can be captured together. The neat thing is that when rendering out to the offscreen target, if you also use One/OneMinusSrcAlpha, for both color and alpha, you end up with the exact color and alpha targets you need! You just need to blend them back in with premultiplied alpha. Porter and Duff actually wrote about this one too:

The pleasant result that the color channels are handled with the same computation as alpha can be traced back to our decision to store pre-multiplied RGBA quadruples.

This last fact is the one that convinced me to become a Premultiplied Alpha convert; I’ve had to explain to artists why their emissive particles weren’t showing up in e.g. character portrait dialogues before, and it was always a sad conversation. Setting up the rest of the engine to do split blend modes requires a lot of special cases and hackery and still isn’t right. Premultiplied Alpha really just works.

The sad thing is that none of this is new. Tom Forsyth wrote about all of this, and more, in 2006 and 2015. Shawn Hargreaves wrote about this in 2019 and also found the render target compositing case. Eric Haines wrote about this in 2016. Premulitplied Alpha will keep getting rediscovered.