shdocvw: Support URLs passed by reference in WebBrowser_Navigate2.
[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     /* This format is nothing special and it is supported perfectly.
203      * However, ati and nvidia driver on windows do not mark this format as
204      * supported (tested with the dxCapsViewer) and pretending to
205      * support this format uncovers a bug in Battlefield 1942 (fonts are missing)
206      * So do the same as Windows drivers and pretend not to support it on dx8 and 9
207      */
208     if(CheckFormat == D3DFMT_R8G8B8)
209     {
210         WARN("D3DFMT_R8G8B8 is not available on windows, returning D3DERR_NOTAVAILABLE\n");
211         return D3DERR_NOTAVAILABLE;
212     }
213
214     switch(RType) {
215         case D3DRTYPE_VERTEXBUFFER:
216         case D3DRTYPE_INDEXBUFFER:
217             WineD3DRType = WINED3DRTYPE_BUFFER;
218             break;
219
220         default:
221             WineD3DRType = RType;
222             break;
223     }
224
225     EnterCriticalSection(&d3d9_cs);
226     hr = IWineD3D_CheckDeviceFormat(This->WineD3D, Adapter, DeviceType, wined3dformat_from_d3dformat(AdapterFormat),
227             Usage, WineD3DRType, wined3dformat_from_d3dformat(CheckFormat), SURFACE_OPENGL);
228     LeaveCriticalSection(&d3d9_cs);
229     return hr;
230 }
231
232 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceMultiSampleType(LPDIRECT3D9EX iface,
233                                                            UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SurfaceFormat,
234                                                            BOOL Windowed, D3DMULTISAMPLE_TYPE MultiSampleType, DWORD* pQualityLevels) {
235     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
236     HRESULT hr;
237     TRACE("%p\n", This);
238
239     EnterCriticalSection(&d3d9_cs);
240     hr = IWineD3D_CheckDeviceMultiSampleType(This->WineD3D, Adapter, DeviceType,
241             wined3dformat_from_d3dformat(SurfaceFormat), Windowed, MultiSampleType, pQualityLevels);
242     LeaveCriticalSection(&d3d9_cs);
243     return hr;
244 }
245
246 static HRESULT WINAPI IDirect3D9Impl_CheckDepthStencilMatch(LPDIRECT3D9EX iface,
247                                                        UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT AdapterFormat,
248                                                        D3DFORMAT RenderTargetFormat, D3DFORMAT DepthStencilFormat) {
249     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
250     HRESULT hr;
251     TRACE("%p\n", This);
252
253     EnterCriticalSection(&d3d9_cs);
254     hr = IWineD3D_CheckDepthStencilMatch(This->WineD3D, Adapter, DeviceType,
255             wined3dformat_from_d3dformat(AdapterFormat), wined3dformat_from_d3dformat(RenderTargetFormat),
256             wined3dformat_from_d3dformat(DepthStencilFormat));
257     LeaveCriticalSection(&d3d9_cs);
258     return hr;
259 }
260
261 static HRESULT WINAPI IDirect3D9Impl_CheckDeviceFormatConversion(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DFORMAT SourceFormat, D3DFORMAT TargetFormat) {
262     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
263     HRESULT hr;
264     TRACE("%p\n", This);
265
266     EnterCriticalSection(&d3d9_cs);
267     hr = IWineD3D_CheckDeviceFormatConversion(This->WineD3D, Adapter, DeviceType,
268             wined3dformat_from_d3dformat(SourceFormat), wined3dformat_from_d3dformat(TargetFormat));
269     LeaveCriticalSection(&d3d9_cs);
270     return hr;
271 }
272
273 void filter_caps(D3DCAPS9* pCaps)
274 {
275
276     DWORD textureFilterCaps =
277         D3DPTFILTERCAPS_MINFPOINT      | D3DPTFILTERCAPS_MINFLINEAR    | D3DPTFILTERCAPS_MINFANISOTROPIC |
278         D3DPTFILTERCAPS_MINFPYRAMIDALQUAD                              | D3DPTFILTERCAPS_MINFGAUSSIANQUAD|
279         D3DPTFILTERCAPS_MIPFPOINT      | D3DPTFILTERCAPS_MIPFLINEAR    | D3DPTFILTERCAPS_MAGFPOINT       |
280         D3DPTFILTERCAPS_MAGFLINEAR     |D3DPTFILTERCAPS_MAGFANISOTROPIC|D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD|
281         D3DPTFILTERCAPS_MAGFGAUSSIANQUAD;
282     pCaps->TextureFilterCaps &= textureFilterCaps;
283     pCaps->CubeTextureFilterCaps &= textureFilterCaps;
284     pCaps->VolumeTextureFilterCaps &= textureFilterCaps;
285
286     pCaps->DevCaps &=
287         D3DDEVCAPS_EXECUTESYSTEMMEMORY | D3DDEVCAPS_EXECUTEVIDEOMEMORY | D3DDEVCAPS_TLVERTEXSYSTEMMEMORY |
288         D3DDEVCAPS_TLVERTEXVIDEOMEMORY | D3DDEVCAPS_TEXTURESYSTEMMEMORY| D3DDEVCAPS_TEXTUREVIDEOMEMORY   |
289         D3DDEVCAPS_DRAWPRIMTLVERTEX    | D3DDEVCAPS_CANRENDERAFTERFLIP | D3DDEVCAPS_TEXTURENONLOCALVIDMEM|
290         D3DDEVCAPS_DRAWPRIMITIVES2     | D3DDEVCAPS_SEPARATETEXTUREMEMORIES                              |
291         D3DDEVCAPS_DRAWPRIMITIVES2EX   | D3DDEVCAPS_HWTRANSFORMANDLIGHT| D3DDEVCAPS_CANBLTSYSTONONLOCAL  |
292         D3DDEVCAPS_HWRASTERIZATION     | D3DDEVCAPS_PUREDEVICE         | D3DDEVCAPS_QUINTICRTPATCHES     |
293         D3DDEVCAPS_RTPATCHES           | D3DDEVCAPS_RTPATCHHANDLEZERO  | D3DDEVCAPS_NPATCHES;
294
295     pCaps->ShadeCaps &=
296         D3DPSHADECAPS_COLORGOURAUDRGB  | D3DPSHADECAPS_SPECULARGOURAUDRGB |
297         D3DPSHADECAPS_ALPHAGOURAUDBLEND | D3DPSHADECAPS_FOGGOURAUD;
298
299     pCaps->RasterCaps &=
300         D3DPRASTERCAPS_DITHER          | D3DPRASTERCAPS_ZTEST          | D3DPRASTERCAPS_FOGVERTEX        |
301         D3DPRASTERCAPS_FOGTABLE        | D3DPRASTERCAPS_MIPMAPLODBIAS  | D3DPRASTERCAPS_ZBUFFERLESSHSR   |
302         D3DPRASTERCAPS_FOGRANGE        | D3DPRASTERCAPS_ANISOTROPY     | D3DPRASTERCAPS_WBUFFER          |
303         D3DPRASTERCAPS_WFOG            | D3DPRASTERCAPS_ZFOG           | D3DPRASTERCAPS_COLORPERSPECTIVE |
304         D3DPRASTERCAPS_SCISSORTEST     | D3DPRASTERCAPS_SLOPESCALEDEPTHBIAS                              |
305         D3DPRASTERCAPS_DEPTHBIAS       | D3DPRASTERCAPS_MULTISAMPLE_TOGGLE;
306
307     pCaps->DevCaps2 &=
308         D3DDEVCAPS2_STREAMOFFSET       | D3DDEVCAPS2_DMAPNPATCH        | D3DDEVCAPS2_ADAPTIVETESSRTPATCH |
309         D3DDEVCAPS2_ADAPTIVETESSNPATCH | D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES                       |
310         D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH| D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET;
311
312     pCaps->Caps2 &=
313         D3DCAPS2_FULLSCREENGAMMA       | D3DCAPS2_CANCALIBRATEGAMMA    | D3DCAPS2_RESERVED               |
314         D3DCAPS2_CANMANAGERESOURCE     | D3DCAPS2_DYNAMICTEXTURES      | D3DCAPS2_CANAUTOGENMIPMAP;
315
316     pCaps->VertexProcessingCaps &=
317         D3DVTXPCAPS_TEXGEN             | D3DVTXPCAPS_MATERIALSOURCE7   | D3DVTXPCAPS_DIRECTIONALLIGHTS   |
318         D3DVTXPCAPS_POSITIONALLIGHTS   | D3DVTXPCAPS_LOCALVIEWER       | D3DVTXPCAPS_TWEENING            |
319         D3DVTXPCAPS_TEXGEN_SPHEREMAP   | D3DVTXPCAPS_NO_TEXGEN_NONLOCALVIEWER;
320
321     pCaps->TextureCaps &=
322         D3DPTEXTURECAPS_PERSPECTIVE    | D3DPTEXTURECAPS_POW2          | D3DPTEXTURECAPS_ALPHA           |
323         D3DPTEXTURECAPS_SQUAREONLY     | D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE                        |
324         D3DPTEXTURECAPS_ALPHAPALETTE   | D3DPTEXTURECAPS_NONPOW2CONDITIONAL                              |
325         D3DPTEXTURECAPS_PROJECTED      | D3DPTEXTURECAPS_CUBEMAP       | D3DPTEXTURECAPS_VOLUMEMAP       |
326         D3DPTEXTURECAPS_MIPMAP         | D3DPTEXTURECAPS_MIPVOLUMEMAP  | D3DPTEXTURECAPS_MIPCUBEMAP      |
327         D3DPTEXTURECAPS_CUBEMAP_POW2   | D3DPTEXTURECAPS_VOLUMEMAP_POW2| D3DPTEXTURECAPS_NOPROJECTEDBUMPENV;
328
329     pCaps->MaxVertexShaderConst = min(D3D9_MAX_VERTEX_SHADER_CONSTANTF, pCaps->MaxVertexShaderConst);
330 }
331
332 static HRESULT WINAPI IDirect3D9Impl_GetDeviceCaps(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType, D3DCAPS9* pCaps) {
333     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
334     HRESULT hrc = D3D_OK;
335     WINED3DCAPS *pWineCaps;
336
337     TRACE("(%p) Relay %d %u %p\n", This, Adapter, DeviceType, pCaps);
338
339     if(NULL == pCaps){
340         return D3DERR_INVALIDCALL;
341     }
342     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
343     if(pWineCaps == NULL){
344         return D3DERR_INVALIDCALL; /*well this is what MSDN says to return*/
345     }
346     memset(pCaps, 0, sizeof(*pCaps));
347     EnterCriticalSection(&d3d9_cs);
348     hrc = IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, pWineCaps);
349     LeaveCriticalSection(&d3d9_cs);
350     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
351     HeapFree(GetProcessHeap(), 0, pWineCaps);
352
353     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
354     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
355
356     filter_caps(pCaps);
357
358     TRACE("(%p) returning %p\n", This, pCaps);
359     return hrc;
360 }
361
362 static HMONITOR WINAPI IDirect3D9Impl_GetAdapterMonitor(LPDIRECT3D9EX iface, UINT Adapter) {
363     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
364     HMONITOR ret;
365     TRACE("%p\n", This);
366
367     EnterCriticalSection(&d3d9_cs);
368     ret = IWineD3D_GetAdapterMonitor(This->WineD3D, Adapter);
369     LeaveCriticalSection(&d3d9_cs);
370     return ret;
371 }
372
373 ULONG WINAPI D3D9CB_DestroyRenderTarget(IWineD3DSurface *pSurface) {
374     IDirect3DSurface9Impl* surfaceParent;
375     TRACE("(%p) call back\n", pSurface);
376
377     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
378     surfaceParent->isImplicit = FALSE;
379     /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
380     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
381 }
382
383 ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *pSwapChain) {
384     IDirect3DSwapChain9Impl* swapChainParent;
385     TRACE("(%p) call back\n", pSwapChain);
386
387     IWineD3DSwapChain_GetParent(pSwapChain,(IUnknown **) &swapChainParent);
388     swapChainParent->isImplicit = FALSE;
389     /* Swap chain had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
390     return IDirect3DSwapChain9_Release((IDirect3DSwapChain9*) swapChainParent);
391 }
392
393 ULONG WINAPI D3D9CB_DestroyDepthStencilSurface(IWineD3DSurface *pSurface) {
394     IDirect3DSurface9Impl* surfaceParent;
395     TRACE("(%p) call back\n", pSurface);
396
397     IWineD3DSurface_GetParent(pSurface, (IUnknown **) &surfaceParent);
398     surfaceParent->isImplicit = FALSE;
399     /* Surface had refcount of 0 GetParent addrefed to 1, so 1 Release is enough */
400     return IDirect3DSurface9_Release((IDirect3DSurface9*) surfaceParent);
401 }
402
403 static HRESULT WINAPI IDirect3D9Impl_CreateDevice(LPDIRECT3D9EX iface, UINT Adapter, D3DDEVTYPE DeviceType,
404                                                   HWND hFocusWindow, DWORD BehaviourFlags,
405                                                   D3DPRESENT_PARAMETERS* pPresentationParameters,
406                                                   IDirect3DDevice9** ppReturnedDeviceInterface) {
407
408     IDirect3D9Impl       *This   = (IDirect3D9Impl *)iface;
409     IDirect3DDevice9Impl *object = NULL;
410     WINED3DPRESENT_PARAMETERS *localParameters;
411     UINT i, count = 1;
412     HRESULT hr;
413     TRACE("(%p) Relay\n", This);
414
415     /* Check the validity range of the adapter parameter */
416     if (Adapter >= IDirect3D9Impl_GetAdapterCount(iface)) {
417         *ppReturnedDeviceInterface = NULL;
418         return D3DERR_INVALIDCALL;
419     }
420
421     /* Allocate the storage for the device object */
422     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DDevice9Impl));
423     if (NULL == object) {
424         FIXME("Allocation of memory failed\n");
425         *ppReturnedDeviceInterface = NULL;
426         return D3DERR_OUTOFVIDEOMEMORY;
427     }
428
429     object->lpVtbl = &Direct3DDevice9_Vtbl;
430     object->device_parent_vtbl = &d3d9_wined3d_device_parent_vtbl;
431     object->ref = 1;
432     *ppReturnedDeviceInterface = (IDirect3DDevice9 *)object;
433
434     /* Allocate an associated WineD3DDevice object */
435     EnterCriticalSection(&d3d9_cs);
436     hr = IWineD3D_CreateDevice(This->WineD3D, Adapter, DeviceType, hFocusWindow, BehaviourFlags,
437             (IUnknown *)object, (IWineD3DDeviceParent *)&object->device_parent_vtbl, &object->WineD3DDevice);
438     if (hr != D3D_OK) {
439         HeapFree(GetProcessHeap(), 0, object);
440         *ppReturnedDeviceInterface = NULL;
441         LeaveCriticalSection(&d3d9_cs);
442         return hr;
443     }
444
445     TRACE("(%p) : Created Device %p\n", This, object);
446
447     if (BehaviourFlags & D3DCREATE_ADAPTERGROUP_DEVICE)
448     {
449         WINED3DCAPS caps;
450
451         IWineD3D_GetDeviceCaps(This->WineD3D, Adapter, DeviceType, &caps);
452         count = caps.NumberOfAdaptersInGroup;
453     }
454
455     if(BehaviourFlags & D3DCREATE_MULTITHREADED) {
456         IWineD3DDevice_SetMultithreaded(object->WineD3DDevice);
457     }
458
459     localParameters = HeapAlloc(GetProcessHeap(), 0, sizeof(*localParameters) * count);
460     for (i = 0; i < count; ++i)
461     {
462         localParameters[i].BackBufferWidth = pPresentationParameters[i].BackBufferWidth;
463         localParameters[i].BackBufferHeight = pPresentationParameters[i].BackBufferHeight;
464         localParameters[i].BackBufferFormat = wined3dformat_from_d3dformat(pPresentationParameters[i].BackBufferFormat);
465         localParameters[i].BackBufferCount = pPresentationParameters[i].BackBufferCount;
466         localParameters[i].MultiSampleType = pPresentationParameters[i].MultiSampleType;
467         localParameters[i].MultiSampleQuality = pPresentationParameters[i].MultiSampleQuality;
468         localParameters[i].SwapEffect = pPresentationParameters[i].SwapEffect;
469         localParameters[i].hDeviceWindow = pPresentationParameters[i].hDeviceWindow;
470         localParameters[i].Windowed = pPresentationParameters[i].Windowed;
471         localParameters[i].EnableAutoDepthStencil = pPresentationParameters[i].EnableAutoDepthStencil;
472         localParameters[i].AutoDepthStencilFormat = wined3dformat_from_d3dformat(pPresentationParameters[i].AutoDepthStencilFormat);
473         localParameters[i].Flags = pPresentationParameters[i].Flags;
474         localParameters[i].FullScreen_RefreshRateInHz = pPresentationParameters[i].FullScreen_RefreshRateInHz;
475         localParameters[i].PresentationInterval = pPresentationParameters[i].PresentationInterval;
476         localParameters[i].AutoRestoreDisplayMode = TRUE;
477     }
478
479     hr = IWineD3DDevice_Init3D(object->WineD3DDevice, localParameters);
480     if (hr != D3D_OK) {
481         FIXME("(%p) D3D Initialization failed for WineD3DDevice %p\n", This, object->WineD3DDevice);
482         HeapFree(GetProcessHeap(), 0, object);
483         *ppReturnedDeviceInterface = NULL;
484     }
485
486     for (i = 0; i < count; ++i)
487     {
488         pPresentationParameters[i].BackBufferWidth = localParameters[i].BackBufferWidth;
489         pPresentationParameters[i].BackBufferHeight = localParameters[i].BackBufferHeight;
490         pPresentationParameters[i].BackBufferFormat = d3dformat_from_wined3dformat(localParameters[i].BackBufferFormat);
491         pPresentationParameters[i].BackBufferCount = localParameters[i].BackBufferCount;
492         pPresentationParameters[i].MultiSampleType = localParameters[i].MultiSampleType;
493         pPresentationParameters[i].MultiSampleQuality = localParameters[i].MultiSampleQuality;
494         pPresentationParameters[i].SwapEffect = localParameters[i].SwapEffect;
495         pPresentationParameters[i].hDeviceWindow = localParameters[i].hDeviceWindow;
496         pPresentationParameters[i].Windowed = localParameters[i].Windowed;
497         pPresentationParameters[i].EnableAutoDepthStencil = localParameters[i].EnableAutoDepthStencil;
498         pPresentationParameters[i].AutoDepthStencilFormat = d3dformat_from_wined3dformat(localParameters[i].AutoDepthStencilFormat);
499         pPresentationParameters[i].Flags = localParameters[i].Flags;
500         pPresentationParameters[i].FullScreen_RefreshRateInHz = localParameters[i].FullScreen_RefreshRateInHz;
501         pPresentationParameters[i].PresentationInterval = localParameters[i].PresentationInterval;
502     }
503     HeapFree(GetProcessHeap(), 0, localParameters);
504
505     /* Initialize the converted declaration array. This creates a valid pointer and when adding decls HeapReAlloc
506      * can be used without further checking
507      */
508     object->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
509     LeaveCriticalSection(&d3d9_cs);
510
511     return hr;
512 }
513
514 static UINT WINAPI IDirect3D9ExImpl_GetAdapterModeCountEx(IDirect3D9Ex *iface, UINT Adapter, CONST D3DDISPLAYMODEFILTER *pFilter) {
515     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
516     FIXME("(%p)->(%d, %p): Stub!\n", This, Adapter, pFilter);
517     return D3DERR_DRIVERINTERNALERROR;
518 }
519
520 static HRESULT WINAPI IDirect3D9ExImpl_EnumAdapterModesEx(IDirect3D9Ex *iface, UINT Adapter, CONST D3DDISPLAYMODEFILTER *pFilter, UINT Mode, D3DDISPLAYMODEEX* pMode) {
521     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
522     FIXME("(%p)->(%d, %p, %p): Stub!\n", This, Adapter, pFilter, pMode);
523     return D3DERR_DRIVERINTERNALERROR;
524 }
525
526 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterDisplayModeEx(IDirect3D9Ex *iface, UINT Adapter, D3DDISPLAYMODEEX *pMode, D3DDISPLAYROTATION *pRotation) {
527     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
528     FIXME("(%p)->(%d, %p, %p): Stub!\n", This, Adapter, pMode, pRotation);
529     return D3DERR_DRIVERINTERNALERROR;
530 }
531
532 static HRESULT WINAPI IDirect3D9ExImpl_CreateDeviceEx(IDirect3D9Ex *iface, UINT Adapter, D3DDEVTYPE DeviceType, HWND hFocusWindow, DWORD BehaviorFlags, D3DPRESENT_PARAMETERS* pPresentationParameters, D3DDISPLAYMODEEX* pFullscreenDisplayMode, struct IDirect3DDevice9Ex **ppReturnedDeviceInterface) {
533     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
534     FIXME("(%p)->(%d, %d, %p, 0x%08x, %p, %p, %p): Stub!\n", This, Adapter, DeviceType, hFocusWindow, BehaviorFlags, pPresentationParameters, pFullscreenDisplayMode, ppReturnedDeviceInterface);
535     *ppReturnedDeviceInterface = NULL;
536     return D3DERR_DRIVERINTERNALERROR;
537 }
538
539 static HRESULT WINAPI IDirect3D9ExImpl_GetAdapterLUID(IDirect3D9Ex *iface, UINT Adapter, LUID *pLUID) {
540     IDirect3D9Impl *This = (IDirect3D9Impl *)iface;
541     FIXME("(%p)->(%d, %p)\n", This, Adapter, pLUID);
542     return D3DERR_DRIVERINTERNALERROR;
543 }
544
545
546 const IDirect3D9ExVtbl Direct3D9_Vtbl =
547 {
548     /* IUnknown */
549     IDirect3D9Impl_QueryInterface,
550     IDirect3D9Impl_AddRef,
551     IDirect3D9Impl_Release,
552     /* IDirect3D9 */
553     IDirect3D9Impl_RegisterSoftwareDevice,
554     IDirect3D9Impl_GetAdapterCount,
555     IDirect3D9Impl_GetAdapterIdentifier,
556     IDirect3D9Impl_GetAdapterModeCount,
557     IDirect3D9Impl_EnumAdapterModes,
558     IDirect3D9Impl_GetAdapterDisplayMode,
559     IDirect3D9Impl_CheckDeviceType,
560     IDirect3D9Impl_CheckDeviceFormat,
561     IDirect3D9Impl_CheckDeviceMultiSampleType,
562     IDirect3D9Impl_CheckDepthStencilMatch,
563     IDirect3D9Impl_CheckDeviceFormatConversion,
564     IDirect3D9Impl_GetDeviceCaps,
565     IDirect3D9Impl_GetAdapterMonitor,
566     IDirect3D9Impl_CreateDevice,
567     /* IDirect3D9Ex */
568     IDirect3D9ExImpl_GetAdapterModeCountEx,
569     IDirect3D9ExImpl_EnumAdapterModesEx,
570     IDirect3D9ExImpl_GetAdapterDisplayModeEx,
571     IDirect3D9ExImpl_CreateDeviceEx,
572     IDirect3D9ExImpl_GetAdapterLUID
573
574 };