src/render/direct3d/SDL_render_d3d.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 10 Nov 2011 00:22:44 -0500
changeset 6076 e69b1bd26e0b
parent 5535 96594ac5fd1a
child 6115 d4621a7e0faa
permissions -rw-r--r--
Fixed issues closing lines with the OpenGL ES renderer.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@1895
    21
#include "SDL_config.h"
slouken@1895
    22
slouken@5226
    23
#if SDL_VIDEO_RENDER_D3D && !SDL_RENDER_DISABLED
slouken@5226
    24
slouken@1895
    25
slouken@5154
    26
#include "../../core/windows/SDL_windows.h"
slouken@5154
    27
slouken@5484
    28
#include "SDL_hints.h"
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@5484
   140
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@1913
   141
} D3D_RenderData;
slouken@1895
   142
slouken@1895
   143
typedef struct
slouken@1895
   144
{
slouken@1903
   145
    IDirect3DTexture9 *texture;
slouken@5484
   146
    D3DTEXTUREFILTERTYPE scaleMode;
slouken@1913
   147
} D3D_TextureData;
slouken@1895
   148
slouken@1903
   149
typedef struct
slouken@1903
   150
{
slouken@1903
   151
    float x, y, z;
slouken@1987
   152
    DWORD color;
slouken@1904
   153
    float u, v;
slouken@1903
   154
} Vertex;
slouken@1903
   155
slouken@1895
   156
static void
slouken@1900
   157
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   158
{
slouken@1900
   159
    const char *error;
slouken@1900
   160
slouken@1900
   161
    switch (result) {
slouken@1900
   162
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   163
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   164
        break;
slouken@1900
   165
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   166
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   167
        break;
slouken@1900
   168
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   169
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   170
        break;
slouken@1900
   171
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   172
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   173
        break;
slouken@1900
   174
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   175
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   176
        break;
slouken@1900
   177
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   178
        error = "TOOMANYOPERATIONS";
slouken@1900
   179
        break;
slouken@1900
   180
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   181
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   182
        break;
slouken@1900
   183
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   184
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   185
        break;
slouken@1900
   186
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   187
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   188
        break;
slouken@1900
   189
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   190
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   191
        break;
slouken@1900
   192
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   193
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   194
        break;
slouken@1900
   195
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   196
        error = "DRIVERINTERNALERROR";
slouken@1900
   197
        break;
slouken@1900
   198
    case D3DERR_NOTFOUND:
slouken@1900
   199
        error = "NOTFOUND";
slouken@1900
   200
        break;
slouken@1900
   201
    case D3DERR_MOREDATA:
slouken@1900
   202
        error = "MOREDATA";
slouken@1900
   203
        break;
slouken@1900
   204
    case D3DERR_DEVICELOST:
slouken@1900
   205
        error = "DEVICELOST";
slouken@1900
   206
        break;
slouken@1900
   207
    case D3DERR_DEVICENOTRESET:
slouken@1900
   208
        error = "DEVICENOTRESET";
slouken@1900
   209
        break;
slouken@1900
   210
    case D3DERR_NOTAVAILABLE:
slouken@1900
   211
        error = "NOTAVAILABLE";
slouken@1900
   212
        break;
slouken@1900
   213
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   214
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   215
        break;
slouken@1900
   216
    case D3DERR_INVALIDDEVICE:
slouken@1900
   217
        error = "INVALIDDEVICE";
slouken@1900
   218
        break;
slouken@1900
   219
    case D3DERR_INVALIDCALL:
slouken@1900
   220
        error = "INVALIDCALL";
slouken@1900
   221
        break;
slouken@1900
   222
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   223
        error = "DRIVERINVALIDCALL";
slouken@1900
   224
        break;
slouken@1900
   225
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   226
        error = "WASSTILLDRAWING";
slouken@1900
   227
        break;
slouken@1900
   228
    default:
slouken@1900
   229
        error = "UNKNOWN";
slouken@1900
   230
        break;
slouken@1900
   231
    }
slouken@1900
   232
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   233
}
slouken@1900
   234
slouken@1903
   235
