Improved Direct3D YUV texture support
authorSam Lantinga <slouken@libsdl.org>
Sat, 03 Jan 2009 05:42:18 +0000
changeset 2973ab0c00f1b070
parent 2972 0a4b70368372
child 2974 d2f68ec8c1d0
Improved Direct3D YUV texture support
src/video/win32/SDL_d3drender.c
     1.1 --- a/src/video/win32/SDL_d3drender.c	Sat Jan 03 04:33:19 2009 +0000
     1.2 +++ b/src/video/win32/SDL_d3drender.c	Sat Jan 03 05:42:18 2009 +0000
     1.3 @@ -24,6 +24,7 @@
     1.4  #if SDL_VIDEO_RENDER_D3D
     1.5  
     1.6  #include "SDL_win32video.h"
     1.7 +#include "../SDL_yuv_sw_c.h"
     1.8  
     1.9  /* Direct3D renderer implementation */
    1.10  
    1.11 @@ -38,6 +39,7 @@
    1.12  static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.13  static int D3D_DisplayModeChanged(SDL_Renderer * renderer);
    1.14  static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.15 +static int D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, void **pixels, int *pitch);
    1.16  static int D3D_SetTexturePalette(SDL_Renderer * renderer,
    1.17                                   SDL_Texture * texture,
    1.18                                   const SDL_Color * colors, int firstcolor,
    1.19 @@ -96,6 +98,7 @@
    1.20  
    1.21  typedef struct
    1.22  {
    1.23 +    IDirect3D9 *d3d;
    1.24      IDirect3DDevice9 *device;
    1.25      D3DPRESENT_PARAMETERS pparams;
    1.26      SDL_bool beginScene;
    1.27 @@ -103,6 +106,8 @@
    1.28  
    1.29  typedef struct
    1.30  {
    1.31 +    SDL_SW_YUVTexture *yuv;
    1.32 +    Uint32 format;
    1.33      IDirect3DTexture9 *texture;
    1.34  } D3D_TextureData;
    1.35  
    1.36 @@ -226,6 +231,50 @@
    1.37      }
    1.38  }
    1.39  
    1.40 +static SDL_bool
    1.41 +D3D_IsTextureFormatAvailable(IDirect3D9 *d3d, Uint32 display_format, Uint32 texture_format)
    1.42 +{
    1.43 +    HRESULT result;
    1.44 +
    1.45 +    result = IDirect3D9_CheckDeviceFormat(d3d,
    1.46 +                                          D3DADAPTER_DEFAULT,        /* FIXME */
    1.47 +                                          D3DDEVTYPE_HAL,
    1.48 +                                          PixelFormatToD3DFMT(display_format),
    1.49 +                                          0,
    1.50 +                                          D3DRTYPE_TEXTURE,
    1.51 +                                          PixelFormatToD3DFMT(texture_format));
    1.52 +    return FAILED(result) ? SDL_FALSE : SDL_TRUE;
    1.53 +}
    1.54 +
    1.55 +static void
    1.56 +UpdateYUVTextureData(SDL_Texture * texture)
    1.57 +{
    1.58 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
    1.59 +    SDL_Rect rect;
    1.60 +    RECT d3drect;
    1.61 +    D3DLOCKED_RECT locked;
    1.62 +    HRESULT result;
    1.63 +
    1.64 +    d3drect.left = 0;
    1.65 +    d3drect.right = texture->w;
    1.66 +    d3drect.top = 0;
    1.67 +    d3drect.bottom = texture->h;
    1.68 +
    1.69 +    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
    1.70 +    if (FAILED(result)) {
    1.71 +        return;
    1.72 +    }
    1.73 +
    1.74 +    rect.x = 0;
    1.75 +    rect.y = 0;
    1.76 +    rect.w = texture->w;
    1.77 +    rect.h = texture->h;
    1.78 +    SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
    1.79 +                        texture->h, locked.pBits, locked.Pitch);
    1.80 +
    1.81 +    IDirect3DTexture9_UnlockRect(data->texture, 0);
    1.82 +}
    1.83 +
    1.84  void
    1.85  D3D_AddRenderDriver(_THIS)
    1.86  {
    1.87 @@ -246,23 +295,18 @@
    1.88            SDL_PIXELFORMAT_RGB888,
    1.89            SDL_PIXELFORMAT_ARGB8888,
    1.90            SDL_PIXELFORMAT_ARGB2101010,
    1.91 -          SDL_PIXELFORMAT_YUY2,
    1.92 -          SDL_PIXELFORMAT_UYVY,
    1.93          };
    1.94 -        HRESULT result;
    1.95  
    1.96          for (i = 0; i < SDL_arraysize(formats); ++i) {
    1.97 -            result = IDirect3D9_CheckDeviceFormat(data->d3d,
    1.98 -                                                  D3DADAPTER_DEFAULT,        /* FIXME */
    1.99 -                                                  D3DDEVTYPE_HAL,
   1.100 -                                                  PixelFormatToD3DFMT(mode->format),
   1.101 -                                                  0,
   1.102 -                                                  D3DRTYPE_TEXTURE,
   1.103 -                                                  PixelFormatToD3DFMT(formats[i]));
   1.104 -            if (!FAILED(result)) {
   1.105 +            if (D3D_IsTextureFormatAvailable(data->d3d, mode->format, formats[i])) {
   1.106                  info->texture_formats[info->num_texture_formats++] = formats[i];
   1.107              }
   1.108          }
   1.109 +        info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YV12;
   1.110 +        info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
   1.111 +        info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YUY2;
   1.112 +        info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_UYVY;
   1.113 +        info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YVYU;
   1.114  
   1.115          SDL_AddRenderDriver(0, &D3D_RenderDriver);
   1.116      }
   1.117 @@ -293,9 +337,11 @@
   1.118          SDL_OutOfMemory();
   1.119          return NULL;
   1.120      }
   1.121 +    data->d3d = videodata->d3d;
   1.122  
   1.123      renderer->DisplayModeChanged = D3D_DisplayModeChanged;
   1.124      renderer->CreateTexture = D3D_CreateTexture;
   1.125 +    renderer->QueryTexturePixels = D3D_QueryTexturePixels;
   1.126      renderer->SetTexturePalette = D3D_SetTexturePalette;
   1.127      renderer->GetTexturePalette = D3D_GetTexturePalette;
   1.128      renderer->SetTextureColorMod = D3D_SetTextureColorMod;
   1.129 @@ -489,6 +535,7 @@
   1.130      D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   1.131      SDL_Window *window = SDL_GetWindowFromID(renderer->window);
   1.132      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
   1.133 +    Uint32 display_format = display->current_mode.format;
   1.134      D3D_TextureData *data;
   1.135      HRESULT result;
   1.136  
   1.137 @@ -500,10 +547,25 @@
   1.138  
   1.139      texture->driverdata = data;
   1.140  
   1.141 +    if (SDL_ISPIXELFORMAT_FOURCC(texture->format) &&
   1.142 +        (texture->format != SDL_PIXELFORMAT_YUY2 ||
   1.143 +         !D3D_IsTextureFormatAvailable(renderdata->d3d, display_format, texture->format)) &&
   1.144 +        (texture->format != SDL_PIXELFORMAT_YVYU ||
   1.145 +         !D3D_IsTextureFormatAvailable(renderdata->d3d, display_format, texture->format))) {
   1.146 +        data->yuv =
   1.147 +            SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
   1.148 +        if (!data->yuv) {
   1.149 +            return -1;
   1.150 +        }
   1.151 +        data->format = display->current_mode.format;
   1.152 +    } else {
   1.153 +        data->format = texture->format;
   1.154 +    }
   1.155 +
   1.156      result =
   1.157          IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   1.158                                         texture->h, 1, 0,
   1.159 -                                       PixelFormatToD3DFMT(texture->format),
   1.160 +                                       PixelFormatToD3DFMT(data->format),
   1.161                                         D3DPOOL_SDL, &data->texture, NULL);
   1.162      if (FAILED(result)) {
   1.163          D3D_SetError("CreateTexture()", result);
   1.164 @@ -514,6 +576,20 @@
   1.165  }
   1.166  
   1.167  static int
   1.168 +D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
   1.169 +                       void **pixels, int *pitch)
   1.170 +{
   1.171 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.172 +
   1.173 +    if (data->yuv) {
   1.174 +        return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
   1.175 +    } else {
   1.176 +        /* D3D textures don't have their pixels hanging out */
   1.177 +        return -1;
   1.178 +    }
   1.179 +}
   1.180 +
   1.181 +static int
   1.182  D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
   1.183                        const SDL_Color * colors, int firstcolor, int ncolors)
   1.184  {
   1.185 @@ -578,103 +654,105 @@
   1.186      return 0;
   1.187  }
   1.188  
   1.189 -#ifdef SDL_MEMORY_POOL_DEFAULT
   1.190  static int
   1.191  D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.192                    const SDL_Rect * rect, const void *pixels, int pitch)
   1.193  {
   1.194      D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.195      D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   1.196 -    IDirect3DTexture9 *temp;
   1.197 -    RECT d3drect;
   1.198 -    D3DLOCKED_RECT locked;
   1.199 -    const Uint8 *src;
   1.200 -    Uint8 *dst;
   1.201 -    int row, length;
   1.202 -    HRESULT result;
   1.203  
   1.204 -    result =
   1.205 -        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   1.206 -                                       texture->h, 1, 0,
   1.207 -                                       PixelFormatToD3DFMT(texture->format),
   1.208 -                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
   1.209 -    if (FAILED(result)) {
   1.210 -        D3D_SetError("CreateTexture()", result);
   1.211 -        return -1;
   1.212 +    if (data->yuv) {
   1.213 +        if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
   1.214 +            return -1;
   1.215 +        }
   1.216 +        UpdateYUVTextureData(texture);
   1.217 +        return 0;
   1.218 +    } else {
   1.219 +#ifdef SDL_MEMORY_POOL_DEFAULT
   1.220 +        IDirect3DTexture9 *temp;
   1.221 +        RECT d3drect;
   1.222 +        D3DLOCKED_RECT locked;
   1.223 +        const Uint8 *src;
   1.224 +        Uint8 *dst;
   1.225 +        int row, length;
   1.226 +        HRESULT result;
   1.227 +
   1.228 +        result =
   1.229 +            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   1.230 +                                           texture->h, 1, 0,
   1.231 +                                           PixelFormatToD3DFMT(texture->format),
   1.232 +                                           D3DPOOL_SYSTEMMEM, &temp, NULL);
   1.233 +        if (FAILED(result)) {
   1.234 +            D3D_SetError("CreateTexture()", result);
   1.235 +            return -1;
   1.236 +        }
   1.237 +
   1.238 +        d3drect.left = rect->x;
   1.239 +        d3drect.right = rect->x + rect->w;
   1.240 +        d3drect.top = rect->y;
   1.241 +        d3drect.bottom = rect->y + rect->h;
   1.242 +
   1.243 +        result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   1.244 +        if (FAILED(result)) {
   1.245 +            IDirect3DTexture9_Release(temp);
   1.246 +            D3D_SetError("LockRect()", result);
   1.247 +            return -1;
   1.248 +        }
   1.249 +
   1.250 +        src = pixels;
   1.251 +        dst = locked.pBits;
   1.252 +        length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.253 +        for (row = 0; row < rect->h; ++row) {
   1.254 +            SDL_memcpy(dst, src, length);
   1.255 +            src += pitch;
   1.256 +            dst += locked.Pitch;
   1.257 +        }
   1.258 +        IDirect3DTexture9_UnlockRect(temp, 0);
   1.259 +
   1.260 +        result =
   1.261 +            IDirect3DDevice9_UpdateTexture(renderdata->device,
   1.262 +                                           (IDirect3DBaseTexture9 *) temp,
   1.263 +                                           (IDirect3DBaseTexture9 *)
   1.264 +                                           data->texture);
   1.265 +        IDirect3DTexture9_Release(temp);
   1.266 +        if (FAILED(result)) {
   1.267 +            D3D_SetError("UpdateTexture()", result);
   1.268 +            return -1;
   1.269 +        }
   1.270 +#else
   1.271 +        RECT d3drect;
   1.272 +        D3DLOCKED_RECT locked;
   1.273 +        const Uint8 *src;
   1.274 +        Uint8 *dst;
   1.275 +        int row, length;
   1.276 +        HRESULT result;
   1.277 +
   1.278 +        d3drect.left = rect->x;
   1.279 +        d3drect.right = rect->x + rect->w;
   1.280 +        d3drect.top = rect->y;
   1.281 +        d3drect.bottom = rect->y + rect->h;
   1.282 +
   1.283 +        result =
   1.284 +            IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   1.285 +        if (FAILED(result)) {
   1.286 +            D3D_SetError("LockRect()", result);
   1.287 +            return -1;
   1.288 +        }
   1.289 +
   1.290 +        src = pixels;
   1.291 +        dst = locked.pBits;
   1.292 +        length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.293 +        for (row = 0; row < rect->h; ++row) {
   1.294 +            SDL_memcpy(dst, src, length);
   1.295 +            src += pitch;
   1.296 +            dst += locked.Pitch;
   1.297 +        }
   1.298 +        IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.299 +#endif // SDL_MEMORY_POOL_DEFAULT
   1.300 +
   1.301 +        return 0;
   1.302      }
   1.303 -
   1.304 -    d3drect.left = rect->x;
   1.305 -    d3drect.right = rect->x + rect->w;
   1.306 -    d3drect.top = rect->y;
   1.307 -    d3drect.bottom = rect->y + rect->h;
   1.308 -
   1.309 -    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
   1.310 -    if (FAILED(result)) {
   1.311 -        IDirect3DTexture9_Release(temp);
   1.312 -        D3D_SetError("LockRect()", result);
   1.313 -        return -1;
   1.314 -    }
   1.315 -
   1.316 -    src = pixels;
   1.317 -    dst = locked.pBits;
   1.318 -    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.319 -    for (row = 0; row < rect->h; ++row) {
   1.320 -        SDL_memcpy(dst, src, length);
   1.321 -        src += pitch;
   1.322 -        dst += locked.Pitch;
   1.323 -    }
   1.324 -    IDirect3DTexture9_UnlockRect(temp, 0);
   1.325 -
   1.326 -    result =
   1.327 -        IDirect3DDevice9_UpdateTexture(renderdata->device,
   1.328 -                                       (IDirect3DBaseTexture9 *) temp,
   1.329 -                                       (IDirect3DBaseTexture9 *)
   1.330 -                                       data->texture);
   1.331 -    IDirect3DTexture9_Release(temp);
   1.332 -    if (FAILED(result)) {
   1.333 -        D3D_SetError("UpdateTexture()", result);
   1.334 -        return -1;
   1.335 -    }
   1.336 -    return 0;
   1.337  }
   1.338 -#else
   1.339 -static int
   1.340 -D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.341 -                  const SDL_Rect * rect, const void *pixels, int pitch)
   1.342 -{
   1.343 -    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.344 -    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   1.345 -    RECT d3drect;
   1.346 -    D3DLOCKED_RECT locked;
   1.347 -    const Uint8 *src;
   1.348 -    Uint8 *dst;
   1.349 -    int row, length;
   1.350 -    HRESULT result;
   1.351 -
   1.352 -    d3drect.left = rect->x;
   1.353 -    d3drect.right = rect->x + rect->w;
   1.354 -    d3drect.top = rect->y;
   1.355 -    d3drect.bottom = rect->y + rect->h;
   1.356 -
   1.357 -    result =
   1.358 -        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   1.359 -    if (FAILED(result)) {
   1.360 -        D3D_SetError("LockRect()", result);
   1.361 -        return -1;
   1.362 -    }
   1.363 -
   1.364 -    src = pixels;
   1.365 -    dst = locked.pBits;
   1.366 -    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.367 -    for (row = 0; row < rect->h; ++row) {
   1.368 -        SDL_memcpy(dst, src, length);
   1.369 -        src += pitch;
   1.370 -        dst += locked.Pitch;
   1.371 -    }
   1.372 -    IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.373 -    return 0;
   1.374 -}
   1.375 -#endif // SDL_MEMORY_POOL_DEFAULT
   1.376  
   1.377  static int
   1.378  D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.379 @@ -682,25 +760,31 @@
   1.380                  int *pitch)
   1.381  {
   1.382      D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.383 -    RECT d3drect;
   1.384 -    D3DLOCKED_RECT locked;
   1.385 -    HRESULT result;
   1.386  
   1.387 -    d3drect.left = rect->x;
   1.388 -    d3drect.right = rect->x + rect->w;
   1.389 -    d3drect.top = rect->y;
   1.390 -    d3drect.bottom = rect->y + rect->h;
   1.391 +    if (data->yuv) {
   1.392 +        return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
   1.393 +                                     pitch);
   1.394 +    } else {
   1.395 +        RECT d3drect;
   1.396 +        D3DLOCKED_RECT locked;
   1.397 +        HRESULT result;
   1.398  
   1.399 -    result =
   1.400 -        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   1.401 -                                   markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   1.402 -    if (FAILED(result)) {
   1.403 -        D3D_SetError("LockRect()", result);
   1.404 -        return -1;
   1.405 +        d3drect.left = rect->x;
   1.406 +        d3drect.right = rect->x + rect->w;
   1.407 +        d3drect.top = rect->y;
   1.408 +        d3drect.bottom = rect->y + rect->h;
   1.409 +
   1.410 +        result =
   1.411 +            IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
   1.412 +                                       markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
   1.413 +        if (FAILED(result)) {
   1.414 +            D3D_SetError("LockRect()", result);
   1.415 +            return -1;
   1.416 +        }
   1.417 +        *pixels = locked.pBits;
   1.418 +        *pitch = locked.Pitch;
   1.419 +        return 0;
   1.420      }
   1.421 -    *pixels = locked.pBits;
   1.422 -    *pitch = locked.Pitch;
   1.423 -    return 0;
   1.424  }
   1.425  
   1.426  static void
   1.427 @@ -708,7 +792,12 @@
   1.428  {
   1.429      D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.430  
   1.431 -    IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.432 +    if (data->yuv) {
   1.433 +        SDL_SW_UnlockYUVTexture(data->yuv);
   1.434 +        UpdateYUVTextureData(texture);
   1.435 +    } else {
   1.436 +        IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.437 +    }
   1.438  }
   1.439  
   1.440  static void
   1.441 @@ -1066,6 +1155,9 @@
   1.442      if (!data) {
   1.443          return;
   1.444      }
   1.445 +    if (data->yuv) {
   1.446 +        SDL_SW_DestroyYUVTexture(data->yuv);
   1.447 +    }
   1.448      if (data->texture) {
   1.449          IDirect3DTexture9_Release(data->texture);
   1.450      }