Add the ability to recover from a removed device (TDR or driver removal) to the D3D11 code path.

TRAC #22411 Signed-off-by: Geoff Lang Signed-off-by: Shannon Woods Author: Jamie Madill git-svn-id: https://angleproject.googlecode.com/svn/branches/dx11proto@1857 736b8ea6-26fd-11df-bfd4-992fa37f6226
parent 779aa261
...@@ -2092,7 +2092,7 @@ GLenum Context::getError() ...@@ -2092,7 +2092,7 @@ GLenum Context::getError()
GLenum Context::getResetStatus() GLenum Context::getResetStatus()
{ {
if (mResetStatus == GL_NO_ERROR) if (mResetStatus == GL_NO_ERROR && !mContextLost)
{ {
// mResetStatus will be set by the markContextLost callback // mResetStatus will be set by the markContextLost callback
// in the case a notification is sent // in the case a notification is sent
...@@ -2103,6 +2103,8 @@ GLenum Context::getResetStatus() ...@@ -2103,6 +2103,8 @@ GLenum Context::getResetStatus()
if (mResetStatus != GL_NO_ERROR) if (mResetStatus != GL_NO_ERROR)
{ {
ASSERT(mContextLost);
if (mRenderer->testDeviceResettable()) if (mRenderer->testDeviceResettable())
{ {
mResetStatus = GL_NO_ERROR; mResetStatus = GL_NO_ERROR;
......
...@@ -401,16 +401,23 @@ HRESULT Image11::map(D3D11_MAPPED_SUBRESOURCE *map) ...@@ -401,16 +401,23 @@ HRESULT Image11::map(D3D11_MAPPED_SUBRESOURCE *map)
{ {
createStagingTexture(); createStagingTexture();
HRESULT result = D3DERR_INVALIDCALL; HRESULT result = E_FAIL;
if (mStagingTexture) if (mStagingTexture)
{ {
ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext(); ID3D11DeviceContext *deviceContext = mRenderer->getDeviceContext();
result = deviceContext->Map(mStagingTexture, 0, D3D11_MAP_WRITE, 0, map); result = deviceContext->Map(mStagingTexture, 0, D3D11_MAP_WRITE, 0, map);
ASSERT(SUCCEEDED(result));
// this can fail if the device is removed (from TDR)
if (d3d11::isDeviceLostError(result))
{
mRenderer->notifyDeviceLost();
}
else if (SUCCEEDED(result))
{
mDirty = true; mDirty = true;
} }
}
return result; return result;
} }
......
...@@ -238,7 +238,7 @@ RenderTarget11::RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height ...@@ -238,7 +238,7 @@ RenderTarget11::RenderTarget11(Renderer *renderer, GLsizei width, GLsizei height
return; return;
} }
HRESULT result = D3DERR_INVALIDCALL; HRESULT result = E_FAIL;
if (width > 0 && height > 0) if (width > 0 && height > 0)
{ {
......
...@@ -107,45 +107,7 @@ Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc( ...@@ -107,45 +107,7 @@ Renderer11::Renderer11(egl::Display *display, HDC hDc) : Renderer(display), mDc(
Renderer11::~Renderer11() Renderer11::~Renderer11()
{ {
releaseDeviceResources(); release();
if (mDxgiFactory)
{
mDxgiFactory->Release();
mDxgiFactory = NULL;
}
if (mDxgiAdapter)
{
mDxgiAdapter->Release();
mDxgiAdapter = NULL;
}
if (mDeviceContext)
{
mDeviceContext->ClearState();
mDeviceContext->Flush();
mDeviceContext->Release();
mDeviceContext = NULL;
}
if (mDevice)
{
mDevice->Release();
mDevice = NULL;
}
if (mD3d11Module)
{
FreeLibrary(mD3d11Module);
mD3d11Module = NULL;
}
if (mDxgiModule)
{
FreeLibrary(mDxgiModule);
mDxgiModule = NULL;
}
} }
Renderer11 *Renderer11::makeRenderer11(Renderer *renderer) Renderer11 *Renderer11::makeRenderer11(Renderer *renderer)
...@@ -170,6 +132,8 @@ EGLint Renderer11::initialize() ...@@ -170,6 +132,8 @@ EGLint Renderer11::initialize()
return EGL_NOT_INITIALIZED; return EGL_NOT_INITIALIZED;
} }
// create the D3D11 device
ASSERT(mDevice == NULL);
PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice"); PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
if (D3D11CreateDevice == NULL) if (D3D11CreateDevice == NULL)
...@@ -1775,11 +1739,18 @@ bool Renderer11::testDeviceLost(bool notify) ...@@ -1775,11 +1739,18 @@ bool Renderer11::testDeviceLost(bool notify)
{ {
bool isLost = false; bool isLost = false;
// TODO // GetRemovedReason is used to test if the device is removed
//UNIMPLEMENTED(); HRESULT result = mDevice->GetDeviceRemovedReason();
isLost = d3d11::isDeviceLostError(result);
if (isLost) if (isLost)
{ {
// Log error if this is a new device lost event
if (mDeviceLost == false)
{
ERR("The D3D11 device was removed: 0x%08X", result);
}
// ensure we note the device loss -- // ensure we note the device loss --
// we'll probably get this done again by notifyDeviceLost // we'll probably get this done again by notifyDeviceLost
// but best to remember it! // but best to remember it!
...@@ -1797,30 +1768,106 @@ bool Renderer11::testDeviceLost(bool notify) ...@@ -1797,30 +1768,106 @@ bool Renderer11::testDeviceLost(bool notify)
bool Renderer11::testDeviceResettable() bool Renderer11::testDeviceResettable()
{ {
HRESULT status = D3D_OK; // determine if the device is resettable by creating a dummy device
PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
// TODO if (D3D11CreateDevice == NULL)
UNIMPLEMENTED(); {
return false;
}
switch (status) D3D_FEATURE_LEVEL featureLevel[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
ID3D11Device* dummyDevice;
D3D_FEATURE_LEVEL dummyFeatureLevel;
ID3D11DeviceContext* dummyContext;
HRESULT result = D3D11CreateDevice(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
#if defined(_DEBUG)
D3D11_CREATE_DEVICE_DEBUG,
#else
0,
#endif
featureLevel,
sizeof(featureLevel)/sizeof(featureLevel[0]),
D3D11_SDK_VERSION,
&dummyDevice,
&dummyFeatureLevel,
&dummyContext);
if (!mDevice || FAILED(result))
{ {
case D3DERR_DEVICENOTRESET:
case D3DERR_DEVICEHUNG:
return true;
default:
return false; return false;
} }
dummyContext->Release();
dummyDevice->Release();
return true;
} }
bool Renderer11::resetDevice() void Renderer11::release()
{ {
releaseDeviceResources(); releaseDeviceResources();
// TODO if (mDxgiFactory)
UNIMPLEMENTED(); {
mDxgiFactory->Release();
mDxgiFactory = NULL;
}
if (mDxgiAdapter)
{
mDxgiAdapter->Release();
mDxgiAdapter = NULL;
}
if (mDeviceContext)
{
mDeviceContext->ClearState();
mDeviceContext->Flush();
mDeviceContext->Release();
mDeviceContext = NULL;
}
if (mDevice)
{
mDevice->Release();
mDevice = NULL;
}
if (mD3d11Module)
{
FreeLibrary(mD3d11Module);
mD3d11Module = NULL;
}
if (mDxgiModule)
{
FreeLibrary(mDxgiModule);
mDxgiModule = NULL;
}
}
bool Renderer11::resetDevice()
{
// recreate everything
release();
EGLint result = initialize();
if (result != EGL_SUCCESS)
{
ERR("Could not reinitialize D3D11 device: %08X", result);
return false;
}
// reset device defaults
initializeDevice();
mDeviceLost = false; mDeviceLost = false;
return true; return true;
......
...@@ -196,6 +196,7 @@ class Renderer11 : public Renderer ...@@ -196,6 +196,7 @@ class Renderer11 : public Renderer
void initializeDevice(); void initializeDevice();
void releaseDeviceResources(); void releaseDeviceResources();
int getMinorShaderModel() const; int getMinorShaderModel() const;
void release();
RenderStateCache mStateCache; RenderStateCache mStateCache;
......
...@@ -536,7 +536,23 @@ EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height) ...@@ -536,7 +536,23 @@ EGLint SwapChain11::swapRect(EGLint x, EGLint y, EGLint width, EGLint height)
// Draw // Draw
deviceContext->Draw(4, 0); deviceContext->Draw(4, 0);
mSwapChain->Present(mSwapInterval, 0); result = mSwapChain->Present(mSwapInterval, 0);
if (result == DXGI_ERROR_DEVICE_REMOVED)
{
HRESULT removedReason = device->GetDeviceRemovedReason();
ERR("Present failed: the D3D11 device was removed: 0x%08X", removedReason);
return EGL_CONTEXT_LOST;
}
else if (result == DXGI_ERROR_DEVICE_RESET)
{
ERR("Present failed: the D3D11 device was reset from a bad command.");
return EGL_CONTEXT_LOST;
}
else if (FAILED(result))
{
ERR("Present failed with error code 0x%08X", result);
}
// Unbind // Unbind
static ID3D11ShaderResourceView *const nullSRV = NULL; static ID3D11ShaderResourceView *const nullSRV = NULL;
......
...@@ -235,7 +235,13 @@ TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum ...@@ -235,7 +235,13 @@ TextureStorage11_2D::TextureStorage11_2D(Renderer *renderer, int levels, GLenum
HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture); HRESULT result = device->CreateTexture2D(&desc, NULL, &mTexture);
if (FAILED(result)) // this can happen from windows TDR
if (d3d11::isDeviceLostError(result))
{
mRenderer->notifyDeviceLost();
gl::error(GL_OUT_OF_MEMORY);
}
else if (FAILED(result))
{ {
ASSERT(result == E_OUTOFMEMORY); ASSERT(result == E_OUTOFMEMORY);
ERR("Creating image failed."); ERR("Creating image failed.");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment