C++ 未处理的异常DirectX11.1渲染二维图形

C++ 未处理的异常DirectX11.1渲染二维图形,c++,exception,directx,render,unhandled,C++,Exception,Directx,Render,Unhandled,我正在Visual studio 2013中使用DirectX11.1,开发一个Windows 8应用商店应用程序 几天来,我一直在尝试使用DirectX创建2D UI,正如我所想的那样,我遇到了一个未经处理的异常,我似乎无法摆脱 “First.exe中0x002E2901处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。” 更新:似乎在“d2dFactory->CreateDevice(dxgiDevice.Get(),&dev2d);”之后dev2d显示为空 更新:

我正在Visual studio 2013中使用DirectX11.1,开发一个Windows 8应用商店应用程序

几天来,我一直在尝试使用DirectX创建2D UI,正如我所想的那样,我遇到了一个未经处理的异常,我似乎无法摆脱

“First.exe中0x002E2901处的未处理异常:0xC0000005:访问冲突读取位置0x00000000。”

更新:似乎在“d2dFactory->CreateDevice(dxgiDevice.Get(),&dev2d);”之后dev2d显示为空

更新:使用HRESULT创建设备方法时出现错误“E_INVALIDARG”

(我一直在跟踪)

下面是我在头文件和.cpp文件中的代码。在创建ID2D1DeviceContex之前,我没有包含任何内容,因为这是错误发生的地方(只有当我包含它时,才会发生异常)

h


事实证明,我没有创建与Direct2D兼容的D3D11设备。下面的代码修复了它。多亏了Trillian的调试帮助,这非常有帮助

// This flag adds support for surfaces with a different color channel ordering than the API default.
    // You need it for compatibility with Direct2D.
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

    // This array defines the set of DirectX hardware feature levels this app  supports.
    // The ordering is important and you should  preserve it.
    // Don't forget to declare your app's minimum required feature level in its
    // description.  All apps are assumed to support 9.1 unless otherwise stated.
    D3D_FEATURE_LEVEL featureLevels[] =
    {
        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 m_featureLevel;

    // Create 3D device and device context objects
    D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        creationFlags,
        featureLevels,
        ARRAYSIZE(featureLevels),
        D3D11_SDK_VERSION,
        &dev11,
        &m_featureLevel,
        &devcon11);

事实证明,我没有创建与Direct2D兼容的D3D11设备。下面的代码修复了它。多亏了Trillian的调试帮助,这非常有帮助

// This flag adds support for surfaces with a different color channel ordering than the API default.
    // You need it for compatibility with Direct2D.
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

    // This array defines the set of DirectX hardware feature levels this app  supports.
    // The ordering is important and you should  preserve it.
    // Don't forget to declare your app's minimum required feature level in its
    // description.  All apps are assumed to support 9.1 unless otherwise stated.
    D3D_FEATURE_LEVEL featureLevels[] =
    {
        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 m_featureLevel;

    // Create 3D device and device context objects
    D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        creationFlags,
        featureLevels,
        ARRAYSIZE(featureLevels),
        D3D11_SDK_VERSION,
        &dev11,
        &m_featureLevel,
        &devcon11);

