src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 31 Dec 2008 06:18:33 +0000
changeset 2933 73f84b1e25e5
parent 2932 676754cc1acb
child 2972 0a4b70368372
permissions -rw-r--r--
indent
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@2786
    30
#if 1                           /* This takes more memory but you won't lose your texture data */
slouken@2932
    31
#define D3DPOOL_SDL    D3DPOOL_MANAGED
slouken@2783
    32
#define SDL_MEMORY_POOL_MANAGED
slouken@2783
    33
#else
slouken@2932
    34
#define D3DPOOL_SDL    D3DPOOL_DEFAULT
slouken@2783
    35
#define SDL_MEMORY_POOL_DEFAULT
slouken@2783
    36
#endif
slouken@2783
    37
slouken@1913
    38
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1975
    39
static int D3D_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1913
    40
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    41
static int D3D_SetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    42
                                 SDL_Texture * texture,
slouken@1913
    43
                                 const SDL_Color * colors, int firstcolor,
slouken@1913
    44
                                 int ncolors);
slouken@1913
    45
static int D3D_GetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    46
                                 SDL_Texture * texture, SDL_Color * colors,
slouken@1913
    47
                                 int firstcolor, int ncolors);
slouken@1985
    48
static int D3D_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    49
                                  SDL_Texture * texture);
slouken@1985
    50
static int D3D_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    51
                                  SDL_Texture * texture);
slouken@1985
    52
static int D3D_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    53
                                   SDL_Texture * texture);
slouken@1985
    54
static int D3D_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    55
                                   SDL_Texture * texture);
slouken@1913
    56
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    57
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
    58
                             int pitch);
slouken@1913
    59
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    60
                           const SDL_Rect * rect, int markDirty,
slouken@1913
    61
                           void **pixels, int *pitch);
slouken@1913
    62
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    63
static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    64
                             int numrects, const SDL_Rect * rects);
slouken@2932
    65
static int D3D_RenderPoint(SDL_Renderer * renderer, int x, int y);
slouken@2932
    66
static int D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
slouken@2932
    67
                          int y2);
slouken@2919
    68
static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
slouken@1913
    69
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    70
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@1913
    71
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
    72
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
    73
                               SDL_Texture * texture);
slouken@1913
    74
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    75
slouken@1895
    76
slouken@1913
    77
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
    78
    D3D_CreateRenderer,
slouken@1895
    79
    {
slouken@1895
    80
     "d3d",
slouken@1965
    81
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1975
    82
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    83
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC |
slouken@1965
    84
      SDL_RENDERER_ACCELERATED),
slouken@1985
    85
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    86
      SDL_TEXTUREMODULATE_ALPHA),
slouken@2884
    87
     (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
slouken@2884
    88
      SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
slouken@1965
    89
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
    90
      SDL_TEXTURESCALEMODE_SLOW | SDL_TEXTURESCALEMODE_BEST),
slouken@2811
    91
     12,
slouken@1895
    92
     {
slouken@1965
    93
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    94
      SDL_PIXELFORMAT_RGB332,
slouken@1965
    95
      SDL_PIXELFORMAT_RGB444,
slouken@1965
    96
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    97
      SDL_PIXELFORMAT_ARGB4444,
slouken@1965
    98
      SDL_PIXELFORMAT_ARGB1555,
slouken@1965
    99
      SDL_PIXELFORMAT_RGB565,
slouken@1965
   100
      SDL_PIXELFORMAT_RGB888,
slouken@1965
   101
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
   102
      SDL_PIXELFORMAT_ARGB2101010,
slouken@2810
   103
      SDL_PIXELFORMAT_YUY2,
slouken@2811
   104
      SDL_PIXELFORMAT_UYVY},
slouken@1895
   105
     0,
slouken@1895
   106
     0}
slouken@1895
   107
};
slouken@1895
   108
slouken@1895
   109
typedef struct
slouken@1895
   110
{
slouken@1895
   111
    IDirect3DDevice9 *device;
slouken@1975
   112
    D3DPRESENT_PARAMETERS pparams;
slouken@1900
   113
    SDL_bool beginScene;
slouken@1913
   114
} D3D_RenderData;
slouken@1895
   115
slouken@1895
   116
typedef struct
slouken@1895
   117
{
slouken@1903
   118
    IDirect3DTexture9 *texture;
slouken@1913
   119
} D3D_TextureData;
slouken@1895
   120
slouken@1903
   121
typedef struct
slouken@1903
   122
{
slouken@1903
   123
    float x, y, z;
slouken@1904
   124
    float rhw;
slouken@1987
   125
    DWORD color;
slouken@1904
   126
    float u, v;
slouken@1903
   127
} Vertex;
slouken@1903
   128
slouken@1895
   129
