src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 18 Jan 2012 22:45:49 -0500
changeset 6232 37e8d0736366
parent 6138 4c64952a58fb
child 6236 4a7abc1b6b0e
permissions -rwxr-xr-x
Implementation of render targets, by Mason Wheeler and Gabriel Jacobo

Thanks guys!
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 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
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@5226
    23
#if SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED
slouken@5226
    24
slouken@1895
    25
slouken@5154
    26
#include "../../core/windows/SDL_windows.h"
slouken@5154
    27
slouken@5484
    28
#include "SDL_hints.h"
slouken@5154
    29
#include "SDL_loadso.h"
slouken@5154
    30
#include "SDL_syswm.h"
slouken@5154
    31
#include "../SDL_sysrender.h"
slouken@5154
    32
slouken@5154
    33
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    34
#define D3D_DEBUG_INFO
slouken@5154
    35
#include <d3d9.h>
slouken@5154
    36
#endif
slouken@1895
    37
slouken@3556
    38
#ifdef ASSEMBLE_SHADER
slouken@3556
    39
///////////////////////////////////////////////////////////////////////////
slouken@3556
    40
// ID3DXBuffer:
slouken@3556
    41
// ------------
slouken@3556
    42
// The buffer object is used by D3DX to return arbitrary size data.
slouken@3556
    43
//
slouken@3556
    44
// GetBufferPointer -
slouken@3556
    45
//    Returns a pointer to the beginning of the buffer.
slouken@3556
    46
//
slouken@3556
    47
// GetBufferSize -
slouken@3556
    48
//    Returns the size of the buffer, in bytes.
slouken@3556
    49
///////////////////////////////////////////////////////////////////////////
slouken@3556
    50
slouken@3556
    51
typedef interface ID3DXBuffer ID3DXBuffer;
slouken@3556
    52
typedef interface ID3DXBuffer *LPD3DXBUFFER;
slouken@3556
    53
slouken@3556
    54
// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
slouken@3556
    55
DEFINE_GUID(IID_ID3DXBuffer, 
slouken@3556
    56
0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
slouken@3556
    57
slouken@3556
    58
#undef INTERFACE
slouken@3556
    59
#define INTERFACE ID3DXBuffer
slouken@3556
    60
slouken@3556
    61
typedef interface ID3DXBuffer {
slouken@3556
    62
    const struct ID3DXBufferVtbl FAR* lpVtbl;
slouken@3556
    63
} ID3DXBuffer;
slouken@3556
    64
typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
slouken@3556
    65
const struct ID3DXBufferVtbl
slouken@3556
    66
{
slouken@3556
    67
    // IUnknown
slouken@3556
    68
    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
slouken@3556
    69
    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
slouken@3556
    70
    STDMETHOD_(ULONG, Release)(THIS) PURE;
slouken@3556
    71
slouken@3556
    72
    // ID3DXBuffer
slouken@3556
    73
    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
slouken@3556
    74
    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
slouken@3556
    75
};
slouken@3556
    76
slouken@3556
    77
HRESULT WINAPI
slouken@3556
    78
    D3DXAssembleShader(
slouken@3556
    79
        LPCSTR                          pSrcData,
slouken@3556
    80
        UINT                            SrcDataLen,
slouken@3556
    81
        CONST LPVOID*                   pDefines,
slouken@3556
    82
        LPVOID                          pInclude,
slouken@3556
    83
        DWORD                           Flags,
slouken@3556
    84
        LPD3DXBUFFER*                   ppShader,
slouken@3556
    85
        LPD3DXBUFFER*                   ppErrorMsgs);
slouken@3556
    86
slouken@3556
    87
#endif /* ASSEMBLE_SHADER */
slouken@3556
    88
slouken@3556
    89
slouken@1895
    90
/* Direct3D renderer implementation */
slouken@1895
    91
slouken@1913
    92
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@5297
    93
static void D3D_WindowEvent(SDL_Renderer * renderer,
slouken@5297
    94
                            const SDL_WindowEvent *event);
slouken@1913
    95
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    96
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    97
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
    98
                             int pitch);
slouken@1913
    99
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   100
                           const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1913
   101
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@5297
   102
static int D3D_UpdateViewport(SDL_Renderer * renderer);
slouken@5297
   103
static int D3D_RenderClear(SDL_Renderer * renderer);
slouken@3599
   104
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3599
   105
                                const SDL_Point * points, int count);
slouken@3599
   106
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
slouken@3599
   107
                               const SDL_Point * points, int count);
slouken@3599
   108
static int D3D_RenderFillRects(SDL_Renderer * renderer,
slouken@5297
   109
                               const SDL_Rect * rects, int count);
slouken@1913
   110
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   111
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
   112
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   113
                                Uint32 format, void * pixels, int pitch);
slouken@6232
   114
static int D3D_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
   115
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
   116
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
   117
                               SDL_Texture * texture);
slouken@1913
   118
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
   119
slouken@1895
   120
slouken@1913
   121
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
   122
    D3D_CreateRenderer,
slouken@1895
   123
    {
slouken@5186
   124
     "direct3d",
slouken@5203
   125
     (SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
slouken@5156
   126
     1,
slouken@5156
   127
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1895
   128
     0,
slouken@1895
   129
     0}
slouken@1895
   130
};
slouken@1895
   131
slouken@1895
   132
typedef struct
slouken@1895
   133
{
slouken@5154
   134
    void* d3dDLL;
slouken@2973
   135
    IDirect3D9 *d3d;
slouken@1895
   136
    IDirect3DDevice9 *device;
slouken@3527
   137
    UINT adapter;
slouken@1975
   138
    D3DPRESENT_PARAMETERS pparams;
slouken@5297
   139
    SDL_bool updateSize;
slouken@1900
   140
    SDL_bool beginScene;
slouken@5484
   141
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@6232
   142
    IDirect3DSurface9 *defaultRenderTarget;
slouken@6232
   143
    IDirect3DSurface9 *currentRenderTarget;
slouken@6232
   144
    SDL_bool renderTargetActive;
slouken@6232
   145
    SDL_Rect viewport_copy;
slouken@6232
   146
    
slouken@6232
   147
    Uint32 NumSimultaneousRTs;
slouken@1913
   148
} D3D_RenderData;
slouken@1895
   149
slouken@1895
   150
typedef struct
slouken@1895
   151
{
slouken@1903
   152
    IDirect3DTexture9 *texture;
slouken@5484
   153
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@1913
   154
} D3D_TextureData;
slouken@1895
   155
slouken@1903
   156
typedef struct
slouken@1903
   157
{
slouken@1903
   158
    float x, y, z;
slouken@1987
   159
    DWORD color;
slouken@1904
   160
    float u, v;
slouken@1903
   161
} Vertex;
slouken@1903
   162
slouken@1895
   163
