msi: Refactor the Installer.RegistryValue method into InstallerImpl_RegistryValue.
[wine] / dlls / d3d9 / device.c
1 /*
2  * IDirect3DDevice9 implementation
3  *
4  * Copyright 2002-2005 Jason Edmeades
5  * Copyright 2002-2005 Raphael Junqueira
6  * Copyright 2005 Oliver Stieber
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21  */
22
23 #include "config.h"
24 #include "d3d9_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
27
28 D3DFORMAT d3dformat_from_wined3dformat(WINED3DFORMAT format)
29 {
30     BYTE *c = (BYTE *)&format;
31
32     /* Don't translate FOURCC formats */
33     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
34
35     switch(format)
36     {
37         case WINED3DFMT_UNKNOWN: return D3DFMT_UNKNOWN;
38         case WINED3DFMT_B8G8R8_UNORM: return D3DFMT_R8G8B8;
39         case WINED3DFMT_B8G8R8A8_UNORM: return D3DFMT_A8R8G8B8;
40         case WINED3DFMT_B8G8R8X8_UNORM: return D3DFMT_X8R8G8B8;
41         case WINED3DFMT_B5G6R5_UNORM: return D3DFMT_R5G6B5;
42         case WINED3DFMT_B5G5R5X1_UNORM: return D3DFMT_X1R5G5B5;
43         case WINED3DFMT_B5G5R5A1_UNORM: return D3DFMT_A1R5G5B5;
44         case WINED3DFMT_B4G4R4A4_UNORM: return D3DFMT_A4R4G4B4;
45         case WINED3DFMT_B2G3R3_UNORM: return D3DFMT_R3G3B2;
46         case WINED3DFMT_A8_UNORM: return D3DFMT_A8;
47         case WINED3DFMT_B2G3R3A8_UNORM: return D3DFMT_A8R3G3B2;
48         case WINED3DFMT_B4G4R4X4_UNORM: return D3DFMT_X4R4G4B4;
49         case WINED3DFMT_R10G10B10A2_UNORM: return D3DFMT_A2B10G10R10;
50         case WINED3DFMT_R8G8B8A8_UNORM: return D3DFMT_A8B8G8R8;
51         case WINED3DFMT_R8G8B8X8_UNORM: return D3DFMT_X8B8G8R8;
52         case WINED3DFMT_R16G16_UNORM: return D3DFMT_G16R16;
53         case WINED3DFMT_B10G10R10A2_UNORM: return D3DFMT_A2R10G10B10;
54         case WINED3DFMT_R16G16B16A16_UNORM: return D3DFMT_A16B16G16R16;
55         case WINED3DFMT_P8_UINT_A8_UNORM: return D3DFMT_A8P8;
56         case WINED3DFMT_P8_UINT: return D3DFMT_P8;
57         case WINED3DFMT_L8_UNORM: return D3DFMT_L8;
58         case WINED3DFMT_L8A8_UNORM: return D3DFMT_A8L8;
59         case WINED3DFMT_L4A4_UNORM: return D3DFMT_A4L4;
60         case WINED3DFMT_R8G8_SNORM: return D3DFMT_V8U8;
61         case WINED3DFMT_R5G5_SNORM_L6_UNORM: return D3DFMT_L6V5U5;
62         case WINED3DFMT_R8G8_SNORM_L8X8_UNORM: return D3DFMT_X8L8V8U8;
63         case WINED3DFMT_R8G8B8A8_SNORM: return D3DFMT_Q8W8V8U8;
64         case WINED3DFMT_R16G16_SNORM: return D3DFMT_V16U16;
65         case WINED3DFMT_R10G10B10_SNORM_A2_UNORM: return D3DFMT_A2W10V10U10;
66         case WINED3DFMT_D16_LOCKABLE: return D3DFMT_D16_LOCKABLE;
67         case WINED3DFMT_D32_UNORM: return D3DFMT_D32;
68         case WINED3DFMT_S1_UINT_D15_UNORM: return D3DFMT_D15S1;
69         case WINED3DFMT_D24_UNORM_S8_UINT: return D3DFMT_D24S8;
70         case WINED3DFMT_X8D24_UNORM: return D3DFMT_D24X8;
71         case WINED3DFMT_S4X4_UINT_D24_UNORM: return D3DFMT_D24X4S4;
72         case WINED3DFMT_D16_UNORM: return D3DFMT_D16;
73         case WINED3DFMT_L16_UNORM: return D3DFMT_L16;
74         case WINED3DFMT_D32_FLOAT: return D3DFMT_D32F_LOCKABLE;
75         case WINED3DFMT_S8_UINT_D24_FLOAT: return D3DFMT_D24FS8;
76         case WINED3DFMT_VERTEXDATA: return D3DFMT_VERTEXDATA;
77         case WINED3DFMT_R16_UINT: return D3DFMT_INDEX16;
78         case WINED3DFMT_R32_UINT: return D3DFMT_INDEX32;
79         case WINED3DFMT_R16G16B16A16_SNORM: return D3DFMT_Q16W16V16U16;
80         case WINED3DFMT_R16_FLOAT: return D3DFMT_R16F;
81         case WINED3DFMT_R16G16_FLOAT: return D3DFMT_G16R16F;
82         case WINED3DFMT_R16G16B16A16_FLOAT: return D3DFMT_A16B16G16R16F;
83         case WINED3DFMT_R32_FLOAT: return D3DFMT_R32F;
84         case WINED3DFMT_R32G32_FLOAT: return D3DFMT_G32R32F;
85         case WINED3DFMT_R32G32B32A32_FLOAT: return D3DFMT_A32B32G32R32F;
86         case WINED3DFMT_R8G8_SNORM_Cx: return D3DFMT_CxV8U8;
87         default:
88             FIXME("Unhandled WINED3DFORMAT %#x\n", format);
89             return D3DFMT_UNKNOWN;
90     }
91 }
92
93 WINED3DFORMAT wined3dformat_from_d3dformat(D3DFORMAT format)
94 {
95     BYTE *c = (BYTE *)&format;
96
97     /* Don't translate FOURCC formats */
98     if (isprint(c[0]) && isprint(c[1]) && isprint(c[2]) && isprint(c[3])) return format;
99
100     switch(format)
101     {
102         case D3DFMT_UNKNOWN: return WINED3DFMT_UNKNOWN;
103         case D3DFMT_R8G8B8: return WINED3DFMT_B8G8R8_UNORM;
104         case D3DFMT_A8R8G8B8: return WINED3DFMT_B8G8R8A8_UNORM;
105         case D3DFMT_X8R8G8B8: return WINED3DFMT_B8G8R8X8_UNORM;
106         case D3DFMT_R5G6B5: return WINED3DFMT_B5G6R5_UNORM;
107         case D3DFMT_X1R5G5B5: return WINED3DFMT_B5G5R5X1_UNORM;
108         case D3DFMT_A1R5G5B5: return WINED3DFMT_B5G5R5A1_UNORM;
109         case D3DFMT_A4R4G4B4: return WINED3DFMT_B4G4R4A4_UNORM;
110         case D3DFMT_R3G3B2: return WINED3DFMT_B2G3R3_UNORM;
111         case D3DFMT_A8: return WINED3DFMT_A8_UNORM;
112         case D3DFMT_A8R3G3B2: return WINED3DFMT_B2G3R3A8_UNORM;
113         case D3DFMT_X4R4G4B4: return WINED3DFMT_B4G4R4X4_UNORM;
114         case D3DFMT_A2B10G10R10: return WINED3DFMT_R10G10B10A2_UNORM;
115         case D3DFMT_A8B8G8R8: return WINED3DFMT_R8G8B8A8_UNORM;
116         case D3DFMT_X8B8G8R8: return WINED3DFMT_R8G8B8X8_UNORM;
117         case D3DFMT_G16R16: return WINED3DFMT_R16G16_UNORM;
118         case D3DFMT_A2R10G10B10: return WINED3DFMT_B10G10R10A2_UNORM;
119         case D3DFMT_A16B16G16R16: return WINED3DFMT_R16G16B16A16_UNORM;
120         case D3DFMT_A8P8: return WINED3DFMT_P8_UINT_A8_UNORM;
121         case D3DFMT_P8: return WINED3DFMT_P8_UINT;
122         case D3DFMT_L8: return WINED3DFMT_L8_UNORM;
123         case D3DFMT_A8L8: return WINED3DFMT_L8A8_UNORM;
124         case D3DFMT_A4L4: return WINED3DFMT_L4A4_UNORM;
125         case D3DFMT_V8U8: return WINED3DFMT_R8G8_SNORM;
126         case D3DFMT_L6V5U5: return WINED3DFMT_R5G5_SNORM_L6_UNORM;
127         case D3DFMT_X8L8V8U8: return WINED3DFMT_R8G8_SNORM_L8X8_UNORM;
128         case D3DFMT_Q8W8V8U8: return WINED3DFMT_R8G8B8A8_SNORM;
129         case D3DFMT_V16U16: return WINED3DFMT_R16G16_SNORM;
130         case D3DFMT_A2W10V10U10: return WINED3DFMT_R10G10B10_SNORM_A2_UNORM;
131         case D3DFMT_D16_LOCKABLE: return WINED3DFMT_D16_LOCKABLE;
132         case D3DFMT_D32: return WINED3DFMT_D32_UNORM;
133         case D3DFMT_D15S1: return WINED3DFMT_S1_UINT_D15_UNORM;
134         case D3DFMT_D24S8: return WINED3DFMT_D24_UNORM_S8_UINT;
135         case D3DFMT_D24X8: return WINED3DFMT_X8D24_UNORM;
136         case D3DFMT_D24X4S4: return WINED3DFMT_S4X4_UINT_D24_UNORM;
137         case D3DFMT_D16: return WINED3DFMT_D16_UNORM;
138         case D3DFMT_L16: return WINED3DFMT_L16_UNORM;
139         case D3DFMT_D32F_LOCKABLE: return WINED3DFMT_D32_FLOAT;
140         case D3DFMT_D24FS8: return WINED3DFMT_S8_UINT_D24_FLOAT;
141         case D3DFMT_VERTEXDATA: return WINED3DFMT_VERTEXDATA;
142         case D3DFMT_INDEX16: return WINED3DFMT_R16_UINT;
143         case D3DFMT_INDEX32: return WINED3DFMT_R32_UINT;
144         case D3DFMT_Q16W16V16U16: return WINED3DFMT_R16G16B16A16_SNORM;
145         case D3DFMT_R16F: return WINED3DFMT_R16_FLOAT;
146         case D3DFMT_G16R16F: return WINED3DFMT_R16G16_FLOAT;
147         case D3DFMT_A16B16G16R16F: return WINED3DFMT_R16G16B16A16_FLOAT;
148         case D3DFMT_R32F: return WINED3DFMT_R32_FLOAT;
149         case D3DFMT_G32R32F: return WINED3DFMT_R32G32_FLOAT;
150         case D3DFMT_A32B32G32R32F: return WINED3DFMT_R32G32B32A32_FLOAT;
151         case D3DFMT_CxV8U8: return WINED3DFMT_R8G8_SNORM_Cx;
152         default:
153             FIXME("Unhandled D3DFORMAT %#x\n", format);
154             return WINED3DFMT_UNKNOWN;
155     }
156 }
157
158 static UINT vertex_count_from_primitive_count(D3DPRIMITIVETYPE primitive_type, UINT primitive_count)
159 {
160     switch(primitive_type)
161     {
162         case D3DPT_POINTLIST:
163             return primitive_count;
164
165         case D3DPT_LINELIST:
166             return primitive_count * 2;
167
168         case D3DPT_LINESTRIP:
169             return primitive_count + 1;
170
171         case D3DPT_TRIANGLELIST:
172             return primitive_count * 3;
173
174         case D3DPT_TRIANGLESTRIP:
175         case D3DPT_TRIANGLEFAN:
176             return primitive_count + 2;
177
178         default:
179             FIXME("Unhandled primitive type %#x\n", primitive_type);
180             return 0;
181     }
182 }
183
184 static ULONG WINAPI D3D9CB_DestroySwapChain(IWineD3DSwapChain *swapchain)
185 {
186     IDirect3DSwapChain9Impl *parent;
187
188     TRACE("swapchain %p.\n", swapchain);
189
190     IWineD3DSwapChain_GetParent(swapchain, (IUnknown **)&parent);
191     parent->isImplicit = FALSE;
192     return IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)parent);
193 }
194
195 /* IDirect3D IUnknown parts follow: */
196 static HRESULT WINAPI IDirect3DDevice9Impl_QueryInterface(LPDIRECT3DDEVICE9EX iface, REFIID riid, LPVOID* ppobj) {
197     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
198     IDirect3D9 *d3d;
199     IDirect3D9Impl *d3dimpl;
200
201     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), ppobj);
202
203     if (IsEqualGUID(riid, &IID_IUnknown)
204         || IsEqualGUID(riid, &IID_IDirect3DDevice9)) {
205         IDirect3DDevice9Ex_AddRef(iface);
206         *ppobj = This;
207         TRACE("Returning IDirect3DDevice9 interface at %p\n", *ppobj);
208         return S_OK;
209     } else if(IsEqualGUID(riid, &IID_IDirect3DDevice9Ex)) {
210         /* Find out if the creating d3d9 interface was created with Direct3DCreate9Ex.
211          * It doesn't matter with which function the device was created.
212          */
213         IDirect3DDevice9_GetDirect3D(iface, &d3d);
214         d3dimpl = (IDirect3D9Impl *) d3d;
215
216         if(d3dimpl->extended) {
217             *ppobj = iface;
218             IDirect3DDevice9Ex_AddRef((IDirect3DDevice9Ex *) *ppobj);
219             IDirect3D9_Release(d3d);
220             TRACE("Returning IDirect3DDevice9Ex interface at %p\n", *ppobj);
221             return S_OK;
222         } else {
223             WARN("IDirect3D9 instance wasn't created with CreateDirect3D9Ex, returning E_NOINTERFACE\n");
224             IDirect3D9_Release(d3d);
225             *ppobj = NULL;
226             return E_NOINTERFACE;
227         }
228     }
229
230     if (IsEqualGUID(riid, &IID_IWineD3DDeviceParent))
231     {
232         IUnknown_AddRef((IUnknown *)&This->device_parent_vtbl);
233         *ppobj = &This->device_parent_vtbl;
234         return S_OK;
235     }
236
237     WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
238     *ppobj = NULL;
239     return E_NOINTERFACE;
240 }
241
242 static ULONG WINAPI IDirect3DDevice9Impl_AddRef(LPDIRECT3DDEVICE9EX iface) {
243     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
244     ULONG ref = InterlockedIncrement(&This->ref);
245
246     TRACE("%p increasing refcount to %u.\n", iface, ref);
247
248     return ref;
249 }
250
251 static ULONG WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Release(LPDIRECT3DDEVICE9EX iface) {
252     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
253     ULONG ref;
254
255     if (This->inDestruction) return 0;
256     ref = InterlockedDecrement(&This->ref);
257
258     TRACE("%p decreasing refcount to %u.\n", iface, ref);
259
260     if (ref == 0) {
261       unsigned i;
262       This->inDestruction = TRUE;
263
264       wined3d_mutex_lock();
265       for(i = 0; i < This->numConvertedDecls; i++) {
266           /* Unless Wine is buggy or the app has a bug the refcount will be 0, because decls hold a reference to the
267            * device
268            */
269           IDirect3DVertexDeclaration9Impl_Destroy(This->convertedDecls[i]);
270       }
271       HeapFree(GetProcessHeap(), 0, This->convertedDecls);
272
273       IWineD3DDevice_Uninit3D(This->WineD3DDevice, D3D9CB_DestroySwapChain);
274       IWineD3DDevice_Release(This->WineD3DDevice);
275       wined3d_mutex_unlock();
276
277       HeapFree(GetProcessHeap(), 0, This);
278     }
279     return ref;
280 }
281
282 /* IDirect3DDevice Interface follow: */
283 static HRESULT WINAPI IDirect3DDevice9Impl_TestCooperativeLevel(IDirect3DDevice9Ex *iface)
284 {
285     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
286
287     TRACE("iface %p.\n", iface);
288
289     if (This->notreset)
290     {
291         TRACE("D3D9 device is marked not reset.\n");
292         return D3DERR_DEVICENOTRESET;
293     }
294
295     return D3D_OK;
296 }
297
298 static UINT     WINAPI  IDirect3DDevice9Impl_GetAvailableTextureMem(LPDIRECT3DDEVICE9EX iface) {
299     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
300     HRESULT hr;
301
302     TRACE("iface %p.\n", iface);
303
304     wined3d_mutex_lock();
305     hr = IWineD3DDevice_GetAvailableTextureMem(This->WineD3DDevice);
306     wined3d_mutex_unlock();
307
308     return hr;
309 }
310
311 static HRESULT  WINAPI  IDirect3DDevice9Impl_EvictManagedResources(LPDIRECT3DDEVICE9EX iface) {
312     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
313     HRESULT hr;
314
315     TRACE("iface %p.\n", iface);
316
317     wined3d_mutex_lock();
318     hr = IWineD3DDevice_EvictManagedResources(This->WineD3DDevice);
319     wined3d_mutex_unlock();
320
321     return hr;
322 }
323
324 static HRESULT WINAPI IDirect3DDevice9Impl_GetDirect3D(LPDIRECT3DDEVICE9EX iface, IDirect3D9** ppD3D9) {
325     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
326     HRESULT hr = D3D_OK;
327     IWineD3D* pWineD3D;
328
329     TRACE("iface %p, d3d9 %p.\n", iface, ppD3D9);
330
331     if (NULL == ppD3D9) {
332         return D3DERR_INVALIDCALL;
333     }
334
335     wined3d_mutex_lock();
336     hr = IWineD3DDevice_GetDirect3D(This->WineD3DDevice, &pWineD3D);
337     if (hr == D3D_OK && pWineD3D != NULL)
338     {
339         IWineD3D_GetParent(pWineD3D,(IUnknown **)ppD3D9);
340         IWineD3D_Release(pWineD3D);
341     } else {
342         FIXME("Call to IWineD3DDevice_GetDirect3D failed\n");
343         *ppD3D9 = NULL;
344     }
345     TRACE("(%p) returning %p\n", This, *ppD3D9);
346     wined3d_mutex_unlock();
347
348     return hr;
349 }
350
351 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDeviceCaps(LPDIRECT3DDEVICE9EX iface, D3DCAPS9* pCaps) {
352     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
353     HRESULT hrc = D3D_OK;
354     WINED3DCAPS *pWineCaps;
355
356     TRACE("iface %p, caps %p.\n", iface, pCaps);
357
358     if(NULL == pCaps){
359         return D3DERR_INVALIDCALL;
360     }
361     pWineCaps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINED3DCAPS));
362     if(pWineCaps == NULL){
363         return D3DERR_INVALIDCALL; /* well this is what MSDN says to return */
364     }
365
366     memset(pCaps, 0, sizeof(*pCaps));
367
368     wined3d_mutex_lock();
369     hrc = IWineD3DDevice_GetDeviceCaps(This->WineD3DDevice, pWineCaps);
370     wined3d_mutex_unlock();
371
372     WINECAPSTOD3D9CAPS(pCaps, pWineCaps)
373     HeapFree(GetProcessHeap(), 0, pWineCaps);
374
375     /* Some functionality is implemented in d3d9.dll, not wined3d.dll. Add the needed caps */
376     pCaps->DevCaps2 |= D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES;
377
378     filter_caps(pCaps);
379
380     TRACE("Returning %p %p\n", This, pCaps);
381     return hrc;
382 }
383
384 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDisplayMode(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DDISPLAYMODE* pMode) {
385     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
386     HRESULT hr;
387
388     TRACE("iface %p, swapchain %u, mode %p.\n", iface, iSwapChain, pMode);
389
390     wined3d_mutex_lock();
391     hr = IWineD3DDevice_GetDisplayMode(This->WineD3DDevice, iSwapChain, (WINED3DDISPLAYMODE *) pMode);
392     wined3d_mutex_unlock();
393
394     if (SUCCEEDED(hr)) pMode->Format = d3dformat_from_wined3dformat(pMode->Format);
395
396     return hr;
397 }
398
399 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCreationParameters(LPDIRECT3DDEVICE9EX iface, D3DDEVICE_CREATION_PARAMETERS *pParameters) {
400     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
401     HRESULT hr;
402
403     TRACE("iface %p, parameters %p.\n", iface, pParameters);
404
405     wined3d_mutex_lock();
406     hr = IWineD3DDevice_GetCreationParameters(This->WineD3DDevice, (WINED3DDEVICE_CREATION_PARAMETERS *) pParameters);
407     wined3d_mutex_unlock();
408
409     return hr;
410 }
411
412 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCursorProperties(LPDIRECT3DDEVICE9EX iface, UINT XHotSpot, UINT YHotSpot, IDirect3DSurface9* pCursorBitmap) {
413     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
414     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pCursorBitmap;
415     HRESULT hr;
416
417     TRACE("iface %p, hotspot_x %u, hotspot_y %u, bitmap %p.\n",
418             iface, XHotSpot, YHotSpot, pCursorBitmap);
419
420     if(!pCursorBitmap) {
421         WARN("No cursor bitmap, returning WINED3DERR_INVALIDCALL\n");
422         return WINED3DERR_INVALIDCALL;
423     }
424
425     wined3d_mutex_lock();
426     hr = IWineD3DDevice_SetCursorProperties(This->WineD3DDevice, XHotSpot, YHotSpot, pSurface->wineD3DSurface);
427     wined3d_mutex_unlock();
428
429     return hr;
430 }
431
432 static void     WINAPI  IDirect3DDevice9Impl_SetCursorPosition(LPDIRECT3DDEVICE9EX iface, int XScreenSpace, int YScreenSpace, DWORD Flags) {
433     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
434
435     TRACE("iface %p, x %u, y %u, flags %#x.\n", iface, XScreenSpace, YScreenSpace, Flags);
436
437     wined3d_mutex_lock();
438     IWineD3DDevice_SetCursorPosition(This->WineD3DDevice, XScreenSpace, YScreenSpace, Flags);
439     wined3d_mutex_unlock();
440 }
441
442 static BOOL     WINAPI  IDirect3DDevice9Impl_ShowCursor(LPDIRECT3DDEVICE9EX iface, BOOL bShow) {
443     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
444     BOOL ret;
445
446     TRACE("iface %p, show %#x.\n", iface, bShow);
447
448     wined3d_mutex_lock();
449     ret = IWineD3DDevice_ShowCursor(This->WineD3DDevice, bShow);
450     wined3d_mutex_unlock();
451
452     return ret;
453 }
454
455 static HRESULT WINAPI reset_enum_callback(IWineD3DResource *resource, void *data) {
456     BOOL *resources_ok = data;
457     D3DRESOURCETYPE type;
458     HRESULT ret = S_OK;
459     WINED3DSURFACE_DESC surface_desc;
460     WINED3DVOLUME_DESC volume_desc;
461     D3DINDEXBUFFER_DESC index_desc;
462     D3DVERTEXBUFFER_DESC vertex_desc;
463     WINED3DPOOL pool;
464     IDirect3DResource9 *parent;
465
466     IWineD3DResource_GetParent(resource, (IUnknown **) &parent);
467     type = IDirect3DResource9_GetType(parent);
468     switch(type) {
469         case D3DRTYPE_SURFACE:
470             IWineD3DSurface_GetDesc((IWineD3DSurface *) resource, &surface_desc);
471             pool = surface_desc.pool;
472             break;
473
474         case D3DRTYPE_VOLUME:
475             IWineD3DVolume_GetDesc((IWineD3DVolume *) resource, &volume_desc);
476             pool = volume_desc.Pool;
477             break;
478
479         case D3DRTYPE_INDEXBUFFER:
480             IDirect3DIndexBuffer9_GetDesc((IDirect3DIndexBuffer9 *) parent, &index_desc);
481             pool = index_desc.Pool;
482             break;
483
484         case D3DRTYPE_VERTEXBUFFER:
485             IDirect3DVertexBuffer9_GetDesc((IDirect3DVertexBuffer9 *)parent, &vertex_desc);
486             pool = vertex_desc.Pool;
487             break;
488
489         /* No need to check for textures. If there is a D3DPOOL_DEFAULT texture, there
490          * is a D3DPOOL_DEFAULT surface or volume as well
491          */
492         default:
493             pool = WINED3DPOOL_SCRATCH; /* a harmless pool */
494             break;
495     }
496
497     if(pool == WINED3DPOOL_DEFAULT) {
498         if(IUnknown_Release(parent) == 0) {
499             TRACE("Parent %p is an implicit resource with ref 0\n", parent);
500         } else {
501             WARN("Resource %p(wineD3D %p) with pool D3DPOOL_DEFAULT blocks the Reset call\n", parent, resource);
502             ret = S_FALSE;
503             *resources_ok = FALSE;
504         }
505     } else {
506         IUnknown_Release(parent);
507     }
508     IWineD3DResource_Release(resource);
509
510     return ret;
511 }
512
513 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Reset(LPDIRECT3DDEVICE9EX iface, D3DPRESENT_PARAMETERS* pPresentationParameters) {
514     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
515     WINED3DPRESENT_PARAMETERS localParameters;
516     HRESULT hr;
517     BOOL resources_ok = TRUE;
518     UINT i;
519
520     TRACE("iface %p, present_parameters %p.\n", iface, pPresentationParameters);
521
522     /* Reset states that hold a COM object. WineD3D holds an internal reference to set objects, because
523      * such objects can still be used for rendering after their external d3d9 object has been destroyed.
524      * These objects must not be enumerated. Unsetting them tells WineD3D that the application will not
525      * make use of the hidden reference and destroys the objects.
526      *
527      * Unsetting them is no problem, because the states are supposed to be reset anyway. If the validation
528      * below fails, the device is considered "lost", and _Reset and _Release are the only allowed calls
529      */
530     wined3d_mutex_lock();
531     IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice, NULL, WINED3DFMT_UNKNOWN);
532     for(i = 0; i < 16; i++) {
533         IWineD3DDevice_SetStreamSource(This->WineD3DDevice, i, NULL, 0, 0);
534     }
535     for(i = 0; i < 16; i++) {
536         IWineD3DDevice_SetTexture(This->WineD3DDevice, i, NULL);
537     }
538
539     IWineD3DDevice_EnumResources(This->WineD3DDevice, reset_enum_callback, &resources_ok);
540     if(!resources_ok) {
541         WARN("The application is holding D3DPOOL_DEFAULT resources, rejecting reset\n");
542         This->notreset = TRUE;
543         wined3d_mutex_unlock();
544
545         return WINED3DERR_INVALIDCALL;
546     }
547
548     localParameters.BackBufferWidth                     = pPresentationParameters->BackBufferWidth;
549     localParameters.BackBufferHeight                    = pPresentationParameters->BackBufferHeight;
550     localParameters.BackBufferFormat                    = wined3dformat_from_d3dformat(pPresentationParameters->BackBufferFormat);
551     localParameters.BackBufferCount                     = pPresentationParameters->BackBufferCount;
552     localParameters.MultiSampleType                     = pPresentationParameters->MultiSampleType;
553     localParameters.MultiSampleQuality                  = pPresentationParameters->MultiSampleQuality;
554     localParameters.SwapEffect                          = pPresentationParameters->SwapEffect;
555     localParameters.hDeviceWindow                       = pPresentationParameters->hDeviceWindow;
556     localParameters.Windowed                            = pPresentationParameters->Windowed;
557     localParameters.EnableAutoDepthStencil              = pPresentationParameters->EnableAutoDepthStencil;
558     localParameters.AutoDepthStencilFormat              = wined3dformat_from_d3dformat(pPresentationParameters->AutoDepthStencilFormat);
559     localParameters.Flags                               = pPresentationParameters->Flags;
560     localParameters.FullScreen_RefreshRateInHz          = pPresentationParameters->FullScreen_RefreshRateInHz;
561     localParameters.PresentationInterval                = pPresentationParameters->PresentationInterval;
562     localParameters.AutoRestoreDisplayMode              = TRUE;
563
564     hr = IWineD3DDevice_Reset(This->WineD3DDevice, &localParameters);
565     if(FAILED(hr)) {
566         This->notreset = TRUE;
567
568         pPresentationParameters->BackBufferWidth            = localParameters.BackBufferWidth;
569         pPresentationParameters->BackBufferHeight           = localParameters.BackBufferHeight;
570         pPresentationParameters->BackBufferFormat           = d3dformat_from_wined3dformat(localParameters.BackBufferFormat);
571         pPresentationParameters->BackBufferCount            = localParameters.BackBufferCount;
572         pPresentationParameters->MultiSampleType            = localParameters.MultiSampleType;
573         pPresentationParameters->MultiSampleQuality         = localParameters.MultiSampleQuality;
574         pPresentationParameters->SwapEffect                 = localParameters.SwapEffect;
575         pPresentationParameters->hDeviceWindow              = localParameters.hDeviceWindow;
576         pPresentationParameters->Windowed                   = localParameters.Windowed;
577         pPresentationParameters->EnableAutoDepthStencil     = localParameters.EnableAutoDepthStencil;
578         pPresentationParameters->AutoDepthStencilFormat     = d3dformat_from_wined3dformat(localParameters.AutoDepthStencilFormat);
579         pPresentationParameters->Flags                      = localParameters.Flags;
580         pPresentationParameters->FullScreen_RefreshRateInHz = localParameters.FullScreen_RefreshRateInHz;
581         pPresentationParameters->PresentationInterval       = localParameters.PresentationInterval;
582     } else {
583         This->notreset = FALSE;
584     }
585
586     wined3d_mutex_unlock();
587
588     return hr;
589 }
590
591 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_Present(LPDIRECT3DDEVICE9EX iface, CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA*
592  pDirtyRegion) {
593     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
594     HRESULT hr;
595
596     TRACE("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p.\n",
597             iface, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
598
599     wined3d_mutex_lock();
600     hr = IWineD3DDevice_Present(This->WineD3DDevice, pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
601     wined3d_mutex_unlock();
602
603     return hr;
604  }
605
606 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetBackBuffer(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, UINT BackBuffer, D3DBACKBUFFER_TYPE Type, IDirect3DSurface9 ** ppBackBuffer) {
607     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
608     IWineD3DSurface *retSurface = NULL;
609     HRESULT rc = D3D_OK;
610
611     TRACE("iface %p, swapchain %u, backbuffer_idx %u, backbuffer_type %#x, backbuffer %p.\n",
612             iface, iSwapChain, BackBuffer, Type, ppBackBuffer);
613
614     wined3d_mutex_lock();
615     rc = IWineD3DDevice_GetBackBuffer(This->WineD3DDevice, iSwapChain, BackBuffer, (WINED3DBACKBUFFER_TYPE) Type, &retSurface);
616     if (rc == D3D_OK && NULL != retSurface && NULL != ppBackBuffer) {
617         IWineD3DSurface_GetParent(retSurface, (IUnknown **)ppBackBuffer);
618         IWineD3DSurface_Release(retSurface);
619     }
620     wined3d_mutex_unlock();
621
622     return rc;
623 }
624 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRasterStatus(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DRASTER_STATUS* pRasterStatus) {
625     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
626     HRESULT hr;
627
628     TRACE("iface %p, swapchain %u, raster_status %p.\n", iface, iSwapChain, pRasterStatus);
629
630     wined3d_mutex_lock();
631     hr = IWineD3DDevice_GetRasterStatus(This->WineD3DDevice, iSwapChain, (WINED3DRASTER_STATUS *) pRasterStatus);
632     wined3d_mutex_unlock();
633
634     return hr;
635 }
636
637 static HRESULT WINAPI IDirect3DDevice9Impl_SetDialogBoxMode(LPDIRECT3DDEVICE9EX iface, BOOL bEnableDialogs) {
638     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
639     HRESULT hr;
640
641     TRACE("iface %p, enable %#x.\n", iface, bEnableDialogs);
642
643     wined3d_mutex_lock();
644     hr = IWineD3DDevice_SetDialogBoxMode(This->WineD3DDevice, bEnableDialogs);
645     wined3d_mutex_unlock();
646
647     return hr;
648 }
649
650 static void WINAPI IDirect3DDevice9Impl_SetGammaRamp(IDirect3DDevice9Ex *iface, UINT iSwapChain,
651         DWORD Flags, const D3DGAMMARAMP *pRamp)
652 {
653     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
654
655     TRACE("iface %p, swapchain %u, flags %#x, ramp %p.\n", iface, iSwapChain, Flags, pRamp);
656
657     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
658     wined3d_mutex_lock();
659     IWineD3DDevice_SetGammaRamp(This->WineD3DDevice, iSwapChain, Flags, (CONST WINED3DGAMMARAMP *)pRamp);
660     wined3d_mutex_unlock();
661 }
662
663 static void WINAPI IDirect3DDevice9Impl_GetGammaRamp(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, D3DGAMMARAMP* pRamp) {
664     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
665
666     TRACE("iface %p, swapchain %u, ramp %p.\n", iface, iSwapChain, pRamp);
667
668     /* Note: D3DGAMMARAMP is compatible with WINED3DGAMMARAMP */
669     wined3d_mutex_lock();
670     IWineD3DDevice_GetGammaRamp(This->WineD3DDevice, iSwapChain, (WINED3DGAMMARAMP *) pRamp);
671     wined3d_mutex_unlock();
672 }
673
674 static HRESULT WINAPI IDirect3DDevice9Impl_CreateTexture(IDirect3DDevice9Ex *iface,
675         UINT width, UINT height, UINT levels, DWORD usage, D3DFORMAT format,
676         D3DPOOL pool, IDirect3DTexture9 **texture, HANDLE *shared_handle)
677 {
678     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
679     IDirect3DTexture9Impl *object;
680     HRESULT hr;
681
682     TRACE("iface %p, width %u, height %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
683             iface, width, height, levels, usage, format, pool, texture, shared_handle);
684
685     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
686     if (!object)
687     {
688         ERR("Failed to allocate texture memory.\n");
689         return D3DERR_OUTOFVIDEOMEMORY;
690     }
691
692     hr = texture_init(object, This, width, height, levels, usage, format, pool);
693     if (FAILED(hr))
694     {
695         WARN("Failed to initialize texture, hr %#x.\n", hr);
696         HeapFree(GetProcessHeap(), 0, object);
697         return hr;
698     }
699
700     TRACE("Created texture %p.\n", object);
701     *texture = (IDirect3DTexture9 *)object;
702
703     return D3D_OK;
704 }
705
706 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVolumeTexture(IDirect3DDevice9Ex *iface,
707         UINT width, UINT height, UINT depth, UINT levels, DWORD usage, D3DFORMAT format,
708         D3DPOOL pool, IDirect3DVolumeTexture9 **texture, HANDLE *shared_handle)
709 {
710     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
711     IDirect3DVolumeTexture9Impl *object;
712     HRESULT hr;
713
714     TRACE("iface %p, width %u, height %u, depth %u, levels %u\n",
715             iface, width, height, depth, levels);
716     TRACE("usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
717             usage, format, pool, texture, shared_handle);
718
719     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
720     if (!object)
721     {
722         ERR("Failed to allocate volume texture memory.\n");
723         return D3DERR_OUTOFVIDEOMEMORY;
724     }
725
726     hr = volumetexture_init(object, This, width, height, depth, levels, usage, format, pool);
727     if (FAILED(hr))
728     {
729         WARN("Failed to initialize volume texture, hr %#x.\n", hr);
730         HeapFree(GetProcessHeap(), 0, object);
731         return hr;
732     }
733
734     TRACE("Created volume texture %p.\n", object);
735     *texture = (IDirect3DVolumeTexture9 *)object;
736
737     return D3D_OK;
738 }
739
740 static HRESULT WINAPI IDirect3DDevice9Impl_CreateCubeTexture(IDirect3DDevice9Ex *iface,
741         UINT edge_length, UINT levels, DWORD usage, D3DFORMAT format, D3DPOOL pool,
742         IDirect3DCubeTexture9 **texture, HANDLE *shared_handle)
743 {
744     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
745     IDirect3DCubeTexture9Impl *object;
746     HRESULT hr;
747
748     TRACE("iface %p, edge_length %u, levels %u, usage %#x, format %#x, pool %#x, texture %p, shared_handle %p.\n",
749             iface, edge_length, levels, usage, format, pool, texture, shared_handle);
750
751     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
752     if (!object)
753     {
754         ERR("Failed to allocate cube texture memory.\n");
755         return D3DERR_OUTOFVIDEOMEMORY;
756     }
757
758     hr = cubetexture_init(object, This, edge_length, levels, usage, format, pool);
759     if (FAILED(hr))
760     {
761         WARN("Failed to initialize cube texture, hr %#x.\n", hr);
762         HeapFree(GetProcessHeap(), 0, object);
763         return hr;
764     }
765
766     TRACE("Created cube texture %p.\n", object);
767     *texture = (IDirect3DCubeTexture9 *)object;
768
769     return D3D_OK;
770 }
771
772 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexBuffer(IDirect3DDevice9Ex *iface, UINT size, DWORD usage,
773         DWORD fvf, D3DPOOL pool, IDirect3DVertexBuffer9 **buffer, HANDLE *shared_handle)
774 {
775     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
776     IDirect3DVertexBuffer9Impl *object;
777     HRESULT hr;
778
779     TRACE("iface %p, size %u, usage %#x, fvf %#x, pool %#x, buffer %p, shared_handle %p.\n",
780             iface, size, usage, fvf, pool, buffer, shared_handle);
781
782     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
783     if (!object)
784     {
785         ERR("Failed to allocate buffer memory.\n");
786         return D3DERR_OUTOFVIDEOMEMORY;
787     }
788
789     hr = vertexbuffer_init(object, This, size, usage, fvf, pool);
790     if (FAILED(hr))
791     {
792         WARN("Failed to initialize vertex buffer, hr %#x.\n", hr);
793         HeapFree(GetProcessHeap(), 0, object);
794         return hr;
795     }
796
797     TRACE("Created vertex buffer %p.\n", object);
798     *buffer = (IDirect3DVertexBuffer9 *)object;
799
800     return D3D_OK;
801 }
802
803 static HRESULT WINAPI IDirect3DDevice9Impl_CreateIndexBuffer(IDirect3DDevice9Ex *iface, UINT size, DWORD usage,
804         D3DFORMAT format, D3DPOOL pool, IDirect3DIndexBuffer9 **buffer, HANDLE *shared_handle)
805 {
806     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
807     IDirect3DIndexBuffer9Impl *object;
808     HRESULT hr;
809
810     TRACE("iface %p, size %u, usage %#x, format %#x, pool %#x, buffer %p, shared_handle %p.\n",
811             iface, size, usage, format, pool, buffer, shared_handle);
812
813     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
814     if (!object)
815     {
816         ERR("Failed to allocate buffer memory.\n");
817         return D3DERR_OUTOFVIDEOMEMORY;
818     }
819
820     hr = indexbuffer_init(object, This, size, usage, format, pool);
821     if (FAILED(hr))
822     {
823         WARN("Failed to initialize index buffer, hr %#x.\n", hr);
824         HeapFree(GetProcessHeap(), 0, object);
825         return hr;
826     }
827
828     TRACE("Created index buffer %p.\n", object);
829     *buffer = (IDirect3DIndexBuffer9 *)object;
830
831     return D3D_OK;
832 }
833
834 static HRESULT IDirect3DDevice9Impl_CreateSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height,
835         D3DFORMAT Format, BOOL Lockable, BOOL Discard, UINT Level, IDirect3DSurface9 **ppSurface,
836         UINT Usage, D3DPOOL Pool, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality)
837 {
838     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
839     IDirect3DSurface9Impl *object;
840     HRESULT hr;
841
842     TRACE("iface %p, width %u, height %u, format %#x, lockable %#x, discard %#x, level %u, surface %p.\n"
843             "usage %#x, pool %#x, multisample_type %#x, multisample_quality %u.\n",
844             iface, Width, Height, Format, Lockable, Discard, Level, ppSurface,
845             Usage, Pool, MultiSample, MultisampleQuality);
846
847     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DSurface9Impl));
848     if (!object)
849     {
850         FIXME("Failed to allocate surface memory.\n");
851         return D3DERR_OUTOFVIDEOMEMORY;
852     }
853
854     hr = surface_init(object, This, Width, Height, Format, Lockable, Discard,
855             Level, Usage, Pool, MultiSample, MultisampleQuality);
856     if (FAILED(hr))
857     {
858         WARN("Failed to initialize surface, hr %#x.\n", hr);
859         HeapFree(GetProcessHeap(), 0, object);
860         return hr;
861     }
862
863     TRACE("Created surface %p.\n", object);
864     *ppSurface = (IDirect3DSurface9 *)object;
865
866     return D3D_OK;
867 }
868
869 static HRESULT WINAPI IDirect3DDevice9Impl_CreateRenderTarget(IDirect3DDevice9Ex *iface, UINT Width, UINT Height,
870         D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample, DWORD MultisampleQuality, BOOL Lockable,
871         IDirect3DSurface9 **ppSurface, HANDLE *pSharedHandle)
872 {
873     HRESULT hr;
874
875     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
876             "lockable %#x, surface %p, shared_handle %p.\n",
877             iface, Width, Height, Format, MultiSample, MultisampleQuality,
878             Lockable, ppSurface, pSharedHandle);
879
880     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, Lockable, FALSE /* Discard */,
881             0 /* Level */, ppSurface, D3DUSAGE_RENDERTARGET, D3DPOOL_DEFAULT, MultiSample, MultisampleQuality);
882
883     return hr;
884 }
885
886 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height,
887                                                                 D3DFORMAT Format, D3DMULTISAMPLE_TYPE MultiSample,
888                                                                 DWORD MultisampleQuality, BOOL Discard,
889                                                                 IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
890     HRESULT hr;
891
892     TRACE("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u.\n"
893             "discard %#x, surface %p, shared_handle %p.\n",
894             iface, Width, Height, Format, MultiSample, MultisampleQuality,
895             Discard, ppSurface, pSharedHandle);
896
897     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, Discard,
898             0 /* Level */, ppSurface, D3DUSAGE_DEPTHSTENCIL, D3DPOOL_DEFAULT, MultiSample, MultisampleQuality);
899
900     return hr;
901 }
902
903
904 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pSourceSurface, CONST RECT* pSourceRect, IDirect3DSurface9* pDestinationSurface, CONST POINT* pDestPoint) {
905     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
906     HRESULT hr;
907
908     TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_point %p.\n",
909             iface, pSourceSurface, pSourceRect, pDestinationSurface, pDestPoint);
910
911     wined3d_mutex_lock();
912     hr = IWineD3DDevice_UpdateSurface(This->WineD3DDevice, ((IDirect3DSurface9Impl *)pSourceSurface)->wineD3DSurface, pSourceRect, ((IDirect3DSurface9Impl *)pDestinationSurface)->wineD3DSurface, pDestPoint);
913     wined3d_mutex_unlock();
914
915     return hr;
916 }
917
918 static HRESULT  WINAPI  IDirect3DDevice9Impl_UpdateTexture(LPDIRECT3DDEVICE9EX iface, IDirect3DBaseTexture9* pSourceTexture, IDirect3DBaseTexture9* pDestinationTexture) {
919     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
920     HRESULT hr;
921
922     TRACE("iface %p, src_texture %p, dst_texture %p.\n", iface, pSourceTexture, pDestinationTexture);
923
924     wined3d_mutex_lock();
925     hr = IWineD3DDevice_UpdateTexture(This->WineD3DDevice,  ((IDirect3DBaseTexture9Impl *)pSourceTexture)->wineD3DBaseTexture, ((IDirect3DBaseTexture9Impl *)pDestinationTexture)->wineD3DBaseTexture);
926     wined3d_mutex_unlock();
927
928     return hr;
929 }
930
931 static HRESULT WINAPI IDirect3DDevice9Impl_GetRenderTargetData(IDirect3DDevice9Ex *iface,
932         IDirect3DSurface9 *pRenderTarget, IDirect3DSurface9 *pDestSurface)
933 {
934     IDirect3DSurface9Impl *renderTarget = (IDirect3DSurface9Impl *)pRenderTarget;
935     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
936     HRESULT hr;
937
938     TRACE("iface %p, render_target %p, dst_surface %p.\n", iface, pRenderTarget, pDestSurface);
939
940     wined3d_mutex_lock();
941     hr = IWineD3DSurface_BltFast(destSurface->wineD3DSurface, 0, 0, renderTarget->wineD3DSurface, NULL, WINEDDBLTFAST_NOCOLORKEY);
942     wined3d_mutex_unlock();
943
944     return hr;
945 }
946
947 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetFrontBufferData(LPDIRECT3DDEVICE9EX iface, UINT iSwapChain, IDirect3DSurface9* pDestSurface) {
948     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
949     IDirect3DSurface9Impl *destSurface = (IDirect3DSurface9Impl *)pDestSurface;
950     HRESULT hr;
951
952     TRACE("iface %p, swapchain %u, dst_surface %p.\n", iface, iSwapChain, pDestSurface);
953
954     wined3d_mutex_lock();
955     hr = IWineD3DDevice_GetFrontBufferData(This->WineD3DDevice, iSwapChain, destSurface->wineD3DSurface);
956     wined3d_mutex_unlock();
957
958     return hr;
959 }
960
961 static HRESULT WINAPI IDirect3DDevice9Impl_StretchRect(IDirect3DDevice9Ex *iface, IDirect3DSurface9 *pSourceSurface,
962         const RECT *pSourceRect, IDirect3DSurface9 *pDestSurface, const RECT *pDestRect, D3DTEXTUREFILTERTYPE Filter)
963 {
964     IDirect3DSurface9Impl *src = (IDirect3DSurface9Impl *) pSourceSurface;
965     IDirect3DSurface9Impl *dst = (IDirect3DSurface9Impl *) pDestSurface;
966     HRESULT hr;
967
968     TRACE("iface %p, src_surface %p, src_rect %p, dst_surface %p, dst_rect %p, filter %#x.\n",
969             iface, pSourceSurface, pSourceRect, pDestSurface, pDestRect, Filter);
970
971     wined3d_mutex_lock();
972     hr = IWineD3DSurface_Blt(dst->wineD3DSurface, pDestRect, src->wineD3DSurface, pSourceRect, 0, NULL, Filter);
973     wined3d_mutex_unlock();
974
975     return hr;
976 }
977
978 static HRESULT  WINAPI  IDirect3DDevice9Impl_ColorFill(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pSurface, CONST RECT* pRect, D3DCOLOR color) {
979     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
980     IDirect3DSurface9Impl *surface = (IDirect3DSurface9Impl *)pSurface;
981     WINED3DPOOL pool;
982     WINED3DRESOURCETYPE restype;
983     DWORD usage;
984     WINED3DSURFACE_DESC desc;
985     HRESULT hr;
986
987     TRACE("iface %p, surface %p, rect %p, color 0x%08x.\n", iface, pSurface, pRect, color);
988
989     wined3d_mutex_lock();
990
991     IWineD3DSurface_GetDesc(surface->wineD3DSurface, &desc);
992     usage = desc.usage;
993     pool = desc.pool;
994     restype = desc.resource_type;
995
996     /* This method is only allowed with surfaces that are render targets, or offscreen plain surfaces
997      * in D3DPOOL_DEFAULT
998      */
999     if(!(usage & WINED3DUSAGE_RENDERTARGET) && (pool != WINED3DPOOL_DEFAULT || restype != WINED3DRTYPE_SURFACE)) {
1000         wined3d_mutex_unlock();
1001         WARN("Surface is not a render target, or not a stand-alone D3DPOOL_DEFAULT surface\n");
1002         return D3DERR_INVALIDCALL;
1003     }
1004
1005     /* Colorfill can only be used on rendertarget surfaces, or offscreen plain surfaces in D3DPOOL_DEFAULT */
1006     /* Note: D3DRECT is compatible with WINED3DRECT */
1007     hr = IWineD3DDevice_ColorFill(This->WineD3DDevice, surface->wineD3DSurface, (CONST WINED3DRECT*)pRect, color);
1008
1009     wined3d_mutex_unlock();
1010
1011     return hr;
1012 }
1013
1014 static HRESULT  WINAPI  IDirect3DDevice9Impl_CreateOffscreenPlainSurface(LPDIRECT3DDEVICE9EX iface, UINT Width, UINT Height, D3DFORMAT Format, D3DPOOL Pool, IDirect3DSurface9 **ppSurface, HANDLE* pSharedHandle) {
1015     HRESULT hr;
1016
1017     TRACE("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p.\n",
1018             iface, Width, Height, Format, Pool, ppSurface, pSharedHandle);
1019
1020     if(Pool == D3DPOOL_MANAGED ){
1021         FIXME("Attempting to create a managed offscreen plain surface\n");
1022         return D3DERR_INVALIDCALL;
1023     }
1024         /*
1025         'Off-screen plain surfaces are always lockable, regardless of their pool types.'
1026         but then...
1027         D3DPOOL_DEFAULT is the appropriate pool for use with the IDirect3DDevice9::StretchRect and IDirect3DDevice9::ColorFill.
1028         Why, their always lockable?
1029         should I change the usage to dynamic?
1030         */
1031     hr = IDirect3DDevice9Impl_CreateSurface(iface, Width, Height, Format, TRUE /* Lockable */, FALSE /* Discard */,
1032             0 /* Level */, ppSurface, 0 /* Usage (undefined/none) */, (WINED3DPOOL)Pool, D3DMULTISAMPLE_NONE,
1033             0 /* MultisampleQuality */);
1034
1035     return hr;
1036 }
1037
1038 /* TODO: move to wineD3D */
1039 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9* pRenderTarget) {
1040     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1041     IDirect3DSurface9Impl *pSurface = (IDirect3DSurface9Impl*)pRenderTarget;
1042     HRESULT hr;
1043
1044     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, pRenderTarget);
1045
1046     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1047     {
1048         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1049         return D3DERR_INVALIDCALL;
1050     }
1051
1052     wined3d_mutex_lock();
1053     hr = IWineD3DDevice_SetRenderTarget(This->WineD3DDevice, RenderTargetIndex, pSurface ? pSurface->wineD3DSurface : NULL, TRUE);
1054     wined3d_mutex_unlock();
1055
1056     return hr;
1057 }
1058
1059 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderTarget(LPDIRECT3DDEVICE9EX iface, DWORD RenderTargetIndex, IDirect3DSurface9 **ppRenderTarget) {
1060     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1061     IWineD3DSurface *pRenderTarget;
1062     HRESULT hr;
1063
1064     TRACE("iface %p, idx %u, surface %p.\n", iface, RenderTargetIndex, ppRenderTarget);
1065
1066     if (ppRenderTarget == NULL) {
1067         return D3DERR_INVALIDCALL;
1068     }
1069
1070     if (RenderTargetIndex >= D3D9_MAX_SIMULTANEOUS_RENDERTARGETS)
1071     {
1072         WARN("Invalid index %u specified.\n", RenderTargetIndex);
1073         return D3DERR_INVALIDCALL;
1074     }
1075
1076     wined3d_mutex_lock();
1077
1078     hr=IWineD3DDevice_GetRenderTarget(This->WineD3DDevice,RenderTargetIndex,&pRenderTarget);
1079
1080     if (FAILED(hr))
1081     {
1082         FIXME("Call to IWineD3DDevice_GetRenderTarget failed, hr %#x\n", hr);
1083     }
1084     else if (!pRenderTarget)
1085     {
1086         *ppRenderTarget = NULL;
1087     }
1088     else
1089     {
1090         IWineD3DSurface_GetParent(pRenderTarget, (IUnknown **)ppRenderTarget);
1091         IWineD3DSurface_Release(pRenderTarget);
1092     }
1093
1094     wined3d_mutex_unlock();
1095
1096     return hr;
1097 }
1098
1099 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9* pZStencilSurface) {
1100     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1101     IDirect3DSurface9Impl *pSurface;
1102     HRESULT hr;
1103
1104     TRACE("iface %p, depth_stencil %p.\n", iface, pZStencilSurface);
1105
1106     pSurface = (IDirect3DSurface9Impl*)pZStencilSurface;
1107
1108     wined3d_mutex_lock();
1109     hr = IWineD3DDevice_SetDepthStencilSurface(This->WineD3DDevice, NULL==pSurface ? NULL : pSurface->wineD3DSurface);
1110     wined3d_mutex_unlock();
1111
1112     return hr;
1113 }
1114
1115 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetDepthStencilSurface(LPDIRECT3DDEVICE9EX iface, IDirect3DSurface9 **ppZStencilSurface) {
1116     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1117     HRESULT hr = D3D_OK;
1118     IWineD3DSurface *pZStencilSurface;
1119
1120     TRACE("iface %p, depth_stencil %p.\n", iface, ppZStencilSurface);
1121
1122     if(ppZStencilSurface == NULL){
1123         return D3DERR_INVALIDCALL;
1124     }
1125
1126     wined3d_mutex_lock();
1127     hr = IWineD3DDevice_GetDepthStencilSurface(This->WineD3DDevice,&pZStencilSurface);
1128     if (hr == WINED3D_OK) {
1129         IWineD3DSurface_GetParent(pZStencilSurface,(IUnknown**)ppZStencilSurface);
1130         IWineD3DSurface_Release(pZStencilSurface);
1131     } else {
1132         if (hr != WINED3DERR_NOTFOUND)
1133                 WARN("Call to IWineD3DDevice_GetDepthStencilSurface failed with 0x%08x\n", hr);
1134         *ppZStencilSurface = NULL;
1135     }
1136     wined3d_mutex_unlock();
1137
1138     return hr;
1139 }
1140
1141 static HRESULT  WINAPI  IDirect3DDevice9Impl_BeginScene(LPDIRECT3DDEVICE9EX iface) {
1142     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1143     HRESULT hr;
1144
1145     TRACE("iface %p.\n", iface);
1146
1147     wined3d_mutex_lock();
1148     hr = IWineD3DDevice_BeginScene(This->WineD3DDevice);
1149     wined3d_mutex_unlock();
1150
1151     return hr;
1152 }
1153
1154 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_EndScene(LPDIRECT3DDEVICE9EX iface) {
1155     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1156     HRESULT hr;
1157
1158     TRACE("iface %p.\n", iface);
1159
1160     wined3d_mutex_lock();
1161     hr = IWineD3DDevice_EndScene(This->WineD3DDevice);
1162     wined3d_mutex_unlock();
1163
1164     return hr;
1165 }
1166
1167 static HRESULT  WINAPI  IDirect3DDevice9Impl_Clear(LPDIRECT3DDEVICE9EX iface, DWORD Count, CONST D3DRECT* pRects, DWORD Flags, D3DCOLOR Color, float Z, DWORD Stencil) {
1168     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1169     HRESULT hr;
1170
1171     TRACE("iface %p, rect_count %u, rects %p, flags %#x, color 0x%08x, z %.8e, stencil %u.\n",
1172             iface, Count, pRects, Flags, Color, Z, Stencil);
1173
1174     /* Note: D3DRECT is compatible with WINED3DRECT */
1175     wined3d_mutex_lock();
1176     hr = IWineD3DDevice_Clear(This->WineD3DDevice, Count, (CONST WINED3DRECT*) pRects, Flags, Color, Z, Stencil);
1177     wined3d_mutex_unlock();
1178
1179     return hr;
1180 }
1181
1182 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* lpMatrix) {
1183     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1184     HRESULT hr;
1185
1186     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, lpMatrix);
1187
1188     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1189     wined3d_mutex_lock();
1190     hr = IWineD3DDevice_SetTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) lpMatrix);
1191     wined3d_mutex_unlock();
1192
1193     return hr;
1194 }
1195
1196 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, D3DMATRIX* pMatrix) {
1197     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1198     HRESULT hr;
1199
1200     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1201
1202     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1203     wined3d_mutex_lock();
1204     hr = IWineD3DDevice_GetTransform(This->WineD3DDevice, State, (WINED3DMATRIX*) pMatrix);
1205     wined3d_mutex_unlock();
1206
1207     return hr;
1208 }
1209
1210 static HRESULT  WINAPI  IDirect3DDevice9Impl_MultiplyTransform(LPDIRECT3DDEVICE9EX iface, D3DTRANSFORMSTATETYPE State, CONST D3DMATRIX* pMatrix) {
1211     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1212     HRESULT hr;
1213
1214     TRACE("iface %p, state %#x, matrix %p.\n", iface, State, pMatrix);
1215
1216     /* Note: D3DMATRIX is compatible with WINED3DMATRIX */
1217     wined3d_mutex_lock();
1218     hr = IWineD3DDevice_MultiplyTransform(This->WineD3DDevice, State, (CONST WINED3DMATRIX*) pMatrix);
1219     wined3d_mutex_unlock();
1220
1221     return hr;
1222 }
1223
1224 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetViewport(LPDIRECT3DDEVICE9EX iface, CONST D3DVIEWPORT9* pViewport) {
1225     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1226     HRESULT hr;
1227
1228     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1229
1230     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
1231     wined3d_mutex_lock();
1232     hr = IWineD3DDevice_SetViewport(This->WineD3DDevice, (const WINED3DVIEWPORT *)pViewport);
1233     wined3d_mutex_unlock();
1234
1235     return hr;
1236 }
1237
1238 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetViewport(LPDIRECT3DDEVICE9EX iface, D3DVIEWPORT9* pViewport) {
1239     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1240     HRESULT hr;
1241
1242     TRACE("iface %p, viewport %p.\n", iface, pViewport);
1243
1244     /* Note: D3DVIEWPORT9 is compatible with WINED3DVIEWPORT */
1245     wined3d_mutex_lock();
1246     hr = IWineD3DDevice_GetViewport(This->WineD3DDevice, (WINED3DVIEWPORT *)pViewport);
1247     wined3d_mutex_unlock();
1248
1249     return hr;
1250 }
1251
1252 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetMaterial(LPDIRECT3DDEVICE9EX iface, CONST D3DMATERIAL9* pMaterial) {
1253     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1254     HRESULT hr;
1255
1256     TRACE("iface %p, material %p.\n", iface, pMaterial);
1257
1258     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
1259     wined3d_mutex_lock();
1260     hr = IWineD3DDevice_SetMaterial(This->WineD3DDevice, (const WINED3DMATERIAL *)pMaterial);
1261     wined3d_mutex_unlock();
1262
1263     return hr;
1264 }
1265
1266 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetMaterial(LPDIRECT3DDEVICE9EX iface, D3DMATERIAL9* pMaterial) {
1267     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1268     HRESULT hr;
1269
1270     TRACE("iface %p, material %p.\n", iface, pMaterial);
1271
1272     /* Note: D3DMATERIAL9 is compatible with WINED3DMATERIAL */
1273     wined3d_mutex_lock();
1274     hr = IWineD3DDevice_GetMaterial(This->WineD3DDevice, (WINED3DMATERIAL *)pMaterial);
1275     wined3d_mutex_unlock();
1276
1277     return hr;
1278 }
1279
1280 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST D3DLIGHT9* pLight) {
1281     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1282     HRESULT hr;
1283
1284     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1285
1286     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
1287     wined3d_mutex_lock();
1288     hr = IWineD3DDevice_SetLight(This->WineD3DDevice, Index, (const WINED3DLIGHT *)pLight);
1289     wined3d_mutex_unlock();
1290
1291     return hr;
1292 }
1293
1294 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLight(LPDIRECT3DDEVICE9EX iface, DWORD Index, D3DLIGHT9* pLight) {
1295     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1296     HRESULT hr;
1297
1298     TRACE("iface %p, index %u, light %p.\n", iface, Index, pLight);
1299
1300     /* Note: D3DLIGHT9 is compatible with WINED3DLIGHT */
1301     wined3d_mutex_lock();
1302     hr = IWineD3DDevice_GetLight(This->WineD3DDevice, Index, (WINED3DLIGHT *)pLight);
1303     wined3d_mutex_unlock();
1304
1305     return hr;
1306 }
1307
1308 static HRESULT  WINAPI  IDirect3DDevice9Impl_LightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL Enable) {
1309     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1310     HRESULT hr;
1311
1312     TRACE("iface %p, index %u, enable %#x.\n", iface, Index, Enable);
1313
1314     wined3d_mutex_lock();
1315     hr = IWineD3DDevice_SetLightEnable(This->WineD3DDevice, Index, Enable);
1316     wined3d_mutex_unlock();
1317
1318     return hr;
1319 }
1320
1321 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetLightEnable(LPDIRECT3DDEVICE9EX iface, DWORD Index, BOOL* pEnable) {
1322     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1323     HRESULT hr;
1324
1325     TRACE("iface %p, index %u, enable %p.\n", iface, Index, pEnable);
1326
1327     wined3d_mutex_lock();
1328     hr = IWineD3DDevice_GetLightEnable(This->WineD3DDevice, Index, pEnable);
1329     wined3d_mutex_unlock();
1330
1331     return hr;
1332 }
1333
1334 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, CONST float* pPlane) {
1335     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1336     HRESULT hr;
1337
1338     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1339
1340     wined3d_mutex_lock();
1341     hr = IWineD3DDevice_SetClipPlane(This->WineD3DDevice, Index, pPlane);
1342     wined3d_mutex_unlock();
1343
1344     return hr;
1345 }
1346
1347 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipPlane(LPDIRECT3DDEVICE9EX iface, DWORD Index, float* pPlane) {
1348     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1349     HRESULT hr;
1350
1351     TRACE("iface %p, index %u, plane %p.\n", iface, Index, pPlane);
1352
1353     wined3d_mutex_lock();
1354     hr = IWineD3DDevice_GetClipPlane(This->WineD3DDevice, Index, pPlane);
1355     wined3d_mutex_unlock();
1356
1357     return hr;
1358 }
1359
1360 static HRESULT  WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD Value) {
1361     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1362     HRESULT hr;
1363
1364     TRACE("iface %p, state %#x, value %#x.\n", iface, State, Value);
1365
1366     wined3d_mutex_lock();
1367     hr = IWineD3DDevice_SetRenderState(This->WineD3DDevice, State, Value);
1368     wined3d_mutex_unlock();
1369
1370     return hr;
1371 }
1372
1373 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetRenderState(LPDIRECT3DDEVICE9EX iface, D3DRENDERSTATETYPE State, DWORD* pValue) {
1374     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1375     HRESULT hr;
1376
1377     TRACE("iface %p, state %#x, value %p.\n", iface, State, pValue);
1378
1379     wined3d_mutex_lock();
1380     hr = IWineD3DDevice_GetRenderState(This->WineD3DDevice, State, pValue);
1381     wined3d_mutex_unlock();
1382
1383     return hr;
1384 }
1385
1386 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetClipStatus(LPDIRECT3DDEVICE9EX iface, CONST D3DCLIPSTATUS9* pClipStatus) {
1387     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1388     HRESULT hr;
1389
1390     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1391
1392     wined3d_mutex_lock();
1393     hr = IWineD3DDevice_SetClipStatus(This->WineD3DDevice, (const WINED3DCLIPSTATUS *)pClipStatus);
1394     wined3d_mutex_unlock();
1395
1396     return hr;
1397 }
1398
1399 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetClipStatus(LPDIRECT3DDEVICE9EX iface, D3DCLIPSTATUS9* pClipStatus) {
1400     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1401     HRESULT hr;
1402
1403     TRACE("iface %p, clip_status %p.\n", iface, pClipStatus);
1404
1405     wined3d_mutex_lock();
1406     hr = IWineD3DDevice_GetClipStatus(This->WineD3DDevice, (WINED3DCLIPSTATUS *)pClipStatus);
1407     wined3d_mutex_unlock();
1408
1409     return hr;
1410 }
1411
1412 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9 **ppTexture) {
1413     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1414     IWineD3DBaseTexture *retTexture = NULL;
1415     HRESULT rc = D3D_OK;
1416
1417     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, ppTexture);
1418
1419     if(ppTexture == NULL){
1420         return D3DERR_INVALIDCALL;
1421     }
1422
1423     wined3d_mutex_lock();
1424     rc = IWineD3DDevice_GetTexture(This->WineD3DDevice, Stage, &retTexture);
1425     if (SUCCEEDED(rc) && NULL != retTexture) {
1426         IWineD3DBaseTexture_GetParent(retTexture, (IUnknown **)ppTexture);
1427         IWineD3DBaseTexture_Release(retTexture);
1428     } else {
1429         if(FAILED(rc)) {
1430             WARN("Call to get texture  (%d) failed (%p)\n", Stage, retTexture);
1431         }
1432         *ppTexture = NULL;
1433     }
1434     wined3d_mutex_unlock();
1435
1436     return rc;
1437 }
1438
1439 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTexture(LPDIRECT3DDEVICE9EX iface, DWORD Stage, IDirect3DBaseTexture9* pTexture) {
1440     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1441     HRESULT hr;
1442
1443     TRACE("iface %p, stage %u, texture %p.\n", iface, Stage, pTexture);
1444
1445     wined3d_mutex_lock();
1446     hr = IWineD3DDevice_SetTexture(This->WineD3DDevice, Stage,
1447                                    pTexture==NULL ? NULL:((IDirect3DBaseTexture9Impl *)pTexture)->wineD3DBaseTexture);
1448     wined3d_mutex_unlock();
1449
1450     return hr;
1451 }
1452
1453 static const WINED3DTEXTURESTAGESTATETYPE tss_lookup[] =
1454 {
1455     WINED3DTSS_FORCE_DWORD,             /*  0, unused */
1456     WINED3DTSS_COLOROP,                 /*  1, D3DTSS_COLOROP */
1457     WINED3DTSS_COLORARG1,               /*  2, D3DTSS_COLORARG1 */
1458     WINED3DTSS_COLORARG2,               /*  3, D3DTSS_COLORARG2 */
1459     WINED3DTSS_ALPHAOP,                 /*  4, D3DTSS_ALPHAOP */
1460     WINED3DTSS_ALPHAARG1,               /*  5, D3DTSS_ALPHAARG1 */
1461     WINED3DTSS_ALPHAARG2,               /*  6, D3DTSS_ALPHAARG2 */
1462     WINED3DTSS_BUMPENVMAT00,            /*  7, D3DTSS_BUMPENVMAT00 */
1463     WINED3DTSS_BUMPENVMAT01,            /*  8, D3DTSS_BUMPENVMAT01 */
1464     WINED3DTSS_BUMPENVMAT10,            /*  9, D3DTSS_BUMPENVMAT10 */
1465     WINED3DTSS_BUMPENVMAT11,            /* 10, D3DTSS_BUMPENVMAT11 */
1466     WINED3DTSS_TEXCOORDINDEX,           /* 11, D3DTSS_TEXCOORDINDEX */
1467     WINED3DTSS_FORCE_DWORD,             /* 12, unused */
1468     WINED3DTSS_FORCE_DWORD,             /* 13, unused */
1469     WINED3DTSS_FORCE_DWORD,             /* 14, unused */
1470     WINED3DTSS_FORCE_DWORD,             /* 15, unused */
1471     WINED3DTSS_FORCE_DWORD,             /* 16, unused */
1472     WINED3DTSS_FORCE_DWORD,             /* 17, unused */
1473     WINED3DTSS_FORCE_DWORD,             /* 18, unused */
1474     WINED3DTSS_FORCE_DWORD,             /* 19, unused */
1475     WINED3DTSS_FORCE_DWORD,             /* 20, unused */
1476     WINED3DTSS_FORCE_DWORD,             /* 21, unused */
1477     WINED3DTSS_BUMPENVLSCALE,           /* 22, D3DTSS_BUMPENVLSCALE */
1478     WINED3DTSS_BUMPENVLOFFSET,          /* 23, D3DTSS_BUMPENVLOFFSET */
1479     WINED3DTSS_TEXTURETRANSFORMFLAGS,   /* 24, D3DTSS_TEXTURETRANSFORMFLAGS */
1480     WINED3DTSS_FORCE_DWORD,             /* 25, unused */
1481     WINED3DTSS_COLORARG0,               /* 26, D3DTSS_COLORARG0 */
1482     WINED3DTSS_ALPHAARG0,               /* 27, D3DTSS_ALPHAARG0 */
1483     WINED3DTSS_RESULTARG,               /* 28, D3DTSS_RESULTARG */
1484     WINED3DTSS_FORCE_DWORD,             /* 29, unused */
1485     WINED3DTSS_FORCE_DWORD,             /* 30, unused */
1486     WINED3DTSS_FORCE_DWORD,             /* 31, unused */
1487     WINED3DTSS_CONSTANT,                /* 32, D3DTSS_CONSTANT */
1488 };
1489
1490 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD* pValue) {
1491     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1492     HRESULT hr;
1493
1494     TRACE("iface %p, stage %u, state %#x, value %p.\n", iface, Stage, Type, pValue);
1495
1496     wined3d_mutex_lock();
1497     hr = IWineD3DDevice_GetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], pValue);
1498     wined3d_mutex_unlock();
1499
1500     return hr;
1501 }
1502
1503 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetTextureStageState(LPDIRECT3DDEVICE9EX iface, DWORD Stage, D3DTEXTURESTAGESTATETYPE Type, DWORD Value) {
1504     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1505     HRESULT hr;
1506
1507     TRACE("iface %p, stage %u, state %#x, value %#x.\n", iface, Stage, Type, Value);
1508
1509     wined3d_mutex_lock();
1510     hr = IWineD3DDevice_SetTextureStageState(This->WineD3DDevice, Stage, tss_lookup[Type], Value);
1511     wined3d_mutex_unlock();
1512
1513     return hr;
1514 }
1515
1516 static HRESULT WINAPI IDirect3DDevice9Impl_GetSamplerState(IDirect3DDevice9Ex *iface, DWORD Sampler,
1517         D3DSAMPLERSTATETYPE Type, DWORD *pValue)
1518 {
1519     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1520     HRESULT hr;
1521
1522     TRACE("iface %p, sampler %u, state %#x, value %p.\n", iface, Sampler, Type, pValue);
1523
1524     wined3d_mutex_lock();
1525     hr = IWineD3DDevice_GetSamplerState(This->WineD3DDevice, Sampler, Type, pValue);
1526     wined3d_mutex_unlock();
1527
1528     return hr;
1529 }
1530
1531 static HRESULT WINAPI DECLSPEC_HOTPATCH IDirect3DDevice9Impl_SetSamplerState(LPDIRECT3DDEVICE9EX iface, DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value) {
1532     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1533     HRESULT hr;
1534
1535     TRACE("iface %p, sampler %u, state %#x, value %#x.\n", iface, Sampler, Type, Value);
1536
1537     wined3d_mutex_lock();
1538     hr = IWineD3DDevice_SetSamplerState(This->WineD3DDevice, Sampler, Type, Value);
1539     wined3d_mutex_unlock();
1540
1541     return hr;
1542 }
1543
1544 static HRESULT  WINAPI  IDirect3DDevice9Impl_ValidateDevice(LPDIRECT3DDEVICE9EX iface, DWORD* pNumPasses) {
1545     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1546     HRESULT hr;
1547
1548     TRACE("iface %p, pass_count %p.\n", iface, pNumPasses);
1549
1550     wined3d_mutex_lock();
1551     hr = IWineD3DDevice_ValidateDevice(This->WineD3DDevice, pNumPasses);
1552     wined3d_mutex_unlock();
1553
1554     return hr;
1555 }
1556
1557 static HRESULT WINAPI IDirect3DDevice9Impl_SetPaletteEntries(IDirect3DDevice9Ex *iface, UINT PaletteNumber,
1558         const PALETTEENTRY *pEntries)
1559 {
1560     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1561     HRESULT hr;
1562
1563     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1564
1565     wined3d_mutex_lock();
1566     hr = IWineD3DDevice_SetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1567     wined3d_mutex_unlock();
1568
1569     return hr;
1570 }
1571
1572 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetPaletteEntries(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber, PALETTEENTRY* pEntries) {
1573     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1574     HRESULT hr;
1575
1576     TRACE("iface %p, palette_idx %u, entries %p.\n", iface, PaletteNumber, pEntries);
1577
1578     wined3d_mutex_lock();
1579     hr = IWineD3DDevice_GetPaletteEntries(This->WineD3DDevice, PaletteNumber, pEntries);
1580     wined3d_mutex_unlock();
1581
1582     return hr;
1583 }
1584
1585 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT PaletteNumber) {
1586     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1587     HRESULT hr;
1588
1589     TRACE("iface %p, palette_idx %u.\n", iface, PaletteNumber);
1590
1591     wined3d_mutex_lock();
1592     hr = IWineD3DDevice_SetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1593     wined3d_mutex_unlock();
1594
1595     return hr;
1596 }
1597
1598 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetCurrentTexturePalette(LPDIRECT3DDEVICE9EX iface, UINT* PaletteNumber) {
1599     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1600     HRESULT hr;
1601
1602     TRACE("iface %p, palette_idx %p.\n", iface, PaletteNumber);
1603
1604     wined3d_mutex_lock();
1605     hr = IWineD3DDevice_GetCurrentTexturePalette(This->WineD3DDevice, PaletteNumber);
1606     wined3d_mutex_unlock();
1607
1608     return hr;
1609 }
1610
1611 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetScissorRect(LPDIRECT3DDEVICE9EX iface, CONST RECT* pRect) {
1612     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1613     HRESULT hr;
1614
1615     TRACE("iface %p, rect %p.\n", iface, pRect);
1616
1617     wined3d_mutex_lock();
1618     hr = IWineD3DDevice_SetScissorRect(This->WineD3DDevice, pRect);
1619     wined3d_mutex_unlock();
1620
1621     return hr;
1622 }
1623
1624 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetScissorRect(LPDIRECT3DDEVICE9EX iface, RECT* pRect) {
1625     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1626     HRESULT hr;
1627
1628     TRACE("iface %p, rect %p.\n", iface, pRect);
1629
1630     wined3d_mutex_lock();
1631     hr = IWineD3DDevice_GetScissorRect(This->WineD3DDevice, pRect);
1632     wined3d_mutex_unlock();
1633
1634     return hr;
1635 }
1636
1637 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface, BOOL bSoftware) {
1638     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1639     HRESULT hr;
1640
1641     TRACE("iface %p, software %#x.\n", iface, bSoftware);
1642
1643     wined3d_mutex_lock();
1644     hr = IWineD3DDevice_SetSoftwareVertexProcessing(This->WineD3DDevice, bSoftware);
1645     wined3d_mutex_unlock();
1646
1647     return hr;
1648 }
1649
1650 static BOOL     WINAPI  IDirect3DDevice9Impl_GetSoftwareVertexProcessing(LPDIRECT3DDEVICE9EX iface) {
1651     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1652     BOOL ret;
1653
1654     TRACE("iface %p.\n", iface);
1655
1656     wined3d_mutex_lock();
1657     ret = IWineD3DDevice_GetSoftwareVertexProcessing(This->WineD3DDevice);
1658     wined3d_mutex_unlock();
1659
1660     return ret;
1661 }
1662
1663 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetNPatchMode(LPDIRECT3DDEVICE9EX iface, float nSegments) {
1664     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1665     HRESULT hr;
1666
1667     TRACE("iface %p, segment_count %.8e.\n", iface, nSegments);
1668
1669     wined3d_mutex_lock();
1670     hr = IWineD3DDevice_SetNPatchMode(This->WineD3DDevice, nSegments);
1671     wined3d_mutex_unlock();
1672
1673     return hr;
1674 }
1675
1676 static float    WINAPI  IDirect3DDevice9Impl_GetNPatchMode(LPDIRECT3DDEVICE9EX iface) {
1677     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1678     float ret;
1679
1680     TRACE("iface %p.\n", iface);
1681
1682     wined3d_mutex_lock();
1683     ret = IWineD3DDevice_GetNPatchMode(This->WineD3DDevice);
1684     wined3d_mutex_unlock();
1685
1686     return ret;
1687 }
1688
1689 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitive(IDirect3DDevice9Ex *iface, D3DPRIMITIVETYPE PrimitiveType,
1690         UINT StartVertex, UINT PrimitiveCount)
1691 {
1692     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1693     HRESULT hr;
1694
1695     TRACE("iface %p, primitive_type %#x, start_vertex %u, primitive_count %u.\n",
1696             iface, PrimitiveType, StartVertex, PrimitiveCount);
1697
1698     wined3d_mutex_lock();
1699     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1700     hr = IWineD3DDevice_DrawPrimitive(This->WineD3DDevice, StartVertex,
1701             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount));
1702     wined3d_mutex_unlock();
1703
1704     return hr;
1705 }
1706
1707 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitive(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType,
1708                                                            INT BaseVertexIndex, UINT MinVertexIndex, UINT NumVertices, UINT startIndex, UINT primCount) {
1709     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1710     HRESULT hr;
1711
1712     TRACE("iface %p, primitive_type %#x, base_vertex_idx %u, min_vertex_idx %u,\n"
1713             "vertex_count %u, start_idx %u, primitive_count %u.\n",
1714             iface, PrimitiveType, BaseVertexIndex, MinVertexIndex,
1715             NumVertices, startIndex, primCount);
1716
1717     wined3d_mutex_lock();
1718     IWineD3DDevice_SetBaseVertexIndex(This->WineD3DDevice, BaseVertexIndex);
1719     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1720     hr = IWineD3DDevice_DrawIndexedPrimitive(This->WineD3DDevice, startIndex,
1721             vertex_count_from_primitive_count(PrimitiveType, primCount));
1722     wined3d_mutex_unlock();
1723
1724     return hr;
1725 }
1726
1727 static HRESULT WINAPI IDirect3DDevice9Impl_DrawPrimitiveUP(IDirect3DDevice9Ex *iface, D3DPRIMITIVETYPE PrimitiveType,
1728         UINT PrimitiveCount, const void *pVertexStreamZeroData, UINT VertexStreamZeroStride)
1729 {
1730     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1731     HRESULT hr;
1732
1733     TRACE("iface %p, primitive_type %#x, primitive_count %u, data %p, stride %u.\n",
1734             iface, PrimitiveType, PrimitiveCount, pVertexStreamZeroData, VertexStreamZeroStride);
1735
1736     wined3d_mutex_lock();
1737     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1738     hr = IWineD3DDevice_DrawPrimitiveUP(This->WineD3DDevice,
1739             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount),
1740             pVertexStreamZeroData, VertexStreamZeroStride);
1741     wined3d_mutex_unlock();
1742
1743     return hr;
1744 }
1745
1746 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawIndexedPrimitiveUP(LPDIRECT3DDEVICE9EX iface, D3DPRIMITIVETYPE PrimitiveType, UINT MinVertexIndex,
1747                                                              UINT NumVertexIndices, UINT PrimitiveCount, CONST void* pIndexData,
1748                                                              D3DFORMAT IndexDataFormat, CONST void* pVertexStreamZeroData, UINT VertexStreamZeroStride) {
1749     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1750     HRESULT hr;
1751
1752     TRACE("iface %p, primitive_type %#x, min_vertex_idx %u, index_count %u, primitive_count %u,\n"
1753             "index_data %p, index_format %#x, vertex_data %p, vertex_stride %u.\n",
1754             iface, PrimitiveType, MinVertexIndex, NumVertexIndices, PrimitiveCount,
1755             pIndexData, IndexDataFormat, pVertexStreamZeroData, VertexStreamZeroStride);
1756
1757     wined3d_mutex_lock();
1758     IWineD3DDevice_SetPrimitiveType(This->WineD3DDevice, PrimitiveType);
1759     hr = IWineD3DDevice_DrawIndexedPrimitiveUP(This->WineD3DDevice,
1760             vertex_count_from_primitive_count(PrimitiveType, PrimitiveCount), pIndexData,
1761             wined3dformat_from_d3dformat(IndexDataFormat), pVertexStreamZeroData, VertexStreamZeroStride);
1762     wined3d_mutex_unlock();
1763
1764     return hr;
1765 }
1766
1767 static HRESULT  WINAPI  IDirect3DDevice9Impl_ProcessVertices(LPDIRECT3DDEVICE9EX iface, UINT SrcStartIndex, UINT DestIndex, UINT VertexCount, IDirect3DVertexBuffer9* pDestBuffer, IDirect3DVertexDeclaration9* pVertexDecl, DWORD Flags) {
1768     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1769     IDirect3DVertexDeclaration9Impl *Decl = (IDirect3DVertexDeclaration9Impl *) pVertexDecl;
1770     HRESULT hr;
1771     IDirect3DVertexBuffer9Impl *dest = (IDirect3DVertexBuffer9Impl *) pDestBuffer;
1772
1773     TRACE("iface %p, src_start_idx %u, dst_idx %u, vertex_count %u, dst_buffer %p, declaration %p, flags %#x.\n",
1774             iface, SrcStartIndex, DestIndex, VertexCount, pDestBuffer, pVertexDecl, Flags);
1775
1776     wined3d_mutex_lock();
1777     hr = IWineD3DDevice_ProcessVertices(This->WineD3DDevice,SrcStartIndex, DestIndex, VertexCount, dest->wineD3DVertexBuffer, Decl ? Decl->wineD3DVertexDeclaration : NULL, Flags, dest->fvf);
1778     wined3d_mutex_unlock();
1779
1780     return hr;
1781 }
1782
1783 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(IDirect3DDevice9Ex *iface,
1784         const D3DVERTEXELEMENT9 *elements, IDirect3DVertexDeclaration9 **declaration)
1785 {
1786     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1787     IDirect3DVertexDeclaration9Impl *object;
1788     HRESULT hr;
1789
1790     TRACE("iface %p, elements %p, declaration %p.\n", iface, elements, declaration);
1791
1792     if (!declaration)
1793     {
1794         WARN("Caller passed a NULL declaration, returning D3DERR_INVALIDCALL.\n");
1795         return D3DERR_INVALIDCALL;
1796     }
1797
1798     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1799     if (!object)
1800     {
1801         ERR("Failed to allocate vertex declaration memory.\n");
1802         return E_OUTOFMEMORY;
1803     }
1804
1805     hr = vertexdeclaration_init(object, This, elements);
1806     if (FAILED(hr))
1807     {
1808         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
1809         HeapFree(GetProcessHeap(), 0, object);
1810         return hr;
1811     }
1812
1813     TRACE("Created vertex declaration %p.\n", object);
1814     *declaration = (IDirect3DVertexDeclaration9 *)object;
1815
1816     return D3D_OK;
1817 }
1818
1819 static IDirect3DVertexDeclaration9 *getConvertedDecl(IDirect3DDevice9Impl *This, DWORD fvf) {
1820     HRESULT hr;
1821     D3DVERTEXELEMENT9* elements = NULL;
1822     IDirect3DVertexDeclaration9* pDecl = NULL;
1823     int p, low, high; /* deliberately signed */
1824     IDirect3DVertexDeclaration9  **convertedDecls = This->convertedDecls;
1825
1826     TRACE("Searching for declaration for fvf %08x... ", fvf);
1827
1828     low = 0;
1829     high = This->numConvertedDecls - 1;
1830     while(low <= high) {
1831         p = (low + high) >> 1;
1832         TRACE("%d ", p);
1833         if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF == fvf) {
1834             TRACE("found %p\n", convertedDecls[p]);
1835             return convertedDecls[p];
1836         } else if(((IDirect3DVertexDeclaration9Impl *) convertedDecls[p])->convFVF < fvf) {
1837             low = p + 1;
1838         } else {
1839             high = p - 1;
1840         }
1841     }
1842     TRACE("not found. Creating and inserting at position %d.\n", low);
1843
1844     hr = vdecl_convert_fvf(fvf, &elements);
1845     if (hr != S_OK) return NULL;
1846
1847     hr = IDirect3DDevice9Impl_CreateVertexDeclaration((IDirect3DDevice9Ex *) This, elements, &pDecl);
1848     HeapFree(GetProcessHeap(), 0, elements); /* CreateVertexDeclaration makes a copy */
1849     if (hr != S_OK) return NULL;
1850
1851     if(This->declArraySize == This->numConvertedDecls) {
1852         int grow = max(This->declArraySize / 2, 8);
1853         convertedDecls = HeapReAlloc(GetProcessHeap(), 0, convertedDecls,
1854                                      sizeof(convertedDecls[0]) * (This->numConvertedDecls + grow));
1855         if(!convertedDecls) {
1856             /* This will destroy it */
1857             IDirect3DVertexDeclaration9_Release(pDecl);
1858             return NULL;
1859         }
1860         This->convertedDecls = convertedDecls;
1861         This->declArraySize += grow;
1862     }
1863
1864     memmove(convertedDecls + low + 1, convertedDecls + low, sizeof(IDirect3DVertexDeclaration9Impl *) * (This->numConvertedDecls - low));
1865     convertedDecls[low] = pDecl;
1866     This->numConvertedDecls++;
1867
1868     /* Will prevent the decl from being destroyed */
1869     ((IDirect3DVertexDeclaration9Impl *) pDecl)->convFVF = fvf;
1870     IDirect3DVertexDeclaration9_Release(pDecl); /* Does not destroy now */
1871
1872     TRACE("Returning %p. %d decls in array\n", pDecl, This->numConvertedDecls);
1873     return pDecl;
1874 }
1875
1876 static HRESULT WINAPI IDirect3DDevice9Impl_SetFVF(LPDIRECT3DDEVICE9EX iface, DWORD FVF) {
1877     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1878     IDirect3DVertexDeclaration9 *decl;
1879     HRESULT hr;
1880
1881     TRACE("iface %p, fvf %#x.\n", iface, FVF);
1882
1883     if (!FVF)
1884     {
1885         WARN("%#x is not a valid FVF\n", FVF);
1886         return D3D_OK;
1887     }
1888
1889     wined3d_mutex_lock();
1890     decl = getConvertedDecl(This, FVF);
1891     wined3d_mutex_unlock();
1892
1893     if (!decl)
1894     {
1895          /* Any situation when this should happen, except out of memory? */
1896          ERR("Failed to create a converted vertex declaration\n");
1897          return D3DERR_DRIVERINTERNALERROR;
1898     }
1899
1900     hr = IDirect3DDevice9Impl_SetVertexDeclaration(iface, decl);
1901     if (FAILED(hr)) ERR("Failed to set vertex declaration\n");
1902
1903     return hr;
1904 }
1905
1906 static HRESULT WINAPI IDirect3DDevice9Impl_GetFVF(IDirect3DDevice9Ex *iface, DWORD *pFVF)
1907 {
1908     IDirect3DVertexDeclaration9 *decl;
1909     HRESULT hr;
1910
1911     TRACE("iface %p, fvf %p.\n", iface, pFVF);
1912
1913     hr = IDirect3DDevice9_GetVertexDeclaration(iface, &decl);
1914     if (FAILED(hr))
1915     {
1916         WARN("Failed to get vertex declaration, %#x\n", hr);
1917         *pFVF = 0;
1918         return hr;
1919     }
1920
1921     if (decl)
1922     {
1923         *pFVF = ((IDirect3DVertexDeclaration9Impl *)decl)->convFVF;
1924         IDirect3DVertexDeclaration9_Release(decl);
1925     }
1926     else
1927     {
1928         *pFVF = 0;
1929     }
1930
1931     TRACE("Returning FVF %#x\n", *pFVF);
1932
1933     return hr;
1934 }
1935
1936 static HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexShader(IDirect3DDevice9Ex *iface,
1937         const DWORD *byte_code, IDirect3DVertexShader9 **shader)
1938 {
1939     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1940     IDirect3DVertexShader9Impl *object;
1941     HRESULT hr;
1942
1943     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
1944
1945     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
1946     if (!object)
1947     {
1948         ERR("Failed to allocate vertex shader memory.\n");
1949         return E_OUTOFMEMORY;
1950     }
1951
1952     hr = vertexshader_init(object, This, byte_code);
1953     if (FAILED(hr))
1954     {
1955         WARN("Failed to initialize vertex shader, hr %#x.\n", hr);
1956         HeapFree(GetProcessHeap(), 0, object);
1957         return hr;
1958     }
1959
1960     TRACE("Created vertex shader %p.\n", object);
1961     *shader = (IDirect3DVertexShader9 *)object;
1962
1963     return D3D_OK;
1964 }
1965
1966 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9* pStreamData, UINT OffsetInBytes, UINT Stride) {
1967     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1968     HRESULT hr;
1969
1970     TRACE("iface %p, stream_idx %u, buffer %p, offset %u, stride %u.\n",
1971             iface, StreamNumber, pStreamData, OffsetInBytes, Stride);
1972
1973     wined3d_mutex_lock();
1974     hr = IWineD3DDevice_SetStreamSource(This->WineD3DDevice, StreamNumber,
1975             pStreamData ? ((IDirect3DVertexBuffer9Impl *)pStreamData)->wineD3DVertexBuffer : NULL,
1976             OffsetInBytes, Stride);
1977     wined3d_mutex_unlock();
1978
1979     return hr;
1980 }
1981
1982 static HRESULT WINAPI IDirect3DDevice9Impl_GetStreamSource(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, IDirect3DVertexBuffer9 **pStream, UINT* OffsetInBytes, UINT* pStride) {
1983     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
1984     IWineD3DBuffer *retStream = NULL;
1985     HRESULT rc = D3D_OK;
1986
1987     TRACE("iface %p, stream_idx %u, buffer %p, offset %p, stride %p.\n",
1988             iface, StreamNumber, pStream, OffsetInBytes, pStride);
1989
1990     if(pStream == NULL){
1991         return D3DERR_INVALIDCALL;
1992     }
1993
1994     wined3d_mutex_lock();
1995     rc = IWineD3DDevice_GetStreamSource(This->WineD3DDevice, StreamNumber, &retStream, OffsetInBytes, pStride);
1996     if (rc == D3D_OK  && NULL != retStream) {
1997         IWineD3DBuffer_GetParent(retStream, (IUnknown **)pStream);
1998         IWineD3DBuffer_Release(retStream);
1999     }else{
2000         if (rc != D3D_OK){
2001             FIXME("Call to GetStreamSource failed %p %p\n", OffsetInBytes, pStride);
2002         }
2003         *pStream = NULL;
2004     }
2005     wined3d_mutex_unlock();
2006
2007     return rc;
2008 }
2009
2010 static HRESULT WINAPI IDirect3DDevice9Impl_SetStreamSourceFreq(IDirect3DDevice9Ex *iface, UINT StreamNumber,
2011         UINT Divider)
2012 {
2013     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2014     HRESULT hr;
2015
2016     TRACE("iface %p, stream_idx %u, freq %u.\n", iface, StreamNumber, Divider);
2017
2018     wined3d_mutex_lock();
2019     hr = IWineD3DDevice_SetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
2020     wined3d_mutex_unlock();
2021
2022     return hr;
2023 }
2024
2025 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetStreamSourceFreq(LPDIRECT3DDEVICE9EX iface, UINT StreamNumber, UINT* Divider) {
2026     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2027     HRESULT hr;
2028
2029     TRACE("iface %p, stream_idx %u, freq %p.\n", iface, StreamNumber, Divider);
2030
2031     wined3d_mutex_lock();
2032     hr = IWineD3DDevice_GetStreamSourceFreq(This->WineD3DDevice, StreamNumber, Divider);
2033     wined3d_mutex_unlock();
2034
2035     return hr;
2036 }
2037
2038 static HRESULT  WINAPI  IDirect3DDevice9Impl_SetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9* pIndexData) {
2039     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2040     HRESULT hr;
2041     IDirect3DIndexBuffer9Impl *ib = (IDirect3DIndexBuffer9Impl *) pIndexData;
2042
2043     TRACE("iface %p, buffer %p.\n", iface, pIndexData);
2044
2045     wined3d_mutex_lock();
2046     hr = IWineD3DDevice_SetIndexBuffer(This->WineD3DDevice,
2047             ib ? ib->wineD3DIndexBuffer : NULL,
2048             ib ? ib->format : WINED3DFMT_UNKNOWN);
2049     wined3d_mutex_unlock();
2050
2051     return hr;
2052 }
2053
2054 static HRESULT  WINAPI  IDirect3DDevice9Impl_GetIndices(LPDIRECT3DDEVICE9EX iface, IDirect3DIndexBuffer9 **ppIndexData) {
2055     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2056     IWineD3DBuffer *retIndexData = NULL;
2057     HRESULT rc = D3D_OK;
2058
2059     TRACE("iface %p, buffer %p.\n", iface, ppIndexData);
2060
2061     if(ppIndexData == NULL){
2062         return D3DERR_INVALIDCALL;
2063     }
2064
2065     wined3d_mutex_lock();
2066     rc = IWineD3DDevice_GetIndexBuffer(This->WineD3DDevice, &retIndexData);
2067     if (SUCCEEDED(rc) && retIndexData) {
2068         IWineD3DBuffer_GetParent(retIndexData, (IUnknown **)ppIndexData);
2069         IWineD3DBuffer_Release(retIndexData);
2070     } else {
2071         if (FAILED(rc)) FIXME("Call to GetIndices failed\n");
2072         *ppIndexData = NULL;
2073     }
2074     wined3d_mutex_unlock();
2075
2076     return rc;
2077 }
2078
2079 static HRESULT WINAPI IDirect3DDevice9Impl_CreatePixelShader(IDirect3DDevice9Ex *iface,
2080         const DWORD *byte_code, IDirect3DPixelShader9 **shader)
2081 {
2082     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2083     IDirect3DPixelShader9Impl *object;
2084     HRESULT hr;
2085
2086     TRACE("iface %p, byte_code %p, shader %p.\n", iface, byte_code, shader);
2087
2088     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2089     if (!object)
2090     {
2091         FIXME("Failed to allocate pixel shader memory.\n");
2092         return E_OUTOFMEMORY;
2093     }
2094
2095     hr = pixelshader_init(object, This, byte_code);
2096     if (FAILED(hr))
2097     {
2098         WARN("Failed to initialize pixel shader, hr %#x.\n", hr);
2099         HeapFree(GetProcessHeap(), 0, object);
2100         return hr;
2101     }
2102
2103     TRACE("Created pixel shader %p.\n", object);
2104     *shader = (IDirect3DPixelShader9 *)object;
2105
2106     return D3D_OK;
2107 }
2108
2109 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawRectPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DRECTPATCH_INFO* pRectPatchInfo) {
2110     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2111     HRESULT hr;
2112
2113     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2114             iface, Handle, pNumSegs, pRectPatchInfo);
2115
2116     wined3d_mutex_lock();
2117     hr = IWineD3DDevice_DrawRectPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DRECTPATCH_INFO *)pRectPatchInfo);
2118     wined3d_mutex_unlock();
2119
2120     return hr;
2121 }
2122
2123 static HRESULT  WINAPI  IDirect3DDevice9Impl_DrawTriPatch(LPDIRECT3DDEVICE9EX iface, UINT Handle, CONST float* pNumSegs, CONST D3DTRIPATCH_INFO* pTriPatchInfo) {
2124     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2125     HRESULT hr;
2126
2127     TRACE("iface %p, handle %#x, segment_count %p, patch_info %p.\n",
2128             iface, Handle, pNumSegs, pTriPatchInfo);
2129
2130     wined3d_mutex_lock();
2131     hr = IWineD3DDevice_DrawTriPatch(This->WineD3DDevice, Handle, pNumSegs, (CONST WINED3DTRIPATCH_INFO *)pTriPatchInfo);
2132     wined3d_mutex_unlock();
2133
2134     return hr;
2135 }
2136
2137 static HRESULT  WINAPI  IDirect3DDevice9Impl_DeletePatch(LPDIRECT3DDEVICE9EX iface, UINT Handle) {
2138     IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
2139     HRESULT hr;
2140
2141     TRACE("iface %p, handle %#x.\n", iface, Handle);
2142
2143     wined3d_mutex_lock();
2144     hr = IWineD3DDevice_DeletePatch(This->WineD3DDevice, Handle);
2145     wined3d_mutex_unlock();
2146
2147     return hr;
2148 }
2149
2150 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetConvolutionMonoKernel(IDirect3DDevice9Ex *iface,
2151         UINT width, UINT height, float *rows, float *columns)
2152 {
2153     FIXME("iface %p, width %u, height %u, rows %p, columns %p stub!\n",
2154             iface, width, height, rows, columns);
2155
2156     return WINED3DERR_INVALIDCALL;
2157 }
2158
2159 static HRESULT WINAPI IDirect3DDevice9ExImpl_ComposeRects(IDirect3DDevice9Ex *iface,
2160         IDirect3DSurface9 *src_surface, IDirect3DSurface9 *dst_surface, IDirect3DVertexBuffer9 *src_descs,
2161         UINT rect_count, IDirect3DVertexBuffer9 *dst_descs, D3DCOMPOSERECTSOP operation, INT offset_x, INT offset_y)
2162 {
2163     FIXME("iface %p, src_surface %p, dst_surface %p, src_descs %p, rect_count %u,\n"
2164             "dst_descs %p, operation %#x, offset_x %u, offset_y %u stub!\n",
2165             iface, src_surface, dst_surface, src_descs, rect_count,
2166             dst_descs, operation, offset_x, offset_y);
2167
2168     return WINED3DERR_INVALIDCALL;
2169 }
2170
2171 static HRESULT WINAPI IDirect3DDevice9ExImpl_PresentEx(IDirect3DDevice9Ex *iface,
2172         const RECT *src_rect, const RECT *dst_rect, HWND dst_window_override,
2173         const RGNDATA *dirty_region, DWORD flags)
2174 {
2175     FIXME("iface %p, src_rect %p, dst_rect %p, dst_window_override %p, dirty_region %p, flags %#x stub!\n",
2176             iface, src_rect, dst_rect, dst_window_override, dirty_region, flags);
2177
2178     return WINED3DERR_INVALIDCALL;
2179 }
2180
2181 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT *priority)
2182 {
2183     FIXME("iface %p, priority %p stub!\n", iface, priority);
2184
2185     return WINED3DERR_INVALIDCALL;
2186 }
2187
2188 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetGPUThreadPriority(IDirect3DDevice9Ex *iface, INT priority)
2189 {
2190     FIXME("iface %p, priority %d stub!\n", iface, priority);
2191
2192     return WINED3DERR_INVALIDCALL;
2193 }
2194
2195 static HRESULT WINAPI IDirect3DDevice9ExImpl_WaitForVBlank(IDirect3DDevice9Ex *iface, UINT swapchain_idx)
2196 {
2197     FIXME("iface %p, swapchain_idx %u stub!\n", iface, swapchain_idx);
2198
2199     return WINED3DERR_INVALIDCALL;
2200 }
2201
2202 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckResourceResidency(IDirect3DDevice9Ex *iface,
2203         IDirect3DResource9 **resources, UINT32 resource_count)
2204 {
2205     FIXME("iface %p, resources %p, resource_count %u stub!\n",
2206             iface, resources, resource_count);
2207
2208     return WINED3DERR_INVALIDCALL;
2209 }
2210
2211 static HRESULT WINAPI IDirect3DDevice9ExImpl_SetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT max_latency)
2212 {
2213     FIXME("iface %p, max_latency %u stub!\n", iface, max_latency);
2214
2215     return WINED3DERR_INVALIDCALL;
2216 }
2217
2218 static HRESULT WINAPI IDirect3DDevice9ExImpl_GetMaximumFrameLatency(IDirect3DDevice9Ex *iface, UINT *max_latency)
2219 {
2220     FIXME("iface %p, max_latency %p stub!\n", iface, max_latency);
2221
2222     *max_latency = 2;
2223
2224     return WINED3DERR_INVALIDCALL;
2225 }
2226
2227 static HRESULT WINAPI IDirect3DDevice9ExImpl_CheckDeviceState(IDirect3DDevice9Ex *iface, HWND dst_window)
2228 {
2229     FIXME("iface %p, dst_window %p stub!\n", iface, dst_window);
2230
2231     return WINED3DERR_INVALIDCALL;
2232 }
2233
2234 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateRenderTargetEx(IDirect3DDevice9Ex *iface,
2235         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2236         BOOL lockable, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2237 {
2238     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2239             "lockable %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2240             iface, width, height, format, multisample_type, multisample_quality,
2241             lockable, surface, shared_handle, usage);
2242
2243     return WINED3DERR_INVALIDCALL;
2244 }
2245
2246 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx(IDirect3DDevice9Ex *iface,
2247         UINT width, UINT height, D3DFORMAT format, D3DPOOL pool, IDirect3DSurface9 **surface,
2248         HANDLE *shared_handle, DWORD usage)
2249 {
2250     FIXME("iface %p, width %u, height %u, format %#x, pool %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2251             iface, width, height, format, pool, surface, shared_handle, usage);
2252
2253     return WINED3DERR_INVALIDCALL;
2254 }
2255
2256 static HRESULT WINAPI IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx(IDirect3DDevice9Ex *iface,
2257         UINT width, UINT height, D3DFORMAT format, D3DMULTISAMPLE_TYPE multisample_type, DWORD multisample_quality,
2258         BOOL discard, IDirect3DSurface9 **surface, HANDLE *shared_handle, DWORD usage)
2259 {
2260     FIXME("iface %p, width %u, height %u, format %#x, multisample_type %#x, multisample_quality %u,\n"
2261             "discard %#x, surface %p, shared_handle %p, usage %#x stub!\n",
2262             iface, width, height, format, multisample_type, multisample_quality,
2263             discard, surface, shared_handle, usage);
2264
2265     return WINED3DERR_INVALIDCALL;
2266 }
2267
2268 static HRESULT WINAPI IDirect3DDevice9ExImpl_ResetEx(IDirect3DDevice9Ex *iface,
2269         D3DPRESENT_PARAMETERS *present_parameters, D3DDISPLAYMODEEX *mode)
2270 {
2271     FIXME("iface %p, present_parameters %p, mode %p stub!\n", iface, present_parameters, mode);
2272
2273     return WINED3DERR_INVALIDCALL;
2274 }
2275
2276 static HRESULT  WINAPI  IDirect3DDevice9ExImpl_GetDisplayModeEx(IDirect3DDevice9Ex *iface,
2277         UINT swapchain_idx, D3DDISPLAYMODEEX *mode, D3DDISPLAYROTATION *rotation)
2278 {
2279     FIXME("iface %p, swapchain_idx %u, mode %p, rotation %p stub!\n", iface, swapchain_idx, mode, rotation);
2280
2281     return WINED3DERR_INVALIDCALL;
2282 }
2283
2284 static const IDirect3DDevice9ExVtbl Direct3DDevice9_Vtbl =
2285 {
2286     /* IUnknown */
2287     IDirect3DDevice9Impl_QueryInterface,
2288     IDirect3DDevice9Impl_AddRef,
2289     IDirect3DDevice9Impl_Release,
2290     /* IDirect3DDevice9 */
2291     IDirect3DDevice9Impl_TestCooperativeLevel,
2292     IDirect3DDevice9Impl_GetAvailableTextureMem,
2293     IDirect3DDevice9Impl_EvictManagedResources,
2294     IDirect3DDevice9Impl_GetDirect3D,
2295     IDirect3DDevice9Impl_GetDeviceCaps,
2296     IDirect3DDevice9Impl_GetDisplayMode,
2297     IDirect3DDevice9Impl_GetCreationParameters,
2298     IDirect3DDevice9Impl_SetCursorProperties,
2299     IDirect3DDevice9Impl_SetCursorPosition,
2300     IDirect3DDevice9Impl_ShowCursor,
2301     IDirect3DDevice9Impl_CreateAdditionalSwapChain,
2302     IDirect3DDevice9Impl_GetSwapChain,
2303     IDirect3DDevice9Impl_GetNumberOfSwapChains,
2304     IDirect3DDevice9Impl_Reset,
2305     IDirect3DDevice9Impl_Present,
2306     IDirect3DDevice9Impl_GetBackBuffer,
2307     IDirect3DDevice9Impl_GetRasterStatus,
2308     IDirect3DDevice9Impl_SetDialogBoxMode,
2309     IDirect3DDevice9Impl_SetGammaRamp,
2310     IDirect3DDevice9Impl_GetGammaRamp,
2311     IDirect3DDevice9Impl_CreateTexture,
2312     IDirect3DDevice9Impl_CreateVolumeTexture,
2313     IDirect3DDevice9Impl_CreateCubeTexture,
2314     IDirect3DDevice9Impl_CreateVertexBuffer,
2315     IDirect3DDevice9Impl_CreateIndexBuffer,
2316     IDirect3DDevice9Impl_CreateRenderTarget,
2317     IDirect3DDevice9Impl_CreateDepthStencilSurface,
2318     IDirect3DDevice9Impl_UpdateSurface,
2319     IDirect3DDevice9Impl_UpdateTexture,
2320     IDirect3DDevice9Impl_GetRenderTargetData,
2321     IDirect3DDevice9Impl_GetFrontBufferData,
2322     IDirect3DDevice9Impl_StretchRect,
2323     IDirect3DDevice9Impl_ColorFill,
2324     IDirect3DDevice9Impl_CreateOffscreenPlainSurface,
2325     IDirect3DDevice9Impl_SetRenderTarget,
2326     IDirect3DDevice9Impl_GetRenderTarget,
2327     IDirect3DDevice9Impl_SetDepthStencilSurface,
2328     IDirect3DDevice9Impl_GetDepthStencilSurface,
2329     IDirect3DDevice9Impl_BeginScene,
2330     IDirect3DDevice9Impl_EndScene,
2331     IDirect3DDevice9Impl_Clear,
2332     IDirect3DDevice9Impl_SetTransform,
2333     IDirect3DDevice9Impl_GetTransform,
2334     IDirect3DDevice9Impl_MultiplyTransform,
2335     IDirect3DDevice9Impl_SetViewport,
2336     IDirect3DDevice9Impl_GetViewport,
2337     IDirect3DDevice9Impl_SetMaterial,
2338     IDirect3DDevice9Impl_GetMaterial,
2339     IDirect3DDevice9Impl_SetLight,
2340     IDirect3DDevice9Impl_GetLight,
2341     IDirect3DDevice9Impl_LightEnable,
2342     IDirect3DDevice9Impl_GetLightEnable,
2343     IDirect3DDevice9Impl_SetClipPlane,
2344     IDirect3DDevice9Impl_GetClipPlane,
2345     IDirect3DDevice9Impl_SetRenderState,
2346     IDirect3DDevice9Impl_GetRenderState,
2347     IDirect3DDevice9Impl_CreateStateBlock,
2348     IDirect3DDevice9Impl_BeginStateBlock,
2349     IDirect3DDevice9Impl_EndStateBlock,
2350     IDirect3DDevice9Impl_SetClipStatus,
2351     IDirect3DDevice9Impl_GetClipStatus,
2352     IDirect3DDevice9Impl_GetTexture,
2353     IDirect3DDevice9Impl_SetTexture,
2354     IDirect3DDevice9Impl_GetTextureStageState,
2355     IDirect3DDevice9Impl_SetTextureStageState,
2356     IDirect3DDevice9Impl_GetSamplerState,
2357     IDirect3DDevice9Impl_SetSamplerState,
2358     IDirect3DDevice9Impl_ValidateDevice,
2359     IDirect3DDevice9Impl_SetPaletteEntries,
2360     IDirect3DDevice9Impl_GetPaletteEntries,
2361     IDirect3DDevice9Impl_SetCurrentTexturePalette,
2362     IDirect3DDevice9Impl_GetCurrentTexturePalette,
2363     IDirect3DDevice9Impl_SetScissorRect,
2364     IDirect3DDevice9Impl_GetScissorRect,
2365     IDirect3DDevice9Impl_SetSoftwareVertexProcessing,
2366     IDirect3DDevice9Impl_GetSoftwareVertexProcessing,
2367     IDirect3DDevice9Impl_SetNPatchMode,
2368     IDirect3DDevice9Impl_GetNPatchMode,
2369     IDirect3DDevice9Impl_DrawPrimitive,
2370     IDirect3DDevice9Impl_DrawIndexedPrimitive,
2371     IDirect3DDevice9Impl_DrawPrimitiveUP,
2372     IDirect3DDevice9Impl_DrawIndexedPrimitiveUP,
2373     IDirect3DDevice9Impl_ProcessVertices,
2374     IDirect3DDevice9Impl_CreateVertexDeclaration,
2375     IDirect3DDevice9Impl_SetVertexDeclaration,
2376     IDirect3DDevice9Impl_GetVertexDeclaration,
2377     IDirect3DDevice9Impl_SetFVF,
2378     IDirect3DDevice9Impl_GetFVF,
2379     IDirect3DDevice9Impl_CreateVertexShader,
2380     IDirect3DDevice9Impl_SetVertexShader,
2381     IDirect3DDevice9Impl_GetVertexShader,
2382     IDirect3DDevice9Impl_SetVertexShaderConstantF,
2383     IDirect3DDevice9Impl_GetVertexShaderConstantF,
2384     IDirect3DDevice9Impl_SetVertexShaderConstantI,
2385     IDirect3DDevice9Impl_GetVertexShaderConstantI,
2386     IDirect3DDevice9Impl_SetVertexShaderConstantB,
2387     IDirect3DDevice9Impl_GetVertexShaderConstantB,
2388     IDirect3DDevice9Impl_SetStreamSource,
2389     IDirect3DDevice9Impl_GetStreamSource,
2390     IDirect3DDevice9Impl_SetStreamSourceFreq,
2391     IDirect3DDevice9Impl_GetStreamSourceFreq,
2392     IDirect3DDevice9Impl_SetIndices,
2393     IDirect3DDevice9Impl_GetIndices,
2394     IDirect3DDevice9Impl_CreatePixelShader,
2395     IDirect3DDevice9Impl_SetPixelShader,
2396     IDirect3DDevice9Impl_GetPixelShader,
2397     IDirect3DDevice9Impl_SetPixelShaderConstantF,
2398     IDirect3DDevice9Impl_GetPixelShaderConstantF,
2399     IDirect3DDevice9Impl_SetPixelShaderConstantI,
2400     IDirect3DDevice9Impl_GetPixelShaderConstantI,
2401     IDirect3DDevice9Impl_SetPixelShaderConstantB,
2402     IDirect3DDevice9Impl_GetPixelShaderConstantB,
2403     IDirect3DDevice9Impl_DrawRectPatch,
2404     IDirect3DDevice9Impl_DrawTriPatch,
2405     IDirect3DDevice9Impl_DeletePatch,
2406     IDirect3DDevice9Impl_CreateQuery,
2407     /* IDirect3DDevice9Ex */
2408     IDirect3DDevice9ExImpl_SetConvolutionMonoKernel,
2409     IDirect3DDevice9ExImpl_ComposeRects,
2410     IDirect3DDevice9ExImpl_PresentEx,
2411     IDirect3DDevice9ExImpl_GetGPUThreadPriority,
2412     IDirect3DDevice9ExImpl_SetGPUThreadPriority,
2413     IDirect3DDevice9ExImpl_WaitForVBlank,
2414     IDirect3DDevice9ExImpl_CheckResourceResidency,
2415     IDirect3DDevice9ExImpl_SetMaximumFrameLatency,
2416     IDirect3DDevice9ExImpl_GetMaximumFrameLatency,
2417     IDirect3DDevice9ExImpl_CheckDeviceState,
2418     IDirect3DDevice9ExImpl_CreateRenderTargetEx,
2419     IDirect3DDevice9ExImpl_CreateOffscreenPlainSurfaceEx,
2420     IDirect3DDevice9ExImpl_CreateDepthStencilSurfaceEx,
2421     IDirect3DDevice9ExImpl_ResetEx,
2422     IDirect3DDevice9ExImpl_GetDisplayModeEx
2423 };
2424
2425 /* IWineD3DDeviceParent IUnknown methods */
2426
2427 static inline struct IDirect3DDevice9Impl *device_from_device_parent(IWineD3DDeviceParent *iface)
2428 {
2429     return (struct IDirect3DDevice9Impl *)((char*)iface
2430             - FIELD_OFFSET(struct IDirect3DDevice9Impl, device_parent_vtbl));
2431 }
2432
2433 static HRESULT STDMETHODCALLTYPE device_parent_QueryInterface(IWineD3DDeviceParent *iface, REFIID riid, void **object)
2434 {
2435     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2436     return IDirect3DDevice9Impl_QueryInterface((IDirect3DDevice9Ex *)This, riid, object);
2437 }
2438
2439 static ULONG STDMETHODCALLTYPE device_parent_AddRef(IWineD3DDeviceParent *iface)
2440 {
2441     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2442     return IDirect3DDevice9Impl_AddRef((IDirect3DDevice9Ex *)This);
2443 }
2444
2445 static ULONG STDMETHODCALLTYPE device_parent_Release(IWineD3DDeviceParent *iface)
2446 {
2447     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2448     return IDirect3DDevice9Impl_Release((IDirect3DDevice9Ex *)This);
2449 }
2450
2451 /* IWineD3DDeviceParent methods */
2452
2453 static void STDMETHODCALLTYPE device_parent_WineD3DDeviceCreated(IWineD3DDeviceParent *iface, IWineD3DDevice *device)
2454 {
2455     TRACE("iface %p, device %p\n", iface, device);
2456 }
2457
2458 static HRESULT STDMETHODCALLTYPE device_parent_CreateSurface(IWineD3DDeviceParent *iface,
2459         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, DWORD usage,
2460         WINED3DPOOL pool, UINT level, WINED3DCUBEMAP_FACES face, IWineD3DSurface **surface)
2461 {
2462     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2463     IDirect3DSurface9Impl *d3d_surface;
2464     BOOL lockable = TRUE;
2465     HRESULT hr;
2466
2467     TRACE("iface %p, superior %p, width %u, height %u, format %#x, usage %#x,\n"
2468             "\tpool %#x, level %u, face %u, surface %p\n",
2469             iface, superior, width, height, format, usage, pool, level, face, surface);
2470
2471     if (pool == WINED3DPOOL_DEFAULT && !(usage & D3DUSAGE_DYNAMIC))
2472         lockable = FALSE;
2473
2474     hr = IDirect3DDevice9Impl_CreateSurface((IDirect3DDevice9Ex *)This, width, height,
2475             d3dformat_from_wined3dformat(format), lockable, FALSE /* Discard */, level,
2476             (IDirect3DSurface9 **)&d3d_surface, usage, pool, D3DMULTISAMPLE_NONE, 0 /* MultisampleQuality */);
2477     if (FAILED(hr))
2478     {
2479         ERR("(%p) CreateSurface failed, returning %#x\n", iface, hr);
2480         return hr;
2481     }
2482
2483     *surface = d3d_surface->wineD3DSurface;
2484     IWineD3DSurface_AddRef(*surface);
2485
2486     d3d_surface->container = superior;
2487     IDirect3DDevice9Ex_Release(d3d_surface->parentDevice);
2488     d3d_surface->parentDevice = NULL;
2489
2490     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
2491     d3d_surface->forwardReference = superior;
2492
2493     return hr;
2494 }
2495
2496 static HRESULT STDMETHODCALLTYPE device_parent_CreateRenderTarget(IWineD3DDeviceParent *iface,
2497         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2498         DWORD multisample_quality, BOOL lockable, IWineD3DSurface **surface)
2499 {
2500     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2501     IDirect3DSurface9Impl *d3d_surface;
2502     HRESULT hr;
2503
2504     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2505             "\tmultisample_quality %u, lockable %u, surface %p\n",
2506             iface, superior, width, height, format, multisample_type, multisample_quality, lockable, surface);
2507
2508     hr = IDirect3DDevice9Impl_CreateRenderTarget((IDirect3DDevice9Ex *)This, width, height,
2509             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, lockable,
2510             (IDirect3DSurface9 **)&d3d_surface, NULL);
2511     if (FAILED(hr))
2512     {
2513         ERR("(%p) CreateRenderTarget failed, returning %#x\n", iface, hr);
2514         return hr;
2515     }
2516
2517     *surface = d3d_surface->wineD3DSurface;
2518     IWineD3DSurface_AddRef(*surface);
2519
2520     d3d_surface->container = superior;
2521     /* Implicit surfaces are created with an refcount of 0 */
2522     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
2523
2524     return hr;
2525 }
2526
2527 static HRESULT STDMETHODCALLTYPE device_parent_CreateDepthStencilSurface(IWineD3DDeviceParent *iface,
2528         IUnknown *superior, UINT width, UINT height, WINED3DFORMAT format, WINED3DMULTISAMPLE_TYPE multisample_type,
2529         DWORD multisample_quality, BOOL discard, IWineD3DSurface **surface)
2530 {
2531     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2532     IDirect3DSurface9Impl *d3d_surface;
2533     HRESULT hr;
2534
2535     TRACE("iface %p, superior %p, width %u, height %u, format %#x, multisample_type %#x,\n"
2536             "\tmultisample_quality %u, discard %u, surface %p\n",
2537             iface, superior, width, height, format, multisample_type, multisample_quality, discard, surface);
2538
2539     hr = IDirect3DDevice9Impl_CreateDepthStencilSurface((IDirect3DDevice9Ex *)This, width, height,
2540             d3dformat_from_wined3dformat(format), multisample_type, multisample_quality, discard,
2541             (IDirect3DSurface9 **)&d3d_surface, NULL);
2542     if (FAILED(hr))
2543     {
2544         ERR("(%p) CreateDepthStencilSurface failed, returning %#x\n", iface, hr);
2545         return hr;
2546     }
2547
2548     *surface = d3d_surface->wineD3DSurface;
2549     IWineD3DSurface_AddRef(*surface);
2550     d3d_surface->container = (IUnknown *)This;
2551     /* Implicit surfaces are created with an refcount of 0 */
2552     IDirect3DSurface9_Release((IDirect3DSurface9 *)d3d_surface);
2553
2554     return hr;
2555 }
2556
2557 static HRESULT STDMETHODCALLTYPE device_parent_CreateVolume(IWineD3DDeviceParent *iface,
2558         IUnknown *superior, UINT width, UINT height, UINT depth, WINED3DFORMAT format,
2559         WINED3DPOOL pool, DWORD usage, IWineD3DVolume **volume)
2560 {
2561     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2562     IDirect3DVolume9Impl *object;
2563     HRESULT hr;
2564
2565     TRACE("iface %p, superior %p, width %u, height %u, depth %u, format %#x, pool %#x, usage %#x, volume %p\n",
2566             iface, superior, width, height, depth, format, pool, usage, volume);
2567
2568     /* Allocate the storage for the device */
2569     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
2570     if (!object)
2571     {
2572         FIXME("Allocation of memory failed\n");
2573         *volume = NULL;
2574         return D3DERR_OUTOFVIDEOMEMORY;
2575     }
2576
2577     hr = volume_init(object, This, width, height, depth, usage, format, pool);
2578     if (FAILED(hr))
2579     {
2580         WARN("Failed to initialize volume, hr %#x.\n", hr);
2581         HeapFree(GetProcessHeap(), 0, object);
2582         return hr;
2583     }
2584
2585     *volume = object->wineD3DVolume;
2586     IWineD3DVolume_AddRef(*volume);
2587     IDirect3DVolume9_Release((IDirect3DVolume9 *)object);
2588
2589     object->container = superior;
2590     object->forwardReference = superior;
2591
2592     TRACE("(%p) Created volume %p\n", iface, object);
2593
2594     return hr;
2595 }
2596
2597 static HRESULT STDMETHODCALLTYPE device_parent_CreateSwapChain(IWineD3DDeviceParent *iface,
2598         WINED3DPRESENT_PARAMETERS *present_parameters, IWineD3DSwapChain **swapchain)
2599 {
2600     struct IDirect3DDevice9Impl *This = device_from_device_parent(iface);
2601     IDirect3DSwapChain9Impl *d3d_swapchain;
2602     D3DPRESENT_PARAMETERS local_parameters;
2603     HRESULT hr;
2604
2605     TRACE("iface %p, present_parameters %p, swapchain %p\n", iface, present_parameters, swapchain);
2606
2607     /* Copy the presentation parameters */
2608     local_parameters.BackBufferWidth = present_parameters->BackBufferWidth;
2609     local_parameters.BackBufferHeight = present_parameters->BackBufferHeight;
2610     local_parameters.BackBufferFormat = d3dformat_from_wined3dformat(present_parameters->BackBufferFormat);
2611     local_parameters.BackBufferCount = present_parameters->BackBufferCount;
2612     local_parameters.MultiSampleType = present_parameters->MultiSampleType;
2613     local_parameters.MultiSampleQuality = present_parameters->MultiSampleQuality;
2614     local_parameters.SwapEffect = present_parameters->SwapEffect;
2615     local_parameters.hDeviceWindow = present_parameters->hDeviceWindow;
2616     local_parameters.Windowed = present_parameters->Windowed;
2617     local_parameters.EnableAutoDepthStencil = present_parameters->EnableAutoDepthStencil;
2618     local_parameters.AutoDepthStencilFormat = d3dformat_from_wined3dformat(present_parameters->AutoDepthStencilFormat);
2619     local_parameters.Flags = present_parameters->Flags;
2620     local_parameters.FullScreen_RefreshRateInHz = present_parameters->FullScreen_RefreshRateInHz;
2621     local_parameters.PresentationInterval = present_parameters->PresentationInterval;
2622
2623     hr = IDirect3DDevice9Impl_CreateAdditionalSwapChain((IDirect3DDevice9Ex *)This,
2624             &local_parameters, (IDirect3DSwapChain9 **)&d3d_swapchain);
2625     if (FAILED(hr))
2626     {
2627         ERR("(%p) CreateAdditionalSwapChain failed, returning %#x\n", iface, hr);
2628         *swapchain = NULL;
2629         return hr;
2630     }
2631
2632     *swapchain = d3d_swapchain->wineD3DSwapChain;
2633     d3d_swapchain->isImplicit = TRUE;
2634     /* Implicit swap chains are created with an refcount of 0 */
2635     IDirect3DSwapChain9_Release((IDirect3DSwapChain9 *)d3d_swapchain);
2636
2637     /* Copy back the presentation parameters */
2638     present_parameters->BackBufferWidth = local_parameters.BackBufferWidth;
2639     present_parameters->BackBufferHeight = local_parameters.BackBufferHeight;
2640     present_parameters->BackBufferFormat = wined3dformat_from_d3dformat(local_parameters.BackBufferFormat);
2641     present_parameters->BackBufferCount = local_parameters.BackBufferCount;
2642     present_parameters->MultiSampleType = local_parameters.MultiSampleType;
2643     present_parameters->MultiSampleQuality = local_parameters.MultiSampleQuality;
2644     present_parameters->SwapEffect = local_parameters.SwapEffect;
2645     present_parameters->hDeviceWindow = local_parameters.hDeviceWindow;
2646     present_parameters->Windowed = local_parameters.Windowed;
2647     present_parameters->EnableAutoDepthStencil = local_parameters.EnableAutoDepthStencil;
2648     present_parameters->AutoDepthStencilFormat = wined3dformat_from_d3dformat(local_parameters.AutoDepthStencilFormat);
2649     present_parameters->Flags = local_parameters.Flags;
2650     present_parameters->FullScreen_RefreshRateInHz = local_parameters.FullScreen_RefreshRateInHz;
2651     present_parameters->PresentationInterval = local_parameters.PresentationInterval;
2652
2653     return hr;
2654 }
2655
2656 static const IWineD3DDeviceParentVtbl d3d9_wined3d_device_parent_vtbl =
2657 {
2658     /* IUnknown methods */
2659     device_parent_QueryInterface,
2660     device_parent_AddRef,
2661     device_parent_Release,
2662     /* IWineD3DDeviceParent methods */
2663     device_parent_WineD3DDeviceCreated,
2664     device_parent_CreateSurface,
2665     device_parent_CreateRenderTarget,
2666     device_parent_CreateDepthStencilSurface,
2667     device_parent_CreateVolume,
2668     device_parent_CreateSwapChain,
2669 };
2670
2671 HRESULT device_init(IDirect3DDevice9Impl *device, IWineD3D *wined3d, UINT adapter, D3DDEVTYPE device_type,
2672         HWND focus_window, DWORD flags, D3DPRESENT_PARAMETERS *parameters)
2673 {
2674     WINED3DPRESENT_PARAMETERS *wined3d_parameters;
2675     UINT i, count = 1;
2676     HRESULT hr;
2677
2678     device->lpVtbl = &Direct3DDevice9_Vtbl;
2679     device->device_parent_vtbl = &d3d9_wined3d_device_parent_vtbl;
2680     device->ref = 1;
2681
2682     wined3d_mutex_lock();
2683     hr = IWineD3D_CreateDevice(wined3d, adapter, device_type, focus_window, flags, (IUnknown *)device,
2684             (IWineD3DDeviceParent *)&device->device_parent_vtbl, &device->WineD3DDevice);
2685     if (FAILED(hr))
2686     {
2687         WARN("Failed to create wined3d device, hr %#x.\n", hr);
2688         wined3d_mutex_unlock();
2689         return hr;
2690     }
2691
2692     if (flags & D3DCREATE_ADAPTERGROUP_DEVICE)
2693     {
2694         WINED3DCAPS caps;
2695
2696         IWineD3D_GetDeviceCaps(wined3d, adapter, device_type, &caps);
2697         count = caps.NumberOfAdaptersInGroup;
2698     }
2699
2700     if (flags & D3DCREATE_MULTITHREADED) IWineD3DDevice_SetMultithreaded(device->WineD3DDevice);
2701
2702     wined3d_parameters = HeapAlloc(GetProcessHeap(), 0, sizeof(*wined3d_parameters) * count);
2703     if (!wined3d_parameters)
2704     {
2705         ERR("Failed to allocate wined3d parameters.\n");
2706         IWineD3DDevice_Release(device->WineD3DDevice);
2707         wined3d_mutex_unlock();
2708         return E_OUTOFMEMORY;
2709     }
2710
2711     for (i = 0; i < count; ++i)
2712     {
2713         wined3d_parameters[i].BackBufferWidth = parameters[i].BackBufferWidth;
2714         wined3d_parameters[i].BackBufferHeight = parameters[i].BackBufferHeight;
2715         wined3d_parameters[i].BackBufferFormat = wined3dformat_from_d3dformat(parameters[i].BackBufferFormat);
2716         wined3d_parameters[i].BackBufferCount = parameters[i].BackBufferCount;
2717         wined3d_parameters[i].MultiSampleType = parameters[i].MultiSampleType;
2718         wined3d_parameters[i].MultiSampleQuality = parameters[i].MultiSampleQuality;
2719         wined3d_parameters[i].SwapEffect = parameters[i].SwapEffect;
2720         wined3d_parameters[i].hDeviceWindow = parameters[i].hDeviceWindow;
2721         wined3d_parameters[i].Windowed = parameters[i].Windowed;
2722         wined3d_parameters[i].EnableAutoDepthStencil = parameters[i].EnableAutoDepthStencil;
2723         wined3d_parameters[i].AutoDepthStencilFormat =
2724                 wined3dformat_from_d3dformat(parameters[i].AutoDepthStencilFormat);
2725         wined3d_parameters[i].Flags = parameters[i].Flags;
2726         wined3d_parameters[i].FullScreen_RefreshRateInHz = parameters[i].FullScreen_RefreshRateInHz;
2727         wined3d_parameters[i].PresentationInterval = parameters[i].PresentationInterval;
2728         wined3d_parameters[i].AutoRestoreDisplayMode = TRUE;
2729     }
2730
2731     hr = IWineD3DDevice_Init3D(device->WineD3DDevice, wined3d_parameters);
2732     if (FAILED(hr))
2733     {
2734         WARN("Failed to initialize 3D, hr %#x.\n", hr);
2735         HeapFree(GetProcessHeap(), 0, wined3d_parameters);
2736         IWineD3DDevice_Release(device->WineD3DDevice);
2737         wined3d_mutex_unlock();
2738         return hr;
2739     }
2740
2741     wined3d_mutex_unlock();
2742
2743     for (i = 0; i < count; ++i)
2744     {
2745         parameters[i].BackBufferWidth = wined3d_parameters[i].BackBufferWidth;
2746         parameters[i].BackBufferHeight = wined3d_parameters[i].BackBufferHeight;
2747         parameters[i].BackBufferFormat = d3dformat_from_wined3dformat(wined3d_parameters[i].BackBufferFormat);
2748         parameters[i].BackBufferCount = wined3d_parameters[i].BackBufferCount;
2749         parameters[i].MultiSampleType = wined3d_parameters[i].MultiSampleType;
2750         parameters[i].MultiSampleQuality = wined3d_parameters[i].MultiSampleQuality;
2751         parameters[i].SwapEffect = wined3d_parameters[i].SwapEffect;
2752         parameters[i].hDeviceWindow = wined3d_parameters[i].hDeviceWindow;
2753         parameters[i].Windowed = wined3d_parameters[i].Windowed;
2754         parameters[i].EnableAutoDepthStencil = wined3d_parameters[i].EnableAutoDepthStencil;
2755         parameters[i].AutoDepthStencilFormat =
2756                 d3dformat_from_wined3dformat(wined3d_parameters[i].AutoDepthStencilFormat);
2757         parameters[i].Flags = wined3d_parameters[i].Flags;
2758         parameters[i].FullScreen_RefreshRateInHz = wined3d_parameters[i].FullScreen_RefreshRateInHz;
2759         parameters[i].PresentationInterval = wined3d_parameters[i].PresentationInterval;
2760     }
2761     HeapFree(GetProcessHeap(), 0, wined3d_parameters);
2762
2763     /* Initialize the converted declaration array. This creates a valid pointer
2764      * and when adding decls HeapReAlloc() can be used without further checking. */
2765     device->convertedDecls = HeapAlloc(GetProcessHeap(), 0, 0);
2766     if (!device->convertedDecls)
2767     {
2768         ERR("Failed to allocate FVF vertex declaration map memory.\n");
2769         wined3d_mutex_lock();
2770         IWineD3DDevice_Uninit3D(device->WineD3DDevice, D3D9CB_DestroySwapChain);
2771         IWineD3DDevice_Release(device->WineD3DDevice);
2772         wined3d_mutex_unlock();
2773         return E_OUTOFMEMORY;
2774     }
2775
2776     return D3D_OK;
2777 }