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