D3D12CreateDevice引发_com_error

时间:2019-02-04 11:52:27

标签: c++ visual-c++ exception-handling directx directx-12

即使指定了适配器,以下代码中的

D3D12CreateDevice也会引发_com_error异常:

#include "d3dx12.h"

int main() {
    ID3D12Device* device;
    D3D12CreateDevice(nullptr, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device));
}

在test.exe中的0x00007FFB1E315549处引发异常:Microsoft C ++异常:内存位置0x0000002906BC90E0出现_com_error。

但是Microsoft的this示例程序没有从D3D12CreateDevice抛出_com_error。 D3D12CreateDevice行为很奇怪,因为如果我将HelloTriangle文件夹重命名为HelloTriangle2,则会再次出现异常。

我从D3D12CreateDevice检查了HRESULT,它返回0(零),表示成功。但是我仍然收到_com_error。我的适配器通过硬件支持DX12。

1 个答案:

答案 0 :(得分:0)

运行时可以在内部使用异常,只要它们不会从仍然正确的函数中传播出去即可。如果从该异常继续,则可能会返回。您无需检查HRESULT中的D3D12CreateDevice,而应该查看返回的内容。

主要区别在于示例代码使用的是明确枚举的适配器,该适配器经过验证可支持Direct3D 12,而您的代码则依赖于默认设备。

// Helper function for acquiring the first available hardware adapter that supports Direct3D 12.
// If no such adapter can be found, *ppAdapter will be set to nullptr.
_Use_decl_annotations_
void DXSample::GetHardwareAdapter(IDXGIFactory2* pFactory, IDXGIAdapter1** ppAdapter)
{
    ComPtr<IDXGIAdapter1> adapter;
    *ppAdapter = nullptr;

    for (UINT adapterIndex = 0; DXGI_ERROR_NOT_FOUND != pFactory->EnumAdapters1(adapterIndex, &adapter); ++adapterIndex)
    {
        DXGI_ADAPTER_DESC1 desc;
        adapter->GetDesc1(&desc);

        if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)
        {
            // Don't select the Basic Render Driver adapter.
            // If you want a software adapter, pass in "/warp" on the command line.
            continue;
        }

        // Check to see if the adapter supports Direct3D 12, but don't create the
        // actual device yet.
        if (SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, _uuidof(ID3D12Device), nullptr)))
        {
            break;
        }
    }

    *ppAdapter = adapter.Detach();
}

如果您的系统没有支持Direct3D 12的设备,则示例代码将使用您的代码也没有执行的WARP软件设备。

因此,您的默认视频设备可能不支持Direct3D 12,并且系统上甚至可能没有任何支持它的视频设备。也就是说,在Direct3D运行时中引发的C ++异常仍会触发调试器中断,因此您必须继续执行它们。

有关创建Direct3D 12设备的详细演练,请参见Anatomy of Direct3D 12 Create Device

  

您可能还想利用DeviceResources处理创建设备的所有逻辑。