src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 14 Jul 2006 06:40:53 +0000
changeset 1903 f132024010be
parent 1901 f1828a500391
child 1904 1a713f9d1f71
permissions -rw-r--r--
More of the Direct3D renderer is implemented, I'm not sure why it's not showing texture copies yet...
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
slouken@1895
    28
/* Direct3D renderer implementation */
slouken@1895
    29
slouken@1895
    30
static SDL_Renderer *SDL_D3D_CreateRenderer(SDL_Window * window,
slouken@1895
    31
                                            Uint32 flags);
slouken@1895
    32
static int SDL_D3D_CreateTexture(SDL_Renderer * renderer,
slouken@1895
    33
                                 SDL_Texture * texture);
slouken@1895
    34
static int SDL_D3D_SetTexturePalette(SDL_Renderer * renderer,
slouken@1895
    35
                                     SDL_Texture * texture,
slouken@1895
    36
                                     const SDL_Color * colors, int firstcolor,
slouken@1895
    37
                                     int ncolors);
slouken@1895
    38
static int SDL_D3D_GetTexturePalette(SDL_Renderer * renderer,
slouken@1895
    39
                                     SDL_Texture * texture,
slouken@1895
    40
                                     SDL_Color * colors, int firstcolor,
slouken@1895
    41
                                     int ncolors);
slouken@1895
    42
static int SDL_D3D_UpdateTexture(SDL_Renderer * renderer,
slouken@1895
    43
                                 SDL_Texture * texture, const SDL_Rect * rect,
slouken@1895
    44
                                 const void *pixels, int pitch);
slouken@1895
    45
static int SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
    46
                               const SDL_Rect * rect, int markDirty,
slouken@1895
    47
                               void **pixels, int *pitch);
slouken@1895
    48
static void SDL_D3D_UnlockTexture(SDL_Renderer * renderer,
slouken@1895
    49
                                  SDL_Texture * texture);
slouken@1895
    50
static void SDL_D3D_DirtyTexture(SDL_Renderer * renderer,
slouken@1895
    51
                                 SDL_Texture * texture, int numrects,
slouken@1895
    52
                                 const SDL_Rect * rects);
slouken@1895
    53
static void SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer,
slouken@1895
    54
                                        SDL_Texture * texture);
slouken@1895
    55
static int SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
    56
                              Uint32 color);
slouken@1895
    57
static int SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
    58
                              const SDL_Rect * srcrect,
slouken@1895
    59
                              const SDL_Rect * dstrect, int blendMode,
slouken@1895
    60
                              int scaleMode);
slouken@1895
    61
static int SDL_D3D_RenderReadPixels(SDL_Renderer * renderer,
slouken@1895
    62
                                    const SDL_Rect * rect, void *pixels,
slouken@1895
    63
                                    int pitch);
slouken@1895
    64
static int SDL_D3D_RenderWritePixels(SDL_Renderer * renderer,
slouken@1895
    65
                                     const SDL_Rect * rect,
slouken@1895
    66
                                     const void *pixels, int pitch);
slouken@1895
    67
static void SDL_D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1895
    68
static void SDL_D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1895
    69
                                   SDL_Texture * texture);
slouken@1895
    70
static void SDL_D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    71
slouken@1895
    72
slouken@1895
    73
