premultiplication in FBO for anti-aliasing

I’m using an FBO to render out several individual images to do anti-aliasing.

  • clear scene out with (0,0,0,0)
  • draw geometry without color writes (zpass)
  • with depth test set to LEQUAL and blending set to GL_ONE/GL_ONE render geometry

I’m left with however many sampled images and am currently combining them by hand (just to check). Using a box filter, I multiply every channel in every image by 1/n and add them together. This gives basically correct results on the object itself, but when I composite over something else, I notice there’s a slight black edge around my objects, where I’m not taking premultiplication into account.

If I understand correctly, colors along the border with an RGBA of (.5,.5,.5,.5) should look more like (1,1,1,.5) so it with won’t be so dark when its composited. What’s the actual premultiplication that needs to be done?

It seems like this normally wouldn’t be an issue if the scene is completely full of rendered geometry, but this isn’t the case every render.

I’ve attached two images–one over a transparent background and the other over an opaque white background, where the problem shows up.

This may be useful to you:

For example, you want to composite 100% white (with 50% translucency) on top of 100% white, and you don’t want a black border.

So first premultiply your incoming fragment color by alpha (somehow – in the shader, in the texture, whatever), yielding (0.5,0.5,0.5,0.5).

Then blend it on top using (1,1-src_alpha) additive blending (i.e. GL_ONE, GL_ONE_MINUS_SRC_ALPHA, FUNC_ADD).

You end up with:

(0.5,0.5,0.5) + 0.5 * (1,1,1) = (1,1,1)