src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5262 b530ef003506
child 5299 33987021a1ed
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

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