src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jul 2006 07:01:35 +0000
changeset 1898 f89e49e51e89
parent 1895 c121d94672cb
child 1900 5c6bdbf3aadf
permissions -rw-r--r--
Playing around, trying to find a fast path with GDI
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#if SDL_VIDEO_RENDER_D3D
slouken@1895
    25
slouken@1895
    26
#include "SDL_win32video.h"
slouken@1895
    27
#include "../SDL_yuv_sw_c.h"
slouken@1895
    28
slouken@1895
    29
/* Direct3D renderer implementation */
slouken@1895
    30
slouken@1895
    31
static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
slouken@1895
    32
                                            Uint32 flags);
slouken@1895
    33
static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
slouken@1895
    34
                                 SDL_Texture * texture);
slouken@1895
    35
static int SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1895
    36
                                      SDL_Texture * texture, void **pixels,
slouken@1895
    37
                                      int *pitch);
slouken@1895
    38
static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
slouken@1895
    39
                                     SDL_Texture * texture,
slouken@1895
    40
                                     const SDL_Color * colors, int firstcolor,
slouken@1895
    41
                                     int ncolors);
slouken@1895
    42
static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
slouken@1895
    43
                                     SDL_Texture * texture,
slouken@1895
    44
                                     SDL_Color * colors, int firstcolor,
slouken@1895
    45
                                     int ncolors);
slouken@1895
    46
static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer,
slouken@1895
    47
                                 SDL_Texture * texture, const SDL_Rect * rect,
slouken@1895
    48
                                 const void *pixels, int pitch);
slouken@1895
    49
static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
    50
                               const SDL_Rect * rect, int markDirty,
slouken@1895
    51
                               void **pixels, int *pitch);
slouken@1895
    52
static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer,
slouken@1895
    53
                                  SDL_Texture * texture);
slouken@1895
    54
static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer,
slouken@1895
    55
                                 SDL_Texture * texture, int numrects,
slouken@1895
    56
                                 const SDL_Rect * rects);
slouken@1895
    57
static void SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer,
slouken@1895
    58
                                        SDL_Texture * texture);
slouken@1895
    59
static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
    60
                              Uint32 color);
slouken@1895
    61
static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
    62
                              const SDL_Rect * srcrect,
slouken@1895
    63
                              const SDL_Rect * dstrect, int blendMode,
slouken@1895
    64
                              int scaleMode);
slouken@1895
    65
static int SDL_D3D_RenderReadPixels(SDL_Renderer * renderer,
slouken@1895
    66
                                    const SDL_Rect * rect, void *pixels,
slouken@1895
    67
                                    int pitch);
slouken@1895
    68
static int SDL_D3D_RenderWritePixels(SDL_Renderer * renderer,
slouken@1895
    69
                                     const SDL_Rect * rect,
slouken@1895
    70
                                     const void *pixels, int pitch);
slouken@1895
    71
static void SDL_D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1895
    72
static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1895
    73
                                   SDL_Texture * texture);
slouken@1895
    74
static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    75
slouken@1895
    76
slouken@1895
    77
