src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 11 Aug 2007 20:54:31 +0000
changeset 2222 926294b2bb4e
parent 1991 3863ba81c1d6
child 2698 e1da92da346c
permissions -rw-r--r--
Emphasized the separation between SDL_Surface and SDL_Texture
- SDL_Surface is a system memory representation of pixel data
- SDL_Texture is a video memory representation of pixel data

The concept of SDL_Surface with SDL_HWSURFACE is no longer used.

Separated SDL_Texture types by usage rather than memory type
- SDL_TEXTUREACCESS_STATIC is for rarely changed pixel data,
can be placed in video memory.
- SDL_TEXTUREACCESS_STREAMING is for frequently changing pixel
data, usually placed in system memory or AGP memory.

Optimized the SDL_compat usage of the OpenGL renderer by only
using one copy of the framebuffer instead of two.
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@1991
   403
    /* Disable second texture stage, since we're done */
slouken@1991
   404
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   405
                                          D3DTOP_DISABLE);
slouken@1991
   406
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   407
                                          D3DTOP_DISABLE);
slouken@1903
   408
slouken@1895
   409
    return renderer;
slouken@1895
   410
}
slouken@1895
   411
slouken@1895
   412
static int
slouken@1975
   413
D3D_Reset(SDL_Renderer * renderer)
slouken@1975
   414
{
slouken@1975
   415
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   416
    HRESULT result;
slouken@1975
   417
slouken@1975
   418
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@1975
   419
    if (FAILED(result)) {
slouken@1975
   420
        if (result == D3DERR_DEVICELOST) {
slouken@1975
   421
            /* Don't worry about it, we'll reset later... */
slouken@1975
   422
            return 0;
slouken@1975
   423
        } else {
slouken@1975
   424
            D3D_SetError("Reset()", result);
slouken@1975
   425
            return -1;
slouken@1975
   426
        }
slouken@1975
   427
    }
slouken@1975
   428
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   429
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   430
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1975
   431
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1975
   432
                                    D3DCULL_NONE);
slouken@1975
   433
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1975
   434
    return 0;
slouken@1975
   435
}
slouken@1975
   436
slouken@1975
   437
static int
slouken@1975
   438
D3D_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1975
   439
{
slouken@1975
   440
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   441
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1975
   442
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1975
   443
slouken@1975
   444
    data->pparams.BackBufferWidth = window->w;
slouken@1975
   445
    data->pparams.BackBufferHeight = window->h;
slouken@1975
   446
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1975
   447
        data->pparams.BackBufferFormat =
slouken@1975
   448
            PixelFormatToD3DFMT(display->fullscreen_mode.format);
slouken@1975
   449
    } else {
slouken@1975
   450
        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1975
   451
    }
slouken@1975
   452
    return D3D_Reset(renderer);
slouken@1975
   453
}
slouken@1975
   454
slouken@1975
   455
static int
slouken@1913
   456
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   457
{
slouken@1913
   458
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   459
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   460
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1913
   461
    D3D_TextureData *data;
slouken@1903
   462
    HRESULT result;
slouken@1895
   463
slouken@1920
   464
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   465
    if (!data) {
slouken@1895
   466
        SDL_OutOfMemory();
slouken@1895
   467
        return -1;
slouken@1895
   468
    }
slouken@1895
   469
slouken@1895
   470
    texture->driverdata = data;
slouken@1895
   471
slouken@1903
   472
    result =
slouken@1903
   473
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   474
                                       texture->h, 1, 0,
slouken@1903
   475
                                       PixelFormatToD3DFMT(texture->format),
slouken@2222
   476
                                       D3DPOOL_MANAGED, &data->texture, NULL);
slouken@1903
   477
    if (FAILED(result)) {
slouken@1903
   478
        D3D_SetError("CreateTexture()", result);
slouken@1903
   479
        return -1;
slouken@1903
   480
    }
slouken@1895
   481
slouken@1903
   482
    return 0;
