src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 17 Jul 2006 06:47:33 +0000
changeset 1913 83420da906a5
parent 1907 06c27a737b7a
child 1916 c773b0c0ac89
permissions -rw-r--r--
Implemented Windows OpenGL support
Fixed slowdown enumerating display modes, which was hosing OpenGL as well...
Removed SDL_ from the render driver prefixes
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#if SDL_VIDEO_RENDER_D3D
slouken@1895
    25
slouken@1895
    26
#include "SDL_win32video.h"
slouken@1895
    27
slouken@1895
    28
/* Direct3D renderer implementation */
slouken@1895
    29
slouken@1913
    30
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1913
    31
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    32
static int D3D_SetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    33
                                 SDL_Texture * texture,
slouken@1913
    34
                                 const SDL_Color * colors, int firstcolor,
slouken@1913
    35
                                 int ncolors);
slouken@1913
    36
static int D3D_GetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    37
                                 SDL_Texture * texture, SDL_Color * colors,
slouken@1913
    38
                                 int firstcolor, int ncolors);
slouken@1913
    39
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    40
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
    41
                             int pitch);
slouken@1913
    42
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    43
                           const SDL_Rect * rect, int markDirty,
slouken@1913
    44
                           void **pixels, int *pitch);
slouken@1913
    45
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    46
static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    47
                             int numrects, const SDL_Rect * rects);
slouken@1913
    48
static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1913
    49
                          Uint32 color);
slouken@1913
    50
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    51
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1913
    52
                          int blendMode, int scaleMode);
slouken@1913
    53
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
    54
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
    55
                               SDL_Texture * texture);
slouken@1913
    56
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    57
slouken@1895
    58
slouken@1913
    59
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
    60
    D3D_CreateRenderer,
slouken@1895
    61
    {
slouken@1895
    62
     "d3d",
slouken@1905
    63
     (SDL_Renderer_SingleBuffer | SDL_Renderer_PresentCopy |
slouken@1905
    64
      SDL_Renderer_PresentFlip2 | SDL_Renderer_PresentFlip3 |
slouken@1907
    65
      SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync),
slouken@1907
    66
     (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask | SDL_TextureBlendMode_Blend),      /* FIXME */
slouken@1907
    67
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast),   /* FIXME */
slouken@1903
    68
     12,
slouken@1895
    69
     {
slouken@1895
    70
      SDL_PixelFormat_Index8,
slouken@1903
    71
      SDL_PixelFormat_RGB332,
slouken@1903
    72
      SDL_PixelFormat_RGB444,
slouken@1895
    73
      SDL_PixelFormat_RGB555,
slouken@1903
    74
      SDL_PixelFormat_ARGB4444,
slouken@1903
    75
      SDL_PixelFormat_ARGB1555,
slouken@1895
    76
      SDL_PixelFormat_RGB565,
slouken@1895
    77
      SDL_PixelFormat_RGB888,
slouken@1895
    78
      SDL_PixelFormat_ARGB8888,
slouken@1903
    79
      SDL_PixelFormat_ARGB2101010,
slouken@1903
    80
      SDL_PixelFormat_UYVY,
slouken@1903
    81
      SDL_PixelFormat_YUY2},
slouken@1895
    82
     0,
slouken@1895
    83
     0}
slouken@1895
    84
};
slouken@1895
    85
slouken@1895
    86
typedef struct
slouken@1895
    87
{
slouken@1895
    88
    IDirect3DDevice9 *device;
slouken@1900
    89
    SDL_bool beginScene;
slouken@1913
    90
} D3D_RenderData;
slouken@1895
    91
slouken@1895
    92
typedef struct
slouken@1895
    93
{
slouken@1903
    94
    IDirect3DTexture9 *texture;
slouken@1913
    95
} D3D_TextureData;
slouken@1895
    96
slouken@1903
    97
typedef struct
slouken@1903
    98
{
slouken@1903
    99
    float x, y, z;
slouken@1904
   100
    float rhw;
slouken@1904
   101
    float u, v;
slouken@1903
   102
} Vertex;
slouken@1903
   103
slouken@1895
   104
