【问题标题】:DXGI_ERROR_INVALID_CALL when creating swap chain for core window为核心窗口创建交换链时出现 DXGI_ERROR_INVALID_CALL
【发布时间】:2017-01-30 19:27:55
【问题描述】:

我正在通过 DirectXTutorials.com 上的 this tutorial 学习 DX11。我已经创建了设备及其上下文,现在需要创建交换链。
但是,当我调用 CreateSwapChainForCoreWindow(...) 时,它会将交换链作为 nullptr 并返回 0x887a0001。 DirectX 错误查找为代码吐出以下内容:

Name: DXGI_ERROR_INVALID_CALL
Description: The application has made an erroneous API call that it had enough information to avoid.
This error is intended to denote that the application should be altered to avoid the error.
Use of the debug version of the DXGI.DLL will provide run-time debug output with further information.
Severity code: Failed

我应该如何更改代码以避免此错误?我尝试按照其他答案更改交换链描述的格式或效果,但无济于事。

这是有问题的代码,这是最后一次失败的调用:

void Game::Initialize()
{
    // Define temporary pointers to a device and a device context
    ComPtr<ID3D11Device> dev11;
    ComPtr<ID3D11DeviceContext> devcon11;

    // Create the device and device context objects
    D3D11CreateDevice(
        nullptr,
        D3D_DRIVER_TYPE_HARDWARE,
        nullptr,
        0,
        nullptr,
        0,
        D3D11_SDK_VERSION,
        &dev11,
        nullptr,
        &devcon11
    );

    // Convert the pointers from the DirectX 11 version to the DirectX 11.1 versions
    dev11.As(&mDevice);
    devcon11.As(&mDeviceContext);

    // Obtain the DXGI factory
    // [1] Convert our ID3D11Device1 into an IDXGIDevice1
    ComPtr<IDXGIDevice1> dxgiDevice;
    mDevice.As(&dxgiDevice);

    // [2] Use the IDXGDevice1 interface to get access to the adapter
    ComPtr<IDXGIAdapter> dxgiAdapter;
    dxgiDevice->GetAdapter(&dxgiAdapter);

    // [3] Use the IDXGIAdapter interface to get access to the factory
    ComPtr<IDXGIFactory2> dxgiFactory;
    dxgiAdapter->GetParent(__uuidof(IDXGIFactory2), &dxgiFactory);

    // 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 and back buffer
    scd.Format = DXGI_FORMAT_R8G8B8A8_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

    HRESULT result = dxgiFactory->CreateSwapChainForCoreWindow(
        mDevice.Get(),      // Address of the device
        reinterpret_cast<IUnknown*>(CoreWindow::GetForCurrentThread()), // Address of the window
        &scd,               // Address of the swap chain description                        
        nullptr,            // Monitor selection stuff - leave null
        &mSwapChain         // Address of the new swap chain
    );
}

mDevicemDeviceContextmSwapChain都是类的成员变量。

我直接从 SDL 和 SFML 转向 DirectX,所以这与我之前的水平一样低。如果有足够的信息来避免有问题的调用,那么我应该调用什么呢?我看到人们打电话给CreateDeviceAndSwapChain 的一些问题,这是首选方法吗?

编辑: 澄清一下,项目类型是DirectX11 App (Universal Windows),如果这有什么不同的话。

【问题讨论】:

  • 你可能想看看这个DeviceResources,以及CreateDXGIFactory2的使用。
  • @ChuckWalbourn 谢谢,我从该模板生成了一个新项目,它的构建和工作没有问题;所以我会把它和我的比较一下。

标签: c++ directx directx-11


【解决方案1】:

这是问题所在:

    reinterpret_cast<IUnknown*>(CoreWindow::GetForCurrentThread()), // Address of the window

查看故障点的堆栈跟踪表明我正在从App::Initialize 调用Game::Initialize。因此,GetForCurrentThread() 返回 null 因为窗口还不存在! (App::SetWindow(CoreWindow^ window)是之后调用的,从那时起我们才能得到窗口。)
这是一个令人痛苦的简单错误。

【讨论】:

    【解决方案2】:

    我只是在同一个调用中遇到了同样的错误,但在我的情况下,原因是我传递的是 D3D 设备而不是 D3D 命令队列。这对于 D3D11 和 D3D12 是不同的。见:

    https://docs.microsoft.com/en-us/windows/win32/api/dxgi1_2/nf-dxgi1_2-idxgifactory2-createswapchainforcorewindow

    参数

    pDevice

    对于 Direct3D 11 和更早版本的 Direct3D,这是指向交换链的 Direct3D 设备的指针。对于 Direct3D 12,这是一个指向直接命令队列的指针(请参阅 ID3D12CommandQueue)。该参数不能为NULL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      相关资源
      最近更新 更多