static D3DFORMAT
slouken@1903
   236
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   237
{
slouken@1903
   238
    switch (format) {
slouken@1965
   239
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   240
        return D3DFMT_R5G6B5;
slouken@1965
   241
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   242
        return D3DFMT_X8R8G8B8;
slouken@1965
   243
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   244
        return D3DFMT_A8R8G8B8;
slouken@1903
   245
    default:
slouken@1903
   246
        return D3DFMT_UNKNOWN;
slouken@1903
   247
    }
slouken@1895
   248
}
slouken@1895
   249
slouken@5156
   250
static Uint32
slouken@5156
   251
D3DFMTToPixelFormat(D3DFORMAT format)
slouken@2973
   252
{
slouken@5156
   253
    switch (format) {
slouken@5156
   254
    case D3DFMT_R5G6B5:
slouken@5156
   255
        return SDL_PIXELFORMAT_RGB565;
slouken@5156
   256
    case D3DFMT_X8R8G8B8:
slouken@5156
   257
        return SDL_PIXELFORMAT_RGB888;
slouken@5156
   258
    case D3DFMT_A8R8G8B8:
slouken@5156
   259
        return SDL_PIXELFORMAT_ARGB8888;
slouken@5156
   260
    default:
slouken@5156
   261
        return SDL_PIXELFORMAT_UNKNOWN;
slouken@2973
   262
    }
slouken@1895
   263
}
slouken@1895
   264
slouken@5297
   265
static int
slouken@5297
   266
D3D_Reset(SDL_Renderer * renderer)
slouken@5297
   267
{
slouken@5297
   268
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   269
    HRESULT result;
slouken@5297
   270
slouken@5297
   271
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@5297
   272
    if (FAILED(result)) {
slouken@5297
   273
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   274
            /* Don't worry about it, we'll reset later... */
slouken@5297
   275
            return 0;
slouken@5297
   276
        } else {
slouken@5297
   277
            D3D_SetError("Reset()", result);
slouken@5297
   278
            return -1;
slouken@5297
   279
        }
slouken@5297
   280
    }
slouken@5297
   281
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@5297
   282
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   283
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@5297
   284
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@5297
   285
                                    D3DCULL_NONE);
slouken@5297
   286
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@5297
   287
    return 0;
slouken@5297
   288
}
slouken@5297
   289
slouken@5297
   290
static int
slouken@5297
   291
D3D_ActivateRenderer(SDL_Renderer * renderer)
slouken@5297
   292
{
slouken@5297
   293
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   294
    HRESULT result;
slouken@5297
   295
slouken@5297
   296
    if (data->updateSize) {
slouken@5297
   297
        SDL_Window *window = renderer->window;
slouken@5297
   298
        int w, h;
slouken@5297
   299
slouken@5297
   300
        SDL_GetWindowSize(window, &w, &h);
slouken@5297
   301
        data->pparams.BackBufferWidth = w;
slouken@5297
   302
        data->pparams.BackBufferHeight = h;
slouken@5297
   303
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@5297
   304
            data->pparams.BackBufferFormat =
slouken@5297
   305
                PixelFormatToD3DFMT(SDL_GetWindowPixelFormat(window));
slouken@5297
   306
        } else {
slouken@5297
   307
            data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@5297
   308
        }
slouken@5297
   309
        if (D3D_Reset(renderer) < 0) {
slouken@5297
   310
            return -1;
slouken@5297
   311
        }
slouken@5297
   312
        D3D_UpdateViewport(renderer);
slouken@5297
   313
slouken@5297
   314
        data->updateSize = SDL_FALSE;
slouken@5297
   315
    }
slouken@5297
   316
    if (data->beginScene) {
slouken@5297
   317
        result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   318
        if (result == D3DERR_DEVICELOST) {
slouken@5297
   319
            if (D3D_Reset(renderer) < 0) {
slouken@5297
   320
                return -1;
slouken@5297
   321
            }
slouken@5297
   322
            result = IDirect3DDevice9_BeginScene(data->device);
slouken@5297
   323
        }
slouken@5297
   324
        if (FAILED(result)) {
slouken@5297
   325
            D3D_SetError("BeginScene()", result);
slouken@5297
   326
            return -1;
slouken@5297
   327
        }
slouken@5297
   328
        data->beginScene = SDL_FALSE;
slouken@5297
   329
    }
slouken@5297
   330
    return 0;
slouken@5297
   331
}
slouken@5297
   332
