src/render/direct3d/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 02 Feb 2011 14:34:54 -0800
changeset 5154 fb424691cfc7
parent 5149 src/video/windows/SDL_d3drender.c@be02be2ea897
child 5156 307ccc9c135e
permissions -rw-r--r--
Moved the rendering code out to a separate directory in the hope that it can someday be completely decoupled from the rest of the library and be expanded to an awesome 2D on 3D library.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@5154
    26
#include "../../core/windows/SDL_windows.h"
slouken@5154
    27
slouken@5154
    28
#include "SDL_loadso.h"
slouken@5154
    29
#include "SDL_syswm.h"
slouken@5154
    30
#include "../SDL_sysrender.h"
slouken@5154
    31
#include "../../video/SDL_yuv_sw_c.h"
slouken@5154
    32
slouken@5154
    33
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    34
#define D3D_DEBUG_INFO
slouken@5154
    35
#include <d3d9.h>
slouken@5154
    36
#endif
slouken@1895
    37
slouken@3556
    38
#ifdef ASSEMBLE_SHADER
slouken@3556
    39
///////////////////////////////////////////////////////////////////////////
slouken@3556
    40
// ID3DXBuffer:
slouken@3556
    41
// ------------
slouken@3556
    42
// The buffer object is used by D3DX to return arbitrary size data.
slouken@3556
    43
//
slouken@3556
    44
// GetBufferPointer -
slouken@3556
    45
//    Returns a pointer to the beginning of the buffer.
slouken@3556
    46
//
slouken@3556
    47
// GetBufferSize -
slouken@3556
    48
//    Returns the size of the buffer, in bytes.
slouken@3556
    49
///////////////////////////////////////////////////////////////////////////
slouken@3556
    50
slouken@3556
    51
typedef interface ID3DXBuffer ID3DXBuffer;
slouken@3556
    52
typedef interface ID3DXBuffer *LPD3DXBUFFER;
slouken@3556
    53
slouken@3556
    54
// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
slouken@3556
    55
DEFINE_GUID(IID_ID3DXBuffer, 
slouken@3556
    56
0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
slouken@3556
    57
slouken@3556
    58
#undef INTERFACE
slouken@3556
    59
#define INTERFACE ID3DXBuffer
slouken@3556
    60
slouken@3556
    61
typedef interface ID3DXBuffer {
slouken@3556
    62
    const struct ID3DXBufferVtbl FAR* lpVtbl;
slouken@3556
    63
} ID3DXBuffer;
slouken@3556
    64
typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
slouken@3556
    65
const struct ID3DXBufferVtbl
slouken@3556
    66
{
slouken@3556
    67
    // IUnknown
slouken@3556
    68
    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
slouken@3556
    69
    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
slouken@3556
    70
    STDMETHOD_(ULONG, Release)(THIS) PURE;
slouken@3556
    71
slouken@3556
    72
    // ID3DXBuffer
slouken@3556
    73
    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
slouken@3556
    74
    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
slouken@3556
    75
};
slouken@3556
    76
slouken@3556
    77
HRESULT WINAPI
slouken@3556
    78
    D3DXAssembleShader(
slouken@3556
    79
        LPCSTR                          pSrcData,
slouken@3556
    80
        UINT                            SrcDataLen,
slouken@3556
    81
        CONST LPVOID*                   pDefines,
slouken@3556
    82
        LPVOID                          pInclude,
slouken@3556
    83
        DWORD                           Flags,
slouken@3556
    84
        LPD3DXBUFFER*                   ppShader,
slouken@3556
    85
        LPD3DXBUFFER*                   ppErrorMsgs);
slouken@3556
    86
slouken@3556
    87
#endif /* ASSEMBLE_SHADER */
slouken@3556
    88
slouken@3556
    89
slouken@1895
    90
/* Direct3D renderer implementation */
slouken@1895
    91
slouken@2786
    92
#if 1                           /* This takes more memory but you won't lose your texture data */
slouken@2932
    93
#define D3DPOOL_SDL    D3DPOOL_MANAGED
slouken@2783
    94
#define SDL_MEMORY_POOL_MANAGED
slouken@2783
    95
#else
slouken@2932
    96
#define D3DPOOL_SDL    D3DPOOL_DEFAULT
slouken@2783
    97
#define SDL_MEMORY_POOL_DEFAULT
slouken@2783
    98
#endif
slouken@2783
    99
slouken@1913
   100
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1913
   101
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@2990
   102
static int D3D_QueryTexturePixels(SDL_Renderer * renderer,
slouken@2990
   103
                                  SDL_Texture * texture, void **pixels,
slouken@2990
   104
                                  int *pitch);
slouken@1913
   105
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   106
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
   107
                             int pitch);
slouken@1913
   108
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   109
                           const SDL_Rect * rect, int markDirty,
slouken@1913
   110
                           void **pixels, int *pitch);
slouken@1913
   111
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
   112
static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   113
                             int numrects, const SDL_Rect * rects);
slouken@3599
   114
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3599
   115
                                const SDL_Point * points, int count);
slouken@3599
   116
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
slouken@3599
   117
                               const SDL_Point * points, int count);
slouken@3599
   118
static int D3D_RenderFillRects(SDL_Renderer * renderer,
slouken@3599
   119
                               const SDL_Rect ** rects, int count);
slouken@1913
   120
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   121
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
   122
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   123
                                Uint32 format, void * pixels, int pitch);
slouken@3480
   124
static int D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   125
                                 Uint32 format, const void * pixels, int pitch);
slouken@1913
   126
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
   127
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
   128
                               SDL_Texture * texture);
slouken@1913
   129
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
   130
slouken@1895
   131
slouken@1913
   132
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
   133
    D3D_CreateRenderer,
slouken@1895
   134
    {
slouken@1895
   135
     "d3d",
slouken@5142
   136
     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@2972
   137
     0,
slouken@2972
   138
     {0},
slouken@1895
   139
     0,
slouken@1895
   140
     0}
slouken@1895
   141
};
slouken@1895
   142
slouken@1895
   143
typedef struct
slouken@1895
   144
{
slouken@5154
   145
    void* d3dDLL;
slouken@2973
   146
    IDirect3D9 *d3d;
slouken@1895
   147
    IDirect3DDevice9 *device;
slouken@3527
   148
    UINT adapter;
slouken@1975
   149
    D3DPRESENT_PARAMETERS pparams;
slouken@1900
   150
    SDL_bool beginScene;
slouken@1913
   151
} D3D_RenderData;
slouken@1895
   152
slouken@1895
   153
typedef struct
slouken@1895
   154
{
slouken@2973
   155
    SDL_SW_YUVTexture *yuv;
slouken@2973
   156
    Uint32 format;
slouken@1903
   157
    IDirect3DTexture9 *texture;
slouken@1913
   158
} D3D_TextureData;
slouken@1895
   159
