src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 22 Jul 2006 08:33:18 +0000
changeset 1920 8a162bfdc838
parent 1918 092bd3a019c5
child 1921 f3399f779a1d
permissions -rw-r--r--
Convert SDL_malloc to SDL_calloc if appropriate, slightly faster on operating systems which map the zero page for memory allocations.
OpenGL renderer in progress
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@1918
    65
      SDL_Renderer_PresentDiscard | SDL_Renderer_PresentVSync |
slouken@1918
    66
      SDL_Renderer_Accelerated),
slouken@1918
    67
     (SDL_TextureBlendMode_None | SDL_TextureBlendMode_Mask |
slouken@1918
    68
      SDL_TextureBlendMode_Blend | SDL_TextureBlendMode_Add |
slouken@1918
    69
      SDL_TextureBlendMode_Mod),
slouken@1918
    70
     (SDL_TextureScaleMode_None | SDL_TextureScaleMode_Fast |
slouken@1918
    71
      SDL_TextureScaleMode_Best),
slouken@1903
    72
     12,
slouken@1895
    73
     {
slouken@1895
    74
      SDL_PixelFormat_Index8,
slouken@1903
    75
      SDL_PixelFormat_RGB332,
slouken@1903
    76
      SDL_PixelFormat_RGB444,
slouken@1895
    77
      SDL_PixelFormat_RGB555,
slouken@1903
    78
      SDL_PixelFormat_ARGB4444,
slouken@1903
    79
      SDL_PixelFormat_ARGB1555,
slouken@1895
    80
      SDL_PixelFormat_RGB565,
slouken@1895
    81
      SDL_PixelFormat_RGB888,
slouken@1895
    82
      SDL_PixelFormat_ARGB8888,
slouken@1903
    83
      SDL_PixelFormat_ARGB2101010,
slouken@1903
    84
      SDL_PixelFormat_UYVY,
slouken@1903
    85
      SDL_PixelFormat_YUY2},
slouken@1895
    86
     0,
slouken@1895
    87
     0}
slouken@1895
    88
};
slouken@1895
    89
slouken@1895
    90
typedef struct
slouken@1895
    91
{
slouken@1895
    92
    IDirect3DDevice9 *device;
slouken@1900
    93
    SDL_bool beginScene;
slouken@1913
    94
} D3D_RenderData;
slouken@1895
    95
slouken@1895
    96
typedef struct
slouken@1895
    97
{
slouken@1903
    98
    IDirect3DTexture9 *texture;
slouken@1913
    99
} D3D_TextureData;
slouken@1895
   100
slouken@1903
   101
typedef struct
slouken@1903
   102
{
slouken@1903
   103
    float x, y, z;
slouken@1904
   104
    float rhw;
slouken@1904
   105
    float u, v;
slouken@1903
   106
} Vertex;
slouken@1903
   107
slouken@1895
   108
static void
slouken@1900
   109
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   110
{
slouken@1900
   111
    const char *error;
slouken@1900
   112
slouken@1900
   113
    switch (result) {
slouken@1900
   114
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   115
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   116
        break;
slouken@1900
   117
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   118
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   119
        break;
slouken@1900
   120
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   121
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   122
        break;
slouken@1900
   123
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   124
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   125
        break;
slouken@1900
   126
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   127
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   128
        break;
slouken@1900
   129
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   130
        error = "TOOMANYOPERATIONS";
slouken@1900
   131
        break;
slouken@1900
   132
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   133
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   134
        break;
slouken@1900
   135
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   136
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   137
        break;
slouken@1900
   138
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   139
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   140
        break;
slouken@1900
   141
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   142
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   143
        break;
slouken@1900
   144
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   145
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   146
        break;
slouken@1900
   147
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   148
        error = "DRIVERINTERNALERROR";
slouken@1900
   149
        break;
slouken@1900
   150
    case D3DERR_NOTFOUND:
slouken@1900
   151
        error = "NOTFOUND";
slouken@1900
   152
        break;
slouken@1900
   153
    case D3DERR_MOREDATA:
slouken@1900
   154
        error = "MOREDATA";
slouken@1900
   155
        break;
slouken@1900
   156
    case D3DERR_DEVICELOST:
slouken@1900
   157
        error = "DEVICELOST";
slouken@1900
   158
        break;
slouken@1900
   159
    case D3DERR_DEVICENOTRESET:
slouken@1900
   160
        error = "DEVICENOTRESET";
slouken@1900
   161
        break;
slouken@1900
   162
    case D3DERR_NOTAVAILABLE:
slouken@1900
   163
        error = "NOTAVAILABLE";
slouken@1900
   164
        break;
slouken@1900
   165
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   166
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   167
        break;
slouken@1900
   168
    case D3DERR_INVALIDDEVICE:
slouken@1900
   169
        error = "INVALIDDEVICE";
slouken@1900
   170
        break;
slouken@1900
   171
    case D3DERR_INVALIDCALL:
slouken@1900
   172
        error = "INVALIDCALL";
slouken@1900
   173
        break;
slouken@1900
   174
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   175
        error = "DRIVERINVALIDCALL";
slouken@1900
   176
        break;
slouken@1900
   177
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   178
        error = "WASSTILLDRAWING";
slouken@1900
   179
        break;
slouken@1900
   180
    default:
slouken@1900
   181
        error = "UNKNOWN";
slouken@1900
   182
        break;
slouken@1900
   183
    }
slouken@1900
   184
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   185
}
slouken@1900
   186