static void
slouken@1900
   105
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   106
{
slouken@1900
   107
    const char *error;
slouken@1900
   108
slouken@1900
   109
    switch (result) {
slouken@1900
   110
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   111
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   112
        break;
slouken@1900
   113
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   114
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   115
        break;
slouken@1900
   116
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   117
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   118
        break;
slouken@1900
   119
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   120
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   121
        break;
slouken@1900
   122
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   123
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   124
        break;
slouken@1900
   125
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   126
        error = "TOOMANYOPERATIONS";
slouken@1900
   127
        break;
slouken@1900
   128
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   129
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   130
        break;
slouken@1900
   131
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   132
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   133
        break;
slouken@1900
   134
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   135
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   136
        break;
slouken@1900
   137
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   138
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   139
        break;
slouken@1900
   140
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   141
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   142
        break;
slouken@1900
   143
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   144
        error = "DRIVERINTERNALERROR";
slouken@1900
   145
        break;
slouken@1900
   146
    case D3DERR_NOTFOUND:
slouken@1900
   147
        error = "NOTFOUND";
slouken@1900
   148
        break;
slouken@1900
   149
    case D3DERR_MOREDATA:
slouken@1900
   150
        error = "MOREDATA";
slouken@1900
   151
        break;
slouken@1900
   152
    case D3DERR_DEVICELOST:
slouken@1900
   153
        error = "DEVICELOST";
slouken@1900
   154
        break;
slouken@1900
   155
    case D3DERR_DEVICENOTRESET:
slouken@1900
   156
        error = "DEVICENOTRESET";
slouken@1900
   157
        break;
slouken@1900
   158
    case D3DERR_NOTAVAILABLE:
slouken@1900
   159
        error = "NOTAVAILABLE";
slouken@1900
   160
        break;
slouken@1900
   161
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   162
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   163
        break;
slouken@1900
   164
    case D3DERR_INVALIDDEVICE:
slouken@1900
   165
        error = "INVALIDDEVICE";
slouken@1900
   166
        break;
slouken@1900
   167
    case D3DERR_INVALIDCALL:
slouken@1900
   168
        error = "INVALIDCALL";
slouken@1900
   169
        break;
slouken@1900
   170
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   171
        error = "DRIVERINVALIDCALL";
slouken@1900
   172
        break;
slouken@1900
   173
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   174
        error = "WASSTILLDRAWING";
slouken@1900
   175
        break;
slouken@1900
   176
    default:
slouken@1900
   177
        error = "UNKNOWN";
slouken@1900
   178
        break;
slouken@1900
   179
    }
slouken@1900
   180
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   181
}
slouken@1900
   182
slouken@1903
   183
static D3DFORMAT
slouken@1903
   184
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   185
{
slouken@1903
   186
    switch (format) {
slouken@1903
   187
    case SDL_PixelFormat_Index8:
slouken@1903
   188
        return D3DFMT_P8;
slouken@1903
   189
    case SDL_PixelFormat_RGB332:
slouken@1903
   190
        return D3DFMT_R3G3B2;
slouken@1903
   191
    case SDL_PixelFormat_RGB444:
slouken@1903
   192
        return D3DFMT_X4R4G4B4;
slouken@1903
   193
    case SDL_PixelFormat_RGB555:
slouken@1903
   194
        return D3DFMT_X1R5G5B5;
slouken@1903
   195
    case SDL_PixelFormat_ARGB4444:
slouken@1903
   196
        return D3DFMT_A4R4G4B4;
slouken@1903
   197
    case SDL_PixelFormat_ARGB1555:
slouken@1903
   198
        return D3DFMT_A1R5G5B5;
slouken@1903
   199
    case SDL_PixelFormat_RGB565:
slouken@1903
   200
        return D3DFMT_R5G6B5;
slouken@1903
   201
    case SDL_PixelFormat_RGB888:
slouken@1903
   202
        return D3DFMT_X8R8G8B8;
slouken@1903
   203
    case SDL_PixelFormat_ARGB8888:
slouken@1903
   204
        return D3DFMT_A8R8G8B8;
slouken@1903
   205
    case SDL_PixelFormat_ARGB2101010:
slouken@1903
   206
        return D3DFMT_A2R10G10B10;
slouken@1903
   207
    case SDL_PixelFormat_UYVY:
slouken@1903
   208
        return D3DFMT_UYVY;
slouken@1903
   209
    case SDL_PixelFormat_YUY2:
slouken@1903
   210
        return D3DFMT_YUY2;
slouken@1903
   211
    default:
slouken@1903
   212
        return D3DFMT_UNKNOWN;
slouken@1903
   213
    }
slouken@1895
   214
}
slouken@1895
   215