slouken@1895
   333
SDL_Renderer *
slouken@1913
   334
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   335
{
slouken@1895
   336
    SDL_Renderer *renderer;
slouken@1913
   337
    D3D_RenderData *data;
slouken@5154
   338
    SDL_SysWMinfo windowinfo;
slouken@1900
   339
    HRESULT result;
slouken@1900
   340
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   341
    IDirect3DSwapChain9 *chain;
slouken@1925
   342
    D3DCAPS9 caps;
slouken@5156
   343
    Uint32 window_flags;
slouken@5156
   344
    int w, h;
slouken@5156
   345
    SDL_DisplayMode fullscreen_mode;
slouken@5297
   346
    D3DMATRIX matrix;
slouken@1895
   347
slouken@1920
   348
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   349
    if (!renderer) {
slouken@1895
   350
        SDL_OutOfMemory();
slouken@1895
   351
        return NULL;
slouken@1895
   352
    }
slouken@1895
   353
slouken@1920
   354
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   355
    if (!data) {
slouken@5154
   356
        SDL_free(renderer);
slouken@1895
   357
        SDL_OutOfMemory();
slouken@1895
   358
        return NULL;
slouken@1895
   359
    }
slouken@1895
   360
slouken@5154
   361
    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
slouken@5154
   362
    if (data->d3dDLL) {
slouken@5154
   363
        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
slouken@4569
   364
slouken@5154
   365
        D3DCreate =
slouken@5154
   366
            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
slouken@5154
   367
                                                            "Direct3DCreate9");
slouken@5154
   368
        if (D3DCreate) {
slouken@5154
   369
            data->d3d = D3DCreate(D3D_SDK_VERSION);
slouken@5154
   370
        }
slouken@5154
   371
        if (!data->d3d) {
slouken@5154
   372
            SDL_UnloadObject(data->d3dDLL);
slouken@5154
   373
            data->d3dDLL = NULL;
slouken@5154
   374
        }
slouken@5154
   375
    }
slouken@5154
   376
    if (!data->d3d) {
slouken@5154
   377
        SDL_free(renderer);
slouken@5154
   378
        SDL_free(data);
slouken@5154
   379
        SDL_SetError("Unable to create Direct3D interface");
slouken@5154
   380
        return NULL;
slouken@5154
   381
    }
slouken@5154
   382
slouken@5297
   383
    renderer->WindowEvent = D3D_WindowEvent;
slouken@1913
   384
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   385
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   386
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   387
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@5297
   388
    renderer->UpdateViewport = D3D_UpdateViewport;
slouken@5297
   389
    renderer->RenderClear = D3D_RenderClear;
slouken@3599
   390
    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
slouken@3599
   391
    renderer->RenderDrawLines = D3D_RenderDrawLines;
slouken@3675
   392
    renderer->RenderFillRects = D3D_RenderFillRects;
slouken@1913
   393
    renderer->RenderCopy = D3D_RenderCopy;
slouken@3427
   394
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@1913
   395
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   396
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   397
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   398
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   399
    renderer->driverdata = data;
slouken@1895
   400
slouken@1965
   401
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   402
slouken@5154
   403
    SDL_VERSION(&windowinfo.version);
slouken@5154
   404
    SDL_GetWindowWMInfo(window, &windowinfo);
slouken@5154
   405
slouken@5156
   406
    window_flags = SDL_GetWindowFlags(window);
slouken@5156
   407
    SDL_GetWindowSize(window, &w, &h);
slouken@5156
   408
    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
slouken@5156
   409
slouken@1900
   410
    SDL_zero(pparams);
slouken@5154
   411
    pparams.hDeviceWindow = windowinfo.info.win.window;
slouken@5156
   412
    pparams.BackBufferWidth = w;
slouken@5156
   413
    pparams.BackBufferHeight = h;
slouken@5156
   414
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   415
        pparams.BackBufferFormat =
slouken@5156
   416
            PixelFormatToD3DFMT(fullscreen_mode.format);
slouken@1903
   417
    } else {
slouken@1903
   418
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   419
    }
slouken@5142
   420
    pparams.BackBufferCount = 1;
slouken@5142
   421
    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@5142
   422
