src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 13 Sep 2013 17:42:46 -0700
changeset 7742 84e32c3d415c
parent 7719 31b5f9ff36ca
child 7756 f45660a4974c
permissions -rw-r--r--
Added SDL_Direct3D9GetAdapterIndex(), which returns the adapter index you would pass into CreateDevice to get your device on the right monitor in full screen mode. This fixes the default adapter in SDL_render_d3d.c, which means that tests will work fullscreen off the main monitor now.

CR: Sam
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"
slouken@7742
    32
#include "../../video/SDL_sysvideo.h"
slouken@7742
    33
#include "../../video/windows/SDL_windowsmodes.h"
philipp@7369
    34
#include <stdio.h>
slouken@5154
    35
slouken@5154
    36
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    37
#define D3D_DEBUG_INFO
slouken@5154
    38
#include <d3d9.h>
slouken@5154
    39
#endif
slouken@1895
    40
gabomdq@6320
    41
gabomdq@6320
    42
typedef interface ID3DXMatrixStack *LPD3DXMATRIXSTACK;
gabomdq@6320
    43
typedef struct _D3DMATRIX D3DXMATRIX, *LPD3DXMATRIX;
gabomdq@6320
    44
typedef struct _D3DVECTOR D3DXVECTOR3, *LPD3DXVECTOR3;
gabomdq@6320
    45
gabomdq@6320
    46
DEFINE_GUID(IID_ID3DXMatrixStack,
gabomdq@6320
    47
0xc7885ba7, 0xf990, 0x4fe7, 0x92, 0x2d, 0x85, 0x15, 0xe4, 0x77, 0xdd, 0x85);
gabomdq@6320
    48
gabomdq@6320
    49
#undef INTERFACE
gabomdq@6320
    50
#define INTERFACE ID3DXMatrixStack
gabomdq@6320
    51
gabomdq@6320
    52
DECLARE_INTERFACE_(ID3DXMatrixStack, IUnknown)
gabomdq@6320
    53
{
gabomdq@6320
    54
    STDMETHOD(QueryInterface)(THIS_ REFIID riid, LPVOID * ppvObj) PURE;
gabomdq@6320
    55
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
gabomdq@6320
    56
    STDMETHOD_(ULONG,Release)(THIS) PURE;
gabomdq@6320
    57
    STDMETHOD(Pop)(THIS) PURE;
gabomdq@6320
    58
    STDMETHOD(Push)(THIS) PURE;
gabomdq@6320
    59
    STDMETHOD(LoadIdentity)(THIS) PURE;
gabomdq@6320
    60
    STDMETHOD(LoadMatrix)(THIS_ CONST D3DXMATRIX* pM ) PURE;
gabomdq@6320
    61
    STDMETHOD(MultMatrix)(THIS_ CONST D3DXMATRIX* pM ) PURE;
gabomdq@6320
    62
    STDMETHOD(MultMatrixLocal)(THIS_ CONST D3DXMATRIX* pM ) PURE;
gabomdq@6320
    63
    STDMETHOD(RotateAxis)(THIS_ CONST D3DXVECTOR3* pV, FLOAT Angle) PURE;
gabomdq@6320
    64
    STDMETHOD(RotateAxisLocal)(THIS_ CONST D3DXVECTOR3* pV, FLOAT Angle) PURE;
gabomdq@6320
    65
    STDMETHOD(RotateYawPitchRoll)(THIS_ FLOAT Yaw, FLOAT Pitch, FLOAT Roll) PURE;
gabomdq@6320
    66
    STDMETHOD(RotateYawPitchRollLocal)(THIS_ FLOAT Yaw, FLOAT Pitch, FLOAT Roll) PURE;
gabomdq@6320
    67
    STDMETHOD(Scale)(THIS_ FLOAT x, FLOAT y, FLOAT z) PURE;
gabomdq@6320
    68
    STDMETHOD(ScaleLocal)(THIS_ FLOAT x, FLOAT y, FLOAT z) PURE;
gabomdq@6320
    69
    STDMETHOD(Translate)(THIS_ FLOAT x, FLOAT y, FLOAT z ) PURE;
gabomdq@6320
    70
    STDMETHOD(TranslateLocal)(THIS_ FLOAT x, FLOAT y, FLOAT z) PURE;
gabomdq@6320
    71
    STDMETHOD_(D3DXMATRIX*, GetTop)(THIS) PURE;
gabomdq@6320
    72
};
gabomdq@6320
    73
gabomdq@6320
    74
#undef INTERFACE
gabomdq@6320
    75
gabomdq@6320
    76
#if !defined(__cplusplus) || defined(CINTERFACE)
gabomdq@6320
    77
#define ID3DXMatrixStack_QueryInterface(p,a,b)            (p)->lpVtbl->QueryInterface(p,a,b)
gabomdq@6320
    78
#define ID3DXMatrixStack_AddRef(p)                        (p)->lpVtbl->AddRef(p)
gabomdq@6320
    79
#define ID3DXMatrixStack_Release(p)                       (p)->lpVtbl->Release(p)
gabomdq@6320
    80
#define ID3DXMatrixStack_Pop(p)                           (p)->lpVtbl->Pop(p)
gabomdq@6320
    81
#define ID3DXMatrixStack_Push(p)                          (p)->lpVtbl->Push(p)
gabomdq@6320
    82
#define ID3DXMatrixStack_LoadIdentity(p)                  (p)->lpVtbl->LoadIdentity(p)
gabomdq@6320
    83
#define ID3DXMatrixStack_LoadMatrix(p,a)                  (p)->lpVtbl->LoadMatrix(p,a)
gabomdq@6320
    84
#define ID3DXMatrixStack_MultMatrix(p,a)                  (p)->lpVtbl->MultMatrix(p,a)
gabomdq@6320
    85
#define ID3DXMatrixStack_MultMatrixLocal(p,a)             (p)->lpVtbl->MultMatrixLocal(p,a)
gabomdq@6320
    86
#define ID3DXMatrixStack_RotateAxis(p,a,b)                (p)->lpVtbl->RotateAxis(p,a,b)
gabomdq@6320
    87
#define ID3DXMatrixStack_RotateAxisLocal(p,a,b)           (p)->lpVtbl->RotateAxisLocal(p,a,b)
gabomdq@6320
    88
#define ID3DXMatrixStack_RotateYawPitchRoll(p,a,b,c)      (p)->lpVtbl->RotateYawPitchRoll(p,a,b,c)
gabomdq@6320
    89
#define ID3DXMatrixStack_RotateYawPitchRollLocal(p,a,b,c) (p)->lpVtbl->RotateYawPitchRollLocal(p,a,b,c)
gabomdq@6320
    90
#define ID3DXMatrixStack_Scale(p,a,b,c)                   (p)->lpVtbl->Scale(p,a,b,c)
gabomdq@6320
    91
#define ID3DXMatrixStack_ScaleLocal(p,a,b,c)              (p)->lpVtbl->ScaleLocal(p,a,b,c)
gabomdq@6320
    92
#define ID3DXMatrixStack_Translate(p,a,b,c)               (p)->lpVtbl->Translate(p,a,b,c)
gabomdq@6320
    93
#define ID3DXMatrixStack_TranslateLocal(p,a,b,c)          (p)->lpVtbl->TranslateLocal(p,a,b,c)
gabomdq@6320
    94
#define ID3DXMatrixStack_GetTop(p)                        (p)->lpVtbl->GetTop(p)
gabomdq@6320
    95
#else
gabomdq@6320
    96
#define ID3DXMatrixStack_QueryInterface(p,a,b)            (p)->QueryInterface(a,b)
gabomdq@6320
    97
#define ID3DXMatrixStack_AddRef(p)                        (p)->AddRef()
gabomdq@6320
    98
#define ID3DXMatrixStack_Release(p)                       (p)->Release()
gabomdq@6320
    99
#define ID3DXMatrixStack_Pop(p)    (p)->Pop()
gabomdq@6320
   100
#define ID3DXMatrixStack_Push(p)    (p)->Push()
gabomdq@6320
   101
#define ID3DXMatrixStack_LoadIdentity(p)    (p)->LoadIdentity()
gabomdq@6320
   102
#define ID3DXMatrixStack_LoadMatrix(p,a)    (p)->LoadMatrix(a)
gabomdq@6320
   103
#define ID3DXMatrixStack_MultMatrix(p,a)    (p)->MultMatrix(a)
gabomdq@6320
   104
#define ID3DXMatrixStack_MultMatrixLocal(p,a)    (p)->MultMatrixLocal(a)
gabomdq@6320
   105
#define ID3DXMatrixStack_RotateAxis(p,a,b)    (p)->RotateAxis(a,b)
gabomdq@6320
   106
#define ID3DXMatrixStack_RotateAxisLocal(p,a,b)    (p)->RotateAxisLocal(a,b)
gabomdq@6320
   107
#define ID3DXMatrixStack_RotateYawPitchRoll(p,a,b,c)    (p)->RotateYawPitchRollLocal(a,b,c)
gabomdq@6320
   108
#define ID3DXMatrixStack_Scale(p,a,b,c)    (p)->Scale(a,b,c)
gabomdq@6320
   109
#define ID3DXMatrixStack_ScaleLocal(p,a,b,c)    (p)->ScaleLocal(a,b,c)
gabomdq@6320
   110
#define ID3DXMatrixStack_Translate(p,a,b,c)    (p)->Translate(a,b,c)
gabomdq@6320
   111
#define ID3DXMatrixStack_TranslateLocal(p,a,b,c)    (p)->TranslateLocal(a,b,c)
gabomdq@6320
   112
#define ID3DXMatrixStack_GetTop(p)    (p)->GetTop()
gabomdq@6320
   113
#endif
gabomdq@6320
   114
gabomdq@6320
   115
#ifdef __cplusplus
gabomdq@6320
   116
extern "C" {
gabomdq@6320
   117
#endif
gabomdq@6320
   118
gabomdq@6320
   119
HRESULT WINAPI D3DXCreateMatrixStack(DWORD flags, LPD3DXMATRIXSTACK* ppstack);
gabomdq@6320
   120
gabomdq@6320
   121
#ifdef __cplusplus
gabomdq@6320
   122
}
gabomdq@6320
   123
#endif
gabomdq@6320
   124
slouken@3556
   125
#ifdef ASSEMBLE_SHADER
slouken@7191
   126
/**************************************************************************
slouken@7191
   127
 * ID3DXBuffer:
slouken@7191
   128
 * ------------
slouken@7191
   129
 * The buffer object is used by D3DX to return arbitrary size data.
slouken@7191
   130
 *
slouken@7191
   131
 * GetBufferPointer -
slouken@7191
   132
 *    Returns a pointer to the beginning of the buffer.
slouken@7191
   133
 *
slouken@7191
   134
 * GetBufferSize -
slouken@7191
   135
 *    Returns the size of the buffer, in bytes.
slouken@7191
   136
 **************************************************************************/
slouken@3556
   137
slouken@3556
   138
typedef interface ID3DXBuffer ID3DXBuffer;
slouken@3556
   139
typedef interface ID3DXBuffer *LPD3DXBUFFER;
slouken@3556
   140
slouken@7191
   141
/* {8BA5FB08-5195-40e2-AC58-0D989C3A0102} */
slouken@7191
   142
