By pauld


2013-03-05 16:09:01 8 Comments

I have a shader that is supposed to work with either alpha blending or alpha testing, but the color values being passed in are premultiplied alpha values.

Is there an easy/standard way to have it produce the "correct" results for both alpha blending and alpha testing when using premultiplied alpha?

For example, if the final result of my shader is RGBA(1,0,0,0.75) straight alpha and therefore RGBA(0.75,0,0,0.75) premultiplied alpha, the result should be remain RGBA(1,0,0,0.75) when alpha testing.

Perhaps one option is "dividing out" the alpha channel if we are alpha testing, i.e. divide the R by A above so that we get 0.75/0.75 = 1.0, our original red value. But this becomes non-deterministic when the alpha goes to 0.

Maybe I can clarify with an example:

Say the back buffer is RGBA(1,1,1,1), and i am rendering with a partially transparent green color RGBA(0,1,0,0.75) (straight alpha). The color is passed in as premultiplied alpha as RGBA(0,0.75,0,0.75). When alpha blending, i blend using one + invsrcalpha and get a final color of RGBA(0.25,1,0.25,1). So my green channel stays at 100%, because the back buffer had green at 100%, and so did my original green color, so no way to get less than 100% on the green channel.

When Alpha testing, there is no blending going on. Instead, my green color will be rendered straight to the back buffer as RGBA(0,0.75,0,1). But the color I wanted was RGBA(0,1,0,1).

1 comments

@trapezoid 2013-12-17 08:32:24

Depending on the shader language/version you're using, you can use conditionals in your shader program to cover the 100% transparent case explicitly.

Alpha testing really seems like it should be like a distinct shader from alpha blending. It doesn't seem like you can use it with premultiplied alpha, since the equations for alpha testing involve a conditional, and can't be expressed with Porter-Duff operations.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Modern OpenGL, billboarding, alpha transparency

0 Answered Questions

Controllable color blending?

2 Answered Questions

[SOLVED] "Normal" Blend Mode with OpenGL Trouble

1 Answered Questions

[SOLVED] Encoding Float to RG/RGBA and Blending

1 Answered Questions

[SOLVED] Blending issue with framebuffer

1 Answered Questions

3 Answered Questions

[SOLVED] OpenGL additive blending not working as expected

1 Answered Questions

0 Answered Questions

AlphaToCoverage + Alpha Blending = Artifacts

1 Answered Questions

[SOLVED] OpenGL ES Shader help (Blending)

Sponsored Content