device.c 207 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
/*
 * Copyright (c) 1998-2004 Lionel Ulmer
 * Copyright (c) 2002-2005 Christian Costa
 * Copyright (c) 2006 Stefan Dsinger
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 * IDirect3DDevice implementation, version 1, 2, 3 and 7. Rendering is relayed
 * to WineD3D, some minimal DirectDraw specific management is handled here.
 * The Direct3DDevice is NOT the parent of the WineD3DDevice, because d3d
 * is initialized when DirectDraw creates the primary surface.
 * Some type management is necessary, because some D3D types changed between
 * D3D7 and D3D9.
 *
 */

#include "config.h"
#include "wine/port.h"
#include "wine/debug.h"

#include <assert.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "wingdi.h"
#include "wine/exception.h"

#include "ddraw.h"
#include "d3d.h"

#include "ddraw_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);

/* The device ID */
const GUID IID_D3DDEVICE_WineD3D = {
  0xaef72d43,
  0xb09a,
  0x4b7b,
  { 0xb7,0x98,0xc6,0x8a,0x77,0x2d,0x72,0x2a }
};

/*****************************************************************************
 * IUnknown Methods. Common for Version 1, 2, 3 and 7 
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DDevice7::QueryInterface
 *
 * Used to query other interfaces from a Direct3DDevice interface.
 * It can return interface pointers to all Direct3DDevice versions as well
71
 * as IDirectDraw and IDirect3D. For a link to QueryInterface
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
 * rules see ddraw.c, IDirectDraw7::QueryInterface
 *
 * Exists in Version 1, 2, 3 and 7
 *
 * Params:
 *  refiid: Interface ID queried for
 *  obj: Used to return the interface pointer
 *
 * Returns:
 *  D3D_OK or E_NOINTERFACE
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_QueryInterface(IDirect3DDevice7 *iface,
                                     REFIID refiid,
                                     void **obj)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);

    /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
    *obj = NULL;

    if(!refiid)
        return DDERR_INVALIDPARAMS;

    if ( IsEqualGUID( &IID_IUnknown, refiid ) )
    {
        *obj = ICOM_INTERFACE(This, IDirect3DDevice7);
    }

    /* Check DirectDraw Interfacs */
    else if( IsEqualGUID( &IID_IDirectDraw7, refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
        TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
        TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw2);
        TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
    }
    else if( IsEqualGUID( &IID_IDirectDraw, refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw);
        TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
    }

    /* Direct3D */
    else if ( IsEqualGUID( &IID_IDirect3D  , refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirect3D);
        TRACE("(%p) Returning IDirect3D interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirect3D2);
        TRACE("(%p) Returning IDirect3D2 interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirect3D3);
        TRACE("(%p) Returning IDirect3D3 interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirect3D7 , refiid ) )
    {
        *obj = ICOM_INTERFACE(This->ddraw, IDirect3D7);
        TRACE("(%p) Returning IDirect3D7 interface at %p\n", This, *obj);
    }

    /* Direct3DDevice */
    else if ( IsEqualGUID( &IID_IDirect3DDevice  , refiid ) )
    {
        *obj = ICOM_INTERFACE(This, IDirect3DDevice);
        TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirect3DDevice2  , refiid ) ) {
        *obj = ICOM_INTERFACE(This, IDirect3DDevice2);
        TRACE("(%p) Returning IDirect3DDevice2 interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirect3DDevice3  , refiid ) ) {
        *obj = ICOM_INTERFACE(This, IDirect3DDevice3);
        TRACE("(%p) Returning IDirect3DDevice3 interface at %p\n", This, *obj);
    }
    else if ( IsEqualGUID( &IID_IDirect3DDevice7  , refiid ) ) {
        *obj = ICOM_INTERFACE(This, IDirect3DDevice7);
        TRACE("(%p) Returning IDirect3DDevice7 interface at %p\n", This, *obj);
    }

    /* Unknown interface */
    else
    {
169
        ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
        return E_NOINTERFACE;
    }

    /* AddRef the returned interface */
    IUnknown_AddRef( (IUnknown *) *obj);
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_QueryInterface(IDirect3DDevice3 *iface,
                                           REFIID riid,
                                           void **obj)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obj);
    return IDirect3DDevice7_QueryInterface(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           riid,
                                           obj);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_QueryInterface(IDirect3DDevice2 *iface,
                                           REFIID riid,
                                           void **obj)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obj);
    return IDirect3DDevice7_QueryInterface(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           riid,
                                           obj);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_QueryInterface(IDirect3DDevice *iface,
                                           REFIID riid,
                                           void **obp)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obp);
    return IDirect3DDevice7_QueryInterface(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           riid,
                                           obp);
}

/*****************************************************************************
 * IDirect3DDevice7::AddRef
 *
 * Increases the refcount....
218
 * The most exciting Method, definitely
219 220 221 222 223 224 225 226 227 228 229 230 231
 *
 * Exists in Version 1, 2, 3 and 7
 *
 * Returns:
 *  The new refcount
 *
 *****************************************************************************/
static ULONG WINAPI
IDirect3DDeviceImpl_7_AddRef(IDirect3DDevice7 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    ULONG ref = InterlockedIncrement(&This->ref);

232
    TRACE("(%p) : incrementing from %u.\n", This, ref -1);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

    return ref;
}

static ULONG WINAPI
Thunk_IDirect3DDeviceImpl_3_AddRef(IDirect3DDevice3 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_AddRef(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static ULONG WINAPI
Thunk_IDirect3DDeviceImpl_2_AddRef(IDirect3DDevice2 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_AddRef(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static ULONG WINAPI
Thunk_IDirect3DDeviceImpl_1_AddRef(IDirect3DDevice *iface)
{
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", iface);
    return IDirect3DDevice7_AddRef(COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice7, iface));
}

/*****************************************************************************
 * IDirect3DDevice7::Release
 *
 * Decreases the refcount of the interface
 * When the refcount is reduced to 0, the object is destroyed.
 *
 * Exists in Version 1, 2, 3 and 7
 *
 * Returns:d
 *  The new refcount
 *
 *****************************************************************************/
static ULONG WINAPI
IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    ULONG ref = InterlockedDecrement(&This->ref);

278
    TRACE("(%p)->() decrementing from %u.\n", This, ref +1);
279 280 281 282 283 284 285 286

    /* This method doesn't destroy the WineD3DDevice, because it's still in use for
     * 2D rendering. IDirectDrawSurface7::Release will destroy the WineD3DDevice
     * when the render target is released
     */
    if (ref == 0)
    {
        IParent *IndexBufferParent;
287 288
        DWORD i;

289
        EnterCriticalSection(&ddraw_cs);
290
        /* Free the index buffer. */
291
        IWineD3DDevice_SetIndices(This->wineD3DDevice, NULL);
292 293 294 295 296 297 298 299
        IWineD3DIndexBuffer_GetParent(This->indexbuffer,
                                      (IUnknown **) &IndexBufferParent);
        IParent_Release(IndexBufferParent); /* Once for the getParent */
        if( IParent_Release(IndexBufferParent) != 0)  /* And now to destroy it */
        {
            ERR(" (%p) Something is still holding the index buffer parent %p\n", This, IndexBufferParent);
        }

300 301 302 303 304
        /* There is no need to unset the vertex buffer here, IWineD3DDevice_Uninit3D will do that when
         * destroying the primary stateblock. If a vertex buffer is destroyed while it is bound
         * IDirect3DVertexBuffer::Release will unset it.
         */

305 306 307
        /* Restore the render targets */
        if(This->OffScreenTarget)
        {
308 309 310 311 312 313 314 315 316 317 318
            WINED3DVIEWPORT vp;

            vp.X = 0;
            vp.Y = 0;
            vp.Width = This->ddraw->d3d_target->surface_desc.dwWidth;
            vp.Height = This->ddraw->d3d_target->surface_desc.dwHeight;
            vp.MinZ = 0.0;
            vp.MaxZ = 1.0;
            IWineD3DDevice_SetViewport(This->wineD3DDevice,
                                       &vp);

319 320 321 322 323
            /* Set the device up to render to the front buffer since the back buffer will
             * vanish soon.
             */
            IWineD3DDevice_SetRenderTarget(This->wineD3DDevice, 0,
                                           This->ddraw->d3d_target->WineD3DSurface);
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
            /* This->target is the offscreen target.
             * This->ddraw->d3d_target is the target used by DDraw
             */
            TRACE("(%p) Release: Using %p as front buffer, %p as back buffer\n", This, This->ddraw->d3d_target, NULL);
            IWineD3DDevice_SetFrontBackBuffers(This->wineD3DDevice,
                                               This->ddraw->d3d_target->WineD3DSurface,
                                               NULL);
        }

        /* Release the WineD3DDevice. This won't destroy it */
        if(IWineD3DDevice_Release(This->wineD3DDevice) <= 0)
        {
            ERR(" (%p) The wineD3D device %p was destroyed unexpectadely. Prepare for trouble\n", This, This->wineD3DDevice);
        }

339 340 341 342 343 344 345 346 347 348 349 350
        /* The texture handles should be unset by now, but there might be some bits
         * missing in our reference counting(needs test). Do a sanity check
         */
        for(i = 0; i < This->numHandles; i++)
        {
            if(This->Handles[i].ptr)
            {
                switch(This->Handles[i].type)
                {
                    case DDrawHandle_Texture:
                    {
                        IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[i].ptr;
351
                        FIXME("Texture Handle %d not unset properly\n", i + 1);
352 353 354 355 356 357 358
                        surf->Handle = 0;
                    }
                    break;

                    case DDrawHandle_Material:
                    {
                        IDirect3DMaterialImpl *mat = (IDirect3DMaterialImpl *) This->Handles[i].ptr;
359
                        FIXME("Material handle %d not unset properly\n", i + 1);
360 361
                        mat->Handle = 0;
                    }
362 363 364 365 366
                    break;

                    case DDrawHandle_Matrix:
                    {
                        /* No fixme here because this might happen because of sloppy apps */
367
                        WARN("Leftover matrix handle %d, deleting\n", i + 1);
368 369 370 371
                        IDirect3DDevice_DeleteMatrix(ICOM_INTERFACE(This, IDirect3DDevice),
                                                     i + 1);
                    }
                    break;
372

373 374 375 376 377 378 379 380 381
                    case DDrawHandle_StateBlock:
                    {
                        /* No fixme here because this might happen because of sloppy apps */
                        WARN("Leftover stateblock handle %d, deleting\n", i + 1);
                        IDirect3DDevice7_DeleteStateBlock(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                          i + 1);
                    }
                    break;

382
                    default:
383
                        FIXME("Unknown handle %d not unset properly\n", i + 1);
384 385 386 387 388 389
                }
            }
        }

        HeapFree(GetProcessHeap(), 0, This->Handles);

390
        TRACE("Releasing target %p %p\n", This->target, This->ddraw->d3d_target);
391 392 393 394 395
        /* Release the render target and the WineD3D render target
         * (See IDirect3D7::CreateDevice for more comments on this)
         */
        IDirectDrawSurface7_Release(ICOM_INTERFACE(This->target, IDirectDrawSurface7));
        IDirectDrawSurface7_Release(ICOM_INTERFACE(This->ddraw->d3d_target,IDirectDrawSurface7));
396
        TRACE("Target release done\n");
397 398 399 400 401

        This->ddraw->d3ddevice = NULL;

        /* Now free the structure */
        HeapFree(GetProcessHeap(), 0, This);
402
        LeaveCriticalSection(&ddraw_cs);
403 404
    }

405
    TRACE("Done\n");
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
    return ref;
}

static ULONG WINAPI
Thunk_IDirect3DDeviceImpl_3_Release(IDirect3DDevice3 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_Release(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static ULONG WINAPI
Thunk_IDirect3DDeviceImpl_2_Release(IDirect3DDevice2 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_Release(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static ULONG WINAPI
Thunk_IDirect3DDeviceImpl_1_Release(IDirect3DDevice *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_Release(ICOM_INTERFACE(This, IDirect3DDevice7));
}

/*****************************************************************************
 * IDirect3DDevice Methods
 *****************************************************************************/

/*****************************************************************************
 * IDirect3DDevice::Initialize
 *
 * Initializes a Direct3DDevice. This implementation is a no-op, as all
 * initialization is done at create time.
 *
 * Exists in Version 1
 *
 * Parameters:
 *  No idea what they mean, as the MSDN page is gone
 *
 * Returns: DD_OK
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_Initialize(IDirect3DDevice *iface,
                                 IDirect3D *Direct3D, GUID *guid,
                                 D3DDEVICEDESC *Desc)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);

    /* It shouldn't be crucial, but print a FIXME, I'm interested if
     * any game calls it and when
     */
    FIXME("(%p)->(%p,%p,%p): No-op!\n", This, Direct3D, guid, Desc);

    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::GetCaps
 *
469
 * Retrieves the device's capabilities
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
 *
 * This implementation is used for Version 7 only, the older versions have
 * their own implementation.
 *
 * Parameters:
 *  Desc: Pointer to a D3DDEVICEDESC7 structure to fill
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_* if a problem occurs. See WineD3D
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetCaps(IDirect3DDevice7 *iface,
                              D3DDEVICEDESC7 *Desc)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    D3DDEVICEDESC OldDesc;
    TRACE("(%p)->(%p)\n", This, Desc);

    /* Call the same function used by IDirect3D, this saves code */
    return IDirect3DImpl_GetCaps(This->ddraw->wineD3D, &OldDesc, Desc);
}

/*****************************************************************************
 * IDirect3DDevice3::GetCaps
 *
497 498
 * Retrieves the capabilities of the hardware device and the emulation
 * device. For Wine, hardware and emulation are the same (it's all HW).
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
 *
 * This implementation is used for Version 1, 2, and 3. Version 7 has its own
 *
 * Parameters:
 *  HWDesc: Structure to fill with the HW caps
 *  HelDesc: Structure to fill with the hardare emulation caps
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_* if a problem occurs. See WineD3D
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_GetCaps(IDirect3DDevice3 *iface,
                              D3DDEVICEDESC *HWDesc,
                              D3DDEVICEDESC *HelDesc)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    D3DDEVICEDESC7 newDesc;
    HRESULT hr;
    TRACE("(%p)->(%p,%p)\n", iface, HWDesc, HelDesc);

    hr = IDirect3DImpl_GetCaps(This->ddraw->wineD3D, HWDesc, &newDesc);
    if(hr != D3D_OK) return hr;

    *HelDesc = *HWDesc;
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetCaps(IDirect3DDevice2 *iface,
                                    D3DDEVICEDESC *D3DHWDevDesc,
                                    D3DDEVICEDESC *D3DHELDevDesc)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice3 interface.\n", This, D3DHWDevDesc, D3DHELDevDesc);
    return IDirect3DDevice3_GetCaps(ICOM_INTERFACE(This, IDirect3DDevice3),
                                    D3DHWDevDesc,
                                    D3DHELDevDesc);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_GetCaps(IDirect3DDevice *iface,
                                    D3DDEVICEDESC *D3DHWDevDesc,
                                    D3DDEVICEDESC *D3DHELDevDesc)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice3 interface.\n", This, D3DHWDevDesc, D3DHELDevDesc);
    return IDirect3DDevice3_GetCaps(ICOM_INTERFACE(This, IDirect3DDevice3),
                                    D3DHWDevDesc,
                                    D3DHELDevDesc);
}

/*****************************************************************************
 * IDirect3DDevice2::SwapTextureHandles
 *
 * Swaps the texture handles of 2 Texture interfaces. Version 1 and 2
 *
 * Parameters:
 *  Tex1, Tex2: The 2 Textures to swap
 *
 * Returns:
 *  D3D_OK
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_2_SwapTextureHandles(IDirect3DDevice2 *iface,
                                         IDirect3DTexture2 *Tex1,
                                         IDirect3DTexture2 *Tex2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
570
    DWORD swap;
571 572
    IDirectDrawSurfaceImpl *surf1 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Tex1);
    IDirectDrawSurfaceImpl *surf2 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Tex2);
573
    TRACE("(%p)->(%p,%p)\n", This, surf1, surf2);
574

575
    EnterCriticalSection(&ddraw_cs);
576 577
    This->Handles[surf1->Handle - 1].ptr = surf2;
    This->Handles[surf2->Handle - 1].ptr = surf1;
578

579 580 581
    swap = surf2->Handle;
    surf2->Handle = surf1->Handle;
    surf1->Handle = swap;
582
    LeaveCriticalSection(&ddraw_cs);
583 584 585 586 587 588 589 590 591

    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles(IDirect3DDevice *iface,
                                               IDirect3DTexture *D3DTex1,
                                               IDirect3DTexture *D3DTex2)
{
592
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    IDirectDrawSurfaceImpl *surf1 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture, D3DTex1);
    IDirectDrawSurfaceImpl *surf2 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture, D3DTex2);
    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice2 interface.\n", This, surf1, surf2);
    return IDirect3DDevice2_SwapTextureHandles(ICOM_INTERFACE(This, IDirect3DDevice2),
                                               ICOM_INTERFACE(surf1, IDirect3DTexture2),
                                               ICOM_INTERFACE(surf2, IDirect3DTexture2));
}

/*****************************************************************************
 * IDirect3DDevice3::GetStats
 *
 * This method seems to retrieve some stats from the device.
 * The MSDN documentation doesn't exist any more, but the D3DSTATS
 * structure suggests that the amout of drawn primitives and processed
 * vertices is returned.
 *
 * Exists in Version 1, 2 and 3
 *
 * Parameters:
 *  Stats: Pointer to a D3DSTATS structure to be filled
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Stats == NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_GetStats(IDirect3DDevice3 *iface,
                               D3DSTATS *Stats)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    FIXME("(%p)->(%p): Stub!\n", This, Stats);

    if(!Stats)
        return DDERR_INVALIDPARAMS;

    /* Fill the Stats with 0 */
    Stats->dwTrianglesDrawn = 0;
    Stats->dwLinesDrawn = 0;
    Stats->dwPointsDrawn = 0;
    Stats->dwSpansDrawn = 0;
    Stats->dwVerticesProcessed = 0;

    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetStats(IDirect3DDevice2 *iface,
                                     D3DSTATS *Stats)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Stats);
    return IDirect3DDevice3_GetStats(ICOM_INTERFACE(This, IDirect3DDevice3),
                                     Stats);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_GetStats(IDirect3DDevice *iface,
                                     D3DSTATS *Stats)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Stats);
    return IDirect3DDevice3_GetStats(ICOM_INTERFACE(This, IDirect3DDevice3),
                                     Stats);
}

/*****************************************************************************
 * IDirect3DDevice::CreateExecuteBuffer
 *
 * Creates an IDirect3DExecuteBuffer, used for rendering with a
 * Direct3DDevice.
 *
 * Version 1 only.
 *
 * Params:
 *  Desc: Buffer description
 *  ExecuteBuffer: Address to return the Interface pointer at
670
 *  UnkOuter: Must be NULL. Basically for aggregation, which ddraw doesn't
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
 *            support
 *
 * Returns:
 *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
 *  DDERR_OUTOFMEMORY if we ran out of memory
 *  D3D_OK on success
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_CreateExecuteBuffer(IDirect3DDevice *iface,
                                          D3DEXECUTEBUFFERDESC *Desc,
                                          IDirect3DExecuteBuffer **ExecuteBuffer,
                                          IUnknown *UnkOuter)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    IDirect3DExecuteBufferImpl* object;
    TRACE("(%p)->(%p,%p,%p)!\n", This, Desc, ExecuteBuffer, UnkOuter);

    if(UnkOuter)
        return CLASS_E_NOAGGREGATION;

    /* Allocate the new Execute Buffer */
    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DExecuteBufferImpl));
    if(!object)
    {
        ERR("Out of memory when allocating a IDirect3DExecuteBufferImpl structure\n");
        return DDERR_OUTOFMEMORY;
    }

    ICOM_INIT_INTERFACE(object, IDirect3DExecuteBuffer, IDirect3DExecuteBuffer_Vtbl);

    object->ref = 1;
    object->d3ddev = This;

    /* Initializes memory */
    memcpy(&object->desc, Desc, Desc->dwSize);

    /* No buffer given */
    if ((object->desc.dwFlags & D3DDEB_LPDATA) == 0)
        object->desc.lpData = NULL;

    /* No buffer size given */
    if ((object->desc.dwFlags & D3DDEB_BUFSIZE) == 0)
        object->desc.dwBufferSize = 0;

    /* Create buffer if asked */
    if ((object->desc.lpData == NULL) && (object->desc.dwBufferSize > 0))
    {
        object->need_free = TRUE;
        object->desc.lpData = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,object->desc.dwBufferSize);
        if(!object->desc.lpData)
        {
            ERR("Out of memory when allocating the execute buffer data\n");
            HeapFree(GetProcessHeap(), 0, object);
            return DDERR_OUTOFMEMORY;
        }
    }
    else
    {
        object->need_free = FALSE;
    }

    /* No vertices for the moment */
    object->vertex_data = NULL;

    object->desc.dwFlags |= D3DDEB_LPDATA;

    object->indices = NULL;
    object->nb_indices = 0;

    *ExecuteBuffer = ICOM_INTERFACE(object, IDirect3DExecuteBuffer);

    TRACE(" Returning IDirect3DExecuteBuffer at %p, implementation is at %p\n", *ExecuteBuffer, object);

    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice::Execute
 *
 * Executes all the stuff in an execute buffer.
 *
 * Params:
 *  ExecuteBuffer: The buffer to execute
 *  Viewport: The viewport used for rendering
 *  Flags: Some flags
 *
 * Returns:
 *  DDERR_INVALIDPARAMS if ExecuteBuffer == NULL
