src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 07 Aug 2006 05:24:13 +0000
changeset 1975 ccef0d0c40c6
parent 1965 a788656ca29a
child 1976 b1620a317791
permissions -rw-r--r--
Added resize support for GDI and Direct3D renderers
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #if SDL_VIDEO_RENDER_D3D
    25 
    26 #include "SDL_win32video.h"
    27 
    28 /* Direct3D renderer implementation */
    29 
    30 static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    31 static int D3D_DisplayModeChanged(SDL_Renderer * renderer);
    32 static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    33 static int D3D_SetTexturePalette(SDL_Renderer * renderer,
    34                                  SDL_Texture * texture,
    35                                  const SDL_Color * colors, int firstcolor,
    36                                  int ncolors);
    37 static int D3D_GetTexturePalette(SDL_Renderer * renderer,
    38                                  SDL_Texture * texture, SDL_Color * colors,
    39                                  int firstcolor, int ncolors);
    40 static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    41                              const SDL_Rect * rect, const void *pixels,
    42                              int pitch);
    43 static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    44                            const SDL_Rect * rect, int markDirty,
    45                            void **pixels, int *pitch);
    46 static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    47 static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    48                              int numrects, const SDL_Rect * rects);
    49 static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    50                           Uint32 color);
    51 static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    52                           const SDL_Rect * srcrect, const SDL_Rect * dstrect,
    53                           int blendMode, int scaleMode);
    54 static void D3D_RenderPresent(SDL_Renderer * renderer);
    55 static void D3D_DestroyTexture(SDL_Renderer * renderer,
    56                                SDL_Texture * texture);
    57 static void D3D_DestroyRenderer(SDL_Renderer * renderer);
    58 
    59 
    60 SDL_RenderDriver D3D_RenderDriver = {
    61     D3D_CreateRenderer,
    62     {
    63      "d3d",
    64      (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
    65       SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
    66       SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC |
    67       SDL_RENDERER_ACCELERATED),
    68      (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
    69       SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
    70       SDL_TEXTUREBLENDMODE_MOD),
    71      (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
    72       SDL_TEXTURESCALEMODE_SLOW | SDL_TEXTURESCALEMODE_BEST),
    73      12,
    74      {
    75       SDL_PIXELFORMAT_INDEX8,
    76       SDL_PIXELFORMAT_RGB332,
    77       SDL_PIXELFORMAT_RGB444,
    78       SDL_PIXELFORMAT_RGB555,
    79       SDL_PIXELFORMAT_ARGB4444,
    80       SDL_PIXELFORMAT_ARGB1555,
    81       SDL_PIXELFORMAT_RGB565,
    82       SDL_PIXELFORMAT_RGB888,
    83       SDL_PIXELFORMAT_ARGB8888,
    84       SDL_PIXELFORMAT_ARGB2101010,
    85       SDL_PIXELFORMAT_UYVY,
    86       SDL_PIXELFORMAT_YUY2},
    87      0,
    88      0}
    89 };
    90 
    91 typedef struct
    92 {
    93     IDirect3DDevice9 *device;
    94     D3DPRESENT_PARAMETERS pparams;
    95     SDL_bool beginScene;
    96 } D3D_RenderData;
    97 
    98 typedef struct
    99 {
   100     IDirect3DTexture9 *texture;
   101 } D3D_TextureData;
   102 
   103 typedef struct
   104 {
   105     float x, y, z;
   106     float rhw;
   107     float u, v;
   108 } Vertex;
   109 
   110 static void
   111 D3D_SetError(const char *prefix, HRESULT result)
   112 {
   113     const char *error;
   114 
   115     switch (result) {
   116     case D3DERR_WRONGTEXTUREFORMAT:
   117         error = "WRONGTEXTUREFORMAT";
   118         break;
   119     case D3DERR_UNSUPPORTEDCOLOROPERATION:
   120         error = "UNSUPPORTEDCOLOROPERATION";
   121         break;
   122     case D3DERR_UNSUPPORTEDCOLORARG:
   123         error = "UNSUPPORTEDCOLORARG";
   124         break;
   125     case D3DERR_UNSUPPORTEDALPHAOPERATION:
   126         error = "UNSUPPORTEDALPHAOPERATION";
   127         break;
   128     case D3DERR_UNSUPPORTEDALPHAARG:
   129         error = "UNSUPPORTEDALPHAARG";
   130         break;
   131     case D3DERR_TOOMANYOPERATIONS:
   132         error = "TOOMANYOPERATIONS";
   133         break;
   134     case D3DERR_CONFLICTINGTEXTUREFILTER:
   135         error = "CONFLICTINGTEXTUREFILTER";
   136         break;
   137     case D3DERR_UNSUPPORTEDFACTORVALUE:
   138         error = "UNSUPPORTEDFACTORVALUE";
   139         break;
   140     case D3DERR_CONFLICTINGRENDERSTATE:
   141         error = "CONFLICTINGRENDERSTATE";
   142         break;
   143     case D3DERR_UNSUPPORTEDTEXTUREFILTER:
   144         error = "UNSUPPORTEDTEXTUREFILTER";
   145         break;
   146     case D3DERR_CONFLICTINGTEXTUREPALETTE:
   147         error = "CONFLICTINGTEXTUREPALETTE";
   148         break;
   149     case D3DERR_DRIVERINTERNALERROR:
   150         error = "DRIVERINTERNALERROR";
   151         break;
   152     case D3DERR_NOTFOUND:
   153         error = "NOTFOUND";
   154         break;
   155     case D3DERR_MOREDATA:
   156         error = "MOREDATA";
   157         break;
   158     case D3DERR_DEVICELOST:
   159         error = "DEVICELOST";
   160         break;
   161     case D3DERR_DEVICENOTRESET:
   162         error = "DEVICENOTRESET";
   163         break;
   164     case D3DERR_NOTAVAILABLE:
   165         error = "NOTAVAILABLE";
   166         break;
   167     case D3DERR_OUTOFVIDEOMEMORY:
   168         error = "OUTOFVIDEOMEMORY";
   169         break;
   170     case D3DERR_INVALIDDEVICE:
   171         error = "INVALIDDEVICE";
   172         break;
   173     case D3DERR_INVALIDCALL:
   174         error = "INVALIDCALL";
   175         break;
   176     case D3DERR_DRIVERINVALIDCALL:
   177         error = "DRIVERINVALIDCALL";
   178         break;
   179     case D3DERR_WASSTILLDRAWING:
   180         error = "WASSTILLDRAWING";
   181         break;
   182     default:
   183         error = "UNKNOWN";
   184         break;
   185     }
   186     SDL_SetError("%s: %s", prefix, error);
   187     fprintf(stderr, "%s: %s\n", prefix, error);
   188 }
   189 
   190 static D3DFORMAT
   191 PixelFormatToD3DFMT(Uint32 format)
   192 {
   193     switch (format) {
   194     case SDL_PIXELFORMAT_INDEX8:
   195         return D3DFMT_P8;
   196     case SDL_PIXELFORMAT_RGB332:
   197         return D3DFMT_R3G3B2;
   198     case SDL_PIXELFORMAT_RGB444:
   199         return D3DFMT_X4R4G4B4;
   200     case SDL_PIXELFORMAT_RGB555:
   201         return D3DFMT_X1R5G5B5;
   202     case SDL_PIXELFORMAT_ARGB4444:
   203         return D3DFMT_A4R4G4B4;
   204     case SDL_PIXELFORMAT_ARGB1555:
   205         return D3DFMT_A1R5G5B5;
   206     case SDL_PIXELFORMAT_RGB565:
   207         return D3DFMT_R5G6B5;
   208     case SDL_PIXELFORMAT_RGB888:
   209         return D3DFMT_X8R8G8B8;
   210     case SDL_PIXELFORMAT_ARGB8888:
   211         return D3DFMT_A8R8G8B8;
   212     case SDL_PIXELFORMAT_ARGB2101010:
   213         return D3DFMT_A2R10G10B10;
   214     case SDL_PIXELFORMAT_UYVY:
   215         return D3DFMT_UYVY;
   216     case SDL_PIXELFORMAT_YUY2:
   217         return D3DFMT_YUY2;
   218     default:
   219         return D3DFMT_UNKNOWN;
   220     }
   221 }
   222 
   223 void
   224 D3D_AddRenderDriver(_THIS)
   225 {
   226     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   227 
   228     if (data->d3d) {
   229         SDL_AddRenderDriver(0, &D3D_RenderDriver);
   230     }
   231 }
   232 
   233 SDL_Renderer *
   234 D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   235 {
   236     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   237     SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   238     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   239     SDL_Renderer *renderer;
   240     D3D_RenderData *data;
   241     HRESULT result;
   242     D3DPRESENT_PARAMETERS pparams;
   243     IDirect3DSwapChain9 *chain;
   244     D3DCAPS9 caps;
   245 
   246     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   247     if (!renderer) {
   248         SDL_OutOfMemory();
   249         return NULL;
   250     }
   251 
   252     data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
   253     if (!data) {
   254         D3D_DestroyRenderer(renderer);
   255         SDL_OutOfMemory();
   256         return NULL;
   257     }
   258 
   259     renderer->DisplayModeChanged = D3D_DisplayModeChanged;
   260     renderer->CreateTexture = D3D_CreateTexture;
   261     renderer->SetTexturePalette = D3D_SetTexturePalette;
   262     renderer->GetTexturePalette = D3D_GetTexturePalette;
   263     renderer->UpdateTexture = D3D_UpdateTexture;
   264     renderer->LockTexture = D3D_LockTexture;
   265     renderer->UnlockTexture = D3D_UnlockTexture;
   266     renderer->DirtyTexture = D3D_DirtyTexture;
   267     renderer->RenderFill = D3D_RenderFill;
   268     renderer->RenderCopy = D3D_RenderCopy;
   269     renderer->RenderPresent = D3D_RenderPresent;
   270     renderer->DestroyTexture = D3D_DestroyTexture;
   271     renderer->DestroyRenderer = D3D_DestroyRenderer;
   272     renderer->info = D3D_RenderDriver.info;
   273     renderer->window = window->id;
   274     renderer->driverdata = data;
   275 
   276     renderer->info.flags = SDL_RENDERER_ACCELERATED;
   277 
   278     SDL_zero(pparams);
   279     pparams.BackBufferWidth = window->w;
   280     pparams.BackBufferHeight = window->h;
   281     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   282         pparams.BackBufferFormat =
   283             PixelFormatToD3DFMT(display->fullscreen_mode.format);
   284     } else {
   285         pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   286     }
   287     if (flags & SDL_RENDERER_PRESENTFLIP2) {
   288         pparams.BackBufferCount = 2;
   289         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   290     } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
   291         pparams.BackBufferCount = 3;
   292         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   293     } else if (flags & SDL_RENDERER_PRESENTCOPY) {
   294         pparams.BackBufferCount = 1;
   295         pparams.SwapEffect = D3DSWAPEFFECT_COPY;
   296     } else {
   297         pparams.BackBufferCount = 1;
   298         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   299     }
   300     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   301         pparams.Windowed = FALSE;
   302         pparams.FullScreen_RefreshRateInHz =
   303             display->fullscreen_mode.refresh_rate;
   304     } else {
   305         pparams.Windowed = TRUE;
   306         pparams.FullScreen_RefreshRateInHz = 0;
   307     }
   308     if (flags & SDL_RENDERER_PRESENTVSYNC) {
   309         pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   310     } else {
   311         pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   312     }
   313 
   314     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   315                                      D3DDEVTYPE_HAL,
   316                                      windowdata->hwnd,
   317                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   318                                      &pparams, &data->device);
   319     if (FAILED(result)) {
   320         D3D_DestroyRenderer(renderer);
   321         D3D_SetError("CreateDevice()", result);
   322         return NULL;
   323     }
   324     data->beginScene = SDL_TRUE;
   325 
   326     /* Get presentation parameters to fill info */
   327     result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   328     if (FAILED(result)) {
   329         D3D_DestroyRenderer(renderer);
   330         D3D_SetError("GetSwapChain()", result);
   331         return NULL;
   332     }
   333     result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   334     if (FAILED(result)) {
   335         IDirect3DSwapChain9_Release(chain);
   336         D3D_DestroyRenderer(renderer);
   337         D3D_SetError("GetPresentParameters()", result);
   338         return NULL;
   339     }
   340     IDirect3DSwapChain9_Release(chain);
   341     switch (pparams.SwapEffect) {
   342     case D3DSWAPEFFECT_COPY:
   343         renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
   344         break;
   345     case D3DSWAPEFFECT_FLIP:
   346         switch (pparams.BackBufferCount) {
   347         case 2:
   348             renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
   349             break;
   350         case 3:
   351             renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
   352             break;
   353         }
   354         break;
   355     case D3DSWAPEFFECT_DISCARD:
   356         renderer->info.flags |= SDL_RENDERER_PRESENTDISCARD;
   357         break;
   358     }
   359     if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   360         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   361     }
   362     data->pparams = pparams;
   363 
   364     IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
   365     renderer->info.max_texture_width = caps.MaxTextureWidth;
   366     renderer->info.max_texture_height = caps.MaxTextureHeight;
   367 
   368     /* Set up parameters for rendering */
   369     IDirect3DDevice9_SetVertexShader(data->device, NULL);
   370     IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
   371     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   372                                     D3DCULL_NONE);
   373     IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   374 
   375     return renderer;
   376 }
   377 
   378 static int
   379 D3D_Reset(SDL_Renderer * renderer)
   380 {
   381     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   382     HRESULT result;
   383 
   384     result = IDirect3DDevice9_Reset(data->device, &data->pparams);
   385     if (FAILED(result)) {
   386         if (result == D3DERR_DEVICELOST) {
   387             /* Don't worry about it, we'll reset later... */
   388             return 0;
   389         } else {
   390             D3D_SetError("Reset()", result);
   391             return -1;
   392         }
   393     }
   394     IDirect3DDevice9_SetVertexShader(data->device, NULL);
   395     IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
   396     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   397                                     D3DCULL_NONE);
   398     IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   399     return 0;
   400 }
   401 
   402 static int
   403 D3D_DisplayModeChanged(SDL_Renderer * renderer)
   404 {
   405     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   406     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   407     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   408 
   409     data->pparams.BackBufferWidth = window->w;
   410     data->pparams.BackBufferHeight = window->h;
   411     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   412         data->pparams.BackBufferFormat =
   413             PixelFormatToD3DFMT(display->fullscreen_mode.format);
   414     } else {
   415         data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   416     }
   417     return D3D_Reset(renderer);
   418 }
   419 
   420 static int
   421 D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   422 {
   423     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   424     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   425     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   426     D3D_TextureData *data;
   427     D3DPOOL pool;
   428     HRESULT result;
   429 
   430     data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   431     if (!data) {
   432         SDL_OutOfMemory();
   433         return -1;
   434     }
   435 
   436     texture->driverdata = data;
   437 
   438 #if 1
   439     /* FIXME: Do we want non-managed textures?
   440        They need to be freed on device reset and then reloaded by the app...
   441      */
   442     texture->access = SDL_TEXTUREACCESS_LOCAL;
   443 #endif
   444     if (texture->access == SDL_TEXTUREACCESS_LOCAL) {
   445         pool = D3DPOOL_MANAGED;
   446     } else {
   447         pool = D3DPOOL_DEFAULT;
   448     }
   449     result =
   450         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   451                                        texture->h, 1, 0,
   452                                        PixelFormatToD3DFMT(texture->format),
   453                                        pool, &data->texture, NULL);
   454     if (FAILED(result)) {
   455         D3D_SetError("CreateTexture()", result);
   456         return -1;
   457     }
   458 
   459     return 0;
   460 }
   461 
   462 static int
   463 D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   464                       const SDL_Color * colors, int firstcolor, int ncolors)
   465 {
   466     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   467     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   468 
   469     return 0;
   470 }
   471 
   472 static int
   473 D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   474                       SDL_Color * colors, int firstcolor, int ncolors)
   475 {
   476     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   477 
   478     return 0;
   479 }
   480 
   481 static int
   482 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   483                   const SDL_Rect * rect, const void *pixels, int pitch)
   484 {
   485     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   486     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   487     IDirect3DTexture9 *temp;
   488     RECT d3drect;
   489     D3DLOCKED_RECT locked;
   490     const Uint8 *src;
   491     Uint8 *dst;
   492     int row, length;
   493     HRESULT result;
   494 
   495     result =
   496         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   497                                        texture->h, 1, 0,
   498                                        PixelFormatToD3DFMT(texture->format),
   499                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
   500     if (FAILED(result)) {
   501         D3D_SetError("CreateTexture()", result);
   502         return -1;
   503     }
   504 
   505     d3drect.left = rect->x;
   506     d3drect.right = rect->x + rect->w;
   507     d3drect.top = rect->y;
   508     d3drect.bottom = rect->y + rect->h;
   509 
   510     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   511     if (FAILED(result)) {
   512         IDirect3DTexture9_Release(temp);
   513         D3D_SetError("LockRect()", result);
   514         return -1;
   515     }
   516 
   517     src = pixels;
   518     dst = locked.pBits;
   519     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   520     for (row = 0; row < rect->h; ++row) {
   521         SDL_memcpy(dst, src, length);
   522         src += pitch;
   523         dst += locked.Pitch;
   524     }
   525     IDirect3DTexture9_UnlockRect(temp, 0);
   526 
   527     result =
   528         IDirect3DDevice9_UpdateTexture(renderdata->device,
   529                                        (IDirect3DBaseTexture9 *) temp,
   530                                        (IDirect3DBaseTexture9 *) data->
   531                                        texture);
   532     IDirect3DTexture9_Release(temp);
   533     if (FAILED(result)) {
   534         D3D_SetError("UpdateTexture()", result);
   535         return -1;
   536     }
   537     return 0;
   538 }
   539 
   540 static int
   541 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   542                 const SDL_Rect * rect, int markDirty, void **pixels,
   543                 int *pitch)
   544 {
   545     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   546     RECT d3drect;
   547     D3DLOCKED_RECT locked;
   548     HRESULT result;
   549 
   550     if (texture->access != SDL_TEXTUREACCESS_LOCAL) {
   551         SDL_SetError("Can't lock remote video memory");
   552         return -1;
   553     }
   554 
   555     d3drect.left = rect->x;
   556     d3drect.right = rect->x + rect->w;
   557     d3drect.top = rect->y;
   558     d3drect.bottom = rect->y + rect->h;
   559 
   560     result =
   561         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   562                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   563     if (FAILED(result)) {
   564         D3D_SetError("LockRect()", result);
   565         return -1;
   566     }
   567     *pixels = locked.pBits;
   568     *pitch = locked.Pitch;
   569     return 0;
   570 }
   571 
   572 static void
   573 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   574 {
   575     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   576 
   577     IDirect3DTexture9_UnlockRect(data->texture, 0);
   578 }
   579 
   580 static void
   581 D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   582                  const SDL_Rect * rects)
   583 {
   584     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   585     RECT d3drect;
   586     int i;
   587 
   588     for (i = 0; i < numrects; ++i) {
   589         const SDL_Rect *rect = &rects[i];
   590 
   591         d3drect.left = rect->x;
   592         d3drect.right = rect->x + rect->w;
   593         d3drect.top = rect->y;
   594         d3drect.bottom = rect->y + rect->h;
   595 
   596         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   597     }
   598 }
   599 
   600 static int
   601 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
   602 {
   603     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   604     D3DRECT d3drect;
   605     HRESULT result;
   606 
   607     if (data->beginScene) {
   608         IDirect3DDevice9_BeginScene(data->device);
   609         data->beginScene = SDL_FALSE;
   610     }
   611 
   612     d3drect.x1 = rect->x;
   613     d3drect.x2 = rect->x + rect->w;
   614     d3drect.y1 = rect->y;
   615     d3drect.y2 = rect->y + rect->h;
   616 
   617     result =
   618         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
   619                                (D3DCOLOR) color, 1.0f, 0);
   620     if (FAILED(result)) {
   621         D3D_SetError("Clear()", result);
   622         return -1;
   623     }
   624     return 0;
   625 }
   626 
   627 static int
   628 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   629                const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   630                int blendMode, int scaleMode)
   631 {
   632     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   633     D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   634     float minx, miny, maxx, maxy;
   635     float minu, maxu, minv, maxv;
   636     Vertex vertices[4];
   637     HRESULT result;
   638 
   639     if (data->beginScene) {
   640         IDirect3DDevice9_BeginScene(data->device);
   641         data->beginScene = SDL_FALSE;
   642     }
   643 
   644     minx = (float) dstrect->x - 0.5f;
   645     miny = (float) dstrect->y - 0.5f;
   646     maxx = (float) dstrect->x + dstrect->w - 0.5f;
   647     maxy = (float) dstrect->y + dstrect->h - 0.5f;
   648 
   649     minu = (float) srcrect->x / texture->w;
   650     maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   651     minv = (float) srcrect->y / texture->h;
   652     maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   653 
   654     vertices[0].x = minx;
   655     vertices[0].y = miny;
   656     vertices[0].z = 0.0f;
   657     vertices[0].rhw = 1.0f;
   658     vertices[0].u = minu;
   659     vertices[0].v = minv;
   660 
   661     vertices[1].x = maxx;
   662     vertices[1].y = miny;
   663     vertices[1].z = 0.0f;
   664     vertices[1].rhw = 1.0f;
   665     vertices[1].u = maxu;
   666     vertices[1].v = minv;
   667 
   668     vertices[2].x = maxx;
   669     vertices[2].y = maxy;
   670     vertices[2].z = 0.0f;
   671     vertices[2].rhw = 1.0f;
   672     vertices[2].u = maxu;
   673     vertices[2].v = maxv;
   674 
   675     vertices[3].x = minx;
   676     vertices[3].y = maxy;
   677     vertices[3].z = 0.0f;
   678     vertices[3].rhw = 1.0f;
   679     vertices[3].u = minu;
   680     vertices[3].v = maxv;
   681 
   682     switch (blendMode) {
   683     case SDL_TEXTUREBLENDMODE_NONE:
   684         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   685                                         FALSE);
   686         break;
   687     case SDL_TEXTUREBLENDMODE_MASK:
   688     case SDL_TEXTUREBLENDMODE_BLEND:
   689         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   690                                         TRUE);
   691         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   692                                         D3DBLEND_SRCALPHA);
   693         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   694                                         D3DBLEND_INVSRCALPHA);
   695         break;
   696     case SDL_TEXTUREBLENDMODE_ADD:
   697         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   698                                         TRUE);
   699         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   700                                         D3DBLEND_SRCALPHA);
   701         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   702                                         D3DBLEND_ONE);
   703         break;
   704     case SDL_TEXTUREBLENDMODE_MOD:
   705         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   706                                         TRUE);
   707         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   708                                         D3DBLEND_ZERO);
   709         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   710                                         D3DBLEND_SRCCOLOR);
   711         break;
   712     }
   713 
   714     switch (scaleMode) {
   715     case SDL_TEXTURESCALEMODE_NONE:
   716     case SDL_TEXTURESCALEMODE_FAST:
   717         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   718                                          D3DTEXF_POINT);
   719         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   720                                          D3DTEXF_POINT);
   721         break;
   722     case SDL_TEXTURESCALEMODE_SLOW:
   723         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   724                                          D3DTEXF_LINEAR);
   725         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   726                                          D3DTEXF_LINEAR);
   727         break;
   728     case SDL_TEXTURESCALEMODE_BEST:
   729         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   730                                          D3DTEXF_GAUSSIANQUAD);
   731         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   732                                          D3DTEXF_GAUSSIANQUAD);
   733         break;
   734     }
   735 
   736     result =
   737         IDirect3DDevice9_SetTexture(data->device, 0,
   738                                     (IDirect3DBaseTexture9 *) texturedata->
   739                                     texture);
   740     if (FAILED(result)) {
   741         D3D_SetError("SetTexture()", result);
   742         return -1;
   743     }
   744     result =
   745         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   746                                          vertices, sizeof(*vertices));
   747     if (FAILED(result)) {
   748         D3D_SetError("DrawPrimitiveUP()", result);
   749         return -1;
   750     }
   751     return 0;
   752 }
   753 
   754 static void
   755 D3D_RenderPresent(SDL_Renderer * renderer)
   756 {
   757     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   758     HRESULT result;
   759 
   760     if (!data->beginScene) {
   761         IDirect3DDevice9_EndScene(data->device);
   762         data->beginScene = SDL_TRUE;
   763     }
   764 
   765     result = IDirect3DDevice9_TestCooperativeLevel(data->device);
   766     if (result == D3DERR_DEVICELOST) {
   767         /* We'll reset later */
   768         return;
   769     }
   770     if (result == D3DERR_DEVICENOTRESET) {
   771         D3D_Reset(renderer);
   772     }
   773     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   774     if (FAILED(result)) {
   775         D3D_SetError("Present()", result);
   776     }
   777 }
   778 
   779 static void
   780 D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   781 {
   782     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   783 
   784     if (!data) {
   785         return;
   786     }
   787     if (data->texture) {
   788         IDirect3DTexture9_Release(data->texture);
   789     }
   790     SDL_free(data);
   791     texture->driverdata = NULL;
   792 }
   793 
   794 static void
   795 D3D_DestroyRenderer(SDL_Renderer * renderer)
   796 {
   797     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   798 
   799     if (data) {
   800         if (data->device) {
   801             IDirect3DDevice9_Release(data->device);
   802         }
   803         SDL_free(data);
   804     }
   805     SDL_free(renderer);
   806 }
   807 
   808 #endif /* SDL_VIDEO_RENDER_D3D */
   809 
   810 /* vi: set ts=4 sw=4 expandtab: */