src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 28 Sep 2013 14:07:05 -0700
changeset 7763 fc802d707136
parent 7762 51abf7664304
child 7764 0f48b5f28668
permissions -rw-r--r--
Added platform specific call: SDL_RenderGetD3DDevice()
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@1900
   547
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   548
    IDirect3DSwapChain9 *chain;
slouken@1925
   549
    D3DCAPS9 caps;
slouken@5156
   550
    Uint32 window_flags;
slouken@5156
   551
    int w, h;
slouken@5156
   552
    SDL_DisplayMode fullscreen_mode;
gabomdq@6320
   553
    int d3dxVersion;
slouken@7191
   554
    char d3dxDLLFile[50];
slouken@7742
   555
	int displayIndex;
slouken@1895
   556
slouken@1920
   557
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   558
    if (!renderer) {
slouken@1895
   559
        SDL_OutOfMemory();
slouken@1895
   560
        return NULL;
slouken@1895
   561
    }
slouken@1895
   562
slouken@1920
   563
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   564
    if (!data) {
slouken@5154
   565
        SDL_free(renderer);
slouken@1895
   566
        SDL_OutOfMemory();
slouken@1895
   567
        return NULL;
slouken@1895
   568
    }
slouken@1895
   569
slouken@7742
   570
	if( D3D_LoadDLL( &data->d3dDLL, &data->d3d ) ) {
gabomdq@6320
   571
        for (d3dxVersion=50;d3dxVersion>0;d3dxVersion--) {
slouken@6971
   572
            LPTSTR dllName;
slouken@6971
   573
            SDL_snprintf(d3dxDLLFile, sizeof(d3dxDLLFile), "D3DX9_%02d.dll", d3dxVersion);
slouken@6971
   574
            dllName = WIN_UTF8ToString(d3dxDLLFile);
slouken@6971
   575
            data->d3dxDLL = (void *)LoadLibrary(dllName); /* not using SDL_LoadObject() as we want silently fail - no error message */
slouken@6971
   576
            SDL_free(dllName);
gabomdq@6320
   577
            if (data->d3dxDLL) {
gabomdq@6320
   578
                HRESULT (WINAPI *D3DXCreateMatrixStack) (DWORD Flags, LPD3DXMATRIXSTACK*  ppStack);
gabomdq@6320
   579
                D3DXCreateMatrixStack = (HRESULT (WINAPI *) (DWORD, LPD3DXMATRIXSTACK*)) SDL_LoadFunction(data->d3dxDLL, "D3DXCreateMatrixStack");
gabomdq@6320
   580
                if (D3DXCreateMatrixStack) {
gabomdq@6320
   581
                    D3DXCreateMatrixStack(0, &data->matrixStack);
gabomdq@6320
   582
                    break;
gabomdq@6320
   583
                }
gabomdq@6320
   584
            }
gabomdq@6320
   585
        }
gabomdq@6320
   586
gabomdq@6320
   587
        if (!data->matrixStack) {
gabomdq@6320
   588
            if (data->d3dxDLL) SDL_UnloadObject(data->d3dxDLL);
gabomdq@6320
   589
        }
slouken@5154
   590
    }
gabomdq@6320
   591
gabomdq@6320
   592
slouken@7191
   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@5154
   670
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   671
                                     D3DDEVTYPE_HAL,
slouken@5154
   672
                                     pparams.hDeviceWindow,
slouken@6115
   673
                                     D3DCREATE_FPU_PRESERVE | ((caps.
slouken@3197
   674
                                      DevCaps &
slouken@3197
   675
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   676
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@6115
   677
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING),
slouken@1900
   678
                                     &pparams, &data->device);
slouken@1900
   679
    if (FAILED(result)) {
slouken@1913
   680
        D3D_DestroyRenderer(renderer);
slouken@1900
   681
        D3D_SetError("CreateDevice()", result);
slouken@1900
   682
        return NULL;
slouken@1900
   683
    }
slouken@1900
   684
slouken@1907
   685
    /* Get presentation parameters to fill info */
slouken@1907
   686
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   687
    if (FAILED(result)) {
slouken@1913
   688
        D3D_DestroyRenderer(renderer);
slouken@1907
   689
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   690
        return NULL;
slouken@1907
   691
    }
slouken@1907
   692
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   693
    if (FAILED(result)) {
slouken@1907
   694
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   695
        D3D_DestroyRenderer(renderer);
slouken@1907
   696
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   697
        return NULL;
slouken@1907
   698
    }
slouken@1907
   699
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   700
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   701
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   702
    }
slouken@1975
   703
    data->pparams = pparams;
slouken@1907
   704
slouken@1925
   705
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   706
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   707
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@6246
   708
    if (caps.NumSimultaneousRTs >= 2) {
slouken@6246
   709
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6246
   710
    }
slouken@1918
   711
slouken@7502
   712
    if (caps.PrimitiveMiscCaps & D3DPMISCCAPS_SEPARATEALPHABLEND) {
slouken@7502
   713
        data->enableSeparateAlphaBlend = SDL_TRUE;
slouken@7502
   714
    }
slouken@7502
   715
slouken@6232
   716
    /* Store the default render target */
slouken@6232
   717
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
slouken@6232
   718
    data->currentRenderTarget = NULL;
slouken@6232
   719
slouken@7648
   720
    /* Set up parameters for rendering */
slouken@7648
   721
    D3D_InitRenderState(data);
slouken@5297
   722
slouken@7505
   723
    if (caps.MaxSimultaneousTextures >= 3)
