Skip to content

Commit

Permalink
D3D11: Added code to handle GPU-device-removed scenarios
Browse files Browse the repository at this point in the history
These scenarios can happen when a GPU is switched, its driver updated, or in
some virtual machines (such as Parallels) are suspended and then resumed.  In
these cases, all GPU resources will already be lost, and it's up to the app to
recover.

For now, SDL's D3D11 renderer will handle this by freeing all GPU resources,
including all textures, and then sending a SDL_RENDER_TARGETS_RESET event.
It's currently up to an app to intercept this event, destroy all of its
textures, then recreate them from scratch.
  • Loading branch information
DavidLudwig committed Mar 23, 2014
1 parent c3d1037 commit 6ce684e
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions src/render/direct3d11/SDL_render_d3d11.c
Expand Up @@ -846,9 +846,10 @@ D3D11_CreateRenderer(SDL_Window * window, Uint32 flags)
}

static void
D3D11_DestroyRenderer(SDL_Renderer * renderer)
D3D11_ReleaseAll(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
SDL_Texture *texture = NULL;

if (data) {
SAFE_RELEASE(data->dxgiFactory);
Expand Down Expand Up @@ -879,6 +880,29 @@ D3D11_DestroyRenderer(SDL_Renderer * renderer)
if (data->hDXGIMod) {
SDL_UnloadObject(data->hDXGIMod);
}

data->swapEffect = (DXGI_SWAP_EFFECT) 0;
data->rotation = DXGI_MODE_ROTATION_UNSPECIFIED;
data->currentRenderTargetView = NULL;
data->currentRasterizerState = NULL;
data->currentBlendState = NULL;
data->currentShader = NULL;
data->currentShaderResource = NULL;
data->currentSampler = NULL;
}

/* Release all textures */
for (texture = renderer->textures; texture; texture = texture->next) {
D3D11_DestroyTexture(renderer, texture);
}
}

static void
D3D11_DestroyRenderer(SDL_Renderer * renderer)
{
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
D3D11_ReleaseAll(renderer);
if (data) {
SDL_free(data);
}
SDL_free(renderer);
Expand Down Expand Up @@ -1463,7 +1487,15 @@ D3D11_CreateWindowSizeDependentResources(SDL_Renderer * renderer)
DXGI_FORMAT_UNKNOWN,
0
);
if (FAILED(result)) {
if (result == DXGI_ERROR_DEVICE_REMOVED) {
/* If the device was removed for any reason, a new device and swap chain will need to be created. */
D3D11_HandleDeviceLost(renderer);

/* Everything is set up now. Do not continue execution of this method. HandleDeviceLost will reenter this method
* and correctly set up the new device.
*/
goto done;
} else if (FAILED(result)) {
WIN_SetErrorFromHRESULT(__FUNCTION__ ", IDXGISwapChain::ResizeBuffers", result);
goto done;
}
Expand Down Expand Up @@ -1537,7 +1569,7 @@ D3D11_HandleDeviceLost(SDL_Renderer * renderer)
D3D11_RenderData *data = (D3D11_RenderData *) renderer->driverdata;
HRESULT result = S_OK;

/* FIXME: Need to release all resources - all textures are invalid! */
D3D11_ReleaseAll(renderer);

result = D3D11_CreateDeviceResources(renderer);
if (FAILED(result)) {
Expand All @@ -1551,6 +1583,14 @@ D3D11_HandleDeviceLost(SDL_Renderer * renderer)
return result;
}

/* Let the application know that the device has been reset */
{
/* TODO/FIXME: consider adding a new SDL event to indicate that the entire rendering device has been reset, not just render targets! */
SDL_Event event;
event.type = SDL_RENDER_TARGETS_RESET;
SDL_PushEvent(&event);
}

return S_OK;
}

Expand Down

0 comments on commit 6ce684e

Please sign in to comment.