Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bug: correct the blend function in DX12 #3447

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/imgui_impl_dx12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2020-09-01: DirectX12: Corrected the blend function.
// 2019-10-18: DirectX12: *BREAKING CHANGE* Added extra ID3D12DescriptorHeap parameter to ImGui_ImplDX12_Init() function.
// 2019-05-29: DirectX12: Added support for large mesh (64K+ vertices), enable ImGuiBackendFlags_RendererHasVtxOffset flag.
// 2019-04-30: DirectX12: Added support for special ImDrawCallback_ResetRenderState callback to reset render state.
Expand Down Expand Up @@ -552,8 +553,8 @@ bool ImGui_ImplDX12_CreateDeviceObjects()
desc.RenderTarget[0].SrcBlend = D3D12_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D12_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D12_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_ZERO;
desc.RenderTarget[0].SrcBlendAlpha = D3D12_BLEND_ONE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intended to write that D3D12_BLEND_SRC_ALPHA would be consistent with other backends, but...

It appears backends has different idea how to blend alpha channel into final image:

Backend Alpha Channel Blend Equation
D3D9 src * SRC_ALPHA + dest * INV_SRC_ALPHA
D3D10 src * INV_SRC_ALPHA + dest * 0
D3D11 src * INV_SRC_ALPHA + dest * 0
D3D12 src * 1 + dest * INV_SRC_ALPHA
OpenGL src * SRC_ALPHA + dest * INV_SRC_ALPHA
Metal src * SRC_ALPHA + dest * INV_SRC_ALPHA
Vulkan src * INV_SRC_ALPHA + dest * 0
Allegro src * SRC_ALPHA + dest * INV_SRC_ALPHA

I guess it does not matter until ImGui is rendered into the texture.
What is desired equation?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

result alpha = 1 - result transparency
result transparency = src transparency * dest transparency
src transparency = 1 - src alpha
dest transparency = 1 - dst alpha

so
result alpha = src alpha * 1 + dest alpha * (1 - src alpha)

D3D12 is correct

desc.RenderTarget[0].DestBlendAlpha = D3D12_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOpAlpha = D3D12_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
}
Expand Down