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