slouken@7505
   724
    {
slouken@7505
   725
#ifdef ASSEMBLE_SHADER
slouken@7505
   726
        /* This shader was created by running the following HLSL through the fxc compiler
slouken@7505
   727
           and then tuning the generated assembly.
slouken@7505
   728
slouken@7505
   729
           fxc /T fx_4_0 /O3 /Gfa /Fc yuv.fxc yuv.fx
slouken@7505
   730
slouken@7505
   731
           --- yuv.fx ---
slouken@7505
   732
           Texture2D g_txY;
slouken@7505
   733
           Texture2D g_txU;
slouken@7505
   734
           Texture2D g_txV;
slouken@7505
   735
slouken@7505
   736
           SamplerState samLinear
slouken@7505
   737
           {
slouken@7505
   738
               Filter = ANISOTROPIC;
slouken@7505
   739
               AddressU = Clamp;
slouken@7505
   740
               AddressV = Clamp;
slouken@7505
   741
               MaxAnisotropy = 1;
slouken@7505
   742
           };
slouken@7505
   743
slouken@7505
   744
           struct VS_OUTPUT
slouken@7505
   745
           {
slouken@7505
   746
                float2 TextureUV  : TEXCOORD0;
slouken@7505
   747
           };
slouken@7505
   748
slouken@7505
   749
           struct PS_OUTPUT
slouken@7505
   750
           {
slouken@7505
   751
                float4 RGBAColor : SV_Target;
slouken@7505
   752
           };
slouken@7505
   753
slouken@7505
   754
           PS_OUTPUT YUV420( VS_OUTPUT In ) 
slouken@7505
   755
           {
slouken@7505
   756
               const float3 offset = {-0.0625, -0.5, -0.5};
slouken@7505
   757
               const float3 Rcoeff = {1.164,  0.000,  1.596};
slouken@7505
   758
               const float3 Gcoeff = {1.164, -0.391, -0.813};
slouken@7505
   759
               const float3 Bcoeff = {1.164,  2.018,  0.000};
slouken@7505
   760
slouken@7505
   761
               PS_OUTPUT Output;
slouken@7505
   762
               float2 TextureUV = In.TextureUV;
slouken@7505
   763
slouken@7505
   764
               float3 yuv;
slouken@7505
   765
               yuv.x = g_txY.Sample( samLinear, TextureUV ).r;
slouken@7505
   766
               yuv.y = g_txU.Sample( samLinear, TextureUV ).r;
slouken@7505
   767
               yuv.z = g_txV.Sample( samLinear, TextureUV ).r;
slouken@7505
   768
slouken@7505
   769
               yuv += offset;
slouken@7505
   770
               Output.RGBAColor.r = dot(yuv, Rcoeff);
slouken@7505
   771
               Output.RGBAColor.g = dot(yuv, Gcoeff);
slouken@7505
   772
               Output.RGBAColor.b = dot(yuv, Bcoeff);
slouken@7505
   773
               Output.RGBAColor.a = 1.0f;
slouken@7505
   774
slouken@7505
   775
               return Output;
slouken@7505
   776
           }
slouken@7505
   777
slouken@7505
   778
           technique10 RenderYUV420
slouken@7505
   779
           {
slouken@7505
   780
               pass P0
slouken@7505
   781
               {
slouken@7505
   782
                    SetPixelShader( CompileShader( ps_4_0_level_9_0, YUV420() ) );
slouken@7505
   783
               }
slouken@7505
   784
           }
slouken@7505
   785
        */
slouken@7505
   786
        const char *shader_text =
slouken@7505
   787
            "ps_2_0\n"
slouken@7505
   788
            "def c0, -0.0625, -0.5, -0.5, 1\n"
slouken@7505
   789
            "def c1, 1.16400003, 0, 1.59599996, 0\n"
slouken@7505
   790
            "def c2, 1.16400003, -0.391000003, -0.813000023, 0\n"
slouken@7505
   791
            "def c3, 1.16400003, 2.01799989, 0, 0\n"
slouken@7505
   792
            "dcl t0.xy\n"
slouken@7505
   793
            "dcl v0.xyzw\n"
slouken@7505
   794
            "dcl_2d s0\n"
slouken@7505
   795
            "dcl_2d s1\n"
slouken@7505
   796
            "dcl_2d s2\n"
slouken@7505
   797
            "texld r0, t0, s0\n"
slouken@7505
   798
            "texld r1, t0, s1\n"
slouken@7505
   799
            "texld r2, t0, s2\n"
slouken@7505
   800
            "mov r0.y, r1.x\n"
slouken@7505
   801
            "mov r0.z, r2.x\n"
slouken@7505
   802
            "add r0.xyz, r0, c0\n"
slouken@7505
   803
            "dp3 r1.x, r0, c1\n"
slouken@7505
   804
            "dp3 r1.y, r0, c2\n"
slouken@7505
   805
            "dp2add r1.z, r0, c3, c3.z\n"   /* Logically this is "dp3 r1.z, r0, c3" but the optimizer did its magic */
slouken@7505
   806
            "mov r1.w, c0.w\n"
slouken@7505
   807
            "mul r0, r1, v0\n"              /* Not in the HLSL, multiply by vertex color */
slouken@7505
   808
            "mov oC0, r0\n"
slouken@7505
   809
        ;
slouken@7505
   810
        LPD3DXBUFFER pCode;
slouken@7505
   811
        LPD3DXBUFFER pErrorMsgs;
slouken@7505
   812
        LPDWORD shader_data = NULL;
slouken@7505
   813
        DWORD   shader_size = 0;
slouken@7505
   814
        result = D3DXAssembleShader(shader_text, SDL_strlen(shader_text), NULL, NULL, 0, &pCode, &pErrorMsgs);
slouken@7505
   815
        if (!FAILED(result)) {
slouken@7505
   816
            shader_data = (DWORD*)pCode->lpVtbl->GetBufferPointer(pCode);
slouken@7505
   817
            shader_size = pCode->lpVtbl->GetBufferSize(pCode);
slouken@7505
   818
            PrintShaderData(shader_data, shader_size);
slouken@7505
   819
        } else {
slouken@7505
   820
            const char *error = (const char *)pErrorMsgs->lpVtbl->GetBufferPointer(pErrorMsgs);
slouken@7505
   821
            SDL_SetError("Couldn't assemble shader: %s", error);
slouken@7505
   822
        }
slouken@7505
   823
#else
slouken@7505
   824
        const DWORD shader_data[] = {
slouken@7505
   825
            0xffff0200, 0x05000051, 0xa00f0000, 0xbd800000, 0xbf000000, 0xbf000000,
slouken@7505
   826
            0x3f800000, 0x05000051, 0xa00f0001, 0x3f94fdf4, 0x00000000, 0x3fcc49ba,
slouken@7505
   827
            0x00000000, 0x05000051, 0xa00f0002, 0x3f94fdf4, 0xbec83127, 0xbf5020c5,
slouken@7505
   828
            0x00000000, 0x05000051, 0xa00f0003, 0x3f94fdf4, 0x400126e9, 0x00000000,
slouken@7505
   829
            0x00000000, 0x0200001f, 0x80000000, 0xb0030000, 0x0200001f, 0x80000000,
slouken@7505
   830
            0x900f0000, 0x0200001f, 0x90000000, 0xa00f0800, 0x0200001f, 0x90000000,
slouken@7505
   831
            0xa00f0801, 0x0200001f, 0x90000000, 0xa00f0802, 0x03000042, 0x800f0000,
slouken@7505
   832
            0xb0e40000, 0xa0e40800, 0x03000042, 0x800f0001, 0xb0e40000, 0xa0e40801,
slouken@7505
   833
            0x03000042, 0x800f0002, 0xb0e40000, 0xa0e40802, 0x02000001, 0x80020000,
slouken@7505
   834
            0x80000001, 0x02000001, 0x80040000, 0x80000002, 0x03000002, 0x80070000,
slouken@7505
   835
            0x80e40000, 0xa0e40000, 0x03000008, 0x80010001, 0x80e40000, 0xa0e40001,
slouken@7505
   836
            0x03000008, 0x80020001, 0x80e40000, 0xa0e40002, 0x0400005a, 0x80040001,
slouken@7505
   837
            0x80e40000, 0xa0e40003, 0xa0aa0003, 0x02000001, 0x80080001, 0xa0ff0000,
slouken@7505
   838
            0x03000005, 0x800f0000, 0x80e40001, 0x90e40000, 0x02000001, 0x800f0800,
slouken@7505
   839
            0x80e40000, 0x0000ffff
slouken@7505
   840
        };
slouken@7505
   841
#endif
slouken@7547
   842
        if (shader_data != NULL) {
slouken@7505
   843
            result = IDirect3DDevice9_CreatePixelShader(data->device, shader_data, &data->ps_yuv);
slouken@7505
   844
            if (!FAILED(result)) {
slouken@7505
   845
                renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_YV12;
slouken@7505
   846
                renderer->info.texture_formats[renderer->info.num_texture_formats++] = SDL_PIXELFORMAT_IYUV;
slouken@7505
   847
            } else {
slouken@7505
   848
                D3D_SetError("CreatePixelShader()", result);
slouken@7505
   849
            }
slouken@7505
   850
        }
slouken@7505
   851
    }
slouken@7505
   852
slouken@1895
   853
    return renderer;
slouken@1895
   854
}
slouken@1895
   855
slouken@5297
   856
static void
slouken@5297
   857
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1975
   858
{
slouken@1975
   859
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   860
slouken@5297
   861
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   862
        data->updateSize = SDL_TRUE;
slouken@1975
   863
    }
slouken@1975
   864
}
slouken@1975
   865
slouken@5484
   866
static D3DTEXTUREFILTERTYPE
slouken@5484
   867
GetScaleQuality(void)
slouken@5484
   868
{
slouken@5484
   869
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   870
slouken@5484
   871
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   872
        return D3DTEXF_POINT;
gabomdq@7678
   873
    } else /* if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) */ {
slouken@5484
   874
        return D3DTEXF_LINEAR;
slouken@5484
   875
    }
slouken@5484
   876
}
slouken@5484
   877
slouken@1975
   878
