Cómo: Crear un dispositivo y un contexto inmediato

En este tema se muestra cómo inicializar un dispositivo. La inicialización de un dispositivo es una de las primeras tareas que la aplicación debe completar para poder representar la escena.

Para crear un dispositivo y un contexto inmediato

Rellene la estructura de DXGI_SWAP_CHAIN_DESC con información sobre los formatos y dimensiones del búfer. Para obtener más información, vea Crear una cadena de intercambio.

En el ejemplo de código siguiente se muestra cómo rellenar la estructura 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;

Con la estructura DXGI_SWAP_CHAIN_DESC del paso uno, llame a D3D11CreateDeviceAndSwapChain para inicializar el dispositivo y la cadena de intercambio al mismo tiempo.

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

Si solicitas un dispositivo D3D_FEATURE_LEVEL_11_1 en un equipo con solo el tiempo de ejecución de Direct3D 11.0, D3D11CreateDeviceAndSwapChain se cierra inmediatamente con E_INVALIDARG. Para solicitar de forma segura todos los niveles de características posibles en un equipo con el entorno de ejecución directX 11.0 o DirectX 11.1, use este código:

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;

 

Cree una vista de destino de representación llamando a ID3D11Device::CreateRenderTargetView y enlace el búfer de reserva como destino de representación llamando a ID3D11DeviceContext::OMSetRenderTargets.

ID3D11Texture2D* pBackBuffer;

Obtenga un puntero al búfer de reserva hr = g_pSwapChain-GetBuffer>( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&pBackBuffer );

Cree una vista render-target g_pd3dDevice-CreateRenderTargetView>( pBackBuffer, NULL, &g_pRenderTargetView );

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

Cree una ventanilla para definir qué partes del destino de representación serán visibles. Defina la ventanilla mediante la estructura D3D11_VIEWPORT y establezca la ventanilla mediante el método 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 );

Dispositivos

Cómo usar Direct3D 11

> >   > >   >