slouken@1895
   483
}
slouken@1895
   484
slouken@1895
   485
static int
slouken@1913
   486
D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   487
                      const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   488
{
slouken@1913
   489
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   490
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   491
slouken@1903
   492
    return 0;
slouken@1895
   493
}
slouken@1895
   494
slouken@1895
   495
static int
slouken@1913
   496
D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   497
                      SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   498
{
slouken@1913
   499
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   500
slouken@1903
   501
    return 0;
slouken@1895
   502
}
slouken@1895
   503
slouken@1895
   504
static int
slouken@1985
   505
D3D_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   506
{
slouken@1987
   507
    return 0;
slouken@1985
   508
}
slouken@1985
   509
slouken@1985
   510
static int
slouken@1985
   511
D3D_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   512
{
slouken@1987
   513
    return 0;
slouken@1985
   514
}
slouken@1985
   515
slouken@1985
   516
static int
slouken@1985
   517
D3D_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   518
{
slouken@1985
   519
    switch (texture->blendMode) {
slouken@1985
   520
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@1985
   521
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@1985
   522
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1985
   523
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@1985
   524
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@1985
   525
        return 0;
slouken@1985
   526
    default:
slouken@1985
   527
        SDL_Unsupported();
slouken@1985
   528
        texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
slouken@1985
   529
        return -1;
slouken@1985
   530
    }
slouken@1985
   531
}
slouken@1985
   532
slouken@1985
   533
static int
slouken@1985
   534
D3D_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   535
{
slouken@1985
   536
    switch (texture->scaleMode) {
slouken@1985
   537
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   538
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   539
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   540
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   541
        return 0;
slouken@1985
   542
    default:
slouken@1985
   543
        SDL_Unsupported();
slouken@1985
   544
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   545
        return -1;
slouken@1985
   546
    }
slouken@1985
   547
    return 0;
slouken@1985
   548
}
slouken@1985
   549
slouken@1985
   550
static int
slouken@1913
   551
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   552
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   553
{
slouken@1913
   554
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   555
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1903
   556
    IDirect3DTexture9 *temp;
slouken@1903
   557
    RECT d3drect;
slouken@1903
   558
    D3DLOCKED_RECT locked;
slouken@1903
   559
    const Uint8 *src;
slouken@1903
   560
    Uint8 *dst;
slouken@1903
   561
    int row, length;
slouken@1903
   562
    HRESULT result;
slouken@1895
   563
slouken@1903
   564
    result =
slouken@1903
   565
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   566
                                       texture->h, 1, 0,
slouken@1903
   567
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   568
                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@1903
   569
    if (FAILED(result)) {
slouken@1903
   570
        D3D_SetError("CreateTexture()", result);
slouken@1903
   571
        return -1;
slouken@1903
   572
    }
slouken@1903
   573
slouken@1903
   574
    d3drect.left = rect->x;
slouken@1903
   575
    d3drect.right = rect->x + rect->w;
slouken@1903
   576
    d3drect.top = rect->y;
slouken@1903
   577
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   578
slouken@1903
   579
    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@1903
   580
    if (FAILED(result)) {
slouken@1903
   581
        IDirect3DTexture9_Release(temp);
slouken@1903
   582
        D3D_SetError("LockRect()", result);
slouken@1903
   583
        return -1;
slouken@1903
   584
    }
slouken@1895
   585
slouken@1903
   586
    src = pixels;
slouken@1903
   587
    dst = locked.pBits;
slouken@1903
   588
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1903
   589
    for (row = 0; row < rect->h; ++row) {
slouken@1903
   590
        SDL_memcpy(dst, src, length);
slouken@1903
   591
        src += pitch;
slouken@1903
   592
        dst += locked.Pitch;
slouken@1895
   593
    }
slouken@1903
   594
    IDirect3DTexture9_UnlockRect(temp, 0);
slouken@1903
   595
slouken@1903
   596
    result =
slouken@1903
   597
        IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@1903
   598
                                       (IDirect3DBaseTexture9 *) temp,
slouken@1903
   599
                                       (IDirect3DBaseTexture9 *) data->
slouken@1903
   600
                                       texture);
