DirectX9: Chapter initialize Direct3D

A, Direct3D Overview

Direct3D is a set of underlying graphics API, an application can be regarded as an intermediary device that interacts with graphics.
The interaction between applications, Direct3D and hardware:

there is a link -HAL (Hardware Abstraction Layer, the hardware abstraction layer) between Direct3D and graphics equipment.
Why should this link: Due to a wide variety of graphics cards available in the market, so Direct3D graphics device can not directly interact with, Direct3D requires equipment manufacturers to achieve a HAL. HAL is a pointing device to complete the set of codes associated with operation of certain devices. Direct3D so you do not have to know the specific details of the devices and their specifications can be specified independent of specific hardware.
Equipment manufacturers will it supports all the functions are implemented into the HAL. Those Direct3D support, but the device does not support the feature is not implemented in the HAL. A call is not implemented in the HAL can result in Direct3D function call fails, unless it is a vertex processing operation, and the user has specified the use of software vertex operation mode. In this case the user function can be implemented in software executed by computing Direct3D time to simulate a desired manner.

REF equipment

Sometimes Direct3D provides some features are not supported by your graphics device, but still want to use these features, in order to meet this demand, Direct3D provides a reference raster devices (reference rasterizer device), REF device that is capable of operation in a software way fully supports Direct3D API. With REF device, you can use those features not currently supported by the hardware in your code, and these properties for testing.

Two, Direct3D initialization

1. Get a pointer to the interface IDirect3D9 (information system physical hardware devices that interface for obtaining the IDirect3DDevice9 and create an interface, the interface is a C ++ object that represents a physical hardware device that we used to display 3D graphics.)

    IDirect3D9 * _d3d9;
    _d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

If the function fails, it returns a NULL pointer.
There are two main purposes IDirect3D9 objects: object creation device enumeration and IDirect3DDevice9 type, the device list acquisition means available in the system performance of each graphics card display mode, and other formatting information. (Such as the type of objects created IDrect3DDevice9 kind of physical devices, we need to know the physical device supported by the display format and model configuration information, in order to find a workable configuration, we must use the interface to enumerate method of IDrect3D9)

2. The inspection device performance (D3DCAPS9), determines whether to support the main graphics card hardware vertex operation. In order to create an interface IDirect3DDevice9, we must be clear whether the video card supports this feature.

//GetDeviceCaps参数介绍
HRESULT IDirect3D9::GetDeviceCaps{
    UINT Adapter, //指定物理显卡序号
    D3DDEVTYPE DeviceType, //指定设备类型(例如硬件设备(D3DDEVTYTPE_HAL)或软件设备(D3DDEVTYPE_REF))
    D3DCAPS9 *pCaps  //返回已初始化的设备性能结构实例
};
D3DCAPS9 caps;
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);

int vp = 0;
if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
    vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; //硬件顶点运算
else
    vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; //软件顶点运算

We will vertex operation type with variable vp saved for later use, this type of operation is the culmination of the future because when you create an object IDirect3DDevice type must be specified to be used.

Example 3. D3DPRESENT_PARAMENTERS a structure initialization. The data structure consists of many members, we can specify the interface IDirect3DDevice9 that will be created by these variables properties

typedef struct _D3DPERSENT_PARAMETERS_{
    UINT BackBufferWidth; //后台缓存区宽度,单位为像素
    UINT BackBufferHeight; //后台缓存区高度
    D3DFORMAT BackBufferFormat; //后台缓存区像素格式(如32为像素格式:D3DFMT_A8R8G8B8)
    UINT BackBufferCount; //后台缓存区个数,通常为1
    D3DMULTISAMPLE_TYPE MultiSampleType; //后台缓存所使用的多重采样类型
    DWORD MultiSampleQuality; //多重采样的质量水平
    D3DSWAPEFFECT SwapEffect; //指定交换链中的缓存的页面置换方式。指定为D3DSWAPEFFECT_DISCARD时效率最高
    HWND hDeviceWindow; //与设备相关的窗口句柄。制定了要进行绘制的应用程序窗口
    BOOL Windowed; //全屏(false)或窗口(true)
    BOOL EnableAutoDepthStencil; //设为true,则Direct3D自动创建并维护深度缓存或模板缓存
    D3DfORMAT AutoDepthStencilFormat; //深度缓存或模板缓存的像素格式(用24为表示深度并将8位保留供模板缓存使用,D3DFMT_D24S8)
    DWORD Flags; //其他的附加特性标志(通常指定为0或NULL)
    UINT FullScren_RefreshRateInHz; //指定屏幕的刷新频率(默认为D3DPRESENT_RATE_DEFAULT)
    UINT PresentationInterval; //D3DPRESENT集合的一个成员。其中两个成员较为常见(D3DPRESENT_INTERVAL_IMMEDIATE,D3DPRESENT_INTERVAL_DEFAULT)
}D3DPRESENT_PARAMETERS;

An example of filling the structure:

    D3DPRESENT_PARAMETERS d3dpp;
    d3dpp.BackBufferWidth = width;
    d3dpp.BackBufferHeight = height;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferCount = 1;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hwnd;
    d3dpp.Windowed = windowed;
    d3dpp.EnableAutoDepthStencil = true;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.Flags = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;

4. Create D3D Device Class
D3DPRESENT_PARAMENTER structure created using the initialized IDirect3DDevice9 objects (represents our physical hardware to display 3D graphics)

HRESULT IDirect3D9::CreateDevice{
    UINT Adapter, //指定我们希望已创建的IDirect3DDevice9对象代表哪块物理显卡
    D3DEVTYPE DeviceType, //制定需要使用的设备类型(如硬件设备D3DDEVTYPE_HAL或软件设备D3DDEVTYPE_REF)
    HWND hFocusWindow, //与设备相关的窗口句柄。通常情况下是指设备所要进行绘制的目标窗口
        DWIRD BehaviorFlags, //该参数可为D3DCREATE_HARDWARE_VERTEXPROCESSING 或 D3DCREATE_SOFTWARE_VERTEXPROCESSING
    D3DPRESENT_PARAMENTERS *pPresentationParmeters, //一个已经完成初始化的D3DPRESENT_PARAMETERS类型的实例,该实例定义了设备的一些特性
    IDirect3DDevice9** ppReturnedDeviceInterface //返回所创建的设备
};

Function call:

hr = d3d9->CreateDevice(
    D3DADAPTER_DEFAULT, // primary adapter
    deviceType,         // device type
    hwnd,               // window associated with device
    vp,                 // vertex processing
    &d3dpp,             // present parameters
    device);            // return created device
if (FAILED(hr))
{
    d3d9->Release(); // done with d3d9 object
    ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);
    return false;
}

Three, D3D example of a completed process run


Reference Links: https://www.cnblogs.com/k5bg/p/11088258.html

Guess you like

Origin www.cnblogs.com/acmlzq/p/11788250.html