static void
slouken@1900
   164
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   165
{
slouken@1900
   166
    const char *error;
slouken@1900
   167
slouken@1900
   168
    switch (result) {
slouken@1900
   169
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   170
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   171
        break;
slouken@1900
   172
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   173
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   174
        break;
slouken@1900
   175
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   176
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   177
        break;
slouken@1900
   178
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   179
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   180
        break;
slouken@1900
   181
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   182
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   183
        break;
slouken@1900
   184
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   185
        error = "TOOMANYOPERATIONS";
slouken@1900
   186
        break;
slouken@1900
   187
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   188
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   189
        break;
slouken@1900
   190
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   191
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   192
        break;
slouken@1900
   193
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   194
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   195
        break;
slouken@1900
   196
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   197
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   198
        break;
slouken@1900
   199
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   200
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   201
        break;
slouken@1900
   202
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   203
        error = "DRIVERINTERNALERROR";
slouken@1900
   204
        break;
slouken@1900
   205
    case D3DERR_NOTFOUND:
slouken@1900
   206
        error = "NOTFOUND";
slouken@1900
   207
        break;
slouken@1900
   208
    case D3DERR_MOREDATA:
slouken@1900
   209
        error = "MOREDATA";
slouken@1900
   210
        break;
slouken@1900
   211
    case D3DERR_DEVICELOST:
slouken@1900
   212
        error = "DEVICELOST";
slouken@1900
   213
        break;
slouken@1900
   214
    case D3DERR_DEVICENOTRESET:
slouken@1900
   215
        error = "DEVICENOTRESET";
slouken@1900
   216
        break;
slouken@1900
   217
    case D3DERR_NOTAVAILABLE:
slouken@1900
   218
        error = "NOTAVAILABLE";
slouken@1900
   219
        break;
slouken@1900
   220
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   221
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   222
        break;
slouken@1900
   223
    case D3DERR_INVALIDDEVICE:
slouken@1900
   224
        error = "INVALIDDEVICE";
slouken@1900
   225
        break;
slouken@1900
   226
    case D3DERR_INVALIDCALL:
slouken@1900
   227
        error = "INVALIDCALL";
slouken@1900
   228
        break;
slouken@1900
   229
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   230
        error = "DRIVERINVALIDCALL";
slouken@1900
   231
        break;
slouken@1900
   232
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   233
        error = "WASSTILLDRAWING";
slouken@1900
   234
        break;
slouken@1900
   235
    default:
slouken@1900
   236
        error = "UNKNOWN";
slouken@1900
   237
        break;
slouken@1900
   238
    }
slouken@1900
   239
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   240
}
slouken@1900
   241
slouken@1903
   242
static D3DFORMAT
slouken@1903
   243
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   244
{
slouken@1903
   245
    switch (format) {
slouken@1965
   246
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   247
        return D3DFMT_R5G6B5;
slouken@1965
   248
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   249
        return D3DFMT_X8R8G8B8;
slouken@1965
   250
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   251
        return D3DFMT_A8R8G8B8;
slouken@1903
   252
    default:
slouken@1903
   253
        return D3DFMT_UNKNOWN;
slouken@1903
   254
    }
slouken@1895
   255
}
slouken@1895
   256
slouken@5156
   257
static Uint32
slouken@5156
   258
D3DFMTToPixelFormat(D3DFORMAT format)
slouken@2973
   259
{
slouken@5156
   260
    switch (format) {
slouken@5156
   261
    case D3DFMT_R5G6B5:
slouken@5156
   262
        return SDL_PIXELFORMAT_RGB565;
slouken@5156
   263
    case D3DFMT_X8R8G8B8:
slouken@5156
   264
        return SDL_PIXELFORMAT_RGB888;
slouken@5156
   265
    case D3DFMT_A8R8G8B8:
slouken@5156
   266
        return SDL_PIXELFORMAT_ARGB8888;
slouken@5156
   267
    default:
slouken@5156
   268
        return SDL_PIXELFORMAT_UNKNOWN;
slouken@2973
   269
    }
slouken@1895
   270
}
slouken@1895
   271
slouken@5297
   272
static int
slouken@5297
   273
D3D_Reset(SDL_Renderer * renderer)
slouken@5297
   274
{
slouken@5297
   275
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   276
    HRESULT result;
slouken@5297
   277
slouken@5297
   278
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@5297
   279
    if (FAILED(result)) {
slouken@5297
   280
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   281
            /* Don't worry about it, we'll reset later... */
slouken@5297
   282
            return 0;
slouken@5297
   283
        } else {
slouken@5297
   284
            D3D_SetError("Reset()", result);
slouken@5297
   285
            return -1;
slouken@5297
   286
        }
slouken@5297
   287
    }
slouken@5297
   288
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@5297
   289
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   290
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@5297
   291
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@5297
   292
                                    D3DCULL_NONE);
slouken@5297
   293
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@5297
   294
    return 0;
slouken@5297
   295
}
slouken@5297
   296
slouken@5297
   297
static int
slouken@5297
   298
D3D_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   299
{
slouken@5297
   300
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   301
    HRESULT result;
slouken@5297
   302
slouken@5297
   303
    if (data->updateSize) {
slouken@5297
   304
        SDL_Window *window = renderer->window;
slouken@5297
   305
        int w, h;
slouken@5297
   306
slouken@5297
   307
        SDL_GetWindowSize(window, &w, &h);
slouken@5297
   308
        data->pparams.BackBufferWidth = w;
slouken@5297
   309
        data->pparams.BackBufferHeight = h;
slouken@5297
   310
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@5297
   311
            data->pparams.BackBufferFormat =
slouken@5297
   312
                PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window));
slouken@5297
   313
        } else {
slouken@5297
   314
            data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@5297
   315
        }
slouken@5297
   316
        if (D3D_Reset(renderer) < 0) {
slouken@5297
   317
            return -1;
slouken@5297
   318
        }
slouken@5297
   319
        D3D_UpdateViewport(renderer);
slouken@5297
   320
slouken@5297
   321
        data->updateSize = SDL_FALSE;
slouken@5297
   322
    }
slouken@5297
   323
    if (data->beginScene) {
slouken@5297
   324
        result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   325
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   326
            if (D3D_Reset(renderer) < 0) {
slouken@5297
   327
                return -1;
slouken@5297
   328
            }
slouken@5297
   329
            result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   330
        }
slouken@5297
   331
        if (FAILED(result)) {
slouken@5297
   332
            D3D_SetError("BeginScene()", result);
slouken@5297
   333
            return -1;
slouken@5297
   334
        }
slouken@5297
   335
        data->beginScene = SDL_FALSE;
slouken@5297
   336
    }
slouken@5297
   337
    return 0;
slouken@5297
   338
}
slouken@5297
   339
slouken@1895
   340