slouken@1903
   601
    IDirect3DTexture9_Release(temp);
slouken@1903
   602
    if (FAILED(result)) {
slouken@1903
   603
        D3D_SetError("UpdateTexture()", result);
slouken@1903
   604
        return -1;
slouken@1903
   605
    }
slouken@1903
   606
    return 0;
slouken@1895
   607
}
slouken@1895
   608
slouken@1895
   609
static int
slouken@1913
   610
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   611
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   612
                int *pitch)
slouken@1895
   613
{
slouken@1913
   614
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   615
    RECT d3drect;
slouken@1903
   616
    D3DLOCKED_RECT locked;
slouken@1903
   617
    HRESULT result;
slouken@1895
   618
slouken@1903
   619
    d3drect.left = rect->x;
slouken@1903
   620
    d3drect.right = rect->x + rect->w;
slouken@1903
   621
    d3drect.top = rect->y;
slouken@1903
   622
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   623
slouken@1903
   624
    result =
slouken@1903
   625
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@1903
   626
                                   markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
slouken@1903
   627
    if (FAILED(result)) {
slouken@1903
   628
        D3D_SetError("LockRect()", result);
slouken@1903
   629
        return -1;
slouken@1903
   630
    }
slouken@1903
   631
    *pixels = locked.pBits;
slouken@1903
   632
    *pitch = locked.Pitch;
slouken@1903
   633
    return 0;
slouken@1895
   634
}
slouken@1895
   635
slouken@1895
   636
static void
slouken@1913
   637
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   638
{
slouken@1913
   639
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   640
slouken@1903
   641
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   642
}
slouken@1895
   643
slouken@1895
   644
static void
slouken@1913
   645
D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   646
                 const SDL_Rect * rects)
slouken@1895
   647
{
slouken@1913
   648
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   649
    RECT d3drect;
slouken@1903
   650
    int i;
slouken@1903
   651
slouken@1903
   652
    for (i = 0; i < numrects; ++i) {
slouken@1903
   653
        const SDL_Rect *rect = &rects[i];
slouken@1903
   654
slouken@1903
   655
        d3drect.left = rect->x;
slouken@1903
   656
        d3drect.right = rect->x + rect->w;
slouken@1903
   657
        d3drect.top = rect->y;
slouken@1903
   658
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   659
slouken@1903
   660
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   661
    }
slouken@1895
   662
}
slouken@1895
   663
slouken@1895
   664
static int
slouken@1985
   665
D3D_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a,
slouken@1985
   666
               const SDL_Rect * rect)
slouken@1895
   667
{
slouken@1913
   668
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1901
   669
    D3DRECT d3drect;
slouken@1900
   670
    HRESULT result;
slouken@1900
   671
slouken@1900
   672
    if (data->beginScene) {
slouken@1900
   673
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   674
        data->beginScene = SDL_FALSE;
slouken@1900
   675
    }
slouken@1895
   676
slouken@1901
   677
    d3drect.x1 = rect->x;
slouken@1903
   678
    d3drect.x2 = rect->x + rect->w;
slouken@1901
   679
    d3drect.y1 = rect->y;
slouken@1903
   680
    d3drect.y2 = rect->y + rect->h;
slouken@1901
   681
slouken@1903
   682
    result =
slouken@1903
   683
        IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
slouken@1985
   684
                               D3DCOLOR_ARGB(a, r, g, b), 1.0f, 0);
slouken@1900
   685
    if (FAILED(result)) {
slouken@1900
   686
        D3D_SetError("Clear()", result);
slouken@1900
   687
        return -1;
slouken@1900
   688
    }
slouken@1895
   689
    return 0;
slouken@1895
   690
}
slouken@1895
   691
