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