src/render/direct3d/SDL_render_d3d.c
changeset 5195 bb45ecd958d8
parent 5194 69f47f2c1856
child 5203 25ffd4e5255c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/render/direct3d/SDL_render_d3d.c	Sat Feb 05 12:01:11 2011 -0800
     1.3 @@ -0,0 +1,1048 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +#if SDL_VIDEO_RENDER_D3D
    1.28 +
    1.29 +#include "../../core/windows/SDL_windows.h"
    1.30 +
    1.31 +#include "SDL_loadso.h"
    1.32 +#include "SDL_syswm.h"
    1.33 +#include "../SDL_sysrender.h"
    1.34 +
    1.35 +#if SDL_VIDEO_RENDER_D3D
    1.36 +#define D3D_DEBUG_INFO
    1.37 +#include <d3d9.h>
    1.38 +#endif
    1.39 +
    1.40 +#ifdef ASSEMBLE_SHADER
    1.41 +///////////////////////////////////////////////////////////////////////////
    1.42 +// ID3DXBuffer:
    1.43 +// ------------
    1.44 +// The buffer object is used by D3DX to return arbitrary size data.
    1.45 +//
    1.46 +// GetBufferPointer -
    1.47 +//    Returns a pointer to the beginning of the buffer.
    1.48 +//
    1.49 +// GetBufferSize -
    1.50 +//    Returns the size of the buffer, in bytes.
    1.51 +///////////////////////////////////////////////////////////////////////////
    1.52 +
    1.53 +typedef interface ID3DXBuffer ID3DXBuffer;
    1.54 +typedef interface ID3DXBuffer *LPD3DXBUFFER;
    1.55 +
    1.56 +// {8BA5FB08-5195-40e2-AC58-0D989C3A0102}
    1.57 +DEFINE_GUID(IID_ID3DXBuffer, 
    1.58 +0x8ba5fb08, 0x5195, 0x40e2, 0xac, 0x58, 0xd, 0x98, 0x9c, 0x3a, 0x1, 0x2);
    1.59 +
    1.60 +#undef INTERFACE
    1.61 +#define INTERFACE ID3DXBuffer
    1.62 +
    1.63 +typedef interface ID3DXBuffer {
    1.64 +    const struct ID3DXBufferVtbl FAR* lpVtbl;
    1.65 +} ID3DXBuffer;
    1.66 +typedef const struct ID3DXBufferVtbl ID3DXBufferVtbl;
    1.67 +const struct ID3DXBufferVtbl
    1.68 +{
    1.69 +    // IUnknown
    1.70 +    STDMETHOD(QueryInterface)(THIS_ REFIID iid, LPVOID *ppv) PURE;
    1.71 +    STDMETHOD_(ULONG, AddRef)(THIS) PURE;
    1.72 +    STDMETHOD_(ULONG, Release)(THIS) PURE;
    1.73 +
    1.74 +    // ID3DXBuffer
    1.75 +    STDMETHOD_(LPVOID, GetBufferPointer)(THIS) PURE;
    1.76 +    STDMETHOD_(DWORD, GetBufferSize)(THIS) PURE;
    1.77 +};
    1.78 +
    1.79 +HRESULT WINAPI
    1.80 +    D3DXAssembleShader(
    1.81 +        LPCSTR                          pSrcData,
    1.82 +        UINT                            SrcDataLen,
    1.83 +        CONST LPVOID*                   pDefines,
    1.84 +        LPVOID                          pInclude,
    1.85 +        DWORD                           Flags,
    1.86 +        LPD3DXBUFFER*                   ppShader,
    1.87 +        LPD3DXBUFFER*                   ppErrorMsgs);
    1.88 +
    1.89 +#endif /* ASSEMBLE_SHADER */
    1.90 +
    1.91 +
    1.92 +/* Direct3D renderer implementation */
    1.93 +
    1.94 +static SDL_Renderer *D3D_CreateRenderer(SDL_Window * window, Uint32 flags);
    1.95 +static int D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    1.96 +static int D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
    1.97 +                             const SDL_Rect * rect, const void *pixels,
    1.98 +                             int pitch);
    1.99 +static int D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.100 +                           const SDL_Rect * rect, void **pixels, int *pitch);
   1.101 +static void D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
   1.102 +static int D3D_RenderDrawPoints(SDL_Renderer * renderer,
   1.103 +                                const SDL_Point * points, int count);
   1.104 +static int D3D_RenderDrawLines(SDL_Renderer * renderer,
   1.105 +                               const SDL_Point * points, int count);
   1.106 +static int D3D_RenderFillRects(SDL_Renderer * renderer,
   1.107 +                               const SDL_Rect ** rects, int count);
   1.108 +static int D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.109 +                          const SDL_Rect * srcrect, const SDL_Rect * dstrect);
   1.110 +static int D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.111 +                                Uint32 format, void * pixels, int pitch);
   1.112 +static void D3D_RenderPresent(SDL_Renderer * renderer);
   1.113 +static void D3D_DestroyTexture(SDL_Renderer * renderer,
   1.114 +                               SDL_Texture * texture);
   1.115 +static void D3D_DestroyRenderer(SDL_Renderer * renderer);
   1.116 +
   1.117 +
   1.118 +SDL_RenderDriver D3D_RenderDriver = {
   1.119 +    D3D_CreateRenderer,
   1.120 +    {
   1.121 +     "direct3d",
   1.122 +     (SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED),
   1.123 +     1,
   1.124 +     {SDL_PIXELFORMAT_ARGB8888},
   1.125 +     0,
   1.126 +     0}
   1.127 +};
   1.128 +
   1.129 +typedef struct
   1.130 +{
   1.131 +    void* d3dDLL;
   1.132 +    IDirect3D9 *d3d;
   1.133 +    IDirect3DDevice9 *device;
   1.134 +    UINT adapter;
   1.135 +    D3DPRESENT_PARAMETERS pparams;
   1.136 +    SDL_bool beginScene;
   1.137 +} D3D_RenderData;
   1.138 +
   1.139 +typedef struct
   1.140 +{
   1.141 +    IDirect3DTexture9 *texture;
   1.142 +} D3D_TextureData;
   1.143 +
   1.144 +typedef struct
   1.145 +{
   1.146 +    float x, y, z;
   1.147 +    float rhw;
   1.148 +    DWORD color;
   1.149 +    float u, v;
   1.150 +} Vertex;
   1.151 +
   1.152 +static void
   1.153 +D3D_SetError(const char *prefix, HRESULT result)
   1.154 +{
   1.155 +    const char *error;
   1.156 +
   1.157 +    switch (result) {
   1.158 +    case D3DERR_WRONGTEXTUREFORMAT:
   1.159 +        error = "WRONGTEXTUREFORMAT";
   1.160 +        break;
   1.161 +    case D3DERR_UNSUPPORTEDCOLOROPERATION:
   1.162 +        error = "UNSUPPORTEDCOLOROPERATION";
   1.163 +        break;
   1.164 +    case D3DERR_UNSUPPORTEDCOLORARG:
   1.165 +        error = "UNSUPPORTEDCOLORARG";
   1.166 +        break;
   1.167 +    case D3DERR_UNSUPPORTEDALPHAOPERATION:
   1.168 +        error = "UNSUPPORTEDALPHAOPERATION";
   1.169 +        break;
   1.170 +    case D3DERR_UNSUPPORTEDALPHAARG:
   1.171 +        error = "UNSUPPORTEDALPHAARG";
   1.172 +        break;
   1.173 +    case D3DERR_TOOMANYOPERATIONS:
   1.174 +        error = "TOOMANYOPERATIONS";
   1.175 +        break;
   1.176 +    case D3DERR_CONFLICTINGTEXTUREFILTER:
   1.177 +        error = "CONFLICTINGTEXTUREFILTER";
   1.178 +        break;
   1.179 +    case D3DERR_UNSUPPORTEDFACTORVALUE:
   1.180 +        error = "UNSUPPORTEDFACTORVALUE";
   1.181 +        break;
   1.182 +    case D3DERR_CONFLICTINGRENDERSTATE:
   1.183 +        error = "CONFLICTINGRENDERSTATE";
   1.184 +        break;
   1.185 +    case D3DERR_UNSUPPORTEDTEXTUREFILTER:
   1.186 +        error = "UNSUPPORTEDTEXTUREFILTER";
   1.187 +        break;
   1.188 +    case D3DERR_CONFLICTINGTEXTUREPALETTE:
   1.189 +        error = "CONFLICTINGTEXTUREPALETTE";
   1.190 +        break;
   1.191 +    case D3DERR_DRIVERINTERNALERROR:
   1.192 +        error = "DRIVERINTERNALERROR";
   1.193 +        break;
   1.194 +    case D3DERR_NOTFOUND:
   1.195 +        error = "NOTFOUND";
   1.196 +        break;
   1.197 +    case D3DERR_MOREDATA:
   1.198 +        error = "MOREDATA";
   1.199 +        break;
   1.200 +    case D3DERR_DEVICELOST:
   1.201 +        error = "DEVICELOST";
   1.202 +        break;
   1.203 +    case D3DERR_DEVICENOTRESET:
   1.204 +        error = "DEVICENOTRESET";
   1.205 +        break;
   1.206 +    case D3DERR_NOTAVAILABLE:
   1.207 +        error = "NOTAVAILABLE";
   1.208 +        break;
   1.209 +    case D3DERR_OUTOFVIDEOMEMORY:
   1.210 +        error = "OUTOFVIDEOMEMORY";
   1.211 +        break;
   1.212 +    case D3DERR_INVALIDDEVICE:
   1.213 +        error = "INVALIDDEVICE";
   1.214 +        break;
   1.215 +    case D3DERR_INVALIDCALL:
   1.216 +        error = "INVALIDCALL";
   1.217 +        break;
   1.218 +    case D3DERR_DRIVERINVALIDCALL:
   1.219 +        error = "DRIVERINVALIDCALL";
   1.220 +        break;
   1.221 +    case D3DERR_WASSTILLDRAWING:
   1.222 +        error = "WASSTILLDRAWING";
   1.223 +        break;
   1.224 +    default:
   1.225 +        error = "UNKNOWN";
   1.226 +        break;
   1.227 +    }
   1.228 +    SDL_SetError("%s: %s", prefix, error);
   1.229 +}
   1.230 +
   1.231 +static D3DFORMAT
   1.232 +PixelFormatToD3DFMT(Uint32 format)
   1.233 +{
   1.234 +    switch (format) {
   1.235 +    case SDL_PIXELFORMAT_RGB565:
   1.236 +        return D3DFMT_R5G6B5;
   1.237 +    case SDL_PIXELFORMAT_RGB888:
   1.238 +        return D3DFMT_X8R8G8B8;
   1.239 +    case SDL_PIXELFORMAT_ARGB8888:
   1.240 +        return D3DFMT_A8R8G8B8;
   1.241 +    default:
   1.242 +        return D3DFMT_UNKNOWN;
   1.243 +    }
   1.244 +}
   1.245 +
   1.246 +static Uint32
   1.247 +D3DFMTToPixelFormat(D3DFORMAT format)
   1.248 +{
   1.249 +    switch (format) {
   1.250 +    case D3DFMT_R5G6B5:
   1.251 +        return SDL_PIXELFORMAT_RGB565;
   1.252 +    case D3DFMT_X8R8G8B8:
   1.253 +        return SDL_PIXELFORMAT_RGB888;
   1.254 +    case D3DFMT_A8R8G8B8:
   1.255 +        return SDL_PIXELFORMAT_ARGB8888;
   1.256 +    default:
   1.257 +        return SDL_PIXELFORMAT_UNKNOWN;
   1.258 +    }
   1.259 +}
   1.260 +
   1.261 +SDL_Renderer *
   1.262 +D3D_CreateRenderer(SDL_Window * window, Uint32 flags)
   1.263 +{
   1.264 +    SDL_Renderer *renderer;
   1.265 +    D3D_RenderData *data;
   1.266 +    SDL_SysWMinfo windowinfo;
   1.267 +    HRESULT result;
   1.268 +    D3DPRESENT_PARAMETERS pparams;
   1.269 +    IDirect3DSwapChain9 *chain;
   1.270 +    D3DCAPS9 caps;
   1.271 +    Uint32 window_flags;
   1.272 +    int w, h;
   1.273 +    SDL_DisplayMode fullscreen_mode;
   1.274 +
   1.275 +    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
   1.276 +    if (!renderer) {
   1.277 +        SDL_OutOfMemory();
   1.278 +        return NULL;
   1.279 +    }
   1.280 +
   1.281 +    data = (D3D_RenderData *) SDL_calloc(1, sizeof(*data));
   1.282 +    if (!data) {
   1.283 +        SDL_free(renderer);
   1.284 +        SDL_OutOfMemory();
   1.285 +        return NULL;
   1.286 +    }
   1.287 +
   1.288 +    data->d3dDLL = SDL_LoadObject("D3D9.DLL");
   1.289 +    if (data->d3dDLL) {
   1.290 +        IDirect3D9 *(WINAPI * D3DCreate) (UINT SDKVersion);
   1.291 +
   1.292 +        D3DCreate =
   1.293 +            (IDirect3D9 * (WINAPI *) (UINT)) SDL_LoadFunction(data->d3dDLL,
   1.294 +                                                            "Direct3DCreate9");
   1.295 +        if (D3DCreate) {
   1.296 +            data->d3d = D3DCreate(D3D_SDK_VERSION);
   1.297 +        }
   1.298 +        if (!data->d3d) {
   1.299 +            SDL_UnloadObject(data->d3dDLL);
   1.300 +            data->d3dDLL = NULL;
   1.301 +        }
   1.302 +    }
   1.303 +    if (!data->d3d) {
   1.304 +        SDL_free(renderer);
   1.305 +        SDL_free(data);
   1.306 +        SDL_SetError("Unable to create Direct3D interface");
   1.307 +        return NULL;
   1.308 +    }
   1.309 +
   1.310 +    renderer->CreateTexture = D3D_CreateTexture;
   1.311 +    renderer->UpdateTexture = D3D_UpdateTexture;
   1.312 +    renderer->LockTexture = D3D_LockTexture;
   1.313 +    renderer->UnlockTexture = D3D_UnlockTexture;
   1.314 +    renderer->RenderDrawPoints = D3D_RenderDrawPoints;
   1.315 +    renderer->RenderDrawLines = D3D_RenderDrawLines;
   1.316 +    renderer->RenderFillRects = D3D_RenderFillRects;
   1.317 +    renderer->RenderCopy = D3D_RenderCopy;
   1.318 +    renderer->RenderReadPixels = D3D_RenderReadPixels;
   1.319 +    renderer->RenderPresent = D3D_RenderPresent;
   1.320 +    renderer->DestroyTexture = D3D_DestroyTexture;
   1.321 +    renderer->DestroyRenderer = D3D_DestroyRenderer;
   1.322 +    renderer->info = D3D_RenderDriver.info;
   1.323 +    renderer->driverdata = data;
   1.324 +
   1.325 +    renderer->info.flags = SDL_RENDERER_ACCELERATED;
   1.326 +
   1.327 +    SDL_VERSION(&windowinfo.version);
   1.328 +    SDL_GetWindowWMInfo(window, &windowinfo);
   1.329 +
   1.330 +    window_flags = SDL_GetWindowFlags(window);
   1.331 +    SDL_GetWindowSize(window, &w, &h);
   1.332 +    SDL_GetWindowDisplayMode(window, &fullscreen_mode);
   1.333 +
   1.334 +    SDL_zero(pparams);
   1.335 +    pparams.hDeviceWindow = windowinfo.info.win.window;
   1.336 +    pparams.BackBufferWidth = w;
   1.337 +    pparams.BackBufferHeight = h;
   1.338 +    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   1.339 +        pparams.BackBufferFormat =
   1.340 +            PixelFormatToD3DFMT(fullscreen_mode.format);
   1.341 +    } else {
   1.342 +        pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   1.343 +    }
   1.344 +    pparams.BackBufferCount = 1;
   1.345 +    pparams.SwapEffect = D3DSWAPEFFECT_DISCARD;
   1.346 +
   1.347 +    if (window_flags & SDL_WINDOW_FULLSCREEN) {
   1.348 +        pparams.Windowed = FALSE;
   1.349 +        pparams.FullScreen_RefreshRateInHz =
   1.350 +            fullscreen_mode.refresh_rate;
   1.351 +    } else {
   1.352 +        pparams.Windowed = TRUE;
   1.353 +        pparams.FullScreen_RefreshRateInHz = 0;
   1.354 +    }
   1.355 +    if (flags & SDL_RENDERER_PRESENTVSYNC) {
   1.356 +        pparams.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
   1.357 +    } else {
   1.358 +        pparams.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
   1.359 +    }
   1.360 +
   1.361 +    /* FIXME: Which adapter? */
   1.362 +    data->adapter = D3DADAPTER_DEFAULT;
   1.363 +    IDirect3D9_GetDeviceCaps(data->d3d, data->adapter, D3DDEVTYPE_HAL, &caps);
   1.364 +
   1.365 +    result = IDirect3D9_CreateDevice(data->d3d, data->adapter,
   1.366 +                                     D3DDEVTYPE_HAL,
   1.367 +                                     pparams.hDeviceWindow,
   1.368 +                                     (caps.
   1.369 +                                      DevCaps &
   1.370 +                                      D3DDEVCAPS_HWTRANSFORMANDLIGHT) ?
   1.371 +                                     D3DCREATE_HARDWARE_VERTEXPROCESSING :
   1.372 +                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING,
   1.373 +                                     &pparams, &data->device);
   1.374 +    if (FAILED(result)) {
   1.375 +        D3D_DestroyRenderer(renderer);
   1.376 +        D3D_SetError("CreateDevice()", result);
   1.377 +        return NULL;
   1.378 +    }
   1.379 +    data->beginScene = SDL_TRUE;
   1.380 +
   1.381 +    /* Get presentation parameters to fill info */
   1.382 +    result = IDirect3DDevice9_GetSwapChain(data->device, 0, &chain);
   1.383 +    if (FAILED(result)) {
   1.384 +        D3D_DestroyRenderer(renderer);
   1.385 +        D3D_SetError("GetSwapChain()", result);
   1.386 +        return NULL;
   1.387 +    }
   1.388 +    result = IDirect3DSwapChain9_GetPresentParameters(chain, &pparams);
   1.389 +    if (FAILED(result)) {
   1.390 +        IDirect3DSwapChain9_Release(chain);
   1.391 +        D3D_DestroyRenderer(renderer);
   1.392 +        D3D_SetError("GetPresentParameters()", result);
   1.393 +        return NULL;
   1.394 +    }
   1.395 +    IDirect3DSwapChain9_Release(chain);
   1.396 +    if (pparams.PresentationInterval == D3DPRESENT_INTERVAL_ONE) {
   1.397 +        renderer->info.flags |= SDL_RENDERER_PRESENTVSYNC;
   1.398 +    }
   1.399 +    data->pparams = pparams;
   1.400 +
   1.401 +    IDirect3DDevice9_GetDeviceCaps(data->device, &caps);
   1.402 +    renderer->info.max_texture_width = caps.MaxTextureWidth;
   1.403 +    renderer->info.max_texture_height = caps.MaxTextureHeight;
   1.404 +
   1.405 +    /* Set up parameters for rendering */
   1.406 +    IDirect3DDevice9_SetVertexShader(data->device, NULL);
   1.407 +    IDirect3DDevice9_SetFVF(data->device,
   1.408 +                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   1.409 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_ZENABLE, D3DZB_FALSE);
   1.410 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   1.411 +                                    D3DCULL_NONE);
   1.412 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   1.413 +    /* Enable color modulation by diffuse color */
   1.414 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLOROP,
   1.415 +                                          D3DTOP_MODULATE);
   1.416 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG1,
   1.417 +                                          D3DTA_TEXTURE);
   1.418 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_COLORARG2,
   1.419 +                                          D3DTA_DIFFUSE);
   1.420 +    /* Enable alpha modulation by diffuse alpha */
   1.421 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAOP,
   1.422 +                                          D3DTOP_MODULATE);
   1.423 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG1,
   1.424 +                                          D3DTA_TEXTURE);
   1.425 +    IDirect3DDevice9_SetTextureStageState(data->device, 0, D3DTSS_ALPHAARG2,
   1.426 +                                          D3DTA_DIFFUSE);
   1.427 +    /* Disable second texture stage, since we're done */
   1.428 +    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_COLOROP,
   1.429 +                                          D3DTOP_DISABLE);
   1.430 +    IDirect3DDevice9_SetTextureStageState(data->device, 1, D3DTSS_ALPHAOP,
   1.431 +                                          D3DTOP_DISABLE);
   1.432 +
   1.433 +    return renderer;
   1.434 +}
   1.435 +
   1.436 +static int
   1.437 +D3D_Reset(SDL_Renderer * renderer)
   1.438 +{
   1.439 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.440 +    HRESULT result;
   1.441 +
   1.442 +    result = IDirect3DDevice9_Reset(data->device, &data->pparams);
   1.443 +    if (FAILED(result)) {
   1.444 +        if (result == D3DERR_DEVICELOST) {
   1.445 +            /* Don't worry about it, we'll reset later... */
   1.446 +            return 0;
   1.447 +        } else {
   1.448 +            D3D_SetError("Reset()", result);
   1.449 +            return -1;
   1.450 +        }
   1.451 +    }
   1.452 +    IDirect3DDevice9_SetVertexShader(data->device, NULL);
   1.453 +    IDirect3DDevice9_SetFVF(data->device,
   1.454 +                            D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1);
   1.455 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_CULLMODE,
   1.456 +                                    D3DCULL_NONE);
   1.457 +    IDirect3DDevice9_SetRenderState(data->device, D3DRS_LIGHTING, FALSE);
   1.458 +    return 0;
   1.459 +}
   1.460 +
   1.461 +/* FIXME: This needs to be called... when? */
   1.462 +#if 0
   1.463 +static int
   1.464 +D3D_DisplayModeChanged(SDL_Renderer * renderer)
   1.465 +{
   1.466 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.467 +    SDL_Window *window = renderer->window;
   1.468 +    SDL_VideoDisplay *display = window->display;
   1.469 +
   1.470 +    data->pparams.BackBufferWidth = window->w;
   1.471 +    data->pparams.BackBufferHeight = window->h;
   1.472 +    if (window->flags & SDL_WINDOW_FULLSCREEN) {
   1.473 +        data->pparams.BackBufferFormat =
   1.474 +            PixelFormatToD3DFMT(window->fullscreen_mode.format);
   1.475 +    } else {
   1.476 +        data->pparams.BackBufferFormat = D3DFMT_UNKNOWN;
   1.477 +    }
   1.478 +    return D3D_Reset(renderer);
   1.479 +}
   1.480 +#endif
   1.481 +
   1.482 +static int
   1.483 +D3D_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.484 +{
   1.485 +    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   1.486 +    SDL_Window *window = renderer->window;
   1.487 +    D3DFORMAT display_format = renderdata->pparams.BackBufferFormat;
   1.488 +    D3D_TextureData *data;
   1.489 +    D3DPOOL pool;
   1.490 +    DWORD usage;
   1.491 +    HRESULT result;
   1.492 +
   1.493 +    data = (D3D_TextureData *) SDL_calloc(1, sizeof(*data));
   1.494 +    if (!data) {
   1.495 +        SDL_OutOfMemory();
   1.496 +        return -1;
   1.497 +    }
   1.498 +
   1.499 +    texture->driverdata = data;
   1.500 +
   1.501 +#ifdef USE_DYNAMIC_TEXTURE
   1.502 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING) {
   1.503 +        pool = D3DPOOL_DEFAULT;
   1.504 +        usage = D3DUSAGE_DYNAMIC;
   1.505 +    } else
   1.506 +#endif
   1.507 +    {
   1.508 +        pool = D3DPOOL_MANAGED;
   1.509 +        usage = 0;
   1.510 +    }
   1.511 +
   1.512 +    result =
   1.513 +        IDirect3DDevice9_CreateTexture(renderdata->device, texture->w,
   1.514 +                                       texture->h, 1, usage,
   1.515 +                                       PixelFormatToD3DFMT(texture->format),
   1.516 +                                       pool, &data->texture, NULL);
   1.517 +    if (FAILED(result)) {
   1.518 +        D3D_SetError("CreateTexture()", result);
   1.519 +        return -1;
   1.520 +    }
   1.521 +
   1.522 +    return 0;
   1.523 +}
   1.524 +
   1.525 +static int
   1.526 +D3D_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.527 +                  const SDL_Rect * rect, const void *pixels, int pitch)
   1.528 +{
   1.529 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.530 +    D3D_RenderData *renderdata = (D3D_RenderData *) renderer->driverdata;
   1.531 +    RECT d3drect;
   1.532 +    D3DLOCKED_RECT locked;
   1.533 +    const Uint8 *src;
   1.534 +    Uint8 *dst;
   1.535 +    int row, length;
   1.536 +    HRESULT result;
   1.537 +
   1.538 +#ifdef USE_DYNAMIC_TEXTURE
   1.539 +    if (texture->access == SDL_TEXTUREACCESS_STREAMING &&
   1.540 +        rect->x == 0 && rect->y == 0 &&
   1.541 +        rect->w == texture->w && rect->h == texture->h) {
   1.542 +        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, NULL, D3DLOCK_DISCARD);
   1.543 +    } else
   1.544 +#endif
   1.545 +    {
   1.546 +        d3drect.left = rect->x;
   1.547 +        d3drect.right = rect->x + rect->w;
   1.548 +        d3drect.top = rect->y;
   1.549 +        d3drect.bottom = rect->y + rect->h;
   1.550 +        result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   1.551 +    }
   1.552 +
   1.553 +    if (FAILED(result)) {
   1.554 +        D3D_SetError("LockRect()", result);
   1.555 +        return -1;
   1.556 +    }
   1.557 +
   1.558 +    src = pixels;
   1.559 +    dst = locked.pBits;
   1.560 +    length = rect->w * SDL_BYTESPERPIXEL(texture->format);
   1.561 +    if (length == pitch && length == locked.Pitch) {
   1.562 +        SDL_memcpy(dst, src, length*rect->h);
   1.563 +    } else {
   1.564 +        for (row = 0; row < rect->h; ++row) {
   1.565 +            SDL_memcpy(dst, src, length);
   1.566 +            src += pitch;
   1.567 +            dst += locked.Pitch;
   1.568 +        }
   1.569 +    }
   1.570 +    IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.571 +
   1.572 +    return 0;
   1.573 +}
   1.574 +
   1.575 +static int
   1.576 +D3D_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
   1.577 +                const SDL_Rect * rect, void **pixels, int *pitch)
   1.578 +{
   1.579 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.580 +    RECT d3drect;
   1.581 +    D3DLOCKED_RECT locked;
   1.582 +    HRESULT result;
   1.583 +
   1.584 +    d3drect.left = rect->x;
   1.585 +    d3drect.right = rect->x + rect->w;
   1.586 +    d3drect.top = rect->y;
   1.587 +    d3drect.bottom = rect->y + rect->h;
   1.588 +
   1.589 +    result = IDirect3DTexture9_LockRect(data->texture, 0, &locked, &d3drect, 0);
   1.590 +    if (FAILED(result)) {
   1.591 +        D3D_SetError("LockRect()", result);
   1.592 +        return -1;
   1.593 +    }
   1.594 +    *pixels = locked.pBits;
   1.595 +    *pitch = locked.Pitch;
   1.596 +    return 0;
   1.597 +}
   1.598 +
   1.599 +static void
   1.600 +D3D_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
   1.601 +{
   1.602 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
   1.603 +
   1.604 +    IDirect3DTexture9_UnlockRect(data->texture, 0);
   1.605 +}
   1.606 +
   1.607 +static void
   1.608 +D3D_SetBlendMode(D3D_RenderData * data, int blendMode)
   1.609 +{
   1.610 +    switch (blendMode) {
   1.611 +    case SDL_BLENDMODE_NONE:
   1.612 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   1.613 +                                        FALSE);
   1.614 +        break;
   1.615 +    case SDL_BLENDMODE_BLEND:
   1.616 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   1.617 +                                        TRUE);
   1.618 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   1.619 +                                        D3DBLEND_SRCALPHA);
   1.620 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   1.621 +                                        D3DBLEND_INVSRCALPHA);
   1.622 +        break;
   1.623 +    case SDL_BLENDMODE_ADD:
   1.624 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   1.625 +                                        TRUE);
   1.626 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   1.627 +                                        D3DBLEND_SRCALPHA);
   1.628 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   1.629 +                                        D3DBLEND_ONE);
   1.630 +        break;
   1.631 +    case SDL_BLENDMODE_MOD:
   1.632 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_ALPHABLENDENABLE,
   1.633 +                                        TRUE);
   1.634 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_SRCBLEND,
   1.635 +                                        D3DBLEND_ZERO);
   1.636 +        IDirect3DDevice9_SetRenderState(data->device, D3DRS_DESTBLEND,
   1.637 +                                        D3DBLEND_SRCCOLOR);
   1.638 +        break;
   1.639 +    }
   1.640 +}
   1.641 +
   1.642 +static int
   1.643 +D3D_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
   1.644 +                     int count)
   1.645 +{
   1.646 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.647 +    DWORD color;
   1.648 +    Vertex *vertices;
   1.649 +    int i;
   1.650 +    HRESULT result;
   1.651 +
   1.652 +    if (data->beginScene) {
   1.653 +        IDirect3DDevice9_BeginScene(data->device);
   1.654 +        data->beginScene = SDL_FALSE;
   1.655 +    }
   1.656 +
   1.657 +    D3D_SetBlendMode(data, renderer->blendMode);
   1.658 +
   1.659 +    result =
   1.660 +        IDirect3DDevice9_SetTexture(data->device, 0,
   1.661 +                                    (IDirect3DBaseTexture9 *) 0);
   1.662 +    if (FAILED(result)) {
   1.663 +        D3D_SetError("SetTexture()", result);
   1.664 +        return -1;
   1.665 +    }
   1.666 +
   1.667 +    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   1.668 +
   1.669 +    vertices = SDL_stack_alloc(Vertex, count);
   1.670 +    for (i = 0; i < count; ++i) {
   1.671 +        vertices[i].x = (float) points[i].x;
   1.672 +        vertices[i].y = (float) points[i].y;
   1.673 +        vertices[i].z = 0.0f;
   1.674 +        vertices[i].rhw = 1.0f;
   1.675 +        vertices[i].color = color;
   1.676 +        vertices[i].u = 0.0f;
   1.677 +        vertices[i].v = 0.0f;
   1.678 +    }
   1.679 +    result =
   1.680 +        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, count,
   1.681 +                                         vertices, sizeof(*vertices));
   1.682 +    SDL_stack_free(vertices);
   1.683 +    if (FAILED(result)) {
   1.684 +        D3D_SetError("DrawPrimitiveUP()", result);
   1.685 +        return -1;
   1.686 +    }
   1.687 +    return 0;
   1.688 +}
   1.689 +
   1.690 +static int
   1.691 +D3D_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
   1.692 +                    int count)
   1.693 +{
   1.694 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.695 +    DWORD color;
   1.696 +    Vertex *vertices;
   1.697 +    int i;
   1.698 +    HRESULT result;
   1.699 +
   1.700 +    if (data->beginScene) {
   1.701 +        IDirect3DDevice9_BeginScene(data->device);
   1.702 +        data->beginScene = SDL_FALSE;
   1.703 +    }
   1.704 +
   1.705 +    D3D_SetBlendMode(data, renderer->blendMode);
   1.706 +
   1.707 +    result =
   1.708 +        IDirect3DDevice9_SetTexture(data->device, 0,
   1.709 +                                    (IDirect3DBaseTexture9 *) 0);
   1.710 +    if (FAILED(result)) {
   1.711 +        D3D_SetError("SetTexture()", result);
   1.712 +        return -1;
   1.713 +    }
   1.714 +
   1.715 +    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   1.716 +
   1.717 +    vertices = SDL_stack_alloc(Vertex, count);
   1.718 +    for (i = 0; i < count; ++i) {
   1.719 +        vertices[i].x = (float) points[i].x;
   1.720 +        vertices[i].y = (float) points[i].y;
   1.721 +        vertices[i].z = 0.0f;
   1.722 +        vertices[i].rhw = 1.0f;
   1.723 +        vertices[i].color = color;
   1.724 +        vertices[i].u = 0.0f;
   1.725 +        vertices[i].v = 0.0f;
   1.726 +    }
   1.727 +    result =
   1.728 +        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_LINESTRIP, count-1,
   1.729 +                                         vertices, sizeof(*vertices));
   1.730 +
   1.731 +    /* DirectX 9 has the same line rasterization semantics as GDI,
   1.732 +       so we need to close the endpoint of the line */
   1.733 +    if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
   1.734 +        vertices[0].x = (float) points[count-1].x;
   1.735 +        vertices[0].y = (float) points[count-1].y;
   1.736 +        result = IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_POINTLIST, 1, vertices, sizeof(*vertices));
   1.737 +    }
   1.738 +
   1.739 +    SDL_stack_free(vertices);
   1.740 +    if (FAILED(result)) {
   1.741 +        D3D_SetError("DrawPrimitiveUP()", result);
   1.742 +        return -1;
   1.743 +    }
   1.744 +    return 0;
   1.745 +}
   1.746 +
   1.747 +static int
   1.748 +D3D_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
   1.749 +                    int count)
   1.750 +{
   1.751 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.752 +    DWORD color;
   1.753 +    int i;
   1.754 +    float minx, miny, maxx, maxy;
   1.755 +    Vertex vertices[4];
   1.756 +    HRESULT result;
   1.757 +
   1.758 +    if (data->beginScene) {
   1.759 +        IDirect3DDevice9_BeginScene(data->device);
   1.760 +        data->beginScene = SDL_FALSE;
   1.761 +    }
   1.762 +
   1.763 +    D3D_SetBlendMode(data, renderer->blendMode);
   1.764 +
   1.765 +    result =
   1.766 +        IDirect3DDevice9_SetTexture(data->device, 0,
   1.767 +                                    (IDirect3DBaseTexture9 *) 0);
   1.768 +    if (FAILED(result)) {
   1.769 +        D3D_SetError("SetTexture()", result);
   1.770 +        return -1;
   1.771 +    }
   1.772 +
   1.773 +    color = D3DCOLOR_ARGB(renderer->a, renderer->r, renderer->g, renderer->b);
   1.774 +
   1.775 +    for (i = 0; i < count; ++i) {
   1.776 +        const SDL_Rect *rect = rects[i];
   1.777 +
   1.778 +        minx = (float) rect->x;
   1.779 +        miny = (float) rect->y;
   1.780 +        maxx = (float) rect->x + rect->w;
   1.781 +        maxy = (float) rect->y + rect->h;
   1.782 +
   1.783 +        vertices[0].x = minx;
   1.784 +        vertices[0].y = miny;
   1.785 +        vertices[0].z = 0.0f;
   1.786 +        vertices[0].rhw = 1.0f;
   1.787 +        vertices[0].color = color;
   1.788 +        vertices[0].u = 0.0f;
   1.789 +        vertices[0].v = 0.0f;
   1.790 +
   1.791 +        vertices[1].x = maxx;
   1.792 +        vertices[1].y = miny;
   1.793 +        vertices[1].z = 0.0f;
   1.794 +        vertices[1].rhw = 1.0f;
   1.795 +        vertices[1].color = color;
   1.796 +        vertices[1].u = 0.0f;
   1.797 +        vertices[1].v = 0.0f;
   1.798 +
   1.799 +        vertices[2].x = maxx;
   1.800 +        vertices[2].y = maxy;
   1.801 +        vertices[2].z = 0.0f;
   1.802 +        vertices[2].rhw = 1.0f;
   1.803 +        vertices[2].color = color;
   1.804 +        vertices[2].u = 0.0f;
   1.805 +        vertices[2].v = 0.0f;
   1.806 +
   1.807 +        vertices[3].x = minx;
   1.808 +        vertices[3].y = maxy;
   1.809 +        vertices[3].z = 0.0f;
   1.810 +        vertices[3].rhw = 1.0f;
   1.811 +        vertices[3].color = color;
   1.812 +        vertices[3].u = 0.0f;
   1.813 +        vertices[3].v = 0.0f;
   1.814 +
   1.815 +        result =
   1.816 +            IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN,
   1.817 +                                             2, vertices, sizeof(*vertices));
   1.818 +        if (FAILED(result)) {
   1.819 +            D3D_SetError("DrawPrimitiveUP()", result);
   1.820 +            return -1;
   1.821 +        }
   1.822 +    }
   1.823 +    return 0;
   1.824 +}
   1.825 +
   1.826 +static int
   1.827 +D3D_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
   1.828 +               const SDL_Rect * srcrect, const SDL_Rect * dstrect)
   1.829 +{
   1.830 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.831 +    D3D_TextureData *texturedata = (D3D_TextureData *) texture->driverdata;
   1.832 +    LPDIRECT3DPIXELSHADER9 shader = NULL;
   1.833 +    float minx, miny, maxx, maxy;
   1.834 +    float minu, maxu, minv, maxv;
   1.835 +    DWORD color;
   1.836 +    Vertex vertices[4];
   1.837 +    HRESULT result;
   1.838 +
   1.839 +    if (data->beginScene) {
   1.840 +        IDirect3DDevice9_BeginScene(data->device);
   1.841 +        data->beginScene = SDL_FALSE;
   1.842 +    }
   1.843 +
   1.844 +    minx = (float) dstrect->x - 0.5f;
   1.845 +    miny = (float) dstrect->y - 0.5f;
   1.846 +    maxx = (float) dstrect->x + dstrect->w - 0.5f;
   1.847 +    maxy = (float) dstrect->y + dstrect->h - 0.5f;
   1.848 +
   1.849 +    minu = (float) srcrect->x / texture->w;
   1.850 +    maxu = (float) (srcrect->x + srcrect->w) / texture->w;
   1.851 +    minv = (float) srcrect->y / texture->h;
   1.852 +    maxv = (float) (srcrect->y + srcrect->h) / texture->h;
   1.853 +
   1.854 +    color = D3DCOLOR_ARGB(texture->a, texture->r, texture->g, texture->b);
   1.855 +
   1.856 +    vertices[0].x = minx;
   1.857 +    vertices[0].y = miny;
   1.858 +    vertices[0].z = 0.0f;
   1.859 +    vertices[0].rhw = 1.0f;
   1.860 +    vertices[0].color = color;
   1.861 +    vertices[0].u = minu;
   1.862 +    vertices[0].v = minv;
   1.863 +
   1.864 +    vertices[1].x = maxx;
   1.865 +    vertices[1].y = miny;
   1.866 +    vertices[1].z = 0.0f;
   1.867 +    vertices[1].rhw = 1.0f;
   1.868 +    vertices[1].color = color;
   1.869 +    vertices[1].u = maxu;
   1.870 +    vertices[1].v = minv;
   1.871 +
   1.872 +    vertices[2].x = maxx;
   1.873 +    vertices[2].y = maxy;
   1.874 +    vertices[2].z = 0.0f;
   1.875 +    vertices[2].rhw = 1.0f;
   1.876 +    vertices[2].color = color;
   1.877 +    vertices[2].u = maxu;
   1.878 +    vertices[2].v = maxv;
   1.879 +
   1.880 +    vertices[3].x = minx;
   1.881 +    vertices[3].y = maxy;
   1.882 +    vertices[3].z = 0.0f;
   1.883 +    vertices[3].rhw = 1.0f;
   1.884 +    vertices[3].color = color;
   1.885 +    vertices[3].u = minu;
   1.886 +    vertices[3].v = maxv;
   1.887 +
   1.888 +    D3D_SetBlendMode(data, texture->blendMode);
   1.889 +
   1.890 +    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MINFILTER,
   1.891 +                                     D3DTEXF_LINEAR);
   1.892 +    IDirect3DDevice9_SetSamplerState(data->device, 0, D3DSAMP_MAGFILTER,
   1.893 +                                     D3DTEXF_LINEAR);
   1.894 +
   1.895 +    result =
   1.896 +        IDirect3DDevice9_SetTexture(data->device, 0, (IDirect3DBaseTexture9 *)
   1.897 +                                    texturedata->texture);
   1.898 +    if (FAILED(result)) {
   1.899 +        D3D_SetError("SetTexture()", result);
   1.900 +        return -1;
   1.901 +    }
   1.902 +    if (shader) {
   1.903 +        result = IDirect3DDevice9_SetPixelShader(data->device, shader);
   1.904 +        if (FAILED(result)) {
   1.905 +            D3D_SetError("SetShader()", result);
   1.906 +            return -1;
   1.907 +        }
   1.908 +    }
   1.909 +    result =
   1.910 +        IDirect3DDevice9_DrawPrimitiveUP(data->device, D3DPT_TRIANGLEFAN, 2,
   1.911 +                                         vertices, sizeof(*vertices));
   1.912 +    if (FAILED(result)) {
   1.913 +        D3D_SetError("DrawPrimitiveUP()", result);
   1.914 +        return -1;
   1.915 +    }
   1.916 +    if (shader) {
   1.917 +        result = IDirect3DDevice9_SetPixelShader(data->device, NULL);
   1.918 +        if (FAILED(result)) {
   1.919 +            D3D_SetError("SetShader()", result);
   1.920 +            return -1;
   1.921 +        }
   1.922 +    }
   1.923 +    return 0;
   1.924 +}
   1.925 +
   1.926 +static int
   1.927 +D3D_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
   1.928 +                     Uint32 format, void * pixels, int pitch)
   1.929 +{
   1.930 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.931 +    D3DSURFACE_DESC desc;
   1.932 +    LPDIRECT3DSURFACE9 backBuffer;
   1.933 +    LPDIRECT3DSURFACE9 surface;
   1.934 +    RECT d3drect;
   1.935 +    D3DLOCKED_RECT locked;
   1.936 +    HRESULT result;
   1.937 +
   1.938 +    result = IDirect3DDevice9_GetBackBuffer(data->device, 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
   1.939 +    if (FAILED(result)) {
   1.940 +        D3D_SetError("GetBackBuffer()", result);
   1.941 +        return -1;
   1.942 +    }
   1.943 +
   1.944 +    result = IDirect3DSurface9_GetDesc(backBuffer, &desc);
   1.945 +    if (FAILED(result)) {
   1.946 +        D3D_SetError("GetDesc()", result);
   1.947 +        IDirect3DSurface9_Release(backBuffer);
   1.948 +        return -1;
   1.949 +    }
   1.950 +
   1.951 +    result = IDirect3DDevice9_CreateOffscreenPlainSurface(data->device, desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &surface, NULL);
   1.952 +    if (FAILED(result)) {
   1.953 +        D3D_SetError("CreateOffscreenPlainSurface()", result);
   1.954 +        IDirect3DSurface9_Release(backBuffer);
   1.955 +        return -1;
   1.956 +    }
   1.957 +
   1.958 +    result = IDirect3DDevice9_GetRenderTargetData(data->device, backBuffer, surface);
   1.959 +    if (FAILED(result)) {
   1.960 +        D3D_SetError("GetRenderTargetData()", result);
   1.961 +        IDirect3DSurface9_Release(surface);
   1.962 +        IDirect3DSurface9_Release(backBuffer);
   1.963 +        return -1;
   1.964 +    }
   1.965 +
   1.966 +    d3drect.left = rect->x;
   1.967 +    d3drect.right = rect->x + rect->w;
   1.968 +    d3drect.top = rect->y;
   1.969 +    d3drect.bottom = rect->y + rect->h;
   1.970 +
   1.971 +    result = IDirect3DSurface9_LockRect(surface, &locked, &d3drect, D3DLOCK_READONLY);
   1.972 +    if (FAILED(result)) {
   1.973 +        D3D_SetError("LockRect()", result);
   1.974 +        IDirect3DSurface9_Release(surface);
   1.975 +        IDirect3DSurface9_Release(backBuffer);
   1.976 +        return -1;
   1.977 +    }
   1.978 +
   1.979 +    SDL_ConvertPixels(rect->w, rect->h,
   1.980 +                      D3DFMTToPixelFormat(desc.Format), locked.pBits, locked.Pitch,
   1.981 +                      format, pixels, pitch);
   1.982 +
   1.983 +    IDirect3DSurface9_UnlockRect(surface);
   1.984 +
   1.985 +    IDirect3DSurface9_Release(surface);
   1.986 +    IDirect3DSurface9_Release(backBuffer);
   1.987 +
   1.988 +    return 0;
   1.989 +}
   1.990 +
   1.991 +static void
   1.992 +D3D_RenderPresent(SDL_Renderer * renderer)
   1.993 +{
   1.994 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
   1.995 +    HRESULT result;
   1.996 +
   1.997 +    if (!data->beginScene) {
   1.998 +        IDirect3DDevice9_EndScene(data->device);
   1.999 +        data->beginScene = SDL_TRUE;
  1.1000 +    }
  1.1001 +
  1.1002 +    result = IDirect3DDevice9_TestCooperativeLevel(data->device);
  1.1003 +    if (result == D3DERR_DEVICELOST) {
  1.1004 +        /* We'll reset later */
  1.1005 +        return;
  1.1006 +    }
  1.1007 +    if (result == D3DERR_DEVICENOTRESET) {
  1.1008 +        D3D_Reset(renderer);
  1.1009 +    }
  1.1010 +    result = IDirect3DDevice9_Present(data->device, NULL, NULL, NULL, NULL);
  1.1011 +    if (FAILED(result)) {
  1.1012 +        D3D_SetError("Present()", result);
  1.1013 +    }
  1.1014 +}
  1.1015 +
  1.1016 +static void
  1.1017 +D3D_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
  1.1018 +{
  1.1019 +    D3D_TextureData *data = (D3D_TextureData *) texture->driverdata;
  1.1020 +
  1.1021 +    if (!data) {
  1.1022 +        return;
  1.1023 +    }
  1.1024 +    if (data->texture) {
  1.1025 +        IDirect3DTexture9_Release(data->texture);
  1.1026 +    }
  1.1027 +    SDL_free(data);
  1.1028 +    texture->driverdata = NULL;
  1.1029 +}
  1.1030 +
  1.1031 +static void
  1.1032 +D3D_DestroyRenderer(SDL_Renderer * renderer)
  1.1033 +{
  1.1034 +    D3D_RenderData *data = (D3D_RenderData *) renderer->driverdata;
  1.1035 +
  1.1036 +    if (data) {
  1.1037 +        if (data->device) {
  1.1038 +            IDirect3DDevice9_Release(data->device);
  1.1039 +        }
  1.1040 +        if (data->d3d) {
  1.1041 +            IDirect3D9_Release(data->d3d);
  1.1042 +            SDL_UnloadObject(data->d3dDLL);
  1.1043 +        }
  1.1044 +        SDL_free(data);
  1.1045 +    }
  1.1046 +    SDL_free(renderer);
  1.1047 +}
  1.1048 +
  1.1049 +#endif /* SDL_VIDEO_RENDER_D3D */
  1.1050 +
  1.1051 +/* vi: set ts=4 sw=4 expandtab: */