slouken@1895
   692
static int
slouken@1913
   693
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   694
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   695
{
slouken@1913
   696
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   697
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@1903
   698
    float minx, miny, maxx, maxy;
slouken@1904
   699
    float minu, maxu, minv, maxv;
slouken@1987
   700
    DWORD color;
slouken@1903
   701
    Vertex vertices[4];
slouken@1903
   702
    HRESULT result;
slouken@1895
   703
slouken@1900
   704
    if (data->beginScene) {
slouken@1900
   705
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   706
        data->beginScene = SDL_FALSE;
slouken@1900
   707
    }
slouken@1903
   708
slouken@1904
   709
    minx = (float) dstrect->x - 0.5f;
slouken@1904
   710
    miny = (float) dstrect->y - 0.5f;
slouken@1904
   711
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@1904
   712
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
   713
slouken@1904
   714
    minu = (float) srcrect->x / texture->w;
slouken@1904
   715
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
   716
    minv = (float) srcrect->y / texture->h;
slouken@1904
   717
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
   718
slouken@1987
   719
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
   720
slouken@1903
   721
    vertices[0].x = minx;
slouken@1903
   722
    vertices[0].y = miny;
slouken@1903
   723
    vertices[0].z = 0.0f;
slouken@1904
   724
    vertices[0].rhw = 1.0f;
slouken@1987
   725
    vertices[0].color = color;
slouken@1904
   726
    vertices[0].u = minu;
slouken@1904
   727
    vertices[0].v = minv;
slouken@1904
   728
slouken@1903
   729
    vertices[1].x = maxx;
slouken@1903
   730
    vertices[1].y = miny;
slouken@1903
   731
    vertices[1].z = 0.0f;
slouken@1904
   732
    vertices[1].rhw = 1.0f;
slouken@1987
   733
    vertices[1].color = color;
slouken@1904
   734
    vertices[1].u = maxu;
slouken@1904
   735
    vertices[1].v = minv;
slouken@1904
   736
slouken@1903
   737
    vertices[2].x = maxx;
slouken@1903
   738
    vertices[2].y = maxy;
slouken@1903
   739
    vertices[2].z = 0.0f;
slouken@1904
   740
    vertices[2].rhw = 1.0f;
slouken@1987
   741
    vertices[2].color = color;
slouken@1904
   742
    vertices[2].u = maxu;
slouken@1904
   743
    vertices[2].v = maxv;
slouken@1904
   744
slouken@1903
   745
    vertices[3].x = minx;
slouken@1903
   746
    vertices[3].y = maxy;
slouken@1903
   747
    vertices[3].z = 0.0f;
slouken@1904
   748
    vertices[3].rhw = 1.0f;
slouken@1987
   749
    vertices[3].color = color;
slouken@1904
   750
    vertices[3].u = minu;
slouken@1904
   751
    vertices[3].v = maxv;
slouken@1903
   752
slouken@1985
   753
    switch (texture->blendMode) {
slouken@1965
   754
    case SDL_TEXTUREBLENDMODE_NONE:
slouken@1916
   755
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   756
                                        FALSE);
slouken@1916
   757
        break;
slouken@1965
   758
    case SDL_TEXTUREBLENDMODE_MASK:
slouken@1965
   759
    case SDL_TEXTUREBLENDMODE_BLEND:
slouken@1916
   760
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   761
                                        TRUE);
slouken@1916
   762
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1916
   763
                                        D3DBLEND_SRCALPHA);
slouken@1916
   764
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1916
   765
                                        D3DBLEND_INVSRCALPHA);
slouken@1916
   766
        break;
slouken@1965
   767
    case SDL_TEXTUREBLENDMODE_ADD:
slouken@1916
   768
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   769
                                        TRUE);
slouken@1916
   770
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1916
   771
                                        D3DBLEND_SRCALPHA);
slouken@1916
   772
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1916
   773
                                        D3DBLEND_ONE);