DEFINE_GUID(IID_ID3DXBuffer,
slouken@3556
   143
0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
slouken@3556
   144
slouken@3556
   145
#undef INTERFACE
slouken@3556
   146
#define INTERFACE ID3DXBuffer
slouken@3556
   147
slouken@3556
   148
typedef interface ID3DXBuffer {
slouken@3556
   149
    const struct ID3DXBufferVtbl FAR* lpVtbl;
slouken@3556
   150
} ID3DXBuffer;
slouken@3556
   151
typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
slouken@3556
   152
const struct ID3DXBufferVtbl
slouken@3556
   153
{
slouken@7191
   154
    /* IUnknown */
slouken@3556
   155
    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
slouken@3556
   156
    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
slouken@3556
   157
    STDMETHOD_(ULONG, Release)(THIS) PURE;
slouken@3556
   158
slouken@7191
   159
    /* ID3DXBuffer */
slouken@3556
   160
    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
slouken@3556
   161
    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
slouken@3556
   162
};
slouken@3556
   163
slouken@3556
   164
HRESULT WINAPI
slouken@3556
   165
    D3DXAssembleShader(
slouken@3556
   166
        LPCSTR                          pSrcData,
slouken@3556
   167
        UINT                            SrcDataLen,
slouken@3556
   168
        CONST LPVOID*                   pDefines,
slouken@3556
   169
        LPVOID                          pInclude,
slouken@3556
   170
        DWORD                           Flags,
slouken@3556
   171
        LPD3DXBUFFER*                   ppShader,
slouken@3556
   172
        LPD3DXBUFFER*                   ppErrorMsgs);
slouken@3556
   173
slouken@7505
   174
static void PrintShaderData(LPDWORD shader_data, DWORD shader_size)
slouken@7505
   175
{
slouken@7505
   176
    OutputDebugStringA("const DWORD shader_data[] = {\n\t");
slouken@7505
   177
    {
slouken@7505
   178
        SDL_bool newline = SDL_FALSE;
slouken@7505
   179
        unsigned i;
slouken@7505
   180
        for (i = 0; i < shader_size / sizeof(DWORD); ++i) {
slouken@7505
   181
            char dword[11];
slouken@7505
   182
            if (i > 0) {
slouken@7505
   183
                if ((i%6) == 0) {
slouken@7505
   184
                    newline = SDL_TRUE;
slouken@7505
   185
                }
slouken@7505
   186
                if (newline) {
slouken@7505
   187
                    OutputDebugStringA(",\n    ");
slouken@7505
   188
                    newline = SDL_FALSE;
slouken@7505
   189
                } else {
slouken@7505
   190
                    OutputDebugStringA(", ");
slouken@7505
   191
                }
slouken@7505
   192
            }
slouken@7505
   193
            SDL_snprintf(dword, sizeof(dword), "0x%8.8x", shader_data[i]);
slouken@7505
   194
            OutputDebugStringA(dword);
slouken@7505
   195
        }
slouken@7505
   196
        OutputDebugStringA("\n};\n");
slouken@7505
   197
    }
slouken@7505
   198
}
slouken@7505
   199
slouken@3556
   200
#endif /* ASSEMBLE_SHADER */
slouken@3556
   201
slouken@3556
   202
slouken@1895
   203
/* Direct3D renderer implementation */
slouken@1895
   204
slouken@1913
   205
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5297
   206
static void D3D_WindowEvent(SDL_Renderer * renderer,
slouken@5297
   207
                            const SDL_WindowEvent *event);
slouken@1913
   208
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
   209
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   210
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
   211
                             int pitch);
slouken@1913
   212
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   213
                           const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1913
   214
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6247
   215
static int D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
   216
static int D3D_UpdateViewport(SDL_Renderer * renderer);
slouken@7141
   217
static int D3D_UpdateClipRect(SDL_Renderer * renderer);
slouken@5297
   218
static int D3D_RenderClear(SDL_Renderer * renderer);
slouken@3599
   219
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
slouken@6528
   220
                                const SDL_FPoint * points, int count);
slouken@3599
   221
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
slouken@6528
   222
                               const SDL_FPoint * points, int count);
slouken@3599
   223
static int D3D_RenderFillRects(SDL_Renderer * renderer,
slouken@6528
   224
                               const SDL_FRect * rects, int count);
slouken@1913
   225
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   226
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect);
gabomdq@6320
   227
static int D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   228
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
   229
                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
slouken@3427
   230
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   231
                                Uint32 format, void * pixels, int pitch);
slouken@1913
   232
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
   233
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
   234
                               SDL_Texture * texture);
slouken@1913
   235
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
   236
slouken@1895
   237
slouken@1913
   238
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
   239
    D3D_CreateRenderer,
slouken@1895
   240
    {
slouken@5186
   241
     "direct3d",
slouken@6237
   242
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
slouken@5156
   243
     1,
slouken@5156
   244
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1895
   245
     0,
slouken@1895
   246
     0}
slouken@1895
   247
};
slouken@1895
   248
slouken@1895
   249
typedef struct
slouken@1895
   250
{
slouken@5154
   251
    void* d3dDLL;
slouken@2973
   252
    IDirect3D9 *d3d;
slouken@1895
   253
    IDirect3DDevice9 *device;
slouken@3527
   254
    UINT adapter;
slouken@1975
   255
    D3DPRESENT_PARAMETERS pparams;
slouken@5297
   256
    SDL_bool updateSize;
slouken@1900
   257
    SDL_bool beginScene;
slouken@7502
   258
    SDL_bool enableSeparateAlphaBlend;
slouken@7505
   259
    D3DTEXTUREFILTERTYPE scaleMode[8];
slouken@6232
   260
    IDirect3DSurface9 *defaultRenderTarget;
slouken@6232
   261
    IDirect3DSurface9 *currentRenderTarget;
gabomdq@6320
   262
    void* d3dxDLL;
gabomdq@6320
   263
    ID3DXMatrixStack *matrixStack;
slouken@7505
   264
    LPDIRECT3DPIXELSHADER9 ps_yuv;
slouken@1913
   265
} D3D_RenderData;
slouken@1895
   266
slouken@1895
   267
typedef struct
slouken@1895
   268
{
slouken@1903
   269
    IDirect3DTexture9 *texture;
slouken@5484
   270
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@7505
   271
slouken@7505
   272
    /* YV12 texture support */
slouken@7505
   273
    SDL_bool yuv;
slouken@7505
   274
    IDirect3DTexture9 *utexture;
slouken@7505
   275
    IDirect3DTexture9 *vtexture;
slouken@7505
   276
    Uint8 *pixels;
slouken@7505
   277
    int pitch;
slouken@7505
   278
    SDL_Rect locked_rect;
slouken@1913
   279
} D3D_TextureData;
slouken@1895
   280
slouken@1903
   281
typedef struct
slouken@1903
   282
{
slouken@1903
   283
    float x, y, z;
slouken@1987
   284
    DWORD color;
slouken@1904
   285
    float u, v;
slouken@1903
   286
} Vertex;
slouken@1903
   287
icculus@7037
   288
static int
slouken@1900
   289
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   290
{
slouken@1900
   291
    const char *error;
slouken@1900
   292
slouken@1900
   293
    switch (result) {
slouken@1900
   294
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   295
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   296
        break;
slouken@1900
   297
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   298
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   299
        break;
slouken@1900
   300
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   301
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   302
        break;
slouken@1900
   303
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   304
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   305
        break;
slouken@1900
   306
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   307
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   308
        break;
slouken@1900
   309
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   310
        error = "TOOMANYOPERATIONS";
slouken@1900
   311
        break;
slouken@1900
   312
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   313
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   314
        break;
slouken@1900
   315
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   316
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   317
        break;
slouken@1900
   318
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   319
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   320
        break;
slouken@1900
   321
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   322
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   323
        break;
slouken@1900
   324
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   325
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   326
        break;
slouken@1900
   327
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   328
        error = "DRIVERINTERNALERROR";
slouken@1900
   329
        break;
slouken@1900
   330
    case D3DERR_NOTFOUND:
slouken@1900
   331
        error = "NOTFOUND";
slouken@1900
   332
        break;
slouken@1900
   333
    case D3DERR_MOREDATA:
slouken@1900
   334
        error = "MOREDATA";
slouken@1900
   335
        break;
slouken@1900
   336
    case D3DERR_DEVICELOST:
slouken@1900
   337
        error = "DEVICELOST";
slouken@1900
   338
        break;
slouken@1900
   339
    case D3DERR_DEVICENOTRESET:
slouken@1900
   340
        error = "DEVICENOTRESET";
slouken@1900
   341
        break;
slouken@1900
   342
    case D3DERR_NOTAVAILABLE:
slouken@1900
   343
        error = "NOTAVAILABLE";
slouken@1900
   344
        break;
slouken@1900
   345
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   346
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   347
        break;
slouken@1900
   348
    case D3DERR_INVALIDDEVICE:
slouken@1900
   349
        error = "INVALIDDEVICE";
slouken@1900
   350
        break;
slouken@1900
   351
    case D3DERR_INVALIDCALL:
slouken@1900
   352
        error = "INVALIDCALL";
slouken@1900
   353
        break;
slouken@1900
   354
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   355
        error = "DRIVERINVALIDCALL";
slouken@1900
   356
        break;
slouken@1900
   357
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   358
        error = "WASSTILLDRAWING";
slouken@1900
   359
        break;
slouken@1900
   360
    default:
slouken@1900
   361
        error = "UNKNOWN";
slouken@1900
   362
        break;
slouken@1900
   363
    }
icculus@7037
   364
    return SDL_SetError("%s: %s", prefix, error);
slouken@1900
   365
}
slouken@1900
   366
slouken@1903
   367
static D3DFORMAT
slouken@1903
   368
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   369
{
slouken@1903
   370
    switch (format) {
slouken@1965
   371
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   372
        return D3DFMT_R5G6B5;
slouken@1965
   373
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   374
        return D3DFMT_X8R8G8B8;
slouken@1965
   375
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   376
        return D3DFMT_A8R8G8B8;
slouken@7505
   377
    case SDL_PIXELFORMAT_YV12:
slouken@7505
   378
    case SDL_PIXELFORMAT_IYUV:
slouken@7505
   379
        return D3DFMT_L8;
slouken@1903
   380
    default:
slouken@1903
   381
        return D3DFMT_UNKNOWN;
slouken@1903
   382
    }
slouken@1895
   383
}
slouken@1895
   384
slouken@5156
   385
static Uint32
slouken@5156
   386
D3DFMTToPixelFormat(D3DFORMAT format)
slouken@2973
   387
{
slouken@5156
   388
    switch (format) {
slouken@5156
   389
    case D3DFMT_R5G6B5:
slouken@5156
   390
        return SDL_PIXELFORMAT_RGB565;
slouken@5156
   391
    case D3DFMT_X8R8G8B8:
slouken@5156
   392
        return SDL_PIXELFORMAT_RGB888;
slouken@5156
   393
    case D3DFMT_A8R8G8B8:
slouken@5156
   394
        return SDL_PIXELFORMAT_ARGB8888;
slouken@5156
   395
    default:
slouken@5156
   396
        return SDL_PIXELFORMAT_UNKNOWN;
slouken@2973
   397
    }
slouken@1895
   398
}
slouken@1895
   399
slouken@7648
   400
static void
slouken@7648
   401
D3D_InitRenderState(D3D_RenderData *data)
slouken@7648
   402
{
slouken@7651
   403
    D3DMATRIX matrix;
slouken@7651
   404
slouken@7651
   405
    IDirect3DDevice9 *device = data->device;
slouken@7648
   406
slouken@7648
   407
    IDirect3DDevice9_SetVertexShader(device, NULL);
slouken@7648
   408
    IDirect3DDevice9_SetFVF(device, D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@7648
   409
    IDirect3DDevice9_SetRenderState(device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@7648
   410
    IDirect3DDevice9_SetRenderState(device, D3DRS_CULLMODE, D3DCULL_NONE);
slouken@7648
   411
    IDirect3DDevice9_SetRenderState(device, D3DRS_LIGHTING, FALSE);
slouken@7648
   412
slouken@7648
   413
    /* Enable color modulation by diffuse color */
slouken@7648
   414
    IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLOROP,
slouken@7648
   415
                                          D3DTOP_MODULATE);
slouken@7648
   416
    IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLORARG1,
slouken@7648
   417
                                          D3DTA_TEXTURE);
slouken@7648
   418
    IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_COLORARG2,
slouken@7648
   419
                                          D3DTA_DIFFUSE);
slouken@7648
   420
slouken@7648
   421
    /* Enable alpha modulation by diffuse alpha */
slouken@7648
   422
    IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAOP,
slouken@7648
   423
                                          D3DTOP_MODULATE);
slouken@7648
   424
    IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAARG1,
slouken@7648
   425
                                          D3DTA_TEXTURE);
slouken@7648
   426
    IDirect3DDevice9_SetTextureStageState(device, 0, D3DTSS_ALPHAARG2,
slouken@7648
   427
                                          D3DTA_DIFFUSE);
