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;
	}
예제 #2
0
HRESULT VMR9CustomPresenter::CreateD3DDevice()
{
    AutoLock lock(m_ObjectLock);

    if (!m_pD3D9)
    {
        return E_FAIL;
    }

    HRESULT hr = S_OK;
    IDirect3DDevice9* pDevice = NULL;

    do
    {
        HWND hwnd = GetDesktopWindow();

        D3DPRESENT_PARAMETERS pp = {0};

        pp.BackBufferWidth = 1;
        pp.BackBufferHeight = 1;
        pp.Windowed = TRUE;
        pp.SwapEffect = D3DSWAPEFFECT_COPY;
        pp.BackBufferFormat = D3DFMT_UNKNOWN;
        pp.hDeviceWindow = hwnd;
        pp.Flags = D3DPRESENTFLAG_VIDEO;
        pp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

        D3DCAPS9 ddCaps;
        BREAK_FAIL(m_pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &ddCaps));

        DWORD vp = D3DCREATE_MULTITHREADED | D3DCREATE_FPU_PRESERVE;

        if (ddCaps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
        {
            vp |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
        }
        else
        {
            vp |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
        }

        if (m_D3D9Ex)
        {
            IDirect3D9Ex* pD3D9Ex = static_cast<IDirect3D9Ex*>(m_pD3D9);
            IDirect3DDevice9Ex* pDeviceEx = NULL;

            BREAK_FAIL(pD3D9Ex->CreateDeviceEx(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, pp.hDeviceWindow,
                                               vp, &pp, NULL, &pDeviceEx));

            SAFE_RELEASE(m_pDevice);
            pDeviceEx->QueryInterface(__uuidof(IDirect3DDevice9), (void**)&m_pDevice);
            SAFE_RELEASE(pDeviceEx);
        }
        else
        {
            IDirect3DDevice9* pDevice = NULL;
            m_pD3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, pp.hDeviceWindow, vp, &pp,
                                  &pDevice);

            SAFE_RELEASE(m_pDevice);
            m_pDevice = pDevice;
        }

    } while (0);

    if (FAILED(hr))
    {
        SAFE_RELEASE(pDevice);
    }

    return hr;
}