D3D11CreateDeviceAndSwapChain
Creates a Direct3D 11 device, a Direct3D 11 immediate context, and a Direct3D 11 swap chain.
HRESULT D3D11CreateDeviceAndSwapChain(
[in, optional] IDXGIAdapter* pAdapter,
[in] D3D_DRIVER_TYPE DriverType,
[in, optional] HMODULE Software,
[in] UINT Flags,
[in] const D3D_FEATURE_LEVEL* pFeatureLevels,
[in] UINT FeatureLevels,
[in] UINT SDKVersion,
[in, optional] const DXGI_SWAP_CHAIN_DESC* pSwapChainDesc,
[out, optional] IDXGISwapChain** ppSwapChain,
[out, optional] ID3D11Device** ppDevice,
[out, optional] D3D_FEATURE_LEVEL* pFeatureLevel,
[out, optional] ID3D11DeviceContext** ppImmediateContext
);
Parameters
- pAdapter
- Pointer to the video memory adapter to use for creating the device. If `NULL`, D3D will attempt to determine the adapter to use.
- DriverType
- A D3D_DRIVER_TYPE-typed value that specifies the driver type.
- Software
- Handle to the DLL that implements a software rasterizer. Must be `NULL` if `DriverType` is not `D3D_DRIVER_TYPE_SOFTWARE`.
- Flags
- Runtime layer to enable or disable during device creation. See D3D11_CREATE_DEVICE_FLAG.
- pFeatureLevels
- An array of D3D_FEATURE_LEVEL-typed values that specifies the feature levels for the device. The array is ordered from highest to lowest.
- FeatureLevels
- The number of feature levels in the `pFeatureLevels` array.
- SDKVersion
- The SDK version. Use `D3D11_SDK_VERSION`.
- pSwapChainDesc
- Pointer to a DXGI_SWAP_CHAIN_DESC structure that describes the swap chain. Can be `NULL`.
- ppSwapChain
- Address of a pointer to the created swap chain object (IDXGISwapChain).
- ppDevice
- Address of a pointer to the created device object (ID3D11Device).
- pFeatureLevel
- Pointer to a D3D_FEATURE_LEVEL-typed variable that receives the feature level for the created device.
- ppImmediateContext
- Address of a pointer to the created immediate context object (ID3D11DeviceContext).
Return Value
Returns `S_OK` on success or one of the Direct3D 11 Return Codes on failure.
Remarks
This function creates a Direct3D 11 device, its immediate context, and a swap chain. The device is the object that represents the graphics adapter. The immediate context is the object used for rendering commands. The swap chain is an array of buffers that DirectX uses to display rendered frames.
When you call D3D11CreateDeviceAndSwapChain, you specify the desired feature levels for the device.
Direct3D will create a device that supports the highest feature level that is also supported by the hardware.
For more information on creating a device, see Creating a Device. For more information on swap chains, see Swap Chains.
Example
The following code example shows how to create a Direct3D 11 device and swap chain.
// Required headers
#include <d3d11.h>
#include <dxgi.h>
// ...
IDXGISwapChain* pSwapChain = NULL;
ID3D11Device* pDevice = NULL;
ID3D11DeviceContext* pImmediateContext = NULL;
D3D_FEATURE_LEVEL featureLevel;
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 1;
sd.BufferDesc.Width = 640;
sd.BufferDesc.Height = 480;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd; // Your window handle
sd.Windowed = TRUE;
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
HRESULT hr = D3D11CreateDeviceAndSwapChain(
NULL, // Use default adapter
D3D_DRIVER_TYPE_HARDWARE,
NULL, // No software rasterizer
0, // No flags
featureLevels,
numFeatureLevels,
D3D11_SDK_VERSION,
&sd,
&pSwapChain,
&pDevice,
&featureLevel,
&pImmediateContext
);
if (FAILED(hr))
{
// Handle error
return E_FAIL;
}
// Device and swap chain created successfully.
// ...
Requirements
| Friendly name | Type |
|---|---|
| Minimum supported client | Windows 7 64-bit |
| Minimum supported server | Windows Server 2008 R2 |
| Header | d3d11.h (include D3D11.h) |
| Library | d3d11.lib |
| DLL | d3d11.dll |
| Product | DirectX 11 |