src/render/direct3d/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 04 Feb 2011 12:25:26 -0800
changeset 5173 ebfedf3787b1
parent 5166 4d39eeaad00b
child 5184 d976b67150c5
permissions -rwxr-xr-x
Standardized on using the managed texture pool.
Also experimented with dynamic textures, but didn't get any speed increase with them. More research and testing is needed.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#if SDL_VIDEO_RENDER_D3D
slouken@1895
    25
slouken@5154
    26
#include "../../core/windows/SDL_windows.h"
slouken@5154
    27
slouken@5154
    28
#include "SDL_loadso.h"
slouken@5154
    29
#include "SDL_syswm.h"
slouken@5154
    30
#include "../SDL_sysrender.h"
slouken@5154
    31
slouken@5154
    32
#if SDL_VIDEO_RENDER_D3D
slouken@5154
    33
#define D3D_DEBUG_INFO
slouken@5154
    34
#include <d3d9.h>
slouken@5154
    35
#endif
slouken@1895
    36
slouken@3556
    37
#ifdef ASSEMBLE_SHADER
slouken@3556
    38
///////////////////////////////////////////////////////////////////////////
slouken@3556
    39
// ID3DXBuffer:
slouken@3556
    40
// ------------
slouken@3556
    41
// The buffer object is used by D3DX to return arbitrary size data.
slouken@3556
    42
//
slouken@3556
    43
// GetBufferPointer -
slouken@3556
    44
//    Returns a pointer to the beginning of the buffer.
slouken@3556
    45
//
slouken@3556
    46
// GetBufferSize -
slouken@3556
    47
//    Returns the size of the buffer, in bytes.
slouken@3556
    48
///////////////////////////////////////////////////////////////////////////
slouken@3556
    49
slouken@3556
    50
typedef interface ID3DXBuffer ID3DXBuffer;
slouken@3556
    51
typedef interface ID3DXBuffer *LPD3DXBUFFER;
slouken@3556
    52
slouken@3556
    53
// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
slouken@3556
    54
DEFINE_GUID(IID_ID3DXBuffer, 
slouken@3556
    55
0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
slouken@3556
    56
slouken@3556
    57
#undef INTERFACE
slouken@3556
    58
#define INTERFACE ID3DXBuffer
slouken@3556
    59
slouken@3556
    60
typedef interface ID3DXBuffer {
slouken@3556
    61
    const struct ID3DXBufferVtbl FAR* lpVtbl;
slouken@3556
    62
} ID3DXBuffer;
slouken@3556
    63
typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
slouken@3556
    64
const struct ID3DXBufferVtbl
slouken@3556
    65
{
slouken@3556
    66
    // IUnknown
slouken@3556
    67
    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
slouken@3556
    68
    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
slouken@3556
    69
    STDMETHOD_(ULONG, Release)(THIS) PURE;
slouken@3556
    70
slouken@3556
    71
    // ID3DXBuffer
slouken@3556
    72
    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
slouken@3556
    73
    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
slouken@3556
    74
};
slouken@3556
    75
slouken@3556
    76
HRESULT WINAPI
slouken@3556
    77
    D3DXAssembleShader(
slouken@3556
    78
        LPCSTR                          pSrcData,
slouken@3556
    79
        UINT                            SrcDataLen,
slouken@3556
    80
        CONST LPVOID*                   pDefines,
slouken@3556
    81
        LPVOID                          pInclude,
slouken@3556
    82
        DWORD                           Flags,
slouken@3556
    83
        LPD3DXBUFFER*                   ppShader,
slouken@3556
    84
        LPD3DXBUFFER*                   ppErrorMsgs);
slouken@3556
    85
slouken@3556
    86
#endif /* ASSEMBLE_SHADER */
slouken@3556
    87
slouken@3556
    88
slouken@1895
    89
/* Direct3D renderer implementation */
slouken@1895
    90
slouken@1913
    91
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1913
    92
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    93
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    94
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
    95
                             int pitch);
slouken@1913
    96
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
    97
                           const SDL_Rect * rect, void **pixels, int *pitch);
slouken@1913
    98
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@3599
    99
static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
slouken@3599
   100
                                const SDL_Point * points, int count);
slouken@3599
   101
static int D3D_RenderDrawLines(SDL_Renderer * renderer,
slouken@3599
   102
                               const SDL_Point * points, int count);
slouken@3599
   103
static int D3D_RenderFillRects(SDL_Renderer * renderer,
slouken@3599
   104
                               const SDL_Rect ** rects, int count);
slouken@1913
   105
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   106
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
   107
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   108
                                Uint32 format, void * pixels, int pitch);
slouken@1913
   109
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
   110
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
   111
                               SDL_Texture * texture);
slouken@1913
   112
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
   113
slouken@1895
   114
slouken@1913
   115
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
   116
    D3D_CreateRenderer,
slouken@1895
   117
    {
slouken@1895
   118
     "d3d",
slouken@5142
   119
     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
slouken@5156
   120
     1,
slouken@5156
   121
     {SDL_PIXELFORMAT_ARGB8888},
slouken@1895
   122
     0,
slouken@1895
   123
     0}
slouken@1895
   124
};
slouken@1895
   125
slouken@1895
   126