static void
slouken@1900
   130
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   131
{
slouken@1900
   132
    const char *error;
slouken@1900
   133
slouken@1900
   134
    switch (result) {
slouken@1900
   135
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   136
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   137
        break;
slouken@1900
   138
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   139
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   140
        break;
slouken@1900
   141
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   142
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   143
        break;
slouken@1900
   144
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   145
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   146
        break;
slouken@1900
   147
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   148
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   149
        break;
slouken@1900
   150
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   151
        error = "TOOMANYOPERATIONS";
slouken@1900
   152
        break;
slouken@1900
   153
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   154
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   155
        break;
slouken@1900
   156
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   157
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   158
        break;
slouken@1900
   159
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   160
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   161
        break;
slouken@1900
   162
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   163
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   164
        break;
slouken@1900
   165
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   166
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   167
        break;
slouken@1900
   168
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   169
        error = "DRIVERINTERNALERROR";
slouken@1900
   170
        break;
slouken@1900
   171
    case D3DERR_NOTFOUND:
slouken@1900
   172
        error = "NOTFOUND";
slouken@1900
   173
        break;
slouken@1900
   174
    case D3DERR_MOREDATA:
slouken@1900
   175
        error = "MOREDATA";
slouken@1900
   176
        break;
slouken@1900
   177
    case D3DERR_DEVICELOST:
slouken@1900
   178
        error = "DEVICELOST";
slouken@1900
   179
        break;
slouken@1900
   180
    case D3DERR_DEVICENOTRESET:
slouken@1900
   181
        error = "DEVICENOTRESET";
slouken@1900
   182
        break;
slouken@1900
   183
    case D3DERR_NOTAVAILABLE:
slouken@1900
   184
        error = "NOTAVAILABLE";
slouken@1900
   185
        break;
slouken@1900
   186
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   187
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   188
        break;
slouken@1900
   189
    case D3DERR_INVALIDDEVICE:
slouken@1900
   190
        error = "INVALIDDEVICE";
slouken@1900
   191
        break;
slouken@1900
   192
    case D3DERR_INVALIDCALL:
slouken@1900
   193
        error = "INVALIDCALL";
slouken@1900
   194
        break;
slouken@1900
   195
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   196
        error = "DRIVERINVALIDCALL";
slouken@1900
   197
        break;
slouken@1900
   198
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   199
        error = "WASSTILLDRAWING";
slouken@1900
   200
        break;
slouken@1900
   201
    default:
slouken@1900
   202
        error = "UNKNOWN";
slouken@1900
   203
        break;
slouken@1900
   204
    }
slouken@1900
   205
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   206
}
slouken@1900
   207
slouken@1903
   208
static D3DFORMAT
slouken@1903
   209
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   210
{
slouken@1903
   211
    switch (format) {
slouken@1965
   212
    case SDL_PIXELFORMAT_INDEX8:
slouken@1903
   213
        return D3DFMT_P8;
slouken@1965
   214
    case SDL_PIXELFORMAT_RGB332:
slouken@1903
   215
        return D3DFMT_R3G3B2;
slouken@1965
   216
    case SDL_PIXELFORMAT_RGB444:
slouken@1903
   217
        return D3DFMT_X4R4G4B4;
slouken@1965
   218
    case SDL_PIXELFORMAT_RGB555:
slouken@1903
   219
        return D3DFMT_X1R5G5B5;
slouken@1965
   220
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1903
   221
        return D3DFMT_A4R4G4B4;
slouken@1965
   222
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1903
   223
        return D3DFMT_A1R5G5B5;
slouken@1965
   224
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   225
        return D3DFMT_R5G6B5;
slouken@1965
   226
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   227
        return D3DFMT_X8R8G8B8;
slouken@1965
   228
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   229
        return D3DFMT_A8R8G8B8;
slouken@1965
   230
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1903
   231
        return D3DFMT_A2R10G10B10;
slouken@1965
   232
    case SDL_PIXELFORMAT_UYVY:
slouken@1903
   233
        return D3DFMT_UYVY;
slouken@1965
   234
    case SDL_PIXELFORMAT_YUY2:
slouken@1903
   235
        return D3DFMT_YUY2;
slouken@1903
   236
    default:
slouken@1903
   237
        return D3DFMT_UNKNOWN;
slouken@1903
   238
    }
slouken@1895
   239
}
slouken@1895
   240
slouken@1895
   241
void
slouken@1895
   242
D3D_AddRenderDriver(_THIS)
slouken@1895
   243
{
slouken@1895
   244
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   245
slouken@1895
   246
    if (data->d3d) {
slouken@1913
   247
        SDL_AddRenderDriver(0, &D3D_RenderDriver);
slouken@1895
   248
    }
slouken@1895
   249
}
slouken@1895
   250
slouken@1895
   251
