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