src/render/direct3d/SDL_render_d3d.c
author Philipp Wiesemann
Sun, 07 Jul 2013 16:13:17 +0200
changeset 7369 80215ecb81cf
parent 7250 bbb6b079cfe5
child 7472 0ec51bd57d56
permissions -rw-r--r--
Changed include directive to standard header.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@5226
    23
#if SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED
slouken@5226
    24
slouken@1895
    25
slouken@5154
    26
#include "../../core/windows/SDL_windows.h"
slouken@5154
    27
slouken@5484
    28
#include "SDL_hints.h"
slouken@5154
    29
#include "SDL_loadso.h"
slouken@5154
    30
#include "SDL_syswm.h"
slouken@5154
    31
#include "../SDL_sysrender.h"
philipp@7369
    32
#include <stdio.h>
slouken@5154
    33
slouken@5154
    34
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    35
#define D3D_DEBUG_INFO
slouken@5154
    36
#include <d3d9.h>
slouken@5154
    37
#endif
slouken@1895
    38
gabomdq@6320
    39
gabomdq@6320
    40
typedef interface ID3DXMatrixStack *LPD3DXMATRIXSTACK;
gabomdq@6320
    41
typedef struct _D3DMATRIX D3DXMATRIX, *LPD3DXMATRIX;
gabomdq@6320
    42
typedef struct _D3DVECTOR D3DXVECTOR3, *LPD3DXVECTOR3;
gabomdq@6320
    43
gabomdq@6320
    44
DEFINE_GUID(IID_ID3DXMatrixStack,
gabomdq@6320
    45
0xc7885ba7, 0xf990, 0x4fe7, 0x92, 0x2d, 0x85, 0x15, 0xe4, 0x77, 0xdd, 0x85);
gabomdq@6320
    46
gabomdq@6320
    47
#undef INTERFACE
gabomdq@6320
    48
#define INTERFACE ID3DXMatrixStack
gabomdq@6320
    49
gabomdq@6320
    50
DECLARE_INTERFACE_(ID3DXMatrixStack, IUnknown)
gabomdq@6320
    51
{
gabomdq@6320
    52
    STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID * ppvObj) PURE;
gabomdq@6320
    53
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
gabomdq@6320
    54
    STDMETHOD_(ULONG,Release)(THIS) PURE;
gabomdq@6320
    55
    STDMETHOD(Pop)(THIS) PURE;
gabomdq@6320
    56
    STDMETHOD(Push)(THIS) PURE;
gabomdq@6320
    57
    STDMETHOD(LoadIdentity)(THIS) PURE;
gabomdq@6320
    58
    STDMETHOD(LoadMatrix)(THIS_ CONST D3DXMATRIX* pM ) PURE;
gabomdq@6320
    59
    STDMETHOD(MultMatrix)(THIS_ CONST D3DXMATRIX* pM ) PURE;
gabomdq@6320
    60
    STDMETHOD(MultMatrixLocal)(THIS_ CONST D3DXMATRIX* pM ) PURE;
gabomdq@6320
    61
    STDMETHOD(RotateAxis)(THIS_ CONST D3DXVECTOR3* pV, FLOAT Angle) PURE;
gabomdq@6320
    62
    STDMETHOD(RotateAxisLocal)(THIS_ CONST D3DXVECTOR3* pV, FLOAT Angle) PURE;
gabomdq@6320
    63
    STDMETHOD(RotateYawPitchRoll)(THIS_ FLOAT Yaw, FLOAT Pitch, FLOAT Roll) PURE;
gabomdq@6320
    64
    STDMETHOD(RotateYawPitchRollLocal)(THIS_ FLOAT Yaw, FLOAT Pitch, FLOAT Roll) PURE;
gabomdq@6320
    65
    STDMETHOD(Scale)(THIS_ FLOAT x, FLOAT y, FLOAT z) PURE;
gabomdq@6320
    66
    STDMETHOD(ScaleLocal)(THIS_ FLOAT x, FLOAT y, FLOAT z) PURE;
gabomdq@6320
    67
    STDMETHOD(Translate)(THIS_ FLOAT x, FLOAT y, FLOAT z ) PURE;
gabomdq@6320
    68
    STDMETHOD(TranslateLocal)(THIS_ FLOAT x, FLOAT y, FLOAT z) PURE;
gabomdq@6320
    69
    STDMETHOD_(D3DXMATRIX*, GetTop)(THIS) PURE;
gabomdq@6320
    70
};
gabomdq@6320
    71
gabomdq@6320
    72
#undef INTERFACE
gabomdq@6320
    73
gabomdq@6320
    74
#if !defined(__cplusplus) || defined(CINTERFACE)
gabomdq@6320
    75
#define ID3DXMatrixStack_QueryInterface(p,a,b)            (p)->lpVtbl->QueryInterface(p,a,b)
gabomdq@6320
    76
#define ID3DXMatrixStack_AddRef(p)                        (p)->lpVtbl->AddRef(p)
gabomdq@6320
    77
#define ID3DXMatrixStack_Release(p)                       (p)->lpVtbl->Release(p)
gabomdq@6320
    78
#define ID3DXMatrixStack_Pop(p)                           (p)->lpVtbl->Pop(p)
gabomdq@6320
    79
#define ID3DXMatrixStack_Push(p)                          (p)->lpVtbl->Push(p)
gabomdq@6320
    80
#define ID3DXMatrixStack_LoadIdentity(p)                  (p)->lpVtbl->LoadIdentity(p)
gabomdq@6320
    81
#define ID3DXMatrixStack_LoadMatrix(p,a)                  (p)->lpVtbl->LoadMatrix(p,a)
gabomdq@6320
    82
#define ID3DXMatrixStack_MultMatrix(p,a)                  (p)->lpVtbl->MultMatrix(p,a)
gabomdq@6320
    83
#define ID3DXMatrixStack_MultMatrixLocal(p,a)             (p)->lpVtbl->MultMatrixLocal(p,a)
gabomdq@6320
    84
#define ID3DXMatrixStack_RotateAxis(p,a,b)                (p)->lpVtbl->RotateAxis(p,a,b)
gabomdq@6320
    85
#define ID3DXMatrixStack_RotateAxisLocal(p,a,b)           (p)->lpVtbl->RotateAxisLocal(p,a,b)
gabomdq@6320
    86
#define ID3DXMatrixStack_RotateYawPitchRoll(p,a,b,c)      (p)->lpVtbl->RotateYawPitchRoll(p,a,b,c)
gabomdq@6320
    87
#define ID3DXMatrixStack_RotateYawPitchRollLocal(p,a,b,c) (p)->lpVtbl->RotateYawPitchRollLocal(p,a,b,c)
gabomdq@6320
    88
#define ID3DXMatrixStack_Scale(p,a,b,c)                   (p)->lpVtbl->Scale(p,a,b,c)
gabomdq@6320
    89
#define ID3DXMatrixStack_ScaleLocal(p,a,b,c)              (p)->lpVtbl->ScaleLocal(p,a,b,c)
gabomdq@6320
    90
#define ID3DXMatrixStack_Translate(p,a,b,c)               (p)->lpVtbl->Translate(p,a,b,c)
gabomdq@6320
    91
#define ID3DXMatrixStack_TranslateLocal(p,a,b,c)          (p)->lpVtbl->TranslateLocal(p,a,b,c)
gabomdq@6320
    92
#define ID3DXMatrixStack_GetTop(p)                        (p)->lpVtbl->GetTop(p)
gabomdq@6320
    93
#else
gabomdq@6320
    94
#define ID3DXMatrixStack_QueryInterface(p,a,b)            (p)->QueryInterface(a,b)
gabomdq@6320
    95
#define ID3DXMatrixStack_AddRef(p)                        (p)->AddRef()
gabomdq@6320
    96
#define ID3DXMatrixStack_Release(p)                       (p)->Release()
gabomdq@6320
    97
#define ID3DXMatrixStack_Pop(p)    (p)->Pop()
gabomdq@6320
    98
#define ID3DXMatrixStack_Push(p)    (p)->Push()
gabomdq@6320
    99
#define ID3DXMatrixStack_LoadIdentity(p)    (p)->LoadIdentity()
gabomdq@6320
   100
#define ID3DXMatrixStack_LoadMatrix(p,a)    (p)->LoadMatrix(a)
gabomdq@6320
   101
#define ID3DXMatrixStack_MultMatrix(p,a)    (p)->MultMatrix(a)
gabomdq@6320
   102
#define ID3DXMatrixStack_MultMatrixLocal(p,a)    (p)->MultMatrixLocal(a)
gabomdq@6320
   103
#define ID3DXMatrixStack_RotateAxis(p,a,b)    (p)->RotateAxis(a,b)
gabomdq@6320
   104
#define ID3DXMatrixStack_RotateAxisLocal(p,a,b)    (p)->RotateAxisLocal(a,b)
gabomdq@6320
   105
#define ID3DXMatrixStack_RotateYawPitchRoll(p,a,b,c)    (p)->RotateYawPitchRollLocal(a,b,c)
gabomdq@6320
   106
