C++ DirectX 11 CreateSwapChain()失败,错误为DXGI\u错误\u无效\u调用

C++ DirectX 11 CreateSwapChain()失败,错误为DXGI\u错误\u无效\u调用,c++,directx,directx-11,C++,Directx,Directx 11,每次我尝试创建swapChain时,它都会抛出此错误。 几个小时后,我找到了一个解决这个问题的方法,但没有找到任何适合我的方法。 以下是代码的重要部分: bool Direct3D::Initialize(HWND hWnd) { HRESULT hResult; ID3D11Device* pDevice = NULL; ID3D11DeviceContext* pDeviceContext = NULL; IDXGIDevice* pDXGIDevice =

每次我尝试创建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;
}
查看for
CreateSwapChain()
似乎
pSwapChain
必须不为
NULL
,但我认为这没有意义,因为我想用
CreateSwapChain()
指定
pSwapChain


有人知道此问题的解决方案吗?

您的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) );
}
hr=dxgiFactory2->CreateSwapChainForHwnd(g_pd3dDevice、g_hWnd和sd、nullptr、nullptr和g_pSwapChain1);
如果(成功(hr))
{
hr=g_pSwapChain1->QueryInterface(uu uuidof(IDXGISwapChain),reinterpret_cast(&g_pSwapChain));
}
此代码来自microsoft Win32 DirectX示例。


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

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

//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;
        }
    }
//循环遍历我们的驱动程序类型,直到找到要使用的驱动程序类型
for(无符号整数i=0;i
DXGI\u ERROR\u INVALID\u CALL
的真正意思是“参数中有错误”。空指针只是简单的例子。如果我删除
DXGI\u SWAP\u CHAIN\u FLAG\u DISPLAY\u ONLY
标志,请将
.Windowed
设置为true,并将
.bufferCount
设置为2(对于窗口测试,可能只需删除标志即可).这对我来说很有效,但无论我告诉
.Windowed
什么,它总是切换到全屏模式。我认为
.Windowed
不会对窗口产生任何影响;它只是交换链的一个参数。还有一些东西必须切换到全屏模式(如果你可以单步调试的话,很容易看到)。哦,我发现了错误:它显示为WINDOWED,我的配置显示为fullscreen,我正在解析它。我只需要添加一个!在
config.fullscreen
:b前面,这将导致完全重写代码,因为我必须在11.2基础上重新实现所有内容,而不是在11.0上,所以必须有另一种方法。