SDL_RenderDriver SDL_D3D_RenderDriver = {
slouken@1895
    78
    SDL_D3D_CreateRenderer,
slouken@1895
    79
    {
slouken@1895
    80
     "d3d",
slouken@1898
    81
     (SDL_Renderer_Minimal |
slouken@1898
    82
      SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
slouken@1898
    83
      SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
slouken@1898
    84
      SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
slouken@1895
    85
     (SDL_TextureBlendMode_None |
slouken@1895
    86
      SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
slouken@1895
    87
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
slouken@1895
    88
     11,
slouken@1895
    89
     {
slouken@1895
    90
      SDL_PixelFormat_Index8,
slouken@1895
    91
      SDL_PixelFormat_RGB555,
slouken@1895
    92
      SDL_PixelFormat_RGB565,
slouken@1895
    93
      SDL_PixelFormat_RGB888,
slouken@1895
    94
      SDL_PixelFormat_BGR888,
slouken@1895
    95
      SDL_PixelFormat_ARGB8888,
slouken@1895
    96
      SDL_PixelFormat_RGBA8888,
slouken@1895
    97
      SDL_PixelFormat_ABGR8888,
slouken@1895
    98
      SDL_PixelFormat_BGRA8888,
slouken@1895
    99
      SDL_PixelFormat_YUY2,
slouken@1895
   100
      SDL_PixelFormat_UYVY},
slouken@1895
   101
     0,
slouken@1895
   102
     0}
slouken@1895
   103
};
slouken@1895
   104
slouken@1895
   105
typedef struct
slouken@1895
   106
{
slouken@1895
   107
    IDirect3DDevice9 *device;
slouken@1895
   108
} SDL_D3D_RenderData;
slouken@1895
   109
slouken@1895
   110
typedef struct
slouken@1895
   111
{
slouken@1895
   112
    SDL_SW_YUVTexture *yuv;
slouken@1895
   113
} SDL_D3D_TextureData;
slouken@1895
   114
slouken@1895
   115
static void
slouken@1895
   116
UpdateYUVTextureData(SDL_Texture * texture)
slouken@1895
   117
{
slouken@1895
   118
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   119
    SDL_Rect rect;
slouken@1895
   120
slouken@1895
   121
    rect.x = 0;
slouken@1895
   122
    rect.y = 0;
slouken@1895
   123
    rect.w = texture->w;
slouken@1895
   124
    rect.h = texture->h;
slouken@1895
   125
    //SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
slouken@1895
   126
    //                    texture->h, data->pixels, data->pitch);
slouken@1895
   127
}
slouken@1895
   128
slouken@1895
   129
void
slouken@1895
   130
D3D_AddRenderDriver(_THIS)
slouken@1895
   131
{
slouken@1895
   132
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   133
slouken@1895
   134
    if (data->d3d) {
slouken@1895
   135
        SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
slouken@1895
   136
    }
slouken@1895
   137
}
slouken@1895
   138
slouken@1895
   139
SDL_Renderer *
slouken@1895
   140
SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   141
{
slouken@1895
   142
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   143
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1895
   144
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   145
    SDL_Renderer *renderer;
slouken@1895
   146
    SDL_D3D_RenderData *data;
slouken@1895
   147
slouken@1895
   148
    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
slouken@1895
   149
    if (!renderer) {
slouken@1895
   150
        SDL_OutOfMemory();
slouken@1895
   151
        return NULL;
slouken@1895
   152
    }
slouken@1895
   153
    SDL_zerop(renderer);
slouken@1895
   154
slouken@1895
   155
    data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
slouken@1895
   156
    if (!data) {
slouken@1895
   157
        SDL_D3D_DestroyRenderer(renderer);
slouken@1895
   158
        SDL_OutOfMemory();
slouken@1895
   159
        return NULL;
slouken@1895
   160
    }
slouken@1895
   161
    SDL_zerop(data);
slouken@1895
   162
slouken@1895
   163
    //data->device = IDirect3D9_CreateDevice(videodata->d3d,
slouken@1895
   164
slouken@1895
   165
    renderer->CreateTexture = SDL_D3D_CreateTexture;
slouken@1895
   166
    renderer->QueryTexturePixels = SDL_D3D_QueryTexturePixels;
slouken@1895
   167
    renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
slouken@1895
   168
    renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
slouken@1895
   169
    renderer->UpdateTexture = SDL_D3D_UpdateTexture;
slouken@1895
   170
    renderer->LockTexture = SDL_D3D_LockTexture;
slouken@1895
   171
    renderer->UnlockTexture = SDL_D3D_UnlockTexture;
slouken@1895
   172
    renderer->DirtyTexture = SDL_D3D_DirtyTexture;
slouken@1895
   173
    renderer->SelectRenderTexture = SDL_D3D_SelectRenderTexture;
slouken@1895
   174
    renderer->RenderFill = SDL_D3D_RenderFill;
slouken@1895
   175
    renderer->RenderCopy = SDL_D3D_RenderCopy;
slouken@1895
   176
    renderer->RenderReadPixels = SDL_D3D_RenderReadPixels;
slouken@1895
   177
    renderer->RenderWritePixels = SDL_D3D_RenderWritePixels;
slouken@1895
   178
    renderer->RenderPresent = SDL_D3D_RenderPresent;
slouken@1895
   179
    renderer->DestroyTexture = SDL_D3D_DestroyTexture;
slouken@1895
   180
    renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
slouken@1895
   181
    renderer->info = SDL_D3D_RenderDriver.info;
slouken@1895
   182
    renderer->window = window->id;
slouken@1895
   183
    renderer->driverdata = data;
slouken@1895
   184
slouken@1895
   185
    renderer->info.flags = SDL_Renderer_RenderTarget;
slouken@1895
   186
slouken@1895
   187
    return renderer;
slouken@1895
   188
}
slouken@1895
   189
slouken@1895
   190
static int
slouken@1895
   191
SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   192
{
slouken@1895
   193
    SDL_D3D_RenderData *renderdata =
slouken@1895
   194
        (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   195
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   196
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   197
    SDL_D3D_TextureData *data;
slouken@1895
   198
slouken@1895
   199
    data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
slouken@1895
   200
    if (!data) {
slouken@1895
   201
        SDL_OutOfMemory();
slouken@1895
   202
        return -1;
slouken@1895
   203
    }
slouken@1895
   204
    SDL_zerop(data);
slouken@1895
   205
slouken@1895
   206
    texture->driverdata = data;
slouken@1895
   207
slouken@1895
   208
    return 0;
slouken@1895
   209
}
slouken@1895
   210
slouken@1895
   211
static int
slouken@1895
   212
SDL_D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   213
                           void **pixels, int *pitch)
slouken@1895
   214
{
slouken@1895
   215
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   216
slouken@1895
   217
    if (data->yuv) {
slouken@1895
   218
        return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
slouken@1895
   219
    } else {
slouken@1895
   220
        return 0;
slouken@1895
   221
    }
slouken@1895
   222
}
slouken@1895
   223
slouken@1895
   224
static int
slouken@1895
   225
SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   226
                          const SDL_Color * colors, int firstcolor,
slouken@1895
   227
                          int ncolors)
slouken@1895
   228
{
slouken@1895
   229
    SDL_D3D_RenderData *renderdata =
slouken@1895
   230
        (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   231
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   232
slouken@1895
   233
    if (data->yuv) {
slouken@1895
   234
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   235
        return -1;
slouken@1895
   236
    } else {
slouken@1895
   237
        return 0;
slouken@1895
   238
    }
slouken@1895
   239
}
slouken@1895
   240
slouken@1895
   241
static int
slouken@1895
   242
SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   243
                          SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   244
{
slouken@1895
   245
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   246
slouken@1895
   247
    if (data->yuv) {
slouken@1895
   248
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   249
        return -1;
slouken@1895
   250
    } else {
slouken@1895
   251
        return 0;
slouken@1895
   252
    }
slouken@1895
   253
}
slouken@1895
   254
slouken@1895
   255
static int
slouken@1895
   256
SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   257
                      const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   258
{
slouken@1895
   259
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   260
slouken@1895
   261
    if (data->yuv) {
slouken@1895
   262
        if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
slouken@1895
   263
            return -1;
slouken@1895
   264
        }
slouken@1895
   265
        UpdateYUVTextureData(texture);
slouken@1895
   266
        return 0;
slouken@1895
   267
    } else {
slouken@1895
   268
        SDL_D3D_RenderData *renderdata =
slouken@1895
   269
            (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   270
slouken@1895
   271
        return 0;
slouken@1895
   272
    }
slouken@1895
   273
}
slouken@1895
   274
slouken@1895
   275
static int
slouken@1895
   276
SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   277
                    const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1895
   278
                    int *pitch)
slouken@1895
   279
{
slouken@1895
   280
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   281
slouken@1895
   282
    if (data->yuv) {
slouken@1895
   283
        return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
slouken@1895
   284
                                     pitch);
slouken@1895
   285
    } else {
slouken@1895
   286
        return 0;
slouken@1895
   287
    }
slouken@1895
   288
}
slouken@1895
   289
