src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 11 Feb 2013 21:12:14 -0800
changeset 6860 c16fc4e46ae5
parent 6782 582d35419e8a
child 6885 700f1b25f77f
permissions -rw-r--r--
Fixed bug 1491 - Directx3d Crash on resize

Spinduluz

RenderTarget has to be released before a device reset is done. It's a
D3DPOOL_DEFAULT surface (resides in video memory and have to be recreated).
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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"
gabomdq@6320
    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@3556
   124
///////////////////////////////////////////////////////////////////////////
slouken@3556
   125
// ID3DXBuffer:
slouken@3556
   126
// ------------
slouken@3556
   127
// The buffer object is used by D3DX to return arbitrary size data.
slouken@3556
   128
//
slouken@3556
   129
// GetBufferPointer -
slouken@3556
   130
//    Returns a pointer to the beginning of the buffer.
slouken@3556
   131
//
slouken@3556
   132
// GetBufferSize -
slouken@3556
   133
//    Returns the size of the buffer, in bytes.
slouken@3556
   134
///////////////////////////////////////////////////////////////////////////
slouken@3556
   135
slouken@3556
   136
typedef interface ID3DXBuffer ID3DXBuffer;
slouken@3556
   137
typedef interface ID3DXBuffer *LPD3DXBUFFER;
slouken@3556
   138
slouken@3556
   139
// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
slouken@3556
   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@3556
   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@3556
   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@3556
   172
#endif /* ASSEMBLE_SHADER */
slouken@3556
   173
slouken@3556
   174
slouken@1895
   175
/* Direct3D renderer implementation */
slouken@1895
   176
slouken@1913
   177
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5297
   178
static void D3D_WindowEvent(SDL_Renderer * renderer,
slouken@5297
   179
                            const SDL_WindowEvent *event);
slouken@1913
   180
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
   181
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   182
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
   183
                             int pitch);
slouken@1913
   184
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   185
                           const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1913
   186
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@6247
   187
static int D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
   188
static int D3D_UpdateViewport(SDL_Renderer * renderer);
slouken@5297
   189
static int D3D_RenderClear(SDL_Renderer * renderer);
slouken@3599
   190
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
slouken@6528
   191
                                const SDL_FPoint * points, int count);
slouken@3599
   192
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
slouken@6528
   193
                               const SDL_FPoint * points, int count);
slouken@3599
   194
static int D3D_RenderFillRects(SDL_Renderer * renderer,
slouken@6528
   195
                               const SDL_FRect * rects, int count);
slouken@1913
   196
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   197
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect);
gabomdq@6320
   198
static int D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
   199
                          const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
   200
                          const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip);
slouken@3427
   201
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   202
                                Uint32 format, void * pixels, int pitch);
slouken@1913
   203
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
   204
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
   205
                               SDL_Texture * texture);
slouken@1913
   206
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
   207
slouken@1895
   208
slouken@1913
   209
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
   210
    D3D_CreateRenderer,
slouken@1895
   211
    {
slouken@5186
   212
     "direct3d",
slouken@6237
   213
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE),
slouken@5156
   214
     1,
slouken@5156
   215
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1895
   216
     0,
slouken@1895
   217
     0}
slouken@1895
   218
};
slouken@1895
   219
slouken@1895
   220
typedef struct
slouken@1895
   221
{
slouken@5154
   222
    void* d3dDLL;
slouken@2973
   223
    IDirect3D9 *d3d;
slouken@1895
   224
    IDirect3DDevice9 *device;
slouken@3527
   225
    UINT adapter;
slouken@1975
   226
    D3DPRESENT_PARAMETERS pparams;
slouken@5297
   227
    SDL_bool updateSize;
slouken@1900
   228
    SDL_bool beginScene;
slouken@5484
   229
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@6232
   230
    IDirect3DSurface9 *defaultRenderTarget;
slouken@6232
   231
    IDirect3DSurface9 *currentRenderTarget;
gabomdq@6320
   232
    void* d3dxDLL;
gabomdq@6320
   233
    ID3DXMatrixStack *matrixStack;
slouken@1913
   234
} D3D_RenderData;
slouken@1895
   235
slouken@1895
   236
typedef struct
slouken@1895
   237
{
slouken@1903
   238
    IDirect3DTexture9 *texture;
slouken@5484
   239
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@1913
   240
} D3D_TextureData;
slouken@1895
   241
slouken@1903
   242
typedef struct
slouken@1903
   243
{
slouken@1903
   244
    float x, y, z;
slouken@1987
   245
    DWORD color;
slouken@1904
   246
    float u, v;
slouken@1903
   247
} Vertex;
slouken@1903
   248
slouken@1895
   249
static void
slouken@1900
   250
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   251
{
slouken@1900
   252
    const char *error;
slouken@1900
   253
slouken@1900
   254
    switch (result) {
slouken@1900
   255
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   256
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   257
        break;
slouken@1900
   258
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   259
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   260
        break;
slouken@1900
   261
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   262
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   263
        break;
slouken@1900
   264
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   265
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   266
        break;
slouken@1900
   267
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   268
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   269
        break;
slouken@1900
   270
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   271
        error = "TOOMANYOPERATIONS";
slouken@1900
   272
        break;
slouken@1900
   273
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   274
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   275
        break;
slouken@1900
   276
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   277
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   278
        break;
slouken@1900
   279
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   280
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   281
        break;
slouken@1900
   282
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   283
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   284
        break;
slouken@1900
   285
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   286
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   287
        break;
slouken@1900
   288
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   289
        error = "DRIVERINTERNALERROR";
slouken@1900
   290
        break;
slouken@1900
   291
    case D3DERR_NOTFOUND:
slouken@1900
   292
        error = "NOTFOUND";
slouken@1900
   293
        break;
slouken@1900
   294
    case D3DERR_MOREDATA:
slouken@1900
   295
        error = "MOREDATA";
slouken@1900
   296
        break;
slouken@1900
   297
    case D3DERR_DEVICELOST:
slouken@1900
   298
        error = "DEVICELOST";
slouken@1900
   299
        break;
slouken@1900
   300
    case D3DERR_DEVICENOTRESET:
slouken@1900
   301
        error = "DEVICENOTRESET";
slouken@1900
   302
        break;
slouken@1900
   303
    case D3DERR_NOTAVAILABLE:
slouken@1900
   304
        error = "NOTAVAILABLE";
slouken@1900
   305
        break;
slouken@1900
   306
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   307
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   308
        break;
slouken@1900
   309
    case D3DERR_INVALIDDEVICE:
slouken@1900
   310
        error = "INVALIDDEVICE";
slouken@1900
   311
        break;
slouken@1900
   312
    case D3DERR_INVALIDCALL:
slouken@1900
   313
        error = "INVALIDCALL";
slouken@1900
   314
        break;
slouken@1900
   315
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   316
        error = "DRIVERINVALIDCALL";
slouken@1900
   317
        break;
slouken@1900
   318
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   319
        error = "WASSTILLDRAWING";
slouken@1900
   320
        break;
slouken@1900
   321
    default:
slouken@1900
   322
        error = "UNKNOWN";
slouken@1900
   323
        break;
slouken@1900
   324
    }
slouken@1900
   325
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   326
}
slouken@1900
   327
slouken@1903
   328
static D3DFORMAT
slouken@1903
   329
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   330
{
slouken@1903
   331
    switch (format) {
slouken@1965
   332
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   333
        return D3DFMT_R5G6B5;
slouken@1965
   334
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   335
        return D3DFMT_X8R8G8B8;
slouken@1965
   336
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   337
        return D3DFMT_A8R8G8B8;
slouken@1903
   338
    default:
slouken@1903
   339
        return D3DFMT_UNKNOWN;
slouken@1903
   340
    }
slouken@1895
   341
}
slouken@1895
   342