slouken@7648
   428
slouken@7648
   429
    /* Enable separate alpha blend function, if possible */
slouken@7648
   430
    if (data->enableSeparateAlphaBlend) {
slouken@7648
   431
        IDirect3DDevice9_SetRenderState(device, D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
slouken@7648
   432
    }
slouken@7648
   433
slouken@7648
   434
    /* Disable second texture stage, since we're done */
slouken@7648
   435
    IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_COLOROP,
slouken@7648
   436
                                          D3DTOP_DISABLE);
slouken@7648
   437
    IDirect3DDevice9_SetTextureStageState(device, 1, D3DTSS_ALPHAOP,
slouken@7648
   438
                                          D3DTOP_DISABLE);
slouken@7648
   439
slouken@7648
   440
    /* Set an identity world and view matrix */
slouken@7648
   441
    matrix.m[0][0] = 1.0f;
slouken@7648
   442
    matrix.m[0][1] = 0.0f;
slouken@7648
   443
    matrix.m[0][2] = 0.0f;
slouken@7648
   444
    matrix.m[0][3] = 0.0f;
slouken@7648
   445
    matrix.m[1][0] = 0.0f;
slouken@7648
   446
    matrix.m[1][1] = 1.0f;
slouken@7648
   447
    matrix.m[1][2] = 0.0f;
slouken@7648
   448
    matrix.m[1][3] = 0.0f;
slouken@7648
   449
    matrix.m[2][0] = 0.0f;
slouken@7648
   450
    matrix.m[2][1] = 0.0f;
slouken@7648
   451
    matrix.m[2][2] = 1.0f;
slouken@7648
   452
    matrix.m[2][3] = 0.0f;
slouken@7648
   453
    matrix.m[3][0] = 0.0f;
slouken@7648
   454
    matrix.m[3][1] = 0.0f;
slouken@7648
   455
    matrix.m[3][2] = 0.0f;
slouken@7648
   456
    matrix.m[3][3] = 1.0f;
slouken@7648
   457
    IDirect3DDevice9_SetTransform(device, D3DTS_WORLD, &matrix);
slouken@7648
   458
    IDirect3DDevice9_SetTransform(device, D3DTS_VIEW, &matrix);
slouken@7648
   459
slouken@7648
   460
    /* Reset our current scale mode */
slouken@7648
   461
    SDL_memset(data->scaleMode, 0xFF, sizeof(data->scaleMode));
slouken@7648
   462
slouken@7648
   463
    /* Start the render with beginScene */
slouken@7648
   464
    data->beginScene = SDL_TRUE;
slouken@7648
   465
}
slouken@7648
   466
slouken@5297
   467
static int
slouken@5297
   468
D3D_Reset(SDL_Renderer * renderer)
slouken@5297
   469
{
slouken@5297
   470
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   471
    HRESULT result;
slouken@5297
   472
slouken@6860
   473
    /* Release the default render target before reset */
slouken@6895
   474
    if (data->defaultRenderTarget) {
slouken@6895
   475
        IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6895
   476
        data->defaultRenderTarget = NULL;
slouken@6895
   477
    }
slouken@6860
   478
slouken@5297
   479
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@5297
   480
    if (FAILED(result)) {
slouken@5297
   481
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   482
            /* Don't worry about it, we'll reset later... */
slouken@5297
   483
            return 0;
slouken@5297
   484
        } else {
icculus@7037
   485
            return D3D_SetError("Reset()", result);
slouken@5297
   486
        }
slouken@5297
   487
    }
slouken@7648
   488
slouken@6860
   489
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
slouken@7648
   490
    D3D_InitRenderState(data);
slouken@7586
   491
    D3D_UpdateViewport(renderer);
slouken@5297
   492
    return 0;
slouken@5297
   493
}
slouken@5297
   494
slouken@5297
   495
static int
slouken@5297
   496
D3D_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   497
{
slouken@5297
   498
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   499
    HRESULT result;
slouken@5297
   500
slouken@5297
   501
    if (data->updateSize) {
slouken@5297
   502
        SDL_Window *window = renderer->window;
slouken@5297
   503
        int w, h;
slouken@5297
   504
slouken@5297
   505
        SDL_GetWindowSize(window, &w, &h);
slouken@5297
   506
        data->pparams.BackBufferWidth = w;
slouken@5297
   507
        data->pparams.BackBufferHeight = h;
slouken@5297
   508
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@5297
   509
            data->pparams.BackBufferFormat =
slouken@5297
   510
                PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window));
slouken@5297
   511
        } else {
slouken@5297
   512
            data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@5297
   513
        }
slouken@5297
   514
        if (D3D_Reset(renderer) < 0) {
slouken@5297
   515
            return -1;
slouken@5297
   516
        }
slouken@5297
   517
slouken@5297
   518
        data->updateSize = SDL_FALSE;
slouken@5297
   519
    }
slouken@5297
   520
    if (data->beginScene) {
slouken@5297
   521
        result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   522
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   523
            if (D3D_Reset(renderer) < 0) {
slouken@5297
   524
                return -1;
slouken@5297
   525
            }
slouken@5297
   526
            result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   527
        }
slouken@5297
   528
        if (FAILED(result)) {
icculus@7037
   529
            return D3D_SetError("BeginScene()", result);
slouken@5297
   530
        }
slouken@5297
   531
        data->beginScene = SDL_FALSE;
slouken@5297
   532
    }
slouken@5297
   533
    return 0;
slouken@5297
   534
}
slouken@5297
   535
slouken@7742
   536
SDL_bool 
slouken@7742
   537
D3D_LoadDLL( void **pD3DDLL, IDirect3D9 **pDirect3D9Interface )
slouken@7742
   538
{
slouken@7742
   539
	*pD3DDLL = SDL_LoadObject("D3D9.DLL");
slouken@7742
   540
	if (*pD3DDLL) {
slouken@7742
   541
		IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@7742
   542
slouken@7742
   543
		D3DCreate =
slouken@7742
   544
			(IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(*pD3DDLL,
slouken@7742
   545
			"Direct3DCreate9");
slouken@7742
   546
		if (D3DCreate) {
slouken@7742
   547
			*pDirect3D9Interface = D3DCreate(D3D_SDK_VERSION);
slouken@7742
   548
		}
slouken@7742
   549
		if (!*pDirect3D9Interface) {
slouken@7742
   550
			SDL_UnloadObject(*pD3DDLL);
slouken@7742
   551
			*pD3DDLL = NULL;
slouken@7742
   552
			return SDL_FALSE;
slouken@7742
   553
		}
slouken@7742
   554
slouken@7742
   555
		return SDL_TRUE;
slouken@7742
   556
	} else {
slouken@7742
   557
		*pDirect3D9Interface = NULL;
slouken@7742
   558
		return SDL_FALSE;
slouken@7742
   559
	}
slouken@7742
   560
}
slouken@7742
   561
slouken@7742
   562
slouken@7742
   563
int 
slouken@7742
   564
SDL_Direct3D9GetAdapterIndex( int displayIndex )
slouken@7742
   565
{
slouken@7742
   566
	void *pD3DDLL;
slouken@7742
   567
	IDirect3D9 *pD3D;
slouken@7742
   568
	if (!D3D_LoadDLL( &pD3DDLL, &pD3D)) {
slouken@7742
   569
		SDL_SetError("Unable to create Direct3D interface");
slouken@7742
   570
		return D3DADAPTER_DEFAULT;
slouken@7742
   571
	} else {
slouken@7742
   572
		SDL_DisplayData *pData = (SDL_DisplayData *)SDL_GetDisplayDriverData( displayIndex );
slouken@7742
   573
		int adapterIndex = D3DADAPTER_DEFAULT;
slouken@7742
   574
slouken@7742
   575
		if (!pData) {
slouken@7742
   576
			SDL_SetError( "Invalid display index" );
slouken@7742
   577
		} else {
slouken@7742
   578
			char *displayName = WIN_StringToUTF8( pData->DeviceName );
slouken@7742
   579
			unsigned int count = IDirect3D9_GetAdapterCount( pD3D );
slouken@7742
   580
			unsigned int i;
slouken@7742
   581
			for (i=0; i<count; i++) {
slouken@7742
   582
				D3DADAPTER_IDENTIFIER9 id;
slouken@7742
   583
				IDirect3D9_GetAdapterIdentifier(pD3D, i, 0, &id);
slouken@7742
   584
slouken@7742
   585
				if (SDL_strcmp(id.DeviceName, displayName) == 0) {
slouken@7742
   586
					adapterIndex = i;
slouken@7742
   587
					break;
slouken@7742
   588
				}
slouken@7742
   589
			}
slouken@7742
   590
			SDL_free( displayName );
slouken@7742
   591
		}
slouken@7742
   592
slouken@7742
   593
		/* free up the D3D stuff we inited */
slouken@7742
   594
		IDirect3D9_Release(pD3D);
slouken@7742
   595
		SDL_UnloadObject(pD3DDLL);
slouken@7742
   596
slouken@7742
   597
		return adapterIndex;
slouken@7742
   598
	}
slouken@7742
   599
}
slouken@7742
   600
slouken@7742
   601
slouken@1895
   602
SDL_Renderer *
slouken@1913
   603
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   604
{
slouken@1895
   605
    SDL_Renderer *renderer;
slouken@1913
   606
    D3D_RenderData *data;
slouken@5154
   607
    SDL_SysWMinfo windowinfo;
slouken@1900
   608
    HRESULT result;
slouken@1900
   609
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   610
    IDirect3DSwapChain9 *chain;
slouken@1925
   611
    D3DCAPS9 caps;
slouken@5156
   612
    Uint32 window_flags;
slouken@5156
   613
    int w, h;
slouken@5156
   614
    SDL_DisplayMode fullscreen_mode;
gabomdq@6320
   615
    int d3dxVersion;
slouken@7191
   616
    char d3dxDLLFile[50];
slouken@7742
   617
	int displayIndex;
slouken@1895
   618
slouken@1920
   619
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   620
    if (!renderer) {
slouken@1895
   621
        SDL_OutOfMemory();
slouken@1895
   622
        return NULL;
slouken@1895
   623
    }
slouken@1895
   624
slouken@1920
   625
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   626
    if (!data) {
slouken@5154
   627
        SDL_free(renderer);
slouken@1895
   628
        SDL_OutOfMemory();
slouken@1895
   629
        return NULL;
slouken@1895
   630
    }
slouken@1895
   631
slouken@7742
   632
	if( D3D_LoadDLL( &data->d3dDLL, &data->d3d ) ) {
gabomdq@6320
   633
        for (d3dxVersion=50;d3dxVersion>0;d3dxVersion--) {
slouken@6971
   634
            LPTSTR dllName;
slouken@6971
   635
            SDL_snprintf(d3dxDLLFile, sizeof(d3dxDLLFile), "D3DX9_%02d.dll", d3dxVersion);
slouken@6971
   636
            dllName = WIN_UTF8ToString(d3dxDLLFile);
slouken@6971
   637
            data->d3dxDLL = (void *)LoadLibrary(dllName); /* not using SDL_LoadObject() as we want silently fail - no error message */
slouken@6971
   638
            SDL_free(dllName);
gabomdq@6320
   639
            if (data->d3dxDLL) {
gabomdq@6320
   640
                HRESULT (WINAPI *D3DXCreateMatrixStack) (DWORD Flags, LPD3DXMATRIXSTACK*  ppStack);
gabomdq@6320
   641
                D3DXCreateMatrixStack = (HRESULT (WINAPI *) (DWORD, LPD3DXMATRIXSTACK*)) SDL_LoadFunction(data->d3dxDLL, "D3DXCreateMatrixStack");
gabomdq@6320
   642
                if (D3DXCreateMatrixStack) {
gabomdq@6320
   643
                    D3DXCreateMatrixStack(0, &data->matrixStack);
gabomdq@6320
   644
                    break;
gabomdq@6320
   645
                }
gabomdq@6320
   646
            }
gabomdq@6320
   647
        }
gabomdq@6320
   648
gabomdq@6320
   649
        if (!data->matrixStack) {
gabomdq@6320
   650
            if (data->d3dxDLL) SDL_UnloadObject(data->d3dxDLL);
gabomdq@6320
   651
        }
slouken@5154
   652
    }
gabomdq@6320
   653
gabomdq@6320
   654
slouken@7191
   655
gabomdq@6320
   656
    if (!data->d3d || !data->matrixStack) {
slouken@5154
   657
        SDL_free(renderer);
slouken@5154
   658
        SDL_free(data);
slouken@5154
   659
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   660
        return NULL;
slouken@5154
   661
    }
slouken@5154
   662
slouken@5297
   663
    renderer->WindowEvent = D3D_WindowEvent;
slouken@1913
   664
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   665
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   666
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   667
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@6247
   668
    renderer->SetRenderTarget = D3D_SetRenderTarget;
slouken@5297
   669
    renderer->UpdateViewport = D3D_UpdateViewport;
slouken@7141
   670
    renderer->UpdateClipRect = D3D_UpdateClipRect;
slouken@5297
   671
    renderer->RenderClear = D3D_RenderClear;
slouken@3599
   672
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   673
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   674
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   675
    renderer->RenderCopy = D3D_RenderCopy;
gabomdq@6320
   676
    renderer->RenderCopyEx = D3D_RenderCopyEx;
slouken@3427
   677
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@1913
   678
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   679
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   680
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   681
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   682
    renderer->driverdata = data;
slouken@1895
   683
slouken@6246
   684
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   685
slouken@5154
   686
    SDL_VERSION(&windowinfo.version);
slouken@5154
   687
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   688
slouken@5156
   689
    window_flags = SDL_GetWindowFlags(window);
slouken@5156
   690
    SDL_GetWindowSize(window, &w, &h);
slouken@5156
   691
    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
slouken@5156
   692
slouken@1900
   693
    SDL_zero(pparams);
slouken@5154
   694
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@5156
   695
    pparams.BackBufferWidth = w;
slouken@5156
   696
    pparams.BackBufferHeight = h;
slouken@5156
   697
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   698
        pparams.BackBufferFormat =
slouken@5156
   699
            PixelFormatToD3DFMT(fullscreen_mode.format);
slouken@1903
   700
    } else {
slouken@1903
   701
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   702
    }
slouken@5142
   703
    pparams.BackBufferCount = 1;
slouken@5142
   704
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   705
slouken@5156
   706
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@7191
   707
        if ( ( window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) == SDL_WINDOW_FULLSCREEN_DESKTOP )  {
slouken@7191
   708
            pparams.Windowed = TRUE;
slouken@7191
   709
            pparams.FullScreen_RefreshRateInHz = 0;
slouken@7191
   710
        } else {
slouken@1900
   711
        pparams.Windowed = FALSE;
slouken@1903
   712
        pparams.FullScreen_RefreshRateInHz =
slouken@5156
   713
            fullscreen_mode.refresh_rate;
slouken@7191
   714
        }
slouken@1900
   715
    } else {
slouken@1900
   716
        pparams.Windowed = TRUE;
slouken@1903
   717
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   718
    }
