8,305
社区成员
发帖
与我相关
我的任务
分享
bool CDx11Base::Initialize( HINSTANCE hInstance, HWND hwnd )
{
hInstance_ = hInstance;
hwnd_ = hwnd;
RECT dimensions;
GetClientRect(hwnd, &dimensions);
unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE,
D3D_DRIVER_TYPE_UNKNOWN
};
unsigned int totalDriverTypes = ARRAYSIZE( driverTypes );
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels );
DXGI_SWAP_CHAIN_DESC swapChainDesc; // 创建交换链结构体对象;
ZeroMemory( &swapChainDesc, sizeof(swapChainDesc)); // 初始化为0;
swapChainDesc.BufferCount = 1; // 设置swap chain中buffer的数量,不包括front buffer;
swapChainDesc.BufferDesc.Width = width; // 设置宽度;
swapChainDesc.BufferDesc.Height = height; // 设置高度;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // 扫描方式;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60; // 设置刷新频率;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; // 设置共同特性;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // 设置surface的使用方式,设置CPU对backbuffer的访问方式,这些backbuffer可以用作shader input或者render target output;
swapChainDesc.OutputWindow = hwnd; // 输出窗口句柄;
swapChainDesc.Windowed = true; // 设置是窗口模式还是全屏模式;
swapChainDesc.SampleDesc.Count = 1; // 没像素多重采样的个数,默认 1;
swapChainDesc.SampleDesc.Quality = 0; // 图像的质量等级,等级越高,性能越低,默认 0;可选范围是0到ID3D10Device::CheckMultisampleQualityLevels;
unsigned int creationFlags = 0;
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT result ;
unsigned int driver =0;
IDXGIAdapter *pAdapter_Set = NULL;
for (driver =0; driver < totalDriverTypes; ++driver)
{
// 确定设备类型;
result = D3D11CreateDeviceAndSwapChain( /*pAdapter_Set*/ 0 , driverTypes[driver], 0, creationFlags,
featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, &swapChain_I,
&d3dDevice_I, &featureLevel_, &d3dContext_I );
if( SUCCEEDED(result) )
{
driverType_ = driverTypes[driver];
break;
}
}
if( FAILED(result) )
{
DXTRACE_MSG( L"Direct3D设备错误!");
return false;
}
ID3D11Texture2D* backBufferTexture;
result = swapChain_I->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&backBufferTexture );
if( FAILED( result ) )
{
DXTRACE_MSG(L"获取交换链后台缓存失败!");
return false;
}
result = d3dDevice_I->CreateRenderTargetView( backBufferTexture, 0, &backBufferTarget_I );
if( backBufferTexture )
backBufferTexture->Release();
if( FAILED( result ) )
{
DXTRACE_MSG(L"创建渲染目标视图失败!");
return false;
}
d3dContext_I->OMSetRenderTargets( 1, &backBufferTarget_I, 0 );
// 创建视口;
D3D11_VIEWPORT viewport;
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;
d3dContext_I->RSSetViewports( 1, &viewport );
return LoadContent();
}
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
但是在debug模式下,有这句,下面D3D11CreateDeviceAndSwapChain就返回错误.
那书上为什么要这么写呢?或者要怎么设置才能用呢?#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
DEBUG模式必须要设置该程序允许debug D3D模式下才能开启。
__in UINT Flags,
__in const D3D_FEATURE_LEVEL *pFeatureLevels,
__in UINT FeatureLevels,
一个设为0,
一个设为NULL,
一个设为0,
函数就能成功创建设备,交换链,立即上下文,特征级别,并返回S_OK了!
哎! 这里没什么人气啊!