slouken@1903
   160
typedef struct
slouken@1903
   161
{
slouken@1903
   162
    float x, y, z;
slouken@1904
   163
    float rhw;
slouken@1987
   164
    DWORD color;
slouken@1904
   165
    float u, v;
slouken@1903
   166
} Vertex;
slouken@1903
   167
slouken@1895
   168
static void
slouken@1900
   169
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   170
{
slouken@1900
   171
    const char *error;
slouken@1900
   172
slouken@1900
   173
    switch (result) {
slouken@1900
   174
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   175
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   176
        break;
slouken@1900
   177
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   178
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   179
        break;
slouken@1900
   180
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   181
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   182
        break;
slouken@1900
   183
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   184
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   185
        break;
slouken@1900
   186
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   187
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   188
        break;
slouken@1900
   189
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   190
        error = "TOOMANYOPERATIONS";
slouken@1900
   191
        break;
slouken@1900
   192
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   193
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   194
        break;
slouken@1900
   195
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   196
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   197
        break;
slouken@1900
   198
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   199
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   200
        break;
slouken@1900
   201
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   202
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   203
        break;
slouken@1900
   204
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   205
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   206
        break;
slouken@1900
   207
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   208
        error = "DRIVERINTERNALERROR";
slouken@1900
   209
        break;
slouken@1900
   210
    case D3DERR_NOTFOUND:
slouken@1900
   211
        error = "NOTFOUND";
slouken@1900
   212
        break;
slouken@1900
   213
    case D3DERR_MOREDATA:
slouken@1900
   214
        error = "MOREDATA";
slouken@1900
   215
        break;
slouken@1900
   216
    case D3DERR_DEVICELOST:
slouken@1900
   217
        error = "DEVICELOST";
slouken@1900
   218
        break;
slouken@1900
   219
    case D3DERR_DEVICENOTRESET:
slouken@1900
   220
        error = "DEVICENOTRESET";
slouken@1900
   221
        break;
slouken@1900
   222
    case D3DERR_NOTAVAILABLE:
slouken@1900
   223
        error = "NOTAVAILABLE";
slouken@1900
   224
        break;
slouken@1900
   225
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   226
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   227
        break;
slouken@1900
   228
    case D3DERR_INVALIDDEVICE:
slouken@1900
   229
        error = "INVALIDDEVICE";
slouken@1900
   230
        break;
slouken@1900
   231
    case D3DERR_INVALIDCALL:
slouken@1900
   232
        error = "INVALIDCALL";
slouken@1900
   233
        break;
slouken@1900
   234
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   235
        error = "DRIVERINVALIDCALL";
slouken@1900
   236
        break;
slouken@1900
   237
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   238
        error = "WASSTILLDRAWING";
slouken@1900
   239
        break;
slouken@1900
   240
    default:
slouken@1900
   241
        error = "UNKNOWN";
slouken@1900
   242
        break;
slouken@1900
   243
    }
slouken@1900
   244
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   245
}
slouken@1900
   246
slouken@1903
   247
static D3DFORMAT
slouken@1903
   248
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   249
{
slouken@1903
   250
    switch (format) {
slouken@1965
   251
    case SDL_PIXELFORMAT_INDEX8:
slouken@1903
   252
        return D3DFMT_P8;
slouken@1965
   253
    case SDL_PIXELFORMAT_RGB332:
slouken@1903
   254
        return D3DFMT_R3G3B2;
slouken@1965
   255
    case SDL_PIXELFORMAT_RGB444:
slouken@1903
   256
        return D3DFMT_X4R4G4B4;
slouken@1965
   257
    case SDL_PIXELFORMAT_RGB555:
slouken@1903
   258
        return D3DFMT_X1R5G5B5;
slouken@1965
   259
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1903
   260
        return D3DFMT_A4R4G4B4;
slouken@1965
   261
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1903
   262
        return D3DFMT_A1R5G5B5;
slouken@1965
   263
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   264
        return D3DFMT_R5G6B5;
slouken@1965
   265
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   266
        return D3DFMT_X8R8G8B8;
slouken@1965
   267
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   268
        return D3DFMT_A8R8G8B8;
slouken@1965
   269
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1903
   270
        return D3DFMT_A2R10G10B10;
slouken@3495
   271
    case SDL_PIXELFORMAT_YV12:
slouken@3495
   272
        return MAKEFOURCC('Y','V','1','2');
slouken@3495
   273
    case SDL_PIXELFORMAT_IYUV:
slouken@3495
   274
        return MAKEFOURCC('I','4','2','0');
slouken@1965
   275
    case SDL_PIXELFORMAT_UYVY:
slouken@1903
   276
        return D3DFMT_UYVY;
slouken@1965
   277
    case SDL_PIXELFORMAT_YUY2:
slouken@1903
   278
        return D3DFMT_YUY2;
slouken@1903
   279
    default:
slouken@1903
   280
        return D3DFMT_UNKNOWN;
slouken@1903
   281
    }
slouken@1895
   282
}
slouken@1895
   283
slouken@2973
   284
static SDL_bool
slouken@3527
   285
D3D_IsTextureFormatAvailable(IDirect3D9 * d3d, UINT adapter,
slouken@5154
   286
                             D3DFORMAT display_format,
slouken@5154
   287
                             D3DFORMAT texture_format)
slouken@2973
   288
{
slouken@2973
   289
    HRESULT result;
slouken@2973
   290
slouken@3527
   291
    result = IDirect3D9_CheckDeviceFormat(d3d, adapter,
slouken@2973
   292
                                          D3DDEVTYPE_HAL,
slouken@5154
   293
                                          display_format,
slouken@2973
   294
                                          0,
slouken@2973
   295
                                          D3DRTYPE_TEXTURE,
slouken@5154
   296
                                          texture_format);
slouken@2973
   297
    return FAILED(result) ? SDL_FALSE : SDL_TRUE;
slouken@2973
   298
}
slouken@2973
   299
slouken@2973
   300
static void
slouken@2973
   301
UpdateYUVTextureData(SDL_Texture * texture)
slouken@2973
   302
{
slouken@2973
   303
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@2973
   304
    SDL_Rect rect;
slouken@2973
   305
    RECT d3drect;
slouken@2973
   306
    D3DLOCKED_RECT locked;
slouken@2973
   307
    HRESULT result;
slouken@2973
   308
slouken@2973
   309
    d3drect.left = 0;
slouken@2973
   310
    d3drect.right = texture->w;
slouken@2973
   311
    d3drect.top = 0;
slouken@2973
   312
    d3drect.bottom = texture->h;
slouken@2973
   313
slouken@2990
   314
    result =
slouken@2990
   315
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@2973
   316
    if (FAILED(result)) {
slouken@2973
   317
        return;
slouken@2973
   318
    }
slouken@2973
   319
slouken@2973
   320
    rect.x = 0;
slouken@2973
   321
    rect.y = 0;
slouken@2973
   322
    rect.w = texture->w;
slouken@2973
   323
    rect.h = texture->h;
slouken@2973
   324
    SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
slouken@2973
   325
                        texture->h, locked.pBits, locked.Pitch);
