wintrust: Use path in WIN_TRUST_SUBJECT_FILE structure rather than assuming a path...
[wine] / dlls / ddraw / direct3d.c
1 /*
2  * Copyright (c) 2006 Stefan Dösinger
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18
19 #include "config.h"
20 #include "wine/port.h"
21 #include "wine/debug.h"
22
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <stdlib.h>
27
28 #define COBJMACROS
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "wingdi.h"
34 #include "wine/exception.h"
35
36 #include "ddraw.h"
37 #include "d3d.h"
38
39 #include "ddraw_private.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
42
43 /*****************************************************************************
44  * IDirect3D7::QueryInterface
45  *
46  * QueryInterface implementation with thunks to IDirectDraw7
47  *
48  *****************************************************************************/
49 static HRESULT WINAPI
50 Thunk_IDirect3DImpl_7_QueryInterface(IDirect3D7 *iface,
51                                     REFIID refiid,
52                                     void **obj)
53 {
54     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
55     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
56
57     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
58                                        refiid,
59                                        obj);
60 }
61
62 static HRESULT WINAPI
63 Thunk_IDirect3DImpl_3_QueryInterface(IDirect3D3 *iface,
64                                     REFIID refiid,
65                                     void **obj)
66 {
67     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
68     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
69
70     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
71                                        refiid,
72                                        obj);
73 }
74
75 static HRESULT WINAPI
76 Thunk_IDirect3DImpl_2_QueryInterface(IDirect3D2 *iface,
77                                     REFIID refiid,
78                                     void **obj)
79 {
80     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
81     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
82
83     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
84                                        refiid,
85                                        obj);
86 }
87
88 static HRESULT WINAPI
89 Thunk_IDirect3DImpl_1_QueryInterface(IDirect3D *iface,
90                                     REFIID refiid,
91                                     void **obj)
92 {
93     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
94     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
95
96     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
97                                        refiid,
98                                        obj);
99 }
100
101 /*****************************************************************************
102  * IDirect3D7::AddRef
103  *
104  * DirectDraw refcounting is a bit odd. Every version of the ddraw interface
105  * has its own refcount, but IDirect3D 1/2/3 refcounts are linked to
106  * IDirectDraw, and IDirect3D7 is linked to IDirectDraw7
107  *
108  * IDirect3D7 -> IDirectDraw7
109  * IDirect3D3 -> IDirectDraw
110  * IDirect3D2 -> IDirectDraw
111  * IDirect3D  -> IDirectDraw
112  *
113  * So every AddRef implementation thunks to a different interface, and the
114  * IDirectDrawX::AddRef implementations have different counters...
115  *
116  * Returns
117  *  The new refcount
118  *
119  *****************************************************************************/
120 static ULONG WINAPI
121 Thunk_IDirect3DImpl_7_AddRef(IDirect3D7 *iface)
122 {
123     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
124     TRACE("(%p) : Thunking to IDirectDraw7.\n", This);
125
126     return IDirectDraw7_AddRef(ICOM_INTERFACE(This, IDirectDraw7));
127 }
128
129 static ULONG WINAPI
130 Thunk_IDirect3DImpl_3_AddRef(IDirect3D3 *iface)
131 {
132     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
133     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
134
135     return IDirectDraw_AddRef(ICOM_INTERFACE(This, IDirectDraw));
136 }
137
138 static ULONG WINAPI
139 Thunk_IDirect3DImpl_2_AddRef(IDirect3D2 *iface)
140 {
141     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
142     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
143
144     return IDirectDraw_AddRef(ICOM_INTERFACE(This, IDirectDraw));
145 }
146
147 static ULONG WINAPI
148 Thunk_IDirect3DImpl_1_AddRef(IDirect3D *iface)
149 {
150     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
151     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
152
153     return IDirectDraw_AddRef(ICOM_INTERFACE(This, IDirectDraw));
154 }
155
156 /*****************************************************************************
157  * IDirect3D7::Release
158  *
159  * Same story as IDirect3D7::AddRef
160  *
161  * Returns: The new refcount
162  *
163  *****************************************************************************/
164 static ULONG WINAPI
165 Thunk_IDirect3DImpl_7_Release(IDirect3D7 *iface)
166 {
167     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
168     TRACE("(%p) : Thunking to IDirectDraw7.\n", This);
169
170     return IDirectDraw7_Release(ICOM_INTERFACE(This, IDirectDraw7));
171 }
172
173 static ULONG WINAPI
174 Thunk_IDirect3DImpl_3_Release(IDirect3D3 *iface)
175 {
176     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
177     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
178
179     return IDirectDraw_Release(ICOM_INTERFACE(This, IDirectDraw));
180 }
181
182 static ULONG WINAPI
183 Thunk_IDirect3DImpl_2_Release(IDirect3D2 *iface)
184 {
185     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
186     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
187
188     return IDirectDraw_Release(ICOM_INTERFACE(This, IDirectDraw));
189 }
190
191 static ULONG WINAPI
192 Thunk_IDirect3DImpl_1_Release(IDirect3D *iface)
193 {
194     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
195     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
196
197     return IDirectDraw_Release(ICOM_INTERFACE(This, IDirectDraw));
198 }
199
200 /*****************************************************************************
201  * IDirect3D Methods
202  *****************************************************************************/
203
204 /*****************************************************************************
205  * IDirect3D::Initialize
206  *
207  * Initializes the IDirect3D interface. This is a no-op implementation,
208  * as all initialization is done at create time.
209  *
210  * Version 1
211  *
212  * Params:
213  *  refiid: ?
214  *
215  * Returns:
216  *  D3D_OK, because it's a no-op
217  *
218  *****************************************************************************/
219 static HRESULT WINAPI
220 IDirect3DImpl_1_Initialize(IDirect3D *iface,
221                            REFIID refiid)
222 {
223     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
224
225     TRACE("(%p)->(%s) no-op...\n", This, debugstr_guid(refiid));
226     return D3D_OK;
227 }
228
229 /*****************************************************************************
230  * IDirect3D7::EnumDevices
231  *
232  * The EnumDevices method for IDirect3D7. It enumerates all supported
233  * D3D7 devices. Currently the T&L, HAL and RGB devices are enumerated.
234  *
235  * Params:
236  *  Callback: Function to call for each enumerated device
237  *  Context: Pointer to pass back to the app
238  *
239  * Returns:
240  *  D3D_OK, or the return value of the GetCaps call
241  *
242  *****************************************************************************/
243 static HRESULT WINAPI
244 IDirect3DImpl_7_EnumDevices(IDirect3D7 *iface,
245                           LPD3DENUMDEVICESCALLBACK7 Callback,
246                           void *Context)
247 {
248     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
249     char interface_name_tnl[] = "WINE Direct3D7 Hardware Transform and Lighting acceleration using WineD3D";
250     char device_name_tnl[] = "Wine D3D7 T&L HAL";
251     char interface_name_hal[] = "WINE Direct3D7 Hardware acceleration using WineD3D";
252     char device_name_hal[] = "Wine D3D7 HAL";
253     char interface_name_rgb[] = "WINE Direct3D7 RGB Software Emulation using WineD3D";
254     char device_name_rgb[] = "Wine D3D7 RGB";
255     D3DDEVICEDESC7 ddesc;
256     D3DDEVICEDESC oldDesc;
257     HRESULT hr;
258
259     TRACE("(%p)->(%p,%p)\n", This, Callback, Context);
260     EnterCriticalSection(&ddraw_cs);
261
262     TRACE("(%p) Enumerating WineD3D D3Device7 interface\n", This);
263     hr = IDirect3DImpl_GetCaps(This->wineD3D, &oldDesc, &ddesc);
264     if(hr != D3D_OK)
265     {
266         LeaveCriticalSection(&ddraw_cs);
267         return hr;
268     }
269     Callback(interface_name_tnl, device_name_tnl, &ddesc, Context);
270
271     ddesc.deviceGUID = IID_IDirect3DHALDevice;
272     Callback(interface_name_hal, device_name_hal, &ddesc, Context);
273
274     ddesc.deviceGUID = IID_IDirect3DRGBDevice;
275     Callback(interface_name_rgb, device_name_rgb, &ddesc, Context);
276
277     TRACE("(%p) End of enumeration\n", This);
278     LeaveCriticalSection(&ddraw_cs);
279     return D3D_OK;
280 }
281
282 /*****************************************************************************
283  * IDirect3D3::EnumDevices
284  *
285  * Enumerates all supported Direct3DDevice interfaces. This is the
286  * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
287  *
288  * Version 1, 2 and 3
289  *
290  * Params:
291  *  Callback: Application-provided routine to call for each enumerated device
292  *  Context: Pointer to pass to the callback
293  *
294  * Returns:
295  *  D3D_OK on success,
296  *  The result of IDirect3DImpl_GetCaps if it failed
297  *
298  *****************************************************************************/
299 static HRESULT WINAPI
300 IDirect3DImpl_3_EnumDevices(IDirect3D3 *iface,
301                             LPD3DENUMDEVICESCALLBACK Callback,
302                             void *Context)
303 {
304     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
305     D3DDEVICEDESC dref, d1, d2;
306     D3DDEVICEDESC7 newDesc;
307     static CHAR wined3d_description[] = "Wine D3DDevice using WineD3D and OpenGL";
308     HRESULT hr;
309
310     /* Some games (Motoracer 2 demo) have the bad idea to modify the device name string.
311        Let's put the string in a sufficiently sized array in writable memory. */
312     char device_name[50];
313     strcpy(device_name,"Direct3D HEL");
314
315     TRACE("(%p)->(%p,%p)\n", This, Callback, Context);
316     EnterCriticalSection(&ddraw_cs);
317
318     hr = IDirect3DImpl_GetCaps(This->wineD3D, &dref, &newDesc);
319     if(hr != D3D_OK)
320     {
321         LeaveCriticalSection(&ddraw_cs);
322         return hr;
323     }
324
325     /* Do I have to enumerate the reference id? Note from old d3d7:
326      * "It seems that enumerating the reference IID on Direct3D 1 games
327      * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
328      *
329      * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers, EnumReference
330      * which enables / disables enumerating the reference rasterizer. It's a DWORD,
331      * 0 means disabled, 2 means enabled. The enablerefrast.reg and disablerefrast.reg
332      * files in the DirectX 7.0 sdk demo directory suggest this.
333      *
334      * Some games(GTA 2) seem to use the second enumerated device, so I have to enumerate
335      * at least 2 devices. So enumerate the reference device to have 2 devices.
336      *
337      * Other games(Rollcage) tell emulation and hal device apart by certain flags.
338      * Rollcage expects D3DPTEXTURECAPS_POW2 to be set(yeah, it is a limitation flag),
339      * and it refuses all devices that have the perspective flag set. This way it refuses
340      * the emulation device, and HAL devices never have POW2 unset in d3d7 on windows.
341      */
342
343     if(This->d3dversion != 1)
344     {
345         static CHAR reference_description[] = "RGB Direct3D emulation";
346
347         TRACE("(%p) Enumerating WineD3D D3DDevice interface\n", This);
348         d1 = dref;
349         d2 = dref;
350         /* The rgb device has the pow2 flag set in the hel caps, but not in the hal caps */
351         d1.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
352         d1.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
353         hr = Callback( (LPIID) &IID_IDirect3DRGBDevice, reference_description, device_name, &d1, &d2, Context);
354         if(hr != D3DENUMRET_OK)
355         {
356             TRACE("Application cancelled the enumeration\n");
357             LeaveCriticalSection(&ddraw_cs);
358             return D3D_OK;
359         }
360     }
361
362     strcpy(device_name,"Direct3D HAL");
363
364     TRACE("(%p) Enumerating HAL Direct3D device\n", This);
365     d1 = dref;
366     d2 = dref;
367     /* The hal device does not have the pow2 flag set in hel, but in hal */
368     d2.dpcLineCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
369     d2.dpcTriCaps.dwTextureCaps &= ~(D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL | D3DPTEXTURECAPS_PERSPECTIVE);
370     hr = Callback( (LPIID) &IID_IDirect3DHALDevice, wined3d_description, device_name, &d1, &d2, Context);
371     if(hr != D3DENUMRET_OK)
372     {
373         TRACE("Application cancelled the enumeration\n");
374         LeaveCriticalSection(&ddraw_cs);
375         return D3D_OK;
376     }
377     TRACE("(%p) End of enumeration\n", This);
378
379     LeaveCriticalSection(&ddraw_cs);
380     return D3D_OK;
381 }
382
383 static HRESULT WINAPI
384 Thunk_IDirect3DImpl_2_EnumDevices(IDirect3D2 *iface,
385                                   LPD3DENUMDEVICESCALLBACK Callback,
386                                   void *Context)
387 {
388     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
389     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Callback, Context);
390     return IDirect3D3_EnumDevices(ICOM_INTERFACE(This, IDirect3D3),
391                                   Callback,
392                                   Context);
393 }
394
395 static HRESULT WINAPI
396 Thunk_IDirect3DImpl_1_EnumDevices(IDirect3D *iface,
397                                   LPD3DENUMDEVICESCALLBACK Callback,
398                                   void *Context)
399 {
400     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
401     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Callback, Context);
402     return IDirect3D3_EnumDevices(ICOM_INTERFACE(This, IDirect3D3),
403                                   Callback,
404                                   Context);
405 }
406
407 /*****************************************************************************
408  * IDirect3D3::CreateLight
409  *
410  * Creates an IDirect3DLight interface. This interface is used in
411  * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
412  * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
413  * uses the IDirect3DDevice7 interface with D3D7 lights.
414  *
415  * Version 1, 2 and 3
416  *
417  * Params:
418  *  Light: Address to store the new interface pointer
419  *  UnkOuter: Basically for aggregation, but ddraw doesn't support it.
420  *            Must be NULL
421  *
422  * Returns:
423  *  D3D_OK on success
424  *  DDERR_OUTOFMEMORY if memory allocation failed
425  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
426  *
427  *****************************************************************************/
428 static HRESULT WINAPI
429 IDirect3DImpl_3_CreateLight(IDirect3D3 *iface,
430                             IDirect3DLight **Light,
431                             IUnknown *UnkOuter )
432 {
433     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
434     IDirect3DLightImpl *object;
435
436     TRACE("(%p)->(%p,%p)\n", This, Light, UnkOuter);
437
438     if(UnkOuter)
439         return CLASS_E_NOAGGREGATION;
440
441     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightImpl));
442     if (object == NULL)
443         return DDERR_OUTOFMEMORY;
444
445     ICOM_INIT_INTERFACE(object, IDirect3DLight, IDirect3DLight_Vtbl);
446     object->ref = 1;
447     object->ddraw = This;
448     object->next = NULL;
449     object->active_viewport = NULL;
450
451     /* Update functions */
452     object->activate = light_update;
453     object->desactivate = light_activate;
454     object->update = light_desactivate;
455     object->active_viewport = NULL;
456
457     *Light = ICOM_INTERFACE(object, IDirect3DLight);
458
459     TRACE("(%p) creating implementation at %p.\n", This, object);
460
461     return D3D_OK;
462 }
463
464 static HRESULT WINAPI
465 Thunk_IDirect3DImpl_2_CreateLight(IDirect3D2 *iface,
466                                   IDirect3DLight **Direct3DLight,
467                                   IUnknown *UnkOuter)
468 {
469     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
470     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DLight, UnkOuter);
471     return IDirect3D3_CreateLight(ICOM_INTERFACE(This, IDirect3D3),
472                                   Direct3DLight,
473                                   UnkOuter);
474 }
475
476 static HRESULT WINAPI
477 Thunk_IDirect3DImpl_1_CreateLight(IDirect3D *iface,
478                                   IDirect3DLight **Direct3DLight,
479                                   IUnknown *UnkOuter)
480 {
481     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
482     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DLight, UnkOuter);
483     return IDirect3D3_CreateLight(ICOM_INTERFACE(This, IDirect3D3),
484                                   Direct3DLight,
485                                   UnkOuter);
486 }
487
488 /*****************************************************************************
489  * IDirect3D3::CreateMaterial
490  *
491  * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
492  * and older versions. The IDirect3DMaterial implementation wraps its
493  * functionality to IDirect3DDevice7::SetMaterial and friends.
494  *
495  * Version 1, 2 and 3
496  *
497  * Params:
498  *  Material: Address to store the new interface's pointer to
499  *  UnkOuter: Basically for aggregation, but ddraw doesn't support it.
500  *            Must be NULL
501  *
502  * Returns:
503  *  D3D_OK on success
504  *  DDERR_OUTOFMEMORY if memory allocation failed
505  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
506  *
507  *****************************************************************************/
508 static HRESULT WINAPI
509 IDirect3DImpl_3_CreateMaterial(IDirect3D3 *iface,
510                                IDirect3DMaterial3 **Material,
511                                IUnknown *UnkOuter )
512 {
513     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
514     IDirect3DMaterialImpl *object;
515
516     TRACE("(%p)->(%p,%p)\n", This, Material, UnkOuter);
517
518     if(UnkOuter)
519         return CLASS_E_NOAGGREGATION;
520
521     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DMaterialImpl));
522     if (object == NULL)
523         return DDERR_OUTOFMEMORY;
524
525     ICOM_INIT_INTERFACE(object, IDirect3DMaterial3, IDirect3DMaterial3_Vtbl);
526     ICOM_INIT_INTERFACE(object, IDirect3DMaterial2, IDirect3DMaterial2_Vtbl);
527     ICOM_INIT_INTERFACE(object, IDirect3DMaterial, IDirect3DMaterial_Vtbl);
528     object->ref = 1;
529     object->ddraw = This;
530     object->activate = material_activate;
531
532     *Material = ICOM_INTERFACE(object, IDirect3DMaterial3);
533
534     TRACE("(%p) creating implementation at %p.\n", This, object);
535
536     return D3D_OK;
537 }
538
539 static HRESULT WINAPI
540 Thunk_IDirect3DImpl_2_CreateMaterial(IDirect3D2 *iface,
541                                      IDirect3DMaterial2 **Direct3DMaterial,
542                                      IUnknown* UnkOuter)
543 {
544     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
545     HRESULT ret;
546     IDirect3DMaterial3 *ret_val;
547
548     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DMaterial, UnkOuter);
549     ret = IDirect3D3_CreateMaterial(ICOM_INTERFACE(This, IDirect3D3),
550                                     &ret_val,
551                                     UnkOuter);
552
553     *Direct3DMaterial = COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial3, IDirect3DMaterial2, ret_val);
554
555     TRACE(" returning interface %p.\n", *Direct3DMaterial);
556
557     return ret;
558 }
559
560 static HRESULT WINAPI
561 Thunk_IDirect3DImpl_1_CreateMaterial(IDirect3D *iface,
562                                      IDirect3DMaterial **Direct3DMaterial,
563                                      IUnknown* UnkOuter)
564 {
565     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
566     HRESULT ret;
567     LPDIRECT3DMATERIAL3 ret_val;
568
569     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DMaterial, UnkOuter);
570     ret = IDirect3D3_CreateMaterial(ICOM_INTERFACE(This, IDirect3D3),
571                                     &ret_val,
572                                     UnkOuter);
573
574     *Direct3DMaterial = COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial3, IDirect3DMaterial, ret_val);
575
576     TRACE(" returning interface %p.\n", *Direct3DMaterial);
577
578     return ret;
579 }
580
581 /*****************************************************************************
582  * IDirect3D3::CreateViewport
583  *
584  * Creates an IDirect3DViewport interface. This interface is used
585  * by Direct3D and earlier versions for Viewport management. In Direct3D7
586  * it has been replaced by a viewport structure and
587  * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
588  * uses the IDirect3DDevice7 methods for its functionality
589  *
590  * Params:
591  *  Viewport: Address to store the new interface pointer
592  *  UnkOuter: Basically for aggregation, but ddraw doesn't support it.
593  *            Must be NULL
594  *
595  * Returns:
596  *  D3D_OK on success
597  *  DDERR_OUTOFMEMORY if memory allocation failed
598  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
599  *
600  *****************************************************************************/
601 static HRESULT WINAPI
602 IDirect3DImpl_3_CreateViewport(IDirect3D3 *iface,
603                               IDirect3DViewport3 **Viewport,
604                               IUnknown *UnkOuter )
605 {
606     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
607     IDirect3DViewportImpl *object;
608
609     if(UnkOuter)
610         return CLASS_E_NOAGGREGATION;
611
612     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DViewportImpl));
613     if (object == NULL)
614         return DDERR_OUTOFMEMORY;
615
616     ICOM_INIT_INTERFACE(object, IDirect3DViewport3, IDirect3DViewport3_Vtbl);
617     object->ref = 1;
618     object->ddraw = This;
619     object->activate = viewport_activate;
620     object->use_vp2 = 0xFF;
621     object->next = NULL;
622     object->lights = NULL;
623     object->num_lights = 0;
624     object->map_lights = 0;
625
626     *Viewport = ICOM_INTERFACE(object, IDirect3DViewport3);
627
628     TRACE("(%p) creating implementation at %p.\n",This, object);
629
630     return D3D_OK;
631 }
632
633 static HRESULT WINAPI
634 Thunk_IDirect3DImpl_2_CreateViewport(IDirect3D2 *iface,
635                                      IDirect3DViewport2 **D3DViewport2,
636                                      IUnknown *UnkOuter)
637 {
638     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
639     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, D3DViewport2, UnkOuter);
640
641     return IDirect3D3_CreateViewport(ICOM_INTERFACE(This, IDirect3D3),
642                                      (IDirect3DViewport3 **) D3DViewport2 /* No need to cast here */,
643                                      UnkOuter);
644 }
645
646 static HRESULT WINAPI
647 Thunk_IDirect3DImpl_1_CreateViewport(IDirect3D *iface,
648                                      IDirect3DViewport **D3DViewport,
649                                      IUnknown* UnkOuter)
650 {
651     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
652     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, D3DViewport, UnkOuter);
653
654     return IDirect3D3_CreateViewport(ICOM_INTERFACE(This, IDirect3D3),
655                                      (IDirect3DViewport3 **) D3DViewport /* No need to cast here */,
656                                      UnkOuter);
657 }
658
659 /*****************************************************************************
660  * IDirect3D3::FindDevice
661  *
662  * This method finds a device with the requested properties and returns a
663  * device description
664  *
665  * Verion 1, 2 and 3
666  * Params:
667  *  D3DDFS: Describes the requested device characteristics
668  *  D3DFDR: Returns the device description
669  *
670  * Returns:
671  *  D3D_OK on success
672  *  DDERR_INVALIDPARAMS if no device was found
673  *
674  *****************************************************************************/
675 static HRESULT WINAPI
676 IDirect3DImpl_3_FindDevice(IDirect3D3 *iface,
677                            D3DFINDDEVICESEARCH *D3DDFS,
678                            D3DFINDDEVICERESULT *D3DFDR)
679 {
680     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
681     D3DDEVICEDESC desc;
682     D3DDEVICEDESC7 newDesc;
683     HRESULT hr;
684
685     TRACE("(%p)->(%p,%p)\n", This, D3DDFS, D3DFDR);
686
687     if ((D3DDFS->dwFlags & D3DFDS_COLORMODEL) &&
688         (D3DDFS->dcmColorModel != D3DCOLOR_RGB))
689     {
690         TRACE(" trying to request a non-RGB D3D color model. Not supported.\n");
691         return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
692     }
693     if (D3DDFS->dwFlags & D3DFDS_GUID)
694     {
695         TRACE(" trying to match guid %s.\n", debugstr_guid(&(D3DDFS->guid)));
696         if ((IsEqualGUID(&IID_D3DDEVICE_WineD3D, &(D3DDFS->guid)) == 0) &&
697             (IsEqualGUID(&IID_IDirect3DHALDevice, &(D3DDFS->guid)) == 0) &&
698             (IsEqualGUID(&IID_IDirect3DRefDevice, &(D3DDFS->guid)) == 0))
699         {
700             TRACE(" no match for this GUID.\n");
701             return DDERR_INVALIDPARAMS;
702         }
703     }
704
705     /* Get the caps */
706     hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc, &newDesc);
707     if(hr != D3D_OK) return hr;
708
709     /* Now return our own GUID */
710     D3DFDR->guid = IID_D3DDEVICE_WineD3D;
711     D3DFDR->ddHwDesc = desc;
712     D3DFDR->ddSwDesc = desc;
713
714     TRACE(" returning Wine's WineD3D device with (undumped) capabilities\n");
715
716     return D3D_OK;
717 }
718
719 static HRESULT WINAPI
720 Thunk_IDirect3DImpl_2_FindDevice(IDirect3D2 *iface,
721                                  D3DFINDDEVICESEARCH *D3DDFS,
722                                  D3DFINDDEVICERESULT *D3DFDR)
723 {
724     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
725     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", iface, D3DDFS, D3DFDR);
726     return IDirect3D3_FindDevice(ICOM_INTERFACE(This, IDirect3D3),
727                                  D3DDFS,
728                                  D3DFDR);
729 }
730
731 static HRESULT WINAPI
732 Thunk_IDirect3DImpl_1_FindDevice(IDirect3D *iface,
733                                 D3DFINDDEVICESEARCH *D3DDFS,
734                                 D3DFINDDEVICERESULT *D3DDevice)
735 {
736     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
737     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, D3DDFS, D3DDevice);
738     return IDirect3D3_FindDevice(ICOM_INTERFACE(This, IDirect3D3),
739                                  D3DDFS,
740                                  D3DDevice);
741 }
742
743 /*****************************************************************************
744  * IDirect3D7::CreateDevice
745  *
746  * Creates an IDirect3DDevice7 interface.
747  *
748  * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
749  * DirectDraw surfaces and are created with
750  * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
751  * create the device object and QueryInterfaces for IDirect3DDevice
752  *
753  * Params:
754  *  refiid: IID of the device to create
755  *  Surface: Initial rendertarget
756  *  Device: Address to return the interface pointer
757  *
758  * Returns:
759  *  D3D_OK on success
760  *  DDERR_OUTOFMEMORY if memory allocation failed
761  *  DDERR_INVALIDPARAMS if a device exists already
762  *
763  *****************************************************************************/
764 static HRESULT WINAPI
765 IDirect3DImpl_7_CreateDevice(IDirect3D7 *iface,
766                              REFCLSID refiid,
767                              IDirectDrawSurface7 *Surface,
768                              IDirect3DDevice7 **Device)
769 {
770     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
771     IDirect3DDeviceImpl *object;
772     IParentImpl *IndexBufferParent;
773     HRESULT hr;
774     IDirectDrawSurfaceImpl *target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Surface);
775     TRACE("(%p)->(%s,%p,%p)\n", iface, debugstr_guid(refiid), Surface, Device);
776
777     EnterCriticalSection(&ddraw_cs);
778     *Device = NULL;
779
780     /* Fail device creation if non-opengl surfaces are used */
781     if(This->ImplType != SURFACE_OPENGL)
782     {
783         ERR("The application wants to create a Direct3D device, but non-opengl surfaces are set in the registry. Please set the surface implementation to opengl or autodetection to allow 3D rendering\n");
784
785         /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
786          * is caught in CreateSurface or QueryInterface
787          */
788         LeaveCriticalSection(&ddraw_cs);
789         return DDERR_NO3D;
790     }
791
792     /* So far we can only create one device per ddraw object */
793     if(This->d3ddevice)
794     {
795         FIXME("(%p): Only one Direct3D device per DirectDraw object supported\n", This);
796         LeaveCriticalSection(&ddraw_cs);
797         return DDERR_INVALIDPARAMS;
798     }
799
800     object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirect3DDeviceImpl));
801     if(!object)
802     {
803         ERR("Out of memory when allocating a IDirect3DDevice implementation\n");
804         LeaveCriticalSection(&ddraw_cs);
805         return DDERR_OUTOFMEMORY;
806     }
807
808     if (This->cooperative_level & DDSCL_FPUPRESERVE)
809         ICOM_INIT_INTERFACE(object, IDirect3DDevice7, IDirect3DDevice7_FPUPreserve_Vtbl);
810     else
811         ICOM_INIT_INTERFACE(object, IDirect3DDevice7, IDirect3DDevice7_FPUSetup_Vtbl);
812
813     ICOM_INIT_INTERFACE(object, IDirect3DDevice3, IDirect3DDevice3_Vtbl);
814     ICOM_INIT_INTERFACE(object, IDirect3DDevice2, IDirect3DDevice2_Vtbl);
815     ICOM_INIT_INTERFACE(object, IDirect3DDevice, IDirect3DDevice1_Vtbl);
816
817     object->ref = 1;
818     object->ddraw = This;
819     object->viewport_list = NULL;
820     object->current_viewport = NULL;
821     object->material = 0;
822     object->target = target;
823
824     object->Handles = NULL;
825     object->numHandles = 0;
826
827     object->legacyTextureBlending = FALSE;
828
829     /* This is for convenience */
830     object->wineD3DDevice = This->wineD3DDevice;
831
832     /* Create an index buffer, it's needed for indexed drawing */
833     IndexBufferParent = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl));
834     if(!IndexBufferParent)
835     {
836         ERR("Allocating memory for an index buffer parent failed\n");
837         HeapFree(GetProcessHeap(), 0, object);
838         LeaveCriticalSection(&ddraw_cs);
839         return DDERR_OUTOFMEMORY;
840     }
841     ICOM_INIT_INTERFACE(IndexBufferParent, IParent, IParent_Vtbl);
842     IndexBufferParent->ref = 1;
843
844     /* Create an Index Buffer. WineD3D needs one for Drawing indexed primitives
845      * Create a (hopefully) long enough buffer, and copy the indices into it
846      * Ideally, a IWineD3DIndexBuffer::SetData method could be created, which
847      * takes the pointer and avoids the memcpy
848      */
849     hr = IWineD3DDevice_CreateIndexBuffer(This->wineD3DDevice,
850                                           0x40000, /* Length. Don't know how long it should be */
851                                           WINED3DUSAGE_DYNAMIC, /* Usage */
852                                           WINED3DFMT_INDEX16, /* Format. D3D7 uses WORDS */
853                                           WINED3DPOOL_DEFAULT,
854                                           &object->indexbuffer,
855                                           0 /* Handle */,
856                                           (IUnknown *) ICOM_INTERFACE(IndexBufferParent, IParent));
857
858     if(FAILED(hr))
859     {
860         ERR("Failed to create an index buffer\n");
861         HeapFree(GetProcessHeap(), 0, object);
862         LeaveCriticalSection(&ddraw_cs);
863         return hr;
864     }
865     IndexBufferParent->child = (IUnknown *) object->indexbuffer;
866
867     /* No need to set the indices, it's done when necessary */
868
869     /* AddRef the WineD3D Device */
870     IWineD3DDevice_AddRef(This->wineD3DDevice);
871
872     /* Don't forget to return the interface ;) */
873     *Device = ICOM_INTERFACE(object, IDirect3DDevice7);
874
875     TRACE(" (%p) Created an IDirect3DDeviceImpl object at %p\n", This, object);
876
877     /* This is for apps which create a non-flip, non-d3d primary surface
878      * and an offscreen D3DDEVICE surface, then render to the offscreen surface
879      * and do a Blt from the offscreen to the primary surface.
880      *
881      * Set the offscreen D3DDDEVICE surface(=target) as the back buffer,
882      * and the primary surface(=This->d3d_target) as the front buffer.
883      *
884      * This way the app will render to the D3DDEVICE surface and WineD3D
885      * will catch the Blt was Back Buffer -> Front buffer blt and perform
886      * a flip instead. This way we don't have to deal with a mixed GL / GDI
887      * environment.
888      *
889      * This should be checked against windowed apps. The only app tested with
890      * this is moto racer 2 during the loading screen.
891      */
892     TRACE("Isrendertarget: %s, d3d_target=%p\n", target->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE ? "true" : "false", This->d3d_target);
893     if(!(target->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) &&
894        (This->d3d_target != target))
895     {
896         WINED3DVIEWPORT vp;
897         TRACE("(%p) Using %p as front buffer, %p as back buffer\n", This, This->d3d_target, target);
898         hr = IWineD3DDevice_SetFrontBackBuffers(This->wineD3DDevice,
899                                                 This->d3d_target->WineD3DSurface,
900                                                 target->WineD3DSurface);
901         if(hr != D3D_OK)
902             ERR("(%p) Error %08x setting the front and back buffer\n", This, hr);
903
904         /* Render to the back buffer */
905         IWineD3DDevice_SetRenderTarget(This->wineD3DDevice, 0,
906                                        target->WineD3DSurface);
907
908         vp.X = 0;
909         vp.Y = 0;
910         vp.Width = target->surface_desc.dwWidth;
911         vp.Height = target->surface_desc.dwHeight;
912         vp.MinZ = 0.0;
913         vp.MaxZ = 1.0;
914         IWineD3DDevice_SetViewport(This->wineD3DDevice,
915                                    &vp);
916
917         object->OffScreenTarget = TRUE;
918     }
919     else
920     {
921         object->OffScreenTarget = FALSE;
922     }
923
924     /* AddRef the render target. Also AddRef the render target from ddraw,
925      * because if it is released before the app releases the D3D device, the D3D capabilities
926      * of WineD3D will be uninitialized, which has bad effects.
927      *
928      * In most cases, those surfaces are the surfaces are the same anyway, but this will simply
929      * add another ref which is released when the device is destroyed.
930      */
931     IDirectDrawSurface7_AddRef(Surface);
932     IDirectDrawSurface7_AddRef(ICOM_INTERFACE(This->d3d_target, IDirectDrawSurface7));
933
934     This->d3ddevice = object;
935
936     IWineD3DDevice_SetRenderState(This->wineD3DDevice,
937                                   WINED3DRS_ZENABLE,
938                                   IDirect3DDeviceImpl_UpdateDepthStencil(object));
939     LeaveCriticalSection(&ddraw_cs);
940     return D3D_OK;
941 }
942
943 static HRESULT WINAPI
944 Thunk_IDirect3DImpl_3_CreateDevice(IDirect3D3 *iface,
945                                    REFCLSID refiid,
946                                    IDirectDrawSurface4 *Surface,
947                                    IDirect3DDevice3 **Device,
948                                    IUnknown *UnkOuter)
949 {
950     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
951     HRESULT hr;
952     TRACE("(%p)->(%s,%p,%p,%p): Thunking to IDirect3D7\n", This, debugstr_guid(refiid), Surface, Device, UnkOuter);
953
954     if(UnkOuter != NULL)
955         return CLASS_E_NOAGGREGATION;
956
957     hr =  IDirect3D7_CreateDevice(ICOM_INTERFACE(This, IDirect3D7),
958                                   refiid,
959                                   (IDirectDrawSurface7 *) Surface /* Same VTables */,
960                                   (IDirect3DDevice7 **) Device);
961
962     *Device = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice3, *Device);
963     return hr;
964 }
965
966 static HRESULT WINAPI
967 Thunk_IDirect3DImpl_2_CreateDevice(IDirect3D2 *iface,
968                                    REFCLSID refiid,
969                                    IDirectDrawSurface *Surface,
970                                    IDirect3DDevice2 **Device)
971 {
972     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
973     HRESULT hr;
974     TRACE("(%p)->(%s,%p,%p): Thunking to IDirect3D7\n", This, debugstr_guid(refiid), Surface, Device);
975
976     hr =  IDirect3D7_CreateDevice(ICOM_INTERFACE(This, IDirect3D7),
977                                   refiid,
978                                   COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface3, IDirectDrawSurface7, Surface),
979                                   (IDirect3DDevice7 **) Device);
980
981     *Device = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice2, *Device);
982     return hr;
983 }
984
985 /*****************************************************************************
986  * IDirect3D7::CreateVertexBuffer
987  *
988  * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
989  * interface.
990  *
991  * Version 3 and 7
992  *
993  * Params:
994  *  Desc: Requested Vertex buffer properties
995  *  VertexBuffer: Address to return the interface pointer at
996  *  Flags: Some flags, must be 0
997  *
998  * Returns
999  *  D3D_OK on success
1000  *  DDERR_OUTOFMEMORY if memory allocation failed
1001  *  The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
1002  *  DDERR_INVALIDPARAMS if Desc or VertexBuffer are NULL, or Flags != 0
1003  *
1004  *****************************************************************************/
1005 static HRESULT WINAPI
1006 IDirect3DImpl_7_CreateVertexBuffer(IDirect3D7 *iface,
1007                                    D3DVERTEXBUFFERDESC *Desc,
1008                                    IDirect3DVertexBuffer7 **VertexBuffer,
1009                                    DWORD Flags)
1010 {
1011     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
1012     IDirect3DVertexBufferImpl *object;
1013     HRESULT hr;
1014     TRACE("(%p)->(%p,%p,%08x)\n", This, Desc, VertexBuffer, Flags);
1015
1016     TRACE("(%p) Vertex buffer description:\n", This);
1017     TRACE("(%p)  dwSize=%d\n", This, Desc->dwSize);
1018     TRACE("(%p)  dwCaps=%08x\n", This, Desc->dwCaps);
1019     TRACE("(%p)  FVF=%08x\n", This, Desc->dwFVF);
1020     TRACE("(%p)  dwNumVertices=%d\n", This, Desc->dwNumVertices);
1021
1022     /* D3D7 SDK: "No Flags are currently defined for this method. This
1023      * parameter must be 0"
1024      *
1025      * Never trust the documentation - this is wrong
1026     if(Flags != 0)
1027     {
1028         ERR("(%p) Flags is %08lx, returning DDERR_INVALIDPARAMS\n", This, Flags);
1029         return DDERR_INVALIDPARAMS;
1030     }
1031      */
1032
1033     /* Well, this sounds sane */
1034     if( (!VertexBuffer) || (!Desc) )
1035         return DDERR_INVALIDPARAMS;
1036
1037     /* Now create the vertex buffer */
1038     object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirect3DVertexBufferImpl));
1039     if(!object)
1040     {
1041         ERR("(%p) Out of memory when allocating a IDirect3DVertexBufferImpl structure\n", This);
1042         return DDERR_OUTOFMEMORY;
1043     }
1044
1045     object->ref = 1;
1046     ICOM_INIT_INTERFACE(object, IDirect3DVertexBuffer7, IDirect3DVertexBuffer7_Vtbl);
1047     ICOM_INIT_INTERFACE(object, IDirect3DVertexBuffer, IDirect3DVertexBuffer1_Vtbl);
1048
1049     object->Caps = Desc->dwCaps;
1050     object->ddraw = This;
1051
1052     EnterCriticalSection(&ddraw_cs);
1053     hr = IWineD3DDevice_CreateVertexBuffer(This->wineD3DDevice,
1054                                            get_flexible_vertex_size(Desc->dwFVF) * Desc->dwNumVertices,
1055                                            Desc->dwCaps & D3DVBCAPS_WRITEONLY ? WINED3DUSAGE_WRITEONLY : 0,
1056                                            Desc->dwFVF,
1057                                            Desc->dwCaps & D3DVBCAPS_SYSTEMMEMORY ? WINED3DPOOL_SYSTEMMEM : WINED3DPOOL_DEFAULT,
1058                                            &object->wineD3DVertexBuffer,
1059                                            0 /* SharedHandle */,
1060                                            (IUnknown *) ICOM_INTERFACE(object, IDirect3DVertexBuffer7));
1061     if(hr != D3D_OK)
1062     {
1063         ERR("(%p) IWineD3DDevice::CreateVertexBuffer failed with hr=%08x\n", This, hr);
1064         HeapFree(GetProcessHeap(), 0, object);
1065         LeaveCriticalSection(&ddraw_cs);
1066         if (hr == WINED3DERR_INVALIDCALL)
1067             return DDERR_INVALIDPARAMS;
1068         else
1069             return hr;
1070     }
1071
1072     object->wineD3DVertexDeclaration = IDirectDrawImpl_FindDecl(This,
1073                                                                 Desc->dwFVF);
1074     if(!object->wineD3DVertexDeclaration)
1075     {
1076         ERR("Cannot find the vertex declaration for fvf %08x\n", Desc->dwFVF);
1077         IWineD3DVertexBuffer_Release(object->wineD3DVertexBuffer);
1078         HeapFree(GetProcessHeap(), 0, object);
1079         LeaveCriticalSection(&ddraw_cs);
1080         return DDERR_INVALIDPARAMS;
1081     }
1082     IWineD3DVertexDeclaration_AddRef(object->wineD3DVertexDeclaration);
1083
1084     /* Return the interface */
1085     *VertexBuffer = ICOM_INTERFACE(object, IDirect3DVertexBuffer7);
1086
1087     TRACE("(%p) Created new vertex buffer implementation at %p, returning interface at %p\n", This, object, *VertexBuffer);
1088     LeaveCriticalSection(&ddraw_cs);
1089     return D3D_OK;
1090 }
1091
1092 static HRESULT WINAPI
1093 Thunk_IDirect3DImpl_3_CreateVertexBuffer(IDirect3D3 *iface,
1094                                          D3DVERTEXBUFFERDESC *Desc,
1095                                          IDirect3DVertexBuffer **VertexBuffer,
1096                                          DWORD Flags,
1097                                          IUnknown *UnkOuter)
1098 {
1099     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
1100     HRESULT hr;
1101     TRACE("(%p)->(%p,%p,%08x,%p): Relaying to IDirect3D7\n", This, Desc, VertexBuffer, Flags, UnkOuter);
1102
1103     if(UnkOuter != NULL) return CLASS_E_NOAGGREGATION;
1104
1105     hr = IDirect3D7_CreateVertexBuffer(ICOM_INTERFACE(This, IDirect3D7),
1106                                        Desc,
1107                                        (IDirect3DVertexBuffer7 **) VertexBuffer,
1108                                        Flags);
1109
1110     *VertexBuffer = COM_INTERFACE_CAST(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, IDirect3DVertexBuffer, *VertexBuffer);
1111     return hr;
1112 }
1113
1114
1115 /*****************************************************************************
1116  * IDirect3D7::EnumZBufferFormats
1117  *
1118  * Enumerates all supported Z buffer pixel formats
1119  *
1120  * Version 3 and 7
1121  *
1122  * Params:
1123  *  refiidDevice:
1124  *  Callback: Callback to call for each pixel format
1125  *  Context: Pointer to pass back to the callback
1126  *
1127  * Returns:
1128  *  D3D_OK on success
1129  *  DDERR_INVALIDPARAMS if Callback is NULL
1130  *  For details, see IWineD3DDevice::EnumZBufferFormats
1131  *
1132  *****************************************************************************/
1133 static HRESULT WINAPI
1134 IDirect3DImpl_7_EnumZBufferFormats(IDirect3D7 *iface,
1135                                    REFCLSID refiidDevice,
1136                                    LPD3DENUMPIXELFORMATSCALLBACK Callback,
1137                                    void *Context)
1138 {
1139     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
1140     HRESULT hr;
1141     int i;
1142     WINED3DDISPLAYMODE d3ddm;
1143     WINED3DDEVTYPE type;
1144
1145     /* Order matters. Specifically, BattleZone II (full version) expects the
1146      * 16-bit depth formats to be listed before the 24 and 32 ones. */
1147     WINED3DFORMAT FormatList[] = {
1148         WINED3DFMT_D15S1,
1149         WINED3DFMT_D16,
1150         WINED3DFMT_D24X8,
1151         WINED3DFMT_D24X4S4,
1152         WINED3DFMT_D24S8,
1153         WINED3DFMT_D32
1154     };
1155
1156     TRACE("(%p)->(%s,%p,%p): Relay\n", iface, debugstr_guid(refiidDevice), Callback, Context);
1157
1158     if(!Callback)
1159         return DDERR_INVALIDPARAMS;
1160
1161     if(IsEqualGUID(refiidDevice, &IID_IDirect3DHALDevice)    ||
1162        IsEqualGUID(refiidDevice, &IID_IDirect3DTnLHalDevice) ||
1163        IsEqualGUID(refiidDevice, &IID_D3DDEVICE_WineD3D))
1164     {
1165         TRACE("Asked for HAL device\n");
1166         type = WINED3DDEVTYPE_HAL;
1167     }
1168     else if(IsEqualGUID(refiidDevice, &IID_IDirect3DRGBDevice) ||
1169             IsEqualGUID(refiidDevice, &IID_IDirect3DMMXDevice))
1170     {
1171         TRACE("Asked for SW device\n");
1172         type = WINED3DDEVTYPE_SW;
1173     }
1174     else if(IsEqualGUID(refiidDevice, &IID_IDirect3DRefDevice))
1175     {
1176         TRACE("Asked for REF device\n");
1177         type = WINED3DDEVTYPE_REF;
1178     }
1179     else if(IsEqualGUID(refiidDevice, &IID_IDirect3DNullDevice))
1180     {
1181         TRACE("Asked for NULLREF device\n");
1182         type = WINED3DDEVTYPE_NULLREF;
1183     }
1184     else
1185     {
1186         FIXME("Unexpected device GUID %s\n", debugstr_guid(refiidDevice));
1187         type = WINED3DDEVTYPE_HAL;
1188     }
1189
1190     EnterCriticalSection(&ddraw_cs);
1191     /* We need an adapter format from somewhere to please wined3d and WGL. Use the current display mode.
1192      * So far all cards offer the same depth stencil format for all modes, but if some do not and apps
1193      * do not like that we'll have to find some workaround, like iterating over all imaginable formats
1194      * and collecting all the depth stencil formats we can get
1195      */
1196     hr = IWineD3DDevice_GetDisplayMode(This->wineD3DDevice,
1197                                        0 /* swapchain 0 */,
1198                                        &d3ddm);
1199
1200     for(i = 0; i < (sizeof(FormatList) / sizeof(FormatList[0])); i++)
1201     {
1202         hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
1203                                         WINED3DADAPTER_DEFAULT /* Adapter */,
1204                                         type /* DeviceType */,
1205                                         d3ddm.Format /* AdapterFormat */,
1206                                         WINED3DUSAGE_DEPTHSTENCIL /* Usage */,
1207                                         WINED3DRTYPE_SURFACE,
1208                                         FormatList[i]);
1209         if(hr == D3D_OK)
1210         {
1211             DDPIXELFORMAT pformat;
1212
1213             memset(&pformat, 0, sizeof(pformat));
1214             pformat.dwSize = sizeof(pformat);
1215             PixelFormat_WineD3DtoDD(&pformat, FormatList[i]);
1216
1217             TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
1218             hr = Callback(&pformat, Context);
1219             if(hr != DDENUMRET_OK)
1220             {
1221                 TRACE("Format enumeration cancelled by application\n");
1222                 LeaveCriticalSection(&ddraw_cs);
1223                 return D3D_OK;
1224             }
1225         }
1226     }
1227     TRACE("End of enumeration\n");
1228     LeaveCriticalSection(&ddraw_cs);
1229     return D3D_OK;
1230 }
1231
1232 static HRESULT WINAPI
1233 Thunk_IDirect3DImpl_3_EnumZBufferFormats(IDirect3D3 *iface,
1234                                          REFCLSID riidDevice,
1235                                          LPD3DENUMPIXELFORMATSCALLBACK Callback,
1236                                          void *Context)
1237 {
1238     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
1239     TRACE("(%p)->(%s,%p,%p) thunking to IDirect3D7 interface.\n", This, debugstr_guid(riidDevice), Callback, Context);
1240     return IDirect3D7_EnumZBufferFormats(ICOM_INTERFACE(This, IDirect3D7),
1241                                          riidDevice,
1242                                          Callback,
1243                                          Context);
1244 }
1245
1246 /*****************************************************************************
1247  * IDirect3D7::EvictManagedTextures
1248  *
1249  * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
1250  * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
1251  *
1252  * Version 3 and 7
1253  *
1254  * Returns:
1255  *  D3D_OK, because it's a stub
1256  *
1257  *****************************************************************************/
1258 static HRESULT WINAPI
1259 IDirect3DImpl_7_EvictManagedTextures(IDirect3D7 *iface)
1260 {
1261     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
1262     FIXME("(%p): Stub!\n", This);
1263
1264     /* Implementation idea:
1265      * Add an IWineD3DSurface method which sets the opengl texture
1266      * priority low or even removes the opengl texture.
1267      */
1268
1269     return D3D_OK;
1270 }
1271
1272 static HRESULT WINAPI
1273 Thunk_IDirect3DImpl_3_EvictManagedTextures(IDirect3D3 *iface)
1274 {
1275     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
1276     TRACE("(%p)->() thunking to IDirect3D7 interface.\n", This);
1277     return IDirect3D7_EvictManagedTextures(ICOM_INTERFACE(This, IDirect3D7));
1278 }
1279
1280 /*****************************************************************************
1281  * IDirect3DImpl_GetCaps
1282  *
1283  * This function retrieves the device caps from wined3d
1284  * and converts it into a D3D7 and D3D - D3D3 structure
1285  * This is a helper function called from various places in ddraw
1286  *
1287  * Params:
1288  *  WineD3D: The interface to get the caps from
1289  *  Desc123: Old D3D <3 structure to fill (needed)
1290  *  Desc7: D3D7 device desc structure to fill (needed)
1291  *
1292  * Returns
1293  *  D3D_OK on success, or the return value of IWineD3D::GetCaps
1294  *
1295  *****************************************************************************/
1296 HRESULT
1297 IDirect3DImpl_GetCaps(IWineD3D *WineD3D,
1298                       D3DDEVICEDESC *Desc123,
1299                       D3DDEVICEDESC7 *Desc7)
1300 {
1301     WINED3DCAPS WCaps;
1302     HRESULT hr;
1303
1304     /* Some variables to assign to the pointers in WCaps */
1305     TRACE("()->(%p,%p,%p\n", WineD3D, Desc123, Desc7);
1306
1307     memset(&WCaps, 0, sizeof(WCaps));
1308     EnterCriticalSection(&ddraw_cs);
1309     hr = IWineD3D_GetDeviceCaps(WineD3D, 0, WINED3DDEVTYPE_HAL, &WCaps);
1310     LeaveCriticalSection(&ddraw_cs);
1311     if(hr != D3D_OK)
1312     {
1313         return hr;
1314     }
1315
1316     /* Copy the results into the d3d7 and d3d3 structures */
1317     Desc7->dwDevCaps = WCaps.DevCaps;
1318     Desc7->dpcLineCaps.dwRasterCaps = WCaps.RasterCaps;
1319     Desc7->dpcLineCaps.dwZCmpCaps = WCaps.ZCmpCaps;
1320     Desc7->dpcLineCaps.dwSrcBlendCaps = WCaps.SrcBlendCaps;
1321     Desc7->dpcLineCaps.dwDestBlendCaps = WCaps.DestBlendCaps;
1322     Desc7->dpcLineCaps.dwAlphaCmpCaps = WCaps.AlphaCmpCaps;
1323     Desc7->dpcLineCaps.dwShadeCaps = WCaps.ShadeCaps;
1324     Desc7->dpcLineCaps.dwTextureCaps = WCaps.TextureCaps;
1325     Desc7->dpcLineCaps.dwTextureFilterCaps = WCaps.TextureFilterCaps;
1326     Desc7->dpcLineCaps.dwTextureAddressCaps = WCaps.TextureAddressCaps;
1327
1328     Desc7->dwMaxTextureWidth = WCaps.MaxTextureWidth;
1329     Desc7->dwMaxTextureHeight = WCaps.MaxTextureHeight;
1330
1331     Desc7->dwMaxTextureRepeat = WCaps.MaxTextureRepeat;
1332     Desc7->dwMaxTextureAspectRatio = WCaps.MaxTextureAspectRatio;
1333     Desc7->dwMaxAnisotropy = WCaps.MaxAnisotropy;
1334     Desc7->dvMaxVertexW = WCaps.MaxVertexW;
1335
1336     Desc7->dvGuardBandLeft = WCaps.GuardBandLeft;
1337     Desc7->dvGuardBandTop = WCaps.GuardBandTop;
1338     Desc7->dvGuardBandRight = WCaps.GuardBandRight;
1339     Desc7->dvGuardBandBottom = WCaps.GuardBandBottom;
1340
1341     Desc7->dvExtentsAdjust = WCaps.ExtentsAdjust;
1342     Desc7->dwStencilCaps = WCaps.StencilCaps;
1343
1344     Desc7->dwFVFCaps = WCaps.FVFCaps;
1345     Desc7->dwTextureOpCaps = WCaps.TextureOpCaps;
1346
1347     Desc7->dwVertexProcessingCaps = WCaps.VertexProcessingCaps;
1348     Desc7->dwMaxActiveLights = WCaps.MaxActiveLights;
1349
1350     /* Remove all non-d3d7 caps */
1351     Desc7->dwDevCaps &= (
1352         D3DDEVCAPS_FLOATTLVERTEX         | D3DDEVCAPS_SORTINCREASINGZ          | D3DDEVCAPS_SORTDECREASINGZ          |
1353         D3DDEVCAPS_SORTEXACT             | D3DDEVCAPS_EXECUTESYSTEMMEMORY      | D3DDEVCAPS_EXECUTEVIDEOMEMORY       |
1354         D3DDEVCAPS_TLVERTEXSYSTEMMEMORY  | D3DDEVCAPS_TLVERTEXVIDEOMEMORY      | D3DDEVCAPS_TEXTURESYSTEMMEMORY      |
1355         D3DDEVCAPS_TEXTUREVIDEOMEMORY    | D3DDEVCAPS_DRAWPRIMTLVERTEX         | D3DDEVCAPS_CANRENDERAFTERFLIP       |
1356         D3DDEVCAPS_TEXTURENONLOCALVIDMEM | D3DDEVCAPS_DRAWPRIMITIVES2          | D3DDEVCAPS_SEPARATETEXTUREMEMORIES  |
1357         D3DDEVCAPS_DRAWPRIMITIVES2EX     | D3DDEVCAPS_HWTRANSFORMANDLIGHT      | D3DDEVCAPS_CANBLTSYSTONONLOCAL      |
1358         D3DDEVCAPS_HWRASTERIZATION);
1359
1360     Desc7->dwStencilCaps &= (
1361         D3DSTENCILCAPS_KEEP              | D3DSTENCILCAPS_ZERO                 | D3DSTENCILCAPS_REPLACE              |
1362         D3DSTENCILCAPS_INCRSAT           | D3DSTENCILCAPS_DECRSAT              | D3DSTENCILCAPS_INVERT               |
1363         D3DSTENCILCAPS_INCR              | D3DSTENCILCAPS_DECR                 | D3DSTENCILCAPS_TWOSIDED);
1364
1365     /* FVF caps ?*/
1366
1367     Desc7->dwTextureOpCaps &= (
1368         D3DTEXOPCAPS_DISABLE             | D3DTEXOPCAPS_SELECTARG1             | D3DTEXOPCAPS_SELECTARG2             |
1369         D3DTEXOPCAPS_MODULATE            | D3DTEXOPCAPS_MODULATE2X             | D3DTEXOPCAPS_MODULATE4X             |
1370         D3DTEXOPCAPS_ADD                 | D3DTEXOPCAPS_ADDSIGNED              | D3DTEXOPCAPS_ADDSIGNED2X            |
1371         D3DTEXOPCAPS_SUBTRACT            | D3DTEXOPCAPS_ADDSMOOTH              | D3DTEXOPCAPS_BLENDTEXTUREALPHA      |
1372         D3DTEXOPCAPS_BLENDFACTORALPHA    | D3DTEXOPCAPS_BLENDTEXTUREALPHAPM    | D3DTEXOPCAPS_BLENDCURRENTALPHA      |
1373         D3DTEXOPCAPS_PREMODULATE         | D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA |
1374         D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR | D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA | D3DTEXOPCAPS_BUMPENVMAP    |
1375         D3DTEXOPCAPS_BUMPENVMAPLUMINANCE | D3DTEXOPCAPS_DOTPRODUCT3);
1376
1377     Desc7->dwVertexProcessingCaps &= (
1378         D3DVTXPCAPS_TEXGEN               | D3DVTXPCAPS_MATERIALSOURCE7         | D3DVTXPCAPS_VERTEXFOG               |
1379         D3DVTXPCAPS_DIRECTIONALLIGHTS    | D3DVTXPCAPS_POSITIONALLIGHTS        | D3DVTXPCAPS_LOCALVIEWER);
1380
1381     Desc7->dpcLineCaps.dwMiscCaps &= (
1382         D3DPMISCCAPS_MASKPLANES          | D3DPMISCCAPS_MASKZ                  | D3DPMISCCAPS_LINEPATTERNREP         |
1383         D3DPMISCCAPS_CONFORMANT          | D3DPMISCCAPS_CULLNONE               | D3DPMISCCAPS_CULLCW                 |
1384         D3DPMISCCAPS_CULLCCW);
1385
1386     Desc7->dpcLineCaps.dwRasterCaps &= (
1387         D3DPRASTERCAPS_DITHER            | D3DPRASTERCAPS_ROP2                 | D3DPRASTERCAPS_XOR                  |
1388         D3DPRASTERCAPS_PAT               | D3DPRASTERCAPS_ZTEST                | D3DPRASTERCAPS_SUBPIXEL             |
1389         D3DPRASTERCAPS_SUBPIXELX         | D3DPRASTERCAPS_FOGVERTEX            | D3DPRASTERCAPS_FOGTABLE             |
1390         D3DPRASTERCAPS_STIPPLE           | D3DPRASTERCAPS_ANTIALIASSORTDEPENDENT | D3DPRASTERCAPS_ANTIALIASSORTINDEPENDENT |
1391         D3DPRASTERCAPS_ANTIALIASEDGES    | D3DPRASTERCAPS_MIPMAPLODBIAS        | D3DPRASTERCAPS_ZBIAS                |
1392         D3DPRASTERCAPS_ZBUFFERLESSHSR    | D3DPRASTERCAPS_FOGRANGE             | D3DPRASTERCAPS_ANISOTROPY           |
1393         D3DPRASTERCAPS_WBUFFER           | D3DPRASTERCAPS_TRANSLUCENTSORTINDEPENDENT | D3DPRASTERCAPS_WFOG           |
1394         D3DPRASTERCAPS_ZFOG);
1395
1396     Desc7->dpcLineCaps.dwZCmpCaps &= (
1397         D3DPCMPCAPS_NEVER                | D3DPCMPCAPS_LESS                    | D3DPCMPCAPS_EQUAL                   |
1398         D3DPCMPCAPS_LESSEQUAL            | D3DPCMPCAPS_GREATER                 | D3DPCMPCAPS_NOTEQUAL                |
1399         D3DPCMPCAPS_GREATEREQUAL         | D3DPCMPCAPS_ALWAYS);
1400
1401     Desc7->dpcLineCaps.dwSrcBlendCaps &= (
1402         D3DPBLENDCAPS_ZERO               | D3DPBLENDCAPS_ONE                   | D3DPBLENDCAPS_SRCCOLOR              |
1403         D3DPBLENDCAPS_INVSRCCOLOR        | D3DPBLENDCAPS_SRCALPHA              | D3DPBLENDCAPS_INVSRCALPHA           |
1404         D3DPBLENDCAPS_DESTALPHA          | D3DPBLENDCAPS_INVDESTALPHA          | D3DPBLENDCAPS_DESTCOLOR             |
1405         D3DPBLENDCAPS_INVDESTCOLOR       | D3DPBLENDCAPS_SRCALPHASAT           | D3DPBLENDCAPS_BOTHSRCALPHA          |
1406         D3DPBLENDCAPS_BOTHINVSRCALPHA);
1407
1408     Desc7->dpcLineCaps.dwDestBlendCaps &= (
1409         D3DPBLENDCAPS_ZERO               | D3DPBLENDCAPS_ONE                   | D3DPBLENDCAPS_SRCCOLOR              |
1410         D3DPBLENDCAPS_INVSRCCOLOR        | D3DPBLENDCAPS_SRCALPHA              | D3DPBLENDCAPS_INVSRCALPHA           |
1411         D3DPBLENDCAPS_DESTALPHA          | D3DPBLENDCAPS_INVDESTALPHA          | D3DPBLENDCAPS_DESTCOLOR             |
1412         D3DPBLENDCAPS_INVDESTCOLOR       | D3DPBLENDCAPS_SRCALPHASAT           | D3DPBLENDCAPS_BOTHSRCALPHA          |
1413         D3DPBLENDCAPS_BOTHINVSRCALPHA);
1414
1415     Desc7->dpcLineCaps.dwAlphaCmpCaps &= (
1416         D3DPCMPCAPS_NEVER                | D3DPCMPCAPS_LESS                    | D3DPCMPCAPS_EQUAL                   |
1417         D3DPCMPCAPS_LESSEQUAL            | D3DPCMPCAPS_GREATER                 | D3DPCMPCAPS_NOTEQUAL                |
1418         D3DPCMPCAPS_GREATEREQUAL         | D3DPCMPCAPS_ALWAYS);
1419
1420     Desc7->dpcLineCaps.dwShadeCaps &= (
1421         D3DPSHADECAPS_COLORFLATMONO      | D3DPSHADECAPS_COLORFLATRGB          | D3DPSHADECAPS_COLORGOURAUDMONO      |
1422         D3DPSHADECAPS_COLORGOURAUDRGB    | D3DPSHADECAPS_COLORPHONGMONO        | D3DPSHADECAPS_COLORPHONGRGB         |
1423         D3DPSHADECAPS_SPECULARFLATMONO   | D3DPSHADECAPS_SPECULARFLATRGB       | D3DPSHADECAPS_SPECULARGOURAUDMONO   |
1424         D3DPSHADECAPS_SPECULARGOURAUDRGB | D3DPSHADECAPS_SPECULARPHONGMONO     | D3DPSHADECAPS_SPECULARPHONGRGB      |
1425         D3DPSHADECAPS_ALPHAFLATBLEND     | D3DPSHADECAPS_ALPHAFLATSTIPPLED     | D3DPSHADECAPS_ALPHAGOURAUDBLEND     |
1426         D3DPSHADECAPS_ALPHAGOURAUDSTIPPLED | D3DPSHADECAPS_ALPHAPHONGBLEND     | D3DPSHADECAPS_ALPHAPHONGSTIPPLED    |
1427         D3DPSHADECAPS_FOGFLAT            | D3DPSHADECAPS_FOGGOURAUD            | D3DPSHADECAPS_FOGPHONG);
1428
1429     Desc7->dpcLineCaps.dwTextureCaps &= (
1430         D3DPTEXTURECAPS_PERSPECTIVE      | D3DPTEXTURECAPS_POW2                | D3DPTEXTURECAPS_ALPHA               |
1431         D3DPTEXTURECAPS_TRANSPARENCY     | D3DPTEXTURECAPS_BORDER              | D3DPTEXTURECAPS_SQUAREONLY          |
1432         D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE | D3DPTEXTURECAPS_ALPHAPALETTE| D3DPTEXTURECAPS_NONPOW2CONDITIONAL  |
1433         D3DPTEXTURECAPS_PROJECTED        | D3DPTEXTURECAPS_CUBEMAP             | D3DPTEXTURECAPS_COLORKEYBLEND);
1434
1435     Desc7->dpcLineCaps.dwTextureFilterCaps &= (
1436         D3DPTFILTERCAPS_NEAREST          | D3DPTFILTERCAPS_LINEAR              | D3DPTFILTERCAPS_MIPNEAREST          |
1437         D3DPTFILTERCAPS_MIPLINEAR        | D3DPTFILTERCAPS_LINEARMIPNEAREST    | D3DPTFILTERCAPS_LINEARMIPLINEAR     |
1438         D3DPTFILTERCAPS_MINFPOINT        | D3DPTFILTERCAPS_MINFLINEAR          | D3DPTFILTERCAPS_MINFANISOTROPIC     |
1439         D3DPTFILTERCAPS_MIPFPOINT        | D3DPTFILTERCAPS_MIPFLINEAR          | D3DPTFILTERCAPS_MAGFPOINT           |
1440         D3DPTFILTERCAPS_MAGFLINEAR       | D3DPTFILTERCAPS_MAGFANISOTROPIC     | D3DPTFILTERCAPS_MAGFAFLATCUBIC      |
1441         D3DPTFILTERCAPS_MAGFGAUSSIANCUBIC);
1442
1443     Desc7->dpcLineCaps.dwTextureBlendCaps &= (
1444         D3DPTBLENDCAPS_DECAL             | D3DPTBLENDCAPS_MODULATE             | D3DPTBLENDCAPS_DECALALPHA           |
1445         D3DPTBLENDCAPS_MODULATEALPHA     | D3DPTBLENDCAPS_DECALMASK            | D3DPTBLENDCAPS_MODULATEMASK         |
1446         D3DPTBLENDCAPS_COPY              | D3DPTBLENDCAPS_ADD);
1447
1448     Desc7->dpcLineCaps.dwTextureAddressCaps &= (
1449         D3DPTADDRESSCAPS_WRAP            | D3DPTADDRESSCAPS_MIRROR             | D3DPTADDRESSCAPS_CLAMP              |
1450         D3DPTADDRESSCAPS_BORDER          | D3DPTADDRESSCAPS_INDEPENDENTUV);
1451
1452     if(!(Desc7->dpcLineCaps.dwTextureCaps & D3DPTEXTURECAPS_POW2)) {
1453         /* DirectX7 always has the np2 flag set, no matter what the card supports. Some old games(rollcage)
1454          * check the caps incorrectly. If wined3d supports nonpow2 textures it also has np2 conditional support
1455          */
1456         Desc7->dpcLineCaps.dwTextureCaps |= D3DPTEXTURECAPS_POW2 | D3DPTEXTURECAPS_NONPOW2CONDITIONAL;
1457     }
1458     /* Fill the missing members, and do some fixup */
1459     Desc7->dpcLineCaps.dwSize = sizeof(Desc7->dpcLineCaps);
1460     Desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
1461                                             D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
1462                                             D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
1463                                             D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
1464     Desc7->dpcLineCaps.dwStippleWidth = 32;
1465     Desc7->dpcLineCaps.dwStippleHeight = 32;
1466     /* Use the same for the TriCaps */
1467     Desc7->dpcTriCaps = Desc7->dpcLineCaps;
1468
1469     Desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1470     Desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1471     Desc7->dwMinTextureWidth = 1;
1472     Desc7->dwMinTextureHeight = 1;
1473
1474     /* Convert DWORDs safely to WORDs */
1475     if(WCaps.MaxTextureBlendStages > 65535) Desc7->wMaxTextureBlendStages = 65535;
1476     else Desc7->wMaxTextureBlendStages = (WORD) WCaps.MaxTextureBlendStages;
1477     if(WCaps.MaxSimultaneousTextures > 65535) Desc7->wMaxSimultaneousTextures = 65535;
1478     else Desc7->wMaxSimultaneousTextures = (WORD) WCaps.MaxSimultaneousTextures;
1479
1480     if(WCaps.MaxUserClipPlanes > 65535) Desc7->wMaxUserClipPlanes = 65535;
1481     else Desc7->wMaxUserClipPlanes = (WORD) WCaps.MaxUserClipPlanes;
1482     if(WCaps.MaxVertexBlendMatrices > 65535) Desc7->wMaxVertexBlendMatrices = 65535;
1483     else Desc7->wMaxVertexBlendMatrices = (WORD) WCaps.MaxVertexBlendMatrices;
1484
1485     Desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
1486
1487     Desc7->dwReserved1 = 0;
1488     Desc7->dwReserved2 = 0;
1489     Desc7->dwReserved3 = 0;
1490     Desc7->dwReserved4 = 0;
1491
1492     /* Fill the old structure */
1493     memset(Desc123, 0x0, sizeof(D3DDEVICEDESC));
1494     Desc123->dwSize = sizeof(D3DDEVICEDESC);
1495     Desc123->dwFlags = D3DDD_COLORMODEL            |
1496                        D3DDD_DEVCAPS               |
1497                        D3DDD_TRANSFORMCAPS         |
1498                        D3DDD_BCLIPPING             |
1499                        D3DDD_LIGHTINGCAPS          |
1500                        D3DDD_LINECAPS              |
1501                        D3DDD_TRICAPS               |
1502                        D3DDD_DEVICERENDERBITDEPTH  |
1503                        D3DDD_DEVICEZBUFFERBITDEPTH |
1504                        D3DDD_MAXBUFFERSIZE         |
1505                        D3DDD_MAXVERTEXCOUNT;
1506     Desc123->dcmColorModel = D3DCOLOR_RGB;
1507     Desc123->dwDevCaps = Desc7->dwDevCaps;
1508     Desc123->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
1509     Desc123->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1510     Desc123->bClipping = TRUE;
1511     Desc123->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
1512     Desc123->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL | D3DLIGHTCAPS_PARALLELPOINT | D3DLIGHTCAPS_POINT | D3DLIGHTCAPS_SPOT;
1513     Desc123->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1514     Desc123->dlcLightingCaps.dwNumLights = Desc7->dwMaxActiveLights;
1515
1516     Desc123->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
1517     Desc123->dpcLineCaps.dwMiscCaps = Desc7->dpcLineCaps.dwMiscCaps;
1518     Desc123->dpcLineCaps.dwRasterCaps = Desc7->dpcLineCaps.dwRasterCaps;
1519     Desc123->dpcLineCaps.dwZCmpCaps = Desc7->dpcLineCaps.dwZCmpCaps;
1520     Desc123->dpcLineCaps.dwSrcBlendCaps = Desc7->dpcLineCaps.dwSrcBlendCaps;
1521     Desc123->dpcLineCaps.dwDestBlendCaps = Desc7->dpcLineCaps.dwDestBlendCaps;
1522     Desc123->dpcLineCaps.dwShadeCaps = Desc7->dpcLineCaps.dwShadeCaps;
1523     Desc123->dpcLineCaps.dwTextureCaps = Desc7->dpcLineCaps.dwTextureCaps;
1524     Desc123->dpcLineCaps.dwTextureFilterCaps = Desc7->dpcLineCaps.dwTextureFilterCaps;
1525     Desc123->dpcLineCaps.dwTextureBlendCaps = Desc7->dpcLineCaps.dwTextureBlendCaps;
1526     Desc123->dpcLineCaps.dwTextureAddressCaps = Desc7->dpcLineCaps.dwTextureAddressCaps;
1527     Desc123->dpcLineCaps.dwStippleWidth = Desc7->dpcLineCaps.dwStippleWidth;
1528     Desc123->dpcLineCaps.dwAlphaCmpCaps = Desc7->dpcLineCaps.dwAlphaCmpCaps;
1529
1530     Desc123->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
1531     Desc123->dpcTriCaps.dwMiscCaps = Desc7->dpcTriCaps.dwMiscCaps;
1532     Desc123->dpcTriCaps.dwRasterCaps = Desc7->dpcTriCaps.dwRasterCaps;
1533     Desc123->dpcTriCaps.dwZCmpCaps = Desc7->dpcTriCaps.dwZCmpCaps;
1534     Desc123->dpcTriCaps.dwSrcBlendCaps = Desc7->dpcTriCaps.dwSrcBlendCaps;
1535     Desc123->dpcTriCaps.dwDestBlendCaps = Desc7->dpcTriCaps.dwDestBlendCaps;
1536     Desc123->dpcTriCaps.dwShadeCaps = Desc7->dpcTriCaps.dwShadeCaps;
1537     Desc123->dpcTriCaps.dwTextureCaps = Desc7->dpcTriCaps.dwTextureCaps;
1538     Desc123->dpcTriCaps.dwTextureFilterCaps = Desc7->dpcTriCaps.dwTextureFilterCaps;
1539     Desc123->dpcTriCaps.dwTextureBlendCaps = Desc7->dpcTriCaps.dwTextureBlendCaps;
1540     Desc123->dpcTriCaps.dwTextureAddressCaps = Desc7->dpcTriCaps.dwTextureAddressCaps;
1541     Desc123->dpcTriCaps.dwStippleWidth = Desc7->dpcTriCaps.dwStippleWidth;
1542     Desc123->dpcTriCaps.dwAlphaCmpCaps = Desc7->dpcTriCaps.dwAlphaCmpCaps;
1543
1544     Desc123->dwDeviceRenderBitDepth = Desc7->dwDeviceRenderBitDepth;
1545     Desc123->dwDeviceZBufferBitDepth = Desc7->dwDeviceZBufferBitDepth;
1546     Desc123->dwMaxBufferSize = 0;
1547     Desc123->dwMaxVertexCount = 65536;
1548     Desc123->dwMinTextureWidth  = Desc7->dwMinTextureWidth;
1549     Desc123->dwMinTextureHeight = Desc7->dwMinTextureHeight;
1550     Desc123->dwMaxTextureWidth  = Desc7->dwMaxTextureWidth;
1551     Desc123->dwMaxTextureHeight = Desc7->dwMaxTextureHeight;
1552     Desc123->dwMinStippleWidth  = 1;
1553     Desc123->dwMinStippleHeight = 1;
1554     Desc123->dwMaxStippleWidth  = 32;
1555     Desc123->dwMaxStippleHeight = 32;
1556     Desc123->dwMaxTextureRepeat = Desc7->dwMaxTextureRepeat;
1557     Desc123->dwMaxTextureAspectRatio = Desc7->dwMaxTextureAspectRatio;
1558     Desc123->dwMaxAnisotropy = Desc7->dwMaxAnisotropy;
1559     Desc123->dvGuardBandLeft = Desc7->dvGuardBandLeft;
1560     Desc123->dvGuardBandRight = Desc7->dvGuardBandRight;
1561     Desc123->dvGuardBandTop = Desc7->dvGuardBandTop;
1562     Desc123->dvGuardBandBottom = Desc7->dvGuardBandBottom;
1563     Desc123->dvExtentsAdjust = Desc7->dvExtentsAdjust;
1564     Desc123->dwStencilCaps = Desc7->dwStencilCaps;
1565     Desc123->dwFVFCaps = Desc7->dwFVFCaps;
1566     Desc123->dwTextureOpCaps = Desc7->dwTextureOpCaps;
1567     Desc123->wMaxTextureBlendStages = Desc7->wMaxTextureBlendStages;
1568     Desc123->wMaxSimultaneousTextures = Desc7->wMaxSimultaneousTextures;
1569
1570     return DD_OK;
1571 }
1572 /*****************************************************************************
1573  * IDirect3D vtables in various versions
1574  *****************************************************************************/
1575
1576 const IDirect3DVtbl IDirect3D1_Vtbl =
1577 {
1578     /*** IUnknown methods ***/
1579     Thunk_IDirect3DImpl_1_QueryInterface,
1580     Thunk_IDirect3DImpl_1_AddRef,
1581     Thunk_IDirect3DImpl_1_Release,
1582     /*** IDirect3D methods ***/
1583     IDirect3DImpl_1_Initialize,
1584     Thunk_IDirect3DImpl_1_EnumDevices,
1585     Thunk_IDirect3DImpl_1_CreateLight,
1586     Thunk_IDirect3DImpl_1_CreateMaterial,
1587     Thunk_IDirect3DImpl_1_CreateViewport,
1588     Thunk_IDirect3DImpl_1_FindDevice
1589 };
1590
1591 const IDirect3D2Vtbl IDirect3D2_Vtbl =
1592 {
1593     /*** IUnknown methods ***/
1594     Thunk_IDirect3DImpl_2_QueryInterface,
1595     Thunk_IDirect3DImpl_2_AddRef,
1596     Thunk_IDirect3DImpl_2_Release,
1597     /*** IDirect3D2 methods ***/
1598     Thunk_IDirect3DImpl_2_EnumDevices,
1599     Thunk_IDirect3DImpl_2_CreateLight,
1600     Thunk_IDirect3DImpl_2_CreateMaterial,
1601     Thunk_IDirect3DImpl_2_CreateViewport,
1602     Thunk_IDirect3DImpl_2_FindDevice,
1603     Thunk_IDirect3DImpl_2_CreateDevice
1604 };
1605
1606 const IDirect3D3Vtbl IDirect3D3_Vtbl =
1607 {
1608     /*** IUnknown methods ***/
1609     Thunk_IDirect3DImpl_3_QueryInterface,
1610     Thunk_IDirect3DImpl_3_AddRef,
1611     Thunk_IDirect3DImpl_3_Release,
1612     /*** IDirect3D3 methods ***/
1613     IDirect3DImpl_3_EnumDevices,
1614     IDirect3DImpl_3_CreateLight,
1615     IDirect3DImpl_3_CreateMaterial,
1616     IDirect3DImpl_3_CreateViewport,
1617     IDirect3DImpl_3_FindDevice,
1618     Thunk_IDirect3DImpl_3_CreateDevice,
1619     Thunk_IDirect3DImpl_3_CreateVertexBuffer,
1620     Thunk_IDirect3DImpl_3_EnumZBufferFormats,
1621     Thunk_IDirect3DImpl_3_EvictManagedTextures
1622 };
1623
1624 const IDirect3D7Vtbl IDirect3D7_Vtbl =
1625 {
1626     /*** IUnknown methods ***/
1627     Thunk_IDirect3DImpl_7_QueryInterface,
1628     Thunk_IDirect3DImpl_7_AddRef,
1629     Thunk_IDirect3DImpl_7_Release,
1630     /*** IDirect3D7 methods ***/
1631     IDirect3DImpl_7_EnumDevices,
1632     IDirect3DImpl_7_CreateDevice,
1633     IDirect3DImpl_7_CreateVertexBuffer,
1634     IDirect3DImpl_7_EnumZBufferFormats,
1635     IDirect3DImpl_7_EvictManagedTextures
1636 };