Standardized on using the managed texture pool.
authorSam Lantinga <slouken@libsdl.org>
Fri, 04 Feb 2011 12:25:26 -0800
changeset 5173ebfedf3787b1
parent 5172 51b4cfdf7ebb
child 5174 b9cf75615028
Standardized on using the managed texture pool.
Also experimented with dynamic textures, but didn't get any speed increase with them. More research and testing is needed.
src/render/direct3d/SDL_d3drender.c
     1.1 --- a/src/render/direct3d/SDL_d3drender.c	Fri Feb 04 12:24:28 2011 -0800
     1.2 +++ b/src/render/direct3d/SDL_d3drender.c	Fri Feb 04 12:25:26 2011 -0800
     1.3 @@ -88,15 +88,6 @@
     1.4  
     1.5  /* Direct3D renderer implementation */
     1.6  
     1.7 -#if 1
     1.8 -/* This takes more memory but you won't lose your texture data */
     1.9 -#define D3DPOOL_SDL    D3DPOOL_MANAGED
    1.10 -#define SDL_MEMORY_POOL_MANAGED
    1.11 -#else
    1.12 -#define D3DPOOL_SDL    D3DPOOL_DEFAULT
    1.13 -#define SDL_MEMORY_POOL_DEFAULT
    1.14 -#endif
    1.15 -
    1.16  static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.17  static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.18  static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.19 @@ -144,7 +135,6 @@
    1.20  
    1.21  typedef struct
    1.22  {
    1.23 -    Uint32 format;
    1.24      IDirect3DTexture9 *texture;
    1.25  } D3D_TextureData;
    1.26  
    1.27 @@ -493,6 +483,8 @@
    1.28      SDL_Window *window = renderer->window;
    1.29      D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
    1.30      D3D_TextureData *data;
    1.31 +    D3DPOOL pool;
    1.32 +    DWORD usage;
    1.33      HRESULT result;
    1.34  
    1.35      data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
    1.36 @@ -503,13 +495,22 @@
    1.37  
    1.38      texture->driverdata = data;
    1.39  
    1.40 -    data->format = texture->format;
    1.41 +#ifdef USE_DYNAMIC_TEXTURE
    1.42 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
    1.43 +        pool = D3DPOOL_DEFAULT;
    1.44 +        usage = D3DUSAGE_DYNAMIC;
    1.45 +    } else
    1.46 +#endif
    1.47 +    {
    1.48 +        pool = D3DPOOL_MANAGED;
    1.49 +        usage = 0;
    1.50 +    }
    1.51  
    1.52      result =
    1.53          IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
    1.54 -                                       texture->h, 1, 0,
    1.55 -                                       PixelFormatToD3DFMT(data->format),
    1.56 -                                       D3DPOOL_SDL, &data->texture, NULL);
    1.57 +                                       texture->h, 1, usage,
    1.58 +                                       PixelFormatToD3DFMT(texture->format),
    1.59 +                                       pool, &data->texture, NULL);
    1.60      if (FAILED(result)) {
    1.61          D3D_SetError("CreateTexture()", result);
    1.62          return -1;
    1.63 @@ -524,9 +525,6 @@
    1.64  {
    1.65      D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
    1.66      D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
    1.67 -
    1.68 -#ifdef SDL_MEMORY_POOL_DEFAULT
    1.69 -    IDirect3DTexture9 *temp;
    1.70      RECT d3drect;
    1.71      D3DLOCKED_RECT locked;
    1.72      const Uint8 *src;
    1.73 @@ -534,62 +532,21 @@
    1.74      int row, length;
    1.75      HRESULT result;
    1.76  
    1.77 -    result =
    1.78 -        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
    1.79 -                                       texture->h, 1, 0,
    1.80 -                                       PixelFormatToD3DFMT(texture-> format),
    1.81 -                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
    1.82 -    if (FAILED(result)) {
    1.83 -        D3D_SetError("CreateTexture()", result);
    1.84 -        return -1;
    1.85 -    }
    1.86 -
    1.87 -    d3drect.left = rect->x;
    1.88 -    d3drect.right = rect->x + rect->w;
    1.89 -    d3drect.top = rect->y;
    1.90 -    d3drect.bottom = rect->y + rect->h;
    1.91 -
    1.92 -    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
    1.93 -    if (FAILED(result)) {
    1.94 -        IDirect3DTexture9_Release(temp);
    1.95 -        D3D_SetError("LockRect()", result);
    1.96 -        return -1;
    1.97 +#ifdef USE_DYNAMIC_TEXTURE
    1.98 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
    1.99 +        rect->x == 0 && rect->y == 0 &&
   1.100 +        rect->w == texture->w && rect->h == texture->h) {
   1.101 +        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
   1.102 +    } else
   1.103 +#endif
   1.104 +    {
   1.105 +        d3drect.left = rect->x;
   1.106 +        d3drect.right = rect->x + rect->w;
   1.107 +        d3drect.top = rect->y;
   1.108 +        d3drect.bottom = rect->y + rect->h;
   1.109 +        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   1.110      }
   1.111  
   1.112 -    src = pixels;
   1.113 -    dst = locked.pBits;
   1.114 -    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.115 -    for (row = 0; row < rect->h; ++row) {
   1.116 -        SDL_memcpy(dst, src, length);
   1.117 -        src += pitch;
   1.118 -        dst += locked.Pitch;
   1.119 -    }
   1.120 -    IDirect3DTexture9_UnlockRect(temp, 0);
   1.121 -
   1.122 -    result =
   1.123 -        IDirect3DDevice9_UpdateTexture(renderdata->device,
   1.124 -                                       (IDirect3DBaseTexture9 *) temp,
   1.125 -                                       (IDirect3DBaseTexture9 *)
   1.126 -                                       data->texture);
   1.127 -    IDirect3DTexture9_Release(temp);
   1.128 -    if (FAILED(result)) {
   1.129 -        D3D_SetError("UpdateTexture()", result);
   1.130 -        return -1;
   1.131 -    }
   1.132 -#else
   1.133 -    RECT d3drect;
   1.134 -    D3DLOCKED_RECT locked;
   1.135 -    const Uint8 *src;
   1.136 -    Uint8 *dst;
   1.137 -    int row, length;
   1.138 -    HRESULT result;
   1.139 -
   1.140 -    d3drect.left = rect->x;
   1.141 -    d3drect.right = rect->x + rect->w;
   1.142 -    d3drect.top = rect->y;
   1.143 -    d3drect.bottom = rect->y + rect->h;
   1.144 -
   1.145 -    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   1.146      if (FAILED(result)) {
   1.147          D3D_SetError("LockRect()", result);
   1.148          return -1;
   1.149 @@ -598,13 +555,16 @@
   1.150      src = pixels;
   1.151      dst = locked.pBits;
   1.152      length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.153 -    for (row = 0; row < rect->h; ++row) {
   1.154 -        SDL_memcpy(dst, src, length);
   1.155 -        src += pitch;
   1.156 -        dst += locked.Pitch;
   1.157 +    if (length == pitch && length == locked.Pitch) {
   1.158 +        SDL_memcpy(dst, src, length*rect->h);
   1.159 +    } else {
   1.160 +        for (row = 0; row < rect->h; ++row) {
   1.161 +            SDL_memcpy(dst, src, length);
   1.162 +            src += pitch;
   1.163 +            dst += locked.Pitch;
   1.164 +        }
   1.165      }
   1.166      IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.167 -#endif // SDL_MEMORY_POOL_DEFAULT
   1.168  
   1.169      return 0;
   1.170  }