src/video/win32/SDL_d3drender.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 09 Nov 2009 05:20:11 +0000
changeset 3427 36cf454ba065
parent 3279 fd207dce9f94
child 3480 cd763d104465
permissions -rw-r--r--
Work in progress on implementation of SDL_RenderReadPixels() and SDL_RenderWritePixels(), code untested.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@2973
    27
#include "../SDL_yuv_sw_c.h"
slouken@1895
    28
slouken@1895
    29
/* Direct3D renderer implementation */
slouken@1895
    30
slouken@2786
    31
#if 1                           /* This takes more memory but you won't lose your texture data */
slouken@2932
    32
#define D3DPOOL_SDL    D3DPOOL_MANAGED
slouken@2783
    33
#define SDL_MEMORY_POOL_MANAGED
slouken@2783
    34
#else
slouken@2932
    35
#define D3DPOOL_SDL    D3DPOOL_DEFAULT
slouken@2783
    36
#define SDL_MEMORY_POOL_DEFAULT
slouken@2783
    37
#endif
slouken@2783
    38
slouken@1913
    39
static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1975
    40
static int D3D_DisplayModeChanged(SDL_Renderer * renderer);
slouken@1913
    41
static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@2990
    42
static int D3D_QueryTexturePixels(SDL_Renderer * renderer,
slouken@2990
    43
                                  SDL_Texture * texture, void **pixels,
slouken@2990
    44
                                  int *pitch);
slouken@1913
    45
static int D3D_SetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    46
                                 SDL_Texture * texture,
slouken@1913
    47
                                 const SDL_Color * colors, int firstcolor,
slouken@1913
    48
                                 int ncolors);
slouken@1913
    49
static int D3D_GetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    50
                                 SDL_Texture * texture, SDL_Color * colors,
slouken@1913
    51
                                 int firstcolor, int ncolors);
slouken@1985
    52
static int D3D_SetTextureColorMod(SDL_Renderer * renderer,
slouken@1985
    53
                                  SDL_Texture * texture);
slouken@1985
    54
static int D3D_SetTextureAlphaMod(SDL_Renderer * renderer,
slouken@1985
    55
                                  SDL_Texture * texture);
slouken@1985
    56
static int D3D_SetTextureBlendMode(SDL_Renderer * renderer,
slouken@1985
    57
                                   SDL_Texture * texture);
slouken@1985
    58
static int D3D_SetTextureScaleMode(SDL_Renderer * renderer,
slouken@1985
    59
                                   SDL_Texture * texture);
slouken@1913
    60
static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    61
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
    62
                             int pitch);
slouken@1913
    63
static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    64
                           const SDL_Rect * rect, int markDirty,
slouken@1913
    65
                           void **pixels, int *pitch);
slouken@1913
    66
static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    67
static void D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    68
                             int numrects, const SDL_Rect * rects);
slouken@2932
    69
static int D3D_RenderPoint(SDL_Renderer * renderer, int x, int y);
slouken@2932
    70
static int D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2,
slouken@2932
    71
                          int y2);
slouken@2919
    72
static int D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect);
slouken@1913
    73
static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
    74
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
slouken@3427
    75
static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3427
    76
                                void * pixels, int pitch);
slouken@1913
    77
static void D3D_RenderPresent(SDL_Renderer * renderer);
slouken@1913
    78
static void D3D_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
    79
                               SDL_Texture * texture);
slouken@1913
    80
static void D3D_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    81
slouken@1895
    82
slouken@1913
    83
SDL_RenderDriver D3D_RenderDriver = {
slouken@1913
    84
    D3D_CreateRenderer,
slouken@1895
    85
    {
slouken@1895
    86
     "d3d",
slouken@1965
    87
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1975
    88
      SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    89
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_PRESENTVSYNC |
slouken@1965
    90
      SDL_RENDERER_ACCELERATED),
slouken@1985
    91
     (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
slouken@1985
    92
      SDL_TEXTUREMODULATE_ALPHA),
slouken@2884
    93
     (SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK |
slouken@2884
    94
      SDL_BLENDMODE_BLEND | SDL_BLENDMODE_ADD | SDL_BLENDMODE_MOD),
slouken@1965
    95
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST |
slouken@1965
    96
      SDL_TEXTURESCALEMODE_SLOW | SDL_TEXTURESCALEMODE_BEST),
slouken@2972
    97
     0,
slouken@2972
    98
     {0},
slouken@1895
    99
     0,
slouken@1895
   100
     0}
slouken@1895
   101
};
slouken@1895
   102
slouken@1895
   103
typedef struct
slouken@1895
   104
{
slouken@2973
   105
    IDirect3D9 *d3d;
slouken@1895
   106
    IDirect3DDevice9 *device;
slouken@1975
   107
    D3DPRESENT_PARAMETERS pparams;
slouken@1900
   108
    SDL_bool beginScene;
slouken@1913
   109
} D3D_RenderData;
slouken@1895
   110
slouken@1895
   111
typedef struct
slouken@1895
   112
{
slouken@2973
   113
    SDL_SW_YUVTexture *yuv;
slouken@2973
   114
    Uint32 format;
slouken@1903
   115
    IDirect3DTexture9 *texture;
slouken@1913
   116
} D3D_TextureData;
slouken@1895
   117
slouken@1903
   118
typedef struct
slouken@1903
   119
{
slouken@1903
   120
    float x, y, z;
slouken@1904
   121
    float rhw;
slouken@1987
   122
    DWORD color;
slouken@1904
   123
    float u, v;
slouken@1903
   124
} Vertex;
slouken@1903
   125
slouken@1895
   126
static void
slouken@1900
   127