SDL_RenderDriver SDL_D3D_RenderDriver = {
slouken@1895
    74
    SDL_D3D_CreateRenderer,
slouken@1895
    75
    {
slouken@1895
    76
     "d3d",
slouken@1900
    77
     (                          //SDL_Renderer_Minimal |
slouken@1900
    78
         SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
slouken@1900
    79
         SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
slouken@1900
    80
         SDL_Renderer_PresentDiscard | SDL_Renderer_RenderTarget),
slouken@1895
    81
     (SDL_TextureBlendMode_None |
slouken@1895
    82
      SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),
slouken@1895
    83
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),
slouken@1903
    84
     12,
slouken@1895
    85
     {
slouken@1895
    86
      SDL_PixelFormat_Index8,
slouken@1903
    87
      SDL_PixelFormat_RGB332,
slouken@1903
    88
      SDL_PixelFormat_RGB444,
slouken@1895
    89
      SDL_PixelFormat_RGB555,
slouken@1903
    90
      SDL_PixelFormat_ARGB4444,
slouken@1903
    91
      SDL_PixelFormat_ARGB1555,
slouken@1895
    92
      SDL_PixelFormat_RGB565,
slouken@1895
    93
      SDL_PixelFormat_RGB888,
slouken@1895
    94
      SDL_PixelFormat_ARGB8888,
slouken@1903
    95
      SDL_PixelFormat_ARGB2101010,
slouken@1903
    96
      SDL_PixelFormat_UYVY,
slouken@1903
    97
      SDL_PixelFormat_YUY2},
slouken@1895
    98
     0,
slouken@1895
    99
     0}
slouken@1895
   100
};
slouken@1895
   101
slouken@1895
   102
typedef struct
slouken@1895
   103
{
slouken@1895
   104
    IDirect3DDevice9 *device;
slouken@1900
   105
    SDL_bool beginScene;
slouken@1895
   106
} SDL_D3D_RenderData;
slouken@1895
   107
slouken@1895
   108
typedef struct
slouken@1895
   109
{
slouken@1903
   110
    IDirect3DTexture9 *texture;
slouken@1895
   111
} SDL_D3D_TextureData;
slouken@1895
   112
slouken@1903
   113
typedef struct
slouken@1903
   114
{
slouken@1903
   115
    float x, y, z;
slouken@1903
   116
    float tu, tv;
slouken@1903
   117
} Vertex;
slouken@1903
   118
slouken@1895
   119
static void
slouken@1900
   120
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   121
{
slouken@1900
   122
    const char *error;
slouken@1900
   123
slouken@1900
   124
    switch (result) {
slouken@1900
   125
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   126
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   127
        break;
slouken@1900
   128
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   129
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   130
        break;
slouken@1900
   131
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   132
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   133
        break;
slouken@1900
   134
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   135
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   136
        break;
slouken@1900
   137
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   138
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   139
        break;
slouken@1900
   140
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   141
        error = "TOOMANYOPERATIONS";
slouken@1900
   142
        break;
slouken@1900
   143
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   144
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   145
        break;
slouken@1900
   146
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   147
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   148
        break;
slouken@1900
   149
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   150
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   151
        break;
slouken@1900
   152
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   153
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   154
        break;
slouken@1900
   155
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   156
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   157
        break;
slouken@1900
   158
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   159
        error = "DRIVERINTERNALERROR";
slouken@1900
   160
        break;
slouken@1900
   161
    case D3DERR_NOTFOUND:
slouken@1900
   162
        error = "NOTFOUND";
slouken@1900
   163
        break;
slouken@1900
   164
    case D3DERR_MOREDATA:
slouken@1900
   165
        error = "MOREDATA";
slouken@1900
   166
        break;
slouken@1900
   167
    case D3DERR_DEVICELOST:
slouken@1900
   168
        error = "DEVICELOST";
slouken@1900
   169
        break;
slouken@1900
   170
    case D3DERR_DEVICENOTRESET:
slouken@1900
   171
        error = "DEVICENOTRESET";
slouken@1900
   172
        break;
slouken@1900
   173
    case D3DERR_NOTAVAILABLE:
slouken@1900
   174
        error = "NOTAVAILABLE";
slouken@1900
   175
        break;
slouken@1900
   176
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   177
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   178
        break;
slouken@1900
   179
    case D3DERR_INVALIDDEVICE:
slouken@1900
   180
        error = "INVALIDDEVICE";
slouken@1900
   181
        break;
slouken@1900
   182
    case D3DERR_INVALIDCALL:
slouken@1900
   183
        error = "INVALIDCALL";
slouken@1900
   184
        break;
slouken@1900
   185
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   186
        error = "DRIVERINVALIDCALL";
slouken@1900
   187
        break;
slouken@1900
   188
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   189
        error = "WASSTILLDRAWING";
slouken@1900
   190
        break;
slouken@1900
   191
    default:
slouken@1900
   192
        error = "UNKNOWN";
slouken@1900
   193
        break;
slouken@1900
   194
    }
slouken@1900
   195
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   196
}
slouken@1900
   197
