wined3d: Add D3DTEXTUREOP to 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, WINED3DTOP_MODULATE);
2258                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, WINED3DTOP_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, WINED3DTOP_MODULATE);
2267                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, WINED3DTOP_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, WINED3DTOP_SELECTARG1);
2274                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, WINED3DTOP_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, WINED3DTOP_SELECTARG1);
2282                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, WINED3DTOP_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     /* FIXME:
2587        Unhandled: D3DTRANSFORMSTATE_WORLD1
2588        Unhandled: D3DTRANSFORMSTATE_WORLD2
2589        Unhandled: D3DTRANSFORMSTATE_WORLD3
2590      */
2591
2592     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2593     return IWineD3DDevice_SetTransform(This->wineD3DDevice,
2594                                        type,
2595                                        (WINED3DMATRIX*) Matrix);
2596 }
2597
2598 static HRESULT WINAPI
2599 Thunk_IDirect3DDeviceImpl_3_SetTransform(IDirect3DDevice3 *iface,
2600                                          D3DTRANSFORMSTATETYPE TransformStateType,
2601                                          D3DMATRIX *D3DMatrix)
2602 {
2603     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2604     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2605     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2606                                          TransformStateType,
2607                                          D3DMatrix);
2608 }
2609
2610 static HRESULT WINAPI
2611 Thunk_IDirect3DDeviceImpl_2_SetTransform(IDirect3DDevice2 *iface,
2612                                          D3DTRANSFORMSTATETYPE TransformStateType,
2613                                          D3DMATRIX *D3DMatrix)
2614 {
2615     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2616     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2617     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2618                                          TransformStateType,
2619                                          D3DMatrix);
2620 }
2621
2622 /*****************************************************************************
2623  * IDirect3DDevice7::GetTransform
2624  *
2625  * Returns the matrix assigned to a transform state
2626  * D3DTRANSFORMSTATE_WORLD is translated to D3DTS_WORLDMATRIX(0), see
2627  * SetTransform
2628  *
2629  * Params:
2630  *  TransformStateType: State to read the matrix from
2631  *  Matrix: Address to store the matrix at
2632  *
2633  * Returns:
2634  *  D3D_OK on success
2635  *  DDERR_INVALIDPARAMS if Matrix == NULL
2636  *  For details, see IWineD3DDevice::GetTransform
2637  *
2638  *****************************************************************************/
2639 static HRESULT WINAPI
2640 IDirect3DDeviceImpl_7_GetTransform(IDirect3DDevice7 *iface,
2641                                    D3DTRANSFORMSTATETYPE TransformStateType,
2642                                    D3DMATRIX *Matrix)
2643 {
2644     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2645     D3DTRANSFORMSTATETYPE type = TransformStateType;
2646     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2647
2648     if(!Matrix)
2649         return DDERR_INVALIDPARAMS;
2650
2651     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2652      * use D3DTS_WORLDMATRIX(0) instead
2653      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2654      */
2655     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2656         type = (D3DTRANSFORMSTATETYPE)(0 + 256);
2657
2658     /* FIXME:
2659        Unhandled: D3DTRANSFORMSTATE_WORLD1
2660        Unhandled: D3DTRANSFORMSTATE_WORLD2
2661        Unhandled: D3DTRANSFORMSTATE_WORLD3
2662      */
2663
2664     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2665     return IWineD3DDevice_GetTransform(This->wineD3DDevice, type, (WINED3DMATRIX*) Matrix);
2666 }
2667
2668 static HRESULT WINAPI
2669 Thunk_IDirect3DDeviceImpl_3_GetTransform(IDirect3DDevice3 *iface,
2670                                          D3DTRANSFORMSTATETYPE TransformStateType,
2671                                          D3DMATRIX *D3DMatrix)
2672 {
2673     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2674     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2675     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2676                                          TransformStateType,
2677                                          D3DMatrix);
2678 }
2679
2680 static HRESULT WINAPI
2681 Thunk_IDirect3DDeviceImpl_2_GetTransform(IDirect3DDevice2 *iface,
2682                                          D3DTRANSFORMSTATETYPE TransformStateType,
2683                                          D3DMATRIX *D3DMatrix)
2684 {
2685     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2686     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2687     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2688                                          TransformStateType,
2689                                          D3DMatrix);
2690 }
2691
2692 /*****************************************************************************
2693  * IDirect3DDevice7::MultiplyTransform
2694  *
2695  * Multiplies the already-set transform matrix of a transform state
2696  * with another matrix. For the world matrix, see SetTransform
2697  *
2698  * Version 2, 3 and 7
2699  *
2700  * Params:
2701  *  TransformStateType: Transform state to multiply
2702  *  D3DMatrix Matrix to multiply with.
2703  *
2704  * Returns
2705  *  D3D_OK on success
2706  *  DDERR_INVALIDPARAMS if D3DMatrix is NULL
2707  *  For details, see IWineD3DDevice::MultiplyTransform
2708  *
2709  *****************************************************************************/
2710 static HRESULT WINAPI
2711 IDirect3DDeviceImpl_7_MultiplyTransform(IDirect3DDevice7 *iface,
2712                                         D3DTRANSFORMSTATETYPE TransformStateType,
2713                                         D3DMATRIX *D3DMatrix)
2714 {
2715     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2716     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, D3DMatrix);
2717
2718     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2719      * use D3DTS_WORLDMATRIX(0) instead
2720      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2721      */
2722     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2723         TransformStateType = (D3DTRANSFORMSTATETYPE)(0 + 256);
2724
2725     /* FIXME:
2726        Unhandled: D3DTRANSFORMSTATE_WORLD1
2727        Unhandled: D3DTRANSFORMSTATE_WORLD2
2728        Unhandled: D3DTRANSFORMSTATE_WORLD3
2729      */
2730
2731     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
2732     return IWineD3DDevice_MultiplyTransform(This->wineD3DDevice,
2733                                             TransformStateType,
2734                                             (WINED3DMATRIX*) D3DMatrix);
2735 }
2736
2737 static HRESULT WINAPI
2738 Thunk_IDirect3DDeviceImpl_3_MultiplyTransform(IDirect3DDevice3 *iface,
2739                                               D3DTRANSFORMSTATETYPE TransformStateType,
2740                                               D3DMATRIX *D3DMatrix)
2741 {
2742     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2743     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2744     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2745                                               TransformStateType,
2746                                               D3DMatrix);
2747 }
2748
2749 static HRESULT WINAPI
2750 Thunk_IDirect3DDeviceImpl_2_MultiplyTransform(IDirect3DDevice2 *iface,
2751                                               D3DTRANSFORMSTATETYPE TransformStateType,
2752                                               D3DMATRIX *D3DMatrix)
2753 {
2754     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2755     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2756     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2757                                               TransformStateType,
2758                                               D3DMatrix);
2759 }
2760
2761 /*****************************************************************************
2762  * IDirect3DDevice7::DrawPrimitive
2763  *
2764  * Draws primitives based on vertices in an application-provided pointer
2765  *
2766  * Version 2, 3 and 7. The IDirect3DDevice2 thunk converts the fixed vertex type into
2767  * an FVF format for D3D7
2768  *
2769  * Params:
2770  *  PrimitiveType: The type of the primitives to draw
2771  *  Vertex type: Flexible vertex format vertex description
2772  *  Vertices: Pointer to the vertex array
2773  *  VertexCount: The number of vertices to draw
2774  *  Flags: As usual a few flags
2775  *
2776  * Returns:
2777  *  D3D_OK on success
2778  *  DDERR_INVALIDPARAMS if Vertices is NULL
2779  *  For details, see IWineD3DDevice::DrawPrimitiveUP
2780  *
2781  *****************************************************************************/
2782 static HRESULT WINAPI
2783 IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
2784                                     D3DPRIMITIVETYPE PrimitiveType,
2785                                     DWORD VertexType,
2786                                     void *Vertices,
2787                                     DWORD VertexCount,
2788                                     DWORD Flags)
2789 {
2790     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2791     UINT PrimitiveCount, stride;
2792     HRESULT hr;
2793     TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2794
2795     if(!Vertices)
2796         return DDERR_INVALIDPARAMS;
2797
2798     /* Get the vertex count */
2799     switch(PrimitiveType)
2800     {
2801       case D3DPT_POINTLIST: 
2802         PrimitiveCount = VertexCount;
2803         break;
2804
2805       case D3DPT_LINELIST: 
2806         PrimitiveCount = VertexCount / 2;
2807         break;
2808
2809       case D3DPT_LINESTRIP:
2810         PrimitiveCount = VertexCount - 1;
2811         break;
2812
2813       case D3DPT_TRIANGLELIST:
2814         PrimitiveCount = VertexCount / 3;
2815         break;
2816
2817       case D3DPT_TRIANGLESTRIP:
2818         PrimitiveCount = VertexCount - 2;
2819         break;
2820
2821       case D3DPT_TRIANGLEFAN:
2822         PrimitiveCount = VertexCount - 2;
2823         break;
2824
2825       default: return DDERR_INVALIDPARAMS;
2826     }
2827
2828     /* Get the stride */
2829     stride = get_flexible_vertex_size(VertexType);
2830
2831     /* Set the FVF */
2832     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, VertexType);
2833     if(hr != D3D_OK) return hr;
2834
2835     /* This method translates to the user pointer draw of WineD3D */
2836     return IWineD3DDevice_DrawPrimitiveUP(This->wineD3DDevice,
2837                                           PrimitiveType,
2838                                           PrimitiveCount,
2839                                           Vertices,
2840                                           stride);
2841 }
2842
2843 static HRESULT WINAPI
2844 Thunk_IDirect3DDeviceImpl_3_DrawPrimitive(IDirect3DDevice3 *iface,
2845                                           D3DPRIMITIVETYPE PrimitiveType,
2846                                           DWORD VertexType,
2847                                           void *Vertices,
2848                                           DWORD VertexCount,
2849                                           DWORD Flags)
2850 {
2851     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2852     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2853     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2854                                           PrimitiveType,
2855                                           VertexType,
2856                                           Vertices,
2857                                           VertexCount,
2858                                           Flags);
2859 }
2860
2861 static HRESULT WINAPI
2862 Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
2863                                           D3DPRIMITIVETYPE PrimitiveType,
2864                                           D3DVERTEXTYPE VertexType,
2865                                           void *Vertices,
2866                                           DWORD VertexCount,
2867                                           DWORD Flags)
2868 {
2869     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2870     DWORD FVF;
2871     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2872
2873     switch(VertexType)
2874     {
2875         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
2876         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
2877         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
2878         default:
2879             ERR("Unexpected vertex type %d\n", VertexType);
2880             return DDERR_INVALIDPARAMS;  /* Should never happen */
2881     }
2882
2883     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2884                                           PrimitiveType,
2885                                           FVF,
2886                                           Vertices,
2887                                           VertexCount,
2888                                           Flags);
2889 }
2890
2891 /*****************************************************************************
2892  * IDirect3DDevice7::DrawIndexedPrimitive
2893  *
2894  * Draws vertices from an application-provided pointer, based on the index
2895  * numbers in a WORD array.
2896  *
2897  * Version 2, 3 and 7. The version 7 thunk translates the vertex type into
2898  * an FVF format for D3D7
2899  *
2900  * Params:
2901  *  PrimitiveType: The primitive type to draw
2902  *  VertexType: The FVF vertex description
2903  *  Vertices: Pointer to the vertex array
2904  *  VertexCount: ?
2905  *  Indices: Pointer to the index array
2906  *  IndexCount: Number of indices = Number of vertices to draw
2907  *  Flags: As usual, some flags
2908  *
2909  * Returns:
2910  *  D3D_OK on success
2911  *  DDERR_INVALIDPARAMS if Vertices or Indices is NULL
2912  *  For details, see IWineD3DDevice::DrawIndexedPrimitiveUP
2913  *
2914  *****************************************************************************/
2915 static HRESULT WINAPI
2916 IDirect3DDeviceImpl_7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
2917                                            D3DPRIMITIVETYPE PrimitiveType,
2918                                            DWORD VertexType,
2919                                            void *Vertices,
2920                                            DWORD VertexCount,
2921                                            WORD *Indices,
2922                                            DWORD IndexCount,
2923                                            DWORD Flags)
2924 {
2925     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2926     UINT PrimitiveCount = 0;
2927     HRESULT hr;
2928     TRACE("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
2929     /* Get the primitive number */
2930     switch(PrimitiveType)
2931     {
2932       case D3DPT_POINTLIST: 
2933         PrimitiveCount = IndexCount;
2934         break;
2935
2936       case D3DPT_LINELIST: 
2937         PrimitiveCount = IndexCount / 2;
2938         break;
2939
2940       case D3DPT_LINESTRIP:
2941         PrimitiveCount = IndexCount - 1;
2942         break;
2943
2944       case D3DPT_TRIANGLELIST:
2945         PrimitiveCount = IndexCount / 3;
2946         break;
2947
2948       case D3DPT_TRIANGLESTRIP:
2949         PrimitiveCount = IndexCount - 2;
2950         break;
2951
2952       case D3DPT_TRIANGLEFAN:
2953         PrimitiveCount = IndexCount - 2;
2954         break;
2955
2956       default: return DDERR_INVALIDPARAMS;
2957     }
2958
2959     /* Set the D3DDevice's FVF */
2960     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, VertexType);
2961     if(FAILED(hr))
2962     {
2963         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
2964         return hr;
2965     }
2966
2967     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->wineD3DDevice,
2968                                                  PrimitiveType,
2969                                                  0 /* MinVertexIndex */,
2970                                                  VertexCount /* UINT NumVertexIndex */,
2971                                                  PrimitiveCount,
2972                                                  Indices,
2973                                                  WINED3DFMT_INDEX16,
2974                                                  Vertices,
2975                                                  get_flexible_vertex_size(VertexType));
2976 }
2977
2978 static HRESULT WINAPI
2979 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
2980                                                  D3DPRIMITIVETYPE PrimitiveType,
2981                                                  DWORD VertexType,
2982                                                  void *Vertices,
2983                                                  DWORD VertexCount,
2984                                                  WORD *Indices,
2985                                                  DWORD IndexCount,
2986                                                  DWORD Flags)
2987 {
2988     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2989     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
2990     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2991                                                  PrimitiveType,
2992                                                  VertexType,
2993                                                  Vertices,
2994                                                  VertexCount,
2995                                                  Indices,
2996                                                  IndexCount,
2997                                                  Flags);
2998 }
2999
3000 static HRESULT WINAPI
3001 Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
3002                                                  D3DPRIMITIVETYPE PrimitiveType,
3003                                                  D3DVERTEXTYPE VertexType,
3004                                                  void *Vertices,
3005                                                  DWORD VertexCount,
3006                                                  WORD *Indices,
3007                                                  DWORD IndexCount,
3008                                                  DWORD Flags)
3009 {
3010     DWORD FVF;
3011     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3012     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
3013
3014     switch(VertexType)
3015     {
3016         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
3017         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
3018         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
3019         default:
3020             ERR("Unexpected vertex type %d\n", VertexType);
3021             return DDERR_INVALIDPARAMS;  /* Should never happen */
3022     }
3023
3024     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
3025                                                  PrimitiveType,
3026                                                  FVF,
3027                                                  Vertices,
3028                                                  VertexCount,
3029                                                  Indices,
3030                                                  IndexCount,
3031                                                  Flags);
3032 }
3033
3034 /*****************************************************************************
3035  * IDirect3DDevice7::SetClipStatus
3036  *
3037  * Sets the clip status. This defines things as clipping conditions and
3038  * the extents of the clipping region.
3039  *
3040  * Version 2, 3 and 7
3041  *
3042  * Params:
3043  *  ClipStatus:
3044  *
3045  * Returns:
3046  *  D3D_OK because it's a stub
3047  *  (DDERR_INVALIDPARAMS if ClipStatus == NULL)
3048  *
3049  *****************************************************************************/
3050 static HRESULT WINAPI
3051 IDirect3DDeviceImpl_7_SetClipStatus(IDirect3DDevice7 *iface,
3052                                     D3DCLIPSTATUS *ClipStatus)
3053 {
3054     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3055     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3056
3057     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
3058      * Perhaps this needs a new data type and an additional IWineD3DDevice method
3059      */
3060     /* return IWineD3DDevice_SetClipStatus(This->wineD3DDevice, ClipStatus);*/
3061     return D3D_OK;
3062 }
3063
3064 static HRESULT WINAPI
3065 Thunk_IDirect3DDeviceImpl_3_SetClipStatus(IDirect3DDevice3 *iface,
3066                                           D3DCLIPSTATUS *ClipStatus)
3067 {
3068     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3069     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3070     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3071                                           ClipStatus);
3072 }
3073
3074 static HRESULT WINAPI
3075 Thunk_IDirect3DDeviceImpl_2_SetClipStatus(IDirect3DDevice2 *iface,
3076                                           D3DCLIPSTATUS *ClipStatus)
3077 {
3078     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3079     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3080     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3081                                           ClipStatus);
3082 }
3083
3084 /*****************************************************************************
3085  * IDirect3DDevice7::GetClipStatus
3086  *
3087  * Returns the clip status
3088  *
3089  * Params:
3090  *  ClipStatus: Address to write the clip status to
3091  *
3092  * Returns:
3093  *  D3D_OK because it's a stub
3094  *
3095  *****************************************************************************/
3096 static HRESULT WINAPI
3097 IDirect3DDeviceImpl_7_GetClipStatus(IDirect3DDevice7 *iface,
3098                                     D3DCLIPSTATUS *ClipStatus)
3099 {
3100     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3101     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3102
3103     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
3104     /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
3105     return D3D_OK;
3106 }
3107
3108 static HRESULT WINAPI
3109 Thunk_IDirect3DDeviceImpl_3_GetClipStatus(IDirect3DDevice3 *iface,
3110                                           D3DCLIPSTATUS *ClipStatus)
3111 {
3112     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3113     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3114     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3115                                           ClipStatus);
3116 }
3117
3118 static HRESULT WINAPI
3119 Thunk_IDirect3DDeviceImpl_2_GetClipStatus(IDirect3DDevice2 *iface,
3120                                           D3DCLIPSTATUS *ClipStatus)
3121 {
3122     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3123     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3124     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3125                                           ClipStatus);
3126 }
3127
3128 /*****************************************************************************
3129  * IDirect3DDevice::DrawPrimitiveStrided
3130  *
3131  * Draws vertices described by a D3DDRAWPRIMITIVESTRIDEDDATA structure.
3132  *
3133  * Version 3 and 7
3134  *
3135  * Params:
3136  *  PrimitiveType: The primitive type to draw
3137  *  VertexType: The FVF description of the vertices to draw (for the stride??)
3138  *  D3DDrawPrimStrideData: A D3DDRAWPRIMITIVESTRIDEDDATA structure describing
3139  *                         the vertex data locations
3140  *  VertexCount: The number of vertices to draw
3141  *  Flags: Some flags
3142  *
3143  * Returns:
3144  *  D3D_OK, because it's a stub
3145  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3146  *  (For details, see IWineD3DDevice::DrawPrimitiveStrided)
3147  *
3148  *****************************************************************************/
3149 static HRESULT WINAPI
3150 IDirect3DDeviceImpl_7_DrawPrimitiveStrided(IDirect3DDevice7 *iface,
3151                                            D3DPRIMITIVETYPE PrimitiveType,
3152                                            DWORD VertexType,
3153                                            D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3154                                            DWORD VertexCount,
3155                                            DWORD Flags)
3156 {
3157     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3158     WineDirect3DVertexStridedData WineD3DStrided;
3159     int i;
3160     UINT PrimitiveCount;
3161
3162     TRACE("(%p)->(%08x,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3163
3164     /* Get the strided data right. the wined3d structure is a bit bigger
3165      * Watch out: The contents of the strided data are determined by the fvf,
3166      * not by the members set in D3DDrawPrimStrideData. So it's valid
3167      * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
3168      * not set in the fvf.
3169      */
3170     if(VertexType & D3DFVF_POSITION_MASK)
3171     {
3172         memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
3173         WineD3DStrided.u.s.position.lpData = D3DDrawPrimStrideData->position.lpvData;
3174         WineD3DStrided.u.s.position.dwStride = D3DDrawPrimStrideData->position.dwStride;
3175         WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
3176         if (VertexType & D3DFVF_XYZRHW)
3177         {
3178             WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
3179             WineD3DStrided.u.s.position_transformed = TRUE;
3180         } else
3181             WineD3DStrided.u.s.position_transformed = FALSE;
3182     }
3183
3184     if(VertexType & D3DFVF_NORMAL)
3185     {
3186         WineD3DStrided.u.s.normal.lpData = D3DDrawPrimStrideData->normal.lpvData;
3187         WineD3DStrided.u.s.normal.dwStride = D3DDrawPrimStrideData->normal.dwStride;
3188         WineD3DStrided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
3189     }
3190
3191     if(VertexType & D3DFVF_DIFFUSE)
3192     {
3193         WineD3DStrided.u.s.diffuse.lpData = D3DDrawPrimStrideData->diffuse.lpvData;
3194         WineD3DStrided.u.s.diffuse.dwStride = D3DDrawPrimStrideData->diffuse.dwStride;
3195         WineD3DStrided.u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
3196     }
3197
3198     if(VertexType & D3DFVF_SPECULAR)
3199     {
3200         WineD3DStrided.u.s.specular.lpData = D3DDrawPrimStrideData->specular.lpvData;
3201         WineD3DStrided.u.s.specular.dwStride = D3DDrawPrimStrideData->specular.dwStride;
3202         WineD3DStrided.u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
3203     }
3204
3205     for( i = 0; i < GET_TEXCOUNT_FROM_FVF(VertexType); i++)
3206     {
3207         WineD3DStrided.u.s.texCoords[i].lpData = D3DDrawPrimStrideData->textureCoords[i].lpvData;
3208         WineD3DStrided.u.s.texCoords[i].dwStride = D3DDrawPrimStrideData->textureCoords[i].dwStride;
3209         switch(GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i))
3210         {
3211             case 1: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT1; break;
3212             case 2: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT2; break;
3213             case 3: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT3; break;
3214             case 4: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT4; break;
3215             default: ERR("Unexpected texture coordinate size %d\n",
3216                          GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i));
3217         }
3218     }
3219
3220     /* Get the primitive count */
3221     switch(PrimitiveType)
3222     {
3223         case D3DPT_POINTLIST: 
3224           PrimitiveCount = VertexCount;
3225           break;
3226
3227         case D3DPT_LINELIST: 
3228           PrimitiveCount = VertexCount / 2;
3229           break;
3230
3231         case D3DPT_LINESTRIP:
3232           PrimitiveCount = VertexCount - 1;
3233           break;
3234
3235         case D3DPT_TRIANGLELIST:
3236           PrimitiveCount = VertexCount / 3;
3237           break;
3238
3239         case D3DPT_TRIANGLESTRIP:
3240           PrimitiveCount = VertexCount - 2;
3241           break;
3242
3243         case D3DPT_TRIANGLEFAN:
3244           PrimitiveCount = VertexCount - 2;
3245           break;
3246
3247         default: return DDERR_INVALIDPARAMS;
3248     }
3249
3250     IWineD3DDevice_SetFVF(This->wineD3DDevice,
3251                           VertexType);
3252
3253     return IWineD3DDevice_DrawPrimitiveStrided(This->wineD3DDevice,
3254                                                PrimitiveType,
3255                                                PrimitiveCount,
3256                                                &WineD3DStrided);
3257 }
3258
3259 static HRESULT WINAPI
3260 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
3261                                                  D3DPRIMITIVETYPE PrimitiveType,
3262                                                  DWORD VertexType,
3263                                                  D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3264                                                  DWORD VertexCount,
3265                                                  DWORD Flags)
3266 {
3267     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3268     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3269     return IDirect3DDevice7_DrawPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3270                                                  PrimitiveType,
3271                                                  VertexType,
3272                                                  D3DDrawPrimStrideData,
3273                                                  VertexCount,
3274                                                  Flags);
3275 }
3276
3277 /*****************************************************************************
3278  * IDirect3DDevice7::DrawIndexedPrimitiveStrided
3279  *
3280  * Draws primitives specified by strided data locations based on indices
3281  *
3282  * Version 3 and 7
3283  *
3284  * Params:
3285  *  PrimitiveType:
3286  *
3287  * Returns:
3288  *  D3D_OK, because it's a stub
3289  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3290  *  (DDERR_INVALIDPARAMS if Indices is NULL)
3291  *  (For more details, see IWineD3DDevice::DrawIndexedPrimitiveStrided)
3292  *
3293  *****************************************************************************/
3294 static HRESULT WINAPI
3295 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
3296                                                   D3DPRIMITIVETYPE PrimitiveType,
3297                                                   DWORD VertexType,
3298                                                   D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3299                                                   DWORD VertexCount,
3300                                                   WORD *Indices,
3301                                                   DWORD IndexCount,
3302                                                   DWORD Flags)
3303 {
3304     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3305     FIXME("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3306
3307     /* I'll implement it as soon as I find a app to test it.
3308      * This needs an additional method in IWineD3DDevice.
3309      */
3310     return D3D_OK;
3311 }
3312
3313 static HRESULT WINAPI
3314 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
3315                                                         D3DPRIMITIVETYPE PrimitiveType,
3316                                                         DWORD VertexType,
3317                                                         D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3318                                                         DWORD VertexCount,
3319                                                         WORD *Indices,
3320                                                         DWORD IndexCount,
3321                                                         DWORD Flags)
3322 {
3323     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3324     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08x,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3325     return IDirect3DDevice7_DrawIndexedPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3326                                                         PrimitiveType,
3327                                                         VertexType,
3328                                                         D3DDrawPrimStrideData,
3329                                                         VertexCount,
3330                                                         Indices,
3331                                                         IndexCount,
3332                                                         Flags);
3333 }
3334
3335 /*****************************************************************************
3336  * IDirect3DDevice7::DrawPrimitiveVB
3337  *
3338  * Draws primitives from a vertex buffer to the screen.
3339  *
3340  * Version 3 and 7
3341  *
3342  * Params:
3343  *  PrimitiveType: Type of primitive to be rendered.
3344  *  D3DVertexBuf: Source Vertex Buffer
3345  *  StartVertex: Index of the first vertex from the buffer to be rendered
3346  *  NumVertices: Number of vertices to be rendered
3347  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3348  *
3349  * Return values
3350  *  D3D_OK on success
3351  *  DDERR_INVALIDPARAMS if D3DVertexBuf is NULL
3352  *
3353  *****************************************************************************/
3354 static HRESULT WINAPI
3355 IDirect3DDeviceImpl_7_DrawPrimitiveVB(IDirect3DDevice7 *iface,
3356                                       D3DPRIMITIVETYPE PrimitiveType,
3357                                       IDirect3DVertexBuffer7 *D3DVertexBuf,
3358                                       DWORD StartVertex,
3359                                       DWORD NumVertices,
3360                                       DWORD Flags)
3361 {
3362     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3363     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3364     UINT PrimitiveCount;
3365     HRESULT hr;
3366     DWORD stride;
3367     WINED3DVERTEXBUFFER_DESC Desc;
3368
3369     TRACE("(%p)->(%08x,%p,%08x,%08x,%08x)\n", This, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
3370
3371     /* Sanity checks */
3372     if(!vb)
3373     {
3374         ERR("(%p) No Vertex buffer specified\n", This);
3375         return DDERR_INVALIDPARAMS;
3376     }
3377
3378     /* Get the primitive count */
3379     switch(PrimitiveType)
3380     {
3381         case D3DPT_POINTLIST: 
3382           PrimitiveCount = NumVertices;
3383           break;
3384
3385         case D3DPT_LINELIST: 
3386           PrimitiveCount = NumVertices / 2;
3387           break;
3388
3389         case D3DPT_LINESTRIP:
3390           PrimitiveCount = NumVertices - 1;
3391           break;
3392
3393         case D3DPT_TRIANGLELIST:
3394           PrimitiveCount = NumVertices / 3;
3395           break;
3396
3397         case D3DPT_TRIANGLESTRIP:
3398           PrimitiveCount = NumVertices - 2;
3399           break;
3400
3401         case D3DPT_TRIANGLEFAN:
3402           PrimitiveCount = NumVertices - 2;
3403           break;
3404
3405         default: return DDERR_INVALIDPARAMS;
3406     }
3407
3408     /* Get the FVF of the vertex buffer, and its stride */
3409     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3410                                       &Desc);
3411     if(hr != D3D_OK)
3412     {
3413         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3414         return hr;
3415     }
3416     stride = get_flexible_vertex_size(Desc.FVF);
3417
3418     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, Desc.FVF);
3419     if(FAILED(hr))
3420     {
3421         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3422         return hr;
3423     }
3424
3425     /* Set the vertex stream source */
3426     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3427                                         0 /* StreamNumber */,
3428                                         vb->wineD3DVertexBuffer,
3429                                         0 /* StartVertex - we pass this to DrawPrimitive */,
3430                                         stride);
3431     if(hr != D3D_OK)
3432     {
3433         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3434         return hr;
3435     }
3436
3437     /* Now draw the primitives */
3438     return IWineD3DDevice_DrawPrimitive(This->wineD3DDevice,
3439                                         PrimitiveType,
3440                                         StartVertex,
3441                                         PrimitiveCount);
3442 }
3443
3444 static HRESULT WINAPI
3445 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB(IDirect3DDevice3 *iface,
3446                                             D3DPRIMITIVETYPE PrimitiveType,
3447                                             IDirect3DVertexBuffer *D3DVertexBuf,
3448                                             DWORD StartVertex,
3449                                             DWORD NumVertices,
3450                                             DWORD Flags)
3451 {
3452     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3453     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3454     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This,  PrimitiveType, vb, StartVertex, NumVertices, Flags);
3455     return IDirect3DDevice7_DrawPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3456                                             PrimitiveType,
3457                                             ICOM_INTERFACE(vb, IDirect3DVertexBuffer7),
3458                                             StartVertex,
3459                                             NumVertices,
3460                                             Flags);
3461 }
3462
3463
3464 /*****************************************************************************
3465  * IDirect3DDevice7::DrawIndexedPrimitiveVB
3466  *
3467  * Draws primitives from a vertex buffer to the screen
3468  *
3469  * Params:
3470  *  PrimitiveType: Type of primitive to be rendered.
3471  *  D3DVertexBuf: Source Vertex Buffer
3472  *  StartVertex: Index of the first vertex from the buffer to be rendered
3473  *  NumVertices: Number of vertices to be rendered
3474  *  Indices: Array of DWORDs used to index into the Vertices
3475  *  IndexCount: Number of indices in Indices
3476  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3477  *
3478  * Return values
3479  *
3480  *****************************************************************************/
3481 static HRESULT WINAPI
3482 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
3483                                              D3DPRIMITIVETYPE PrimitiveType,
3484                                              IDirect3DVertexBuffer7 *D3DVertexBuf,
3485                                              DWORD StartVertex,
3486                                              DWORD NumVertices,
3487                                              WORD *Indices,
3488                                              DWORD IndexCount,
3489                                              DWORD Flags)
3490 {
3491     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3492     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3493     DWORD stride;
3494     UINT PrimitiveCount;
3495     WORD *LockedIndices;
3496     HRESULT hr;
3497     WINED3DVERTEXBUFFER_DESC Desc;
3498
3499     TRACE("(%p)->(%08x,%p,%d,%d,%p,%d,%08x)\n", This, PrimitiveType, vb, StartVertex, NumVertices, Indices, IndexCount, Flags);
3500
3501     /* Steps:
3502      * 1) Calculate some things: Vertex count -> Primitive count, stride, ...
3503      * 2) Upload the Indices to the index buffer
3504      * 3) Set the index source
3505      * 4) Set the Vertex Buffer as the Stream source
3506      * 5) Call IWineD3DDevice::DrawIndexedPrimitive
3507      */
3508
3509     /* Get the primitive count */
3510     switch(PrimitiveType)
3511     {
3512         case D3DPT_POINTLIST: 
3513           PrimitiveCount = IndexCount;
3514           break;
3515
3516         case D3DPT_LINELIST: 
3517           PrimitiveCount = IndexCount / 2;
3518           break;
3519
3520         case D3DPT_LINESTRIP:
3521           PrimitiveCount = IndexCount - 1;
3522           break;
3523
3524         case D3DPT_TRIANGLELIST:
3525           PrimitiveCount = IndexCount / 3;
3526           break;
3527
3528         case D3DPT_TRIANGLESTRIP:
3529           PrimitiveCount = IndexCount - 2;
3530           break;
3531
3532         case D3DPT_TRIANGLEFAN:
3533           PrimitiveCount = IndexCount - 2;
3534           break;
3535
3536         default: return DDERR_INVALIDPARAMS;
3537     }
3538
3539     /* Get the FVF of the vertex buffer, and its stride */
3540     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3541                                       &Desc);
3542     if(hr != D3D_OK)
3543     {
3544         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08x\n", This, hr);
3545         return hr;
3546     }
3547     stride = get_flexible_vertex_size(Desc.FVF);
3548     TRACE("Vertex buffer FVF = %08x, stride=%d\n", Desc.FVF, stride);
3549
3550     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, Desc.FVF);
3551     if(FAILED(hr))
3552     {
3553         ERR(" (%p) Setting the FVF failed, hr = %x!\n", This, hr);
3554         return hr;
3555     }
3556
3557     /* copy the index stream into the index buffer.
3558      * A new IWineD3DDevice method could be created
3559      * which takes an user pointer containing the indices
3560      * or a SetData-Method for the index buffer, which
3561      * overrides the index buffer data with our pointer.
3562      */
3563     hr = IWineD3DIndexBuffer_Lock(This->indexbuffer,
3564                                   0 /* OffSetToLock */,
3565                                   0 /* SizeToLock - doesn't matter */,
3566                                   (BYTE **) &LockedIndices,
3567                                   0 /* Flags */);
3568     assert(IndexCount < 0x100000);
3569     if(hr != D3D_OK)
3570     {
3571         ERR("(%p) IWineD3DIndexBuffer::Lock failed with hr = %08x\n", This, hr);
3572         return hr;
3573     }
3574     memcpy(LockedIndices, Indices, IndexCount * sizeof(WORD));
3575     hr = IWineD3DIndexBuffer_Unlock(This->indexbuffer);
3576     if(hr != D3D_OK)
3577     {
3578         ERR("(%p) IWineD3DIndexBuffer::Unlock failed with hr = %08x\n", This, hr);
3579         return hr;
3580     }
3581
3582     /* Set the index stream */
3583     hr = IWineD3DDevice_SetIndices(This->wineD3DDevice,
3584                                    This->indexbuffer,
3585                                    0);
3586
3587     /* Set the vertex stream source */
3588     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3589                                         0 /* StreamNumber */,
3590                                         vb->wineD3DVertexBuffer,
3591                                         0 /* offset, we pass this to DrawIndexedPrimitive */,
3592                                         stride);
3593     if(hr != D3D_OK)
3594     {
3595         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08x\n", This, hr);
3596         return hr;
3597     }
3598
3599
3600     hr = IWineD3DDevice_DrawIndexedPrimitive(This->wineD3DDevice,
3601                                              PrimitiveType,
3602                                              StartVertex,
3603                                              0 /* minIndex */,
3604                                              NumVertices,
3605                                              0 /* StartIndex */,
3606                                              PrimitiveCount);
3607
3608     return D3D_OK;
3609 }
3610
3611 static HRESULT WINAPI
3612 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
3613                                                    D3DPRIMITIVETYPE PrimitiveType,
3614                                                    IDirect3DVertexBuffer *D3DVertexBuf,
3615                                                    WORD *Indices,
3616                                                    DWORD IndexCount,
3617                                                    DWORD Flags)
3618 {
3619     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3620     IDirect3DVertexBufferImpl *VB = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3621     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VB, Indices, IndexCount, Flags);
3622
3623     return IDirect3DDevice7_DrawIndexedPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3624                                                    PrimitiveType,
3625                                                    ICOM_INTERFACE(VB, IDirect3DVertexBuffer7),
3626                                                    0,
3627                                                    IndexCount,
3628                                                    Indices,
3629                                                    IndexCount,
3630                                                    Flags);
3631 }
3632
3633 /*****************************************************************************
3634  * IDirect3DDevice7::ComputeSphereVisibility
3635  *
3636  * Calculates the visibility of spheres in the current viewport. The spheres
3637  * are passed in the Centers and Radii arrays, the results are passed back
3638  * in the ReturnValues array. Return values are either completely visible,
3639  * partially visible or completely invisible.
3640  * The return value consist of a combination of D3DCLIP_* flags, or it's
3641  * 0 if the sphere is completely visible(according to the SDK, not checked)
3642  *
3643  * Sounds like an overdose of math ;)
3644  *
3645  * Version 3 and 7
3646  *
3647  * Params:
3648  *  Centers: Array containing the sphere centers
3649  *  Radii: Array containing the sphere radii
3650  *  NumSpheres: The number of centers and radii in the arrays
3651  *  Flags: Some flags
3652  *  ReturnValues: Array to write the results to
3653  *
3654  * Returns:
3655  *  D3D_OK because it's a stub
3656  *  (DDERR_INVALIDPARAMS if Centers, Radii or ReturnValues are NULL)
3657  *  (D3DERR_INVALIDMATRIX if the combined world, view and proj matrix
3658  *  is singular)
3659  *
3660  *****************************************************************************/
3661 static HRESULT WINAPI
3662 IDirect3DDeviceImpl_7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
3663                                               D3DVECTOR *Centers,
3664                                               D3DVALUE *Radii,
3665                                               DWORD NumSpheres,
3666                                               DWORD Flags,
3667                                               DWORD *ReturnValues)
3668 {
3669     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3670     FIXME("(%p)->(%p,%p,%08x,%08x,%p): stub!\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3671
3672     /* the DirectX 7 sdk says that the visibility is computed by
3673      * back-transforming the viewing frustum to model space
3674      * using the inverse of the combined world, view and projection
3675      * matrix. If the matrix can't be reversed, D3DERR_INVALIDMATRIX
3676      * is returned.
3677      *
3678      * Basic implementation idea:
3679      * 1) Check if the center is in the viewing frustum
3680      * 2) Cut the sphere with the planes of the viewing
3681      *    frustum
3682      *
3683      * ->Center inside the frustum, no intersections:
3684      *    Fully visible
3685      * ->Center outside the frustum, no intersections:
3686      *    Not visible
3687      * ->Some intersections: Partially visible
3688      *
3689      * Implement this call in WineD3D. Either implement the
3690      * matrix and vector stuff in WineD3D, or use some external
3691      * math library.
3692      */
3693
3694     return D3D_OK;
3695 }
3696
3697 static HRESULT WINAPI
3698 Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
3699                                                     D3DVECTOR *Centers,
3700                                                     D3DVALUE *Radii,
3701                                                     DWORD NumSpheres,
3702                                                     DWORD Flags,
3703                                                     DWORD *ReturnValues)
3704 {
3705     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3706     TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3707     return IDirect3DDevice7_ComputeSphereVisibility(ICOM_INTERFACE(This, IDirect3DDevice7),
3708                                                     Centers,
3709                                                     Radii,
3710                                                     NumSpheres,
3711                                                     Flags,
3712                                                     ReturnValues);
3713 }
3714
3715 /*****************************************************************************
3716  * IDirect3DDevice7::GetTexture
3717  *
3718  * Returns the texture interface handle assigned to a texture stage.
3719  * The returned texture is AddRefed. This is taken from old ddraw,
3720  * not checked in Windows.
3721  *
3722  * Version 3 and 7
3723  *
3724  * Params:
3725  *  Stage: Texture stage to read the texture from
3726  *  Texture: Address to store the interface pointer at
3727  *
3728  * Returns:
3729  *  D3D_OK on success
3730  *  DDERR_INVALIDPARAMS if Texture is NULL
3731  *  For details, see IWineD3DDevice::GetTexture
3732  *
3733  *****************************************************************************/
3734 static HRESULT WINAPI
3735 IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
3736                                  DWORD Stage,
3737                                  IDirectDrawSurface7 **Texture)
3738 {
3739     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3740     IWineD3DBaseTexture *Surf;
3741     HRESULT hr;
3742     TRACE("(%p)->(%d,%p): Relay\n", This, Stage, Texture);
3743
3744     if(!Texture)
3745     {
3746         TRACE("Texture == NULL, failing with DDERR_INVALIDPARAMS\n");
3747         return DDERR_INVALIDPARAMS;
3748     }
3749
3750     hr = IWineD3DDevice_GetTexture(This->wineD3DDevice, Stage, (IWineD3DBaseTexture **) &Surf);
3751     if( (hr != D3D_OK) || (!Surf) ) 
3752     {
3753         *Texture = NULL;
3754         return hr;
3755     }
3756
3757     /* GetParent AddRef()s, which is perfectly OK.
3758      * We have passed the IDirectDrawSurface7 interface to WineD3D, so that's OK too.
3759      */
3760     return IWineD3DBaseTexture_GetParent(Surf,
3761                                          (IUnknown **) Texture);
3762 }
3763
3764 static HRESULT WINAPI
3765 Thunk_IDirect3DDeviceImpl_3_GetTexture(IDirect3DDevice3 *iface,
3766                                        DWORD Stage,
3767                                        IDirect3DTexture2 **Texture2)
3768 {
3769     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3770     HRESULT ret;
3771     IDirectDrawSurface7 *ret_val;
3772
3773     TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, Texture2);
3774     ret = IDirect3DDevice7_GetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3775                                       Stage,
3776                                       &ret_val);
3777
3778     *Texture2 = COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirect3DTexture2, ret_val);
3779
3780     TRACE_(ddraw_thunk)(" returning interface %p.\n", *Texture2);
3781
3782     return ret;
3783 }
3784
3785 /*****************************************************************************
3786  * IDirect3DDevice7::SetTexture
3787  *
3788  * Assigns a texture to a texture stage. Is the texture AddRef-ed?
3789  *
3790  * Version 3 and 7
3791  *
3792  * Params:
3793  *  Stage: The stage to assign the texture to
3794  *  Texture: Interface pointer to the texture surface
3795  *
3796  * Returns
3797  * D3D_OK on success
3798  * For details, see IWineD3DDevice::SetTexture
3799  *
3800  *****************************************************************************/
3801 static HRESULT WINAPI
3802 IDirect3DDeviceImpl_7_SetTexture(IDirect3DDevice7 *iface,
3803                                  DWORD Stage,
3804                                  IDirectDrawSurface7 *Texture)
3805 {
3806     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3807     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
3808     TRACE("(%p)->(%08x,%p): Relay!\n", This, Stage, surf);
3809
3810     /* Texture may be NULL here */
3811     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
3812                                      Stage,
3813                                      surf ? (IWineD3DBaseTexture * ) surf->wineD3DTexture : NULL);
3814 }
3815
3816 static HRESULT WINAPI
3817 Thunk_IDirect3DDeviceImpl_3_SetTexture(IDirect3DDevice3 *iface,
3818                                        DWORD Stage,
3819                                        IDirect3DTexture2 *Texture2)
3820 {
3821     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3822     IDirectDrawSurfaceImpl *tex = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Texture2);
3823     TRACE_(ddraw_thunk)("(%p)->(%d,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, tex);
3824     return IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3825                                        Stage,
3826                                        ICOM_INTERFACE(tex, IDirectDrawSurface7));
3827 }
3828
3829 /*****************************************************************************
3830  * IDirect3DDevice7::GetTextureStageState
3831  *
3832  * Retrieves a state from a texture stage.
3833  *
3834  * Version 3 and 7
3835  *
3836  * Params:
3837  *  Stage: The stage to retrieve the state from
3838  *  TexStageStateType: The state type to retrieve
3839  *  State: Address to store the state's value at
3840  *
3841  * Returns:
3842  *  D3D_OK on success
3843  *  DDERR_INVALIDPARAMS if State is NULL
3844  *  For details, see IWineD3DDevice::GetTextureStageState
3845  *
3846  *****************************************************************************/
3847 static HRESULT WINAPI
3848 IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
3849                                            DWORD Stage,
3850                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
3851                                            DWORD *State)
3852 {
3853     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3854     TRACE("(%p)->(%08x,%08x,%p): Relay!\n", This, Stage, TexStageStateType, State);
3855
3856     if(!State)
3857         return DDERR_INVALIDPARAMS;
3858
3859     return IWineD3DDevice_GetTextureStageState(This->wineD3DDevice,
3860                                                Stage,
3861                                                TexStageStateType,
3862                                                State);
3863 }
3864
3865 static HRESULT WINAPI
3866 Thunk_IDirect3DDeviceImpl_3_GetTextureStageState(IDirect3DDevice3 *iface,
3867                                                  DWORD Stage,
3868                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
3869                                                  DWORD *State)
3870 {
3871     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3872     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
3873     return IDirect3DDevice7_GetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
3874                                                  Stage,
3875                                                  TexStageStateType,
3876                                                  State);
3877 }
3878
3879 /*****************************************************************************
3880  * IDirect3DDevice7::SetTextureStageState
3881  *
3882  * Sets a texture stage state. Some stage types need to be handled specially,
3883  * because they do not exist in WineD3D and were moved to another place
3884  *
3885  * Version 3 and 7
3886  *
3887  * Params:
3888  *  Stage: The stage to modify
3889  *  TexStageStateType: The state to change
3890  *  State: The new value for the state
3891  *
3892  * Returns:
3893  *  D3D_OK on success
3894  *  For details, see IWineD3DDevice::SetTextureStageState
3895  *
3896  *****************************************************************************/
3897 static HRESULT WINAPI
3898 IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
3899                                            DWORD Stage,
3900                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
3901                                            DWORD State)
3902 {
3903     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3904     TRACE("(%p)->(%08x,%08x,%08x): Relay!\n", This, Stage, TexStageStateType, State);
3905     switch(TexStageStateType)
3906     {
3907         /* Mipfilter is a sampler state with different values */
3908         case D3DTSS_MIPFILTER:
3909         {
3910             WINED3DTEXTUREFILTERTYPE value;
3911             switch(State)
3912             {
3913                 case D3DTFP_NONE: value = WINED3DTEXF_NONE; break;
3914                 case D3DTFP_POINT: value = WINED3DTEXF_POINT; break;
3915                 case 0: /* Unchecked */
3916                 case D3DTFP_LINEAR: value = WINED3DTEXF_LINEAR; break;
3917                 default:
3918                     ERR("Unexpected mipfilter value %d\n", State);
3919                     value = WINED3DTEXF_NONE;
3920             }
3921             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
3922                                                   Stage,
3923                                                   WINED3DSAMP_MIPFILTER,
3924                                                   value);
3925         }
3926
3927         /* Minfilter is a sampler state too, equal values */
3928         case D3DTSS_MINFILTER:
3929             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
3930                                                   Stage,
3931                                                   WINED3DSAMP_MINFILTER,
3932                                                   State);
3933         /* Same for MAGFILTER */
3934         case D3DTSS_MAGFILTER:
3935             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
3936                                                   Stage,
3937                                                   WINED3DSAMP_MAGFILTER,
3938                                                   State);
3939
3940         default:
3941
3942             return IWineD3DDevice_SetTextureStageState(This->wineD3DDevice,
3943                                                       Stage,
3944                                                       TexStageStateType,
3945                                                       State);
3946     }
3947 }
3948
3949 static HRESULT WINAPI
3950 Thunk_IDirect3DDeviceImpl_3_SetTextureStageState(IDirect3DDevice3 *iface,
3951                                                  DWORD Stage,
3952                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
3953                                                  DWORD State)
3954 {
3955     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3956     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%08x) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
3957     return IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
3958                                                  Stage,
3959                                                  TexStageStateType,
3960                                                  State);
3961 }
3962
3963 /*****************************************************************************
3964  * IDirect3DDevice7::ValidateDevice
3965  *
3966  * SDK: "Reports the device's ability to render the currently set
3967  * texture-blending operations in a single pass". Whatever that means
3968  * exactly...
3969  *
3970  * Version 3 and 7
3971  *
3972  * Params:
3973  *  NumPasses: Address to write the number of necessary passes for the
3974  *             desired effect to.
3975  *
3976  * Returns:
3977  *  D3D_OK on success
3978  *  See IWineD3DDevice::ValidateDevice for more details
3979  *
3980  *****************************************************************************/
3981 static HRESULT WINAPI
3982 IDirect3DDeviceImpl_7_ValidateDevice(IDirect3DDevice7 *iface,
3983                                      DWORD *NumPasses)
3984 {
3985     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3986     TRACE("(%p)->(%p): Relay\n", This, NumPasses);
3987
3988     return IWineD3DDevice_ValidateDevice(This->wineD3DDevice, NumPasses);
3989 }
3990
3991 static HRESULT WINAPI
3992 Thunk_IDirect3DDeviceImpl_3_ValidateDevice(IDirect3DDevice3 *iface,
3993                                            DWORD *Passes)
3994 {
3995     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3996     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Passes);
3997     return IDirect3DDevice7_ValidateDevice(ICOM_INTERFACE(This, IDirect3DDevice7),
3998                                            Passes);
3999 }
4000
4001 /*****************************************************************************
4002  * IDirect3DDevice7::Clear
4003  *
4004  * Fills the render target, the z buffer and the stencil buffer with a
4005  * clear color / value
4006  *
4007  * Version 7 only
4008  *
4009  * Params:
4010  *  Count: Number of rectangles in Rects must be 0 if Rects is NULL
4011  *  Rects: Rectangles to clear. If NULL, the whole surface is cleared
4012  *  Flags: Some flags, as usual
4013  *  Color: Clear color for the render target
4014  *  Z: Clear value for the Z buffer
4015  *  Stencil: Clear value to store in each stencil buffer entry
4016  *
4017  * Returns:
4018  *  D3D_OK on success
4019  *  For details, see IWineD3DDevice::Clear
4020  *
4021  *****************************************************************************/
4022 static HRESULT WINAPI
4023 IDirect3DDeviceImpl_7_Clear(IDirect3DDevice7 *iface,
4024                             DWORD Count,
4025                             D3DRECT *Rects,
4026                             DWORD Flags,
4027                             D3DCOLOR Color,
4028                             D3DVALUE Z,
4029                             DWORD Stencil)
4030 {
4031     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4032     TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x): Relay\n", This, Count, Rects, Flags, (DWORD) Color, Z, Stencil);
4033
4034     /* Note; D3DRECT is compatible with WINED3DRECT */
4035     return IWineD3DDevice_Clear(This->wineD3DDevice, Count, (WINED3DRECT*) Rects, Flags, Color, Z, Stencil);
4036 }
4037
4038 /*****************************************************************************
4039  * IDirect3DDevice7::SetViewport
4040  *
4041  * Sets the current viewport.
4042  *
4043  * Version 7 only, but IDirect3DViewport uses this call for older
4044  * versions
4045  *
4046  * Params:
4047  *  Data: The new viewport to set
4048  *
4049  * Returns:
4050  *  D3D_OK on success
4051  *  DDERR_INVALIDPARAMS if Data is NULL
4052  *  For more details, see IWineDDDevice::SetViewport
4053  *
4054  *****************************************************************************/
4055 static HRESULT WINAPI
4056 IDirect3DDeviceImpl_7_SetViewport(IDirect3DDevice7 *iface,
4057                                   D3DVIEWPORT7 *Data)
4058 {
4059     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4060     TRACE("(%p)->(%p) Relay!\n", This, Data);
4061
4062     if(!Data)
4063         return DDERR_INVALIDPARAMS;
4064
4065     /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4066     return IWineD3DDevice_SetViewport(This->wineD3DDevice,
4067                                       (WINED3DVIEWPORT*) Data);
4068 }
4069
4070 /*****************************************************************************
4071  * IDirect3DDevice::GetViewport
4072  *
4073  * Returns the current viewport
4074  *
4075  * Version 7
4076  *
4077  * Params:
4078  *  Data: D3D7Viewport structure to write the viewport information to
4079  *
4080  * Returns:
4081  *  D3D_OK on success
4082  *  DDERR_INVALIDPARAMS if Data is NULL
4083  *  For more details, see IWineD3DDevice::GetViewport
4084  *
4085  *****************************************************************************/
4086 static HRESULT WINAPI
4087 IDirect3DDeviceImpl_7_GetViewport(IDirect3DDevice7 *iface,
4088                                   D3DVIEWPORT7 *Data)
4089 {
4090     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4091     HRESULT hr;
4092     TRACE("(%p)->(%p) Relay!\n", This, Data);
4093
4094     if(!Data)
4095         return DDERR_INVALIDPARAMS;
4096
4097     /* Note: D3DVIEWPORT7 is compatible with WINED3DVIEWPORT */
4098     hr = IWineD3DDevice_GetViewport(This->wineD3DDevice,
4099                                     (WINED3DVIEWPORT*) Data);
4100
4101     return hr_ddraw_from_wined3d(hr);
4102 }
4103
4104 /*****************************************************************************
4105  * IDirect3DDevice7::SetMaterial
4106  *
4107  * Sets the Material
4108  *
4109  * Version 7
4110  *
4111  * Params:
4112  *  Mat: The material to set
4113  *
4114  * Returns:
4115  *  D3D_OK on success
4116  *  DDERR_INVALIDPARAMS if Mat is NULL.
4117  *  For more details, see IWineD3DDevice::SetMaterial
4118  *
4119  *****************************************************************************/
4120 static HRESULT WINAPI
4121 IDirect3DDeviceImpl_7_SetMaterial(IDirect3DDevice7 *iface,
4122                                   D3DMATERIAL7 *Mat)
4123 {
4124     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4125     HRESULT hr;
4126     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4127
4128     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */
4129     hr = IWineD3DDevice_SetMaterial(This->wineD3DDevice,
4130                                     (WINED3DMATERIAL*) Mat);
4131
4132     return hr_ddraw_from_wined3d(hr);
4133 }
4134
4135 /*****************************************************************************
4136  * IDirect3DDevice7::GetMaterial
4137  *
4138  * Returns the current material
4139  *
4140  * Version 7
4141  *
4142  * Params:
4143  *  Mat: D3DMATERIAL7 structure to write the material parameters to
4144  *
4145  * Returns:
4146  *  D3D_OK on success
4147  *  DDERR_INVALIDPARAMS if Mat is NULL
4148  *  For more details, see IWineD3DDevice::GetMaterial
4149  *
4150  *****************************************************************************/
4151 static HRESULT WINAPI
4152 IDirect3DDeviceImpl_7_GetMaterial(IDirect3DDevice7 *iface,
4153                                   D3DMATERIAL7 *Mat)
4154 {
4155     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4156     HRESULT hr;
4157     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4158
4159     /* Note: D3DMATERIAL7 is compatible with WINED3DMATERIAL */ 
4160     hr = IWineD3DDevice_GetMaterial(This->wineD3DDevice,
4161                                     (WINED3DMATERIAL*) Mat);
4162
4163     return hr_ddraw_from_wined3d(hr);
4164 }
4165
4166 /*****************************************************************************
4167  * IDirect3DDevice7::SetLight
4168  *
4169  * Assigns a light to a light index, but doesn't activate it yet.
4170  *
4171  * Version 7, IDirect3DLight uses this method for older versions
4172  *
4173  * Params:
4174  *  LightIndex: The index of the new light
4175  *  Light: A D3DLIGHT7 structure describing the light
4176  *
4177  * Returns:
4178  *  D3D_OK on success
4179  *  For more details, see IWineD3DDevice::SetLight
4180  *
4181  *****************************************************************************/
4182 static HRESULT WINAPI
4183 IDirect3DDeviceImpl_7_SetLight(IDirect3DDevice7 *iface,
4184                                DWORD LightIndex,
4185                                D3DLIGHT7 *Light)
4186 {
4187     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4188     HRESULT hr;
4189     TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4190
4191     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4192     hr = IWineD3DDevice_SetLight(This->wineD3DDevice,
4193                                  LightIndex,
4194                                  (WINED3DLIGHT*) Light);
4195
4196     return hr_ddraw_from_wined3d(hr);
4197 }
4198
4199 /*****************************************************************************
4200  * IDirect3DDevice7::GetLight
4201  *
4202  * Returns the light assigned to a light index
4203  *
4204  * Params:
4205  *  Light: Structure to write the light information to
4206  *
4207  * Returns:
4208  *  D3D_OK on success
4209  *  DDERR_INVALIDPARAMS if Light is NULL
4210  *  For details, see IWineD3DDevice::GetLight
4211  *
4212  *****************************************************************************/
4213 static HRESULT WINAPI
4214 IDirect3DDeviceImpl_7_GetLight(IDirect3DDevice7 *iface,
4215                                DWORD LightIndex,
4216                                D3DLIGHT7 *Light)
4217 {
4218     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4219     HRESULT rc;
4220     TRACE("(%p)->(%08x,%p): Relay!\n", This, LightIndex, Light);
4221
4222     /* Note: D3DLIGHT7 is compatible with WINED3DLIGHT */
4223     rc =  IWineD3DDevice_GetLight(This->wineD3DDevice,
4224                                   LightIndex,
4225                                   (WINED3DLIGHT*) Light);
4226
4227     /* Translate the result. WineD3D returns other values than D3D7 */
4228     return hr_ddraw_from_wined3d(rc);
4229 }
4230
4231 /*****************************************************************************
4232  * IDirect3DDevice7::BeginStateBlock
4233  *
4234  * Begins recording to a stateblock
4235  *
4236  * Version 7
4237  *
4238  * Returns:
4239  *  D3D_OK on success
4240  *  For details see IWineD3DDevice::BeginStateBlock
4241  *
4242  *****************************************************************************/
4243 static HRESULT WINAPI
4244 IDirect3DDeviceImpl_7_BeginStateBlock(IDirect3DDevice7 *iface)
4245 {
4246     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4247     HRESULT hr;
4248     TRACE("(%p)->(): Relay!\n", This);
4249
4250     hr = IWineD3DDevice_BeginStateBlock(This->wineD3DDevice);
4251     return hr_ddraw_from_wined3d(hr);
4252 }
4253
4254 /*****************************************************************************
4255  * IDirect3DDevice7::EndStateBlock
4256  *
4257  * Stops recording to a state block and returns the created stateblock
4258  * handle. The d3d7 stateblock handles are the interface pointers of the
4259  * IWineD3DStateBlock interface
4260  *
4261  * Version 7
4262  *
4263  * Params:
4264  *  BlockHandle: Address to store the stateblock's handle to
4265  *
4266  * Returns:
4267  *  D3D_OK on success
4268  *  DDERR_INVALIDPARAMS if BlockHandle is NULL
4269  *  See IWineD3DDevice::EndStateBlock for more details
4270  *
4271  *****************************************************************************/
4272 static HRESULT WINAPI
4273 IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
4274                                     DWORD *BlockHandle)
4275 {
4276     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4277     HRESULT hr;
4278     TRACE("(%p)->(%p): Relay!\n", This, BlockHandle);
4279
4280     if(!BlockHandle)
4281         return DDERR_INVALIDPARAMS;
4282
4283     hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice,
4284                                       (IWineD3DStateBlock **) BlockHandle);
4285     return hr_ddraw_from_wined3d(hr);
4286 }
4287
4288 /*****************************************************************************
4289  * IDirect3DDevice7::PreLoad
4290  *
4291  * Allows the app to signal that a texture will be used soon, to allow
4292  * the Direct3DDevice to load it to the video card in the meantime.
4293  *
4294  * Version 7
4295  *
4296  * Params:
4297  *  Texture: The texture to preload
4298  *
4299  * Returns:
4300  *  D3D_OK on success
4301  *  DDERR_INVALIDPARAMS if Texture is NULL
4302  *  See IWineD3DSurface::PreLoad for details
4303  *
4304  *****************************************************************************/
4305 static HRESULT WINAPI
4306 IDirect3DDeviceImpl_7_PreLoad(IDirect3DDevice7 *iface,
4307                               IDirectDrawSurface7 *Texture)
4308 {
4309     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4310     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
4311
4312     TRACE("(%p)->(%p): Relay!\n", This, surf);
4313
4314     if(!Texture)
4315         return DDERR_INVALIDPARAMS;
4316
4317     IWineD3DSurface_PreLoad(surf->WineD3DSurface);
4318     return D3D_OK;
4319 }
4320
4321 /*****************************************************************************
4322  * IDirect3DDevice7::ApplyStateBlock
4323  *
4324  * Activates the state stored in a state block handle.
4325  *
4326  * Params:
4327  *  BlockHandle: The stateblock handle to activate
4328  *
4329  * Returns:
4330  *  D3D_OK on success
4331  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4332  *
4333  *****************************************************************************/
4334 static HRESULT WINAPI
4335 IDirect3DDeviceImpl_7_ApplyStateBlock(IDirect3DDevice7 *iface,
4336                                       DWORD BlockHandle)
4337 {
4338     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4339     HRESULT hr;
4340     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4341
4342     if(!BlockHandle)
4343         return D3DERR_INVALIDSTATEBLOCK;
4344
4345     hr = IWineD3DStateBlock_Apply((IWineD3DStateBlock *) BlockHandle);
4346     return hr_ddraw_from_wined3d(hr);
4347 }
4348
4349 /*****************************************************************************
4350  * IDirect3DDevice7::CaptureStateBlock
4351  *
4352  * Updates a stateblock's values to the values currently set for the device
4353  *
4354  * Version 7
4355  *
4356  * Params:
4357  *  BlockHandle: Stateblock to update
4358  *
4359  * Returns:
4360  *  D3D_OK on success
4361  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4362  *  See IWineD3DDevice::CaptureStateBlock for more details
4363  *
4364  *****************************************************************************/
4365 static HRESULT WINAPI
4366 IDirect3DDeviceImpl_7_CaptureStateBlock(IDirect3DDevice7 *iface,
4367                                         DWORD BlockHandle)
4368 {
4369     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4370     HRESULT hr;
4371     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4372
4373     if(BlockHandle == 0)
4374         return D3DERR_INVALIDSTATEBLOCK;
4375
4376     hr = IWineD3DStateBlock_Capture((IWineD3DStateBlock *) BlockHandle);
4377     return hr_ddraw_from_wined3d(hr);
4378 }
4379
4380 /*****************************************************************************
4381  * IDirect3DDevice7::DeleteStateBlock
4382  *
4383  * Deletes a stateblock handle. This means releasing the WineD3DStateBlock
4384  *
4385  * Version 7
4386  *
4387  * Params:
4388  *  BlockHandle: Stateblock handle to delete
4389  *
4390  * Returns:
4391  *  D3D_OK on success
4392  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is 0
4393  *
4394  *****************************************************************************/
4395 static HRESULT WINAPI
4396 IDirect3DDeviceImpl_7_DeleteStateBlock(IDirect3DDevice7 *iface,
4397                                        DWORD BlockHandle)
4398 {
4399     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4400     TRACE("(%p)->(%08x): Relay!\n", This, BlockHandle);
4401
4402     if(BlockHandle == 0)
4403         return D3DERR_INVALIDSTATEBLOCK;
4404
4405     IWineD3DStateBlock_Release((IWineD3DStateBlock *) BlockHandle);
4406
4407     return D3D_OK;
4408 }
4409
4410 /*****************************************************************************
4411  * IDirect3DDevice7::CreateStateBlock
4412  *
4413  * Creates a new state block handle.
4414  *
4415  * Version 7
4416  *
4417  * Params:
4418  *  Type: The state block type
4419  *  BlockHandle: Address to write the created handle to
4420  *
4421  * Returns:
4422  *   D3D_OK on success
4423  *   DDERR_INVALIDPARAMS if BlockHandle is NULL
4424  *
4425  *****************************************************************************/
4426 static HRESULT WINAPI
4427 IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
4428                                        D3DSTATEBLOCKTYPE Type,
4429                                        DWORD *BlockHandle)
4430 {
4431     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4432     HRESULT hr;
4433     TRACE("(%p)->(%08x,%p)!\n", This, Type, BlockHandle);
4434
4435     if(!BlockHandle)
4436         return DDERR_INVALIDPARAMS;
4437
4438     /* The D3DSTATEBLOCKTYPE enum is fine here */
4439     hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice,
4440                                          Type,
4441                                          (IWineD3DStateBlock **) BlockHandle,
4442                                          NULL /* Parent, hope that works */);
4443     return hr_ddraw_from_wined3d(hr);
4444 }
4445
4446 /*****************************************************************************
4447  * IDirect3DDevice7::Load
4448  *
4449  * Loads a rectangular area from the source into the destination texture.
4450  * It can also copy the source to the faces of a cubic environment map
4451  *
4452  * Version 7
4453  *
4454  * Params:
4455  *  DestTex: Destination texture
4456  *  DestPoint: Point in the destination where the source image should be
4457  *             written to
4458  *  SrcTex: Source texture
4459  *  SrcRect: Source rectangle
4460  *  Flags: Some flags
4461  *
4462  * Returns:
4463  *  D3D_OK on success
4464  *  DDERR_INVALIDPARAMS if DestTex or SrcTex are NULL
4465  *  See IDirect3DTexture2::Load for details
4466  *
4467  *****************************************************************************/
4468 static HRESULT WINAPI
4469 IDirect3DDeviceImpl_7_Load(IDirect3DDevice7 *iface,
4470                            IDirectDrawSurface7 *DestTex,
4471                            POINT *DestPoint,
4472                            IDirectDrawSurface7 *SrcTex,
4473                            RECT *SrcRect,
4474                            DWORD Flags)
4475 {
4476     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4477     IDirectDrawSurfaceImpl *dest = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestTex);
4478     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcTex);
4479     FIXME("(%p)->(%p,%p,%p,%p,%08x): Partially Implemented!\n", This, dest, DestPoint, src, SrcRect, Flags);
4480
4481     if( (!src) || (!dest) )
4482         return DDERR_INVALIDPARAMS;
4483
4484     IDirect3DTexture2_Load(ICOM_INTERFACE(dest, IDirect3DTexture2),
4485                            ICOM_INTERFACE(src, IDirect3DTexture2));
4486     return D3D_OK;
4487 }
4488
4489 /*****************************************************************************
4490  * IDirect3DDevice7::LightEnable
4491  *
4492  * Enables or disables a light
4493  *
4494  * Version 7, IDirect3DLight uses this method too.
4495  *
4496  * Params:
4497  *  LightIndex: The index of the light to enable / disable
4498  *  Enable: Enable or disable the light
4499  *
4500  * Returns:
4501  *  D3D_OK on success
4502  *  For more details, see IWineD3DDevice::SetLightEnable
4503  *
4504  *****************************************************************************/
4505 static HRESULT WINAPI
4506 IDirect3DDeviceImpl_7_LightEnable(IDirect3DDevice7 *iface,
4507                                   DWORD LightIndex,
4508                                   BOOL Enable)
4509 {
4510     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4511     HRESULT hr;
4512     TRACE("(%p)->(%08x,%d): Relay!\n", This, LightIndex, Enable);
4513
4514     hr = IWineD3DDevice_SetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4515     return hr_ddraw_from_wined3d(hr);
4516 }
4517
4518 /*****************************************************************************
4519  * IDirect3DDevice7::GetLightEnable
4520  *
4521  * Retrieves if the light with the given index is enabled or not
4522  *
4523  * Version 7
4524  *
4525  * Params:
4526  *  LightIndex: Index of desired light
4527  *  Enable: Pointer to a BOOL which contains the result
4528  *
4529  * Returns:
4530  *  D3D_OK on success
4531  *  DDERR_INVALIDPARAMS if Enable is NULL
4532  *  See IWineD3DDevice::GetLightEnable for more details
4533  *
4534  *****************************************************************************/
4535 static HRESULT WINAPI
4536 IDirect3DDeviceImpl_7_GetLightEnable(IDirect3DDevice7 *iface,
4537                                      DWORD LightIndex,
4538                                      BOOL* Enable)
4539 {
4540     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4541     HRESULT hr;
4542     TRACE("(%p)->(%08x,%p): Relay\n", This, LightIndex, Enable);
4543
4544     if(!Enable)
4545         return DDERR_INVALIDPARAMS;
4546
4547     hr = IWineD3DDevice_GetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4548     return hr_ddraw_from_wined3d(hr);
4549 }
4550
4551 /*****************************************************************************
4552  * IDirect3DDevice7::SetClipPlane
4553  *
4554  * Sets custom clipping plane
4555  *
4556  * Version 7
4557  *
4558  * Params:
4559  *  Index: The index of the clipping plane
4560  *  PlaneEquation: An equation defining the clipping plane
4561  *
4562  * Returns:
4563  *  D3D_OK on success
4564  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4565  *  See IWineD3DDevice::SetClipPlane for more details
4566  *
4567  *****************************************************************************/
4568 static HRESULT WINAPI
4569 IDirect3DDeviceImpl_7_SetClipPlane(IDirect3DDevice7 *iface,
4570                                    DWORD Index,
4571                                    D3DVALUE* PlaneEquation)
4572 {
4573     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4574     TRACE("(%p)->(%08x,%p): Relay!\n", This, Index, PlaneEquation);
4575
4576     if(!PlaneEquation)
4577         return DDERR_INVALIDPARAMS;
4578
4579     return IWineD3DDevice_SetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4580 }
4581
4582 /*****************************************************************************
4583  * IDirect3DDevice7::GetClipPlane
4584  *
4585  * Returns the clipping plane with a specific index
4586  *
4587  * Params:
4588  *  Index: The index of the desired plane
4589  *  PlaneEquation: Address to store the plane equation to
4590  *
4591  * Returns:
4592  *  D3D_OK on success
4593  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4594  *  See IWineD3DDevice::GetClipPlane for more details
4595  *
4596  *****************************************************************************/
4597 static HRESULT WINAPI
4598 IDirect3DDeviceImpl_7_GetClipPlane(IDirect3DDevice7 *iface,
4599                                    DWORD Index,
4600                                    D3DVALUE* PlaneEquation)
4601 {
4602     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4603     TRACE("(%p)->(%d,%p): Relay!\n", This, Index, PlaneEquation);
4604
4605     if(!PlaneEquation)
4606         return DDERR_INVALIDPARAMS;
4607
4608     return IWineD3DDevice_GetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4609 }
4610
4611 /*****************************************************************************
4612  * IDirect3DDevice7::GetInfo
4613  *
4614  * Retrieves some information about the device. The DirectX sdk says that
4615  * this version returns S_FALSE for all retail builds of DirectX, that's what
4616  * this implementation does.
4617  *
4618  * Params:
4619  *  DevInfoID: Information type requested
4620  *  DevInfoStruct: Pointer to a structure to store the info to
4621  *  Size: Size of the structure
4622  *
4623  * Returns:
4624  *  S_FALSE, because it's a non-debug driver
4625  *
4626  *****************************************************************************/
4627 static HRESULT WINAPI
4628 IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
4629                               DWORD DevInfoID,
4630                               void *DevInfoStruct,
4631                               DWORD Size)
4632 {
4633     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4634     TRACE("(%p)->(%08x,%p,%08x)\n", This, DevInfoID, DevInfoStruct, Size);
4635
4636     if (TRACE_ON(d3d7))
4637     {
4638         TRACE(" info requested : ");
4639         switch (DevInfoID)
4640         {
4641             case D3DDEVINFOID_TEXTUREMANAGER: TRACE("D3DDEVINFOID_TEXTUREMANAGER\n"); break;
4642             case D3DDEVINFOID_D3DTEXTUREMANAGER: TRACE("D3DDEVINFOID_D3DTEXTUREMANAGER\n"); break;
4643             case D3DDEVINFOID_TEXTURING: TRACE("D3DDEVINFOID_TEXTURING\n"); break;
4644             default: ERR(" invalid flag !!!\n"); return DDERR_INVALIDPARAMS;
4645         }
4646     }
4647
4648     return S_FALSE; /* According to MSDN, this is valid for a non-debug driver */
4649 }
4650
4651 const IDirect3DDevice7Vtbl IDirect3DDevice7_Vtbl =
4652 {
4653     /*** IUnknown Methods ***/
4654     IDirect3DDeviceImpl_7_QueryInterface,
4655     IDirect3DDeviceImpl_7_AddRef,
4656     IDirect3DDeviceImpl_7_Release,
4657     /*** IDirect3DDevice7 ***/
4658     IDirect3DDeviceImpl_7_GetCaps,
4659     IDirect3DDeviceImpl_7_EnumTextureFormats,
4660     IDirect3DDeviceImpl_7_BeginScene,
4661     IDirect3DDeviceImpl_7_EndScene,
4662     IDirect3DDeviceImpl_7_GetDirect3D,
4663     IDirect3DDeviceImpl_7_SetRenderTarget,
4664     IDirect3DDeviceImpl_7_GetRenderTarget,
4665     IDirect3DDeviceImpl_7_Clear,
4666     IDirect3DDeviceImpl_7_SetTransform,
4667     IDirect3DDeviceImpl_7_GetTransform,
4668     IDirect3DDeviceImpl_7_SetViewport,
4669     IDirect3DDeviceImpl_7_MultiplyTransform,
4670     IDirect3DDeviceImpl_7_GetViewport,
4671     IDirect3DDeviceImpl_7_SetMaterial,
4672     IDirect3DDeviceImpl_7_GetMaterial,
4673     IDirect3DDeviceImpl_7_SetLight,
4674     IDirect3DDeviceImpl_7_GetLight,
4675     IDirect3DDeviceImpl_7_SetRenderState,
4676     IDirect3DDeviceImpl_7_GetRenderState,
4677     IDirect3DDeviceImpl_7_BeginStateBlock,
4678     IDirect3DDeviceImpl_7_EndStateBlock,
4679     IDirect3DDeviceImpl_7_PreLoad,
4680     IDirect3DDeviceImpl_7_DrawPrimitive,
4681     IDirect3DDeviceImpl_7_DrawIndexedPrimitive,
4682     IDirect3DDeviceImpl_7_SetClipStatus,
4683     IDirect3DDeviceImpl_7_GetClipStatus,
4684     IDirect3DDeviceImpl_7_DrawPrimitiveStrided,
4685     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided,
4686     IDirect3DDeviceImpl_7_DrawPrimitiveVB,
4687     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB,
4688     IDirect3DDeviceImpl_7_ComputeSphereVisibility,
4689     IDirect3DDeviceImpl_7_GetTexture,
4690     IDirect3DDeviceImpl_7_SetTexture,
4691     IDirect3DDeviceImpl_7_GetTextureStageState,
4692     IDirect3DDeviceImpl_7_SetTextureStageState,
4693     IDirect3DDeviceImpl_7_ValidateDevice,
4694     IDirect3DDeviceImpl_7_ApplyStateBlock,
4695     IDirect3DDeviceImpl_7_CaptureStateBlock,
4696     IDirect3DDeviceImpl_7_DeleteStateBlock,
4697     IDirect3DDeviceImpl_7_CreateStateBlock,
4698     IDirect3DDeviceImpl_7_Load,
4699     IDirect3DDeviceImpl_7_LightEnable,
4700     IDirect3DDeviceImpl_7_GetLightEnable,
4701     IDirect3DDeviceImpl_7_SetClipPlane,
4702     IDirect3DDeviceImpl_7_GetClipPlane,
4703     IDirect3DDeviceImpl_7_GetInfo
4704 };
4705
4706 const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
4707 {
4708     /*** IUnknown Methods ***/
4709     Thunk_IDirect3DDeviceImpl_3_QueryInterface,
4710     Thunk_IDirect3DDeviceImpl_3_AddRef,
4711     Thunk_IDirect3DDeviceImpl_3_Release,
4712     /*** IDirect3DDevice3 ***/
4713     IDirect3DDeviceImpl_3_GetCaps,
4714     IDirect3DDeviceImpl_3_GetStats,
4715     IDirect3DDeviceImpl_3_AddViewport,
4716     IDirect3DDeviceImpl_3_DeleteViewport,
4717     IDirect3DDeviceImpl_3_NextViewport,
4718     Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats,
4719     Thunk_IDirect3DDeviceImpl_3_BeginScene,
4720     Thunk_IDirect3DDeviceImpl_3_EndScene,
4721     Thunk_IDirect3DDeviceImpl_3_GetDirect3D,
4722     IDirect3DDeviceImpl_3_SetCurrentViewport,
4723     IDirect3DDeviceImpl_3_GetCurrentViewport,
4724     Thunk_IDirect3DDeviceImpl_3_SetRenderTarget,
4725     Thunk_IDirect3DDeviceImpl_3_GetRenderTarget,
4726     IDirect3DDeviceImpl_3_Begin,
4727     IDirect3DDeviceImpl_3_BeginIndexed,
4728     IDirect3DDeviceImpl_3_Vertex,
4729     IDirect3DDeviceImpl_3_Index,
4730     IDirect3DDeviceImpl_3_End,
4731     Thunk_IDirect3DDeviceImpl_3_GetRenderState,
4732     Thunk_IDirect3DDeviceImpl_3_SetRenderState,
4733     IDirect3DDeviceImpl_3_GetLightState,
4734     IDirect3DDeviceImpl_3_SetLightState,
4735     Thunk_IDirect3DDeviceImpl_3_SetTransform,
4736     Thunk_IDirect3DDeviceImpl_3_GetTransform,
4737     Thunk_IDirect3DDeviceImpl_3_MultiplyTransform,
4738     Thunk_IDirect3DDeviceImpl_3_DrawPrimitive,
4739     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive,
4740     Thunk_IDirect3DDeviceImpl_3_SetClipStatus,
4741     Thunk_IDirect3DDeviceImpl_3_GetClipStatus,
4742     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided,
4743     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided,
4744     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB,
4745     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB,
4746     Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility,
4747     Thunk_IDirect3DDeviceImpl_3_GetTexture,
4748     Thunk_IDirect3DDeviceImpl_3_SetTexture,
4749     Thunk_IDirect3DDeviceImpl_3_GetTextureStageState,
4750     Thunk_IDirect3DDeviceImpl_3_SetTextureStageState,
4751     Thunk_IDirect3DDeviceImpl_3_ValidateDevice
4752 };
4753
4754 const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
4755 {
4756     /*** IUnknown Methods ***/
4757     Thunk_IDirect3DDeviceImpl_2_QueryInterface,
4758     Thunk_IDirect3DDeviceImpl_2_AddRef,
4759     Thunk_IDirect3DDeviceImpl_2_Release,
4760     /*** IDirect3DDevice2 ***/
4761     Thunk_IDirect3DDeviceImpl_2_GetCaps,
4762     IDirect3DDeviceImpl_2_SwapTextureHandles,
4763     Thunk_IDirect3DDeviceImpl_2_GetStats,
4764     Thunk_IDirect3DDeviceImpl_2_AddViewport,
4765     Thunk_IDirect3DDeviceImpl_2_DeleteViewport,
4766     Thunk_IDirect3DDeviceImpl_2_NextViewport,
4767     IDirect3DDeviceImpl_2_EnumTextureFormats,
4768     Thunk_IDirect3DDeviceImpl_2_BeginScene,
4769     Thunk_IDirect3DDeviceImpl_2_EndScene,
4770     Thunk_IDirect3DDeviceImpl_2_GetDirect3D,
4771     Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport,
4772     Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport,
4773     Thunk_IDirect3DDeviceImpl_2_SetRenderTarget,
4774     Thunk_IDirect3DDeviceImpl_2_GetRenderTarget,
4775     Thunk_IDirect3DDeviceImpl_2_Begin,
4776     Thunk_IDirect3DDeviceImpl_2_BeginIndexed,
4777     Thunk_IDirect3DDeviceImpl_2_Vertex,
4778     Thunk_IDirect3DDeviceImpl_2_Index,
4779     Thunk_IDirect3DDeviceImpl_2_End,
4780     Thunk_IDirect3DDeviceImpl_2_GetRenderState,
4781     Thunk_IDirect3DDeviceImpl_2_SetRenderState,
4782     Thunk_IDirect3DDeviceImpl_2_GetLightState,
4783     Thunk_IDirect3DDeviceImpl_2_SetLightState,
4784     Thunk_IDirect3DDeviceImpl_2_SetTransform,
4785     Thunk_IDirect3DDeviceImpl_2_GetTransform,
4786     Thunk_IDirect3DDeviceImpl_2_MultiplyTransform,
4787     Thunk_IDirect3DDeviceImpl_2_DrawPrimitive,
4788     Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive,
4789     Thunk_IDirect3DDeviceImpl_2_SetClipStatus,
4790     Thunk_IDirect3DDeviceImpl_2_GetClipStatus
4791 };
4792
4793 const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
4794 {
4795     /*** IUnknown Methods ***/
4796     Thunk_IDirect3DDeviceImpl_1_QueryInterface,
4797     Thunk_IDirect3DDeviceImpl_1_AddRef,
4798     Thunk_IDirect3DDeviceImpl_1_Release,
4799     /*** IDirect3DDevice1 ***/
4800     IDirect3DDeviceImpl_1_Initialize,
4801     Thunk_IDirect3DDeviceImpl_1_GetCaps,
4802     Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles,
4803     IDirect3DDeviceImpl_1_CreateExecuteBuffer,
4804     Thunk_IDirect3DDeviceImpl_1_GetStats,
4805     IDirect3DDeviceImpl_1_Execute,
4806     Thunk_IDirect3DDeviceImpl_1_AddViewport,
4807     Thunk_IDirect3DDeviceImpl_1_DeleteViewport,
4808     Thunk_IDirect3DDeviceImpl_1_NextViewport,
4809     IDirect3DDeviceImpl_1_Pick,
4810     IDirect3DDeviceImpl_1_GetPickRecords,
4811     Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats,
4812     IDirect3DDeviceImpl_1_CreateMatrix,
4813     IDirect3DDeviceImpl_1_SetMatrix,
4814     IDirect3DDeviceImpl_1_GetMatrix,
4815     IDirect3DDeviceImpl_1_DeleteMatrix,
4816     Thunk_IDirect3DDeviceImpl_1_EndScene,
4817     Thunk_IDirect3DDeviceImpl_1_BeginScene,
4818     Thunk_IDirect3DDeviceImpl_1_GetDirect3D
4819 };
4820
4821 /*****************************************************************************
4822  * IDirect3DDeviceImpl_CreateHandle
4823  *
4824  * Not called from the VTable
4825  *
4826  * Some older interface versions operate with handles, which are basically
4827  * DWORDs which identify an interface, for example
4828  * IDirect3DDevice::SetRenderState with DIRECT3DRENDERSTATE_TEXTUREHANDLE
4829  *
4830  * Those handle could be just casts to the interface pointers or vice versa,
4831  * but that is not 64 bit safe and would mean blindly derefering a DWORD
4832  * passed by the app. Instead there is a dynamic array in the device which
4833  * keeps a DWORD to pointer information and a type for the handle.
4834  *
4835  * Basically this array only grows, when a handle is freed its pointer is
4836  * just set to NULL. There will be much more reads from the array than
4837  * insertion operations, so a dynamic array is fine.
4838  *
4839  * Params:
4840  *  This: D3DDevice implementation for which this handle should be created
4841  *
4842  * Returns:
4843  *  A free handle on success
4844  *  0 on failure
4845  *
4846  *****************************************************************************/
4847 DWORD
4848 IDirect3DDeviceImpl_CreateHandle(IDirect3DDeviceImpl *This)
4849 {
4850     DWORD i;
4851     struct HandleEntry *oldHandles = This->Handles;
4852
4853     TRACE("(%p)\n", This);
4854
4855     for(i = 0; i < This->numHandles; i++)
4856     {
4857         if(This->Handles[i].ptr == NULL &&
4858            This->Handles[i].type == DDrawHandle_Unknown)
4859         {
4860             TRACE("Reusing freed handle %d\n", i + 1);
4861             return i + 1;
4862         }
4863     }
4864
4865     TRACE("Growing the handle array\n");
4866
4867     This->numHandles++;
4868     This->Handles = HeapAlloc(GetProcessHeap(), 0, sizeof(struct HandleEntry) * This->numHandles);
4869     if(!This->Handles)
4870     {
4871         ERR("Out of memory\n");
4872         This->Handles = oldHandles;
4873         This->numHandles--;
4874         return 0;
4875     }
4876     if(oldHandles)
4877     {
4878         memcpy(This->Handles, oldHandles, (This->numHandles - 1) * sizeof(struct HandleEntry));
4879         HeapFree(GetProcessHeap(), 0, oldHandles);
4880     }
4881
4882     TRACE("Returning %d\n", This->numHandles);
4883     return This->numHandles;
4884 }