slouken@5156
   423
    if (window_flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   424
        pparams.Windowed = FALSE;
slouken@1903
   425
        pparams.FullScreen_RefreshRateInHz =
slouken@5156
   426
            fullscreen_mode.refresh_rate;
slouken@1900
   427
    } else {
slouken@1900
   428
        pparams.Windowed = TRUE;
slouken@1903
   429
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   430
    }
slouken@1965
   431
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   432
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   433
    } else {
slouken@1907
   434
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   435
    }
slouken@1900
   436
slouken@5154
   437
    /* FIXME: Which adapter? */
slouken@5154
   438
    data->adapter = D3DADAPTER_DEFAULT;
slouken@5154
   439
    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
slouken@3197
   440
slouken@5154
   441
    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
slouken@1900
   442
                                     D3DDEVTYPE_HAL,
slouken@5154
   443
                                     pparams.hDeviceWindow,
slouken@3197
   444
                                     (caps.
slouken@3197
   445
                                      DevCaps &
slouken@3197
   446
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   447
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@1900
   448
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   449
                                     &pparams, &data->device);
slouken@1900
   450
    if (FAILED(result)) {
slouken@1913
   451
        D3D_DestroyRenderer(renderer);
slouken@1900
   452
        D3D_SetError("CreateDevice()", result);
slouken@1900
   453
        return NULL;
slouken@1900
   454
    }
slouken@1900
   455
    data->beginScene = SDL_TRUE;
slouken@5484
   456
    data->scaleMode = D3DTEXF_FORCE_DWORD;
slouken@1900
   457
slouken@1907
   458
    /* Get presentation parameters to fill info */
slouken@1907
   459
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   460
    if (FAILED(result)) {
slouken@1913
   461
        D3D_DestroyRenderer(renderer);
slouken@1907
   462
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   463
        return NULL;
slouken@1907
   464
    }
slouken@1907
   465
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   466
    if (FAILED(result)) {
slouken@1907
   467
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   468
        D3D_DestroyRenderer(renderer);
slouken@1907
   469
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   470
        return NULL;
slouken@1907
   471
    }
slouken@1907
   472
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   473
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   474
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   475
    }
slouken@1975
   476
    data->pparams = pparams;
slouken@1907
   477
slouken@1925
   478
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   479
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   480
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@1918
   481
slouken@1903
   482
    /* Set up parameters for rendering */
slouken@1904
   483
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   484
    IDirect3DDevice9_SetFVF(data->device,
slouken@5297
   485
                            D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   486
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   487
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   488
                                    D3DCULL_NONE);
slouken@1904
   489
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   490
    /* Enable color modulation by diffuse color */
slouken@1988
   491
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   492
                                          D3DTOP_MODULATE);
slouken@1988
   493
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   494
                                          D3DTA_TEXTURE);
slouken@1988
   495
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   496
                                          D3DTA_DIFFUSE);
slouken@1988
   497
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   498
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   499
                                          D3DTOP_MODULATE);
slouken@1988
   500
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   501
                                          D3DTA_TEXTURE);
slouken@1988
   502
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   503
                                          D3DTA_DIFFUSE);
slouken@1991
   504
    /* Disable second texture stage, since we're done */
slouken@1991
   505
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   506
                                          D3DTOP_DISABLE);
slouken@1991
   507
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   508
                                          D3DTOP_DISABLE);
slouken@1903
   509
slouken@5297
   510
    /* Set an identity world and view matrix */
slouken@5297
   511
    matrix.m[0][0] = 1.0f;
slouken@5297
   512
    matrix.m[0][1] = 0.0f;
slouken@5297
   513
    matrix.m[0][2] = 0.0f;
slouken@5297
   514
    matrix.m[0][3] = 0.0f;
slouken@5297
   515
    matrix.m[1][0] = 0.0f;
slouken@5297
   516
    matrix.m[1][1] = 1.0f;
slouken@5297
   517
    matrix.m[1][2] = 0.0f;
slouken@5297
   518
    matrix.m[1][3] = 0.0f;
slouken@5297
   519
    matrix.m[2][0] = 0.0f;
slouken@5297
   520
    matrix.m[2][1] = 0.0f;
slouken@5297
   521
    matrix.m[2][2] = 1.0f;
slouken@5297
   522
    matrix.m[2][3] = 0.0f;
