src/video/win32/SDL_d3drender.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 2222 926294b2bb4e
child 2698 e1da92da346c
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
     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     /* Disable second texture stage, since we're done */
   404     IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
   405                                           D3DTOP_DISABLE);
   406     IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
   407                                           D3DTOP_DISABLE);
   408 
   409     return renderer;
   410 }
   411 
   412 static int
   413 D3D_Reset(SDL_Renderer * renderer)
   414 {
   415     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   416     HRESULT result;
   417 
   418     result = IDirect3DDevice9_Reset(data->device, &data->pparams);
   419     if (FAILED(result)) {
   420         if (result == D3DERR_DEVICELOST) {
   421             /* Don't worry about it, we'll reset later... */
   422             return 0;
   423         } else {
   424             D3D_SetError("Reset()", result);
   425             return -1;
   426         }
   427     }
   428     IDirect3DDevice9_SetVertexShader(data->device, NULL);
   429     IDirect3DDevice9_SetFVF(data->device,
   430                             D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   431     IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   432                                     D3DCULL_NONE);
   433     IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   434     return 0;
   435 }
   436 
   437 static int
   438 D3D_DisplayModeChanged(SDL_Renderer * renderer)
   439 {
   440     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   441     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   442     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   443 
   444     data->pparams.BackBufferWidth = window->w;
   445     data->pparams.BackBufferHeight = window->h;
   446     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   447         data->pparams.BackBufferFormat =
   448             PixelFormatToD3DFMT(display->fullscreen_mode.format);
   449     } else {
   450         data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   451     }
   452     return D3D_Reset(renderer);
   453 }
   454 
   455 static int
   456 D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   457 {
   458     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   459     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   460     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   461     D3D_TextureData *data;
   462     HRESULT result;
   463 
   464     data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   465     if (!data) {
   466         SDL_OutOfMemory();
   467         return -1;
   468     }
   469 
   470     texture->driverdata = data;
   471 
   472     result =
   473         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   474                                        texture->h, 1, 0,
   475                                        PixelFormatToD3DFMT(texture->format),
   476                                        D3DPOOL_MANAGED, &data->texture, NULL);
   477     if (FAILED(result)) {
   478         D3D_SetError("CreateTexture()", result);
   479         return -1;
   480     }
   481 
   482     return 0;
   483 }
   484 
   485 static int
   486 D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   487                       const SDL_Color * colors, int firstcolor, int ncolors)
   488 {
   489     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   490     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   491 
   492     return 0;
   493 }
   494 
   495 static int
   496 D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   497                       SDL_Color * colors, int firstcolor, int ncolors)
   498 {
   499     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   500 
   501     return 0;
   502 }
   503 
   504 static int
   505 D3D_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
   506 {
   507     return 0;
   508 }
   509 
   510 static int
   511 D3D_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
   512 {
   513     return 0;
   514 }
   515 
   516 static int
   517 D3D_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
   518 {
   519     switch (texture->blendMode) {
   520     case SDL_TEXTUREBLENDMODE_NONE:
   521     case SDL_TEXTUREBLENDMODE_MASK:
   522     case SDL_TEXTUREBLENDMODE_BLEND:
   523     case SDL_TEXTUREBLENDMODE_ADD:
   524     case SDL_TEXTUREBLENDMODE_MOD:
   525         return 0;
   526     default:
   527         SDL_Unsupported();
   528         texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
   529         return -1;
   530     }
   531 }
   532 
   533 static int
   534 D3D_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
   535 {
   536     switch (texture->scaleMode) {
   537     case SDL_TEXTURESCALEMODE_NONE:
   538     case SDL_TEXTURESCALEMODE_FAST:
   539     case SDL_TEXTURESCALEMODE_SLOW:
   540     case SDL_TEXTURESCALEMODE_BEST:
   541         return 0;
   542     default:
   543         SDL_Unsupported();
   544         texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
   545         return -1;
   546     }
   547     return 0;
   548 }
   549 
   550 static int
   551 D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   552                   const SDL_Rect * rect, const void *pixels, int pitch)
   553 {
   554     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   555     D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   556     IDirect3DTexture9 *temp;
   557     RECT d3drect;
   558     D3DLOCKED_RECT locked;
   559     const Uint8 *src;
   560     Uint8 *dst;
   561     int row, length;
   562     HRESULT result;
   563 
   564     result =
   565         IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   566                                        texture->h, 1, 0,
   567                                        PixelFormatToD3DFMT(texture->format),
   568                                        D3DPOOL_SYSTEMMEM, &temp, NULL);
   569     if (FAILED(result)) {
   570         D3D_SetError("CreateTexture()", result);
   571         return -1;
   572     }
   573 
   574     d3drect.left = rect->x;
   575     d3drect.right = rect->x + rect->w;
   576     d3drect.top = rect->y;
   577     d3drect.bottom = rect->y + rect->h;
   578 
   579     result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   580     if (FAILED(result)) {
   581         IDirect3DTexture9_Release(temp);
   582         D3D_SetError("LockRect()", result);
   583         return -1;
   584     }
   585 
   586     src = pixels;
   587     dst = locked.pBits;
   588     length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   589     for (row = 0; row < rect->h; ++row) {
   590         SDL_memcpy(dst, src, length);
   591         src += pitch;
   592         dst += locked.Pitch;
   593     }
   594     IDirect3DTexture9_UnlockRect(temp, 0);
   595 
   596     result =
   597         IDirect3DDevice9_UpdateTexture(renderdata->device,
   598                                        (IDirect3DBaseTexture9 *) temp,
   599                                        (IDirect3DBaseTexture9 *) data->
   600                                        texture);
   601     IDirect3DTexture9_Release(temp);
   602     if (FAILED(result)) {
   603         D3D_SetError("UpdateTexture()", result);
   604         return -1;
   605     }
   606     return 0;
   607 }
   608 
   609 static int
   610 D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   611                 const SDL_Rect * rect, int markDirty, void **pixels,
   612                 int *pitch)
   613 {
   614     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   615     RECT d3drect;
   616     D3DLOCKED_RECT locked;
   617     HRESULT result;
   618 
   619     d3drect.left = rect->x;
   620     d3drect.right = rect->x + rect->w;
   621     d3drect.top = rect->y;
   622     d3drect.bottom = rect->y + rect->h;
   623 
   624     result =
   625         IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   626                                    markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   627     if (FAILED(result)) {
   628         D3D_SetError("LockRect()", result);
   629         return -1;
   630     }
   631     *pixels = locked.pBits;
   632     *pitch = locked.Pitch;
   633     return 0;
   634 }
   635 
   636 static void
   637 D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   638 {
   639     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   640 
   641     IDirect3DTexture9_UnlockRect(data->texture, 0);
   642 }
   643 
   644 static void
   645 D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
   646                  const SDL_Rect * rects)
   647 {
   648     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   649     RECT d3drect;
   650     int i;
   651 
   652     for (i = 0; i < numrects; ++i) {
   653         const SDL_Rect *rect = &rects[i];
   654 
   655         d3drect.left = rect->x;
   656         d3drect.right = rect->x + rect->w;
   657         d3drect.top = rect->y;
   658         d3drect.bottom = rect->y + rect->h;
   659 
   660         IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
   661     }
   662 }
   663 
   664 static int
   665 D3D_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
   666                const SDL_Rect * rect)
   667 {
   668     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   669     D3DRECT d3drect;
   670     HRESULT result;
   671 
   672     if (data->beginScene) {
   673         IDirect3DDevice9_BeginScene(data->device);
   674         data->beginScene = SDL_FALSE;
   675     }
   676 
   677     d3drect.x1 = rect->x;
   678     d3drect.x2 = rect->x + rect->w;
   679     d3drect.y1 = rect->y;
   680     d3drect.y2 = rect->y + rect->h;
   681 
   682     result =
   683         IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
   684                                D3DCOLOR_ARGB(a, r, g, b), 1.0f, 0);
   685     if (FAILED(result)) {
   686         D3D_SetError("Clear()", result);
   687         return -1;
   688     }
   689     return 0;
   690 }
   691 
   692 static int
   693 D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   694                const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   695 {
   696     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   697     D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   698     float minx, miny, maxx, maxy;
   699     float minu, maxu, minv, maxv;
   700     DWORD color;
   701     Vertex vertices[4];
   702     HRESULT result;
   703 
   704     if (data->beginScene) {
   705         IDirect3DDevice9_BeginScene(data->device);
   706         data->beginScene = SDL_FALSE;
   707     }
   708 
   709     minx = (float) dstrect->x - 0.5f;
   710     miny = (float) dstrect->y - 0.5f;
   711     maxx = (float) dstrect->x + dstrect->w - 0.5f;
   712     maxy = (float) dstrect->y + dstrect->h - 0.5f;
   713 
   714     minu = (float) srcrect->x / texture->w;
   715     maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   716     minv = (float) srcrect->y / texture->h;
   717     maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   718 
   719     color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
   720 
   721     vertices[0].x = minx;
   722     vertices[0].y = miny;
   723     vertices[0].z = 0.0f;
   724     vertices[0].rhw = 1.0f;
   725     vertices[0].color = color;
   726     vertices[0].u = minu;
   727     vertices[0].v = minv;
   728 
   729     vertices[1].x = maxx;
   730     vertices[1].y = miny;
   731     vertices[1].z = 0.0f;
   732     vertices[1].rhw = 1.0f;
   733     vertices[1].color = color;
   734     vertices[1].u = maxu;
   735     vertices[1].v = minv;
   736 
   737     vertices[2].x = maxx;
   738     vertices[2].y = maxy;
   739     vertices[2].z = 0.0f;
   740     vertices[2].rhw = 1.0f;
   741     vertices[2].color = color;
   742     vertices[2].u = maxu;
   743     vertices[2].v = maxv;
   744 
   745     vertices[3].x = minx;
   746     vertices[3].y = maxy;
   747     vertices[3].z = 0.0f;
   748     vertices[3].rhw = 1.0f;
   749     vertices[3].color = color;
   750     vertices[3].u = minu;
   751     vertices[3].v = maxv;
   752 
   753     switch (texture->blendMode) {
   754     case SDL_TEXTUREBLENDMODE_NONE:
   755         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   756                                         FALSE);
   757         break;
   758     case SDL_TEXTUREBLENDMODE_MASK:
   759     case SDL_TEXTUREBLENDMODE_BLEND:
   760         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   761                                         TRUE);
   762         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   763                                         D3DBLEND_SRCALPHA);
   764         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   765                                         D3DBLEND_INVSRCALPHA);
   766         break;
   767     case SDL_TEXTUREBLENDMODE_ADD:
   768         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   769                                         TRUE);
   770         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   771                                         D3DBLEND_SRCALPHA);
   772         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   773                                         D3DBLEND_ONE);
   774         break;
   775     case SDL_TEXTUREBLENDMODE_MOD:
   776         IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   777                                         TRUE);
   778         IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   779                                         D3DBLEND_ZERO);
   780         IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   781                                         D3DBLEND_SRCCOLOR);
   782         break;
   783     }
   784 
   785     switch (texture->scaleMode) {
   786     case SDL_TEXTURESCALEMODE_NONE:
   787     case SDL_TEXTURESCALEMODE_FAST:
   788         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   789                                          D3DTEXF_POINT);
   790         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   791                                          D3DTEXF_POINT);
   792         break;
   793     case SDL_TEXTURESCALEMODE_SLOW:
   794         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   795                                          D3DTEXF_LINEAR);
   796         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   797                                          D3DTEXF_LINEAR);
   798         break;
   799     case SDL_TEXTURESCALEMODE_BEST:
   800         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   801                                          D3DTEXF_GAUSSIANQUAD);
   802         IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   803                                          D3DTEXF_GAUSSIANQUAD);
   804         break;
   805     }
   806 
   807     result =
   808         IDirect3DDevice9_SetTexture(data->device, 0,
   809                                     (IDirect3DBaseTexture9 *) texturedata->
   810                                     texture);
   811     if (FAILED(result)) {
   812         D3D_SetError("SetTexture()", result);
   813         return -1;
   814     }
   815     result =
   816         IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   817                                          vertices, sizeof(*vertices));
   818     if (FAILED(result)) {
   819         D3D_SetError("DrawPrimitiveUP()", result);
   820         return -1;
   821     }
   822     return 0;
   823 }
   824 
   825 static void
   826 D3D_RenderPresent(SDL_Renderer * renderer)
   827 {
   828     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   829     HRESULT result;
   830 
   831     if (!data->beginScene) {
   832         IDirect3DDevice9_EndScene(data->device);
   833         data->beginScene = SDL_TRUE;
   834     }
   835 
   836     result = IDirect3DDevice9_TestCooperativeLevel(data->device);
   837     if (result == D3DERR_DEVICELOST) {
   838         /* We'll reset later */
   839         return;
   840     }
   841     if (result == D3DERR_DEVICENOTRESET) {
   842         D3D_Reset(renderer);
   843     }
   844     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   845     if (FAILED(result)) {
   846         D3D_SetError("Present()", result);
   847     }
   848 }
   849 
   850 static void
   851 D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   852 {
   853     D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   854 
   855     if (!data) {
   856         return;
   857     }
   858     if (data->texture) {
   859         IDirect3DTexture9_Release(data->texture);
   860     }
   861     SDL_free(data);
   862     texture->driverdata = NULL;
   863 }
   864 
   865 static void
   866 D3D_DestroyRenderer(SDL_Renderer * renderer)
   867 {
   868     D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   869 
   870     if (data) {
   871         if (data->device) {
   872             IDirect3DDevice9_Release(data->device);
   873         }
   874         SDL_free(data);
   875     }
   876     SDL_free(renderer);
   877 }
   878 
   879 #endif /* SDL_VIDEO_RENDER_D3D */
   880 
   881 /* vi: set ts=4 sw=4 expandtab: */