MSDN Community

D3D11CreateDevice

Namespace: DirectX::D3D11

Synopsis

HRESULT D3D11CreateDevice(
    IDXGIAdapter*            pAdapter,
    D3D_DRIVER_TYPE         DriverType,
    HMODULE                  Software,
    UINT                     Flags,
    const D3D_FEATURE_LEVEL* pFeatureLevels,
    UINT                     FeatureLevels,
    UINT                     SDKVersion,
    ID3D11Device**           ppDevice,
    D3D_FEATURE_LEVEL*       pFeatureLevel,
    ID3D11DeviceContext**    ppImmediateContext
);

Parameters

ParameterIn/OutDescription
pAdapterInPointer to the adapter to use, or NULL to let the system select one.
DriverTypeInSpecifies the driver type (e.g., D3D_DRIVER_TYPE_HARDWARE).
SoftwareInHandle to a DLL that implements a software rasterizer (only used when DriverType is D3D_DRIVER_TYPE_SOFTWARE).
FlagsInCreation flags (e.g., D3D11_CREATE_DEVICE_DEBUG).
pFeatureLevelsInArray of feature levels to attempt, ordered from highest to lowest.
FeatureLevelsInNumber of entries in pFeatureLevels.
SDKVersionInMust be set to D3D11_SDK_VERSION.
ppDeviceOutReceives the created ID3D11Device interface pointer.
pFeatureLevelOutReturns the feature level of the created device.
ppImmediateContextOutReceives the immediate context (ID3D11DeviceContext).

Return Value

If the function succeeds, it returns S_OK. Otherwise, it returns an HRESULT error code such as E_INVALIDARG or DXGI_ERROR_NOT_FOUND.

Remarks

Example

#include <d3d11.h>
#include <dxgi.h>
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")

int main()
{
    ID3D11Device*           device = nullptr;
    ID3D11DeviceContext*    context = nullptr;
    D3D_FEATURE_LEVEL       featureLevel;

    D3D_FEATURE_LEVEL levels[] = {
        D3D_FEATURE_LEVEL_11_1,
        D3D_FEATURE_LEVEL_11_0,
        D3D_FEATURE_LEVEL_10_1,
        D3D_FEATURE_LEVEL_10_0
    };

    HRESULT hr = D3D11CreateDevice(
        nullptr,                    // default adapter
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        D3D11_CREATE_DEVICE_DEBUG,
        levels,
        _countof(levels),
        D3D11_SDK_VERSION,
        &device,
        &featureLevel,
        &context
    );

    if (SUCCEEDED(hr))
    {
        // Device and context ready for rendering
    }
    // Release resources
    if (context) context->Release();
    if (device)  device->Release();
    return 0;
}

See Also