Advertisement

Creating a swap chain

Started by September 24, 2020 08:17 PM
8 comments, last by goukenfan 3 years, 11 months ago

	// Create the DXGI device object to use in other factories, such as Direct2D.
	IDXGIDevice3* dxgiDevice;

	// Create swap chain.
	IDXGIAdapter* adapter;
	IDXGIFactory2* factory;
	IDXGISwapChain1* swapchain;

	hr = device->QueryInterface(__uuidof(IDXGIDevice3), (void**)&dxgiDevice);

	hr = dxgiDevice->GetAdapter(&adapter);

	if (SUCCEEDED(hr))
	{

		hr = adapter->GetParent(__uuidof(IDXGIFactory2), (void**)&factory);

		hr = factory->CreateSwapChainForHwnd(
			device,
			m_amahwnd,
			&desc,
			NULL,
			NULL,
			&swapchain
		);

		try
		{
			if (!SUCCEEDED(hr))
			{
				throw(std::invalid_argument("Blah"));
			}
		}
		catch (std::invalid_argument& blah)
		{
			MessageBox(m_amahwnd, L"Blah", L"blah", MB_OK);
		}
		

The “Blah” message box always appears because the Swap Chain creation keeps failing. What do you do to create the swap chain?

Advertisement

Why are you torturing yourself? Go with OpenGL. LOL

Tip: Look at the value of hr and then read this page: https://docs.microsoft.com/en-us/windows/win32/api/dxgi1_2/nf-dxgi1_2-idxgifactory2-createswapchainforhwnd

It seems that the pointer swapchain declared at the top of the code is not initialized. Am I wrong ?

arturapps said:

It seems that the pointer swapchain declared at the top of the code is not initialized. Am I wrong ?

That shouldn't be a problem, the CreateSwapChainForHwnd() creates the swapchain object and fills the pointer.

Advertisement

In fact the documentation informs:

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

ppSwapChain

A pointer to a variable that receives a pointer to the IDXGISwapChain1 interface for the swap chain that CreateSwapChainForHwnd creates.

But on the returning value information it prompts this value among other possibilities:

  • DXGI_ERROR_INVALID_CALL if the calling application provided invalid data, for example, if pDesc or ppSwapChain is NULL.

If I have understood, The method creates the structure, but must copy the content to the structure of the type ppSwipeChan that you have to pass by pointer.

&swapchain should not be null, its taking a pointer to a pointer, the documentation is saying that is needed, not that dereferencing it is a valid existing object.

Essentially:

// ignoring other parameters in this example!
HRESULT CreateSwapChainForHwnd(
  IUnknown                              *pDevice,
  HWND                                  hWnd,
  const DXGI_SWAP_CHAIN_DESC1           *pDesc,
  const DXGI_SWAP_CHAIN_FULLSCREEN_DESC *pFullscreenDesc,
  IDXGIOutput                           *pRestrictToOutput,
  IDXGISwapChain1                       **ppSwapChain
)
{
    if (!pDesc || !ppSwapChain)
        return DXGI_ERROR_INVALID_CALL; 
    *ppSwapChain = new DXGISwapChainImplementation();
    return S_OK;
}

As for the OP's question check the required parameters are valid, what is in desc? And which exact error was returned?

I bought a book for Vulkan because the books for DirectX were always by the same author. I wanted something that might be more open source too. So thank you for the help but I'm giving up DirectX like the second poster said to.

This topic is closed to new replies.

Advertisement