slouken@1895
   290
static void
slouken@1895
   291
SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   292
{
slouken@1895
   293
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   294
slouken@1895
   295
    if (data->yuv) {
slouken@1895
   296
        SDL_SW_UnlockYUVTexture(data->yuv);
slouken@1895
   297
        UpdateYUVTextureData(texture);
slouken@1895
   298
    }
slouken@1895
   299
}
slouken@1895
   300
slouken@1895
   301
static void
slouken@1895
   302
SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   303
                     int numrects, const SDL_Rect * rects)
slouken@1895
   304
{
slouken@1895
   305
}
slouken@1895
   306
slouken@1895
   307
static void
slouken@1895
   308
SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   309
{
slouken@1895
   310
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   311
}
slouken@1895
   312
slouken@1895
   313
static int
slouken@1895
   314
SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   315
                   Uint32 color)
slouken@1895
   316
{
slouken@1895
   317
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   318
    Uint8 r, g, b;
slouken@1895
   319
slouken@1895
   320
    r = (Uint8) ((color >> 16) & 0xFF);
slouken@1895
   321
    g = (Uint8) ((color >> 8) & 0xFF);
slouken@1895
   322
    b = (Uint8) (color & 0xFF);
slouken@1895
   323
slouken@1895
   324
    return 0;
slouken@1895
   325
}
slouken@1895
   326