slouken@1903
   198
static D3DFORMAT
slouken@1903
   199
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   200
{
slouken@1903
   201
    switch (format) {
slouken@1903
   202
    case SDL_PixelFormat_Index8:
slouken@1903
   203
        return D3DFMT_P8;
slouken@1903
   204
    case SDL_PixelFormat_RGB332:
slouken@1903
   205
        return D3DFMT_R3G3B2;
slouken@1903
   206
    case SDL_PixelFormat_RGB444:
slouken@1903
   207
        return D3DFMT_X4R4G4B4;
slouken@1903
   208
    case SDL_PixelFormat_RGB555:
slouken@1903
   209
        return D3DFMT_X1R5G5B5;
slouken@1903
   210
    case SDL_PixelFormat_ARGB4444:
slouken@1903
   211
        return D3DFMT_A4R4G4B4;
slouken@1903
   212
    case SDL_PixelFormat_ARGB1555:
slouken@1903
   213
        return D3DFMT_A1R5G5B5;
slouken@1903
   214
    case SDL_PixelFormat_RGB565:
slouken@1903
   215
        return D3DFMT_R5G6B5;
slouken@1903
   216
    case SDL_PixelFormat_RGB888:
slouken@1903
   217
        return D3DFMT_X8R8G8B8;
slouken@1903
   218
    case SDL_PixelFormat_ARGB8888:
slouken@1903
   219
        return D3DFMT_A8R8G8B8;
slouken@1903
   220
    case SDL_PixelFormat_ARGB2101010:
slouken@1903
   221
        return D3DFMT_A2R10G10B10;
slouken@1903
   222
    case SDL_PixelFormat_UYVY:
slouken@1903
   223
        return D3DFMT_UYVY;
slouken@1903
   224
    case SDL_PixelFormat_YUY2:
slouken@1903
   225
        return D3DFMT_YUY2;
slouken@1903
   226
    default:
slouken@1903
   227
        return D3DFMT_UNKNOWN;
slouken@1903
   228
    }
slouken@1895
   229
}
slouken@1895
   230
slouken@1895
   231
void
slouken@1895
   232
D3D_AddRenderDriver(_THIS)
slouken@1895
   233
{
slouken@1895
   234
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   235
slouken@1895
   236
    if (data->d3d) {
slouken@1895
   237
        SDL_AddRenderDriver(0, &SDL_D3D_RenderDriver);
slouken@1895
   238
    }
slouken@1895
   239
}
slouken@1895
   240
slouken@1895
   241
SDL_Renderer *
slouken@1895
   242