SDL_Renderer *
slouken@1913
   252
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   253
{
slouken@1895
   254
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   255
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1895
   256
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   257
    SDL_Renderer *renderer;
slouken@1913
   258
    D3D_RenderData *data;
slouken@1900
   259
    HRESULT result;
slouken@1900
   260
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   261
    IDirect3DSwapChain9 *chain;
slouken@1925
   262
    D3DCAPS9 caps;
slouken@1895
   263
slouken@1920
   264
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   265
    if (!renderer) {
slouken@1895
   266
        SDL_OutOfMemory();
slouken@1895
   267
        return NULL;
slouken@1895
   268
    }
slouken@1895
   269
slouken@1920
   270
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   271
    if (!data) {
slouken@1913
   272
        D3D_DestroyRenderer(renderer);
slouken@1895
   273
        SDL_OutOfMemory();
slouken@1895
   274
        return NULL;
slouken@1895
   275
    }
slouken@1895
   276
slouken@1975
   277
    renderer->DisplayModeChanged = D3D_DisplayModeChanged;
slouken@1913
   278
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   279
    renderer->SetTexturePalette = D3D_SetTexturePalette;
slouken@1913
   280
    renderer->GetTexturePalette = D3D_GetTexturePalette;
slouken@1985
   281
    renderer->SetTextureColorMod = D3D_SetTextureColorMod;
slouken@1985
   282
    renderer->SetTextureAlphaMod = D3D_SetTextureAlphaMod;
slouken@1985
   283
    renderer->SetTextureBlendMode = D3D_SetTextureBlendMode;
slouken@1985
   284
    renderer->SetTextureScaleMode = D3D_SetTextureScaleMode;
slouken@1913
   285
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   286
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   287
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@1913
   288
    renderer->DirtyTexture = D3D_DirtyTexture;
slouken@2932
   289
    renderer->RenderPoint = D3D_RenderPoint;
slouken@2932
   290
    renderer->RenderLine = D3D_RenderLine;
slouken@1913
   291
    renderer->RenderFill = D3D_RenderFill;
slouken@1913
   292
    renderer->RenderCopy = D3D_RenderCopy;
slouken@1913
   293
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   294
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   295
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   296
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   297
    renderer->window = window->id;
slouken@1895
   298
    renderer->driverdata = data;
slouken@1895
   299
slouken@1965
   300
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   301
slouken@1900
   302
    SDL_zero(pparams);
slouken@1900
   303
    pparams.BackBufferWidth = window->w;
slouken@1900
   304
    pparams.BackBufferHeight = window->h;
slouken@1903
   305
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   306
        pparams.BackBufferFormat =
slouken@1975
   307
            PixelFormatToD3DFMT(display->fullscreen_mode.format);
slouken@1903
   308
    } else {
slouken@1903
   309
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   310
    }
slouken@1965
   311
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1900
   312
        pparams.BackBufferCount = 2;
slouken@1900
   313
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1965
   314
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1900
   315
        pparams.BackBufferCount = 3;
slouken@1900
   316
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1965
   317
    } else if (flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1900
   318
        pparams.BackBufferCount = 1;
slouken@1900
   319
        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
slouken@1900
   320
    } else {
slouken@1900
   321
        pparams.BackBufferCount = 1;
slouken@1900
   322
        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@1900
   323
    }
slouken@1900
   324
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   325
        pparams.Windowed = FALSE;
slouken@1903
   326
        pparams.FullScreen_RefreshRateInHz =
slouken@1975
   327
            display->fullscreen_mode.refresh_rate;
slouken@1900
   328
    } else {
slouken@1900
   329
        pparams.Windowed = TRUE;
slouken@1903
   330
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   331
    }
slouken@1965
   332
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   333
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   334
    } else {
slouken@1907
   335
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   336
    }
slouken@1900
   337
slouken@1900
   338
    result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
slouken@1900
   339
                                     D3DDEVTYPE_HAL,
slouken@1900
   340
                                     windowdata->hwnd,
slouken@1900
   341
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   342
                                     &pparams, &data->device);
slouken@1900
   343
    if (FAILED(result)) {
slouken@1913
   344
        D3D_DestroyRenderer(renderer);
slouken@1900
   345
        D3D_SetError("CreateDevice()", result);
slouken@1900
   346
        return NULL;
slouken@1900
   347
    }
slouken@1900
   348
    data->beginScene = SDL_TRUE;
slouken@1900
   349
slouken@1907
   350
    /* Get presentation parameters to fill info */
slouken@1907
   351
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   352
    if (FAILED(result)) {
slouken@1913
   353
        D3D_DestroyRenderer(renderer);
slouken@1907
   354
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   355
        return NULL;
slouken@1907
   356
    }
slouken@1907
   357
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   358
    if (FAILED(result)) {
slouken@1907
   359
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   360
        D3D_DestroyRenderer(renderer);
slouken@1907
   361
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   362
        return NULL;
slouken@1907
   363
    }
slouken@1907
   364
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   365
    switch (pparams.SwapEffect) {
slouken@1907
   366
    case D3DSWAPEFFECT_COPY:
slouken@1965
   367
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1907
   368
        break;
slouken@1907
   369
    case D3DSWAPEFFECT_FLIP:
slouken@1907
   370
        switch (pparams.BackBufferCount) {
slouken@1907
   371
        case 2:
slouken@1965
   372
            renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1907
   373
            break;
slouken@1907
   374
        case 3:
slouken@1965
   375
            renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1907
   376
            break;
slouken@1907
   377
        }
slouken@1907
   378
        break;
slouken@1907
   379
    case D3DSWAPEFFECT_DISCARD:
slouken@1965
   380
        renderer->info.flags |= SDL_RENDERER_PRESENTDISCARD;
slouken@1907
   381
        break;
slouken@1907
   382
    }
slouken@1907
   383
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   384
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   385
    }
slouken@1975
   386
    data->pparams = pparams;
slouken@1907
   387
slouken@1925
   388
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   389
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   390
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@1918
   391
slouken@1903
   392
    /* Set up parameters for rendering */
slouken@1904
   393
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   394
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   395
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   396
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   397
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   398
                                    D3DCULL_NONE);
slouken@1904
   399
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   400
    /* Enable color modulation by diffuse color */
slouken@1988
   401
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   402
                                          D3DTOP_MODULATE);
slouken@1988
   403
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   404
                                          D3DTA_TEXTURE);
slouken@1988
   405
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   406
                                          D3DTA_DIFFUSE);
slouken@1988
   407
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   408
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   409
                                          D3DTOP_MODULATE);
