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