slouken@2973
   326
slouken@2973
   327
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   328
}
slouken@2973
   329
slouken@5154
   330
static void
slouken@5154
   331
D3D_AddTextureFormats(D3D_RenderData *data, SDL_RendererInfo *info)
slouken@1895
   332
{
slouken@5154
   333
    int i;
slouken@5154
   334
    int formats[] = {
slouken@5154
   335
        SDL_PIXELFORMAT_RGB332,
slouken@5154
   336
        SDL_PIXELFORMAT_RGB444,
slouken@5154
   337
        SDL_PIXELFORMAT_RGB555,
slouken@5154
   338
        SDL_PIXELFORMAT_ARGB4444,
slouken@5154
   339
        SDL_PIXELFORMAT_ARGB1555,
slouken@5154
   340
        SDL_PIXELFORMAT_RGB565,
slouken@5154
   341
        SDL_PIXELFORMAT_RGB888,
slouken@5154
   342
        SDL_PIXELFORMAT_ARGB8888,
slouken@5154
   343
        SDL_PIXELFORMAT_ARGB2101010,
slouken@5154
   344
    };
slouken@1895
   345
slouken@5154
   346
    info->num_texture_formats = 0;
slouken@5154
   347
    for (i = 0; i < SDL_arraysize(formats); ++i) {
slouken@5154
   348
        if (D3D_IsTextureFormatAvailable
slouken@5154
   349
            (data->d3d, data->adapter, data->pparams.BackBufferFormat, PixelFormatToD3DFMT(formats[i]))) {
slouken@5154
   350
            info->texture_formats[info->num_texture_formats++] = formats[i];
slouken@3520
   351
        }
slouken@1895
   352
    }
slouken@5154
   353
    info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@5154
   354
    info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@5154
   355
    info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YUY2;
slouken@5154
   356
    info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_UYVY;
slouken@5154
   357
    info->texture_formats[info->num_texture_formats++] = SDL_PIXELFORMAT_YVYU;
slouken@1895
   358
}
slouken@1895
   359
slouken@1895
   360
SDL_Renderer *
slouken@1913
   361
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   362
{
slouken@1895
   363
    SDL_Renderer *renderer;
slouken@1913
   364
    D3D_RenderData *data;
slouken@5154
   365
    SDL_SysWMinfo windowinfo;
slouken@1900
   366
    HRESULT result;
slouken@1900
   367
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   368
    IDirect3DSwapChain9 *chain;
slouken@1925
   369
    D3DCAPS9 caps;
slouken@1895
   370
slouken@1920
   371
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   372
    if (!renderer) {
slouken@1895
   373
        SDL_OutOfMemory();
slouken@1895
   374
        return NULL;
slouken@1895
   375
    }
slouken@1895
   376
slouken@1920
   377
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   378
    if (!data) {
slouken@5154
   379
        SDL_free(renderer);
slouken@1895
   380
        SDL_OutOfMemory();
slouken@1895
   381
        return NULL;
slouken@1895
   382
    }
slouken@1895
   383
slouken@5154
   384
    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
slouken@5154
   385
    if (data->d3dDLL) {
slouken@5154
   386
        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@4569
   387
slouken@5154
   388
        D3DCreate =
slouken@5154
   389
            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
slouken@5154
   390
                                                            "Direct3DCreate9");
slouken@5154
   391
        if (D3DCreate) {
slouken@5154
   392
            data->d3d = D3DCreate(D3D_SDK_VERSION);
slouken@5154
   393
        }
slouken@5154
   394
        if (!data->d3d) {
slouken@5154
   395
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
   396
            data->d3dDLL = NULL;
slouken@5154
   397
        }
slouken@5154
   398
    }
slouken@5154
   399
    if (!data->d3d) {
slouken@5154
   400
        SDL_free(renderer);
slouken@5154
   401
        SDL_free(data);
slouken@5154
   402
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   403
        return NULL;
slouken@5154
   404
    }
slouken@5154
   405
slouken@1913
   406
    renderer->CreateTexture = D3D_CreateTexture;
slouken@2973
   407
    renderer->QueryTexturePixels = D3D_QueryTexturePixels;
slouken@1913
   408
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   409
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   410
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@1913
   411
    renderer->DirtyTexture = D3D_DirtyTexture;
slouken@3599
   412
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   413
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   414
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   415
    renderer->RenderCopy = D3D_RenderCopy;
slouken@3427
   416
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@3480
   417
    renderer->RenderWritePixels = D3D_RenderWritePixels;
slouken@1913
   418
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   419
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   420
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   421
    renderer->info = D3D_RenderDriver.info;
slouken@3685
   422
    renderer->window = window;
slouken@1895
   423
    renderer->driverdata = data;
slouken@1895
   424
slouken@1965
   425
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   426
slouken@5154
   427
    SDL_VERSION(&windowinfo.version);
slouken@5154
   428
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   429
slouken@1900
   430
    SDL_zero(pparams);
slouken@5154
   431
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@1900
   432
    pparams.BackBufferWidth = window->w;
slouken@1900
   433
    pparams.BackBufferHeight = window->h;
slouken@1903
   434
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   435
        pparams.BackBufferFormat =
slouken@3519
   436
            PixelFormatToD3DFMT(window->fullscreen_mode.format);
slouken@1903
   437
    } else {
slouken@1903
   438
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   439
    }
slouken@5142
   440
    pparams.BackBufferCount = 1;
slouken@5142
   441
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   442
slouken@1900
   443
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   444
        pparams.Windowed = FALSE;
slouken@1903
   445
        pparams.FullScreen_RefreshRateInHz =
slouken@3519
   446
            window->fullscreen_mode.refresh_rate;
slouken@1900
   447
    } else {
slouken@1900
   448
        pparams.Windowed = TRUE;
slouken@1903
   449
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   450
    }
slouken@1965
   451
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   452
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   453
    } else {
slouken@1907
   454
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   455
    }
slouken@1900
   456
slouken@5154
   457
    /* FIXME: Which adapter? */
slouken@5154
   458
    data->adapter = D3DADAPTER_DEFAULT;
slouken@5154
   459
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   460
slouken@5154
   461
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   462
                                     D3DDEVTYPE_HAL,