slouken@1965
   719
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   720
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   721
    } else {
slouken@1907
   722
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   723
    }
slouken@1900
   724
slouken@7742
   725
    /* Get the adapter for the display that the window is on */
slouken@7742
   726
	displayIndex = SDL_GetWindowDisplayIndex( window );
slouken@7742
   727
    data->adapter = SDL_Direct3D9GetAdapterIndex( displayIndex );
slouken@7742
   728
slouken@5154
   729
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   730
slouken@5154
   731
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   732
                                     D3DDEVTYPE_HAL,
slouken@5154
   733
                                     pparams.hDeviceWindow,
slouken@6115
   734
                                     D3DCREATE_FPU_PRESERVE | ((caps.
slouken@3197
   735
                                      DevCaps &
slouken@3197
   736
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   737
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@6115
   738
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING),
slouken@1900
   739
                                     &pparams, &data->device);
slouken@1900
   740
    if (FAILED(result)) {
slouken@1913
   741
        D3D_DestroyRenderer(renderer);
slouken@1900
   742
        D3D_SetError("CreateDevice()", result);
slouken@1900
   743
        return NULL;
slouken@1900
   744
    }
slouken@1900
   745
slouken@1907
   746
    /* Get presentation parameters to fill info */
slouken@1907
   747
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   748
    if (FAILED(result)) {
slouken@1913
   749
        D3D_DestroyRenderer(renderer);
slouken@1907
   750
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   751
        return NULL;
slouken@1907
   752
    }
slouken@1907
   753
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   754
    if (FAILED(result)) {
slouken@1907
   755
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   756
        D3D_DestroyRenderer(renderer);
slouken@1907
   757
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   758
        return NULL;
slouken@1907
   759
    }
slouken@1907
   760
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   761
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   762
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   763
    }
slouken@1975
   764
    data->pparams = pparams;
slouken@1907
   765
slouken@1925
   766
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   767
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   768
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@6246
   769
    if (caps.NumSimultaneousRTs >= 2) {
slouken@6246
   770
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6246
   771
    }
slouken@1918
   772
slouken@7502
   773
    if (caps.PrimitiveMiscCaps & D3DPMISCCAPS_SEPARATEALPHABLEND) {
slouken@7502
   774
        data->enableSeparateAlphaBlend = SDL_TRUE;
slouken@7502
   775
    }
slouken@7502
   776
slouken@6232
   777
    /* Store the default render target */
slouken@6232
   778
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
slouken@6232
   779
    data->currentRenderTarget = NULL;
slouken@6232
   780
slouken@7648
   781
    /* Set up parameters for rendering */
slouken@7648
   782
    D3D_InitRenderState(data);
slouken@5297
   783
slouken@7505
   784
    if (caps.MaxSimultaneousTextures >= 3)
slouken@7505
   785
    {
slouken@7505
   786
#ifdef ASSEMBLE_SHADER
slouken@7505
   787
        /* This shader was created by running the following HLSL through the fxc compiler
slouken@7505
   788
           and then tuning the generated assembly.
slouken@7505
   789
slouken@7505
   790
           fxc /T fx_4_0 /O3 /Gfa /Fc yuv.fxc yuv.fx
slouken@7505
   791
slouken@7505
   792
           --- yuv.fx ---
slouken@7505
   793
           Texture2D g_txY;
slouken@7505
   794
           Texture2D g_txU;
slouken@7505
   795
           Texture2D g_txV;
slouken@7505
   796
slouken@7505
   797
           SamplerState samLinear
slouken@7505
   798
           {
slouken@7505
   799
               Filter = ANISOTROPIC;
slouken@7505
   800
               AddressU = Clamp;
slouken@7505
   801
               AddressV = Clamp;
slouken@7505
   802
               MaxAnisotropy = 1;
slouken@7505
   803
           };
slouken@7505
   804
slouken@7505
   805
           struct VS_OUTPUT
slouken@7505
   806
           {
slouken@7505
   807
                float2 TextureUV  : TEXCOORD0;
slouken@7505
   808
           };
slouken@7505
   809
slouken@7505
   810
           struct PS_OUTPUT
slouken@7505
   811
           {
slouken@7505
   812
                float4 RGBAColor : SV_Target;
slouken@7505
   813
           };
slouken@7505
   814
slouken@7505
   815
           PS_OUTPUT YUV420( VS_OUTPUT In ) 
slouken@7505
   816
           {
slouken@7505
   817
               const float3 offset = {-0.0625, -0.5, -0.5};
slouken@7505
   818
               const float3 Rcoeff = {1.164,  0.000,  1.596};
slouken@7505
   819
               const float3 Gcoeff = {1.164, -0.391, -0.813};
slouken@7505
   820
               const float3 Bcoeff = {1.164,  2.018,  0.000};
slouken@7505
   821
slouken@7505
   822
               PS_OUTPUT Output;
slouken@7505
   823
               float2 TextureUV = In.TextureUV;
slouken@7505
   824
slouken@7505
   825
               float3 yuv;
slouken@7505
   826
               yuv.x = g_txY.Sample( samLinear, TextureUV ).r;
slouken@7505
   827
               yuv.y = g_txU.Sample( samLinear, TextureUV ).r;
slouken@7505
   828
               yuv.z = g_txV.Sample( samLinear, TextureUV ).r;
slouken@7505
   829
slouken@7505
   830
               yuv += offset;
slouken@7505
   831
               Output.RGBAColor.r = dot(yuv, Rcoeff);
slouken@7505
   832
               Output.RGBAColor.g = dot(yuv, Gcoeff);
slouken@7505
   833
               Output.RGBAColor.b = dot(yuv, Bcoeff);
slouken@7505
   834
               Output.RGBAColor.a = 1.0f;
slouken@7505
   835
slouken@7505
   836
               return Output;
slouken@7505
   837
           }
slouken@7505
   838
slouken@7505
   839
           technique10 RenderYUV420
slouken@7505
   840
           {
slouken@7505
   841
               pass P0
slouken@7505
   842
               {
slouken@7505
   843
                    SetPixelShader( CompileShader( ps_4_0_level_9_0, YUV420() ) );
slouken@7505
   844
               }
slouken@7505
   845
           }
slouken@7505
   846
        */
slouken@7505
   847
        const char *shader_text =
slouken@7505
   848
            "ps_2_0\n"
slouken@7505
   849
            "def c0, -0.0625, -0.5, -0.5, 1\n"
slouken@7505
   850
            "def c1, 1.16400003, 0, 1.59599996, 0\n"
slouken@7505
   851
            "def c2, 1.16400003, -0.391000003, -0.813000023, 0\n"
slouken@7505
   852
            "def c3, 1.16400003, 2.01799989, 0, 0\n"
slouken@7505
   853
            "dcl t0.xy\n"
slouken@7505
   854
            "dcl v0.xyzw\n"
slouken@7505
   855
            "dcl_2d s0\n"
slouken@7505
   856
            "dcl_2d s1\n"
slouken@7505
   857
            "dcl_2d s2\n"
slouken@7505
   858
            "texld r0, t0, s0\n"
slouken@7505
   859
            "texld r1, t0, s1\n"
slouken@7505
   860
            "texld r2, t0, s2\n"
slouken@7505
   861
            "mov r0.y, r1.x\n"
slouken@7505
   862
            "mov r0.z, r2.x\n"
slouken@7505
   863
            "add r0.xyz, r0, c0\n"
slouken@7505
   864
            "dp3 r1.x, r0, c1\n"
slouken@7505
   865
            "dp3 r1.y, r0, c2\n"
slouken@7505
   866
            "dp2add r1.z, r0, c3, c3.z\n"   /* Logically this is "dp3 r1.z, r0, c3" but the optimizer did its magic */
slouken@7505
   867
            "mov r1.w, c0.w\n"
slouken@7505
   868
            "mul r0, r1, v0\n"              /* Not in the HLSL, multiply by vertex color */
slouken@7505
   869
            "mov oC0, r0\n"
slouken@7505
   870
        ;
slouken@7505
   871
        LPD3DXBUFFER pCode;
slouken@7505
   872
        LPD3DXBUFFER pErrorMsgs;
slouken@7505
   873
        LPDWORD shader_data = NULL;
slouken@7505
   874
        DWORD   shader_size = 0;
slouken@7505
   875
        result = D3DXAssembleShader(shader_text, SDL_strlen(shader_text), NULL, NULL, 0, &pCode, &pErrorMsgs);
slouken@7505
   876
        if (!FAILED(result)) {
slouken@7505
   877
            shader_data = (DWORD*)pCode->lpVtbl->GetBufferPointer(pCode);
slouken@7505
   878
            shader_size = pCode->lpVtbl->GetBufferSize(pCode);
slouken@7505
   879
            PrintShaderData(shader_data, shader_size);
slouken@7505
   880
        } else {
slouken@7505
   881
            const char *error = (const char *)pErrorMsgs->lpVtbl->GetBufferPointer(pErrorMsgs);
slouken@7505
   882
            SDL_SetError("Couldn't assemble shader: %s", error);
slouken@7505
   883
        }
slouken@7505
   884
#else
slouken@7505
   885
        const DWORD shader_data[] = {
slouken@7505
   886
            0xffff0200, 0x05000051, 0xa00f0000, 0xbd800000, 0xbf000000, 0xbf000000,
slouken@7505
   887
            0x3f800000, 0x05000051, 0xa00f0001, 0x3f94fdf4, 0x00000000, 0x3fcc49ba,
slouken@7505
   888
            0x00000000, 0x05000051, 0xa00f0002, 0x3f94fdf4, 0xbec83127, 0xbf5020c5,
slouken@7505
   889
            0x00000000, 0x05000051, 0xa00f0003, 0x3f94fdf4, 0x400126e9, 0x00000000,
slouken@7505
   890
            0x00000000, 0x0200001f, 0x80000000, 0xb0030000, 0x0200001f, 0x80000000,
slouken@7505
   891
            0x900f0000, 0x0200001f, 0x90000000, 0xa00f0800, 0x0200001f, 0x90000000,
slouken@7505
   892
            0xa00f0801, 0x0200001f, 0x90000000, 0xa00f0802, 0x03000042, 0x800f0000,
slouken@7505
   893
            0xb0e40000, 0xa0e40800, 0x03000042, 0x800f0001, 0xb0e40000, 0xa0e40801,
slouken@7505
   894
            0x03000042, 0x800f0002, 0xb0e40000, 0xa0e40802, 0x02000001, 0x80020000,
slouken@7505
   895
            0x80000001, 0x02000001, 0x80040000, 0x80000002, 0x03000002, 0x80070000,
slouken@7505
   896
            0x80e40000, 0xa0e40000, 0x03000008, 0x80010001, 0x80e40000, 0xa0e40001,
slouken@7505
   897
            0x03000008, 0x80020001, 0x80e40000, 0xa0e40002, 0x0400005a, 0x80040001,
slouken@7505
   898
            0x80e40000, 0xa0e40003, 0xa0aa0003, 0x02000001, 0x80080001, 0xa0ff0000,
slouken@7505
   899
            0x03000005, 0x800f0000, 0x80e40001, 0x90e40000, 0x02000001, 0x800f0800,
slouken@7505
   900
            0x80e40000, 0x0000ffff
slouken@7505
   901
        };
slouken@7505
   902
#endif
slouken@7547
   903
        if (shader_data != NULL) {
slouken@7505
   904
            result = IDirect3DDevice9_CreatePixelShader(data->device, shader_data, &data->ps_yuv);
slouken@7505
   905
            if (!FAILED(result)) {
slouken@7505
   906
                renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@7505
   907
                renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@7505
   908
            } else {
slouken@7505
   909
                D3D_SetError("CreatePixelShader()", result);
slouken@7505
   910
            }
slouken@7505
   911
        }
slouken@7505
   912
    }