SDL_D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   243
{
slouken@1895
   244
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   245
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1895
   246
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   247
    SDL_Renderer *renderer;
slouken@1895
   248
    SDL_D3D_RenderData *data;
slouken@1900
   249
    HRESULT result;
slouken@1900
   250
    D3DPRESENT_PARAMETERS pparams;
slouken@1895
   251
slouken@1895
   252
    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
slouken@1895
   253
    if (!renderer) {
slouken@1895
   254
        SDL_OutOfMemory();
slouken@1895
   255
        return NULL;
slouken@1895
   256
    }
slouken@1895
   257
    SDL_zerop(renderer);
slouken@1895
   258
slouken@1895
   259
    data = (SDL_D3D_RenderData *) SDL_malloc(sizeof(*data));
slouken@1895
   260
    if (!data) {
slouken@1895
   261
        SDL_D3D_DestroyRenderer(renderer);
slouken@1895
   262
        SDL_OutOfMemory();
slouken@1895
   263
        return NULL;
slouken@1895
   264
    }
slouken@1895
   265
    SDL_zerop(data);
slouken@1895
   266
slouken@1895
   267
    renderer->CreateTexture = SDL_D3D_CreateTexture;
slouken@1895
   268
    renderer->SetTexturePalette = SDL_D3D_SetTexturePalette;
slouken@1895
   269
    renderer->GetTexturePalette = SDL_D3D_GetTexturePalette;
slouken@1895
   270
    renderer->UpdateTexture = SDL_D3D_UpdateTexture;
slouken@1895
   271
    renderer->LockTexture = SDL_D3D_LockTexture;
slouken@1895
   272
    renderer->UnlockTexture = SDL_D3D_UnlockTexture;
slouken@1895
   273
    renderer->DirtyTexture = SDL_D3D_DirtyTexture;
slouken@1895
   274
    renderer->SelectRenderTexture = SDL_D3D_SelectRenderTexture;
slouken@1895
   275
    renderer->RenderFill = SDL_D3D_RenderFill;
slouken@1895
   276
    renderer->RenderCopy = SDL_D3D_RenderCopy;
slouken@1895
   277
    renderer->RenderReadPixels = SDL_D3D_RenderReadPixels;
slouken@1895
   278
    renderer->RenderWritePixels = SDL_D3D_RenderWritePixels;
slouken@1895
   279
    renderer->RenderPresent = SDL_D3D_RenderPresent;
slouken@1895
   280
    renderer->DestroyTexture = SDL_D3D_DestroyTexture;
slouken@1895
   281
    renderer->DestroyRenderer = SDL_D3D_DestroyRenderer;
slouken@1895
   282
    renderer->info = SDL_D3D_RenderDriver.info;
slouken@1895
   283
    renderer->window = window->id;
slouken@1895
   284
    renderer->driverdata = data;
slouken@1895
   285
slouken@1895
   286
    renderer->info.flags = SDL_Renderer_RenderTarget;
slouken@1895
   287
slouken@1900
   288
    SDL_zero(pparams);
slouken@1900
   289
    pparams.BackBufferWidth = window->w;
slouken@1900
   290
    pparams.BackBufferHeight = window->h;
slouken@1903
   291
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   292
        pparams.BackBufferFormat =
slouken@1903
   293
            PixelFormatToD3DFMT(display->fullscreen_mode->format);
slouken@1903
   294
    } else {
slouken@1903
   295
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   296
    }
slouken@1900
   297
    if (flags & SDL_Renderer_PresentFlip2) {
slouken@1900
   298
        pparams.BackBufferCount = 2;
slouken@1900
   299
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1900
   300
    } else if (flags & SDL_Renderer_PresentFlip3) {
slouken@1900
   301
        pparams.BackBufferCount = 3;
slouken@1900
   302
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1900
   303
    } else if (flags & SDL_Renderer_PresentCopy) {
slouken@1900
   304
        pparams.BackBufferCount = 1;
slouken@1900
   305
        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
slouken@1900
   306
    } else {
slouken@1900
   307
        pparams.BackBufferCount = 1;
slouken@1900
   308
        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@1900
   309
    }
slouken@1900
   310
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   311
        pparams.Windowed = FALSE;
slouken@1903
   312
        pparams.FullScreen_RefreshRateInHz =
slouken@1903
   313
            display->fullscreen_mode->refresh_rate;
slouken@1900
   314
    } else {
slouken@1900
   315
        pparams.Windowed = TRUE;
slouken@1903
   316
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   317
    }
slouken@1901
   318
    pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1900
   319
slouken@1900
   320
    result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
slouken@1900
   321
                                     D3DDEVTYPE_HAL,
slouken@1900
   322
                                     windowdata->hwnd,
