First Chance Exception

1

I am following a tutorial to learn game programming with DirectX11. When I run the sample code it gives me this error:

First-chance exception at 0x76E12EEC in Chapter1.exe: Microsoft C++ exception: Platform::COMException ^ at memory location 0x0307E824. HRESULT:0x887A0004

The problem appears to be in featureLevel and creationFlag in the following code:

hr = D3D11CreateDevice(
            nullptr,
            D3D_DRIVER_TYPE_HARDWARE,
            nullptr,
            creationFlags, 
            featureLevels, 
            ARRAYSIZE(featureLevels),
            D3D11_SDK_VERSION, 
            &device, 
            &featureLevel,
            &context);  
    ThrowIfFailed(hr);

However, if I change values of creationFlags and featureLevels to 0 and nullptr, the code works fine. I am using Visual Studio 2012 with Windows 8.1 and Windows SDK 8.0.

Here is the relevant code:

UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

#if defined(_DEBUG)
    // For debugging
    creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

    D3D_FEATURE_LEVEL featureLevels[] = 
    {
        D3D_FEATURE_LEVEL_11_1,
        D3D_FEATURE_LEVEL_11_0,
    };

I read about first chance exception that it doesn't mean there is something really wrong with code, but it doesn't just go away. What should I do?

directx
directx-11
asked on Stack Overflow Nov 7, 2013 by Tehreem • edited Nov 7, 2013 by Ivan Aksamentov - Drop

1 Answer

3

Issue

You passing only D3D_FEATURE_LEVEL_11_1 and D3D_FEATURE_LEVEL_11_0, so D3D11CreateDevice() function fails, returns HRESULT which is not S_OK and your ThrowIfFailed(hr); function throws exception you've got.

You cannot create DirectX 11 hardware device and context if your GPU only supports DirectX 10: Createdevice*() function will fail. To be able to use DirectX 11 API (but not DirectX 11 features) on down-level hardware Microsoft introduced feature levels.

How to fix?

Just use conventional way of device creation. You will need to pass array with all possible feature levels

D3D_FEATURE_LEVEL arrFeatLevels [] =
{
    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,
};

so, DirectX API will automatically choose the highest supported one. (After device creation, you can find which one was chosen by looking at returned &featureLevel):

if(featureLevel >= D3D_FEATURE_LEVEL_11_0)
    std::cout << "Yay! we using D3D11! :) " << std::endl;
else if( featureLevel >= D3D_FEATURE_LEVEL_10_0)
    std::cout << "Oh noes! only D3D10 available! :(" <<std::endl;
else
    std::cout << "Man, where did you take that old videocard? =\ " <<std::endl;

Note, that DirectX 11 features (such as Shader Model 5, tessellation shaders; compute shader) will not be available on device/context created with feature level lower than D3D_FEATURE_LEVEL_11_0. Same way, DirectX 10 features (e.g. geometry shader) will not be available with feature level lower than D3D_FEATURE_LEVEL_10_0. All hardware supported features will run as usual.

Also, there are way to test features, that not supported by your hardware. You can create software emulated WARP device: pass D3D_DRIVER_TYPE_WARP. It is very slow and not intended for production code, but it permits developers test and debug D3D11 features even if they don't have top hardware.

Where to find my GPU's capabilities?

On GPU manufacturer's site. Or simply use tools like GPU-Z (it shows DX support) or GPU Caps Viewer (shows many OpenGL features).

Happy coding!= )

answered on Stack Overflow Nov 7, 2013 by Ivan Aksamentov - Drop • edited Jun 20, 2020 by Community

User contributions licensed under CC BY-SA 3.0