slouken@5156
   343
static Uint32
slouken@5156
   344
D3DFMTToPixelFormat(D3DFORMAT format)
slouken@2973
   345
{
slouken@5156
   346
    switch (format) {
slouken@5156
   347
    case D3DFMT_R5G6B5:
slouken@5156
   348
        return SDL_PIXELFORMAT_RGB565;
slouken@5156
   349
    case D3DFMT_X8R8G8B8:
slouken@5156
   350
        return SDL_PIXELFORMAT_RGB888;
slouken@5156
   351
    case D3DFMT_A8R8G8B8:
slouken@5156
   352
        return SDL_PIXELFORMAT_ARGB8888;
slouken@5156
   353
    default:
slouken@5156
   354
        return SDL_PIXELFORMAT_UNKNOWN;
slouken@2973
   355
    }
slouken@1895
   356
}
slouken@1895
   357
slouken@5297
   358
static int
slouken@5297
   359
D3D_Reset(SDL_Renderer * renderer)
slouken@5297
   360
{
slouken@5297
   361
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   362
    HRESULT result;
slouken@5297
   363
slouken@6860
   364
    /* Release the default render target before reset */
slouken@6860
   365
    IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6860
   366
slouken@5297
   367
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@5297
   368
    if (FAILED(result)) {
slouken@5297
   369
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   370
            /* Don't worry about it, we'll reset later... */
slouken@5297
   371
            return 0;
slouken@5297
   372
        } else {
slouken@5297
   373
            D3D_SetError("Reset()", result);
slouken@5297
   374
            return -1;
slouken@5297
   375
        }
slouken@5297
   376
    }
slouken@5297
   377
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@5297
   378
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   379
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@5297
   380
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@5297
   381
                                    D3DCULL_NONE);
slouken@5297
   382
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@6860
   383
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget);
slouken@5297
   384
    return 0;
slouken@5297
   385
}
slouken@5297
   386
slouken@5297
   387
static int
slouken@5297
   388
D3D_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   389
{
slouken@5297
   390
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   391
    HRESULT result;
slouken@5297
   392
slouken@5297
   393
    if (data->updateSize) {
slouken@5297
   394
        SDL_Window *window = renderer->window;
slouken@5297
   395
        int w, h;
slouken@5297
   396
slouken@5297
   397
        SDL_GetWindowSize(window, &w, &h);
slouken@5297
   398
        data->pparams.BackBufferWidth = w;
slouken@5297
   399
        data->pparams.BackBufferHeight = h;
slouken@5297
   400
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@5297
   401
            data->pparams.BackBufferFormat =
slouken@5297
   402
                PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window));
slouken@5297
   403
        } else {
slouken@5297
   404
            data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@5297
   405
        }
slouken@5297
   406
        if (D3D_Reset(renderer) < 0) {
slouken@5297
   407
            return -1;
slouken@5297
   408
        }
slouken@5297
   409
        D3D_UpdateViewport(renderer);
slouken@5297
   410
slouken@5297
   411
        data->updateSize = SDL_FALSE;
slouken@5297
   412
    }
slouken@5297
   413
    if (data->beginScene) {
slouken@5297
   414
        result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   415
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   416
            if (D3D_Reset(renderer) < 0) {
slouken@5297
   417
                return -1;
slouken@5297
   418
            }
slouken@5297
   419
            result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   420
        }
slouken@5297
   421
        if (FAILED(result)) {
slouken@5297
   422
            D3D_SetError("BeginScene()", result);
slouken@5297
   423
            return -1;
slouken@5297
   424
        }
slouken@5297
   425
        data->beginScene = SDL_FALSE;
slouken@5297
   426
    }
slouken@5297
   427
    return 0;
slouken@5297
   428
}
slouken@5297
   429
slouken@1895
   430
SDL_Renderer *
slouken@1913
   431
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   432
{
slouken@1895
   433
    SDL_Renderer *renderer;
slouken@1913
   434
    D3D_RenderData *data;
slouken@5154
   435
    SDL_SysWMinfo windowinfo;
slouken@1900
   436
    HRESULT result;
slouken@1900
   437
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   438
    IDirect3DSwapChain9 *chain;
slouken@1925
   439
    D3DCAPS9 caps;
slouken@5156
   440
    Uint32 window_flags;
slouken@5156
   441
    int w, h;
slouken@5156
   442
    SDL_DisplayMode fullscreen_mode;
slouken@5297
   443
    D3DMATRIX matrix;
gabomdq@6320
   444
    int d3dxVersion;
gabomdq@6320
   445
	char d3dxDLLFile[50];
slouken@1895
   446
slouken@1920
   447
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   448
    if (!renderer) {
slouken@1895
   449
        SDL_OutOfMemory();
slouken@1895
   450
        return NULL;
slouken@1895
   451
    }
slouken@1895
   452
slouken@1920
   453
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   454
    if (!data) {
slouken@5154
   455
        SDL_free(renderer);
slouken@1895
   456
        SDL_OutOfMemory();
slouken@1895
   457
        return NULL;
slouken@1895
   458
    }
slouken@1895
   459
slouken@5154
   460
    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
slouken@5154
   461
    if (data->d3dDLL) {
slouken@5154
   462
        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@4569
   463
slouken@5154
   464
        D3DCreate =
slouken@5154
   465
            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
slouken@5154
   466
                                                            "Direct3DCreate9");
slouken@5154
   467
        if (D3DCreate) {
slouken@5154
   468
            data->d3d = D3DCreate(D3D_SDK_VERSION);
slouken@5154
   469
        }
slouken@5154
   470
        if (!data->d3d) {
slouken@5154
   471
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
   472
            data->d3dDLL = NULL;
slouken@5154
   473
        }
gabomdq@6320
   474
gabomdq@6320
   475
        for (d3dxVersion=50;d3dxVersion>0;d3dxVersion--) {
gabomdq@6320
   476
            SDL_snprintf(d3dxDLLFile, 49, "D3DX9_%02d.dll", d3dxVersion);
gabomdq@6320
   477
            data->d3dxDLL = SDL_LoadObject(d3dxDLLFile);
gabomdq@6320
   478
            if (data->d3dxDLL) {
gabomdq@6320
   479
                HRESULT (WINAPI *D3DXCreateMatrixStack) (DWORD Flags, LPD3DXMATRIXSTACK*  ppStack);
gabomdq@6320
   480
                D3DXCreateMatrixStack = (HRESULT (WINAPI *) (DWORD, LPD3DXMATRIXSTACK*)) SDL_LoadFunction(data->d3dxDLL, "D3DXCreateMatrixStack");
gabomdq@6320
   481
                if (D3DXCreateMatrixStack) {
gabomdq@6320
   482
                    D3DXCreateMatrixStack(0, &data->matrixStack);
gabomdq@6320
   483
                    break;
gabomdq@6320
   484
                }
gabomdq@6320
   485
            }
gabomdq@6320
   486
        }
gabomdq@6320
   487
gabomdq@6320
   488
        if (!data->matrixStack) {
gabomdq@6320
   489
            if (data->d3dxDLL) SDL_UnloadObject(data->d3dxDLL);
gabomdq@6320
   490
        }
slouken@5154
   491
    }
gabomdq@6320
   492
gabomdq@6320
   493
gabomdq@6320
   494
    
