每像素透明 window 在 C++ 中使用 Windows 合成引擎

per-pixel transparent window using Windows Composition engine in C++

我想在 window 上显示原始 BGRA 数据(因此我有一个 unsigned int *,具有宽度和高度),以便将 alpha 分量考虑到具有每个像素的透明度。最终目的是将其集成到图形工具包中。

阅读了 Kenny Kerr 的文章 (https://docs.microsoft.com/en-us/archive/msdn-magazine/2014/june/windows-with-c-high-performance-window-layering-using-the-windows-composition-engine) 我认为它可以解决我的问题。

我已经编写了一个完整的代码来执行文章中描述的内容 ():透明 window 带有半透明磁盘

所以,基本上,我现在正在做的是:

但是无论我为传递给 CreateBitmapFromDxgiSurface 的属性的位图选项使用什么标志,应用程序都会崩溃并且 D2D 调试层会提到一个错误,例如:“警告:D2D 调试错误 - 位图选项 [0x1] 必须是与 DXGI 表面关联的标志。"

完整代码在这里:

#include <iostream>

#ifdef _WIN32_WINNT
# undef _WIN32_WINNT
#endif
#define _WIN32_WINNT 0x0A00

#include <guiddef.h>
#include <d2d1_1.h>
#include <d3d11.h>
#include <dxgi1_3.h>
#include <dcomp.h>

static HINSTANCE instance = NULL;
static HWND win = NULL;

LRESULT CALLBACK _window_procedure(HWND   window,
                                   UINT   message,
                                   WPARAM window_param,
                                   LPARAM data_param);

typedef struct
{
    ID3D11Device *d3d_device;
    IDXGIDevice *dxgi_device;
    IDXGIFactory2 *dxgi_factory;
    ID2D1Factory1 *d2d_factory;
    ID2D1Device *d2d_device;
    ID2D1DeviceContext *d2d_device_ctx;
    IDCompositionDevice *dcomp_device;
    IDCompositionVisual *dcomp_visual;
    IDCompositionTarget *dcomp_target;
    IDXGISwapChain1 *dxgi_swapchain;
} D2d;

int d2d_init(HWND window, D2d *d2d)
{
    const D3D_FEATURE_LEVEL levels[] =
    {
        D3D_FEATURE_LEVEL_12_1,
        D3D_FEATURE_LEVEL_12_0,
        D3D_FEATURE_LEVEL_11_1,
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0
    };
    D2D1_FACTORY_OPTIONS opt;
    DXGI_SWAP_CHAIN_DESC1 desc;
    RECT r;
    HRESULT res;

    /* direct3d device */
    res = D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL,
                            D3D11_CREATE_DEVICE_BGRA_SUPPORT |
                            D3D11_CREATE_DEVICE_DEBUG,
                            levels, sizeof(levels) / sizeof(D3D_FEATURE_LEVEL),
                            D3D11_SDK_VERSION, &d2d->d3d_device, NULL, NULL);
    if (FAILED(res))
        return 0;

    /* dxgi device */
    res = d2d->d3d_device->QueryInterface(&d2d->dxgi_device);
    if (FAILED(res))
        goto release_d3d_device;

    /* dxgi factory */
    res = CreateDXGIFactory2(DXGI_CREATE_FACTORY_DEBUG,
                             __uuidof(d2d->dxgi_factory),
                             (void **)&d2d->dxgi_factory);
    if (FAILED(res))
        goto release_dxgi_device;

    /* d2d factory */
    opt.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
    res = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,
                            __uuidof(d2d->d2d_factory), &opt,
                            (void **)&d2d->d2d_factory);
    if (FAILED(res))
        goto release_dxgi_factory;

    /* d2d device */

    res = d2d->d2d_factory->CreateDevice(d2d->dxgi_device, &d2d->d2d_device);
    if (FAILED(res))
        goto release_d2d_factory;

    /* d2d device context */

    // FIXME : D2D1_DEVICE_CONTEXT_OPTIONS_ENABLE_MULTITHREADED_OPTIMIZATIONS
    res = d2d->d2d_device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
                                               &d2d->d2d_device_ctx);
    if (FAILED(res))
        goto release_d2d_device;

    /* dcomp device */
    res = DCompositionCreateDevice(d2d->dxgi_device,
                                   __uuidof(d2d->dcomp_device),
                                   (void **)&d2d->dcomp_device);
    if (FAILED(res))
        goto release_d2d_device_ctx;

    /* dcomp visual */
    res = d2d->dcomp_device->CreateVisual(&d2d->dcomp_visual);
    if (FAILED(res))
        goto release_dcomp_device;

    /* dcomp target */
    res = d2d->dcomp_device->CreateTargetForHwnd(window, TRUE,
                                                 &d2d->dcomp_target);
    if (FAILED(res))
        goto release_dcomp_visual;

    /* dxgi swapchain */
    if (!GetClientRect(window, &r))
        goto release_dcomp_target;

    desc.Width = r.right - r.left; /* width of client area */
    desc.Height = r.bottom - r.top; /* height of client area */
    desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
    desc.Stereo = FALSE;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    desc.BufferCount = 2;
    desc.Scaling = DXGI_SCALING_STRETCH;
    desc.SwapEffect= DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
    desc.AlphaMode = DXGI_ALPHA_MODE_PREMULTIPLIED;
    desc.Flags = 0;

    res = d2d->dxgi_factory->CreateSwapChainForComposition(d2d->dxgi_device,
                                                           &desc,
                                                           NULL,
                                                           &d2d->dxgi_swapchain);
    if (FAILED(res))
        goto release_dcomp_target;

    return 1;

  release_dcomp_target:
    d2d->dcomp_target->Release();
  release_dcomp_visual:
    d2d->dcomp_visual->Release();
  release_dcomp_device:
    d2d->dcomp_device->Release();
  release_d2d_device_ctx:
    d2d->d2d_device_ctx->Release();
  release_d2d_device:
    d2d->d2d_device->Release();
  release_d2d_factory:
    d2d->d2d_factory->Release();
  release_dxgi_factory:
    d2d->dxgi_factory->Release();
  release_dxgi_device:
    d2d->dxgi_device->Release();
  release_d3d_device:
    d2d->d3d_device->Release();

    return 0;
}