typedef struct
slouken@1895
   127
{
slouken@5154
   128
    void* d3dDLL;
slouken@2973
   129
    IDirect3D9 *d3d;
slouken@1895
   130
    IDirect3DDevice9 *device;
slouken@3527
   131
    UINT adapter;
slouken@1975
   132
    D3DPRESENT_PARAMETERS pparams;
slouken@1900
   133
    SDL_bool beginScene;
slouken@1913
   134
} D3D_RenderData;
slouken@1895
   135
slouken@1895
   136
typedef struct
slouken@1895
   137
{
slouken@1903
   138
    IDirect3DTexture9 *texture;
slouken@1913
   139
} D3D_TextureData;
slouken@1895
   140
slouken@1903
   141
typedef struct
slouken@1903
   142
{
slouken@1903
   143
    float x, y, z;
slouken@1904
   144
    float rhw;
slouken@1987
   145
    DWORD color;
slouken@1904
   146
    float u, v;
slouken@1903
   147
} Vertex;
slouken@1903
   148
slouken@1895
   149
static void
slouken@1900
   150
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   151
{
slouken@1900
   152
    const char *error;
slouken@1900
   153
slouken@1900
   154
    switch (result) {
slouken@1900
   155
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   156
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   157
        break;
slouken@1900
   158
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   159
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   160
        break;
slouken@1900
   161
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   162
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   163
        break;
slouken@1900
   164
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   165
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   166
        break;
slouken@1900
   167
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   168
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   169
        break;
slouken@1900
   170
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   171
        error = "TOOMANYOPERATIONS";
slouken@1900
   172
        break;
slouken@1900
   173
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   174
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   175
        break;
slouken@1900
   176
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   177
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   178
        break;
slouken@1900
   179
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   180
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   181
        break;
slouken@1900
   182
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   183
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   184
        break;
slouken@1900
   185
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   186
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   187
        break;
slouken@1900
   188
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   189
        error = "DRIVERINTERNALERROR";
slouken@1900
   190
        break;
slouken@1900
   191
    case D3DERR_NOTFOUND:
slouken@1900
   192
        error = "NOTFOUND";
slouken@1900
   193
        break;
slouken@1900
   194
    case D3DERR_MOREDATA:
slouken@1900
   195
        error = "MOREDATA";
slouken@1900
   196
        break;
slouken@1900
   197
    case D3DERR_DEVICELOST:
slouken@1900
   198
        error = "DEVICELOST";
slouken@1900
   199
        break;
slouken@1900
   200
    case D3DERR_DEVICENOTRESET:
slouken@1900
   201
        error = "DEVICENOTRESET";
slouken@1900
   202
        break;
slouken@1900
   203
    case D3DERR_NOTAVAILABLE:
slouken@1900
   204
        error = "NOTAVAILABLE";
slouken@1900
   205
        break;
slouken@1900
   206
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   207
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   208
        break;
slouken@1900
   209
    case D3DERR_INVALIDDEVICE:
slouken@1900
   210
        error = "INVALIDDEVICE";
slouken@1900
   211
        break;
slouken@1900
   212
    case D3DERR_INVALIDCALL:
slouken@1900
   213
        error = "INVALIDCALL";
slouken@1900
   214
        break;
slouken@1900
   215
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   216
        error = "DRIVERINVALIDCALL";
slouken@1900
   217
        break;
slouken@1900
   218
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   219
        error = "WASSTILLDRAWING";
slouken@1900
   220
        break;
slouken@1900
   221
    default:
slouken@1900
   222
        error = "UNKNOWN";
slouken@1900
   223
        break;
slouken@1900
   224
    }
slouken@1900
   225
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   226
}
slouken@1900
   227
slouken@1903
   228
static D3DFORMAT
slouken@1903
   229
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   230
{
slouken@1903
   231
    switch (format) {
slouken@1965
   232
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   233
        return D3DFMT_R5G6B5;
slouken@1965
   234
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   235
        return D3DFMT_X8R8G8B8;
slouken@1965
   236
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   237
        return D3DFMT_A8R8G8B8;
slouken@1903
   238
    default:
slouken@1903
   239
        return D3DFMT_UNKNOWN;
slouken@1903
   240
    }
slouken@1895
   241
}
slouken@1895
   242
slouken@5156
   243
static Uint32
slouken@5156
   244
D3DFMTToPixelFormat(D3DFORMAT format)
slouken@2973
   245
{
slouken@5156
   246
    switch (format) {
slouken@5156
   247
    case D3DFMT_R5G6B5:
slouken@5156
   248
        return SDL_PIXELFORMAT_RGB565;
slouken@5156
   249
    case D3DFMT_X8R8G8B8:
slouken@5156
   250
        return SDL_PIXELFORMAT_RGB888;
slouken@5156
   251
    case D3DFMT_A8R8G8B8:
slouken@5156
   252
        return SDL_PIXELFORMAT_ARGB8888;
slouken@5156
   253
    default:
slouken@5156
   254
        return SDL_PIXELFORMAT_UNKNOWN;
slouken@2973
   255
    }
slouken@1895
   256
}
slouken@1895
   257
slouken@1895
   258