slouken@7505
   913
slouken@1895
   914
    return renderer;
slouken@1895
   915
}
slouken@1895
   916
slouken@5297
   917
static void
slouken@5297
   918
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1975
   919
{
slouken@1975
   920
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   921
slouken@5297
   922
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   923
        data->updateSize = SDL_TRUE;
slouken@1975
   924
    }
slouken@1975
   925
}
slouken@1975
   926
slouken@5484
   927
static D3DTEXTUREFILTERTYPE
slouken@5484
   928
GetScaleQuality(void)
slouken@5484
   929
{
slouken@5484
   930
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   931
slouken@5484
   932
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   933
        return D3DTEXF_POINT;
gabomdq@7678
   934
    } else /* if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) */ {
slouken@5484
   935
        return D3DTEXF_LINEAR;
slouken@5484
   936
    }
slouken@5484
   937
}
slouken@5484
   938
slouken@1975
   939
static int
slouken@1913
   940
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   941
{
icculus@6404
   942
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   943
    D3D_TextureData *data;
slouken@5173
   944
    D3DPOOL pool;
slouken@5173
   945
    DWORD usage;
slouken@1903
   946
    HRESULT result;
slouken@1895
   947
slouken@1920
   948
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   949
    if (!data) {
icculus@7037
   950
        return SDL_OutOfMemory();
slouken@1895
   951
    }
slouken@5484
   952
    data->scaleMode = GetScaleQuality();
slouken@1895
   953
slouken@1895
   954
    texture->driverdata = data;
slouken@1895
   955
slouken@5173
   956
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   957
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   958
        pool = D3DPOOL_DEFAULT;
slouken@5173
   959
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   960
    } else
slouken@5173
   961
#endif
slouken@6232
   962
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6246
   963
        /* D3DPOOL_MANAGED does not work with D3DUSAGE_RENDERTARGET */
slouken@6246
   964
        pool = D3DPOOL_DEFAULT;
slouken@6232
   965
        usage = D3DUSAGE_RENDERTARGET;
slouken@6246
   966
    } else {
slouken@5173
   967
        pool = D3DPOOL_MANAGED;
slouken@5173
   968
        usage = 0;
slouken@5173
   969
    }
slouken@2973
   970
slouken@1903
   971
    result =
slouken@1903
   972
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   973
                                       texture->h, 1, usage,
slouken@5173
   974
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   975
                                       pool, &data->texture, NULL);
slouken@1903
   976
    if (FAILED(result)) {
icculus@7037
   977
        return D3D_SetError("CreateTexture()", result);
slouken@1903
   978
    }
slouken@1903
   979
slouken@7505
   980
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@7505
   981
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@7505
   982
        data->yuv = SDL_TRUE;
slouken@7505
   983
slouken@7505
   984
        result =
slouken@7505
   985
            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w / 2,
slouken@7505
   986
                                           texture->h / 2, 1, usage,
slouken@7505
   987
                                           PixelFormatToD3DFMT(texture->format),
slouken@7505
   988
                                           pool, &data->utexture, NULL);
slouken@7505
   989
        if (FAILED(result)) {
slouken@7505
   990
            return D3D_SetError("CreateTexture()", result);
slouken@7505
   991
        }
slouken@7505
   992
slouken@7505
   993
        result =
slouken@7505
   994
            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w / 2,
slouken@7505
   995
                                           texture->h / 2, 1, usage,
slouken@7505
   996
                                           PixelFormatToD3DFMT(texture->format),
slouken@7505
   997
                                           pool, &data->vtexture, NULL);
slouken@7505
   998
        if (FAILED(result)) {
slouken@7505
   999
            return D3D_SetError("CreateTexture()", result);
slouken@7505
  1000
        }
slouken@7505
  1001
    }
slouken@7505
  1002
slouken@7505
  1003
    return 0;
slouken@7505
  1004
}
slouken@7505
  1005
slouken@7505
  1006
static int
slouken@7505
  1007
D3D_UpdateTextureInternal(IDirect3DTexture9 *texture, Uint32 format, SDL_bool full_texture, int x, int y, int w, int h, const void *pixels, int pitch)
slouken@7505
  1008
{
slouken@7505
  1009
    RECT d3drect;
slouken@7505
  1010
    D3DLOCKED_RECT locked;
slouken@7505
  1011
    const Uint8 *src;
slouken@7505
  1012
    Uint8 *dst;
slouken@7505
  1013
    int row, length;
slouken@7505
  1014
    HRESULT result;
slouken@7505
  1015
slouken@7505
  1016
    if (full_texture) {
slouken@7505
  1017
        result = IDirect3DTexture9_LockRect(texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@7505
  1018
    } else {
slouken@7505
  1019
        d3drect.left = x;
slouken@7505
  1020
        d3drect.right = x + w;
slouken@7505
  1021
        d3drect.top = y;
slouken@7505
  1022
        d3drect.bottom = y + h;
slouken@7505
  1023
        result = IDirect3DTexture9_LockRect(texture, 0, &locked, &d3drect, 0);
slouken@7505
  1024
    }
slouken@7505
  1025
slouken@7505
  1026
    if (FAILED(result)) {
slouken@7505
  1027
        return D3D_SetError("LockRect()", result);
slouken@7505
  1028
    }
slouken@7505
  1029
slouken@7505
  1030
    src = (const Uint8 *)pixels;
slouken@7505
  1031
    dst = locked.pBits;
slouken@7505
  1032
    length = w * SDL_BYTESPERPIXEL(format);
slouken@7505
  1033
    if (length == pitch && length == locked.Pitch) {
slouken@7505
  1034
        SDL_memcpy(dst, src, length*h);
slouken@7505
  1035
    } else {
slouken@7505
  1036
        for (row = 0; row < h; ++row) {
slouken@7505
  1037
            SDL_memcpy(dst, src, length);
slouken@7505
  1038
            src += pitch;
slouken@7505
  1039
            dst += locked.Pitch;
slouken@7505
  1040
        }
slouken@7505
  1041
    }
slouken@7505
  1042
    IDirect3DTexture9_UnlockRect(texture, 0);
slouken@7505
  1043
slouken@1895
  1044
    return 0;
slouken@1895
  1045
}
slouken@1895
  1046
slouken@1895
  1047
static int
slouken@1913
  1048
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
  1049
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
  1050
{
slouken@1913
  1051
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@7505
  1052
    SDL_bool full_texture = SDL_FALSE;
slouken@2973
  1053
slouken@5173
  1054
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
  1055
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
  1056
        rect->x == 0 && rect->y == 0 &&
slouken@5173
  1057
        rect->w == texture->w && rect->h == texture->h) {
slouken@7505
  1058
        full_texture = SDL_TRUE;
slouken@7505
  1059
    }
slouken@5173
  1060
#endif
slouken@7505
  1061
slouken@7505
  1062
    if (D3D_UpdateTextureInternal(data->texture, texture->format, full_texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
slouken@7505
  1063
        return -1;
slouken@5156
  1064
    }
slouken@2973
  1065
slouken@7505
  1066
    if (data->yuv) {
slouken@7505
  1067
        /* Skip to the correct offset into the next texture */
slouken@7505
  1068
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@2973
  1069
slouken@7505
  1070
        if (D3D_UpdateTextureInternal(texture->format == SDL_PIXELFORMAT_YV12 ? data->vtexture : data->utexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
slouken@7505
  1071
            return -1;
slouken@7505
  1072
        }
slouken@7505
  1073
slouken@7505
  1074
        /* Skip to the correct offset into the next texture */
slouken@7505
  1075
        pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
slouken@7505
  1076
        if (D3D_UpdateTextureInternal(texture->format == SDL_PIXELFORMAT_YV12 ? data->utexture : data->vtexture, texture->format, full_texture, rect->x / 2, rect->y / 2, rect->w / 2, rect->h / 2, pixels, pitch / 2) < 0) {
slouken@7505
  1077
            return -1;
slouken@5173
  1078
        }
slouken@5156
  1079
    }
slouken@5156
  1080
    return 0;
slouken@1895
  1081
}
slouken@1895
  1082
slouken@1895
  1083
static int
slouken@1913
  1084
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
  1085
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
  1086
{
slouken@1913
  1087
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
  1088
    RECT d3drect;
slouken@5156
  1089
    D3DLOCKED_RECT locked;
slouken@5156
  1090
    HRESULT result;
slouken@1895
  1091
slouken@7505
  1092
    if (data->yuv) {
gabomdq@7663
  1093
        /* It's more efficient to upload directly... */
slouken@7505
  1094
        if (!data->pixels) {
slouken@7505
  1095
            data->pitch = texture->w;
slouken@7505
  1096
            data->pixels = (Uint8 *)SDL_malloc((texture->h * data->pitch * 3) / 2);
slouken@7505
  1097
            if (!data->pixels) {
slouken@7505
  1098
                return SDL_OutOfMemory();
slouken@7505
  1099
            }
slouken@7505
  1100
        }
slouken@7505
  1101
        data->locked_rect = *rect;
slouken@7505
  1102
        *pixels =
slouken@7505
  1103
            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@7505
  1104
                      rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@7505
  1105
        *pitch = data->pitch;
slouken@7505
  1106
    } else {
slouken@7505
  1107
        d3drect.left = rect->x;
slouken@7505
  1108
        d3drect.right = rect->x + rect->w;
slouken@7505
  1109
        d3drect.top = rect->y;
slouken@7505
  1110
        d3drect.bottom = rect->y + rect->h;
slouken@1903
  1111
slouken@7505
  1112
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@7505
  1113
        if (FAILED(result)) {
slouken@7505
  1114
            return D3D_SetError("LockRect()", result);
slouken@7505
  1115
        }
slouken@7505
  1116
        *pixels = locked.pBits;
slouken@7505
  1117
        *pitch = locked.Pitch;
slouken@1903
  1118
    }
slouken@5156
  1119
    return 0;
slouken@1895
  1120
}
slouken@1895
  1121
slouken@1895
  1122
static void
slouken@1913
  1123
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1124
{
slouken@1913
  1125
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1126
slouken@7505
  1127
    if (data->yuv) {
slouken@7505
  1128
        const SDL_Rect *rect = &data->locked_rect;
slouken@7505
  1129
        void *pixels =
slouken@7505
  1130
            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@7505
  1131
                      rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@7505
  1132
        D3D_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@7505
  1133
    } else {
slouken@7505
  1134
        IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@7505
  1135
    }
slouken@1895
  1136
}
slouken@1895
  1137
slouken@5297
  1138
static int
slouken@7141
  1139
D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@7141
  1140
{
slouken@7141
  1141
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7141
  1142
    D3D_TextureData *texturedata;
slouken@7141
  1143
    HRESULT result;
slouken@7141
  1144
slouken@7141
  1145
    D3D_ActivateRenderer(renderer);
slouken@7141
  1146
slouken@7141
  1147
    /* Release the previous render target if it wasn't the default one */
slouken@7141
  1148
    if (data->currentRenderTarget != NULL) {
slouken@7141
  1149
        IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@7141
  1150
        data->currentRenderTarget = NULL;
slouken@7141
  1151
    }
slouken@7141
  1152
slouken@7141
  1153
    if (texture == NULL) {
slouken@7141
  1154
        IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget);
slouken@7141
  1155
        return 0;
slouken@7141
  1156
    }
slouken@7141
  1157
slouken@7141
  1158
    texturedata = (D3D_TextureData *) texture->driverdata;
slouken@7141
  1159
    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget);
slouken@7141
  1160
    if(FAILED(result)) {
slouken@7141
  1161
        return D3D_SetError("GetSurfaceLevel()", result);
slouken@7141
  1162
    }
slouken@7141
  1163
    result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget);
