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