slouken@5297
   523
    matrix.m[3][0] = 0.0f;
slouken@5297
   524
    matrix.m[3][1] = 0.0f;
slouken@5297
   525
    matrix.m[3][2] = 0.0f;
slouken@5297
   526
    matrix.m[3][3] = 1.0f;
slouken@5297
   527
    IDirect3DDevice9_SetTransform(data->device, D3DTS_WORLD, &matrix);
slouken@5297
   528
    IDirect3DDevice9_SetTransform(data->device, D3DTS_VIEW, &matrix);
slouken@5297
   529
slouken@1895
   530
    return renderer;
slouken@1895
   531
}
slouken@1895
   532
slouken@5297
   533
static void
slouken@5297
   534
D3D_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
slouken@1975
   535
{
slouken@1975
   536
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   537
slouken@5297
   538
    if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
slouken@5297
   539
        data->updateSize = SDL_TRUE;
slouken@1975
   540
    }
slouken@1975
   541
}
slouken@1975
   542
slouken@5484
   543
static D3DTEXTUREFILTERTYPE
slouken@5484
   544
GetScaleQuality(void)
slouken@5484
   545
{
slouken@5484
   546
    const char *hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY);
slouken@5484
   547
slouken@5484
   548
    if (!hint || *hint == '0' || SDL_strcasecmp(hint, "nearest") == 0) {
slouken@5484
   549
        return D3DTEXF_POINT;
slouken@5484
   550
    } else if (*hint == '1' || SDL_strcasecmp(hint, "linear") == 0) {
slouken@5484
   551
        return D3DTEXF_LINEAR;
slouken@5484
   552
    } else {
slouken@5484
   553
        return D3DTEXF_ANISOTROPIC;
slouken@5484
   554
    }
slouken@5484
   555
}
slouken@5484
   556
slouken@1975
   557
static int
slouken@1913
   558
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   559
{
slouken@1913
   560
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@3685
   561
    SDL_Window *window = renderer->window;
slouken@5154
   562
    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
slouken@1913
   563
    D3D_TextureData *data;
slouken@5173
   564
    D3DPOOL pool;
slouken@5173
   565
    DWORD usage;
slouken@1903
   566
    HRESULT result;
slouken@1895
   567
slouken@1920
   568
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   569
    if (!data) {
slouken@1895
   570
        SDL_OutOfMemory();
slouken@1895
   571
        return -1;
slouken@1895
   572
    }
slouken@5484
   573
    data->scaleMode = GetScaleQuality();
slouken@1895
   574
slouken@1895
   575
    texture->driverdata = data;
slouken@1895
   576
slouken@5173
   577
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   578
    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
slouken@5173
   579
        pool = D3DPOOL_DEFAULT;
slouken@5173
   580
        usage = D3DUSAGE_DYNAMIC;
slouken@5173
   581
    } else
slouken@5173
   582
#endif
slouken@5173
   583
    {
slouken@5173
   584
        pool = D3DPOOL_MANAGED;
slouken@5173
   585
        usage = 0;
slouken@5173
   586
    }
slouken@2973
   587
slouken@1903
   588
    result =
slouken@1903
   589
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@5173
   590
                                       texture->h, 1, usage,
slouken@5173
   591
                                       PixelFormatToD3DFMT(texture->format),
slouken@5173
   592
                                       pool, &data->texture, NULL);
slouken@1903
   593
    if (FAILED(result)) {
slouken@1903
   594
        D3D_SetError("CreateTexture()", result);
slouken@1903
   595
        return -1;
slouken@1903
   596
    }
slouken@1903
   597
slouken@1895
   598
    return 0;
slouken@1895
   599
}
slouken@1895
   600
slouken@1895
   601
static int
slouken@1913
   602
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   603
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   604
{
slouken@1913
   605
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   606
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@5156
   607
    RECT d3drect;
slouken@5156
   608
    D3DLOCKED_RECT locked;
slouken@5156
   609
    const Uint8 *src;
slouken@5156
   610
    Uint8 *dst;
slouken@5156
   611
    int row, length;
slouken@5156
   612
    HRESULT result;
slouken@2973
   613
slouken@5173
   614
#ifdef USE_DYNAMIC_TEXTURE
slouken@5173
   615
    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
slouken@5173
   616
        rect->x == 0 && rect->y == 0 &&
slouken@5173
   617
        rect->w == texture->w && rect->h == texture->h) {
slouken@5194
   618
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
slouken@5173
   619
    } else
