DirectX 11 Alpha Blending Not Working

scribblesiam picture scribblesiam · Aug 5, 2012 · Viewed 9.9k times · Source

Okay, so I have been trying to get Alpha Blending to work in my 3D application but it just doesn't want to happen. I am drawing 2d images with an orthogonal projection at the very end of the rendering loop (depth testing remains enabled) and the image textures have transparent parts but they render black. Here is my blending code:

D3D11_BLEND_DESC blendStateDesc; 
ZeroMemory(&blendStateDesc, sizeof(D3D11_BLEND_DESC));
blendStateDesc.AlphaToCoverageEnable = FALSE;
blendStateDesc.IndependentBlendEnable = FALSE;        
blendStateDesc.RenderTarget[0].BlendEnable = TRUE;
blendStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
blendStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

if (FAILED(device->CreateBlendState(&blendStateDesc, &blendState))) {
printf("Failed To Create Blend State\n");
}
deviceContext->OMSetBlendState(blendState, NULL, 0xFFFFFF);

And if it helps here is the texture description:

D3D11_TEXTURE2D_DESC texDesc;
texDesc.Width = TextureWidth;
texDesc.Height = textureHeight;
texDesc.MipLevels = 1;
texDesc.ArraySize = 1;
texDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
texDesc.SampleDesc.Count = 1;
texDesc.SampleDesc.Quality = 0;
texDesc.Usage = D3D11_USAGE_IMMUTABLE;
texDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
texDesc.CPUAccessFlags = 0;
texDesc.MiscFlags = 0;

I am only using a single render target and not pre-multiplying alphas inside the shaders. I have looked everywhere and tried all manner of different combinations for the D3D11_BLEND_STATE but nothing has worked. The closest I can get is when I set the AlphaToCoverage to TRUE but then it doesn't work if I change the alpha of the vertices, and I know for what I'm doing AlphaToCoverage should be FALSE.

Answer

Kartik150 picture Kartik150 · Sep 29, 2016

I know this is a very old question but i had a similiar issue.

What i had to do was to make sure you set/enabble the blend state before drawing and then disable after the draw call of the transparent 2d image. More information can be found: DirectX Image texture quad displays underlying controls color where it is transparent