SDL_Renderer *
slouken@1913
   259
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   260
{
slouken@1895
   261
    SDL_Renderer *renderer;
slouken@1913
   262
    D3D_RenderData *data;
slouken@5154
   263
    SDL_SysWMinfo windowinfo;
slouken@1900
   264
    HRESULT result;
slouken@1900
   265
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   266
    IDirect3DSwapChain9 *chain;
slouken@1925
   267
    D3DCAPS9 caps;
slouken@5156
   268
    Uint32 window_flags;
slouken@5156
   269
    int w, h;
slouken@5156
   270
    SDL_DisplayMode fullscreen_mode;
slouken@1895
   271
slouken@1920
   272
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   273
    if (!renderer) {
slouken@1895
   274
        SDL_OutOfMemory();
slouken@1895
   275
        return NULL;
slouken@1895
   276
    }
slouken@1895
   277
slouken@1920
   278
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   279
    if (!data) {
slouken@5154
   280
        SDL_free(renderer);
slouken@1895
   281
        SDL_OutOfMemory();
slouken@1895
   282
        return NULL;
slouken@1895
   283
    }
slouken@1895
   284
slouken@5154
   285
    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
slouken@5154
   286
    if (data->d3dDLL) {
slouken@5154
   287
        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@4569
   288
slouken@5154
   289
        D3DCreate =
slouken@5154
   290
            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
slouken@5154
   291
                                                            "Direct3DCreate9");
slouken@5154
   292
        if (D3DCreate) {
slouken@5154
   293
            data->d3d = D3DCreate(D3D_SDK_VERSION);
slouken@5154
   294
        }
slouken@5154
   295
        if (!data->d3d) {
slouken@5154
   296
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
   297
            data->d3dDLL = NULL;
slouken@5154
   298
        }
slouken@5154
   299
    }
slouken@5154
   300
    if (!data->d3d) {
slouken@5154
   301
        SDL_free(renderer);
slouken@5154
   302
        SDL_free(data);
slouken@5154
   303
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   304
        return NULL;
slouken@5154
   305
    }
slouken@5154
   306
slouken@1913
   307
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   308
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   309
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   310
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@3599
   311
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   312
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   313
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   314
    renderer->RenderCopy = D3D_RenderCopy;
slouken@3427
   315
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@1913
   316
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   317
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   318
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   319
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   320
    renderer->driverdata = data;
slouken@1895
   321
slouken@1965
   322
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   323
slouken@5154
   324
    SDL_VERSION(&windowinfo.version);
slouken@5154
   325
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   326
slouken@5156
   327
    window_flags = SDL_GetWindowFlags(window);
slouken@5156
   328
    SDL_GetWindowSize(window, &w, &h);
slouken@5156
   329
    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
slouken@5156
   330
slouken@1900
   331
    SDL_zero(pparams);
slouken@5154
   332
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@5156
   333
    pparams.BackBufferWidth = w;
slouken@5156
   334
    pparams.BackBufferHeight = h;
slouken@5156
   335
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   336
        pparams.BackBufferFormat =
slouken@5156
   337
            PixelFormatToD3DFMT(fullscreen_mode.format);
slouken@1903
   338
    } else {
slouken@1903
   339
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   340
    }
slouken@5142
   341
    pparams.BackBufferCount = 1;
slouken@5142
   342
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   343
slouken@5156
   344
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   345
        pparams.Windowed = FALSE;
slouken@1903
   346
        pparams.FullScreen_RefreshRateInHz =
slouken@5156
   347
            fullscreen_mode.refresh_rate;
slouken@1900
   348
    } else {
slouken@1900
   349
        pparams.Windowed = TRUE;
slouken@1903
   350
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   351
    }
slouken@1965
   352
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   353
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   354
    } else {
slouken@1907
   355
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   356
    }
slouken@1900
   357
slouken@5154
   358
    /* FIXME: Which adapter? */
slouken@5154
   359
    data->adapter = D3DADAPTER_DEFAULT;
slouken@5154
   360
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   361
slouken@5154
   362
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   363
                                     D3DDEVTYPE_HAL,
slouken@5154
   364
                                     pparams.hDeviceWindow,
slouken@3197
   365
                                     (caps.
slouken@3197
   366
                                      DevCaps &
slouken@3197
   367
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   368
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@1900
   369
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   370
                                     &pparams, &data->device);
slouken@1900
   371
    if (FAILED(result)) {
slouken@1913
   372
        D3D_DestroyRenderer(renderer);
slouken@1900
   373
        D3D_SetError("CreateDevice()", result);
slouken@1900
   374
        return NULL;
slouken@1900
   375
    }
slouken@1900
   376
    data->beginScene = SDL_TRUE;
slouken@1900
   377
slouken@1907
   378
    /* Get presentation parameters to fill info */
slouken@1907
   379
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   380
    if (FAILED(result)) {
slouken@1913
   381
        D3D_DestroyRenderer(renderer);
slouken@1907
   382
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   383
        return NULL;
slouken@1907
   384
    }
slouken@1907
   385
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   386
    if (FAILED(result)) {
slouken@1907
   387
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   388
        D3D_DestroyRenderer(renderer);
slouken@1907
   389
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   390
        return NULL;
slouken@1907
   391
    }
slouken@1907
   392
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   393
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   394
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   395
    }
slouken@1975
   396
    data->pparams = pparams;
slouken@1907
   397
slouken@1925
   398
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   399
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   400
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@1918
   401
