src/video/win32/SDL_d3drender.c
changeset 1975 ccef0d0c40c6
parent 1965 a788656ca29a
child 1976 b1620a317791
     1.1 --- a/src/video/win32/SDL_d3drender.c	Sun Aug 06 23:34:59 2006 +0000
     1.2 +++ b/src/video/win32/SDL_d3drender.c	Mon Aug 07 05:24:13 2006 +0000
     1.3 @@ -28,6 +28,7 @@
     1.4  /* Direct3D renderer implementation */
     1.5  
     1.6  static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
     1.7 +static int D3D_DisplayModeChanged(SDL_Renderer * renderer);
     1.8  static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     1.9  static int D3D_SetTexturePalette(SDL_Renderer * renderer,
    1.10                                   SDL_Texture * texture,
    1.11 @@ -61,7 +62,7 @@
    1.12      {
    1.13       "d3d",
    1.14       (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
    1.15 -      SDL_RENDERER_PRESENTFLIP2 | sDL_RENDERER_PRESENTFLIP3 |
    1.16 +      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
    1.17        SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC |
    1.18        SDL_RENDERER_ACCELERATED),
    1.19       (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
    1.20 @@ -90,6 +91,7 @@
    1.21  typedef struct
    1.22  {
    1.23      IDirect3DDevice9 *device;
    1.24 +    D3DPRESENT_PARAMETERS pparams;
    1.25      SDL_bool beginScene;
    1.26  } D3D_RenderData;
    1.27  
    1.28 @@ -182,6 +184,7 @@
    1.29          break;
    1.30      }
    1.31      SDL_SetError("%s: %s", prefix, error);
    1.32 +    fprintf(stderr, "%s: %s\n", prefix, error);
    1.33  }
    1.34  
    1.35  static D3DFORMAT
    1.36 @@ -253,6 +256,7 @@
    1.37          return NULL;
    1.38      }
    1.39  
    1.40 +    renderer->DisplayModeChanged = D3D_DisplayModeChanged;
    1.41      renderer->CreateTexture = D3D_CreateTexture;
    1.42      renderer->SetTexturePalette = D3D_SetTexturePalette;
    1.43      renderer->GetTexturePalette = D3D_GetTexturePalette;
    1.44 @@ -276,7 +280,7 @@
    1.45      pparams.BackBufferHeight = window->h;
    1.46      if (window->flags & SDL_WINDOW_FULLSCREEN) {
    1.47          pparams.BackBufferFormat =
    1.48 -            PixelFormatToD3DFMT(display->fullscreen_mode->format);
    1.49 +            PixelFormatToD3DFMT(display->fullscreen_mode.format);
    1.50      } else {
    1.51          pparams.BackBufferFormat = D3DFMT_UNKNOWN;
    1.52      }
    1.53 @@ -296,7 +300,7 @@
    1.54      if (window->flags & SDL_WINDOW_FULLSCREEN) {
    1.55          pparams.Windowed = FALSE;
    1.56          pparams.FullScreen_RefreshRateInHz =
    1.57 -            display->fullscreen_mode->refresh_rate;
    1.58 +            display->fullscreen_mode.refresh_rate;
    1.59      } else {
    1.60          pparams.Windowed = TRUE;
    1.61          pparams.FullScreen_RefreshRateInHz = 0;
    1.62 @@ -355,6 +359,7 @@
    1.63      if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
    1.64          renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
    1.65      }
    1.66 +    data->pparams = pparams;
    1.67  
    1.68      IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
    1.69      renderer->info.max_texture_width = caps.MaxTextureWidth;
    1.70 @@ -371,6 +376,48 @@
    1.71  }
    1.72  
    1.73  static int
    1.74 +D3D_Reset(SDL_Renderer * renderer)
    1.75 +{
    1.76 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
    1.77 +    HRESULT result;
    1.78 +
    1.79 +    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
    1.80 +    if (FAILED(result)) {
    1.81 +        if (result == D3DERR_DEVICELOST) {
    1.82 +            /* Don't worry about it, we'll reset later... */
    1.83 +            return 0;
    1.84 +        } else {
    1.85 +            D3D_SetError("Reset()", result);
    1.86 +            return -1;
    1.87 +        }
    1.88 +    }
    1.89 +    IDirect3DDevice9_SetVertexShader(data->device, NULL);
    1.90 +    IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
    1.91 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
    1.92 +                                    D3DCULL_NONE);
    1.93 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
    1.94 +    return 0;
    1.95 +}
    1.96 +
    1.97 +static int
    1.98 +D3D_DisplayModeChanged(SDL_Renderer * renderer)
    1.99 +{
   1.100 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.101 +    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.102 +    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.103 +
   1.104 +    data->pparams.BackBufferWidth = window->w;
   1.105 +    data->pparams.BackBufferHeight = window->h;
   1.106 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.107 +        data->pparams.BackBufferFormat =
   1.108 +            PixelFormatToD3DFMT(display->fullscreen_mode.format);
   1.109 +    } else {
   1.110 +        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   1.111 +    }
   1.112 +    return D3D_Reset(renderer);
   1.113 +}
   1.114 +
   1.115 +static int
   1.116  D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.117  {
   1.118      D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   1.119 @@ -388,6 +435,12 @@
   1.120  
   1.121      texture->driverdata = data;
   1.122  
   1.123 +#if 1
   1.124 +    /* FIXME: Do we want non-managed textures?
   1.125 +       They need to be freed on device reset and then reloaded by the app...
   1.126 +     */
   1.127 +    texture->access = SDL_TEXTUREACCESS_LOCAL;
   1.128 +#endif
   1.129      if (texture->access == SDL_TEXTUREACCESS_LOCAL) {
   1.130          pool = D3DPOOL_MANAGED;
   1.131      } else {
   1.132 @@ -709,6 +762,14 @@
   1.133          data->beginScene = SDL_TRUE;
   1.134      }
   1.135  
   1.136 +    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
   1.137 +    if (result == D3DERR_DEVICELOST) {
   1.138 +        /* We'll reset later */
   1.139 +        return;
   1.140 +    }
   1.141 +    if (result == D3DERR_DEVICENOTRESET) {
   1.142 +        D3D_Reset(renderer);
   1.143 +    }
   1.144      result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   1.145      if (FAILED(result)) {
   1.146          D3D_SetError("Present()", result);
   1.147 @@ -730,7 +791,7 @@
   1.148      texture->driverdata = NULL;
   1.149  }
   1.150  
   1.151 -void
   1.152 +static void
   1.153  D3D_DestroyRenderer(SDL_Renderer * renderer)
   1.154  {
   1.155      D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;