When initializing DirectX 11.1 for win32 I was following MSDN sample code. The code declare two Direct3d devices:
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11Device1* g_pd3dDevice1 = nullptr;
and then acquire the device like:
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE( featureLevels );
hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
createDeviceFlags,
featureLevels,
numFeatureLevels,
D3D11_SDK_VERSION,
&g_pd3dDevice,
&g_featureLevel,
&g_pImmediateContext );
if ( hr == E_INVALIDARG )
{
hr = D3D11CreateDevice(
nullptr,
D3D_DRIVER_TYPE_HARDWARE,
nullptr,
createDeviceFlags,
&featureLevels[1],
numFeatureLevels - 1,
D3D11_SDK_VERSION,
&g_pd3dDevice,
&g_featureLevel,
&g_pImmediateContext );
}
if( FAILED( hr ) )
return hr;
Then we acquire DXGIDevice:
IDXGIFactory1* dxgiFactory = nullptr;
IDXGIDevice* dxgiDevice = nullptr;
hr = g_pd3dDevice->QueryInterface( __uuidof(IDXGIDevice),
reinterpret_cast<void**>(&dxgiDevice)
);
Then we get the adapter:
IDXGIAdapter* adapter = nullptr;
hr = dxgiDevice->GetAdapter(&adapter);
From the adapter we get the IDXGIFactory1 interface:
hr = adapter->GetParent( __uuidof(IDXGIFactory1),
reinterpret_cast<void**>(&dxgiFactory) );
From the IDXGIFactory1 interface, we request IDXGIFactory2 interface:
IDXGIFactory2* dxgiFactory2 = nullptr;
hr = dxgiFactory->QueryInterface( __uuidof(IDXGIFactory2),
reinterpret_cast<void**>(&dxgiFactory2)
);
If IDXGIFactory2 is available, we request Direct3D11.1 device interface. Also get the ID3D11DeviceContext1 interface:
if ( dxgiFactory2 )
{
// DirectX 11.1 or later
hr = g_pd3dDevice->QueryInterface( __uuidof(ID3D11Device1),
reinterpret_cast<void**>(&g_pd3dDevice1)
);
if (SUCCEEDED(hr))
{
(void) g_pImmediateContext->QueryInterface(
__uuidof(ID3D11DeviceContext1),
reinterpret_cast<void**> (&g_pImmediateContext1)
);
}
Then we create the swapchain:
hr = dxgiFactory2->CreateSwapChainForHwnd( g_pd3dDevice,
g_hWnd,
&sd,
nullptr,
nullptr,
&g_pSwapChain1 );
My first question is why this code is using DirectX11 version of device while creating the swapchain? Should we use the g_pd3dDevice1 instead of g_pd3dDevice?
My second question is, even though we could acquire directx11.1 version of interfaces, the msdn sample code acquired the IDXGISwapChain interface from IDXGISwapChain1 interface:
hr = g_pSwapChain1->QueryInterface( __uuidof(IDXGISwapChain),
reinterpret_cast<void**>(&g_pSwapChain) );
and use that version of swapchain in the present call:
g_pSwapChain->Present( 0, 0 );
Why is that?