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