src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Aug 2006 17:09:42 +0000
changeset 1965 a788656ca29a
parent 1925 411bfb37082b
child 1975 ccef0d0c40c6
permissions -rw-r--r--
SDL constants are all uppercase.
     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     D3DCAPS9 caps;
   242 
   243     renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   244     if (!renderer) {
   245         SDL_OutOfMemory();
   246         return NULL;
   247     }
   248 
   249     data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
   250     if (!data) {
   251         D3D_DestroyRenderer(renderer);
   252         SDL_OutOfMemory();
   253         return NULL;
   254     }
   255 
   256     renderer->CreateTexture = D3D_CreateTexture;
   257     renderer->SetTexturePalette = D3D_SetTexturePalette;
   258     renderer->GetTexturePalette = D3D_GetTexturePalette;
   259     renderer->UpdateTexture = D3D_UpdateTexture;
   260     renderer->LockTexture = D3D_LockTexture;
   261     renderer->UnlockTexture = D3D_UnlockTexture;
   262     renderer->DirtyTexture = D3D_DirtyTexture;
   263     renderer->RenderFill = D3D_RenderFill;
   264     renderer->RenderCopy = D3D_RenderCopy;
   265     renderer->RenderPresent = D3D_RenderPresent;
   266     renderer->DestroyTexture = D3D_DestroyTexture;
   267     renderer->DestroyRenderer = D3D_DestroyRenderer;
   268     renderer->info = D3D_RenderDriver.info;
   269     renderer->window = window->id;
   270     renderer->driverdata = data;
   271 
   272     renderer->info.flags = SDL_RENDERER_ACCELERATED;
   273 
   274     SDL_zero(pparams);
   275     pparams.BackBufferWidth = window->w;
   276     pparams.BackBufferHeight = window->h;
   277     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   278         pparams.BackBufferFormat =
   279             PixelFormatToD3DFMT(display->fullscreen_mode->format);
   280     } else {
   281         pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   282     }
   283     if (flags & SDL_RENDERER_PRESENTFLIP2) {
   284         pparams.BackBufferCount = 2;
   285         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   286     } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
   287         pparams.BackBufferCount = 3;
   288         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   289     } else if (flags & SDL_RENDERER_PRESENTCOPY) {
   290         pparams.BackBufferCount = 1;
   291         pparams.SwapEffect = D3DSWAPEFFECT_COPY;
   292     } else {
   293         pparams.BackBufferCount = 1;
   294         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   295     }
   296     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   297         pparams.Windowed = FALSE;
   298         pparams.FullScreen_RefreshRateInHz =
   299             display->fullscreen_mode->refresh_rate;
   300     } else {
   301         pparams.Windowed = TRUE;
   302         pparams.FullScreen_RefreshRateInHz = 0;
   303     }
   304     if (flags & SDL_RENDERER_PRESENTVSYNC) {
   305         pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   306     } else {
   307         pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   308     }
   309 
   310     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   311                                      D3DDEVTYPE_HAL,
   312                                      windowdata->hwnd,
   313                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   314                                      &pparams, &data->device);
   315     if (FAILED(result)) {
   316         D3D_DestroyRenderer(renderer);
   317         D3D_SetError("CreateDevice()", result);
   318         return NULL;
   319     }
   320     data->beginScene = SDL_TRUE;
   321 
   322     /* Get presentation parameters to fill info */
   323     result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   324     if (FAILED(result)) {
   325         D3D_DestroyRenderer(renderer);
   326         D3D_SetError("GetSwapChain()", result);
   327         return NULL;
   328     }
   329     result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   330     if (FAILED(result)) {
   331         IDirect3DSwapChain9_Release(chain);
   332         D3D_DestroyRenderer(renderer);
   333         D3D_SetError("GetPresentParameters()", result);
   334         return NULL;
   335     }
   336     IDirect3DSwapChain9_Release(chain);
   337     switch (pparams.SwapEffect) {
   338     case D3DSWAPEFFECT_COPY:
   339         renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
   340         break;
   341     case D3DSWAPEFFECT_FLIP:
   342         switch (pparams.BackBufferCount) {
   343         case 2:
   344             renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
   345             break;
   346         case 3:
   347             renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
   348             break;
   349         }
   350         break;
   351     case D3DSWAPEFFECT_DISCARD:
   352         renderer->info.flags |= SDL_RENDERER_PRESENTDISCARD;
   353         break;
   354     }
   355     if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   356         renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   357     }
   358 
   359     IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
   360     renderer->info.max_texture_width = caps.MaxTextureWidth;
   361     renderer->info.max_texture_height = caps.MaxTextureHeight;
   362 
   363     /* Set up parameters for rendering */
   364     IDirect3DDevice9_SetVertexShader(data->device, NULL);
   365     IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
   366     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   367                                     D3DCULL_NONE);
   368     IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   369 
   370     return renderer;
   371 }
   372 
   373 static int
   374 D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   375 {
   376     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   377     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   378     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   379     D3D_TextureData *data;
   380     D3DPOOL pool;
   381     HRESULT result;
   382 
   383     data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   384     if (!data) {
   385         SDL_OutOfMemory();
   386         return -1;
   387     }
   388 
   389     texture->driverdata = data;
   390 
   391     if (texture->access == SDL_TEXTUREACCESS_LOCAL) {
   392         pool = D3DPOOL_MANAGED;
   393     } else {
   394         pool = D3DPOOL_DEFAULT;
   395     }
   396     result =
   397         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   398                                        texture->h, 1, 0,
   399                                        PixelFormatToD3DFMT(texture->format),
   400                                        pool, &data->texture, NULL);
   401     if (FAILED(result)) {
   402         D3D_SetError("CreateTexture()", result);
   403         return -1;
   404     }
   405 
   406     return 0;
   407 }
   408 
   409 static int
   410 D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   411                       const SDL_Color * colors, int firstcolor, int ncolors)
   412 {
   413     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   414     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   415 
   416     return 0;
   417 }
   418 
   419 static int
   420 D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   421                       SDL_Color * colors, int firstcolor, int ncolors)
   422 {
   423     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   424 
   425     return 0;
   426 }
   427 
   428 static int
   429 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   430                   const SDL_Rect * rect, const void *pixels, int pitch)
   431 {
   432     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   433     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   434     IDirect3DTexture9 *temp;
   435     RECT d3drect;
   436     D3DLOCKED_RECT locked;
   437     const Uint8 *src;
   438     Uint8 *dst;
   439     int row, length;
   440     HRESULT result;
   441 
   442     result =
   443         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   444                                        texture->h, 1, 0,
   445                                        PixelFormatToD3DFMT(texture->format),
   446                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
   447     if (FAILED(result)) {
   448         D3D_SetError("CreateTexture()", result);
   449         return -1;
   450     }
   451 
   452     d3drect.left = rect->x;
   453     d3drect.right = rect->x + rect->w;
   454     d3drect.top = rect->y;
   455     d3drect.bottom = rect->y + rect->h;
   456 
   457     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   458     if (FAILED(result)) {
   459         IDirect3DTexture9_Release(temp);
   460         D3D_SetError("LockRect()", result);
   461         return -1;
   462     }
   463 
   464     src = pixels;
   465     dst = locked.pBits;
   466     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   467     for (row = 0; row < rect->h; ++row) {
   468         SDL_memcpy(dst, src, length);
   469         src += pitch;
   470         dst += locked.Pitch;
   471     }
   472     IDirect3DTexture9_UnlockRect(temp, 0);
   473 
   474     result =
   475         IDirect3DDevice9_UpdateTexture(renderdata->device,
   476                                        (IDirect3DBaseTexture9 *) temp,
   477                                        (IDirect3DBaseTexture9 *) data->
   478                                        texture);
   479     IDirect3DTexture9_Release(temp);
   480     if (FAILED(result)) {
   481         D3D_SetError("UpdateTexture()", result);
   482         return -1;
   483     }
   484     return 0;
   485 }
   486 
   487 static int
   488 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   489                 const SDL_Rect * rect, int markDirty, void **pixels,
   490                 int *pitch)
   491 {
   492     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   493     RECT d3drect;
   494     D3DLOCKED_RECT locked;
   495     HRESULT result;
   496 
   497     if (texture->access != SDL_TEXTUREACCESS_LOCAL) {
   498         SDL_SetError("Can't lock remote video memory");
   499         return -1;
   500     }
   501 
   502     d3drect.left = rect->x;
   503     d3drect.right = rect->x + rect->w;
   504     d3drect.top = rect->y;
   505     d3drect.bottom = rect->y + rect->h;
   506 
   507     result =
   508         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   509                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   510     if (FAILED(result)) {
   511         D3D_SetError("LockRect()", result);
   512         return -1;
   513     }
   514     *pixels = locked.pBits;
   515     *pitch = locked.Pitch;
   516     return 0;
   517 }
   518 
   519 static void
   520 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   521 {
   522     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   523 
   524     IDirect3DTexture9_UnlockRect(data->texture, 0);
   525 }
   526 
   527 static void
   528 D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   529                  const SDL_Rect * rects)
   530 {
   531     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   532     RECT d3drect;
   533     int i;
   534 
   535     for (i = 0; i < numrects; ++i) {
   536         const SDL_Rect *rect = &rects[i];
   537 
   538         d3drect.left = rect->x;
   539         d3drect.right = rect->x + rect->w;
   540         d3drect.top = rect->y;
   541         d3drect.bottom = rect->y + rect->h;
   542 
   543         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   544     }
   545 }
   546 
   547 static int
   548 D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
   549 {
   550     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   551     D3DRECT d3drect;
   552     HRESULT result;
   553 
   554     if (data->beginScene) {
   555         IDirect3DDevice9_BeginScene(data->device);
   556         data->beginScene = SDL_FALSE;
   557     }
   558 
   559     d3drect.x1 = rect->x;
   560     d3drect.x2 = rect->x + rect->w;
   561     d3drect.y1 = rect->y;
   562     d3drect.y2 = rect->y + rect->h;
   563 
   564     result =
   565         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
   566                                (D3DCOLOR) color, 1.0f, 0);
   567     if (FAILED(result)) {
   568         D3D_SetError("Clear()", result);
   569         return -1;
   570     }
   571     return 0;
   572 }
   573 
   574 static int
   575 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   576                const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   577                int blendMode, int scaleMode)
   578 {
   579     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   580     D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   581     float minx, miny, maxx, maxy;
   582     float minu, maxu, minv, maxv;
   583     Vertex vertices[4];
   584     HRESULT result;
   585 
   586     if (data->beginScene) {
   587         IDirect3DDevice9_BeginScene(data->device);
   588         data->beginScene = SDL_FALSE;
   589     }
   590 
   591     minx = (float) dstrect->x - 0.5f;
   592     miny = (float) dstrect->y - 0.5f;
   593     maxx = (float) dstrect->x + dstrect->w - 0.5f;
   594     maxy = (float) dstrect->y + dstrect->h - 0.5f;
   595 
   596     minu = (float) srcrect->x / texture->w;
   597     maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   598     minv = (float) srcrect->y / texture->h;
   599     maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   600 
   601     vertices[0].x = minx;
   602     vertices[0].y = miny;
   603     vertices[0].z = 0.0f;
   604     vertices[0].rhw = 1.0f;
   605     vertices[0].u = minu;
   606     vertices[0].v = minv;
   607 
   608     vertices[1].x = maxx;
   609     vertices[1].y = miny;
   610     vertices[1].z = 0.0f;
   611     vertices[1].rhw = 1.0f;
   612     vertices[1].u = maxu;
   613     vertices[1].v = minv;
   614 
   615     vertices[2].x = maxx;
   616     vertices[2].y = maxy;
   617     vertices[2].z = 0.0f;
   618     vertices[2].rhw = 1.0f;
   619     vertices[2].u = maxu;
   620     vertices[2].v = maxv;
   621 
   622     vertices[3].x = minx;
   623     vertices[3].y = maxy;
   624     vertices[3].z = 0.0f;
   625     vertices[3].rhw = 1.0f;
   626     vertices[3].u = minu;
   627     vertices[3].v = maxv;
   628 
   629     switch (blendMode) {
   630     case SDL_TEXTUREBLENDMODE_NONE:
   631         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   632                                         FALSE);
   633         break;
   634     case SDL_TEXTUREBLENDMODE_MASK:
   635     case SDL_TEXTUREBLENDMODE_BLEND:
   636         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   637                                         TRUE);
   638         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   639                                         D3DBLEND_SRCALPHA);
   640         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   641                                         D3DBLEND_INVSRCALPHA);
   642         break;
   643     case SDL_TEXTUREBLENDMODE_ADD:
   644         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   645                                         TRUE);
   646         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   647                                         D3DBLEND_SRCALPHA);
   648         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   649                                         D3DBLEND_ONE);
   650         break;
   651     case SDL_TEXTUREBLENDMODE_MOD:
   652         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   653                                         TRUE);
   654         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   655                                         D3DBLEND_ZERO);
   656         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   657                                         D3DBLEND_SRCCOLOR);
   658         break;
   659     }
   660 
   661     switch (scaleMode) {
   662     case SDL_TEXTURESCALEMODE_NONE:
   663     case SDL_TEXTURESCALEMODE_FAST:
   664         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   665                                          D3DTEXF_POINT);
   666         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   667                                          D3DTEXF_POINT);
   668         break;
   669     case SDL_TEXTURESCALEMODE_SLOW:
   670         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   671                                          D3DTEXF_LINEAR);
   672         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   673                                          D3DTEXF_LINEAR);
   674         break;
   675     case SDL_TEXTURESCALEMODE_BEST:
   676         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   677                                          D3DTEXF_GAUSSIANQUAD);
   678         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   679                                          D3DTEXF_GAUSSIANQUAD);
   680         break;
   681     }
   682 
   683     result =
   684         IDirect3DDevice9_SetTexture(data->device, 0,
   685                                     (IDirect3DBaseTexture9 *) texturedata->
   686                                     texture);
   687     if (FAILED(result)) {
   688         D3D_SetError("SetTexture()", result);
   689         return -1;
   690     }
   691     result =
   692         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   693                                          vertices, sizeof(*vertices));
   694     if (FAILED(result)) {
   695         D3D_SetError("DrawPrimitiveUP()", result);
   696         return -1;
   697     }
   698     return 0;
   699 }
   700 
   701 static void
   702 D3D_RenderPresent(SDL_Renderer * renderer)
   703 {
   704     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   705     HRESULT result;
   706 
   707     if (!data->beginScene) {
   708         IDirect3DDevice9_EndScene(data->device);
   709         data->beginScene = SDL_TRUE;
   710     }
   711 
   712     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   713     if (FAILED(result)) {
   714         D3D_SetError("Present()", result);
   715     }
   716 }
   717 
   718 static void
   719 D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   720 {
   721     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   722 
   723     if (!data) {
   724         return;
   725     }
   726     if (data->texture) {
   727         IDirect3DTexture9_Release(data->texture);
   728     }
   729     SDL_free(data);
   730     texture->driverdata = NULL;
   731 }
   732 
   733 void
   734 D3D_DestroyRenderer(SDL_Renderer * renderer)
   735 {
   736     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   737 
   738     if (data) {
   739         if (data->device) {
   740             IDirect3DDevice9_Release(data->device);
   741         }
   742         SDL_free(data);
   743     }
   744     SDL_free(renderer);
   745 }
   746 
   747 #endif /* SDL_VIDEO_RENDER_D3D */
   748 
   749 /* vi: set ts=4 sw=4 expandtab: */