slouken@1895
   327
static int
slouken@1895
   328
SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   329
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1895
   330
                   int blendMode, int scaleMode)
slouken@1895
   331
{
slouken@1895
   332
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   333
    SDL_D3D_TextureData *texturedata =
slouken@1895
   334
        (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   335
slouken@1895
   336
    return 0;
slouken@1895
   337
}
slouken@1895
   338
slouken@1895
   339
static int
slouken@1895
   340
SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   341
                         void *pixels, int pitch)
slouken@1895
   342
{
slouken@1895
   343
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   344
slouken@1895
   345
    return 0;
slouken@1895
   346
}
slouken@1895
   347
slouken@1895
   348
static int
slouken@1895
   349
SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   350
                          const void *pixels, int pitch)
slouken@1895
   351
{
slouken@1895
   352
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   353
slouken@1895
   354
    return 0;
slouken@1895
   355
}
slouken@1895
   356
slouken@1895
   357
static void
slouken@1895
   358
SDL_D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   359
{
slouken@1895
   360
}
slouken@1895
   361
slouken@1895
   362
static void
slouken@1895
   363
SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   364
{
slouken@1895
   365
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   366
slouken@1895
   367
    if (!data) {
slouken@1895
   368
        return;
slouken@1895
   369
    }
slouken@1895
   370
    SDL_free(data);
slouken@1895
   371
    texture->driverdata = NULL;
slouken@1895
   372
}
slouken@1895
   373
slouken@1895
   374
void
slouken@1895
   375
SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   376
{
slouken@1895
   377
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   378
slouken@1895
   379
    if (data) {
slouken@1895
   380
        SDL_free(data);
slouken@1895
   381
    }
slouken@1895
   382
    SDL_free(renderer);
slouken@1895
   383
}
slouken@1895
   384
slouken@1895
   385
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
   386
slouken@1895
   387
/* vi: set ts=4 sw=4 expandtab: */