slouken@1900
   323
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   324
                                     &pparams, &data->device);
slouken@1900
   325
    if (FAILED(result)) {
slouken@1900
   326
        SDL_D3D_DestroyRenderer(renderer);
slouken@1900
   327
        D3D_SetError("CreateDevice()", result);
slouken@1900
   328
        return NULL;
slouken@1900
   329
    }
slouken@1900
   330
    data->beginScene = SDL_TRUE;
slouken@1900
   331
slouken@1903
   332
    /* Set up parameters for rendering */
slouken@1903
   333
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   334
                                    D3DCULL_NONE);
slouken@1903
   335
    IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZ | D3DFVF_TEX1);
slouken@1903
   336
slouken@1895
   337
    return renderer;
slouken@1895
   338
}
slouken@1895
   339
slouken@1895
   340
static int
slouken@1895
   341
SDL_D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   342
{
slouken@1895
   343
    SDL_D3D_RenderData *renderdata =
slouken@1895
   344
        (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   345
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   346
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   347
    SDL_D3D_TextureData *data;
slouken@1903
   348
    D3DPOOL pool;
slouken@1903
   349
    HRESULT result;
slouken@1895
   350
slouken@1895
   351
    data = (SDL_D3D_TextureData *) SDL_malloc(sizeof(*data));
slouken@1895
   352
    if (!data) {
slouken@1895
   353
        SDL_OutOfMemory();
slouken@1895
   354
        return -1;
slouken@1895
   355
    }
slouken@1895
   356
    SDL_zerop(data);
slouken@1895
   357
slouken@1895
   358
    texture->driverdata = data;
slouken@1895
   359
slouken@1903
   360
    if (texture->access == SDL_TextureAccess_Local) {
slouken@1903
   361
        pool = D3DPOOL_MANAGED;
slouken@1903
   362
    } else {
slouken@1903
   363
        pool = D3DPOOL_DEFAULT;
slouken@1903
   364
    }
slouken@1903
   365
    result =
slouken@1903
   366
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   367
                                       texture->h, 1, 0,
slouken@1903
   368
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   369
                                       pool, &data->texture, NULL);
slouken@1903
   370
    if (FAILED(result)) {
slouken@1903
   371
        SDL_free(data);
slouken@1903
   372
        D3D_SetError("CreateTexture()", result);
slouken@1903
   373
        return -1;
slouken@1903
   374
    }
slouken@1903
   375
slouken@1895
   376
    return 0;
slouken@1895
   377
}
slouken@1895
   378
slouken@1895
   379
static int
slouken@1895
   380
SDL_D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   381
                          const SDL_Color * colors, int firstcolor,
slouken@1895
   382
                          int ncolors)