gabomdq@6320
   495
    if (!data->d3d || !data->matrixStack) {
slouken@5154
   496
        SDL_free(renderer);
slouken@5154
   497
        SDL_free(data);
slouken@5154
   498
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   499
        return NULL;
slouken@5154
   500
    }
slouken@5154
   501
slouken@5297
   502
    renderer->WindowEvent = D3D_WindowEvent;
slouken@1913
   503
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   504
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   505
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   506
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@6247
   507
    renderer->SetRenderTarget = D3D_SetRenderTarget;
slouken@5297
   508
    renderer->UpdateViewport = D3D_UpdateViewport;
slouken@5297
   509
    renderer->RenderClear = D3D_RenderClear;
slouken@3599
   510
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   511
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   512
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   513
    renderer->RenderCopy = D3D_RenderCopy;
gabomdq@6320
   514
    renderer->RenderCopyEx = D3D_RenderCopyEx;
slouken@3427
   515
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@1913
   516
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   517
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   518
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   519
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   520
    renderer->driverdata = data;
slouken@1895
   521
slouken@6246
   522
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   523
slouken@5154
   524
    SDL_VERSION(&windowinfo.version);
slouken@5154
   525
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   526
slouken@5156
   527
    window_flags = SDL_GetWindowFlags(window);
slouken@5156
   528
    SDL_GetWindowSize(window, &w, &h);
slouken@5156
   529
    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
slouken@5156
   530
slouken@1900
   531
    SDL_zero(pparams);
slouken@5154
   532
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@5156
   533
    pparams.BackBufferWidth = w;
slouken@5156
   534
    pparams.BackBufferHeight = h;
slouken@5156
   535
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   536
        pparams.BackBufferFormat =
slouken@5156
   537
            PixelFormatToD3DFMT(fullscreen_mode.format);
slouken@1903
   538
    } else {
slouken@1903
   539
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   540
    }
slouken@5142
   541
    pparams.BackBufferCount = 1;
slouken@5142
   542
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   543
slouken@5156
   544
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@6782
   545
		if ( ( window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP ) == SDL_WINDOW_FULLSCREEN_DESKTOP )  {
slouken@6782
   546
			pparams.Windowed = TRUE;
slouken@6782
   547
			pparams.FullScreen_RefreshRateInHz = 0;
slouken@6782
   548
		} else {
slouken@1900
   549
        pparams.Windowed = FALSE;
slouken@1903
   550
        pparams.FullScreen_RefreshRateInHz =
slouken@5156
   551
            fullscreen_mode.refresh_rate;
slouken@6782
   552
		}
slouken@1900
   553
    } else {
slouken@1900
   554
        pparams.Windowed = TRUE;
slouken@1903
   555
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   556
    }
slouken@1965
   557
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   558
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   559
    } else {
slouken@1907
   560
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   561
    }
slouken@1900
   562
slouken@5154
   563
    /* FIXME: Which adapter? */
slouken@5154
   564
    data->adapter = D3DADAPTER_DEFAULT;
slouken@5154
   565
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   566
slouken@5154
   567
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   568
                                     D3DDEVTYPE_HAL,
slouken@5154
   569
                                     pparams.hDeviceWindow,
slouken@6115
   570
                                     D3DCREATE_FPU_PRESERVE | ((caps.
slouken@3197
   571
                                      DevCaps &
slouken@3197
   572
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   573
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@6115
   574
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING),
slouken@1900
   575
                                     &pparams, &data->device);
slouken@1900
   576
    if (FAILED(result)) {
slouken@1913
   577
        D3D_DestroyRenderer(renderer);
slouken@1900
   578
        D3D_SetError("CreateDevice()", result);
slouken@1900
   579
        return NULL;
slouken@1900
   580
    }
slouken@1900
   581
    data->beginScene = SDL_TRUE;
slouken@5484
   582
    data->scaleMode = D3DTEXF_FORCE_DWORD;
slouken@1900
   583
slouken@1907
   584
    /* Get presentation parameters to fill info */
slouken@1907
   585
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   586
    if (FAILED(result)) {
slouken@1913
   587
        D3D_DestroyRenderer(renderer);
slouken@1907
   588
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   589
        return NULL;
slouken@1907
   590
    }
slouken@1907
   591
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   592
    if (FAILED(result)) {
slouken@1907
   593
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   594
        D3D_DestroyRenderer(renderer);
slouken@1907
   595
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   596
        return NULL;
slouken@1907
   597
    }
slouken@1907
   598
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   599
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   600
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   601
    }
slouken@1975
   602
    data->pparams = pparams;
slouken@1907
   603
slouken@1925
   604
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   605
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   606
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@6246
   607
    if (caps.NumSimultaneousRTs >= 2) {
slouken@6246
   608
        renderer->info.flags |= SDL_RENDERER_TARGETTEXTURE;
slouken@6246
   609
    }
slouken@1918
   610
slouken@1903
   611
    /* Set up parameters for rendering */
slouken@1904
   612
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   613
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   614
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   615
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   616
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   617
                                    D3DCULL_NONE);
slouken@1904
   618
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   619
    /* Enable color modulation by diffuse color */
slouken@1988
   620
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   621
                                          D3DTOP_MODULATE);
slouken@1988
   622
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   623
                                          D3DTA_TEXTURE);
slouken@1988
   624
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   625
                                          D3DTA_DIFFUSE);
slouken@1988
   626
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   627
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   628
                                          D3DTOP_MODULATE);
slouken@1988
   629
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   630
                                          D3DTA_TEXTURE);
slouken@1988
   631
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   632
                                          D3DTA_DIFFUSE);
slouken@1991
   633
    /* Disable second texture stage, since we're done */
slouken@1991
   634
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   635
                                          D3DTOP_DISABLE);
slouken@1991
   636
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   637
                                          D3DTOP_DISABLE);
slouken@1903
   638
slouken@6232
   639
    /* Store the default render target */
slouken@6232
   640
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
slouken@6232
   641
    data->currentRenderTarget = NULL;
slouken@6232
   642
slouken@5297
   643
    /* Set an identity world and view matrix */
slouken@5297
   644
    matrix.m[0][0] = 1.0f;
slouken@5297
   645
    matrix.m[0][1] = 0.0f;
slouken@5297
   646
    matrix.m[0][2] = 0.0f;
slouken@5297
   647
    matrix.m[0][3] = 0.0f;
slouken@5297
   648
    matrix.m[1][0] = 0.0f;
slouken@5297
   649
    matrix.m[1][1] = 1.0f;
slouken@5297
   650
    matrix.m[1][2] = 0.0f;
slouken@5297
   651
    matrix.m[1][3] = 0.0f;
slouken@5297
   652
    matrix.m[2][0] = 0.0f;
slouken@5297
   653
    matrix.m[2][1] = 0.0f;
slouken@5297
   654
    matrix.m[2][2] = 1.0f;
slouken@5297
   655
    matrix.m[2][3] = 0.0f;
slouken@5297
   656
    matrix.m[3][0] = 0.0f;
slouken@5297
   657
    matrix.m[3][1] = 0.0f;
slouken@5297
   658
    matrix.m[3][2] = 0.0f;
slouken@5297
   659
    matrix.m[3][3] = 1.0f;
slouken@5297
   660
    IDirect3DDevice9_SetTransform(data->device, D3DTS_WORLD, &matrix);
slouken@5297
   661
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, &matrix);
slouken@5297
   662
slouken@1895
   663
    return renderer;
slouken@1895
   664
}
slouken@1895
   665
slouken@5297
   666
