Export (0) Print
Expand All

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.

Ff476879.wedge(en-us,VS.85).gifTo create a device and immediate context

  1. 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;
    
    
  2. 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:

    
    
    const D3D_FEATURE_LEVEL lvl[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0,
    D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0,
    D3D_FEATURE_LEVEL_9_3, D3D_FEATURE_LEVEL_9_2, D3D_FEATURE_LEVEL_9_1 };
    
    UINT createDeviceFlags = 0;
    #ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
    #endif
    
    ID3D11Device* device = nullptr;
    HRESULT hr = D3D11CreateDeviceAndSwapChain( nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, lvl, _countof(lvl), D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3ddevice, &FeatureLevelsSupported, &g_pImmediateContext );
    if ( hr == E_INVALIDARG )
    {
        hr = D3D11CreateDeviceAndSwapChain( nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, &lvl[1], _countof(lvl) - 1, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3ddevice, &FeatureLevelsSupported, &g_pImmediateContext );
    }
    
    if (FAILED(hr))
        return hr;
    
    
    
  3. Create a render-target view by calling ID3D11Device::CreateRenderTargetView and bind the back-buffer as a render target by calling ID3D11DeviceContext::OMSetRenderTargets.

    
    
    ID3D11Texture2D* pBackBuffer;
    
    // Get a pointer to the back buffer
    hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), 
                                 ( LPVOID* )&pBackBuffer );
    
    // Create a render-target view
    g_pd3dDevice->CreateRenderTargetView( pBackBuffer, NULL,
                                          &g_pRenderTargetView );
    
    // Bind the view
    g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );
    
    
    
  4. 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.
    
        // Setup the viewport
        D3D11_VIEWPORT vp;
        vp.Width = 640;
        vp.Height = 480;
        vp.MinDepth = 0.0f;
        vp.MaxDepth = 1.0f;
        vp.TopLeftX = 0;
        vp.TopLeftY = 0;
        g_pImmediateContext->RSSetViewports( 1, &vp );
    
    
    

Related topics

Devices

 

 

Community Additions

ADD
Show:
© 2014 Microsoft