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