static void
slouken@5297
   667
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1975
   668
{
slouken@1975
   669
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   670
slouken@5297
   671
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   672
        data->updateSize = SDL_TRUE;
slouken@1975
   673
    }
slouken@1975
   674
}
slouken@1975
   675
slouken@5484
   676
static D3DTEXTUREFILTERTYPE
slouken@5484
   677
GetScaleQuality(void)
slouken@5484
   678
{
slouken@5484
   679
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   680
slouken@5484
   681
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   682
        return D3DTEXF_POINT;
slouken@5484
   683
    } else if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) {
slouken@5484
   684
        return D3DTEXF_LINEAR;
slouken@5484
   685
    } else {
slouken@5484
   686
        return D3DTEXF_ANISOTROPIC;
slouken@5484
   687
    }
slouken@5484
   688
}
slouken@5484
   689
slouken@1975
   690
static int
slouken@1913
   691
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   692
{
icculus@6404
   693
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   694
    D3D_TextureData *data;
slouken@5173
   695
    D3DPOOL pool;
slouken@5173
   696
    DWORD usage;
slouken@1903
   697
    HRESULT result;
slouken@1895
   698
slouken@1920
   699
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   700
    if (!data) {
slouken@1895
   701
        SDL_OutOfMemory();
slouken@1895
   702
        return -1;
slouken@1895
   703
    }
slouken@5484
   704
    data->scaleMode = GetScaleQuality();
slouken@1895
   705
slouken@1895
   706
    texture->driverdata = data;
slouken@1895
   707
slouken@5173
   708
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   709
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   710
        pool = D3DPOOL_DEFAULT;
slouken@5173
   711
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   712
    } else
slouken@5173
   713
#endif
slouken@6232
   714
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6246
   715
        /* D3DPOOL_MANAGED does not work with D3DUSAGE_RENDERTARGET */
slouken@6246
   716
        pool = D3DPOOL_DEFAULT;
slouken@6232
   717
        usage = D3DUSAGE_RENDERTARGET;
slouken@6246
   718
    } else {
slouken@5173
   719
        pool = D3DPOOL_MANAGED;
slouken@5173
   720
        usage = 0;
slouken@5173
   721
    }
slouken@2973
   722
slouken@1903
   723
    result =
slouken@1903
   724
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   725
                                       texture->h, 1, usage,
slouken@5173
   726
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   727
                                       pool, &data->texture, NULL);
slouken@1903
   728
    if (FAILED(result)) {
slouken@1903
   729
        D3D_SetError("CreateTexture()", result);
slouken@1903
   730
        return -1;
slouken@1903
   731
    }
slouken@1903
   732
slouken@1895
   733
    return 0;
slouken@1895
   734
}
slouken@1895
   735
slouken@1895
   736
static int
slouken@1913
   737
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   738
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   739
{
slouken@1913
   740
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   741
    RECT d3drect;
slouken@5156
   742
    D3DLOCKED_RECT locked;
slouken@5156
   743
    const Uint8 *src;
slouken@5156
   744
    Uint8 *dst;
slouken@5156
   745
    int row, length;
slouken@5156
   746
    HRESULT result;
slouken@2973
   747
slouken@5173
   748
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   749
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
   750
        rect->x == 0 && rect->y == 0 &&
slouken@5173
   751
        rect->w == texture->w && rect->h == texture->h) {
slouken@5194
   752
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@5173
   753
    } else
slouken@5173
   754
#endif
slouken@5173
   755
    {
slouken@5173
   756
        d3drect.left = rect->x;
slouken@5173
   757
        d3drect.right = rect->x + rect->w;
slouken@5173
   758
        d3drect.top = rect->y;
slouken@5173
   759
        d3drect.bottom = rect->y + rect->h;
slouken@5194
   760
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   761
    }
slouken@2973
   762
slouken@5156
   763
    if (FAILED(result)) {
slouken@5156
   764
        D3D_SetError("LockRect()", result);
slouken@5156
   765
        return -1;
slouken@5156
   766
    }
slouken@2973
   767
slouken@5156
   768
    src = pixels;
slouken@5156
   769
    dst = locked.pBits;
slouken@5156
   770
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5173
   771
    if (length == pitch && length == locked.Pitch) {
slouken@5173
   772
        SDL_memcpy(dst, src, length*rect->h);
slouken@5173
   773
    } else {
slouken@5173
   774
        for (row = 0; row < rect->h; ++row) {
slouken@5173
   775
            SDL_memcpy(dst, src, length);
slouken@5173
   776
            src += pitch;
slouken@5173
   777
            dst += locked.Pitch;
slouken@5173
   778
        }
slouken@5156
   779
    }
slouken@5156
   780
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   781
slouken@5156
   782
    return 0;
slouken@1895
   783
}
slouken@1895
   784
slouken@1895
   785
static int
slouken@1913
   786
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   787
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   788
{
slouken@1913
   789
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   790
    RECT d3drect;
slouken@5156
   791
    D3DLOCKED_RECT locked;
slouken@5156
   792
    HRESULT result;
slouken@1895
   793
slouken@5156
   794
    d3drect.left = rect->x;
slouken@5156
   795
    d3drect.right = rect->x + rect->w;
slouken@5156
   796
    d3drect.top = rect->y;
slouken@5156
   797
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   798
slouken@5156
   799
    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   800
    if (FAILED(result)) {
slouken@5156
   801
        D3D_SetError("LockRect()", result);
slouken@5156
   802
        return -1;
slouken@1903
   803
    }
slouken@5156
   804
    *pixels = locked.pBits;
slouken@5156
   805
    *pitch = locked.Pitch;
slouken@5156
   806
    return 0;
slouken@1895
   807
}
slouken@1895
   808
slouken@1895
   809
static void
slouken@1913
   810
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   811
{
slouken@1913
   812
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   813
slouken@5156
   814
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   815
}
slouken@1895
   816
slouken@5297
   817
static int
slouken@5297
   818
D3D_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   819
{
slouken@5224
   820
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   821
    D3DVIEWPORT9 viewport;
slouken@5297
   822
    D3DMATRIX matrix;
slouken@5224
   823
slouken@5297
   824
    /* Set the viewport */
slouken@5297
   825
    viewport.X = renderer->viewport.x;
slouken@5297
   826
    viewport.Y = renderer->viewport.y;
slouken@5297
   827
    viewport.Width = renderer->viewport.w;
slouken@5297
   828
    viewport.Height = renderer->viewport.h;
slouken@5297
   829
    viewport.MinZ = 0.0f;
slouken@5297
   830
    viewport.MaxZ = 1.0f;
slouken@5297
   831
    IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5224
   832
slouken@5297
   833
    /* Set an orthographic projection matrix */
slouken@5297
   834
    matrix.m[0][0] = 2.0f / renderer->viewport.w;
slouken@5297
   835
    matrix.m[0][1] = 0.0f;
slouken@5297
   836
    matrix.m[0][2] = 0.0f;
slouken@5297
   837
    matrix.m[0][3] = 0.0f;
slouken@5297
   838
    matrix.m[1][0] = 0.0f;
slouken@5297
   839
    matrix.m[1][1] = -2.0f / renderer->viewport.h;
slouken@5297
   840
    matrix.m[1][2] = 0.0f;
slouken@5297
   841
    matrix.m[1][3] = 0.0f;
slouken@5297
   842
    matrix.m[2][0] = 0.0f;
slouken@5297
   843
    matrix.m[2][1] = 0.0f;
slouken@5297
   844
    matrix.m[2][2] = 1.0f;
slouken@5297
   845
    matrix.m[2][3] = 0.0f;
slouken@5297
   846
    matrix.m[3][0] = -1.0f;
slouken@5297
   847
    matrix.m[3][1] = 1.0f;
slouken@5297
   848
    matrix.m[3][2] = 0.0f;
slouken@5297
   849
    matrix.m[3][3] = 1.0f;
slouken@5297
   850
    IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &matrix);
