Creating a Device

To create a Direct3D device, first create a Direct3D object (see Direct3DCreate9). All rendering devices created by a Direct3D object share the same physical resources. If you create multiple rendering devices from a single Direct3D object, extreme performance penalties will be incurred because they share the same hardware.

First, initialize values for the D3DPRESENT_PARAMETERS structure that is used to create the Direct3D device. The following code example specifies a windowed application where the back buffer is flipped to the front buffer during a vertical sync operation only.

LPDIRECT3DDEVICE9 pDevice = NULL;

D3DPRESENT_PARAMETERS d3dpp; 

ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed   = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY;

Next, create the Direct3D device. The following IDirect3D9::CreateDevice call specifies the default adapter, a hardware abstraction layer (HAL) device, and software vertex processing.

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
									D3DCREATE_SOFTWARE_VERTEXPROCESSING,
									&d3dpp, &d3dDevice ) ) )
	return E_FAIL;

Note that a call to create, release, or reset the device should happen only on the same thread as the window procedure of the focus window.

After creating the device, set its state.