slouken@5154
   463
                                     pparams.hDeviceWindow,
slouken@3197
   464
                                     (caps.
slouken@3197
   465
                                      DevCaps &
slouken@3197
   466
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   467
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@1900
   468
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   469
                                     &pparams, &data->device);
slouken@1900
   470
    if (FAILED(result)) {
slouken@1913
   471
        D3D_DestroyRenderer(renderer);
slouken@1900
   472
        D3D_SetError("CreateDevice()", result);
slouken@1900
   473
        return NULL;
slouken@1900
   474
    }
slouken@1900
   475
    data->beginScene = SDL_TRUE;
slouken@1900
   476
slouken@1907
   477
    /* Get presentation parameters to fill info */
slouken@1907
   478
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   479
    if (FAILED(result)) {
slouken@1913
   480
        D3D_DestroyRenderer(renderer);
slouken@1907
   481
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   482
        return NULL;
slouken@1907
   483
    }
slouken@1907
   484
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   485
    if (FAILED(result)) {
slouken@1907
   486
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   487
        D3D_DestroyRenderer(renderer);
slouken@1907
   488
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   489
        return NULL;
slouken@1907
   490
    }
slouken@1907
   491
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   492
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   493
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   494
    }
slouken@1975
   495
    data->pparams = pparams;
slouken@1907
   496
slouken@5154
   497
    D3D_AddTextureFormats(data, &renderer->info);
slouken@5154
   498
slouken@1925
   499
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   500
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   501
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@1918
   502
slouken@1903
   503
    /* Set up parameters for rendering */
slouken@1904
   504
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   505
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   506
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   507
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   508
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   509
                                    D3DCULL_NONE);
slouken@1904
   510
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   511
    /* Enable color modulation by diffuse color */
slouken@1988
   512
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   513
                                          D3DTOP_MODULATE);
slouken@1988
   514
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   515
                                          D3DTA_TEXTURE);
slouken@1988
   516
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   517
                                          D3DTA_DIFFUSE);
slouken@1988
   518
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   519
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   520
                                          D3DTOP_MODULATE);
slouken@1988
   521
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   522
                                          D3DTA_TEXTURE);
slouken@1988
   523
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   524
                                          D3DTA_DIFFUSE);
slouken@1991
   525
    /* Disable second texture stage, since we're done */
slouken@1991
   526
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   527
                                          D3DTOP_DISABLE);
slouken@1991
   528
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   529
                                          D3DTOP_DISABLE);
slouken@1903
   530
slouken@1895
   531
    return renderer;
slouken@1895
   532
}
slouken@1895
   533
slouken@1895
   534
static int
slouken@1975
   535
D3D_Reset(SDL_Renderer * renderer)
slouken@1975
   536
{
slouken@1975
   537
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   538
    HRESULT result;
slouken@1975
   539
slouken@1975
   540
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@1975
   541
    if (FAILED(result)) {
slouken@1975
   542
        if (result == D3DERR_DEVICELOST) {
slouken@1975
   543
            /* Don't worry about it, we'll reset later... */
slouken@1975
   544
            return 0;
slouken@1975
   545
        } else {
slouken@1975
   546
            D3D_SetError("Reset()", result);
slouken@1975
   547
            return -1;
slouken@1975
   548
        }
slouken@1975
   549
    }
slouken@1975
   550
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   551
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   552
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1975
   553
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1975
   554
                                    D3DCULL_NONE);
slouken@1975
   555
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1975
   556
    return 0;
slouken@1975
   557
}
slouken@1975
   558
slouken@5154
   559
/* FIXME: This needs to be called... when? */
slouken@5154
   560
#if 0
slouken@1975
   561
static int
slouken@1975
   562
D3D_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1975
   563
{
slouken@1975
   564
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3685
   565
    SDL_Window *window = renderer->window;
slouken@3685
   566
    SDL_VideoDisplay *display = window->display;
slouken@1975
   567
slouken@1975
   568
    data->pparams.BackBufferWidth = window->w;
slouken@1975
   569
    data->pparams.BackBufferHeight = window->h;
slouken@1975
   570
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1975
   571
        data->pparams.BackBufferFormat =
slouken@3519
   572
            PixelFormatToD3DFMT(window->fullscreen_mode.format);
slouken@1975
   573
    } else {
slouken@1975
   574
        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1975
   575
    }
slouken@1975
   576
    return D3D_Reset(renderer);
slouken@1975
   577
}
slouken@5154
   578
#endif
slouken@1975
   579
slouken@1975
   580
static int
slouken@1913
   581
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   582
{
slouken@1913
   583
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@3685
   584
    SDL_Window *window = renderer->window;
slouken@5154
   585
    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
slouken@1913
   586
    D3D_TextureData *data;
slouken@1903
   587
    HRESULT result;
slouken@1895
   588
slouken@1920
   589
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   590
    if (!data) {
slouken@1895
   591
        SDL_OutOfMemory();
slouken@1895
   592
        return -1;
slouken@1895
   593
    }
slouken@1895
   594
slouken@1895
   595
    texture->driverdata = data;
slouken@1895
   596
slouken@2973
   597
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format) &&
slouken@2973
   598
        (texture->format != SDL_PIXELFORMAT_YUY2 ||
slouken@3527
   599
         !D3D_IsTextureFormatAvailable(renderdata->d3d, renderdata->adapter,
slouken@5154
   600
                                       display_format, PixelFormatToD3DFMT(texture->format)))
slouken@2990
   601
        && (texture->format != SDL_PIXELFORMAT_YVYU
slouken@3527
   602
            || !D3D_IsTextureFormatAvailable(renderdata->d3d, renderdata->adapter,
slouken@5154
   603
                                             display_format, PixelFormatToD3DFMT(texture->format)))) {
slouken@2973
   604
        data->yuv =
slouken@2973
   605
            SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
slouken@2973
   606
        if (!data->yuv) {
slouken@2973
   607
            return -1;
slouken@2973
   608
        }
slouken@5154
   609
        data->format = SDL_GetWindowPixelFormat(window);
slouken@2973
   610
    } else {
slouken@2973
   611
        data->format = texture->format;
slouken@2973
   612
    }
slouken@2973
   613
slouken@1903
   614
    result =
slouken@1903
   615
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   616
                                       texture->h, 1, 0,
slouken@2973
   617
                                       PixelFormatToD3DFMT(data->format),
slouken@2783
   618
                                       D3DPOOL_SDL, &data->texture, NULL);
slouken@1903
   619
    if (FAILED(result)) {
slouken@1903
   620
        D3D_SetError("CreateTexture()", result);
slouken@1903
   621
        return -1;
slouken@1903
   622
    }
slouken@1903
   623
