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