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