Imgui: Dx11 Overlay Transparency Issue

Created on 31 Oct 2016  路  3Comments  路  Source: ocornut/imgui

Directx11 does not like a transparent background. With an alpha set to 255 it displays as if it was 0. The max alpha is 126. I am trying to create a desktop overlay application and would like to use dx11 but I'm stuck with dx9 until fixed.

Screenshots:
dx9 vs dx11 :
aoajzbd
dx9 dekstop overlay :
3bqxjgq

How to recreate it: (based of demo app)
1) Set CreateWindow to flag to WS_POPUP from WS_OVERLAPPEDWINDOW
2) Set clear_col to ImColor(0, 0, 0, 0) from ImColor(114, 144, 154)
3) Add after CreateWindow the following,

    #pragma comment(lib, "dwmapi.lib")
    #include "Dwmapi.h"

    .....

    SetLayeredWindowAttributes(hwnd, 0, 0, LWA_ALPHA);
    SetLayeredWindowAttributes(hwnd, 0, RGB(0, 0, 0), LWA_COLORKEY);
    ShowWindow(hwnd, SW_SHOW);

    MARGINS Margin = { -1, -1, -1, -1 };
    DwmExtendFrameIntoClientArea(hwnd, &Margin);
backenbinding

Most helpful comment

Found a fix on unknowncheats (https://www.unknowncheats.me/forum/direct3d/192197-dx11-overlay-imgui.html)

By setting the blend states to,

D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.AlphaToCoverageEnable = false;
desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA; //
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; //
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);

vs

D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.AlphaToCoverageEnable = false;
desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;//
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;//
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);

Is this going to create problems with anything else? It fixed all my problems that I was having.

dx11fixed

All 3 comments

I'm sorry but this is offtopic, it hasn't much to do with imgui, it's a rendering and os/wm problem.

(Also: please use github attachment system for pictures and not imgur links. The knowledge is useful for future reference if someone ask the same thing but imgur links are not future proof)

Found a fix on unknowncheats (https://www.unknowncheats.me/forum/direct3d/192197-dx11-overlay-imgui.html)

By setting the blend states to,

D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.AlphaToCoverageEnable = false;
desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_DEST_ALPHA; //
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ONE; //
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);

vs

D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.AlphaToCoverageEnable = false;
desc.RenderTarget[0].BlendEnable = true;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_INV_SRC_ALPHA;//
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;//
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
g_pd3dDevice->CreateBlendState(&desc, &g_pBlendState);

Is this going to create problems with anything else? It fixed all my problems that I was having.

dx11fixed

Sorry this is still off-topic;. This is a DX11/rendering question.
The old unmerged PR https://github.com/ocornut/imgui/pull/18 was using a similar technique.
But details of blending/rendering are up to your application, how the window and render buffers are setup, there's so many variables going on there and none of them are related to imgui.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

the-lay picture the-lay  路  3Comments

GrammarLord picture GrammarLord  路  3Comments

bogdaNNNN1 picture bogdaNNNN1  路  3Comments

ghost picture ghost  路  3Comments

KaungZawHtet picture KaungZawHtet  路  3Comments