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