我一直在按照一些教程学习 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 时,我都会收到一条错误消息,指出 swapChain 是一个 NULL 指针。我假设 bufferDesc 和/或 swapChainDesc 有一些无效数据,但编译器不能给我任何线索是什么导致了错误。有人可以告诉我如何跟踪和修复这样的错误吗?谢谢。
最佳答案
您没有检查 HRESULT
值,因此您缺少所有错误处理。对于 COM 编程,即使使用 Direct3D,您也必须检查每个可以返回失败的方法的 HRESULT
。如果返回值可以安全忽略,则返回 void
。
在老式 C/C++ 程序中检查 HRESULT
值是通过 FAILED
或 SUCCEEDED
宏完成的。
hr = D3D11CreateDeviceAndSwapChain( /* ... */ *);
if (FAILED(hr))
return false;
在大多数情况下,失败的 HRESULT
被视为“快速失败”或 fatal error 。换句话说,如果调用失败,程序将无法继续。
在其他情况下,可以通过使用不同的选项进行特殊情况处理以从错误中恢复。有关详细示例,请参阅 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
orV_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();
}
}
}
您的程序应该使用 /EHsc
编译,它已经在默认的 Visual Studio 模板中。参见 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 调试层,它在输出窗口中提供额外的调试信息。
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-schoolNULL
. That's because it is typed. You were usingNULL
in two places in your version where the parameter wasn't actually a pointer, it's a number.
有了这个,您将获得很多关于基本错误的反馈,这将有助于诊断在您的特定情况下交换链未能创建的原因。我怀疑这是因为您提供的一些值仅对“独占全屏模式”有意义,对窗口模式 (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 useZeroMemory
like you did in your old-school code above.
请注意,在缺少仅供开发人员使用的调试设备的系统上,使用 D3D11_CREATE_DEVICE_DEBUG
将失败。有关从何处获取调试设备层的详细信息因您使用的 Windows 版本而异。参见 Direct3D SDK Debug Layer Tricks底部有一个小汇总表。
此外,在存在未初始化变量的情况下进行调试是一个巨大的痛苦。未初始化的指针尤其会浪费大量时间。如果您执行以下操作将会有所帮助:
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 应用商店、UWP 和 Xbox One 应用程序,因为它只是一个 C++ 模板。
关于c++ - 调试 DirectX 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41163733/