static int
slouken@1913
   879
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   880
{
icculus@6404
   881
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   882
    D3D_TextureData *data;
slouken@5173
   883
    D3DPOOL pool;
slouken@5173
   884
    DWORD usage;
slouken@1903
   885
    HRESULT result;
slouken@1895
   886
slouken@1920
   887
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   888
    if (!data) {
icculus@7037
   889
        return SDL_OutOfMemory();
slouken@1895
   890
    }
slouken@5484
   891
    data->scaleMode = GetScaleQuality();
slouken@1895
   892
slouken@1895
   893
    texture->driverdata = data;
slouken@1895
   894
slouken@5173
   895
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   896
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   897
        pool = D3DPOOL_DEFAULT;
slouken@5173
   898
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   899
    } else
slouken@5173
   900
#endif
slouken@6232
   901
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6246
   902
        /* D3DPOOL_MANAGED does not work with D3DUSAGE_RENDERTARGET */
slouken@6246
   903
        pool = D3DPOOL_DEFAULT;
slouken@6232
   904
        usage = D3DUSAGE_RENDERTARGET;
slouken@6246
   905
    } else {
slouken@5173
   906
        pool = D3DPOOL_MANAGED;
slouken@5173
   907
        usage = 0;
slouken@5173
   908
    }
slouken@2973
   909
slouken@1903
   910
    result =
slouken@1903
   911
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   912
                                       texture->h, 1, usage,
slouken@5173
   913
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   914
                                       pool, &data->texture, NULL);
slouken@1903
   915
    if (FAILED(result)) {
icculus@7037
   916
        return D3D_SetError("CreateTexture()", result);
slouken@1903
   917
    }
slouken@1903
   918
slouken@7505
   919
    if (texture->format == SDL_PIXELFORMAT_YV12 ||
slouken@7505
   920
        texture->format == SDL_PIXELFORMAT_IYUV) {
slouken@7505
   921
        data->yuv = SDL_TRUE;
slouken@7505
   922
slouken@7505
   923
        result =
slouken@7505
   924
            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w / 2,
slouken@7505
   925
                                           texture->h / 2, 1, usage,
slouken@7505
   926
                                           PixelFormatToD3DFMT(texture->format),
slouken@7505
   927
                                           pool, &data->utexture, NULL);
slouken@7505
   928
        if (FAILED(result)) {
slouken@7505
   929
            return D3D_SetError("CreateTexture()", result);
slouken@7505
   930
        }
slouken@7505
   931
slouken@7505
   932
        result =
slouken@7505
   933
            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w / 2,
slouken@7505
   934
                                           texture->h / 2, 1, usage,
slouken@7505
   935
                                           PixelFormatToD3DFMT(texture->format),
slouken@7505
   936
                                           pool, &data->vtexture, NULL);
slouken@7505
   937
        if (FAILED(result)) {
slouken@7505
   938
            return D3D_SetError("CreateTexture()", result);
slouken@7505
   939
        }
slouken@7505
   940
    }
slouken@7505
   941
slouken@7505
   942
    return 0;
slouken@7505
   943
}
slouken@7505
   944
slouken@7505
   945
static int
slouken@7505
   946
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
   947
{
slouken@7505
   948
    RECT d3drect;
slouken@7505
   949
    D3DLOCKED_RECT locked;
slouken@7505
   950
    const Uint8 *src;
slouken@7505
   951
    Uint8 *dst;
slouken@7505
   952
    int row, length;
slouken@7505
   953
    HRESULT result;
slouken@7505
   954
slouken@7505
   955
    if (full_texture) {
slouken@7505
   956
        result = IDirect3DTexture9_LockRect(texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@7505
   957
    } else {
slouken@7505
   958
        d3drect.left = x;
slouken@7505
   959
        d3drect.right = x + w;
slouken@7505
   960
        d3drect.top = y;
slouken@7505
   961
        d3drect.bottom = y + h;
slouken@7505
   962
        result = IDirect3DTexture9_LockRect(texture, 0, &locked, &d3drect, 0);
slouken@7505
   963
    }
slouken@7505
   964
slouken@7505
   965
    if (FAILED(result)) {
slouken@7505
   966
        return D3D_SetError("LockRect()", result);
slouken@7505
   967
    }
slouken@7505
   968
slouken@7505
   969
    src = (const Uint8 *)pixels;
slouken@7505
   970
    dst = locked.pBits;
slouken@7505
   971
    length = w * SDL_BYTESPERPIXEL(format);
slouken@7505
   972
    if (length == pitch && length == locked.Pitch) {
slouken@7505
   973
        SDL_memcpy(dst, src, length*h);
slouken@7505
   974
    } else {
slouken@7505
   975
        for (row = 0; row < h; ++row) {
slouken@7505
   976
            SDL_memcpy(dst, src, length);
slouken@7505
   977
            src += pitch;
slouken@7505
   978
            dst += locked.Pitch;
slouken@7505
   979
        }
slouken@7505
   980
    }
slouken@7505
   981
    IDirect3DTexture9_UnlockRect(texture, 0);
slouken@7505
   982
slouken@1895
   983
    return 0;
slouken@1895
   984
}
slouken@1895
   985
slouken@1895
   986
static int
slouken@1913
   987
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   988
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   989
{
slouken@1913
   990
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@7505
   991
    SDL_bool full_texture = SDL_FALSE;
slouken@2973
   992
slouken@5173
   993
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   994
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
   995
        rect->x == 0 && rect->y == 0 &&
slouken@5173
   996
        rect->w == texture->w && rect->h == texture->h) {
slouken@7505
   997
        full_texture = SDL_TRUE;
slouken@7505
   998
    }
slouken@5173
   999
#endif
slouken@7505
  1000
slouken@7505
  1001
    if (D3D_UpdateTextureInternal(data->texture, texture->format, full_texture, rect->x, rect->y, rect->w, rect->h, pixels, pitch) < 0) {
slouken@7505
  1002
        return -1;
slouken@5156
  1003
    }
slouken@2973
  1004
slouken@7505
  1005
    if (data->yuv) {
slouken@7505
  1006
        /* Skip to the correct offset into the next texture */
slouken@7505
  1007
        pixels = (const void*)((const Uint8*)pixels + rect->h * pitch);
slouken@2973
  1008
slouken@7505
  1009
        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
  1010
            return -1;
slouken@7505
  1011
        }
slouken@7505
  1012
slouken@7505
  1013
        /* Skip to the correct offset into the next texture */
slouken@7505
  1014
        pixels = (const void*)((const Uint8*)pixels + (rect->h * pitch)/4);
slouken@7505
  1015
        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
  1016
            return -1;
slouken@5173
  1017
        }
slouken@5156
  1018
    }
slouken@5156
  1019
    return 0;
slouken@1895
  1020
}
slouken@1895
  1021
slouken@1895
  1022
static int
slouken@7761
  1023
D3D_UpdateTextureYUV(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@7761
  1024
                     const SDL_Rect * rect,
slouken@7761
  1025
                     const Uint8 *Yplane, int Ypitch,
slouken@7761
  1026
                     const Uint8 *Uplane, int Upitch,
slouken@7761
  1027
                     const Uint8 *Vplane, int Vpitch)
slouken@7761
  1028
{
slouken@7761
  1029
	D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@7761
  1030
	SDL_bool full_texture = SDL_FALSE;
slouken@7761
  1031
slouken@7761
  1032
#ifdef USE_DYNAMIC_TEXTURE
slouken@7761
  1033
	if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@7761
  1034
		rect->x == 0 && rect->y == 0 &&
slouken@7761
  1035
		rect->w == texture->w && rect->h == texture->h) {
slouken@7761
  1036
			full_texture = SDL_TRUE;
slouken@7761
  1037
	}
slouken@7761
  1038
#endif
slouken@7761
  1039
slouken@7761
  1040
	if (D3D_UpdateTextureInternal(data->texture, texture->format, full_texture, rect->x, rect->y, rect->w, rect->h, Yplane, Ypitch) < 0) {
slouken@7761
  1041
		return -1;
slouken@7761
  1042
	}
slouken@7761
  1043
	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
  1044
		return -1;
slouken@7761
  1045
	}
