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