slouken@5297
   851
slouken@5297
   852
    return 0;
slouken@5297
   853
}
slouken@5297
   854
slouken@5297
   855
static int
slouken@6247
   856
D3D_SetRenderTarget(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6246
   857
{
slouken@6246
   858
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@6246
   859
    D3D_TextureData *texturedata;
slouken@6246
   860
    HRESULT result;
slouken@6246
   861
slouken@6246
   862
    D3D_ActivateRenderer(renderer);
slouken@6246
   863
slouken@6246
   864
    /* Release the previous render target if it wasn't the default one */
slouken@6246
   865
    if (data->currentRenderTarget != NULL) {
slouken@6246
   866
        IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6246
   867
        data->currentRenderTarget = NULL;
slouken@6246
   868
    }
slouken@6246
   869
slouken@6246
   870
    if (texture == NULL) {
slouken@6246
   871
        IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget);
slouken@6246
   872
        return 0;
slouken@6246
   873
    }
slouken@6246
   874
slouken@6246
   875
    texturedata = (D3D_TextureData *) texture->driverdata;
slouken@6246
   876
    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget);
slouken@6246
   877
    if(FAILED(result)) {
slouken@6246
   878
        D3D_SetError("GetSurfaceLevel()", result);
slouken@6246
   879
        return -1;
slouken@6246
   880
    }
slouken@6246
   881
    result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget);
slouken@6246
   882
    if(FAILED(result)) {
slouken@6246
   883
        D3D_SetError("SetRenderTarget()", result);
slouken@6246
   884
        return -1;
slouken@6246
   885
    }
slouken@6246
   886
slouken@6246
   887
    return 0;
slouken@6246
   888
}
slouken@6246
   889
slouken@6246
   890
static int
slouken@5297
   891
D3D_RenderClear(SDL_Renderer * renderer)
slouken@5297
   892
{
slouken@5297
   893
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   894
    DWORD color;
slouken@5297
   895
    HRESULT result;
slouken@5297
   896
slouken@5297
   897
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   898
        return -1;
slouken@5224
   899
    }
slouken@5297
   900
slouken@5297
   901
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@5297
   902
slouken@5299
   903
    /* Don't reset the viewport if we don't have to! */
slouken@5299
   904
    if (!renderer->viewport.x && !renderer->viewport.y &&
slouken@5299
   905
        renderer->viewport.w == data->pparams.BackBufferWidth &&
slouken@5299
   906
        renderer->viewport.h == data->pparams.BackBufferHeight) {
slouken@5299
   907
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   908
    } else {
slouken@5299
   909
        D3DVIEWPORT9 viewport;
slouken@5297
   910
slouken@5299
   911
        /* Clear is defined to clear the entire render target */
slouken@5299
   912
        viewport.X = 0;
slouken@5299
   913
        viewport.Y = 0;
slouken@5299
   914
        viewport.Width = data->pparams.BackBufferWidth;
slouken@5299
   915
        viewport.Height = data->pparams.BackBufferHeight;
slouken@5299
   916
        viewport.MinZ = 0.0f;
slouken@5299
   917
        viewport.MaxZ = 1.0f;
slouken@5299
   918
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   919
slouken@5299
   920
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   921
slouken@5299
   922
        /* Reset the viewport */
slouken@5299
   923
        viewport.X = renderer->viewport.x;
slouken@5299
   924
        viewport.Y = renderer->viewport.y;
slouken@5299
   925
        viewport.Width = renderer->viewport.w;
slouken@5299
   926
        viewport.Height = renderer->viewport.h;
slouken@5299
   927
        viewport.MinZ = 0.0f;
slouken@5299
   928
        viewport.MaxZ = 1.0f;
slouken@5299
   929
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   930
    }
slouken@5297
   931
slouken@5297
   932
    if (FAILED(result)) {
slouken@5297
   933
        D3D_SetError("Clear()", result);
slouken@5297
   934
        return -1;
slouken@5297
   935
    }
slouken@5297
   936
    return 0;
slouken@5224
   937
}
slouken@5224
   938
slouken@5224
   939
static void
slouken@2933
   940
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   941
{
slouken@2932
   942
    switch (blendMode) {
slouken@2932
   943
    case SDL_BLENDMODE_NONE:
slouken@2932
   944
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   945
                                        FALSE);
slouken@2932
   946
        break;
slouken@2932
   947
    case SDL_BLENDMODE_BLEND:
slouken@2932
   948
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   949
                                        TRUE);
slouken@2932
   950
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   951
                                        D3DBLEND_SRCALPHA);
slouken@2932
   952
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   953
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   954
        break;
slouken@2932
   955
    case SDL_BLENDMODE_ADD:
slouken@2932
   956
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   957
                                        TRUE);
slouken@2932
   958
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   959
                                        D3DBLEND_SRCALPHA);
slouken@2932
   960
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   961
                                        D3DBLEND_ONE);
slouken@2932
   962
        break;
slouken@5184
   963
    case SDL_BLENDMODE_MOD:
slouken@5184
   964
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@5184
   965
                                        TRUE);
slouken@5184
   966
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@5184
   967
                                        D3DBLEND_ZERO);
slouken@5184
   968
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@5184
   969
                                        D3DBLEND_SRCCOLOR);
slouken@5184
   970
        break;
slouken@2932
   971
    }
slouken@2932
   972
}
slouken@2932
   973
slouken@1895
   974
static int
slouken@6528
   975
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
   976
                     int count)
slouken@1895
   977
{
slouken@1913
   978
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   979
    DWORD color;
slouken@3536
   980
    Vertex *vertices;
slouken@3536
   981
    int i;
slouken@1900
   982
    HRESULT result;
slouken@1895
   983
slouken@5297
   984
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   985
        return -1;
slouken@1900
   986
    }
slouken@1895
   987
slouken@2932
   988
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   989
slouken@2933
   990
    result =
slouken@2933
   991
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   992
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   993
    if (FAILED(result)) {
slouken@2932
   994
        D3D_SetError("SetTexture()", result);
slouken@2932
   995
        return -1;
slouken@2932
   996
    }
slouken@3536
   997
slouken@3536
   998
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   999
slouken@3536
  1000
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1001
    for (i = 0; i < count; ++i) {
slouken@6528
  1002
        vertices[i].x = points[i].x;
slouken@6528
  1003
        vertices[i].y = points[i].y;
slouken@3536
  1004
        vertices[i].z = 0.0f;
slouken@3536
  1005
        vertices[i].color = color;
slouken@3536
  1006
        vertices[i].u = 0.0f;
slouken@3536
  1007
        vertices[i].v = 0.0f;
slouken@3536
  1008
    }
slouken@1903
  1009
    result =
slouken@3536
  1010
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
  1011
                                         vertices, sizeof(*vertices));
slouken@3536
  1012
    SDL_stack_free(vertices);
slouken@1900
  1013
    if (FAILED(result)) {
slouken@2932
  1014
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1015
        return -1;
slouken@2932
  1016
    }
slouken@2932
  1017
    return 0;
slouken@2932
  1018
}
slouken@2932
  1019
slouken@2932
  1020
static int
slouken@6528
  1021
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_FPoint * points,
slouken@3599
  1022
                    int count)