#define ID3DXMatrixStack_Scale(p,a,b,c)    (p)->Scale(a,b,c)
gabomdq@6320
   107
#define ID3DXMatrixStack_ScaleLocal(p,a,b,c)    (p)->ScaleLocal(a,b,c)
gabomdq@6320
   108
#define ID3DXMatrixStack_Translate(p,a,b,c)    (p)->Translate(a,b,c)
gabomdq@6320
   109
#define ID3DXMatrixStack_TranslateLocal(p,a,b,c)    (p)->TranslateLocal(a,b,c)
gabomdq@6320
   110
#define ID3DXMatrixStack_GetTop(p)    (p)->GetTop()
gabomdq@6320
   111
#endif
gabomdq@6320
   112
gabomdq@6320
   113
#ifdef __cplusplus
gabomdq@6320
   114
extern "C" {
gabomdq@6320
   115
#endif
gabomdq@6320
   116
gabomdq@6320
   117
HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack);
gabomdq@6320
   118
gabomdq@6320
   119
#ifdef __cplusplus
gabomdq@6320
   120
}
gabomdq@6320
   121
#endif
gabomdq@6320
   122
slouken@3556
   123
#ifdef ASSEMBLE_SHADER
slouken@7191
   124
/**************************************************************************
slouken@7191
   125
 * ID3DXBuffer:
slouken@7191
   126
 * ------------
slouken@7191
   127
 * The buffer object is used by D3DX to return arbitrary size data.
slouken@7191
   128
 *
slouken@7191
   129
 * GetBufferPointer -
slouken@7191
   130
 *    Returns a pointer to the beginning of the buffer.
slouken@7191
   131
 *
slouken@7191
   132
 * GetBufferSize -
slouken@7191
   133
 *    Returns the size of the buffer, in bytes.
slouken@7191
   134
 **************************************************************************/
slouken@3556
   135
slouken@3556
   136
typedef interface ID3DXBuffer ID3DXBuffer;
slouken@3556
   137
typedef interface ID3DXBuffer *LPD3DXBUFFER;
slouken@3556
   138
slouken@7191
   139
/* {8BA5FB08-5195-40e2-AC58-0D989C3A0102} */
slouken@7191
   140
DEFINE_GUID(IID_ID3DXBuffer,
slouken@3556
   141
0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
slouken@3556
   142
slouken@3556
   143
#undef INTERFACE
slouken@3556
   144
#define INTERFACE ID3DXBuffer
slouken@3556
   145
slouken@3556
   146
typedef interface ID3DXBuffer {
slouken@3556
   147
    const struct ID3DXBufferVtbl FAR* lpVtbl;
slouken@3556
   148
} ID3DXBuffer;
slouken@3556
   149
typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
slouken@3556
   150
const struct ID3DXBufferVtbl
slouken@3556
   151
{
slouken@7191
   152
    /* IUnknown */
slouken@3556
   153
    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
slouken@3556
   154
    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
slouken@3556
   155
    STDMETHOD_(ULONG, Release)(THIS) PURE;
slouken@3556
   156
slouken@7191
   157
    /* ID3DXBuffer */
slouken@3556
   158
    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
slouken@3556
   159
    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
slouken@3556
   160
};
slouken@3556
   161
slouken@3556
   162
HRESULT WINAPI
slouken@3556
   163
    D3DXAssembleShader(
slouken@3556
   164
        LPCSTR                          pSrcData,
slouken@3556
   165
        UINT                            SrcDataLen,
slouken@3556
   166
        CONST LPVOID*                   pDefines,
slouken@3556
   167
        LPVOID                          pInclude,
slouken@3556
   168
        DWORD                           Flags,
slouken@3556
   169
        LPD3DXBUFFER*                   ppShader,
slouken@3556
   170
        LPD3DXBUFFER*                   ppErrorMsgs);
slouken@3556
   171
slouken@3556
   172
#endif /* ASSEMBLE_SHADER */
slouken@3556
   173
slouken@3556
   174
slouken@1895
   175
/* Direct3D renderer implementation */
slouken@1895
   176
slouken@1913
   177
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5297
   178
static void D3D_WindowEvent(SDL_Renderer * renderer,
slouken@5297
   179
                            const SDL_WindowEvent *event);
slouken@1913
   180
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
   181
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   182
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
   183
                             int pitch);
slouken@1913
   184
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   185
                           const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1913
   186
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6247
   187
static int D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
   188
static int D3D_UpdateViewport(SDL_Renderer * renderer);
slouken@7141
   189
static int D3D_UpdateClipRect(SDL_Renderer * renderer);
slouken@5297
   190
static int D3D_RenderClear(SDL_Renderer * renderer);
slouken@3599
   191
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
slouken@6528
   192
                                const SDL_FPoint * points, int count);
slouken@3599
   193
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
slouken@6528
   194
                               const SDL_FPoint * points, int count);
slouken@3599
   195
static int D3D_RenderFillRects(SDL_Renderer * renderer,
slouken@6528
   196
                               const SDL_FRect * rects, int count);
slouken@1913
   197
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   198
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect);
gabomdq@6320
   199
static int D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   200
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
   201
                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
slouken@3427
   202
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   203
                                Uint32 format, void * pixels, int pitch);
slouken@1913
   204
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
   205
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
   206
                               SDL_Texture * texture);
slouken@1913
   207
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
   208
slouken@1895
   209
slouken@1913
   210
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
   211
    D3D_CreateRenderer,
slouken@1895
   212
    {
slouken@5186
   213
     "direct3d",
slouken@6237
   214
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
slouken@5156
   215
     1,
slouken@5156
   216
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1895
   217
     0,
slouken@1895
   218
     0}
slouken@1895
   219
};
slouken@1895
   220
slouken@1895
   221
typedef struct
slouken@1895
   222
{
slouken@5154
   223
    void* d3dDLL;
slouken@2973
   224
    IDirect3D9 *d3d;
slouken@1895
   225
    IDirect3DDevice9 *device;
slouken@3527
   226
    UINT adapter;
slouken@1975
   227
    D3DPRESENT_PARAMETERS pparams;
slouken@5297
   228
    SDL_bool updateSize;
slouken@1900
   229
    SDL_bool beginScene;
slouken@5484
   230
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@6232
   231
    IDirect3DSurface9 *defaultRenderTarget;
slouken@6232
   232
    IDirect3DSurface9 *currentRenderTarget;
gabomdq@6320
   233
    void* d3dxDLL;
gabomdq@6320
   234
    ID3DXMatrixStack *matrixStack;
slouken@1913
   235
} D3D_RenderData;
slouken@1895
   236
slouken@1895
   237
typedef struct
slouken@1895
   238
{
slouken@1903
   239
    IDirect3DTexture9 *texture;
slouken@5484
   240
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@1913
   241
} D3D_TextureData;
slouken@1895
   242
slouken@1903
   243
typedef struct
slouken@1903
   244
{
slouken@1903
   245
    float x, y, z;
slouken@1987
   246
    DWORD color;
slouken@1904
   247
    float u, v;
slouken@1903
   248
} Vertex;
slouken@1903
   249
icculus@7037
   250
static int
slouken@1900
   251
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   252
{
slouken@1900
   253
    const char *error;
slouken@1900
   254
slouken@1900
   255
    switch (result) {
slouken@1900
   256
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   257
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   258
        break;
slouken@1900
   259
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   260
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   261
        break;
slouken@1900
   262
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   263
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   264
        break;
slouken@1900
   265
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   266
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   267
        break;
slouken@1900
   268
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   269
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   270
        break;
slouken@1900
   271
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   272
        error = "TOOMANYOPERATIONS";
slouken@1900
   273
        break;
slouken@1900
   274
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   275
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   276
        break;
slouken@1900
   277
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   278
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   279
        break;
slouken@1900
   280
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   281
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   282
        break;
slouken@1900
   283
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   284
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   285
        break;
slouken@1900
   286
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   287
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   288
        break;
slouken@1900
   289
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   290
        error = "DRIVERINTERNALERROR";
slouken@1900
   291
        break;
slouken@1900
   292
    case D3DERR_NOTFOUND:
slouken@1900
   293
        error = "NOTFOUND";
slouken@1900
   294
        break;
slouken@1900
   295
    case D3DERR_MOREDATA:
slouken@1900
   296
        error = "MOREDATA";
slouken@1900
   297
        break;
slouken@1900
   298
    case D3DERR_DEVICELOST:
slouken@1900
   299
        error = "DEVICELOST";
slouken@1900
   300
        break;
slouken@1900
   301
    case D3DERR_DEVICENOTRESET:
slouken@1900
   302
        error = "DEVICENOTRESET";
slouken@1900
   303
        break;
slouken@1900
   304
    case D3DERR_NOTAVAILABLE:
slouken@1900
   305
        error = "NOTAVAILABLE";
slouken@1900
   306
        break;
slouken@1900
   307
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   308
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   309
        break;
slouken@1900
   310
    case D3DERR_INVALIDDEVICE:
slouken@1900
   311
        error = "INVALIDDEVICE";
slouken@1900
   312
        break;
slouken@1900
   313
    case D3DERR_INVALIDCALL:
slouken@1900
   314
        error = "INVALIDCALL";
slouken@1900
   315
        break;
slouken@1900
   316
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   317
        error = "DRIVERINVALIDCALL";
slouken@1900
   318
        break;
slouken@1900
   319
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   320
        error = "WASSTILLDRAWING";
slouken@1900
   321
        break;
slouken@1900
   322
    default:
slouken@1900
   323
        error = "UNKNOWN";
slouken@1900
   324
        break;
slouken@1900
   325
    }
icculus@7037
   326
    return SDL_SetError("%s: %s", prefix, error);
slouken@1900
   327
}
slouken@1900
   328