slouken@1988
   410
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   411
                                          D3DTA_TEXTURE);
slouken@1988
   412
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   413
                                          D3DTA_DIFFUSE);
slouken@1991
   414
    /* Disable second texture stage, since we're done */
slouken@1991
   415
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   416
                                          D3DTOP_DISABLE);
slouken@1991
   417
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   418
                                          D3DTOP_DISABLE);
slouken@1903
   419
slouken@1895
   420
    return renderer;
slouken@1895
   421
}
slouken@1895
   422
slouken@1895
   423
static int
slouken@1975
   424
D3D_Reset(SDL_Renderer * renderer)
slouken@1975
   425
{
slouken@1975
   426
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   427
    HRESULT result;
slouken@1975
   428
slouken@1975
   429
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@1975
   430
    if (FAILED(result)) {
slouken@1975
   431
        if (result == D3DERR_DEVICELOST) {
slouken@1975
   432
            /* Don't worry about it, we'll reset later... */
slouken@1975
   433
            return 0;
slouken@1975
   434
        } else {
slouken@1975
   435
            D3D_SetError("Reset()", result);
slouken@1975
   436
            return -1;
slouken@1975
   437
        }
slouken@1975
   438
    }
slouken@1975
   439
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   440
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   441
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1975
   442
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1975
   443
                                    D3DCULL_NONE);
slouken@1975
   444
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1975
   445
    return 0;
slouken@1975
   446
}
slouken@1975
   447
slouken@1975
   448
static int
slouken@1975
   449
D3D_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1975
   450
{
slouken@1975
   451
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   452
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1975
   453
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1975
   454
slouken@1975
   455
    data->pparams.BackBufferWidth = window->w;
slouken@1975
   456
    data->pparams.BackBufferHeight = window->h;
slouken@1975
   457
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1975
   458
        data->pparams.BackBufferFormat =
slouken@1975
   459
            PixelFormatToD3DFMT(display->fullscreen_mode.format);
slouken@1975
   460
    } else {
slouken@1975
   461
        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1975
   462
    }
slouken@1975
   463
    return D3D_Reset(renderer);
slouken@1975
   464
}
slouken@1975
   465
slouken@1975
   466
static int
slouken@1913
   467
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   468
{
slouken@1913
   469
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   470
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   471
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1913
   472
    D3D_TextureData *data;
slouken@1903
   473
    HRESULT result;
slouken@1895
   474
slouken@1920
   475
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   476
    if (!data) {
slouken@1895
   477
        SDL_OutOfMemory();
slouken@1895
   478
        return -1;
slouken@1895
   479
    }
slouken@1895
   480
slouken@1895
   481
    texture->driverdata = data;
slouken@1895
   482
slouken@1903
   483
    result =
slouken@1903
   484
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   485
                                       texture->h, 1, 0,
slouken@1903
   486
                                       PixelFormatToD3DFMT(texture->format),
slouken@2783
   487
                                       D3DPOOL_SDL, &data->texture, NULL);
slouken@1903
   488
    if (FAILED(result)) {
slouken@1903
   489
        D3D_SetError("CreateTexture()", result);
slouken@1903
   490
        return -1;
slouken@1903
   491
    }
slouken@1903
   492
slouken@1895
   493
    return 0;
slouken@1895
   494
}
slouken@1895
   495
slouken@1895
   496
static int
slouken@1913
   497
D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   498
                      const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   499
{
slouken@1913
   500
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   501
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   502
slouken@1903
   503
    return 0;
slouken@1895
   504
}
slouken@1895
   505
slouken@1895
   506
static int
slouken@1913
   507
D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   508
                      SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   509
{
slouken@1913
   510
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   511
slouken@1903
   512
    return 0;
slouken@1895
   513
}
slouken@1895
   514
slouken@1895
   515
static int
slouken@1985
   516
D3D_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   517
{
slouken@1987
   518
    return 0;
slouken@1985
   519
}
slouken@1985
   520
slouken@1985
   521
static int
slouken@1985
   522
D3D_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   523
{
slouken@1987
   524
    return 0;
slouken@1985
   525
}
slouken@1985
   526
slouken@1985
   527
static int
slouken@1985
   528
D3D_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   529
{
slouken@1985
   530
    switch (texture->blendMode) {
slouken@2884
   531
    case SDL_BLENDMODE_NONE:
slouken@2884
   532
    case SDL_BLENDMODE_MASK:
slouken@2884
   533
    case SDL_BLENDMODE_BLEND:
slouken@2884
   534
    case SDL_BLENDMODE_ADD:
slouken@2884
   535
    case SDL_BLENDMODE_MOD:
slouken@1985
   536
        return 0;
slouken@1985
   537
    default:
slouken@1985
   538
        SDL_Unsupported();
slouken@2884
   539
        texture->blendMode = SDL_BLENDMODE_NONE;
slouken@1985
   540
        return -1;
slouken@1985
   541
    }
slouken@1985
   542
}
slouken@1985
   543
slouken@1985
   544
static int
slouken@1985
   545
D3D_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   546
{
slouken@1985
   547
    switch (texture->scaleMode) {
slouken@1985
   548
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   549
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   550
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   551
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   552
        return 0;
slouken@1985
   553
    default:
slouken@1985
   554
        SDL_Unsupported();
slouken@1985
   555
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   556
        return -1;
slouken@1985
   557
    }
slouken@1985
   558
    return 0;
slouken@1985
   559
}
slouken@1985
   560