slouken@2932
  1023
{
slouken@2932
  1024
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
  1025
    DWORD color;
slouken@3536
  1026
    Vertex *vertices;
slouken@3536
  1027
    int i;
slouken@2932
  1028
    HRESULT result;
slouken@2932
  1029
slouken@5297
  1030
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1031
        return -1;
slouken@2932
  1032
    }
slouken@2932
  1033
slouken@2932
  1034
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1035
slouken@2933
  1036
    result =
slouken@2933
  1037
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1038
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1039
    if (FAILED(result)) {
slouken@2932
  1040
        D3D_SetError("SetTexture()", result);
slouken@2932
  1041
        return -1;
slouken@2932
  1042
    }
slouken@3536
  1043
slouken@3536
  1044
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1045
slouken@3536
  1046
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
  1047
    for (i = 0; i < count; ++i) {
slouken@6528
  1048
        vertices[i].x = points[i].x;
slouken@6528
  1049
        vertices[i].y = points[i].y;
slouken@3536
  1050
        vertices[i].z = 0.0f;
slouken@3536
  1051
        vertices[i].color = color;
slouken@3536
  1052
        vertices[i].u = 0.0f;
slouken@3536
  1053
        vertices[i].v = 0.0f;
slouken@3536
  1054
    }
slouken@2932
  1055
    result =
slouken@3551
  1056
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
  1057
                                         vertices, sizeof(*vertices));
slouken@3551
  1058
slouken@3551
  1059
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
  1060
       so we need to close the endpoint of the line */
slouken@6076
  1061
    if (count == 2 ||
slouken@6076
  1062
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@6528
  1063
        vertices[0].x = points[count-1].x;
slouken@6528
  1064
        vertices[0].y = points[count-1].y;
slouken@3551
  1065
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
  1066
    }
slouken@3551
  1067
slouken@3536
  1068
    SDL_stack_free(vertices);
slouken@2932
  1069
    if (FAILED(result)) {
slouken@2932
  1070
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
  1071
        return -1;
slouken@2932
  1072
    }
slouken@2932
  1073
    return 0;
slouken@2932
  1074
}
slouken@2932
  1075
slouken@2932
  1076
static int
slouken@6528
  1077
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_FRect * rects,
slouken@3599
  1078
                    int count)
slouken@2932
  1079
{
slouken@2932
  1080
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
  1081
    DWORD color;
slouken@3536
  1082
    int i;
slouken@2932
  1083
    float minx, miny, maxx, maxy;
slouken@2932
  1084
    Vertex vertices[4];
slouken@2932
  1085
    HRESULT result;
slouken@2932
  1086
slouken@5297
  1087
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1088
        return -1;
slouken@2932
  1089
    }
slouken@2932
  1090
slouken@2932
  1091
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1092
slouken@2933
  1093
    result =
slouken@2933
  1094
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1095
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1096
    if (FAILED(result)) {
slouken@2932
  1097
        D3D_SetError("SetTexture()", result);
slouken@2932
  1098
        return -1;
slouken@2932
  1099
    }
slouken@3536
  1100
slouken@3536
  1101
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1102
slouken@3536
  1103
    for (i = 0; i < count; ++i) {
slouken@6528
  1104
        const SDL_FRect *rect = &rects[i];
slouken@3536
  1105
slouken@6528
  1106
        minx = rect->x;
slouken@6528
  1107
        miny = rect->y;
slouken@6528
  1108
        maxx = rect->x + rect->w;
slouken@6528
  1109
        maxy = rect->y + rect->h;
slouken@3536
  1110
slouken@3536
  1111
        vertices[0].x = minx;
slouken@3536
  1112
        vertices[0].y = miny;
slouken@3536
  1113
        vertices[0].z = 0.0f;
slouken@3536
  1114
        vertices[0].color = color;
slouken@3536
  1115
        vertices[0].u = 0.0f;
slouken@3536
  1116
        vertices[0].v = 0.0f;
slouken@3536
  1117
slouken@3536
  1118
        vertices[1].x = maxx;
slouken@3536
  1119
        vertices[1].y = miny;
slouken@3536
  1120
        vertices[1].z = 0.0f;
slouken@3536
  1121
        vertices[1].color = color;
slouken@3536
  1122
        vertices[1].u = 0.0f;
slouken@3536
  1123
        vertices[1].v = 0.0f;
slouken@3536
  1124
slouken@3536
  1125
        vertices[2].x = maxx;
slouken@3536
  1126
        vertices[2].y = maxy;
slouken@3536
  1127
        vertices[2].z = 0.0f;
slouken@3536
  1128
        vertices[2].color = color;
slouken@3536
  1129
        vertices[2].u = 0.0f;
slouken@3536
  1130
        vertices[2].v = 0.0f;
slouken@3536
  1131
slouken@3536
  1132
        vertices[3].x = minx;
slouken@3536
  1133
        vertices[3].y = maxy;
slouken@3536
  1134
        vertices[3].z = 0.0f;
slouken@3536
  1135
        vertices[3].color = color;
slouken@3536
  1136
        vertices[3].u = 0.0f;
slouken@3536
  1137
        vertices[3].v = 0.0f;
slouken@3536
  1138
slouken@3536
  1139
        result =
slouken@3536
  1140
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
  1141
                                             2, vertices, sizeof(*vertices));
slouken@3536
  1142
        if (FAILED(result)) {
slouken@3536
  1143
            D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1144
            return -1;
slouken@3536
  1145
        }
slouken@1900
  1146
    }
slouken@1895
  1147
    return 0;
slouken@1895
  1148
}
slouken@1895
  1149
slouken@1895
  1150
static int
slouken@1913
  1151
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1152
               const SDL_Rect * srcrect, const SDL_FRect * dstrect)
slouken@1895
  1153
{
slouken@1913
  1154
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1155
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
  1156
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
  1157
    float minx, miny, maxx, maxy;
slouken@1904
  1158
    float minu, maxu, minv, maxv;
slouken@1987
  1159
    DWORD color;
slouken@1903
  1160
    Vertex vertices[4];
slouken@1903
  1161
    HRESULT result;
slouken@1895
  1162
slouken@5297
  1163
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1164
        return -1;
slouken@1900
  1165
    }
slouken@1903
  1166
slouken@6528
  1167
    minx = dstrect->x - 0.5f;
slouken@6528
  1168
    miny = dstrect->y - 0.5f;
slouken@6528
  1169
    maxx = dstrect->x + dstrect->w - 0.5f;
slouken@6528
  1170
    maxy = dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1171
slouken@1904
  1172
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1173
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1174
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1175
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1176
slouken@1987
  1177
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1178
slouken@1903
  1179
    vertices[0].x = minx;
slouken@1903
  1180
    vertices[0].y = miny;
slouken@1903
  1181
    vertices[0].z = 0.0f;
slouken@1987
  1182
    vertices[0].color = color;
slouken@1904
  1183
    vertices[0].u = minu;
slouken@1904
  1184
    vertices[0].v = minv;
slouken@1904
  1185
slouken@1903
  1186
    vertices[1].x = maxx;
slouken@1903
  1187
    vertices[1].y = miny;
slouken@1903
  1188
    vertices[1].z = 0.0f;
slouken@1987
  1189
    vertices[1].color = color;
slouken@1904
  1190
    vertices[1].u = maxu;
slouken@1904
  1191
    vertices[1].v = minv;
slouken@1904
  1192
slouken@1903
  1193
    vertices[2].x = maxx;
slouken@1903
  1194
    vertices[2].y = maxy;