slouken@1903
   329
static D3DFORMAT
slouken@1903
   330
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   331
{
slouken@1903
   332
    switch (format) {
slouken@1965
   333
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   334
        return D3DFMT_R5G6B5;
slouken@1965
   335
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   336
        return D3DFMT_X8R8G8B8;
slouken@1965
   337
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   338
        return D3DFMT_A8R8G8B8;
slouken@1903
   339
    default:
slouken@1903
   340
        return D3DFMT_UNKNOWN;
slouken@1903
   341
    }
slouken@1895
   342
}
slouken@1895
   343
slouken@5156
   344
static Uint32
slouken@5156
   345
D3DFMTToPixelFormat(D3DFORMAT format)
slouken@2973
   346
{
slouken@5156
   347
    switch (format) {
slouken@5156
   348
    case D3DFMT_R5G6B5:
slouken@5156
   349
        return SDL_PIXELFORMAT_RGB565;
slouken@5156
   350
    case D3DFMT_X8R8G8B8:
slouken@5156
   351
        return SDL_PIXELFORMAT_RGB888;
slouken@5156
   352
    case D3DFMT_A8R8G8B8:
slouken@5156
   353
        return SDL_PIXELFORMAT_ARGB8888;
slouken@5156
   354
    default:
slouken@5156
   355
        return SDL_PIXELFORMAT_UNKNOWN;
slouken@2973
   356
    }
slouken@1895
   357
}
slouken@1895
   358
slouken@5297
   359
static int
slouken@5297
   360
D3D_Reset(SDL_Renderer * renderer)
slouken@5297
   361
{
slouken@5297
   362
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   363
    HRESULT result;
slouken@5297
   364
slouken@6860
   365
    /* Release the default render target before reset */
slouken@6895
   366
    if (data->defaultRenderTarget) {
slouken@6895
   367
        IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6895
   368
        data->defaultRenderTarget = NULL;
slouken@6895
   369
    }
slouken@6860
   370
slouken@5297
   371
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@5297
   372
    if (FAILED(result)) {
slouken@5297
   373
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   374
            /* Don't worry about it, we'll reset later... */
slouken@5297
   375
            return 0;
slouken@5297
   376
        } else {
icculus@7037
   377
            return D3D_SetError("Reset()", result);
slouken@5297
   378
        }
slouken@5297
   379
    }
slouken@5297
   380
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@5297
   381
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   382
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@5297
   383
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@5297
   384
                                    D3DCULL_NONE);
slouken@5297
   385
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@6860
   386
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
yuriks@7250
   387
    data->scaleMode = D3DTEXF_FORCE_DWORD;
slouken@5297
   388
    return 0;
slouken@5297
   389
}
slouken@5297
   390
slouken@5297
   391
static int
slouken@5297
   392
D3D_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   393
{
slouken@5297
   394
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   395
    HRESULT result;
slouken@5297
   396
slouken@5297
   397
    if (data->updateSize) {
slouken@5297
   398
        SDL_Window *window = renderer->window;
slouken@5297
   399
        int w, h;
slouken@5297
   400
slouken@5297
   401
        SDL_GetWindowSize(window, &w, &h);
slouken@5297
   402
        data->pparams.BackBufferWidth = w;
slouken@5297
   403
        data->pparams.BackBufferHeight = h;
slouken@5297
   404
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@5297
   405
            data->pparams.BackBufferFormat =
slouken@5297
   406
                PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window));
slouken@5297
   407
        } else {
slouken@5297
   408
            data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@5297
   409
        }
slouken@5297
   410
        if (D3D_Reset(renderer) < 0) {
slouken@5297
   411
            return -1;
slouken@5297
   412
        }
slouken@5297
   413
        D3D_UpdateViewport(renderer);
slouken@5297
   414
slouken@5297
   415
        data->updateSize = SDL_FALSE;
slouken@5297
   416
    }
slouken@5297
   417
    if (data->beginScene) {
slouken@5297
   418
        result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   419
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   420
            if (D3D_Reset(renderer) < 0) {
slouken@5297
   421
                return -1;
slouken@5297
   422
            }
slouken@5297
   423
            result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   424
        }
slouken@5297
   425
        if (FAILED(result)) {
icculus@7037
   426
            return D3D_SetError("BeginScene()", result);
slouken@5297
   427
        }
slouken@5297
   428
        data->beginScene = SDL_FALSE;
slouken@5297
   429
    }
slouken@5297
   430
    return 0;
slouken@5297
   431
}
slouken@5297
   432
slouken@1895
   433
SDL_Renderer *
slouken@1913
   434
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   435
{
slouken@1895
   436
    SDL_Renderer *renderer;
slouken@1913
   437
    D3D_RenderData *data;
slouken@5154
   438
    SDL_SysWMinfo windowinfo;
slouken@1900
   439
    HRESULT result;
slouken@1900
   440
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   441
    IDirect3DSwapChain9 *chain;
slouken@1925
   442
    D3DCAPS9 caps;
slouken@5156
   443
    Uint32 window_flags;
slouken@5156
   444
    int w, h;
slouken@5156
   445
    SDL_DisplayMode fullscreen_mode;
slouken@5297
   446
    D3DMATRIX matrix;
gabomdq@6320
   447
    int d3dxVersion;
slouken@7191
   448
    char d3dxDLLFile[50];
slouken@1895
   449
slouken@1920
   450
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   451
    if (!renderer) {
slouken@1895
   452
        SDL_OutOfMemory();
slouken@1895
   453
        return NULL;
slouken@1895
   454
    }
slouken@1895
   455
slouken@1920
   456
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   457
    if (!data) {
slouken@5154
   458
        SDL_free(renderer);
slouken@1895
   459
        SDL_OutOfMemory();
slouken@1895
   460
        return NULL;
slouken@1895
   461
    }
slouken@5154
   462
slouken@5154
   463
    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
slouken@5154
   464
    if (data->d3dDLL) {
slouken@5154
   465
        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@1895
   466
slouken@5154
   467
        D3DCreate =
slouken@5154
   468
            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
slouken@5154
   469
                                                            "Direct3DCreate9");
slouken@5154
   470
        if (D3DCreate) {
slouken@5154
   471
            data->d3d = D3DCreate(D3D_SDK_VERSION);
slouken@5154
   472
        }
slouken@5154
   473
        if (!data->d3d) {
slouken@5154
   474
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
   475
            data->d3dDLL = NULL;
slouken@5154
   476
        }
gabomdq@6320
   477
gabomdq@6320
   478
        for (d3dxVersion=50;d3dxVersion>0;d3dxVersion--) {
slouken@6971
   479
            LPTSTR dllName;
slouken@6971
   480
            SDL_snprintf(d3dxDLLFile, sizeof(d3dxDLLFile), "D3DX9_%02d.dll", d3dxVersion);
slouken@6971
   481
            dllName = WIN_UTF8ToString(d3dxDLLFile);
slouken@6971
   482
            data->d3dxDLL = (void *)LoadLibrary(dllName); /* not using SDL_LoadObject() as we want silently fail - no error message */
slouken@6971
   483
            SDL_free(dllName);
gabomdq@6320
   484
            if (data->d3dxDLL) {
gabomdq@6320
   485
                HRESULT (WINAPI *D3DXCreateMatrixStack) (DWORD Flags, LPD3DXMATRIXSTACK*  ppStack);
gabomdq@6320
   486
                D3DXCreateMatrixStack = (HRESULT (WINAPI *) (DWORD, LPD3DXMATRIXSTACK*)) SDL_LoadFunction(data->d3dxDLL, "D3DXCreateMatrixStack");
gabomdq@6320
   487
                if (D3DXCreateMatrixStack) {
gabomdq@6320
   488
                    D3DXCreateMatrixStack(0, &data->matrixStack);
gabomdq@6320
   489
                    break;
gabomdq@6320
   490
                }
gabomdq@6320
   491
            }
gabomdq@6320
   492
        }
gabomdq@6320
   493
gabomdq@6320
   494
        if (!data->matrixStack) {
gabomdq@6320
   495
            if (data->d3dxDLL) SDL_UnloadObject(data->d3dxDLL);
gabomdq@6320
   496
        }
slouken@5154
   497
    }