slouken@2783
   561
#ifdef SDL_MEMORY_POOL_DEFAULT
slouken@1985
   562
static int
slouken@1913
   563
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   564
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   565
{
slouken@1913
   566
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   567
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1903
   568
    IDirect3DTexture9 *temp;
slouken@1903
   569
    RECT d3drect;
slouken@1903
   570
    D3DLOCKED_RECT locked;
slouken@1903
   571
    const Uint8 *src;
slouken@1903
   572
    Uint8 *dst;
slouken@1903
   573
    int row, length;
slouken@1903
   574
    HRESULT result;
slouken@1895
   575
slouken@1903
   576
    result =
slouken@1903
   577
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   578
                                       texture->h, 1, 0,
slouken@1903
   579
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   580
                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@1903
   581
    if (FAILED(result)) {
slouken@1903
   582
        D3D_SetError("CreateTexture()", result);
slouken@1903
   583
        return -1;
slouken@1903
   584
    }
slouken@1895
   585
slouken@1903
   586
    d3drect.left = rect->x;
slouken@1903
   587
    d3drect.right = rect->x + rect->w;
slouken@1903
   588
    d3drect.top = rect->y;
slouken@1903
   589
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   590
slouken@1903
   591
    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@1903
   592
    if (FAILED(result)) {
slouken@1903
   593
        IDirect3DTexture9_Release(temp);
slouken@1903
   594
        D3D_SetError("LockRect()", result);
slouken@1903
   595
        return -1;
slouken@1895
   596
    }
slouken@1903
   597
slouken@1903
   598
    src = pixels;
slouken@1903
   599
    dst = locked.pBits;
slouken@1903
   600
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1903
   601
    for (row = 0; row < rect->h; ++row) {
slouken@1903
   602
        SDL_memcpy(dst, src, length);
slouken@1903
   603
        src += pitch;
slouken@1903
   604
        dst += locked.Pitch;
slouken@1903
   605
    }
slouken@1903
   606
    IDirect3DTexture9_UnlockRect(temp, 0);
slouken@1903
   607
slouken@1903
   608
    result =
slouken@1903
   609
        IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@1903
   610
                                       (IDirect3DBaseTexture9 *) temp,
slouken@2735
   611
                                       (IDirect3DBaseTexture9 *)
slouken@2735
   612
                                       data->texture);
slouken@1903
   613
    IDirect3DTexture9_Release(temp);
slouken@1903
   614
    if (FAILED(result)) {
slouken@1903
   615
        D3D_SetError("UpdateTexture()", result);
slouken@1903
   616
        return -1;
slouken@1903
   617
    }
slouken@1903
   618
    return 0;
slouken@1895
   619
}
slouken@2783
   620
#else
slouken@2783
   621
static int
slouken@2783
   622
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2783
   623
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@2783
   624
{
slouken@2783
   625
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@2783
   626
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@2783
   627
    RECT d3drect;
slouken@2783
   628
    D3DLOCKED_RECT locked;
slouken@2783
   629
    const Uint8 *src;
slouken@2783
   630
    Uint8 *dst;
slouken@2783
   631
    int row, length;
slouken@2783
   632
    HRESULT result;
slouken@2783
   633
slouken@2783
   634
    d3drect.left = rect->x;
slouken@2783
   635
    d3drect.right = rect->x + rect->w;
slouken@2783
   636
    d3drect.top = rect->y;
slouken@2783
   637
    d3drect.bottom = rect->y + rect->h;
slouken@2783
   638
slouken@2786
   639
    result =
slouken@2786
   640
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@2783
   641
    if (FAILED(result)) {
slouken@2783
   642
        D3D_SetError("LockRect()", result);
slouken@2783
   643
        return -1;
slouken@2783
   644
    }
slouken@2783
   645
slouken@2783
   646
    src = pixels;
slouken@2783
   647
    dst = locked.pBits;
slouken@2783
   648
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2783
   649
    for (row = 0; row < rect->h; ++row) {
slouken@2783
   650
        SDL_memcpy(dst, src, length);
slouken@2783
   651
        src += pitch;
slouken@2783
   652
        dst += locked.Pitch;
slouken@2783
   653
    }
slouken@2783
   654
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2783
   655
    return 0;
slouken@2783
   656
}
slouken@2783
   657
#endif // SDL_MEMORY_POOL_DEFAULT
slouken@1895
   658
slouken@1895
   659
static int
slouken@1913
   660
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   661
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   662
                int *pitch)
slouken@1895
   663
{
slouken@1913
   664
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   665
    RECT d3drect;
slouken@1903
   666
    D3DLOCKED_RECT locked;
slouken@1903
   667
    HRESULT result;
slouken@1895
   668
slouken@1903
   669
    d3drect.left = rect->x;
slouken@1903
   670
    d3drect.right = rect->x + rect->w;
slouken@1903
   671
    d3drect.top = rect->y;
slouken@1903
   672
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   673
slouken@1903
   674
    result =
slouken@1903
   675
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@1903
   676
                                   markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
slouken@1903
   677
    if (FAILED(result)) {
slouken@1903
   678
        D3D_SetError("LockRect()", result);
slouken@1903
   679
        return -1;
slouken@1903
   680
    }
slouken@1903
   681
    *pixels = locked.pBits;
slouken@1903
   682
    *pitch = locked.Pitch;
slouken@1903
   683
    return 0;
slouken@1895
   684
}
slouken@1895
   685
