src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 18:01:56 +0000
changeset 1921 f3399f779a1d
parent 1920 8a162bfdc838
child 1924 69217fdd2c0a
permissions -rw-r--r--
Bug fixes to the OpenGL 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 *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    31 static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    32 static int D3D_SetTexturePalette(SDL_Renderer * renderer,
    33                                  SDL_Texture * texture,
    34                                  const SDL_Color * colors, int firstcolor,
    35                                  int ncolors);
    36 static int D3D_GetTexturePalette(SDL_Renderer * renderer,
    37                                  SDL_Texture * texture, SDL_Color * colors,
    38                                  int firstcolor, int ncolors);
    39 static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    40                              const SDL_Rect * rect, const void *pixels,
    41                              int pitch);
    42 static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    43                            const SDL_Rect * rect, int markDirty,
    44                            void **pixels, int *pitch);
    45 static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    46 static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    47                              int numrects, const SDL_Rect * rects);
    48 static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    49                           Uint32 color);
    50 static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    51                           const SDL_Rect * srcrect, const SDL_Rect * dstrect,
    52                           int blendMode, int scaleMode);
    53 static void D3D_RenderPresent(SDL_Renderer * renderer);
    54 static void D3D_DestroyTexture(SDL_Renderer * renderer,
    55                                SDL_Texture * texture);
    56 static void D3D_DestroyRenderer(SDL_Renderer * renderer);
    57 
    58 
    59 SDL_RenderDriver D3D_RenderDriver = {
    60     D3D_CreateRenderer,
    61     {
    62      "d3d",
    63      (SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    64       SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    65       SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync |
    66       SDL_Renderer_Accelerated),
    67      (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
    68       SDL_TextureBlendMode_Blend | SDL_TextureBlendMode_Add |
    69       SDL_TextureBlendMode_Mod),
    70      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
    71       SDL_TextureScaleMode_Slow | SDL_TextureScaleMode_Best),
    72      12,
    73      {
    74       SDL_PixelFormat_Index8,
    75       SDL_PixelFormat_RGB332,
    76       SDL_PixelFormat_RGB444,
    77       SDL_PixelFormat_RGB555,
    78       SDL_PixelFormat_ARGB4444,
    79       SDL_PixelFormat_ARGB1555,
    80       SDL_PixelFormat_RGB565,
    81       SDL_PixelFormat_RGB888,
    82       SDL_PixelFormat_ARGB8888,
    83       SDL_PixelFormat_ARGB2101010,
    84       SDL_PixelFormat_UYVY,
    85       SDL_PixelFormat_YUY2},
    86      0,
    87      0}
    88 };
    89 
    90 typedef struct
    91 {
    92     IDirect3DDevice9 *device;
    93     SDL_bool beginScene;
    94 } D3D_RenderData;
    95 
    96 typedef struct
    97 {
    98     IDirect3DTexture9 *texture;
    99 } D3D_TextureData;
   100 
   101 typedef struct
   102 {
   103     float x, y, z;
   104     float rhw;
   105     float u, v;
   106 } Vertex;
   107 
   108 static void
   109 D3D_SetError(const char *prefix, HRESULT result)
   110 {
   111     const char *error;
   112 
   113     switch (result) {
   114     case D3DERR_WRONGTEXTUREFORMAT:
   115         error = "WRONGTEXTUREFORMAT";
   116         break;
   117     case D3DERR_UNSUPPORTEDCOLOROPERATION:
   118         error = "UNSUPPORTEDCOLOROPERATION";
   119         break;
   120     case D3DERR_UNSUPPORTEDCOLORARG:
   121         error = "UNSUPPORTEDCOLORARG";
   122         break;
   123     case D3DERR_UNSUPPORTEDALPHAOPERATION:
   124         error = "UNSUPPORTEDALPHAOPERATION";
   125         break;
   126     case D3DERR_UNSUPPORTEDALPHAARG:
   127         error = "UNSUPPORTEDALPHAARG";
   128         break;
   129     case D3DERR_TOOMANYOPERATIONS:
   130         error = "TOOMANYOPERATIONS";
   131         break;
   132     case D3DERR_CONFLICTINGTEXTUREFILTER:
   133         error = "CONFLICTINGTEXTUREFILTER";
   134         break;
   135     case D3DERR_UNSUPPORTEDFACTORVALUE:
   136         error = "UNSUPPORTEDFACTORVALUE";
   137         break;
   138     case D3DERR_CONFLICTINGRENDERSTATE:
   139         error = "CONFLICTINGRENDERSTATE";
   140         break;
   141     case D3DERR_UNSUPPORTEDTEXTUREFILTER:
   142         error = "UNSUPPORTEDTEXTUREFILTER";
   143         break;
   144     case D3DERR_CONFLICTINGTEXTUREPALETTE:
   145         error = "CONFLICTINGTEXTUREPALETTE";
   146         break;
   147     case D3DERR_DRIVERINTERNALERROR:
   148         error = "DRIVERINTERNALERROR";
   149         break;
   150     case D3DERR_NOTFOUND:
   151         error = "NOTFOUND";
   152         break;
   153     case D3DERR_MOREDATA:
   154         error = "MOREDATA";
   155         break;
   156     case D3DERR_DEVICELOST:
   157         error = "DEVICELOST";
   158         break;
   159     case D3DERR_DEVICENOTRESET:
   160         error = "DEVICENOTRESET";
   161         break;
   162     case D3DERR_NOTAVAILABLE:
   163         error = "NOTAVAILABLE";
   164         break;
   165     case D3DERR_OUTOFVIDEOMEMORY:
   166         error = "OUTOFVIDEOMEMORY";
   167         break;
   168     case D3DERR_INVALIDDEVICE:
   169         error = "INVALIDDEVICE";
   170         break;
   171     case D3DERR_INVALIDCALL:
   172         error = "INVALIDCALL";
   173         break;
   174     case D3DERR_DRIVERINVALIDCALL:
   175         error = "DRIVERINVALIDCALL";
   176         break;
   177     case D3DERR_WASSTILLDRAWING:
   178         error = "WASSTILLDRAWING";
   179         break;
   180     default:
   181         error = "UNKNOWN";
   182         break;
   183     }
   184     SDL_SetError("%s: %s", prefix, error);
   185 }
   186 
   187 static D3DFORMAT
   188 PixelFormatToD3DFMT(Uint32 format)
   189 {
   190     switch (format) {
   191     case SDL_PixelFormat_Index8:
   192         return D3DFMT_P8;
   193     case SDL_PixelFormat_RGB332:
   194         return D3DFMT_R3G3B2;
   195     case SDL_PixelFormat_RGB444:
   196         return D3DFMT_X4R4G4B4;
   197     case SDL_PixelFormat_RGB555:
   198         return D3DFMT_X1R5G5B5;
   199     case SDL_PixelFormat_ARGB4444:
   200         return D3DFMT_A4R4G4B4;
   201     case SDL_PixelFormat_ARGB1555:
   202         return D3DFMT_A1R5G5B5;
   203     case SDL_PixelFormat_RGB565:
   204         return D3DFMT_R5G6B5;
   205     case SDL_PixelFormat_RGB888:
   206         return D3DFMT_X8R8G8B8;
   207     case SDL_PixelFormat_ARGB8888:
   208         return D3DFMT_A8R8G8B8;
   209     case SDL_PixelFormat_ARGB2101010:
   210         return D3DFMT_A2R10G10B10;
   211     case SDL_PixelFormat_UYVY:
   212         return D3DFMT_UYVY;
   213     case SDL_PixelFormat_YUY2:
   214         return D3DFMT_YUY2;
   215     default:
   216         return D3DFMT_UNKNOWN;
   217     }
   218 }
   219 
   220 void
   221 D3D_AddRenderDriver(_THIS)
   222 {
   223     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   224 
   225     if (data->d3d) {
   226         SDL_AddRenderDriver(0, &D3D_RenderDriver);
   227     }
   228 }
   229 
   230 SDL_Renderer *
   231 D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   232 {
   233     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   234     SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   235     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   236     SDL_Renderer *renderer;
   237     D3D_RenderData *data;
   238     HRESULT result;
   239     D3DPRESENT_PARAMETERS pparams;
   240     IDirect3DSwapChain9 *chain;
   241 
   242     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   243     if (!renderer) {
   244         SDL_OutOfMemory();
   245         return NULL;
   246     }
   247 
   248     data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
   249     if (!data) {
   250         D3D_DestroyRenderer(renderer);
   251         SDL_OutOfMemory();
   252         return NULL;
   253     }
   254 
   255     renderer->CreateTexture = D3D_CreateTexture;
   256     renderer->SetTexturePalette = D3D_SetTexturePalette;
   257     renderer->GetTexturePalette = D3D_GetTexturePalette;
   258     renderer->UpdateTexture = D3D_UpdateTexture;
   259     renderer->LockTexture = D3D_LockTexture;
   260     renderer->UnlockTexture = D3D_UnlockTexture;
   261     renderer->DirtyTexture = D3D_DirtyTexture;
   262     renderer->RenderFill = D3D_RenderFill;
   263     renderer->RenderCopy = D3D_RenderCopy;
   264     renderer->RenderPresent = D3D_RenderPresent;
   265     renderer->DestroyTexture = D3D_DestroyTexture;
   266     renderer->DestroyRenderer = D3D_DestroyRenderer;
   267     renderer->info = D3D_RenderDriver.info;
   268     renderer->window = window->id;
   269     renderer->driverdata = data;
   270 
   271     renderer->info.flags = SDL_Renderer_Accelerated;
   272 
   273     SDL_zero(pparams);
   274     pparams.BackBufferWidth = window->w;
   275     pparams.BackBufferHeight = window->h;
   276     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   277         pparams.BackBufferFormat =
   278             PixelFormatToD3DFMT(display->fullscreen_mode->format);
   279     } else {
   280         pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   281     }
   282     if (flags & SDL_Renderer_PresentFlip2) {
   283         pparams.BackBufferCount = 2;
   284         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   285     } else if (flags & SDL_Renderer_PresentFlip3) {
   286         pparams.BackBufferCount = 3;
   287         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   288     } else if (flags & SDL_Renderer_PresentCopy) {
   289         pparams.BackBufferCount = 1;
   290         pparams.SwapEffect = D3DSWAPEFFECT_COPY;
   291     } else {
   292         pparams.BackBufferCount = 1;
   293         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   294     }
   295     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   296         pparams.Windowed = FALSE;
   297         pparams.FullScreen_RefreshRateInHz =
   298             display->fullscreen_mode->refresh_rate;
   299     } else {
   300         pparams.Windowed = TRUE;
   301         pparams.FullScreen_RefreshRateInHz = 0;
   302     }
   303     if (flags & SDL_Renderer_PresentVSync) {
   304         pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   305     } else {
   306         pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   307     }
   308 
   309     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   310                                      D3DDEVTYPE_HAL,
   311                                      windowdata->hwnd,
   312                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   313                                      &pparams, &data->device);
   314     if (FAILED(result)) {
   315         D3D_DestroyRenderer(renderer);
   316         D3D_SetError("CreateDevice()", result);
   317         return NULL;
   318     }
   319     data->beginScene = SDL_TRUE;
   320 
   321     /* Get presentation parameters to fill info */
   322     result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   323     if (FAILED(result)) {
   324         D3D_DestroyRenderer(renderer);
   325         D3D_SetError("GetSwapChain()", result);
   326         return NULL;
   327     }
   328     result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   329     if (FAILED(result)) {
   330         IDirect3DSwapChain9_Release(chain);
   331         D3D_DestroyRenderer(renderer);
   332         D3D_SetError("GetPresentParameters()", result);
   333         return NULL;
   334     }
   335     IDirect3DSwapChain9_Release(chain);
   336     switch (pparams.SwapEffect) {
   337     case D3DSWAPEFFECT_COPY:
   338         renderer->info.flags |= SDL_Renderer_PresentCopy;
   339         break;
   340     case D3DSWAPEFFECT_FLIP:
   341         switch (pparams.BackBufferCount) {
   342         case 2:
   343             renderer->info.flags |= SDL_Renderer_PresentFlip2;
   344             break;
   345         case 3:
   346             renderer->info.flags |= SDL_Renderer_PresentFlip3;
   347             break;
   348         }
   349         break;
   350     case D3DSWAPEFFECT_DISCARD:
   351         renderer->info.flags |= SDL_Renderer_PresentDiscard;
   352         break;
   353     }
   354     if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   355         renderer->info.flags |= SDL_Renderer_PresentVSync;
   356     }
   357 
   358     /* FIXME: Query maximum texture size */
   359 
   360     /* Set up parameters for rendering */
   361     IDirect3DDevice9_SetVertexShader(data->device, NULL);
   362     IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
   363     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   364                                     D3DCULL_NONE);
   365     IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   366 
   367     return renderer;
   368 }
   369 
   370 static int
   371 D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   372 {
   373     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   374     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   375     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   376     D3D_TextureData *data;
   377     D3DPOOL pool;
   378     HRESULT result;
   379 
   380     data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   381     if (!data) {
   382         SDL_OutOfMemory();
   383         return -1;
   384     }
   385 
   386     texture->driverdata = data;
   387 
   388     if (texture->access == SDL_TextureAccess_Local) {
   389         pool = D3DPOOL_MANAGED;
   390     } else {
   391         pool = D3DPOOL_DEFAULT;
   392     }
   393     result =
   394         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   395                                        texture->h, 1, 0,
   396                                        PixelFormatToD3DFMT(texture->format),
   397                                        pool, &data->texture, NULL);
   398     if (FAILED(result)) {
   399         SDL_free(data);
   400         D3D_SetError("CreateTexture()", result);
   401         return -1;
   402     }
   403 
   404     return 0;
   405 }
   406 
   407 static int
   408 D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   409                       const SDL_Color * colors, int firstcolor, int ncolors)
   410 {
   411     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   412     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   413 
   414     return 0;
   415 }
   416 
   417 static int
   418 D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   419                       SDL_Color * colors, int firstcolor, int ncolors)
   420 {
   421     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   422 
   423     return 0;
   424 }
   425 
   426 static int
   427 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   428                   const SDL_Rect * rect, const void *pixels, int pitch)
   429 {
   430     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   431     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   432     IDirect3DTexture9 *temp;
   433     RECT d3drect;
   434     D3DLOCKED_RECT locked;
   435     const Uint8 *src;
   436     Uint8 *dst;
   437     int row, length;
   438     HRESULT result;
   439 
   440     result =
   441         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   442                                        texture->h, 1, 0,
   443                                        PixelFormatToD3DFMT(texture->format),
   444                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
   445     if (FAILED(result)) {
   446         D3D_SetError("CreateTexture()", result);
   447         return -1;
   448     }
   449 
   450     d3drect.left = rect->x;
   451     d3drect.right = rect->x + rect->w;
   452     d3drect.top = rect->y;
   453     d3drect.bottom = rect->y + rect->h;
   454 
   455     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   456     if (FAILED(result)) {
   457         IDirect3DTexture9_Release(temp);
   458         D3D_SetError("LockRect()", result);
   459         return -1;
   460     }
   461 
   462     src = pixels;
   463     dst = locked.pBits;
   464     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   465     for (row = 0; row < rect->h; ++row) {
   466         SDL_memcpy(dst, src, length);
   467         src += pitch;
   468         dst += locked.Pitch;
   469     }
   470     IDirect3DTexture9_UnlockRect(temp, 0);
   471 
   472     result =
   473         IDirect3DDevice9_UpdateTexture(renderdata->device,
   474                                        (IDirect3DBaseTexture9 *) temp,
   475                                        (IDirect3DBaseTexture9 *) data->
   476                                        texture);
   477     IDirect3DTexture9_Release(temp);
   478     if (FAILED(result)) {
   479         D3D_SetError("UpdateTexture()", result);
   480         return -1;
   481     }
   482     return 0;
   483 }
   484 
   485 static int
   486 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   487                 const SDL_Rect * rect, int markDirty, void **pixels,
   488                 int *pitch)
   489 {
   490     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   491     RECT d3drect;
   492     D3DLOCKED_RECT locked;
   493     HRESULT result;
   494 
   495     if (texture->access != SDL_TextureAccess_Local) {
   496         SDL_SetError("Can't lock remote video memory");
   497         return -1;
   498     }
   499 
   500     d3drect.left = rect->x;
   501     d3drect.right = rect->x + rect->w;
   502     d3drect.top = rect->y;
   503     d3drect.bottom = rect->y + rect->h;
   504 
   505     result =
   506         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   507                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   508     if (FAILED(result)) {
   509         D3D_SetError("LockRect()", result);
   510         return -1;
   511     }
   512     *pixels = locked.pBits;
   513     *pitch = locked.Pitch;
   514     return 0;
   515 }
   516 
   517 static void
   518 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   519 {
   520     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   521 
   522     IDirect3DTexture9_UnlockRect(data->texture, 0);
   523 }
   524 
   525 static void
   526 D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   527                  const SDL_Rect * rects)
   528 {
   529     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   530     RECT d3drect;
   531     int i;
   532 
   533     for (i = 0; i < numrects; ++i) {
   534         const SDL_Rect *rect = &rects[i];
   535 
   536         d3drect.left = rect->x;
   537         d3drect.right = rect->x + rect->w;
   538         d3drect.top = rect->y;
   539         d3drect.bottom = rect->y + rect->h;
   540 
   541         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   542     }
   543 }
   544 
   545 static int
   546 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
   547 {
   548     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   549     D3DRECT d3drect;
   550     HRESULT result;
   551 
   552     if (data->beginScene) {
   553         IDirect3DDevice9_BeginScene(data->device);
   554         data->beginScene = SDL_FALSE;
   555     }
   556 
   557     d3drect.x1 = rect->x;
   558     d3drect.x2 = rect->x + rect->w;
   559     d3drect.y1 = rect->y;
   560     d3drect.y2 = rect->y + rect->h;
   561 
   562     result =
   563         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
   564                                (D3DCOLOR) color, 1.0f, 0);
   565     if (FAILED(result)) {
   566         D3D_SetError("Clear()", result);
   567         return -1;
   568     }
   569     return 0;
   570 }
   571 
   572 static int
   573 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   574                const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   575                int blendMode, int scaleMode)
   576 {
   577     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   578     D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   579     float minx, miny, maxx, maxy;
   580     float minu, maxu, minv, maxv;
   581     Vertex vertices[4];
   582     HRESULT result;
   583 
   584     if (data->beginScene) {
   585         IDirect3DDevice9_BeginScene(data->device);
   586         data->beginScene = SDL_FALSE;
   587     }
   588 
   589     minx = (float) dstrect->x - 0.5f;
   590     miny = (float) dstrect->y - 0.5f;
   591     maxx = (float) dstrect->x + dstrect->w - 0.5f;
   592     maxy = (float) dstrect->y + dstrect->h - 0.5f;
   593 
   594     minu = (float) srcrect->x / texture->w;
   595     maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   596     minv = (float) srcrect->y / texture->h;
   597     maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   598 
   599     vertices[0].x = minx;
   600     vertices[0].y = miny;
   601     vertices[0].z = 0.0f;
   602     vertices[0].rhw = 1.0f;
   603     vertices[0].u = minu;
   604     vertices[0].v = minv;
   605 
   606     vertices[1].x = maxx;
   607     vertices[1].y = miny;
   608     vertices[1].z = 0.0f;
   609     vertices[1].rhw = 1.0f;
   610     vertices[1].u = maxu;
   611     vertices[1].v = minv;
   612 
   613     vertices[2].x = maxx;
   614     vertices[2].y = maxy;
   615     vertices[2].z = 0.0f;
   616     vertices[2].rhw = 1.0f;
   617     vertices[2].u = maxu;
   618     vertices[2].v = maxv;
   619 
   620     vertices[3].x = minx;
   621     vertices[3].y = maxy;
   622     vertices[3].z = 0.0f;
   623     vertices[3].rhw = 1.0f;
   624     vertices[3].u = minu;
   625     vertices[3].v = maxv;
   626 
   627     switch (blendMode) {
   628     case SDL_TextureBlendMode_None:
   629         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   630                                         FALSE);
   631         break;
   632     case SDL_TextureBlendMode_Mask:
   633     case SDL_TextureBlendMode_Blend:
   634         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   635                                         TRUE);
   636         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   637                                         D3DBLEND_SRCALPHA);
   638         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   639                                         D3DBLEND_INVSRCALPHA);
   640         break;
   641     case SDL_TextureBlendMode_Add:
   642         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   643                                         TRUE);
   644         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   645                                         D3DBLEND_SRCALPHA);
   646         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   647                                         D3DBLEND_ONE);
   648         break;
   649     case SDL_TextureBlendMode_Mod:
   650         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   651                                         TRUE);
   652         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   653                                         D3DBLEND_ZERO);
   654         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   655                                         D3DBLEND_SRCCOLOR);
   656         break;
   657     }
   658 
   659     switch (scaleMode) {
   660     case SDL_TextureScaleMode_None:
   661     case SDL_TextureScaleMode_Fast:
   662         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   663                                          D3DTEXF_POINT);
   664         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   665                                          D3DTEXF_POINT);
   666         break;
   667     case SDL_TextureScaleMode_Slow:
   668         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   669                                          D3DTEXF_LINEAR);
   670         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   671                                          D3DTEXF_LINEAR);
   672         break;
   673     case SDL_TextureScaleMode_Best:
   674         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   675                                          D3DTEXF_GAUSSIANQUAD);
   676         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   677                                          D3DTEXF_GAUSSIANQUAD);
   678         break;
   679     }
   680 
   681     result =
   682         IDirect3DDevice9_SetTexture(data->device, 0,
   683                                     (IDirect3DBaseTexture9 *) texturedata->
   684                                     texture);
   685     if (FAILED(result)) {
   686         D3D_SetError("SetTexture()", result);
   687         return -1;
   688     }
   689     result =
   690         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   691                                          vertices, sizeof(*vertices));
   692     if (FAILED(result)) {
   693         D3D_SetError("DrawPrimitiveUP()", result);
   694         return -1;
   695     }
   696     return 0;
   697 }
   698 
   699 static void
   700 D3D_RenderPresent(SDL_Renderer * renderer)
   701 {
   702     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   703     HRESULT result;
   704 
   705     if (!data->beginScene) {
   706         IDirect3DDevice9_EndScene(data->device);
   707         data->beginScene = SDL_TRUE;
   708     }
   709 
   710     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   711     if (FAILED(result)) {
   712         D3D_SetError("Present()", result);
   713     }
   714 }
   715 
   716 static void
   717 D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   718 {
   719     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   720 
   721     if (!data) {
   722         return;
   723     }
   724     if (data->texture) {
   725         IDirect3DTexture9_Release(data->texture);
   726     }
   727     SDL_free(data);
   728     texture->driverdata = NULL;
   729 }
   730 
   731 void
   732 D3D_DestroyRenderer(SDL_Renderer * renderer)
   733 {
   734     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   735 
   736     if (data) {
   737         if (data->device) {
   738             IDirect3DDevice9_Release(data->device);
   739         }
   740         SDL_free(data);
   741     }
   742     SDL_free(renderer);
   743 }
   744 
   745 #endif /* SDL_VIDEO_RENDER_D3D */
   746 
   747 /* vi: set ts=4 sw=4 expandtab: */