slouken@1895
   383
{
slouken@1895
   384
    SDL_D3D_RenderData *renderdata =
slouken@1895
   385
        (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   386
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   387
slouken@1903
   388
    return 0;
slouken@1895
   389
}
slouken@1895
   390
slouken@1895
   391
static int
slouken@1895
   392
SDL_D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   393
                          SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   394
{
slouken@1895
   395
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   396
slouken@1903
   397
    return 0;
slouken@1895
   398
}
slouken@1895
   399
slouken@1895
   400
static int
slouken@1895
   401
SDL_D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   402
                      const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   403
{
slouken@1895
   404
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1903
   405
    SDL_D3D_RenderData *renderdata =
slouken@1903
   406
        (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1903
   407
    IDirect3DTexture9 *temp;
slouken@1903
   408
    RECT d3drect;
slouken@1903
   409
    D3DLOCKED_RECT locked;
slouken@1903
   410
    const Uint8 *src;
slouken@1903
   411
    Uint8 *dst;
slouken@1903
   412
    int row, length;
slouken@1903
   413
    HRESULT result;
slouken@1895
   414
slouken@1903
   415
    result =
slouken@1903
   416
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   417
                                       texture->h, 1, 0,
slouken@1903
   418
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   419
                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@1903
   420
    if (FAILED(result)) {
slouken@1903
   421
        D3D_SetError("CreateTexture()", result);
slouken@1903
   422
        return -1;
slouken@1903
   423
    }
slouken@1895
   424
slouken@1903
   425
    d3drect.left = rect->x;
slouken@1903
   426
    d3drect.right = rect->x + rect->w;
slouken@1903
   427
    d3drect.top = rect->y;
slouken@1903
   428
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   429
slouken@1903
   430
    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@1903
   431
    if (FAILED(result)) {
slouken@1903
   432
        IDirect3DTexture9_Release(temp);
slouken@1903
   433
        D3D_SetError("LockRect()", result);
slouken@1903
   434
        return -1;
slouken@1895
   435
    }
slouken@1903
   436
slouken@1903
   437
    src = pixels;
slouken@1903
   438
    dst = locked.pBits;
slouken@1903
   439
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1903
   440
    for (row = 0; row < rect->h; ++row) {
slouken@1903
   441
        SDL_memcpy(dst, src, length);
slouken@1903
   442
        src += pitch;
slouken@1903
   443
        dst += locked.Pitch;
slouken@1903
   444
    }
slouken@1903
   445
    IDirect3DTexture9_UnlockRect(temp, 0);
slouken@1903
   446
slouken@1903
   447
    result =
slouken@1903
   448
        IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@1903
   449
                                       (IDirect3DBaseTexture9 *) temp,
slouken@1903
   450
                                       (IDirect3DBaseTexture9 *) data->
slouken@1903
   451
                                       texture);
slouken@1903
   452
    IDirect3DTexture9_Release(temp);
slouken@1903
   453
    if (FAILED(result)) {
slouken@1903
   454
        D3D_SetError("UpdateTexture()", result);
slouken@1903
   455
        return -1;
slouken@1903
   456
    }
slouken@1903
   457
    return 0;
slouken@1895
   458
}
slouken@1895
   459
slouken@1895
   460
static int
slouken@1895
   461
SDL_D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   462
                    const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1895
   463
                    int *pitch)
slouken@1895
   464
{
slouken@1895
   465
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1903
   466
    RECT d3drect;
slouken@1903
   467
    D3DLOCKED_RECT locked;
slouken@1903
   468
    HRESULT result;
slouken@1895
   469
slouken@1903
   470
    if (texture->access != SDL_TextureAccess_Local) {
slouken@1903
   471
        SDL_SetError("Can't lock remote video memory");
slouken@1900
   472
        return -1;
slouken@1895
   473
    }
slouken@1903
   474
slouken@1903
   475
    d3drect.left = rect->x;
slouken@1903
   476
    d3drect.right = rect->x + rect->w;
slouken@1903
   477
    d3drect.top = rect->y;
slouken@1903
   478
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   479
slouken@1903
   480
    result =
slouken@1903
   481
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@1903
   482
                                   markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
slouken@1903
   483
    if (FAILED(result)) {
slouken@1903
   484
        D3D_SetError("LockRect()", result);
slouken@1903
   485
        return -1;
slouken@1903
   486
    }
slouken@1903
   487
    *pixels = locked.pBits;
slouken@1903
   488
    *pitch = locked.Pitch;
slouken@1903
   489
    return 0;
slouken@1895
   490
}
slouken@1895
   491
slouken@1895
   492
static void
slouken@1895
   493
SDL_D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   494
{
slouken@1895
   495
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   496
slouken@1903
   497
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   498
}
slouken@1895
   499
slouken@1895
   500
static void
slouken@1895
   501
SDL_D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   502
                     int numrects, const SDL_Rect * rects)
