Procedura: Creare un dispositivo e un contesto immediato

In questo argomento viene illustrato come inizializzare un dispositivo. L'inizializzazione di un dispositivo è una delle prime attività che l'applicazione deve completare prima di poter eseguire il rendering della scena.

Per creare un dispositivo e un contesto immediato

Compilare la struttura DXGI_SWAP_CHAIN_DESC con informazioni sui formati e le dimensioni del buffer. Per altre informazioni, vedere Creazione di una catena di scambio.

Nell'esempio di codice seguente viene illustrato come compilare la struttura DXGI_SWAP_CHAIN_DESC.

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;

Usando la struttura DXGI_SWAP_CHAIN_DESC dal passaggio 1, chiamare D3D11CreateDeviceAndSwapChain per inizializzare il dispositivo e la catena di scambio contemporaneamente.

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;
}

Nota

Se si richiede un dispositivo D3D_FEATURE_LEVEL_11_1 in un computer con solo il runtime Direct3D 11.0, D3D11CreateDeviceAndSwapChain viene immediatamente chiuso con E_INVALIDARG. Per richiedere in modo sicuro tutti i livelli di funzionalità possibili in un computer con il runtime DirectX 11.0 o DirectX 11.1, usare questo codice:

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;

 

Creare una visualizzazione di destinazione di rendering chiamando ID3D11Device::CreateRenderTargetView e associando il buffer nascosto come destinazione di rendering chiamando ID3D11DeviceContext::OMSetRenderTargets.

ID3D11Texture2D* pBackBuffer; 

Ottenere un puntatore al buffer nascosto hr = g_pSwapChain-GetBuffer>( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );

Creare una visualizzazione di destinazione di rendering g_pd3dDevice-CreateRenderTargetView>( pBackBuffer, NULL, &g_pRenderTargetView );

// Bind the view g_pImmediateContext->OMSetRenderTargets( 1, &g_pRenderTargetView, NULL );

Creare un riquadro di visualizzazione per definire quali parti della destinazione di rendering saranno visibili. Definire il riquadro di visualizzazione usando la struttura D3D11_VIEWPORT e impostare il riquadro di visualizzazione usando il metodo ID3D11DeviceContext::RSSetViewports.

C++
    // 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 );

Dispositivi

Come usare Direct3D 11

> >   > >   >