d3drm: Implement D3DRMCreateColorRGB.
[wine] / dlls / ddraw / device.c
1 /*
2  * Copyright (c) 1998-2004 Lionel Ulmer
3  * Copyright (c) 2002-2005 Christian Costa
4  * Copyright (c) 2006 Stefan Dösinger
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * IDirect3DDevice implementation, version 1, 2, 3 and 7. Rendering is relayed
21  * to WineD3D, some minimal DirectDraw specific management is handled here.
22  * The Direct3DDevice is NOT the parent of the WineD3DDevice, because d3d
23  * is initialized when DirectDraw creates the primary surface.
24  * Some type management is necessary, because some D3D types changed between
25  * D3D7 and D3D9.
26  *
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31 #include "wine/debug.h"
32
33 #include <assert.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38 #define COBJMACROS
39
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winerror.h"
43 #include "wingdi.h"
44 #include "wine/exception.h"
45
46 #include "ddraw.h"
47 #include "d3d.h"
48
49 #include "ddraw_private.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
52 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
53
54 /* The device ID */
55 const GUID IID_D3DDEVICE_WineD3D = {
56   0xaef72d43,
57   0xb09a,
58   0x4b7b,
59   { 0xb7,0x98,0xc6,0x8a,0x77,0x2d,0x72,0x2a }
60 };
61
62 /*****************************************************************************
63  * IUnknown Methods. Common for Version 1, 2, 3 and 7 
64  *****************************************************************************/
65
66 /*****************************************************************************
67  * IDirect3DDevice7::QueryInterface
68  *
69  * Used to query other interfaces from a Direct3DDevice interface.
70  * It can return interface pointers to all Direct3DDevice versions as well
71  * as IDirectDraw and IDirect3D. For a link to QueryInterface
72  * rules see ddraw.c, IDirectDraw7::QueryInterface
73  *
74  * Exists in Version 1, 2, 3 and 7
75  *
76  * Params:
77  *  refiid: Interface ID queried for
78  *  obj: Used to return the interface pointer
79  *
80  * Returns:
81  *  D3D_OK or E_NOINTERFACE
82  *
83  *****************************************************************************/
84 static HRESULT WINAPI
85 IDirect3DDeviceImpl_7_QueryInterface(IDirect3DDevice7 *iface,
86                                      REFIID refiid,
87                                      void **obj)
88 {
89     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
90     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(refiid), obj);
91
92     /* According to COM docs, if the QueryInterface fails, obj should be set to NULL */
93     *obj = NULL;
94
95     if(!refiid)
96         return DDERR_INVALIDPARAMS;
97
98     if ( IsEqualGUID( &IID_IUnknown, refiid ) )
99     {
100         *obj = ICOM_INTERFACE(This, IDirect3DDevice7);
101     }
102
103     /* Check DirectDraw Interfac\ 1s */
104     else if( IsEqualGUID( &IID_IDirectDraw7, refiid ) )
105     {
106         *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw7);
107         TRACE("(%p) Returning IDirectDraw7 interface at %p\n", This, *obj);
108     }
109     else if ( IsEqualGUID( &IID_IDirectDraw4, refiid ) )
110     {
111         *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw4);
112         TRACE("(%p) Returning IDirectDraw4 interface at %p\n", This, *obj);
113     }
114     else if ( IsEqualGUID( &IID_IDirectDraw2, refiid ) )
115     {
116         *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw2);
117         TRACE("(%p) Returning IDirectDraw2 interface at %p\n", This, *obj);
118     }
119     else if( IsEqualGUID( &IID_IDirectDraw, refiid ) )
120     {
121         *obj = ICOM_INTERFACE(This->ddraw, IDirectDraw);
122         TRACE("(%p) Returning IDirectDraw interface at %p\n", This, *obj);
123     }
124
125     /* Direct3D */
126     else if ( IsEqualGUID( &IID_IDirect3D  , refiid ) )
127     {
128         *obj = ICOM_INTERFACE(This->ddraw, IDirect3D);
129         TRACE("(%p) Returning IDirect3D interface at %p\n", This, *obj);
130     }
131     else if ( IsEqualGUID( &IID_IDirect3D2 , refiid ) )
132     {
133         *obj = ICOM_INTERFACE(This->ddraw, IDirect3D2);
134         TRACE("(%p) Returning IDirect3D2 interface at %p\n", This, *obj);
135     }
136     else if ( IsEqualGUID( &IID_IDirect3D3 , refiid ) )
137     {
138         *obj = ICOM_INTERFACE(This->ddraw, IDirect3D3);
139         TRACE("(%p) Returning IDirect3D3 interface at %p\n", This, *obj);
140     }
141     else if ( IsEqualGUID( &IID_IDirect3D7 , refiid ) )
142     {
143         *obj = ICOM_INTERFACE(This->ddraw, IDirect3D7);
144         TRACE("(%p) Returning IDirect3D7 interface at %p\n", This, *obj);
145     }
146
147     /* Direct3DDevice */
148     else if ( IsEqualGUID( &IID_IDirect3DDevice  , refiid ) )
149     {
150         *obj = ICOM_INTERFACE(This, IDirect3DDevice);
151         TRACE("(%p) Returning IDirect3DDevice interface at %p\n", This, *obj);
152     }
153     else if ( IsEqualGUID( &IID_IDirect3DDevice2  , refiid ) ) {
154         *obj = ICOM_INTERFACE(This, IDirect3DDevice2);
155         TRACE("(%p) Returning IDirect3DDevice2 interface at %p\n", This, *obj);
156     }
157     else if ( IsEqualGUID( &IID_IDirect3DDevice3  , refiid ) ) {
158         *obj = ICOM_INTERFACE(This, IDirect3DDevice3);
159         TRACE("(%p) Returning IDirect3DDevice3 interface at %p\n", This, *obj);
160     }
161     else if ( IsEqualGUID( &IID_IDirect3DDevice7  , refiid ) ) {
162         *obj = ICOM_INTERFACE(This, IDirect3DDevice7);
163         TRACE("(%p) Returning IDirect3DDevice7 interface at %p\n", This, *obj);
164     }
165
166     /* Unknown interface */
167     else
168     {
169         ERR("(%p)->(%s, %p): No interface found\n", This, debugstr_guid(refiid), obj);
170         return E_NOINTERFACE;
171     }
172
173     /* AddRef the returned interface */
174     IUnknown_AddRef( (IUnknown *) *obj);
175     return D3D_OK;
176 }
177
178 static HRESULT WINAPI
179 Thunk_IDirect3DDeviceImpl_3_QueryInterface(IDirect3DDevice3 *iface,
180                                            REFIID riid,
181                                            void **obj)
182 {
183     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
184     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obj);
185     return IDirect3DDevice7_QueryInterface(ICOM_INTERFACE(This, IDirect3DDevice7),
186                                            riid,
187                                            obj);
188 }
189
190 static HRESULT WINAPI
191 Thunk_IDirect3DDeviceImpl_2_QueryInterface(IDirect3DDevice2 *iface,
192                                            REFIID riid,
193                                            void **obj)
194 {
195     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
196     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obj);
197     return IDirect3DDevice7_QueryInterface(ICOM_INTERFACE(This, IDirect3DDevice7),
198                                            riid,
199                                            obj);
200 }
201
202 static HRESULT WINAPI
203 Thunk_IDirect3DDeviceImpl_1_QueryInterface(IDirect3DDevice *iface,
204                                            REFIID riid,
205                                            void **obp)
206 {
207     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
208     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DDevice7 interface.\n", This, debugstr_guid(riid), obp);
209     return IDirect3DDevice7_QueryInterface(ICOM_INTERFACE(This, IDirect3DDevice7),
210                                            riid,
211                                            obp);
212 }
213
214 /*****************************************************************************
215  * IDirect3DDevice7::AddRef
216  *
217  * Increases the refcount....
218  * The most exciting Method, definitely
219  *
220  * Exists in Version 1, 2, 3 and 7
221  *
222  * Returns:
223  *  The new refcount
224  *
225  *****************************************************************************/
226 static ULONG WINAPI
227 IDirect3DDeviceImpl_7_AddRef(IDirect3DDevice7 *iface)
228 {
229     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
230     ULONG ref = InterlockedIncrement(&This->ref);
231
232     TRACE("(%p) : incrementing from %u.\n", This, ref -1);
233
234     return ref;
235 }
236
237 static ULONG WINAPI
238 Thunk_IDirect3DDeviceImpl_3_AddRef(IDirect3DDevice3 *iface)
239 {
240     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
241     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
242     return IDirect3DDevice7_AddRef(ICOM_INTERFACE(This, IDirect3DDevice7));
243 }
244
245 static ULONG WINAPI
246 Thunk_IDirect3DDeviceImpl_2_AddRef(IDirect3DDevice2 *iface)
247 {
248     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
249     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
250     return IDirect3DDevice7_AddRef(ICOM_INTERFACE(This, IDirect3DDevice7));
251 }
252
253 static ULONG WINAPI
254 Thunk_IDirect3DDeviceImpl_1_AddRef(IDirect3DDevice *iface)
255 {
256     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", iface);
257     return IDirect3DDevice7_AddRef(COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice7, iface));
258 }
259
260 /*****************************************************************************
261  * IDirect3DDevice7::Release
262  *
263  * Decreases the refcount of the interface
264  * When the refcount is reduced to 0, the object is destroyed.
265  *
266  * Exists in Version 1, 2, 3 and 7
267  *
268  * Returns:d
269  *  The new refcount
270  *
271  *****************************************************************************/
272 static ULONG WINAPI
273 IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
274 {
275     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
276     ULONG ref = InterlockedDecrement(&This->ref);
277
278     TRACE("(%p)->() decrementing from %u.\n", This, ref +1);
279
280     /* This method doesn't destroy the WineD3DDevice, because it's still in use for
281      * 2D rendering. IDirectDrawSurface7::Release will destroy the WineD3DDevice
282      * when the render target is released
283      */
284     if (ref == 0)
285     {
286         IParent *IndexBufferParent;
287         DWORD i;
288
289         /* Free the index buffer. */
290         IWineD3DDevice_SetIndices(This->wineD3DDevice,
291                                   NULL,
292                                   0);
293         IWineD3DIndexBuffer_GetParent(This->indexbuffer,
294                                       (IUnknown **) &IndexBufferParent);
295         IParent_Release(IndexBufferParent); /* Once for the getParent */
296         if( IParent_Release(IndexBufferParent) != 0)  /* And now to destroy it */
297         {
298             ERR(" (%p) Something is still holding the index buffer parent %p\n", This, IndexBufferParent);
299         }
300
301         /* There is no need to unset the vertex buffer here, IWineD3DDevice_Uninit3D will do that when
302          * destroying the primary stateblock. If a vertex buffer is destroyed while it is bound
303          * IDirect3DVertexBuffer::Release will unset it.
304          */
305
306         /* Restore the render targets */
307         if(This->OffScreenTarget)
308         {
309             WINED3DVIEWPORT vp;
310
311             vp.X = 0;
312             vp.Y = 0;
313             vp.Width = This->ddraw->d3d_target->surface_desc.dwWidth;
314             vp.Height = This->ddraw->d3d_target->surface_desc.dwHeight;
315             vp.MinZ = 0.0;
316             vp.MaxZ = 1.0;
317             IWineD3DDevice_SetViewport(This->wineD3DDevice,
318                                        &vp);
319
320             /* Set the device up to render to the front buffer since the back buffer will
321              * vanish soon.
322              */
323             IWineD3DDevice_SetRenderTarget(This->wineD3DDevice, 0,
324                                            This->ddraw->d3d_target->WineD3DSurface);
325             /* This->target is the offscreen target.
326              * This->ddraw->d3d_target is the target used by DDraw
327              */
328             TRACE("(%p) Release: Using %p as front buffer, %p as back buffer\n", This, This->ddraw->d3d_target, NULL);
329             IWineD3DDevice_SetFrontBackBuffers(This->wineD3DDevice,
330                                                This->ddraw->d3d_target->WineD3DSurface,
331                                                NULL);
332         }
333
334         /* Release the WineD3DDevice. This won't destroy it */
335         if(IWineD3DDevice_Release(This->wineD3DDevice) <= 0)
336         {
337             ERR(" (%p) The wineD3D device %p was destroyed unexpectadely. Prepare for trouble\n", This, This->wineD3DDevice);
338         }
339
340         /* The texture handles should be unset by now, but there might be some bits
341          * missing in our reference counting(needs test). Do a sanity check
342          */
343         for(i = 0; i < This->numHandles; i++)
344         {
345             if(This->Handles[i].ptr)
346             {
347                 switch(This->Handles[i].type)
348                 {
349                     case DDrawHandle_Texture:
350                     {
351                         IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[i].ptr;
352                         FIXME("Texture Handle %d not unset properly\n", i + 1);
353                         surf->Handle = 0;
354                     }
355                     break;
356
357                     case DDrawHandle_Material:
358                     {
359                         IDirect3DMaterialImpl *mat = (IDirect3DMaterialImpl *) This->Handles[i].ptr;
360                         FIXME("Material handle %d not unset properly\n", i + 1);
361                         mat->Handle = 0;
362                     }
363                     break;
364
365                     case DDrawHandle_Matrix:
366                     {
367                         /* No fixme here because this might happen because of sloppy apps */
368                         WARN("Leftover matrix handle %d, deleting\n", i + 1);
369                         IDirect3DDevice_DeleteMatrix(ICOM_INTERFACE(This, IDirect3DDevice),
370                                                      i + 1);
371                     }
372                     break;
373
374                     case DDrawHandle_StateBlock:
375                     {
376                         /* No fixme here because this might happen because of sloppy apps */
377                         WARN("Leftover stateblock handle %d, deleting\n", i + 1);
378                         IDirect3DDevice7_DeleteStateBlock(ICOM_INTERFACE(This, IDirect3DDevice7),
379                                                           i + 1);
380                     }
381                     break;
382
383                     default:
384                         FIXME("Unknown handle %d not unset properly\n", i + 1);
385                 }
386             }
387         }
388
389         HeapFree(GetProcessHeap(), 0, This->Handles);
390
391         TRACE("Releasing target %p %p\n", This->target, This->ddraw->d3d_target);
392         /* Release the render target and the WineD3D render target
393          * (See IDirect3D7::CreateDevice for more comments on this)
394          */
395         IDirectDrawSurface7_Release(ICOM_INTERFACE(This->target, IDirectDrawSurface7));
396         IDirectDrawSurface7_Release(ICOM_INTERFACE(This->ddraw->d3d_target,IDirectDrawSurface7));
397         TRACE("Target release done\n");
398
399         This->ddraw->d3ddevice = NULL;
400
401         /* Now free the structure */
402         HeapFree(GetProcessHeap(), 0, This);
403     }
404
405     TRACE("Done\n");
406     return ref;
407 }
408
409 static ULONG WINAPI
410 Thunk_IDirect3DDeviceImpl_3_Release(IDirect3DDevice3 *iface)
411 {
412     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
413     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
414     return IDirect3DDevice7_Release(ICOM_INTERFACE(This, IDirect3DDevice7));
415 }
416
417 static ULONG WINAPI
418 Thunk_IDirect3DDeviceImpl_2_Release(IDirect3DDevice2 *iface)
419 {
420     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
421     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
422     return IDirect3DDevice7_Release(ICOM_INTERFACE(This, IDirect3DDevice7));
423 }
424
425 static ULONG WINAPI
426 Thunk_IDirect3DDeviceImpl_1_Release(IDirect3DDevice *iface)
427 {
428     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
429     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
430     return IDirect3DDevice7_Release(ICOM_INTERFACE(This, IDirect3DDevice7));
431 }
432
433 /*****************************************************************************
434  * IDirect3DDevice Methods
435  *****************************************************************************/
436
437 /*****************************************************************************
438  * IDirect3DDevice::Initialize
439  *
440  * Initializes a Direct3DDevice. This implementation is a no-op, as all
441  * initialization is done at create time.
442  *
443  * Exists in Version 1
444  *
445  * Parameters:
446  *  No idea what they mean, as the MSDN page is gone
447  *
448  * Returns: DD_OK
449  *
450  *****************************************************************************/
451 static HRESULT WINAPI
452 IDirect3DDeviceImpl_1_Initialize(IDirect3DDevice *iface,
453                                  IDirect3D *Direct3D, GUID *guid,
454                                  D3DDEVICEDESC *Desc)
455 {
456     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
457
458     /* It shouldn't be crucial, but print a FIXME, I'm interested if
459      * any game calls it and when
460      */
461     FIXME("(%p)->(%p,%p,%p): No-op!\n", This, Direct3D, guid, Desc);
462
463     return D3D_OK;
464 }
465
466 /*****************************************************************************
467  * IDirect3DDevice7::GetCaps
468  *
469  * Retrieves the device's capabilities
470  *
471  * This implementation is used for Version 7 only, the older versions have
472  * their own implementation.
473  *
474  * Parameters:
475  *  Desc: Pointer to a D3DDEVICEDESC7 structure to fill
476  *
477  * Returns:
478  *  D3D_OK on success
479  *  D3DERR_* if a problem occurs. See WineD3D
480  *
481  *****************************************************************************/
482 static HRESULT WINAPI
483 IDirect3DDeviceImpl_7_GetCaps(IDirect3DDevice7 *iface,
484                               D3DDEVICEDESC7 *Desc)
485 {
486     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
487     D3DDEVICEDESC OldDesc;
488     TRACE("(%p)->(%p)\n", This, Desc);
489
490     /* Call the same function used by IDirect3D, this saves code */
491     return IDirect3DImpl_GetCaps(This->ddraw->wineD3D, &OldDesc, Desc);
492 }
493
494 /*****************************************************************************
495  * IDirect3DDevice3::GetCaps
496  *
497  * Retrieves the capabilities of the hardware device and the emulation
498  * device. For Wine, hardware and emulation are the same (it's all HW).
499  *
500  * This implementation is used for Version 1, 2, and 3. Version 7 has its own
501  *
502  * Parameters:
503  *  HWDesc: Structure to fill with the HW caps
504  *  HelDesc: Structure to fill with the hardare emulation caps
505  *
506  * Returns:
507  *  D3D_OK on success
508  *  D3DERR_* if a problem occurs. See WineD3D
509  *
510  *****************************************************************************/
511 static HRESULT WINAPI
512 IDirect3DDeviceImpl_3_GetCaps(IDirect3DDevice3 *iface,
513                               D3DDEVICEDESC *HWDesc,
514                               D3DDEVICEDESC *HelDesc)
515 {
516     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
517     D3DDEVICEDESC7 newDesc;
518     HRESULT hr;
519     TRACE("(%p)->(%p,%p)\n", iface, HWDesc, HelDesc);
520
521     hr = IDirect3DImpl_GetCaps(This->ddraw->wineD3D, HWDesc, &newDesc);
522     if(hr != D3D_OK) return hr;
523
524     *HelDesc = *HWDesc;
525     return D3D_OK;
526 }
527
528 static HRESULT WINAPI
529 Thunk_IDirect3DDeviceImpl_2_GetCaps(IDirect3DDevice2 *iface,
530                                     D3DDEVICEDESC *D3DHWDevDesc,
531                                     D3DDEVICEDESC *D3DHELDevDesc)
532 {
533     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
534     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice3 interface.\n", This, D3DHWDevDesc, D3DHELDevDesc);
535     return IDirect3DDevice3_GetCaps(ICOM_INTERFACE(This, IDirect3DDevice3),
536                                     D3DHWDevDesc,
537                                     D3DHELDevDesc);
538 }
539
540 static HRESULT WINAPI
541 Thunk_IDirect3DDeviceImpl_1_GetCaps(IDirect3DDevice *iface,
542                                     D3DDEVICEDESC *D3DHWDevDesc,
543                                     D3DDEVICEDESC *D3DHELDevDesc)
544 {
545     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
546     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice3 interface.\n", This, D3DHWDevDesc, D3DHELDevDesc);
547     return IDirect3DDevice3_GetCaps(ICOM_INTERFACE(This, IDirect3DDevice3),
548                                     D3DHWDevDesc,
549                                     D3DHELDevDesc);
550 }
551
552 /*****************************************************************************
553  * IDirect3DDevice2::SwapTextureHandles
554  *
555  * Swaps the texture handles of 2 Texture interfaces. Version 1 and 2
556  *
557  * Parameters:
558  *  Tex1, Tex2: The 2 Textures to swap
559  *
560  * Returns:
561  *  D3D_OK
562  *
563  *****************************************************************************/
564 static HRESULT WINAPI
565 IDirect3DDeviceImpl_2_SwapTextureHandles(IDirect3DDevice2 *iface,
566                                          IDirect3DTexture2 *Tex1,
567                                          IDirect3DTexture2 *Tex2)
568 {
569     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
570     DWORD swap;
571     IDirectDrawSurfaceImpl *surf1 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Tex1);
572     IDirectDrawSurfaceImpl *surf2 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Tex2);
573     TRACE("(%p)->(%p,%p)\n", This, surf1, surf2);
574
575     This->Handles[surf1->Handle - 1].ptr = surf2;
576     This->Handles[surf2->Handle - 1].ptr = surf1;
577
578     swap = surf2->Handle;
579     surf2->Handle = surf1->Handle;
580     surf1->Handle = swap;
581
582     return D3D_OK;
583 }
584
585 static HRESULT WINAPI
586 Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles(IDirect3DDevice *iface,
587                                                IDirect3DTexture *D3DTex1,
588                                                IDirect3DTexture *D3DTex2)
589 {
590     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
591     IDirectDrawSurfaceImpl *surf1 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture, D3DTex1);
592     IDirectDrawSurfaceImpl *surf2 = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture, D3DTex2);
593     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice2 interface.\n", This, surf1, surf2);
594     return IDirect3DDevice2_SwapTextureHandles(ICOM_INTERFACE(This, IDirect3DDevice2),
595                                                ICOM_INTERFACE(surf1, IDirect3DTexture2),
596                                                ICOM_INTERFACE(surf2, IDirect3DTexture2));
597 }
598
599 /*****************************************************************************
600  * IDirect3DDevice3::GetStats
601  *
602  * This method seems to retrieve some stats from the device.
603  * The MSDN documentation doesn't exist any more, but the D3DSTATS
604  * structure suggests that the amout of drawn primitives and processed
605  * vertices is returned.
606  *
607  * Exists in Version 1, 2 and 3
608  *
609  * Parameters:
610  *  Stats: Pointer to a D3DSTATS structure to be filled
611  *
612  * Returns:
613  *  D3D_OK on success
614  *  DDERR_INVALIDPARAMS if Stats == NULL
615  *
616  *****************************************************************************/
617 static HRESULT WINAPI
618 IDirect3DDeviceImpl_3_GetStats(IDirect3DDevice3 *iface,
619                                D3DSTATS *Stats)
620 {
621     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
622     FIXME("(%p)->(%p): Stub!\n", This, Stats);
623
624     if(!Stats)
625         return DDERR_INVALIDPARAMS;
626
627     /* Fill the Stats with 0 */
628     Stats->dwTrianglesDrawn = 0;
629     Stats->dwLinesDrawn = 0;
630     Stats->dwPointsDrawn = 0;
631     Stats->dwSpansDrawn = 0;
632     Stats->dwVerticesProcessed = 0;
633
634     return D3D_OK;
635 }
636
637 static HRESULT WINAPI
638 Thunk_IDirect3DDeviceImpl_2_GetStats(IDirect3DDevice2 *iface,
639                                      D3DSTATS *Stats)
640 {
641     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
642     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Stats);
643     return IDirect3DDevice3_GetStats(ICOM_INTERFACE(This, IDirect3DDevice3),
644                                      Stats);
645 }
646
647 static HRESULT WINAPI
648 Thunk_IDirect3DDeviceImpl_1_GetStats(IDirect3DDevice *iface,
649                                      D3DSTATS *Stats)
650 {
651     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
652     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Stats);
653     return IDirect3DDevice3_GetStats(ICOM_INTERFACE(This, IDirect3DDevice3),
654                                      Stats);
655 }
656
657 /*****************************************************************************
658  * IDirect3DDevice::CreateExecuteBuffer
659  *
660  * Creates an IDirect3DExecuteBuffer, used for rendering with a
661  * Direct3DDevice.
662  *
663  * Version 1 only.
664  *
665  * Params:
666  *  Desc: Buffer description
667  *  ExecuteBuffer: Address to return the Interface pointer at
668  *  UnkOuter: Must be NULL. Basically for aggregation, which ddraw doesn't
669  *            support
670  *
671  * Returns:
672  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
673  *  DDERR_OUTOFMEMORY if we ran out of memory
674  *  D3D_OK on success
675  *
676  *****************************************************************************/
677 static HRESULT WINAPI
678 IDirect3DDeviceImpl_1_CreateExecuteBuffer(IDirect3DDevice *iface,
679                                           D3DEXECUTEBUFFERDESC *Desc,
680                                           IDirect3DExecuteBuffer **ExecuteBuffer,
681                                           IUnknown *UnkOuter)
682 {
683     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
684     IDirect3DExecuteBufferImpl* object;
685     TRACE("(%p)->(%p,%p,%p)!\n", This, Desc, ExecuteBuffer, UnkOuter);
686
687     if(UnkOuter)
688         return CLASS_E_NOAGGREGATION;
689
690     /* Allocate the new Execute Buffer */
691     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DExecuteBufferImpl));
692     if(!object)
693     {
694         ERR("Out of memory when allocating a IDirect3DExecuteBufferImpl structure\n");
695         return DDERR_OUTOFMEMORY;
696     }
697
698     ICOM_INIT_INTERFACE(object, IDirect3DExecuteBuffer, IDirect3DExecuteBuffer_Vtbl);
699
700     object->ref = 1;
701     object->d3ddev = This;
702
703     /* Initializes memory */
704     memcpy(&object->desc, Desc, Desc->dwSize);
705
706     /* No buffer given */
707     if ((object->desc.dwFlags & D3DDEB_LPDATA) == 0)
708         object->desc.lpData = NULL;
709
710     /* No buffer size given */
711     if ((object->desc.dwFlags & D3DDEB_BUFSIZE) == 0)
712         object->desc.dwBufferSize = 0;
713
714     /* Create buffer if asked */
715     if ((object->desc.lpData == NULL) && (object->desc.dwBufferSize > 0))
716     {
717         object->need_free = TRUE;
718         object->desc.lpData = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,object->desc.dwBufferSize);
719         if(!object->desc.lpData)
720         {
721             ERR("Out of memory when allocating the execute buffer data\n");
722             HeapFree(GetProcessHeap(), 0, object);
723             return DDERR_OUTOFMEMORY;
724         }
725     }
726     else
727     {
728         object->need_free = FALSE;
729     }
730
731     /* No vertices for the moment */
732     object->vertex_data = NULL;
733
734     object->desc.dwFlags |= D3DDEB_LPDATA;
735
736     object->indices = NULL;
737     object->nb_indices = 0;
738
739     *ExecuteBuffer = ICOM_INTERFACE(object, IDirect3DExecuteBuffer);
740
741     TRACE(" Returning IDirect3DExecuteBuffer at %p, implementation is at %p\n", *ExecuteBuffer, object);
742
743     return D3D_OK;
744 }
745
746 /*****************************************************************************
747  * IDirect3DDevice::Execute
748  *
749  * Executes all the stuff in an execute buffer.
750  *
751  * Params:
752  *  ExecuteBuffer: The buffer to execute
753  *  Viewport: The viewport used for rendering
754  *  Flags: Some flags
755  *
756  * Returns:
757  *  DDERR_INVALIDPARAMS if ExecuteBuffer == NULL
758  *  D3D_OK on success
759  *
760  *****************************************************************************/
761 static HRESULT WINAPI
762 IDirect3DDeviceImpl_1_Execute(IDirect3DDevice *iface,
763                               IDirect3DExecuteBuffer *ExecuteBuffer,
764                               IDirect3DViewport *Viewport,
765                               DWORD Flags)
766 {
767     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
768     IDirect3DExecuteBufferImpl *Direct3DExecuteBufferImpl = ICOM_OBJECT(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, ExecuteBuffer);
769     IDirect3DViewportImpl *Direct3DViewportImpl = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);
770
771     TRACE("(%p)->(%p,%p,%08x)\n", This, Direct3DExecuteBufferImpl, Direct3DViewportImpl, Flags);
772
773     if(!Direct3DExecuteBufferImpl)
774         return DDERR_INVALIDPARAMS;
775
776     /* Execute... */
777     IDirect3DExecuteBufferImpl_Execute(Direct3DExecuteBufferImpl, This, Direct3DViewportImpl);
778
779     return D3D_OK;
780 }
781
782 /*****************************************************************************
783  * IDirect3DDevice3::AddViewport
784  *
785  * Add a Direct3DViewport to the device's viewport list. These viewports
786  * are wrapped to IDirect3DDevice7 viewports in viewport.c
787  *
788  * Exists in Version 1, 2 and 3. Note that IDirect3DViewport 1, 2 and 3
789  * are the same interfaces.
790  *
791  * Params:
792  *  Viewport: The viewport to add
793  *
794  * Returns:
795  *  DDERR_INVALIDPARAMS if Viewport == NULL
796  *  D3D_OK on success
797  *
798  *****************************************************************************/
799 static HRESULT WINAPI
800 IDirect3DDeviceImpl_3_AddViewport(IDirect3DDevice3 *iface,
801                                   IDirect3DViewport3 *Viewport)
802 {
803     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
804     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);
805
806     TRACE("(%p)->(%p)\n", This, vp);
807
808     /* Sanity check */
809     if(!vp)
810         return DDERR_INVALIDPARAMS;
811
812     vp->next = This->viewport_list;
813     This->viewport_list = vp;
814
815     return D3D_OK;
816 }
817
818 static HRESULT WINAPI
819 Thunk_IDirect3DDeviceImpl_2_AddViewport(IDirect3DDevice2 *iface,
820                                         IDirect3DViewport2 *Direct3DViewport2)
821 {
822     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
823     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
824     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
825     return IDirect3DDevice3_AddViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
826                                         ICOM_INTERFACE(vp, IDirect3DViewport3));
827 }
828
829 static HRESULT WINAPI
830 Thunk_IDirect3DDeviceImpl_1_AddViewport(IDirect3DDevice *iface,
831                                         IDirect3DViewport *Direct3DViewport)
832 {
833     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
834     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport);
835     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
836     return IDirect3DDevice3_AddViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
837                                         ICOM_INTERFACE(vp, IDirect3DViewport3));
838 }
839
840 /*****************************************************************************
841  * IDirect3DDevice3::DeleteViewport
842  *
843  * Deletes a Direct3DViewport from the device's viewport list.
844  *
845  * Exists in Version 1, 2 and 3. Note that all Viewport interface versions
846  * are equal.
847  *
848  * Params:
849  *  Viewport: The viewport to delete
850  *
851  * Returns:
852  *  D3D_OK on success
853  *  DDERR_INVALIDPARAMS if the viewport wasn't found in the list
854  *
855  *****************************************************************************/
856 static HRESULT WINAPI
857 IDirect3DDeviceImpl_3_DeleteViewport(IDirect3DDevice3 *iface,
858                                      IDirect3DViewport3 *Viewport)
859 {
860     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
861     IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *) Viewport;
862     IDirect3DViewportImpl *cur_viewport, *prev_viewport = NULL;
863
864     TRACE("(%p)->(%p)\n", This, vp);
865
866     cur_viewport = This->viewport_list;
867     while (cur_viewport != NULL)
868     {
869         if (cur_viewport == vp)
870         {
871             if (prev_viewport == NULL) This->viewport_list = cur_viewport->next;
872             else prev_viewport->next = cur_viewport->next;
873             /* TODO : add desactivate of the viewport and all associated lights... */
874             return D3D_OK;
875         }
876         prev_viewport = cur_viewport;
877         cur_viewport = cur_viewport->next;
878     }
879
880     return DDERR_INVALIDPARAMS;
881 }
882
883 static HRESULT WINAPI
884 Thunk_IDirect3DDeviceImpl_2_DeleteViewport(IDirect3DDevice2 *iface,
885                                            IDirect3DViewport2 *Direct3DViewport2)
886 {
887     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
888     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
889     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
890     return IDirect3DDevice3_DeleteViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
891                                            ICOM_INTERFACE(vp, IDirect3DViewport3));
892 }
893
894 static HRESULT WINAPI
895 Thunk_IDirect3DDeviceImpl_1_DeleteViewport(IDirect3DDevice *iface,
896                                            IDirect3DViewport *Direct3DViewport)
897 {
898     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
899     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport);
900     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
901     return IDirect3DDevice3_DeleteViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
902                                            ICOM_INTERFACE(vp, IDirect3DViewport3));
903 }
904
905 /*****************************************************************************
906  * IDirect3DDevice3::NextViewport
907  *
908  * Returns a viewport from the viewport list, depending on the
909  * passed viewport and the flags.
910  *
911  * Exists in Version 1, 2 and 3. Note that all Viewport interface versions
912  * are equal.
913  *
914  * Params:
915  *  Viewport: Viewport to use for beginning the search
916  *  Flags: D3DNEXT_NEXT, D3DNEXT_HEAD or D3DNEXT_TAIL
917  *
918  * Returns:
919  *  D3D_OK on success
920  *  DDERR_INVALIDPARAMS if the flags were wrong, or Viewport was NULL
921  *
922  *****************************************************************************/
923 static HRESULT WINAPI
924 IDirect3DDeviceImpl_3_NextViewport(IDirect3DDevice3 *iface,
925                                    IDirect3DViewport3 *Viewport3,
926                                    IDirect3DViewport3 **lplpDirect3DViewport3,
927                                    DWORD Flags)
928 {
929     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
930     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport3);
931     IDirect3DViewportImpl *res = NULL;
932
933     TRACE("(%p)->(%p,%p,%08x)\n", This, vp, lplpDirect3DViewport3, Flags);
934
935     if(!vp)
936     {
937         *lplpDirect3DViewport3 = NULL;
938         return DDERR_INVALIDPARAMS;
939     }
940
941
942     switch (Flags)
943     {
944         case D3DNEXT_NEXT:
945         {
946             res = vp->next;
947         }
948         break;
949         case D3DNEXT_HEAD:
950         {
951             res = This->viewport_list;
952         }
953         break;
954         case D3DNEXT_TAIL:
955         {
956             IDirect3DViewportImpl *cur_viewport = This->viewport_list;
957             if (cur_viewport != NULL)
958             {
959                 while (cur_viewport->next != NULL) cur_viewport = cur_viewport->next;
960             }
961             res = cur_viewport;
962         }
963         break;
964         default:
965             *lplpDirect3DViewport3 = NULL;
966             return DDERR_INVALIDPARAMS;
967     }
968
969     *lplpDirect3DViewport3 = ICOM_INTERFACE(res, IDirect3DViewport3);
970     return D3D_OK;
971 }
972
973 static HRESULT WINAPI
974 Thunk_IDirect3DDeviceImpl_2_NextViewport(IDirect3DDevice2 *iface,
975                                          IDirect3DViewport2 *Viewport2,
976                                          IDirect3DViewport2 **lplpDirect3DViewport2,
977                                          DWORD Flags)
978 {
979     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
980     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport2);
981     IDirect3DViewport3 *res;
982     HRESULT hr;
983     TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x) thunking to IDirect3DDevice3 interface.\n", This, vp, lplpDirect3DViewport2, Flags);
984     hr = IDirect3DDevice3_NextViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
985                                        ICOM_INTERFACE(vp, IDirect3DViewport3),
986                                        &res,
987                                        Flags);
988     *lplpDirect3DViewport2 = (IDirect3DViewport2 *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, res);
989     return hr;
990 }
991
992 static HRESULT WINAPI
993 Thunk_IDirect3DDeviceImpl_1_NextViewport(IDirect3DDevice *iface,
994                                          IDirect3DViewport *Viewport,
995                                          IDirect3DViewport **lplpDirect3DViewport,
996                                          DWORD Flags)
997 {
998     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
999     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);
1000     IDirect3DViewport3 *res;
1001     HRESULT hr;
1002     TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x) thunking to IDirect3DDevice3 interface.\n", This, vp, lplpDirect3DViewport, Flags);
1003     hr = IDirect3DDevice3_NextViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
1004                                        ICOM_INTERFACE(vp, IDirect3DViewport3),
1005                                        &res,
1006                                        Flags);
1007     *lplpDirect3DViewport = (IDirect3DViewport *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, res);
1008     return hr;
1009 }
1010
1011 /*****************************************************************************
1012  * IDirect3DDevice::Pick
1013  *
1014  * Executes an execute buffer without performing rendering. Instead, a
1015  * list of primitives that intersect with (x1,y1) of the passed rectangle
1016  * is created. IDirect3DDevice::GetPickRecords can be used to retrieve
1017  * this list.
1018  *
1019  * Version 1 only
1020  *
1021  * Params:
1022  *  ExecuteBuffer: Buffer to execute
1023  *  Viewport: Viewport to use for execution
1024  *  Flags: None are defined, according to the SDK
1025  *  Rect: Specifies the coordinates to be picked. Only x1 and y2 are used,
1026  *        x2 and y2 are ignored.
1027  *
1028  * Returns:
1029  *  D3D_OK because it's a stub
1030  *
1031  *****************************************************************************/
1032 static HRESULT WINAPI
1033 IDirect3DDeviceImpl_1_Pick(IDirect3DDevice *iface,
1034                            IDirect3DExecuteBuffer *ExecuteBuffer,
1035                            IDirect3DViewport *Viewport,
1036                            DWORD Flags,
1037                            D3DRECT *Rect)
1038 {
1039     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1040     IDirect3DExecuteBufferImpl *execbuf = ICOM_OBJECT(IDirect3DExecuteBufferImpl, IDirect3DExecuteBuffer, ExecuteBuffer);
1041     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Viewport);
1042     FIXME("(%p)->(%p,%p,%08x,%p): stub!\n", This, execbuf, vp, Flags, Rect);
1043
1044     return D3D_OK;
1045 }
1046
1047 /*****************************************************************************
1048  * IDirect3DDevice::GetPickRecords
1049  *
1050  * Retrieves the pick records generated by IDirect3DDevice::GetPickRecords
1051  *
1052  * Version 1 only
1053  *
1054  * Params:
1055  *  Count: Pointer to a DWORD containing the numbers of pick records to
1056  *         retrieve
1057  *  D3DPickRec: Address to store the resulting D3DPICKRECORD arry.
1058  *
1059  * Returns:
1060  *  D3D_OK, because it's a stub
1061  *
1062  *****************************************************************************/
1063 static HRESULT WINAPI
1064 IDirect3DDeviceImpl_1_GetPickRecords(IDirect3DDevice *iface,
1065                                      DWORD *Count,
1066                                      D3DPICKRECORD *D3DPickRec)
1067 {
1068     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1069     FIXME("(%p)->(%p,%p): stub!\n", This, Count, D3DPickRec);
1070
1071     return D3D_OK;
1072 }
1073
1074 /*****************************************************************************
1075  * IDirect3DDevice7::EnumTextureformats
1076  *
1077  * Enumerates the supported texture formats. It has a list of all possible
1078  * formats and calls IWineD3D::CheckDeviceFormat for each format to see if
1079  * WineD3D supports it. If so, then it is passed to the app.
1080  *
1081  * This is for Version 7 and 3, older versions have a different
1082  * callback function and their own implementation
1083  *
1084  * Params:
1085  *  Callback: Callback to call for each enumerated format
1086  *  Arg: Argument to pass to the callback
1087  *
1088  * Returns:
1089  *  D3D_OK on success
1090  *  DDERR_INVALIDPARAMS if Callback == NULL
1091  *
1092  *****************************************************************************/
1093 static HRESULT WINAPI
1094 IDirect3DDeviceImpl_7_EnumTextureFormats(IDirect3DDevice7 *iface,
1095                                          LPD3DENUMPIXELFORMATSCALLBACK Callback,
1096                                          void *Arg)
1097 {
1098     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1099     HRESULT hr;
1100     int i;
1101
1102     WINED3DFORMAT FormatList[] = {
1103         /* 32 bit */
1104         WINED3DFMT_A8R8G8B8,
1105         WINED3DFMT_X8R8G8B8,
1106         /* 24 bit */
1107         WINED3DFMT_R8G8B8,
1108         /* 16 Bit */
1109         WINED3DFMT_A1R5G5B5,
1110         WINED3DFMT_A4R4G4B4,
1111         WINED3DFMT_R5G6B5,
1112         WINED3DFMT_X1R5G5B5,
1113         /* 8 Bit */
1114         WINED3DFMT_R3G3B2,
1115         WINED3DFMT_P8,
1116         /* FOURCC codes */
1117         WINED3DFMT_DXT1,
1118         WINED3DFMT_DXT3,
1119         WINED3DFMT_DXT5,
1120     };
1121
1122     WINED3DFORMAT BumpFormatList[] = {
1123         WINED3DFMT_V8U8,
1124         WINED3DFMT_L6V5U5,
1125         WINED3DFMT_X8L8V8U8,
1126         WINED3DFMT_Q8W8V8U8,
1127         WINED3DFMT_V16U16,
1128         WINED3DFMT_W11V11U10,
1129         WINED3DFMT_A2W10V10U10
1130     };
1131
1132     TRACE("(%p)->(%p,%p): Relay\n", This, Callback, Arg);
1133
1134     if(!Callback)
1135         return DDERR_INVALIDPARAMS;
1136
1137     for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
1138     {
1139         hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
1140                                         0 /* Adapter */,
1141                                         0 /* DeviceType */,
1142                                         0 /* AdapterFormat */,
1143                                         0 /* Usage */,
1144                                         0 /* ResourceType */,
1145                                         FormatList[i]);
1146         if(hr == D3D_OK)
1147         {
1148             DDPIXELFORMAT pformat;
1149
1150             memset(&pformat, 0, sizeof(pformat));
1151             pformat.dwSize = sizeof(pformat);
1152             PixelFormat_WineD3DtoDD(&pformat, FormatList[i]);
1153
1154             TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
1155             hr = Callback(&pformat, Arg);
1156             if(hr != DDENUMRET_OK)
1157             {
1158                 TRACE("Format enumeration cancelled by application\n");
1159                 return D3D_OK;
1160             }
1161         }
1162     }
1163
1164     for(i = 0; i < sizeof(BumpFormatList) / sizeof(WINED3DFORMAT); i++)
1165     {
1166         hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
1167                                         0 /* Adapter */,
1168                                         0 /* DeviceType */,
1169                                         0 /* AdapterFormat */,
1170                                         WINED3DUSAGE_QUERY_LEGACYBUMPMAP,
1171                                         0 /* ResourceType */,
1172                                         BumpFormatList[i]);
1173         if(hr == D3D_OK)
1174         {
1175             DDPIXELFORMAT pformat;
1176
1177             memset(&pformat, 0, sizeof(pformat));
1178             pformat.dwSize = sizeof(pformat);
1179             PixelFormat_WineD3DtoDD(&pformat, BumpFormatList[i]);
1180
1181             TRACE("Enumerating WineD3DFormat %d\n", BumpFormatList[i]);
1182             hr = Callback(&pformat, Arg);
1183             if(hr != DDENUMRET_OK)
1184             {
1185                 TRACE("Format enumeration cancelled by application\n");
1186                 return D3D_OK;
1187             }
1188         }
1189     }
1190     TRACE("End of enumeration\n");
1191     return D3D_OK;
1192 }
1193
1194 static HRESULT WINAPI
1195 Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats(IDirect3DDevice3 *iface,
1196                                                LPD3DENUMPIXELFORMATSCALLBACK Callback,
1197                                                void *Arg)
1198 {
1199     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1200     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice7 interface.\n", This, Callback, Arg);
1201     return IDirect3DDevice7_EnumTextureFormats(ICOM_INTERFACE(This, IDirect3DDevice7),
1202                                                Callback,
1203                                                Arg);
1204 }
1205
1206 /*****************************************************************************
1207  * IDirect3DDevice2::EnumTextureformats
1208  *
1209  * EnumTextureFormats for Version 1 and 2, see
1210  * IDirect3DDevice7::EnumTexureFormats for a more detailed description.
1211  *
1212  * This version has a different callback and does not enumerate FourCC
1213  * formats
1214  *
1215  *****************************************************************************/
1216 static HRESULT WINAPI
1217 IDirect3DDeviceImpl_2_EnumTextureFormats(IDirect3DDevice2 *iface,
1218                                          LPD3DENUMTEXTUREFORMATSCALLBACK Callback,
1219                                          void *Arg)
1220 {
1221     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1222     HRESULT hr;
1223     int i;
1224
1225     WINED3DFORMAT FormatList[] = {
1226         /* 32 bit */
1227         WINED3DFMT_A8R8G8B8,
1228         WINED3DFMT_X8R8G8B8,
1229         /* 24 bit */
1230         WINED3DFMT_R8G8B8,
1231         /* 16 Bit */
1232         WINED3DFMT_A1R5G5B5,
1233         WINED3DFMT_A4R4G4B4,
1234         WINED3DFMT_R5G6B5,
1235         WINED3DFMT_X1R5G5B5,
1236         /* 8 Bit */
1237         WINED3DFMT_R3G3B2,
1238         WINED3DFMT_P8,
1239         /* FOURCC codes - Not in this version*/
1240     };
1241
1242     TRACE("(%p)->(%p,%p): Relay\n", This, Callback, Arg);
1243
1244     if(!Callback)
1245         return DDERR_INVALIDPARAMS;
1246
1247     for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
1248     {
1249         hr = IWineD3D_CheckDeviceFormat(This->ddraw->wineD3D,
1250                                         0 /* Adapter */,
1251                                         0 /* DeviceType */,
1252                                         0 /* AdapterFormat */,
1253                                         0 /* Usage */,
1254                                         0 /* ResourceType */,
1255                                         FormatList[i]);
1256         if(hr == D3D_OK)
1257         {
1258             DDSURFACEDESC sdesc;
1259
1260             memset(&sdesc, 0, sizeof(sdesc));
1261             sdesc.dwSize = sizeof(sdesc);
1262             sdesc.dwFlags = DDSD_PIXELFORMAT | DDSD_CAPS;
1263             sdesc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
1264             sdesc.ddpfPixelFormat.dwSize = sizeof(sdesc.ddpfPixelFormat);
1265             PixelFormat_WineD3DtoDD(&sdesc.ddpfPixelFormat, FormatList[i]);
1266
1267             TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
1268             hr = Callback(&sdesc, Arg);
1269             if(hr != DDENUMRET_OK)
1270             {
1271                 TRACE("Format enumeration cancelled by application\n");
1272                 return D3D_OK;
1273             }
1274         }
1275     }
1276     TRACE("End of enumeration\n");
1277     return D3D_OK;
1278 }
1279
1280 static HRESULT WINAPI
1281 Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats(IDirect3DDevice *iface,
1282                                                LPD3DENUMTEXTUREFORMATSCALLBACK Callback,
1283                                                void *Arg)
1284 {
1285     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1286     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DDevice2 interface.\n", This, Callback, Arg);
1287     return IDirect3DDevice2_EnumTextureFormats(ICOM_INTERFACE(This, IDirect3DDevice2),
1288                                                Callback,
1289                                                Arg);
1290 }
1291
1292 /*****************************************************************************
1293  * IDirect3DDevice::CreateMatrix
1294  *
1295  * Creates a matrix handle. A handle is created and memory for a D3DMATRIX is
1296  * allocated for the handle.
1297  *
1298  * Version 1 only
1299  *
1300  * Params
1301  *  D3DMatHandle: Address to return the handle at
1302  *
1303  * Returns:
1304  *  D3D_OK on success
1305  *  DDERR_INVALIDPARAMS if D3DMatHandle = NULL
1306  *
1307  *****************************************************************************/
1308 static HRESULT WINAPI
1309 IDirect3DDeviceImpl_1_CreateMatrix(IDirect3DDevice *iface, D3DMATRIXHANDLE *D3DMatHandle)
1310 {
1311     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1312     D3DMATRIX *Matrix;
1313     TRACE("(%p)->(%p)\n", This, D3DMatHandle);
1314
1315     if(!D3DMatHandle)
1316         return DDERR_INVALIDPARAMS;
1317
1318     Matrix = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(D3DMATRIX));
1319     if(!Matrix)
1320     {
1321         ERR("Out of memory when allocating a D3DMATRIX\n");
1322         return DDERR_OUTOFMEMORY;
1323     }
1324     *D3DMatHandle = IDirect3DDeviceImpl_CreateHandle(This);
1325     if(!(*D3DMatHandle))
1326     {
1327         ERR("Failed to create a matrix handle\n");
1328         HeapFree(GetProcessHeap(), 0, Matrix);
1329         return DDERR_OUTOFMEMORY;
1330     }
1331     This->Handles[(DWORD) *D3DMatHandle - 1].ptr = Matrix;
1332     This->Handles[(DWORD) *D3DMatHandle - 1].type = DDrawHandle_Matrix;
1333     TRACE(" returning matrix handle %d\n", *D3DMatHandle);
1334
1335     return D3D_OK;
1336 }
1337
1338 /*****************************************************************************
1339  * IDirect3DDevice::SetMatrix
1340  *
1341  * Sets a matrix for a matrix handle. The matrix is copied into the memory
1342  * allocated for the handle
1343  *
1344  * Version 1 only
1345  *
1346  * Params:
1347  *  D3DMatHandle: Handle to set the matrix to
1348  *  D3DMatrix: Matrix to set
1349  *
1350  * Returns:
1351  *  D3D_OK on success
1352  *  DDERR_INVALIDPARAMS if the handle of the matrix is invalid or the matrix
1353  *   to set is NULL
1354  *
1355  *****************************************************************************/
1356 static HRESULT WINAPI
1357 IDirect3DDeviceImpl_1_SetMatrix(IDirect3DDevice *iface,
1358                                 D3DMATRIXHANDLE D3DMatHandle,
1359                                 D3DMATRIX *D3DMatrix)
1360 {
1361     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1362     TRACE("(%p)->(%08x,%p)\n", This, (DWORD) D3DMatHandle, D3DMatrix);
1363
1364     if( (!D3DMatHandle) || (!D3DMatrix) )
1365         return DDERR_INVALIDPARAMS;
1366
1367     if(D3DMatHandle > This->numHandles)
1368     {
1369         ERR("Handle %d out of range\n", D3DMatHandle);
1370         return DDERR_INVALIDPARAMS;
1371     }
1372     else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
1373     {
1374         ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
1375         return DDERR_INVALIDPARAMS;
1376     }
1377
1378     if (TRACE_ON(d3d7))
1379         dump_D3DMATRIX(D3DMatrix);
1380
1381     *((D3DMATRIX *) This->Handles[D3DMatHandle - 1].ptr) = *D3DMatrix;
1382
1383     return D3D_OK;
1384 }
1385
1386 /*****************************************************************************
1387  * IDirect3DDevice::SetMatrix
1388  *
1389  * Returns the content of a D3DMATRIX handle
1390  *
1391  * Version 1 only
1392  *
1393  * Params:
1394  *  D3DMatHandle: Matrix handle to read the content from
1395  *  D3DMatrix: Address to store the content at
1396  *
1397  * Returns:
1398  *  D3D_OK on success
1399  *  DDERR_INVALIDPARAMS if D3DMatHandle is invalid or D3DMatrix is NULL
1400  *
1401  *****************************************************************************/
1402 static HRESULT WINAPI
1403 IDirect3DDeviceImpl_1_GetMatrix(IDirect3DDevice *iface,
1404                                 D3DMATRIXHANDLE D3DMatHandle,
1405                                 D3DMATRIX *D3DMatrix)
1406 {
1407     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1408     TRACE("(%p)->(%08x,%p)\n", This, (DWORD) D3DMatHandle, D3DMatrix);
1409
1410     if(!D3DMatrix)
1411         return DDERR_INVALIDPARAMS;
1412     if(!D3DMatHandle)
1413         return DDERR_INVALIDPARAMS;
1414
1415     if(D3DMatHandle > This->numHandles)
1416     {
1417         ERR("Handle %d out of range\n", D3DMatHandle);
1418         return DDERR_INVALIDPARAMS;
1419     }
1420     else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
1421     {
1422         ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
1423         return DDERR_INVALIDPARAMS;
1424     }
1425
1426     /* The handle is simply a pointer to a D3DMATRIX structure */
1427     *D3DMatrix = *((D3DMATRIX *) This->Handles[D3DMatHandle - 1].ptr);
1428
1429     return D3D_OK;
1430 }
1431
1432 /*****************************************************************************
1433  * IDirect3DDevice::DeleteMatrix
1434  *
1435  * Destroys a Matrix handle. Frees the memory and unsets the handle data
1436  *
1437  * Version 1 only
1438  *
1439  * Params:
1440  *  D3DMatHandle: Handle to destroy
1441  *
1442  * Returns:
1443  *  D3D_OK on success
1444  *  DDERR_INVALIDPARAMS if D3DMatHandle is invalid
1445  *
1446  *****************************************************************************/
1447 static HRESULT WINAPI
1448 IDirect3DDeviceImpl_1_DeleteMatrix(IDirect3DDevice *iface,
1449                                    D3DMATRIXHANDLE D3DMatHandle)
1450 {
1451     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1452     TRACE("(%p)->(%08x)\n", This, (DWORD) D3DMatHandle);
1453
1454     if(!D3DMatHandle)
1455         return DDERR_INVALIDPARAMS;
1456
1457     if(D3DMatHandle > This->numHandles)
1458     {
1459         ERR("Handle %d out of range\n", D3DMatHandle);
1460         return DDERR_INVALIDPARAMS;
1461     }
1462     else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
1463     {
1464         ERR("Handle %d is not a matrix handle\n", D3DMatHandle);
1465         return DDERR_INVALIDPARAMS;
1466     }
1467
1468     HeapFree(GetProcessHeap(), 0, This->Handles[D3DMatHandle - 1].ptr);
1469     This->Handles[D3DMatHandle - 1].ptr = NULL;
1470     This->Handles[D3DMatHandle - 1].type = DDrawHandle_Unknown;
1471
1472     return D3D_OK;
1473 }
1474
1475 /*****************************************************************************
1476  * IDirect3DDevice7::BeginScene
1477  *
1478  * This method must be called before any rendering is performed.
1479  * IDirect3DDevice::EndScene has to be called after the scene is complete
1480  *
1481  * Version 1, 2, 3 and 7
1482  *
1483  * Returns:
1484  *  D3D_OK on success, for details see IWineD3DDevice::BeginScene
1485  *  D3DERR_SCENE_IN_SCENE if WineD3D returns an error(Only in case of an already
1486  *  started scene).
1487  *
1488  *****************************************************************************/
1489 static HRESULT WINAPI
1490 IDirect3DDeviceImpl_7_BeginScene(IDirect3DDevice7 *iface)
1491 {
1492     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1493     HRESULT hr;
1494     TRACE("(%p): Relay\n", This);
1495
1496     hr = IWineD3DDevice_BeginScene(This->wineD3DDevice);
1497     if(hr == WINED3D_OK) return D3D_OK;
1498     else return D3DERR_SCENE_IN_SCENE; /* TODO: Other possible causes of failure */
1499 }
1500
1501 static HRESULT WINAPI
1502 Thunk_IDirect3DDeviceImpl_3_BeginScene(IDirect3DDevice3 *iface)
1503 {
1504     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1505     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1506     return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1507 }
1508
1509 static HRESULT WINAPI
1510 Thunk_IDirect3DDeviceImpl_2_BeginScene(IDirect3DDevice2 *iface)
1511 {
1512     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1513     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1514     return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1515 }
1516
1517 static HRESULT WINAPI
1518 Thunk_IDirect3DDeviceImpl_1_BeginScene(IDirect3DDevice *iface)
1519 {
1520     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1521     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1522     return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1523 }
1524
1525 /*****************************************************************************
1526  * IDirect3DDevice7::EndScene
1527  *
1528  * Ends a scene that has been begun with IDirect3DDevice7::BeginScene.
1529  * This method must be called after rendering is finished.
1530  *
1531  * Version 1, 2, 3 and 7
1532  *
1533  * Returns:
1534  *  D3D_OK on success, for details see IWineD3DDevice::EndScene
1535  *  D3DERR_SCENE_NOT_IN_SCENE is returned if WineD3D returns an error. It does
1536  *  that only if the scene was already ended.
1537  *
1538  *****************************************************************************/
1539 static HRESULT WINAPI
1540 IDirect3DDeviceImpl_7_EndScene(IDirect3DDevice7 *iface)
1541 {
1542     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1543     HRESULT hr;
1544     TRACE("(%p): Relay\n", This);
1545
1546     hr = IWineD3DDevice_EndScene(This->wineD3DDevice);
1547     if(hr == WINED3D_OK) return D3D_OK;
1548     else return D3DERR_SCENE_NOT_IN_SCENE;
1549 }
1550
1551 static HRESULT WINAPI
1552 Thunk_IDirect3DDeviceImpl_3_EndScene(IDirect3DDevice3 *iface)
1553 {
1554     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1555     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1556     return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1557 }
1558
1559 static HRESULT WINAPI
1560 Thunk_IDirect3DDeviceImpl_2_EndScene(IDirect3DDevice2 *iface)
1561 {
1562     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1563     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1564     return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1565 }
1566
1567 static HRESULT WINAPI
1568 Thunk_IDirect3DDeviceImpl_1_EndScene(IDirect3DDevice *iface)
1569 {
1570     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1571     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1572     return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1573 }
1574
1575 /*****************************************************************************
1576  * IDirect3DDevice7::GetDirect3D
1577  *
1578  * Returns the IDirect3D(= interface to the DirectDraw object) used to create
1579  * this device.
1580  *
1581  * Params:
1582  *  Direct3D7: Address to store the interface pointer at
1583  *
1584  * Returns:
1585  *  D3D_OK on success
1586  *  DDERR_INVALIDPARAMS if Direct3D7 == NULL
1587  *
1588  *****************************************************************************/
1589 static HRESULT WINAPI
1590 IDirect3DDeviceImpl_7_GetDirect3D(IDirect3DDevice7 *iface,
1591                                   IDirect3D7 **Direct3D7)
1592 {
1593     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1594     TRACE("(%p)->(%p)\n", This, Direct3D7);
1595
1596     if(!Direct3D7)
1597         return DDERR_INVALIDPARAMS;
1598
1599     *Direct3D7 = ICOM_INTERFACE(This->ddraw, IDirect3D7);
1600     IDirect3D7_AddRef(*Direct3D7);
1601
1602     TRACE(" returning interface %p\n", *Direct3D7);
1603     return D3D_OK;
1604 }
1605
1606 static HRESULT WINAPI
1607 Thunk_IDirect3DDeviceImpl_3_GetDirect3D(IDirect3DDevice3 *iface,
1608                                         IDirect3D3 **Direct3D3)
1609 {
1610     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1611     HRESULT ret;
1612     IDirect3D7 *ret_ptr;
1613
1614     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D3);
1615     ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
1616                                        &ret_ptr);
1617     if(ret != D3D_OK)
1618         return ret;
1619     *Direct3D3 = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D3, ret_ptr);
1620     TRACE(" returning interface %p\n", *Direct3D3);
1621     return D3D_OK;
1622 }
1623
1624 static HRESULT WINAPI
1625 Thunk_IDirect3DDeviceImpl_2_GetDirect3D(IDirect3DDevice2 *iface,
1626                                         IDirect3D2 **Direct3D2)
1627 {
1628     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1629     HRESULT ret;
1630     IDirect3D7 *ret_ptr;
1631
1632     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D2);
1633     ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
1634                                        &ret_ptr);
1635     if(ret != D3D_OK)
1636         return ret;
1637     *Direct3D2 = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D2, ret_ptr);
1638     TRACE(" returning interface %p\n", *Direct3D2);
1639     return D3D_OK;
1640 }
1641
1642 static HRESULT WINAPI
1643 Thunk_IDirect3DDeviceImpl_1_GetDirect3D(IDirect3DDevice *iface,
1644                                         IDirect3D **Direct3D)
1645 {
1646     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1647     HRESULT ret;
1648     IDirect3D7 *ret_ptr;
1649
1650     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D);
1651     ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
1652                                        &ret_ptr);
1653     if(ret != D3D_OK)
1654         return ret;
1655     *Direct3D = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D, ret_ptr);
1656     TRACE(" returning interface %p\n", *Direct3D);
1657     return D3D_OK;
1658 }
1659
1660 /*****************************************************************************
1661  * IDirect3DDevice3::SetCurrentViewport
1662  *
1663  * Sets a Direct3DViewport as the current viewport.
1664  * For the thunks note that all viewport interface versions are equal
1665  *
1666  * Params:
1667  *  Direct3DViewport3: The viewport to set
1668  *
1669  * Version 2 and 3
1670  *
1671  * Returns:
1672  *  D3D_OK on success
1673  *  (Is a NULL viewport valid?)
1674  *
1675  *****************************************************************************/
1676 static HRESULT WINAPI
1677 IDirect3DDeviceImpl_3_SetCurrentViewport(IDirect3DDevice3 *iface,
1678                                          IDirect3DViewport3 *Direct3DViewport3)
1679 {
1680     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1681     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport3);
1682     TRACE("(%p)->(%p)\n", This, Direct3DViewport3);
1683
1684     /* Do nothing if the specified viewport is the same as the current one */
1685     if (This->current_viewport == vp )
1686       return D3D_OK;
1687
1688     /* Should check if the viewport was added or not */
1689
1690     /* Release previous viewport and AddRef the new one */
1691     if (This->current_viewport)
1692     {
1693         TRACE("ViewportImpl is at %p, interface is at %p\n", This->current_viewport, ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3));
1694         IDirect3DViewport3_Release( ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3) );
1695     }
1696     IDirect3DViewport3_AddRef(Direct3DViewport3);
1697
1698     /* Set this viewport as the current viewport */
1699     This->current_viewport = vp;
1700
1701     /* Activate this viewport */
1702     This->current_viewport->active_device = This;
1703     This->current_viewport->activate(This->current_viewport);
1704
1705     return D3D_OK;
1706 }
1707
1708 static HRESULT WINAPI
1709 Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport(IDirect3DDevice2 *iface,
1710                                                IDirect3DViewport2 *Direct3DViewport2)
1711 {
1712     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1713     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
1714     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
1715     return IDirect3DDevice3_SetCurrentViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
1716                                                ICOM_INTERFACE(vp, IDirect3DViewport3));
1717 }
1718
1719 /*****************************************************************************
1720  * IDirect3DDevice3::GetCurrentViewport
1721  *
1722  * Returns the currently active viewport.
1723  *
1724  * Version 2 and 3
1725  *
1726  * Params:
1727  *  Direct3DViewport3: Address to return the interface pointer at
1728  *
1729  * Returns:
1730  *  D3D_OK on success
1731  *  DDERR_INVALIDPARAMS if Direct3DViewport == NULL
1732  *
1733  *****************************************************************************/
1734 static HRESULT WINAPI
1735 IDirect3DDeviceImpl_3_GetCurrentViewport(IDirect3DDevice3 *iface,
1736                                          IDirect3DViewport3 **Direct3DViewport3)
1737 {
1738     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1739     TRACE("(%p)->(%p)\n", This, Direct3DViewport3);
1740
1741     if(!Direct3DViewport3)
1742         return DDERR_INVALIDPARAMS;
1743
1744     *Direct3DViewport3 = ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3);
1745
1746     /* AddRef the returned viewport */
1747     if(*Direct3DViewport3) IDirect3DViewport3_AddRef(*Direct3DViewport3);
1748
1749     TRACE(" returning interface %p\n", *Direct3DViewport3);
1750
1751     return D3D_OK;
1752 }
1753
1754 static HRESULT WINAPI
1755 Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport(IDirect3DDevice2 *iface,
1756                                                IDirect3DViewport2 **Direct3DViewport2)
1757 {
1758     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1759     HRESULT hr;
1760     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Direct3DViewport2);
1761     hr = IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
1762                                             (IDirect3DViewport3 **) Direct3DViewport2);
1763     if(hr != D3D_OK) return hr;
1764     *Direct3DViewport2 = (IDirect3DViewport2 *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, *Direct3DViewport2);
1765     return D3D_OK;
1766 }
1767
1768 /*****************************************************************************
1769  * IDirect3DDevice7::SetRenderTarget
1770  *
1771  * Sets the render target for the Direct3DDevice.
1772  * For the thunks note that IDirectDrawSurface7 == IDirectDrawSurface4 and
1773  * IDirectDrawSurface3 == IDirectDrawSurface
1774  *
1775  * Version 2, 3 and 7
1776  *
1777  * Params:
1778  *  NewTarget: Pointer to an IDirectDrawSurface7 interface to set as the new
1779  *             render target
1780  *  Flags: Some flags
1781  *
1782  * Returns:
1783  *  D3D_OK on success, for details see IWineD3DDevice::SetRenderTarget
1784  *
1785  *****************************************************************************/
1786 static HRESULT WINAPI
1787 IDirect3DDeviceImpl_7_SetRenderTarget(IDirect3DDevice7 *iface,
1788                                       IDirectDrawSurface7 *NewTarget,
1789                                       DWORD Flags)
1790 {
1791     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1792     IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, NewTarget);
1793     HRESULT hr;
1794     TRACE("(%p)->(%p,%08x): Relay\n", This, NewTarget, Flags);
1795
1796     /* Flags: Not used */
1797
1798     hr = IWineD3DDevice_SetRenderTarget(This->wineD3DDevice,
1799                                         0,
1800                                         Target ? Target->WineD3DSurface : NULL);
1801     if(hr != D3D_OK)
1802     {
1803         return hr;
1804     }
1805     IDirectDrawSurface7_AddRef(NewTarget);
1806     IDirectDrawSurface7_Release(ICOM_INTERFACE(This->target, IDirectDrawSurface7));
1807     This->target = Target;
1808     IDirect3DDeviceImpl_UpdateDepthStencil(This);
1809     return D3D_OK;
1810 }
1811
1812 static HRESULT WINAPI
1813 Thunk_IDirect3DDeviceImpl_3_SetRenderTarget(IDirect3DDevice3 *iface,
1814                                             IDirectDrawSurface4 *NewRenderTarget,
1815                                             DWORD Flags)
1816 {
1817     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1818     IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, NewRenderTarget);
1819     TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
1820     return IDirect3DDevice7_SetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1821                                             ICOM_INTERFACE(Target, IDirectDrawSurface7),
1822                                             Flags);
1823 }
1824
1825 static HRESULT WINAPI
1826 Thunk_IDirect3DDeviceImpl_2_SetRenderTarget(IDirect3DDevice2 *iface,
1827                                             IDirectDrawSurface *NewRenderTarget,
1828                                             DWORD Flags)
1829 {
1830     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1831     IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface3, NewRenderTarget);
1832     TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
1833     return IDirect3DDevice7_SetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1834                                             ICOM_INTERFACE(Target, IDirectDrawSurface7),
1835                                             Flags);
1836 }
1837
1838 /*****************************************************************************
1839  * IDirect3DDevice7::GetRenderTarget
1840  *
1841  * Returns the current render target.
1842  * This is handled locally, because the WineD3D render target's parent
1843  * is an IParent
1844  *
1845  * Version 2, 3 and 7
1846  *
1847  * Params:
1848  *  RenderTarget: Address to store the surface interface pointer
1849  *
1850  * Returns:
1851  *  D3D_OK on success
1852  *  DDERR_INVALIDPARAMS if RenderTarget == NULL
1853  *
1854  *****************************************************************************/
1855 static HRESULT WINAPI
1856 IDirect3DDeviceImpl_7_GetRenderTarget(IDirect3DDevice7 *iface,
1857                                       IDirectDrawSurface7 **RenderTarget)
1858 {
1859     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1860     TRACE("(%p)->(%p): Relay\n", This, RenderTarget);
1861
1862     if(!RenderTarget)
1863         return DDERR_INVALIDPARAMS;
1864
1865     *RenderTarget = ICOM_INTERFACE(This->target, IDirectDrawSurface7);
1866     IDirectDrawSurface7_AddRef(*RenderTarget);
1867
1868     return D3D_OK;
1869 }
1870
1871 static HRESULT WINAPI
1872 Thunk_IDirect3DDeviceImpl_3_GetRenderTarget(IDirect3DDevice3 *iface,
1873                                             IDirectDrawSurface4 **RenderTarget)
1874 {
1875     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1876     HRESULT hr;
1877     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
1878     hr = IDirect3DDevice7_GetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1879                                           (IDirectDrawSurface7 **) RenderTarget);
1880     if(hr != D3D_OK) return hr;
1881     *RenderTarget = (IDirectDrawSurface4 *) COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirectDrawSurface7, *RenderTarget);
1882     return D3D_OK;
1883 }
1884
1885 static HRESULT WINAPI
1886 Thunk_IDirect3DDeviceImpl_2_GetRenderTarget(IDirect3DDevice2 *iface,
1887                                             IDirectDrawSurface **RenderTarget)
1888 {
1889     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1890     HRESULT hr;
1891     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
1892     hr = IDirect3DDevice7_GetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1893                                           (IDirectDrawSurface7 **) RenderTarget);
1894     if(hr != D3D_OK) return hr;
1895     *RenderTarget = (IDirectDrawSurface *) COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirectDrawSurface3, *RenderTarget);
1896     return D3D_OK;
1897 }
1898
1899 /*****************************************************************************
1900  * IDirect3DDevice3::Begin
1901  *
1902  * Begins a description block of vertices. This is similar to glBegin()
1903  * and glEnd(). After a call to IDirect3DDevice3::End, the vertices
1904  * described with IDirect3DDevice::Vertex are drawn.
1905  *
1906  * Version 2 and 3
1907  *
1908  * Params:
1909  *  PrimitiveType: The type of primitives to draw
1910  *  VertexTypeDesc: A flexible vertex format description of the vertices
1911  *  Flags: Some flags..
1912  *
1913  * Returns:
1914  *  D3D_OK on success
1915  *
1916  *****************************************************************************/
1917 static HRESULT WINAPI
1918 IDirect3DDeviceImpl_3_Begin(IDirect3DDevice3 *iface,
1919                             D3DPRIMITIVETYPE PrimitiveType,
1920                             DWORD VertexTypeDesc,
1921                             DWORD Flags)
1922 {
1923     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1924     TRACE("(%p)->(%d,%d,%08x)\n", This, PrimitiveType, VertexTypeDesc, Flags);
1925
1926     This->primitive_type = PrimitiveType;
1927     This->vertex_type = VertexTypeDesc;
1928     This->render_flags = Flags;
1929     This->vertex_size = get_flexible_vertex_size(This->vertex_type);
1930     This->nb_vertices = 0;
1931
1932     return D3D_OK;
1933 }
1934
1935 static HRESULT WINAPI
1936 Thunk_IDirect3DDeviceImpl_2_Begin(IDirect3DDevice2 *iface,
1937                                   D3DPRIMITIVETYPE d3dpt,
1938                                   D3DVERTEXTYPE dwVertexTypeDesc,
1939                                   DWORD dwFlags)
1940 {
1941     DWORD FVF;
1942     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1943     TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dpt, dwVertexTypeDesc, dwFlags);
1944
1945     switch(dwVertexTypeDesc)
1946     {
1947         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
1948         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
1949         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
1950         default:
1951             ERR("Unexpected vertex type %d\n", dwVertexTypeDesc);
1952             return DDERR_INVALIDPARAMS;  /* Should never happen */
1953     };
1954
1955     return IDirect3DDevice3_Begin(ICOM_INTERFACE(This, IDirect3DDevice3),
1956                                   d3dpt,
1957                                   FVF,
1958                                   dwFlags);
1959 }
1960
1961 /*****************************************************************************
1962  * IDirect3DDevice3::BeginIndexed
1963  *
1964  * Draws primitives based on vertices in a vertex array which are specified
1965  * by indices.
1966  *
1967  * Version 2 and 3
1968  *
1969  * Params:
1970  *  PrimitiveType: Primitive type to draw
1971  *  VertexType: A FVF description of the vertex format
1972  *  Vertices: pointer to an array containing the vertices
1973  *  NumVertices: The number of vertices in the vertex array
1974  *  Flags: Some flags ...
1975  *
1976  * Returns:
1977  *  D3D_OK, because it's a stub
1978  *
1979  *****************************************************************************/
1980 static HRESULT WINAPI
1981 IDirect3DDeviceImpl_3_BeginIndexed(IDirect3DDevice3 *iface,
1982                                    D3DPRIMITIVETYPE PrimitiveType,
1983                                    DWORD VertexType,
1984                                    void *Vertices,
1985                                    DWORD NumVertices,
1986                                    DWORD Flags)
1987 {
1988     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1989     FIXME("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, Vertices, NumVertices, Flags);
1990     return D3D_OK;
1991 }
1992
1993
1994 static HRESULT WINAPI
1995 Thunk_IDirect3DDeviceImpl_2_BeginIndexed(IDirect3DDevice2 *iface,
1996                                          D3DPRIMITIVETYPE d3dptPrimitiveType,
1997                                          D3DVERTEXTYPE d3dvtVertexType,
1998                                          void *lpvVertices,
1999                                          DWORD dwNumVertices,
2000                                          DWORD dwFlags)
2001 {
2002     DWORD FVF;
2003     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2004     TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%p,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dptPrimitiveType, d3dvtVertexType, lpvVertices, dwNumVertices, dwFlags);
2005
2006     switch(d3dvtVertexType)
2007     {
2008         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
2009         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
2010         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
2011         default:
2012             ERR("Unexpected vertex type %d\n", d3dvtVertexType);
2013             return DDERR_INVALIDPARAMS;  /* Should never happen */
2014     };
2015
2016     return IDirect3DDevice3_BeginIndexed(ICOM_INTERFACE(This,IDirect3DDevice3),
2017                                          d3dptPrimitiveType,
2018                                          FVF,
2019                                          lpvVertices,
2020                                          dwNumVertices,
2021                                          dwFlags);
2022 }
2023
2024 /*****************************************************************************
2025  * IDirect3DDevice3::Vertex
2026  *
2027  * Draws a vertex as described by IDirect3DDevice3::Begin. It places all
2028  * drawn vertices in a vertex buffer. If the buffer is too small, its
2029  * size is increased.
2030  *
2031  * Version 2 and 3
2032  *
2033  * Params:
2034  *  Vertex: Pointer to the vertex
2035  *
2036  * Returns:
2037  *  D3D_OK, on success
2038  *  DDERR_INVALIDPARAMS if Vertex is NULL
2039  *
2040  *****************************************************************************/
2041 static HRESULT WINAPI
2042 IDirect3DDeviceImpl_3_Vertex(IDirect3DDevice3 *iface,
2043                              void *Vertex)
2044 {
2045     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2046     TRACE("(%p)->(%p)\n", This, Vertex);
2047
2048     if(!Vertex)
2049         return DDERR_INVALIDPARAMS;
2050
2051     if ((This->nb_vertices+1)*This->vertex_size > This->buffer_size)
2052     {
2053         BYTE *old_buffer;
2054         This->buffer_size = This->buffer_size ? This->buffer_size * 2 : This->vertex_size * 3;
2055         old_buffer = This->vertex_buffer;
2056         This->vertex_buffer = HeapAlloc(GetProcessHeap(), 0, This->buffer_size);
2057         if (old_buffer)
2058         {
2059             CopyMemory(This->vertex_buffer, old_buffer, This->nb_vertices * This->vertex_size);
2060             HeapFree(GetProcessHeap(), 0, old_buffer);
2061         }
2062     }
2063
2064     CopyMemory(This->vertex_buffer + This->nb_vertices++ * This->vertex_size, Vertex, This->vertex_size);
2065
2066     return D3D_OK;
2067 }
2068
2069 static HRESULT WINAPI
2070 Thunk_IDirect3DDeviceImpl_2_Vertex(IDirect3DDevice2 *iface,
2071                                    void *lpVertexType)
2072 {
2073     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2074     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, lpVertexType);
2075     return IDirect3DDevice3_Vertex(ICOM_INTERFACE(This, IDirect3DDevice3),
2076                                                   lpVertexType);
2077 }
2078
2079 /*****************************************************************************
2080  * IDirect3DDevice3::Index
2081  *
2082  * Specifies an index to a vertex to be drawn. The vertex array has to
2083  * be specified with BeginIndexed first.
2084  *
2085  * Parameters:
2086  *  VertexIndex: The index of the vertex to draw
2087  *
2088  * Returns:
2089  *  D3D_OK because it's a stub
2090  *
2091  *****************************************************************************/
2092 static HRESULT WINAPI
2093 IDirect3DDeviceImpl_3_Index(IDirect3DDevice3 *iface,
2094                             WORD VertexIndex)
2095 {
2096     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2097     FIXME("(%p)->(%04x): stub!\n", This, VertexIndex);
2098     return D3D_OK;
2099 }
2100
2101 static HRESULT WINAPI
2102 Thunk_IDirect3DDeviceImpl_2_Index(IDirect3DDevice2 *iface,
2103                                   WORD wVertexIndex)
2104 {
2105     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2106     TRACE_(ddraw_thunk)("(%p)->(%04x) thunking to IDirect3DDevice3 interface.\n", This, wVertexIndex);
2107     return IDirect3DDevice3_Index(ICOM_INTERFACE(This, IDirect3DDevice3),
2108                                   wVertexIndex);
2109 }
2110
2111 /*****************************************************************************
2112  * IDirect3DDevice3::End
2113  *
2114  * Ends a draw begun with IDirect3DDevice3::Begin or
2115  * IDirect3DDevice::BeginIndexed. The vertices specified with
2116  * IDirect3DDevice::Vertex or IDirect3DDevice::Index are drawn using
2117  * the IDirect3DDevice7::DrawPrimitive method. So far only
2118  * non-indexed mode is supported
2119  *
2120  * Version 2 and 3
2121  *
2122  * Params:
2123  *  Flags: Some flags, as usual. Don't know which are defined
2124  *
2125  * Returns:
2126  *  The return value of IDirect3DDevice7::DrawPrimitive
2127  *
2128  *****************************************************************************/
2129 static HRESULT WINAPI
2130 IDirect3DDeviceImpl_3_End(IDirect3DDevice3 *iface,
2131                           DWORD Flags)
2132 {
2133     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2134     TRACE("(%p)->(%08x)\n", This, Flags);
2135
2136     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2137                                           This->primitive_type, This->vertex_type,
2138                                           This->vertex_buffer, This->nb_vertices,
2139                                           This->render_flags);
2140 }
2141
2142 static HRESULT WINAPI
2143 Thunk_IDirect3DDeviceImpl_2_End(IDirect3DDevice2 *iface,
2144                                 DWORD dwFlags)
2145 {
2146     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2147     TRACE_(ddraw_thunk)("(%p)->(%08x) thunking to IDirect3DDevice3 interface.\n", This, dwFlags);
2148     return IDirect3DDevice3_End(ICOM_INTERFACE(This, IDirect3DDevice3),
2149                                 dwFlags);
2150 }
2151
2152 /*****************************************************************************
2153  * IDirect3DDevice7::GetRenderState
2154  *
2155  * Returns the value of a render state. The possible render states are
2156  * defined in include/d3dtypes.h
2157  *
2158  * Version 2, 3 and 7
2159  *
2160  * Params:
2161  *  RenderStateType: Render state to return the current setting of
2162  *  Value: Address to store the value at
2163  *
2164  * Returns:
2165  *  D3D_OK on success, for details see IWineD3DDevice::GetRenderState
2166  *  DDERR_INVALIDPARAMS if Value == NULL
2167  *
2168  *****************************************************************************/
2169 static HRESULT WINAPI
2170 IDirect3DDeviceImpl_7_GetRenderState(IDirect3DDevice7 *iface,
2171                                      D3DRENDERSTATETYPE RenderStateType,
2172                                      DWORD *Value)
2173 {
2174     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2175     HRESULT hr;
2176     TRACE("(%p)->(%08x,%p): Relay\n", This, RenderStateType, Value);
2177
2178     if(!Value)
2179         return DDERR_INVALIDPARAMS;
2180
2181     switch(RenderStateType)
2182     {
2183         case D3DRENDERSTATE_TEXTUREHANDLE:
2184         {
2185             /* This state is wrapped to SetTexture in SetRenderState, so
2186              * it has to be wrapped to GetTexture here
2187              */
2188             IWineD3DBaseTexture *tex = NULL;
2189             *Value = 0;
2190
2191             hr = IWineD3DDevice_GetTexture(This->wineD3DDevice,
2192                                            0,
2193                                            &tex);
2194
2195             if(hr == WINED3D_OK && tex)
2196             {
2197                 IDirectDrawSurface7 *parent = NULL;
2198                 hr = IWineD3DBaseTexture_GetParent(tex,
2199                                                    (IUnknown **) &parent);
2200                 if(parent)
2201                 {
2202                     /* The parent of the texture is the IDirectDrawSurface7 interface
2203                      * of the ddraw surface
2204                      */
2205                     IDirectDrawSurfaceImpl *texImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
2206                                                                   IDirectDrawSurface7,
2207                                                                   parent);
2208                     *Value = texImpl->Handle;
2209                     IDirectDrawSurface7_Release(parent);
2210                 }
2211                 IWineD3DBaseTexture_Release(tex);
2212             }
2213             return hr;
2214         }
2215
2216         case D3DRENDERSTATE_TEXTUREMAG:
2217         {
2218             WINED3DTEXTUREFILTERTYPE tex_mag;
2219
2220             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2221                                                 0, WINED3DSAMP_MAGFILTER,
2222                                                 &tex_mag);
2223
2224             switch (tex_mag)
2225             {
2226                 case WINED3DTEXF_POINT:
2227                     *Value = D3DFILTER_NEAREST;
2228                     break;
2229                 case WINED3DTEXF_LINEAR:
2230                     *Value = D3DFILTER_LINEAR;
2231                     break;
2232                 default:
2233                     ERR("Unhandled texture mag %d !\n",tex_mag);
2234                     *Value = 0;
2235             }
2236             return hr;
2237         }
2238
2239         case D3DRENDERSTATE_TEXTUREMIN:
2240         {
2241             WINED3DTEXTUREFILTERTYPE tex_min;
2242
2243             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2244                                                 0, WINED3DSAMP_MINFILTER,
2245                                                 &tex_min);
2246
2247             switch (tex_min)
2248             {
2249                 case WINED3DTEXF_POINT:
2250                     *Value = D3DFILTER_NEAREST;
2251                     break;
2252                 case WINED3DTEXF_LINEAR:
2253                     *Value = D3DFILTER_LINEAR;
2254                     break;
2255                 default:
2256                     ERR("Unhandled texture mag %d !\n",tex_min);
2257                     *Value = 0;
2258             }
2259             return hr;
2260         }
2261
2262         case D3DRENDERSTATE_TEXTUREADDRESS:
2263         case D3DRENDERSTATE_TEXTUREADDRESSU:
2264             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2265                                                   0, WINED3DSAMP_ADDRESSU,
2266                                                   Value);
2267         case D3DRENDERSTATE_TEXTUREADDRESSV:
2268             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2269                                                   0, WINED3DSAMP_ADDRESSV,
2270                                                   Value);
2271
2272         default:
2273             /* FIXME: Unhandled: D3DRENDERSTATE_STIPPLEPATTERN00 - 31 */
2274             return IWineD3DDevice_GetRenderState(This->wineD3DDevice,
2275                                                  RenderStateType,
2276                                                  Value);
2277     }
2278 }
2279
2280 static HRESULT WINAPI
2281 Thunk_IDirect3DDeviceImpl_3_GetRenderState(IDirect3DDevice3 *iface,
2282                                            D3DRENDERSTATETYPE dwRenderStateType,
2283                                            DWORD *lpdwRenderState)
2284 {
2285     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2286     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
2287     return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2288                                            dwRenderStateType,
2289                                            lpdwRenderState);
2290 }
2291
2292 static HRESULT WINAPI
2293 Thunk_IDirect3DDeviceImpl_2_GetRenderState(IDirect3DDevice2 *iface,
2294                                            D3DRENDERSTATETYPE dwRenderStateType,
2295                                            DWORD *lpdwRenderState)
2296 {
2297     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2298     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
2299     return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2300                                            dwRenderStateType,
2301                                            lpdwRenderState);
2302 }
2303
2304 /*****************************************************************************
2305  * IDirect3DDevice7::SetRenderState
2306  *
2307  * Sets a render state. The possible render states are defined in
2308  * include/d3dtypes.h
2309  *
2310  * Version 2, 3 and 7
2311  *
2312  * Params:
2313  *  RenderStateType: State to set
2314  *  Value: Value to assign to that state
2315  *
2316  * Returns:
2317  *  D3D_OK on success,
2318  *  for details see IWineD3DDevice::SetRenderState
2319  *
2320  *****************************************************************************/
2321 static HRESULT WINAPI
2322 IDirect3DDeviceImpl_7_SetRenderState(IDirect3DDevice7 *iface,
2323                                      D3DRENDERSTATETYPE RenderStateType,
2324                                      DWORD Value)
2325 {
2326     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2327     TRACE("(%p)->(%08x,%d): Relay\n", This, RenderStateType, Value);
2328
2329     /* Some render states need special care */
2330     switch(RenderStateType)
2331     {
2332         case D3DRENDERSTATE_TEXTUREHANDLE:
2333         {
2334             if(Value == 0)
2335             {
2336                     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
2337                                                      0,
2338                                                      NULL);
2339             }
2340
2341             if(Value > This->numHandles)
2342             {
2343                 FIXME("Specified handle %d out of range\n", Value);
2344                 return DDERR_INVALIDPARAMS;
2345             }
2346             if(This->Handles[Value - 1].type != DDrawHandle_Texture)
2347             {
2348                 FIXME("Handle %d isn't a texture handle\n", Value);
2349                 return DDERR_INVALIDPARAMS;
2350             }
2351             else
2352             {
2353                 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[Value - 1].ptr;
2354                 return IWineD3DDevice_SetTexture(This->wineD3DDevice,
2355                                                  0,
2356                                                  surf->wineD3DTexture);
2357             }
2358         }
2359
2360         case D3DRENDERSTATE_TEXTUREMAG:
2361         {
2362             WINED3DTEXTUREFILTERTYPE tex_mag = WINED3DTEXF_NONE;
2363
2364             switch ((D3DTEXTUREFILTER) Value)
2365             {
2366                 case D3DFILTER_NEAREST:
2367                 case D3DFILTER_LINEARMIPNEAREST:
2368                     tex_mag = WINED3DTEXF_POINT;
2369                     break;
2370                 case D3DFILTER_LINEAR:
2371                 case D3DFILTER_LINEARMIPLINEAR:
2372                     tex_mag = WINED3DTEXF_LINEAR;
2373                     break;
2374                 default:
2375                     ERR("Unhandled texture mag %d !\n",Value);
2376             }
2377
2378             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2379                                                   0, WINED3DSAMP_MAGFILTER,
2380                                                   tex_mag);
2381         }
2382
2383         case D3DRENDERSTATE_TEXTUREMIN:
2384         {
2385             WINED3DTEXTUREFILTERTYPE tex_min = WINED3DTEXF_NONE;
2386             WINED3DTEXTUREFILTERTYPE tex_mip = WINED3DTEXF_NONE;
2387
2388             switch ((D3DTEXTUREFILTER) Value)
2389             {
2390                 case D3DFILTER_NEAREST:
2391                     tex_min = WINED3DTEXF_POINT;
2392                     break;
2393                 case D3DFILTER_LINEAR:
2394                     tex_min = WINED3DTEXF_LINEAR;
2395                     break;
2396                 case D3DFILTER_MIPNEAREST:
2397                     tex_min = WINED3DTEXF_NONE;
2398                     tex_mip = WINED3DTEXF_POINT;
2399                     break;
2400                 case D3DFILTER_MIPLINEAR:
2401                     tex_min = WINED3DTEXF_NONE;
2402                     tex_mip = WINED3DTEXF_LINEAR;
2403                     break;
2404                 case D3DFILTER_LINEARMIPNEAREST:
2405                     tex_min = WINED3DTEXF_POINT;
2406                     tex_mip = WINED3DTEXF_LINEAR;
2407                     break;
2408                 case D3DFILTER_LINEARMIPLINEAR:
2409                     tex_min = WINED3DTEXF_LINEAR;
2410                     tex_mip = WINED3DTEXF_LINEAR;
2411                     break;
2412
2413                 default:
2414                     ERR("Unhandled texture min %d !\n",Value);
2415             }
2416
2417                    IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2418                                                   0, WINED3DSAMP_MIPFILTER,
2419                                                   tex_mip);
2420             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2421                                                   0, WINED3DSAMP_MINFILTER,
2422                                                   tex_min);
2423         }
2424
2425         case D3DRENDERSTATE_TEXTUREADDRESS:
2426                    IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2427                                                   0, WINED3DSAMP_ADDRESSV,
2428                                                   Value);
2429             /* Drop through */
2430         case D3DRENDERSTATE_TEXTUREADDRESSU:
2431             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2432                                                   0, WINED3DSAMP_ADDRESSU,
2433                                                   Value);
2434         case D3DRENDERSTATE_TEXTUREADDRESSV:
2435             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2436                                                   0, WINED3DSAMP_ADDRESSV,
2437                                                   Value);
2438
2439         case D3DRENDERSTATE_TEXTUREMAPBLEND:
2440         {
2441             /* Old texture combine setup style, superseded by texture stage states
2442              * in D3D7. It is safe for us to wrap it to texture stage states.
2443              */
2444             switch ( (D3DTEXTUREBLEND) Value)
2445             {
2446                 case D3DTBLEND_MODULATE:
2447                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2448                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2449                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG2, WINED3DTA_CURRENT);
2450                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_MODULATE);
2451                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_SELECTARG1);
2452                     break;
2453
2454                 case D3DTBLEND_MODULATEALPHA:
2455                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2456                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2457                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG2, WINED3DTA_CURRENT);
2458                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG2, WINED3DTA_CURRENT);
2459                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_MODULATE);
2460                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_MODULATE);
2461                     break;
2462
2463                 case D3DTBLEND_DECAL:
2464                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2465                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2466                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_SELECTARG1);
2467                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_SELECTARG1);
2468                     break;
2469
2470                 case D3DTBLEND_DECALALPHA:
2471                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2472                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2473                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG2, WINED3DTA_CURRENT);
2474                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_SELECTARG1);
2475                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_MODULATE);
2476                     break;
2477
2478                 default:
2479                     ERR("Unhandled texture environment %d !\n",Value);
2480                 }
2481                 return D3D_OK;
2482             break;
2483         }
2484
2485         default:
2486
2487             /* FIXME: Unhandled: D3DRENDERSTATE_STIPPLEPATTERN00 - 31 */
2488
2489             return IWineD3DDevice_SetRenderState(This->wineD3DDevice,
2490                                                  RenderStateType,
2491                                                  Value);
2492     }
2493 }
2494
2495 static HRESULT WINAPI
2496 Thunk_IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
2497                                            D3DRENDERSTATETYPE RenderStateType,
2498                                            DWORD Value)
2499 {
2500     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2501     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2502     return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2503                                            RenderStateType,
2504                                            Value);
2505 }
2506
2507 static HRESULT WINAPI
2508 Thunk_IDirect3DDeviceImpl_2_SetRenderState(IDirect3DDevice2 *iface,
2509                                            D3DRENDERSTATETYPE RenderStateType,
2510                                            DWORD Value)
2511 {
2512     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2513     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2514     return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2515                                            RenderStateType,
2516                                            Value);
2517 }
2518
2519 /*****************************************************************************
2520  * Direct3DDevice3::SetLightState
2521  *
2522  * Sets a light state for Direct3DDevice3 and Direct3DDevice2. The
2523  * light states are forwarded to Direct3DDevice7 render states
2524  *
2525  * Version 2 and 3
2526  *
2527  * Params:
2528  *  LightStateType: The light state to change
2529  *  Value: The value to assign to that light state
2530  *
2531  * Returns:
2532  *  D3D_OK on success
2533  *  DDERR_INVALIDPARAMS if the parameters were incorrect
2534  *  Also check IDirect3DDevice7::SetRenderState
2535  *
2536  *****************************************************************************/
2537 static HRESULT WINAPI
2538 IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
2539                                     D3DLIGHTSTATETYPE LightStateType,
2540                                     DWORD Value)
2541 {
2542     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2543
2544     TRACE("(%p)->(%08x,%08x)\n", This, LightStateType, Value);
2545
2546     if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
2547     {
2548         TRACE("Unexpected Light State Type\n");
2549         return DDERR_INVALIDPARAMS;
2550     }
2551
2552     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
2553     {
2554         IDirect3DMaterialImpl *mat;
2555
2556         if(Value == 0) mat = NULL;
2557         else if(Value > This->numHandles)
2558         {
2559             ERR("Material handle out of range(%d)\n", Value);
2560             return DDERR_INVALIDPARAMS;
2561         }
2562         else if(This->Handles[Value - 1].type != DDrawHandle_Material)
2563         {
2564             ERR("Invalid handle %d\n", Value);
2565             return DDERR_INVALIDPARAMS;
2566         }
2567         else
2568         {
2569             mat = (IDirect3DMaterialImpl *) This->Handles[Value - 1].ptr;
2570         }
2571
2572         if (mat != NULL)
2573         {
2574             TRACE(" activating material %p.\n", mat);
2575             mat->activate(mat);
2576         }
2577         else
2578         {
2579             FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
2580         }
2581         This->material = Value;
2582     }
2583     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
2584     {
2585         switch (Value)
2586         {
2587             case D3DCOLOR_MONO:
2588                 ERR("DDCOLOR_MONO should not happen!\n");
2589                 break;
2590             case D3DCOLOR_RGB:
2591                 /* We are already in this mode */
2592                 TRACE("Setting color model to RGB (no-op).\n");
2593                 break;
2594             default:
2595                 ERR("Unknown color model!\n");
2596                 return DDERR_INVALIDPARAMS;
2597         }
2598     }
2599     else
2600     {
2601         D3DRENDERSTATETYPE rs;
2602         switch (LightStateType)
2603         {
2604             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
2605                 rs = D3DRENDERSTATE_AMBIENT;
2606                 break;          
2607             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
2608                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2609                 break;
2610             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
2611                 rs = D3DRENDERSTATE_FOGSTART;
2612                 break;
2613             case D3DLIGHTSTATE_FOGEND:        /* 6 */
2614                 rs = D3DRENDERSTATE_FOGEND;
2615                 break;
2616             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
2617                 rs = D3DRENDERSTATE_FOGDENSITY;
2618                 break;
2619             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
2620                 rs = D3DRENDERSTATE_COLORVERTEX;
2621                 break;
2622             default:
2623                 ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2624                 return DDERR_INVALIDPARAMS;
2625         }
2626
2627         return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7), 
2628                                                rs,
2629                                                Value);
2630     }
2631
2632     return D3D_OK;
2633 }
2634
2635 static HRESULT WINAPI
2636 Thunk_IDirect3DDeviceImpl_2_SetLightState(IDirect3DDevice2 *iface,
2637                                           D3DLIGHTSTATETYPE LightStateType,
2638                                           DWORD Value)
2639 {
2640     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2641     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2642     return IDirect3DDevice3_SetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
2643                                           LightStateType,
2644                                           Value);
2645 }
2646
2647 /*****************************************************************************
2648  * IDirect3DDevice3::GetLightState
2649  *
2650  * Returns the current setting of a light state. The state is read from
2651  * the Direct3DDevice7 render state.
2652  *
2653  * Version 2 and 3
2654  *
2655  * Params:
2656  *  LightStateType: The light state to return
2657  *  Value: The address to store the light state setting at
2658  *
2659  * Returns:
2660  *  D3D_OK on success
2661  *  DDDERR_INVALIDPARAMS if the parameters were incorrect
2662  *  Also see IDirect3DDevice7::GetRenderState
2663  *
2664  *****************************************************************************/
2665 static HRESULT WINAPI
2666 IDirect3DDeviceImpl_3_GetLightState(IDirect3DDevice3 *iface,
2667                                     D3DLIGHTSTATETYPE LightStateType,
2668                                     DWORD *Value)
2669 {
2670     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2671
2672     TRACE("(%p)->(%08x,%p)\n", This, LightStateType, Value);
2673
2674     if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
2675     {
2676         TRACE("Unexpected Light State Type\n");
2677         return DDERR_INVALIDPARAMS;
2678     }
2679
2680     if(!Value)
2681         return DDERR_INVALIDPARAMS;
2682
2683     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
2684     {
2685         *Value = This->material;
2686     }
2687     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
2688     {
2689         *Value = D3DCOLOR_RGB;
2690     }
2691     else
2692     {
2693         D3DRENDERSTATETYPE rs;
2694         switch (LightStateType)
2695         {
2696             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
2697                 rs = D3DRENDERSTATE_AMBIENT;
2698                 break;          
2699             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
2700                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2701                 break;
2702             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
2703                 rs = D3DRENDERSTATE_FOGSTART;
2704                 break;
2705             case D3DLIGHTSTATE_FOGEND:        /* 6 */
2706                 rs = D3DRENDERSTATE_FOGEND;
2707                 break;
2708             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
2709                 rs = D3DRENDERSTATE_FOGDENSITY;
2710                 break;
2711             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
2712                 rs = D3DRENDERSTATE_COLORVERTEX;
2713                 break;
2714             default:
2715                 ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2716                 return DDERR_INVALIDPARAMS;
2717         }
2718
2719         return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2720                                                rs,
2721                                                Value);
2722     }
2723
2724     return D3D_OK;
2725 }
2726
2727 static HRESULT WINAPI
2728 Thunk_IDirect3DDeviceImpl_2_GetLightState(IDirect3DDevice2 *iface,
2729                                           D3DLIGHTSTATETYPE LightStateType,
2730                                           DWORD *Value)
2731 {
2732     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2733     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2734     return IDirect3DDevice3_GetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
2735                                           LightStateType,
2736                                           Value);
2737 }
2738
2739 /*****************************************************************************
2740  * IDirect3DDevice7::SetTransform
2741  *
2742  * Assigns a D3DMATRIX to a transform type. The transform types are defined
2743  * in include/d3dtypes.h.
2744  * The D3DTRANSFORMSTATE_WORLD (=1) is translated to D3DTS_WORLDMATRIX(0)
2745  * (=255) for wined3d, because the 1 transform state was removed in d3d8
2746  * and WineD3D already understands the replacement D3DTS_WORLDMATRIX(0)
2747  *
2748  * Version 2, 3 and 7
2749  *
2750  * Params:
2751  *  TransformStateType: transform state to set
2752  *  Matrix: Matrix to assign to the state
2753  *
2754  * Returns:
2755  *  D3D_OK on success
2756  *  DDERR_INVALIDPARAMS if Matrix == NULL
2757  *  For details see IWineD3DDevice::SetTransform
2758  *
2759  *****************************************************************************/
2760 static HRESULT WINAPI
2761 IDirect3DDeviceImpl_7_SetTransform(IDirect3DDevice7 *iface,
2762                                    D3DTRANSFORMSTATETYPE TransformStateType,
2763                                    D3DMATRIX *Matrix)
2764 {
2765     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2766     D3DTRANSFORMSTATETYPE type = TransformStateType;
2767     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2768
2769     if(!Matrix)
2770         return DDERR_INVALIDPARAMS;
2771
2772     switch(TransformStateType)
2773     {
2774         case D3DTRANSFORMSTATE_WORLD :  type = WINED3DTS_WORLDMATRIX(0); break;
2775         case D3DTRANSFORMSTATE_WORLD1:  type = WINED3DTS_WORLDMATRIX(1); break;
2776         case D3DTRANSFORMSTATE_WORLD2:  type = WINED3DTS_WORLDMATRIX(2); break;
2777         case D3DTRANSFORMSTATE_WORLD3:  type = WINED3DTS_WORLDMATRIX(3); break;
2778         default:                        type = TransformStateType;
2779     }
2780
2781     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2782     return IWineD3DDevice_SetTransform(This->wineD3DDevice,
2783                                        type,
2784                                        (WINED3DMATRIX*) Matrix);
2785 }
2786
2787 static HRESULT WINAPI
2788 Thunk_IDirect3DDeviceImpl_3_SetTransform(IDirect3DDevice3 *iface,
2789                                          D3DTRANSFORMSTATETYPE TransformStateType,
2790                                          D3DMATRIX *D3DMatrix)
2791 {
2792     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2793     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2794     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2795                                          TransformStateType,
2796                                          D3DMatrix);
2797 }
2798
2799 static HRESULT WINAPI
2800 Thunk_IDirect3DDeviceImpl_2_SetTransform(IDirect3DDevice2 *iface,
2801                                          D3DTRANSFORMSTATETYPE TransformStateType,
2802                                          D3DMATRIX *D3DMatrix)
2803 {
2804     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2805     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2806     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2807                                          TransformStateType,
2808                                          D3DMatrix);
2809 }
2810
2811 /*****************************************************************************
2812  * IDirect3DDevice7::GetTransform
2813  *
2814  * Returns the matrix assigned to a transform state
2815  * D3DTRANSFORMSTATE_WORLD is translated to D3DTS_WORLDMATRIX(0), see
2816  * SetTransform
2817  *
2818  * Params:
2819  *  TransformStateType: State to read the matrix from
2820  *  Matrix: Address to store the matrix at
2821  *
2822  * Returns:
2823  *  D3D_OK on success
2824  *  DDERR_INVALIDPARAMS if Matrix == NULL
2825  *  For details, see IWineD3DDevice::GetTransform
2826  *
2827  *****************************************************************************/
2828 static HRESULT WINAPI
2829 IDirect3DDeviceImpl_7_GetTransform(IDirect3DDevice7 *iface,
2830                                    D3DTRANSFORMSTATETYPE TransformStateType,
2831                                    D3DMATRIX *Matrix)
2832 {
2833     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2834     D3DTRANSFORMSTATETYPE type = TransformStateType;
2835     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2836
2837     if(!Matrix)
2838         return DDERR_INVALIDPARAMS;
2839
2840     switch(TransformStateType)
2841     {
2842         case D3DTRANSFORMSTATE_WORLD :  type = WINED3DTS_WORLDMATRIX(0); break;
2843         case D3DTRANSFORMSTATE_WORLD1:  type = WINED3DTS_WORLDMATRIX(1); break;
2844         case D3DTRANSFORMSTATE_WORLD2:  type = WINED3DTS_WORLDMATRIX(2); break;
2845         case D3DTRANSFORMSTATE_WORLD3:  type = WINED3DTS_WORLDMATRIX(3); break;
2846         default:                        type = TransformStateType;
2847     }
2848
2849     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2850     return IWineD3DDevice_GetTransform(This->wineD3DDevice, type, (WINED3DMATRIX*) Matrix);
2851 }
2852
2853 static HRESULT WINAPI
2854 Thunk_IDirect3DDeviceImpl_3_GetTransform(IDirect3DDevice3 *iface,
2855                                          D3DTRANSFORMSTATETYPE TransformStateType,
2856                                          D3DMATRIX *D3DMatrix)
2857 {
2858     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2859     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2860     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2861                                          TransformStateType,
2862                                          D3DMatrix);
2863 }
2864
2865 static HRESULT WINAPI
2866 Thunk_IDirect3DDeviceImpl_2_GetTransform(IDirect3DDevice2 *iface,
2867                                          D3DTRANSFORMSTATETYPE TransformStateType,
2868                                          D3DMATRIX *D3DMatrix)
2869 {
2870     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2871     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2872     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2873                                          TransformStateType,
2874                                          D3DMatrix);
2875 }
2876
2877 /*****************************************************************************
2878  * IDirect3DDevice7::MultiplyTransform
2879  *
2880  * Multiplies the already-set transform matrix of a transform state
2881  * with another matrix. For the world matrix, see SetTransform
2882  *
2883  * Version 2, 3 and 7
2884  *
2885  * Params:
2886  *  TransformStateType: Transform state to multiply
2887  *  D3DMatrix Matrix to multiply with.
2888  *
2889  * Returns
2890  *  D3D_OK on success
2891  *  DDERR_INVALIDPARAMS if D3DMatrix is NULL
2892  *  For details, see IWineD3DDevice::MultiplyTransform
2893  *
2894  *****************************************************************************/
2895 static HRESULT WINAPI
2896 IDirect3DDeviceImpl_7_MultiplyTransform(IDirect3DDevice7 *iface,
2897                                         D3DTRANSFORMSTATETYPE TransformStateType,
2898                                         D3DMATRIX *D3DMatrix)
2899 {
2900     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2901     D3DTRANSFORMSTATETYPE type;
2902     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, D3DMatrix);
2903
2904     switch(TransformStateType)
2905     {
2906         case D3DTRANSFORMSTATE_WORLD :  type = WINED3DTS_WORLDMATRIX(0); break;
2907         case D3DTRANSFORMSTATE_WORLD1:  type = WINED3DTS_WORLDMATRIX(1); break;
2908         case D3DTRANSFORMSTATE_WORLD2:  type = WINED3DTS_WORLDMATRIX(2); break;
2909         case D3DTRANSFORMSTATE_WORLD3:  type = WINED3DTS_WORLDMATRIX(3); break;
2910         default:                        type = TransformStateType;
2911     }
2912
2913     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2914     return IWineD3DDevice_MultiplyTransform(This->wineD3DDevice,
2915                                             type,
2916                                             (WINED3DMATRIX*) D3DMatrix);
2917 }
2918
2919 static HRESULT WINAPI
2920 Thunk_IDirect3DDeviceImpl_3_MultiplyTransform(IDirect3DDevice3 *iface,
2921                                               D3DTRANSFORMSTATETYPE TransformStateType,
2922                                               D3DMATRIX *D3DMatrix)
2923 {
2924     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2925     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2926     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2927                                               TransformStateType,
2928                                               D3DMatrix);
2929 }
2930
2931 static HRESULT WINAPI
2932 Thunk_IDirect3DDeviceImpl_2_MultiplyTransform(IDirect3DDevice2 *iface,
2933                                               D3DTRANSFORMSTATETYPE TransformStateType,
2934                                               D3DMATRIX *D3DMatrix)
2935 {
2936     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2937     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2938     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2939                                               TransformStateType,
2940                                               D3DMatrix);
2941 }
2942
2943 /*****************************************************************************
2944  * IDirect3DDevice7::DrawPrimitive
2945  *
2946  * Draws primitives based on vertices in an application-provided pointer
2947  *
2948  * Version 2, 3 and 7. The IDirect3DDevice2 thunk converts the fixed vertex type into
2949  * an FVF format for D3D7
2950  *
2951  * Params:
2952  *  PrimitiveType: The type of the primitives to draw
2953  *  Vertex type: Flexible vertex format vertex description
2954  *  Vertices: Pointer to the vertex array
2955  *  VertexCount: The number of vertices to draw
2956  *  Flags: As usual a few flags
2957  *
2958  * Returns:
2959  *  D3D_OK on success
2960  *  DDERR_INVALIDPARAMS if Vertices is NULL
2961  *  For details, see IWineD3DDevice::DrawPrimitiveUP
2962  *
2963  *****************************************************************************/
2964 static HRESULT WINAPI
2965 IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
2966                                     D3DPRIMITIVETYPE PrimitiveType,
2967                                     DWORD VertexType,
2968                                     void *Vertices,
2969                                     DWORD VertexCount,
2970                                     DWORD Flags)
2971 {
2972     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2973     UINT PrimitiveCount, stride;
2974     HRESULT hr;
2975     TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2976
2977     if(!Vertices)
2978         return DDERR_INVALIDPARAMS;
2979
2980     /* Get the vertex count */
2981     switch(PrimitiveType)
2982     {
2983       case D3DPT_POINTLIST: 
2984         PrimitiveCount = VertexCount;
2985         break;
2986
2987       case D3DPT_LINELIST: 
2988         PrimitiveCount = VertexCount / 2;
2989         break;
2990
2991       case D3DPT_LINESTRIP:
2992         PrimitiveCount = VertexCount - 1;
2993         break;
2994
2995       case D3DPT_TRIANGLELIST:
2996         PrimitiveCount = VertexCount / 3;
2997         break;
2998
2999       case D3DPT_TRIANGLESTRIP:
3000         PrimitiveCount = VertexCount - 2;
3001         break;
3002
3003       case D3DPT_TRIANGLEFAN:
3004         PrimitiveCount = VertexCount - 2;
3005         break;
3006
3007       default: return DDERR_INVALIDPARAMS;
3008     }
3009
3010     /* Get the stride */
3011     stride = get_flexible_vertex_size(VertexType);
3012
3013     /* Set the FVF */
3014     hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
3015                                              IDirectDrawImpl_FindDecl(This->ddraw, VertexType));
3016     if(hr != D3D_OK) return hr;
3017
3018     /* This method translates to the user pointer draw of WineD3D */
3019     return IWineD3DDevice_DrawPrimitiveUP(This->wineD3DDevice,
3020                                           PrimitiveType,
3021                                           PrimitiveCount,
3022                                           Vertices,
3023                                           stride);
3024 }
3025
3026 static HRESULT WINAPI
3027 Thunk_IDirect3DDeviceImpl_3_DrawPrimitive(IDirect3DDevice3 *iface,
3028                                           D3DPRIMITIVETYPE PrimitiveType,
3029                                           DWORD VertexType,
3030                                           void *Vertices,
3031                                           DWORD VertexCount,
3032                                           DWORD Flags)
3033 {
3034     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3035     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
3036     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3037                                           PrimitiveType,
3038                                           VertexType,
3039                                           Vertices,
3040                                           VertexCount,
3041                                           Flags);
3042 }
3043
3044 static HRESULT WINAPI
3045 Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
3046                                           D3DPRIMITIVETYPE PrimitiveType,
3047                                           D3DVERTEXTYPE VertexType,
3048                                           void *Vertices,
3049                                           DWORD VertexCount,
3050                                           DWORD Flags)
3051 {
3052     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3053     DWORD FVF;
3054     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
3055
3056     switch(VertexType)
3057     {
3058         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
3059         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
3060         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
3061         default:
3062             ERR("Unexpected vertex type %d\n", VertexType);
3063             return DDERR_INVALIDPARAMS;  /* Should never happen */
3064     }
3065
3066     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3067                                           PrimitiveType,
3068                                           FVF,
3069                                           Vertices,
3070                                           VertexCount,
3071                                           Flags);
3072 }
3073
3074 /*****************************************************************************
3075  * IDirect3DDevice7::DrawIndexedPrimitive
3076  *
3077  * Draws vertices from an application-provided pointer, based on the index
3078  * numbers in a WORD array.
3079  *
3080  * Version 2, 3 and 7. The version 7 thunk translates the vertex type into
3081  * an FVF format for D3D7
3082  *
3083  * Params:
3084  *  PrimitiveType: The primitive type to draw
3085  *  VertexType: The FVF vertex description
3086  *  Vertices: Pointer to the vertex array
3087  *  VertexCount: ?
3088  *  Indices: Pointer to the index array
3089  *  IndexCount: Number of indices = Number of vertices to draw
3090  *  Flags: As usual, some flags
3091  *
3092  * Returns:
3093  *  D3D_OK on success
3094  *  DDERR_INVALIDPARAMS if Vertices or Indices is NULL
3095  *  For details, see IWineD3DDevice::DrawIndexedPrimitiveUP
3096  *
3097  *****************************************************************************/
3098 static HRESULT WINAPI
3099 IDirect3DDeviceImpl_7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
3100                                            D3DPRIMITIVETYPE PrimitiveType,
3101                                            DWORD VertexType,
3102                                            void *Vertices,
3103                                            DWORD VertexCount,
3104                                            WORD *Indices,
3105                                            DWORD IndexCount,
3106                                            DWORD Flags)
3107 {
3108     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3109     UINT PrimitiveCount = 0;
3110     HRESULT hr;
3111     TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3112     /* Get the primitive number */
3113     switch(PrimitiveType)
3114     {
3115       case D3DPT_POINTLIST: 
3116         PrimitiveCount = IndexCount;
3117         break;
3118
3119       case D3DPT_LINELIST: 
3120         PrimitiveCount = IndexCount / 2;
3121         break;
3122
3123       case D3DPT_LINESTRIP:
3124         PrimitiveCount = IndexCount - 1;
3125         break;
3126
3127       case D3DPT_TRIANGLELIST:
3128         PrimitiveCount = IndexCount / 3;
3129         break;
3130
3131       case D3DPT_TRIANGLESTRIP:
3132         PrimitiveCount = IndexCount - 2;
3133         break;
3134
3135       case D3DPT_TRIANGLEFAN:
3136         PrimitiveCount = IndexCount - 2;
3137         break;
3138
3139       default: return DDERR_INVALIDPARAMS;
3140     }
3141
3142     /* Set the D3DDevice's FVF */
3143     hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
3144                                              IDirectDrawImpl_FindDecl(This->ddraw, VertexType));
3145     if(FAILED(hr))
3146     {
3147         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3148         return hr;
3149     }
3150
3151     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->wineD3DDevice,
3152                                                  PrimitiveType,
3153                                                  0 /* MinVertexIndex */,
3154                                                  VertexCount /* UINT NumVertexIndex */,
3155                                                  PrimitiveCount,
3156                                                  Indices,
3157                                                  WINED3DFMT_INDEX16,
3158                                                  Vertices,
3159                                                  get_flexible_vertex_size(VertexType));
3160 }
3161
3162 static HRESULT WINAPI
3163 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
3164                                                  D3DPRIMITIVETYPE PrimitiveType,
3165                                                  DWORD VertexType,
3166                                                  void *Vertices,
3167                                                  DWORD VertexCount,
3168                                                  WORD *Indices,
3169                                                  DWORD IndexCount,
3170                                                  DWORD Flags)
3171 {
3172     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3173     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3174     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3175                                                  PrimitiveType,
3176                                                  VertexType,
3177                                                  Vertices,
3178                                                  VertexCount,
3179                                                  Indices,
3180                                                  IndexCount,
3181                                                  Flags);
3182 }
3183
3184 static HRESULT WINAPI
3185 Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
3186                                                  D3DPRIMITIVETYPE PrimitiveType,
3187                                                  D3DVERTEXTYPE VertexType,
3188                                                  void *Vertices,
3189                                                  DWORD VertexCount,
3190                                                  WORD *Indices,
3191                                                  DWORD IndexCount,
3192                                                  DWORD Flags)
3193 {
3194     DWORD FVF;
3195     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3196     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3197
3198     switch(VertexType)
3199     {
3200         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
3201         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
3202         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
3203         default:
3204             ERR("Unexpected vertex type %d\n", VertexType);
3205             return DDERR_INVALIDPARAMS;  /* Should never happen */
3206     }
3207
3208     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3209                                                  PrimitiveType,
3210                                                  FVF,
3211                                                  Vertices,
3212                                                  VertexCount,
3213                                                  Indices,
3214                                                  IndexCount,
3215                                                  Flags);
3216 }
3217
3218 /*****************************************************************************
3219  * IDirect3DDevice7::SetClipStatus
3220  *
3221  * Sets the clip status. This defines things as clipping conditions and
3222  * the extents of the clipping region.
3223  *
3224  * Version 2, 3 and 7
3225  *
3226  * Params:
3227  *  ClipStatus:
3228  *
3229  * Returns:
3230  *  D3D_OK because it's a stub
3231  *  (DDERR_INVALIDPARAMS if ClipStatus == NULL)
3232  *
3233  *****************************************************************************/
3234 static HRESULT WINAPI
3235 IDirect3DDeviceImpl_7_SetClipStatus(IDirect3DDevice7 *iface,
3236                                     D3DCLIPSTATUS *ClipStatus)
3237 {
3238     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3239     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3240
3241     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
3242      * Perhaps this needs a new data type and an additional IWineD3DDevice method
3243      */
3244     /* return IWineD3DDevice_SetClipStatus(This->wineD3DDevice, ClipStatus);*/
3245     return D3D_OK;
3246 }
3247
3248 static HRESULT WINAPI
3249 Thunk_IDirect3DDeviceImpl_3_SetClipStatus(IDirect3DDevice3 *iface,
3250                                           D3DCLIPSTATUS *ClipStatus)
3251 {
3252     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3253     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3254     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3255                                           ClipStatus);
3256 }
3257
3258 static HRESULT WINAPI
3259 Thunk_IDirect3DDeviceImpl_2_SetClipStatus(IDirect3DDevice2 *iface,
3260                                           D3DCLIPSTATUS *ClipStatus)
3261 {
3262     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3263     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3264     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3265                                           ClipStatus);
3266 }
3267
3268 /*****************************************************************************
3269  * IDirect3DDevice7::GetClipStatus
3270  *
3271  * Returns the clip status
3272  *
3273  * Params:
3274  *  ClipStatus: Address to write the clip status to
3275  *
3276  * Returns:
3277  *  D3D_OK because it's a stub
3278  *
3279  *****************************************************************************/
3280 static HRESULT WINAPI
3281 IDirect3DDeviceImpl_7_GetClipStatus(IDirect3DDevice7 *iface,
3282                                     D3DCLIPSTATUS *ClipStatus)
3283 {
3284     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3285     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3286
3287     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
3288     /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
3289     return D3D_OK;
3290 }
3291
3292 static HRESULT WINAPI
3293 Thunk_IDirect3DDeviceImpl_3_GetClipStatus(IDirect3DDevice3 *iface,
3294                                           D3DCLIPSTATUS *ClipStatus)
3295 {
3296     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3297     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3298     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3299                                           ClipStatus);
3300 }
3301
3302 static HRESULT WINAPI
3303 Thunk_IDirect3DDeviceImpl_2_GetClipStatus(IDirect3DDevice2 *iface,
3304                                           D3DCLIPSTATUS *ClipStatus)
3305 {
3306     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3307     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3308     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3309                                           ClipStatus);
3310 }
3311
3312 /*****************************************************************************
3313  * IDirect3DDevice::DrawPrimitiveStrided
3314  *
3315  * Draws vertices described by a D3DDRAWPRIMITIVESTRIDEDDATA structure.
3316  *
3317  * Version 3 and 7
3318  *
3319  * Params:
3320  *  PrimitiveType: The primitive type to draw
3321  *  VertexType: The FVF description of the vertices to draw (for the stride??)
3322  *  D3DDrawPrimStrideData: A D3DDRAWPRIMITIVESTRIDEDDATA structure describing
3323  *                         the vertex data locations
3324  *  VertexCount: The number of vertices to draw
3325  *  Flags: Some flags
3326  *
3327  * Returns:
3328  *  D3D_OK, because it's a stub
3329  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3330  *  (For details, see IWineD3DDevice::DrawPrimitiveStrided)
3331  *
3332  *****************************************************************************/
3333 static HRESULT WINAPI
3334 IDirect3DDeviceImpl_7_DrawPrimitiveStrided(IDirect3DDevice7 *iface,
3335                                            D3DPRIMITIVETYPE PrimitiveType,
3336                                            DWORD VertexType,
3337                                            D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3338                                            DWORD VertexCount,
3339                                            DWORD Flags)
3340 {
3341     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3342     WineDirect3DVertexStridedData WineD3DStrided;
3343     int i;
3344     UINT PrimitiveCount;
3345
3346     TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3347
3348     memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
3349     /* Get the strided data right. the wined3d structure is a bit bigger
3350      * Watch out: The contents of the strided data are determined by the fvf,
3351      * not by the members set in D3DDrawPrimStrideData. So it's valid
3352      * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
3353      * not set in the fvf.
3354      */
3355     if(VertexType & D3DFVF_POSITION_MASK)
3356     {
3357         WineD3DStrided.u.s.position.lpData = D3DDrawPrimStrideData->position.lpvData;
3358         WineD3DStrided.u.s.position.dwStride = D3DDrawPrimStrideData->position.dwStride;
3359         WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
3360         if (VertexType & D3DFVF_XYZRHW)
3361         {
3362             WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
3363             WineD3DStrided.u.s.position_transformed = TRUE;
3364         } else
3365             WineD3DStrided.u.s.position_transformed = FALSE;
3366     }
3367
3368     if(VertexType & D3DFVF_NORMAL)
3369     {
3370         WineD3DStrided.u.s.normal.lpData = D3DDrawPrimStrideData->normal.lpvData;
3371         WineD3DStrided.u.s.normal.dwStride = D3DDrawPrimStrideData->normal.dwStride;
3372         WineD3DStrided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
3373     }
3374
3375     if(VertexType & D3DFVF_DIFFUSE)
3376     {
3377         WineD3DStrided.u.s.diffuse.lpData = D3DDrawPrimStrideData->diffuse.lpvData;
3378         WineD3DStrided.u.s.diffuse.dwStride = D3DDrawPrimStrideData->diffuse.dwStride;
3379         WineD3DStrided.u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
3380     }
3381
3382     if(VertexType & D3DFVF_SPECULAR)
3383     {
3384         WineD3DStrided.u.s.specular.lpData = D3DDrawPrimStrideData->specular.lpvData;
3385         WineD3DStrided.u.s.specular.dwStride = D3DDrawPrimStrideData->specular.dwStride;
3386         WineD3DStrided.u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
3387     }
3388
3389     for( i = 0; i < GET_TEXCOUNT_FROM_FVF(VertexType); i++)
3390     {
3391         WineD3DStrided.u.s.texCoords[i].lpData = D3DDrawPrimStrideData->textureCoords[i].lpvData;
3392         WineD3DStrided.u.s.texCoords[i].dwStride = D3DDrawPrimStrideData->textureCoords[i].dwStride;
3393         switch(GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i))
3394         {
3395             case 1: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT1; break;
3396             case 2: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT2; break;
3397             case 3: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT3; break;
3398             case 4: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT4; break;
3399             default: ERR("Unexpected texture coordinate size %d\n",
3400                          GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i));
3401         }
3402     }
3403
3404     /* Get the primitive count */
3405     switch(PrimitiveType)
3406     {
3407         case D3DPT_POINTLIST: 
3408           PrimitiveCount = VertexCount;
3409           break;
3410
3411         case D3DPT_LINELIST: 
3412           PrimitiveCount = VertexCount / 2;
3413           break;
3414
3415         case D3DPT_LINESTRIP:
3416           PrimitiveCount = VertexCount - 1;
3417           break;
3418
3419         case D3DPT_TRIANGLELIST:
3420           PrimitiveCount = VertexCount / 3;
3421           break;
3422
3423         case D3DPT_TRIANGLESTRIP:
3424           PrimitiveCount = VertexCount - 2;
3425           break;
3426
3427         case D3DPT_TRIANGLEFAN:
3428           PrimitiveCount = VertexCount - 2;
3429           break;
3430
3431         default: return DDERR_INVALIDPARAMS;
3432     }
3433
3434     /* WineD3D doesn't need the FVF here */
3435     return IWineD3DDevice_DrawPrimitiveStrided(This->wineD3DDevice,
3436                                                PrimitiveType,
3437                                                PrimitiveCount,
3438                                                &WineD3DStrided);
3439 }
3440
3441 static HRESULT WINAPI
3442 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
3443                                                  D3DPRIMITIVETYPE PrimitiveType,
3444                                                  DWORD VertexType,
3445                                                  D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3446                                                  DWORD VertexCount,
3447                                                  DWORD Flags)
3448 {
3449     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3450     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3451     return IDirect3DDevice7_DrawPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3452                                                  PrimitiveType,
3453                                                  VertexType,
3454                                                  D3DDrawPrimStrideData,
3455                                                  VertexCount,
3456                                                  Flags);
3457 }
3458
3459 /*****************************************************************************
3460  * IDirect3DDevice7::DrawIndexedPrimitiveStrided
3461  *
3462  * Draws primitives specified by strided data locations based on indices
3463  *
3464  * Version 3 and 7
3465  *
3466  * Params:
3467  *  PrimitiveType:
3468  *
3469  * Returns:
3470  *  D3D_OK, because it's a stub
3471  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3472  *  (DDERR_INVALIDPARAMS if Indices is NULL)
3473  *  (For more details, see IWineD3DDevice::DrawIndexedPrimitiveStrided)
3474  *
3475  *****************************************************************************/
3476 static HRESULT WINAPI
3477 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
3478                                                   D3DPRIMITIVETYPE PrimitiveType,
3479                                                   DWORD VertexType,
3480                                                   D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3481                                                   DWORD VertexCount,
3482                                                   WORD *Indices,
3483                                                   DWORD IndexCount,
3484                                                   DWORD Flags)
3485 {
3486     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3487     FIXME("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3488
3489     /* I'll implement it as soon as I find a app to test it.
3490      * This needs an additional method in IWineD3DDevice.
3491      */
3492     return D3D_OK;
3493 }
3494
3495 static HRESULT WINAPI
3496 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
3497                                                         D3DPRIMITIVETYPE PrimitiveType,
3498                                                         DWORD VertexType,
3499                                                         D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3500                                                         DWORD VertexCount,
3501                                                         WORD *Indices,
3502                                                         DWORD IndexCount,
3503                                                         DWORD Flags)
3504 {
3505     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3506     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3507     return IDirect3DDevice7_DrawIndexedPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3508                                                         PrimitiveType,
3509                                                         VertexType,
3510                                                         D3DDrawPrimStrideData,
3511                                                         VertexCount,
3512                                                         Indices,
3513                                                         IndexCount,
3514                                                         Flags);
3515 }
3516
3517 /*****************************************************************************
3518  * IDirect3DDevice7::DrawPrimitiveVB
3519  *
3520  * Draws primitives from a vertex buffer to the screen.
3521  *
3522  * Version 3 and 7
3523  *
3524  * Params:
3525  *  PrimitiveType: Type of primitive to be rendered.
3526  *  D3DVertexBuf: Source Vertex Buffer
3527  *  StartVertex: Index of the first vertex from the buffer to be rendered
3528  *  NumVertices: Number of vertices to be rendered
3529  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3530  *
3531  * Return values
3532  *  D3D_OK on success
3533  *  DDERR_INVALIDPARAMS if D3DVertexBuf is NULL
3534  *
3535  *****************************************************************************/
3536 static HRESULT WINAPI
3537 IDirect3DDeviceImpl_7_DrawPrimitiveVB(IDirect3DDevice7 *iface,
3538                                       D3DPRIMITIVETYPE PrimitiveType,
3539                                       IDirect3DVertexBuffer7 *D3DVertexBuf,
3540                                       DWORD StartVertex,
3541                                       DWORD NumVertices,
3542                                       DWORD Flags)
3543 {
3544     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3545     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3546     UINT PrimitiveCount;
3547     HRESULT hr;
3548     DWORD stride;
3549     WINED3DVERTEXBUFFER_DESC Desc;
3550
3551     TRACE("(%p)->(%08x,%p,%08x,%08x,%08x)\n", This, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
3552
3553     /* Sanity checks */
3554     if(!vb)
3555     {
3556         ERR("(%p) No Vertex buffer specified\n", This);
3557         return DDERR_INVALIDPARAMS;
3558     }
3559
3560     /* Get the primitive count */
3561     switch(PrimitiveType)
3562     {
3563         case D3DPT_POINTLIST: 
3564           PrimitiveCount = NumVertices;
3565           break;
3566
3567         case D3DPT_LINELIST: 
3568           PrimitiveCount = NumVertices / 2;
3569           break;
3570
3571         case D3DPT_LINESTRIP:
3572           PrimitiveCount = NumVertices - 1;
3573           break;
3574
3575         case D3DPT_TRIANGLELIST:
3576           PrimitiveCount = NumVertices / 3;
3577           break;
3578
3579         case D3DPT_TRIANGLESTRIP:
3580           PrimitiveCount = NumVertices - 2;
3581           break;
3582
3583         case D3DPT_TRIANGLEFAN:
3584           PrimitiveCount = NumVertices - 2;
3585           break;
3586
3587         default: return DDERR_INVALIDPARAMS;
3588     }
3589
3590     /* Get the FVF of the vertex buffer, and its stride */
3591     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3592                                       &Desc);
3593     if(hr != D3D_OK)
3594     {
3595         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3596         return hr;
3597     }
3598     stride = get_flexible_vertex_size(Desc.FVF);
3599
3600     hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
3601                                              vb->wineD3DVertexDeclaration);
3602     if(FAILED(hr))
3603     {
3604         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3605         return hr;
3606     }
3607
3608     /* Set the vertex stream source */
3609     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3610                                         0 /* StreamNumber */,
3611                                         vb->wineD3DVertexBuffer,
3612                                         0 /* StartVertex - we pass this to DrawPrimitive */,
3613                                         stride);
3614     if(hr != D3D_OK)
3615     {
3616         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3617         return hr;
3618     }
3619
3620     /* Now draw the primitives */
3621     return IWineD3DDevice_DrawPrimitive(This->wineD3DDevice,
3622                                         PrimitiveType,
3623                                         StartVertex,
3624                                         PrimitiveCount);
3625 }
3626
3627 static HRESULT WINAPI
3628 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB(IDirect3DDevice3 *iface,
3629                                             D3DPRIMITIVETYPE PrimitiveType,
3630                                             IDirect3DVertexBuffer *D3DVertexBuf,
3631                                             DWORD StartVertex,
3632                                             DWORD NumVertices,
3633                                             DWORD Flags)
3634 {
3635     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3636     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3637     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This,  PrimitiveType, vb, StartVertex, NumVertices, Flags);
3638     return IDirect3DDevice7_DrawPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3639                                             PrimitiveType,
3640                                             ICOM_INTERFACE(vb, IDirect3DVertexBuffer7),
3641                                             StartVertex,
3642                                             NumVertices,
3643                                             Flags);
3644 }
3645
3646
3647 /*****************************************************************************
3648  * IDirect3DDevice7::DrawIndexedPrimitiveVB
3649  *
3650  * Draws primitives from a vertex buffer to the screen
3651  *
3652  * Params:
3653  *  PrimitiveType: Type of primitive to be rendered.
3654  *  D3DVertexBuf: Source Vertex Buffer
3655  *  StartVertex: Index of the first vertex from the buffer to be rendered
3656  *  NumVertices: Number of vertices to be rendered
3657  *  Indices: Array of DWORDs used to index into the Vertices
3658  *  IndexCount: Number of indices in Indices
3659  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3660  *
3661  * Return values
3662  *
3663  *****************************************************************************/
3664 static HRESULT WINAPI
3665 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
3666                                              D3DPRIMITIVETYPE PrimitiveType,
3667                                              IDirect3DVertexBuffer7 *D3DVertexBuf,
3668                                              DWORD StartVertex,
3669                                              DWORD NumVertices,
3670                                              WORD *Indices,
3671                                              DWORD IndexCount,
3672                                              DWORD Flags)
3673 {
3674     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3675     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3676     DWORD stride;
3677     UINT PrimitiveCount;
3678     WORD *LockedIndices;
3679     HRESULT hr;
3680     WINED3DVERTEXBUFFER_DESC Desc;
3681
3682     TRACE("(%p)->(%08x,%p,%d,%d,%p,%d,%08x)\n", This, PrimitiveType, vb, StartVertex, NumVertices, Indices, IndexCount, Flags);
3683
3684     /* Steps:
3685      * 1) Calculate some things: Vertex count -> Primitive count, stride, ...
3686      * 2) Upload the Indices to the index buffer
3687      * 3) Set the index source
3688      * 4) Set the Vertex Buffer as the Stream source
3689      * 5) Call IWineD3DDevice::DrawIndexedPrimitive
3690      */
3691
3692     /* Get the primitive count */
3693     switch(PrimitiveType)
3694     {
3695         case D3DPT_POINTLIST: 
3696           PrimitiveCount = IndexCount;
3697           break;
3698
3699         case D3DPT_LINELIST: 
3700           PrimitiveCount = IndexCount / 2;
3701           break;
3702
3703         case D3DPT_LINESTRIP:
3704           PrimitiveCount = IndexCount - 1;
3705           break;
3706
3707         case D3DPT_TRIANGLELIST:
3708           PrimitiveCount = IndexCount / 3;
3709           break;
3710
3711         case D3DPT_TRIANGLESTRIP:
3712           PrimitiveCount = IndexCount - 2;
3713           break;
3714
3715         case D3DPT_TRIANGLEFAN:
3716           PrimitiveCount = IndexCount - 2;
3717           break;
3718
3719         default: return DDERR_INVALIDPARAMS;
3720     }
3721
3722     /* Get the FVF of the vertex buffer, and its stride */
3723     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3724                                       &Desc);
3725     if(hr != D3D_OK)
3726     {
3727         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3728         return hr;
3729     }
3730     stride = get_flexible_vertex_size(Desc.FVF);
3731     TRACE("Vertex buffer FVF = %08x, stride=%d\n", Desc.FVF, stride);
3732
3733     hr = IWineD3DDevice_SetVertexDeclaration(This->wineD3DDevice,
3734                                              vb->wineD3DVertexDeclaration);
3735     if(FAILED(hr))
3736     {
3737         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3738         return hr;
3739     }
3740
3741     /* copy the index stream into the index buffer.
3742      * A new IWineD3DDevice method could be created
3743      * which takes an user pointer containing the indices
3744      * or a SetData-Method for the index buffer, which
3745      * overrides the index buffer data with our pointer.
3746      */
3747     hr = IWineD3DIndexBuffer_Lock(This->indexbuffer,
3748                                   0 /* OffSetToLock */,
3749                                   IndexCount * sizeof(WORD),
3750                                   (BYTE **) &LockedIndices,
3751                                   0 /* Flags */);
3752     assert(IndexCount < 0x100000);
3753     if(hr != D3D_OK)
3754     {
3755         ERR("(%p) IWineD3DIndexBuffer::Lock failed with hr = %08x\n", This, hr);
3756         return hr;
3757     }
3758     memcpy(LockedIndices, Indices, IndexCount * sizeof(WORD));
3759     hr = IWineD3DIndexBuffer_Unlock(This->indexbuffer);
3760     if(hr != D3D_OK)
3761     {
3762         ERR("(%p) IWineD3DIndexBuffer::Unlock failed with hr = %08x\n", This, hr);
3763         return hr;
3764     }
3765
3766     /* Set the index stream */
3767     hr = IWineD3DDevice_SetIndices(This->wineD3DDevice,
3768                                    This->indexbuffer,
3769                                    StartVertex);
3770
3771     /* Set the vertex stream source */
3772     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3773                                         0 /* StreamNumber */,
3774                                         vb->wineD3DVertexBuffer,
3775                                         0 /* offset, we pass this to DrawIndexedPrimitive */,
3776                                         stride);
3777     if(hr != D3D_OK)
3778     {
3779         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3780         return hr;
3781     }
3782
3783
3784     hr = IWineD3DDevice_DrawIndexedPrimitive(This->wineD3DDevice,
3785                                              PrimitiveType,
3786                                              0 /* minIndex */,
3787                                              NumVertices,
3788                                              0 /* StartIndex */,
3789                                              PrimitiveCount);
3790
3791     return D3D_OK;
3792 }
3793
3794 static HRESULT WINAPI
3795 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
3796                                                    D3DPRIMITIVETYPE PrimitiveType,
3797                                                    IDirect3DVertexBuffer *D3DVertexBuf,
3798                                                    WORD *Indices,
3799                                                    DWORD IndexCount,
3800                                                    DWORD Flags)
3801 {
3802     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3803     IDirect3DVertexBufferImpl *VB = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3804     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VB, Indices, IndexCount, Flags);
3805
3806     return IDirect3DDevice7_DrawIndexedPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3807                                                    PrimitiveType,
3808                                                    ICOM_INTERFACE(VB, IDirect3DVertexBuffer7),
3809                                                    0,
3810                                                    IndexCount,
3811                                                    Indices,
3812                                                    IndexCount,
3813                                                    Flags);
3814 }
3815
3816 /*****************************************************************************
3817  * IDirect3DDevice7::ComputeSphereVisibility
3818  *
3819  * Calculates the visibility of spheres in the current viewport. The spheres
3820  * are passed in the Centers and Radii arrays, the results are passed back
3821  * in the ReturnValues array. Return values are either completely visible,
3822  * partially visible or completely invisible.
3823  * The return value consist of a combination of D3DCLIP_* flags, or it's
3824  * 0 if the sphere is completely visible(according to the SDK, not checked)
3825  *
3826  * Sounds like an overdose of math ;)
3827  *
3828  * Version 3 and 7
3829  *
3830  * Params:
3831  *  Centers: Array containing the sphere centers
3832  *  Radii: Array containing the sphere radii
3833  *  NumSpheres: The number of centers and radii in the arrays
3834  *  Flags: Some flags
3835  *  ReturnValues: Array to write the results to
3836  *
3837  * Returns:
3838  *  D3D_OK because it's a stub
3839  *  (DDERR_INVALIDPARAMS if Centers, Radii or ReturnValues are NULL)
3840  *  (D3DERR_INVALIDMATRIX if the combined world, view and proj matrix
3841  *  is singular)
3842  *
3843  *****************************************************************************/
3844 static HRESULT WINAPI
3845 IDirect3DDeviceImpl_7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
3846                                               D3DVECTOR *Centers,
3847                                               D3DVALUE *Radii,
3848                                               DWORD NumSpheres,
3849                                               DWORD Flags,
3850                                               DWORD *ReturnValues)
3851 {
3852     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3853     FIXME("(%p)->(%p,%p,%08x,%08x,%p): stub!\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3854
3855     /* the DirectX 7 sdk says that the visibility is computed by
3856      * back-transforming the viewing frustum to model space
3857      * using the inverse of the combined world, view and projection
3858      * matrix. If the matrix can't be reversed, D3DERR_INVALIDMATRIX
3859      * is returned.
3860      *
3861      * Basic implementation idea:
3862      * 1) Check if the center is in the viewing frustum
3863      * 2) Cut the sphere with the planes of the viewing
3864      *    frustum
3865      *
3866      * ->Center inside the frustum, no intersections:
3867      *    Fully visible
3868      * ->Center outside the frustum, no intersections:
3869      *    Not visible
3870      * ->Some intersections: Partially visible
3871      *
3872      * Implement this call in WineD3D. Either implement the
3873      * matrix and vector stuff in WineD3D, or use some external
3874      * math library.
3875      */
3876
3877     return D3D_OK;
3878 }
3879
3880 static HRESULT WINAPI
3881 Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
3882                                                     D3DVECTOR *Centers,
3883                                                     D3DVALUE *Radii,
3884                                                     DWORD NumSpheres,
3885                                                     DWORD Flags,
3886                                                     DWORD *ReturnValues)
3887 {
3888     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3889     TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3890     return IDirect3DDevice7_ComputeSphereVisibility(ICOM_INTERFACE(This, IDirect3DDevice7),
3891                                                     Centers,
3892                                                     Radii,
3893                                                     NumSpheres,
3894                                                     Flags,
3895                                                     ReturnValues);
3896 }
3897
3898 /*****************************************************************************
3899  * IDirect3DDevice7::GetTexture
3900  *
3901  * Returns the texture interface handle assigned to a texture stage.
3902  * The returned texture is AddRefed. This is taken from old ddraw,
3903  * not checked in Windows.
3904  *
3905  * Version 3 and 7
3906  *
3907  * Params:
3908  *  Stage: Texture stage to read the texture from
3909  *  Texture: Address to store the interface pointer at
3910  *
3911  * Returns:
3912  *  D3D_OK on success
3913  *  DDERR_INVALIDPARAMS if Texture is NULL
3914  *  For details, see IWineD3DDevice::GetTexture
3915  *
3916  *****************************************************************************/
3917 static HRESULT WINAPI
3918 IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
3919                                  DWORD Stage,
3920                                  IDirectDrawSurface7 **Texture)
3921 {
3922     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3923     IWineD3DBaseTexture *Surf;
3924     HRESULT hr;
3925     TRACE("(%p)->(%d,%p): Relay\n", This, Stage, Texture);
3926
3927     if(!Texture)
3928     {
3929         TRACE("Texture == NULL, failing with DDERR_INVALIDPARAMS\n");
3930         return DDERR_INVALIDPARAMS;
3931     }
3932
3933     hr = IWineD3DDevice_GetTexture(This->wineD3DDevice, Stage, (IWineD3DBaseTexture **) &Surf);
3934     if( (hr != D3D_OK) || (!Surf) ) 
3935     {
3936         *Texture = NULL;
3937         return hr;
3938     }
3939
3940     /* GetParent AddRef()s, which is perfectly OK.
3941      * We have passed the IDirectDrawSurface7 interface to WineD3D, so that's OK too.
3942      */
3943     return IWineD3DBaseTexture_GetParent(Surf,
3944                                          (IUnknown **) Texture);
3945 }
3946
3947 static HRESULT WINAPI
3948 Thunk_IDirect3DDeviceImpl_3_GetTexture(IDirect3DDevice3 *iface,
3949                                        DWORD Stage,
3950                                        IDirect3DTexture2 **Texture2)
3951 {
3952     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3953     HRESULT ret;
3954     IDirectDrawSurface7 *ret_val;
3955
3956     TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, Texture2);
3957     ret = IDirect3DDevice7_GetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3958                                       Stage,
3959                                       &ret_val);
3960
3961     *Texture2 = COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirect3DTexture2, ret_val);
3962
3963     TRACE_(ddraw_thunk)(" returning interface %p.\n", *Texture2);
3964
3965     return ret;
3966 }
3967
3968 /*****************************************************************************
3969  * IDirect3DDevice7::SetTexture
3970  *
3971  * Assigns a texture to a texture stage. Is the texture AddRef-ed?
3972  *
3973  * Version 3 and 7
3974  *
3975  * Params:
3976  *  Stage: The stage to assign the texture to
3977  *  Texture: Interface pointer to the texture surface
3978  *
3979  * Returns
3980  * D3D_OK on success
3981  * For details, see IWineD3DDevice::SetTexture
3982  *
3983  *****************************************************************************/
3984 static HRESULT WINAPI
3985 IDirect3DDeviceImpl_7_SetTexture(IDirect3DDevice7 *iface,
3986                                  DWORD Stage,
3987                                  IDirectDrawSurface7 *Texture)
3988 {
3989     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3990     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
3991     TRACE("(%p)->(%08x,%p): Relay!\n", This, Stage, surf);
3992
3993     /* Texture may be NULL here */
3994     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
3995                                      Stage,
3996                                      surf ? surf->wineD3DTexture : NULL);
3997 }
3998
3999 static HRESULT WINAPI
4000 Thunk_IDirect3DDeviceImpl_3_SetTexture(IDirect3DDevice3 *iface,
4001                                        DWORD Stage,
4002                                        IDirect3DTexture2 *Texture2)
4003 {
4004     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4005     IDirectDrawSurfaceImpl *tex = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Texture2);
4006     TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, tex);
4007     return IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
4008                                        Stage,
4009                                        ICOM_INTERFACE(tex, IDirectDrawSurface7));
4010 }
4011
4012 /*****************************************************************************
4013  * IDirect3DDevice7::GetTextureStageState
4014  *
4015  * Retrieves a state from a texture stage.
4016  *
4017  * Version 3 and 7
4018  *
4019  * Params:
4020  *  Stage: The stage to retrieve the state from
4021  *  TexStageStateType: The state type to retrieve
4022  *  State: Address to store the state's value at
4023  *
4024  * Returns:
4025  *  D3D_OK on success
4026  *  DDERR_INVALIDPARAMS if State is NULL
4027  *  For details, see IWineD3DDevice::GetTextureStageState
4028  *
4029  *****************************************************************************/
4030 static HRESULT WINAPI
4031 IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
4032                                            DWORD Stage,
4033                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
4034                                            DWORD *State)
4035 {
4036     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4037     TRACE("(%p)->(%08x,%08x,%p): Relay!\n", This, Stage, TexStageStateType, State);
4038
4039     if(!State)
4040         return DDERR_INVALIDPARAMS;
4041
4042     switch(TexStageStateType)
4043     {
4044         /* Mipfilter is a sampler state with different values */
4045         case D3DTSS_MIPFILTER:
4046         {
4047             HRESULT hr;
4048             WINED3DTEXTUREFILTERTYPE value;
4049
4050             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4051                                                 Stage,
4052                                                 WINED3DSAMP_MIPFILTER,
4053                                                 &value);
4054             switch(value)
4055             {
4056                 case WINED3DTEXF_NONE: *State = D3DTFP_NONE; break;
4057                 case WINED3DTEXF_POINT: *State = D3DTFP_POINT; break;
4058                 case WINED3DTEXF_LINEAR: *State = D3DTFP_LINEAR; break;
4059                 default:
4060                     ERR("Unexpected mipfilter value %d\n", value);
4061                     *State = D3DTFP_NONE;
4062             }
4063             return hr;
4064         }
4065
4066         /* Minfilter is a sampler state too, equal values */
4067         case D3DTSS_MINFILTER:
4068             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4069                                                   Stage,
4070                                                   WINED3DSAMP_MINFILTER,
4071                                                   State);
4072         /* Magfilter has slightly different values */
4073         case D3DTSS_MAGFILTER:
4074         {
4075             HRESULT hr;
4076             WINED3DTEXTUREFILTERTYPE wined3dfilter;
4077             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4078                                                 Stage,
4079                                                 WINED3DSAMP_MAGFILTER,
4080                                                 &wined3dfilter);
4081             switch(wined3dfilter)
4082             {
4083                 case WINED3DTEXF_POINT:             *State = D3DTFG_POINT;          break;
4084                 case WINED3DTEXF_LINEAR:            *State = D3DTFG_LINEAR;         break;
4085                 case WINED3DTEXF_ANISOTROPIC:       *State = D3DTFG_ANISOTROPIC;    break;
4086                 case WINED3DTEXF_FLATCUBIC:         *State = D3DTFG_FLATCUBIC;      break;
4087                 case WINED3DTEXF_GAUSSIANCUBIC:     *State = D3DTFG_GAUSSIANCUBIC;  break;
4088                 default:
4089                     ERR("Unexpected wined3d mag filter value %d\n", wined3dfilter);
4090                     *State = D3DTFG_POINT;
4091             }
4092             return hr;
4093         }
4094
4095         case D3DTSS_ADDRESS:
4096         case D3DTSS_ADDRESSU:
4097             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4098                                                   Stage,
4099                                                   WINED3DSAMP_ADDRESSU,
4100                                                   State);
4101         case D3DTSS_ADDRESSV:
4102             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4103                                                   Stage,
4104                                                   WINED3DSAMP_ADDRESSV,
4105                                                   State);
4106         default:
4107             return IWineD3DDevice_GetTextureStageState(This->wineD3DDevice,
4108                                                        Stage,
4109                                                        TexStageStateType,
4110                                                        State);
4111     }
4112 }
4113
4114 static HRESULT WINAPI
4115 Thunk_IDirect3DDeviceImpl_3_GetTextureStageState(IDirect3DDevice3 *iface,
4116                                                  DWORD Stage,
4117                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
4118                                                  DWORD *State)
4119 {
4120     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4121     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
4122     return IDirect3DDevice7_GetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
4123                                                  Stage,
4124                                                  TexStageStateType,
4125                                                  State);
4126 }
4127
4128 /*****************************************************************************
4129  * IDirect3DDevice7::SetTextureStageState
4130  *
4131  * Sets a texture stage state. Some stage types need to be handled specially,
4132  * because they do not exist in WineD3D and were moved to another place
4133  *
4134  * Version 3 and 7
4135  *
4136  * Params:
4137  *  Stage: The stage to modify
4138  *  TexStageStateType: The state to change
4139  *  State: The new value for the state
4140  *
4141  * Returns:
4142  *  D3D_OK on success
4143  *  For details, see IWineD3DDevice::SetTextureStageState
4144  *
4145  *****************************************************************************/
4146 static HRESULT WINAPI
4147 IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
4148                                            DWORD Stage,
4149                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
4150                                            DWORD State)
4151 {
4152     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4153     TRACE("(%p)->(%08x,%08x,%08x): Relay!\n", This, Stage, TexStageStateType, State);
4154     switch(TexStageStateType)
4155     {
4156         /* Mipfilter is a sampler state with different values */
4157         case D3DTSS_MIPFILTER:
4158         {
4159             WINED3DTEXTUREFILTERTYPE value;
4160             switch(State)
4161             {
4162                 case D3DTFP_NONE: value = WINED3DTEXF_NONE; break;
4163                 case D3DTFP_POINT: value = WINED3DTEXF_POINT; break;
4164                 case 0: /* Unchecked */
4165                 case D3DTFP_LINEAR: value = WINED3DTEXF_LINEAR; break;
4166                 default:
4167                     ERR("Unexpected mipfilter value %d\n", State);
4168                     value = WINED3DTEXF_NONE;
4169             }
4170             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4171                                                   Stage,
4172                                                   WINED3DSAMP_MIPFILTER,
4173                                                   value);
4174         }
4175
4176         /* Minfilter is a sampler state too, equal values */
4177         case D3DTSS_MINFILTER:
4178             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4179                                                   Stage,
4180                                                   WINED3DSAMP_MINFILTER,
4181                                                   State);
4182         /* Magfilter has slightly different values */
4183         case D3DTSS_MAGFILTER:
4184         {
4185             WINED3DTEXTUREFILTERTYPE wined3dfilter;
4186             switch((D3DTEXTUREMAGFILTER) State)
4187             {
4188                 case D3DTFG_POINT:          wined3dfilter = WINED3DTEXF_POINT;          break;
4189                 case D3DTFG_LINEAR:         wined3dfilter = WINED3DTEXF_LINEAR;         break;
4190                 case D3DTFG_FLATCUBIC:      wined3dfilter = WINED3DTEXF_FLATCUBIC;      break;
4191                 case D3DTFG_GAUSSIANCUBIC:  wined3dfilter = WINED3DTEXF_GAUSSIANCUBIC;  break;
4192                 case D3DTFG_ANISOTROPIC:    wined3dfilter = WINED3DTEXF_ANISOTROPIC;    break;
4193                 default:
4194                     ERR("Unexpected d3d7 mag filter type %d\n", State);
4195                     wined3dfilter = WINED3DTEXF_POINT;
4196             }
4197             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4198                                                   Stage,
4199                                                   WINED3DSAMP_MAGFILTER,
4200                                                   wined3dfilter);
4201         }
4202
4203         case D3DTSS_ADDRESS:
4204                    IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4205                                                   Stage,
4206                                                   WINED3DSAMP_ADDRESSV,
4207                                                   State);
4208             /* Drop through */
4209         case D3DTSS_ADDRESSU:
4210             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4211                                                   Stage,
4212                                                   WINED3DSAMP_ADDRESSU,
4213                                                   State);
4214         case D3DTSS_ADDRESSV:
4215             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4216                                                   Stage,
4217                                                   WINED3DSAMP_ADDRESSV,
4218                                                   State);
4219         default:
4220
4221             return IWineD3DDevice_SetTextureStageState(This->wineD3DDevice,
4222                                                        Stage,
4223                                                        TexStageStateType,
4224                                                        State);
4225     }
4226 }
4227
4228 static HRESULT WINAPI
4229 Thunk_IDirect3DDeviceImpl_3_SetTextureStageState(IDirect3DDevice3 *iface,
4230                                                  DWORD Stage,
4231                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
4232                                                  DWORD State)
4233 {
4234     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4235     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
4236     return IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
4237                                                  Stage,
4238                                                  TexStageStateType,
4239                                                  State);
4240 }
4241
4242 /*****************************************************************************
4243  * IDirect3DDevice7::ValidateDevice
4244  *
4245  * SDK: "Reports the device's ability to render the currently set
4246  * texture-blending operations in a single pass". Whatever that means
4247  * exactly...
4248  *
4249  * Version 3 and 7
4250  *
4251  * Params:
4252  *  NumPasses: Address to write the number of necessary passes for the
4253  *             desired effect to.
4254  *
4255  * Returns:
4256  *  D3D_OK on success
4257  *  See IWineD3DDevice::ValidateDevice for more details
4258  *
4259  *****************************************************************************/
4260 static HRESULT WINAPI
4261 IDirect3DDeviceImpl_7_ValidateDevice(IDirect3DDevice7 *iface,
4262                                      DWORD *NumPasses)
4263 {
4264     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4265     TRACE("(%p)->(%p): Relay\n", This, NumPasses);
4266
4267     return IWineD3DDevice_ValidateDevice(This->wineD3DDevice, NumPasses);
4268 }
4269
4270 static HRESULT WINAPI
4271 Thunk_IDirect3DDeviceImpl_3_ValidateDevice(IDirect3DDevice3 *iface,
4272                                            DWORD *Passes)
4273 {
4274     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4275     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Passes);
4276     return IDirect3DDevice7_ValidateDevice(ICOM_INTERFACE(This, IDirect3DDevice7),
4277                                            Passes);
4278 }
4279
4280 /*****************************************************************************
4281  * IDirect3DDevice7::Clear
4282  *
4283  * Fills the render target, the z buffer and the stencil buffer with a
4284  * clear color / value
4285  *
4286  * Version 7 only
4287  *
4288  * Params:
4289  *  Count: Number of rectangles in Rects must be 0 if Rects is NULL
4290  *  Rects: Rectangles to clear. If NULL, the whole surface is cleared
4291  *  Flags: Some flags, as usual
4292  *  Color: Clear color for the render target
4293  *  Z: Clear value for the Z buffer
4294  *  Stencil: Clear value to store in each stencil buffer entry
4295  *
4296  * Returns:
4297  *  D3D_OK on success
4298  *  For details, see IWineD3DDevice::Clear
4299  *
4300  *****************************************************************************/
4301 static HRESULT WINAPI
4302 IDirect3DDeviceImpl_7_Clear(IDirect3DDevice7 *iface,
4303                             DWORD Count,
4304                             D3DRECT *Rects,
4305                             DWORD Flags,
4306                             D3DCOLOR Color,
4307                             D3DVALUE Z,
4308                             DWORD Stencil)
4309 {
4310     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4311     TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x): Relay\n", This, Count, Rects, Flags, (DWORD) Color, Z, Stencil);
4312
4313     /* Note; D3DRECT is compatible with WINED3DRECT */
4314     return IWineD3DDevice_Clear(This->wineD3DDevice, Count, (WINED3DRECT*) Rects, Flags, Color, Z, Stencil);
4315 }
4316
4317 /*****************************************************************************
4318  * IDirect3DDevice7::SetViewport
4319  *
4320  * Sets the current viewport.
4321  *
4322  * Version 7 only, but IDirect3DViewport uses this call for older
4323  * versions
4324  *
4325  * Params:
4326  *  Data: The new viewport to set
4327  *
4328  * Returns:
4329  *  D3D_OK on success
4330  *  DDERR_INVALIDPARAMS if Data is NULL
4331  *  For more details, see IWineDDDevice::SetViewport
4332  *
4333  *****************************************************************************/
4334 static HRESULT WINAPI
4335 IDirect3DDeviceImpl_7_SetViewport(IDirect3DDevice7 *iface,
4336                                   D3DVIEWPORT7 *Data)
4337 {
4338     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4339     TRACE("(%p)->(%p) Relay!\n", This, Data);
4340
4341     if(!Data)
4342         return DDERR_INVALIDPARAMS;
4343
4344     /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4345     return IWineD3DDevice_SetViewport(This->wineD3DDevice,
4346                                       (WINED3DVIEWPORT*) Data);
4347 }
4348
4349 /*****************************************************************************
4350  * IDirect3DDevice::GetViewport
4351  *
4352  * Returns the current viewport
4353  *
4354  * Version 7
4355  *
4356  * Params:
4357  *  Data: D3D7Viewport structure to write the viewport information to
4358  *
4359  * Returns:
4360  *  D3D_OK on success
4361  *  DDERR_INVALIDPARAMS if Data is NULL
4362  *  For more details, see IWineD3DDevice::GetViewport
4363  *
4364  *****************************************************************************/
4365 static HRESULT WINAPI
4366 IDirect3DDeviceImpl_7_GetViewport(IDirect3DDevice7 *iface,
4367                                   D3DVIEWPORT7 *Data)
4368 {
4369     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4370     HRESULT hr;
4371     TRACE("(%p)->(%p) Relay!\n", This, Data);
4372
4373     if(!Data)
4374         return DDERR_INVALIDPARAMS;
4375
4376     /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4377     hr = IWineD3DDevice_GetViewport(This->wineD3DDevice,
4378                                     (WINED3DVIEWPORT*) Data);
4379
4380     return hr_ddraw_from_wined3d(hr);
4381 }
4382
4383 /*****************************************************************************
4384  * IDirect3DDevice7::SetMaterial
4385  *
4386  * Sets the Material
4387  *
4388  * Version 7
4389  *
4390  * Params:
4391  *  Mat: The material to set
4392  *
4393  * Returns:
4394  *  D3D_OK on success
4395  *  DDERR_INVALIDPARAMS if Mat is NULL.
4396  *  For more details, see IWineD3DDevice::SetMaterial
4397  *
4398  *****************************************************************************/
4399 static HRESULT WINAPI
4400 IDirect3DDeviceImpl_7_SetMaterial(IDirect3DDevice7 *iface,
4401                                   D3DMATERIAL7 *Mat)
4402 {
4403     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4404     HRESULT hr;
4405     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4406
4407     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */
4408     hr = IWineD3DDevice_SetMaterial(This->wineD3DDevice,
4409                                     (WINED3DMATERIAL*) Mat);
4410
4411     return hr_ddraw_from_wined3d(hr);
4412 }
4413
4414 /*****************************************************************************
4415  * IDirect3DDevice7::GetMaterial
4416  *
4417  * Returns the current material
4418  *
4419  * Version 7
4420  *
4421  * Params:
4422  *  Mat: D3DMATERIAL7 structure to write the material parameters to
4423  *
4424  * Returns:
4425  *  D3D_OK on success
4426  *  DDERR_INVALIDPARAMS if Mat is NULL
4427  *  For more details, see IWineD3DDevice::GetMaterial
4428  *
4429  *****************************************************************************/
4430 static HRESULT WINAPI
4431 IDirect3DDeviceImpl_7_GetMaterial(IDirect3DDevice7 *iface,
4432                                   D3DMATERIAL7 *Mat)
4433 {
4434     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4435     HRESULT hr;
4436     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4437
4438     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */ 
4439     hr = IWineD3DDevice_GetMaterial(This->wineD3DDevice,
4440                                     (WINED3DMATERIAL*) Mat);
4441
4442     return hr_ddraw_from_wined3d(hr);
4443 }
4444
4445 /*****************************************************************************
4446  * IDirect3DDevice7::SetLight
4447  *
4448  * Assigns a light to a light index, but doesn't activate it yet.
4449  *
4450  * Version 7, IDirect3DLight uses this method for older versions
4451  *
4452  * Params:
4453  *  LightIndex: The index of the new light
4454  *  Light: A D3DLIGHT7 structure describing the light
4455  *
4456  * Returns:
4457  *  D3D_OK on success
4458  *  For more details, see IWineD3DDevice::SetLight
4459  *
4460  *****************************************************************************/
4461 static HRESULT WINAPI
4462 IDirect3DDeviceImpl_7_SetLight(IDirect3DDevice7 *iface,
4463                                DWORD LightIndex,
4464                                D3DLIGHT7 *Light)
4465 {
4466     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4467     HRESULT hr;
4468     TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4469
4470     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4471     hr = IWineD3DDevice_SetLight(This->wineD3DDevice,
4472                                  LightIndex,
4473                                  (WINED3DLIGHT*) Light);
4474
4475     return hr_ddraw_from_wined3d(hr);
4476 }
4477
4478 /*****************************************************************************
4479  * IDirect3DDevice7::GetLight
4480  *
4481  * Returns the light assigned to a light index
4482  *
4483  * Params:
4484  *  Light: Structure to write the light information to
4485  *
4486  * Returns:
4487  *  D3D_OK on success
4488  *  DDERR_INVALIDPARAMS if Light is NULL
4489  *  For details, see IWineD3DDevice::GetLight
4490  *
4491  *****************************************************************************/
4492 static HRESULT WINAPI
4493 IDirect3DDeviceImpl_7_GetLight(IDirect3DDevice7 *iface,
4494                                DWORD LightIndex,
4495                                D3DLIGHT7 *Light)
4496 {
4497     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4498     HRESULT rc;
4499     TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4500
4501     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4502     rc =  IWineD3DDevice_GetLight(This->wineD3DDevice,
4503                                   LightIndex,
4504                                   (WINED3DLIGHT*) Light);
4505
4506     /* Translate the result. WineD3D returns other values than D3D7 */
4507     return hr_ddraw_from_wined3d(rc);
4508 }
4509
4510 /*****************************************************************************
4511  * IDirect3DDevice7::BeginStateBlock
4512  *
4513  * Begins recording to a stateblock
4514  *
4515  * Version 7
4516  *
4517  * Returns:
4518  *  D3D_OK on success
4519  *  For details see IWineD3DDevice::BeginStateBlock
4520  *
4521  *****************************************************************************/
4522 static HRESULT WINAPI
4523 IDirect3DDeviceImpl_7_BeginStateBlock(IDirect3DDevice7 *iface)
4524 {
4525     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4526     HRESULT hr;
4527     TRACE("(%p)->(): Relay!\n", This);
4528
4529     hr = IWineD3DDevice_BeginStateBlock(This->wineD3DDevice);
4530     return hr_ddraw_from_wined3d(hr);
4531 }
4532
4533 /*****************************************************************************
4534  * IDirect3DDevice7::EndStateBlock
4535  *
4536  * Stops recording to a state block and returns the created stateblock
4537  * handle.
4538  *
4539  * Version 7
4540  *
4541  * Params:
4542  *  BlockHandle: Address to store the stateblock's handle to
4543  *
4544  * Returns:
4545  *  D3D_OK on success
4546  *  DDERR_INVALIDPARAMS if BlockHandle is NULL
4547  *  See IWineD3DDevice::EndStateBlock for more details
4548  *
4549  *****************************************************************************/
4550 static HRESULT WINAPI
4551 IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
4552                                     DWORD *BlockHandle)
4553 {
4554     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4555     HRESULT hr;
4556     TRACE("(%p)->(%p): Relay!\n", This, BlockHandle);
4557
4558     if(!BlockHandle)
4559     {
4560         WARN("BlockHandle == NULL, returning DDERR_INVALIDPARAMS\n");
4561         return DDERR_INVALIDPARAMS;
4562     }
4563
4564     *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
4565     if(!*BlockHandle)
4566     {
4567         ERR("Cannot get a handle number for the stateblock\n");
4568         return DDERR_OUTOFMEMORY;
4569     }
4570     This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
4571     hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice,
4572                                       (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr);
4573     return hr_ddraw_from_wined3d(hr);
4574 }
4575
4576 /*****************************************************************************
4577  * IDirect3DDevice7::PreLoad
4578  *
4579  * Allows the app to signal that a texture will be used soon, to allow
4580  * the Direct3DDevice to load it to the video card in the meantime.
4581  *
4582  * Version 7
4583  *
4584  * Params:
4585  *  Texture: The texture to preload
4586  *
4587  * Returns:
4588  *  D3D_OK on success
4589  *  DDERR_INVALIDPARAMS if Texture is NULL
4590  *  See IWineD3DSurface::PreLoad for details
4591  *
4592  *****************************************************************************/
4593 static HRESULT WINAPI
4594 IDirect3DDeviceImpl_7_PreLoad(IDirect3DDevice7 *iface,
4595                               IDirectDrawSurface7 *Texture)
4596 {
4597     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4598     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
4599
4600     TRACE("(%p)->(%p): Relay!\n", This, surf);
4601
4602     if(!Texture)
4603         return DDERR_INVALIDPARAMS;
4604
4605     IWineD3DSurface_PreLoad(surf->WineD3DSurface);
4606     return D3D_OK;
4607 }
4608
4609 /*****************************************************************************
4610  * IDirect3DDevice7::ApplyStateBlock
4611  *
4612  * Activates the state stored in a state block handle.
4613  *
4614  * Params:
4615  *  BlockHandle: The stateblock handle to activate
4616  *
4617  * Returns:
4618  *  D3D_OK on success
4619  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4620  *
4621  *****************************************************************************/
4622 static HRESULT WINAPI
4623 IDirect3DDeviceImpl_7_ApplyStateBlock(IDirect3DDevice7 *iface,
4624                                       DWORD BlockHandle)
4625 {
4626     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4627     HRESULT hr;
4628     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4629
4630     if(!BlockHandle || BlockHandle > This->numHandles)
4631     {
4632         WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4633         return D3DERR_INVALIDSTATEBLOCK;
4634     }
4635     if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
4636     {
4637         WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4638         return D3DERR_INVALIDSTATEBLOCK;
4639     }
4640
4641     hr = IWineD3DStateBlock_Apply((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4642     return hr_ddraw_from_wined3d(hr);
4643 }
4644
4645 /*****************************************************************************
4646  * IDirect3DDevice7::CaptureStateBlock
4647  *
4648  * Updates a stateblock's values to the values currently set for the device
4649  *
4650  * Version 7
4651  *
4652  * Params:
4653  *  BlockHandle: Stateblock to update
4654  *
4655  * Returns:
4656  *  D3D_OK on success
4657  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4658  *  See IWineD3DDevice::CaptureStateBlock for more details
4659  *
4660  *****************************************************************************/
4661 static HRESULT WINAPI
4662 IDirect3DDeviceImpl_7_CaptureStateBlock(IDirect3DDevice7 *iface,
4663                                         DWORD BlockHandle)
4664 {
4665     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4666     HRESULT hr;
4667     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4668
4669     if(BlockHandle == 0 || BlockHandle > This->numHandles)
4670     {
4671         WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4672         return D3DERR_INVALIDSTATEBLOCK;
4673     }
4674     if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
4675     {
4676         WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4677         return D3DERR_INVALIDSTATEBLOCK;
4678     }
4679
4680     hr = IWineD3DStateBlock_Capture((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4681     return hr_ddraw_from_wined3d(hr);
4682 }
4683
4684 /*****************************************************************************
4685  * IDirect3DDevice7::DeleteStateBlock
4686  *
4687  * Deletes a stateblock handle. This means releasing the WineD3DStateBlock
4688  *
4689  * Version 7
4690  *
4691  * Params:
4692  *  BlockHandle: Stateblock handle to delete
4693  *
4694  * Returns:
4695  *  D3D_OK on success
4696  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is 0
4697  *
4698  *****************************************************************************/
4699 static HRESULT WINAPI
4700 IDirect3DDeviceImpl_7_DeleteStateBlock(IDirect3DDevice7 *iface,
4701                                        DWORD BlockHandle)
4702 {
4703     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4704     ULONG ref;
4705     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4706
4707     if(BlockHandle == 0 || BlockHandle > This->numHandles)
4708     {
4709         WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4710         return D3DERR_INVALIDSTATEBLOCK;
4711     }
4712     if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
4713     {
4714         WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4715         return D3DERR_INVALIDSTATEBLOCK;
4716     }
4717
4718     ref = IWineD3DStateBlock_Release((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4719     if(ref)
4720     {
4721         ERR("Something is still holding the stateblock %p(Handle %d). Ref = %d\n", This->Handles[BlockHandle - 1].ptr, BlockHandle, ref);
4722     }
4723     This->Handles[BlockHandle - 1].ptr = NULL;
4724     This->Handles[BlockHandle - 1].type = DDrawHandle_Unknown;
4725
4726     return D3D_OK;
4727 }
4728
4729 /*****************************************************************************
4730  * IDirect3DDevice7::CreateStateBlock
4731  *
4732  * Creates a new state block handle.
4733  *
4734  * Version 7
4735  *
4736  * Params:
4737  *  Type: The state block type
4738  *  BlockHandle: Address to write the created handle to
4739  *
4740  * Returns:
4741  *   D3D_OK on success
4742  *   DDERR_INVALIDPARAMS if BlockHandle is NULL
4743  *
4744  *****************************************************************************/
4745 static HRESULT WINAPI
4746 IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
4747                                        D3DSTATEBLOCKTYPE Type,
4748                                        DWORD *BlockHandle)
4749 {
4750     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4751     HRESULT hr;
4752     TRACE("(%p)->(%08x,%p)!\n", This, Type, BlockHandle);
4753
4754     if(!BlockHandle)
4755     {
4756         WARN("BlockHandle == NULL, returning DDERR_INVALIDPARAMS\n");
4757         return DDERR_INVALIDPARAMS;
4758     }
4759
4760     *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
4761     if(!*BlockHandle)
4762     {
4763         ERR("Cannot get a handle number for the stateblock\n");
4764         return DDERR_OUTOFMEMORY;
4765     }
4766     This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
4767
4768     /* The D3DSTATEBLOCKTYPE enum is fine here */
4769     hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice,
4770                                          Type,
4771                                          (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr,
4772                                          NULL /* Parent, hope that works */);
4773     return hr_ddraw_from_wined3d(hr);
4774 }
4775
4776 /*****************************************************************************
4777  * IDirect3DDevice7::Load
4778  *
4779  * Loads a rectangular area from the source into the destination texture.
4780  * It can also copy the source to the faces of a cubic environment map
4781  *
4782  * Version 7
4783  *
4784  * Params:
4785  *  DestTex: Destination texture
4786  *  DestPoint: Point in the destination where the source image should be
4787  *             written to
4788  *  SrcTex: Source texture
4789  *  SrcRect: Source rectangle
4790  *  Flags: Some flags
4791  *
4792  * Returns:
4793  *  D3D_OK on success
4794  *  DDERR_INVALIDPARAMS if DestTex or SrcTex are NULL
4795  *  See IDirect3DTexture2::Load for details
4796  *
4797  *****************************************************************************/
4798 static HRESULT WINAPI
4799 IDirect3DDeviceImpl_7_Load(IDirect3DDevice7 *iface,
4800                            IDirectDrawSurface7 *DestTex,
4801                            POINT *DestPoint,
4802                            IDirectDrawSurface7 *SrcTex,
4803                            RECT *SrcRect,
4804                            DWORD Flags)
4805 {
4806     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4807     IDirectDrawSurfaceImpl *dest = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestTex);
4808     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcTex);
4809     FIXME("(%p)->(%p,%p,%p,%p,%08x): Partially Implemented!\n", This, dest, DestPoint, src, SrcRect, Flags);
4810
4811     if( (!src) || (!dest) )
4812         return DDERR_INVALIDPARAMS;
4813
4814     IDirect3DTexture2_Load(ICOM_INTERFACE(dest, IDirect3DTexture2),
4815                            ICOM_INTERFACE(src, IDirect3DTexture2));
4816     return D3D_OK;
4817 }
4818
4819 /*****************************************************************************
4820  * IDirect3DDevice7::LightEnable
4821  *
4822  * Enables or disables a light
4823  *
4824  * Version 7, IDirect3DLight uses this method too.
4825  *
4826  * Params:
4827  *  LightIndex: The index of the light to enable / disable
4828  *  Enable: Enable or disable the light
4829  *
4830  * Returns:
4831  *  D3D_OK on success
4832  *  For more details, see IWineD3DDevice::SetLightEnable
4833  *
4834  *****************************************************************************/
4835 static HRESULT WINAPI
4836 IDirect3DDeviceImpl_7_LightEnable(IDirect3DDevice7 *iface,
4837                                   DWORD LightIndex,
4838                                   BOOL Enable)
4839 {
4840     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4841     HRESULT hr;
4842     TRACE("(%p)->(%08x,%d): Relay!\n", This, LightIndex, Enable);
4843
4844     hr = IWineD3DDevice_SetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4845     return hr_ddraw_from_wined3d(hr);
4846 }
4847
4848 /*****************************************************************************
4849  * IDirect3DDevice7::GetLightEnable
4850  *
4851  * Retrieves if the light with the given index is enabled or not
4852  *
4853  * Version 7
4854  *
4855  * Params:
4856  *  LightIndex: Index of desired light
4857  *  Enable: Pointer to a BOOL which contains the result
4858  *
4859  * Returns:
4860  *  D3D_OK on success
4861  *  DDERR_INVALIDPARAMS if Enable is NULL
4862  *  See IWineD3DDevice::GetLightEnable for more details
4863  *
4864  *****************************************************************************/
4865 static HRESULT WINAPI
4866 IDirect3DDeviceImpl_7_GetLightEnable(IDirect3DDevice7 *iface,
4867                                      DWORD LightIndex,
4868                                      BOOL* Enable)
4869 {
4870     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4871     HRESULT hr;
4872     TRACE("(%p)->(%08x,%p): Relay\n", This, LightIndex, Enable);
4873
4874     if(!Enable)
4875         return DDERR_INVALIDPARAMS;
4876
4877     hr = IWineD3DDevice_GetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4878     return hr_ddraw_from_wined3d(hr);
4879 }
4880
4881 /*****************************************************************************
4882  * IDirect3DDevice7::SetClipPlane
4883  *
4884  * Sets custom clipping plane
4885  *
4886  * Version 7
4887  *
4888  * Params:
4889  *  Index: The index of the clipping plane
4890  *  PlaneEquation: An equation defining the clipping plane
4891  *
4892  * Returns:
4893  *  D3D_OK on success
4894  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4895  *  See IWineD3DDevice::SetClipPlane for more details
4896  *
4897  *****************************************************************************/
4898 static HRESULT WINAPI
4899 IDirect3DDeviceImpl_7_SetClipPlane(IDirect3DDevice7 *iface,
4900                                    DWORD Index,
4901                                    D3DVALUE* PlaneEquation)
4902 {
4903     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4904     TRACE("(%p)->(%08x,%p): Relay!\n", This, Index, PlaneEquation);
4905
4906     if(!PlaneEquation)
4907         return DDERR_INVALIDPARAMS;
4908
4909     return IWineD3DDevice_SetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4910 }
4911
4912 /*****************************************************************************
4913  * IDirect3DDevice7::GetClipPlane
4914  *
4915  * Returns the clipping plane with a specific index
4916  *
4917  * Params:
4918  *  Index: The index of the desired plane
4919  *  PlaneEquation: Address to store the plane equation to
4920  *
4921  * Returns:
4922  *  D3D_OK on success
4923  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4924  *  See IWineD3DDevice::GetClipPlane for more details
4925  *
4926  *****************************************************************************/
4927 static HRESULT WINAPI
4928 IDirect3DDeviceImpl_7_GetClipPlane(IDirect3DDevice7 *iface,
4929                                    DWORD Index,
4930                                    D3DVALUE* PlaneEquation)
4931 {
4932     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4933     TRACE("(%p)->(%d,%p): Relay!\n", This, Index, PlaneEquation);
4934
4935     if(!PlaneEquation)
4936         return DDERR_INVALIDPARAMS;
4937
4938     return IWineD3DDevice_GetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4939 }
4940
4941 /*****************************************************************************
4942  * IDirect3DDevice7::GetInfo
4943  *
4944  * Retrieves some information about the device. The DirectX sdk says that
4945  * this version returns S_FALSE for all retail builds of DirectX, that's what
4946  * this implementation does.
4947  *
4948  * Params:
4949  *  DevInfoID: Information type requested
4950  *  DevInfoStruct: Pointer to a structure to store the info to
4951  *  Size: Size of the structure
4952  *
4953  * Returns:
4954  *  S_FALSE, because it's a non-debug driver
4955  *
4956  *****************************************************************************/
4957 static HRESULT WINAPI
4958 IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
4959                               DWORD DevInfoID,
4960                               void *DevInfoStruct,
4961                               DWORD Size)
4962 {
4963     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4964     TRACE("(%p)->(%08x,%p,%08x)\n", This, DevInfoID, DevInfoStruct, Size);
4965
4966     if (TRACE_ON(d3d7))
4967     {
4968         TRACE(" info requested : ");
4969         switch (DevInfoID)
4970         {
4971             case D3DDEVINFOID_TEXTUREMANAGER: TRACE("D3DDEVINFOID_TEXTUREMANAGER\n"); break;
4972             case D3DDEVINFOID_D3DTEXTUREMANAGER: TRACE("D3DDEVINFOID_D3DTEXTUREMANAGER\n"); break;
4973             case D3DDEVINFOID_TEXTURING: TRACE("D3DDEVINFOID_TEXTURING\n"); break;
4974             default: ERR(" invalid flag !!!\n"); return DDERR_INVALIDPARAMS;
4975         }
4976     }
4977
4978     return S_FALSE; /* According to MSDN, this is valid for a non-debug driver */
4979 }
4980
4981 const IDirect3DDevice7Vtbl IDirect3DDevice7_Vtbl =
4982 {
4983     /*** IUnknown Methods ***/
4984     IDirect3DDeviceImpl_7_QueryInterface,
4985     IDirect3DDeviceImpl_7_AddRef,
4986     IDirect3DDeviceImpl_7_Release,
4987     /*** IDirect3DDevice7 ***/
4988     IDirect3DDeviceImpl_7_GetCaps,
4989     IDirect3DDeviceImpl_7_EnumTextureFormats,
4990     IDirect3DDeviceImpl_7_BeginScene,
4991     IDirect3DDeviceImpl_7_EndScene,
4992     IDirect3DDeviceImpl_7_GetDirect3D,
4993     IDirect3DDeviceImpl_7_SetRenderTarget,
4994     IDirect3DDeviceImpl_7_GetRenderTarget,
4995     IDirect3DDeviceImpl_7_Clear,
4996     IDirect3DDeviceImpl_7_SetTransform,
4997     IDirect3DDeviceImpl_7_GetTransform,
4998     IDirect3DDeviceImpl_7_SetViewport,
4999     IDirect3DDeviceImpl_7_MultiplyTransform,
5000     IDirect3DDeviceImpl_7_GetViewport,
5001     IDirect3DDeviceImpl_7_SetMaterial,
5002     IDirect3DDeviceImpl_7_GetMaterial,
5003     IDirect3DDeviceImpl_7_SetLight,
5004     IDirect3DDeviceImpl_7_GetLight,
5005     IDirect3DDeviceImpl_7_SetRenderState,
5006     IDirect3DDeviceImpl_7_GetRenderState,
5007     IDirect3DDeviceImpl_7_BeginStateBlock,
5008     IDirect3DDeviceImpl_7_EndStateBlock,
5009     IDirect3DDeviceImpl_7_PreLoad,
5010     IDirect3DDeviceImpl_7_DrawPrimitive,
5011     IDirect3DDeviceImpl_7_DrawIndexedPrimitive,
5012     IDirect3DDeviceImpl_7_SetClipStatus,
5013     IDirect3DDeviceImpl_7_GetClipStatus,
5014     IDirect3DDeviceImpl_7_DrawPrimitiveStrided,
5015     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided,
5016     IDirect3DDeviceImpl_7_DrawPrimitiveVB,
5017     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB,
5018     IDirect3DDeviceImpl_7_ComputeSphereVisibility,
5019     IDirect3DDeviceImpl_7_GetTexture,
5020     IDirect3DDeviceImpl_7_SetTexture,
5021     IDirect3DDeviceImpl_7_GetTextureStageState,
5022     IDirect3DDeviceImpl_7_SetTextureStageState,
5023     IDirect3DDeviceImpl_7_ValidateDevice,
5024     IDirect3DDeviceImpl_7_ApplyStateBlock,
5025     IDirect3DDeviceImpl_7_CaptureStateBlock,
5026     IDirect3DDeviceImpl_7_DeleteStateBlock,
5027     IDirect3DDeviceImpl_7_CreateStateBlock,
5028     IDirect3DDeviceImpl_7_Load,
5029     IDirect3DDeviceImpl_7_LightEnable,
5030     IDirect3DDeviceImpl_7_GetLightEnable,
5031     IDirect3DDeviceImpl_7_SetClipPlane,
5032     IDirect3DDeviceImpl_7_GetClipPlane,
5033     IDirect3DDeviceImpl_7_GetInfo
5034 };
5035
5036 const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
5037 {
5038     /*** IUnknown Methods ***/
5039     Thunk_IDirect3DDeviceImpl_3_QueryInterface,
5040     Thunk_IDirect3DDeviceImpl_3_AddRef,
5041     Thunk_IDirect3DDeviceImpl_3_Release,
5042     /*** IDirect3DDevice3 ***/
5043     IDirect3DDeviceImpl_3_GetCaps,
5044     IDirect3DDeviceImpl_3_GetStats,
5045     IDirect3DDeviceImpl_3_AddViewport,
5046     IDirect3DDeviceImpl_3_DeleteViewport,
5047     IDirect3DDeviceImpl_3_NextViewport,
5048     Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats,
5049     Thunk_IDirect3DDeviceImpl_3_BeginScene,
5050     Thunk_IDirect3DDeviceImpl_3_EndScene,
5051     Thunk_IDirect3DDeviceImpl_3_GetDirect3D,
5052     IDirect3DDeviceImpl_3_SetCurrentViewport,
5053     IDirect3DDeviceImpl_3_GetCurrentViewport,
5054     Thunk_IDirect3DDeviceImpl_3_SetRenderTarget,
5055     Thunk_IDirect3DDeviceImpl_3_GetRenderTarget,
5056     IDirect3DDeviceImpl_3_Begin,
5057     IDirect3DDeviceImpl_3_BeginIndexed,
5058     IDirect3DDeviceImpl_3_Vertex,
5059     IDirect3DDeviceImpl_3_Index,
5060     IDirect3DDeviceImpl_3_End,
5061     Thunk_IDirect3DDeviceImpl_3_GetRenderState,
5062     Thunk_IDirect3DDeviceImpl_3_SetRenderState,
5063     IDirect3DDeviceImpl_3_GetLightState,
5064     IDirect3DDeviceImpl_3_SetLightState,
5065     Thunk_IDirect3DDeviceImpl_3_SetTransform,
5066     Thunk_IDirect3DDeviceImpl_3_GetTransform,
5067     Thunk_IDirect3DDeviceImpl_3_MultiplyTransform,
5068     Thunk_IDirect3DDeviceImpl_3_DrawPrimitive,
5069     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive,
5070     Thunk_IDirect3DDeviceImpl_3_SetClipStatus,
5071     Thunk_IDirect3DDeviceImpl_3_GetClipStatus,
5072     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided,
5073     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided,
5074     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB,
5075     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB,
5076     Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility,
5077     Thunk_IDirect3DDeviceImpl_3_GetTexture,
5078     Thunk_IDirect3DDeviceImpl_3_SetTexture,
5079     Thunk_IDirect3DDeviceImpl_3_GetTextureStageState,
5080     Thunk_IDirect3DDeviceImpl_3_SetTextureStageState,
5081     Thunk_IDirect3DDeviceImpl_3_ValidateDevice
5082 };
5083
5084 const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
5085 {
5086     /*** IUnknown Methods ***/
5087     Thunk_IDirect3DDeviceImpl_2_QueryInterface,
5088     Thunk_IDirect3DDeviceImpl_2_AddRef,
5089     Thunk_IDirect3DDeviceImpl_2_Release,
5090     /*** IDirect3DDevice2 ***/
5091     Thunk_IDirect3DDeviceImpl_2_GetCaps,
5092     IDirect3DDeviceImpl_2_SwapTextureHandles,
5093     Thunk_IDirect3DDeviceImpl_2_GetStats,
5094     Thunk_IDirect3DDeviceImpl_2_AddViewport,
5095     Thunk_IDirect3DDeviceImpl_2_DeleteViewport,
5096     Thunk_IDirect3DDeviceImpl_2_NextViewport,
5097     IDirect3DDeviceImpl_2_EnumTextureFormats,
5098     Thunk_IDirect3DDeviceImpl_2_BeginScene,
5099     Thunk_IDirect3DDeviceImpl_2_EndScene,
5100     Thunk_IDirect3DDeviceImpl_2_GetDirect3D,
5101     Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport,
5102     Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport,
5103     Thunk_IDirect3DDeviceImpl_2_SetRenderTarget,
5104     Thunk_IDirect3DDeviceImpl_2_GetRenderTarget,
5105     Thunk_IDirect3DDeviceImpl_2_Begin,
5106     Thunk_IDirect3DDeviceImpl_2_BeginIndexed,
5107     Thunk_IDirect3DDeviceImpl_2_Vertex,
5108     Thunk_IDirect3DDeviceImpl_2_Index,
5109     Thunk_IDirect3DDeviceImpl_2_End,
5110     Thunk_IDirect3DDeviceImpl_2_GetRenderState,
5111     Thunk_IDirect3DDeviceImpl_2_SetRenderState,
5112     Thunk_IDirect3DDeviceImpl_2_GetLightState,
5113     Thunk_IDirect3DDeviceImpl_2_SetLightState,
5114     Thunk_IDirect3DDeviceImpl_2_SetTransform,
5115     Thunk_IDirect3DDeviceImpl_2_GetTransform,
5116     Thunk_IDirect3DDeviceImpl_2_MultiplyTransform,
5117     Thunk_IDirect3DDeviceImpl_2_DrawPrimitive,
5118     Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive,
5119     Thunk_IDirect3DDeviceImpl_2_SetClipStatus,
5120     Thunk_IDirect3DDeviceImpl_2_GetClipStatus
5121 };
5122
5123 const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
5124 {
5125     /*** IUnknown Methods ***/
5126     Thunk_IDirect3DDeviceImpl_1_QueryInterface,
5127     Thunk_IDirect3DDeviceImpl_1_AddRef,
5128     Thunk_IDirect3DDeviceImpl_1_Release,
5129     /*** IDirect3DDevice1 ***/
5130     IDirect3DDeviceImpl_1_Initialize,
5131     Thunk_IDirect3DDeviceImpl_1_GetCaps,
5132     Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles,
5133     IDirect3DDeviceImpl_1_CreateExecuteBuffer,
5134     Thunk_IDirect3DDeviceImpl_1_GetStats,
5135     IDirect3DDeviceImpl_1_Execute,
5136     Thunk_IDirect3DDeviceImpl_1_AddViewport,
5137     Thunk_IDirect3DDeviceImpl_1_DeleteViewport,
5138     Thunk_IDirect3DDeviceImpl_1_NextViewport,
5139     IDirect3DDeviceImpl_1_Pick,
5140     IDirect3DDeviceImpl_1_GetPickRecords,
5141     Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats,
5142     IDirect3DDeviceImpl_1_CreateMatrix,
5143     IDirect3DDeviceImpl_1_SetMatrix,
5144     IDirect3DDeviceImpl_1_GetMatrix,
5145     IDirect3DDeviceImpl_1_DeleteMatrix,
5146     Thunk_IDirect3DDeviceImpl_1_EndScene,
5147     Thunk_IDirect3DDeviceImpl_1_BeginScene,
5148     Thunk_IDirect3DDeviceImpl_1_GetDirect3D
5149 };
5150
5151 /*****************************************************************************
5152  * IDirect3DDeviceImpl_CreateHandle
5153  *
5154  * Not called from the VTable
5155  *
5156  * Some older interface versions operate with handles, which are basically
5157  * DWORDs which identify an interface, for example
5158  * IDirect3DDevice::SetRenderState with DIRECT3DRENDERSTATE_TEXTUREHANDLE
5159  *
5160  * Those handle could be just casts to the interface pointers or vice versa,
5161  * but that is not 64 bit safe and would mean blindly derefering a DWORD
5162  * passed by the app. Instead there is a dynamic array in the device which
5163  * keeps a DWORD to pointer information and a type for the handle.
5164  *
5165  * Basically this array only grows, when a handle is freed its pointer is
5166  * just set to NULL. There will be much more reads from the array than
5167  * insertion operations, so a dynamic array is fine.
5168  *
5169  * Params:
5170  *  This: D3DDevice implementation for which this handle should be created
5171  *
5172  * Returns:
5173  *  A free handle on success
5174  *  0 on failure
5175  *
5176  *****************************************************************************/
5177 DWORD
5178 IDirect3DDeviceImpl_CreateHandle(IDirect3DDeviceImpl *This)
5179 {
5180     DWORD i;
5181     struct HandleEntry *oldHandles = This->Handles;
5182
5183     TRACE("(%p)\n", This);
5184
5185     for(i = 0; i < This->numHandles; i++)
5186     {
5187         if(This->Handles[i].ptr == NULL &&
5188            This->Handles[i].type == DDrawHandle_Unknown)
5189         {
5190             TRACE("Reusing freed handle %d\n", i + 1);
5191             return i + 1;
5192         }
5193     }
5194
5195     TRACE("Growing the handle array\n");
5196
5197     This->numHandles++;
5198     This->Handles = HeapAlloc(GetProcessHeap(), 0, sizeof(struct HandleEntry) * This->numHandles);
5199     if(!This->Handles)
5200     {
5201         ERR("Out of memory\n");
5202         This->Handles = oldHandles;
5203         This->numHandles--;
5204         return 0;
5205     }
5206     if(oldHandles)
5207     {
5208         memcpy(This->Handles, oldHandles, (This->numHandles - 1) * sizeof(struct HandleEntry));
5209         HeapFree(GetProcessHeap(), 0, oldHandles);
5210     }
5211
5212     TRACE("Returning %d\n", This->numHandles);
5213     return This->numHandles;
5214 }
5215
5216 /*****************************************************************************
5217  * IDirect3DDeviceImpl_UpdateDepthStencil
5218  *
5219  * Checks the current render target for attached depth stencils and sets the
5220  * WineD3D depth stencil accordingly.
5221  *
5222  * Returns:
5223  *  The depth stencil state to set if creating the device
5224  *
5225  *****************************************************************************/
5226 WINED3DZBUFFERTYPE
5227 IDirect3DDeviceImpl_UpdateDepthStencil(IDirect3DDeviceImpl *This)
5228 {
5229     IDirectDrawSurface7 *depthStencil = NULL;
5230     IDirectDrawSurfaceImpl *dsi;
5231     static DDSCAPS2 depthcaps = { DDSCAPS_ZBUFFER, 0, 0, 0 };
5232
5233     IDirectDrawSurface7_GetAttachedSurface(ICOM_INTERFACE(This->target, IDirectDrawSurface7),
5234                                            &depthcaps,
5235                                            &depthStencil);
5236     if(!depthStencil)
5237     {
5238         TRACE("Setting wined3d depth stencil to NULL\n");
5239         IWineD3DDevice_SetDepthStencilSurface(This->wineD3DDevice,
5240                                               NULL);
5241         return WINED3DZB_FALSE;
5242     }
5243
5244     dsi = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, depthStencil);
5245     TRACE("Setting wined3d depth stencil to %p (wined3d %p)\n", dsi, dsi->WineD3DSurface);
5246     IWineD3DDevice_SetDepthStencilSurface(This->wineD3DDevice,
5247                                           dsi->WineD3DSurface);
5248
5249     IDirectDrawSurface7_Release(depthStencil);
5250     return WINED3DZB_TRUE;
5251 }