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