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