760
 *  D3D_OK on success
761 762 763 764 765 766 767 768 769 770 771 772
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_Execute(IDirect3DDevice *iface,
                              IDirect3DExecuteBuffer *ExecuteBuffer,
                              IDirect3DViewport *Viewport,
                              DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    IDirect3DExecuteBufferImpl *Direct3DExecuteBufferImpl = ICOM_OBJECT(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, ExecuteBuffer);
    IDirect3DViewportImpl *Direct3DViewportImpl = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);

773
    TRACE("(%p)->(%p,%p,%08x)\n", This, Direct3DExecuteBufferImpl, Direct3DViewportImpl, Flags);
774 775 776 777 778

    if(!Direct3DExecuteBufferImpl)
        return DDERR_INVALIDPARAMS;

    /* Execute... */
779
    EnterCriticalSection(&ddraw_cs);
780
    IDirect3DExecuteBufferImpl_Execute(Direct3DExecuteBufferImpl, This, Direct3DViewportImpl);
781
    LeaveCriticalSection(&ddraw_cs);
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815

    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice3::AddViewport
 *
 * Add a Direct3DViewport to the device's viewport list. These viewports
 * are wrapped to IDirect3DDevice7 viewports in viewport.c
 *
 * Exists in Version 1, 2 and 3. Note that IDirect3DViewport 1, 2 and 3
 * are the same interfaces.
 *
 * Params:
 *  Viewport: The viewport to add
 *
 * Returns:
 *  DDERR_INVALIDPARAMS if Viewport == NULL
 *  D3D_OK on success
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_AddViewport(IDirect3DDevice3 *iface,
                                  IDirect3DViewport3 *Viewport)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);

    TRACE("(%p)->(%p)\n", This, vp);

    /* Sanity check */
    if(!vp)
        return DDERR_INVALIDPARAMS;

816
    EnterCriticalSection(&ddraw_cs);
817 818
    vp->next = This->viewport_list;
    This->viewport_list = vp;
819
    LeaveCriticalSection(&ddraw_cs);
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838

    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_AddViewport(IDirect3DDevice2 *iface,
                                        IDirect3DViewport2 *Direct3DViewport2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
    return IDirect3DDevice3_AddViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                        ICOM_INTERFACE(vp, IDirect3DViewport3));
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_AddViewport(IDirect3DDevice *iface,
                                        IDirect3DViewport *Direct3DViewport)
{
839
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
    return IDirect3DDevice3_AddViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                        ICOM_INTERFACE(vp, IDirect3DViewport3));
}

/*****************************************************************************
 * IDirect3DDevice3::DeleteViewport
 *
 * Deletes a Direct3DViewport from the device's viewport list.
 *
 * Exists in Version 1, 2 and 3. Note that all Viewport interface versions
 * are equal.
 *
 * Params:
 *  Viewport: The viewport to delete
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if the viewport wasn't found in the list
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_DeleteViewport(IDirect3DDevice3 *iface,
                                     IDirect3DViewport3 *Viewport)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *) Viewport;
    IDirect3DViewportImpl *cur_viewport, *prev_viewport = NULL;

    TRACE("(%p)->(%p)\n", This, vp);

872
    EnterCriticalSection(&ddraw_cs);
873 874 875 876 877 878 879 880
    cur_viewport = This->viewport_list;
    while (cur_viewport != NULL)
    {
        if (cur_viewport == vp)
        {
            if (prev_viewport == NULL) This->viewport_list = cur_viewport->next;
            else prev_viewport->next = cur_viewport->next;
            /* TODO : add desactivate of the viewport and all associated lights... */
881
            LeaveCriticalSection(&ddraw_cs);
882 883 884 885 886 887
            return D3D_OK;
        }
        prev_viewport = cur_viewport;
        cur_viewport = cur_viewport->next;
    }

888
    LeaveCriticalSection(&ddraw_cs);
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904
    return DDERR_INVALIDPARAMS;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_DeleteViewport(IDirect3DDevice2 *iface,
                                           IDirect3DViewport2 *Direct3DViewport2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
    return IDirect3DDevice3_DeleteViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                           ICOM_INTERFACE(vp, IDirect3DViewport3));
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_DeleteViewport(IDirect3DDevice *iface,
905
                                           IDirect3DViewport *Direct3DViewport)
906 907
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
908
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport);
909 910 911 912 913 914 915 916
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
    return IDirect3DDevice3_DeleteViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                           ICOM_INTERFACE(vp, IDirect3DViewport3));
}

/*****************************************************************************
 * IDirect3DDevice3::NextViewport
 *
917
 * Returns a viewport from the viewport list, depending on the
918 919 920 921 922 923 924 925 926 927 928
 * passed viewport and the flags.
 *
 * Exists in Version 1, 2 and 3. Note that all Viewport interface versions
 * are equal.
 *
 * Params:
 *  Viewport: Viewport to use for beginning the search
 *  Flags: D3DNEXT_NEXT, D3DNEXT_HEAD or D3DNEXT_TAIL
 *
 * Returns:
 *  D3D_OK on success
929
 *  DDERR_INVALIDPARAMS if the flags were wrong, or Viewport was NULL
930 931 932 933 934 935 936 937 938 939 940 941
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_NextViewport(IDirect3DDevice3 *iface,
                                   IDirect3DViewport3 *Viewport3,
                                   IDirect3DViewport3 **lplpDirect3DViewport3,
                                   DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport3);
    IDirect3DViewportImpl *res = NULL;

942
    TRACE("(%p)->(%p,%p,%08x)\n", This, vp, lplpDirect3DViewport3, Flags);
943 944 945 946

    if(!vp)
    {
        *lplpDirect3DViewport3 = NULL;
947
        return DDERR_INVALIDPARAMS;
948 949 950
    }


951
    EnterCriticalSection(&ddraw_cs);
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    switch (Flags)
    {
        case D3DNEXT_NEXT:
        {
            res = vp->next;
        }
        break;
        case D3DNEXT_HEAD:
        {
            res = This->viewport_list;
        }
        break;
        case D3DNEXT_TAIL:
        {
            IDirect3DViewportImpl *cur_viewport = This->viewport_list;
            if (cur_viewport != NULL)
            {
                while (cur_viewport->next != NULL) cur_viewport = cur_viewport->next;
            }
            res = cur_viewport;
        }
        break;
        default:
            *lplpDirect3DViewport3 = NULL;
976
            LeaveCriticalSection(&ddraw_cs);
977 978 979 980
            return DDERR_INVALIDPARAMS;
    }

    *lplpDirect3DViewport3 = ICOM_INTERFACE(res, IDirect3DViewport3);
981
    LeaveCriticalSection(&ddraw_cs);
982 983 984 985 986 987 988 989 990 991 992 993 994
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_NextViewport(IDirect3DDevice2 *iface,
                                         IDirect3DViewport2 *Viewport2,
                                         IDirect3DViewport2 **lplpDirect3DViewport2,
                                         DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport2);
    IDirect3DViewport3 *res;
    HRESULT hr;
995
    TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x) thunking to IDirect3DDevice3 interface.\n", This, vp, lplpDirect3DViewport2, Flags);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    hr = IDirect3DDevice3_NextViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                       ICOM_INTERFACE(vp, IDirect3DViewport3),
                                       &res,
                                       Flags);
    *lplpDirect3DViewport2 = (IDirect3DViewport2 *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, res);
    return hr;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_NextViewport(IDirect3DDevice *iface,
                                         IDirect3DViewport *Viewport,
                                         IDirect3DViewport **lplpDirect3DViewport,
                                         DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);
    IDirect3DViewport3 *res;
    HRESULT hr;
1014
    TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x) thunking to IDirect3DDevice3 interface.\n", This, vp, lplpDirect3DViewport, Flags);
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
    hr = IDirect3DDevice3_NextViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                       ICOM_INTERFACE(vp, IDirect3DViewport3),
                                       &res,
                                       Flags);
    *lplpDirect3DViewport = (IDirect3DViewport *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, res);
    return hr;
}

/*****************************************************************************
 * IDirect3DDevice::Pick
 *
 * Executes an execute buffer without performing rendering. Instead, a
 * list of primitives that intersect with (x1,y1) of the passed rectangle
 * is created. IDirect3DDevice::GetPickRecords can be used to retrieve
 * this list.
 *
 * Version 1 only
 *
 * Params:
 *  ExecuteBuffer: Buffer to execute
 *  Viewport: Viewport to use for execution
 *  Flags: None are defined, according to the SDK
 *  Rect: Specifies the coordinates to be picked. Only x1 and y2 are used,
 *        x2 and y2 are ignored.
 *
 * Returns:
 *  D3D_OK because it's a stub
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_Pick(IDirect3DDevice *iface,
                           IDirect3DExecuteBuffer *ExecuteBuffer,
                           IDirect3DViewport *Viewport,
                           DWORD Flags,
                           D3DRECT *Rect)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    IDirect3DExecuteBufferImpl *execbuf = ICOM_OBJECT(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, ExecuteBuffer);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);
1054
    FIXME("(%p)->(%p,%p,%08x,%p): stub!\n", This, execbuf, vp, Flags, Rect);
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088

    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice::GetPickRecords
 *
 * Retrieves the pick records generated by IDirect3DDevice::GetPickRecords
 *
 * Version 1 only
 *
 * Params:
 *  Count: Pointer to a DWORD containing the numbers of pick records to
 *         retrieve
 *  D3DPickRec: Address to store the resulting D3DPICKRECORD arry.
 *
 * Returns:
 *  D3D_OK, because it's a stub
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_GetPickRecords(IDirect3DDevice *iface,
                                     DWORD *Count,
                                     D3DPICKRECORD *D3DPickRec)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    FIXME("(%p)->(%p,%p): stub!\n", This, Count, D3DPickRec);

    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::EnumTextureformats
 *
1089 1090 1091
 * Enumerates the supported texture formats. It has a list of all possible
 * formats and calls IWineD3D::CheckDeviceFormat for each format to see if
 * WineD3D supports it. If so, then it is passed to the app.
1092
 *
1093
 * This is for Version 7 and 3, older versions have a different
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
 * callback function and their own implementation
 *
 * Params:
 *  Callback: Callback to call for each enumerated format
 *  Arg: Argument to pass to the callback
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Callback == NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_EnumTextureFormats(IDirect3DDevice7 *iface,
                                         LPD3DENUMPIXELFORMATSCALLBACK Callback,
                                         void *Arg)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    int i;

    WINED3DFORMAT FormatList[] = {
        /* 32 bit */
        WINED3DFMT_A8R8G8B8,
        WINED3DFMT_X8R8G8B8,
        /* 24 bit */
        WINED3DFMT_R8G8B8,
        /* 16 Bit */
        WINED3DFMT_A1R5G5B5,
        WINED3DFMT_A4R4G4B4,
        WINED3DFMT_R5G6B5,
        WINED3DFMT_X1R5G5B5,
        /* 8 Bit */
        WINED3DFMT_R3G3B2,
        WINED3DFMT_P8,
        /* FOURCC codes */
        WINED3DFMT_DXT1,
        WINED3DFMT_DXT3,
        WINED3DFMT_DXT5,
    };

1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
    WINED3DFORMAT BumpFormatList[] = {
        WINED3DFMT_V8U8,
        WINED3DFMT_L6V5U5,
        WINED3DFMT_X8L8V8U8,
        WINED3DFMT_Q8W8V8U8,
        WINED3DFMT_V16U16,
        WINED3DFMT_W11V11U10,
        WINED3DFMT_A2W10V10U10
    };

1144 1145 1146 1147 1148
    TRACE("(%p)->(%p,%p): Relay\n", This, Callback, Arg);

    if(!Callback)
        return DDERR_INVALIDPARAMS;

1149
    EnterCriticalSection(&ddraw_cs);
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
    for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
    {
        hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
                                        0 /* Adapter */,
                                        0 /* DeviceType */,
                                        0 /* AdapterFormat */,
                                        0 /* Usage */,
                                        0 /* ResourceType */,
                                        FormatList[i]);
        if(hr == D3D_OK)
        {
            DDPIXELFORMAT pformat;

            memset(&pformat, 0, sizeof(pformat));
            pformat.dwSize = sizeof(pformat);
            PixelFormat_WineD3DtoDD(&pformat, FormatList[i]);

            TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
            hr = Callback(&pformat, Arg);
            if(hr != DDENUMRET_OK)
            {
                TRACE("Format enumeration cancelled by application\n");
1172
                LeaveCriticalSection(&ddraw_cs);
1173 1174 1175 1176
                return D3D_OK;
            }
        }
    }
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199

    for(i = 0; i < sizeof(BumpFormatList) / sizeof(WINED3DFORMAT); i++)
    {
        hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
                                        0 /* Adapter */,
                                        0 /* DeviceType */,
                                        0 /* AdapterFormat */,
                                        WINED3DUSAGE_QUERY_LEGACYBUMPMAP,
                                        0 /* ResourceType */,
                                        BumpFormatList[i]);
        if(hr == D3D_OK)
        {
            DDPIXELFORMAT pformat;

            memset(&pformat, 0, sizeof(pformat));
            pformat.dwSize = sizeof(pformat);
            PixelFormat_WineD3DtoDD(&pformat, BumpFormatList[i]);

            TRACE("Enumerating WineD3DFormat %d\n", BumpFormatList[i]);
            hr = Callback(&pformat, Arg);
            if(hr != DDENUMRET_OK)
            {
                TRACE("Format enumeration cancelled by application\n");
1200
                LeaveCriticalSection(&ddraw_cs);
1201 1202 1203 1204
                return D3D_OK;
            }
        }
    }
1205
    TRACE("End of enumeration\n");
1206
    LeaveCriticalSection(&ddraw_cs);
1207
    return D3D_OK;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats(IDirect3DDevice3 *iface,
                                               LPD3DENUMPIXELFORMATSCALLBACK Callback,
                                               void *Arg)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice7 interface.\n", This, Callback, Arg);
    return IDirect3DDevice7_EnumTextureFormats(ICOM_INTERFACE(This, IDirect3DDevice7),
                                               Callback,
                                               Arg);
}

/*****************************************************************************
 * IDirect3DDevice2::EnumTextureformats
 *
 * EnumTextureFormats for Version 1 and 2, see
1226 1227 1228 1229
 * IDirect3DDevice7::EnumTexureFormats for a more detailed description.
 *
 * This version has a different callback and does not enumerate FourCC
 * formats
1230 1231 1232 1233 1234 1235 1236 1237 1238
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_2_EnumTextureFormats(IDirect3DDevice2 *iface,
                                         LPD3DENUMTEXTUREFORMATSCALLBACK Callback,
                                         void *Arg)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    HRESULT hr;
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    int i;

    WINED3DFORMAT FormatList[] = {
        /* 32 bit */
        WINED3DFMT_A8R8G8B8,
        WINED3DFMT_X8R8G8B8,
        /* 24 bit */
        WINED3DFMT_R8G8B8,
        /* 16 Bit */
        WINED3DFMT_A1R5G5B5,
        WINED3DFMT_A4R4G4B4,
        WINED3DFMT_R5G6B5,
        WINED3DFMT_X1R5G5B5,
        /* 8 Bit */
        WINED3DFMT_R3G3B2,
        WINED3DFMT_P8,
        /* FOURCC codes - Not in this version*/
    };

1258 1259
    TRACE("(%p)->(%p,%p): Relay\n", This, Callback, Arg);

1260 1261 1262
    if(!Callback)
        return DDERR_INVALIDPARAMS;

1263
    EnterCriticalSection(&ddraw_cs);
1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
    for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
    {
        hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
                                        0 /* Adapter */,
                                        0 /* DeviceType */,
                                        0 /* AdapterFormat */,
                                        0 /* Usage */,
                                        0 /* ResourceType */,
                                        FormatList[i]);
        if(hr == D3D_OK)
        {
            DDSURFACEDESC sdesc;

            memset(&sdesc, 0, sizeof(sdesc));
            sdesc.dwSize = sizeof(sdesc);
            sdesc.dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
            sdesc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1281
            sdesc.ddpfPixelFormat.dwSize = sizeof(sdesc.ddpfPixelFormat);
1282 1283 1284 1285 1286 1287 1288
            PixelFormat_WineD3DtoDD(&sdesc.ddpfPixelFormat, FormatList[i]);

            TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
            hr = Callback(&sdesc, Arg);
            if(hr != DDENUMRET_OK)
            {
                TRACE("Format enumeration cancelled by application\n");
1289
                LeaveCriticalSection(&ddraw_cs);
1290 1291 1292 1293 1294
                return D3D_OK;
            }
        }
    }
    TRACE("End of enumeration\n");
1295
    LeaveCriticalSection(&ddraw_cs);
1296
    return D3D_OK;
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats(IDirect3DDevice *iface,
                                               LPD3DENUMTEXTUREFORMATSCALLBACK Callback,
                                               void *Arg)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice2 interface.\n", This, Callback, Arg);
    return IDirect3DDevice2_EnumTextureFormats(ICOM_INTERFACE(This, IDirect3DDevice2),
                                               Callback,
                                               Arg);
}

