Recreate render target textures when the D3D device is being reset, and notify the application using the SDL_RENDER_TARGETS_RESET event when this happens.
authorSam Lantinga <slouken@libsdl.org>
Mon, 10 Feb 2014 10:02:51 -0800
changeset 8207f06add42160c
parent 8206 6e59aa7b53af
child 8208 e749005cfd72
Recreate render target textures when the D3D device is being reset, and notify the application using the SDL_RENDER_TARGETS_RESET event when this happens.
include/SDL_events.h
src/render/direct3d/SDL_render_d3d.c
     1.1 --- a/include/SDL_events.h	Mon Feb 10 10:02:42 2014 -0800
     1.2 +++ b/include/SDL_events.h	Mon Feb 10 10:02:51 2014 -0800
     1.3 @@ -134,6 +134,9 @@
     1.4      /* Drag and drop events */
     1.5      SDL_DROPFILE        = 0x1000, /**< The system requests a file open */
     1.6  
     1.7 +    /* Render events */
     1.8 +    SDL_RENDER_TARGETS_RESET = 0x2000, /**< The render targets have been reset */
     1.9 +
    1.10      /** Events ::SDL_USEREVENT through ::SDL_LASTEVENT are for your use,
    1.11       *  and should be allocated with SDL_RegisterEvents()
    1.12       */
     2.1 --- a/src/render/direct3d/SDL_render_d3d.c	Mon Feb 10 10:02:42 2014 -0800
     2.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Mon Feb 10 10:02:51 2014 -0800
     2.3 @@ -474,6 +474,7 @@
     2.4  {
     2.5      D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
     2.6      HRESULT result;
     2.7 +    SDL_Texture *texture;
     2.8  
     2.9      /* Release the default render target before reset */
    2.10      if (data->defaultRenderTarget) {
    2.11 @@ -481,6 +482,13 @@
    2.12          data->defaultRenderTarget = NULL;
    2.13      }
    2.14  
    2.15 +    /* Release application render targets */
    2.16 +    for (texture = renderer->textures; texture; texture = texture->next) {
    2.17 +        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
    2.18 +            D3D_DestroyTexture(renderer, texture);
    2.19 +        }
    2.20 +    }
    2.21 +
    2.22      result = IDirect3DDevice9_Reset(data->device, &data->pparams);
    2.23      if (FAILED(result)) {
    2.24          if (result == D3DERR_DEVICELOST) {
    2.25 @@ -491,9 +499,24 @@
    2.26          }
    2.27      }
    2.28  
    2.29 +    /* Allocate application render targets */
    2.30 +    for (texture = renderer->textures; texture; texture = texture->next) {
    2.31 +        if (texture->access == SDL_TEXTUREACCESS_TARGET) {
    2.32 +            D3D_CreateTexture(renderer, texture);
    2.33 +        }
    2.34 +    }
    2.35 +
    2.36      IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
    2.37      D3D_InitRenderState(data);
    2.38      D3D_UpdateViewport(renderer);
    2.39 +
    2.40 +    /* Let the application know that render targets were reset */
    2.41 +    {
    2.42 +        SDL_Event event;
    2.43 +        event.type = SDL_RENDER_TARGETS_RESET;
    2.44 +        SDL_PushEvent(&event);
    2.45 +    }
    2.46 +
    2.47      return 0;
    2.48  }
    2.49  
    2.50 @@ -570,7 +593,7 @@
    2.51          return NULL;
    2.52      }
    2.53  
    2.54 -    if( D3D_LoadDLL( &data->d3dDLL, &data->d3d ) ) {
    2.55 +    if (D3D_LoadDLL(&data->d3dDLL, &data->d3d)) {
    2.56          for (d3dxVersion=50;d3dxVersion>0;d3dxVersion--) {
    2.57              LPTSTR dllName;
    2.58              SDL_snprintf(d3dxDLLFile, sizeof(d3dxDLLFile), "D3DX9_%02d.dll", d3dxVersion);
    2.59 @@ -644,13 +667,12 @@
    2.60      pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
    2.61  
    2.62      if (window_flags & SDL_WINDOW_FULLSCREEN) {
    2.63 -        if ( ( window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) == SDL_WINDOW_FULLSCREEN_DESKTOP )  {
    2.64 +        if ((window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)  {
    2.65              pparams.Windowed = TRUE;
    2.66              pparams.FullScreen_RefreshRateInHz = 0;
    2.67          } else {
    2.68 -        pparams.Windowed = FALSE;
    2.69 -        pparams.FullScreen_RefreshRateInHz =
    2.70 -            fullscreen_mode.refresh_rate;
    2.71 +            pparams.Windowed = FALSE;
    2.72 +            pparams.FullScreen_RefreshRateInHz = fullscreen_mode.refresh_rate;
    2.73          }
    2.74      } else {
    2.75          pparams.Windowed = TRUE;
    2.76 @@ -663,8 +685,8 @@
    2.77      }
    2.78  
    2.79      /* Get the adapter for the display that the window is on */
    2.80 -    displayIndex = SDL_GetWindowDisplayIndex( window );
    2.81 -    data->adapter = SDL_Direct3D9GetAdapterIndex( displayIndex );
    2.82 +    displayIndex = SDL_GetWindowDisplayIndex(window);
    2.83 +    data->adapter = SDL_Direct3D9GetAdapterIndex(displayIndex);
    2.84  
    2.85      IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
    2.86  
    2.87 @@ -1912,7 +1934,7 @@
    2.88  
    2.89      device = data->device;
    2.90      if (device) {
    2.91 -        IDirect3DDevice9_AddRef( device );
    2.92 +        IDirect3DDevice9_AddRef(device);
    2.93      }
    2.94  #endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
    2.95