您是否尝试过使用
D3D11\u CREATE\u DEVICE\u DEBUG
和Direct3D调试运行时?它们可能能够输出更有意义的诊断。因此,我认为在进行一些调试后,问题一定与调用“d2dFactory->CreateDevice(dxgiDevice.Get(),&dev2d);”后dev2d为空有关(至少看起来是这样)。因此调用dev2d->CreateDevice时会出现故障。至于为什么我还不确定,我对DirectX比较陌生。首先要尝试的是检查
CreateDevice
返回的
HRESULT
。空指针值应该伴随一个错误返回代码。好的,因此错误是“E_INVALIDARG一个或多个参数是无效的”(为此欢呼,我意识到这似乎很明显,尽管我仍在处理DirectX和Windows API)来自:“向返回函数传递了无效参数“您是否尝试过使用
D3D11\u CREATE\u DEVICE\u DEBUG
和Direct3D调试运行时?它们可能能够输出更有意义的诊断。因此,我认为在进行一些调试后,问题一定与调用'd2dFactory->CreateDevice(dxgiDevice.Get(),&dev2d);'后dev2d为Null有关(至少看起来是这样的情况)。因此调用dev2d->CreateDevice时会出现故障。至于为什么我还不确定,我对DirectX比较陌生。首先要尝试的是检查
CreateDevice
返回的
HRESULT
。空指针值应该伴随一个错误返回代码。好的,因此错误是“E_INVALIDARG一个或多个参数无效”(为此欢呼,我意识到这似乎很明显,尽管我仍在处理DirectX和Windows API)来自:“向返回函数传递了无效参数”
// set up the swap chain description
    DXGI_SWAP_CHAIN_DESC1 scd = { 0 };
    scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;    // how the swap chain should be used
    scd.BufferCount = 2;                                  // a front buffer and a back buffer
    scd.Format = DXGI_FORMAT_B8G8R8A8_UNORM;              // the most common swap chain format
    scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;    // the recommended flip mode
    scd.SampleDesc.Count = 1;                             // disable anti-aliasing

    CoreWindow^ Window = CoreWindow::GetForCurrentThread();    // get the window pointer

    // create the swap chain
    dxgiFactory->CreateSwapChainForCoreWindow(
        dev.Get(),                                  // address of the device
        reinterpret_cast<IUnknown*>(Window),        // address of the window
        &scd,                                       // address of the swap chain description
        nullptr,                                    // advanced
        &swapchain);                                // address of the new swap chain pointer

    // get a pointer directly to the back buffer
    ComPtr<ID3D11Texture2D> backbuffer;
    swapchain->GetBuffer(0, __uuidof(ID3D11Texture2D), &backbuffer);

    // create a render target pointing to the back buffer
    dev->CreateRenderTargetView(backbuffer.Get(), nullptr, &rendertarget);

    //Set up Direct2D render target bitmap, linking it to the swapchain
    D2D1_PIXEL_FORMAT pixelFormat = { DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED };

    D2D1_BITMAP_PROPERTIES1 bitmapProperties =
    {   pixelFormat,
        0,
        0,
        D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
        0,
    };

    //Direct2D needs the dxgi version of the backbuffer surface pointer
    swapchain->GetBuffer(0, IID_PPV_ARGS(&dxgiBackBuffer));

    //Get a D2D surface from the DXGI back buffer to use as the D2D render target.
    devcon2d->CreateBitmapFromDxgiSurface(
        dxgiBackBuffer.Get(),
        &bitmapProperties,
        &targetBitmap
        );

    //Now we can set the Direct2D render target
    devcon2d->SetTarget(targetBitmap.Get());



    //Create a brush for 2D
    devcon2d->CreateSolidColorBrush(
        D2D1::ColorF(D2D1::ColorF::Black),
        &pBlackBrush);

    // set the viewport
    D3D11_VIEWPORT viewport = { 0 };

    viewport.TopLeftX = 0;
    viewport.TopLeftY = 0;
    viewport.Width = Window->Bounds.Width;
    viewport.Height = Window->Bounds.Height;

    devcon->RSSetViewports(1, &viewport);
'First.exe' (Win32): Loaded 'C:\Users\Alec\Documents\Visual Studio 2013\Projects\First\Debug\First\AppX\First.exe'. Symbols loaded.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\d2d1.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\d3d11.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dxgi.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ole32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\DWrite.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Program Files\WindowsApps\Microsoft.VCLibs.120.00.Debug_12.0.21005.1_x86__8wekyb3d8bbwe\vccorlib120d_app.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Program Files\WindowsApps\Microsoft.VCLibs.120.00.Debug_12.0.21005.1_x86__8wekyb3d8bbwe\msvcp120d_app.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Program Files\WindowsApps\Microsoft.VCLibs.120.00.Debug_12.0.21005.1_x86__8wekyb3d8bbwe\msvcr120d_app.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\oleaut32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel.appcore.dll'. Cannot find or open the PDB file.
The thread 0x22c0 has exited with code 0 (0x0).
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\twinapi.appcore.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\WinTypes.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\SHCore.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptsp.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rsaenh.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcrypt.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\actxprxy.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\MrmCoreR.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\BCP47Langs.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\Windows.UI.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ninput.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\igd10umd32.dll'. Cannot find or open the PDB file.
'First.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
// This flag adds support for surfaces with a different color channel ordering than the API default.
    // You need it for compatibility with Direct2D.
    UINT creationFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;

    // This array defines the set of DirectX hardware feature levels this app  supports.
    // The ordering is important and you should  preserve it.
    // Don't forget to declare your app's minimum required feature level in its
    // description.  All apps are assumed to support 9.1 unless otherwise stated.
    D3D_FEATURE_LEVEL featureLevels[] =
    {
        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 m_featureLevel;

    // Create 3D device and device context objects
    D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        creationFlags,
        featureLevels,
        ARRAYSIZE(featureLevels),
        D3D11_SDK_VERSION,
        &dev11,
        &m_featureLevel,
        &devcon11);