src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 17 Aug 2014 14:44:53 -0700
changeset 9085 7cafa57c23ec
parent 9074 86a6f6d92960
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2685 - SDL_RenderReadPixels() doesn't work with offscreen targets

Andreas Falkenhahn

SDL_RenderReadPixels() doesn't seem to work when trying to read pixels from a texture that has been created using SDL_TEXTUREACCESS_TARGET and has been selected as the render target using SDL_SetRenderTarget().

I am attaching a small program that demonstrates the issue. I get the following result here:

READ PIXEL RETURN: 0 --- COLOR CHECK: ff000000

But it should be:

READ PIXEL RETURN: 0 --- COLOR CHECK: ffff0000

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