D3D_SetError(const char *prefix, HRESULT result)
slouken@1900
   128
{
slouken@1900
   129
    const char *error;
slouken@1900
   130
slouken@1900
   131
    switch (result) {
slouken@1900
   132
    case D3DERR_WRONGTEXTUREFORMAT:
slouken@1900
   133
        error = "WRONGTEXTUREFORMAT";
slouken@1900
   134
        break;
slouken@1900
   135
    case D3DERR_UNSUPPORTEDCOLOROPERATION:
slouken@1900
   136
        error = "UNSUPPORTEDCOLOROPERATION";
slouken@1900
   137
        break;
slouken@1900
   138
    case D3DERR_UNSUPPORTEDCOLORARG:
slouken@1900
   139
        error = "UNSUPPORTEDCOLORARG";
slouken@1900
   140
        break;
slouken@1900
   141
    case D3DERR_UNSUPPORTEDALPHAOPERATION:
slouken@1900
   142
        error = "UNSUPPORTEDALPHAOPERATION";
slouken@1900
   143
        break;
slouken@1900
   144
    case D3DERR_UNSUPPORTEDALPHAARG:
slouken@1900
   145
        error = "UNSUPPORTEDALPHAARG";
slouken@1900
   146
        break;
slouken@1900
   147
    case D3DERR_TOOMANYOPERATIONS:
slouken@1900
   148
        error = "TOOMANYOPERATIONS";
slouken@1900
   149
        break;
slouken@1900
   150
    case D3DERR_CONFLICTINGTEXTUREFILTER:
slouken@1900
   151
        error = "CONFLICTINGTEXTUREFILTER";
slouken@1900
   152
        break;
slouken@1900
   153
    case D3DERR_UNSUPPORTEDFACTORVALUE:
slouken@1900
   154
        error = "UNSUPPORTEDFACTORVALUE";
slouken@1900
   155
        break;
slouken@1900
   156
    case D3DERR_CONFLICTINGRENDERSTATE:
slouken@1900
   157
        error = "CONFLICTINGRENDERSTATE";
slouken@1900
   158
        break;
slouken@1900
   159
    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
slouken@1900
   160
        error = "UNSUPPORTEDTEXTUREFILTER";
slouken@1900
   161
        break;
slouken@1900
   162
    case D3DERR_CONFLICTINGTEXTUREPALETTE:
slouken@1900
   163
        error = "CONFLICTINGTEXTUREPALETTE";
slouken@1900
   164
        break;
slouken@1900
   165
    case D3DERR_DRIVERINTERNALERROR:
slouken@1900
   166
        error = "DRIVERINTERNALERROR";
slouken@1900
   167
        break;
slouken@1900
   168
    case D3DERR_NOTFOUND:
slouken@1900
   169
        error = "NOTFOUND";
slouken@1900
   170
        break;
slouken@1900
   171
    case D3DERR_MOREDATA:
slouken@1900
   172
        error = "MOREDATA";
slouken@1900
   173
        break;
slouken@1900
   174
    case D3DERR_DEVICELOST:
slouken@1900
   175
        error = "DEVICELOST";
slouken@1900
   176
        break;
slouken@1900
   177
    case D3DERR_DEVICENOTRESET:
slouken@1900
   178
        error = "DEVICENOTRESET";
slouken@1900
   179
        break;
slouken@1900
   180
    case D3DERR_NOTAVAILABLE:
slouken@1900
   181
        error = "NOTAVAILABLE";
slouken@1900
   182
        break;
slouken@1900
   183
    case D3DERR_OUTOFVIDEOMEMORY:
slouken@1900
   184
        error = "OUTOFVIDEOMEMORY";
slouken@1900
   185
        break;
slouken@1900
   186
    case D3DERR_INVALIDDEVICE:
slouken@1900
   187
        error = "INVALIDDEVICE";
slouken@1900
   188
        break;
slouken@1900
   189
    case D3DERR_INVALIDCALL:
slouken@1900
   190
        error = "INVALIDCALL";
slouken@1900
   191
        break;
slouken@1900
   192
    case D3DERR_DRIVERINVALIDCALL:
slouken@1900
   193
        error = "DRIVERINVALIDCALL";
slouken@1900
   194
        break;
slouken@1900
   195
    case D3DERR_WASSTILLDRAWING:
slouken@1900
   196
        error = "WASSTILLDRAWING";
slouken@1900
   197
        break;
slouken@1900
   198
    default:
slouken@1900
   199
        error = "UNKNOWN";
slouken@1900
   200
        break;
slouken@1900
   201
    }
slouken@1900
   202
    SDL_SetError("%s: %s", prefix, error);
slouken@1900
   203
}
slouken@1900
   204
slouken@1903
   205
static D3DFORMAT
slouken@1903
   206
PixelFormatToD3DFMT(Uint32 format)
slouken@1895
   207
{
slouken@1903
   208
    switch (format) {
slouken@1965
   209
    case SDL_PIXELFORMAT_INDEX8:
slouken@1903
   210
        return D3DFMT_P8;
slouken@1965
   211
    case SDL_PIXELFORMAT_RGB332:
slouken@1903
   212
        return D3DFMT_R3G3B2;
slouken@1965
   213
    case SDL_PIXELFORMAT_RGB444:
slouken@1903
   214
        return D3DFMT_X4R4G4B4;
slouken@1965
   215
    case SDL_PIXELFORMAT_RGB555:
slouken@1903
   216
        return D3DFMT_X1R5G5B5;
slouken@1965
   217
    case SDL_PIXELFORMAT_ARGB4444:
slouken@1903
   218
        return D3DFMT_A4R4G4B4;
slouken@1965
   219
    case SDL_PIXELFORMAT_ARGB1555:
slouken@1903
   220
        return D3DFMT_A1R5G5B5;
slouken@1965
   221
    case SDL_PIXELFORMAT_RGB565:
slouken@1903
   222
        return D3DFMT_R5G6B5;
slouken@1965
   223
    case SDL_PIXELFORMAT_RGB888:
slouken@1903
   224
        return D3DFMT_X8R8G8B8;
slouken@1965
   225
    case SDL_PIXELFORMAT_ARGB8888:
slouken@1903
   226
        return D3DFMT_A8R8G8B8;
slouken@1965
   227
    case SDL_PIXELFORMAT_ARGB2101010:
slouken@1903
   228
        return D3DFMT_A2R10G10B10;
slouken@1965
   229
    case SDL_PIXELFORMAT_UYVY:
slouken@1903
   230
        return D3DFMT_UYVY;
slouken@1965
   231
    case SDL_PIXELFORMAT_YUY2:
slouken@1903
   232
        return D3DFMT_YUY2;
slouken@1903
   233
    default:
slouken@1903
   234
        return D3DFMT_UNKNOWN;
slouken@1903
   235
    }
slouken@1895
   236
}
slouken@1895
   237
slouken@2973
   238
static SDL_bool
slouken@2990
   239
D3D_IsTextureFormatAvailable(IDirect3D9 * d3d, Uint32 display_format,
slouken@2990
   240
                             Uint32 texture_format)
slouken@2973
   241
{
slouken@2973
   242
    HRESULT result;
slouken@2973
   243
slouken@2990
   244
    result = IDirect3D9_CheckDeviceFormat(d3d, D3DADAPTER_DEFAULT,      /* FIXME */
slouken@2973
   245
                                          D3DDEVTYPE_HAL,
slouken@2973
   246
                                          PixelFormatToD3DFMT(display_format),
slouken@2973
   247
                                          0,
slouken@2973
   248
                                          D3DRTYPE_TEXTURE,
slouken@2990
   249
                                          PixelFormatToD3DFMT
slouken@2990
   250
                                          (texture_format));
slouken@2973
   251
    return FAILED(result) ? SDL_FALSE : SDL_TRUE;
slouken@2973
   252
}
slouken@2973
   253
slouken@2973
   254
static void
slouken@2973
   255
UpdateYUVTextureData(SDL_Texture * texture)
slouken@2973
   256
{
slouken@2973
   257
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@2973
   258
    SDL_Rect rect;
slouken@2973
   259
    RECT d3drect;
slouken@2973
   260
    D3DLOCKED_RECT locked;
slouken@2973
   261
    HRESULT result;
slouken@2973
   262
slouken@2973
   263
    d3drect.left = 0;
slouken@2973
   264
    d3drect.right = texture->w;
slouken@2973
   265
    d3drect.top = 0;
slouken@2973
   266
    d3drect.bottom = texture->h;
slouken@2973
   267
slouken@2990
   268
    result =
slouken@2990
   269
        IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
slouken@2973
   270
    if (FAILED(result)) {
slouken@2973
   271
        return;
slouken@2973
   272
    }
slouken@2973
   273
slouken@2973
   274
    rect.x = 0;
slouken@2973
   275
    rect.y = 0;
slouken@2973
   276
    rect.w = texture->w;
slouken@2973
   277
    rect.h = texture->h;
slouken@2973
   278
    SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
slouken@2973
   279
                        texture->h, locked.pBits, locked.Pitch);
slouken@2973
   280
slouken@2973
   281
    IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   282
}
slouken@2973
   283
slouken@1895
   284
void
slouken@1895
   285
