问题
I am learning DirectX by following code examples from here. I am at Code Set 1, chapter 6 Box code example. When I try to compile code of chapter 6 in DirectX 11 compatible machine, it runs fine but when I run that code in FEATURE LEVEL
of directX 10 in DirectX 11 machine with shader 5.0
//d3App.cpp.
//In InitDirect3D() method
D3D_FEATURE_LEVEL a[]={D3D_FEATURE_LEVEL_10_0};
HRESULT hr = D3D11CreateDevice(
0, // default adapter
md3dDriverType,
0, // no software device
createDeviceFlags,
a, 1, // default feature level array
D3D11_SDK_VERSION,
&md3dDevice,
&featureLevel,
&md3dImmediateContext);
if( FAILED(hr) )
{
MessageBox(0, L"D3D11CreateDevice Failed.", 0, 0);
return false;
}
//if( featureLevel != D3D_FEATURE_LEVEL_11_0 )
//{
// MessageBox(0, L"Direct3D Feature Level 11 unsupported.", 0, 0);
// return false;
//}
Then in box.cpp
//Box.cpp
//In BuildFX() method
HRESULT hr = D3DX11CompileFromFile(L"FX/color.fx", 0, 0, 0, "fx_5_0", shaderFlags,
0, 0, &compiledShader, &compilationMsgs, 0);
It gave error at
//Box.cpp. Line 172
HR(mSwapChain->Present(0, 0));
If I changed shader to fx_4_0
with feature level
of DirectX 10, it fails at swap chain creation
in both DirectX 11 and DirectX 10 machines.
Moreover, in DirectX 10 machine, if I run it at DirectX 10 feature level with shader fx_5_0
, it don't gave an error but it also does not do anything.
Is it possible to build applications using DirectX 11 API which are compatible with machine which only support lower versions of DirectX? If son, does directX handle it automatically or do we have to do it by ourselves?
回答1:
As you probably know, shader model 5 is only supported by D3D_FEATURE_LEVEL_11_0 and up. So you would expect that dropping down to DX 10 with shader fx_5_0 will not work. However building applications using DX 11, but supporting lower versions is very common.
Include each version you wish to support.
static const D3D_FEATURE_LEVEL levels[] = {
D3D_FEATURE_LEVEL_11_1,
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
};
NOTE: D3D_FEATURE_LEVEL_11_1 currently must be explicitly set (if desired, and attempting to use that feature on a computer that doesn't have DX 11_1 will fail with E_INVALIDARG.
It is important you include the debug flag when compiling with the debug information. It will be a tremendous help when determining what is happening by simply checking the output window. A great deal of warnings and specific error information is sent there.
unsigned flags = [YOUR FLAGS];
#ifdef _DEBUG
flags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
Also, as a side note, you may want to consider using the windows runtime ComPtr<>. As your projects become more complex, ComPtr<> will efficiently and properly handle resources.
Microsoft::WRL::ComPtr<ID3D11Device> d3dDevice = nullptr;
Microsoft::WRL::ComPtr<ID3D11DeviceContext> deviceContext = nullptr;
D3D_FEATURE_LEVEL selectedFeatureLevel;
HRESULT hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, flags,
levels, ARRAYSIZE(levels), D3D11_SDK_VERSION,
d3dDevice.GetAddressOf(), &selectedFeatureLevel,
deviceContext.GetAddressOf()));
However, since shader model 5 is only supported by D3D_FEATURE_LEVEL_11_0 and up, ensure proper file formats.
EDIT: Also note that a fair amount has changed in recent D3D development since the book you referenced above was published. For instance 'D3DX11CreateTextureFromFile' has been deprecated, and there are topics available for migrating XNA code.
I hope this helps.
来源:https://stackoverflow.com/questions/26018799/directx-11-backward-compatibility