DX11 Alpha Blending When Rendering Texture

COMPLETION:

Solved ... just need to learn how alpha blending works in depth. I should have: oBlendStateDesc.RenderTarget [a] .DestBlendAlpha = D3D11_BLEND_ZERO; ... set D3D11_BLEND_ONE to keep the alpha.

When rendering in a backbuffer, the problem will not be noticed as the colors mix normally and this is the final result. When rendering a texture, the same applies, only when the texture is returned to the buffer, the incorrect alpha plays a role in the incorrect mixing of the texture in the backbuffer.

Then I ran into another issue where the alpha seemed to decrease. This is because the color is mixed twice, for example ...

Source.RBGA = 1.0f, 0.0f, 0.0f, 0.5f
Dest.RGBA   = 0.0f, 0.0f, 0.0f, 0.0f

      

Paste Texture ...

Result.RGB = Source.RBG * Source.A +  Dest.RGB * (1 - Source.A) = 0.5f, 0.0f, 0.0f
Result.A   = Source.A * 1 + Dest.A * 1 = 0.5f

      

Now...

Source.RBGA = 0.5f, 0.0f, 0.0f, 0.5f
Dest.RGBA   = 0.0f, 0.0f, 0.0f, 0.0f

      

Give yourself to the backbuffer ...

Result.RGB = Source.RBG * Source.A +  Dest.RGB * (1 - Source.A) = 0.25f, 0.0f, 0.0f
Result.A = Source.A * 1 + Dest.A * 1 = 0.5f

      

, backbuffer blendstate, SrcBlend D3D11_BLEND_ONE, .

, , ....

EDITEND

, , , .

2D, , -.

, , , -, , , ... .

blend, . backbuffer . backbuffer . , , , , .

:

D3D11_BLEND_DESC oBlendStateDesc;

oBlendStateDesc.AlphaToCoverageEnable = 0;
oBlendStateDesc.IndependentBlendEnable = 0; //set to false, dont need loop below... but just incase

for (unsigned int a = 0; a < 8; ++a)
{
oBlendStateDesc.RenderTarget[a].BlendEnable = 1;
oBlendStateDesc.RenderTarget[a].SrcBlend = D3D11_BLEND_SRC_ALPHA;
oBlendStateDesc.RenderTarget[a].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
oBlendStateDesc.RenderTarget[a].BlendOp = D3D11_BLEND_OP_ADD;
oBlendStateDesc.RenderTarget[a].SrcBlendAlpha = D3D11_BLEND_ONE;
oBlendStateDesc.RenderTarget[a].DestBlendAlpha = D3D11_BLEND_ZERO;
oBlendStateDesc.RenderTarget[a].BlendOpAlpha = D3D11_BLEND_OP_ADD;
oBlendStateDesc.RenderTarget[a].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
}

// Create the blend state from the description
HResult = m_poDevice->CreateBlendState(&oBlendStateDesc, &m_poBlendState_Default);

m_poDeviceContext->OMSetBlendState(m_poBlendState_Default, nullptr, 0xffffff);

      

- , , ?

EDIT: AlphaToCoverageEnable true, , . , , , blend... - , backbuffer :/ desc...

m_oTexureDesc.Width = a_oDesc.m_uiWidth;
m_oTexureDesc.Height = a_oDesc.m_uiHeight;
m_oTexureDesc.MipLevels = 1;
m_oTexureDesc.ArraySize = 1;
m_oTexureDesc.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
m_oTexureDesc.SampleDesc.Count = 1; //No sampling
m_oTexureDesc.SampleDesc.Quality = 0;
m_oTexureDesc.Usage = D3D11_USAGE_DEFAULT; //GPU writes & reads
m_oTexureDesc.BindFlags = D3D11_BIND_RENDER_TARGET | D3D11_BIND_SHADER_RESOURCE;
m_oTexureDesc.CPUAccessFlags = 0;
m_oTexureDesc.MiscFlags = 0;

      

EDIT: ... Text

  • backbuffer - AlphaBlending.
  • Texture rendering - AlphaBlending enabled.
  • Backbuffer rendering - disabled AlphaBlending.
  • The letter T is taken from the font file

* When rendering with AB disabled, letters match exactly (compare 4 and 3)

* When rendering to the backbuffer with AB enabled, the letters are slightly rendered (barely noticeable), but still blended (compare 4 and 1)

* When rendering textures with AB enabled, the letters are even more noticeably blurred without mixing at all. (compare 4 and 2)

Not sure why the colors are washed out with alpha blending enabled ... but maybe that's the key?

EDIT: If I clear the render texture to say ... 0.0f, 0.0f, 1.0f, 1.0f (RGBA, blue) ... this is the result: enter image description here

Only pixels with alpha> 0.0f and <1.0f are blended with color. Another clue, but I have no idea how to solve this problem ...

+3


source to share





All Articles