通過DXUT使用設(shè)備
DirectX設(shè)備的創(chuàng)建在DXUT中得到了改進(jìn)。你可以讓你的應(yīng)用程序直接創(chuàng)建設(shè)備而其它有框架提供的特征仍然可用。
創(chuàng)建設(shè)備
選擇最佳的設(shè)備設(shè)置
修改可用的設(shè)備設(shè)置
降為軟件頂點(diǎn)處理
使用你自己的設(shè)備
創(chuàng)建設(shè)備
典型地,你將通過標(biāo)準(zhǔn)的Direct3D方法創(chuàng)建設(shè)備
HRESULT CreateDevice(
??? UINT????????????????? Adapter,
??? D3DDEVTYPE??????????? DeviceType,
??? HWND????????????????? hFocusWindow,
??? DWORD???????????????? BehaviorFlags,
??? D3DPRESENT_PARAMETERS *pPresentationParameters,
??? IDirect3DDevice9????? **ppReturnedDeviceInterface
);
這個(gè)方法需要有效的適配器,設(shè)備類型(HAL or REF),窗口句柄,行為標(biāo)志(software/hardware vertex processing 和其它驅(qū)動(dòng)標(biāo)志),以及呈現(xiàn)參數(shù)(presentation parameters).此外,D3DPRESENT_PARAMETER結(jié)構(gòu)體還擁有大量的成員指定后備緩沖區(qū),多重采樣設(shè)定,交換效果,窗口模式,深度模版緩沖,刷新率,呈現(xiàn)間隔,以及呈現(xiàn)標(biāo)志。
為所有這些參數(shù)選擇有效的設(shè)定是具有挑戰(zhàn)性的。框架通過DXUTCreateDevice函數(shù)簡(jiǎn)化了這一選擇過程。
HRESULT DXUTCreateDevice(
??? UINT AdapterOrdinal? = D3DADAPTER_DEFAULT,
??? BOOL bWindowed?????? = TRUE,
??? INT nSuggestedWidth? = 640,
??? INT nSuggestedHeight = 480,
??? LPDXUTCALLBACKISDEVICEACCEPTABLE pCallbackIsDeviceAcceptable???? = NULL,
??? LPDXUTCALLBACKMODIFYDEVICESETTINGS pCallbackModifyDeviceSettings = NULL
);
最基本的用法是全部使用缺省參數(shù)調(diào)用:
DXUTCreateDevice();
通過這樣的調(diào)用框架使用缺省設(shè)置創(chuàng)建一個(gè)在大多數(shù)情況下可用的設(shè)備。缺省的設(shè)置如下:
Direct3D Creation Flag |
Description |
Default Value from DXUTCreateDevice |
AdapterFormat parameter of CheckDeviceFormat |
Adapter surface format. |
Desktop display mode, or D3DFMT_X8R8G8B8 if the desktop display mode is less than 32 bits. |
Adapter parameter of IDirect3D9::CreateDevice |
Display adapter ordinal. |
D3DADAPTER_DEFAULT
|
D3DPRESENT_PARAMETERS. BackBufferCount |
Number of back buffers. |
2, indicating triple buffering. |
D3DPRESENT_PARAMETERS. BackBufferFormat |
Back buffer format. |
Desktop display mode, or D3DFMT_X8R8G8B8 if the desktop display mode is less than 32 bits. |
D3DPRESENT_PARAMETERS. AutoDepthStencilFormat |
Depth format of the automatic depth-stencil surface that the device will create. |
D3DFMT_D16 if the backbuffer format is 16 bits or less, or D3DFMT_D32 otherwise. |
The DeviceType parameter of IDirect3D9::CreateDevice |
Enumerated type of the device. |
D3DDEVTYPE_HAL if available, otherwise D3DDEVTYPE_REF or failure code if neither is available. |
D3DPRESENT_PARAMETERS. MultiSampleQuality |
Quality level. |
MultiSampleQuality = 0, indicating multisampling is disabled. |
D3DPRESENT_PARAMETERS. Flags |
Presentation parameters flags. |
D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL
|
D3DPRESENT_PARAMETERS. PresentationInterval |
Presentation interval. |
D3DPRESENT_INTERVAL_IMMEDIATE for windowed mode, or D3DPRESENT_INTERVAL_DEFAULT for full-screen mode. |
D3DPRESENT_PARAMETERS. FullScreen_RefreshRateInHz |
Rate at which the display adapter refreshes the screen. |
0, indicating windowed mode. |
D3DPRESENT_PARAMETERS. BackBufferWidth and .BackBufferHeight |
Display mode resolution. |
640 x 480 pixels for windowed mode, or the desktop resolution for full-screen mode. |
D3DPRESENT_PARAMETERS. AutoDepthStencilFormat |
Stencil format of the automatic depth-stencil surface that the device will create. |
D3DFMT_D16 if the backbuffer format is 16 bits or less, or D3DFMT_D32 otherwise. |
D3DPRESENT_PARAMETERS. SwapEffect |
Swap effect. |
D3DSWAPEFFECT_DISCARD |
BehaviorFlags parameter of IDirect3D9::CreateDevice |
Vertex processing flags. |
D3DCREATE_HARDWARE_VERTEXPROCESSING if supported, otherwise D3DCREATE_SOFTWARE_VERTEXPROCESSING. |
D3DPRESENT_PARAMETERS. Windowed |
Windowed or full-screen mode. |
true, indicating windowed mode. |
hFocusWindow parameter of CreateDevice |
Handle to the created window (see Using Application Windows with DXUT). |
hWndFocus parameter of DXUTSetWindow |
D3DPRESENT_PARAMETERS. hDeviceWindow |
Handle to the device window. |
hWndDeviceFullScreen or hWndDeviceWindowed parameters of DXUTSetWindow |
D3DPRESENT_PARAMETERS. EnableAutoDepthStencil |
Depth-stencil buffer creation flag. |
true. |
應(yīng)用程序可以通過參數(shù)傳遞給CreateDevice來更多的控制設(shè)備的創(chuàng)建,這將比使用缺省的方式更好。例如,你可以通過nSuggestedWidth and nSuggestedHeight參數(shù)改變窗口的尺寸。
DXUTCreateDevice(
??? D3DADAPTER_DEFAULT,
??? false,
??? 1024,
??? 786,
??? NULL,
??? NULL,
??? NULL
);
要得到更多的控制權(quán),應(yīng)用程序可以使用這兩個(gè)可選的回調(diào)函數(shù),LPDXUTCALLBACKISDEVICEACCEPTABLE and LPDXUTCALLBACKMODIFYDEVICESETTINGS.
選擇最佳的設(shè)備設(shè)置
你可以使用IsDeviceAcceptable回調(diào)函數(shù)幫助框架為你的應(yīng)用程序選擇最佳的設(shè)備設(shè)置,就像下面的代碼:
bool CALLBACK IsDeviceAcceptable(
D3DCAPS9*???? pCaps,
D3DFORMAT???? AdapterFormat,
D3DFORMAT???? BackBufferFormat,
bool????????? bWindowed,
void*???????? pUserContext )
{
??? // TODO: return true for acceptable settings and false otherwise.
??? return true;
}
這個(gè)回調(diào)函數(shù)的模型基于LPDXUTCALLBACKISDEVICEACCEPTABLE原型(This callback function is modeled on the prototype LPDXUTCALLBACKISDEVICEACCEPTABLE),框架為每個(gè)唯一的以下5個(gè)設(shè)置的有效組合調(diào)用這個(gè)函數(shù)一次:
D3DDEVTYPE DeviceType;
UINT?????? AdapterOrdinal;
D3DFORMAT? AdapterFormat;
D3DFORMAT? BackBufferFormat;
bool?????? Windowed;
注意適配器序號(hào)和設(shè)備類型沒有直接的傳入回調(diào)函數(shù),而是分別作為D3DCAPS9結(jié)構(gòu)體的成員。
通過這個(gè)回調(diào)函數(shù),應(yīng)用程序可以拒絕任何它不支持的或不想要的組合。例如,應(yīng)用程序可以使用下面的代碼拒絕16bits的后備緩沖區(qū)格式和所有至少不能支持像素著色器PS_2_0的設(shè)備:
bool CALLBACK IsDeviceAcceptable(
??? D3DCAPS9*???? pCaps,
??? D3DFORMAT???? AdapterFormat,
??? D3DFORMAT???? BackBufferFormat,
??? bool????????? bWindowed )
{
??? if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
???? return false;
??? if( BackBufferFormat == D3DFMT_X1R5G5B5 || BackBufferFormat == D3DFMT_R5G6B5 )
??????? return false;
??? return true;
}
為每個(gè)唯一的組合調(diào)用回調(diào)函數(shù)后,框架排列剩下的可用組合,并選擇它們當(dāng)中最好的。排名較高的如下:
D3DDEVTYPE_HAL,獲取硬件加速
如果應(yīng)用程序以全屏模式顯示,框架更趨向于使用匹配桌面格式的適配器格式,這樣可以在全屏與窗口之間快速切換。例外的是,如果桌面顯示模式小于32位,框架更趨向于D3DFMT_X8R8G8B8.
匹配適配器格式的后備緩沖區(qū)格式
在選擇了這些排名高的組合后,要?jiǎng)?chuàng)建設(shè)備,行為標(biāo)志和呈現(xiàn)參數(shù)仍然是需要的。對(duì)于這些設(shè)置,Direct3D使用上面表中的缺省值。
修改可用的設(shè)備設(shè)置
應(yīng)用程序可以通過使用第二個(gè)可選的回調(diào)函數(shù)修改對(duì)框架可用的設(shè)置,這個(gè)函數(shù)是ModifyDeviceSettings:
bool CALLBACK ModifyDeviceSettings(
??? DXUTDeviceSettings* pDeviceSettings,
??? const D3DCAPS9*???? pCaps )
{
??? // TODO: Include device creation requirements here.?
??? // 返回真創(chuàng)建設(shè)備返回False保持當(dāng)前設(shè)置
??? return true;
}
這個(gè)函數(shù)是基于原型LPDXUTCALLBACKMODIFYDEVICESETTINGS的。DXUTDeviceSettings結(jié)構(gòu)體被框架定義為:
struct DXUTDeviceSettings
{
??? UINT?????? AdapterOrdinal;
??? D3DDEVTYPE DeviceType;
??? D3DFORMAT? AdapterFormat;
??? DWORD????? BehaviorFlags;
??? D3DPRESENT_PARAMETERS pp;
};
這個(gè)結(jié)構(gòu)體包含了創(chuàng)建設(shè)備所需要的所有東西,除了窗口句柄,它被假定為先前創(chuàng)建的窗口的句柄??蚣苡糜行У臄?shù)據(jù)填充這個(gè)結(jié)構(gòu)體,然后允許應(yīng)用程序通過ModifyDeviceSettings回調(diào)函數(shù)改變?cè)O(shè)備創(chuàng)建的選擇。
在這個(gè)回調(diào)函數(shù)中,應(yīng)用程序可以在DXUTDeviceSettings結(jié)構(gòu)體中改變行為標(biāo)志以及呈現(xiàn)參數(shù),乃至結(jié)構(gòu)體中任何其它的東西。如果應(yīng)用程序在回調(diào)函數(shù)中什么都不改變,設(shè)備會(huì)成功的創(chuàng)建。然而,對(duì)設(shè)備創(chuàng)建設(shè)置的任何改變都需要被設(shè)備支持,否則可能會(huì)導(dǎo)致設(shè)備創(chuàng)建失敗。
比如,如果應(yīng)用程序需要一個(gè)D3DFMT_D24S8的深度模板緩沖區(qū)格式,就必須驗(yàn)證設(shè)備是否支持,就像下面的代碼:
bool CALLBACK ModifyDeviceSettings(
??? DXUTDeviceSettings* pDeviceSettings,
??? const D3DCAPS9*???? pCaps )
{
??? IDirect3D9* pD3D = DXUTGetD3DObject();
??? if( SUCCEEDED( pD3D->CheckDeviceFormat(
???????? pDeviceSettings->AdapterOrdinal,
???????? pDeviceSettings->DeviceType,
???????? pDeviceSettings->AdapterFormat,
???????? D3DUSAGE_DEPTHSTENCIL,
???????? D3DRTYPE_SURFACE,
???????? D3DFMT_D24S8 ) ) )
??? {
???? if( SUCCEEDED( pD3D->CheckDepthStencilMatch(
???????????? pDeviceSettings->AdapterOrdinal,
???????????? pDeviceSettings->DeviceType,
???????????? pDeviceSettings->AdapterFormat,
???????????? pDeviceSettings->pp.BackBufferFormat,
???????????? D3DFMT_D24S8 ) ) )
??? ?{
???????? pDeviceSettings->pp.AutoDepthStencilFormat = D3DFMT_D24S8;
??? ?}
??? }
???
??? return true;
}
候選的方案是,回調(diào)函數(shù)可以使用框架的CD3DEnumeration 對(duì)象驗(yàn)證D3DFMT_D24S8是否被支持:
?bool CALLBACK ModifyDeviceSettings(
??? DXUTDeviceSettings* pDeviceSettings,
??? const D3DCAPS9*???? pCaps )
{
??? CD3DEnumeration *pEnum = DXUTGetEnumeration();
??? CD3DEnumDeviceSettingsCombo *pCombo;
?
??? pCombo = pEnum->GetDeviceSettingsCombo( pDeviceSettings );
?
??? if( pCombo->depthStencilFormatList.Contains( D3DFMT_D24S8 ) )
??????? pDeviceSettings->pp.AutoDepthStencilFormat = D3DFMT_D24S8;
???????
??? return true;
}
應(yīng)用程序修改了設(shè)備的設(shè)置后,框架就會(huì)用新的設(shè)置創(chuàng)建設(shè)備。
DirectX April 2005 SDK Update中的更新,ModifyDeviceSettings 回調(diào)函數(shù)返回了一個(gè)bool值。如果應(yīng)用程序返回true框架繼續(xù)正常的創(chuàng)建設(shè)備。如果返回false框架不改變?cè)O(shè)備并且保持當(dāng)前的設(shè)備,如果已經(jīng)有一個(gè)存在的話。這允許應(yīng)用程序能夠拒絕框架將設(shè)備改變到程序不能使用的請(qǐng)求。例如,在多監(jiān)視器的缺省配置下,在監(jiān)視器之間拖動(dòng)窗口會(huì)導(dǎo)致框架改變?cè)O(shè)備。然而,如果應(yīng)用程序不能使用其它的設(shè)備的話,它應(yīng)當(dāng)可以拒絕改變,并繼續(xù)使用當(dāng)前的設(shè)備。
回降到軟件頂點(diǎn)處理
如果你設(shè)置一個(gè)Direct3D設(shè)備到支持像素處理卻不支持頂點(diǎn)處理的硬件,你會(huì)因此需要改變行為標(biāo)志。為了確保正確地降到軟件頂點(diǎn)處理,謹(jǐn)防你不能拒絕一個(gè)基于IsDeviceAcceptable回調(diào)函數(shù)中頂點(diǎn)著色器版本的設(shè)備,并確保行為標(biāo)志在ModifyDeviceSettings 回調(diào)函數(shù)中被正確調(diào)整。這兒有一個(gè)例子演示怎樣做這些事情。
bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings,
??????????????????????????????????? const D3DCAPS9* pCaps )
{
??? // If device doesn't support HW T&L or doesn't support 1.1 vertex
??? // shaders in HW, then switch to SWVP.
??? if( (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) == 0 ||
???????? pCaps->VertexShaderVersion < D3DVS_VERSION(1,1) )
??? {
??????? pDeviceSettings->BehaviorFlags = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
??? }
??? else
??? {
??????? pDeviceSettings->BehaviorFlags = D3DCREATE_HARDWARE_VERTEXPROCESSING;
??? }
???
??? return true;
}
使用你自己的設(shè)備
你沒有必要依賴于框架來創(chuàng)建Direct3D設(shè)備。應(yīng)用程序自己可以創(chuàng)建設(shè)備并將他傳遞給框架使用。就像應(yīng)用程序可以覆蓋框架的window creation 設(shè)置。簡(jiǎn)單的使用你想要的設(shè)置創(chuàng)建一個(gè)設(shè)備,然后調(diào)用 DXUTSetDevice函數(shù)讓框架在你的設(shè)備上渲染。
注意:如果應(yīng)用程序創(chuàng)建了不依賴于框架的設(shè)備,那么應(yīng)用程序也必須在主循環(huán)執(zhí)行完以后親自的通過cleanup 釋放設(shè)備接口。
另請(qǐng)參閱
通過DXUT作更高級(jí)的設(shè)備選擇