場景提交概述
場景提交即將在后臺緩沖區繪制好的場景提交到前臺緩沖區,從而在屏幕上顯示出來。提交接口函數是一組控制特定的渲染設備狀態的方法,這些設備影響顯示器的顯示。
(1)前臺緩沖區:這是一塊由顯卡轉換來的矩形存儲區,這塊矩形存儲區的內容顯示在顯示器或其他輸出設備上。
(2)后臺緩沖區:后臺緩沖區是一個表面,其內容可以提交到前臺緩沖區。
(3)交換鏈:一組后臺緩沖區集合,它們被順序地提交到前臺緩沖區。一般情況下,一個全屏交換鏈通過翻轉設備驅動接口(DDI)來提交隨后的顯示內容,窗口交換鏈通過位塊傳送DDI提交顯示內容。
前臺緩沖區不能直接在Direct3D API中使用。所以,應用程序不能對前臺緩沖區進行鎖定或渲染。DirectX
9.0應用程序中沒有主表面的概念,不能創建一個帶有主表面的對象。
窗口模式下的多視口(視區)
Direct3D設備對象擁有并控制自己的交換鏈,此外,應用程序可以使用函數IDirect3DDevice9::CreateAdditionalSwapChain()創建附加交換鏈,用來在同一個設備中提交多個視口。一般地說,應用程序為每個視口創建一個交換鏈,每個交換鏈對應一個特定的視口。應用程序在每個視口的后臺緩沖區內渲染圖形,然后用函數 IDirect3DDevice9::Present()將它們分別提交。注意:對于任何Direct3D設備對象,一次只能有一個交換鏈用于全屏顯示。
多顯示器操作
當一個設備被成功設置為全屏操作時,創建該設備的Direct3D對象被標識為擁有系統的所有顯卡。這種狀態稱為獨占模式(exclusive
mode),也就是說,Direct3D對象為獨占模式。獨占模式是指,這時其他所有的Direct3D對象創建的設備都不能進行全屏操作,也不能申請資源空間。此外,當一個對象是獨占模式時,所有未在全屏模式下的設備都將被設為丟失狀態。當Direct3D對象的最后一個全屏設備被設置為窗口模式或被銷毀時,獨占模式被取消。
當一個Direct3D設備是獨占模式時,設備將被分為兩大類,第一類設備有下列屬性:
(1)它們都是由同一個創建全屏設備的Direct3D對象創建的。
(2)因為設備是全屏的,它們具有同一個焦點窗口。
(3)它們代表不同于任何全屏設備的顯卡。
對于這種類型的設備,不用關心它們能否被重新設置或創建,因為它們不處于丟失狀態。甚至,這種類型的設備都可以被設置為全屏狀態。
不屬于第一類的設備即由其他Direct3D對象創建的設備,或和當前全屏設備不具有相同的焦點窗口,或和當前全屏設備使用不同的顯卡。這一類Direct3D設備不能被重新設置,將一直處于丟失狀態,直至當前全屏設備的獨占模式取消。這樣一來,一個多顯示器應用程序可在全屏模式下擁有多個設備,但是,這些設備必須由相同的Direct3D對象創建,對應于不同的物理顯卡并且共享同一個焦點窗口。
操作深度緩沖區
深度緩沖區與設備相關。當應用程序設置渲染目標時,需要訪問深度緩沖區??梢允褂煤瘮礗Direct3DDevice9::GetDepthStencilSurface()和IDirect3DDevice9::SetDepthStencilSurface()來操作深度緩沖區。
訪問前臺緩沖區
可以通過函數IDirect3DDevice9::GetFrontBufferData()訪問前臺緩沖區,這是得到一個反鋸齒場景屏幕快照的唯一方法。
圖形反鋸齒(antialiasing)
圖形像素在顏色緩沖區或屏幕中以一個二維坐標(x, y)表示當前位置。如果實際計算的像素值是浮點數,則將被轉換為整數坐標顯示,這種光柵化的處理方法可能使圖形出現鋸齒形外觀。圖形學中稱這種由于采樣頻率不足而造成的失真為鋸齒(alisasing),Direct3D采用圖形反鋸齒(通過多重采樣)來改善圖形的鋸齒效果,增加圖形邊緣的平滑度。
查詢設備是否支持多重采樣
使用IDirect3D::CheckDeviceMultiSampleType()函數檢查當前設備是否支持圖形多重采樣:
Determines if a multisampling technique is available on
this device.
HRESULT CheckDeviceMultiSampleType(
UINT Adapter,
D3DDEVTYPE DeviceType,
D3DFORMAT SurfaceFormat,
BOOL Windowed,
D3DMULTISAMPLE_TYPE MultiSampleType,
DWORD* pQualityLevels
);
Parameters
- Adapter
- [in] Ordinal number denoting the display adapter
to query. D3DADAPTER_DEFAULT is always the primary display adapter. This
method returns FALSE when this value equals or exceeds the number of display
adapters in the system. See Remarks.
- DeviceType
- [in] Member of the D3DDEVTYPE enumerated type,
identifying the device type.
- SurfaceFormat
- [in] Member of the D3DFORMAT enumerated type that
specifies the format of the surface to be multisampled. For more
information, see Remarks.
- Windowed
- [in] bool value. Specify TRUE to inquire about
windowed multisampling, and specify FALSE to inquire about full-screen
multisampling.
- MultiSampleType
- [in] Member of the D3DMULTISAMPLE_TYPE enumerated
type, identifying the multisampling technique to test.
- pQualityLevels
- [out] The number of quality stops available for a
given multisample type. This can be NULL if it is not necessary to return
the values.
Return Values
If the device can perform the specified multisampling
method, this method returns D3D_OK. D3DERR_INVALIDCALL is returned if the
Adapter or MultiSampleType parameters are invalid. This method returns
D3DERR_NOTAVAILABLE if the queried multisampling technique is not supported by
this device. D3DERR_INVALIDDEVICE is returned if DeviceType does not apply to
this adapter.
Remarks
This method is intended for use with both render-target
and depth-stencil surfaces because you must create both surfaces multisampled if
you want to use them together.
The following code fragment shows how you could use
IDirect3D9::CheckDeviceMultiSampleType to test for devices that support a
specific multisampling method.
if( SUCCEEDED(pD3D->CheckDeviceMultiSampleType( pCaps->AdapterOrdinal,
pCaps->DeviceType, BackBufferFormat,
FALSE, D3DMULTISAMPLE_3_SAMPLES, NULL ) ) &&
SUCCEEDED(pD3D->CheckDeviceMultiSampleType( pCaps->AdapterOrdinal,
pCaps->DeviceType, DepthBufferFormat,
FALSE, D3DMULTISAMPLE_3_SAMPLES, NULL ) ) )
return S_OK;
The preceding code will return S_OK if the device
supports the full-screen D3DMULTISAMPLE_3_SAMPLES multisampling method with the
surface format.
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;
Constants
- D3DMULTISAMPLE_NONE
- No level of full-scene multisampling is available.
- D3DMULTISAMPLE_NONMASKABLE
- Enables the multisample quality value. See
Remarks.
- D3DMULTISAMPLE_2_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_3_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_4_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_5_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_6_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_7_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_8_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_9__SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_10_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_11_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_12_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_13_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_14_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_15_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_16_SAMPLES
- Level of full-scene multisampling available.
- D3DMULTISAMPLE_FORCE_DWORD
- Forces this enumeration to compile to 32 bits in
size. Without this value, some compilers would allow this enumeration to
compile to a size other than 32 bits. This value is not used.
Remarks
In addition to enabling full-scene multisampling at
IDirect3DDevice9::Reset time, there will be render states that turn various
aspects on and off at fine-grained levels.
Multisampling is valid only on a swap chain that is
being created or reset with the D3DSWAPEFFECT_DISCARD swap effect.
The multisample antialiasing value can be set with the
parameters (or sub-parameters) in the following methods.
Method |
Parameters |
Sub-parameters |
IDirect3D9::CheckDeviceMultiSampleType |
MultiSampleType and pQualityLevels |
|
IDirect3D9::CreateDevice |
pPresentationParameters |
MultiSampleType and pQualityLevels |
IDirect3DDevice9::CreateAdditionalSwapChain |
pPresentationParameters |
MultiSampleType and pQualityLevels |
IDirect3DDevice9::CreateDepthStencilSurface |
MultiSampleType and pQualityLevels |
|
IDirect3DDevice9::CreateRenderTarget |
MultiSampleType and pQualityLevels |
|
IDirect3DDevice9::Reset |
pPresentationParameters |
MultiSampleType and pQualityLevels |
It is not good practice to switch from one multisample
type to another to raise the quality of the antialiasing.
D3DMULTISAMPLE_NONE enables swap effects other than
discarding, locking, and so on.
Whether the display device supports maskable
multisampling (more than one sample for a multiple-sample render-target format
plus antialias support) or just non-maskable multisampling (only antialias
support), the driver for the device provides the number of quality levels for
the D3DMULTISAMPLE_NONMASKABLE multiple-sample type. Applications that just use
multisampling for antialiasing purposes only need to query for the number of
non-maskable multiple-sample quality levels that the driver supports.
The quality levels supported by the device can be
obtained with the pQualityLevels parameter of
IDirect3D9::CheckDeviceMultiSampleType. Quality levels used by the
application are set with the MultiSampleQuality parameter of
IDirect3DDevice9::CreateDepthStencilSurface and
IDirect3DDevice9::CreateRenderTarget.
See D3DRS_MULTISAMPLEMASK for discussion of maskable
multisampling.
創建使用多重采樣的Direct3D設備
創建使用多重采樣的Direct3D設備,需要將函數CreateDevice()的第5個參數D3DPRESENT_PARAMETERS結構體的MultiSampleType成員設為將要設置的多重采樣類型,SwapEffect成員設為D3DSWAPEFFECT_DISCARD。
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES;
啟用多重采樣的全景圖形反鋸齒
調用渲染狀態設置函數IDirect3DDevice9::SetRenderState(),將第一個參數設置為D3DRS_MULTISAMPLEANTIALIAS,將第二個參數設為TRUE將激活多重采樣,設置FALSE將禁用多重采樣。
圖形反鋸齒示例程序
示例程序AntiAlisa演示了圖形反鋸齒效果。目前很多顯示硬件可能不支持圖形反鋸齒,所以在創建渲染設備前應進行設備檢查,如果當前顯示硬件不支持,可以創建參考設備來測試圖形反鋸齒效果。在示例程序AntiAlisa運行時通過單擊鼠標左鍵,可以切換是否啟用圖形反鋸齒操作。