slouken@1895
   503
{
slouken@1903
   504
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1903
   505
    RECT d3drect;
slouken@1903
   506
    int i;
slouken@1903
   507
slouken@1903
   508
    for (i = 0; i < numrects; ++i) {
slouken@1903
   509
        const SDL_Rect *rect = &rects[i];
slouken@1903
   510
slouken@1903
   511
        d3drect.left = rect->x;
slouken@1903
   512
        d3drect.right = rect->x + rect->w;
slouken@1903
   513
        d3drect.top = rect->y;
slouken@1903
   514
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   515
slouken@1903
   516
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   517
    }
slouken@1895
   518
}
slouken@1895
   519
slouken@1895
   520
static void
slouken@1895
   521
SDL_D3D_SelectRenderTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   522
{
slouken@1895
   523
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   524
}
slouken@1895
   525
slouken@1895
   526
static int
slouken@1895
   527
SDL_D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   528
                   Uint32 color)
slouken@1895
   529
{
slouken@1895
   530
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1901
   531
    D3DRECT d3drect;
slouken@1900
   532
    HRESULT result;
slouken@1895
   533
slouken@1900
   534
    if (data->beginScene) {
slouken@1900
   535
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   536
        data->beginScene = SDL_FALSE;
slouken@1900
   537
    }
slouken@1895
   538
slouken@1901
   539
    d3drect.x1 = rect->x;
slouken@1903
   540
    d3drect.x2 = rect->x + rect->w;
slouken@1901
   541
    d3drect.y1 = rect->y;
slouken@1903
   542
    d3drect.y2 = rect->y + rect->h;
slouken@1901
   543
slouken@1903
   544
    result =
slouken@1903
   545
        IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
slouken@1903
   546
                               (D3DCOLOR) color, 1.0f, 0);
slouken@1900
   547
    if (FAILED(result)) {
slouken@1900
   548
        D3D_SetError("Clear()", result);
slouken@1900
   549
        return -1;
slouken@1900
   550
    }
slouken@1895
   551
    return 0;
slouken@1895
   552
}
slouken@1895
   553
slouken@1895
   554
static int
slouken@1895
   555
SDL_D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1895
   556
                   const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1895
   557
                   int blendMode, int scaleMode)
slouken@1895
   558
{
slouken@1895
   559
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   560
    SDL_D3D_TextureData *texturedata =
slouken@1895
   561
        (SDL_D3D_TextureData *) texture->driverdata;
slouken@1903
   562
    float minx, miny, maxx, maxy;
slouken@1903
   563
    float mintu, maxtu, mintv, maxtv;
slouken@1903
   564
    Vertex vertices[4];
slouken@1903
   565
    HRESULT result;
slouken@1895
   566
slouken@1900
   567
    if (data->beginScene) {
slouken@1900
   568
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   569
        data->beginScene = SDL_FALSE;
slouken@1900
   570
    }
slouken@1903
   571
slouken@1903
   572
    minx = (float) dstrect->x;
slouken@1903
   573
    miny = (float) dstrect->y;
slouken@1903
   574
    maxx = (float) dstrect->x + dstrect->w;
slouken@1903
   575
    maxy = (float) dstrect->y + dstrect->h;
slouken@1903
   576
slouken@1903
   577
    mintu = (float) srcrect->x / texture->w;
slouken@1903
   578
    maxtu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1903
   579
    mintv = (float) srcrect->y / texture->h;
slouken@1903
   580
    maxtv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
   581
slouken@1903
   582
    vertices[0].x = minx;
slouken@1903
   583
    vertices[0].y = miny;
slouken@1903
   584
    vertices[0].z = 0.0f;
slouken@1903
   585
    vertices[0].tu = mintu;
slouken@1903
   586
    vertices[0].tv = mintv;
slouken@1903
   587
    vertices[1].x = maxx;
slouken@1903
   588
    vertices[1].y = miny;
slouken@1903
   589
    vertices[1].z = 0.0f;
slouken@1903
   590
    vertices[1].tu = maxtu;
slouken@1903
   591
    vertices[1].tv = mintv;
slouken@1903
   592
    vertices[2].x = maxx;
slouken@1903
   593
    vertices[2].y = maxy;
slouken@1903
   594
    vertices[2].z = 0.0f;
slouken@1903
   595
    vertices[2].tu = maxtu;
slouken@1903
   596
    vertices[2].tv = maxtv;
slouken@1903
   597
    vertices[3].x = minx;
slouken@1903
   598
    vertices[3].y = maxy;
slouken@1903
   599
    vertices[3].z = 0.0f;
slouken@1903
   600
    vertices[3].tu = mintu;
slouken@1903
   601
    vertices[3].tv = maxtv;
slouken@1903
   602
slouken@1903
   603
    result =
slouken@1903
   604
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@1903
   605
                                    (IDirect3DBaseTexture9 *) texturedata->
slouken@1903
   606
                                    texture);
