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