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