slouken@1903
   607
    if (FAILED(result)) {
slouken@1903
   608
        D3D_SetError("SetTexture()", result);
slouken@1903
   609
        return -1;
slouken@1903
   610
    }
slouken@1903
   611
    result =
slouken@1903
   612
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
   613
                                         vertices, sizeof(*vertices));
slouken@1903
   614
    if (FAILED(result)) {
slouken@1903
   615
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
   616
        return -1;
slouken@1903
   617
    }
slouken@1895
   618
    return 0;
slouken@1895
   619
}
slouken@1895
   620
slouken@1895
   621
static int
slouken@1895
   622
SDL_D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   623
                         void *pixels, int pitch)
slouken@1895
   624
{
slouken@1895
   625
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   626
slouken@1895
   627
    return 0;
slouken@1895
   628
}
slouken@1895
   629
slouken@1895
   630
static int
slouken@1895
   631
SDL_D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1895
   632
                          const void *pixels, int pitch)
slouken@1895
   633
{
slouken@1895
   634
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   635
slouken@1895
   636
    return 0;
slouken@1895
   637
}
slouken@1895
   638
slouken@1895
   639
static void
slouken@1895
   640
SDL_D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   641
{
slouken@1900
   642
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1900
   643
    HRESULT result;
slouken@1900
   644
slouken@1900
   645
    if (!data->beginScene) {
slouken@1900
   646
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
   647
        data->beginScene = SDL_TRUE;
slouken@1900
   648
    }
slouken@1900
   649
slouken@1900
   650
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
   651
    if (FAILED(result)) {
slouken@1900
   652
        D3D_SetError("Present()", result);
slouken@1900
   653
    }
slouken@1895
   654
}
slouken@1895
   655
slouken@1895
   656
static void
slouken@1895
   657
SDL_D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   658
{
slouken@1895
   659
    SDL_D3D_TextureData *data = (SDL_D3D_TextureData *) texture->driverdata;
slouken@1895
   660
slouken@1895
   661
    if (!data) {
slouken@1895
   662
        return;
slouken@1895
   663
    }
slouken@1903
   664
    if (data->texture) {
slouken@1903
   665
        IDirect3DTexture9_Release(data->texture);
slouken@1903
   666
    }
slouken@1895
   667
    SDL_free(data);
slouken@1895
   668
    texture->driverdata = NULL;
slouken@1895
   669
}
slouken@1895
   670
slouken@1895
   671
void
slouken@1895
   672
SDL_D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   673
{
slouken@1895
   674
    SDL_D3D_RenderData *data = (SDL_D3D_RenderData *) renderer->driverdata;
slouken@1895
   675
slouken@1895
   676
    if (data) {
slouken@1900
   677
        if (data->device) {
slouken@1900
   678
            IDirect3DDevice9_Release(data->device);
slouken@1900
   679
        }
slouken@1895
   680
        SDL_free(data);
slouken@1895
   681
    }
slouken@1895
   682
    SDL_free(renderer);
slouken@1895
   683
}
slouken@1895
   684
slouken@1895
   685
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
   686
slouken@1895
   687
/* vi: set ts=4 sw=4 expandtab: */