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