/*****************************************************************************
 * IDirect3DDevice::CreateMatrix
 *
1314 1315
 * Creates a matrix handle. A handle is created and memory for a D3DMATRIX is
 * allocated for the handle.
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
 *
 * Version 1 only
 *
 * Params
 *  D3DMatHandle: Address to return the handle at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if D3DMatHandle = NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_CreateMatrix(IDirect3DDevice *iface, D3DMATRIXHANDLE *D3DMatHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1331
    D3DMATRIX *Matrix;
1332 1333 1334 1335 1336
    TRACE("(%p)->(%p)\n", This, D3DMatHandle);

    if(!D3DMatHandle)
        return DDERR_INVALIDPARAMS;

1337 1338 1339 1340 1341 1342
    Matrix = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DMATRIX));
    if(!Matrix)
    {
        ERR("Out of memory when allocating a D3DMATRIX\n");
        return DDERR_OUTOFMEMORY;
    }
1343 1344

    EnterCriticalSection(&ddraw_cs);
1345 1346 1347 1348 1349
    *D3DMatHandle = IDirect3DDeviceImpl_CreateHandle(This);
    if(!(*D3DMatHandle))
    {
        ERR("Failed to create a matrix handle\n");
        HeapFree(GetProcessHeap(), 0, Matrix);
1350
        LeaveCriticalSection(&ddraw_cs);
1351 1352
        return DDERR_OUTOFMEMORY;
    }
1353 1354
    This->Handles[(DWORD) *D3DMatHandle - 1].ptr = Matrix;
    This->Handles[(DWORD) *D3DMatHandle - 1].type = DDrawHandle_Matrix;
1355
    TRACE(" returning matrix handle %d\n", *D3DMatHandle);
1356

1357
    LeaveCriticalSection(&ddraw_cs);
1358 1359 1360 1361 1362 1363
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice::SetMatrix
 *
1364 1365
 * Sets a matrix for a matrix handle. The matrix is copied into the memory
 * allocated for the handle
1366 1367 1368 1369 1370 1371 1372 1373 1374
 *
 * Version 1 only
 *
 * Params:
 *  D3DMatHandle: Handle to set the matrix to
 *  D3DMatrix: Matrix to set
 *
 * Returns:
 *  D3D_OK on success
1375 1376
 *  DDERR_INVALIDPARAMS if the handle of the matrix is invalid or the matrix
 *   to set is NULL
1377 1378 1379 1380 1381 1382 1383 1384
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_SetMatrix(IDirect3DDevice *iface,
                                D3DMATRIXHANDLE D3DMatHandle,
                                D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1385
    TRACE("(%p)->(%08x,%p)\n", This, (DWORD) D3DMatHandle, D3DMatrix);
1386 1387 1388 1389

    if( (!D3DMatHandle) || (!D3DMatrix) )
        return DDERR_INVALIDPARAMS;

1390
    EnterCriticalSection(&ddraw_cs);
1391 1392
    if(D3DMatHandle > This->numHandles)
    {
1393
        ERR("Handle %d out of range\n", D3DMatHandle);
1394
        LeaveCriticalSection(&ddraw_cs);
1395 1396 1397 1398
        return DDERR_INVALIDPARAMS;
    }
    else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
    {
1399
        ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
1400
        LeaveCriticalSection(&ddraw_cs);
1401 1402 1403
        return DDERR_INVALIDPARAMS;
    }

1404 1405 1406
    if (TRACE_ON(d3d7))
        dump_D3DMATRIX(D3DMatrix);

1407
    *((D3DMATRIX *) This->Handles[D3DMatHandle - 1].ptr) = *D3DMatrix;
1408

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
    if(This->world == D3DMatHandle)
    {
        IWineD3DDevice_SetTransform(This->wineD3DDevice,
                                    WINED3DTS_WORLDMATRIX(0),
                                    (WINED3DMATRIX *) D3DMatrix);
    }
    if(This->view == D3DMatHandle)
    {
        IWineD3DDevice_SetTransform(This->wineD3DDevice,
                                    WINED3DTS_VIEW,
                                    (WINED3DMATRIX *) D3DMatrix);
    }
    if(This->proj == D3DMatHandle)
    {
        IWineD3DDevice_SetTransform(This->wineD3DDevice,
                                    WINED3DTS_PROJECTION,
                                    (WINED3DMATRIX *) D3DMatrix);
    }

1428
    LeaveCriticalSection(&ddraw_cs);
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice::SetMatrix
 *
 * Returns the content of a D3DMATRIX handle
 *
 * Version 1 only
 *
 * Params:
 *  D3DMatHandle: Matrix handle to read the content from
 *  D3DMatrix: Address to store the content at
 *
 * Returns:
 *  D3D_OK on success
1445
 *  DDERR_INVALIDPARAMS if D3DMatHandle is invalid or D3DMatrix is NULL
1446 1447 1448 1449 1450 1451 1452 1453
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_GetMatrix(IDirect3DDevice *iface,
                                D3DMATRIXHANDLE D3DMatHandle,
                                D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1454
    TRACE("(%p)->(%08x,%p)\n", This, (DWORD) D3DMatHandle, D3DMatrix);
1455 1456 1457

    if(!D3DMatrix)
        return DDERR_INVALIDPARAMS;
1458 1459 1460
    if(!D3DMatHandle)
        return DDERR_INVALIDPARAMS;

1461
    EnterCriticalSection(&ddraw_cs);
1462 1463
    if(D3DMatHandle > This->numHandles)
    {
1464
        ERR("Handle %d out of range\n", D3DMatHandle);
1465
        LeaveCriticalSection(&ddraw_cs);
1466
        return DDERR_INVALIDPARAMS;
1467 1468 1469
    }
    else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
    {
1470
        ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
1471
        LeaveCriticalSection(&ddraw_cs);
1472 1473
        return DDERR_INVALIDPARAMS;
    }
1474 1475

    /* The handle is simply a pointer to a D3DMATRIX structure */
1476
    *D3DMatrix = *((D3DMATRIX *) This->Handles[D3DMatHandle - 1].ptr);
1477

1478
    LeaveCriticalSection(&ddraw_cs);
1479 1480 1481 1482 1483 1484
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice::DeleteMatrix
 *
1485
 * Destroys a Matrix handle. Frees the memory and unsets the handle data
1486 1487 1488 1489 1490 1491 1492 1493
 *
 * Version 1 only
 *
 * Params:
 *  D3DMatHandle: Handle to destroy
 *
 * Returns:
 *  D3D_OK on success
1494
 *  DDERR_INVALIDPARAMS if D3DMatHandle is invalid
1495 1496 1497 1498 1499 1500 1501
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_1_DeleteMatrix(IDirect3DDevice *iface,
                                   D3DMATRIXHANDLE D3DMatHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1502
    TRACE("(%p)->(%08x)\n", This, (DWORD) D3DMatHandle);
1503

1504 1505 1506
    if(!D3DMatHandle)
        return DDERR_INVALIDPARAMS;

1507
    EnterCriticalSection(&ddraw_cs);
1508 1509
    if(D3DMatHandle > This->numHandles)
    {
1510
        ERR("Handle %d out of range\n", D3DMatHandle);
1511
        LeaveCriticalSection(&ddraw_cs);
1512
        return DDERR_INVALIDPARAMS;
1513 1514 1515
    }
    else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
    {
1516
        ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
1517
        LeaveCriticalSection(&ddraw_cs);
1518 1519
        return DDERR_INVALIDPARAMS;
    }
1520

1521 1522 1523
    HeapFree(GetProcessHeap(), 0, This->Handles[D3DMatHandle - 1].ptr);
    This->Handles[D3DMatHandle - 1].ptr = NULL;
    This->Handles[D3DMatHandle - 1].type = DDrawHandle_Unknown;
1524

1525
    LeaveCriticalSection(&ddraw_cs);
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::BeginScene
 *
 * This method must be called before any rendering is performed.
 * IDirect3DDevice::EndScene has to be called after the scene is complete
 *
 * Version 1, 2, 3 and 7
 *
 * Returns:
1538
 *  D3D_OK on success, for details see IWineD3DDevice::BeginScene
1539 1540
 *  D3DERR_SCENE_IN_SCENE if WineD3D returns an error(Only in case of an already
 *  started scene).
1541 1542 1543 1544 1545 1546
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_BeginScene(IDirect3DDevice7 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1547
    HRESULT hr;
1548 1549
    TRACE("(%p): Relay\n", This);

1550
    EnterCriticalSection(&ddraw_cs);
1551
    hr = IWineD3DDevice_BeginScene(This->wineD3DDevice);
1552
    LeaveCriticalSection(&ddraw_cs);
1553 1554
    if(hr == WINED3D_OK) return D3D_OK;
    else return D3DERR_SCENE_IN_SCENE; /* TODO: Other possible causes of failure */
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_BeginScene(IDirect3DDevice3 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_BeginScene(IDirect3DDevice2 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_BeginScene(IDirect3DDevice *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
}

/*****************************************************************************
 * IDirect3DDevice7::EndScene
 *
 * Ends a scene that has been begun with IDirect3DDevice7::BeginScene.
 * This method must be called after rendering is finished.
 *
 * Version 1, 2, 3 and 7
 *
 * Returns:
 *  D3D_OK on success, for details see IWineD3DDevice::EndScene
1591 1592
 *  D3DERR_SCENE_NOT_IN_SCENE is returned if WineD3D returns an error. It does
 *  that only if the scene was already ended.
1593 1594 1595 1596 1597 1598
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_EndScene(IDirect3DDevice7 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1599
    HRESULT hr;
1600 1601
    TRACE("(%p): Relay\n", This);

1602
    EnterCriticalSection(&ddraw_cs);
1603
    hr = IWineD3DDevice_EndScene(This->wineD3DDevice);
1604
    LeaveCriticalSection(&ddraw_cs);
1605 1606
    if(hr == WINED3D_OK) return D3D_OK;
    else return D3DERR_SCENE_NOT_IN_SCENE;
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_EndScene(IDirect3DDevice3 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_EndScene(IDirect3DDevice2 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_EndScene(IDirect3DDevice *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
    return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
}

/*****************************************************************************
 * IDirect3DDevice7::GetDirect3D
 *
 * Returns the IDirect3D(= interface to the DirectDraw object) used to create
 * this device.
 *
 * Params:
 *  Direct3D7: Address to store the interface pointer at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Direct3D7 == NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetDirect3D(IDirect3DDevice7 *iface,
                                  IDirect3D7 **Direct3D7)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    TRACE("(%p)->(%p)\n", This, Direct3D7);

    if(!Direct3D7)
        return DDERR_INVALIDPARAMS;

    *Direct3D7 = ICOM_INTERFACE(This->ddraw, IDirect3D7);
    IDirect3D7_AddRef(*Direct3D7);

    TRACE(" returning interface %p\n", *Direct3D7);
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetDirect3D(IDirect3DDevice3 *iface,
                                        IDirect3D3 **Direct3D3)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    HRESULT ret;
    IDirect3D7 *ret_ptr;

    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D3);
    ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
                                       &ret_ptr);
    if(ret != D3D_OK)
        return ret;
    *Direct3D3 = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D3, ret_ptr);
    TRACE(" returning interface %p\n", *Direct3D3);
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetDirect3D(IDirect3DDevice2 *iface,
                                        IDirect3D2 **Direct3D2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    HRESULT ret;
    IDirect3D7 *ret_ptr;

    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D2);
    ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
                                       &ret_ptr);
    if(ret != D3D_OK)
        return ret;
    *Direct3D2 = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D2, ret_ptr);
    TRACE(" returning interface %p\n", *Direct3D2);
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_1_GetDirect3D(IDirect3DDevice *iface,
                                        IDirect3D **Direct3D)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
    HRESULT ret;
    IDirect3D7 *ret_ptr;

    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D);
    ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
                                       &ret_ptr);
    if(ret != D3D_OK)
        return ret;
    *Direct3D = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D, ret_ptr);
    TRACE(" returning interface %p\n", *Direct3D);
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice3::SetCurrentViewport
 *
 * Sets a Direct3DViewport as the current viewport.
 * For the thunks note that all viewport interface versions are equal
 *
 * Params:
 *  Direct3DViewport3: The viewport to set
 *
 * Version 2 and 3
 *
 * Returns:
 *  D3D_OK on success
 *  (Is a NULL viewport valid?)
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_SetCurrentViewport(IDirect3DDevice3 *iface,
                                         IDirect3DViewport3 *Direct3DViewport3)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport3);
    TRACE("(%p)->(%p)\n", This, Direct3DViewport3);

1742
    EnterCriticalSection(&ddraw_cs);
1743 1744
    /* Do nothing if the specified viewport is the same as the current one */
    if (This->current_viewport == vp )
1745 1746 1747 1748
    {
        LeaveCriticalSection(&ddraw_cs);
        return D3D_OK;
    }
1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766

    /* Should check if the viewport was added or not */

    /* Release previous viewport and AddRef the new one */
    if (This->current_viewport)
    {
        TRACE("ViewportImpl is at %p, interface is at %p\n", This->current_viewport, ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3));
        IDirect3DViewport3_Release( ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3) );
    }
    IDirect3DViewport3_AddRef(Direct3DViewport3);

    /* Set this viewport as the current viewport */
    This->current_viewport = vp;

    /* Activate this viewport */
    This->current_viewport->active_device = This;
    This->current_viewport->activate(This->current_viewport);

1767
    LeaveCriticalSection(&ddraw_cs);
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport(IDirect3DDevice2 *iface,
                                               IDirect3DViewport2 *Direct3DViewport2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
    return IDirect3DDevice3_SetCurrentViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                               ICOM_INTERFACE(vp, IDirect3DViewport3));
}

/*****************************************************************************
 * IDirect3DDevice3::GetCurrentViewport
 *
 * Returns the currently active viewport.
 *
 * Version 2 and 3
 *
 * Params:
 *  Direct3DViewport3: Address to return the interface pointer at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Direct3DViewport == NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_GetCurrentViewport(IDirect3DDevice3 *iface,
                                         IDirect3DViewport3 **Direct3DViewport3)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE("(%p)->(%p)\n", This, Direct3DViewport3);

    if(!Direct3DViewport3)
        return DDERR_INVALIDPARAMS;

1807
    EnterCriticalSection(&ddraw_cs);
1808 1809 1810
    *Direct3DViewport3 = ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3);

    /* AddRef the returned viewport */
1811
    if(*Direct3DViewport3) IDirect3DViewport3_AddRef(*Direct3DViewport3);
1812 1813 1814

    TRACE(" returning interface %p\n", *Direct3DViewport3);

1815
    LeaveCriticalSection(&ddraw_cs);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport(IDirect3DDevice2 *iface,
                                               IDirect3DViewport2 **Direct3DViewport2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    HRESULT hr;
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Direct3DViewport2);
    hr = IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
                                            (IDirect3DViewport3 **) Direct3DViewport2);
    if(hr != D3D_OK) return hr;
1829
    *Direct3DViewport2 = (IDirect3DViewport2 *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, *Direct3DViewport2);
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::SetRenderTarget
 *
 * Sets the render target for the Direct3DDevice.
 * For the thunks note that IDirectDrawSurface7 == IDirectDrawSurface4 and
 * IDirectDrawSurface3 == IDirectDrawSurface
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  NewTarget: Pointer to an IDirectDrawSurface7 interface to set as the new
 *             render target
 *  Flags: Some flags
 *
 * Returns:
 *  D3D_OK on success, for details see IWineD3DDevice::SetRenderTarget
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetRenderTarget(IDirect3DDevice7 *iface,
                                      IDirectDrawSurface7 *NewTarget,
                                      DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, NewTarget);
1858
    HRESULT hr;
1859
    TRACE("(%p)->(%p,%08x): Relay\n", This, NewTarget, Flags);
1860

1861
    EnterCriticalSection(&ddraw_cs);
1862 1863
    /* Flags: Not used */

1864 1865 1866 1867 1868
    hr = IWineD3DDevice_SetRenderTarget(This->wineD3DDevice,
                                        0,
                                        Target ? Target->WineD3DSurface : NULL);
    if(hr != D3D_OK)
    {
1869
        LeaveCriticalSection(&ddraw_cs);
1870 1871 1872 1873 1874 1875
        return hr;
    }
    IDirectDrawSurface7_AddRef(NewTarget);
    IDirectDrawSurface7_Release(ICOM_INTERFACE(This->target, IDirectDrawSurface7));
    This->target = Target;
    IDirect3DDeviceImpl_UpdateDepthStencil(This);
1876
    LeaveCriticalSection(&ddraw_cs);
1877
    return D3D_OK;
1878 1879 1880 1881 1882 1883 1884 1885 1886
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_SetRenderTarget(IDirect3DDevice3 *iface,
                                            IDirectDrawSurface4 *NewRenderTarget,
                                            DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, NewRenderTarget);
1887
    TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899
    return IDirect3DDevice7_SetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
                                            ICOM_INTERFACE(Target, IDirectDrawSurface7),
                                            Flags);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_SetRenderTarget(IDirect3DDevice2 *iface,
                                            IDirectDrawSurface *NewRenderTarget,
                                            DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface3, NewRenderTarget);
1900
    TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932
    return IDirect3DDevice7_SetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
                                            ICOM_INTERFACE(Target, IDirectDrawSurface7),
                                            Flags);
}

/*****************************************************************************
 * IDirect3DDevice7::GetRenderTarget
 *
 * Returns the current render target.
 * This is handled locally, because the WineD3D render target's parent
 * is an IParent
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  RenderTarget: Address to store the surface interface pointer
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if RenderTarget == NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetRenderTarget(IDirect3DDevice7 *iface,
                                      IDirectDrawSurface7 **RenderTarget)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    TRACE("(%p)->(%p): Relay\n", This, RenderTarget);

    if(!RenderTarget)
        return DDERR_INVALIDPARAMS;

1933
    EnterCriticalSection(&ddraw_cs);
1934 1935 1936
    *RenderTarget = ICOM_INTERFACE(This->target, IDirectDrawSurface7);
    IDirectDrawSurface7_AddRef(*RenderTarget);

1937
    LeaveCriticalSection(&ddraw_cs);
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetRenderTarget(IDirect3DDevice3 *iface,
                                            IDirectDrawSurface4 **RenderTarget)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    HRESULT hr;
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
    hr = IDirect3DDevice7_GetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          (IDirectDrawSurface7 **) RenderTarget);
    if(hr != D3D_OK) return hr;
1951
    *RenderTarget = (IDirectDrawSurface4 *) COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirectDrawSurface7, *RenderTarget);
1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetRenderTarget(IDirect3DDevice2 *iface,
                                            IDirectDrawSurface **RenderTarget)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    HRESULT hr;
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
    hr = IDirect3DDevice7_GetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          (IDirectDrawSurface7 **) RenderTarget);
    if(hr != D3D_OK) return hr;
1965
    *RenderTarget = (IDirectDrawSurface *) COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirectDrawSurface3, *RenderTarget);
1966 1967 1968 1969 1970 1971
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice3::Begin
 *
1972
 * Begins a description block of vertices. This is similar to glBegin()
1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993
 * and glEnd(). After a call to IDirect3DDevice3::End, the vertices
 * described with IDirect3DDevice::Vertex are drawn.
 *
 * Version 2 and 3
 *
 * Params:
 *  PrimitiveType: The type of primitives to draw
 *  VertexTypeDesc: A flexible vertex format description of the vertices
 *  Flags: Some flags..
 *
 * Returns:
 *  D3D_OK on success
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_Begin(IDirect3DDevice3 *iface,
                            D3DPRIMITIVETYPE PrimitiveType,
                            DWORD VertexTypeDesc,
                            DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1994
    TRACE("(%p)->(%d,%d,%08x)\n", This, PrimitiveType, VertexTypeDesc, Flags);
1995

1996
    EnterCriticalSection(&ddraw_cs);
1997 1998 1999 2000 2001
    This->primitive_type = PrimitiveType;
    This->vertex_type = VertexTypeDesc;
    This->render_flags = Flags;
    This->vertex_size = get_flexible_vertex_size(This->vertex_type);
    This->nb_vertices = 0;
2002
    LeaveCriticalSection(&ddraw_cs);
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014

    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_Begin(IDirect3DDevice2 *iface,
                                  D3DPRIMITIVETYPE d3dpt,
                                  D3DVERTEXTYPE dwVertexTypeDesc,
                                  DWORD dwFlags)
{
    DWORD FVF;
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2015
    TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dpt, dwVertexTypeDesc, dwFlags);
2016 2017 2018 2019 2020 2021 2022

    switch(dwVertexTypeDesc)
    {
        case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
        case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
        case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
        default:
2023 2024
            ERR("Unexpected vertex type %d\n", dwVertexTypeDesc);
            return DDERR_INVALIDPARAMS;  /* Should never happen */
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043
    };

    return IDirect3DDevice3_Begin(ICOM_INTERFACE(This, IDirect3DDevice3),
                                  d3dpt,
                                  FVF,
                                  dwFlags);
}

/*****************************************************************************
 * IDirect3DDevice3::BeginIndexed
 *
 * Draws primitives based on vertices in a vertex array which are specified
 * by indices.
 *
 * Version 2 and 3
 *
 * Params:
 *  PrimitiveType: Primitive type to draw
 *  VertexType: A FVF description of the vertex format
2044
 *  Vertices: pointer to an array containing the vertices
2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
 *  NumVertices: The number of vertices in the vertex array
 *  Flags: Some flags ...
 *
 * Returns:
 *  D3D_OK, because it's a stub
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_BeginIndexed(IDirect3DDevice3 *iface,
                                   D3DPRIMITIVETYPE PrimitiveType,
                                   DWORD VertexType,
                                   void *Vertices,
                                   DWORD NumVertices,
                                   DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2061
    FIXME("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, Vertices, NumVertices, Flags);
2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075
    return D3D_OK;
}


static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_BeginIndexed(IDirect3DDevice2 *iface,
                                         D3DPRIMITIVETYPE d3dptPrimitiveType,
                                         D3DVERTEXTYPE d3dvtVertexType,
                                         void *lpvVertices,
                                         DWORD dwNumVertices,
                                         DWORD dwFlags)
{
    DWORD FVF;
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2076
    TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%p,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dptPrimitiveType, d3dvtVertexType, lpvVertices, dwNumVertices, dwFlags);
2077 2078 2079 2080 2081 2082 2083

    switch(d3dvtVertexType)
    {
        case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
        case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
        case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
        default:
2084 2085
            ERR("Unexpected vertex type %d\n", d3dvtVertexType);
            return DDERR_INVALIDPARAMS;  /* Should never happen */
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
    };

    return IDirect3DDevice3_BeginIndexed(ICOM_INTERFACE(This,IDirect3DDevice3),
                                         d3dptPrimitiveType,
                                         FVF,
                                         lpvVertices,
                                         dwNumVertices,
                                         dwFlags);
}

