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
| Parameter | In/Out | Description |
|---|---|---|
pAdapter | In | Pointer to the adapter to use, or NULL to let the system select one. |
DriverType | In | Specifies the driver type (e.g., D3D_DRIVER_TYPE_HARDWARE). |
Software | In | Handle to a DLL that implements a software rasterizer (only used when DriverType is D3D_DRIVER_TYPE_SOFTWARE). |
Flags | In | Creation flags (e.g., D3D11_CREATE_DEVICE_DEBUG). |
pFeatureLevels | In | Array of feature levels to attempt, ordered from highest to lowest. |
FeatureLevels | In | Number of entries in pFeatureLevels. |
SDKVersion | In | Must be set to D3D11_SDK_VERSION. |
ppDevice | Out | Receives the created ID3D11Device interface pointer. |
pFeatureLevel | Out | Returns the feature level of the created device. |
ppImmediateContext | Out | Receives 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
- The function creates a Direct3D 11 device and an immediate context. The device represents the virtual GPU, while the context is used for rendering commands.
- When
DriverTypeisD3D_DRIVER_TYPE_HARDWAREandpAdapterisNULL, the runtime selects the default adapter. - Debug layers can be enabled with
D3D11_CREATE_DEVICE_DEBUGbut require the Graphics Tools optional component to be installed. - Feature level negotiation determines the highest level supported by both the hardware and the requested list.
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;
}