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