SDL_Renderer *
slouken@1913
   341
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   342
{
slouken@1895
   343
    SDL_Renderer *renderer;
slouken@1913
   344
    D3D_RenderData *data;
slouken@5154
   345
    SDL_SysWMinfo windowinfo;
slouken@1900
   346
    HRESULT result;
slouken@1900
   347
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   348
    IDirect3DSwapChain9 *chain;
slouken@1925
   349
    D3DCAPS9 caps;
slouken@5156
   350
    Uint32 window_flags;
slouken@5156
   351
    int w, h;
slouken@5156
   352
    SDL_DisplayMode fullscreen_mode;
slouken@5297
   353
    D3DMATRIX matrix;
slouken@1895
   354
slouken@1920
   355
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   356
    if (!renderer) {
slouken@1895
   357
        SDL_OutOfMemory();
slouken@1895
   358
        return NULL;
slouken@1895
   359
    }
slouken@1895
   360
slouken@1920
   361
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   362
    if (!data) {
slouken@5154
   363
        SDL_free(renderer);
slouken@1895
   364
        SDL_OutOfMemory();
slouken@1895
   365
        return NULL;
slouken@1895
   366
    }
slouken@1895
   367
slouken@5154
   368
    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
slouken@5154
   369
    if (data->d3dDLL) {
slouken@5154
   370
        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@4569
   371
slouken@5154
   372
        D3DCreate =
slouken@5154
   373
            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
slouken@5154
   374
                                                            "Direct3DCreate9");
slouken@5154
   375
        if (D3DCreate) {
slouken@5154
   376
            data->d3d = D3DCreate(D3D_SDK_VERSION);
slouken@5154
   377
        }
slouken@5154
   378
        if (!data->d3d) {
slouken@5154
   379
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
   380
            data->d3dDLL = NULL;
slouken@5154
   381
        }
slouken@5154
   382
    }
slouken@5154
   383
    if (!data->d3d) {
slouken@5154
   384
        SDL_free(renderer);
slouken@5154
   385
        SDL_free(data);
slouken@5154
   386
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   387
        return NULL;
slouken@5154
   388
    }
slouken@5154
   389
slouken@5297
   390
    renderer->WindowEvent = D3D_WindowEvent;
slouken@1913
   391
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   392
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   393
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   394
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@5297
   395
    renderer->UpdateViewport = D3D_UpdateViewport;
slouken@5297
   396
    renderer->RenderClear = D3D_RenderClear;
slouken@3599
   397
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   398
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   399
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   400
    renderer->RenderCopy = D3D_RenderCopy;
slouken@3427
   401
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@6232
   402
    renderer->SetTargetTexture = D3D_SetTargetTexture;
slouken@1913
   403
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   404
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   405
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   406
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   407
    renderer->driverdata = data;
slouken@1895
   408
slouken@1965
   409
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   410
slouken@5154
   411
    SDL_VERSION(&windowinfo.version);
slouken@5154
   412
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   413
slouken@5156
   414
    window_flags = SDL_GetWindowFlags(window);
slouken@5156
   415
    SDL_GetWindowSize(window, &w, &h);
slouken@5156
   416
    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
slouken@5156
   417
slouken@1900
   418
    SDL_zero(pparams);
slouken@5154
   419
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@5156
   420
    pparams.BackBufferWidth = w;
slouken@5156
   421
    pparams.BackBufferHeight = h;
slouken@5156
   422
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   423
        pparams.BackBufferFormat =
slouken@5156
   424
            PixelFormatToD3DFMT(fullscreen_mode.format);
slouken@1903
   425
    } else {
slouken@1903
   426
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   427
    }
slouken@5142
   428
    pparams.BackBufferCount = 1;
slouken@5142
   429
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   430
slouken@5156
   431
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   432
        pparams.Windowed = FALSE;
slouken@1903
   433
        pparams.FullScreen_RefreshRateInHz =
slouken@5156
   434
            fullscreen_mode.refresh_rate;
slouken@1900
   435
    } else {
slouken@1900
   436
        pparams.Windowed = TRUE;
slouken@1903
   437
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   438
    }
slouken@1965
   439
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   440
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   441
    } else {
slouken@1907
   442
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   443
    }
slouken@1900
   444
slouken@5154
   445
    /* FIXME: Which adapter? */
slouken@5154
   446
    data->adapter = D3DADAPTER_DEFAULT;
slouken@5154
   447
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   448
slouken@5154
   449
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   450
                                     D3DDEVTYPE_HAL,
slouken@5154
   451
                                     pparams.hDeviceWindow,
slouken@6115
   452
                                     D3DCREATE_FPU_PRESERVE | ((caps.
slouken@3197
   453
                                      DevCaps &
slouken@3197
   454
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   455
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@6115
   456
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING),
slouken@1900
   457
                                     &pparams, &data->device);
slouken@1900
   458
    if (FAILED(result)) {
slouken@1913
   459
        D3D_DestroyRenderer(renderer);
slouken@1900
   460
        D3D_SetError("CreateDevice()", result);
slouken@1900
   461
        return NULL;
slouken@1900
   462
    }
slouken@1900
   463
    data->beginScene = SDL_TRUE;
slouken@5484
   464
    data->scaleMode = D3DTEXF_FORCE_DWORD;
slouken@1900
   465
slouken@1907
   466
    /* Get presentation parameters to fill info */
slouken@1907
   467
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   468
    if (FAILED(result)) {
slouken@1913
   469
        D3D_DestroyRenderer(renderer);
slouken@1907
   470
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   471
        return NULL;
slouken@1907
   472
    }
slouken@1907
   473
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   474
    if (FAILED(result)) {
slouken@1907
   475
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   476
        D3D_DestroyRenderer(renderer);
slouken@1907
   477
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   478
        return NULL;
slouken@1907
   479
    }
slouken@1907
   480
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   481
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   482
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   483
    }
slouken@1975
   484
    data->pparams = pparams;
slouken@1907
   485
slouken@1925
   486
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   487
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   488
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@6232
   489
    data->NumSimultaneousRTs = caps.NumSimultaneousRTs;
slouken@1918
   490
slouken@1903
   491
    /* Set up parameters for rendering */
slouken@1904
   492
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   493
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   494
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   495
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   496
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   497
                                    D3DCULL_NONE);
slouken@1904
   498
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   499
    /* Enable color modulation by diffuse color */
slouken@1988
   500
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   501
                                          D3DTOP_MODULATE);
slouken@1988
   502
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   503
                                          D3DTA_TEXTURE);
slouken@1988
   504
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   505
                                          D3DTA_DIFFUSE);
slouken@1988
   506
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   507
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   508
                                          D3DTOP_MODULATE);
slouken@1988
   509
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   510
                                          D3DTA_TEXTURE);
slouken@1988
   511
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   512
                                          D3DTA_DIFFUSE);
slouken@1991
   513
    /* Disable second texture stage, since we're done */
slouken@1991
   514
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   515
                                          D3DTOP_DISABLE);
slouken@1991
   516
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   517
                                          D3DTOP_DISABLE);
slouken@1903
   518
slouken@6232
   519
    /* Store the default render target */
slouken@6232
   520
    IDirect3DDevice9_GetRenderTarget(data->device, 0, &data->defaultRenderTarget );
