How AlphaBlend Blendstate works in XNA when accumulighting light into a RenderTarget?

Posted by cubrman on Game Development See other posts from Game Development or by cubrman
Published on 2013-10-30T09:08:45Z Indexed on 2013/10/30 10:24 UTC
Read the original article Hit count: 291

Filed under:
|
|

I am using a Deferred Rendering engine from Catalin Zima's tutorial:

His lighting shader returns the color of the light in the rgb channels and the specular component in the alpha channel. Here is how light gets accumulated:

Game.GraphicsDevice.SetRenderTarget(LightRT);

Game.GraphicsDevice.Clear(Color.Transparent);

Game.GraphicsDevice.BlendState = BlendState.AlphaBlend;

// Continuously draw 3d spheres with lighting pixel shader.
...

Game.GraphicsDevice.BlendState = BlendState.Opaque;

MSDN states that AlphaBlend field of the BlendState class uses the next formula for alphablending:

(source × Blend.SourceAlpha) + (destination × Blend.InvSourceAlpha),

where "source" is the color of the pixel returned by the shader and "destination" is the color of the pixel in the rendertarget.

My question is why do my colors are accumulated correctly in the Light rendertarget even when the new pixels' alphas equal zero? As a quick sanity check I ran the following code in the light's pixel shader:

float specularLight = 0;

float4 light4 = attenuation * lightIntensity * float4(diffuseLight.rgb,specularLight);

if (light4.a == 0)
light4 = 0;

return light4;

This prevents lighting from getting accumulated and, subsequently, drawn on the screen. But when I do the following:

float specularLight = 0;

float4 light4 = attenuation * lightIntensity * float4(diffuseLight.rgb,specularLight);

return light4;

The light is accumulated and drawn exactly where it needs to be. What am I missing? According to the formula above:

(source x 0) + (destination x 1) should equal destination, so the "LightRT" rendertarget must not change when I draw light spheres into it!

It feels like the GPU is using the Additive blend instead:

(source × Blend.One) + (destination × Blend.One)

© Game Development or respective owner

Related posts about XNA

Related posts about hlsl