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