How To: Create a Device and Immediate Context
This topics shows how to initialize a device. Initializing a device is one of the first tasks that your application must complete before you can render your scene.
To create a device and immediate context
Fill out the DXGI_SWAP_CHAIN_DESC structure with information about buffer formats and dimensions. For more information, see Creating a Swap Chain.
The following code example demonstrates how to fill in the DXGI_SWAP_CHAIN_DESC structure.
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof( sd ) );
sd.BufferCount = 1;
sd.BufferDesc.Width = 640;
sd.BufferDesc.Height = 480;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = g_hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
Using the DXGI_SWAP_CHAIN_DESC structure from step one, call D3D11CreateDeviceAndSwapChain to initialize the device and swap chain at the same time.
D3D_FEATURE_LEVEL FeatureLevelsRequested = D3D_FEATURE_LEVEL_11_0;
UINT numLevelsRequested = 1;
D3D_FEATURE_LEVEL FeatureLevelsSupported;
if( FAILED (hr = D3D11CreateDeviceAndSwapChain( NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
0,
&FeatureLevelsRequested,
numFeatureLevelsRequested,
D3D11_SDK_VERSION,
&sd,
&g_pSwapChain,
&g_pd3dDevice,
&FeatureLevelsSupported,
&g_pImmediateContext )))
{
return hr;
}
Note
If you request a D3D_FEATURE_LEVEL_11_1 device on a computer with only the Direct3D 11.0 runtime, D3D11CreateDeviceAndSwapChain immediately exits with E_INVALIDARG. To safely request all possible feature levels on a computer with the DirectX 11.0 or DirectX 11.1 runtime, use this code:
|
Create a render-target view by calling ID3D11Device::CreateRenderTargetView and bind the back-buffer as a render target by calling ID3D11DeviceContext::OMSetRenderTargets.
|
Create a viewport to define which parts of the render target will be visible. Define the viewport using the D3D11_VIEWPORT structure and set the viewport using the ID3D11DeviceContext::RSSetViewports method.
C++ |
---|
|