quartz: Use proper alloc/free functions for COM objects.
[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  *  D3DERR_SCENE_IN_SCENE if WineD3D returns an error(Only in case of an already
1432  *  started scene).
1433  *
1434  *****************************************************************************/
1435 static HRESULT WINAPI
1436 IDirect3DDeviceImpl_7_BeginScene(IDirect3DDevice7 *iface)
1437 {
1438     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1439     HRESULT hr;
1440     TRACE("(%p): Relay\n", This);
1441
1442     hr = IWineD3DDevice_BeginScene(This->wineD3DDevice);
1443     if(hr == WINED3D_OK) return D3D_OK;
1444     else return D3DERR_SCENE_IN_SCENE; /* TODO: Other possible causes of failure */
1445 }
1446
1447 static HRESULT WINAPI
1448 Thunk_IDirect3DDeviceImpl_3_BeginScene(IDirect3DDevice3 *iface)
1449 {
1450     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1451     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1452     return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1453 }
1454
1455 static HRESULT WINAPI
1456 Thunk_IDirect3DDeviceImpl_2_BeginScene(IDirect3DDevice2 *iface)
1457 {
1458     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1459     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1460     return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1461 }
1462
1463 static HRESULT WINAPI
1464 Thunk_IDirect3DDeviceImpl_1_BeginScene(IDirect3DDevice *iface)
1465 {
1466     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1467     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1468     return IDirect3DDevice7_BeginScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1469 }
1470
1471 /*****************************************************************************
1472  * IDirect3DDevice7::EndScene
1473  *
1474  * Ends a scene that has been begun with IDirect3DDevice7::BeginScene.
1475  * This method must be called after rendering is finished.
1476  *
1477  * Version 1, 2, 3 and 7
1478  *
1479  * Returns:
1480  *  D3D_OK on success, for details see IWineD3DDevice::EndScene
1481  *  D3DERR_SCENE_NOT_IN_SCENE is returned if WineD3D returns an error. It does
1482  *  that only if the scene was already ended.
1483  *
1484  *****************************************************************************/
1485 static HRESULT WINAPI
1486 IDirect3DDeviceImpl_7_EndScene(IDirect3DDevice7 *iface)
1487 {
1488     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1489     HRESULT hr;
1490     TRACE("(%p): Relay\n", This);
1491
1492     hr = IWineD3DDevice_EndScene(This->wineD3DDevice);
1493     if(hr == WINED3D_OK) return D3D_OK;
1494     else return D3DERR_SCENE_NOT_IN_SCENE;
1495 }
1496
1497 static HRESULT WINAPI
1498 Thunk_IDirect3DDeviceImpl_3_EndScene(IDirect3DDevice3 *iface)
1499 {
1500     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1501     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1502     return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1503 }
1504
1505 static HRESULT WINAPI
1506 Thunk_IDirect3DDeviceImpl_2_EndScene(IDirect3DDevice2 *iface)
1507 {
1508     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1509     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1510     return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1511 }
1512
1513 static HRESULT WINAPI
1514 Thunk_IDirect3DDeviceImpl_1_EndScene(IDirect3DDevice *iface)
1515 {
1516     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1517     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
1518     return IDirect3DDevice7_EndScene(ICOM_INTERFACE(This, IDirect3DDevice7));
1519 }
1520
1521 /*****************************************************************************
1522  * IDirect3DDevice7::GetDirect3D
1523  *
1524  * Returns the IDirect3D(= interface to the DirectDraw object) used to create
1525  * this device.
1526  *
1527  * Params:
1528  *  Direct3D7: Address to store the interface pointer at
1529  *
1530  * Returns:
1531  *  D3D_OK on success
1532  *  DDERR_INVALIDPARAMS if Direct3D7 == NULL
1533  *
1534  *****************************************************************************/
1535 static HRESULT WINAPI
1536 IDirect3DDeviceImpl_7_GetDirect3D(IDirect3DDevice7 *iface,
1537                                   IDirect3D7 **Direct3D7)
1538 {
1539     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1540     TRACE("(%p)->(%p)\n", This, Direct3D7);
1541
1542     if(!Direct3D7)
1543         return DDERR_INVALIDPARAMS;
1544
1545     *Direct3D7 = ICOM_INTERFACE(This->ddraw, IDirect3D7);
1546     IDirect3D7_AddRef(*Direct3D7);
1547
1548     TRACE(" returning interface %p\n", *Direct3D7);
1549     return D3D_OK;
1550 }
1551
1552 static HRESULT WINAPI
1553 Thunk_IDirect3DDeviceImpl_3_GetDirect3D(IDirect3DDevice3 *iface,
1554                                         IDirect3D3 **Direct3D3)
1555 {
1556     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1557     HRESULT ret;
1558     IDirect3D7 *ret_ptr;
1559
1560     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D3);
1561     ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
1562                                        &ret_ptr);
1563     if(ret != D3D_OK)
1564         return ret;
1565     *Direct3D3 = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D3, ret_ptr);
1566     TRACE(" returning interface %p\n", *Direct3D3);
1567     return D3D_OK;
1568 }
1569
1570 static HRESULT WINAPI
1571 Thunk_IDirect3DDeviceImpl_2_GetDirect3D(IDirect3DDevice2 *iface,
1572                                         IDirect3D2 **Direct3D2)
1573 {
1574     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1575     HRESULT ret;
1576     IDirect3D7 *ret_ptr;
1577
1578     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D2);
1579     ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
1580                                        &ret_ptr);
1581     if(ret != D3D_OK)
1582         return ret;
1583     *Direct3D2 = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D2, ret_ptr);
1584     TRACE(" returning interface %p\n", *Direct3D2);
1585     return D3D_OK;
1586 }
1587
1588 static HRESULT WINAPI
1589 Thunk_IDirect3DDeviceImpl_1_GetDirect3D(IDirect3DDevice *iface,
1590                                         IDirect3D **Direct3D)
1591 {
1592     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice, iface);
1593     HRESULT ret;
1594     IDirect3D7 *ret_ptr;
1595
1596     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Direct3D);
1597     ret = IDirect3DDevice7_GetDirect3D(ICOM_INTERFACE(This, IDirect3DDevice7),
1598                                        &ret_ptr);
1599     if(ret != D3D_OK)
1600         return ret;
1601     *Direct3D = COM_INTERFACE_CAST(IDirectDrawImpl, IDirect3D7, IDirect3D, ret_ptr);
1602     TRACE(" returning interface %p\n", *Direct3D);
1603     return D3D_OK;
1604 }
1605
1606 /*****************************************************************************
1607  * IDirect3DDevice3::SetCurrentViewport
1608  *
1609  * Sets a Direct3DViewport as the current viewport.
1610  * For the thunks note that all viewport interface versions are equal
1611  *
1612  * Params:
1613  *  Direct3DViewport3: The viewport to set
1614  *
1615  * Version 2 and 3
1616  *
1617  * Returns:
1618  *  D3D_OK on success
1619  *  (Is a NULL viewport valid?)
1620  *
1621  *****************************************************************************/
1622 static HRESULT WINAPI
1623 IDirect3DDeviceImpl_3_SetCurrentViewport(IDirect3DDevice3 *iface,
1624                                          IDirect3DViewport3 *Direct3DViewport3)
1625 {
1626     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1627     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport3);
1628     TRACE("(%p)->(%p)\n", This, Direct3DViewport3);
1629
1630     /* Do nothing if the specified viewport is the same as the current one */
1631     if (This->current_viewport == vp )
1632       return D3D_OK;
1633
1634     /* Should check if the viewport was added or not */
1635
1636     /* Release previous viewport and AddRef the new one */
1637     if (This->current_viewport)
1638     {
1639         TRACE("ViewportImpl is at %p, interface is at %p\n", This->current_viewport, ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3));
1640         IDirect3DViewport3_Release( ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3) );
1641     }
1642     IDirect3DViewport3_AddRef(Direct3DViewport3);
1643
1644     /* Set this viewport as the current viewport */
1645     This->current_viewport = vp;
1646
1647     /* Activate this viewport */
1648     This->current_viewport->active_device = This;
1649     This->current_viewport->activate(This->current_viewport);
1650
1651     return D3D_OK;
1652 }
1653
1654 static HRESULT WINAPI
1655 Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport(IDirect3DDevice2 *iface,
1656                                                IDirect3DViewport2 *Direct3DViewport2)
1657 {
1658     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1659     IDirect3DViewportImpl *vp = ICOM_OBJECT(IDirect3DViewportImpl, IDirect3DViewport3, Direct3DViewport2);
1660     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, vp);
1661     return IDirect3DDevice3_SetCurrentViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
1662                                                ICOM_INTERFACE(vp, IDirect3DViewport3));
1663 }
1664
1665 /*****************************************************************************
1666  * IDirect3DDevice3::GetCurrentViewport
1667  *
1668  * Returns the currently active viewport.
1669  *
1670  * Version 2 and 3
1671  *
1672  * Params:
1673  *  Direct3DViewport3: Address to return the interface pointer at
1674  *
1675  * Returns:
1676  *  D3D_OK on success
1677  *  DDERR_INVALIDPARAMS if Direct3DViewport == NULL
1678  *
1679  *****************************************************************************/
1680 static HRESULT WINAPI
1681 IDirect3DDeviceImpl_3_GetCurrentViewport(IDirect3DDevice3 *iface,
1682                                          IDirect3DViewport3 **Direct3DViewport3)
1683 {
1684     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1685     TRACE("(%p)->(%p)\n", This, Direct3DViewport3);
1686
1687     if(!Direct3DViewport3)
1688         return DDERR_INVALIDPARAMS;
1689
1690     *Direct3DViewport3 = ICOM_INTERFACE(This->current_viewport, IDirect3DViewport3);
1691
1692     /* AddRef the returned viewport */
1693     if(*Direct3DViewport3) IDirect3DViewport3_AddRef(*Direct3DViewport3);
1694
1695     TRACE(" returning interface %p\n", *Direct3DViewport3);
1696
1697     return D3D_OK;
1698 }
1699
1700 static HRESULT WINAPI
1701 Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport(IDirect3DDevice2 *iface,
1702                                                IDirect3DViewport2 **Direct3DViewport2)
1703 {
1704     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1705     HRESULT hr;
1706     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, Direct3DViewport2);
1707     hr = IDirect3DDevice3_GetCurrentViewport(ICOM_INTERFACE(This, IDirect3DDevice3),
1708                                             (IDirect3DViewport3 **) Direct3DViewport2);
1709     if(hr != D3D_OK) return hr;
1710     *Direct3DViewport2 = (IDirect3DViewport2 *) COM_INTERFACE_CAST(IDirect3DViewportImpl, IDirect3DViewport3, IDirect3DViewport3, *Direct3DViewport2);
1711     return D3D_OK;
1712 }
1713
1714 /*****************************************************************************
1715  * IDirect3DDevice7::SetRenderTarget
1716  *
1717  * Sets the render target for the Direct3DDevice.
1718  * For the thunks note that IDirectDrawSurface7 == IDirectDrawSurface4 and
1719  * IDirectDrawSurface3 == IDirectDrawSurface
1720  *
1721  * Version 2, 3 and 7
1722  *
1723  * Params:
1724  *  NewTarget: Pointer to an IDirectDrawSurface7 interface to set as the new
1725  *             render target
1726  *  Flags: Some flags
1727  *
1728  * Returns:
1729  *  D3D_OK on success, for details see IWineD3DDevice::SetRenderTarget
1730  *
1731  *****************************************************************************/
1732 static HRESULT WINAPI
1733 IDirect3DDeviceImpl_7_SetRenderTarget(IDirect3DDevice7 *iface,
1734                                       IDirectDrawSurface7 *NewTarget,
1735                                       DWORD Flags)
1736 {
1737     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1738     IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, NewTarget);
1739     TRACE("(%p)->(%p,%08x): Relay\n", This, NewTarget, Flags);
1740
1741     /* Flags: Not used */
1742
1743     return IWineD3DDevice_SetRenderTarget(This->wineD3DDevice,
1744                                           0,
1745                                           Target ? Target->WineD3DSurface : NULL);
1746 }
1747
1748 static HRESULT WINAPI
1749 Thunk_IDirect3DDeviceImpl_3_SetRenderTarget(IDirect3DDevice3 *iface,
1750                                             IDirectDrawSurface4 *NewRenderTarget,
1751                                             DWORD Flags)
1752 {
1753     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1754     IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, NewRenderTarget);
1755     TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
1756     return IDirect3DDevice7_SetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1757                                             ICOM_INTERFACE(Target, IDirectDrawSurface7),
1758                                             Flags);
1759 }
1760
1761 static HRESULT WINAPI
1762 Thunk_IDirect3DDeviceImpl_2_SetRenderTarget(IDirect3DDevice2 *iface,
1763                                             IDirectDrawSurface *NewRenderTarget,
1764                                             DWORD Flags)
1765 {
1766     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1767     IDirectDrawSurfaceImpl *Target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface3, NewRenderTarget);
1768     TRACE_(ddraw_thunk)("(%p)->(%p,%08x) thunking to IDirect3DDevice7 interface.\n", This, Target, Flags);
1769     return IDirect3DDevice7_SetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1770                                             ICOM_INTERFACE(Target, IDirectDrawSurface7),
1771                                             Flags);
1772 }
1773
1774 /*****************************************************************************
1775  * IDirect3DDevice7::GetRenderTarget
1776  *
1777  * Returns the current render target.
1778  * This is handled locally, because the WineD3D render target's parent
1779  * is an IParent
1780  *
1781  * Version 2, 3 and 7
1782  *
1783  * Params:
1784  *  RenderTarget: Address to store the surface interface pointer
1785  *
1786  * Returns:
1787  *  D3D_OK on success
1788  *  DDERR_INVALIDPARAMS if RenderTarget == NULL
1789  *
1790  *****************************************************************************/
1791 static HRESULT WINAPI
1792 IDirect3DDeviceImpl_7_GetRenderTarget(IDirect3DDevice7 *iface,
1793                                       IDirectDrawSurface7 **RenderTarget)
1794 {
1795     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
1796     TRACE("(%p)->(%p): Relay\n", This, RenderTarget);
1797
1798     if(!RenderTarget)
1799         return DDERR_INVALIDPARAMS;
1800
1801     *RenderTarget = ICOM_INTERFACE(This->target, IDirectDrawSurface7);
1802     IDirectDrawSurface7_AddRef(*RenderTarget);
1803
1804     return D3D_OK;
1805 }
1806
1807 static HRESULT WINAPI
1808 Thunk_IDirect3DDeviceImpl_3_GetRenderTarget(IDirect3DDevice3 *iface,
1809                                             IDirectDrawSurface4 **RenderTarget)
1810 {
1811     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1812     HRESULT hr;
1813     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
1814     hr = IDirect3DDevice7_GetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1815                                           (IDirectDrawSurface7 **) RenderTarget);
1816     if(hr != D3D_OK) return hr;
1817     *RenderTarget = (IDirectDrawSurface4 *) COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirectDrawSurface7, *RenderTarget);
1818     return D3D_OK;
1819 }
1820
1821 static HRESULT WINAPI
1822 Thunk_IDirect3DDeviceImpl_2_GetRenderTarget(IDirect3DDevice2 *iface,
1823                                             IDirectDrawSurface **RenderTarget)
1824 {
1825     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1826     HRESULT hr;
1827     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, RenderTarget);
1828     hr = IDirect3DDevice7_GetRenderTarget(ICOM_INTERFACE(This, IDirect3DDevice7),
1829                                           (IDirectDrawSurface7 **) RenderTarget);
1830     if(hr != D3D_OK) return hr;
1831     *RenderTarget = (IDirectDrawSurface *) COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirectDrawSurface3, *RenderTarget);
1832     return D3D_OK;
1833 }
1834
1835 /*****************************************************************************
1836  * IDirect3DDevice3::Begin
1837  *
1838  * Begins a description block of vertices. This is similar to glBegin()
1839  * and glEnd(). After a call to IDirect3DDevice3::End, the vertices
1840  * described with IDirect3DDevice::Vertex are drawn.
1841  *
1842  * Version 2 and 3
1843  *
1844  * Params:
1845  *  PrimitiveType: The type of primitives to draw
1846  *  VertexTypeDesc: A flexible vertex format description of the vertices
1847  *  Flags: Some flags..
1848  *
1849  * Returns:
1850  *  D3D_OK on success
1851  *
1852  *****************************************************************************/
1853 static HRESULT WINAPI
1854 IDirect3DDeviceImpl_3_Begin(IDirect3DDevice3 *iface,
1855                             D3DPRIMITIVETYPE PrimitiveType,
1856                             DWORD VertexTypeDesc,
1857                             DWORD Flags)
1858 {
1859     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1860     TRACE("(%p)->(%d,%d,%08x)\n", This, PrimitiveType, VertexTypeDesc, Flags);
1861
1862     This->primitive_type = PrimitiveType;
1863     This->vertex_type = VertexTypeDesc;
1864     This->render_flags = Flags;
1865     This->vertex_size = get_flexible_vertex_size(This->vertex_type);
1866     This->nb_vertices = 0;
1867
1868     return D3D_OK;
1869 }
1870
1871 static HRESULT WINAPI
1872 Thunk_IDirect3DDeviceImpl_2_Begin(IDirect3DDevice2 *iface,
1873                                   D3DPRIMITIVETYPE d3dpt,
1874                                   D3DVERTEXTYPE dwVertexTypeDesc,
1875                                   DWORD dwFlags)
1876 {
1877     DWORD FVF;
1878     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1879     TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dpt, dwVertexTypeDesc, dwFlags);
1880
1881     switch(dwVertexTypeDesc)
1882     {
1883         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
1884         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
1885         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
1886         default:
1887             ERR("Unexpected vertex type %d\n", dwVertexTypeDesc);
1888             return DDERR_INVALIDPARAMS;  /* Should never happen */
1889     };
1890
1891     return IDirect3DDevice3_Begin(ICOM_INTERFACE(This, IDirect3DDevice3),
1892                                   d3dpt,
1893                                   FVF,
1894                                   dwFlags);
1895 }
1896
1897 /*****************************************************************************
1898  * IDirect3DDevice3::BeginIndexed
1899  *
1900  * Draws primitives based on vertices in a vertex array which are specified
1901  * by indices.
1902  *
1903  * Version 2 and 3
1904  *
1905  * Params:
1906  *  PrimitiveType: Primitive type to draw
1907  *  VertexType: A FVF description of the vertex format
1908  *  Vertices: pointer to an array containing the vertices
1909  *  NumVertices: The number of vertices in the vertex array
1910  *  Flags: Some flags ...
1911  *
1912  * Returns:
1913  *  D3D_OK, because it's a stub
1914  *
1915  *****************************************************************************/
1916 static HRESULT WINAPI
1917 IDirect3DDeviceImpl_3_BeginIndexed(IDirect3DDevice3 *iface,
1918                                    D3DPRIMITIVETYPE PrimitiveType,
1919                                    DWORD VertexType,
1920                                    void *Vertices,
1921                                    DWORD NumVertices,
1922                                    DWORD Flags)
1923 {
1924     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1925     FIXME("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, Vertices, NumVertices, Flags);
1926     return D3D_OK;
1927 }
1928
1929
1930 static HRESULT WINAPI
1931 Thunk_IDirect3DDeviceImpl_2_BeginIndexed(IDirect3DDevice2 *iface,
1932                                          D3DPRIMITIVETYPE d3dptPrimitiveType,
1933                                          D3DVERTEXTYPE d3dvtVertexType,
1934                                          void *lpvVertices,
1935                                          DWORD dwNumVertices,
1936                                          DWORD dwFlags)
1937 {
1938     DWORD FVF;
1939     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
1940     TRACE_(ddraw_thunk)("(%p/%p)->(%08x,%08x,%p,%08x,%08x): Thunking to IDirect3DDevice3\n", This, iface, d3dptPrimitiveType, d3dvtVertexType, lpvVertices, dwNumVertices, dwFlags);
1941
1942     switch(d3dvtVertexType)
1943     {
1944         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
1945         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
1946         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
1947         default:
1948             ERR("Unexpected vertex type %d\n", d3dvtVertexType);
1949             return DDERR_INVALIDPARAMS;  /* Should never happen */
1950     };
1951
1952     return IDirect3DDevice3_BeginIndexed(ICOM_INTERFACE(This,IDirect3DDevice3),
1953                                          d3dptPrimitiveType,
1954                                          FVF,
1955                                          lpvVertices,
1956                                          dwNumVertices,
1957                                          dwFlags);
1958 }
1959
1960 /*****************************************************************************
1961  * IDirect3DDevice3::Vertex
1962  *
1963  * Draws a vertex as described by IDirect3DDevice3::Begin. It places all
1964  * drawn vertices in a vertex buffer. If the buffer is too small, its
1965  * size is increased.
1966  *
1967  * Version 2 and 3
1968  *
1969  * Params:
1970  *  Vertex: Pointer to the vertex
1971  *
1972  * Returns:
1973  *  D3D_OK, on success
1974  *  DDERR_INVALIDPARAMS if Vertex is NULL
1975  *
1976  *****************************************************************************/
1977 static HRESULT WINAPI
1978 IDirect3DDeviceImpl_3_Vertex(IDirect3DDevice3 *iface,
1979                              void *Vertex)
1980 {
1981     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
1982     TRACE("(%p)->(%p)\n", This, Vertex);
1983
1984     if(!Vertex)
1985         return DDERR_INVALIDPARAMS;
1986
1987     if ((This->nb_vertices+1)*This->vertex_size > This->buffer_size)
1988     {
1989         BYTE *old_buffer;
1990         This->buffer_size = This->buffer_size ? This->buffer_size * 2 : This->vertex_size * 3;
1991         old_buffer = This->vertex_buffer;
1992         This->vertex_buffer = HeapAlloc(GetProcessHeap(), 0, This->buffer_size);
1993         if (old_buffer)
1994         {
1995             CopyMemory(This->vertex_buffer, old_buffer, This->nb_vertices * This->vertex_size);
1996             HeapFree(GetProcessHeap(), 0, old_buffer);
1997         }
1998     }
1999
2000     CopyMemory(This->vertex_buffer + This->nb_vertices++ * This->vertex_size, Vertex, This->vertex_size);
2001
2002     return D3D_OK;
2003 }
2004
2005 static HRESULT WINAPI
2006 Thunk_IDirect3DDeviceImpl_2_Vertex(IDirect3DDevice2 *iface,
2007                                    void *lpVertexType)
2008 {
2009     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2010     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice3 interface.\n", This, lpVertexType);
2011     return IDirect3DDevice3_Vertex(ICOM_INTERFACE(This, IDirect3DDevice3),
2012                                                   lpVertexType);
2013 }
2014
2015 /*****************************************************************************
2016  * IDirect3DDevice3::Index
2017  *
2018  * Specifies an index to a vertex to be drawn. The vertex array has to
2019  * be specified with BeginIndexed first.
2020  *
2021  * Parameters:
2022  *  VertexIndex: The index of the vertex to draw
2023  *
2024  * Returns:
2025  *  D3D_OK because it's a stub
2026  *
2027  *****************************************************************************/
2028 static HRESULT WINAPI
2029 IDirect3DDeviceImpl_3_Index(IDirect3DDevice3 *iface,
2030                             WORD VertexIndex)
2031 {
2032     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2033     FIXME("(%p)->(%04x): stub!\n", This, VertexIndex);
2034     return D3D_OK;
2035 }
2036
2037 static HRESULT WINAPI
2038 Thunk_IDirect3DDeviceImpl_2_Index(IDirect3DDevice2 *iface,
2039                                   WORD wVertexIndex)
2040 {
2041     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2042     TRACE_(ddraw_thunk)("(%p)->(%04x) thunking to IDirect3DDevice3 interface.\n", This, wVertexIndex);
2043     return IDirect3DDevice3_Index(ICOM_INTERFACE(This, IDirect3DDevice3),
2044                                   wVertexIndex);
2045 }
2046
2047 /*****************************************************************************
2048  * IDirect3DDevice3::End
2049  *
2050  * Ends a draw begun with IDirect3DDevice3::Begin or
2051  * IDirect3DDevice::BeginIndexed. The vertices specified with
2052  * IDirect3DDevice::Vertex or IDirect3DDevice::Index are drawn using
2053  * the IDirect3DDevice7::DrawPrimitive method. So far only
2054  * non-indexed mode is supported
2055  *
2056  * Version 2 and 3
2057  *
2058  * Params:
2059  *  Flags: Some flags, as usual. Don't know which are defined
2060  *
2061  * Returns:
2062  *  The return value of IDirect3DDevice7::DrawPrimitive
2063  *
2064  *****************************************************************************/
2065 static HRESULT WINAPI
2066 IDirect3DDeviceImpl_3_End(IDirect3DDevice3 *iface,
2067                           DWORD Flags)
2068 {
2069     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2070     TRACE("(%p)->(%08x)\n", This, Flags);
2071
2072     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2073                                           This->primitive_type, This->vertex_type,
2074                                           This->vertex_buffer, This->nb_vertices,
2075                                           This->render_flags);
2076 }
2077
2078 static HRESULT WINAPI
2079 Thunk_IDirect3DDeviceImpl_2_End(IDirect3DDevice2 *iface,
2080                                 DWORD dwFlags)
2081 {
2082     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2083     TRACE_(ddraw_thunk)("(%p)->(%08x) thunking to IDirect3DDevice3 interface.\n", This, dwFlags);
2084     return IDirect3DDevice3_End(ICOM_INTERFACE(This, IDirect3DDevice3),
2085                                 dwFlags);
2086 }
2087
2088 /*****************************************************************************
2089  * IDirect3DDevice7::GetRenderState
2090  *
2091  * Returns the value of a render state. The possible render states are
2092  * defined in include/d3dtypes.h
2093  *
2094  * Version 2, 3 and 7
2095  *
2096  * Params:
2097  *  RenderStateType: Render state to return the current setting of
2098  *  Value: Address to store the value at
2099  *
2100  * Returns:
2101  *  D3D_OK on success, for details see IWineD3DDevice::GetRenderState
2102  *  DDERR_INVALIDPARAMS if Value == NULL
2103  *
2104  *****************************************************************************/
2105 static HRESULT WINAPI
2106 IDirect3DDeviceImpl_7_GetRenderState(IDirect3DDevice7 *iface,
2107                                      D3DRENDERSTATETYPE RenderStateType,
2108                                      DWORD *Value)
2109 {
2110     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2111     HRESULT hr;
2112     TRACE("(%p)->(%08x,%p): Relay\n", This, RenderStateType, Value);
2113
2114     if(!Value)
2115         return DDERR_INVALIDPARAMS;
2116
2117     switch(RenderStateType)
2118     {
2119         case D3DRENDERSTATE_TEXTUREHANDLE:
2120         {
2121             /* This state is wrapped to SetTexture in SetRenderState, so
2122              * it has to be wrapped to GetTexture here
2123              */
2124             IWineD3DBaseTexture *tex = NULL;
2125             *Value = 0;
2126
2127             hr = IWineD3DDevice_GetTexture(This->wineD3DDevice,
2128                                            0,
2129                                            &tex);
2130
2131             if(hr == WINED3D_OK && tex)
2132             {
2133                 IDirectDrawSurface7 *parent = NULL;
2134                 hr = IWineD3DBaseTexture_GetParent(tex,
2135                                                    (IUnknown **) &parent);
2136                 if(parent)
2137                 {
2138                     /* The parent of the texture is the IDirectDrawSurface7 interface
2139                      * of the ddraw surface
2140                      */
2141                     IDirectDrawSurfaceImpl *texImpl = ICOM_OBJECT(IDirectDrawSurfaceImpl,
2142                                                                   IDirectDrawSurface7,
2143                                                                   parent);
2144                     *Value = texImpl->Handle;
2145                     IDirectDrawSurface7_Release(parent);
2146                 }
2147                 IWineD3DBaseTexture_Release(tex);
2148             }
2149             return hr;
2150         }
2151
2152         case D3DRENDERSTATE_TEXTUREMAG:
2153         {
2154             WINED3DTEXTUREFILTERTYPE tex_mag;
2155
2156             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2157                                                 0, WINED3DSAMP_MAGFILTER,
2158                                                 &tex_mag);
2159
2160             switch (tex_mag)
2161             {
2162                 case WINED3DTEXF_POINT:
2163                     *Value = D3DFILTER_NEAREST;
2164                     break;
2165                 case WINED3DTEXF_LINEAR:
2166                     *Value = D3DFILTER_LINEAR;
2167                     break;
2168                 default:
2169                     ERR("Unhandled texture mag %d !\n",tex_mag);
2170                     *Value = 0;
2171             }
2172             return hr;
2173         }
2174
2175         case D3DRENDERSTATE_TEXTUREMIN:
2176         {
2177             WINED3DTEXTUREFILTERTYPE tex_min;
2178
2179             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2180                                                 0, WINED3DSAMP_MINFILTER,
2181                                                 &tex_min);
2182
2183             switch (tex_min)
2184             {
2185                 case WINED3DTEXF_POINT:
2186                     *Value = D3DFILTER_NEAREST;
2187                     break;
2188                 case WINED3DTEXF_LINEAR:
2189                     *Value = D3DFILTER_LINEAR;
2190                     break;
2191                 default:
2192                     ERR("Unhandled texture mag %d !\n",tex_min);
2193                     *Value = 0;
2194             }
2195             return hr;
2196         }
2197
2198         case D3DRENDERSTATE_TEXTUREADDRESS:
2199         case D3DRENDERSTATE_TEXTUREADDRESSU:
2200             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2201                                                   0, WINED3DSAMP_ADDRESSU,
2202                                                   Value);
2203         case D3DRENDERSTATE_TEXTUREADDRESSV:
2204             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
2205                                                   0, WINED3DSAMP_ADDRESSV,
2206                                                   Value);
2207
2208         default:
2209             /* FIXME: Unhandled: D3DRENDERSTATE_STIPPLEPATTERN00 - 31 */
2210             return IWineD3DDevice_GetRenderState(This->wineD3DDevice,
2211                                                  RenderStateType,
2212                                                  Value);
2213     }
2214 }
2215
2216 static HRESULT WINAPI
2217 Thunk_IDirect3DDeviceImpl_3_GetRenderState(IDirect3DDevice3 *iface,
2218                                            D3DRENDERSTATETYPE dwRenderStateType,
2219                                            DWORD *lpdwRenderState)
2220 {
2221     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2222     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
2223     return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2224                                            dwRenderStateType,
2225                                            lpdwRenderState);
2226 }
2227
2228 static HRESULT WINAPI
2229 Thunk_IDirect3DDeviceImpl_2_GetRenderState(IDirect3DDevice2 *iface,
2230                                            D3DRENDERSTATETYPE dwRenderStateType,
2231                                            DWORD *lpdwRenderState)
2232 {
2233     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2234     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
2235     return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2236                                            dwRenderStateType,
2237                                            lpdwRenderState);
2238 }
2239
2240 /*****************************************************************************
2241  * IDirect3DDevice7::SetRenderState
2242  *
2243  * Sets a render state. The possible render states are defined in
2244  * include/d3dtypes.h
2245  *
2246  * Version 2, 3 and 7
2247  *
2248  * Params:
2249  *  RenderStateType: State to set
2250  *  Value: Value to assign to that state
2251  *
2252  * Returns:
2253  *  D3D_OK on success,
2254  *  for details see IWineD3DDevice::SetRenderState
2255  *
2256  *****************************************************************************/
2257 static HRESULT WINAPI
2258 IDirect3DDeviceImpl_7_SetRenderState(IDirect3DDevice7 *iface,
2259                                      D3DRENDERSTATETYPE RenderStateType,
2260                                      DWORD Value)
2261 {
2262     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2263     TRACE("(%p)->(%08x,%d): Relay\n", This, RenderStateType, Value);
2264
2265     /* Some render states need special care */
2266     switch(RenderStateType)
2267     {
2268         case D3DRENDERSTATE_TEXTUREHANDLE:
2269         {
2270             if(Value == 0)
2271             {
2272                     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
2273                                                      0,
2274                                                      NULL);
2275             }
2276
2277             if(Value > This->numHandles)
2278             {
2279                 FIXME("Specified handle %d out of range\n", Value);
2280                 return DDERR_INVALIDPARAMS;
2281             }
2282             if(This->Handles[Value - 1].type != DDrawHandle_Texture)
2283             {
2284                 FIXME("Handle %d isn't a texture handle\n", Value);
2285                 return DDERR_INVALIDPARAMS;
2286             }
2287             else
2288             {
2289                 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[Value - 1].ptr;
2290                 return IWineD3DDevice_SetTexture(This->wineD3DDevice,
2291                                                  0,
2292                                                  (IWineD3DBaseTexture *) surf->wineD3DTexture);
2293             }
2294         }
2295
2296         case D3DRENDERSTATE_TEXTUREMAG:
2297         {
2298             WINED3DTEXTUREFILTERTYPE tex_mag = WINED3DTEXF_NONE;
2299
2300             switch ((D3DTEXTUREFILTER) Value)
2301             {
2302                 case D3DFILTER_NEAREST:
2303                 case D3DFILTER_LINEARMIPNEAREST:
2304                     tex_mag = WINED3DTEXF_POINT;
2305                     break;
2306                 case D3DFILTER_LINEAR:
2307                 case D3DFILTER_LINEARMIPLINEAR:
2308                     tex_mag = WINED3DTEXF_LINEAR;
2309                     break;
2310                 default:
2311                     ERR("Unhandled texture mag %d !\n",Value);
2312             }
2313
2314             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2315                                                   0, WINED3DSAMP_MAGFILTER,
2316                                                   tex_mag);
2317         }
2318
2319         case D3DRENDERSTATE_TEXTUREMIN:
2320         {
2321             WINED3DTEXTUREFILTERTYPE tex_min = WINED3DTEXF_NONE;
2322             WINED3DTEXTUREFILTERTYPE tex_mip = WINED3DTEXF_NONE;
2323
2324             switch ((D3DTEXTUREFILTER) Value)
2325             {
2326                 case D3DFILTER_NEAREST:
2327                     tex_min = WINED3DTEXF_POINT;
2328                     break;
2329                 case D3DFILTER_LINEAR:
2330                     tex_min = WINED3DTEXF_LINEAR;
2331                     break;
2332                 case D3DFILTER_MIPNEAREST:
2333                     tex_min = WINED3DTEXF_NONE;
2334                     tex_mip = WINED3DTEXF_POINT;
2335                     break;
2336                 case D3DFILTER_MIPLINEAR:
2337                     tex_min = WINED3DTEXF_NONE;
2338                     tex_mip = WINED3DTEXF_LINEAR;
2339                     break;
2340                 case D3DFILTER_LINEARMIPNEAREST:
2341                     tex_min = WINED3DTEXF_POINT;
2342                     tex_mip = WINED3DTEXF_LINEAR;
2343                     break;
2344                 case D3DFILTER_LINEARMIPLINEAR:
2345                     tex_min = WINED3DTEXF_LINEAR;
2346                     tex_mip = WINED3DTEXF_LINEAR;
2347                     break;
2348
2349                 default:
2350                     ERR("Unhandled texture min %d !\n",Value);
2351             }
2352
2353                    IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2354                                                   0, WINED3DSAMP_MIPFILTER,
2355                                                   tex_mip);
2356             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2357                                                   0, WINED3DSAMP_MINFILTER,
2358                                                   tex_min);
2359         }
2360
2361         case D3DRENDERSTATE_TEXTUREADDRESS:
2362                    IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2363                                                   0, WINED3DSAMP_ADDRESSV,
2364                                                   Value);
2365             /* Drop through */
2366         case D3DRENDERSTATE_TEXTUREADDRESSU:
2367             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2368                                                   0, WINED3DSAMP_ADDRESSU,
2369                                                   Value);
2370         case D3DRENDERSTATE_TEXTUREADDRESSV:
2371             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2372                                                   0, WINED3DSAMP_ADDRESSV,
2373                                                   Value);
2374
2375         case D3DRENDERSTATE_TEXTUREMAPBLEND:
2376         {
2377             /* Old texture combine setup style, superseded by texture stage states
2378              * in D3D7. It is safe for us to wrap it to texture stage states.
2379              */
2380             switch ( (D3DTEXTUREBLEND) Value)
2381             {
2382                 case D3DTBLEND_MODULATE:
2383                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2384                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2385                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG2, WINED3DTA_CURRENT);
2386                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_MODULATE);
2387                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_SELECTARG1);
2388                     break;
2389
2390                 case D3DTBLEND_MODULATEALPHA:
2391                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2392                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2393                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG2, WINED3DTA_CURRENT);
2394                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG2, WINED3DTA_CURRENT);
2395                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_MODULATE);
2396                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_MODULATE);
2397                     break;
2398
2399                 case D3DTBLEND_DECAL:
2400                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2401                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2402                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_SELECTARG1);
2403                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_SELECTARG1);
2404                     break;
2405
2406                 case D3DTBLEND_DECALALPHA:
2407                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLORARG1, WINED3DTA_TEXTURE);
2408                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG1, WINED3DTA_TEXTURE);
2409                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAARG2, WINED3DTA_CURRENT);
2410                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_COLOROP, WINED3DTOP_SELECTARG1);
2411                     IWineD3DDevice_SetTextureStageState(This->wineD3DDevice, 0, WINED3DTSS_ALPHAOP, WINED3DTOP_MODULATE);
2412                     break;
2413
2414                 default:
2415                     ERR("Unhandled texture environment %d !\n",Value);
2416                 }
2417                 return D3D_OK;
2418             break;
2419         }
2420
2421         default:
2422
2423             /* FIXME: Unhandled: D3DRENDERSTATE_STIPPLEPATTERN00 - 31 */
2424
2425             return IWineD3DDevice_SetRenderState(This->wineD3DDevice,
2426                                                  RenderStateType,
2427                                                  Value);
2428     }
2429 }
2430
2431 static HRESULT WINAPI
2432 Thunk_IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
2433                                            D3DRENDERSTATETYPE RenderStateType,
2434                                            DWORD Value)
2435 {
2436     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2437     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2438     return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2439                                            RenderStateType,
2440                                            Value);
2441 }
2442
2443 static HRESULT WINAPI
2444 Thunk_IDirect3DDeviceImpl_2_SetRenderState(IDirect3DDevice2 *iface,
2445                                            D3DRENDERSTATETYPE RenderStateType,
2446                                            DWORD Value)
2447 {
2448     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2449     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2450     return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2451                                            RenderStateType,
2452                                            Value);
2453 }
2454
2455 /*****************************************************************************
2456  * Direct3DDevice3::SetLightState
2457  *
2458  * Sets a light state for Direct3DDevice3 and Direct3DDevice2. The
2459  * light states are forwarded to Direct3DDevice7 render states
2460  *
2461  * Version 2 and 3
2462  *
2463  * Params:
2464  *  LightStateType: The light state to change
2465  *  Value: The value to assign to that light state
2466  *
2467  * Returns:
2468  *  D3D_OK on success
2469  *  DDERR_INVALIDPARAMS if the parameters were incorrect
2470  *  Also check IDirect3DDevice7::SetRenderState
2471  *
2472  *****************************************************************************/
2473 static HRESULT WINAPI
2474 IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
2475                                     D3DLIGHTSTATETYPE LightStateType,
2476                                     DWORD Value)
2477 {
2478     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2479
2480     TRACE("(%p)->(%08x,%08x)\n", This, LightStateType, Value);
2481
2482     if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
2483     {
2484         TRACE("Unexpected Light State Type\n");
2485         return DDERR_INVALIDPARAMS;
2486     }
2487
2488     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
2489     {
2490         IDirect3DMaterialImpl *mat;
2491
2492         if(Value == 0) mat = NULL;
2493         else if(Value > This->numHandles)
2494         {
2495             ERR("Material handle out of range(%d)\n", Value);
2496             return DDERR_INVALIDPARAMS;
2497         }
2498         else if(This->Handles[Value - 1].type != DDrawHandle_Material)
2499         {
2500             ERR("Invalid handle %d\n", Value);
2501             return DDERR_INVALIDPARAMS;
2502         }
2503         else
2504         {
2505             mat = (IDirect3DMaterialImpl *) This->Handles[Value - 1].ptr;
2506         }
2507
2508         if (mat != NULL)
2509         {
2510             TRACE(" activating material %p.\n", mat);
2511             mat->activate(mat);
2512         }
2513         else
2514         {
2515             FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
2516         }
2517         This->material = Value;
2518     }
2519     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
2520     {
2521         switch (Value)
2522         {
2523             case D3DCOLOR_MONO:
2524                 ERR("DDCOLOR_MONO should not happen!\n");
2525                 break;
2526             case D3DCOLOR_RGB:
2527                 /* We are already in this mode */
2528                 TRACE("Setting color model to RGB (no-op).\n");
2529                 break;
2530             default:
2531                 ERR("Unknown color model!\n");
2532                 return DDERR_INVALIDPARAMS;
2533         }
2534     }
2535     else
2536     {
2537         D3DRENDERSTATETYPE rs;
2538         switch (LightStateType)
2539         {
2540             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
2541                 rs = D3DRENDERSTATE_AMBIENT;
2542                 break;          
2543             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
2544                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2545                 break;
2546             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
2547                 rs = D3DRENDERSTATE_FOGSTART;
2548                 break;
2549             case D3DLIGHTSTATE_FOGEND:        /* 6 */
2550                 rs = D3DRENDERSTATE_FOGEND;
2551                 break;
2552             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
2553                 rs = D3DRENDERSTATE_FOGDENSITY;
2554                 break;
2555             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
2556                 rs = D3DRENDERSTATE_COLORVERTEX;
2557                 break;
2558             default:
2559                 ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2560                 return DDERR_INVALIDPARAMS;
2561         }
2562
2563         return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7), 
2564                                                rs,
2565                                                Value);
2566     }
2567
2568     return D3D_OK;
2569 }
2570
2571 static HRESULT WINAPI
2572 Thunk_IDirect3DDeviceImpl_2_SetLightState(IDirect3DDevice2 *iface,
2573                                           D3DLIGHTSTATETYPE LightStateType,
2574                                           DWORD Value)
2575 {
2576     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2577     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2578     return IDirect3DDevice3_SetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
2579                                           LightStateType,
2580                                           Value);
2581 }
2582
2583 /*****************************************************************************
2584  * IDirect3DDevice3::GetLightState
2585  *
2586  * Returns the current setting of a light state. The state is read from
2587  * the Direct3DDevice7 render state.
2588  *
2589  * Version 2 and 3
2590  *
2591  * Params:
2592  *  LightStateType: The light state to return
2593  *  Value: The address to store the light state setting at
2594  *
2595  * Returns:
2596  *  D3D_OK on success
2597  *  DDDERR_INVALIDPARAMS if the parameters were incorrect
2598  *  Also see IDirect3DDevice7::GetRenderState
2599  *
2600  *****************************************************************************/
2601 static HRESULT WINAPI
2602 IDirect3DDeviceImpl_3_GetLightState(IDirect3DDevice3 *iface,
2603                                     D3DLIGHTSTATETYPE LightStateType,
2604                                     DWORD *Value)
2605 {
2606     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2607
2608     TRACE("(%p)->(%08x,%p)\n", This, LightStateType, Value);
2609
2610     if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
2611     {
2612         TRACE("Unexpected Light State Type\n");
2613         return DDERR_INVALIDPARAMS;
2614     }
2615
2616     if(!Value)
2617         return DDERR_INVALIDPARAMS;
2618
2619     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
2620     {
2621         *Value = This->material;
2622     }
2623     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
2624     {
2625         *Value = D3DCOLOR_RGB;
2626     }
2627     else
2628     {
2629         D3DRENDERSTATETYPE rs;
2630         switch (LightStateType)
2631         {
2632             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
2633                 rs = D3DRENDERSTATE_AMBIENT;
2634                 break;          
2635             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
2636                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2637                 break;
2638             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
2639                 rs = D3DRENDERSTATE_FOGSTART;
2640                 break;
2641             case D3DLIGHTSTATE_FOGEND:        /* 6 */
2642                 rs = D3DRENDERSTATE_FOGEND;
2643                 break;
2644             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
2645                 rs = D3DRENDERSTATE_FOGDENSITY;
2646                 break;
2647             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
2648                 rs = D3DRENDERSTATE_COLORVERTEX;
2649                 break;
2650             default:
2651                 ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2652                 return DDERR_INVALIDPARAMS;
2653         }
2654
2655         return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2656                                                rs,
2657                                                Value);
2658     }
2659
2660     return D3D_OK;
2661 }
2662
2663 static HRESULT WINAPI
2664 Thunk_IDirect3DDeviceImpl_2_GetLightState(IDirect3DDevice2 *iface,
2665                                           D3DLIGHTSTATETYPE LightStateType,
2666                                           DWORD *Value)
2667 {
2668     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2669     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2670     return IDirect3DDevice3_GetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
2671                                           LightStateType,
2672                                           Value);
2673 }
2674
2675 /*****************************************************************************
2676  * IDirect3DDevice7::SetTransform
2677  *
2678  * Assigns a D3DMATRIX to a transform type. The transform types are defined
2679  * in include/d3dtypes.h.
2680  * The D3DTRANSFORMSTATE_WORLD (=1) is translated to D3DTS_WORLDMATRIX(0)
2681  * (=255) for wined3d, because the 1 transform state was removed in d3d8
2682  * and WineD3D already understands the replacement D3DTS_WORLDMATRIX(0)
2683  *
2684  * Version 2, 3 and 7
2685  *
2686  * Params:
2687  *  TransformStateType: transform state to set
2688  *  Matrix: Matrix to assign to the state
2689  *
2690  * Returns:
2691  *  D3D_OK on success
2692  *  DDERR_INVALIDPARAMS if Matrix == NULL
2693  *  For details see IWineD3DDevice::SetTransform
2694  *
2695  *****************************************************************************/
2696 static HRESULT WINAPI
2697 IDirect3DDeviceImpl_7_SetTransform(IDirect3DDevice7 *iface,
2698                                    D3DTRANSFORMSTATETYPE TransformStateType,
2699                                    D3DMATRIX *Matrix)
2700 {
2701     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2702     D3DTRANSFORMSTATETYPE type = TransformStateType;
2703     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2704
2705     if(!Matrix)
2706         return DDERR_INVALIDPARAMS;
2707
2708     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2709      * use D3DTS_WORLDMATRIX(0) instead
2710      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2711      */
2712     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2713         type = (D3DTRANSFORMSTATETYPE)(0 + 256);
2714
2715     /* FIXME:
2716        Unhandled: D3DTRANSFORMSTATE_WORLD1
2717        Unhandled: D3DTRANSFORMSTATE_WORLD2
2718        Unhandled: D3DTRANSFORMSTATE_WORLD3
2719      */
2720
2721     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2722     return IWineD3DDevice_SetTransform(This->wineD3DDevice,
2723                                        type,
2724                                        (WINED3DMATRIX*) Matrix);
2725 }
2726
2727 static HRESULT WINAPI
2728 Thunk_IDirect3DDeviceImpl_3_SetTransform(IDirect3DDevice3 *iface,
2729                                          D3DTRANSFORMSTATETYPE TransformStateType,
2730                                          D3DMATRIX *D3DMatrix)
2731 {
2732     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2733     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2734     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2735                                          TransformStateType,
2736                                          D3DMatrix);
2737 }
2738
2739 static HRESULT WINAPI
2740 Thunk_IDirect3DDeviceImpl_2_SetTransform(IDirect3DDevice2 *iface,
2741                                          D3DTRANSFORMSTATETYPE TransformStateType,
2742                                          D3DMATRIX *D3DMatrix)
2743 {
2744     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2745     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2746     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2747                                          TransformStateType,
2748                                          D3DMatrix);
2749 }
2750
2751 /*****************************************************************************
2752  * IDirect3DDevice7::GetTransform
2753  *
2754  * Returns the matrix assigned to a transform state
2755  * D3DTRANSFORMSTATE_WORLD is translated to D3DTS_WORLDMATRIX(0), see
2756  * SetTransform
2757  *
2758  * Params:
2759  *  TransformStateType: State to read the matrix from
2760  *  Matrix: Address to store the matrix at
2761  *
2762  * Returns:
2763  *  D3D_OK on success
2764  *  DDERR_INVALIDPARAMS if Matrix == NULL
2765  *  For details, see IWineD3DDevice::GetTransform
2766  *
2767  *****************************************************************************/
2768 static HRESULT WINAPI
2769 IDirect3DDeviceImpl_7_GetTransform(IDirect3DDevice7 *iface,
2770                                    D3DTRANSFORMSTATETYPE TransformStateType,
2771                                    D3DMATRIX *Matrix)
2772 {
2773     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2774     D3DTRANSFORMSTATETYPE type = TransformStateType;
2775     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2776
2777     if(!Matrix)
2778         return DDERR_INVALIDPARAMS;
2779
2780     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2781      * use D3DTS_WORLDMATRIX(0) instead
2782      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2783      */
2784     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2785         type = (D3DTRANSFORMSTATETYPE)(0 + 256);
2786
2787     /* FIXME:
2788        Unhandled: D3DTRANSFORMSTATE_WORLD1
2789        Unhandled: D3DTRANSFORMSTATE_WORLD2
2790        Unhandled: D3DTRANSFORMSTATE_WORLD3
2791      */
2792
2793     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2794     return IWineD3DDevice_GetTransform(This->wineD3DDevice, type, (WINED3DMATRIX*) Matrix);
2795 }
2796
2797 static HRESULT WINAPI
2798 Thunk_IDirect3DDeviceImpl_3_GetTransform(IDirect3DDevice3 *iface,
2799                                          D3DTRANSFORMSTATETYPE TransformStateType,
2800                                          D3DMATRIX *D3DMatrix)
2801 {
2802     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2803     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2804     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2805                                          TransformStateType,
2806                                          D3DMatrix);
2807 }
2808
2809 static HRESULT WINAPI
2810 Thunk_IDirect3DDeviceImpl_2_GetTransform(IDirect3DDevice2 *iface,
2811                                          D3DTRANSFORMSTATETYPE TransformStateType,
2812                                          D3DMATRIX *D3DMatrix)
2813 {
2814     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2815     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2816     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2817                                          TransformStateType,
2818                                          D3DMatrix);
2819 }
2820
2821 /*****************************************************************************
2822  * IDirect3DDevice7::MultiplyTransform
2823  *
2824  * Multiplies the already-set transform matrix of a transform state
2825  * with another matrix. For the world matrix, see SetTransform
2826  *
2827  * Version 2, 3 and 7
2828  *
2829  * Params:
2830  *  TransformStateType: Transform state to multiply
2831  *  D3DMatrix Matrix to multiply with.
2832  *
2833  * Returns
2834  *  D3D_OK on success
2835  *  DDERR_INVALIDPARAMS if D3DMatrix is NULL
2836  *  For details, see IWineD3DDevice::MultiplyTransform
2837  *
2838  *****************************************************************************/
2839 static HRESULT WINAPI
2840 IDirect3DDeviceImpl_7_MultiplyTransform(IDirect3DDevice7 *iface,
2841                                         D3DTRANSFORMSTATETYPE TransformStateType,
2842                                         D3DMATRIX *D3DMatrix)
2843 {
2844     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2845     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, D3DMatrix);
2846
2847     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2848      * use D3DTS_WORLDMATRIX(0) instead
2849      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2850      */
2851     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2852         TransformStateType = (D3DTRANSFORMSTATETYPE)(0 + 256);
2853
2854     /* FIXME:
2855        Unhandled: D3DTRANSFORMSTATE_WORLD1
2856        Unhandled: D3DTRANSFORMSTATE_WORLD2
2857        Unhandled: D3DTRANSFORMSTATE_WORLD3
2858      */
2859
2860     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2861     return IWineD3DDevice_MultiplyTransform(This->wineD3DDevice,
2862                                             TransformStateType,
2863                                             (WINED3DMATRIX*) D3DMatrix);
2864 }
2865
2866 static HRESULT WINAPI
2867 Thunk_IDirect3DDeviceImpl_3_MultiplyTransform(IDirect3DDevice3 *iface,
2868                                               D3DTRANSFORMSTATETYPE TransformStateType,
2869                                               D3DMATRIX *D3DMatrix)
2870 {
2871     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2872     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2873     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2874                                               TransformStateType,
2875                                               D3DMatrix);
2876 }
2877
2878 static HRESULT WINAPI
2879 Thunk_IDirect3DDeviceImpl_2_MultiplyTransform(IDirect3DDevice2 *iface,
2880                                               D3DTRANSFORMSTATETYPE TransformStateType,
2881                                               D3DMATRIX *D3DMatrix)
2882 {
2883     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2884     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2885     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2886                                               TransformStateType,
2887                                               D3DMatrix);
2888 }
2889
2890 /*****************************************************************************
2891  * IDirect3DDevice7::DrawPrimitive
2892  *
2893  * Draws primitives based on vertices in an application-provided pointer
2894  *
2895  * Version 2, 3 and 7. The IDirect3DDevice2 thunk converts the fixed vertex type into
2896  * an FVF format for D3D7
2897  *
2898  * Params:
2899  *  PrimitiveType: The type of the primitives to draw
2900  *  Vertex type: Flexible vertex format vertex description
2901  *  Vertices: Pointer to the vertex array
2902  *  VertexCount: The number of vertices to draw
2903  *  Flags: As usual a few flags
2904  *
2905  * Returns:
2906  *  D3D_OK on success
2907  *  DDERR_INVALIDPARAMS if Vertices is NULL
2908  *  For details, see IWineD3DDevice::DrawPrimitiveUP
2909  *
2910  *****************************************************************************/
2911 static HRESULT WINAPI
2912 IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
2913                                     D3DPRIMITIVETYPE PrimitiveType,
2914                                     DWORD VertexType,
2915                                     void *Vertices,
2916                                     DWORD VertexCount,
2917                                     DWORD Flags)
2918 {
2919     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2920     UINT PrimitiveCount, stride;
2921     HRESULT hr;
2922     TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2923
2924     if(!Vertices)
2925         return DDERR_INVALIDPARAMS;
2926
2927     /* Get the vertex count */
2928     switch(PrimitiveType)
2929     {
2930       case D3DPT_POINTLIST: 
2931         PrimitiveCount = VertexCount;
2932         break;
2933
2934       case D3DPT_LINELIST: 
2935         PrimitiveCount = VertexCount / 2;
2936         break;
2937
2938       case D3DPT_LINESTRIP:
2939         PrimitiveCount = VertexCount - 1;
2940         break;
2941
2942       case D3DPT_TRIANGLELIST:
2943         PrimitiveCount = VertexCount / 3;
2944         break;
2945
2946       case D3DPT_TRIANGLESTRIP:
2947         PrimitiveCount = VertexCount - 2;
2948         break;
2949
2950       case D3DPT_TRIANGLEFAN:
2951         PrimitiveCount = VertexCount - 2;
2952         break;
2953
2954       default: return DDERR_INVALIDPARAMS;
2955     }
2956
2957     /* Get the stride */
2958     stride = get_flexible_vertex_size(VertexType);
2959
2960     /* Set the FVF */
2961     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, VertexType);
2962     if(hr != D3D_OK) return hr;
2963
2964     /* This method translates to the user pointer draw of WineD3D */
2965     return IWineD3DDevice_DrawPrimitiveUP(This->wineD3DDevice,
2966                                           PrimitiveType,
2967                                           PrimitiveCount,
2968                                           Vertices,
2969                                           stride);
2970 }
2971
2972 static HRESULT WINAPI
2973 Thunk_IDirect3DDeviceImpl_3_DrawPrimitive(IDirect3DDevice3 *iface,
2974                                           D3DPRIMITIVETYPE PrimitiveType,
2975                                           DWORD VertexType,
2976                                           void *Vertices,
2977                                           DWORD VertexCount,
2978                                           DWORD Flags)
2979 {
2980     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2981     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2982     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2983                                           PrimitiveType,
2984                                           VertexType,
2985                                           Vertices,
2986                                           VertexCount,
2987                                           Flags);
2988 }
2989
2990 static HRESULT WINAPI
2991 Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
2992                                           D3DPRIMITIVETYPE PrimitiveType,
2993                                           D3DVERTEXTYPE VertexType,
2994                                           void *Vertices,
2995                                           DWORD VertexCount,
2996                                           DWORD Flags)
2997 {
2998     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2999     DWORD FVF;
3000     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
3001
3002     switch(VertexType)
3003     {
3004         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
3005         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
3006         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
3007         default:
3008             ERR("Unexpected vertex type %d\n", VertexType);
3009             return DDERR_INVALIDPARAMS;  /* Should never happen */
3010     }
3011
3012     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3013                                           PrimitiveType,
3014                                           FVF,
3015                                           Vertices,
3016                                           VertexCount,
3017                                           Flags);
3018 }
3019
3020 /*****************************************************************************
3021  * IDirect3DDevice7::DrawIndexedPrimitive
3022  *
3023  * Draws vertices from an application-provided pointer, based on the index
3024  * numbers in a WORD array.
3025  *
3026  * Version 2, 3 and 7. The version 7 thunk translates the vertex type into
3027  * an FVF format for D3D7
3028  *
3029  * Params:
3030  *  PrimitiveType: The primitive type to draw
3031  *  VertexType: The FVF vertex description
3032  *  Vertices: Pointer to the vertex array
3033  *  VertexCount: ?
3034  *  Indices: Pointer to the index array
3035  *  IndexCount: Number of indices = Number of vertices to draw
3036  *  Flags: As usual, some flags
3037  *
3038  * Returns:
3039  *  D3D_OK on success
3040  *  DDERR_INVALIDPARAMS if Vertices or Indices is NULL
3041  *  For details, see IWineD3DDevice::DrawIndexedPrimitiveUP
3042  *
3043  *****************************************************************************/
3044 static HRESULT WINAPI
3045 IDirect3DDeviceImpl_7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
3046                                            D3DPRIMITIVETYPE PrimitiveType,
3047                                            DWORD VertexType,
3048                                            void *Vertices,
3049                                            DWORD VertexCount,
3050                                            WORD *Indices,
3051                                            DWORD IndexCount,
3052                                            DWORD Flags)
3053 {
3054     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3055     UINT PrimitiveCount = 0;
3056     HRESULT hr;
3057     TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3058     /* Get the primitive number */
3059     switch(PrimitiveType)
3060     {
3061       case D3DPT_POINTLIST: 
3062         PrimitiveCount = IndexCount;
3063         break;
3064
3065       case D3DPT_LINELIST: 
3066         PrimitiveCount = IndexCount / 2;
3067         break;
3068
3069       case D3DPT_LINESTRIP:
3070         PrimitiveCount = IndexCount - 1;
3071         break;
3072
3073       case D3DPT_TRIANGLELIST:
3074         PrimitiveCount = IndexCount / 3;
3075         break;
3076
3077       case D3DPT_TRIANGLESTRIP:
3078         PrimitiveCount = IndexCount - 2;
3079         break;
3080
3081       case D3DPT_TRIANGLEFAN:
3082         PrimitiveCount = IndexCount - 2;
3083         break;
3084
3085       default: return DDERR_INVALIDPARAMS;
3086     }
3087
3088     /* Set the D3DDevice's FVF */
3089     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, VertexType);
3090     if(FAILED(hr))
3091     {
3092         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3093         return hr;
3094     }
3095
3096     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->wineD3DDevice,
3097                                                  PrimitiveType,
3098                                                  0 /* MinVertexIndex */,
3099                                                  VertexCount /* UINT NumVertexIndex */,
3100                                                  PrimitiveCount,
3101                                                  Indices,
3102                                                  WINED3DFMT_INDEX16,
3103                                                  Vertices,
3104                                                  get_flexible_vertex_size(VertexType));
3105 }
3106
3107 static HRESULT WINAPI
3108 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
3109                                                  D3DPRIMITIVETYPE PrimitiveType,
3110                                                  DWORD VertexType,
3111                                                  void *Vertices,
3112                                                  DWORD VertexCount,
3113                                                  WORD *Indices,
3114                                                  DWORD IndexCount,
3115                                                  DWORD Flags)
3116 {
3117     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3118     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3119     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3120                                                  PrimitiveType,
3121                                                  VertexType,
3122                                                  Vertices,
3123                                                  VertexCount,
3124                                                  Indices,
3125                                                  IndexCount,
3126                                                  Flags);
3127 }
3128
3129 static HRESULT WINAPI
3130 Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
3131                                                  D3DPRIMITIVETYPE PrimitiveType,
3132                                                  D3DVERTEXTYPE VertexType,
3133                                                  void *Vertices,
3134                                                  DWORD VertexCount,
3135                                                  WORD *Indices,
3136                                                  DWORD IndexCount,
3137                                                  DWORD Flags)
3138 {
3139     DWORD FVF;
3140     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3141     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3142
3143     switch(VertexType)
3144     {
3145         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
3146         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
3147         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
3148         default:
3149             ERR("Unexpected vertex type %d\n", VertexType);
3150             return DDERR_INVALIDPARAMS;  /* Should never happen */
3151     }
3152
3153     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3154                                                  PrimitiveType,
3155                                                  FVF,
3156                                                  Vertices,
3157                                                  VertexCount,
3158                                                  Indices,
3159                                                  IndexCount,
3160                                                  Flags);
3161 }
3162
3163 /*****************************************************************************
3164  * IDirect3DDevice7::SetClipStatus
3165  *
3166  * Sets the clip status. This defines things as clipping conditions and
3167  * the extents of the clipping region.
3168  *
3169  * Version 2, 3 and 7
3170  *
3171  * Params:
3172  *  ClipStatus:
3173  *
3174  * Returns:
3175  *  D3D_OK because it's a stub
3176  *  (DDERR_INVALIDPARAMS if ClipStatus == NULL)
3177  *
3178  *****************************************************************************/
3179 static HRESULT WINAPI
3180 IDirect3DDeviceImpl_7_SetClipStatus(IDirect3DDevice7 *iface,
3181                                     D3DCLIPSTATUS *ClipStatus)
3182 {
3183     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3184     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3185
3186     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
3187      * Perhaps this needs a new data type and an additional IWineD3DDevice method
3188      */
3189     /* return IWineD3DDevice_SetClipStatus(This->wineD3DDevice, ClipStatus);*/
3190     return D3D_OK;
3191 }
3192
3193 static HRESULT WINAPI
3194 Thunk_IDirect3DDeviceImpl_3_SetClipStatus(IDirect3DDevice3 *iface,
3195                                           D3DCLIPSTATUS *ClipStatus)
3196 {
3197     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3198     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3199     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3200                                           ClipStatus);
3201 }
3202
3203 static HRESULT WINAPI
3204 Thunk_IDirect3DDeviceImpl_2_SetClipStatus(IDirect3DDevice2 *iface,
3205                                           D3DCLIPSTATUS *ClipStatus)
3206 {
3207     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3208     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3209     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3210                                           ClipStatus);
3211 }
3212
3213 /*****************************************************************************
3214  * IDirect3DDevice7::GetClipStatus
3215  *
3216  * Returns the clip status
3217  *
3218  * Params:
3219  *  ClipStatus: Address to write the clip status to
3220  *
3221  * Returns:
3222  *  D3D_OK because it's a stub
3223  *
3224  *****************************************************************************/
3225 static HRESULT WINAPI
3226 IDirect3DDeviceImpl_7_GetClipStatus(IDirect3DDevice7 *iface,
3227                                     D3DCLIPSTATUS *ClipStatus)
3228 {
3229     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3230     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3231
3232     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
3233     /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
3234     return D3D_OK;
3235 }
3236
3237 static HRESULT WINAPI
3238 Thunk_IDirect3DDeviceImpl_3_GetClipStatus(IDirect3DDevice3 *iface,
3239                                           D3DCLIPSTATUS *ClipStatus)
3240 {
3241     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3242     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3243     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3244                                           ClipStatus);
3245 }
3246
3247 static HRESULT WINAPI
3248 Thunk_IDirect3DDeviceImpl_2_GetClipStatus(IDirect3DDevice2 *iface,
3249                                           D3DCLIPSTATUS *ClipStatus)
3250 {
3251     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3252     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3253     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3254                                           ClipStatus);
3255 }
3256
3257 /*****************************************************************************
3258  * IDirect3DDevice::DrawPrimitiveStrided
3259  *
3260  * Draws vertices described by a D3DDRAWPRIMITIVESTRIDEDDATA structure.
3261  *
3262  * Version 3 and 7
3263  *
3264  * Params:
3265  *  PrimitiveType: The primitive type to draw
3266  *  VertexType: The FVF description of the vertices to draw (for the stride??)
3267  *  D3DDrawPrimStrideData: A D3DDRAWPRIMITIVESTRIDEDDATA structure describing
3268  *                         the vertex data locations
3269  *  VertexCount: The number of vertices to draw
3270  *  Flags: Some flags
3271  *
3272  * Returns:
3273  *  D3D_OK, because it's a stub
3274  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3275  *  (For details, see IWineD3DDevice::DrawPrimitiveStrided)
3276  *
3277  *****************************************************************************/
3278 static HRESULT WINAPI
3279 IDirect3DDeviceImpl_7_DrawPrimitiveStrided(IDirect3DDevice7 *iface,
3280                                            D3DPRIMITIVETYPE PrimitiveType,
3281                                            DWORD VertexType,
3282                                            D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3283                                            DWORD VertexCount,
3284                                            DWORD Flags)
3285 {
3286     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3287     WineDirect3DVertexStridedData WineD3DStrided;
3288     int i;
3289     UINT PrimitiveCount;
3290
3291     TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3292
3293     memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
3294     /* Get the strided data right. the wined3d structure is a bit bigger
3295      * Watch out: The contents of the strided data are determined by the fvf,
3296      * not by the members set in D3DDrawPrimStrideData. So it's valid
3297      * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
3298      * not set in the fvf.
3299      */
3300     if(VertexType & D3DFVF_POSITION_MASK)
3301     {
3302         WineD3DStrided.u.s.position.lpData = D3DDrawPrimStrideData->position.lpvData;
3303         WineD3DStrided.u.s.position.dwStride = D3DDrawPrimStrideData->position.dwStride;
3304         WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
3305         if (VertexType & D3DFVF_XYZRHW)
3306         {
3307             WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
3308             WineD3DStrided.u.s.position_transformed = TRUE;
3309         } else
3310             WineD3DStrided.u.s.position_transformed = FALSE;
3311     }
3312
3313     if(VertexType & D3DFVF_NORMAL)
3314     {
3315         WineD3DStrided.u.s.normal.lpData = D3DDrawPrimStrideData->normal.lpvData;
3316         WineD3DStrided.u.s.normal.dwStride = D3DDrawPrimStrideData->normal.dwStride;
3317         WineD3DStrided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
3318     }
3319
3320     if(VertexType & D3DFVF_DIFFUSE)
3321     {
3322         WineD3DStrided.u.s.diffuse.lpData = D3DDrawPrimStrideData->diffuse.lpvData;
3323         WineD3DStrided.u.s.diffuse.dwStride = D3DDrawPrimStrideData->diffuse.dwStride;
3324         WineD3DStrided.u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
3325     }
3326
3327     if(VertexType & D3DFVF_SPECULAR)
3328     {
3329         WineD3DStrided.u.s.specular.lpData = D3DDrawPrimStrideData->specular.lpvData;
3330         WineD3DStrided.u.s.specular.dwStride = D3DDrawPrimStrideData->specular.dwStride;
3331         WineD3DStrided.u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
3332     }
3333
3334     for( i = 0; i < GET_TEXCOUNT_FROM_FVF(VertexType); i++)
3335     {
3336         WineD3DStrided.u.s.texCoords[i].lpData = D3DDrawPrimStrideData->textureCoords[i].lpvData;
3337         WineD3DStrided.u.s.texCoords[i].dwStride = D3DDrawPrimStrideData->textureCoords[i].dwStride;
3338         switch(GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i))
3339         {
3340             case 1: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT1; break;
3341             case 2: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT2; break;
3342             case 3: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT3; break;
3343             case 4: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT4; break;
3344             default: ERR("Unexpected texture coordinate size %d\n",
3345                          GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i));
3346         }
3347     }
3348
3349     /* Get the primitive count */
3350     switch(PrimitiveType)
3351     {
3352         case D3DPT_POINTLIST: 
3353           PrimitiveCount = VertexCount;
3354           break;
3355
3356         case D3DPT_LINELIST: 
3357           PrimitiveCount = VertexCount / 2;
3358           break;
3359
3360         case D3DPT_LINESTRIP:
3361           PrimitiveCount = VertexCount - 1;
3362           break;
3363
3364         case D3DPT_TRIANGLELIST:
3365           PrimitiveCount = VertexCount / 3;
3366           break;
3367
3368         case D3DPT_TRIANGLESTRIP:
3369           PrimitiveCount = VertexCount - 2;
3370           break;
3371
3372         case D3DPT_TRIANGLEFAN:
3373           PrimitiveCount = VertexCount - 2;
3374           break;
3375
3376         default: return DDERR_INVALIDPARAMS;
3377     }
3378
3379     IWineD3DDevice_SetFVF(This->wineD3DDevice,
3380                           VertexType);
3381
3382     return IWineD3DDevice_DrawPrimitiveStrided(This->wineD3DDevice,
3383                                                PrimitiveType,
3384                                                PrimitiveCount,
3385                                                &WineD3DStrided);
3386 }
3387
3388 static HRESULT WINAPI
3389 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
3390                                                  D3DPRIMITIVETYPE PrimitiveType,
3391                                                  DWORD VertexType,
3392                                                  D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3393                                                  DWORD VertexCount,
3394                                                  DWORD Flags)
3395 {
3396     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3397     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3398     return IDirect3DDevice7_DrawPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3399                                                  PrimitiveType,
3400                                                  VertexType,
3401                                                  D3DDrawPrimStrideData,
3402                                                  VertexCount,
3403                                                  Flags);
3404 }
3405
3406 /*****************************************************************************
3407  * IDirect3DDevice7::DrawIndexedPrimitiveStrided
3408  *
3409  * Draws primitives specified by strided data locations based on indices
3410  *
3411  * Version 3 and 7
3412  *
3413  * Params:
3414  *  PrimitiveType:
3415  *
3416  * Returns:
3417  *  D3D_OK, because it's a stub
3418  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3419  *  (DDERR_INVALIDPARAMS if Indices is NULL)
3420  *  (For more details, see IWineD3DDevice::DrawIndexedPrimitiveStrided)
3421  *
3422  *****************************************************************************/
3423 static HRESULT WINAPI
3424 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
3425                                                   D3DPRIMITIVETYPE PrimitiveType,
3426                                                   DWORD VertexType,
3427                                                   D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3428                                                   DWORD VertexCount,
3429                                                   WORD *Indices,
3430                                                   DWORD IndexCount,
3431                                                   DWORD Flags)
3432 {
3433     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3434     FIXME("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3435
3436     /* I'll implement it as soon as I find a app to test it.
3437      * This needs an additional method in IWineD3DDevice.
3438      */
3439     return D3D_OK;
3440 }
3441
3442 static HRESULT WINAPI
3443 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
3444                                                         D3DPRIMITIVETYPE PrimitiveType,
3445                                                         DWORD VertexType,
3446                                                         D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3447                                                         DWORD VertexCount,
3448                                                         WORD *Indices,
3449                                                         DWORD IndexCount,
3450                                                         DWORD Flags)
3451 {
3452     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3453     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3454     return IDirect3DDevice7_DrawIndexedPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3455                                                         PrimitiveType,
3456                                                         VertexType,
3457                                                         D3DDrawPrimStrideData,
3458                                                         VertexCount,
3459                                                         Indices,
3460                                                         IndexCount,
3461                                                         Flags);
3462 }
3463
3464 /*****************************************************************************
3465  * IDirect3DDevice7::DrawPrimitiveVB
3466  *
3467  * Draws primitives from a vertex buffer to the screen.
3468  *
3469  * Version 3 and 7
3470  *
3471  * Params:
3472  *  PrimitiveType: Type of primitive to be rendered.
3473  *  D3DVertexBuf: Source Vertex Buffer
3474  *  StartVertex: Index of the first vertex from the buffer to be rendered
3475  *  NumVertices: Number of vertices to be rendered
3476  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3477  *
3478  * Return values
3479  *  D3D_OK on success
3480  *  DDERR_INVALIDPARAMS if D3DVertexBuf is NULL
3481  *
3482  *****************************************************************************/
3483 static HRESULT WINAPI
3484 IDirect3DDeviceImpl_7_DrawPrimitiveVB(IDirect3DDevice7 *iface,
3485                                       D3DPRIMITIVETYPE PrimitiveType,
3486                                       IDirect3DVertexBuffer7 *D3DVertexBuf,
3487                                       DWORD StartVertex,
3488                                       DWORD NumVertices,
3489                                       DWORD Flags)
3490 {
3491     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3492     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3493     UINT PrimitiveCount;
3494     HRESULT hr;
3495     DWORD stride;
3496     WINED3DVERTEXBUFFER_DESC Desc;
3497
3498     TRACE("(%p)->(%08x,%p,%08x,%08x,%08x)\n", This, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
3499
3500     /* Sanity checks */
3501     if(!vb)
3502     {
3503         ERR("(%p) No Vertex buffer specified\n", This);
3504         return DDERR_INVALIDPARAMS;
3505     }
3506
3507     /* Get the primitive count */
3508     switch(PrimitiveType)
3509     {
3510         case D3DPT_POINTLIST: 
3511           PrimitiveCount = NumVertices;
3512           break;
3513
3514         case D3DPT_LINELIST: 
3515           PrimitiveCount = NumVertices / 2;
3516           break;
3517
3518         case D3DPT_LINESTRIP:
3519           PrimitiveCount = NumVertices - 1;
3520           break;
3521
3522         case D3DPT_TRIANGLELIST:
3523           PrimitiveCount = NumVertices / 3;
3524           break;
3525
3526         case D3DPT_TRIANGLESTRIP:
3527           PrimitiveCount = NumVertices - 2;
3528           break;
3529
3530         case D3DPT_TRIANGLEFAN:
3531           PrimitiveCount = NumVertices - 2;
3532           break;
3533
3534         default: return DDERR_INVALIDPARAMS;
3535     }
3536
3537     /* Get the FVF of the vertex buffer, and its stride */
3538     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3539                                       &Desc);
3540     if(hr != D3D_OK)
3541     {
3542         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3543         return hr;
3544     }
3545     stride = get_flexible_vertex_size(Desc.FVF);
3546
3547     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, Desc.FVF);
3548     if(FAILED(hr))
3549     {
3550         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3551         return hr;
3552     }
3553
3554     /* Set the vertex stream source */
3555     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3556                                         0 /* StreamNumber */,
3557                                         vb->wineD3DVertexBuffer,
3558                                         0 /* StartVertex - we pass this to DrawPrimitive */,
3559                                         stride);
3560     if(hr != D3D_OK)
3561     {
3562         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3563         return hr;
3564     }
3565
3566     /* Now draw the primitives */
3567     return IWineD3DDevice_DrawPrimitive(This->wineD3DDevice,
3568                                         PrimitiveType,
3569                                         StartVertex,
3570                                         PrimitiveCount);
3571 }
3572
3573 static HRESULT WINAPI
3574 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB(IDirect3DDevice3 *iface,
3575                                             D3DPRIMITIVETYPE PrimitiveType,
3576                                             IDirect3DVertexBuffer *D3DVertexBuf,
3577                                             DWORD StartVertex,
3578                                             DWORD NumVertices,
3579                                             DWORD Flags)
3580 {
3581     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3582     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3583     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This,  PrimitiveType, vb, StartVertex, NumVertices, Flags);
3584     return IDirect3DDevice7_DrawPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3585                                             PrimitiveType,
3586                                             ICOM_INTERFACE(vb, IDirect3DVertexBuffer7),
3587                                             StartVertex,
3588                                             NumVertices,
3589                                             Flags);
3590 }
3591
3592
3593 /*****************************************************************************
3594  * IDirect3DDevice7::DrawIndexedPrimitiveVB
3595  *
3596  * Draws primitives from a vertex buffer to the screen
3597  *
3598  * Params:
3599  *  PrimitiveType: Type of primitive to be rendered.
3600  *  D3DVertexBuf: Source Vertex Buffer
3601  *  StartVertex: Index of the first vertex from the buffer to be rendered
3602  *  NumVertices: Number of vertices to be rendered
3603  *  Indices: Array of DWORDs used to index into the Vertices
3604  *  IndexCount: Number of indices in Indices
3605  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3606  *
3607  * Return values
3608  *
3609  *****************************************************************************/
3610 static HRESULT WINAPI
3611 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
3612                                              D3DPRIMITIVETYPE PrimitiveType,
3613                                              IDirect3DVertexBuffer7 *D3DVertexBuf,
3614                                              DWORD StartVertex,
3615                                              DWORD NumVertices,
3616                                              WORD *Indices,
3617                                              DWORD IndexCount,
3618                                              DWORD Flags)
3619 {
3620     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3621     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3622     DWORD stride;
3623     UINT PrimitiveCount;
3624     WORD *LockedIndices;
3625     HRESULT hr;
3626     WINED3DVERTEXBUFFER_DESC Desc;
3627
3628     TRACE("(%p)->(%08x,%p,%d,%d,%p,%d,%08x)\n", This, PrimitiveType, vb, StartVertex, NumVertices, Indices, IndexCount, Flags);
3629
3630     /* Steps:
3631      * 1) Calculate some things: Vertex count -> Primitive count, stride, ...
3632      * 2) Upload the Indices to the index buffer
3633      * 3) Set the index source
3634      * 4) Set the Vertex Buffer as the Stream source
3635      * 5) Call IWineD3DDevice::DrawIndexedPrimitive
3636      */
3637
3638     /* Get the primitive count */
3639     switch(PrimitiveType)
3640     {
3641         case D3DPT_POINTLIST: 
3642           PrimitiveCount = IndexCount;
3643           break;
3644
3645         case D3DPT_LINELIST: 
3646           PrimitiveCount = IndexCount / 2;
3647           break;
3648
3649         case D3DPT_LINESTRIP:
3650           PrimitiveCount = IndexCount - 1;
3651           break;
3652
3653         case D3DPT_TRIANGLELIST:
3654           PrimitiveCount = IndexCount / 3;
3655           break;
3656
3657         case D3DPT_TRIANGLESTRIP:
3658           PrimitiveCount = IndexCount - 2;
3659           break;
3660
3661         case D3DPT_TRIANGLEFAN:
3662           PrimitiveCount = IndexCount - 2;
3663           break;
3664
3665         default: return DDERR_INVALIDPARAMS;
3666     }
3667
3668     /* Get the FVF of the vertex buffer, and its stride */
3669     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3670                                       &Desc);
3671     if(hr != D3D_OK)
3672     {
3673         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3674         return hr;
3675     }
3676     stride = get_flexible_vertex_size(Desc.FVF);
3677     TRACE("Vertex buffer FVF = %08x, stride=%d\n", Desc.FVF, stride);
3678
3679     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, Desc.FVF);
3680     if(FAILED(hr))
3681     {
3682         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3683         return hr;
3684     }
3685
3686     /* copy the index stream into the index buffer.
3687      * A new IWineD3DDevice method could be created
3688      * which takes an user pointer containing the indices
3689      * or a SetData-Method for the index buffer, which
3690      * overrides the index buffer data with our pointer.
3691      */
3692     hr = IWineD3DIndexBuffer_Lock(This->indexbuffer,
3693                                   0 /* OffSetToLock */,
3694                                   IndexCount * sizeof(WORD),
3695                                   (BYTE **) &LockedIndices,
3696                                   0 /* Flags */);
3697     assert(IndexCount < 0x100000);
3698     if(hr != D3D_OK)
3699     {
3700         ERR("(%p) IWineD3DIndexBuffer::Lock failed with hr = %08x\n", This, hr);
3701         return hr;
3702     }
3703     memcpy(LockedIndices, Indices, IndexCount * sizeof(WORD));
3704     hr = IWineD3DIndexBuffer_Unlock(This->indexbuffer);
3705     if(hr != D3D_OK)
3706     {
3707         ERR("(%p) IWineD3DIndexBuffer::Unlock failed with hr = %08x\n", This, hr);
3708         return hr;
3709     }
3710
3711     /* Set the index stream */
3712     hr = IWineD3DDevice_SetIndices(This->wineD3DDevice,
3713                                    This->indexbuffer,
3714                                    StartVertex);
3715
3716     /* Set the vertex stream source */
3717     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3718                                         0 /* StreamNumber */,
3719                                         vb->wineD3DVertexBuffer,
3720                                         0 /* offset, we pass this to DrawIndexedPrimitive */,
3721                                         stride);
3722     if(hr != D3D_OK)
3723     {
3724         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3725         return hr;
3726     }
3727
3728
3729     hr = IWineD3DDevice_DrawIndexedPrimitive(This->wineD3DDevice,
3730                                              PrimitiveType,
3731                                              0 /* minIndex */,
3732                                              NumVertices,
3733                                              0 /* StartIndex */,
3734                                              PrimitiveCount);
3735
3736     return D3D_OK;
3737 }
3738
3739 static HRESULT WINAPI
3740 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
3741                                                    D3DPRIMITIVETYPE PrimitiveType,
3742                                                    IDirect3DVertexBuffer *D3DVertexBuf,
3743                                                    WORD *Indices,
3744                                                    DWORD IndexCount,
3745                                                    DWORD Flags)
3746 {
3747     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3748     IDirect3DVertexBufferImpl *VB = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3749     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VB, Indices, IndexCount, Flags);
3750
3751     return IDirect3DDevice7_DrawIndexedPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3752                                                    PrimitiveType,
3753                                                    ICOM_INTERFACE(VB, IDirect3DVertexBuffer7),
3754                                                    0,
3755                                                    IndexCount,
3756                                                    Indices,
3757                                                    IndexCount,
3758                                                    Flags);
3759 }
3760
3761 /*****************************************************************************
3762  * IDirect3DDevice7::ComputeSphereVisibility
3763  *
3764  * Calculates the visibility of spheres in the current viewport. The spheres
3765  * are passed in the Centers and Radii arrays, the results are passed back
3766  * in the ReturnValues array. Return values are either completely visible,
3767  * partially visible or completely invisible.
3768  * The return value consist of a combination of D3DCLIP_* flags, or it's
3769  * 0 if the sphere is completely visible(according to the SDK, not checked)
3770  *
3771  * Sounds like an overdose of math ;)
3772  *
3773  * Version 3 and 7
3774  *
3775  * Params:
3776  *  Centers: Array containing the sphere centers
3777  *  Radii: Array containing the sphere radii
3778  *  NumSpheres: The number of centers and radii in the arrays
3779  *  Flags: Some flags
3780  *  ReturnValues: Array to write the results to
3781  *
3782  * Returns:
3783  *  D3D_OK because it's a stub
3784  *  (DDERR_INVALIDPARAMS if Centers, Radii or ReturnValues are NULL)
3785  *  (D3DERR_INVALIDMATRIX if the combined world, view and proj matrix
3786  *  is singular)
3787  *
3788  *****************************************************************************/
3789 static HRESULT WINAPI
3790 IDirect3DDeviceImpl_7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
3791                                               D3DVECTOR *Centers,
3792                                               D3DVALUE *Radii,
3793                                               DWORD NumSpheres,
3794                                               DWORD Flags,
3795                                               DWORD *ReturnValues)
3796 {
3797     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3798     FIXME("(%p)->(%p,%p,%08x,%08x,%p): stub!\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3799
3800     /* the DirectX 7 sdk says that the visibility is computed by
3801      * back-transforming the viewing frustum to model space
3802      * using the inverse of the combined world, view and projection
3803      * matrix. If the matrix can't be reversed, D3DERR_INVALIDMATRIX
3804      * is returned.
3805      *
3806      * Basic implementation idea:
3807      * 1) Check if the center is in the viewing frustum
3808      * 2) Cut the sphere with the planes of the viewing
3809      *    frustum
3810      *
3811      * ->Center inside the frustum, no intersections:
3812      *    Fully visible
3813      * ->Center outside the frustum, no intersections:
3814      *    Not visible
3815      * ->Some intersections: Partially visible
3816      *
3817      * Implement this call in WineD3D. Either implement the
3818      * matrix and vector stuff in WineD3D, or use some external
3819      * math library.
3820      */
3821
3822     return D3D_OK;
3823 }
3824
3825 static HRESULT WINAPI
3826 Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
3827                                                     D3DVECTOR *Centers,
3828                                                     D3DVALUE *Radii,
3829                                                     DWORD NumSpheres,
3830                                                     DWORD Flags,
3831                                                     DWORD *ReturnValues)
3832 {
3833     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3834     TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3835     return IDirect3DDevice7_ComputeSphereVisibility(ICOM_INTERFACE(This, IDirect3DDevice7),
3836                                                     Centers,
3837                                                     Radii,
3838                                                     NumSpheres,
3839                                                     Flags,
3840                                                     ReturnValues);
3841 }
3842
3843 /*****************************************************************************
3844  * IDirect3DDevice7::GetTexture
3845  *
3846  * Returns the texture interface handle assigned to a texture stage.
3847  * The returned texture is AddRefed. This is taken from old ddraw,
3848  * not checked in Windows.
3849  *
3850  * Version 3 and 7
3851  *
3852  * Params:
3853  *  Stage: Texture stage to read the texture from
3854  *  Texture: Address to store the interface pointer at
3855  *
3856  * Returns:
3857  *  D3D_OK on success
3858  *  DDERR_INVALIDPARAMS if Texture is NULL
3859  *  For details, see IWineD3DDevice::GetTexture
3860  *
3861  *****************************************************************************/
3862 static HRESULT WINAPI
3863 IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
3864                                  DWORD Stage,
3865                                  IDirectDrawSurface7 **Texture)
3866 {
3867     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3868     IWineD3DBaseTexture *Surf;
3869     HRESULT hr;
3870     TRACE("(%p)->(%d,%p): Relay\n", This, Stage, Texture);
3871
3872     if(!Texture)
3873     {
3874         TRACE("Texture == NULL, failing with DDERR_INVALIDPARAMS\n");
3875         return DDERR_INVALIDPARAMS;
3876     }
3877
3878     hr = IWineD3DDevice_GetTexture(This->wineD3DDevice, Stage, (IWineD3DBaseTexture **) &Surf);
3879     if( (hr != D3D_OK) || (!Surf) ) 
3880     {
3881         *Texture = NULL;
3882         return hr;
3883     }
3884
3885     /* GetParent AddRef()s, which is perfectly OK.
3886      * We have passed the IDirectDrawSurface7 interface to WineD3D, so that's OK too.
3887      */
3888     return IWineD3DBaseTexture_GetParent(Surf,
3889                                          (IUnknown **) Texture);
3890 }
3891
3892 static HRESULT WINAPI
3893 Thunk_IDirect3DDeviceImpl_3_GetTexture(IDirect3DDevice3 *iface,
3894                                        DWORD Stage,
3895                                        IDirect3DTexture2 **Texture2)
3896 {
3897     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3898     HRESULT ret;
3899     IDirectDrawSurface7 *ret_val;
3900
3901     TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, Texture2);
3902     ret = IDirect3DDevice7_GetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3903                                       Stage,
3904                                       &ret_val);
3905
3906     *Texture2 = COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirect3DTexture2, ret_val);
3907
3908     TRACE_(ddraw_thunk)(" returning interface %p.\n", *Texture2);
3909
3910     return ret;
3911 }
3912
3913 /*****************************************************************************
3914  * IDirect3DDevice7::SetTexture
3915  *
3916  * Assigns a texture to a texture stage. Is the texture AddRef-ed?
3917  *
3918  * Version 3 and 7
3919  *
3920  * Params:
3921  *  Stage: The stage to assign the texture to
3922  *  Texture: Interface pointer to the texture surface
3923  *
3924  * Returns
3925  * D3D_OK on success
3926  * For details, see IWineD3DDevice::SetTexture
3927  *
3928  *****************************************************************************/
3929 static HRESULT WINAPI
3930 IDirect3DDeviceImpl_7_SetTexture(IDirect3DDevice7 *iface,
3931                                  DWORD Stage,
3932                                  IDirectDrawSurface7 *Texture)
3933 {
3934     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3935     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
3936     TRACE("(%p)->(%08x,%p): Relay!\n", This, Stage, surf);
3937
3938     /* Texture may be NULL here */
3939     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
3940                                      Stage,
3941                                      surf ? (IWineD3DBaseTexture * ) surf->wineD3DTexture : NULL);
3942 }
3943
3944 static HRESULT WINAPI
3945 Thunk_IDirect3DDeviceImpl_3_SetTexture(IDirect3DDevice3 *iface,
3946                                        DWORD Stage,
3947                                        IDirect3DTexture2 *Texture2)
3948 {
3949     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3950     IDirectDrawSurfaceImpl *tex = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Texture2);
3951     TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, tex);
3952     return IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3953                                        Stage,
3954                                        ICOM_INTERFACE(tex, IDirectDrawSurface7));
3955 }
3956
3957 /*****************************************************************************
3958  * IDirect3DDevice7::GetTextureStageState
3959  *
3960  * Retrieves a state from a texture stage.
3961  *
3962  * Version 3 and 7
3963  *
3964  * Params:
3965  *  Stage: The stage to retrieve the state from
3966  *  TexStageStateType: The state type to retrieve
3967  *  State: Address to store the state's value at
3968  *
3969  * Returns:
3970  *  D3D_OK on success
3971  *  DDERR_INVALIDPARAMS if State is NULL
3972  *  For details, see IWineD3DDevice::GetTextureStageState
3973  *
3974  *****************************************************************************/
3975 static HRESULT WINAPI
3976 IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
3977                                            DWORD Stage,
3978                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
3979                                            DWORD *State)
3980 {
3981     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3982     TRACE("(%p)->(%08x,%08x,%p): Relay!\n", This, Stage, TexStageStateType, State);
3983
3984     if(!State)
3985         return DDERR_INVALIDPARAMS;
3986
3987     switch(TexStageStateType)
3988     {
3989         /* Mipfilter is a sampler state with different values */
3990         case D3DTSS_MIPFILTER:
3991         {
3992             HRESULT hr;
3993             WINED3DTEXTUREFILTERTYPE value;
3994
3995             hr = IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
3996                                                 Stage,
3997                                                 WINED3DSAMP_MIPFILTER,
3998                                                 &value);
3999             switch(value)
4000             {
4001                 case WINED3DTEXF_NONE: *State = D3DTFP_NONE; break;
4002                 case WINED3DTEXF_POINT: *State = D3DTFP_POINT; break;
4003                 case WINED3DTEXF_LINEAR: *State = D3DTFP_LINEAR; break;
4004                 default:
4005                     ERR("Unexpected mipfilter value %d\n", value);
4006                     *State = D3DTFP_NONE;
4007             }
4008             return hr;
4009         }
4010
4011         /* Minfilter is a sampler state too, equal values */
4012         case D3DTSS_MINFILTER:
4013             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4014                                                   Stage,
4015                                                   WINED3DSAMP_MINFILTER,
4016                                                   State);
4017         /* Same for MAGFILTER */
4018         case D3DTSS_MAGFILTER:
4019             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4020                                                   Stage,
4021                                                   WINED3DSAMP_MAGFILTER,
4022                                                   State);
4023
4024         case D3DTSS_ADDRESS:
4025         case D3DTSS_ADDRESSU:
4026             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4027                                                   Stage,
4028                                                   WINED3DSAMP_ADDRESSU,
4029                                                   State);
4030         case D3DTSS_ADDRESSV:
4031             return IWineD3DDevice_GetSamplerState(This->wineD3DDevice,
4032                                                   Stage,
4033                                                   WINED3DSAMP_ADDRESSV,
4034                                                   State);
4035         default:
4036             return IWineD3DDevice_GetTextureStageState(This->wineD3DDevice,
4037                                                        Stage,
4038                                                        TexStageStateType,
4039                                                        State);
4040     }
4041 }
4042
4043 static HRESULT WINAPI
4044 Thunk_IDirect3DDeviceImpl_3_GetTextureStageState(IDirect3DDevice3 *iface,
4045                                                  DWORD Stage,
4046                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
4047                                                  DWORD *State)
4048 {
4049     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4050     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
4051     return IDirect3DDevice7_GetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
4052                                                  Stage,
4053                                                  TexStageStateType,
4054                                                  State);
4055 }
4056
4057 /*****************************************************************************
4058  * IDirect3DDevice7::SetTextureStageState
4059  *
4060  * Sets a texture stage state. Some stage types need to be handled specially,
4061  * because they do not exist in WineD3D and were moved to another place
4062  *
4063  * Version 3 and 7
4064  *
4065  * Params:
4066  *  Stage: The stage to modify
4067  *  TexStageStateType: The state to change
4068  *  State: The new value for the state
4069  *
4070  * Returns:
4071  *  D3D_OK on success
4072  *  For details, see IWineD3DDevice::SetTextureStageState
4073  *
4074  *****************************************************************************/
4075 static HRESULT WINAPI
4076 IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
4077                                            DWORD Stage,
4078                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
4079                                            DWORD State)
4080 {
4081     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4082     TRACE("(%p)->(%08x,%08x,%08x): Relay!\n", This, Stage, TexStageStateType, State);
4083     switch(TexStageStateType)
4084     {
4085         /* Mipfilter is a sampler state with different values */
4086         case D3DTSS_MIPFILTER:
4087         {
4088             WINED3DTEXTUREFILTERTYPE value;
4089             switch(State)
4090             {
4091                 case D3DTFP_NONE: value = WINED3DTEXF_NONE; break;
4092                 case D3DTFP_POINT: value = WINED3DTEXF_POINT; break;
4093                 case 0: /* Unchecked */
4094                 case D3DTFP_LINEAR: value = WINED3DTEXF_LINEAR; break;
4095                 default:
4096                     ERR("Unexpected mipfilter value %d\n", State);
4097                     value = WINED3DTEXF_NONE;
4098             }
4099             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4100                                                   Stage,
4101                                                   WINED3DSAMP_MIPFILTER,
4102                                                   value);
4103         }
4104
4105         /* Minfilter is a sampler state too, equal values */
4106         case D3DTSS_MINFILTER:
4107             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4108                                                   Stage,
4109                                                   WINED3DSAMP_MINFILTER,
4110                                                   State);
4111         /* Same for MAGFILTER */
4112         case D3DTSS_MAGFILTER:
4113             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4114                                                   Stage,
4115                                                   WINED3DSAMP_MAGFILTER,
4116                                                   State);
4117
4118         case D3DTSS_ADDRESS:
4119                    IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4120                                                   Stage,
4121                                                   WINED3DSAMP_ADDRESSV,
4122                                                   State);
4123             /* Drop through */
4124         case D3DTSS_ADDRESSU:
4125             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4126                                                   Stage,
4127                                                   WINED3DSAMP_ADDRESSU,
4128                                                   State);
4129         case D3DTSS_ADDRESSV:
4130             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
4131                                                   Stage,
4132                                                   WINED3DSAMP_ADDRESSV,
4133                                                   State);
4134         default:
4135
4136             return IWineD3DDevice_SetTextureStageState(This->wineD3DDevice,
4137                                                        Stage,
4138                                                        TexStageStateType,
4139                                                        State);
4140     }
4141 }
4142
4143 static HRESULT WINAPI
4144 Thunk_IDirect3DDeviceImpl_3_SetTextureStageState(IDirect3DDevice3 *iface,
4145                                                  DWORD Stage,
4146                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
4147                                                  DWORD State)
4148 {
4149     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4150     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
4151     return IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
4152                                                  Stage,
4153                                                  TexStageStateType,
4154                                                  State);
4155 }
4156
4157 /*****************************************************************************
4158  * IDirect3DDevice7::ValidateDevice
4159  *
4160  * SDK: "Reports the device's ability to render the currently set
4161  * texture-blending operations in a single pass". Whatever that means
4162  * exactly...
4163  *
4164  * Version 3 and 7
4165  *
4166  * Params:
4167  *  NumPasses: Address to write the number of necessary passes for the
4168  *             desired effect to.
4169  *
4170  * Returns:
4171  *  D3D_OK on success
4172  *  See IWineD3DDevice::ValidateDevice for more details
4173  *
4174  *****************************************************************************/
4175 static HRESULT WINAPI
4176 IDirect3DDeviceImpl_7_ValidateDevice(IDirect3DDevice7 *iface,
4177                                      DWORD *NumPasses)
4178 {
4179     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4180     TRACE("(%p)->(%p): Relay\n", This, NumPasses);
4181
4182     return IWineD3DDevice_ValidateDevice(This->wineD3DDevice, NumPasses);
4183 }
4184
4185 static HRESULT WINAPI
4186 Thunk_IDirect3DDeviceImpl_3_ValidateDevice(IDirect3DDevice3 *iface,
4187                                            DWORD *Passes)
4188 {
4189     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
4190     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Passes);
4191     return IDirect3DDevice7_ValidateDevice(ICOM_INTERFACE(This, IDirect3DDevice7),
4192                                            Passes);
4193 }
4194
4195 /*****************************************************************************
4196  * IDirect3DDevice7::Clear
4197  *
4198  * Fills the render target, the z buffer and the stencil buffer with a
4199  * clear color / value
4200  *
4201  * Version 7 only
4202  *
4203  * Params:
4204  *  Count: Number of rectangles in Rects must be 0 if Rects is NULL
4205  *  Rects: Rectangles to clear. If NULL, the whole surface is cleared
4206  *  Flags: Some flags, as usual
4207  *  Color: Clear color for the render target
4208  *  Z: Clear value for the Z buffer
4209  *  Stencil: Clear value to store in each stencil buffer entry
4210  *
4211  * Returns:
4212  *  D3D_OK on success
4213  *  For details, see IWineD3DDevice::Clear
4214  *
4215  *****************************************************************************/
4216 static HRESULT WINAPI
4217 IDirect3DDeviceImpl_7_Clear(IDirect3DDevice7 *iface,
4218                             DWORD Count,
4219                             D3DRECT *Rects,
4220                             DWORD Flags,
4221                             D3DCOLOR Color,
4222                             D3DVALUE Z,
4223                             DWORD Stencil)
4224 {
4225     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4226     TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x): Relay\n", This, Count, Rects, Flags, (DWORD) Color, Z, Stencil);
4227
4228     /* Note; D3DRECT is compatible with WINED3DRECT */
4229     return IWineD3DDevice_Clear(This->wineD3DDevice, Count, (WINED3DRECT*) Rects, Flags, Color, Z, Stencil);
4230 }
4231
4232 /*****************************************************************************
4233  * IDirect3DDevice7::SetViewport
4234  *
4235  * Sets the current viewport.
4236  *
4237  * Version 7 only, but IDirect3DViewport uses this call for older
4238  * versions
4239  *
4240  * Params:
4241  *  Data: The new viewport to set
4242  *
4243  * Returns:
4244  *  D3D_OK on success
4245  *  DDERR_INVALIDPARAMS if Data is NULL
4246  *  For more details, see IWineDDDevice::SetViewport
4247  *
4248  *****************************************************************************/
4249 static HRESULT WINAPI
4250 IDirect3DDeviceImpl_7_SetViewport(IDirect3DDevice7 *iface,
4251                                   D3DVIEWPORT7 *Data)
4252 {
4253     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4254     TRACE("(%p)->(%p) Relay!\n", This, Data);
4255
4256     if(!Data)
4257         return DDERR_INVALIDPARAMS;
4258
4259     /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4260     return IWineD3DDevice_SetViewport(This->wineD3DDevice,
4261                                       (WINED3DVIEWPORT*) Data);
4262 }
4263
4264 /*****************************************************************************
4265  * IDirect3DDevice::GetViewport
4266  *
4267  * Returns the current viewport
4268  *
4269  * Version 7
4270  *
4271  * Params:
4272  *  Data: D3D7Viewport structure to write the viewport information to
4273  *
4274  * Returns:
4275  *  D3D_OK on success
4276  *  DDERR_INVALIDPARAMS if Data is NULL
4277  *  For more details, see IWineD3DDevice::GetViewport
4278  *
4279  *****************************************************************************/
4280 static HRESULT WINAPI
4281 IDirect3DDeviceImpl_7_GetViewport(IDirect3DDevice7 *iface,
4282                                   D3DVIEWPORT7 *Data)
4283 {
4284     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4285     HRESULT hr;
4286     TRACE("(%p)->(%p) Relay!\n", This, Data);
4287
4288     if(!Data)
4289         return DDERR_INVALIDPARAMS;
4290
4291     /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4292     hr = IWineD3DDevice_GetViewport(This->wineD3DDevice,
4293                                     (WINED3DVIEWPORT*) Data);
4294
4295     return hr_ddraw_from_wined3d(hr);
4296 }
4297
4298 /*****************************************************************************
4299  * IDirect3DDevice7::SetMaterial
4300  *
4301  * Sets the Material
4302  *
4303  * Version 7
4304  *
4305  * Params:
4306  *  Mat: The material to set
4307  *
4308  * Returns:
4309  *  D3D_OK on success
4310  *  DDERR_INVALIDPARAMS if Mat is NULL.
4311  *  For more details, see IWineD3DDevice::SetMaterial
4312  *
4313  *****************************************************************************/
4314 static HRESULT WINAPI
4315 IDirect3DDeviceImpl_7_SetMaterial(IDirect3DDevice7 *iface,
4316                                   D3DMATERIAL7 *Mat)
4317 {
4318     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4319     HRESULT hr;
4320     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4321
4322     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */
4323     hr = IWineD3DDevice_SetMaterial(This->wineD3DDevice,
4324                                     (WINED3DMATERIAL*) Mat);
4325
4326     return hr_ddraw_from_wined3d(hr);
4327 }
4328
4329 /*****************************************************************************
4330  * IDirect3DDevice7::GetMaterial
4331  *
4332  * Returns the current material
4333  *
4334  * Version 7
4335  *
4336  * Params:
4337  *  Mat: D3DMATERIAL7 structure to write the material parameters to
4338  *
4339  * Returns:
4340  *  D3D_OK on success
4341  *  DDERR_INVALIDPARAMS if Mat is NULL
4342  *  For more details, see IWineD3DDevice::GetMaterial
4343  *
4344  *****************************************************************************/
4345 static HRESULT WINAPI
4346 IDirect3DDeviceImpl_7_GetMaterial(IDirect3DDevice7 *iface,
4347                                   D3DMATERIAL7 *Mat)
4348 {
4349     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4350     HRESULT hr;
4351     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4352
4353     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */ 
4354     hr = IWineD3DDevice_GetMaterial(This->wineD3DDevice,
4355                                     (WINED3DMATERIAL*) Mat);
4356
4357     return hr_ddraw_from_wined3d(hr);
4358 }
4359
4360 /*****************************************************************************
4361  * IDirect3DDevice7::SetLight
4362  *
4363  * Assigns a light to a light index, but doesn't activate it yet.
4364  *
4365  * Version 7, IDirect3DLight uses this method for older versions
4366  *
4367  * Params:
4368  *  LightIndex: The index of the new light
4369  *  Light: A D3DLIGHT7 structure describing the light
4370  *
4371  * Returns:
4372  *  D3D_OK on success
4373  *  For more details, see IWineD3DDevice::SetLight
4374  *
4375  *****************************************************************************/
4376 static HRESULT WINAPI
4377 IDirect3DDeviceImpl_7_SetLight(IDirect3DDevice7 *iface,
4378                                DWORD LightIndex,
4379                                D3DLIGHT7 *Light)
4380 {
4381     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4382     HRESULT hr;
4383     TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4384
4385     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4386     hr = IWineD3DDevice_SetLight(This->wineD3DDevice,
4387                                  LightIndex,
4388                                  (WINED3DLIGHT*) Light);
4389
4390     return hr_ddraw_from_wined3d(hr);
4391 }
4392
4393 /*****************************************************************************
4394  * IDirect3DDevice7::GetLight
4395  *
4396  * Returns the light assigned to a light index
4397  *
4398  * Params:
4399  *  Light: Structure to write the light information to
4400  *
4401  * Returns:
4402  *  D3D_OK on success
4403  *  DDERR_INVALIDPARAMS if Light is NULL
4404  *  For details, see IWineD3DDevice::GetLight
4405  *
4406  *****************************************************************************/
4407 static HRESULT WINAPI
4408 IDirect3DDeviceImpl_7_GetLight(IDirect3DDevice7 *iface,
4409                                DWORD LightIndex,
4410                                D3DLIGHT7 *Light)
4411 {
4412     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4413     HRESULT rc;
4414     TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4415
4416     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4417     rc =  IWineD3DDevice_GetLight(This->wineD3DDevice,
4418                                   LightIndex,
4419                                   (WINED3DLIGHT*) Light);
4420
4421     /* Translate the result. WineD3D returns other values than D3D7 */
4422     return hr_ddraw_from_wined3d(rc);
4423 }
4424
4425 /*****************************************************************************
4426  * IDirect3DDevice7::BeginStateBlock
4427  *
4428  * Begins recording to a stateblock
4429  *
4430  * Version 7
4431  *
4432  * Returns:
4433  *  D3D_OK on success
4434  *  For details see IWineD3DDevice::BeginStateBlock
4435  *
4436  *****************************************************************************/
4437 static HRESULT WINAPI
4438 IDirect3DDeviceImpl_7_BeginStateBlock(IDirect3DDevice7 *iface)
4439 {
4440     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4441     HRESULT hr;
4442     TRACE("(%p)->(): Relay!\n", This);
4443
4444     hr = IWineD3DDevice_BeginStateBlock(This->wineD3DDevice);
4445     return hr_ddraw_from_wined3d(hr);
4446 }
4447
4448 /*****************************************************************************
4449  * IDirect3DDevice7::EndStateBlock
4450  *
4451  * Stops recording to a state block and returns the created stateblock
4452  * handle.
4453  *
4454  * Version 7
4455  *
4456  * Params:
4457  *  BlockHandle: Address to store the stateblock's handle to
4458  *
4459  * Returns:
4460  *  D3D_OK on success
4461  *  DDERR_INVALIDPARAMS if BlockHandle is NULL
4462  *  See IWineD3DDevice::EndStateBlock for more details
4463  *
4464  *****************************************************************************/
4465 static HRESULT WINAPI
4466 IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
4467                                     DWORD *BlockHandle)
4468 {
4469     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4470     HRESULT hr;
4471     TRACE("(%p)->(%p): Relay!\n", This, BlockHandle);
4472
4473     if(!BlockHandle)
4474     {
4475         WARN("BlockHandle == NULL, returning DDERR_INVALIDPARAMS\n");
4476         return DDERR_INVALIDPARAMS;
4477     }
4478
4479     *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
4480     if(!*BlockHandle)
4481     {
4482         ERR("Cannot get a handle number for the stateblock\n");
4483         return DDERR_OUTOFMEMORY;
4484     }
4485     This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
4486     hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice,
4487                                       (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr);
4488     return hr_ddraw_from_wined3d(hr);
4489 }
4490
4491 /*****************************************************************************
4492  * IDirect3DDevice7::PreLoad
4493  *
4494  * Allows the app to signal that a texture will be used soon, to allow
4495  * the Direct3DDevice to load it to the video card in the meantime.
4496  *
4497  * Version 7
4498  *
4499  * Params:
4500  *  Texture: The texture to preload
4501  *
4502  * Returns:
4503  *  D3D_OK on success
4504  *  DDERR_INVALIDPARAMS if Texture is NULL
4505  *  See IWineD3DSurface::PreLoad for details
4506  *
4507  *****************************************************************************/
4508 static HRESULT WINAPI
4509 IDirect3DDeviceImpl_7_PreLoad(IDirect3DDevice7 *iface,
4510                               IDirectDrawSurface7 *Texture)
4511 {
4512     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4513     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
4514
4515     TRACE("(%p)->(%p): Relay!\n", This, surf);
4516
4517     if(!Texture)
4518         return DDERR_INVALIDPARAMS;
4519
4520     IWineD3DSurface_PreLoad(surf->WineD3DSurface);
4521     return D3D_OK;
4522 }
4523
4524 /*****************************************************************************
4525  * IDirect3DDevice7::ApplyStateBlock
4526  *
4527  * Activates the state stored in a state block handle.
4528  *
4529  * Params:
4530  *  BlockHandle: The stateblock handle to activate
4531  *
4532  * Returns:
4533  *  D3D_OK on success
4534  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4535  *
4536  *****************************************************************************/
4537 static HRESULT WINAPI
4538 IDirect3DDeviceImpl_7_ApplyStateBlock(IDirect3DDevice7 *iface,
4539                                       DWORD BlockHandle)
4540 {
4541     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4542     HRESULT hr;
4543     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4544
4545     if(!BlockHandle || BlockHandle > This->numHandles)
4546     {
4547         WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4548         return D3DERR_INVALIDSTATEBLOCK;
4549     }
4550     if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
4551     {
4552         WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4553         return D3DERR_INVALIDSTATEBLOCK;
4554     }
4555
4556     hr = IWineD3DStateBlock_Apply((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4557     return hr_ddraw_from_wined3d(hr);
4558 }
4559
4560 /*****************************************************************************
4561  * IDirect3DDevice7::CaptureStateBlock
4562  *
4563  * Updates a stateblock's values to the values currently set for the device
4564  *
4565  * Version 7
4566  *
4567  * Params:
4568  *  BlockHandle: Stateblock to update
4569  *
4570  * Returns:
4571  *  D3D_OK on success
4572  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4573  *  See IWineD3DDevice::CaptureStateBlock for more details
4574  *
4575  *****************************************************************************/
4576 static HRESULT WINAPI
4577 IDirect3DDeviceImpl_7_CaptureStateBlock(IDirect3DDevice7 *iface,
4578                                         DWORD BlockHandle)
4579 {
4580     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4581     HRESULT hr;
4582     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4583
4584     if(BlockHandle == 0 || BlockHandle > This->numHandles)
4585     {
4586         WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4587         return D3DERR_INVALIDSTATEBLOCK;
4588     }
4589     if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
4590     {
4591         WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4592         return D3DERR_INVALIDSTATEBLOCK;
4593     }
4594
4595     hr = IWineD3DStateBlock_Capture((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4596     return hr_ddraw_from_wined3d(hr);
4597 }
4598
4599 /*****************************************************************************
4600  * IDirect3DDevice7::DeleteStateBlock
4601  *
4602  * Deletes a stateblock handle. This means releasing the WineD3DStateBlock
4603  *
4604  * Version 7
4605  *
4606  * Params:
4607  *  BlockHandle: Stateblock handle to delete
4608  *
4609  * Returns:
4610  *  D3D_OK on success
4611  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is 0
4612  *
4613  *****************************************************************************/
4614 static HRESULT WINAPI
4615 IDirect3DDeviceImpl_7_DeleteStateBlock(IDirect3DDevice7 *iface,
4616                                        DWORD BlockHandle)
4617 {
4618     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4619     ULONG ref;
4620     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4621
4622     if(BlockHandle == 0 || BlockHandle > This->numHandles)
4623     {
4624         WARN("Out of range handle %d, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4625         return D3DERR_INVALIDSTATEBLOCK;
4626     }
4627     if(This->Handles[BlockHandle - 1].type != DDrawHandle_StateBlock)
4628     {
4629         WARN("Handle %d is not a stateblock, returning D3DERR_INVALIDSTATEBLOCK\n", BlockHandle);
4630         return D3DERR_INVALIDSTATEBLOCK;
4631     }
4632
4633     ref = IWineD3DStateBlock_Release((IWineD3DStateBlock *) This->Handles[BlockHandle - 1].ptr);
4634     if(ref)
4635     {
4636         ERR("Something is still holding the stateblock %p(Handle %d). Ref = %d\n", This->Handles[BlockHandle - 1].ptr, BlockHandle, ref);
4637     }
4638     This->Handles[BlockHandle - 1].ptr = NULL;
4639     This->Handles[BlockHandle - 1].type = DDrawHandle_Unknown;
4640
4641     return D3D_OK;
4642 }
4643
4644 /*****************************************************************************
4645  * IDirect3DDevice7::CreateStateBlock
4646  *
4647  * Creates a new state block handle.
4648  *
4649  * Version 7
4650  *
4651  * Params:
4652  *  Type: The state block type
4653  *  BlockHandle: Address to write the created handle to
4654  *
4655  * Returns:
4656  *   D3D_OK on success
4657  *   DDERR_INVALIDPARAMS if BlockHandle is NULL
4658  *
4659  *****************************************************************************/
4660 static HRESULT WINAPI
4661 IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
4662                                        D3DSTATEBLOCKTYPE Type,
4663                                        DWORD *BlockHandle)
4664 {
4665     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4666     HRESULT hr;
4667     TRACE("(%p)->(%08x,%p)!\n", This, Type, BlockHandle);
4668
4669     if(!BlockHandle)
4670     {
4671         WARN("BlockHandle == NULL, returning DDERR_INVALIDPARAMS\n");
4672         return DDERR_INVALIDPARAMS;
4673     }
4674
4675     *BlockHandle = IDirect3DDeviceImpl_CreateHandle(This);
4676     if(!*BlockHandle)
4677     {
4678         ERR("Cannot get a handle number for the stateblock\n");
4679         return DDERR_OUTOFMEMORY;
4680     }
4681     This->Handles[*BlockHandle - 1].type = DDrawHandle_StateBlock;
4682
4683     /* The D3DSTATEBLOCKTYPE enum is fine here */
4684     hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice,
4685                                          Type,
4686                                          (IWineD3DStateBlock **) &This->Handles[*BlockHandle - 1].ptr,
4687                                          NULL /* Parent, hope that works */);
4688     return hr_ddraw_from_wined3d(hr);
4689 }
4690
4691 /*****************************************************************************
4692  * IDirect3DDevice7::Load
4693  *
4694  * Loads a rectangular area from the source into the destination texture.
4695  * It can also copy the source to the faces of a cubic environment map
4696  *
4697  * Version 7
4698  *
4699  * Params:
4700  *  DestTex: Destination texture
4701  *  DestPoint: Point in the destination where the source image should be
4702  *             written to
4703  *  SrcTex: Source texture
4704  *  SrcRect: Source rectangle
4705  *  Flags: Some flags
4706  *
4707  * Returns:
4708  *  D3D_OK on success
4709  *  DDERR_INVALIDPARAMS if DestTex or SrcTex are NULL
4710  *  See IDirect3DTexture2::Load for details
4711  *
4712  *****************************************************************************/
4713 static HRESULT WINAPI
4714 IDirect3DDeviceImpl_7_Load(IDirect3DDevice7 *iface,
4715                            IDirectDrawSurface7 *DestTex,
4716                            POINT *DestPoint,
4717                            IDirectDrawSurface7 *SrcTex,
4718                            RECT *SrcRect,
4719                            DWORD Flags)
4720 {
4721     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4722     IDirectDrawSurfaceImpl *dest = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestTex);
4723     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcTex);
4724     FIXME("(%p)->(%p,%p,%p,%p,%08x): Partially Implemented!\n", This, dest, DestPoint, src, SrcRect, Flags);
4725
4726     if( (!src) || (!dest) )
4727         return DDERR_INVALIDPARAMS;
4728
4729     IDirect3DTexture2_Load(ICOM_INTERFACE(dest, IDirect3DTexture2),
4730                            ICOM_INTERFACE(src, IDirect3DTexture2));
4731     return D3D_OK;
4732 }
4733
4734 /*****************************************************************************
4735  * IDirect3DDevice7::LightEnable
4736  *
4737  * Enables or disables a light
4738  *
4739  * Version 7, IDirect3DLight uses this method too.
4740  *
4741  * Params:
4742  *  LightIndex: The index of the light to enable / disable
4743  *  Enable: Enable or disable the light
4744  *
4745  * Returns:
4746  *  D3D_OK on success
4747  *  For more details, see IWineD3DDevice::SetLightEnable
4748  *
4749  *****************************************************************************/
4750 static HRESULT WINAPI
4751 IDirect3DDeviceImpl_7_LightEnable(IDirect3DDevice7 *iface,
4752                                   DWORD LightIndex,
4753                                   BOOL Enable)
4754 {
4755     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4756     HRESULT hr;
4757     TRACE("(%p)->(%08x,%d): Relay!\n", This, LightIndex, Enable);
4758
4759     hr = IWineD3DDevice_SetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4760     return hr_ddraw_from_wined3d(hr);
4761 }
4762
4763 /*****************************************************************************
4764  * IDirect3DDevice7::GetLightEnable
4765  *
4766  * Retrieves if the light with the given index is enabled or not
4767  *
4768  * Version 7
4769  *
4770  * Params:
4771  *  LightIndex: Index of desired light
4772  *  Enable: Pointer to a BOOL which contains the result
4773  *
4774  * Returns:
4775  *  D3D_OK on success
4776  *  DDERR_INVALIDPARAMS if Enable is NULL
4777  *  See IWineD3DDevice::GetLightEnable for more details
4778  *
4779  *****************************************************************************/
4780 static HRESULT WINAPI
4781 IDirect3DDeviceImpl_7_GetLightEnable(IDirect3DDevice7 *iface,
4782                                      DWORD LightIndex,
4783                                      BOOL* Enable)
4784 {
4785     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4786     HRESULT hr;
4787     TRACE("(%p)->(%08x,%p): Relay\n", This, LightIndex, Enable);
4788
4789     if(!Enable)
4790         return DDERR_INVALIDPARAMS;
4791
4792     hr = IWineD3DDevice_GetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4793     return hr_ddraw_from_wined3d(hr);
4794 }
4795
4796 /*****************************************************************************
4797  * IDirect3DDevice7::SetClipPlane
4798  *
4799  * Sets custom clipping plane
4800  *
4801  * Version 7
4802  *
4803  * Params:
4804  *  Index: The index of the clipping plane
4805  *  PlaneEquation: An equation defining the clipping plane
4806  *
4807  * Returns:
4808  *  D3D_OK on success
4809  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4810  *  See IWineD3DDevice::SetClipPlane for more details
4811  *
4812  *****************************************************************************/
4813 static HRESULT WINAPI
4814 IDirect3DDeviceImpl_7_SetClipPlane(IDirect3DDevice7 *iface,
4815                                    DWORD Index,
4816                                    D3DVALUE* PlaneEquation)
4817 {
4818     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4819     TRACE("(%p)->(%08x,%p): Relay!\n", This, Index, PlaneEquation);
4820
4821     if(!PlaneEquation)
4822         return DDERR_INVALIDPARAMS;
4823
4824     return IWineD3DDevice_SetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4825 }
4826
4827 /*****************************************************************************
4828  * IDirect3DDevice7::GetClipPlane
4829  *
4830  * Returns the clipping plane with a specific index
4831  *
4832  * Params:
4833  *  Index: The index of the desired plane
4834  *  PlaneEquation: Address to store the plane equation to
4835  *
4836  * Returns:
4837  *  D3D_OK on success
4838  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4839  *  See IWineD3DDevice::GetClipPlane for more details
4840  *
4841  *****************************************************************************/
4842 static HRESULT WINAPI
4843 IDirect3DDeviceImpl_7_GetClipPlane(IDirect3DDevice7 *iface,
4844                                    DWORD Index,
4845                                    D3DVALUE* PlaneEquation)
4846 {
4847     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4848     TRACE("(%p)->(%d,%p): Relay!\n", This, Index, PlaneEquation);
4849
4850     if(!PlaneEquation)
4851         return DDERR_INVALIDPARAMS;
4852
4853     return IWineD3DDevice_GetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4854 }
4855
4856 /*****************************************************************************
4857  * IDirect3DDevice7::GetInfo
4858  *
4859  * Retrieves some information about the device. The DirectX sdk says that
4860  * this version returns S_FALSE for all retail builds of DirectX, that's what
4861  * this implementation does.
4862  *
4863  * Params:
4864  *  DevInfoID: Information type requested
4865  *  DevInfoStruct: Pointer to a structure to store the info to
4866  *  Size: Size of the structure
4867  *
4868  * Returns:
4869  *  S_FALSE, because it's a non-debug driver
4870  *
4871  *****************************************************************************/
4872 static HRESULT WINAPI
4873 IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
4874                               DWORD DevInfoID,
4875                               void *DevInfoStruct,
4876                               DWORD Size)
4877 {
4878     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4879     TRACE("(%p)->(%08x,%p,%08x)\n", This, DevInfoID, DevInfoStruct, Size);
4880
4881     if (TRACE_ON(d3d7))
4882     {
4883         TRACE(" info requested : ");
4884         switch (DevInfoID)
4885         {
4886             case D3DDEVINFOID_TEXTUREMANAGER: TRACE("D3DDEVINFOID_TEXTUREMANAGER\n"); break;
4887             case D3DDEVINFOID_D3DTEXTUREMANAGER: TRACE("D3DDEVINFOID_D3DTEXTUREMANAGER\n"); break;
4888             case D3DDEVINFOID_TEXTURING: TRACE("D3DDEVINFOID_TEXTURING\n"); break;
4889             default: ERR(" invalid flag !!!\n"); return DDERR_INVALIDPARAMS;
4890         }
4891     }
4892
4893     return S_FALSE; /* According to MSDN, this is valid for a non-debug driver */
4894 }
4895
4896 const IDirect3DDevice7Vtbl IDirect3DDevice7_Vtbl =
4897 {
4898     /*** IUnknown Methods ***/
4899     IDirect3DDeviceImpl_7_QueryInterface,
4900     IDirect3DDeviceImpl_7_AddRef,
4901     IDirect3DDeviceImpl_7_Release,
4902     /*** IDirect3DDevice7 ***/
4903     IDirect3DDeviceImpl_7_GetCaps,
4904     IDirect3DDeviceImpl_7_EnumTextureFormats,
4905     IDirect3DDeviceImpl_7_BeginScene,
4906     IDirect3DDeviceImpl_7_EndScene,
4907     IDirect3DDeviceImpl_7_GetDirect3D,
4908     IDirect3DDeviceImpl_7_SetRenderTarget,
4909     IDirect3DDeviceImpl_7_GetRenderTarget,
4910     IDirect3DDeviceImpl_7_Clear,
4911     IDirect3DDeviceImpl_7_SetTransform,
4912     IDirect3DDeviceImpl_7_GetTransform,
4913     IDirect3DDeviceImpl_7_SetViewport,
4914     IDirect3DDeviceImpl_7_MultiplyTransform,
4915     IDirect3DDeviceImpl_7_GetViewport,
4916     IDirect3DDeviceImpl_7_SetMaterial,
4917     IDirect3DDeviceImpl_7_GetMaterial,
4918     IDirect3DDeviceImpl_7_SetLight,
4919     IDirect3DDeviceImpl_7_GetLight,
4920     IDirect3DDeviceImpl_7_SetRenderState,
4921     IDirect3DDeviceImpl_7_GetRenderState,
4922     IDirect3DDeviceImpl_7_BeginStateBlock,
4923     IDirect3DDeviceImpl_7_EndStateBlock,
4924     IDirect3DDeviceImpl_7_PreLoad,
4925     IDirect3DDeviceImpl_7_DrawPrimitive,
4926     IDirect3DDeviceImpl_7_DrawIndexedPrimitive,
4927     IDirect3DDeviceImpl_7_SetClipStatus,
4928     IDirect3DDeviceImpl_7_GetClipStatus,
4929     IDirect3DDeviceImpl_7_DrawPrimitiveStrided,
4930     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided,
4931     IDirect3DDeviceImpl_7_DrawPrimitiveVB,
4932     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB,
4933     IDirect3DDeviceImpl_7_ComputeSphereVisibility,
4934     IDirect3DDeviceImpl_7_GetTexture,
4935     IDirect3DDeviceImpl_7_SetTexture,
4936     IDirect3DDeviceImpl_7_GetTextureStageState,
4937     IDirect3DDeviceImpl_7_SetTextureStageState,
4938     IDirect3DDeviceImpl_7_ValidateDevice,
4939     IDirect3DDeviceImpl_7_ApplyStateBlock,
4940     IDirect3DDeviceImpl_7_CaptureStateBlock,
4941     IDirect3DDeviceImpl_7_DeleteStateBlock,
4942     IDirect3DDeviceImpl_7_CreateStateBlock,
4943     IDirect3DDeviceImpl_7_Load,
4944     IDirect3DDeviceImpl_7_LightEnable,
4945     IDirect3DDeviceImpl_7_GetLightEnable,
4946     IDirect3DDeviceImpl_7_SetClipPlane,
4947     IDirect3DDeviceImpl_7_GetClipPlane,
4948     IDirect3DDeviceImpl_7_GetInfo
4949 };
4950
4951 const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
4952 {
4953     /*** IUnknown Methods ***/
4954     Thunk_IDirect3DDeviceImpl_3_QueryInterface,
4955     Thunk_IDirect3DDeviceImpl_3_AddRef,
4956     Thunk_IDirect3DDeviceImpl_3_Release,
4957     /*** IDirect3DDevice3 ***/
4958     IDirect3DDeviceImpl_3_GetCaps,
4959     IDirect3DDeviceImpl_3_GetStats,
4960     IDirect3DDeviceImpl_3_AddViewport,
4961     IDirect3DDeviceImpl_3_DeleteViewport,
4962     IDirect3DDeviceImpl_3_NextViewport,
4963     Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats,
4964     Thunk_IDirect3DDeviceImpl_3_BeginScene,
4965     Thunk_IDirect3DDeviceImpl_3_EndScene,
4966     Thunk_IDirect3DDeviceImpl_3_GetDirect3D,
4967     IDirect3DDeviceImpl_3_SetCurrentViewport,
4968     IDirect3DDeviceImpl_3_GetCurrentViewport,
4969     Thunk_IDirect3DDeviceImpl_3_SetRenderTarget,
4970     Thunk_IDirect3DDeviceImpl_3_GetRenderTarget,
4971     IDirect3DDeviceImpl_3_Begin,
4972     IDirect3DDeviceImpl_3_BeginIndexed,
4973     IDirect3DDeviceImpl_3_Vertex,
4974     IDirect3DDeviceImpl_3_Index,
4975     IDirect3DDeviceImpl_3_End,
4976     Thunk_IDirect3DDeviceImpl_3_GetRenderState,
4977     Thunk_IDirect3DDeviceImpl_3_SetRenderState,
4978     IDirect3DDeviceImpl_3_GetLightState,
4979     IDirect3DDeviceImpl_3_SetLightState,
4980     Thunk_IDirect3DDeviceImpl_3_SetTransform,
4981     Thunk_IDirect3DDeviceImpl_3_GetTransform,
4982     Thunk_IDirect3DDeviceImpl_3_MultiplyTransform,
4983     Thunk_IDirect3DDeviceImpl_3_DrawPrimitive,
4984     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive,
4985     Thunk_IDirect3DDeviceImpl_3_SetClipStatus,
4986     Thunk_IDirect3DDeviceImpl_3_GetClipStatus,
4987     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided,
4988     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided,
4989     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB,
4990     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB,
4991     Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility,
4992     Thunk_IDirect3DDeviceImpl_3_GetTexture,
4993     Thunk_IDirect3DDeviceImpl_3_SetTexture,
4994     Thunk_IDirect3DDeviceImpl_3_GetTextureStageState,
4995     Thunk_IDirect3DDeviceImpl_3_SetTextureStageState,
4996     Thunk_IDirect3DDeviceImpl_3_ValidateDevice
4997 };
4998
4999 const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
5000 {
5001     /*** IUnknown Methods ***/
5002     Thunk_IDirect3DDeviceImpl_2_QueryInterface,
5003     Thunk_IDirect3DDeviceImpl_2_AddRef,
5004     Thunk_IDirect3DDeviceImpl_2_Release,
5005     /*** IDirect3DDevice2 ***/
5006     Thunk_IDirect3DDeviceImpl_2_GetCaps,
5007     IDirect3DDeviceImpl_2_SwapTextureHandles,
5008     Thunk_IDirect3DDeviceImpl_2_GetStats,
5009     Thunk_IDirect3DDeviceImpl_2_AddViewport,
5010     Thunk_IDirect3DDeviceImpl_2_DeleteViewport,
5011     Thunk_IDirect3DDeviceImpl_2_NextViewport,
5012     IDirect3DDeviceImpl_2_EnumTextureFormats,
5013     Thunk_IDirect3DDeviceImpl_2_BeginScene,
5014     Thunk_IDirect3DDeviceImpl_2_EndScene,
5015     Thunk_IDirect3DDeviceImpl_2_GetDirect3D,
5016     Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport,
5017     Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport,
5018     Thunk_IDirect3DDeviceImpl_2_SetRenderTarget,
5019     Thunk_IDirect3DDeviceImpl_2_GetRenderTarget,
5020     Thunk_IDirect3DDeviceImpl_2_Begin,
5021     Thunk_IDirect3DDeviceImpl_2_BeginIndexed,
5022     Thunk_IDirect3DDeviceImpl_2_Vertex,
5023     Thunk_IDirect3DDeviceImpl_2_Index,
5024     Thunk_IDirect3DDeviceImpl_2_End,
5025     Thunk_IDirect3DDeviceImpl_2_GetRenderState,
5026     Thunk_IDirect3DDeviceImpl_2_SetRenderState,
5027     Thunk_IDirect3DDeviceImpl_2_GetLightState,
5028     Thunk_IDirect3DDeviceImpl_2_SetLightState,
5029     Thunk_IDirect3DDeviceImpl_2_SetTransform,
5030     Thunk_IDirect3DDeviceImpl_2_GetTransform,
5031     Thunk_IDirect3DDeviceImpl_2_MultiplyTransform,
5032     Thunk_IDirect3DDeviceImpl_2_DrawPrimitive,
5033     Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive,
5034     Thunk_IDirect3DDeviceImpl_2_SetClipStatus,
5035     Thunk_IDirect3DDeviceImpl_2_GetClipStatus
5036 };
5037
5038 const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
5039 {
5040     /*** IUnknown Methods ***/
5041     Thunk_IDirect3DDeviceImpl_1_QueryInterface,
5042     Thunk_IDirect3DDeviceImpl_1_AddRef,
5043     Thunk_IDirect3DDeviceImpl_1_Release,
5044     /*** IDirect3DDevice1 ***/
5045     IDirect3DDeviceImpl_1_Initialize,
5046     Thunk_IDirect3DDeviceImpl_1_GetCaps,
5047     Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles,
5048     IDirect3DDeviceImpl_1_CreateExecuteBuffer,
5049     Thunk_IDirect3DDeviceImpl_1_GetStats,
5050     IDirect3DDeviceImpl_1_Execute,
5051     Thunk_IDirect3DDeviceImpl_1_AddViewport,
5052     Thunk_IDirect3DDeviceImpl_1_DeleteViewport,
5053     Thunk_IDirect3DDeviceImpl_1_NextViewport,
5054     IDirect3DDeviceImpl_1_Pick,
5055     IDirect3DDeviceImpl_1_GetPickRecords,
5056     Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats,
5057     IDirect3DDeviceImpl_1_CreateMatrix,
5058     IDirect3DDeviceImpl_1_SetMatrix,
5059     IDirect3DDeviceImpl_1_GetMatrix,
5060     IDirect3DDeviceImpl_1_DeleteMatrix,
5061     Thunk_IDirect3DDeviceImpl_1_EndScene,
5062     Thunk_IDirect3DDeviceImpl_1_BeginScene,
5063     Thunk_IDirect3DDeviceImpl_1_GetDirect3D
5064 };
5065
5066 /*****************************************************************************
5067  * IDirect3DDeviceImpl_CreateHandle
5068  *
5069  * Not called from the VTable
5070  *
5071  * Some older interface versions operate with handles, which are basically
5072  * DWORDs which identify an interface, for example
5073  * IDirect3DDevice::SetRenderState with DIRECT3DRENDERSTATE_TEXTUREHANDLE
5074  *
5075  * Those handle could be just casts to the interface pointers or vice versa,
5076  * but that is not 64 bit safe and would mean blindly derefering a DWORD
5077  * passed by the app. Instead there is a dynamic array in the device which
5078  * keeps a DWORD to pointer information and a type for the handle.
5079  *
5080  * Basically this array only grows, when a handle is freed its pointer is
5081  * just set to NULL. There will be much more reads from the array than
5082  * insertion operations, so a dynamic array is fine.
5083  *
5084  * Params:
5085  *  This: D3DDevice implementation for which this handle should be created
5086  *
5087  * Returns:
5088  *  A free handle on success
5089  *  0 on failure
5090  *
5091  *****************************************************************************/
5092 DWORD
5093 IDirect3DDeviceImpl_CreateHandle(IDirect3DDeviceImpl *This)
5094 {
5095     DWORD i;
5096     struct HandleEntry *oldHandles = This->Handles;
5097
5098     TRACE("(%p)\n", This);
5099
5100     for(i = 0; i < This->numHandles; i++)
5101     {
5102         if(This->Handles[i].ptr == NULL &&
5103            This->Handles[i].type == DDrawHandle_Unknown)
5104         {
5105             TRACE("Reusing freed handle %d\n", i + 1);
5106             return i + 1;
5107         }
5108     }
5109
5110     TRACE("Growing the handle array\n");
5111
5112     This->numHandles++;
5113     This->Handles = HeapAlloc(GetProcessHeap(), 0, sizeof(struct HandleEntry) * This->numHandles);
5114     if(!This->Handles)
5115     {
5116         ERR("Out of memory\n");
5117         This->Handles = oldHandles;
5118         This->numHandles--;
5119         return 0;
5120     }
5121     if(oldHandles)
5122     {
5123         memcpy(This->Handles, oldHandles, (This->numHandles - 1) * sizeof(struct HandleEntry));
5124         HeapFree(GetProcessHeap(), 0, oldHandles);
5125     }
5126
5127     TRACE("Returning %d\n", This->numHandles);
5128     return This->numHandles;
5129 }