/*****************************************************************************
 * IDirect3DDevice3::Vertex
 *
 * Draws a vertex as described by IDirect3DDevice3::Begin. It places all
2100
 * drawn vertices in a vertex buffer. If the buffer is too small, its
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122
 * size is increased.
 *
 * Version 2 and 3
 *
 * Params:
 *  Vertex: Pointer to the vertex
 *
 * Returns:
 *  D3D_OK, on success
 *  DDERR_INVALIDPARAMS if Vertex is NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_Vertex(IDirect3DDevice3 *iface,
                             void *Vertex)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE("(%p)->(%p)\n", This, Vertex);

    if(!Vertex)
        return DDERR_INVALIDPARAMS;

2123
    EnterCriticalSection(&ddraw_cs);
2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138
    if ((This->nb_vertices+1)*This->vertex_size > This->buffer_size)
    {
        BYTE *old_buffer;
        This->buffer_size = This->buffer_size ? This->buffer_size * 2 : This->vertex_size * 3;
        old_buffer = This->vertex_buffer;
        This->vertex_buffer = HeapAlloc(GetProcessHeap(), 0, This->buffer_size);
        if (old_buffer)
        {
            CopyMemory(This->vertex_buffer, old_buffer, This->nb_vertices * This->vertex_size);
            HeapFree(GetProcessHeap(), 0, old_buffer);
        }
    }

    CopyMemory(This->vertex_buffer + This->nb_vertices++ * This->vertex_size, Vertex, This->vertex_size);

2139
    LeaveCriticalSection(&ddraw_cs);
2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_Vertex(IDirect3DDevice2 *iface,
                                   void *lpVertexType)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, lpVertexType);
    return IDirect3DDevice3_Vertex(ICOM_INTERFACE(This, IDirect3DDevice3),
                                                  lpVertexType);
}

/*****************************************************************************
 * IDirect3DDevice3::Index
 *
2156
 * Specifies an index to a vertex to be drawn. The vertex array has to
2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
 * be specified with BeginIndexed first.
 *
 * Parameters:
 *  VertexIndex: The index of the vertex to draw
 *
 * Returns:
 *  D3D_OK because it's a stub
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_Index(IDirect3DDevice3 *iface,
                            WORD VertexIndex)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    FIXME("(%p)->(%04x): stub!\n", This, VertexIndex);
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_Index(IDirect3DDevice2 *iface,
                                  WORD wVertexIndex)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%04x) thunking to IDirect3DDevice3 interface.\n", This, wVertexIndex);
    return IDirect3DDevice3_Index(ICOM_INTERFACE(This, IDirect3DDevice3),
                                  wVertexIndex);
}

/*****************************************************************************
 * IDirect3DDevice3::End
 *
 * Ends a draw begun with IDirect3DDevice3::Begin or
 * IDirect3DDevice::BeginIndexed. The vertices specified with
 * IDirect3DDevice::Vertex or IDirect3DDevice::Index are drawn using
 * the IDirect3DDevice7::DrawPrimitive method. So far only
 * non-indexed mode is supported
 *
 * Version 2 and 3
 *
 * Params:
 *  Flags: Some flags, as usual. Don't know which are defined
 *
 * Returns:
 *  The return value of IDirect3DDevice7::DrawPrimitive
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_End(IDirect3DDevice3 *iface,
                          DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2208
    TRACE("(%p)->(%08x)\n", This, Flags);
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220

    return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          This->primitive_type, This->vertex_type,
                                          This->vertex_buffer, This->nb_vertices,
                                          This->render_flags);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_End(IDirect3DDevice2 *iface,
                                DWORD dwFlags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2221
    TRACE_(ddraw_thunk)("(%p)->(%08x) thunking to IDirect3DDevice3 interface.\n", This, dwFlags);
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248
    return IDirect3DDevice3_End(ICOM_INTERFACE(This, IDirect3DDevice3),
                                dwFlags);
}

/*****************************************************************************
 * IDirect3DDevice7::GetRenderState
 *
 * Returns the value of a render state. The possible render states are
 * defined in include/d3dtypes.h
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  RenderStateType: Render state to return the current setting of
 *  Value: Address to store the value at
 *
 * Returns:
 *  D3D_OK on success, for details see IWineD3DDevice::GetRenderState
 *  DDERR_INVALIDPARAMS if Value == NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetRenderState(IDirect3DDevice7 *iface,
                                     D3DRENDERSTATETYPE RenderStateType,
                                     DWORD *Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2249
    HRESULT hr;
2250 2251 2252 2253 2254
    TRACE("(%p)->(%08x,%p): Relay\n", This, RenderStateType, Value);

    if(!Value)
        return DDERR_INVALIDPARAMS;

2255
    EnterCriticalSection(&ddraw_cs);
2256 2257 2258 2259 2260 2261 2262 2263 2264
    switch(RenderStateType)
    {
        case D3DRENDERSTATE_TEXTUREHANDLE:
        {
            /* This state is wrapped to SetTexture in SetRenderState, so
             * it has to be wrapped to GetTexture here
             */
            IWineD3DBaseTexture *tex = NULL;
            *Value = 0;
2265

2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
            hr = IWineD3DDevice_GetTexture(This->wineD3DDevice,
                                           0,
                                           &tex);

            if(hr == WINED3D_OK && tex)
            {
                IDirectDrawSurface7 *parent = NULL;
                hr = IWineD3DBaseTexture_GetParent(tex,
                                                   (IUnknown **) &parent);
                if(parent)
                {
                    /* The parent of the texture is the IDirectDrawSurface7 interface
                     * of the ddraw surface
                     */
                    IDirectDrawSurfaceImpl *texImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
                                                                  IDirectDrawSurface7,
                                                                  parent);
                    *Value = texImpl->Handle;
                    IDirectDrawSurface7_Release(parent);
                }
2286
                IWineD3DBaseTexture_Release(tex);
2287
            }
2288
            break;
2289 2290
        }

2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310
        case D3DRENDERSTATE_TEXTUREMAG:
        {
            WINED3DTEXTUREFILTERTYPE tex_mag;

            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_MAGFILTER,
                                                &tex_mag);

            switch (tex_mag)
            {
                case WINED3DTEXF_POINT:
                    *Value = D3DFILTER_NEAREST;
                    break;
                case WINED3DTEXF_LINEAR:
                    *Value = D3DFILTER_LINEAR;
                    break;
                default:
                    ERR("Unhandled texture mag %d !\n",tex_mag);
                    *Value = 0;
            }
2311
            break;
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
        }

        case D3DRENDERSTATE_TEXTUREMIN:
        {
            WINED3DTEXTUREFILTERTYPE tex_min;

            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_MINFILTER,
                                                &tex_min);

            switch (tex_min)
            {
                case WINED3DTEXF_POINT:
                    *Value = D3DFILTER_NEAREST;
                    break;
                case WINED3DTEXF_LINEAR:
                    *Value = D3DFILTER_LINEAR;
                    break;
                default:
                    ERR("Unhandled texture mag %d !\n",tex_min);
                    *Value = 0;
            }
2334
            break;
2335 2336
        }

2337
        case D3DRENDERSTATE_TEXTUREADDRESS:
2338
        case D3DRENDERSTATE_TEXTUREADDRESSU:
2339 2340 2341 2342
            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_ADDRESSU,
                                                Value);
            break;
2343
        case D3DRENDERSTATE_TEXTUREADDRESSV:
2344 2345 2346 2347
            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_ADDRESSV,
                                                Value);
            break;
2348

2349 2350
        default:
            /* FIXME: Unhandled: D3DRENDERSTATE_STIPPLEPATTERN00 - 31 */
2351 2352 2353
            hr = IWineD3DDevice_GetRenderState(This->wineD3DDevice,
                                               RenderStateType,
                                               Value);
2354
    }
2355 2356
    LeaveCriticalSection(&ddraw_cs);
    return hr;
2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetRenderState(IDirect3DDevice3 *iface,
                                           D3DRENDERSTATETYPE dwRenderStateType,
                                           DWORD *lpdwRenderState)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
    return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           dwRenderStateType,
                                           lpdwRenderState);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetRenderState(IDirect3DDevice2 *iface,
                                           D3DRENDERSTATETYPE dwRenderStateType,
                                           DWORD *lpdwRenderState)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
    return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           dwRenderStateType,
                                           lpdwRenderState);
}

/*****************************************************************************
 * IDirect3DDevice7::SetRenderState
 *
 * Sets a render state. The possible render states are defined in
 * include/d3dtypes.h
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  RenderStateType: State to set
 *  Value: Value to assign to that state
 *
 * Returns:
 *  D3D_OK on success,
 *  for details see IWineD3DDevice::SetRenderState
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetRenderState(IDirect3DDevice7 *iface,
                                     D3DRENDERSTATETYPE RenderStateType,
                                     DWORD Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2406
    HRESULT hr;
2407
    TRACE("(%p)->(%08x,%d): Relay\n", This, RenderStateType, Value);
2408

2409
    EnterCriticalSection(&ddraw_cs);
2410 2411 2412 2413
    /* Some render states need special care */
    switch(RenderStateType)
    {
        case D3DRENDERSTATE_TEXTUREHANDLE:
2414 2415 2416
        {
            if(Value == 0)
            {
2417 2418 2419 2420
                hr = IWineD3DDevice_SetTexture(This->wineD3DDevice,
                                               0,
                                               NULL);
                break;
2421 2422 2423 2424
            }

            if(Value > This->numHandles)
            {
2425
                FIXME("Specified handle %d out of range\n", Value);
2426 2427
                hr = DDERR_INVALIDPARAMS;
                break;
2428 2429 2430
            }
            if(This->Handles[Value - 1].type != DDrawHandle_Texture)
            {
2431
                FIXME("Handle %d isn't a texture handle\n", Value);
2432 2433
                hr = DDERR_INVALIDPARAMS;
                break;
2434 2435 2436 2437
            }
            else
            {
                IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[Value - 1].ptr;
2438 2439 2440 2441
                hr = IWineD3DDevice_SetTexture(This->wineD3DDevice,
                                               0,
                                               surf->wineD3DTexture);
                break;
2442 2443
            }
        }
2444 2445 2446 2447 2448 2449 2450 2451

        case D3DRENDERSTATE_TEXTUREMAG:
        {
            WINED3DTEXTUREFILTERTYPE tex_mag = WINED3DTEXF_NONE;

            switch ((D3DTEXTUREFILTER) Value)
            {
                case D3DFILTER_NEAREST:
2452
                case D3DFILTER_LINEARMIPNEAREST:
2453 2454 2455
                    tex_mag = WINED3DTEXF_POINT;
                    break;
                case D3DFILTER_LINEAR:
2456
                case D3DFILTER_LINEARMIPLINEAR:
2457 2458 2459
                    tex_mag = WINED3DTEXF_LINEAR;
                    break;
                default:
2460
                    ERR("Unhandled texture mag %d !\n",Value);
2461 2462
            }

2463 2464 2465 2466
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_MAGFILTER,
                                                tex_mag);
            break;
2467 2468 2469 2470 2471
        }

        case D3DRENDERSTATE_TEXTUREMIN:
        {
            WINED3DTEXTUREFILTERTYPE tex_min = WINED3DTEXF_NONE;
2472
            WINED3DTEXTUREFILTERTYPE tex_mip = WINED3DTEXF_NONE;
2473 2474 2475 2476 2477 2478 2479 2480 2481

            switch ((D3DTEXTUREFILTER) Value)
            {
                case D3DFILTER_NEAREST:
                    tex_min = WINED3DTEXF_POINT;
                    break;
                case D3DFILTER_LINEAR:
                    tex_min = WINED3DTEXF_LINEAR;
                    break;
2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498
                case D3DFILTER_MIPNEAREST:
                    tex_min = WINED3DTEXF_NONE;
                    tex_mip = WINED3DTEXF_POINT;
                    break;
                case D3DFILTER_MIPLINEAR:
                    tex_min = WINED3DTEXF_NONE;
                    tex_mip = WINED3DTEXF_LINEAR;
                    break;
                case D3DFILTER_LINEARMIPNEAREST:
                    tex_min = WINED3DTEXF_POINT;
                    tex_mip = WINED3DTEXF_LINEAR;
                    break;
                case D3DFILTER_LINEARMIPLINEAR:
                    tex_min = WINED3DTEXF_LINEAR;
                    tex_mip = WINED3DTEXF_LINEAR;
                    break;

2499
                default:
2500
                    ERR("Unhandled texture min %d !\n",Value);
2501 2502
            }

2503 2504 2505
                   IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                  0, WINED3DSAMP_MIPFILTER,
                                                  tex_mip);
2506 2507 2508 2509
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_MINFILTER,
                                                tex_min);
            break;
2510 2511
        }

2512 2513 2514 2515 2516
        case D3DRENDERSTATE_TEXTUREADDRESS:
                   IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                  0, WINED3DSAMP_ADDRESSV,
                                                  Value);
            /* Drop through */
2517
        case D3DRENDERSTATE_TEXTUREADDRESSU:
2518 2519 2520 2521
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_ADDRESSU,
                                                Value);
            break;
2522
        case D3DRENDERSTATE_TEXTUREADDRESSV:
2523 2524 2525 2526
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                0, WINED3DSAMP_ADDRESSV,
                                                Value);
            break;
2527

2528 2529 2530 2531 2532 2533 2534 2535
        case D3DRENDERSTATE_TEXTUREMAPBLEND:
        {
            /* Old texture combine setup style, superseded by texture stage states
             * in D3D7. It is safe for us to wrap it to texture stage states.
             */
            switch ( (D3DTEXTUREBLEND) Value)
            {
                case D3DTBLEND_MODULATE:
2536 2537 2538 2539 2540
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG2, WINED3DTA_CURRENT);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_MODULATE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_SELECTARG1);
2541 2542 2543
                    break;

                case D3DTBLEND_MODULATEALPHA:
2544 2545 2546 2547 2548 2549
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG2, WINED3DTA_CURRENT);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG2, WINED3DTA_CURRENT);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_MODULATE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_MODULATE);
2550 2551 2552
                    break;

                case D3DTBLEND_DECAL:
2553 2554 2555 2556
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_SELECTARG1);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_SELECTARG1);
2557 2558 2559
                    break;

                case D3DTBLEND_DECALALPHA:
2560 2561 2562 2563 2564
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG2, WINED3DTA_CURRENT);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_SELECTARG1);
                    IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_MODULATE);
2565 2566 2567
                    break;

                default:
2568
                    ERR("Unhandled texture environment %d !\n",Value);
2569
                }
2570
                hr = D3D_OK;
2571 2572 2573
            break;
        }

2574
        default:
2575 2576 2577

            /* FIXME: Unhandled: D3DRENDERSTATE_STIPPLEPATTERN00 - 31 */

2578 2579 2580 2581
            hr = IWineD3DDevice_SetRenderState(This->wineD3DDevice,
                                               RenderStateType,
                                               Value);
            break;
2582
    }
2583 2584
    LeaveCriticalSection(&ddraw_cs);
    return hr;
2585 2586 2587 2588 2589 2590 2591 2592
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
                                           D3DRENDERSTATETYPE RenderStateType,
                                           DWORD Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2593
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604
    return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           RenderStateType,
                                           Value);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_SetRenderState(IDirect3DDevice2 *iface,
                                           D3DRENDERSTATETYPE RenderStateType,
                                           DWORD Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2605
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624
    return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           RenderStateType,
                                           Value);
}

/*****************************************************************************
 * Direct3DDevice3::SetLightState
 *
 * Sets a light state for Direct3DDevice3 and Direct3DDevice2. The
 * light states are forwarded to Direct3DDevice7 render states
 *
 * Version 2 and 3
 *
 * Params:
 *  LightStateType: The light state to change
 *  Value: The value to assign to that light state
 *
 * Returns:
 *  D3D_OK on success
2625
 *  DDERR_INVALIDPARAMS if the parameters were incorrect
2626 2627 2628 2629 2630 2631 2632 2633 2634
 *  Also check IDirect3DDevice7::SetRenderState
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
                                    D3DLIGHTSTATETYPE LightStateType,
                                    DWORD Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2635
    HRESULT hr;
2636

2637
    TRACE("(%p)->(%08x,%08x)\n", This, LightStateType, Value);
2638 2639 2640 2641 2642 2643 2644

    if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
    {
        TRACE("Unexpected Light State Type\n");
        return DDERR_INVALIDPARAMS;
    }

2645
    EnterCriticalSection(&ddraw_cs);
2646 2647
    if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
    {
2648 2649 2650 2651 2652
        IDirect3DMaterialImpl *mat;

        if(Value == 0) mat = NULL;
        else if(Value > This->numHandles)
        {
2653
            ERR("Material handle out of range(%d)\n", Value);
2654
            LeaveCriticalSection(&ddraw_cs);
2655 2656 2657 2658
            return DDERR_INVALIDPARAMS;
        }
        else if(This->Handles[Value - 1].type != DDrawHandle_Material)
        {
2659
            ERR("Invalid handle %d\n", Value);
2660
            LeaveCriticalSection(&ddraw_cs);
2661 2662 2663 2664 2665 2666
            return DDERR_INVALIDPARAMS;
        }
        else
        {
            mat = (IDirect3DMaterialImpl *) This->Handles[Value - 1].ptr;
        }
2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691

        if (mat != NULL)
        {
            TRACE(" activating material %p.\n", mat);
            mat->activate(mat);
        }
        else
        {
            FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
        }
        This->material = Value;
    }
    else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
    {
        switch (Value)
        {
            case D3DCOLOR_MONO:
                ERR("DDCOLOR_MONO should not happen!\n");
                break;
            case D3DCOLOR_RGB:
                /* We are already in this mode */
                TRACE("Setting color model to RGB (no-op).\n");
                break;
            default:
                ERR("Unknown color model!\n");
2692
                LeaveCriticalSection(&ddraw_cs);
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720
                return DDERR_INVALIDPARAMS;
        }
    }
    else
    {
        D3DRENDERSTATETYPE rs;
        switch (LightStateType)
        {
            case D3DLIGHTSTATE_AMBIENT:       /* 2 */
                rs = D3DRENDERSTATE_AMBIENT;
                break;		
            case D3DLIGHTSTATE_FOGMODE:       /* 4 */
                rs = D3DRENDERSTATE_FOGVERTEXMODE;
                break;
            case D3DLIGHTSTATE_FOGSTART:      /* 5 */
                rs = D3DRENDERSTATE_FOGSTART;
                break;
            case D3DLIGHTSTATE_FOGEND:        /* 6 */
                rs = D3DRENDERSTATE_FOGEND;
                break;
            case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
                rs = D3DRENDERSTATE_FOGDENSITY;
                break;
            case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
                rs = D3DRENDERSTATE_COLORVERTEX;
                break;
            default:
                ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2721
                LeaveCriticalSection(&ddraw_cs);
2722 2723 2724
                return DDERR_INVALIDPARAMS;
        }

2725 2726 2727 2728 2729
        hr = IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                             rs,
                                             Value);
        LeaveCriticalSection(&ddraw_cs);
        return hr;
2730 2731
    }

2732
    LeaveCriticalSection(&ddraw_cs);
2733 2734 2735 2736 2737 2738 2739 2740 2741
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_SetLightState(IDirect3DDevice2 *iface,
                                          D3DLIGHTSTATETYPE LightStateType,
                                          DWORD Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2742
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
    return IDirect3DDevice3_SetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
                                          LightStateType,
                                          Value);
}

/*****************************************************************************
 * IDirect3DDevice3::GetLightState
 *
 * Returns the current setting of a light state. The state is read from
 * the Direct3DDevice7 render state.
 *
 * Version 2 and 3
 *
 * Params:
 *  LightStateType: The light state to return
 *  Value: The address to store the light state setting at
 *
 * Returns:
 *  D3D_OK on success
 *  DDDERR_INVALIDPARAMS if the parameters were incorrect
 *  Also see IDirect3DDevice7::GetRenderState
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_3_GetLightState(IDirect3DDevice3 *iface,
                                    D3DLIGHTSTATETYPE LightStateType,
                                    DWORD *Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2772
    HRESULT hr;
2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784

    TRACE("(%p)->(%08x,%p)\n", This, LightStateType, Value);

    if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
    {
        TRACE("Unexpected Light State Type\n");
        return DDERR_INVALIDPARAMS;
    }

    if(!Value)
        return DDERR_INVALIDPARAMS;

2785
    EnterCriticalSection(&ddraw_cs);
2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818
    if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
    {
        *Value = This->material;
    }
    else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
    {
        *Value = D3DCOLOR_RGB;
    }
    else
    {
        D3DRENDERSTATETYPE rs;
        switch (LightStateType)
        {
            case D3DLIGHTSTATE_AMBIENT:       /* 2 */
                rs = D3DRENDERSTATE_AMBIENT;
                break;		
            case D3DLIGHTSTATE_FOGMODE:       /* 4 */
                rs = D3DRENDERSTATE_FOGVERTEXMODE;
                break;
            case D3DLIGHTSTATE_FOGSTART:      /* 5 */
                rs = D3DRENDERSTATE_FOGSTART;
                break;
            case D3DLIGHTSTATE_FOGEND:        /* 6 */
                rs = D3DRENDERSTATE_FOGEND;
                break;
            case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
                rs = D3DRENDERSTATE_FOGDENSITY;
                break;
            case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
                rs = D3DRENDERSTATE_COLORVERTEX;
                break;
            default:
                ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2819
                LeaveCriticalSection(&ddraw_cs);
2820 2821 2822
                return DDERR_INVALIDPARAMS;
        }

2823 2824 2825 2826 2827
        hr = IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                             rs,
                                             Value);
        LeaveCriticalSection(&ddraw_cs);
        return hr;
