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