src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 13 Jul 2006 08:13:02 +0000
changeset 1901 f1828a500391
parent 1900 5c6bdbf3aadf
child 1903 f132024010be
permissions -rw-r--r--
Removed libc dependency on Windows again, to fix building with Visual C++ 2005 Express Edition.
Fixed performance problem with testsprite2 on the D3D driver.
     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 #include "../SDL_yuv_sw_c.h"
    28 
    29 /* Direct3D renderer implementation */
    30 
    31 static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
    32                                             Uint32 flags);
    33 static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
    34                                  SDL_Texture * texture);
    35 static int SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer,
    36                                       SDL_Texture * texture, void **pixels,
    37                                       int *pitch);
    38 static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
    39                                      SDL_Texture * texture,
    40                                      const SDL_Color * colors, int firstcolor,
    41                                      int ncolors);
    42 static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
    43                                      SDL_Texture * texture,
    44                                      SDL_Color * colors, int firstcolor,
    45                                      int ncolors);
    46 static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer,
    47                                  SDL_Texture * texture, const SDL_Rect * rect,
    48                                  const void *pixels, int pitch);
    49 static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    50                                const SDL_Rect * rect, int markDirty,
    51                                void **pixels, int *pitch);
    52 static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer,
    53                                   SDL_Texture * texture);
    54 static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer,
    55                                  SDL_Texture * texture, int numrects,
    56                                  const SDL_Rect * rects);
    57 static void SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer,
    58                                         SDL_Texture * texture);
    59 static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
    60                               Uint32 color);
    61 static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
    62                               const SDL_Rect * srcrect,
    63                               const SDL_Rect * dstrect, int blendMode,
    64                               int scaleMode);
    65 static int SDL_D3D_RenderReadPixels(SDL_Renderer * renderer,
    66                                     const SDL_Rect * rect, void *pixels,
    67                                     int pitch);
    68 static int SDL_D3D_RenderWritePixels(SDL_Renderer * renderer,
    69                                      const SDL_Rect * rect,
    70                                      const void *pixels, int pitch);
    71 static void SDL_D3D_RenderPresent(SDL_Renderer * renderer);
    72 static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer,
    73                                    SDL_Texture * texture);
    74 static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
    75 
    76 
    77 SDL_RenderDriver SDL_D3D_RenderDriver = {
    78     SDL_D3D_CreateRenderer,
    79     {
    80      "d3d",
    81      (                          //SDL_Renderer_Minimal |
    82          SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
    83          SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
    84          SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
    85      (SDL_TextureBlendMode_None |
    86       SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    87      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    88      11,
    89      {
    90       SDL_PixelFormat_Index8,
    91       SDL_PixelFormat_RGB555,
    92       SDL_PixelFormat_RGB565,
    93       SDL_PixelFormat_RGB888,
    94       SDL_PixelFormat_BGR888,
    95       SDL_PixelFormat_ARGB8888,
    96       SDL_PixelFormat_RGBA8888,
    97       SDL_PixelFormat_ABGR8888,
    98       SDL_PixelFormat_BGRA8888,
    99       SDL_PixelFormat_YUY2,
   100       SDL_PixelFormat_UYVY},
   101      0,
   102      0}
   103 };
   104 
   105 typedef struct
   106 {
   107     IDirect3DDevice9 *device;
   108     SDL_bool beginScene;
   109 } SDL_D3D_RenderData;
   110 
   111 typedef struct
   112 {
   113     SDL_SW_YUVTexture *yuv;
   114 } SDL_D3D_TextureData;
   115 
   116 static void
   117 D3D_SetError(const char *prefix, HRESULT result)
   118 {
   119     const char *error;
   120 
   121     switch (result) {
   122     case D3DERR_WRONGTEXTUREFORMAT:
   123         error = "WRONGTEXTUREFORMAT";
   124         break;
   125     case D3DERR_UNSUPPORTEDCOLOROPERATION:
   126         error = "UNSUPPORTEDCOLOROPERATION";
   127         break;
   128     case D3DERR_UNSUPPORTEDCOLORARG:
   129         error = "UNSUPPORTEDCOLORARG";
   130         break;
   131     case D3DERR_UNSUPPORTEDALPHAOPERATION:
   132         error = "UNSUPPORTEDALPHAOPERATION";
   133         break;
   134     case D3DERR_UNSUPPORTEDALPHAARG:
   135         error = "UNSUPPORTEDALPHAARG";
   136         break;
   137     case D3DERR_TOOMANYOPERATIONS:
   138         error = "TOOMANYOPERATIONS";
   139         break;
   140     case D3DERR_CONFLICTINGTEXTUREFILTER:
   141         error = "CONFLICTINGTEXTUREFILTER";
   142         break;
   143     case D3DERR_UNSUPPORTEDFACTORVALUE:
   144         error = "UNSUPPORTEDFACTORVALUE";
   145         break;
   146     case D3DERR_CONFLICTINGRENDERSTATE:
   147         error = "CONFLICTINGRENDERSTATE";
   148         break;
   149     case D3DERR_UNSUPPORTEDTEXTUREFILTER:
   150         error = "UNSUPPORTEDTEXTUREFILTER";
   151         break;
   152     case D3DERR_CONFLICTINGTEXTUREPALETTE:
   153         error = "CONFLICTINGTEXTUREPALETTE";
   154         break;
   155     case D3DERR_DRIVERINTERNALERROR:
   156         error = "DRIVERINTERNALERROR";
   157         break;
   158     case D3DERR_NOTFOUND:
   159         error = "NOTFOUND";
   160         break;
   161     case D3DERR_MOREDATA:
   162         error = "MOREDATA";
   163         break;
   164     case D3DERR_DEVICELOST:
   165         error = "DEVICELOST";
   166         break;
   167     case D3DERR_DEVICENOTRESET:
   168         error = "DEVICENOTRESET";
   169         break;
   170     case D3DERR_NOTAVAILABLE:
   171         error = "NOTAVAILABLE";
   172         break;
   173     case D3DERR_OUTOFVIDEOMEMORY:
   174         error = "OUTOFVIDEOMEMORY";
   175         break;
   176     case D3DERR_INVALIDDEVICE:
   177         error = "INVALIDDEVICE";
   178         break;
   179     case D3DERR_INVALIDCALL:
   180         error = "INVALIDCALL";
   181         break;
   182     case D3DERR_DRIVERINVALIDCALL:
   183         error = "DRIVERINVALIDCALL";
   184         break;
   185     case D3DERR_WASSTILLDRAWING:
   186         error = "WASSTILLDRAWING";
   187         break;
   188     default:
   189         error = "UNKNOWN";
   190         break;
   191     }
   192     SDL_SetError("%s: %s", prefix, error);
   193 }
   194 
   195 static void
   196 UpdateYUVTextureData(SDL_Texture * texture)
   197 {
   198     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   199     SDL_Rect rect;
   200 
   201     rect.x = 0;
   202     rect.y = 0;
   203     rect.w = texture->w;
   204     rect.h = texture->h;
   205     //SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
   206     //                    texture->h, data->pixels, data->pitch);
   207 }
   208 
   209 void
   210 D3D_AddRenderDriver(_THIS)
   211 {
   212     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   213 
   214     if (data->d3d) {
   215         SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
   216     }
   217 }
   218 
   219 SDL_Renderer *
   220 SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   221 {
   222     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   223     SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   224     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   225     SDL_Renderer *renderer;
   226     SDL_D3D_RenderData *data;
   227     HRESULT result;
   228     D3DPRESENT_PARAMETERS pparams;
   229 
   230     renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   231     if (!renderer) {
   232         SDL_OutOfMemory();
   233         return NULL;
   234     }
   235     SDL_zerop(renderer);
   236 
   237     data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
   238     if (!data) {
   239         SDL_D3D_DestroyRenderer(renderer);
   240         SDL_OutOfMemory();
   241         return NULL;
   242     }
   243     SDL_zerop(data);
   244 
   245     renderer->CreateTexture = SDL_D3D_CreateTexture;
   246     renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
   247     renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   248     renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   249     renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   250     renderer->LockTexture = SDL_D3D_LockTexture;
   251     renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   252     renderer->DirtyTexture = SDL_D3D_DirtyTexture;
   253     renderer->SelectRenderTexture = SDL_D3D_SelectRenderTexture;
   254     renderer->RenderFill = SDL_D3D_RenderFill;
   255     renderer->RenderCopy = SDL_D3D_RenderCopy;
   256     renderer->RenderReadPixels = SDL_D3D_RenderReadPixels;
   257     renderer->RenderWritePixels = SDL_D3D_RenderWritePixels;
   258     renderer->RenderPresent = SDL_D3D_RenderPresent;
   259     renderer->DestroyTexture = SDL_D3D_DestroyTexture;
   260     renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
   261     renderer->info = SDL_D3D_RenderDriver.info;
   262     renderer->window = window->id;
   263     renderer->driverdata = data;
   264 
   265     renderer->info.flags = SDL_Renderer_RenderTarget;
   266 
   267     SDL_zero(pparams);
   268     pparams.BackBufferWidth = window->w;
   269     pparams.BackBufferHeight = window->h;
   270     pparams.BackBufferFormat = D3DFMT_UNKNOWN;  /* FIXME */
   271     if (flags & SDL_Renderer_PresentFlip2) {
   272         pparams.BackBufferCount = 2;
   273         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   274     } else if (flags & SDL_Renderer_PresentFlip3) {
   275         pparams.BackBufferCount = 3;
   276         pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
   277     } else if (flags & SDL_Renderer_PresentCopy) {
   278         pparams.BackBufferCount = 1;
   279         pparams.SwapEffect = D3DSWAPEFFECT_COPY;
   280     } else {
   281         pparams.BackBufferCount = 1;
   282         pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   283     }
   284     if (window->flags & SDL_WINDOW_FULLSCREEN) {
   285         pparams.Windowed = FALSE;
   286     } else {
   287         pparams.Windowed = TRUE;
   288     }
   289     pparams.FullScreen_RefreshRateInHz = 0;     /* FIXME */
   290     pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   291 
   292     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   293                                      D3DDEVTYPE_HAL,
   294                                      windowdata->hwnd,
   295                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   296                                      &pparams, &data->device);
   297     if (FAILED(result)) {
   298         SDL_D3D_DestroyRenderer(renderer);
   299         D3D_SetError("CreateDevice()", result);
   300         return NULL;
   301     }
   302     data->beginScene = SDL_TRUE;
   303 
   304     return renderer;
   305 }
   306 
   307 static int
   308 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   309 {
   310     SDL_D3D_RenderData *renderdata =
   311         (SDL_D3D_RenderData *) renderer->driverdata;
   312     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   313     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   314     SDL_D3D_TextureData *data;
   315 
   316     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   317     if (!data) {
   318         SDL_OutOfMemory();
   319         return -1;
   320     }
   321     SDL_zerop(data);
   322 
   323     texture->driverdata = data;
   324 
   325     return 0;
   326 }
   327 
   328 static int
   329 SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   330                            void **pixels, int *pitch)
   331 {
   332     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   333 
   334     if (data->yuv) {
   335         return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
   336     } else {
   337         return 0;
   338     }
   339 }
   340 
   341 static int
   342 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   343                           const SDL_Color * colors, int firstcolor,
   344                           int ncolors)
   345 {
   346     SDL_D3D_RenderData *renderdata =
   347         (SDL_D3D_RenderData *) renderer->driverdata;
   348     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   349 
   350     if (data->yuv) {
   351         SDL_SetError("YUV textures don't have a palette");
   352         return -1;
   353     } else {
   354         return 0;
   355     }
   356 }
   357 
   358 static int
   359 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   360                           SDL_Color * colors, int firstcolor, int ncolors)
   361 {
   362     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   363 
   364     if (data->yuv) {
   365         SDL_SetError("YUV textures don't have a palette");
   366         return -1;
   367     } else {
   368         return 0;
   369     }
   370 }
   371 
   372 static int
   373 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   374                       const SDL_Rect * rect, const void *pixels, int pitch)
   375 {
   376     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   377 
   378     if (data->yuv) {
   379         if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   380             return -1;
   381         }
   382         UpdateYUVTextureData(texture);
   383         return 0;
   384     } else {
   385         SDL_D3D_RenderData *renderdata =
   386             (SDL_D3D_RenderData *) renderer->driverdata;
   387 
   388         return 0;
   389     }
   390 }
   391 
   392 static int
   393 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   394                     const SDL_Rect * rect, int markDirty, void **pixels,
   395                     int *pitch)
   396 {
   397     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   398 
   399     if (data->yuv) {
   400         return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   401                                      pitch);
   402     } else {
   403         return -1;
   404     }
   405 }
   406 
   407 static void
   408 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   409 {
   410     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   411 
   412     if (data->yuv) {
   413         SDL_SW_UnlockYUVTexture(data->yuv);
   414         UpdateYUVTextureData(texture);
   415     }
   416 }
   417 
   418 static void
   419 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   420                      int numrects, const SDL_Rect * rects)
   421 {
   422 }
   423 
   424 static void
   425 SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   426 {
   427     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   428 }
   429 
   430 static int
   431 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   432                    Uint32 color)
   433 {
   434     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   435     D3DRECT d3drect;
   436     HRESULT result;
   437 
   438     if (data->beginScene) {
   439         IDirect3DDevice9_BeginScene(data->device);
   440         data->beginScene = SDL_FALSE;
   441     }
   442 
   443     d3drect.x1 = rect->x;
   444     d3drect.x2 = rect->x+rect->w;
   445     d3drect.y1 = rect->y;
   446     d3drect.y2 = rect->y+rect->h;
   447 
   448     result = IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET, (D3DCOLOR) color, 1.0f, 0);
   449     if (FAILED(result)) {
   450         D3D_SetError("Clear()", result);
   451         return -1;
   452     }
   453     return 0;
   454 }
   455 
   456 static int
   457 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   458                    const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   459                    int blendMode, int scaleMode)
   460 {
   461     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   462     SDL_D3D_TextureData *texturedata =
   463         (SDL_D3D_TextureData *) texture->driverdata;
   464 
   465     if (data->beginScene) {
   466         IDirect3DDevice9_BeginScene(data->device);
   467         data->beginScene = SDL_FALSE;
   468     }
   469     return 0;
   470 }
   471 
   472 static int
   473 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   474                          void *pixels, int pitch)
   475 {
   476     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   477 
   478     return 0;
   479 }
   480 
   481 static int
   482 SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   483                           const void *pixels, int pitch)
   484 {
   485     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   486 
   487     return 0;
   488 }
   489 
   490 static void
   491 SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   492 {
   493     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   494     HRESULT result;
   495 
   496     if (!data->beginScene) {
   497         IDirect3DDevice9_EndScene(data->device);
   498         data->beginScene = SDL_TRUE;
   499     }
   500 
   501     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   502     if (FAILED(result)) {
   503         D3D_SetError("Present()", result);
   504     }
   505 }
   506 
   507 static void
   508 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   509 {
   510     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   511 
   512     if (!data) {
   513         return;
   514     }
   515     SDL_free(data);
   516     texture->driverdata = NULL;
   517 }
   518 
   519 void
   520 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
   521 {
   522     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   523 
   524     if (data) {
   525         if (data->device) {
   526             IDirect3DDevice9_Release(data->device);
   527         }
   528         SDL_free(data);
   529     }
   530     SDL_free(renderer);
   531 }
   532 
   533 #endif /* SDL_VIDEO_RENDER_D3D */
   534 
   535 /* vi: set ts=4 sw=4 expandtab: */