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