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