1
\$\begingroup\$

I am writing a program where with Directx11 I am rendering a texture to a flat rectangle something along the line a of 2D engine. Now parts of this image need to be transparent, to this effect I looked into alpha blending and it seemed to work until i changed textures and realized that it had removed all black pixels and made the whole image transparent not just those pixels that are blank. So how in Directx11 do you have it so that you preserve the transparency of the original image. I am using a ShaderResourceView that is a loaded png for the texture

**//Setup Blend State
D3D11_BLEND_DESC BlendStateDescription;
ZeroMemory(&BlendStateDescription, sizeof(D3D11_BLEND_DESC));


BlendStateDescription.RenderTarget[0].BlendEnable = TRUE;
BlendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
BlendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;


BlendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC1_COLOR;
BlendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
BlendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE;
BlendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
BlendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;


dev->CreateBlendState(&BlendStateDescription, &blend);
float blendFactor[] = { 0, 0, 0, 0 };
UINT sampleMask = 0xffffffff;

devcon->OMSetBlendState(blend, blendFactor, sampleMask);**

What it looks like right now

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Your blend state isn't set up to be standard alpha-blending. The general blend state specifies dest.rgb = src.rgb * [SrcBlend] [Op] dest.rgb * [DestBlend], and likewise for the separately blended alpha channel. For standard alpha-blending, you should use:

SrcBlend = D3D11_BLEND_SRC_ALPHA
DestBlend = D3D11_BLEND_INV_SRC_ALPHA
BendOp = D3D11_BLEND_OP_ADD
// dest.rgb = src.rgb * src.a + dest.rgb * (1 - src.a) ==> standard alpha blended color

SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA
DestBlendAlpha = D3D11_BLEND_ONE
BendOp = D3D11_BLEND_OP_ADD
// dest.a = src.a * (1 - dest.a) + dest.a
//       == src.a + dest.a - src.a * dest.a
//       == 1 - (1 - src.a) * (1 - dest.a) ==> preserves alpha to target (optional)
\$\endgroup\$
6
  • \$\begingroup\$ That did not fix the problem, while it preserves the black pixels now the overall transparency is still there, and the areas where there are no colors now has a white fill. \$\endgroup\$ Dec 12, 2013 at 6:59
  • \$\begingroup\$ If you need me to i can send you what it looks like right now, and what i want to appear on screen \$\endgroup\$ Dec 12, 2013 at 6:59
  • \$\begingroup\$ @MooseBoys This is premultiplied-alpha, which is the default in XNA (its content pipeline converts images to premultiplied-alpha) but probably not right if the OP is just loading a regular PNG file into a texture and using it with stock D3D11. (Also, I think you meant INV_SRC_ALPHA, not INV_SRC1_ALPHA, for DestBlend.) \$\endgroup\$ Dec 12, 2013 at 7:05
  • \$\begingroup\$ @NathanReed Yeah this is just plain Directx11 c++ \$\endgroup\$ Dec 12, 2013 at 7:07
  • \$\begingroup\$ @NathanReed Good catch. Fixed both. \$\endgroup\$
    – MooseBoys
    Dec 12, 2013 at 7:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .