src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jul 2006 09:25:17 +0000
changeset 1900 5c6bdbf3aadf
parent 1898 f89e49e51e89
child 1901 f1828a500391
permissions -rw-r--r--
First stab at a D3D renderer, only 30FPS so far... ?
     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 
   291     result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
   292                                      D3DDEVTYPE_HAL,
   293                                      windowdata->hwnd,
   294                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   295                                      &pparams, &data->device);
   296     if (FAILED(result)) {
   297         SDL_D3D_DestroyRenderer(renderer);
   298         D3D_SetError("CreateDevice()", result);
   299         return NULL;
   300     }
   301     data->beginScene = SDL_TRUE;
   302 
   303     return renderer;
   304 }
   305 
   306 static int
   307 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   308 {
   309     SDL_D3D_RenderData *renderdata =
   310         (SDL_D3D_RenderData *) renderer->driverdata;
   311     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   312     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   313     SDL_D3D_TextureData *data;
   314 
   315     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   316     if (!data) {
   317         SDL_OutOfMemory();
   318         return -1;
   319     }
   320     SDL_zerop(data);
   321 
   322     texture->driverdata = data;
   323 
   324     return 0;
   325 }
   326 
   327 static int
   328 SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   329                            void **pixels, int *pitch)
   330 {
   331     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   332 
   333     if (data->yuv) {
   334         return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
   335     } else {
   336         return 0;
   337     }
   338 }
   339 
   340 static int
   341 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   342                           const SDL_Color * colors, int firstcolor,
   343                           int ncolors)
   344 {
   345     SDL_D3D_RenderData *renderdata =
   346         (SDL_D3D_RenderData *) renderer->driverdata;
   347     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   348 
   349     if (data->yuv) {
   350         SDL_SetError("YUV textures don't have a palette");
   351         return -1;
   352     } else {
   353         return 0;
   354     }
   355 }
   356 
   357 static int
   358 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   359                           SDL_Color * colors, int firstcolor, int ncolors)
   360 {
   361     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   362 
   363     if (data->yuv) {
   364         SDL_SetError("YUV textures don't have a palette");
   365         return -1;
   366     } else {
   367         return 0;
   368     }
   369 }
   370 
   371 static int
   372 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   373                       const SDL_Rect * rect, const void *pixels, int pitch)
   374 {
   375     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   376 
   377     if (data->yuv) {
   378         if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   379             return -1;
   380         }
   381         UpdateYUVTextureData(texture);
   382         return 0;
   383     } else {
   384         SDL_D3D_RenderData *renderdata =
   385             (SDL_D3D_RenderData *) renderer->driverdata;
   386 
   387         return 0;
   388     }
   389 }
   390 
   391 static int
   392 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   393                     const SDL_Rect * rect, int markDirty, void **pixels,
   394                     int *pitch)
   395 {
   396     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   397 
   398     if (data->yuv) {
   399         return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   400                                      pitch);
   401     } else {
   402         return -1;
   403     }
   404 }
   405 
   406 static void
   407 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   408 {
   409     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   410 
   411     if (data->yuv) {
   412         SDL_SW_UnlockYUVTexture(data->yuv);
   413         UpdateYUVTextureData(texture);
   414     }
   415 }
   416 
   417 static void
   418 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   419                      int numrects, const SDL_Rect * rects)
   420 {
   421 }
   422 
   423 static void
   424 SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   425 {
   426     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   427 }
   428 
   429 static int
   430 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   431                    Uint32 color)
   432 {
   433     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   434     HRESULT result;
   435 
   436     if (data->beginScene) {
   437         IDirect3DDevice9_BeginScene(data->device);
   438         data->beginScene = SDL_FALSE;
   439     }
   440 
   441     result =
   442         IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET,
   443                                (D3DCOLOR) color, 1.0f, 0);
   444     if (FAILED(result)) {
   445         D3D_SetError("Clear()", result);
   446         return -1;
   447     }
   448     return 0;
   449 }
   450 
   451 static int
   452 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   453                    const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   454                    int blendMode, int scaleMode)
   455 {
   456     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   457     SDL_D3D_TextureData *texturedata =
   458         (SDL_D3D_TextureData *) texture->driverdata;
   459 
   460     if (data->beginScene) {
   461         IDirect3DDevice9_BeginScene(data->device);
   462         data->beginScene = SDL_FALSE;
   463     }
   464     return 0;
   465 }
   466 
   467 static int
   468 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   469                          void *pixels, int pitch)
   470 {
   471     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   472 
   473     return 0;
   474 }
   475 
   476 static int
   477 SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   478                           const void *pixels, int pitch)
   479 {
   480     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   481 
   482     return 0;
   483 }
   484 
   485 static void
   486 SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   487 {
   488     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   489     HRESULT result;
   490 
   491     if (!data->beginScene) {
   492         IDirect3DDevice9_EndScene(data->device);
   493         data->beginScene = SDL_TRUE;
   494     }
   495 
   496     result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
   497     if (FAILED(result)) {
   498         D3D_SetError("Present()", result);
   499     }
   500 }
   501 
   502 static void
   503 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   504 {
   505     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   506 
   507     if (!data) {
   508         return;
   509     }
   510     SDL_free(data);
   511     texture->driverdata = NULL;
   512 }
   513 
   514 void
   515 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
   516 {
   517     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   518 
   519     if (data) {
   520         if (data->device) {
   521             IDirect3DDevice9_Release(data->device);
   522         }
   523         SDL_free(data);
   524     }
   525     SDL_free(renderer);
   526 }
   527 
   528 #endif /* SDL_VIDEO_RENDER_D3D */
   529 
   530 /* vi: set ts=4 sw=4 expandtab: */