When initializing DirectX 11.1 for win32, I followed the example MSDN code. The code declares two Direct3d devices:
ID3D11Device* g_pd3dDevice = nullptr; ID3D11Device1* g_pd3dDevice1 = nullptr;
and then purchase a device, for example:
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 get the 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) );
In the IDXGIFactory1 interface, we request the IDXGIFactory2 interface:
IDXGIFactory2* dxgiFactory2 = nullptr; hr = dxgiFactory->QueryInterface( __uuidof(IDXGIFactory2), reinterpret_cast<void**>(&dxgiFactory2) );
If IDXGIFactory2 is available, we request a 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 a swapchain:
hr = dxgiFactory2->CreateSwapChainForHwnd( g_pd3dDevice, g_hWnd, &sd, nullptr, nullptr, &g_pSwapChain1 );
My first question is , why does this code use the DirectX11 device version when creating swapchain? Should we use g_pd3dDevice1 instead of g_pd3dDevice?
My second question , although we could get the DirectX11.1 version of the interfaces, the msdn example code acquired the IDXGISwapChain interface from the IDXGISwapChain1 interface:
hr = g_pSwapChain1->QueryInterface( __uuidof(IDXGISwapChain), reinterpret_cast<void**>(&g_pSwapChain) );
and use this version of swapchain in the current call:
g_pSwapChain->Present( 0, 0 );
Why is this?