slouken@7141
  1164
    if(FAILED(result)) {
slouken@7141
  1165
        return D3D_SetError("SetRenderTarget()", result);
slouken@7141
  1166
    }
slouken@7141
  1167
slouken@7141
  1168
    return 0;
slouken@7141
  1169
}
slouken@7141
  1170
slouken@7141
  1171
static int
slouken@5297
  1172
D3D_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
  1173
{
slouken@5224
  1174
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
  1175
    D3DVIEWPORT9 viewport;
slouken@5297
  1176
    D3DMATRIX matrix;
slouken@5224
  1177
slouken@5297
  1178
    /* Set the viewport */
slouken@5297
  1179
    viewport.X = renderer->viewport.x;
slouken@5297
  1180
    viewport.Y = renderer->viewport.y;
slouken@5297
  1181
    viewport.Width = renderer->viewport.w;
slouken@5297
  1182
    viewport.Height = renderer->viewport.h;
slouken@5297
  1183
    viewport.MinZ = 0.0f;
slouken@5297
  1184
    viewport.MaxZ = 1.0f;
slouken@5297
  1185
    IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5224
  1186
slouken@5297
  1187
    /* Set an orthographic projection matrix */
slouken@7239
  1188
    if (renderer->viewport.w && renderer->viewport.h) {
slouken@7239
  1189
        matrix.m[0][0] = 2.0f / renderer->viewport.w;
slouken@7239
  1190
        matrix.m[0][1] = 0.0f;
slouken@7239
  1191
        matrix.m[0][2] = 0.0f;
slouken@7239
  1192
        matrix.m[0][3] = 0.0f;
slouken@7239
  1193
        matrix.m[1][0] = 0.0f;
slouken@7239
  1194
        matrix.m[1][1] = -2.0f / renderer->viewport.h;
slouken@7239
  1195
        matrix.m[1][2] = 0.0f;
slouken@7239
  1196
        matrix.m[1][3] = 0.0f;
slouken@7239
  1197
        matrix.m[2][0] = 0.0f;
slouken@7239
  1198
        matrix.m[2][1] = 0.0f;
slouken@7239
  1199
        matrix.m[2][2] = 1.0f;
slouken@7239
  1200
        matrix.m[2][3] = 0.0f;
slouken@7239
  1201
        matrix.m[3][0] = -1.0f;
slouken@7239
  1202
        matrix.m[3][1] = 1.0f;
slouken@7239
  1203
        matrix.m[3][2] = 0.0f;
slouken@7239
  1204
        matrix.m[3][3] = 1.0f;
slouken@7239
  1205
        IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &matrix);
slouken@7239
  1206
    }
slouken@5297
  1207
slouken@5297
  1208
    return 0;
slouken@5297
  1209
}
slouken@5297
  1210
slouken@5297
  1211
static int
slouken@7141
  1212
D3D_UpdateClipRect(SDL_Renderer * renderer)
slouken@6246
  1213
{
slouken@7141
  1214
    const SDL_Rect *rect = &renderer->clip_rect;
slouken@6246
  1215
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7141
  1216
    RECT r;
slouken@6246
  1217
    HRESULT result;
slouken@6246
  1218
slouken@7141
  1219
    if (!SDL_RectEmpty(rect)) {
slouken@7141
  1220
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, TRUE);
slouken@7141
  1221
        r.left = rect->x;
slouken@7141
  1222
        r.top = rect->y;
slouken@7472
  1223
        r.right = rect->x + rect->w;
slouken@7141
  1224
        r.bottom = rect->y + rect->h;
slouken@6246
  1225
slouken@7141
  1226
        result = IDirect3DDevice9_SetScissorRect(data->device, &r);
slouken@7141
  1227
        if (result != D3D_OK) {
slouken@7141
  1228
            D3D_SetError("SetScissor()", result);
slouken@7141
  1229
            return -1;
slouken@7141
  1230
        }
slouken@7141
  1231
    } else {
slouken@7141
  1232
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, FALSE);
slouken@6246
  1233
    }
slouken@6246
  1234
    return 0;
slouken@6246
  1235
}
slouken@6246
  1236
slouken@6246
  1237
static int
slouken@5297
  1238
D3D_RenderClear(SDL_Renderer * renderer)
slouken@5297
  1239
{
slouken@5297
  1240
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
  1241
    DWORD color;
slouken@5297
  1242
    HRESULT result;
slouken@5297
  1243
slouken@5297
  1244
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1245
        return -1;
slouken@5224
  1246
    }
slouken@5297
  1247
slouken@5297
  1248
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@5297
  1249
slouken@5299
  1250
    /* Don't reset the viewport if we don't have to! */
slouken@5299
  1251
    if (!renderer->viewport.x && !renderer->viewport.y &&
slouken@5299
  1252
        renderer->viewport.w == data->pparams.BackBufferWidth &&
slouken@5299
  1253
        renderer->viewport.h == data->pparams.BackBufferHeight) {
slouken@5299
  1254
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
  1255
    } else {
slouken@5299
  1256
        D3DVIEWPORT9 viewport;
slouken@5297
  1257
slouken@5299
  1258
        /* Clear is defined to clear the entire render target */
slouken@5299
  1259
        viewport.X = 0;
slouken@5299
  1260
        viewport.Y = 0;
slouken@5299
  1261
        viewport.Width = data->pparams.BackBufferWidth;
slouken@5299
  1262
        viewport.Height = data->pparams.BackBufferHeight;
slouken@5299
  1263
        viewport.MinZ = 0.0f;
slouken@5299
  1264
        viewport.MaxZ = 1.0f;
slouken@5299
  1265
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
  1266
slouken@5299
  1267
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
  1268
slouken@5299
  1269
        /* Reset the viewport */
slouken@5299
  1270
        viewport.X = renderer->viewport.x;
slouken@5299
  1271
        viewport.Y = renderer->viewport.y;
slouken@5299
  1272
        viewport.Width = renderer->viewport.w;
slouken@5299
  1273
        viewport.Height = renderer->viewport.h;
slouken@5299
  1274
        viewport.MinZ = 0.0f;
slouken@5299
  1275
        viewport.MaxZ = 1.0f;
slouken@5299
  1276
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
  1277
    }
slouken@5297
  1278
slouken@5297
  1279
    if (FAILED(result)) {
icculus@7037
  1280
        return D3D_SetError("Clear()", result);
slouken@5297
  1281
    }
slouken@5297
  1282
    return 0;
slouken@5224
  1283
}
slouken@5224
  1284
slouken@5224
  1285
static void
slouken@2933
  1286
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
  1287
{
slouken@2932
  1288
    switch (blendMode) {
slouken@2932
  1289
    case SDL_BLENDMODE_NONE:
slouken@2932
  1290
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
  1291
                                        FALSE);
slouken@2932
  1292
        break;
slouken@2932
  1293
    case SDL_BLENDMODE_BLEND:
slouken@2932
  1294
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
  1295
                                        TRUE);
slouken@2932
  1296
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
  1297
                                        D3DBLEND_SRCALPHA);
slouken@2932
  1298
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
  1299
                                        D3DBLEND_INVSRCALPHA);
slouken@7502
  1300
        if (data->enableSeparateAlphaBlend) {
slouken@7502
  1301
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
slouken@7502
  1302
                                            D3DBLEND_ONE);
slouken@7502
  1303
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
slouken@7502
  1304
                                            D3DBLEND_INVSRCALPHA);
slouken@7502
  1305
        }
slouken@2932
  1306
        break;
slouken@2932
  1307
    case SDL_BLENDMODE_ADD:
slouken@2932
  1308
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
  1309
                                        TRUE);
slouken@2932
  1310
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
  1311
                                        D3DBLEND_SRCALPHA);
slouken@2932
  1312
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
  1313
                                        D3DBLEND_ONE);
slouken@7502
  1314
        if (data->enableSeparateAlphaBlend) {
slouken@7502
  1315
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
slouken@7502
  1316
                                            D3DBLEND_ZERO);
slouken@7502
  1317
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
slouken@7502
  1318
                                            D3DBLEND_ONE);
slouken@7502
  1319
        }
slouken@2932
  1320
        break;
slouken@5184
  1321
    case SDL_BLENDMODE_MOD:
slouken@5184
  1322
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@5184
  1323
                                        TRUE);
slouken@5184
  1324
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@5184
  1325
                                        D3DBLEND_ZERO);
slouken@5184
  1326
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@5184
  1327
                                        D3DBLEND_SRCCOLOR);
slouken@7502
  1328
        if (data->enableSeparateAlphaBlend) {
slouken@7502
  1329
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
slouken@7502
  1330
                                            D3DBLEND_ZERO);
slouken@7502
  1331
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
slouken@7502
  1332
                                            D3DBLEND_ONE);
slouken@7502
  1333
        }
slouken@5184
  1334
        break;
slouken@2932
  1335
    }
slouken@2932
  1336
}
slouken@2932
  1337
slouken@1895
  1338
static int
slouken@6528
  1339
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1340
                     int count)
slouken@1895
  1341
{
slouken@1913
  1342
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1343
    DWORD color;
slouken@3536
  1344
    Vertex *vertices;
slouken@3536
  1345
    int i;
slouken@1900
  1346
    HRESULT result;
slouken@1895
  1347
slouken@5297
  1348
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1349
        return -1;
slouken@1900
  1350
    }
slouken@1895
  1351
slouken@2932
  1352
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1353
slouken@2933
  1354
    result =
slouken@2933
  1355
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1356
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1357
    if (FAILED(result)) {
icculus@7037
  1358
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1359
    }
slouken@3536
  1360
slouken@3536
  1361
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1362
slouken@3536
  1363
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1364
    for (i = 0; i < count; ++i) {
slouken@6528
  1365
        vertices[i].x = points[i].x;
slouken@6528
  1366
        vertices[i].y = points[i].y;
slouken@3536
  1367
        vertices[i].z = 0.0f;
slouken@3536
  1368
        vertices[i].color = color;
slouken@3536
  1369
        vertices[i].u = 0.0f;
slouken@3536
  1370
        vertices[i].v = 0.0f;
slouken@3536
  1371
    }