slouken@5173
   620
#endif
slouken@5173
   621
    {
slouken@5173
   622
        d3drect.left = rect->x;
slouken@5173
   623
        d3drect.right = rect->x + rect->w;
slouken@5173
   624
        d3drect.top = rect->y;
slouken@5173
   625
        d3drect.bottom = rect->y + rect->h;
slouken@5194
   626
        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   627
    }
slouken@2973
   628
slouken@5156
   629
    if (FAILED(result)) {
slouken@5156
   630
        D3D_SetError("LockRect()", result);
slouken@5156
   631
        return -1;
slouken@5156
   632
    }
slouken@2973
   633
slouken@5156
   634
    src = pixels;
slouken@5156
   635
    dst = locked.pBits;
slouken@5156
   636
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@5173
   637
    if (length == pitch && length == locked.Pitch) {
slouken@5173
   638
        SDL_memcpy(dst, src, length*rect->h);
slouken@5173
   639
    } else {
slouken@5173
   640
        for (row = 0; row < rect->h; ++row) {
slouken@5173
   641
            SDL_memcpy(dst, src, length);
slouken@5173
   642
            src += pitch;
slouken@5173
   643
            dst += locked.Pitch;
slouken@5173
   644
        }
slouken@5156
   645
    }
slouken@5156
   646
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   647
slouken@5156
   648
    return 0;
slouken@1895
   649
}
slouken@1895
   650
slouken@1895
   651
static int
slouken@1913
   652
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@5156
   653
                const SDL_Rect * rect, void **pixels, int *pitch)
slouken@1895
   654
{
slouken@1913
   655
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@5156
   656
    RECT d3drect;
slouken@5156
   657
    D3DLOCKED_RECT locked;
slouken@5156
   658
    HRESULT result;
slouken@1895
   659
slouken@5156
   660
    d3drect.left = rect->x;
slouken@5156
   661
    d3drect.right = rect->x + rect->w;
slouken@5156
   662
    d3drect.top = rect->y;
slouken@5156
   663
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   664
slouken@5156
   665
    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@5156
   666
    if (FAILED(result)) {
slouken@5156
   667
        D3D_SetError("LockRect()", result);
slouken@5156
   668
        return -1;
slouken@1903
   669
    }
slouken@5156
   670
    *pixels = locked.pBits;
slouken@5156
   671
    *pitch = locked.Pitch;
slouken@5156
   672
    return 0;
slouken@1895
   673
}
slouken@1895
   674
slouken@1895
   675
static void
slouken@1913
   676
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   677
{
slouken@1913
   678
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   679
slouken@5156
   680
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   681
}
slouken@1895
   682
slouken@5297
   683
static int
slouken@5297
   684
D3D_UpdateViewport(SDL_Renderer * renderer)
slouken@5224
   685
{
slouken@5224
   686
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   687
    D3DVIEWPORT9 viewport;
slouken@5297
   688
    D3DMATRIX matrix;
slouken@5224
   689
slouken@5297
   690
    /* Set the viewport */
slouken@5297
   691
    viewport.X = renderer->viewport.x;
slouken@5297
   692
    viewport.Y = renderer->viewport.y;
slouken@5297
   693
    viewport.Width = renderer->viewport.w;
slouken@5297
   694
    viewport.Height = renderer->viewport.h;
slouken@5297
   695
    viewport.MinZ = 0.0f;
slouken@5297
   696
    viewport.MaxZ = 1.0f;
slouken@5297
   697
    IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5224
   698
slouken@5297
   699
    /* Set an orthographic projection matrix */
slouken@5297
   700
    matrix.m[0][0] = 2.0f / renderer->viewport.w;
slouken@5297
   701
    matrix.m[0][1] = 0.0f;
slouken@5297
   702
    matrix.m[0][2] = 0.0f;
slouken@5297
   703
    matrix.m[0][3] = 0.0f;
slouken@5297
   704
    matrix.m[1][0] = 0.0f;
slouken@5297
   705
    matrix.m[1][1] = -2.0f / renderer->viewport.h;
slouken@5297
   706
    matrix.m[1][2] = 0.0f;
slouken@5297
   707
    matrix.m[1][3] = 0.0f;
slouken@5297
   708
    matrix.m[2][0] = 0.0f;
slouken@5297
   709
    matrix.m[2][1] = 0.0f;
slouken@5297
   710
    matrix.m[2][2] = 1.0f;
slouken@5297
   711
    matrix.m[2][3] = 0.0f;
slouken@5297
   712
    matrix.m[3][0] = -1.0f;
slouken@5297
   713
    matrix.m[3][1] = 1.0f;
slouken@5297
   714
    matrix.m[3][2] = 0.0f;
slouken@5297
   715
    matrix.m[3][3] = 1.0f;
slouken@5297
   716
    IDirect3DDevice9_SetTransform(data->device, D3DTS_PROJECTION, &matrix);
slouken@5297
   717
slouken@5297
   718
    return 0;
slouken@5297
   719
}
slouken@5297
   720
