D3D11CreateDeviceAndSwapChain,HRESULT 是 "Application made an invalid call."?

D3D11CreateDeviceAndSwapChain, HRESULT is "Application made an invalid call."?

因此,正如标题所述,我将 HRESULT 设置为 D3D11CreateDeviceAndSwapChain 并检查它是否失败。显然它每次都会,当我使用

显示错误消息时
_com_error error(hresult);
LPCSTR errorText = error.ErrorMessage();
MessageBox(hwnd, errorText, "Fatal Error", MB_OK);

以下留言内容为MessageBox,

The application made a call that is invalid. Either the parameters of the call or the state of the object was incorrect.
Enable the D3D debug layer in order to see details via debug messages.

我使用

创建我的交换链描述
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
swapChainDesc.BufferCount                        = 1;
swapChainDesc.BufferDesc.Width                   = screenWidth;
swapChainDesc.BufferDesc.Height                  = screenHeight;
swapChainDesc.BufferDesc.Format                  = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator   = 0;  // Numerator and denominator are as they are due to a bug in calculation, leaving me with a number around 760000 while calculating for VSync.
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage                        = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow                       = hwnd;
swapChainDesc.SampleDesc.Count                   = 1;
swapChainDesc.SampleDesc.Quality                 = 0;
swapChainDesc.Windowed                           = true;
swapChainDesc.BufferDesc.ScanlineOrdering        = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
swapChainDesc.BufferDesc.Scaling                 = DXGI_MODE_SCALING_UNSPECIFIED;
swapChainDesc.SwapEffect                         = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags                              = 0;
featureLevel                                     = D3D_FEATURE_LEVEL_11_0;

然后我尝试创建我的交换链,

result = D3D11CreateDeviceAndSwapChain(
NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
0,
&featureLevel,
1,
D3D11_SDK_VERSION,
&swapChainDesc,
&m_swapChain,
&m_device,
NULL,
&m_deviceContext);

所以我的问题是,我缺少或配置错误的参数导致了这个奇怪的错误?提前谢谢你。

注意:我试过将 D3D_DRIVER_TYPE_HARDWARE 更改为 REFERENCE 但没有效果。

注意:如果您想查看完整的源代码或我可能忘记提及的其他有用信息,可以找到 here

更新:调试时,我收到这个错误,

DXGI ERROR: IDXGIFactory::CreateSwapChain: No target window specified in DXGI_SWAP_CHAIN_DESC, and no window associated with owning factory. [ MISCELLANEOUS ERROR #6: ]

这清楚地表明我的问题是交换链没有被赋予 window 以附加到因此失败。我不明白为什么会这样,因为在我的游戏 class 中,我用

初始化了 window
m_HWND = CreateWindowEx(
    WS_EX_APPWINDOW,
    (LPCSTR)m_AppName,
    "Engine",
    WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_POPUP,
    xPos,
    yPos,
    screenWidth,
    screenHeight,
    NULL,
    NULL,
    m_hInst,
    NULL);

并通过函数将 HWND 传递到交换链。

问题:

在将 HWND 发送到 CreateDeviceAndSwapChain() 函数时,我有一个小的混淆,将原来的 HWND 换成了另一个。

解决方案:

第四次回头检查我的代码后,我发现混淆发生在我的主框架中class,我通过删除另一个[=10]的二次无用创建来解决这个问题=],并修复了它们之间的名称更改。

我希望这个 post 可以帮助其他收到我在上面 post 中遇到的错误的人,因为它只是交换链没有收到工作和活动 HWND.一个简单的修复,但很难找到。