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