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