D3D_AddRenderDriver(_THIS)
slouken@1895
   286
{
slouken@1895
   287
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@2972
   288
    SDL_RendererInfo *info = &D3D_RenderDriver.info;
slouken@2972
   289
    SDL_DisplayMode *mode = &SDL_CurrentDisplay.desktop_mode;
slouken@1895
   290
slouken@1895
   291
    if (data->d3d) {
slouken@2972
   292
        int i;
slouken@2972
   293
        int formats[] = {
slouken@2990
   294
            SDL_PIXELFORMAT_INDEX8,
slouken@2990
   295
            SDL_PIXELFORMAT_RGB332,
slouken@2990
   296
            SDL_PIXELFORMAT_RGB444,
slouken@2990
   297
            SDL_PIXELFORMAT_RGB555,
slouken@2990
   298
            SDL_PIXELFORMAT_ARGB4444,
slouken@2990
   299
            SDL_PIXELFORMAT_ARGB1555,
slouken@2990
   300
            SDL_PIXELFORMAT_RGB565,
slouken@2990
   301
            SDL_PIXELFORMAT_RGB888,
slouken@2990
   302
            SDL_PIXELFORMAT_ARGB8888,
slouken@2990
   303
            SDL_PIXELFORMAT_ARGB2101010,
slouken@2972
   304
        };
slouken@2972
   305
slouken@2972
   306
        for (i = 0; i < SDL_arraysize(formats); ++i) {
slouken@2990
   307
            if (D3D_IsTextureFormatAvailable
slouken@2990
   308
                (data->d3d, mode->format, formats[i])) {
slouken@2990
   309
                info->texture_formats[info->num_texture_formats++] =
slouken@2990
   310
                    formats[i];
slouken@2972
   311
            }
slouken@2972
   312
        }
slouken@2990
   313
        info->texture_formats[info->num_texture_formats++] =
slouken@2990
   314
            SDL_PIXELFORMAT_YV12;
slouken@2990
   315
        info->texture_formats[info->num_texture_formats++] =
slouken@2990
   316
            SDL_PIXELFORMAT_IYUV;
slouken@2990
   317
        info->texture_formats[info->num_texture_formats++] =
slouken@2990
   318
            SDL_PIXELFORMAT_YUY2;
slouken@2990
   319
        info->texture_formats[info->num_texture_formats++] =
slouken@2990
   320
            SDL_PIXELFORMAT_UYVY;
slouken@2990
   321
        info->texture_formats[info->num_texture_formats++] =
slouken@2990
   322
            SDL_PIXELFORMAT_YVYU;
slouken@2972
   323
slouken@1913
   324
        SDL_AddRenderDriver(0, &D3D_RenderDriver);
slouken@1895
   325
    }
slouken@1895
   326
}
slouken@1895
   327
slouken@1895
   328
SDL_Renderer *
slouken@1913
   329
D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   330
{
slouken@1895
   331
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1895
   332
    SDL_VideoData *videodata = (SDL_VideoData *) display->device->driverdata;
slouken@1895
   333
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   334
    SDL_Renderer *renderer;
slouken@1913
   335
    D3D_RenderData *data;
slouken@1900
   336
    HRESULT result;
slouken@1900
   337
    D3DPRESENT_PARAMETERS pparams;
slouken@1907
   338
    IDirect3DSwapChain9 *chain;
slouken@1925
   339
    D3DCAPS9 caps;
slouken@1895
   340
slouken@1920
   341
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   342
    if (!renderer) {
slouken@1895
   343
        SDL_OutOfMemory();
slouken@1895
   344
        return NULL;
slouken@1895
   345
    }
slouken@1895
   346
slouken@1920
   347
    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   348
    if (!data) {
slouken@1913
   349
        D3D_DestroyRenderer(renderer);
slouken@1895
   350
        SDL_OutOfMemory();
slouken@1895
   351
        return NULL;
slouken@1895
   352
    }
slouken@2973
   353
    data->d3d = videodata->d3d;
slouken@1895
   354
slouken@1975
   355
    renderer->DisplayModeChanged = D3D_DisplayModeChanged;
slouken@1913
   356
    renderer->CreateTexture = D3D_CreateTexture;
slouken@2973
   357
    renderer->QueryTexturePixels = D3D_QueryTexturePixels;
slouken@1913
   358
    renderer->SetTexturePalette = D3D_SetTexturePalette;
slouken@1913
   359
    renderer->GetTexturePalette = D3D_GetTexturePalette;
slouken@1985
   360
    renderer->SetTextureColorMod = D3D_SetTextureColorMod;
slouken@1985
   361
    renderer->SetTextureAlphaMod = D3D_SetTextureAlphaMod;
slouken@1985
   362
    renderer->SetTextureBlendMode = D3D_SetTextureBlendMode;
slouken@1985
   363
    renderer->SetTextureScaleMode = D3D_SetTextureScaleMode;
slouken@1913
   364
    renderer->UpdateTexture = D3D_UpdateTexture;
slouken@1913
   365
    renderer->LockTexture = D3D_LockTexture;
slouken@1913
   366
    renderer->UnlockTexture = D3D_UnlockTexture;
slouken@1913
   367
    renderer->DirtyTexture = D3D_DirtyTexture;
slouken@2932
   368
    renderer->RenderPoint = D3D_RenderPoint;
slouken@2932
   369
    renderer->RenderLine = D3D_RenderLine;
slouken@1913
   370
    renderer->RenderFill = D3D_RenderFill;
slouken@1913
   371
    renderer->RenderCopy = D3D_RenderCopy;
slouken@3427
   372
    renderer->RenderReadPixels = D3D_RenderReadPixels;
slouken@1913
   373
    renderer->RenderPresent = D3D_RenderPresent;
slouken@1913
   374
    renderer->DestroyTexture = D3D_DestroyTexture;
slouken@1913
   375
    renderer->DestroyRenderer = D3D_DestroyRenderer;
slouken@1913
   376
    renderer->info = D3D_RenderDriver.info;
slouken@1895
   377
    renderer->window = window->id;
slouken@1895
   378
    renderer->driverdata = data;
slouken@1895
   379
slouken@1965
   380
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   381
slouken@1900
   382
    SDL_zero(pparams);
slouken@1900
   383
    pparams.BackBufferWidth = window->w;
slouken@1900
   384
    pparams.BackBufferHeight = window->h;
slouken@1903
   385
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1903
   386
        pparams.BackBufferFormat =
slouken@1975
   387
            PixelFormatToD3DFMT(display->fullscreen_mode.format);
slouken@1903
   388
    } else {
slouken@1903
   389
        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1903
   390
    }
slouken@1965
   391
    if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1900
   392
        pparams.BackBufferCount = 2;
slouken@1900
   393
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1965
   394
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1900
   395
        pparams.BackBufferCount = 3;
slouken@1900
   396
        pparams.SwapEffect = D3DSWAPEFFECT_FLIP;
slouken@1965
   397
    } else if (flags & SDL_RENDERER_PRESENTCOPY) {
slouken@1900
   398
        pparams.BackBufferCount = 1;
slouken@1900
   399
        pparams.SwapEffect = D3DSWAPEFFECT_COPY;
slouken@1900
   400
    } else {
slouken@1900
   401
        pparams.BackBufferCount = 1;
slouken@1900
   402
        pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
slouken@1900
   403
    }
slouken@1900
   404
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1900
   405
        pparams.Windowed = FALSE;
slouken@1903
   406
        pparams.FullScreen_RefreshRateInHz =
slouken@1975
   407
            display->fullscreen_mode.refresh_rate;
slouken@1900
   408
    } else {
slouken@1900
   409
        pparams.Windowed = TRUE;
slouken@1903
   410
        pparams.FullScreen_RefreshRateInHz = 0;
slouken@1900
   411
    }
slouken@1965
   412
    if (flags & SDL_RENDERER_PRESENTVSYNC) {
slouken@1907
   413
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
slouken@1907
   414
    } else {
slouken@1907
   415
        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
slouken@1907
   416
    }
slouken@1900
   417
slouken@3197
   418
    IDirect3D9_GetDeviceCaps(videodata->d3d, D3DADAPTER_DEFAULT,
slouken@3197
   419
                             D3DDEVTYPE_HAL, &caps);
slouken@3197
   420
