claurendine
Topic Author
Posts: 2
Joined: 17 Nov 2016, 17:51

alpha blending when rendering to a texture

05 Jan 2017, 19:39

I am trying to render with transparency to a texture which is then embedded within my 3d scene..

The texture is generated, but the alpha values from the xaml surface is clamped to zero or one.. fully transparent anywhere the source alpha is zero or fully opaque with any alpha value greater than zero.

When I render the same xaml surface on top of my framebuffer as an overlay, it composes properly and background colors with alpha apply as expected..

I have the suspicion that the blend state inside IRenderer::Render() is something like:

blendStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_MAX;
blendStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;


and the alpha value from the source is not preserved in the destination buffer.

For rendering to an offline texture I would expect a blend state something like:

blendStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
blendStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;

This should allow the alpha value to flow through to the destination.

Is there an option in the SDK to allow alpha to be applied to the output color buffer? Or is there a way to alter the applied blend state?
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: alpha blending when rendering to a texture

10 Jan 2017, 04:38

We use premultiplied alpha in all our pipeline. So, the correct blending mode should be:
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
Inside the 1.3 SDK you can also find the sources of our reference renderers.
 
claurendine
Topic Author
Posts: 2
Joined: 17 Nov 2016, 17:51

Re: alpha blending when rendering to a texture

13 Jan 2017, 15:53

Thank you for the fast response.

I experimented with changing blend states as suggested and I am still not able to get the composed alpha value passed through to the alpha channel of my render target. With premultiplied alpha it works great for accumulating color but doesn't seem to pass along the accumulated alpha.

The suggested blend state calculates alpha = 1 * A + (1 - A) * 1. Which always results in 1, this is what I am experiencing. There must be a discard in the shader when A = 0 to allow the destination alpha to fall through.

Is there a separate blend state used during composition vs final presentation into the render target?
 
User avatar
jsantos
Site Admin
Posts: 2904
Joined: 20 Jan 2012, 17:18
Contact:

Re: alpha blending when rendering to a texture

13 Jan 2017, 20:55

There are no distinct blendstates for composition. Thanks to premultplied alpha we can use the same calculations everywhere. Even the alpha channel use the same formula than the color channel.

If your destination target has an alpha of 1 (so, the render target is solid) it makes sense that the final output is always 1 (solid).

If you clear your render target to (0, 0, 0, 0) I think you will get the desired effect. Just make sure that the colors in your render target are alpha premultiplied. That's the important trick.

Who is online

Users browsing this forum: No registered users and 0 guests