slouken@7761
  1046
	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
  1047
		return -1;
slouken@7761
  1048
	}
slouken@7761
  1049
	return 0;
slouken@7761
  1050
}
slouken@7761
  1051
slouken@7761
  1052
static int
slouken@1913
  1053
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
  1054
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
  1055
{
slouken@1913
  1056
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
  1057
    RECT d3drect;
slouken@5156
  1058
    D3DLOCKED_RECT locked;
slouken@5156
  1059
    HRESULT result;
slouken@1895
  1060
slouken@7505
  1061
    if (data->yuv) {
gabomdq@7663
  1062
        /* It's more efficient to upload directly... */
slouken@7505
  1063
        if (!data->pixels) {
slouken@7505
  1064
            data->pitch = texture->w;
slouken@7505
  1065
            data->pixels = (Uint8 *)SDL_malloc((texture->h * data->pitch * 3) / 2);
slouken@7505
  1066
            if (!data->pixels) {
slouken@7505
  1067
                return SDL_OutOfMemory();
slouken@7505
  1068
            }
slouken@7505
  1069
        }
slouken@7505
  1070
        data->locked_rect = *rect;
slouken@7505
  1071
        *pixels =
slouken@7505
  1072
            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@7505
  1073
                      rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@7505
  1074
        *pitch = data->pitch;
slouken@7505
  1075
    } else {
slouken@7505
  1076
        d3drect.left = rect->x;
slouken@7505
  1077
        d3drect.right = rect->x + rect->w;
slouken@7505
  1078
        d3drect.top = rect->y;
slouken@7505
  1079
        d3drect.bottom = rect->y + rect->h;
slouken@1903
  1080
slouken@7505
  1081
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@7505
  1082
        if (FAILED(result)) {
slouken@7505
  1083
            return D3D_SetError("LockRect()", result);
slouken@7505
  1084
        }
slouken@7505
  1085
        *pixels = locked.pBits;
slouken@7505
  1086
        *pitch = locked.Pitch;
slouken@1903
  1087
    }
slouken@5156
  1088
    return 0;
slouken@1895
  1089
}
slouken@1895
  1090
slouken@1895
  1091
static void
slouken@1913
  1092
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1093
{
slouken@1913
  1094
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1095
slouken@7505
  1096
    if (data->yuv) {
slouken@7505
  1097
        const SDL_Rect *rect = &data->locked_rect;
slouken@7505
  1098
        void *pixels =
slouken@7505
  1099
            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@7505
  1100
                      rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@7505
  1101
        D3D_UpdateTexture(renderer, texture, rect, pixels, data->pitch);
slouken@7505
  1102
    } else {
slouken@7505
  1103
        IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@7505
  1104
    }
slouken@1895
  1105
}
slouken@1895
  1106
slouken@5297
  1107
static int
slouken@7141
  1108
D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@7141
  1109
{
slouken@7141
  1110
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7141
  1111
    D3D_TextureData *texturedata;
slouken@7141
  1112
    HRESULT result;
slouken@7141
  1113
slouken@7141
  1114
    D3D_ActivateRenderer(renderer);
slouken@7141
  1115
slouken@7141
  1116
    /* Release the previous render target if it wasn't the default one */
slouken@7141
  1117
    if (data->currentRenderTarget != NULL) {
slouken@7141
  1118
        IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@7141
  1119
        data->currentRenderTarget = NULL;
slouken@7141
  1120
    }
slouken@7141
  1121
slouken@7141
  1122
    if (texture == NULL) {
slouken@7141
  1123
        IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget);
slouken@7141
  1124
        return 0;
slouken@7141
  1125
    }
slouken@7141
  1126
slouken@7141
  1127
    texturedata = (D3D_TextureData *) texture->driverdata;
slouken@7141
  1128
    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget);
slouken@7141
  1129
    if(FAILED(result)) {
slouken@7141
  1130
        return D3D_SetError("GetSurfaceLevel()", result);
slouken@7141
  1131
    }
slouken@7141
  1132
    result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget);
slouken@7141
  1133
    if(FAILED(result)) {
slouken@7141
  1134
        return D3D_SetError("SetRenderTarget()", result);
slouken@7141
  1135
    }
slouken@7141
  1136
slouken@7141
  1137
    return 0;
slouken@7141
  1138
}
slouken@7141
  1139
slouken@7141
  1140
static int
slouken@5297
  1141
D3D_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
  1142
{
slouken@5224
  1143
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
  1144
    D3DVIEWPORT9 viewport;
slouken@5297
  1145
    D3DMATRIX matrix;
slouken@5224
  1146
slouken@5297
  1147
    /* Set the viewport */
slouken@5297
  1148
    viewport.X = renderer->viewport.x;
slouken@5297
  1149
    viewport.Y = renderer->viewport.y;
slouken@5297
  1150
    viewport.Width = renderer->viewport.w;
slouken@5297
  1151
    viewport.Height = renderer->viewport.h;
slouken@5297
  1152
    viewport.MinZ = 0.0f;
slouken@5297
  1153
    viewport.MaxZ = 1.0f;
slouken@5297
  1154
    IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5224
  1155
slouken@5297
  1156
    /* Set an orthographic projection matrix */
slouken@7239
  1157
    if (renderer->viewport.w && renderer->viewport.h) {
slouken@7239
  1158
        matrix.m[0][0] = 2.0f / renderer->viewport.w;
slouken@7239
  1159
        matrix.m[0][1] = 0.0f;
slouken@7239
  1160
        matrix.m[0][2] = 0.0f;
slouken@7239
  1161
        matrix.m[0][3] = 0.0f;
slouken@7239
  1162
        matrix.m[1][0] = 0.0f;
slouken@7239
  1163
        matrix.m[1][1] = -2.0f / renderer->viewport.h;
slouken@7239
  1164
        matrix.m[1][2] = 0.0f;
slouken@7239
  1165
        matrix.m[1][3] = 0.0f;
slouken@7239
  1166
        matrix.m[2][0] = 0.0f;
slouken@7239
  1167
        matrix.m[2][1] = 0.0f;
slouken@7239
  1168
        matrix.m[2][2] = 1.0f;
slouken@7239
  1169
        matrix.m[2][3] = 0.0f;
slouken@7239
  1170
        matrix.m[3][0] = -1.0f;
slouken@7239
  1171
        matrix.m[3][1] = 1.0f;
slouken@7239
  1172
        matrix.m[3][2] = 0.0f;
slouken@7239
  1173
        matrix.m[3][3] = 1.0f;
slouken@7239
  1174
        IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &matrix);
slouken@7239
  1175
    }
slouken@5297
  1176
slouken@5297
  1177
    return 0;
slouken@5297
  1178
}
slouken@5297
  1179
slouken@5297
  1180
static int
slouken@7141
  1181
D3D_UpdateClipRect(SDL_Renderer * renderer)
slouken@6246
  1182
{
slouken@7141
  1183
    const SDL_Rect *rect = &renderer->clip_rect;
slouken@6246
  1184
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7141
  1185
    RECT r;
slouken@6246
  1186
    HRESULT result;
slouken@6246
  1187
slouken@7141
  1188
    if (!SDL_RectEmpty(rect)) {
slouken@7141
  1189
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, TRUE);
slouken@7141
  1190
        r.left = rect->x;
slouken@7141
  1191
        r.top = rect->y;
slouken@7472
  1192
        r.right = rect->x + rect->w;
slouken@7141
  1193
        r.bottom = rect->y + rect->h;
slouken@6246
  1194
slouken@7141
  1195
        result = IDirect3DDevice9_SetScissorRect(data->device, &r);
slouken@7141
  1196
        if (result != D3D_OK) {
slouken@7141
  1197
            D3D_SetError("SetScissor()", result);
slouken@7141
  1198
            return -1;
slouken@7141
  1199
        }
slouken@7141
  1200
    } else {
slouken@7141
  1201
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SCISSORTESTENABLE, FALSE);
slouken@6246
  1202
    }
