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