slouken@6232
   521
    data->currentRenderTarget = NULL;
slouken@6232
   522
    data->renderTargetActive = SDL_FALSE;
slouken@6232
   523
slouken@5297
   524
    /* Set an identity world and view matrix */
slouken@5297
   525
    matrix.m[0][0] = 1.0f;
slouken@5297
   526
    matrix.m[0][1] = 0.0f;
slouken@5297
   527
    matrix.m[0][2] = 0.0f;
slouken@5297
   528
    matrix.m[0][3] = 0.0f;
slouken@5297
   529
    matrix.m[1][0] = 0.0f;
slouken@5297
   530
    matrix.m[1][1] = 1.0f;
slouken@5297
   531
    matrix.m[1][2] = 0.0f;
slouken@5297
   532
    matrix.m[1][3] = 0.0f;
slouken@5297
   533
    matrix.m[2][0] = 0.0f;
slouken@5297
   534
    matrix.m[2][1] = 0.0f;
slouken@5297
   535
    matrix.m[2][2] = 1.0f;
slouken@5297
   536
    matrix.m[2][3] = 0.0f;
slouken@5297
   537
    matrix.m[3][0] = 0.0f;
slouken@5297
   538
    matrix.m[3][1] = 0.0f;
slouken@5297
   539
    matrix.m[3][2] = 0.0f;
slouken@5297
   540
    matrix.m[3][3] = 1.0f;
slouken@5297
   541
    IDirect3DDevice9_SetTransform(data->device, D3DTS_WORLD, &matrix);
slouken@5297
   542
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, &matrix);
slouken@5297
   543
slouken@1895
   544
    return renderer;
slouken@1895
   545
}
slouken@1895
   546
slouken@5297
   547
static void
slouken@5297
   548
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1975
   549
{
slouken@1975
   550
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   551
slouken@5297
   552
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   553
        data->updateSize = SDL_TRUE;
slouken@1975
   554
    }
slouken@1975
   555
}
slouken@1975
   556
slouken@5484
   557
static D3DTEXTUREFILTERTYPE
slouken@5484
   558
GetScaleQuality(void)
slouken@5484
   559
{
slouken@5484
   560
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   561
slouken@5484
   562
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   563
        return D3DTEXF_POINT;
slouken@5484
   564
    } else if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) {
slouken@5484
   565
        return D3DTEXF_LINEAR;
slouken@5484
   566
    } else {
slouken@5484
   567
        return D3DTEXF_ANISOTROPIC;
slouken@5484
   568
    }
slouken@5484
   569
}
slouken@5484
   570
slouken@1975
   571
static int
slouken@6232
   572
D3D_SetTargetTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@6232
   573
{
slouken@6232
   574
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@6232
   575
    D3D_TextureData *texturedata;
slouken@6232
   576
    HRESULT result;
slouken@6232
   577
slouken@6232
   578
    if (!renderer) return -1;
slouken@6232
   579
    D3D_ActivateRenderer(renderer);
slouken@6232
   580
slouken@6232
   581
    if (data->NumSimultaneousRTs < 2) {
slouken@6232
   582
        SDL_Unsupported();
slouken@6232
   583
        return -1;
slouken@6232
   584
    }
slouken@6232
   585
slouken@6232
   586
    // Release the previous render target if it wasn't the default one
slouken@6232
   587
    if (data->currentRenderTarget != NULL) {
slouken@6232
   588
        IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6232
   589
        data->currentRenderTarget = NULL;
slouken@6232
   590
    }
slouken@6232
   591
slouken@6232
   592
    /* Prepare an identity world and view matrix */
slouken@6232
   593
    D3DMATRIX matrix;
slouken@6232
   594
    matrix.m[0][0] = 1.0f;
slouken@6232
   595
    matrix.m[0][1] = 0.0f;
slouken@6232
   596
    matrix.m[0][2] = 0.0f;
slouken@6232
   597
    matrix.m[0][3] = 0.0f;
slouken@6232
   598
    matrix.m[1][0] = 0.0f;
slouken@6232
   599
    matrix.m[1][1] = 1.0f;
slouken@6232
   600
    matrix.m[1][2] = 0.0f;
slouken@6232
   601
    matrix.m[1][3] = 0.0f;
slouken@6232
   602
    matrix.m[2][0] = 0.0f;
slouken@6232
   603
    matrix.m[2][1] = 0.0f;
slouken@6232
   604
    matrix.m[2][2] = 1.0f;
slouken@6232
   605
    matrix.m[2][3] = 0.0f;
slouken@6232
   606
    matrix.m[3][0] = 0.0f;
slouken@6232
   607
    matrix.m[3][1] = 0.0f;
slouken@6232
   608
    matrix.m[3][2] = 0.0f;
slouken@6232
   609
    matrix.m[3][3] = 1.0f;
slouken@6232
   610
slouken@6232
   611
    if (texture == NULL) {
slouken@6232
   612
        if (data->renderTargetActive) {
slouken@6232
   613
            data->renderTargetActive = SDL_FALSE;
slouken@6232
   614
            IDirect3DDevice9_SetRenderTarget(data->device, 0, data->defaultRenderTarget );
slouken@6232
   615
            renderer->viewport = data->viewport_copy;
slouken@6232
   616
            D3D_UpdateViewport(renderer);
slouken@6232
   617
        }
slouken@6232
   618
        return 0;
slouken@6232
   619
    }
slouken@6232
   620
    if (renderer != texture->renderer) return -1;
slouken@6232
   621
slouken@6232
   622
    if ( !data->renderTargetActive ) {
slouken@6232
   623
        data->viewport_copy = renderer->viewport;
slouken@6232
   624
    }
slouken@6232
   625
slouken@6232
   626
    texturedata = (D3D_TextureData *) texture->driverdata;
slouken@6232
   627
    result = IDirect3DTexture9_GetSurfaceLevel(texturedata->texture, 0, &data->currentRenderTarget );
slouken@6232
   628
    if(FAILED(result)) {
slouken@6232
   629
        return -1;
slouken@6232
   630
    }
slouken@6232
   631
    result = IDirect3DDevice9_SetRenderTarget(data->device, 0, data->currentRenderTarget );
slouken@6232
   632
    if(FAILED(result)) {
slouken@6232
   633
        return -1;
slouken@6232
   634
    }
slouken@6232
   635
slouken@6232
   636
    data->renderTargetActive = SDL_TRUE;
slouken@6232
   637
    renderer->viewport.x = 0;
slouken@6232
   638
    renderer->viewport.y = 0;
slouken@6232
   639
    renderer->viewport.w = texture->w;
slouken@6232
   640
    renderer->viewport.h = texture->h;
slouken@6232
   641
    D3D_UpdateViewport(renderer);
slouken@6232
   642
    return 0;
slouken@6232
   643
}
slouken@6232
   644
slouken@6232
   645