slouken@1903
   402
    /* Set up parameters for rendering */
slouken@1904
   403
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   404
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   405
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   406
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   407
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   408
                                    D3DCULL_NONE);
slouken@1904
   409
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   410
    /* Enable color modulation by diffuse color */
slouken@1988
   411
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   412
                                          D3DTOP_MODULATE);
slouken@1988
   413
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   414
                                          D3DTA_TEXTURE);
slouken@1988
   415
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   416
                                          D3DTA_DIFFUSE);
slouken@1988
   417
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   418
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   419
                                          D3DTOP_MODULATE);
slouken@1988
   420
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   421
                                          D3DTA_TEXTURE);
slouken@1988
   422
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   423
                                          D3DTA_DIFFUSE);
slouken@1991
   424
    /* Disable second texture stage, since we're done */
slouken@1991
   425
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   426
                                          D3DTOP_DISABLE);
slouken@1991
   427
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   428
                                          D3DTOP_DISABLE);
slouken@1903
   429
slouken@1895
   430
    return renderer;
slouken@1895
   431
}
slouken@1895
   432
slouken@1895
   433
static int
slouken@1975
   434
D3D_Reset(SDL_Renderer * renderer)
slouken@1975
   435
{
slouken@1975
   436
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   437
    HRESULT result;
slouken@1975
   438
slouken@1975
   439
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@1975
   440
    if (FAILED(result)) {
slouken@1975
   441
        if (result == D3DERR_DEVICELOST) {
slouken@1975
   442
            /* Don't worry about it, we'll reset later... */
slouken@1975
   443
            return 0;
slouken@1975
   444
        } else {
slouken@1975
   445
            D3D_SetError("Reset()", result);
slouken@1975
   446
            return -1;
slouken@1975
   447
        }
slouken@1975
   448
    }
slouken@1975
   449
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   450
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   451
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1975
   452
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1975
   453
                                    D3DCULL_NONE);
slouken@1975
   454
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1975
   455
    return 0;
slouken@1975
   456
}
slouken@1975
   457
slouken@5154
   458
/* FIXME: This needs to be called... when? */
slouken@5154
   459
#if 0
slouken@1975
   460
static int
slouken@1975
   461
D3D_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1975
   462
{
slouken@1975
   463
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3685
   464
    SDL_Window *window = renderer->window;
slouken@3685
   465
    SDL_VideoDisplay *display = window->display;
slouken@1975
   466
slouken@1975
   467
    data->pparams.BackBufferWidth = window->w;
slouken@1975
   468
    data->pparams.BackBufferHeight = window->h;
slouken@1975
   469
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1975
   470
        data->pparams.BackBufferFormat =
slouken@3519
   471
            PixelFormatToD3DFMT(window->fullscreen_mode.format);
slouken@1975
   472
    } else {
slouken@1975
   473
        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1975
   474
    }
slouken@1975
   475
    return D3D_Reset(renderer);
slouken@1975
   476
}
slouken@5154
   477
#endif
slouken@1975
   478
slouken@1975
   479
static int
slouken@1913
   480
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   481
{
slouken@1913
   482
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@3685
   483
    SDL_Window *window = renderer->window;
slouken@5154
   484
    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
slouken@1913
   485
    D3D_TextureData *data;
slouken@5173
   486
    D3DPOOL pool;
slouken@5173
   487
    DWORD usage;
slouken@1903
   488
    HRESULT result;
slouken@1895
   489
slouken@1920
   490
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   491
    if (!data) {
slouken@1895
   492
        SDL_OutOfMemory();
slouken@1895
   493
        return -1;
slouken@1895
   494
    }
slouken@1895
   495
slouken@1895
   496
    texture->driverdata = data;
slouken@1895
   497
slouken@5173
   498
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   499
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   500
        pool = D3DPOOL_DEFAULT;
slouken@5173
   501
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   502
    } else
slouken@5173
   503
#endif
slouken@5173
   504
    {
slouken@5173
   505
        pool = D3DPOOL_MANAGED;
slouken@5173
   506
        usage = 0;
slouken@5173
   507
    }
slouken@2973
   508
slouken@1903
   509
    result =
slouken@1903
   510
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   511
                                       texture->h, 1, usage,
slouken@5173
   512
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   513
                                       pool, &data->texture, NULL);
slouken@1903
   514
    if (FAILED(result)) {
slouken@1903
   515
        D3D_SetError("CreateTexture()", result);
slouken@1903
   516
        return -1;
slouken@1903
   517
    }
slouken@1903
   518
slouken@1895
   519
    return 0;
slouken@1895
   520
}
slouken@1895
   521
slouken@1895
   522
static int
slouken@1913
   523
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   524
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   525
{
slouken@1913
   526
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   527
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@5156
   528
    RECT d3drect;
slouken@5156
   529
    D3DLOCKED_RECT locked;
slouken@5156
   530
    const Uint8 *src;
slouken@5156
   531
    Uint8 *dst;
slouken@5156
   532
    int row, length;
slouken@5156
   533
    HRESULT result;
slouken@2973
   534
slouken@5173
   535
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   536
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
   537
        rect->x == 0 && rect->y == 0 &&
slouken@5173
   538
        rect->w == texture->w && rect->h == texture->h) {
slouken@5173
   539
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@5173
   540
    } else