gabomdq@6320
   498
gabomdq@6320
   499
slouken@7191
   500
gabomdq@6320
   501
    if (!data->d3d || !data->matrixStack) {
slouken@5154
   502
        SDL_free(renderer);
slouken@5154
   503
        SDL_free(data);
slouken@5154
   504
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   505
        return NULL;
slouken@5154
   506
    }
slouken@4569
   507
slouken@5297
   508
    renderer->WindowEvent = D3D_WindowEvent;
slouken@1913
   509
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   510
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   511
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   512
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@6247
   513
    renderer->SetRenderTarget = D3D_SetRenderTarget;
slouken@5297
   514
    renderer->UpdateViewport = D3D_UpdateViewport;
slouken@7141
   515
    renderer->UpdateClipRect = D3D_UpdateClipRect;
slouken@5297
   516
    renderer->RenderClear = D3D_RenderClear;
slouken@3599
   517
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   518
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   519
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   520
    renderer->RenderCopy = D3D_RenderCopy;
gabomdq@6320
   521
    renderer->RenderCopyEx = D3D_RenderCopyEx;
slouken@3427
   522
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@1913
   523
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   524
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   525
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   526
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   527
    renderer->driverdata = data;
slouken@1895
   528
slouken@6246
   529
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   530
slouken@5154
   531
    SDL_VERSION(&windowinfo.version);
slouken@5154
   532
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   533
slouken@5156
   534
    window_flags = SDL_GetWindowFlags(window);
slouken@5156
   535
    SDL_GetWindowSize(window, &w, &h);
slouken@5156
   536
    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
slouken@5156
   537
slouken@1900
   538
    SDL_zero(pparams);
slouken@5154
   539
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@5156
   540
    pparams.BackBufferWidth = w;
slouken@5156
   541
    pparams.BackBufferHeight = h;
slouken@5156
   542
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   543
        pparams.BackBufferFormat =
slouken@5156
   544
            PixelFormatToD3DFMT(fullscreen_mode.format);
slouken@1903
   545
    } else {
slouken@1903
   546
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   547
    }
slouken@5142
   548
    pparams.BackBufferCount = 1;
slouken@5142
   549
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   550
slouken@5156
   551
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@7191
   552
        if ( ( window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) == SDL_WINDOW_FULLSCREEN_DESKTOP )  {
slouken@7191
   553
            pparams.Windowed = TRUE;
slouken@7191
   554
            pparams.FullScreen_RefreshRateInHz = 0;
slouken@7191
   555
        } else {
slouken@1900
   556
        pparams.Windowed = FALSE;
slouken@1903
   557
        pparams.FullScreen_RefreshRateInHz =
slouken@5156
   558
            fullscreen_mode.refresh_rate;
slouken@7191
   559
        }
slouken@1900
   560
    } else {
slouken@1900
   561
        pparams.Windowed = TRUE;
slouken@1903
   562
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   563
    }
slouken@1965
   564
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   565
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   566
    } else {
slouken@1907
   567
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   568
    }
slouken@1900
   569
slouken@5154
   570
    /* FIXME: Which adapter? */
slouken@5154
   571
    data->adapter = D3DADAPTER_DEFAULT;
slouken@5154
   572
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   573
slouken@5154
   574
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   575
                                     D3DDEVTYPE_HAL,
slouken@5154
   576
                                     pparams.hDeviceWindow,
slouken@6115
   577
                                     D3DCREATE_FPU_PRESERVE | ((caps.
slouken@3197
   578
                                      DevCaps &
slouken@3197
   579
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   580
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@6115
   581
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING),
slouken@1900
   582
                                     &pparams, &data->device);
slouken@1900
   583
    if (FAILED(result)) {
slouken@1913
   584
        D3D_DestroyRenderer(renderer);
slouken@1900
   585
        D3D_SetError("CreateDevice()", result);
slouken@1900
   586
        return NULL;
slouken@1900
   587
    }
slouken@1900
   588
    data->beginScene = SDL_TRUE;
slouken@5484
   589
    data->scaleMode = D3DTEXF_FORCE_DWORD;
slouken@1900
   590
slouken@1907
   591
    /* Get presentation parameters to fill info */
slouken@1907
   592
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   593
    if (FAILED(result)) {
slouken@1913
   594
        D3D_DestroyRenderer(renderer);
slouken@1907
   595
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   596
        return NULL;
slouken@1907
   597
    }
slouken@1907
   598
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   599
    if (FAILED(result)) {
slouken@1907
   600
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   601
        D3D_DestroyRenderer(renderer);
slouken@1907
   602
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   603
        return NULL;
slouken@1907
   604
    }
slouken@1907
   605
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   606
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   607
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   608
    }
slouken@1975
   609
    data->pparams = pparams;
slouken@1907
   610
slouken@1925
   611
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   612
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   613
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@6246
   614
    if (caps.NumSimultaneousRTs >= 2) {
slouken@6246
   615
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6246
   616
    }
slouken@1918
   617
slouken@1903
   618
    /* Set up parameters for rendering */
slouken@1904
   619
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   620
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   621
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   622
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   623
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   624
                                    D3DCULL_NONE);
slouken@1904
   625
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   626
    /* Enable color modulation by diffuse color */
slouken@1988
   627
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   628
                                          D3DTOP_MODULATE);
slouken@1988
   629
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   630
                                          D3DTA_TEXTURE);
slouken@1988
   631
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   632
                                          D3DTA_DIFFUSE);
slouken@1988
   633
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   634
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   635
                                          D3DTOP_MODULATE);
slouken@1988
   636
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   637
                                          D3DTA_TEXTURE);
slouken@1988
   638
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   639
                                          D3DTA_DIFFUSE);
slouken@1991
   640
    /* Disable second texture stage, since we're done */
slouken@1991
   641
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   642
                                          D3DTOP_DISABLE);
slouken@1991
   643
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   644
                                          D3DTOP_DISABLE);
slouken@1903
   645
slouken@6232
   646
    /* Store the default render target */
slouken@6232
   647
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
slouken@6232
   648
    data->currentRenderTarget = NULL;
slouken@6232
   649
slouken@5297
   650
    /* Set an identity world and view matrix */
slouken@5297
   651
    matrix.m[0][0] = 1.0f;
slouken@5297
   652
    matrix.m[0][1] = 0.0f;
slouken@5297
   653
    matrix.m[0][2] = 0.0f;
slouken@5297
   654
    matrix.m[0][3] = 0.0f;
slouken@5297
   655
    matrix.m[1][0] = 0.0f;
slouken@5297
   656
    matrix.m[1][1] = 1.0f;
slouken@5297
   657
    matrix.m[1][2] = 0.0f;
slouken@5297
   658
    matrix.m[1][3] = 0.0f;
slouken@5297
   659
    matrix.m[2][0] = 0.0f;
slouken@5297
   660
    matrix.m[2][1] = 0.0f;
slouken@5297
   661
    matrix.m[2][2] = 1.0f;
slouken@5297
   662
    matrix.m[2][3] = 0.0f;
slouken@5297
   663
    matrix.m[3][0] = 0.0f;
slouken@5297
   664
    matrix.m[3][1] = 0.0f;
slouken@5297
   665
    matrix.m[3][2] = 0.0f;
slouken@5297
   666
    matrix.m[3][3] = 1.0f;
slouken@5297
   667
    IDirect3DDevice9_SetTransform(data->device, D3DTS_WORLD, &matrix);
slouken@5297
   668
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, &matrix);
slouken@5297
   669
slouken@1895
   670
    return renderer;
slouken@1895
   671
}
slouken@1895
   672
slouken@5297
   673
static void
slouken@5297
   674
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1975
   675
{
slouken@1975
   676
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   677
slouken@5297
   678
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   679
        data->updateSize = SDL_TRUE;
slouken@1975
   680
    }
slouken@1975
   681
}
slouken@1975
   682
slouken@5484
   683
static D3DTEXTUREFILTERTYPE
slouken@5484
   684
GetScaleQuality(void)
slouken@5484
   685
{
slouken@5484
   686
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   687
slouken@5484
   688
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   689
        return D3DTEXF_POINT;
slouken@5484
   690
    } else if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) {
slouken@5484
   691
        return D3DTEXF_LINEAR;
slouken@5484
   692
    } else {
slouken@5484
   693
        return D3DTEXF_ANISOTROPIC;
slouken@5484
   694
    }
slouken@5484
   695
}
slouken@5484
   696
slouken@1975
   697
static int
slouken@1913
   698
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   699
{
icculus@6404
   700
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   701
    D3D_TextureData *data;
slouken@5173
   702
    D3DPOOL pool;
slouken@5173
   703
    DWORD usage;
slouken@1903
   704
    HRESULT result;
slouken@1895
   705
slouken@1920
   706
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   707
    if (!data) {
icculus@7037
   708
        return SDL_OutOfMemory();
slouken@1895
   709
    }
slouken@5484
   710
    data->scaleMode = GetScaleQuality();
slouken@1895
   711
slouken@1895
   712
    texture->driverdata = data;
slouken@1895
   713
slouken@5173
   714
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   715
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   716
        pool = D3DPOOL_DEFAULT;
slouken@5173
   717
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   718
    } else
