One of DX notes --- direct3d Basics

Source: Internet
Author: User

I. Prerequisites

1. Surface

The surface is a pixel matrix used by direct3d to store 2D image data. Width and height are measured in pixels, and pitch is measured in bytes. The interface idirect3dsurface is used to describe the surface.

Lockrect: This method is used to obtain the pointer pointing to the surface storage area. You can perform read/write operations on each pixel through pointer operations;

Unlockrect: Used in pairs. The call lock must be unlocked to unlock the surface storage area;

Getdesc: Get the description of the surface by filling d3dsurface_desc;

//Assume _surface is a pointer to an IDirect3DSurface9 interface//Assume a 32-bit pixel format for each pixel//Get the surface description.D3DSURFACE_DESC surfaceDesc;_surface->GetDesc(&surfaceDesc);//Get a pointer to the surface pixel dataD3DLOCKED_RECT lockedRect;_surface->LockRect(    &lockedRect,    0,    0);//Iterate through each pixel in the surface and set it to redDWORD *imageData = (DWORD *)lockedRect.pBits;for (int i = 0; i < surfaceDesc.height; ++i){    for (int j = 0; j < surfaceDesc.width; ++j)    {        int nIndex = i * lockedRect.pitch / 4 + j;        imageData[nIndex] = 0xffff000;    }}_surface->UnlockRect();

2. Multiple sampling

Multisample (multisampling) is a technology used to smooth block images.

D3dmultisample_type: defines the levels of Full-scene multisampling that the device can apply.

typedef enum D3DMULTISAMPLE_TYPE{    D3DMULTISAMPLE_NONE = 0,    D3DMULTISAMPLE_NONMASKABLE  = 1,    D3DMULTISAMPLE_2_SAMPLES = 2,    D3DMULTISAMPLE_3_SAMPLES = 3,    D3DMULTISAMPLE_4_SAMPLES = 4,    D3DMULTISAMPLE_5_SAMPLES = 5,    D3DMULTISAMPLE_6_SAMPLES = 6,    D3DMULTISAMPLE_7_SAMPLES = 7,    D3DMULTISAMPLE_8_SAMPLES = 8,    D3DMULTISAMPLE_9__SAMPLES = 9,    D3DMULTISAMPLE_10_SAMPLES = 10,    D3DMULTISAMPLE_11_SAMPLES = 11,    D3DMULTISAMPLE_12_SAMPLES = 12,    D3DMULTISAMPLE_13_SAMPLES = 13,    D3DMULTISAMPLE_14_SAMPLES = 14,    D3DMULTISAMPLE_15_SAMPLES = 15,    D3DMULTISAMPLE_16_SAMPLES = 16,    D3DMULTISAMPLE_FORCE_DWORD = 0xffffffff,} D3DMULTISAMPLE_TYPE, *LPD3DMULTISAMPLE_TYPE;

3. pixel format d3dformat

Specify the pixel format when creating a surface or texture (texture)

D3dfmt_r8g8b8 d3dfmt_x8r8g8b8 d3dfmt_a8r8g8b8 d3dfmt_a16b16g16r16f d3dfmt_a32b32g32r32f d3dfmt_dxt5

4. Memory Pool

D3d rutime memory types include video memory (VM), AGP memory (AM), and system memory (SM)

Video memory (VM ):VM is located on the graphics card memory, the CPU can only access through the AGP or PCI-E bus, read and write speed is very slow,
The number of consecutive VM writes on the CPU is slightly faster than the read speed because 32 or 64 bytes are allocated to the cache when the CPU writes to the VM (depending on the cache line length)
When the buffer is full, it will be written to the VM at a time;

System memory (SM ):SM is the system memory, and the CPU reads and writes are very fast,
Because SM is cached to Level 2 buffer, but the GPU cannot directly access the system buffer, the resources created in SM are,
GPUs cannot be used directly;

AGP memory (AM ):Am is the most troublesome type. am actually exists in the system memory,
However, this part of MEM will not be cached by the CPU, which means that the CPU read and write am will write a cache missing before accessing am through the memory bus,
Therefore, CPU read/write am is slower than SM, but continuous write is slightly faster than read,
The reason is that CPU write am uses "Write combining", and GPU can access am directly through the AGP or PCI-E Bus

All d3d resources are created in these three types of memory. When creating resources, we can specify the following storage flag,
D3dpool_default, d3dpool_managed, d3dpool_systemmem, and d3dpool_scratch

D3dpool_default: default value. This type of memory pool indicates that direct3d places Resources in the storage zone that is most suitable for this type of resource. This storage area may be a video memory, an AGP storage area, or a system storage area. Note: Before calling idirect3ddevice9: reset, you must destroy or release the resources in the default memory pool. After the preceding function is called, you must reinitialize the resources in the memory pool.
D3dpool_manage: resources placed in the managed memory pool are managed by direct3d (that is, resources are automatically transferred to the video memory or AGP storage zone by devices as needed ). In addition, these resources will retain a backup in the system storage area. In this way, direct3d will automatically update these resources to the video memory if necessary.
D3dpool_systemmem: Specify to put resources into the system storage area.
D3dpool_scratch: Specify to put resources into the system storage area. Unlike d3dpool_systemmem, these resources are not restricted by graphics devices. Therefore, devices cannot access resources in this type of memory pool. However, these resources can be copied to each other.

