4

Everytime I try to create the swapChain it throws this error. After hours searching for a fix for this I found nothing that worked for me. Here's the important part of the code:

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;
}

Looking at the documentation for CreateSwapChain() it seems to be that pSwapChain has to be not NULL, but that doesn't make sense in my opinion because I want to specify pSwapChain with CreateSwapChain().

Does anyone know a solution for this problem?

Rafiwui
  • 544
  • 6
  • 19
  • 1
    `DXGI_ERROR_INVALID_CALL` really means "there is something wrong in the parameters". Null pointers are just the simple cases. Your code runs with no errors on my PC if I remove the `DXGI_SWAP_CHAIN_FLAG_DISPLAY_ONLY` flag, set `.Windowed` to true and `.bufferCount` to 2 (for a windowed test ; maybe it will work for you with just the flag removed). – ElderBug May 30 '16 at 14:31
  • That worked for me but no matter what i tell `.Windowed` to be it always switches to fullscreen mode. – Rafiwui May 30 '16 at 14:39
  • I don't think `.Windowed` will do anything to the window ; it's just a parameter for the swap chain. Something else must be switching to fullscreen mode (it's easy to see what if you can step-debug). – ElderBug May 30 '16 at 14:49
  • Oh well I found the error :D It says WINDOWED and my config says FULLSCREEN and I'm just parsing it. I just had to add an ! in front of `config.fullscreen` :D – Rafiwui May 30 '16 at 15:08

2 Answers2

0

Your IDXGISwapChain should be associated with SwapChainPanel XAML control(or HWND if you are running Win32 application). You can do initialization like this:

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) );
}

This code is from microsoft Win32 DirectX sample. https://code.msdn.microsoft.com/windowsdesktop/Direct3D-Tutorial-Win32-829979ef/view/Discussions#content

If you are running WinRT application you can look trough DirectX and XAML application template.

  • but that would end up in a complete rewriting of the code because i would have to reimplement everything on 11.2 base and not on 11.0 so there has to be another way. – Rafiwui May 30 '16 at 14:32
0

You are passing in the address of your Swap Chain Pointer. This is so the Create device and swap chain function can fill that pointer out with information. Here is a example.

//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;
        }
    }