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