slouken@1895
   624
    return 0;
slouken@1895
   625
}
slouken@1895
   626
slouken@1895
   627
static int
slouken@2973
   628
D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2973
   629
                       void **pixels, int *pitch)
slouken@2973
   630
{
slouken@2973
   631
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@2973
   632
slouken@2973
   633
    if (data->yuv) {
slouken@2973
   634
        return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
slouken@2973
   635
    } else {
slouken@2973
   636
        /* D3D textures don't have their pixels hanging out */
slouken@2973
   637
        return -1;
slouken@2973
   638
    }
slouken@2973
   639
}
slouken@2973
   640
slouken@2973
   641
static int
slouken@1913
   642
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   643
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   644
{
slouken@1913
   645
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   646
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   647
slouken@2973
   648
    if (data->yuv) {
slouken@2973
   649
        if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
slouken@2973
   650
            return -1;
slouken@2973
   651
        }
slouken@2973
   652
        UpdateYUVTextureData(texture);
slouken@2973
   653
        return 0;
slouken@2973
   654
    } else {
slouken@2973
   655
#ifdef SDL_MEMORY_POOL_DEFAULT
slouken@2973
   656
        IDirect3DTexture9 *temp;
slouken@2973
   657
        RECT d3drect;
slouken@2973
   658
        D3DLOCKED_RECT locked;
slouken@2973
   659
        const Uint8 *src;
slouken@2973
   660
        Uint8 *dst;
slouken@2973
   661
        int row, length;
slouken@2973
   662
        HRESULT result;
slouken@2973
   663
slouken@2973
   664
        result =
slouken@2973
   665
            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@2973
   666
                                           texture->h, 1, 0,
slouken@3013
   667
                                           PixelFormatToD3DFMT(texture->
slouken@3013
   668
                                                               format),
slouken@2973
   669
                                           D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@2973
   670
        if (FAILED(result)) {
slouken@2973
   671
            D3D_SetError("CreateTexture()", result);
slouken@2973
   672
            return -1;
slouken@2973
   673
        }
slouken@2973
   674
slouken@2973
   675
        d3drect.left = rect->x;
slouken@2973
   676
        d3drect.right = rect->x + rect->w;
slouken@2973
   677
        d3drect.top = rect->y;
slouken@2973
   678
        d3drect.bottom = rect->y + rect->h;
slouken@2973
   679
slouken@2973
   680
        result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@2973
   681
        if (FAILED(result)) {
slouken@2973
   682
            IDirect3DTexture9_Release(temp);
slouken@2973
   683
            D3D_SetError("LockRect()", result);
slouken@2973
   684
            return -1;
slouken@2973
   685
        }
slouken@2973
   686
slouken@2973
   687
        src = pixels;
slouken@2973
   688
        dst = locked.pBits;
slouken@2973
   689
        length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2973
   690
        for (row = 0; row < rect->h; ++row) {
slouken@2973
   691
            SDL_memcpy(dst, src, length);
slouken@2973
   692
            src += pitch;
slouken@2973
   693
            dst += locked.Pitch;
slouken@2973
   694
        }
slouken@2973
   695
        IDirect3DTexture9_UnlockRect(temp, 0);
slouken@2973
   696
slouken@2973
   697
        result =
slouken@2973
   698
            IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@2973
   699
                                           (IDirect3DBaseTexture9 *) temp,
slouken@2973
   700
                                           (IDirect3DBaseTexture9 *)
slouken@2973
   701
                                           data->texture);
slouken@2973
   702
        IDirect3DTexture9_Release(temp);
slouken@2973
   703
        if (FAILED(result)) {
slouken@2973
   704
            D3D_SetError("UpdateTexture()", result);
slouken@2973
   705
            return -1;
slouken@2973
   706
        }
slouken@2973
   707
#else
slouken@2973
   708
        RECT d3drect;
slouken@2973
   709
        D3DLOCKED_RECT locked;
slouken@2973
   710
        const Uint8 *src;
slouken@2973
   711
        Uint8 *dst;
slouken@2973
   712
        int row, length;
slouken@2973
   713
        HRESULT result;
slouken@2973
   714
slouken@2973
   715
        d3drect.left = rect->x;
slouken@2973
   716
        d3drect.right = rect->x + rect->w;
slouken@2973
   717
        d3drect.top = rect->y;
slouken@2973
   718
        d3drect.bottom = rect->y + rect->h;
slouken@2973
   719
slouken@2973
   720
        result =
slouken@2990
   721
            IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@2990
   722
                                       0);
slouken@2973
   723
        if (FAILED(result)) {
slouken@2973
   724
            D3D_SetError("LockRect()", result);
slouken@2973
   725
            return -1;
slouken@2973
   726
        }
slouken@2973
   727
slouken@2973
   728
        src = pixels;
slouken@2973
   729
        dst = locked.pBits;
slouken@2973
   730
        length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2973
   731
        for (row = 0; row < rect->h; ++row) {
slouken@2973
   732
            SDL_memcpy(dst, src, length);
slouken@2973
   733
            src += pitch;
slouken@2973
   734
            dst += locked.Pitch;
slouken@2973
   735
        }
slouken@2973
   736
        IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   737
#endif // SDL_MEMORY_POOL_DEFAULT
slouken@2973
   738
slouken@2973
   739
        return 0;
slouken@1903
   740
    }
slouken@1895
   741
}
slouken@1895
   742
slouken@1895
   743
static int
slouken@1913
   744
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   745
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   746
                int *pitch)
slouken@1895
   747
{
slouken@1913
   748
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   749
slouken@2973
   750
    if (data->yuv) {
slouken@2973
   751
        return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
slouken@2973
   752
                                     pitch);
slouken@2973
   753
    } else {
slouken@2973
   754
        RECT d3drect;
slouken@2973
   755
        D3DLOCKED_RECT locked;
slouken@2973
   756
        HRESULT result;
slouken@1903
   757
slouken@2973
   758
        d3drect.left = rect->x;
slouken@2973
   759
        d3drect.right = rect->x + rect->w;
slouken@2973
   760
        d3drect.top = rect->y;
slouken@2973
   761
        d3drect.bottom = rect->y + rect->h;
slouken@2973
   762
slouken@2973
   763
        result =
slouken@2973
   764
            IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@2990
   765
                                       markDirty ? 0 :
slouken@2990
   766
                                       D3DLOCK_NO_DIRTY_UPDATE);
slouken@2973
   767
        if (FAILED(result)) {
slouken@2973
   768
            D3D_SetError("LockRect()", result);
slouken@2973
   769
            return -1;
slouken@2973
   770
        }
slouken@2973
   771
        *pixels = locked.pBits;
slouken@2973
   772
        *pitch = locked.Pitch;
