Search code examples
c++3ddirectxdirectx-11graphical-programming

DirectX - CreateDeviceAndSwapChain returns E_INVALIDARG


I'm trying to initialize Direct3D11 in C++. On machines that have Visual Studio installed(all of those are running on Windows 10), it runs fine. On other computers (without Visual studio installed, Windows 10 and 7) it returns E_INVALIDARG.

The flag P_FeatureLevelsSupported says 0 on those computers. On mine it says D3D_FEATURE_LEVEL_11_1. So I guess it has something to do with the DirectX installation or maybe because the SDK is missing( but wouldn't that be strange? :D )

By running dxdiag, I know that those machines support DirectX11_0.

Is there something i need to install? The software has to run on the PCs of our clients.

The Code that causes the error:

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


//see microsoft documentation, we use 11_1 or 11_0 if 11_1 is not supported by the client machine
//https://learn.microsoft.com/en-us/windows/desktop/direct3d11/overviews-direct3d-11-devices-initialize
result  = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, D3D11_CREATE_DEVICE_DEBUG, lvl, _countof(lvl), D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &device, &P_FeatureLevelsSupported, &deviceContext);
if(result == E_INVALIDARG) //check with FEATURE_LEVEL_11_0
      D3D11CreateDeviceAndSwapChain(NULL, 
                              D3D_DRIVER_TYPE_HARDWARE, 
                              NULL,
                              D3D11_CREATE_DEVICE_DEBUG, 
                              &lvl[1],
                              _countof(lvl) - 1,
                              D3D11_SDK_VERSION,
                              &swapChainDesc, 
                              &swapChain, 
                              &device,
                              &P_FeatureLevelsSupported, 
                              &deviceContext);

Thanks in advance :)


Solution

  • You are asking to create the debug device by passing in D3D11_CREATE_DEVICE_DEBUG. For that to succeed you must have D3D11*SDKLayers.dll installed which you probably have on your dev machines. See here for details which includes:

    Debug Layer The debug layer provides extensive additional parameter and consistency validation (such as validating shader linkage and resource binding, validating parameter consistency, and reporting error descriptions).

    To create a device that supports the debug layer, you must install the DirectX SDK (to get D3D11SDKLayers.dll), and then specify the D3D11_CREATE_DEVICE_DEBUG flag when calling the D3D11CreateDevice function or the D3D11CreateDeviceAndSwapChain function. If you run your application with the debug layer enabled, the application will be substantially slower. But, to ensure that your application is clean of errors and warnings before you ship it, use the debug layer. For more info, see Using the debug layer to debug apps.

    Note

    For Windows 8, to create a device that supports the debug layer, install the Windows Software Development Kit (SDK) for Windows 8 to get D3D11_1SDKLayers.dll.

    If you don't need a debug device when on a customer machine just remove that flag.