D3D11CreateDevice() returns garbage value and fails

2

I just started studying direct 3d with the book 3D Game Programming with DirectX11.

I followed the first tutorial and got a MessageBox saying "D3D11CreateDevice Failed".

I checked the return value of this function and got the garbage value of -2005270483. The hexadecimal value of 0x887a002d, which people say it's DXGI_ERROR_SDK_COMPONENT_MISSING error.

However, I couldn't find any information about it. Also, it was none of the possible value of D3D11CreateDevice() that MSDN notifies.

Following is the code of the book creating the D3D device:

bool D3DApp::InitDirect3D()
{
    // Create the device and device context.

    UINT createDeviceFlags = 0;
#if defined(DEBUG) || defined(_DEBUG)  
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    D3D_FEATURE_LEVEL featureLevel;
    HRESULT hr = D3D11CreateDevice(
        0,                 // default adapter
        md3dDriverType,
        0,                 // no software device
        createDeviceFlags, // createDeviceFlags, 
        0, 0,              // default feature level array
        D3D11_SDK_VERSION,
        &md3dDevice,
        &featureLevel,
        &md3dImmediateContext);

    if( FAILED(hr) )
    {
        // I added this part to check out the return value
        TCHAR str[100];
        int code = -1;

        switch(hr) {
        case D3D11_ERROR_FILE_NOT_FOUND: code=0;
            break;
        case D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS: code=1;
            break;
        case D3D11_ERROR_TOO_MANY_UNIQUE_VIEW_OBJECTS: code=2;
            break;
        case D3D11_ERROR_DEFERRED_CONTEXT_MAP_WITHOUT_INITIAL_DISCARD: code=3;
            break;
        case DXGI_ERROR_INVALID_CALL: code=4;
            break;
        case DXGI_ERROR_WAS_STILL_DRAWING: code=5;
            break;
        case E_FAIL: code=6;
            break;
        case E_INVALIDARG: code=7;
            break;
        case E_OUTOFMEMORY: code=8;
            break;
        case E_NOTIMPL: code=9;
            break;
        case S_FALSE: code=10;
            break;
        case S_OK: code=11;
            break;
        } // switch
        _stprintf(str, L"D3D11CreateDevice Failed. Code:%ld %d", hr, code);

        MessageBox(0, str, 0, 0);
        return false;
    } // if
...

I'm currently working on Windows 8.1 with DirectX version 11. My graphics card is NVIDIA GeForce GTX 650. Compiler I use is VS2010 Express.

Does anyone knows what's the problem and solution?

c++
windows-8.1
directx-11
asked on Stack Overflow Aug 21, 2014 by Peter • edited Aug 21, 2014 by glampert

1 Answer

8

The problem is that you do not have the correct DirectX Debug Runtime installed for your operating system. The legacy DirectX SDK (June 2010) has the correct DirectX Debug Runtime for Windows Vista SP2 and Windows 7 aka DirectX 11.0, but not the correct one for Windows 8.0, Windows 8.1, or Windows 7 SP1 with KB 2670838 aka DirectX 11.1 or DirectX 11.2.

For Windows 8.1, the only way to get the proper DirectX Debug Runtime installed is to install the Windows 8.1 SDK. This is done automatically if you had installed Visual Studio 2013, or you can install the SDK directly on your system. You can also get the Windows 8.1 SDK debug runtime by installing the Visual Studio 2013 Remote Debugging Tools packages. See Direct3D SDK Debug Layer Tricks for a complete table of matching up debug runtimes with SDKs.

Note that it would be sufficient to install the Windows 8.1 SDK standalone and continue to use the legacy DirectX SDK (June 2010) with VS 2010 to get D3D11_CREATE_DEVICE_DEBUG to work again on Windows 8.1, but I would recommend either (a) upgrading to VS 2012 or VS 2013 -or- (b) using the 'props' solution to integrate the Windows 8.1 SDK with VS 2010 for the latest versions of the headers. The DirectX Tool Kit does this for VS 2010 support.

If you need to continue to use legacy DirectX SDK only components like D3DX11 as well for your VS 2010 project, then you can combine the Windows 8.1 SDK with the DirectX SDK but be sure to list the DirectX SDK include and lib paths after the Windows 8.1 SDK include and lib paths (see MSDN).

To sum up: the ideal solution is to just use Visual Studio 2013 on Windows 8.1 without the legacy DirectX SDK at all. The Win32 desktop Direct3D tutorial or the Windows Store app Direct3D tutorial builds just fine without the legacy DirectX SDK at all. Many of the DirectX SDK samples are available now on MSDN Code Gallery that build with the Windows 8.x SDK and do not make use of the legacy DirectX SDK.

The minimal solution to your immediate issue is to just install the Windows 8.1 SDK or the Visual Studio 2013 Remote Debugging Tools (x86 or x64)to get the correct version of the SDKDebugLayers installed for your operating system, but keep in mind that Frank Luna's book is rather dated now. See Book Recommendations for some additional notes.

answered on Stack Overflow Aug 21, 2014 by Chuck Walbourn • edited Aug 21, 2014 by Chuck Walbourn

User contributions licensed under CC BY-SA 3.0