static int
slouken@1913
   646
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   647
{
slouken@1913
   648
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@3685
   649
    SDL_Window *window = renderer->window;
slouken@5154
   650
    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
slouken@1913
   651
    D3D_TextureData *data;
slouken@5173
   652
    D3DPOOL pool;
slouken@5173
   653
    DWORD usage;
slouken@1903
   654
    HRESULT result;
slouken@1895
   655
slouken@1920
   656
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   657
    if (!data) {
slouken@1895
   658
        SDL_OutOfMemory();
slouken@1895
   659
        return -1;
slouken@1895
   660
    }
slouken@5484
   661
    data->scaleMode = GetScaleQuality();
slouken@1895
   662
slouken@1895
   663
    texture->driverdata = data;
slouken@1895
   664
slouken@5173
   665
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   666
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   667
        pool = D3DPOOL_DEFAULT;
slouken@5173
   668
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   669
    } else
slouken@5173
   670
#endif
slouken@6232
   671
    if (texture->access == SDL_TEXTUREACCESS_TARGET) {
slouken@6232
   672
        pool = D3DPOOL_DEFAULT;         // D3DPOOL_MANAGED does not work with usage=D3DUSAGE_RENDERTARGET
slouken@6232
   673
        usage = D3DUSAGE_RENDERTARGET;
slouken@6232
   674
    }
slouken@6232
   675
    else
slouken@5173
   676
    {
slouken@5173
   677
        pool = D3DPOOL_MANAGED;
slouken@5173
   678
        usage = 0;
slouken@5173
   679
    }
slouken@2973
   680
slouken@1903
   681
    result =
slouken@1903
   682
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   683
                                       texture->h, 1, usage,
slouken@5173
   684
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   685
                                       pool, &data->texture, NULL);
slouken@1903
   686
    if (FAILED(result)) {
slouken@1903
   687
        D3D_SetError("CreateTexture()", result);
slouken@1903
   688
        return -1;
slouken@1903
   689
    }
slouken@1903
   690
slouken@1895
   691
    return 0;
slouken@1895
   692
}
slouken@1895
   693
slouken@1895
   694
static int
slouken@1913
   695
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   696
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   697
{
slouken@1913
   698
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   699
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@5156
   700
    RECT d3drect;
slouken@5156
   701
    D3DLOCKED_RECT locked;
slouken@5156
   702
    const Uint8 *src;
slouken@5156
   703
    Uint8 *dst;
slouken@5156
   704
    int row, length;
slouken@5156
   705
    HRESULT result;
slouken@2973
   706
slouken@5173
   707
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   708
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
   709
        rect->x == 0 && rect->y == 0 &&
slouken@5173
   710
        rect->w == texture->w && rect->h == texture->h) {
slouken@5194
   711
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@5173
   712
    } else
slouken@5173
   713
#endif
slouken@5173
   714
    {
slouken@5173
   715
        d3drect.left = rect->x;
slouken@5173
   716
        d3drect.right = rect->x + rect->w;
slouken@5173
   717
        d3drect.top = rect->y;
slouken@5173
   718
        d3drect.bottom = rect->y + rect->h;
slouken@5194
   719
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   720
    }
slouken@2973
   721
slouken@5156
   722
    if (FAILED(result)) {
slouken@5156
   723
        D3D_SetError("LockRect()", result);
slouken@5156
   724
        return -1;
slouken@5156
   725
    }
slouken@2973
   726
slouken@5156
   727
    src = pixels;
slouken@5156
   728
    dst = locked.pBits;
slouken@5156
   729
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5173
   730
    if (length == pitch && length == locked.Pitch) {
slouken@5173
   731
        SDL_memcpy(dst, src, length*rect->h);
slouken@5173
   732
    } else {
slouken@5173
   733
        for (row = 0; row < rect->h; ++row) {
slouken@5173
   734
            SDL_memcpy(dst, src, length);
slouken@5173
   735
            src += pitch;
slouken@5173
   736
            dst += locked.Pitch;
slouken@5173
   737
        }
slouken@5156
   738
    }
slouken@5156
   739
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   740
slouken@5156
   741
    return 0;
slouken@1895
   742
}
slouken@1895
   743
slouken@1895
   744
static int
slouken@1913
   745
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   746
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   747
{
slouken@1913
   748
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   749
    RECT d3drect;
slouken@5156
   750
    D3DLOCKED_RECT locked;
slouken@5156
   751
    HRESULT result;
slouken@1895
   752
slouken@5156
   753
    d3drect.left = rect->x;
slouken@5156
   754
    d3drect.right = rect->x + rect->w;
slouken@5156
   755
    d3drect.top = rect->y;
slouken@5156
   756
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   757
slouken@5156
   758
    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   759
    if (FAILED(result)) {
slouken@5156
   760
        D3D_SetError("LockRect()", result);
slouken@5156
   761
        return -1;
slouken@1903
   762
    }
slouken@5156
   763
    *pixels = locked.pBits;
slouken@5156
   764
    *pitch = locked.Pitch;
slouken@5156
   765
    return 0;
slouken@1895
   766
}
slouken@1895
   767
slouken@1895
   768
static void
slouken@1913
   769
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   770
{
slouken@1913
   771
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   772
slouken@5156
   773
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   774
}
slouken@1895
   775
slouken@5297
   776
static int
slouken@5297
   777
D3D_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   778
{
slouken@5224
   779
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   780
    D3DVIEWPORT9 viewport;
slouken@5297
   781
    D3DMATRIX matrix;
slouken@5224
   782
slouken@5297
   783
    /* Set the viewport */
slouken@5297
   784
    viewport.X = renderer->viewport.x;
slouken@5297
   785
    viewport.Y = renderer->viewport.y;
slouken@5297
   786
    viewport.Width = renderer->viewport.w;
slouken@5297
   787
    viewport.Height = renderer->viewport.h;
slouken@5297
   788
    viewport.MinZ = 0.0f;
slouken@5297
   789
    viewport.MaxZ = 1.0f;
slouken@5297
   790
    IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5224
   791
slouken@5297
   792
    /* Set an orthographic projection matrix */
slouken@5297
   793
    matrix.m[0][0] = 2.0f / renderer->viewport.w;
slouken@5297
   794
    matrix.m[0][1] = 0.0f;
slouken@5297
   795
    matrix.m[0][2] = 0.0f;
slouken@5297
   796
    matrix.m[0][3] = 0.0f;
slouken@5297
   797
    matrix.m[1][0] = 0.0f;
slouken@5297
   798
    matrix.m[1][1] = -2.0f / renderer->viewport.h;
slouken@5297
   799
    matrix.m[1][2] = 0.0f;
slouken@5297
   800
    matrix.m[1][3] = 0.0f;
slouken@5297
   801
    matrix.m[2][0] = 0.0f;
slouken@5297
   802
    matrix.m[2][1] = 0.0f;
slouken@5297
   803
    matrix.m[2][2] = 1.0f;
slouken@5297
   804
    matrix.m[2][3] = 0.0f;
slouken@5297
   805
    matrix.m[3][0] = -1.0f;
slouken@5297
   806
    matrix.m[3][1] = 1.0f;
slouken@5297
   807
    matrix.m[3][2] = 0.0f;
slouken@5297
   808
    matrix.m[3][3] = 1.0f;
slouken@5297
   809
    IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &matrix);
