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