DXGI 1.5 DuplicateOutput1 失败并显示 DXGI_ERROR_UNSUPPORTED (0x887a0004)

DXGI 1.5 DuplicateOutput1 fails with DXGI_ERROR_UNSUPPORTED (0x887a0004)

由于某些原因 DuplicateOutput1 失败,而 DuplicateOutput 没有。

#include <D3D11.h>
#include <DXGI1_5.h>

int main() {
    ID3D11Device *device;
    D3D_FEATURE_LEVEL levels[] = { D3D_FEATURE_LEVEL_11_1 };
    D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, levels, ARRAYSIZE(levels), D3D11_SDK_VERSION, &device, NULL, NULL);

    IDXGIDevice *dxDevice;
    device->QueryInterface<IDXGIDevice>(&dxDevice);

    IDXGIAdapter *adapter;
    dxDevice->GetAdapter(&adapter);

    IDXGIOutput *output;
    adapter->EnumOutputs(0, &output);

    IDXGIOutput5 *output5;
    output->QueryInterface<IDXGIOutput5>(&output5);

    IDXGIOutputDuplication *outputDuplication;
    auto hr1 = output5->DuplicateOutput(device, &outputDuplication);

S_OK这里

    const DXGI_FORMAT formats[] = { DXGI_FORMAT_B8G8R8A8_UNORM };
    auto hr2 = output5->DuplicateOutput1(device, 0, ARRAYSIZE(formats), formats, &outputDuplication);
}

0x887a0004: 此系统不支持指定的设备接口或功能级别。

如果您 运行 在同时具有集成图形芯片和独立 GPU 的系统上,可能会发生这种情况。见 https://support.microsoft.com/en-us/kb/3019314:

unfortunately this issue occurs because the Desktop Duplication API does not support being run against the discrete GPU on a Microsoft Hybrid system. By design, the call fails together with error code DXGI_ERROR_UNSUPPORTED in such a scenario.

To work around this issue, run the application on the integrated GPU instead of on the discrete GPU on a Microsoft Hybrid system.

我会post这里是@weggo 的回答,因为我差点错过了!

For those that might stumble upon this in the future, calling SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) allows the DuplicateOutput1 to succeed. I have no idea why the DuplicateOutput1 checks the process dpi version, though.

我只想补充一点,您必须在清单设置中的解决方案属性中将 DPI 感知设置为 False,才能使 SetProcessDpiAwarenessContext 正常工作:)