DirectX 11 CreateSwapChain()失败,错误DXGI_ERROR_INVALID_CALL

时间:2016-05-30 12:25:01

标签: c++ directx directx-11

每次我尝试创建swapChain时都会抛出此错误。 经过几个小时寻找修复,我发现没有什么对我有用。 这是代码的重要部分:

bool Direct3D::Initialize(HWND hWnd)
{
    HRESULT hResult;

    ID3D11Device* pDevice = NULL;
    ID3D11DeviceContext* pDeviceContext = NULL;
    IDXGIDevice* pDXGIDevice = NULL;
    IDXGIAdapter* pAdapter = NULL;
    IDXGIFactory* pFactory = NULL;
    IDXGISwapChain* pSwapChain = NULL;


    D3D_FEATURE_LEVEL featureLevels[] = {   //Add feature levels to support here
        D3D_FEATURE_LEVEL_11_0
    };
#ifdef _DEBUG
    UINT deviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_DEBUG;
#else
    UINT deviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#endif
    //Create the device and deviceContext
    hResult = D3D11CreateDevice(NULL,                               //needs to be NULL if D3D_DRIVER_TYPE_HARDWARE is used; NULL takes the default adapter
                                D3D_DRIVER_TYPE_HARDWARE,
                                NULL,                               //needs to be not NULL if D3D_DRIVER_TYPE_SOFTWARE is used
                                deviceFlags,
                                featureLevels,
                                ARRAYSIZE(featureLevels),
                                D3D11_SDK_VERSION,
                                &pDevice,
                                NULL,
                                &pDeviceContext);
    if (FAILED(hResult))
    {
        return false;
    }


    hResult = pDevice->QueryInterface(__uuidof(IDXGIDevice), (void**)&pDXGIDevice);
    if (FAILED(hResult))
    {
        return false;
    }

    hResult = pDXGIDevice->GetAdapter(&pAdapter);
    if (FAILED(hResult))
    {
        return false;
    }

    hResult = pAdapter->GetParent(__uuidof(IDXGIFactory), (void**)&pFactory);
    if (FAILED(hResult))
    {
        return false;
    }

    DXGI_MODE_DESC bufferDesc;
    ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC));

    bufferDesc.Width = 0;                   //Zero for evaluating it from the output window
    bufferDesc.Height = 0;                  //Zero for evaluating it from the output window
    bufferDesc.RefreshRate.Numerator = config.refreshRate;
    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 = config.fullscreen;
    swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
    swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH | DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY;

    hResult = pFactory->CreateSwapChain(pDevice, &swapChainDesc, &pSwapChain);
    CGE_SAFE_RELEASE(pDXGIDevice);
    CGE_SAFE_RELEASE(pAdapter);
    CGE_SAFE_RELEASE(pFactory);
    if (FAILED(hResult))
    {
        return false;
    }


    return true;
}

查看CreateSwapChain()的{​​{3}}似乎pSwapChain必须不是NULL,但这在我看来没有意义,因为我想要使用pSwapChain指定CreateSwapChain()

有没有人知道这个问题的解决方案?

2 个答案:

答案 0 :(得分:0)

您的IDXGISwapChain应该与SwapChainPanel XAML控件相关联(如果您运行的是Win32应用程序,则与HWND相关联)。您可以像这样进行初始化:

hr = dxgiFactory2->CreateSwapChainForHwnd( g_pd3dDevice, g_hWnd, &sd, nullptr, nullptr, &g_pSwapChain1 );
if (SUCCEEDED(hr))
{
    hr = g_pSwapChain1->QueryInterface( __uuidof(IDXGISwapChain), reinterpret_cast<void**>(&g_pSwapChain) );
}

此代码来自Microsoft Win32 DirectX示例。 https://code.msdn.microsoft.com/windowsdesktop/Direct3D-Tutorial-Win32-829979ef/view/Discussions#content

如果您正在运行WinRT应用程序,则可以查看DirectX和XAML应用程序模板。

答案 1 :(得分:0)

您正在传递您的交换链指针的地址。这样创建设备和交换链功能就可以用信息填充指针。这是一个例子。

//loop through our driver types till we find the one we will be using
    for (unsigned int i = 0; i < DriverCount; i++)
    {
        //Create our device and swap chain
        DXERROR = D3D11CreateDeviceAndSwapChain(nullptr, drivers[i], nullptr, 
Flag, levels, LevelsCount, D3D11_SDK_VERSION, &SwapDesc, &DX.pSwapChain, 
&DX.pDevice, &DX.FeatureLevel, &DX.pImmediateContext);

        if (SUCCEEDED(DXERROR))
        {
            DX.DriverType = drivers[i];
            break;
        }
    }