comctl32/listview: Free ID array when removing all items.
[wine] / dlls / d3d9 / directx.c
1 /*
2  * IDirect3D9 implementation
3  *
4  * Copyright 2002 Jason Edmeades
5  * Copyright 2005 Oliver Stieber
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include "config.h"
23 #include "d3d9_private.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
26
27 /* IDirect3D9 IUnknown parts follow: */
28 static HRESULT WINAPI IDirect3D9Impl_QueryInterface(LPDIRECT3D9EX iface, REFIID riid, LPVOID* ppobj)
29 {
30     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
31
32     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
33
34     if (IsEqualGUID(riid, &IID_IUnknown)
35         || IsEqualGUID(riid, &IID_IDirect3D9)) {
36         IDirect3D9Ex_AddRef(iface);
37         *ppobj = This;
38         TRACE("Returning IDirect3D9 interface at %p\n", *ppobj);
39         return S_OK;
40     } else if(IsEqualGUID(riid, &IID_IDirect3D9Ex)) {
41         if(This->extended) {
42             *ppobj = This;
43             TRACE("Returning IDirect3D9Ex interface at %p\n", *ppobj);
44             IDirect3D9Ex_AddRef((IDirect3D9Ex *)*ppobj);
45         } else {
46             WARN("Application asks for IDirect3D9Ex, but this instance wasn't created with Direct3DCreate9Ex\n");
47             WARN("Returning E_NOINTERFACE\n");
48             *ppobj = NULL;
49             return E_NOINTERFACE;
50         }
51     }
52
53     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
54     *ppobj = NULL;
55     return E_NOINTERFACE;
56 }
57
58 static ULONG WINAPI IDirect3D9Impl_AddRef(LPDIRECT3D9EX iface) {
59     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
60     ULONG ref = InterlockedIncrement(&This->ref);
61
62     TRACE("%p increasing refcount to %u.\n", iface, ref);
63
64     return ref;
65 }
66
67 static ULONG WINAPI IDirect3D9Impl_Release(LPDIRECT3D9EX iface) {
68     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
69     ULONG ref = InterlockedDecrement(&This->ref);
70
71     TRACE("%p decreasing refcount to %u.\n", iface, ref);
72
73     if (ref == 0) {
74         wined3d_mutex_lock();
75         IWineD3D_Release(This->WineD3D);
76         wined3d_mutex_unlock();
77
78         HeapFree(GetProcessHeap(), 0, This);
79     }
80
81     return ref;
82 }
83
84 /* IDirect3D9 Interface follow: */
85 static HRESULT  WINAPI  IDirect3D9Impl_RegisterSoftwareDevice(LPDIRECT3D9EX iface, void* pInitializeFunction) {
86     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
87     HRESULT hr;
88
89     TRACE("iface %p, init_function %p.\n", iface, pInitializeFunction);
90
91     wined3d_mutex_lock();
92     hr = IWineD3D_RegisterSoftwareDevice(This->WineD3D, pInitializeFunction);
93     wined3d_mutex_unlock();
94
95     return hr;
96 }
97
98 static UINT     WINAPI  IDirect3D9Impl_GetAdapterCount(LPDIRECT3D9EX iface) {
99     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
100     HRESULT hr;
101
102     TRACE("iface %p.\n", iface);
103
104     wined3d_mutex_lock();
105     hr = IWineD3D_GetAdapterCount(This->WineD3D);
106     wined3d_mutex_unlock();
107
108     return hr;
109 }
110
111 static HRESULT WINAPI IDirect3D9Impl_GetAdapterIdentifier(LPDIRECT3D9EX iface, UINT Adapter, DWORD Flags, D3DADAPTER_IDENTIFIER9* pIdentifier) {
112     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
113     WINED3DADAPTER_IDENTIFIER adapter_id;
114     HRESULT hr;
115
116     TRACE("iface %p, adapter %u, flags %#x, identifier %p.\n",
117             iface, Adapter, Flags, pIdentifier);
118
119     adapter_id.driver = pIdentifier->Driver;
120     adapter_id.driver_size = sizeof(pIdentifier->Driver);
121     adapter_id.description = pIdentifier->Description;
122     adapter_id.description_size = sizeof(pIdentifier->Description);
123     adapter_id.device_name = pIdentifier->DeviceName;
124     adapter_id.device_name_size = sizeof(pIdentifier->DeviceName);
125
126     wined3d_mutex_lock();
127     hr = IWineD3D_GetAdapterIdentifier(This->WineD3D, Adapter, Flags, &adapter_id);
128     wined3d_mutex_unlock();
129
130     pIdentifier->DriverVersion = adapter_id.driver_version;
131     pIdentifier->VendorId = adapter_id.vendor_id;
132     pIdentifier->DeviceId = adapter_id.device_id;
133     pIdentifier->SubSysId = adapter_id.subsystem_id;
134     pIdentifier->Revision = adapter_id.revision;
135     memcpy(&pIdentifier->DeviceIdentifier, &adapter_id.device_identifier, sizeof(pIdentifier->DeviceIdentifier));
136     pIdentifier->WHQLLevel = adapter_id.whql_level;
137
138     return hr;
139 }
140
141 static UINT WINAPI IDirect3D9Impl_GetAdapterModeCount(LPDIRECT3D9EX iface, UINT Adapter, D3DFORMAT Format) {
142     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
143     HRESULT hr;
144
145     TRACE("iface %p, adapter %u, format %#x.\n", iface, Adapter, Format);
146
147     /* Others than that not supported by d3d9, but reported by wined3d for ddraw. Filter them out */
148     if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5) {
149         return 0;
150     }
151
152     wined3d_mutex_lock();
153     hr = IWineD3D_GetAdapterModeCount(This->WineD3D, Adapter, wined3dformat_from_d3dformat(Format));
154     wined3d_mutex_unlock();
155
156     return hr;
157 }
158
159 static HRESULT WINAPI IDirect3D9Impl_EnumAdapterModes(LPDIRECT3D9EX iface, UINT Adapter, D3DFORMAT Format, UINT Mode, D3DDISPLAYMODE* pMode) {
160     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
161     HRESULT hr;
162
163     TRACE("iface %p, adapter %u, format %#x, mode_idx %u, mode %p.\n",
164             iface, Adapter, Format, Mode, pMode);
165
166     /* We can't pass this to WineD3D, otherwise it'll think it came from D3D8 or DDraw.
167        It's supposed to fail anyway, so no harm returning failure. */
168     if(Format != D3DFMT_X8R8G8B8 && Format != D3DFMT_R5G6B5)
169         return D3DERR_INVALIDCALL;
170
171     wined3d_mutex_lock();
172     hr = IWineD3D_EnumAdapterModes(This->WineD3D, Adapter, wined3dformat_from_d3dformat(Format),
173             Mode, (WINED3DDISPLAYMODE *) pMode);
174     wined3d_mutex_unlock();
175
176     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
177
178     return hr;
179 }
180
181 static HRESULT WINAPI IDirect3D9Impl_GetAdapterDisplayMode(LPDIRECT3D9EX iface, UINT Adapter, D3DDISPLAYMODE* pMode) {
182     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
183     HRESULT hr;
184
185     TRACE("iface %p, adapter %u, mode %p.\n", iface, Adapter, pMode);
186
187     wined3d_mutex_lock();
188     hr = IWineD3D_GetAdapterDisplayMode(This->WineD3D, Adapter, (WINED3DDISPLAYMODE *) pMode);
189     wined3d_mutex_unlock();
190
191     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
192
193     return hr;
194 }
195
196 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceType(IDirect3D9Ex *iface, UINT Adapter,
197         D3DDEVTYPE CheckType, D3DFORMAT DisplayFormat, D3DFORMAT BackBufferFormat, BOOL Windowed)
198 {
199     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
200     HRESULT hr;
201
202     TRACE("iface %p, adapter %u, device_type %#x, display_format %#x, backbuffer_format %#x, windowed %#x.\n",
203             iface, Adapter, CheckType, DisplayFormat, BackBufferFormat, Windowed);
204
205     wined3d_mutex_lock();
206     hr = IWineD3D_CheckDeviceType(This->WineD3D, Adapter, CheckType, wined3dformat_from_d3dformat(DisplayFormat),
207             wined3dformat_from_d3dformat(BackBufferFormat), Windowed);
208     wined3d_mutex_unlock();
209
210     return hr;
211 }
212
213 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormat(IDirect3D9Ex *iface, UINT Adapter, D3DDEVTYPE DeviceType,
214         D3DFORMAT AdapterFormat, DWORD Usage, D3DRESOURCETYPE RType, D3DFORMAT CheckFormat)
215 {
216     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
217     HRESULT hr;
218     WINED3DRESOURCETYPE WineD3DRType;
219
220     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, usage %#x, resource_type %#x, format %#x.\n",
221             iface, Adapter, DeviceType, AdapterFormat, Usage, RType, CheckFormat);
222
223     /* This format is nothing special and it is supported perfectly.
224      * However, ati and nvidia driver on windows do not mark this format as
225      * supported (tested with the dxCapsViewer) and pretending to
226      * support this format uncovers a bug in Battlefield 1942 (fonts are missing)
227      * So do the same as Windows drivers and pretend not to support it on dx8 and 9
228      */
229     if(CheckFormat == D3DFMT_R8G8B8)
230     {
231         WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
232         return D3DERR_NOTAVAILABLE;
233     }
234
235     switch(RType) {
236         case D3DRTYPE_VERTEXBUFFER:
237         case D3DRTYPE_INDEXBUFFER:
238             WineD3DRType = WINED3DRTYPE_BUFFER;
239             break;
240
241         default:
242             WineD3DRType = RType;
243             break;
244     }
245
246     wined3d_mutex_lock();
247     hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
248             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
249     wined3d_mutex_unlock();
250
251     return hr;
252 }
253
254 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(IDirect3D9Ex *iface, UINT Adapter,
255         D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat, BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType,
256         DWORD *pQualityLevels)
257 {
258     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
259     HRESULT hr;
260
261     TRACE("iface %p, adapter %u, device_type %#x, format %#x, windowed %#x, multisample_type %#x, levels %p.\n",
262             iface, Adapter, DeviceType, SurfaceFormat, Windowed, MultiSampleType, pQualityLevels);
263
264     wined3d_mutex_lock();
265     hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
266             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, MultiSampleType, pQualityLevels);
267     wined3d_mutex_unlock();
268
269     return hr;
270 }
271
272 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(IDirect3D9Ex *iface, UINT Adapter,
273         D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat, D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat)
274 {
275     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
276     HRESULT hr;
277
278     TRACE("iface %p, adapter %u, device_type %#x, adapter_format %#x, rt_format %#x, ds_format %#x.\n",
279             iface, Adapter, DeviceType, AdapterFormat, RenderTargetFormat, DepthStencilFormat);
280
281     wined3d_mutex_lock();
282     hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
283             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
284             wined3dformat_from_d3dformat(DepthStencilFormat));
285     wined3d_mutex_unlock();
286
287     return hr;
288 }
289
290 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
291     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
292     HRESULT hr;
293
294     TRACE("iface %p, adapter %u, device_type %#x, src_format %#x, dst_format %#x.\n",
295             iface, Adapter, DeviceType, SourceFormat, TargetFormat);
296
297     wined3d_mutex_lock();
298     hr = IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType,
299             wined3dformat_from_d3dformat(SourceFormat), wined3dformat_from_d3dformat(TargetFormat));
300     wined3d_mutex_unlock();
301
302     return hr;
303 }
304
305 void filter_caps(D3DCAPS9* pCaps)
306 {
307
308     DWORD textureFilterCaps =
309         D3DPTFILTERCAPS_MINFPOINT      | D3DPTFILTERCAPS_MINFLINEAR    | D3DPTFILTERCAPS_MINFANISOTROPIC |
310         D3DPTFILTERCAPS_MINFPYRAMIDALQUAD                              | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
311         D3DPTFILTERCAPS_MIPFPOINT      | D3DPTFILTERCAPS_MIPFLINEAR    | D3DPTFILTERCAPS_MAGFPOINT       |
312         D3DPTFILTERCAPS_MAGFLINEAR     |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
313         D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
314     pCaps->TextureFilterCaps &= textureFilterCaps;
315     pCaps->CubeTextureFilterCaps &= textureFilterCaps;
316     pCaps->VolumeTextureFilterCaps &= textureFilterCaps;
317
318     pCaps->DevCaps &=
319         D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
320         D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY   |
321         D3DDEVCAPS_DRAWPRIMTLVERTEX    | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
322         D3DDEVCAPS_DRAWPRIMITIVES2     | D3DDEVCAPS_SEPARATETEXTUREMEMORIES                              |
323         D3DDEVCAPS_DRAWPRIMITIVES2EX   | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL  |
324         D3DDEVCAPS_HWRASTERIZATION     | D3DDEVCAPS_PUREDEVICE         | D3DDEVCAPS_QUINTICRTPATCHES     |
325         D3DDEVCAPS_RTPATCHES           | D3DDEVCAPS_RTPATCHHANDLEZERO  | D3DDEVCAPS_NPATCHES;
326
327     pCaps->ShadeCaps &=
328         D3DPSHADECAPS_COLORGOURAUDRGB  | D3DPSHADECAPS_SPECULARGOURAUDRGB |
329         D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
330
331     pCaps->RasterCaps &=
332         D3DPRASTERCAPS_DITHER          | D3DPRASTERCAPS_ZTEST          | D3DPRASTERCAPS_FOGVERTEX        |
333         D3DPRASTERCAPS_FOGTABLE        | D3DPRASTERCAPS_MIPMAPLODBIAS  | D3DPRASTERCAPS_ZBUFFERLESSHSR   |
334         D3DPRASTERCAPS_FOGRANGE        | D3DPRASTERCAPS_ANISOTROPY     | D3DPRASTERCAPS_WBUFFER          |
335         D3DPRASTERCAPS_WFOG            | D3DPRASTERCAPS_ZFOG           | D3DPRASTERCAPS_COLORPERSPECTIVE |
336         D3DPRASTERCAPS_SCISSORTEST     | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS                              |
337         D3DPRASTERCAPS_DEPTHBIAS       | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
338
339     pCaps->DevCaps2 &=
340         D3DDEVCAPS2_STREAMOFFSET       | D3DDEVCAPS2_DMAPNPATCH        | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
341         D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES                       |
342         D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
343
344     pCaps->Caps2 &=
345         D3DCAPS2_FULLSCREENGAMMA       | D3DCAPS2_CANCALIBRATEGAMMA    | D3DCAPS2_RESERVED               |
346         D3DCAPS2_CANMANAGERESOURCE     | D3DCAPS2_DYNAMICTEXTURES      | D3DCAPS2_CANAUTOGENMIPMAP;
347
348     pCaps->VertexProcessingCaps &=
349         D3DVTXPCAPS_TEXGEN             | D3DVTXPCAPS_MATERIALSOURCE7   | D3DVTXPCAPS_DIRECTIONALLIGHTS   |
350         D3DVTXPCAPS_POSITIONALLIGHTS   | D3DVTXPCAPS_LOCALVIEWER       | D3DVTXPCAPS_TWEENING            |
351         D3DVTXPCAPS_TEXGEN_SPHEREMAP   | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
352
353     pCaps->TextureCaps &=
354         D3DPTEXTURECAPS_PERSPECTIVE    | D3DPTEXTURECAPS_POW2          | D3DPTEXTURECAPS_ALPHA           |
355         D3DPTEXTURECAPS_SQUAREONLY     | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE                        |
356         D3DPTEXTURECAPS_ALPHAPALETTE   | D3DPTEXTURECAPS_NONPOW2CONDITIONAL                              |
357         D3DPTEXTURECAPS_PROJECTED      | D3DPTEXTURECAPS_CUBEMAP       | D3DPTEXTURECAPS_VOLUMEMAP       |
358         D3DPTEXTURECAPS_MIPMAP         | D3DPTEXTURECAPS_MIPVOLUMEMAP  | D3DPTEXTURECAPS_MIPCUBEMAP      |
359         D3DPTEXTURECAPS_CUBEMAP_POW2   | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
360
361     pCaps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, pCaps->MaxVertexShaderConst);
362     pCaps->NumSimultaneousRTs = min(D3D9_MAX_SIMULTANEOUS_RENDERTARGETS, pCaps->NumSimultaneousRTs);
363 }
364
365 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
366     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
367     HRESULT hrc = D3D_OK;
368     WINED3DCAPS *pWineCaps;
369
370     TRACE("iface %p, adapter %u, device_type %#x, caps %p.\n", iface, Adapter, DeviceType, pCaps);
371
372     if(NULL == pCaps){
373         return D3DERR_INVALIDCALL;
374     }
375     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
376     if(pWineCaps == NULL){
377         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
378     }
379     memset(pCaps, 0, sizeof(*pCaps));
380
381     wined3d_mutex_lock();
382     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
383     wined3d_mutex_unlock();
384
385     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
386     HeapFree(GetProcessHeap(), 0, pWineCaps);
387
388     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
389     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
390
391     filter_caps(pCaps);
392
393     TRACE("(%p) returning %p\n", This, pCaps);
394     return hrc;
395 }
396
397 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9EX iface, UINT Adapter) {
398     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
399     HMONITOR ret;
400
401     TRACE("iface %p, adapter %u.\n", iface, Adapter);
402
403     wined3d_mutex_lock();
404     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
405     wined3d_mutex_unlock();
406
407     return ret;
408 }
409
410 ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
411     IDirect3DSwapChain9Impl* swapChainParent;
412     TRACE("(%p) call back\n", pSwapChain);
413
414     IWineD3DSwapChain_GetParent(pSwapChain,(IUnknown **) &swapChainParent);
415     swapChainParent->isImplicit = FALSE;
416     /* Swap chain had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
417     return IDirect3DSwapChain9_Release((IDirect3DSwapChain9*) swapChainParent);
418 }
419
420 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3D9Impl_CreateDevice(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType,
421                                                             HWND hFocusWindow, DWORD BehaviourFlags,
422                                                             D3DPRESENT_PARAMETERS* pPresentationParameters,
423                                                             IDirect3DDevice9** ppReturnedDeviceInterface) {
424
425     IDirect3D9Impl       *This   = (IDirect3D9Impl *)iface;
426     IDirect3DDevice9Impl *object = NULL;
427     WINED3DPRESENT_PARAMETERS *localParameters;
428     UINT i, count = 1;
429     HRESULT hr;
430
431     TRACE("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x, parameters %p, device %p.\n",
432             iface, Adapter, DeviceType, hFocusWindow, BehaviourFlags, pPresentationParameters,
433             ppReturnedDeviceInterface);
434
435     /* Check the validity range of the adapter parameter */
436     if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
437         *ppReturnedDeviceInterface = NULL;
438         return D3DERR_INVALIDCALL;
439     }
440
441     /* Allocate the storage for the device object */
442     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
443     if (NULL == object) {
444         FIXME("Allocation of memory failed\n");
445         *ppReturnedDeviceInterface = NULL;
446         return D3DERR_OUTOFVIDEOMEMORY;
447     }
448
449     object->lpVtbl = &Direct3DDevice9_Vtbl;
450     object->device_parent_vtbl = &d3d9_wined3d_device_parent_vtbl;
451     object->ref = 1;
452     *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
453
454     /* Allocate an associated WineD3DDevice object */
455     wined3d_mutex_lock();
456     hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
457             (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
458     if (hr != D3D_OK) {
459         HeapFree(GetProcessHeap(), 0, object);
460         *ppReturnedDeviceInterface = NULL;
461         wined3d_mutex_unlock();
462
463         return hr;
464     }
465
466     TRACE("(%p) : Created Device %p\n", This, object);
467
468     if (BehaviourFlags & D3DCREATE_ADAPTERGROUP_DEVICE)
469     {
470         WINED3DCAPS caps;
471
472         IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, &caps);
473         count = caps.NumberOfAdaptersInGroup;
474     }
475
476     if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
477         IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
478     }
479
480     localParameters = HeapAlloc(GetProcessHeap(), 0, sizeof(*localParameters) * count);
481     for (i = 0; i < count; ++i)
482     {
483         localParameters[i].BackBufferWidth = pPresentationParameters[i].BackBufferWidth;
484         localParameters[i].BackBufferHeight = pPresentationParameters[i].BackBufferHeight;
485         localParameters[i].BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters[i].BackBufferFormat);
486         localParameters[i].BackBufferCount = pPresentationParameters[i].BackBufferCount;
487         localParameters[i].MultiSampleType = pPresentationParameters[i].MultiSampleType;
488         localParameters[i].MultiSampleQuality = pPresentationParameters[i].MultiSampleQuality;
489         localParameters[i].SwapEffect = pPresentationParameters[i].SwapEffect;
490         localParameters[i].hDeviceWindow = pPresentationParameters[i].hDeviceWindow;
491         localParameters[i].Windowed = pPresentationParameters[i].Windowed;
492         localParameters[i].EnableAutoDepthStencil = pPresentationParameters[i].EnableAutoDepthStencil;
493         localParameters[i].AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters[i].AutoDepthStencilFormat);
494         localParameters[i].Flags = pPresentationParameters[i].Flags;
495         localParameters[i].FullScreen_RefreshRateInHz = pPresentationParameters[i].FullScreen_RefreshRateInHz;
496         localParameters[i].PresentationInterval = pPresentationParameters[i].PresentationInterval;
497         localParameters[i].AutoRestoreDisplayMode = TRUE;
498     }
499
500     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, localParameters);
501     if (hr != D3D_OK) {
502         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
503         HeapFree(GetProcessHeap(), 0, object);
504         *ppReturnedDeviceInterface = NULL;
505     }
506
507     for (i = 0; i < count; ++i)
508     {
509         pPresentationParameters[i].BackBufferWidth = localParameters[i].BackBufferWidth;
510         pPresentationParameters[i].BackBufferHeight = localParameters[i].BackBufferHeight;
511         pPresentationParameters[i].BackBufferFormat = d3dformat_from_wined3dformat(localParameters[i].BackBufferFormat);
512         pPresentationParameters[i].BackBufferCount = localParameters[i].BackBufferCount;
513         pPresentationParameters[i].MultiSampleType = localParameters[i].MultiSampleType;
514         pPresentationParameters[i].MultiSampleQuality = localParameters[i].MultiSampleQuality;
515         pPresentationParameters[i].SwapEffect = localParameters[i].SwapEffect;
516         pPresentationParameters[i].hDeviceWindow = localParameters[i].hDeviceWindow;
517         pPresentationParameters[i].Windowed = localParameters[i].Windowed;
518         pPresentationParameters[i].EnableAutoDepthStencil = localParameters[i].EnableAutoDepthStencil;
519         pPresentationParameters[i].AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters[i].AutoDepthStencilFormat);
520         pPresentationParameters[i].Flags = localParameters[i].Flags;
521         pPresentationParameters[i].FullScreen_RefreshRateInHz = localParameters[i].FullScreen_RefreshRateInHz;
522         pPresentationParameters[i].PresentationInterval = localParameters[i].PresentationInterval;
523     }
524     HeapFree(GetProcessHeap(), 0, localParameters);
525
526     /* Initialize the converted declaration array. This creates a valid pointer and when adding decls HeapReAlloc
527      * can be used without further checking
528      */
529     object->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
530     wined3d_mutex_unlock();
531
532     return hr;
533 }
534
535 static UINT WINAPI IDirect3D9ExImpl_GetAdapterModeCountEx(IDirect3D9Ex *iface,
536         UINT adapter, const D3DDISPLAYMODEFILTER *filter)
537 {
538     FIXME("iface %p, adapter %u, filter %p stub!\n", iface, adapter, filter);
539
540     return D3DERR_DRIVERINTERNALERROR;
541 }
542
543 static HRESULT WINAPI IDirect3D9ExImpl_EnumAdapterModesEx(IDirect3D9Ex *iface,
544         UINT adapter, const D3DDISPLAYMODEFILTER *filter, UINT mode_idx, D3DDISPLAYMODEEX *mode)
545 {
546     FIXME("iface %p, adapter %u, filter %p, mode_idx %u, mode %p stub!\n",
547             iface, adapter, filter, mode_idx, mode);
548
549     return D3DERR_DRIVERINTERNALERROR;
550 }
551
552 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterDisplayModeEx(IDirect3D9Ex *iface,
553         UINT adapter, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
554 {
555     FIXME("iface %p, adapter %u, mode %p, rotation %p stub!\n",
556             iface, adapter, mode, rotation);
557
558     return D3DERR_DRIVERINTERNALERROR;
559 }
560
561 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3D9ExImpl_CreateDeviceEx(IDirect3D9Ex *iface,
562         UINT adapter, D3DDEVTYPE device_type, HWND focus_window, DWORD flags,
563         D3DPRESENT_PARAMETERS *parameters, D3DDISPLAYMODEEX *mode, IDirect3DDevice9Ex **device)
564 {
565     FIXME("iface %p, adapter %u, device_type %#x, focus_window %p, flags %#x,\n"
566             "parameters %p, mode %p, device %p stub!\n",
567             iface, adapter, device_type, focus_window, flags,
568             parameters, mode, device);
569
570     *device = NULL;
571
572     return D3DERR_DRIVERINTERNALERROR;
573 }
574
575 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterLUID(IDirect3D9Ex *iface, UINT adapter, LUID *luid)
576 {
577     FIXME("iface %p, adapter %u, luid %p stub!\n", iface, adapter, luid);
578
579     return D3DERR_DRIVERINTERNALERROR;
580 }
581
582
583 const IDirect3D9ExVtbl Direct3D9_Vtbl =
584 {
585     /* IUnknown */
586     IDirect3D9Impl_QueryInterface,
587     IDirect3D9Impl_AddRef,
588     IDirect3D9Impl_Release,
589     /* IDirect3D9 */
590     IDirect3D9Impl_RegisterSoftwareDevice,
591     IDirect3D9Impl_GetAdapterCount,
592     IDirect3D9Impl_GetAdapterIdentifier,
593     IDirect3D9Impl_GetAdapterModeCount,
594     IDirect3D9Impl_EnumAdapterModes,
595     IDirect3D9Impl_GetAdapterDisplayMode,
596     IDirect3D9Impl_CheckDeviceType,
597     IDirect3D9Impl_CheckDeviceFormat,
598     IDirect3D9Impl_CheckDeviceMultiSampleType,
599     IDirect3D9Impl_CheckDepthStencilMatch,
600     IDirect3D9Impl_CheckDeviceFormatConversion,
601     IDirect3D9Impl_GetDeviceCaps,
602     IDirect3D9Impl_GetAdapterMonitor,
603     IDirect3D9Impl_CreateDevice,
604     /* IDirect3D9Ex */
605     IDirect3D9ExImpl_GetAdapterModeCountEx,
606     IDirect3D9ExImpl_EnumAdapterModesEx,
607     IDirect3D9ExImpl_GetAdapterDisplayModeEx,
608     IDirect3D9ExImpl_CreateDeviceEx,
609     IDirect3D9ExImpl_GetAdapterLUID
610
611 };