slouken@1903
  1195
    vertices[2].z = 0.0f;
slouken@1987
  1196
    vertices[2].color = color;
slouken@1904
  1197
    vertices[2].u = maxu;
slouken@1904
  1198
    vertices[2].v = maxv;
slouken@1904
  1199
slouken@1903
  1200
    vertices[3].x = minx;
slouken@1903
  1201
    vertices[3].y = maxy;
slouken@1903
  1202
    vertices[3].z = 0.0f;
slouken@1987
  1203
    vertices[3].color = color;
slouken@1904
  1204
    vertices[3].u = minu;
slouken@1904
  1205
    vertices[3].v = maxv;
slouken@1903
  1206
slouken@2932
  1207
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1208
slouken@5484
  1209
    if (texturedata->scaleMode != data->scaleMode) {
slouken@5484
  1210
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@5484
  1211
                                         texturedata->scaleMode);
slouken@5484
  1212
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@5484
  1213
                                         texturedata->scaleMode);
slouken@5484
  1214
        data->scaleMode = texturedata->scaleMode;
slouken@5484
  1215
    }
slouken@1917
  1216
slouken@1903
  1217
    result =
slouken@2735
  1218
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1219
                                    texturedata->texture);
slouken@1903
  1220
    if (FAILED(result)) {
slouken@1903
  1221
        D3D_SetError("SetTexture()", result);
slouken@1903
  1222
        return -1;
slouken@1903
  1223
    }
slouken@3556
  1224
    if (shader) {
slouken@3556
  1225
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
  1226
        if (FAILED(result)) {
slouken@3556
  1227
            D3D_SetError("SetShader()", result);
slouken@3556
  1228
            return -1;
slouken@3556
  1229
        }
slouken@3556
  1230
    }
slouken@1903
  1231
    result =
slouken@1903
  1232
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1233
                                         vertices, sizeof(*vertices));
slouken@1903
  1234
    if (FAILED(result)) {
slouken@1903
  1235
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1236
        return -1;
slouken@1903
  1237
    }
slouken@3556
  1238
    if (shader) {
slouken@3556
  1239
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
  1240
        if (FAILED(result)) {
slouken@3556
  1241
            D3D_SetError("SetShader()", result);
slouken@3556
  1242
            return -1;
slouken@3556
  1243
        }
slouken@3556
  1244
    }
slouken@1895
  1245
    return 0;
slouken@1895
  1246
}
slouken@1895
  1247
gabomdq@6320
  1248
gabomdq@6320
  1249
static int
gabomdq@6320
  1250
D3D_RenderCopyEx(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@6528
  1251
               const SDL_Rect * srcrect, const SDL_FRect * dstrect,
slouken@6528
  1252
               const double angle, const SDL_FPoint * center, const SDL_RendererFlip flip)
gabomdq@6320
  1253
{
gabomdq@6320
  1254
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
gabomdq@6320
  1255
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
gabomdq@6320
  1256
    LPDIRECT3DPIXELSHADER9 shader = NULL;
gabomdq@6320
  1257
    float minx, miny, maxx, maxy;
gabomdq@6320
  1258
    float minu, maxu, minv, maxv;
gabomdq@6320
  1259
    float centerx, centery;
gabomdq@6320
  1260
    DWORD color;
gabomdq@6320
  1261
    Vertex vertices[4];
gabomdq@6320
  1262
    HRESULT result;
gabomdq@6320
  1263
gabomdq@6320
  1264
    if (D3D_ActivateRenderer(renderer) < 0) {
gabomdq@6320
  1265
        return -1;
gabomdq@6320
  1266
    }
gabomdq@6320
  1267
slouken@6528
  1268
    centerx = center->x;
slouken@6528
  1269
    centery = center->y;
gabomdq@6320
  1270
gabomdq@6320
  1271
    if (flip & SDL_FLIP_HORIZONTAL) {
slouken@6528
  1272
        minx = dstrect->w - centerx - 0.5f;
slouken@6528
  1273
        maxx = -centerx - 0.5f;
gabomdq@6320
  1274
    }
gabomdq@6320
  1275
    else {
slouken@6528
  1276
        minx = -centerx - 0.5f;
slouken@6528
  1277
        maxx = dstrect->w - centerx - 0.5f;
gabomdq@6320
  1278
    }
gabomdq@6320
  1279
gabomdq@6320
  1280
    if (flip & SDL_FLIP_VERTICAL) {
slouken@6528
  1281
        miny = dstrect->h - centery - 0.5f;
slouken@6528
  1282
        maxy = -centery - 0.5f;
gabomdq@6320
  1283
    }
gabomdq@6320
  1284
    else {
slouken@6528
  1285
        miny = -centery - 0.5f;
slouken@6528
  1286
        maxy = dstrect->h - centery - 0.5f;
gabomdq@6320
  1287
    }
gabomdq@6320
  1288
gabomdq@6320
  1289
    minu = (float) srcrect->x / texture->w;
gabomdq@6320
  1290
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
gabomdq@6320
  1291
    minv = (float) srcrect->y / texture->h;
gabomdq@6320
  1292
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
gabomdq@6320
  1293
gabomdq@6320
  1294
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
gabomdq@6320
  1295
gabomdq@6320
  1296
    vertices[0].x = minx;
gabomdq@6320
  1297
    vertices[0].y = miny;
gabomdq@6320
  1298
    vertices[0].z = 0.0f;
gabomdq@6320
  1299
    vertices[0].color = color;
gabomdq@6320
  1300
    vertices[0].u = minu;
gabomdq@6320
  1301
    vertices[0].v = minv;
gabomdq@6320
  1302
gabomdq@6320
  1303
    vertices[1].x = maxx;
gabomdq@6320
  1304
    vertices[1].y = miny;
gabomdq@6320
  1305
    vertices[1].z = 0.0f;
gabomdq@6320
  1306
    vertices[1].color = color;
gabomdq@6320
  1307
    vertices[1].u = maxu;
gabomdq@6320
  1308
    vertices[1].v = minv;
gabomdq@6320
  1309
gabomdq@6320
  1310
    vertices[2].x = maxx;
gabomdq@6320
  1311
    vertices[2].y = maxy;
gabomdq@6320
  1312
    vertices[2].z = 0.0f;
gabomdq@6320
  1313
    vertices[2].color = color;
gabomdq@6320
  1314
    vertices[2].u = maxu;
gabomdq@6320
  1315
    vertices[2].v = maxv;
gabomdq@6320
  1316
gabomdq@6320
  1317
    vertices[3].x = minx;
gabomdq@6320
  1318
    vertices[3].y = maxy;
gabomdq@6320
  1319
    vertices[3].z = 0.0f;
gabomdq@6320
  1320
    vertices[3].color = color;
gabomdq@6320
  1321
    vertices[3].u = minu;
gabomdq@6320
  1322
    vertices[3].v = maxv;
gabomdq@6320
  1323
gabomdq@6320
  1324
    D3D_SetBlendMode(data, texture->blendMode);
gabomdq@6320
  1325
gabomdq@6320
  1326
    // Rotate and translate
gabomdq@6320
  1327
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1328
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
slouken@6332
  1329
    ID3DXMatrixStack_RotateYawPitchRoll(data->matrixStack, 0.0, 0.0, (float)(M_PI * (float) angle / 180.0f));
gabomdq@6320
  1330
    ID3DXMatrixStack_Translate(data->matrixStack, (float)dstrect->x + centerx, (float)dstrect->y + centery, (float)0.0);
gabomdq@6320
  1331
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1332
gabomdq@6320
  1333
    if (texturedata->scaleMode != data->scaleMode) {
gabomdq@6320
  1334
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
gabomdq@6320
  1335
                                         texturedata->scaleMode);
gabomdq@6320
  1336
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
gabomdq@6320
  1337
                                         texturedata->scaleMode);