void d2d_shutdown(D2d *d2d)
{
    d2d->dxgi_swapchain->Release();
    d2d->dcomp_target->Release();
    d2d->dcomp_visual->Release();
    d2d->dcomp_device->Release();
    d2d->d2d_device_ctx->Release();
    d2d->d2d_device->Release();
    d2d->d2d_factory->Release();
    d2d->dxgi_factory->Release();
    d2d->dxgi_device->Release();
    d2d->d3d_device->Release();
}

void render(D2d *d2d)
{
    IDXGISurface *surface;
    ID2D1Bitmap1 *bitmap;
    D2D1_BITMAP_PROPERTIES1 properties;
    D2D1_COLOR_F c;
    HRESULT res;

    std::cout << "render" << std::endl;

    /* swapchain buffer in a IDXGISurface */
    res = d2d->dxgi_swapchain->GetBuffer(0, __uuidof(surface),
                                         (void **)&surface);
    if (FAILED(res))
        return;
    std::cout << "render 1" << std::endl;

    properties.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
    properties.pixelFormat.format = DXGI_FORMAT_B8G8R8A8_UNORM;
    properties.dpiX = 96;
    properties.dpiY = 96;
    // which value to set here ??
    properties.bitmapOptions = D2D1_BITMAP_OPTIONS_TARGET;
    properties.colorContext = NULL;

    res = d2d->d2d_device_ctx->CreateBitmapFromDxgiSurface(surface,
                                                           &properties,
                                                           &bitmap);
    if (FAILED(res))
        goto release_surface;
    std::cout << "render 2" << std::endl;

    d2d->d2d_device_ctx->SetTarget(bitmap);
    std::cout << "render 3" << std::endl;

    unsigned int *data, *iter;
    unsigned int i, j;
    D2D1_SIZE_U s;
    D2D1_RECT_U r;

    // BGRA test data
    s = bitmap->GetPixelSize();
    data = (unsigned int *)malloc(s.width * s.height * sizeof(unsigned int));
    if (!data)
    {
        std::cout << "malloc fails" << std::endl;
        goto bitmap_release;
    }
    iter = data;
    for (i = 0; i < s.height; i++)
    {
        for (j = 0; j < s.width; j++, iter++)
        {
            if (i >= 50 && j>= 50)
                *iter = 0xff0000ff;
            else
                *iter = 0xffff0000;
        }
    }

    r.left = 0;
    r.top = 0;
    r.right = s.width;
    r.bottom = s.height;
    std::cout << "render 4" << std::endl;
    res = bitmap->CopyFromMemory(&r, data, 4 * s.width);
    if (FAILED(res))
    {
        std::cout << "copy failed" << std::endl;
        goto data_free;
    }
    std::cout << "render 5" << std::endl;

    d2d->d2d_device_ctx->BeginDraw();

    // c.r = 0.18f;
    // c.g = 0.55f;
    // c.b = 0.34f;
    // c.a = 0.75f;

    c.r = 0.0f;
    c.g = 0.0f;
    c.b = 0.0f;
    c.a = 0.0f;
    d2d->d2d_device_ctx->Clear(&c);
    std::cout << "render 6" << std::endl;

    D2D1_RECT_F rect;
    rect.left = 0.0f;
    rect.top = 0.0f;
    rect.right = (float)s.width;
    rect.bottom = (float)s.height;
    d2d->d2d_device_ctx->DrawBitmap(bitmap, rect, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, rect);
    std::cout << "render 7" << std::endl;

    d2d->d2d_device_ctx->EndDraw(NULL, NULL);

    d2d->dxgi_swapchain->Present(1, 0);
    d2d->dcomp_visual->SetContent(d2d->dxgi_swapchain);
    d2d->dcomp_target->SetRoot(d2d->dcomp_visual);
    d2d->dcomp_device->Commit();

  data_free:
    free(data);
  bitmap_release:
    bitmap->Release();
  release_surface:
    surface->Release();
}


