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