slouken@5297
   721
static int
slouken@5297
   722
D3D_RenderClear(SDL_Renderer * renderer)
slouken@5297
   723
{
slouken@5297
   724
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@5297
   725
    DWORD color;
slouken@5297
   726
    HRESULT result;
slouken@5297
   727
slouken@5297
   728
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   729
        return -1;
slouken@5224
   730
    }
slouken@5297
   731
slouken@5297
   732
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@5297
   733
slouken@5299
   734
    /* Don't reset the viewport if we don't have to! */
slouken@5299
   735
    if (!renderer->viewport.x && !renderer->viewport.y &&
slouken@5299
   736
        renderer->viewport.w == data->pparams.BackBufferWidth &&
slouken@5299
   737
        renderer->viewport.h == data->pparams.BackBufferHeight) {
slouken@5299
   738
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   739
    } else {
slouken@5299
   740
        D3DVIEWPORT9 viewport;
slouken@5297
   741
slouken@5299
   742
        /* Clear is defined to clear the entire render target */
slouken@5299
   743
        viewport.X = 0;
slouken@5299
   744
        viewport.Y = 0;
slouken@5299
   745
        viewport.Width = data->pparams.BackBufferWidth;
slouken@5299
   746
        viewport.Height = data->pparams.BackBufferHeight;
slouken@5299
   747
        viewport.MinZ = 0.0f;
slouken@5299
   748
        viewport.MaxZ = 1.0f;
slouken@5299
   749
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   750
slouken@5299
   751
        result = IDirect3DDevice9_Clear(data->device, 0, NULL, D3DCLEAR_TARGET, color, 0.0f, 0);
slouken@5299
   752
slouken@5299
   753
        /* Reset the viewport */
slouken@5299
   754
        viewport.X = renderer->viewport.x;
slouken@5299
   755
        viewport.Y = renderer->viewport.y;
slouken@5299
   756
        viewport.Width = renderer->viewport.w;
slouken@5299
   757
        viewport.Height = renderer->viewport.h;
slouken@5299
   758
        viewport.MinZ = 0.0f;
slouken@5299
   759
        viewport.MaxZ = 1.0f;
slouken@5299
   760
        IDirect3DDevice9_SetViewport(data->device, &viewport);
slouken@5299
   761
    }
slouken@5297
   762
slouken@5297
   763
    if (FAILED(result)) {
slouken@5297
   764
        D3D_SetError("Clear()", result);
slouken@5297
   765
        return -1;
slouken@5297
   766
    }
slouken@5297
   767
    return 0;
slouken@5224
   768
}
slouken@5224
   769
slouken@5224
   770
static void
slouken@2933
   771
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   772
{
slouken@2932
   773
    switch (blendMode) {
slouken@2932
   774
    case SDL_BLENDMODE_NONE:
slouken@2932
   775
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   776
                                        FALSE);
slouken@2932
   777
        break;
slouken@2932
   778
    case SDL_BLENDMODE_BLEND:
slouken@2932
   779
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   780
                                        TRUE);
slouken@2932
   781
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   782
                                        D3DBLEND_SRCALPHA);
slouken@2932
   783
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   784
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   785
        break;
slouken@2932
   786
    case SDL_BLENDMODE_ADD:
slouken@2932
   787
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   788
                                        TRUE);