LRESULT CALLBACK
_window_procedure(HWND   window,
                  UINT   message,
                  WPARAM window_param,
                  LPARAM data_param)
{
  switch (message)
    {
    case WM_CLOSE:
      PostQuitMessage(0);
      return 0;
    case WM_KEYUP:
      if (window_param == 'Q')
        {
          PostQuitMessage(0);
        }
      if (window_param == 'T')
        {
            std::cout << "transp" << std::endl;
        }
      return 0;
      /* GDI notifications */
    case WM_CREATE:
        return 0;
    case WM_PAINT:
      {
        RECT rect;
        D2d *d2d;

        d2d = (D2d *)GetWindowLongPtr(window, GWLP_USERDATA);

        std::cout << "paint" << std::endl;

        if (GetUpdateRect(window, &rect, FALSE))
          {
            PAINTSTRUCT ps;
            BeginPaint(window, &ps);

            render(d2d);

            EndPaint(window, &ps);
          }
        return 0;
      }
    default:
      return DefWindowProc(window, message, window_param, data_param);
    }
}

int main()
{
    /* class */
    WNDCLASS wc;

    instance = GetModuleHandle(NULL);
    if (!instance)
        return 1;

    memset (&wc, 0, sizeof (WNDCLASS));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = _window_procedure;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = instance;
    wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor (NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(1 + COLOR_BTNFACE);
    wc.lpszMenuName =  NULL;
    wc.lpszClassName = "D2D";

    if(!RegisterClass(&wc))
        goto free_library;

    /* Window */
    int w;
    int h;
    RECT r;
    DWORD style;
    DWORD exstyle;

    w = 640;
    h = 480;

    style = WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
    exstyle = WS_EX_NOREDIRECTIONBITMAP;

    r.left = 0;
    r.top = 0;
    r.right = w;
    r.bottom = h;
    if (!AdjustWindowRectEx(&r, style, FALSE, exstyle))
        goto unregister_class;

    win = CreateWindowEx(exstyle,
                         "D2D", "Test",
                         style,
                         100, 100,
                         r.right - r.left,
                         r.bottom - r.top,
                         NULL,
                         NULL, instance, NULL);
    if (!win)
        goto unregister_class;

    /* d2d init */
    D2d d2d;

    if (!d2d_init(win, &d2d))
    {
        std::cout << "d2d init failed" << std::endl;
        goto destroy_win;
    }

    SetWindowLongPtr(win, GWLP_USERDATA, (LONG_PTR)&d2d);
    ShowWindow(win, SW_SHOWNORMAL);
    UpdateWindow(win);

    /* msg loop */
    while(1)
    {
        MSG msg;
        BOOL ret;

        ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
        if (ret)
        {
            do
            {
                if (msg.message == WM_QUIT)
                  goto beach;
                TranslateMessage(&msg);
                DispatchMessageW(&msg);
            } while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
        }
    }

 beach:
    d2d_shutdown(&d2d);
    DestroyWindow(win);
    UnregisterClass("D2D", instance);
    FreeLibrary(instance);

    std::cout << "exiting..." << std::endl;

    return 0;

  destroy_win:
    DestroyWindow(win);
  unregister_class:
    UnregisterClass("D2D", instance);
  free_library:
    FreeLibrary(instance);

    std::cout << "exiting 2..." << std::endl;

    return 1;
}

在互联网上搜索 1 周后,尤其是在 msdn 上,我找不到解决方案。我已经尝试了几种标志组合,但这个程序总是崩溃。

有人知道哪里出了问题吗?

谢谢

位图(D2D1 或 GPU 上下文中的其他位图)“只是”GPU 资源,可以用于非常不同的目的。

您的错误是因为您将设备上下文的目标位图与您要写入设备上下文的位图混淆了。

  • 第一个位图只是在这里表示设备上下文的目标实际上是 DXGI 表面。
  • 第二个位图用于在设备上下文上写入一些像素(这反过来将落在 DXGI 表面上)。

因此您需要更改代码。首先,只需将设备上下文(=渲染目标)连接到 DXGI 表面,如下所示:

void setBackBuffer(D2d* d2d)
{
    IDXGISurface* surface;
    ID2D1Bitmap1* bitmap;
    HRESULT res = d2d->dxgi_swapchain->GetBuffer(0, __uuidof(surface), (void**)&surface);
    if (FAILED(res))
        return;

    res = d2d->d2d_device_ctx->CreateBitmapFromDxgiSurface(surface, NULL, &bitmap);
    if (FAILED(res))
        goto release_surface;

    d2d->d2d_device_ctx->SetTarget(bitmap);
    bitmap->Release();
release_surface:
    surface->Release();
}

并在您调用 CreateSwapChainForComposition 之后添加它。

之后,修改您的渲染方法,例如:

void render(D2d* d2d)
{
    ID2D1Bitmap1* bitmap;
    D2D1_BITMAP_PROPERTIES1 properties;
    DXGI_SWAP_CHAIN_DESC1 desc;
    D2D1_COLOR_F c;
    HRESULT res;

    std::cout << "render" << std::endl;

    // get size from swapchain (for example)
    d2d->dxgi_swapchain->GetDesc1(&desc);
    D2D1_SIZE_U s = { desc.Width, desc.Height };

    unsigned int* data, * iter;
    unsigned int i, j;

    // BGRA test data
    data = (unsigned int*)malloc(s.width * s.height * sizeof(unsigned int));
    if (!data)
    {
        std::cout << "malloc fails" << std::endl;
        return;
    }
    iter = data;
    for (i = 0; i < s.height; i++)
    {
        for (j = 0; j < s.width; j++, iter++)
        {
            if (i >= 50 && j >= 50)
                *iter = 0xff0000ff;
            else
                *iter = 0xffff0000;
        }
    }

    std::cout << "render 4" << std::endl;

    // create a bitmap from properties & pixel buffer
    // hint: in general for most structures, it's much easier to use ZeroMemory or memset(0) so by default values are automatically set
    ZeroMemory(&properties, sizeof(properties));
    properties.pixelFormat.format = desc.Format;
    properties.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
    res = d2d->d2d_device_ctx->CreateBitmap(s, data, 4 * s.width, properties, &bitmap);
    if (FAILED(res))
    {
        std::cout << "create failed" << std::endl;
        goto data_free;
    }
    std::cout << "render 5" << std::endl;

    d2d->d2d_device_ctx->BeginDraw();

    c.r = 0.0f;
    c.g = 0.0f;
    c.b = 0.0f;
    c.a = 0.0f;
    d2d->d2d_device_ctx->Clear(&c);
    std::cout << "render 6" << std::endl;

    D2D1_RECT_F rect;
    rect.left = 0.0f;
    rect.top = 0.0f;
    rect.right = (float)s.width;
    rect.bottom = (float)s.height;
    d2d->d2d_device_ctx->DrawBitmap(bitmap, rect, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, rect);
    std::cout << "render 7" << std::endl;

    d2d->d2d_device_ctx->EndDraw(NULL, NULL);

    d2d->dxgi_swapchain->Present(1, 0);
    d2d->dcomp_visual->SetContent(d2d->dxgi_swapchain);
    d2d->dcomp_target->SetRoot(d2d->dcomp_visual);
    d2d->dcomp_device->Commit();

    bitmap->Release();
data_free:
    free(data);
}