通過DXUT使用設備
DirectX設備的創建在DXUT中得到了改進。你可以讓你的應用程序直接創建設備而其它有框架提供的特征仍然可用。
創建設備
選擇最佳的設備設置
修改可用的設備設置
降為軟件頂點處理
使用你自己的設備
創建設備
典型地,你將通過標準的Direct3D方法創建設備
HRESULT CreateDevice(
??? UINT????????????????? Adapter,
??? D3DDEVTYPE??????????? DeviceType,
??? HWND????????????????? hFocusWindow,
??? DWORD???????????????? BehaviorFlags,
??? D3DPRESENT_PARAMETERS *pPresentationParameters,
??? IDirect3DDevice9????? **ppReturnedDeviceInterface
);
這個方法需要有效的適配器,設備類型(HAL or REF),窗口句柄,行為標志(software/hardware vertex processing 和其它驅動標志),以及呈現參數(presentation parameters).此外,D3DPRESENT_PARAMETER結構體還擁有大量的成員指定后備緩沖區,多重采樣設定,交換效果,窗口模式,深度模版緩沖,刷新率,呈現間隔,以及呈現標志。
為所有這些參數選擇有效的設定是具有挑戰性的。框架通過DXUTCreateDevice函數簡化了這一選擇過程。
HRESULT DXUTCreateDevice(
??? UINT AdapterOrdinal? = D3DADAPTER_DEFAULT,
??? BOOL bWindowed?????? = TRUE,
??? INT nSuggestedWidth? = 640,
??? INT nSuggestedHeight = 480,
??? LPDXUTCALLBACKISDEVICEACCEPTABLE pCallbackIsDeviceAcceptable???? = NULL,
??? LPDXUTCALLBACKMODIFYDEVICESETTINGS pCallbackModifyDeviceSettings = NULL
);
最基本的用法是全部使用缺省參數調用:
DXUTCreateDevice();
通過這樣的調用框架使用缺省設置創建一個在大多數情況下可用的設備。缺省的設置如下:
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. |
應用程序可以通過參數傳遞給CreateDevice來更多的控制設備的創建,這將比使用缺省的方式更好。例如,你可以通過nSuggestedWidth and nSuggestedHeight參數改變窗口的尺寸。
DXUTCreateDevice(
??? D3DADAPTER_DEFAULT,
??? false,
??? 1024,
??? 786,
??? NULL,
??? NULL,
??? NULL
);
要得到更多的控制權,應用程序可以使用這兩個可選的回調函數,LPDXUTCALLBACKISDEVICEACCEPTABLE and LPDXUTCALLBACKMODIFYDEVICESETTINGS.
選擇最佳的設備設置
你可以使用IsDeviceAcceptable回調函數幫助框架為你的應用程序選擇最佳的設備設置,就像下面的代碼:
bool CALLBACK IsDeviceAcceptable(
D3DCAPS9*???? pCaps,
D3DFORMAT???? AdapterFormat,
D3DFORMAT???? BackBufferFormat,
bool????????? bWindowed,
void*???????? pUserContext )
{
??? // TODO: return true for acceptable settings and false otherwise.
??? return true;
}
這個回調函數的模型基于LPDXUTCALLBACKISDEVICEACCEPTABLE原型(This callback function is modeled on the prototype LPDXUTCALLBACKISDEVICEACCEPTABLE),框架為每個唯一的以下5個設置的有效組合調用這個函數一次:
D3DDEVTYPE DeviceType;
UINT?????? AdapterOrdinal;
D3DFORMAT? AdapterFormat;
D3DFORMAT? BackBufferFormat;
bool?????? Windowed;
注意適配器序號和設備類型沒有直接的傳入回調函數,而是分別作為D3DCAPS9結構體的成員。
通過這個回調函數,應用程序可以拒絕任何它不支持的或不想要的組合。例如,應用程序可以使用下面的代碼拒絕16bits的后備緩沖區格式和所有至少不能支持像素著色器PS_2_0的設備:
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;
}
為每個唯一的組合調用回調函數后,框架排列剩下的可用組合,并選擇它們當中最好的。排名較高的如下:
D3DDEVTYPE_HAL,獲取硬件加速
如果應用程序以全屏模式顯示,框架更趨向于使用匹配桌面格式的適配器格式,這樣可以在全屏與窗口之間快速切換。例外的是,如果桌面顯示模式小于32位,框架更趨向于D3DFMT_X8R8G8B8.
匹配適配器格式的后備緩沖區格式
在選擇了這些排名高的組合后,要創建設備,行為標志和呈現參數仍然是需要的。對于這些設置,Direct3D使用上面表中的缺省值。
修改可用的設備設置
應用程序可以通過使用第二個可選的回調函數修改對框架可用的設置,這個函數是ModifyDeviceSettings:
bool CALLBACK ModifyDeviceSettings(
??? DXUTDeviceSettings* pDeviceSettings,
??? const D3DCAPS9*???? pCaps )
{
??? // TODO: Include device creation requirements here.?
??? // 返回真創建設備返回False保持當前設置
??? return true;
}
這個函數是基于原型LPDXUTCALLBACKMODIFYDEVICESETTINGS的。DXUTDeviceSettings結構體被框架定義為:
struct DXUTDeviceSettings
{
??? UINT?????? AdapterOrdinal;
??? D3DDEVTYPE DeviceType;
??? D3DFORMAT? AdapterFormat;
??? DWORD????? BehaviorFlags;
??? D3DPRESENT_PARAMETERS pp;
};
這個結構體包含了創建設備所需要的所有東西,除了窗口句柄,它被假定為先前創建的窗口的句柄。框架用有效的數據填充這個結構體,然后允許應用程序通過ModifyDeviceSettings回調函數改變設備創建的選擇。
在這個回調函數中,應用程序可以在DXUTDeviceSettings結構體中改變行為標志以及呈現參數,乃至結構體中任何其它的東西。如果應用程序在回調函數中什么都不改變,設備會成功的創建。然而,對設備創建設置的任何改變都需要被設備支持,否則可能會導致設備創建失敗。
比如,如果應用程序需要一個D3DFMT_D24S8的深度模板緩沖區格式,就必須驗證設備是否支持,就像下面的代碼:
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;
}
候選的方案是,回調函數可以使用框架的CD3DEnumeration 對象驗證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;
}
應用程序修改了設備的設置后,框架就會用新的設置創建設備。
DirectX April 2005 SDK Update中的更新,ModifyDeviceSettings 回調函數返回了一個bool值。如果應用程序返回true框架繼續正常的創建設備。如果返回false框架不改變設備并且保持當前的設備,如果已經有一個存在的話。這允許應用程序能夠拒絕框架將設備改變到程序不能使用的請求。例如,在多監視器的缺省配置下,在監視器之間拖動窗口會導致框架改變設備。然而,如果應用程序不能使用其它的設備的話,它應當可以拒絕改變,并繼續使用當前的設備。
回降到軟件頂點處理
如果你設置一個Direct3D設備到支持像素處理卻不支持頂點處理的硬件,你會因此需要改變行為標志。為了確保正確地降到軟件頂點處理,謹防你不能拒絕一個基于IsDeviceAcceptable回調函數中頂點著色器版本的設備,并確保行為標志在ModifyDeviceSettings 回調函數中被正確調整。這兒有一個例子演示怎樣做這些事情。
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;
}
使用你自己的設備
你沒有必要依賴于框架來創建Direct3D設備。應用程序自己可以創建設備并將他傳遞給框架使用。就像應用程序可以覆蓋框架的window creation 設置。簡單的使用你想要的設置創建一個設備,然后調用 DXUTSetDevice函數讓框架在你的設備上渲染。
注意:如果應用程序創建了不依賴于框架的設備,那么應用程序也必須在主循環執行完以后親自的通過cleanup 釋放設備接口。
另請參閱
通過DXUT作更高級的設備選擇