slouken@1903
   187
static D3DFORMAT
slouken@1903
   188
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   189
{
slouken@1903
   190
    switch (format) {
slouken@1903
   191
    case SDL_PixelFormat_Index8:
slouken@1903
   192
        return D3DFMT_P8;
slouken@1903
   193
    case SDL_PixelFormat_RGB332:
slouken@1903
   194
        return D3DFMT_R3G3B2;
slouken@1903
   195
    case SDL_PixelFormat_RGB444:
slouken@1903
   196
        return D3DFMT_X4R4G4B4;
slouken@1903
   197
    case SDL_PixelFormat_RGB555:
slouken@1903
   198
        return D3DFMT_X1R5G5B5;
slouken@1903
   199
    case SDL_PixelFormat_ARGB4444:
slouken@1903
   200
        return D3DFMT_A4R4G4B4;
slouken@1903
   201
    case SDL_PixelFormat_ARGB1555:
slouken@1903
   202
        return D3DFMT_A1R5G5B5;
slouken@1903
   203
    case SDL_PixelFormat_RGB565:
slouken@1903
   204
        return D3DFMT_R5G6B5;
slouken@1903
   205
    case SDL_PixelFormat_RGB888:
slouken@1903
   206
        return D3DFMT_X8R8G8B8;
slouken@1903
   207
    case SDL_PixelFormat_ARGB8888:
slouken@1903
   208
        return D3DFMT_A8R8G8B8;
slouken@1903
   209
    case SDL_PixelFormat_ARGB2101010:
slouken@1903
   210
        return D3DFMT_A2R10G10B10;
slouken@1903
   211
    case SDL_PixelFormat_UYVY:
slouken@1903
   212
        return D3DFMT_UYVY;
slouken@1903
   213
    case SDL_PixelFormat_YUY2:
slouken@1903
   214
        return D3DFMT_YUY2;
slouken@1903
   215
    default:
slouken@1903
   216
        return D3DFMT_UNKNOWN;
slouken@1903
   217
    }
slouken@1895
   218
}
slouken@1895
   219
slouken@1895
   220
void
slouken@1895
   221
D3D_AddRenderDriver(_THIS)
slouken@1895
   222
{
slouken@1895
   223
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1895
   224
slouken@1895
   225
    if (data->d3d) {
slouken@1913
   226
        SDL_AddRenderDriver(0, &D3D_RenderDriver);
slouken@1895
   227
    }
slouken@1895
   228
}
slouken@1895
   229
slouken@1895
   230
