D3D學習筆記(初始化Direct3D-1)

原文出處:點擊打開鏈接

Direct3D:是一種低層圖形API,它能讓我們利用3D硬件加速來渲染3D世界。我們可以把Direct3D看作是應用程序和圖形設備之間的中介。


HAL Direct3D不能直接作用於圖形設備,因爲現在市面上的顯卡種類實在是太多了並且每種顯卡都有不同的性能和處理事件的方式。例如,兩種不同的顯卡實現清屏的方式也可能是不同的。因此,Direct3D要求設備製造商實現HALHAL是一組指示設備執行某種操作的特殊設備代碼的集合。用這種方法,Direct3D避免了必須去了解某個設備的特殊細節,使它能夠獨立於硬件設備。


表面(Surface):表面是一個像素點陣,在Direct3D中主要用來存儲2D圖形數據。


表面的Width和Height是按像素計算的。Pitch以字節爲單位。而且Pitch有可能比Width大且依賴於低層硬件,所以不能單純的認爲Pitch = Width * sizeof (pixelFormat)。

----------------------------------------------------------------------------------------------------------------------

先來看看SDK上定義的D3DSURFACE_DESC結構

----------------------------------------------------------------------------------------------------------------------


D3DSURFACE_DESC Structure

           Describes a surface.

Syntax

           typedef struct _D3DSURFACE_DESC {

                                        D3DFORMAT Format;

                                        D3DRESOURCETYPE Type;

                                        DWORD Usage;

                                        D3DPOOL Pool;

                                        D3DMULTISAMPLE_TYPE MultiSampleType;

                                        DWORD MultiSampleQuality;

                                        UINT Width;

                                        UINT Height;

           }D3DSURFACE_DESC;

Members

         Format

                      Member of theD3DFORMAT enumerated type, describing the surface format.

           Type

                      Member of theD3DRESOURCETYPEenumerated type, identifying this resource as a surface.

         Usage

                     Either theD3DUSAGE_DEPTHSTENCILorD3DUSAGE_RENDERTARGETvalues. For more information, seeD3DUSAGE.

           Pool

                     Member of theD3DPOOL enumerated type, specifying the class of memory allocated for this surface.

MultiSampleType

                     Member of the D3DMULTISAMPLE_TYPEenumerated type, specifying the levels of full-scene multisampling supported by the surface.

MultiSampleQuality

                     Quality level. The valid range is between zero and one less than the level returned by pQualityLevels used byIDirect3D9::CheckDeviceMultiSampleType. Passing a larger value returns the error,D3DERR_INVALIDCALL. The MultisampleQuality values of paired render targets, depth stencil surfaces and the MultiSample type must all match.

           Width

                     Width of the surface, in pixels.

         Height

                     Height of the surface, in pixels.

----------------------------------------------------------------------------------------------------------------------

SDK中對 LockRect的說明

----------------------------------------------------------------------------------------------------------------------

IDirect3DSurface9::LockRect   Method


             Locks a rectangle on a surface.

Syntax

             HRESULT LockRect(      
                                  D3DLOCKED_RECT *pLockedRect,

                                  const RECT *pRect,

                                  DWORD Flags

                                  );

Parameters

pLockedRect

                      [out] Pointer to a D3DLOCKED_RECT structure that describes the locked region.

          pRect

                      [in] Pointer to a rectangle to lock. Specified by a pointer to aRECT  structure. Specifying NULL for this parameter expands the dirty region to cover the entire surface.

           Flags

                      [in] Combination of zero or more locking flags that describe the type of lock to perform. For this method, the valid flags are:

                                                        D3DLOCK_DISCARD

                                                        D3DLOCK_DONOTWAIT

                                                        D3DLOCK_NO_DIRTY_UPDATE

                                                        D3DLOCK_NOSYSLOCK

                                                        D3DLOCK_READONLY

          You may not specify a subrect when using D3DLOCK_DISCARD. For a description of the flags, seeD3DLOCK.

Return Value

                        If the method succeeds, the return value is D3D_OK.

                        If the method fails, the return value can be D3DERR_INVALIDCALL or D3DERR_WASSTILLDRAWING.

Remarks

         If theD3DLOCK_DONOTWAIT flag is specified and the driver cannot lock the surface immediately, IDirect3DSurface9::LockRect will return D3DERR_WASSTILLDRAWING  so that an application can use the CPU cycles while waiting for the driver to lock the surface.

         The only lockable format for a depth-stencil surface is D3DFMT_D16_LOCKABLE. See D3DFORMAT.

          For performance reasons, dirty regions are recorded only for level zero of a texture. Dirty regions are automatically recorded when IDirect3DSurface9::LockRect  is called withoutD3DLOCK_NO_DIRTY_UPDATE orD3DLOCK_READONLY. See  IDirect3DDevice9::UpdateTexture  for more information.

          A multisample back buffer cannot be locked.

          This method cannot retrieve data from a surface that is is contained by a texture resource created with D3DUSAGE_RENDERTARGET  because such a texture must be assigned to  D3DPOOL_DEFAULT  memory and is therefore not lockable. In this case, use insteadIDirect3DDevice9::GetRenderTargetData to copy texture data from device memory to system memory.

----------------------------------------------------------------------------------------------------------------------------------------

在代碼中,我們可以使用IDirect3DSurface9接口來描述表面。這個接口提供若干方法來直接讀寫表面數據並且還有一個方法用來返回表面信息。IDirect3DSurface9中最重要的方法是:

        LockRect——使用這個方法,我們將獲得一個指向表面內存的指針,然後,通過一系列指針運算,我們可以對表面上任一個像素點進行讀、寫操作。

        UnlockRect——當你調用了LockRect和完成了對錶面內存的訪問後,你必須調用這個方法給表面解鎖。

        GetDesc——這個方法將通過填充D3DSURFACE_DESC結構來返回表面的描述信息。

最初鎖定表面和改寫每一像素看來稍微有點迷茫。下面的代碼表示鎖定表面並將每一像素染成紅色:

// Assume _surface is a pointer to an IDirect3DSurface9 interface.

// Assumes a 32-bit pixel format for each pixel.

// Get the surface description.描述表面

D3DSURFACE_DESC surfaceDesc;

_surface->GetDesc(&surfaceDesc); //注意已經假定了_surface是指向IDirect3DSurface9藉口的指針


// Get a pointer to the surface pixel data.取得指向該內存區的指針

D3DLOCKED_RECT lockedRect;

_surface->LockRect(

              &lockedRect,//pointer to receive locked data指向申請到的內存區域

              0, // lock entire surface

              0); // no lock flags specified


// Iterate through each pixel in the surface and set it to red.設置指定的表面爲紅色

DWORD* imageData = (DWORD*)lockedRect.pBits;

for(int i = 0; i < surfaceDesc.Height; i++)

{

       for(int j = 0; j < surfaceDesc.Width; j++)

       {

              // index into texture, note we use the pitch and divide by

              // four since the pitch is given in bytes and there are

              // 4 bytes per DWORD.

              int index = i * lockedRect.Pitch / 4 + j;

              imageData[index] = 0xffff0000; // red

       }

}

_surface->UnlockRect();

程序中D3DLOCKED_RECT結構的定義如下:

typedef struct _D3DLOCKED_RECT {

       INT Pitch; // the surface pitch

       void *pBits; // pointer to the start of the surface memory

} D3DLOCKED_RECT;

在這裏有一些關於表面鎖定代碼的一些說明。32-bit像素格式設定這是很重要的,我們把bits轉換成DWORDs


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章