slouken@5173
   541
#endif
slouken@5173
   542
    {
slouken@5173
   543
        d3drect.left = rect->x;
slouken@5173
   544
        d3drect.right = rect->x + rect->w;
slouken@5173
   545
        d3drect.top = rect->y;
slouken@5173
   546
        d3drect.bottom = rect->y + rect->h;
slouken@5173
   547
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   548
    }
slouken@2973
   549
slouken@5156
   550
    if (FAILED(result)) {
slouken@5156
   551
        D3D_SetError("LockRect()", result);
slouken@5156
   552
        return -1;
slouken@5156
   553
    }
slouken@2973
   554
slouken@5156
   555
    src = pixels;
slouken@5156
   556
    dst = locked.pBits;
slouken@5156
   557
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5173
   558
    if (length == pitch && length == locked.Pitch) {
slouken@5173
   559
        SDL_memcpy(dst, src, length*rect->h);
slouken@5173
   560
    } else {
slouken@5173
   561
        for (row = 0; row < rect->h; ++row) {
slouken@5173
   562
            SDL_memcpy(dst, src, length);
slouken@5173
   563
            src += pitch;
slouken@5173
   564
            dst += locked.Pitch;
slouken@5173
   565
        }
slouken@5156
   566
    }
slouken@5156
   567
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   568
slouken@5156
   569
    return 0;
slouken@1895
   570
}
slouken@1895
   571
slouken@1895
   572
static int
slouken@1913
   573
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   574
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   575
{
slouken@1913
   576
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   577
    RECT d3drect;
slouken@5156
   578
    D3DLOCKED_RECT locked;
slouken@5156
   579
    HRESULT result;
slouken@1895
   580
slouken@5156
   581
    d3drect.left = rect->x;
slouken@5156
   582
    d3drect.right = rect->x + rect->w;
slouken@5156
   583
    d3drect.top = rect->y;
slouken@5156
   584
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   585
slouken@5156
   586
    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   587
    if (FAILED(result)) {
slouken@5156
   588
        D3D_SetError("LockRect()", result);
slouken@5156
   589
        return -1;
slouken@1903
   590
    }
slouken@5156
   591
    *pixels = locked.pBits;
slouken@5156
   592
    *pitch = locked.Pitch;
slouken@5156
   593
    return 0;
slouken@1895
   594
}
slouken@1895
   595
slouken@1895
   596
static void
slouken@1913
   597
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   598
{
slouken@1913
   599
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   600
slouken@5156
   601
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   602
}
slouken@1895
   603
slouken@2932
   604
static void
slouken@2933
   605
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   606
{
slouken@2932
   607
    switch (blendMode) {
slouken@2932
   608
    case SDL_BLENDMODE_NONE:
slouken@2932
   609
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   610
                                        FALSE);
slouken@2932
   611
        break;
slouken@2932
   612
    case SDL_BLENDMODE_BLEND:
slouken@2932
   613
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   614
                                        TRUE);
slouken@2932
   615
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   616
                                        D3DBLEND_SRCALPHA);
slouken@2932
   617
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   618
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   619
        break;
slouken@2932
   620
    case SDL_BLENDMODE_ADD:
slouken@2932
   621
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   622
                                        TRUE);
slouken@2932
   623
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   624
                                        D3DBLEND_SRCALPHA);
slouken@2932
   625
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   626
                                        D3DBLEND_ONE);
slouken@2932
   627
        break;
slouken@2932
   628
    }
slouken@2932
   629
}
slouken@2932
   630
slouken@1895
   631
static int
slouken@3599
   632
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   633
                     int count)
slouken@1895
   634
{
slouken@1913
   635
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   636
    DWORD color;
slouken@3536
   637
    Vertex *vertices;
slouken@3536
   638
    int i;
slouken@1900
   639
    HRESULT result;
slouken@1895
   640
slouken@1900
   641
    if (data->beginScene) {
slouken@1900
   642
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   643
        data->beginScene = SDL_FALSE;
slouken@1900
   644
    }
slouken@1895
   645
slouken@2932
   646
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   647
slouken@2933
   648
    result =
slouken@2933
   649
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   650
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   651
    if (FAILED(result)) {
slouken@2932
   652
        D3D_SetError("SetTexture()", result);
slouken@2932
   653
        return -1;
slouken@2932
   654
    }
slouken@3536
   655
slouken@3536
   656
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   657
slouken@3536
   658
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   659
    for (i = 0; i < count; ++i) {
slouken@3536
   660
        vertices[i].x = (float) points[i].x;
slouken@3536
   661
        vertices[i].y = (float) points[i].y;
slouken@3536
   662
        vertices[i].z = 0.0f;
slouken@3536
   663
        vertices[i].rhw = 1.0f;
slouken@3536
   664
        vertices[i].color = color;
slouken@3536
   665
        vertices[i].u = 0.0f;
slouken@3536
   666
        vertices[i].v = 0.0f;
slouken@3536
   667
    }
slouken@1903
   668
    result =
slouken@3536
   669
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
   670
                                         vertices, sizeof(*vertices));
slouken@3536
   671
    SDL_stack_free(vertices);
slouken@1900
   672
    if (FAILED(result)) {
slouken@2932
   673
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   674
        return -1;
slouken@2932
   675
    }