SDL_Renderer *
slouken@1913
   231
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   232
{
slouken@1895
   233
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   234
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1895
   235
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   236
    SDL_Renderer *renderer;
slouken@1913
   237
    D3D_RenderData *data;
slouken@1900
   238
    HRESULT result;
slouken@1900
   239
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   240
    IDirect3DSwapChain9 *chain;
slouken@1895
   241
slouken@1920
   242
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   243
    if (!renderer) {
slouken@1895
   244
        SDL_OutOfMemory();
slouken@1895
   245
        return NULL;
slouken@1895
   246
    }
slouken@1895
   247
slouken@1920
   248
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   249
    if (!data) {
slouken@1913
   250
        D3D_DestroyRenderer(renderer);
slouken@1895
   251
        SDL_OutOfMemory();
slouken@1895
   252
        return NULL;
slouken@1895
   253
    }
slouken@1895
   254
slouken@1913
   255
    renderer->CreateTexture = D3D_CreateTexture;
slouken@1913
   256
    renderer->SetTexturePalette = D3D_SetTexturePalette;
slouken@1913
   257
    renderer->GetTexturePalette = D3D_GetTexturePalette;
slouken@1913
   258
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   259
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   260
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@1913
   261
    renderer->DirtyTexture = D3D_DirtyTexture;
slouken@1913
   262
    renderer->RenderFill = D3D_RenderFill;
slouken@1913
   263
    renderer->RenderCopy = D3D_RenderCopy;
slouken@1913
   264
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   265
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   266
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   267
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   268
    renderer->window = window->id;
slouken@1895
   269
    renderer->driverdata = data;
slouken@1895
   270
slouken@1907
   271
    renderer->info.flags = SDL_Renderer_Accelerated;
slouken@1895
   272
slouken@1900
   273
    SDL_zero(pparams);
slouken@1900
   274
    pparams.BackBufferWidth = window->w;
slouken@1900
   275
    pparams.BackBufferHeight = window->h;
slouken@1903
   276
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   277
        pparams.BackBufferFormat =
slouken@1903
   278
            PixelFormatToD3DFMT(display->fullscreen_mode->format);
slouken@1903
   279
    } else {
slouken@1903
   280
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   281
    }
slouken@1900
   282
    if (flags & SDL_Renderer_PresentFlip2) {
slouken@1900
   283
        pparams.BackBufferCount = 2;
slouken@1900
   284
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1900
   285
    } else if (flags & SDL_Renderer_PresentFlip3) {
slouken@1900
   286
        pparams.BackBufferCount = 3;
slouken@1900
   287
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1900
   288
    } else if (flags & SDL_Renderer_PresentCopy) {
slouken@1900
   289
        pparams.BackBufferCount = 1;
slouken@1900
   290
        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
slouken@1900
   291
    } else {
slouken@1900
   292
        pparams.BackBufferCount = 1;
slouken@1900
   293
        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@1900
   294
    }
slouken@1900
   295
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   296
        pparams.Windowed = FALSE;
slouken@1903
   297
        pparams.FullScreen_RefreshRateInHz =
slouken@1903
   298
            display->fullscreen_mode->refresh_rate;
slouken@1900
   299
    } else {
slouken@1900
   300
        pparams.Windowed = TRUE;
slouken@1903
   301
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   302
    }
slouken@1907
   303
    if (flags & SDL_Renderer_PresentVSync) {
slouken@1907
   304
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   305
    } else {
slouken@1907
   306
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   307
    }
slouken@1900
   308
slouken@1900
   309
    result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
slouken@1900
   310
                                     D3DDEVTYPE_HAL,
slouken@1900
   311
                                     windowdata->hwnd,
slouken@1900
   312
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   313
                                     &pparams, &data->device);
slouken@1900
   314
    if (FAILED(result)) {
slouken@1913
   315
        D3D_DestroyRenderer(renderer);
slouken@1900
   316
        D3D_SetError("CreateDevice()", result);
slouken@1900
   317
        return NULL;
slouken@1900
   318
    }
slouken@1900
   319
    data->beginScene = SDL_TRUE;
slouken@1900
   320
slouken@1907
   321
    /* Get presentation parameters to fill info */
slouken@1907
   322
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   323
    if (FAILED(result)) {
slouken@1913
   324
        D3D_DestroyRenderer(renderer);
slouken@1907
   325
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   326
        return NULL;
slouken@1907
   327
    }
slouken@1907
   328
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   329
    if (FAILED(result)) {
slouken@1907
   330
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   331
        D3D_DestroyRenderer(renderer);
slouken@1907
   332
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   333
        return NULL;
slouken@1907
   334
    }
