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