slouken@1895
   216
void
slouken@1895
   217
D3D_AddRenderDriver(_THIS)
slouken@1895
   218
{
slouken@1895
   219
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   220
slouken@1895
   221
    if (data->d3d) {
slouken@1913
   222
        SDL_AddRenderDriver(0, &D3D_RenderDriver);
slouken@1895
   223
    }
slouken@1895
   224
}
slouken@1895
   225
slouken@1895
   226
SDL_Renderer *
slouken@1913
   227
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   228
{
slouken@1895
   229
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   230
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1895
   231
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   232
    SDL_Renderer *renderer;
slouken@1913
   233
    D3D_RenderData *data;
slouken@1900
   234
    HRESULT result;
slouken@1900
   235
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   236
    IDirect3DSwapChain9 *chain;
slouken@1895
   237
slouken@1895
   238
    renderer = (SDL_Renderer *) SDL_malloc(sizeof(*renderer));
slouken@1895
   239
    if (!renderer) {
slouken@1895
   240
        SDL_OutOfMemory();
slouken@1895
   241
        return NULL;
slouken@1895
   242
    }
slouken@1895
   243
    SDL_zerop(renderer);
slouken@1895
   244
slouken@1913
   245
    data = (D3D_RenderData *) SDL_malloc(sizeof(*data));
slouken@1895
   246
    if (!data) {
slouken@1913
   247
        D3D_DestroyRenderer(renderer);
slouken@1895
   248
        SDL_OutOfMemory();
slouken@1895
   249
        return NULL;
slouken@1895
   250
    }
slouken@1895
   251
    SDL_zerop(data);
slouken@1895
   252
slouken@1913
   253
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   254
    renderer->SetTexturePalette = D3D_SetTexturePalette;
slouken@1913
   255
    renderer->GetTexturePalette = D3D_GetTexturePalette;
slouken@1913
   256
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   257
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   258
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@1913
   259
    renderer->DirtyTexture = D3D_DirtyTexture;
slouken@1913
   260
    renderer->RenderFill = D3D_RenderFill;
slouken@1913
   261
    renderer->RenderCopy = D3D_RenderCopy;
slouken@1913
   262
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   263
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   264
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   265
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   266
    renderer->window = window->id;
slouken@1895
   267
    renderer->driverdata = data;
slouken@1895
   268
slouken@1907
   269
    renderer->info.flags = SDL_Renderer_Accelerated;
slouken@1895
   270
slouken@1900
   271
    SDL_zero(pparams);
slouken@1900
   272
    pparams.BackBufferWidth = window->w;
slouken@1900
   273
    pparams.BackBufferHeight = window->h;
slouken@1903
   274
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   275
        pparams.BackBufferFormat =
slouken@1903
   276
            PixelFormatToD3DFMT(display->fullscreen_mode->format);
slouken@1903
   277
    } else {
slouken@1903
   278
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   279
    }
slouken@1900
   280
    if (flags & SDL_Renderer_PresentFlip2) {
slouken@1900
   281
        pparams.BackBufferCount = 2;
slouken@1900
   282
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1900
   283
    } else if (flags & SDL_Renderer_PresentFlip3) {
slouken@1900
   284
        pparams.BackBufferCount = 3;
slouken@1900
   285
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1900
   286
    } else if (flags & SDL_Renderer_PresentCopy) {
slouken@1900
   287
        pparams.BackBufferCount = 1;
slouken@1900
   288
        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
slouken@1900
   289
    } else {
slouken@1900
   290
        pparams.BackBufferCount = 1;
slouken@1900
   291
        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@1900
   292
    }
slouken@1900
   293
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   294
        pparams.Windowed = FALSE;
slouken@1903
   295
        pparams.FullScreen_RefreshRateInHz =