slouken@2932
   789
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   790
                                        D3DBLEND_SRCALPHA);
slouken@2932
   791
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   792
                                        D3DBLEND_ONE);
slouken@2932
   793
        break;
slouken@5184
   794
    case SDL_BLENDMODE_MOD:
slouken@5184
   795
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@5184
   796
                                        TRUE);
slouken@5184
   797
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@5184
   798
                                        D3DBLEND_ZERO);
slouken@5184
   799
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@5184
   800
                                        D3DBLEND_SRCCOLOR);
slouken@5184
   801
        break;
slouken@2932
   802
    }
slouken@2932
   803
}
slouken@2932
   804
slouken@1895
   805
static int
slouken@3599
   806
D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   807
                     int count)
slouken@1895
   808
{
slouken@1913
   809
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   810
    DWORD color;
slouken@3536
   811
    Vertex *vertices;
slouken@3536
   812
    int i;
slouken@1900
   813
    HRESULT result;
slouken@1895
   814
slouken@5297
   815
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   816
        return -1;
slouken@1900
   817
    }
slouken@1895
   818
slouken@2932
   819
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   820
slouken@2933
   821
    result =
slouken@2933
   822
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   823
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   824
    if (FAILED(result)) {
slouken@2932
   825
        D3D_SetError("SetTexture()", result);
slouken@2932
   826
        return -1;
slouken@2932
   827
    }
slouken@3536
   828
slouken@3536
   829
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   830
slouken@3536
   831
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   832
    for (i = 0; i < count; ++i) {
slouken@3536
   833
        vertices[i].x = (float) points[i].x;
slouken@3536
   834
        vertices[i].y = (float) points[i].y;
slouken@3536
   835
        vertices[i].z = 0.0f;
slouken@3536
   836
        vertices[i].color = color;
slouken@3536
   837
        vertices[i].u = 0.0f;
slouken@3536
   838
        vertices[i].v = 0.0f;
slouken@3536
   839
    }
slouken@1903
   840
    result =
slouken@3536
   841
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
slouken@2932
   842
                                         vertices, sizeof(*vertices));
slouken@3536
   843
    SDL_stack_free(vertices);
slouken@1900
   844
    if (FAILED(result)) {
slouken@2932
   845
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   846
        return -1;
slouken@2932
   847
    }
slouken@2932
   848
    return 0;
slouken@2932
   849
}
slouken@2932
   850
slouken@2932
   851
static int
slouken@3599
   852
D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
slouken@3599
   853
                    int count)
slouken@2932
   854
{
slouken@2932
   855
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   856
    DWORD color;
slouken@3536
   857
    Vertex *vertices;
slouken@3536
   858
    int i;
slouken@2932
   859
    HRESULT result;
slouken@2932
   860
slouken@5297
   861
    if (D3D_ActivateRenderer(renderer) < 0) {
slouken@5297
   862
        return -1;
slouken@2932
   863
    }
slouken@2932
   864
slouken@2932
   865
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   866
slouken@2933
   867
    result =
slouken@2933
   868
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   869
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   870
    if (FAILED(result)) {
slouken@2932
   871
        D3D_SetError("SetTexture()", result);
slouken@2932
   872
        return -1;
slouken@2932
   873
    }
slouken@3536
   874
slouken@3536
   875
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@3536
   876
slouken@3536
   877
    vertices = SDL_stack_alloc(Vertex, count);
slouken@3536
   878
    for (i = 0; i < count; ++i) {
slouken@3536
   879
        vertices[i].x = (float) points[i].x;
slouken@3536
   880
        vertices[i].y = (float) points[i].y;
slouken@3536
   881
        vertices[i].z = 0.0f;
slouken@3536
   882
        vertices[i].color = color;
slouken@3536
   883
        vertices[i].u = 0.0f;
slouken@3536
   884
        vertices[i].v = 0.0f;
slouken@3536
   885
    }
slouken@2932
   886
    result =
slouken@3551
   887
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
slouken@2932
   888
                                         vertices, sizeof(*vertices));
slouken@3551
   889
slouken@3551
   890
    /* DirectX 9 has the same line rasterization semantics as GDI,
slouken@3551
   891
       so we need to close the endpoint of the line */
slouken@6076
   892
    if (count == 2 ||
slouken@6076
   893
        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: */