Assorted spelling fixes.
[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 "winnls.h"
33 #include "winerror.h"
34 #include "wingdi.h"
35 #include "wine/exception.h"
36 #include "excpt.h"
37
38 #include "ddraw.h"
39 #include "d3d.h"
40
41 #include "ddraw_private.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
44
45 /*****************************************************************************
46  * IDirect3D7::QueryInterface
47  *
48  * QueryInterface implementation with thunks to IDirectDraw7
49  *
50  *****************************************************************************/
51 static HRESULT WINAPI
52 Thunk_IDirect3DImpl_7_QueryInterface(IDirect3D7 *iface,
53                                     REFIID refiid,
54                                     void **obj)
55 {
56     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
57     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
58
59     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
60                                        refiid,
61                                        obj);
62 }
63
64 static HRESULT WINAPI
65 Thunk_IDirect3DImpl_3_QueryInterface(IDirect3D3 *iface,
66                                     REFIID refiid,
67                                     void **obj)
68 {
69     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
70     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
71
72     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
73                                        refiid,
74                                        obj);
75 }
76
77 static HRESULT WINAPI
78 Thunk_IDirect3DImpl_2_QueryInterface(IDirect3D2 *iface,
79                                     REFIID refiid,
80                                     void **obj)
81 {
82     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
83     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
84
85     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
86                                        refiid,
87                                        obj);
88 }
89
90 static HRESULT WINAPI
91 Thunk_IDirect3DImpl_1_QueryInterface(IDirect3D *iface,
92                                     REFIID refiid,
93                                     void **obj)
94 {
95     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
96     TRACE("(%p)->(%s,%p): Thunking to IDirectDraw7\n", This, debugstr_guid(refiid), obj);
97
98     return IDirectDraw7_QueryInterface(ICOM_INTERFACE(This, IDirectDraw7),
99                                        refiid,
100                                        obj);
101 }
102
103 /*****************************************************************************
104  * IDirect3D7::AddRef
105  *
106  * DirectDraw refcounting is a bit odd. Every version of the ddraw interface
107  * has its own refcount, but IDirect3D 1/2/3 refcounts are linked to
108  * IDirectDraw, and IDirect3D7 is linked to IDirectDraw7
109  *
110  * IDirect3D7 -> IDirectDraw7
111  * IDirect3D3 -> IDirectDraw
112  * IDirect3D2 -> IDirectDraw
113  * IDirect3D  -> IDirectDraw
114  *
115  * So every AddRef implementation thunks to a different interface, and the
116  * IDirectDrawX::AddRef implementations have different counters...
117  *
118  * Returns
119  *  The new refcount
120  *
121  *****************************************************************************/
122 static ULONG WINAPI
123 Thunk_IDirect3DImpl_7_AddRef(IDirect3D7 *iface)
124 {
125     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
126     TRACE("(%p) : Thunking to IDirectDraw7.\n", This);
127
128     return IDirectDraw7_AddRef(ICOM_INTERFACE(This, IDirectDraw7));
129 }
130
131 static ULONG WINAPI
132 Thunk_IDirect3DImpl_3_AddRef(IDirect3D3 *iface)
133 {
134     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
135     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
136
137     return IDirectDraw_AddRef(ICOM_INTERFACE(This, IDirectDraw));
138 }
139
140 static ULONG WINAPI
141 Thunk_IDirect3DImpl_2_AddRef(IDirect3D2 *iface)
142 {
143     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
144     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
145
146     return IDirectDraw_AddRef(ICOM_INTERFACE(This, IDirectDraw));
147 }
148
149 static ULONG WINAPI
150 Thunk_IDirect3DImpl_1_AddRef(IDirect3D *iface)
151 {
152     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
153     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
154
155     return IDirectDraw_AddRef(ICOM_INTERFACE(This, IDirectDraw));
156 }
157
158 /*****************************************************************************
159  * IDirect3D7::Release
160  *
161  * Same story as IDirect3D7::AddRef
162  *
163  * Returns: The new refcount
164  *
165  *****************************************************************************/
166 static ULONG WINAPI
167 Thunk_IDirect3DImpl_7_Release(IDirect3D7 *iface)
168 {
169     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
170     TRACE("(%p) : Thunking to IDirectDraw7.\n", This);
171
172     return IDirectDraw7_Release(ICOM_INTERFACE(This, IDirectDraw7));
173 }
174
175 static ULONG WINAPI
176 Thunk_IDirect3DImpl_3_Release(IDirect3D3 *iface)
177 {
178     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
179     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
180
181     return IDirectDraw_Release(ICOM_INTERFACE(This, IDirectDraw));
182 }
183
184 static ULONG WINAPI
185 Thunk_IDirect3DImpl_2_Release(IDirect3D2 *iface)
186 {
187     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
188     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
189
190     return IDirectDraw_Release(ICOM_INTERFACE(This, IDirectDraw));
191 }
192
193 static ULONG WINAPI
194 Thunk_IDirect3DImpl_1_Release(IDirect3D *iface)
195 {
196     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
197     TRACE("(%p) : Thunking to IDirectDraw.\n", This);
198
199     return IDirectDraw_Release(ICOM_INTERFACE(This, IDirectDraw));
200 }
201
202 /*****************************************************************************
203  * IDirect3D Methods
204  *****************************************************************************/
205
206 /*****************************************************************************
207  * IDirect3D::Initialize
208  *
209  * Initializes the IDirect3D interface. This is a no-op implementation,
210  * as all initialization is done at create time.
211  *
212  * Version 1
213  *
214  * Params:
215  *  refiid: ?
216  *
217  * Returns:
218  *  D3D_OK, because it's a no-op
219  *
220  *****************************************************************************/
221 static HRESULT WINAPI
222 IDirect3DImpl_1_Initialize(IDirect3D *iface,
223                            REFIID refiid)
224 {
225     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
226
227     TRACE("(%p)->(%s) no-op...\n", This, debugstr_guid(refiid));
228     return D3D_OK;
229 }
230
231 /*****************************************************************************
232  * IDirect3D7::EnumDevices
233  *
234  * The EnumDevices method for IDirect3D7. It enumerates all supported
235  * D3D7 devices. Currently there's only one.
236  *
237  * Params:
238  *  Callback: Function to call for each enumerated device
239  *  Context: Pointer to pass back to the app
240  *
241  * Returns:
242  *  D3D_OK, or the return value of the GetCaps call
243  *
244  *****************************************************************************/
245 static HRESULT WINAPI
246 IDirect3DImpl_7_EnumDevices(IDirect3D7 *iface,
247                           LPD3DENUMDEVICESCALLBACK7 Callback,
248                           void *Context)
249 {
250     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
251     char interface_name[] = "WINE Direct3D7 using WineD3D";
252     char device_name[] = "Wine D3D7 device";
253     D3DDEVICEDESC7 ddesc;
254     D3DDEVICEDESC oldDesc;
255     HRESULT hr;
256
257     TRACE("(%p)->(%p,%p)\n", This, Callback, Context);
258
259     TRACE("(%p) Enumerating WineD3D D3Device7 interface\n", This);
260     hr = IDirect3DImpl_GetCaps(This->wineD3D, &oldDesc, &ddesc);
261     if(hr != D3D_OK) return hr;
262     Callback(interface_name, device_name, &ddesc, Context);
263
264     TRACE("(%p) End of enumeration\n", This);
265     return D3D_OK;
266 }
267
268 /*****************************************************************************
269  * IDirect3D3::EnumDevices
270  *
271  * Enumerates all supported Direct3DDevice interfaces. This is the
272  * implementation for Direct3D 1 to Direc3D 3, Version 7 has its own.
273  *
274  * Version 1, 2 and 3
275  *
276  * Params:
277  *  Callback: Application-provided routine to call for each enumerated device
278  *  Context: Pointer to pass to the callback
279  *
280  * Returns:
281  *  D3D_OK on success,
282  *  The result of IDirect3DImpl_GetCaps if it failed
283  *
284  *****************************************************************************/
285 static HRESULT WINAPI
286 IDirect3DImpl_3_EnumDevices(IDirect3D3 *iface,
287                             LPD3DENUMDEVICESCALLBACK Callback,
288                             void *Context)
289 {
290     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
291     D3DDEVICEDESC dref, d1, d2;
292     D3DDEVICEDESC7 newDesc;
293     HRESULT hr;
294
295     /* Some games (Motoracer 2 demo) have the bad idea to modify the device name string.
296        Let's put the string in a sufficiently sized array in writable memory. */
297     char device_name[50];
298     strcpy(device_name,"direct3d");
299
300     TRACE("(%p)->(%p,%p)\n", This, Callback, Context);
301
302     hr = IDirect3DImpl_GetCaps(This->wineD3D, &dref, &newDesc);
303     if(hr != D3D_OK) return hr;
304
305     /* Do I have to enumerate the reference id? Note from old d3d7:
306      * "It seems that enumerating the reference IID on Direct3D 1 games
307      * (AvP / Motoracer2) breaks them". So do not enumerate this iid in V1
308      *
309      * There's a registry key HKLM\Software\Microsoft\Direct3D\Drivers, EnumReference
310      * which enables / disables enumerating the reference rasterizer. It's a DWORD,
311      * 0 means disabled, 2 means enabled. The enablerefrast.reg and disablerefrast.reg
312      * files in the DirectX 7.0 sdk demo directory suggest this.
313      *
314      * Some games(GTA 2) seem to use the second enumerated device, so I have to enumerate
315      * at least 2 devices. So enumerate the reference device to have 2 devices.
316      */
317
318     if(This->d3dversion != 1)
319     {
320         TRACE("(%p) Enumerating WineD3D D3DDevice interface\n", This);
321         d1 = dref;
322         d2 = dref;
323         hr = Callback( (LPIID) &IID_IDirect3DRefDevice, "Reference Direct3D ID", device_name, &d1, &d2, Context);
324         if(hr != D3DENUMRET_OK)
325         {
326             TRACE("Application cancelled the enumeration\n");
327             return D3D_OK;
328         }
329     }
330
331     TRACE("(%p) Enumerating WineD3D D3DDevice interface\n", This);
332     d1 = dref;
333     d2 = dref;
334     hr = Callback( (LPIID) &IID_D3DDEVICE_WineD3D, "Wine D3DDevice using WineD3D and OpenGL", device_name, &d1, &d2, Context);
335     if(hr != D3DENUMRET_OK)
336     {
337         TRACE("Application cancelled the enumeration\n");
338         return D3D_OK;
339     }
340     TRACE("(%p) End of enumeration\n", This);
341
342     return D3D_OK;
343 }
344
345 static HRESULT WINAPI
346 Thunk_IDirect3DImpl_2_EnumDevices(IDirect3D2 *iface,
347                                   LPD3DENUMDEVICESCALLBACK Callback,
348                                   void *Context)
349 {
350     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
351     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Callback, Context);
352     return IDirect3D3_EnumDevices(ICOM_INTERFACE(This, IDirect3D3),
353                                   Callback,
354                                   Context);
355 }
356
357 static HRESULT WINAPI
358 Thunk_IDirect3DImpl_1_EnumDevices(IDirect3D *iface,
359                                   LPD3DENUMDEVICESCALLBACK Callback,
360                                   void *Context)
361 {
362     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
363     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Callback, Context);
364     return IDirect3D3_EnumDevices(ICOM_INTERFACE(This, IDirect3D3),
365                                   Callback,
366                                   Context);
367 }
368
369 /*****************************************************************************
370  * IDirect3D3::CreateLight
371  *
372  * Creates an IDirect3DLight interface. This interface is used in
373  * Direct3D3 or earlier for lighting. In Direct3D7 it has been replaced
374  * by the DIRECT3DLIGHT7 structure. Wine's Direct3DLight implementation
375  * uses the IDirect3DDevice7 interface with D3D7 lights.
376  *
377  * Version 1, 2 and 3
378  *
379  * Params:
380  *  Light: Address to store the new interface pointer
381  *  UnkOuter: Basically for aggregation, but ddraw doesn't support it.
382  *            Must be NULL
383  *
384  * Returns:
385  *  D3D_OK on success
386  *  DDERR_OUTOFMEMORY if memory allocation failed
387  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
388  *
389  *****************************************************************************/
390 static HRESULT WINAPI
391 IDirect3DImpl_3_CreateLight(IDirect3D3 *iface,
392                             IDirect3DLight **Light,
393                             IUnknown *UnkOuter )
394 {
395     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
396     IDirect3DLightImpl *object;
397
398     TRACE("(%p)->(%p,%p)\n", This, Light, UnkOuter);
399
400     if(UnkOuter)
401         return CLASS_E_NOAGGREGATION;
402
403     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightImpl));
404     if (object == NULL)
405         return DDERR_OUTOFMEMORY;
406
407     ICOM_INIT_INTERFACE(object, IDirect3DLight, IDirect3DLight_Vtbl);
408     object->ref = 1;
409     object->ddraw = This;
410     object->next = NULL;
411     object->active_viewport = NULL;
412
413     /* Update functions */
414     object->activate = light_update;
415     object->desactivate = light_activate;
416     object->update = light_desactivate;
417     object->active_viewport = NULL;
418
419     *Light = ICOM_INTERFACE(object, IDirect3DLight);
420
421     TRACE("(%p) creating implementation at %p.\n", This, object);
422
423     return D3D_OK;
424 }
425
426 static HRESULT WINAPI
427 Thunk_IDirect3DImpl_2_CreateLight(IDirect3D2 *iface,
428                                   IDirect3DLight **Direct3DLight,
429                                   IUnknown *UnkOuter)
430 {
431     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
432     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DLight, UnkOuter);
433     return IDirect3D3_CreateLight(ICOM_INTERFACE(This, IDirect3D3),
434                                   Direct3DLight,
435                                   UnkOuter);
436 }
437
438 static HRESULT WINAPI
439 Thunk_IDirect3DImpl_1_CreateLight(IDirect3D *iface,
440                                   IDirect3DLight **Direct3DLight,
441                                   IUnknown *UnkOuter)
442 {
443     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
444     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DLight, UnkOuter);
445     return IDirect3D3_CreateLight(ICOM_INTERFACE(This, IDirect3D3),
446                                   Direct3DLight,
447                                   UnkOuter);
448 }
449
450 /*****************************************************************************
451  * IDirect3D3::CreateMaterial
452  *
453  * Creates an IDirect3DMaterial interface. This interface is used by Direct3D3
454  * and older versions. The IDirect3DMaterial implementation wraps its
455  * functionality to IDirect3DDevice7::SetMaterial and friends.
456  *
457  * Version 1, 2 and 3
458  *
459  * Params:
460  *  Material: Address to store the new interface's pointer to
461  *  UnkOuter: Basically for aggregation, but ddraw doesn't support it.
462  *            Must be NULL
463  *
464  * Returns:
465  *  D3D_OK on success
466  *  DDERR_OUTOFMEMORY if memory allocation failed
467  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
468  *
469  *****************************************************************************/
470 static HRESULT WINAPI
471 IDirect3DImpl_3_CreateMaterial(IDirect3D3 *iface,
472                                IDirect3DMaterial3 **Material,
473                                IUnknown *UnkOuter )
474 {
475     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
476     IDirect3DMaterialImpl *object;
477
478     TRACE("(%p)->(%p,%p)\n", This, Material, UnkOuter);
479
480     if(UnkOuter)
481         return CLASS_E_NOAGGREGATION;
482
483     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DMaterialImpl));
484     if (object == NULL)
485         return DDERR_OUTOFMEMORY;
486
487     ICOM_INIT_INTERFACE(object, IDirect3DMaterial3, IDirect3DMaterial3_Vtbl);
488     ICOM_INIT_INTERFACE(object, IDirect3DMaterial2, IDirect3DMaterial2_Vtbl);
489     ICOM_INIT_INTERFACE(object, IDirect3DMaterial, IDirect3DMaterial_Vtbl);
490     object->ref = 1;
491     object->ddraw = This;
492     object->activate = material_activate;
493
494     *Material = ICOM_INTERFACE(object, IDirect3DMaterial3);
495
496     TRACE("(%p) creating implementation at %p.\n", This, object);
497
498     return D3D_OK;
499 }
500
501 static HRESULT WINAPI
502 Thunk_IDirect3DImpl_2_CreateMaterial(IDirect3D2 *iface,
503                                      IDirect3DMaterial2 **Direct3DMaterial,
504                                      IUnknown* UnkOuter)
505 {
506     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
507     HRESULT ret;
508     IDirect3DMaterial3 *ret_val;
509
510     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DMaterial, UnkOuter);
511     ret = IDirect3D3_CreateMaterial(ICOM_INTERFACE(This, IDirect3D3),
512                                     &ret_val,
513                                     UnkOuter);
514
515     *Direct3DMaterial = COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial3, IDirect3DMaterial2, ret_val);
516
517     TRACE(" returning interface %p.\n", *Direct3DMaterial);
518
519     return ret;
520 }
521
522 static HRESULT WINAPI
523 Thunk_IDirect3DImpl_1_CreateMaterial(IDirect3D *iface,
524                                      IDirect3DMaterial **Direct3DMaterial,
525                                      IUnknown* UnkOuter)
526 {
527     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
528     HRESULT ret;
529     LPDIRECT3DMATERIAL3 ret_val;
530
531     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, Direct3DMaterial, UnkOuter);
532     ret = IDirect3D3_CreateMaterial(ICOM_INTERFACE(This, IDirect3D3),
533                                     &ret_val,
534                                     UnkOuter);
535
536     *Direct3DMaterial = COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial3, IDirect3DMaterial, ret_val);
537
538     TRACE(" returning interface %p.\n", *Direct3DMaterial);
539
540     return ret;
541 }
542
543 /*****************************************************************************
544  * IDirect3D3::CreateViewport
545  *
546  * Creates an IDirect3DViewport interface. This interface is used
547  * by Direct3D and earlier versions for Viewport management. In Direct3D7
548  * it has been replaced by a viewport structure and
549  * IDirect3DDevice7::*Viewport. Wine's IDirect3DViewport implementation
550  * uses the IDirect3DDevice7 methods for its functionality
551  *
552  * Params:
553  *  Viewport: Address to store the new interface pointer
554  *  UnkOuter: Basically for aggregation, but ddraw doesn't support it.
555  *            Must be NULL
556  *
557  * Returns:
558  *  D3D_OK on success
559  *  DDERR_OUTOFMEMORY if memory allocation failed
560  *  CLASS_E_NOAGGREGATION if UnkOuter != NULL
561  *
562  *****************************************************************************/
563 static HRESULT WINAPI
564 IDirect3DImpl_3_CreateViewport(IDirect3D3 *iface,
565                               IDirect3DViewport3 **Viewport,
566                               IUnknown *UnkOuter )
567 {
568     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
569     IDirect3DViewportImpl *object;
570
571     if(UnkOuter)
572         return CLASS_E_NOAGGREGATION;
573
574     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DViewportImpl));
575     if (object == NULL)
576         return DDERR_OUTOFMEMORY;
577
578     ICOM_INIT_INTERFACE(object, IDirect3DViewport3, IDirect3DViewport3_Vtbl);
579     object->ref = 1;
580     object->ddraw = This;
581     object->activate = viewport_activate;
582     object->use_vp2 = 0xFF;
583     object->next = NULL;
584     object->lights = NULL;
585     object->num_lights = 0;
586     object->map_lights = 0;
587
588     *Viewport = ICOM_INTERFACE(object, IDirect3DViewport3);
589
590     TRACE("(%p) creating implementation at %p.\n",This, object);
591
592     return D3D_OK;
593 }
594
595 static HRESULT WINAPI
596 Thunk_IDirect3DImpl_2_CreateViewport(IDirect3D2 *iface,
597                                      IDirect3DViewport2 **D3DViewport2,
598                                      IUnknown *UnkOuter)
599 {
600     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
601     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, D3DViewport2, UnkOuter);
602
603     return IDirect3D3_CreateViewport(ICOM_INTERFACE(This, IDirect3D3),
604                                      (IDirect3DViewport3 **) D3DViewport2 /* No need to cast here */,
605                                      UnkOuter);
606 }
607
608 static HRESULT WINAPI
609 Thunk_IDirect3DImpl_1_CreateViewport(IDirect3D *iface,
610                                      IDirect3DViewport **D3DViewport,
611                                      IUnknown* UnkOuter)
612 {
613     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
614     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, D3DViewport, UnkOuter);
615
616     return IDirect3D3_CreateViewport(ICOM_INTERFACE(This, IDirect3D3),
617                                      (IDirect3DViewport3 **) D3DViewport /* No need to cast here */,
618                                      UnkOuter);
619 }
620
621 /*****************************************************************************
622  * IDirect3D3::FindDevice
623  *
624  * This method finds a device with the requested properties and returns a
625  * device description
626  *
627  * Verion 1, 2 and 3
628  * Params:
629  *  D3DDFS: Describes the requested device characteristics
630  *  D3DFDR: Returns the device description
631  *
632  * Returns:
633  *  D3D_OK on success
634  *  DDERR_INVALIDPARAMS if no device was found
635  *
636  *****************************************************************************/
637 static HRESULT WINAPI
638 IDirect3DImpl_3_FindDevice(IDirect3D3 *iface,
639                            D3DFINDDEVICESEARCH *D3DDFS,
640                            D3DFINDDEVICERESULT *D3DFDR)
641 {
642     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
643     D3DDEVICEDESC desc;
644     D3DDEVICEDESC7 newDesc;
645     HRESULT hr;
646
647     TRACE("(%p)->(%p,%p)\n", This, D3DDFS, D3DFDR);
648
649     if ((D3DDFS->dwFlags & D3DFDS_COLORMODEL) &&
650         (D3DDFS->dcmColorModel != D3DCOLOR_RGB))
651     {
652         TRACE(" trying to request a non-RGB D3D color model. Not supported.\n");
653         return DDERR_INVALIDPARAMS; /* No real idea what to return here :-) */
654     }
655     if (D3DDFS->dwFlags & D3DFDS_GUID)
656     {
657         TRACE(" trying to match guid %s.\n", debugstr_guid(&(D3DDFS->guid)));
658         if ((IsEqualGUID(&IID_D3DDEVICE_WineD3D, &(D3DDFS->guid)) == 0) &&
659             (IsEqualGUID(&IID_IDirect3DHALDevice, &(D3DDFS->guid)) == 0) &&
660             (IsEqualGUID(&IID_IDirect3DRefDevice, &(D3DDFS->guid)) == 0))
661         {
662             TRACE(" no match for this GUID.\n");
663             return DDERR_INVALIDPARAMS;
664         }
665     }
666
667     /* Get the caps */
668     hr = IDirect3DImpl_GetCaps(This->wineD3D, &desc, &newDesc);
669     if(hr != D3D_OK) return hr;
670
671     /* Now return our own GUID */
672     D3DFDR->guid = IID_D3DDEVICE_WineD3D;
673     D3DFDR->ddHwDesc = desc;
674     D3DFDR->ddSwDesc = desc;
675
676     TRACE(" returning Wine's WineD3D device with (undumped) capabilities\n");
677
678     return D3D_OK;
679 }
680
681 static HRESULT WINAPI
682 Thunk_IDirect3DImpl_2_FindDevice(IDirect3D2 *iface,
683                                  D3DFINDDEVICESEARCH *D3DDFS,
684                                  D3DFINDDEVICERESULT *D3DFDR)
685 {
686     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
687     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", iface, D3DDFS, D3DFDR);
688     return IDirect3D3_FindDevice(ICOM_INTERFACE(This, IDirect3D3),
689                                  D3DDFS,
690                                  D3DFDR);
691 }
692
693 static HRESULT WINAPI
694 Thunk_IDirect3DImpl_1_FindDevice(IDirect3D *iface,
695                                 D3DFINDDEVICESEARCH *D3DDFS,
696                                 D3DFINDDEVICERESULT *D3DDevice)
697 {
698     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D, iface);
699     TRACE("(%p)->(%p,%p) thunking to IDirect3D3 interface.\n", This, D3DDFS, D3DDevice);
700     return IDirect3D3_FindDevice(ICOM_INTERFACE(This, IDirect3D3),
701                                  D3DDFS,
702                                  D3DDevice);
703 }
704
705 /*****************************************************************************
706  * IDirect3D7::CreateDevice
707  *
708  * Creates an IDirect3DDevice7 interface.
709  *
710  * Version 2, 3 and 7. IDirect3DDevice 1 interfaces are interfaces to
711  * DirectDraw surfaces and are created with
712  * IDirectDrawSurface::QueryInterface. This method uses CreateDevice to
713  * create the device object and QueryInterfaces for IDirect3DDevice
714  *
715  * Params:
716  *  refiid: IID of the device to create
717  *  Surface: Inititial rendertarget
718  *  Device: Address to return the interface pointer
719  *
720  * Returns:
721  *  D3D_OK on success
722  *  DDERR_OUTOFMEMORY if memory allocation failed
723  *  DDERR_INVALIDPARAMS if a device exists already
724  *
725  *****************************************************************************/
726 static HRESULT WINAPI
727 IDirect3DImpl_7_CreateDevice(IDirect3D7 *iface,
728                              REFCLSID refiid,
729                              IDirectDrawSurface7 *Surface,
730                              IDirect3DDevice7 **Device)
731 {
732     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
733     IDirect3DDeviceImpl *object;
734     IParentImpl *IndexBufferParent;
735     HRESULT hr;
736     IDirectDrawSurfaceImpl *target = ICOM_OBJECT(IDirectDrawSurfaceImpl, IDirectDrawSurface7, Surface);
737     TRACE("(%p)->(%s,%p,%p)\n", iface, debugstr_guid(refiid), Surface, Device);
738
739     *Device = NULL;
740
741     /* Fail device creation if non-opengl surfaces are used */
742     if(This->ImplType != SURFACE_OPENGL)
743     {
744         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");
745
746         /* We only hit this path if a default surface is set in the registry. Incorrect autodetection
747          * is caught in CreateSurface or QueryInterface
748          */
749         return DDERR_NO3D;
750     }
751
752     /* So far we can only create one device per ddraw object */
753     if(This->d3ddevice)
754     {
755         FIXME("(%p): Only one Direct3D device per DirectDraw object supported\n", This);
756         return DDERR_INVALIDPARAMS;
757     }
758
759     object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirect3DDeviceImpl));
760     if(!object)
761     {
762         ERR("Out of memory when allocating a IDirect3DDevice implementation\n");
763         return DDERR_OUTOFMEMORY;
764     }
765
766     ICOM_INIT_INTERFACE(object, IDirect3DDevice7, IDirect3DDevice7_Vtbl);
767     ICOM_INIT_INTERFACE(object, IDirect3DDevice3, IDirect3DDevice3_Vtbl);
768     ICOM_INIT_INTERFACE(object, IDirect3DDevice2, IDirect3DDevice2_Vtbl);
769     ICOM_INIT_INTERFACE(object, IDirect3DDevice, IDirect3DDevice1_Vtbl);
770
771     object->ref = 1;
772     object->ddraw = This;
773     object->viewport_list = NULL;
774     object->current_viewport = NULL;
775     object->material = 0;
776     object->target = target;
777
778     object->Handles = NULL;
779     object->numHandles = 0;
780
781     /* This is for convenience */
782     object->wineD3DDevice = This->wineD3DDevice;
783
784     /* Create an index buffer, it's needed for indexed drawing */
785     IndexBufferParent = HeapAlloc(GetProcessHeap(), 0, sizeof(IParentImpl *));
786     if(!IndexBufferParent)
787     {
788         ERR("Allocating memory for an index buffer parent failed\n");
789         HeapFree(GetProcessHeap(), 0, object);
790         return DDERR_OUTOFMEMORY;
791     }
792     ICOM_INIT_INTERFACE(IndexBufferParent, IParent, IParent_Vtbl);
793     IndexBufferParent->ref = 1;
794
795     /* Create an Index Buffer. WineD3D needs one for Drawing indexed primitives
796      * Create a (hopefully) long enough buffer, and copy the indices into it
797      * Ideally, a IWineD3DIndexBuffer::SetData method could be created, which
798      * takes the pointer and avoids the memcpy
799      */
800     hr = IWineD3DDevice_CreateIndexBuffer(This->wineD3DDevice,
801                                           0x40000, /* Length. Don't know how long it should be */
802                                           0, /* Usage */
803                                           WINED3DFMT_INDEX16, /* Format. D3D7 uses WORDS */
804                                           WINED3DPOOL_DEFAULT,
805                                           &object->indexbuffer,
806                                           0 /* Handle */,
807                                           (IUnknown *) ICOM_INTERFACE(IndexBufferParent, IParent));
808
809     if(FAILED(hr))
810     {
811         ERR("Failed to create an index buffer\n");
812         HeapFree(GetProcessHeap(), 0, object);
813         return hr;
814     }
815     IndexBufferParent->child = (IUnknown *) object->indexbuffer;
816
817     /* No need to set the indices, it's done when necessary */
818
819     /* AddRef the WineD3D Device */
820     IWineD3DDevice_AddRef(This->wineD3DDevice);
821
822     /* Don't forget to return the interface ;) */
823     *Device = ICOM_INTERFACE(object, IDirect3DDevice7);
824
825     TRACE(" (%p) Created an IDirect3DDeviceImpl object at %p\n", This, object);
826
827     /* This is for apps which create a non-flip, non-d3d primary surface
828      * and an offscreen D3DDEVICE surface, then render to the offscreen surface
829      * and do a Blt from the offscreen to the primary surface.
830      *
831      * Set the offscreen D3DDDEVICE surface(=target) as the back buffer,
832      * and the primary surface(=This->d3d_target) as the front buffer.
833      *
834      * This way the app will render to the D3DDEVICE surface and WineD3D
835      * will catch the Blt was Back Buffer -> Front buffer blt and perform
836      * a flip instead. This way we don't have to deal with a mixed GL / GDI
837      * environment.
838      *
839      * This should be checked against windowed apps. The only app tested with
840      * this is moto racer 2 during the loading screen.
841      */
842     TRACE("Isrendertarget: %s, d3d_target=%p\n", target->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE ? "true" : "false", This->d3d_target);
843     if(!(target->surface_desc.ddsCaps.dwCaps & DDSCAPS_PRIMARYSURFACE) &&
844        (This->d3d_target != target))
845     {
846         TRACE("(%p) Using %p as front buffer, %p as back buffer\n", This, This->d3d_target, target);
847         hr = IWineD3DDevice_SetFrontBackBuffers(This->wineD3DDevice,
848                                                 This->d3d_target->WineD3DSurface,
849                                                 target->WineD3DSurface);
850         if(hr != D3D_OK)
851             ERR("(%p) Error %08lx setting the front and back buffer\n", This, hr);
852
853         object->OffScreenTarget = TRUE;
854     }
855     else
856     {
857         object->OffScreenTarget = FALSE;
858     }
859
860     /* AddRef the render target. Also AddRef the render target from ddraw,
861      * because if it is released before the app releases the D3D device, the D3D capabilities
862      * of WineD3D will be uninitialized, which has bad effects.
863      *
864      * In most cases, those surfaces are the surfaces are the same anyway, but this will simply
865      * add another ref which is released when the device is destroyed.
866      */
867     IDirectDrawSurface7_AddRef(Surface);
868     IDirectDrawSurface7_AddRef(ICOM_INTERFACE(This->d3d_target, IDirectDrawSurface7));
869
870     This->d3ddevice = object;
871
872     return D3D_OK;
873 }
874
875 static HRESULT WINAPI
876 Thunk_IDirect3DImpl_3_CreateDevice(IDirect3D3 *iface,
877                                    REFCLSID refiid,
878                                    IDirectDrawSurface4 *Surface,
879                                    IDirect3DDevice3 **Device,
880                                    IUnknown *UnkOuter)
881 {
882     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
883     HRESULT hr;
884     TRACE("(%p)->(%s,%p,%p,%p): Thunking to IDirect3D7\n", This, debugstr_guid(refiid), Surface, Device, UnkOuter);
885
886     if(UnkOuter != NULL)
887         return CLASS_E_NOAGGREGATION;
888
889     hr =  IDirect3D7_CreateDevice(ICOM_INTERFACE(This, IDirect3D7),
890                                   refiid,
891                                   (IDirectDrawSurface7 *) Surface /* Same VTables */,
892                                   (IDirect3DDevice7 **) Device);
893
894     *Device = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice3, *Device);
895     return hr;
896 }
897
898 static HRESULT WINAPI
899 Thunk_IDirect3DImpl_2_CreateDevice(IDirect3D2 *iface,
900                                    REFCLSID refiid,
901                                    IDirectDrawSurface *Surface,
902                                    IDirect3DDevice2 **Device)
903 {
904     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D2, iface);
905     HRESULT hr;
906     TRACE("(%p)->(%s,%p,%p): Thunking to IDirect3D7\n", This, debugstr_guid(refiid), Surface, Device);
907
908     hr =  IDirect3D7_CreateDevice(ICOM_INTERFACE(This, IDirect3D7),
909                                   refiid,
910                                   COM_INTERFACE_CAST(IDirectDrawSurfaceImpl, IDirectDrawSurface3, IDirectDrawSurface7, Surface),
911                                   (IDirect3DDevice7 **) Device);
912
913     *Device = COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice7, IDirect3DDevice2, *Device);
914     return hr;
915 }
916
917 /*****************************************************************************
918  * IDirect3D7::CreateVertexBuffer
919  *
920  * Creates a new vertex buffer object and returns a IDirect3DVertexBuffer7
921  * interface.
922  *
923  * Version 3 and 7
924  *
925  * Params:
926  *  Desc: Requested Vertex buffer properties
927  *  VertexBuffer: Address to return the interface pointer at
928  *  Flags: Some flags, must be 0
929  *
930  * Returns
931  *  D3D_OK on success
932  *  DDERR_OUTOFMEMORY if memory allocation failed
933  *  The return value of IWineD3DDevice::CreateVertexBuffer if this call fails
934  *  DDERR_INVALIDPARAMS if Desc or VertexBuffer are NULL, or Flags != 0
935  *
936  *****************************************************************************/
937 static HRESULT WINAPI
938 IDirect3DImpl_7_CreateVertexBuffer(IDirect3D7 *iface,
939                                    D3DVERTEXBUFFERDESC *Desc,
940                                    IDirect3DVertexBuffer7 **VertexBuffer,
941                                    DWORD Flags)
942 {
943     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
944     IDirect3DVertexBufferImpl *object;
945     HRESULT hr;
946     TRACE("(%p)->(%p,%p,%08lx)\n", This, Desc, VertexBuffer, Flags);
947
948     TRACE("(%p) Vertex buffer description:\n", This);
949     TRACE("(%p)  dwSize=%ld\n", This, Desc->dwSize);
950     TRACE("(%p)  dwCaps=%08lx\n", This, Desc->dwCaps);
951     TRACE("(%p)  FVF=%08lx\n", This, Desc->dwFVF);
952     TRACE("(%p)  dwNumVertices=%ld\n", This, Desc->dwNumVertices);
953
954     /* D3D7 SDK: "No Flags are currently defined for this method. This
955      * parameter must be 0"
956      *
957      * Never trust the documentation - this is wrong
958     if(Flags != 0)
959     {
960         ERR("(%p) Flags is %08lx, returning DDERR_INVALIDPARAMS\n", This, Flags);
961         return DDERR_INVALIDPARAMS;
962     }
963      */
964
965     /* Well, this sounds sane */
966     if( (!VertexBuffer) || (!Desc) )
967         return DDERR_INVALIDPARAMS;
968
969     /* Now create the vertex buffer */
970     object = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirect3DVertexBufferImpl));
971     if(!object)
972     {
973         ERR("(%p) Out of memory when allocating a IDirect3DVertexBufferImpl structure\n", This);
974         return DDERR_OUTOFMEMORY;
975     }
976
977     object->ref = 1;
978     ICOM_INIT_INTERFACE(object, IDirect3DVertexBuffer7, IDirect3DVertexBuffer7_Vtbl);
979     ICOM_INIT_INTERFACE(object, IDirect3DVertexBuffer, IDirect3DVertexBuffer1_Vtbl);
980
981     object->Caps = Desc->dwCaps;
982
983     hr = IWineD3DDevice_CreateVertexBuffer(This->wineD3DDevice,
984                                            get_flexible_vertex_size(Desc->dwFVF) * Desc->dwNumVertices,
985                                            Desc->dwCaps & D3DVBCAPS_WRITEONLY ? WINED3DUSAGE_WRITEONLY : 0,
986                                            Desc->dwFVF,
987                                            Desc->dwCaps & D3DVBCAPS_SYSTEMMEMORY ? WINED3DPOOL_SYSTEMMEM : WINED3DPOOL_DEFAULT,
988                                            &object->wineD3DVertexBuffer,
989                                            0 /* SharedHandle */,
990                                            (IUnknown *) ICOM_INTERFACE(object, IDirect3DVertexBuffer7));
991     if(hr != D3D_OK)
992     {
993         ERR("(%p) IWineD3DDevice::CreateVertexBuffer failed with hr=%08lx\n", This, hr);
994         HeapFree(GetProcessHeap(), 0, object);
995         return hr;
996     }
997
998     /* Return the interface */
999     *VertexBuffer = ICOM_INTERFACE(object, IDirect3DVertexBuffer7);
1000
1001     TRACE("(%p) Created new vertex buffer implementation at %p, returning interface at %p\n", This, object, *VertexBuffer);
1002     return D3D_OK;
1003 }
1004
1005 static HRESULT WINAPI
1006 Thunk_IDirect3DImpl_3_CreateVertexBuffer(IDirect3D3 *iface,
1007                                          D3DVERTEXBUFFERDESC *Desc,
1008                                          IDirect3DVertexBuffer **VertexBuffer,
1009                                          DWORD Flags,
1010                                          IUnknown *UnkOuter)
1011 {
1012     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
1013     HRESULT hr;
1014     TRACE("(%p)->(%p,%p,%08lx,%p): Relaying to IDirect3D7\n", This, Desc, VertexBuffer, Flags, UnkOuter);
1015
1016     if(UnkOuter != NULL) return CLASS_E_NOAGGREGATION;
1017
1018     hr = IDirect3D7_CreateVertexBuffer(ICOM_INTERFACE(This, IDirect3D7),
1019                                        Desc,
1020                                        (IDirect3DVertexBuffer7 **) VertexBuffer,
1021                                        Flags);
1022
1023     *VertexBuffer = COM_INTERFACE_CAST(IDirect3DVertexBufferImpl, IDirect3DVertexBuffer7, IDirect3DVertexBuffer, *VertexBuffer);
1024     return hr;
1025 }
1026
1027
1028 /*****************************************************************************
1029  * IDirect3D7::EnumZBufferFormats
1030  *
1031  * Enumerates all supported Z buffer pixel formats
1032  *
1033  * Version 3 and 7
1034  *
1035  * Params:
1036  *  refiidDevice:
1037  *  Callback: Callback to call for each pixel format
1038  *  Context: Pointer to pass back to the callback
1039  *
1040  * Returns:
1041  *  D3D_OK on success
1042  *  DDERR_INVALIDPARAMS if Callback is NULL
1043  *  For details, see IWineD3DDevice::EnumZBufferFormats
1044  *
1045  *****************************************************************************/
1046 static HRESULT WINAPI
1047 IDirect3DImpl_7_EnumZBufferFormats(IDirect3D7 *iface,
1048                                    REFCLSID refiidDevice,
1049                                    LPD3DENUMPIXELFORMATSCALLBACK Callback,
1050                                    void *Context)
1051 {
1052     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
1053     HRESULT hr;
1054     int i;
1055
1056     /* Order matters. Specifically, BattleZone II (full version) expects the
1057      * 16-bit depth formats to be listed before the 24 and 32 ones. */
1058     WINED3DFORMAT FormatList[] = {
1059         WINED3DFMT_D15S1,
1060         WINED3DFMT_D16,
1061         WINED3DFMT_D24X8,
1062         WINED3DFMT_D24X4S4,
1063         WINED3DFMT_D24S8,
1064         WINED3DFMT_D32
1065     };
1066
1067     TRACE("(%p)->(%s,%p,%p): Relay\n", iface, debugstr_guid(refiidDevice), Callback, Context);
1068
1069     if(!Callback)
1070         return DDERR_INVALIDPARAMS;
1071
1072     for(i = 0; i < sizeof(FormatList) / sizeof(WINED3DFORMAT); i++)
1073     {
1074         hr = IWineD3D_CheckDeviceFormat(This->wineD3D,
1075                                         0 /* Adapter */,
1076                                         0 /* DeviceType */,
1077                                         0 /* AdapterFormat */,
1078                                         WINED3DUSAGE_DEPTHSTENCIL /* Usage */,
1079                                         0 /* ResourceType */,
1080                                         FormatList[i]);
1081         if(hr == D3D_OK)
1082         {
1083             DDPIXELFORMAT pformat;
1084
1085             memset(&pformat, 0, sizeof(pformat));
1086             pformat.dwSize = sizeof(pformat);
1087             PixelFormat_WineD3DtoDD(&pformat, FormatList[i]);
1088
1089             TRACE("Enumerating WineD3DFormat %d\n", FormatList[i]);
1090             hr = Callback(&pformat, Context);
1091             if(hr != DDENUMRET_OK)
1092             {
1093                 TRACE("Format enumeration cancelled by application\n");
1094                 return D3D_OK;
1095             }
1096         }
1097     }
1098     TRACE("End of enumeration\n");
1099     return D3D_OK;
1100 }
1101
1102 static HRESULT WINAPI
1103 Thunk_IDirect3DImpl_3_EnumZBufferFormats(IDirect3D3 *iface,
1104                                          REFCLSID riidDevice,
1105                                          LPD3DENUMPIXELFORMATSCALLBACK Callback,
1106                                          void *Context)
1107 {
1108     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
1109     TRACE("(%p)->(%s,%p,%p) thunking to IDirect3D7 interface.\n", This, debugstr_guid(riidDevice), Callback, Context);
1110     return IDirect3D7_EnumZBufferFormats(ICOM_INTERFACE(This, IDirect3D7),
1111                                          riidDevice,
1112                                          Callback,
1113                                          Context);
1114 }
1115
1116 /*****************************************************************************
1117  * IDirect3D7::EvictManagedTextures
1118  *
1119  * Removes all managed textures (=surfaces with DDSCAPS2_TEXTUREMANAGE or
1120  * DDSCAPS2_D3DTEXTUREMANAGE caps) to be removed from video memory.
1121  *
1122  * Version 3 and 7
1123  *
1124  * Returns:
1125  *  D3D_OK, because it's a stub
1126  *
1127  *****************************************************************************/
1128 static HRESULT WINAPI
1129 IDirect3DImpl_7_EvictManagedTextures(IDirect3D7 *iface)
1130 {
1131     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D7, iface);
1132     FIXME("(%p): Stub!\n", This);
1133
1134     /* Implementation idea:
1135      * Add an IWineD3DSurface method which sets the opengl texture
1136      * priority low or even removes the opengl texture.
1137      */
1138
1139     return D3D_OK;
1140 }
1141
1142 static HRESULT WINAPI
1143 Thunk_IDirect3DImpl_3_EvictManagedTextures(IDirect3D3 *iface)
1144 {
1145     ICOM_THIS_FROM(IDirectDrawImpl, IDirect3D3, iface);
1146     TRACE("(%p)->() thunking to IDirect3D7 interface.\n", This);
1147     return IDirect3D7_EvictManagedTextures(ICOM_INTERFACE(This, IDirect3D7));
1148 }
1149
1150 /*****************************************************************************
1151  * IDirect3DImpl_GetCaps
1152  *
1153  * This function retrieves the device caps from wined3d
1154  * and converts it into a D3D7 and D3D - D3D3 structure
1155  * This is a helper function called from various places in ddraw
1156  *
1157  * Params:
1158  *  WineD3D: The interface to get the caps from
1159  *  Desc123: Old D3D <3 structure to fill (needed)
1160  *  Desc7: D3D7 device desc structure to fill (needed)
1161  *
1162  * Returns
1163  *  D3D_OK on success, or the return value of IWineD3D::GetCaps
1164  *
1165  *****************************************************************************/
1166 HRESULT
1167 IDirect3DImpl_GetCaps(IWineD3D *WineD3D,
1168                       D3DDEVICEDESC *Desc123,
1169                       D3DDEVICEDESC7 *Desc7)
1170 {
1171     WINED3DCAPS WCaps;
1172     HRESULT hr;
1173
1174     /* Some Variables to asign to the pointers in WCaps */
1175     WINED3DDEVTYPE DevType;
1176     UINT dummy_uint;
1177     float dummy_float;
1178     DWORD dummy_dword, MaxTextureBlendStages, MaxSimultaneousTextures;
1179     DWORD MaxUserClipPlanes, MaxVertexBlendMatrices;
1180
1181     TRACE("()->(%p,%p,%p\n", WineD3D, Desc123, Desc7);
1182
1183     /* Asign the pointers in WCaps */
1184     WCaps.DeviceType = &DevType;
1185     WCaps.AdapterOrdinal = &dummy_uint;
1186
1187     WCaps.Caps = &dummy_dword;
1188     WCaps.Caps2 = &dummy_dword;
1189     WCaps.Caps3 = &dummy_dword;
1190     WCaps.PresentationIntervals = &dummy_dword;
1191
1192     WCaps.CursorCaps = &dummy_dword;
1193
1194     WCaps.DevCaps = &Desc7->dwDevCaps;
1195     WCaps.PrimitiveMiscCaps = &dummy_dword;
1196     WCaps.RasterCaps = &Desc7->dpcLineCaps.dwRasterCaps;
1197     WCaps.ZCmpCaps = &Desc7->dpcLineCaps.dwZCmpCaps;
1198     WCaps.SrcBlendCaps = &Desc7->dpcLineCaps.dwSrcBlendCaps;
1199     WCaps.DestBlendCaps = &Desc7->dpcLineCaps.dwDestBlendCaps;
1200     WCaps.AlphaCmpCaps = &Desc7->dpcLineCaps.dwAlphaCmpCaps;
1201     WCaps.ShadeCaps = &Desc7->dpcLineCaps.dwShadeCaps;
1202     WCaps.TextureCaps = &Desc7->dpcLineCaps.dwTextureCaps;
1203     WCaps.TextureFilterCaps = &Desc7->dpcLineCaps.dwTextureFilterCaps;
1204     WCaps.CubeTextureFilterCaps = &dummy_dword;
1205     WCaps.VolumeTextureFilterCaps = &dummy_dword;
1206     WCaps.TextureAddressCaps = &Desc7->dpcLineCaps.dwTextureAddressCaps;
1207     WCaps.VolumeTextureAddressCaps = &dummy_dword;
1208
1209     WCaps.LineCaps = &dummy_dword;
1210     WCaps.MaxTextureWidth = &Desc7->dwMaxTextureWidth;
1211     WCaps.MaxTextureHeight = &Desc7->dwMaxTextureHeight;
1212     WCaps.MaxVolumeExtent = &dummy_dword;
1213
1214     WCaps.MaxTextureRepeat = &Desc7->dwMaxTextureRepeat;
1215     WCaps.MaxTextureAspectRatio = &Desc7->dwMaxTextureAspectRatio;
1216     WCaps.MaxAnisotropy = &Desc7->dwMaxAnisotropy;
1217     WCaps.MaxVertexW = &Desc7->dvMaxVertexW;
1218
1219     WCaps.GuardBandLeft = &Desc7->dvGuardBandLeft;
1220     WCaps.GuardBandTop = &Desc7->dvGuardBandTop;
1221     WCaps.GuardBandRight = &Desc7->dvGuardBandRight;
1222     WCaps.GuardBandBottom = &Desc7->dvGuardBandBottom;
1223
1224     WCaps.ExtentsAdjust = &Desc7->dvExtentsAdjust;
1225     WCaps.StencilCaps = &Desc7->dwStencilCaps;
1226
1227     WCaps.FVFCaps = &Desc7->dwFVFCaps;
1228     WCaps.TextureOpCaps = &Desc7->dwTextureOpCaps;
1229     WCaps.MaxTextureBlendStages = &MaxTextureBlendStages;
1230     WCaps.MaxSimultaneousTextures = &MaxSimultaneousTextures;
1231
1232     WCaps.VertexProcessingCaps = &Desc7->dwVertexProcessingCaps;
1233     WCaps.MaxActiveLights = &Desc7->dwMaxActiveLights;
1234     WCaps.MaxUserClipPlanes = &MaxUserClipPlanes;
1235     WCaps.MaxVertexBlendMatrices = &MaxVertexBlendMatrices;
1236     WCaps.MaxVertexBlendMatrixIndex = &dummy_dword;
1237
1238     WCaps.MaxPointSize = &dummy_float;
1239     WCaps.MaxPrimitiveCount = &dummy_dword;
1240     WCaps.MaxVertexIndex = &dummy_dword;
1241     WCaps.MaxStreams = &dummy_dword;
1242     WCaps.MaxStreamStride = &dummy_dword;
1243
1244     WCaps.VertexShaderVersion = &dummy_dword;
1245     WCaps.MaxVertexShaderConst = &dummy_dword;
1246
1247     WCaps.PixelShaderVersion = &dummy_dword;
1248     WCaps.PixelShader1xMaxValue = &dummy_float;
1249
1250     /* These are dx9 only, set them to NULL */
1251     WCaps.DevCaps2 = NULL;
1252     WCaps.MaxNpatchTessellationLevel = NULL;
1253     WCaps.Reserved5 = NULL;
1254     WCaps.MasterAdapterOrdinal = NULL;
1255     WCaps.AdapterOrdinalInGroup = NULL;
1256     WCaps.NumberOfAdaptersInGroup = NULL;
1257     WCaps.DeclTypes = NULL;
1258     WCaps.NumSimultaneousRTs = NULL;
1259     WCaps.StretchRectFilterCaps = NULL;
1260     /* WCaps.VS20Caps = NULL; */
1261     /* WCaps.PS20Caps = NULL; */
1262     WCaps.VertexTextureFilterCaps = NULL;
1263     WCaps.MaxVShaderInstructionsExecuted = NULL;
1264     WCaps.MaxPShaderInstructionsExecuted = NULL;
1265     WCaps.MaxVertexShader30InstructionSlots = NULL;
1266     WCaps.MaxPixelShader30InstructionSlots = NULL;
1267     WCaps.Reserved2 = NULL;
1268     WCaps.Reserved3 = NULL;
1269
1270     /* Now get the caps */
1271     hr = IWineD3D_GetDeviceCaps(WineD3D, 0, WINED3DDEVTYPE_HAL, &WCaps);
1272     if(hr != D3D_OK) return hr;
1273
1274     /* Fill the missing members, and do some fixup */
1275     Desc7->dpcLineCaps.dwSize = sizeof(Desc7->dpcLineCaps);
1276     Desc7->dpcLineCaps.dwTextureBlendCaps = D3DPTBLENDCAPS_ADD | D3DPTBLENDCAPS_MODULATEMASK |
1277                                             D3DPTBLENDCAPS_COPY | D3DPTBLENDCAPS_DECAL |
1278                                             D3DPTBLENDCAPS_DECALALPHA | D3DPTBLENDCAPS_DECALMASK |
1279                                             D3DPTBLENDCAPS_MODULATE | D3DPTBLENDCAPS_MODULATEALPHA;
1280     Desc7->dpcLineCaps.dwStippleWidth = 32;
1281     Desc7->dpcLineCaps.dwStippleHeight = 32;
1282     /* Use the same for the TriCaps */
1283     Desc7->dpcTriCaps = Desc7->dpcLineCaps;
1284
1285     Desc7->dwDeviceRenderBitDepth = DDBD_16 | DDBD_24 | DDBD_32;
1286     Desc7->dwDeviceZBufferBitDepth = DDBD_16 | DDBD_24;
1287     Desc7->dwMinTextureWidth = 1;
1288     Desc7->dwMinTextureHeight = 1;
1289
1290     /* Convert DWORDs safely to WORDs */
1291     if(MaxTextureBlendStages > 65535) Desc7->wMaxTextureBlendStages = 65535;
1292     else Desc7->wMaxTextureBlendStages = (WORD) MaxTextureBlendStages;
1293     if(MaxSimultaneousTextures > 65535) Desc7->wMaxSimultaneousTextures = 65535;
1294     else Desc7->wMaxSimultaneousTextures = (WORD) MaxSimultaneousTextures;
1295
1296     if(MaxUserClipPlanes > 65535) Desc7->wMaxUserClipPlanes = 65535;
1297     else Desc7->wMaxUserClipPlanes = (WORD) MaxUserClipPlanes;
1298     if(MaxVertexBlendMatrices > 65535) Desc7->wMaxVertexBlendMatrices = 65535;
1299     else Desc7->wMaxVertexBlendMatrices = (WORD) MaxVertexBlendMatrices;
1300
1301     Desc7->deviceGUID = IID_IDirect3DTnLHalDevice;
1302
1303     Desc7->dwReserved1 = 0;
1304     Desc7->dwReserved2 = 0;
1305     Desc7->dwReserved3 = 0;
1306     Desc7->dwReserved4 = 0;
1307
1308     /* Fill the old structure */
1309     memset(Desc123, 0x0, sizeof(D3DDEVICEDESC));
1310     Desc123->dwSize = sizeof(D3DDEVICEDESC);
1311     Desc123->dwFlags = D3DDD_COLORMODEL            |
1312                        D3DDD_DEVCAPS               |
1313                        D3DDD_TRANSFORMCAPS         |
1314                        D3DDD_BCLIPPING             |
1315                        D3DDD_LIGHTINGCAPS          |
1316                        D3DDD_LINECAPS              |
1317                        D3DDD_TRICAPS               |
1318                        D3DDD_DEVICERENDERBITDEPTH  |
1319                        D3DDD_DEVICEZBUFFERBITDEPTH |
1320                        D3DDD_MAXBUFFERSIZE         |
1321                        D3DDD_MAXVERTEXCOUNT;
1322     Desc123->dcmColorModel = D3DCOLOR_RGB;
1323     Desc123->dwDevCaps = Desc7->dwDevCaps;
1324     Desc123->dtcTransformCaps.dwSize = sizeof(D3DTRANSFORMCAPS);
1325     Desc123->dtcTransformCaps.dwCaps = D3DTRANSFORMCAPS_CLIP;
1326     Desc123->bClipping = TRUE;
1327     Desc123->dlcLightingCaps.dwSize = sizeof(D3DLIGHTINGCAPS);
1328     Desc123->dlcLightingCaps.dwCaps = D3DLIGHTCAPS_DIRECTIONAL | D3DLIGHTCAPS_PARALLELPOINT | D3DLIGHTCAPS_POINT | D3DLIGHTCAPS_SPOT;
1329     Desc123->dlcLightingCaps.dwLightingModel = D3DLIGHTINGMODEL_RGB;
1330     Desc123->dlcLightingCaps.dwNumLights = Desc7->dwMaxActiveLights;
1331
1332     Desc123->dpcLineCaps.dwSize = sizeof(D3DPRIMCAPS);
1333     Desc123->dpcLineCaps.dwMiscCaps = Desc7->dpcLineCaps.dwMiscCaps;
1334     Desc123->dpcLineCaps.dwRasterCaps = Desc7->dpcLineCaps.dwRasterCaps;
1335     Desc123->dpcLineCaps.dwZCmpCaps = Desc7->dpcLineCaps.dwZCmpCaps;
1336     Desc123->dpcLineCaps.dwSrcBlendCaps = Desc7->dpcLineCaps.dwSrcBlendCaps;
1337     Desc123->dpcLineCaps.dwDestBlendCaps = Desc7->dpcLineCaps.dwDestBlendCaps;
1338     Desc123->dpcLineCaps.dwShadeCaps = Desc7->dpcLineCaps.dwShadeCaps;
1339     Desc123->dpcLineCaps.dwTextureCaps = Desc7->dpcLineCaps.dwTextureCaps;
1340     Desc123->dpcLineCaps.dwTextureFilterCaps = Desc7->dpcLineCaps.dwTextureFilterCaps;
1341     Desc123->dpcLineCaps.dwTextureBlendCaps = Desc7->dpcLineCaps.dwTextureBlendCaps;
1342     Desc123->dpcLineCaps.dwTextureAddressCaps = Desc7->dpcLineCaps.dwTextureAddressCaps;
1343     Desc123->dpcLineCaps.dwStippleWidth = Desc7->dpcLineCaps.dwStippleWidth;
1344     Desc123->dpcLineCaps.dwAlphaCmpCaps = Desc7->dpcLineCaps.dwAlphaCmpCaps;
1345
1346     Desc123->dpcTriCaps.dwSize = sizeof(D3DPRIMCAPS);
1347     Desc123->dpcTriCaps.dwMiscCaps = Desc7->dpcTriCaps.dwMiscCaps;
1348     Desc123->dpcTriCaps.dwRasterCaps = Desc7->dpcTriCaps.dwRasterCaps;
1349     Desc123->dpcTriCaps.dwZCmpCaps = Desc7->dpcTriCaps.dwZCmpCaps;
1350     Desc123->dpcTriCaps.dwSrcBlendCaps = Desc7->dpcTriCaps.dwSrcBlendCaps;
1351     Desc123->dpcTriCaps.dwDestBlendCaps = Desc7->dpcTriCaps.dwDestBlendCaps;
1352     Desc123->dpcTriCaps.dwShadeCaps = Desc7->dpcTriCaps.dwShadeCaps;
1353     Desc123->dpcTriCaps.dwTextureCaps = Desc7->dpcTriCaps.dwTextureCaps;
1354     Desc123->dpcTriCaps.dwTextureFilterCaps = Desc7->dpcTriCaps.dwTextureFilterCaps;
1355     Desc123->dpcTriCaps.dwTextureBlendCaps = Desc7->dpcTriCaps.dwTextureBlendCaps;
1356     Desc123->dpcTriCaps.dwTextureAddressCaps = Desc7->dpcTriCaps.dwTextureAddressCaps;
1357     Desc123->dpcTriCaps.dwStippleWidth = Desc7->dpcTriCaps.dwStippleWidth;
1358     Desc123->dpcTriCaps.dwAlphaCmpCaps = Desc7->dpcTriCaps.dwAlphaCmpCaps;
1359
1360     Desc123->dwDeviceRenderBitDepth = Desc7->dwDeviceRenderBitDepth;
1361     Desc123->dwDeviceZBufferBitDepth = Desc7->dwDeviceZBufferBitDepth;
1362     Desc123->dwMaxBufferSize = 0;
1363     Desc123->dwMaxVertexCount = 65536;
1364     Desc123->dwMinTextureWidth  = Desc7->dwMinTextureWidth;
1365     Desc123->dwMinTextureHeight = Desc7->dwMinTextureHeight;
1366     Desc123->dwMaxTextureWidth  = Desc7->dwMaxTextureWidth;
1367     Desc123->dwMaxTextureHeight = Desc7->dwMaxTextureHeight;
1368     Desc123->dwMinStippleWidth  = 1;
1369     Desc123->dwMinStippleHeight = 1;
1370     Desc123->dwMaxStippleWidth  = 32;
1371     Desc123->dwMaxStippleHeight = 32;
1372     Desc123->dwMaxTextureRepeat = Desc7->dwMaxTextureRepeat;
1373     Desc123->dwMaxTextureAspectRatio = Desc7->dwMaxTextureAspectRatio;
1374     Desc123->dwMaxAnisotropy = Desc7->dwMaxAnisotropy;
1375     Desc123->dvGuardBandLeft = Desc7->dvGuardBandLeft;
1376     Desc123->dvGuardBandRight = Desc7->dvGuardBandRight;
1377     Desc123->dvGuardBandTop = Desc7->dvGuardBandTop;
1378     Desc123->dvGuardBandBottom = Desc7->dvGuardBandBottom;
1379     Desc123->dvExtentsAdjust = Desc7->dvExtentsAdjust;
1380     Desc123->dwStencilCaps = Desc7->dwStencilCaps;
1381     Desc123->dwFVFCaps = Desc7->dwFVFCaps;
1382     Desc123->dwTextureOpCaps = Desc7->dwTextureOpCaps;
1383     Desc123->wMaxTextureBlendStages = Desc7->wMaxTextureBlendStages;
1384     Desc123->wMaxSimultaneousTextures = Desc7->wMaxSimultaneousTextures;
1385
1386     return DD_OK;
1387 }
1388 /*****************************************************************************
1389  * IDirect3D vtables in various versions
1390  *****************************************************************************/
1391
1392 const IDirect3DVtbl IDirect3D1_Vtbl =
1393 {
1394     /*** IUnknown methods ***/
1395     Thunk_IDirect3DImpl_1_QueryInterface,
1396     Thunk_IDirect3DImpl_1_AddRef,
1397     Thunk_IDirect3DImpl_1_Release,
1398     /*** IDirect3D methods ***/
1399     IDirect3DImpl_1_Initialize,
1400     Thunk_IDirect3DImpl_1_EnumDevices,
1401     Thunk_IDirect3DImpl_1_CreateLight,
1402     Thunk_IDirect3DImpl_1_CreateMaterial,
1403     Thunk_IDirect3DImpl_1_CreateViewport,
1404     Thunk_IDirect3DImpl_1_FindDevice
1405 };
1406
1407 const IDirect3D2Vtbl IDirect3D2_Vtbl =
1408 {
1409     /*** IUnknown methods ***/
1410     Thunk_IDirect3DImpl_2_QueryInterface,
1411     Thunk_IDirect3DImpl_2_AddRef,
1412     Thunk_IDirect3DImpl_2_Release,
1413     /*** IDirect3D2 methods ***/
1414     Thunk_IDirect3DImpl_2_EnumDevices,
1415     Thunk_IDirect3DImpl_2_CreateLight,
1416     Thunk_IDirect3DImpl_2_CreateMaterial,
1417     Thunk_IDirect3DImpl_2_CreateViewport,
1418     Thunk_IDirect3DImpl_2_FindDevice,
1419     Thunk_IDirect3DImpl_2_CreateDevice
1420 };
1421
1422 const IDirect3D3Vtbl IDirect3D3_Vtbl =
1423 {
1424     /*** IUnknown methods ***/
1425     Thunk_IDirect3DImpl_3_QueryInterface,
1426     Thunk_IDirect3DImpl_3_AddRef,
1427     Thunk_IDirect3DImpl_3_Release,
1428     /*** IDirect3D3 methods ***/
1429     IDirect3DImpl_3_EnumDevices,
1430     IDirect3DImpl_3_CreateLight,
1431     IDirect3DImpl_3_CreateMaterial,
1432     IDirect3DImpl_3_CreateViewport,
1433     IDirect3DImpl_3_FindDevice,
1434     Thunk_IDirect3DImpl_3_CreateDevice,
1435     Thunk_IDirect3DImpl_3_CreateVertexBuffer,
1436     Thunk_IDirect3DImpl_3_EnumZBufferFormats,
1437     Thunk_IDirect3DImpl_3_EvictManagedTextures
1438 };
1439
1440 const IDirect3D7Vtbl IDirect3D7_Vtbl =
1441 {
1442     /*** IUnknown methods ***/
1443     Thunk_IDirect3DImpl_7_QueryInterface,
1444     Thunk_IDirect3DImpl_7_AddRef,
1445     Thunk_IDirect3DImpl_7_Release,
1446     /*** IDirect3D7 methods ***/
1447     IDirect3DImpl_7_EnumDevices,
1448     IDirect3DImpl_7_CreateDevice,
1449     IDirect3DImpl_7_CreateVertexBuffer,
1450     IDirect3DImpl_7_EnumZBufferFormats,
1451     IDirect3DImpl_7_EvictManagedTextures
1452 };