slouken@2932
   676
    return 0;
slouken@2932
   677
}
slouken@2932
   678
slouken@2932
   679
static int
slouken@3599
   680
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   681
                    int count)
slouken@2932
   682
{
slouken@2932
   683
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   684
    DWORD color;
slouken@3536
   685
    Vertex *vertices;
slouken@3536
   686
    int i;
slouken@2932
   687
    HRESULT result;
slouken@2932
   688
slouken@2932
   689
    if (data->beginScene) {
slouken@2932
   690
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   691
        data->beginScene = SDL_FALSE;
slouken@2932
   692
    }
slouken@2932
   693
slouken@2932
   694
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   695
slouken@2933
   696
    result =
slouken@2933
   697
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   698
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   699
    if (FAILED(result)) {
slouken@2932
   700
        D3D_SetError("SetTexture()", result);
slouken@2932
   701
        return -1;
slouken@2932
   702
    }
slouken@3536
   703
slouken@3536
   704
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   705
slouken@3536
   706
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   707
    for (i = 0; i < count; ++i) {
slouken@3536
   708
        vertices[i].x = (float) points[i].x;
slouken@3536
   709
        vertices[i].y = (float) points[i].y;
slouken@3536
   710
        vertices[i].z = 0.0f;
slouken@3536
   711
        vertices[i].rhw = 1.0f;
slouken@3536
   712
        vertices[i].color = color;
slouken@3536
   713
        vertices[i].u = 0.0f;
slouken@3536
   714
        vertices[i].v = 0.0f;
slouken@3536
   715
    }
slouken@2932
   716
    result =
slouken@3551
   717
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
   718
                                         vertices, sizeof(*vertices));
slouken@3551
   719
slouken@3551
   720
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
   721
       so we need to close the endpoint of the line */
slouken@3551
   722
    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
slouken@3551
   723
        vertices[0].x = (float) points[count-1].x;
slouken@3551
   724
        vertices[0].y = (float) points[count-1].y;
slouken@3551
   725
        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
slouken@3551
   726
    }
slouken@3551
   727
slouken@3536
   728
    SDL_stack_free(vertices);
slouken@2932
   729
    if (FAILED(result)) {
slouken@2932
   730
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   731
        return -1;
slouken@2932
   732
    }
slouken@2932
   733
    return 0;
slouken@2932
   734
}
slouken@2932
   735
slouken@2932
   736
static int
slouken@3599
   737
D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
slouken@3599
   738
                    int count)
slouken@2932
   739
{
slouken@2932
   740
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3536
   741
    DWORD color;
slouken@3536
   742
    int i;
slouken@2932
   743
    float minx, miny, maxx, maxy;
slouken@2932
   744
    Vertex vertices[4];
slouken@2932
   745
    HRESULT result;
slouken@2932
   746
slouken@2932
   747
    if (data->beginScene) {
slouken@2932
   748
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   749
        data->beginScene = SDL_FALSE;
slouken@2932
   750
    }
slouken@2932
   751
slouken@2932
   752
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   753
slouken@2933
   754
    result =
slouken@2933
   755
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   756
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   757
    if (FAILED(result)) {
slouken@2932
   758
        D3D_SetError("SetTexture()", result);
slouken@2932
   759
        return -1;
slouken@2932
   760
    }
slouken@3536
   761
slouken@3536
   762
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   763
slouken@3536
   764
    for (i = 0; i < count; ++i) {
slouken@3536
   765
        const SDL_Rect *rect = rects[i];
slouken@3536
   766
slouken@3536
   767
        minx = (float) rect->x;
slouken@3536
   768
        miny = (float) rect->y;
slouken@3536
   769
        maxx = (float) rect->x + rect->w;
slouken@3536
   770
        maxy = (float) rect->y + rect->h;
slouken@3536
   771
slouken@3536
   772
        vertices[0].x = minx;
slouken@3536
   773
        vertices[0].y = miny;
slouken@3536
   774
        vertices[0].z = 0.0f;
slouken@3536
   775
        vertices[0].rhw = 1.0f;
slouken@3536
   776
        vertices[0].color = color;
slouken@3536
   777
        vertices[0].u = 0.0f;
slouken@3536
   778
        vertices[0].v = 0.0f;
slouken@3536
   779
slouken@3536
   780
        vertices[1].x = maxx;
slouken@3536
   781
        vertices[1].y = miny;
slouken@3536
   782
        vertices[1].z = 0.0f;
slouken@3536
   783
        vertices[1].rhw = 1.0f;
slouken@3536
   784
        vertices[1].color = color;
slouken@3536
   785
        vertices[1].u = 0.0f;
slouken@3536
   786
        vertices[1].v = 0.0f;
slouken@3536
   787
slouken@3536
   788
        vertices[2].x = maxx;
slouken@3536
   789
        vertices[2].y = maxy;
slouken@3536
   790
        vertices[2].z = 0.0f;
slouken@3536
   791
        vertices[2].rhw = 1.0f;
slouken@3536
   792
        vertices[2].color = color;
slouken@3536
   793
        vertices[2].u = 0.0f;
slouken@3536
   794
        vertices[2].v = 0.0f;
slouken@3536
   795
slouken@3536
   796
        vertices[3].x = minx;
slouken@3536
   797
        vertices[3].y = maxy;
slouken@3536
   798
        vertices[3].z = 0.0f;
slouken@3536
   799
        vertices[3].rhw = 1.0f;
slouken@3536
   800
        vertices[3].color = color;
slouken@3536
   801
        vertices[3].u = 0.0f;
slouken@3536
   802
        vertices[3].v = 0.0f;
slouken@3536
   803
slouken@3536
   804
        result =
slouken@3536
   805
            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
slouken@3536
   806
                                             2, vertices, sizeof(*vertices));
slouken@3536
   807
        if (FAILED(result)) {
slouken@3536
   808
            D3D_SetError("DrawPrimitiveUP()", result);
slouken@3536
   809
            return -1;
slouken@3536
   810
        }
slouken@1900
   811
    }