slouken@5173
   719
#endif
slouken@6232
   720
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6246
   721
        /* D3DPOOL_MANAGED does not work with D3DUSAGE_RENDERTARGET */
slouken@6246
   722
        pool = D3DPOOL_DEFAULT;
slouken@6232
   723
        usage = D3DUSAGE_RENDERTARGET;
slouken@6246
   724
    } else {
slouken@5173
   725
        pool = D3DPOOL_MANAGED;
slouken@5173
   726
        usage = 0;
slouken@5173
   727
    }
slouken@2973
   728
slouken@1903
   729
    result =
slouken@1903
   730
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   731
                                       texture->h, 1, usage,
slouken@5173
   732
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   733
                                       pool, &data->texture, NULL);
slouken@1903
   734
    if (FAILED(result)) {
icculus@7037
   735
        return D3D_SetError("CreateTexture()", result);
slouken@1903
   736
    }
slouken@1895
   737
slouken@1903
   738
    return 0;
slouken@1895
   739
}
slouken@1895
   740
slouken@1895
   741
static int
slouken@1913
   742
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   743
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   744
{
slouken@1913
   745
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   746
    RECT d3drect;
slouken@5156
   747
    D3DLOCKED_RECT locked;
slouken@5156
   748
    const Uint8 *src;
slouken@5156
   749
    Uint8 *dst;
slouken@5156
   750
    int row, length;
slouken@5156
   751
    HRESULT result;
slouken@1903
   752
slouken@5173
   753
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   754
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
   755
        rect->x == 0 && rect->y == 0 &&
slouken@5173
   756
        rect->w == texture->w && rect->h == texture->h) {
slouken@5194
   757
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@5173
   758
    } else
slouken@5173
   759
#endif
slouken@5173
   760
    {
slouken@5173
   761
        d3drect.left = rect->x;
slouken@5173
   762
        d3drect.right = rect->x + rect->w;
slouken@5173
   763
        d3drect.top = rect->y;
slouken@5173
   764
        d3drect.bottom = rect->y + rect->h;
slouken@5194
   765
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   766
    }
slouken@1903
   767
slouken@5156
   768
    if (FAILED(result)) {
icculus@7037
   769
        return D3D_SetError("LockRect()", result);
slouken@5156
   770
    }
slouken@2783
   771
slouken@5156
   772
    src = pixels;
slouken@5156
   773
    dst = locked.pBits;
slouken@5156
   774
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5173
   775
    if (length == pitch && length == locked.Pitch) {
slouken@5173
   776
        SDL_memcpy(dst, src, length*rect->h);
slouken@5173
   777
    } else {
slouken@5173
   778
        for (row = 0; row < rect->h; ++row) {
slouken@5173
   779
            SDL_memcpy(dst, src, length);
slouken@5173
   780
            src += pitch;
slouken@5173
   781
            dst += locked.Pitch;
slouken@5173
   782
        }
slouken@5156
   783
    }
slouken@5156
   784
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   785
slouken@5156
   786
    return 0;
slouken@2783
   787
}
slouken@1895
   788
slouken@1895
   789
static int
slouken@1913
   790
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   791
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   792
{
slouken@1913
   793
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   794
    RECT d3drect;
slouken@5156
   795
    D3DLOCKED_RECT locked;
slouken@5156
   796
    HRESULT result;
slouken@1895
   797
slouken@5156
   798
    d3drect.left = rect->x;
slouken@5156
   799
    d3drect.right = rect->x + rect->w;
slouken@5156
   800
    d3drect.top = rect->y;
slouken@5156
   801
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   802
slouken@5156
   803
    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   804
    if (FAILED(result)) {
icculus@7037
   805
        return D3D_SetError("LockRect()", result);
slouken@1903
   806
    }
slouken@5156
   807
    *pixels = locked.pBits;
slouken@5156
   808
    *pitch = locked.Pitch;
slouken@5156
   809
    return 0;
slouken@1895
   810
}
slouken@1895
   811
slouken@1895
   812
static void
slouken@1913
   813
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   814
{
slouken@1913
   815
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   816
slouken@5156
   817
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   818
}
slouken@1895
   819
slouken@5297
   820
static int
slouken@7141
   821
D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@7141
   822
{
slouken@7141
   823
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7141
   824
    D3D_TextureData *texturedata;
slouken@7141
   825
    HRESULT result;
slouken@7141
   826
slouken@7141
   827
    D3D_ActivateRenderer(renderer);
slouken@7141
   828
slouken@7141
   829
    /* Release the previous render target if it wasn't the default one */
slouken@7141
   830
    if (data->currentRenderTarget != NULL) {
slouken@7141
   831
        IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@7141
   832
        data->currentRenderTarget = NULL;
slouken@7141
   833
    }
slouken@7141
   834
slouken@7141
   835
    if (texture == NULL) {
slouken@7141
   836
        IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget);
slouken@7141
   837
        return 0;
slouken@7141
   838
    }
slouken@7141
   839
slouken@7141
   840
    texturedata = (D3D_TextureData *) texture->driverdata;
slouken@7141
   841
    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget);
slouken@7141
   842
    if(FAILED(result)) {
slouken@7141
   843
        return D3D_SetError("GetSurfaceLevel()", result);
slouken@7141
   844
    }
slouken@7141
   845
    result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget);
slouken@7141
   846
    if(FAILED(result)) {
slouken@7141
   847
        return D3D_SetError("SetRenderTarget()", result);
slouken@7141
   848
    }
slouken@7141
   849
slouken@7141
   850
    return 0;
slouken@7141
   851
}
slouken@7141
   852
slouken@7141
   853
static int
slouken@5297
   854
D3D_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   855
{
slouken@5224
   856
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   857
    D3DVIEWPORT9 viewport;
slouken@5297
   858
    D3DMATRIX matrix;
slouken@5224
   859
slouken@5297
   860
    /* Set the viewport */
slouken@5297
   861
    viewport.X = renderer->viewport.x;
slouken@5297
   862
    viewport.Y = renderer->viewport.y;
slouken@5297
   863
    viewport.Width = renderer->viewport.w;
slouken@5297
   864
    viewport.Height = renderer->viewport.h;
slouken@5297
   865
    viewport.MinZ = 0.0f;
slouken@5297
   866
    viewport.MaxZ = 1.0f;
slouken@5297
   867
    IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5297
   868
slouken@5297
   869
    /* Set an orthographic projection matrix */
slouken@7239
   870
    if (renderer->viewport.w && renderer->viewport.h) {
slouken@7239
   871
        matrix.m[0][0] = 2.0f / renderer->viewport.w;
slouken@7239
   872
        matrix.m[0][1] = 0.0f;
slouken@7239
   873
        matrix.m[0][2] = 0.0f;
slouken@7239
   874
        matrix.m[0][3] = 0.0f;
slouken@7239
   875
        matrix.m[1][0] = 0.0f;
slouken@7239
   876
        matrix.m[1][1] = -2.0f / renderer->viewport.h;
slouken@7239
   877
        matrix.m[1][2] = 0.0f;
slouken@7239
   878
        matrix.m[1][3] = 0.0f;
slouken@7239
   879
        matrix.m[2][0] = 0.0f;
slouken@7239
   880
        matrix.m[2][1] = 0.0f;
slouken@7239
   881
        matrix.m[2][2] = 1.0f;
slouken@7239
   882
        matrix.m[2][3] = 0.0f;
slouken@7239
   883
        matrix.m[3][0] = -1.0f;
slouken@7239
   884
        matrix.m[3][1] = 1.0f;
slouken@7239
   885
        matrix.m[3][2] = 0.0f;
slouken@7239
   886
        matrix.m[3][3] = 1.0f;
slouken@7239
   887
        IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &matrix);
slouken@7239
   888
    }
slouken@5297
   889
slouken@5297
   890
    return 0;
slouken@5297
   891
}
slouken@5224
   892
slouken@5297
   893
static int
slouken@7141
   894
D3D_UpdateClipRect(SDL_Renderer * renderer)
slouken@6246
   895
{
slouken@7141
   896
    const SDL_Rect *rect = &renderer->clip_rect;
slouken@6246
   897
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7141
   898
    RECT r;
slouken@6246
   899
    HRESULT result;
slouken@6246
   900
slouken@7141
   901
    if (!SDL_RectEmpty(rect)) {
slouken@7141
   902
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, TRUE);
slouken@7141
   903
        r.left = rect->x;
slouken@7141
   904
        r.top = rect->y;
slouken@7141
   905
        r.right = rect->w + rect->w;
slouken@7141
   906
        r.bottom = rect->y + rect->h;
slouken@6246
   907
slouken@7141
   908
        result = IDirect3DDevice9_SetScissorRect(data->device, &r);
slouken@7141
   909
        if (result != D3D_OK) {
slouken@7141
   910
            D3D_SetError("SetScissor()", result);
slouken@7141
   911
            return -1;
slouken@7141
   912
        }
slouken@7141
   913
    } else {
slouken@7141
   914
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, FALSE);
slouken@6246
   915
    }