slouken@1916
   774
        break;
slouken@1965
   775
    case SDL_TEXTUREBLENDMODE_MOD:
slouken@1916
   776
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   777
                                        TRUE);
slouken@1916
   778
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1916
   779
                                        D3DBLEND_ZERO);
slouken@1916
   780
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1916
   781
                                        D3DBLEND_SRCCOLOR);
slouken@1916
   782
        break;
slouken@1916
   783
    }
slouken@1916
   784
slouken@1985
   785
    switch (texture->scaleMode) {
slouken@1965
   786
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
   787
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1917
   788
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   789
                                         D3DTEXF_POINT);
slouken@1917
   790
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   791
                                         D3DTEXF_POINT);
slouken@1917
   792
        break;
slouken@1965
   793
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1917
   794
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   795
                                         D3DTEXF_LINEAR);
slouken@1917
   796
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   797
                                         D3DTEXF_LINEAR);
slouken@1917
   798
        break;
slouken@1965
   799
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1917
   800
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   801
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
   802
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   803
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
   804
        break;
slouken@1917
   805
    }
slouken@1917
   806
slouken@1903
   807
    result =
slouken@1903
   808
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@1903
   809
                                    (IDirect3DBaseTexture9 *) texturedata->
slouken@1903
   810
                                    texture);
slouken@1903
   811
    if (FAILED(result)) {
slouken@1903
   812
        D3D_SetError("SetTexture()", result);
slouken@1903
   813
        return -1;
slouken@1903
   814
    }
slouken@1903
   815
    result =
slouken@1903
   816
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
   817
                                         vertices, sizeof(*vertices));
slouken@1903
   818
    if (FAILED(result)) {
slouken@1903
   819
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
   820
        return -1;
slouken@1903
   821
    }
slouken@1895
   822
    return 0;
slouken@1895
   823
}
slouken@1895
   824
slouken@1895
   825
static void
slouken@1913
   826
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   827
{
slouken@1913
   828
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
   829
    HRESULT result;
slouken@1900
   830
slouken@1900
   831
    if (!data->beginScene) {
slouken@1900
   832
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
   833
        data->beginScene = SDL_TRUE;
slouken@1900
   834
    }
slouken@1900
   835
slouken@1975
   836
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
   837
    if (result == D3DERR_DEVICELOST) {
slouken@1975
   838
        /* We'll reset later */
slouken@1975
   839
        return;
slouken@1975
   840
    }
slouken@1975
   841
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
   842
        D3D_Reset(renderer);
slouken@1975
   843
    }
slouken@1900
   844
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
   845
    if (FAILED(result)) {
slouken@1900
   846
        D3D_SetError("Present()", result);
slouken@1900
   847
    }
slouken@1895
   848
}
slouken@1895
   849
slouken@1895
   850
static void
slouken@1913
   851
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   852
{
slouken@1913
   853
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   854
slouken@1895
   855
    if (!data) {
slouken@1895
   856
        return;
slouken@1895
   857
    }
slouken@1903
   858
    if (data->texture) {
slouken@1903
   859
        IDirect3DTexture9_Release(data->texture);
slouken@1903
   860
    }
slouken@1895
   861
    SDL_free(data);
slouken@1895
   862
    texture->driverdata = NULL;
slouken@1895
   863
}
slouken@1895
   864
slouken@1975
   865
static void
slouken@1913
   866
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   867
{
slouken@1913
   868
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   869
slouken@1895
   870
    if (data) {
slouken@1900
   871
        if (data->device) {
slouken@1900
   872
            IDirect3DDevice9_Release(data->device);
slouken@1900
   873
        }
slouken@1895
   874
        SDL_free(data);
slouken@1895
   875
    }
slouken@1895
   876
    SDL_free(renderer);
slouken@1895
   877
}
slouken@1895
   878
slouken@1895
   879
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
   880
slouken@1895
   881
/* vi: set ts=4 sw=4 expandtab: */