slouken@1907
   335
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   336
    switch (pparams.SwapEffect) {
slouken@1907
   337
    case D3DSWAPEFFECT_COPY:
slouken@1907
   338
        renderer->info.flags |= SDL_Renderer_PresentCopy;
slouken@1907
   339
        break;
slouken@1907
   340
    case D3DSWAPEFFECT_FLIP:
slouken@1907
   341
        switch (pparams.BackBufferCount) {
slouken@1907
   342
        case 2:
slouken@1907
   343
            renderer->info.flags |= SDL_Renderer_PresentFlip2;
slouken@1907
   344
            break;
slouken@1907
   345
        case 3:
slouken@1907
   346
            renderer->info.flags |= SDL_Renderer_PresentFlip3;
slouken@1907
   347
            break;
slouken@1907
   348
        }
slouken@1907
   349
        break;
slouken@1907
   350
    case D3DSWAPEFFECT_DISCARD:
slouken@1907
   351
        renderer->info.flags |= SDL_Renderer_PresentDiscard;
slouken@1907
   352
        break;
slouken@1907
   353
    }
slouken@1907
   354
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1907
   355
        renderer->info.flags |= SDL_Renderer_PresentVSync;
slouken@1907
   356
    }
slouken@1907
   357
slouken@1918
   358
    /* FIXME: Query maximum texture size */
slouken@1918
   359
slouken@1903
   360
    /* Set up parameters for rendering */
slouken@1904
   361
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1904
   362
    IDirect3DDevice9_SetFVF(data->device, D3DFVF_XYZRHW | D3DFVF_TEX1);
slouken@1903
   363
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   364
                                    D3DCULL_NONE);
slouken@1904
   365
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1903
   366
slouken@1895
   367
    return renderer;
slouken@1895
   368
}
slouken@1895
   369
slouken@1895
   370
static int
slouken@1913
   371
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   372
{
slouken@1913
   373
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   374
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   375
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1913
   376
    D3D_TextureData *data;
slouken@1903
   377
    D3DPOOL pool;
slouken@1903
   378
    HRESULT result;
slouken@1895
   379
slouken@1920
   380
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   381
    if (!data) {
slouken@1895
   382
        SDL_OutOfMemory();
slouken@1895
   383
        return -1;
slouken@1895
   384
    }
slouken@1895
   385
slouken@1895
   386
    texture->driverdata = data;
slouken@1895
   387
slouken@1903
   388
    if (texture->access == SDL_TextureAccess_Local) {
slouken@1903
   389
        pool = D3DPOOL_MANAGED;
slouken@1903
   390
    } else {
slouken@1903
   391
        pool = D3DPOOL_DEFAULT;
slouken@1903
   392
    }
slouken@1903
   393
    result =
slouken@1903
   394
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   395
                                       texture->h, 1, 0,
slouken@1903
   396
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   397
                                       pool, &data->texture, NULL);
slouken@1903
   398
    if (FAILED(result)) {
slouken@1903
   399
        SDL_free(data);
slouken@1903
   400
        D3D_SetError("CreateTexture()", result);
slouken@1903
   401
        return -1;
slouken@1903
   402
    }
slouken@1903
   403
slouken@1895
   404
    return 0;
slouken@1895
   405
}
slouken@1895
   406
slouken@1895
   407
static int
slouken@1913
   408
D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   409
                      const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   410
{
slouken@1913
   411
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   412
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   413
slouken@1903
   414
    return 0;
slouken@1895
   415
}
slouken@1895
   416
slouken@1895
   417
static int
slouken@1913
   418
D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   419
                      SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   420
{
slouken@1913
   421
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   422
slouken@1903
   423
    return 0;
slouken@1895
   424
}
slouken@1895
   425
slouken@1895
   426