slouken@2973
   773
        return 0;
slouken@1903
   774
    }
slouken@1895
   775
}
slouken@1895
   776
slouken@1895
   777
static void
slouken@1913
   778
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   779
{
slouken@1913
   780
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   781
slouken@2973
   782
    if (data->yuv) {
slouken@2973
   783
        SDL_SW_UnlockYUVTexture(data->yuv);
slouken@2973
   784
        UpdateYUVTextureData(texture);
slouken@2973
   785
    } else {
slouken@2973
   786
        IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   787
    }
slouken@1895
   788
}
slouken@1895
   789
slouken@1895
   790
static void
slouken@1913
   791
D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   792
                 const SDL_Rect * rects)
slouken@1895
   793
{
slouken@1913
   794
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   795
    RECT d3drect;
slouken@1903
   796
    int i;
slouken@1903
   797
slouken@1903
   798
    for (i = 0; i < numrects; ++i) {
slouken@1903
   799
        const SDL_Rect *rect = &rects[i];
slouken@1903
   800
slouken@1903
   801
        d3drect.left = rect->x;
slouken@1903
   802
        d3drect.right = rect->x + rect->w;
slouken@1903
   803
        d3drect.top = rect->y;
slouken@1903
   804
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   805
slouken@1903
   806
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   807
    }
slouken@1895
   808
}
slouken@1895
   809
slouken@2932
   810
static void
slouken@2933
   811
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   812
{
slouken@2932
   813
    switch (blendMode) {
slouken@2932
   814
    case SDL_BLENDMODE_NONE:
slouken@2932
   815
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   816
                                        FALSE);
slouken@2932
   817
        break;
slouken@2932
   818
    case SDL_BLENDMODE_BLEND:
slouken@2932
   819
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   820
                                        TRUE);
slouken@2932
   821
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   822
                                        D3DBLEND_SRCALPHA);
slouken@2932
   823
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   824
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   825
        break;
slouken@2932
   826
    case SDL_BLENDMODE_ADD:
slouken@2932
   827
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   828
                                        TRUE);
slouken@2932
   829
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   830
                                        D3DBLEND_SRCALPHA);
slouken@2932
   831
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   832
                                        D3DBLEND_ONE);
slouken@2932
   833
        break;
slouken@2932
   834
    }
slouken@2932
   835
}
slouken@2932
   836
slouken@1895
   837
static int
slouken@3599
   838
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   839
                     int count)
slouken@1895
   840
{
slouken@1913
   841
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   842
    DWORD color;
slouken@3536
   843
    Vertex *vertices;
slouken@3536
   844
    int i;
slouken@1900
   845
    HRESULT result;
slouken@1895
   846
slouken@1900
   847
    if (data->beginScene) {
slouken@1900
   848
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   849
        data->beginScene = SDL_FALSE;
slouken@1900
   850
    }
slouken@1895
   851
slouken@2932
   852
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   853
slouken@2933
   854
    result =
slouken@2933
   855
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   856
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   857
    if (FAILED(result)) {
slouken@2932
   858
        D3D_SetError("SetTexture()", result);
slouken@2932
   859
        return -1;
slouken@2932
   860
    }
slouken@3536
   861
slouken@3536
   862
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   863
slouken@3536
   864
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   865
    for (i = 0; i < count; ++i) {
slouken@3536
   866
        vertices[i].x = (float) points[i].x;
slouken@3536
   867
        vertices[i].y = (float) points[i].y;
slouken@3536
   868
        vertices[i].z = 0.0f;
slouken@3536
   869
        vertices[i].rhw = 1.0f;
slouken@3536
   870
        vertices[i].color = color;
slouken@3536
   871
        vertices[i].u = 0.0f;
slouken@3536
   872
        vertices[i].v = 0.0f;
slouken@3536
   873
    }
slouken@1903
   874
    result =
slouken@3536
   875
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
   876
                                         vertices, sizeof(*vertices));
slouken@3536
   877
    SDL_stack_free(vertices);
slouken@1900
   878
    if (FAILED(result)) {
slouken@2932
   879
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   880
        return -1;
slouken@2932
   881
    }
slouken@2932
   882
    return 0;
slouken@2932
   883
}
slouken@2932
   884
slouken@2932
   885
static int
slouken@3599
   886
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   887
                    int count)
slouken@2932
   888
{
slouken@2932
   889
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   890
    DWORD color;
slouken@3536
   891
    Vertex *vertices;
slouken@3536
   892
    int i;
slouken@2932
   893
    HRESULT result;
slouken@2932
   894
slouken@2932
   895
    if (data->beginScene) {
slouken@2932
   896
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   897
        data->beginScene = SDL_FALSE;
slouken@2932
   898
    }
slouken@2932
   899
slouken@2932
   900
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   901
slouken@2933
   902
    result =
slouken@2933
   903
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   904
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   905
    if (FAILED(result)) {
slouken@2932
   906
        D3D_SetError("SetTexture()", result);
slouken@2932
   907
        return -1;
slouken@2932
   908
    }
slouken@3536
   909
slouken@3536
   910
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   911
slouken@3536
   912
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   913
    for (i = 0; i < count; ++i) {
slouken@3536
   914
        vertices[i].x = (float) points[i].x;
slouken@3536
   915
        vertices[i].y = (float) points[i].y;
slouken@3536
   916
        vertices[i].z = 0.0f;
slouken@3536
   917
        vertices[i].rhw = 1.0f;
slouken@3536
   918
        vertices[i].color = color;
slouken@3536
   919
        vertices[i].u = 0.0f;
slouken@3536
   920
        vertices[i].v = 0.0f;
slouken@3536
   921
    }
slouken@2932
   922
    result =
slouken@3551
   923
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
   924
                                         vertices, sizeof(*vertices));
slouken@3551
   925
slouken@3551
   926
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
   927
       so we need to close the endpoint of the line */
slouken@3551
   928
    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3551
   929
        vertices[0].x = (float) points[count-1].x;
slouken@3551
   930
        vertices[0].y = (float) points[count-1].y;
slouken@3551
   931
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
   932
    }
slouken@3551
   933
slouken@3536
   934
    SDL_stack_free(vertices);
slouken@2932
   935
    if (FAILED(result)) {
slouken@2932
   936
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   937
        return -1;
slouken@2932
   938
    }
slouken@2932
   939
    return 0;
slouken@2932
   940
}
slouken@2932
   941
slouken@2932
   942
static int
slouken@3599
   943
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
slouken@3599
   944
                    int count)