slouken@1903
   296
            display->fullscreen_mode->refresh_rate;
slouken@1900
   297
    } else {
slouken@1900
   298
        pparams.Windowed = TRUE;
slouken@1903
   299
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   300
    }
slouken@1907
   301
    if (flags & SDL_Renderer_PresentVSync) {
slouken@1907
   302
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   303
    } else {
slouken@1907
   304
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   305
    }
slouken@1900
   306
slouken@1900
   307
    result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
slouken@1900
   308
                                     D3DDEVTYPE_HAL,
slouken@1900
   309
                                     windowdata->hwnd,
slouken@1900
   310
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   311
                                     &pparams, &data->device);
slouken@1900
   312
    if (FAILED(result)) {
slouken@1913
   313
        D3D_DestroyRenderer(renderer);
slouken@1900
   314
        D3D_SetError("CreateDevice()", result);
slouken@1900
   315
        return NULL;
slouken@1900
   316
    }
slouken@1900
   317
    data->beginScene = SDL_TRUE;
slouken@1900
   318
slouken@1907
   319
    /* Get presentation parameters to fill info */
slouken@1907
   320
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   321
    if (FAILED(result)) {
slouken@1913
   322
        D3D_DestroyRenderer(renderer);
slouken@1907
   323
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   324
        return NULL;
slouken@1907
   325
    }
slouken@1907
   326
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   327
    if (FAILED(result)) {
slouken@1907
   328
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   329
        D3D_DestroyRenderer(renderer);
slouken@1907
   330
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   331
        return NULL;
slouken@1907
   332
    }
slouken@1907
   333
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   334
    switch (pparams.SwapEffect) {
slouken@1907
   335
    case D3DSWAPEFFECT_COPY:
slouken@1907
   336
        renderer->info.flags |= SDL_Renderer_PresentCopy;
slouken@1907
   337
        break;
slouken@1907
   338
    case D3DSWAPEFFECT_FLIP:
slouken@1907
   339
        switch (pparams.BackBufferCount) {
slouken@1907
   340
        case 2:
slouken@1907
   341
            renderer->info.flags |= SDL_Renderer_PresentFlip2;
slouken@1907
   342
            break;
slouken@1907
   343
        case 3:
slouken@1907
   344
            renderer->info.flags |= SDL_Renderer_PresentFlip3;
slouken@1907
   345
            break;
slouken@1907
   346
        }
slouken@1907
   347
        break;
slouken@1907
   348
    case D3DSWAPEFFECT_DISCARD:
slouken@1907
   349
        renderer->info.flags |= SDL_Renderer_PresentDiscard;
slouken@1907
   350
        break;
slouken@1907
   351
    }
slouken@1907
   352
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1907
   353
        renderer->info.flags |= SDL_Renderer_PresentVSync;
slouken@1907
   354
    }
slouken@1907
   355
slouken@1903
   356
    /* Set up parameters for rendering */
slouken@1904
   357
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1904
   358
    IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
slouken@1903
   359
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   360
                                    D3DCULL_NONE);
slouken@1904
   361
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1904
   362
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1904
   363
                                    TRUE);
slouken@1904
   364
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1904
   365
                                    D3DBLEND_SRCALPHA);
slouken@1904
   366
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1904
   367
                                    D3DBLEND_INVSRCALPHA);
slouken@1903
   368
slouken@1895
   369
    return renderer;
slouken@1895
   370
}
slouken@1895
   371
slouken@1895
   372
static int
slouken@1913
   373
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   374
{
slouken@1913
   375
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   376
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   377
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1913
   378
    D3D_TextureData *data;
slouken@1903
   379
    D3DPOOL pool;
slouken@1903
   380
    HRESULT result;
slouken@1895
   381
slouken@1913
   382
    data = (D3D_TextureData *) SDL_malloc(sizeof(*data));
slouken@1895
   383
    if (!data) {
slouken@1895
   384
        SDL_OutOfMemory();
slouken@1895
   385
        return -1;
slouken@1895
   386
    }
slouken@1895
   387
    SDL_zerop(data);
slouken@1895
   388
slouken@1895
   389
    texture->driverdata = data;
slouken@1895
   390
slouken@1903
   391
    if (texture->access == SDL_TextureAccess_Local) {
slouken@1903
   392
        pool = D3DPOOL_MANAGED;
slouken@1903
   393
    } else {
slouken@1903
   394
        pool = D3DPOOL_DEFAULT;
slouken@1903
   395
    }
slouken@1903
   396
    result =
slouken@1903
   397
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   398
                                       texture->h, 1, 0,
slouken@1903
   399
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   400
                                       pool, &data->texture, NULL);
