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