slouken@1895
   812
    return 0;
slouken@1895
   813
}
slouken@1895
   814
slouken@1895
   815
static int
slouken@1913
   816
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
   817
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
   818
{
slouken@1913
   819
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   820
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@3556
   821
    LPDIRECT3DPIXELSHADER9 shader = NULL;
slouken@1903
   822
    float minx, miny, maxx, maxy;
slouken@1904
   823
    float minu, maxu, minv, maxv;
slouken@1987
   824
    DWORD color;
slouken@1903
   825
    Vertex vertices[4];
slouken@1903
   826
    HRESULT result;
slouken@1895
   827
slouken@1900
   828
    if (data->beginScene) {
slouken@1900
   829
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   830
        data->beginScene = SDL_FALSE;
slouken@1900
   831
    }
slouken@1903
   832
slouken@3279
   833
    minx = (float) dstrect->x - 0.5f;
slouken@3279
   834
    miny = (float) dstrect->y - 0.5f;
slouken@3279
   835
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@3279
   836
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
   837
slouken@1904
   838
    minu = (float) srcrect->x / texture->w;
slouken@1904
   839
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
   840
    minv = (float) srcrect->y / texture->h;
slouken@1904
   841
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
   842
slouken@1987
   843
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
   844
slouken@1903
   845
    vertices[0].x = minx;
slouken@1903
   846
    vertices[0].y = miny;
slouken@1903
   847
    vertices[0].z = 0.0f;
slouken@1904
   848
    vertices[0].rhw = 1.0f;
slouken@1987
   849
    vertices[0].color = color;
slouken@1904
   850
    vertices[0].u = minu;
slouken@1904
   851
    vertices[0].v = minv;
slouken@1904
   852
slouken@1903
   853
    vertices[1].x = maxx;
slouken@1903
   854
    vertices[1].y = miny;
slouken@1903
   855
    vertices[1].z = 0.0f;
slouken@1904
   856
    vertices[1].rhw = 1.0f;
slouken@1987
   857
    vertices[1].color = color;
slouken@1904
   858
    vertices[1].u = maxu;
slouken@1904
   859
    vertices[1].v = minv;
slouken@1904
   860
slouken@1903
   861
    vertices[2].x = maxx;
slouken@1903
   862
    vertices[2].y = maxy;
slouken@1903
   863
    vertices[2].z = 0.0f;
slouken@1904
   864
    vertices[2].rhw = 1.0f;
slouken@1987
   865
    vertices[2].color = color;
slouken@1904
   866
    vertices[2].u = maxu;
slouken@1904
   867
    vertices[2].v = maxv;
slouken@1904
   868
slouken@1903
   869
    vertices[3].x = minx;
slouken@1903
   870
    vertices[3].y = maxy;
slouken@1903
   871
    vertices[3].z = 0.0f;
slouken@1904
   872
    vertices[3].rhw = 1.0f;
slouken@1987
   873
    vertices[3].color = color;
slouken@1904
   874
    vertices[3].u = minu;
slouken@1904
   875
    vertices[3].v = maxv;
slouken@1903
   876
slouken@2932
   877
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
   878
slouken@5138
   879
    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@5138
   880
                                     D3DTEXF_LINEAR);
slouken@5138
   881
    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@5138
   882
                                     D3DTEXF_LINEAR);
slouken@1917
   883
slouken@1903
   884
    result =
slouken@2735
   885
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
   886
                                    texturedata->texture);
slouken@1903
   887
    if (FAILED(result)) {
slouken@1903
   888
        D3D_SetError("SetTexture()", result);
slouken@1903
   889
        return -1;
slouken@1903
   890
    }
slouken@3556
   891
    if (shader) {
slouken@3556
   892
        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
slouken@3556
   893
        if (FAILED(result)) {
slouken@3556
   894
            D3D_SetError("SetShader()", result);
slouken@3556
   895
            return -1;
slouken@3556
   896
        }
slouken@3556
   897
    }
slouken@1903
   898
    result =
slouken@1903
   899
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
   900
                                         vertices, sizeof(*vertices));
slouken@1903
   901
    if (FAILED(result)) {
slouken@1903
   902
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
   903
        return -1;
slouken@1903
   904
    }
slouken@3556
   905
    if (shader) {
slouken@3556
   906
        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
slouken@3556
   907
        if (FAILED(result)) {
slouken@3556
   908
            D3D_SetError("SetShader()", result);
slouken@3556
   909
            return -1;
slouken@3556
   910
        }
slouken@3556
   911
    }
