DX notes --- Direct3D basics, dx notes --- direct3d

Source: Internet
Author: User

DX notes --- Direct3D basics, dx notes --- direct3d

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.


3. pixel format D3DFORMAT

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


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: 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

A problem with dxdiag and Direct3D

You enter dxdiag in the running process and the DirectX diagnostic tool is displayed. Select display and enable Direct3D acceleration.

Start-run-dxdiag-display Direct3D unavailable

Install the video card driver.

Driver genie is the product of driver house.
Is a professional driver tool.

Related Article

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.