slouken@1903
  1372
    result =
slouken@3536
  1373
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
  1374
                                         vertices, sizeof(*vertices));
slouken@3536
  1375
    SDL_stack_free(vertices);
slouken@1900
  1376
    if (FAILED(result)) {
icculus@7037
  1377
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1378
    }
slouken@2932
  1379
    return 0;
slouken@2932
  1380
}
slouken@2932
  1381
slouken@2932
  1382
static int
slouken@6528
  1383
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1384
                    int count)
slouken@2932
  1385
{
slouken@2932
  1386
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1387
    DWORD color;
slouken@3536
  1388
    Vertex *vertices;
slouken@3536
  1389
    int i;
slouken@2932
  1390
    HRESULT result;
slouken@2932
  1391
slouken@5297
  1392
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1393
        return -1;
slouken@2932
  1394
    }
slouken@2932
  1395
slouken@2932
  1396
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1397
slouken@2933
  1398
    result =
slouken@2933
  1399
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1400
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1401
    if (FAILED(result)) {
icculus@7037
  1402
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1403
    }
slouken@3536
  1404
slouken@3536
  1405
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1406
slouken@3536
  1407
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1408
    for (i = 0; i < count; ++i) {
slouken@6528
  1409
        vertices[i].x = points[i].x;
slouken@6528
  1410
        vertices[i].y = points[i].y;
slouken@3536
  1411
        vertices[i].z = 0.0f;
slouken@3536
  1412
        vertices[i].color = color;
slouken@3536
  1413
        vertices[i].u = 0.0f;
slouken@3536
  1414
        vertices[i].v = 0.0f;
slouken@3536
  1415
    }
slouken@2932
  1416
    result =
slouken@3551
  1417
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
  1418
                                         vertices, sizeof(*vertices));
slouken@3551
  1419
slouken@3551
  1420
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
  1421
       so we need to close the endpoint of the line */
slouken@6076
  1422
    if (count == 2 ||
slouken@6076
  1423
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@6528
  1424
        vertices[0].x = points[count-1].x;
slouken@6528
  1425
        vertices[0].y = points[count-1].y;
slouken@3551
  1426
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
  1427
    }
slouken@3551
  1428
slouken@3536
  1429
    SDL_stack_free(vertices);
slouken@2932
  1430
    if (FAILED(result)) {
icculus@7037
  1431
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1432
    }
slouken@2932
  1433
    return 0;
slouken@2932
  1434
}
slouken@2932
  1435
slouken@2932
  1436
static int
slouken@6528
  1437
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
slouken@3599
  1438
                    int count)
slouken@2932
  1439
{
slouken@2932
  1440
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
  1441
    DWORD color;
slouken@3536
  1442
    int i;
slouken@2932
  1443
    float minx, miny, maxx, maxy;
slouken@2932
  1444
    Vertex vertices[4];
slouken@2932
  1445
    HRESULT result;
slouken@2932
  1446
slouken@5297
  1447
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1448
        return -1;
slouken@2932
  1449
    }
slouken@2932
  1450
slouken@2932
  1451
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1452
slouken@2933
  1453
    result =
slouken@2933
  1454
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1455
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1456
    if (FAILED(result)) {
icculus@7037
  1457
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1458
    }
slouken@3536
  1459
slouken@3536
  1460
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1461
slouken@3536
  1462
    for (i = 0; i < count; ++i) {
slouken@6528
  1463
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1464
slouken@6528
  1465
        minx = rect->x;
slouken@6528
  1466
        miny = rect->y;
slouken@6528
  1467
        maxx = rect->x + rect->w;
slouken@6528
  1468
        maxy = rect->y + rect->h;
slouken@3536
  1469
slouken@3536
  1470
        vertices[0].x = minx;
slouken@3536
  1471
        vertices[0].y = miny;
slouken@3536
  1472
        vertices[0].z = 0.0f;
slouken@3536
  1473
        vertices[0].color = color;
slouken@3536
  1474
        vertices[0].u = 0.0f;
slouken@3536
  1475
        vertices[0].v = 0.0f;
slouken@3536
  1476
slouken@3536
  1477
        vertices[1].x = maxx;
slouken@3536
  1478
        vertices[1].y = miny;
slouken@3536
  1479
        vertices[1].z = 0.0f;
slouken@3536
  1480
        vertices[1].color = color;
slouken@3536
  1481
        vertices[1].u = 0.0f;
slouken@3536
  1482
        vertices[1].v = 0.0f;
slouken@3536
  1483
slouken@3536
  1484
        vertices[2].x = maxx;
slouken@3536
  1485
        vertices[2].y = maxy;
slouken@3536
  1486
        vertices[2].z = 0.0f;
slouken@3536
  1487
        vertices[2].color = color;
slouken@3536
  1488
        vertices[2].u = 0.0f;
slouken@3536
  1489
        vertices[2].v = 0.0f;
slouken@3536
  1490
slouken@3536
  1491
        vertices[3].x = minx;
slouken@3536
  1492
        vertices[3].y = maxy;
slouken@3536
  1493
        vertices[3].z = 0.0f;
slouken@3536
  1494
        vertices[3].color = color;
slouken@3536
  1495
        vertices[3].u = 0.0f;
slouken@3536
  1496
        vertices[3].v = 0.0f;
slouken@3536
  1497
slouken@3536
  1498
        result =
slouken@3536
  1499
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
  1500
                                             2, vertices, sizeof(*vertices));
slouken@3536
  1501
        if (FAILED(result)) {
icculus@7037
  1502
            return D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1503
        }
slouken@1900
  1504
    }
slouken@1895
  1505
    return 0;
slouken@1895
  1506
}
slouken@1895
  1507
slouken@7505
  1508
static void
slouken@7505
  1509
D3D_UpdateTextureScaleMode(D3D_RenderData *data, D3D_TextureData *texturedata, unsigned index)
slouken@7505
  1510
{
slouken@7505
  1511
    if (texturedata->scaleMode != data->scaleMode[index]) {
slouken@7505
  1512
        IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MINFILTER,
slouken@7505
  1513
                                         texturedata->scaleMode);
slouken@7505
  1514
        IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MAGFILTER,
slouken@7505
  1515
                                         texturedata->scaleMode);
slouken@7505
  1516
        data->scaleMode[index] = texturedata->scaleMode;
slouken@7505
  1517
    }
slouken@7505
  1518
}
slouken@7505
  1519
slouken@1895
  1520
static int
slouken@1913
  1521
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1522
               const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@1895
  1523
{
slouken@1913
  1524
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1525
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
  1526
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
  1527
    float minx, miny, maxx, maxy;
slouken@1904
  1528
    float minu, maxu, minv, maxv;
slouken@1987
  1529
    DWORD color;
slouken@1903
  1530
    Vertex vertices[4];
slouken@1903
  1531
    HRESULT result;
slouken@1895
  1532
slouken@5297
  1533
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1534
        return -1;
slouken@1900
  1535
    }
slouken@1903
  1536
slouken@6528
  1537
    minx = dstrect->x - 0.5f;
slouken@6528
  1538
    miny = dstrect->y - 0.5f;
slouken@6528
  1539
    maxx = dstrect->x + dstrect->w - 0.5f;
slouken@6528
  1540
    maxy = dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1541
slouken@1904
  1542
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1543
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1544
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1545
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1546
slouken@1987
  1547
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1548
slouken@1903
  1549
    vertices[0].x = minx;
slouken@1903
  1550
    vertices[0].y = miny;
slouken@1903
  1551
    vertices[0].z = 0.0f;
slouken@1987
  1552
    vertices[0].color = color;
slouken@1904
  1553
    vertices[0].u = minu;
slouken@1904
  1554
    vertices[0].v = minv;
slouken@1904
  1555
slouken@1903
  1556
    vertices[1].x = maxx;
slouken@1903
  1557
    vertices[1].y = miny;
slouken@1903
  1558
    vertices[1].z = 0.0f;
slouken@1987
  1559
    vertices[1].color = color;
slouken@1904
  1560
    vertices[1].u = maxu;
slouken@1904
  1561
    vertices[1].v = minv;
slouken@1904
  1562
slouken@1903
  1563
    vertices[2].x = maxx;
slouken@1903
  1564
    vertices[2].y = maxy;
slouken@1903
  1565
    vertices[2].z = 0.0f;
slouken@1987
  1566
    vertices[2].color = color;
slouken@1904
  1567
    vertices[2].u = maxu;
slouken@1904
  1568
    vertices[2].v = maxv;
slouken@1904
  1569
slouken@1903
  1570
    vertices[3].x = minx;
slouken@1903
  1571
    vertices[3].y = maxy;
slouken@1903
  1572
    vertices[3].z = 0.0f;
slouken@1987
  1573
    vertices[3].color = color;
slouken@1904
  1574
    vertices[3].u = minu;
slouken@1904
  1575
    vertices[3].v = maxv;
slouken@1903
  1576
slouken@2932
  1577
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1578
slouken@7505
  1579
    D3D_UpdateTextureScaleMode(data, texturedata, 0);
slouken@1917
  1580
slouken@1903
  1581
    result =
slouken@2735
  1582
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1583
                                    texturedata->texture);
slouken@1903
  1584
    if (FAILED(result)) {
icculus@7037
  1585
        return D3D_SetError("SetTexture()", result);
slouken@1903
  1586
    }
slouken@7505
  1587
slouken@7505
  1588
    if (texturedata->yuv) {
slouken@7505
  1589
        shader = data->ps_yuv;
slouken@7505
  1590
slouken@7505
  1591
        D3D_UpdateTextureScaleMode(data, texturedata, 1);
slouken@7505
  1592
        D3D_UpdateTextureScaleMode(data, texturedata, 2);
slouken@7505
  1593
slouken@7505
  1594
        result =
slouken@7505
  1595
            IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
slouken@7505
  1596
                                        texturedata->utexture);
slouken@7505
  1597
        if (FAILED(result)) {
slouken@7505
  1598
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1599
        }
slouken@7505
  1600
slouken@7505
  1601
        result =
slouken@7505
  1602
            IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
slouken@7505
  1603
                                        texturedata->vtexture);
slouken@7505
  1604
        if (FAILED(result)) {
slouken@7505
  1605
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1606
        }
slouken@7505
  1607
    }
slouken@7505
  1608
slouken@3556
  1609
    if (shader) {
slouken@3556
  1610
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
  1611
        if (FAILED(result)) {
icculus@7037
  1612
            return D3D_SetError("SetShader()", result);
slouken@3556
  1613
        }
slouken@3556
  1614
    }
slouken@1903
  1615
    result =
slouken@1903
  1616
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1617
                                         vertices, sizeof(*vertices));
slouken@1903
  1618
    if (FAILED(result)) {
icculus@7037
  1619
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1620
    }
slouken@3556
  1621
    if (shader) {
slouken@3556
  1622
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
  1623
        if (FAILED(result)) {
icculus@7037
  1624
            return D3D_SetError("SetShader()", result);
slouken@3556
  1625
        }
slouken@3556
  1626
    }
slouken@1895
  1627
    return 0;
slouken@1895
  1628
}
slouken@1895
  1629
gabomdq@6320
  1630
gabomdq@6320
  1631
static int
gabomdq@6320
  1632
D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1633
               const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1634
               const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
gabomdq@6320
  1635
{
gabomdq@6320
  1636
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
gabomdq@6320
  1637
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
gabomdq@6320
  1638
    LPDIRECT3DPIXELSHADER9 shader = NULL;
gabomdq@6320
  1639
    float minx, miny, maxx, maxy;
gabomdq@6320
  1640
    float minu, maxu, minv, maxv;
gabomdq@6320
  1641
    float centerx, centery;
gabomdq@6320
  1642
    DWORD color;
gabomdq@6320
  1643
    Vertex vertices[4];
gabomdq@6320
  1644
    HRESULT result;
gabomdq@6320
  1645
gabomdq@6320
  1646
    if (D3D_ActivateRenderer(renderer) < 0) {
gabomdq@6320
  1647
        return -1;
gabomdq@6320
  1648
    }
gabomdq@6320
  1649
slouken@6528
  1650
    centerx = center->x;
slouken@6528
  1651
    centery = center->y;
gabomdq@6320
  1652
gabomdq@6320
  1653
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@6528
  1654
        minx = dstrect->w - centerx - 0.5f;
slouken@6528
  1655
        maxx = -centerx - 0.5f;
gabomdq@6320
  1656
    }