啟用反鋸齒

禁用反鋸齒
完整源代碼:
#include <d3d9.h>
#define CLASS_NAME "GameApp"
#define release_com(p) do { if(p) { (p)->Release(); (p) = NULL; } } while(0)
IDirect3D9* g_d3d;
IDirect3DDevice9* g_device;
IDirect3DVertexBuffer9* g_vertex_buffer;
BOOL g_support_anti_aliasing = TRUE;
struct sCustomVertex
{
float x, y, z, rhw;
DWORD color;
};
#define D3DFVF_CUSTOM_VERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
void init_vertices()
{
sCustomVertex vertices[] =
{
{ 50.0f, 250.0f, 0.5f, 1.0f, 0xffff0000, },
{ 150.0f, 50.0f, 0.5f, 1.0f, 0xff00ff00, },
{ 250.0f, 250.0f, 0.5f, 1.0f, 0xff0000ff, },
};
// push vertex data into vertex buffer
g_device->CreateVertexBuffer(sizeof(vertices), 0, D3DFVF_CUSTOM_VERTEX, D3DPOOL_DEFAULT, &g_vertex_buffer, NULL);
void* ptr;
g_vertex_buffer->Lock(0, sizeof(vertices), (void**)&ptr, 0);
memcpy(ptr, vertices, sizeof(vertices));
g_vertex_buffer->Unlock();
}
bool init_d3d(HWND hwnd)
{
g_d3d = Direct3DCreate9(D3D_SDK_VERSION);
if(g_d3d == NULL)
return false;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.MultiSampleType = D3DMULTISAMPLE_4_SAMPLES;
if(FAILED(g_d3d->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X8R8G8B8, FALSE,
D3DMULTISAMPLE_4_SAMPLES, NULL)))
{
MessageBox(hwnd, "Hardware do not support antialiasing, use ref device!", "ERROR", MB_OK);
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
}
else
{
if(FAILED(g_d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_device)))
{
return false;
}
}
init_vertices();
return true;
}
void cleanup()
{
release_com(g_vertex_buffer);
release_com(g_device);
release_com(g_d3d);
}
void render()
{
g_device->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, g_support_anti_aliasing);
g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(5, 5, 5), 1.0f, 0);
g_device->BeginScene();
g_device->SetStreamSource(0, g_vertex_buffer, 0, sizeof(sCustomVertex));
g_device->SetFVF(D3DFVF_CUSTOM_VERTEX);
g_device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
LRESULT WINAPI WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
g_support_anti_aliasing = !g_support_anti_aliasing;
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR, INT)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_CLASSDC;
wc.lpfnWndProc = WinProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = inst;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = CLASS_NAME;
wc.hIconSm = NULL;
if(! RegisterClassEx(&wc))
return -1;
HWND hwnd = CreateWindow(CLASS_NAME, "Direct3D App", WS_OVERLAPPEDWINDOW, 200, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL);
if(hwnd == NULL)
return -1;
if(init_d3d(hwnd))
{
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
UpdateWindow(hwnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
render();
}
}
cleanup();
UnregisterClass(CLASS_NAME, wc.hInstance);
return 0;
}