slouken@1895
   686
static void
slouken@1913
   687
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   688
{
slouken@1913
   689
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   690
slouken@1903
   691
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   692
}
slouken@1895
   693
slouken@1895
   694
static void
slouken@1913
   695
D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   696
                 const SDL_Rect * rects)
slouken@1895
   697
{
slouken@1913
   698
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   699
    RECT d3drect;
slouken@1903
   700
    int i;
slouken@1903
   701
slouken@1903
   702
    for (i = 0; i < numrects; ++i) {
slouken@1903
   703
        const SDL_Rect *rect = &rects[i];
slouken@1903
   704
slouken@1903
   705
        d3drect.left = rect->x;
slouken@1903
   706
        d3drect.right = rect->x + rect->w;
slouken@1903
   707
        d3drect.top = rect->y;
slouken@1903
   708
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   709
slouken@1903
   710
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   711
    }
slouken@1895
   712
}
slouken@1895
   713
slouken@2932
   714
static void
slouken@2933
   715
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   716
{
slouken@2932
   717
    switch (blendMode) {
slouken@2932
   718
    case SDL_BLENDMODE_NONE:
slouken@2932
   719
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   720
                                        FALSE);
slouken@2932
   721
        break;
slouken@2932
   722
    case SDL_BLENDMODE_MASK:
slouken@2932
   723
    case SDL_BLENDMODE_BLEND:
slouken@2932
   724
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   725
                                        TRUE);
slouken@2932
   726
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   727
                                        D3DBLEND_SRCALPHA);
slouken@2932
   728
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   729
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   730
        break;
slouken@2932
   731
    case SDL_BLENDMODE_ADD:
slouken@2932
   732
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   733
                                        TRUE);
slouken@2932
   734
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   735
                                        D3DBLEND_SRCALPHA);
slouken@2932
   736
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   737
                                        D3DBLEND_ONE);
slouken@2932
   738
        break;
slouken@2932
   739
    case SDL_BLENDMODE_MOD:
slouken@2932
   740
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   741
                                        TRUE);
slouken@2932
   742
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   743
                                        D3DBLEND_ZERO);
slouken@2932
   744
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   745
                                        D3DBLEND_SRCCOLOR);
slouken@2932
   746
        break;
slouken@2932
   747
    }
slouken@2932
   748
}
slouken@2932
   749
slouken@1895
   750
static int
slouken@2932
   751
D3D_RenderPoint(SDL_Renderer * renderer, int x, int y)
slouken@1895
   752
{
slouken@1913
   753
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   754
    DWORD color;
slouken@2932
   755
    Vertex vertices[1];
slouken@1900
   756
    HRESULT result;
slouken@1895
   757
slouken@1900
   758
    if (data->beginScene) {
slouken@1900
   759
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   760
        data->beginScene = SDL_FALSE;
slouken@1900
   761
    }
slouken@1895
   762
slouken@2932
   763
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@1901
   764
slouken@2932
   765
    vertices[0].x = (float) x - 0.5f;
slouken@2932
   766
    vertices[0].y = (float) y - 0.5f;
slouken@2932
   767
    vertices[0].z = 0.0f;
slouken@2932
   768
    vertices[0].rhw = 1.0f;
slouken@2932
   769
    vertices[0].color = color;
slouken@2932
   770
    vertices[0].u = 0.0f;
slouken@2932
   771
    vertices[0].v = 0.0f;
slouken@2932
   772
slouken@2932
   773
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   774
slouken@2933
   775
    result =
slouken@2933
   776
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   777
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   778
    if (FAILED(result)) {
slouken@2932
   779
        D3D_SetError("SetTexture()", result);
slouken@2932
   780
        return -1;
slouken@2932
   781
    }
slouken@1903
   782
    result =
slouken@2932
   783
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1,
slouken@2932
   784
                                         vertices, sizeof(*vertices));
slouken@1900
   785
    if (FAILED(result)) {
slouken@2932
   786
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   787
        return -1;
slouken@2932
   788
    }
slouken@2932
   789
    return 0;
slouken@2932
   790
}
slouken@2932
   791
slouken@2932
   792
static int
slouken@2932
   793
D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@2932
   794
{
slouken@2932
   795
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   796
    DWORD color;
slouken@2932
   797
    Vertex vertices[2];
slouken@2932
   798
    HRESULT result;
slouken@2932
   799
slouken@2932
   800
    if (data->beginScene) {
slouken@2932
   801
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   802
        data->beginScene = SDL_FALSE;
slouken@2932
   803
    }
slouken@2932
   804
slouken@2932
   805
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@2932
   806
slouken@2932
   807
    vertices[0].x = (float) x1 - 0.5f;
slouken@2932
   808
    vertices[0].y = (float) y1 - 0.5f;
slouken@2932
   809
    vertices[0].z = 0.0f;
slouken@2932
   810
    vertices[0].rhw = 1.0f;
slouken@2932
   811
    vertices[0].color = color;
slouken@2932
   812
    vertices[0].u = 0.0f;
slouken@2932
   813
    vertices[0].v = 0.0f;
slouken@2932
   814
slouken@2932
   815
    vertices[1].x = (float) x2 - 0.5f;
slouken@2932
   816
    vertices[1].y = (float) y2 - 0.5f;
slouken@2932
   817
    vertices[1].z = 0.0f;
slouken@2932
   818
    vertices[1].rhw = 1.0f;
slouken@2932
   819
    vertices[1].color = color;
slouken@2932
   820
    vertices[1].u = 0.0f;
slouken@2932
   821
    vertices[1].v = 0.0f;
slouken@2932
   822
slouken@2932
   823
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   824
slouken@2933
   825
    result =
slouken@2933
   826
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   827
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   828
    if (FAILED(result)) {
slouken@2932
   829
        D3D_SetError("SetTexture()", result);
slouken@2932
   830
        return -1;
slouken@2932
   831
    }
slouken@2932
   832
    result =
slouken@2932
   833
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINELIST, 1,
slouken@2932
   834
                                         vertices, sizeof(*vertices));
