Stereoscopic 3D - how to create a swapchain

Started by
1 comment, last by Shnoutz 11 years, 2 months ago

Hi,

I would like to know how to setup stereoscopic 3D using DirectX 11 Win32 application.

I got to the point where I need to create a swap chain but my application fails in CreateSwapChainForHwnd...

Here's the code I use to detect a stereo capable output (monitor) and create a swap chain for it:


if(m_dxgiFactory->IsWindowedStereoEnabled())
{
	// Enumerate all outputs
	DirectX11::Handle < IDXGIOutput > dxgiOutput;
	for(UINT outputIt = 0; SUCCEEDED(m_dxgiAdapter->EnumOutputs(outputIt, dxgiOutput.ptr())); ++outputIt)
	{
		// Get stereo 3D capable output
		DirectX11::Handle < IDXGIOutput1 > dxgiOutput1;
		if(FAILED(dxgiOutput->QueryInterface(__uuidof(IDXGIOutput1), (void **) dxgiOutput1.ptr())))
			throw "Failed to retrieve DXGI stereo 3D output";
		// Get display mode list
		UINT numModes;
		if(FAILED(dxgiOutput1->GetDisplayModeList1(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED|DXGI_ENUM_MODES_STEREO, &numModes, NULL)))
			throw "Failed to retrieve DXGI display mode list (count)";
		std::vector < DXGI_MODE_DESC1 > displayModes(numModes);
		if(FAILED(dxgiOutput1->GetDisplayModeList1(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED|DXGI_ENUM_MODES_STEREO, &numModes, displayModes.data())))
			throw "Failed to retrieve DXGI display mode list";
		// Output display mode list
		double maxRefreshRateInHertz = 0.0;
		for(uint i = 0; i < numModes; i++)
		{
			if(displayModes[i].Stereo)
			{
				double const refreshRateInHertz = 
(double) displayModes[i].RefreshRate.Numerator / (double) displayModes[i].RefreshRate.Denominator;
				if(refreshRateInHertz > maxRefreshRateInHertz)
				{
					maxRefreshRateInHertz = refreshRateInHertz;
					m_fullscreenWidth = displayModes[i].Width;
					m_fullscreenHeight = displayModes[i].Height;
					m_fullscreenRefreshRateNumerator = displayModes[i].RefreshRate.Numerator;
					m_fullscreenRefreshRateDenominator = displayModes[i].RefreshRate.Denominator;
					m_dxgiOutput = dxgiOutput;
				}
			}
		}
		dxgiOutput = nullptr;
	}

	// Create initial swap chain descriptors
	m_swapChainDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	m_swapChainDesc.Stereo = TRUE;
	m_swapChainDesc.SampleDesc.Count = 1;
	m_swapChainDesc.SampleDesc.Quality = 0;
	m_swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	m_swapChainDesc.BufferCount = 2;
	m_swapChainDesc.Scaling = DXGI_SCALING_NONE;
	m_swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_SEQUENTIAL;
	m_swapChainDesc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
	m_swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;			
	m_swapChainFullscreenDesc.RefreshRate.Numerator = m_fullscreenRefreshRateNumerator;
	m_swapChainFullscreenDesc.RefreshRate.Denominator = m_fullscreenRefreshRateDenominator;
	m_swapChainFullscreenDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	m_swapChainFullscreenDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
	m_swapChainDesc.Width = m_fullscreenWidth;
	m_swapChainDesc.Height = m_fullscreenHeight;
	m_swapChainFullscreenDesc.Windowed = FALSE;

	// Create swap chain
	if(FAILED(m_dxgiFactory->CreateSwapChainForHwnd(
		m_device,
		p_hWnd,
		&m_swapChainDesc,
		&m_swapChainFullscreenDesc,
		m_dxgiOutput,
		m_swapChain.ptr())))
		throw "Failed to create DXGI swap chain";
}

The call to CreateSwapChainForHwnd fails without any debug output so I don't really know why it doesn't work.

Anyone tried something similar?

Advertisement

Try changing


    if(FAILED(m_dxgiFactory->CreateSwapChainForHwnd(
        m_device,
        p_hWnd,
        &m_swapChainDesc,
        &m_swapChainFullscreenDesc,
        m_dxgiOutput,
        m_swapChain.ptr())))
        throw "Failed to create DXGI swap chain";

to


    HRESULT hr;
    hr = m_dxgiFactory->CreateSwapChainForHwnd(
        m_device,
        p_hWnd,
        &m_swapChainDesc,
        &m_swapChainFullscreenDesc,
        m_dxgiOutput,
        m_swapChain.ptr());

    if (FAILED(hr))
    {
        std::stringstream str;
        str << "Failed to create DXGI swap chain. HR: 0x" << std::hex << std::uppercase << static_cast<unsigned long>(hr) << std::endl;
        OutputDebugStringA(str.str().c_str());
        throw str.str().c_str();
    }



Note that you'll need to #include <string> <sstream> <ios> and <ostream>. Also, I'd generally recommend throwing C++ standard library-based exceptions rather than raw ASCII strings and working in Unicode (e.g. L"Something" rather than "Something") along with the corresponding wide character variants of the STL functionality (e.g. std::wstringstream) and Windows API calls (OutputDebugStringW).

Once you have the HRESULT code from the swap chain failure, you'll potentially have much more information than just that the call failed.

At a guess, what's probably going on based on the code you provided you are passing in for the HWND a parameter named "p_hWnd". If that's really a pointer to an HWND, you should be passing in "*p_hWnd" since the API call is expecting an HWND, not a pointer to one.

Also, from the documentation - http://msdn.microsoft.com/en-us/library/hh768891(v=vs.85).aspx , you are calling m_dxgiFactory->IsWindowedStereoEnabled to check for stereo support but this just tells you whether or not you can render a windowed app in stereo. The proper check for a full screen app is to call IDXGIOutput1::GetDisplayModeList1 and see if any of the returned modes supports stereo. You are doing that anyway but your initial call to IsWindowedStereoEnabled is superfluous and might even improperly exclude stereo if the card didn't support windowed stereo with its current settings but would support full screen stereo.

Thanks Mike for your answer.

I will re-check the return value of the CreateSwapChainForHwnd... Somehow I forgot about it and I was relying on directx's debug output to explain the problem (as most of the time it is quite detailed).

From the link you provided, it seems like one needs to register a stereo notification event to enable stereo rendering... I am not sure if CreateSwapChain will fail if no event is registered (like in my case). I havent worked with win api events before and I would like to see an example of its use.

Gab.

P.S.: the p_ this is to specify that p_hWnd is a paramter of the function and I am throwing ascii strings only for fatal errors, its a choice I made a while ago... Probably not the best idea but it works for me.

This topic is closed to new replies.

Advertisement