slouken@1900
   421
    result = IDirect3D9_CreateDevice(videodata->d3d, D3DADAPTER_DEFAULT,        /* FIXME */
slouken@1900
   422
                                     D3DDEVTYPE_HAL,
slouken@1900
   423
                                     windowdata->hwnd,
slouken@3197
   424
                                     (caps.
slouken@3197
   425
                                      DevCaps &
slouken@3197
   426
                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
slouken@3197
   427
                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
slouken@1900
   428
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
slouken@1900
   429
                                     &pparams, &data->device);
slouken@1900
   430
    if (FAILED(result)) {
slouken@1913
   431
        D3D_DestroyRenderer(renderer);
slouken@1900
   432
        D3D_SetError("CreateDevice()", result);
slouken@1900
   433
        return NULL;
slouken@1900
   434
    }
slouken@1900
   435
    data->beginScene = SDL_TRUE;
slouken@1900
   436
slouken@1907
   437
    /* Get presentation parameters to fill info */
slouken@1907
   438
    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
slouken@1907
   439
    if (FAILED(result)) {
slouken@1913
   440
        D3D_DestroyRenderer(renderer);
slouken@1907
   441
        D3D_SetError("GetSwapChain()", result);
slouken@1907
   442
        return NULL;
slouken@1907
   443
    }
slouken@1907
   444
    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
slouken@1907
   445
    if (FAILED(result)) {
slouken@1907
   446
        IDirect3DSwapChain9_Release(chain);
slouken@1913
   447
        D3D_DestroyRenderer(renderer);
slouken@1907
   448
        D3D_SetError("GetPresentParameters()", result);
slouken@1907
   449
        return NULL;
slouken@1907
   450
    }
slouken@1907
   451
    IDirect3DSwapChain9_Release(chain);
slouken@1907
   452
    switch (pparams.SwapEffect) {
slouken@1907
   453
    case D3DSWAPEFFECT_COPY:
slouken@1965
   454
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1907
   455
        break;
slouken@1907
   456
    case D3DSWAPEFFECT_FLIP:
slouken@1907
   457
        switch (pparams.BackBufferCount) {
slouken@1907
   458
        case 2:
slouken@1965
   459
            renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1907
   460
            break;
slouken@1907
   461
        case 3:
slouken@1965
   462
            renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1907
   463
            break;
slouken@1907
   464
        }
slouken@1907
   465
        break;
slouken@1907
   466
    case D3DSWAPEFFECT_DISCARD:
slouken@1965
   467
        renderer->info.flags |= SDL_RENDERER_PRESENTDISCARD;
slouken@1907
   468
        break;
slouken@1907
   469
    }
slouken@1907
   470
    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
slouken@1965
   471
        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
slouken@1907
   472
    }
slouken@1975
   473
    data->pparams = pparams;
slouken@1907
   474
slouken@1925
   475
    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
slouken@1925
   476
    renderer->info.max_texture_width = caps.MaxTextureWidth;
slouken@1925
   477
    renderer->info.max_texture_height = caps.MaxTextureHeight;
slouken@1918
   478
slouken@1903
   479
    /* Set up parameters for rendering */
slouken@1904
   480
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   481
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   482
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1988
   483
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
slouken@1903
   484
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1903
   485
                                    D3DCULL_NONE);
slouken@1904
   486
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1988
   487
    /* Enable color modulation by diffuse color */
slouken@1988
   488
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
slouken@1988
   489
                                          D3DTOP_MODULATE);
slouken@1988
   490
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
slouken@1988
   491
                                          D3DTA_TEXTURE);
slouken@1988
   492
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
slouken@1988
   493
                                          D3DTA_DIFFUSE);
slouken@1988
   494
    /* Enable alpha modulation by diffuse alpha */
slouken@1988
   495
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
slouken@1988
   496
                                          D3DTOP_MODULATE);
slouken@1988
   497
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
slouken@1988
   498
                                          D3DTA_TEXTURE);
slouken@1988
   499
    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
slouken@1988
   500
                                          D3DTA_DIFFUSE);
slouken@1991
   501
    /* Disable second texture stage, since we're done */
slouken@1991
   502
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
slouken@1991
   503
                                          D3DTOP_DISABLE);
slouken@1991
   504
    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
slouken@1991
   505
                                          D3DTOP_DISABLE);
slouken@1903
   506
slouken@1895
   507
    return renderer;
slouken@1895
   508
}
slouken@1895
   509
slouken@1895
   510
static int
slouken@1975
   511
D3D_Reset(SDL_Renderer * renderer)
slouken@1975
   512
{
slouken@1975
   513
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   514
    HRESULT result;
slouken@1975
   515
slouken@1975
   516
    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
slouken@1975
   517
    if (FAILED(result)) {
slouken@1975
   518
        if (result == D3DERR_DEVICELOST) {
slouken@1975
   519
            /* Don't worry about it, we'll reset later... */
slouken@1975
   520
            return 0;
slouken@1975
   521
        } else {
slouken@1975
   522
            D3D_SetError("Reset()", result);
slouken@1975
   523
            return -1;
slouken@1975
   524
        }
slouken@1975
   525
    }
slouken@1975
   526
    IDirect3DDevice9_SetVertexShader(data->device, NULL);
slouken@1987
   527
    IDirect3DDevice9_SetFVF(data->device,
slouken@1987
   528
                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
slouken@1975
   529
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
slouken@1975
   530
                                    D3DCULL_NONE);
slouken@1975
   531
    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
slouken@1975
   532
    return 0;
slouken@1975
   533
}
slouken@1975
   534
slouken@1975
   535
static int
slouken@1975
   536
D3D_DisplayModeChanged(SDL_Renderer * renderer)
slouken@1975
   537
{
slouken@1975
   538
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1975
   539
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1975
   540
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1975
   541
slouken@1975
   542
    data->pparams.BackBufferWidth = window->w;
slouken@1975
   543
    data->pparams.BackBufferHeight = window->h;
slouken@1975
   544
    if (window->flags & SDL_WINDOW_FULLSCREEN) {
slouken@1975
   545
        data->pparams.BackBufferFormat =
slouken@1975
   546
            PixelFormatToD3DFMT(display->fullscreen_mode.format);
slouken@1975
   547
    } else {
slouken@1975
   548
        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
slouken@1975
   549
    }
slouken@1975
   550
    return D3D_Reset(renderer);
slouken@1975
   551
}
slouken@1975
   552
slouken@1975
   553
static int
slouken@1913
   554
D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   555
{
slouken@1913
   556
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   557
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   558
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@2973
   559
    Uint32 display_format = display->current_mode.format;
slouken@1913
   560
    D3D_TextureData *data;
slouken@1903
   561
    HRESULT result;
slouken@1895
   562
slouken@1920
   563
    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   564
    if (!data) {
slouken@1895
   565
        SDL_OutOfMemory();
slouken@1895
   566
        return -1;
slouken@1895
   567
    }
slouken@1895
   568
slouken@1895
   569
    texture->driverdata = data;
slouken@1895
   570
slouken@2973
   571
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format) &&
slouken@2973
   572
        (texture->format != SDL_PIXELFORMAT_YUY2 ||
slouken@2990
   573
         !D3D_IsTextureFormatAvailable(renderdata->d3d, display_format,
slouken@2990
   574
                                       texture->format))
slouken@2990
   575
        && (texture->format != SDL_PIXELFORMAT_YVYU
slouken@2990
   576
            || !D3D_IsTextureFormatAvailable(renderdata->d3d, display_format,
slouken@2990
   577
                                             texture->format))) {
slouken@2973
   578
        data->yuv =
slouken@2973
   579
            SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
slouken@2973
   580
        if (!data->yuv) {
slouken@2973
   581
            return -1;
slouken@2973
   582
        }
slouken@2973
   583
        data->format = display->current_mode.format;
slouken@2973
   584
    } else {
slouken@2973
   585
        data->format = texture->format;
slouken@2973
   586
    }
