Setting AlphaMode member of DXGI_SWAP_CHAIN_DESC1 makes CreateSwapChainForHwnd fail with 0x887a0001

1

I want to create a swap chain that I can alter my alpha values so it can get semi-transparent. My code is here:

#include <Windows.h>
#include <d3d11.h>
#include <d2d1_3.h>
#include <dxgi1_6.h>
#include <dwmapi.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dwmapi.lib")
#pragma comment(lib, "d2d1.lib")

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch (msg)
{
case WM_DESTROY:
    PostQuitMessage(0);
    break;
default:
    return DefWindowProcW(hwnd, msg, wparam, lparam);
}

return 0;
}

int WINAPI wWinMain(HINSTANCE hinstance, HINSTANCE, LPWSTR, int)
{
WNDCLASSEX wc{
    sizeof WNDCLASSEX, CS_HREDRAW | CS_VREDRAW, WndProc, 0, 0, hinstance, nullptr, LoadCursorW(nullptr, IDC_ARROW), nullptr, nullptr,
    L"DxClass", nullptr
};

const auto hwnd{
    CreateWindowExW(WS_EX_COMPOSITED, MAKEINTATOM(RegisterClassExW(&wc)), L"DirectX",
                    WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 800, 650, HWND_DESKTOP, nullptr,
                    hinstance, nullptr)
};
    const MARGINS marg{-1, -1, -1, -1};
    DwmExtendFrameIntoClientArea(hwnd, &marg);
D3D_FEATURE_LEVEL levels[]{ D3D_FEATURE_LEVEL_12_1, D3D_FEATURE_LEVEL_12_0 };
ID3D11Device* device;
IDXGIDevice* dxdevice;
IDXGIAdapter* adapter;
IDXGIFactory7* dxFactory;
ID2D1Device* d2dDevice;
ID2D1DeviceContext* d2dContext;
IDXGISwapChain1* swapChain;
D3D11CreateDevice(0, D3D_DRIVER_TYPE_HARDWARE, 0, D3D11_CREATE_DEVICE_BGRA_SUPPORT, levels, 2u, D3D11_SDK_VERSION, &device, 0, 0);

device->QueryInterface(IID_PPV_ARGS(&dxdevice));

dxdevice->GetAdapter(&adapter);
adapter->GetParent(IID_PPV_ARGS(&dxFactory));

D2D1CreateDevice(dxdevice, { D2D1_THREADING_MODE_SINGLE_THREADED, D2D1_DEBUG_LEVEL_INFORMATION, D2D1_DEVICE_CONTEXT_OPTIONS_NONE }, &d2dDevice);

d2dDevice->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &d2dContext);

DXGI_SWAP_CHAIN_DESC1 sc{ 0 };

sc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
sc.SampleDesc.Count = 1;
sc.Scaling = DXGI_SCALING_NONE;
sc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sc.BufferCount = 2;
sc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
sc.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED;
sc.Flags = DXGI_SWAP_CHAIN_FLAG_FOREGROUND_LAYER;

HRESULT hr = dxFactory->CreateSwapChainForHwnd(dxdevice, hwnd, &sc, nullptr, 0, &swapChain);


if (FAILED(hr))
{
    __debugbreak();
    return -1;
}

MSG m;
while (GetMessageW(&m, nullptr, 0, 0))
{
    TranslateMessage(&m);
    DispatchMessageW(&m);
}

swapChain->Release();
d2dContext->Release();
device->Release();
d2dDevice->Release();
dxFactory->Release();
dxdevice->Release();
adapter->Release();

return 0;
}

I want the drawing surface to be semi-transparent, so the back will show through. I've read that WS_EX_COMPOSITED with DwmExtendFrameIntoClientArea will create a drawing surface that Direct3D will be able to draw directly into, however, I haven't gotten that far becuase of this problem. My goal is to do this without using layered windows, as I have seen other people do it: Vista D3D

It works when I comment out the AlphaMode member of the swap chain struct, but I need to use it. How can I get it to work? Thanks.

c++
winapi
directx
direct3d
dxgi

1 Answer

2

If you enable debug layer, the message coming with the error explains the error:

DXGI ERROR: IDXGIFactory::CreateSwapChain: Alpha blended swapchains must be created with CreateSwapChainForComposition, or CreateSwapChainForCoreWindow with the DXGI_SWAP_CHAIN_FLAG_FOREGROUND_LAYER flag.

answered on Stack Overflow Dec 2, 2019 by Roman R.

User contributions licensed under CC BY-SA 3.0