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