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