文中提到的书,如无特殊标注,都指经典的directx编程指南:《Introduction to 3D Game Programming with Directx 11》
D3D初始化主要根据以下步骤:
- Create the
ID3D11Device
andID3D11DeviceContext
interfaces using theD3D11CreateDevice
function.- Check 4X MSAA quality level support using the
ID3D11Device::CheckMultisampleQualityLevels
method.- Describe the characteristics of the swap chain we are going to create by filling out an instance of the
DXGI_SWAP_CHAIN_DESC
structure.- Query the
IDXGIFactory
instance that was used to create the device, and create anIDXGISwapChain
instance.- Create a render target view to the swap chain’s back buffer.
- Create the depth/stencil buffer and its associated depth/stencil view.
- Bind the render target view and depth/stencil view to the output merger stage of the rendering pipeline so that they can be used by Direct3D.
- Set the viewport.
1.创建 ID3D11Device
和ID3D11DeviceContext
两者的作用如下:
- The
ID3D11Device
interface is used to check feature support, and allocate resources.- The
ID3D11DeviceContext
interface is used to set render states, bind resources to the graphics pipeline, and issue rendering commands.
补充一下:DeviceContext分immediate context和deferred context两类。一般主线程内都设置为immediate context。
ID3D11Device
的创建可用如下函数:
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
);
pAdapter
指明了要使用的设备(个人理解为显卡),设为nullptr
表示当前采用的主设备。DriverType
一般使用D3D_DRIVER_TYPE_HARDWARE
。只有当硬件设备无法支持时考虑其他选项(效果一般较差)。其他可选的有:D3D_DRIVER_TYPE_REFERENCE
(非常慢的软件模拟设备)、D3D_DRIVER_TYPE_WARP
(D3D10版本提供的软件模拟设备,不被D3D11支持)、D3D_DRIVER_TYPE_SOFTWARE
(软件设备总称,用于选择其他第三方软件模拟设备)。Software
:在第二项使用D3D_DRIVER_TYPE_SOFTWARE
,在这个参数里提供相应的软件设备。在使用硬件设备时一般置空。Flags
:标志位,有两个常见的标志位(可以通过|
,即“位或”相结合):D3D11_CREATE_DEVICE_DEBUG
(这个标志位开启时,D3D会把调试信息发送到VC++ output window)、D3D11_CREATE_DEVICE_SINGLETHREADED
(如果能保证D3D仅在单线程下被调用,则可以提供效率——此处我理解是仅针对该进程内部,如果没有多线程即可)。pFeatureLevels
:可选的D3D_FEATURE_LEVEL
数组列表。函数会依次测试数组中各项,直到遇到一个可用的D3D_FEATURE_LEVEL
。置为nullptr表示选用可用的最高级D3D_FEATURE_LEVEL
。FeatureLevels
:第5个参数中数组元素的个数。如果第5个参数为nullptr,这里用0填充。SDKVersion
:用D3D11_SDK_VERSION