src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 08 Jul 2006 18:06:02 +0000
branchSDL-1.3
changeset 1730 e70477157db9
child 1898 f89e49e51e89
permissions -rw-r--r--
Starting support for Direct3D render 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_PresentDiscard |
    82       SDL_Renderer_PresentCopy | SDL_Renderer_RenderTarget),
    83      (SDL_TextureBlendMode_None |
    84       SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
    85      (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
    86      11,
    87      {
    88       SDL_PixelFormat_Index8,
    89       SDL_PixelFormat_RGB555,
    90       SDL_PixelFormat_RGB565,
    91       SDL_PixelFormat_RGB888,
    92       SDL_PixelFormat_BGR888,
    93       SDL_PixelFormat_ARGB8888,
    94       SDL_PixelFormat_RGBA8888,
    95       SDL_PixelFormat_ABGR8888,
    96       SDL_PixelFormat_BGRA8888,
    97       SDL_PixelFormat_YUY2,
    98       SDL_PixelFormat_UYVY},
    99      0,
   100      0}
   101 };
   102 
   103 typedef struct
   104 {
   105     IDirect3DDevice9 *device;
   106 } SDL_D3D_RenderData;
   107 
   108 typedef struct
   109 {
   110     SDL_SW_YUVTexture *yuv;
   111 } SDL_D3D_TextureData;
   112 
   113 static void
   114 UpdateYUVTextureData(SDL_Texture * texture)
   115 {
   116     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   117     SDL_Rect rect;
   118 
   119     rect.x = 0;
   120     rect.y = 0;
   121     rect.w = texture->w;
   122     rect.h = texture->h;
   123     //SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
   124     //                    texture->h, data->pixels, data->pitch);
   125 }
   126 
   127 void
   128 D3D_AddRenderDriver(_THIS)
   129 {
   130     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   131 
   132     if (data->d3d) {
   133         SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
   134     }
   135 }
   136 
   137 SDL_Renderer *
   138 SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   139 {
   140     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   141     SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
   142     SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
   143     SDL_Renderer *renderer;
   144     SDL_D3D_RenderData *data;
   145 
   146     renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
   147     if (!renderer) {
   148         SDL_OutOfMemory();
   149         return NULL;
   150     }
   151     SDL_zerop(renderer);
   152 
   153     data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
   154     if (!data) {
   155         SDL_D3D_DestroyRenderer(renderer);
   156         SDL_OutOfMemory();
   157         return NULL;
   158     }
   159     SDL_zerop(data);
   160 
   161     //data->device = IDirect3D9_CreateDevice(videodata->d3d,
   162 
   163     renderer->CreateTexture = SDL_D3D_CreateTexture;
   164     renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
   165     renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
   166     renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
   167     renderer->UpdateTexture = SDL_D3D_UpdateTexture;
   168     renderer->LockTexture = SDL_D3D_LockTexture;
   169     renderer->UnlockTexture = SDL_D3D_UnlockTexture;
   170     renderer->DirtyTexture = SDL_D3D_DirtyTexture;
   171     renderer->SelectRenderTexture = SDL_D3D_SelectRenderTexture;
   172     renderer->RenderFill = SDL_D3D_RenderFill;
   173     renderer->RenderCopy = SDL_D3D_RenderCopy;
   174     renderer->RenderReadPixels = SDL_D3D_RenderReadPixels;
   175     renderer->RenderWritePixels = SDL_D3D_RenderWritePixels;
   176     renderer->RenderPresent = SDL_D3D_RenderPresent;
   177     renderer->DestroyTexture = SDL_D3D_DestroyTexture;
   178     renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
   179     renderer->info = SDL_D3D_RenderDriver.info;
   180     renderer->window = window->id;
   181     renderer->driverdata = data;
   182 
   183     renderer->info.flags = SDL_Renderer_RenderTarget;
   184 
   185     return renderer;
   186 }
   187 
   188 static int
   189 SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   190 {
   191     SDL_D3D_RenderData *renderdata =
   192         (SDL_D3D_RenderData *) renderer->driverdata;
   193     SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   194     SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   195     SDL_D3D_TextureData *data;
   196 
   197     data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
   198     if (!data) {
   199         SDL_OutOfMemory();
   200         return -1;
   201     }
   202     SDL_zerop(data);
   203 
   204     texture->driverdata = data;
   205 
   206     return 0;
   207 }
   208 
   209 static int
   210 SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   211                            void **pixels, int *pitch)
   212 {
   213     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   214 
   215     if (data->yuv) {
   216         return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
   217     } else {
   218         return 0;
   219     }
   220 }
   221 
   222 static int
   223 SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   224                           const SDL_Color * colors, int firstcolor,
   225                           int ncolors)
   226 {
   227     SDL_D3D_RenderData *renderdata =
   228         (SDL_D3D_RenderData *) renderer->driverdata;
   229     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   230 
   231     if (data->yuv) {
   232         SDL_SetError("YUV textures don't have a palette");
   233         return -1;
   234     } else {
   235         return 0;
   236     }
   237 }
   238 
   239 static int
   240 SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   241                           SDL_Color * colors, int firstcolor, int ncolors)
   242 {
   243     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   244 
   245     if (data->yuv) {
   246         SDL_SetError("YUV textures don't have a palette");
   247         return -1;
   248     } else {
   249         return 0;
   250     }
   251 }
   252 
   253 static int
   254 SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   255                       const SDL_Rect * rect, const void *pixels, int pitch)
   256 {
   257     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   258 
   259     if (data->yuv) {
   260         if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   261             return -1;
   262         }
   263         UpdateYUVTextureData(texture);
   264         return 0;
   265     } else {
   266         SDL_D3D_RenderData *renderdata =
   267             (SDL_D3D_RenderData *) renderer->driverdata;
   268 
   269         return 0;
   270     }
   271 }
   272 
   273 static int
   274 SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   275                     const SDL_Rect * rect, int markDirty, void **pixels,
   276                     int *pitch)
   277 {
   278     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   279 
   280     if (data->yuv) {
   281         return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   282                                      pitch);
   283     } else {
   284         return 0;
   285     }
   286 }
   287 
   288 static void
   289 SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   290 {
   291     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   292 
   293     if (data->yuv) {
   294         SDL_SW_UnlockYUVTexture(data->yuv);
   295         UpdateYUVTextureData(texture);
   296     }
   297 }
   298 
   299 static void
   300 SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   301                      int numrects, const SDL_Rect * rects)
   302 {
   303 }
   304 
   305 static void
   306 SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   307 {
   308     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   309 }
   310 
   311 static int
   312 SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
   313                    Uint32 color)
   314 {
   315     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   316     Uint8 r, g, b;
   317 
   318     r = (Uint8) ((color >> 16) & 0xFF);
   319     g = (Uint8) ((color >> 8) & 0xFF);
   320     b = (Uint8) (color & 0xFF);
   321 
   322     return 0;
   323 }
   324 
   325 static int
   326 SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   327                    const SDL_Rect * srcrect, const SDL_Rect * dstrect,
   328                    int blendMode, int scaleMode)
   329 {
   330     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   331     SDL_D3D_TextureData *texturedata =
   332         (SDL_D3D_TextureData *) texture->driverdata;
   333 
   334     return 0;
   335 }
   336 
   337 static int
   338 SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   339                          void *pixels, int pitch)
   340 {
   341     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   342 
   343     return 0;
   344 }
   345 
   346 static int
   347 SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   348                           const void *pixels, int pitch)
   349 {
   350     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   351 
   352     return 0;
   353 }
   354 
   355 static void
   356 SDL_D3D_RenderPresent(SDL_Renderer * renderer)
   357 {
   358 }
   359 
   360 static void
   361 SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   362 {
   363     SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
   364 
   365     if (!data) {
   366         return;
   367     }
   368     SDL_free(data);
   369     texture->driverdata = NULL;
   370 }
   371 
   372 void
   373 SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
   374 {
   375     SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
   376 
   377     if (data) {
   378         SDL_free(data);
   379     }
   380     SDL_free(renderer);
   381 }
   382 
   383 #endif /* SDL_VIDEO_RENDER_D3D */
   384 
   385 /* vi: set ts=4 sw=4 expandtab: */