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