2828 2829
    }

2830
    LeaveCriticalSection(&ddraw_cs);
2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetLightState(IDirect3DDevice2 *iface,
                                          D3DLIGHTSTATETYPE LightStateType,
                                          DWORD *Value)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
    return IDirect3DDevice3_GetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
                                          LightStateType,
                                          Value);
}

/*****************************************************************************
 * IDirect3DDevice7::SetTransform
 *
2849
 * Assigns a D3DMATRIX to a transform type. The transform types are defined
2850 2851 2852
 * in include/d3dtypes.h.
 * The D3DTRANSFORMSTATE_WORLD (=1) is translated to D3DTS_WORLDMATRIX(0)
 * (=255) for wined3d, because the 1 transform state was removed in d3d8
2853
 * and WineD3D already understands the replacement D3DTS_WORLDMATRIX(0)
2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  TransformStateType: transform state to set
 *  Matrix: Matrix to assign to the state
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Matrix == NULL
 *  For details see IWineD3DDevice::SetTransform
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetTransform(IDirect3DDevice7 *iface,
                                   D3DTRANSFORMSTATETYPE TransformStateType,
                                   D3DMATRIX *Matrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    D3DTRANSFORMSTATETYPE type = TransformStateType;
2874
    HRESULT hr;
2875 2876
    TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);

2877 2878 2879 2880 2881 2882 2883 2884
    switch(TransformStateType)
    {
        case D3DTRANSFORMSTATE_WORLD :  type = WINED3DTS_WORLDMATRIX(0); break;
        case D3DTRANSFORMSTATE_WORLD1:  type = WINED3DTS_WORLDMATRIX(1); break;
        case D3DTRANSFORMSTATE_WORLD2:  type = WINED3DTS_WORLDMATRIX(2); break;
        case D3DTRANSFORMSTATE_WORLD3:  type = WINED3DTS_WORLDMATRIX(3); break;
        default:                        type = TransformStateType;
    }
2885

2886 2887 2888
    if(!Matrix)
       return DDERR_INVALIDPARAMS;

2889
    /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2890 2891 2892 2893 2894 2895
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_SetTransform(This->wineD3DDevice,
                                     type,
                                     (WINED3DMATRIX*) Matrix);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_SetTransform(IDirect3DDevice3 *iface,
                                         D3DTRANSFORMSTATETYPE TransformStateType,
                                         D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
    return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
                                         TransformStateType,
                                         D3DMatrix);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_SetTransform(IDirect3DDevice2 *iface,
                                         D3DTRANSFORMSTATETYPE TransformStateType,
                                         D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
    return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
                                         TransformStateType,
                                         D3DMatrix);
}

/*****************************************************************************
 * IDirect3DDevice7::GetTransform
 *
 * Returns the matrix assigned to a transform state
 * D3DTRANSFORMSTATE_WORLD is translated to D3DTS_WORLDMATRIX(0), see
 * SetTransform
 *
 * Params:
 *  TransformStateType: State to read the matrix from
 *  Matrix: Address to store the matrix at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Matrix == NULL
 *  For details, see IWineD3DDevice::GetTransform
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetTransform(IDirect3DDevice7 *iface,
                                   D3DTRANSFORMSTATETYPE TransformStateType,
                                   D3DMATRIX *Matrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    D3DTRANSFORMSTATETYPE type = TransformStateType;
2946
    HRESULT hr;
2947 2948
    TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);

2949 2950 2951 2952 2953 2954 2955 2956
    switch(TransformStateType)
    {
        case D3DTRANSFORMSTATE_WORLD :  type = WINED3DTS_WORLDMATRIX(0); break;
        case D3DTRANSFORMSTATE_WORLD1:  type = WINED3DTS_WORLDMATRIX(1); break;
        case D3DTRANSFORMSTATE_WORLD2:  type = WINED3DTS_WORLDMATRIX(2); break;
        case D3DTRANSFORMSTATE_WORLD3:  type = WINED3DTS_WORLDMATRIX(3); break;
        default:                        type = TransformStateType;
    }
2957

2958 2959 2960
    if(!Matrix)
        return DDERR_INVALIDPARAMS;

2961
    /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2962 2963 2964 2965
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_GetTransform(This->wineD3DDevice, type, (WINED3DMATRIX*) Matrix);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetTransform(IDirect3DDevice3 *iface,
                                         D3DTRANSFORMSTATETYPE TransformStateType,
                                         D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
    return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
                                         TransformStateType,
                                         D3DMatrix);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetTransform(IDirect3DDevice2 *iface,
                                         D3DTRANSFORMSTATETYPE TransformStateType,
                                         D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
    return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
                                         TransformStateType,
                                         D3DMatrix);
}

/*****************************************************************************
 * IDirect3DDevice7::MultiplyTransform
 *
 * Multiplies the already-set transform matrix of a transform state
 * with another matrix. For the world matrix, see SetTransform
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  TransformStateType: Transform state to multiply
 *  D3DMatrix Matrix to multiply with.
 *
 * Returns
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if D3DMatrix is NULL
 *  For details, see IWineD3DDevice::MultiplyTransform
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_MultiplyTransform(IDirect3DDevice7 *iface,
                                        D3DTRANSFORMSTATETYPE TransformStateType,
                                        D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3016
    HRESULT hr;
3017
    D3DTRANSFORMSTATETYPE type;
3018 3019
    TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, D3DMatrix);

3020 3021 3022 3023 3024 3025 3026 3027
    switch(TransformStateType)
    {
        case D3DTRANSFORMSTATE_WORLD :  type = WINED3DTS_WORLDMATRIX(0); break;
        case D3DTRANSFORMSTATE_WORLD1:  type = WINED3DTS_WORLDMATRIX(1); break;
        case D3DTRANSFORMSTATE_WORLD2:  type = WINED3DTS_WORLDMATRIX(2); break;
        case D3DTRANSFORMSTATE_WORLD3:  type = WINED3DTS_WORLDMATRIX(3); break;
        default:                        type = TransformStateType;
    }
3028

3029
    /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
3030 3031 3032 3033 3034 3035
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_MultiplyTransform(This->wineD3DDevice,
                                          type,
                                          (WINED3DMATRIX*) D3DMatrix);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_MultiplyTransform(IDirect3DDevice3 *iface,
                                              D3DTRANSFORMSTATETYPE TransformStateType,
                                              D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
    return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
                                              TransformStateType,
                                              D3DMatrix);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_MultiplyTransform(IDirect3DDevice2 *iface,
                                              D3DTRANSFORMSTATETYPE TransformStateType,
                                              D3DMATRIX *D3DMatrix)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
    return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
                                              TransformStateType,
                                              D3DMatrix);
}

/*****************************************************************************
 * IDirect3DDevice7::DrawPrimitive
 *
3065
 * Draws primitives based on vertices in an application-provided pointer
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093
 *
 * Version 2, 3 and 7. The IDirect3DDevice2 thunk converts the fixed vertex type into
 * an FVF format for D3D7
 *
 * Params:
 *  PrimitiveType: The type of the primitives to draw
 *  Vertex type: Flexible vertex format vertex description
 *  Vertices: Pointer to the vertex array
 *  VertexCount: The number of vertices to draw
 *  Flags: As usual a few flags
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Vertices is NULL
 *  For details, see IWineD3DDevice::DrawPrimitiveUP
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
                                    D3DPRIMITIVETYPE PrimitiveType,
                                    DWORD VertexType,
                                    void *Vertices,
                                    DWORD VertexCount,
                                    DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    UINT PrimitiveCount, stride;
    HRESULT hr;
3094
    TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125

    if(!Vertices)
        return DDERR_INVALIDPARAMS;

    /* Get the vertex count */
    switch(PrimitiveType)
    {
      case D3DPT_POINTLIST: 
        PrimitiveCount = VertexCount;
        break;

      case D3DPT_LINELIST: 
        PrimitiveCount = VertexCount / 2;
        break;

      case D3DPT_LINESTRIP:
        PrimitiveCount = VertexCount - 1;
        break;

      case D3DPT_TRIANGLELIST:
        PrimitiveCount = VertexCount / 3;
        break;

      case D3DPT_TRIANGLESTRIP:
        PrimitiveCount = VertexCount - 2;
        break;

      case D3DPT_TRIANGLEFAN:
        PrimitiveCount = VertexCount - 2;
        break;

3126 3127
      default:
        return DDERR_INVALIDPARAMS;
3128 3129 3130 3131 3132 3133
    }

    /* Get the stride */
    stride = get_flexible_vertex_size(VertexType);

    /* Set the FVF */
3134
    EnterCriticalSection(&ddraw_cs);
3135 3136
    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
                                             IDirectDrawImpl_FindDecl(This->ddraw, VertexType));
3137 3138 3139 3140 3141
    if(hr != D3D_OK)
    {
        LeaveCriticalSection(&ddraw_cs);
        return hr;
    }
3142 3143

    /* This method translates to the user pointer draw of WineD3D */
3144 3145 3146 3147 3148 3149 3150
    hr = IWineD3DDevice_DrawPrimitiveUP(This->wineD3DDevice,
                                        PrimitiveType,
                                        PrimitiveCount,
                                        Vertices,
                                        stride);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_DrawPrimitive(IDirect3DDevice3 *iface,
                                          D3DPRIMITIVETYPE PrimitiveType,
                                          DWORD VertexType,
                                          void *Vertices,
                                          DWORD VertexCount,
                                          DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3162
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180
    return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          PrimitiveType,
                                          VertexType,
                                          Vertices,
                                          VertexCount,
                                          Flags);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
                                          D3DPRIMITIVETYPE PrimitiveType,
                                          D3DVERTEXTYPE VertexType,
                                          void *Vertices,
                                          DWORD VertexCount,
                                          DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    DWORD FVF;
3181
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
3182 3183 3184 3185 3186 3187 3188

    switch(VertexType)
    {
        case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
        case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
        case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
        default:
3189 3190
            ERR("Unexpected vertex type %d\n", VertexType);
            return DDERR_INVALIDPARAMS;  /* Should never happen */
3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237
    }

    return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          PrimitiveType,
                                          FVF,
                                          Vertices,
                                          VertexCount,
                                          Flags);
}

/*****************************************************************************
 * IDirect3DDevice7::DrawIndexedPrimitive
 *
 * Draws vertices from an application-provided pointer, based on the index
 * numbers in a WORD array.
 *
 * Version 2, 3 and 7. The version 7 thunk translates the vertex type into
 * an FVF format for D3D7
 *
 * Params:
 *  PrimitiveType: The primitive type to draw
 *  VertexType: The FVF vertex description
 *  Vertices: Pointer to the vertex array
 *  VertexCount: ?
 *  Indices: Pointer to the index array
 *  IndexCount: Number of indices = Number of vertices to draw
 *  Flags: As usual, some flags
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Vertices or Indices is NULL
 *  For details, see IWineD3DDevice::DrawIndexedPrimitiveUP
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
                                           D3DPRIMITIVETYPE PrimitiveType,
                                           DWORD VertexType,
                                           void *Vertices,
                                           DWORD VertexCount,
                                           WORD *Indices,
                                           DWORD IndexCount,
                                           DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    UINT PrimitiveCount = 0;
    HRESULT hr;
3238
    TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3239

3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266
    /* Get the primitive number */
    switch(PrimitiveType)
    {
      case D3DPT_POINTLIST: 
        PrimitiveCount = IndexCount;
        break;

      case D3DPT_LINELIST: 
        PrimitiveCount = IndexCount / 2;
        break;

      case D3DPT_LINESTRIP:
        PrimitiveCount = IndexCount - 1;
        break;

      case D3DPT_TRIANGLELIST:
        PrimitiveCount = IndexCount / 3;
        break;

      case D3DPT_TRIANGLESTRIP:
        PrimitiveCount = IndexCount - 2;
        break;

      case D3DPT_TRIANGLEFAN:
        PrimitiveCount = IndexCount - 2;
        break;

3267 3268
      default:
        return DDERR_INVALIDPARAMS;
3269 3270 3271
    }

    /* Set the D3DDevice's FVF */
3272
    EnterCriticalSection(&ddraw_cs);
3273 3274
    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
                                             IDirectDrawImpl_FindDecl(This->ddraw, VertexType));
3275 3276
    if(FAILED(hr))
    {
3277
        ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3278
        LeaveCriticalSection(&ddraw_cs);
3279 3280 3281
        return hr;
    }

3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292
    hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->wineD3DDevice,
                                               PrimitiveType,
                                               0 /* MinVertexIndex */,
                                               VertexCount /* UINT NumVertexIndex */,
                                               PrimitiveCount,
                                               Indices,
                                               WINED3DFMT_INDEX16,
                                               Vertices,
                                               get_flexible_vertex_size(VertexType));
    LeaveCriticalSection(&ddraw_cs);
    return hr;
3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
                                                 D3DPRIMITIVETYPE PrimitiveType,
                                                 DWORD VertexType,
                                                 void *Vertices,
                                                 DWORD VertexCount,
                                                 WORD *Indices,
                                                 DWORD IndexCount,
                                                 DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3306
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328
    return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                 PrimitiveType,
                                                 VertexType,
                                                 Vertices,
                                                 VertexCount,
                                                 Indices,
                                                 IndexCount,
                                                 Flags);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
                                                 D3DPRIMITIVETYPE PrimitiveType,
                                                 D3DVERTEXTYPE VertexType,
                                                 void *Vertices,
                                                 DWORD VertexCount,
                                                 WORD *Indices,
                                                 DWORD IndexCount,
                                                 DWORD Flags)
{
    DWORD FVF;
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3329
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3330 3331 3332 3333 3334 3335 3336

    switch(VertexType)
    {
        case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
        case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
        case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
        default:
3337 3338
            ERR("Unexpected vertex type %d\n", VertexType);
            return DDERR_INVALIDPARAMS;  /* Should never happen */
3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453
    }

    return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                 PrimitiveType,
                                                 FVF,
                                                 Vertices,
                                                 VertexCount,
                                                 Indices,
                                                 IndexCount,
                                                 Flags);
}

/*****************************************************************************
 * IDirect3DDevice7::SetClipStatus
 *
 * Sets the clip status. This defines things as clipping conditions and
 * the extents of the clipping region.
 *
 * Version 2, 3 and 7
 *
 * Params:
 *  ClipStatus:
 *
 * Returns:
 *  D3D_OK because it's a stub
 *  (DDERR_INVALIDPARAMS if ClipStatus == NULL)
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetClipStatus(IDirect3DDevice7 *iface,
                                    D3DCLIPSTATUS *ClipStatus)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);

    /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
     * Perhaps this needs a new data type and an additional IWineD3DDevice method
     */
    /* return IWineD3DDevice_SetClipStatus(This->wineD3DDevice, ClipStatus);*/
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_SetClipStatus(IDirect3DDevice3 *iface,
                                          D3DCLIPSTATUS *ClipStatus)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
    return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          ClipStatus);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_SetClipStatus(IDirect3DDevice2 *iface,
                                          D3DCLIPSTATUS *ClipStatus)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
    return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          ClipStatus);
}

/*****************************************************************************
 * IDirect3DDevice7::GetClipStatus
 *
 * Returns the clip status
 *
 * Params:
 *  ClipStatus: Address to write the clip status to
 *
 * Returns:
 *  D3D_OK because it's a stub
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetClipStatus(IDirect3DDevice7 *iface,
                                    D3DCLIPSTATUS *ClipStatus)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);

    /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
    /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetClipStatus(IDirect3DDevice3 *iface,
                                          D3DCLIPSTATUS *ClipStatus)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
    return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          ClipStatus);
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_2_GetClipStatus(IDirect3DDevice2 *iface,
                                          D3DCLIPSTATUS *ClipStatus)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
    return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
                                          ClipStatus);
}

/*****************************************************************************
 * IDirect3DDevice::DrawPrimitiveStrided
 *
 * Draws vertices described by a D3DDRAWPRIMITIVESTRIDEDDATA structure.
 *
 * Version 3 and 7
 *
 * Params:
 *  PrimitiveType: The primitive type to draw
3454
 *  VertexType: The FVF description of the vertices to draw (for the stride??)
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477
 *  D3DDrawPrimStrideData: A D3DDRAWPRIMITIVESTRIDEDDATA structure describing
 *                         the vertex data locations
 *  VertexCount: The number of vertices to draw
 *  Flags: Some flags
 *
 * Returns:
 *  D3D_OK, because it's a stub
 *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
 *  (For details, see IWineD3DDevice::DrawPrimitiveStrided)
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DrawPrimitiveStrided(IDirect3DDevice7 *iface,
                                           D3DPRIMITIVETYPE PrimitiveType,
                                           DWORD VertexType,
                                           D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
                                           DWORD VertexCount,
                                           DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    WineDirect3DVertexStridedData WineD3DStrided;
    int i;
    UINT PrimitiveCount;
3478
    HRESULT hr;
3479

3480
    TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3481

3482
    memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
3483 3484 3485
    /* Get the strided data right. the wined3d structure is a bit bigger
     * Watch out: The contents of the strided data are determined by the fvf,
     * not by the members set in D3DDrawPrimStrideData. So it's valid
3486 3487
     * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
     * not set in the fvf.
3488 3489 3490 3491 3492 3493 3494 3495 3496
     */
    if(VertexType & D3DFVF_POSITION_MASK)
    {
        WineD3DStrided.u.s.position.lpData = D3DDrawPrimStrideData->position.lpvData;
        WineD3DStrided.u.s.position.dwStride = D3DDrawPrimStrideData->position.dwStride;
        WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
        if (VertexType & D3DFVF_XYZRHW)
        {
            WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
3497 3498 3499
            WineD3DStrided.u.s.position_transformed = TRUE;
        } else
            WineD3DStrided.u.s.position_transformed = FALSE;
3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532
    }

    if(VertexType & D3DFVF_NORMAL)
    {
        WineD3DStrided.u.s.normal.lpData = D3DDrawPrimStrideData->normal.lpvData;
        WineD3DStrided.u.s.normal.dwStride = D3DDrawPrimStrideData->normal.dwStride;
        WineD3DStrided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
    }

    if(VertexType & D3DFVF_DIFFUSE)
    {
        WineD3DStrided.u.s.diffuse.lpData = D3DDrawPrimStrideData->diffuse.lpvData;
        WineD3DStrided.u.s.diffuse.dwStride = D3DDrawPrimStrideData->diffuse.dwStride;
        WineD3DStrided.u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
    }

    if(VertexType & D3DFVF_SPECULAR)
    {
        WineD3DStrided.u.s.specular.lpData = D3DDrawPrimStrideData->specular.lpvData;
        WineD3DStrided.u.s.specular.dwStride = D3DDrawPrimStrideData->specular.dwStride;
        WineD3DStrided.u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
    }

    for( i = 0; i < GET_TEXCOUNT_FROM_FVF(VertexType); i++)
    {
        WineD3DStrided.u.s.texCoords[i].lpData = D3DDrawPrimStrideData->textureCoords[i].lpvData;
        WineD3DStrided.u.s.texCoords[i].dwStride = D3DDrawPrimStrideData->textureCoords[i].dwStride;
        switch(GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i))
        {
            case 1: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT1; break;
            case 2: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT2; break;
            case 3: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT3; break;
            case 4: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT4; break;
3533
            default: ERR("Unexpected texture coordinate size %d\n",
3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567
                         GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i));
        }
    }

    /* Get the primitive count */
    switch(PrimitiveType)
    {
        case D3DPT_POINTLIST: 
          PrimitiveCount = VertexCount;
          break;

        case D3DPT_LINELIST: 
          PrimitiveCount = VertexCount / 2;
          break;

        case D3DPT_LINESTRIP:
          PrimitiveCount = VertexCount - 1;
          break;

        case D3DPT_TRIANGLELIST:
          PrimitiveCount = VertexCount / 3;
          break;

        case D3DPT_TRIANGLESTRIP:
          PrimitiveCount = VertexCount - 2;
          break;

        case D3DPT_TRIANGLEFAN:
          PrimitiveCount = VertexCount - 2;
          break;

        default: return DDERR_INVALIDPARAMS;
    }

3568
    /* WineD3D doesn't need the FVF here */
3569 3570 3571 3572 3573 3574 3575
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_DrawPrimitiveStrided(This->wineD3DDevice,
                                             PrimitiveType,
                                             PrimitiveCount,
                                             &WineD3DStrided);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
                                                 D3DPRIMITIVETYPE PrimitiveType,
                                                 DWORD VertexType,
                                                 D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
                                                 DWORD VertexCount,
                                                 DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3587
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623
    return IDirect3DDevice7_DrawPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                 PrimitiveType,
                                                 VertexType,
                                                 D3DDrawPrimStrideData,
                                                 VertexCount,
                                                 Flags);
}

