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