slouken@2973
   587
slouken@1903
   588
    result =
slouken@1903
   589
        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@1903
   590
                                       texture->h, 1, 0,
slouken@2973
   591
                                       PixelFormatToD3DFMT(data->format),
slouken@2783
   592
                                       D3DPOOL_SDL, &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@2973
   602
D3D_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@2973
   603
                       void **pixels, int *pitch)
slouken@2973
   604
{
slouken@2973
   605
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@2973
   606
slouken@2973
   607
    if (data->yuv) {
slouken@2973
   608
        return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
slouken@2973
   609
    } else {
slouken@2973
   610
        /* D3D textures don't have their pixels hanging out */
slouken@2973
   611
        return -1;
slouken@2973
   612
    }
slouken@2973
   613
}
slouken@2973
   614
slouken@2973
   615
static int
slouken@1913
   616
D3D_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   617
                      const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   618
{
slouken@1913
   619
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1913
   620
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   621
slouken@1903
   622
    return 0;
slouken@1895
   623
}
slouken@1895
   624
slouken@1895
   625
static int
slouken@1913
   626
D3D_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   627
                      SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   628
{
slouken@1913
   629
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   630
slouken@1903
   631
    return 0;
slouken@1895
   632
}
slouken@1895
   633
slouken@1895
   634
static int
slouken@1985
   635
D3D_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   636
{
slouken@1987
   637
    return 0;
slouken@1985
   638
}
slouken@1985
   639
slouken@1985
   640
static int
slouken@1985
   641
D3D_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   642
{
slouken@1987
   643
    return 0;
slouken@1985
   644
}
slouken@1985
   645
slouken@1985
   646
static int
slouken@1985
   647
D3D_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   648
{
slouken@1985
   649
    switch (texture->blendMode) {
slouken@2884
   650
    case SDL_BLENDMODE_NONE:
slouken@2884
   651
    case SDL_BLENDMODE_MASK:
slouken@2884
   652
    case SDL_BLENDMODE_BLEND:
slouken@2884
   653
    case SDL_BLENDMODE_ADD:
slouken@2884
   654
    case SDL_BLENDMODE_MOD:
slouken@1985
   655
        return 0;
slouken@1985
   656
    default:
slouken@1985
   657
        SDL_Unsupported();
slouken@2884
   658
        texture->blendMode = SDL_BLENDMODE_NONE;
slouken@1985
   659
        return -1;
slouken@1985
   660
    }
slouken@1985
   661
}
slouken@1985
   662
slouken@1985
   663
static int
slouken@1985
   664
D3D_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1985
   665
{
slouken@1985
   666
    switch (texture->scaleMode) {
slouken@1985
   667
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1985
   668
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1985
   669
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1985
   670
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1985
   671
        return 0;
slouken@1985
   672
    default:
slouken@1985
   673
        SDL_Unsupported();
slouken@1985
   674
        texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
slouken@1985
   675
        return -1;
slouken@1985
   676
    }
slouken@1985
   677
    return 0;
slouken@1985
   678
}
slouken@1985
   679
slouken@1985
   680
static int
slouken@1913
   681
D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   682
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   683
{
slouken@1913
   684
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1913
   685
    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
slouken@1895
   686
slouken@2973
   687
    if (data->yuv) {
slouken@2973
   688
        if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
slouken@2973
   689
            return -1;
slouken@2973
   690
        }
slouken@2973
   691
        UpdateYUVTextureData(texture);
slouken@2973
   692
        return 0;
slouken@2973
   693
    } else {
slouken@2973
   694
#ifdef SDL_MEMORY_POOL_DEFAULT
slouken@2973
   695
        IDirect3DTexture9 *temp;
slouken@2973
   696
        RECT d3drect;
slouken@2973
   697
        D3DLOCKED_RECT locked;
slouken@2973
   698
        const Uint8 *src;
slouken@2973
   699
        Uint8 *dst;
slouken@2973
   700
        int row, length;
slouken@2973
   701
        HRESULT result;
slouken@2973
   702
slouken@2973
   703
        result =
slouken@2973
   704
            IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
slouken@2973
   705
                                           texture->h, 1, 0,
slouken@3013
   706
                                           PixelFormatToD3DFMT(texture->
slouken@3013
   707
                                                               format),
slouken@2973
   708
                                           D3DPOOL_SYSTEMMEM, &temp, NULL);
slouken@2973
   709
        if (FAILED(result)) {
slouken@2973
   710
            D3D_SetError("CreateTexture()", result);
slouken@2973
   711
            return -1;
slouken@2973
   712
        }
slouken@2973
   713
slouken@2973
   714
        d3drect.left = rect->x;
slouken@2973
   715
        d3drect.right = rect->x + rect->w;
slouken@2973
   716
        d3drect.top = rect->y;
slouken@2973
   717
        d3drect.bottom = rect->y + rect->h;
slouken@2973
   718
slouken@2973
   719
        result = IDirect3DTexture9_LockRect(temp, 0, &locked, &d3drect, 0);
slouken@2973
   720
        if (FAILED(result)) {
slouken@2973
   721
            IDirect3DTexture9_Release(temp);
slouken@2973
   722
            D3D_SetError("LockRect()", result);
slouken@2973
   723
            return -1;
slouken@2973
   724
        }
slouken@2973
   725
slouken@2973
   726
        src = pixels;
slouken@2973
   727
        dst = locked.pBits;
slouken@2973
   728
        length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2973
   729
        for (row = 0; row < rect->h; ++row) {
slouken@2973
   730
            SDL_memcpy(dst, src, length);
slouken@2973
   731
            src += pitch;
slouken@2973
   732
            dst += locked.Pitch;
slouken@2973
   733
        }
slouken@2973
   734
        IDirect3DTexture9_UnlockRect(temp, 0);
slouken@2973
   735
slouken@2973
   736
        result =
slouken@2973
   737
            IDirect3DDevice9_UpdateTexture(renderdata->device,
slouken@2973
   738
                                           (IDirect3DBaseTexture9 *) temp,
slouken@2973
   739
                                           (IDirect3DBaseTexture9 *)
slouken@2973
   740
                                           data->texture);
slouken@2973
   741
        IDirect3DTexture9_Release(temp);
slouken@2973
   742
        if (FAILED(result)) {
slouken@2973
   743
            D3D_SetError("UpdateTexture()", result);
slouken@2973
   744
            return -1;
slouken@2973
   745
        }
slouken@2973
   746
#else
slouken@2973
   747
        RECT d3drect;
slouken@2973
   748
        D3DLOCKED_RECT locked;
slouken@2973
   749
        const Uint8 *src;
slouken@2973
   750
        Uint8 *dst;
slouken@2973
   751
        int row, length;
slouken@2973
   752
        HRESULT result;
slouken@2973
   753
slouken@2973
   754
        d3drect.left = rect->x;
slouken@2973
   755
        d3drect.right = rect->x + rect->w;
slouken@2973
   756
        d3drect.top = rect->y;
slouken@2973
   757
        d3drect.bottom = rect->y + rect->h;
slouken@2973
   758
slouken@2973
   759
        result =
slouken@2990
   760
            IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@2990
   761
                                       0);
slouken@2973
   762
        if (FAILED(result)) {
slouken@2973
   763
            D3D_SetError("LockRect()", result);
slouken@2973
   764
            return -1;
slouken@2973
   765
        }
slouken@2973
   766
slouken@2973
   767
        src = pixels;
slouken@2973
   768
        dst = locked.pBits;
slouken@2973
   769
        length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@2973
   770
        for (row = 0; row < rect->h; ++row) {
slouken@2973
   771
            SDL_memcpy(dst, src, length);
slouken@2973
   772
            src += pitch;
slouken@2973
   773
            dst += locked.Pitch;
slouken@2973
   774
        }