/*****************************************************************************
 * IDirect3DDevice7::DrawIndexedPrimitiveStrided
 *
 * Draws primitives specified by strided data locations based on indices
 *
 * Version 3 and 7
 *
 * Params:
 *  PrimitiveType:
 *
 * Returns:
 *  D3D_OK, because it's a stub
 *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
 *  (DDERR_INVALIDPARAMS if Indices is NULL)
 *  (For more details, see IWineD3DDevice::DrawIndexedPrimitiveStrided)
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
                                                  D3DPRIMITIVETYPE PrimitiveType,
                                                  DWORD VertexType,
                                                  D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
                                                  DWORD VertexCount,
                                                  WORD *Indices,
                                                  DWORD IndexCount,
                                                  DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3624 3625 3626 3627 3628 3629
    WineDirect3DVertexStridedData WineD3DStrided;
    int i;
    UINT PrimitiveCount;
    HRESULT hr;

    TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x)\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3630

3631 3632 3633 3634 3635 3636
    memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
    /* Get the strided data right. the wined3d structure is a bit bigger
     * Watch out: The contents of the strided data are determined by the fvf,
     * not by the members set in D3DDrawPrimStrideData. So it's valid
     * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
     * not set in the fvf.
3637
     */
3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727
    if(VertexType & D3DFVF_POSITION_MASK)
    {
        WineD3DStrided.u.s.position.lpData = D3DDrawPrimStrideData->position.lpvData;
        WineD3DStrided.u.s.position.dwStride = D3DDrawPrimStrideData->position.dwStride;
        WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
        if (VertexType & D3DFVF_XYZRHW)
        {
            WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
            WineD3DStrided.u.s.position_transformed = TRUE;
        } else
            WineD3DStrided.u.s.position_transformed = FALSE;
    }

    if(VertexType & D3DFVF_NORMAL)
    {
        WineD3DStrided.u.s.normal.lpData = D3DDrawPrimStrideData->normal.lpvData;
        WineD3DStrided.u.s.normal.dwStride = D3DDrawPrimStrideData->normal.dwStride;
        WineD3DStrided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
    }

    if(VertexType & D3DFVF_DIFFUSE)
    {
        WineD3DStrided.u.s.diffuse.lpData = D3DDrawPrimStrideData->diffuse.lpvData;
        WineD3DStrided.u.s.diffuse.dwStride = D3DDrawPrimStrideData->diffuse.dwStride;
        WineD3DStrided.u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
    }

    if(VertexType & D3DFVF_SPECULAR)
    {
        WineD3DStrided.u.s.specular.lpData = D3DDrawPrimStrideData->specular.lpvData;
        WineD3DStrided.u.s.specular.dwStride = D3DDrawPrimStrideData->specular.dwStride;
        WineD3DStrided.u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
    }

    for( i = 0; i < GET_TEXCOUNT_FROM_FVF(VertexType); i++)
    {
        WineD3DStrided.u.s.texCoords[i].lpData = D3DDrawPrimStrideData->textureCoords[i].lpvData;
        WineD3DStrided.u.s.texCoords[i].dwStride = D3DDrawPrimStrideData->textureCoords[i].dwStride;
        switch(GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i))
        {
            case 1: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT1; break;
            case 2: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT2; break;
            case 3: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT3; break;
            case 4: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT4; break;
            default: ERR("Unexpected texture coordinate size %d\n",
                         GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i));
        }
    }

    /* Get the primitive count */
    switch(PrimitiveType)
    {
        case D3DPT_POINTLIST:
            PrimitiveCount = IndexCount;
            break;

        case D3DPT_LINELIST:
            PrimitiveCount = IndexCount / 2;
            break;

        case D3DPT_LINESTRIP:
            PrimitiveCount = IndexCount - 1;
            break;

        case D3DPT_TRIANGLELIST:
            PrimitiveCount = IndexCount / 3;
            break;

        case D3DPT_TRIANGLESTRIP:
            PrimitiveCount = IndexCount - 2;
            break;

        case D3DPT_TRIANGLEFAN:
            PrimitiveCount = IndexCount - 2;
            break;

            default: return DDERR_INVALIDPARAMS;
    }

    /* WineD3D doesn't need the FVF here */
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_DrawIndexedPrimitiveStrided(This->wineD3DDevice,
                                                    PrimitiveType,
                                                    PrimitiveCount,
                                                    &WineD3DStrided,
                                                    VertexCount,
                                                    Indices,
                                                    WINED3DFMT_INDEX16);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
                                                        D3DPRIMITIVETYPE PrimitiveType,
                                                        DWORD VertexType,
                                                        D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
                                                        DWORD VertexCount,
                                                        WORD *Indices,
                                                        DWORD IndexCount,
                                                        DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3741
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785
    return IDirect3DDevice7_DrawIndexedPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                        PrimitiveType,
                                                        VertexType,
                                                        D3DDrawPrimStrideData,
                                                        VertexCount,
                                                        Indices,
                                                        IndexCount,
                                                        Flags);
}

/*****************************************************************************
 * IDirect3DDevice7::DrawPrimitiveVB
 *
 * Draws primitives from a vertex buffer to the screen.
 *
 * Version 3 and 7
 *
 * Params:
 *  PrimitiveType: Type of primitive to be rendered.
 *  D3DVertexBuf: Source Vertex Buffer
 *  StartVertex: Index of the first vertex from the buffer to be rendered
 *  NumVertices: Number of vertices to be rendered
 *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
 *
 * Return values
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if D3DVertexBuf is NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DrawPrimitiveVB(IDirect3DDevice7 *iface,
                                      D3DPRIMITIVETYPE PrimitiveType,
                                      IDirect3DVertexBuffer7 *D3DVertexBuf,
                                      DWORD StartVertex,
                                      DWORD NumVertices,
                                      DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
    UINT PrimitiveCount;
    HRESULT hr;
    DWORD stride;
    WINED3DVERTEXBUFFER_DESC Desc;

3786
    TRACE("(%p)->(%08x,%p,%08x,%08x,%08x)\n", This, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821

    /* Sanity checks */
    if(!vb)
    {
        ERR("(%p) No Vertex buffer specified\n", This);
        return DDERR_INVALIDPARAMS;
    }

    /* Get the primitive count */
    switch(PrimitiveType)
    {
        case D3DPT_POINTLIST: 
          PrimitiveCount = NumVertices;
          break;

        case D3DPT_LINELIST: 
          PrimitiveCount = NumVertices / 2;
          break;

        case D3DPT_LINESTRIP:
          PrimitiveCount = NumVertices - 1;
          break;

        case D3DPT_TRIANGLELIST:
          PrimitiveCount = NumVertices / 3;
          break;

        case D3DPT_TRIANGLESTRIP:
          PrimitiveCount = NumVertices - 2;
          break;

        case D3DPT_TRIANGLEFAN:
          PrimitiveCount = NumVertices - 2;
          break;

3822 3823
        default:
          return DDERR_INVALIDPARAMS;
3824 3825 3826
    }

    /* Get the FVF of the vertex buffer, and its stride */
3827
    EnterCriticalSection(&ddraw_cs);
3828 3829 3830 3831
    hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
                                      &Desc);
    if(hr != D3D_OK)
    {
3832
        ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3833
        LeaveCriticalSection(&ddraw_cs);
3834 3835 3836 3837
        return hr;
    }
    stride = get_flexible_vertex_size(Desc.FVF);

3838 3839
    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
                                             vb->wineD3DVertexDeclaration);
3840 3841
    if(FAILED(hr))
    {
3842
        ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3843
        LeaveCriticalSection(&ddraw_cs);
3844 3845 3846
        return hr;
    }

3847
    /* Set the vertex stream source */
3848 3849 3850 3851 3852 3853 3854
    hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
                                        0 /* StreamNumber */,
                                        vb->wineD3DVertexBuffer,
                                        0 /* StartVertex - we pass this to DrawPrimitive */,
                                        stride);
    if(hr != D3D_OK)
    {
3855
        ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3856
        LeaveCriticalSection(&ddraw_cs);
3857 3858 3859 3860
        return hr;
    }

    /* Now draw the primitives */
3861 3862 3863 3864 3865 3866
    hr = IWineD3DDevice_DrawPrimitive(This->wineD3DDevice,
                                      PrimitiveType,
                                      StartVertex,
                                      PrimitiveCount);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB(IDirect3DDevice3 *iface,
                                            D3DPRIMITIVETYPE PrimitiveType,
                                            IDirect3DVertexBuffer *D3DVertexBuf,
                                            DWORD StartVertex,
                                            DWORD NumVertices,
                                            DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3879
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This,  PrimitiveType, vb, StartVertex, NumVertices, Flags);
3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923
    return IDirect3DDevice7_DrawPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
                                            PrimitiveType,
                                            ICOM_INTERFACE(vb, IDirect3DVertexBuffer7),
                                            StartVertex,
                                            NumVertices,
                                            Flags);
}


/*****************************************************************************
 * IDirect3DDevice7::DrawIndexedPrimitiveVB
 *
 * Draws primitives from a vertex buffer to the screen
 *
 * Params:
 *  PrimitiveType: Type of primitive to be rendered.
 *  D3DVertexBuf: Source Vertex Buffer
 *  StartVertex: Index of the first vertex from the buffer to be rendered
 *  NumVertices: Number of vertices to be rendered
 *  Indices: Array of DWORDs used to index into the Vertices
 *  IndexCount: Number of indices in Indices
 *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
 *
 * Return values
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
                                             D3DPRIMITIVETYPE PrimitiveType,
                                             IDirect3DVertexBuffer7 *D3DVertexBuf,
                                             DWORD StartVertex,
                                             DWORD NumVertices,
                                             WORD *Indices,
                                             DWORD IndexCount,
                                             DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
    DWORD stride;
    UINT PrimitiveCount;
    WORD *LockedIndices;
    HRESULT hr;
    WINED3DVERTEXBUFFER_DESC Desc;

3924
    TRACE("(%p)->(%08x,%p,%d,%d,%p,%d,%08x)\n", This, PrimitiveType, vb, StartVertex, NumVertices, Indices, IndexCount, Flags);
3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963

    /* Steps:
     * 1) Calculate some things: Vertex count -> Primitive count, stride, ...
     * 2) Upload the Indices to the index buffer
     * 3) Set the index source
     * 4) Set the Vertex Buffer as the Stream source
     * 5) Call IWineD3DDevice::DrawIndexedPrimitive
     */

    /* Get the primitive count */
    switch(PrimitiveType)
    {
        case D3DPT_POINTLIST: 
          PrimitiveCount = IndexCount;
          break;

        case D3DPT_LINELIST: 
          PrimitiveCount = IndexCount / 2;
          break;

        case D3DPT_LINESTRIP:
          PrimitiveCount = IndexCount - 1;
          break;

        case D3DPT_TRIANGLELIST:
          PrimitiveCount = IndexCount / 3;
          break;

        case D3DPT_TRIANGLESTRIP:
          PrimitiveCount = IndexCount - 2;
          break;

        case D3DPT_TRIANGLEFAN:
          PrimitiveCount = IndexCount - 2;
          break;

        default: return DDERR_INVALIDPARAMS;
    }

3964
    EnterCriticalSection(&ddraw_cs);
3965 3966 3967 3968 3969
    /* Get the FVF of the vertex buffer, and its stride */
    hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
                                      &Desc);
    if(hr != D3D_OK)
    {
3970
        ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3971
        LeaveCriticalSection(&ddraw_cs);
3972 3973 3974
        return hr;
    }
    stride = get_flexible_vertex_size(Desc.FVF);
3975
    TRACE("Vertex buffer FVF = %08x, stride=%d\n", Desc.FVF, stride);
3976

3977 3978
    hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
                                             vb->wineD3DVertexDeclaration);
3979 3980
    if(FAILED(hr))
    {
3981
        ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3982
        LeaveCriticalSection(&ddraw_cs);
3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993
        return hr;
    }

    /* copy the index stream into the index buffer.
     * A new IWineD3DDevice method could be created
     * which takes an user pointer containing the indices
     * or a SetData-Method for the index buffer, which
     * overrides the index buffer data with our pointer.
     */
    hr = IWineD3DIndexBuffer_Lock(This->indexbuffer,
                                  0 /* OffSetToLock */,
3994
                                  IndexCount * sizeof(WORD),
3995 3996 3997 3998 3999
                                  (BYTE **) &LockedIndices,
                                  0 /* Flags */);
    assert(IndexCount < 0x100000);
    if(hr != D3D_OK)
    {
4000
        ERR("(%p) IWineD3DIndexBuffer::Lock failed with hr = %08x\n", This, hr);
4001
        LeaveCriticalSection(&ddraw_cs);
4002 4003 4004 4005 4006 4007
        return hr;
    }
    memcpy(LockedIndices, Indices, IndexCount * sizeof(WORD));
    hr = IWineD3DIndexBuffer_Unlock(This->indexbuffer);
    if(hr != D3D_OK)
    {
4008
        ERR("(%p) IWineD3DIndexBuffer::Unlock failed with hr = %08x\n", This, hr);
4009
        LeaveCriticalSection(&ddraw_cs);
4010 4011 4012 4013
        return hr;
    }

    /* Set the index stream */
4014 4015
    IWineD3DDevice_SetBaseVertexIndex(This->wineD3DDevice, StartVertex);
    hr = IWineD3DDevice_SetIndices(This->wineD3DDevice, This->indexbuffer);
4016

4017
    /* Set the vertex stream source */
4018 4019 4020 4021 4022 4023 4024
    hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
                                        0 /* StreamNumber */,
                                        vb->wineD3DVertexBuffer,
                                        0 /* offset, we pass this to DrawIndexedPrimitive */,
                                        stride);
    if(hr != D3D_OK)
    {
4025
        ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
4026
        LeaveCriticalSection(&ddraw_cs);
4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037
        return hr;
    }


    hr = IWineD3DDevice_DrawIndexedPrimitive(This->wineD3DDevice,
                                             PrimitiveType,
                                             0 /* minIndex */,
                                             NumVertices,
                                             0 /* StartIndex */,
                                             PrimitiveCount);

4038 4039
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
                                                   D3DPRIMITIVETYPE PrimitiveType,
                                                   IDirect3DVertexBuffer *D3DVertexBuf,
                                                   WORD *Indices,
                                                   DWORD IndexCount,
                                                   DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirect3DVertexBufferImpl *VB = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
4052
    TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VB, Indices, IndexCount, Flags);
4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068

    return IDirect3DDevice7_DrawIndexedPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                   PrimitiveType,
                                                   ICOM_INTERFACE(VB, IDirect3DVertexBuffer7),
                                                   0,
                                                   IndexCount,
                                                   Indices,
                                                   IndexCount,
                                                   Flags);
}

/*****************************************************************************
 * IDirect3DDevice7::ComputeSphereVisibility
 *
 * Calculates the visibility of spheres in the current viewport. The spheres
 * are passed in the Centers and Radii arrays, the results are passed back
4069
 * in the ReturnValues array. Return values are either completely visible,
4070 4071 4072 4073
 * partially visible or completely invisible.
 * The return value consist of a combination of D3DCLIP_* flags, or it's
 * 0 if the sphere is completely visible(according to the SDK, not checked)
 *
4074
 * Sounds like an overdose of math ;)
4075 4076 4077 4078 4079
 *
 * Version 3 and 7
 *
 * Params:
 *  Centers: Array containing the sphere centers
4080 4081
 *  Radii: Array containing the sphere radii
 *  NumSpheres: The number of centers and radii in the arrays
4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100
 *  Flags: Some flags
 *  ReturnValues: Array to write the results to
 *
 * Returns:
 *  D3D_OK because it's a stub
 *  (DDERR_INVALIDPARAMS if Centers, Radii or ReturnValues are NULL)
 *  (D3DERR_INVALIDMATRIX if the combined world, view and proj matrix
 *  is singular)
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
                                              D3DVECTOR *Centers,
                                              D3DVALUE *Radii,
                                              DWORD NumSpheres,
                                              DWORD Flags,
                                              DWORD *ReturnValues)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4101
    FIXME("(%p)->(%p,%p,%08x,%08x,%p): stub!\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119

    /* the DirectX 7 sdk says that the visibility is computed by
     * back-transforming the viewing frustum to model space
     * using the inverse of the combined world, view and projection
     * matrix. If the matrix can't be reversed, D3DERR_INVALIDMATRIX
     * is returned.
     *
     * Basic implementation idea:
     * 1) Check if the center is in the viewing frustum
     * 2) Cut the sphere with the planes of the viewing
     *    frustum
     *
     * ->Center inside the frustum, no intersections:
     *    Fully visible
     * ->Center outside the frustum, no intersections:
     *    Not visible
     * ->Some intersections: Partially visible
     *
4120
     * Implement this call in WineD3D. Either implement the
4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136
     * matrix and vector stuff in WineD3D, or use some external
     * math library.
     */

    return D3D_OK;
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
                                                    D3DVECTOR *Centers,
                                                    D3DVALUE *Radii,
                                                    DWORD NumSpheres,
                                                    DWORD Flags,
                                                    DWORD *ReturnValues)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4137
    TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172
    return IDirect3DDevice7_ComputeSphereVisibility(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                    Centers,
                                                    Radii,
                                                    NumSpheres,
                                                    Flags,
                                                    ReturnValues);
}

/*****************************************************************************
 * IDirect3DDevice7::GetTexture
 *
 * Returns the texture interface handle assigned to a texture stage.
 * The returned texture is AddRefed. This is taken from old ddraw,
 * not checked in Windows.
 *
 * Version 3 and 7
 *
 * Params:
 *  Stage: Texture stage to read the texture from
 *  Texture: Address to store the interface pointer at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Texture is NULL
 *  For details, see IWineD3DDevice::GetTexture
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
                                 DWORD Stage,
                                 IDirectDrawSurface7 **Texture)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IWineD3DBaseTexture *Surf;
    HRESULT hr;
4173
    TRACE("(%p)->(%d,%p): Relay\n", This, Stage, Texture);
4174 4175 4176 4177 4178 4179 4180

    if(!Texture)
    {
        TRACE("Texture == NULL, failing with DDERR_INVALIDPARAMS\n");
        return DDERR_INVALIDPARAMS;
    }

4181
    EnterCriticalSection(&ddraw_cs);
4182 4183 4184 4185
    hr = IWineD3DDevice_GetTexture(This->wineD3DDevice, Stage, (IWineD3DBaseTexture **) &Surf);
    if( (hr != D3D_OK) || (!Surf) ) 
    {
        *Texture = NULL;
4186
        LeaveCriticalSection(&ddraw_cs);
4187 4188 4189 4190 4191 4192
        return hr;
    }

    /* GetParent AddRef()s, which is perfectly OK.
     * We have passed the IDirectDrawSurface7 interface to WineD3D, so that's OK too.
     */
4193 4194 4195 4196
    hr = IWineD3DBaseTexture_GetParent(Surf,
                                       (IUnknown **) Texture);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetTexture(IDirect3DDevice3 *iface,
                                       DWORD Stage,
                                       IDirect3DTexture2 **Texture2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    HRESULT ret;
    IDirectDrawSurface7 *ret_val;

4208
    TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, Texture2);
4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222
    ret = IDirect3DDevice7_GetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
                                      Stage,
                                      &ret_val);

    *Texture2 = COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirect3DTexture2, ret_val);

    TRACE_(ddraw_thunk)(" returning interface %p.\n", *Texture2);

    return ret;
}

/*****************************************************************************
 * IDirect3DDevice7::SetTexture
 *
4223
 * Assigns a texture to a texture stage. Is the texture AddRef-ed?
4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242
 *
 * Version 3 and 7
 *
 * Params:
 *  Stage: The stage to assign the texture to
 *  Texture: Interface pointer to the texture surface
 *
 * Returns
 * D3D_OK on success
 * For details, see IWineD3DDevice::SetTexture
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetTexture(IDirect3DDevice7 *iface,
                                 DWORD Stage,
                                 IDirectDrawSurface7 *Texture)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
4243
    HRESULT hr;
4244
    TRACE("(%p)->(%08x,%p): Relay!\n", This, Stage, surf);
4245 4246

    /* Texture may be NULL here */
4247 4248 4249 4250 4251 4252
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_SetTexture(This->wineD3DDevice,
                                   Stage,
                                   surf ? surf->wineD3DTexture : NULL);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4253 4254 4255 4256 4257 4258 4259 4260 4261
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_SetTexture(IDirect3DDevice3 *iface,
                                       DWORD Stage,
                                       IDirect3DTexture2 *Texture2)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    IDirectDrawSurfaceImpl *tex = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Texture2);
4262
    TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, tex);
4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292
    return IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
                                       Stage,
                                       ICOM_INTERFACE(tex, IDirectDrawSurface7));
}

