Search code examples
c++directxdirectx-11dxgi

Get new connected display via existing IDXGIDevice


I have a program that creates ID3D11Device device on start and destroys on shutdown. I encountered the issue when I connected the second monitor after ID3D11Device was created. In this simplified piece of code it enumerates adapter outputs and this enumeration returns only one monitor, the first one that had been connected before program started.

A

IDXGIDevice* DXGIDevice;
D3D11Device->QueryInterface(IID_IDXGIDevice, (void**)&DXGIDevice);

IDXGIAdapter* DXGIAdapter;
DXGIDevice->GetAdapter(&DXGIAdapter);

IDXGIOutput* Output;
int i = 0;
while (DXGIAdapter->EnumOutputs(i, &Output) != DXGI_ERROR_NOT_FOUND)
{
    DXGI_OUTPUT_DESC desc;
    Output->GetDesc(&desc);

    PrintMonitor(desc.Monitor);
}

But if I try manually create factory and get main adapter, it enumerates both monitors.

B

IDXGIFactory* DXGIFactory;
IDXGIAdapter* DXGIAdapter;

CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&DXGIFactory);
factory->EnumAdapters(0, &DXGIAdapter);

IDXGIOutput* Output;
int i = 0;
while (DXGIAdapter->EnumOutputs(i, &Output) != DXGI_ERROR_NOT_FOUND)
{
    DXGI_OUTPUT_DESC desc;
    Output->GetDesc(&desc);

    PrintMonitor(desc.Monitor);
}

My question is: is this possible "refresh" outputs list of adapter that was get from DXGIDevice device without this device recreation, and see any monitors plug-in/plug-out in code like the first one (A)?


Solution

  • The method IDXGIFactory1::IsCurrent() should help. If it returns FALSE, then you should destroy and re-create the factory to refresh the information. Just check it after each all to Present.

    I use this method to support HDR10 output in my directx-vs-templates project. See DX11 and DX12.