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