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