slouken@1903
   401
    if (FAILED(result)) {
slouken@1903
   402
        SDL_free(data);
slouken@1903
   403
        D3D_SetError("CreateTexture()", result);
slouken@1903
   404
        return -1;
slouken@1903
   405
    }
slouken@1903
   406
slouken@1895
   407
    return 0;
slouken@1895
   408
}
slouken@1895
   409
slouken@1895
   410
static int
slouken@1913
   411
D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   412
                      const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   413
{
slouken@1913
   414
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   415
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   416
slouken@1903
   417
    return 0;
slouken@1895
   418
}
slouken@1895
   419
slouken@1895
   420
static int
slouken@1913
   421
D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   422
                      SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   423
{
slouken@1913
   424
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   425
slouken@1903
   426
    return 0;
slouken@1895
   427
}
slouken@1895
   428
slouken@1895
   429
static int
slouken@1913
   430
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   431
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   432
{
slouken@1913
   433
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   434
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1903
   435
    IDirect3DTexture9 *temp;
slouken@1903
   436
    RECT d3drect;
slouken@1903
   437
    D3DLOCKED_RECT locked;
slouken@1903
   438
    const Uint8 *src;
slouken@1903
   439
    Uint8 *dst;
slouken@1903
   440
    int row, length;
slouken@1903
   441
    HRESULT result;
slouken@1895
   442
slouken@1903
   443
    result =
slouken@1903
   444
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   445
                                       texture->h, 1, 0,
slouken@1903
   446
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   447
                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@1903
   448
    if (FAILED(result)) {
slouken@1903
   449
        D3D_SetError("CreateTexture()", result);
slouken@1903
   450
        return -1;
slouken@1903
   451
    }
slouken@1895
   452
slouken@1903
   453
    d3drect.left = rect->x;
slouken@1903
   454
    d3drect.right = rect->x + rect->w;
slouken@1903
   455
    d3drect.top = rect->y;
slouken@1903
   456
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   457
slouken@1903
   458
    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@1903
   459
    if (FAILED(result)) {
slouken@1903
   460
        IDirect3DTexture9_Release(temp);
slouken@1903
   461
        D3D_SetError("LockRect()", result);
slouken@1903
   462
        return -1;
slouken@1895
   463
    }
slouken@1903
   464
slouken@1903
   465
    src = pixels;
slouken@1903
   466
    dst = locked.pBits;
slouken@1903
   467
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1903
   468
    for (row = 0; row < rect->h; ++row) {
slouken@1903
   469
        SDL_memcpy(dst, src, length);
slouken@1903
   470
        src += pitch;
slouken@1903
   471
        dst += locked.Pitch;
slouken@1903
   472
    }
slouken@1903
   473
    IDirect3DTexture9_UnlockRect(temp, 0);
slouken@1903
   474
slouken@1903
   475
    result =
slouken@1903
   476
        IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@1903
   477
                                       (IDirect3DBaseTexture9 *) temp,
slouken@1903
   478
                                       (IDirect3DBaseTexture9 *) data->
slouken@1903
   479
                                       texture);
slouken@1903
   480
    IDirect3DTexture9_Release(temp);
slouken@1903
   481
    if (FAILED(result)) {
slouken@1903
   482
        D3D_SetError("UpdateTexture()", result);
slouken@1903
   483
        return -1;
slouken@1903
   484
    }
slouken@1903
   485
    return 0;
slouken@1895
   486
}
slouken@1895
   487
slouken@1895
   488
static int
slouken@1913
   489
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   490
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   491
                int *pitch)