static int
slouken@1913
   427
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   428
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   429
{
slouken@1913
   430
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   431
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1903
   432
    IDirect3DTexture9 *temp;
slouken@1903
   433
    RECT d3drect;
slouken@1903
   434
    D3DLOCKED_RECT locked;
slouken@1903
   435
    const Uint8 *src;
slouken@1903
   436
    Uint8 *dst;
slouken@1903
   437
    int row, length;
slouken@1903
   438
    HRESULT result;
slouken@1895
   439
slouken@1903
   440
    result =
slouken@1903
   441
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   442
                                       texture->h, 1, 0,
slouken@1903
   443
                                       PixelFormatToD3DFMT(texture->format),
slouken@1903
   444
                                       D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@1903
   445
    if (FAILED(result)) {
slouken@1903
   446
        D3D_SetError("CreateTexture()", result);
slouken@1903
   447
        return -1;
slouken@1903
   448
    }
slouken@1895
   449
slouken@1903
   450
    d3drect.left = rect->x;
slouken@1903
   451
    d3drect.right = rect->x + rect->w;
slouken@1903
   452
    d3drect.top = rect->y;
slouken@1903
   453
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   454
slouken@1903
   455
    result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@1903
   456
    if (FAILED(result)) {
slouken@1903
   457
        IDirect3DTexture9_Release(temp);
slouken@1903
   458
        D3D_SetError("LockRect()", result);
slouken@1903
   459
        return -1;
slouken@1895
   460
    }
slouken@1903
   461
slouken@1903
   462
    src = pixels;
slouken@1903
   463
    dst = locked.pBits;
slouken@1903
   464
    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1903
   465
    for (row = 0; row < rect->h; ++row) {
slouken@1903
   466
        SDL_memcpy(dst, src, length);
slouken@1903
   467
        src += pitch;
slouken@1903
   468
        dst += locked.Pitch;
slouken@1903
   469
    }
slouken@1903
   470
    IDirect3DTexture9_UnlockRect(temp, 0);
slouken@1903
   471
slouken@1903
   472
    result =
slouken@1903
   473
        IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@1903
   474
                                       (IDirect3DBaseTexture9 *) temp,
slouken@1903
   475
                                       (IDirect3DBaseTexture9 *) data->
slouken@1903
   476
                                       texture);
slouken@1903
   477
    IDirect3DTexture9_Release(temp);
slouken@1903
   478
    if (FAILED(result)) {
slouken@1903
   479
        D3D_SetError("UpdateTexture()", result);
slouken@1903
   480
        return -1;
slouken@1903
   481
    }
slouken@1903
   482
    return 0;
slouken@1895
   483
}
slouken@1895
   484
slouken@1895
   485
static int
slouken@1913
   486
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   487
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   488
                int *pitch)
slouken@1895
   489
{
slouken@1913
   490
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   491
    RECT d3drect;
slouken@1903
   492
    D3DLOCKED_RECT locked;
slouken@1903
   493
    HRESULT result;
slouken@1895
   494
slouken@1903
   495
    if (texture->access != SDL_TextureAccess_Local) {
slouken@1903
   496
        SDL_SetError("Can't lock remote video memory");
slouken@1900
   497
        return -1;
slouken@1895
   498
    }
slouken@1903
   499
slouken@1903
   500
    d3drect.left = rect->x;
slouken@1903
   501
    d3drect.right = rect->x + rect->w;
slouken@1903
   502
    d3drect.top = rect->y;
slouken@1903
   503
    d3drect.bottom = rect->y + rect->h;
slouken@1903
   504
slouken@1903
   505
    result =
slouken@1903
   506
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@1903
   507
                                   markDirty ? 0 : D3DLOCK_NO_DIRTY_UPDATE);
slouken@1903
   508
    if (FAILED(result)) {
slouken@1903
   509
        D3D_SetError("LockRect()", result);
slouken@1903
   510
        return -1;
slouken@1903
   511
    }
slouken@1903
   512
    *pixels = locked.pBits;
slouken@1903
   513
    *pitch = locked.Pitch;
slouken@1903
   514
    return 0;
slouken@1895
   515
}
slouken@1895
   516
slouken@1895
   517
static void
slouken@1913
   518
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   519
{
slouken@1913
   520
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   521
slouken@1903
   522
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@1895
   523
}
slouken@1895
   524
slouken@1895
   525
static void
slouken@1913
   526
D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   527
                 const SDL_Rect * rects)
slouken@1895
   528
{
slouken@1913
   529
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   530
    RECT d3drect;
slouken@1903
   531
    int i;
slouken@1903
   532
slouken@1903
   533
    for (i = 0; i < numrects; ++i) {
slouken@1903
   534
        const SDL_Rect *rect = &rects[i];
slouken@1903
   535
slouken@1903
   536
        d3drect.left = rect->x;
slouken@1903
   537
        d3drect.right = rect->x + rect->w;
slouken@1903
   538
        d3drect.top = rect->y;
slouken@1903
   539
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   540
slouken@1903
   541
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   542
    }
slouken@1895
   543
}
slouken@1895
   544
slouken@1895
   545
static int
slouken@1913
   546