slouken@6246
  1203
    return 0;
slouken@6246
  1204
}
slouken@6246
  1205
slouken@6246
  1206
static int
slouken@5297
  1207
D3D_RenderClear(SDL_Renderer * renderer)
slouken@5297
  1208
{
slouken@5297
  1209
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
  1210
    DWORD color;
slouken@5297
  1211
    HRESULT result;
slouken@5297
  1212
slouken@5297
  1213
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1214
        return -1;
slouken@5224
  1215
    }
slouken@5297
  1216
slouken@5297
  1217
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@5297
  1218
slouken@5299
  1219
    /* Don't reset the viewport if we don't have to! */
slouken@5299
  1220
    if (!renderer->viewport.x && !renderer->viewport.y &&
slouken@5299
  1221
        renderer->viewport.w == data->pparams.BackBufferWidth &&
slouken@5299
  1222
        renderer->viewport.h == data->pparams.BackBufferHeight) {
slouken@5299
  1223
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
  1224
    } else {
slouken@5299
  1225
        D3DVIEWPORT9 viewport;
slouken@5297
  1226
slouken@5299
  1227
        /* Clear is defined to clear the entire render target */
slouken@5299
  1228
        viewport.X = 0;
slouken@5299
  1229
        viewport.Y = 0;
slouken@5299
  1230
        viewport.Width = data->pparams.BackBufferWidth;
slouken@5299
  1231
        viewport.Height = data->pparams.BackBufferHeight;
slouken@5299
  1232
        viewport.MinZ = 0.0f;
slouken@5299
  1233
        viewport.MaxZ = 1.0f;
slouken@5299
  1234
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
  1235
slouken@5299
  1236
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
  1237
slouken@5299
  1238
        /* Reset the viewport */
slouken@5299
  1239
        viewport.X = renderer->viewport.x;
slouken@5299
  1240
        viewport.Y = renderer->viewport.y;
slouken@5299
  1241
        viewport.Width = renderer->viewport.w;
slouken@5299
  1242
        viewport.Height = renderer->viewport.h;
slouken@5299
  1243
        viewport.MinZ = 0.0f;
slouken@5299
  1244
        viewport.MaxZ = 1.0f;
slouken@5299
  1245
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
  1246
    }
slouken@5297
  1247
slouken@5297
  1248
    if (FAILED(result)) {
icculus@7037
  1249
        return D3D_SetError("Clear()", result);
slouken@5297
  1250
    }
slouken@5297
  1251
    return 0;
slouken@5224
  1252
}
slouken@5224
  1253
slouken@5224
  1254
static void
slouken@2933
  1255
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
  1256
{
slouken@2932
  1257
    switch (blendMode) {
slouken@2932
  1258
    case SDL_BLENDMODE_NONE:
slouken@2932
  1259
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
  1260
                                        FALSE);
slouken@2932
  1261
        break;
slouken@2932
  1262
    case SDL_BLENDMODE_BLEND:
slouken@2932
  1263
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
  1264
                                        TRUE);
slouken@2932
  1265
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
  1266
                                        D3DBLEND_SRCALPHA);
slouken@2932
  1267
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
  1268
                                        D3DBLEND_INVSRCALPHA);
slouken@7502
  1269
        if (data->enableSeparateAlphaBlend) {
slouken@7502
  1270
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
slouken@7502
  1271
                                            D3DBLEND_ONE);
slouken@7502
  1272
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
slouken@7502
  1273
                                            D3DBLEND_INVSRCALPHA);
slouken@7502
  1274
        }
slouken@2932
  1275
        break;
slouken@2932
  1276
    case SDL_BLENDMODE_ADD:
slouken@2932
  1277
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
  1278
                                        TRUE);
slouken@2932
  1279
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
  1280
                                        D3DBLEND_SRCALPHA);
slouken@2932
  1281
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
  1282
                                        D3DBLEND_ONE);
slouken@7502
  1283
        if (data->enableSeparateAlphaBlend) {
slouken@7502
  1284
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
slouken@7502
  1285
                                            D3DBLEND_ZERO);
slouken@7502
  1286
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
slouken@7502
  1287
                                            D3DBLEND_ONE);
slouken@7502
  1288
        }
slouken@2932
  1289
        break;
slouken@5184
  1290
    case SDL_BLENDMODE_MOD:
slouken@5184
  1291
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@5184
  1292
                                        TRUE);
slouken@5184
  1293
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@5184
  1294
                                        D3DBLEND_ZERO);
slouken@5184
  1295
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@5184
  1296
                                        D3DBLEND_SRCCOLOR);
slouken@7502
  1297
        if (data->enableSeparateAlphaBlend) {
slouken@7502
  1298
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLENDALPHA,
slouken@7502
  1299
                                            D3DBLEND_ZERO);
slouken@7502
  1300
            IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLENDALPHA,
slouken@7502
  1301
                                            D3DBLEND_ONE);
slouken@7502
  1302
        }
slouken@5184
  1303
        break;
slouken@2932
  1304
    }
slouken@2932
  1305
}
slouken@2932
  1306
slouken@1895
  1307
static int
slouken@6528
  1308
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1309
                     int count)
slouken@1895
  1310
{
slouken@1913
  1311
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1312
    DWORD color;
slouken@3536
  1313
    Vertex *vertices;
slouken@3536
  1314
    int i;
slouken@1900
  1315
    HRESULT result;
slouken@1895
  1316
slouken@5297
  1317
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1318
        return -1;
slouken@1900
  1319
    }
slouken@1895
  1320
slouken@2932
  1321
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1322
slouken@2933
  1323
    result =
slouken@2933
  1324
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1325
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1326
    if (FAILED(result)) {
icculus@7037
  1327
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1328
    }
slouken@3536
  1329
slouken@3536
  1330
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1331
slouken@3536
  1332
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1333
    for (i = 0; i < count; ++i) {
slouken@6528
  1334
        vertices[i].x = points[i].x;
slouken@6528
  1335
        vertices[i].y = points[i].y;
slouken@3536
  1336
        vertices[i].z = 0.0f;
slouken@3536
  1337
        vertices[i].color = color;
slouken@3536
  1338
        vertices[i].u = 0.0f;
slouken@3536
  1339
        vertices[i].v = 0.0f;
slouken@3536
  1340
    }
slouken@1903
  1341
    result =
slouken@3536
  1342
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
  1343
                                         vertices, sizeof(*vertices));
slouken@3536
  1344
    SDL_stack_free(vertices);
slouken@1900
  1345
    if (FAILED(result)) {
icculus@7037
  1346
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1347
    }
slouken@2932
  1348
    return 0;
slouken@2932
  1349
}
slouken@2932
  1350
slouken@2932
  1351
static int
slouken@6528
  1352
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1353
                    int count)
slouken@2932
  1354
{
slouken@2932
  1355
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1356
    DWORD color;
slouken@3536
  1357
    Vertex *vertices;
slouken@3536
  1358
    int i;
slouken@2932
  1359
    HRESULT result;
slouken@2932
  1360
slouken@5297
  1361
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1362
        return -1;
slouken@2932
  1363
    }
slouken@2932
  1364
slouken@2932
  1365
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1366
slouken@2933
  1367
    result =
slouken@2933
  1368
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1369
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1370
    if (FAILED(result)) {
icculus@7037
  1371
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1372
    }
slouken@3536
  1373
slouken@3536
  1374
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1375
slouken@3536
  1376
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1377
    for (i = 0; i < count; ++i) {
slouken@6528
  1378
        vertices[i].x = points[i].x;
slouken@6528
  1379
        vertices[i].y = points[i].y;
slouken@3536
  1380
        vertices[i].z = 0.0f;
slouken@3536
  1381
        vertices[i].color = color;
slouken@3536
  1382
        vertices[i].u = 0.0f;
slouken@3536
  1383
        vertices[i].v = 0.0f;
slouken@3536
  1384
    }
slouken@2932
  1385
    result =
