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