slouken@1895
   912
    return 0;
slouken@1895
   913
}
slouken@1895
   914
slouken@3427
   915
static int
slouken@3427
   916
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3480
   917
                     Uint32 format, void * pixels, int pitch)
slouken@3427
   918
{
slouken@3549
   919
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@3549
   920
    D3DSURFACE_DESC desc;
slouken@3549
   921
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3549
   922
    LPDIRECT3DSURFACE9 surface;
slouken@3549
   923
    RECT d3drect;
slouken@3549
   924
    D3DLOCKED_RECT locked;
slouken@3549
   925
    HRESULT result;
slouken@3427
   926
slouken@3549
   927
    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3549
   928
    if (FAILED(result)) {
slouken@3549
   929
        D3D_SetError("GetBackBuffer()", result);
slouken@3549
   930
        return -1;
slouken@3549
   931
    }
slouken@3427
   932
slouken@3549
   933
    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3549
   934
    if (FAILED(result)) {
slouken@3549
   935
        D3D_SetError("GetDesc()", result);
slouken@3549
   936
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
   937
        return -1;
slouken@3549
   938
    }
slouken@3427
   939
slouken@3549
   940
    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
slouken@3549
   941
    if (FAILED(result)) {
slouken@3549
   942
        D3D_SetError("CreateOffscreenPlainSurface()", result);
slouken@3549
   943
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
   944
        return -1;
slouken@3549
   945
    }
slouken@3427
   946
slouken@3549
   947
    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
slouken@3549
   948
    if (FAILED(result)) {
slouken@3549
   949
        D3D_SetError("GetRenderTargetData()", result);
slouken@3549
   950
        IDirect3DSurface9_Release(surface);
slouken@3549
   951
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
   952
        return -1;
slouken@3549
   953
    }
slouken@3427
   954
slouken@3549
   955
    d3drect.left = rect->x;
slouken@3549
   956
    d3drect.right = rect->x + rect->w;
slouken@3549
   957
    d3drect.top = rect->y;
slouken@3549
   958
    d3drect.bottom = rect->y + rect->h;
slouken@3427
   959
slouken@3549
   960
    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
slouken@3549
   961
    if (FAILED(result)) {
slouken@3549
   962
        D3D_SetError("LockRect()", result);
slouken@3549
   963
        IDirect3DSurface9_Release(surface);
slouken@3549
   964
        IDirect3DSurface9_Release(backBuffer);
slouken@3549
   965
        return -1;
slouken@3549
   966
    }
slouken@3427
   967
slouken@3549
   968
    SDL_ConvertPixels(rect->w, rect->h,
slouken@5156
   969
                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
slouken@3549
   970
                      format, pixels, pitch);
slouken@3549
   971
slouken@3549
   972
    IDirect3DSurface9_UnlockRect(surface);
slouken@3549
   973
slouken@3549
   974
    IDirect3DSurface9_Release(surface);
slouken@3549
   975
    IDirect3DSurface9_Release(backBuffer);
slouken@3549
   976
slouken@3549
   977
    return 0;
slouken@3427
   978
}
slouken@3427
   979
slouken@1895
   980
static void
slouken@1913
   981
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   982
{
slouken@1913
   983
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
   984
    HRESULT result;
slouken@1900
   985
slouken@1900
   986
    if (!data->beginScene) {
slouken@1900
   987
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
   988
        data->beginScene = SDL_TRUE;
slouken@1900
   989
    }
slouken@1900
   990
slouken@1975
   991
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
   992
    if (result == D3DERR_DEVICELOST) {
slouken@1975
   993
        /* We'll reset later */
slouken@1975
   994
        return;
slouken@1975
   995
    }
slouken@1975
   996
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
   997
        D3D_Reset(renderer);
slouken@1975
   998
    }
slouken@1900
   999
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1000
    if (FAILED(result)) {
slouken@1900
  1001
        D3D_SetError("Present()", result);
slouken@1900
  1002
    }
slouken@1895
  1003
}
slouken@1895
  1004
slouken@1895
  1005
static void
slouken@1913
  1006
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1007
{
slouken@1913
  1008
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1009
slouken@1895
  1010
    if (!data) {
slouken@1895
  1011
        return;
slouken@1895
  1012
    }
slouken@1903
  1013
    if (data->texture) {
slouken@1903
  1014
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1015
    }
slouken@1895
  1016
    SDL_free(data);
slouken@1895
  1017
    texture->driverdata = NULL;
slouken@1895
  1018
}
slouken@1895
  1019
slouken@1975
  1020
static void
slouken@1913
  1021
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1022
{
slouken@1913
  1023
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1024
slouken@1895
  1025
    if (data) {
slouken@1900
  1026
        if (data->device) {
slouken@1900
  1027
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1028
        }
slouken@5154
  1029
        if (data->d3d) {
slouken@5154
  1030
            IDirect3D9_Release(data->d3d);
slouken@5154
  1031
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
  1032
        }
slouken@1895
  1033
        SDL_free(data);
slouken@1895
  1034
    }
slouken@1895
  1035
    SDL_free(renderer);
slouken@1895
  1036
}
slouken@1895
  1037
slouken@1895
  1038
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
  1039
slouken@1895
  1040
/* vi: set ts=4 sw=4 expandtab: */