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