Note: Subsequent d3d explanations are based on C ++.
Before explaining d3d initialization, we will introduce several pilot concepts.
1. The d3d SDK provides developers with two device types: reference device or ref and hardware abstract layer or Hal ).
The difference between the two devices is that ref performs 3D computation by means of software simulation, and all computation is completed by the CPU. It ensures that the functions used are completely correct, however, it is inefficient. It is only developed by developers and provides verification functions. Hal uses hardware acceleration cards for computing. Ref is only available during development and cannot be released.
In d3d, d3ddevtype_hal and d3ddevtype_ref are used to distinguish them.
2. antialiasing)
On a display with a limited resolution, the image is composed of a small lattice of an image. The grids are arranged in a regular row and line, which leads to a problem. When a diagonal line is displayed, a jagged image may be displayed, which degrades the visual effect of the image. In order to alleviate this problem, anti-sample technology has been developed.
It is developed based on the characteristics of the human eye. The principle is to fill the Sawtooth edge with some excessively similar colors, so that the Sawtooth edge looks like a smooth and excessive.
The general method for implementing anti-image removal is: first, scale up the image to be drawn, such as 2 times or 4 times. Then draw in memory according to the enlarged size. Finally, we need a sampling operation to reduce this large image by N times. Taking 2X as an example, we need to calculate the color of the corresponding pixel by adding two adjacent horizontal and vertical values in the large image, with a total of four pixel values weighted average.
Although the anti-sample algorithm is simple, it occupies a lot of resources. Therefore, in real-time rendering, try not to use a high anti-sample level.
In d3d, the anti-spam samples are divided into d3dmultisample_none and d3dmultisample_0000sample ...... d3dmultisample_16_sample, which have a total of 16 levels.
3. pixel Storage Format
D3dfmt_r8g8b, d3dfmt_x8r8g8b8, d3dfmt_a8r8g8b8, d3dfmt_a16r16g16f8, d3dfmt_a32r32g32b32
4. Memory Pool Classification
D3dpool_default: Default
D3dpool_manage: d3d hosting Mode
D3dpool_systemmem: system storage area, which cannot be accessed by d3d Devices
D3dpool_scratch: system storage zone (different from the previous one)
D3d initialization:
1. Before Initialization
Get a d3d interface pointer from com
Idirect3d9 * pd3d = direct3dcreate9 (d3d_sdk_version );
2. Check whether the hardware supports related features.
This process is mainly used to check whether the hardware supports features such as anti-sample and the level of support of the coloring tool.
Hresult idirect3d9: getdevicecaps (
Uint adapter, // [in] video card number, usually only one video card
D3ddevtype devicetype, // [in] device type, ref or Hal
D3dcaps9 * pcaps); // [out] basic performance information of the device
For example:
D3dcaps9 caps;
Pd3d-> getdevicecaps (d3dadapter_default, d3ddevtype_hal, & caps );
Verify the hardware 3D acceleration capability (hardware vertex transformation and illumination computing capability ):
Int Vp = 0;
If (caps. devcaps & d3ddevcaps_hwtransformanlight)
{
Vp = d3dcreate_hardware_vertexprocessing;
}
Else
{
Vp = d3dcreate_software_vertexprocessing;
}
Verify the version of the shadow.
If (caps. pixelshaderversion <d3dps_version (2, 0 ))
3. Set the parameters of the d3d device to be initialized.
D3dpresent_parameters para;
For more information, see d3d help.
4. Start Initialization
Idirect3ddevice9 * pdevice = 0;
Hresult hR = pd3d-> createdevice (
D3dadapter_default, // [in] use the default video card
D3ddevtype_hal, // [in] HAL Method
Hwnd, // window associated with [in]
VP, // [in] vertex Processing Method
& Para, // [in] device parameters
& Pdevice); // [out] Device
5. Check whether the initialization is successful.
If (failed (HR ))
{
: MessageBox (0, "Create device failed !! ", 0, 0 );
Return 0;
}