slouken@5297
   810
slouken@5297
   811
    return 0;
slouken@5297
   812
}
slouken@5297
   813
slouken@5297
   814
static int
slouken@5297
   815
D3D_RenderClear(SDL_Renderer * renderer)
slouken@5297
   816
{
slouken@5297
   817
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   818
    DWORD color;
slouken@5297
   819
    HRESULT result;
slouken@5297
   820
slouken@5297
   821
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   822
        return -1;
slouken@5224
   823
    }
slouken@5297
   824
slouken@5297
   825
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@5297
   826
slouken@5299
   827
    /* Don't reset the viewport if we don't have to! */
slouken@5299
   828
    if (!renderer->viewport.x && !renderer->viewport.y &&
slouken@5299
   829
        renderer->viewport.w == data->pparams.BackBufferWidth &&
slouken@5299
   830
        renderer->viewport.h == data->pparams.BackBufferHeight) {
slouken@5299
   831
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   832
    } else {
slouken@5299
   833
        D3DVIEWPORT9 viewport;
slouken@5297
   834
slouken@5299
   835
        /* Clear is defined to clear the entire render target */
slouken@5299
   836
        viewport.X = 0;
slouken@5299
   837
        viewport.Y = 0;
slouken@5299
   838
        viewport.Width = data->pparams.BackBufferWidth;
slouken@5299
   839
        viewport.Height = data->pparams.BackBufferHeight;
slouken@5299
   840
        viewport.MinZ = 0.0f;
slouken@5299
   841
        viewport.MaxZ = 1.0f;
slouken@5299
   842
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   843
slouken@5299
   844
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   845
slouken@5299
   846
        /* Reset the viewport */
slouken@5299
   847
        viewport.X = renderer->viewport.x;
slouken@5299
   848
        viewport.Y = renderer->viewport.y;
slouken@5299
   849
        viewport.Width = renderer->viewport.w;
slouken@5299
   850
        viewport.Height = renderer->viewport.h;
slouken@5299
   851
        viewport.MinZ = 0.0f;
slouken@5299
   852
        viewport.MaxZ = 1.0f;
slouken@5299
   853
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   854
    }
slouken@5297
   855
slouken@5297
   856
    if (FAILED(result)) {
slouken@5297
   857
        D3D_SetError("Clear()", result);
slouken@5297
   858
        return -1;
slouken@5297
   859
    }
slouken@5297
   860
    return 0;
slouken@5224
   861
}
slouken@5224
   862
slouken@5224
   863
static void
slouken@2933
   864
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   865
{
slouken@2932
   866
    switch (blendMode) {
slouken@2932
   867
    case SDL_BLENDMODE_NONE:
slouken@2932
   868
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   869
                                        FALSE);
slouken@2932
   870
        break;
slouken@2932
   871
    case SDL_BLENDMODE_BLEND:
slouken@2932
   872
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   873
                                        TRUE);
slouken@2932
   874
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   875
                                        D3DBLEND_SRCALPHA);
slouken@2932
   876
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   877
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   878
        break;
slouken@2932
   879
    case SDL_BLENDMODE_ADD:
slouken@2932
   880
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   881
                                        TRUE);
slouken@2932
   882
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   883
                                        D3DBLEND_SRCALPHA);
slouken@2932
   884
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   885
                                        D3DBLEND_ONE);
slouken@2932
   886
        break;
slouken@5184
   887
    case SDL_BLENDMODE_MOD:
slouken@5184
   888
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@5184
   889
                                        TRUE);
slouken@5184
   890
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@5184
   891
                                        D3DBLEND_ZERO);
slouken@5184
   892
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@5184
   893
                                        D3DBLEND_SRCCOLOR);
slouken@5184
   894
        break;
slouken@2932
   895
    }
slouken@2932
   896
}
slouken@2932
   897
slouken@1895
   898
static int
slouken@3599
   899
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   900
                     int count)
slouken@1895
   901
{
slouken@1913
   902
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   903
    DWORD color;
slouken@3536
   904
    Vertex *vertices;
slouken@3536
   905
    int i;
slouken@1900
   906
    HRESULT result;
slouken@1895
   907
slouken@5297
   908
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   909
        return -1;
slouken@1900
   910
    }
slouken@1895
   911
slouken@2932
   912
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   913
slouken@2933
   914
    result =
slouken@2933
   915
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   916
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   917
    if (FAILED(result)) {
slouken@2932
   918
        D3D_SetError("SetTexture()", result);
slouken@2932
   919
        return -1;
slouken@2932
   920
    }
slouken@3536
   921
slouken@3536
   922
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   923
slouken@3536
   924
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   925
    for (i = 0; i < count; ++i) {
slouken@3536
   926
        vertices[i].x = (float) points[i].x;
slouken@3536
   927
        vertices[i].y = (float) points[i].y;
slouken@3536
   928
        vertices[i].z = 0.0f;
slouken@3536
   929
        vertices[i].color = color;
slouken@3536
   930
        vertices[i].u = 0.0f;
slouken@3536
   931
        vertices[i].v = 0.0f;
slouken@3536
   932
    }
slouken@1903
   933
    result =
slouken@3536
   934
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
   935
                                         vertices, sizeof(*vertices));
slouken@3536
   936
    SDL_stack_free(vertices);
slouken@1900
   937
    if (FAILED(result)) {
slouken@2932
   938
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   939
        return -1;
slouken@2932
   940
    }
slouken@2932
   941
    return 0;
slouken@2932
   942
}
slouken@2932
   943
slouken@2932
   944
static int
slouken@3599
   945
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   946
                    int count)
slouken@2932
   947
{
slouken@2932
   948
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   949
    DWORD color;
slouken@3536
   950
    Vertex *vertices;
slouken@3536
   951
    int i;
slouken@2932
   952
    HRESULT result;
slouken@2932
   953
slouken@5297
   954
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   955
        return -1;
slouken@2932
   956
    }
slouken@2932
   957
slouken@2932
   958
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   959
slouken@2933
   960
    result =
slouken@2933
   961
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   962
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   963
    if (FAILED(result)) {
slouken@2932
   964
        D3D_SetError("SetTexture()", result);
slouken@2932
   965
        return -1;
slouken@2932
   966
    }
slouken@3536
   967
slouken@3536
   968
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   969
slouken@3536
   970
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   971
    for (i = 0; i < count; ++i) {
slouken@3536
   972
        vertices[i].x = (float) points[i].x;
slouken@3536
   973
        vertices[i].y = (float) points[i].y;
slouken@3536
   974
        vertices[i].z = 0.0f;
slouken@3536
   975
        vertices[i].color = color;
slouken@3536
   976
        vertices[i].u = 0.0f;
slouken@3536
   977
        vertices[i].v = 0.0f;
slouken@3536
   978
    }