slouken@3551
  1386
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
  1387
                                         vertices, sizeof(*vertices));
slouken@3551
  1388
slouken@3551
  1389
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
  1390
       so we need to close the endpoint of the line */
slouken@6076
  1391
    if (count == 2 ||
slouken@6076
  1392
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@6528
  1393
        vertices[0].x = points[count-1].x;
slouken@6528
  1394
        vertices[0].y = points[count-1].y;
slouken@3551
  1395
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
  1396
    }
slouken@3551
  1397
slouken@3536
  1398
    SDL_stack_free(vertices);
slouken@2932
  1399
    if (FAILED(result)) {
icculus@7037
  1400
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1401
    }
slouken@2932
  1402
    return 0;
slouken@2932
  1403
}
slouken@2932
  1404
slouken@2932
  1405
static int
slouken@6528
  1406
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
slouken@3599
  1407
                    int count)
slouken@2932
  1408
{
slouken@2932
  1409
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
  1410
    DWORD color;
slouken@3536
  1411
    int i;
slouken@2932
  1412
    float minx, miny, maxx, maxy;
slouken@2932
  1413
    Vertex vertices[4];
slouken@2932
  1414
    HRESULT result;
slouken@2932
  1415
slouken@5297
  1416
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1417
        return -1;
slouken@2932
  1418
    }
slouken@2932
  1419
slouken@2932
  1420
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1421
slouken@2933
  1422
    result =
slouken@2933
  1423
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1424
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1425
    if (FAILED(result)) {
icculus@7037
  1426
        return D3D_SetError("SetTexture()", result);
slouken@2932
  1427
    }
slouken@3536
  1428
slouken@3536
  1429
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1430
slouken@3536
  1431
    for (i = 0; i < count; ++i) {
slouken@6528
  1432
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1433
slouken@6528
  1434
        minx = rect->x;
slouken@6528
  1435
        miny = rect->y;
slouken@6528
  1436
        maxx = rect->x + rect->w;
slouken@6528
  1437
        maxy = rect->y + rect->h;
slouken@3536
  1438
slouken@3536
  1439
        vertices[0].x = minx;
slouken@3536
  1440
        vertices[0].y = miny;
slouken@3536
  1441
        vertices[0].z = 0.0f;
slouken@3536
  1442
        vertices[0].color = color;
slouken@3536
  1443
        vertices[0].u = 0.0f;
slouken@3536
  1444
        vertices[0].v = 0.0f;
slouken@3536
  1445
slouken@3536
  1446
        vertices[1].x = maxx;
slouken@3536
  1447
        vertices[1].y = miny;
slouken@3536
  1448
        vertices[1].z = 0.0f;
slouken@3536
  1449
        vertices[1].color = color;
slouken@3536
  1450
        vertices[1].u = 0.0f;
slouken@3536
  1451
        vertices[1].v = 0.0f;
slouken@3536
  1452
slouken@3536
  1453
        vertices[2].x = maxx;
slouken@3536
  1454
        vertices[2].y = maxy;
slouken@3536
  1455
        vertices[2].z = 0.0f;
slouken@3536
  1456
        vertices[2].color = color;
slouken@3536
  1457
        vertices[2].u = 0.0f;
slouken@3536
  1458
        vertices[2].v = 0.0f;
slouken@3536
  1459
slouken@3536
  1460
        vertices[3].x = minx;
slouken@3536
  1461
        vertices[3].y = maxy;
slouken@3536
  1462
        vertices[3].z = 0.0f;
slouken@3536
  1463
        vertices[3].color = color;
slouken@3536
  1464
        vertices[3].u = 0.0f;
slouken@3536
  1465
        vertices[3].v = 0.0f;
slouken@3536
  1466
slouken@3536
  1467
        result =
slouken@3536
  1468
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
  1469
                                             2, vertices, sizeof(*vertices));
slouken@3536
  1470
        if (FAILED(result)) {
icculus@7037
  1471
            return D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1472
        }
slouken@1900
  1473
    }
slouken@1895
  1474
    return 0;
slouken@1895
  1475
}
slouken@1895
  1476
slouken@7505
  1477
static void
slouken@7505
  1478
D3D_UpdateTextureScaleMode(D3D_RenderData *data, D3D_TextureData *texturedata, unsigned index)
slouken@7505
  1479
{
slouken@7505
  1480
    if (texturedata->scaleMode != data->scaleMode[index]) {
slouken@7505
  1481
        IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MINFILTER,
slouken@7505
  1482
                                         texturedata->scaleMode);
slouken@7505
  1483
        IDirect3DDevice9_SetSamplerState(data->device, index, D3DSAMP_MAGFILTER,
slouken@7505
  1484
                                         texturedata->scaleMode);
slouken@7505
  1485
        data->scaleMode[index] = texturedata->scaleMode;
slouken@7505
  1486
    }
slouken@7505
  1487
}
slouken@7505
  1488
slouken@1895
  1489
static int
slouken@1913
  1490
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1491
               const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@1895
  1492
{
slouken@1913
  1493
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1494
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
  1495
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
  1496
    float minx, miny, maxx, maxy;
slouken@1904
  1497
    float minu, maxu, minv, maxv;
slouken@1987
  1498
    DWORD color;
slouken@1903
  1499
    Vertex vertices[4];
slouken@1903
  1500
    HRESULT result;
slouken@1895
  1501
slouken@5297
  1502
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1503
        return -1;
slouken@1900
  1504
    }
slouken@1903
  1505
slouken@6528
  1506
    minx = dstrect->x - 0.5f;
slouken@6528
  1507
    miny = dstrect->y - 0.5f;
slouken@6528
  1508
    maxx = dstrect->x + dstrect->w - 0.5f;
slouken@6528
  1509
    maxy = dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1510
slouken@1904
  1511
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1512
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1513
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1514
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1515
slouken@1987
  1516
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1517
slouken@1903
  1518
    vertices[0].x = minx;
slouken@1903
  1519
    vertices[0].y = miny;
slouken@1903
  1520
    vertices[0].z = 0.0f;
slouken@1987
  1521
    vertices[0].color = color;
slouken@1904
  1522
    vertices[0].u = minu;
slouken@1904
  1523
    vertices[0].v = minv;
slouken@1904
  1524
slouken@1903
  1525
    vertices[1].x = maxx;
slouken@1903
  1526
    vertices[1].y = miny;
slouken@1903
  1527
    vertices[1].z = 0.0f;
slouken@1987
  1528
    vertices[1].color = color;
slouken@1904
  1529
    vertices[1].u = maxu;
slouken@1904
  1530
    vertices[1].v = minv;
slouken@1904
  1531
slouken@1903
  1532
    vertices[2].x = maxx;
slouken@1903
  1533
    vertices[2].y = maxy;
slouken@1903
  1534
    vertices[2].z = 0.0f;
slouken@1987
  1535
    vertices[2].color = color;
slouken@1904
  1536
    vertices[2].u = maxu;
slouken@1904
  1537
    vertices[2].v = maxv;
slouken@1904
  1538
slouken@1903
  1539
    vertices[3].x = minx;
slouken@1903
  1540
    vertices[3].y = maxy;
slouken@1903
  1541
    vertices[3].z = 0.0f;
slouken@1987
  1542
    vertices[3].color = color;
slouken@1904
  1543
    vertices[3].u = minu;
slouken@1904
  1544
    vertices[3].v = maxv;
slouken@1903
  1545
slouken@2932
  1546
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1547
slouken@7505
  1548
    D3D_UpdateTextureScaleMode(data, texturedata, 0);
slouken@1917
  1549
slouken@1903
  1550
    result =
slouken@2735
  1551
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1552
                                    texturedata->texture);
slouken@1903
  1553
    if (FAILED(result)) {
icculus@7037
  1554
        return D3D_SetError("SetTexture()", result);
slouken@1903
  1555
    }
slouken@7505
  1556
