Skip to content

Commit

Permalink
Fix all error handling in CreateSwapChain calls.
Browse files Browse the repository at this point in the history
Multiple instances of checking for S_OK, when it's possible to get a non-zero status result back.  Changed to check for SUCCEEDED instead.

In d3d11, CreateDeviceAndSwapChain, there was a similar check, modified to only exit on FAILED, allowing status results to continue.

Also noticed an error check bug where copy/paste of wrapper needed to be wrapper2, so error logging would have been missed.
  • Loading branch information
bo3b committed Apr 3, 2014
1 parent c4c637b commit ad1defe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
5 changes: 3 additions & 2 deletions DirectX10/d3d10Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,11 @@ HRESULT WINAPI D3D10CreateDeviceAndSwapChain(D3D11Base::IDXGIAdapter *pAdapter,
D3D11Base::ID3D10Device *origDevice = 0;
HRESULT ret = (*_D3D10CreateDeviceAndSwapChain)(ReplaceAdapter(pAdapter), DriverType, Software, Flags, SDKVersion,
pSwapChainDesc, ppSwapChain, &origDevice);
if (ret != S_OK)

// Changed to recognize that >0 DXGISTATUS values are possible, not just S_OK.
if (FAILED(ret))
{
if (LogFile) fprintf(LogFile, " failed with HRESULT=%x\n", ret);

return ret;
}

Expand Down
7 changes: 4 additions & 3 deletions DirectX11/d3d11Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2186,10 +2186,11 @@ HRESULT WINAPI D3D11CreateDeviceAndSwapChain(
D3D11Base::ID3D11DeviceContext *origContext = 0;
HRESULT ret = (*_D3D11CreateDeviceAndSwapChain)(ReplaceAdapter(pAdapter), DriverType, Software, Flags, pFeatureLevels,
FeatureLevels, SDKVersion, pSwapChainDesc, ppSwapChain, &origDevice, pFeatureLevel, &origContext);
if (ret != S_OK)

// Changed to recognize that >0 DXGISTATUS values are possible, not just S_OK.
if (FAILED(ret))
{
if (LogFile) fprintf(LogFile, " failed with HRESULT=%x\n", ret);

return ret;
}

Expand All @@ -2214,7 +2215,7 @@ HRESULT WINAPI D3D11CreateDeviceAndSwapChain(
*ppDevice = wrapper;

D3D11Wrapper::ID3D11DeviceContext *wrapper2 = D3D11Wrapper::ID3D11DeviceContext::GetDirect3DDeviceContext(origContext);
if(wrapper == NULL)
if (wrapper2 == NULL)
{
if (LogFile) fprintf(LogFile, " error allocating wrapper2.\n");

Expand Down
23 changes: 12 additions & 11 deletions DirectXGI/Direct3DXGIFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ STDMETHODIMP D3D11Wrapper::IDXGIFactory::CreateSwapChain(THIS_
// can be occluded when we return from creating the real swap chain.
// The check below was looking ONLY for S_OK, and that would lead to it skipping the sub-block
// which set up ppSwapChain and returned it. So, ppSwapChain==NULL and it would crash, sometimes.
// There are other legitimate DXGI_STATUS results, so checking for FAILED is the correct way.
// There are other legitimate DXGI_STATUS results, so checking for SUCCEEDED is the correct way.
// Same bug fix is applied for the other CreateSwapChain* variants.

if (!FAILED(hr))
if (SUCCEEDED(hr))
{
*ppSwapChain = D3D11Wrapper::IDXGISwapChain::GetDirectSwapChain(origSwapChain);
if ((*ppSwapChain)->m_WrappedDevice) (*ppSwapChain)->m_WrappedDevice->Release();
Expand Down Expand Up @@ -223,16 +224,16 @@ STDMETHODIMP D3D11Wrapper::IDXGIFactory2::CreateSwapChainForHwnd(THIS_
HRESULT hr = -1;
if (pRestrictToOutput)
hr = GetFactory2()->CreateSwapChainForHwnd(realDevice, hWnd, pDesc, pFullscreenDesc, pRestrictToOutput->m_pOutput, &origSwapChain);
if (hr == S_OK)
if (LogFile) fprintf(LogFile, " return value = %x\n", hr);

if (SUCCEEDED(hr))
{
*ppSwapChain = D3D11Wrapper::IDXGISwapChain1::GetDirectSwapChain(origSwapChain);
if ((*ppSwapChain)->m_WrappedDevice) (*ppSwapChain)->m_WrappedDevice->Release();
(*ppSwapChain)->m_WrappedDevice = pDevice; pDevice->AddRef();
(*ppSwapChain)->m_RealDevice = realDevice;
if (pDesc) SendScreenResolution(pDevice, pDesc->Width, pDesc->Height);
}

if (LogFile) fprintf(LogFile, " return value = %x\n", hr);

return hr;
}
Expand Down Expand Up @@ -263,7 +264,9 @@ STDMETHODIMP D3D11Wrapper::IDXGIFactory2::CreateSwapChainForCoreWindow(THIS_
HRESULT hr = -1;
if (pRestrictToOutput)
hr = GetFactory2()->CreateSwapChainForCoreWindow(realDevice, pWindow, pDesc, pRestrictToOutput->m_pOutput, &origSwapChain);
if (hr == S_OK)
if (LogFile) fprintf(LogFile, " return value = %x\n", hr);

if (SUCCEEDED(hr))
{
*ppSwapChain = D3D11Wrapper::IDXGISwapChain1::GetDirectSwapChain(origSwapChain);
if ((*ppSwapChain)->m_WrappedDevice) (*ppSwapChain)->m_WrappedDevice->Release();
Expand All @@ -272,8 +275,6 @@ STDMETHODIMP D3D11Wrapper::IDXGIFactory2::CreateSwapChainForCoreWindow(THIS_
if (pDesc) SendScreenResolution(pDevice, pDesc->Width, pDesc->Height);
}

if (LogFile) fprintf(LogFile, " return value = %x\n", hr);

return hr;
}

Expand Down Expand Up @@ -301,7 +302,9 @@ STDMETHODIMP D3D11Wrapper::IDXGIFactory2::CreateSwapChainForComposition(THIS_
HRESULT hr = -1;
if (pRestrictToOutput)
hr = GetFactory2()->CreateSwapChainForComposition(realDevice, pDesc, pRestrictToOutput->m_pOutput, &origSwapChain);
if (hr == S_OK)
if (LogFile) fprintf(LogFile, " return value = %x\n", hr);

if (SUCCEEDED(hr))
{
*ppSwapChain = D3D11Wrapper::IDXGISwapChain1::GetDirectSwapChain(origSwapChain);
if ((*ppSwapChain)->m_WrappedDevice) (*ppSwapChain)->m_WrappedDevice->Release();
Expand All @@ -310,8 +313,6 @@ STDMETHODIMP D3D11Wrapper::IDXGIFactory2::CreateSwapChainForComposition(THIS_
if (pDesc) SendScreenResolution(pDevice, pDesc->Width, pDesc->Height);
}

if (LogFile) fprintf(LogFile, " return value = %x\n", hr);

return hr;
}

Expand Down

0 comments on commit ad1defe

Please sign in to comment.