dxut 框架依次执行IsD3D9DeviceAcceptable(), ModifyDeviceSettings(), OnD3D9CreateDevice(),OnD3DResetDevice() 四个函数,接下来看看MSDN对以上几个函数的评述:
1.Customize Device Settings in the IsDeviceAcceptable callback
To help DXUT choose the best device settings, use the IsDeviceAcceptable callback function (either DXUTSetCallbackD3D9DeviceAcceptable or DXUTSetCallbackD3D10DeviceAcceptable) to filter out any settings that are not appropriate for your application.Within the callback function, the application can reject any combination that it does not support or want by returning false in the callback. For instance, this example rejects 16-bit back-buffer formats and devices that do not support at least pixel shader model 2.
bool CALLBACK IsD3D9DeviceAcceptable(
D3DCAPS9* pCaps,
D3DFORMAT AdapterFormat,
D3DFORMAT BackBufferFormat,
bool bWindowed
)
{
if( BackBufferFormat == D3DFMT_X1R5G5B5 ||
BackBufferFormat == D3DFMT_R5G6B5
)
return
false;
if( pCaps->PixelShaderVersion <
D3DPS_VERSION(2,0) )
return
false;
return true;
}
2. Change Device Settings in the ModifyDeviceSettings callback
After choosing the best settings, there is one more callback that DXUT uses which can be used to modify the settings. This is the ModifyDeviceSettings callback function. This callback function takes a DXUTDeviceSettings structure. This structure is a union of DXUTD3D10DeviceSettings and DXUTD3D9DeviceSettings. It contains everything needed to create a device. The ver field of the structure indicates whether the settings structure is for a Direct3D 9 or Direct3D 10 device.
3. onD3D9CreateDevice()
创建设备之后,接下来调用OnD3D9CreateDevice() callback, 在此函数中创建内存池类型为D3DPOOL_MANAGED 或者 D3DPOOL_SYSTEMMEM资源。以类型D3DPOOL_MANAGED创建的设备由Direct3D系统代替管理(位于显存或系统内存中),以类型D3DPOOL_SYSTEMMEM创建的设备位于系统内存中,在程序退出之前,这些资源常驻内存,不会出现设备丢失的现象。也就是说,以这两种内存类型创建的资源不需要程序员进行管理。
4.OnD3D9ResetDevice()
DXUT框架在调用OnD3D9CreateDevice()回调函数之后,将调用OnD3D9ResetDevice()回调函数。我们可在函数OnD3D9ResetDevice()中创建所有内存池类型为D3DPOOL_DEFAULT的资源,这一类资源将尽可能存放在显存中,这样可以提高程序的运行速度。但是,这类资源在程序运行时会出现设备丢失的现象,因此需要程序员自己管理。在设备丢失时释放它的内存,当设备恢复时重新为它分配内存。此外,观察变换矩阵和投影变换矩阵以及在整个程序运行期间保持不变的渲染状态通常也在该回调函数中设置。
1.Customize Device Settings in the IsDeviceAcceptable callback
To help DXUT choose the best device settings, use the IsDeviceAcceptable callback function (either DXUTSetCallbackD3D9DeviceAcceptable or DXUTSetCallbackD3D10DeviceAcceptable) to filter out any settings that are not appropriate for your application.Within the callback function, the application can reject any combination that it does not support or want by returning false in the callback. For instance, this example rejects 16-bit back-buffer formats and devices that do not support at least pixel shader model 2.
bool CALLBACK IsD3D9DeviceAcceptable(
D3DCAPS9* pCaps,
D3DFORMAT AdapterFormat,
D3DFORMAT BackBufferFormat,
bool bWindowed
)
{
if( BackBufferFormat == D3DFMT_X1R5G5B5 ||
BackBufferFormat == D3DFMT_R5G6B5
)
return
false;
if( pCaps->PixelShaderVersion <
D3DPS_VERSION(2,0) )
return
false;
return true;
}
2. Change Device Settings in the ModifyDeviceSettings callback
After choosing the best settings, there is one more callback that DXUT uses which can be used to modify the settings. This is the ModifyDeviceSettings callback function. This callback function takes a DXUTDeviceSettings structure. This structure is a union of DXUTD3D10DeviceSettings and DXUTD3D9DeviceSettings. It contains everything needed to create a device. The ver field of the structure indicates whether the settings structure is for a Direct3D 9 or Direct3D 10 device.
3. onD3D9CreateDevice()
创建设备之后,接下来调用OnD3D9CreateDevice() callback, 在此函数中创建内存池类型为D3DPOOL_MANAGED 或者 D3DPOOL_SYSTEMMEM资源。以类型D3DPOOL_MANAGED创建的设备由Direct3D系统代替管理(位于显存或系统内存中),以类型D3DPOOL_SYSTEMMEM创建的设备位于系统内存中,在程序退出之前,这些资源常驻内存,不会出现设备丢失的现象。也就是说,以这两种内存类型创建的资源不需要程序员进行管理。
4.OnD3D9ResetDevice()
DXUT框架在调用OnD3D9CreateDevice()回调函数之后,将调用OnD3D9ResetDevice()回调函数。我们可在函数OnD3D9ResetDevice()中创建所有内存池类型为D3DPOOL_DEFAULT的资源,这一类资源将尽可能存放在显存中,这样可以提高程序的运行速度。但是,这类资源在程序运行时会出现设备丢失的现象,因此需要程序员自己管理。在设备丢失时释放它的内存,当设备恢复时重新为它分配内存。此外,观察变换矩阵和投影变换矩阵以及在整个程序运行期间保持不变的渲染状态通常也在该回调函数中设置。