slouken@7505
  1557
    if (texturedata->yuv) {
slouken@7505
  1558
        shader = data->ps_yuv;
slouken@7505
  1559
slouken@7505
  1560
        D3D_UpdateTextureScaleMode(data, texturedata, 1);
slouken@7505
  1561
        D3D_UpdateTextureScaleMode(data, texturedata, 2);
slouken@7505
  1562
slouken@7505
  1563
        result =
slouken@7505
  1564
            IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
slouken@7505
  1565
                                        texturedata->utexture);
slouken@7505
  1566
        if (FAILED(result)) {
slouken@7505
  1567
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1568
        }
slouken@7505
  1569
slouken@7505
  1570
        result =
slouken@7505
  1571
            IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
slouken@7505
  1572
                                        texturedata->vtexture);
slouken@7505
  1573
        if (FAILED(result)) {
slouken@7505
  1574
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1575
        }
slouken@7505
  1576
    }
slouken@7505
  1577
slouken@3556
  1578
    if (shader) {
slouken@3556
  1579
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
  1580
        if (FAILED(result)) {
icculus@7037
  1581
            return D3D_SetError("SetShader()", result);
slouken@3556
  1582
        }
slouken@3556
  1583
    }
slouken@1903
  1584
    result =
slouken@1903
  1585
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1586
                                         vertices, sizeof(*vertices));
slouken@1903
  1587
    if (FAILED(result)) {
icculus@7037
  1588
        return D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1589
    }
slouken@3556
  1590
    if (shader) {
slouken@3556
  1591
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
  1592
        if (FAILED(result)) {
icculus@7037
  1593
            return D3D_SetError("SetShader()", result);
slouken@3556
  1594
        }
slouken@3556
  1595
    }
slouken@1895
  1596
    return 0;
slouken@1895
  1597
}
slouken@1895
  1598
gabomdq@6320
  1599
gabomdq@6320
  1600
static int
gabomdq@6320
  1601
D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1602
               const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1603
               const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
gabomdq@6320
  1604
{
gabomdq@6320
  1605
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
gabomdq@6320
  1606
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
gabomdq@6320
  1607
    LPDIRECT3DPIXELSHADER9 shader = NULL;
gabomdq@6320
  1608
    float minx, miny, maxx, maxy;
gabomdq@6320
  1609
    float minu, maxu, minv, maxv;
gabomdq@6320
  1610
    float centerx, centery;
gabomdq@6320
  1611
    DWORD color;
gabomdq@6320
  1612
    Vertex vertices[4];
gabomdq@6320
  1613
    HRESULT result;
gabomdq@6320
  1614
gabomdq@6320
  1615
    if (D3D_ActivateRenderer(renderer) < 0) {
gabomdq@6320
  1616
        return -1;
gabomdq@6320
  1617
    }
gabomdq@6320
  1618
slouken@6528
  1619
    centerx = center->x;
slouken@6528
  1620
    centery = center->y;
gabomdq@6320
  1621
gabomdq@6320
  1622
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@6528
  1623
        minx = dstrect->w - centerx - 0.5f;
slouken@6528
  1624
        maxx = -centerx - 0.5f;
gabomdq@6320
  1625
    }
gabomdq@6320
  1626
    else {
slouken@6528
  1627
        minx = -centerx - 0.5f;
slouken@6528
  1628
        maxx = dstrect->w - centerx - 0.5f;
gabomdq@6320
  1629
    }
gabomdq@6320
  1630
gabomdq@6320
  1631
    if (flip & SDL_FLIP_VERTICAL) {
slouken@6528
  1632
        miny = dstrect->h - centery - 0.5f;
slouken@6528
  1633
        maxy = -centery - 0.5f;
gabomdq@6320
  1634
    }
gabomdq@6320
  1635
    else {
slouken@6528
  1636
        miny = -centery - 0.5f;
slouken@6528
  1637
        maxy = dstrect->h - centery - 0.5f;
gabomdq@6320
  1638
    }
gabomdq@6320
  1639
gabomdq@6320
  1640
    minu = (float) srcrect->x / texture->w;
gabomdq@6320
  1641
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1642
    minv = (float) srcrect->y / texture->h;
gabomdq@6320
  1643
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1644
gabomdq@6320
  1645
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
gabomdq@6320
  1646
gabomdq@6320
  1647
    vertices[0].x = minx;
gabomdq@6320
  1648
    vertices[0].y = miny;
gabomdq@6320
  1649
    vertices[0].z = 0.0f;
gabomdq@6320
  1650
    vertices[0].color = color;
gabomdq@6320
  1651
    vertices[0].u = minu;
gabomdq@6320
  1652
    vertices[0].v = minv;
gabomdq@6320
  1653
gabomdq@6320
  1654
    vertices[1].x = maxx;
gabomdq@6320
  1655
    vertices[1].y = miny;
gabomdq@6320
  1656
    vertices[1].z = 0.0f;
gabomdq@6320
  1657
    vertices[1].color = color;
gabomdq@6320
  1658
    vertices[1].u = maxu;
gabomdq@6320
  1659
    vertices[1].v = minv;
gabomdq@6320
  1660
gabomdq@6320
  1661
    vertices[2].x = maxx;
gabomdq@6320
  1662
    vertices[2].y = maxy;
gabomdq@6320
  1663
    vertices[2].z = 0.0f;
gabomdq@6320
  1664
    vertices[2].color = color;
gabomdq@6320
  1665
    vertices[2].u = maxu;
gabomdq@6320
  1666
    vertices[2].v = maxv;
gabomdq@6320
  1667
gabomdq@6320
  1668
    vertices[3].x = minx;
gabomdq@6320
  1669
    vertices[3].y = maxy;
gabomdq@6320
  1670
    vertices[3].z = 0.0f;
gabomdq@6320
  1671
    vertices[3].color = color;
gabomdq@6320
  1672
    vertices[3].u = minu;
gabomdq@6320
  1673
    vertices[3].v = maxv;
gabomdq@6320
  1674
gabomdq@6320
  1675
    D3D_SetBlendMode(data, texture->blendMode);
gabomdq@6320
  1676
slouken@7191
  1677
    /* Rotate and translate */
gabomdq@6320
  1678
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1679
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
slouken@6332
  1680
    ID3DXMatrixStack_RotateYawPitchRoll(data->matrixStack, 0.0, 0.0, (float)(M_PI * (float) angle / 180.0f));
gabomdq@6320
  1681
    ID3DXMatrixStack_Translate(data->matrixStack, (float)dstrect->x + centerx, (float)dstrect->y + centery, (float)0.0);
gabomdq@6320
  1682
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1683
slouken@7505
  1684
    D3D_UpdateTextureScaleMode(data, texturedata, 0);
gabomdq@6320
  1685
gabomdq@6320
  1686
    result =
gabomdq@6320
  1687
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
gabomdq@6320
  1688
                                    texturedata->texture);
gabomdq@6320
  1689
    if (FAILED(result)) {
icculus@7037
  1690
        return D3D_SetError("SetTexture()", result);
gabomdq@6320
  1691
    }
slouken@7505
  1692
slouken@7505
  1693
    if (texturedata->yuv) {
slouken@7505
  1694
        shader = data->ps_yuv;
slouken@7505
  1695
slouken@7505
  1696
        D3D_UpdateTextureScaleMode(data, texturedata, 1);
slouken@7505
  1697
        D3D_UpdateTextureScaleMode(data, texturedata, 2);
slouken@7505
  1698
slouken@7505
  1699
        result =
slouken@7505
  1700
            IDirect3DDevice9_SetTexture(data->device, 1, (IDirect3DBaseTexture9 *)
slouken@7505
  1701
                                        texturedata->utexture);
slouken@7505
  1702
        if (FAILED(result)) {
slouken@7505
  1703
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1704
        }
slouken@7505
  1705
slouken@7505
  1706
        result =
slouken@7505
  1707
            IDirect3DDevice9_SetTexture(data->device, 2, (IDirect3DBaseTexture9 *)
slouken@7505
  1708
                                        texturedata->vtexture);
slouken@7505
  1709
        if (FAILED(result)) {
slouken@7505
  1710
            return D3D_SetError("SetTexture()", result);
slouken@7505
  1711
        }
slouken@7505
  1712
    }