slouken@1895
   492
{
slouken@1913
   493
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   494
    RECT d3drect;
slouken@1903
   495
    D3DLOCKED_RECT locked;
slouken@1903
   496
    HRESULT result;
slouken@1895
   497
slouken@1903
   498
    if (texture->access != SDL_TextureAccess_Local) {
slouken@1903
   499
        SDL_SetError("Can't lock remote video memory");
slouken@1900
   500
        return -1;
slouken@1895
   501
    }
slouken@1903
   502
slouken@1903
   503
    d3drect.left = rect->x;
slouken@1903
   504
    d3drect.right = rect->x + rect->w;
slouken@1903
   505
    d3drect.top = rect->y;
slouken@1903
   506
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   507
slouken@1903
   508
    result =
slouken@1903
   509
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@1903
   510
                                   markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
slouken@1903
   511
    if (FAILED(result)) {
slouken@1903
   512
        D3D_SetError("LockRect()", result);
slouken@1903
   513
        return -1;
slouken@1903
   514
    }
slouken@1903
   515
    *pixels = locked.pBits;
slouken@1903
   516
    *pitch = locked.Pitch;
slouken@1903
   517
    return 0;
slouken@1895
   518
}
slouken@1895
   519
slouken@1895
   520
static void
slouken@1913
   521
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   522
{
slouken@1913
   523
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   524
slouken@1903
   525
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   526
}
slouken@1895
   527
slouken@1895
   528
static void
slouken@1913
   529
D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   530
                 const SDL_Rect * rects)
slouken@1895
   531
{
slouken@1913
   532
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   533
    RECT d3drect;
slouken@1903
   534
    int i;
slouken@1903
   535
slouken@1903
   536
    for (i = 0; i < numrects; ++i) {
slouken@1903
   537
        const SDL_Rect *rect = &rects[i];
slouken@1903
   538
slouken@1903
   539
        d3drect.left = rect->x;
slouken@1903
   540
        d3drect.right = rect->x + rect->w;
slouken@1903
   541
        d3drect.top = rect->y;
slouken@1903
   542
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   543
slouken@1903
   544
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   545
    }
slouken@1895
   546
}
slouken@1895
   547
slouken@1895
   548
static int
slouken@1913
   549
D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1895
   550
{
slouken@1913
   551
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1901
   552
    D3DRECT d3drect;
slouken@1900
   553
    HRESULT result;
slouken@1895
   554
slouken@1900
   555
    if (data->beginScene) {
slouken@1900
   556
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   557
        data->beginScene = SDL_FALSE;
slouken@1900
   558
    }
slouken@1895
   559
slouken@1901
   560
    d3drect.x1 = rect->x;
slouken@1903
   561
    d3drect.x2 = rect->x + rect->w;
slouken@1901
   562
    d3drect.y1 = rect->y;
slouken@1903
   563
    d3drect.y2 = rect->y + rect->h;
slouken@1901
   564
slouken@1903
   565
    result =
slouken@1903
   566
        IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
slouken@1903
   567
                               (D3DCOLOR) color, 1.0f, 0);
slouken@1900
   568
    if (FAILED(result)) {
slouken@1900
   569
        D3D_SetError("Clear()", result);
slouken@1900
   570
        return -1;
slouken@1900
   571
    }
slouken@1895
   572
    return 0;
slouken@1895
   573
}
slouken@1895
   574
slouken@1895
   575
static int
slouken@1913
   576
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   577
               const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1913
   578
               int blendMode, int scaleMode)
