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