slouken@6246
   916
    return 0;
slouken@6246
   917
}
slouken@6246
   918
slouken@6246
   919
static int
slouken@5297
   920
D3D_RenderClear(SDL_Renderer * renderer)
slouken@5297
   921
{
slouken@5297
   922
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   923
    DWORD color;
slouken@5297
   924
    HRESULT result;
slouken@5297
   925
slouken@5297
   926
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   927
        return -1;
slouken@5224
   928
    }
slouken@5297
   929
slouken@5297
   930
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@5297
   931
slouken@5299
   932
    /* Don't reset the viewport if we don't have to! */
slouken@5299
   933
    if (!renderer->viewport.x && !renderer->viewport.y &&
slouken@5299
   934
        renderer->viewport.w == data->pparams.BackBufferWidth &&
slouken@5299
   935
        renderer->viewport.h == data->pparams.BackBufferHeight) {
slouken@5299
   936
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   937
    } else {
slouken@5299
   938
        D3DVIEWPORT9 viewport;
slouken@5297
   939
slouken@5299
   940
        /* Clear is defined to clear the entire render target */
slouken@5299
   941
        viewport.X = 0;
slouken@5299
   942
        viewport.Y = 0;
slouken@5299
   943
        viewport.Width = data->pparams.BackBufferWidth;
slouken@5299
   944
        viewport.Height = data->pparams.BackBufferHeight;
slouken@5299
   945
        viewport.MinZ = 0.0f;
slouken@5299
   946
        viewport.MaxZ = 1.0f;
slouken@5299
   947
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   948
slouken@5299
   949
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   950
slouken@5299
   951
        /* Reset the viewport */
slouken@5299
   952
        viewport.X = renderer->viewport.x;
slouken@5299
   953
        viewport.Y = renderer->viewport.y;
slouken@5299
   954
        viewport.Width = renderer->viewport.w;
slouken@5299
   955
        viewport.Height = renderer->viewport.h;
slouken@5299
   956
        viewport.MinZ = 0.0f;
slouken@5299
   957
        viewport.MaxZ = 1.0f;
slouken@5299
   958
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   959
    }
slouken@5297
   960
slouken@5297
   961
    if (FAILED(result)) {
icculus@7037
   962
        return D3D_SetError("Clear()", result);
slouken@5297
   963
    }
slouken@5297
   964
    return 0;
slouken@5224
   965
}
slouken@5224
   966
slouken@5224
   967
static void
slouken@2933
   968
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   969
{
slouken@2932
   970
    switch (blendMode) {
slouken@2932
   971
    case SDL_BLENDMODE_NONE:
slouken@2932
   972
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   973
                                        FALSE);
slouken@2932
   974
        break;
slouken@2932
   975
    case SDL_BLENDMODE_BLEND:
slouken@2932
   976
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   977
                                        TRUE);
slouken@2932
   978
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   979
                                        D3DBLEND_SRCALPHA);
slouken@2932
   980
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   981
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   982
        break;
slouken@2932
   983
    case SDL_BLENDMODE_ADD:
slouken@2932
   984
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   985
                                        TRUE);
slouken@2932
   986
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   987
                                        D3DBLEND_SRCALPHA);
slouken@2932
   988
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   989
                                        D3DBLEND_ONE);
slouken@2932
   990
        break;
slouken@5184
   991
    case SDL_BLENDMODE_MOD:
slouken@5184
   992
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@5184
   993
                                        TRUE);
slouken@5184
   994
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@5184
   995
                                        D3DBLEND_ZERO);
slouken@5184
   996
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@5184
   997
                                        D3DBLEND_SRCCOLOR);
slouken@5184
   998
        break;
slouken@2932
   999
    }
slouken@2932
  1000
}
slouken@2932
  1001
slouken@1895
  1002
static int
slouken@6528
  1003
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1004
                     int count)
slouken@1895
  1005
{
slouken@1913
  1006
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1007
    DWORD color;
slouken@3536
  1008
    Vertex *vertices;
slouken@3536
  1009
    int i;
slouken@2932
  1010
    HRESULT result;
slouken@2932
  1011
slouken@5297
  1012
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1013
        return -1;
slouken@2932
  1014
    }
slouken@2932
  1015
slouken@3536
  1016
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@3536
  1017
slouken@3536
  1018
    result =
slouken@3536
  1019
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@3536
  1020
                                    (IDirect3DBaseTexture9 *) 0);
slouken@3536
  1021
    if (FAILED(result)) {
icculus@7037
  1022
        return D3D_SetError("SetTexture()", result);
slouken@3536
  1023
    }
slouken@3536
  1024
slouken@2932
  1025
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@2932
  1026
slouken@3536
  1027
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1028
    for (i = 0; i < count; ++i) {
slouken@6528
  1029
        vertices[i].x = points[i].x;
slouken@6528
  1030
        vertices[i].y = points[i].y;
slouken@3536
  1031
        vertices[i].z = 0.0f;
slouken@3536
  1032
        vertices[i].color = color;
slouken@3536
  1033
        vertices[i].u = 0.0f;
slouken@3536
  1034
        vertices[i].v = 0.0f;
slouken@3536
  1035
    }
slouken@3536
  1036
    result =
slouken@3536
  1037
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@3536
  1038
                                         vertices, sizeof(*vertices));
slouken@3536
  1039
    SDL_stack_free(vertices);
slouken@3536
  1040
    if (FAILED(result)) {
icculus@7037
  1041
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1042
    }
slouken@3536
  1043
    return 0;
slouken@3536
  1044
}
slouken@3536
  1045
slouken@3536
  1046
static int
slouken@6528
  1047
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1048
                    int count)
slouken@3536
  1049
{
slouken@3536
  1050
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
  1051
    DWORD color;
slouken@3536
  1052
    Vertex *vertices;
slouken@3536
  1053
    int i;
slouken@3536
  1054
    HRESULT result;
slouken@3536
  1055
slouken@5297
  1056
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1057
        return -1;
slouken@3536
  1058
    }
slouken@2932
  1059
slouken@2932
  1060
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1061
slouken@2933
  1062
    result =
slouken@2933
  1063
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1064
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1065
    if (FAILED(result)) {
icculus@7037
  1066
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1067
    }
slouken@3536
  1068
slouken@3536
  1069
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1070
slouken@3536
  1071
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1072
    for (i = 0; i < count; ++i) {
slouken@6528
  1073
        vertices[i].x = points[i].x;
slouken@6528
  1074
        vertices[i].y = points[i].y;
slouken@3536
  1075
        vertices[i].z = 0.0f;
slouken@3536
  1076
        vertices[i].color = color;
slouken@3536
  1077
        vertices[i].u = 0.0f;
slouken@3536
  1078
        vertices[i].v = 0.0f;
slouken@3536
  1079
    }
slouken@2932
  1080
    result =
slouken@3551
  1081
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
  1082
                                         vertices, sizeof(*vertices));
slouken@3551
  1083
slouken@3551
  1084
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
  1085
       so we need to close the endpoint of the line */
slouken@6076
  1086
    if (count == 2 ||
slouken@6076
  1087
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@6528
  1088
        vertices[0].x = points[count-1].x;
slouken@6528
  1089
        vertices[0].y = points[count-1].y;
slouken@3551
  1090
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
  1091
    }
slouken@3551
  1092
slouken@3536
  1093
    SDL_stack_free(vertices);
slouken@2932
  1094
    if (FAILED(result)) {
icculus@7037
  1095
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1096
    }
slouken@2932
  1097
    return 0;
slouken@2932
  1098
}
slouken@2932
  1099
slouken@2932
  1100
static int
slouken@6528
  1101
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
slouken@3599
  1102
                    int count)
slouken@2932
  1103
{
slouken@2932
  1104
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1105
    DWORD color;
slouken@3536
  1106
    int i;
slouken@3536
  1107
    float minx, miny, maxx, maxy;
slouken@3536
  1108
    Vertex vertices[4];
slouken@1900
  1109
    HRESULT result;
slouken@1900
  1110
slouken@5297
  1111
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1112
        return -1;
slouken@1900
  1113
    }
slouken@1895
  1114
slouken@2932
  1115
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1116
slouken@2933
  1117
    result =
slouken@2933
  1118
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1119
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1120
    if (FAILED(result)) {
icculus@7037
  1121
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1122
    }
slouken@2932
  1123
slouken@2932
  1124
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@2932
  1125