slouken@2932
   835
    if (FAILED(result)) {
slouken@2932
   836
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   837
        return -1;
slouken@2932
   838
    }
slouken@2932
   839
    return 0;
slouken@2932
   840
}
slouken@2932
   841
slouken@2932
   842
static int
slouken@2932
   843
D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@2932
   844
{
slouken@2932
   845
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   846
    float minx, miny, maxx, maxy;
slouken@2932
   847
    DWORD color;
slouken@2932
   848
    Vertex vertices[4];
slouken@2932
   849
    HRESULT result;
slouken@2932
   850
slouken@2932
   851
    if (data->beginScene) {
slouken@2932
   852
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   853
        data->beginScene = SDL_FALSE;
slouken@2932
   854
    }
slouken@2932
   855
slouken@2932
   856
    minx = (float) rect->x - 0.5f;
slouken@2932
   857
    miny = (float) rect->y - 0.5f;
slouken@2932
   858
    maxx = (float) rect->x + rect->w - 0.5f;
slouken@2932
   859
    maxy = (float) rect->y + rect->h - 0.5f;
slouken@2932
   860
slouken@2932
   861
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@2932
   862
slouken@2932
   863
    vertices[0].x = minx;
slouken@2932
   864
    vertices[0].y = miny;
slouken@2932
   865
    vertices[0].z = 0.0f;
slouken@2932
   866
    vertices[0].rhw = 1.0f;
slouken@2932
   867
    vertices[0].color = color;
slouken@2932
   868
    vertices[0].u = 0.0f;
slouken@2932
   869
    vertices[0].v = 0.0f;
slouken@2932
   870
slouken@2932
   871
    vertices[1].x = maxx;
slouken@2932
   872
    vertices[1].y = miny;
slouken@2932
   873
    vertices[1].z = 0.0f;
slouken@2932
   874
    vertices[1].rhw = 1.0f;
slouken@2932
   875
    vertices[1].color = color;
slouken@2932
   876
    vertices[1].u = 0.0f;
slouken@2932
   877
    vertices[1].v = 0.0f;
slouken@2932
   878
slouken@2932
   879
    vertices[2].x = maxx;
slouken@2932
   880
    vertices[2].y = maxy;
slouken@2932
   881
    vertices[2].z = 0.0f;
slouken@2932
   882
    vertices[2].rhw = 1.0f;
slouken@2932
   883
    vertices[2].color = color;
slouken@2932
   884
    vertices[2].u = 0.0f;
slouken@2932
   885
    vertices[2].v = 0.0f;
slouken@2932
   886
slouken@2932
   887
    vertices[3].x = minx;
slouken@2932
   888
    vertices[3].y = maxy;
slouken@2932
   889
    vertices[3].z = 0.0f;
slouken@2932
   890
    vertices[3].rhw = 1.0f;
slouken@2932
   891
    vertices[3].color = color;
slouken@2932
   892
    vertices[3].u = 0.0f;
slouken@2932
   893
    vertices[3].v = 0.0f;
slouken@2932
   894
slouken@2932
   895
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   896
slouken@2933
   897
    result =
slouken@2933
   898
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   899
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   900
    if (FAILED(result)) {
slouken@2932
   901
        D3D_SetError("SetTexture()", result);
slouken@2932
   902
        return -1;
slouken@2932
   903
    }
slouken@2932
   904
    result =
slouken@2932
   905
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@2932
   906
                                         vertices, sizeof(*vertices));
slouken@2932
   907
    if (FAILED(result)) {
slouken@2932
   908
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1900
   909
        return -1;
slouken@1900
   910
    }
slouken@1895
   911
    return 0;
slouken@1895
   912
}
slouken@1895
   913
slouken@1895
   914
