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