slouken@2973
   775
        IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   776
#endif // SDL_MEMORY_POOL_DEFAULT
slouken@2973
   777
slouken@2973
   778
        return 0;
slouken@1903
   779
    }
slouken@1895
   780
}
slouken@1895
   781
slouken@1895
   782
static int
slouken@1913
   783
D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   784
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   785
                int *pitch)
slouken@1895
   786
{
slouken@1913
   787
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   788
slouken@2973
   789
    if (data->yuv) {
slouken@2973
   790
        return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
slouken@2973
   791
                                     pitch);
slouken@2973
   792
    } else {
slouken@2973
   793
        RECT d3drect;
slouken@2973
   794
        D3DLOCKED_RECT locked;
slouken@2973
   795
        HRESULT result;
slouken@1903
   796
slouken@2973
   797
        d3drect.left = rect->x;
slouken@2973
   798
        d3drect.right = rect->x + rect->w;
slouken@2973
   799
        d3drect.top = rect->y;
slouken@2973
   800
        d3drect.bottom = rect->y + rect->h;
slouken@2973
   801
slouken@2973
   802
        result =
slouken@2973
   803
            IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect,
slouken@2990
   804
                                       markDirty ? 0 :
slouken@2990
   805
                                       D3DLOCK_NO_DIRTY_UPDATE);
slouken@2973
   806
        if (FAILED(result)) {
slouken@2973
   807
            D3D_SetError("LockRect()", result);
slouken@2973
   808
            return -1;
slouken@2973
   809
        }
slouken@2973
   810
        *pixels = locked.pBits;
slouken@2973
   811
        *pitch = locked.Pitch;
slouken@2973
   812
        return 0;
slouken@1903
   813
    }
slouken@1895
   814
}
slouken@1895
   815
slouken@1895
   816
static void
slouken@1913
   817
D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   818
{
slouken@1913
   819
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
   820
slouken@2973
   821
    if (data->yuv) {
slouken@2973
   822
        SDL_SW_UnlockYUVTexture(data->yuv);
slouken@2973
   823
        UpdateYUVTextureData(texture);
slouken@2973
   824
    } else {
slouken@2973
   825
        IDirect3DTexture9_UnlockRect(data->texture, 0);
slouken@2973
   826
    }
slouken@1895
   827
}
slouken@1895
   828
slouken@1895
   829
static void
slouken@1913
   830
D3D_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   831
                 const SDL_Rect * rects)
slouken@1895
   832
{
slouken@1913
   833
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1903
   834
    RECT d3drect;
slouken@1903
   835
    int i;
slouken@1903
   836
slouken@1903
   837
    for (i = 0; i < numrects; ++i) {
slouken@1903
   838
        const SDL_Rect *rect = &rects[i];
slouken@1903
   839
slouken@1903
   840
        d3drect.left = rect->x;
slouken@1903
   841
        d3drect.right = rect->x + rect->w;
slouken@1903
   842
        d3drect.top = rect->y;
slouken@1903
   843
        d3drect.bottom = rect->y + rect->h;
slouken@1903
   844
slouken@1903
   845
        IDirect3DTexture9_AddDirtyRect(data->texture, &d3drect);
slouken@1903
   846
    }
slouken@1895
   847
}
slouken@1895
   848
slouken@2932
   849
static void
slouken@2933
   850
D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
slouken@2932
   851
{
slouken@2932
   852
    switch (blendMode) {
slouken@2932
   853
    case SDL_BLENDMODE_NONE:
slouken@2932
   854
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   855
                                        FALSE);
slouken@2932
   856
        break;
slouken@2932
   857
    case SDL_BLENDMODE_MASK:
slouken@2932
   858
    case SDL_BLENDMODE_BLEND:
slouken@2932
   859
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   860
                                        TRUE);
slouken@2932
   861
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   862
                                        D3DBLEND_SRCALPHA);
slouken@2932
   863
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   864
                                        D3DBLEND_INVSRCALPHA);
slouken@2932
   865
        break;
slouken@2932
   866
    case SDL_BLENDMODE_ADD:
slouken@2932
   867
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   868
                                        TRUE);
slouken@2932
   869
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   870
                                        D3DBLEND_SRCALPHA);
slouken@2932
   871
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   872
                                        D3DBLEND_ONE);
slouken@2932
   873
        break;
slouken@2932
   874
    case SDL_BLENDMODE_MOD:
slouken@2932
   875
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
slouken@2932
   876
                                        TRUE);
slouken@2932
   877
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
slouken@2932
   878
                                        D3DBLEND_ZERO);
slouken@2932
   879
        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
slouken@2932
   880
                                        D3DBLEND_SRCCOLOR);
slouken@2932
   881
        break;
slouken@2932
   882
    }
slouken@2932
   883
}
slouken@2932
   884
slouken@1895
   885
static int
slouken@2932
   886
D3D_RenderPoint(SDL_Renderer * renderer, int x, int y)
slouken@1895
   887
{
slouken@1913
   888
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   889
    DWORD color;
slouken@2932
   890
    Vertex vertices[1];
slouken@1900
   891
    HRESULT result;
slouken@1895
   892
slouken@1900
   893
    if (data->beginScene) {
slouken@1900
   894
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
   895
        data->beginScene = SDL_FALSE;
slouken@1900
   896
    }
slouken@1895
   897
slouken@2932
   898
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@1901
   899
slouken@3267
   900
    vertices[0].x = (float) x;
slouken@3267
   901
    vertices[0].y = (float) y;
slouken@2932
   902
    vertices[0].z = 0.0f;
slouken@2932
   903
    vertices[0].rhw = 1.0f;
slouken@2932
   904
    vertices[0].color = color;
slouken@2932
   905
    vertices[0].u = 0.0f;
slouken@2932
   906
    vertices[0].v = 0.0f;
slouken@2932
   907
slouken@2932
   908
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   909
slouken@2933
   910
    result =
slouken@2933
   911
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   912
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   913
    if (FAILED(result)) {
slouken@2932
   914
        D3D_SetError("SetTexture()", result);
slouken@2932
   915
        return -1;
slouken@2932
   916
    }
slouken@1903
   917
    result =
slouken@2932
   918
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1,
slouken@2932
   919
                                         vertices, sizeof(*vertices));
slouken@1900
   920
    if (FAILED(result)) {
slouken@2932
   921
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   922
        return -1;
slouken@2932
   923
    }
slouken@2932
   924
    return 0;
slouken@2932
   925
}
slouken@2932
   926
slouken@2932
   927
static int
slouken@2932
   928
D3D_RenderLine(SDL_Renderer * renderer, int x1, int y1, int x2, int y2)
slouken@2932
   929
{
slouken@2932
   930
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   931
    DWORD color;
slouken@2932
   932
    Vertex vertices[2];
slouken@2932
   933
    HRESULT result;
slouken@2932
   934
slouken@2932
   935
    if (data->beginScene) {
slouken@2932
   936
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   937
        data->beginScene = SDL_FALSE;
slouken@2932
   938
    }
slouken@2932
   939
slouken@2932
   940
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@2932
   941
slouken@3267
   942
    vertices[0].x = (float) x1;
slouken@3267
   943
    vertices[0].y = (float) y1;
slouken@2932
   944
    vertices[0].z = 0.0f;
slouken@2932
   945
    vertices[0].rhw = 1.0f;
slouken@2932
   946
    vertices[0].color = color;
slouken@2932
   947
    vertices[0].u = 0.0f;
slouken@2932
   948
    vertices[0].v = 0.0f;
slouken@2932
   949
slouken@3267
   950
    vertices[1].x = (float) x2;
slouken@3267
   951
    vertices[1].y = (float) y2;
slouken@2932
   952
    vertices[1].z = 0.0f;
slouken@2932
   953
    vertices[1].rhw = 1.0f;
slouken@2932
   954
    vertices[1].color = color;
slouken@2932
   955
    vertices[1].u = 0.0f;
slouken@2932
   956
    vertices[1].v = 0.0f;
slouken@2932
   957
slouken@2932
   958
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
   959
slouken@2933
   960
    result =
slouken@2933
   961
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
   962
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
   963
    if (FAILED(result)) {
slouken@2932
   964
        D3D_SetError("SetTexture()", result);
slouken@2932
   965
        return -1;
slouken@2932
   966
    }
slouken@2932
   967
    result =
slouken@2932
   968
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINELIST, 1,
slouken@2932
   969
                                         vertices, sizeof(*vertices));
