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