5. Switching links and Page Replacement

Direct3d maintains a surface set, which is usually composed of two to three surfaces called switching chains. This set is represented by the idirect3dswapchain9 interface.

The switching link and page replacement technologies are mainly used to generate smoother animations.

6. Deep Cache

The deep cache is a surface that contains only the depth information of a specific pixel, not the image data. The depth cache retains a depth entry for each pixel in the final drawn image. Therefore, when the resolution of the drawn image is 640x800, there are x Depth items in the depth cache.

In order to determine which pixels of an object are located before another object, direct3d blocks the objects located behind it. A technology called Deep cache or Z-cache is used.

Deep cache is used to calculate the depth of each pixel and perform a deep test. The basic content of the deep test is to compete different pixels in the same position based on the depth value. To select the pixels that should be written to this position. The pixel closest to the camera wins and is written to the corresponding position of the deep cache.

The format of the deep cache determines the accuracy of the deep test. The 24-bit depth cache is much more precise than the 16-bit depth cache.

D3dfmt_d32: 32-bit deep cache.
D3dfmt_d24s8: 24-bit deep cache, where 8-bit is reserved for template cache.
D3dfmt_d24x8: Specify only 24-bit depth buffer.
D3dfmt_x4s4: Specifies the 24-bit deep cache, 4 of which are reserved for template cache.
D3dfmt_d16: Specify only 16-bit deep cache.

Ii. direct3d Initialization

1. Obtain the idirect3d9 pointer.

IDirect3D9* Direct3DCreate9(  UINT SDKVersion);
Create an IDirect3D9 object and return an interface to it.

Sdkversion:The value of this parameter shocould be d3d_sdk_version
//step 1: Create the IDirect3D9 object    IDirect3D9 d3d9;    d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
 

2. Verify hardware vertex operations

//step 2: Check for hardware vp    //     HRESULT GetDeviceCaps(    //         [in]   UINT Adapter,    //         [in]   D3DDEVTYPE DeviceType,    //         [out]  D3DCAPS9 *pCaps    //         );    // Adapter [in]    // Type: UINT    //           Ordinal number that denotes the display adapter. D3DADAPTER_DEFAULT is always the primary display adapter.    // DeviceType [in]    // Type: D3DDEVTYPE    //           Member of the D3DDEVTYPE enumerated type. Denotes the device type.    // pCaps [out]    // Type: D3DCAPS9*    //           Pointer to a D3DCAPS9 structure to be filled with information describing the capabilities of the device.    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;
3. Fill in the d3dpresent_parameter Structure
// Step 3: Fill out the d3dpresent_parameters structure. d3dpresent_parameters d3dpp; d3dpp. backbufferwidth = nwidth; // The width of the surface in the backend buffer, in the unit of pixels d3dpp. backbufferheight = nheight; // The height of the surface in the backend buffer. The unit is pixel d3dpp. backbufferformat = d3dfmt_a8r8g8b8; // The background buffer pixel format d3dpp. backbuffercount = 1; // The number of backend caches required, usually 1 d3dpp. multisampletype = d3dmultisample_none; // multiple sampling types used by the background cache d3dpp. multisamplequality = 0; // multi-Sample Quality Level d3dpp. swap Effect = d3dswapeffect_discard; // a member of The d3dswapeffect Enumeration type. This type specifies the Page Replacement mode of the buffer in the switching link. The maximum effect of d3dswapeffect_discard is d3dpp. hdevicewindow = hwnd; // specify the application window d3dpp to be drawn. export wed = bwindowed; // true: window mode false: Full Screen mode d3dpp. enableautodepthstenpencil = true; // true: dierct3d automatically creates and maintains a deep cache or template cache d3dpp. autodepthstencilformat = d3dfmt_d24s8; // The depth cache or template cache pixel format (d3dfmt_d24s8 24-bit depth, 8-bit template) d3dpp. flags = 0; // additional features 0 unlabeled or d3dpresentf Two frequently-used d3dpresentflag_lockable_depthbuffer parameters in the lag set specify that the backend cache can be locked, which may reduce performance. // d3dpresentflag_discard_depthbuffer specifies that when the next backend cache is submitted, which depth or template cache will be discarded. This improves performance. d3dpp. frequency = d3dpresent_rate_default; // refresh frequency d3dpp. presentationinterval = d3dpresent_interval_immediate; // a member in the d3dpresent set, // submit immediately

4. Create the idirect3ddevice9 Interface

hResult = 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(hResult))    {        // try again using a 16-bit depth buffer        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;        hResult = d3d9->CreateDevice(            D3DADAPTER_DEFAULT,            deviceType,            hWnd,            vp,            &d3dpp,            device);        if(FAILED(hResult))        {            d3d9->Release(); // done with d3d9 object            ::MessageBox(0, "CreateDevice() - FAILED", 0, 0);            return false;        }    }

Finally, I will post my own results, although I basically copied them from the longbook

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.