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