slouken@2932
   970
    if (FAILED(result)) {
slouken@2932
   971
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@2932
   972
        return -1;
slouken@2932
   973
    }
slouken@2932
   974
    return 0;
slouken@2932
   975
}
slouken@2932
   976
slouken@2932
   977
static int
slouken@2932
   978
D3D_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect)
slouken@2932
   979
{
slouken@2932
   980
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@2932
   981
    float minx, miny, maxx, maxy;
slouken@2932
   982
    DWORD color;
slouken@2932
   983
    Vertex vertices[4];
slouken@2932
   984
    HRESULT result;
slouken@2932
   985
slouken@2932
   986
    if (data->beginScene) {
slouken@2932
   987
        IDirect3DDevice9_BeginScene(data->device);
slouken@2932
   988
        data->beginScene = SDL_FALSE;
slouken@2932
   989
    }
slouken@2932
   990
slouken@3267
   991
    minx = (float) rect->x;
slouken@3267
   992
    miny = (float) rect->y;
slouken@3267
   993
    maxx = (float) rect->x + rect->w;
slouken@3267
   994
    maxy = (float) rect->y + rect->h;
slouken@2932
   995
slouken@2932
   996
    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
slouken@2932
   997
slouken@2932
   998
    vertices[0].x = minx;
slouken@2932
   999
    vertices[0].y = miny;
slouken@2932
  1000
    vertices[0].z = 0.0f;
slouken@2932
  1001
    vertices[0].rhw = 1.0f;
slouken@2932
  1002
    vertices[0].color = color;
slouken@2932
  1003
    vertices[0].u = 0.0f;
slouken@2932
  1004
    vertices[0].v = 0.0f;
slouken@2932
  1005
slouken@2932
  1006
    vertices[1].x = maxx;
slouken@2932
  1007
    vertices[1].y = miny;
slouken@2932
  1008
    vertices[1].z = 0.0f;
slouken@2932
  1009
    vertices[1].rhw = 1.0f;
slouken@2932
  1010
    vertices[1].color = color;
slouken@2932
  1011
    vertices[1].u = 0.0f;
slouken@2932
  1012
    vertices[1].v = 0.0f;
slouken@2932
  1013
slouken@2932
  1014
    vertices[2].x = maxx;
slouken@2932
  1015
    vertices[2].y = maxy;
slouken@2932
  1016
    vertices[2].z = 0.0f;
slouken@2932
  1017
    vertices[2].rhw = 1.0f;
slouken@2932
  1018
    vertices[2].color = color;
slouken@2932
  1019
    vertices[2].u = 0.0f;
slouken@2932
  1020
    vertices[2].v = 0.0f;
slouken@2932
  1021
slouken@2932
  1022
    vertices[3].x = minx;
slouken@2932
  1023
    vertices[3].y = maxy;
slouken@2932
  1024
    vertices[3].z = 0.0f;
slouken@2932
  1025
    vertices[3].rhw = 1.0f;
slouken@2932
  1026
    vertices[3].color = color;
slouken@2932
  1027
    vertices[3].u = 0.0f;
slouken@2932
  1028
    vertices[3].v = 0.0f;
slouken@2932
  1029
slouken@2932
  1030
    D3D_SetBlendMode(data, renderer->blendMode);
slouken@2932
  1031
slouken@2933
  1032
    result =
slouken@2933
  1033
        IDirect3DDevice9_SetTexture(data->device, 0,
slouken@2933
  1034
                                    (IDirect3DBaseTexture9 *) 0);
slouken@2932
  1035
    if (FAILED(result)) {
slouken@2932
  1036
        D3D_SetError("SetTexture()", result);
slouken@2932
  1037
        return -1;
slouken@2932
  1038
    }
slouken@2932
  1039
    result =
slouken@2932
  1040
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@2932
  1041
                                         vertices, sizeof(*vertices));
slouken@2932
  1042
    if (FAILED(result)) {
slouken@2932
  1043
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1900
  1044
        return -1;
slouken@1900
  1045
    }
slouken@1895
  1046
    return 0;
slouken@1895
  1047
}
slouken@1895
  1048
slouken@1895
  1049
static int
slouken@1913
  1050
D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1985
  1051
               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
slouken@1895
  1052
{
slouken@1913
  1053
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1913
  1054
    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
slouken@1903
  1055
    float minx, miny, maxx, maxy;
slouken@1904
  1056
    float minu, maxu, minv, maxv;
slouken@1987
  1057
    DWORD color;
slouken@1903
  1058
    Vertex vertices[4];
slouken@1903
  1059
    HRESULT result;
slouken@1895
  1060
slouken@1900
  1061
    if (data->beginScene) {
slouken@1900
  1062
        IDirect3DDevice9_BeginScene(data->device);
slouken@1900
  1063
        data->beginScene = SDL_FALSE;
slouken@1900
  1064
    }
slouken@1903
  1065
slouken@3279
  1066
    minx = (float) dstrect->x - 0.5f;
slouken@3279
  1067
    miny = (float) dstrect->y - 0.5f;
slouken@3279
  1068
    maxx = (float) dstrect->x + dstrect->w - 0.5f;
slouken@3279
  1069
    maxy = (float) dstrect->y + dstrect->h - 0.5f;
slouken@1903
  1070
slouken@1904
  1071
    minu = (float) srcrect->x / texture->w;
slouken@1904
  1072
    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
slouken@1904
  1073
    minv = (float) srcrect->y / texture->h;
slouken@1904
  1074
    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
slouken@1903
  1075
slouken@1987
  1076
    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
slouken@1987
  1077
slouken@1903
  1078
    vertices[0].x = minx;
slouken@1903
  1079
    vertices[0].y = miny;
slouken@1903
  1080
    vertices[0].z = 0.0f;
slouken@1904
  1081
    vertices[0].rhw = 1.0f;
slouken@1987
  1082
    vertices[0].color = color;
slouken@1904
  1083
    vertices[0].u = minu;
slouken@1904
  1084
    vertices[0].v = minv;
slouken@1904
  1085
slouken@1903
  1086
    vertices[1].x = maxx;
slouken@1903
  1087
    vertices[1].y = miny;
slouken@1903
  1088
    vertices[1].z = 0.0f;
slouken@1904
  1089
    vertices[1].rhw = 1.0f;
slouken@1987
  1090
    vertices[1].color = color;
slouken@1904
  1091
    vertices[1].u = maxu;
slouken@1904
  1092
    vertices[1].v = minv;
slouken@1904
  1093
slouken@1903
  1094
    vertices[2].x = maxx;
slouken@1903
  1095
    vertices[2].y = maxy;
slouken@1903
  1096
    vertices[2].z = 0.0f;
slouken@1904
  1097
    vertices[2].rhw = 1.0f;
slouken@1987
  1098
    vertices[2].color = color;
slouken@1904
  1099
    vertices[2].u = maxu;
slouken@1904
  1100
    vertices[2].v = maxv;
slouken@1904
  1101
slouken@1903
  1102
    vertices[3].x = minx;
slouken@1903
  1103
    vertices[3].y = maxy;
slouken@1903
  1104
    vertices[3].z = 0.0f;
slouken@1904
  1105
    vertices[3].rhw = 1.0f;
slouken@1987
  1106
    vertices[3].color = color;
slouken@1904
  1107
    vertices[3].u = minu;
slouken@1904
  1108
    vertices[3].v = maxv;
slouken@1903
  1109
slouken@2932
  1110
    D3D_SetBlendMode(data, texture->blendMode);
slouken@1916
  1111
slouken@1985
  1112
    switch (texture->scaleMode) {
slouken@1965
  1113
    case SDL_TEXTURESCALEMODE_NONE:
slouken@1965
  1114
    case SDL_TEXTURESCALEMODE_FAST:
slouken@1917
  1115
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
  1116
                                         D3DTEXF_POINT);
slouken@1917
  1117
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
  1118
                                         D3DTEXF_POINT);