slouken@2932
   945
{
slouken@2932
   946
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
   947
    DWORD color;
slouken@3536
   948
    int i;
slouken@2932
   949
    float minx, miny, maxx, maxy;
slouken@2932
   950
    Vertex vertices[4];
slouken@2932
   951
    HRESULT result;
slouken@2932
   952
slouken@2932
   953
    if (data->beginScene) {
slouken@2932
   954
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   955
        data->beginScene = SDL_FALSE;
slouken@2932
   956
    }
slouken@2932
   957
slouken@2932
   958
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   959
slouken@2933
   960
    result =
slouken@2933
   961
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   962
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   963
    if (FAILED(result)) {
slouken@2932
   964
        D3D_SetError("SetTexture()", result);
slouken@2932
   965
        return -1;
slouken@2932
   966
    }
slouken@3536
   967
slouken@3536
   968
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   969
slouken@3536
   970
    for (i = 0; i < count; ++i) {
slouken@3536
   971
        const SDL_Rect *rect = rects[i];
slouken@3536
   972
slouken@3536
   973
        minx = (float) rect->x;
slouken@3536
   974
        miny = (float) rect->y;
slouken@3536
   975
        maxx = (float) rect->x + rect->w;
slouken@3536
   976
        maxy = (float) rect->y + rect->h;
slouken@3536
   977
slouken@3536
   978
        vertices[0].x = minx;
slouken@3536
   979
        vertices[0].y = miny;
slouken@3536
   980
        vertices[0].z = 0.0f;
slouken@3536
   981
        vertices[0].rhw = 1.0f;
slouken@3536
   982
        vertices[0].color = color;
slouken@3536
   983
        vertices[0].u = 0.0f;
slouken@3536
   984
        vertices[0].v = 0.0f;
slouken@3536
   985
slouken@3536
   986
        vertices[1].x = maxx;
slouken@3536
   987
        vertices[1].y = miny;
slouken@3536
   988
        vertices[1].z = 0.0f;
slouken@3536
   989
        vertices[1].rhw = 1.0f;
slouken@3536
   990
        vertices[1].color = color;
slouken@3536
   991
        vertices[1].u = 0.0f;
slouken@3536
   992
        vertices[1].v = 0.0f;
slouken@3536
   993
slouken@3536
   994
        vertices[2].x = maxx;
slouken@3536
   995
        vertices[2].y = maxy;
slouken@3536
   996
        vertices[2].z = 0.0f;
slouken@3536
   997
        vertices[2].rhw = 1.0f;
slouken@3536
   998
        vertices[2].color = color;
slouken@3536
   999
        vertices[2].u = 0.0f;
slouken@3536
  1000
        vertices[2].v = 0.0f;
slouken@3536
  1001
slouken@3536
  1002
        vertices[3].x = minx;
slouken@3536
  1003
        vertices[3].y = maxy;
slouken@3536
  1004
        vertices[3].z = 0.0f;
slouken@3536
  1005
        vertices[3].rhw = 1.0f;
slouken@3536
  1006
        vertices[3].color = color;
slouken@3536
  1007
        vertices[3].u = 0.0f;
slouken@3536
  1008
        vertices[3].v = 0.0f;
slouken@3536
  1009
slouken@3536
  1010
        result =
slouken@3536
  1011
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
  1012
                                             2, vertices, sizeof(*vertices));
slouken@3536
  1013
        if (FAILED(result)) {
slouken@3536
  1014
            D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1015
            return -1;
slouken@3536
  1016
        }
slouken@1900
  1017
    }
slouken@1895
  1018
    return 0;
slouken@1895
  1019
}
slouken@1895
  1020
slouken@1895
  1021
static int
slouken@1913
  1022
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1023
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
  1024
{
slouken@1913
  1025
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1026
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
  1027
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
  1028
    float minx, miny, maxx, maxy;
slouken@1904
  1029
    float minu, maxu, minv, maxv;
slouken@1987
  1030
    DWORD color;
slouken@1903
  1031
    Vertex vertices[4];
slouken@1903
  1032
    HRESULT result;
slouken@1895
  1033
slouken@1900
  1034
    if (data->beginScene) {
slouken@1900
  1035
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
  1036
        data->beginScene = SDL_FALSE;
slouken@1900
  1037
    }
slouken@1903
  1038
slouken@3279
  1039
    minx = (float) dstrect->x - 0.5f;
slouken@3279
  1040
    miny = (float) dstrect->y - 0.5f;
slouken@3279
  1041
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@3279
  1042
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1043
slouken@1904
  1044
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1045
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1046
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1047
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1048
slouken@1987
  1049
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1050
slouken@1903
  1051
    vertices[0].x = minx;
slouken@1903
  1052
    vertices[0].y = miny;
slouken@1903
  1053
    vertices[0].z = 0.0f;
slouken@1904
  1054
    vertices[0].rhw = 1.0f;
slouken@1987
  1055
    vertices[0].color = color;
slouken@1904
  1056
    vertices[0].u = minu;
slouken@1904
  1057
    vertices[0].v = minv;
slouken@1904
  1058
slouken@1903
  1059
    vertices[1].x = maxx;
slouken@1903
  1060
    vertices[1].y = miny;
slouken@1903
  1061
    vertices[1].z = 0.0f;
slouken@1904
  1062
    vertices[1].rhw = 1.0f;
slouken@1987
  1063
    vertices[1].color = color;
slouken@1904
  1064
    vertices[1].u = maxu;
slouken@1904
  1065
    vertices[1].v = minv;
slouken@1904
  1066
slouken@1903
  1067
    vertices[2].x = maxx;
slouken@1903
  1068
    vertices[2].y = maxy;
slouken@1903
  1069
    vertices[2].z = 0.0f;
slouken@1904
  1070
    vertices[2].rhw = 1.0f;
slouken@1987
  1071
    vertices[2].color = color;
slouken@1904
  1072
    vertices[2].u = maxu;
slouken@1904
  1073
    vertices[2].v = maxv;
slouken@1904
  1074
slouken@1903
  1075
    vertices[3].x = minx;
slouken@1903
  1076
    vertices[3].y = maxy;
slouken@1903
  1077
    vertices[3].z = 0.0f;
slouken@1904
  1078
    vertices[3].rhw = 1.0f;
slouken@1987
  1079
    vertices[3].color = color;
slouken@1904
  1080
    vertices[3].u = minu;
slouken@1904
  1081
    vertices[3].v = maxv;
slouken@1903
  1082
slouken@2932
  1083
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1084
slouken@5138
  1085
    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@5138
  1086
                                     D3DTEXF_LINEAR);
slouken@5138
  1087
    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@5138
  1088
                                     D3DTEXF_LINEAR);
slouken@1917
  1089
slouken@1903
  1090
    result =
slouken@2735
  1091
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1092
                                    texturedata->texture);
slouken@1903
  1093
    if (FAILED(result)) {
slouken@1903
  1094
        D3D_SetError("SetTexture()", result);
slouken@1903
  1095
        return -1;
slouken@1903
  1096
    }