/*****************************************************************************
 * IDirect3DDevice7::GetTextureStageState
 *
 * Retrieves a state from a texture stage.
 *
 * Version 3 and 7
 *
 * Params:
 *  Stage: The stage to retrieve the state from
 *  TexStageStateType: The state type to retrieve
 *  State: Address to store the state's value at
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if State is NULL
 *  For details, see IWineD3DDevice::GetTextureStageState
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
                                           DWORD Stage,
                                           D3DTEXTURESTAGESTATETYPE TexStageStateType,
                                           DWORD *State)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4293
    HRESULT hr;
4294
    TRACE("(%p)->(%08x,%08x,%p): Relay!\n", This, Stage, TexStageStateType, State);
4295 4296 4297 4298

    if(!State)
        return DDERR_INVALIDPARAMS;

4299
    EnterCriticalSection(&ddraw_cs);
4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319
    switch(TexStageStateType)
    {
        /* Mipfilter is a sampler state with different values */
        case D3DTSS_MIPFILTER:
        {
            WINED3DTEXTUREFILTERTYPE value;

            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_MIPFILTER,
                                                &value);
            switch(value)
            {
                case WINED3DTEXF_NONE: *State = D3DTFP_NONE; break;
                case WINED3DTEXF_POINT: *State = D3DTFP_POINT; break;
                case WINED3DTEXF_LINEAR: *State = D3DTFP_LINEAR; break;
                default:
                    ERR("Unexpected mipfilter value %d\n", value);
                    *State = D3DTFP_NONE;
            }
4320
            break;
4321 4322 4323 4324
        }

        /* Minfilter is a sampler state too, equal values */
        case D3DTSS_MINFILTER:
4325 4326 4327 4328 4329 4330
            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_MINFILTER,
                                                State);
            break;

4331
        /* Magfilter has slightly different values */
4332
        case D3DTSS_MAGFILTER:
4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349
        {
            WINED3DTEXTUREFILTERTYPE wined3dfilter;
            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_MAGFILTER,
                                                &wined3dfilter);
            switch(wined3dfilter)
            {
                case WINED3DTEXF_POINT:             *State = D3DTFG_POINT;          break;
                case WINED3DTEXF_LINEAR:            *State = D3DTFG_LINEAR;         break;
                case WINED3DTEXF_ANISOTROPIC:       *State = D3DTFG_ANISOTROPIC;    break;
                case WINED3DTEXF_FLATCUBIC:         *State = D3DTFG_FLATCUBIC;      break;
                case WINED3DTEXF_GAUSSIANCUBIC:     *State = D3DTFG_GAUSSIANCUBIC;  break;
                default:
                    ERR("Unexpected wined3d mag filter value %d\n", wined3dfilter);
                    *State = D3DTFG_POINT;
            }
4350
            break;
4351
        }
4352 4353 4354

        case D3DTSS_ADDRESS:
        case D3DTSS_ADDRESSU:
4355 4356 4357 4358 4359
            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_ADDRESSU,
                                                State);
            break;
4360
        case D3DTSS_ADDRESSV:
4361 4362 4363 4364 4365
            hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_ADDRESSV,
                                                State);
            break;
4366
        default:
4367 4368 4369 4370 4371
            hr = IWineD3DDevice_GetTextureStageState(This->wineD3DDevice,
                                                     Stage,
                                                     TexStageStateType,
                                                     State);
            break;
4372
    }
4373 4374
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4375 4376 4377 4378 4379 4380 4381 4382 4383
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_GetTextureStageState(IDirect3DDevice3 *iface,
                                                 DWORD Stage,
                                                 D3DTEXTURESTAGESTATETYPE TexStageStateType,
                                                 DWORD *State)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4384
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415
    return IDirect3DDevice7_GetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                 Stage,
                                                 TexStageStateType,
                                                 State);
}

/*****************************************************************************
 * IDirect3DDevice7::SetTextureStageState
 *
 * Sets a texture stage state. Some stage types need to be handled specially,
 * because they do not exist in WineD3D and were moved to another place
 *
 * Version 3 and 7
 *
 * Params:
 *  Stage: The stage to modify
 *  TexStageStateType: The state to change
 *  State: The new value for the state
 *
 * Returns:
 *  D3D_OK on success
 *  For details, see IWineD3DDevice::SetTextureStageState
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
                                           DWORD Stage,
                                           D3DTEXTURESTAGESTATETYPE TexStageStateType,
                                           DWORD State)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4416
    HRESULT hr;
4417
    TRACE("(%p)->(%08x,%08x,%08x): Relay!\n", This, Stage, TexStageStateType, State);
4418 4419

    EnterCriticalSection(&ddraw_cs);
4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432
    switch(TexStageStateType)
    {
        /* Mipfilter is a sampler state with different values */
        case D3DTSS_MIPFILTER:
        {
            WINED3DTEXTUREFILTERTYPE value;
            switch(State)
            {
                case D3DTFP_NONE: value = WINED3DTEXF_NONE; break;
                case D3DTFP_POINT: value = WINED3DTEXF_POINT; break;
                case 0: /* Unchecked */
                case D3DTFP_LINEAR: value = WINED3DTEXF_LINEAR; break;
                default:
4433
                    ERR("Unexpected mipfilter value %d\n", State);
4434 4435
                    value = WINED3DTEXF_NONE;
            }
4436 4437 4438 4439 4440
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_MIPFILTER,
                                                value);
            break;
4441 4442 4443 4444
        }

        /* Minfilter is a sampler state too, equal values */
        case D3DTSS_MINFILTER:
4445 4446 4447 4448 4449 4450
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_MINFILTER,
                                                State);
            break;

4451
        /* Magfilter has slightly different values */
4452
        case D3DTSS_MAGFILTER:
4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465
        {
            WINED3DTEXTUREFILTERTYPE wined3dfilter;
            switch((D3DTEXTUREMAGFILTER) State)
            {
                case D3DTFG_POINT:          wined3dfilter = WINED3DTEXF_POINT;          break;
                case D3DTFG_LINEAR:         wined3dfilter = WINED3DTEXF_LINEAR;         break;
                case D3DTFG_FLATCUBIC:      wined3dfilter = WINED3DTEXF_FLATCUBIC;      break;
                case D3DTFG_GAUSSIANCUBIC:  wined3dfilter = WINED3DTEXF_GAUSSIANCUBIC;  break;
                case D3DTFG_ANISOTROPIC:    wined3dfilter = WINED3DTEXF_ANISOTROPIC;    break;
                default:
                    ERR("Unexpected d3d7 mag filter type %d\n", State);
                    wined3dfilter = WINED3DTEXF_POINT;
            }
4466 4467 4468 4469 4470
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_MAGFILTER,
                                                wined3dfilter);
            break;
4471
        }
4472

4473 4474 4475 4476 4477 4478 4479
        case D3DTSS_ADDRESS:
                   IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                  Stage,
                                                  WINED3DSAMP_ADDRESSV,
                                                  State);
            /* Drop through */
        case D3DTSS_ADDRESSU:
4480 4481 4482 4483 4484 4485
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_ADDRESSU,
                                                State);
            break;

4486
        case D3DTSS_ADDRESSV:
4487 4488 4489 4490 4491
            hr = IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
                                                Stage,
                                                WINED3DSAMP_ADDRESSV,
                                                State);
            break;
4492

4493 4494 4495 4496 4497 4498
        default:
            hr = IWineD3DDevice_SetTextureStageState(This->wineD3DDevice,
                                                     Stage,
                                                     TexStageStateType,
                                                     State);
            break;
4499
    }
4500 4501
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4502 4503 4504 4505 4506 4507 4508 4509 4510
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_SetTextureStageState(IDirect3DDevice3 *iface,
                                                 DWORD Stage,
                                                 D3DTEXTURESTAGESTATETYPE TexStageStateType,
                                                 DWORD State)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4511
    TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527
    return IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
                                                 Stage,
                                                 TexStageStateType,
                                                 State);
}

/*****************************************************************************
 * IDirect3DDevice7::ValidateDevice
 *
 * SDK: "Reports the device's ability to render the currently set
 * texture-blending operations in a single pass". Whatever that means
 * exactly...
 *
 * Version 3 and 7
 *
 * Params:
4528
 *  NumPasses: Address to write the number of necessary passes for the
4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540
 *             desired effect to.
 *
 * Returns:
 *  D3D_OK on success
 *  See IWineD3DDevice::ValidateDevice for more details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_ValidateDevice(IDirect3DDevice7 *iface,
                                     DWORD *NumPasses)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4541
    HRESULT hr;
4542 4543
    TRACE("(%p)->(%p): Relay\n", This, NumPasses);

4544 4545 4546 4547
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_ValidateDevice(This->wineD3DDevice, NumPasses);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590
}

static HRESULT WINAPI
Thunk_IDirect3DDeviceImpl_3_ValidateDevice(IDirect3DDevice3 *iface,
                                           DWORD *Passes)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
    TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Passes);
    return IDirect3DDevice7_ValidateDevice(ICOM_INTERFACE(This, IDirect3DDevice7),
                                           Passes);
}

/*****************************************************************************
 * IDirect3DDevice7::Clear
 *
 * Fills the render target, the z buffer and the stencil buffer with a
 * clear color / value
 *
 * Version 7 only
 *
 * Params:
 *  Count: Number of rectangles in Rects must be 0 if Rects is NULL
 *  Rects: Rectangles to clear. If NULL, the whole surface is cleared
 *  Flags: Some flags, as usual
 *  Color: Clear color for the render target
 *  Z: Clear value for the Z buffer
 *  Stencil: Clear value to store in each stencil buffer entry
 *
 * Returns:
 *  D3D_OK on success
 *  For details, see IWineD3DDevice::Clear
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_Clear(IDirect3DDevice7 *iface,
                            DWORD Count,
                            D3DRECT *Rects,
                            DWORD Flags,
                            D3DCOLOR Color,
                            D3DVALUE Z,
                            DWORD Stencil)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4591
    HRESULT hr;
4592
    TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x): Relay\n", This, Count, Rects, Flags, (DWORD) Color, Z, Stencil);
4593

4594
    /* Note; D3DRECT is compatible with WINED3DRECT */
4595 4596 4597 4598
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_Clear(This->wineD3DDevice, Count, (WINED3DRECT*) Rects, Flags, Color, Z, Stencil);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622
}

/*****************************************************************************
 * IDirect3DDevice7::SetViewport
 *
 * Sets the current viewport.
 *
 * Version 7 only, but IDirect3DViewport uses this call for older
 * versions
 *
 * Params:
 *  Data: The new viewport to set
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Data is NULL
 *  For more details, see IWineDDDevice::SetViewport
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetViewport(IDirect3DDevice7 *iface,
                                  D3DVIEWPORT7 *Data)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4623
    HRESULT hr;
4624 4625 4626 4627 4628
    TRACE("(%p)->(%p) Relay!\n", This, Data);

    if(!Data)
        return DDERR_INVALIDPARAMS;

4629
    /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4630 4631 4632 4633 4634
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_SetViewport(This->wineD3DDevice,
                                    (WINED3DVIEWPORT*) Data);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663
}

/*****************************************************************************
 * IDirect3DDevice::GetViewport
 *
 * Returns the current viewport
 *
 * Version 7
 *
 * Params:
 *  Data: D3D7Viewport structure to write the viewport information to
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Data is NULL
 *  For more details, see IWineD3DDevice::GetViewport
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetViewport(IDirect3DDevice7 *iface,
                                  D3DVIEWPORT7 *Data)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
    TRACE("(%p)->(%p) Relay!\n", This, Data);

    if(!Data)
        return DDERR_INVALIDPARAMS;

4664
    /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4665
    EnterCriticalSection(&ddraw_cs);
4666
    hr = IWineD3DDevice_GetViewport(This->wineD3DDevice,
4667 4668
                                    (WINED3DVIEWPORT*) Data);

4669
    LeaveCriticalSection(&ddraw_cs);
4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::SetMaterial
 *
 * Sets the Material
 *
 * Version 7
 *
 * Params:
 *  Mat: The material to set
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Mat is NULL.
 *  For more details, see IWineD3DDevice::SetMaterial
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetMaterial(IDirect3DDevice7 *iface,
                                  D3DMATERIAL7 *Mat)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
    TRACE("(%p)->(%p): Relay!\n", This, Mat);

4697
    /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */
4698
    EnterCriticalSection(&ddraw_cs);
4699
    hr = IWineD3DDevice_SetMaterial(This->wineD3DDevice,
4700
                                    (WINED3DMATERIAL*) Mat);
4701
    LeaveCriticalSection(&ddraw_cs);
4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::GetMaterial
 *
 * Returns the current material
 *
 * Version 7
 *
 * Params:
4713
 *  Mat: D3DMATERIAL7 structure to write the material parameters to
4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Mat is NULL
 *  For more details, see IWineD3DDevice::GetMaterial
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetMaterial(IDirect3DDevice7 *iface,
                                  D3DMATERIAL7 *Mat)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
    TRACE("(%p)->(%p): Relay!\n", This, Mat);

4729
    EnterCriticalSection(&ddraw_cs);
4730
    /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */ 
4731
    hr = IWineD3DDevice_GetMaterial(This->wineD3DDevice,
4732
                                    (WINED3DMATERIAL*) Mat);
4733
    LeaveCriticalSection(&ddraw_cs);
4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::SetLight
 *
 * Assigns a light to a light index, but doesn't activate it yet.
 *
 * Version 7, IDirect3DLight uses this method for older versions
 *
 * Params:
 *  LightIndex: The index of the new light
 *  Light: A D3DLIGHT7 structure describing the light
 *
 * Returns:
 *  D3D_OK on success
 *  For more details, see IWineD3DDevice::SetLight
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetLight(IDirect3DDevice7 *iface,
                               DWORD LightIndex,
                               D3DLIGHT7 *Light)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
4760
    TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4761

4762
    EnterCriticalSection(&ddraw_cs);
4763
    /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4764 4765
    hr = IWineD3DDevice_SetLight(This->wineD3DDevice,
                                 LightIndex,
4766
                                 (WINED3DLIGHT*) Light);
4767
    LeaveCriticalSection(&ddraw_cs);
4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::GetLight
 *
 * Returns the light assigned to a light index
 *
 * Params:
 *  Light: Structure to write the light information to
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Light is NULL
 *  For details, see IWineD3DDevice::GetLight
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetLight(IDirect3DDevice7 *iface,
                               DWORD LightIndex,
                               D3DLIGHT7 *Light)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT rc;
4792
    TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4793

4794
    EnterCriticalSection(&ddraw_cs);
4795
    /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4796 4797
    rc =  IWineD3DDevice_GetLight(This->wineD3DDevice,
                                  LightIndex,
4798
                                  (WINED3DLIGHT*) Light);
4799 4800

    /* Translate the result. WineD3D returns other values than D3D7 */
4801
    LeaveCriticalSection(&ddraw_cs);
4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823
    return hr_ddraw_from_wined3d(rc);
}

/*****************************************************************************
 * IDirect3DDevice7::BeginStateBlock
 *
 * Begins recording to a stateblock
 *
 * Version 7
 *
 * Returns:
 *  D3D_OK on success
 *  For details see IWineD3DDevice::BeginStateBlock
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_BeginStateBlock(IDirect3DDevice7 *iface)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
    TRACE("(%p)->(): Relay!\n", This);

4824
    EnterCriticalSection(&ddraw_cs);
4825
    hr = IWineD3DDevice_BeginStateBlock(This->wineD3DDevice);
4826
    LeaveCriticalSection(&ddraw_cs);
4827 4828 4829 4830 4831 4832 4833
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::EndStateBlock
 *
 * Stops recording to a state block and returns the created stateblock
4834
 * handle.
4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855
 *
 * Version 7
 *
 * Params:
 *  BlockHandle: Address to store the stateblock's handle to
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if BlockHandle is NULL
 *  See IWineD3DDevice::EndStateBlock for more details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
                                    DWORD *BlockHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
    TRACE("(%p)->(%p): Relay!\n", This, BlockHandle);

    if(!BlockHandle)
4856 4857
    {
        WARN("BlockHandle == NULL, returning DDERR_INVALIDPARAMS\n");
4858
        return DDERR_INVALIDPARAMS;
4859
    }
4860

4861
    EnterCriticalSection(&ddraw_cs);
4862 4863 4864 4865
    *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
    if(!*BlockHandle)
    {
        ERR("Cannot get a handle number for the stateblock\n");
4866
        LeaveCriticalSection(&ddraw_cs);
4867 4868 4869
        return DDERR_OUTOFMEMORY;
    }
    This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
4870
    hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice,
4871
                                      (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr);
4872
    LeaveCriticalSection(&ddraw_cs);
4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::PreLoad
 *
 * Allows the app to signal that a texture will be used soon, to allow
 * the Direct3DDevice to load it to the video card in the meantime.
 *
 * Version 7
 *
 * Params:
 *  Texture: The texture to preload
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Texture is NULL
 *  See IWineD3DSurface::PreLoad for details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_PreLoad(IDirect3DDevice7 *iface,
                              IDirectDrawSurface7 *Texture)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);

    TRACE("(%p)->(%p): Relay!\n", This, surf);

    if(!Texture)
        return DDERR_INVALIDPARAMS;

4905
    EnterCriticalSection(&ddraw_cs);
4906
    IWineD3DSurface_PreLoad(surf->WineD3DSurface);
4907
    LeaveCriticalSection(&ddraw_cs);
4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::ApplyStateBlock
 *
 * Activates the state stored in a state block handle.
 *
 * Params:
 *  BlockHandle: The stateblock handle to activate
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_ApplyStateBlock(IDirect3DDevice7 *iface,
                                      DWORD BlockHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
4930
    TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4931

4932
    EnterCriticalSection(&ddraw_cs);
4933 4934 4935
    if(!BlockHandle || BlockHandle > This->numHandles)
    {
        WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4936
        LeaveCriticalSection(&ddraw_cs);
4937 4938 4939 4940 4941
        return D3DERR_INVALIDSTATEBLOCK;
    }
    if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
    {
        WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4942
        LeaveCriticalSection(&ddraw_cs);
4943
        return D3DERR_INVALIDSTATEBLOCK;
4944
    }
4945

4946
    hr = IWineD3DStateBlock_Apply((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4947
    LeaveCriticalSection(&ddraw_cs);
4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::CaptureStateBlock
 *
 * Updates a stateblock's values to the values currently set for the device
 *
 * Version 7
 *
 * Params:
 *  BlockHandle: Stateblock to update
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
 *  See IWineD3DDevice::CaptureStateBlock for more details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_CaptureStateBlock(IDirect3DDevice7 *iface,
                                        DWORD BlockHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
4973
    TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4974

4975
    EnterCriticalSection(&ddraw_cs);
4976 4977 4978
    if(BlockHandle == 0 || BlockHandle > This->numHandles)
    {
        WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4979
        LeaveCriticalSection(&ddraw_cs);
4980
        return D3DERR_INVALIDSTATEBLOCK;
4981 4982 4983 4984
    }
    if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
    {
        WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4985
        LeaveCriticalSection(&ddraw_cs);
4986 4987
        return D3DERR_INVALIDSTATEBLOCK;
    }
4988

4989
    hr = IWineD3DStateBlock_Capture((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4990
    LeaveCriticalSection(&ddraw_cs);
4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::DeleteStateBlock
 *
 * Deletes a stateblock handle. This means releasing the WineD3DStateBlock
 *
 * Version 7
 *
 * Params:
 *  BlockHandle: Stateblock handle to delete
 *
 * Returns:
 *  D3D_OK on success
 *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is 0
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_DeleteStateBlock(IDirect3DDevice7 *iface,
                                       DWORD BlockHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
5014
    ULONG ref;
5015
    TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
5016

5017
    EnterCriticalSection(&ddraw_cs);
5018 5019 5020
    if(BlockHandle == 0 || BlockHandle > This->numHandles)
    {
        WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
5021
        LeaveCriticalSection(&ddraw_cs);
5022
        return D3DERR_INVALIDSTATEBLOCK;
5023 5024 5025 5026
    }
    if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
    {
        WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
5027
        LeaveCriticalSection(&ddraw_cs);
5028 5029
        return D3DERR_INVALIDSTATEBLOCK;
    }
5030

5031 5032 5033 5034 5035 5036 5037
    ref = IWineD3DStateBlock_Release((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
    if(ref)
    {
        ERR("Something is still holding the stateblock %p(Handle %d). Ref = %d\n", This->Handles[BlockHandle - 1].ptr, BlockHandle, ref);
    }
    This->Handles[BlockHandle - 1].ptr = NULL;
    This->Handles[BlockHandle - 1].type = DDrawHandle_Unknown;
5038

5039
    LeaveCriticalSection(&ddraw_cs);
5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::CreateStateBlock
 *
 * Creates a new state block handle.
 *
 * Version 7
 *
 * Params:
 *  Type: The state block type
 *  BlockHandle: Address to write the created handle to
 *
 * Returns:
 *   D3D_OK on success
 *   DDERR_INVALIDPARAMS if BlockHandle is NULL
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
                                       D3DSTATEBLOCKTYPE Type,
                                       DWORD *BlockHandle)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
    TRACE("(%p)->(%08x,%p)!\n", This, Type, BlockHandle);

    if(!BlockHandle)
5069 5070
    {
        WARN("BlockHandle == NULL, returning DDERR_INVALIDPARAMS\n");
5071
        return DDERR_INVALIDPARAMS;
5072
    }
5073 5074 5075 5076 5077
    if(Type != D3DSBT_ALL         && Type != D3DSBT_PIXELSTATE &&
       Type != D3DSBT_VERTEXSTATE                              ) {
        WARN("Unexpected stateblock type, returning DDERR_INVALIDPARAMS\n");
        return DDERR_INVALIDPARAMS;
    }
5078

5079
    EnterCriticalSection(&ddraw_cs);
5080 5081 5082 5083
    *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
    if(!*BlockHandle)
    {
        ERR("Cannot get a handle number for the stateblock\n");
5084
        LeaveCriticalSection(&ddraw_cs);
5085 5086 5087
        return DDERR_OUTOFMEMORY;
    }
    This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
5088 5089 5090 5091

    /* The D3DSTATEBLOCKTYPE enum is fine here */
    hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice,
                                         Type,
