本文介绍了DirectX 11向后兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过的代码示例学习DirectX。我在代码集1,第6章框代码示例。当我尝试在DirectX 11兼容机中编译第6章的代码时,它运行良好,但是当我在DirectX 11机器中使用着色器5.0在 FEATURE LEVEL

  //d3App.cpp。 
//在InitDirect3D()方法中

D3D_FEATURE_LEVEL a [] = {D3D_FEATURE_LEVEL_10_0};
HRESULT hr = D3D11CreateDevice(
0,//默认适配器
md3dDriverType,
0,//没有软件设备
createDeviceFlags,
a,1, / default feature level array
D3D11_SDK_VERSION,
& md3dDevice,
& featureLevel,
& md3dImmediateContext);

if(FAILED(hr))
{
MessageBox(0,LD3D11CreateDevice Failed。,0,0)
return false;
}

// if(featureLevel!= D3D_FEATURE_LEVEL_11_0)
// {
// MessageBox(0,LDirect3D Feature Level 11 unsupported。 ,0);
// return false;
//}

然后在box.cpp

  //Box.cpp 
//在BuildFX()方法中

HRESULT hr = D3DX11CompileFromFile(LFX / color。 fx,0,0,0,fx_5_0,shaderFlags,
0,0& compiledShader,& compilationMsgs,0);

它在

上出现错误

  //Box.cpp。行172 
HR(mSwapChain-> Present(0,0));



如果我改变着色器为 fx_4_0 code>特征级别,在DirectX 10和DirectX 10机器上交换链创建时失败。



此外,在DirectX 10机器中,如果我使用着色器 fx_5_0 在DirectX 10功能级别运行它,



是否可以使用DirectX 11 API构建应用程序,这些应用程序与只支持较低版本DirectX的机器兼容?如果是儿子,directX自动处理它,还是我们自己做呢?

解决方案

5仅由D3D_FEATURE_LEVEL_11_0及以上版本支持。所以你希望下降到DX 10与shader fx_5_0将无法工作。但是使用DX 11构建应用程序,但支持较低版本是非常常见的。



包括您要支持的每个版本。

  static const D3D_FEATURE_LEVEL_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
};

注意:目前必须显式设置D3D_FEATURE_LEVEL_11_1(如果需要,并尝试在计算机上使用该功能



在使用调试信息进行编译时,包括调试标志是非常重要的,这将是一个巨大的帮助,当确定

  unsigned flags = [YOUR FLAGS]; 

#ifdef _DEBUG
flags | = D3D11_CREATE_DEVICE_DEBUG;
#endif

$因为你的项目变得更加复杂,ComPtr<>将有效地和正确地处理资源。

  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()));

但是,由于着色器模型5仅受D3D_FEATURE_LEVEL_11_0及更高版本支持,请确保正确的文件格式。 p>

编辑:
另外请注意,自从您上面引用的图书发布以来,最近的D3D开发已经改变了一个合理的数额。例如,D3DX11CreateTextureFromFile已被弃用,并且有一些主题可用于迁移XNA代码。



我希望这会有帮助。


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?

解决方案

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.

这篇关于DirectX 11向后兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 06:03