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