slouken@1917
  1119
        break;
slouken@1965
  1120
    case SDL_TEXTURESCALEMODE_SLOW:
slouken@1917
  1121
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
  1122
                                         D3DTEXF_LINEAR);
slouken@1917
  1123
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
  1124
                                         D3DTEXF_LINEAR);
slouken@1917
  1125
        break;
slouken@1965
  1126
    case SDL_TEXTURESCALEMODE_BEST:
slouken@1917
  1127
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
slouken@1917
  1128
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
  1129
        IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
slouken@1917
  1130
                                         D3DTEXF_GAUSSIANQUAD);
slouken@1917
  1131
        break;
slouken@1917
  1132
    }
slouken@1917
  1133
slouken@1903
  1134
    result =
slouken@2735
  1135
        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
slouken@2735
  1136
                                    texturedata->texture);
slouken@1903
  1137
    if (FAILED(result)) {
slouken@1903
  1138
        D3D_SetError("SetTexture()", result);
slouken@1903
  1139
        return -1;
slouken@1903
  1140
    }
slouken@1903
  1141
    result =
slouken@1903
  1142
        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
slouken@1903
  1143
                                         vertices, sizeof(*vertices));
slouken@1903
  1144
    if (FAILED(result)) {
slouken@1903
  1145
        D3D_SetError("DrawPrimitiveUP()", result);
slouken@1903
  1146
        return -1;
slouken@1903
  1147
    }
slouken@1895
  1148
    return 0;
slouken@1895
  1149
}
slouken@1895
  1150
slouken@3427
  1151
static int
slouken@3427
  1152
D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@3427
  1153
                     void * pixels, int pitch)
slouken@3427
  1154
{
slouken@3427
  1155
    BYTE * pBytes;
slouken@3427
  1156
    D3DLOCKED_RECT lockedRect;
slouken@3427
  1157
    BYTE b, g, r, a;
slouken@3427
  1158
    unsigned long index;
slouken@3427
  1159
    int cur_mouse;
slouken@3427
  1160
    int x, y;
slouken@3427
  1161
slouken@3427
  1162
    LPDIRECT3DSURFACE9 backBuffer;
slouken@3427
  1163
    LPDIRECT3DSURFACE9 pickOffscreenSurface;
slouken@3427
  1164
    D3DSURFACE_DESC desc;
slouken@3427
  1165
slouken@3427
  1166
    D3D_RenderData * data = (D3D_RenderData *) renderer->driverdata;
slouken@3427
  1167
    
slouken@3427
  1168
    IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
slouken@3427
  1169
    
slouken@3427
  1170
    
slouken@3427
  1171
    IDirect3DSurface9_GetDesc(backBuffer, &desc);
slouken@3427
  1172
slouken@3427
  1173
    IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &pickOffscreenSurface, NULL);
slouken@3427
  1174
slouken@3427
  1175
    IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, pickOffscreenSurface);
slouken@3427
  1176
slouken@3427
  1177
    IDirect3DSurface9_LockRect(pickOffscreenSurface, &lockedRect, NULL, D3DLOCK_READONLY);
slouken@3427
  1178
    pBytes = (BYTE*)lockedRect.pBits;
slouken@3427
  1179
    IDirect3DSurface9_UnlockRect(pickOffscreenSurface);
slouken@3427
  1180
slouken@3427
  1181
    // just to debug -->
slouken@3427
  1182
    cur_mouse = SDL_SelectMouse(-1);
slouken@3427
  1183
    SDL_GetMouseState(cur_mouse, &x, &y);
slouken@3427
  1184
    index = (x * 4 + (y * lockedRect.Pitch));
slouken@3427
  1185
slouken@3427
  1186
    b = pBytes[index];
slouken@3427
  1187
    g = pBytes[index+1];
slouken@3427
  1188
    r = pBytes[index+2];
slouken@3427
  1189
    a = pBytes[index+3];
slouken@3427
  1190
    // <--
slouken@3427
  1191
    
slouken@3427
  1192
    return -1;
slouken@3427
  1193
}
slouken@3427
  1194
slouken@1895
  1195
static void
slouken@1913
  1196
D3D_RenderPresent(SDL_Renderer * renderer)
slouken@1895
  1197
{
slouken@1913
  1198
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1900
  1199
    HRESULT result;
slouken@1900
  1200
slouken@1900
  1201
    if (!data->beginScene) {
slouken@1900
  1202
        IDirect3DDevice9_EndScene(data->device);
slouken@1900
  1203
        data->beginScene = SDL_TRUE;
slouken@1900
  1204
    }
slouken@1900
  1205
slouken@1975
  1206
    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
slouken@1975
  1207
    if (result == D3DERR_DEVICELOST) {
slouken@1975
  1208
        /* We'll reset later */
slouken@1975
  1209
        return;
slouken@1975
  1210
    }
slouken@1975
  1211
    if (result == D3DERR_DEVICENOTRESET) {
slouken@1975
  1212
        D3D_Reset(renderer);
slouken@1975
  1213
    }
slouken@1900
  1214
    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
slouken@1900
  1215
    if (FAILED(result)) {
slouken@1900
  1216
        D3D_SetError("Present()", result);
slouken@1900
  1217
    }
slouken@1895
  1218
}
slouken@1895
  1219
slouken@1895
  1220
static void
slouken@1913
  1221
D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
  1222
{
slouken@1913
  1223
    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
slouken@1895
  1224
slouken@1895
  1225
    if (!data) {
slouken@1895
  1226
        return;
slouken@1895
  1227
    }
slouken@2973
  1228
    if (data->yuv) {
slouken@2973
  1229
        SDL_SW_DestroyYUVTexture(data->yuv);
slouken@2973
  1230
    }
slouken@1903
  1231
    if (data->texture) {
slouken@1903
  1232
        IDirect3DTexture9_Release(data->texture);
slouken@1903
  1233
    }
slouken@1895
  1234
    SDL_free(data);
slouken@1895
  1235
    texture->driverdata = NULL;
slouken@1895
  1236
}
slouken@1895
  1237
slouken@1975
  1238
static void
slouken@1913
  1239
D3D_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
  1240
{
slouken@1913
  1241
    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
slouken@1895
  1242
slouken@1895
  1243
    if (data) {
slouken@1900
  1244
        if (data->device) {
slouken@1900
  1245
            IDirect3DDevice9_Release(data->device);
slouken@1900
  1246
        }
slouken@1895
  1247
        SDL_free(data);
slouken@1895
  1248
    }
slouken@1895
  1249
    SDL_free(renderer);
slouken@1895
  1250
}
slouken@1895
  1251
slouken@1895
  1252
#endif /* SDL_VIDEO_RENDER_D3D */
slouken@1895
  1253
slouken@1895
  1254
/* vi: set ts=4 sw=4 expandtab: */