gabomdq@6320
  1338
        data->scaleMode = texturedata->scaleMode;
gabomdq@6320
  1339
    }
gabomdq@6320
  1340
gabomdq@6320
  1341
    result =
gabomdq@6320
  1342
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
gabomdq@6320
  1343
                                    texturedata->texture);
gabomdq@6320
  1344
    if (FAILED(result)) {
gabomdq@6320
  1345
        D3D_SetError("SetTexture()", result);
gabomdq@6320
  1346
        return -1;
gabomdq@6320
  1347
    }
gabomdq@6320
  1348
    if (shader) {
gabomdq@6320
  1349
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
gabomdq@6320
  1350
        if (FAILED(result)) {
gabomdq@6320
  1351
            D3D_SetError("SetShader()", result);
gabomdq@6320
  1352
            return -1;
gabomdq@6320
  1353
        }
gabomdq@6320
  1354
    }
gabomdq@6320
  1355
    result =
gabomdq@6320
  1356
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
gabomdq@6320
  1357
                                         vertices, sizeof(*vertices));
gabomdq@6320
  1358
    if (FAILED(result)) {
gabomdq@6320
  1359
        D3D_SetError("DrawPrimitiveUP()", result);
gabomdq@6320
  1360
        return -1;
gabomdq@6320
  1361
    }
gabomdq@6320
  1362
    if (shader) {
gabomdq@6320
  1363
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
gabomdq@6320
  1364
        if (FAILED(result)) {
gabomdq@6320
  1365
            D3D_SetError("SetShader()", result);
gabomdq@6320
  1366
            return -1;
gabomdq@6320
  1367
        }
gabomdq@6320
  1368
    }
gabomdq@6320
  1369
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1370
    ID3DXMatrixStack_Push(data->matrixStack);
gabomdq@6320
  1371
    ID3DXMatrixStack_LoadIdentity(data->matrixStack);
gabomdq@6320
  1372
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, (D3DMATRIX*)ID3DXMatrixStack_GetTop(data->matrixStack));
gabomdq@6320
  1373
    ID3DXMatrixStack_Pop(data->matrixStack);
gabomdq@6320
  1374
    return 0;
gabomdq@6320
  1375
}
gabomdq@6320
  1376
slouken@3427
  1377
static int
slouken@3427
  1378
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1379
                     Uint32 format, void * pixels, int pitch)
slouken@3427
  1380
{
slouken@3549
  1381
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3549
  1382
    D3DSURFACE_DESC desc;
slouken@3549
  1383
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
  1384
    LPDIRECT3DSURFACE9 surface;
slouken@3549
  1385
    RECT d3drect;
slouken@3549
  1386
    D3DLOCKED_RECT locked;
slouken@3549
  1387
    HRESULT result;
slouken@3427
  1388
slouken@3549
  1389
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
  1390
    if (FAILED(result)) {
slouken@3549
  1391
        D3D_SetError("GetBackBuffer()", result);
slouken@3549
  1392
        return -1;
slouken@3549
  1393
    }
slouken@3427
  1394
slouken@3549
  1395
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
  1396
    if (FAILED(result)) {
slouken@3549
  1397
        D3D_SetError("GetDesc()", result);
slouken@3549
  1398
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1399
        return -1;
slouken@3549
  1400
    }
slouken@3427
  1401
slouken@3549
  1402
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
  1403
    if (FAILED(result)) {
slouken@3549
  1404
        D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
  1405
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1406
        return -1;
slouken@3549
  1407
    }
slouken@3427
  1408
slouken@3549
  1409
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
  1410
    if (FAILED(result)) {
slouken@3549
  1411
        D3D_SetError("GetRenderTargetData()", result);
slouken@3549
  1412
        IDirect3DSurface9_Release(surface);
slouken@3549
  1413
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1414
        return -1;
slouken@3549
  1415
    }
slouken@3427
  1416
slouken@3549
  1417
    d3drect.left = rect->x;
slouken@3549
  1418
    d3drect.right = rect->x + rect->w;
slouken@3549
  1419
    d3drect.top = rect->y;
slouken@3549
  1420
    d3drect.bottom = rect->y + rect->h;
slouken@3427
  1421
slouken@3549
  1422
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
  1423
    if (FAILED(result)) {
slouken@3549
  1424
        D3D_SetError("LockRect()", result);
slouken@3549
  1425
        IDirect3DSurface9_Release(surface);
slouken@3549
  1426
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1427
        return -1;
slouken@3549
  1428
    }
slouken@3427
  1429
slouken@3549
  1430
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
  1431
                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
slouken@3549
  1432
                      format, pixels, pitch);
slouken@3549
  1433
slouken@3549
  1434
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
  1435
slouken@3549
  1436
    IDirect3DSurface9_Release(surface);
slouken@3549
  1437
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1438
slouken@3549
  1439
    return 0;
slouken@3427
  1440
}
slouken@3427
  1441
slouken@1895
  1442
static void
slouken@1913
  1443
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1444
{
slouken@1913
  1445
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1446
    HRESULT result;
slouken@1900
  1447
slouken@1900
  1448
    if (!data->beginScene) {
slouken@1900
  1449
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1450
        data->beginScene = SDL_TRUE;
slouken@1900
  1451
    }
slouken@1900
  1452
slouken@1975
  1453
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1454
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1455
        /* We'll reset later */
slouken@1975
  1456
        return;
slouken@1975
  1457
    }
slouken@1975
  1458
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1459
        D3D_Reset(renderer);
slouken@1975
  1460
    }
slouken@1900
  1461
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1462
    if (FAILED(result)) {
slouken@1900
  1463
        D3D_SetError("Present()", result);
slouken@1900
  1464
    }
slouken@1895
  1465
}
slouken@1895
  1466
slouken@1895
  1467
static void
slouken@1913
  1468
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1469
{
slouken@1913
  1470
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1471
slouken@1895
  1472
    if (!data) {
slouken@1895
  1473
        return;
slouken@1895
  1474
    }
slouken@1903
  1475
    if (data->texture) {
slouken@1903
  1476
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1477
    }
slouken@1895
  1478
    SDL_free(data);
slouken@1895
  1479
    texture->driverdata = NULL;
slouken@1895
  1480
}
slouken@1895
  1481
slouken@1975
  1482
static void
slouken@1913
  1483
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1484
{
slouken@1913
  1485
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1486
slouken@1895
  1487
    if (data) {
slouken@6232
  1488
        // Release the render target
slouken@6232
  1489
        IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6232
  1490
        if (data->currentRenderTarget != NULL) {
slouken@6232
  1491
            IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6232
  1492
            data->currentRenderTarget = NULL;
slouken@6232
  1493
        }
slouken@6232
  1494
        
slouken@1900
  1495
        if (data->device) {
slouken@1900
  1496
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1497
        }
slouken@5154
  1498
        if (data->d3d) {
slouken@5154
  1499
            IDirect3D9_Release(data->d3d);
slouken@5154
  1500
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1501
        }
slouken@1895
  1502
        SDL_free(data);
slouken@1895
  1503
    }
slouken@1895
  1504
    SDL_free(renderer);
slouken@1895
  1505
}
slouken@1895
  1506
slouken@5226
  1507
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
slouken@1895
  1508
slouken@1895
  1509
/* vi: set ts=4 sw=4 expandtab: */