src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 21:02:57 +0000
changeset 1924 69217fdd2c0a
parent 1921 f3399f779a1d
child 1925 411bfb37082b
permissions -rw-r--r--
If the OpenGL renderer is selected for a non-OpenGL window, recreate the window with OpenGL enabled.
Added OpenGL renderer error checking.
Use fast-path texture formats in 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         D3D_SetError("CreateTexture()", result);
   400         return -1;
   401     }
   402 
   403     return 0;
   404 }
   405 
   406 static int
   407 D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   408                       const SDL_Color * colors, int firstcolor, int ncolors)
   409 {
   410     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   411     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   412 
   413     return 0;
   414 }
   415 
   416 static int
   417 D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   418                       SDL_Color * colors, int firstcolor, int ncolors)
   419 {
   420     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   421 
   422     return 0;
   423 }
   424 
   425 static int
   426 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   427                   const SDL_Rect * rect, const void *pixels, int pitch)
   428 {
   429     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   430     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   431     IDirect3DTexture9 *temp;
   432     RECT d3drect;
   433     D3DLOCKED_RECT locked;
   434     const Uint8 *src;
   435     Uint8 *dst;
   436     int row, length;
   437     HRESULT result;
   438 
   439     result =
   440         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   441                                        texture->h, 1, 0,
   442                                        PixelFormatToD3DFMT(texture->format),
   443                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
   444     if (FAILED(result)) {
   445         D3D_SetError("CreateTexture()", result);
   446         return -1;
   447     }
   448 
   449     d3drect.left = rect->x;
   450     d3drect.right = rect->x + rect->w;
   451     d3drect.top = rect->y;
   452     d3drect.bottom = rect->y + rect->h;
   453 
   454     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   455     if (FAILED(result)) {
   456         IDirect3DTexture9_Release(temp);
   457         D3D_SetError("LockRect()", result);
   458         return -1;
   459     }
   460 
   461     src = pixels;
   462     dst = locked.pBits;
   463     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   464     for (row = 0; row < rect->h; ++row) {
   465         SDL_memcpy(dst, src, length);
   466         src += pitch;
   467         dst += locked.Pitch;
   468     }
   469     IDirect3DTexture9_UnlockRect(temp, 0);
   470 
   471     result =
   472         IDirect3DDevice9_UpdateTexture(renderdata->device,
   473                                        (IDirect3DBaseTexture9 *) temp,
   474                                        (IDirect3DBaseTexture9 *) data->
   475                                        texture);
   476     IDirect3DTexture9_Release(temp);
   477     if (FAILED(result)) {
   478         D3D_SetError("UpdateTexture()", result);
   479         return -1;
   480     }
   481     return 0;
   482 }
   483 
   484 static int
   485 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   486                 const SDL_Rect * rect, int markDirty, void **pixels,
   487                 int *pitch)
   488 {
   489     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   490     RECT d3drect;
   491     D3DLOCKED_RECT locked;
   492     HRESULT result;
   493 
   494     if (texture->access != SDL_TextureAccess_Local) {
   495         SDL_SetError("Can't lock remote video memory");
   496         return -1;
   497     }
   498 
   499     d3drect.left = rect->x;
   500     d3drect.right = rect->x + rect->w;
   501     d3drect.top = rect->y;
   502     d3drect.bottom = rect->y + rect->h;
   503 
   504     result =
   505         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   506                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   507     if (FAILED(result)) {
   508         D3D_SetError("LockRect()", result);
   509         return -1;
   510     }
   511     *pixels = locked.pBits;
   512     *pitch = locked.Pitch;
   513     return 0;
   514 }
   515 
   516 static void
   517 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   518 {
   519     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   520 
   521     IDirect3DTexture9_UnlockRect(data->texture, 0);
   522 }
   523 
   524 static void
   525 D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   526                  const SDL_Rect * rects)
   527 {
   528     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   529     RECT d3drect;
   530     int i;
   531 
   532     for (i = 0; i < numrects; ++i) {
   533         const SDL_Rect *rect = &rects[i];
   534 
   535         d3drect.left = rect->x;
   536         d3drect.right = rect->x + rect->w;
   537         d3drect.top = rect->y;
   538         d3drect.bottom = rect->y + rect->h;
   539 
   540         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   541     }
   542 }
   543 
   544 static int
   545 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
   546 {
   547     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   548     D3DRECT d3drect;
   549     HRESULT result;
   550 
   551     if (data->beginScene) {
   552         IDirect3DDevice9_BeginScene(data->device);
   553         data->beginScene = SDL_FALSE;
   554     }
   555 
   556     d3drect.x1 = rect->x;
   557     d3drect.x2 = rect->x + rect->w;
   558     d3drect.y1 = rect->y;
   559     d3drect.y2 = rect->y + rect->h;
   560 
   561     result =
   562         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
   563                                (D3DCOLOR) color, 1.0f, 0);
   564     if (FAILED(result)) {
   565         D3D_SetError("Clear()", result);
   566         return -1;
   567     }
   568     return 0;
   569 }
   570 
   571 static int
   572 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   573                const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   574                int blendMode, int scaleMode)
   575 {
   576     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   577     D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   578     float minx, miny, maxx, maxy;
   579     float minu, maxu, minv, maxv;
   580     Vertex vertices[4];
   581     HRESULT result;
   582 
   583     if (data->beginScene) {
   584         IDirect3DDevice9_BeginScene(data->device);
   585         data->beginScene = SDL_FALSE;
   586     }
   587 
   588     minx = (float) dstrect->x - 0.5f;
   589     miny = (float) dstrect->y - 0.5f;
   590     maxx = (float) dstrect->x + dstrect->w - 0.5f;
   591     maxy = (float) dstrect->y + dstrect->h - 0.5f;
   592 
   593     minu = (float) srcrect->x / texture->w;
   594     maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   595     minv = (float) srcrect->y / texture->h;
   596     maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   597 
   598     vertices[0].x = minx;
   599     vertices[0].y = miny;
   600     vertices[0].z = 0.0f;
   601     vertices[0].rhw = 1.0f;
   602     vertices[0].u = minu;
   603     vertices[0].v = minv;
   604 
   605     vertices[1].x = maxx;
   606     vertices[1].y = miny;
   607     vertices[1].z = 0.0f;
   608     vertices[1].rhw = 1.0f;
   609     vertices[1].u = maxu;
   610     vertices[1].v = minv;
   611 
   612     vertices[2].x = maxx;
   613     vertices[2].y = maxy;
   614     vertices[2].z = 0.0f;
   615     vertices[2].rhw = 1.0f;
   616     vertices[2].u = maxu;
   617     vertices[2].v = maxv;
   618 
   619     vertices[3].x = minx;
   620     vertices[3].y = maxy;
   621     vertices[3].z = 0.0f;
   622     vertices[3].rhw = 1.0f;
   623     vertices[3].u = minu;
   624     vertices[3].v = maxv;
   625 
   626     switch (blendMode) {
   627     case SDL_TextureBlendMode_None:
   628         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   629                                         FALSE);
   630         break;
   631     case SDL_TextureBlendMode_Mask:
   632     case SDL_TextureBlendMode_Blend:
   633         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   634                                         TRUE);
   635         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   636                                         D3DBLEND_SRCALPHA);
   637         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   638                                         D3DBLEND_INVSRCALPHA);
   639         break;
   640     case SDL_TextureBlendMode_Add:
   641         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   642                                         TRUE);
   643         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   644                                         D3DBLEND_SRCALPHA);
   645         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   646                                         D3DBLEND_ONE);
   647         break;
   648     case SDL_TextureBlendMode_Mod:
   649         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   650                                         TRUE);
   651         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   652                                         D3DBLEND_ZERO);
   653         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   654                                         D3DBLEND_SRCCOLOR);
   655         break;
   656     }
   657 
   658     switch (scaleMode) {
   659     case SDL_TextureScaleMode_None:
   660     case SDL_TextureScaleMode_Fast:
   661         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   662                                          D3DTEXF_POINT);
   663         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   664                                          D3DTEXF_POINT);
   665         break;
   666     case SDL_TextureScaleMode_Slow:
   667         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   668                                          D3DTEXF_LINEAR);
   669         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   670                                          D3DTEXF_LINEAR);
   671         break;
   672     case SDL_TextureScaleMode_Best:
   673         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   674                                          D3DTEXF_GAUSSIANQUAD);
   675         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   676                                          D3DTEXF_GAUSSIANQUAD);
   677         break;
   678     }
   679 
   680     result =
   681         IDirect3DDevice9_SetTexture(data->device, 0,
   682                                     (IDirect3DBaseTexture9 *) texturedata->
   683                                     texture);
   684     if (FAILED(result)) {
   685         D3D_SetError("SetTexture()", result);
   686         return -1;
   687     }
   688     result =
   689         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   690                                          vertices, sizeof(*vertices));
   691     if (FAILED(result)) {
   692         D3D_SetError("DrawPrimitiveUP()", result);
   693         return -1;
   694     }
   695     return 0;
   696 }
   697 
   698 static void
   699 D3D_RenderPresent(SDL_Renderer * renderer)
   700 {
   701     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   702     HRESULT result;
   703 
   704     if (!data->beginScene) {
   705         IDirect3DDevice9_EndScene(data->device);
   706         data->beginScene = SDL_TRUE;
   707     }
   708 
   709     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   710     if (FAILED(result)) {
   711         D3D_SetError("Present()", result);
   712     }
   713 }
   714 
   715 static void
   716 D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   717 {
   718     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   719 
   720     if (!data) {
   721         return;
   722     }
   723     if (data->texture) {
   724         IDirect3DTexture9_Release(data->texture);
   725     }
   726     SDL_free(data);
   727     texture->driverdata = NULL;
   728 }
   729 
   730 void
   731 D3D_DestroyRenderer(SDL_Renderer * renderer)
   732 {
   733     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   734 
   735     if (data) {
   736         if (data->device) {
   737             IDirect3DDevice9_Release(data->device);
   738         }
   739         SDL_free(data);
   740     }
   741     SDL_free(renderer);
   742 }
   743 
   744 #endif /* SDL_VIDEO_RENDER_D3D */
   745 
   746 /* vi: set ts=4 sw=4 expandtab: */