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