slouken@3556
  1097
    if (shader) {
slouken@3556
  1098
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
  1099
        if (FAILED(result)) {
slouken@3556
  1100
            D3D_SetError("SetShader()", result);
slouken@3556
  1101
            return -1;
slouken@3556
  1102
        }
slouken@3556
  1103
    }
slouken@1903
  1104
    result =
slouken@1903
  1105
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1106
                                         vertices, sizeof(*vertices));
slouken@1903
  1107
    if (FAILED(result)) {
slouken@1903
  1108
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1109
        return -1;
slouken@1903
  1110
    }
slouken@3556
  1111
    if (shader) {
slouken@3556
  1112
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
  1113
        if (FAILED(result)) {
slouken@3556
  1114
            D3D_SetError("SetShader()", result);
slouken@3556
  1115
            return -1;
slouken@3556
  1116
        }
slouken@3556
  1117
    }
slouken@1895
  1118
    return 0;
slouken@1895
  1119
}
slouken@1895
  1120
slouken@3427
  1121
static int
slouken@3427
  1122
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1123
                     Uint32 format, void * pixels, int pitch)
slouken@3427
  1124
{
slouken@3549
  1125
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3685
  1126
    SDL_Window *window = renderer->window;
slouken@3685
  1127
    SDL_VideoDisplay *display = window->display;
slouken@3549
  1128
    D3DSURFACE_DESC desc;
slouken@3549
  1129
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
  1130
    LPDIRECT3DSURFACE9 surface;
slouken@3549
  1131
    RECT d3drect;
slouken@3549
  1132
    D3DLOCKED_RECT locked;
slouken@3549
  1133
    HRESULT result;
slouken@3427
  1134
slouken@3549
  1135
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
  1136
    if (FAILED(result)) {
slouken@3549
  1137
        D3D_SetError("GetBackBuffer()", result);
slouken@3549
  1138
        return -1;
slouken@3549
  1139
    }
slouken@3427
  1140
slouken@3549
  1141
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
  1142
    if (FAILED(result)) {
slouken@3549
  1143
        D3D_SetError("GetDesc()", result);
slouken@3549
  1144
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1145
        return -1;
slouken@3549
  1146
    }
slouken@3427
  1147
slouken@3549
  1148
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
  1149
    if (FAILED(result)) {
slouken@3549
  1150
        D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
  1151
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1152
        return -1;
slouken@3549
  1153
    }
slouken@3427
  1154
slouken@3549
  1155
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
  1156
    if (FAILED(result)) {
slouken@3549
  1157
        D3D_SetError("GetRenderTargetData()", result);
slouken@3549
  1158
        IDirect3DSurface9_Release(surface);
slouken@3549
  1159
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1160
        return -1;
slouken@3549
  1161
    }
slouken@3427
  1162
slouken@3549
  1163
    d3drect.left = rect->x;
slouken@3549
  1164
    d3drect.right = rect->x + rect->w;
slouken@3549
  1165
    d3drect.top = rect->y;
slouken@3549
  1166
    d3drect.bottom = rect->y + rect->h;
slouken@3427
  1167
slouken@3549
  1168
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
  1169
    if (FAILED(result)) {
slouken@3549
  1170
        D3D_SetError("LockRect()", result);
slouken@3549
  1171
        IDirect3DSurface9_Release(surface);
slouken@3549
  1172
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1173
        return -1;
slouken@3549
  1174
    }
slouken@3427
  1175
slouken@3549
  1176
    SDL_ConvertPixels(rect->w, rect->h,
slouken@3549
  1177
                      display->current_mode.format, locked.pBits, locked.Pitch,
slouken@3549
  1178
                      format, pixels, pitch);
slouken@3549
  1179
slouken@3549
  1180
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
  1181
slouken@3549
  1182
    IDirect3DSurface9_Release(surface);
slouken@3549
  1183
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1184
slouken@3549
  1185
    return 0;
slouken@3427
  1186
}
slouken@3427
  1187
slouken@3480
  1188
static int
slouken@3480
  1189
D3D_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1190
                      Uint32 format, const void * pixels, int pitch)
slouken@3480
  1191
{
slouken@3480
  1192
    /* Work in progress */
slouken@3553
  1193
    SDL_Unsupported();
slouken@3480
  1194
    return -1;
slouken@3480
  1195
}
slouken@3480
  1196
slouken@1895
  1197
static void
slouken@1913
  1198
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1199
{
slouken@1913
  1200
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1201
    HRESULT result;
slouken@1900
  1202
slouken@1900
  1203
    if (!data->beginScene) {
slouken@1900
  1204
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1205
        data->beginScene = SDL_TRUE;
slouken@1900
  1206
    }
slouken@1900
  1207
slouken@1975
  1208
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1209
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1210
        /* We'll reset later */
slouken@1975
  1211
        return;
slouken@1975
  1212
    }
slouken@1975
  1213
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1214
        D3D_Reset(renderer);
slouken@1975
  1215
    }
slouken@1900
  1216
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1217
    if (FAILED(result)) {
slouken@1900
  1218
        D3D_SetError("Present()", result);
slouken@1900
  1219
    }
slouken@1895
  1220
}
slouken@1895
  1221
slouken@1895
  1222
static void
slouken@1913
  1223
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1224
{
slouken@1913
  1225
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1226
slouken@1895
  1227
    if (!data) {
slouken@1895
  1228
        return;
slouken@1895
  1229
    }
slouken@2973
  1230
    if (data->yuv) {
slouken@2973
  1231
        SDL_SW_DestroyYUVTexture(data->yuv);
slouken@2973
  1232
    }
slouken@1903
  1233
    if (data->texture) {
slouken@1903
  1234
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1235
    }
slouken@1895
  1236
    SDL_free(data);
slouken@1895
  1237
    texture->driverdata = NULL;
slouken@1895
  1238
}
slouken@1895
  1239
slouken@1975
  1240
static void
slouken@1913
  1241
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1242
{
slouken@1913
  1243
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1244
slouken@1895
  1245
    if (data) {
slouken@1900
  1246
        if (data->device) {
slouken@1900
  1247
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1248
        }
slouken@5154
  1249
        if (data->d3d) {
slouken@5154
  1250
            IDirect3D9_Release(data->d3d);
slouken@5154
  1251
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1252
        }
slouken@1895
  1253
        SDL_free(data);
slouken@1895
  1254
    }
slouken@1895
  1255
    SDL_free(renderer);
slouken@1895
  1256
}
slouken@1895
  1257
slouken@1895
  1258
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
  1259
slouken@1895
  1260
/* vi: set ts=4 sw=4 expandtab: */