slouken@3536
  1126
    for (i = 0; i < count; ++i) {
slouken@6528
  1127
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1128
slouken@6528
  1129
        minx = rect->x;
slouken@6528
  1130
        miny = rect->y;
slouken@6528
  1131
        maxx = rect->x + rect->w;
slouken@6528
  1132
        maxy = rect->y + rect->h;
slouken@2932
  1133
slouken@3536
  1134
        vertices[0].x = minx;
slouken@3536
  1135
        vertices[0].y = miny;
slouken@3536
  1136
        vertices[0].z = 0.0f;
slouken@3536
  1137
        vertices[0].color = color;
slouken@3536
  1138
        vertices[0].u = 0.0f;
slouken@3536
  1139
        vertices[0].v = 0.0f;
slouken@2932
  1140
slouken@3536
  1141
        vertices[1].x = maxx;
slouken@3536
  1142
        vertices[1].y = miny;
slouken@3536
  1143
        vertices[1].z = 0.0f;
slouken@3536
  1144
        vertices[1].color = color;
slouken@3536
  1145
        vertices[1].u = 0.0f;
slouken@3536
  1146
        vertices[1].v = 0.0f;
slouken@2932
  1147
slouken@3536
  1148
        vertices[2].x = maxx;
slouken@3536
  1149
        vertices[2].y = maxy;
slouken@3536
  1150
        vertices[2].z = 0.0f;
slouken@3536
  1151
        vertices[2].color = color;
slouken@3536
  1152
        vertices[2].u = 0.0f;
slouken@3536
  1153
        vertices[2].v = 0.0f;
slouken@2932
  1154
slouken@3536
  1155
        vertices[3].x = minx;
slouken@3536
  1156
        vertices[3].y = maxy;
slouken@3536
  1157
        vertices[3].z = 0.0f;
slouken@3536
  1158
        vertices[3].color = color;
slouken@3536
  1159
        vertices[3].u = 0.0f;
slouken@3536
  1160
        vertices[3].v = 0.0f;
slouken@3536
  1161
slouken@3536
  1162
        result =
slouken@3536
  1163
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
  1164
                                             2, vertices, sizeof(*vertices));
slouken@3536
  1165
        if (FAILED(result)) {
icculus@7037
  1166
            return D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1167
        }
slouken@1900
  1168
    }
slouken@1895
  1169
    return 0;
slouken@1895
  1170
}
slouken@1895
  1171
slouken@1895
  1172
static int
slouken@1913
  1173
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1174
               const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@1895
  1175
{
slouken@1913
  1176
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1177
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
  1178
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
  1179
    float minx, miny, maxx, maxy;
slouken@1904
  1180
    float minu, maxu, minv, maxv;
slouken@1987
  1181
    DWORD color;
slouken@1903
  1182
    Vertex vertices[4];
slouken@1903
  1183
    HRESULT result;
slouken@1895
  1184
slouken@5297
  1185
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1186
        return -1;
slouken@1900
  1187
    }
slouken@1903
  1188
slouken@6528
  1189
    minx = dstrect->x - 0.5f;
slouken@6528
  1190
    miny = dstrect->y - 0.5f;
slouken@6528
  1191
    maxx = dstrect->x + dstrect->w - 0.5f;
slouken@6528
  1192
    maxy = dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1193
slouken@1904
  1194
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1195
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1196
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1197
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1198
slouken@1987
  1199
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1200
slouken@1903
  1201
    vertices[0].x = minx;
slouken@1903
  1202
    vertices[0].y = miny;
slouken@1903
  1203
    vertices[0].z = 0.0f;
slouken@1987
  1204
    vertices[0].color = color;
slouken@1904
  1205
    vertices[0].u = minu;
slouken@1904
  1206
    vertices[0].v = minv;
slouken@1904
  1207
slouken@1903
  1208
    vertices[1].x = maxx;
slouken@1903
  1209
    vertices[1].y = miny;
slouken@1903
  1210
    vertices[1].z = 0.0f;
slouken@1987
  1211
    vertices[1].color = color;
slouken@1904
  1212
    vertices[1].u = maxu;
slouken@1904
  1213
    vertices[1].v = minv;
slouken@1904
  1214
slouken@1903
  1215
    vertices[2].x = maxx;
slouken@1903
  1216
    vertices[2].y = maxy;
slouken@1903
  1217
    vertices[2].z = 0.0f;
slouken@1987
  1218
    vertices[2].color = color;
slouken@1904
  1219
    vertices[2].u = maxu;
slouken@1904
  1220
    vertices[2].v = maxv;
slouken@1904
  1221
slouken@1903
  1222
    vertices[3].x = minx;
slouken@1903
  1223
    vertices[3].y = maxy;
slouken@1903
  1224
    vertices[3].z = 0.0f;
slouken@1987
  1225
    vertices[3].color = color;
slouken@1904
  1226
    vertices[3].u = minu;
slouken@1904
  1227
    vertices[3].v = maxv;
slouken@1903
  1228
slouken@2932
  1229
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1230
slouken@5484
  1231
    if (texturedata->scaleMode != data->scaleMode) {
slouken@5484
  1232
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@5484
  1233
                                         texturedata->scaleMode);
slouken@5484
  1234
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@5484
  1235
                                         texturedata->scaleMode);
slouken@5484
  1236
        data->scaleMode = texturedata->scaleMode;
slouken@5484
  1237
    }
slouken@1917
  1238
slouken@1903
  1239
    result =
slouken@2735
  1240
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1241
                                    texturedata->texture);
slouken@1903
  1242
    if (FAILED(result)) {
icculus@7037
  1243
        return D3D_SetError("SetTexture()", result);
slouken@1903
  1244
    }
slouken@3556
  1245
    if (shader) {
slouken@3556
  1246
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
  1247
        if (FAILED(result)) {
icculus@7037
  1248
            return D3D_SetError("SetShader()", result);
slouken@3556
  1249
        }
slouken@3556
  1250
    }
slouken@1903
  1251
    result =
slouken@1903
  1252
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1253
                                         vertices, sizeof(*vertices));
slouken@1903
  1254
    if (FAILED(result)) {
icculus@7037
  1255
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1256
    }
slouken@3556
  1257
    if (shader) {
slouken@3556
  1258
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
  1259
        if (FAILED(result)) {
icculus@7037
  1260
            return D3D_SetError("SetShader()", result);
slouken@3556
  1261
        }
slouken@3556
  1262
    }
slouken@1895
  1263
    return 0;
slouken@1895
  1264
}
slouken@1895
  1265
gabomdq@6320
  1266
gabomdq@6320
  1267
static int
gabomdq@6320
  1268
D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1269
               const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1270
               const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
gabomdq@6320
  1271
{
gabomdq@6320
  1272
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
gabomdq@6320
  1273
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
gabomdq@6320
  1274
    LPDIRECT3DPIXELSHADER9 shader = NULL;
gabomdq@6320
  1275
    float minx, miny, maxx, maxy;
gabomdq@6320
  1276
    float minu, maxu, minv, maxv;
gabomdq@6320
  1277
    float centerx, centery;
gabomdq@6320
  1278
    DWORD color;
gabomdq@6320
  1279
    Vertex vertices[4];
gabomdq@6320
  1280
    HRESULT result;
gabomdq@6320
  1281
gabomdq@6320
  1282
    if (D3D_ActivateRenderer(renderer) < 0) {
gabomdq@6320
  1283
        return -1;
gabomdq@6320
  1284
    }
gabomdq@6320
  1285
slouken@6528
  1286
    centerx = center->x;
slouken@6528
  1287
    centery = center->y;
gabomdq@6320
  1288
gabomdq@6320
  1289
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@6528
  1290
        minx = dstrect->w - centerx - 0.5f;
slouken@6528
  1291
        maxx = -centerx - 0.5f;
gabomdq@6320
  1292
    }
gabomdq@6320
  1293
    else {
slouken@6528
  1294
        minx = -centerx - 0.5f;
slouken@6528
  1295
        maxx = dstrect->w - centerx - 0.5f;
gabomdq@6320
  1296
    }
gabomdq@6320
  1297
gabomdq@6320
  1298
    if (flip & SDL_FLIP_VERTICAL) {
slouken@6528
  1299
        miny = dstrect->h - centery - 0.5f;
slouken@6528
  1300
        maxy = -centery - 0.5f;
gabomdq@6320
  1301
    }
gabomdq@6320
  1302
    else {
slouken@6528
  1303
        miny = -centery - 0.5f;
slouken@6528
  1304
        maxy = dstrect->h - centery - 0.5f;
gabomdq@6320
  1305
    }
gabomdq@6320
  1306
gabomdq@6320
  1307
    minu = (float) srcrect->x / texture->w;
gabomdq@6320
  1308
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1309
    minv = (float) srcrect->y / texture->h;
gabomdq@6320
  1310
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1311
gabomdq@6320
  1312
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
gabomdq@6320
  1313
gabomdq@6320
  1314
    vertices[0].x = minx;
gabomdq@6320
  1315
    vertices[0].y = miny;
gabomdq@6320
  1316
    vertices[0].z = 0.0f;
gabomdq@6320
  1317
    vertices[0].color = color;
gabomdq@6320
  1318
    vertices[0].u = minu;
gabomdq@6320
  1319
    vertices[0].v = minv;
gabomdq@6320
  1320