D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1895
   547
{
slouken@1913
   548
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1901
   549
    D3DRECT d3drect;
slouken@1900
   550
    HRESULT result;
slouken@1895
   551
slouken@1900
   552
    if (data->beginScene) {
slouken@1900
   553
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   554
        data->beginScene = SDL_FALSE;
slouken@1900
   555
    }
slouken@1895
   556
slouken@1901
   557
    d3drect.x1 = rect->x;
slouken@1903
   558
    d3drect.x2 = rect->x + rect->w;
slouken@1901
   559
    d3drect.y1 = rect->y;
slouken@1903
   560
    d3drect.y2 = rect->y + rect->h;
slouken@1901
   561
slouken@1903
   562
    result =
slouken@1903
   563
        IDirect3DDevice9_Clear(data->device, 1, &d3drect, D3DCLEAR_TARGET,
slouken@1903
   564
                               (D3DCOLOR) color, 1.0f, 0);
slouken@1900
   565
    if (FAILED(result)) {
slouken@1900
   566
        D3D_SetError("Clear()", result);
slouken@1900
   567
        return -1;
slouken@1900
   568
    }
slouken@1895
   569
    return 0;
slouken@1895
   570
}
slouken@1895
   571
slouken@1895
   572
static int
slouken@1913
   573
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   574
               const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1913
   575
               int blendMode, int scaleMode)
slouken@1895
   576
{
slouken@1913
   577
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   578
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@1903
   579
    float minx, miny, maxx, maxy;
slouken@1904
   580
    float minu, maxu, minv, maxv;
slouken@1903
   581
    Vertex vertices[4];
slouken@1903
   582
    HRESULT result;
slouken@1895
   583
slouken@1900
   584
    if (data->beginScene) {
slouken@1900
   585
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   586
        data->beginScene = SDL_FALSE;
slouken@1900
   587
    }
slouken@1903
   588
slouken@1904
   589
    minx = (float) dstrect->x - 0.5f;
slouken@1904
   590
    miny = (float) dstrect->y - 0.5f;
slouken@1904
   591
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@1904
   592
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
   593
slouken@1904
   594
    minu = (float) srcrect->x / texture->w;
slouken@1904
   595
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
   596
    minv = (float) srcrect->y / texture->h;
slouken@1904
   597
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
   598
slouken@1903
   599
    vertices[0].x = minx;
slouken@1903
   600
    vertices[0].y = miny;
slouken@1903
   601
    vertices[0].z = 0.0f;
slouken@1904
   602
    vertices[0].rhw = 1.0f;
slouken@1904
   603
    vertices[0].u = minu;
slouken@1904
   604
    vertices[0].v = minv;
slouken@1904
   605
slouken@1903
   606
    vertices[1].x = maxx;
slouken@1903
   607
    vertices[1].y = miny;
slouken@1903
   608
    vertices[1].z = 0.0f;
slouken@1904
   609
    vertices[1].rhw = 1.0f;
slouken@1904
   610
    vertices[1].u = maxu;
slouken@1904
   611
    vertices[1].v = minv;
slouken@1904
   612
slouken@1903
   613
    vertices[2].x = maxx;
slouken@1903
   614
    vertices[2].y = maxy;
slouken@1903
   615
    vertices[2].z = 0.0f;
slouken@1904
   616
    vertices[2].rhw = 1.0f;
slouken@1904
   617
    vertices[2].u = maxu;
slouken@1904
   618
    vertices[2].v = maxv;
slouken@1904
   619
slouken@1903
   620
    vertices[3].x = minx;
slouken@1903
   621
    vertices[3].y = maxy;
slouken@1903
   622
    vertices[3].z = 0.0f;
slouken@1904
   623
    vertices[3].rhw = 1.0f;
slouken@1904
   624
    vertices[3].u = minu;
slouken@1904
   625
    vertices[3].v = maxv;
slouken@1903
   626
slouken@1916
   627
    switch (blendMode) {
slouken@1916
   628
    case SDL_TextureBlendMode_None:
slouken@1916
   629
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   630
                                        FALSE);
slouken@1916
   631
        break;
slouken@1916
   632
    case SDL_TextureBlendMode_Mask:
slouken@1916
   633
    case SDL_TextureBlendMode_Blend:
slouken@1916
   634
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   635
                                        TRUE);
slouken@1916
   636
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1916
   637
                                        D3DBLEND_SRCALPHA);