gabomdq@6320
  1657
    else {
slouken@6528
  1658
        minx = -centerx - 0.5f;
slouken@6528
  1659
        maxx = dstrect->w - centerx - 0.5f;
gabomdq@6320
  1660
    }
gabomdq@6320
  1661
gabomdq@6320
  1662
    if (flip & SDL_FLIP_VERTICAL) {
slouken@6528
  1663
        miny = dstrect->h - centery - 0.5f;
slouken@6528
  1664
        maxy = -centery - 0.5f;
gabomdq@6320
  1665
    }
gabomdq@6320
  1666
    else {
slouken@6528
  1667
        miny = -centery - 0.5f;
slouken@6528
  1668
        maxy = dstrect->h - centery - 0.5f;
gabomdq@6320
  1669
    }
gabomdq@6320
  1670
gabomdq@6320
  1671
    minu = (float) srcrect->x / texture->w;
gabomdq@6320
  1672
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1673
    minv = (float) srcrect->y / texture->h;
gabomdq@6320
  1674
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1675
gabomdq@6320
  1676
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
gabomdq@6320
  1677
gabomdq@6320
  1678
    vertices[0].x = minx;
gabomdq@6320
  1679
    vertices[0].y = miny;
gabomdq@6320
  1680
    vertices[0].z = 0.0f;
gabomdq@6320
  1681
    vertices[0].color = color;
gabomdq@6320
  1682
    vertices[0].u = minu;
gabomdq@6320
  1683
    vertices[0].v = minv;
gabomdq@6320
  1684
gabomdq@6320
  1685
    vertices[1].x = maxx;
gabomdq@6320
  1686
    vertices[1].y = miny;
gabomdq@6320
  1687
    vertices[1].z = 0.0f;
gabomdq@6320
  1688
    vertices[1].color = color;
gabomdq@6320
  1689
    vertices[1].u = maxu;
gabomdq@6320
  1690
    vertices[1].v = minv;
gabomdq@6320
  1691
gabomdq@6320
  1692
    vertices[2].x = maxx;
gabomdq@6320
  1693
    vertices[2].y = maxy;
gabomdq@6320
  1694
    vertices[2].z = 0.0f;
gabomdq@6320
  1695
    vertices[2].color = color;
gabomdq@6320
  1696
    vertices[2].u = maxu;
gabomdq@6320
  1697
    vertices[2].v = maxv;
gabomdq@6320
  1698
gabomdq@6320
  1699
    vertices[3].x = minx;
gabomdq@6320
  1700
    vertices[3].y = maxy;
gabomdq@6320
  1701
    vertices[3].z = 0.0f;
gabomdq@6320
  1702
    vertices[3].color = color;
gabomdq@6320
  1703
    vertices[3].u = minu;
gabomdq@6320
  1704
    vertices[3].v = maxv;
gabomdq@6320
  1705
gabomdq@6320
  1706
    D3D_SetBlendMode(data, texture->blendMode);
gabomdq@6320
  1707
slouken@7191
  1708
    /* Rotate and translate */
gabomdq@6320
  1709
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1710
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
slouken@6332
  1711
    ID3DXMatrixStack_RotateYawPitchRoll(data->matrixStack, 0.0, 0.0, (float)(M_PI * (float) angle / 180.0f));
gabomdq@6320
  1712
    ID3DXMatrixStack_Translate(data->matrixStack, (float)dstrect->x + centerx, (float)dstrect->y + centery, (float)0.0);
gabomdq@6320
  1713
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1714
slouken@7505
  1715
    D3D_UpdateTextureScaleMode(data, texturedata, 0);
gabomdq@6320
  1716
gabomdq@6320
  1717
    result =
gabomdq@6320
  1718
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
gabomdq@6320
  1719
                                    texturedata->texture);
gabomdq@6320
  1720
    if (FAILED(result)) {
icculus@7037
  1721
        return D3D_SetError("SetTexture()", result);
gabomdq@6320
  1722
    }
slouken@7505
  1723
slouken@7505
  1724
    if (texturedata->yuv) {
slouken@7505
  1725
        shader = data->ps_yuv;
slouken@7505
  1726
slouken@7505
  1727
        D3D_UpdateTextureScaleMode(data, texturedata, 1);
slouken@7505
  1728
        D3D_UpdateTextureScaleMode(data, texturedata, 2);
slouken@7505
  1729
slouken@7505
  1730
        result =
slouken@7505
  1731
            IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
slouken@7505
  1732
                                        texturedata->utexture);
slouken@7505
  1733
        if (FAILED(result)) {
slouken@7505
  1734
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1735
        }
slouken@7505
  1736
slouken@7505
  1737
        result =
slouken@7505
  1738
            IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
slouken@7505
  1739
                                        texturedata->vtexture);
slouken@7505
  1740
        if (FAILED(result)) {
slouken@7505
  1741
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1742
        }
slouken@7505
  1743
    }
slouken@7505
  1744
gabomdq@6320
  1745
    if (shader) {
gabomdq@6320
  1746
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
gabomdq@6320
  1747
        if (FAILED(result)) {
icculus@7037
  1748
            return D3D_SetError("SetShader()", result);
gabomdq@6320
  1749
        }
gabomdq@6320
  1750
    }
gabomdq@6320
  1751
    result =
gabomdq@6320
  1752
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
gabomdq@6320
  1753
                                         vertices, sizeof(*vertices));
gabomdq@6320
  1754
    if (FAILED(result)) {
icculus@7037
  1755
        return D3D_SetError("DrawPrimitiveUP()", result);
gabomdq@6320
  1756
    }
gabomdq@6320
  1757
    if (shader) {
gabomdq@6320
  1758
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
gabomdq@6320
  1759
        if (FAILED(result)) {
icculus@7037
  1760
            return D3D_SetError("SetShader()", result);
gabomdq@6320
  1761
        }
gabomdq@6320
  1762
    }
gabomdq@6320
  1763
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1764
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1765
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
gabomdq@6320
  1766
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1767
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1768
    return 0;
gabomdq@6320
  1769
}
gabomdq@6320
  1770
slouken@3427
  1771
static int
slouken@3427
  1772
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1773
                     Uint32 format, void * pixels, int pitch)
slouken@3427
  1774
{
slouken@3549
  1775
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3549
  1776
    D3DSURFACE_DESC desc;
slouken@3549
  1777
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
  1778
    LPDIRECT3DSURFACE9 surface;
slouken@3549
  1779
    RECT d3drect;
slouken@3549
  1780
    D3DLOCKED_RECT locked;
slouken@3549
  1781
    HRESULT result;
slouken@3427
  1782
slouken@3549
  1783
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
  1784
    if (FAILED(result)) {
icculus@7037
  1785
        return D3D_SetError("GetBackBuffer()", result);
slouken@3549
  1786
    }
slouken@3427
  1787
slouken@3549
  1788
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
  1789
    if (FAILED(result)) {
slouken@3549
  1790
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1791
        return D3D_SetError("GetDesc()", result);
slouken@3549
  1792
    }
slouken@3427
  1793
slouken@3549
  1794
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
  1795
    if (FAILED(result)) {
slouken@3549
  1796
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1797
        return D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
  1798
    }
slouken@3427
  1799
slouken@3549
  1800
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
  1801
    if (FAILED(result)) {
slouken@3549
  1802
        IDirect3DSurface9_Release(surface);
slouken@3549
  1803
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1804
        return D3D_SetError("GetRenderTargetData()", result);
slouken@3549
  1805
    }
slouken@3427
  1806
slouken@3549
  1807
    d3drect.left = rect->x;
slouken@3549
  1808
    d3drect.right = rect->x + rect->w;
slouken@3549
  1809
    d3drect.top = rect->y;
slouken@3549
  1810
    d3drect.bottom = rect->y + rect->h;
slouken@3427
  1811
slouken@3549
  1812
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
  1813
    if (FAILED(result)) {
slouken@3549
  1814
        IDirect3DSurface9_Release(surface);
slouken@3549
  1815
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1816
        return D3D_SetError("LockRect()", result);
slouken@3549
  1817
    }
slouken@3427
  1818
slouken@3549
  1819
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
  1820
                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
slouken@3549
  1821
                      format, pixels, pitch);
slouken@3549
  1822
slouken@3549
  1823
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
  1824
slouken@3549
  1825
    IDirect3DSurface9_Release(surface);
slouken@3549
  1826
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1827
slouken@3549
  1828
    return 0;
slouken@3427
  1829
}
slouken@3427
  1830
slouken@1895
  1831
static void
slouken@1913
  1832
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1833
{
slouken@1913
  1834
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1835
    HRESULT result;
slouken@1900
  1836
slouken@1900
  1837
    if (!data->beginScene) {
slouken@1900
  1838
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1839
        data->beginScene = SDL_TRUE;
slouken@1900
  1840
    }
slouken@1900
  1841
slouken@1975
  1842
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1843
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1844
        /* We'll reset later */
slouken@1975
  1845
        return;
slouken@1975
  1846
    }
slouken@1975
  1847
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1848
        D3D_Reset(renderer);
slouken@1975
  1849
    }
slouken@1900
  1850
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1851
    if (FAILED(result)) {
slouken@1900
  1852
        D3D_SetError("Present()", result);
slouken@1900
  1853
    }
slouken@1895
  1854
}
slouken@1895
  1855
slouken@1895
  1856
static void
slouken@1913
  1857
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1858
{
slouken@1913
  1859
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1860
slouken@1895
  1861
    if (!data) {
slouken@1895
  1862
        return;
slouken@1895
  1863
    }
slouken@1903
  1864
    if (data->texture) {
slouken@1903
  1865
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1866
    }
slouken@7505
  1867
    if (data->utexture) {
slouken@7505
  1868
        IDirect3DTexture9_Release(data->utexture);
slouken@7505
  1869
    }
slouken@7505
  1870
    if (data->vtexture) {
slouken@7505
  1871
        IDirect3DTexture9_Release(data->vtexture);
slouken@7505
  1872
    }
slouken@7719
  1873
    SDL_free(data->pixels);
slouken@1895
  1874
    SDL_free(data);
slouken@1895
  1875
    texture->driverdata = NULL;
slouken@1895
  1876
}
slouken@1895
  1877
slouken@1975
  1878
static void
slouken@1913
  1879
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1880
{
slouken@1913
  1881
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1882
slouken@1895
  1883
    if (data) {
slouken@7191
  1884
        /* Release the render target */
slouken@6895
  1885
        if (data->defaultRenderTarget) {
slouken@6895
  1886
            IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6895
  1887
            data->defaultRenderTarget = NULL;
slouken@6895
  1888
        }
slouken@6232
  1889
        if (data->currentRenderTarget != NULL) {
slouken@6232
  1890
            IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6232
  1891
            data->currentRenderTarget = NULL;
slouken@6232
  1892
        }
icculus@7658
  1893
        if (data->ps_yuv) {
icculus@7658
  1894
            IDirect3DPixelShader9_Release(data->ps_yuv);
icculus@7658
  1895
        }
slouken@1900
  1896
        if (data->device) {
slouken@1900
  1897
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1898
        }
slouken@5154
  1899
        if (data->d3d) {
slouken@5154
  1900
            IDirect3D9_Release(data->d3d);
slouken@7183
  1901
            ID3DXMatrixStack_Release(data->matrixStack);
slouken@5154
  1902
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1903
        }
slouken@1895
  1904
        SDL_free(data);
slouken@1895
  1905
    }
slouken@1895
  1906
    SDL_free(renderer);
slouken@1895
  1907
}
slouken@1895
  1908
slouken@5226
  1909
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
slouken@1895
  1910
slouken@1895
  1911
/* vi: set ts=4 sw=4 expandtab: */