gabomdq@6320
  1321
    vertices[1].x = maxx;
gabomdq@6320
  1322
    vertices[1].y = miny;
gabomdq@6320
  1323
    vertices[1].z = 0.0f;
gabomdq@6320
  1324
    vertices[1].color = color;
gabomdq@6320
  1325
    vertices[1].u = maxu;
gabomdq@6320
  1326
    vertices[1].v = minv;
gabomdq@6320
  1327
gabomdq@6320
  1328
    vertices[2].x = maxx;
gabomdq@6320
  1329
    vertices[2].y = maxy;
gabomdq@6320
  1330
    vertices[2].z = 0.0f;
gabomdq@6320
  1331
    vertices[2].color = color;
gabomdq@6320
  1332
    vertices[2].u = maxu;
gabomdq@6320
  1333
    vertices[2].v = maxv;
gabomdq@6320
  1334
gabomdq@6320
  1335
    vertices[3].x = minx;
gabomdq@6320
  1336
    vertices[3].y = maxy;
gabomdq@6320
  1337
    vertices[3].z = 0.0f;
gabomdq@6320
  1338
    vertices[3].color = color;
gabomdq@6320
  1339
    vertices[3].u = minu;
gabomdq@6320
  1340
    vertices[3].v = maxv;
gabomdq@6320
  1341
gabomdq@6320
  1342
    D3D_SetBlendMode(data, texture->blendMode);
gabomdq@6320
  1343
slouken@7191
  1344
    /* Rotate and translate */
gabomdq@6320
  1345
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1346
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
slouken@6332
  1347
    ID3DXMatrixStack_RotateYawPitchRoll(data->matrixStack, 0.0, 0.0, (float)(M_PI * (float) angle / 180.0f));
gabomdq@6320
  1348
    ID3DXMatrixStack_Translate(data->matrixStack, (float)dstrect->x + centerx, (float)dstrect->y + centery, (float)0.0);
gabomdq@6320
  1349
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1350
gabomdq@6320
  1351
    if (texturedata->scaleMode != data->scaleMode) {
gabomdq@6320
  1352
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
gabomdq@6320
  1353
                                         texturedata->scaleMode);
gabomdq@6320
  1354
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
gabomdq@6320
  1355
                                         texturedata->scaleMode);
gabomdq@6320
  1356
        data->scaleMode = texturedata->scaleMode;
gabomdq@6320
  1357
    }
gabomdq@6320
  1358
gabomdq@6320
  1359
    result =
gabomdq@6320
  1360
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
gabomdq@6320
  1361
                                    texturedata->texture);
gabomdq@6320
  1362
    if (FAILED(result)) {
icculus@7037
  1363
        return D3D_SetError("SetTexture()", result);
gabomdq@6320
  1364
    }
gabomdq@6320
  1365
    if (shader) {
gabomdq@6320
  1366
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
gabomdq@6320
  1367
        if (FAILED(result)) {
icculus@7037
  1368
            return D3D_SetError("SetShader()", result);
gabomdq@6320
  1369
        }
gabomdq@6320
  1370
    }
gabomdq@6320
  1371
    result =
gabomdq@6320
  1372
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
gabomdq@6320
  1373
                                         vertices, sizeof(*vertices));
gabomdq@6320
  1374
    if (FAILED(result)) {
icculus@7037
  1375
        return D3D_SetError("DrawPrimitiveUP()", result);
gabomdq@6320
  1376
    }
gabomdq@6320
  1377
    if (shader) {
gabomdq@6320
  1378
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
gabomdq@6320
  1379
        if (FAILED(result)) {
icculus@7037
  1380
            return D3D_SetError("SetShader()", result);
gabomdq@6320
  1381
        }
gabomdq@6320
  1382
    }
gabomdq@6320
  1383
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1384
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1385
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
gabomdq@6320
  1386
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1387
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1388
    return 0;
gabomdq@6320
  1389
}
gabomdq@6320
  1390
slouken@3427
  1391
static int
slouken@3427
  1392
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1393
                     Uint32 format, void * pixels, int pitch)
slouken@3427
  1394
{
slouken@3549
  1395
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3549
  1396
    D3DSURFACE_DESC desc;
slouken@3427
  1397
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
  1398
    LPDIRECT3DSURFACE9 surface;
slouken@3549
  1399
    RECT d3drect;
slouken@3549
  1400
    D3DLOCKED_RECT locked;
slouken@3549
  1401
    HRESULT result;
slouken@3549
  1402
slouken@3549
  1403
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
  1404
    if (FAILED(result)) {
icculus@7037
  1405
        return D3D_SetError("GetBackBuffer()", result);
slouken@3549
  1406
    }
slouken@3427
  1407
slouken@3549
  1408
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
  1409
    if (FAILED(result)) {
slouken@3549
  1410
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1411
        return D3D_SetError("GetDesc()", result);
slouken@3549
  1412
    }
slouken@3549
  1413
slouken@3549
  1414
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
  1415
    if (FAILED(result)) {
slouken@3549
  1416
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1417
        return D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
  1418
    }
slouken@3427
  1419
slouken@3549
  1420
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
  1421
    if (FAILED(result)) {
slouken@3549
  1422
        IDirect3DSurface9_Release(surface);
slouken@3549
  1423
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1424
        return D3D_SetError("GetRenderTargetData()", result);
slouken@3549
  1425
    }
slouken@3427
  1426
slouken@3549
  1427
    d3drect.left = rect->x;
slouken@3549
  1428
    d3drect.right = rect->x + rect->w;
slouken@3549
  1429
    d3drect.top = rect->y;
slouken@3549
  1430
    d3drect.bottom = rect->y + rect->h;
slouken@3427
  1431
slouken@3549
  1432
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
  1433
    if (FAILED(result)) {
slouken@3549
  1434
        IDirect3DSurface9_Release(surface);
slouken@3549
  1435
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1436
        return D3D_SetError("LockRect()", result);
slouken@3549
  1437
    }
slouken@3427
  1438
slouken@3549
  1439
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
  1440
                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
slouken@3549
  1441
                      format, pixels, pitch);
slouken@3549
  1442
slouken@3549
  1443
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
  1444
slouken@3549
  1445
    IDirect3DSurface9_Release(surface);
slouken@3549
  1446
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1447
slouken@3549
  1448
    return 0;
slouken@3427
  1449
}
slouken@3427
  1450
slouken@1895
  1451
static void
slouken@1913
  1452
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1453
{
slouken@1913
  1454
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1455
    HRESULT result;
slouken@1900
  1456
slouken@1900
  1457
    if (!data->beginScene) {
slouken@1900
  1458
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1459
        data->beginScene = SDL_TRUE;
slouken@1900
  1460
    }
slouken@1900
  1461
slouken@1975
  1462
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1463
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1464
        /* We'll reset later */
slouken@1975
  1465
        return;
slouken@1975
  1466
    }
slouken@1975
  1467
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1468
        D3D_Reset(renderer);
slouken@1975
  1469
    }
slouken@1900
  1470
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1471
    if (FAILED(result)) {
slouken@1900
  1472
        D3D_SetError("Present()", result);
slouken@1900
  1473
    }
slouken@1895
  1474
}
slouken@1895
  1475
slouken@1895
  1476
static void
slouken@1913
  1477
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1478
{
slouken@1913
  1479
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1480
slouken@1895
  1481
    if (!data) {
slouken@1895
  1482
        return;
slouken@1895
  1483
    }
slouken@1903
  1484
    if (data->texture) {
slouken@1903
  1485
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1486
    }
slouken@1895
  1487
    SDL_free(data);
slouken@1895
  1488
    texture->driverdata = NULL;
slouken@1895
  1489
}
slouken@1895
  1490
slouken@1975
  1491
static void
slouken@1913
  1492
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1493
{
slouken@1913
  1494
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1495
slouken@1895
  1496
    if (data) {
slouken@7191
  1497
        /* Release the render target */
slouken@6895
  1498
        if (data->defaultRenderTarget) {
slouken@6895
  1499
            IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6895
  1500
            data->defaultRenderTarget = NULL;
slouken@6895
  1501
        }
slouken@6232
  1502
        if (data->currentRenderTarget != NULL) {
slouken@6232
  1503
            IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6232
  1504
            data->currentRenderTarget = NULL;
slouken@6232
  1505
        }
slouken@7191
  1506
slouken@1900
  1507
        if (data->device) {
slouken@1900
  1508
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1509
        }
slouken@5154
  1510
        if (data->d3d) {
slouken@5154
  1511
            IDirect3D9_Release(data->d3d);
slouken@7183
  1512
            ID3DXMatrixStack_Release(data->matrixStack);
slouken@5154
  1513
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1514
        }
slouken@1895
  1515
        SDL_free(data);
slouken@1895
  1516
    }
slouken@1895
  1517
    SDL_free(renderer);
slouken@1895
  1518
}
slouken@1895
  1519
slouken@5226
  1520
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
slouken@1895
  1521
slouken@1895
  1522
/* vi: set ts=4 sw=4 expandtab: */