janitorial: Remove remaining NULL checks before free() (found by Smatch).
[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 %lu.\n", This, ref -1);
235
236     return ref;
237 }
238
239 static ULONG WINAPI
240 Thunk_IDirect3DDeviceImpl_3_AddRef(IDirect3DDevice3 *iface)
241 {
242     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
243     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
244     return IDirect3DDevice7_AddRef(ICOM_INTERFACE(This, IDirect3DDevice7));
245 }
246
247 static ULONG WINAPI
248 Thunk_IDirect3DDeviceImpl_2_AddRef(IDirect3DDevice2 *iface)
249 {
250     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
251     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", This);
252     return IDirect3DDevice7_AddRef(ICOM_INTERFACE(This, IDirect3DDevice7));
253 }
254
255 static ULONG WINAPI
256 Thunk_IDirect3DDeviceImpl_1_AddRef(IDirect3DDevice *iface)
257 {
258     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DDevice7 interface.\n", iface);
259     return IDirect3DDevice7_AddRef(COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice7, iface));
260 }
261
262 /*****************************************************************************
263  * IDirect3DDevice7::Release
264  *
265  * Decreases the refcount of the interface
266  * When the refcount is reduced to 0, the object is destroyed.
267  *
268  * Exists in Version 1, 2, 3 and 7
269  *
270  * Returns:d
271  *  The new refcount
272  *
273  *****************************************************************************/
274 static ULONG WINAPI
275 IDirect3DDeviceImpl_7_Release(IDirect3DDevice7 *iface)
276 {
277     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
278     ULONG ref = InterlockedDecrement(&This->ref);
279
280     TRACE("(%p)->() decrementing from %lu.\n", This, ref +1);
281
282     /* This method doesn't destroy the WineD3DDevice, because it's still in use for
283      * 2D rendering. IDirectDrawSurface7::Release will destroy the WineD3DDevice
284      * when the render target is released
285      */
286     if (ref == 0)
287     {
288         IParent *IndexBufferParent;
289         DWORD i;
290
291         /* Free the index buffer */
292         IWineD3DDevice_SetIndices(This->wineD3DDevice,
293                                   NULL,
294                                   0);
295         IWineD3DIndexBuffer_GetParent(This->indexbuffer,
296                                       (IUnknown **) &IndexBufferParent);
297         IParent_Release(IndexBufferParent); /* Once for the getParent */
298         if( IParent_Release(IndexBufferParent) != 0)  /* And now to destroy it */
299         {
300             ERR(" (%p) Something is still holding the index buffer parent %p\n", This, IndexBufferParent);
301         }
302
303         /* Restore the render targets */
304         if(This->OffScreenTarget)
305         {
306             /* This->target is the offscreen target.
307              * This->ddraw->d3d_target is the target used by DDraw
308              */
309             TRACE("(%p) Release: Using %p as front buffer, %p as back buffer\n", This, This->ddraw->d3d_target, NULL);
310             IWineD3DDevice_SetFrontBackBuffers(This->wineD3DDevice,
311                                                This->ddraw->d3d_target->WineD3DSurface,
312                                                NULL);
313         }
314
315         /* Release the WineD3DDevice. This won't destroy it */
316         if(IWineD3DDevice_Release(This->wineD3DDevice) <= 0)
317         {
318             ERR(" (%p) The wineD3D device %p was destroyed unexpectadely. Prepare for trouble\n", This, This->wineD3DDevice);
319         }
320
321         /* The texture handles should be unset by now, but there might be some bits
322          * missing in our reference counting(needs test). Do a sanity check
323          */
324         for(i = 0; i < This->numHandles; i++)
325         {
326             if(This->Handles[i].ptr)
327             {
328                 switch(This->Handles[i].type)
329                 {
330                     case DDrawHandle_Texture:
331                     {
332                         IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[i].ptr;
333                         FIXME("Texture Handle %ld not unset properly\n", i + 1);
334                         surf->Handle = 0;
335                     }
336                     break;
337
338                     case DDrawHandle_Material:
339                     {
340                         IDirect3DMaterialImpl *mat = (IDirect3DMaterialImpl *) This->Handles[i].ptr;
341                         FIXME("Material handle %ld not unset properly\n", i + 1);
342                         mat->Handle = 0;
343                     }
344                     break;
345
346                     case DDrawHandle_Matrix:
347                     {
348                         /* No fixme here because this might happen because of sloppy apps */
349                         WARN("Leftover matrix handle %ld, deleting\n", i + 1);
350                         IDirect3DDevice_DeleteMatrix(ICOM_INTERFACE(This, IDirect3DDevice),
351                                                      i + 1);
352                     }
353                     break;
354
355                     default:
356                         FIXME("Unknown handle %ld 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,%08lx)\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,%08lx)\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,%08lx) 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,%08lx) 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,%08lx,%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 %ld\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)->(%08lx,%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 %ld out of range\n", D3DMatHandle);
1302         return DDERR_INVALIDPARAMS;
1303     }
1304     else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
1305     {
1306         ERR("Handle %ld 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)->(%08lx,%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 %ld out of range\n", D3DMatHandle);
1350         return DDERR_INVALIDPARAMS;
1351     }
1352     else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
1353     {
1354         ERR("Handle %ld 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)->(%08lx)\n", This, (DWORD) D3DMatHandle);
1385
1386     if(!D3DMatHandle)
1387         return DDERR_INVALIDPARAMS;
1388
1389     if(D3DMatHandle > This->numHandles)
1390     {
1391         ERR("Handle %ld out of range\n", D3DMatHandle);
1392         return DDERR_INVALIDPARAMS;
1393     }
1394     else if(This->Handles[D3DMatHandle - 1].type != DDrawHandle_Matrix)
1395     {
1396         ERR("Handle %ld 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,%08lx): 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,%08lx) 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,%08lx) 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,%ld,%08lx)\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,%08lx): 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,%08lx,%p,%08lx,%08lx): 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,%08lx,%08lx): 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)->(%08lx)\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)->(%08lx) 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     return IWineD3DDevice_GetRenderState(This->wineD3DDevice,
2094                                          RenderStateType,
2095                                          Value);
2096 }
2097
2098 static HRESULT WINAPI
2099 Thunk_IDirect3DDeviceImpl_3_GetRenderState(IDirect3DDevice3 *iface,
2100                                            D3DRENDERSTATETYPE dwRenderStateType,
2101                                            DWORD *lpdwRenderState)
2102 {
2103     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2104     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
2105     return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2106                                            dwRenderStateType,
2107                                            lpdwRenderState);
2108 }
2109
2110 static HRESULT WINAPI
2111 Thunk_IDirect3DDeviceImpl_2_GetRenderState(IDirect3DDevice2 *iface,
2112                                            D3DRENDERSTATETYPE dwRenderStateType,
2113                                            DWORD *lpdwRenderState)
2114 {
2115     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2116     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, dwRenderStateType, lpdwRenderState);
2117     return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2118                                            dwRenderStateType,
2119                                            lpdwRenderState);
2120 }
2121
2122 /*****************************************************************************
2123  * IDirect3DDevice7::SetRenderState
2124  *
2125  * Sets a render state. The possible render states are defined in
2126  * include/d3dtypes.h
2127  *
2128  * Version 2, 3 and 7
2129  *
2130  * Params:
2131  *  RenderStateType: State to set
2132  *  Value: Value to assign to that state
2133  *
2134  * Returns:
2135  *  D3D_OK on success,
2136  *  for details see IWineD3DDevice::SetRenderState
2137  *
2138  *****************************************************************************/
2139 static HRESULT WINAPI
2140 IDirect3DDeviceImpl_7_SetRenderState(IDirect3DDevice7 *iface,
2141                                      D3DRENDERSTATETYPE RenderStateType,
2142                                      DWORD Value)
2143 {
2144     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2145     TRACE("(%p)->(%08x,%ld): Relay\n", This, RenderStateType, Value);
2146
2147     /* Some render states need special care */
2148     switch(RenderStateType)
2149     {
2150         case D3DRENDERSTATE_TEXTUREHANDLE:
2151         {
2152             if(Value == 0)
2153             {
2154                     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
2155                                                      0,
2156                                                      NULL);
2157             }
2158
2159             if(Value > This->numHandles)
2160             {
2161                 FIXME("Specified handle %ld out of range\n", Value);
2162                 return DDERR_INVALIDPARAMS;
2163             }
2164             if(This->Handles[Value - 1].type != DDrawHandle_Texture)
2165             {
2166                 FIXME("Handle %ld isn't a texture handle\n", Value);
2167                 return DDERR_INVALIDPARAMS;
2168             }
2169             else
2170             {
2171                 IDirectDrawSurfaceImpl *surf = (IDirectDrawSurfaceImpl *) This->Handles[Value - 1].ptr;
2172                 return IWineD3DDevice_SetTexture(This->wineD3DDevice,
2173                                                  0,
2174                                                  (IWineD3DBaseTexture *) surf->wineD3DTexture);
2175             }
2176         }
2177
2178         case D3DRENDERSTATE_TEXTUREMAG:
2179         {
2180             WINED3DTEXTUREFILTERTYPE tex_mag = WINED3DTEXF_NONE;
2181
2182             switch ((D3DTEXTUREFILTER) Value)
2183             {
2184                 case D3DFILTER_NEAREST:
2185                     tex_mag = WINED3DTEXF_POINT;
2186                     break;
2187                 case D3DFILTER_LINEAR:
2188                     tex_mag = WINED3DTEXF_LINEAR;
2189                     break;
2190                 default:
2191                     ERR("Unhandled texture mag %ld !\n",Value);
2192             }
2193
2194             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2195                                                   0, WINED3DSAMP_MAGFILTER,
2196                                                   tex_mag);
2197         }
2198
2199         case D3DRENDERSTATE_TEXTUREMIN:
2200         {
2201             WINED3DTEXTUREFILTERTYPE tex_min = WINED3DTEXF_NONE;
2202
2203             switch ((D3DTEXTUREFILTER) Value)
2204             {
2205                 case D3DFILTER_NEAREST:
2206                     tex_min = WINED3DTEXF_POINT;
2207                     break;
2208                 case D3DFILTER_LINEAR:
2209                     tex_min = WINED3DTEXF_LINEAR;
2210                     break;
2211                 default:
2212                     ERR("Unhandled texture mag %ld !\n",Value);
2213             }
2214
2215             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
2216                                                   0, WINED3DSAMP_MINFILTER,
2217                                                   tex_min);
2218         }
2219
2220         case D3DRENDERSTATE_TEXTUREADDRESSU:
2221         case D3DRENDERSTATE_TEXTUREADDRESSV:
2222         case D3DRENDERSTATE_TEXTUREADDRESS:
2223         {
2224             WINED3DTEXTURESTAGESTATETYPE TexStageStateType;
2225
2226             if (RenderStateType == D3DRENDERSTATE_TEXTUREADDRESS)
2227             {
2228                 TexStageStateType = WINED3DTSS_ADDRESS;
2229             }
2230             else if (RenderStateType == D3DRENDERSTATE_TEXTUREADDRESSU)
2231             {
2232                 TexStageStateType = WINED3DTSS_ADDRESSU;
2233             }
2234             else
2235             {
2236                 TexStageStateType = WINED3DTSS_ADDRESSV;
2237             }
2238
2239             return IWineD3DDevice_SetTextureStageState(This->wineD3DDevice,
2240                                                        0, TexStageStateType,
2241                                                        Value);
2242         }
2243
2244         case D3DRENDERSTATE_TEXTUREMAPBLEND:
2245         {
2246             /* Old texture combine setup style, superseded by texture stage states
2247              * in D3D7. It is safe for us to wrap it to texture stage states.
2248              */
2249             switch ( (D3DTEXTUREBLEND) Value)
2250             {
2251                 case D3DTBLEND_MODULATE:
2252                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
2253                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
2254                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLORARG2, D3DTA_CURRENT);
2255                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
2256                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2257                     break;
2258
2259                 case D3DTBLEND_MODULATEALPHA:
2260                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
2261                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
2262                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLORARG2, D3DTA_CURRENT);
2263                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
2264                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
2265                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
2266                     break;
2267
2268                 case D3DTBLEND_DECAL:
2269                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
2270                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
2271                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2272                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2273                     break;
2274
2275                 case D3DTBLEND_DECALALPHA:
2276                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
2277                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
2278                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAARG2, D3DTA_CURRENT);
2279                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2280                     IWineD3DDevice_SetTextureStageState(iface, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
2281                     break;
2282
2283                 default:
2284                     ERR("Unhandled texture environment %ld !\n",Value);
2285                 }
2286                 return D3D_OK;
2287             break;
2288         }
2289
2290         default:
2291             return IWineD3DDevice_SetRenderState(This->wineD3DDevice,
2292                                                  RenderStateType,
2293                                                  Value);
2294     }
2295 }
2296
2297 static HRESULT WINAPI
2298 Thunk_IDirect3DDeviceImpl_3_SetRenderState(IDirect3DDevice3 *iface,
2299                                            D3DRENDERSTATETYPE RenderStateType,
2300                                            DWORD Value)
2301 {
2302     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2303     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2304     return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2305                                            RenderStateType,
2306                                            Value);
2307 }
2308
2309 static HRESULT WINAPI
2310 Thunk_IDirect3DDeviceImpl_2_SetRenderState(IDirect3DDevice2 *iface,
2311                                            D3DRENDERSTATETYPE RenderStateType,
2312                                            DWORD Value)
2313 {
2314     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2315     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx) thunking to IDirect3DDevice7 interface.\n", This, RenderStateType, Value);
2316     return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2317                                            RenderStateType,
2318                                            Value);
2319 }
2320
2321 /*****************************************************************************
2322  * Direct3DDevice3::SetLightState
2323  *
2324  * Sets a light state for Direct3DDevice3 and Direct3DDevice2. The
2325  * light states are forwarded to Direct3DDevice7 render states
2326  *
2327  * Version 2 and 3
2328  *
2329  * Params:
2330  *  LightStateType: The light state to change
2331  *  Value: The value to assign to that light state
2332  *
2333  * Returns:
2334  *  D3D_OK on success
2335  *  DDERR_INVALIDPARAMS if the parameters were incorrect
2336  *  Also check IDirect3DDevice7::SetRenderState
2337  *
2338  *****************************************************************************/
2339 static HRESULT WINAPI
2340 IDirect3DDeviceImpl_3_SetLightState(IDirect3DDevice3 *iface,
2341                                     D3DLIGHTSTATETYPE LightStateType,
2342                                     DWORD Value)
2343 {
2344     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2345
2346     TRACE("(%p)->(%08x,%08lx)\n", This, LightStateType, Value);
2347
2348     if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
2349     {
2350         TRACE("Unexpected Light State Type\n");
2351         return DDERR_INVALIDPARAMS;
2352     }
2353
2354     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
2355     {
2356         IDirect3DMaterialImpl *mat;
2357
2358         if(Value == 0) mat = NULL;
2359         else if(Value > This->numHandles)
2360         {
2361             ERR("Material handle out of range(%ld)\n", Value);
2362             return DDERR_INVALIDPARAMS;
2363         }
2364         else if(This->Handles[Value - 1].type != DDrawHandle_Material)
2365         {
2366             ERR("Invalid handle %ld\n", Value);
2367             return DDERR_INVALIDPARAMS;
2368         }
2369         else
2370         {
2371             mat = (IDirect3DMaterialImpl *) This->Handles[Value - 1].ptr;
2372         }
2373
2374         if (mat != NULL)
2375         {
2376             TRACE(" activating material %p.\n", mat);
2377             mat->activate(mat);
2378         }
2379         else
2380         {
2381             FIXME(" D3DLIGHTSTATE_MATERIAL called with NULL material !!!\n");
2382         }
2383         This->material = Value;
2384     }
2385     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
2386     {
2387         switch (Value)
2388         {
2389             case D3DCOLOR_MONO:
2390                 ERR("DDCOLOR_MONO should not happen!\n");
2391                 break;
2392             case D3DCOLOR_RGB:
2393                 /* We are already in this mode */
2394                 TRACE("Setting color model to RGB (no-op).\n");
2395                 break;
2396             default:
2397                 ERR("Unknown color model!\n");
2398                 return DDERR_INVALIDPARAMS;
2399         }
2400     }
2401     else
2402     {
2403         D3DRENDERSTATETYPE rs;
2404         switch (LightStateType)
2405         {
2406             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
2407                 rs = D3DRENDERSTATE_AMBIENT;
2408                 break;          
2409             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
2410                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2411                 break;
2412             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
2413                 rs = D3DRENDERSTATE_FOGSTART;
2414                 break;
2415             case D3DLIGHTSTATE_FOGEND:        /* 6 */
2416                 rs = D3DRENDERSTATE_FOGEND;
2417                 break;
2418             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
2419                 rs = D3DRENDERSTATE_FOGDENSITY;
2420                 break;
2421             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
2422                 rs = D3DRENDERSTATE_COLORVERTEX;
2423                 break;
2424             default:
2425                 ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2426                 return DDERR_INVALIDPARAMS;
2427         }
2428
2429         return IDirect3DDevice7_SetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7), 
2430                                                rs,
2431                                                Value);
2432     }
2433
2434     return D3D_OK;
2435 }
2436
2437 static HRESULT WINAPI
2438 Thunk_IDirect3DDeviceImpl_2_SetLightState(IDirect3DDevice2 *iface,
2439                                           D3DLIGHTSTATETYPE LightStateType,
2440                                           DWORD Value)
2441 {
2442     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2443     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2444     return IDirect3DDevice3_SetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
2445                                           LightStateType,
2446                                           Value);
2447 }
2448
2449 /*****************************************************************************
2450  * IDirect3DDevice3::GetLightState
2451  *
2452  * Returns the current setting of a light state. The state is read from
2453  * the Direct3DDevice7 render state.
2454  *
2455  * Version 2 and 3
2456  *
2457  * Params:
2458  *  LightStateType: The light state to return
2459  *  Value: The address to store the light state setting at
2460  *
2461  * Returns:
2462  *  D3D_OK on success
2463  *  DDDERR_INVALIDPARAMS if the parameters were incorrect
2464  *  Also see IDirect3DDevice7::GetRenderState
2465  *
2466  *****************************************************************************/
2467 static HRESULT WINAPI
2468 IDirect3DDeviceImpl_3_GetLightState(IDirect3DDevice3 *iface,
2469                                     D3DLIGHTSTATETYPE LightStateType,
2470                                     DWORD *Value)
2471 {
2472     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2473
2474     TRACE("(%p)->(%08x,%p)\n", This, LightStateType, Value);
2475
2476     if (!LightStateType && (LightStateType > D3DLIGHTSTATE_COLORVERTEX))
2477     {
2478         TRACE("Unexpected Light State Type\n");
2479         return DDERR_INVALIDPARAMS;
2480     }
2481
2482     if(!Value)
2483         return DDERR_INVALIDPARAMS;
2484
2485     if (LightStateType == D3DLIGHTSTATE_MATERIAL /* 1 */)
2486     {
2487         *Value = This->material;
2488     }
2489     else if (LightStateType == D3DLIGHTSTATE_COLORMODEL /* 3 */)
2490     {
2491         *Value = D3DCOLOR_RGB;
2492     }
2493     else
2494     {
2495         D3DRENDERSTATETYPE rs;
2496         switch (LightStateType)
2497         {
2498             case D3DLIGHTSTATE_AMBIENT:       /* 2 */
2499                 rs = D3DRENDERSTATE_AMBIENT;
2500                 break;          
2501             case D3DLIGHTSTATE_FOGMODE:       /* 4 */
2502                 rs = D3DRENDERSTATE_FOGVERTEXMODE;
2503                 break;
2504             case D3DLIGHTSTATE_FOGSTART:      /* 5 */
2505                 rs = D3DRENDERSTATE_FOGSTART;
2506                 break;
2507             case D3DLIGHTSTATE_FOGEND:        /* 6 */
2508                 rs = D3DRENDERSTATE_FOGEND;
2509                 break;
2510             case D3DLIGHTSTATE_FOGDENSITY:    /* 7 */
2511                 rs = D3DRENDERSTATE_FOGDENSITY;
2512                 break;
2513             case D3DLIGHTSTATE_COLORVERTEX:   /* 8 */
2514                 rs = D3DRENDERSTATE_COLORVERTEX;
2515                 break;
2516             default:
2517                 ERR("Unknown D3DLIGHTSTATETYPE %d.\n", LightStateType);
2518                 return DDERR_INVALIDPARAMS;
2519         }
2520
2521         return IDirect3DDevice7_GetRenderState(ICOM_INTERFACE(This, IDirect3DDevice7),
2522                                                rs,
2523                                                Value);
2524     }
2525
2526     return D3D_OK;
2527 }
2528
2529 static HRESULT WINAPI
2530 Thunk_IDirect3DDeviceImpl_2_GetLightState(IDirect3DDevice2 *iface,
2531                                           D3DLIGHTSTATETYPE LightStateType,
2532                                           DWORD *Value)
2533 {
2534     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2535     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice3 interface.\n", This, LightStateType, Value);
2536     return IDirect3DDevice3_GetLightState(ICOM_INTERFACE(This, IDirect3DDevice3),
2537                                           LightStateType,
2538                                           Value);
2539 }
2540
2541 /*****************************************************************************
2542  * IDirect3DDevice7::SetTransform
2543  *
2544  * Assigns a D3DMATRIX to a transform type. The transform types are defined
2545  * in include/d3dtypes.h.
2546  * The D3DTRANSFORMSTATE_WORLD (=1) is translated to D3DTS_WORLDMATRIX(0)
2547  * (=255) for wined3d, because the 1 transform state was removed in d3d8
2548  * and WineD3D already understands the replacement D3DTS_WORLDMATRIX(0)
2549  *
2550  * Version 2, 3 and 7
2551  *
2552  * Params:
2553  *  TransformStateType: transform state to set
2554  *  Matrix: Matrix to assign to the state
2555  *
2556  * Returns:
2557  *  D3D_OK on success
2558  *  DDERR_INVALIDPARAMS if Matrix == NULL
2559  *  For details see IWineD3DDevice::SetTransform
2560  *
2561  *****************************************************************************/
2562 static HRESULT WINAPI
2563 IDirect3DDeviceImpl_7_SetTransform(IDirect3DDevice7 *iface,
2564                                    D3DTRANSFORMSTATETYPE TransformStateType,
2565                                    D3DMATRIX *Matrix)
2566 {
2567     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2568     D3DTRANSFORMSTATETYPE type = TransformStateType;
2569     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2570
2571     if(!Matrix)
2572         return DDERR_INVALIDPARAMS;
2573
2574     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2575      * use D3DTS_WORLDMATRIX(0) instead
2576      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2577      */
2578     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2579         type = (D3DTRANSFORMSTATETYPE)(0 + 256);
2580
2581     return IWineD3DDevice_SetTransform(This->wineD3DDevice,
2582                                        type,
2583                                        Matrix);
2584 }
2585
2586 static HRESULT WINAPI
2587 Thunk_IDirect3DDeviceImpl_3_SetTransform(IDirect3DDevice3 *iface,
2588                                          D3DTRANSFORMSTATETYPE TransformStateType,
2589                                          D3DMATRIX *D3DMatrix)
2590 {
2591     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2592     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2593     return IDirect3DDevice7_SetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2594                                          TransformStateType,
2595                                          D3DMatrix);
2596 }
2597
2598 static HRESULT WINAPI
2599 Thunk_IDirect3DDeviceImpl_2_SetTransform(IDirect3DDevice2 *iface,
2600                                          D3DTRANSFORMSTATETYPE TransformStateType,
2601                                          D3DMATRIX *D3DMatrix)
2602 {
2603     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, 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 /*****************************************************************************
2611  * IDirect3DDevice7::GetTransform
2612  *
2613  * Returns the matrix assigned to a transform state
2614  * D3DTRANSFORMSTATE_WORLD is translated to D3DTS_WORLDMATRIX(0), see
2615  * SetTransform
2616  *
2617  * Params:
2618  *  TransformStateType: State to read the matrix from
2619  *  Matrix: Address to store the matrix at
2620  *
2621  * Returns:
2622  *  D3D_OK on success
2623  *  DDERR_INVALIDPARAMS if Matrix == NULL
2624  *  For details, see IWineD3DDevice::GetTransform
2625  *
2626  *****************************************************************************/
2627 static HRESULT WINAPI
2628 IDirect3DDeviceImpl_7_GetTransform(IDirect3DDevice7 *iface,
2629                                    D3DTRANSFORMSTATETYPE TransformStateType,
2630                                    D3DMATRIX *Matrix)
2631 {
2632     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2633     D3DTRANSFORMSTATETYPE type = TransformStateType;
2634     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, Matrix);
2635
2636     if(!Matrix)
2637         return DDERR_INVALIDPARAMS;
2638
2639     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2640      * use D3DTS_WORLDMATRIX(0) instead
2641      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2642      */
2643     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2644         type = (D3DTRANSFORMSTATETYPE)(0 + 256);
2645
2646     return IWineD3DDevice_GetTransform(This->wineD3DDevice, type, Matrix);
2647 }
2648
2649 static HRESULT WINAPI
2650 Thunk_IDirect3DDeviceImpl_3_GetTransform(IDirect3DDevice3 *iface,
2651                                          D3DTRANSFORMSTATETYPE TransformStateType,
2652                                          D3DMATRIX *D3DMatrix)
2653 {
2654     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2655     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2656     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2657                                          TransformStateType,
2658                                          D3DMatrix);
2659 }
2660
2661 static HRESULT WINAPI
2662 Thunk_IDirect3DDeviceImpl_2_GetTransform(IDirect3DDevice2 *iface,
2663                                          D3DTRANSFORMSTATETYPE TransformStateType,
2664                                          D3DMATRIX *D3DMatrix)
2665 {
2666     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2667     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2668     return IDirect3DDevice7_GetTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2669                                          TransformStateType,
2670                                          D3DMatrix);
2671 }
2672
2673 /*****************************************************************************
2674  * IDirect3DDevice7::MultiplyTransform
2675  *
2676  * Multiplies the already-set transform matrix of a transform state
2677  * with another matrix. For the world matrix, see SetTransform
2678  *
2679  * Version 2, 3 and 7
2680  *
2681  * Params:
2682  *  TransformStateType: Transform state to multiply
2683  *  D3DMatrix Matrix to multiply with.
2684  *
2685  * Returns
2686  *  D3D_OK on success
2687  *  DDERR_INVALIDPARAMS if D3DMatrix is NULL
2688  *  For details, see IWineD3DDevice::MultiplyTransform
2689  *
2690  *****************************************************************************/
2691 static HRESULT WINAPI
2692 IDirect3DDeviceImpl_7_MultiplyTransform(IDirect3DDevice7 *iface,
2693                                         D3DTRANSFORMSTATETYPE TransformStateType,
2694                                         D3DMATRIX *D3DMatrix)
2695 {
2696     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2697     TRACE("(%p)->(%08x,%p): Relay\n", This, TransformStateType, D3DMatrix);
2698
2699     /* D3DTRANSFORMSTATE_WORLD doesn't exist in WineD3D,
2700      * use D3DTS_WORLDMATRIX(0) instead
2701      * D3DTS_WORLDMATRIX(index) is (D3DTRANSFORMSTATETYPE)(index + 256)
2702      */
2703     if(TransformStateType == D3DTRANSFORMSTATE_WORLD)
2704         TransformStateType = (D3DTRANSFORMSTATETYPE)(0 + 256);
2705
2706     return IWineD3DDevice_MultiplyTransform(This->wineD3DDevice,
2707                                             TransformStateType,
2708                                             D3DMatrix);
2709 }
2710
2711 static HRESULT WINAPI
2712 Thunk_IDirect3DDeviceImpl_3_MultiplyTransform(IDirect3DDevice3 *iface,
2713                                               D3DTRANSFORMSTATETYPE TransformStateType,
2714                                               D3DMATRIX *D3DMatrix)
2715 {
2716     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2717     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2718     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2719                                               TransformStateType,
2720                                               D3DMatrix);
2721 }
2722
2723 static HRESULT WINAPI
2724 Thunk_IDirect3DDeviceImpl_2_MultiplyTransform(IDirect3DDevice2 *iface,
2725                                               D3DTRANSFORMSTATETYPE TransformStateType,
2726                                               D3DMATRIX *D3DMatrix)
2727 {
2728     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2729     TRACE_(ddraw_thunk)("(%p)->(%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, TransformStateType, D3DMatrix);
2730     return IDirect3DDevice7_MultiplyTransform(ICOM_INTERFACE(This, IDirect3DDevice7),
2731                                               TransformStateType,
2732                                               D3DMatrix);
2733 }
2734
2735 /*****************************************************************************
2736  * IDirect3DDevice7::DrawPrimitive
2737  *
2738  * Draws primitives based on vertices in an application-provided pointer
2739  *
2740  * Version 2, 3 and 7. The IDirect3DDevice2 thunk converts the fixed vertex type into
2741  * an FVF format for D3D7
2742  *
2743  * Params:
2744  *  PrimitiveType: The type of the primitives to draw
2745  *  Vertex type: Flexible vertex format vertex description
2746  *  Vertices: Pointer to the vertex array
2747  *  VertexCount: The number of vertices to draw
2748  *  Flags: As usual a few flags
2749  *
2750  * Returns:
2751  *  D3D_OK on success
2752  *  DDERR_INVALIDPARAMS if Vertices is NULL
2753  *  For details, see IWineD3DDevice::DrawPrimitiveUP
2754  *
2755  *****************************************************************************/
2756 static HRESULT WINAPI
2757 IDirect3DDeviceImpl_7_DrawPrimitive(IDirect3DDevice7 *iface,
2758                                     D3DPRIMITIVETYPE PrimitiveType,
2759                                     DWORD VertexType,
2760                                     void *Vertices,
2761                                     DWORD VertexCount,
2762                                     DWORD Flags)
2763 {
2764     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2765     UINT PrimitiveCount, stride;
2766     HRESULT hr;
2767     TRACE("(%p)->(%08x,%08lx,%p,%08lx,%08lx): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2768
2769     if(!Vertices)
2770         return DDERR_INVALIDPARAMS;
2771
2772     /* Get the vertex count */
2773     switch(PrimitiveType)
2774     {
2775       case D3DPT_POINTLIST: 
2776         PrimitiveCount = VertexCount;
2777         break;
2778
2779       case D3DPT_LINELIST: 
2780         PrimitiveCount = VertexCount / 2;
2781         break;
2782
2783       case D3DPT_LINESTRIP:
2784         PrimitiveCount = VertexCount - 1;
2785         break;
2786
2787       case D3DPT_TRIANGLELIST:
2788         PrimitiveCount = VertexCount / 3;
2789         break;
2790
2791       case D3DPT_TRIANGLESTRIP:
2792         PrimitiveCount = VertexCount - 2;
2793         break;
2794
2795       case D3DPT_TRIANGLEFAN:
2796         PrimitiveCount = VertexCount - 2;
2797         break;
2798
2799       default: return DDERR_INVALIDPARAMS;
2800     }
2801
2802     /* Get the stride */
2803     stride = get_flexible_vertex_size(VertexType);
2804
2805     /* Set the FVF */
2806     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, VertexType);
2807     if(hr != D3D_OK) return hr;
2808
2809     /* This method translates to the user pointer draw of WineD3D */
2810     return IWineD3DDevice_DrawPrimitiveUP(This->wineD3DDevice,
2811                                           PrimitiveType,
2812                                           PrimitiveCount,
2813                                           Vertices,
2814                                           stride);
2815 }
2816
2817 static HRESULT WINAPI
2818 Thunk_IDirect3DDeviceImpl_3_DrawPrimitive(IDirect3DDevice3 *iface,
2819                                           D3DPRIMITIVETYPE PrimitiveType,
2820                                           DWORD VertexType,
2821                                           void *Vertices,
2822                                           DWORD VertexCount,
2823                                           DWORD Flags)
2824 {
2825     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2826     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2827     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2828                                           PrimitiveType,
2829                                           VertexType,
2830                                           Vertices,
2831                                           VertexCount,
2832                                           Flags);
2833 }
2834
2835 static HRESULT WINAPI
2836 Thunk_IDirect3DDeviceImpl_2_DrawPrimitive(IDirect3DDevice2 *iface,
2837                                           D3DPRIMITIVETYPE PrimitiveType,
2838                                           D3DVERTEXTYPE VertexType,
2839                                           void *Vertices,
2840                                           DWORD VertexCount,
2841                                           DWORD Flags)
2842 {
2843     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2844     DWORD FVF;
2845     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Flags);
2846
2847     switch(VertexType)
2848     {
2849         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
2850         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
2851         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
2852         default:
2853             ERR("Unexpected vertex type %d\n", VertexType);
2854             return DDERR_INVALIDPARAMS;  /* Should never happen */
2855     }
2856
2857     return IDirect3DDevice7_DrawPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2858                                           PrimitiveType,
2859                                           FVF,
2860                                           Vertices,
2861                                           VertexCount,
2862                                           Flags);
2863 }
2864
2865 /*****************************************************************************
2866  * IDirect3DDevice7::DrawIndexedPrimitive
2867  *
2868  * Draws vertices from an application-provided pointer, based on the index
2869  * numbers in a WORD array.
2870  *
2871  * Version 2, 3 and 7. The version 7 thunk translates the vertex type into
2872  * an FVF format for D3D7
2873  *
2874  * Params:
2875  *  PrimitiveType: The primitive type to draw
2876  *  VertexType: The FVF vertex description
2877  *  Vertices: Pointer to the vertex array
2878  *  VertexCount: ?
2879  *  Indices: Pointer to the index array
2880  *  IndexCount: Number of indices = Number of vertices to draw
2881  *  Flags: As usual, some flags
2882  *
2883  * Returns:
2884  *  D3D_OK on success
2885  *  DDERR_INVALIDPARAMS if Vertices or Indices is NULL
2886  *  For details, see IWineD3DDevice::DrawIndexedPrimitiveUP
2887  *
2888  *****************************************************************************/
2889 static HRESULT WINAPI
2890 IDirect3DDeviceImpl_7_DrawIndexedPrimitive(IDirect3DDevice7 *iface,
2891                                            D3DPRIMITIVETYPE PrimitiveType,
2892                                            DWORD VertexType,
2893                                            void *Vertices,
2894                                            DWORD VertexCount,
2895                                            WORD *Indices,
2896                                            DWORD IndexCount,
2897                                            DWORD Flags)
2898 {
2899     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
2900     UINT PrimitiveCount = 0;
2901     HRESULT hr;
2902     TRACE("(%p)->(%08x,%08lx,%p,%08lx,%p,%08lx,%08lx): Relay!\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
2903     /* Get the primitive number */
2904     switch(PrimitiveType)
2905     {
2906       case D3DPT_POINTLIST: 
2907         PrimitiveCount = IndexCount;
2908         break;
2909
2910       case D3DPT_LINELIST: 
2911         PrimitiveCount = IndexCount / 2;
2912         break;
2913
2914       case D3DPT_LINESTRIP:
2915         PrimitiveCount = IndexCount - 1;
2916         break;
2917
2918       case D3DPT_TRIANGLELIST:
2919         PrimitiveCount = IndexCount / 3;
2920         break;
2921
2922       case D3DPT_TRIANGLESTRIP:
2923         PrimitiveCount = IndexCount - 2;
2924         break;
2925
2926       case D3DPT_TRIANGLEFAN:
2927         PrimitiveCount = IndexCount - 2;
2928         break;
2929
2930       default: return DDERR_INVALIDPARAMS;
2931     }
2932
2933     /* Set the D3DDevice's FVF */
2934     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, VertexType);
2935     if(FAILED(hr))
2936     {
2937         ERR(" (%p) Setting the FVF failed, hr = %lx!\n", This, hr);
2938         return hr;
2939     }
2940
2941     return IWineD3DDevice_DrawIndexedPrimitiveUP(This->wineD3DDevice,
2942                                                  PrimitiveType,
2943                                                  0 /* MinVertexIndex */,
2944                                                  VertexCount /* UINT NumVertexIndex */,
2945                                                  PrimitiveCount,
2946                                                  Indices,
2947                                                  WINED3DFMT_INDEX16,
2948                                                  Vertices,
2949                                                  get_flexible_vertex_size(VertexType));
2950 }
2951
2952 static HRESULT WINAPI
2953 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive(IDirect3DDevice3 *iface,
2954                                                  D3DPRIMITIVETYPE PrimitiveType,
2955                                                  DWORD VertexType,
2956                                                  void *Vertices,
2957                                                  DWORD VertexCount,
2958                                                  WORD *Indices,
2959                                                  DWORD IndexCount,
2960                                                  DWORD Flags)
2961 {
2962     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
2963     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx,%p,%08lx,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
2964     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2965                                                  PrimitiveType,
2966                                                  VertexType,
2967                                                  Vertices,
2968                                                  VertexCount,
2969                                                  Indices,
2970                                                  IndexCount,
2971                                                  Flags);
2972 }
2973
2974 static HRESULT WINAPI
2975 Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive(IDirect3DDevice2 *iface,
2976                                                  D3DPRIMITIVETYPE PrimitiveType,
2977                                                  D3DVERTEXTYPE VertexType,
2978                                                  void *Vertices,
2979                                                  DWORD VertexCount,
2980                                                  WORD *Indices,
2981                                                  DWORD IndexCount,
2982                                                  DWORD Flags)
2983 {
2984     DWORD FVF;
2985     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
2986     TRACE_(ddraw_thunk)("(%p)->(%08x,%08x,%p,%08lx,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, Vertices, VertexCount, Indices, IndexCount, Flags);
2987
2988     switch(VertexType)
2989     {
2990         case D3DVT_VERTEX: FVF = D3DFVF_VERTEX; break;
2991         case D3DVT_LVERTEX: FVF = D3DFVF_LVERTEX; break;
2992         case D3DVT_TLVERTEX: FVF = D3DFVF_TLVERTEX; break;
2993         default:
2994             ERR("Unexpected vertex type %d\n", VertexType);
2995             return DDERR_INVALIDPARAMS;  /* Should never happen */
2996     }
2997
2998     return IDirect3DDevice7_DrawIndexedPrimitive(ICOM_INTERFACE(This, IDirect3DDevice7),
2999                                                  PrimitiveType,
3000                                                  FVF,
3001                                                  Vertices,
3002                                                  VertexCount,
3003                                                  Indices,
3004                                                  IndexCount,
3005                                                  Flags);
3006 }
3007
3008 /*****************************************************************************
3009  * IDirect3DDevice7::SetClipStatus
3010  *
3011  * Sets the clip status. This defines things as clipping conditions and
3012  * the extents of the clipping region.
3013  *
3014  * Version 2, 3 and 7
3015  *
3016  * Params:
3017  *  ClipStatus:
3018  *
3019  * Returns:
3020  *  D3D_OK because it's a stub
3021  *  (DDERR_INVALIDPARAMS if ClipStatus == NULL)
3022  *
3023  *****************************************************************************/
3024 static HRESULT WINAPI
3025 IDirect3DDeviceImpl_7_SetClipStatus(IDirect3DDevice7 *iface,
3026                                     D3DCLIPSTATUS *ClipStatus)
3027 {
3028     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3029     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3030
3031     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them
3032      * Perhaps this needs a new data type and an additional IWineD3DDevice method
3033      */
3034     /* return IWineD3DDevice_SetClipStatus(This->wineD3DDevice, ClipStatus);*/
3035     return D3D_OK;
3036 }
3037
3038 static HRESULT WINAPI
3039 Thunk_IDirect3DDeviceImpl_3_SetClipStatus(IDirect3DDevice3 *iface,
3040                                           D3DCLIPSTATUS *ClipStatus)
3041 {
3042     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3043     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3044     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3045                                           ClipStatus);
3046 }
3047
3048 static HRESULT WINAPI
3049 Thunk_IDirect3DDeviceImpl_2_SetClipStatus(IDirect3DDevice2 *iface,
3050                                           D3DCLIPSTATUS *ClipStatus)
3051 {
3052     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3053     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3054     return IDirect3DDevice7_SetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3055                                           ClipStatus);
3056 }
3057
3058 /*****************************************************************************
3059  * IDirect3DDevice7::GetClipStatus
3060  *
3061  * Returns the clip status
3062  *
3063  * Params:
3064  *  ClipStatus: Address to write the clip status to
3065  *
3066  * Returns:
3067  *  D3D_OK because it's a stub
3068  *
3069  *****************************************************************************/
3070 static HRESULT WINAPI
3071 IDirect3DDeviceImpl_7_GetClipStatus(IDirect3DDevice7 *iface,
3072                                     D3DCLIPSTATUS *ClipStatus)
3073 {
3074     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3075     FIXME("(%p)->(%p): Stub!\n", This, ClipStatus);
3076
3077     /* D3DCLIPSTATUS and WINED3DCLIPSTATUS are different. I don't know how to convert them */
3078     /* return IWineD3DDevice_GetClipStatus(This->wineD3DDevice, ClipStatus);*/
3079     return D3D_OK;
3080 }
3081
3082 static HRESULT WINAPI
3083 Thunk_IDirect3DDeviceImpl_3_GetClipStatus(IDirect3DDevice3 *iface,
3084                                           D3DCLIPSTATUS *ClipStatus)
3085 {
3086     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3087     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3088     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3089                                           ClipStatus);
3090 }
3091
3092 static HRESULT WINAPI
3093 Thunk_IDirect3DDeviceImpl_2_GetClipStatus(IDirect3DDevice2 *iface,
3094                                           D3DCLIPSTATUS *ClipStatus)
3095 {
3096     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice2, iface);
3097     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, ClipStatus);
3098     return IDirect3DDevice7_GetClipStatus(ICOM_INTERFACE(This, IDirect3DDevice7),
3099                                           ClipStatus);
3100 }
3101
3102 /*****************************************************************************
3103  * IDirect3DDevice::DrawPrimitiveStrided
3104  *
3105  * Draws vertices described by a D3DDRAWPRIMITIVESTRIDEDDATA structure.
3106  *
3107  * Version 3 and 7
3108  *
3109  * Params:
3110  *  PrimitiveType: The primitive type to draw
3111  *  VertexType: The FVF description of the vertices to draw (for the stride??)
3112  *  D3DDrawPrimStrideData: A D3DDRAWPRIMITIVESTRIDEDDATA structure describing
3113  *                         the vertex data locations
3114  *  VertexCount: The number of vertices to draw
3115  *  Flags: Some flags
3116  *
3117  * Returns:
3118  *  D3D_OK, because it's a stub
3119  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3120  *  (For details, see IWineD3DDevice::DrawPrimitiveStrided)
3121  *
3122  *****************************************************************************/
3123 static HRESULT WINAPI
3124 IDirect3DDeviceImpl_7_DrawPrimitiveStrided(IDirect3DDevice7 *iface,
3125                                            D3DPRIMITIVETYPE PrimitiveType,
3126                                            DWORD VertexType,
3127                                            D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3128                                            DWORD VertexCount,
3129                                            DWORD Flags)
3130 {
3131     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3132     WineDirect3DVertexStridedData WineD3DStrided;
3133     int i;
3134     UINT PrimitiveCount;
3135
3136     TRACE("(%p)->(%08x,%08lx,%p,%08lx,%08lx): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3137
3138     /* Get the strided data right. the wined3d structure is a bit bigger
3139      * Watch out: The contents of the strided data are determined by the fvf,
3140      * not by the members set in D3DDrawPrimStrideData. So it's valid
3141      * to have diffuse.lpvData set to 0xdeadbeef if the diffuse flag is
3142      * not set in the fvf.
3143      */
3144     if(VertexType & D3DFVF_POSITION_MASK)
3145     {
3146         memset(&WineD3DStrided, 0, sizeof(WineD3DStrided));
3147         WineD3DStrided.u.s.position.lpData = D3DDrawPrimStrideData->position.lpvData;
3148         WineD3DStrided.u.s.position.dwStride = D3DDrawPrimStrideData->position.dwStride;
3149         WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT3;
3150         if (VertexType & D3DFVF_XYZRHW)
3151         {
3152             WineD3DStrided.u.s.position.dwType = WINED3DDECLTYPE_FLOAT4;
3153             WineD3DStrided.u.s.position_transformed = TRUE;
3154         } else
3155             WineD3DStrided.u.s.position_transformed = FALSE;
3156     }
3157
3158     if(VertexType & D3DFVF_NORMAL)
3159     {
3160         WineD3DStrided.u.s.normal.lpData = D3DDrawPrimStrideData->normal.lpvData;
3161         WineD3DStrided.u.s.normal.dwStride = D3DDrawPrimStrideData->normal.dwStride;
3162         WineD3DStrided.u.s.normal.dwType = WINED3DDECLTYPE_FLOAT3;
3163     }
3164
3165     if(VertexType & D3DFVF_DIFFUSE)
3166     {
3167         WineD3DStrided.u.s.diffuse.lpData = D3DDrawPrimStrideData->diffuse.lpvData;
3168         WineD3DStrided.u.s.diffuse.dwStride = D3DDrawPrimStrideData->diffuse.dwStride;
3169         WineD3DStrided.u.s.diffuse.dwType = WINED3DDECLTYPE_SHORT4;
3170     }
3171
3172     if(VertexType & D3DFVF_SPECULAR)
3173     {
3174         WineD3DStrided.u.s.specular.lpData = D3DDrawPrimStrideData->specular.lpvData;
3175         WineD3DStrided.u.s.specular.dwStride = D3DDrawPrimStrideData->specular.dwStride;
3176         WineD3DStrided.u.s.specular.dwType = WINED3DDECLTYPE_SHORT4;
3177     }
3178
3179     for( i = 0; i < GET_TEXCOUNT_FROM_FVF(VertexType); i++)
3180     {
3181         WineD3DStrided.u.s.texCoords[i].lpData = D3DDrawPrimStrideData->textureCoords[i].lpvData;
3182         WineD3DStrided.u.s.texCoords[i].dwStride = D3DDrawPrimStrideData->textureCoords[i].dwStride;
3183         switch(GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i))
3184         {
3185             case 1: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT1; break;
3186             case 2: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT2; break;
3187             case 3: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT3; break;
3188             case 4: WineD3DStrided.u.s.texCoords[i].dwType = WINED3DDECLTYPE_FLOAT4; break;
3189             default: ERR("Unexpected texture coordinate size %ld\n",
3190                          GET_TEXCOORD_SIZE_FROM_FVF(VertexType, i));
3191         }
3192     }
3193
3194     /* Get the primitive count */
3195     switch(PrimitiveType)
3196     {
3197         case D3DPT_POINTLIST: 
3198           PrimitiveCount = VertexCount;
3199           break;
3200
3201         case D3DPT_LINELIST: 
3202           PrimitiveCount = VertexCount / 2;
3203           break;
3204
3205         case D3DPT_LINESTRIP:
3206           PrimitiveCount = VertexCount - 1;
3207           break;
3208
3209         case D3DPT_TRIANGLELIST:
3210           PrimitiveCount = VertexCount / 3;
3211           break;
3212
3213         case D3DPT_TRIANGLESTRIP:
3214           PrimitiveCount = VertexCount - 2;
3215           break;
3216
3217         case D3DPT_TRIANGLEFAN:
3218           PrimitiveCount = VertexCount - 2;
3219           break;
3220
3221         default: return DDERR_INVALIDPARAMS;
3222     }
3223
3224     IWineD3DDevice_SetFVF(This->wineD3DDevice,
3225                           VertexType);
3226
3227     return IWineD3DDevice_DrawPrimitiveStrided(This->wineD3DDevice,
3228                                                PrimitiveType,
3229                                                PrimitiveCount,
3230                                                &WineD3DStrided);
3231 }
3232
3233 static HRESULT WINAPI
3234 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided(IDirect3DDevice3 *iface,
3235                                                  D3DPRIMITIVETYPE PrimitiveType,
3236                                                  DWORD VertexType,
3237                                                  D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3238                                                  DWORD VertexCount,
3239                                                  DWORD Flags)
3240 {
3241     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3242     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Flags);
3243     return IDirect3DDevice7_DrawPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3244                                                  PrimitiveType,
3245                                                  VertexType,
3246                                                  D3DDrawPrimStrideData,
3247                                                  VertexCount,
3248                                                  Flags);
3249 }
3250
3251 /*****************************************************************************
3252  * IDirect3DDevice7::DrawIndexedPrimitiveStrided
3253  *
3254  * Draws primitives specified by strided data locations based on indices
3255  *
3256  * Version 3 and 7
3257  *
3258  * Params:
3259  *  PrimitiveType:
3260  *
3261  * Returns:
3262  *  D3D_OK, because it's a stub
3263  *  (DDERR_INVALIDPARAMS if D3DDrawPrimStrideData is NULL)
3264  *  (DDERR_INVALIDPARAMS if Indices is NULL)
3265  *  (For more details, see IWineD3DDevice::DrawIndexedPrimitiveStrided)
3266  *
3267  *****************************************************************************/
3268 static HRESULT WINAPI
3269 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided(IDirect3DDevice7 *iface,
3270                                                   D3DPRIMITIVETYPE PrimitiveType,
3271                                                   DWORD VertexType,
3272                                                   D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3273                                                   DWORD VertexCount,
3274                                                   WORD *Indices,
3275                                                   DWORD IndexCount,
3276                                                   DWORD Flags)
3277 {
3278     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3279     FIXME("(%p)->(%08x,%08lx,%p,%08lx,%p,%08lx,%08lx): stub!\n", This, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3280
3281     /* I'll implement it as soon as I find a app to test it.
3282      * This needs an additional method in IWineD3DDevice.
3283      */
3284     return D3D_OK;
3285 }
3286
3287 static HRESULT WINAPI
3288 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided(IDirect3DDevice3 *iface,
3289                                                         D3DPRIMITIVETYPE PrimitiveType,
3290                                                         DWORD VertexType,
3291                                                         D3DDRAWPRIMITIVESTRIDEDDATA *D3DDrawPrimStrideData,
3292                                                         DWORD VertexCount,
3293                                                         WORD *Indices,
3294                                                         DWORD IndexCount,
3295                                                         DWORD Flags)
3296 {
3297     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3298     TRACE_(ddraw_thunk)("(%p)->(%08x,%08lx,%p,%08lx,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", iface, PrimitiveType, VertexType, D3DDrawPrimStrideData, VertexCount, Indices, IndexCount, Flags);
3299     return IDirect3DDevice7_DrawIndexedPrimitiveStrided(ICOM_INTERFACE(This, IDirect3DDevice7),
3300                                                         PrimitiveType,
3301                                                         VertexType,
3302                                                         D3DDrawPrimStrideData,
3303                                                         VertexCount,
3304                                                         Indices,
3305                                                         IndexCount,
3306                                                         Flags);
3307 }
3308
3309 /*****************************************************************************
3310  * IDirect3DDevice7::DrawPrimitiveVB
3311  *
3312  * Draws primitives from a vertex buffer to the screen.
3313  *
3314  * Version 3 and 7
3315  *
3316  * Params:
3317  *  PrimitiveType: Type of primitive to be rendered.
3318  *  D3DVertexBuf: Source Vertex Buffer
3319  *  StartVertex: Index of the first vertex from the buffer to be rendered
3320  *  NumVertices: Number of vertices to be rendered
3321  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3322  *
3323  * Return values
3324  *  D3D_OK on success
3325  *  DDERR_INVALIDPARAMS if D3DVertexBuf is NULL
3326  *
3327  *****************************************************************************/
3328 static HRESULT WINAPI
3329 IDirect3DDeviceImpl_7_DrawPrimitiveVB(IDirect3DDevice7 *iface,
3330                                       D3DPRIMITIVETYPE PrimitiveType,
3331                                       IDirect3DVertexBuffer7 *D3DVertexBuf,
3332                                       DWORD StartVertex,
3333                                       DWORD NumVertices,
3334                                       DWORD Flags)
3335 {
3336     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3337     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3338     UINT PrimitiveCount;
3339     HRESULT hr;
3340     DWORD stride;
3341     WINED3DVERTEXBUFFER_DESC Desc;
3342
3343     TRACE("(%p)->(%08x,%p,%08lx,%08lx,%08lx)\n", This, PrimitiveType, D3DVertexBuf, StartVertex, NumVertices, Flags);
3344
3345     /* Sanity checks */
3346     if(!vb)
3347     {
3348         ERR("(%p) No Vertex buffer specified\n", This);
3349         return DDERR_INVALIDPARAMS;
3350     }
3351
3352     /* Get the primitive count */
3353     switch(PrimitiveType)
3354     {
3355         case D3DPT_POINTLIST: 
3356           PrimitiveCount = NumVertices;
3357           break;
3358
3359         case D3DPT_LINELIST: 
3360           PrimitiveCount = NumVertices / 2;
3361           break;
3362
3363         case D3DPT_LINESTRIP:
3364           PrimitiveCount = NumVertices - 1;
3365           break;
3366
3367         case D3DPT_TRIANGLELIST:
3368           PrimitiveCount = NumVertices / 3;
3369           break;
3370
3371         case D3DPT_TRIANGLESTRIP:
3372           PrimitiveCount = NumVertices - 2;
3373           break;
3374
3375         case D3DPT_TRIANGLEFAN:
3376           PrimitiveCount = NumVertices - 2;
3377           break;
3378
3379         default: return DDERR_INVALIDPARAMS;
3380     }
3381
3382     /* Get the FVF of the vertex buffer, and its stride */
3383     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3384                                       &Desc);
3385     if(hr != D3D_OK)
3386     {
3387         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08lx\n", This, hr);
3388         return hr;
3389     }
3390     stride = get_flexible_vertex_size(Desc.FVF);
3391
3392     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, Desc.FVF);
3393     if(FAILED(hr))
3394     {
3395         ERR(" (%p) Setting the FVF failed, hr = %lx!\n", This, hr);
3396         return hr;
3397     }
3398
3399     /* Set the vertex stream source */
3400     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3401                                         0 /* StreamNumber */,
3402                                         vb->wineD3DVertexBuffer,
3403                                         0 /* StartVertex - we pass this to DrawPrimitive */,
3404                                         stride);
3405     if(hr != D3D_OK)
3406     {
3407         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08lx\n", This, hr);
3408         return hr;
3409     }
3410
3411     /* Now draw the primitives */
3412     return IWineD3DDevice_DrawPrimitive(This->wineD3DDevice,
3413                                         PrimitiveType,
3414                                         StartVertex,
3415                                         PrimitiveCount);
3416 }
3417
3418 static HRESULT WINAPI
3419 Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB(IDirect3DDevice3 *iface,
3420                                             D3DPRIMITIVETYPE PrimitiveType,
3421                                             IDirect3DVertexBuffer *D3DVertexBuf,
3422                                             DWORD StartVertex,
3423                                             DWORD NumVertices,
3424                                             DWORD Flags)
3425 {
3426     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3427     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3428     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%08lx,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This,  PrimitiveType, vb, StartVertex, NumVertices, Flags);
3429     return IDirect3DDevice7_DrawPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3430                                             PrimitiveType,
3431                                             ICOM_INTERFACE(vb, IDirect3DVertexBuffer7),
3432                                             StartVertex,
3433                                             NumVertices,
3434                                             Flags);
3435 }
3436
3437
3438 /*****************************************************************************
3439  * IDirect3DDevice7::DrawIndexedPrimitiveVB
3440  *
3441  * Draws primitives from a vertex buffer to the screen
3442  *
3443  * Params:
3444  *  PrimitiveType: Type of primitive to be rendered.
3445  *  D3DVertexBuf: Source Vertex Buffer
3446  *  StartVertex: Index of the first vertex from the buffer to be rendered
3447  *  NumVertices: Number of vertices to be rendered
3448  *  Indices: Array of DWORDs used to index into the Vertices
3449  *  IndexCount: Number of indices in Indices
3450  *  Flags: Can be D3DDP_WAIT to wait until rendering has finished
3451  *
3452  * Return values
3453  *
3454  *****************************************************************************/
3455 static HRESULT WINAPI
3456 IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB(IDirect3DDevice7 *iface,
3457                                              D3DPRIMITIVETYPE PrimitiveType,
3458                                              IDirect3DVertexBuffer7 *D3DVertexBuf,
3459                                              DWORD StartVertex,
3460                                              DWORD NumVertices,
3461                                              WORD *Indices,
3462                                              DWORD IndexCount,
3463                                              DWORD Flags)
3464 {
3465     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3466     IDirect3DVertexBufferImpl *vb = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, D3DVertexBuf);
3467     DWORD stride;
3468     UINT PrimitiveCount;
3469     WORD *LockedIndices;
3470     HRESULT hr;
3471     WINED3DVERTEXBUFFER_DESC Desc;
3472
3473     TRACE("(%p)->(%08x,%p,%ld,%ld,%p,%ld,%08lx)\n", This, PrimitiveType, vb, StartVertex, NumVertices, Indices, IndexCount, Flags);
3474
3475     /* Steps:
3476      * 1) Calculate some things: Vertex count -> Primitive count, stride, ...
3477      * 2) Upload the Indices to the index buffer
3478      * 3) Set the index source
3479      * 4) Set the Vertex Buffer as the Stream source
3480      * 5) Call IWineD3DDevice::DrawIndexedPrimitive
3481      */
3482
3483     /* Get the primitive count */
3484     switch(PrimitiveType)
3485     {
3486         case D3DPT_POINTLIST: 
3487           PrimitiveCount = IndexCount;
3488           break;
3489
3490         case D3DPT_LINELIST: 
3491           PrimitiveCount = IndexCount / 2;
3492           break;
3493
3494         case D3DPT_LINESTRIP:
3495           PrimitiveCount = IndexCount - 1;
3496           break;
3497
3498         case D3DPT_TRIANGLELIST:
3499           PrimitiveCount = IndexCount / 3;
3500           break;
3501
3502         case D3DPT_TRIANGLESTRIP:
3503           PrimitiveCount = IndexCount - 2;
3504           break;
3505
3506         case D3DPT_TRIANGLEFAN:
3507           PrimitiveCount = IndexCount - 2;
3508           break;
3509
3510         default: return DDERR_INVALIDPARAMS;
3511     }
3512
3513     /* Get the FVF of the vertex buffer, and its stride */
3514     hr = IWineD3DVertexBuffer_GetDesc(vb->wineD3DVertexBuffer,
3515                                       &Desc);
3516     if(hr != D3D_OK)
3517     {
3518         ERR("(%p) IWineD3DVertexBuffer::GetDesc failed with hr = %08lx\n", This, hr);
3519         return hr;
3520     }
3521     stride = get_flexible_vertex_size(Desc.FVF);
3522     TRACE("Vertex buffer FVF = %08lx, stride=%ld\n", Desc.FVF, stride);
3523
3524     hr = IWineD3DDevice_SetFVF(This->wineD3DDevice, Desc.FVF);
3525     if(FAILED(hr))
3526     {
3527         ERR(" (%p) Setting the FVF failed, hr = %lx!\n", This, hr);
3528         return hr;
3529     }
3530
3531     /* copy the index stream into the index buffer.
3532      * A new IWineD3DDevice method could be created
3533      * which takes an user pointer containing the indices
3534      * or a SetData-Method for the index buffer, which
3535      * overrides the index buffer data with our pointer.
3536      */
3537     hr = IWineD3DIndexBuffer_Lock(This->indexbuffer,
3538                                   0 /* OffSetToLock */,
3539                                   0 /* SizeToLock - doesn't matter */,
3540                                   (BYTE **) &LockedIndices,
3541                                   0 /* Flags */);
3542     assert(IndexCount < 0x100000);
3543     if(hr != D3D_OK)
3544     {
3545         ERR("(%p) IWineD3DIndexBuffer::Lock failed with hr = %08lx\n", This, hr);
3546         return hr;
3547     }
3548     memcpy(LockedIndices, Indices, IndexCount * sizeof(WORD));
3549     hr = IWineD3DIndexBuffer_Unlock(This->indexbuffer);
3550     if(hr != D3D_OK)
3551     {
3552         ERR("(%p) IWineD3DIndexBuffer::Unlock failed with hr = %08lx\n", This, hr);
3553         return hr;
3554     }
3555
3556     /* Set the index stream */
3557     hr = IWineD3DDevice_SetIndices(This->wineD3DDevice,
3558                                    This->indexbuffer,
3559                                    0);
3560
3561     /* Set the vertex stream source */
3562     hr = IWineD3DDevice_SetStreamSource(This->wineD3DDevice,
3563                                         0 /* StreamNumber */,
3564                                         vb->wineD3DVertexBuffer,
3565                                         0 /* offset, we pass this to DrawIndexedPrimitive */,
3566                                         stride);
3567     if(hr != D3D_OK)
3568     {
3569         ERR("(%p) IDirect3DDevice::SetStreamSource failed with hr = %08lx\n", This, hr);
3570         return hr;
3571     }
3572
3573
3574     hr = IWineD3DDevice_DrawIndexedPrimitive(This->wineD3DDevice,
3575                                              PrimitiveType,
3576                                              StartVertex,
3577                                              0 /* minIndex */,
3578                                              NumVertices,
3579                                              0 /* StartIndex */,
3580                                              PrimitiveCount);
3581
3582     return D3D_OK;
3583 }
3584
3585 static HRESULT WINAPI
3586 Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB(IDirect3DDevice3 *iface,
3587                                                    D3DPRIMITIVETYPE PrimitiveType,
3588                                                    IDirect3DVertexBuffer *D3DVertexBuf,
3589                                                    WORD *Indices,
3590                                                    DWORD IndexCount,
3591                                                    DWORD Flags)
3592 {
3593     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3594     IDirect3DVertexBufferImpl *VB = ICOM_OBJECT(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer, D3DVertexBuf);
3595     TRACE_(ddraw_thunk)("(%p)->(%08x,%p,%p,%08lx,%08lx) thunking to IDirect3DDevice7 interface.\n", This, PrimitiveType, VB, Indices, IndexCount, Flags);
3596
3597     return IDirect3DDevice7_DrawIndexedPrimitiveVB(ICOM_INTERFACE(This, IDirect3DDevice7),
3598                                                    PrimitiveType,
3599                                                    ICOM_INTERFACE(VB, IDirect3DVertexBuffer7),
3600                                                    0,
3601                                                    IndexCount,
3602                                                    Indices,
3603                                                    IndexCount,
3604                                                    Flags);
3605 }
3606
3607 /*****************************************************************************
3608  * IDirect3DDevice7::ComputeSphereVisibility
3609  *
3610  * Calculates the visibility of spheres in the current viewport. The spheres
3611  * are passed in the Centers and Radii arrays, the results are passed back
3612  * in the ReturnValues array. Return values are either completely visible,
3613  * partially visible or completely invisible.
3614  * The return value consist of a combination of D3DCLIP_* flags, or it's
3615  * 0 if the sphere is completely visible(according to the SDK, not checked)
3616  *
3617  * Sounds like an overdose of math ;)
3618  *
3619  * Version 3 and 7
3620  *
3621  * Params:
3622  *  Centers: Array containing the sphere centers
3623  *  Radii: Array containing the sphere radii
3624  *  NumSpheres: The number of centers and radii in the arrays
3625  *  Flags: Some flags
3626  *  ReturnValues: Array to write the results to
3627  *
3628  * Returns:
3629  *  D3D_OK because it's a stub
3630  *  (DDERR_INVALIDPARAMS if Centers, Radii or ReturnValues are NULL)
3631  *  (D3DERR_INVALIDMATRIX if the combined world, view and proj matrix
3632  *  is singular)
3633  *
3634  *****************************************************************************/
3635 static HRESULT WINAPI
3636 IDirect3DDeviceImpl_7_ComputeSphereVisibility(IDirect3DDevice7 *iface,
3637                                               D3DVECTOR *Centers,
3638                                               D3DVALUE *Radii,
3639                                               DWORD NumSpheres,
3640                                               DWORD Flags,
3641                                               DWORD *ReturnValues)
3642 {
3643     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3644     FIXME("(%p)->(%p,%p,%08lx,%08lx,%p): stub!\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3645
3646     /* the DirectX 7 sdk says that the visibility is computed by
3647      * back-transforming the viewing frustum to model space
3648      * using the inverse of the combined world, view and projection
3649      * matrix. If the matrix can't be reversed, D3DERR_INVALIDMATRIX
3650      * is returned.
3651      *
3652      * Basic implementation idea:
3653      * 1) Check if the center is in the viewing frustum
3654      * 2) Cut the sphere with the planes of the viewing
3655      *    frustum
3656      *
3657      * ->Center inside the frustum, no intersections:
3658      *    Fully visible
3659      * ->Center outside the frustum, no intersections:
3660      *    Not visible
3661      * ->Some intersections: Partially visible
3662      *
3663      * Implement this call in WineD3D. Either implement the
3664      * matrix and vector stuff in WineD3D, or use some external
3665      * math library.
3666      */
3667
3668     return D3D_OK;
3669 }
3670
3671 static HRESULT WINAPI
3672 Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility(IDirect3DDevice3 *iface,
3673                                                     D3DVECTOR *Centers,
3674                                                     D3DVALUE *Radii,
3675                                                     DWORD NumSpheres,
3676                                                     DWORD Flags,
3677                                                     DWORD *ReturnValues)
3678 {
3679     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3680     TRACE_(ddraw_thunk)("(%p)->(%p,%p,%08lx,%08lx,%p) thunking to IDirect3DDevice7 interface.\n", This, Centers, Radii, NumSpheres, Flags, ReturnValues);
3681     return IDirect3DDevice7_ComputeSphereVisibility(ICOM_INTERFACE(This, IDirect3DDevice7),
3682                                                     Centers,
3683                                                     Radii,
3684                                                     NumSpheres,
3685                                                     Flags,
3686                                                     ReturnValues);
3687 }
3688
3689 /*****************************************************************************
3690  * IDirect3DDevice7::GetTexture
3691  *
3692  * Returns the texture interface handle assigned to a texture stage.
3693  * The returned texture is AddRefed. This is taken from old ddraw,
3694  * not checked in Windows.
3695  *
3696  * Version 3 and 7
3697  *
3698  * Params:
3699  *  Stage: Texture stage to read the texture from
3700  *  Texture: Address to store the interface pointer at
3701  *
3702  * Returns:
3703  *  D3D_OK on success
3704  *  DDERR_INVALIDPARAMS if Texture is NULL
3705  *  For details, see IWineD3DDevice::GetTexture
3706  *
3707  *****************************************************************************/
3708 static HRESULT WINAPI
3709 IDirect3DDeviceImpl_7_GetTexture(IDirect3DDevice7 *iface,
3710                                  DWORD Stage,
3711                                  IDirectDrawSurface7 **Texture)
3712 {
3713     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3714     IWineD3DBaseTexture *Surf;
3715     HRESULT hr;
3716     TRACE("(%p)->(%ld,%p): Relay\n", This, Stage, Texture);
3717
3718     if(!Texture)
3719     {
3720         TRACE("Texture == NULL, failing with DDERR_INVALIDPARAMS\n");
3721         return DDERR_INVALIDPARAMS;
3722     }
3723
3724     hr = IWineD3DDevice_GetTexture(This->wineD3DDevice, Stage, (IWineD3DBaseTexture **) &Surf);
3725     if( (hr != D3D_OK) || (!Surf) ) 
3726     {
3727         *Texture = NULL;
3728         return hr;
3729     }
3730
3731     /* GetParent AddRef()s, which is perfectly OK.
3732      * We have passed the IDirectDrawSurface7 interface to WineD3D, so that's OK too.
3733      */
3734     return IWineD3DBaseTexture_GetParent(Surf,
3735                                          (IUnknown **) Texture);
3736 }
3737
3738 static HRESULT WINAPI
3739 Thunk_IDirect3DDeviceImpl_3_GetTexture(IDirect3DDevice3 *iface,
3740                                        DWORD Stage,
3741                                        IDirect3DTexture2 **Texture2)
3742 {
3743     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3744     HRESULT ret;
3745     IDirectDrawSurface7 *ret_val;
3746
3747     TRACE_(ddraw_thunk)("(%p)->(%ld,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, Texture2);
3748     ret = IDirect3DDevice7_GetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3749                                       Stage,
3750                                       &ret_val);
3751
3752     *Texture2 = COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface7, IDirect3DTexture2, ret_val);
3753
3754     TRACE_(ddraw_thunk)(" returning interface %p.\n", *Texture2);
3755
3756     return ret;
3757 }
3758
3759 /*****************************************************************************
3760  * IDirect3DDevice7::SetTexture
3761  *
3762  * Assigns a texture to a texture stage. Is the texture AddRef-ed?
3763  *
3764  * Version 3 and 7
3765  *
3766  * Params:
3767  *  Stage: The stage to assign the texture to
3768  *  Texture: Interface pointer to the texture surface
3769  *
3770  * Returns
3771  * D3D_OK on success
3772  * For details, see IWineD3DDevice::SetTexture
3773  *
3774  *****************************************************************************/
3775 static HRESULT WINAPI
3776 IDirect3DDeviceImpl_7_SetTexture(IDirect3DDevice7 *iface,
3777                                  DWORD Stage,
3778                                  IDirectDrawSurface7 *Texture)
3779 {
3780     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3781     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
3782     TRACE("(%p)->(%08lx,%p): Relay!\n", This, Stage, surf);
3783
3784     /* Texture may be NULL here */
3785     return IWineD3DDevice_SetTexture(This->wineD3DDevice,
3786                                      Stage,
3787                                      surf ? (IWineD3DBaseTexture * ) surf->wineD3DTexture : NULL);
3788 }
3789
3790 static HRESULT WINAPI
3791 Thunk_IDirect3DDeviceImpl_3_SetTexture(IDirect3DDevice3 *iface,
3792                                        DWORD Stage,
3793                                        IDirect3DTexture2 *Texture2)
3794 {
3795     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3796     IDirectDrawSurfaceImpl *tex = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirect3DTexture2, Texture2);
3797     TRACE_(ddraw_thunk)("(%p)->(%ld,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, tex);
3798     return IDirect3DDevice7_SetTexture(ICOM_INTERFACE(This, IDirect3DDevice7),
3799                                        Stage,
3800                                        ICOM_INTERFACE(tex, IDirectDrawSurface7));
3801 }
3802
3803 /*****************************************************************************
3804  * IDirect3DDevice7::GetTextureStageState
3805  *
3806  * Retrieves a state from a texture stage.
3807  *
3808  * Version 3 and 7
3809  *
3810  * Params:
3811  *  Stage: The stage to retrieve the state from
3812  *  TexStageStateType: The state type to retrieve
3813  *  State: Address to store the state's value at
3814  *
3815  * Returns:
3816  *  D3D_OK on success
3817  *  DDERR_INVALIDPARAMS if State is NULL
3818  *  For details, see IWineD3DDevice::GetTextureStageState
3819  *
3820  *****************************************************************************/
3821 static HRESULT WINAPI
3822 IDirect3DDeviceImpl_7_GetTextureStageState(IDirect3DDevice7 *iface,
3823                                            DWORD Stage,
3824                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
3825                                            DWORD *State)
3826 {
3827     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3828     TRACE("(%p)->(%08lx,%08x,%p): Relay!\n", This, Stage, TexStageStateType, State);
3829
3830     if(!State)
3831         return DDERR_INVALIDPARAMS;
3832
3833     return IWineD3DDevice_GetTextureStageState(This->wineD3DDevice,
3834                                                Stage,
3835                                                TexStageStateType,
3836                                                State);
3837 }
3838
3839 static HRESULT WINAPI
3840 Thunk_IDirect3DDeviceImpl_3_GetTextureStageState(IDirect3DDevice3 *iface,
3841                                                  DWORD Stage,
3842                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
3843                                                  DWORD *State)
3844 {
3845     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3846     TRACE_(ddraw_thunk)("(%p)->(%08lx,%08x,%p) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
3847     return IDirect3DDevice7_GetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
3848                                                  Stage,
3849                                                  TexStageStateType,
3850                                                  State);
3851 }
3852
3853 /*****************************************************************************
3854  * IDirect3DDevice7::SetTextureStageState
3855  *
3856  * Sets a texture stage state. Some stage types need to be handled specially,
3857  * because they do not exist in WineD3D and were moved to another place
3858  *
3859  * Version 3 and 7
3860  *
3861  * Params:
3862  *  Stage: The stage to modify
3863  *  TexStageStateType: The state to change
3864  *  State: The new value for the state
3865  *
3866  * Returns:
3867  *  D3D_OK on success
3868  *  For details, see IWineD3DDevice::SetTextureStageState
3869  *
3870  *****************************************************************************/
3871 static HRESULT WINAPI
3872 IDirect3DDeviceImpl_7_SetTextureStageState(IDirect3DDevice7 *iface,
3873                                            DWORD Stage,
3874                                            D3DTEXTURESTAGESTATETYPE TexStageStateType,
3875                                            DWORD State)
3876 {
3877     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3878     TRACE("(%p)->(%08lx,%08x,%08lx): Relay!\n", This, Stage, TexStageStateType, State);
3879     switch(TexStageStateType)
3880     {
3881         /* Mipfilter is a sampler state with different values */
3882         case D3DTSS_MIPFILTER:
3883         {
3884             WINED3DTEXTUREFILTERTYPE value;
3885             switch(State)
3886             {
3887                 case D3DTFP_NONE: value = WINED3DTEXF_NONE; break;
3888                 case D3DTFP_POINT: value = WINED3DTEXF_POINT; break;
3889                 case 0: /* Unchecked */
3890                 case D3DTFP_LINEAR: value = WINED3DTEXF_LINEAR; break;
3891                 default:
3892                     ERR("Unexpected mipfilter value %ld\n", State);
3893                     value = WINED3DTEXF_NONE;
3894             }
3895             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
3896                                                   Stage,
3897                                                   WINED3DSAMP_MIPFILTER,
3898                                                   value);
3899         }
3900
3901         /* Minfilter is a sampler state too, equal values */
3902         case D3DTSS_MINFILTER:
3903             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
3904                                                   Stage,
3905                                                   WINED3DSAMP_MINFILTER,
3906                                                   State);
3907         /* Same for MAGFILTER */
3908         case D3DTSS_MAGFILTER:
3909             return IWineD3DDevice_SetSamplerState(This->wineD3DDevice,
3910                                                   Stage,
3911                                                   WINED3DSAMP_MAGFILTER,
3912                                                   State);
3913
3914         default:
3915
3916             return IWineD3DDevice_SetTextureStageState(This->wineD3DDevice,
3917                                                       Stage,
3918                                                       TexStageStateType,
3919                                                       State);
3920     }
3921 }
3922
3923 static HRESULT WINAPI
3924 Thunk_IDirect3DDeviceImpl_3_SetTextureStageState(IDirect3DDevice3 *iface,
3925                                                  DWORD Stage,
3926                                                  D3DTEXTURESTAGESTATETYPE TexStageStateType,
3927                                                  DWORD State)
3928 {
3929     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3930     TRACE_(ddraw_thunk)("(%p)->(%08lx,%08x,%08lx) thunking to IDirect3DDevice7 interface.\n", This, Stage, TexStageStateType, State);
3931     return IDirect3DDevice7_SetTextureStageState(ICOM_INTERFACE(This, IDirect3DDevice7),
3932                                                  Stage,
3933                                                  TexStageStateType,
3934                                                  State);
3935 }
3936
3937 /*****************************************************************************
3938  * IDirect3DDevice7::ValidateDevice
3939  *
3940  * SDK: "Reports the device's ability to render the currently set
3941  * texture-blending operations in a single pass". Whatever that means
3942  * exactly...
3943  *
3944  * Version 3 and 7
3945  *
3946  * Params:
3947  *  NumPasses: Address to write the number of necessary passes for the
3948  *             desired effect to.
3949  *
3950  * Returns:
3951  *  D3D_OK on success
3952  *  See IWineD3DDevice::ValidateDevice for more details
3953  *
3954  *****************************************************************************/
3955 static HRESULT WINAPI
3956 IDirect3DDeviceImpl_7_ValidateDevice(IDirect3DDevice7 *iface,
3957                                      DWORD *NumPasses)
3958 {
3959     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
3960     TRACE("(%p)->(%p): Relay\n", This, NumPasses);
3961
3962     return IWineD3DDevice_ValidateDevice(This->wineD3DDevice, NumPasses);
3963 }
3964
3965 static HRESULT WINAPI
3966 Thunk_IDirect3DDeviceImpl_3_ValidateDevice(IDirect3DDevice3 *iface,
3967                                            DWORD *Passes)
3968 {
3969     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice3, iface);
3970     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DDevice7 interface.\n", This, Passes);
3971     return IDirect3DDevice7_ValidateDevice(ICOM_INTERFACE(This, IDirect3DDevice7),
3972                                            Passes);
3973 }
3974
3975 /*****************************************************************************
3976  * IDirect3DDevice7::Clear
3977  *
3978  * Fills the render target, the z buffer and the stencil buffer with a
3979  * clear color / value
3980  *
3981  * Version 7 only
3982  *
3983  * Params:
3984  *  Count: Number of rectangles in Rects must be 0 if Rects is NULL
3985  *  Rects: Rectangles to clear. If NULL, the whole surface is cleared
3986  *  Flags: Some flags, as usual
3987  *  Color: Clear color for the render target
3988  *  Z: Clear value for the Z buffer
3989  *  Stencil: Clear value to store in each stencil buffer entry
3990  *
3991  * Returns:
3992  *  D3D_OK on success
3993  *  For details, see IWineD3DDevice::Clear
3994  *
3995  *****************************************************************************/
3996 static HRESULT WINAPI
3997 IDirect3DDeviceImpl_7_Clear(IDirect3DDevice7 *iface,
3998                             DWORD Count,
3999                             D3DRECT *Rects,
4000                             DWORD Flags,
4001                             D3DCOLOR Color,
4002                             D3DVALUE Z,
4003                             DWORD Stencil)
4004 {
4005     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4006     TRACE("(%p)->(%08lx,%p,%08lx,%08lx,%f,%08lx): Relay\n", This, Count, Rects, Flags, (DWORD) Color, Z, Stencil);
4007
4008     return IWineD3DDevice_Clear(This->wineD3DDevice, Count, Rects, Flags, Color, Z, Stencil);
4009 }
4010
4011 /*****************************************************************************
4012  * IDirect3DDevice7::SetViewport
4013  *
4014  * Sets the current viewport.
4015  *
4016  * Version 7 only, but IDirect3DViewport uses this call for older
4017  * versions
4018  *
4019  * Params:
4020  *  Data: The new viewport to set
4021  *
4022  * Returns:
4023  *  D3D_OK on success
4024  *  DDERR_INVALIDPARAMS if Data is NULL
4025  *  For more details, see IWineDDDevice::SetViewport
4026  *
4027  *****************************************************************************/
4028 static HRESULT WINAPI
4029 IDirect3DDeviceImpl_7_SetViewport(IDirect3DDevice7 *iface,
4030                                   D3DVIEWPORT7 *Data)
4031 {
4032     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4033     TRACE("(%p)->(%p) Relay!\n", This, Data);
4034
4035     if(!Data)
4036         return DDERR_INVALIDPARAMS;
4037
4038     return IWineD3DDevice_SetViewport(This->wineD3DDevice,
4039                                       Data);
4040 }
4041
4042 /*****************************************************************************
4043  * IDirect3DDevice::GetViewport
4044  *
4045  * Returns the current viewport
4046  *
4047  * Version 7
4048  *
4049  * Params:
4050  *  Data: D3D7Viewport structure to write the viewport information to
4051  *
4052  * Returns:
4053  *  D3D_OK on success
4054  *  DDERR_INVALIDPARAMS if Data is NULL
4055  *  For more details, see IWineD3DDevice::GetViewport
4056  *
4057  *****************************************************************************/
4058 static HRESULT WINAPI
4059 IDirect3DDeviceImpl_7_GetViewport(IDirect3DDevice7 *iface,
4060                                   D3DVIEWPORT7 *Data)
4061 {
4062     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4063     HRESULT hr;
4064     TRACE("(%p)->(%p) Relay!\n", This, Data);
4065
4066     if(!Data)
4067         return DDERR_INVALIDPARAMS;
4068
4069     hr = IWineD3DDevice_GetViewport(This->wineD3DDevice,
4070                                     Data);
4071     return hr_ddraw_from_wined3d(hr);
4072 }
4073
4074 /*****************************************************************************
4075  * IDirect3DDevice7::SetMaterial
4076  *
4077  * Sets the Material
4078  *
4079  * Version 7
4080  *
4081  * Params:
4082  *  Mat: The material to set
4083  *
4084  * Returns:
4085  *  D3D_OK on success
4086  *  DDERR_INVALIDPARAMS if Mat is NULL.
4087  *  For more details, see IWineD3DDevice::SetMaterial
4088  *
4089  *****************************************************************************/
4090 static HRESULT WINAPI
4091 IDirect3DDeviceImpl_7_SetMaterial(IDirect3DDevice7 *iface,
4092                                   D3DMATERIAL7 *Mat)
4093 {
4094     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4095     HRESULT hr;
4096     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4097
4098     hr = IWineD3DDevice_SetMaterial(This->wineD3DDevice,
4099                                     Mat);
4100     return hr_ddraw_from_wined3d(hr);
4101 }
4102
4103 /*****************************************************************************
4104  * IDirect3DDevice7::GetMaterial
4105  *
4106  * Returns the current material
4107  *
4108  * Version 7
4109  *
4110  * Params:
4111  *  Mat: D3DMATERIAL7 structure to write the material parameters to
4112  *
4113  * Returns:
4114  *  D3D_OK on success
4115  *  DDERR_INVALIDPARAMS if Mat is NULL
4116  *  For more details, see IWineD3DDevice::GetMaterial
4117  *
4118  *****************************************************************************/
4119 static HRESULT WINAPI
4120 IDirect3DDeviceImpl_7_GetMaterial(IDirect3DDevice7 *iface,
4121                                   D3DMATERIAL7 *Mat)
4122 {
4123     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4124     HRESULT hr;
4125     TRACE("(%p)->(%p): Relay!\n", This, Mat);
4126
4127     hr = IWineD3DDevice_GetMaterial(This->wineD3DDevice,
4128                                     Mat);
4129     return hr_ddraw_from_wined3d(hr);
4130 }
4131
4132 /*****************************************************************************
4133  * IDirect3DDevice7::SetLight
4134  *
4135  * Assigns a light to a light index, but doesn't activate it yet.
4136  *
4137  * Version 7, IDirect3DLight uses this method for older versions
4138  *
4139  * Params:
4140  *  LightIndex: The index of the new light
4141  *  Light: A D3DLIGHT7 structure describing the light
4142  *
4143  * Returns:
4144  *  D3D_OK on success
4145  *  For more details, see IWineD3DDevice::SetLight
4146  *
4147  *****************************************************************************/
4148 static HRESULT WINAPI
4149 IDirect3DDeviceImpl_7_SetLight(IDirect3DDevice7 *iface,
4150                                DWORD LightIndex,
4151                                D3DLIGHT7 *Light)
4152 {
4153     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4154     HRESULT hr;
4155     TRACE("(%p)->(%08lx,%p): Relay!\n", This, LightIndex, Light);
4156
4157     hr = IWineD3DDevice_SetLight(This->wineD3DDevice,
4158                                  LightIndex,
4159                                  Light);
4160     return hr_ddraw_from_wined3d(hr);
4161 }
4162
4163 /*****************************************************************************
4164  * IDirect3DDevice7::GetLight
4165  *
4166  * Returns the light assigned to a light index
4167  *
4168  * Params:
4169  *  Light: Structure to write the light information to
4170  *
4171  * Returns:
4172  *  D3D_OK on success
4173  *  DDERR_INVALIDPARAMS if Light is NULL
4174  *  For details, see IWineD3DDevice::GetLight
4175  *
4176  *****************************************************************************/
4177 static HRESULT WINAPI
4178 IDirect3DDeviceImpl_7_GetLight(IDirect3DDevice7 *iface,
4179                                DWORD LightIndex,
4180                                D3DLIGHT7 *Light)
4181 {
4182     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4183     HRESULT rc;
4184     TRACE("(%p)->(%08lx,%p): Relay!\n", This, LightIndex, Light);
4185
4186     rc =  IWineD3DDevice_GetLight(This->wineD3DDevice,
4187                                   LightIndex,
4188                                   Light);
4189
4190     /* Translate the result. WineD3D returns other values than D3D7 */
4191     return hr_ddraw_from_wined3d(rc);
4192 }
4193
4194 /*****************************************************************************
4195  * IDirect3DDevice7::BeginStateBlock
4196  *
4197  * Begins recording to a stateblock
4198  *
4199  * Version 7
4200  *
4201  * Returns:
4202  *  D3D_OK on success
4203  *  For details see IWineD3DDevice::BeginStateBlock
4204  *
4205  *****************************************************************************/
4206 static HRESULT WINAPI
4207 IDirect3DDeviceImpl_7_BeginStateBlock(IDirect3DDevice7 *iface)
4208 {
4209     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4210     HRESULT hr;
4211     TRACE("(%p)->(): Relay!\n", This);
4212
4213     hr = IWineD3DDevice_BeginStateBlock(This->wineD3DDevice);
4214     return hr_ddraw_from_wined3d(hr);
4215 }
4216
4217 /*****************************************************************************
4218  * IDirect3DDevice7::EndStateBlock
4219  *
4220  * Stops recording to a state block and returns the created stateblock
4221  * handle. The d3d7 stateblock handles are the interface pointers of the
4222  * IWineD3DStateBlock interface
4223  *
4224  * Version 7
4225  *
4226  * Params:
4227  *  BlockHandle: Address to store the stateblock's handle to
4228  *
4229  * Returns:
4230  *  D3D_OK on success
4231  *  DDERR_INVALIDPARAMS if BlockHandle is NULL
4232  *  See IWineD3DDevice::EndStateBlock for more details
4233  *
4234  *****************************************************************************/
4235 static HRESULT WINAPI
4236 IDirect3DDeviceImpl_7_EndStateBlock(IDirect3DDevice7 *iface,
4237                                     DWORD *BlockHandle)
4238 {
4239     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4240     HRESULT hr;
4241     TRACE("(%p)->(%p): Relay!\n", This, BlockHandle);
4242
4243     if(!BlockHandle)
4244         return DDERR_INVALIDPARAMS;
4245
4246     hr = IWineD3DDevice_EndStateBlock(This->wineD3DDevice,
4247                                       (IWineD3DStateBlock **) BlockHandle);
4248     return hr_ddraw_from_wined3d(hr);
4249 }
4250
4251 /*****************************************************************************
4252  * IDirect3DDevice7::PreLoad
4253  *
4254  * Allows the app to signal that a texture will be used soon, to allow
4255  * the Direct3DDevice to load it to the video card in the meantime.
4256  *
4257  * Version 7
4258  *
4259  * Params:
4260  *  Texture: The texture to preload
4261  *
4262  * Returns:
4263  *  D3D_OK on success
4264  *  DDERR_INVALIDPARAMS if Texture is NULL
4265  *  See IWineD3DSurface::PreLoad for details
4266  *
4267  *****************************************************************************/
4268 static HRESULT WINAPI
4269 IDirect3DDeviceImpl_7_PreLoad(IDirect3DDevice7 *iface,
4270                               IDirectDrawSurface7 *Texture)
4271 {
4272     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4273     IDirectDrawSurfaceImpl *surf = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Texture);
4274
4275     TRACE("(%p)->(%p): Relay!\n", This, surf);
4276
4277     if(!Texture)
4278         return DDERR_INVALIDPARAMS;
4279
4280     IWineD3DSurface_PreLoad(surf->WineD3DSurface);
4281     return D3D_OK;
4282 }
4283
4284 /*****************************************************************************
4285  * IDirect3DDevice7::ApplyStateBlock
4286  *
4287  * Activates the state stored in a state block handle.
4288  *
4289  * Params:
4290  *  BlockHandle: The stateblock handle to activate
4291  *
4292  * Returns:
4293  *  D3D_OK on success
4294  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4295  *
4296  *****************************************************************************/
4297 static HRESULT WINAPI
4298 IDirect3DDeviceImpl_7_ApplyStateBlock(IDirect3DDevice7 *iface,
4299                                       DWORD BlockHandle)
4300 {
4301     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4302     HRESULT hr;
4303     TRACE("(%p)->(%08lx): Relay!\n", This, BlockHandle);
4304
4305     if(!BlockHandle)
4306         return D3DERR_INVALIDSTATEBLOCK;
4307
4308     hr = IWineD3DStateBlock_Apply((IWineD3DStateBlock *) BlockHandle);
4309     return hr_ddraw_from_wined3d(hr);
4310 }
4311
4312 /*****************************************************************************
4313  * IDirect3DDevice7::CaptureStateBlock
4314  *
4315  * Updates a stateblock's values to the values currently set for the device
4316  *
4317  * Version 7
4318  *
4319  * Params:
4320  *  BlockHandle: Stateblock to update
4321  *
4322  * Returns:
4323  *  D3D_OK on success
4324  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is NULL
4325  *  See IWineD3DDevice::CaptureStateBlock for more details
4326  *
4327  *****************************************************************************/
4328 static HRESULT WINAPI
4329 IDirect3DDeviceImpl_7_CaptureStateBlock(IDirect3DDevice7 *iface,
4330                                         DWORD BlockHandle)
4331 {
4332     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4333     HRESULT hr;
4334     TRACE("(%p)->(%08lx): Relay!\n", This, BlockHandle);
4335
4336     if(BlockHandle == 0)
4337         return D3DERR_INVALIDSTATEBLOCK;
4338
4339     hr = IWineD3DStateBlock_Capture((IWineD3DStateBlock *) BlockHandle);
4340     return hr_ddraw_from_wined3d(hr);
4341 }
4342
4343 /*****************************************************************************
4344  * IDirect3DDevice7::DeleteStateBlock
4345  *
4346  * Deletes a stateblock handle. This means releasing the WineD3DStateBlock
4347  *
4348  * Version 7
4349  *
4350  * Params:
4351  *  BlockHandle: Stateblock handle to delete
4352  *
4353  * Returns:
4354  *  D3D_OK on success
4355  *  D3DERR_INVALIDSTATEBLOCK if BlockHandle is 0
4356  *
4357  *****************************************************************************/
4358 static HRESULT WINAPI
4359 IDirect3DDeviceImpl_7_DeleteStateBlock(IDirect3DDevice7 *iface,
4360                                        DWORD BlockHandle)
4361 {
4362     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4363     TRACE("(%p)->(%08lx): Relay!\n", This, BlockHandle);
4364
4365     if(BlockHandle == 0)
4366         return D3DERR_INVALIDSTATEBLOCK;
4367
4368     IWineD3DStateBlock_Release((IWineD3DStateBlock *) BlockHandle);
4369
4370     return D3D_OK;
4371 }
4372
4373 /*****************************************************************************
4374  * IDirect3DDevice7::CreateStateBlock
4375  *
4376  * Creates a new state block handle.
4377  *
4378  * Version 7
4379  *
4380  * Params:
4381  *  Type: The state block type
4382  *  BlockHandle: Address to write the created handle to
4383  *
4384  * Returns:
4385  *   D3D_OK on success
4386  *   DDERR_INVALIDPARAMS if BlockHandle is NULL
4387  *
4388  *****************************************************************************/
4389 static HRESULT WINAPI
4390 IDirect3DDeviceImpl_7_CreateStateBlock(IDirect3DDevice7 *iface,
4391                                        D3DSTATEBLOCKTYPE Type,
4392                                        DWORD *BlockHandle)
4393 {
4394     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4395     HRESULT hr;
4396     TRACE("(%p)->(%08x,%p)!\n", This, Type, BlockHandle);
4397
4398     if(!BlockHandle)
4399         return DDERR_INVALIDPARAMS;
4400
4401     /* The D3DSTATEBLOCKTYPE enum is fine here */
4402     hr = IWineD3DDevice_CreateStateBlock(This->wineD3DDevice,
4403                                          Type,
4404                                          (IWineD3DStateBlock **) BlockHandle,
4405                                          NULL /* Parent, hope that works */);
4406     return hr_ddraw_from_wined3d(hr);
4407 }
4408
4409 /*****************************************************************************
4410  * IDirect3DDevice7::Load
4411  *
4412  * Loads a rectangular area from the source into the destination texture.
4413  * It can also copy the source to the faces of a cubic environment map
4414  *
4415  * Version 7
4416  *
4417  * Params:
4418  *  DestTex: Destination texture
4419  *  DestPoint: Point in the destination where the source image should be
4420  *             written to
4421  *  SrcTex: Source texture
4422  *  SrcRect: Source rectangle
4423  *  Flags: Some flags
4424  *
4425  * Returns:
4426  *  D3D_OK on success
4427  *  DDERR_INVALIDPARAMS if DestTex or SrcTex are NULL
4428  *  See IDirect3DTexture2::Load for details
4429  *
4430  *****************************************************************************/
4431 static HRESULT WINAPI
4432 IDirect3DDeviceImpl_7_Load(IDirect3DDevice7 *iface,
4433                            IDirectDrawSurface7 *DestTex,
4434                            POINT *DestPoint,
4435                            IDirectDrawSurface7 *SrcTex,
4436                            RECT *SrcRect,
4437                            DWORD Flags)
4438 {
4439     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4440     IDirectDrawSurfaceImpl *dest = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, DestTex);
4441     IDirectDrawSurfaceImpl *src = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, SrcTex);
4442     FIXME("(%p)->(%p,%p,%p,%p,%08lx): Partially Implemented!\n", This, dest, DestPoint, src, SrcRect, Flags);
4443
4444     if( (!src) || (!dest) )
4445         return DDERR_INVALIDPARAMS;
4446
4447     IDirect3DTexture2_Load(ICOM_INTERFACE(dest, IDirect3DTexture2),
4448                            ICOM_INTERFACE(src, IDirect3DTexture2));
4449     return D3D_OK;
4450 }
4451
4452 /*****************************************************************************
4453  * IDirect3DDevice7::LightEnable
4454  *
4455  * Enables or disables a light
4456  *
4457  * Version 7, IDirect3DLight uses this method too.
4458  *
4459  * Params:
4460  *  LightIndex: The index of the light to enable / disable
4461  *  Enable: Enable or disable the light
4462  *
4463  * Returns:
4464  *  D3D_OK on success
4465  *  For more details, see IWineD3DDevice::SetLightEnable
4466  *
4467  *****************************************************************************/
4468 static HRESULT WINAPI
4469 IDirect3DDeviceImpl_7_LightEnable(IDirect3DDevice7 *iface,
4470                                   DWORD LightIndex,
4471                                   BOOL Enable)
4472 {
4473     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4474     HRESULT hr;
4475     TRACE("(%p)->(%08lx,%d): Relay!\n", This, LightIndex, Enable);
4476
4477     hr = IWineD3DDevice_SetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4478     return hr_ddraw_from_wined3d(hr);
4479 }
4480
4481 /*****************************************************************************
4482  * IDirect3DDevice7::GetLightEnable
4483  *
4484  * Retrieves if the light with the given index is enabled or not
4485  *
4486  * Version 7
4487  *
4488  * Params:
4489  *  LightIndex: Index of desired light
4490  *  Enable: Pointer to a BOOL which contains the result
4491  *
4492  * Returns:
4493  *  D3D_OK on success
4494  *  DDERR_INVALIDPARAMS if Enable is NULL
4495  *  See IWineD3DDevice::GetLightEnable for more details
4496  *
4497  *****************************************************************************/
4498 static HRESULT WINAPI
4499 IDirect3DDeviceImpl_7_GetLightEnable(IDirect3DDevice7 *iface,
4500                                      DWORD LightIndex,
4501                                      BOOL* Enable)
4502 {
4503     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4504     HRESULT hr;
4505     TRACE("(%p)->(%08lx,%p): Relay\n", This, LightIndex, Enable);
4506
4507     if(!Enable)
4508         return DDERR_INVALIDPARAMS;
4509
4510     hr = IWineD3DDevice_GetLightEnable(This->wineD3DDevice, LightIndex, Enable);
4511     return hr_ddraw_from_wined3d(hr);
4512 }
4513
4514 /*****************************************************************************
4515  * IDirect3DDevice7::SetClipPlane
4516  *
4517  * Sets custom clipping plane
4518  *
4519  * Version 7
4520  *
4521  * Params:
4522  *  Index: The index of the clipping plane
4523  *  PlaneEquation: An equation defining the clipping plane
4524  *
4525  * Returns:
4526  *  D3D_OK on success
4527  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4528  *  See IWineD3DDevice::SetClipPlane for more details
4529  *
4530  *****************************************************************************/
4531 static HRESULT WINAPI
4532 IDirect3DDeviceImpl_7_SetClipPlane(IDirect3DDevice7 *iface,
4533                                    DWORD Index,
4534                                    D3DVALUE* PlaneEquation)
4535 {
4536     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4537     TRACE("(%p)->(%08lx,%p): Relay!\n", This, Index, PlaneEquation);
4538
4539     if(!PlaneEquation)
4540         return DDERR_INVALIDPARAMS;
4541
4542     return IWineD3DDevice_SetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4543 }
4544
4545 /*****************************************************************************
4546  * IDirect3DDevice7::GetClipPlane
4547  *
4548  * Returns the clipping plane with a specific index
4549  *
4550  * Params:
4551  *  Index: The index of the desired plane
4552  *  PlaneEquation: Address to store the plane equation to
4553  *
4554  * Returns:
4555  *  D3D_OK on success
4556  *  DDERR_INVALIDPARAMS if PlaneEquation is NULL
4557  *  See IWineD3DDevice::GetClipPlane for more details
4558  *
4559  *****************************************************************************/
4560 static HRESULT WINAPI
4561 IDirect3DDeviceImpl_7_GetClipPlane(IDirect3DDevice7 *iface,
4562                                    DWORD Index,
4563                                    D3DVALUE* PlaneEquation)
4564 {
4565     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4566     TRACE("(%p)->(%ld,%p): Relay!\n", This, Index, PlaneEquation);
4567
4568     if(!PlaneEquation)
4569         return DDERR_INVALIDPARAMS;
4570
4571     return IWineD3DDevice_GetClipPlane(This->wineD3DDevice, Index, PlaneEquation);
4572 }
4573
4574 /*****************************************************************************
4575  * IDirect3DDevice7::GetInfo
4576  *
4577  * Retrieves some information about the device. The DirectX sdk says that
4578  * this version returns S_FALSE for all retail builds of DirectX, that's what
4579  * this implementation does.
4580  *
4581  * Params:
4582  *  DevInfoID: Information type requested
4583  *  DevInfoStruct: Pointer to a structure to store the info to
4584  *  Size: Size of the structure
4585  *
4586  * Returns:
4587  *  S_FALSE, because it's a non-debug driver
4588  *
4589  *****************************************************************************/
4590 static HRESULT WINAPI
4591 IDirect3DDeviceImpl_7_GetInfo(IDirect3DDevice7 *iface,
4592                               DWORD DevInfoID,
4593                               void *DevInfoStruct,
4594                               DWORD Size)
4595 {
4596     ICOM_THIS_FROM(IDirect3DDeviceImpl, IDirect3DDevice7, iface);
4597     TRACE("(%p)->(%08lx,%p,%08lx)\n", This, DevInfoID, DevInfoStruct, Size);
4598
4599     if (TRACE_ON(d3d7))
4600     {
4601         TRACE(" info requested : ");
4602         switch (DevInfoID)
4603         {
4604             case D3DDEVINFOID_TEXTUREMANAGER: TRACE("D3DDEVINFOID_TEXTUREMANAGER\n"); break;
4605             case D3DDEVINFOID_D3DTEXTUREMANAGER: TRACE("D3DDEVINFOID_D3DTEXTUREMANAGER\n"); break;
4606             case D3DDEVINFOID_TEXTURING: TRACE("D3DDEVINFOID_TEXTURING\n"); break;
4607             default: ERR(" invalid flag !!!\n"); return DDERR_INVALIDPARAMS;
4608         }
4609     }
4610
4611     return S_FALSE; /* According to MSDN, this is valid for a non-debug driver */
4612 }
4613
4614 const IDirect3DDevice7Vtbl IDirect3DDevice7_Vtbl =
4615 {
4616     /*** IUnknown Methods ***/
4617     IDirect3DDeviceImpl_7_QueryInterface,
4618     IDirect3DDeviceImpl_7_AddRef,
4619     IDirect3DDeviceImpl_7_Release,
4620     /*** IDirect3DDevice7 ***/
4621     IDirect3DDeviceImpl_7_GetCaps,
4622     IDirect3DDeviceImpl_7_EnumTextureFormats,
4623     IDirect3DDeviceImpl_7_BeginScene,
4624     IDirect3DDeviceImpl_7_EndScene,
4625     IDirect3DDeviceImpl_7_GetDirect3D,
4626     IDirect3DDeviceImpl_7_SetRenderTarget,
4627     IDirect3DDeviceImpl_7_GetRenderTarget,
4628     IDirect3DDeviceImpl_7_Clear,
4629     IDirect3DDeviceImpl_7_SetTransform,
4630     IDirect3DDeviceImpl_7_GetTransform,
4631     IDirect3DDeviceImpl_7_SetViewport,
4632     IDirect3DDeviceImpl_7_MultiplyTransform,
4633     IDirect3DDeviceImpl_7_GetViewport,
4634     IDirect3DDeviceImpl_7_SetMaterial,
4635     IDirect3DDeviceImpl_7_GetMaterial,
4636     IDirect3DDeviceImpl_7_SetLight,
4637     IDirect3DDeviceImpl_7_GetLight,
4638     IDirect3DDeviceImpl_7_SetRenderState,
4639     IDirect3DDeviceImpl_7_GetRenderState,
4640     IDirect3DDeviceImpl_7_BeginStateBlock,
4641     IDirect3DDeviceImpl_7_EndStateBlock,
4642     IDirect3DDeviceImpl_7_PreLoad,
4643     IDirect3DDeviceImpl_7_DrawPrimitive,
4644     IDirect3DDeviceImpl_7_DrawIndexedPrimitive,
4645     IDirect3DDeviceImpl_7_SetClipStatus,
4646     IDirect3DDeviceImpl_7_GetClipStatus,
4647     IDirect3DDeviceImpl_7_DrawPrimitiveStrided,
4648     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveStrided,
4649     IDirect3DDeviceImpl_7_DrawPrimitiveVB,
4650     IDirect3DDeviceImpl_7_DrawIndexedPrimitiveVB,
4651     IDirect3DDeviceImpl_7_ComputeSphereVisibility,
4652     IDirect3DDeviceImpl_7_GetTexture,
4653     IDirect3DDeviceImpl_7_SetTexture,
4654     IDirect3DDeviceImpl_7_GetTextureStageState,
4655     IDirect3DDeviceImpl_7_SetTextureStageState,
4656     IDirect3DDeviceImpl_7_ValidateDevice,
4657     IDirect3DDeviceImpl_7_ApplyStateBlock,
4658     IDirect3DDeviceImpl_7_CaptureStateBlock,
4659     IDirect3DDeviceImpl_7_DeleteStateBlock,
4660     IDirect3DDeviceImpl_7_CreateStateBlock,
4661     IDirect3DDeviceImpl_7_Load,
4662     IDirect3DDeviceImpl_7_LightEnable,
4663     IDirect3DDeviceImpl_7_GetLightEnable,
4664     IDirect3DDeviceImpl_7_SetClipPlane,
4665     IDirect3DDeviceImpl_7_GetClipPlane,
4666     IDirect3DDeviceImpl_7_GetInfo
4667 };
4668
4669 const IDirect3DDevice3Vtbl IDirect3DDevice3_Vtbl =
4670 {
4671     /*** IUnknown Methods ***/
4672     Thunk_IDirect3DDeviceImpl_3_QueryInterface,
4673     Thunk_IDirect3DDeviceImpl_3_AddRef,
4674     Thunk_IDirect3DDeviceImpl_3_Release,
4675     /*** IDirect3DDevice3 ***/
4676     IDirect3DDeviceImpl_3_GetCaps,
4677     IDirect3DDeviceImpl_3_GetStats,
4678     IDirect3DDeviceImpl_3_AddViewport,
4679     IDirect3DDeviceImpl_3_DeleteViewport,
4680     IDirect3DDeviceImpl_3_NextViewport,
4681     Thunk_IDirect3DDeviceImpl_3_EnumTextureFormats,
4682     Thunk_IDirect3DDeviceImpl_3_BeginScene,
4683     Thunk_IDirect3DDeviceImpl_3_EndScene,
4684     Thunk_IDirect3DDeviceImpl_3_GetDirect3D,
4685     IDirect3DDeviceImpl_3_SetCurrentViewport,
4686     IDirect3DDeviceImpl_3_GetCurrentViewport,
4687     Thunk_IDirect3DDeviceImpl_3_SetRenderTarget,
4688     Thunk_IDirect3DDeviceImpl_3_GetRenderTarget,
4689     IDirect3DDeviceImpl_3_Begin,
4690     IDirect3DDeviceImpl_3_BeginIndexed,
4691     IDirect3DDeviceImpl_3_Vertex,
4692     IDirect3DDeviceImpl_3_Index,
4693     IDirect3DDeviceImpl_3_End,
4694     Thunk_IDirect3DDeviceImpl_3_GetRenderState,
4695     Thunk_IDirect3DDeviceImpl_3_SetRenderState,
4696     IDirect3DDeviceImpl_3_GetLightState,
4697     IDirect3DDeviceImpl_3_SetLightState,
4698     Thunk_IDirect3DDeviceImpl_3_SetTransform,
4699     Thunk_IDirect3DDeviceImpl_3_GetTransform,
4700     Thunk_IDirect3DDeviceImpl_3_MultiplyTransform,
4701     Thunk_IDirect3DDeviceImpl_3_DrawPrimitive,
4702     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitive,
4703     Thunk_IDirect3DDeviceImpl_3_SetClipStatus,
4704     Thunk_IDirect3DDeviceImpl_3_GetClipStatus,
4705     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveStrided,
4706     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveStrided,
4707     Thunk_IDirect3DDeviceImpl_3_DrawPrimitiveVB,
4708     Thunk_IDirect3DDeviceImpl_3_DrawIndexedPrimitiveVB,
4709     Thunk_IDirect3DDeviceImpl_3_ComputeSphereVisibility,
4710     Thunk_IDirect3DDeviceImpl_3_GetTexture,
4711     Thunk_IDirect3DDeviceImpl_3_SetTexture,
4712     Thunk_IDirect3DDeviceImpl_3_GetTextureStageState,
4713     Thunk_IDirect3DDeviceImpl_3_SetTextureStageState,
4714     Thunk_IDirect3DDeviceImpl_3_ValidateDevice
4715 };
4716
4717 const IDirect3DDevice2Vtbl IDirect3DDevice2_Vtbl =
4718 {
4719     /*** IUnknown Methods ***/
4720     Thunk_IDirect3DDeviceImpl_2_QueryInterface,
4721     Thunk_IDirect3DDeviceImpl_2_AddRef,
4722     Thunk_IDirect3DDeviceImpl_2_Release,
4723     /*** IDirect3DDevice2 ***/
4724     Thunk_IDirect3DDeviceImpl_2_GetCaps,
4725     IDirect3DDeviceImpl_2_SwapTextureHandles,
4726     Thunk_IDirect3DDeviceImpl_2_GetStats,
4727     Thunk_IDirect3DDeviceImpl_2_AddViewport,
4728     Thunk_IDirect3DDeviceImpl_2_DeleteViewport,
4729     Thunk_IDirect3DDeviceImpl_2_NextViewport,
4730     IDirect3DDeviceImpl_2_EnumTextureFormats,
4731     Thunk_IDirect3DDeviceImpl_2_BeginScene,
4732     Thunk_IDirect3DDeviceImpl_2_EndScene,
4733     Thunk_IDirect3DDeviceImpl_2_GetDirect3D,
4734     Thunk_IDirect3DDeviceImpl_2_SetCurrentViewport,
4735     Thunk_IDirect3DDeviceImpl_2_GetCurrentViewport,
4736     Thunk_IDirect3DDeviceImpl_2_SetRenderTarget,
4737     Thunk_IDirect3DDeviceImpl_2_GetRenderTarget,
4738     Thunk_IDirect3DDeviceImpl_2_Begin,
4739     Thunk_IDirect3DDeviceImpl_2_BeginIndexed,
4740     Thunk_IDirect3DDeviceImpl_2_Vertex,
4741     Thunk_IDirect3DDeviceImpl_2_Index,
4742     Thunk_IDirect3DDeviceImpl_2_End,
4743     Thunk_IDirect3DDeviceImpl_2_GetRenderState,
4744     Thunk_IDirect3DDeviceImpl_2_SetRenderState,
4745     Thunk_IDirect3DDeviceImpl_2_GetLightState,
4746     Thunk_IDirect3DDeviceImpl_2_SetLightState,
4747     Thunk_IDirect3DDeviceImpl_2_SetTransform,
4748     Thunk_IDirect3DDeviceImpl_2_GetTransform,
4749     Thunk_IDirect3DDeviceImpl_2_MultiplyTransform,
4750     Thunk_IDirect3DDeviceImpl_2_DrawPrimitive,
4751     Thunk_IDirect3DDeviceImpl_2_DrawIndexedPrimitive,
4752     Thunk_IDirect3DDeviceImpl_2_SetClipStatus,
4753     Thunk_IDirect3DDeviceImpl_2_GetClipStatus
4754 };
4755
4756 const IDirect3DDeviceVtbl IDirect3DDevice1_Vtbl =
4757 {
4758     /*** IUnknown Methods ***/
4759     Thunk_IDirect3DDeviceImpl_1_QueryInterface,
4760     Thunk_IDirect3DDeviceImpl_1_AddRef,
4761     Thunk_IDirect3DDeviceImpl_1_Release,
4762     /*** IDirect3DDevice1 ***/
4763     IDirect3DDeviceImpl_1_Initialize,
4764     Thunk_IDirect3DDeviceImpl_1_GetCaps,
4765     Thunk_IDirect3DDeviceImpl_1_SwapTextureHandles,
4766     IDirect3DDeviceImpl_1_CreateExecuteBuffer,
4767     Thunk_IDirect3DDeviceImpl_1_GetStats,
4768     IDirect3DDeviceImpl_1_Execute,
4769     Thunk_IDirect3DDeviceImpl_1_AddViewport,
4770     Thunk_IDirect3DDeviceImpl_1_DeleteViewport,
4771     Thunk_IDirect3DDeviceImpl_1_NextViewport,
4772     IDirect3DDeviceImpl_1_Pick,
4773     IDirect3DDeviceImpl_1_GetPickRecords,
4774     Thunk_IDirect3DDeviceImpl_1_EnumTextureFormats,
4775     IDirect3DDeviceImpl_1_CreateMatrix,
4776     IDirect3DDeviceImpl_1_SetMatrix,
4777     IDirect3DDeviceImpl_1_GetMatrix,
4778     IDirect3DDeviceImpl_1_DeleteMatrix,
4779     Thunk_IDirect3DDeviceImpl_1_EndScene,
4780     Thunk_IDirect3DDeviceImpl_1_BeginScene,
4781     Thunk_IDirect3DDeviceImpl_1_GetDirect3D
4782 };
4783
4784 /*****************************************************************************
4785  * IDirect3DDeviceImpl_CreateHandle
4786  *
4787  * Not called from the VTable
4788  *
4789  * Some older interface versions operate with handles, which are basically
4790  * DWORDs which identify an interface, for example
4791  * IDirect3DDevice::SetRenderState with DIRECT3DRENDERSTATE_TEXTUREHANDLE
4792  *
4793  * Those handle could be just casts to the interface pointers or vice versa,
4794  * but that is not 64 bit safe and would mean blindly derefering a DWORD
4795  * passed by the app. Instead there is a dynamic array in the device which
4796  * keeps a DWORD to pointer information and a type for the handle.
4797  *
4798  * Basically this array only grows, when a handle is freed its pointer is
4799  * just set to NULL. There will be much more reads from the array than
4800  * insertion operations, so a dynamic array is fine.
4801  *
4802  * Params:
4803  *  This: D3DDevice implementation for which this handle should be created
4804  *
4805  * Returns:
4806  *  A free handle on success
4807  *  0 on failure
4808  *
4809  *****************************************************************************/
4810 DWORD
4811 IDirect3DDeviceImpl_CreateHandle(IDirect3DDeviceImpl *This)
4812 {
4813     DWORD i;
4814     struct HandleEntry *oldHandles = This->Handles;
4815
4816     TRACE("(%p)\n", This);
4817
4818     for(i = 0; i < This->numHandles; i++)
4819     {
4820         if(This->Handles[i].ptr == NULL &&
4821            This->Handles[i].type == DDrawHandle_Unknown)
4822         {
4823             TRACE("Reusing freed handle %ld\n", i + 1);
4824             return i + 1;
4825         }
4826     }
4827
4828     TRACE("Growing the handle array\n");
4829
4830     This->numHandles++;
4831     This->Handles = HeapAlloc(GetProcessHeap(), 0, sizeof(struct HandleEntry) * This->numHandles);
4832     if(!This->Handles)
4833     {
4834         ERR("Out of memory\n");
4835         This->Handles = oldHandles;
4836         This->numHandles--;
4837         return 0;
4838     }
4839     if(oldHandles)
4840     {
4841         memcpy(This->Handles, oldHandles, (This->numHandles - 1) * sizeof(struct HandleEntry));
4842         HeapFree(GetProcessHeap(), 0, oldHandles);
4843     }
4844
4845     TRACE("Returning %ld\n", This->numHandles);
4846     return This->numHandles;
4847 }