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