src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 15 Jul 2006 09:46:36 +0000
changeset 1907 06c27a737b7a
parent 1905 36d52b1f0504
child 1913 83420da906a5
permissions -rw-r--r--
Streamlined the API a bit and optimized the software renderer.
     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 *SDL_D3D_CreateRenderer(SDL_Window * window,
    31                                             Uint32 flags);
    32 static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
    33                                  SDL_Texture * texture);
    34 static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
    35                                      SDL_Texture * texture,
    36                                      const SDL_Color * colors, int firstcolor,
    37                                      int ncolors);
    38 static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
    39                                      SDL_Texture * texture,
    40                                      SDL_Color * colors, int firstcolor,
    41                                      int ncolors);
    42 static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer,
    43                                  SDL_Texture * texture, const SDL_Rect * rect,
    44                                  const void *pixels, int pitch);
    45 static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    46                                const SDL_Rect * rect, int markDirty,
    47                                void **pixels, int *pitch);
    48 static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer,
    49                                   SDL_Texture * texture);
    50 static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer,
    51                                  SDL_Texture * texture, int numrects,
    52                                  const SDL_Rect * rects);
    53 static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    54                               Uint32 color);
    55 static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    56                               const SDL_Rect * srcrect,
    57                               const SDL_Rect * dstrect, int blendMode,
    58                               int scaleMode);
    59 static void SDL_D3D_RenderPresent(SDL_Renderer * renderer);
    60 static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer,
    61                                    SDL_Texture * texture);
    62 static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
    63 
    64 
    65 SDL_RenderDriver SDL_D3D_RenderDriver = {
    66     SDL_D3D_CreateRenderer,
    67     {
    68      "d3d",
    69      (SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    70       SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    71       SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync),
    72      (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),      /* FIXME */
    73      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),   /* FIXME */
    74      12,
    75      {
    76       SDL_PixelFormat_Index8,
    77       SDL_PixelFormat_RGB332,
    78       SDL_PixelFormat_RGB444,
    79       SDL_PixelFormat_RGB555,
    80       SDL_PixelFormat_ARGB4444,
    81       SDL_PixelFormat_ARGB1555,
    82       SDL_PixelFormat_RGB565,
    83       SDL_PixelFormat_RGB888,
    84       SDL_PixelFormat_ARGB8888,
    85       SDL_PixelFormat_ARGB2101010,
    86       SDL_PixelFormat_UYVY,
    87       SDL_PixelFormat_YUY2},
    88      0,
    89      0}
    90 };
    91 
    92 typedef struct
    93 {
    94     IDirect3DDevice9 *device;
    95     SDL_bool beginScene;
    96 } SDL_D3D_RenderData;
    97 
    98 typedef struct
    99 {
   100     IDirect3DTexture9 *texture;
   101 } SDL_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 }
   188 
   189 static D3DFORMAT
   190 PixelFormatToD3DFMT(Uint32 format)
   191 {
   192     switch (format) {
   193     case SDL_PixelFormat_Index8:
   194         return D3DFMT_P8;
   195     case SDL_PixelFormat_RGB332:
   196         return D3DFMT_R3G3B2;
   197     case SDL_PixelFormat_RGB444:
   198         return D3DFMT_X4R4G4B4;
   199     case SDL_PixelFormat_RGB555:
   200         return D3DFMT_X1R5G5B5;
   201     case SDL_PixelFormat_ARGB4444:
   202         return D3DFMT_A4R4G4B4;
   203     case SDL_PixelFormat_ARGB1555:
   204         return D3DFMT_A1R5G5B5;
   205     case SDL_PixelFormat_RGB565:
   206         return D3DFMT_R5G6B5;
   207     case SDL_PixelFormat_RGB888:
   208         return D3DFMT_X8R8G8B8;
   209     case SDL_PixelFormat_ARGB8888:
   210         return D3DFMT_A8R8G8B8;
   211     case SDL_PixelFormat_ARGB2101010:
   212         return D3DFMT_A2R10G10B10;
   213     case SDL_PixelFormat_UYVY:
   214         return D3DFMT_UYVY;
   215     case SDL_PixelFormat_YUY2:
   216         return D3DFMT_YUY2;
   217     default:
   218         return D3DFMT_UNKNOWN;
   219     }
   220 }
   221 
   222 void
   223 D3D_AddRenderDriver(_THIS)
   224 {
   225     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   226 
   227     if (data->d3d) {
   228         SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
   229     }
   230 }
   231 
   232 SDL_Renderer *
   233 SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   234 {
   235     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   236     SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   237     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   238     SDL_Renderer *renderer;
   239     SDL_D3D_RenderData *data;
   240     HRESULT result;
   241     D3DPRESENT_PARAMETERS pparams;
   242     IDirect3DSwapChain9 *chain;
   243 
   244     renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   245     if (!renderer) {
   246         SDL_OutOfMemory();
   247         return NULL;
   248     }
   249     SDL_zerop(renderer);
   250 
   251     data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
   252     if (!data) {
   253         SDL_D3D_DestroyRenderer(renderer);
   254         SDL_OutOfMemory();
   255         return NULL;
   256     }
   257     SDL_zerop(data);
   258 
   259     renderer->CreateTexture = SDL_D3D_CreateTexture;
   260     renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   261     renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   262     renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   263     renderer->LockTexture = SDL_D3D_LockTexture;
   264     renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   265     renderer->DirtyTexture = SDL_D3D_DirtyTexture;
   266     renderer->RenderFill = SDL_D3D_RenderFill;
   267     renderer->RenderCopy = SDL_D3D_RenderCopy;
   268     renderer->RenderPresent = SDL_D3D_RenderPresent;
   269     renderer->DestroyTexture = SDL_D3D_DestroyTexture;
   270     renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
   271     renderer->info = SDL_D3D_RenderDriver.info;
   272     renderer->window = window->id;
   273     renderer->driverdata = data;
   274 
   275     renderer->info.flags = SDL_Renderer_Accelerated;
   276 
   277     SDL_zero(pparams);
   278     pparams.BackBufferWidth = window->w;
   279     pparams.BackBufferHeight = window->h;
   280     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   281         pparams.BackBufferFormat =
   282             PixelFormatToD3DFMT(display->fullscreen_mode->format);
   283     } else {
   284         pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   285     }
   286     if (flags & SDL_Renderer_PresentFlip2) {
   287         pparams.BackBufferCount = 2;
   288         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   289     } else if (flags & SDL_Renderer_PresentFlip3) {
   290         pparams.BackBufferCount = 3;
   291         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   292     } else if (flags & SDL_Renderer_PresentCopy) {
   293         pparams.BackBufferCount = 1;
   294         pparams.SwapEffect = D3DSWAPEFFECT_COPY;
   295     } else {
   296         pparams.BackBufferCount = 1;
   297         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   298     }
   299     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   300         pparams.Windowed = FALSE;
   301         pparams.FullScreen_RefreshRateInHz =
   302             display->fullscreen_mode->refresh_rate;
   303     } else {
   304         pparams.Windowed = TRUE;
   305         pparams.FullScreen_RefreshRateInHz = 0;
   306     }
   307     if (flags & SDL_Renderer_PresentVSync) {
   308         pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   309     } else {
   310         pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   311     }
   312 
   313     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   314                                      D3DDEVTYPE_HAL,
   315                                      windowdata->hwnd,
   316                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   317                                      &pparams, &data->device);
   318     if (FAILED(result)) {
   319         SDL_D3D_DestroyRenderer(renderer);
   320         D3D_SetError("CreateDevice()", result);
   321         return NULL;
   322     }
   323     data->beginScene = SDL_TRUE;
   324 
   325     /* Get presentation parameters to fill info */
   326     result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   327     if (FAILED(result)) {
   328         SDL_D3D_DestroyRenderer(renderer);
   329         D3D_SetError("GetSwapChain()", result);
   330         return NULL;
   331     }
   332     result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   333     if (FAILED(result)) {
   334         IDirect3DSwapChain9_Release(chain);
   335         SDL_D3D_DestroyRenderer(renderer);
   336         D3D_SetError("GetPresentParameters()", result);
   337         return NULL;
   338     }
   339     IDirect3DSwapChain9_Release(chain);
   340     switch (pparams.SwapEffect) {
   341     case D3DSWAPEFFECT_COPY:
   342         renderer->info.flags |= SDL_Renderer_PresentCopy;
   343         break;
   344     case D3DSWAPEFFECT_FLIP:
   345         switch (pparams.BackBufferCount) {
   346         case 2:
   347             renderer->info.flags |= SDL_Renderer_PresentFlip2;
   348             break;
   349         case 3:
   350             renderer->info.flags |= SDL_Renderer_PresentFlip3;
   351             break;
   352         }
   353         break;
   354     case D3DSWAPEFFECT_DISCARD:
   355         renderer->info.flags |= SDL_Renderer_PresentDiscard;
   356         break;
   357     }
   358     if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   359         renderer->info.flags |= SDL_Renderer_PresentVSync;
   360     }
   361 
   362     /* Set up parameters for rendering */
   363     IDirect3DDevice9_SetVertexShader(data->device, NULL);
   364     IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
   365     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   366                                     D3DCULL_NONE);
   367     IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   368     IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   369                                     TRUE);
   370     IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   371                                     D3DBLEND_SRCALPHA);
   372     IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   373                                     D3DBLEND_INVSRCALPHA);
   374 
   375     return renderer;
   376 }
   377 
   378 static int
   379 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   380 {
   381     SDL_D3D_RenderData *renderdata =
   382         (SDL_D3D_RenderData *) renderer->driverdata;
   383     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   384     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   385     SDL_D3D_TextureData *data;
   386     D3DPOOL pool;
   387     HRESULT result;
   388 
   389     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   390     if (!data) {
   391         SDL_OutOfMemory();
   392         return -1;
   393     }
   394     SDL_zerop(data);
   395 
   396     texture->driverdata = data;
   397 
   398     if (texture->access == SDL_TextureAccess_Local) {
   399         pool = D3DPOOL_MANAGED;
   400     } else {
   401         pool = D3DPOOL_DEFAULT;
   402     }
   403     result =
   404         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   405                                        texture->h, 1, 0,
   406                                        PixelFormatToD3DFMT(texture->format),
   407                                        pool, &data->texture, NULL);
   408     if (FAILED(result)) {
   409         SDL_free(data);
   410         D3D_SetError("CreateTexture()", result);
   411         return -1;
   412     }
   413 
   414     return 0;
   415 }
   416 
   417 static int
   418 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   419                           const SDL_Color * colors, int firstcolor,
   420                           int ncolors)
   421 {
   422     SDL_D3D_RenderData *renderdata =
   423         (SDL_D3D_RenderData *) renderer->driverdata;
   424     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   425 
   426     return 0;
   427 }
   428 
   429 static int
   430 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   431                           SDL_Color * colors, int firstcolor, int ncolors)
   432 {
   433     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   434 
   435     return 0;
   436 }
   437 
   438 static int
   439 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   440                       const SDL_Rect * rect, const void *pixels, int pitch)
   441 {
   442     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   443     SDL_D3D_RenderData *renderdata =
   444         (SDL_D3D_RenderData *) renderer->driverdata;
   445     IDirect3DTexture9 *temp;
   446     RECT d3drect;
   447     D3DLOCKED_RECT locked;
   448     const Uint8 *src;
   449     Uint8 *dst;
   450     int row, length;
   451     HRESULT result;
   452 
   453     result =
   454         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   455                                        texture->h, 1, 0,
   456                                        PixelFormatToD3DFMT(texture->format),
   457                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
   458     if (FAILED(result)) {
   459         D3D_SetError("CreateTexture()", result);
   460         return -1;
   461     }
   462 
   463     d3drect.left = rect->x;
   464     d3drect.right = rect->x + rect->w;
   465     d3drect.top = rect->y;
   466     d3drect.bottom = rect->y + rect->h;
   467 
   468     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   469     if (FAILED(result)) {
   470         IDirect3DTexture9_Release(temp);
   471         D3D_SetError("LockRect()", result);
   472         return -1;
   473     }
   474 
   475     src = pixels;
   476     dst = locked.pBits;
   477     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   478     for (row = 0; row < rect->h; ++row) {
   479         SDL_memcpy(dst, src, length);
   480         src += pitch;
   481         dst += locked.Pitch;
   482     }
   483     IDirect3DTexture9_UnlockRect(temp, 0);
   484 
   485     result =
   486         IDirect3DDevice9_UpdateTexture(renderdata->device,
   487                                        (IDirect3DBaseTexture9 *) temp,
   488                                        (IDirect3DBaseTexture9 *) data->
   489                                        texture);
   490     IDirect3DTexture9_Release(temp);
   491     if (FAILED(result)) {
   492         D3D_SetError("UpdateTexture()", result);
   493         return -1;
   494     }
   495     return 0;
   496 }
   497 
   498 static int
   499 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   500                     const SDL_Rect * rect, int markDirty, void **pixels,
   501                     int *pitch)
   502 {
   503     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   504     RECT d3drect;
   505     D3DLOCKED_RECT locked;
   506     HRESULT result;
   507 
   508     if (texture->access != SDL_TextureAccess_Local) {
   509         SDL_SetError("Can't lock remote video memory");
   510         return -1;
   511     }
   512 
   513     d3drect.left = rect->x;
   514     d3drect.right = rect->x + rect->w;
   515     d3drect.top = rect->y;
   516     d3drect.bottom = rect->y + rect->h;
   517 
   518     result =
   519         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   520                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   521     if (FAILED(result)) {
   522         D3D_SetError("LockRect()", result);
   523         return -1;
   524     }
   525     *pixels = locked.pBits;
   526     *pitch = locked.Pitch;
   527     return 0;
   528 }
   529 
   530 static void
   531 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   532 {
   533     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   534 
   535     IDirect3DTexture9_UnlockRect(data->texture, 0);
   536 }
   537 
   538 static void
   539 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   540                      int numrects, const SDL_Rect * rects)
   541 {
   542     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   543     RECT d3drect;
   544     int i;
   545 
   546     for (i = 0; i < numrects; ++i) {
   547         const SDL_Rect *rect = &rects[i];
   548 
   549         d3drect.left = rect->x;
   550         d3drect.right = rect->x + rect->w;
   551         d3drect.top = rect->y;
   552         d3drect.bottom = rect->y + rect->h;
   553 
   554         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   555     }
   556 }
   557 
   558 static int
   559 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   560                    Uint32 color)
   561 {
   562     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   563     D3DRECT d3drect;
   564     HRESULT result;
   565 
   566     if (data->beginScene) {
   567         IDirect3DDevice9_BeginScene(data->device);
   568         data->beginScene = SDL_FALSE;
   569     }
   570 
   571     d3drect.x1 = rect->x;
   572     d3drect.x2 = rect->x + rect->w;
   573     d3drect.y1 = rect->y;
   574     d3drect.y2 = rect->y + rect->h;
   575 
   576     result =
   577         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
   578                                (D3DCOLOR) color, 1.0f, 0);
   579     if (FAILED(result)) {
   580         D3D_SetError("Clear()", result);
   581         return -1;
   582     }
   583     return 0;
   584 }
   585 
   586 static int
   587 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   588                    const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   589                    int blendMode, int scaleMode)
   590 {
   591     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   592     SDL_D3D_TextureData *texturedata =
   593         (SDL_D3D_TextureData *) texture->driverdata;
   594     float minx, miny, maxx, maxy;
   595     float minu, maxu, minv, maxv;
   596     Vertex vertices[4];
   597     HRESULT result;
   598 
   599     if (data->beginScene) {
   600         IDirect3DDevice9_BeginScene(data->device);
   601         data->beginScene = SDL_FALSE;
   602     }
   603 
   604     minx = (float) dstrect->x - 0.5f;
   605     miny = (float) dstrect->y - 0.5f;
   606     maxx = (float) dstrect->x + dstrect->w - 0.5f;
   607     maxy = (float) dstrect->y + dstrect->h - 0.5f;
   608 
   609     minu = (float) srcrect->x / texture->w;
   610     maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   611     minv = (float) srcrect->y / texture->h;
   612     maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   613 
   614     vertices[0].x = minx;
   615     vertices[0].y = miny;
   616     vertices[0].z = 0.0f;
   617     vertices[0].rhw = 1.0f;
   618     vertices[0].u = minu;
   619     vertices[0].v = minv;
   620 
   621     vertices[1].x = maxx;
   622     vertices[1].y = miny;
   623     vertices[1].z = 0.0f;
   624     vertices[1].rhw = 1.0f;
   625     vertices[1].u = maxu;
   626     vertices[1].v = minv;
   627 
   628     vertices[2].x = maxx;
   629     vertices[2].y = maxy;
   630     vertices[2].z = 0.0f;
   631     vertices[2].rhw = 1.0f;
   632     vertices[2].u = maxu;
   633     vertices[2].v = maxv;
   634 
   635     vertices[3].x = minx;
   636     vertices[3].y = maxy;
   637     vertices[3].z = 0.0f;
   638     vertices[3].rhw = 1.0f;
   639     vertices[3].u = minu;
   640     vertices[3].v = maxv;
   641 
   642     result =
   643         IDirect3DDevice9_SetTexture(data->device, 0,
   644                                     (IDirect3DBaseTexture9 *) texturedata->
   645                                     texture);
   646     if (FAILED(result)) {
   647         D3D_SetError("SetTexture()", result);
   648         return -1;
   649     }
   650     result =
   651         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   652                                          vertices, sizeof(*vertices));
   653     if (FAILED(result)) {
   654         D3D_SetError("DrawPrimitiveUP()", result);
   655         return -1;
   656     }
   657     return 0;
   658 }
   659 
   660 static void
   661 SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   662 {
   663     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   664     HRESULT result;
   665 
   666     if (!data->beginScene) {
   667         IDirect3DDevice9_EndScene(data->device);
   668         data->beginScene = SDL_TRUE;
   669     }
   670 
   671     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   672     if (FAILED(result)) {
   673         D3D_SetError("Present()", result);
   674     }
   675 }
   676 
   677 static void
   678 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   679 {
   680     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   681 
   682     if (!data) {
   683         return;
   684     }
   685     if (data->texture) {
   686         IDirect3DTexture9_Release(data->texture);
   687     }
   688     SDL_free(data);
   689     texture->driverdata = NULL;
   690 }
   691 
   692 void
   693 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
   694 {
   695     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   696 
   697     if (data) {
   698         if (data->device) {
   699             IDirect3DDevice9_Release(data->device);
   700         }
   701         SDL_free(data);
   702     }
   703     SDL_free(renderer);
   704 }
   705 
   706 #endif /* SDL_VIDEO_RENDER_D3D */
   707 
   708 /* vi: set ts=4 sw=4 expandtab: */