slouken@1916
   638
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1916
   639
                                        D3DBLEND_INVSRCALPHA);
slouken@1916
   640
        break;
slouken@1916
   641
    case SDL_TextureBlendMode_Add:
slouken@1916
   642
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   643
                                        TRUE);
slouken@1916
   644
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1916
   645
                                        D3DBLEND_SRCALPHA);
slouken@1916
   646
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1916
   647
                                        D3DBLEND_ONE);
slouken@1916
   648
        break;
slouken@1916
   649
    case SDL_TextureBlendMode_Mod:
slouken@1916
   650
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@1916
   651
                                        TRUE);
slouken@1916
   652
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@1916
   653
                                        D3DBLEND_ZERO);
slouken@1916
   654
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@1916
   655
                                        D3DBLEND_SRCCOLOR);
slouken@1916
   656
        break;
slouken@1916
   657
    }
slouken@1916
   658
slouken@1917
   659
    switch (scaleMode) {
slouken@1917
   660
    case SDL_TextureScaleMode_None:
slouken@1917
   661
    case SDL_TextureScaleMode_Fast:
slouken@1917
   662
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   663
                                         D3DTEXF_POINT);
slouken@1917
   664
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   665
                                         D3DTEXF_POINT);
slouken@1917
   666
        break;
slouken@1917
   667
    case SDL_TextureScaleMode_Slow:
slouken@1917
   668
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   669
                                         D3DTEXF_LINEAR);
slouken@1917
   670
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   671
                                         D3DTEXF_LINEAR);
slouken@1917
   672
        break;
slouken@1917
   673
    case SDL_TextureScaleMode_Best:
slouken@1917
   674
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
   675
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
   676
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
   677
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
   678
        break;
slouken@1917
   679
    }
slouken@1917
   680
slouken@1903
   681
    result =
slouken@1903
   682
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@1903
   683
                                    (IDirect3DBaseTexture9 *) texturedata->
slouken@1903
   684
                                    texture);
slouken@1903
   685
    if (FAILED(result)) {
slouken@1903
   686
        D3D_SetError("SetTexture()", result);
slouken@1903
   687
        return -1;
slouken@1903
   688
    }
slouken@1903
   689
    result =
slouken@1903
   690
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
   691
                                         vertices, sizeof(*vertices));
slouken@1903
   692
    if (FAILED(result)) {
slouken@1903
   693
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
   694
        return -1;
slouken@1903
   695
    }
slouken@1895
   696
    return 0;
slouken@1895
   697
}
slouken@1895
   698
slouken@1895
   699
static void
slouken@1913
   700
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   701
{
slouken@1913
   702
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
   703
    HRESULT result;
slouken@1900
   704
slouken@1900
   705
    if (!data->beginScene) {
slouken@1900
   706
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
   707
        data->beginScene = SDL_TRUE;
slouken@1900
   708
    }
slouken@1900
   709
slouken@1900
   710
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
   711
    if (FAILED(result)) {
slouken@1900
   712
        D3D_SetError("Present()", result);
slouken@1900
   713
    }
slouken@1895
   714
}
slouken@1895
   715
slouken@1895
   716
static void
slouken@1913
   717
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   718
{
slouken@1913
   719
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   720
slouken@1895
   721
    if (!data) {
slouken@1895
   722
        return;
slouken@1895
   723
    }
slouken@1903
   724
    if (data->texture) {
slouken@1903
   725
        IDirect3DTexture9_Release(data->texture);
slouken@1903
   726
    }
slouken@1895
   727
    SDL_free(data);
slouken@1895
   728
    texture->driverdata = NULL;
slouken@1895
   729
}
slouken@1895
   730
slouken@1895
   731
void
slouken@1913
   732
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   733
{
slouken@1913
   734
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   735
slouken@1895
   736
    if (data) {
slouken@1900
   737
        if (data->device) {
slouken@1900
   738
            IDirect3DDevice9_Release(data->device);
slouken@1900
   739
        }
slouken@1895
   740
        SDL_free(data);
slouken@1895
   741
    }
slouken@1895
   742
    SDL_free(renderer);
slouken@1895
   743
}
slouken@1895
   744
slouken@1895
   745
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
   746
slouken@1895
   747
/* vi: set ts=4 sw=4 expandtab: */