slouken@2932
   979
    result =
slouken@3551
   980
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
   981
                                         vertices, sizeof(*vertices));
slouken@3551
   982
slouken@3551
   983
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
   984
       so we need to close the endpoint of the line */
slouken@6076
   985
    if (count == 2 ||
slouken@6076
   986
        points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3551
   987
        vertices[0].x = (float) points[count-1].x;
slouken@3551
   988
        vertices[0].y = (float) points[count-1].y;
slouken@3551
   989
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
   990
    }
slouken@3551
   991
slouken@3536
   992
    SDL_stack_free(vertices);
slouken@2932
   993
    if (FAILED(result)) {
slouken@2932
   994
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   995
        return -1;
slouken@2932
   996
    }
slouken@2932
   997
    return 0;
slouken@2932
   998
}
slouken@2932
   999
slouken@2932
  1000
static int
slouken@5297
  1001
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect * rects,
slouken@3599
  1002
                    int count)
slouken@2932
  1003
{
slouken@2932
  1004
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
  1005
    DWORD color;
slouken@3536
  1006
    int i;
slouken@2932
  1007
    float minx, miny, maxx, maxy;
slouken@2932
  1008
    Vertex vertices[4];
slouken@2932
  1009
    HRESULT result;
slouken@2932
  1010
slouken@5297
  1011
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1012
        return -1;
slouken@2932
  1013
    }
slouken@2932
  1014
slouken@2932
  1015
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1016
slouken@2933
  1017
    result =
slouken@2933
  1018
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1019
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1020
    if (FAILED(result)) {
slouken@2932
  1021
        D3D_SetError("SetTexture()", result);
slouken@2932
  1022
        return -1;
slouken@2932
  1023
    }
slouken@3536
  1024
slouken@3536
  1025
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
  1026
slouken@3536
  1027
    for (i = 0; i < count; ++i) {
slouken@5297
  1028
        const SDL_Rect *rect = &rects[i];
slouken@3536
  1029
slouken@3536
  1030
        minx = (float) rect->x;
slouken@3536
  1031
        miny = (float) rect->y;
slouken@3536
  1032
        maxx = (float) rect->x + rect->w;
slouken@3536
  1033
        maxy = (float) rect->y + rect->h;
slouken@3536
  1034
slouken@3536
  1035
        vertices[0].x = minx;
slouken@3536
  1036
        vertices[0].y = miny;
slouken@3536
  1037
        vertices[0].z = 0.0f;
slouken@3536
  1038
        vertices[0].color = color;
slouken@3536
  1039
        vertices[0].u = 0.0f;
slouken@3536
  1040
        vertices[0].v = 0.0f;
slouken@3536
  1041
slouken@3536
  1042
        vertices[1].x = maxx;
slouken@3536
  1043
        vertices[1].y = miny;
slouken@3536
  1044
        vertices[1].z = 0.0f;
slouken@3536
  1045
        vertices[1].color = color;
slouken@3536
  1046
        vertices[1].u = 0.0f;
slouken@3536
  1047
        vertices[1].v = 0.0f;
slouken@3536
  1048
slouken@3536
  1049
        vertices[2].x = maxx;
slouken@3536
  1050
        vertices[2].y = maxy;
slouken@3536
  1051
        vertices[2].z = 0.0f;
slouken@3536
  1052
        vertices[2].color = color;
slouken@3536
  1053
        vertices[2].u = 0.0f;
slouken@3536
  1054
        vertices[2].v = 0.0f;
slouken@3536
  1055
slouken@3536
  1056
        vertices[3].x = minx;
slouken@3536
  1057
        vertices[3].y = maxy;
slouken@3536
  1058
        vertices[3].z = 0.0f;
slouken@3536
  1059
        vertices[3].color = color;
slouken@3536
  1060
        vertices[3].u = 0.0f;
slouken@3536
  1061
        vertices[3].v = 0.0f;
slouken@3536
  1062
slouken@3536
  1063
        result =
slouken@3536
  1064
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
  1065
                                             2, vertices, sizeof(*vertices));
slouken@3536
  1066
        if (FAILED(result)) {
slouken@3536
  1067
            D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
  1068
            return -1;
slouken@3536
  1069
        }
slouken@1900
  1070
    }
slouken@1895
  1071
    return 0;
slouken@1895
  1072
}
slouken@1895
  1073
slouken@1895
  1074
static int
slouken@1913
  1075
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1076
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
  1077
{
slouken@1913
  1078
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1079
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
  1080
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
  1081
    float minx, miny, maxx, maxy;
slouken@1904
  1082
    float minu, maxu, minv, maxv;
slouken@1987
  1083
    DWORD color;
slouken@1903
  1084
    Vertex vertices[4];
slouken@1903
  1085
    HRESULT result;
slouken@1895
  1086
slouken@5297
  1087
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
  1088
        return -1;
slouken@1900
  1089
    }
slouken@1903
  1090
slouken@3279
  1091
    minx = (float) dstrect->x - 0.5f;
slouken@3279
  1092
    miny = (float) dstrect->y - 0.5f;
slouken@3279
  1093
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@3279
  1094
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1095
slouken@1904
  1096
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1097
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1098
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1099
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1100
slouken@1987
  1101
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1102
slouken@1903
  1103
    vertices[0].x = minx;
slouken@1903
  1104
    vertices[0].y = miny;
slouken@1903
  1105
    vertices[0].z = 0.0f;
slouken@1987
  1106
    vertices[0].color = color;
slouken@1904
  1107
    vertices[0].u = minu;
slouken@1904
  1108
    vertices[0].v = minv;
slouken@1904
  1109
slouken@1903
  1110
    vertices[1].x = maxx;
slouken@1903
  1111
    vertices[1].y = miny;
slouken@1903
  1112
    vertices[1].z = 0.0f;
slouken@1987
  1113
    vertices[1].color = color;
slouken@1904
  1114
    vertices[1].u = maxu;
slouken@1904
  1115
    vertices[1].v = minv;
slouken@1904
  1116
slouken@1903
  1117
    vertices[2].x = maxx;
slouken@1903
  1118
    vertices[2].y = maxy;
slouken@1903
  1119
    vertices[2].z = 0.0f;
slouken@1987
  1120
    vertices[2].color = color;
slouken@1904
  1121
    vertices[2].u = maxu;
slouken@1904
  1122
    vertices[2].v = maxv;
slouken@1904
  1123
slouken@1903
  1124
    vertices[3].x = minx;
slouken@1903
  1125
    vertices[3].y = maxy;
slouken@1903
  1126
    vertices[3].z = 0.0f;
slouken@1987
  1127
    vertices[3].color = color;
slouken@1904
  1128
    vertices[3].u = minu;
slouken@1904
  1129
    vertices[3].v = maxv;
slouken@1903
  1130