static int
slouken@1913
   915
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   916
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   917
{
slouken@1913
   918
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   919
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@1903
   920
    float minx, miny, maxx, maxy;
slouken@1904
   921
    float minu, maxu, minv, maxv;
slouken@1987
   922
    DWORD color;
slouken@1903
   923
    Vertex vertices[4];
slouken@1903
   924
    HRESULT result;
slouken@1895
   925
slouken@1900
   926
    if (data->beginScene) {
slouken@1900
   927
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   928
        data->beginScene = SDL_FALSE;
slouken@1900
   929
    }
slouken@1903
   930
slouken@1904
   931
    minx = (float) dstrect->x - 0.5f;
slouken@1904
   932
    miny = (float) dstrect->y - 0.5f;
slouken@1904
   933
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@1904
   934
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
   935
slouken@1904
   936
    minu = (float) srcrect->x / texture->w;
slouken@1904
   937
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
   938
    minv = (float) srcrect->y / texture->h;
slouken@1904
   939
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
   940
slouken@1987
   941
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
   942
slouken@1903
   943
    vertices[0].x = minx;
slouken@1903
   944
    vertices[0].y = miny;
slouken@1903
   945
    vertices[0].z = 0.0f;
slouken@1904
   946
    vertices[0].rhw = 1.0f;
slouken@1987
   947
    vertices[0].color = color;
slouken@1904
   948
    vertices[0].u = minu;
slouken@1904
   949
    vertices[0].v = minv;
slouken@1904
   950
slouken@1903
   951
    vertices[1].x = maxx;
slouken@1903
   952
    vertices[1].y = miny;
slouken@1903
   953
    vertices[1].z = 0.0f;
slouken@1904
   954
    vertices[1].rhw = 1.0f;
slouken@1987
   955
    vertices[1].color = color;
slouken@1904
   956
    vertices[1].u = maxu;
slouken@1904
   957
    vertices[1].v = minv;
slouken@1904
   958
slouken@1903
   959
    vertices[2].x = maxx;
slouken@1903
   960
    vertices[2].y = maxy;
slouken@1903
   961
    vertices[2].z = 0.0f;
slouken@1904
   962
    vertices[2].rhw = 1.0f;
slouken@1987
   963
    vertices[2].color = color;
slouken@1904
   964
    vertices[2].u = maxu;
slouken@1904
   965
    vertices[2].v = maxv;
slouken@1904
   966
slouken@1903
   967
    vertices[3].x = minx;
slouken@1903
   968
    vertices[3].y = maxy;
slouken@1903
   969
    vertices[3].z = 0.0f;
slouken@1904
   970
    vertices[3].rhw = 1.0f;
slouken@1987
   971
    vertices[3].color = color;
slouken@1904
   972
    vertices[3].u = minu;
slouken@1904
   973
    vertices[3].v = maxv;
slouken@1903
   974
slouken@2932
   975
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
   976
slouken@1985
   977
    switch (texture->scaleMode) {
slouken@1965
   978
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
   979
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1917
   980
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   981
                                         D3DTEXF_POINT);
slouken@1917
   982
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   983
                                         D3DTEXF_POINT);
slouken@1917
   984
        break;
slouken@1965
   985
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1917
   986
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   987
                                         D3DTEXF_LINEAR);
slouken@1917
   988
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   989
                                         D3DTEXF_LINEAR);
slouken@1917
   990
        break;
slouken@1965
   991
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1917
   992
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   993
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
   994
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   995
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
   996
        break;
slouken@1917
   997
    }
slouken@1917
   998
slouken@1903
   999
    result =
slouken@2735
  1000
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1001
                                    texturedata->texture);
slouken@1903
  1002
    if (FAILED(result)) {
slouken@1903
  1003
        D3D_SetError("SetTexture()", result);
slouken@1903
  1004
        return -1;
slouken@1903
  1005
    }
slouken@1903
  1006
    result =
slouken@1903
  1007
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1008
                                         vertices, sizeof(*vertices));
slouken@1903
  1009
    if (FAILED(result)) {
slouken@1903
  1010
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1011
        return -1;
slouken@1903
  1012
    }
slouken@1895
  1013
    return 0;
slouken@1895
  1014
}
slouken@1895
  1015
slouken@1895
  1016
static void
slouken@1913
  1017
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1018
{
slouken@1913
  1019
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1020
    HRESULT result;
slouken@1900
  1021
slouken@1900
  1022
    if (!data->beginScene) {
slouken@1900
  1023
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1024
        data->beginScene = SDL_TRUE;
slouken@1900
  1025
    }
slouken@1900
  1026
slouken@1975
  1027
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1028
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1029
        /* We'll reset later */
slouken@1975
  1030
        return;
slouken@1975
  1031
    }
slouken@1975
  1032
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1033
        D3D_Reset(renderer);
slouken@1975
  1034
    }
slouken@1900
  1035
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1036
    if (FAILED(result)) {
slouken@1900
  1037
        D3D_SetError("Present()", result);
slouken@1900
  1038
    }
slouken@1895
  1039
}
slouken@1895
  1040
slouken@1895
  1041
static void
slouken@1913
  1042
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1043
{
slouken@1913
  1044
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1045
slouken@1895
  1046
    if (!data) {
slouken@1895
  1047
        return;
slouken@1895
  1048
    }
slouken@1903
  1049
    if (data->texture) {
slouken@1903
  1050
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1051
    }
slouken@1895
  1052
    SDL_free(data);
slouken@1895
  1053
    texture->driverdata = NULL;
slouken@1895
  1054
}
slouken@1895
  1055
slouken@1975
  1056
static void
slouken@1913
  1057
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1058
{
slouken@1913
  1059
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1060
slouken@1895
  1061
    if (data) {
slouken@1900
  1062
        if (data->device) {
slouken@1900
  1063
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1064
        }
slouken@1895
  1065
        SDL_free(data);
slouken@1895
  1066
    }
slouken@1895
  1067
    SDL_free(renderer);
slouken@1895
  1068
}
slouken@1895
  1069
slouken@1895
  1070
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
  1071
slouken@1895
  1072
/* vi: set ts=4 sw=4 expandtab: */