D3D12CreateDevice function

Creates a device that represents the display adapter.

Syntax


HRESULT WINAPI D3D12CreateDevice(
  _In_opt_  IUnknown          *pAdapter,
            D3D_FEATURE_LEVEL MinimumFeatureLevel,
  _In_      REFIID            riid,
  _Out_opt_ void              **ppDevice
);

Parameters

pAdapter [in, optional]

Type: IUnknown*

A pointer to the video adapter to use when creating a device. Pass NULL to use the default adapter, which is the first adapter that is enumerated by IDXGIFactory1::EnumAdapters.

Note   Don't mix the use of DXGI 1.0 (IDXGIFactory) and DXGI 1.1 (IDXGIFactory1) in an application. Use IDXGIFactory or IDXGIFactory1, but not both in an application.
 
MinimumFeatureLevel

Type: D3D_FEATURE_LEVEL

The minimum D3D_FEATURE_LEVEL required for successful device creation.

riid [in]

Type: REFIID

The globally unique identifier (GUID) for the device interface. This parameter, and ppDevice, can be addressed with the single macro IID_PPV_ARGS.

ppDevice [out, optional]

Type: void**

A pointer to a memory block that receives a pointer to the device.

Return value

Type: HRESULT

This method can return one of the Direct3D 12 Return Codes.

Possible return values include those documented for CreateDXGIFactory1 and IDXGIFactory::EnumAdapters.

Example

The following code calls the creation methods in turn:

The process of creating devices and swap chains is similar to Direct3D 11.

Remarks

In order to be sure to pick up the first adapter that supports D3D12, use the following code.

void GetHardwareAdapter(IDXGIFactory4* pFactory, IDXGIAdapter1** ppAdapter)
{
    *ppAdapter = nullptr;
    for (UINT adapterIndex = 0; ; ++adapterIndex)
    {
        IDXGIAdapter1* pAdapter = nullptr;
        if (DXGI_ERROR_NOT_FOUND == pFactory->EnumAdapters1(adapterIndex, &pAdapter))
        {
            // No more adapters to enumerate.
            break;
        } 

        // Check to see if the adapter supports Direct3D 12, but don't create the
        // actual device yet.
        if (SUCCEEDED(D3D12CreateDevice(pAdapter, D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
        {
            *ppAdapter = pAdapter;
            return;
        }
        pAdapter->Release();
    }
}

The function signature PFN_D3D12_CREATE_DEVICE is provided as a typedef, so that you can use dynamic linking techniques (GetProcAddress) instead of statically linking.

The REFIID, or GUID, of the interface to a device can be obtained by using the __uuidof() macro. For example, __uuidof(ID3D12Device) will get the GUID of the interface to a device.

Examples

Create a hardware based device, unless instructed to create a WARP software device.


ComPtr<IDXGIFactory4> factory;
ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&factory)));

if (m_useWarpDevice)
{
    ComPtr<IDXGIAdapter> warpAdapter;
    ThrowIfFailed(factory->EnumWarpAdapter(IID_PPV_ARGS(&warpAdapter)));

    ThrowIfFailed(D3D12CreateDevice(
        warpAdapter.Get(),
        D3D_FEATURE_LEVEL_11_0,
        IID_PPV_ARGS(&m_device)
        ));
}
else
{
    ComPtr<IDXGIAdapter1> hardwareAdapter;
    GetHardwareAdapter(factory.Get(), &hardwareAdapter);

    ThrowIfFailed(D3D12CreateDevice(
        hardwareAdapter.Get(),
        D3D_FEATURE_LEVEL_11_0,
        IID_PPV_ARGS(&m_device)
        ));
}


Refer to the Example Code in the D3D12 Reference.

Requirements

Header

D3D12.h

Library

D3D12.lib

DLL

D3D12.dll

See also

Core Functions
Working Samples

 

 

Show: