1

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)?

  • 3
    If you have Windows 10 1809 you can use IDXGIFactory7::RegisterAdaptersChangedEvent https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_6/nf-dxgi1_6-idxgifactory7-registeradapterschangedevent otherwise just poll (recreate the factory each time). This is demonstrated in this official sample https://github.com/microsoft/DirectX-Graphics-Samples/tree/master/Samples/Desktop/D3D12xGPU of if you have a window: https://stackoverflow.com/questions/33762140/what-is-the-notification-when-the-number-of-monitors-changes – Simon Mourier May 05 '21 at 06:22

1 Answers1

3

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.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81