调试 DirectX 代码

Debug DirectX code

我一直在按照一些教程学习 DirectX,但每次我自己输入内容时,DirectX 都无法工作。这是我经过数小时的研究仍无法修复的最新错误的示例:

     //Header.h
     static HWND hWnd;
    static IDXGISwapChain* swapChain;
    static ID3D11Device* dev;
    static ID3D11DeviceContext* devCon;
    static ID3D11RenderTargetView* renderTarget;

    //DirectX.cpp
    bool InitD3D11(HINSTANCE hInst)
{
    HRESULT hr;
    DXGI_MODE_DESC bufferDesc;
    ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));
    bufferDesc.Width = 800;
    bufferDesc.Height = 600;
    bufferDesc.RefreshRate.Numerator = 60;
    bufferDesc.RefreshRate.Denominator = 1;
    bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
    bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

    DXGI_SWAP_CHAIN_DESC swapChainDesc;
    ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
    swapChainDesc.BufferDesc = bufferDesc;
    swapChainDesc.SampleDesc.Count = 1;
    swapChainDesc.SampleDesc.Quality = 0;
    swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
    swapChainDesc.BufferCount = 1;
    swapChainDesc.OutputWindow = hWnd;
    swapChainDesc.Windowed = true;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

    hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &swapChainDesc, &swapChain, &dev, NULL, &devCon);

    ID3D11Texture2D* backBuffer;
    hr = swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&backBuffer);
    hr = dev->CreateRenderTargetView(backBuffer, NULL, &renderTarget);
    backBuffer->Release();
    devCon->OMSetRenderTargets(1, &renderTarget, NULL);
    return true;
}
//This is not the full code - I linked directx and lib files and stuff like that.
//If I copy and paste code from tutorials, everything runs fine

每当我调用 InitD3d11 时,我都会收到一条错误消息,指出交换链是一个 NULL 指针。我假设 bufferDesc and/or swapChainDesc 有一些无效数据,但编译器不能给我任何线索是什么导致了错误。有人可以告诉我如何跟踪和修复这样的错误吗?谢谢

您没有检查 HRESULT 值,因此您缺少所有错误处理。对于 COM 编程,即使使用 Direct3D,您 也必须 检查每个可能 return 失败的方法的 HRESULT。如果可以安全地忽略 return 值,则 return 改为 void

在老式 C/C++ 程序中检查 HRESULT 值是使用 FAILEDSUCCEEDED 宏完成的。

hr = D3D11CreateDeviceAndSwapChain( /* ... */ *);
if (FAILED(hr))
    return false;

在大多数情况下,失败的 HRESULT 被视为 'fast-fail' 或致命错误。换句话说,如果调用失败,程序将无法继续。

在其他情况下,可以使用不同的选项进行特殊情况处理以从错误中恢复。有关详细示例,请参阅 Anatomy of Direct3D 11 Create Device.

In older Microsoft samples based on the legacy DXUT framework, the error handling was done with macros like V or V_RETURN which did some tracing or tracing & fatal exit.

在现代 C++ 示例中,我们实际上使用了一个助手 DX::ThrowIfFailed,它在 fast-fail 场景的失败 HRESULT 上生成 C++ 异常。这使代码更加精简和可读:

DX::ThrowIfFailed(
    D3D11CreateDeviceAndSwapChain( /* ... */ *)
);

函数本身定义为:

#include <exception>

namespace DX
{
    inline void ThrowIfFailed(HRESULT hr)
    {
        if (FAILED(hr))
        {
            // Set a breakpoint on this line to catch DirectX API errors
            throw std::exception();
        }
    }
}

您的程序应该使用默认 Visual Studio 模板中已经存在的 /EHsc 进行编译。有关详细信息,请参阅 this topic page

From your code snippet above, you are following a pretty old-school tutorial. A lot has changed even for DirectX 11 development and most of those older tutorials are going to lead to confusion. As you are new to DirectX, I recommend you take a look at the DirectX Tool Kit and the tutorials there first. You can then come back to the older tutorials with a better understanding of 'modern' Direct3D and be able to extract more relevant information from the old stuff.

检查完所有 HRESULTS 之后,您应该做的下一件事是启用 Direct3D 调试层,它在输出中提供额外的调试信息 window.

DWORD createDeviceFlags = 0;
#ifdef _DEBUG
    createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

hr = D3D11CreateDeviceAndSwapChain(
    nullptr, D3D_DRIVER_TYPE_HARDWARE,
    nullptr, createDeviceFlags, nullptr,
    0, D3D11_SDK_VERSION,
    &swapChainDesc, &swapChain, &dev, nullptr, &devCon);

You'll note I'm using the C++11 nullptr which is supported on Visual C++ 2010 or later instead of the old-school NULL. That's because it is typed. You were using NULL in two places in your version where the parameter wasn't actually a pointer, it's a number.

有了这个,你会得到很多关于基本错误的反馈,这将有助于诊断在你的特定情况下交换链未能创建的原因。我怀疑这是因为您提供的一些值仅对“独占全屏模式”有意义,对 windowed 模式 (bufferDesc.RefreshRate) 没有意义。相反,尝试:

DXGI_SWAP_CHAIN_DESC swapChainDesc = {};
swapChainDesc.BufferDesc.Width = 800; 
swapChainDesc.BufferDesc.Height = 600; 
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 
swapChainDesc.SampleDesc.Count = 1; 
swapChainDesc.SampleDesc.Quality = 0; 
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; 
swapChainDesc.BufferCount = 1; 
swapChainDesc.OutputWindow = hWnd; 
swapChainDesc.Windowed = TRUE; 
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; 

This works on VS 2013 or VS 2015 which will initialize the structure to 0 with the ={}; syntax. On older compilers, you'll need to use ZeroMemory like you did in your old-school code above.

请注意,在缺少仅供开发人员使用的调试设备的系统上使用 D3D11_CREATE_DEVICE_DEBUG 会失败。根据您使用的 Windows 版本的不同,有关从何处获取调试设备层的详细信息会有所不同。请参阅 Direct3D SDK Debug Layer Tricks,底部有一个小摘要 table。

还有,在存在未初始化变量的情况下调试是一个巨大的痛苦。未初始化的指针尤其会浪费大量时间。如果您执行以下操作将会有所帮助:

static HWND hWnd = nullptr;
static IDXGISwapChain* swapChain = nullptr;
static ID3D11Device* dev = nullptr;
static ID3D11DeviceContext* devCon = nullptr;
static ID3D11RenderTargetView* renderTarget = nullptr;

更好的是,您应该使用像 Microsoft::WRL::ComPtr 这样的 C++ 智能指针,而不是为您的 COM 对象使用原始指针。有关详细信息,请参阅 this topic page。我们的现代示例使用它,它适用于经典的 Win32 桌面应用程序以及 Windows Store、UWP 和 Xbox One 应用程序,因为它只是一个 C++ 模板。