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