Xenonic
Xenonic

Reputation: 364

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

So, just like the title states, I am setting an HRESULT to D3D11CreateDeviceAndSwapChain with a check to see if it fails. Obviously it does everytime, and when I display the error message by using,

_com_error error(hresult);
LPCSTR errorText = error.ErrorMessage();
MessageBox(hwnd, errorText, "Fatal Error", MB_OK);

The following message is the contents of the 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.

I create my swap chain description using,

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;

And then I attempt to create my swap chain with,

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

So my question is, what parameter am I missing or misconfigured that results in this strange error? Thank you for anything in advance.

Note: I have tried changing D3D_DRIVER_TYPE_HARDWARE to REFERENCE with no effect.

Note: If you want to see the whole source code or other useful information that I may have forgotten to mention, it can be found here.

Update: When debugging, I receive this error,

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

This clearly shows that my problem is that the swap chain is not being given a window to attach to and therefore failing. I don't understand why this is happening because in my game class, I initialize the window with,

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

And pass the HWND down through functions to get it to the swap chain.

Upvotes: 0

Views: 1589

Answers (1)

Xenonic
Xenonic

Reputation: 364

Problem:

While sending the HWND to the CreateDeviceAndSwapChain() function, I had a small mix-up that swapped the original HWND with a different one.

Solution:

After going back and reviewing my code for the fourth time, I found that the mix-up happened in my main frame class, I solved this by removing the secondary useless creation of the other HWND, and fixed the name change between them.

I hope that this post can helps others that receive the error that I got in the post above, as it is merely the swap chain not receiving a working and active HWND. An easy fix but hard to find.

Upvotes: 2

Related Questions