slouken@7505
  1713
gabomdq@6320
  1714
    if (shader) {
gabomdq@6320
  1715
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
gabomdq@6320
  1716
        if (FAILED(result)) {
icculus@7037
  1717
            return D3D_SetError("SetShader()", result);
gabomdq@6320
  1718
        }
gabomdq@6320
  1719
    }
gabomdq@6320
  1720
    result =
gabomdq@6320
  1721
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
gabomdq@6320
  1722
                                         vertices, sizeof(*vertices));
gabomdq@6320
  1723
    if (FAILED(result)) {
icculus@7037
  1724
        return D3D_SetError("DrawPrimitiveUP()", result);
gabomdq@6320
  1725
    }
gabomdq@6320
  1726
    if (shader) {
gabomdq@6320
  1727
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
gabomdq@6320
  1728
        if (FAILED(result)) {
icculus@7037
  1729
            return D3D_SetError("SetShader()", result);
gabomdq@6320
  1730
        }
gabomdq@6320
  1731
    }
gabomdq@6320
  1732
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1733
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1734
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
gabomdq@6320
  1735
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1736
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1737
    return 0;
gabomdq@6320
  1738
}
gabomdq@6320
  1739
slouken@3427
  1740
static int
slouken@3427
  1741
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1742
                     Uint32 format, void * pixels, int pitch)
slouken@3427
  1743
{
slouken@3549
  1744
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3549
  1745
    D3DSURFACE_DESC desc;
slouken@3549
  1746
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
  1747
    LPDIRECT3DSURFACE9 surface;
slouken@3549
  1748
    RECT d3drect;
slouken@3549
  1749
    D3DLOCKED_RECT locked;
slouken@3549
  1750
    HRESULT result;
slouken@3427
  1751
slouken@3549
  1752
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
  1753
    if (FAILED(result)) {
icculus@7037
  1754
        return D3D_SetError("GetBackBuffer()", result);
slouken@3549
  1755
    }
slouken@3427
  1756
slouken@3549
  1757
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
  1758
    if (FAILED(result)) {
slouken@3549
  1759
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1760
        return D3D_SetError("GetDesc()", result);
slouken@3549
  1761
    }
slouken@3427
  1762
slouken@3549
  1763
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
  1764
    if (FAILED(result)) {
slouken@3549
  1765
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1766
        return D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
  1767
    }
slouken@3427
  1768
slouken@3549
  1769
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
  1770
    if (FAILED(result)) {
slouken@3549
  1771
        IDirect3DSurface9_Release(surface);
slouken@3549
  1772
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1773
        return D3D_SetError("GetRenderTargetData()", result);
slouken@3549
  1774
    }
slouken@3427
  1775
slouken@3549
  1776
    d3drect.left = rect->x;
slouken@3549
  1777
    d3drect.right = rect->x + rect->w;
slouken@3549
  1778
    d3drect.top = rect->y;
slouken@3549
  1779
    d3drect.bottom = rect->y + rect->h;
slouken@3427
  1780
slouken@3549
  1781
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
  1782
    if (FAILED(result)) {
slouken@3549
  1783
        IDirect3DSurface9_Release(surface);
slouken@3549
  1784
        IDirect3DSurface9_Release(backBuffer);
icculus@7037
  1785
        return D3D_SetError("LockRect()", result);
slouken@3549
  1786
    }
slouken@3427
  1787
slouken@3549
  1788
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
  1789
                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
slouken@3549
  1790
                      format, pixels, pitch);
slouken@3549
  1791
slouken@3549
  1792
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
  1793
slouken@3549
  1794
    IDirect3DSurface9_Release(surface);
slouken@3549
  1795
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1796
slouken@3549
  1797
    return 0;
slouken@3427
  1798
}
slouken@3427
  1799
slouken@1895
  1800
static void
slouken@1913
  1801
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1802
{
slouken@1913
  1803
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1804
    HRESULT result;
slouken@1900
  1805
slouken@1900
  1806
    if (!data->beginScene) {
slouken@1900
  1807
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1808
        data->beginScene = SDL_TRUE;
slouken@1900
  1809
    }
slouken@1900
  1810
slouken@1975
  1811
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1812
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1813
        /* We'll reset later */
slouken@1975
  1814
        return;
slouken@1975
  1815
    }
slouken@1975
  1816
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1817
        D3D_Reset(renderer);
slouken@1975
  1818
    }
slouken@1900
  1819
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1820
    if (FAILED(result)) {
slouken@1900
  1821
        D3D_SetError("Present()", result);
slouken@1900
  1822
    }
slouken@1895
  1823
}
slouken@1895
  1824
slouken@1895
  1825
static void
slouken@1913
  1826
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1827
{
slouken@1913
  1828
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1829
slouken@1895
  1830
    if (!data) {
slouken@1895
  1831
        return;
slouken@1895
  1832
    }
slouken@1903
  1833
    if (data->texture) {
slouken@1903
  1834
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1835
    }
slouken@7505
  1836
    if (data->utexture) {
slouken@7505
  1837
        IDirect3DTexture9_Release(data->utexture);
slouken@7505
  1838
    }
slouken@7505
  1839
    if (data->vtexture) {
slouken@7505
  1840
        IDirect3DTexture9_Release(data->vtexture);
slouken@7505
  1841
    }
slouken@7719
  1842
    SDL_free(data->pixels);
slouken@1895
  1843
    SDL_free(data);
slouken@1895
  1844
    texture->driverdata = NULL;
slouken@1895
  1845
}
slouken@1895
  1846
slouken@1975
  1847
static void
slouken@1913
  1848
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1849
{
slouken@1913
  1850
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1851
slouken@1895
  1852
    if (data) {
slouken@7191
  1853
        /* Release the render target */
slouken@6895
  1854
        if (data->defaultRenderTarget) {
slouken@6895
  1855
            IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6895
  1856
            data->defaultRenderTarget = NULL;
slouken@6895
  1857
        }
slouken@6232
  1858
        if (data->currentRenderTarget != NULL) {
slouken@6232
  1859
            IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6232
  1860
            data->currentRenderTarget = NULL;
slouken@6232
  1861
        }
icculus@7658
  1862
        if (data->ps_yuv) {
icculus@7658
  1863
            IDirect3DPixelShader9_Release(data->ps_yuv);
icculus@7658
  1864
        }
slouken@1900
  1865
        if (data->device) {
slouken@1900
  1866
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1867
        }
slouken@5154
  1868
        if (data->d3d) {
slouken@5154
  1869
            IDirect3D9_Release(data->d3d);
slouken@7183
  1870
            ID3DXMatrixStack_Release(data->matrixStack);
slouken@5154
  1871
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1872
        }
slouken@1895
  1873
        SDL_free(data);
slouken@1895
  1874
    }
slouken@1895
  1875
    SDL_free(renderer);
slouken@1895
  1876
}
slouken@1895
  1877
slouken@7763
  1878
IDirect3DDevice9 *
slouken@7763
  1879
SDL_RenderGetD3DDevice(SDL_Renderer * renderer)
slouken@7763
  1880
{
slouken@7763
  1881
	D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@7763
  1882
slouken@7763
  1883
	// Make sure that this is a D3D renderer
slouken@7763
  1884
	if (renderer->DestroyRenderer != D3D_DestroyRenderer) {
slouken@7763
  1885
		SDL_SetError("Renderer is not a D3D renderer");
slouken@7763
  1886
		return NULL;
slouken@7763
  1887
	}
slouken@7763
  1888
	return data->device;
slouken@7763
  1889
}
slouken@7763
  1890
slouken@5226
  1891
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
slouken@1895
  1892
slouken@1895
  1893
/* vi: set ts=4 sw=4 expandtab: */