5092
                                         (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr,
5093
                                         NULL /* Parent, hope that works */);
5094
    LeaveCriticalSection(&ddraw_cs);
5095 5096 5097 5098 5099 5100
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::Load
 *
5101
 * Loads a rectangular area from the source into the destination texture.
5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130
 * It can also copy the source to the faces of a cubic environment map
 *
 * Version 7
 *
 * Params:
 *  DestTex: Destination texture
 *  DestPoint: Point in the destination where the source image should be
 *             written to
 *  SrcTex: Source texture
 *  SrcRect: Source rectangle
 *  Flags: Some flags
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if DestTex or SrcTex are NULL
 *  See IDirect3DTexture2::Load for details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_Load(IDirect3DDevice7 *iface,
                           IDirectDrawSurface7 *DestTex,
                           POINT *DestPoint,
                           IDirectDrawSurface7 *SrcTex,
                           RECT *SrcRect,
                           DWORD Flags)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    IDirectDrawSurfaceImpl *dest = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestTex);
    IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcTex);
5131
    FIXME("(%p)->(%p,%p,%p,%p,%08x): Partially Implemented!\n", This, dest, DestPoint, src, SrcRect, Flags);
5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163

    if( (!src) || (!dest) )
        return DDERR_INVALIDPARAMS;

    IDirect3DTexture2_Load(ICOM_INTERFACE(dest, IDirect3DTexture2),
                           ICOM_INTERFACE(src, IDirect3DTexture2));
    return D3D_OK;
}

/*****************************************************************************
 * IDirect3DDevice7::LightEnable
 *
 * Enables or disables a light
 *
 * Version 7, IDirect3DLight uses this method too.
 *
 * Params:
 *  LightIndex: The index of the light to enable / disable
 *  Enable: Enable or disable the light
 *
 * Returns:
 *  D3D_OK on success
 *  For more details, see IWineD3DDevice::SetLightEnable
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_LightEnable(IDirect3DDevice7 *iface,
                                  DWORD LightIndex,
                                  BOOL Enable)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
5164
    TRACE("(%p)->(%08x,%d): Relay!\n", This, LightIndex, Enable);
5165

5166
    EnterCriticalSection(&ddraw_cs);
5167
    hr = IWineD3DDevice_SetLightEnable(This->wineD3DDevice, LightIndex, Enable);
5168
    LeaveCriticalSection(&ddraw_cs);
5169 5170 5171 5172 5173 5174
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::GetLightEnable
 *
5175
 * Retrieves if the light with the given index is enabled or not
5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195
 *
 * Version 7
 *
 * Params:
 *  LightIndex: Index of desired light
 *  Enable: Pointer to a BOOL which contains the result
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if Enable is NULL
 *  See IWineD3DDevice::GetLightEnable for more details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetLightEnable(IDirect3DDevice7 *iface,
                                     DWORD LightIndex,
                                     BOOL* Enable)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
    HRESULT hr;
5196
    TRACE("(%p)->(%08x,%p): Relay\n", This, LightIndex, Enable);
5197 5198 5199 5200

    if(!Enable)
        return DDERR_INVALIDPARAMS;

5201
    EnterCriticalSection(&ddraw_cs);
5202
    hr = IWineD3DDevice_GetLightEnable(This->wineD3DDevice, LightIndex, Enable);
5203
    LeaveCriticalSection(&ddraw_cs);
5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229
    return hr_ddraw_from_wined3d(hr);
}

/*****************************************************************************
 * IDirect3DDevice7::SetClipPlane
 *
 * Sets custom clipping plane
 *
 * Version 7
 *
 * Params:
 *  Index: The index of the clipping plane
 *  PlaneEquation: An equation defining the clipping plane
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
 *  See IWineD3DDevice::SetClipPlane for more details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_SetClipPlane(IDirect3DDevice7 *iface,
                                   DWORD Index,
                                   D3DVALUE* PlaneEquation)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
5230
    HRESULT hr;
5231
    TRACE("(%p)->(%08x,%p): Relay!\n", This, Index, PlaneEquation);
5232 5233 5234 5235

    if(!PlaneEquation)
        return DDERR_INVALIDPARAMS;

5236 5237 5238 5239
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_SetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262
}

/*****************************************************************************
 * IDirect3DDevice7::GetClipPlane
 *
 * Returns the clipping plane with a specific index
 *
 * Params:
 *  Index: The index of the desired plane
 *  PlaneEquation: Address to store the plane equation to
 *
 * Returns:
 *  D3D_OK on success
 *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
 *  See IWineD3DDevice::GetClipPlane for more details
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetClipPlane(IDirect3DDevice7 *iface,
                                   DWORD Index,
                                   D3DVALUE* PlaneEquation)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
5263
    HRESULT hr;
5264
    TRACE("(%p)->(%d,%p): Relay!\n", This, Index, PlaneEquation);
5265 5266 5267 5268

    if(!PlaneEquation)
        return DDERR_INVALIDPARAMS;

5269 5270 5271 5272
    EnterCriticalSection(&ddraw_cs);
    hr = IWineD3DDevice_GetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
    LeaveCriticalSection(&ddraw_cs);
    return hr;
5273 5274 5275 5276 5277 5278
}

/*****************************************************************************
 * IDirect3DDevice7::GetInfo
 *
 * Retrieves some information about the device. The DirectX sdk says that
5279
 * this version returns S_FALSE for all retail builds of DirectX, that's what
5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297
 * this implementation does.
 *
 * Params:
 *  DevInfoID: Information type requested
 *  DevInfoStruct: Pointer to a structure to store the info to
 *  Size: Size of the structure
 *
 * Returns:
 *  S_FALSE, because it's a non-debug driver
 *
 *****************************************************************************/
static HRESULT WINAPI
IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
                              DWORD DevInfoID,
                              void *DevInfoStruct,
                              DWORD Size)
{
    ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
5298
    TRACE("(%p)->(%08x,%p,%08x)\n", This, DevInfoID, DevInfoStruct, Size);
5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483

    if (TRACE_ON(d3d7))
    {
        TRACE(" info requested : ");
        switch (DevInfoID)
        {
            case D3DDEVINFOID_TEXTUREMANAGER: TRACE("D3DDEVINFOID_TEXTUREMANAGER\n"); break;
            case D3DDEVINFOID_D3DTEXTUREMANAGER: TRACE("D3DDEVINFOID_D3DTEXTUREMANAGER\n"); break;
            case D3DDEVINFOID_TEXTURING: TRACE("D3DDEVINFOID_TEXTURING\n"); break;
            default: ERR(" invalid flag !!!\n"); return DDERR_INVALIDPARAMS;
        }
    }

    return S_FALSE; /* According to MSDN, this is valid for a non-debug driver */
}

const IDirect3DDevice7Vtbl IDirect3DDevice7_Vtbl =
{
    /*** IUnknown Methods ***/
    IDirect3DDeviceImpl_7_QueryInterface,
    IDirect3DDeviceImpl_7_AddRef,
    IDirect3DDeviceImpl_7_Release,
    /*** IDirect3DDevice7 ***/
    IDirect3DDeviceImpl_7_GetCaps,
    IDirect3DDeviceImpl_7_EnumTextureFormats,
    IDirect3DDeviceImpl_7_BeginScene,
    IDirect3DDeviceImpl_7_EndScene,
    IDirect3DDeviceImpl_7_GetDirect3D,
    IDirect3DDeviceImpl_7_SetRenderTarget,
    IDirect3DDeviceImpl_7_GetRenderTarget,
    IDirect3DDeviceImpl_7_Clear,
    IDirect3DDeviceImpl_7_SetTransform,
    IDirect3DDeviceImpl_7_GetTransform,
    IDirect3DDeviceImpl_7_SetViewport,
    IDirect3DDeviceImpl_7_MultiplyTransform,
    IDirect3DDeviceImpl_7_GetViewport,
    IDirect3DDeviceImpl_7_SetMaterial,
    IDirect3DDeviceImpl_7_GetMaterial,
    IDirect3DDeviceImpl_7_SetLight,
    IDirect3DDeviceImpl_7_GetLight,
    IDirect3DDeviceImpl_7_SetRenderState,
    IDirect3DDeviceImpl_7_GetRenderState,
    IDirect3DDeviceImpl_7_BeginStateBlock,
    IDirect3DDeviceImpl_7_EndStateBlock,
    IDirect3DDeviceImpl_7_PreLoad,
    IDirect3DDeviceImpl_7_DrawPrimitive,
    IDirect3DDeviceImpl_7_DrawIndexedPrimitive,
    IDirect3DDeviceImpl_7_SetClipStatus,
    IDirect3DDeviceImpl_7_GetClipStatus,
    IDirect3DDeviceImpl_7_DrawPrimitiveStrided,
    IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided,
    IDirect3DDeviceImpl_7_DrawPrimitiveVB,
    IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB,
    IDirect3DDeviceImpl_7_ComputeSphereVisibility,
    IDirect3DDeviceImpl_7_GetTexture,
    IDirect3DDeviceImpl_7_SetTexture,
    IDirect3DDeviceImpl_7_GetTextureStageState,
    IDirect3DDeviceImpl_7_SetTextureStageState,
    IDirect3DDeviceImpl_7_ValidateDevice,
    IDirect3DDeviceImpl_7_ApplyStateBlock,
    IDirect3DDeviceImpl_7_CaptureStateBlock,
    IDirect3DDeviceImpl_7_DeleteStateBlock,
    IDirect3DDeviceImpl_7_CreateStateBlock,
    IDirect3DDeviceImpl_7_Load,
    IDirect3DDeviceImpl_7_LightEnable,
    IDirect3DDeviceImpl_7_GetLightEnable,
    IDirect3DDeviceImpl_7_SetClipPlane,
    IDirect3DDeviceImpl_7_GetClipPlane,
    IDirect3DDeviceImpl_7_GetInfo
};

const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
{
    /*** IUnknown Methods ***/
    Thunk_IDirect3DDeviceImpl_3_QueryInterface,
    Thunk_IDirect3DDeviceImpl_3_AddRef,
    Thunk_IDirect3DDeviceImpl_3_Release,
    /*** IDirect3DDevice3 ***/
    IDirect3DDeviceImpl_3_GetCaps,
    IDirect3DDeviceImpl_3_GetStats,
    IDirect3DDeviceImpl_3_AddViewport,
    IDirect3DDeviceImpl_3_DeleteViewport,
    IDirect3DDeviceImpl_3_NextViewport,
    Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats,
    Thunk_IDirect3DDeviceImpl_3_BeginScene,
    Thunk_IDirect3DDeviceImpl_3_EndScene,
    Thunk_IDirect3DDeviceImpl_3_GetDirect3D,
    IDirect3DDeviceImpl_3_SetCurrentViewport,
    IDirect3DDeviceImpl_3_GetCurrentViewport,
    Thunk_IDirect3DDeviceImpl_3_SetRenderTarget,
    Thunk_IDirect3DDeviceImpl_3_GetRenderTarget,
    IDirect3DDeviceImpl_3_Begin,
    IDirect3DDeviceImpl_3_BeginIndexed,
    IDirect3DDeviceImpl_3_Vertex,
    IDirect3DDeviceImpl_3_Index,
    IDirect3DDeviceImpl_3_End,
    Thunk_IDirect3DDeviceImpl_3_GetRenderState,
    Thunk_IDirect3DDeviceImpl_3_SetRenderState,
    IDirect3DDeviceImpl_3_GetLightState,
    IDirect3DDeviceImpl_3_SetLightState,
    Thunk_IDirect3DDeviceImpl_3_SetTransform,
    Thunk_IDirect3DDeviceImpl_3_GetTransform,
    Thunk_IDirect3DDeviceImpl_3_MultiplyTransform,
    Thunk_IDirect3DDeviceImpl_3_DrawPrimitive,
    Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive,
    Thunk_IDirect3DDeviceImpl_3_SetClipStatus,
    Thunk_IDirect3DDeviceImpl_3_GetClipStatus,
    Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided,
    Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided,
    Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB,
    Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB,
    Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility,
    Thunk_IDirect3DDeviceImpl_3_GetTexture,
    Thunk_IDirect3DDeviceImpl_3_SetTexture,
    Thunk_IDirect3DDeviceImpl_3_GetTextureStageState,
    Thunk_IDirect3DDeviceImpl_3_SetTextureStageState,
    Thunk_IDirect3DDeviceImpl_3_ValidateDevice
};

const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
{
    /*** IUnknown Methods ***/
    Thunk_IDirect3DDeviceImpl_2_QueryInterface,
    Thunk_IDirect3DDeviceImpl_2_AddRef,
    Thunk_IDirect3DDeviceImpl_2_Release,
    /*** IDirect3DDevice2 ***/
    Thunk_IDirect3DDeviceImpl_2_GetCaps,
    IDirect3DDeviceImpl_2_SwapTextureHandles,
    Thunk_IDirect3DDeviceImpl_2_GetStats,
    Thunk_IDirect3DDeviceImpl_2_AddViewport,
    Thunk_IDirect3DDeviceImpl_2_DeleteViewport,
    Thunk_IDirect3DDeviceImpl_2_NextViewport,
    IDirect3DDeviceImpl_2_EnumTextureFormats,
    Thunk_IDirect3DDeviceImpl_2_BeginScene,
    Thunk_IDirect3DDeviceImpl_2_EndScene,
    Thunk_IDirect3DDeviceImpl_2_GetDirect3D,
    Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport,
    Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport,
    Thunk_IDirect3DDeviceImpl_2_SetRenderTarget,
    Thunk_IDirect3DDeviceImpl_2_GetRenderTarget,
    Thunk_IDirect3DDeviceImpl_2_Begin,
    Thunk_IDirect3DDeviceImpl_2_BeginIndexed,
    Thunk_IDirect3DDeviceImpl_2_Vertex,
    Thunk_IDirect3DDeviceImpl_2_Index,
    Thunk_IDirect3DDeviceImpl_2_End,
    Thunk_IDirect3DDeviceImpl_2_GetRenderState,
    Thunk_IDirect3DDeviceImpl_2_SetRenderState,
    Thunk_IDirect3DDeviceImpl_2_GetLightState,
    Thunk_IDirect3DDeviceImpl_2_SetLightState,
    Thunk_IDirect3DDeviceImpl_2_SetTransform,
    Thunk_IDirect3DDeviceImpl_2_GetTransform,
    Thunk_IDirect3DDeviceImpl_2_MultiplyTransform,
    Thunk_IDirect3DDeviceImpl_2_DrawPrimitive,
    Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive,
    Thunk_IDirect3DDeviceImpl_2_SetClipStatus,
    Thunk_IDirect3DDeviceImpl_2_GetClipStatus
};

const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
{
    /*** IUnknown Methods ***/
    Thunk_IDirect3DDeviceImpl_1_QueryInterface,
    Thunk_IDirect3DDeviceImpl_1_AddRef,
    Thunk_IDirect3DDeviceImpl_1_Release,
    /*** IDirect3DDevice1 ***/
    IDirect3DDeviceImpl_1_Initialize,
    Thunk_IDirect3DDeviceImpl_1_GetCaps,
    Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles,
    IDirect3DDeviceImpl_1_CreateExecuteBuffer,
    Thunk_IDirect3DDeviceImpl_1_GetStats,
    IDirect3DDeviceImpl_1_Execute,
    Thunk_IDirect3DDeviceImpl_1_AddViewport,
    Thunk_IDirect3DDeviceImpl_1_DeleteViewport,
    Thunk_IDirect3DDeviceImpl_1_NextViewport,
    IDirect3DDeviceImpl_1_Pick,
    IDirect3DDeviceImpl_1_GetPickRecords,
    Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats,
    IDirect3DDeviceImpl_1_CreateMatrix,
    IDirect3DDeviceImpl_1_SetMatrix,
    IDirect3DDeviceImpl_1_GetMatrix,
    IDirect3DDeviceImpl_1_DeleteMatrix,
    Thunk_IDirect3DDeviceImpl_1_EndScene,
    Thunk_IDirect3DDeviceImpl_1_BeginScene,
    Thunk_IDirect3DDeviceImpl_1_GetDirect3D
};
5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523

/*****************************************************************************
 * IDirect3DDeviceImpl_CreateHandle
 *
 * Not called from the VTable
 *
 * Some older interface versions operate with handles, which are basically
 * DWORDs which identify an interface, for example
 * IDirect3DDevice::SetRenderState with DIRECT3DRENDERSTATE_TEXTUREHANDLE
 *
 * Those handle could be just casts to the interface pointers or vice versa,
 * but that is not 64 bit safe and would mean blindly derefering a DWORD
 * passed by the app. Instead there is a dynamic array in the device which
 * keeps a DWORD to pointer information and a type for the handle.
 *
 * Basically this array only grows, when a handle is freed its pointer is
 * just set to NULL. There will be much more reads from the array than
 * insertion operations, so a dynamic array is fine.
 *
 * Params:
 *  This: D3DDevice implementation for which this handle should be created
 *
 * Returns:
 *  A free handle on success
 *  0 on failure
 *
 *****************************************************************************/
DWORD
IDirect3DDeviceImpl_CreateHandle(IDirect3DDeviceImpl *This)
{
    DWORD i;
    struct HandleEntry *oldHandles = This->Handles;

    TRACE("(%p)\n", This);

    for(i = 0; i < This->numHandles; i++)
    {
        if(This->Handles[i].ptr == NULL &&
           This->Handles[i].type == DDrawHandle_Unknown)
        {
5524
            TRACE("Reusing freed handle %d\n", i + 1);
5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545
            return i + 1;
        }
    }

    TRACE("Growing the handle array\n");

    This->numHandles++;
    This->Handles = HeapAlloc(GetProcessHeap(), 0, sizeof(struct HandleEntry) * This->numHandles);
    if(!This->Handles)
    {
        ERR("Out of memory\n");
        This->Handles = oldHandles;
        This->numHandles--;
        return 0;
    }
    if(oldHandles)
    {
        memcpy(This->Handles, oldHandles, (This->numHandles - 1) * sizeof(struct HandleEntry));
        HeapFree(GetProcessHeap(), 0, oldHandles);
    }

5546
    TRACE("Returning %d\n", This->numHandles);
5547 5548
    return This->numHandles;
}
5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585

/*****************************************************************************
 * IDirect3DDeviceImpl_UpdateDepthStencil
 *
 * Checks the current render target for attached depth stencils and sets the
 * WineD3D depth stencil accordingly.
 *
 * Returns:
 *  The depth stencil state to set if creating the device
 *
 *****************************************************************************/
WINED3DZBUFFERTYPE
IDirect3DDeviceImpl_UpdateDepthStencil(IDirect3DDeviceImpl *This)
{
    IDirectDrawSurface7 *depthStencil = NULL;
    IDirectDrawSurfaceImpl *dsi;
    static DDSCAPS2 depthcaps = { DDSCAPS_ZBUFFER, 0, 0, 0 };

    IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->target, IDirectDrawSurface7),
                                           &depthcaps,
                                           &depthStencil);
    if(!depthStencil)
    {
        TRACE("Setting wined3d depth stencil to NULL\n");
        IWineD3DDevice_SetDepthStencilSurface(This->wineD3DDevice,
                                              NULL);
        return WINED3DZB_FALSE;
    }

    dsi = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, depthStencil);
    TRACE("Setting wined3d depth stencil to %p (wined3d %p)\n", dsi, dsi->WineD3DSurface);
    IWineD3DDevice_SetDepthStencilSurface(This->wineD3DDevice,
                                          dsi->WineD3DSurface);

    IDirectDrawSurface7_Release(depthStencil);
    return WINED3DZB_TRUE;
}