无法在 DirectX12 下全屏显示

Can't go fullscreen under DirectX12

本文关键字:显示 DirectX12      更新时间:2023-10-16

我有一个使用DirectX12的应用程序。我想在我的应用程序中支持全屏模式。但是,每次我调用IDXGISwapChain::SetFullscreenState((时,我都会收到此错误:

DXGI 错误:IDXGISwapChain::GetContainingOutput:交换链的适配器 不控制交换链窗口所在的输出。

IDXGISwapChain::SetFullscreenState(( 返回的错误代码是

0x887a0004

我的电脑有两个 GPU:

英特尔® 核芯显卡 630 和

NVIDIA GeForce GTX 1060

后者是发生错误时用于创建 D3D12 设备的适配器。如果适配器是前者,则不会有错误。

用于创建 IDXGISwapChain3 的代码流是

//variables used in the code example
ID3D12Device *pDevice;
IDXGIFactory4 *pDXGIFactory;
IDXGIAdapter *pAdapter;
ID3D12CommandQueue *pCommandQueue;
IDXGISwapChain1 *pTempSwapchain;
IDXGISwapChain3 *pSwapchain;
//the code flow
CreateDXGIFactory2();
pDXGIFactory->EnumAdapter();
D3D12CreateDevice(pAdapter, ...);
pD3DDevice->CreateCommandQueue();
pDXGIFactory->CreateSwapChainForHwnd(pCommandQueue, ..., &pTempSwapchain);
pTempSwapchain->QueryInterface(IID_PPV_ARGS(&pSwapChain));

IDXGISwapChain::SetFullscreenState(( 应该成功,但它失败了。

请注意,该问题是 DXGI 调试层的怪癖和"混合图形"解决方案的实现方式的特定组合,具体取决于您当时使用的设备。

这里最好的选择是只抑制错误:

#if defined(_DEBUG)
    // Enable the debug layer (requires the Graphics Tools "optional feature").
    //
    // NOTE: Enabling the debug layer after device creation will invalidate the active device.
    {
        ComPtr<ID3D12Debug> debugController;
        if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(debugController.GetAddressOf()))))
        {
            debugController->EnableDebugLayer();
        }
        else
        {
            OutputDebugStringA("WARNING: Direct3D Debug Device is not availablen");
        }
        ComPtr<IDXGIInfoQueue> dxgiInfoQueue;
        if (SUCCEEDED(DXGIGetDebugInterface1(0, IID_PPV_ARGS(dxgiInfoQueue.GetAddressOf()))))
        {
            dxgiInfoQueue->SetBreakOnSeverity(DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_ERROR, true);
            dxgiInfoQueue->SetBreakOnSeverity(DXGI_DEBUG_ALL, DXGI_INFO_QUEUE_MESSAGE_SEVERITY_CORRUPTION, true);
            DXGI_INFO_QUEUE_MESSAGE_ID hide[] =
            {
                80 /* IDXGISwapChain::GetContainingOutput: The swapchain's adapter does not control the output on which the swapchain's window resides. */,
            };
            DXGI_INFO_QUEUE_FILTER filter = {};
            filter.DenyList.NumIDs = _countof(hide);
            filter.DenyList.pIDList = hide;
            dxgiInfoQueue->AddStorageFilterEntries(DXGI_DEBUG_DXGI, &filter);
        }
    }
#endif

我在 GitHub 上的所有模板中都使用它。

我找到了解决方案。使用 IDXGIFactory6::EnumAdapterByGpuPreference(( 方法而不是 IDXGIFactory::EnumAdapter(( 方法,则错误将消失。