slouken@1895
   579
{
slouken@1913
   580
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   581
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@1903
   582
    float minx, miny, maxx, maxy;
slouken@1904
   583
    float minu, maxu, minv, maxv;
slouken@1903
   584
    Vertex vertices[4];
slouken@1903
   585
    HRESULT result;
slouken@1895
   586
slouken@1900
   587
    if (data->beginScene) {
slouken@1900
   588
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   589
        data->beginScene = SDL_FALSE;
slouken@1900
   590
    }
slouken@1903
   591
slouken@1904
   592
    minx = (float) dstrect->x - 0.5f;
slouken@1904
   593
    miny = (float) dstrect->y - 0.5f;
slouken@1904
   594
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@1904
   595
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
   596
slouken@1904
   597
    minu = (float) srcrect->x / texture->w;
slouken@1904
   598
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
   599
    minv = (float) srcrect->y / texture->h;
slouken@1904
   600
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
   601
slouken@1903
   602
    vertices[0].x = minx;
slouken@1903
   603
    vertices[0].y = miny;
slouken@1903
   604
    vertices[0].z = 0.0f;
slouken@1904
   605
    vertices[0].rhw = 1.0f;
slouken@1904
   606
    vertices[0].u = minu;
slouken@1904
   607
    vertices[0].v = minv;
slouken@1904
   608
slouken@1903
   609
    vertices[1].x = maxx;
slouken@1903
   610
    vertices[1].y = miny;
slouken@1903
   611
    vertices[1].z = 0.0f;
slouken@1904
   612
    vertices[1].rhw = 1.0f;
slouken@1904
   613
    vertices[1].u = maxu;
slouken@1904
   614
    vertices[1].v = minv;
slouken@1904
   615
slouken@1903
   616
    vertices[2].x = maxx;
slouken@1903
   617
    vertices[2].y = maxy;
slouken@1903
   618
    vertices[2].z = 0.0f;
slouken@1904
   619
    vertices[2].rhw = 1.0f;
slouken@1904
   620
    vertices[2].u = maxu;
slouken@1904
   621
    vertices[2].v = maxv;
slouken@1904
   622
slouken@1903
   623
    vertices[3].x = minx;
slouken@1903
   624
    vertices[3].y = maxy;
slouken@1903
   625
    vertices[3].z = 0.0f;
slouken@1904
   626
    vertices[3].rhw = 1.0f;
slouken@1904
   627
    vertices[3].u = minu;
slouken@1904
   628
    vertices[3].v = maxv;
slouken@1903
   629
slouken@1903
   630
    result =
slouken@1903
   631
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@1903
   632
                                    (IDirect3DBaseTexture9 *) texturedata->
slouken@1903
   633
                                    texture);
slouken@1903
   634
    if (FAILED(result)) {
slouken@1903
   635
        D3D_SetError("SetTexture()", result);
slouken@1903
   636
        return -1;
slouken@1903
   637
    }
slouken@1903
   638
    result =
slouken@1903
   639
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
   640
                                         vertices, sizeof(*vertices));
slouken@1903
   641
    if (FAILED(result)) {
slouken@1903
   642
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
   643
        return -1;
slouken@1903
   644
    }
slouken@1895
   645
    return 0;
slouken@1895
   646
}
slouken@1895
   647
slouken@1895
   648
static void
slouken@1913
   649
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   650
{
slouken@1913
   651
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
   652
    HRESULT result;
slouken@1900
   653
slouken@1900
   654
    if (!data->beginScene) {
slouken@1900
   655
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
   656
        data->beginScene = SDL_TRUE;
slouken@1900
   657
    }
slouken@1900
   658
slouken@1900
   659
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
   660
    if (FAILED(result)) {
slouken@1900
   661
        D3D_SetError("Present()", result);
slouken@1900
   662
    }
slouken@1895
   663
}
slouken@1895
   664
slouken@1895
   665
static void
slouken@1913
   666
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   667
{
slouken@1913
   668
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   669
slouken@1895
   670
    if (!data) {
slouken@1895
   671
        return;
slouken@1895
   672
    }
slouken@1903
   673
    if (data->texture) {
slouken@1903
   674
        IDirect3DTexture9_Release(data->texture);
slouken@1903
   675
    }
slouken@1895
   676
    SDL_free(data);
slouken@1895
   677
    texture->driverdata = NULL;
slouken@1895
   678
}
slouken@1895
   679
slouken@1895
   680
void
slouken@1913
   681
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   682
{
slouken@1913
   683
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   684
slouken@1895
   685
    if (data) {
slouken@1900
   686
        if (data->device) {
slouken@1900
   687
            IDirect3DDevice9_Release(data->device);
slouken@1900
   688
        }
slouken@1895
   689
        SDL_free(data);
slouken@1895
   690
    }
slouken@1895
   691
    SDL_free(renderer);
slouken@1895
   692
}
slouken@1895
   693
slouken@1895
   694
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
   695
slouken@1895
   696
/* vi: set ts=4 sw=4 expandtab: */