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