slouken@2932
  1131
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1132
slouken@5484
  1133
    if (texturedata->scaleMode != data->scaleMode) {
slouken@5484
  1134
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@5484
  1135
                                         texturedata->scaleMode);
slouken@5484
  1136
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@5484
  1137
                                         texturedata->scaleMode);
slouken@5484
  1138
        data->scaleMode = texturedata->scaleMode;
slouken@5484
  1139
    }
slouken@1917
  1140
slouken@1903
  1141
    result =
slouken@2735
  1142
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1143
                                    texturedata->texture);
slouken@1903
  1144
    if (FAILED(result)) {
slouken@1903
  1145
        D3D_SetError("SetTexture()", result);
slouken@1903
  1146
        return -1;
slouken@1903
  1147
    }
slouken@3556
  1148
    if (shader) {
slouken@3556
  1149
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
  1150
        if (FAILED(result)) {
slouken@3556
  1151
            D3D_SetError("SetShader()", result);
slouken@3556
  1152
            return -1;
slouken@3556
  1153
        }
slouken@3556
  1154
    }
slouken@1903
  1155
    result =
slouken@1903
  1156
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1157
                                         vertices, sizeof(*vertices));
slouken@1903
  1158
    if (FAILED(result)) {
slouken@1903
  1159
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1160
        return -1;
slouken@1903
  1161
    }
slouken@3556
  1162
    if (shader) {
slouken@3556
  1163
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
  1164
        if (FAILED(result)) {
slouken@3556
  1165
            D3D_SetError("SetShader()", result);
slouken@3556
  1166
            return -1;
slouken@3556
  1167
        }
slouken@3556
  1168
    }
slouken@1895
  1169
    return 0;
slouken@1895
  1170
}
slouken@1895
  1171
slouken@3427
  1172
static int
slouken@3427
  1173
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
  1174
                     Uint32 format, void * pixels, int pitch)
slouken@3427
  1175
{
slouken@3549
  1176
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3549
  1177
    D3DSURFACE_DESC desc;
slouken@3549
  1178
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
  1179
    LPDIRECT3DSURFACE9 surface;
slouken@3549
  1180
    RECT d3drect;
slouken@3549
  1181
    D3DLOCKED_RECT locked;
slouken@3549
  1182
    HRESULT result;
slouken@3427
  1183
slouken@3549
  1184
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
  1185
    if (FAILED(result)) {
slouken@3549
  1186
        D3D_SetError("GetBackBuffer()", result);
slouken@3549
  1187
        return -1;
slouken@3549
  1188
    }
slouken@3427
  1189
slouken@3549
  1190
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
  1191
    if (FAILED(result)) {
slouken@3549
  1192
        D3D_SetError("GetDesc()", result);
slouken@3549
  1193
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1194
        return -1;
slouken@3549
  1195
    }
slouken@3427
  1196
slouken@3549
  1197
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
  1198
    if (FAILED(result)) {
slouken@3549
  1199
        D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
  1200
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1201
        return -1;
slouken@3549
  1202
    }
slouken@3427
  1203
slouken@3549
  1204
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
  1205
    if (FAILED(result)) {
slouken@3549
  1206
        D3D_SetError("GetRenderTargetData()", result);
slouken@3549
  1207
        IDirect3DSurface9_Release(surface);
slouken@3549
  1208
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1209
        return -1;
slouken@3549
  1210
    }
slouken@3427
  1211
slouken@3549
  1212
    d3drect.left = rect->x;
slouken@3549
  1213
    d3drect.right = rect->x + rect->w;
slouken@3549
  1214
    d3drect.top = rect->y;
slouken@3549
  1215
    d3drect.bottom = rect->y + rect->h;
slouken@3427
  1216
slouken@3549
  1217
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
  1218
    if (FAILED(result)) {
slouken@3549
  1219
        D3D_SetError("LockRect()", result);
slouken@3549
  1220
        IDirect3DSurface9_Release(surface);
slouken@3549
  1221
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1222
        return -1;
slouken@3549
  1223
    }
slouken@3427
  1224
slouken@3549
  1225
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
  1226
                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
slouken@3549
  1227
                      format, pixels, pitch);
slouken@3549
  1228
slouken@3549
  1229
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
  1230
slouken@3549
  1231
    IDirect3DSurface9_Release(surface);
slouken@3549
  1232
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
  1233
slouken@3549
  1234
    return 0;
slouken@3427
  1235
}
slouken@3427
  1236
slouken@1895
  1237
static void
slouken@1913
  1238
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1239
{
slouken@1913
  1240
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1241
    HRESULT result;
slouken@1900
  1242
slouken@1900
  1243
    if (!data->beginScene) {
slouken@1900
  1244
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1245
        data->beginScene = SDL_TRUE;
slouken@1900
  1246
    }
slouken@1900
  1247
slouken@1975
  1248
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1249
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1250
        /* We'll reset later */
slouken@1975
  1251
        return;
slouken@1975
  1252
    }
slouken@1975
  1253
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1254
        D3D_Reset(renderer);
slouken@1975
  1255
    }
slouken@1900
  1256
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1257
    if (FAILED(result)) {
slouken@1900
  1258
        D3D_SetError("Present()", result);
slouken@1900
  1259
    }
slouken@1895
  1260
}
slouken@1895
  1261
slouken@1895
  1262
static void
slouken@1913
  1263
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1264
{
slouken@1913
  1265
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1266
slouken@1895
  1267
    if (!data) {
slouken@1895
  1268
        return;
slouken@1895
  1269
    }
slouken@1903
  1270
    if (data->texture) {
slouken@1903
  1271
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1272
    }
slouken@1895
  1273
    SDL_free(data);
slouken@1895
  1274
    texture->driverdata = NULL;
slouken@1895
  1275
}
slouken@1895
  1276
slouken@1975
  1277
static void
slouken@1913
  1278
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1279
{
slouken@1913
  1280
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1281
slouken@1895
  1282
    if (data) {
slouken@6232
  1283
        // Release the render target
slouken@6232
  1284
        IDirect3DSurface9_Release(data->defaultRenderTarget);
slouken@6232
  1285
        if (data->currentRenderTarget != NULL) {
slouken@6232
  1286
            IDirect3DSurface9_Release(data->currentRenderTarget);
slouken@6232
  1287
            data->currentRenderTarget = NULL;
slouken@6232
  1288
        }
slouken@6232
  1289
        
slouken@1900
  1290
        if (data->device) {
slouken@1900
  1291
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1292
        }
slouken@5154
  1293
        if (data->d3d) {
slouken@5154
  1294
            IDirect3D9_Release(data->d3d);
slouken@5154
  1295
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1296
        }
slouken@1895
  1297
        SDL_free(data);
slouken@1895
  1298
    }
slouken@1895
  1299
    SDL_free(renderer);
slouken@1895
  1300
}
slouken@1895
  1301
slouken@5226
  1302
#endif /* SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED */
slouken@1895
  1303
slouken@1895
  1304
/* vi: set ts=4 sw=4 expandtab: */