HRESULT InitializeD3DEx(HWND hWnd, D3DPRESENT_PARAMETERS d3dpp, IDirect3D9*& d3d, IDirect3DDevice9*& d3dDevice, D3DEXCreateFn createFn )
	{
		d3d = NULL;
		d3dDevice = NULL;
		IDirect3D9Ex* d3dEx = NULL;
		// initialize Direct3D using the Ex function
		HRESULT result = createFn( D3D_SDK_VERSION, &d3dEx );
		if ( FAILED( result ) ) return result;

		result = d3dEx->QueryInterface(__uuidof(IDirect3D9), reinterpret_cast<void **>(&d3d) );	
		if ( FAILED( result ) ) { d3dEx->Release(); return result; }
	
		// determine what type of vertex processing to use based on the device capabilities
		DWORD dwVertexProcessing = GetVertexProcessingCaps(d3d);

		// create the D3D device using the Ex function
		IDirect3DDevice9Ex* deviceEx = NULL;
		result = d3dEx->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
				dwVertexProcessing | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
				&d3dpp, NULL, &deviceEx);

		if ( FAILED(result) ) { d3dEx->Release(); d3d->Release();  return result; }
		
		// obtain the standard D3D device interface
		deviceEx->QueryInterface(__uuidof(IDirect3DDevice9), reinterpret_cast<void **>(&d3dDevice) );

		deviceEx->Release();
		d3dEx->Release();

		return 0;
	}
	HRESULT InitializeD3D(HWND hWnd, D3DPRESENT_PARAMETERS d3dpp, IDirect3D9*& d3d, IDirect3DDevice9*& d3dDevice )
	{
		d3d = NULL;
		d3dDevice = NULL;
		// initialize Direct3D
		d3d = Direct3DCreate9(D3D_SDK_VERSION);
		if ( d3d != NULL )
		{
			// determine what type of vertex processing to use based on the device capabilities
			DWORD dwVertexProcessing = GetVertexProcessingCaps(d3d);
			// create the D3D device
			return d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
									dwVertexProcessing | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
									&d3dpp, &d3dDevice);
		}
		return -1;
	}
	HRESULT DirectX9Renderer::InitDevice(D3DPRESENT_PARAMETERS d3dpp)
	{
		// Create the D3D object, which is needed to create the D3DDevice.
		if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
			return E_FAIL;

		////D3DCREATE_PUREDEVICE
		////D3DCREATE_MULTITHREADED
		//if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWindow,
		//	D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
		//	&d3dpp, &m_pd3dDevice ) ) )
		//{
		//	if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWindow,
		//		D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
		//		&d3dpp, &m_pd3dDevice ) ) )
		//	{
		//		m_pd3dDevice=NULL;
		//		return E_FAIL;
		//	}
		//}

		// determine what type of vertex processing to use based on the device capabilities
		DWORD dwVertexProcessing = GetVertexProcessingCaps(m_pD3D);
		// create the D3D device
		if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWindow,
			//dwVertexProcessing | D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE,
			dwVertexProcessing | D3DCREATE_MULTITHREADED,
			//dwVertexProcessing, //Very slow
			&d3dpp, &m_pd3dDevice))){
				return E_FAIL;
		}

		m_pd3dDevice->GetRenderTarget(0,&m_pBackBuffer);
		m_pd3dDevice->GetDepthStencilSurface(&m_pZStencilSurface);

		return S_OK;
	}